xref: /linux/mm/memory.c (revision 9ec23531fd48031d1b6ca5366f5f967d17a8bc28)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/mm/memory.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds /*
81da177e4SLinus Torvalds  * demand-loading started 01.12.91 - seems it is high on the list of
91da177e4SLinus Torvalds  * things wanted, and it should be easy to implement. - Linus
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds /*
131da177e4SLinus Torvalds  * Ok, demand-loading was easy, shared pages a little bit tricker. Shared
141da177e4SLinus Torvalds  * pages started 02.12.91, seems to work. - Linus.
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  * Tested sharing by executing about 30 /bin/sh: under the old kernel it
171da177e4SLinus Torvalds  * would have taken more than the 6M I have free, but it worked well as
181da177e4SLinus Torvalds  * far as I could see.
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  * Also corrected some "invalidate()"s - I wasn't doing enough of them.
211da177e4SLinus Torvalds  */
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds /*
241da177e4SLinus Torvalds  * Real VM (paging to/from disk) started 18.12.91. Much more work and
251da177e4SLinus Torvalds  * thought has to go into this. Oh, well..
261da177e4SLinus Torvalds  * 19.12.91  -  works, somewhat. Sometimes I get faults, don't know why.
271da177e4SLinus Torvalds  *		Found it. Everything seems to work now.
281da177e4SLinus Torvalds  * 20.12.91  -  Ok, making the swap-device changeable like the root.
291da177e4SLinus Torvalds  */
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds /*
321da177e4SLinus Torvalds  * 05.04.94  -  Multi-page memory management added for v1.1.
331da177e4SLinus Torvalds  * 		Idea by Alex Bligh (alex@cconcepts.co.uk)
341da177e4SLinus Torvalds  *
351da177e4SLinus Torvalds  * 16.07.99  -  Support of BIGMEM added by Gerhard Wichert, Siemens AG
361da177e4SLinus Torvalds  *		(Gerhard.Wichert@pdb.siemens.de)
371da177e4SLinus Torvalds  *
381da177e4SLinus Torvalds  * Aug/Sep 2004 Changed to four level page tables (Andi Kleen)
391da177e4SLinus Torvalds  */
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #include <linux/kernel_stat.h>
421da177e4SLinus Torvalds #include <linux/mm.h>
431da177e4SLinus Torvalds #include <linux/hugetlb.h>
441da177e4SLinus Torvalds #include <linux/mman.h>
451da177e4SLinus Torvalds #include <linux/swap.h>
461da177e4SLinus Torvalds #include <linux/highmem.h>
471da177e4SLinus Torvalds #include <linux/pagemap.h>
489a840895SHugh Dickins #include <linux/ksm.h>
491da177e4SLinus Torvalds #include <linux/rmap.h>
50b95f1b31SPaul Gortmaker #include <linux/export.h>
510ff92245SShailabh Nagar #include <linux/delayacct.h>
521da177e4SLinus Torvalds #include <linux/init.h>
53edc79b2aSPeter Zijlstra #include <linux/writeback.h>
548a9f3ccdSBalbir Singh #include <linux/memcontrol.h>
55cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h>
563dc14741SHugh Dickins #include <linux/kallsyms.h>
573dc14741SHugh Dickins #include <linux/swapops.h>
583dc14741SHugh Dickins #include <linux/elf.h>
595a0e3ad6STejun Heo #include <linux/gfp.h>
604daae3b4SMel Gorman #include <linux/migrate.h>
612fbc57c5SAndy Shevchenko #include <linux/string.h>
620abdd7a8SDan Williams #include <linux/dma-debug.h>
631592eef0SKirill A. Shutemov #include <linux/debugfs.h>
641da177e4SLinus Torvalds 
656952b61dSAlexey Dobriyan #include <asm/io.h>
661da177e4SLinus Torvalds #include <asm/pgalloc.h>
671da177e4SLinus Torvalds #include <asm/uaccess.h>
681da177e4SLinus Torvalds #include <asm/tlb.h>
691da177e4SLinus Torvalds #include <asm/tlbflush.h>
701da177e4SLinus Torvalds #include <asm/pgtable.h>
711da177e4SLinus Torvalds 
7242b77728SJan Beulich #include "internal.h"
7342b77728SJan Beulich 
7490572890SPeter Zijlstra #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
7590572890SPeter Zijlstra #warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid.
7675980e97SPeter Zijlstra #endif
7775980e97SPeter Zijlstra 
78d41dee36SAndy Whitcroft #ifndef CONFIG_NEED_MULTIPLE_NODES
791da177e4SLinus Torvalds /* use the per-pgdat data instead for discontigmem - mbligh */
801da177e4SLinus Torvalds unsigned long max_mapnr;
811da177e4SLinus Torvalds struct page *mem_map;
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds EXPORT_SYMBOL(max_mapnr);
841da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map);
851da177e4SLinus Torvalds #endif
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds /*
881da177e4SLinus Torvalds  * A number of key systems in x86 including ioremap() rely on the assumption
891da177e4SLinus Torvalds  * that high_memory defines the upper bound on direct map memory, then end
901da177e4SLinus Torvalds  * of ZONE_NORMAL.  Under CONFIG_DISCONTIG this means that max_low_pfn and
911da177e4SLinus Torvalds  * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL
921da177e4SLinus Torvalds  * and ZONE_HIGHMEM.
931da177e4SLinus Torvalds  */
941da177e4SLinus Torvalds void * high_memory;
951da177e4SLinus Torvalds 
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 
1210b70068eSArd Biesheuvel EXPORT_SYMBOL(zero_pfn);
1220b70068eSArd Biesheuvel 
123a13ea5b7SHugh Dickins /*
124a13ea5b7SHugh Dickins  * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init()
125a13ea5b7SHugh Dickins  */
126a13ea5b7SHugh Dickins static int __init init_zero_pfn(void)
127a13ea5b7SHugh Dickins {
128a13ea5b7SHugh Dickins 	zero_pfn = page_to_pfn(ZERO_PAGE(0));
129a13ea5b7SHugh Dickins 	return 0;
130a13ea5b7SHugh Dickins }
131a13ea5b7SHugh Dickins core_initcall(init_zero_pfn);
132a62eaf15SAndi Kleen 
133d559db08SKAMEZAWA Hiroyuki 
13434e55232SKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING)
13534e55232SKAMEZAWA Hiroyuki 
136ea48cf78SDavid Rientjes void sync_mm_rss(struct mm_struct *mm)
13734e55232SKAMEZAWA Hiroyuki {
13834e55232SKAMEZAWA Hiroyuki 	int i;
13934e55232SKAMEZAWA Hiroyuki 
14034e55232SKAMEZAWA Hiroyuki 	for (i = 0; i < NR_MM_COUNTERS; i++) {
14105af2e10SDavid Rientjes 		if (current->rss_stat.count[i]) {
14205af2e10SDavid Rientjes 			add_mm_counter(mm, i, current->rss_stat.count[i]);
14305af2e10SDavid Rientjes 			current->rss_stat.count[i] = 0;
14434e55232SKAMEZAWA Hiroyuki 		}
14534e55232SKAMEZAWA Hiroyuki 	}
14605af2e10SDavid Rientjes 	current->rss_stat.events = 0;
14734e55232SKAMEZAWA Hiroyuki }
14834e55232SKAMEZAWA Hiroyuki 
14934e55232SKAMEZAWA Hiroyuki static void add_mm_counter_fast(struct mm_struct *mm, int member, int val)
15034e55232SKAMEZAWA Hiroyuki {
15134e55232SKAMEZAWA Hiroyuki 	struct task_struct *task = current;
15234e55232SKAMEZAWA Hiroyuki 
15334e55232SKAMEZAWA Hiroyuki 	if (likely(task->mm == mm))
15434e55232SKAMEZAWA Hiroyuki 		task->rss_stat.count[member] += val;
15534e55232SKAMEZAWA Hiroyuki 	else
15634e55232SKAMEZAWA Hiroyuki 		add_mm_counter(mm, member, val);
15734e55232SKAMEZAWA Hiroyuki }
15834e55232SKAMEZAWA Hiroyuki #define inc_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, 1)
15934e55232SKAMEZAWA Hiroyuki #define dec_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, -1)
16034e55232SKAMEZAWA Hiroyuki 
16134e55232SKAMEZAWA Hiroyuki /* sync counter once per 64 page faults */
16234e55232SKAMEZAWA Hiroyuki #define TASK_RSS_EVENTS_THRESH	(64)
16334e55232SKAMEZAWA Hiroyuki static void check_sync_rss_stat(struct task_struct *task)
16434e55232SKAMEZAWA Hiroyuki {
16534e55232SKAMEZAWA Hiroyuki 	if (unlikely(task != current))
16634e55232SKAMEZAWA Hiroyuki 		return;
16734e55232SKAMEZAWA Hiroyuki 	if (unlikely(task->rss_stat.events++ > TASK_RSS_EVENTS_THRESH))
168ea48cf78SDavid Rientjes 		sync_mm_rss(task->mm);
16934e55232SKAMEZAWA Hiroyuki }
1709547d01bSPeter Zijlstra #else /* SPLIT_RSS_COUNTING */
17134e55232SKAMEZAWA Hiroyuki 
17234e55232SKAMEZAWA Hiroyuki #define inc_mm_counter_fast(mm, member) inc_mm_counter(mm, member)
17334e55232SKAMEZAWA Hiroyuki #define dec_mm_counter_fast(mm, member) dec_mm_counter(mm, member)
17434e55232SKAMEZAWA Hiroyuki 
17534e55232SKAMEZAWA Hiroyuki static void check_sync_rss_stat(struct task_struct *task)
17634e55232SKAMEZAWA Hiroyuki {
17734e55232SKAMEZAWA Hiroyuki }
17834e55232SKAMEZAWA Hiroyuki 
1799547d01bSPeter Zijlstra #endif /* SPLIT_RSS_COUNTING */
1809547d01bSPeter Zijlstra 
1819547d01bSPeter Zijlstra #ifdef HAVE_GENERIC_MMU_GATHER
1829547d01bSPeter Zijlstra 
1839547d01bSPeter Zijlstra static int tlb_next_batch(struct mmu_gather *tlb)
1849547d01bSPeter Zijlstra {
1859547d01bSPeter Zijlstra 	struct mmu_gather_batch *batch;
1869547d01bSPeter Zijlstra 
1879547d01bSPeter Zijlstra 	batch = tlb->active;
1889547d01bSPeter Zijlstra 	if (batch->next) {
1899547d01bSPeter Zijlstra 		tlb->active = batch->next;
1909547d01bSPeter Zijlstra 		return 1;
1919547d01bSPeter Zijlstra 	}
1929547d01bSPeter Zijlstra 
19353a59fc6SMichal Hocko 	if (tlb->batch_count == MAX_GATHER_BATCH_COUNT)
19453a59fc6SMichal Hocko 		return 0;
19553a59fc6SMichal Hocko 
1969547d01bSPeter Zijlstra 	batch = (void *)__get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0);
1979547d01bSPeter Zijlstra 	if (!batch)
1989547d01bSPeter Zijlstra 		return 0;
1999547d01bSPeter Zijlstra 
20053a59fc6SMichal Hocko 	tlb->batch_count++;
2019547d01bSPeter Zijlstra 	batch->next = NULL;
2029547d01bSPeter Zijlstra 	batch->nr   = 0;
2039547d01bSPeter Zijlstra 	batch->max  = MAX_GATHER_BATCH;
2049547d01bSPeter Zijlstra 
2059547d01bSPeter Zijlstra 	tlb->active->next = batch;
2069547d01bSPeter Zijlstra 	tlb->active = batch;
2079547d01bSPeter Zijlstra 
2089547d01bSPeter Zijlstra 	return 1;
2099547d01bSPeter Zijlstra }
2109547d01bSPeter Zijlstra 
2119547d01bSPeter Zijlstra /* tlb_gather_mmu
2129547d01bSPeter Zijlstra  *	Called to initialize an (on-stack) mmu_gather structure for page-table
2139547d01bSPeter Zijlstra  *	tear-down from @mm. The @fullmm argument is used when @mm is without
2149547d01bSPeter Zijlstra  *	users and we're going to destroy the full address space (exit/execve).
2159547d01bSPeter Zijlstra  */
2162b047252SLinus Torvalds void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long start, unsigned long end)
2179547d01bSPeter Zijlstra {
2189547d01bSPeter Zijlstra 	tlb->mm = mm;
2199547d01bSPeter Zijlstra 
2202b047252SLinus Torvalds 	/* Is it from 0 to ~0? */
2212b047252SLinus Torvalds 	tlb->fullmm     = !(start | (end+1));
2221de14c3cSDave Hansen 	tlb->need_flush_all = 0;
2239547d01bSPeter Zijlstra 	tlb->local.next = NULL;
2249547d01bSPeter Zijlstra 	tlb->local.nr   = 0;
2259547d01bSPeter Zijlstra 	tlb->local.max  = ARRAY_SIZE(tlb->__pages);
2269547d01bSPeter Zijlstra 	tlb->active     = &tlb->local;
22753a59fc6SMichal Hocko 	tlb->batch_count = 0;
2289547d01bSPeter Zijlstra 
2299547d01bSPeter Zijlstra #ifdef CONFIG_HAVE_RCU_TABLE_FREE
2309547d01bSPeter Zijlstra 	tlb->batch = NULL;
23134e55232SKAMEZAWA Hiroyuki #endif
232fb7332a9SWill Deacon 
233fb7332a9SWill Deacon 	__tlb_reset_range(tlb);
2349547d01bSPeter Zijlstra }
2359547d01bSPeter Zijlstra 
2361cf35d47SLinus Torvalds static void tlb_flush_mmu_tlbonly(struct mmu_gather *tlb)
2379547d01bSPeter Zijlstra {
238721c21c1SWill Deacon 	if (!tlb->end)
239721c21c1SWill Deacon 		return;
240721c21c1SWill Deacon 
2419547d01bSPeter Zijlstra 	tlb_flush(tlb);
24234ee645eSJoerg Roedel 	mmu_notifier_invalidate_range(tlb->mm, tlb->start, tlb->end);
2439547d01bSPeter Zijlstra #ifdef CONFIG_HAVE_RCU_TABLE_FREE
2449547d01bSPeter Zijlstra 	tlb_table_flush(tlb);
2459547d01bSPeter Zijlstra #endif
246fb7332a9SWill Deacon 	__tlb_reset_range(tlb);
2471cf35d47SLinus Torvalds }
2481cf35d47SLinus Torvalds 
2491cf35d47SLinus Torvalds static void tlb_flush_mmu_free(struct mmu_gather *tlb)
2501cf35d47SLinus Torvalds {
2511cf35d47SLinus Torvalds 	struct mmu_gather_batch *batch;
2529547d01bSPeter Zijlstra 
253721c21c1SWill Deacon 	for (batch = &tlb->local; batch && batch->nr; batch = batch->next) {
2549547d01bSPeter Zijlstra 		free_pages_and_swap_cache(batch->pages, batch->nr);
2559547d01bSPeter Zijlstra 		batch->nr = 0;
2569547d01bSPeter Zijlstra 	}
2579547d01bSPeter Zijlstra 	tlb->active = &tlb->local;
2589547d01bSPeter Zijlstra }
2599547d01bSPeter Zijlstra 
2601cf35d47SLinus Torvalds void tlb_flush_mmu(struct mmu_gather *tlb)
2611cf35d47SLinus Torvalds {
2621cf35d47SLinus Torvalds 	tlb_flush_mmu_tlbonly(tlb);
2631cf35d47SLinus Torvalds 	tlb_flush_mmu_free(tlb);
2641cf35d47SLinus Torvalds }
2651cf35d47SLinus Torvalds 
2669547d01bSPeter Zijlstra /* tlb_finish_mmu
2679547d01bSPeter Zijlstra  *	Called at the end of the shootdown operation to free up any resources
2689547d01bSPeter Zijlstra  *	that were required.
2699547d01bSPeter Zijlstra  */
2709547d01bSPeter Zijlstra void tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end)
2719547d01bSPeter Zijlstra {
2729547d01bSPeter Zijlstra 	struct mmu_gather_batch *batch, *next;
2739547d01bSPeter Zijlstra 
2749547d01bSPeter Zijlstra 	tlb_flush_mmu(tlb);
2759547d01bSPeter Zijlstra 
2769547d01bSPeter Zijlstra 	/* keep the page table cache within bounds */
2779547d01bSPeter Zijlstra 	check_pgt_cache();
2789547d01bSPeter Zijlstra 
2799547d01bSPeter Zijlstra 	for (batch = tlb->local.next; batch; batch = next) {
2809547d01bSPeter Zijlstra 		next = batch->next;
2819547d01bSPeter Zijlstra 		free_pages((unsigned long)batch, 0);
2829547d01bSPeter Zijlstra 	}
2839547d01bSPeter Zijlstra 	tlb->local.next = NULL;
2849547d01bSPeter Zijlstra }
2859547d01bSPeter Zijlstra 
2869547d01bSPeter Zijlstra /* __tlb_remove_page
2879547d01bSPeter Zijlstra  *	Must perform the equivalent to __free_pte(pte_get_and_clear(ptep)), while
2889547d01bSPeter Zijlstra  *	handling the additional races in SMP caused by other CPUs caching valid
2899547d01bSPeter Zijlstra  *	mappings in their TLBs. Returns the number of free page slots left.
2909547d01bSPeter Zijlstra  *	When out of page slots we must call tlb_flush_mmu().
2919547d01bSPeter Zijlstra  */
2929547d01bSPeter Zijlstra int __tlb_remove_page(struct mmu_gather *tlb, struct page *page)
2939547d01bSPeter Zijlstra {
2949547d01bSPeter Zijlstra 	struct mmu_gather_batch *batch;
2959547d01bSPeter Zijlstra 
296fb7332a9SWill Deacon 	VM_BUG_ON(!tlb->end);
2979547d01bSPeter Zijlstra 
2989547d01bSPeter Zijlstra 	batch = tlb->active;
2999547d01bSPeter Zijlstra 	batch->pages[batch->nr++] = page;
3009547d01bSPeter Zijlstra 	if (batch->nr == batch->max) {
3019547d01bSPeter Zijlstra 		if (!tlb_next_batch(tlb))
3029547d01bSPeter Zijlstra 			return 0;
3030b43c3aaSShaohua Li 		batch = tlb->active;
3049547d01bSPeter Zijlstra 	}
305309381feSSasha Levin 	VM_BUG_ON_PAGE(batch->nr > batch->max, page);
3069547d01bSPeter Zijlstra 
3079547d01bSPeter Zijlstra 	return batch->max - batch->nr;
3089547d01bSPeter Zijlstra }
3099547d01bSPeter Zijlstra 
3109547d01bSPeter Zijlstra #endif /* HAVE_GENERIC_MMU_GATHER */
31134e55232SKAMEZAWA Hiroyuki 
31226723911SPeter Zijlstra #ifdef CONFIG_HAVE_RCU_TABLE_FREE
31326723911SPeter Zijlstra 
31426723911SPeter Zijlstra /*
31526723911SPeter Zijlstra  * See the comment near struct mmu_table_batch.
31626723911SPeter Zijlstra  */
31726723911SPeter Zijlstra 
31826723911SPeter Zijlstra static void tlb_remove_table_smp_sync(void *arg)
31926723911SPeter Zijlstra {
32026723911SPeter Zijlstra 	/* Simply deliver the interrupt */
32126723911SPeter Zijlstra }
32226723911SPeter Zijlstra 
32326723911SPeter Zijlstra static void tlb_remove_table_one(void *table)
32426723911SPeter Zijlstra {
32526723911SPeter Zijlstra 	/*
32626723911SPeter Zijlstra 	 * This isn't an RCU grace period and hence the page-tables cannot be
32726723911SPeter Zijlstra 	 * assumed to be actually RCU-freed.
32826723911SPeter Zijlstra 	 *
32926723911SPeter Zijlstra 	 * It is however sufficient for software page-table walkers that rely on
33026723911SPeter Zijlstra 	 * IRQ disabling. See the comment near struct mmu_table_batch.
33126723911SPeter Zijlstra 	 */
33226723911SPeter Zijlstra 	smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
33326723911SPeter Zijlstra 	__tlb_remove_table(table);
33426723911SPeter Zijlstra }
33526723911SPeter Zijlstra 
33626723911SPeter Zijlstra static void tlb_remove_table_rcu(struct rcu_head *head)
33726723911SPeter Zijlstra {
33826723911SPeter Zijlstra 	struct mmu_table_batch *batch;
33926723911SPeter Zijlstra 	int i;
34026723911SPeter Zijlstra 
34126723911SPeter Zijlstra 	batch = container_of(head, struct mmu_table_batch, rcu);
34226723911SPeter Zijlstra 
34326723911SPeter Zijlstra 	for (i = 0; i < batch->nr; i++)
34426723911SPeter Zijlstra 		__tlb_remove_table(batch->tables[i]);
34526723911SPeter Zijlstra 
34626723911SPeter Zijlstra 	free_page((unsigned long)batch);
34726723911SPeter Zijlstra }
34826723911SPeter Zijlstra 
34926723911SPeter Zijlstra void tlb_table_flush(struct mmu_gather *tlb)
35026723911SPeter Zijlstra {
35126723911SPeter Zijlstra 	struct mmu_table_batch **batch = &tlb->batch;
35226723911SPeter Zijlstra 
35326723911SPeter Zijlstra 	if (*batch) {
35426723911SPeter Zijlstra 		call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
35526723911SPeter Zijlstra 		*batch = NULL;
35626723911SPeter Zijlstra 	}
35726723911SPeter Zijlstra }
35826723911SPeter Zijlstra 
35926723911SPeter Zijlstra void tlb_remove_table(struct mmu_gather *tlb, void *table)
36026723911SPeter Zijlstra {
36126723911SPeter Zijlstra 	struct mmu_table_batch **batch = &tlb->batch;
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  * Note: this doesn't free the actual pages themselves. That
3891da177e4SLinus Torvalds  * has been handled earlier when unmapping all the memory regions.
3901da177e4SLinus Torvalds  */
3919e1b32caSBenjamin Herrenschmidt static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd,
3929e1b32caSBenjamin Herrenschmidt 			   unsigned long addr)
3931da177e4SLinus Torvalds {
3942f569afdSMartin Schwidefsky 	pgtable_t token = pmd_pgtable(*pmd);
3951da177e4SLinus Torvalds 	pmd_clear(pmd);
3969e1b32caSBenjamin Herrenschmidt 	pte_free_tlb(tlb, token, addr);
397e1f56c89SKirill A. Shutemov 	atomic_long_dec(&tlb->mm->nr_ptes);
3981da177e4SLinus Torvalds }
3991da177e4SLinus Torvalds 
400e0da382cSHugh Dickins static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
401e0da382cSHugh Dickins 				unsigned long addr, unsigned long end,
402e0da382cSHugh Dickins 				unsigned long floor, unsigned long ceiling)
4031da177e4SLinus Torvalds {
4041da177e4SLinus Torvalds 	pmd_t *pmd;
4051da177e4SLinus Torvalds 	unsigned long next;
406e0da382cSHugh Dickins 	unsigned long start;
4071da177e4SLinus Torvalds 
408e0da382cSHugh Dickins 	start = addr;
4091da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
4101da177e4SLinus Torvalds 	do {
4111da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
4121da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(pmd))
4131da177e4SLinus Torvalds 			continue;
4149e1b32caSBenjamin Herrenschmidt 		free_pte_range(tlb, pmd, addr);
4151da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
4161da177e4SLinus Torvalds 
417e0da382cSHugh Dickins 	start &= PUD_MASK;
418e0da382cSHugh Dickins 	if (start < floor)
419e0da382cSHugh Dickins 		return;
420e0da382cSHugh Dickins 	if (ceiling) {
421e0da382cSHugh Dickins 		ceiling &= PUD_MASK;
422e0da382cSHugh Dickins 		if (!ceiling)
423e0da382cSHugh Dickins 			return;
4241da177e4SLinus Torvalds 	}
425e0da382cSHugh Dickins 	if (end - 1 > ceiling - 1)
426e0da382cSHugh Dickins 		return;
427e0da382cSHugh Dickins 
428e0da382cSHugh Dickins 	pmd = pmd_offset(pud, start);
429e0da382cSHugh Dickins 	pud_clear(pud);
4309e1b32caSBenjamin Herrenschmidt 	pmd_free_tlb(tlb, pmd, start);
431dc6c9a35SKirill A. Shutemov 	mm_dec_nr_pmds(tlb->mm);
4321da177e4SLinus Torvalds }
4331da177e4SLinus Torvalds 
434e0da382cSHugh Dickins static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
435e0da382cSHugh Dickins 				unsigned long addr, unsigned long end,
436e0da382cSHugh Dickins 				unsigned long floor, unsigned long ceiling)
4371da177e4SLinus Torvalds {
4381da177e4SLinus Torvalds 	pud_t *pud;
4391da177e4SLinus Torvalds 	unsigned long next;
440e0da382cSHugh Dickins 	unsigned long start;
4411da177e4SLinus Torvalds 
442e0da382cSHugh Dickins 	start = addr;
4431da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
4441da177e4SLinus Torvalds 	do {
4451da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
4461da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
4471da177e4SLinus Torvalds 			continue;
448e0da382cSHugh Dickins 		free_pmd_range(tlb, pud, addr, next, floor, ceiling);
4491da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
4501da177e4SLinus Torvalds 
451e0da382cSHugh Dickins 	start &= PGDIR_MASK;
452e0da382cSHugh Dickins 	if (start < floor)
453e0da382cSHugh Dickins 		return;
454e0da382cSHugh Dickins 	if (ceiling) {
455e0da382cSHugh Dickins 		ceiling &= PGDIR_MASK;
456e0da382cSHugh Dickins 		if (!ceiling)
457e0da382cSHugh Dickins 			return;
4581da177e4SLinus Torvalds 	}
459e0da382cSHugh Dickins 	if (end - 1 > ceiling - 1)
460e0da382cSHugh Dickins 		return;
461e0da382cSHugh Dickins 
462e0da382cSHugh Dickins 	pud = pud_offset(pgd, start);
463e0da382cSHugh Dickins 	pgd_clear(pgd);
4649e1b32caSBenjamin Herrenschmidt 	pud_free_tlb(tlb, pud, start);
4651da177e4SLinus Torvalds }
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds /*
468e0da382cSHugh Dickins  * This function frees user-level page tables of a process.
4691da177e4SLinus Torvalds  */
47042b77728SJan Beulich void free_pgd_range(struct mmu_gather *tlb,
471e0da382cSHugh Dickins 			unsigned long addr, unsigned long end,
472e0da382cSHugh Dickins 			unsigned long floor, unsigned long ceiling)
4731da177e4SLinus Torvalds {
4741da177e4SLinus Torvalds 	pgd_t *pgd;
4751da177e4SLinus Torvalds 	unsigned long next;
4761da177e4SLinus Torvalds 
477e0da382cSHugh Dickins 	/*
478e0da382cSHugh Dickins 	 * The next few lines have given us lots of grief...
479e0da382cSHugh Dickins 	 *
480e0da382cSHugh Dickins 	 * Why are we testing PMD* at this top level?  Because often
481e0da382cSHugh Dickins 	 * there will be no work to do at all, and we'd prefer not to
482e0da382cSHugh Dickins 	 * go all the way down to the bottom just to discover that.
483e0da382cSHugh Dickins 	 *
484e0da382cSHugh Dickins 	 * Why all these "- 1"s?  Because 0 represents both the bottom
485e0da382cSHugh Dickins 	 * of the address space and the top of it (using -1 for the
486e0da382cSHugh Dickins 	 * top wouldn't help much: the masks would do the wrong thing).
487e0da382cSHugh Dickins 	 * The rule is that addr 0 and floor 0 refer to the bottom of
488e0da382cSHugh Dickins 	 * the address space, but end 0 and ceiling 0 refer to the top
489e0da382cSHugh Dickins 	 * Comparisons need to use "end - 1" and "ceiling - 1" (though
490e0da382cSHugh Dickins 	 * that end 0 case should be mythical).
491e0da382cSHugh Dickins 	 *
492e0da382cSHugh Dickins 	 * Wherever addr is brought up or ceiling brought down, we must
493e0da382cSHugh Dickins 	 * be careful to reject "the opposite 0" before it confuses the
494e0da382cSHugh Dickins 	 * subsequent tests.  But what about where end is brought down
495e0da382cSHugh Dickins 	 * by PMD_SIZE below? no, end can't go down to 0 there.
496e0da382cSHugh Dickins 	 *
497e0da382cSHugh Dickins 	 * Whereas we round start (addr) and ceiling down, by different
498e0da382cSHugh Dickins 	 * masks at different levels, in order to test whether a table
499e0da382cSHugh Dickins 	 * now has no other vmas using it, so can be freed, we don't
500e0da382cSHugh Dickins 	 * bother to round floor or end up - the tests don't need that.
501e0da382cSHugh Dickins 	 */
502e0da382cSHugh Dickins 
503e0da382cSHugh Dickins 	addr &= PMD_MASK;
504e0da382cSHugh Dickins 	if (addr < floor) {
505e0da382cSHugh Dickins 		addr += PMD_SIZE;
506e0da382cSHugh Dickins 		if (!addr)
507e0da382cSHugh Dickins 			return;
508e0da382cSHugh Dickins 	}
509e0da382cSHugh Dickins 	if (ceiling) {
510e0da382cSHugh Dickins 		ceiling &= PMD_MASK;
511e0da382cSHugh Dickins 		if (!ceiling)
512e0da382cSHugh Dickins 			return;
513e0da382cSHugh Dickins 	}
514e0da382cSHugh Dickins 	if (end - 1 > ceiling - 1)
515e0da382cSHugh Dickins 		end -= PMD_SIZE;
516e0da382cSHugh Dickins 	if (addr > end - 1)
517e0da382cSHugh Dickins 		return;
518e0da382cSHugh Dickins 
51942b77728SJan Beulich 	pgd = pgd_offset(tlb->mm, addr);
5201da177e4SLinus Torvalds 	do {
5211da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
5221da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
5231da177e4SLinus Torvalds 			continue;
52442b77728SJan Beulich 		free_pud_range(tlb, pgd, addr, next, floor, ceiling);
5251da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
526e0da382cSHugh Dickins }
527e0da382cSHugh Dickins 
52842b77728SJan Beulich void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma,
529e0da382cSHugh Dickins 		unsigned long floor, unsigned long ceiling)
530e0da382cSHugh Dickins {
531e0da382cSHugh Dickins 	while (vma) {
532e0da382cSHugh Dickins 		struct vm_area_struct *next = vma->vm_next;
533e0da382cSHugh Dickins 		unsigned long addr = vma->vm_start;
534e0da382cSHugh Dickins 
5358f4f8c16SHugh Dickins 		/*
53625d9e2d1Snpiggin@suse.de 		 * Hide vma from rmap and truncate_pagecache before freeing
53725d9e2d1Snpiggin@suse.de 		 * pgtables
5388f4f8c16SHugh Dickins 		 */
5395beb4930SRik van Riel 		unlink_anon_vmas(vma);
5408f4f8c16SHugh Dickins 		unlink_file_vma(vma);
5418f4f8c16SHugh Dickins 
5429da61aefSDavid Gibson 		if (is_vm_hugetlb_page(vma)) {
5433bf5ee95SHugh Dickins 			hugetlb_free_pgd_range(tlb, addr, vma->vm_end,
5443bf5ee95SHugh Dickins 				floor, next? next->vm_start: ceiling);
5453bf5ee95SHugh Dickins 		} else {
5463bf5ee95SHugh Dickins 			/*
5473bf5ee95SHugh Dickins 			 * Optimization: gather nearby vmas into one call down
5483bf5ee95SHugh Dickins 			 */
5493bf5ee95SHugh Dickins 			while (next && next->vm_start <= vma->vm_end + PMD_SIZE
5504866920bSDavid Gibson 			       && !is_vm_hugetlb_page(next)) {
551e0da382cSHugh Dickins 				vma = next;
552e0da382cSHugh Dickins 				next = vma->vm_next;
5535beb4930SRik van Riel 				unlink_anon_vmas(vma);
5548f4f8c16SHugh Dickins 				unlink_file_vma(vma);
555e0da382cSHugh Dickins 			}
5563bf5ee95SHugh Dickins 			free_pgd_range(tlb, addr, vma->vm_end,
557e0da382cSHugh Dickins 				floor, next? next->vm_start: ceiling);
5583bf5ee95SHugh Dickins 		}
559e0da382cSHugh Dickins 		vma = next;
560e0da382cSHugh Dickins 	}
5611da177e4SLinus Torvalds }
5621da177e4SLinus Torvalds 
5638ac1f832SAndrea Arcangeli int __pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma,
5648ac1f832SAndrea Arcangeli 		pmd_t *pmd, unsigned long address)
5651da177e4SLinus Torvalds {
566c4088ebdSKirill A. Shutemov 	spinlock_t *ptl;
5672f569afdSMartin Schwidefsky 	pgtable_t new = pte_alloc_one(mm, address);
5688ac1f832SAndrea Arcangeli 	int wait_split_huge_page;
5691da177e4SLinus Torvalds 	if (!new)
5701bb3630eSHugh Dickins 		return -ENOMEM;
5711bb3630eSHugh Dickins 
572362a61adSNick Piggin 	/*
573362a61adSNick Piggin 	 * Ensure all pte setup (eg. pte page lock and page clearing) are
574362a61adSNick Piggin 	 * visible before the pte is made visible to other CPUs by being
575362a61adSNick Piggin 	 * put into page tables.
576362a61adSNick Piggin 	 *
577362a61adSNick Piggin 	 * The other side of the story is the pointer chasing in the page
578362a61adSNick Piggin 	 * table walking code (when walking the page table without locking;
579362a61adSNick Piggin 	 * ie. most of the time). Fortunately, these data accesses consist
580362a61adSNick Piggin 	 * of a chain of data-dependent loads, meaning most CPUs (alpha
581362a61adSNick Piggin 	 * being the notable exception) will already guarantee loads are
582362a61adSNick Piggin 	 * seen in-order. See the alpha page table accessors for the
583362a61adSNick Piggin 	 * smp_read_barrier_depends() barriers in page table walking code.
584362a61adSNick Piggin 	 */
585362a61adSNick Piggin 	smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */
586362a61adSNick Piggin 
587c4088ebdSKirill A. Shutemov 	ptl = pmd_lock(mm, pmd);
5888ac1f832SAndrea Arcangeli 	wait_split_huge_page = 0;
5898ac1f832SAndrea Arcangeli 	if (likely(pmd_none(*pmd))) {	/* Has another populated it ? */
590e1f56c89SKirill A. Shutemov 		atomic_long_inc(&mm->nr_ptes);
5911da177e4SLinus Torvalds 		pmd_populate(mm, pmd, new);
5922f569afdSMartin Schwidefsky 		new = NULL;
5938ac1f832SAndrea Arcangeli 	} else if (unlikely(pmd_trans_splitting(*pmd)))
5948ac1f832SAndrea Arcangeli 		wait_split_huge_page = 1;
595c4088ebdSKirill A. Shutemov 	spin_unlock(ptl);
5962f569afdSMartin Schwidefsky 	if (new)
5972f569afdSMartin Schwidefsky 		pte_free(mm, new);
5988ac1f832SAndrea Arcangeli 	if (wait_split_huge_page)
5998ac1f832SAndrea Arcangeli 		wait_split_huge_page(vma->anon_vma, pmd);
6001bb3630eSHugh Dickins 	return 0;
6011da177e4SLinus Torvalds }
6021da177e4SLinus Torvalds 
6031bb3630eSHugh Dickins int __pte_alloc_kernel(pmd_t *pmd, unsigned long address)
6041da177e4SLinus Torvalds {
6051bb3630eSHugh Dickins 	pte_t *new = pte_alloc_one_kernel(&init_mm, address);
6061da177e4SLinus Torvalds 	if (!new)
6071bb3630eSHugh Dickins 		return -ENOMEM;
6081da177e4SLinus Torvalds 
609362a61adSNick Piggin 	smp_wmb(); /* See comment in __pte_alloc */
610362a61adSNick Piggin 
611872fec16SHugh Dickins 	spin_lock(&init_mm.page_table_lock);
6128ac1f832SAndrea Arcangeli 	if (likely(pmd_none(*pmd))) {	/* Has another populated it ? */
613872fec16SHugh Dickins 		pmd_populate_kernel(&init_mm, pmd, new);
6142f569afdSMartin Schwidefsky 		new = NULL;
6158ac1f832SAndrea Arcangeli 	} else
6168ac1f832SAndrea Arcangeli 		VM_BUG_ON(pmd_trans_splitting(*pmd));
617872fec16SHugh Dickins 	spin_unlock(&init_mm.page_table_lock);
6182f569afdSMartin Schwidefsky 	if (new)
6192f569afdSMartin Schwidefsky 		pte_free_kernel(&init_mm, new);
6201bb3630eSHugh Dickins 	return 0;
6211da177e4SLinus Torvalds }
6221da177e4SLinus Torvalds 
623d559db08SKAMEZAWA Hiroyuki static inline void init_rss_vec(int *rss)
624ae859762SHugh Dickins {
625d559db08SKAMEZAWA Hiroyuki 	memset(rss, 0, sizeof(int) * NR_MM_COUNTERS);
626d559db08SKAMEZAWA Hiroyuki }
627d559db08SKAMEZAWA Hiroyuki 
628d559db08SKAMEZAWA Hiroyuki static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss)
629d559db08SKAMEZAWA Hiroyuki {
630d559db08SKAMEZAWA Hiroyuki 	int i;
631d559db08SKAMEZAWA Hiroyuki 
63234e55232SKAMEZAWA Hiroyuki 	if (current->mm == mm)
63305af2e10SDavid Rientjes 		sync_mm_rss(mm);
634d559db08SKAMEZAWA Hiroyuki 	for (i = 0; i < NR_MM_COUNTERS; i++)
635d559db08SKAMEZAWA Hiroyuki 		if (rss[i])
636d559db08SKAMEZAWA Hiroyuki 			add_mm_counter(mm, i, rss[i]);
637ae859762SHugh Dickins }
638ae859762SHugh Dickins 
6391da177e4SLinus Torvalds /*
6406aab341eSLinus Torvalds  * This function is called to print an error when a bad pte
6416aab341eSLinus Torvalds  * is found. For example, we might have a PFN-mapped pte in
6426aab341eSLinus Torvalds  * a region that doesn't allow it.
643b5810039SNick Piggin  *
644b5810039SNick Piggin  * The calling function must still handle the error.
645b5810039SNick Piggin  */
6463dc14741SHugh Dickins static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
6473dc14741SHugh Dickins 			  pte_t pte, struct page *page)
648b5810039SNick Piggin {
6493dc14741SHugh Dickins 	pgd_t *pgd = pgd_offset(vma->vm_mm, addr);
6503dc14741SHugh Dickins 	pud_t *pud = pud_offset(pgd, addr);
6513dc14741SHugh Dickins 	pmd_t *pmd = pmd_offset(pud, addr);
6523dc14741SHugh Dickins 	struct address_space *mapping;
6533dc14741SHugh Dickins 	pgoff_t index;
654d936cf9bSHugh Dickins 	static unsigned long resume;
655d936cf9bSHugh Dickins 	static unsigned long nr_shown;
656d936cf9bSHugh Dickins 	static unsigned long nr_unshown;
657d936cf9bSHugh Dickins 
658d936cf9bSHugh Dickins 	/*
659d936cf9bSHugh Dickins 	 * Allow a burst of 60 reports, then keep quiet for that minute;
660d936cf9bSHugh Dickins 	 * or allow a steady drip of one report per second.
661d936cf9bSHugh Dickins 	 */
662d936cf9bSHugh Dickins 	if (nr_shown == 60) {
663d936cf9bSHugh Dickins 		if (time_before(jiffies, resume)) {
664d936cf9bSHugh Dickins 			nr_unshown++;
665d936cf9bSHugh Dickins 			return;
666d936cf9bSHugh Dickins 		}
667d936cf9bSHugh Dickins 		if (nr_unshown) {
6681e9e6365SHugh Dickins 			printk(KERN_ALERT
6691e9e6365SHugh Dickins 				"BUG: Bad page map: %lu messages suppressed\n",
670d936cf9bSHugh Dickins 				nr_unshown);
671d936cf9bSHugh Dickins 			nr_unshown = 0;
672d936cf9bSHugh Dickins 		}
673d936cf9bSHugh Dickins 		nr_shown = 0;
674d936cf9bSHugh Dickins 	}
675d936cf9bSHugh Dickins 	if (nr_shown++ == 0)
676d936cf9bSHugh Dickins 		resume = jiffies + 60 * HZ;
6773dc14741SHugh Dickins 
6783dc14741SHugh Dickins 	mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL;
6793dc14741SHugh Dickins 	index = linear_page_index(vma, addr);
6803dc14741SHugh Dickins 
6811e9e6365SHugh Dickins 	printk(KERN_ALERT
6821e9e6365SHugh Dickins 		"BUG: Bad page map in process %s  pte:%08llx pmd:%08llx\n",
6833dc14741SHugh Dickins 		current->comm,
6843dc14741SHugh Dickins 		(long long)pte_val(pte), (long long)pmd_val(*pmd));
685718a3821SWu Fengguang 	if (page)
686f0b791a3SDave Hansen 		dump_page(page, "bad pte");
6871e9e6365SHugh Dickins 	printk(KERN_ALERT
6883dc14741SHugh Dickins 		"addr:%p vm_flags:%08lx anon_vma:%p mapping:%p index:%lx\n",
6893dc14741SHugh Dickins 		(void *)addr, vma->vm_flags, vma->anon_vma, mapping, index);
6903dc14741SHugh Dickins 	/*
6913dc14741SHugh Dickins 	 * Choose text because data symbols depend on CONFIG_KALLSYMS_ALL=y
6923dc14741SHugh Dickins 	 */
6932682582aSKonstantin Khlebnikov 	pr_alert("file:%pD fault:%pf mmap:%pf readpage:%pf\n",
6942682582aSKonstantin Khlebnikov 		 vma->vm_file,
6952682582aSKonstantin Khlebnikov 		 vma->vm_ops ? vma->vm_ops->fault : NULL,
6962682582aSKonstantin Khlebnikov 		 vma->vm_file ? vma->vm_file->f_op->mmap : NULL,
6972682582aSKonstantin Khlebnikov 		 mapping ? mapping->a_ops->readpage : NULL);
698b5810039SNick Piggin 	dump_stack();
699373d4d09SRusty Russell 	add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
700b5810039SNick Piggin }
701b5810039SNick Piggin 
702b5810039SNick Piggin /*
7037e675137SNick Piggin  * vm_normal_page -- This function gets the "struct page" associated with a pte.
7046aab341eSLinus Torvalds  *
7057e675137SNick Piggin  * "Special" mappings do not wish to be associated with a "struct page" (either
7067e675137SNick Piggin  * it doesn't exist, or it exists but they don't want to touch it). In this
7077e675137SNick Piggin  * case, NULL is returned here. "Normal" mappings do have a struct page.
708b379d790SJared Hulbert  *
7097e675137SNick Piggin  * There are 2 broad cases. Firstly, an architecture may define a pte_special()
7107e675137SNick Piggin  * pte bit, in which case this function is trivial. Secondly, an architecture
7117e675137SNick Piggin  * may not have a spare pte bit, which requires a more complicated scheme,
7127e675137SNick Piggin  * described below.
7137e675137SNick Piggin  *
7147e675137SNick Piggin  * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a
7157e675137SNick Piggin  * special mapping (even if there are underlying and valid "struct pages").
7167e675137SNick Piggin  * COWed pages of a VM_PFNMAP are always normal.
7176aab341eSLinus Torvalds  *
718b379d790SJared Hulbert  * The way we recognize COWed pages within VM_PFNMAP mappings is through the
719b379d790SJared Hulbert  * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit
7207e675137SNick Piggin  * set, and the vm_pgoff will point to the first PFN mapped: thus every special
7217e675137SNick Piggin  * mapping will always honor the rule
7226aab341eSLinus Torvalds  *
7236aab341eSLinus Torvalds  *	pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
7246aab341eSLinus Torvalds  *
7257e675137SNick Piggin  * And for normal mappings this is false.
726b379d790SJared Hulbert  *
7277e675137SNick Piggin  * This restricts such mappings to be a linear translation from virtual address
7287e675137SNick Piggin  * to pfn. To get around this restriction, we allow arbitrary mappings so long
7297e675137SNick Piggin  * as the vma is not a COW mapping; in that case, we know that all ptes are
7307e675137SNick Piggin  * special (because none can have been COWed).
731b379d790SJared Hulbert  *
732b379d790SJared Hulbert  *
7337e675137SNick Piggin  * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP.
7347e675137SNick Piggin  *
735b379d790SJared Hulbert  * VM_MIXEDMAP mappings can likewise contain memory with or without "struct
736b379d790SJared Hulbert  * page" backing, however the difference is that _all_ pages with a struct
737b379d790SJared Hulbert  * page (that is, those where pfn_valid is true) are refcounted and considered
738b379d790SJared Hulbert  * normal pages by the VM. The disadvantage is that pages are refcounted
739b379d790SJared Hulbert  * (which can be slower and simply not an option for some PFNMAP users). The
740b379d790SJared Hulbert  * advantage is that we don't have to follow the strict linearity rule of
741b379d790SJared Hulbert  * PFNMAP mappings in order to support COWable mappings.
742b379d790SJared Hulbert  *
743ee498ed7SHugh Dickins  */
7447e675137SNick Piggin #ifdef __HAVE_ARCH_PTE_SPECIAL
7457e675137SNick Piggin # define HAVE_PTE_SPECIAL 1
7467e675137SNick Piggin #else
7477e675137SNick Piggin # define HAVE_PTE_SPECIAL 0
7487e675137SNick Piggin #endif
7497e675137SNick Piggin struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
7507e675137SNick Piggin 				pte_t pte)
751ee498ed7SHugh Dickins {
75222b31eecSHugh Dickins 	unsigned long pfn = pte_pfn(pte);
7537e675137SNick Piggin 
7547e675137SNick Piggin 	if (HAVE_PTE_SPECIAL) {
755b38af472SHugh Dickins 		if (likely(!pte_special(pte)))
75622b31eecSHugh Dickins 			goto check_pfn;
757667a0a06SDavid Vrabel 		if (vma->vm_ops && vma->vm_ops->find_special_page)
758667a0a06SDavid Vrabel 			return vma->vm_ops->find_special_page(vma, addr);
759a13ea5b7SHugh Dickins 		if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
760a13ea5b7SHugh Dickins 			return NULL;
76162eede62SHugh Dickins 		if (!is_zero_pfn(pfn))
76222b31eecSHugh Dickins 			print_bad_pte(vma, addr, pte, NULL);
7637e675137SNick Piggin 		return NULL;
7647e675137SNick Piggin 	}
7657e675137SNick Piggin 
7667e675137SNick Piggin 	/* !HAVE_PTE_SPECIAL case follows: */
7677e675137SNick Piggin 
768b379d790SJared Hulbert 	if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
769b379d790SJared Hulbert 		if (vma->vm_flags & VM_MIXEDMAP) {
770b379d790SJared Hulbert 			if (!pfn_valid(pfn))
771b379d790SJared Hulbert 				return NULL;
772b379d790SJared Hulbert 			goto out;
773b379d790SJared Hulbert 		} else {
7747e675137SNick Piggin 			unsigned long off;
7757e675137SNick Piggin 			off = (addr - vma->vm_start) >> PAGE_SHIFT;
7766aab341eSLinus Torvalds 			if (pfn == vma->vm_pgoff + off)
7776aab341eSLinus Torvalds 				return NULL;
77867121172SLinus Torvalds 			if (!is_cow_mapping(vma->vm_flags))
779fb155c16SLinus Torvalds 				return NULL;
7806aab341eSLinus Torvalds 		}
781b379d790SJared Hulbert 	}
7826aab341eSLinus Torvalds 
783b38af472SHugh Dickins 	if (is_zero_pfn(pfn))
784b38af472SHugh Dickins 		return NULL;
78522b31eecSHugh Dickins check_pfn:
78622b31eecSHugh Dickins 	if (unlikely(pfn > highest_memmap_pfn)) {
78722b31eecSHugh Dickins 		print_bad_pte(vma, addr, pte, NULL);
78822b31eecSHugh Dickins 		return NULL;
78922b31eecSHugh Dickins 	}
7906aab341eSLinus Torvalds 
7916aab341eSLinus Torvalds 	/*
7927e675137SNick Piggin 	 * NOTE! We still have PageReserved() pages in the page tables.
7937e675137SNick Piggin 	 * eg. VDSO mappings can cause them to exist.
7946aab341eSLinus Torvalds 	 */
795b379d790SJared Hulbert out:
7966aab341eSLinus Torvalds 	return pfn_to_page(pfn);
797ee498ed7SHugh Dickins }
798ee498ed7SHugh Dickins 
799ee498ed7SHugh Dickins /*
8001da177e4SLinus Torvalds  * copy one vm_area from one task to the other. Assumes the page tables
8011da177e4SLinus Torvalds  * already present in the new task to be cleared in the whole range
8021da177e4SLinus Torvalds  * covered by this vma.
8031da177e4SLinus Torvalds  */
8041da177e4SLinus Torvalds 
805570a335bSHugh Dickins static inline unsigned long
8061da177e4SLinus Torvalds copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
807b5810039SNick Piggin 		pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma,
8088c103762SHugh Dickins 		unsigned long addr, int *rss)
8091da177e4SLinus Torvalds {
810b5810039SNick Piggin 	unsigned long vm_flags = vma->vm_flags;
8111da177e4SLinus Torvalds 	pte_t pte = *src_pte;
8121da177e4SLinus Torvalds 	struct page *page;
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds 	/* pte contains position in swap or file, so copy. */
8151da177e4SLinus Torvalds 	if (unlikely(!pte_present(pte))) {
8160697212aSChristoph Lameter 		swp_entry_t entry = pte_to_swp_entry(pte);
8170697212aSChristoph Lameter 
8182022b4d1SHugh Dickins 		if (likely(!non_swap_entry(entry))) {
819570a335bSHugh Dickins 			if (swap_duplicate(entry) < 0)
820570a335bSHugh Dickins 				return entry.val;
821570a335bSHugh Dickins 
8221da177e4SLinus Torvalds 			/* make sure dst_mm is on swapoff's mmlist. */
8231da177e4SLinus Torvalds 			if (unlikely(list_empty(&dst_mm->mmlist))) {
8241da177e4SLinus Torvalds 				spin_lock(&mmlist_lock);
825f412ac08SHugh Dickins 				if (list_empty(&dst_mm->mmlist))
826f412ac08SHugh Dickins 					list_add(&dst_mm->mmlist,
827f412ac08SHugh Dickins 							&src_mm->mmlist);
8281da177e4SLinus Torvalds 				spin_unlock(&mmlist_lock);
8291da177e4SLinus Torvalds 			}
830b084d435SKAMEZAWA Hiroyuki 			rss[MM_SWAPENTS]++;
8312022b4d1SHugh Dickins 		} else if (is_migration_entry(entry)) {
8329f9f1acdSKonstantin Khlebnikov 			page = migration_entry_to_page(entry);
8339f9f1acdSKonstantin Khlebnikov 
8349f9f1acdSKonstantin Khlebnikov 			if (PageAnon(page))
8359f9f1acdSKonstantin Khlebnikov 				rss[MM_ANONPAGES]++;
8369f9f1acdSKonstantin Khlebnikov 			else
8379f9f1acdSKonstantin Khlebnikov 				rss[MM_FILEPAGES]++;
8389f9f1acdSKonstantin Khlebnikov 
8399f9f1acdSKonstantin Khlebnikov 			if (is_write_migration_entry(entry) &&
8400697212aSChristoph Lameter 					is_cow_mapping(vm_flags)) {
8410697212aSChristoph Lameter 				/*
8429f9f1acdSKonstantin Khlebnikov 				 * COW mappings require pages in both
8439f9f1acdSKonstantin Khlebnikov 				 * parent and child to be set to read.
8440697212aSChristoph Lameter 				 */
8450697212aSChristoph Lameter 				make_migration_entry_read(&entry);
8460697212aSChristoph Lameter 				pte = swp_entry_to_pte(entry);
847c3d16e16SCyrill Gorcunov 				if (pte_swp_soft_dirty(*src_pte))
848c3d16e16SCyrill Gorcunov 					pte = pte_swp_mksoft_dirty(pte);
8490697212aSChristoph Lameter 				set_pte_at(src_mm, addr, src_pte, pte);
8500697212aSChristoph Lameter 			}
8511da177e4SLinus Torvalds 		}
852ae859762SHugh Dickins 		goto out_set_pte;
8531da177e4SLinus Torvalds 	}
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 	/*
8561da177e4SLinus Torvalds 	 * If it's a COW mapping, write protect it both
8571da177e4SLinus Torvalds 	 * in the parent and the child
8581da177e4SLinus Torvalds 	 */
85967121172SLinus Torvalds 	if (is_cow_mapping(vm_flags)) {
8601da177e4SLinus Torvalds 		ptep_set_wrprotect(src_mm, addr, src_pte);
8613dc90795SZachary Amsden 		pte = pte_wrprotect(pte);
8621da177e4SLinus Torvalds 	}
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 	/*
8651da177e4SLinus Torvalds 	 * If it's a shared mapping, mark it clean in
8661da177e4SLinus Torvalds 	 * the child
8671da177e4SLinus Torvalds 	 */
8681da177e4SLinus Torvalds 	if (vm_flags & VM_SHARED)
8691da177e4SLinus Torvalds 		pte = pte_mkclean(pte);
8701da177e4SLinus Torvalds 	pte = pte_mkold(pte);
8716aab341eSLinus Torvalds 
8726aab341eSLinus Torvalds 	page = vm_normal_page(vma, addr, pte);
8736aab341eSLinus Torvalds 	if (page) {
8741da177e4SLinus Torvalds 		get_page(page);
87521333b2bSHugh Dickins 		page_dup_rmap(page);
876d559db08SKAMEZAWA Hiroyuki 		if (PageAnon(page))
877d559db08SKAMEZAWA Hiroyuki 			rss[MM_ANONPAGES]++;
878d559db08SKAMEZAWA Hiroyuki 		else
879d559db08SKAMEZAWA Hiroyuki 			rss[MM_FILEPAGES]++;
8806aab341eSLinus Torvalds 	}
881ae859762SHugh Dickins 
882ae859762SHugh Dickins out_set_pte:
883ae859762SHugh Dickins 	set_pte_at(dst_mm, addr, dst_pte, pte);
884570a335bSHugh Dickins 	return 0;
8851da177e4SLinus Torvalds }
8861da177e4SLinus Torvalds 
88721bda264SJerome Marchand static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
8881da177e4SLinus Torvalds 		   pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
8891da177e4SLinus Torvalds 		   unsigned long addr, unsigned long end)
8901da177e4SLinus Torvalds {
891c36987e2SDaisuke Nishimura 	pte_t *orig_src_pte, *orig_dst_pte;
8921da177e4SLinus Torvalds 	pte_t *src_pte, *dst_pte;
893c74df32cSHugh Dickins 	spinlock_t *src_ptl, *dst_ptl;
894e040f218SHugh Dickins 	int progress = 0;
895d559db08SKAMEZAWA Hiroyuki 	int rss[NR_MM_COUNTERS];
896570a335bSHugh Dickins 	swp_entry_t entry = (swp_entry_t){0};
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds again:
899d559db08SKAMEZAWA Hiroyuki 	init_rss_vec(rss);
900d559db08SKAMEZAWA Hiroyuki 
901c74df32cSHugh Dickins 	dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
9021da177e4SLinus Torvalds 	if (!dst_pte)
9031da177e4SLinus Torvalds 		return -ENOMEM;
904ece0e2b6SPeter Zijlstra 	src_pte = pte_offset_map(src_pmd, addr);
9054c21e2f2SHugh Dickins 	src_ptl = pte_lockptr(src_mm, src_pmd);
906f20dc5f7SIngo Molnar 	spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
907c36987e2SDaisuke Nishimura 	orig_src_pte = src_pte;
908c36987e2SDaisuke Nishimura 	orig_dst_pte = dst_pte;
9096606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	do {
9121da177e4SLinus Torvalds 		/*
9131da177e4SLinus Torvalds 		 * We are holding two locks at this point - either of them
9141da177e4SLinus Torvalds 		 * could generate latencies in another task on another CPU.
9151da177e4SLinus Torvalds 		 */
916e040f218SHugh Dickins 		if (progress >= 32) {
917e040f218SHugh Dickins 			progress = 0;
918e040f218SHugh Dickins 			if (need_resched() ||
91995c354feSNick Piggin 			    spin_needbreak(src_ptl) || spin_needbreak(dst_ptl))
9201da177e4SLinus Torvalds 				break;
921e040f218SHugh Dickins 		}
9221da177e4SLinus Torvalds 		if (pte_none(*src_pte)) {
9231da177e4SLinus Torvalds 			progress++;
9241da177e4SLinus Torvalds 			continue;
9251da177e4SLinus Torvalds 		}
926570a335bSHugh Dickins 		entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte,
927570a335bSHugh Dickins 							vma, addr, rss);
928570a335bSHugh Dickins 		if (entry.val)
929570a335bSHugh Dickins 			break;
9301da177e4SLinus Torvalds 		progress += 8;
9311da177e4SLinus Torvalds 	} while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
9321da177e4SLinus Torvalds 
9336606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
934c74df32cSHugh Dickins 	spin_unlock(src_ptl);
935ece0e2b6SPeter Zijlstra 	pte_unmap(orig_src_pte);
936d559db08SKAMEZAWA Hiroyuki 	add_mm_rss_vec(dst_mm, rss);
937c36987e2SDaisuke Nishimura 	pte_unmap_unlock(orig_dst_pte, dst_ptl);
938c74df32cSHugh Dickins 	cond_resched();
939570a335bSHugh Dickins 
940570a335bSHugh Dickins 	if (entry.val) {
941570a335bSHugh Dickins 		if (add_swap_count_continuation(entry, GFP_KERNEL) < 0)
942570a335bSHugh Dickins 			return -ENOMEM;
943570a335bSHugh Dickins 		progress = 0;
944570a335bSHugh Dickins 	}
9451da177e4SLinus Torvalds 	if (addr != end)
9461da177e4SLinus Torvalds 		goto again;
9471da177e4SLinus Torvalds 	return 0;
9481da177e4SLinus Torvalds }
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
9511da177e4SLinus Torvalds 		pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma,
9521da177e4SLinus Torvalds 		unsigned long addr, unsigned long end)
9531da177e4SLinus Torvalds {
9541da177e4SLinus Torvalds 	pmd_t *src_pmd, *dst_pmd;
9551da177e4SLinus Torvalds 	unsigned long next;
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 	dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
9581da177e4SLinus Torvalds 	if (!dst_pmd)
9591da177e4SLinus Torvalds 		return -ENOMEM;
9601da177e4SLinus Torvalds 	src_pmd = pmd_offset(src_pud, addr);
9611da177e4SLinus Torvalds 	do {
9621da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
96371e3aac0SAndrea Arcangeli 		if (pmd_trans_huge(*src_pmd)) {
96471e3aac0SAndrea Arcangeli 			int err;
96514d1a55cSAndrea Arcangeli 			VM_BUG_ON(next-addr != HPAGE_PMD_SIZE);
96671e3aac0SAndrea Arcangeli 			err = copy_huge_pmd(dst_mm, src_mm,
96771e3aac0SAndrea Arcangeli 					    dst_pmd, src_pmd, addr, vma);
96871e3aac0SAndrea Arcangeli 			if (err == -ENOMEM)
96971e3aac0SAndrea Arcangeli 				return -ENOMEM;
97071e3aac0SAndrea Arcangeli 			if (!err)
97171e3aac0SAndrea Arcangeli 				continue;
97271e3aac0SAndrea Arcangeli 			/* fall through */
97371e3aac0SAndrea Arcangeli 		}
9741da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(src_pmd))
9751da177e4SLinus Torvalds 			continue;
9761da177e4SLinus Torvalds 		if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd,
9771da177e4SLinus Torvalds 						vma, addr, next))
9781da177e4SLinus Torvalds 			return -ENOMEM;
9791da177e4SLinus Torvalds 	} while (dst_pmd++, src_pmd++, addr = next, addr != end);
9801da177e4SLinus Torvalds 	return 0;
9811da177e4SLinus Torvalds }
9821da177e4SLinus Torvalds 
9831da177e4SLinus Torvalds static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
9841da177e4SLinus Torvalds 		pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma,
9851da177e4SLinus Torvalds 		unsigned long addr, unsigned long end)
9861da177e4SLinus Torvalds {
9871da177e4SLinus Torvalds 	pud_t *src_pud, *dst_pud;
9881da177e4SLinus Torvalds 	unsigned long next;
9891da177e4SLinus Torvalds 
9901da177e4SLinus Torvalds 	dst_pud = pud_alloc(dst_mm, dst_pgd, addr);
9911da177e4SLinus Torvalds 	if (!dst_pud)
9921da177e4SLinus Torvalds 		return -ENOMEM;
9931da177e4SLinus Torvalds 	src_pud = pud_offset(src_pgd, addr);
9941da177e4SLinus Torvalds 	do {
9951da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
9961da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(src_pud))
9971da177e4SLinus Torvalds 			continue;
9981da177e4SLinus Torvalds 		if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud,
9991da177e4SLinus Torvalds 						vma, addr, next))
10001da177e4SLinus Torvalds 			return -ENOMEM;
10011da177e4SLinus Torvalds 	} while (dst_pud++, src_pud++, addr = next, addr != end);
10021da177e4SLinus Torvalds 	return 0;
10031da177e4SLinus Torvalds }
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
10061da177e4SLinus Torvalds 		struct vm_area_struct *vma)
10071da177e4SLinus Torvalds {
10081da177e4SLinus Torvalds 	pgd_t *src_pgd, *dst_pgd;
10091da177e4SLinus Torvalds 	unsigned long next;
10101da177e4SLinus Torvalds 	unsigned long addr = vma->vm_start;
10111da177e4SLinus Torvalds 	unsigned long end = vma->vm_end;
10122ec74c3eSSagi Grimberg 	unsigned long mmun_start;	/* For mmu_notifiers */
10132ec74c3eSSagi Grimberg 	unsigned long mmun_end;		/* For mmu_notifiers */
10142ec74c3eSSagi Grimberg 	bool is_cow;
1015cddb8a5cSAndrea Arcangeli 	int ret;
10161da177e4SLinus Torvalds 
1017d992895bSNick Piggin 	/*
1018d992895bSNick Piggin 	 * Don't copy ptes where a page fault will fill them correctly.
1019d992895bSNick Piggin 	 * Fork becomes much lighter when there are big shared or private
1020d992895bSNick Piggin 	 * readonly mappings. The tradeoff is that copy_page_range is more
1021d992895bSNick Piggin 	 * efficient than faulting.
1022d992895bSNick Piggin 	 */
10230661a336SKirill A. Shutemov 	if (!(vma->vm_flags & (VM_HUGETLB | VM_PFNMAP | VM_MIXEDMAP)) &&
10240661a336SKirill A. Shutemov 			!vma->anon_vma)
1025d992895bSNick Piggin 		return 0;
1026d992895bSNick Piggin 
10271da177e4SLinus Torvalds 	if (is_vm_hugetlb_page(vma))
10281da177e4SLinus Torvalds 		return copy_hugetlb_page_range(dst_mm, src_mm, vma);
10291da177e4SLinus Torvalds 
1030b3b9c293SKonstantin Khlebnikov 	if (unlikely(vma->vm_flags & VM_PFNMAP)) {
10312ab64037Svenkatesh.pallipadi@intel.com 		/*
10322ab64037Svenkatesh.pallipadi@intel.com 		 * We do not free on error cases below as remove_vma
10332ab64037Svenkatesh.pallipadi@intel.com 		 * gets called on error from higher level routine
10342ab64037Svenkatesh.pallipadi@intel.com 		 */
10355180da41SSuresh Siddha 		ret = track_pfn_copy(vma);
10362ab64037Svenkatesh.pallipadi@intel.com 		if (ret)
10372ab64037Svenkatesh.pallipadi@intel.com 			return ret;
10382ab64037Svenkatesh.pallipadi@intel.com 	}
10392ab64037Svenkatesh.pallipadi@intel.com 
1040cddb8a5cSAndrea Arcangeli 	/*
1041cddb8a5cSAndrea Arcangeli 	 * We need to invalidate the secondary MMU mappings only when
1042cddb8a5cSAndrea Arcangeli 	 * there could be a permission downgrade on the ptes of the
1043cddb8a5cSAndrea Arcangeli 	 * parent mm. And a permission downgrade will only happen if
1044cddb8a5cSAndrea Arcangeli 	 * is_cow_mapping() returns true.
1045cddb8a5cSAndrea Arcangeli 	 */
10462ec74c3eSSagi Grimberg 	is_cow = is_cow_mapping(vma->vm_flags);
10472ec74c3eSSagi Grimberg 	mmun_start = addr;
10482ec74c3eSSagi Grimberg 	mmun_end   = end;
10492ec74c3eSSagi Grimberg 	if (is_cow)
10502ec74c3eSSagi Grimberg 		mmu_notifier_invalidate_range_start(src_mm, mmun_start,
10512ec74c3eSSagi Grimberg 						    mmun_end);
1052cddb8a5cSAndrea Arcangeli 
1053cddb8a5cSAndrea Arcangeli 	ret = 0;
10541da177e4SLinus Torvalds 	dst_pgd = pgd_offset(dst_mm, addr);
10551da177e4SLinus Torvalds 	src_pgd = pgd_offset(src_mm, addr);
10561da177e4SLinus Torvalds 	do {
10571da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
10581da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(src_pgd))
10591da177e4SLinus Torvalds 			continue;
1060cddb8a5cSAndrea Arcangeli 		if (unlikely(copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd,
1061cddb8a5cSAndrea Arcangeli 					    vma, addr, next))) {
1062cddb8a5cSAndrea Arcangeli 			ret = -ENOMEM;
1063cddb8a5cSAndrea Arcangeli 			break;
1064cddb8a5cSAndrea Arcangeli 		}
10651da177e4SLinus Torvalds 	} while (dst_pgd++, src_pgd++, addr = next, addr != end);
1066cddb8a5cSAndrea Arcangeli 
10672ec74c3eSSagi Grimberg 	if (is_cow)
10682ec74c3eSSagi Grimberg 		mmu_notifier_invalidate_range_end(src_mm, mmun_start, mmun_end);
1069cddb8a5cSAndrea Arcangeli 	return ret;
10701da177e4SLinus Torvalds }
10711da177e4SLinus Torvalds 
107251c6f666SRobin Holt static unsigned long zap_pte_range(struct mmu_gather *tlb,
1073b5810039SNick Piggin 				struct vm_area_struct *vma, pmd_t *pmd,
10741da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
107597a89413SPeter Zijlstra 				struct zap_details *details)
10761da177e4SLinus Torvalds {
1077b5810039SNick Piggin 	struct mm_struct *mm = tlb->mm;
1078d16dfc55SPeter Zijlstra 	int force_flush = 0;
1079d559db08SKAMEZAWA Hiroyuki 	int rss[NR_MM_COUNTERS];
108097a89413SPeter Zijlstra 	spinlock_t *ptl;
10815f1a1907SSteven Rostedt 	pte_t *start_pte;
108297a89413SPeter Zijlstra 	pte_t *pte;
10838a5f14a2SKirill A. Shutemov 	swp_entry_t entry;
1084d559db08SKAMEZAWA Hiroyuki 
1085d16dfc55SPeter Zijlstra again:
1086e303297eSPeter Zijlstra 	init_rss_vec(rss);
10875f1a1907SSteven Rostedt 	start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
10885f1a1907SSteven Rostedt 	pte = start_pte;
10896606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
10901da177e4SLinus Torvalds 	do {
10911da177e4SLinus Torvalds 		pte_t ptent = *pte;
109251c6f666SRobin Holt 		if (pte_none(ptent)) {
10931da177e4SLinus Torvalds 			continue;
109451c6f666SRobin Holt 		}
109551c6f666SRobin Holt 
10966f5e6b9eSHugh Dickins 		if (pte_present(ptent)) {
10976f5e6b9eSHugh Dickins 			struct page *page;
10986f5e6b9eSHugh Dickins 
10996aab341eSLinus Torvalds 			page = vm_normal_page(vma, addr, ptent);
11001da177e4SLinus Torvalds 			if (unlikely(details) && page) {
11011da177e4SLinus Torvalds 				/*
11021da177e4SLinus Torvalds 				 * unmap_shared_mapping_pages() wants to
11031da177e4SLinus Torvalds 				 * invalidate cache without truncating:
11041da177e4SLinus Torvalds 				 * unmap shared but keep private pages.
11051da177e4SLinus Torvalds 				 */
11061da177e4SLinus Torvalds 				if (details->check_mapping &&
11071da177e4SLinus Torvalds 				    details->check_mapping != page->mapping)
11081da177e4SLinus Torvalds 					continue;
11091da177e4SLinus Torvalds 			}
1110b5810039SNick Piggin 			ptent = ptep_get_and_clear_full(mm, addr, pte,
1111a600388dSZachary Amsden 							tlb->fullmm);
11121da177e4SLinus Torvalds 			tlb_remove_tlb_entry(tlb, pte, addr);
11131da177e4SLinus Torvalds 			if (unlikely(!page))
11141da177e4SLinus Torvalds 				continue;
11151da177e4SLinus Torvalds 			if (PageAnon(page))
1116d559db08SKAMEZAWA Hiroyuki 				rss[MM_ANONPAGES]--;
11176237bcd9SHugh Dickins 			else {
11181cf35d47SLinus Torvalds 				if (pte_dirty(ptent)) {
11191cf35d47SLinus Torvalds 					force_flush = 1;
11206237bcd9SHugh Dickins 					set_page_dirty(page);
11211cf35d47SLinus Torvalds 				}
11224917e5d0SJohannes Weiner 				if (pte_young(ptent) &&
112364363aadSJoe Perches 				    likely(!(vma->vm_flags & VM_SEQ_READ)))
1124bf3f3bc5SNick Piggin 					mark_page_accessed(page);
1125d559db08SKAMEZAWA Hiroyuki 				rss[MM_FILEPAGES]--;
11266237bcd9SHugh Dickins 			}
1127edc315fdSHugh Dickins 			page_remove_rmap(page);
11283dc14741SHugh Dickins 			if (unlikely(page_mapcount(page) < 0))
11293dc14741SHugh Dickins 				print_bad_pte(vma, addr, ptent, page);
11301cf35d47SLinus Torvalds 			if (unlikely(!__tlb_remove_page(tlb, page))) {
11311cf35d47SLinus Torvalds 				force_flush = 1;
1132ce9ec37bSWill Deacon 				addr += PAGE_SIZE;
1133d16dfc55SPeter Zijlstra 				break;
11341cf35d47SLinus Torvalds 			}
11351da177e4SLinus Torvalds 			continue;
11361da177e4SLinus Torvalds 		}
11378a5f14a2SKirill A. Shutemov 		/* If details->check_mapping, we leave swap entries. */
11381da177e4SLinus Torvalds 		if (unlikely(details))
11391da177e4SLinus Torvalds 			continue;
1140b084d435SKAMEZAWA Hiroyuki 
11418a5f14a2SKirill A. Shutemov 		entry = pte_to_swp_entry(ptent);
1142b084d435SKAMEZAWA Hiroyuki 		if (!non_swap_entry(entry))
1143b084d435SKAMEZAWA Hiroyuki 			rss[MM_SWAPENTS]--;
11449f9f1acdSKonstantin Khlebnikov 		else if (is_migration_entry(entry)) {
11459f9f1acdSKonstantin Khlebnikov 			struct page *page;
11469f9f1acdSKonstantin Khlebnikov 
11479f9f1acdSKonstantin Khlebnikov 			page = migration_entry_to_page(entry);
11489f9f1acdSKonstantin Khlebnikov 
11499f9f1acdSKonstantin Khlebnikov 			if (PageAnon(page))
11509f9f1acdSKonstantin Khlebnikov 				rss[MM_ANONPAGES]--;
11519f9f1acdSKonstantin Khlebnikov 			else
11529f9f1acdSKonstantin Khlebnikov 				rss[MM_FILEPAGES]--;
11539f9f1acdSKonstantin Khlebnikov 		}
1154b084d435SKAMEZAWA Hiroyuki 		if (unlikely(!free_swap_and_cache(entry)))
11552509ef26SHugh Dickins 			print_bad_pte(vma, addr, ptent, NULL);
11569888a1caSZachary Amsden 		pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
115797a89413SPeter Zijlstra 	} while (pte++, addr += PAGE_SIZE, addr != end);
1158ae859762SHugh Dickins 
1159d559db08SKAMEZAWA Hiroyuki 	add_mm_rss_vec(mm, rss);
11606606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
116151c6f666SRobin Holt 
11621cf35d47SLinus Torvalds 	/* Do the actual TLB flush before dropping ptl */
1163fb7332a9SWill Deacon 	if (force_flush)
11641cf35d47SLinus Torvalds 		tlb_flush_mmu_tlbonly(tlb);
11651cf35d47SLinus Torvalds 	pte_unmap_unlock(start_pte, ptl);
11661cf35d47SLinus Torvalds 
11671cf35d47SLinus Torvalds 	/*
11681cf35d47SLinus Torvalds 	 * If we forced a TLB flush (either due to running out of
11691cf35d47SLinus Torvalds 	 * batch buffers or because we needed to flush dirty TLB
11701cf35d47SLinus Torvalds 	 * entries before releasing the ptl), free the batched
11711cf35d47SLinus Torvalds 	 * memory too. Restart if we didn't do everything.
11721cf35d47SLinus Torvalds 	 */
11731cf35d47SLinus Torvalds 	if (force_flush) {
11741cf35d47SLinus Torvalds 		force_flush = 0;
11751cf35d47SLinus Torvalds 		tlb_flush_mmu_free(tlb);
11762b047252SLinus Torvalds 
11772b047252SLinus Torvalds 		if (addr != end)
1178d16dfc55SPeter Zijlstra 			goto again;
1179d16dfc55SPeter Zijlstra 	}
1180d16dfc55SPeter Zijlstra 
118151c6f666SRobin Holt 	return addr;
11821da177e4SLinus Torvalds }
11831da177e4SLinus Torvalds 
118451c6f666SRobin Holt static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
1185b5810039SNick Piggin 				struct vm_area_struct *vma, pud_t *pud,
11861da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
118797a89413SPeter Zijlstra 				struct zap_details *details)
11881da177e4SLinus Torvalds {
11891da177e4SLinus Torvalds 	pmd_t *pmd;
11901da177e4SLinus Torvalds 	unsigned long next;
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
11931da177e4SLinus Torvalds 	do {
11941da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
119571e3aac0SAndrea Arcangeli 		if (pmd_trans_huge(*pmd)) {
119614d1a55cSAndrea Arcangeli 			if (next - addr != HPAGE_PMD_SIZE) {
1197e0897d75SDavid Rientjes #ifdef CONFIG_DEBUG_VM
1198e0897d75SDavid Rientjes 				if (!rwsem_is_locked(&tlb->mm->mmap_sem)) {
1199e0897d75SDavid 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",
1200e0897d75SDavid Rientjes 						__func__, addr, end,
1201e0897d75SDavid Rientjes 						vma->vm_start,
1202e0897d75SDavid Rientjes 						vma->vm_end);
1203e0897d75SDavid Rientjes 					BUG();
1204e0897d75SDavid Rientjes 				}
1205e0897d75SDavid Rientjes #endif
1206e180377fSKirill A. Shutemov 				split_huge_page_pmd(vma, addr, pmd);
1207f21760b1SShaohua Li 			} else if (zap_huge_pmd(tlb, vma, pmd, addr))
12081a5a9906SAndrea Arcangeli 				goto next;
120971e3aac0SAndrea Arcangeli 			/* fall through */
121071e3aac0SAndrea Arcangeli 		}
12111a5a9906SAndrea Arcangeli 		/*
12121a5a9906SAndrea Arcangeli 		 * Here there can be other concurrent MADV_DONTNEED or
12131a5a9906SAndrea Arcangeli 		 * trans huge page faults running, and if the pmd is
12141a5a9906SAndrea Arcangeli 		 * none or trans huge it can change under us. This is
12151a5a9906SAndrea Arcangeli 		 * because MADV_DONTNEED holds the mmap_sem in read
12161a5a9906SAndrea Arcangeli 		 * mode.
12171a5a9906SAndrea Arcangeli 		 */
12181a5a9906SAndrea Arcangeli 		if (pmd_none_or_trans_huge_or_clear_bad(pmd))
12191a5a9906SAndrea Arcangeli 			goto next;
122097a89413SPeter Zijlstra 		next = zap_pte_range(tlb, vma, pmd, addr, next, details);
12211a5a9906SAndrea Arcangeli next:
122297a89413SPeter Zijlstra 		cond_resched();
122397a89413SPeter Zijlstra 	} while (pmd++, addr = next, addr != end);
122451c6f666SRobin Holt 
122551c6f666SRobin Holt 	return addr;
12261da177e4SLinus Torvalds }
12271da177e4SLinus Torvalds 
122851c6f666SRobin Holt static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
1229b5810039SNick Piggin 				struct vm_area_struct *vma, pgd_t *pgd,
12301da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
123197a89413SPeter Zijlstra 				struct zap_details *details)
12321da177e4SLinus Torvalds {
12331da177e4SLinus Torvalds 	pud_t *pud;
12341da177e4SLinus Torvalds 	unsigned long next;
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
12371da177e4SLinus Torvalds 	do {
12381da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
123997a89413SPeter Zijlstra 		if (pud_none_or_clear_bad(pud))
12401da177e4SLinus Torvalds 			continue;
124197a89413SPeter Zijlstra 		next = zap_pmd_range(tlb, vma, pud, addr, next, details);
124297a89413SPeter Zijlstra 	} while (pud++, addr = next, addr != end);
124351c6f666SRobin Holt 
124451c6f666SRobin Holt 	return addr;
12451da177e4SLinus Torvalds }
12461da177e4SLinus Torvalds 
1247038c7aa1SAl Viro static void unmap_page_range(struct mmu_gather *tlb,
124851c6f666SRobin Holt 			     struct vm_area_struct *vma,
12491da177e4SLinus Torvalds 			     unsigned long addr, unsigned long end,
125097a89413SPeter Zijlstra 			     struct zap_details *details)
12511da177e4SLinus Torvalds {
12521da177e4SLinus Torvalds 	pgd_t *pgd;
12531da177e4SLinus Torvalds 	unsigned long next;
12541da177e4SLinus Torvalds 
12558a5f14a2SKirill A. Shutemov 	if (details && !details->check_mapping)
12561da177e4SLinus Torvalds 		details = NULL;
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds 	BUG_ON(addr >= end);
12591da177e4SLinus Torvalds 	tlb_start_vma(tlb, vma);
12601da177e4SLinus Torvalds 	pgd = pgd_offset(vma->vm_mm, addr);
12611da177e4SLinus Torvalds 	do {
12621da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
126397a89413SPeter Zijlstra 		if (pgd_none_or_clear_bad(pgd))
12641da177e4SLinus Torvalds 			continue;
126597a89413SPeter Zijlstra 		next = zap_pud_range(tlb, vma, pgd, addr, next, details);
126697a89413SPeter Zijlstra 	} while (pgd++, addr = next, addr != end);
12671da177e4SLinus Torvalds 	tlb_end_vma(tlb, vma);
12681da177e4SLinus Torvalds }
12691da177e4SLinus Torvalds 
1270f5cc4eefSAl Viro 
1271f5cc4eefSAl Viro static void unmap_single_vma(struct mmu_gather *tlb,
12721da177e4SLinus Torvalds 		struct vm_area_struct *vma, unsigned long start_addr,
12734f74d2c8SLinus Torvalds 		unsigned long end_addr,
12741da177e4SLinus Torvalds 		struct zap_details *details)
12751da177e4SLinus Torvalds {
1276f5cc4eefSAl Viro 	unsigned long start = max(vma->vm_start, start_addr);
12771da177e4SLinus Torvalds 	unsigned long end;
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds 	if (start >= vma->vm_end)
1280f5cc4eefSAl Viro 		return;
12811da177e4SLinus Torvalds 	end = min(vma->vm_end, end_addr);
12821da177e4SLinus Torvalds 	if (end <= vma->vm_start)
1283f5cc4eefSAl Viro 		return;
12841da177e4SLinus Torvalds 
1285cbc91f71SSrikar Dronamraju 	if (vma->vm_file)
1286cbc91f71SSrikar Dronamraju 		uprobe_munmap(vma, start, end);
1287cbc91f71SSrikar Dronamraju 
1288b3b9c293SKonstantin Khlebnikov 	if (unlikely(vma->vm_flags & VM_PFNMAP))
12895180da41SSuresh Siddha 		untrack_pfn(vma, 0, 0);
12902ab64037Svenkatesh.pallipadi@intel.com 
12918b2a1238SAl Viro 	if (start != end) {
129251c6f666SRobin Holt 		if (unlikely(is_vm_hugetlb_page(vma))) {
1293a137e1ccSAndi Kleen 			/*
1294a137e1ccSAndi Kleen 			 * It is undesirable to test vma->vm_file as it
1295a137e1ccSAndi Kleen 			 * should be non-null for valid hugetlb area.
1296a137e1ccSAndi Kleen 			 * However, vm_file will be NULL in the error
12977aa6b4adSDavidlohr Bueso 			 * cleanup path of mmap_region. When
1298a137e1ccSAndi Kleen 			 * hugetlbfs ->mmap method fails,
12997aa6b4adSDavidlohr Bueso 			 * mmap_region() nullifies vma->vm_file
1300a137e1ccSAndi Kleen 			 * before calling this function to clean up.
1301a137e1ccSAndi Kleen 			 * Since no pte has actually been setup, it is
1302a137e1ccSAndi Kleen 			 * safe to do nothing in this case.
1303a137e1ccSAndi Kleen 			 */
130424669e58SAneesh Kumar K.V 			if (vma->vm_file) {
130583cde9e8SDavidlohr Bueso 				i_mmap_lock_write(vma->vm_file->f_mapping);
1306d833352aSMel Gorman 				__unmap_hugepage_range_final(tlb, vma, start, end, NULL);
130783cde9e8SDavidlohr Bueso 				i_mmap_unlock_write(vma->vm_file->f_mapping);
130824669e58SAneesh Kumar K.V 			}
130951c6f666SRobin Holt 		} else
1310038c7aa1SAl Viro 			unmap_page_range(tlb, vma, start, end, details);
131197a89413SPeter Zijlstra 	}
131251c6f666SRobin Holt }
13131da177e4SLinus Torvalds 
1314f5cc4eefSAl Viro /**
1315f5cc4eefSAl Viro  * unmap_vmas - unmap a range of memory covered by a list of vma's
1316f5cc4eefSAl Viro  * @tlb: address of the caller's struct mmu_gather
1317f5cc4eefSAl Viro  * @vma: the starting vma
1318f5cc4eefSAl Viro  * @start_addr: virtual address at which to start unmapping
1319f5cc4eefSAl Viro  * @end_addr: virtual address at which to end unmapping
1320f5cc4eefSAl Viro  *
1321f5cc4eefSAl Viro  * Unmap all pages in the vma list.
1322f5cc4eefSAl Viro  *
1323f5cc4eefSAl Viro  * Only addresses between `start' and `end' will be unmapped.
1324f5cc4eefSAl Viro  *
1325f5cc4eefSAl Viro  * The VMA list must be sorted in ascending virtual address order.
1326f5cc4eefSAl Viro  *
1327f5cc4eefSAl Viro  * unmap_vmas() assumes that the caller will flush the whole unmapped address
1328f5cc4eefSAl Viro  * range after unmap_vmas() returns.  So the only responsibility here is to
1329f5cc4eefSAl Viro  * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
1330f5cc4eefSAl Viro  * drops the lock and schedules.
1331f5cc4eefSAl Viro  */
1332f5cc4eefSAl Viro void unmap_vmas(struct mmu_gather *tlb,
1333f5cc4eefSAl Viro 		struct vm_area_struct *vma, unsigned long start_addr,
13344f74d2c8SLinus Torvalds 		unsigned long end_addr)
1335f5cc4eefSAl Viro {
1336f5cc4eefSAl Viro 	struct mm_struct *mm = vma->vm_mm;
1337f5cc4eefSAl Viro 
1338f5cc4eefSAl Viro 	mmu_notifier_invalidate_range_start(mm, start_addr, end_addr);
1339f5cc4eefSAl Viro 	for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next)
13404f74d2c8SLinus Torvalds 		unmap_single_vma(tlb, vma, start_addr, end_addr, NULL);
1341cddb8a5cSAndrea Arcangeli 	mmu_notifier_invalidate_range_end(mm, start_addr, end_addr);
13421da177e4SLinus Torvalds }
13431da177e4SLinus Torvalds 
13441da177e4SLinus Torvalds /**
13451da177e4SLinus Torvalds  * zap_page_range - remove user pages in a given range
13461da177e4SLinus Torvalds  * @vma: vm_area_struct holding the applicable pages
1347eb4546bbSRandy Dunlap  * @start: starting address of pages to zap
13481da177e4SLinus Torvalds  * @size: number of bytes to zap
13498a5f14a2SKirill A. Shutemov  * @details: details of shared cache invalidation
1350f5cc4eefSAl Viro  *
1351f5cc4eefSAl Viro  * Caller must protect the VMA list
13521da177e4SLinus Torvalds  */
13537e027b14SLinus Torvalds void zap_page_range(struct vm_area_struct *vma, unsigned long start,
13541da177e4SLinus Torvalds 		unsigned long size, struct zap_details *details)
13551da177e4SLinus Torvalds {
13561da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
1357d16dfc55SPeter Zijlstra 	struct mmu_gather tlb;
13587e027b14SLinus Torvalds 	unsigned long end = start + size;
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds 	lru_add_drain();
13612b047252SLinus Torvalds 	tlb_gather_mmu(&tlb, mm, start, end);
1362365e9c87SHugh Dickins 	update_hiwater_rss(mm);
13637e027b14SLinus Torvalds 	mmu_notifier_invalidate_range_start(mm, start, end);
13647e027b14SLinus Torvalds 	for ( ; vma && vma->vm_start < end; vma = vma->vm_next)
13654f74d2c8SLinus Torvalds 		unmap_single_vma(&tlb, vma, start, end, details);
13667e027b14SLinus Torvalds 	mmu_notifier_invalidate_range_end(mm, start, end);
13677e027b14SLinus Torvalds 	tlb_finish_mmu(&tlb, start, end);
13681da177e4SLinus Torvalds }
13691da177e4SLinus Torvalds 
1370c627f9ccSJack Steiner /**
1371f5cc4eefSAl Viro  * zap_page_range_single - remove user pages in a given range
1372f5cc4eefSAl Viro  * @vma: vm_area_struct holding the applicable pages
1373f5cc4eefSAl Viro  * @address: starting address of pages to zap
1374f5cc4eefSAl Viro  * @size: number of bytes to zap
13758a5f14a2SKirill A. Shutemov  * @details: details of shared cache invalidation
1376f5cc4eefSAl Viro  *
1377f5cc4eefSAl Viro  * The range must fit into one VMA.
1378f5cc4eefSAl Viro  */
1379f5cc4eefSAl Viro static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,
1380f5cc4eefSAl Viro 		unsigned long size, struct zap_details *details)
1381f5cc4eefSAl Viro {
1382f5cc4eefSAl Viro 	struct mm_struct *mm = vma->vm_mm;
1383f5cc4eefSAl Viro 	struct mmu_gather tlb;
1384f5cc4eefSAl Viro 	unsigned long end = address + size;
1385f5cc4eefSAl Viro 
1386f5cc4eefSAl Viro 	lru_add_drain();
13872b047252SLinus Torvalds 	tlb_gather_mmu(&tlb, mm, address, end);
1388f5cc4eefSAl Viro 	update_hiwater_rss(mm);
1389f5cc4eefSAl Viro 	mmu_notifier_invalidate_range_start(mm, address, end);
13904f74d2c8SLinus Torvalds 	unmap_single_vma(&tlb, vma, address, end, details);
1391f5cc4eefSAl Viro 	mmu_notifier_invalidate_range_end(mm, address, end);
1392f5cc4eefSAl Viro 	tlb_finish_mmu(&tlb, address, end);
13931da177e4SLinus Torvalds }
13941da177e4SLinus Torvalds 
1395c627f9ccSJack Steiner /**
1396c627f9ccSJack Steiner  * zap_vma_ptes - remove ptes mapping the vma
1397c627f9ccSJack Steiner  * @vma: vm_area_struct holding ptes to be zapped
1398c627f9ccSJack Steiner  * @address: starting address of pages to zap
1399c627f9ccSJack Steiner  * @size: number of bytes to zap
1400c627f9ccSJack Steiner  *
1401c627f9ccSJack Steiner  * This function only unmaps ptes assigned to VM_PFNMAP vmas.
1402c627f9ccSJack Steiner  *
1403c627f9ccSJack Steiner  * The entire address range must be fully contained within the vma.
1404c627f9ccSJack Steiner  *
1405c627f9ccSJack Steiner  * Returns 0 if successful.
1406c627f9ccSJack Steiner  */
1407c627f9ccSJack Steiner int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
1408c627f9ccSJack Steiner 		unsigned long size)
1409c627f9ccSJack Steiner {
1410c627f9ccSJack Steiner 	if (address < vma->vm_start || address + size > vma->vm_end ||
1411c627f9ccSJack Steiner 	    		!(vma->vm_flags & VM_PFNMAP))
1412c627f9ccSJack Steiner 		return -1;
1413f5cc4eefSAl Viro 	zap_page_range_single(vma, address, size, NULL);
1414c627f9ccSJack Steiner 	return 0;
1415c627f9ccSJack Steiner }
1416c627f9ccSJack Steiner EXPORT_SYMBOL_GPL(zap_vma_ptes);
1417c627f9ccSJack Steiner 
141825ca1d6cSNamhyung Kim pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
1419920c7a5dSHarvey Harrison 			spinlock_t **ptl)
1420c9cfcddfSLinus Torvalds {
1421c9cfcddfSLinus Torvalds 	pgd_t * pgd = pgd_offset(mm, addr);
1422c9cfcddfSLinus Torvalds 	pud_t * pud = pud_alloc(mm, pgd, addr);
1423c9cfcddfSLinus Torvalds 	if (pud) {
142449c91fb0STrond Myklebust 		pmd_t * pmd = pmd_alloc(mm, pud, addr);
1425f66055abSAndrea Arcangeli 		if (pmd) {
1426f66055abSAndrea Arcangeli 			VM_BUG_ON(pmd_trans_huge(*pmd));
1427c9cfcddfSLinus Torvalds 			return pte_alloc_map_lock(mm, pmd, addr, ptl);
1428c9cfcddfSLinus Torvalds 		}
1429f66055abSAndrea Arcangeli 	}
1430c9cfcddfSLinus Torvalds 	return NULL;
1431c9cfcddfSLinus Torvalds }
1432c9cfcddfSLinus Torvalds 
14331da177e4SLinus Torvalds /*
1434238f58d8SLinus Torvalds  * This is the old fallback for page remapping.
1435238f58d8SLinus Torvalds  *
1436238f58d8SLinus Torvalds  * For historical reasons, it only allows reserved pages. Only
1437238f58d8SLinus Torvalds  * old drivers should use this, and they needed to mark their
1438238f58d8SLinus Torvalds  * pages reserved for the old functions anyway.
1439238f58d8SLinus Torvalds  */
1440423bad60SNick Piggin static int insert_page(struct vm_area_struct *vma, unsigned long addr,
1441423bad60SNick Piggin 			struct page *page, pgprot_t prot)
1442238f58d8SLinus Torvalds {
1443423bad60SNick Piggin 	struct mm_struct *mm = vma->vm_mm;
1444238f58d8SLinus Torvalds 	int retval;
1445238f58d8SLinus Torvalds 	pte_t *pte;
1446238f58d8SLinus Torvalds 	spinlock_t *ptl;
1447238f58d8SLinus Torvalds 
1448238f58d8SLinus Torvalds 	retval = -EINVAL;
1449a145dd41SLinus Torvalds 	if (PageAnon(page))
14505b4e655eSKAMEZAWA Hiroyuki 		goto out;
1451238f58d8SLinus Torvalds 	retval = -ENOMEM;
1452238f58d8SLinus Torvalds 	flush_dcache_page(page);
1453c9cfcddfSLinus Torvalds 	pte = get_locked_pte(mm, addr, &ptl);
1454238f58d8SLinus Torvalds 	if (!pte)
14555b4e655eSKAMEZAWA Hiroyuki 		goto out;
1456238f58d8SLinus Torvalds 	retval = -EBUSY;
1457238f58d8SLinus Torvalds 	if (!pte_none(*pte))
1458238f58d8SLinus Torvalds 		goto out_unlock;
1459238f58d8SLinus Torvalds 
1460238f58d8SLinus Torvalds 	/* Ok, finally just insert the thing.. */
1461238f58d8SLinus Torvalds 	get_page(page);
146234e55232SKAMEZAWA Hiroyuki 	inc_mm_counter_fast(mm, MM_FILEPAGES);
1463238f58d8SLinus Torvalds 	page_add_file_rmap(page);
1464238f58d8SLinus Torvalds 	set_pte_at(mm, addr, pte, mk_pte(page, prot));
1465238f58d8SLinus Torvalds 
1466238f58d8SLinus Torvalds 	retval = 0;
14678a9f3ccdSBalbir Singh 	pte_unmap_unlock(pte, ptl);
14688a9f3ccdSBalbir Singh 	return retval;
1469238f58d8SLinus Torvalds out_unlock:
1470238f58d8SLinus Torvalds 	pte_unmap_unlock(pte, ptl);
1471238f58d8SLinus Torvalds out:
1472238f58d8SLinus Torvalds 	return retval;
1473238f58d8SLinus Torvalds }
1474238f58d8SLinus Torvalds 
1475bfa5bf6dSRolf Eike Beer /**
1476bfa5bf6dSRolf Eike Beer  * vm_insert_page - insert single page into user vma
1477bfa5bf6dSRolf Eike Beer  * @vma: user vma to map to
1478bfa5bf6dSRolf Eike Beer  * @addr: target user address of this page
1479bfa5bf6dSRolf Eike Beer  * @page: source kernel page
1480bfa5bf6dSRolf Eike Beer  *
1481a145dd41SLinus Torvalds  * This allows drivers to insert individual pages they've allocated
1482a145dd41SLinus Torvalds  * into a user vma.
1483a145dd41SLinus Torvalds  *
1484a145dd41SLinus Torvalds  * The page has to be a nice clean _individual_ kernel allocation.
1485a145dd41SLinus Torvalds  * If you allocate a compound page, you need to have marked it as
1486a145dd41SLinus Torvalds  * such (__GFP_COMP), or manually just split the page up yourself
14878dfcc9baSNick Piggin  * (see split_page()).
1488a145dd41SLinus Torvalds  *
1489a145dd41SLinus Torvalds  * NOTE! Traditionally this was done with "remap_pfn_range()" which
1490a145dd41SLinus Torvalds  * took an arbitrary page protection parameter. This doesn't allow
1491a145dd41SLinus Torvalds  * that. Your vma protection will have to be set up correctly, which
1492a145dd41SLinus Torvalds  * means that if you want a shared writable mapping, you'd better
1493a145dd41SLinus Torvalds  * ask for a shared writable mapping!
1494a145dd41SLinus Torvalds  *
1495a145dd41SLinus Torvalds  * The page does not need to be reserved.
14964b6e1e37SKonstantin Khlebnikov  *
14974b6e1e37SKonstantin Khlebnikov  * Usually this function is called from f_op->mmap() handler
14984b6e1e37SKonstantin Khlebnikov  * under mm->mmap_sem write-lock, so it can change vma->vm_flags.
14994b6e1e37SKonstantin Khlebnikov  * Caller must set VM_MIXEDMAP on vma if it wants to call this
15004b6e1e37SKonstantin Khlebnikov  * function from other places, for example from page-fault handler.
1501a145dd41SLinus Torvalds  */
1502423bad60SNick Piggin int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
1503423bad60SNick Piggin 			struct page *page)
1504a145dd41SLinus Torvalds {
1505a145dd41SLinus Torvalds 	if (addr < vma->vm_start || addr >= vma->vm_end)
1506a145dd41SLinus Torvalds 		return -EFAULT;
1507a145dd41SLinus Torvalds 	if (!page_count(page))
1508a145dd41SLinus Torvalds 		return -EINVAL;
15094b6e1e37SKonstantin Khlebnikov 	if (!(vma->vm_flags & VM_MIXEDMAP)) {
15104b6e1e37SKonstantin Khlebnikov 		BUG_ON(down_read_trylock(&vma->vm_mm->mmap_sem));
15114b6e1e37SKonstantin Khlebnikov 		BUG_ON(vma->vm_flags & VM_PFNMAP);
15124b6e1e37SKonstantin Khlebnikov 		vma->vm_flags |= VM_MIXEDMAP;
15134b6e1e37SKonstantin Khlebnikov 	}
1514423bad60SNick Piggin 	return insert_page(vma, addr, page, vma->vm_page_prot);
1515a145dd41SLinus Torvalds }
1516e3c3374fSLinus Torvalds EXPORT_SYMBOL(vm_insert_page);
1517a145dd41SLinus Torvalds 
1518423bad60SNick Piggin static int insert_pfn(struct vm_area_struct *vma, unsigned long addr,
1519423bad60SNick Piggin 			unsigned long pfn, pgprot_t prot)
1520423bad60SNick Piggin {
1521423bad60SNick Piggin 	struct mm_struct *mm = vma->vm_mm;
1522423bad60SNick Piggin 	int retval;
1523423bad60SNick Piggin 	pte_t *pte, entry;
1524423bad60SNick Piggin 	spinlock_t *ptl;
1525423bad60SNick Piggin 
1526423bad60SNick Piggin 	retval = -ENOMEM;
1527423bad60SNick Piggin 	pte = get_locked_pte(mm, addr, &ptl);
1528423bad60SNick Piggin 	if (!pte)
1529423bad60SNick Piggin 		goto out;
1530423bad60SNick Piggin 	retval = -EBUSY;
1531423bad60SNick Piggin 	if (!pte_none(*pte))
1532423bad60SNick Piggin 		goto out_unlock;
1533423bad60SNick Piggin 
1534423bad60SNick Piggin 	/* Ok, finally just insert the thing.. */
1535423bad60SNick Piggin 	entry = pte_mkspecial(pfn_pte(pfn, prot));
1536423bad60SNick Piggin 	set_pte_at(mm, addr, pte, entry);
15374b3073e1SRussell King 	update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */
1538423bad60SNick Piggin 
1539423bad60SNick Piggin 	retval = 0;
1540423bad60SNick Piggin out_unlock:
1541423bad60SNick Piggin 	pte_unmap_unlock(pte, ptl);
1542423bad60SNick Piggin out:
1543423bad60SNick Piggin 	return retval;
1544423bad60SNick Piggin }
1545423bad60SNick Piggin 
1546e0dc0d8fSNick Piggin /**
1547e0dc0d8fSNick Piggin  * vm_insert_pfn - insert single pfn into user vma
1548e0dc0d8fSNick Piggin  * @vma: user vma to map to
1549e0dc0d8fSNick Piggin  * @addr: target user address of this page
1550e0dc0d8fSNick Piggin  * @pfn: source kernel pfn
1551e0dc0d8fSNick Piggin  *
1552c462f179SRobert P. J. Day  * Similar to vm_insert_page, this allows drivers to insert individual pages
1553e0dc0d8fSNick Piggin  * they've allocated into a user vma. Same comments apply.
1554e0dc0d8fSNick Piggin  *
1555e0dc0d8fSNick Piggin  * This function should only be called from a vm_ops->fault handler, and
1556e0dc0d8fSNick Piggin  * in that case the handler should return NULL.
15570d71d10aSNick Piggin  *
15580d71d10aSNick Piggin  * vma cannot be a COW mapping.
15590d71d10aSNick Piggin  *
15600d71d10aSNick Piggin  * As this is called only for pages that do not currently exist, we
15610d71d10aSNick Piggin  * do not need to flush old virtual caches or the TLB.
1562e0dc0d8fSNick Piggin  */
1563e0dc0d8fSNick Piggin int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
1564e0dc0d8fSNick Piggin 			unsigned long pfn)
1565e0dc0d8fSNick Piggin {
15662ab64037Svenkatesh.pallipadi@intel.com 	int ret;
1567e4b866edSvenkatesh.pallipadi@intel.com 	pgprot_t pgprot = vma->vm_page_prot;
15687e675137SNick Piggin 	/*
15697e675137SNick Piggin 	 * Technically, architectures with pte_special can avoid all these
15707e675137SNick Piggin 	 * restrictions (same for remap_pfn_range).  However we would like
15717e675137SNick Piggin 	 * consistency in testing and feature parity among all, so we should
15727e675137SNick Piggin 	 * try to keep these invariants in place for everybody.
15737e675137SNick Piggin 	 */
1574b379d790SJared Hulbert 	BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
1575b379d790SJared Hulbert 	BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
1576b379d790SJared Hulbert 						(VM_PFNMAP|VM_MIXEDMAP));
1577b379d790SJared Hulbert 	BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
1578b379d790SJared Hulbert 	BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
1579e0dc0d8fSNick Piggin 
1580423bad60SNick Piggin 	if (addr < vma->vm_start || addr >= vma->vm_end)
1581423bad60SNick Piggin 		return -EFAULT;
15825180da41SSuresh Siddha 	if (track_pfn_insert(vma, &pgprot, pfn))
15832ab64037Svenkatesh.pallipadi@intel.com 		return -EINVAL;
15842ab64037Svenkatesh.pallipadi@intel.com 
1585e4b866edSvenkatesh.pallipadi@intel.com 	ret = insert_pfn(vma, addr, pfn, pgprot);
15862ab64037Svenkatesh.pallipadi@intel.com 
15872ab64037Svenkatesh.pallipadi@intel.com 	return ret;
1588e0dc0d8fSNick Piggin }
1589e0dc0d8fSNick Piggin EXPORT_SYMBOL(vm_insert_pfn);
1590e0dc0d8fSNick Piggin 
1591423bad60SNick Piggin int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
1592423bad60SNick Piggin 			unsigned long pfn)
1593423bad60SNick Piggin {
1594423bad60SNick Piggin 	BUG_ON(!(vma->vm_flags & VM_MIXEDMAP));
1595423bad60SNick Piggin 
1596423bad60SNick Piggin 	if (addr < vma->vm_start || addr >= vma->vm_end)
1597423bad60SNick Piggin 		return -EFAULT;
1598423bad60SNick Piggin 
1599423bad60SNick Piggin 	/*
1600423bad60SNick Piggin 	 * If we don't have pte special, then we have to use the pfn_valid()
1601423bad60SNick Piggin 	 * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must*
1602423bad60SNick Piggin 	 * refcount the page if pfn_valid is true (hence insert_page rather
160362eede62SHugh Dickins 	 * than insert_pfn).  If a zero_pfn were inserted into a VM_MIXEDMAP
160462eede62SHugh Dickins 	 * without pte special, it would there be refcounted as a normal page.
1605423bad60SNick Piggin 	 */
1606423bad60SNick Piggin 	if (!HAVE_PTE_SPECIAL && pfn_valid(pfn)) {
1607423bad60SNick Piggin 		struct page *page;
1608423bad60SNick Piggin 
1609423bad60SNick Piggin 		page = pfn_to_page(pfn);
1610423bad60SNick Piggin 		return insert_page(vma, addr, page, vma->vm_page_prot);
1611423bad60SNick Piggin 	}
1612423bad60SNick Piggin 	return insert_pfn(vma, addr, pfn, vma->vm_page_prot);
1613423bad60SNick Piggin }
1614423bad60SNick Piggin EXPORT_SYMBOL(vm_insert_mixed);
1615423bad60SNick Piggin 
1616a145dd41SLinus Torvalds /*
16171da177e4SLinus Torvalds  * maps a range of physical memory into the requested pages. the old
16181da177e4SLinus Torvalds  * mappings are removed. any references to nonexistent pages results
16191da177e4SLinus Torvalds  * in null mappings (currently treated as "copy-on-access")
16201da177e4SLinus Torvalds  */
16211da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
16221da177e4SLinus Torvalds 			unsigned long addr, unsigned long end,
16231da177e4SLinus Torvalds 			unsigned long pfn, pgprot_t prot)
16241da177e4SLinus Torvalds {
16251da177e4SLinus Torvalds 	pte_t *pte;
1626c74df32cSHugh Dickins 	spinlock_t *ptl;
16271da177e4SLinus Torvalds 
1628c74df32cSHugh Dickins 	pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
16291da177e4SLinus Torvalds 	if (!pte)
16301da177e4SLinus Torvalds 		return -ENOMEM;
16316606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
16321da177e4SLinus Torvalds 	do {
16331da177e4SLinus Torvalds 		BUG_ON(!pte_none(*pte));
16347e675137SNick Piggin 		set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot)));
16351da177e4SLinus Torvalds 		pfn++;
16361da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
16376606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
1638c74df32cSHugh Dickins 	pte_unmap_unlock(pte - 1, ptl);
16391da177e4SLinus Torvalds 	return 0;
16401da177e4SLinus Torvalds }
16411da177e4SLinus Torvalds 
16421da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
16431da177e4SLinus Torvalds 			unsigned long addr, unsigned long end,
16441da177e4SLinus Torvalds 			unsigned long pfn, pgprot_t prot)
16451da177e4SLinus Torvalds {
16461da177e4SLinus Torvalds 	pmd_t *pmd;
16471da177e4SLinus Torvalds 	unsigned long next;
16481da177e4SLinus Torvalds 
16491da177e4SLinus Torvalds 	pfn -= addr >> PAGE_SHIFT;
16501da177e4SLinus Torvalds 	pmd = pmd_alloc(mm, pud, addr);
16511da177e4SLinus Torvalds 	if (!pmd)
16521da177e4SLinus Torvalds 		return -ENOMEM;
1653f66055abSAndrea Arcangeli 	VM_BUG_ON(pmd_trans_huge(*pmd));
16541da177e4SLinus Torvalds 	do {
16551da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
16561da177e4SLinus Torvalds 		if (remap_pte_range(mm, pmd, addr, next,
16571da177e4SLinus Torvalds 				pfn + (addr >> PAGE_SHIFT), prot))
16581da177e4SLinus Torvalds 			return -ENOMEM;
16591da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
16601da177e4SLinus Torvalds 	return 0;
16611da177e4SLinus Torvalds }
16621da177e4SLinus Torvalds 
16631da177e4SLinus Torvalds static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd,
16641da177e4SLinus Torvalds 			unsigned long addr, unsigned long end,
16651da177e4SLinus Torvalds 			unsigned long pfn, pgprot_t prot)
16661da177e4SLinus Torvalds {
16671da177e4SLinus Torvalds 	pud_t *pud;
16681da177e4SLinus Torvalds 	unsigned long next;
16691da177e4SLinus Torvalds 
16701da177e4SLinus Torvalds 	pfn -= addr >> PAGE_SHIFT;
16711da177e4SLinus Torvalds 	pud = pud_alloc(mm, pgd, addr);
16721da177e4SLinus Torvalds 	if (!pud)
16731da177e4SLinus Torvalds 		return -ENOMEM;
16741da177e4SLinus Torvalds 	do {
16751da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
16761da177e4SLinus Torvalds 		if (remap_pmd_range(mm, pud, addr, next,
16771da177e4SLinus Torvalds 				pfn + (addr >> PAGE_SHIFT), prot))
16781da177e4SLinus Torvalds 			return -ENOMEM;
16791da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
16801da177e4SLinus Torvalds 	return 0;
16811da177e4SLinus Torvalds }
16821da177e4SLinus Torvalds 
1683bfa5bf6dSRolf Eike Beer /**
1684bfa5bf6dSRolf Eike Beer  * remap_pfn_range - remap kernel memory to userspace
1685bfa5bf6dSRolf Eike Beer  * @vma: user vma to map to
1686bfa5bf6dSRolf Eike Beer  * @addr: target user address to start at
1687bfa5bf6dSRolf Eike Beer  * @pfn: physical address of kernel memory
1688bfa5bf6dSRolf Eike Beer  * @size: size of map area
1689bfa5bf6dSRolf Eike Beer  * @prot: page protection flags for this mapping
1690bfa5bf6dSRolf Eike Beer  *
1691bfa5bf6dSRolf Eike Beer  *  Note: this is only safe if the mm semaphore is held when called.
1692bfa5bf6dSRolf Eike Beer  */
16931da177e4SLinus Torvalds int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
16941da177e4SLinus Torvalds 		    unsigned long pfn, unsigned long size, pgprot_t prot)
16951da177e4SLinus Torvalds {
16961da177e4SLinus Torvalds 	pgd_t *pgd;
16971da177e4SLinus Torvalds 	unsigned long next;
16982d15cab8SHugh Dickins 	unsigned long end = addr + PAGE_ALIGN(size);
16991da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
17001da177e4SLinus Torvalds 	int err;
17011da177e4SLinus Torvalds 
17021da177e4SLinus Torvalds 	/*
17031da177e4SLinus Torvalds 	 * Physically remapped pages are special. Tell the
17041da177e4SLinus Torvalds 	 * rest of the world about it:
17051da177e4SLinus Torvalds 	 *   VM_IO tells people not to look at these pages
17061da177e4SLinus Torvalds 	 *	(accesses can have side effects).
17076aab341eSLinus Torvalds 	 *   VM_PFNMAP tells the core MM that the base pages are just
17086aab341eSLinus Torvalds 	 *	raw PFN mappings, and do not have a "struct page" associated
17096aab341eSLinus Torvalds 	 *	with them.
1710314e51b9SKonstantin Khlebnikov 	 *   VM_DONTEXPAND
1711314e51b9SKonstantin Khlebnikov 	 *      Disable vma merging and expanding with mremap().
1712314e51b9SKonstantin Khlebnikov 	 *   VM_DONTDUMP
1713314e51b9SKonstantin Khlebnikov 	 *      Omit vma from core dump, even when VM_IO turned off.
1714fb155c16SLinus Torvalds 	 *
1715fb155c16SLinus Torvalds 	 * There's a horrible special case to handle copy-on-write
1716fb155c16SLinus Torvalds 	 * behaviour that some programs depend on. We mark the "original"
1717fb155c16SLinus Torvalds 	 * un-COW'ed pages by matching them up with "vma->vm_pgoff".
1718b3b9c293SKonstantin Khlebnikov 	 * See vm_normal_page() for details.
17191da177e4SLinus Torvalds 	 */
1720b3b9c293SKonstantin Khlebnikov 	if (is_cow_mapping(vma->vm_flags)) {
1721b3b9c293SKonstantin Khlebnikov 		if (addr != vma->vm_start || end != vma->vm_end)
1722b3b9c293SKonstantin Khlebnikov 			return -EINVAL;
17236aab341eSLinus Torvalds 		vma->vm_pgoff = pfn;
1724b3b9c293SKonstantin Khlebnikov 	}
1725b3b9c293SKonstantin Khlebnikov 
1726b3b9c293SKonstantin Khlebnikov 	err = track_pfn_remap(vma, &prot, pfn, addr, PAGE_ALIGN(size));
1727b3b9c293SKonstantin Khlebnikov 	if (err)
17283c8bb73aSvenkatesh.pallipadi@intel.com 		return -EINVAL;
1729fb155c16SLinus Torvalds 
1730314e51b9SKonstantin Khlebnikov 	vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
17311da177e4SLinus Torvalds 
17321da177e4SLinus Torvalds 	BUG_ON(addr >= end);
17331da177e4SLinus Torvalds 	pfn -= addr >> PAGE_SHIFT;
17341da177e4SLinus Torvalds 	pgd = pgd_offset(mm, addr);
17351da177e4SLinus Torvalds 	flush_cache_range(vma, addr, end);
17361da177e4SLinus Torvalds 	do {
17371da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
17381da177e4SLinus Torvalds 		err = remap_pud_range(mm, pgd, addr, next,
17391da177e4SLinus Torvalds 				pfn + (addr >> PAGE_SHIFT), prot);
17401da177e4SLinus Torvalds 		if (err)
17411da177e4SLinus Torvalds 			break;
17421da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
17432ab64037Svenkatesh.pallipadi@intel.com 
17442ab64037Svenkatesh.pallipadi@intel.com 	if (err)
17455180da41SSuresh Siddha 		untrack_pfn(vma, pfn, PAGE_ALIGN(size));
17462ab64037Svenkatesh.pallipadi@intel.com 
17471da177e4SLinus Torvalds 	return err;
17481da177e4SLinus Torvalds }
17491da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range);
17501da177e4SLinus Torvalds 
1751b4cbb197SLinus Torvalds /**
1752b4cbb197SLinus Torvalds  * vm_iomap_memory - remap memory to userspace
1753b4cbb197SLinus Torvalds  * @vma: user vma to map to
1754b4cbb197SLinus Torvalds  * @start: start of area
1755b4cbb197SLinus Torvalds  * @len: size of area
1756b4cbb197SLinus Torvalds  *
1757b4cbb197SLinus Torvalds  * This is a simplified io_remap_pfn_range() for common driver use. The
1758b4cbb197SLinus Torvalds  * driver just needs to give us the physical memory range to be mapped,
1759b4cbb197SLinus Torvalds  * we'll figure out the rest from the vma information.
1760b4cbb197SLinus Torvalds  *
1761b4cbb197SLinus Torvalds  * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get
1762b4cbb197SLinus Torvalds  * whatever write-combining details or similar.
1763b4cbb197SLinus Torvalds  */
1764b4cbb197SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
1765b4cbb197SLinus Torvalds {
1766b4cbb197SLinus Torvalds 	unsigned long vm_len, pfn, pages;
1767b4cbb197SLinus Torvalds 
1768b4cbb197SLinus Torvalds 	/* Check that the physical memory area passed in looks valid */
1769b4cbb197SLinus Torvalds 	if (start + len < start)
1770b4cbb197SLinus Torvalds 		return -EINVAL;
1771b4cbb197SLinus Torvalds 	/*
1772b4cbb197SLinus Torvalds 	 * You *really* shouldn't map things that aren't page-aligned,
1773b4cbb197SLinus Torvalds 	 * but we've historically allowed it because IO memory might
1774b4cbb197SLinus Torvalds 	 * just have smaller alignment.
1775b4cbb197SLinus Torvalds 	 */
1776b4cbb197SLinus Torvalds 	len += start & ~PAGE_MASK;
1777b4cbb197SLinus Torvalds 	pfn = start >> PAGE_SHIFT;
1778b4cbb197SLinus Torvalds 	pages = (len + ~PAGE_MASK) >> PAGE_SHIFT;
1779b4cbb197SLinus Torvalds 	if (pfn + pages < pfn)
1780b4cbb197SLinus Torvalds 		return -EINVAL;
1781b4cbb197SLinus Torvalds 
1782b4cbb197SLinus Torvalds 	/* We start the mapping 'vm_pgoff' pages into the area */
1783b4cbb197SLinus Torvalds 	if (vma->vm_pgoff > pages)
1784b4cbb197SLinus Torvalds 		return -EINVAL;
1785b4cbb197SLinus Torvalds 	pfn += vma->vm_pgoff;
1786b4cbb197SLinus Torvalds 	pages -= vma->vm_pgoff;
1787b4cbb197SLinus Torvalds 
1788b4cbb197SLinus Torvalds 	/* Can we fit all of the mapping? */
1789b4cbb197SLinus Torvalds 	vm_len = vma->vm_end - vma->vm_start;
1790b4cbb197SLinus Torvalds 	if (vm_len >> PAGE_SHIFT > pages)
1791b4cbb197SLinus Torvalds 		return -EINVAL;
1792b4cbb197SLinus Torvalds 
1793b4cbb197SLinus Torvalds 	/* Ok, let it rip */
1794b4cbb197SLinus Torvalds 	return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
1795b4cbb197SLinus Torvalds }
1796b4cbb197SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory);
1797b4cbb197SLinus Torvalds 
1798aee16b3cSJeremy Fitzhardinge static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
1799aee16b3cSJeremy Fitzhardinge 				     unsigned long addr, unsigned long end,
1800aee16b3cSJeremy Fitzhardinge 				     pte_fn_t fn, void *data)
1801aee16b3cSJeremy Fitzhardinge {
1802aee16b3cSJeremy Fitzhardinge 	pte_t *pte;
1803aee16b3cSJeremy Fitzhardinge 	int err;
18042f569afdSMartin Schwidefsky 	pgtable_t token;
180594909914SBorislav Petkov 	spinlock_t *uninitialized_var(ptl);
1806aee16b3cSJeremy Fitzhardinge 
1807aee16b3cSJeremy Fitzhardinge 	pte = (mm == &init_mm) ?
1808aee16b3cSJeremy Fitzhardinge 		pte_alloc_kernel(pmd, addr) :
1809aee16b3cSJeremy Fitzhardinge 		pte_alloc_map_lock(mm, pmd, addr, &ptl);
1810aee16b3cSJeremy Fitzhardinge 	if (!pte)
1811aee16b3cSJeremy Fitzhardinge 		return -ENOMEM;
1812aee16b3cSJeremy Fitzhardinge 
1813aee16b3cSJeremy Fitzhardinge 	BUG_ON(pmd_huge(*pmd));
1814aee16b3cSJeremy Fitzhardinge 
181538e0edb1SJeremy Fitzhardinge 	arch_enter_lazy_mmu_mode();
181638e0edb1SJeremy Fitzhardinge 
18172f569afdSMartin Schwidefsky 	token = pmd_pgtable(*pmd);
1818aee16b3cSJeremy Fitzhardinge 
1819aee16b3cSJeremy Fitzhardinge 	do {
1820c36987e2SDaisuke Nishimura 		err = fn(pte++, token, addr, data);
1821aee16b3cSJeremy Fitzhardinge 		if (err)
1822aee16b3cSJeremy Fitzhardinge 			break;
1823c36987e2SDaisuke Nishimura 	} while (addr += PAGE_SIZE, addr != end);
1824aee16b3cSJeremy Fitzhardinge 
182538e0edb1SJeremy Fitzhardinge 	arch_leave_lazy_mmu_mode();
182638e0edb1SJeremy Fitzhardinge 
1827aee16b3cSJeremy Fitzhardinge 	if (mm != &init_mm)
1828aee16b3cSJeremy Fitzhardinge 		pte_unmap_unlock(pte-1, ptl);
1829aee16b3cSJeremy Fitzhardinge 	return err;
1830aee16b3cSJeremy Fitzhardinge }
1831aee16b3cSJeremy Fitzhardinge 
1832aee16b3cSJeremy Fitzhardinge static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
1833aee16b3cSJeremy Fitzhardinge 				     unsigned long addr, unsigned long end,
1834aee16b3cSJeremy Fitzhardinge 				     pte_fn_t fn, void *data)
1835aee16b3cSJeremy Fitzhardinge {
1836aee16b3cSJeremy Fitzhardinge 	pmd_t *pmd;
1837aee16b3cSJeremy Fitzhardinge 	unsigned long next;
1838aee16b3cSJeremy Fitzhardinge 	int err;
1839aee16b3cSJeremy Fitzhardinge 
1840ceb86879SAndi Kleen 	BUG_ON(pud_huge(*pud));
1841ceb86879SAndi Kleen 
1842aee16b3cSJeremy Fitzhardinge 	pmd = pmd_alloc(mm, pud, addr);
1843aee16b3cSJeremy Fitzhardinge 	if (!pmd)
1844aee16b3cSJeremy Fitzhardinge 		return -ENOMEM;
1845aee16b3cSJeremy Fitzhardinge 	do {
1846aee16b3cSJeremy Fitzhardinge 		next = pmd_addr_end(addr, end);
1847aee16b3cSJeremy Fitzhardinge 		err = apply_to_pte_range(mm, pmd, addr, next, fn, data);
1848aee16b3cSJeremy Fitzhardinge 		if (err)
1849aee16b3cSJeremy Fitzhardinge 			break;
1850aee16b3cSJeremy Fitzhardinge 	} while (pmd++, addr = next, addr != end);
1851aee16b3cSJeremy Fitzhardinge 	return err;
1852aee16b3cSJeremy Fitzhardinge }
1853aee16b3cSJeremy Fitzhardinge 
1854aee16b3cSJeremy Fitzhardinge static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd,
1855aee16b3cSJeremy Fitzhardinge 				     unsigned long addr, unsigned long end,
1856aee16b3cSJeremy Fitzhardinge 				     pte_fn_t fn, void *data)
1857aee16b3cSJeremy Fitzhardinge {
1858aee16b3cSJeremy Fitzhardinge 	pud_t *pud;
1859aee16b3cSJeremy Fitzhardinge 	unsigned long next;
1860aee16b3cSJeremy Fitzhardinge 	int err;
1861aee16b3cSJeremy Fitzhardinge 
1862aee16b3cSJeremy Fitzhardinge 	pud = pud_alloc(mm, pgd, addr);
1863aee16b3cSJeremy Fitzhardinge 	if (!pud)
1864aee16b3cSJeremy Fitzhardinge 		return -ENOMEM;
1865aee16b3cSJeremy Fitzhardinge 	do {
1866aee16b3cSJeremy Fitzhardinge 		next = pud_addr_end(addr, end);
1867aee16b3cSJeremy Fitzhardinge 		err = apply_to_pmd_range(mm, pud, addr, next, fn, data);
1868aee16b3cSJeremy Fitzhardinge 		if (err)
1869aee16b3cSJeremy Fitzhardinge 			break;
1870aee16b3cSJeremy Fitzhardinge 	} while (pud++, addr = next, addr != end);
1871aee16b3cSJeremy Fitzhardinge 	return err;
1872aee16b3cSJeremy Fitzhardinge }
1873aee16b3cSJeremy Fitzhardinge 
1874aee16b3cSJeremy Fitzhardinge /*
1875aee16b3cSJeremy Fitzhardinge  * Scan a region of virtual memory, filling in page tables as necessary
1876aee16b3cSJeremy Fitzhardinge  * and calling a provided function on each leaf page table.
1877aee16b3cSJeremy Fitzhardinge  */
1878aee16b3cSJeremy Fitzhardinge int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
1879aee16b3cSJeremy Fitzhardinge 			unsigned long size, pte_fn_t fn, void *data)
1880aee16b3cSJeremy Fitzhardinge {
1881aee16b3cSJeremy Fitzhardinge 	pgd_t *pgd;
1882aee16b3cSJeremy Fitzhardinge 	unsigned long next;
188357250a5bSJeremy Fitzhardinge 	unsigned long end = addr + size;
1884aee16b3cSJeremy Fitzhardinge 	int err;
1885aee16b3cSJeremy Fitzhardinge 
1886aee16b3cSJeremy Fitzhardinge 	BUG_ON(addr >= end);
1887aee16b3cSJeremy Fitzhardinge 	pgd = pgd_offset(mm, addr);
1888aee16b3cSJeremy Fitzhardinge 	do {
1889aee16b3cSJeremy Fitzhardinge 		next = pgd_addr_end(addr, end);
1890aee16b3cSJeremy Fitzhardinge 		err = apply_to_pud_range(mm, pgd, addr, next, fn, data);
1891aee16b3cSJeremy Fitzhardinge 		if (err)
1892aee16b3cSJeremy Fitzhardinge 			break;
1893aee16b3cSJeremy Fitzhardinge 	} while (pgd++, addr = next, addr != end);
189457250a5bSJeremy Fitzhardinge 
1895aee16b3cSJeremy Fitzhardinge 	return err;
1896aee16b3cSJeremy Fitzhardinge }
1897aee16b3cSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(apply_to_page_range);
1898aee16b3cSJeremy Fitzhardinge 
18991da177e4SLinus Torvalds /*
19009b4bdd2fSKirill A. Shutemov  * handle_pte_fault chooses page fault handler according to an entry which was
19019b4bdd2fSKirill A. Shutemov  * read non-atomically.  Before making any commitment, on those architectures
19029b4bdd2fSKirill A. Shutemov  * or configurations (e.g. i386 with PAE) which might give a mix of unmatched
19039b4bdd2fSKirill A. Shutemov  * parts, do_swap_page must check under lock before unmapping the pte and
19049b4bdd2fSKirill A. Shutemov  * proceeding (but do_wp_page is only called after already making such a check;
1905a335b2e1SRyota Ozaki  * and do_anonymous_page can safely check later on).
19068f4e2101SHugh Dickins  */
19074c21e2f2SHugh Dickins static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
19088f4e2101SHugh Dickins 				pte_t *page_table, pte_t orig_pte)
19098f4e2101SHugh Dickins {
19108f4e2101SHugh Dickins 	int same = 1;
19118f4e2101SHugh Dickins #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
19128f4e2101SHugh Dickins 	if (sizeof(pte_t) > sizeof(unsigned long)) {
19134c21e2f2SHugh Dickins 		spinlock_t *ptl = pte_lockptr(mm, pmd);
19144c21e2f2SHugh Dickins 		spin_lock(ptl);
19158f4e2101SHugh Dickins 		same = pte_same(*page_table, orig_pte);
19164c21e2f2SHugh Dickins 		spin_unlock(ptl);
19178f4e2101SHugh Dickins 	}
19188f4e2101SHugh Dickins #endif
19198f4e2101SHugh Dickins 	pte_unmap(page_table);
19208f4e2101SHugh Dickins 	return same;
19218f4e2101SHugh Dickins }
19228f4e2101SHugh Dickins 
19239de455b2SAtsushi Nemoto static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma)
19246aab341eSLinus Torvalds {
19250abdd7a8SDan Williams 	debug_dma_assert_idle(src);
19260abdd7a8SDan Williams 
19276aab341eSLinus Torvalds 	/*
19286aab341eSLinus Torvalds 	 * If the source page was a PFN mapping, we don't have
19296aab341eSLinus Torvalds 	 * a "struct page" for it. We do a best-effort copy by
19306aab341eSLinus Torvalds 	 * just copying from the original user address. If that
19316aab341eSLinus Torvalds 	 * fails, we just zero-fill it. Live with it.
19326aab341eSLinus Torvalds 	 */
19336aab341eSLinus Torvalds 	if (unlikely(!src)) {
19349b04c5feSCong Wang 		void *kaddr = kmap_atomic(dst);
19355d2a2dbbSLinus Torvalds 		void __user *uaddr = (void __user *)(va & PAGE_MASK);
19365d2a2dbbSLinus Torvalds 
19375d2a2dbbSLinus Torvalds 		/*
19385d2a2dbbSLinus Torvalds 		 * This really shouldn't fail, because the page is there
19395d2a2dbbSLinus Torvalds 		 * in the page tables. But it might just be unreadable,
19405d2a2dbbSLinus Torvalds 		 * in which case we just give up and fill the result with
19415d2a2dbbSLinus Torvalds 		 * zeroes.
19425d2a2dbbSLinus Torvalds 		 */
19435d2a2dbbSLinus Torvalds 		if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE))
19443ecb01dfSJan Beulich 			clear_page(kaddr);
19459b04c5feSCong Wang 		kunmap_atomic(kaddr);
1946c4ec7b0dSDmitriy Monakhov 		flush_dcache_page(dst);
19470ed361deSNick Piggin 	} else
19489de455b2SAtsushi Nemoto 		copy_user_highpage(dst, src, va, vma);
19496aab341eSLinus Torvalds }
19506aab341eSLinus Torvalds 
19511da177e4SLinus Torvalds /*
1952fb09a464SKirill A. Shutemov  * Notify the address space that the page is about to become writable so that
1953fb09a464SKirill A. Shutemov  * it can prohibit this or wait for the page to get into an appropriate state.
1954fb09a464SKirill A. Shutemov  *
1955fb09a464SKirill A. Shutemov  * We do this without the lock held, so that it can sleep if it needs to.
1956fb09a464SKirill A. Shutemov  */
1957fb09a464SKirill A. Shutemov static int do_page_mkwrite(struct vm_area_struct *vma, struct page *page,
1958fb09a464SKirill A. Shutemov 	       unsigned long address)
1959fb09a464SKirill A. Shutemov {
1960fb09a464SKirill A. Shutemov 	struct vm_fault vmf;
1961fb09a464SKirill A. Shutemov 	int ret;
1962fb09a464SKirill A. Shutemov 
1963fb09a464SKirill A. Shutemov 	vmf.virtual_address = (void __user *)(address & PAGE_MASK);
1964fb09a464SKirill A. Shutemov 	vmf.pgoff = page->index;
1965fb09a464SKirill A. Shutemov 	vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
1966fb09a464SKirill A. Shutemov 	vmf.page = page;
19672e4cdab0SMatthew Wilcox 	vmf.cow_page = NULL;
1968fb09a464SKirill A. Shutemov 
1969fb09a464SKirill A. Shutemov 	ret = vma->vm_ops->page_mkwrite(vma, &vmf);
1970fb09a464SKirill A. Shutemov 	if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))
1971fb09a464SKirill A. Shutemov 		return ret;
1972fb09a464SKirill A. Shutemov 	if (unlikely(!(ret & VM_FAULT_LOCKED))) {
1973fb09a464SKirill A. Shutemov 		lock_page(page);
1974fb09a464SKirill A. Shutemov 		if (!page->mapping) {
1975fb09a464SKirill A. Shutemov 			unlock_page(page);
1976fb09a464SKirill A. Shutemov 			return 0; /* retry */
1977fb09a464SKirill A. Shutemov 		}
1978fb09a464SKirill A. Shutemov 		ret |= VM_FAULT_LOCKED;
1979fb09a464SKirill A. Shutemov 	} else
1980fb09a464SKirill A. Shutemov 		VM_BUG_ON_PAGE(!PageLocked(page), page);
1981fb09a464SKirill A. Shutemov 	return ret;
1982fb09a464SKirill A. Shutemov }
1983fb09a464SKirill A. Shutemov 
1984fb09a464SKirill A. Shutemov /*
19854e047f89SShachar Raindel  * Handle write page faults for pages that can be reused in the current vma
19864e047f89SShachar Raindel  *
19874e047f89SShachar Raindel  * This can happen either due to the mapping being with the VM_SHARED flag,
19884e047f89SShachar Raindel  * or due to us being the last reference standing to the page. In either
19894e047f89SShachar Raindel  * case, all we need to do here is to mark the page as writable and update
19904e047f89SShachar Raindel  * any related book-keeping.
19914e047f89SShachar Raindel  */
19924e047f89SShachar Raindel static inline int wp_page_reuse(struct mm_struct *mm,
19934e047f89SShachar Raindel 			struct vm_area_struct *vma, unsigned long address,
19944e047f89SShachar Raindel 			pte_t *page_table, spinlock_t *ptl, pte_t orig_pte,
19954e047f89SShachar Raindel 			struct page *page, int page_mkwrite,
19964e047f89SShachar Raindel 			int dirty_shared)
19974e047f89SShachar Raindel 	__releases(ptl)
19984e047f89SShachar Raindel {
19994e047f89SShachar Raindel 	pte_t entry;
20004e047f89SShachar Raindel 	/*
20014e047f89SShachar Raindel 	 * Clear the pages cpupid information as the existing
20024e047f89SShachar Raindel 	 * information potentially belongs to a now completely
20034e047f89SShachar Raindel 	 * unrelated process.
20044e047f89SShachar Raindel 	 */
20054e047f89SShachar Raindel 	if (page)
20064e047f89SShachar Raindel 		page_cpupid_xchg_last(page, (1 << LAST_CPUPID_SHIFT) - 1);
20074e047f89SShachar Raindel 
20084e047f89SShachar Raindel 	flush_cache_page(vma, address, pte_pfn(orig_pte));
20094e047f89SShachar Raindel 	entry = pte_mkyoung(orig_pte);
20104e047f89SShachar Raindel 	entry = maybe_mkwrite(pte_mkdirty(entry), vma);
20114e047f89SShachar Raindel 	if (ptep_set_access_flags(vma, address, page_table, entry, 1))
20124e047f89SShachar Raindel 		update_mmu_cache(vma, address, page_table);
20134e047f89SShachar Raindel 	pte_unmap_unlock(page_table, ptl);
20144e047f89SShachar Raindel 
20154e047f89SShachar Raindel 	if (dirty_shared) {
20164e047f89SShachar Raindel 		struct address_space *mapping;
20174e047f89SShachar Raindel 		int dirtied;
20184e047f89SShachar Raindel 
20194e047f89SShachar Raindel 		if (!page_mkwrite)
20204e047f89SShachar Raindel 			lock_page(page);
20214e047f89SShachar Raindel 
20224e047f89SShachar Raindel 		dirtied = set_page_dirty(page);
20234e047f89SShachar Raindel 		VM_BUG_ON_PAGE(PageAnon(page), page);
20244e047f89SShachar Raindel 		mapping = page->mapping;
20254e047f89SShachar Raindel 		unlock_page(page);
20264e047f89SShachar Raindel 		page_cache_release(page);
20274e047f89SShachar Raindel 
20284e047f89SShachar Raindel 		if ((dirtied || page_mkwrite) && mapping) {
20294e047f89SShachar Raindel 			/*
20304e047f89SShachar Raindel 			 * Some device drivers do not set page.mapping
20314e047f89SShachar Raindel 			 * but still dirty their pages
20324e047f89SShachar Raindel 			 */
20334e047f89SShachar Raindel 			balance_dirty_pages_ratelimited(mapping);
20344e047f89SShachar Raindel 		}
20354e047f89SShachar Raindel 
20364e047f89SShachar Raindel 		if (!page_mkwrite)
20374e047f89SShachar Raindel 			file_update_time(vma->vm_file);
20384e047f89SShachar Raindel 	}
20394e047f89SShachar Raindel 
20404e047f89SShachar Raindel 	return VM_FAULT_WRITE;
20414e047f89SShachar Raindel }
20424e047f89SShachar Raindel 
20434e047f89SShachar Raindel /*
20442f38ab2cSShachar Raindel  * Handle the case of a page which we actually need to copy to a new page.
20452f38ab2cSShachar Raindel  *
20462f38ab2cSShachar Raindel  * Called with mmap_sem locked and the old page referenced, but
20472f38ab2cSShachar Raindel  * without the ptl held.
20482f38ab2cSShachar Raindel  *
20492f38ab2cSShachar Raindel  * High level logic flow:
20502f38ab2cSShachar Raindel  *
20512f38ab2cSShachar Raindel  * - Allocate a page, copy the content of the old page to the new one.
20522f38ab2cSShachar Raindel  * - Handle book keeping and accounting - cgroups, mmu-notifiers, etc.
20532f38ab2cSShachar Raindel  * - Take the PTL. If the pte changed, bail out and release the allocated page
20542f38ab2cSShachar Raindel  * - If the pte is still the way we remember it, update the page table and all
20552f38ab2cSShachar Raindel  *   relevant references. This includes dropping the reference the page-table
20562f38ab2cSShachar Raindel  *   held to the old page, as well as updating the rmap.
20572f38ab2cSShachar Raindel  * - In any case, unlock the PTL and drop the reference we took to the old page.
20582f38ab2cSShachar Raindel  */
20592f38ab2cSShachar Raindel static int wp_page_copy(struct mm_struct *mm, struct vm_area_struct *vma,
20602f38ab2cSShachar Raindel 			unsigned long address, pte_t *page_table, pmd_t *pmd,
20612f38ab2cSShachar Raindel 			pte_t orig_pte, struct page *old_page)
20622f38ab2cSShachar Raindel {
20632f38ab2cSShachar Raindel 	struct page *new_page = NULL;
20642f38ab2cSShachar Raindel 	spinlock_t *ptl = NULL;
20652f38ab2cSShachar Raindel 	pte_t entry;
20662f38ab2cSShachar Raindel 	int page_copied = 0;
20672f38ab2cSShachar Raindel 	const unsigned long mmun_start = address & PAGE_MASK;	/* For mmu_notifiers */
20682f38ab2cSShachar Raindel 	const unsigned long mmun_end = mmun_start + PAGE_SIZE;	/* For mmu_notifiers */
20692f38ab2cSShachar Raindel 	struct mem_cgroup *memcg;
20702f38ab2cSShachar Raindel 
20712f38ab2cSShachar Raindel 	if (unlikely(anon_vma_prepare(vma)))
20722f38ab2cSShachar Raindel 		goto oom;
20732f38ab2cSShachar Raindel 
20742f38ab2cSShachar Raindel 	if (is_zero_pfn(pte_pfn(orig_pte))) {
20752f38ab2cSShachar Raindel 		new_page = alloc_zeroed_user_highpage_movable(vma, address);
20762f38ab2cSShachar Raindel 		if (!new_page)
20772f38ab2cSShachar Raindel 			goto oom;
20782f38ab2cSShachar Raindel 	} else {
20792f38ab2cSShachar Raindel 		new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
20802f38ab2cSShachar Raindel 		if (!new_page)
20812f38ab2cSShachar Raindel 			goto oom;
20822f38ab2cSShachar Raindel 		cow_user_page(new_page, old_page, address, vma);
20832f38ab2cSShachar Raindel 	}
20842f38ab2cSShachar Raindel 	__SetPageUptodate(new_page);
20852f38ab2cSShachar Raindel 
20862f38ab2cSShachar Raindel 	if (mem_cgroup_try_charge(new_page, mm, GFP_KERNEL, &memcg))
20872f38ab2cSShachar Raindel 		goto oom_free_new;
20882f38ab2cSShachar Raindel 
20892f38ab2cSShachar Raindel 	mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
20902f38ab2cSShachar Raindel 
20912f38ab2cSShachar Raindel 	/*
20922f38ab2cSShachar Raindel 	 * Re-check the pte - we dropped the lock
20932f38ab2cSShachar Raindel 	 */
20942f38ab2cSShachar Raindel 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
20952f38ab2cSShachar Raindel 	if (likely(pte_same(*page_table, orig_pte))) {
20962f38ab2cSShachar Raindel 		if (old_page) {
20972f38ab2cSShachar Raindel 			if (!PageAnon(old_page)) {
20982f38ab2cSShachar Raindel 				dec_mm_counter_fast(mm, MM_FILEPAGES);
20992f38ab2cSShachar Raindel 				inc_mm_counter_fast(mm, MM_ANONPAGES);
21002f38ab2cSShachar Raindel 			}
21012f38ab2cSShachar Raindel 		} else {
21022f38ab2cSShachar Raindel 			inc_mm_counter_fast(mm, MM_ANONPAGES);
21032f38ab2cSShachar Raindel 		}
21042f38ab2cSShachar Raindel 		flush_cache_page(vma, address, pte_pfn(orig_pte));
21052f38ab2cSShachar Raindel 		entry = mk_pte(new_page, vma->vm_page_prot);
21062f38ab2cSShachar Raindel 		entry = maybe_mkwrite(pte_mkdirty(entry), vma);
21072f38ab2cSShachar Raindel 		/*
21082f38ab2cSShachar Raindel 		 * Clear the pte entry and flush it first, before updating the
21092f38ab2cSShachar Raindel 		 * pte with the new entry. This will avoid a race condition
21102f38ab2cSShachar Raindel 		 * seen in the presence of one thread doing SMC and another
21112f38ab2cSShachar Raindel 		 * thread doing COW.
21122f38ab2cSShachar Raindel 		 */
21132f38ab2cSShachar Raindel 		ptep_clear_flush_notify(vma, address, page_table);
21142f38ab2cSShachar Raindel 		page_add_new_anon_rmap(new_page, vma, address);
21152f38ab2cSShachar Raindel 		mem_cgroup_commit_charge(new_page, memcg, false);
21162f38ab2cSShachar Raindel 		lru_cache_add_active_or_unevictable(new_page, vma);
21172f38ab2cSShachar Raindel 		/*
21182f38ab2cSShachar Raindel 		 * We call the notify macro here because, when using secondary
21192f38ab2cSShachar Raindel 		 * mmu page tables (such as kvm shadow page tables), we want the
21202f38ab2cSShachar Raindel 		 * new page to be mapped directly into the secondary page table.
21212f38ab2cSShachar Raindel 		 */
21222f38ab2cSShachar Raindel 		set_pte_at_notify(mm, address, page_table, entry);
21232f38ab2cSShachar Raindel 		update_mmu_cache(vma, address, page_table);
21242f38ab2cSShachar Raindel 		if (old_page) {
21252f38ab2cSShachar Raindel 			/*
21262f38ab2cSShachar Raindel 			 * Only after switching the pte to the new page may
21272f38ab2cSShachar Raindel 			 * we remove the mapcount here. Otherwise another
21282f38ab2cSShachar Raindel 			 * process may come and find the rmap count decremented
21292f38ab2cSShachar Raindel 			 * before the pte is switched to the new page, and
21302f38ab2cSShachar Raindel 			 * "reuse" the old page writing into it while our pte
21312f38ab2cSShachar Raindel 			 * here still points into it and can be read by other
21322f38ab2cSShachar Raindel 			 * threads.
21332f38ab2cSShachar Raindel 			 *
21342f38ab2cSShachar Raindel 			 * The critical issue is to order this
21352f38ab2cSShachar Raindel 			 * page_remove_rmap with the ptp_clear_flush above.
21362f38ab2cSShachar Raindel 			 * Those stores are ordered by (if nothing else,)
21372f38ab2cSShachar Raindel 			 * the barrier present in the atomic_add_negative
21382f38ab2cSShachar Raindel 			 * in page_remove_rmap.
21392f38ab2cSShachar Raindel 			 *
21402f38ab2cSShachar Raindel 			 * Then the TLB flush in ptep_clear_flush ensures that
21412f38ab2cSShachar Raindel 			 * no process can access the old page before the
21422f38ab2cSShachar Raindel 			 * decremented mapcount is visible. And the old page
21432f38ab2cSShachar Raindel 			 * cannot be reused until after the decremented
21442f38ab2cSShachar Raindel 			 * mapcount is visible. So transitively, TLBs to
21452f38ab2cSShachar Raindel 			 * old page will be flushed before it can be reused.
21462f38ab2cSShachar Raindel 			 */
21472f38ab2cSShachar Raindel 			page_remove_rmap(old_page);
21482f38ab2cSShachar Raindel 		}
21492f38ab2cSShachar Raindel 
21502f38ab2cSShachar Raindel 		/* Free the old page.. */
21512f38ab2cSShachar Raindel 		new_page = old_page;
21522f38ab2cSShachar Raindel 		page_copied = 1;
21532f38ab2cSShachar Raindel 	} else {
21542f38ab2cSShachar Raindel 		mem_cgroup_cancel_charge(new_page, memcg);
21552f38ab2cSShachar Raindel 	}
21562f38ab2cSShachar Raindel 
21572f38ab2cSShachar Raindel 	if (new_page)
21582f38ab2cSShachar Raindel 		page_cache_release(new_page);
21592f38ab2cSShachar Raindel 
21602f38ab2cSShachar Raindel 	pte_unmap_unlock(page_table, ptl);
21612f38ab2cSShachar Raindel 	mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
21622f38ab2cSShachar Raindel 	if (old_page) {
21632f38ab2cSShachar Raindel 		/*
21642f38ab2cSShachar Raindel 		 * Don't let another task, with possibly unlocked vma,
21652f38ab2cSShachar Raindel 		 * keep the mlocked page.
21662f38ab2cSShachar Raindel 		 */
21672f38ab2cSShachar Raindel 		if (page_copied && (vma->vm_flags & VM_LOCKED)) {
21682f38ab2cSShachar Raindel 			lock_page(old_page);	/* LRU manipulation */
21692f38ab2cSShachar Raindel 			munlock_vma_page(old_page);
21702f38ab2cSShachar Raindel 			unlock_page(old_page);
21712f38ab2cSShachar Raindel 		}
21722f38ab2cSShachar Raindel 		page_cache_release(old_page);
21732f38ab2cSShachar Raindel 	}
21742f38ab2cSShachar Raindel 	return page_copied ? VM_FAULT_WRITE : 0;
21752f38ab2cSShachar Raindel oom_free_new:
21762f38ab2cSShachar Raindel 	page_cache_release(new_page);
21772f38ab2cSShachar Raindel oom:
21782f38ab2cSShachar Raindel 	if (old_page)
21792f38ab2cSShachar Raindel 		page_cache_release(old_page);
21802f38ab2cSShachar Raindel 	return VM_FAULT_OOM;
21812f38ab2cSShachar Raindel }
21822f38ab2cSShachar Raindel 
2183dd906184SBoaz Harrosh /*
2184dd906184SBoaz Harrosh  * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED
2185dd906184SBoaz Harrosh  * mapping
2186dd906184SBoaz Harrosh  */
2187dd906184SBoaz Harrosh static int wp_pfn_shared(struct mm_struct *mm,
2188dd906184SBoaz Harrosh 			struct vm_area_struct *vma, unsigned long address,
2189dd906184SBoaz Harrosh 			pte_t *page_table, spinlock_t *ptl, pte_t orig_pte,
2190dd906184SBoaz Harrosh 			pmd_t *pmd)
2191dd906184SBoaz Harrosh {
2192dd906184SBoaz Harrosh 	if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) {
2193dd906184SBoaz Harrosh 		struct vm_fault vmf = {
2194dd906184SBoaz Harrosh 			.page = NULL,
2195dd906184SBoaz Harrosh 			.pgoff = linear_page_index(vma, address),
2196dd906184SBoaz Harrosh 			.virtual_address = (void __user *)(address & PAGE_MASK),
2197dd906184SBoaz Harrosh 			.flags = FAULT_FLAG_WRITE | FAULT_FLAG_MKWRITE,
2198dd906184SBoaz Harrosh 		};
2199dd906184SBoaz Harrosh 		int ret;
2200dd906184SBoaz Harrosh 
2201dd906184SBoaz Harrosh 		pte_unmap_unlock(page_table, ptl);
2202dd906184SBoaz Harrosh 		ret = vma->vm_ops->pfn_mkwrite(vma, &vmf);
2203dd906184SBoaz Harrosh 		if (ret & VM_FAULT_ERROR)
2204dd906184SBoaz Harrosh 			return ret;
2205dd906184SBoaz Harrosh 		page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
2206dd906184SBoaz Harrosh 		/*
2207dd906184SBoaz Harrosh 		 * We might have raced with another page fault while we
2208dd906184SBoaz Harrosh 		 * released the pte_offset_map_lock.
2209dd906184SBoaz Harrosh 		 */
2210dd906184SBoaz Harrosh 		if (!pte_same(*page_table, orig_pte)) {
2211dd906184SBoaz Harrosh 			pte_unmap_unlock(page_table, ptl);
2212dd906184SBoaz Harrosh 			return 0;
2213dd906184SBoaz Harrosh 		}
2214dd906184SBoaz Harrosh 	}
2215dd906184SBoaz Harrosh 	return wp_page_reuse(mm, vma, address, page_table, ptl, orig_pte,
2216dd906184SBoaz Harrosh 			     NULL, 0, 0);
2217dd906184SBoaz Harrosh }
2218dd906184SBoaz Harrosh 
221993e478d4SShachar Raindel static int wp_page_shared(struct mm_struct *mm, struct vm_area_struct *vma,
222093e478d4SShachar Raindel 			  unsigned long address, pte_t *page_table,
222193e478d4SShachar Raindel 			  pmd_t *pmd, spinlock_t *ptl, pte_t orig_pte,
222293e478d4SShachar Raindel 			  struct page *old_page)
222393e478d4SShachar Raindel 	__releases(ptl)
222493e478d4SShachar Raindel {
222593e478d4SShachar Raindel 	int page_mkwrite = 0;
222693e478d4SShachar Raindel 
222793e478d4SShachar Raindel 	page_cache_get(old_page);
222893e478d4SShachar Raindel 
222993e478d4SShachar Raindel 	/*
223093e478d4SShachar Raindel 	 * Only catch write-faults on shared writable pages,
223193e478d4SShachar Raindel 	 * read-only shared pages can get COWed by
223293e478d4SShachar Raindel 	 * get_user_pages(.write=1, .force=1).
223393e478d4SShachar Raindel 	 */
223493e478d4SShachar Raindel 	if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
223593e478d4SShachar Raindel 		int tmp;
223693e478d4SShachar Raindel 
223793e478d4SShachar Raindel 		pte_unmap_unlock(page_table, ptl);
223893e478d4SShachar Raindel 		tmp = do_page_mkwrite(vma, old_page, address);
223993e478d4SShachar Raindel 		if (unlikely(!tmp || (tmp &
224093e478d4SShachar Raindel 				      (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
224193e478d4SShachar Raindel 			page_cache_release(old_page);
224293e478d4SShachar Raindel 			return tmp;
224393e478d4SShachar Raindel 		}
224493e478d4SShachar Raindel 		/*
224593e478d4SShachar Raindel 		 * Since we dropped the lock we need to revalidate
224693e478d4SShachar Raindel 		 * the PTE as someone else may have changed it.  If
224793e478d4SShachar Raindel 		 * they did, we just return, as we can count on the
224893e478d4SShachar Raindel 		 * MMU to tell us if they didn't also make it writable.
224993e478d4SShachar Raindel 		 */
225093e478d4SShachar Raindel 		page_table = pte_offset_map_lock(mm, pmd, address,
225193e478d4SShachar Raindel 						 &ptl);
225293e478d4SShachar Raindel 		if (!pte_same(*page_table, orig_pte)) {
225393e478d4SShachar Raindel 			unlock_page(old_page);
225493e478d4SShachar Raindel 			pte_unmap_unlock(page_table, ptl);
225593e478d4SShachar Raindel 			page_cache_release(old_page);
225693e478d4SShachar Raindel 			return 0;
225793e478d4SShachar Raindel 		}
225893e478d4SShachar Raindel 		page_mkwrite = 1;
225993e478d4SShachar Raindel 	}
226093e478d4SShachar Raindel 
226193e478d4SShachar Raindel 	return wp_page_reuse(mm, vma, address, page_table, ptl,
226293e478d4SShachar Raindel 			     orig_pte, old_page, page_mkwrite, 1);
226393e478d4SShachar Raindel }
226493e478d4SShachar Raindel 
22652f38ab2cSShachar Raindel /*
22661da177e4SLinus Torvalds  * This routine handles present pages, when users try to write
22671da177e4SLinus Torvalds  * to a shared page. It is done by copying the page to a new address
22681da177e4SLinus Torvalds  * and decrementing the shared-page counter for the old page.
22691da177e4SLinus Torvalds  *
22701da177e4SLinus Torvalds  * Note that this routine assumes that the protection checks have been
22711da177e4SLinus Torvalds  * done by the caller (the low-level page fault routine in most cases).
22721da177e4SLinus Torvalds  * Thus we can safely just mark it writable once we've done any necessary
22731da177e4SLinus Torvalds  * COW.
22741da177e4SLinus Torvalds  *
22751da177e4SLinus Torvalds  * We also mark the page dirty at this point even though the page will
22761da177e4SLinus Torvalds  * change only once the write actually happens. This avoids a few races,
22771da177e4SLinus Torvalds  * and potentially makes it more efficient.
22781da177e4SLinus Torvalds  *
22798f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
22808f4e2101SHugh Dickins  * but allow concurrent faults), with pte both mapped and locked.
22818f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
22821da177e4SLinus Torvalds  */
22831da177e4SLinus Torvalds static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
228465500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
22858f4e2101SHugh Dickins 		spinlock_t *ptl, pte_t orig_pte)
2286e6219ec8SNamhyung Kim 	__releases(ptl)
22871da177e4SLinus Torvalds {
22882f38ab2cSShachar Raindel 	struct page *old_page;
22891da177e4SLinus Torvalds 
22906aab341eSLinus Torvalds 	old_page = vm_normal_page(vma, address, orig_pte);
2291251b97f5SPeter Zijlstra 	if (!old_page) {
2292251b97f5SPeter Zijlstra 		/*
229364e45507SPeter Feiner 		 * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a
229464e45507SPeter Feiner 		 * VM_PFNMAP VMA.
2295251b97f5SPeter Zijlstra 		 *
2296251b97f5SPeter Zijlstra 		 * We should not cow pages in a shared writeable mapping.
2297dd906184SBoaz Harrosh 		 * Just mark the pages writable and/or call ops->pfn_mkwrite.
2298251b97f5SPeter Zijlstra 		 */
2299251b97f5SPeter Zijlstra 		if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
2300251b97f5SPeter Zijlstra 				     (VM_WRITE|VM_SHARED))
2301dd906184SBoaz Harrosh 			return wp_pfn_shared(mm, vma, address, page_table, ptl,
2302dd906184SBoaz Harrosh 					     orig_pte, pmd);
23032f38ab2cSShachar Raindel 
23042f38ab2cSShachar Raindel 		pte_unmap_unlock(page_table, ptl);
23052f38ab2cSShachar Raindel 		return wp_page_copy(mm, vma, address, page_table, pmd,
23062f38ab2cSShachar Raindel 				    orig_pte, old_page);
2307251b97f5SPeter Zijlstra 	}
23081da177e4SLinus Torvalds 
2309d08b3851SPeter Zijlstra 	/*
2310ee6a6457SPeter Zijlstra 	 * Take out anonymous pages first, anonymous shared vmas are
2311ee6a6457SPeter Zijlstra 	 * not dirty accountable.
2312d08b3851SPeter Zijlstra 	 */
23139a840895SHugh Dickins 	if (PageAnon(old_page) && !PageKsm(old_page)) {
2314ab967d86SHugh Dickins 		if (!trylock_page(old_page)) {
2315ab967d86SHugh Dickins 			page_cache_get(old_page);
2316ab967d86SHugh Dickins 			pte_unmap_unlock(page_table, ptl);
2317ab967d86SHugh Dickins 			lock_page(old_page);
2318ab967d86SHugh Dickins 			page_table = pte_offset_map_lock(mm, pmd, address,
2319ab967d86SHugh Dickins 							 &ptl);
2320ab967d86SHugh Dickins 			if (!pte_same(*page_table, orig_pte)) {
2321ab967d86SHugh Dickins 				unlock_page(old_page);
232228766805SShachar Raindel 				pte_unmap_unlock(page_table, ptl);
232328766805SShachar Raindel 				page_cache_release(old_page);
232428766805SShachar Raindel 				return 0;
2325ab967d86SHugh Dickins 			}
2326ab967d86SHugh Dickins 			page_cache_release(old_page);
2327ab967d86SHugh Dickins 		}
2328b009c024SMichel Lespinasse 		if (reuse_swap_page(old_page)) {
2329c44b6743SRik van Riel 			/*
2330c44b6743SRik van Riel 			 * The page is all ours.  Move it to our anon_vma so
2331c44b6743SRik van Riel 			 * the rmap code will not search our parent or siblings.
2332c44b6743SRik van Riel 			 * Protected against the rmap code by the page lock.
2333c44b6743SRik van Riel 			 */
2334c44b6743SRik van Riel 			page_move_anon_rmap(old_page, vma, address);
2335ee6a6457SPeter Zijlstra 			unlock_page(old_page);
23364e047f89SShachar Raindel 			return wp_page_reuse(mm, vma, address, page_table, ptl,
23374e047f89SShachar Raindel 					     orig_pte, old_page, 0, 0);
2338b009c024SMichel Lespinasse 		}
2339b009c024SMichel Lespinasse 		unlock_page(old_page);
2340ee6a6457SPeter Zijlstra 	} else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
2341d08b3851SPeter Zijlstra 					(VM_WRITE|VM_SHARED))) {
234293e478d4SShachar Raindel 		return wp_page_shared(mm, vma, address, page_table, pmd,
234393e478d4SShachar Raindel 				      ptl, orig_pte, old_page);
23441da177e4SLinus Torvalds 	}
23451da177e4SLinus Torvalds 
23461da177e4SLinus Torvalds 	/*
23471da177e4SLinus Torvalds 	 * Ok, we need to copy. Oh, well..
23481da177e4SLinus Torvalds 	 */
23491da177e4SLinus Torvalds 	page_cache_get(old_page);
235028766805SShachar Raindel 
23518f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
23522f38ab2cSShachar Raindel 	return wp_page_copy(mm, vma, address, page_table, pmd,
23532f38ab2cSShachar Raindel 			    orig_pte, old_page);
23541da177e4SLinus Torvalds }
23551da177e4SLinus Torvalds 
235697a89413SPeter Zijlstra static void unmap_mapping_range_vma(struct vm_area_struct *vma,
23571da177e4SLinus Torvalds 		unsigned long start_addr, unsigned long end_addr,
23581da177e4SLinus Torvalds 		struct zap_details *details)
23591da177e4SLinus Torvalds {
2360f5cc4eefSAl Viro 	zap_page_range_single(vma, start_addr, end_addr - start_addr, details);
23611da177e4SLinus Torvalds }
23621da177e4SLinus Torvalds 
23636b2dbba8SMichel Lespinasse static inline void unmap_mapping_range_tree(struct rb_root *root,
23641da177e4SLinus Torvalds 					    struct zap_details *details)
23651da177e4SLinus Torvalds {
23661da177e4SLinus Torvalds 	struct vm_area_struct *vma;
23671da177e4SLinus Torvalds 	pgoff_t vba, vea, zba, zea;
23681da177e4SLinus Torvalds 
23696b2dbba8SMichel Lespinasse 	vma_interval_tree_foreach(vma, root,
23701da177e4SLinus Torvalds 			details->first_index, details->last_index) {
23711da177e4SLinus Torvalds 
23721da177e4SLinus Torvalds 		vba = vma->vm_pgoff;
2373d6e93217SLibin 		vea = vba + vma_pages(vma) - 1;
23741da177e4SLinus Torvalds 		/* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
23751da177e4SLinus Torvalds 		zba = details->first_index;
23761da177e4SLinus Torvalds 		if (zba < vba)
23771da177e4SLinus Torvalds 			zba = vba;
23781da177e4SLinus Torvalds 		zea = details->last_index;
23791da177e4SLinus Torvalds 		if (zea > vea)
23801da177e4SLinus Torvalds 			zea = vea;
23811da177e4SLinus Torvalds 
238297a89413SPeter Zijlstra 		unmap_mapping_range_vma(vma,
23831da177e4SLinus Torvalds 			((zba - vba) << PAGE_SHIFT) + vma->vm_start,
23841da177e4SLinus Torvalds 			((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
238597a89413SPeter Zijlstra 				details);
23861da177e4SLinus Torvalds 	}
23871da177e4SLinus Torvalds }
23881da177e4SLinus Torvalds 
23891da177e4SLinus Torvalds /**
23908a5f14a2SKirill A. Shutemov  * unmap_mapping_range - unmap the portion of all mmaps in the specified
23918a5f14a2SKirill A. Shutemov  * address_space corresponding to the specified page range in the underlying
23928a5f14a2SKirill A. Shutemov  * file.
23938a5f14a2SKirill A. Shutemov  *
23943d41088fSMartin Waitz  * @mapping: the address space containing mmaps to be unmapped.
23951da177e4SLinus Torvalds  * @holebegin: byte in first page to unmap, relative to the start of
23961da177e4SLinus Torvalds  * the underlying file.  This will be rounded down to a PAGE_SIZE
239725d9e2d1Snpiggin@suse.de  * boundary.  Note that this is different from truncate_pagecache(), which
23981da177e4SLinus Torvalds  * must keep the partial page.  In contrast, we must get rid of
23991da177e4SLinus Torvalds  * partial pages.
24001da177e4SLinus Torvalds  * @holelen: size of prospective hole in bytes.  This will be rounded
24011da177e4SLinus Torvalds  * up to a PAGE_SIZE boundary.  A holelen of zero truncates to the
24021da177e4SLinus Torvalds  * end of the file.
24031da177e4SLinus Torvalds  * @even_cows: 1 when truncating a file, unmap even private COWed pages;
24041da177e4SLinus Torvalds  * but 0 when invalidating pagecache, don't throw away private data.
24051da177e4SLinus Torvalds  */
24061da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping,
24071da177e4SLinus Torvalds 		loff_t const holebegin, loff_t const holelen, int even_cows)
24081da177e4SLinus Torvalds {
24091da177e4SLinus Torvalds 	struct zap_details details;
24101da177e4SLinus Torvalds 	pgoff_t hba = holebegin >> PAGE_SHIFT;
24111da177e4SLinus Torvalds 	pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
24121da177e4SLinus Torvalds 
24131da177e4SLinus Torvalds 	/* Check for overflow. */
24141da177e4SLinus Torvalds 	if (sizeof(holelen) > sizeof(hlen)) {
24151da177e4SLinus Torvalds 		long long holeend =
24161da177e4SLinus Torvalds 			(holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
24171da177e4SLinus Torvalds 		if (holeend & ~(long long)ULONG_MAX)
24181da177e4SLinus Torvalds 			hlen = ULONG_MAX - hba + 1;
24191da177e4SLinus Torvalds 	}
24201da177e4SLinus Torvalds 
24211da177e4SLinus Torvalds 	details.check_mapping = even_cows? NULL: mapping;
24221da177e4SLinus Torvalds 	details.first_index = hba;
24231da177e4SLinus Torvalds 	details.last_index = hba + hlen - 1;
24241da177e4SLinus Torvalds 	if (details.last_index < details.first_index)
24251da177e4SLinus Torvalds 		details.last_index = ULONG_MAX;
24261da177e4SLinus Torvalds 
242797a89413SPeter Zijlstra 
2428283307c7SMatthew Wilcox 	/* DAX uses i_mmap_lock to serialise file truncate vs page fault */
242948ec833bSKirill A. Shutemov 	i_mmap_lock_write(mapping);
24306b2dbba8SMichel Lespinasse 	if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap)))
24311da177e4SLinus Torvalds 		unmap_mapping_range_tree(&mapping->i_mmap, &details);
243248ec833bSKirill A. Shutemov 	i_mmap_unlock_write(mapping);
24331da177e4SLinus Torvalds }
24341da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range);
24351da177e4SLinus Torvalds 
24361da177e4SLinus Torvalds /*
24378f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
24388f4e2101SHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
24399a95f3cfSPaul Cassella  * We return with pte unmapped and unlocked.
24409a95f3cfSPaul Cassella  *
24419a95f3cfSPaul Cassella  * We return with the mmap_sem locked or unlocked in the same cases
24429a95f3cfSPaul Cassella  * as does filemap_fault().
24431da177e4SLinus Torvalds  */
244465500d23SHugh Dickins static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
244565500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
244630c9f3a9SLinus Torvalds 		unsigned int flags, pte_t orig_pte)
24471da177e4SLinus Torvalds {
24488f4e2101SHugh Dickins 	spinlock_t *ptl;
244956f31801SHugh Dickins 	struct page *page, *swapcache;
245000501b53SJohannes Weiner 	struct mem_cgroup *memcg;
245165500d23SHugh Dickins 	swp_entry_t entry;
24521da177e4SLinus Torvalds 	pte_t pte;
2453d065bd81SMichel Lespinasse 	int locked;
2454ad8c2ee8SRik van Riel 	int exclusive = 0;
245583c54070SNick Piggin 	int ret = 0;
24561da177e4SLinus Torvalds 
24574c21e2f2SHugh Dickins 	if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
24588f4e2101SHugh Dickins 		goto out;
245965500d23SHugh Dickins 
246065500d23SHugh Dickins 	entry = pte_to_swp_entry(orig_pte);
2461d1737fdbSAndi Kleen 	if (unlikely(non_swap_entry(entry))) {
24620697212aSChristoph Lameter 		if (is_migration_entry(entry)) {
24630697212aSChristoph Lameter 			migration_entry_wait(mm, pmd, address);
2464d1737fdbSAndi Kleen 		} else if (is_hwpoison_entry(entry)) {
2465d1737fdbSAndi Kleen 			ret = VM_FAULT_HWPOISON;
2466d1737fdbSAndi Kleen 		} else {
2467d1737fdbSAndi Kleen 			print_bad_pte(vma, address, orig_pte, NULL);
2468d99be1a8SHugh Dickins 			ret = VM_FAULT_SIGBUS;
2469d1737fdbSAndi Kleen 		}
24700697212aSChristoph Lameter 		goto out;
24710697212aSChristoph Lameter 	}
24720ff92245SShailabh Nagar 	delayacct_set_flag(DELAYACCT_PF_SWAPIN);
24731da177e4SLinus Torvalds 	page = lookup_swap_cache(entry);
24741da177e4SLinus Torvalds 	if (!page) {
247502098feaSHugh Dickins 		page = swapin_readahead(entry,
247602098feaSHugh Dickins 					GFP_HIGHUSER_MOVABLE, vma, address);
24771da177e4SLinus Torvalds 		if (!page) {
24781da177e4SLinus Torvalds 			/*
24798f4e2101SHugh Dickins 			 * Back out if somebody else faulted in this pte
24808f4e2101SHugh Dickins 			 * while we released the pte lock.
24811da177e4SLinus Torvalds 			 */
24828f4e2101SHugh Dickins 			page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
24831da177e4SLinus Torvalds 			if (likely(pte_same(*page_table, orig_pte)))
24841da177e4SLinus Torvalds 				ret = VM_FAULT_OOM;
24850ff92245SShailabh Nagar 			delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
248665500d23SHugh Dickins 			goto unlock;
24871da177e4SLinus Torvalds 		}
24881da177e4SLinus Torvalds 
24891da177e4SLinus Torvalds 		/* Had to read the page from swap area: Major fault */
24901da177e4SLinus Torvalds 		ret = VM_FAULT_MAJOR;
2491f8891e5eSChristoph Lameter 		count_vm_event(PGMAJFAULT);
2492456f998eSYing Han 		mem_cgroup_count_vm_event(mm, PGMAJFAULT);
2493d1737fdbSAndi Kleen 	} else if (PageHWPoison(page)) {
249471f72525SWu Fengguang 		/*
249571f72525SWu Fengguang 		 * hwpoisoned dirty swapcache pages are kept for killing
249671f72525SWu Fengguang 		 * owner processes (which may be unknown at hwpoison time)
249771f72525SWu Fengguang 		 */
2498d1737fdbSAndi Kleen 		ret = VM_FAULT_HWPOISON;
2499d1737fdbSAndi Kleen 		delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
250056f31801SHugh Dickins 		swapcache = page;
25014779cb31SAndi Kleen 		goto out_release;
25021da177e4SLinus Torvalds 	}
25031da177e4SLinus Torvalds 
250456f31801SHugh Dickins 	swapcache = page;
2505d065bd81SMichel Lespinasse 	locked = lock_page_or_retry(page, mm, flags);
2506e709ffd6SRik van Riel 
250720a1022dSBalbir Singh 	delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
2508d065bd81SMichel Lespinasse 	if (!locked) {
2509d065bd81SMichel Lespinasse 		ret |= VM_FAULT_RETRY;
2510d065bd81SMichel Lespinasse 		goto out_release;
2511d065bd81SMichel Lespinasse 	}
25121da177e4SLinus Torvalds 
25134969c119SAndrea Arcangeli 	/*
251431c4a3d3SHugh Dickins 	 * Make sure try_to_free_swap or reuse_swap_page or swapoff did not
251531c4a3d3SHugh Dickins 	 * release the swapcache from under us.  The page pin, and pte_same
251631c4a3d3SHugh Dickins 	 * test below, are not enough to exclude that.  Even if it is still
251731c4a3d3SHugh Dickins 	 * swapcache, we need to check that the page's swap has not changed.
25184969c119SAndrea Arcangeli 	 */
251931c4a3d3SHugh Dickins 	if (unlikely(!PageSwapCache(page) || page_private(page) != entry.val))
25204969c119SAndrea Arcangeli 		goto out_page;
25214969c119SAndrea Arcangeli 
2522cbf86cfeSHugh Dickins 	page = ksm_might_need_to_copy(page, vma, address);
25234969c119SAndrea Arcangeli 	if (unlikely(!page)) {
25245ad64688SHugh Dickins 		ret = VM_FAULT_OOM;
25254969c119SAndrea Arcangeli 		page = swapcache;
25264969c119SAndrea Arcangeli 		goto out_page;
25274969c119SAndrea Arcangeli 	}
25285ad64688SHugh Dickins 
252900501b53SJohannes Weiner 	if (mem_cgroup_try_charge(page, mm, GFP_KERNEL, &memcg)) {
2530073e587eSKAMEZAWA Hiroyuki 		ret = VM_FAULT_OOM;
2531bc43f75cSJohannes Weiner 		goto out_page;
2532073e587eSKAMEZAWA Hiroyuki 	}
2533073e587eSKAMEZAWA Hiroyuki 
25341da177e4SLinus Torvalds 	/*
25358f4e2101SHugh Dickins 	 * Back out if somebody else already faulted in this pte.
25361da177e4SLinus Torvalds 	 */
25378f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
25389e9bef07SHugh Dickins 	if (unlikely(!pte_same(*page_table, orig_pte)))
2539b8107480SKirill Korotaev 		goto out_nomap;
2540b8107480SKirill Korotaev 
2541b8107480SKirill Korotaev 	if (unlikely(!PageUptodate(page))) {
2542b8107480SKirill Korotaev 		ret = VM_FAULT_SIGBUS;
2543b8107480SKirill Korotaev 		goto out_nomap;
25441da177e4SLinus Torvalds 	}
25451da177e4SLinus Torvalds 
25468c7c6e34SKAMEZAWA Hiroyuki 	/*
25478c7c6e34SKAMEZAWA Hiroyuki 	 * The page isn't present yet, go ahead with the fault.
25488c7c6e34SKAMEZAWA Hiroyuki 	 *
25498c7c6e34SKAMEZAWA Hiroyuki 	 * Be careful about the sequence of operations here.
25508c7c6e34SKAMEZAWA Hiroyuki 	 * To get its accounting right, reuse_swap_page() must be called
25518c7c6e34SKAMEZAWA Hiroyuki 	 * while the page is counted on swap but not yet in mapcount i.e.
25528c7c6e34SKAMEZAWA Hiroyuki 	 * before page_add_anon_rmap() and swap_free(); try_to_free_swap()
25538c7c6e34SKAMEZAWA Hiroyuki 	 * must be called after the swap_free(), or it will never succeed.
25548c7c6e34SKAMEZAWA Hiroyuki 	 */
25551da177e4SLinus Torvalds 
255634e55232SKAMEZAWA Hiroyuki 	inc_mm_counter_fast(mm, MM_ANONPAGES);
2557b084d435SKAMEZAWA Hiroyuki 	dec_mm_counter_fast(mm, MM_SWAPENTS);
25581da177e4SLinus Torvalds 	pte = mk_pte(page, vma->vm_page_prot);
255930c9f3a9SLinus Torvalds 	if ((flags & FAULT_FLAG_WRITE) && reuse_swap_page(page)) {
25601da177e4SLinus Torvalds 		pte = maybe_mkwrite(pte_mkdirty(pte), vma);
256130c9f3a9SLinus Torvalds 		flags &= ~FAULT_FLAG_WRITE;
25629a5b489bSAndrea Arcangeli 		ret |= VM_FAULT_WRITE;
2563ad8c2ee8SRik van Riel 		exclusive = 1;
25641da177e4SLinus Torvalds 	}
25651da177e4SLinus Torvalds 	flush_icache_page(vma, page);
2566179ef71cSCyrill Gorcunov 	if (pte_swp_soft_dirty(orig_pte))
2567179ef71cSCyrill Gorcunov 		pte = pte_mksoft_dirty(pte);
25681da177e4SLinus Torvalds 	set_pte_at(mm, address, page_table, pte);
256900501b53SJohannes Weiner 	if (page == swapcache) {
2570ad8c2ee8SRik van Riel 		do_page_add_anon_rmap(page, vma, address, exclusive);
257100501b53SJohannes Weiner 		mem_cgroup_commit_charge(page, memcg, true);
257200501b53SJohannes Weiner 	} else { /* ksm created a completely new copy */
257356f31801SHugh Dickins 		page_add_new_anon_rmap(page, vma, address);
257400501b53SJohannes Weiner 		mem_cgroup_commit_charge(page, memcg, false);
257500501b53SJohannes Weiner 		lru_cache_add_active_or_unevictable(page, vma);
257600501b53SJohannes Weiner 	}
25771da177e4SLinus Torvalds 
2578c475a8abSHugh Dickins 	swap_free(entry);
2579b291f000SNick Piggin 	if (vm_swap_full() || (vma->vm_flags & VM_LOCKED) || PageMlocked(page))
2580a2c43eedSHugh Dickins 		try_to_free_swap(page);
2581c475a8abSHugh Dickins 	unlock_page(page);
258256f31801SHugh Dickins 	if (page != swapcache) {
25834969c119SAndrea Arcangeli 		/*
25844969c119SAndrea Arcangeli 		 * Hold the lock to avoid the swap entry to be reused
25854969c119SAndrea Arcangeli 		 * until we take the PT lock for the pte_same() check
25864969c119SAndrea Arcangeli 		 * (to avoid false positives from pte_same). For
25874969c119SAndrea Arcangeli 		 * further safety release the lock after the swap_free
25884969c119SAndrea Arcangeli 		 * so that the swap count won't change under a
25894969c119SAndrea Arcangeli 		 * parallel locked swapcache.
25904969c119SAndrea Arcangeli 		 */
25914969c119SAndrea Arcangeli 		unlock_page(swapcache);
25924969c119SAndrea Arcangeli 		page_cache_release(swapcache);
25934969c119SAndrea Arcangeli 	}
2594c475a8abSHugh Dickins 
259530c9f3a9SLinus Torvalds 	if (flags & FAULT_FLAG_WRITE) {
259661469f1dSHugh Dickins 		ret |= do_wp_page(mm, vma, address, page_table, pmd, ptl, pte);
259761469f1dSHugh Dickins 		if (ret & VM_FAULT_ERROR)
259861469f1dSHugh Dickins 			ret &= VM_FAULT_ERROR;
25991da177e4SLinus Torvalds 		goto out;
26001da177e4SLinus Torvalds 	}
26011da177e4SLinus Torvalds 
26021da177e4SLinus Torvalds 	/* No need to invalidate - it was non-present before */
26034b3073e1SRussell King 	update_mmu_cache(vma, address, page_table);
260465500d23SHugh Dickins unlock:
26058f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
26061da177e4SLinus Torvalds out:
26071da177e4SLinus Torvalds 	return ret;
2608b8107480SKirill Korotaev out_nomap:
260900501b53SJohannes Weiner 	mem_cgroup_cancel_charge(page, memcg);
26108f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
2611bc43f75cSJohannes Weiner out_page:
2612b8107480SKirill Korotaev 	unlock_page(page);
26134779cb31SAndi Kleen out_release:
2614b8107480SKirill Korotaev 	page_cache_release(page);
261556f31801SHugh Dickins 	if (page != swapcache) {
26164969c119SAndrea Arcangeli 		unlock_page(swapcache);
26174969c119SAndrea Arcangeli 		page_cache_release(swapcache);
26184969c119SAndrea Arcangeli 	}
261965500d23SHugh Dickins 	return ret;
26201da177e4SLinus Torvalds }
26211da177e4SLinus Torvalds 
26221da177e4SLinus Torvalds /*
26238ca3eb08SLuck, Tony  * This is like a special single-page "expand_{down|up}wards()",
26248ca3eb08SLuck, Tony  * except we must first make sure that 'address{-|+}PAGE_SIZE'
2625320b2b8dSLinus Torvalds  * doesn't hit another vma.
2626320b2b8dSLinus Torvalds  */
2627320b2b8dSLinus Torvalds static inline int check_stack_guard_page(struct vm_area_struct *vma, unsigned long address)
2628320b2b8dSLinus Torvalds {
2629320b2b8dSLinus Torvalds 	address &= PAGE_MASK;
2630320b2b8dSLinus Torvalds 	if ((vma->vm_flags & VM_GROWSDOWN) && address == vma->vm_start) {
26310e8e50e2SLinus Torvalds 		struct vm_area_struct *prev = vma->vm_prev;
2632320b2b8dSLinus Torvalds 
26330e8e50e2SLinus Torvalds 		/*
26340e8e50e2SLinus Torvalds 		 * Is there a mapping abutting this one below?
26350e8e50e2SLinus Torvalds 		 *
26360e8e50e2SLinus Torvalds 		 * That's only ok if it's the same stack mapping
26370e8e50e2SLinus Torvalds 		 * that has gotten split..
26380e8e50e2SLinus Torvalds 		 */
26390e8e50e2SLinus Torvalds 		if (prev && prev->vm_end == address)
26400e8e50e2SLinus Torvalds 			return prev->vm_flags & VM_GROWSDOWN ? 0 : -ENOMEM;
26410e8e50e2SLinus Torvalds 
2642fee7e49dSLinus Torvalds 		return expand_downwards(vma, address - PAGE_SIZE);
2643320b2b8dSLinus Torvalds 	}
26448ca3eb08SLuck, Tony 	if ((vma->vm_flags & VM_GROWSUP) && address + PAGE_SIZE == vma->vm_end) {
26458ca3eb08SLuck, Tony 		struct vm_area_struct *next = vma->vm_next;
26468ca3eb08SLuck, Tony 
26478ca3eb08SLuck, Tony 		/* As VM_GROWSDOWN but s/below/above/ */
26488ca3eb08SLuck, Tony 		if (next && next->vm_start == address + PAGE_SIZE)
26498ca3eb08SLuck, Tony 			return next->vm_flags & VM_GROWSUP ? 0 : -ENOMEM;
26508ca3eb08SLuck, Tony 
2651fee7e49dSLinus Torvalds 		return expand_upwards(vma, address + PAGE_SIZE);
26528ca3eb08SLuck, Tony 	}
2653320b2b8dSLinus Torvalds 	return 0;
2654320b2b8dSLinus Torvalds }
2655320b2b8dSLinus Torvalds 
2656320b2b8dSLinus Torvalds /*
26578f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
26588f4e2101SHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
26598f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
26601da177e4SLinus Torvalds  */
266165500d23SHugh Dickins static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
266265500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
266330c9f3a9SLinus Torvalds 		unsigned int flags)
26641da177e4SLinus Torvalds {
266500501b53SJohannes Weiner 	struct mem_cgroup *memcg;
26668f4e2101SHugh Dickins 	struct page *page;
26678f4e2101SHugh Dickins 	spinlock_t *ptl;
26681da177e4SLinus Torvalds 	pte_t entry;
26691da177e4SLinus Torvalds 
26705528f913SLinus Torvalds 	pte_unmap(page_table);
2671320b2b8dSLinus Torvalds 
267211ac5524SLinus Torvalds 	/* Check if we need to add a guard page to the stack */
267311ac5524SLinus Torvalds 	if (check_stack_guard_page(vma, address) < 0)
26749c145c56SLinus Torvalds 		return VM_FAULT_SIGSEGV;
267511ac5524SLinus Torvalds 
267611ac5524SLinus Torvalds 	/* Use the zero-page for reads */
2677593befa6SDominik Dingel 	if (!(flags & FAULT_FLAG_WRITE) && !mm_forbids_zeropage(mm)) {
267862eede62SHugh Dickins 		entry = pte_mkspecial(pfn_pte(my_zero_pfn(address),
267962eede62SHugh Dickins 						vma->vm_page_prot));
268011ac5524SLinus Torvalds 		page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
2681a13ea5b7SHugh Dickins 		if (!pte_none(*page_table))
2682a13ea5b7SHugh Dickins 			goto unlock;
2683a13ea5b7SHugh Dickins 		goto setpte;
2684a13ea5b7SHugh Dickins 	}
2685a13ea5b7SHugh Dickins 
26861da177e4SLinus Torvalds 	/* Allocate our own private page. */
26871da177e4SLinus Torvalds 	if (unlikely(anon_vma_prepare(vma)))
268865500d23SHugh Dickins 		goto oom;
2689769848c0SMel Gorman 	page = alloc_zeroed_user_highpage_movable(vma, address);
26901da177e4SLinus Torvalds 	if (!page)
269165500d23SHugh Dickins 		goto oom;
269252f37629SMinchan Kim 	/*
269352f37629SMinchan Kim 	 * The memory barrier inside __SetPageUptodate makes sure that
269452f37629SMinchan Kim 	 * preceeding stores to the page contents become visible before
269552f37629SMinchan Kim 	 * the set_pte_at() write.
269652f37629SMinchan Kim 	 */
26970ed361deSNick Piggin 	__SetPageUptodate(page);
26981da177e4SLinus Torvalds 
269900501b53SJohannes Weiner 	if (mem_cgroup_try_charge(page, mm, GFP_KERNEL, &memcg))
27008a9f3ccdSBalbir Singh 		goto oom_free_page;
27018a9f3ccdSBalbir Singh 
270265500d23SHugh Dickins 	entry = mk_pte(page, vma->vm_page_prot);
27031ac0cb5dSHugh Dickins 	if (vma->vm_flags & VM_WRITE)
27041ac0cb5dSHugh Dickins 		entry = pte_mkwrite(pte_mkdirty(entry));
27058f4e2101SHugh Dickins 
27068f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
27071c2fb7a4SAndrea Arcangeli 	if (!pte_none(*page_table))
27088f4e2101SHugh Dickins 		goto release;
27099ba69294SHugh Dickins 
271034e55232SKAMEZAWA Hiroyuki 	inc_mm_counter_fast(mm, MM_ANONPAGES);
27119617d95eSNick Piggin 	page_add_new_anon_rmap(page, vma, address);
271200501b53SJohannes Weiner 	mem_cgroup_commit_charge(page, memcg, false);
271300501b53SJohannes Weiner 	lru_cache_add_active_or_unevictable(page, vma);
2714a13ea5b7SHugh Dickins setpte:
271565500d23SHugh Dickins 	set_pte_at(mm, address, page_table, entry);
27161da177e4SLinus Torvalds 
27171da177e4SLinus Torvalds 	/* No need to invalidate - it was non-present before */
27184b3073e1SRussell King 	update_mmu_cache(vma, address, page_table);
271965500d23SHugh Dickins unlock:
27208f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
272183c54070SNick Piggin 	return 0;
27228f4e2101SHugh Dickins release:
272300501b53SJohannes Weiner 	mem_cgroup_cancel_charge(page, memcg);
27248f4e2101SHugh Dickins 	page_cache_release(page);
27258f4e2101SHugh Dickins 	goto unlock;
27268a9f3ccdSBalbir Singh oom_free_page:
27276dbf6d3bSHugh Dickins 	page_cache_release(page);
272865500d23SHugh Dickins oom:
27291da177e4SLinus Torvalds 	return VM_FAULT_OOM;
27301da177e4SLinus Torvalds }
27311da177e4SLinus Torvalds 
27329a95f3cfSPaul Cassella /*
27339a95f3cfSPaul Cassella  * The mmap_sem must have been held on entry, and may have been
27349a95f3cfSPaul Cassella  * released depending on flags and vma->vm_ops->fault() return value.
27359a95f3cfSPaul Cassella  * See filemap_fault() and __lock_page_retry().
27369a95f3cfSPaul Cassella  */
27377eae74afSKirill A. Shutemov static int __do_fault(struct vm_area_struct *vma, unsigned long address,
27382e4cdab0SMatthew Wilcox 			pgoff_t pgoff, unsigned int flags,
27392e4cdab0SMatthew Wilcox 			struct page *cow_page, struct page **page)
27407eae74afSKirill A. Shutemov {
27417eae74afSKirill A. Shutemov 	struct vm_fault vmf;
27427eae74afSKirill A. Shutemov 	int ret;
27437eae74afSKirill A. Shutemov 
27447eae74afSKirill A. Shutemov 	vmf.virtual_address = (void __user *)(address & PAGE_MASK);
27457eae74afSKirill A. Shutemov 	vmf.pgoff = pgoff;
27467eae74afSKirill A. Shutemov 	vmf.flags = flags;
27477eae74afSKirill A. Shutemov 	vmf.page = NULL;
27482e4cdab0SMatthew Wilcox 	vmf.cow_page = cow_page;
27497eae74afSKirill A. Shutemov 
27507eae74afSKirill A. Shutemov 	ret = vma->vm_ops->fault(vma, &vmf);
27517eae74afSKirill A. Shutemov 	if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
27527eae74afSKirill A. Shutemov 		return ret;
27532e4cdab0SMatthew Wilcox 	if (!vmf.page)
27542e4cdab0SMatthew Wilcox 		goto out;
27557eae74afSKirill A. Shutemov 
27567eae74afSKirill A. Shutemov 	if (unlikely(PageHWPoison(vmf.page))) {
27577eae74afSKirill A. Shutemov 		if (ret & VM_FAULT_LOCKED)
27587eae74afSKirill A. Shutemov 			unlock_page(vmf.page);
27597eae74afSKirill A. Shutemov 		page_cache_release(vmf.page);
27607eae74afSKirill A. Shutemov 		return VM_FAULT_HWPOISON;
27617eae74afSKirill A. Shutemov 	}
27627eae74afSKirill A. Shutemov 
27637eae74afSKirill A. Shutemov 	if (unlikely(!(ret & VM_FAULT_LOCKED)))
27647eae74afSKirill A. Shutemov 		lock_page(vmf.page);
27657eae74afSKirill A. Shutemov 	else
27667eae74afSKirill A. Shutemov 		VM_BUG_ON_PAGE(!PageLocked(vmf.page), vmf.page);
27677eae74afSKirill A. Shutemov 
27682e4cdab0SMatthew Wilcox  out:
27697eae74afSKirill A. Shutemov 	*page = vmf.page;
27707eae74afSKirill A. Shutemov 	return ret;
27717eae74afSKirill A. Shutemov }
27727eae74afSKirill A. Shutemov 
27738c6e50b0SKirill A. Shutemov /**
27748c6e50b0SKirill A. Shutemov  * do_set_pte - setup new PTE entry for given page and add reverse page mapping.
27758c6e50b0SKirill A. Shutemov  *
27768c6e50b0SKirill A. Shutemov  * @vma: virtual memory area
27778c6e50b0SKirill A. Shutemov  * @address: user virtual address
27788c6e50b0SKirill A. Shutemov  * @page: page to map
27798c6e50b0SKirill A. Shutemov  * @pte: pointer to target page table entry
27808c6e50b0SKirill A. Shutemov  * @write: true, if new entry is writable
27818c6e50b0SKirill A. Shutemov  * @anon: true, if it's anonymous page
27828c6e50b0SKirill A. Shutemov  *
27838c6e50b0SKirill A. Shutemov  * Caller must hold page table lock relevant for @pte.
27848c6e50b0SKirill A. Shutemov  *
27858c6e50b0SKirill A. Shutemov  * Target users are page handler itself and implementations of
27868c6e50b0SKirill A. Shutemov  * vm_ops->map_pages.
27878c6e50b0SKirill A. Shutemov  */
27888c6e50b0SKirill A. Shutemov void do_set_pte(struct vm_area_struct *vma, unsigned long address,
27893bb97794SKirill A. Shutemov 		struct page *page, pte_t *pte, bool write, bool anon)
27903bb97794SKirill A. Shutemov {
27913bb97794SKirill A. Shutemov 	pte_t entry;
27923bb97794SKirill A. Shutemov 
27933bb97794SKirill A. Shutemov 	flush_icache_page(vma, page);
27943bb97794SKirill A. Shutemov 	entry = mk_pte(page, vma->vm_page_prot);
27953bb97794SKirill A. Shutemov 	if (write)
27963bb97794SKirill A. Shutemov 		entry = maybe_mkwrite(pte_mkdirty(entry), vma);
27973bb97794SKirill A. Shutemov 	if (anon) {
27983bb97794SKirill A. Shutemov 		inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
27993bb97794SKirill A. Shutemov 		page_add_new_anon_rmap(page, vma, address);
28003bb97794SKirill A. Shutemov 	} else {
28013bb97794SKirill A. Shutemov 		inc_mm_counter_fast(vma->vm_mm, MM_FILEPAGES);
28023bb97794SKirill A. Shutemov 		page_add_file_rmap(page);
28033bb97794SKirill A. Shutemov 	}
28043bb97794SKirill A. Shutemov 	set_pte_at(vma->vm_mm, address, pte, entry);
28053bb97794SKirill A. Shutemov 
28063bb97794SKirill A. Shutemov 	/* no need to invalidate: a not-present page won't be cached */
28073bb97794SKirill A. Shutemov 	update_mmu_cache(vma, address, pte);
28083bb97794SKirill A. Shutemov }
28093bb97794SKirill A. Shutemov 
28103a91053aSKirill A. Shutemov static unsigned long fault_around_bytes __read_mostly =
28113a91053aSKirill A. Shutemov 	rounddown_pow_of_two(65536);
2812a9b0f861SKirill A. Shutemov 
28131592eef0SKirill A. Shutemov #ifdef CONFIG_DEBUG_FS
2814a9b0f861SKirill A. Shutemov static int fault_around_bytes_get(void *data, u64 *val)
28151592eef0SKirill A. Shutemov {
2816a9b0f861SKirill A. Shutemov 	*val = fault_around_bytes;
28171592eef0SKirill A. Shutemov 	return 0;
28181592eef0SKirill A. Shutemov }
28191592eef0SKirill A. Shutemov 
2820b4903d6eSAndrey Ryabinin /*
2821b4903d6eSAndrey Ryabinin  * fault_around_pages() and fault_around_mask() expects fault_around_bytes
2822b4903d6eSAndrey Ryabinin  * rounded down to nearest page order. It's what do_fault_around() expects to
2823b4903d6eSAndrey Ryabinin  * see.
2824b4903d6eSAndrey Ryabinin  */
2825a9b0f861SKirill A. Shutemov static int fault_around_bytes_set(void *data, u64 val)
28261592eef0SKirill A. Shutemov {
2827a9b0f861SKirill A. Shutemov 	if (val / PAGE_SIZE > PTRS_PER_PTE)
28281592eef0SKirill A. Shutemov 		return -EINVAL;
2829b4903d6eSAndrey Ryabinin 	if (val > PAGE_SIZE)
2830b4903d6eSAndrey Ryabinin 		fault_around_bytes = rounddown_pow_of_two(val);
2831b4903d6eSAndrey Ryabinin 	else
2832b4903d6eSAndrey Ryabinin 		fault_around_bytes = PAGE_SIZE; /* rounddown_pow_of_two(0) is undefined */
28331592eef0SKirill A. Shutemov 	return 0;
28341592eef0SKirill A. Shutemov }
2835a9b0f861SKirill A. Shutemov DEFINE_SIMPLE_ATTRIBUTE(fault_around_bytes_fops,
2836a9b0f861SKirill A. Shutemov 		fault_around_bytes_get, fault_around_bytes_set, "%llu\n");
28371592eef0SKirill A. Shutemov 
28381592eef0SKirill A. Shutemov static int __init fault_around_debugfs(void)
28391592eef0SKirill A. Shutemov {
28401592eef0SKirill A. Shutemov 	void *ret;
28411592eef0SKirill A. Shutemov 
2842a9b0f861SKirill A. Shutemov 	ret = debugfs_create_file("fault_around_bytes", 0644, NULL, NULL,
2843a9b0f861SKirill A. Shutemov 			&fault_around_bytes_fops);
28441592eef0SKirill A. Shutemov 	if (!ret)
2845a9b0f861SKirill A. Shutemov 		pr_warn("Failed to create fault_around_bytes in debugfs");
28461592eef0SKirill A. Shutemov 	return 0;
28471592eef0SKirill A. Shutemov }
28481592eef0SKirill A. Shutemov late_initcall(fault_around_debugfs);
28491592eef0SKirill A. Shutemov #endif
28508c6e50b0SKirill A. Shutemov 
28511fdb412bSKirill A. Shutemov /*
28521fdb412bSKirill A. Shutemov  * do_fault_around() tries to map few pages around the fault address. The hope
28531fdb412bSKirill A. Shutemov  * is that the pages will be needed soon and this will lower the number of
28541fdb412bSKirill A. Shutemov  * faults to handle.
28551fdb412bSKirill A. Shutemov  *
28561fdb412bSKirill A. Shutemov  * It uses vm_ops->map_pages() to map the pages, which skips the page if it's
28571fdb412bSKirill A. Shutemov  * not ready to be mapped: not up-to-date, locked, etc.
28581fdb412bSKirill A. Shutemov  *
28591fdb412bSKirill A. Shutemov  * This function is called with the page table lock taken. In the split ptlock
28601fdb412bSKirill A. Shutemov  * case the page table lock only protects only those entries which belong to
28611fdb412bSKirill A. Shutemov  * the page table corresponding to the fault address.
28621fdb412bSKirill A. Shutemov  *
28631fdb412bSKirill A. Shutemov  * This function doesn't cross the VMA boundaries, in order to call map_pages()
28641fdb412bSKirill A. Shutemov  * only once.
28651fdb412bSKirill A. Shutemov  *
28661fdb412bSKirill A. Shutemov  * fault_around_pages() defines how many pages we'll try to map.
28671fdb412bSKirill A. Shutemov  * do_fault_around() expects it to return a power of two less than or equal to
28681fdb412bSKirill A. Shutemov  * PTRS_PER_PTE.
28691fdb412bSKirill A. Shutemov  *
28701fdb412bSKirill A. Shutemov  * The virtual address of the area that we map is naturally aligned to the
28711fdb412bSKirill A. Shutemov  * fault_around_pages() value (and therefore to page order).  This way it's
28721fdb412bSKirill A. Shutemov  * easier to guarantee that we don't cross page table boundaries.
28731fdb412bSKirill A. Shutemov  */
28748c6e50b0SKirill A. Shutemov static void do_fault_around(struct vm_area_struct *vma, unsigned long address,
28758c6e50b0SKirill A. Shutemov 		pte_t *pte, pgoff_t pgoff, unsigned int flags)
28768c6e50b0SKirill A. Shutemov {
2877aecd6f44SKirill A. Shutemov 	unsigned long start_addr, nr_pages, mask;
28788c6e50b0SKirill A. Shutemov 	pgoff_t max_pgoff;
28798c6e50b0SKirill A. Shutemov 	struct vm_fault vmf;
28808c6e50b0SKirill A. Shutemov 	int off;
28818c6e50b0SKirill A. Shutemov 
28824db0c3c2SJason Low 	nr_pages = READ_ONCE(fault_around_bytes) >> PAGE_SHIFT;
2883aecd6f44SKirill A. Shutemov 	mask = ~(nr_pages * PAGE_SIZE - 1) & PAGE_MASK;
2884aecd6f44SKirill A. Shutemov 
2885aecd6f44SKirill A. Shutemov 	start_addr = max(address & mask, vma->vm_start);
28868c6e50b0SKirill A. Shutemov 	off = ((address - start_addr) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
28878c6e50b0SKirill A. Shutemov 	pte -= off;
28888c6e50b0SKirill A. Shutemov 	pgoff -= off;
28898c6e50b0SKirill A. Shutemov 
28908c6e50b0SKirill A. Shutemov 	/*
28918c6e50b0SKirill A. Shutemov 	 *  max_pgoff is either end of page table or end of vma
2892850e9c69SKirill A. Shutemov 	 *  or fault_around_pages() from pgoff, depending what is nearest.
28938c6e50b0SKirill A. Shutemov 	 */
28948c6e50b0SKirill A. Shutemov 	max_pgoff = pgoff - ((start_addr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) +
28958c6e50b0SKirill A. Shutemov 		PTRS_PER_PTE - 1;
28968c6e50b0SKirill A. Shutemov 	max_pgoff = min3(max_pgoff, vma_pages(vma) + vma->vm_pgoff - 1,
2897aecd6f44SKirill A. Shutemov 			pgoff + nr_pages - 1);
28988c6e50b0SKirill A. Shutemov 
28998c6e50b0SKirill A. Shutemov 	/* Check if it makes any sense to call ->map_pages */
29008c6e50b0SKirill A. Shutemov 	while (!pte_none(*pte)) {
29018c6e50b0SKirill A. Shutemov 		if (++pgoff > max_pgoff)
29028c6e50b0SKirill A. Shutemov 			return;
29038c6e50b0SKirill A. Shutemov 		start_addr += PAGE_SIZE;
29048c6e50b0SKirill A. Shutemov 		if (start_addr >= vma->vm_end)
29058c6e50b0SKirill A. Shutemov 			return;
29068c6e50b0SKirill A. Shutemov 		pte++;
29078c6e50b0SKirill A. Shutemov 	}
29088c6e50b0SKirill A. Shutemov 
29098c6e50b0SKirill A. Shutemov 	vmf.virtual_address = (void __user *) start_addr;
29108c6e50b0SKirill A. Shutemov 	vmf.pte = pte;
29118c6e50b0SKirill A. Shutemov 	vmf.pgoff = pgoff;
29128c6e50b0SKirill A. Shutemov 	vmf.max_pgoff = max_pgoff;
29138c6e50b0SKirill A. Shutemov 	vmf.flags = flags;
29148c6e50b0SKirill A. Shutemov 	vma->vm_ops->map_pages(vma, &vmf);
29158c6e50b0SKirill A. Shutemov }
29168c6e50b0SKirill A. Shutemov 
2917e655fb29SKirill A. Shutemov static int do_read_fault(struct mm_struct *mm, struct vm_area_struct *vma,
2918e655fb29SKirill A. Shutemov 		unsigned long address, pmd_t *pmd,
2919e655fb29SKirill A. Shutemov 		pgoff_t pgoff, unsigned int flags, pte_t orig_pte)
2920e655fb29SKirill A. Shutemov {
2921e655fb29SKirill A. Shutemov 	struct page *fault_page;
2922e655fb29SKirill A. Shutemov 	spinlock_t *ptl;
29233bb97794SKirill A. Shutemov 	pte_t *pte;
29248c6e50b0SKirill A. Shutemov 	int ret = 0;
29258c6e50b0SKirill A. Shutemov 
29268c6e50b0SKirill A. Shutemov 	/*
29278c6e50b0SKirill A. Shutemov 	 * Let's call ->map_pages() first and use ->fault() as fallback
29288c6e50b0SKirill A. Shutemov 	 * if page by the offset is not ready to be mapped (cold cache or
29298c6e50b0SKirill A. Shutemov 	 * something).
29308c6e50b0SKirill A. Shutemov 	 */
29319b4bdd2fSKirill A. Shutemov 	if (vma->vm_ops->map_pages && fault_around_bytes >> PAGE_SHIFT > 1) {
29328c6e50b0SKirill A. Shutemov 		pte = pte_offset_map_lock(mm, pmd, address, &ptl);
29338c6e50b0SKirill A. Shutemov 		do_fault_around(vma, address, pte, pgoff, flags);
29348c6e50b0SKirill A. Shutemov 		if (!pte_same(*pte, orig_pte))
29358c6e50b0SKirill A. Shutemov 			goto unlock_out;
29368c6e50b0SKirill A. Shutemov 		pte_unmap_unlock(pte, ptl);
29378c6e50b0SKirill A. Shutemov 	}
2938e655fb29SKirill A. Shutemov 
29392e4cdab0SMatthew Wilcox 	ret = __do_fault(vma, address, pgoff, flags, NULL, &fault_page);
2940e655fb29SKirill A. Shutemov 	if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
2941e655fb29SKirill A. Shutemov 		return ret;
2942e655fb29SKirill A. Shutemov 
2943e655fb29SKirill A. Shutemov 	pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2944e655fb29SKirill A. Shutemov 	if (unlikely(!pte_same(*pte, orig_pte))) {
2945e655fb29SKirill A. Shutemov 		pte_unmap_unlock(pte, ptl);
2946e655fb29SKirill A. Shutemov 		unlock_page(fault_page);
2947e655fb29SKirill A. Shutemov 		page_cache_release(fault_page);
2948e655fb29SKirill A. Shutemov 		return ret;
2949e655fb29SKirill A. Shutemov 	}
29503bb97794SKirill A. Shutemov 	do_set_pte(vma, address, fault_page, pte, false, false);
2951e655fb29SKirill A. Shutemov 	unlock_page(fault_page);
29528c6e50b0SKirill A. Shutemov unlock_out:
29538c6e50b0SKirill A. Shutemov 	pte_unmap_unlock(pte, ptl);
2954e655fb29SKirill A. Shutemov 	return ret;
2955e655fb29SKirill A. Shutemov }
2956e655fb29SKirill A. Shutemov 
2957ec47c3b9SKirill A. Shutemov static int do_cow_fault(struct mm_struct *mm, struct vm_area_struct *vma,
2958ec47c3b9SKirill A. Shutemov 		unsigned long address, pmd_t *pmd,
2959ec47c3b9SKirill A. Shutemov 		pgoff_t pgoff, unsigned int flags, pte_t orig_pte)
2960ec47c3b9SKirill A. Shutemov {
2961ec47c3b9SKirill A. Shutemov 	struct page *fault_page, *new_page;
296200501b53SJohannes Weiner 	struct mem_cgroup *memcg;
2963ec47c3b9SKirill A. Shutemov 	spinlock_t *ptl;
29643bb97794SKirill A. Shutemov 	pte_t *pte;
2965ec47c3b9SKirill A. Shutemov 	int ret;
2966ec47c3b9SKirill A. Shutemov 
2967ec47c3b9SKirill A. Shutemov 	if (unlikely(anon_vma_prepare(vma)))
2968ec47c3b9SKirill A. Shutemov 		return VM_FAULT_OOM;
2969ec47c3b9SKirill A. Shutemov 
2970ec47c3b9SKirill A. Shutemov 	new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
2971ec47c3b9SKirill A. Shutemov 	if (!new_page)
2972ec47c3b9SKirill A. Shutemov 		return VM_FAULT_OOM;
2973ec47c3b9SKirill A. Shutemov 
297400501b53SJohannes Weiner 	if (mem_cgroup_try_charge(new_page, mm, GFP_KERNEL, &memcg)) {
2975ec47c3b9SKirill A. Shutemov 		page_cache_release(new_page);
2976ec47c3b9SKirill A. Shutemov 		return VM_FAULT_OOM;
2977ec47c3b9SKirill A. Shutemov 	}
2978ec47c3b9SKirill A. Shutemov 
29792e4cdab0SMatthew Wilcox 	ret = __do_fault(vma, address, pgoff, flags, new_page, &fault_page);
2980ec47c3b9SKirill A. Shutemov 	if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
2981ec47c3b9SKirill A. Shutemov 		goto uncharge_out;
2982ec47c3b9SKirill A. Shutemov 
29832e4cdab0SMatthew Wilcox 	if (fault_page)
2984ec47c3b9SKirill A. Shutemov 		copy_user_highpage(new_page, fault_page, address, vma);
2985ec47c3b9SKirill A. Shutemov 	__SetPageUptodate(new_page);
2986ec47c3b9SKirill A. Shutemov 
2987ec47c3b9SKirill A. Shutemov 	pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2988ec47c3b9SKirill A. Shutemov 	if (unlikely(!pte_same(*pte, orig_pte))) {
2989ec47c3b9SKirill A. Shutemov 		pte_unmap_unlock(pte, ptl);
29902e4cdab0SMatthew Wilcox 		if (fault_page) {
2991ec47c3b9SKirill A. Shutemov 			unlock_page(fault_page);
2992ec47c3b9SKirill A. Shutemov 			page_cache_release(fault_page);
29932e4cdab0SMatthew Wilcox 		} else {
29942e4cdab0SMatthew Wilcox 			/*
29952e4cdab0SMatthew Wilcox 			 * The fault handler has no page to lock, so it holds
29962e4cdab0SMatthew Wilcox 			 * i_mmap_lock for read to protect against truncate.
29972e4cdab0SMatthew Wilcox 			 */
29982e4cdab0SMatthew Wilcox 			i_mmap_unlock_read(vma->vm_file->f_mapping);
29992e4cdab0SMatthew Wilcox 		}
3000ec47c3b9SKirill A. Shutemov 		goto uncharge_out;
3001ec47c3b9SKirill A. Shutemov 	}
30023bb97794SKirill A. Shutemov 	do_set_pte(vma, address, new_page, pte, true, true);
300300501b53SJohannes Weiner 	mem_cgroup_commit_charge(new_page, memcg, false);
300400501b53SJohannes Weiner 	lru_cache_add_active_or_unevictable(new_page, vma);
3005ec47c3b9SKirill A. Shutemov 	pte_unmap_unlock(pte, ptl);
30062e4cdab0SMatthew Wilcox 	if (fault_page) {
3007ec47c3b9SKirill A. Shutemov 		unlock_page(fault_page);
3008ec47c3b9SKirill A. Shutemov 		page_cache_release(fault_page);
30092e4cdab0SMatthew Wilcox 	} else {
30102e4cdab0SMatthew Wilcox 		/*
30112e4cdab0SMatthew Wilcox 		 * The fault handler has no page to lock, so it holds
30122e4cdab0SMatthew Wilcox 		 * i_mmap_lock for read to protect against truncate.
30132e4cdab0SMatthew Wilcox 		 */
30142e4cdab0SMatthew Wilcox 		i_mmap_unlock_read(vma->vm_file->f_mapping);
30152e4cdab0SMatthew Wilcox 	}
3016ec47c3b9SKirill A. Shutemov 	return ret;
3017ec47c3b9SKirill A. Shutemov uncharge_out:
301800501b53SJohannes Weiner 	mem_cgroup_cancel_charge(new_page, memcg);
3019ec47c3b9SKirill A. Shutemov 	page_cache_release(new_page);
3020ec47c3b9SKirill A. Shutemov 	return ret;
3021ec47c3b9SKirill A. Shutemov }
3022ec47c3b9SKirill A. Shutemov 
3023f0c6d4d2SKirill A. Shutemov static int do_shared_fault(struct mm_struct *mm, struct vm_area_struct *vma,
302416abfa08SHugh Dickins 		unsigned long address, pmd_t *pmd,
302554cb8821SNick Piggin 		pgoff_t pgoff, unsigned int flags, pte_t orig_pte)
30261da177e4SLinus Torvalds {
3027f0c6d4d2SKirill A. Shutemov 	struct page *fault_page;
3028f0c6d4d2SKirill A. Shutemov 	struct address_space *mapping;
30298f4e2101SHugh Dickins 	spinlock_t *ptl;
30303bb97794SKirill A. Shutemov 	pte_t *pte;
3031f0c6d4d2SKirill A. Shutemov 	int dirtied = 0;
3032f0c6d4d2SKirill A. Shutemov 	int ret, tmp;
30331d65f86dSKAMEZAWA Hiroyuki 
30342e4cdab0SMatthew Wilcox 	ret = __do_fault(vma, address, pgoff, flags, NULL, &fault_page);
30357eae74afSKirill A. Shutemov 	if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
3036f0c6d4d2SKirill A. Shutemov 		return ret;
30371da177e4SLinus Torvalds 
30381da177e4SLinus Torvalds 	/*
3039f0c6d4d2SKirill A. Shutemov 	 * Check if the backing address space wants to know that the page is
3040f0c6d4d2SKirill A. Shutemov 	 * about to become writable
30411da177e4SLinus Torvalds 	 */
3042fb09a464SKirill A. Shutemov 	if (vma->vm_ops->page_mkwrite) {
3043f0c6d4d2SKirill A. Shutemov 		unlock_page(fault_page);
3044fb09a464SKirill A. Shutemov 		tmp = do_page_mkwrite(vma, fault_page, address);
3045fb09a464SKirill A. Shutemov 		if (unlikely(!tmp ||
3046fb09a464SKirill A. Shutemov 				(tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
3047f0c6d4d2SKirill A. Shutemov 			page_cache_release(fault_page);
3048f0c6d4d2SKirill A. Shutemov 			return tmp;
304969676147SMark Fasheh 		}
3050d0217ac0SNick Piggin 	}
3051fb09a464SKirill A. Shutemov 
3052f0c6d4d2SKirill A. Shutemov 	pte = pte_offset_map_lock(mm, pmd, address, &ptl);
3053f0c6d4d2SKirill A. Shutemov 	if (unlikely(!pte_same(*pte, orig_pte))) {
3054f0c6d4d2SKirill A. Shutemov 		pte_unmap_unlock(pte, ptl);
3055f0c6d4d2SKirill A. Shutemov 		unlock_page(fault_page);
3056f0c6d4d2SKirill A. Shutemov 		page_cache_release(fault_page);
3057f0c6d4d2SKirill A. Shutemov 		return ret;
30589637a5efSDavid Howells 	}
30593bb97794SKirill A. Shutemov 	do_set_pte(vma, address, fault_page, pte, true, false);
3060f0c6d4d2SKirill A. Shutemov 	pte_unmap_unlock(pte, ptl);
3061d00806b1SNick Piggin 
3062f0c6d4d2SKirill A. Shutemov 	if (set_page_dirty(fault_page))
306341c4d25fSJan Kara 		dirtied = 1;
3064d82fa87dSAndrew Morton 	/*
3065d82fa87dSAndrew Morton 	 * Take a local copy of the address_space - page.mapping may be zeroed
3066d82fa87dSAndrew Morton 	 * by truncate after unlock_page().   The address_space itself remains
3067d82fa87dSAndrew Morton 	 * pinned by vma->vm_file's reference.  We rely on unlock_page()'s
3068d82fa87dSAndrew Morton 	 * release semantics to prevent the compiler from undoing this copying.
3069d82fa87dSAndrew Morton 	 */
3070f0c6d4d2SKirill A. Shutemov 	mapping = fault_page->mapping;
3071f0c6d4d2SKirill A. Shutemov 	unlock_page(fault_page);
3072f0c6d4d2SKirill A. Shutemov 	if ((dirtied || vma->vm_ops->page_mkwrite) && mapping) {
3073b827e496SNick Piggin 		/*
3074b827e496SNick Piggin 		 * Some device drivers do not set page.mapping but still
3075b827e496SNick Piggin 		 * dirty their pages
3076b827e496SNick Piggin 		 */
3077b827e496SNick Piggin 		balance_dirty_pages_ratelimited(mapping);
3078d08b3851SPeter Zijlstra 	}
3079d00806b1SNick Piggin 
308074ec6751SJohannes Weiner 	if (!vma->vm_ops->page_mkwrite)
3081b827e496SNick Piggin 		file_update_time(vma->vm_file);
3082b827e496SNick Piggin 
3083b827e496SNick Piggin 	return ret;
308454cb8821SNick Piggin }
3085d00806b1SNick Piggin 
30869a95f3cfSPaul Cassella /*
30879a95f3cfSPaul Cassella  * We enter with non-exclusive mmap_sem (to exclude vma changes,
30889a95f3cfSPaul Cassella  * but allow concurrent faults).
30899a95f3cfSPaul Cassella  * The mmap_sem may have been released depending on flags and our
30909a95f3cfSPaul Cassella  * return value.  See filemap_fault() and __lock_page_or_retry().
30919a95f3cfSPaul Cassella  */
30929b4bdd2fSKirill A. Shutemov static int do_fault(struct mm_struct *mm, struct vm_area_struct *vma,
309354cb8821SNick Piggin 		unsigned long address, pte_t *page_table, pmd_t *pmd,
309430c9f3a9SLinus Torvalds 		unsigned int flags, pte_t orig_pte)
309554cb8821SNick Piggin {
309654cb8821SNick Piggin 	pgoff_t pgoff = (((address & PAGE_MASK)
30970da7e01fSDean Nelson 			- vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
309854cb8821SNick Piggin 
309916abfa08SHugh Dickins 	pte_unmap(page_table);
3100e655fb29SKirill A. Shutemov 	if (!(flags & FAULT_FLAG_WRITE))
3101e655fb29SKirill A. Shutemov 		return do_read_fault(mm, vma, address, pmd, pgoff, flags,
3102e655fb29SKirill A. Shutemov 				orig_pte);
3103ec47c3b9SKirill A. Shutemov 	if (!(vma->vm_flags & VM_SHARED))
3104ec47c3b9SKirill A. Shutemov 		return do_cow_fault(mm, vma, address, pmd, pgoff, flags,
3105ec47c3b9SKirill A. Shutemov 				orig_pte);
3106f0c6d4d2SKirill A. Shutemov 	return do_shared_fault(mm, vma, address, pmd, pgoff, flags, orig_pte);
310754cb8821SNick Piggin }
310854cb8821SNick Piggin 
3109b19a9939SRashika Kheria static int numa_migrate_prep(struct page *page, struct vm_area_struct *vma,
311004bb2f94SRik van Riel 				unsigned long addr, int page_nid,
311104bb2f94SRik van Riel 				int *flags)
31129532fec1SMel Gorman {
31139532fec1SMel Gorman 	get_page(page);
31149532fec1SMel Gorman 
31159532fec1SMel Gorman 	count_vm_numa_event(NUMA_HINT_FAULTS);
311604bb2f94SRik van Riel 	if (page_nid == numa_node_id()) {
31179532fec1SMel Gorman 		count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
311804bb2f94SRik van Riel 		*flags |= TNF_FAULT_LOCAL;
311904bb2f94SRik van Riel 	}
31209532fec1SMel Gorman 
31219532fec1SMel Gorman 	return mpol_misplaced(page, vma, addr);
31229532fec1SMel Gorman }
31239532fec1SMel Gorman 
3124b19a9939SRashika Kheria static int do_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
3125d10e63f2SMel Gorman 		   unsigned long addr, pte_t pte, pte_t *ptep, pmd_t *pmd)
3126d10e63f2SMel Gorman {
31274daae3b4SMel Gorman 	struct page *page = NULL;
3128d10e63f2SMel Gorman 	spinlock_t *ptl;
31298191acbdSMel Gorman 	int page_nid = -1;
313090572890SPeter Zijlstra 	int last_cpupid;
3131cbee9f88SPeter Zijlstra 	int target_nid;
3132b8593bfdSMel Gorman 	bool migrated = false;
3133b191f9b1SMel Gorman 	bool was_writable = pte_write(pte);
31346688cc05SPeter Zijlstra 	int flags = 0;
3135d10e63f2SMel Gorman 
3136c0e7cad9SMel Gorman 	/* A PROT_NONE fault should not end up here */
3137c0e7cad9SMel Gorman 	BUG_ON(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)));
3138c0e7cad9SMel Gorman 
3139d10e63f2SMel Gorman 	/*
3140d10e63f2SMel Gorman 	* The "pte" at this point cannot be used safely without
3141d10e63f2SMel Gorman 	* validation through pte_unmap_same(). It's of NUMA type but
3142d10e63f2SMel Gorman 	* the pfn may be screwed if the read is non atomic.
3143d10e63f2SMel Gorman 	*
31444d942466SMel Gorman 	* We can safely just do a "set_pte_at()", because the old
31454d942466SMel Gorman 	* page table entry is not accessible, so there would be no
31464d942466SMel Gorman 	* concurrent hardware modifications to the PTE.
3147d10e63f2SMel Gorman 	*/
3148d10e63f2SMel Gorman 	ptl = pte_lockptr(mm, pmd);
3149d10e63f2SMel Gorman 	spin_lock(ptl);
31504daae3b4SMel Gorman 	if (unlikely(!pte_same(*ptep, pte))) {
31514daae3b4SMel Gorman 		pte_unmap_unlock(ptep, ptl);
31524daae3b4SMel Gorman 		goto out;
31534daae3b4SMel Gorman 	}
31544daae3b4SMel Gorman 
31554d942466SMel Gorman 	/* Make it present again */
31564d942466SMel Gorman 	pte = pte_modify(pte, vma->vm_page_prot);
31574d942466SMel Gorman 	pte = pte_mkyoung(pte);
3158b191f9b1SMel Gorman 	if (was_writable)
3159b191f9b1SMel Gorman 		pte = pte_mkwrite(pte);
3160d10e63f2SMel Gorman 	set_pte_at(mm, addr, ptep, pte);
3161d10e63f2SMel Gorman 	update_mmu_cache(vma, addr, ptep);
3162d10e63f2SMel Gorman 
3163d10e63f2SMel Gorman 	page = vm_normal_page(vma, addr, pte);
3164d10e63f2SMel Gorman 	if (!page) {
3165d10e63f2SMel Gorman 		pte_unmap_unlock(ptep, ptl);
3166d10e63f2SMel Gorman 		return 0;
3167d10e63f2SMel Gorman 	}
3168d10e63f2SMel Gorman 
31696688cc05SPeter Zijlstra 	/*
3170bea66fbdSMel Gorman 	 * Avoid grouping on RO pages in general. RO pages shouldn't hurt as
3171bea66fbdSMel Gorman 	 * much anyway since they can be in shared cache state. This misses
3172bea66fbdSMel Gorman 	 * the case where a mapping is writable but the process never writes
3173bea66fbdSMel Gorman 	 * to it but pte_write gets cleared during protection updates and
3174bea66fbdSMel Gorman 	 * pte_dirty has unpredictable behaviour between PTE scan updates,
3175bea66fbdSMel Gorman 	 * background writeback, dirty balancing and application behaviour.
31766688cc05SPeter Zijlstra 	 */
3177bea66fbdSMel Gorman 	if (!(vma->vm_flags & VM_WRITE))
31786688cc05SPeter Zijlstra 		flags |= TNF_NO_GROUP;
31796688cc05SPeter Zijlstra 
3180dabe1d99SRik van Riel 	/*
3181dabe1d99SRik van Riel 	 * Flag if the page is shared between multiple address spaces. This
3182dabe1d99SRik van Riel 	 * is later used when determining whether to group tasks together
3183dabe1d99SRik van Riel 	 */
3184dabe1d99SRik van Riel 	if (page_mapcount(page) > 1 && (vma->vm_flags & VM_SHARED))
3185dabe1d99SRik van Riel 		flags |= TNF_SHARED;
3186dabe1d99SRik van Riel 
318790572890SPeter Zijlstra 	last_cpupid = page_cpupid_last(page);
31888191acbdSMel Gorman 	page_nid = page_to_nid(page);
318904bb2f94SRik van Riel 	target_nid = numa_migrate_prep(page, vma, addr, page_nid, &flags);
3190d10e63f2SMel Gorman 	pte_unmap_unlock(ptep, ptl);
31914daae3b4SMel Gorman 	if (target_nid == -1) {
31924daae3b4SMel Gorman 		put_page(page);
31934daae3b4SMel Gorman 		goto out;
31944daae3b4SMel Gorman 	}
31954daae3b4SMel Gorman 
31964daae3b4SMel Gorman 	/* Migrate to the requested node */
31971bc115d8SMel Gorman 	migrated = migrate_misplaced_page(page, vma, target_nid);
31986688cc05SPeter Zijlstra 	if (migrated) {
31998191acbdSMel Gorman 		page_nid = target_nid;
32006688cc05SPeter Zijlstra 		flags |= TNF_MIGRATED;
3201074c2381SMel Gorman 	} else
3202074c2381SMel Gorman 		flags |= TNF_MIGRATE_FAIL;
32034daae3b4SMel Gorman 
32044daae3b4SMel Gorman out:
32058191acbdSMel Gorman 	if (page_nid != -1)
32066688cc05SPeter Zijlstra 		task_numa_fault(last_cpupid, page_nid, 1, flags);
3207d10e63f2SMel Gorman 	return 0;
3208d10e63f2SMel Gorman }
3209d10e63f2SMel Gorman 
32101da177e4SLinus Torvalds /*
32111da177e4SLinus Torvalds  * These routines also need to handle stuff like marking pages dirty
32121da177e4SLinus Torvalds  * and/or accessed for architectures that don't do it in hardware (most
32131da177e4SLinus Torvalds  * RISC architectures).  The early dirtying is also good on the i386.
32141da177e4SLinus Torvalds  *
32151da177e4SLinus Torvalds  * There is also a hook called "update_mmu_cache()" that architectures
32161da177e4SLinus Torvalds  * with external mmu caches can use to update those (ie the Sparc or
32171da177e4SLinus Torvalds  * PowerPC hashed page tables that act as extended TLBs).
32181da177e4SLinus Torvalds  *
3219c74df32cSHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
3220c74df32cSHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
32219a95f3cfSPaul Cassella  * We return with pte unmapped and unlocked.
32229a95f3cfSPaul Cassella  *
32239a95f3cfSPaul Cassella  * The mmap_sem may have been released depending on flags and our
32249a95f3cfSPaul Cassella  * return value.  See filemap_fault() and __lock_page_or_retry().
32251da177e4SLinus Torvalds  */
3226c0292554SKirill A. Shutemov static int handle_pte_fault(struct mm_struct *mm,
32271da177e4SLinus Torvalds 		     struct vm_area_struct *vma, unsigned long address,
322830c9f3a9SLinus Torvalds 		     pte_t *pte, pmd_t *pmd, unsigned int flags)
32291da177e4SLinus Torvalds {
32301da177e4SLinus Torvalds 	pte_t entry;
32318f4e2101SHugh Dickins 	spinlock_t *ptl;
32321da177e4SLinus Torvalds 
3233e37c6982SChristian Borntraeger 	/*
3234e37c6982SChristian Borntraeger 	 * some architectures can have larger ptes than wordsize,
3235e37c6982SChristian Borntraeger 	 * e.g.ppc44x-defconfig has CONFIG_PTE_64BIT=y and CONFIG_32BIT=y,
3236e37c6982SChristian Borntraeger 	 * so READ_ONCE or ACCESS_ONCE cannot guarantee atomic accesses.
3237e37c6982SChristian Borntraeger 	 * The code below just needs a consistent view for the ifs and
3238e37c6982SChristian Borntraeger 	 * we later double check anyway with the ptl lock held. So here
3239e37c6982SChristian Borntraeger 	 * a barrier will do.
3240e37c6982SChristian Borntraeger 	 */
3241e37c6982SChristian Borntraeger 	entry = *pte;
3242e37c6982SChristian Borntraeger 	barrier();
32431da177e4SLinus Torvalds 	if (!pte_present(entry)) {
324465500d23SHugh Dickins 		if (pte_none(entry)) {
3245f4b81804SJes Sorensen 			if (vma->vm_ops) {
32463c18ddd1SNick Piggin 				if (likely(vma->vm_ops->fault))
32479b4bdd2fSKirill A. Shutemov 					return do_fault(mm, vma, address, pte,
32489b4bdd2fSKirill A. Shutemov 							pmd, flags, entry);
3249f4b81804SJes Sorensen 			}
3250f4b81804SJes Sorensen 			return do_anonymous_page(mm, vma, address,
325130c9f3a9SLinus Torvalds 						 pte, pmd, flags);
325265500d23SHugh Dickins 		}
325365500d23SHugh Dickins 		return do_swap_page(mm, vma, address,
325430c9f3a9SLinus Torvalds 					pte, pmd, flags, entry);
32551da177e4SLinus Torvalds 	}
32561da177e4SLinus Torvalds 
32578a0516edSMel Gorman 	if (pte_protnone(entry))
3258d10e63f2SMel Gorman 		return do_numa_page(mm, vma, address, entry, pte, pmd);
3259d10e63f2SMel Gorman 
32604c21e2f2SHugh Dickins 	ptl = pte_lockptr(mm, pmd);
32618f4e2101SHugh Dickins 	spin_lock(ptl);
32628f4e2101SHugh Dickins 	if (unlikely(!pte_same(*pte, entry)))
32638f4e2101SHugh Dickins 		goto unlock;
326430c9f3a9SLinus Torvalds 	if (flags & FAULT_FLAG_WRITE) {
32651da177e4SLinus Torvalds 		if (!pte_write(entry))
32668f4e2101SHugh Dickins 			return do_wp_page(mm, vma, address,
32678f4e2101SHugh Dickins 					pte, pmd, ptl, entry);
32681da177e4SLinus Torvalds 		entry = pte_mkdirty(entry);
32691da177e4SLinus Torvalds 	}
32701da177e4SLinus Torvalds 	entry = pte_mkyoung(entry);
327130c9f3a9SLinus Torvalds 	if (ptep_set_access_flags(vma, address, pte, entry, flags & FAULT_FLAG_WRITE)) {
32724b3073e1SRussell King 		update_mmu_cache(vma, address, pte);
32731a44e149SAndrea Arcangeli 	} else {
32741a44e149SAndrea Arcangeli 		/*
32751a44e149SAndrea Arcangeli 		 * This is needed only for protection faults but the arch code
32761a44e149SAndrea Arcangeli 		 * is not yet telling us if this is a protection fault or not.
32771a44e149SAndrea Arcangeli 		 * This still avoids useless tlb flushes for .text page faults
32781a44e149SAndrea Arcangeli 		 * with threads.
32791a44e149SAndrea Arcangeli 		 */
328030c9f3a9SLinus Torvalds 		if (flags & FAULT_FLAG_WRITE)
328161c77326SShaohua Li 			flush_tlb_fix_spurious_fault(vma, address);
32821a44e149SAndrea Arcangeli 	}
32838f4e2101SHugh Dickins unlock:
32848f4e2101SHugh Dickins 	pte_unmap_unlock(pte, ptl);
328583c54070SNick Piggin 	return 0;
32861da177e4SLinus Torvalds }
32871da177e4SLinus Torvalds 
32881da177e4SLinus Torvalds /*
32891da177e4SLinus Torvalds  * By the time we get here, we already hold the mm semaphore
32909a95f3cfSPaul Cassella  *
32919a95f3cfSPaul Cassella  * The mmap_sem may have been released depending on flags and our
32929a95f3cfSPaul Cassella  * return value.  See filemap_fault() and __lock_page_or_retry().
32931da177e4SLinus Torvalds  */
3294519e5247SJohannes Weiner static int __handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
3295d06063ccSLinus Torvalds 			     unsigned long address, unsigned int flags)
32961da177e4SLinus Torvalds {
32971da177e4SLinus Torvalds 	pgd_t *pgd;
32981da177e4SLinus Torvalds 	pud_t *pud;
32991da177e4SLinus Torvalds 	pmd_t *pmd;
33001da177e4SLinus Torvalds 	pte_t *pte;
33011da177e4SLinus Torvalds 
3302ac9b9c66SHugh Dickins 	if (unlikely(is_vm_hugetlb_page(vma)))
330330c9f3a9SLinus Torvalds 		return hugetlb_fault(mm, vma, address, flags);
33041da177e4SLinus Torvalds 
33051da177e4SLinus Torvalds 	pgd = pgd_offset(mm, address);
33061da177e4SLinus Torvalds 	pud = pud_alloc(mm, pgd, address);
33071da177e4SLinus Torvalds 	if (!pud)
3308c74df32cSHugh Dickins 		return VM_FAULT_OOM;
33091da177e4SLinus Torvalds 	pmd = pmd_alloc(mm, pud, address);
33101da177e4SLinus Torvalds 	if (!pmd)
3311c74df32cSHugh Dickins 		return VM_FAULT_OOM;
331271e3aac0SAndrea Arcangeli 	if (pmd_none(*pmd) && transparent_hugepage_enabled(vma)) {
3313c0292554SKirill A. Shutemov 		int ret = VM_FAULT_FALLBACK;
331471e3aac0SAndrea Arcangeli 		if (!vma->vm_ops)
3315c0292554SKirill A. Shutemov 			ret = do_huge_pmd_anonymous_page(mm, vma, address,
331671e3aac0SAndrea Arcangeli 					pmd, flags);
3317c0292554SKirill A. Shutemov 		if (!(ret & VM_FAULT_FALLBACK))
3318c0292554SKirill A. Shutemov 			return ret;
331971e3aac0SAndrea Arcangeli 	} else {
332071e3aac0SAndrea Arcangeli 		pmd_t orig_pmd = *pmd;
33211f1d06c3SDavid Rientjes 		int ret;
33221f1d06c3SDavid Rientjes 
332371e3aac0SAndrea Arcangeli 		barrier();
332471e3aac0SAndrea Arcangeli 		if (pmd_trans_huge(orig_pmd)) {
3325a1dd450bSWill Deacon 			unsigned int dirty = flags & FAULT_FLAG_WRITE;
3326a1dd450bSWill Deacon 
3327e53289c0SLinus Torvalds 			/*
3328e53289c0SLinus Torvalds 			 * If the pmd is splitting, return and retry the
3329e53289c0SLinus Torvalds 			 * the fault.  Alternative: wait until the split
3330e53289c0SLinus Torvalds 			 * is done, and goto retry.
3331e53289c0SLinus Torvalds 			 */
3332e53289c0SLinus Torvalds 			if (pmd_trans_splitting(orig_pmd))
3333e53289c0SLinus Torvalds 				return 0;
3334e53289c0SLinus Torvalds 
33358a0516edSMel Gorman 			if (pmd_protnone(orig_pmd))
33364daae3b4SMel Gorman 				return do_huge_pmd_numa_page(mm, vma, address,
3337d10e63f2SMel Gorman 							     orig_pmd, pmd);
3338d10e63f2SMel Gorman 
33393d59eebcSLinus Torvalds 			if (dirty && !pmd_write(orig_pmd)) {
33401f1d06c3SDavid Rientjes 				ret = do_huge_pmd_wp_page(mm, vma, address, pmd,
33411f1d06c3SDavid Rientjes 							  orig_pmd);
33429845cbbdSKirill A. Shutemov 				if (!(ret & VM_FAULT_FALLBACK))
33431f1d06c3SDavid Rientjes 					return ret;
3344a1dd450bSWill Deacon 			} else {
3345a1dd450bSWill Deacon 				huge_pmd_set_accessed(mm, vma, address, pmd,
3346a1dd450bSWill Deacon 						      orig_pmd, dirty);
334771e3aac0SAndrea Arcangeli 				return 0;
334871e3aac0SAndrea Arcangeli 			}
334971e3aac0SAndrea Arcangeli 		}
33509845cbbdSKirill A. Shutemov 	}
335171e3aac0SAndrea Arcangeli 
335271e3aac0SAndrea Arcangeli 	/*
335371e3aac0SAndrea Arcangeli 	 * Use __pte_alloc instead of pte_alloc_map, because we can't
335471e3aac0SAndrea Arcangeli 	 * run pte_offset_map on the pmd, if an huge pmd could
335571e3aac0SAndrea Arcangeli 	 * materialize from under us from a different thread.
335671e3aac0SAndrea Arcangeli 	 */
33574fd01770SMel Gorman 	if (unlikely(pmd_none(*pmd)) &&
33584fd01770SMel Gorman 	    unlikely(__pte_alloc(mm, vma, pmd, address)))
3359c74df32cSHugh Dickins 		return VM_FAULT_OOM;
336071e3aac0SAndrea Arcangeli 	/* if an huge pmd materialized from under us just retry later */
336171e3aac0SAndrea Arcangeli 	if (unlikely(pmd_trans_huge(*pmd)))
336271e3aac0SAndrea Arcangeli 		return 0;
336371e3aac0SAndrea Arcangeli 	/*
336471e3aac0SAndrea Arcangeli 	 * A regular pmd is established and it can't morph into a huge pmd
336571e3aac0SAndrea Arcangeli 	 * from under us anymore at this point because we hold the mmap_sem
336671e3aac0SAndrea Arcangeli 	 * read mode and khugepaged takes it in write mode. So now it's
336771e3aac0SAndrea Arcangeli 	 * safe to run pte_offset_map().
336871e3aac0SAndrea Arcangeli 	 */
336971e3aac0SAndrea Arcangeli 	pte = pte_offset_map(pmd, address);
33701da177e4SLinus Torvalds 
337130c9f3a9SLinus Torvalds 	return handle_pte_fault(mm, vma, address, pte, pmd, flags);
33721da177e4SLinus Torvalds }
33731da177e4SLinus Torvalds 
33749a95f3cfSPaul Cassella /*
33759a95f3cfSPaul Cassella  * By the time we get here, we already hold the mm semaphore
33769a95f3cfSPaul Cassella  *
33779a95f3cfSPaul Cassella  * The mmap_sem may have been released depending on flags and our
33789a95f3cfSPaul Cassella  * return value.  See filemap_fault() and __lock_page_or_retry().
33799a95f3cfSPaul Cassella  */
3380519e5247SJohannes Weiner int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
3381519e5247SJohannes Weiner 		    unsigned long address, unsigned int flags)
3382519e5247SJohannes Weiner {
3383519e5247SJohannes Weiner 	int ret;
3384519e5247SJohannes Weiner 
3385519e5247SJohannes Weiner 	__set_current_state(TASK_RUNNING);
3386519e5247SJohannes Weiner 
3387519e5247SJohannes Weiner 	count_vm_event(PGFAULT);
3388519e5247SJohannes Weiner 	mem_cgroup_count_vm_event(mm, PGFAULT);
3389519e5247SJohannes Weiner 
3390519e5247SJohannes Weiner 	/* do counter updates before entering really critical section. */
3391519e5247SJohannes Weiner 	check_sync_rss_stat(current);
3392519e5247SJohannes Weiner 
3393519e5247SJohannes Weiner 	/*
3394519e5247SJohannes Weiner 	 * Enable the memcg OOM handling for faults triggered in user
3395519e5247SJohannes Weiner 	 * space.  Kernel faults are handled more gracefully.
3396519e5247SJohannes Weiner 	 */
3397519e5247SJohannes Weiner 	if (flags & FAULT_FLAG_USER)
339849426420SJohannes Weiner 		mem_cgroup_oom_enable();
3399519e5247SJohannes Weiner 
3400519e5247SJohannes Weiner 	ret = __handle_mm_fault(mm, vma, address, flags);
3401519e5247SJohannes Weiner 
340249426420SJohannes Weiner 	if (flags & FAULT_FLAG_USER) {
340349426420SJohannes Weiner 		mem_cgroup_oom_disable();
340449426420SJohannes Weiner                 /*
340549426420SJohannes Weiner                  * The task may have entered a memcg OOM situation but
340649426420SJohannes Weiner                  * if the allocation error was handled gracefully (no
340749426420SJohannes Weiner                  * VM_FAULT_OOM), there is no need to kill anything.
340849426420SJohannes Weiner                  * Just clean up the OOM state peacefully.
340949426420SJohannes Weiner                  */
341049426420SJohannes Weiner                 if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM))
341149426420SJohannes Weiner                         mem_cgroup_oom_synchronize(false);
341249426420SJohannes Weiner 	}
34133812c8c8SJohannes Weiner 
3414519e5247SJohannes Weiner 	return ret;
3415519e5247SJohannes Weiner }
3416e1d6d01aSJesse Barnes EXPORT_SYMBOL_GPL(handle_mm_fault);
3417519e5247SJohannes Weiner 
34181da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED
34191da177e4SLinus Torvalds /*
34201da177e4SLinus Torvalds  * Allocate page upper directory.
3421872fec16SHugh Dickins  * We've already handled the fast-path in-line.
34221da177e4SLinus Torvalds  */
34231bb3630eSHugh Dickins int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
34241da177e4SLinus Torvalds {
3425c74df32cSHugh Dickins 	pud_t *new = pud_alloc_one(mm, address);
3426c74df32cSHugh Dickins 	if (!new)
34271bb3630eSHugh Dickins 		return -ENOMEM;
34281da177e4SLinus Torvalds 
3429362a61adSNick Piggin 	smp_wmb(); /* See comment in __pte_alloc */
3430362a61adSNick Piggin 
3431872fec16SHugh Dickins 	spin_lock(&mm->page_table_lock);
34321bb3630eSHugh Dickins 	if (pgd_present(*pgd))		/* Another has populated it */
34335e541973SBenjamin Herrenschmidt 		pud_free(mm, new);
34341bb3630eSHugh Dickins 	else
34351da177e4SLinus Torvalds 		pgd_populate(mm, pgd, new);
3436872fec16SHugh Dickins 	spin_unlock(&mm->page_table_lock);
34371bb3630eSHugh Dickins 	return 0;
34381da177e4SLinus Torvalds }
34391da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */
34401da177e4SLinus Torvalds 
34411da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED
34421da177e4SLinus Torvalds /*
34431da177e4SLinus Torvalds  * Allocate page middle directory.
3444872fec16SHugh Dickins  * We've already handled the fast-path in-line.
34451da177e4SLinus Torvalds  */
34461bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
34471da177e4SLinus Torvalds {
3448c74df32cSHugh Dickins 	pmd_t *new = pmd_alloc_one(mm, address);
3449c74df32cSHugh Dickins 	if (!new)
34501bb3630eSHugh Dickins 		return -ENOMEM;
34511da177e4SLinus Torvalds 
3452362a61adSNick Piggin 	smp_wmb(); /* See comment in __pte_alloc */
3453362a61adSNick Piggin 
3454872fec16SHugh Dickins 	spin_lock(&mm->page_table_lock);
34551da177e4SLinus Torvalds #ifndef __ARCH_HAS_4LEVEL_HACK
3456dc6c9a35SKirill A. Shutemov 	if (!pud_present(*pud)) {
3457dc6c9a35SKirill A. Shutemov 		mm_inc_nr_pmds(mm);
34581da177e4SLinus Torvalds 		pud_populate(mm, pud, new);
3459dc6c9a35SKirill A. Shutemov 	} else	/* Another has populated it */
34605e541973SBenjamin Herrenschmidt 		pmd_free(mm, new);
3461dc6c9a35SKirill A. Shutemov #else
3462dc6c9a35SKirill A. Shutemov 	if (!pgd_present(*pud)) {
3463dc6c9a35SKirill A. Shutemov 		mm_inc_nr_pmds(mm);
34641da177e4SLinus Torvalds 		pgd_populate(mm, pud, new);
3465dc6c9a35SKirill A. Shutemov 	} else /* Another has populated it */
3466dc6c9a35SKirill A. Shutemov 		pmd_free(mm, new);
34671da177e4SLinus Torvalds #endif /* __ARCH_HAS_4LEVEL_HACK */
3468872fec16SHugh Dickins 	spin_unlock(&mm->page_table_lock);
34691bb3630eSHugh Dickins 	return 0;
34701da177e4SLinus Torvalds }
34711da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */
34721da177e4SLinus Torvalds 
34731b36ba81SNamhyung Kim static int __follow_pte(struct mm_struct *mm, unsigned long address,
3474f8ad0f49SJohannes Weiner 		pte_t **ptepp, spinlock_t **ptlp)
3475f8ad0f49SJohannes Weiner {
3476f8ad0f49SJohannes Weiner 	pgd_t *pgd;
3477f8ad0f49SJohannes Weiner 	pud_t *pud;
3478f8ad0f49SJohannes Weiner 	pmd_t *pmd;
3479f8ad0f49SJohannes Weiner 	pte_t *ptep;
3480f8ad0f49SJohannes Weiner 
3481f8ad0f49SJohannes Weiner 	pgd = pgd_offset(mm, address);
3482f8ad0f49SJohannes Weiner 	if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
3483f8ad0f49SJohannes Weiner 		goto out;
3484f8ad0f49SJohannes Weiner 
3485f8ad0f49SJohannes Weiner 	pud = pud_offset(pgd, address);
3486f8ad0f49SJohannes Weiner 	if (pud_none(*pud) || unlikely(pud_bad(*pud)))
3487f8ad0f49SJohannes Weiner 		goto out;
3488f8ad0f49SJohannes Weiner 
3489f8ad0f49SJohannes Weiner 	pmd = pmd_offset(pud, address);
3490f66055abSAndrea Arcangeli 	VM_BUG_ON(pmd_trans_huge(*pmd));
3491f8ad0f49SJohannes Weiner 	if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
3492f8ad0f49SJohannes Weiner 		goto out;
3493f8ad0f49SJohannes Weiner 
3494f8ad0f49SJohannes Weiner 	/* We cannot handle huge page PFN maps. Luckily they don't exist. */
3495f8ad0f49SJohannes Weiner 	if (pmd_huge(*pmd))
3496f8ad0f49SJohannes Weiner 		goto out;
3497f8ad0f49SJohannes Weiner 
3498f8ad0f49SJohannes Weiner 	ptep = pte_offset_map_lock(mm, pmd, address, ptlp);
3499f8ad0f49SJohannes Weiner 	if (!ptep)
3500f8ad0f49SJohannes Weiner 		goto out;
3501f8ad0f49SJohannes Weiner 	if (!pte_present(*ptep))
3502f8ad0f49SJohannes Weiner 		goto unlock;
3503f8ad0f49SJohannes Weiner 	*ptepp = ptep;
3504f8ad0f49SJohannes Weiner 	return 0;
3505f8ad0f49SJohannes Weiner unlock:
3506f8ad0f49SJohannes Weiner 	pte_unmap_unlock(ptep, *ptlp);
3507f8ad0f49SJohannes Weiner out:
3508f8ad0f49SJohannes Weiner 	return -EINVAL;
3509f8ad0f49SJohannes Weiner }
3510f8ad0f49SJohannes Weiner 
35111b36ba81SNamhyung Kim static inline int follow_pte(struct mm_struct *mm, unsigned long address,
35121b36ba81SNamhyung Kim 			     pte_t **ptepp, spinlock_t **ptlp)
35131b36ba81SNamhyung Kim {
35141b36ba81SNamhyung Kim 	int res;
35151b36ba81SNamhyung Kim 
35161b36ba81SNamhyung Kim 	/* (void) is needed to make gcc happy */
35171b36ba81SNamhyung Kim 	(void) __cond_lock(*ptlp,
35181b36ba81SNamhyung Kim 			   !(res = __follow_pte(mm, address, ptepp, ptlp)));
35191b36ba81SNamhyung Kim 	return res;
35201b36ba81SNamhyung Kim }
35211b36ba81SNamhyung Kim 
35223b6748e2SJohannes Weiner /**
35233b6748e2SJohannes Weiner  * follow_pfn - look up PFN at a user virtual address
35243b6748e2SJohannes Weiner  * @vma: memory mapping
35253b6748e2SJohannes Weiner  * @address: user virtual address
35263b6748e2SJohannes Weiner  * @pfn: location to store found PFN
35273b6748e2SJohannes Weiner  *
35283b6748e2SJohannes Weiner  * Only IO mappings and raw PFN mappings are allowed.
35293b6748e2SJohannes Weiner  *
35303b6748e2SJohannes Weiner  * Returns zero and the pfn at @pfn on success, -ve otherwise.
35313b6748e2SJohannes Weiner  */
35323b6748e2SJohannes Weiner int follow_pfn(struct vm_area_struct *vma, unsigned long address,
35333b6748e2SJohannes Weiner 	unsigned long *pfn)
35343b6748e2SJohannes Weiner {
35353b6748e2SJohannes Weiner 	int ret = -EINVAL;
35363b6748e2SJohannes Weiner 	spinlock_t *ptl;
35373b6748e2SJohannes Weiner 	pte_t *ptep;
35383b6748e2SJohannes Weiner 
35393b6748e2SJohannes Weiner 	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
35403b6748e2SJohannes Weiner 		return ret;
35413b6748e2SJohannes Weiner 
35423b6748e2SJohannes Weiner 	ret = follow_pte(vma->vm_mm, address, &ptep, &ptl);
35433b6748e2SJohannes Weiner 	if (ret)
35443b6748e2SJohannes Weiner 		return ret;
35453b6748e2SJohannes Weiner 	*pfn = pte_pfn(*ptep);
35463b6748e2SJohannes Weiner 	pte_unmap_unlock(ptep, ptl);
35473b6748e2SJohannes Weiner 	return 0;
35483b6748e2SJohannes Weiner }
35493b6748e2SJohannes Weiner EXPORT_SYMBOL(follow_pfn);
35503b6748e2SJohannes Weiner 
355128b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT
3552d87fe660Svenkatesh.pallipadi@intel.com int follow_phys(struct vm_area_struct *vma,
355328b2ee20SRik van Riel 		unsigned long address, unsigned int flags,
3554d87fe660Svenkatesh.pallipadi@intel.com 		unsigned long *prot, resource_size_t *phys)
355528b2ee20SRik van Riel {
355603668a4dSJohannes Weiner 	int ret = -EINVAL;
355728b2ee20SRik van Riel 	pte_t *ptep, pte;
355828b2ee20SRik van Riel 	spinlock_t *ptl;
355928b2ee20SRik van Riel 
3560d87fe660Svenkatesh.pallipadi@intel.com 	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
3561d87fe660Svenkatesh.pallipadi@intel.com 		goto out;
356228b2ee20SRik van Riel 
356303668a4dSJohannes Weiner 	if (follow_pte(vma->vm_mm, address, &ptep, &ptl))
3564d87fe660Svenkatesh.pallipadi@intel.com 		goto out;
356528b2ee20SRik van Riel 	pte = *ptep;
356603668a4dSJohannes Weiner 
356728b2ee20SRik van Riel 	if ((flags & FOLL_WRITE) && !pte_write(pte))
356828b2ee20SRik van Riel 		goto unlock;
356928b2ee20SRik van Riel 
357028b2ee20SRik van Riel 	*prot = pgprot_val(pte_pgprot(pte));
357103668a4dSJohannes Weiner 	*phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT;
357228b2ee20SRik van Riel 
357303668a4dSJohannes Weiner 	ret = 0;
357428b2ee20SRik van Riel unlock:
357528b2ee20SRik van Riel 	pte_unmap_unlock(ptep, ptl);
357628b2ee20SRik van Riel out:
3577d87fe660Svenkatesh.pallipadi@intel.com 	return ret;
357828b2ee20SRik van Riel }
357928b2ee20SRik van Riel 
358028b2ee20SRik van Riel int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
358128b2ee20SRik van Riel 			void *buf, int len, int write)
358228b2ee20SRik van Riel {
358328b2ee20SRik van Riel 	resource_size_t phys_addr;
358428b2ee20SRik van Riel 	unsigned long prot = 0;
35852bc7273bSKOSAKI Motohiro 	void __iomem *maddr;
358628b2ee20SRik van Riel 	int offset = addr & (PAGE_SIZE-1);
358728b2ee20SRik van Riel 
3588d87fe660Svenkatesh.pallipadi@intel.com 	if (follow_phys(vma, addr, write, &prot, &phys_addr))
358928b2ee20SRik van Riel 		return -EINVAL;
359028b2ee20SRik van Riel 
35919cb12d7bSGrazvydas Ignotas 	maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot);
359228b2ee20SRik van Riel 	if (write)
359328b2ee20SRik van Riel 		memcpy_toio(maddr + offset, buf, len);
359428b2ee20SRik van Riel 	else
359528b2ee20SRik van Riel 		memcpy_fromio(buf, maddr + offset, len);
359628b2ee20SRik van Riel 	iounmap(maddr);
359728b2ee20SRik van Riel 
359828b2ee20SRik van Riel 	return len;
359928b2ee20SRik van Riel }
36005a73633eSUwe Kleine-König EXPORT_SYMBOL_GPL(generic_access_phys);
360128b2ee20SRik van Riel #endif
360228b2ee20SRik van Riel 
36030ec76a11SDavid Howells /*
3604206cb636SStephen Wilson  * Access another process' address space as given in mm.  If non-NULL, use the
3605206cb636SStephen Wilson  * given task for page fault accounting.
36060ec76a11SDavid Howells  */
3607206cb636SStephen Wilson static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
3608206cb636SStephen Wilson 		unsigned long addr, void *buf, int len, int write)
36090ec76a11SDavid Howells {
36100ec76a11SDavid Howells 	struct vm_area_struct *vma;
36110ec76a11SDavid Howells 	void *old_buf = buf;
36120ec76a11SDavid Howells 
36130ec76a11SDavid Howells 	down_read(&mm->mmap_sem);
3614183ff22bSSimon Arlott 	/* ignore errors, just check how much was successfully transferred */
36150ec76a11SDavid Howells 	while (len) {
36160ec76a11SDavid Howells 		int bytes, ret, offset;
36170ec76a11SDavid Howells 		void *maddr;
361828b2ee20SRik van Riel 		struct page *page = NULL;
36190ec76a11SDavid Howells 
36200ec76a11SDavid Howells 		ret = get_user_pages(tsk, mm, addr, 1,
36210ec76a11SDavid Howells 				write, 1, &page, &vma);
362228b2ee20SRik van Riel 		if (ret <= 0) {
3623dbffcd03SRik van Riel #ifndef CONFIG_HAVE_IOREMAP_PROT
3624dbffcd03SRik van Riel 			break;
3625dbffcd03SRik van Riel #else
362628b2ee20SRik van Riel 			/*
362728b2ee20SRik van Riel 			 * Check if this is a VM_IO | VM_PFNMAP VMA, which
362828b2ee20SRik van Riel 			 * we can access using slightly different code.
362928b2ee20SRik van Riel 			 */
363028b2ee20SRik van Riel 			vma = find_vma(mm, addr);
3631fe936dfcSMichael Ellerman 			if (!vma || vma->vm_start > addr)
36320ec76a11SDavid Howells 				break;
363328b2ee20SRik van Riel 			if (vma->vm_ops && vma->vm_ops->access)
363428b2ee20SRik van Riel 				ret = vma->vm_ops->access(vma, addr, buf,
363528b2ee20SRik van Riel 							  len, write);
363628b2ee20SRik van Riel 			if (ret <= 0)
363728b2ee20SRik van Riel 				break;
363828b2ee20SRik van Riel 			bytes = ret;
3639dbffcd03SRik van Riel #endif
364028b2ee20SRik van Riel 		} else {
36410ec76a11SDavid Howells 			bytes = len;
36420ec76a11SDavid Howells 			offset = addr & (PAGE_SIZE-1);
36430ec76a11SDavid Howells 			if (bytes > PAGE_SIZE-offset)
36440ec76a11SDavid Howells 				bytes = PAGE_SIZE-offset;
36450ec76a11SDavid Howells 
36460ec76a11SDavid Howells 			maddr = kmap(page);
36470ec76a11SDavid Howells 			if (write) {
36480ec76a11SDavid Howells 				copy_to_user_page(vma, page, addr,
36490ec76a11SDavid Howells 						  maddr + offset, buf, bytes);
36500ec76a11SDavid Howells 				set_page_dirty_lock(page);
36510ec76a11SDavid Howells 			} else {
36520ec76a11SDavid Howells 				copy_from_user_page(vma, page, addr,
36530ec76a11SDavid Howells 						    buf, maddr + offset, bytes);
36540ec76a11SDavid Howells 			}
36550ec76a11SDavid Howells 			kunmap(page);
36560ec76a11SDavid Howells 			page_cache_release(page);
365728b2ee20SRik van Riel 		}
36580ec76a11SDavid Howells 		len -= bytes;
36590ec76a11SDavid Howells 		buf += bytes;
36600ec76a11SDavid Howells 		addr += bytes;
36610ec76a11SDavid Howells 	}
36620ec76a11SDavid Howells 	up_read(&mm->mmap_sem);
36630ec76a11SDavid Howells 
36640ec76a11SDavid Howells 	return buf - old_buf;
36650ec76a11SDavid Howells }
366603252919SAndi Kleen 
36675ddd36b9SStephen Wilson /**
3668ae91dbfcSRandy Dunlap  * access_remote_vm - access another process' address space
36695ddd36b9SStephen Wilson  * @mm:		the mm_struct of the target address space
36705ddd36b9SStephen Wilson  * @addr:	start address to access
36715ddd36b9SStephen Wilson  * @buf:	source or destination buffer
36725ddd36b9SStephen Wilson  * @len:	number of bytes to transfer
36735ddd36b9SStephen Wilson  * @write:	whether the access is a write
36745ddd36b9SStephen Wilson  *
36755ddd36b9SStephen Wilson  * The caller must hold a reference on @mm.
36765ddd36b9SStephen Wilson  */
36775ddd36b9SStephen Wilson int access_remote_vm(struct mm_struct *mm, unsigned long addr,
36785ddd36b9SStephen Wilson 		void *buf, int len, int write)
36795ddd36b9SStephen Wilson {
36805ddd36b9SStephen Wilson 	return __access_remote_vm(NULL, mm, addr, buf, len, write);
36815ddd36b9SStephen Wilson }
36825ddd36b9SStephen Wilson 
368303252919SAndi Kleen /*
3684206cb636SStephen Wilson  * Access another process' address space.
3685206cb636SStephen Wilson  * Source/target buffer must be kernel space,
3686206cb636SStephen Wilson  * Do not walk the page table directly, use get_user_pages
3687206cb636SStephen Wilson  */
3688206cb636SStephen Wilson int access_process_vm(struct task_struct *tsk, unsigned long addr,
3689206cb636SStephen Wilson 		void *buf, int len, int write)
3690206cb636SStephen Wilson {
3691206cb636SStephen Wilson 	struct mm_struct *mm;
3692206cb636SStephen Wilson 	int ret;
3693206cb636SStephen Wilson 
3694206cb636SStephen Wilson 	mm = get_task_mm(tsk);
3695206cb636SStephen Wilson 	if (!mm)
3696206cb636SStephen Wilson 		return 0;
3697206cb636SStephen Wilson 
3698206cb636SStephen Wilson 	ret = __access_remote_vm(tsk, mm, addr, buf, len, write);
3699206cb636SStephen Wilson 	mmput(mm);
3700206cb636SStephen Wilson 
3701206cb636SStephen Wilson 	return ret;
3702206cb636SStephen Wilson }
3703206cb636SStephen Wilson 
370403252919SAndi Kleen /*
370503252919SAndi Kleen  * Print the name of a VMA.
370603252919SAndi Kleen  */
370703252919SAndi Kleen void print_vma_addr(char *prefix, unsigned long ip)
370803252919SAndi Kleen {
370903252919SAndi Kleen 	struct mm_struct *mm = current->mm;
371003252919SAndi Kleen 	struct vm_area_struct *vma;
371103252919SAndi Kleen 
3712e8bff74aSIngo Molnar 	/*
3713e8bff74aSIngo Molnar 	 * Do not print if we are in atomic
3714e8bff74aSIngo Molnar 	 * contexts (in exception stacks, etc.):
3715e8bff74aSIngo Molnar 	 */
3716e8bff74aSIngo Molnar 	if (preempt_count())
3717e8bff74aSIngo Molnar 		return;
3718e8bff74aSIngo Molnar 
371903252919SAndi Kleen 	down_read(&mm->mmap_sem);
372003252919SAndi Kleen 	vma = find_vma(mm, ip);
372103252919SAndi Kleen 	if (vma && vma->vm_file) {
372203252919SAndi Kleen 		struct file *f = vma->vm_file;
372303252919SAndi Kleen 		char *buf = (char *)__get_free_page(GFP_KERNEL);
372403252919SAndi Kleen 		if (buf) {
37252fbc57c5SAndy Shevchenko 			char *p;
372603252919SAndi Kleen 
3727cf28b486SJan Blunck 			p = d_path(&f->f_path, buf, PAGE_SIZE);
372803252919SAndi Kleen 			if (IS_ERR(p))
372903252919SAndi Kleen 				p = "?";
37302fbc57c5SAndy Shevchenko 			printk("%s%s[%lx+%lx]", prefix, kbasename(p),
373103252919SAndi Kleen 					vma->vm_start,
373203252919SAndi Kleen 					vma->vm_end - vma->vm_start);
373303252919SAndi Kleen 			free_page((unsigned long)buf);
373403252919SAndi Kleen 		}
373503252919SAndi Kleen 	}
373651a07e50SJeff Liu 	up_read(&mm->mmap_sem);
373703252919SAndi Kleen }
37383ee1afa3SNick Piggin 
3739662bbcb2SMichael S. Tsirkin #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)
3740*9ec23531SDavid Hildenbrand void __might_fault(const char *file, int line)
37413ee1afa3SNick Piggin {
374295156f00SPeter Zijlstra 	/*
374395156f00SPeter Zijlstra 	 * Some code (nfs/sunrpc) uses socket ops on kernel memory while
374495156f00SPeter Zijlstra 	 * holding the mmap_sem, this is safe because kernel memory doesn't
374595156f00SPeter Zijlstra 	 * get paged out, therefore we'll never actually fault, and the
374695156f00SPeter Zijlstra 	 * below annotations will generate false positives.
374795156f00SPeter Zijlstra 	 */
374895156f00SPeter Zijlstra 	if (segment_eq(get_fs(), KERNEL_DS))
374995156f00SPeter Zijlstra 		return;
3750*9ec23531SDavid Hildenbrand 	if (pagefault_disabled())
3751662bbcb2SMichael S. Tsirkin 		return;
3752*9ec23531SDavid Hildenbrand 	__might_sleep(file, line, 0);
3753*9ec23531SDavid Hildenbrand #if defined(CONFIG_DEBUG_ATOMIC_SLEEP)
3754662bbcb2SMichael S. Tsirkin 	if (current->mm)
37553ee1afa3SNick Piggin 		might_lock_read(&current->mm->mmap_sem);
3756*9ec23531SDavid Hildenbrand #endif
37573ee1afa3SNick Piggin }
3758*9ec23531SDavid Hildenbrand EXPORT_SYMBOL(__might_fault);
37593ee1afa3SNick Piggin #endif
376047ad8475SAndrea Arcangeli 
376147ad8475SAndrea Arcangeli #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
376247ad8475SAndrea Arcangeli static void clear_gigantic_page(struct page *page,
376347ad8475SAndrea Arcangeli 				unsigned long addr,
376447ad8475SAndrea Arcangeli 				unsigned int pages_per_huge_page)
376547ad8475SAndrea Arcangeli {
376647ad8475SAndrea Arcangeli 	int i;
376747ad8475SAndrea Arcangeli 	struct page *p = page;
376847ad8475SAndrea Arcangeli 
376947ad8475SAndrea Arcangeli 	might_sleep();
377047ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page;
377147ad8475SAndrea Arcangeli 	     i++, p = mem_map_next(p, page, i)) {
377247ad8475SAndrea Arcangeli 		cond_resched();
377347ad8475SAndrea Arcangeli 		clear_user_highpage(p, addr + i * PAGE_SIZE);
377447ad8475SAndrea Arcangeli 	}
377547ad8475SAndrea Arcangeli }
377647ad8475SAndrea Arcangeli void clear_huge_page(struct page *page,
377747ad8475SAndrea Arcangeli 		     unsigned long addr, unsigned int pages_per_huge_page)
377847ad8475SAndrea Arcangeli {
377947ad8475SAndrea Arcangeli 	int i;
378047ad8475SAndrea Arcangeli 
378147ad8475SAndrea Arcangeli 	if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
378247ad8475SAndrea Arcangeli 		clear_gigantic_page(page, addr, pages_per_huge_page);
378347ad8475SAndrea Arcangeli 		return;
378447ad8475SAndrea Arcangeli 	}
378547ad8475SAndrea Arcangeli 
378647ad8475SAndrea Arcangeli 	might_sleep();
378747ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page; i++) {
378847ad8475SAndrea Arcangeli 		cond_resched();
378947ad8475SAndrea Arcangeli 		clear_user_highpage(page + i, addr + i * PAGE_SIZE);
379047ad8475SAndrea Arcangeli 	}
379147ad8475SAndrea Arcangeli }
379247ad8475SAndrea Arcangeli 
379347ad8475SAndrea Arcangeli static void copy_user_gigantic_page(struct page *dst, struct page *src,
379447ad8475SAndrea Arcangeli 				    unsigned long addr,
379547ad8475SAndrea Arcangeli 				    struct vm_area_struct *vma,
379647ad8475SAndrea Arcangeli 				    unsigned int pages_per_huge_page)
379747ad8475SAndrea Arcangeli {
379847ad8475SAndrea Arcangeli 	int i;
379947ad8475SAndrea Arcangeli 	struct page *dst_base = dst;
380047ad8475SAndrea Arcangeli 	struct page *src_base = src;
380147ad8475SAndrea Arcangeli 
380247ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page; ) {
380347ad8475SAndrea Arcangeli 		cond_resched();
380447ad8475SAndrea Arcangeli 		copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma);
380547ad8475SAndrea Arcangeli 
380647ad8475SAndrea Arcangeli 		i++;
380747ad8475SAndrea Arcangeli 		dst = mem_map_next(dst, dst_base, i);
380847ad8475SAndrea Arcangeli 		src = mem_map_next(src, src_base, i);
380947ad8475SAndrea Arcangeli 	}
381047ad8475SAndrea Arcangeli }
381147ad8475SAndrea Arcangeli 
381247ad8475SAndrea Arcangeli void copy_user_huge_page(struct page *dst, struct page *src,
381347ad8475SAndrea Arcangeli 			 unsigned long addr, struct vm_area_struct *vma,
381447ad8475SAndrea Arcangeli 			 unsigned int pages_per_huge_page)
381547ad8475SAndrea Arcangeli {
381647ad8475SAndrea Arcangeli 	int i;
381747ad8475SAndrea Arcangeli 
381847ad8475SAndrea Arcangeli 	if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
381947ad8475SAndrea Arcangeli 		copy_user_gigantic_page(dst, src, addr, vma,
382047ad8475SAndrea Arcangeli 					pages_per_huge_page);
382147ad8475SAndrea Arcangeli 		return;
382247ad8475SAndrea Arcangeli 	}
382347ad8475SAndrea Arcangeli 
382447ad8475SAndrea Arcangeli 	might_sleep();
382547ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page; i++) {
382647ad8475SAndrea Arcangeli 		cond_resched();
382747ad8475SAndrea Arcangeli 		copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
382847ad8475SAndrea Arcangeli 	}
382947ad8475SAndrea Arcangeli }
383047ad8475SAndrea Arcangeli #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */
383149076ec2SKirill A. Shutemov 
383240b64acdSOlof Johansson #if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS
3833b35f1819SKirill A. Shutemov 
3834b35f1819SKirill A. Shutemov static struct kmem_cache *page_ptl_cachep;
3835b35f1819SKirill A. Shutemov 
3836b35f1819SKirill A. Shutemov void __init ptlock_cache_init(void)
3837b35f1819SKirill A. Shutemov {
3838b35f1819SKirill A. Shutemov 	page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0,
3839b35f1819SKirill A. Shutemov 			SLAB_PANIC, NULL);
3840b35f1819SKirill A. Shutemov }
3841b35f1819SKirill A. Shutemov 
3842539edb58SPeter Zijlstra bool ptlock_alloc(struct page *page)
384349076ec2SKirill A. Shutemov {
384449076ec2SKirill A. Shutemov 	spinlock_t *ptl;
384549076ec2SKirill A. Shutemov 
3846b35f1819SKirill A. Shutemov 	ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL);
384749076ec2SKirill A. Shutemov 	if (!ptl)
384849076ec2SKirill A. Shutemov 		return false;
3849539edb58SPeter Zijlstra 	page->ptl = ptl;
385049076ec2SKirill A. Shutemov 	return true;
385149076ec2SKirill A. Shutemov }
385249076ec2SKirill A. Shutemov 
3853539edb58SPeter Zijlstra void ptlock_free(struct page *page)
385449076ec2SKirill A. Shutemov {
3855b35f1819SKirill A. Shutemov 	kmem_cache_free(page_ptl_cachep, page->ptl);
385649076ec2SKirill A. Shutemov }
385749076ec2SKirill A. Shutemov #endif
3858