xref: /linux/mm/memory.c (revision 4b471e8898c3d0f5c97a3c73ac32d0549fe01c87)
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>
646b251fc9SAndrea Arcangeli #include <linux/userfaultfd_k.h>
651da177e4SLinus Torvalds 
666952b61dSAlexey Dobriyan #include <asm/io.h>
671da177e4SLinus Torvalds #include <asm/pgalloc.h>
681da177e4SLinus Torvalds #include <asm/uaccess.h>
691da177e4SLinus Torvalds #include <asm/tlb.h>
701da177e4SLinus Torvalds #include <asm/tlbflush.h>
711da177e4SLinus Torvalds #include <asm/pgtable.h>
721da177e4SLinus Torvalds 
7342b77728SJan Beulich #include "internal.h"
7442b77728SJan Beulich 
7590572890SPeter Zijlstra #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
7690572890SPeter Zijlstra #warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid.
7775980e97SPeter Zijlstra #endif
7875980e97SPeter Zijlstra 
79d41dee36SAndy Whitcroft #ifndef CONFIG_NEED_MULTIPLE_NODES
801da177e4SLinus Torvalds /* use the per-pgdat data instead for discontigmem - mbligh */
811da177e4SLinus Torvalds unsigned long max_mapnr;
821da177e4SLinus Torvalds struct page *mem_map;
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds EXPORT_SYMBOL(max_mapnr);
851da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map);
861da177e4SLinus Torvalds #endif
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds /*
891da177e4SLinus Torvalds  * A number of key systems in x86 including ioremap() rely on the assumption
901da177e4SLinus Torvalds  * that high_memory defines the upper bound on direct map memory, then end
911da177e4SLinus Torvalds  * of ZONE_NORMAL.  Under CONFIG_DISCONTIG this means that max_low_pfn and
921da177e4SLinus Torvalds  * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL
931da177e4SLinus Torvalds  * and ZONE_HIGHMEM.
941da177e4SLinus Torvalds  */
951da177e4SLinus Torvalds void * high_memory;
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds EXPORT_SYMBOL(high_memory);
981da177e4SLinus Torvalds 
9932a93233SIngo Molnar /*
10032a93233SIngo Molnar  * Randomize the address space (stacks, mmaps, brk, etc.).
10132a93233SIngo Molnar  *
10232a93233SIngo Molnar  * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization,
10332a93233SIngo Molnar  *   as ancient (libc5 based) binaries can segfault. )
10432a93233SIngo Molnar  */
10532a93233SIngo Molnar int randomize_va_space __read_mostly =
10632a93233SIngo Molnar #ifdef CONFIG_COMPAT_BRK
10732a93233SIngo Molnar 					1;
10832a93233SIngo Molnar #else
10932a93233SIngo Molnar 					2;
11032a93233SIngo Molnar #endif
111a62eaf15SAndi Kleen 
112a62eaf15SAndi Kleen static int __init disable_randmaps(char *s)
113a62eaf15SAndi Kleen {
114a62eaf15SAndi Kleen 	randomize_va_space = 0;
1159b41046cSOGAWA Hirofumi 	return 1;
116a62eaf15SAndi Kleen }
117a62eaf15SAndi Kleen __setup("norandmaps", disable_randmaps);
118a62eaf15SAndi Kleen 
11962eede62SHugh Dickins unsigned long zero_pfn __read_mostly;
12003f6462aSHugh Dickins unsigned long highest_memmap_pfn __read_mostly;
121a13ea5b7SHugh Dickins 
1220b70068eSArd Biesheuvel EXPORT_SYMBOL(zero_pfn);
1230b70068eSArd Biesheuvel 
124a13ea5b7SHugh Dickins /*
125a13ea5b7SHugh Dickins  * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init()
126a13ea5b7SHugh Dickins  */
127a13ea5b7SHugh Dickins static int __init init_zero_pfn(void)
128a13ea5b7SHugh Dickins {
129a13ea5b7SHugh Dickins 	zero_pfn = page_to_pfn(ZERO_PAGE(0));
130a13ea5b7SHugh Dickins 	return 0;
131a13ea5b7SHugh Dickins }
132a13ea5b7SHugh Dickins core_initcall(init_zero_pfn);
133a62eaf15SAndi Kleen 
134d559db08SKAMEZAWA Hiroyuki 
13534e55232SKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING)
13634e55232SKAMEZAWA Hiroyuki 
137ea48cf78SDavid Rientjes void sync_mm_rss(struct mm_struct *mm)
13834e55232SKAMEZAWA Hiroyuki {
13934e55232SKAMEZAWA Hiroyuki 	int i;
14034e55232SKAMEZAWA Hiroyuki 
14134e55232SKAMEZAWA Hiroyuki 	for (i = 0; i < NR_MM_COUNTERS; i++) {
14205af2e10SDavid Rientjes 		if (current->rss_stat.count[i]) {
14305af2e10SDavid Rientjes 			add_mm_counter(mm, i, current->rss_stat.count[i]);
14405af2e10SDavid Rientjes 			current->rss_stat.count[i] = 0;
14534e55232SKAMEZAWA Hiroyuki 		}
14634e55232SKAMEZAWA Hiroyuki 	}
14705af2e10SDavid Rientjes 	current->rss_stat.events = 0;
14834e55232SKAMEZAWA Hiroyuki }
14934e55232SKAMEZAWA Hiroyuki 
15034e55232SKAMEZAWA Hiroyuki static void add_mm_counter_fast(struct mm_struct *mm, int member, int val)
15134e55232SKAMEZAWA Hiroyuki {
15234e55232SKAMEZAWA Hiroyuki 	struct task_struct *task = current;
15334e55232SKAMEZAWA Hiroyuki 
15434e55232SKAMEZAWA Hiroyuki 	if (likely(task->mm == mm))
15534e55232SKAMEZAWA Hiroyuki 		task->rss_stat.count[member] += val;
15634e55232SKAMEZAWA Hiroyuki 	else
15734e55232SKAMEZAWA Hiroyuki 		add_mm_counter(mm, member, val);
15834e55232SKAMEZAWA Hiroyuki }
15934e55232SKAMEZAWA Hiroyuki #define inc_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, 1)
16034e55232SKAMEZAWA Hiroyuki #define dec_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, -1)
16134e55232SKAMEZAWA Hiroyuki 
16234e55232SKAMEZAWA Hiroyuki /* sync counter once per 64 page faults */
16334e55232SKAMEZAWA Hiroyuki #define TASK_RSS_EVENTS_THRESH	(64)
16434e55232SKAMEZAWA Hiroyuki static void check_sync_rss_stat(struct task_struct *task)
16534e55232SKAMEZAWA Hiroyuki {
16634e55232SKAMEZAWA Hiroyuki 	if (unlikely(task != current))
16734e55232SKAMEZAWA Hiroyuki 		return;
16834e55232SKAMEZAWA Hiroyuki 	if (unlikely(task->rss_stat.events++ > TASK_RSS_EVENTS_THRESH))
169ea48cf78SDavid Rientjes 		sync_mm_rss(task->mm);
17034e55232SKAMEZAWA Hiroyuki }
1719547d01bSPeter Zijlstra #else /* SPLIT_RSS_COUNTING */
17234e55232SKAMEZAWA Hiroyuki 
17334e55232SKAMEZAWA Hiroyuki #define inc_mm_counter_fast(mm, member) inc_mm_counter(mm, member)
17434e55232SKAMEZAWA Hiroyuki #define dec_mm_counter_fast(mm, member) dec_mm_counter(mm, member)
17534e55232SKAMEZAWA Hiroyuki 
17634e55232SKAMEZAWA Hiroyuki static void check_sync_rss_stat(struct task_struct *task)
17734e55232SKAMEZAWA Hiroyuki {
17834e55232SKAMEZAWA Hiroyuki }
17934e55232SKAMEZAWA Hiroyuki 
1809547d01bSPeter Zijlstra #endif /* SPLIT_RSS_COUNTING */
1819547d01bSPeter Zijlstra 
1829547d01bSPeter Zijlstra #ifdef HAVE_GENERIC_MMU_GATHER
1839547d01bSPeter Zijlstra 
184ca1d6c7dSNicholas Krause static bool tlb_next_batch(struct mmu_gather *tlb)
1859547d01bSPeter Zijlstra {
1869547d01bSPeter Zijlstra 	struct mmu_gather_batch *batch;
1879547d01bSPeter Zijlstra 
1889547d01bSPeter Zijlstra 	batch = tlb->active;
1899547d01bSPeter Zijlstra 	if (batch->next) {
1909547d01bSPeter Zijlstra 		tlb->active = batch->next;
191ca1d6c7dSNicholas Krause 		return true;
1929547d01bSPeter Zijlstra 	}
1939547d01bSPeter Zijlstra 
19453a59fc6SMichal Hocko 	if (tlb->batch_count == MAX_GATHER_BATCH_COUNT)
195ca1d6c7dSNicholas Krause 		return false;
19653a59fc6SMichal Hocko 
1979547d01bSPeter Zijlstra 	batch = (void *)__get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0);
1989547d01bSPeter Zijlstra 	if (!batch)
199ca1d6c7dSNicholas Krause 		return false;
2009547d01bSPeter Zijlstra 
20153a59fc6SMichal Hocko 	tlb->batch_count++;
2029547d01bSPeter Zijlstra 	batch->next = NULL;
2039547d01bSPeter Zijlstra 	batch->nr   = 0;
2049547d01bSPeter Zijlstra 	batch->max  = MAX_GATHER_BATCH;
2059547d01bSPeter Zijlstra 
2069547d01bSPeter Zijlstra 	tlb->active->next = batch;
2079547d01bSPeter Zijlstra 	tlb->active = batch;
2089547d01bSPeter Zijlstra 
209ca1d6c7dSNicholas Krause 	return true;
2109547d01bSPeter Zijlstra }
2119547d01bSPeter Zijlstra 
2129547d01bSPeter Zijlstra /* tlb_gather_mmu
2139547d01bSPeter Zijlstra  *	Called to initialize an (on-stack) mmu_gather structure for page-table
2149547d01bSPeter Zijlstra  *	tear-down from @mm. The @fullmm argument is used when @mm is without
2159547d01bSPeter Zijlstra  *	users and we're going to destroy the full address space (exit/execve).
2169547d01bSPeter Zijlstra  */
2172b047252SLinus Torvalds void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long start, unsigned long end)
2189547d01bSPeter Zijlstra {
2199547d01bSPeter Zijlstra 	tlb->mm = mm;
2209547d01bSPeter Zijlstra 
2212b047252SLinus Torvalds 	/* Is it from 0 to ~0? */
2222b047252SLinus Torvalds 	tlb->fullmm     = !(start | (end+1));
2231de14c3cSDave Hansen 	tlb->need_flush_all = 0;
2249547d01bSPeter Zijlstra 	tlb->local.next = NULL;
2259547d01bSPeter Zijlstra 	tlb->local.nr   = 0;
2269547d01bSPeter Zijlstra 	tlb->local.max  = ARRAY_SIZE(tlb->__pages);
2279547d01bSPeter Zijlstra 	tlb->active     = &tlb->local;
22853a59fc6SMichal Hocko 	tlb->batch_count = 0;
2299547d01bSPeter Zijlstra 
2309547d01bSPeter Zijlstra #ifdef CONFIG_HAVE_RCU_TABLE_FREE
2319547d01bSPeter Zijlstra 	tlb->batch = NULL;
23234e55232SKAMEZAWA Hiroyuki #endif
233fb7332a9SWill Deacon 
234fb7332a9SWill Deacon 	__tlb_reset_range(tlb);
2359547d01bSPeter Zijlstra }
2369547d01bSPeter Zijlstra 
2371cf35d47SLinus Torvalds static void tlb_flush_mmu_tlbonly(struct mmu_gather *tlb)
2389547d01bSPeter Zijlstra {
239721c21c1SWill Deacon 	if (!tlb->end)
240721c21c1SWill Deacon 		return;
241721c21c1SWill Deacon 
2429547d01bSPeter Zijlstra 	tlb_flush(tlb);
24334ee645eSJoerg Roedel 	mmu_notifier_invalidate_range(tlb->mm, tlb->start, tlb->end);
2449547d01bSPeter Zijlstra #ifdef CONFIG_HAVE_RCU_TABLE_FREE
2459547d01bSPeter Zijlstra 	tlb_table_flush(tlb);
2469547d01bSPeter Zijlstra #endif
247fb7332a9SWill Deacon 	__tlb_reset_range(tlb);
2481cf35d47SLinus Torvalds }
2491cf35d47SLinus Torvalds 
2501cf35d47SLinus Torvalds static void tlb_flush_mmu_free(struct mmu_gather *tlb)
2511cf35d47SLinus Torvalds {
2521cf35d47SLinus Torvalds 	struct mmu_gather_batch *batch;
2539547d01bSPeter Zijlstra 
254721c21c1SWill Deacon 	for (batch = &tlb->local; batch && batch->nr; batch = batch->next) {
2559547d01bSPeter Zijlstra 		free_pages_and_swap_cache(batch->pages, batch->nr);
2569547d01bSPeter Zijlstra 		batch->nr = 0;
2579547d01bSPeter Zijlstra 	}
2589547d01bSPeter Zijlstra 	tlb->active = &tlb->local;
2599547d01bSPeter Zijlstra }
2609547d01bSPeter Zijlstra 
2611cf35d47SLinus Torvalds void tlb_flush_mmu(struct mmu_gather *tlb)
2621cf35d47SLinus Torvalds {
2631cf35d47SLinus Torvalds 	tlb_flush_mmu_tlbonly(tlb);
2641cf35d47SLinus Torvalds 	tlb_flush_mmu_free(tlb);
2651cf35d47SLinus Torvalds }
2661cf35d47SLinus Torvalds 
2679547d01bSPeter Zijlstra /* tlb_finish_mmu
2689547d01bSPeter Zijlstra  *	Called at the end of the shootdown operation to free up any resources
2699547d01bSPeter Zijlstra  *	that were required.
2709547d01bSPeter Zijlstra  */
2719547d01bSPeter Zijlstra void tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end)
2729547d01bSPeter Zijlstra {
2739547d01bSPeter Zijlstra 	struct mmu_gather_batch *batch, *next;
2749547d01bSPeter Zijlstra 
2759547d01bSPeter Zijlstra 	tlb_flush_mmu(tlb);
2769547d01bSPeter Zijlstra 
2779547d01bSPeter Zijlstra 	/* keep the page table cache within bounds */
2789547d01bSPeter Zijlstra 	check_pgt_cache();
2799547d01bSPeter Zijlstra 
2809547d01bSPeter Zijlstra 	for (batch = tlb->local.next; batch; batch = next) {
2819547d01bSPeter Zijlstra 		next = batch->next;
2829547d01bSPeter Zijlstra 		free_pages((unsigned long)batch, 0);
2839547d01bSPeter Zijlstra 	}
2849547d01bSPeter Zijlstra 	tlb->local.next = NULL;
2859547d01bSPeter Zijlstra }
2869547d01bSPeter Zijlstra 
2879547d01bSPeter Zijlstra /* __tlb_remove_page
2889547d01bSPeter Zijlstra  *	Must perform the equivalent to __free_pte(pte_get_and_clear(ptep)), while
2899547d01bSPeter Zijlstra  *	handling the additional races in SMP caused by other CPUs caching valid
2909547d01bSPeter Zijlstra  *	mappings in their TLBs. Returns the number of free page slots left.
2919547d01bSPeter Zijlstra  *	When out of page slots we must call tlb_flush_mmu().
2929547d01bSPeter Zijlstra  */
2939547d01bSPeter Zijlstra int __tlb_remove_page(struct mmu_gather *tlb, struct page *page)
2949547d01bSPeter Zijlstra {
2959547d01bSPeter Zijlstra 	struct mmu_gather_batch *batch;
2969547d01bSPeter Zijlstra 
297fb7332a9SWill Deacon 	VM_BUG_ON(!tlb->end);
2989547d01bSPeter Zijlstra 
2999547d01bSPeter Zijlstra 	batch = tlb->active;
3009547d01bSPeter Zijlstra 	batch->pages[batch->nr++] = page;
3019547d01bSPeter Zijlstra 	if (batch->nr == batch->max) {
3029547d01bSPeter Zijlstra 		if (!tlb_next_batch(tlb))
3039547d01bSPeter Zijlstra 			return 0;
3040b43c3aaSShaohua Li 		batch = tlb->active;
3059547d01bSPeter Zijlstra 	}
306309381feSSasha Levin 	VM_BUG_ON_PAGE(batch->nr > batch->max, page);
3079547d01bSPeter Zijlstra 
3089547d01bSPeter Zijlstra 	return batch->max - batch->nr;
3099547d01bSPeter Zijlstra }
3109547d01bSPeter Zijlstra 
3119547d01bSPeter Zijlstra #endif /* HAVE_GENERIC_MMU_GATHER */
31234e55232SKAMEZAWA Hiroyuki 
31326723911SPeter Zijlstra #ifdef CONFIG_HAVE_RCU_TABLE_FREE
31426723911SPeter Zijlstra 
31526723911SPeter Zijlstra /*
31626723911SPeter Zijlstra  * See the comment near struct mmu_table_batch.
31726723911SPeter Zijlstra  */
31826723911SPeter Zijlstra 
31926723911SPeter Zijlstra static void tlb_remove_table_smp_sync(void *arg)
32026723911SPeter Zijlstra {
32126723911SPeter Zijlstra 	/* Simply deliver the interrupt */
32226723911SPeter Zijlstra }
32326723911SPeter Zijlstra 
32426723911SPeter Zijlstra static void tlb_remove_table_one(void *table)
32526723911SPeter Zijlstra {
32626723911SPeter Zijlstra 	/*
32726723911SPeter Zijlstra 	 * This isn't an RCU grace period and hence the page-tables cannot be
32826723911SPeter Zijlstra 	 * assumed to be actually RCU-freed.
32926723911SPeter Zijlstra 	 *
33026723911SPeter Zijlstra 	 * It is however sufficient for software page-table walkers that rely on
33126723911SPeter Zijlstra 	 * IRQ disabling. See the comment near struct mmu_table_batch.
33226723911SPeter Zijlstra 	 */
33326723911SPeter Zijlstra 	smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
33426723911SPeter Zijlstra 	__tlb_remove_table(table);
33526723911SPeter Zijlstra }
33626723911SPeter Zijlstra 
33726723911SPeter Zijlstra static void tlb_remove_table_rcu(struct rcu_head *head)
33826723911SPeter Zijlstra {
33926723911SPeter Zijlstra 	struct mmu_table_batch *batch;
34026723911SPeter Zijlstra 	int i;
34126723911SPeter Zijlstra 
34226723911SPeter Zijlstra 	batch = container_of(head, struct mmu_table_batch, rcu);
34326723911SPeter Zijlstra 
34426723911SPeter Zijlstra 	for (i = 0; i < batch->nr; i++)
34526723911SPeter Zijlstra 		__tlb_remove_table(batch->tables[i]);
34626723911SPeter Zijlstra 
34726723911SPeter Zijlstra 	free_page((unsigned long)batch);
34826723911SPeter Zijlstra }
34926723911SPeter Zijlstra 
35026723911SPeter Zijlstra void tlb_table_flush(struct mmu_gather *tlb)
35126723911SPeter Zijlstra {
35226723911SPeter Zijlstra 	struct mmu_table_batch **batch = &tlb->batch;
35326723911SPeter Zijlstra 
35426723911SPeter Zijlstra 	if (*batch) {
35526723911SPeter Zijlstra 		call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
35626723911SPeter Zijlstra 		*batch = NULL;
35726723911SPeter Zijlstra 	}
35826723911SPeter Zijlstra }
35926723911SPeter Zijlstra 
36026723911SPeter Zijlstra void tlb_remove_table(struct mmu_gather *tlb, void *table)
36126723911SPeter Zijlstra {
36226723911SPeter Zijlstra 	struct mmu_table_batch **batch = &tlb->batch;
36326723911SPeter Zijlstra 
36426723911SPeter Zijlstra 	/*
36526723911SPeter Zijlstra 	 * When there's less then two users of this mm there cannot be a
36626723911SPeter Zijlstra 	 * concurrent page-table walk.
36726723911SPeter Zijlstra 	 */
36826723911SPeter Zijlstra 	if (atomic_read(&tlb->mm->mm_users) < 2) {
36926723911SPeter Zijlstra 		__tlb_remove_table(table);
37026723911SPeter Zijlstra 		return;
37126723911SPeter Zijlstra 	}
37226723911SPeter Zijlstra 
37326723911SPeter Zijlstra 	if (*batch == NULL) {
37426723911SPeter Zijlstra 		*batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
37526723911SPeter Zijlstra 		if (*batch == NULL) {
37626723911SPeter Zijlstra 			tlb_remove_table_one(table);
37726723911SPeter Zijlstra 			return;
37826723911SPeter Zijlstra 		}
37926723911SPeter Zijlstra 		(*batch)->nr = 0;
38026723911SPeter Zijlstra 	}
38126723911SPeter Zijlstra 	(*batch)->tables[(*batch)->nr++] = table;
38226723911SPeter Zijlstra 	if ((*batch)->nr == MAX_TABLE_BATCH)
38326723911SPeter Zijlstra 		tlb_table_flush(tlb);
38426723911SPeter Zijlstra }
38526723911SPeter Zijlstra 
3869547d01bSPeter Zijlstra #endif /* CONFIG_HAVE_RCU_TABLE_FREE */
38726723911SPeter Zijlstra 
3881da177e4SLinus Torvalds /*
3891da177e4SLinus Torvalds  * Note: this doesn't free the actual pages themselves. That
3901da177e4SLinus Torvalds  * has been handled earlier when unmapping all the memory regions.
3911da177e4SLinus Torvalds  */
3929e1b32caSBenjamin Herrenschmidt static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd,
3939e1b32caSBenjamin Herrenschmidt 			   unsigned long addr)
3941da177e4SLinus Torvalds {
3952f569afdSMartin Schwidefsky 	pgtable_t token = pmd_pgtable(*pmd);
3961da177e4SLinus Torvalds 	pmd_clear(pmd);
3979e1b32caSBenjamin Herrenschmidt 	pte_free_tlb(tlb, token, addr);
398e1f56c89SKirill A. Shutemov 	atomic_long_dec(&tlb->mm->nr_ptes);
3991da177e4SLinus Torvalds }
4001da177e4SLinus Torvalds 
401e0da382cSHugh Dickins static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
402e0da382cSHugh Dickins 				unsigned long addr, unsigned long end,
403e0da382cSHugh Dickins 				unsigned long floor, unsigned long ceiling)
4041da177e4SLinus Torvalds {
4051da177e4SLinus Torvalds 	pmd_t *pmd;
4061da177e4SLinus Torvalds 	unsigned long next;
407e0da382cSHugh Dickins 	unsigned long start;
4081da177e4SLinus Torvalds 
409e0da382cSHugh Dickins 	start = addr;
4101da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
4111da177e4SLinus Torvalds 	do {
4121da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
4131da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(pmd))
4141da177e4SLinus Torvalds 			continue;
4159e1b32caSBenjamin Herrenschmidt 		free_pte_range(tlb, pmd, addr);
4161da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
4171da177e4SLinus Torvalds 
418e0da382cSHugh Dickins 	start &= PUD_MASK;
419e0da382cSHugh Dickins 	if (start < floor)
420e0da382cSHugh Dickins 		return;
421e0da382cSHugh Dickins 	if (ceiling) {
422e0da382cSHugh Dickins 		ceiling &= PUD_MASK;
423e0da382cSHugh Dickins 		if (!ceiling)
424e0da382cSHugh Dickins 			return;
4251da177e4SLinus Torvalds 	}
426e0da382cSHugh Dickins 	if (end - 1 > ceiling - 1)
427e0da382cSHugh Dickins 		return;
428e0da382cSHugh Dickins 
429e0da382cSHugh Dickins 	pmd = pmd_offset(pud, start);
430e0da382cSHugh Dickins 	pud_clear(pud);
4319e1b32caSBenjamin Herrenschmidt 	pmd_free_tlb(tlb, pmd, start);
432dc6c9a35SKirill A. Shutemov 	mm_dec_nr_pmds(tlb->mm);
4331da177e4SLinus Torvalds }
4341da177e4SLinus Torvalds 
435e0da382cSHugh Dickins static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
436e0da382cSHugh Dickins 				unsigned long addr, unsigned long end,
437e0da382cSHugh Dickins 				unsigned long floor, unsigned long ceiling)
4381da177e4SLinus Torvalds {
4391da177e4SLinus Torvalds 	pud_t *pud;
4401da177e4SLinus Torvalds 	unsigned long next;
441e0da382cSHugh Dickins 	unsigned long start;
4421da177e4SLinus Torvalds 
443e0da382cSHugh Dickins 	start = addr;
4441da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
4451da177e4SLinus Torvalds 	do {
4461da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
4471da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
4481da177e4SLinus Torvalds 			continue;
449e0da382cSHugh Dickins 		free_pmd_range(tlb, pud, addr, next, floor, ceiling);
4501da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
4511da177e4SLinus Torvalds 
452e0da382cSHugh Dickins 	start &= PGDIR_MASK;
453e0da382cSHugh Dickins 	if (start < floor)
454e0da382cSHugh Dickins 		return;
455e0da382cSHugh Dickins 	if (ceiling) {
456e0da382cSHugh Dickins 		ceiling &= PGDIR_MASK;
457e0da382cSHugh Dickins 		if (!ceiling)
458e0da382cSHugh Dickins 			return;
4591da177e4SLinus Torvalds 	}
460e0da382cSHugh Dickins 	if (end - 1 > ceiling - 1)
461e0da382cSHugh Dickins 		return;
462e0da382cSHugh Dickins 
463e0da382cSHugh Dickins 	pud = pud_offset(pgd, start);
464e0da382cSHugh Dickins 	pgd_clear(pgd);
4659e1b32caSBenjamin Herrenschmidt 	pud_free_tlb(tlb, pud, start);
4661da177e4SLinus Torvalds }
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds /*
469e0da382cSHugh Dickins  * This function frees user-level page tables of a process.
4701da177e4SLinus Torvalds  */
47142b77728SJan Beulich void free_pgd_range(struct mmu_gather *tlb,
472e0da382cSHugh Dickins 			unsigned long addr, unsigned long end,
473e0da382cSHugh Dickins 			unsigned long floor, unsigned long ceiling)
4741da177e4SLinus Torvalds {
4751da177e4SLinus Torvalds 	pgd_t *pgd;
4761da177e4SLinus Torvalds 	unsigned long next;
4771da177e4SLinus Torvalds 
478e0da382cSHugh Dickins 	/*
479e0da382cSHugh Dickins 	 * The next few lines have given us lots of grief...
480e0da382cSHugh Dickins 	 *
481e0da382cSHugh Dickins 	 * Why are we testing PMD* at this top level?  Because often
482e0da382cSHugh Dickins 	 * there will be no work to do at all, and we'd prefer not to
483e0da382cSHugh Dickins 	 * go all the way down to the bottom just to discover that.
484e0da382cSHugh Dickins 	 *
485e0da382cSHugh Dickins 	 * Why all these "- 1"s?  Because 0 represents both the bottom
486e0da382cSHugh Dickins 	 * of the address space and the top of it (using -1 for the
487e0da382cSHugh Dickins 	 * top wouldn't help much: the masks would do the wrong thing).
488e0da382cSHugh Dickins 	 * The rule is that addr 0 and floor 0 refer to the bottom of
489e0da382cSHugh Dickins 	 * the address space, but end 0 and ceiling 0 refer to the top
490e0da382cSHugh Dickins 	 * Comparisons need to use "end - 1" and "ceiling - 1" (though
491e0da382cSHugh Dickins 	 * that end 0 case should be mythical).
492e0da382cSHugh Dickins 	 *
493e0da382cSHugh Dickins 	 * Wherever addr is brought up or ceiling brought down, we must
494e0da382cSHugh Dickins 	 * be careful to reject "the opposite 0" before it confuses the
495e0da382cSHugh Dickins 	 * subsequent tests.  But what about where end is brought down
496e0da382cSHugh Dickins 	 * by PMD_SIZE below? no, end can't go down to 0 there.
497e0da382cSHugh Dickins 	 *
498e0da382cSHugh Dickins 	 * Whereas we round start (addr) and ceiling down, by different
499e0da382cSHugh Dickins 	 * masks at different levels, in order to test whether a table
500e0da382cSHugh Dickins 	 * now has no other vmas using it, so can be freed, we don't
501e0da382cSHugh Dickins 	 * bother to round floor or end up - the tests don't need that.
502e0da382cSHugh Dickins 	 */
503e0da382cSHugh Dickins 
504e0da382cSHugh Dickins 	addr &= PMD_MASK;
505e0da382cSHugh Dickins 	if (addr < floor) {
506e0da382cSHugh Dickins 		addr += PMD_SIZE;
507e0da382cSHugh Dickins 		if (!addr)
508e0da382cSHugh Dickins 			return;
509e0da382cSHugh Dickins 	}
510e0da382cSHugh Dickins 	if (ceiling) {
511e0da382cSHugh Dickins 		ceiling &= PMD_MASK;
512e0da382cSHugh Dickins 		if (!ceiling)
513e0da382cSHugh Dickins 			return;
514e0da382cSHugh Dickins 	}
515e0da382cSHugh Dickins 	if (end - 1 > ceiling - 1)
516e0da382cSHugh Dickins 		end -= PMD_SIZE;
517e0da382cSHugh Dickins 	if (addr > end - 1)
518e0da382cSHugh Dickins 		return;
519e0da382cSHugh Dickins 
52042b77728SJan Beulich 	pgd = pgd_offset(tlb->mm, addr);
5211da177e4SLinus Torvalds 	do {
5221da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
5231da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
5241da177e4SLinus Torvalds 			continue;
52542b77728SJan Beulich 		free_pud_range(tlb, pgd, addr, next, floor, ceiling);
5261da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
527e0da382cSHugh Dickins }
528e0da382cSHugh Dickins 
52942b77728SJan Beulich void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma,
530e0da382cSHugh Dickins 		unsigned long floor, unsigned long ceiling)
531e0da382cSHugh Dickins {
532e0da382cSHugh Dickins 	while (vma) {
533e0da382cSHugh Dickins 		struct vm_area_struct *next = vma->vm_next;
534e0da382cSHugh Dickins 		unsigned long addr = vma->vm_start;
535e0da382cSHugh Dickins 
5368f4f8c16SHugh Dickins 		/*
53725d9e2d1Snpiggin@suse.de 		 * Hide vma from rmap and truncate_pagecache before freeing
53825d9e2d1Snpiggin@suse.de 		 * pgtables
5398f4f8c16SHugh Dickins 		 */
5405beb4930SRik van Riel 		unlink_anon_vmas(vma);
5418f4f8c16SHugh Dickins 		unlink_file_vma(vma);
5428f4f8c16SHugh Dickins 
5439da61aefSDavid Gibson 		if (is_vm_hugetlb_page(vma)) {
5443bf5ee95SHugh Dickins 			hugetlb_free_pgd_range(tlb, addr, vma->vm_end,
5453bf5ee95SHugh Dickins 				floor, next? next->vm_start: ceiling);
5463bf5ee95SHugh Dickins 		} else {
5473bf5ee95SHugh Dickins 			/*
5483bf5ee95SHugh Dickins 			 * Optimization: gather nearby vmas into one call down
5493bf5ee95SHugh Dickins 			 */
5503bf5ee95SHugh Dickins 			while (next && next->vm_start <= vma->vm_end + PMD_SIZE
5514866920bSDavid Gibson 			       && !is_vm_hugetlb_page(next)) {
552e0da382cSHugh Dickins 				vma = next;
553e0da382cSHugh Dickins 				next = vma->vm_next;
5545beb4930SRik van Riel 				unlink_anon_vmas(vma);
5558f4f8c16SHugh Dickins 				unlink_file_vma(vma);
556e0da382cSHugh Dickins 			}
5573bf5ee95SHugh Dickins 			free_pgd_range(tlb, addr, vma->vm_end,
558e0da382cSHugh Dickins 				floor, next? next->vm_start: ceiling);
5593bf5ee95SHugh Dickins 		}
560e0da382cSHugh Dickins 		vma = next;
561e0da382cSHugh Dickins 	}
5621da177e4SLinus Torvalds }
5631da177e4SLinus Torvalds 
5648ac1f832SAndrea Arcangeli int __pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma,
5658ac1f832SAndrea Arcangeli 		pmd_t *pmd, unsigned long address)
5661da177e4SLinus Torvalds {
567c4088ebdSKirill A. Shutemov 	spinlock_t *ptl;
5682f569afdSMartin Schwidefsky 	pgtable_t new = pte_alloc_one(mm, address);
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 	if (likely(pmd_none(*pmd))) {	/* Has another populated it ? */
589e1f56c89SKirill A. Shutemov 		atomic_long_inc(&mm->nr_ptes);
5901da177e4SLinus Torvalds 		pmd_populate(mm, pmd, new);
5912f569afdSMartin Schwidefsky 		new = NULL;
592*4b471e88SKirill A. Shutemov 	}
593c4088ebdSKirill A. Shutemov 	spin_unlock(ptl);
5942f569afdSMartin Schwidefsky 	if (new)
5952f569afdSMartin Schwidefsky 		pte_free(mm, new);
5961bb3630eSHugh Dickins 	return 0;
5971da177e4SLinus Torvalds }
5981da177e4SLinus Torvalds 
5991bb3630eSHugh Dickins int __pte_alloc_kernel(pmd_t *pmd, unsigned long address)
6001da177e4SLinus Torvalds {
6011bb3630eSHugh Dickins 	pte_t *new = pte_alloc_one_kernel(&init_mm, address);
6021da177e4SLinus Torvalds 	if (!new)
6031bb3630eSHugh Dickins 		return -ENOMEM;
6041da177e4SLinus Torvalds 
605362a61adSNick Piggin 	smp_wmb(); /* See comment in __pte_alloc */
606362a61adSNick Piggin 
607872fec16SHugh Dickins 	spin_lock(&init_mm.page_table_lock);
6088ac1f832SAndrea Arcangeli 	if (likely(pmd_none(*pmd))) {	/* Has another populated it ? */
609872fec16SHugh Dickins 		pmd_populate_kernel(&init_mm, pmd, new);
6102f569afdSMartin Schwidefsky 		new = NULL;
611*4b471e88SKirill A. Shutemov 	}
612872fec16SHugh Dickins 	spin_unlock(&init_mm.page_table_lock);
6132f569afdSMartin Schwidefsky 	if (new)
6142f569afdSMartin Schwidefsky 		pte_free_kernel(&init_mm, new);
6151bb3630eSHugh Dickins 	return 0;
6161da177e4SLinus Torvalds }
6171da177e4SLinus Torvalds 
618d559db08SKAMEZAWA Hiroyuki static inline void init_rss_vec(int *rss)
619ae859762SHugh Dickins {
620d559db08SKAMEZAWA Hiroyuki 	memset(rss, 0, sizeof(int) * NR_MM_COUNTERS);
621d559db08SKAMEZAWA Hiroyuki }
622d559db08SKAMEZAWA Hiroyuki 
623d559db08SKAMEZAWA Hiroyuki static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss)
624d559db08SKAMEZAWA Hiroyuki {
625d559db08SKAMEZAWA Hiroyuki 	int i;
626d559db08SKAMEZAWA Hiroyuki 
62734e55232SKAMEZAWA Hiroyuki 	if (current->mm == mm)
62805af2e10SDavid Rientjes 		sync_mm_rss(mm);
629d559db08SKAMEZAWA Hiroyuki 	for (i = 0; i < NR_MM_COUNTERS; i++)
630d559db08SKAMEZAWA Hiroyuki 		if (rss[i])
631d559db08SKAMEZAWA Hiroyuki 			add_mm_counter(mm, i, rss[i]);
632ae859762SHugh Dickins }
633ae859762SHugh Dickins 
6341da177e4SLinus Torvalds /*
6356aab341eSLinus Torvalds  * This function is called to print an error when a bad pte
6366aab341eSLinus Torvalds  * is found. For example, we might have a PFN-mapped pte in
6376aab341eSLinus Torvalds  * a region that doesn't allow it.
638b5810039SNick Piggin  *
639b5810039SNick Piggin  * The calling function must still handle the error.
640b5810039SNick Piggin  */
6413dc14741SHugh Dickins static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
6423dc14741SHugh Dickins 			  pte_t pte, struct page *page)
643b5810039SNick Piggin {
6443dc14741SHugh Dickins 	pgd_t *pgd = pgd_offset(vma->vm_mm, addr);
6453dc14741SHugh Dickins 	pud_t *pud = pud_offset(pgd, addr);
6463dc14741SHugh Dickins 	pmd_t *pmd = pmd_offset(pud, addr);
6473dc14741SHugh Dickins 	struct address_space *mapping;
6483dc14741SHugh Dickins 	pgoff_t index;
649d936cf9bSHugh Dickins 	static unsigned long resume;
650d936cf9bSHugh Dickins 	static unsigned long nr_shown;
651d936cf9bSHugh Dickins 	static unsigned long nr_unshown;
652d936cf9bSHugh Dickins 
653d936cf9bSHugh Dickins 	/*
654d936cf9bSHugh Dickins 	 * Allow a burst of 60 reports, then keep quiet for that minute;
655d936cf9bSHugh Dickins 	 * or allow a steady drip of one report per second.
656d936cf9bSHugh Dickins 	 */
657d936cf9bSHugh Dickins 	if (nr_shown == 60) {
658d936cf9bSHugh Dickins 		if (time_before(jiffies, resume)) {
659d936cf9bSHugh Dickins 			nr_unshown++;
660d936cf9bSHugh Dickins 			return;
661d936cf9bSHugh Dickins 		}
662d936cf9bSHugh Dickins 		if (nr_unshown) {
6631e9e6365SHugh Dickins 			printk(KERN_ALERT
6641e9e6365SHugh Dickins 				"BUG: Bad page map: %lu messages suppressed\n",
665d936cf9bSHugh Dickins 				nr_unshown);
666d936cf9bSHugh Dickins 			nr_unshown = 0;
667d936cf9bSHugh Dickins 		}
668d936cf9bSHugh Dickins 		nr_shown = 0;
669d936cf9bSHugh Dickins 	}
670d936cf9bSHugh Dickins 	if (nr_shown++ == 0)
671d936cf9bSHugh Dickins 		resume = jiffies + 60 * HZ;
6723dc14741SHugh Dickins 
6733dc14741SHugh Dickins 	mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL;
6743dc14741SHugh Dickins 	index = linear_page_index(vma, addr);
6753dc14741SHugh Dickins 
6761e9e6365SHugh Dickins 	printk(KERN_ALERT
6771e9e6365SHugh Dickins 		"BUG: Bad page map in process %s  pte:%08llx pmd:%08llx\n",
6783dc14741SHugh Dickins 		current->comm,
6793dc14741SHugh Dickins 		(long long)pte_val(pte), (long long)pmd_val(*pmd));
680718a3821SWu Fengguang 	if (page)
681f0b791a3SDave Hansen 		dump_page(page, "bad pte");
6821e9e6365SHugh Dickins 	printk(KERN_ALERT
6833dc14741SHugh Dickins 		"addr:%p vm_flags:%08lx anon_vma:%p mapping:%p index:%lx\n",
6843dc14741SHugh Dickins 		(void *)addr, vma->vm_flags, vma->anon_vma, mapping, index);
6853dc14741SHugh Dickins 	/*
6863dc14741SHugh Dickins 	 * Choose text because data symbols depend on CONFIG_KALLSYMS_ALL=y
6873dc14741SHugh Dickins 	 */
6882682582aSKonstantin Khlebnikov 	pr_alert("file:%pD fault:%pf mmap:%pf readpage:%pf\n",
6892682582aSKonstantin Khlebnikov 		 vma->vm_file,
6902682582aSKonstantin Khlebnikov 		 vma->vm_ops ? vma->vm_ops->fault : NULL,
6912682582aSKonstantin Khlebnikov 		 vma->vm_file ? vma->vm_file->f_op->mmap : NULL,
6922682582aSKonstantin Khlebnikov 		 mapping ? mapping->a_ops->readpage : NULL);
693b5810039SNick Piggin 	dump_stack();
694373d4d09SRusty Russell 	add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
695b5810039SNick Piggin }
696b5810039SNick Piggin 
697b5810039SNick Piggin /*
6987e675137SNick Piggin  * vm_normal_page -- This function gets the "struct page" associated with a pte.
6996aab341eSLinus Torvalds  *
7007e675137SNick Piggin  * "Special" mappings do not wish to be associated with a "struct page" (either
7017e675137SNick Piggin  * it doesn't exist, or it exists but they don't want to touch it). In this
7027e675137SNick Piggin  * case, NULL is returned here. "Normal" mappings do have a struct page.
703b379d790SJared Hulbert  *
7047e675137SNick Piggin  * There are 2 broad cases. Firstly, an architecture may define a pte_special()
7057e675137SNick Piggin  * pte bit, in which case this function is trivial. Secondly, an architecture
7067e675137SNick Piggin  * may not have a spare pte bit, which requires a more complicated scheme,
7077e675137SNick Piggin  * described below.
7087e675137SNick Piggin  *
7097e675137SNick Piggin  * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a
7107e675137SNick Piggin  * special mapping (even if there are underlying and valid "struct pages").
7117e675137SNick Piggin  * COWed pages of a VM_PFNMAP are always normal.
7126aab341eSLinus Torvalds  *
713b379d790SJared Hulbert  * The way we recognize COWed pages within VM_PFNMAP mappings is through the
714b379d790SJared Hulbert  * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit
7157e675137SNick Piggin  * set, and the vm_pgoff will point to the first PFN mapped: thus every special
7167e675137SNick Piggin  * mapping will always honor the rule
7176aab341eSLinus Torvalds  *
7186aab341eSLinus Torvalds  *	pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
7196aab341eSLinus Torvalds  *
7207e675137SNick Piggin  * And for normal mappings this is false.
721b379d790SJared Hulbert  *
7227e675137SNick Piggin  * This restricts such mappings to be a linear translation from virtual address
7237e675137SNick Piggin  * to pfn. To get around this restriction, we allow arbitrary mappings so long
7247e675137SNick Piggin  * as the vma is not a COW mapping; in that case, we know that all ptes are
7257e675137SNick Piggin  * special (because none can have been COWed).
726b379d790SJared Hulbert  *
727b379d790SJared Hulbert  *
7287e675137SNick Piggin  * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP.
7297e675137SNick Piggin  *
730b379d790SJared Hulbert  * VM_MIXEDMAP mappings can likewise contain memory with or without "struct
731b379d790SJared Hulbert  * page" backing, however the difference is that _all_ pages with a struct
732b379d790SJared Hulbert  * page (that is, those where pfn_valid is true) are refcounted and considered
733b379d790SJared Hulbert  * normal pages by the VM. The disadvantage is that pages are refcounted
734b379d790SJared Hulbert  * (which can be slower and simply not an option for some PFNMAP users). The
735b379d790SJared Hulbert  * advantage is that we don't have to follow the strict linearity rule of
736b379d790SJared Hulbert  * PFNMAP mappings in order to support COWable mappings.
737b379d790SJared Hulbert  *
738ee498ed7SHugh Dickins  */
7397e675137SNick Piggin #ifdef __HAVE_ARCH_PTE_SPECIAL
7407e675137SNick Piggin # define HAVE_PTE_SPECIAL 1
7417e675137SNick Piggin #else
7427e675137SNick Piggin # define HAVE_PTE_SPECIAL 0
7437e675137SNick Piggin #endif
7447e675137SNick Piggin struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
7457e675137SNick Piggin 				pte_t pte)
746ee498ed7SHugh Dickins {
74722b31eecSHugh Dickins 	unsigned long pfn = pte_pfn(pte);
7487e675137SNick Piggin 
7497e675137SNick Piggin 	if (HAVE_PTE_SPECIAL) {
750b38af472SHugh Dickins 		if (likely(!pte_special(pte)))
75122b31eecSHugh Dickins 			goto check_pfn;
752667a0a06SDavid Vrabel 		if (vma->vm_ops && vma->vm_ops->find_special_page)
753667a0a06SDavid Vrabel 			return vma->vm_ops->find_special_page(vma, addr);
754a13ea5b7SHugh Dickins 		if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
755a13ea5b7SHugh Dickins 			return NULL;
75662eede62SHugh Dickins 		if (!is_zero_pfn(pfn))
75722b31eecSHugh Dickins 			print_bad_pte(vma, addr, pte, NULL);
7587e675137SNick Piggin 		return NULL;
7597e675137SNick Piggin 	}
7607e675137SNick Piggin 
7617e675137SNick Piggin 	/* !HAVE_PTE_SPECIAL case follows: */
7627e675137SNick Piggin 
763b379d790SJared Hulbert 	if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
764b379d790SJared Hulbert 		if (vma->vm_flags & VM_MIXEDMAP) {
765b379d790SJared Hulbert 			if (!pfn_valid(pfn))
766b379d790SJared Hulbert 				return NULL;
767b379d790SJared Hulbert 			goto out;
768b379d790SJared Hulbert 		} else {
7697e675137SNick Piggin 			unsigned long off;
7707e675137SNick Piggin 			off = (addr - vma->vm_start) >> PAGE_SHIFT;
7716aab341eSLinus Torvalds 			if (pfn == vma->vm_pgoff + off)
7726aab341eSLinus Torvalds 				return NULL;
77367121172SLinus Torvalds 			if (!is_cow_mapping(vma->vm_flags))
774fb155c16SLinus Torvalds 				return NULL;
7756aab341eSLinus Torvalds 		}
776b379d790SJared Hulbert 	}
7776aab341eSLinus Torvalds 
778b38af472SHugh Dickins 	if (is_zero_pfn(pfn))
779b38af472SHugh Dickins 		return NULL;
78022b31eecSHugh Dickins check_pfn:
78122b31eecSHugh Dickins 	if (unlikely(pfn > highest_memmap_pfn)) {
78222b31eecSHugh Dickins 		print_bad_pte(vma, addr, pte, NULL);
78322b31eecSHugh Dickins 		return NULL;
78422b31eecSHugh Dickins 	}
7856aab341eSLinus Torvalds 
7866aab341eSLinus Torvalds 	/*
7877e675137SNick Piggin 	 * NOTE! We still have PageReserved() pages in the page tables.
7887e675137SNick Piggin 	 * eg. VDSO mappings can cause them to exist.
7896aab341eSLinus Torvalds 	 */
790b379d790SJared Hulbert out:
7916aab341eSLinus Torvalds 	return pfn_to_page(pfn);
792ee498ed7SHugh Dickins }
793ee498ed7SHugh Dickins 
794ee498ed7SHugh Dickins /*
7951da177e4SLinus Torvalds  * copy one vm_area from one task to the other. Assumes the page tables
7961da177e4SLinus Torvalds  * already present in the new task to be cleared in the whole range
7971da177e4SLinus Torvalds  * covered by this vma.
7981da177e4SLinus Torvalds  */
7991da177e4SLinus Torvalds 
800570a335bSHugh Dickins static inline unsigned long
8011da177e4SLinus Torvalds copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
802b5810039SNick Piggin 		pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma,
8038c103762SHugh Dickins 		unsigned long addr, int *rss)
8041da177e4SLinus Torvalds {
805b5810039SNick Piggin 	unsigned long vm_flags = vma->vm_flags;
8061da177e4SLinus Torvalds 	pte_t pte = *src_pte;
8071da177e4SLinus Torvalds 	struct page *page;
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 	/* pte contains position in swap or file, so copy. */
8101da177e4SLinus Torvalds 	if (unlikely(!pte_present(pte))) {
8110697212aSChristoph Lameter 		swp_entry_t entry = pte_to_swp_entry(pte);
8120697212aSChristoph Lameter 
8132022b4d1SHugh Dickins 		if (likely(!non_swap_entry(entry))) {
814570a335bSHugh Dickins 			if (swap_duplicate(entry) < 0)
815570a335bSHugh Dickins 				return entry.val;
816570a335bSHugh Dickins 
8171da177e4SLinus Torvalds 			/* make sure dst_mm is on swapoff's mmlist. */
8181da177e4SLinus Torvalds 			if (unlikely(list_empty(&dst_mm->mmlist))) {
8191da177e4SLinus Torvalds 				spin_lock(&mmlist_lock);
820f412ac08SHugh Dickins 				if (list_empty(&dst_mm->mmlist))
821f412ac08SHugh Dickins 					list_add(&dst_mm->mmlist,
822f412ac08SHugh Dickins 							&src_mm->mmlist);
8231da177e4SLinus Torvalds 				spin_unlock(&mmlist_lock);
8241da177e4SLinus Torvalds 			}
825b084d435SKAMEZAWA Hiroyuki 			rss[MM_SWAPENTS]++;
8262022b4d1SHugh Dickins 		} else if (is_migration_entry(entry)) {
8279f9f1acdSKonstantin Khlebnikov 			page = migration_entry_to_page(entry);
8289f9f1acdSKonstantin Khlebnikov 
829eca56ff9SJerome Marchand 			rss[mm_counter(page)]++;
8309f9f1acdSKonstantin Khlebnikov 
8319f9f1acdSKonstantin Khlebnikov 			if (is_write_migration_entry(entry) &&
8320697212aSChristoph Lameter 					is_cow_mapping(vm_flags)) {
8330697212aSChristoph Lameter 				/*
8349f9f1acdSKonstantin Khlebnikov 				 * COW mappings require pages in both
8359f9f1acdSKonstantin Khlebnikov 				 * parent and child to be set to read.
8360697212aSChristoph Lameter 				 */
8370697212aSChristoph Lameter 				make_migration_entry_read(&entry);
8380697212aSChristoph Lameter 				pte = swp_entry_to_pte(entry);
839c3d16e16SCyrill Gorcunov 				if (pte_swp_soft_dirty(*src_pte))
840c3d16e16SCyrill Gorcunov 					pte = pte_swp_mksoft_dirty(pte);
8410697212aSChristoph Lameter 				set_pte_at(src_mm, addr, src_pte, pte);
8420697212aSChristoph Lameter 			}
8431da177e4SLinus Torvalds 		}
844ae859762SHugh Dickins 		goto out_set_pte;
8451da177e4SLinus Torvalds 	}
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	/*
8481da177e4SLinus Torvalds 	 * If it's a COW mapping, write protect it both
8491da177e4SLinus Torvalds 	 * in the parent and the child
8501da177e4SLinus Torvalds 	 */
85167121172SLinus Torvalds 	if (is_cow_mapping(vm_flags)) {
8521da177e4SLinus Torvalds 		ptep_set_wrprotect(src_mm, addr, src_pte);
8533dc90795SZachary Amsden 		pte = pte_wrprotect(pte);
8541da177e4SLinus Torvalds 	}
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 	/*
8571da177e4SLinus Torvalds 	 * If it's a shared mapping, mark it clean in
8581da177e4SLinus Torvalds 	 * the child
8591da177e4SLinus Torvalds 	 */
8601da177e4SLinus Torvalds 	if (vm_flags & VM_SHARED)
8611da177e4SLinus Torvalds 		pte = pte_mkclean(pte);
8621da177e4SLinus Torvalds 	pte = pte_mkold(pte);
8636aab341eSLinus Torvalds 
8646aab341eSLinus Torvalds 	page = vm_normal_page(vma, addr, pte);
8656aab341eSLinus Torvalds 	if (page) {
8661da177e4SLinus Torvalds 		get_page(page);
86721333b2bSHugh Dickins 		page_dup_rmap(page);
868eca56ff9SJerome Marchand 		rss[mm_counter(page)]++;
8696aab341eSLinus Torvalds 	}
870ae859762SHugh Dickins 
871ae859762SHugh Dickins out_set_pte:
872ae859762SHugh Dickins 	set_pte_at(dst_mm, addr, dst_pte, pte);
873570a335bSHugh Dickins 	return 0;
8741da177e4SLinus Torvalds }
8751da177e4SLinus Torvalds 
87621bda264SJerome Marchand static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
8771da177e4SLinus Torvalds 		   pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
8781da177e4SLinus Torvalds 		   unsigned long addr, unsigned long end)
8791da177e4SLinus Torvalds {
880c36987e2SDaisuke Nishimura 	pte_t *orig_src_pte, *orig_dst_pte;
8811da177e4SLinus Torvalds 	pte_t *src_pte, *dst_pte;
882c74df32cSHugh Dickins 	spinlock_t *src_ptl, *dst_ptl;
883e040f218SHugh Dickins 	int progress = 0;
884d559db08SKAMEZAWA Hiroyuki 	int rss[NR_MM_COUNTERS];
885570a335bSHugh Dickins 	swp_entry_t entry = (swp_entry_t){0};
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds again:
888d559db08SKAMEZAWA Hiroyuki 	init_rss_vec(rss);
889d559db08SKAMEZAWA Hiroyuki 
890c74df32cSHugh Dickins 	dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
8911da177e4SLinus Torvalds 	if (!dst_pte)
8921da177e4SLinus Torvalds 		return -ENOMEM;
893ece0e2b6SPeter Zijlstra 	src_pte = pte_offset_map(src_pmd, addr);
8944c21e2f2SHugh Dickins 	src_ptl = pte_lockptr(src_mm, src_pmd);
895f20dc5f7SIngo Molnar 	spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
896c36987e2SDaisuke Nishimura 	orig_src_pte = src_pte;
897c36987e2SDaisuke Nishimura 	orig_dst_pte = dst_pte;
8986606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds 	do {
9011da177e4SLinus Torvalds 		/*
9021da177e4SLinus Torvalds 		 * We are holding two locks at this point - either of them
9031da177e4SLinus Torvalds 		 * could generate latencies in another task on another CPU.
9041da177e4SLinus Torvalds 		 */
905e040f218SHugh Dickins 		if (progress >= 32) {
906e040f218SHugh Dickins 			progress = 0;
907e040f218SHugh Dickins 			if (need_resched() ||
90895c354feSNick Piggin 			    spin_needbreak(src_ptl) || spin_needbreak(dst_ptl))
9091da177e4SLinus Torvalds 				break;
910e040f218SHugh Dickins 		}
9111da177e4SLinus Torvalds 		if (pte_none(*src_pte)) {
9121da177e4SLinus Torvalds 			progress++;
9131da177e4SLinus Torvalds 			continue;
9141da177e4SLinus Torvalds 		}
915570a335bSHugh Dickins 		entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte,
916570a335bSHugh Dickins 							vma, addr, rss);
917570a335bSHugh Dickins 		if (entry.val)
918570a335bSHugh Dickins 			break;
9191da177e4SLinus Torvalds 		progress += 8;
9201da177e4SLinus Torvalds 	} while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
9211da177e4SLinus Torvalds 
9226606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
923c74df32cSHugh Dickins 	spin_unlock(src_ptl);
924ece0e2b6SPeter Zijlstra 	pte_unmap(orig_src_pte);
925d559db08SKAMEZAWA Hiroyuki 	add_mm_rss_vec(dst_mm, rss);
926c36987e2SDaisuke Nishimura 	pte_unmap_unlock(orig_dst_pte, dst_ptl);
927c74df32cSHugh Dickins 	cond_resched();
928570a335bSHugh Dickins 
929570a335bSHugh Dickins 	if (entry.val) {
930570a335bSHugh Dickins 		if (add_swap_count_continuation(entry, GFP_KERNEL) < 0)
931570a335bSHugh Dickins 			return -ENOMEM;
932570a335bSHugh Dickins 		progress = 0;
933570a335bSHugh Dickins 	}
9341da177e4SLinus Torvalds 	if (addr != end)
9351da177e4SLinus Torvalds 		goto again;
9361da177e4SLinus Torvalds 	return 0;
9371da177e4SLinus Torvalds }
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
9401da177e4SLinus Torvalds 		pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma,
9411da177e4SLinus Torvalds 		unsigned long addr, unsigned long end)
9421da177e4SLinus Torvalds {
9431da177e4SLinus Torvalds 	pmd_t *src_pmd, *dst_pmd;
9441da177e4SLinus Torvalds 	unsigned long next;
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds 	dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
9471da177e4SLinus Torvalds 	if (!dst_pmd)
9481da177e4SLinus Torvalds 		return -ENOMEM;
9491da177e4SLinus Torvalds 	src_pmd = pmd_offset(src_pud, addr);
9501da177e4SLinus Torvalds 	do {
9511da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
95271e3aac0SAndrea Arcangeli 		if (pmd_trans_huge(*src_pmd)) {
95371e3aac0SAndrea Arcangeli 			int err;
95414d1a55cSAndrea Arcangeli 			VM_BUG_ON(next-addr != HPAGE_PMD_SIZE);
95571e3aac0SAndrea Arcangeli 			err = copy_huge_pmd(dst_mm, src_mm,
95671e3aac0SAndrea Arcangeli 					    dst_pmd, src_pmd, addr, vma);
95771e3aac0SAndrea Arcangeli 			if (err == -ENOMEM)
95871e3aac0SAndrea Arcangeli 				return -ENOMEM;
95971e3aac0SAndrea Arcangeli 			if (!err)
96071e3aac0SAndrea Arcangeli 				continue;
96171e3aac0SAndrea Arcangeli 			/* fall through */
96271e3aac0SAndrea Arcangeli 		}
9631da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(src_pmd))
9641da177e4SLinus Torvalds 			continue;
9651da177e4SLinus Torvalds 		if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd,
9661da177e4SLinus Torvalds 						vma, addr, next))
9671da177e4SLinus Torvalds 			return -ENOMEM;
9681da177e4SLinus Torvalds 	} while (dst_pmd++, src_pmd++, addr = next, addr != end);
9691da177e4SLinus Torvalds 	return 0;
9701da177e4SLinus Torvalds }
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
9731da177e4SLinus Torvalds 		pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma,
9741da177e4SLinus Torvalds 		unsigned long addr, unsigned long end)
9751da177e4SLinus Torvalds {
9761da177e4SLinus Torvalds 	pud_t *src_pud, *dst_pud;
9771da177e4SLinus Torvalds 	unsigned long next;
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds 	dst_pud = pud_alloc(dst_mm, dst_pgd, addr);
9801da177e4SLinus Torvalds 	if (!dst_pud)
9811da177e4SLinus Torvalds 		return -ENOMEM;
9821da177e4SLinus Torvalds 	src_pud = pud_offset(src_pgd, addr);
9831da177e4SLinus Torvalds 	do {
9841da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
9851da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(src_pud))
9861da177e4SLinus Torvalds 			continue;
9871da177e4SLinus Torvalds 		if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud,
9881da177e4SLinus Torvalds 						vma, addr, next))
9891da177e4SLinus Torvalds 			return -ENOMEM;
9901da177e4SLinus Torvalds 	} while (dst_pud++, src_pud++, addr = next, addr != end);
9911da177e4SLinus Torvalds 	return 0;
9921da177e4SLinus Torvalds }
9931da177e4SLinus Torvalds 
9941da177e4SLinus Torvalds int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
9951da177e4SLinus Torvalds 		struct vm_area_struct *vma)
9961da177e4SLinus Torvalds {
9971da177e4SLinus Torvalds 	pgd_t *src_pgd, *dst_pgd;
9981da177e4SLinus Torvalds 	unsigned long next;
9991da177e4SLinus Torvalds 	unsigned long addr = vma->vm_start;
10001da177e4SLinus Torvalds 	unsigned long end = vma->vm_end;
10012ec74c3eSSagi Grimberg 	unsigned long mmun_start;	/* For mmu_notifiers */
10022ec74c3eSSagi Grimberg 	unsigned long mmun_end;		/* For mmu_notifiers */
10032ec74c3eSSagi Grimberg 	bool is_cow;
1004cddb8a5cSAndrea Arcangeli 	int ret;
10051da177e4SLinus Torvalds 
1006d992895bSNick Piggin 	/*
1007d992895bSNick Piggin 	 * Don't copy ptes where a page fault will fill them correctly.
1008d992895bSNick Piggin 	 * Fork becomes much lighter when there are big shared or private
1009d992895bSNick Piggin 	 * readonly mappings. The tradeoff is that copy_page_range is more
1010d992895bSNick Piggin 	 * efficient than faulting.
1011d992895bSNick Piggin 	 */
10120661a336SKirill A. Shutemov 	if (!(vma->vm_flags & (VM_HUGETLB | VM_PFNMAP | VM_MIXEDMAP)) &&
10130661a336SKirill A. Shutemov 			!vma->anon_vma)
1014d992895bSNick Piggin 		return 0;
1015d992895bSNick Piggin 
10161da177e4SLinus Torvalds 	if (is_vm_hugetlb_page(vma))
10171da177e4SLinus Torvalds 		return copy_hugetlb_page_range(dst_mm, src_mm, vma);
10181da177e4SLinus Torvalds 
1019b3b9c293SKonstantin Khlebnikov 	if (unlikely(vma->vm_flags & VM_PFNMAP)) {
10202ab64037Svenkatesh.pallipadi@intel.com 		/*
10212ab64037Svenkatesh.pallipadi@intel.com 		 * We do not free on error cases below as remove_vma
10222ab64037Svenkatesh.pallipadi@intel.com 		 * gets called on error from higher level routine
10232ab64037Svenkatesh.pallipadi@intel.com 		 */
10245180da41SSuresh Siddha 		ret = track_pfn_copy(vma);
10252ab64037Svenkatesh.pallipadi@intel.com 		if (ret)
10262ab64037Svenkatesh.pallipadi@intel.com 			return ret;
10272ab64037Svenkatesh.pallipadi@intel.com 	}
10282ab64037Svenkatesh.pallipadi@intel.com 
1029cddb8a5cSAndrea Arcangeli 	/*
1030cddb8a5cSAndrea Arcangeli 	 * We need to invalidate the secondary MMU mappings only when
1031cddb8a5cSAndrea Arcangeli 	 * there could be a permission downgrade on the ptes of the
1032cddb8a5cSAndrea Arcangeli 	 * parent mm. And a permission downgrade will only happen if
1033cddb8a5cSAndrea Arcangeli 	 * is_cow_mapping() returns true.
1034cddb8a5cSAndrea Arcangeli 	 */
10352ec74c3eSSagi Grimberg 	is_cow = is_cow_mapping(vma->vm_flags);
10362ec74c3eSSagi Grimberg 	mmun_start = addr;
10372ec74c3eSSagi Grimberg 	mmun_end   = end;
10382ec74c3eSSagi Grimberg 	if (is_cow)
10392ec74c3eSSagi Grimberg 		mmu_notifier_invalidate_range_start(src_mm, mmun_start,
10402ec74c3eSSagi Grimberg 						    mmun_end);
1041cddb8a5cSAndrea Arcangeli 
1042cddb8a5cSAndrea Arcangeli 	ret = 0;
10431da177e4SLinus Torvalds 	dst_pgd = pgd_offset(dst_mm, addr);
10441da177e4SLinus Torvalds 	src_pgd = pgd_offset(src_mm, addr);
10451da177e4SLinus Torvalds 	do {
10461da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
10471da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(src_pgd))
10481da177e4SLinus Torvalds 			continue;
1049cddb8a5cSAndrea Arcangeli 		if (unlikely(copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd,
1050cddb8a5cSAndrea Arcangeli 					    vma, addr, next))) {
1051cddb8a5cSAndrea Arcangeli 			ret = -ENOMEM;
1052cddb8a5cSAndrea Arcangeli 			break;
1053cddb8a5cSAndrea Arcangeli 		}
10541da177e4SLinus Torvalds 	} while (dst_pgd++, src_pgd++, addr = next, addr != end);
1055cddb8a5cSAndrea Arcangeli 
10562ec74c3eSSagi Grimberg 	if (is_cow)
10572ec74c3eSSagi Grimberg 		mmu_notifier_invalidate_range_end(src_mm, mmun_start, mmun_end);
1058cddb8a5cSAndrea Arcangeli 	return ret;
10591da177e4SLinus Torvalds }
10601da177e4SLinus Torvalds 
106151c6f666SRobin Holt static unsigned long zap_pte_range(struct mmu_gather *tlb,
1062b5810039SNick Piggin 				struct vm_area_struct *vma, pmd_t *pmd,
10631da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
106497a89413SPeter Zijlstra 				struct zap_details *details)
10651da177e4SLinus Torvalds {
1066b5810039SNick Piggin 	struct mm_struct *mm = tlb->mm;
1067d16dfc55SPeter Zijlstra 	int force_flush = 0;
1068d559db08SKAMEZAWA Hiroyuki 	int rss[NR_MM_COUNTERS];
106997a89413SPeter Zijlstra 	spinlock_t *ptl;
10705f1a1907SSteven Rostedt 	pte_t *start_pte;
107197a89413SPeter Zijlstra 	pte_t *pte;
10728a5f14a2SKirill A. Shutemov 	swp_entry_t entry;
1073d559db08SKAMEZAWA Hiroyuki 
1074d16dfc55SPeter Zijlstra again:
1075e303297eSPeter Zijlstra 	init_rss_vec(rss);
10765f1a1907SSteven Rostedt 	start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
10775f1a1907SSteven Rostedt 	pte = start_pte;
10786606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
10791da177e4SLinus Torvalds 	do {
10801da177e4SLinus Torvalds 		pte_t ptent = *pte;
108151c6f666SRobin Holt 		if (pte_none(ptent)) {
10821da177e4SLinus Torvalds 			continue;
108351c6f666SRobin Holt 		}
108451c6f666SRobin Holt 
10856f5e6b9eSHugh Dickins 		if (pte_present(ptent)) {
10866f5e6b9eSHugh Dickins 			struct page *page;
10876f5e6b9eSHugh Dickins 
10886aab341eSLinus Torvalds 			page = vm_normal_page(vma, addr, ptent);
10891da177e4SLinus Torvalds 			if (unlikely(details) && page) {
10901da177e4SLinus Torvalds 				/*
10911da177e4SLinus Torvalds 				 * unmap_shared_mapping_pages() wants to
10921da177e4SLinus Torvalds 				 * invalidate cache without truncating:
10931da177e4SLinus Torvalds 				 * unmap shared but keep private pages.
10941da177e4SLinus Torvalds 				 */
10951da177e4SLinus Torvalds 				if (details->check_mapping &&
10961da177e4SLinus Torvalds 				    details->check_mapping != page->mapping)
10971da177e4SLinus Torvalds 					continue;
10981da177e4SLinus Torvalds 			}
1099b5810039SNick Piggin 			ptent = ptep_get_and_clear_full(mm, addr, pte,
1100a600388dSZachary Amsden 							tlb->fullmm);
11011da177e4SLinus Torvalds 			tlb_remove_tlb_entry(tlb, pte, addr);
11021da177e4SLinus Torvalds 			if (unlikely(!page))
11031da177e4SLinus Torvalds 				continue;
1104eca56ff9SJerome Marchand 
1105eca56ff9SJerome Marchand 			if (!PageAnon(page)) {
11061cf35d47SLinus Torvalds 				if (pte_dirty(ptent)) {
11071cf35d47SLinus Torvalds 					force_flush = 1;
11086237bcd9SHugh Dickins 					set_page_dirty(page);
11091cf35d47SLinus Torvalds 				}
11104917e5d0SJohannes Weiner 				if (pte_young(ptent) &&
111164363aadSJoe Perches 				    likely(!(vma->vm_flags & VM_SEQ_READ)))
1112bf3f3bc5SNick Piggin 					mark_page_accessed(page);
11136237bcd9SHugh Dickins 			}
1114eca56ff9SJerome Marchand 			rss[mm_counter(page)]--;
1115d281ee61SKirill A. Shutemov 			page_remove_rmap(page, false);
11163dc14741SHugh Dickins 			if (unlikely(page_mapcount(page) < 0))
11173dc14741SHugh Dickins 				print_bad_pte(vma, addr, ptent, page);
11181cf35d47SLinus Torvalds 			if (unlikely(!__tlb_remove_page(tlb, page))) {
11191cf35d47SLinus Torvalds 				force_flush = 1;
1120ce9ec37bSWill Deacon 				addr += PAGE_SIZE;
1121d16dfc55SPeter Zijlstra 				break;
11221cf35d47SLinus Torvalds 			}
11231da177e4SLinus Torvalds 			continue;
11241da177e4SLinus Torvalds 		}
11258a5f14a2SKirill A. Shutemov 		/* If details->check_mapping, we leave swap entries. */
11261da177e4SLinus Torvalds 		if (unlikely(details))
11271da177e4SLinus Torvalds 			continue;
1128b084d435SKAMEZAWA Hiroyuki 
11298a5f14a2SKirill A. Shutemov 		entry = pte_to_swp_entry(ptent);
1130b084d435SKAMEZAWA Hiroyuki 		if (!non_swap_entry(entry))
1131b084d435SKAMEZAWA Hiroyuki 			rss[MM_SWAPENTS]--;
11329f9f1acdSKonstantin Khlebnikov 		else if (is_migration_entry(entry)) {
11339f9f1acdSKonstantin Khlebnikov 			struct page *page;
11349f9f1acdSKonstantin Khlebnikov 
11359f9f1acdSKonstantin Khlebnikov 			page = migration_entry_to_page(entry);
1136eca56ff9SJerome Marchand 			rss[mm_counter(page)]--;
11379f9f1acdSKonstantin Khlebnikov 		}
1138b084d435SKAMEZAWA Hiroyuki 		if (unlikely(!free_swap_and_cache(entry)))
11392509ef26SHugh Dickins 			print_bad_pte(vma, addr, ptent, NULL);
11409888a1caSZachary Amsden 		pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
114197a89413SPeter Zijlstra 	} while (pte++, addr += PAGE_SIZE, addr != end);
1142ae859762SHugh Dickins 
1143d559db08SKAMEZAWA Hiroyuki 	add_mm_rss_vec(mm, rss);
11446606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
114551c6f666SRobin Holt 
11461cf35d47SLinus Torvalds 	/* Do the actual TLB flush before dropping ptl */
1147fb7332a9SWill Deacon 	if (force_flush)
11481cf35d47SLinus Torvalds 		tlb_flush_mmu_tlbonly(tlb);
11491cf35d47SLinus Torvalds 	pte_unmap_unlock(start_pte, ptl);
11501cf35d47SLinus Torvalds 
11511cf35d47SLinus Torvalds 	/*
11521cf35d47SLinus Torvalds 	 * If we forced a TLB flush (either due to running out of
11531cf35d47SLinus Torvalds 	 * batch buffers or because we needed to flush dirty TLB
11541cf35d47SLinus Torvalds 	 * entries before releasing the ptl), free the batched
11551cf35d47SLinus Torvalds 	 * memory too. Restart if we didn't do everything.
11561cf35d47SLinus Torvalds 	 */
11571cf35d47SLinus Torvalds 	if (force_flush) {
11581cf35d47SLinus Torvalds 		force_flush = 0;
11591cf35d47SLinus Torvalds 		tlb_flush_mmu_free(tlb);
11602b047252SLinus Torvalds 
11612b047252SLinus Torvalds 		if (addr != end)
1162d16dfc55SPeter Zijlstra 			goto again;
1163d16dfc55SPeter Zijlstra 	}
1164d16dfc55SPeter Zijlstra 
116551c6f666SRobin Holt 	return addr;
11661da177e4SLinus Torvalds }
11671da177e4SLinus Torvalds 
116851c6f666SRobin Holt static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
1169b5810039SNick Piggin 				struct vm_area_struct *vma, pud_t *pud,
11701da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
117197a89413SPeter Zijlstra 				struct zap_details *details)
11721da177e4SLinus Torvalds {
11731da177e4SLinus Torvalds 	pmd_t *pmd;
11741da177e4SLinus Torvalds 	unsigned long next;
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
11771da177e4SLinus Torvalds 	do {
11781da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
117971e3aac0SAndrea Arcangeli 		if (pmd_trans_huge(*pmd)) {
118014d1a55cSAndrea Arcangeli 			if (next - addr != HPAGE_PMD_SIZE) {
1181e0897d75SDavid Rientjes #ifdef CONFIG_DEBUG_VM
1182e0897d75SDavid Rientjes 				if (!rwsem_is_locked(&tlb->mm->mmap_sem)) {
1183e0897d75SDavid 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",
1184e0897d75SDavid Rientjes 						__func__, addr, end,
1185e0897d75SDavid Rientjes 						vma->vm_start,
1186e0897d75SDavid Rientjes 						vma->vm_end);
1187e0897d75SDavid Rientjes 					BUG();
1188e0897d75SDavid Rientjes 				}
1189e0897d75SDavid Rientjes #endif
119078ddc534SKirill A. Shutemov 				split_huge_pmd(vma, pmd, addr);
1191f21760b1SShaohua Li 			} else if (zap_huge_pmd(tlb, vma, pmd, addr))
11921a5a9906SAndrea Arcangeli 				goto next;
119371e3aac0SAndrea Arcangeli 			/* fall through */
119471e3aac0SAndrea Arcangeli 		}
11951a5a9906SAndrea Arcangeli 		/*
11961a5a9906SAndrea Arcangeli 		 * Here there can be other concurrent MADV_DONTNEED or
11971a5a9906SAndrea Arcangeli 		 * trans huge page faults running, and if the pmd is
11981a5a9906SAndrea Arcangeli 		 * none or trans huge it can change under us. This is
11991a5a9906SAndrea Arcangeli 		 * because MADV_DONTNEED holds the mmap_sem in read
12001a5a9906SAndrea Arcangeli 		 * mode.
12011a5a9906SAndrea Arcangeli 		 */
12021a5a9906SAndrea Arcangeli 		if (pmd_none_or_trans_huge_or_clear_bad(pmd))
12031a5a9906SAndrea Arcangeli 			goto next;
120497a89413SPeter Zijlstra 		next = zap_pte_range(tlb, vma, pmd, addr, next, details);
12051a5a9906SAndrea Arcangeli next:
120697a89413SPeter Zijlstra 		cond_resched();
120797a89413SPeter Zijlstra 	} while (pmd++, addr = next, addr != end);
120851c6f666SRobin Holt 
120951c6f666SRobin Holt 	return addr;
12101da177e4SLinus Torvalds }
12111da177e4SLinus Torvalds 
121251c6f666SRobin Holt static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
1213b5810039SNick Piggin 				struct vm_area_struct *vma, pgd_t *pgd,
12141da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
121597a89413SPeter Zijlstra 				struct zap_details *details)
12161da177e4SLinus Torvalds {
12171da177e4SLinus Torvalds 	pud_t *pud;
12181da177e4SLinus Torvalds 	unsigned long next;
12191da177e4SLinus Torvalds 
12201da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
12211da177e4SLinus Torvalds 	do {
12221da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
122397a89413SPeter Zijlstra 		if (pud_none_or_clear_bad(pud))
12241da177e4SLinus Torvalds 			continue;
122597a89413SPeter Zijlstra 		next = zap_pmd_range(tlb, vma, pud, addr, next, details);
122697a89413SPeter Zijlstra 	} while (pud++, addr = next, addr != end);
122751c6f666SRobin Holt 
122851c6f666SRobin Holt 	return addr;
12291da177e4SLinus Torvalds }
12301da177e4SLinus Torvalds 
1231038c7aa1SAl Viro static void unmap_page_range(struct mmu_gather *tlb,
123251c6f666SRobin Holt 			     struct vm_area_struct *vma,
12331da177e4SLinus Torvalds 			     unsigned long addr, unsigned long end,
123497a89413SPeter Zijlstra 			     struct zap_details *details)
12351da177e4SLinus Torvalds {
12361da177e4SLinus Torvalds 	pgd_t *pgd;
12371da177e4SLinus Torvalds 	unsigned long next;
12381da177e4SLinus Torvalds 
12398a5f14a2SKirill A. Shutemov 	if (details && !details->check_mapping)
12401da177e4SLinus Torvalds 		details = NULL;
12411da177e4SLinus Torvalds 
12421da177e4SLinus Torvalds 	BUG_ON(addr >= end);
12431da177e4SLinus Torvalds 	tlb_start_vma(tlb, vma);
12441da177e4SLinus Torvalds 	pgd = pgd_offset(vma->vm_mm, addr);
12451da177e4SLinus Torvalds 	do {
12461da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
124797a89413SPeter Zijlstra 		if (pgd_none_or_clear_bad(pgd))
12481da177e4SLinus Torvalds 			continue;
124997a89413SPeter Zijlstra 		next = zap_pud_range(tlb, vma, pgd, addr, next, details);
125097a89413SPeter Zijlstra 	} while (pgd++, addr = next, addr != end);
12511da177e4SLinus Torvalds 	tlb_end_vma(tlb, vma);
12521da177e4SLinus Torvalds }
12531da177e4SLinus Torvalds 
1254f5cc4eefSAl Viro 
1255f5cc4eefSAl Viro static void unmap_single_vma(struct mmu_gather *tlb,
12561da177e4SLinus Torvalds 		struct vm_area_struct *vma, unsigned long start_addr,
12574f74d2c8SLinus Torvalds 		unsigned long end_addr,
12581da177e4SLinus Torvalds 		struct zap_details *details)
12591da177e4SLinus Torvalds {
1260f5cc4eefSAl Viro 	unsigned long start = max(vma->vm_start, start_addr);
12611da177e4SLinus Torvalds 	unsigned long end;
12621da177e4SLinus Torvalds 
12631da177e4SLinus Torvalds 	if (start >= vma->vm_end)
1264f5cc4eefSAl Viro 		return;
12651da177e4SLinus Torvalds 	end = min(vma->vm_end, end_addr);
12661da177e4SLinus Torvalds 	if (end <= vma->vm_start)
1267f5cc4eefSAl Viro 		return;
12681da177e4SLinus Torvalds 
1269cbc91f71SSrikar Dronamraju 	if (vma->vm_file)
1270cbc91f71SSrikar Dronamraju 		uprobe_munmap(vma, start, end);
1271cbc91f71SSrikar Dronamraju 
1272b3b9c293SKonstantin Khlebnikov 	if (unlikely(vma->vm_flags & VM_PFNMAP))
12735180da41SSuresh Siddha 		untrack_pfn(vma, 0, 0);
12742ab64037Svenkatesh.pallipadi@intel.com 
12758b2a1238SAl Viro 	if (start != end) {
127651c6f666SRobin Holt 		if (unlikely(is_vm_hugetlb_page(vma))) {
1277a137e1ccSAndi Kleen 			/*
1278a137e1ccSAndi Kleen 			 * It is undesirable to test vma->vm_file as it
1279a137e1ccSAndi Kleen 			 * should be non-null for valid hugetlb area.
1280a137e1ccSAndi Kleen 			 * However, vm_file will be NULL in the error
12817aa6b4adSDavidlohr Bueso 			 * cleanup path of mmap_region. When
1282a137e1ccSAndi Kleen 			 * hugetlbfs ->mmap method fails,
12837aa6b4adSDavidlohr Bueso 			 * mmap_region() nullifies vma->vm_file
1284a137e1ccSAndi Kleen 			 * before calling this function to clean up.
1285a137e1ccSAndi Kleen 			 * Since no pte has actually been setup, it is
1286a137e1ccSAndi Kleen 			 * safe to do nothing in this case.
1287a137e1ccSAndi Kleen 			 */
128824669e58SAneesh Kumar K.V 			if (vma->vm_file) {
128983cde9e8SDavidlohr Bueso 				i_mmap_lock_write(vma->vm_file->f_mapping);
1290d833352aSMel Gorman 				__unmap_hugepage_range_final(tlb, vma, start, end, NULL);
129183cde9e8SDavidlohr Bueso 				i_mmap_unlock_write(vma->vm_file->f_mapping);
129224669e58SAneesh Kumar K.V 			}
129351c6f666SRobin Holt 		} else
1294038c7aa1SAl Viro 			unmap_page_range(tlb, vma, start, end, details);
129597a89413SPeter Zijlstra 	}
129651c6f666SRobin Holt }
12971da177e4SLinus Torvalds 
1298f5cc4eefSAl Viro /**
1299f5cc4eefSAl Viro  * unmap_vmas - unmap a range of memory covered by a list of vma's
1300f5cc4eefSAl Viro  * @tlb: address of the caller's struct mmu_gather
1301f5cc4eefSAl Viro  * @vma: the starting vma
1302f5cc4eefSAl Viro  * @start_addr: virtual address at which to start unmapping
1303f5cc4eefSAl Viro  * @end_addr: virtual address at which to end unmapping
1304f5cc4eefSAl Viro  *
1305f5cc4eefSAl Viro  * Unmap all pages in the vma list.
1306f5cc4eefSAl Viro  *
1307f5cc4eefSAl Viro  * Only addresses between `start' and `end' will be unmapped.
1308f5cc4eefSAl Viro  *
1309f5cc4eefSAl Viro  * The VMA list must be sorted in ascending virtual address order.
1310f5cc4eefSAl Viro  *
1311f5cc4eefSAl Viro  * unmap_vmas() assumes that the caller will flush the whole unmapped address
1312f5cc4eefSAl Viro  * range after unmap_vmas() returns.  So the only responsibility here is to
1313f5cc4eefSAl Viro  * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
1314f5cc4eefSAl Viro  * drops the lock and schedules.
1315f5cc4eefSAl Viro  */
1316f5cc4eefSAl Viro void unmap_vmas(struct mmu_gather *tlb,
1317f5cc4eefSAl Viro 		struct vm_area_struct *vma, unsigned long start_addr,
13184f74d2c8SLinus Torvalds 		unsigned long end_addr)
1319f5cc4eefSAl Viro {
1320f5cc4eefSAl Viro 	struct mm_struct *mm = vma->vm_mm;
1321f5cc4eefSAl Viro 
1322f5cc4eefSAl Viro 	mmu_notifier_invalidate_range_start(mm, start_addr, end_addr);
1323f5cc4eefSAl Viro 	for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next)
13244f74d2c8SLinus Torvalds 		unmap_single_vma(tlb, vma, start_addr, end_addr, NULL);
1325cddb8a5cSAndrea Arcangeli 	mmu_notifier_invalidate_range_end(mm, start_addr, end_addr);
13261da177e4SLinus Torvalds }
13271da177e4SLinus Torvalds 
13281da177e4SLinus Torvalds /**
13291da177e4SLinus Torvalds  * zap_page_range - remove user pages in a given range
13301da177e4SLinus Torvalds  * @vma: vm_area_struct holding the applicable pages
1331eb4546bbSRandy Dunlap  * @start: starting address of pages to zap
13321da177e4SLinus Torvalds  * @size: number of bytes to zap
13338a5f14a2SKirill A. Shutemov  * @details: details of shared cache invalidation
1334f5cc4eefSAl Viro  *
1335f5cc4eefSAl Viro  * Caller must protect the VMA list
13361da177e4SLinus Torvalds  */
13377e027b14SLinus Torvalds void zap_page_range(struct vm_area_struct *vma, unsigned long start,
13381da177e4SLinus Torvalds 		unsigned long size, struct zap_details *details)
13391da177e4SLinus Torvalds {
13401da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
1341d16dfc55SPeter Zijlstra 	struct mmu_gather tlb;
13427e027b14SLinus Torvalds 	unsigned long end = start + size;
13431da177e4SLinus Torvalds 
13441da177e4SLinus Torvalds 	lru_add_drain();
13452b047252SLinus Torvalds 	tlb_gather_mmu(&tlb, mm, start, end);
1346365e9c87SHugh Dickins 	update_hiwater_rss(mm);
13477e027b14SLinus Torvalds 	mmu_notifier_invalidate_range_start(mm, start, end);
13487e027b14SLinus Torvalds 	for ( ; vma && vma->vm_start < end; vma = vma->vm_next)
13494f74d2c8SLinus Torvalds 		unmap_single_vma(&tlb, vma, start, end, details);
13507e027b14SLinus Torvalds 	mmu_notifier_invalidate_range_end(mm, start, end);
13517e027b14SLinus Torvalds 	tlb_finish_mmu(&tlb, start, end);
13521da177e4SLinus Torvalds }
13531da177e4SLinus Torvalds 
1354c627f9ccSJack Steiner /**
1355f5cc4eefSAl Viro  * zap_page_range_single - remove user pages in a given range
1356f5cc4eefSAl Viro  * @vma: vm_area_struct holding the applicable pages
1357f5cc4eefSAl Viro  * @address: starting address of pages to zap
1358f5cc4eefSAl Viro  * @size: number of bytes to zap
13598a5f14a2SKirill A. Shutemov  * @details: details of shared cache invalidation
1360f5cc4eefSAl Viro  *
1361f5cc4eefSAl Viro  * The range must fit into one VMA.
1362f5cc4eefSAl Viro  */
1363f5cc4eefSAl Viro static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,
1364f5cc4eefSAl Viro 		unsigned long size, struct zap_details *details)
1365f5cc4eefSAl Viro {
1366f5cc4eefSAl Viro 	struct mm_struct *mm = vma->vm_mm;
1367f5cc4eefSAl Viro 	struct mmu_gather tlb;
1368f5cc4eefSAl Viro 	unsigned long end = address + size;
1369f5cc4eefSAl Viro 
1370f5cc4eefSAl Viro 	lru_add_drain();
13712b047252SLinus Torvalds 	tlb_gather_mmu(&tlb, mm, address, end);
1372f5cc4eefSAl Viro 	update_hiwater_rss(mm);
1373f5cc4eefSAl Viro 	mmu_notifier_invalidate_range_start(mm, address, end);
13744f74d2c8SLinus Torvalds 	unmap_single_vma(&tlb, vma, address, end, details);
1375f5cc4eefSAl Viro 	mmu_notifier_invalidate_range_end(mm, address, end);
1376f5cc4eefSAl Viro 	tlb_finish_mmu(&tlb, address, end);
13771da177e4SLinus Torvalds }
13781da177e4SLinus Torvalds 
1379c627f9ccSJack Steiner /**
1380c627f9ccSJack Steiner  * zap_vma_ptes - remove ptes mapping the vma
1381c627f9ccSJack Steiner  * @vma: vm_area_struct holding ptes to be zapped
1382c627f9ccSJack Steiner  * @address: starting address of pages to zap
1383c627f9ccSJack Steiner  * @size: number of bytes to zap
1384c627f9ccSJack Steiner  *
1385c627f9ccSJack Steiner  * This function only unmaps ptes assigned to VM_PFNMAP vmas.
1386c627f9ccSJack Steiner  *
1387c627f9ccSJack Steiner  * The entire address range must be fully contained within the vma.
1388c627f9ccSJack Steiner  *
1389c627f9ccSJack Steiner  * Returns 0 if successful.
1390c627f9ccSJack Steiner  */
1391c627f9ccSJack Steiner int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
1392c627f9ccSJack Steiner 		unsigned long size)
1393c627f9ccSJack Steiner {
1394c627f9ccSJack Steiner 	if (address < vma->vm_start || address + size > vma->vm_end ||
1395c627f9ccSJack Steiner 	    		!(vma->vm_flags & VM_PFNMAP))
1396c627f9ccSJack Steiner 		return -1;
1397f5cc4eefSAl Viro 	zap_page_range_single(vma, address, size, NULL);
1398c627f9ccSJack Steiner 	return 0;
1399c627f9ccSJack Steiner }
1400c627f9ccSJack Steiner EXPORT_SYMBOL_GPL(zap_vma_ptes);
1401c627f9ccSJack Steiner 
140225ca1d6cSNamhyung Kim pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
1403920c7a5dSHarvey Harrison 			spinlock_t **ptl)
1404c9cfcddfSLinus Torvalds {
1405c9cfcddfSLinus Torvalds 	pgd_t * pgd = pgd_offset(mm, addr);
1406c9cfcddfSLinus Torvalds 	pud_t * pud = pud_alloc(mm, pgd, addr);
1407c9cfcddfSLinus Torvalds 	if (pud) {
140849c91fb0STrond Myklebust 		pmd_t * pmd = pmd_alloc(mm, pud, addr);
1409f66055abSAndrea Arcangeli 		if (pmd) {
1410f66055abSAndrea Arcangeli 			VM_BUG_ON(pmd_trans_huge(*pmd));
1411c9cfcddfSLinus Torvalds 			return pte_alloc_map_lock(mm, pmd, addr, ptl);
1412c9cfcddfSLinus Torvalds 		}
1413f66055abSAndrea Arcangeli 	}
1414c9cfcddfSLinus Torvalds 	return NULL;
1415c9cfcddfSLinus Torvalds }
1416c9cfcddfSLinus Torvalds 
14171da177e4SLinus Torvalds /*
1418238f58d8SLinus Torvalds  * This is the old fallback for page remapping.
1419238f58d8SLinus Torvalds  *
1420238f58d8SLinus Torvalds  * For historical reasons, it only allows reserved pages. Only
1421238f58d8SLinus Torvalds  * old drivers should use this, and they needed to mark their
1422238f58d8SLinus Torvalds  * pages reserved for the old functions anyway.
1423238f58d8SLinus Torvalds  */
1424423bad60SNick Piggin static int insert_page(struct vm_area_struct *vma, unsigned long addr,
1425423bad60SNick Piggin 			struct page *page, pgprot_t prot)
1426238f58d8SLinus Torvalds {
1427423bad60SNick Piggin 	struct mm_struct *mm = vma->vm_mm;
1428238f58d8SLinus Torvalds 	int retval;
1429238f58d8SLinus Torvalds 	pte_t *pte;
1430238f58d8SLinus Torvalds 	spinlock_t *ptl;
1431238f58d8SLinus Torvalds 
1432238f58d8SLinus Torvalds 	retval = -EINVAL;
1433a145dd41SLinus Torvalds 	if (PageAnon(page))
14345b4e655eSKAMEZAWA Hiroyuki 		goto out;
1435238f58d8SLinus Torvalds 	retval = -ENOMEM;
1436238f58d8SLinus Torvalds 	flush_dcache_page(page);
1437c9cfcddfSLinus Torvalds 	pte = get_locked_pte(mm, addr, &ptl);
1438238f58d8SLinus Torvalds 	if (!pte)
14395b4e655eSKAMEZAWA Hiroyuki 		goto out;
1440238f58d8SLinus Torvalds 	retval = -EBUSY;
1441238f58d8SLinus Torvalds 	if (!pte_none(*pte))
1442238f58d8SLinus Torvalds 		goto out_unlock;
1443238f58d8SLinus Torvalds 
1444238f58d8SLinus Torvalds 	/* Ok, finally just insert the thing.. */
1445238f58d8SLinus Torvalds 	get_page(page);
1446eca56ff9SJerome Marchand 	inc_mm_counter_fast(mm, mm_counter_file(page));
1447238f58d8SLinus Torvalds 	page_add_file_rmap(page);
1448238f58d8SLinus Torvalds 	set_pte_at(mm, addr, pte, mk_pte(page, prot));
1449238f58d8SLinus Torvalds 
1450238f58d8SLinus Torvalds 	retval = 0;
14518a9f3ccdSBalbir Singh 	pte_unmap_unlock(pte, ptl);
14528a9f3ccdSBalbir Singh 	return retval;
1453238f58d8SLinus Torvalds out_unlock:
1454238f58d8SLinus Torvalds 	pte_unmap_unlock(pte, ptl);
1455238f58d8SLinus Torvalds out:
1456238f58d8SLinus Torvalds 	return retval;
1457238f58d8SLinus Torvalds }
1458238f58d8SLinus Torvalds 
1459bfa5bf6dSRolf Eike Beer /**
1460bfa5bf6dSRolf Eike Beer  * vm_insert_page - insert single page into user vma
1461bfa5bf6dSRolf Eike Beer  * @vma: user vma to map to
1462bfa5bf6dSRolf Eike Beer  * @addr: target user address of this page
1463bfa5bf6dSRolf Eike Beer  * @page: source kernel page
1464bfa5bf6dSRolf Eike Beer  *
1465a145dd41SLinus Torvalds  * This allows drivers to insert individual pages they've allocated
1466a145dd41SLinus Torvalds  * into a user vma.
1467a145dd41SLinus Torvalds  *
1468a145dd41SLinus Torvalds  * The page has to be a nice clean _individual_ kernel allocation.
1469a145dd41SLinus Torvalds  * If you allocate a compound page, you need to have marked it as
1470a145dd41SLinus Torvalds  * such (__GFP_COMP), or manually just split the page up yourself
14718dfcc9baSNick Piggin  * (see split_page()).
1472a145dd41SLinus Torvalds  *
1473a145dd41SLinus Torvalds  * NOTE! Traditionally this was done with "remap_pfn_range()" which
1474a145dd41SLinus Torvalds  * took an arbitrary page protection parameter. This doesn't allow
1475a145dd41SLinus Torvalds  * that. Your vma protection will have to be set up correctly, which
1476a145dd41SLinus Torvalds  * means that if you want a shared writable mapping, you'd better
1477a145dd41SLinus Torvalds  * ask for a shared writable mapping!
1478a145dd41SLinus Torvalds  *
1479a145dd41SLinus Torvalds  * The page does not need to be reserved.
14804b6e1e37SKonstantin Khlebnikov  *
14814b6e1e37SKonstantin Khlebnikov  * Usually this function is called from f_op->mmap() handler
14824b6e1e37SKonstantin Khlebnikov  * under mm->mmap_sem write-lock, so it can change vma->vm_flags.
14834b6e1e37SKonstantin Khlebnikov  * Caller must set VM_MIXEDMAP on vma if it wants to call this
14844b6e1e37SKonstantin Khlebnikov  * function from other places, for example from page-fault handler.
1485a145dd41SLinus Torvalds  */
1486423bad60SNick Piggin int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
1487423bad60SNick Piggin 			struct page *page)
1488a145dd41SLinus Torvalds {
1489a145dd41SLinus Torvalds 	if (addr < vma->vm_start || addr >= vma->vm_end)
1490a145dd41SLinus Torvalds 		return -EFAULT;
1491a145dd41SLinus Torvalds 	if (!page_count(page))
1492a145dd41SLinus Torvalds 		return -EINVAL;
14934b6e1e37SKonstantin Khlebnikov 	if (!(vma->vm_flags & VM_MIXEDMAP)) {
14944b6e1e37SKonstantin Khlebnikov 		BUG_ON(down_read_trylock(&vma->vm_mm->mmap_sem));
14954b6e1e37SKonstantin Khlebnikov 		BUG_ON(vma->vm_flags & VM_PFNMAP);
14964b6e1e37SKonstantin Khlebnikov 		vma->vm_flags |= VM_MIXEDMAP;
14974b6e1e37SKonstantin Khlebnikov 	}
1498423bad60SNick Piggin 	return insert_page(vma, addr, page, vma->vm_page_prot);
1499a145dd41SLinus Torvalds }
1500e3c3374fSLinus Torvalds EXPORT_SYMBOL(vm_insert_page);
1501a145dd41SLinus Torvalds 
1502423bad60SNick Piggin static int insert_pfn(struct vm_area_struct *vma, unsigned long addr,
1503423bad60SNick Piggin 			unsigned long pfn, pgprot_t prot)
1504423bad60SNick Piggin {
1505423bad60SNick Piggin 	struct mm_struct *mm = vma->vm_mm;
1506423bad60SNick Piggin 	int retval;
1507423bad60SNick Piggin 	pte_t *pte, entry;
1508423bad60SNick Piggin 	spinlock_t *ptl;
1509423bad60SNick Piggin 
1510423bad60SNick Piggin 	retval = -ENOMEM;
1511423bad60SNick Piggin 	pte = get_locked_pte(mm, addr, &ptl);
1512423bad60SNick Piggin 	if (!pte)
1513423bad60SNick Piggin 		goto out;
1514423bad60SNick Piggin 	retval = -EBUSY;
1515423bad60SNick Piggin 	if (!pte_none(*pte))
1516423bad60SNick Piggin 		goto out_unlock;
1517423bad60SNick Piggin 
1518423bad60SNick Piggin 	/* Ok, finally just insert the thing.. */
1519423bad60SNick Piggin 	entry = pte_mkspecial(pfn_pte(pfn, prot));
1520423bad60SNick Piggin 	set_pte_at(mm, addr, pte, entry);
15214b3073e1SRussell King 	update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */
1522423bad60SNick Piggin 
1523423bad60SNick Piggin 	retval = 0;
1524423bad60SNick Piggin out_unlock:
1525423bad60SNick Piggin 	pte_unmap_unlock(pte, ptl);
1526423bad60SNick Piggin out:
1527423bad60SNick Piggin 	return retval;
1528423bad60SNick Piggin }
1529423bad60SNick Piggin 
1530e0dc0d8fSNick Piggin /**
1531e0dc0d8fSNick Piggin  * vm_insert_pfn - insert single pfn into user vma
1532e0dc0d8fSNick Piggin  * @vma: user vma to map to
1533e0dc0d8fSNick Piggin  * @addr: target user address of this page
1534e0dc0d8fSNick Piggin  * @pfn: source kernel pfn
1535e0dc0d8fSNick Piggin  *
1536c462f179SRobert P. J. Day  * Similar to vm_insert_page, this allows drivers to insert individual pages
1537e0dc0d8fSNick Piggin  * they've allocated into a user vma. Same comments apply.
1538e0dc0d8fSNick Piggin  *
1539e0dc0d8fSNick Piggin  * This function should only be called from a vm_ops->fault handler, and
1540e0dc0d8fSNick Piggin  * in that case the handler should return NULL.
15410d71d10aSNick Piggin  *
15420d71d10aSNick Piggin  * vma cannot be a COW mapping.
15430d71d10aSNick Piggin  *
15440d71d10aSNick Piggin  * As this is called only for pages that do not currently exist, we
15450d71d10aSNick Piggin  * do not need to flush old virtual caches or the TLB.
1546e0dc0d8fSNick Piggin  */
1547e0dc0d8fSNick Piggin int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
1548e0dc0d8fSNick Piggin 			unsigned long pfn)
1549e0dc0d8fSNick Piggin {
15502ab64037Svenkatesh.pallipadi@intel.com 	int ret;
1551e4b866edSvenkatesh.pallipadi@intel.com 	pgprot_t pgprot = vma->vm_page_prot;
15527e675137SNick Piggin 	/*
15537e675137SNick Piggin 	 * Technically, architectures with pte_special can avoid all these
15547e675137SNick Piggin 	 * restrictions (same for remap_pfn_range).  However we would like
15557e675137SNick Piggin 	 * consistency in testing and feature parity among all, so we should
15567e675137SNick Piggin 	 * try to keep these invariants in place for everybody.
15577e675137SNick Piggin 	 */
1558b379d790SJared Hulbert 	BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
1559b379d790SJared Hulbert 	BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
1560b379d790SJared Hulbert 						(VM_PFNMAP|VM_MIXEDMAP));
1561b379d790SJared Hulbert 	BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
1562b379d790SJared Hulbert 	BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
1563e0dc0d8fSNick Piggin 
1564423bad60SNick Piggin 	if (addr < vma->vm_start || addr >= vma->vm_end)
1565423bad60SNick Piggin 		return -EFAULT;
15665180da41SSuresh Siddha 	if (track_pfn_insert(vma, &pgprot, pfn))
15672ab64037Svenkatesh.pallipadi@intel.com 		return -EINVAL;
15682ab64037Svenkatesh.pallipadi@intel.com 
1569e4b866edSvenkatesh.pallipadi@intel.com 	ret = insert_pfn(vma, addr, pfn, pgprot);
15702ab64037Svenkatesh.pallipadi@intel.com 
15712ab64037Svenkatesh.pallipadi@intel.com 	return ret;
1572e0dc0d8fSNick Piggin }
1573e0dc0d8fSNick Piggin EXPORT_SYMBOL(vm_insert_pfn);
1574e0dc0d8fSNick Piggin 
1575423bad60SNick Piggin int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
1576423bad60SNick Piggin 			unsigned long pfn)
1577423bad60SNick Piggin {
1578423bad60SNick Piggin 	BUG_ON(!(vma->vm_flags & VM_MIXEDMAP));
1579423bad60SNick Piggin 
1580423bad60SNick Piggin 	if (addr < vma->vm_start || addr >= vma->vm_end)
1581423bad60SNick Piggin 		return -EFAULT;
1582423bad60SNick Piggin 
1583423bad60SNick Piggin 	/*
1584423bad60SNick Piggin 	 * If we don't have pte special, then we have to use the pfn_valid()
1585423bad60SNick Piggin 	 * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must*
1586423bad60SNick Piggin 	 * refcount the page if pfn_valid is true (hence insert_page rather
158762eede62SHugh Dickins 	 * than insert_pfn).  If a zero_pfn were inserted into a VM_MIXEDMAP
158862eede62SHugh Dickins 	 * without pte special, it would there be refcounted as a normal page.
1589423bad60SNick Piggin 	 */
1590423bad60SNick Piggin 	if (!HAVE_PTE_SPECIAL && pfn_valid(pfn)) {
1591423bad60SNick Piggin 		struct page *page;
1592423bad60SNick Piggin 
1593423bad60SNick Piggin 		page = pfn_to_page(pfn);
1594423bad60SNick Piggin 		return insert_page(vma, addr, page, vma->vm_page_prot);
1595423bad60SNick Piggin 	}
1596423bad60SNick Piggin 	return insert_pfn(vma, addr, pfn, vma->vm_page_prot);
1597423bad60SNick Piggin }
1598423bad60SNick Piggin EXPORT_SYMBOL(vm_insert_mixed);
1599423bad60SNick Piggin 
1600a145dd41SLinus Torvalds /*
16011da177e4SLinus Torvalds  * maps a range of physical memory into the requested pages. the old
16021da177e4SLinus Torvalds  * mappings are removed. any references to nonexistent pages results
16031da177e4SLinus Torvalds  * in null mappings (currently treated as "copy-on-access")
16041da177e4SLinus Torvalds  */
16051da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
16061da177e4SLinus Torvalds 			unsigned long addr, unsigned long end,
16071da177e4SLinus Torvalds 			unsigned long pfn, pgprot_t prot)
16081da177e4SLinus Torvalds {
16091da177e4SLinus Torvalds 	pte_t *pte;
1610c74df32cSHugh Dickins 	spinlock_t *ptl;
16111da177e4SLinus Torvalds 
1612c74df32cSHugh Dickins 	pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
16131da177e4SLinus Torvalds 	if (!pte)
16141da177e4SLinus Torvalds 		return -ENOMEM;
16156606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
16161da177e4SLinus Torvalds 	do {
16171da177e4SLinus Torvalds 		BUG_ON(!pte_none(*pte));
16187e675137SNick Piggin 		set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot)));
16191da177e4SLinus Torvalds 		pfn++;
16201da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
16216606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
1622c74df32cSHugh Dickins 	pte_unmap_unlock(pte - 1, ptl);
16231da177e4SLinus Torvalds 	return 0;
16241da177e4SLinus Torvalds }
16251da177e4SLinus Torvalds 
16261da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
16271da177e4SLinus Torvalds 			unsigned long addr, unsigned long end,
16281da177e4SLinus Torvalds 			unsigned long pfn, pgprot_t prot)
16291da177e4SLinus Torvalds {
16301da177e4SLinus Torvalds 	pmd_t *pmd;
16311da177e4SLinus Torvalds 	unsigned long next;
16321da177e4SLinus Torvalds 
16331da177e4SLinus Torvalds 	pfn -= addr >> PAGE_SHIFT;
16341da177e4SLinus Torvalds 	pmd = pmd_alloc(mm, pud, addr);
16351da177e4SLinus Torvalds 	if (!pmd)
16361da177e4SLinus Torvalds 		return -ENOMEM;
1637f66055abSAndrea Arcangeli 	VM_BUG_ON(pmd_trans_huge(*pmd));
16381da177e4SLinus Torvalds 	do {
16391da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
16401da177e4SLinus Torvalds 		if (remap_pte_range(mm, pmd, addr, next,
16411da177e4SLinus Torvalds 				pfn + (addr >> PAGE_SHIFT), prot))
16421da177e4SLinus Torvalds 			return -ENOMEM;
16431da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
16441da177e4SLinus Torvalds 	return 0;
16451da177e4SLinus Torvalds }
16461da177e4SLinus Torvalds 
16471da177e4SLinus Torvalds static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd,
16481da177e4SLinus Torvalds 			unsigned long addr, unsigned long end,
16491da177e4SLinus Torvalds 			unsigned long pfn, pgprot_t prot)
16501da177e4SLinus Torvalds {
16511da177e4SLinus Torvalds 	pud_t *pud;
16521da177e4SLinus Torvalds 	unsigned long next;
16531da177e4SLinus Torvalds 
16541da177e4SLinus Torvalds 	pfn -= addr >> PAGE_SHIFT;
16551da177e4SLinus Torvalds 	pud = pud_alloc(mm, pgd, addr);
16561da177e4SLinus Torvalds 	if (!pud)
16571da177e4SLinus Torvalds 		return -ENOMEM;
16581da177e4SLinus Torvalds 	do {
16591da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
16601da177e4SLinus Torvalds 		if (remap_pmd_range(mm, pud, addr, next,
16611da177e4SLinus Torvalds 				pfn + (addr >> PAGE_SHIFT), prot))
16621da177e4SLinus Torvalds 			return -ENOMEM;
16631da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
16641da177e4SLinus Torvalds 	return 0;
16651da177e4SLinus Torvalds }
16661da177e4SLinus Torvalds 
1667bfa5bf6dSRolf Eike Beer /**
1668bfa5bf6dSRolf Eike Beer  * remap_pfn_range - remap kernel memory to userspace
1669bfa5bf6dSRolf Eike Beer  * @vma: user vma to map to
1670bfa5bf6dSRolf Eike Beer  * @addr: target user address to start at
1671bfa5bf6dSRolf Eike Beer  * @pfn: physical address of kernel memory
1672bfa5bf6dSRolf Eike Beer  * @size: size of map area
1673bfa5bf6dSRolf Eike Beer  * @prot: page protection flags for this mapping
1674bfa5bf6dSRolf Eike Beer  *
1675bfa5bf6dSRolf Eike Beer  *  Note: this is only safe if the mm semaphore is held when called.
1676bfa5bf6dSRolf Eike Beer  */
16771da177e4SLinus Torvalds int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
16781da177e4SLinus Torvalds 		    unsigned long pfn, unsigned long size, pgprot_t prot)
16791da177e4SLinus Torvalds {
16801da177e4SLinus Torvalds 	pgd_t *pgd;
16811da177e4SLinus Torvalds 	unsigned long next;
16822d15cab8SHugh Dickins 	unsigned long end = addr + PAGE_ALIGN(size);
16831da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
16841da177e4SLinus Torvalds 	int err;
16851da177e4SLinus Torvalds 
16861da177e4SLinus Torvalds 	/*
16871da177e4SLinus Torvalds 	 * Physically remapped pages are special. Tell the
16881da177e4SLinus Torvalds 	 * rest of the world about it:
16891da177e4SLinus Torvalds 	 *   VM_IO tells people not to look at these pages
16901da177e4SLinus Torvalds 	 *	(accesses can have side effects).
16916aab341eSLinus Torvalds 	 *   VM_PFNMAP tells the core MM that the base pages are just
16926aab341eSLinus Torvalds 	 *	raw PFN mappings, and do not have a "struct page" associated
16936aab341eSLinus Torvalds 	 *	with them.
1694314e51b9SKonstantin Khlebnikov 	 *   VM_DONTEXPAND
1695314e51b9SKonstantin Khlebnikov 	 *      Disable vma merging and expanding with mremap().
1696314e51b9SKonstantin Khlebnikov 	 *   VM_DONTDUMP
1697314e51b9SKonstantin Khlebnikov 	 *      Omit vma from core dump, even when VM_IO turned off.
1698fb155c16SLinus Torvalds 	 *
1699fb155c16SLinus Torvalds 	 * There's a horrible special case to handle copy-on-write
1700fb155c16SLinus Torvalds 	 * behaviour that some programs depend on. We mark the "original"
1701fb155c16SLinus Torvalds 	 * un-COW'ed pages by matching them up with "vma->vm_pgoff".
1702b3b9c293SKonstantin Khlebnikov 	 * See vm_normal_page() for details.
17031da177e4SLinus Torvalds 	 */
1704b3b9c293SKonstantin Khlebnikov 	if (is_cow_mapping(vma->vm_flags)) {
1705b3b9c293SKonstantin Khlebnikov 		if (addr != vma->vm_start || end != vma->vm_end)
1706b3b9c293SKonstantin Khlebnikov 			return -EINVAL;
17076aab341eSLinus Torvalds 		vma->vm_pgoff = pfn;
1708b3b9c293SKonstantin Khlebnikov 	}
1709b3b9c293SKonstantin Khlebnikov 
1710b3b9c293SKonstantin Khlebnikov 	err = track_pfn_remap(vma, &prot, pfn, addr, PAGE_ALIGN(size));
1711b3b9c293SKonstantin Khlebnikov 	if (err)
17123c8bb73aSvenkatesh.pallipadi@intel.com 		return -EINVAL;
1713fb155c16SLinus Torvalds 
1714314e51b9SKonstantin Khlebnikov 	vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
17151da177e4SLinus Torvalds 
17161da177e4SLinus Torvalds 	BUG_ON(addr >= end);
17171da177e4SLinus Torvalds 	pfn -= addr >> PAGE_SHIFT;
17181da177e4SLinus Torvalds 	pgd = pgd_offset(mm, addr);
17191da177e4SLinus Torvalds 	flush_cache_range(vma, addr, end);
17201da177e4SLinus Torvalds 	do {
17211da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
17221da177e4SLinus Torvalds 		err = remap_pud_range(mm, pgd, addr, next,
17231da177e4SLinus Torvalds 				pfn + (addr >> PAGE_SHIFT), prot);
17241da177e4SLinus Torvalds 		if (err)
17251da177e4SLinus Torvalds 			break;
17261da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
17272ab64037Svenkatesh.pallipadi@intel.com 
17282ab64037Svenkatesh.pallipadi@intel.com 	if (err)
17295180da41SSuresh Siddha 		untrack_pfn(vma, pfn, PAGE_ALIGN(size));
17302ab64037Svenkatesh.pallipadi@intel.com 
17311da177e4SLinus Torvalds 	return err;
17321da177e4SLinus Torvalds }
17331da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range);
17341da177e4SLinus Torvalds 
1735b4cbb197SLinus Torvalds /**
1736b4cbb197SLinus Torvalds  * vm_iomap_memory - remap memory to userspace
1737b4cbb197SLinus Torvalds  * @vma: user vma to map to
1738b4cbb197SLinus Torvalds  * @start: start of area
1739b4cbb197SLinus Torvalds  * @len: size of area
1740b4cbb197SLinus Torvalds  *
1741b4cbb197SLinus Torvalds  * This is a simplified io_remap_pfn_range() for common driver use. The
1742b4cbb197SLinus Torvalds  * driver just needs to give us the physical memory range to be mapped,
1743b4cbb197SLinus Torvalds  * we'll figure out the rest from the vma information.
1744b4cbb197SLinus Torvalds  *
1745b4cbb197SLinus Torvalds  * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get
1746b4cbb197SLinus Torvalds  * whatever write-combining details or similar.
1747b4cbb197SLinus Torvalds  */
1748b4cbb197SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
1749b4cbb197SLinus Torvalds {
1750b4cbb197SLinus Torvalds 	unsigned long vm_len, pfn, pages;
1751b4cbb197SLinus Torvalds 
1752b4cbb197SLinus Torvalds 	/* Check that the physical memory area passed in looks valid */
1753b4cbb197SLinus Torvalds 	if (start + len < start)
1754b4cbb197SLinus Torvalds 		return -EINVAL;
1755b4cbb197SLinus Torvalds 	/*
1756b4cbb197SLinus Torvalds 	 * You *really* shouldn't map things that aren't page-aligned,
1757b4cbb197SLinus Torvalds 	 * but we've historically allowed it because IO memory might
1758b4cbb197SLinus Torvalds 	 * just have smaller alignment.
1759b4cbb197SLinus Torvalds 	 */
1760b4cbb197SLinus Torvalds 	len += start & ~PAGE_MASK;
1761b4cbb197SLinus Torvalds 	pfn = start >> PAGE_SHIFT;
1762b4cbb197SLinus Torvalds 	pages = (len + ~PAGE_MASK) >> PAGE_SHIFT;
1763b4cbb197SLinus Torvalds 	if (pfn + pages < pfn)
1764b4cbb197SLinus Torvalds 		return -EINVAL;
1765b4cbb197SLinus Torvalds 
1766b4cbb197SLinus Torvalds 	/* We start the mapping 'vm_pgoff' pages into the area */
1767b4cbb197SLinus Torvalds 	if (vma->vm_pgoff > pages)
1768b4cbb197SLinus Torvalds 		return -EINVAL;
1769b4cbb197SLinus Torvalds 	pfn += vma->vm_pgoff;
1770b4cbb197SLinus Torvalds 	pages -= vma->vm_pgoff;
1771b4cbb197SLinus Torvalds 
1772b4cbb197SLinus Torvalds 	/* Can we fit all of the mapping? */
1773b4cbb197SLinus Torvalds 	vm_len = vma->vm_end - vma->vm_start;
1774b4cbb197SLinus Torvalds 	if (vm_len >> PAGE_SHIFT > pages)
1775b4cbb197SLinus Torvalds 		return -EINVAL;
1776b4cbb197SLinus Torvalds 
1777b4cbb197SLinus Torvalds 	/* Ok, let it rip */
1778b4cbb197SLinus Torvalds 	return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
1779b4cbb197SLinus Torvalds }
1780b4cbb197SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory);
1781b4cbb197SLinus Torvalds 
1782aee16b3cSJeremy Fitzhardinge static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
1783aee16b3cSJeremy Fitzhardinge 				     unsigned long addr, unsigned long end,
1784aee16b3cSJeremy Fitzhardinge 				     pte_fn_t fn, void *data)
1785aee16b3cSJeremy Fitzhardinge {
1786aee16b3cSJeremy Fitzhardinge 	pte_t *pte;
1787aee16b3cSJeremy Fitzhardinge 	int err;
17882f569afdSMartin Schwidefsky 	pgtable_t token;
178994909914SBorislav Petkov 	spinlock_t *uninitialized_var(ptl);
1790aee16b3cSJeremy Fitzhardinge 
1791aee16b3cSJeremy Fitzhardinge 	pte = (mm == &init_mm) ?
1792aee16b3cSJeremy Fitzhardinge 		pte_alloc_kernel(pmd, addr) :
1793aee16b3cSJeremy Fitzhardinge 		pte_alloc_map_lock(mm, pmd, addr, &ptl);
1794aee16b3cSJeremy Fitzhardinge 	if (!pte)
1795aee16b3cSJeremy Fitzhardinge 		return -ENOMEM;
1796aee16b3cSJeremy Fitzhardinge 
1797aee16b3cSJeremy Fitzhardinge 	BUG_ON(pmd_huge(*pmd));
1798aee16b3cSJeremy Fitzhardinge 
179938e0edb1SJeremy Fitzhardinge 	arch_enter_lazy_mmu_mode();
180038e0edb1SJeremy Fitzhardinge 
18012f569afdSMartin Schwidefsky 	token = pmd_pgtable(*pmd);
1802aee16b3cSJeremy Fitzhardinge 
1803aee16b3cSJeremy Fitzhardinge 	do {
1804c36987e2SDaisuke Nishimura 		err = fn(pte++, token, addr, data);
1805aee16b3cSJeremy Fitzhardinge 		if (err)
1806aee16b3cSJeremy Fitzhardinge 			break;
1807c36987e2SDaisuke Nishimura 	} while (addr += PAGE_SIZE, addr != end);
1808aee16b3cSJeremy Fitzhardinge 
180938e0edb1SJeremy Fitzhardinge 	arch_leave_lazy_mmu_mode();
181038e0edb1SJeremy Fitzhardinge 
1811aee16b3cSJeremy Fitzhardinge 	if (mm != &init_mm)
1812aee16b3cSJeremy Fitzhardinge 		pte_unmap_unlock(pte-1, ptl);
1813aee16b3cSJeremy Fitzhardinge 	return err;
1814aee16b3cSJeremy Fitzhardinge }
1815aee16b3cSJeremy Fitzhardinge 
1816aee16b3cSJeremy Fitzhardinge static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
1817aee16b3cSJeremy Fitzhardinge 				     unsigned long addr, unsigned long end,
1818aee16b3cSJeremy Fitzhardinge 				     pte_fn_t fn, void *data)
1819aee16b3cSJeremy Fitzhardinge {
1820aee16b3cSJeremy Fitzhardinge 	pmd_t *pmd;
1821aee16b3cSJeremy Fitzhardinge 	unsigned long next;
1822aee16b3cSJeremy Fitzhardinge 	int err;
1823aee16b3cSJeremy Fitzhardinge 
1824ceb86879SAndi Kleen 	BUG_ON(pud_huge(*pud));
1825ceb86879SAndi Kleen 
1826aee16b3cSJeremy Fitzhardinge 	pmd = pmd_alloc(mm, pud, addr);
1827aee16b3cSJeremy Fitzhardinge 	if (!pmd)
1828aee16b3cSJeremy Fitzhardinge 		return -ENOMEM;
1829aee16b3cSJeremy Fitzhardinge 	do {
1830aee16b3cSJeremy Fitzhardinge 		next = pmd_addr_end(addr, end);
1831aee16b3cSJeremy Fitzhardinge 		err = apply_to_pte_range(mm, pmd, addr, next, fn, data);
1832aee16b3cSJeremy Fitzhardinge 		if (err)
1833aee16b3cSJeremy Fitzhardinge 			break;
1834aee16b3cSJeremy Fitzhardinge 	} while (pmd++, addr = next, addr != end);
1835aee16b3cSJeremy Fitzhardinge 	return err;
1836aee16b3cSJeremy Fitzhardinge }
1837aee16b3cSJeremy Fitzhardinge 
1838aee16b3cSJeremy Fitzhardinge static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd,
1839aee16b3cSJeremy Fitzhardinge 				     unsigned long addr, unsigned long end,
1840aee16b3cSJeremy Fitzhardinge 				     pte_fn_t fn, void *data)
1841aee16b3cSJeremy Fitzhardinge {
1842aee16b3cSJeremy Fitzhardinge 	pud_t *pud;
1843aee16b3cSJeremy Fitzhardinge 	unsigned long next;
1844aee16b3cSJeremy Fitzhardinge 	int err;
1845aee16b3cSJeremy Fitzhardinge 
1846aee16b3cSJeremy Fitzhardinge 	pud = pud_alloc(mm, pgd, addr);
1847aee16b3cSJeremy Fitzhardinge 	if (!pud)
1848aee16b3cSJeremy Fitzhardinge 		return -ENOMEM;
1849aee16b3cSJeremy Fitzhardinge 	do {
1850aee16b3cSJeremy Fitzhardinge 		next = pud_addr_end(addr, end);
1851aee16b3cSJeremy Fitzhardinge 		err = apply_to_pmd_range(mm, pud, addr, next, fn, data);
1852aee16b3cSJeremy Fitzhardinge 		if (err)
1853aee16b3cSJeremy Fitzhardinge 			break;
1854aee16b3cSJeremy Fitzhardinge 	} while (pud++, addr = next, addr != end);
1855aee16b3cSJeremy Fitzhardinge 	return err;
1856aee16b3cSJeremy Fitzhardinge }
1857aee16b3cSJeremy Fitzhardinge 
1858aee16b3cSJeremy Fitzhardinge /*
1859aee16b3cSJeremy Fitzhardinge  * Scan a region of virtual memory, filling in page tables as necessary
1860aee16b3cSJeremy Fitzhardinge  * and calling a provided function on each leaf page table.
1861aee16b3cSJeremy Fitzhardinge  */
1862aee16b3cSJeremy Fitzhardinge int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
1863aee16b3cSJeremy Fitzhardinge 			unsigned long size, pte_fn_t fn, void *data)
1864aee16b3cSJeremy Fitzhardinge {
1865aee16b3cSJeremy Fitzhardinge 	pgd_t *pgd;
1866aee16b3cSJeremy Fitzhardinge 	unsigned long next;
186757250a5bSJeremy Fitzhardinge 	unsigned long end = addr + size;
1868aee16b3cSJeremy Fitzhardinge 	int err;
1869aee16b3cSJeremy Fitzhardinge 
1870aee16b3cSJeremy Fitzhardinge 	BUG_ON(addr >= end);
1871aee16b3cSJeremy Fitzhardinge 	pgd = pgd_offset(mm, addr);
1872aee16b3cSJeremy Fitzhardinge 	do {
1873aee16b3cSJeremy Fitzhardinge 		next = pgd_addr_end(addr, end);
1874aee16b3cSJeremy Fitzhardinge 		err = apply_to_pud_range(mm, pgd, addr, next, fn, data);
1875aee16b3cSJeremy Fitzhardinge 		if (err)
1876aee16b3cSJeremy Fitzhardinge 			break;
1877aee16b3cSJeremy Fitzhardinge 	} while (pgd++, addr = next, addr != end);
187857250a5bSJeremy Fitzhardinge 
1879aee16b3cSJeremy Fitzhardinge 	return err;
1880aee16b3cSJeremy Fitzhardinge }
1881aee16b3cSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(apply_to_page_range);
1882aee16b3cSJeremy Fitzhardinge 
18831da177e4SLinus Torvalds /*
18849b4bdd2fSKirill A. Shutemov  * handle_pte_fault chooses page fault handler according to an entry which was
18859b4bdd2fSKirill A. Shutemov  * read non-atomically.  Before making any commitment, on those architectures
18869b4bdd2fSKirill A. Shutemov  * or configurations (e.g. i386 with PAE) which might give a mix of unmatched
18879b4bdd2fSKirill A. Shutemov  * parts, do_swap_page must check under lock before unmapping the pte and
18889b4bdd2fSKirill A. Shutemov  * proceeding (but do_wp_page is only called after already making such a check;
1889a335b2e1SRyota Ozaki  * and do_anonymous_page can safely check later on).
18908f4e2101SHugh Dickins  */
18914c21e2f2SHugh Dickins static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
18928f4e2101SHugh Dickins 				pte_t *page_table, pte_t orig_pte)
18938f4e2101SHugh Dickins {
18948f4e2101SHugh Dickins 	int same = 1;
18958f4e2101SHugh Dickins #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
18968f4e2101SHugh Dickins 	if (sizeof(pte_t) > sizeof(unsigned long)) {
18974c21e2f2SHugh Dickins 		spinlock_t *ptl = pte_lockptr(mm, pmd);
18984c21e2f2SHugh Dickins 		spin_lock(ptl);
18998f4e2101SHugh Dickins 		same = pte_same(*page_table, orig_pte);
19004c21e2f2SHugh Dickins 		spin_unlock(ptl);
19018f4e2101SHugh Dickins 	}
19028f4e2101SHugh Dickins #endif
19038f4e2101SHugh Dickins 	pte_unmap(page_table);
19048f4e2101SHugh Dickins 	return same;
19058f4e2101SHugh Dickins }
19068f4e2101SHugh Dickins 
19079de455b2SAtsushi Nemoto static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma)
19086aab341eSLinus Torvalds {
19090abdd7a8SDan Williams 	debug_dma_assert_idle(src);
19100abdd7a8SDan Williams 
19116aab341eSLinus Torvalds 	/*
19126aab341eSLinus Torvalds 	 * If the source page was a PFN mapping, we don't have
19136aab341eSLinus Torvalds 	 * a "struct page" for it. We do a best-effort copy by
19146aab341eSLinus Torvalds 	 * just copying from the original user address. If that
19156aab341eSLinus Torvalds 	 * fails, we just zero-fill it. Live with it.
19166aab341eSLinus Torvalds 	 */
19176aab341eSLinus Torvalds 	if (unlikely(!src)) {
19189b04c5feSCong Wang 		void *kaddr = kmap_atomic(dst);
19195d2a2dbbSLinus Torvalds 		void __user *uaddr = (void __user *)(va & PAGE_MASK);
19205d2a2dbbSLinus Torvalds 
19215d2a2dbbSLinus Torvalds 		/*
19225d2a2dbbSLinus Torvalds 		 * This really shouldn't fail, because the page is there
19235d2a2dbbSLinus Torvalds 		 * in the page tables. But it might just be unreadable,
19245d2a2dbbSLinus Torvalds 		 * in which case we just give up and fill the result with
19255d2a2dbbSLinus Torvalds 		 * zeroes.
19265d2a2dbbSLinus Torvalds 		 */
19275d2a2dbbSLinus Torvalds 		if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE))
19283ecb01dfSJan Beulich 			clear_page(kaddr);
19299b04c5feSCong Wang 		kunmap_atomic(kaddr);
1930c4ec7b0dSDmitriy Monakhov 		flush_dcache_page(dst);
19310ed361deSNick Piggin 	} else
19329de455b2SAtsushi Nemoto 		copy_user_highpage(dst, src, va, vma);
19336aab341eSLinus Torvalds }
19346aab341eSLinus Torvalds 
1935c20cd45eSMichal Hocko static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma)
1936c20cd45eSMichal Hocko {
1937c20cd45eSMichal Hocko 	struct file *vm_file = vma->vm_file;
1938c20cd45eSMichal Hocko 
1939c20cd45eSMichal Hocko 	if (vm_file)
1940c20cd45eSMichal Hocko 		return mapping_gfp_mask(vm_file->f_mapping) | __GFP_FS | __GFP_IO;
1941c20cd45eSMichal Hocko 
1942c20cd45eSMichal Hocko 	/*
1943c20cd45eSMichal Hocko 	 * Special mappings (e.g. VDSO) do not have any file so fake
1944c20cd45eSMichal Hocko 	 * a default GFP_KERNEL for them.
1945c20cd45eSMichal Hocko 	 */
1946c20cd45eSMichal Hocko 	return GFP_KERNEL;
1947c20cd45eSMichal Hocko }
1948c20cd45eSMichal Hocko 
19491da177e4SLinus Torvalds /*
1950fb09a464SKirill A. Shutemov  * Notify the address space that the page is about to become writable so that
1951fb09a464SKirill A. Shutemov  * it can prohibit this or wait for the page to get into an appropriate state.
1952fb09a464SKirill A. Shutemov  *
1953fb09a464SKirill A. Shutemov  * We do this without the lock held, so that it can sleep if it needs to.
1954fb09a464SKirill A. Shutemov  */
1955fb09a464SKirill A. Shutemov static int do_page_mkwrite(struct vm_area_struct *vma, struct page *page,
1956fb09a464SKirill A. Shutemov 	       unsigned long address)
1957fb09a464SKirill A. Shutemov {
1958fb09a464SKirill A. Shutemov 	struct vm_fault vmf;
1959fb09a464SKirill A. Shutemov 	int ret;
1960fb09a464SKirill A. Shutemov 
1961fb09a464SKirill A. Shutemov 	vmf.virtual_address = (void __user *)(address & PAGE_MASK);
1962fb09a464SKirill A. Shutemov 	vmf.pgoff = page->index;
1963fb09a464SKirill A. Shutemov 	vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
1964c20cd45eSMichal Hocko 	vmf.gfp_mask = __get_fault_gfp_mask(vma);
1965fb09a464SKirill A. Shutemov 	vmf.page = page;
19662e4cdab0SMatthew Wilcox 	vmf.cow_page = NULL;
1967fb09a464SKirill A. Shutemov 
1968fb09a464SKirill A. Shutemov 	ret = vma->vm_ops->page_mkwrite(vma, &vmf);
1969fb09a464SKirill A. Shutemov 	if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))
1970fb09a464SKirill A. Shutemov 		return ret;
1971fb09a464SKirill A. Shutemov 	if (unlikely(!(ret & VM_FAULT_LOCKED))) {
1972fb09a464SKirill A. Shutemov 		lock_page(page);
1973fb09a464SKirill A. Shutemov 		if (!page->mapping) {
1974fb09a464SKirill A. Shutemov 			unlock_page(page);
1975fb09a464SKirill A. Shutemov 			return 0; /* retry */
1976fb09a464SKirill A. Shutemov 		}
1977fb09a464SKirill A. Shutemov 		ret |= VM_FAULT_LOCKED;
1978fb09a464SKirill A. Shutemov 	} else
1979fb09a464SKirill A. Shutemov 		VM_BUG_ON_PAGE(!PageLocked(page), page);
1980fb09a464SKirill A. Shutemov 	return ret;
1981fb09a464SKirill A. Shutemov }
1982fb09a464SKirill A. Shutemov 
1983fb09a464SKirill A. Shutemov /*
19844e047f89SShachar Raindel  * Handle write page faults for pages that can be reused in the current vma
19854e047f89SShachar Raindel  *
19864e047f89SShachar Raindel  * This can happen either due to the mapping being with the VM_SHARED flag,
19874e047f89SShachar Raindel  * or due to us being the last reference standing to the page. In either
19884e047f89SShachar Raindel  * case, all we need to do here is to mark the page as writable and update
19894e047f89SShachar Raindel  * any related book-keeping.
19904e047f89SShachar Raindel  */
19914e047f89SShachar Raindel static inline int wp_page_reuse(struct mm_struct *mm,
19924e047f89SShachar Raindel 			struct vm_area_struct *vma, unsigned long address,
19934e047f89SShachar Raindel 			pte_t *page_table, spinlock_t *ptl, pte_t orig_pte,
19944e047f89SShachar Raindel 			struct page *page, int page_mkwrite,
19954e047f89SShachar Raindel 			int dirty_shared)
19964e047f89SShachar Raindel 	__releases(ptl)
19974e047f89SShachar Raindel {
19984e047f89SShachar Raindel 	pte_t entry;
19994e047f89SShachar Raindel 	/*
20004e047f89SShachar Raindel 	 * Clear the pages cpupid information as the existing
20014e047f89SShachar Raindel 	 * information potentially belongs to a now completely
20024e047f89SShachar Raindel 	 * unrelated process.
20034e047f89SShachar Raindel 	 */
20044e047f89SShachar Raindel 	if (page)
20054e047f89SShachar Raindel 		page_cpupid_xchg_last(page, (1 << LAST_CPUPID_SHIFT) - 1);
20064e047f89SShachar Raindel 
20074e047f89SShachar Raindel 	flush_cache_page(vma, address, pte_pfn(orig_pte));
20084e047f89SShachar Raindel 	entry = pte_mkyoung(orig_pte);
20094e047f89SShachar Raindel 	entry = maybe_mkwrite(pte_mkdirty(entry), vma);
20104e047f89SShachar Raindel 	if (ptep_set_access_flags(vma, address, page_table, entry, 1))
20114e047f89SShachar Raindel 		update_mmu_cache(vma, address, page_table);
20124e047f89SShachar Raindel 	pte_unmap_unlock(page_table, ptl);
20134e047f89SShachar Raindel 
20144e047f89SShachar Raindel 	if (dirty_shared) {
20154e047f89SShachar Raindel 		struct address_space *mapping;
20164e047f89SShachar Raindel 		int dirtied;
20174e047f89SShachar Raindel 
20184e047f89SShachar Raindel 		if (!page_mkwrite)
20194e047f89SShachar Raindel 			lock_page(page);
20204e047f89SShachar Raindel 
20214e047f89SShachar Raindel 		dirtied = set_page_dirty(page);
20224e047f89SShachar Raindel 		VM_BUG_ON_PAGE(PageAnon(page), page);
20234e047f89SShachar Raindel 		mapping = page->mapping;
20244e047f89SShachar Raindel 		unlock_page(page);
20254e047f89SShachar Raindel 		page_cache_release(page);
20264e047f89SShachar Raindel 
20274e047f89SShachar Raindel 		if ((dirtied || page_mkwrite) && mapping) {
20284e047f89SShachar Raindel 			/*
20294e047f89SShachar Raindel 			 * Some device drivers do not set page.mapping
20304e047f89SShachar Raindel 			 * but still dirty their pages
20314e047f89SShachar Raindel 			 */
20324e047f89SShachar Raindel 			balance_dirty_pages_ratelimited(mapping);
20334e047f89SShachar Raindel 		}
20344e047f89SShachar Raindel 
20354e047f89SShachar Raindel 		if (!page_mkwrite)
20364e047f89SShachar Raindel 			file_update_time(vma->vm_file);
20374e047f89SShachar Raindel 	}
20384e047f89SShachar Raindel 
20394e047f89SShachar Raindel 	return VM_FAULT_WRITE;
20404e047f89SShachar Raindel }
20414e047f89SShachar Raindel 
20424e047f89SShachar Raindel /*
20432f38ab2cSShachar Raindel  * Handle the case of a page which we actually need to copy to a new page.
20442f38ab2cSShachar Raindel  *
20452f38ab2cSShachar Raindel  * Called with mmap_sem locked and the old page referenced, but
20462f38ab2cSShachar Raindel  * without the ptl held.
20472f38ab2cSShachar Raindel  *
20482f38ab2cSShachar Raindel  * High level logic flow:
20492f38ab2cSShachar Raindel  *
20502f38ab2cSShachar Raindel  * - Allocate a page, copy the content of the old page to the new one.
20512f38ab2cSShachar Raindel  * - Handle book keeping and accounting - cgroups, mmu-notifiers, etc.
20522f38ab2cSShachar Raindel  * - Take the PTL. If the pte changed, bail out and release the allocated page
20532f38ab2cSShachar Raindel  * - If the pte is still the way we remember it, update the page table and all
20542f38ab2cSShachar Raindel  *   relevant references. This includes dropping the reference the page-table
20552f38ab2cSShachar Raindel  *   held to the old page, as well as updating the rmap.
20562f38ab2cSShachar Raindel  * - In any case, unlock the PTL and drop the reference we took to the old page.
20572f38ab2cSShachar Raindel  */
20582f38ab2cSShachar Raindel static int wp_page_copy(struct mm_struct *mm, struct vm_area_struct *vma,
20592f38ab2cSShachar Raindel 			unsigned long address, pte_t *page_table, pmd_t *pmd,
20602f38ab2cSShachar Raindel 			pte_t orig_pte, struct page *old_page)
20612f38ab2cSShachar Raindel {
20622f38ab2cSShachar Raindel 	struct page *new_page = NULL;
20632f38ab2cSShachar Raindel 	spinlock_t *ptl = NULL;
20642f38ab2cSShachar Raindel 	pte_t entry;
20652f38ab2cSShachar Raindel 	int page_copied = 0;
20662f38ab2cSShachar Raindel 	const unsigned long mmun_start = address & PAGE_MASK;	/* For mmu_notifiers */
20672f38ab2cSShachar Raindel 	const unsigned long mmun_end = mmun_start + PAGE_SIZE;	/* For mmu_notifiers */
20682f38ab2cSShachar Raindel 	struct mem_cgroup *memcg;
20692f38ab2cSShachar Raindel 
20702f38ab2cSShachar Raindel 	if (unlikely(anon_vma_prepare(vma)))
20712f38ab2cSShachar Raindel 		goto oom;
20722f38ab2cSShachar Raindel 
20732f38ab2cSShachar Raindel 	if (is_zero_pfn(pte_pfn(orig_pte))) {
20742f38ab2cSShachar Raindel 		new_page = alloc_zeroed_user_highpage_movable(vma, address);
20752f38ab2cSShachar Raindel 		if (!new_page)
20762f38ab2cSShachar Raindel 			goto oom;
20772f38ab2cSShachar Raindel 	} else {
20782f38ab2cSShachar Raindel 		new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
20792f38ab2cSShachar Raindel 		if (!new_page)
20802f38ab2cSShachar Raindel 			goto oom;
20812f38ab2cSShachar Raindel 		cow_user_page(new_page, old_page, address, vma);
20822f38ab2cSShachar Raindel 	}
20832f38ab2cSShachar Raindel 
2084f627c2f5SKirill A. Shutemov 	if (mem_cgroup_try_charge(new_page, mm, GFP_KERNEL, &memcg, false))
20852f38ab2cSShachar Raindel 		goto oom_free_new;
20862f38ab2cSShachar Raindel 
2087eb3c24f3SMel Gorman 	__SetPageUptodate(new_page);
2088eb3c24f3SMel Gorman 
20892f38ab2cSShachar Raindel 	mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
20902f38ab2cSShachar Raindel 
20912f38ab2cSShachar Raindel 	/*
20922f38ab2cSShachar Raindel 	 * Re-check the pte - we dropped the lock
20932f38ab2cSShachar Raindel 	 */
20942f38ab2cSShachar Raindel 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
20952f38ab2cSShachar Raindel 	if (likely(pte_same(*page_table, orig_pte))) {
20962f38ab2cSShachar Raindel 		if (old_page) {
20972f38ab2cSShachar Raindel 			if (!PageAnon(old_page)) {
2098eca56ff9SJerome Marchand 				dec_mm_counter_fast(mm,
2099eca56ff9SJerome Marchand 						mm_counter_file(old_page));
21002f38ab2cSShachar Raindel 				inc_mm_counter_fast(mm, MM_ANONPAGES);
21012f38ab2cSShachar Raindel 			}
21022f38ab2cSShachar Raindel 		} else {
21032f38ab2cSShachar Raindel 			inc_mm_counter_fast(mm, MM_ANONPAGES);
21042f38ab2cSShachar Raindel 		}
21052f38ab2cSShachar Raindel 		flush_cache_page(vma, address, pte_pfn(orig_pte));
21062f38ab2cSShachar Raindel 		entry = mk_pte(new_page, vma->vm_page_prot);
21072f38ab2cSShachar Raindel 		entry = maybe_mkwrite(pte_mkdirty(entry), vma);
21082f38ab2cSShachar Raindel 		/*
21092f38ab2cSShachar Raindel 		 * Clear the pte entry and flush it first, before updating the
21102f38ab2cSShachar Raindel 		 * pte with the new entry. This will avoid a race condition
21112f38ab2cSShachar Raindel 		 * seen in the presence of one thread doing SMC and another
21122f38ab2cSShachar Raindel 		 * thread doing COW.
21132f38ab2cSShachar Raindel 		 */
21142f38ab2cSShachar Raindel 		ptep_clear_flush_notify(vma, address, page_table);
2115d281ee61SKirill A. Shutemov 		page_add_new_anon_rmap(new_page, vma, address, false);
2116f627c2f5SKirill A. Shutemov 		mem_cgroup_commit_charge(new_page, memcg, false, false);
21172f38ab2cSShachar Raindel 		lru_cache_add_active_or_unevictable(new_page, vma);
21182f38ab2cSShachar Raindel 		/*
21192f38ab2cSShachar Raindel 		 * We call the notify macro here because, when using secondary
21202f38ab2cSShachar Raindel 		 * mmu page tables (such as kvm shadow page tables), we want the
21212f38ab2cSShachar Raindel 		 * new page to be mapped directly into the secondary page table.
21222f38ab2cSShachar Raindel 		 */
21232f38ab2cSShachar Raindel 		set_pte_at_notify(mm, address, page_table, entry);
21242f38ab2cSShachar Raindel 		update_mmu_cache(vma, address, page_table);
21252f38ab2cSShachar Raindel 		if (old_page) {
21262f38ab2cSShachar Raindel 			/*
21272f38ab2cSShachar Raindel 			 * Only after switching the pte to the new page may
21282f38ab2cSShachar Raindel 			 * we remove the mapcount here. Otherwise another
21292f38ab2cSShachar Raindel 			 * process may come and find the rmap count decremented
21302f38ab2cSShachar Raindel 			 * before the pte is switched to the new page, and
21312f38ab2cSShachar Raindel 			 * "reuse" the old page writing into it while our pte
21322f38ab2cSShachar Raindel 			 * here still points into it and can be read by other
21332f38ab2cSShachar Raindel 			 * threads.
21342f38ab2cSShachar Raindel 			 *
21352f38ab2cSShachar Raindel 			 * The critical issue is to order this
21362f38ab2cSShachar Raindel 			 * page_remove_rmap with the ptp_clear_flush above.
21372f38ab2cSShachar Raindel 			 * Those stores are ordered by (if nothing else,)
21382f38ab2cSShachar Raindel 			 * the barrier present in the atomic_add_negative
21392f38ab2cSShachar Raindel 			 * in page_remove_rmap.
21402f38ab2cSShachar Raindel 			 *
21412f38ab2cSShachar Raindel 			 * Then the TLB flush in ptep_clear_flush ensures that
21422f38ab2cSShachar Raindel 			 * no process can access the old page before the
21432f38ab2cSShachar Raindel 			 * decremented mapcount is visible. And the old page
21442f38ab2cSShachar Raindel 			 * cannot be reused until after the decremented
21452f38ab2cSShachar Raindel 			 * mapcount is visible. So transitively, TLBs to
21462f38ab2cSShachar Raindel 			 * old page will be flushed before it can be reused.
21472f38ab2cSShachar Raindel 			 */
2148d281ee61SKirill A. Shutemov 			page_remove_rmap(old_page, false);
21492f38ab2cSShachar Raindel 		}
21502f38ab2cSShachar Raindel 
21512f38ab2cSShachar Raindel 		/* Free the old page.. */
21522f38ab2cSShachar Raindel 		new_page = old_page;
21532f38ab2cSShachar Raindel 		page_copied = 1;
21542f38ab2cSShachar Raindel 	} else {
2155f627c2f5SKirill A. Shutemov 		mem_cgroup_cancel_charge(new_page, memcg, false);
21562f38ab2cSShachar Raindel 	}
21572f38ab2cSShachar Raindel 
21582f38ab2cSShachar Raindel 	if (new_page)
21592f38ab2cSShachar Raindel 		page_cache_release(new_page);
21602f38ab2cSShachar Raindel 
21612f38ab2cSShachar Raindel 	pte_unmap_unlock(page_table, ptl);
21622f38ab2cSShachar Raindel 	mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
21637479df6dSKirill A. Shutemov 	/* THP pages are never mlocked */
21647479df6dSKirill A. Shutemov 	if (old_page && !PageTransCompound(old_page)) {
21652f38ab2cSShachar Raindel 		/*
21662f38ab2cSShachar Raindel 		 * Don't let another task, with possibly unlocked vma,
21672f38ab2cSShachar Raindel 		 * keep the mlocked page.
21682f38ab2cSShachar Raindel 		 */
21692f38ab2cSShachar Raindel 		if (page_copied && (vma->vm_flags & VM_LOCKED)) {
21702f38ab2cSShachar Raindel 			lock_page(old_page);	/* LRU manipulation */
21712f38ab2cSShachar Raindel 			munlock_vma_page(old_page);
21722f38ab2cSShachar Raindel 			unlock_page(old_page);
21732f38ab2cSShachar Raindel 		}
21742f38ab2cSShachar Raindel 		page_cache_release(old_page);
21752f38ab2cSShachar Raindel 	}
21762f38ab2cSShachar Raindel 	return page_copied ? VM_FAULT_WRITE : 0;
21772f38ab2cSShachar Raindel oom_free_new:
21782f38ab2cSShachar Raindel 	page_cache_release(new_page);
21792f38ab2cSShachar Raindel oom:
21802f38ab2cSShachar Raindel 	if (old_page)
21812f38ab2cSShachar Raindel 		page_cache_release(old_page);
21822f38ab2cSShachar Raindel 	return VM_FAULT_OOM;
21832f38ab2cSShachar Raindel }
21842f38ab2cSShachar Raindel 
2185dd906184SBoaz Harrosh /*
2186dd906184SBoaz Harrosh  * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED
2187dd906184SBoaz Harrosh  * mapping
2188dd906184SBoaz Harrosh  */
2189dd906184SBoaz Harrosh static int wp_pfn_shared(struct mm_struct *mm,
2190dd906184SBoaz Harrosh 			struct vm_area_struct *vma, unsigned long address,
2191dd906184SBoaz Harrosh 			pte_t *page_table, spinlock_t *ptl, pte_t orig_pte,
2192dd906184SBoaz Harrosh 			pmd_t *pmd)
2193dd906184SBoaz Harrosh {
2194dd906184SBoaz Harrosh 	if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) {
2195dd906184SBoaz Harrosh 		struct vm_fault vmf = {
2196dd906184SBoaz Harrosh 			.page = NULL,
2197dd906184SBoaz Harrosh 			.pgoff = linear_page_index(vma, address),
2198dd906184SBoaz Harrosh 			.virtual_address = (void __user *)(address & PAGE_MASK),
2199dd906184SBoaz Harrosh 			.flags = FAULT_FLAG_WRITE | FAULT_FLAG_MKWRITE,
2200dd906184SBoaz Harrosh 		};
2201dd906184SBoaz Harrosh 		int ret;
2202dd906184SBoaz Harrosh 
2203dd906184SBoaz Harrosh 		pte_unmap_unlock(page_table, ptl);
2204dd906184SBoaz Harrosh 		ret = vma->vm_ops->pfn_mkwrite(vma, &vmf);
2205dd906184SBoaz Harrosh 		if (ret & VM_FAULT_ERROR)
2206dd906184SBoaz Harrosh 			return ret;
2207dd906184SBoaz Harrosh 		page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
2208dd906184SBoaz Harrosh 		/*
2209dd906184SBoaz Harrosh 		 * We might have raced with another page fault while we
2210dd906184SBoaz Harrosh 		 * released the pte_offset_map_lock.
2211dd906184SBoaz Harrosh 		 */
2212dd906184SBoaz Harrosh 		if (!pte_same(*page_table, orig_pte)) {
2213dd906184SBoaz Harrosh 			pte_unmap_unlock(page_table, ptl);
2214dd906184SBoaz Harrosh 			return 0;
2215dd906184SBoaz Harrosh 		}
2216dd906184SBoaz Harrosh 	}
2217dd906184SBoaz Harrosh 	return wp_page_reuse(mm, vma, address, page_table, ptl, orig_pte,
2218dd906184SBoaz Harrosh 			     NULL, 0, 0);
2219dd906184SBoaz Harrosh }
2220dd906184SBoaz Harrosh 
222193e478d4SShachar Raindel static int wp_page_shared(struct mm_struct *mm, struct vm_area_struct *vma,
222293e478d4SShachar Raindel 			  unsigned long address, pte_t *page_table,
222393e478d4SShachar Raindel 			  pmd_t *pmd, spinlock_t *ptl, pte_t orig_pte,
222493e478d4SShachar Raindel 			  struct page *old_page)
222593e478d4SShachar Raindel 	__releases(ptl)
222693e478d4SShachar Raindel {
222793e478d4SShachar Raindel 	int page_mkwrite = 0;
222893e478d4SShachar Raindel 
222993e478d4SShachar Raindel 	page_cache_get(old_page);
223093e478d4SShachar Raindel 
223193e478d4SShachar Raindel 	/*
223293e478d4SShachar Raindel 	 * Only catch write-faults on shared writable pages,
223393e478d4SShachar Raindel 	 * read-only shared pages can get COWed by
223493e478d4SShachar Raindel 	 * get_user_pages(.write=1, .force=1).
223593e478d4SShachar Raindel 	 */
223693e478d4SShachar Raindel 	if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
223793e478d4SShachar Raindel 		int tmp;
223893e478d4SShachar Raindel 
223993e478d4SShachar Raindel 		pte_unmap_unlock(page_table, ptl);
224093e478d4SShachar Raindel 		tmp = do_page_mkwrite(vma, old_page, address);
224193e478d4SShachar Raindel 		if (unlikely(!tmp || (tmp &
224293e478d4SShachar Raindel 				      (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
224393e478d4SShachar Raindel 			page_cache_release(old_page);
224493e478d4SShachar Raindel 			return tmp;
224593e478d4SShachar Raindel 		}
224693e478d4SShachar Raindel 		/*
224793e478d4SShachar Raindel 		 * Since we dropped the lock we need to revalidate
224893e478d4SShachar Raindel 		 * the PTE as someone else may have changed it.  If
224993e478d4SShachar Raindel 		 * they did, we just return, as we can count on the
225093e478d4SShachar Raindel 		 * MMU to tell us if they didn't also make it writable.
225193e478d4SShachar Raindel 		 */
225293e478d4SShachar Raindel 		page_table = pte_offset_map_lock(mm, pmd, address,
225393e478d4SShachar Raindel 						 &ptl);
225493e478d4SShachar Raindel 		if (!pte_same(*page_table, orig_pte)) {
225593e478d4SShachar Raindel 			unlock_page(old_page);
225693e478d4SShachar Raindel 			pte_unmap_unlock(page_table, ptl);
225793e478d4SShachar Raindel 			page_cache_release(old_page);
225893e478d4SShachar Raindel 			return 0;
225993e478d4SShachar Raindel 		}
226093e478d4SShachar Raindel 		page_mkwrite = 1;
226193e478d4SShachar Raindel 	}
226293e478d4SShachar Raindel 
226393e478d4SShachar Raindel 	return wp_page_reuse(mm, vma, address, page_table, ptl,
226493e478d4SShachar Raindel 			     orig_pte, old_page, page_mkwrite, 1);
226593e478d4SShachar Raindel }
226693e478d4SShachar Raindel 
22672f38ab2cSShachar Raindel /*
22681da177e4SLinus Torvalds  * This routine handles present pages, when users try to write
22691da177e4SLinus Torvalds  * to a shared page. It is done by copying the page to a new address
22701da177e4SLinus Torvalds  * and decrementing the shared-page counter for the old page.
22711da177e4SLinus Torvalds  *
22721da177e4SLinus Torvalds  * Note that this routine assumes that the protection checks have been
22731da177e4SLinus Torvalds  * done by the caller (the low-level page fault routine in most cases).
22741da177e4SLinus Torvalds  * Thus we can safely just mark it writable once we've done any necessary
22751da177e4SLinus Torvalds  * COW.
22761da177e4SLinus Torvalds  *
22771da177e4SLinus Torvalds  * We also mark the page dirty at this point even though the page will
22781da177e4SLinus Torvalds  * change only once the write actually happens. This avoids a few races,
22791da177e4SLinus Torvalds  * and potentially makes it more efficient.
22801da177e4SLinus Torvalds  *
22818f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
22828f4e2101SHugh Dickins  * but allow concurrent faults), with pte both mapped and locked.
22838f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
22841da177e4SLinus Torvalds  */
22851da177e4SLinus Torvalds static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
228665500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
22878f4e2101SHugh Dickins 		spinlock_t *ptl, pte_t orig_pte)
2288e6219ec8SNamhyung Kim 	__releases(ptl)
22891da177e4SLinus Torvalds {
22902f38ab2cSShachar Raindel 	struct page *old_page;
22911da177e4SLinus Torvalds 
22926aab341eSLinus Torvalds 	old_page = vm_normal_page(vma, address, orig_pte);
2293251b97f5SPeter Zijlstra 	if (!old_page) {
2294251b97f5SPeter Zijlstra 		/*
229564e45507SPeter Feiner 		 * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a
229664e45507SPeter Feiner 		 * VM_PFNMAP VMA.
2297251b97f5SPeter Zijlstra 		 *
2298251b97f5SPeter Zijlstra 		 * We should not cow pages in a shared writeable mapping.
2299dd906184SBoaz Harrosh 		 * Just mark the pages writable and/or call ops->pfn_mkwrite.
2300251b97f5SPeter Zijlstra 		 */
2301251b97f5SPeter Zijlstra 		if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
2302251b97f5SPeter Zijlstra 				     (VM_WRITE|VM_SHARED))
2303dd906184SBoaz Harrosh 			return wp_pfn_shared(mm, vma, address, page_table, ptl,
2304dd906184SBoaz Harrosh 					     orig_pte, pmd);
23052f38ab2cSShachar Raindel 
23062f38ab2cSShachar Raindel 		pte_unmap_unlock(page_table, ptl);
23072f38ab2cSShachar Raindel 		return wp_page_copy(mm, vma, address, page_table, pmd,
23082f38ab2cSShachar Raindel 				    orig_pte, old_page);
2309251b97f5SPeter Zijlstra 	}
23101da177e4SLinus Torvalds 
2311d08b3851SPeter Zijlstra 	/*
2312ee6a6457SPeter Zijlstra 	 * Take out anonymous pages first, anonymous shared vmas are
2313ee6a6457SPeter Zijlstra 	 * not dirty accountable.
2314d08b3851SPeter Zijlstra 	 */
23159a840895SHugh Dickins 	if (PageAnon(old_page) && !PageKsm(old_page)) {
2316ab967d86SHugh Dickins 		if (!trylock_page(old_page)) {
2317ab967d86SHugh Dickins 			page_cache_get(old_page);
2318ab967d86SHugh Dickins 			pte_unmap_unlock(page_table, ptl);
2319ab967d86SHugh Dickins 			lock_page(old_page);
2320ab967d86SHugh Dickins 			page_table = pte_offset_map_lock(mm, pmd, address,
2321ab967d86SHugh Dickins 							 &ptl);
2322ab967d86SHugh Dickins 			if (!pte_same(*page_table, orig_pte)) {
2323ab967d86SHugh Dickins 				unlock_page(old_page);
232428766805SShachar Raindel 				pte_unmap_unlock(page_table, ptl);
232528766805SShachar Raindel 				page_cache_release(old_page);
232628766805SShachar Raindel 				return 0;
2327ab967d86SHugh Dickins 			}
2328ab967d86SHugh Dickins 			page_cache_release(old_page);
2329ab967d86SHugh Dickins 		}
2330b009c024SMichel Lespinasse 		if (reuse_swap_page(old_page)) {
2331c44b6743SRik van Riel 			/*
2332c44b6743SRik van Riel 			 * The page is all ours.  Move it to our anon_vma so
2333c44b6743SRik van Riel 			 * the rmap code will not search our parent or siblings.
2334c44b6743SRik van Riel 			 * Protected against the rmap code by the page lock.
2335c44b6743SRik van Riel 			 */
2336c44b6743SRik van Riel 			page_move_anon_rmap(old_page, vma, address);
2337ee6a6457SPeter Zijlstra 			unlock_page(old_page);
23384e047f89SShachar Raindel 			return wp_page_reuse(mm, vma, address, page_table, ptl,
23394e047f89SShachar Raindel 					     orig_pte, old_page, 0, 0);
2340b009c024SMichel Lespinasse 		}
2341b009c024SMichel Lespinasse 		unlock_page(old_page);
2342ee6a6457SPeter Zijlstra 	} else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
2343d08b3851SPeter Zijlstra 					(VM_WRITE|VM_SHARED))) {
234493e478d4SShachar Raindel 		return wp_page_shared(mm, vma, address, page_table, pmd,
234593e478d4SShachar Raindel 				      ptl, orig_pte, old_page);
23461da177e4SLinus Torvalds 	}
23471da177e4SLinus Torvalds 
23481da177e4SLinus Torvalds 	/*
23491da177e4SLinus Torvalds 	 * Ok, we need to copy. Oh, well..
23501da177e4SLinus Torvalds 	 */
23511da177e4SLinus Torvalds 	page_cache_get(old_page);
235228766805SShachar Raindel 
23538f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
23542f38ab2cSShachar Raindel 	return wp_page_copy(mm, vma, address, page_table, pmd,
23552f38ab2cSShachar Raindel 			    orig_pte, old_page);
23561da177e4SLinus Torvalds }
23571da177e4SLinus Torvalds 
235897a89413SPeter Zijlstra static void unmap_mapping_range_vma(struct vm_area_struct *vma,
23591da177e4SLinus Torvalds 		unsigned long start_addr, unsigned long end_addr,
23601da177e4SLinus Torvalds 		struct zap_details *details)
23611da177e4SLinus Torvalds {
2362f5cc4eefSAl Viro 	zap_page_range_single(vma, start_addr, end_addr - start_addr, details);
23631da177e4SLinus Torvalds }
23641da177e4SLinus Torvalds 
23656b2dbba8SMichel Lespinasse static inline void unmap_mapping_range_tree(struct rb_root *root,
23661da177e4SLinus Torvalds 					    struct zap_details *details)
23671da177e4SLinus Torvalds {
23681da177e4SLinus Torvalds 	struct vm_area_struct *vma;
23691da177e4SLinus Torvalds 	pgoff_t vba, vea, zba, zea;
23701da177e4SLinus Torvalds 
23716b2dbba8SMichel Lespinasse 	vma_interval_tree_foreach(vma, root,
23721da177e4SLinus Torvalds 			details->first_index, details->last_index) {
23731da177e4SLinus Torvalds 
23741da177e4SLinus Torvalds 		vba = vma->vm_pgoff;
2375d6e93217SLibin 		vea = vba + vma_pages(vma) - 1;
23761da177e4SLinus Torvalds 		/* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
23771da177e4SLinus Torvalds 		zba = details->first_index;
23781da177e4SLinus Torvalds 		if (zba < vba)
23791da177e4SLinus Torvalds 			zba = vba;
23801da177e4SLinus Torvalds 		zea = details->last_index;
23811da177e4SLinus Torvalds 		if (zea > vea)
23821da177e4SLinus Torvalds 			zea = vea;
23831da177e4SLinus Torvalds 
238497a89413SPeter Zijlstra 		unmap_mapping_range_vma(vma,
23851da177e4SLinus Torvalds 			((zba - vba) << PAGE_SHIFT) + vma->vm_start,
23861da177e4SLinus Torvalds 			((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
238797a89413SPeter Zijlstra 				details);
23881da177e4SLinus Torvalds 	}
23891da177e4SLinus Torvalds }
23901da177e4SLinus Torvalds 
23911da177e4SLinus Torvalds /**
23928a5f14a2SKirill A. Shutemov  * unmap_mapping_range - unmap the portion of all mmaps in the specified
23938a5f14a2SKirill A. Shutemov  * address_space corresponding to the specified page range in the underlying
23948a5f14a2SKirill A. Shutemov  * file.
23958a5f14a2SKirill A. Shutemov  *
23963d41088fSMartin Waitz  * @mapping: the address space containing mmaps to be unmapped.
23971da177e4SLinus Torvalds  * @holebegin: byte in first page to unmap, relative to the start of
23981da177e4SLinus Torvalds  * the underlying file.  This will be rounded down to a PAGE_SIZE
239925d9e2d1Snpiggin@suse.de  * boundary.  Note that this is different from truncate_pagecache(), which
24001da177e4SLinus Torvalds  * must keep the partial page.  In contrast, we must get rid of
24011da177e4SLinus Torvalds  * partial pages.
24021da177e4SLinus Torvalds  * @holelen: size of prospective hole in bytes.  This will be rounded
24031da177e4SLinus Torvalds  * up to a PAGE_SIZE boundary.  A holelen of zero truncates to the
24041da177e4SLinus Torvalds  * end of the file.
24051da177e4SLinus Torvalds  * @even_cows: 1 when truncating a file, unmap even private COWed pages;
24061da177e4SLinus Torvalds  * but 0 when invalidating pagecache, don't throw away private data.
24071da177e4SLinus Torvalds  */
24081da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping,
24091da177e4SLinus Torvalds 		loff_t const holebegin, loff_t const holelen, int even_cows)
24101da177e4SLinus Torvalds {
24111da177e4SLinus Torvalds 	struct zap_details details;
24121da177e4SLinus Torvalds 	pgoff_t hba = holebegin >> PAGE_SHIFT;
24131da177e4SLinus Torvalds 	pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
24141da177e4SLinus Torvalds 
24151da177e4SLinus Torvalds 	/* Check for overflow. */
24161da177e4SLinus Torvalds 	if (sizeof(holelen) > sizeof(hlen)) {
24171da177e4SLinus Torvalds 		long long holeend =
24181da177e4SLinus Torvalds 			(holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
24191da177e4SLinus Torvalds 		if (holeend & ~(long long)ULONG_MAX)
24201da177e4SLinus Torvalds 			hlen = ULONG_MAX - hba + 1;
24211da177e4SLinus Torvalds 	}
24221da177e4SLinus Torvalds 
24231da177e4SLinus Torvalds 	details.check_mapping = even_cows? NULL: mapping;
24241da177e4SLinus Torvalds 	details.first_index = hba;
24251da177e4SLinus Torvalds 	details.last_index = hba + hlen - 1;
24261da177e4SLinus Torvalds 	if (details.last_index < details.first_index)
24271da177e4SLinus Torvalds 		details.last_index = ULONG_MAX;
24281da177e4SLinus Torvalds 
24290f90cc66SRoss Zwisler 
24300f90cc66SRoss Zwisler 	/* DAX uses i_mmap_lock to serialise file truncate vs page fault */
243148ec833bSKirill A. Shutemov 	i_mmap_lock_write(mapping);
24326b2dbba8SMichel Lespinasse 	if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap)))
24331da177e4SLinus Torvalds 		unmap_mapping_range_tree(&mapping->i_mmap, &details);
243448ec833bSKirill A. Shutemov 	i_mmap_unlock_write(mapping);
24351da177e4SLinus Torvalds }
24361da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range);
24371da177e4SLinus Torvalds 
24381da177e4SLinus Torvalds /*
24398f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
24408f4e2101SHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
24419a95f3cfSPaul Cassella  * We return with pte unmapped and unlocked.
24429a95f3cfSPaul Cassella  *
24439a95f3cfSPaul Cassella  * We return with the mmap_sem locked or unlocked in the same cases
24449a95f3cfSPaul Cassella  * as does filemap_fault().
24451da177e4SLinus Torvalds  */
244665500d23SHugh Dickins static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
244765500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
244830c9f3a9SLinus Torvalds 		unsigned int flags, pte_t orig_pte)
24491da177e4SLinus Torvalds {
24508f4e2101SHugh Dickins 	spinlock_t *ptl;
245156f31801SHugh Dickins 	struct page *page, *swapcache;
245200501b53SJohannes Weiner 	struct mem_cgroup *memcg;
245365500d23SHugh Dickins 	swp_entry_t entry;
24541da177e4SLinus Torvalds 	pte_t pte;
2455d065bd81SMichel Lespinasse 	int locked;
2456ad8c2ee8SRik van Riel 	int exclusive = 0;
245783c54070SNick Piggin 	int ret = 0;
24581da177e4SLinus Torvalds 
24594c21e2f2SHugh Dickins 	if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
24608f4e2101SHugh Dickins 		goto out;
246165500d23SHugh Dickins 
246265500d23SHugh Dickins 	entry = pte_to_swp_entry(orig_pte);
2463d1737fdbSAndi Kleen 	if (unlikely(non_swap_entry(entry))) {
24640697212aSChristoph Lameter 		if (is_migration_entry(entry)) {
24650697212aSChristoph Lameter 			migration_entry_wait(mm, pmd, address);
2466d1737fdbSAndi Kleen 		} else if (is_hwpoison_entry(entry)) {
2467d1737fdbSAndi Kleen 			ret = VM_FAULT_HWPOISON;
2468d1737fdbSAndi Kleen 		} else {
2469d1737fdbSAndi Kleen 			print_bad_pte(vma, address, orig_pte, NULL);
2470d99be1a8SHugh Dickins 			ret = VM_FAULT_SIGBUS;
2471d1737fdbSAndi Kleen 		}
24720697212aSChristoph Lameter 		goto out;
24730697212aSChristoph Lameter 	}
24740ff92245SShailabh Nagar 	delayacct_set_flag(DELAYACCT_PF_SWAPIN);
24751da177e4SLinus Torvalds 	page = lookup_swap_cache(entry);
24761da177e4SLinus Torvalds 	if (!page) {
247702098feaSHugh Dickins 		page = swapin_readahead(entry,
247802098feaSHugh Dickins 					GFP_HIGHUSER_MOVABLE, vma, address);
24791da177e4SLinus Torvalds 		if (!page) {
24801da177e4SLinus Torvalds 			/*
24818f4e2101SHugh Dickins 			 * Back out if somebody else faulted in this pte
24828f4e2101SHugh Dickins 			 * while we released the pte lock.
24831da177e4SLinus Torvalds 			 */
24848f4e2101SHugh Dickins 			page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
24851da177e4SLinus Torvalds 			if (likely(pte_same(*page_table, orig_pte)))
24861da177e4SLinus Torvalds 				ret = VM_FAULT_OOM;
24870ff92245SShailabh Nagar 			delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
248865500d23SHugh Dickins 			goto unlock;
24891da177e4SLinus Torvalds 		}
24901da177e4SLinus Torvalds 
24911da177e4SLinus Torvalds 		/* Had to read the page from swap area: Major fault */
24921da177e4SLinus Torvalds 		ret = VM_FAULT_MAJOR;
2493f8891e5eSChristoph Lameter 		count_vm_event(PGMAJFAULT);
2494456f998eSYing Han 		mem_cgroup_count_vm_event(mm, PGMAJFAULT);
2495d1737fdbSAndi Kleen 	} else if (PageHWPoison(page)) {
249671f72525SWu Fengguang 		/*
249771f72525SWu Fengguang 		 * hwpoisoned dirty swapcache pages are kept for killing
249871f72525SWu Fengguang 		 * owner processes (which may be unknown at hwpoison time)
249971f72525SWu Fengguang 		 */
2500d1737fdbSAndi Kleen 		ret = VM_FAULT_HWPOISON;
2501d1737fdbSAndi Kleen 		delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
250256f31801SHugh Dickins 		swapcache = page;
25034779cb31SAndi Kleen 		goto out_release;
25041da177e4SLinus Torvalds 	}
25051da177e4SLinus Torvalds 
250656f31801SHugh Dickins 	swapcache = page;
2507d065bd81SMichel Lespinasse 	locked = lock_page_or_retry(page, mm, flags);
2508e709ffd6SRik van Riel 
250920a1022dSBalbir Singh 	delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
2510d065bd81SMichel Lespinasse 	if (!locked) {
2511d065bd81SMichel Lespinasse 		ret |= VM_FAULT_RETRY;
2512d065bd81SMichel Lespinasse 		goto out_release;
2513d065bd81SMichel Lespinasse 	}
25141da177e4SLinus Torvalds 
25154969c119SAndrea Arcangeli 	/*
251631c4a3d3SHugh Dickins 	 * Make sure try_to_free_swap or reuse_swap_page or swapoff did not
251731c4a3d3SHugh Dickins 	 * release the swapcache from under us.  The page pin, and pte_same
251831c4a3d3SHugh Dickins 	 * test below, are not enough to exclude that.  Even if it is still
251931c4a3d3SHugh Dickins 	 * swapcache, we need to check that the page's swap has not changed.
25204969c119SAndrea Arcangeli 	 */
252131c4a3d3SHugh Dickins 	if (unlikely(!PageSwapCache(page) || page_private(page) != entry.val))
25224969c119SAndrea Arcangeli 		goto out_page;
25234969c119SAndrea Arcangeli 
2524cbf86cfeSHugh Dickins 	page = ksm_might_need_to_copy(page, vma, address);
25254969c119SAndrea Arcangeli 	if (unlikely(!page)) {
25265ad64688SHugh Dickins 		ret = VM_FAULT_OOM;
25274969c119SAndrea Arcangeli 		page = swapcache;
25284969c119SAndrea Arcangeli 		goto out_page;
25294969c119SAndrea Arcangeli 	}
25305ad64688SHugh Dickins 
2531f627c2f5SKirill A. Shutemov 	if (mem_cgroup_try_charge(page, mm, GFP_KERNEL, &memcg, false)) {
2532073e587eSKAMEZAWA Hiroyuki 		ret = VM_FAULT_OOM;
2533bc43f75cSJohannes Weiner 		goto out_page;
2534073e587eSKAMEZAWA Hiroyuki 	}
2535073e587eSKAMEZAWA Hiroyuki 
25361da177e4SLinus Torvalds 	/*
25378f4e2101SHugh Dickins 	 * Back out if somebody else already faulted in this pte.
25381da177e4SLinus Torvalds 	 */
25398f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
25409e9bef07SHugh Dickins 	if (unlikely(!pte_same(*page_table, orig_pte)))
2541b8107480SKirill Korotaev 		goto out_nomap;
2542b8107480SKirill Korotaev 
2543b8107480SKirill Korotaev 	if (unlikely(!PageUptodate(page))) {
2544b8107480SKirill Korotaev 		ret = VM_FAULT_SIGBUS;
2545b8107480SKirill Korotaev 		goto out_nomap;
25461da177e4SLinus Torvalds 	}
25471da177e4SLinus Torvalds 
25488c7c6e34SKAMEZAWA Hiroyuki 	/*
25498c7c6e34SKAMEZAWA Hiroyuki 	 * The page isn't present yet, go ahead with the fault.
25508c7c6e34SKAMEZAWA Hiroyuki 	 *
25518c7c6e34SKAMEZAWA Hiroyuki 	 * Be careful about the sequence of operations here.
25528c7c6e34SKAMEZAWA Hiroyuki 	 * To get its accounting right, reuse_swap_page() must be called
25538c7c6e34SKAMEZAWA Hiroyuki 	 * while the page is counted on swap but not yet in mapcount i.e.
25548c7c6e34SKAMEZAWA Hiroyuki 	 * before page_add_anon_rmap() and swap_free(); try_to_free_swap()
25558c7c6e34SKAMEZAWA Hiroyuki 	 * must be called after the swap_free(), or it will never succeed.
25568c7c6e34SKAMEZAWA Hiroyuki 	 */
25571da177e4SLinus Torvalds 
255834e55232SKAMEZAWA Hiroyuki 	inc_mm_counter_fast(mm, MM_ANONPAGES);
2559b084d435SKAMEZAWA Hiroyuki 	dec_mm_counter_fast(mm, MM_SWAPENTS);
25601da177e4SLinus Torvalds 	pte = mk_pte(page, vma->vm_page_prot);
256130c9f3a9SLinus Torvalds 	if ((flags & FAULT_FLAG_WRITE) && reuse_swap_page(page)) {
25621da177e4SLinus Torvalds 		pte = maybe_mkwrite(pte_mkdirty(pte), vma);
256330c9f3a9SLinus Torvalds 		flags &= ~FAULT_FLAG_WRITE;
25649a5b489bSAndrea Arcangeli 		ret |= VM_FAULT_WRITE;
2565d281ee61SKirill A. Shutemov 		exclusive = RMAP_EXCLUSIVE;
25661da177e4SLinus Torvalds 	}
25671da177e4SLinus Torvalds 	flush_icache_page(vma, page);
2568179ef71cSCyrill Gorcunov 	if (pte_swp_soft_dirty(orig_pte))
2569179ef71cSCyrill Gorcunov 		pte = pte_mksoft_dirty(pte);
25701da177e4SLinus Torvalds 	set_pte_at(mm, address, page_table, pte);
257100501b53SJohannes Weiner 	if (page == swapcache) {
2572ad8c2ee8SRik van Riel 		do_page_add_anon_rmap(page, vma, address, exclusive);
2573f627c2f5SKirill A. Shutemov 		mem_cgroup_commit_charge(page, memcg, true, false);
257400501b53SJohannes Weiner 	} else { /* ksm created a completely new copy */
2575d281ee61SKirill A. Shutemov 		page_add_new_anon_rmap(page, vma, address, false);
2576f627c2f5SKirill A. Shutemov 		mem_cgroup_commit_charge(page, memcg, false, false);
257700501b53SJohannes Weiner 		lru_cache_add_active_or_unevictable(page, vma);
257800501b53SJohannes Weiner 	}
25791da177e4SLinus Torvalds 
2580c475a8abSHugh Dickins 	swap_free(entry);
2581b291f000SNick Piggin 	if (vm_swap_full() || (vma->vm_flags & VM_LOCKED) || PageMlocked(page))
2582a2c43eedSHugh Dickins 		try_to_free_swap(page);
2583c475a8abSHugh Dickins 	unlock_page(page);
258456f31801SHugh Dickins 	if (page != swapcache) {
25854969c119SAndrea Arcangeli 		/*
25864969c119SAndrea Arcangeli 		 * Hold the lock to avoid the swap entry to be reused
25874969c119SAndrea Arcangeli 		 * until we take the PT lock for the pte_same() check
25884969c119SAndrea Arcangeli 		 * (to avoid false positives from pte_same). For
25894969c119SAndrea Arcangeli 		 * further safety release the lock after the swap_free
25904969c119SAndrea Arcangeli 		 * so that the swap count won't change under a
25914969c119SAndrea Arcangeli 		 * parallel locked swapcache.
25924969c119SAndrea Arcangeli 		 */
25934969c119SAndrea Arcangeli 		unlock_page(swapcache);
25944969c119SAndrea Arcangeli 		page_cache_release(swapcache);
25954969c119SAndrea Arcangeli 	}
2596c475a8abSHugh Dickins 
259730c9f3a9SLinus Torvalds 	if (flags & FAULT_FLAG_WRITE) {
259861469f1dSHugh Dickins 		ret |= do_wp_page(mm, vma, address, page_table, pmd, ptl, pte);
259961469f1dSHugh Dickins 		if (ret & VM_FAULT_ERROR)
260061469f1dSHugh Dickins 			ret &= VM_FAULT_ERROR;
26011da177e4SLinus Torvalds 		goto out;
26021da177e4SLinus Torvalds 	}
26031da177e4SLinus Torvalds 
26041da177e4SLinus Torvalds 	/* No need to invalidate - it was non-present before */
26054b3073e1SRussell King 	update_mmu_cache(vma, address, page_table);
260665500d23SHugh Dickins unlock:
26078f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
26081da177e4SLinus Torvalds out:
26091da177e4SLinus Torvalds 	return ret;
2610b8107480SKirill Korotaev out_nomap:
2611f627c2f5SKirill A. Shutemov 	mem_cgroup_cancel_charge(page, memcg, false);
26128f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
2613bc43f75cSJohannes Weiner out_page:
2614b8107480SKirill Korotaev 	unlock_page(page);
26154779cb31SAndi Kleen out_release:
2616b8107480SKirill Korotaev 	page_cache_release(page);
261756f31801SHugh Dickins 	if (page != swapcache) {
26184969c119SAndrea Arcangeli 		unlock_page(swapcache);
26194969c119SAndrea Arcangeli 		page_cache_release(swapcache);
26204969c119SAndrea Arcangeli 	}
262165500d23SHugh Dickins 	return ret;
26221da177e4SLinus Torvalds }
26231da177e4SLinus Torvalds 
26241da177e4SLinus Torvalds /*
26258ca3eb08SLuck, Tony  * This is like a special single-page "expand_{down|up}wards()",
26268ca3eb08SLuck, Tony  * except we must first make sure that 'address{-|+}PAGE_SIZE'
2627320b2b8dSLinus Torvalds  * doesn't hit another vma.
2628320b2b8dSLinus Torvalds  */
2629320b2b8dSLinus Torvalds static inline int check_stack_guard_page(struct vm_area_struct *vma, unsigned long address)
2630320b2b8dSLinus Torvalds {
2631320b2b8dSLinus Torvalds 	address &= PAGE_MASK;
2632320b2b8dSLinus Torvalds 	if ((vma->vm_flags & VM_GROWSDOWN) && address == vma->vm_start) {
26330e8e50e2SLinus Torvalds 		struct vm_area_struct *prev = vma->vm_prev;
2634320b2b8dSLinus Torvalds 
26350e8e50e2SLinus Torvalds 		/*
26360e8e50e2SLinus Torvalds 		 * Is there a mapping abutting this one below?
26370e8e50e2SLinus Torvalds 		 *
26380e8e50e2SLinus Torvalds 		 * That's only ok if it's the same stack mapping
26390e8e50e2SLinus Torvalds 		 * that has gotten split..
26400e8e50e2SLinus Torvalds 		 */
26410e8e50e2SLinus Torvalds 		if (prev && prev->vm_end == address)
26420e8e50e2SLinus Torvalds 			return prev->vm_flags & VM_GROWSDOWN ? 0 : -ENOMEM;
26430e8e50e2SLinus Torvalds 
2644fee7e49dSLinus Torvalds 		return expand_downwards(vma, address - PAGE_SIZE);
2645320b2b8dSLinus Torvalds 	}
26468ca3eb08SLuck, Tony 	if ((vma->vm_flags & VM_GROWSUP) && address + PAGE_SIZE == vma->vm_end) {
26478ca3eb08SLuck, Tony 		struct vm_area_struct *next = vma->vm_next;
26488ca3eb08SLuck, Tony 
26498ca3eb08SLuck, Tony 		/* As VM_GROWSDOWN but s/below/above/ */
26508ca3eb08SLuck, Tony 		if (next && next->vm_start == address + PAGE_SIZE)
26518ca3eb08SLuck, Tony 			return next->vm_flags & VM_GROWSUP ? 0 : -ENOMEM;
26528ca3eb08SLuck, Tony 
2653fee7e49dSLinus Torvalds 		return expand_upwards(vma, address + PAGE_SIZE);
26548ca3eb08SLuck, Tony 	}
2655320b2b8dSLinus Torvalds 	return 0;
2656320b2b8dSLinus Torvalds }
2657320b2b8dSLinus Torvalds 
2658320b2b8dSLinus Torvalds /*
26598f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
26608f4e2101SHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
26618f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
26621da177e4SLinus Torvalds  */
266365500d23SHugh Dickins static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
266465500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
266530c9f3a9SLinus Torvalds 		unsigned int flags)
26661da177e4SLinus Torvalds {
266700501b53SJohannes Weiner 	struct mem_cgroup *memcg;
26688f4e2101SHugh Dickins 	struct page *page;
26698f4e2101SHugh Dickins 	spinlock_t *ptl;
26701da177e4SLinus Torvalds 	pte_t entry;
26711da177e4SLinus Torvalds 
26725528f913SLinus Torvalds 	pte_unmap(page_table);
2673320b2b8dSLinus Torvalds 
26746b7339f4SKirill A. Shutemov 	/* File mapping without ->vm_ops ? */
26756b7339f4SKirill A. Shutemov 	if (vma->vm_flags & VM_SHARED)
26766b7339f4SKirill A. Shutemov 		return VM_FAULT_SIGBUS;
26776b7339f4SKirill A. Shutemov 
267811ac5524SLinus Torvalds 	/* Check if we need to add a guard page to the stack */
267911ac5524SLinus Torvalds 	if (check_stack_guard_page(vma, address) < 0)
26809c145c56SLinus Torvalds 		return VM_FAULT_SIGSEGV;
268111ac5524SLinus Torvalds 
268211ac5524SLinus Torvalds 	/* Use the zero-page for reads */
2683593befa6SDominik Dingel 	if (!(flags & FAULT_FLAG_WRITE) && !mm_forbids_zeropage(mm)) {
268462eede62SHugh Dickins 		entry = pte_mkspecial(pfn_pte(my_zero_pfn(address),
268562eede62SHugh Dickins 						vma->vm_page_prot));
268611ac5524SLinus Torvalds 		page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
2687a13ea5b7SHugh Dickins 		if (!pte_none(*page_table))
2688a13ea5b7SHugh Dickins 			goto unlock;
26896b251fc9SAndrea Arcangeli 		/* Deliver the page fault to userland, check inside PT lock */
26906b251fc9SAndrea Arcangeli 		if (userfaultfd_missing(vma)) {
26916b251fc9SAndrea Arcangeli 			pte_unmap_unlock(page_table, ptl);
26926b251fc9SAndrea Arcangeli 			return handle_userfault(vma, address, flags,
26936b251fc9SAndrea Arcangeli 						VM_UFFD_MISSING);
26946b251fc9SAndrea Arcangeli 		}
2695a13ea5b7SHugh Dickins 		goto setpte;
2696a13ea5b7SHugh Dickins 	}
2697a13ea5b7SHugh Dickins 
26981da177e4SLinus Torvalds 	/* Allocate our own private page. */
26991da177e4SLinus Torvalds 	if (unlikely(anon_vma_prepare(vma)))
270065500d23SHugh Dickins 		goto oom;
2701769848c0SMel Gorman 	page = alloc_zeroed_user_highpage_movable(vma, address);
27021da177e4SLinus Torvalds 	if (!page)
270365500d23SHugh Dickins 		goto oom;
2704eb3c24f3SMel Gorman 
2705f627c2f5SKirill A. Shutemov 	if (mem_cgroup_try_charge(page, mm, GFP_KERNEL, &memcg, false))
2706eb3c24f3SMel Gorman 		goto oom_free_page;
2707eb3c24f3SMel Gorman 
270852f37629SMinchan Kim 	/*
270952f37629SMinchan Kim 	 * The memory barrier inside __SetPageUptodate makes sure that
271052f37629SMinchan Kim 	 * preceeding stores to the page contents become visible before
271152f37629SMinchan Kim 	 * the set_pte_at() write.
271252f37629SMinchan Kim 	 */
27130ed361deSNick Piggin 	__SetPageUptodate(page);
27141da177e4SLinus Torvalds 
271565500d23SHugh Dickins 	entry = mk_pte(page, vma->vm_page_prot);
27161ac0cb5dSHugh Dickins 	if (vma->vm_flags & VM_WRITE)
27171ac0cb5dSHugh Dickins 		entry = pte_mkwrite(pte_mkdirty(entry));
27188f4e2101SHugh Dickins 
27198f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
27201c2fb7a4SAndrea Arcangeli 	if (!pte_none(*page_table))
27218f4e2101SHugh Dickins 		goto release;
27229ba69294SHugh Dickins 
27236b251fc9SAndrea Arcangeli 	/* Deliver the page fault to userland, check inside PT lock */
27246b251fc9SAndrea Arcangeli 	if (userfaultfd_missing(vma)) {
27256b251fc9SAndrea Arcangeli 		pte_unmap_unlock(page_table, ptl);
2726f627c2f5SKirill A. Shutemov 		mem_cgroup_cancel_charge(page, memcg, false);
27276b251fc9SAndrea Arcangeli 		page_cache_release(page);
27286b251fc9SAndrea Arcangeli 		return handle_userfault(vma, address, flags,
27296b251fc9SAndrea Arcangeli 					VM_UFFD_MISSING);
27306b251fc9SAndrea Arcangeli 	}
27316b251fc9SAndrea Arcangeli 
273234e55232SKAMEZAWA Hiroyuki 	inc_mm_counter_fast(mm, MM_ANONPAGES);
2733d281ee61SKirill A. Shutemov 	page_add_new_anon_rmap(page, vma, address, false);
2734f627c2f5SKirill A. Shutemov 	mem_cgroup_commit_charge(page, memcg, false, false);
273500501b53SJohannes Weiner 	lru_cache_add_active_or_unevictable(page, vma);
2736a13ea5b7SHugh Dickins setpte:
273765500d23SHugh Dickins 	set_pte_at(mm, address, page_table, entry);
27381da177e4SLinus Torvalds 
27391da177e4SLinus Torvalds 	/* No need to invalidate - it was non-present before */
27404b3073e1SRussell King 	update_mmu_cache(vma, address, page_table);
274165500d23SHugh Dickins unlock:
27428f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
274383c54070SNick Piggin 	return 0;
27448f4e2101SHugh Dickins release:
2745f627c2f5SKirill A. Shutemov 	mem_cgroup_cancel_charge(page, memcg, false);
27468f4e2101SHugh Dickins 	page_cache_release(page);
27478f4e2101SHugh Dickins 	goto unlock;
27488a9f3ccdSBalbir Singh oom_free_page:
27496dbf6d3bSHugh Dickins 	page_cache_release(page);
275065500d23SHugh Dickins oom:
27511da177e4SLinus Torvalds 	return VM_FAULT_OOM;
27521da177e4SLinus Torvalds }
27531da177e4SLinus Torvalds 
27549a95f3cfSPaul Cassella /*
27559a95f3cfSPaul Cassella  * The mmap_sem must have been held on entry, and may have been
27569a95f3cfSPaul Cassella  * released depending on flags and vma->vm_ops->fault() return value.
27579a95f3cfSPaul Cassella  * See filemap_fault() and __lock_page_retry().
27589a95f3cfSPaul Cassella  */
27597eae74afSKirill A. Shutemov static int __do_fault(struct vm_area_struct *vma, unsigned long address,
27602e4cdab0SMatthew Wilcox 			pgoff_t pgoff, unsigned int flags,
27612e4cdab0SMatthew Wilcox 			struct page *cow_page, struct page **page)
27627eae74afSKirill A. Shutemov {
27637eae74afSKirill A. Shutemov 	struct vm_fault vmf;
27647eae74afSKirill A. Shutemov 	int ret;
27657eae74afSKirill A. Shutemov 
27667eae74afSKirill A. Shutemov 	vmf.virtual_address = (void __user *)(address & PAGE_MASK);
27677eae74afSKirill A. Shutemov 	vmf.pgoff = pgoff;
27687eae74afSKirill A. Shutemov 	vmf.flags = flags;
27697eae74afSKirill A. Shutemov 	vmf.page = NULL;
2770c20cd45eSMichal Hocko 	vmf.gfp_mask = __get_fault_gfp_mask(vma);
27712e4cdab0SMatthew Wilcox 	vmf.cow_page = cow_page;
27727eae74afSKirill A. Shutemov 
27737eae74afSKirill A. Shutemov 	ret = vma->vm_ops->fault(vma, &vmf);
27747eae74afSKirill A. Shutemov 	if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
27757eae74afSKirill A. Shutemov 		return ret;
27762e4cdab0SMatthew Wilcox 	if (!vmf.page)
27772e4cdab0SMatthew Wilcox 		goto out;
27787eae74afSKirill A. Shutemov 
27797eae74afSKirill A. Shutemov 	if (unlikely(PageHWPoison(vmf.page))) {
27807eae74afSKirill A. Shutemov 		if (ret & VM_FAULT_LOCKED)
27817eae74afSKirill A. Shutemov 			unlock_page(vmf.page);
27827eae74afSKirill A. Shutemov 		page_cache_release(vmf.page);
27837eae74afSKirill A. Shutemov 		return VM_FAULT_HWPOISON;
27847eae74afSKirill A. Shutemov 	}
27857eae74afSKirill A. Shutemov 
27867eae74afSKirill A. Shutemov 	if (unlikely(!(ret & VM_FAULT_LOCKED)))
27877eae74afSKirill A. Shutemov 		lock_page(vmf.page);
27887eae74afSKirill A. Shutemov 	else
27897eae74afSKirill A. Shutemov 		VM_BUG_ON_PAGE(!PageLocked(vmf.page), vmf.page);
27907eae74afSKirill A. Shutemov 
27912e4cdab0SMatthew Wilcox  out:
27927eae74afSKirill A. Shutemov 	*page = vmf.page;
27937eae74afSKirill A. Shutemov 	return ret;
27947eae74afSKirill A. Shutemov }
27957eae74afSKirill A. Shutemov 
27968c6e50b0SKirill A. Shutemov /**
27978c6e50b0SKirill A. Shutemov  * do_set_pte - setup new PTE entry for given page and add reverse page mapping.
27988c6e50b0SKirill A. Shutemov  *
27998c6e50b0SKirill A. Shutemov  * @vma: virtual memory area
28008c6e50b0SKirill A. Shutemov  * @address: user virtual address
28018c6e50b0SKirill A. Shutemov  * @page: page to map
28028c6e50b0SKirill A. Shutemov  * @pte: pointer to target page table entry
28038c6e50b0SKirill A. Shutemov  * @write: true, if new entry is writable
28048c6e50b0SKirill A. Shutemov  * @anon: true, if it's anonymous page
28058c6e50b0SKirill A. Shutemov  *
28068c6e50b0SKirill A. Shutemov  * Caller must hold page table lock relevant for @pte.
28078c6e50b0SKirill A. Shutemov  *
28088c6e50b0SKirill A. Shutemov  * Target users are page handler itself and implementations of
28098c6e50b0SKirill A. Shutemov  * vm_ops->map_pages.
28108c6e50b0SKirill A. Shutemov  */
28118c6e50b0SKirill A. Shutemov void do_set_pte(struct vm_area_struct *vma, unsigned long address,
28123bb97794SKirill A. Shutemov 		struct page *page, pte_t *pte, bool write, bool anon)
28133bb97794SKirill A. Shutemov {
28143bb97794SKirill A. Shutemov 	pte_t entry;
28153bb97794SKirill A. Shutemov 
28163bb97794SKirill A. Shutemov 	flush_icache_page(vma, page);
28173bb97794SKirill A. Shutemov 	entry = mk_pte(page, vma->vm_page_prot);
28183bb97794SKirill A. Shutemov 	if (write)
28193bb97794SKirill A. Shutemov 		entry = maybe_mkwrite(pte_mkdirty(entry), vma);
28203bb97794SKirill A. Shutemov 	if (anon) {
28213bb97794SKirill A. Shutemov 		inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
2822d281ee61SKirill A. Shutemov 		page_add_new_anon_rmap(page, vma, address, false);
28233bb97794SKirill A. Shutemov 	} else {
2824eca56ff9SJerome Marchand 		inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page));
28253bb97794SKirill A. Shutemov 		page_add_file_rmap(page);
28263bb97794SKirill A. Shutemov 	}
28273bb97794SKirill A. Shutemov 	set_pte_at(vma->vm_mm, address, pte, entry);
28283bb97794SKirill A. Shutemov 
28293bb97794SKirill A. Shutemov 	/* no need to invalidate: a not-present page won't be cached */
28303bb97794SKirill A. Shutemov 	update_mmu_cache(vma, address, pte);
28313bb97794SKirill A. Shutemov }
28323bb97794SKirill A. Shutemov 
28333a91053aSKirill A. Shutemov static unsigned long fault_around_bytes __read_mostly =
28343a91053aSKirill A. Shutemov 	rounddown_pow_of_two(65536);
2835a9b0f861SKirill A. Shutemov 
28361592eef0SKirill A. Shutemov #ifdef CONFIG_DEBUG_FS
2837a9b0f861SKirill A. Shutemov static int fault_around_bytes_get(void *data, u64 *val)
28381592eef0SKirill A. Shutemov {
2839a9b0f861SKirill A. Shutemov 	*val = fault_around_bytes;
28401592eef0SKirill A. Shutemov 	return 0;
28411592eef0SKirill A. Shutemov }
28421592eef0SKirill A. Shutemov 
2843b4903d6eSAndrey Ryabinin /*
2844b4903d6eSAndrey Ryabinin  * fault_around_pages() and fault_around_mask() expects fault_around_bytes
2845b4903d6eSAndrey Ryabinin  * rounded down to nearest page order. It's what do_fault_around() expects to
2846b4903d6eSAndrey Ryabinin  * see.
2847b4903d6eSAndrey Ryabinin  */
2848a9b0f861SKirill A. Shutemov static int fault_around_bytes_set(void *data, u64 val)
28491592eef0SKirill A. Shutemov {
2850a9b0f861SKirill A. Shutemov 	if (val / PAGE_SIZE > PTRS_PER_PTE)
28511592eef0SKirill A. Shutemov 		return -EINVAL;
2852b4903d6eSAndrey Ryabinin 	if (val > PAGE_SIZE)
2853b4903d6eSAndrey Ryabinin 		fault_around_bytes = rounddown_pow_of_two(val);
2854b4903d6eSAndrey Ryabinin 	else
2855b4903d6eSAndrey Ryabinin 		fault_around_bytes = PAGE_SIZE; /* rounddown_pow_of_two(0) is undefined */
28561592eef0SKirill A. Shutemov 	return 0;
28571592eef0SKirill A. Shutemov }
2858a9b0f861SKirill A. Shutemov DEFINE_SIMPLE_ATTRIBUTE(fault_around_bytes_fops,
2859a9b0f861SKirill A. Shutemov 		fault_around_bytes_get, fault_around_bytes_set, "%llu\n");
28601592eef0SKirill A. Shutemov 
28611592eef0SKirill A. Shutemov static int __init fault_around_debugfs(void)
28621592eef0SKirill A. Shutemov {
28631592eef0SKirill A. Shutemov 	void *ret;
28641592eef0SKirill A. Shutemov 
2865a9b0f861SKirill A. Shutemov 	ret = debugfs_create_file("fault_around_bytes", 0644, NULL, NULL,
2866a9b0f861SKirill A. Shutemov 			&fault_around_bytes_fops);
28671592eef0SKirill A. Shutemov 	if (!ret)
2868a9b0f861SKirill A. Shutemov 		pr_warn("Failed to create fault_around_bytes in debugfs");
28691592eef0SKirill A. Shutemov 	return 0;
28701592eef0SKirill A. Shutemov }
28711592eef0SKirill A. Shutemov late_initcall(fault_around_debugfs);
28721592eef0SKirill A. Shutemov #endif
28738c6e50b0SKirill A. Shutemov 
28741fdb412bSKirill A. Shutemov /*
28751fdb412bSKirill A. Shutemov  * do_fault_around() tries to map few pages around the fault address. The hope
28761fdb412bSKirill A. Shutemov  * is that the pages will be needed soon and this will lower the number of
28771fdb412bSKirill A. Shutemov  * faults to handle.
28781fdb412bSKirill A. Shutemov  *
28791fdb412bSKirill A. Shutemov  * It uses vm_ops->map_pages() to map the pages, which skips the page if it's
28801fdb412bSKirill A. Shutemov  * not ready to be mapped: not up-to-date, locked, etc.
28811fdb412bSKirill A. Shutemov  *
28821fdb412bSKirill A. Shutemov  * This function is called with the page table lock taken. In the split ptlock
28831fdb412bSKirill A. Shutemov  * case the page table lock only protects only those entries which belong to
28841fdb412bSKirill A. Shutemov  * the page table corresponding to the fault address.
28851fdb412bSKirill A. Shutemov  *
28861fdb412bSKirill A. Shutemov  * This function doesn't cross the VMA boundaries, in order to call map_pages()
28871fdb412bSKirill A. Shutemov  * only once.
28881fdb412bSKirill A. Shutemov  *
28891fdb412bSKirill A. Shutemov  * fault_around_pages() defines how many pages we'll try to map.
28901fdb412bSKirill A. Shutemov  * do_fault_around() expects it to return a power of two less than or equal to
28911fdb412bSKirill A. Shutemov  * PTRS_PER_PTE.
28921fdb412bSKirill A. Shutemov  *
28931fdb412bSKirill A. Shutemov  * The virtual address of the area that we map is naturally aligned to the
28941fdb412bSKirill A. Shutemov  * fault_around_pages() value (and therefore to page order).  This way it's
28951fdb412bSKirill A. Shutemov  * easier to guarantee that we don't cross page table boundaries.
28961fdb412bSKirill A. Shutemov  */
28978c6e50b0SKirill A. Shutemov static void do_fault_around(struct vm_area_struct *vma, unsigned long address,
28988c6e50b0SKirill A. Shutemov 		pte_t *pte, pgoff_t pgoff, unsigned int flags)
28998c6e50b0SKirill A. Shutemov {
2900aecd6f44SKirill A. Shutemov 	unsigned long start_addr, nr_pages, mask;
29018c6e50b0SKirill A. Shutemov 	pgoff_t max_pgoff;
29028c6e50b0SKirill A. Shutemov 	struct vm_fault vmf;
29038c6e50b0SKirill A. Shutemov 	int off;
29048c6e50b0SKirill A. Shutemov 
29054db0c3c2SJason Low 	nr_pages = READ_ONCE(fault_around_bytes) >> PAGE_SHIFT;
2906aecd6f44SKirill A. Shutemov 	mask = ~(nr_pages * PAGE_SIZE - 1) & PAGE_MASK;
2907aecd6f44SKirill A. Shutemov 
2908aecd6f44SKirill A. Shutemov 	start_addr = max(address & mask, vma->vm_start);
29098c6e50b0SKirill A. Shutemov 	off = ((address - start_addr) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
29108c6e50b0SKirill A. Shutemov 	pte -= off;
29118c6e50b0SKirill A. Shutemov 	pgoff -= off;
29128c6e50b0SKirill A. Shutemov 
29138c6e50b0SKirill A. Shutemov 	/*
29148c6e50b0SKirill A. Shutemov 	 *  max_pgoff is either end of page table or end of vma
2915850e9c69SKirill A. Shutemov 	 *  or fault_around_pages() from pgoff, depending what is nearest.
29168c6e50b0SKirill A. Shutemov 	 */
29178c6e50b0SKirill A. Shutemov 	max_pgoff = pgoff - ((start_addr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) +
29188c6e50b0SKirill A. Shutemov 		PTRS_PER_PTE - 1;
29198c6e50b0SKirill A. Shutemov 	max_pgoff = min3(max_pgoff, vma_pages(vma) + vma->vm_pgoff - 1,
2920aecd6f44SKirill A. Shutemov 			pgoff + nr_pages - 1);
29218c6e50b0SKirill A. Shutemov 
29228c6e50b0SKirill A. Shutemov 	/* Check if it makes any sense to call ->map_pages */
29238c6e50b0SKirill A. Shutemov 	while (!pte_none(*pte)) {
29248c6e50b0SKirill A. Shutemov 		if (++pgoff > max_pgoff)
29258c6e50b0SKirill A. Shutemov 			return;
29268c6e50b0SKirill A. Shutemov 		start_addr += PAGE_SIZE;
29278c6e50b0SKirill A. Shutemov 		if (start_addr >= vma->vm_end)
29288c6e50b0SKirill A. Shutemov 			return;
29298c6e50b0SKirill A. Shutemov 		pte++;
29308c6e50b0SKirill A. Shutemov 	}
29318c6e50b0SKirill A. Shutemov 
29328c6e50b0SKirill A. Shutemov 	vmf.virtual_address = (void __user *) start_addr;
29338c6e50b0SKirill A. Shutemov 	vmf.pte = pte;
29348c6e50b0SKirill A. Shutemov 	vmf.pgoff = pgoff;
29358c6e50b0SKirill A. Shutemov 	vmf.max_pgoff = max_pgoff;
29368c6e50b0SKirill A. Shutemov 	vmf.flags = flags;
2937c20cd45eSMichal Hocko 	vmf.gfp_mask = __get_fault_gfp_mask(vma);
29388c6e50b0SKirill A. Shutemov 	vma->vm_ops->map_pages(vma, &vmf);
29398c6e50b0SKirill A. Shutemov }
29408c6e50b0SKirill A. Shutemov 
2941e655fb29SKirill A. Shutemov static int do_read_fault(struct mm_struct *mm, struct vm_area_struct *vma,
2942e655fb29SKirill A. Shutemov 		unsigned long address, pmd_t *pmd,
2943e655fb29SKirill A. Shutemov 		pgoff_t pgoff, unsigned int flags, pte_t orig_pte)
2944e655fb29SKirill A. Shutemov {
2945e655fb29SKirill A. Shutemov 	struct page *fault_page;
2946e655fb29SKirill A. Shutemov 	spinlock_t *ptl;
29473bb97794SKirill A. Shutemov 	pte_t *pte;
29488c6e50b0SKirill A. Shutemov 	int ret = 0;
29498c6e50b0SKirill A. Shutemov 
29508c6e50b0SKirill A. Shutemov 	/*
29518c6e50b0SKirill A. Shutemov 	 * Let's call ->map_pages() first and use ->fault() as fallback
29528c6e50b0SKirill A. Shutemov 	 * if page by the offset is not ready to be mapped (cold cache or
29538c6e50b0SKirill A. Shutemov 	 * something).
29548c6e50b0SKirill A. Shutemov 	 */
29559b4bdd2fSKirill A. Shutemov 	if (vma->vm_ops->map_pages && fault_around_bytes >> PAGE_SHIFT > 1) {
29568c6e50b0SKirill A. Shutemov 		pte = pte_offset_map_lock(mm, pmd, address, &ptl);
29578c6e50b0SKirill A. Shutemov 		do_fault_around(vma, address, pte, pgoff, flags);
29588c6e50b0SKirill A. Shutemov 		if (!pte_same(*pte, orig_pte))
29598c6e50b0SKirill A. Shutemov 			goto unlock_out;
29608c6e50b0SKirill A. Shutemov 		pte_unmap_unlock(pte, ptl);
29618c6e50b0SKirill A. Shutemov 	}
2962e655fb29SKirill A. Shutemov 
29632e4cdab0SMatthew Wilcox 	ret = __do_fault(vma, address, pgoff, flags, NULL, &fault_page);
2964e655fb29SKirill A. Shutemov 	if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
2965e655fb29SKirill A. Shutemov 		return ret;
2966e655fb29SKirill A. Shutemov 
2967e655fb29SKirill A. Shutemov 	pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2968e655fb29SKirill A. Shutemov 	if (unlikely(!pte_same(*pte, orig_pte))) {
2969e655fb29SKirill A. Shutemov 		pte_unmap_unlock(pte, ptl);
2970e655fb29SKirill A. Shutemov 		unlock_page(fault_page);
2971e655fb29SKirill A. Shutemov 		page_cache_release(fault_page);
2972e655fb29SKirill A. Shutemov 		return ret;
2973e655fb29SKirill A. Shutemov 	}
29743bb97794SKirill A. Shutemov 	do_set_pte(vma, address, fault_page, pte, false, false);
2975e655fb29SKirill A. Shutemov 	unlock_page(fault_page);
29768c6e50b0SKirill A. Shutemov unlock_out:
29778c6e50b0SKirill A. Shutemov 	pte_unmap_unlock(pte, ptl);
2978e655fb29SKirill A. Shutemov 	return ret;
2979e655fb29SKirill A. Shutemov }
2980e655fb29SKirill A. Shutemov 
2981ec47c3b9SKirill A. Shutemov static int do_cow_fault(struct mm_struct *mm, struct vm_area_struct *vma,
2982ec47c3b9SKirill A. Shutemov 		unsigned long address, pmd_t *pmd,
2983ec47c3b9SKirill A. Shutemov 		pgoff_t pgoff, unsigned int flags, pte_t orig_pte)
2984ec47c3b9SKirill A. Shutemov {
2985ec47c3b9SKirill A. Shutemov 	struct page *fault_page, *new_page;
298600501b53SJohannes Weiner 	struct mem_cgroup *memcg;
2987ec47c3b9SKirill A. Shutemov 	spinlock_t *ptl;
29883bb97794SKirill A. Shutemov 	pte_t *pte;
2989ec47c3b9SKirill A. Shutemov 	int ret;
2990ec47c3b9SKirill A. Shutemov 
2991ec47c3b9SKirill A. Shutemov 	if (unlikely(anon_vma_prepare(vma)))
2992ec47c3b9SKirill A. Shutemov 		return VM_FAULT_OOM;
2993ec47c3b9SKirill A. Shutemov 
2994ec47c3b9SKirill A. Shutemov 	new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
2995ec47c3b9SKirill A. Shutemov 	if (!new_page)
2996ec47c3b9SKirill A. Shutemov 		return VM_FAULT_OOM;
2997ec47c3b9SKirill A. Shutemov 
2998f627c2f5SKirill A. Shutemov 	if (mem_cgroup_try_charge(new_page, mm, GFP_KERNEL, &memcg, false)) {
2999ec47c3b9SKirill A. Shutemov 		page_cache_release(new_page);
3000ec47c3b9SKirill A. Shutemov 		return VM_FAULT_OOM;
3001ec47c3b9SKirill A. Shutemov 	}
3002ec47c3b9SKirill A. Shutemov 
30032e4cdab0SMatthew Wilcox 	ret = __do_fault(vma, address, pgoff, flags, new_page, &fault_page);
3004ec47c3b9SKirill A. Shutemov 	if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
3005ec47c3b9SKirill A. Shutemov 		goto uncharge_out;
3006ec47c3b9SKirill A. Shutemov 
30072e4cdab0SMatthew Wilcox 	if (fault_page)
3008ec47c3b9SKirill A. Shutemov 		copy_user_highpage(new_page, fault_page, address, vma);
3009ec47c3b9SKirill A. Shutemov 	__SetPageUptodate(new_page);
3010ec47c3b9SKirill A. Shutemov 
3011ec47c3b9SKirill A. Shutemov 	pte = pte_offset_map_lock(mm, pmd, address, &ptl);
3012ec47c3b9SKirill A. Shutemov 	if (unlikely(!pte_same(*pte, orig_pte))) {
3013ec47c3b9SKirill A. Shutemov 		pte_unmap_unlock(pte, ptl);
30142e4cdab0SMatthew Wilcox 		if (fault_page) {
3015ec47c3b9SKirill A. Shutemov 			unlock_page(fault_page);
3016ec47c3b9SKirill A. Shutemov 			page_cache_release(fault_page);
30172e4cdab0SMatthew Wilcox 		} else {
30182e4cdab0SMatthew Wilcox 			/*
30192e4cdab0SMatthew Wilcox 			 * The fault handler has no page to lock, so it holds
30200df9d41aSYigal Korman 			 * i_mmap_lock for read to protect against truncate.
30212e4cdab0SMatthew Wilcox 			 */
30220df9d41aSYigal Korman 			i_mmap_unlock_read(vma->vm_file->f_mapping);
30232e4cdab0SMatthew Wilcox 		}
3024ec47c3b9SKirill A. Shutemov 		goto uncharge_out;
3025ec47c3b9SKirill A. Shutemov 	}
30263bb97794SKirill A. Shutemov 	do_set_pte(vma, address, new_page, pte, true, true);
3027f627c2f5SKirill A. Shutemov 	mem_cgroup_commit_charge(new_page, memcg, false, false);
302800501b53SJohannes Weiner 	lru_cache_add_active_or_unevictable(new_page, vma);
3029ec47c3b9SKirill A. Shutemov 	pte_unmap_unlock(pte, ptl);
30302e4cdab0SMatthew Wilcox 	if (fault_page) {
3031ec47c3b9SKirill A. Shutemov 		unlock_page(fault_page);
3032ec47c3b9SKirill A. Shutemov 		page_cache_release(fault_page);
30332e4cdab0SMatthew Wilcox 	} else {
30342e4cdab0SMatthew Wilcox 		/*
30352e4cdab0SMatthew Wilcox 		 * The fault handler has no page to lock, so it holds
30360df9d41aSYigal Korman 		 * i_mmap_lock for read to protect against truncate.
30372e4cdab0SMatthew Wilcox 		 */
30380df9d41aSYigal Korman 		i_mmap_unlock_read(vma->vm_file->f_mapping);
30392e4cdab0SMatthew Wilcox 	}
3040ec47c3b9SKirill A. Shutemov 	return ret;
3041ec47c3b9SKirill A. Shutemov uncharge_out:
3042f627c2f5SKirill A. Shutemov 	mem_cgroup_cancel_charge(new_page, memcg, false);
3043ec47c3b9SKirill A. Shutemov 	page_cache_release(new_page);
3044ec47c3b9SKirill A. Shutemov 	return ret;
3045ec47c3b9SKirill A. Shutemov }
3046ec47c3b9SKirill A. Shutemov 
3047f0c6d4d2SKirill A. Shutemov static int do_shared_fault(struct mm_struct *mm, struct vm_area_struct *vma,
304816abfa08SHugh Dickins 		unsigned long address, pmd_t *pmd,
304954cb8821SNick Piggin 		pgoff_t pgoff, unsigned int flags, pte_t orig_pte)
30501da177e4SLinus Torvalds {
3051f0c6d4d2SKirill A. Shutemov 	struct page *fault_page;
3052f0c6d4d2SKirill A. Shutemov 	struct address_space *mapping;
30538f4e2101SHugh Dickins 	spinlock_t *ptl;
30543bb97794SKirill A. Shutemov 	pte_t *pte;
3055f0c6d4d2SKirill A. Shutemov 	int dirtied = 0;
3056f0c6d4d2SKirill A. Shutemov 	int ret, tmp;
30571d65f86dSKAMEZAWA Hiroyuki 
30582e4cdab0SMatthew Wilcox 	ret = __do_fault(vma, address, pgoff, flags, NULL, &fault_page);
30597eae74afSKirill A. Shutemov 	if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
3060f0c6d4d2SKirill A. Shutemov 		return ret;
30611da177e4SLinus Torvalds 
30621da177e4SLinus Torvalds 	/*
3063f0c6d4d2SKirill A. Shutemov 	 * Check if the backing address space wants to know that the page is
3064f0c6d4d2SKirill A. Shutemov 	 * about to become writable
30651da177e4SLinus Torvalds 	 */
3066fb09a464SKirill A. Shutemov 	if (vma->vm_ops->page_mkwrite) {
3067f0c6d4d2SKirill A. Shutemov 		unlock_page(fault_page);
3068fb09a464SKirill A. Shutemov 		tmp = do_page_mkwrite(vma, fault_page, address);
3069fb09a464SKirill A. Shutemov 		if (unlikely(!tmp ||
3070fb09a464SKirill A. Shutemov 				(tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
3071f0c6d4d2SKirill A. Shutemov 			page_cache_release(fault_page);
3072f0c6d4d2SKirill A. Shutemov 			return tmp;
307369676147SMark Fasheh 		}
3074d0217ac0SNick Piggin 	}
3075fb09a464SKirill A. Shutemov 
3076f0c6d4d2SKirill A. Shutemov 	pte = pte_offset_map_lock(mm, pmd, address, &ptl);
3077f0c6d4d2SKirill A. Shutemov 	if (unlikely(!pte_same(*pte, orig_pte))) {
3078f0c6d4d2SKirill A. Shutemov 		pte_unmap_unlock(pte, ptl);
3079f0c6d4d2SKirill A. Shutemov 		unlock_page(fault_page);
3080f0c6d4d2SKirill A. Shutemov 		page_cache_release(fault_page);
3081f0c6d4d2SKirill A. Shutemov 		return ret;
30829637a5efSDavid Howells 	}
30833bb97794SKirill A. Shutemov 	do_set_pte(vma, address, fault_page, pte, true, false);
3084f0c6d4d2SKirill A. Shutemov 	pte_unmap_unlock(pte, ptl);
3085d00806b1SNick Piggin 
3086f0c6d4d2SKirill A. Shutemov 	if (set_page_dirty(fault_page))
308741c4d25fSJan Kara 		dirtied = 1;
3088d82fa87dSAndrew Morton 	/*
3089d82fa87dSAndrew Morton 	 * Take a local copy of the address_space - page.mapping may be zeroed
3090d82fa87dSAndrew Morton 	 * by truncate after unlock_page().   The address_space itself remains
3091d82fa87dSAndrew Morton 	 * pinned by vma->vm_file's reference.  We rely on unlock_page()'s
3092d82fa87dSAndrew Morton 	 * release semantics to prevent the compiler from undoing this copying.
3093d82fa87dSAndrew Morton 	 */
30941c290f64SKirill A. Shutemov 	mapping = page_rmapping(fault_page);
3095f0c6d4d2SKirill A. Shutemov 	unlock_page(fault_page);
3096f0c6d4d2SKirill A. Shutemov 	if ((dirtied || vma->vm_ops->page_mkwrite) && mapping) {
3097b827e496SNick Piggin 		/*
3098b827e496SNick Piggin 		 * Some device drivers do not set page.mapping but still
3099b827e496SNick Piggin 		 * dirty their pages
3100b827e496SNick Piggin 		 */
3101b827e496SNick Piggin 		balance_dirty_pages_ratelimited(mapping);
3102d08b3851SPeter Zijlstra 	}
3103d00806b1SNick Piggin 
310474ec6751SJohannes Weiner 	if (!vma->vm_ops->page_mkwrite)
3105b827e496SNick Piggin 		file_update_time(vma->vm_file);
3106b827e496SNick Piggin 
3107b827e496SNick Piggin 	return ret;
310854cb8821SNick Piggin }
3109d00806b1SNick Piggin 
31109a95f3cfSPaul Cassella /*
31119a95f3cfSPaul Cassella  * We enter with non-exclusive mmap_sem (to exclude vma changes,
31129a95f3cfSPaul Cassella  * but allow concurrent faults).
31139a95f3cfSPaul Cassella  * The mmap_sem may have been released depending on flags and our
31149a95f3cfSPaul Cassella  * return value.  See filemap_fault() and __lock_page_or_retry().
31159a95f3cfSPaul Cassella  */
31169b4bdd2fSKirill A. Shutemov static int do_fault(struct mm_struct *mm, struct vm_area_struct *vma,
311754cb8821SNick Piggin 		unsigned long address, pte_t *page_table, pmd_t *pmd,
311830c9f3a9SLinus Torvalds 		unsigned int flags, pte_t orig_pte)
311954cb8821SNick Piggin {
312054cb8821SNick Piggin 	pgoff_t pgoff = (((address & PAGE_MASK)
31210da7e01fSDean Nelson 			- vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
312254cb8821SNick Piggin 
312316abfa08SHugh Dickins 	pte_unmap(page_table);
31246b7339f4SKirill A. Shutemov 	/* The VMA was not fully populated on mmap() or missing VM_DONTEXPAND */
31256b7339f4SKirill A. Shutemov 	if (!vma->vm_ops->fault)
31266b7339f4SKirill A. Shutemov 		return VM_FAULT_SIGBUS;
3127e655fb29SKirill A. Shutemov 	if (!(flags & FAULT_FLAG_WRITE))
3128e655fb29SKirill A. Shutemov 		return do_read_fault(mm, vma, address, pmd, pgoff, flags,
3129e655fb29SKirill A. Shutemov 				orig_pte);
3130ec47c3b9SKirill A. Shutemov 	if (!(vma->vm_flags & VM_SHARED))
3131ec47c3b9SKirill A. Shutemov 		return do_cow_fault(mm, vma, address, pmd, pgoff, flags,
3132ec47c3b9SKirill A. Shutemov 				orig_pte);
3133f0c6d4d2SKirill A. Shutemov 	return do_shared_fault(mm, vma, address, pmd, pgoff, flags, orig_pte);
313454cb8821SNick Piggin }
313554cb8821SNick Piggin 
3136b19a9939SRashika Kheria static int numa_migrate_prep(struct page *page, struct vm_area_struct *vma,
313704bb2f94SRik van Riel 				unsigned long addr, int page_nid,
313804bb2f94SRik van Riel 				int *flags)
31399532fec1SMel Gorman {
31409532fec1SMel Gorman 	get_page(page);
31419532fec1SMel Gorman 
31429532fec1SMel Gorman 	count_vm_numa_event(NUMA_HINT_FAULTS);
314304bb2f94SRik van Riel 	if (page_nid == numa_node_id()) {
31449532fec1SMel Gorman 		count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
314504bb2f94SRik van Riel 		*flags |= TNF_FAULT_LOCAL;
314604bb2f94SRik van Riel 	}
31479532fec1SMel Gorman 
31489532fec1SMel Gorman 	return mpol_misplaced(page, vma, addr);
31499532fec1SMel Gorman }
31509532fec1SMel Gorman 
3151b19a9939SRashika Kheria static int do_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
3152d10e63f2SMel Gorman 		   unsigned long addr, pte_t pte, pte_t *ptep, pmd_t *pmd)
3153d10e63f2SMel Gorman {
31544daae3b4SMel Gorman 	struct page *page = NULL;
3155d10e63f2SMel Gorman 	spinlock_t *ptl;
31568191acbdSMel Gorman 	int page_nid = -1;
315790572890SPeter Zijlstra 	int last_cpupid;
3158cbee9f88SPeter Zijlstra 	int target_nid;
3159b8593bfdSMel Gorman 	bool migrated = false;
3160b191f9b1SMel Gorman 	bool was_writable = pte_write(pte);
31616688cc05SPeter Zijlstra 	int flags = 0;
3162d10e63f2SMel Gorman 
3163c0e7cad9SMel Gorman 	/* A PROT_NONE fault should not end up here */
3164c0e7cad9SMel Gorman 	BUG_ON(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)));
3165c0e7cad9SMel Gorman 
3166d10e63f2SMel Gorman 	/*
3167d10e63f2SMel Gorman 	* The "pte" at this point cannot be used safely without
3168d10e63f2SMel Gorman 	* validation through pte_unmap_same(). It's of NUMA type but
3169d10e63f2SMel Gorman 	* the pfn may be screwed if the read is non atomic.
3170d10e63f2SMel Gorman 	*
31714d942466SMel Gorman 	* We can safely just do a "set_pte_at()", because the old
31724d942466SMel Gorman 	* page table entry is not accessible, so there would be no
31734d942466SMel Gorman 	* concurrent hardware modifications to the PTE.
3174d10e63f2SMel Gorman 	*/
3175d10e63f2SMel Gorman 	ptl = pte_lockptr(mm, pmd);
3176d10e63f2SMel Gorman 	spin_lock(ptl);
31774daae3b4SMel Gorman 	if (unlikely(!pte_same(*ptep, pte))) {
31784daae3b4SMel Gorman 		pte_unmap_unlock(ptep, ptl);
31794daae3b4SMel Gorman 		goto out;
31804daae3b4SMel Gorman 	}
31814daae3b4SMel Gorman 
31824d942466SMel Gorman 	/* Make it present again */
31834d942466SMel Gorman 	pte = pte_modify(pte, vma->vm_page_prot);
31844d942466SMel Gorman 	pte = pte_mkyoung(pte);
3185b191f9b1SMel Gorman 	if (was_writable)
3186b191f9b1SMel Gorman 		pte = pte_mkwrite(pte);
3187d10e63f2SMel Gorman 	set_pte_at(mm, addr, ptep, pte);
3188d10e63f2SMel Gorman 	update_mmu_cache(vma, addr, ptep);
3189d10e63f2SMel Gorman 
3190d10e63f2SMel Gorman 	page = vm_normal_page(vma, addr, pte);
3191d10e63f2SMel Gorman 	if (!page) {
3192d10e63f2SMel Gorman 		pte_unmap_unlock(ptep, ptl);
3193d10e63f2SMel Gorman 		return 0;
3194d10e63f2SMel Gorman 	}
3195d10e63f2SMel Gorman 
31966688cc05SPeter Zijlstra 	/*
3197bea66fbdSMel Gorman 	 * Avoid grouping on RO pages in general. RO pages shouldn't hurt as
3198bea66fbdSMel Gorman 	 * much anyway since they can be in shared cache state. This misses
3199bea66fbdSMel Gorman 	 * the case where a mapping is writable but the process never writes
3200bea66fbdSMel Gorman 	 * to it but pte_write gets cleared during protection updates and
3201bea66fbdSMel Gorman 	 * pte_dirty has unpredictable behaviour between PTE scan updates,
3202bea66fbdSMel Gorman 	 * background writeback, dirty balancing and application behaviour.
32036688cc05SPeter Zijlstra 	 */
3204bea66fbdSMel Gorman 	if (!(vma->vm_flags & VM_WRITE))
32056688cc05SPeter Zijlstra 		flags |= TNF_NO_GROUP;
32066688cc05SPeter Zijlstra 
3207dabe1d99SRik van Riel 	/*
3208dabe1d99SRik van Riel 	 * Flag if the page is shared between multiple address spaces. This
3209dabe1d99SRik van Riel 	 * is later used when determining whether to group tasks together
3210dabe1d99SRik van Riel 	 */
3211dabe1d99SRik van Riel 	if (page_mapcount(page) > 1 && (vma->vm_flags & VM_SHARED))
3212dabe1d99SRik van Riel 		flags |= TNF_SHARED;
3213dabe1d99SRik van Riel 
321490572890SPeter Zijlstra 	last_cpupid = page_cpupid_last(page);
32158191acbdSMel Gorman 	page_nid = page_to_nid(page);
321604bb2f94SRik van Riel 	target_nid = numa_migrate_prep(page, vma, addr, page_nid, &flags);
3217d10e63f2SMel Gorman 	pte_unmap_unlock(ptep, ptl);
32184daae3b4SMel Gorman 	if (target_nid == -1) {
32194daae3b4SMel Gorman 		put_page(page);
32204daae3b4SMel Gorman 		goto out;
32214daae3b4SMel Gorman 	}
32224daae3b4SMel Gorman 
32234daae3b4SMel Gorman 	/* Migrate to the requested node */
32241bc115d8SMel Gorman 	migrated = migrate_misplaced_page(page, vma, target_nid);
32256688cc05SPeter Zijlstra 	if (migrated) {
32268191acbdSMel Gorman 		page_nid = target_nid;
32276688cc05SPeter Zijlstra 		flags |= TNF_MIGRATED;
3228074c2381SMel Gorman 	} else
3229074c2381SMel Gorman 		flags |= TNF_MIGRATE_FAIL;
32304daae3b4SMel Gorman 
32314daae3b4SMel Gorman out:
32328191acbdSMel Gorman 	if (page_nid != -1)
32336688cc05SPeter Zijlstra 		task_numa_fault(last_cpupid, page_nid, 1, flags);
3234d10e63f2SMel Gorman 	return 0;
3235d10e63f2SMel Gorman }
3236d10e63f2SMel Gorman 
3237b96375f7SMatthew Wilcox static int create_huge_pmd(struct mm_struct *mm, struct vm_area_struct *vma,
3238b96375f7SMatthew Wilcox 			unsigned long address, pmd_t *pmd, unsigned int flags)
3239b96375f7SMatthew Wilcox {
3240fb6dd5faSKirill A. Shutemov 	if (vma_is_anonymous(vma))
3241b96375f7SMatthew Wilcox 		return do_huge_pmd_anonymous_page(mm, vma, address, pmd, flags);
3242b96375f7SMatthew Wilcox 	if (vma->vm_ops->pmd_fault)
3243b96375f7SMatthew Wilcox 		return vma->vm_ops->pmd_fault(vma, address, pmd, flags);
3244b96375f7SMatthew Wilcox 	return VM_FAULT_FALLBACK;
3245b96375f7SMatthew Wilcox }
3246b96375f7SMatthew Wilcox 
3247b96375f7SMatthew Wilcox static int wp_huge_pmd(struct mm_struct *mm, struct vm_area_struct *vma,
3248b96375f7SMatthew Wilcox 			unsigned long address, pmd_t *pmd, pmd_t orig_pmd,
3249b96375f7SMatthew Wilcox 			unsigned int flags)
3250b96375f7SMatthew Wilcox {
3251fb6dd5faSKirill A. Shutemov 	if (vma_is_anonymous(vma))
3252b96375f7SMatthew Wilcox 		return do_huge_pmd_wp_page(mm, vma, address, pmd, orig_pmd);
3253b96375f7SMatthew Wilcox 	if (vma->vm_ops->pmd_fault)
3254b96375f7SMatthew Wilcox 		return vma->vm_ops->pmd_fault(vma, address, pmd, flags);
3255b96375f7SMatthew Wilcox 	return VM_FAULT_FALLBACK;
3256b96375f7SMatthew Wilcox }
3257b96375f7SMatthew Wilcox 
32581da177e4SLinus Torvalds /*
32591da177e4SLinus Torvalds  * These routines also need to handle stuff like marking pages dirty
32601da177e4SLinus Torvalds  * and/or accessed for architectures that don't do it in hardware (most
32611da177e4SLinus Torvalds  * RISC architectures).  The early dirtying is also good on the i386.
32621da177e4SLinus Torvalds  *
32631da177e4SLinus Torvalds  * There is also a hook called "update_mmu_cache()" that architectures
32641da177e4SLinus Torvalds  * with external mmu caches can use to update those (ie the Sparc or
32651da177e4SLinus Torvalds  * PowerPC hashed page tables that act as extended TLBs).
32661da177e4SLinus Torvalds  *
3267c74df32cSHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
3268c74df32cSHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
32699a95f3cfSPaul Cassella  * We return with pte unmapped and unlocked.
32709a95f3cfSPaul Cassella  *
32719a95f3cfSPaul Cassella  * The mmap_sem may have been released depending on flags and our
32729a95f3cfSPaul Cassella  * return value.  See filemap_fault() and __lock_page_or_retry().
32731da177e4SLinus Torvalds  */
3274c0292554SKirill A. Shutemov static int handle_pte_fault(struct mm_struct *mm,
32751da177e4SLinus Torvalds 		     struct vm_area_struct *vma, unsigned long address,
327630c9f3a9SLinus Torvalds 		     pte_t *pte, pmd_t *pmd, unsigned int flags)
32771da177e4SLinus Torvalds {
32781da177e4SLinus Torvalds 	pte_t entry;
32798f4e2101SHugh Dickins 	spinlock_t *ptl;
32801da177e4SLinus Torvalds 
3281e37c6982SChristian Borntraeger 	/*
3282e37c6982SChristian Borntraeger 	 * some architectures can have larger ptes than wordsize,
3283e37c6982SChristian Borntraeger 	 * e.g.ppc44x-defconfig has CONFIG_PTE_64BIT=y and CONFIG_32BIT=y,
3284e37c6982SChristian Borntraeger 	 * so READ_ONCE or ACCESS_ONCE cannot guarantee atomic accesses.
3285e37c6982SChristian Borntraeger 	 * The code below just needs a consistent view for the ifs and
3286e37c6982SChristian Borntraeger 	 * we later double check anyway with the ptl lock held. So here
3287e37c6982SChristian Borntraeger 	 * a barrier will do.
3288e37c6982SChristian Borntraeger 	 */
3289e37c6982SChristian Borntraeger 	entry = *pte;
3290e37c6982SChristian Borntraeger 	barrier();
32911da177e4SLinus Torvalds 	if (!pte_present(entry)) {
329265500d23SHugh Dickins 		if (pte_none(entry)) {
3293b5330628SOleg Nesterov 			if (vma_is_anonymous(vma))
3294b5330628SOleg Nesterov 				return do_anonymous_page(mm, vma, address,
3295b5330628SOleg Nesterov 							 pte, pmd, flags);
3296b5330628SOleg Nesterov 			else
32976b7339f4SKirill A. Shutemov 				return do_fault(mm, vma, address, pte, pmd,
32986b7339f4SKirill A. Shutemov 						flags, entry);
329965500d23SHugh Dickins 		}
330065500d23SHugh Dickins 		return do_swap_page(mm, vma, address,
330130c9f3a9SLinus Torvalds 					pte, pmd, flags, entry);
33021da177e4SLinus Torvalds 	}
33031da177e4SLinus Torvalds 
33048a0516edSMel Gorman 	if (pte_protnone(entry))
3305d10e63f2SMel Gorman 		return do_numa_page(mm, vma, address, entry, pte, pmd);
3306d10e63f2SMel Gorman 
33074c21e2f2SHugh Dickins 	ptl = pte_lockptr(mm, pmd);
33088f4e2101SHugh Dickins 	spin_lock(ptl);
33098f4e2101SHugh Dickins 	if (unlikely(!pte_same(*pte, entry)))
33108f4e2101SHugh Dickins 		goto unlock;
331130c9f3a9SLinus Torvalds 	if (flags & FAULT_FLAG_WRITE) {
33121da177e4SLinus Torvalds 		if (!pte_write(entry))
33138f4e2101SHugh Dickins 			return do_wp_page(mm, vma, address,
33148f4e2101SHugh Dickins 					pte, pmd, ptl, entry);
33151da177e4SLinus Torvalds 		entry = pte_mkdirty(entry);
33161da177e4SLinus Torvalds 	}
33171da177e4SLinus Torvalds 	entry = pte_mkyoung(entry);
331830c9f3a9SLinus Torvalds 	if (ptep_set_access_flags(vma, address, pte, entry, flags & FAULT_FLAG_WRITE)) {
33194b3073e1SRussell King 		update_mmu_cache(vma, address, pte);
33201a44e149SAndrea Arcangeli 	} else {
33211a44e149SAndrea Arcangeli 		/*
33221a44e149SAndrea Arcangeli 		 * This is needed only for protection faults but the arch code
33231a44e149SAndrea Arcangeli 		 * is not yet telling us if this is a protection fault or not.
33241a44e149SAndrea Arcangeli 		 * This still avoids useless tlb flushes for .text page faults
33251a44e149SAndrea Arcangeli 		 * with threads.
33261a44e149SAndrea Arcangeli 		 */
332730c9f3a9SLinus Torvalds 		if (flags & FAULT_FLAG_WRITE)
332861c77326SShaohua Li 			flush_tlb_fix_spurious_fault(vma, address);
33291a44e149SAndrea Arcangeli 	}
33308f4e2101SHugh Dickins unlock:
33318f4e2101SHugh Dickins 	pte_unmap_unlock(pte, ptl);
333283c54070SNick Piggin 	return 0;
33331da177e4SLinus Torvalds }
33341da177e4SLinus Torvalds 
33351da177e4SLinus Torvalds /*
33361da177e4SLinus Torvalds  * By the time we get here, we already hold the mm semaphore
33379a95f3cfSPaul Cassella  *
33389a95f3cfSPaul Cassella  * The mmap_sem may have been released depending on flags and our
33399a95f3cfSPaul Cassella  * return value.  See filemap_fault() and __lock_page_or_retry().
33401da177e4SLinus Torvalds  */
3341519e5247SJohannes Weiner static int __handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
3342d06063ccSLinus Torvalds 			     unsigned long address, unsigned int flags)
33431da177e4SLinus Torvalds {
33441da177e4SLinus Torvalds 	pgd_t *pgd;
33451da177e4SLinus Torvalds 	pud_t *pud;
33461da177e4SLinus Torvalds 	pmd_t *pmd;
33471da177e4SLinus Torvalds 	pte_t *pte;
33481da177e4SLinus Torvalds 
3349ac9b9c66SHugh Dickins 	if (unlikely(is_vm_hugetlb_page(vma)))
335030c9f3a9SLinus Torvalds 		return hugetlb_fault(mm, vma, address, flags);
33511da177e4SLinus Torvalds 
33521da177e4SLinus Torvalds 	pgd = pgd_offset(mm, address);
33531da177e4SLinus Torvalds 	pud = pud_alloc(mm, pgd, address);
33541da177e4SLinus Torvalds 	if (!pud)
3355c74df32cSHugh Dickins 		return VM_FAULT_OOM;
33561da177e4SLinus Torvalds 	pmd = pmd_alloc(mm, pud, address);
33571da177e4SLinus Torvalds 	if (!pmd)
3358c74df32cSHugh Dickins 		return VM_FAULT_OOM;
335971e3aac0SAndrea Arcangeli 	if (pmd_none(*pmd) && transparent_hugepage_enabled(vma)) {
3360b96375f7SMatthew Wilcox 		int ret = create_huge_pmd(mm, vma, address, pmd, flags);
3361c0292554SKirill A. Shutemov 		if (!(ret & VM_FAULT_FALLBACK))
3362c0292554SKirill A. Shutemov 			return ret;
336371e3aac0SAndrea Arcangeli 	} else {
336471e3aac0SAndrea Arcangeli 		pmd_t orig_pmd = *pmd;
33651f1d06c3SDavid Rientjes 		int ret;
33661f1d06c3SDavid Rientjes 
336771e3aac0SAndrea Arcangeli 		barrier();
336871e3aac0SAndrea Arcangeli 		if (pmd_trans_huge(orig_pmd)) {
3369a1dd450bSWill Deacon 			unsigned int dirty = flags & FAULT_FLAG_WRITE;
3370a1dd450bSWill Deacon 
33718a0516edSMel Gorman 			if (pmd_protnone(orig_pmd))
33724daae3b4SMel Gorman 				return do_huge_pmd_numa_page(mm, vma, address,
3373d10e63f2SMel Gorman 							     orig_pmd, pmd);
3374d10e63f2SMel Gorman 
33753d59eebcSLinus Torvalds 			if (dirty && !pmd_write(orig_pmd)) {
3376b96375f7SMatthew Wilcox 				ret = wp_huge_pmd(mm, vma, address, pmd,
3377b96375f7SMatthew Wilcox 							orig_pmd, flags);
33789845cbbdSKirill A. Shutemov 				if (!(ret & VM_FAULT_FALLBACK))
33791f1d06c3SDavid Rientjes 					return ret;
3380a1dd450bSWill Deacon 			} else {
3381a1dd450bSWill Deacon 				huge_pmd_set_accessed(mm, vma, address, pmd,
3382a1dd450bSWill Deacon 						      orig_pmd, dirty);
338371e3aac0SAndrea Arcangeli 				return 0;
338471e3aac0SAndrea Arcangeli 			}
338571e3aac0SAndrea Arcangeli 		}
33869845cbbdSKirill A. Shutemov 	}
338771e3aac0SAndrea Arcangeli 
338871e3aac0SAndrea Arcangeli 	/*
338971e3aac0SAndrea Arcangeli 	 * Use __pte_alloc instead of pte_alloc_map, because we can't
339071e3aac0SAndrea Arcangeli 	 * run pte_offset_map on the pmd, if an huge pmd could
339171e3aac0SAndrea Arcangeli 	 * materialize from under us from a different thread.
339271e3aac0SAndrea Arcangeli 	 */
33934fd01770SMel Gorman 	if (unlikely(pmd_none(*pmd)) &&
33944fd01770SMel Gorman 	    unlikely(__pte_alloc(mm, vma, pmd, address)))
3395c74df32cSHugh Dickins 		return VM_FAULT_OOM;
339671e3aac0SAndrea Arcangeli 	/* if an huge pmd materialized from under us just retry later */
339771e3aac0SAndrea Arcangeli 	if (unlikely(pmd_trans_huge(*pmd)))
339871e3aac0SAndrea Arcangeli 		return 0;
339971e3aac0SAndrea Arcangeli 	/*
340071e3aac0SAndrea Arcangeli 	 * A regular pmd is established and it can't morph into a huge pmd
340171e3aac0SAndrea Arcangeli 	 * from under us anymore at this point because we hold the mmap_sem
340271e3aac0SAndrea Arcangeli 	 * read mode and khugepaged takes it in write mode. So now it's
340371e3aac0SAndrea Arcangeli 	 * safe to run pte_offset_map().
340471e3aac0SAndrea Arcangeli 	 */
340571e3aac0SAndrea Arcangeli 	pte = pte_offset_map(pmd, address);
34061da177e4SLinus Torvalds 
340730c9f3a9SLinus Torvalds 	return handle_pte_fault(mm, vma, address, pte, pmd, flags);
34081da177e4SLinus Torvalds }
34091da177e4SLinus Torvalds 
34109a95f3cfSPaul Cassella /*
34119a95f3cfSPaul Cassella  * By the time we get here, we already hold the mm semaphore
34129a95f3cfSPaul Cassella  *
34139a95f3cfSPaul Cassella  * The mmap_sem may have been released depending on flags and our
34149a95f3cfSPaul Cassella  * return value.  See filemap_fault() and __lock_page_or_retry().
34159a95f3cfSPaul Cassella  */
3416519e5247SJohannes Weiner int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
3417519e5247SJohannes Weiner 		    unsigned long address, unsigned int flags)
3418519e5247SJohannes Weiner {
3419519e5247SJohannes Weiner 	int ret;
3420519e5247SJohannes Weiner 
3421519e5247SJohannes Weiner 	__set_current_state(TASK_RUNNING);
3422519e5247SJohannes Weiner 
3423519e5247SJohannes Weiner 	count_vm_event(PGFAULT);
3424519e5247SJohannes Weiner 	mem_cgroup_count_vm_event(mm, PGFAULT);
3425519e5247SJohannes Weiner 
3426519e5247SJohannes Weiner 	/* do counter updates before entering really critical section. */
3427519e5247SJohannes Weiner 	check_sync_rss_stat(current);
3428519e5247SJohannes Weiner 
3429519e5247SJohannes Weiner 	/*
3430519e5247SJohannes Weiner 	 * Enable the memcg OOM handling for faults triggered in user
3431519e5247SJohannes Weiner 	 * space.  Kernel faults are handled more gracefully.
3432519e5247SJohannes Weiner 	 */
3433519e5247SJohannes Weiner 	if (flags & FAULT_FLAG_USER)
343449426420SJohannes Weiner 		mem_cgroup_oom_enable();
3435519e5247SJohannes Weiner 
3436519e5247SJohannes Weiner 	ret = __handle_mm_fault(mm, vma, address, flags);
3437519e5247SJohannes Weiner 
343849426420SJohannes Weiner 	if (flags & FAULT_FLAG_USER) {
343949426420SJohannes Weiner 		mem_cgroup_oom_disable();
344049426420SJohannes Weiner                 /*
344149426420SJohannes Weiner                  * The task may have entered a memcg OOM situation but
344249426420SJohannes Weiner                  * if the allocation error was handled gracefully (no
344349426420SJohannes Weiner                  * VM_FAULT_OOM), there is no need to kill anything.
344449426420SJohannes Weiner                  * Just clean up the OOM state peacefully.
344549426420SJohannes Weiner                  */
344649426420SJohannes Weiner                 if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM))
344749426420SJohannes Weiner                         mem_cgroup_oom_synchronize(false);
344849426420SJohannes Weiner 	}
34493812c8c8SJohannes Weiner 
3450519e5247SJohannes Weiner 	return ret;
3451519e5247SJohannes Weiner }
3452e1d6d01aSJesse Barnes EXPORT_SYMBOL_GPL(handle_mm_fault);
3453519e5247SJohannes Weiner 
34541da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED
34551da177e4SLinus Torvalds /*
34561da177e4SLinus Torvalds  * Allocate page upper directory.
3457872fec16SHugh Dickins  * We've already handled the fast-path in-line.
34581da177e4SLinus Torvalds  */
34591bb3630eSHugh Dickins int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
34601da177e4SLinus Torvalds {
3461c74df32cSHugh Dickins 	pud_t *new = pud_alloc_one(mm, address);
3462c74df32cSHugh Dickins 	if (!new)
34631bb3630eSHugh Dickins 		return -ENOMEM;
34641da177e4SLinus Torvalds 
3465362a61adSNick Piggin 	smp_wmb(); /* See comment in __pte_alloc */
3466362a61adSNick Piggin 
3467872fec16SHugh Dickins 	spin_lock(&mm->page_table_lock);
34681bb3630eSHugh Dickins 	if (pgd_present(*pgd))		/* Another has populated it */
34695e541973SBenjamin Herrenschmidt 		pud_free(mm, new);
34701bb3630eSHugh Dickins 	else
34711da177e4SLinus Torvalds 		pgd_populate(mm, pgd, new);
3472872fec16SHugh Dickins 	spin_unlock(&mm->page_table_lock);
34731bb3630eSHugh Dickins 	return 0;
34741da177e4SLinus Torvalds }
34751da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */
34761da177e4SLinus Torvalds 
34771da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED
34781da177e4SLinus Torvalds /*
34791da177e4SLinus Torvalds  * Allocate page middle directory.
3480872fec16SHugh Dickins  * We've already handled the fast-path in-line.
34811da177e4SLinus Torvalds  */
34821bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
34831da177e4SLinus Torvalds {
3484c74df32cSHugh Dickins 	pmd_t *new = pmd_alloc_one(mm, address);
3485c74df32cSHugh Dickins 	if (!new)
34861bb3630eSHugh Dickins 		return -ENOMEM;
34871da177e4SLinus Torvalds 
3488362a61adSNick Piggin 	smp_wmb(); /* See comment in __pte_alloc */
3489362a61adSNick Piggin 
3490872fec16SHugh Dickins 	spin_lock(&mm->page_table_lock);
34911da177e4SLinus Torvalds #ifndef __ARCH_HAS_4LEVEL_HACK
3492dc6c9a35SKirill A. Shutemov 	if (!pud_present(*pud)) {
3493dc6c9a35SKirill A. Shutemov 		mm_inc_nr_pmds(mm);
34941da177e4SLinus Torvalds 		pud_populate(mm, pud, new);
3495dc6c9a35SKirill A. Shutemov 	} else	/* Another has populated it */
34965e541973SBenjamin Herrenschmidt 		pmd_free(mm, new);
3497dc6c9a35SKirill A. Shutemov #else
3498dc6c9a35SKirill A. Shutemov 	if (!pgd_present(*pud)) {
3499dc6c9a35SKirill A. Shutemov 		mm_inc_nr_pmds(mm);
35001da177e4SLinus Torvalds 		pgd_populate(mm, pud, new);
3501dc6c9a35SKirill A. Shutemov 	} else /* Another has populated it */
3502dc6c9a35SKirill A. Shutemov 		pmd_free(mm, new);
35031da177e4SLinus Torvalds #endif /* __ARCH_HAS_4LEVEL_HACK */
3504872fec16SHugh Dickins 	spin_unlock(&mm->page_table_lock);
35051bb3630eSHugh Dickins 	return 0;
35061da177e4SLinus Torvalds }
35071da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */
35081da177e4SLinus Torvalds 
35091b36ba81SNamhyung Kim static int __follow_pte(struct mm_struct *mm, unsigned long address,
3510f8ad0f49SJohannes Weiner 		pte_t **ptepp, spinlock_t **ptlp)
3511f8ad0f49SJohannes Weiner {
3512f8ad0f49SJohannes Weiner 	pgd_t *pgd;
3513f8ad0f49SJohannes Weiner 	pud_t *pud;
3514f8ad0f49SJohannes Weiner 	pmd_t *pmd;
3515f8ad0f49SJohannes Weiner 	pte_t *ptep;
3516f8ad0f49SJohannes Weiner 
3517f8ad0f49SJohannes Weiner 	pgd = pgd_offset(mm, address);
3518f8ad0f49SJohannes Weiner 	if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
3519f8ad0f49SJohannes Weiner 		goto out;
3520f8ad0f49SJohannes Weiner 
3521f8ad0f49SJohannes Weiner 	pud = pud_offset(pgd, address);
3522f8ad0f49SJohannes Weiner 	if (pud_none(*pud) || unlikely(pud_bad(*pud)))
3523f8ad0f49SJohannes Weiner 		goto out;
3524f8ad0f49SJohannes Weiner 
3525f8ad0f49SJohannes Weiner 	pmd = pmd_offset(pud, address);
3526f66055abSAndrea Arcangeli 	VM_BUG_ON(pmd_trans_huge(*pmd));
3527f8ad0f49SJohannes Weiner 	if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
3528f8ad0f49SJohannes Weiner 		goto out;
3529f8ad0f49SJohannes Weiner 
3530f8ad0f49SJohannes Weiner 	/* We cannot handle huge page PFN maps. Luckily they don't exist. */
3531f8ad0f49SJohannes Weiner 	if (pmd_huge(*pmd))
3532f8ad0f49SJohannes Weiner 		goto out;
3533f8ad0f49SJohannes Weiner 
3534f8ad0f49SJohannes Weiner 	ptep = pte_offset_map_lock(mm, pmd, address, ptlp);
3535f8ad0f49SJohannes Weiner 	if (!ptep)
3536f8ad0f49SJohannes Weiner 		goto out;
3537f8ad0f49SJohannes Weiner 	if (!pte_present(*ptep))
3538f8ad0f49SJohannes Weiner 		goto unlock;
3539f8ad0f49SJohannes Weiner 	*ptepp = ptep;
3540f8ad0f49SJohannes Weiner 	return 0;
3541f8ad0f49SJohannes Weiner unlock:
3542f8ad0f49SJohannes Weiner 	pte_unmap_unlock(ptep, *ptlp);
3543f8ad0f49SJohannes Weiner out:
3544f8ad0f49SJohannes Weiner 	return -EINVAL;
3545f8ad0f49SJohannes Weiner }
3546f8ad0f49SJohannes Weiner 
35471b36ba81SNamhyung Kim static inline int follow_pte(struct mm_struct *mm, unsigned long address,
35481b36ba81SNamhyung Kim 			     pte_t **ptepp, spinlock_t **ptlp)
35491b36ba81SNamhyung Kim {
35501b36ba81SNamhyung Kim 	int res;
35511b36ba81SNamhyung Kim 
35521b36ba81SNamhyung Kim 	/* (void) is needed to make gcc happy */
35531b36ba81SNamhyung Kim 	(void) __cond_lock(*ptlp,
35541b36ba81SNamhyung Kim 			   !(res = __follow_pte(mm, address, ptepp, ptlp)));
35551b36ba81SNamhyung Kim 	return res;
35561b36ba81SNamhyung Kim }
35571b36ba81SNamhyung Kim 
35583b6748e2SJohannes Weiner /**
35593b6748e2SJohannes Weiner  * follow_pfn - look up PFN at a user virtual address
35603b6748e2SJohannes Weiner  * @vma: memory mapping
35613b6748e2SJohannes Weiner  * @address: user virtual address
35623b6748e2SJohannes Weiner  * @pfn: location to store found PFN
35633b6748e2SJohannes Weiner  *
35643b6748e2SJohannes Weiner  * Only IO mappings and raw PFN mappings are allowed.
35653b6748e2SJohannes Weiner  *
35663b6748e2SJohannes Weiner  * Returns zero and the pfn at @pfn on success, -ve otherwise.
35673b6748e2SJohannes Weiner  */
35683b6748e2SJohannes Weiner int follow_pfn(struct vm_area_struct *vma, unsigned long address,
35693b6748e2SJohannes Weiner 	unsigned long *pfn)
35703b6748e2SJohannes Weiner {
35713b6748e2SJohannes Weiner 	int ret = -EINVAL;
35723b6748e2SJohannes Weiner 	spinlock_t *ptl;
35733b6748e2SJohannes Weiner 	pte_t *ptep;
35743b6748e2SJohannes Weiner 
35753b6748e2SJohannes Weiner 	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
35763b6748e2SJohannes Weiner 		return ret;
35773b6748e2SJohannes Weiner 
35783b6748e2SJohannes Weiner 	ret = follow_pte(vma->vm_mm, address, &ptep, &ptl);
35793b6748e2SJohannes Weiner 	if (ret)
35803b6748e2SJohannes Weiner 		return ret;
35813b6748e2SJohannes Weiner 	*pfn = pte_pfn(*ptep);
35823b6748e2SJohannes Weiner 	pte_unmap_unlock(ptep, ptl);
35833b6748e2SJohannes Weiner 	return 0;
35843b6748e2SJohannes Weiner }
35853b6748e2SJohannes Weiner EXPORT_SYMBOL(follow_pfn);
35863b6748e2SJohannes Weiner 
358728b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT
3588d87fe660Svenkatesh.pallipadi@intel.com int follow_phys(struct vm_area_struct *vma,
358928b2ee20SRik van Riel 		unsigned long address, unsigned int flags,
3590d87fe660Svenkatesh.pallipadi@intel.com 		unsigned long *prot, resource_size_t *phys)
359128b2ee20SRik van Riel {
359203668a4dSJohannes Weiner 	int ret = -EINVAL;
359328b2ee20SRik van Riel 	pte_t *ptep, pte;
359428b2ee20SRik van Riel 	spinlock_t *ptl;
359528b2ee20SRik van Riel 
3596d87fe660Svenkatesh.pallipadi@intel.com 	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
3597d87fe660Svenkatesh.pallipadi@intel.com 		goto out;
359828b2ee20SRik van Riel 
359903668a4dSJohannes Weiner 	if (follow_pte(vma->vm_mm, address, &ptep, &ptl))
3600d87fe660Svenkatesh.pallipadi@intel.com 		goto out;
360128b2ee20SRik van Riel 	pte = *ptep;
360203668a4dSJohannes Weiner 
360328b2ee20SRik van Riel 	if ((flags & FOLL_WRITE) && !pte_write(pte))
360428b2ee20SRik van Riel 		goto unlock;
360528b2ee20SRik van Riel 
360628b2ee20SRik van Riel 	*prot = pgprot_val(pte_pgprot(pte));
360703668a4dSJohannes Weiner 	*phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT;
360828b2ee20SRik van Riel 
360903668a4dSJohannes Weiner 	ret = 0;
361028b2ee20SRik van Riel unlock:
361128b2ee20SRik van Riel 	pte_unmap_unlock(ptep, ptl);
361228b2ee20SRik van Riel out:
3613d87fe660Svenkatesh.pallipadi@intel.com 	return ret;
361428b2ee20SRik van Riel }
361528b2ee20SRik van Riel 
361628b2ee20SRik van Riel int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
361728b2ee20SRik van Riel 			void *buf, int len, int write)
361828b2ee20SRik van Riel {
361928b2ee20SRik van Riel 	resource_size_t phys_addr;
362028b2ee20SRik van Riel 	unsigned long prot = 0;
36212bc7273bSKOSAKI Motohiro 	void __iomem *maddr;
362228b2ee20SRik van Riel 	int offset = addr & (PAGE_SIZE-1);
362328b2ee20SRik van Riel 
3624d87fe660Svenkatesh.pallipadi@intel.com 	if (follow_phys(vma, addr, write, &prot, &phys_addr))
362528b2ee20SRik van Riel 		return -EINVAL;
362628b2ee20SRik van Riel 
36279cb12d7bSGrazvydas Ignotas 	maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot);
362828b2ee20SRik van Riel 	if (write)
362928b2ee20SRik van Riel 		memcpy_toio(maddr + offset, buf, len);
363028b2ee20SRik van Riel 	else
363128b2ee20SRik van Riel 		memcpy_fromio(buf, maddr + offset, len);
363228b2ee20SRik van Riel 	iounmap(maddr);
363328b2ee20SRik van Riel 
363428b2ee20SRik van Riel 	return len;
363528b2ee20SRik van Riel }
36365a73633eSUwe Kleine-König EXPORT_SYMBOL_GPL(generic_access_phys);
363728b2ee20SRik van Riel #endif
363828b2ee20SRik van Riel 
36390ec76a11SDavid Howells /*
3640206cb636SStephen Wilson  * Access another process' address space as given in mm.  If non-NULL, use the
3641206cb636SStephen Wilson  * given task for page fault accounting.
36420ec76a11SDavid Howells  */
3643206cb636SStephen Wilson static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
3644206cb636SStephen Wilson 		unsigned long addr, void *buf, int len, int write)
36450ec76a11SDavid Howells {
36460ec76a11SDavid Howells 	struct vm_area_struct *vma;
36470ec76a11SDavid Howells 	void *old_buf = buf;
36480ec76a11SDavid Howells 
36490ec76a11SDavid Howells 	down_read(&mm->mmap_sem);
3650183ff22bSSimon Arlott 	/* ignore errors, just check how much was successfully transferred */
36510ec76a11SDavid Howells 	while (len) {
36520ec76a11SDavid Howells 		int bytes, ret, offset;
36530ec76a11SDavid Howells 		void *maddr;
365428b2ee20SRik van Riel 		struct page *page = NULL;
36550ec76a11SDavid Howells 
36560ec76a11SDavid Howells 		ret = get_user_pages(tsk, mm, addr, 1,
36570ec76a11SDavid Howells 				write, 1, &page, &vma);
365828b2ee20SRik van Riel 		if (ret <= 0) {
3659dbffcd03SRik van Riel #ifndef CONFIG_HAVE_IOREMAP_PROT
3660dbffcd03SRik van Riel 			break;
3661dbffcd03SRik van Riel #else
366228b2ee20SRik van Riel 			/*
366328b2ee20SRik van Riel 			 * Check if this is a VM_IO | VM_PFNMAP VMA, which
366428b2ee20SRik van Riel 			 * we can access using slightly different code.
366528b2ee20SRik van Riel 			 */
366628b2ee20SRik van Riel 			vma = find_vma(mm, addr);
3667fe936dfcSMichael Ellerman 			if (!vma || vma->vm_start > addr)
36680ec76a11SDavid Howells 				break;
366928b2ee20SRik van Riel 			if (vma->vm_ops && vma->vm_ops->access)
367028b2ee20SRik van Riel 				ret = vma->vm_ops->access(vma, addr, buf,
367128b2ee20SRik van Riel 							  len, write);
367228b2ee20SRik van Riel 			if (ret <= 0)
367328b2ee20SRik van Riel 				break;
367428b2ee20SRik van Riel 			bytes = ret;
3675dbffcd03SRik van Riel #endif
367628b2ee20SRik van Riel 		} else {
36770ec76a11SDavid Howells 			bytes = len;
36780ec76a11SDavid Howells 			offset = addr & (PAGE_SIZE-1);
36790ec76a11SDavid Howells 			if (bytes > PAGE_SIZE-offset)
36800ec76a11SDavid Howells 				bytes = PAGE_SIZE-offset;
36810ec76a11SDavid Howells 
36820ec76a11SDavid Howells 			maddr = kmap(page);
36830ec76a11SDavid Howells 			if (write) {
36840ec76a11SDavid Howells 				copy_to_user_page(vma, page, addr,
36850ec76a11SDavid Howells 						  maddr + offset, buf, bytes);
36860ec76a11SDavid Howells 				set_page_dirty_lock(page);
36870ec76a11SDavid Howells 			} else {
36880ec76a11SDavid Howells 				copy_from_user_page(vma, page, addr,
36890ec76a11SDavid Howells 						    buf, maddr + offset, bytes);
36900ec76a11SDavid Howells 			}
36910ec76a11SDavid Howells 			kunmap(page);
36920ec76a11SDavid Howells 			page_cache_release(page);
369328b2ee20SRik van Riel 		}
36940ec76a11SDavid Howells 		len -= bytes;
36950ec76a11SDavid Howells 		buf += bytes;
36960ec76a11SDavid Howells 		addr += bytes;
36970ec76a11SDavid Howells 	}
36980ec76a11SDavid Howells 	up_read(&mm->mmap_sem);
36990ec76a11SDavid Howells 
37000ec76a11SDavid Howells 	return buf - old_buf;
37010ec76a11SDavid Howells }
370203252919SAndi Kleen 
37035ddd36b9SStephen Wilson /**
3704ae91dbfcSRandy Dunlap  * access_remote_vm - access another process' address space
37055ddd36b9SStephen Wilson  * @mm:		the mm_struct of the target address space
37065ddd36b9SStephen Wilson  * @addr:	start address to access
37075ddd36b9SStephen Wilson  * @buf:	source or destination buffer
37085ddd36b9SStephen Wilson  * @len:	number of bytes to transfer
37095ddd36b9SStephen Wilson  * @write:	whether the access is a write
37105ddd36b9SStephen Wilson  *
37115ddd36b9SStephen Wilson  * The caller must hold a reference on @mm.
37125ddd36b9SStephen Wilson  */
37135ddd36b9SStephen Wilson int access_remote_vm(struct mm_struct *mm, unsigned long addr,
37145ddd36b9SStephen Wilson 		void *buf, int len, int write)
37155ddd36b9SStephen Wilson {
37165ddd36b9SStephen Wilson 	return __access_remote_vm(NULL, mm, addr, buf, len, write);
37175ddd36b9SStephen Wilson }
37185ddd36b9SStephen Wilson 
371903252919SAndi Kleen /*
3720206cb636SStephen Wilson  * Access another process' address space.
3721206cb636SStephen Wilson  * Source/target buffer must be kernel space,
3722206cb636SStephen Wilson  * Do not walk the page table directly, use get_user_pages
3723206cb636SStephen Wilson  */
3724206cb636SStephen Wilson int access_process_vm(struct task_struct *tsk, unsigned long addr,
3725206cb636SStephen Wilson 		void *buf, int len, int write)
3726206cb636SStephen Wilson {
3727206cb636SStephen Wilson 	struct mm_struct *mm;
3728206cb636SStephen Wilson 	int ret;
3729206cb636SStephen Wilson 
3730206cb636SStephen Wilson 	mm = get_task_mm(tsk);
3731206cb636SStephen Wilson 	if (!mm)
3732206cb636SStephen Wilson 		return 0;
3733206cb636SStephen Wilson 
3734206cb636SStephen Wilson 	ret = __access_remote_vm(tsk, mm, addr, buf, len, write);
3735206cb636SStephen Wilson 	mmput(mm);
3736206cb636SStephen Wilson 
3737206cb636SStephen Wilson 	return ret;
3738206cb636SStephen Wilson }
3739206cb636SStephen Wilson 
374003252919SAndi Kleen /*
374103252919SAndi Kleen  * Print the name of a VMA.
374203252919SAndi Kleen  */
374303252919SAndi Kleen void print_vma_addr(char *prefix, unsigned long ip)
374403252919SAndi Kleen {
374503252919SAndi Kleen 	struct mm_struct *mm = current->mm;
374603252919SAndi Kleen 	struct vm_area_struct *vma;
374703252919SAndi Kleen 
3748e8bff74aSIngo Molnar 	/*
3749e8bff74aSIngo Molnar 	 * Do not print if we are in atomic
3750e8bff74aSIngo Molnar 	 * contexts (in exception stacks, etc.):
3751e8bff74aSIngo Molnar 	 */
3752e8bff74aSIngo Molnar 	if (preempt_count())
3753e8bff74aSIngo Molnar 		return;
3754e8bff74aSIngo Molnar 
375503252919SAndi Kleen 	down_read(&mm->mmap_sem);
375603252919SAndi Kleen 	vma = find_vma(mm, ip);
375703252919SAndi Kleen 	if (vma && vma->vm_file) {
375803252919SAndi Kleen 		struct file *f = vma->vm_file;
375903252919SAndi Kleen 		char *buf = (char *)__get_free_page(GFP_KERNEL);
376003252919SAndi Kleen 		if (buf) {
37612fbc57c5SAndy Shevchenko 			char *p;
376203252919SAndi Kleen 
37639bf39ab2SMiklos Szeredi 			p = file_path(f, buf, PAGE_SIZE);
376403252919SAndi Kleen 			if (IS_ERR(p))
376503252919SAndi Kleen 				p = "?";
37662fbc57c5SAndy Shevchenko 			printk("%s%s[%lx+%lx]", prefix, kbasename(p),
376703252919SAndi Kleen 					vma->vm_start,
376803252919SAndi Kleen 					vma->vm_end - vma->vm_start);
376903252919SAndi Kleen 			free_page((unsigned long)buf);
377003252919SAndi Kleen 		}
377103252919SAndi Kleen 	}
377251a07e50SJeff Liu 	up_read(&mm->mmap_sem);
377303252919SAndi Kleen }
37743ee1afa3SNick Piggin 
3775662bbcb2SMichael S. Tsirkin #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)
37769ec23531SDavid Hildenbrand void __might_fault(const char *file, int line)
37773ee1afa3SNick Piggin {
377895156f00SPeter Zijlstra 	/*
377995156f00SPeter Zijlstra 	 * Some code (nfs/sunrpc) uses socket ops on kernel memory while
378095156f00SPeter Zijlstra 	 * holding the mmap_sem, this is safe because kernel memory doesn't
378195156f00SPeter Zijlstra 	 * get paged out, therefore we'll never actually fault, and the
378295156f00SPeter Zijlstra 	 * below annotations will generate false positives.
378395156f00SPeter Zijlstra 	 */
378495156f00SPeter Zijlstra 	if (segment_eq(get_fs(), KERNEL_DS))
378595156f00SPeter Zijlstra 		return;
37869ec23531SDavid Hildenbrand 	if (pagefault_disabled())
3787662bbcb2SMichael S. Tsirkin 		return;
37889ec23531SDavid Hildenbrand 	__might_sleep(file, line, 0);
37899ec23531SDavid Hildenbrand #if defined(CONFIG_DEBUG_ATOMIC_SLEEP)
3790662bbcb2SMichael S. Tsirkin 	if (current->mm)
37913ee1afa3SNick Piggin 		might_lock_read(&current->mm->mmap_sem);
37929ec23531SDavid Hildenbrand #endif
37933ee1afa3SNick Piggin }
37949ec23531SDavid Hildenbrand EXPORT_SYMBOL(__might_fault);
37953ee1afa3SNick Piggin #endif
379647ad8475SAndrea Arcangeli 
379747ad8475SAndrea Arcangeli #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
379847ad8475SAndrea Arcangeli static void clear_gigantic_page(struct page *page,
379947ad8475SAndrea Arcangeli 				unsigned long addr,
380047ad8475SAndrea Arcangeli 				unsigned int pages_per_huge_page)
380147ad8475SAndrea Arcangeli {
380247ad8475SAndrea Arcangeli 	int i;
380347ad8475SAndrea Arcangeli 	struct page *p = page;
380447ad8475SAndrea Arcangeli 
380547ad8475SAndrea Arcangeli 	might_sleep();
380647ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page;
380747ad8475SAndrea Arcangeli 	     i++, p = mem_map_next(p, page, i)) {
380847ad8475SAndrea Arcangeli 		cond_resched();
380947ad8475SAndrea Arcangeli 		clear_user_highpage(p, addr + i * PAGE_SIZE);
381047ad8475SAndrea Arcangeli 	}
381147ad8475SAndrea Arcangeli }
381247ad8475SAndrea Arcangeli void clear_huge_page(struct page *page,
381347ad8475SAndrea Arcangeli 		     unsigned long addr, unsigned int pages_per_huge_page)
381447ad8475SAndrea Arcangeli {
381547ad8475SAndrea Arcangeli 	int i;
381647ad8475SAndrea Arcangeli 
381747ad8475SAndrea Arcangeli 	if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
381847ad8475SAndrea Arcangeli 		clear_gigantic_page(page, addr, pages_per_huge_page);
381947ad8475SAndrea Arcangeli 		return;
382047ad8475SAndrea Arcangeli 	}
382147ad8475SAndrea Arcangeli 
382247ad8475SAndrea Arcangeli 	might_sleep();
382347ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page; i++) {
382447ad8475SAndrea Arcangeli 		cond_resched();
382547ad8475SAndrea Arcangeli 		clear_user_highpage(page + i, addr + i * PAGE_SIZE);
382647ad8475SAndrea Arcangeli 	}
382747ad8475SAndrea Arcangeli }
382847ad8475SAndrea Arcangeli 
382947ad8475SAndrea Arcangeli static void copy_user_gigantic_page(struct page *dst, struct page *src,
383047ad8475SAndrea Arcangeli 				    unsigned long addr,
383147ad8475SAndrea Arcangeli 				    struct vm_area_struct *vma,
383247ad8475SAndrea Arcangeli 				    unsigned int pages_per_huge_page)
383347ad8475SAndrea Arcangeli {
383447ad8475SAndrea Arcangeli 	int i;
383547ad8475SAndrea Arcangeli 	struct page *dst_base = dst;
383647ad8475SAndrea Arcangeli 	struct page *src_base = src;
383747ad8475SAndrea Arcangeli 
383847ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page; ) {
383947ad8475SAndrea Arcangeli 		cond_resched();
384047ad8475SAndrea Arcangeli 		copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma);
384147ad8475SAndrea Arcangeli 
384247ad8475SAndrea Arcangeli 		i++;
384347ad8475SAndrea Arcangeli 		dst = mem_map_next(dst, dst_base, i);
384447ad8475SAndrea Arcangeli 		src = mem_map_next(src, src_base, i);
384547ad8475SAndrea Arcangeli 	}
384647ad8475SAndrea Arcangeli }
384747ad8475SAndrea Arcangeli 
384847ad8475SAndrea Arcangeli void copy_user_huge_page(struct page *dst, struct page *src,
384947ad8475SAndrea Arcangeli 			 unsigned long addr, struct vm_area_struct *vma,
385047ad8475SAndrea Arcangeli 			 unsigned int pages_per_huge_page)
385147ad8475SAndrea Arcangeli {
385247ad8475SAndrea Arcangeli 	int i;
385347ad8475SAndrea Arcangeli 
385447ad8475SAndrea Arcangeli 	if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
385547ad8475SAndrea Arcangeli 		copy_user_gigantic_page(dst, src, addr, vma,
385647ad8475SAndrea Arcangeli 					pages_per_huge_page);
385747ad8475SAndrea Arcangeli 		return;
385847ad8475SAndrea Arcangeli 	}
385947ad8475SAndrea Arcangeli 
386047ad8475SAndrea Arcangeli 	might_sleep();
386147ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page; i++) {
386247ad8475SAndrea Arcangeli 		cond_resched();
386347ad8475SAndrea Arcangeli 		copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
386447ad8475SAndrea Arcangeli 	}
386547ad8475SAndrea Arcangeli }
386647ad8475SAndrea Arcangeli #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */
386749076ec2SKirill A. Shutemov 
386840b64acdSOlof Johansson #if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS
3869b35f1819SKirill A. Shutemov 
3870b35f1819SKirill A. Shutemov static struct kmem_cache *page_ptl_cachep;
3871b35f1819SKirill A. Shutemov 
3872b35f1819SKirill A. Shutemov void __init ptlock_cache_init(void)
3873b35f1819SKirill A. Shutemov {
3874b35f1819SKirill A. Shutemov 	page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0,
3875b35f1819SKirill A. Shutemov 			SLAB_PANIC, NULL);
3876b35f1819SKirill A. Shutemov }
3877b35f1819SKirill A. Shutemov 
3878539edb58SPeter Zijlstra bool ptlock_alloc(struct page *page)
387949076ec2SKirill A. Shutemov {
388049076ec2SKirill A. Shutemov 	spinlock_t *ptl;
388149076ec2SKirill A. Shutemov 
3882b35f1819SKirill A. Shutemov 	ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL);
388349076ec2SKirill A. Shutemov 	if (!ptl)
388449076ec2SKirill A. Shutemov 		return false;
3885539edb58SPeter Zijlstra 	page->ptl = ptl;
388649076ec2SKirill A. Shutemov 	return true;
388749076ec2SKirill A. Shutemov }
388849076ec2SKirill A. Shutemov 
3889539edb58SPeter Zijlstra void ptlock_free(struct page *page)
389049076ec2SKirill A. Shutemov {
3891b35f1819SKirill A. Shutemov 	kmem_cache_free(page_ptl_cachep, page->ptl);
389249076ec2SKirill A. Shutemov }
389349076ec2SKirill A. Shutemov #endif
3894