xref: /linux/mm/memory.c (revision 4e047f897771222215ee572e1c0b25e9417376eb)
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 	 */
6933dc14741SHugh Dickins 	if (vma->vm_ops)
694071361d3SJoe Perches 		printk(KERN_ALERT "vma->vm_ops->fault: %pSR\n",
695071361d3SJoe Perches 		       vma->vm_ops->fault);
69672c2d531SAl Viro 	if (vma->vm_file)
697071361d3SJoe Perches 		printk(KERN_ALERT "vma->vm_file->f_op->mmap: %pSR\n",
698071361d3SJoe Perches 		       vma->vm_file->f_op->mmap);
699b5810039SNick Piggin 	dump_stack();
700373d4d09SRusty Russell 	add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
701b5810039SNick Piggin }
702b5810039SNick Piggin 
703b5810039SNick Piggin /*
7047e675137SNick Piggin  * vm_normal_page -- This function gets the "struct page" associated with a pte.
7056aab341eSLinus Torvalds  *
7067e675137SNick Piggin  * "Special" mappings do not wish to be associated with a "struct page" (either
7077e675137SNick Piggin  * it doesn't exist, or it exists but they don't want to touch it). In this
7087e675137SNick Piggin  * case, NULL is returned here. "Normal" mappings do have a struct page.
709b379d790SJared Hulbert  *
7107e675137SNick Piggin  * There are 2 broad cases. Firstly, an architecture may define a pte_special()
7117e675137SNick Piggin  * pte bit, in which case this function is trivial. Secondly, an architecture
7127e675137SNick Piggin  * may not have a spare pte bit, which requires a more complicated scheme,
7137e675137SNick Piggin  * described below.
7147e675137SNick Piggin  *
7157e675137SNick Piggin  * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a
7167e675137SNick Piggin  * special mapping (even if there are underlying and valid "struct pages").
7177e675137SNick Piggin  * COWed pages of a VM_PFNMAP are always normal.
7186aab341eSLinus Torvalds  *
719b379d790SJared Hulbert  * The way we recognize COWed pages within VM_PFNMAP mappings is through the
720b379d790SJared Hulbert  * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit
7217e675137SNick Piggin  * set, and the vm_pgoff will point to the first PFN mapped: thus every special
7227e675137SNick Piggin  * mapping will always honor the rule
7236aab341eSLinus Torvalds  *
7246aab341eSLinus Torvalds  *	pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
7256aab341eSLinus Torvalds  *
7267e675137SNick Piggin  * And for normal mappings this is false.
727b379d790SJared Hulbert  *
7287e675137SNick Piggin  * This restricts such mappings to be a linear translation from virtual address
7297e675137SNick Piggin  * to pfn. To get around this restriction, we allow arbitrary mappings so long
7307e675137SNick Piggin  * as the vma is not a COW mapping; in that case, we know that all ptes are
7317e675137SNick Piggin  * special (because none can have been COWed).
732b379d790SJared Hulbert  *
733b379d790SJared Hulbert  *
7347e675137SNick Piggin  * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP.
7357e675137SNick Piggin  *
736b379d790SJared Hulbert  * VM_MIXEDMAP mappings can likewise contain memory with or without "struct
737b379d790SJared Hulbert  * page" backing, however the difference is that _all_ pages with a struct
738b379d790SJared Hulbert  * page (that is, those where pfn_valid is true) are refcounted and considered
739b379d790SJared Hulbert  * normal pages by the VM. The disadvantage is that pages are refcounted
740b379d790SJared Hulbert  * (which can be slower and simply not an option for some PFNMAP users). The
741b379d790SJared Hulbert  * advantage is that we don't have to follow the strict linearity rule of
742b379d790SJared Hulbert  * PFNMAP mappings in order to support COWable mappings.
743b379d790SJared Hulbert  *
744ee498ed7SHugh Dickins  */
7457e675137SNick Piggin #ifdef __HAVE_ARCH_PTE_SPECIAL
7467e675137SNick Piggin # define HAVE_PTE_SPECIAL 1
7477e675137SNick Piggin #else
7487e675137SNick Piggin # define HAVE_PTE_SPECIAL 0
7497e675137SNick Piggin #endif
7507e675137SNick Piggin struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
7517e675137SNick Piggin 				pte_t pte)
752ee498ed7SHugh Dickins {
75322b31eecSHugh Dickins 	unsigned long pfn = pte_pfn(pte);
7547e675137SNick Piggin 
7557e675137SNick Piggin 	if (HAVE_PTE_SPECIAL) {
756b38af472SHugh Dickins 		if (likely(!pte_special(pte)))
75722b31eecSHugh Dickins 			goto check_pfn;
758667a0a06SDavid Vrabel 		if (vma->vm_ops && vma->vm_ops->find_special_page)
759667a0a06SDavid Vrabel 			return vma->vm_ops->find_special_page(vma, addr);
760a13ea5b7SHugh Dickins 		if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
761a13ea5b7SHugh Dickins 			return NULL;
76262eede62SHugh Dickins 		if (!is_zero_pfn(pfn))
76322b31eecSHugh Dickins 			print_bad_pte(vma, addr, pte, NULL);
7647e675137SNick Piggin 		return NULL;
7657e675137SNick Piggin 	}
7667e675137SNick Piggin 
7677e675137SNick Piggin 	/* !HAVE_PTE_SPECIAL case follows: */
7687e675137SNick Piggin 
769b379d790SJared Hulbert 	if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
770b379d790SJared Hulbert 		if (vma->vm_flags & VM_MIXEDMAP) {
771b379d790SJared Hulbert 			if (!pfn_valid(pfn))
772b379d790SJared Hulbert 				return NULL;
773b379d790SJared Hulbert 			goto out;
774b379d790SJared Hulbert 		} else {
7757e675137SNick Piggin 			unsigned long off;
7767e675137SNick Piggin 			off = (addr - vma->vm_start) >> PAGE_SHIFT;
7776aab341eSLinus Torvalds 			if (pfn == vma->vm_pgoff + off)
7786aab341eSLinus Torvalds 				return NULL;
77967121172SLinus Torvalds 			if (!is_cow_mapping(vma->vm_flags))
780fb155c16SLinus Torvalds 				return NULL;
7816aab341eSLinus Torvalds 		}
782b379d790SJared Hulbert 	}
7836aab341eSLinus Torvalds 
784b38af472SHugh Dickins 	if (is_zero_pfn(pfn))
785b38af472SHugh Dickins 		return NULL;
78622b31eecSHugh Dickins check_pfn:
78722b31eecSHugh Dickins 	if (unlikely(pfn > highest_memmap_pfn)) {
78822b31eecSHugh Dickins 		print_bad_pte(vma, addr, pte, NULL);
78922b31eecSHugh Dickins 		return NULL;
79022b31eecSHugh Dickins 	}
7916aab341eSLinus Torvalds 
7926aab341eSLinus Torvalds 	/*
7937e675137SNick Piggin 	 * NOTE! We still have PageReserved() pages in the page tables.
7947e675137SNick Piggin 	 * eg. VDSO mappings can cause them to exist.
7956aab341eSLinus Torvalds 	 */
796b379d790SJared Hulbert out:
7976aab341eSLinus Torvalds 	return pfn_to_page(pfn);
798ee498ed7SHugh Dickins }
799ee498ed7SHugh Dickins 
800ee498ed7SHugh Dickins /*
8011da177e4SLinus Torvalds  * copy one vm_area from one task to the other. Assumes the page tables
8021da177e4SLinus Torvalds  * already present in the new task to be cleared in the whole range
8031da177e4SLinus Torvalds  * covered by this vma.
8041da177e4SLinus Torvalds  */
8051da177e4SLinus Torvalds 
806570a335bSHugh Dickins static inline unsigned long
8071da177e4SLinus Torvalds copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
808b5810039SNick Piggin 		pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma,
8098c103762SHugh Dickins 		unsigned long addr, int *rss)
8101da177e4SLinus Torvalds {
811b5810039SNick Piggin 	unsigned long vm_flags = vma->vm_flags;
8121da177e4SLinus Torvalds 	pte_t pte = *src_pte;
8131da177e4SLinus Torvalds 	struct page *page;
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds 	/* pte contains position in swap or file, so copy. */
8161da177e4SLinus Torvalds 	if (unlikely(!pte_present(pte))) {
8170697212aSChristoph Lameter 		swp_entry_t entry = pte_to_swp_entry(pte);
8180697212aSChristoph Lameter 
8192022b4d1SHugh Dickins 		if (likely(!non_swap_entry(entry))) {
820570a335bSHugh Dickins 			if (swap_duplicate(entry) < 0)
821570a335bSHugh Dickins 				return entry.val;
822570a335bSHugh Dickins 
8231da177e4SLinus Torvalds 			/* make sure dst_mm is on swapoff's mmlist. */
8241da177e4SLinus Torvalds 			if (unlikely(list_empty(&dst_mm->mmlist))) {
8251da177e4SLinus Torvalds 				spin_lock(&mmlist_lock);
826f412ac08SHugh Dickins 				if (list_empty(&dst_mm->mmlist))
827f412ac08SHugh Dickins 					list_add(&dst_mm->mmlist,
828f412ac08SHugh Dickins 							&src_mm->mmlist);
8291da177e4SLinus Torvalds 				spin_unlock(&mmlist_lock);
8301da177e4SLinus Torvalds 			}
831b084d435SKAMEZAWA Hiroyuki 			rss[MM_SWAPENTS]++;
8322022b4d1SHugh Dickins 		} else if (is_migration_entry(entry)) {
8339f9f1acdSKonstantin Khlebnikov 			page = migration_entry_to_page(entry);
8349f9f1acdSKonstantin Khlebnikov 
8359f9f1acdSKonstantin Khlebnikov 			if (PageAnon(page))
8369f9f1acdSKonstantin Khlebnikov 				rss[MM_ANONPAGES]++;
8379f9f1acdSKonstantin Khlebnikov 			else
8389f9f1acdSKonstantin Khlebnikov 				rss[MM_FILEPAGES]++;
8399f9f1acdSKonstantin Khlebnikov 
8409f9f1acdSKonstantin Khlebnikov 			if (is_write_migration_entry(entry) &&
8410697212aSChristoph Lameter 					is_cow_mapping(vm_flags)) {
8420697212aSChristoph Lameter 				/*
8439f9f1acdSKonstantin Khlebnikov 				 * COW mappings require pages in both
8449f9f1acdSKonstantin Khlebnikov 				 * parent and child to be set to read.
8450697212aSChristoph Lameter 				 */
8460697212aSChristoph Lameter 				make_migration_entry_read(&entry);
8470697212aSChristoph Lameter 				pte = swp_entry_to_pte(entry);
848c3d16e16SCyrill Gorcunov 				if (pte_swp_soft_dirty(*src_pte))
849c3d16e16SCyrill Gorcunov 					pte = pte_swp_mksoft_dirty(pte);
8500697212aSChristoph Lameter 				set_pte_at(src_mm, addr, src_pte, pte);
8510697212aSChristoph Lameter 			}
8521da177e4SLinus Torvalds 		}
853ae859762SHugh Dickins 		goto out_set_pte;
8541da177e4SLinus Torvalds 	}
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 	/*
8571da177e4SLinus Torvalds 	 * If it's a COW mapping, write protect it both
8581da177e4SLinus Torvalds 	 * in the parent and the child
8591da177e4SLinus Torvalds 	 */
86067121172SLinus Torvalds 	if (is_cow_mapping(vm_flags)) {
8611da177e4SLinus Torvalds 		ptep_set_wrprotect(src_mm, addr, src_pte);
8623dc90795SZachary Amsden 		pte = pte_wrprotect(pte);
8631da177e4SLinus Torvalds 	}
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds 	/*
8661da177e4SLinus Torvalds 	 * If it's a shared mapping, mark it clean in
8671da177e4SLinus Torvalds 	 * the child
8681da177e4SLinus Torvalds 	 */
8691da177e4SLinus Torvalds 	if (vm_flags & VM_SHARED)
8701da177e4SLinus Torvalds 		pte = pte_mkclean(pte);
8711da177e4SLinus Torvalds 	pte = pte_mkold(pte);
8726aab341eSLinus Torvalds 
8736aab341eSLinus Torvalds 	page = vm_normal_page(vma, addr, pte);
8746aab341eSLinus Torvalds 	if (page) {
8751da177e4SLinus Torvalds 		get_page(page);
87621333b2bSHugh Dickins 		page_dup_rmap(page);
877d559db08SKAMEZAWA Hiroyuki 		if (PageAnon(page))
878d559db08SKAMEZAWA Hiroyuki 			rss[MM_ANONPAGES]++;
879d559db08SKAMEZAWA Hiroyuki 		else
880d559db08SKAMEZAWA Hiroyuki 			rss[MM_FILEPAGES]++;
8816aab341eSLinus Torvalds 	}
882ae859762SHugh Dickins 
883ae859762SHugh Dickins out_set_pte:
884ae859762SHugh Dickins 	set_pte_at(dst_mm, addr, dst_pte, pte);
885570a335bSHugh Dickins 	return 0;
8861da177e4SLinus Torvalds }
8871da177e4SLinus Torvalds 
88821bda264SJerome Marchand static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
8891da177e4SLinus Torvalds 		   pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
8901da177e4SLinus Torvalds 		   unsigned long addr, unsigned long end)
8911da177e4SLinus Torvalds {
892c36987e2SDaisuke Nishimura 	pte_t *orig_src_pte, *orig_dst_pte;
8931da177e4SLinus Torvalds 	pte_t *src_pte, *dst_pte;
894c74df32cSHugh Dickins 	spinlock_t *src_ptl, *dst_ptl;
895e040f218SHugh Dickins 	int progress = 0;
896d559db08SKAMEZAWA Hiroyuki 	int rss[NR_MM_COUNTERS];
897570a335bSHugh Dickins 	swp_entry_t entry = (swp_entry_t){0};
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds again:
900d559db08SKAMEZAWA Hiroyuki 	init_rss_vec(rss);
901d559db08SKAMEZAWA Hiroyuki 
902c74df32cSHugh Dickins 	dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
9031da177e4SLinus Torvalds 	if (!dst_pte)
9041da177e4SLinus Torvalds 		return -ENOMEM;
905ece0e2b6SPeter Zijlstra 	src_pte = pte_offset_map(src_pmd, addr);
9064c21e2f2SHugh Dickins 	src_ptl = pte_lockptr(src_mm, src_pmd);
907f20dc5f7SIngo Molnar 	spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
908c36987e2SDaisuke Nishimura 	orig_src_pte = src_pte;
909c36987e2SDaisuke Nishimura 	orig_dst_pte = dst_pte;
9106606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds 	do {
9131da177e4SLinus Torvalds 		/*
9141da177e4SLinus Torvalds 		 * We are holding two locks at this point - either of them
9151da177e4SLinus Torvalds 		 * could generate latencies in another task on another CPU.
9161da177e4SLinus Torvalds 		 */
917e040f218SHugh Dickins 		if (progress >= 32) {
918e040f218SHugh Dickins 			progress = 0;
919e040f218SHugh Dickins 			if (need_resched() ||
92095c354feSNick Piggin 			    spin_needbreak(src_ptl) || spin_needbreak(dst_ptl))
9211da177e4SLinus Torvalds 				break;
922e040f218SHugh Dickins 		}
9231da177e4SLinus Torvalds 		if (pte_none(*src_pte)) {
9241da177e4SLinus Torvalds 			progress++;
9251da177e4SLinus Torvalds 			continue;
9261da177e4SLinus Torvalds 		}
927570a335bSHugh Dickins 		entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte,
928570a335bSHugh Dickins 							vma, addr, rss);
929570a335bSHugh Dickins 		if (entry.val)
930570a335bSHugh Dickins 			break;
9311da177e4SLinus Torvalds 		progress += 8;
9321da177e4SLinus Torvalds 	} while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
9331da177e4SLinus Torvalds 
9346606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
935c74df32cSHugh Dickins 	spin_unlock(src_ptl);
936ece0e2b6SPeter Zijlstra 	pte_unmap(orig_src_pte);
937d559db08SKAMEZAWA Hiroyuki 	add_mm_rss_vec(dst_mm, rss);
938c36987e2SDaisuke Nishimura 	pte_unmap_unlock(orig_dst_pte, dst_ptl);
939c74df32cSHugh Dickins 	cond_resched();
940570a335bSHugh Dickins 
941570a335bSHugh Dickins 	if (entry.val) {
942570a335bSHugh Dickins 		if (add_swap_count_continuation(entry, GFP_KERNEL) < 0)
943570a335bSHugh Dickins 			return -ENOMEM;
944570a335bSHugh Dickins 		progress = 0;
945570a335bSHugh Dickins 	}
9461da177e4SLinus Torvalds 	if (addr != end)
9471da177e4SLinus Torvalds 		goto again;
9481da177e4SLinus Torvalds 	return 0;
9491da177e4SLinus Torvalds }
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
9521da177e4SLinus Torvalds 		pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma,
9531da177e4SLinus Torvalds 		unsigned long addr, unsigned long end)
9541da177e4SLinus Torvalds {
9551da177e4SLinus Torvalds 	pmd_t *src_pmd, *dst_pmd;
9561da177e4SLinus Torvalds 	unsigned long next;
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds 	dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
9591da177e4SLinus Torvalds 	if (!dst_pmd)
9601da177e4SLinus Torvalds 		return -ENOMEM;
9611da177e4SLinus Torvalds 	src_pmd = pmd_offset(src_pud, addr);
9621da177e4SLinus Torvalds 	do {
9631da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
96471e3aac0SAndrea Arcangeli 		if (pmd_trans_huge(*src_pmd)) {
96571e3aac0SAndrea Arcangeli 			int err;
96614d1a55cSAndrea Arcangeli 			VM_BUG_ON(next-addr != HPAGE_PMD_SIZE);
96771e3aac0SAndrea Arcangeli 			err = copy_huge_pmd(dst_mm, src_mm,
96871e3aac0SAndrea Arcangeli 					    dst_pmd, src_pmd, addr, vma);
96971e3aac0SAndrea Arcangeli 			if (err == -ENOMEM)
97071e3aac0SAndrea Arcangeli 				return -ENOMEM;
97171e3aac0SAndrea Arcangeli 			if (!err)
97271e3aac0SAndrea Arcangeli 				continue;
97371e3aac0SAndrea Arcangeli 			/* fall through */
97471e3aac0SAndrea Arcangeli 		}
9751da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(src_pmd))
9761da177e4SLinus Torvalds 			continue;
9771da177e4SLinus Torvalds 		if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd,
9781da177e4SLinus Torvalds 						vma, addr, next))
9791da177e4SLinus Torvalds 			return -ENOMEM;
9801da177e4SLinus Torvalds 	} while (dst_pmd++, src_pmd++, addr = next, addr != end);
9811da177e4SLinus Torvalds 	return 0;
9821da177e4SLinus Torvalds }
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
9851da177e4SLinus Torvalds 		pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma,
9861da177e4SLinus Torvalds 		unsigned long addr, unsigned long end)
9871da177e4SLinus Torvalds {
9881da177e4SLinus Torvalds 	pud_t *src_pud, *dst_pud;
9891da177e4SLinus Torvalds 	unsigned long next;
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 	dst_pud = pud_alloc(dst_mm, dst_pgd, addr);
9921da177e4SLinus Torvalds 	if (!dst_pud)
9931da177e4SLinus Torvalds 		return -ENOMEM;
9941da177e4SLinus Torvalds 	src_pud = pud_offset(src_pgd, addr);
9951da177e4SLinus Torvalds 	do {
9961da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
9971da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(src_pud))
9981da177e4SLinus Torvalds 			continue;
9991da177e4SLinus Torvalds 		if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud,
10001da177e4SLinus Torvalds 						vma, addr, next))
10011da177e4SLinus Torvalds 			return -ENOMEM;
10021da177e4SLinus Torvalds 	} while (dst_pud++, src_pud++, addr = next, addr != end);
10031da177e4SLinus Torvalds 	return 0;
10041da177e4SLinus Torvalds }
10051da177e4SLinus Torvalds 
10061da177e4SLinus Torvalds int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
10071da177e4SLinus Torvalds 		struct vm_area_struct *vma)
10081da177e4SLinus Torvalds {
10091da177e4SLinus Torvalds 	pgd_t *src_pgd, *dst_pgd;
10101da177e4SLinus Torvalds 	unsigned long next;
10111da177e4SLinus Torvalds 	unsigned long addr = vma->vm_start;
10121da177e4SLinus Torvalds 	unsigned long end = vma->vm_end;
10132ec74c3eSSagi Grimberg 	unsigned long mmun_start;	/* For mmu_notifiers */
10142ec74c3eSSagi Grimberg 	unsigned long mmun_end;		/* For mmu_notifiers */
10152ec74c3eSSagi Grimberg 	bool is_cow;
1016cddb8a5cSAndrea Arcangeli 	int ret;
10171da177e4SLinus Torvalds 
1018d992895bSNick Piggin 	/*
1019d992895bSNick Piggin 	 * Don't copy ptes where a page fault will fill them correctly.
1020d992895bSNick Piggin 	 * Fork becomes much lighter when there are big shared or private
1021d992895bSNick Piggin 	 * readonly mappings. The tradeoff is that copy_page_range is more
1022d992895bSNick Piggin 	 * efficient than faulting.
1023d992895bSNick Piggin 	 */
10240661a336SKirill A. Shutemov 	if (!(vma->vm_flags & (VM_HUGETLB | VM_PFNMAP | VM_MIXEDMAP)) &&
10250661a336SKirill A. Shutemov 			!vma->anon_vma)
1026d992895bSNick Piggin 		return 0;
1027d992895bSNick Piggin 
10281da177e4SLinus Torvalds 	if (is_vm_hugetlb_page(vma))
10291da177e4SLinus Torvalds 		return copy_hugetlb_page_range(dst_mm, src_mm, vma);
10301da177e4SLinus Torvalds 
1031b3b9c293SKonstantin Khlebnikov 	if (unlikely(vma->vm_flags & VM_PFNMAP)) {
10322ab64037Svenkatesh.pallipadi@intel.com 		/*
10332ab64037Svenkatesh.pallipadi@intel.com 		 * We do not free on error cases below as remove_vma
10342ab64037Svenkatesh.pallipadi@intel.com 		 * gets called on error from higher level routine
10352ab64037Svenkatesh.pallipadi@intel.com 		 */
10365180da41SSuresh Siddha 		ret = track_pfn_copy(vma);
10372ab64037Svenkatesh.pallipadi@intel.com 		if (ret)
10382ab64037Svenkatesh.pallipadi@intel.com 			return ret;
10392ab64037Svenkatesh.pallipadi@intel.com 	}
10402ab64037Svenkatesh.pallipadi@intel.com 
1041cddb8a5cSAndrea Arcangeli 	/*
1042cddb8a5cSAndrea Arcangeli 	 * We need to invalidate the secondary MMU mappings only when
1043cddb8a5cSAndrea Arcangeli 	 * there could be a permission downgrade on the ptes of the
1044cddb8a5cSAndrea Arcangeli 	 * parent mm. And a permission downgrade will only happen if
1045cddb8a5cSAndrea Arcangeli 	 * is_cow_mapping() returns true.
1046cddb8a5cSAndrea Arcangeli 	 */
10472ec74c3eSSagi Grimberg 	is_cow = is_cow_mapping(vma->vm_flags);
10482ec74c3eSSagi Grimberg 	mmun_start = addr;
10492ec74c3eSSagi Grimberg 	mmun_end   = end;
10502ec74c3eSSagi Grimberg 	if (is_cow)
10512ec74c3eSSagi Grimberg 		mmu_notifier_invalidate_range_start(src_mm, mmun_start,
10522ec74c3eSSagi Grimberg 						    mmun_end);
1053cddb8a5cSAndrea Arcangeli 
1054cddb8a5cSAndrea Arcangeli 	ret = 0;
10551da177e4SLinus Torvalds 	dst_pgd = pgd_offset(dst_mm, addr);
10561da177e4SLinus Torvalds 	src_pgd = pgd_offset(src_mm, addr);
10571da177e4SLinus Torvalds 	do {
10581da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
10591da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(src_pgd))
10601da177e4SLinus Torvalds 			continue;
1061cddb8a5cSAndrea Arcangeli 		if (unlikely(copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd,
1062cddb8a5cSAndrea Arcangeli 					    vma, addr, next))) {
1063cddb8a5cSAndrea Arcangeli 			ret = -ENOMEM;
1064cddb8a5cSAndrea Arcangeli 			break;
1065cddb8a5cSAndrea Arcangeli 		}
10661da177e4SLinus Torvalds 	} while (dst_pgd++, src_pgd++, addr = next, addr != end);
1067cddb8a5cSAndrea Arcangeli 
10682ec74c3eSSagi Grimberg 	if (is_cow)
10692ec74c3eSSagi Grimberg 		mmu_notifier_invalidate_range_end(src_mm, mmun_start, mmun_end);
1070cddb8a5cSAndrea Arcangeli 	return ret;
10711da177e4SLinus Torvalds }
10721da177e4SLinus Torvalds 
107351c6f666SRobin Holt static unsigned long zap_pte_range(struct mmu_gather *tlb,
1074b5810039SNick Piggin 				struct vm_area_struct *vma, pmd_t *pmd,
10751da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
107697a89413SPeter Zijlstra 				struct zap_details *details)
10771da177e4SLinus Torvalds {
1078b5810039SNick Piggin 	struct mm_struct *mm = tlb->mm;
1079d16dfc55SPeter Zijlstra 	int force_flush = 0;
1080d559db08SKAMEZAWA Hiroyuki 	int rss[NR_MM_COUNTERS];
108197a89413SPeter Zijlstra 	spinlock_t *ptl;
10825f1a1907SSteven Rostedt 	pte_t *start_pte;
108397a89413SPeter Zijlstra 	pte_t *pte;
10848a5f14a2SKirill A. Shutemov 	swp_entry_t entry;
1085d559db08SKAMEZAWA Hiroyuki 
1086d16dfc55SPeter Zijlstra again:
1087e303297eSPeter Zijlstra 	init_rss_vec(rss);
10885f1a1907SSteven Rostedt 	start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
10895f1a1907SSteven Rostedt 	pte = start_pte;
10906606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
10911da177e4SLinus Torvalds 	do {
10921da177e4SLinus Torvalds 		pte_t ptent = *pte;
109351c6f666SRobin Holt 		if (pte_none(ptent)) {
10941da177e4SLinus Torvalds 			continue;
109551c6f666SRobin Holt 		}
109651c6f666SRobin Holt 
10976f5e6b9eSHugh Dickins 		if (pte_present(ptent)) {
10986f5e6b9eSHugh Dickins 			struct page *page;
10996f5e6b9eSHugh Dickins 
11006aab341eSLinus Torvalds 			page = vm_normal_page(vma, addr, ptent);
11011da177e4SLinus Torvalds 			if (unlikely(details) && page) {
11021da177e4SLinus Torvalds 				/*
11031da177e4SLinus Torvalds 				 * unmap_shared_mapping_pages() wants to
11041da177e4SLinus Torvalds 				 * invalidate cache without truncating:
11051da177e4SLinus Torvalds 				 * unmap shared but keep private pages.
11061da177e4SLinus Torvalds 				 */
11071da177e4SLinus Torvalds 				if (details->check_mapping &&
11081da177e4SLinus Torvalds 				    details->check_mapping != page->mapping)
11091da177e4SLinus Torvalds 					continue;
11101da177e4SLinus Torvalds 			}
1111b5810039SNick Piggin 			ptent = ptep_get_and_clear_full(mm, addr, pte,
1112a600388dSZachary Amsden 							tlb->fullmm);
11131da177e4SLinus Torvalds 			tlb_remove_tlb_entry(tlb, pte, addr);
11141da177e4SLinus Torvalds 			if (unlikely(!page))
11151da177e4SLinus Torvalds 				continue;
11161da177e4SLinus Torvalds 			if (PageAnon(page))
1117d559db08SKAMEZAWA Hiroyuki 				rss[MM_ANONPAGES]--;
11186237bcd9SHugh Dickins 			else {
11191cf35d47SLinus Torvalds 				if (pte_dirty(ptent)) {
11201cf35d47SLinus Torvalds 					force_flush = 1;
11216237bcd9SHugh Dickins 					set_page_dirty(page);
11221cf35d47SLinus Torvalds 				}
11234917e5d0SJohannes Weiner 				if (pte_young(ptent) &&
112464363aadSJoe Perches 				    likely(!(vma->vm_flags & VM_SEQ_READ)))
1125bf3f3bc5SNick Piggin 					mark_page_accessed(page);
1126d559db08SKAMEZAWA Hiroyuki 				rss[MM_FILEPAGES]--;
11276237bcd9SHugh Dickins 			}
1128edc315fdSHugh Dickins 			page_remove_rmap(page);
11293dc14741SHugh Dickins 			if (unlikely(page_mapcount(page) < 0))
11303dc14741SHugh Dickins 				print_bad_pte(vma, addr, ptent, page);
11311cf35d47SLinus Torvalds 			if (unlikely(!__tlb_remove_page(tlb, page))) {
11321cf35d47SLinus Torvalds 				force_flush = 1;
1133ce9ec37bSWill Deacon 				addr += PAGE_SIZE;
1134d16dfc55SPeter Zijlstra 				break;
11351cf35d47SLinus Torvalds 			}
11361da177e4SLinus Torvalds 			continue;
11371da177e4SLinus Torvalds 		}
11388a5f14a2SKirill A. Shutemov 		/* If details->check_mapping, we leave swap entries. */
11391da177e4SLinus Torvalds 		if (unlikely(details))
11401da177e4SLinus Torvalds 			continue;
1141b084d435SKAMEZAWA Hiroyuki 
11428a5f14a2SKirill A. Shutemov 		entry = pte_to_swp_entry(ptent);
1143b084d435SKAMEZAWA Hiroyuki 		if (!non_swap_entry(entry))
1144b084d435SKAMEZAWA Hiroyuki 			rss[MM_SWAPENTS]--;
11459f9f1acdSKonstantin Khlebnikov 		else if (is_migration_entry(entry)) {
11469f9f1acdSKonstantin Khlebnikov 			struct page *page;
11479f9f1acdSKonstantin Khlebnikov 
11489f9f1acdSKonstantin Khlebnikov 			page = migration_entry_to_page(entry);
11499f9f1acdSKonstantin Khlebnikov 
11509f9f1acdSKonstantin Khlebnikov 			if (PageAnon(page))
11519f9f1acdSKonstantin Khlebnikov 				rss[MM_ANONPAGES]--;
11529f9f1acdSKonstantin Khlebnikov 			else
11539f9f1acdSKonstantin Khlebnikov 				rss[MM_FILEPAGES]--;
11549f9f1acdSKonstantin Khlebnikov 		}
1155b084d435SKAMEZAWA Hiroyuki 		if (unlikely(!free_swap_and_cache(entry)))
11562509ef26SHugh Dickins 			print_bad_pte(vma, addr, ptent, NULL);
11579888a1caSZachary Amsden 		pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
115897a89413SPeter Zijlstra 	} while (pte++, addr += PAGE_SIZE, addr != end);
1159ae859762SHugh Dickins 
1160d559db08SKAMEZAWA Hiroyuki 	add_mm_rss_vec(mm, rss);
11616606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
116251c6f666SRobin Holt 
11631cf35d47SLinus Torvalds 	/* Do the actual TLB flush before dropping ptl */
1164fb7332a9SWill Deacon 	if (force_flush)
11651cf35d47SLinus Torvalds 		tlb_flush_mmu_tlbonly(tlb);
11661cf35d47SLinus Torvalds 	pte_unmap_unlock(start_pte, ptl);
11671cf35d47SLinus Torvalds 
11681cf35d47SLinus Torvalds 	/*
11691cf35d47SLinus Torvalds 	 * If we forced a TLB flush (either due to running out of
11701cf35d47SLinus Torvalds 	 * batch buffers or because we needed to flush dirty TLB
11711cf35d47SLinus Torvalds 	 * entries before releasing the ptl), free the batched
11721cf35d47SLinus Torvalds 	 * memory too. Restart if we didn't do everything.
11731cf35d47SLinus Torvalds 	 */
11741cf35d47SLinus Torvalds 	if (force_flush) {
11751cf35d47SLinus Torvalds 		force_flush = 0;
11761cf35d47SLinus Torvalds 		tlb_flush_mmu_free(tlb);
11772b047252SLinus Torvalds 
11782b047252SLinus Torvalds 		if (addr != end)
1179d16dfc55SPeter Zijlstra 			goto again;
1180d16dfc55SPeter Zijlstra 	}
1181d16dfc55SPeter Zijlstra 
118251c6f666SRobin Holt 	return addr;
11831da177e4SLinus Torvalds }
11841da177e4SLinus Torvalds 
118551c6f666SRobin Holt static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
1186b5810039SNick Piggin 				struct vm_area_struct *vma, pud_t *pud,
11871da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
118897a89413SPeter Zijlstra 				struct zap_details *details)
11891da177e4SLinus Torvalds {
11901da177e4SLinus Torvalds 	pmd_t *pmd;
11911da177e4SLinus Torvalds 	unsigned long next;
11921da177e4SLinus Torvalds 
11931da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
11941da177e4SLinus Torvalds 	do {
11951da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
119671e3aac0SAndrea Arcangeli 		if (pmd_trans_huge(*pmd)) {
119714d1a55cSAndrea Arcangeli 			if (next - addr != HPAGE_PMD_SIZE) {
1198e0897d75SDavid Rientjes #ifdef CONFIG_DEBUG_VM
1199e0897d75SDavid Rientjes 				if (!rwsem_is_locked(&tlb->mm->mmap_sem)) {
1200e0897d75SDavid 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",
1201e0897d75SDavid Rientjes 						__func__, addr, end,
1202e0897d75SDavid Rientjes 						vma->vm_start,
1203e0897d75SDavid Rientjes 						vma->vm_end);
1204e0897d75SDavid Rientjes 					BUG();
1205e0897d75SDavid Rientjes 				}
1206e0897d75SDavid Rientjes #endif
1207e180377fSKirill A. Shutemov 				split_huge_page_pmd(vma, addr, pmd);
1208f21760b1SShaohua Li 			} else if (zap_huge_pmd(tlb, vma, pmd, addr))
12091a5a9906SAndrea Arcangeli 				goto next;
121071e3aac0SAndrea Arcangeli 			/* fall through */
121171e3aac0SAndrea Arcangeli 		}
12121a5a9906SAndrea Arcangeli 		/*
12131a5a9906SAndrea Arcangeli 		 * Here there can be other concurrent MADV_DONTNEED or
12141a5a9906SAndrea Arcangeli 		 * trans huge page faults running, and if the pmd is
12151a5a9906SAndrea Arcangeli 		 * none or trans huge it can change under us. This is
12161a5a9906SAndrea Arcangeli 		 * because MADV_DONTNEED holds the mmap_sem in read
12171a5a9906SAndrea Arcangeli 		 * mode.
12181a5a9906SAndrea Arcangeli 		 */
12191a5a9906SAndrea Arcangeli 		if (pmd_none_or_trans_huge_or_clear_bad(pmd))
12201a5a9906SAndrea Arcangeli 			goto next;
122197a89413SPeter Zijlstra 		next = zap_pte_range(tlb, vma, pmd, addr, next, details);
12221a5a9906SAndrea Arcangeli next:
122397a89413SPeter Zijlstra 		cond_resched();
122497a89413SPeter Zijlstra 	} while (pmd++, addr = next, addr != end);
122551c6f666SRobin Holt 
122651c6f666SRobin Holt 	return addr;
12271da177e4SLinus Torvalds }
12281da177e4SLinus Torvalds 
122951c6f666SRobin Holt static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
1230b5810039SNick Piggin 				struct vm_area_struct *vma, pgd_t *pgd,
12311da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
123297a89413SPeter Zijlstra 				struct zap_details *details)
12331da177e4SLinus Torvalds {
12341da177e4SLinus Torvalds 	pud_t *pud;
12351da177e4SLinus Torvalds 	unsigned long next;
12361da177e4SLinus Torvalds 
12371da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
12381da177e4SLinus Torvalds 	do {
12391da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
124097a89413SPeter Zijlstra 		if (pud_none_or_clear_bad(pud))
12411da177e4SLinus Torvalds 			continue;
124297a89413SPeter Zijlstra 		next = zap_pmd_range(tlb, vma, pud, addr, next, details);
124397a89413SPeter Zijlstra 	} while (pud++, addr = next, addr != end);
124451c6f666SRobin Holt 
124551c6f666SRobin Holt 	return addr;
12461da177e4SLinus Torvalds }
12471da177e4SLinus Torvalds 
1248038c7aa1SAl Viro static void unmap_page_range(struct mmu_gather *tlb,
124951c6f666SRobin Holt 			     struct vm_area_struct *vma,
12501da177e4SLinus Torvalds 			     unsigned long addr, unsigned long end,
125197a89413SPeter Zijlstra 			     struct zap_details *details)
12521da177e4SLinus Torvalds {
12531da177e4SLinus Torvalds 	pgd_t *pgd;
12541da177e4SLinus Torvalds 	unsigned long next;
12551da177e4SLinus Torvalds 
12568a5f14a2SKirill A. Shutemov 	if (details && !details->check_mapping)
12571da177e4SLinus Torvalds 		details = NULL;
12581da177e4SLinus Torvalds 
12591da177e4SLinus Torvalds 	BUG_ON(addr >= end);
12601da177e4SLinus Torvalds 	tlb_start_vma(tlb, vma);
12611da177e4SLinus Torvalds 	pgd = pgd_offset(vma->vm_mm, addr);
12621da177e4SLinus Torvalds 	do {
12631da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
126497a89413SPeter Zijlstra 		if (pgd_none_or_clear_bad(pgd))
12651da177e4SLinus Torvalds 			continue;
126697a89413SPeter Zijlstra 		next = zap_pud_range(tlb, vma, pgd, addr, next, details);
126797a89413SPeter Zijlstra 	} while (pgd++, addr = next, addr != end);
12681da177e4SLinus Torvalds 	tlb_end_vma(tlb, vma);
12691da177e4SLinus Torvalds }
12701da177e4SLinus Torvalds 
1271f5cc4eefSAl Viro 
1272f5cc4eefSAl Viro static void unmap_single_vma(struct mmu_gather *tlb,
12731da177e4SLinus Torvalds 		struct vm_area_struct *vma, unsigned long start_addr,
12744f74d2c8SLinus Torvalds 		unsigned long end_addr,
12751da177e4SLinus Torvalds 		struct zap_details *details)
12761da177e4SLinus Torvalds {
1277f5cc4eefSAl Viro 	unsigned long start = max(vma->vm_start, start_addr);
12781da177e4SLinus Torvalds 	unsigned long end;
12791da177e4SLinus Torvalds 
12801da177e4SLinus Torvalds 	if (start >= vma->vm_end)
1281f5cc4eefSAl Viro 		return;
12821da177e4SLinus Torvalds 	end = min(vma->vm_end, end_addr);
12831da177e4SLinus Torvalds 	if (end <= vma->vm_start)
1284f5cc4eefSAl Viro 		return;
12851da177e4SLinus Torvalds 
1286cbc91f71SSrikar Dronamraju 	if (vma->vm_file)
1287cbc91f71SSrikar Dronamraju 		uprobe_munmap(vma, start, end);
1288cbc91f71SSrikar Dronamraju 
1289b3b9c293SKonstantin Khlebnikov 	if (unlikely(vma->vm_flags & VM_PFNMAP))
12905180da41SSuresh Siddha 		untrack_pfn(vma, 0, 0);
12912ab64037Svenkatesh.pallipadi@intel.com 
12928b2a1238SAl Viro 	if (start != end) {
129351c6f666SRobin Holt 		if (unlikely(is_vm_hugetlb_page(vma))) {
1294a137e1ccSAndi Kleen 			/*
1295a137e1ccSAndi Kleen 			 * It is undesirable to test vma->vm_file as it
1296a137e1ccSAndi Kleen 			 * should be non-null for valid hugetlb area.
1297a137e1ccSAndi Kleen 			 * However, vm_file will be NULL in the error
12987aa6b4adSDavidlohr Bueso 			 * cleanup path of mmap_region. When
1299a137e1ccSAndi Kleen 			 * hugetlbfs ->mmap method fails,
13007aa6b4adSDavidlohr Bueso 			 * mmap_region() nullifies vma->vm_file
1301a137e1ccSAndi Kleen 			 * before calling this function to clean up.
1302a137e1ccSAndi Kleen 			 * Since no pte has actually been setup, it is
1303a137e1ccSAndi Kleen 			 * safe to do nothing in this case.
1304a137e1ccSAndi Kleen 			 */
130524669e58SAneesh Kumar K.V 			if (vma->vm_file) {
130683cde9e8SDavidlohr Bueso 				i_mmap_lock_write(vma->vm_file->f_mapping);
1307d833352aSMel Gorman 				__unmap_hugepage_range_final(tlb, vma, start, end, NULL);
130883cde9e8SDavidlohr Bueso 				i_mmap_unlock_write(vma->vm_file->f_mapping);
130924669e58SAneesh Kumar K.V 			}
131051c6f666SRobin Holt 		} else
1311038c7aa1SAl Viro 			unmap_page_range(tlb, vma, start, end, details);
131297a89413SPeter Zijlstra 	}
131351c6f666SRobin Holt }
13141da177e4SLinus Torvalds 
1315f5cc4eefSAl Viro /**
1316f5cc4eefSAl Viro  * unmap_vmas - unmap a range of memory covered by a list of vma's
1317f5cc4eefSAl Viro  * @tlb: address of the caller's struct mmu_gather
1318f5cc4eefSAl Viro  * @vma: the starting vma
1319f5cc4eefSAl Viro  * @start_addr: virtual address at which to start unmapping
1320f5cc4eefSAl Viro  * @end_addr: virtual address at which to end unmapping
1321f5cc4eefSAl Viro  *
1322f5cc4eefSAl Viro  * Unmap all pages in the vma list.
1323f5cc4eefSAl Viro  *
1324f5cc4eefSAl Viro  * Only addresses between `start' and `end' will be unmapped.
1325f5cc4eefSAl Viro  *
1326f5cc4eefSAl Viro  * The VMA list must be sorted in ascending virtual address order.
1327f5cc4eefSAl Viro  *
1328f5cc4eefSAl Viro  * unmap_vmas() assumes that the caller will flush the whole unmapped address
1329f5cc4eefSAl Viro  * range after unmap_vmas() returns.  So the only responsibility here is to
1330f5cc4eefSAl Viro  * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
1331f5cc4eefSAl Viro  * drops the lock and schedules.
1332f5cc4eefSAl Viro  */
1333f5cc4eefSAl Viro void unmap_vmas(struct mmu_gather *tlb,
1334f5cc4eefSAl Viro 		struct vm_area_struct *vma, unsigned long start_addr,
13354f74d2c8SLinus Torvalds 		unsigned long end_addr)
1336f5cc4eefSAl Viro {
1337f5cc4eefSAl Viro 	struct mm_struct *mm = vma->vm_mm;
1338f5cc4eefSAl Viro 
1339f5cc4eefSAl Viro 	mmu_notifier_invalidate_range_start(mm, start_addr, end_addr);
1340f5cc4eefSAl Viro 	for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next)
13414f74d2c8SLinus Torvalds 		unmap_single_vma(tlb, vma, start_addr, end_addr, NULL);
1342cddb8a5cSAndrea Arcangeli 	mmu_notifier_invalidate_range_end(mm, start_addr, end_addr);
13431da177e4SLinus Torvalds }
13441da177e4SLinus Torvalds 
13451da177e4SLinus Torvalds /**
13461da177e4SLinus Torvalds  * zap_page_range - remove user pages in a given range
13471da177e4SLinus Torvalds  * @vma: vm_area_struct holding the applicable pages
1348eb4546bbSRandy Dunlap  * @start: starting address of pages to zap
13491da177e4SLinus Torvalds  * @size: number of bytes to zap
13508a5f14a2SKirill A. Shutemov  * @details: details of shared cache invalidation
1351f5cc4eefSAl Viro  *
1352f5cc4eefSAl Viro  * Caller must protect the VMA list
13531da177e4SLinus Torvalds  */
13547e027b14SLinus Torvalds void zap_page_range(struct vm_area_struct *vma, unsigned long start,
13551da177e4SLinus Torvalds 		unsigned long size, struct zap_details *details)
13561da177e4SLinus Torvalds {
13571da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
1358d16dfc55SPeter Zijlstra 	struct mmu_gather tlb;
13597e027b14SLinus Torvalds 	unsigned long end = start + size;
13601da177e4SLinus Torvalds 
13611da177e4SLinus Torvalds 	lru_add_drain();
13622b047252SLinus Torvalds 	tlb_gather_mmu(&tlb, mm, start, end);
1363365e9c87SHugh Dickins 	update_hiwater_rss(mm);
13647e027b14SLinus Torvalds 	mmu_notifier_invalidate_range_start(mm, start, end);
13657e027b14SLinus Torvalds 	for ( ; vma && vma->vm_start < end; vma = vma->vm_next)
13664f74d2c8SLinus Torvalds 		unmap_single_vma(&tlb, vma, start, end, details);
13677e027b14SLinus Torvalds 	mmu_notifier_invalidate_range_end(mm, start, end);
13687e027b14SLinus Torvalds 	tlb_finish_mmu(&tlb, start, end);
13691da177e4SLinus Torvalds }
13701da177e4SLinus Torvalds 
1371c627f9ccSJack Steiner /**
1372f5cc4eefSAl Viro  * zap_page_range_single - remove user pages in a given range
1373f5cc4eefSAl Viro  * @vma: vm_area_struct holding the applicable pages
1374f5cc4eefSAl Viro  * @address: starting address of pages to zap
1375f5cc4eefSAl Viro  * @size: number of bytes to zap
13768a5f14a2SKirill A. Shutemov  * @details: details of shared cache invalidation
1377f5cc4eefSAl Viro  *
1378f5cc4eefSAl Viro  * The range must fit into one VMA.
1379f5cc4eefSAl Viro  */
1380f5cc4eefSAl Viro static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,
1381f5cc4eefSAl Viro 		unsigned long size, struct zap_details *details)
1382f5cc4eefSAl Viro {
1383f5cc4eefSAl Viro 	struct mm_struct *mm = vma->vm_mm;
1384f5cc4eefSAl Viro 	struct mmu_gather tlb;
1385f5cc4eefSAl Viro 	unsigned long end = address + size;
1386f5cc4eefSAl Viro 
1387f5cc4eefSAl Viro 	lru_add_drain();
13882b047252SLinus Torvalds 	tlb_gather_mmu(&tlb, mm, address, end);
1389f5cc4eefSAl Viro 	update_hiwater_rss(mm);
1390f5cc4eefSAl Viro 	mmu_notifier_invalidate_range_start(mm, address, end);
13914f74d2c8SLinus Torvalds 	unmap_single_vma(&tlb, vma, address, end, details);
1392f5cc4eefSAl Viro 	mmu_notifier_invalidate_range_end(mm, address, end);
1393f5cc4eefSAl Viro 	tlb_finish_mmu(&tlb, address, end);
13941da177e4SLinus Torvalds }
13951da177e4SLinus Torvalds 
1396c627f9ccSJack Steiner /**
1397c627f9ccSJack Steiner  * zap_vma_ptes - remove ptes mapping the vma
1398c627f9ccSJack Steiner  * @vma: vm_area_struct holding ptes to be zapped
1399c627f9ccSJack Steiner  * @address: starting address of pages to zap
1400c627f9ccSJack Steiner  * @size: number of bytes to zap
1401c627f9ccSJack Steiner  *
1402c627f9ccSJack Steiner  * This function only unmaps ptes assigned to VM_PFNMAP vmas.
1403c627f9ccSJack Steiner  *
1404c627f9ccSJack Steiner  * The entire address range must be fully contained within the vma.
1405c627f9ccSJack Steiner  *
1406c627f9ccSJack Steiner  * Returns 0 if successful.
1407c627f9ccSJack Steiner  */
1408c627f9ccSJack Steiner int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
1409c627f9ccSJack Steiner 		unsigned long size)
1410c627f9ccSJack Steiner {
1411c627f9ccSJack Steiner 	if (address < vma->vm_start || address + size > vma->vm_end ||
1412c627f9ccSJack Steiner 	    		!(vma->vm_flags & VM_PFNMAP))
1413c627f9ccSJack Steiner 		return -1;
1414f5cc4eefSAl Viro 	zap_page_range_single(vma, address, size, NULL);
1415c627f9ccSJack Steiner 	return 0;
1416c627f9ccSJack Steiner }
1417c627f9ccSJack Steiner EXPORT_SYMBOL_GPL(zap_vma_ptes);
1418c627f9ccSJack Steiner 
141925ca1d6cSNamhyung Kim pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
1420920c7a5dSHarvey Harrison 			spinlock_t **ptl)
1421c9cfcddfSLinus Torvalds {
1422c9cfcddfSLinus Torvalds 	pgd_t * pgd = pgd_offset(mm, addr);
1423c9cfcddfSLinus Torvalds 	pud_t * pud = pud_alloc(mm, pgd, addr);
1424c9cfcddfSLinus Torvalds 	if (pud) {
142549c91fb0STrond Myklebust 		pmd_t * pmd = pmd_alloc(mm, pud, addr);
1426f66055abSAndrea Arcangeli 		if (pmd) {
1427f66055abSAndrea Arcangeli 			VM_BUG_ON(pmd_trans_huge(*pmd));
1428c9cfcddfSLinus Torvalds 			return pte_alloc_map_lock(mm, pmd, addr, ptl);
1429c9cfcddfSLinus Torvalds 		}
1430f66055abSAndrea Arcangeli 	}
1431c9cfcddfSLinus Torvalds 	return NULL;
1432c9cfcddfSLinus Torvalds }
1433c9cfcddfSLinus Torvalds 
14341da177e4SLinus Torvalds /*
1435238f58d8SLinus Torvalds  * This is the old fallback for page remapping.
1436238f58d8SLinus Torvalds  *
1437238f58d8SLinus Torvalds  * For historical reasons, it only allows reserved pages. Only
1438238f58d8SLinus Torvalds  * old drivers should use this, and they needed to mark their
1439238f58d8SLinus Torvalds  * pages reserved for the old functions anyway.
1440238f58d8SLinus Torvalds  */
1441423bad60SNick Piggin static int insert_page(struct vm_area_struct *vma, unsigned long addr,
1442423bad60SNick Piggin 			struct page *page, pgprot_t prot)
1443238f58d8SLinus Torvalds {
1444423bad60SNick Piggin 	struct mm_struct *mm = vma->vm_mm;
1445238f58d8SLinus Torvalds 	int retval;
1446238f58d8SLinus Torvalds 	pte_t *pte;
1447238f58d8SLinus Torvalds 	spinlock_t *ptl;
1448238f58d8SLinus Torvalds 
1449238f58d8SLinus Torvalds 	retval = -EINVAL;
1450a145dd41SLinus Torvalds 	if (PageAnon(page))
14515b4e655eSKAMEZAWA Hiroyuki 		goto out;
1452238f58d8SLinus Torvalds 	retval = -ENOMEM;
1453238f58d8SLinus Torvalds 	flush_dcache_page(page);
1454c9cfcddfSLinus Torvalds 	pte = get_locked_pte(mm, addr, &ptl);
1455238f58d8SLinus Torvalds 	if (!pte)
14565b4e655eSKAMEZAWA Hiroyuki 		goto out;
1457238f58d8SLinus Torvalds 	retval = -EBUSY;
1458238f58d8SLinus Torvalds 	if (!pte_none(*pte))
1459238f58d8SLinus Torvalds 		goto out_unlock;
1460238f58d8SLinus Torvalds 
1461238f58d8SLinus Torvalds 	/* Ok, finally just insert the thing.. */
1462238f58d8SLinus Torvalds 	get_page(page);
146334e55232SKAMEZAWA Hiroyuki 	inc_mm_counter_fast(mm, MM_FILEPAGES);
1464238f58d8SLinus Torvalds 	page_add_file_rmap(page);
1465238f58d8SLinus Torvalds 	set_pte_at(mm, addr, pte, mk_pte(page, prot));
1466238f58d8SLinus Torvalds 
1467238f58d8SLinus Torvalds 	retval = 0;
14688a9f3ccdSBalbir Singh 	pte_unmap_unlock(pte, ptl);
14698a9f3ccdSBalbir Singh 	return retval;
1470238f58d8SLinus Torvalds out_unlock:
1471238f58d8SLinus Torvalds 	pte_unmap_unlock(pte, ptl);
1472238f58d8SLinus Torvalds out:
1473238f58d8SLinus Torvalds 	return retval;
1474238f58d8SLinus Torvalds }
1475238f58d8SLinus Torvalds 
1476bfa5bf6dSRolf Eike Beer /**
1477bfa5bf6dSRolf Eike Beer  * vm_insert_page - insert single page into user vma
1478bfa5bf6dSRolf Eike Beer  * @vma: user vma to map to
1479bfa5bf6dSRolf Eike Beer  * @addr: target user address of this page
1480bfa5bf6dSRolf Eike Beer  * @page: source kernel page
1481bfa5bf6dSRolf Eike Beer  *
1482a145dd41SLinus Torvalds  * This allows drivers to insert individual pages they've allocated
1483a145dd41SLinus Torvalds  * into a user vma.
1484a145dd41SLinus Torvalds  *
1485a145dd41SLinus Torvalds  * The page has to be a nice clean _individual_ kernel allocation.
1486a145dd41SLinus Torvalds  * If you allocate a compound page, you need to have marked it as
1487a145dd41SLinus Torvalds  * such (__GFP_COMP), or manually just split the page up yourself
14888dfcc9baSNick Piggin  * (see split_page()).
1489a145dd41SLinus Torvalds  *
1490a145dd41SLinus Torvalds  * NOTE! Traditionally this was done with "remap_pfn_range()" which
1491a145dd41SLinus Torvalds  * took an arbitrary page protection parameter. This doesn't allow
1492a145dd41SLinus Torvalds  * that. Your vma protection will have to be set up correctly, which
1493a145dd41SLinus Torvalds  * means that if you want a shared writable mapping, you'd better
1494a145dd41SLinus Torvalds  * ask for a shared writable mapping!
1495a145dd41SLinus Torvalds  *
1496a145dd41SLinus Torvalds  * The page does not need to be reserved.
14974b6e1e37SKonstantin Khlebnikov  *
14984b6e1e37SKonstantin Khlebnikov  * Usually this function is called from f_op->mmap() handler
14994b6e1e37SKonstantin Khlebnikov  * under mm->mmap_sem write-lock, so it can change vma->vm_flags.
15004b6e1e37SKonstantin Khlebnikov  * Caller must set VM_MIXEDMAP on vma if it wants to call this
15014b6e1e37SKonstantin Khlebnikov  * function from other places, for example from page-fault handler.
1502a145dd41SLinus Torvalds  */
1503423bad60SNick Piggin int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
1504423bad60SNick Piggin 			struct page *page)
1505a145dd41SLinus Torvalds {
1506a145dd41SLinus Torvalds 	if (addr < vma->vm_start || addr >= vma->vm_end)
1507a145dd41SLinus Torvalds 		return -EFAULT;
1508a145dd41SLinus Torvalds 	if (!page_count(page))
1509a145dd41SLinus Torvalds 		return -EINVAL;
15104b6e1e37SKonstantin Khlebnikov 	if (!(vma->vm_flags & VM_MIXEDMAP)) {
15114b6e1e37SKonstantin Khlebnikov 		BUG_ON(down_read_trylock(&vma->vm_mm->mmap_sem));
15124b6e1e37SKonstantin Khlebnikov 		BUG_ON(vma->vm_flags & VM_PFNMAP);
15134b6e1e37SKonstantin Khlebnikov 		vma->vm_flags |= VM_MIXEDMAP;
15144b6e1e37SKonstantin Khlebnikov 	}
1515423bad60SNick Piggin 	return insert_page(vma, addr, page, vma->vm_page_prot);
1516a145dd41SLinus Torvalds }
1517e3c3374fSLinus Torvalds EXPORT_SYMBOL(vm_insert_page);
1518a145dd41SLinus Torvalds 
1519423bad60SNick Piggin static int insert_pfn(struct vm_area_struct *vma, unsigned long addr,
1520423bad60SNick Piggin 			unsigned long pfn, pgprot_t prot)
1521423bad60SNick Piggin {
1522423bad60SNick Piggin 	struct mm_struct *mm = vma->vm_mm;
1523423bad60SNick Piggin 	int retval;
1524423bad60SNick Piggin 	pte_t *pte, entry;
1525423bad60SNick Piggin 	spinlock_t *ptl;
1526423bad60SNick Piggin 
1527423bad60SNick Piggin 	retval = -ENOMEM;
1528423bad60SNick Piggin 	pte = get_locked_pte(mm, addr, &ptl);
1529423bad60SNick Piggin 	if (!pte)
1530423bad60SNick Piggin 		goto out;
1531423bad60SNick Piggin 	retval = -EBUSY;
1532423bad60SNick Piggin 	if (!pte_none(*pte))
1533423bad60SNick Piggin 		goto out_unlock;
1534423bad60SNick Piggin 
1535423bad60SNick Piggin 	/* Ok, finally just insert the thing.. */
1536423bad60SNick Piggin 	entry = pte_mkspecial(pfn_pte(pfn, prot));
1537423bad60SNick Piggin 	set_pte_at(mm, addr, pte, entry);
15384b3073e1SRussell King 	update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */
1539423bad60SNick Piggin 
1540423bad60SNick Piggin 	retval = 0;
1541423bad60SNick Piggin out_unlock:
1542423bad60SNick Piggin 	pte_unmap_unlock(pte, ptl);
1543423bad60SNick Piggin out:
1544423bad60SNick Piggin 	return retval;
1545423bad60SNick Piggin }
1546423bad60SNick Piggin 
1547e0dc0d8fSNick Piggin /**
1548e0dc0d8fSNick Piggin  * vm_insert_pfn - insert single pfn into user vma
1549e0dc0d8fSNick Piggin  * @vma: user vma to map to
1550e0dc0d8fSNick Piggin  * @addr: target user address of this page
1551e0dc0d8fSNick Piggin  * @pfn: source kernel pfn
1552e0dc0d8fSNick Piggin  *
1553c462f179SRobert P. J. Day  * Similar to vm_insert_page, this allows drivers to insert individual pages
1554e0dc0d8fSNick Piggin  * they've allocated into a user vma. Same comments apply.
1555e0dc0d8fSNick Piggin  *
1556e0dc0d8fSNick Piggin  * This function should only be called from a vm_ops->fault handler, and
1557e0dc0d8fSNick Piggin  * in that case the handler should return NULL.
15580d71d10aSNick Piggin  *
15590d71d10aSNick Piggin  * vma cannot be a COW mapping.
15600d71d10aSNick Piggin  *
15610d71d10aSNick Piggin  * As this is called only for pages that do not currently exist, we
15620d71d10aSNick Piggin  * do not need to flush old virtual caches or the TLB.
1563e0dc0d8fSNick Piggin  */
1564e0dc0d8fSNick Piggin int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
1565e0dc0d8fSNick Piggin 			unsigned long pfn)
1566e0dc0d8fSNick Piggin {
15672ab64037Svenkatesh.pallipadi@intel.com 	int ret;
1568e4b866edSvenkatesh.pallipadi@intel.com 	pgprot_t pgprot = vma->vm_page_prot;
15697e675137SNick Piggin 	/*
15707e675137SNick Piggin 	 * Technically, architectures with pte_special can avoid all these
15717e675137SNick Piggin 	 * restrictions (same for remap_pfn_range).  However we would like
15727e675137SNick Piggin 	 * consistency in testing and feature parity among all, so we should
15737e675137SNick Piggin 	 * try to keep these invariants in place for everybody.
15747e675137SNick Piggin 	 */
1575b379d790SJared Hulbert 	BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
1576b379d790SJared Hulbert 	BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
1577b379d790SJared Hulbert 						(VM_PFNMAP|VM_MIXEDMAP));
1578b379d790SJared Hulbert 	BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
1579b379d790SJared Hulbert 	BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
1580e0dc0d8fSNick Piggin 
1581423bad60SNick Piggin 	if (addr < vma->vm_start || addr >= vma->vm_end)
1582423bad60SNick Piggin 		return -EFAULT;
15835180da41SSuresh Siddha 	if (track_pfn_insert(vma, &pgprot, pfn))
15842ab64037Svenkatesh.pallipadi@intel.com 		return -EINVAL;
15852ab64037Svenkatesh.pallipadi@intel.com 
1586e4b866edSvenkatesh.pallipadi@intel.com 	ret = insert_pfn(vma, addr, pfn, pgprot);
15872ab64037Svenkatesh.pallipadi@intel.com 
15882ab64037Svenkatesh.pallipadi@intel.com 	return ret;
1589e0dc0d8fSNick Piggin }
1590e0dc0d8fSNick Piggin EXPORT_SYMBOL(vm_insert_pfn);
1591e0dc0d8fSNick Piggin 
1592423bad60SNick Piggin int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
1593423bad60SNick Piggin 			unsigned long pfn)
1594423bad60SNick Piggin {
1595423bad60SNick Piggin 	BUG_ON(!(vma->vm_flags & VM_MIXEDMAP));
1596423bad60SNick Piggin 
1597423bad60SNick Piggin 	if (addr < vma->vm_start || addr >= vma->vm_end)
1598423bad60SNick Piggin 		return -EFAULT;
1599423bad60SNick Piggin 
1600423bad60SNick Piggin 	/*
1601423bad60SNick Piggin 	 * If we don't have pte special, then we have to use the pfn_valid()
1602423bad60SNick Piggin 	 * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must*
1603423bad60SNick Piggin 	 * refcount the page if pfn_valid is true (hence insert_page rather
160462eede62SHugh Dickins 	 * than insert_pfn).  If a zero_pfn were inserted into a VM_MIXEDMAP
160562eede62SHugh Dickins 	 * without pte special, it would there be refcounted as a normal page.
1606423bad60SNick Piggin 	 */
1607423bad60SNick Piggin 	if (!HAVE_PTE_SPECIAL && pfn_valid(pfn)) {
1608423bad60SNick Piggin 		struct page *page;
1609423bad60SNick Piggin 
1610423bad60SNick Piggin 		page = pfn_to_page(pfn);
1611423bad60SNick Piggin 		return insert_page(vma, addr, page, vma->vm_page_prot);
1612423bad60SNick Piggin 	}
1613423bad60SNick Piggin 	return insert_pfn(vma, addr, pfn, vma->vm_page_prot);
1614423bad60SNick Piggin }
1615423bad60SNick Piggin EXPORT_SYMBOL(vm_insert_mixed);
1616423bad60SNick Piggin 
1617a145dd41SLinus Torvalds /*
16181da177e4SLinus Torvalds  * maps a range of physical memory into the requested pages. the old
16191da177e4SLinus Torvalds  * mappings are removed. any references to nonexistent pages results
16201da177e4SLinus Torvalds  * in null mappings (currently treated as "copy-on-access")
16211da177e4SLinus Torvalds  */
16221da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
16231da177e4SLinus Torvalds 			unsigned long addr, unsigned long end,
16241da177e4SLinus Torvalds 			unsigned long pfn, pgprot_t prot)
16251da177e4SLinus Torvalds {
16261da177e4SLinus Torvalds 	pte_t *pte;
1627c74df32cSHugh Dickins 	spinlock_t *ptl;
16281da177e4SLinus Torvalds 
1629c74df32cSHugh Dickins 	pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
16301da177e4SLinus Torvalds 	if (!pte)
16311da177e4SLinus Torvalds 		return -ENOMEM;
16326606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
16331da177e4SLinus Torvalds 	do {
16341da177e4SLinus Torvalds 		BUG_ON(!pte_none(*pte));
16357e675137SNick Piggin 		set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot)));
16361da177e4SLinus Torvalds 		pfn++;
16371da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
16386606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
1639c74df32cSHugh Dickins 	pte_unmap_unlock(pte - 1, ptl);
16401da177e4SLinus Torvalds 	return 0;
16411da177e4SLinus Torvalds }
16421da177e4SLinus Torvalds 
16431da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
16441da177e4SLinus Torvalds 			unsigned long addr, unsigned long end,
16451da177e4SLinus Torvalds 			unsigned long pfn, pgprot_t prot)
16461da177e4SLinus Torvalds {
16471da177e4SLinus Torvalds 	pmd_t *pmd;
16481da177e4SLinus Torvalds 	unsigned long next;
16491da177e4SLinus Torvalds 
16501da177e4SLinus Torvalds 	pfn -= addr >> PAGE_SHIFT;
16511da177e4SLinus Torvalds 	pmd = pmd_alloc(mm, pud, addr);
16521da177e4SLinus Torvalds 	if (!pmd)
16531da177e4SLinus Torvalds 		return -ENOMEM;
1654f66055abSAndrea Arcangeli 	VM_BUG_ON(pmd_trans_huge(*pmd));
16551da177e4SLinus Torvalds 	do {
16561da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
16571da177e4SLinus Torvalds 		if (remap_pte_range(mm, pmd, addr, next,
16581da177e4SLinus Torvalds 				pfn + (addr >> PAGE_SHIFT), prot))
16591da177e4SLinus Torvalds 			return -ENOMEM;
16601da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
16611da177e4SLinus Torvalds 	return 0;
16621da177e4SLinus Torvalds }
16631da177e4SLinus Torvalds 
16641da177e4SLinus Torvalds static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd,
16651da177e4SLinus Torvalds 			unsigned long addr, unsigned long end,
16661da177e4SLinus Torvalds 			unsigned long pfn, pgprot_t prot)
16671da177e4SLinus Torvalds {
16681da177e4SLinus Torvalds 	pud_t *pud;
16691da177e4SLinus Torvalds 	unsigned long next;
16701da177e4SLinus Torvalds 
16711da177e4SLinus Torvalds 	pfn -= addr >> PAGE_SHIFT;
16721da177e4SLinus Torvalds 	pud = pud_alloc(mm, pgd, addr);
16731da177e4SLinus Torvalds 	if (!pud)
16741da177e4SLinus Torvalds 		return -ENOMEM;
16751da177e4SLinus Torvalds 	do {
16761da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
16771da177e4SLinus Torvalds 		if (remap_pmd_range(mm, pud, addr, next,
16781da177e4SLinus Torvalds 				pfn + (addr >> PAGE_SHIFT), prot))
16791da177e4SLinus Torvalds 			return -ENOMEM;
16801da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
16811da177e4SLinus Torvalds 	return 0;
16821da177e4SLinus Torvalds }
16831da177e4SLinus Torvalds 
1684bfa5bf6dSRolf Eike Beer /**
1685bfa5bf6dSRolf Eike Beer  * remap_pfn_range - remap kernel memory to userspace
1686bfa5bf6dSRolf Eike Beer  * @vma: user vma to map to
1687bfa5bf6dSRolf Eike Beer  * @addr: target user address to start at
1688bfa5bf6dSRolf Eike Beer  * @pfn: physical address of kernel memory
1689bfa5bf6dSRolf Eike Beer  * @size: size of map area
1690bfa5bf6dSRolf Eike Beer  * @prot: page protection flags for this mapping
1691bfa5bf6dSRolf Eike Beer  *
1692bfa5bf6dSRolf Eike Beer  *  Note: this is only safe if the mm semaphore is held when called.
1693bfa5bf6dSRolf Eike Beer  */
16941da177e4SLinus Torvalds int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
16951da177e4SLinus Torvalds 		    unsigned long pfn, unsigned long size, pgprot_t prot)
16961da177e4SLinus Torvalds {
16971da177e4SLinus Torvalds 	pgd_t *pgd;
16981da177e4SLinus Torvalds 	unsigned long next;
16992d15cab8SHugh Dickins 	unsigned long end = addr + PAGE_ALIGN(size);
17001da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
17011da177e4SLinus Torvalds 	int err;
17021da177e4SLinus Torvalds 
17031da177e4SLinus Torvalds 	/*
17041da177e4SLinus Torvalds 	 * Physically remapped pages are special. Tell the
17051da177e4SLinus Torvalds 	 * rest of the world about it:
17061da177e4SLinus Torvalds 	 *   VM_IO tells people not to look at these pages
17071da177e4SLinus Torvalds 	 *	(accesses can have side effects).
17086aab341eSLinus Torvalds 	 *   VM_PFNMAP tells the core MM that the base pages are just
17096aab341eSLinus Torvalds 	 *	raw PFN mappings, and do not have a "struct page" associated
17106aab341eSLinus Torvalds 	 *	with them.
1711314e51b9SKonstantin Khlebnikov 	 *   VM_DONTEXPAND
1712314e51b9SKonstantin Khlebnikov 	 *      Disable vma merging and expanding with mremap().
1713314e51b9SKonstantin Khlebnikov 	 *   VM_DONTDUMP
1714314e51b9SKonstantin Khlebnikov 	 *      Omit vma from core dump, even when VM_IO turned off.
1715fb155c16SLinus Torvalds 	 *
1716fb155c16SLinus Torvalds 	 * There's a horrible special case to handle copy-on-write
1717fb155c16SLinus Torvalds 	 * behaviour that some programs depend on. We mark the "original"
1718fb155c16SLinus Torvalds 	 * un-COW'ed pages by matching them up with "vma->vm_pgoff".
1719b3b9c293SKonstantin Khlebnikov 	 * See vm_normal_page() for details.
17201da177e4SLinus Torvalds 	 */
1721b3b9c293SKonstantin Khlebnikov 	if (is_cow_mapping(vma->vm_flags)) {
1722b3b9c293SKonstantin Khlebnikov 		if (addr != vma->vm_start || end != vma->vm_end)
1723b3b9c293SKonstantin Khlebnikov 			return -EINVAL;
17246aab341eSLinus Torvalds 		vma->vm_pgoff = pfn;
1725b3b9c293SKonstantin Khlebnikov 	}
1726b3b9c293SKonstantin Khlebnikov 
1727b3b9c293SKonstantin Khlebnikov 	err = track_pfn_remap(vma, &prot, pfn, addr, PAGE_ALIGN(size));
1728b3b9c293SKonstantin Khlebnikov 	if (err)
17293c8bb73aSvenkatesh.pallipadi@intel.com 		return -EINVAL;
1730fb155c16SLinus Torvalds 
1731314e51b9SKonstantin Khlebnikov 	vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
17321da177e4SLinus Torvalds 
17331da177e4SLinus Torvalds 	BUG_ON(addr >= end);
17341da177e4SLinus Torvalds 	pfn -= addr >> PAGE_SHIFT;
17351da177e4SLinus Torvalds 	pgd = pgd_offset(mm, addr);
17361da177e4SLinus Torvalds 	flush_cache_range(vma, addr, end);
17371da177e4SLinus Torvalds 	do {
17381da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
17391da177e4SLinus Torvalds 		err = remap_pud_range(mm, pgd, addr, next,
17401da177e4SLinus Torvalds 				pfn + (addr >> PAGE_SHIFT), prot);
17411da177e4SLinus Torvalds 		if (err)
17421da177e4SLinus Torvalds 			break;
17431da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
17442ab64037Svenkatesh.pallipadi@intel.com 
17452ab64037Svenkatesh.pallipadi@intel.com 	if (err)
17465180da41SSuresh Siddha 		untrack_pfn(vma, pfn, PAGE_ALIGN(size));
17472ab64037Svenkatesh.pallipadi@intel.com 
17481da177e4SLinus Torvalds 	return err;
17491da177e4SLinus Torvalds }
17501da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range);
17511da177e4SLinus Torvalds 
1752b4cbb197SLinus Torvalds /**
1753b4cbb197SLinus Torvalds  * vm_iomap_memory - remap memory to userspace
1754b4cbb197SLinus Torvalds  * @vma: user vma to map to
1755b4cbb197SLinus Torvalds  * @start: start of area
1756b4cbb197SLinus Torvalds  * @len: size of area
1757b4cbb197SLinus Torvalds  *
1758b4cbb197SLinus Torvalds  * This is a simplified io_remap_pfn_range() for common driver use. The
1759b4cbb197SLinus Torvalds  * driver just needs to give us the physical memory range to be mapped,
1760b4cbb197SLinus Torvalds  * we'll figure out the rest from the vma information.
1761b4cbb197SLinus Torvalds  *
1762b4cbb197SLinus Torvalds  * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get
1763b4cbb197SLinus Torvalds  * whatever write-combining details or similar.
1764b4cbb197SLinus Torvalds  */
1765b4cbb197SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
1766b4cbb197SLinus Torvalds {
1767b4cbb197SLinus Torvalds 	unsigned long vm_len, pfn, pages;
1768b4cbb197SLinus Torvalds 
1769b4cbb197SLinus Torvalds 	/* Check that the physical memory area passed in looks valid */
1770b4cbb197SLinus Torvalds 	if (start + len < start)
1771b4cbb197SLinus Torvalds 		return -EINVAL;
1772b4cbb197SLinus Torvalds 	/*
1773b4cbb197SLinus Torvalds 	 * You *really* shouldn't map things that aren't page-aligned,
1774b4cbb197SLinus Torvalds 	 * but we've historically allowed it because IO memory might
1775b4cbb197SLinus Torvalds 	 * just have smaller alignment.
1776b4cbb197SLinus Torvalds 	 */
1777b4cbb197SLinus Torvalds 	len += start & ~PAGE_MASK;
1778b4cbb197SLinus Torvalds 	pfn = start >> PAGE_SHIFT;
1779b4cbb197SLinus Torvalds 	pages = (len + ~PAGE_MASK) >> PAGE_SHIFT;
1780b4cbb197SLinus Torvalds 	if (pfn + pages < pfn)
1781b4cbb197SLinus Torvalds 		return -EINVAL;
1782b4cbb197SLinus Torvalds 
1783b4cbb197SLinus Torvalds 	/* We start the mapping 'vm_pgoff' pages into the area */
1784b4cbb197SLinus Torvalds 	if (vma->vm_pgoff > pages)
1785b4cbb197SLinus Torvalds 		return -EINVAL;
1786b4cbb197SLinus Torvalds 	pfn += vma->vm_pgoff;
1787b4cbb197SLinus Torvalds 	pages -= vma->vm_pgoff;
1788b4cbb197SLinus Torvalds 
1789b4cbb197SLinus Torvalds 	/* Can we fit all of the mapping? */
1790b4cbb197SLinus Torvalds 	vm_len = vma->vm_end - vma->vm_start;
1791b4cbb197SLinus Torvalds 	if (vm_len >> PAGE_SHIFT > pages)
1792b4cbb197SLinus Torvalds 		return -EINVAL;
1793b4cbb197SLinus Torvalds 
1794b4cbb197SLinus Torvalds 	/* Ok, let it rip */
1795b4cbb197SLinus Torvalds 	return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
1796b4cbb197SLinus Torvalds }
1797b4cbb197SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory);
1798b4cbb197SLinus Torvalds 
1799aee16b3cSJeremy Fitzhardinge static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
1800aee16b3cSJeremy Fitzhardinge 				     unsigned long addr, unsigned long end,
1801aee16b3cSJeremy Fitzhardinge 				     pte_fn_t fn, void *data)
1802aee16b3cSJeremy Fitzhardinge {
1803aee16b3cSJeremy Fitzhardinge 	pte_t *pte;
1804aee16b3cSJeremy Fitzhardinge 	int err;
18052f569afdSMartin Schwidefsky 	pgtable_t token;
180694909914SBorislav Petkov 	spinlock_t *uninitialized_var(ptl);
1807aee16b3cSJeremy Fitzhardinge 
1808aee16b3cSJeremy Fitzhardinge 	pte = (mm == &init_mm) ?
1809aee16b3cSJeremy Fitzhardinge 		pte_alloc_kernel(pmd, addr) :
1810aee16b3cSJeremy Fitzhardinge 		pte_alloc_map_lock(mm, pmd, addr, &ptl);
1811aee16b3cSJeremy Fitzhardinge 	if (!pte)
1812aee16b3cSJeremy Fitzhardinge 		return -ENOMEM;
1813aee16b3cSJeremy Fitzhardinge 
1814aee16b3cSJeremy Fitzhardinge 	BUG_ON(pmd_huge(*pmd));
1815aee16b3cSJeremy Fitzhardinge 
181638e0edb1SJeremy Fitzhardinge 	arch_enter_lazy_mmu_mode();
181738e0edb1SJeremy Fitzhardinge 
18182f569afdSMartin Schwidefsky 	token = pmd_pgtable(*pmd);
1819aee16b3cSJeremy Fitzhardinge 
1820aee16b3cSJeremy Fitzhardinge 	do {
1821c36987e2SDaisuke Nishimura 		err = fn(pte++, token, addr, data);
1822aee16b3cSJeremy Fitzhardinge 		if (err)
1823aee16b3cSJeremy Fitzhardinge 			break;
1824c36987e2SDaisuke Nishimura 	} while (addr += PAGE_SIZE, addr != end);
1825aee16b3cSJeremy Fitzhardinge 
182638e0edb1SJeremy Fitzhardinge 	arch_leave_lazy_mmu_mode();
182738e0edb1SJeremy Fitzhardinge 
1828aee16b3cSJeremy Fitzhardinge 	if (mm != &init_mm)
1829aee16b3cSJeremy Fitzhardinge 		pte_unmap_unlock(pte-1, ptl);
1830aee16b3cSJeremy Fitzhardinge 	return err;
1831aee16b3cSJeremy Fitzhardinge }
1832aee16b3cSJeremy Fitzhardinge 
1833aee16b3cSJeremy Fitzhardinge static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
1834aee16b3cSJeremy Fitzhardinge 				     unsigned long addr, unsigned long end,
1835aee16b3cSJeremy Fitzhardinge 				     pte_fn_t fn, void *data)
1836aee16b3cSJeremy Fitzhardinge {
1837aee16b3cSJeremy Fitzhardinge 	pmd_t *pmd;
1838aee16b3cSJeremy Fitzhardinge 	unsigned long next;
1839aee16b3cSJeremy Fitzhardinge 	int err;
1840aee16b3cSJeremy Fitzhardinge 
1841ceb86879SAndi Kleen 	BUG_ON(pud_huge(*pud));
1842ceb86879SAndi Kleen 
1843aee16b3cSJeremy Fitzhardinge 	pmd = pmd_alloc(mm, pud, addr);
1844aee16b3cSJeremy Fitzhardinge 	if (!pmd)
1845aee16b3cSJeremy Fitzhardinge 		return -ENOMEM;
1846aee16b3cSJeremy Fitzhardinge 	do {
1847aee16b3cSJeremy Fitzhardinge 		next = pmd_addr_end(addr, end);
1848aee16b3cSJeremy Fitzhardinge 		err = apply_to_pte_range(mm, pmd, addr, next, fn, data);
1849aee16b3cSJeremy Fitzhardinge 		if (err)
1850aee16b3cSJeremy Fitzhardinge 			break;
1851aee16b3cSJeremy Fitzhardinge 	} while (pmd++, addr = next, addr != end);
1852aee16b3cSJeremy Fitzhardinge 	return err;
1853aee16b3cSJeremy Fitzhardinge }
1854aee16b3cSJeremy Fitzhardinge 
1855aee16b3cSJeremy Fitzhardinge static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd,
1856aee16b3cSJeremy Fitzhardinge 				     unsigned long addr, unsigned long end,
1857aee16b3cSJeremy Fitzhardinge 				     pte_fn_t fn, void *data)
1858aee16b3cSJeremy Fitzhardinge {
1859aee16b3cSJeremy Fitzhardinge 	pud_t *pud;
1860aee16b3cSJeremy Fitzhardinge 	unsigned long next;
1861aee16b3cSJeremy Fitzhardinge 	int err;
1862aee16b3cSJeremy Fitzhardinge 
1863aee16b3cSJeremy Fitzhardinge 	pud = pud_alloc(mm, pgd, addr);
1864aee16b3cSJeremy Fitzhardinge 	if (!pud)
1865aee16b3cSJeremy Fitzhardinge 		return -ENOMEM;
1866aee16b3cSJeremy Fitzhardinge 	do {
1867aee16b3cSJeremy Fitzhardinge 		next = pud_addr_end(addr, end);
1868aee16b3cSJeremy Fitzhardinge 		err = apply_to_pmd_range(mm, pud, addr, next, fn, data);
1869aee16b3cSJeremy Fitzhardinge 		if (err)
1870aee16b3cSJeremy Fitzhardinge 			break;
1871aee16b3cSJeremy Fitzhardinge 	} while (pud++, addr = next, addr != end);
1872aee16b3cSJeremy Fitzhardinge 	return err;
1873aee16b3cSJeremy Fitzhardinge }
1874aee16b3cSJeremy Fitzhardinge 
1875aee16b3cSJeremy Fitzhardinge /*
1876aee16b3cSJeremy Fitzhardinge  * Scan a region of virtual memory, filling in page tables as necessary
1877aee16b3cSJeremy Fitzhardinge  * and calling a provided function on each leaf page table.
1878aee16b3cSJeremy Fitzhardinge  */
1879aee16b3cSJeremy Fitzhardinge int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
1880aee16b3cSJeremy Fitzhardinge 			unsigned long size, pte_fn_t fn, void *data)
1881aee16b3cSJeremy Fitzhardinge {
1882aee16b3cSJeremy Fitzhardinge 	pgd_t *pgd;
1883aee16b3cSJeremy Fitzhardinge 	unsigned long next;
188457250a5bSJeremy Fitzhardinge 	unsigned long end = addr + size;
1885aee16b3cSJeremy Fitzhardinge 	int err;
1886aee16b3cSJeremy Fitzhardinge 
1887aee16b3cSJeremy Fitzhardinge 	BUG_ON(addr >= end);
1888aee16b3cSJeremy Fitzhardinge 	pgd = pgd_offset(mm, addr);
1889aee16b3cSJeremy Fitzhardinge 	do {
1890aee16b3cSJeremy Fitzhardinge 		next = pgd_addr_end(addr, end);
1891aee16b3cSJeremy Fitzhardinge 		err = apply_to_pud_range(mm, pgd, addr, next, fn, data);
1892aee16b3cSJeremy Fitzhardinge 		if (err)
1893aee16b3cSJeremy Fitzhardinge 			break;
1894aee16b3cSJeremy Fitzhardinge 	} while (pgd++, addr = next, addr != end);
189557250a5bSJeremy Fitzhardinge 
1896aee16b3cSJeremy Fitzhardinge 	return err;
1897aee16b3cSJeremy Fitzhardinge }
1898aee16b3cSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(apply_to_page_range);
1899aee16b3cSJeremy Fitzhardinge 
19001da177e4SLinus Torvalds /*
19019b4bdd2fSKirill A. Shutemov  * handle_pte_fault chooses page fault handler according to an entry which was
19029b4bdd2fSKirill A. Shutemov  * read non-atomically.  Before making any commitment, on those architectures
19039b4bdd2fSKirill A. Shutemov  * or configurations (e.g. i386 with PAE) which might give a mix of unmatched
19049b4bdd2fSKirill A. Shutemov  * parts, do_swap_page must check under lock before unmapping the pte and
19059b4bdd2fSKirill A. Shutemov  * proceeding (but do_wp_page is only called after already making such a check;
1906a335b2e1SRyota Ozaki  * and do_anonymous_page can safely check later on).
19078f4e2101SHugh Dickins  */
19084c21e2f2SHugh Dickins static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
19098f4e2101SHugh Dickins 				pte_t *page_table, pte_t orig_pte)
19108f4e2101SHugh Dickins {
19118f4e2101SHugh Dickins 	int same = 1;
19128f4e2101SHugh Dickins #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
19138f4e2101SHugh Dickins 	if (sizeof(pte_t) > sizeof(unsigned long)) {
19144c21e2f2SHugh Dickins 		spinlock_t *ptl = pte_lockptr(mm, pmd);
19154c21e2f2SHugh Dickins 		spin_lock(ptl);
19168f4e2101SHugh Dickins 		same = pte_same(*page_table, orig_pte);
19174c21e2f2SHugh Dickins 		spin_unlock(ptl);
19188f4e2101SHugh Dickins 	}
19198f4e2101SHugh Dickins #endif
19208f4e2101SHugh Dickins 	pte_unmap(page_table);
19218f4e2101SHugh Dickins 	return same;
19228f4e2101SHugh Dickins }
19238f4e2101SHugh Dickins 
19249de455b2SAtsushi Nemoto static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma)
19256aab341eSLinus Torvalds {
19260abdd7a8SDan Williams 	debug_dma_assert_idle(src);
19270abdd7a8SDan Williams 
19286aab341eSLinus Torvalds 	/*
19296aab341eSLinus Torvalds 	 * If the source page was a PFN mapping, we don't have
19306aab341eSLinus Torvalds 	 * a "struct page" for it. We do a best-effort copy by
19316aab341eSLinus Torvalds 	 * just copying from the original user address. If that
19326aab341eSLinus Torvalds 	 * fails, we just zero-fill it. Live with it.
19336aab341eSLinus Torvalds 	 */
19346aab341eSLinus Torvalds 	if (unlikely(!src)) {
19359b04c5feSCong Wang 		void *kaddr = kmap_atomic(dst);
19365d2a2dbbSLinus Torvalds 		void __user *uaddr = (void __user *)(va & PAGE_MASK);
19375d2a2dbbSLinus Torvalds 
19385d2a2dbbSLinus Torvalds 		/*
19395d2a2dbbSLinus Torvalds 		 * This really shouldn't fail, because the page is there
19405d2a2dbbSLinus Torvalds 		 * in the page tables. But it might just be unreadable,
19415d2a2dbbSLinus Torvalds 		 * in which case we just give up and fill the result with
19425d2a2dbbSLinus Torvalds 		 * zeroes.
19435d2a2dbbSLinus Torvalds 		 */
19445d2a2dbbSLinus Torvalds 		if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE))
19453ecb01dfSJan Beulich 			clear_page(kaddr);
19469b04c5feSCong Wang 		kunmap_atomic(kaddr);
1947c4ec7b0dSDmitriy Monakhov 		flush_dcache_page(dst);
19480ed361deSNick Piggin 	} else
19499de455b2SAtsushi Nemoto 		copy_user_highpage(dst, src, va, vma);
19506aab341eSLinus Torvalds }
19516aab341eSLinus Torvalds 
19521da177e4SLinus Torvalds /*
1953fb09a464SKirill A. Shutemov  * Notify the address space that the page is about to become writable so that
1954fb09a464SKirill A. Shutemov  * it can prohibit this or wait for the page to get into an appropriate state.
1955fb09a464SKirill A. Shutemov  *
1956fb09a464SKirill A. Shutemov  * We do this without the lock held, so that it can sleep if it needs to.
1957fb09a464SKirill A. Shutemov  */
1958fb09a464SKirill A. Shutemov static int do_page_mkwrite(struct vm_area_struct *vma, struct page *page,
1959fb09a464SKirill A. Shutemov 	       unsigned long address)
1960fb09a464SKirill A. Shutemov {
1961fb09a464SKirill A. Shutemov 	struct vm_fault vmf;
1962fb09a464SKirill A. Shutemov 	int ret;
1963fb09a464SKirill A. Shutemov 
1964fb09a464SKirill A. Shutemov 	vmf.virtual_address = (void __user *)(address & PAGE_MASK);
1965fb09a464SKirill A. Shutemov 	vmf.pgoff = page->index;
1966fb09a464SKirill A. Shutemov 	vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
1967fb09a464SKirill A. Shutemov 	vmf.page = page;
19682e4cdab0SMatthew Wilcox 	vmf.cow_page = NULL;
1969fb09a464SKirill A. Shutemov 
1970fb09a464SKirill A. Shutemov 	ret = vma->vm_ops->page_mkwrite(vma, &vmf);
1971fb09a464SKirill A. Shutemov 	if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))
1972fb09a464SKirill A. Shutemov 		return ret;
1973fb09a464SKirill A. Shutemov 	if (unlikely(!(ret & VM_FAULT_LOCKED))) {
1974fb09a464SKirill A. Shutemov 		lock_page(page);
1975fb09a464SKirill A. Shutemov 		if (!page->mapping) {
1976fb09a464SKirill A. Shutemov 			unlock_page(page);
1977fb09a464SKirill A. Shutemov 			return 0; /* retry */
1978fb09a464SKirill A. Shutemov 		}
1979fb09a464SKirill A. Shutemov 		ret |= VM_FAULT_LOCKED;
1980fb09a464SKirill A. Shutemov 	} else
1981fb09a464SKirill A. Shutemov 		VM_BUG_ON_PAGE(!PageLocked(page), page);
1982fb09a464SKirill A. Shutemov 	return ret;
1983fb09a464SKirill A. Shutemov }
1984fb09a464SKirill A. Shutemov 
1985fb09a464SKirill A. Shutemov /*
1986*4e047f89SShachar Raindel  * Handle write page faults for pages that can be reused in the current vma
1987*4e047f89SShachar Raindel  *
1988*4e047f89SShachar Raindel  * This can happen either due to the mapping being with the VM_SHARED flag,
1989*4e047f89SShachar Raindel  * or due to us being the last reference standing to the page. In either
1990*4e047f89SShachar Raindel  * case, all we need to do here is to mark the page as writable and update
1991*4e047f89SShachar Raindel  * any related book-keeping.
1992*4e047f89SShachar Raindel  */
1993*4e047f89SShachar Raindel static inline int wp_page_reuse(struct mm_struct *mm,
1994*4e047f89SShachar Raindel 			struct vm_area_struct *vma, unsigned long address,
1995*4e047f89SShachar Raindel 			pte_t *page_table, spinlock_t *ptl, pte_t orig_pte,
1996*4e047f89SShachar Raindel 			struct page *page, int page_mkwrite,
1997*4e047f89SShachar Raindel 			int dirty_shared)
1998*4e047f89SShachar Raindel 	__releases(ptl)
1999*4e047f89SShachar Raindel {
2000*4e047f89SShachar Raindel 	pte_t entry;
2001*4e047f89SShachar Raindel 	/*
2002*4e047f89SShachar Raindel 	 * Clear the pages cpupid information as the existing
2003*4e047f89SShachar Raindel 	 * information potentially belongs to a now completely
2004*4e047f89SShachar Raindel 	 * unrelated process.
2005*4e047f89SShachar Raindel 	 */
2006*4e047f89SShachar Raindel 	if (page)
2007*4e047f89SShachar Raindel 		page_cpupid_xchg_last(page, (1 << LAST_CPUPID_SHIFT) - 1);
2008*4e047f89SShachar Raindel 
2009*4e047f89SShachar Raindel 	flush_cache_page(vma, address, pte_pfn(orig_pte));
2010*4e047f89SShachar Raindel 	entry = pte_mkyoung(orig_pte);
2011*4e047f89SShachar Raindel 	entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2012*4e047f89SShachar Raindel 	if (ptep_set_access_flags(vma, address, page_table, entry, 1))
2013*4e047f89SShachar Raindel 		update_mmu_cache(vma, address, page_table);
2014*4e047f89SShachar Raindel 	pte_unmap_unlock(page_table, ptl);
2015*4e047f89SShachar Raindel 
2016*4e047f89SShachar Raindel 	if (dirty_shared) {
2017*4e047f89SShachar Raindel 		struct address_space *mapping;
2018*4e047f89SShachar Raindel 		int dirtied;
2019*4e047f89SShachar Raindel 
2020*4e047f89SShachar Raindel 		if (!page_mkwrite)
2021*4e047f89SShachar Raindel 			lock_page(page);
2022*4e047f89SShachar Raindel 
2023*4e047f89SShachar Raindel 		dirtied = set_page_dirty(page);
2024*4e047f89SShachar Raindel 		VM_BUG_ON_PAGE(PageAnon(page), page);
2025*4e047f89SShachar Raindel 		mapping = page->mapping;
2026*4e047f89SShachar Raindel 		unlock_page(page);
2027*4e047f89SShachar Raindel 		page_cache_release(page);
2028*4e047f89SShachar Raindel 
2029*4e047f89SShachar Raindel 		if ((dirtied || page_mkwrite) && mapping) {
2030*4e047f89SShachar Raindel 			/*
2031*4e047f89SShachar Raindel 			 * Some device drivers do not set page.mapping
2032*4e047f89SShachar Raindel 			 * but still dirty their pages
2033*4e047f89SShachar Raindel 			 */
2034*4e047f89SShachar Raindel 			balance_dirty_pages_ratelimited(mapping);
2035*4e047f89SShachar Raindel 		}
2036*4e047f89SShachar Raindel 
2037*4e047f89SShachar Raindel 		if (!page_mkwrite)
2038*4e047f89SShachar Raindel 			file_update_time(vma->vm_file);
2039*4e047f89SShachar Raindel 	}
2040*4e047f89SShachar Raindel 
2041*4e047f89SShachar Raindel 	return VM_FAULT_WRITE;
2042*4e047f89SShachar Raindel }
2043*4e047f89SShachar Raindel 
2044*4e047f89SShachar Raindel /*
20451da177e4SLinus Torvalds  * This routine handles present pages, when users try to write
20461da177e4SLinus Torvalds  * to a shared page. It is done by copying the page to a new address
20471da177e4SLinus Torvalds  * and decrementing the shared-page counter for the old page.
20481da177e4SLinus Torvalds  *
20491da177e4SLinus Torvalds  * Note that this routine assumes that the protection checks have been
20501da177e4SLinus Torvalds  * done by the caller (the low-level page fault routine in most cases).
20511da177e4SLinus Torvalds  * Thus we can safely just mark it writable once we've done any necessary
20521da177e4SLinus Torvalds  * COW.
20531da177e4SLinus Torvalds  *
20541da177e4SLinus Torvalds  * We also mark the page dirty at this point even though the page will
20551da177e4SLinus Torvalds  * change only once the write actually happens. This avoids a few races,
20561da177e4SLinus Torvalds  * and potentially makes it more efficient.
20571da177e4SLinus Torvalds  *
20588f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
20598f4e2101SHugh Dickins  * but allow concurrent faults), with pte both mapped and locked.
20608f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
20611da177e4SLinus Torvalds  */
20621da177e4SLinus Torvalds static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
206365500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
20648f4e2101SHugh Dickins 		spinlock_t *ptl, pte_t orig_pte)
2065e6219ec8SNamhyung Kim 	__releases(ptl)
20661da177e4SLinus Torvalds {
20672ec74c3eSSagi Grimberg 	struct page *old_page, *new_page = NULL;
20681da177e4SLinus Torvalds 	pte_t entry;
2069b009c024SMichel Lespinasse 	int ret = 0;
20701756954cSDavid Rientjes 	unsigned long mmun_start = 0;	/* For mmu_notifiers */
20711756954cSDavid Rientjes 	unsigned long mmun_end = 0;	/* For mmu_notifiers */
207200501b53SJohannes Weiner 	struct mem_cgroup *memcg;
20731da177e4SLinus Torvalds 
20746aab341eSLinus Torvalds 	old_page = vm_normal_page(vma, address, orig_pte);
2075251b97f5SPeter Zijlstra 	if (!old_page) {
2076251b97f5SPeter Zijlstra 		/*
207764e45507SPeter Feiner 		 * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a
207864e45507SPeter Feiner 		 * VM_PFNMAP VMA.
2079251b97f5SPeter Zijlstra 		 *
2080251b97f5SPeter Zijlstra 		 * We should not cow pages in a shared writeable mapping.
2081251b97f5SPeter Zijlstra 		 * Just mark the pages writable as we can't do any dirty
2082251b97f5SPeter Zijlstra 		 * accounting on raw pfn maps.
2083251b97f5SPeter Zijlstra 		 */
2084251b97f5SPeter Zijlstra 		if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
2085251b97f5SPeter Zijlstra 				     (VM_WRITE|VM_SHARED))
2086*4e047f89SShachar Raindel 			return wp_page_reuse(mm, vma, address, page_table, ptl,
2087*4e047f89SShachar Raindel 					     orig_pte, old_page, 0, 0);
2088920fc356SHugh Dickins 		goto gotten;
2089251b97f5SPeter Zijlstra 	}
20901da177e4SLinus Torvalds 
2091d08b3851SPeter Zijlstra 	/*
2092ee6a6457SPeter Zijlstra 	 * Take out anonymous pages first, anonymous shared vmas are
2093ee6a6457SPeter Zijlstra 	 * not dirty accountable.
2094d08b3851SPeter Zijlstra 	 */
20959a840895SHugh Dickins 	if (PageAnon(old_page) && !PageKsm(old_page)) {
2096ab967d86SHugh Dickins 		if (!trylock_page(old_page)) {
2097ab967d86SHugh Dickins 			page_cache_get(old_page);
2098ab967d86SHugh Dickins 			pte_unmap_unlock(page_table, ptl);
2099ab967d86SHugh Dickins 			lock_page(old_page);
2100ab967d86SHugh Dickins 			page_table = pte_offset_map_lock(mm, pmd, address,
2101ab967d86SHugh Dickins 							 &ptl);
2102ab967d86SHugh Dickins 			if (!pte_same(*page_table, orig_pte)) {
2103ab967d86SHugh Dickins 				unlock_page(old_page);
2104ab967d86SHugh Dickins 				goto unlock;
2105ab967d86SHugh Dickins 			}
2106ab967d86SHugh Dickins 			page_cache_release(old_page);
2107ab967d86SHugh Dickins 		}
2108b009c024SMichel Lespinasse 		if (reuse_swap_page(old_page)) {
2109c44b6743SRik van Riel 			/*
2110c44b6743SRik van Riel 			 * The page is all ours.  Move it to our anon_vma so
2111c44b6743SRik van Riel 			 * the rmap code will not search our parent or siblings.
2112c44b6743SRik van Riel 			 * Protected against the rmap code by the page lock.
2113c44b6743SRik van Riel 			 */
2114c44b6743SRik van Riel 			page_move_anon_rmap(old_page, vma, address);
2115ee6a6457SPeter Zijlstra 			unlock_page(old_page);
2116*4e047f89SShachar Raindel 			return wp_page_reuse(mm, vma, address, page_table, ptl,
2117*4e047f89SShachar Raindel 					     orig_pte, old_page, 0, 0);
2118b009c024SMichel Lespinasse 		}
2119b009c024SMichel Lespinasse 		unlock_page(old_page);
2120ee6a6457SPeter Zijlstra 	} else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
2121d08b3851SPeter Zijlstra 					(VM_WRITE|VM_SHARED))) {
2122*4e047f89SShachar Raindel 		int page_mkwrite = 0;
2123*4e047f89SShachar Raindel 
2124f38b4b31SJohannes Weiner 		page_cache_get(old_page);
2125*4e047f89SShachar Raindel 
2126ee6a6457SPeter Zijlstra 		/*
2127ee6a6457SPeter Zijlstra 		 * Only catch write-faults on shared writable pages,
2128ee6a6457SPeter Zijlstra 		 * read-only shared pages can get COWed by
2129ee6a6457SPeter Zijlstra 		 * get_user_pages(.write=1, .force=1).
2130ee6a6457SPeter Zijlstra 		 */
21319637a5efSDavid Howells 		if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
2132c2ec175cSNick Piggin 			int tmp;
2133f38b4b31SJohannes Weiner 
21349637a5efSDavid Howells 			pte_unmap_unlock(page_table, ptl);
2135fb09a464SKirill A. Shutemov 			tmp = do_page_mkwrite(vma, old_page, address);
2136fb09a464SKirill A. Shutemov 			if (unlikely(!tmp || (tmp &
2137fb09a464SKirill A. Shutemov 					(VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
2138fb09a464SKirill A. Shutemov 				page_cache_release(old_page);
2139fb09a464SKirill A. Shutemov 				return tmp;
2140c2ec175cSNick Piggin 			}
21419637a5efSDavid Howells 			/*
21429637a5efSDavid Howells 			 * Since we dropped the lock we need to revalidate
21439637a5efSDavid Howells 			 * the PTE as someone else may have changed it.  If
21449637a5efSDavid Howells 			 * they did, we just return, as we can count on the
21459637a5efSDavid Howells 			 * MMU to tell us if they didn't also make it writable.
21469637a5efSDavid Howells 			 */
21479637a5efSDavid Howells 			page_table = pte_offset_map_lock(mm, pmd, address,
21489637a5efSDavid Howells 							 &ptl);
2149b827e496SNick Piggin 			if (!pte_same(*page_table, orig_pte)) {
2150b827e496SNick Piggin 				unlock_page(old_page);
21519637a5efSDavid Howells 				goto unlock;
2152b827e496SNick Piggin 			}
2153a200ee18SPeter Zijlstra 			page_mkwrite = 1;
21549637a5efSDavid Howells 		}
2155f38b4b31SJohannes Weiner 
2156*4e047f89SShachar Raindel 		return wp_page_reuse(mm, vma, address, page_table, ptl,
2157*4e047f89SShachar Raindel 				     orig_pte, old_page, page_mkwrite, 1);
21581da177e4SLinus Torvalds 	}
21591da177e4SLinus Torvalds 
21601da177e4SLinus Torvalds 	/*
21611da177e4SLinus Torvalds 	 * Ok, we need to copy. Oh, well..
21621da177e4SLinus Torvalds 	 */
21631da177e4SLinus Torvalds 	page_cache_get(old_page);
2164920fc356SHugh Dickins gotten:
21658f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
21661da177e4SLinus Torvalds 
21671da177e4SLinus Torvalds 	if (unlikely(anon_vma_prepare(vma)))
216865500d23SHugh Dickins 		goto oom;
2169a13ea5b7SHugh Dickins 
217062eede62SHugh Dickins 	if (is_zero_pfn(pte_pfn(orig_pte))) {
2171a13ea5b7SHugh Dickins 		new_page = alloc_zeroed_user_highpage_movable(vma, address);
2172a13ea5b7SHugh Dickins 		if (!new_page)
2173a13ea5b7SHugh Dickins 			goto oom;
2174a13ea5b7SHugh Dickins 	} else {
2175769848c0SMel Gorman 		new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
21761da177e4SLinus Torvalds 		if (!new_page)
217765500d23SHugh Dickins 			goto oom;
2178a13ea5b7SHugh Dickins 		cow_user_page(new_page, old_page, address, vma);
2179a13ea5b7SHugh Dickins 	}
2180a13ea5b7SHugh Dickins 	__SetPageUptodate(new_page);
2181a13ea5b7SHugh Dickins 
218200501b53SJohannes Weiner 	if (mem_cgroup_try_charge(new_page, mm, GFP_KERNEL, &memcg))
21838a9f3ccdSBalbir Singh 		goto oom_free_new;
21848a9f3ccdSBalbir Singh 
21856bdb913fSHaggai Eran 	mmun_start  = address & PAGE_MASK;
21861756954cSDavid Rientjes 	mmun_end    = mmun_start + PAGE_SIZE;
21876bdb913fSHaggai Eran 	mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
21886bdb913fSHaggai Eran 
21891da177e4SLinus Torvalds 	/*
21901da177e4SLinus Torvalds 	 * Re-check the pte - we dropped the lock
21911da177e4SLinus Torvalds 	 */
21928f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
219365500d23SHugh Dickins 	if (likely(pte_same(*page_table, orig_pte))) {
2194920fc356SHugh Dickins 		if (old_page) {
21954294621fSHugh Dickins 			if (!PageAnon(old_page)) {
219634e55232SKAMEZAWA Hiroyuki 				dec_mm_counter_fast(mm, MM_FILEPAGES);
219734e55232SKAMEZAWA Hiroyuki 				inc_mm_counter_fast(mm, MM_ANONPAGES);
21984294621fSHugh Dickins 			}
2199920fc356SHugh Dickins 		} else
220034e55232SKAMEZAWA Hiroyuki 			inc_mm_counter_fast(mm, MM_ANONPAGES);
2201eca35133SBen Collins 		flush_cache_page(vma, address, pte_pfn(orig_pte));
220265500d23SHugh Dickins 		entry = mk_pte(new_page, vma->vm_page_prot);
220365500d23SHugh Dickins 		entry = maybe_mkwrite(pte_mkdirty(entry), vma);
22044ce072f1SSiddha, Suresh B 		/*
22054ce072f1SSiddha, Suresh B 		 * Clear the pte entry and flush it first, before updating the
22064ce072f1SSiddha, Suresh B 		 * pte with the new entry. This will avoid a race condition
22074ce072f1SSiddha, Suresh B 		 * seen in the presence of one thread doing SMC and another
22084ce072f1SSiddha, Suresh B 		 * thread doing COW.
22094ce072f1SSiddha, Suresh B 		 */
221034ee645eSJoerg Roedel 		ptep_clear_flush_notify(vma, address, page_table);
22119617d95eSNick Piggin 		page_add_new_anon_rmap(new_page, vma, address);
221200501b53SJohannes Weiner 		mem_cgroup_commit_charge(new_page, memcg, false);
221300501b53SJohannes Weiner 		lru_cache_add_active_or_unevictable(new_page, vma);
2214828502d3SIzik Eidus 		/*
2215828502d3SIzik Eidus 		 * We call the notify macro here because, when using secondary
2216828502d3SIzik Eidus 		 * mmu page tables (such as kvm shadow page tables), we want the
2217828502d3SIzik Eidus 		 * new page to be mapped directly into the secondary page table.
2218828502d3SIzik Eidus 		 */
2219828502d3SIzik Eidus 		set_pte_at_notify(mm, address, page_table, entry);
22204b3073e1SRussell King 		update_mmu_cache(vma, address, page_table);
2221945754a1SNick Piggin 		if (old_page) {
2222945754a1SNick Piggin 			/*
2223945754a1SNick Piggin 			 * Only after switching the pte to the new page may
2224945754a1SNick Piggin 			 * we remove the mapcount here. Otherwise another
2225945754a1SNick Piggin 			 * process may come and find the rmap count decremented
2226945754a1SNick Piggin 			 * before the pte is switched to the new page, and
2227945754a1SNick Piggin 			 * "reuse" the old page writing into it while our pte
2228945754a1SNick Piggin 			 * here still points into it and can be read by other
2229945754a1SNick Piggin 			 * threads.
2230945754a1SNick Piggin 			 *
2231945754a1SNick Piggin 			 * The critical issue is to order this
2232945754a1SNick Piggin 			 * page_remove_rmap with the ptp_clear_flush above.
2233945754a1SNick Piggin 			 * Those stores are ordered by (if nothing else,)
2234945754a1SNick Piggin 			 * the barrier present in the atomic_add_negative
2235945754a1SNick Piggin 			 * in page_remove_rmap.
2236945754a1SNick Piggin 			 *
2237945754a1SNick Piggin 			 * Then the TLB flush in ptep_clear_flush ensures that
2238945754a1SNick Piggin 			 * no process can access the old page before the
2239945754a1SNick Piggin 			 * decremented mapcount is visible. And the old page
2240945754a1SNick Piggin 			 * cannot be reused until after the decremented
2241945754a1SNick Piggin 			 * mapcount is visible. So transitively, TLBs to
2242945754a1SNick Piggin 			 * old page will be flushed before it can be reused.
2243945754a1SNick Piggin 			 */
2244edc315fdSHugh Dickins 			page_remove_rmap(old_page);
2245945754a1SNick Piggin 		}
2246945754a1SNick Piggin 
22471da177e4SLinus Torvalds 		/* Free the old page.. */
22481da177e4SLinus Torvalds 		new_page = old_page;
2249f33ea7f4SNick Piggin 		ret |= VM_FAULT_WRITE;
22508a9f3ccdSBalbir Singh 	} else
225100501b53SJohannes Weiner 		mem_cgroup_cancel_charge(new_page, memcg);
22528a9f3ccdSBalbir Singh 
22536bdb913fSHaggai Eran 	if (new_page)
22546bdb913fSHaggai Eran 		page_cache_release(new_page);
225565500d23SHugh Dickins unlock:
22568f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
22571756954cSDavid Rientjes 	if (mmun_end > mmun_start)
22586bdb913fSHaggai Eran 		mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
2259e15f8c01SMichel Lespinasse 	if (old_page) {
2260e15f8c01SMichel Lespinasse 		/*
2261e15f8c01SMichel Lespinasse 		 * Don't let another task, with possibly unlocked vma,
2262e15f8c01SMichel Lespinasse 		 * keep the mlocked page.
2263e15f8c01SMichel Lespinasse 		 */
2264e15f8c01SMichel Lespinasse 		if ((ret & VM_FAULT_WRITE) && (vma->vm_flags & VM_LOCKED)) {
2265e15f8c01SMichel Lespinasse 			lock_page(old_page);	/* LRU manipulation */
2266e15f8c01SMichel Lespinasse 			munlock_vma_page(old_page);
2267e15f8c01SMichel Lespinasse 			unlock_page(old_page);
2268e15f8c01SMichel Lespinasse 		}
2269e15f8c01SMichel Lespinasse 		page_cache_release(old_page);
2270e15f8c01SMichel Lespinasse 	}
2271f33ea7f4SNick Piggin 	return ret;
22728a9f3ccdSBalbir Singh oom_free_new:
22736dbf6d3bSHugh Dickins 	page_cache_release(new_page);
227465500d23SHugh Dickins oom:
227566521d5aSDominik Dingel 	if (old_page)
22761da177e4SLinus Torvalds 		page_cache_release(old_page);
22771da177e4SLinus Torvalds 	return VM_FAULT_OOM;
22781da177e4SLinus Torvalds }
22791da177e4SLinus Torvalds 
228097a89413SPeter Zijlstra static void unmap_mapping_range_vma(struct vm_area_struct *vma,
22811da177e4SLinus Torvalds 		unsigned long start_addr, unsigned long end_addr,
22821da177e4SLinus Torvalds 		struct zap_details *details)
22831da177e4SLinus Torvalds {
2284f5cc4eefSAl Viro 	zap_page_range_single(vma, start_addr, end_addr - start_addr, details);
22851da177e4SLinus Torvalds }
22861da177e4SLinus Torvalds 
22876b2dbba8SMichel Lespinasse static inline void unmap_mapping_range_tree(struct rb_root *root,
22881da177e4SLinus Torvalds 					    struct zap_details *details)
22891da177e4SLinus Torvalds {
22901da177e4SLinus Torvalds 	struct vm_area_struct *vma;
22911da177e4SLinus Torvalds 	pgoff_t vba, vea, zba, zea;
22921da177e4SLinus Torvalds 
22936b2dbba8SMichel Lespinasse 	vma_interval_tree_foreach(vma, root,
22941da177e4SLinus Torvalds 			details->first_index, details->last_index) {
22951da177e4SLinus Torvalds 
22961da177e4SLinus Torvalds 		vba = vma->vm_pgoff;
2297d6e93217SLibin 		vea = vba + vma_pages(vma) - 1;
22981da177e4SLinus Torvalds 		/* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
22991da177e4SLinus Torvalds 		zba = details->first_index;
23001da177e4SLinus Torvalds 		if (zba < vba)
23011da177e4SLinus Torvalds 			zba = vba;
23021da177e4SLinus Torvalds 		zea = details->last_index;
23031da177e4SLinus Torvalds 		if (zea > vea)
23041da177e4SLinus Torvalds 			zea = vea;
23051da177e4SLinus Torvalds 
230697a89413SPeter Zijlstra 		unmap_mapping_range_vma(vma,
23071da177e4SLinus Torvalds 			((zba - vba) << PAGE_SHIFT) + vma->vm_start,
23081da177e4SLinus Torvalds 			((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
230997a89413SPeter Zijlstra 				details);
23101da177e4SLinus Torvalds 	}
23111da177e4SLinus Torvalds }
23121da177e4SLinus Torvalds 
23131da177e4SLinus Torvalds /**
23148a5f14a2SKirill A. Shutemov  * unmap_mapping_range - unmap the portion of all mmaps in the specified
23158a5f14a2SKirill A. Shutemov  * address_space corresponding to the specified page range in the underlying
23168a5f14a2SKirill A. Shutemov  * file.
23178a5f14a2SKirill A. Shutemov  *
23183d41088fSMartin Waitz  * @mapping: the address space containing mmaps to be unmapped.
23191da177e4SLinus Torvalds  * @holebegin: byte in first page to unmap, relative to the start of
23201da177e4SLinus Torvalds  * the underlying file.  This will be rounded down to a PAGE_SIZE
232125d9e2d1Snpiggin@suse.de  * boundary.  Note that this is different from truncate_pagecache(), which
23221da177e4SLinus Torvalds  * must keep the partial page.  In contrast, we must get rid of
23231da177e4SLinus Torvalds  * partial pages.
23241da177e4SLinus Torvalds  * @holelen: size of prospective hole in bytes.  This will be rounded
23251da177e4SLinus Torvalds  * up to a PAGE_SIZE boundary.  A holelen of zero truncates to the
23261da177e4SLinus Torvalds  * end of the file.
23271da177e4SLinus Torvalds  * @even_cows: 1 when truncating a file, unmap even private COWed pages;
23281da177e4SLinus Torvalds  * but 0 when invalidating pagecache, don't throw away private data.
23291da177e4SLinus Torvalds  */
23301da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping,
23311da177e4SLinus Torvalds 		loff_t const holebegin, loff_t const holelen, int even_cows)
23321da177e4SLinus Torvalds {
23331da177e4SLinus Torvalds 	struct zap_details details;
23341da177e4SLinus Torvalds 	pgoff_t hba = holebegin >> PAGE_SHIFT;
23351da177e4SLinus Torvalds 	pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
23361da177e4SLinus Torvalds 
23371da177e4SLinus Torvalds 	/* Check for overflow. */
23381da177e4SLinus Torvalds 	if (sizeof(holelen) > sizeof(hlen)) {
23391da177e4SLinus Torvalds 		long long holeend =
23401da177e4SLinus Torvalds 			(holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
23411da177e4SLinus Torvalds 		if (holeend & ~(long long)ULONG_MAX)
23421da177e4SLinus Torvalds 			hlen = ULONG_MAX - hba + 1;
23431da177e4SLinus Torvalds 	}
23441da177e4SLinus Torvalds 
23451da177e4SLinus Torvalds 	details.check_mapping = even_cows? NULL: mapping;
23461da177e4SLinus Torvalds 	details.first_index = hba;
23471da177e4SLinus Torvalds 	details.last_index = hba + hlen - 1;
23481da177e4SLinus Torvalds 	if (details.last_index < details.first_index)
23491da177e4SLinus Torvalds 		details.last_index = ULONG_MAX;
23501da177e4SLinus Torvalds 
235197a89413SPeter Zijlstra 
2352283307c7SMatthew Wilcox 	/* DAX uses i_mmap_lock to serialise file truncate vs page fault */
235348ec833bSKirill A. Shutemov 	i_mmap_lock_write(mapping);
23546b2dbba8SMichel Lespinasse 	if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap)))
23551da177e4SLinus Torvalds 		unmap_mapping_range_tree(&mapping->i_mmap, &details);
235648ec833bSKirill A. Shutemov 	i_mmap_unlock_write(mapping);
23571da177e4SLinus Torvalds }
23581da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range);
23591da177e4SLinus Torvalds 
23601da177e4SLinus Torvalds /*
23618f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
23628f4e2101SHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
23639a95f3cfSPaul Cassella  * We return with pte unmapped and unlocked.
23649a95f3cfSPaul Cassella  *
23659a95f3cfSPaul Cassella  * We return with the mmap_sem locked or unlocked in the same cases
23669a95f3cfSPaul Cassella  * as does filemap_fault().
23671da177e4SLinus Torvalds  */
236865500d23SHugh Dickins static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
236965500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
237030c9f3a9SLinus Torvalds 		unsigned int flags, pte_t orig_pte)
23711da177e4SLinus Torvalds {
23728f4e2101SHugh Dickins 	spinlock_t *ptl;
237356f31801SHugh Dickins 	struct page *page, *swapcache;
237400501b53SJohannes Weiner 	struct mem_cgroup *memcg;
237565500d23SHugh Dickins 	swp_entry_t entry;
23761da177e4SLinus Torvalds 	pte_t pte;
2377d065bd81SMichel Lespinasse 	int locked;
2378ad8c2ee8SRik van Riel 	int exclusive = 0;
237983c54070SNick Piggin 	int ret = 0;
23801da177e4SLinus Torvalds 
23814c21e2f2SHugh Dickins 	if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
23828f4e2101SHugh Dickins 		goto out;
238365500d23SHugh Dickins 
238465500d23SHugh Dickins 	entry = pte_to_swp_entry(orig_pte);
2385d1737fdbSAndi Kleen 	if (unlikely(non_swap_entry(entry))) {
23860697212aSChristoph Lameter 		if (is_migration_entry(entry)) {
23870697212aSChristoph Lameter 			migration_entry_wait(mm, pmd, address);
2388d1737fdbSAndi Kleen 		} else if (is_hwpoison_entry(entry)) {
2389d1737fdbSAndi Kleen 			ret = VM_FAULT_HWPOISON;
2390d1737fdbSAndi Kleen 		} else {
2391d1737fdbSAndi Kleen 			print_bad_pte(vma, address, orig_pte, NULL);
2392d99be1a8SHugh Dickins 			ret = VM_FAULT_SIGBUS;
2393d1737fdbSAndi Kleen 		}
23940697212aSChristoph Lameter 		goto out;
23950697212aSChristoph Lameter 	}
23960ff92245SShailabh Nagar 	delayacct_set_flag(DELAYACCT_PF_SWAPIN);
23971da177e4SLinus Torvalds 	page = lookup_swap_cache(entry);
23981da177e4SLinus Torvalds 	if (!page) {
239902098feaSHugh Dickins 		page = swapin_readahead(entry,
240002098feaSHugh Dickins 					GFP_HIGHUSER_MOVABLE, vma, address);
24011da177e4SLinus Torvalds 		if (!page) {
24021da177e4SLinus Torvalds 			/*
24038f4e2101SHugh Dickins 			 * Back out if somebody else faulted in this pte
24048f4e2101SHugh Dickins 			 * while we released the pte lock.
24051da177e4SLinus Torvalds 			 */
24068f4e2101SHugh Dickins 			page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
24071da177e4SLinus Torvalds 			if (likely(pte_same(*page_table, orig_pte)))
24081da177e4SLinus Torvalds 				ret = VM_FAULT_OOM;
24090ff92245SShailabh Nagar 			delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
241065500d23SHugh Dickins 			goto unlock;
24111da177e4SLinus Torvalds 		}
24121da177e4SLinus Torvalds 
24131da177e4SLinus Torvalds 		/* Had to read the page from swap area: Major fault */
24141da177e4SLinus Torvalds 		ret = VM_FAULT_MAJOR;
2415f8891e5eSChristoph Lameter 		count_vm_event(PGMAJFAULT);
2416456f998eSYing Han 		mem_cgroup_count_vm_event(mm, PGMAJFAULT);
2417d1737fdbSAndi Kleen 	} else if (PageHWPoison(page)) {
241871f72525SWu Fengguang 		/*
241971f72525SWu Fengguang 		 * hwpoisoned dirty swapcache pages are kept for killing
242071f72525SWu Fengguang 		 * owner processes (which may be unknown at hwpoison time)
242171f72525SWu Fengguang 		 */
2422d1737fdbSAndi Kleen 		ret = VM_FAULT_HWPOISON;
2423d1737fdbSAndi Kleen 		delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
242456f31801SHugh Dickins 		swapcache = page;
24254779cb31SAndi Kleen 		goto out_release;
24261da177e4SLinus Torvalds 	}
24271da177e4SLinus Torvalds 
242856f31801SHugh Dickins 	swapcache = page;
2429d065bd81SMichel Lespinasse 	locked = lock_page_or_retry(page, mm, flags);
2430e709ffd6SRik van Riel 
243120a1022dSBalbir Singh 	delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
2432d065bd81SMichel Lespinasse 	if (!locked) {
2433d065bd81SMichel Lespinasse 		ret |= VM_FAULT_RETRY;
2434d065bd81SMichel Lespinasse 		goto out_release;
2435d065bd81SMichel Lespinasse 	}
24361da177e4SLinus Torvalds 
24374969c119SAndrea Arcangeli 	/*
243831c4a3d3SHugh Dickins 	 * Make sure try_to_free_swap or reuse_swap_page or swapoff did not
243931c4a3d3SHugh Dickins 	 * release the swapcache from under us.  The page pin, and pte_same
244031c4a3d3SHugh Dickins 	 * test below, are not enough to exclude that.  Even if it is still
244131c4a3d3SHugh Dickins 	 * swapcache, we need to check that the page's swap has not changed.
24424969c119SAndrea Arcangeli 	 */
244331c4a3d3SHugh Dickins 	if (unlikely(!PageSwapCache(page) || page_private(page) != entry.val))
24444969c119SAndrea Arcangeli 		goto out_page;
24454969c119SAndrea Arcangeli 
2446cbf86cfeSHugh Dickins 	page = ksm_might_need_to_copy(page, vma, address);
24474969c119SAndrea Arcangeli 	if (unlikely(!page)) {
24485ad64688SHugh Dickins 		ret = VM_FAULT_OOM;
24494969c119SAndrea Arcangeli 		page = swapcache;
24504969c119SAndrea Arcangeli 		goto out_page;
24514969c119SAndrea Arcangeli 	}
24525ad64688SHugh Dickins 
245300501b53SJohannes Weiner 	if (mem_cgroup_try_charge(page, mm, GFP_KERNEL, &memcg)) {
2454073e587eSKAMEZAWA Hiroyuki 		ret = VM_FAULT_OOM;
2455bc43f75cSJohannes Weiner 		goto out_page;
2456073e587eSKAMEZAWA Hiroyuki 	}
2457073e587eSKAMEZAWA Hiroyuki 
24581da177e4SLinus Torvalds 	/*
24598f4e2101SHugh Dickins 	 * Back out if somebody else already faulted in this pte.
24601da177e4SLinus Torvalds 	 */
24618f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
24629e9bef07SHugh Dickins 	if (unlikely(!pte_same(*page_table, orig_pte)))
2463b8107480SKirill Korotaev 		goto out_nomap;
2464b8107480SKirill Korotaev 
2465b8107480SKirill Korotaev 	if (unlikely(!PageUptodate(page))) {
2466b8107480SKirill Korotaev 		ret = VM_FAULT_SIGBUS;
2467b8107480SKirill Korotaev 		goto out_nomap;
24681da177e4SLinus Torvalds 	}
24691da177e4SLinus Torvalds 
24708c7c6e34SKAMEZAWA Hiroyuki 	/*
24718c7c6e34SKAMEZAWA Hiroyuki 	 * The page isn't present yet, go ahead with the fault.
24728c7c6e34SKAMEZAWA Hiroyuki 	 *
24738c7c6e34SKAMEZAWA Hiroyuki 	 * Be careful about the sequence of operations here.
24748c7c6e34SKAMEZAWA Hiroyuki 	 * To get its accounting right, reuse_swap_page() must be called
24758c7c6e34SKAMEZAWA Hiroyuki 	 * while the page is counted on swap but not yet in mapcount i.e.
24768c7c6e34SKAMEZAWA Hiroyuki 	 * before page_add_anon_rmap() and swap_free(); try_to_free_swap()
24778c7c6e34SKAMEZAWA Hiroyuki 	 * must be called after the swap_free(), or it will never succeed.
24788c7c6e34SKAMEZAWA Hiroyuki 	 */
24791da177e4SLinus Torvalds 
248034e55232SKAMEZAWA Hiroyuki 	inc_mm_counter_fast(mm, MM_ANONPAGES);
2481b084d435SKAMEZAWA Hiroyuki 	dec_mm_counter_fast(mm, MM_SWAPENTS);
24821da177e4SLinus Torvalds 	pte = mk_pte(page, vma->vm_page_prot);
248330c9f3a9SLinus Torvalds 	if ((flags & FAULT_FLAG_WRITE) && reuse_swap_page(page)) {
24841da177e4SLinus Torvalds 		pte = maybe_mkwrite(pte_mkdirty(pte), vma);
248530c9f3a9SLinus Torvalds 		flags &= ~FAULT_FLAG_WRITE;
24869a5b489bSAndrea Arcangeli 		ret |= VM_FAULT_WRITE;
2487ad8c2ee8SRik van Riel 		exclusive = 1;
24881da177e4SLinus Torvalds 	}
24891da177e4SLinus Torvalds 	flush_icache_page(vma, page);
2490179ef71cSCyrill Gorcunov 	if (pte_swp_soft_dirty(orig_pte))
2491179ef71cSCyrill Gorcunov 		pte = pte_mksoft_dirty(pte);
24921da177e4SLinus Torvalds 	set_pte_at(mm, address, page_table, pte);
249300501b53SJohannes Weiner 	if (page == swapcache) {
2494ad8c2ee8SRik van Riel 		do_page_add_anon_rmap(page, vma, address, exclusive);
249500501b53SJohannes Weiner 		mem_cgroup_commit_charge(page, memcg, true);
249600501b53SJohannes Weiner 	} else { /* ksm created a completely new copy */
249756f31801SHugh Dickins 		page_add_new_anon_rmap(page, vma, address);
249800501b53SJohannes Weiner 		mem_cgroup_commit_charge(page, memcg, false);
249900501b53SJohannes Weiner 		lru_cache_add_active_or_unevictable(page, vma);
250000501b53SJohannes Weiner 	}
25011da177e4SLinus Torvalds 
2502c475a8abSHugh Dickins 	swap_free(entry);
2503b291f000SNick Piggin 	if (vm_swap_full() || (vma->vm_flags & VM_LOCKED) || PageMlocked(page))
2504a2c43eedSHugh Dickins 		try_to_free_swap(page);
2505c475a8abSHugh Dickins 	unlock_page(page);
250656f31801SHugh Dickins 	if (page != swapcache) {
25074969c119SAndrea Arcangeli 		/*
25084969c119SAndrea Arcangeli 		 * Hold the lock to avoid the swap entry to be reused
25094969c119SAndrea Arcangeli 		 * until we take the PT lock for the pte_same() check
25104969c119SAndrea Arcangeli 		 * (to avoid false positives from pte_same). For
25114969c119SAndrea Arcangeli 		 * further safety release the lock after the swap_free
25124969c119SAndrea Arcangeli 		 * so that the swap count won't change under a
25134969c119SAndrea Arcangeli 		 * parallel locked swapcache.
25144969c119SAndrea Arcangeli 		 */
25154969c119SAndrea Arcangeli 		unlock_page(swapcache);
25164969c119SAndrea Arcangeli 		page_cache_release(swapcache);
25174969c119SAndrea Arcangeli 	}
2518c475a8abSHugh Dickins 
251930c9f3a9SLinus Torvalds 	if (flags & FAULT_FLAG_WRITE) {
252061469f1dSHugh Dickins 		ret |= do_wp_page(mm, vma, address, page_table, pmd, ptl, pte);
252161469f1dSHugh Dickins 		if (ret & VM_FAULT_ERROR)
252261469f1dSHugh Dickins 			ret &= VM_FAULT_ERROR;
25231da177e4SLinus Torvalds 		goto out;
25241da177e4SLinus Torvalds 	}
25251da177e4SLinus Torvalds 
25261da177e4SLinus Torvalds 	/* No need to invalidate - it was non-present before */
25274b3073e1SRussell King 	update_mmu_cache(vma, address, page_table);
252865500d23SHugh Dickins unlock:
25298f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
25301da177e4SLinus Torvalds out:
25311da177e4SLinus Torvalds 	return ret;
2532b8107480SKirill Korotaev out_nomap:
253300501b53SJohannes Weiner 	mem_cgroup_cancel_charge(page, memcg);
25348f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
2535bc43f75cSJohannes Weiner out_page:
2536b8107480SKirill Korotaev 	unlock_page(page);
25374779cb31SAndi Kleen out_release:
2538b8107480SKirill Korotaev 	page_cache_release(page);
253956f31801SHugh Dickins 	if (page != swapcache) {
25404969c119SAndrea Arcangeli 		unlock_page(swapcache);
25414969c119SAndrea Arcangeli 		page_cache_release(swapcache);
25424969c119SAndrea Arcangeli 	}
254365500d23SHugh Dickins 	return ret;
25441da177e4SLinus Torvalds }
25451da177e4SLinus Torvalds 
25461da177e4SLinus Torvalds /*
25478ca3eb08SLuck, Tony  * This is like a special single-page "expand_{down|up}wards()",
25488ca3eb08SLuck, Tony  * except we must first make sure that 'address{-|+}PAGE_SIZE'
2549320b2b8dSLinus Torvalds  * doesn't hit another vma.
2550320b2b8dSLinus Torvalds  */
2551320b2b8dSLinus Torvalds static inline int check_stack_guard_page(struct vm_area_struct *vma, unsigned long address)
2552320b2b8dSLinus Torvalds {
2553320b2b8dSLinus Torvalds 	address &= PAGE_MASK;
2554320b2b8dSLinus Torvalds 	if ((vma->vm_flags & VM_GROWSDOWN) && address == vma->vm_start) {
25550e8e50e2SLinus Torvalds 		struct vm_area_struct *prev = vma->vm_prev;
2556320b2b8dSLinus Torvalds 
25570e8e50e2SLinus Torvalds 		/*
25580e8e50e2SLinus Torvalds 		 * Is there a mapping abutting this one below?
25590e8e50e2SLinus Torvalds 		 *
25600e8e50e2SLinus Torvalds 		 * That's only ok if it's the same stack mapping
25610e8e50e2SLinus Torvalds 		 * that has gotten split..
25620e8e50e2SLinus Torvalds 		 */
25630e8e50e2SLinus Torvalds 		if (prev && prev->vm_end == address)
25640e8e50e2SLinus Torvalds 			return prev->vm_flags & VM_GROWSDOWN ? 0 : -ENOMEM;
25650e8e50e2SLinus Torvalds 
2566fee7e49dSLinus Torvalds 		return expand_downwards(vma, address - PAGE_SIZE);
2567320b2b8dSLinus Torvalds 	}
25688ca3eb08SLuck, Tony 	if ((vma->vm_flags & VM_GROWSUP) && address + PAGE_SIZE == vma->vm_end) {
25698ca3eb08SLuck, Tony 		struct vm_area_struct *next = vma->vm_next;
25708ca3eb08SLuck, Tony 
25718ca3eb08SLuck, Tony 		/* As VM_GROWSDOWN but s/below/above/ */
25728ca3eb08SLuck, Tony 		if (next && next->vm_start == address + PAGE_SIZE)
25738ca3eb08SLuck, Tony 			return next->vm_flags & VM_GROWSUP ? 0 : -ENOMEM;
25748ca3eb08SLuck, Tony 
2575fee7e49dSLinus Torvalds 		return expand_upwards(vma, address + PAGE_SIZE);
25768ca3eb08SLuck, Tony 	}
2577320b2b8dSLinus Torvalds 	return 0;
2578320b2b8dSLinus Torvalds }
2579320b2b8dSLinus Torvalds 
2580320b2b8dSLinus Torvalds /*
25818f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
25828f4e2101SHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
25838f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
25841da177e4SLinus Torvalds  */
258565500d23SHugh Dickins static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
258665500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
258730c9f3a9SLinus Torvalds 		unsigned int flags)
25881da177e4SLinus Torvalds {
258900501b53SJohannes Weiner 	struct mem_cgroup *memcg;
25908f4e2101SHugh Dickins 	struct page *page;
25918f4e2101SHugh Dickins 	spinlock_t *ptl;
25921da177e4SLinus Torvalds 	pte_t entry;
25931da177e4SLinus Torvalds 
25945528f913SLinus Torvalds 	pte_unmap(page_table);
2595320b2b8dSLinus Torvalds 
259611ac5524SLinus Torvalds 	/* Check if we need to add a guard page to the stack */
259711ac5524SLinus Torvalds 	if (check_stack_guard_page(vma, address) < 0)
25989c145c56SLinus Torvalds 		return VM_FAULT_SIGSEGV;
259911ac5524SLinus Torvalds 
260011ac5524SLinus Torvalds 	/* Use the zero-page for reads */
2601593befa6SDominik Dingel 	if (!(flags & FAULT_FLAG_WRITE) && !mm_forbids_zeropage(mm)) {
260262eede62SHugh Dickins 		entry = pte_mkspecial(pfn_pte(my_zero_pfn(address),
260362eede62SHugh Dickins 						vma->vm_page_prot));
260411ac5524SLinus Torvalds 		page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
2605a13ea5b7SHugh Dickins 		if (!pte_none(*page_table))
2606a13ea5b7SHugh Dickins 			goto unlock;
2607a13ea5b7SHugh Dickins 		goto setpte;
2608a13ea5b7SHugh Dickins 	}
2609a13ea5b7SHugh Dickins 
26101da177e4SLinus Torvalds 	/* Allocate our own private page. */
26111da177e4SLinus Torvalds 	if (unlikely(anon_vma_prepare(vma)))
261265500d23SHugh Dickins 		goto oom;
2613769848c0SMel Gorman 	page = alloc_zeroed_user_highpage_movable(vma, address);
26141da177e4SLinus Torvalds 	if (!page)
261565500d23SHugh Dickins 		goto oom;
261652f37629SMinchan Kim 	/*
261752f37629SMinchan Kim 	 * The memory barrier inside __SetPageUptodate makes sure that
261852f37629SMinchan Kim 	 * preceeding stores to the page contents become visible before
261952f37629SMinchan Kim 	 * the set_pte_at() write.
262052f37629SMinchan Kim 	 */
26210ed361deSNick Piggin 	__SetPageUptodate(page);
26221da177e4SLinus Torvalds 
262300501b53SJohannes Weiner 	if (mem_cgroup_try_charge(page, mm, GFP_KERNEL, &memcg))
26248a9f3ccdSBalbir Singh 		goto oom_free_page;
26258a9f3ccdSBalbir Singh 
262665500d23SHugh Dickins 	entry = mk_pte(page, vma->vm_page_prot);
26271ac0cb5dSHugh Dickins 	if (vma->vm_flags & VM_WRITE)
26281ac0cb5dSHugh Dickins 		entry = pte_mkwrite(pte_mkdirty(entry));
26298f4e2101SHugh Dickins 
26308f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
26311c2fb7a4SAndrea Arcangeli 	if (!pte_none(*page_table))
26328f4e2101SHugh Dickins 		goto release;
26339ba69294SHugh Dickins 
263434e55232SKAMEZAWA Hiroyuki 	inc_mm_counter_fast(mm, MM_ANONPAGES);
26359617d95eSNick Piggin 	page_add_new_anon_rmap(page, vma, address);
263600501b53SJohannes Weiner 	mem_cgroup_commit_charge(page, memcg, false);
263700501b53SJohannes Weiner 	lru_cache_add_active_or_unevictable(page, vma);
2638a13ea5b7SHugh Dickins setpte:
263965500d23SHugh Dickins 	set_pte_at(mm, address, page_table, entry);
26401da177e4SLinus Torvalds 
26411da177e4SLinus Torvalds 	/* No need to invalidate - it was non-present before */
26424b3073e1SRussell King 	update_mmu_cache(vma, address, page_table);
264365500d23SHugh Dickins unlock:
26448f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
264583c54070SNick Piggin 	return 0;
26468f4e2101SHugh Dickins release:
264700501b53SJohannes Weiner 	mem_cgroup_cancel_charge(page, memcg);
26488f4e2101SHugh Dickins 	page_cache_release(page);
26498f4e2101SHugh Dickins 	goto unlock;
26508a9f3ccdSBalbir Singh oom_free_page:
26516dbf6d3bSHugh Dickins 	page_cache_release(page);
265265500d23SHugh Dickins oom:
26531da177e4SLinus Torvalds 	return VM_FAULT_OOM;
26541da177e4SLinus Torvalds }
26551da177e4SLinus Torvalds 
26569a95f3cfSPaul Cassella /*
26579a95f3cfSPaul Cassella  * The mmap_sem must have been held on entry, and may have been
26589a95f3cfSPaul Cassella  * released depending on flags and vma->vm_ops->fault() return value.
26599a95f3cfSPaul Cassella  * See filemap_fault() and __lock_page_retry().
26609a95f3cfSPaul Cassella  */
26617eae74afSKirill A. Shutemov static int __do_fault(struct vm_area_struct *vma, unsigned long address,
26622e4cdab0SMatthew Wilcox 			pgoff_t pgoff, unsigned int flags,
26632e4cdab0SMatthew Wilcox 			struct page *cow_page, struct page **page)
26647eae74afSKirill A. Shutemov {
26657eae74afSKirill A. Shutemov 	struct vm_fault vmf;
26667eae74afSKirill A. Shutemov 	int ret;
26677eae74afSKirill A. Shutemov 
26687eae74afSKirill A. Shutemov 	vmf.virtual_address = (void __user *)(address & PAGE_MASK);
26697eae74afSKirill A. Shutemov 	vmf.pgoff = pgoff;
26707eae74afSKirill A. Shutemov 	vmf.flags = flags;
26717eae74afSKirill A. Shutemov 	vmf.page = NULL;
26722e4cdab0SMatthew Wilcox 	vmf.cow_page = cow_page;
26737eae74afSKirill A. Shutemov 
26747eae74afSKirill A. Shutemov 	ret = vma->vm_ops->fault(vma, &vmf);
26757eae74afSKirill A. Shutemov 	if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
26767eae74afSKirill A. Shutemov 		return ret;
26772e4cdab0SMatthew Wilcox 	if (!vmf.page)
26782e4cdab0SMatthew Wilcox 		goto out;
26797eae74afSKirill A. Shutemov 
26807eae74afSKirill A. Shutemov 	if (unlikely(PageHWPoison(vmf.page))) {
26817eae74afSKirill A. Shutemov 		if (ret & VM_FAULT_LOCKED)
26827eae74afSKirill A. Shutemov 			unlock_page(vmf.page);
26837eae74afSKirill A. Shutemov 		page_cache_release(vmf.page);
26847eae74afSKirill A. Shutemov 		return VM_FAULT_HWPOISON;
26857eae74afSKirill A. Shutemov 	}
26867eae74afSKirill A. Shutemov 
26877eae74afSKirill A. Shutemov 	if (unlikely(!(ret & VM_FAULT_LOCKED)))
26887eae74afSKirill A. Shutemov 		lock_page(vmf.page);
26897eae74afSKirill A. Shutemov 	else
26907eae74afSKirill A. Shutemov 		VM_BUG_ON_PAGE(!PageLocked(vmf.page), vmf.page);
26917eae74afSKirill A. Shutemov 
26922e4cdab0SMatthew Wilcox  out:
26937eae74afSKirill A. Shutemov 	*page = vmf.page;
26947eae74afSKirill A. Shutemov 	return ret;
26957eae74afSKirill A. Shutemov }
26967eae74afSKirill A. Shutemov 
26978c6e50b0SKirill A. Shutemov /**
26988c6e50b0SKirill A. Shutemov  * do_set_pte - setup new PTE entry for given page and add reverse page mapping.
26998c6e50b0SKirill A. Shutemov  *
27008c6e50b0SKirill A. Shutemov  * @vma: virtual memory area
27018c6e50b0SKirill A. Shutemov  * @address: user virtual address
27028c6e50b0SKirill A. Shutemov  * @page: page to map
27038c6e50b0SKirill A. Shutemov  * @pte: pointer to target page table entry
27048c6e50b0SKirill A. Shutemov  * @write: true, if new entry is writable
27058c6e50b0SKirill A. Shutemov  * @anon: true, if it's anonymous page
27068c6e50b0SKirill A. Shutemov  *
27078c6e50b0SKirill A. Shutemov  * Caller must hold page table lock relevant for @pte.
27088c6e50b0SKirill A. Shutemov  *
27098c6e50b0SKirill A. Shutemov  * Target users are page handler itself and implementations of
27108c6e50b0SKirill A. Shutemov  * vm_ops->map_pages.
27118c6e50b0SKirill A. Shutemov  */
27128c6e50b0SKirill A. Shutemov void do_set_pte(struct vm_area_struct *vma, unsigned long address,
27133bb97794SKirill A. Shutemov 		struct page *page, pte_t *pte, bool write, bool anon)
27143bb97794SKirill A. Shutemov {
27153bb97794SKirill A. Shutemov 	pte_t entry;
27163bb97794SKirill A. Shutemov 
27173bb97794SKirill A. Shutemov 	flush_icache_page(vma, page);
27183bb97794SKirill A. Shutemov 	entry = mk_pte(page, vma->vm_page_prot);
27193bb97794SKirill A. Shutemov 	if (write)
27203bb97794SKirill A. Shutemov 		entry = maybe_mkwrite(pte_mkdirty(entry), vma);
27213bb97794SKirill A. Shutemov 	if (anon) {
27223bb97794SKirill A. Shutemov 		inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
27233bb97794SKirill A. Shutemov 		page_add_new_anon_rmap(page, vma, address);
27243bb97794SKirill A. Shutemov 	} else {
27253bb97794SKirill A. Shutemov 		inc_mm_counter_fast(vma->vm_mm, MM_FILEPAGES);
27263bb97794SKirill A. Shutemov 		page_add_file_rmap(page);
27273bb97794SKirill A. Shutemov 	}
27283bb97794SKirill A. Shutemov 	set_pte_at(vma->vm_mm, address, pte, entry);
27293bb97794SKirill A. Shutemov 
27303bb97794SKirill A. Shutemov 	/* no need to invalidate: a not-present page won't be cached */
27313bb97794SKirill A. Shutemov 	update_mmu_cache(vma, address, pte);
27323bb97794SKirill A. Shutemov }
27333bb97794SKirill A. Shutemov 
27343a91053aSKirill A. Shutemov static unsigned long fault_around_bytes __read_mostly =
27353a91053aSKirill A. Shutemov 	rounddown_pow_of_two(65536);
2736a9b0f861SKirill A. Shutemov 
27371592eef0SKirill A. Shutemov #ifdef CONFIG_DEBUG_FS
2738a9b0f861SKirill A. Shutemov static int fault_around_bytes_get(void *data, u64 *val)
27391592eef0SKirill A. Shutemov {
2740a9b0f861SKirill A. Shutemov 	*val = fault_around_bytes;
27411592eef0SKirill A. Shutemov 	return 0;
27421592eef0SKirill A. Shutemov }
27431592eef0SKirill A. Shutemov 
2744b4903d6eSAndrey Ryabinin /*
2745b4903d6eSAndrey Ryabinin  * fault_around_pages() and fault_around_mask() expects fault_around_bytes
2746b4903d6eSAndrey Ryabinin  * rounded down to nearest page order. It's what do_fault_around() expects to
2747b4903d6eSAndrey Ryabinin  * see.
2748b4903d6eSAndrey Ryabinin  */
2749a9b0f861SKirill A. Shutemov static int fault_around_bytes_set(void *data, u64 val)
27501592eef0SKirill A. Shutemov {
2751a9b0f861SKirill A. Shutemov 	if (val / PAGE_SIZE > PTRS_PER_PTE)
27521592eef0SKirill A. Shutemov 		return -EINVAL;
2753b4903d6eSAndrey Ryabinin 	if (val > PAGE_SIZE)
2754b4903d6eSAndrey Ryabinin 		fault_around_bytes = rounddown_pow_of_two(val);
2755b4903d6eSAndrey Ryabinin 	else
2756b4903d6eSAndrey Ryabinin 		fault_around_bytes = PAGE_SIZE; /* rounddown_pow_of_two(0) is undefined */
27571592eef0SKirill A. Shutemov 	return 0;
27581592eef0SKirill A. Shutemov }
2759a9b0f861SKirill A. Shutemov DEFINE_SIMPLE_ATTRIBUTE(fault_around_bytes_fops,
2760a9b0f861SKirill A. Shutemov 		fault_around_bytes_get, fault_around_bytes_set, "%llu\n");
27611592eef0SKirill A. Shutemov 
27621592eef0SKirill A. Shutemov static int __init fault_around_debugfs(void)
27631592eef0SKirill A. Shutemov {
27641592eef0SKirill A. Shutemov 	void *ret;
27651592eef0SKirill A. Shutemov 
2766a9b0f861SKirill A. Shutemov 	ret = debugfs_create_file("fault_around_bytes", 0644, NULL, NULL,
2767a9b0f861SKirill A. Shutemov 			&fault_around_bytes_fops);
27681592eef0SKirill A. Shutemov 	if (!ret)
2769a9b0f861SKirill A. Shutemov 		pr_warn("Failed to create fault_around_bytes in debugfs");
27701592eef0SKirill A. Shutemov 	return 0;
27711592eef0SKirill A. Shutemov }
27721592eef0SKirill A. Shutemov late_initcall(fault_around_debugfs);
27731592eef0SKirill A. Shutemov #endif
27748c6e50b0SKirill A. Shutemov 
27751fdb412bSKirill A. Shutemov /*
27761fdb412bSKirill A. Shutemov  * do_fault_around() tries to map few pages around the fault address. The hope
27771fdb412bSKirill A. Shutemov  * is that the pages will be needed soon and this will lower the number of
27781fdb412bSKirill A. Shutemov  * faults to handle.
27791fdb412bSKirill A. Shutemov  *
27801fdb412bSKirill A. Shutemov  * It uses vm_ops->map_pages() to map the pages, which skips the page if it's
27811fdb412bSKirill A. Shutemov  * not ready to be mapped: not up-to-date, locked, etc.
27821fdb412bSKirill A. Shutemov  *
27831fdb412bSKirill A. Shutemov  * This function is called with the page table lock taken. In the split ptlock
27841fdb412bSKirill A. Shutemov  * case the page table lock only protects only those entries which belong to
27851fdb412bSKirill A. Shutemov  * the page table corresponding to the fault address.
27861fdb412bSKirill A. Shutemov  *
27871fdb412bSKirill A. Shutemov  * This function doesn't cross the VMA boundaries, in order to call map_pages()
27881fdb412bSKirill A. Shutemov  * only once.
27891fdb412bSKirill A. Shutemov  *
27901fdb412bSKirill A. Shutemov  * fault_around_pages() defines how many pages we'll try to map.
27911fdb412bSKirill A. Shutemov  * do_fault_around() expects it to return a power of two less than or equal to
27921fdb412bSKirill A. Shutemov  * PTRS_PER_PTE.
27931fdb412bSKirill A. Shutemov  *
27941fdb412bSKirill A. Shutemov  * The virtual address of the area that we map is naturally aligned to the
27951fdb412bSKirill A. Shutemov  * fault_around_pages() value (and therefore to page order).  This way it's
27961fdb412bSKirill A. Shutemov  * easier to guarantee that we don't cross page table boundaries.
27971fdb412bSKirill A. Shutemov  */
27988c6e50b0SKirill A. Shutemov static void do_fault_around(struct vm_area_struct *vma, unsigned long address,
27998c6e50b0SKirill A. Shutemov 		pte_t *pte, pgoff_t pgoff, unsigned int flags)
28008c6e50b0SKirill A. Shutemov {
2801aecd6f44SKirill A. Shutemov 	unsigned long start_addr, nr_pages, mask;
28028c6e50b0SKirill A. Shutemov 	pgoff_t max_pgoff;
28038c6e50b0SKirill A. Shutemov 	struct vm_fault vmf;
28048c6e50b0SKirill A. Shutemov 	int off;
28058c6e50b0SKirill A. Shutemov 
2806aecd6f44SKirill A. Shutemov 	nr_pages = ACCESS_ONCE(fault_around_bytes) >> PAGE_SHIFT;
2807aecd6f44SKirill A. Shutemov 	mask = ~(nr_pages * PAGE_SIZE - 1) & PAGE_MASK;
2808aecd6f44SKirill A. Shutemov 
2809aecd6f44SKirill A. Shutemov 	start_addr = max(address & mask, vma->vm_start);
28108c6e50b0SKirill A. Shutemov 	off = ((address - start_addr) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
28118c6e50b0SKirill A. Shutemov 	pte -= off;
28128c6e50b0SKirill A. Shutemov 	pgoff -= off;
28138c6e50b0SKirill A. Shutemov 
28148c6e50b0SKirill A. Shutemov 	/*
28158c6e50b0SKirill A. Shutemov 	 *  max_pgoff is either end of page table or end of vma
2816850e9c69SKirill A. Shutemov 	 *  or fault_around_pages() from pgoff, depending what is nearest.
28178c6e50b0SKirill A. Shutemov 	 */
28188c6e50b0SKirill A. Shutemov 	max_pgoff = pgoff - ((start_addr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) +
28198c6e50b0SKirill A. Shutemov 		PTRS_PER_PTE - 1;
28208c6e50b0SKirill A. Shutemov 	max_pgoff = min3(max_pgoff, vma_pages(vma) + vma->vm_pgoff - 1,
2821aecd6f44SKirill A. Shutemov 			pgoff + nr_pages - 1);
28228c6e50b0SKirill A. Shutemov 
28238c6e50b0SKirill A. Shutemov 	/* Check if it makes any sense to call ->map_pages */
28248c6e50b0SKirill A. Shutemov 	while (!pte_none(*pte)) {
28258c6e50b0SKirill A. Shutemov 		if (++pgoff > max_pgoff)
28268c6e50b0SKirill A. Shutemov 			return;
28278c6e50b0SKirill A. Shutemov 		start_addr += PAGE_SIZE;
28288c6e50b0SKirill A. Shutemov 		if (start_addr >= vma->vm_end)
28298c6e50b0SKirill A. Shutemov 			return;
28308c6e50b0SKirill A. Shutemov 		pte++;
28318c6e50b0SKirill A. Shutemov 	}
28328c6e50b0SKirill A. Shutemov 
28338c6e50b0SKirill A. Shutemov 	vmf.virtual_address = (void __user *) start_addr;
28348c6e50b0SKirill A. Shutemov 	vmf.pte = pte;
28358c6e50b0SKirill A. Shutemov 	vmf.pgoff = pgoff;
28368c6e50b0SKirill A. Shutemov 	vmf.max_pgoff = max_pgoff;
28378c6e50b0SKirill A. Shutemov 	vmf.flags = flags;
28388c6e50b0SKirill A. Shutemov 	vma->vm_ops->map_pages(vma, &vmf);
28398c6e50b0SKirill A. Shutemov }
28408c6e50b0SKirill A. Shutemov 
2841e655fb29SKirill A. Shutemov static int do_read_fault(struct mm_struct *mm, struct vm_area_struct *vma,
2842e655fb29SKirill A. Shutemov 		unsigned long address, pmd_t *pmd,
2843e655fb29SKirill A. Shutemov 		pgoff_t pgoff, unsigned int flags, pte_t orig_pte)
2844e655fb29SKirill A. Shutemov {
2845e655fb29SKirill A. Shutemov 	struct page *fault_page;
2846e655fb29SKirill A. Shutemov 	spinlock_t *ptl;
28473bb97794SKirill A. Shutemov 	pte_t *pte;
28488c6e50b0SKirill A. Shutemov 	int ret = 0;
28498c6e50b0SKirill A. Shutemov 
28508c6e50b0SKirill A. Shutemov 	/*
28518c6e50b0SKirill A. Shutemov 	 * Let's call ->map_pages() first and use ->fault() as fallback
28528c6e50b0SKirill A. Shutemov 	 * if page by the offset is not ready to be mapped (cold cache or
28538c6e50b0SKirill A. Shutemov 	 * something).
28548c6e50b0SKirill A. Shutemov 	 */
28559b4bdd2fSKirill A. Shutemov 	if (vma->vm_ops->map_pages && fault_around_bytes >> PAGE_SHIFT > 1) {
28568c6e50b0SKirill A. Shutemov 		pte = pte_offset_map_lock(mm, pmd, address, &ptl);
28578c6e50b0SKirill A. Shutemov 		do_fault_around(vma, address, pte, pgoff, flags);
28588c6e50b0SKirill A. Shutemov 		if (!pte_same(*pte, orig_pte))
28598c6e50b0SKirill A. Shutemov 			goto unlock_out;
28608c6e50b0SKirill A. Shutemov 		pte_unmap_unlock(pte, ptl);
28618c6e50b0SKirill A. Shutemov 	}
2862e655fb29SKirill A. Shutemov 
28632e4cdab0SMatthew Wilcox 	ret = __do_fault(vma, address, pgoff, flags, NULL, &fault_page);
2864e655fb29SKirill A. Shutemov 	if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
2865e655fb29SKirill A. Shutemov 		return ret;
2866e655fb29SKirill A. Shutemov 
2867e655fb29SKirill A. Shutemov 	pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2868e655fb29SKirill A. Shutemov 	if (unlikely(!pte_same(*pte, orig_pte))) {
2869e655fb29SKirill A. Shutemov 		pte_unmap_unlock(pte, ptl);
2870e655fb29SKirill A. Shutemov 		unlock_page(fault_page);
2871e655fb29SKirill A. Shutemov 		page_cache_release(fault_page);
2872e655fb29SKirill A. Shutemov 		return ret;
2873e655fb29SKirill A. Shutemov 	}
28743bb97794SKirill A. Shutemov 	do_set_pte(vma, address, fault_page, pte, false, false);
2875e655fb29SKirill A. Shutemov 	unlock_page(fault_page);
28768c6e50b0SKirill A. Shutemov unlock_out:
28778c6e50b0SKirill A. Shutemov 	pte_unmap_unlock(pte, ptl);
2878e655fb29SKirill A. Shutemov 	return ret;
2879e655fb29SKirill A. Shutemov }
2880e655fb29SKirill A. Shutemov 
2881ec47c3b9SKirill A. Shutemov static int do_cow_fault(struct mm_struct *mm, struct vm_area_struct *vma,
2882ec47c3b9SKirill A. Shutemov 		unsigned long address, pmd_t *pmd,
2883ec47c3b9SKirill A. Shutemov 		pgoff_t pgoff, unsigned int flags, pte_t orig_pte)
2884ec47c3b9SKirill A. Shutemov {
2885ec47c3b9SKirill A. Shutemov 	struct page *fault_page, *new_page;
288600501b53SJohannes Weiner 	struct mem_cgroup *memcg;
2887ec47c3b9SKirill A. Shutemov 	spinlock_t *ptl;
28883bb97794SKirill A. Shutemov 	pte_t *pte;
2889ec47c3b9SKirill A. Shutemov 	int ret;
2890ec47c3b9SKirill A. Shutemov 
2891ec47c3b9SKirill A. Shutemov 	if (unlikely(anon_vma_prepare(vma)))
2892ec47c3b9SKirill A. Shutemov 		return VM_FAULT_OOM;
2893ec47c3b9SKirill A. Shutemov 
2894ec47c3b9SKirill A. Shutemov 	new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
2895ec47c3b9SKirill A. Shutemov 	if (!new_page)
2896ec47c3b9SKirill A. Shutemov 		return VM_FAULT_OOM;
2897ec47c3b9SKirill A. Shutemov 
289800501b53SJohannes Weiner 	if (mem_cgroup_try_charge(new_page, mm, GFP_KERNEL, &memcg)) {
2899ec47c3b9SKirill A. Shutemov 		page_cache_release(new_page);
2900ec47c3b9SKirill A. Shutemov 		return VM_FAULT_OOM;
2901ec47c3b9SKirill A. Shutemov 	}
2902ec47c3b9SKirill A. Shutemov 
29032e4cdab0SMatthew Wilcox 	ret = __do_fault(vma, address, pgoff, flags, new_page, &fault_page);
2904ec47c3b9SKirill A. Shutemov 	if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
2905ec47c3b9SKirill A. Shutemov 		goto uncharge_out;
2906ec47c3b9SKirill A. Shutemov 
29072e4cdab0SMatthew Wilcox 	if (fault_page)
2908ec47c3b9SKirill A. Shutemov 		copy_user_highpage(new_page, fault_page, address, vma);
2909ec47c3b9SKirill A. Shutemov 	__SetPageUptodate(new_page);
2910ec47c3b9SKirill A. Shutemov 
2911ec47c3b9SKirill A. Shutemov 	pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2912ec47c3b9SKirill A. Shutemov 	if (unlikely(!pte_same(*pte, orig_pte))) {
2913ec47c3b9SKirill A. Shutemov 		pte_unmap_unlock(pte, ptl);
29142e4cdab0SMatthew Wilcox 		if (fault_page) {
2915ec47c3b9SKirill A. Shutemov 			unlock_page(fault_page);
2916ec47c3b9SKirill A. Shutemov 			page_cache_release(fault_page);
29172e4cdab0SMatthew Wilcox 		} else {
29182e4cdab0SMatthew Wilcox 			/*
29192e4cdab0SMatthew Wilcox 			 * The fault handler has no page to lock, so it holds
29202e4cdab0SMatthew Wilcox 			 * i_mmap_lock for read to protect against truncate.
29212e4cdab0SMatthew Wilcox 			 */
29222e4cdab0SMatthew Wilcox 			i_mmap_unlock_read(vma->vm_file->f_mapping);
29232e4cdab0SMatthew Wilcox 		}
2924ec47c3b9SKirill A. Shutemov 		goto uncharge_out;
2925ec47c3b9SKirill A. Shutemov 	}
29263bb97794SKirill A. Shutemov 	do_set_pte(vma, address, new_page, pte, true, true);
292700501b53SJohannes Weiner 	mem_cgroup_commit_charge(new_page, memcg, false);
292800501b53SJohannes Weiner 	lru_cache_add_active_or_unevictable(new_page, vma);
2929ec47c3b9SKirill A. Shutemov 	pte_unmap_unlock(pte, ptl);
29302e4cdab0SMatthew Wilcox 	if (fault_page) {
2931ec47c3b9SKirill A. Shutemov 		unlock_page(fault_page);
2932ec47c3b9SKirill A. Shutemov 		page_cache_release(fault_page);
29332e4cdab0SMatthew Wilcox 	} else {
29342e4cdab0SMatthew Wilcox 		/*
29352e4cdab0SMatthew Wilcox 		 * The fault handler has no page to lock, so it holds
29362e4cdab0SMatthew Wilcox 		 * i_mmap_lock for read to protect against truncate.
29372e4cdab0SMatthew Wilcox 		 */
29382e4cdab0SMatthew Wilcox 		i_mmap_unlock_read(vma->vm_file->f_mapping);
29392e4cdab0SMatthew Wilcox 	}
2940ec47c3b9SKirill A. Shutemov 	return ret;
2941ec47c3b9SKirill A. Shutemov uncharge_out:
294200501b53SJohannes Weiner 	mem_cgroup_cancel_charge(new_page, memcg);
2943ec47c3b9SKirill A. Shutemov 	page_cache_release(new_page);
2944ec47c3b9SKirill A. Shutemov 	return ret;
2945ec47c3b9SKirill A. Shutemov }
2946ec47c3b9SKirill A. Shutemov 
2947f0c6d4d2SKirill A. Shutemov static int do_shared_fault(struct mm_struct *mm, struct vm_area_struct *vma,
294816abfa08SHugh Dickins 		unsigned long address, pmd_t *pmd,
294954cb8821SNick Piggin 		pgoff_t pgoff, unsigned int flags, pte_t orig_pte)
29501da177e4SLinus Torvalds {
2951f0c6d4d2SKirill A. Shutemov 	struct page *fault_page;
2952f0c6d4d2SKirill A. Shutemov 	struct address_space *mapping;
29538f4e2101SHugh Dickins 	spinlock_t *ptl;
29543bb97794SKirill A. Shutemov 	pte_t *pte;
2955f0c6d4d2SKirill A. Shutemov 	int dirtied = 0;
2956f0c6d4d2SKirill A. Shutemov 	int ret, tmp;
29571d65f86dSKAMEZAWA Hiroyuki 
29582e4cdab0SMatthew Wilcox 	ret = __do_fault(vma, address, pgoff, flags, NULL, &fault_page);
29597eae74afSKirill A. Shutemov 	if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
2960f0c6d4d2SKirill A. Shutemov 		return ret;
29611da177e4SLinus Torvalds 
29621da177e4SLinus Torvalds 	/*
2963f0c6d4d2SKirill A. Shutemov 	 * Check if the backing address space wants to know that the page is
2964f0c6d4d2SKirill A. Shutemov 	 * about to become writable
29651da177e4SLinus Torvalds 	 */
2966fb09a464SKirill A. Shutemov 	if (vma->vm_ops->page_mkwrite) {
2967f0c6d4d2SKirill A. Shutemov 		unlock_page(fault_page);
2968fb09a464SKirill A. Shutemov 		tmp = do_page_mkwrite(vma, fault_page, address);
2969fb09a464SKirill A. Shutemov 		if (unlikely(!tmp ||
2970fb09a464SKirill A. Shutemov 				(tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
2971f0c6d4d2SKirill A. Shutemov 			page_cache_release(fault_page);
2972f0c6d4d2SKirill A. Shutemov 			return tmp;
297369676147SMark Fasheh 		}
2974d0217ac0SNick Piggin 	}
2975fb09a464SKirill A. Shutemov 
2976f0c6d4d2SKirill A. Shutemov 	pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2977f0c6d4d2SKirill A. Shutemov 	if (unlikely(!pte_same(*pte, orig_pte))) {
2978f0c6d4d2SKirill A. Shutemov 		pte_unmap_unlock(pte, ptl);
2979f0c6d4d2SKirill A. Shutemov 		unlock_page(fault_page);
2980f0c6d4d2SKirill A. Shutemov 		page_cache_release(fault_page);
2981f0c6d4d2SKirill A. Shutemov 		return ret;
29829637a5efSDavid Howells 	}
29833bb97794SKirill A. Shutemov 	do_set_pte(vma, address, fault_page, pte, true, false);
2984f0c6d4d2SKirill A. Shutemov 	pte_unmap_unlock(pte, ptl);
2985d00806b1SNick Piggin 
2986f0c6d4d2SKirill A. Shutemov 	if (set_page_dirty(fault_page))
298741c4d25fSJan Kara 		dirtied = 1;
2988d82fa87dSAndrew Morton 	/*
2989d82fa87dSAndrew Morton 	 * Take a local copy of the address_space - page.mapping may be zeroed
2990d82fa87dSAndrew Morton 	 * by truncate after unlock_page().   The address_space itself remains
2991d82fa87dSAndrew Morton 	 * pinned by vma->vm_file's reference.  We rely on unlock_page()'s
2992d82fa87dSAndrew Morton 	 * release semantics to prevent the compiler from undoing this copying.
2993d82fa87dSAndrew Morton 	 */
2994f0c6d4d2SKirill A. Shutemov 	mapping = fault_page->mapping;
2995f0c6d4d2SKirill A. Shutemov 	unlock_page(fault_page);
2996f0c6d4d2SKirill A. Shutemov 	if ((dirtied || vma->vm_ops->page_mkwrite) && mapping) {
2997b827e496SNick Piggin 		/*
2998b827e496SNick Piggin 		 * Some device drivers do not set page.mapping but still
2999b827e496SNick Piggin 		 * dirty their pages
3000b827e496SNick Piggin 		 */
3001b827e496SNick Piggin 		balance_dirty_pages_ratelimited(mapping);
3002d08b3851SPeter Zijlstra 	}
3003d00806b1SNick Piggin 
300474ec6751SJohannes Weiner 	if (!vma->vm_ops->page_mkwrite)
3005b827e496SNick Piggin 		file_update_time(vma->vm_file);
3006b827e496SNick Piggin 
3007b827e496SNick Piggin 	return ret;
300854cb8821SNick Piggin }
3009d00806b1SNick Piggin 
30109a95f3cfSPaul Cassella /*
30119a95f3cfSPaul Cassella  * We enter with non-exclusive mmap_sem (to exclude vma changes,
30129a95f3cfSPaul Cassella  * but allow concurrent faults).
30139a95f3cfSPaul Cassella  * The mmap_sem may have been released depending on flags and our
30149a95f3cfSPaul Cassella  * return value.  See filemap_fault() and __lock_page_or_retry().
30159a95f3cfSPaul Cassella  */
30169b4bdd2fSKirill A. Shutemov static int do_fault(struct mm_struct *mm, struct vm_area_struct *vma,
301754cb8821SNick Piggin 		unsigned long address, pte_t *page_table, pmd_t *pmd,
301830c9f3a9SLinus Torvalds 		unsigned int flags, pte_t orig_pte)
301954cb8821SNick Piggin {
302054cb8821SNick Piggin 	pgoff_t pgoff = (((address & PAGE_MASK)
30210da7e01fSDean Nelson 			- vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
302254cb8821SNick Piggin 
302316abfa08SHugh Dickins 	pte_unmap(page_table);
3024e655fb29SKirill A. Shutemov 	if (!(flags & FAULT_FLAG_WRITE))
3025e655fb29SKirill A. Shutemov 		return do_read_fault(mm, vma, address, pmd, pgoff, flags,
3026e655fb29SKirill A. Shutemov 				orig_pte);
3027ec47c3b9SKirill A. Shutemov 	if (!(vma->vm_flags & VM_SHARED))
3028ec47c3b9SKirill A. Shutemov 		return do_cow_fault(mm, vma, address, pmd, pgoff, flags,
3029ec47c3b9SKirill A. Shutemov 				orig_pte);
3030f0c6d4d2SKirill A. Shutemov 	return do_shared_fault(mm, vma, address, pmd, pgoff, flags, orig_pte);
303154cb8821SNick Piggin }
303254cb8821SNick Piggin 
3033b19a9939SRashika Kheria static int numa_migrate_prep(struct page *page, struct vm_area_struct *vma,
303404bb2f94SRik van Riel 				unsigned long addr, int page_nid,
303504bb2f94SRik van Riel 				int *flags)
30369532fec1SMel Gorman {
30379532fec1SMel Gorman 	get_page(page);
30389532fec1SMel Gorman 
30399532fec1SMel Gorman 	count_vm_numa_event(NUMA_HINT_FAULTS);
304004bb2f94SRik van Riel 	if (page_nid == numa_node_id()) {
30419532fec1SMel Gorman 		count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
304204bb2f94SRik van Riel 		*flags |= TNF_FAULT_LOCAL;
304304bb2f94SRik van Riel 	}
30449532fec1SMel Gorman 
30459532fec1SMel Gorman 	return mpol_misplaced(page, vma, addr);
30469532fec1SMel Gorman }
30479532fec1SMel Gorman 
3048b19a9939SRashika Kheria static int do_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
3049d10e63f2SMel Gorman 		   unsigned long addr, pte_t pte, pte_t *ptep, pmd_t *pmd)
3050d10e63f2SMel Gorman {
30514daae3b4SMel Gorman 	struct page *page = NULL;
3052d10e63f2SMel Gorman 	spinlock_t *ptl;
30538191acbdSMel Gorman 	int page_nid = -1;
305490572890SPeter Zijlstra 	int last_cpupid;
3055cbee9f88SPeter Zijlstra 	int target_nid;
3056b8593bfdSMel Gorman 	bool migrated = false;
3057b191f9b1SMel Gorman 	bool was_writable = pte_write(pte);
30586688cc05SPeter Zijlstra 	int flags = 0;
3059d10e63f2SMel Gorman 
3060c0e7cad9SMel Gorman 	/* A PROT_NONE fault should not end up here */
3061c0e7cad9SMel Gorman 	BUG_ON(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)));
3062c0e7cad9SMel Gorman 
3063d10e63f2SMel Gorman 	/*
3064d10e63f2SMel Gorman 	* The "pte" at this point cannot be used safely without
3065d10e63f2SMel Gorman 	* validation through pte_unmap_same(). It's of NUMA type but
3066d10e63f2SMel Gorman 	* the pfn may be screwed if the read is non atomic.
3067d10e63f2SMel Gorman 	*
30684d942466SMel Gorman 	* We can safely just do a "set_pte_at()", because the old
30694d942466SMel Gorman 	* page table entry is not accessible, so there would be no
30704d942466SMel Gorman 	* concurrent hardware modifications to the PTE.
3071d10e63f2SMel Gorman 	*/
3072d10e63f2SMel Gorman 	ptl = pte_lockptr(mm, pmd);
3073d10e63f2SMel Gorman 	spin_lock(ptl);
30744daae3b4SMel Gorman 	if (unlikely(!pte_same(*ptep, pte))) {
30754daae3b4SMel Gorman 		pte_unmap_unlock(ptep, ptl);
30764daae3b4SMel Gorman 		goto out;
30774daae3b4SMel Gorman 	}
30784daae3b4SMel Gorman 
30794d942466SMel Gorman 	/* Make it present again */
30804d942466SMel Gorman 	pte = pte_modify(pte, vma->vm_page_prot);
30814d942466SMel Gorman 	pte = pte_mkyoung(pte);
3082b191f9b1SMel Gorman 	if (was_writable)
3083b191f9b1SMel Gorman 		pte = pte_mkwrite(pte);
3084d10e63f2SMel Gorman 	set_pte_at(mm, addr, ptep, pte);
3085d10e63f2SMel Gorman 	update_mmu_cache(vma, addr, ptep);
3086d10e63f2SMel Gorman 
3087d10e63f2SMel Gorman 	page = vm_normal_page(vma, addr, pte);
3088d10e63f2SMel Gorman 	if (!page) {
3089d10e63f2SMel Gorman 		pte_unmap_unlock(ptep, ptl);
3090d10e63f2SMel Gorman 		return 0;
3091d10e63f2SMel Gorman 	}
3092d10e63f2SMel Gorman 
30936688cc05SPeter Zijlstra 	/*
3094bea66fbdSMel Gorman 	 * Avoid grouping on RO pages in general. RO pages shouldn't hurt as
3095bea66fbdSMel Gorman 	 * much anyway since they can be in shared cache state. This misses
3096bea66fbdSMel Gorman 	 * the case where a mapping is writable but the process never writes
3097bea66fbdSMel Gorman 	 * to it but pte_write gets cleared during protection updates and
3098bea66fbdSMel Gorman 	 * pte_dirty has unpredictable behaviour between PTE scan updates,
3099bea66fbdSMel Gorman 	 * background writeback, dirty balancing and application behaviour.
31006688cc05SPeter Zijlstra 	 */
3101bea66fbdSMel Gorman 	if (!(vma->vm_flags & VM_WRITE))
31026688cc05SPeter Zijlstra 		flags |= TNF_NO_GROUP;
31036688cc05SPeter Zijlstra 
3104dabe1d99SRik van Riel 	/*
3105dabe1d99SRik van Riel 	 * Flag if the page is shared between multiple address spaces. This
3106dabe1d99SRik van Riel 	 * is later used when determining whether to group tasks together
3107dabe1d99SRik van Riel 	 */
3108dabe1d99SRik van Riel 	if (page_mapcount(page) > 1 && (vma->vm_flags & VM_SHARED))
3109dabe1d99SRik van Riel 		flags |= TNF_SHARED;
3110dabe1d99SRik van Riel 
311190572890SPeter Zijlstra 	last_cpupid = page_cpupid_last(page);
31128191acbdSMel Gorman 	page_nid = page_to_nid(page);
311304bb2f94SRik van Riel 	target_nid = numa_migrate_prep(page, vma, addr, page_nid, &flags);
3114d10e63f2SMel Gorman 	pte_unmap_unlock(ptep, ptl);
31154daae3b4SMel Gorman 	if (target_nid == -1) {
31164daae3b4SMel Gorman 		put_page(page);
31174daae3b4SMel Gorman 		goto out;
31184daae3b4SMel Gorman 	}
31194daae3b4SMel Gorman 
31204daae3b4SMel Gorman 	/* Migrate to the requested node */
31211bc115d8SMel Gorman 	migrated = migrate_misplaced_page(page, vma, target_nid);
31226688cc05SPeter Zijlstra 	if (migrated) {
31238191acbdSMel Gorman 		page_nid = target_nid;
31246688cc05SPeter Zijlstra 		flags |= TNF_MIGRATED;
3125074c2381SMel Gorman 	} else
3126074c2381SMel Gorman 		flags |= TNF_MIGRATE_FAIL;
31274daae3b4SMel Gorman 
31284daae3b4SMel Gorman out:
31298191acbdSMel Gorman 	if (page_nid != -1)
31306688cc05SPeter Zijlstra 		task_numa_fault(last_cpupid, page_nid, 1, flags);
3131d10e63f2SMel Gorman 	return 0;
3132d10e63f2SMel Gorman }
3133d10e63f2SMel Gorman 
31341da177e4SLinus Torvalds /*
31351da177e4SLinus Torvalds  * These routines also need to handle stuff like marking pages dirty
31361da177e4SLinus Torvalds  * and/or accessed for architectures that don't do it in hardware (most
31371da177e4SLinus Torvalds  * RISC architectures).  The early dirtying is also good on the i386.
31381da177e4SLinus Torvalds  *
31391da177e4SLinus Torvalds  * There is also a hook called "update_mmu_cache()" that architectures
31401da177e4SLinus Torvalds  * with external mmu caches can use to update those (ie the Sparc or
31411da177e4SLinus Torvalds  * PowerPC hashed page tables that act as extended TLBs).
31421da177e4SLinus Torvalds  *
3143c74df32cSHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
3144c74df32cSHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
31459a95f3cfSPaul Cassella  * We return with pte unmapped and unlocked.
31469a95f3cfSPaul Cassella  *
31479a95f3cfSPaul Cassella  * The mmap_sem may have been released depending on flags and our
31489a95f3cfSPaul Cassella  * return value.  See filemap_fault() and __lock_page_or_retry().
31491da177e4SLinus Torvalds  */
3150c0292554SKirill A. Shutemov static int handle_pte_fault(struct mm_struct *mm,
31511da177e4SLinus Torvalds 		     struct vm_area_struct *vma, unsigned long address,
315230c9f3a9SLinus Torvalds 		     pte_t *pte, pmd_t *pmd, unsigned int flags)
31531da177e4SLinus Torvalds {
31541da177e4SLinus Torvalds 	pte_t entry;
31558f4e2101SHugh Dickins 	spinlock_t *ptl;
31561da177e4SLinus Torvalds 
3157e37c6982SChristian Borntraeger 	/*
3158e37c6982SChristian Borntraeger 	 * some architectures can have larger ptes than wordsize,
3159e37c6982SChristian Borntraeger 	 * e.g.ppc44x-defconfig has CONFIG_PTE_64BIT=y and CONFIG_32BIT=y,
3160e37c6982SChristian Borntraeger 	 * so READ_ONCE or ACCESS_ONCE cannot guarantee atomic accesses.
3161e37c6982SChristian Borntraeger 	 * The code below just needs a consistent view for the ifs and
3162e37c6982SChristian Borntraeger 	 * we later double check anyway with the ptl lock held. So here
3163e37c6982SChristian Borntraeger 	 * a barrier will do.
3164e37c6982SChristian Borntraeger 	 */
3165e37c6982SChristian Borntraeger 	entry = *pte;
3166e37c6982SChristian Borntraeger 	barrier();
31671da177e4SLinus Torvalds 	if (!pte_present(entry)) {
316865500d23SHugh Dickins 		if (pte_none(entry)) {
3169f4b81804SJes Sorensen 			if (vma->vm_ops) {
31703c18ddd1SNick Piggin 				if (likely(vma->vm_ops->fault))
31719b4bdd2fSKirill A. Shutemov 					return do_fault(mm, vma, address, pte,
31729b4bdd2fSKirill A. Shutemov 							pmd, flags, entry);
3173f4b81804SJes Sorensen 			}
3174f4b81804SJes Sorensen 			return do_anonymous_page(mm, vma, address,
317530c9f3a9SLinus Torvalds 						 pte, pmd, flags);
317665500d23SHugh Dickins 		}
317765500d23SHugh Dickins 		return do_swap_page(mm, vma, address,
317830c9f3a9SLinus Torvalds 					pte, pmd, flags, entry);
31791da177e4SLinus Torvalds 	}
31801da177e4SLinus Torvalds 
31818a0516edSMel Gorman 	if (pte_protnone(entry))
3182d10e63f2SMel Gorman 		return do_numa_page(mm, vma, address, entry, pte, pmd);
3183d10e63f2SMel Gorman 
31844c21e2f2SHugh Dickins 	ptl = pte_lockptr(mm, pmd);
31858f4e2101SHugh Dickins 	spin_lock(ptl);
31868f4e2101SHugh Dickins 	if (unlikely(!pte_same(*pte, entry)))
31878f4e2101SHugh Dickins 		goto unlock;
318830c9f3a9SLinus Torvalds 	if (flags & FAULT_FLAG_WRITE) {
31891da177e4SLinus Torvalds 		if (!pte_write(entry))
31908f4e2101SHugh Dickins 			return do_wp_page(mm, vma, address,
31918f4e2101SHugh Dickins 					pte, pmd, ptl, entry);
31921da177e4SLinus Torvalds 		entry = pte_mkdirty(entry);
31931da177e4SLinus Torvalds 	}
31941da177e4SLinus Torvalds 	entry = pte_mkyoung(entry);
319530c9f3a9SLinus Torvalds 	if (ptep_set_access_flags(vma, address, pte, entry, flags & FAULT_FLAG_WRITE)) {
31964b3073e1SRussell King 		update_mmu_cache(vma, address, pte);
31971a44e149SAndrea Arcangeli 	} else {
31981a44e149SAndrea Arcangeli 		/*
31991a44e149SAndrea Arcangeli 		 * This is needed only for protection faults but the arch code
32001a44e149SAndrea Arcangeli 		 * is not yet telling us if this is a protection fault or not.
32011a44e149SAndrea Arcangeli 		 * This still avoids useless tlb flushes for .text page faults
32021a44e149SAndrea Arcangeli 		 * with threads.
32031a44e149SAndrea Arcangeli 		 */
320430c9f3a9SLinus Torvalds 		if (flags & FAULT_FLAG_WRITE)
320561c77326SShaohua Li 			flush_tlb_fix_spurious_fault(vma, address);
32061a44e149SAndrea Arcangeli 	}
32078f4e2101SHugh Dickins unlock:
32088f4e2101SHugh Dickins 	pte_unmap_unlock(pte, ptl);
320983c54070SNick Piggin 	return 0;
32101da177e4SLinus Torvalds }
32111da177e4SLinus Torvalds 
32121da177e4SLinus Torvalds /*
32131da177e4SLinus Torvalds  * By the time we get here, we already hold the mm semaphore
32149a95f3cfSPaul Cassella  *
32159a95f3cfSPaul Cassella  * The mmap_sem may have been released depending on flags and our
32169a95f3cfSPaul Cassella  * return value.  See filemap_fault() and __lock_page_or_retry().
32171da177e4SLinus Torvalds  */
3218519e5247SJohannes Weiner static int __handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
3219d06063ccSLinus Torvalds 			     unsigned long address, unsigned int flags)
32201da177e4SLinus Torvalds {
32211da177e4SLinus Torvalds 	pgd_t *pgd;
32221da177e4SLinus Torvalds 	pud_t *pud;
32231da177e4SLinus Torvalds 	pmd_t *pmd;
32241da177e4SLinus Torvalds 	pte_t *pte;
32251da177e4SLinus Torvalds 
3226ac9b9c66SHugh Dickins 	if (unlikely(is_vm_hugetlb_page(vma)))
322730c9f3a9SLinus Torvalds 		return hugetlb_fault(mm, vma, address, flags);
32281da177e4SLinus Torvalds 
32291da177e4SLinus Torvalds 	pgd = pgd_offset(mm, address);
32301da177e4SLinus Torvalds 	pud = pud_alloc(mm, pgd, address);
32311da177e4SLinus Torvalds 	if (!pud)
3232c74df32cSHugh Dickins 		return VM_FAULT_OOM;
32331da177e4SLinus Torvalds 	pmd = pmd_alloc(mm, pud, address);
32341da177e4SLinus Torvalds 	if (!pmd)
3235c74df32cSHugh Dickins 		return VM_FAULT_OOM;
323671e3aac0SAndrea Arcangeli 	if (pmd_none(*pmd) && transparent_hugepage_enabled(vma)) {
3237c0292554SKirill A. Shutemov 		int ret = VM_FAULT_FALLBACK;
323871e3aac0SAndrea Arcangeli 		if (!vma->vm_ops)
3239c0292554SKirill A. Shutemov 			ret = do_huge_pmd_anonymous_page(mm, vma, address,
324071e3aac0SAndrea Arcangeli 					pmd, flags);
3241c0292554SKirill A. Shutemov 		if (!(ret & VM_FAULT_FALLBACK))
3242c0292554SKirill A. Shutemov 			return ret;
324371e3aac0SAndrea Arcangeli 	} else {
324471e3aac0SAndrea Arcangeli 		pmd_t orig_pmd = *pmd;
32451f1d06c3SDavid Rientjes 		int ret;
32461f1d06c3SDavid Rientjes 
324771e3aac0SAndrea Arcangeli 		barrier();
324871e3aac0SAndrea Arcangeli 		if (pmd_trans_huge(orig_pmd)) {
3249a1dd450bSWill Deacon 			unsigned int dirty = flags & FAULT_FLAG_WRITE;
3250a1dd450bSWill Deacon 
3251e53289c0SLinus Torvalds 			/*
3252e53289c0SLinus Torvalds 			 * If the pmd is splitting, return and retry the
3253e53289c0SLinus Torvalds 			 * the fault.  Alternative: wait until the split
3254e53289c0SLinus Torvalds 			 * is done, and goto retry.
3255e53289c0SLinus Torvalds 			 */
3256e53289c0SLinus Torvalds 			if (pmd_trans_splitting(orig_pmd))
3257e53289c0SLinus Torvalds 				return 0;
3258e53289c0SLinus Torvalds 
32598a0516edSMel Gorman 			if (pmd_protnone(orig_pmd))
32604daae3b4SMel Gorman 				return do_huge_pmd_numa_page(mm, vma, address,
3261d10e63f2SMel Gorman 							     orig_pmd, pmd);
3262d10e63f2SMel Gorman 
32633d59eebcSLinus Torvalds 			if (dirty && !pmd_write(orig_pmd)) {
32641f1d06c3SDavid Rientjes 				ret = do_huge_pmd_wp_page(mm, vma, address, pmd,
32651f1d06c3SDavid Rientjes 							  orig_pmd);
32669845cbbdSKirill A. Shutemov 				if (!(ret & VM_FAULT_FALLBACK))
32671f1d06c3SDavid Rientjes 					return ret;
3268a1dd450bSWill Deacon 			} else {
3269a1dd450bSWill Deacon 				huge_pmd_set_accessed(mm, vma, address, pmd,
3270a1dd450bSWill Deacon 						      orig_pmd, dirty);
327171e3aac0SAndrea Arcangeli 				return 0;
327271e3aac0SAndrea Arcangeli 			}
327371e3aac0SAndrea Arcangeli 		}
32749845cbbdSKirill A. Shutemov 	}
327571e3aac0SAndrea Arcangeli 
327671e3aac0SAndrea Arcangeli 	/*
327771e3aac0SAndrea Arcangeli 	 * Use __pte_alloc instead of pte_alloc_map, because we can't
327871e3aac0SAndrea Arcangeli 	 * run pte_offset_map on the pmd, if an huge pmd could
327971e3aac0SAndrea Arcangeli 	 * materialize from under us from a different thread.
328071e3aac0SAndrea Arcangeli 	 */
32814fd01770SMel Gorman 	if (unlikely(pmd_none(*pmd)) &&
32824fd01770SMel Gorman 	    unlikely(__pte_alloc(mm, vma, pmd, address)))
3283c74df32cSHugh Dickins 		return VM_FAULT_OOM;
328471e3aac0SAndrea Arcangeli 	/* if an huge pmd materialized from under us just retry later */
328571e3aac0SAndrea Arcangeli 	if (unlikely(pmd_trans_huge(*pmd)))
328671e3aac0SAndrea Arcangeli 		return 0;
328771e3aac0SAndrea Arcangeli 	/*
328871e3aac0SAndrea Arcangeli 	 * A regular pmd is established and it can't morph into a huge pmd
328971e3aac0SAndrea Arcangeli 	 * from under us anymore at this point because we hold the mmap_sem
329071e3aac0SAndrea Arcangeli 	 * read mode and khugepaged takes it in write mode. So now it's
329171e3aac0SAndrea Arcangeli 	 * safe to run pte_offset_map().
329271e3aac0SAndrea Arcangeli 	 */
329371e3aac0SAndrea Arcangeli 	pte = pte_offset_map(pmd, address);
32941da177e4SLinus Torvalds 
329530c9f3a9SLinus Torvalds 	return handle_pte_fault(mm, vma, address, pte, pmd, flags);
32961da177e4SLinus Torvalds }
32971da177e4SLinus Torvalds 
32989a95f3cfSPaul Cassella /*
32999a95f3cfSPaul Cassella  * By the time we get here, we already hold the mm semaphore
33009a95f3cfSPaul Cassella  *
33019a95f3cfSPaul Cassella  * The mmap_sem may have been released depending on flags and our
33029a95f3cfSPaul Cassella  * return value.  See filemap_fault() and __lock_page_or_retry().
33039a95f3cfSPaul Cassella  */
3304519e5247SJohannes Weiner int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
3305519e5247SJohannes Weiner 		    unsigned long address, unsigned int flags)
3306519e5247SJohannes Weiner {
3307519e5247SJohannes Weiner 	int ret;
3308519e5247SJohannes Weiner 
3309519e5247SJohannes Weiner 	__set_current_state(TASK_RUNNING);
3310519e5247SJohannes Weiner 
3311519e5247SJohannes Weiner 	count_vm_event(PGFAULT);
3312519e5247SJohannes Weiner 	mem_cgroup_count_vm_event(mm, PGFAULT);
3313519e5247SJohannes Weiner 
3314519e5247SJohannes Weiner 	/* do counter updates before entering really critical section. */
3315519e5247SJohannes Weiner 	check_sync_rss_stat(current);
3316519e5247SJohannes Weiner 
3317519e5247SJohannes Weiner 	/*
3318519e5247SJohannes Weiner 	 * Enable the memcg OOM handling for faults triggered in user
3319519e5247SJohannes Weiner 	 * space.  Kernel faults are handled more gracefully.
3320519e5247SJohannes Weiner 	 */
3321519e5247SJohannes Weiner 	if (flags & FAULT_FLAG_USER)
332249426420SJohannes Weiner 		mem_cgroup_oom_enable();
3323519e5247SJohannes Weiner 
3324519e5247SJohannes Weiner 	ret = __handle_mm_fault(mm, vma, address, flags);
3325519e5247SJohannes Weiner 
332649426420SJohannes Weiner 	if (flags & FAULT_FLAG_USER) {
332749426420SJohannes Weiner 		mem_cgroup_oom_disable();
332849426420SJohannes Weiner                 /*
332949426420SJohannes Weiner                  * The task may have entered a memcg OOM situation but
333049426420SJohannes Weiner                  * if the allocation error was handled gracefully (no
333149426420SJohannes Weiner                  * VM_FAULT_OOM), there is no need to kill anything.
333249426420SJohannes Weiner                  * Just clean up the OOM state peacefully.
333349426420SJohannes Weiner                  */
333449426420SJohannes Weiner                 if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM))
333549426420SJohannes Weiner                         mem_cgroup_oom_synchronize(false);
333649426420SJohannes Weiner 	}
33373812c8c8SJohannes Weiner 
3338519e5247SJohannes Weiner 	return ret;
3339519e5247SJohannes Weiner }
3340e1d6d01aSJesse Barnes EXPORT_SYMBOL_GPL(handle_mm_fault);
3341519e5247SJohannes Weiner 
33421da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED
33431da177e4SLinus Torvalds /*
33441da177e4SLinus Torvalds  * Allocate page upper directory.
3345872fec16SHugh Dickins  * We've already handled the fast-path in-line.
33461da177e4SLinus Torvalds  */
33471bb3630eSHugh Dickins int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
33481da177e4SLinus Torvalds {
3349c74df32cSHugh Dickins 	pud_t *new = pud_alloc_one(mm, address);
3350c74df32cSHugh Dickins 	if (!new)
33511bb3630eSHugh Dickins 		return -ENOMEM;
33521da177e4SLinus Torvalds 
3353362a61adSNick Piggin 	smp_wmb(); /* See comment in __pte_alloc */
3354362a61adSNick Piggin 
3355872fec16SHugh Dickins 	spin_lock(&mm->page_table_lock);
33561bb3630eSHugh Dickins 	if (pgd_present(*pgd))		/* Another has populated it */
33575e541973SBenjamin Herrenschmidt 		pud_free(mm, new);
33581bb3630eSHugh Dickins 	else
33591da177e4SLinus Torvalds 		pgd_populate(mm, pgd, new);
3360872fec16SHugh Dickins 	spin_unlock(&mm->page_table_lock);
33611bb3630eSHugh Dickins 	return 0;
33621da177e4SLinus Torvalds }
33631da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */
33641da177e4SLinus Torvalds 
33651da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED
33661da177e4SLinus Torvalds /*
33671da177e4SLinus Torvalds  * Allocate page middle directory.
3368872fec16SHugh Dickins  * We've already handled the fast-path in-line.
33691da177e4SLinus Torvalds  */
33701bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
33711da177e4SLinus Torvalds {
3372c74df32cSHugh Dickins 	pmd_t *new = pmd_alloc_one(mm, address);
3373c74df32cSHugh Dickins 	if (!new)
33741bb3630eSHugh Dickins 		return -ENOMEM;
33751da177e4SLinus Torvalds 
3376362a61adSNick Piggin 	smp_wmb(); /* See comment in __pte_alloc */
3377362a61adSNick Piggin 
3378872fec16SHugh Dickins 	spin_lock(&mm->page_table_lock);
33791da177e4SLinus Torvalds #ifndef __ARCH_HAS_4LEVEL_HACK
3380dc6c9a35SKirill A. Shutemov 	if (!pud_present(*pud)) {
3381dc6c9a35SKirill A. Shutemov 		mm_inc_nr_pmds(mm);
33821da177e4SLinus Torvalds 		pud_populate(mm, pud, new);
3383dc6c9a35SKirill A. Shutemov 	} else	/* Another has populated it */
33845e541973SBenjamin Herrenschmidt 		pmd_free(mm, new);
3385dc6c9a35SKirill A. Shutemov #else
3386dc6c9a35SKirill A. Shutemov 	if (!pgd_present(*pud)) {
3387dc6c9a35SKirill A. Shutemov 		mm_inc_nr_pmds(mm);
33881da177e4SLinus Torvalds 		pgd_populate(mm, pud, new);
3389dc6c9a35SKirill A. Shutemov 	} else /* Another has populated it */
3390dc6c9a35SKirill A. Shutemov 		pmd_free(mm, new);
33911da177e4SLinus Torvalds #endif /* __ARCH_HAS_4LEVEL_HACK */
3392872fec16SHugh Dickins 	spin_unlock(&mm->page_table_lock);
33931bb3630eSHugh Dickins 	return 0;
33941da177e4SLinus Torvalds }
33951da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */
33961da177e4SLinus Torvalds 
33971b36ba81SNamhyung Kim static int __follow_pte(struct mm_struct *mm, unsigned long address,
3398f8ad0f49SJohannes Weiner 		pte_t **ptepp, spinlock_t **ptlp)
3399f8ad0f49SJohannes Weiner {
3400f8ad0f49SJohannes Weiner 	pgd_t *pgd;
3401f8ad0f49SJohannes Weiner 	pud_t *pud;
3402f8ad0f49SJohannes Weiner 	pmd_t *pmd;
3403f8ad0f49SJohannes Weiner 	pte_t *ptep;
3404f8ad0f49SJohannes Weiner 
3405f8ad0f49SJohannes Weiner 	pgd = pgd_offset(mm, address);
3406f8ad0f49SJohannes Weiner 	if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
3407f8ad0f49SJohannes Weiner 		goto out;
3408f8ad0f49SJohannes Weiner 
3409f8ad0f49SJohannes Weiner 	pud = pud_offset(pgd, address);
3410f8ad0f49SJohannes Weiner 	if (pud_none(*pud) || unlikely(pud_bad(*pud)))
3411f8ad0f49SJohannes Weiner 		goto out;
3412f8ad0f49SJohannes Weiner 
3413f8ad0f49SJohannes Weiner 	pmd = pmd_offset(pud, address);
3414f66055abSAndrea Arcangeli 	VM_BUG_ON(pmd_trans_huge(*pmd));
3415f8ad0f49SJohannes Weiner 	if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
3416f8ad0f49SJohannes Weiner 		goto out;
3417f8ad0f49SJohannes Weiner 
3418f8ad0f49SJohannes Weiner 	/* We cannot handle huge page PFN maps. Luckily they don't exist. */
3419f8ad0f49SJohannes Weiner 	if (pmd_huge(*pmd))
3420f8ad0f49SJohannes Weiner 		goto out;
3421f8ad0f49SJohannes Weiner 
3422f8ad0f49SJohannes Weiner 	ptep = pte_offset_map_lock(mm, pmd, address, ptlp);
3423f8ad0f49SJohannes Weiner 	if (!ptep)
3424f8ad0f49SJohannes Weiner 		goto out;
3425f8ad0f49SJohannes Weiner 	if (!pte_present(*ptep))
3426f8ad0f49SJohannes Weiner 		goto unlock;
3427f8ad0f49SJohannes Weiner 	*ptepp = ptep;
3428f8ad0f49SJohannes Weiner 	return 0;
3429f8ad0f49SJohannes Weiner unlock:
3430f8ad0f49SJohannes Weiner 	pte_unmap_unlock(ptep, *ptlp);
3431f8ad0f49SJohannes Weiner out:
3432f8ad0f49SJohannes Weiner 	return -EINVAL;
3433f8ad0f49SJohannes Weiner }
3434f8ad0f49SJohannes Weiner 
34351b36ba81SNamhyung Kim static inline int follow_pte(struct mm_struct *mm, unsigned long address,
34361b36ba81SNamhyung Kim 			     pte_t **ptepp, spinlock_t **ptlp)
34371b36ba81SNamhyung Kim {
34381b36ba81SNamhyung Kim 	int res;
34391b36ba81SNamhyung Kim 
34401b36ba81SNamhyung Kim 	/* (void) is needed to make gcc happy */
34411b36ba81SNamhyung Kim 	(void) __cond_lock(*ptlp,
34421b36ba81SNamhyung Kim 			   !(res = __follow_pte(mm, address, ptepp, ptlp)));
34431b36ba81SNamhyung Kim 	return res;
34441b36ba81SNamhyung Kim }
34451b36ba81SNamhyung Kim 
34463b6748e2SJohannes Weiner /**
34473b6748e2SJohannes Weiner  * follow_pfn - look up PFN at a user virtual address
34483b6748e2SJohannes Weiner  * @vma: memory mapping
34493b6748e2SJohannes Weiner  * @address: user virtual address
34503b6748e2SJohannes Weiner  * @pfn: location to store found PFN
34513b6748e2SJohannes Weiner  *
34523b6748e2SJohannes Weiner  * Only IO mappings and raw PFN mappings are allowed.
34533b6748e2SJohannes Weiner  *
34543b6748e2SJohannes Weiner  * Returns zero and the pfn at @pfn on success, -ve otherwise.
34553b6748e2SJohannes Weiner  */
34563b6748e2SJohannes Weiner int follow_pfn(struct vm_area_struct *vma, unsigned long address,
34573b6748e2SJohannes Weiner 	unsigned long *pfn)
34583b6748e2SJohannes Weiner {
34593b6748e2SJohannes Weiner 	int ret = -EINVAL;
34603b6748e2SJohannes Weiner 	spinlock_t *ptl;
34613b6748e2SJohannes Weiner 	pte_t *ptep;
34623b6748e2SJohannes Weiner 
34633b6748e2SJohannes Weiner 	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
34643b6748e2SJohannes Weiner 		return ret;
34653b6748e2SJohannes Weiner 
34663b6748e2SJohannes Weiner 	ret = follow_pte(vma->vm_mm, address, &ptep, &ptl);
34673b6748e2SJohannes Weiner 	if (ret)
34683b6748e2SJohannes Weiner 		return ret;
34693b6748e2SJohannes Weiner 	*pfn = pte_pfn(*ptep);
34703b6748e2SJohannes Weiner 	pte_unmap_unlock(ptep, ptl);
34713b6748e2SJohannes Weiner 	return 0;
34723b6748e2SJohannes Weiner }
34733b6748e2SJohannes Weiner EXPORT_SYMBOL(follow_pfn);
34743b6748e2SJohannes Weiner 
347528b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT
3476d87fe660Svenkatesh.pallipadi@intel.com int follow_phys(struct vm_area_struct *vma,
347728b2ee20SRik van Riel 		unsigned long address, unsigned int flags,
3478d87fe660Svenkatesh.pallipadi@intel.com 		unsigned long *prot, resource_size_t *phys)
347928b2ee20SRik van Riel {
348003668a4dSJohannes Weiner 	int ret = -EINVAL;
348128b2ee20SRik van Riel 	pte_t *ptep, pte;
348228b2ee20SRik van Riel 	spinlock_t *ptl;
348328b2ee20SRik van Riel 
3484d87fe660Svenkatesh.pallipadi@intel.com 	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
3485d87fe660Svenkatesh.pallipadi@intel.com 		goto out;
348628b2ee20SRik van Riel 
348703668a4dSJohannes Weiner 	if (follow_pte(vma->vm_mm, address, &ptep, &ptl))
3488d87fe660Svenkatesh.pallipadi@intel.com 		goto out;
348928b2ee20SRik van Riel 	pte = *ptep;
349003668a4dSJohannes Weiner 
349128b2ee20SRik van Riel 	if ((flags & FOLL_WRITE) && !pte_write(pte))
349228b2ee20SRik van Riel 		goto unlock;
349328b2ee20SRik van Riel 
349428b2ee20SRik van Riel 	*prot = pgprot_val(pte_pgprot(pte));
349503668a4dSJohannes Weiner 	*phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT;
349628b2ee20SRik van Riel 
349703668a4dSJohannes Weiner 	ret = 0;
349828b2ee20SRik van Riel unlock:
349928b2ee20SRik van Riel 	pte_unmap_unlock(ptep, ptl);
350028b2ee20SRik van Riel out:
3501d87fe660Svenkatesh.pallipadi@intel.com 	return ret;
350228b2ee20SRik van Riel }
350328b2ee20SRik van Riel 
350428b2ee20SRik van Riel int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
350528b2ee20SRik van Riel 			void *buf, int len, int write)
350628b2ee20SRik van Riel {
350728b2ee20SRik van Riel 	resource_size_t phys_addr;
350828b2ee20SRik van Riel 	unsigned long prot = 0;
35092bc7273bSKOSAKI Motohiro 	void __iomem *maddr;
351028b2ee20SRik van Riel 	int offset = addr & (PAGE_SIZE-1);
351128b2ee20SRik van Riel 
3512d87fe660Svenkatesh.pallipadi@intel.com 	if (follow_phys(vma, addr, write, &prot, &phys_addr))
351328b2ee20SRik van Riel 		return -EINVAL;
351428b2ee20SRik van Riel 
35159cb12d7bSGrazvydas Ignotas 	maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot);
351628b2ee20SRik van Riel 	if (write)
351728b2ee20SRik van Riel 		memcpy_toio(maddr + offset, buf, len);
351828b2ee20SRik van Riel 	else
351928b2ee20SRik van Riel 		memcpy_fromio(buf, maddr + offset, len);
352028b2ee20SRik van Riel 	iounmap(maddr);
352128b2ee20SRik van Riel 
352228b2ee20SRik van Riel 	return len;
352328b2ee20SRik van Riel }
35245a73633eSUwe Kleine-König EXPORT_SYMBOL_GPL(generic_access_phys);
352528b2ee20SRik van Riel #endif
352628b2ee20SRik van Riel 
35270ec76a11SDavid Howells /*
3528206cb636SStephen Wilson  * Access another process' address space as given in mm.  If non-NULL, use the
3529206cb636SStephen Wilson  * given task for page fault accounting.
35300ec76a11SDavid Howells  */
3531206cb636SStephen Wilson static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
3532206cb636SStephen Wilson 		unsigned long addr, void *buf, int len, int write)
35330ec76a11SDavid Howells {
35340ec76a11SDavid Howells 	struct vm_area_struct *vma;
35350ec76a11SDavid Howells 	void *old_buf = buf;
35360ec76a11SDavid Howells 
35370ec76a11SDavid Howells 	down_read(&mm->mmap_sem);
3538183ff22bSSimon Arlott 	/* ignore errors, just check how much was successfully transferred */
35390ec76a11SDavid Howells 	while (len) {
35400ec76a11SDavid Howells 		int bytes, ret, offset;
35410ec76a11SDavid Howells 		void *maddr;
354228b2ee20SRik van Riel 		struct page *page = NULL;
35430ec76a11SDavid Howells 
35440ec76a11SDavid Howells 		ret = get_user_pages(tsk, mm, addr, 1,
35450ec76a11SDavid Howells 				write, 1, &page, &vma);
354628b2ee20SRik van Riel 		if (ret <= 0) {
3547dbffcd03SRik van Riel #ifndef CONFIG_HAVE_IOREMAP_PROT
3548dbffcd03SRik van Riel 			break;
3549dbffcd03SRik van Riel #else
355028b2ee20SRik van Riel 			/*
355128b2ee20SRik van Riel 			 * Check if this is a VM_IO | VM_PFNMAP VMA, which
355228b2ee20SRik van Riel 			 * we can access using slightly different code.
355328b2ee20SRik van Riel 			 */
355428b2ee20SRik van Riel 			vma = find_vma(mm, addr);
3555fe936dfcSMichael Ellerman 			if (!vma || vma->vm_start > addr)
35560ec76a11SDavid Howells 				break;
355728b2ee20SRik van Riel 			if (vma->vm_ops && vma->vm_ops->access)
355828b2ee20SRik van Riel 				ret = vma->vm_ops->access(vma, addr, buf,
355928b2ee20SRik van Riel 							  len, write);
356028b2ee20SRik van Riel 			if (ret <= 0)
356128b2ee20SRik van Riel 				break;
356228b2ee20SRik van Riel 			bytes = ret;
3563dbffcd03SRik van Riel #endif
356428b2ee20SRik van Riel 		} else {
35650ec76a11SDavid Howells 			bytes = len;
35660ec76a11SDavid Howells 			offset = addr & (PAGE_SIZE-1);
35670ec76a11SDavid Howells 			if (bytes > PAGE_SIZE-offset)
35680ec76a11SDavid Howells 				bytes = PAGE_SIZE-offset;
35690ec76a11SDavid Howells 
35700ec76a11SDavid Howells 			maddr = kmap(page);
35710ec76a11SDavid Howells 			if (write) {
35720ec76a11SDavid Howells 				copy_to_user_page(vma, page, addr,
35730ec76a11SDavid Howells 						  maddr + offset, buf, bytes);
35740ec76a11SDavid Howells 				set_page_dirty_lock(page);
35750ec76a11SDavid Howells 			} else {
35760ec76a11SDavid Howells 				copy_from_user_page(vma, page, addr,
35770ec76a11SDavid Howells 						    buf, maddr + offset, bytes);
35780ec76a11SDavid Howells 			}
35790ec76a11SDavid Howells 			kunmap(page);
35800ec76a11SDavid Howells 			page_cache_release(page);
358128b2ee20SRik van Riel 		}
35820ec76a11SDavid Howells 		len -= bytes;
35830ec76a11SDavid Howells 		buf += bytes;
35840ec76a11SDavid Howells 		addr += bytes;
35850ec76a11SDavid Howells 	}
35860ec76a11SDavid Howells 	up_read(&mm->mmap_sem);
35870ec76a11SDavid Howells 
35880ec76a11SDavid Howells 	return buf - old_buf;
35890ec76a11SDavid Howells }
359003252919SAndi Kleen 
35915ddd36b9SStephen Wilson /**
3592ae91dbfcSRandy Dunlap  * access_remote_vm - access another process' address space
35935ddd36b9SStephen Wilson  * @mm:		the mm_struct of the target address space
35945ddd36b9SStephen Wilson  * @addr:	start address to access
35955ddd36b9SStephen Wilson  * @buf:	source or destination buffer
35965ddd36b9SStephen Wilson  * @len:	number of bytes to transfer
35975ddd36b9SStephen Wilson  * @write:	whether the access is a write
35985ddd36b9SStephen Wilson  *
35995ddd36b9SStephen Wilson  * The caller must hold a reference on @mm.
36005ddd36b9SStephen Wilson  */
36015ddd36b9SStephen Wilson int access_remote_vm(struct mm_struct *mm, unsigned long addr,
36025ddd36b9SStephen Wilson 		void *buf, int len, int write)
36035ddd36b9SStephen Wilson {
36045ddd36b9SStephen Wilson 	return __access_remote_vm(NULL, mm, addr, buf, len, write);
36055ddd36b9SStephen Wilson }
36065ddd36b9SStephen Wilson 
360703252919SAndi Kleen /*
3608206cb636SStephen Wilson  * Access another process' address space.
3609206cb636SStephen Wilson  * Source/target buffer must be kernel space,
3610206cb636SStephen Wilson  * Do not walk the page table directly, use get_user_pages
3611206cb636SStephen Wilson  */
3612206cb636SStephen Wilson int access_process_vm(struct task_struct *tsk, unsigned long addr,
3613206cb636SStephen Wilson 		void *buf, int len, int write)
3614206cb636SStephen Wilson {
3615206cb636SStephen Wilson 	struct mm_struct *mm;
3616206cb636SStephen Wilson 	int ret;
3617206cb636SStephen Wilson 
3618206cb636SStephen Wilson 	mm = get_task_mm(tsk);
3619206cb636SStephen Wilson 	if (!mm)
3620206cb636SStephen Wilson 		return 0;
3621206cb636SStephen Wilson 
3622206cb636SStephen Wilson 	ret = __access_remote_vm(tsk, mm, addr, buf, len, write);
3623206cb636SStephen Wilson 	mmput(mm);
3624206cb636SStephen Wilson 
3625206cb636SStephen Wilson 	return ret;
3626206cb636SStephen Wilson }
3627206cb636SStephen Wilson 
362803252919SAndi Kleen /*
362903252919SAndi Kleen  * Print the name of a VMA.
363003252919SAndi Kleen  */
363103252919SAndi Kleen void print_vma_addr(char *prefix, unsigned long ip)
363203252919SAndi Kleen {
363303252919SAndi Kleen 	struct mm_struct *mm = current->mm;
363403252919SAndi Kleen 	struct vm_area_struct *vma;
363503252919SAndi Kleen 
3636e8bff74aSIngo Molnar 	/*
3637e8bff74aSIngo Molnar 	 * Do not print if we are in atomic
3638e8bff74aSIngo Molnar 	 * contexts (in exception stacks, etc.):
3639e8bff74aSIngo Molnar 	 */
3640e8bff74aSIngo Molnar 	if (preempt_count())
3641e8bff74aSIngo Molnar 		return;
3642e8bff74aSIngo Molnar 
364303252919SAndi Kleen 	down_read(&mm->mmap_sem);
364403252919SAndi Kleen 	vma = find_vma(mm, ip);
364503252919SAndi Kleen 	if (vma && vma->vm_file) {
364603252919SAndi Kleen 		struct file *f = vma->vm_file;
364703252919SAndi Kleen 		char *buf = (char *)__get_free_page(GFP_KERNEL);
364803252919SAndi Kleen 		if (buf) {
36492fbc57c5SAndy Shevchenko 			char *p;
365003252919SAndi Kleen 
3651cf28b486SJan Blunck 			p = d_path(&f->f_path, buf, PAGE_SIZE);
365203252919SAndi Kleen 			if (IS_ERR(p))
365303252919SAndi Kleen 				p = "?";
36542fbc57c5SAndy Shevchenko 			printk("%s%s[%lx+%lx]", prefix, kbasename(p),
365503252919SAndi Kleen 					vma->vm_start,
365603252919SAndi Kleen 					vma->vm_end - vma->vm_start);
365703252919SAndi Kleen 			free_page((unsigned long)buf);
365803252919SAndi Kleen 		}
365903252919SAndi Kleen 	}
366051a07e50SJeff Liu 	up_read(&mm->mmap_sem);
366103252919SAndi Kleen }
36623ee1afa3SNick Piggin 
3663662bbcb2SMichael S. Tsirkin #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)
36643ee1afa3SNick Piggin void might_fault(void)
36653ee1afa3SNick Piggin {
366695156f00SPeter Zijlstra 	/*
366795156f00SPeter Zijlstra 	 * Some code (nfs/sunrpc) uses socket ops on kernel memory while
366895156f00SPeter Zijlstra 	 * holding the mmap_sem, this is safe because kernel memory doesn't
366995156f00SPeter Zijlstra 	 * get paged out, therefore we'll never actually fault, and the
367095156f00SPeter Zijlstra 	 * below annotations will generate false positives.
367195156f00SPeter Zijlstra 	 */
367295156f00SPeter Zijlstra 	if (segment_eq(get_fs(), KERNEL_DS))
367395156f00SPeter Zijlstra 		return;
367495156f00SPeter Zijlstra 
36753ee1afa3SNick Piggin 	/*
36763ee1afa3SNick Piggin 	 * it would be nicer only to annotate paths which are not under
36773ee1afa3SNick Piggin 	 * pagefault_disable, however that requires a larger audit and
36783ee1afa3SNick Piggin 	 * providing helpers like get_user_atomic.
36793ee1afa3SNick Piggin 	 */
3680662bbcb2SMichael S. Tsirkin 	if (in_atomic())
3681662bbcb2SMichael S. Tsirkin 		return;
3682662bbcb2SMichael S. Tsirkin 
3683662bbcb2SMichael S. Tsirkin 	__might_sleep(__FILE__, __LINE__, 0);
3684662bbcb2SMichael S. Tsirkin 
3685662bbcb2SMichael S. Tsirkin 	if (current->mm)
36863ee1afa3SNick Piggin 		might_lock_read(&current->mm->mmap_sem);
36873ee1afa3SNick Piggin }
36883ee1afa3SNick Piggin EXPORT_SYMBOL(might_fault);
36893ee1afa3SNick Piggin #endif
369047ad8475SAndrea Arcangeli 
369147ad8475SAndrea Arcangeli #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
369247ad8475SAndrea Arcangeli static void clear_gigantic_page(struct page *page,
369347ad8475SAndrea Arcangeli 				unsigned long addr,
369447ad8475SAndrea Arcangeli 				unsigned int pages_per_huge_page)
369547ad8475SAndrea Arcangeli {
369647ad8475SAndrea Arcangeli 	int i;
369747ad8475SAndrea Arcangeli 	struct page *p = page;
369847ad8475SAndrea Arcangeli 
369947ad8475SAndrea Arcangeli 	might_sleep();
370047ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page;
370147ad8475SAndrea Arcangeli 	     i++, p = mem_map_next(p, page, i)) {
370247ad8475SAndrea Arcangeli 		cond_resched();
370347ad8475SAndrea Arcangeli 		clear_user_highpage(p, addr + i * PAGE_SIZE);
370447ad8475SAndrea Arcangeli 	}
370547ad8475SAndrea Arcangeli }
370647ad8475SAndrea Arcangeli void clear_huge_page(struct page *page,
370747ad8475SAndrea Arcangeli 		     unsigned long addr, unsigned int pages_per_huge_page)
370847ad8475SAndrea Arcangeli {
370947ad8475SAndrea Arcangeli 	int i;
371047ad8475SAndrea Arcangeli 
371147ad8475SAndrea Arcangeli 	if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
371247ad8475SAndrea Arcangeli 		clear_gigantic_page(page, addr, pages_per_huge_page);
371347ad8475SAndrea Arcangeli 		return;
371447ad8475SAndrea Arcangeli 	}
371547ad8475SAndrea Arcangeli 
371647ad8475SAndrea Arcangeli 	might_sleep();
371747ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page; i++) {
371847ad8475SAndrea Arcangeli 		cond_resched();
371947ad8475SAndrea Arcangeli 		clear_user_highpage(page + i, addr + i * PAGE_SIZE);
372047ad8475SAndrea Arcangeli 	}
372147ad8475SAndrea Arcangeli }
372247ad8475SAndrea Arcangeli 
372347ad8475SAndrea Arcangeli static void copy_user_gigantic_page(struct page *dst, struct page *src,
372447ad8475SAndrea Arcangeli 				    unsigned long addr,
372547ad8475SAndrea Arcangeli 				    struct vm_area_struct *vma,
372647ad8475SAndrea Arcangeli 				    unsigned int pages_per_huge_page)
372747ad8475SAndrea Arcangeli {
372847ad8475SAndrea Arcangeli 	int i;
372947ad8475SAndrea Arcangeli 	struct page *dst_base = dst;
373047ad8475SAndrea Arcangeli 	struct page *src_base = src;
373147ad8475SAndrea Arcangeli 
373247ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page; ) {
373347ad8475SAndrea Arcangeli 		cond_resched();
373447ad8475SAndrea Arcangeli 		copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma);
373547ad8475SAndrea Arcangeli 
373647ad8475SAndrea Arcangeli 		i++;
373747ad8475SAndrea Arcangeli 		dst = mem_map_next(dst, dst_base, i);
373847ad8475SAndrea Arcangeli 		src = mem_map_next(src, src_base, i);
373947ad8475SAndrea Arcangeli 	}
374047ad8475SAndrea Arcangeli }
374147ad8475SAndrea Arcangeli 
374247ad8475SAndrea Arcangeli void copy_user_huge_page(struct page *dst, struct page *src,
374347ad8475SAndrea Arcangeli 			 unsigned long addr, struct vm_area_struct *vma,
374447ad8475SAndrea Arcangeli 			 unsigned int pages_per_huge_page)
374547ad8475SAndrea Arcangeli {
374647ad8475SAndrea Arcangeli 	int i;
374747ad8475SAndrea Arcangeli 
374847ad8475SAndrea Arcangeli 	if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
374947ad8475SAndrea Arcangeli 		copy_user_gigantic_page(dst, src, addr, vma,
375047ad8475SAndrea Arcangeli 					pages_per_huge_page);
375147ad8475SAndrea Arcangeli 		return;
375247ad8475SAndrea Arcangeli 	}
375347ad8475SAndrea Arcangeli 
375447ad8475SAndrea Arcangeli 	might_sleep();
375547ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page; i++) {
375647ad8475SAndrea Arcangeli 		cond_resched();
375747ad8475SAndrea Arcangeli 		copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
375847ad8475SAndrea Arcangeli 	}
375947ad8475SAndrea Arcangeli }
376047ad8475SAndrea Arcangeli #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */
376149076ec2SKirill A. Shutemov 
376240b64acdSOlof Johansson #if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS
3763b35f1819SKirill A. Shutemov 
3764b35f1819SKirill A. Shutemov static struct kmem_cache *page_ptl_cachep;
3765b35f1819SKirill A. Shutemov 
3766b35f1819SKirill A. Shutemov void __init ptlock_cache_init(void)
3767b35f1819SKirill A. Shutemov {
3768b35f1819SKirill A. Shutemov 	page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0,
3769b35f1819SKirill A. Shutemov 			SLAB_PANIC, NULL);
3770b35f1819SKirill A. Shutemov }
3771b35f1819SKirill A. Shutemov 
3772539edb58SPeter Zijlstra bool ptlock_alloc(struct page *page)
377349076ec2SKirill A. Shutemov {
377449076ec2SKirill A. Shutemov 	spinlock_t *ptl;
377549076ec2SKirill A. Shutemov 
3776b35f1819SKirill A. Shutemov 	ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL);
377749076ec2SKirill A. Shutemov 	if (!ptl)
377849076ec2SKirill A. Shutemov 		return false;
3779539edb58SPeter Zijlstra 	page->ptl = ptl;
378049076ec2SKirill A. Shutemov 	return true;
378149076ec2SKirill A. Shutemov }
378249076ec2SKirill A. Shutemov 
3783539edb58SPeter Zijlstra void ptlock_free(struct page *page)
378449076ec2SKirill A. Shutemov {
3785b35f1819SKirill A. Shutemov 	kmem_cache_free(page_ptl_cachep, page->ptl);
378649076ec2SKirill A. Shutemov }
378749076ec2SKirill A. Shutemov #endif
3788