xref: /linux/mm/memory.c (revision e303297e6c3a7b847c4731eb14006ca6b435ecca)
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>
501da177e4SLinus Torvalds #include <linux/module.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>
601da177e4SLinus Torvalds 
616952b61dSAlexey Dobriyan #include <asm/io.h>
621da177e4SLinus Torvalds #include <asm/pgalloc.h>
631da177e4SLinus Torvalds #include <asm/uaccess.h>
641da177e4SLinus Torvalds #include <asm/tlb.h>
651da177e4SLinus Torvalds #include <asm/tlbflush.h>
661da177e4SLinus Torvalds #include <asm/pgtable.h>
671da177e4SLinus Torvalds 
6842b77728SJan Beulich #include "internal.h"
6942b77728SJan Beulich 
70d41dee36SAndy Whitcroft #ifndef CONFIG_NEED_MULTIPLE_NODES
711da177e4SLinus Torvalds /* use the per-pgdat data instead for discontigmem - mbligh */
721da177e4SLinus Torvalds unsigned long max_mapnr;
731da177e4SLinus Torvalds struct page *mem_map;
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds EXPORT_SYMBOL(max_mapnr);
761da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map);
771da177e4SLinus Torvalds #endif
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds unsigned long num_physpages;
801da177e4SLinus Torvalds /*
811da177e4SLinus Torvalds  * A number of key systems in x86 including ioremap() rely on the assumption
821da177e4SLinus Torvalds  * that high_memory defines the upper bound on direct map memory, then end
831da177e4SLinus Torvalds  * of ZONE_NORMAL.  Under CONFIG_DISCONTIG this means that max_low_pfn and
841da177e4SLinus Torvalds  * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL
851da177e4SLinus Torvalds  * and ZONE_HIGHMEM.
861da177e4SLinus Torvalds  */
871da177e4SLinus Torvalds void * high_memory;
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds EXPORT_SYMBOL(num_physpages);
901da177e4SLinus Torvalds EXPORT_SYMBOL(high_memory);
911da177e4SLinus Torvalds 
9232a93233SIngo Molnar /*
9332a93233SIngo Molnar  * Randomize the address space (stacks, mmaps, brk, etc.).
9432a93233SIngo Molnar  *
9532a93233SIngo Molnar  * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization,
9632a93233SIngo Molnar  *   as ancient (libc5 based) binaries can segfault. )
9732a93233SIngo Molnar  */
9832a93233SIngo Molnar int randomize_va_space __read_mostly =
9932a93233SIngo Molnar #ifdef CONFIG_COMPAT_BRK
10032a93233SIngo Molnar 					1;
10132a93233SIngo Molnar #else
10232a93233SIngo Molnar 					2;
10332a93233SIngo Molnar #endif
104a62eaf15SAndi Kleen 
105a62eaf15SAndi Kleen static int __init disable_randmaps(char *s)
106a62eaf15SAndi Kleen {
107a62eaf15SAndi Kleen 	randomize_va_space = 0;
1089b41046cSOGAWA Hirofumi 	return 1;
109a62eaf15SAndi Kleen }
110a62eaf15SAndi Kleen __setup("norandmaps", disable_randmaps);
111a62eaf15SAndi Kleen 
11262eede62SHugh Dickins unsigned long zero_pfn __read_mostly;
11303f6462aSHugh Dickins unsigned long highest_memmap_pfn __read_mostly;
114a13ea5b7SHugh Dickins 
115a13ea5b7SHugh Dickins /*
116a13ea5b7SHugh Dickins  * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init()
117a13ea5b7SHugh Dickins  */
118a13ea5b7SHugh Dickins static int __init init_zero_pfn(void)
119a13ea5b7SHugh Dickins {
120a13ea5b7SHugh Dickins 	zero_pfn = page_to_pfn(ZERO_PAGE(0));
121a13ea5b7SHugh Dickins 	return 0;
122a13ea5b7SHugh Dickins }
123a13ea5b7SHugh Dickins core_initcall(init_zero_pfn);
124a62eaf15SAndi Kleen 
125d559db08SKAMEZAWA Hiroyuki 
12634e55232SKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING)
12734e55232SKAMEZAWA Hiroyuki 
128a3a2e76cSKAMEZAWA Hiroyuki static void __sync_task_rss_stat(struct task_struct *task, struct mm_struct *mm)
12934e55232SKAMEZAWA Hiroyuki {
13034e55232SKAMEZAWA Hiroyuki 	int i;
13134e55232SKAMEZAWA Hiroyuki 
13234e55232SKAMEZAWA Hiroyuki 	for (i = 0; i < NR_MM_COUNTERS; i++) {
13334e55232SKAMEZAWA Hiroyuki 		if (task->rss_stat.count[i]) {
13434e55232SKAMEZAWA Hiroyuki 			add_mm_counter(mm, i, task->rss_stat.count[i]);
13534e55232SKAMEZAWA Hiroyuki 			task->rss_stat.count[i] = 0;
13634e55232SKAMEZAWA Hiroyuki 		}
13734e55232SKAMEZAWA Hiroyuki 	}
13834e55232SKAMEZAWA Hiroyuki 	task->rss_stat.events = 0;
13934e55232SKAMEZAWA Hiroyuki }
14034e55232SKAMEZAWA Hiroyuki 
14134e55232SKAMEZAWA Hiroyuki static void add_mm_counter_fast(struct mm_struct *mm, int member, int val)
14234e55232SKAMEZAWA Hiroyuki {
14334e55232SKAMEZAWA Hiroyuki 	struct task_struct *task = current;
14434e55232SKAMEZAWA Hiroyuki 
14534e55232SKAMEZAWA Hiroyuki 	if (likely(task->mm == mm))
14634e55232SKAMEZAWA Hiroyuki 		task->rss_stat.count[member] += val;
14734e55232SKAMEZAWA Hiroyuki 	else
14834e55232SKAMEZAWA Hiroyuki 		add_mm_counter(mm, member, val);
14934e55232SKAMEZAWA Hiroyuki }
15034e55232SKAMEZAWA Hiroyuki #define inc_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, 1)
15134e55232SKAMEZAWA Hiroyuki #define dec_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, -1)
15234e55232SKAMEZAWA Hiroyuki 
15334e55232SKAMEZAWA Hiroyuki /* sync counter once per 64 page faults */
15434e55232SKAMEZAWA Hiroyuki #define TASK_RSS_EVENTS_THRESH	(64)
15534e55232SKAMEZAWA Hiroyuki static void check_sync_rss_stat(struct task_struct *task)
15634e55232SKAMEZAWA Hiroyuki {
15734e55232SKAMEZAWA Hiroyuki 	if (unlikely(task != current))
15834e55232SKAMEZAWA Hiroyuki 		return;
15934e55232SKAMEZAWA Hiroyuki 	if (unlikely(task->rss_stat.events++ > TASK_RSS_EVENTS_THRESH))
16034e55232SKAMEZAWA Hiroyuki 		__sync_task_rss_stat(task, task->mm);
16134e55232SKAMEZAWA Hiroyuki }
16234e55232SKAMEZAWA Hiroyuki 
16334e55232SKAMEZAWA Hiroyuki unsigned long get_mm_counter(struct mm_struct *mm, int member)
16434e55232SKAMEZAWA Hiroyuki {
16534e55232SKAMEZAWA Hiroyuki 	long val = 0;
16634e55232SKAMEZAWA Hiroyuki 
16734e55232SKAMEZAWA Hiroyuki 	/*
16834e55232SKAMEZAWA Hiroyuki 	 * Don't use task->mm here...for avoiding to use task_get_mm()..
16934e55232SKAMEZAWA Hiroyuki 	 * The caller must guarantee task->mm is not invalid.
17034e55232SKAMEZAWA Hiroyuki 	 */
17134e55232SKAMEZAWA Hiroyuki 	val = atomic_long_read(&mm->rss_stat.count[member]);
17234e55232SKAMEZAWA Hiroyuki 	/*
17334e55232SKAMEZAWA Hiroyuki 	 * counter is updated in asynchronous manner and may go to minus.
17434e55232SKAMEZAWA Hiroyuki 	 * But it's never be expected number for users.
17534e55232SKAMEZAWA Hiroyuki 	 */
17634e55232SKAMEZAWA Hiroyuki 	if (val < 0)
17734e55232SKAMEZAWA Hiroyuki 		return 0;
17834e55232SKAMEZAWA Hiroyuki 	return (unsigned long)val;
17934e55232SKAMEZAWA Hiroyuki }
18034e55232SKAMEZAWA Hiroyuki 
18134e55232SKAMEZAWA Hiroyuki void sync_mm_rss(struct task_struct *task, struct mm_struct *mm)
18234e55232SKAMEZAWA Hiroyuki {
18334e55232SKAMEZAWA Hiroyuki 	__sync_task_rss_stat(task, mm);
18434e55232SKAMEZAWA Hiroyuki }
18534e55232SKAMEZAWA Hiroyuki #else
18634e55232SKAMEZAWA Hiroyuki 
18734e55232SKAMEZAWA Hiroyuki #define inc_mm_counter_fast(mm, member) inc_mm_counter(mm, member)
18834e55232SKAMEZAWA Hiroyuki #define dec_mm_counter_fast(mm, member) dec_mm_counter(mm, member)
18934e55232SKAMEZAWA Hiroyuki 
19034e55232SKAMEZAWA Hiroyuki static void check_sync_rss_stat(struct task_struct *task)
19134e55232SKAMEZAWA Hiroyuki {
19234e55232SKAMEZAWA Hiroyuki }
19334e55232SKAMEZAWA Hiroyuki 
19434e55232SKAMEZAWA Hiroyuki #endif
19534e55232SKAMEZAWA Hiroyuki 
19626723911SPeter Zijlstra #ifdef CONFIG_HAVE_RCU_TABLE_FREE
19726723911SPeter Zijlstra 
19826723911SPeter Zijlstra /*
19926723911SPeter Zijlstra  * See the comment near struct mmu_table_batch.
20026723911SPeter Zijlstra  */
20126723911SPeter Zijlstra 
20226723911SPeter Zijlstra static void tlb_remove_table_smp_sync(void *arg)
20326723911SPeter Zijlstra {
20426723911SPeter Zijlstra 	/* Simply deliver the interrupt */
20526723911SPeter Zijlstra }
20626723911SPeter Zijlstra 
20726723911SPeter Zijlstra static void tlb_remove_table_one(void *table)
20826723911SPeter Zijlstra {
20926723911SPeter Zijlstra 	/*
21026723911SPeter Zijlstra 	 * This isn't an RCU grace period and hence the page-tables cannot be
21126723911SPeter Zijlstra 	 * assumed to be actually RCU-freed.
21226723911SPeter Zijlstra 	 *
21326723911SPeter Zijlstra 	 * It is however sufficient for software page-table walkers that rely on
21426723911SPeter Zijlstra 	 * IRQ disabling. See the comment near struct mmu_table_batch.
21526723911SPeter Zijlstra 	 */
21626723911SPeter Zijlstra 	smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
21726723911SPeter Zijlstra 	__tlb_remove_table(table);
21826723911SPeter Zijlstra }
21926723911SPeter Zijlstra 
22026723911SPeter Zijlstra static void tlb_remove_table_rcu(struct rcu_head *head)
22126723911SPeter Zijlstra {
22226723911SPeter Zijlstra 	struct mmu_table_batch *batch;
22326723911SPeter Zijlstra 	int i;
22426723911SPeter Zijlstra 
22526723911SPeter Zijlstra 	batch = container_of(head, struct mmu_table_batch, rcu);
22626723911SPeter Zijlstra 
22726723911SPeter Zijlstra 	for (i = 0; i < batch->nr; i++)
22826723911SPeter Zijlstra 		__tlb_remove_table(batch->tables[i]);
22926723911SPeter Zijlstra 
23026723911SPeter Zijlstra 	free_page((unsigned long)batch);
23126723911SPeter Zijlstra }
23226723911SPeter Zijlstra 
23326723911SPeter Zijlstra void tlb_table_flush(struct mmu_gather *tlb)
23426723911SPeter Zijlstra {
23526723911SPeter Zijlstra 	struct mmu_table_batch **batch = &tlb->batch;
23626723911SPeter Zijlstra 
23726723911SPeter Zijlstra 	if (*batch) {
23826723911SPeter Zijlstra 		call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
23926723911SPeter Zijlstra 		*batch = NULL;
24026723911SPeter Zijlstra 	}
24126723911SPeter Zijlstra }
24226723911SPeter Zijlstra 
24326723911SPeter Zijlstra void tlb_remove_table(struct mmu_gather *tlb, void *table)
24426723911SPeter Zijlstra {
24526723911SPeter Zijlstra 	struct mmu_table_batch **batch = &tlb->batch;
24626723911SPeter Zijlstra 
24726723911SPeter Zijlstra 	tlb->need_flush = 1;
24826723911SPeter Zijlstra 
24926723911SPeter Zijlstra 	/*
25026723911SPeter Zijlstra 	 * When there's less then two users of this mm there cannot be a
25126723911SPeter Zijlstra 	 * concurrent page-table walk.
25226723911SPeter Zijlstra 	 */
25326723911SPeter Zijlstra 	if (atomic_read(&tlb->mm->mm_users) < 2) {
25426723911SPeter Zijlstra 		__tlb_remove_table(table);
25526723911SPeter Zijlstra 		return;
25626723911SPeter Zijlstra 	}
25726723911SPeter Zijlstra 
25826723911SPeter Zijlstra 	if (*batch == NULL) {
25926723911SPeter Zijlstra 		*batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
26026723911SPeter Zijlstra 		if (*batch == NULL) {
26126723911SPeter Zijlstra 			tlb_remove_table_one(table);
26226723911SPeter Zijlstra 			return;
26326723911SPeter Zijlstra 		}
26426723911SPeter Zijlstra 		(*batch)->nr = 0;
26526723911SPeter Zijlstra 	}
26626723911SPeter Zijlstra 	(*batch)->tables[(*batch)->nr++] = table;
26726723911SPeter Zijlstra 	if ((*batch)->nr == MAX_TABLE_BATCH)
26826723911SPeter Zijlstra 		tlb_table_flush(tlb);
26926723911SPeter Zijlstra }
27026723911SPeter Zijlstra 
27126723911SPeter Zijlstra #endif
27226723911SPeter Zijlstra 
2731da177e4SLinus Torvalds /*
2741da177e4SLinus Torvalds  * If a p?d_bad entry is found while walking page tables, report
2751da177e4SLinus Torvalds  * the error, before resetting entry to p?d_none.  Usually (but
2761da177e4SLinus Torvalds  * very seldom) called out from the p?d_none_or_clear_bad macros.
2771da177e4SLinus Torvalds  */
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds void pgd_clear_bad(pgd_t *pgd)
2801da177e4SLinus Torvalds {
2811da177e4SLinus Torvalds 	pgd_ERROR(*pgd);
2821da177e4SLinus Torvalds 	pgd_clear(pgd);
2831da177e4SLinus Torvalds }
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds void pud_clear_bad(pud_t *pud)
2861da177e4SLinus Torvalds {
2871da177e4SLinus Torvalds 	pud_ERROR(*pud);
2881da177e4SLinus Torvalds 	pud_clear(pud);
2891da177e4SLinus Torvalds }
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds void pmd_clear_bad(pmd_t *pmd)
2921da177e4SLinus Torvalds {
2931da177e4SLinus Torvalds 	pmd_ERROR(*pmd);
2941da177e4SLinus Torvalds 	pmd_clear(pmd);
2951da177e4SLinus Torvalds }
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds /*
2981da177e4SLinus Torvalds  * Note: this doesn't free the actual pages themselves. That
2991da177e4SLinus Torvalds  * has been handled earlier when unmapping all the memory regions.
3001da177e4SLinus Torvalds  */
3019e1b32caSBenjamin Herrenschmidt static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd,
3029e1b32caSBenjamin Herrenschmidt 			   unsigned long addr)
3031da177e4SLinus Torvalds {
3042f569afdSMartin Schwidefsky 	pgtable_t token = pmd_pgtable(*pmd);
3051da177e4SLinus Torvalds 	pmd_clear(pmd);
3069e1b32caSBenjamin Herrenschmidt 	pte_free_tlb(tlb, token, addr);
3071da177e4SLinus Torvalds 	tlb->mm->nr_ptes--;
3081da177e4SLinus Torvalds }
3091da177e4SLinus Torvalds 
310e0da382cSHugh Dickins static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
311e0da382cSHugh Dickins 				unsigned long addr, unsigned long end,
312e0da382cSHugh Dickins 				unsigned long floor, unsigned long ceiling)
3131da177e4SLinus Torvalds {
3141da177e4SLinus Torvalds 	pmd_t *pmd;
3151da177e4SLinus Torvalds 	unsigned long next;
316e0da382cSHugh Dickins 	unsigned long start;
3171da177e4SLinus Torvalds 
318e0da382cSHugh Dickins 	start = addr;
3191da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
3201da177e4SLinus Torvalds 	do {
3211da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
3221da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(pmd))
3231da177e4SLinus Torvalds 			continue;
3249e1b32caSBenjamin Herrenschmidt 		free_pte_range(tlb, pmd, addr);
3251da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
3261da177e4SLinus Torvalds 
327e0da382cSHugh Dickins 	start &= PUD_MASK;
328e0da382cSHugh Dickins 	if (start < floor)
329e0da382cSHugh Dickins 		return;
330e0da382cSHugh Dickins 	if (ceiling) {
331e0da382cSHugh Dickins 		ceiling &= PUD_MASK;
332e0da382cSHugh Dickins 		if (!ceiling)
333e0da382cSHugh Dickins 			return;
3341da177e4SLinus Torvalds 	}
335e0da382cSHugh Dickins 	if (end - 1 > ceiling - 1)
336e0da382cSHugh Dickins 		return;
337e0da382cSHugh Dickins 
338e0da382cSHugh Dickins 	pmd = pmd_offset(pud, start);
339e0da382cSHugh Dickins 	pud_clear(pud);
3409e1b32caSBenjamin Herrenschmidt 	pmd_free_tlb(tlb, pmd, start);
3411da177e4SLinus Torvalds }
3421da177e4SLinus Torvalds 
343e0da382cSHugh Dickins static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
344e0da382cSHugh Dickins 				unsigned long addr, unsigned long end,
345e0da382cSHugh Dickins 				unsigned long floor, unsigned long ceiling)
3461da177e4SLinus Torvalds {
3471da177e4SLinus Torvalds 	pud_t *pud;
3481da177e4SLinus Torvalds 	unsigned long next;
349e0da382cSHugh Dickins 	unsigned long start;
3501da177e4SLinus Torvalds 
351e0da382cSHugh Dickins 	start = addr;
3521da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
3531da177e4SLinus Torvalds 	do {
3541da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
3551da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
3561da177e4SLinus Torvalds 			continue;
357e0da382cSHugh Dickins 		free_pmd_range(tlb, pud, addr, next, floor, ceiling);
3581da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
3591da177e4SLinus Torvalds 
360e0da382cSHugh Dickins 	start &= PGDIR_MASK;
361e0da382cSHugh Dickins 	if (start < floor)
362e0da382cSHugh Dickins 		return;
363e0da382cSHugh Dickins 	if (ceiling) {
364e0da382cSHugh Dickins 		ceiling &= PGDIR_MASK;
365e0da382cSHugh Dickins 		if (!ceiling)
366e0da382cSHugh Dickins 			return;
3671da177e4SLinus Torvalds 	}
368e0da382cSHugh Dickins 	if (end - 1 > ceiling - 1)
369e0da382cSHugh Dickins 		return;
370e0da382cSHugh Dickins 
371e0da382cSHugh Dickins 	pud = pud_offset(pgd, start);
372e0da382cSHugh Dickins 	pgd_clear(pgd);
3739e1b32caSBenjamin Herrenschmidt 	pud_free_tlb(tlb, pud, start);
3741da177e4SLinus Torvalds }
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds /*
377e0da382cSHugh Dickins  * This function frees user-level page tables of a process.
378e0da382cSHugh Dickins  *
3791da177e4SLinus Torvalds  * Must be called with pagetable lock held.
3801da177e4SLinus Torvalds  */
38142b77728SJan Beulich void free_pgd_range(struct mmu_gather *tlb,
382e0da382cSHugh Dickins 			unsigned long addr, unsigned long end,
383e0da382cSHugh Dickins 			unsigned long floor, unsigned long ceiling)
3841da177e4SLinus Torvalds {
3851da177e4SLinus Torvalds 	pgd_t *pgd;
3861da177e4SLinus Torvalds 	unsigned long next;
3871da177e4SLinus Torvalds 
388e0da382cSHugh Dickins 	/*
389e0da382cSHugh Dickins 	 * The next few lines have given us lots of grief...
390e0da382cSHugh Dickins 	 *
391e0da382cSHugh Dickins 	 * Why are we testing PMD* at this top level?  Because often
392e0da382cSHugh Dickins 	 * there will be no work to do at all, and we'd prefer not to
393e0da382cSHugh Dickins 	 * go all the way down to the bottom just to discover that.
394e0da382cSHugh Dickins 	 *
395e0da382cSHugh Dickins 	 * Why all these "- 1"s?  Because 0 represents both the bottom
396e0da382cSHugh Dickins 	 * of the address space and the top of it (using -1 for the
397e0da382cSHugh Dickins 	 * top wouldn't help much: the masks would do the wrong thing).
398e0da382cSHugh Dickins 	 * The rule is that addr 0 and floor 0 refer to the bottom of
399e0da382cSHugh Dickins 	 * the address space, but end 0 and ceiling 0 refer to the top
400e0da382cSHugh Dickins 	 * Comparisons need to use "end - 1" and "ceiling - 1" (though
401e0da382cSHugh Dickins 	 * that end 0 case should be mythical).
402e0da382cSHugh Dickins 	 *
403e0da382cSHugh Dickins 	 * Wherever addr is brought up or ceiling brought down, we must
404e0da382cSHugh Dickins 	 * be careful to reject "the opposite 0" before it confuses the
405e0da382cSHugh Dickins 	 * subsequent tests.  But what about where end is brought down
406e0da382cSHugh Dickins 	 * by PMD_SIZE below? no, end can't go down to 0 there.
407e0da382cSHugh Dickins 	 *
408e0da382cSHugh Dickins 	 * Whereas we round start (addr) and ceiling down, by different
409e0da382cSHugh Dickins 	 * masks at different levels, in order to test whether a table
410e0da382cSHugh Dickins 	 * now has no other vmas using it, so can be freed, we don't
411e0da382cSHugh Dickins 	 * bother to round floor or end up - the tests don't need that.
412e0da382cSHugh Dickins 	 */
413e0da382cSHugh Dickins 
414e0da382cSHugh Dickins 	addr &= PMD_MASK;
415e0da382cSHugh Dickins 	if (addr < floor) {
416e0da382cSHugh Dickins 		addr += PMD_SIZE;
417e0da382cSHugh Dickins 		if (!addr)
418e0da382cSHugh Dickins 			return;
419e0da382cSHugh Dickins 	}
420e0da382cSHugh Dickins 	if (ceiling) {
421e0da382cSHugh Dickins 		ceiling &= PMD_MASK;
422e0da382cSHugh Dickins 		if (!ceiling)
423e0da382cSHugh Dickins 			return;
424e0da382cSHugh Dickins 	}
425e0da382cSHugh Dickins 	if (end - 1 > ceiling - 1)
426e0da382cSHugh Dickins 		end -= PMD_SIZE;
427e0da382cSHugh Dickins 	if (addr > end - 1)
428e0da382cSHugh Dickins 		return;
429e0da382cSHugh Dickins 
43042b77728SJan Beulich 	pgd = pgd_offset(tlb->mm, addr);
4311da177e4SLinus Torvalds 	do {
4321da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
4331da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
4341da177e4SLinus Torvalds 			continue;
43542b77728SJan Beulich 		free_pud_range(tlb, pgd, addr, next, floor, ceiling);
4361da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
437e0da382cSHugh Dickins }
438e0da382cSHugh Dickins 
43942b77728SJan Beulich void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma,
440e0da382cSHugh Dickins 		unsigned long floor, unsigned long ceiling)
441e0da382cSHugh Dickins {
442e0da382cSHugh Dickins 	while (vma) {
443e0da382cSHugh Dickins 		struct vm_area_struct *next = vma->vm_next;
444e0da382cSHugh Dickins 		unsigned long addr = vma->vm_start;
445e0da382cSHugh Dickins 
4468f4f8c16SHugh Dickins 		/*
44725d9e2d1Snpiggin@suse.de 		 * Hide vma from rmap and truncate_pagecache before freeing
44825d9e2d1Snpiggin@suse.de 		 * pgtables
4498f4f8c16SHugh Dickins 		 */
4505beb4930SRik van Riel 		unlink_anon_vmas(vma);
4518f4f8c16SHugh Dickins 		unlink_file_vma(vma);
4528f4f8c16SHugh Dickins 
4539da61aefSDavid Gibson 		if (is_vm_hugetlb_page(vma)) {
4543bf5ee95SHugh Dickins 			hugetlb_free_pgd_range(tlb, addr, vma->vm_end,
4553bf5ee95SHugh Dickins 				floor, next? next->vm_start: ceiling);
4563bf5ee95SHugh Dickins 		} else {
4573bf5ee95SHugh Dickins 			/*
4583bf5ee95SHugh Dickins 			 * Optimization: gather nearby vmas into one call down
4593bf5ee95SHugh Dickins 			 */
4603bf5ee95SHugh Dickins 			while (next && next->vm_start <= vma->vm_end + PMD_SIZE
4614866920bSDavid Gibson 			       && !is_vm_hugetlb_page(next)) {
462e0da382cSHugh Dickins 				vma = next;
463e0da382cSHugh Dickins 				next = vma->vm_next;
4645beb4930SRik van Riel 				unlink_anon_vmas(vma);
4658f4f8c16SHugh Dickins 				unlink_file_vma(vma);
466e0da382cSHugh Dickins 			}
4673bf5ee95SHugh Dickins 			free_pgd_range(tlb, addr, vma->vm_end,
468e0da382cSHugh Dickins 				floor, next? next->vm_start: ceiling);
4693bf5ee95SHugh Dickins 		}
470e0da382cSHugh Dickins 		vma = next;
471e0da382cSHugh Dickins 	}
4721da177e4SLinus Torvalds }
4731da177e4SLinus Torvalds 
4748ac1f832SAndrea Arcangeli int __pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma,
4758ac1f832SAndrea Arcangeli 		pmd_t *pmd, unsigned long address)
4761da177e4SLinus Torvalds {
4772f569afdSMartin Schwidefsky 	pgtable_t new = pte_alloc_one(mm, address);
4788ac1f832SAndrea Arcangeli 	int wait_split_huge_page;
4791da177e4SLinus Torvalds 	if (!new)
4801bb3630eSHugh Dickins 		return -ENOMEM;
4811bb3630eSHugh Dickins 
482362a61adSNick Piggin 	/*
483362a61adSNick Piggin 	 * Ensure all pte setup (eg. pte page lock and page clearing) are
484362a61adSNick Piggin 	 * visible before the pte is made visible to other CPUs by being
485362a61adSNick Piggin 	 * put into page tables.
486362a61adSNick Piggin 	 *
487362a61adSNick Piggin 	 * The other side of the story is the pointer chasing in the page
488362a61adSNick Piggin 	 * table walking code (when walking the page table without locking;
489362a61adSNick Piggin 	 * ie. most of the time). Fortunately, these data accesses consist
490362a61adSNick Piggin 	 * of a chain of data-dependent loads, meaning most CPUs (alpha
491362a61adSNick Piggin 	 * being the notable exception) will already guarantee loads are
492362a61adSNick Piggin 	 * seen in-order. See the alpha page table accessors for the
493362a61adSNick Piggin 	 * smp_read_barrier_depends() barriers in page table walking code.
494362a61adSNick Piggin 	 */
495362a61adSNick Piggin 	smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */
496362a61adSNick Piggin 
497c74df32cSHugh Dickins 	spin_lock(&mm->page_table_lock);
4988ac1f832SAndrea Arcangeli 	wait_split_huge_page = 0;
4998ac1f832SAndrea Arcangeli 	if (likely(pmd_none(*pmd))) {	/* Has another populated it ? */
5001da177e4SLinus Torvalds 		mm->nr_ptes++;
5011da177e4SLinus Torvalds 		pmd_populate(mm, pmd, new);
5022f569afdSMartin Schwidefsky 		new = NULL;
5038ac1f832SAndrea Arcangeli 	} else if (unlikely(pmd_trans_splitting(*pmd)))
5048ac1f832SAndrea Arcangeli 		wait_split_huge_page = 1;
505c74df32cSHugh Dickins 	spin_unlock(&mm->page_table_lock);
5062f569afdSMartin Schwidefsky 	if (new)
5072f569afdSMartin Schwidefsky 		pte_free(mm, new);
5088ac1f832SAndrea Arcangeli 	if (wait_split_huge_page)
5098ac1f832SAndrea Arcangeli 		wait_split_huge_page(vma->anon_vma, pmd);
5101bb3630eSHugh Dickins 	return 0;
5111da177e4SLinus Torvalds }
5121da177e4SLinus Torvalds 
5131bb3630eSHugh Dickins int __pte_alloc_kernel(pmd_t *pmd, unsigned long address)
5141da177e4SLinus Torvalds {
5151bb3630eSHugh Dickins 	pte_t *new = pte_alloc_one_kernel(&init_mm, address);
5161da177e4SLinus Torvalds 	if (!new)
5171bb3630eSHugh Dickins 		return -ENOMEM;
5181da177e4SLinus Torvalds 
519362a61adSNick Piggin 	smp_wmb(); /* See comment in __pte_alloc */
520362a61adSNick Piggin 
521872fec16SHugh Dickins 	spin_lock(&init_mm.page_table_lock);
5228ac1f832SAndrea Arcangeli 	if (likely(pmd_none(*pmd))) {	/* Has another populated it ? */
523872fec16SHugh Dickins 		pmd_populate_kernel(&init_mm, pmd, new);
5242f569afdSMartin Schwidefsky 		new = NULL;
5258ac1f832SAndrea Arcangeli 	} else
5268ac1f832SAndrea Arcangeli 		VM_BUG_ON(pmd_trans_splitting(*pmd));
527872fec16SHugh Dickins 	spin_unlock(&init_mm.page_table_lock);
5282f569afdSMartin Schwidefsky 	if (new)
5292f569afdSMartin Schwidefsky 		pte_free_kernel(&init_mm, new);
5301bb3630eSHugh Dickins 	return 0;
5311da177e4SLinus Torvalds }
5321da177e4SLinus Torvalds 
533d559db08SKAMEZAWA Hiroyuki static inline void init_rss_vec(int *rss)
534ae859762SHugh Dickins {
535d559db08SKAMEZAWA Hiroyuki 	memset(rss, 0, sizeof(int) * NR_MM_COUNTERS);
536d559db08SKAMEZAWA Hiroyuki }
537d559db08SKAMEZAWA Hiroyuki 
538d559db08SKAMEZAWA Hiroyuki static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss)
539d559db08SKAMEZAWA Hiroyuki {
540d559db08SKAMEZAWA Hiroyuki 	int i;
541d559db08SKAMEZAWA Hiroyuki 
54234e55232SKAMEZAWA Hiroyuki 	if (current->mm == mm)
54334e55232SKAMEZAWA Hiroyuki 		sync_mm_rss(current, mm);
544d559db08SKAMEZAWA Hiroyuki 	for (i = 0; i < NR_MM_COUNTERS; i++)
545d559db08SKAMEZAWA Hiroyuki 		if (rss[i])
546d559db08SKAMEZAWA Hiroyuki 			add_mm_counter(mm, i, rss[i]);
547ae859762SHugh Dickins }
548ae859762SHugh Dickins 
5491da177e4SLinus Torvalds /*
5506aab341eSLinus Torvalds  * This function is called to print an error when a bad pte
5516aab341eSLinus Torvalds  * is found. For example, we might have a PFN-mapped pte in
5526aab341eSLinus Torvalds  * a region that doesn't allow it.
553b5810039SNick Piggin  *
554b5810039SNick Piggin  * The calling function must still handle the error.
555b5810039SNick Piggin  */
5563dc14741SHugh Dickins static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
5573dc14741SHugh Dickins 			  pte_t pte, struct page *page)
558b5810039SNick Piggin {
5593dc14741SHugh Dickins 	pgd_t *pgd = pgd_offset(vma->vm_mm, addr);
5603dc14741SHugh Dickins 	pud_t *pud = pud_offset(pgd, addr);
5613dc14741SHugh Dickins 	pmd_t *pmd = pmd_offset(pud, addr);
5623dc14741SHugh Dickins 	struct address_space *mapping;
5633dc14741SHugh Dickins 	pgoff_t index;
564d936cf9bSHugh Dickins 	static unsigned long resume;
565d936cf9bSHugh Dickins 	static unsigned long nr_shown;
566d936cf9bSHugh Dickins 	static unsigned long nr_unshown;
567d936cf9bSHugh Dickins 
568d936cf9bSHugh Dickins 	/*
569d936cf9bSHugh Dickins 	 * Allow a burst of 60 reports, then keep quiet for that minute;
570d936cf9bSHugh Dickins 	 * or allow a steady drip of one report per second.
571d936cf9bSHugh Dickins 	 */
572d936cf9bSHugh Dickins 	if (nr_shown == 60) {
573d936cf9bSHugh Dickins 		if (time_before(jiffies, resume)) {
574d936cf9bSHugh Dickins 			nr_unshown++;
575d936cf9bSHugh Dickins 			return;
576d936cf9bSHugh Dickins 		}
577d936cf9bSHugh Dickins 		if (nr_unshown) {
5781e9e6365SHugh Dickins 			printk(KERN_ALERT
5791e9e6365SHugh Dickins 				"BUG: Bad page map: %lu messages suppressed\n",
580d936cf9bSHugh Dickins 				nr_unshown);
581d936cf9bSHugh Dickins 			nr_unshown = 0;
582d936cf9bSHugh Dickins 		}
583d936cf9bSHugh Dickins 		nr_shown = 0;
584d936cf9bSHugh Dickins 	}
585d936cf9bSHugh Dickins 	if (nr_shown++ == 0)
586d936cf9bSHugh Dickins 		resume = jiffies + 60 * HZ;
5873dc14741SHugh Dickins 
5883dc14741SHugh Dickins 	mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL;
5893dc14741SHugh Dickins 	index = linear_page_index(vma, addr);
5903dc14741SHugh Dickins 
5911e9e6365SHugh Dickins 	printk(KERN_ALERT
5921e9e6365SHugh Dickins 		"BUG: Bad page map in process %s  pte:%08llx pmd:%08llx\n",
5933dc14741SHugh Dickins 		current->comm,
5943dc14741SHugh Dickins 		(long long)pte_val(pte), (long long)pmd_val(*pmd));
595718a3821SWu Fengguang 	if (page)
596718a3821SWu Fengguang 		dump_page(page);
5971e9e6365SHugh Dickins 	printk(KERN_ALERT
5983dc14741SHugh Dickins 		"addr:%p vm_flags:%08lx anon_vma:%p mapping:%p index:%lx\n",
5993dc14741SHugh Dickins 		(void *)addr, vma->vm_flags, vma->anon_vma, mapping, index);
6003dc14741SHugh Dickins 	/*
6013dc14741SHugh Dickins 	 * Choose text because data symbols depend on CONFIG_KALLSYMS_ALL=y
6023dc14741SHugh Dickins 	 */
6033dc14741SHugh Dickins 	if (vma->vm_ops)
6041e9e6365SHugh Dickins 		print_symbol(KERN_ALERT "vma->vm_ops->fault: %s\n",
6053dc14741SHugh Dickins 				(unsigned long)vma->vm_ops->fault);
6063dc14741SHugh Dickins 	if (vma->vm_file && vma->vm_file->f_op)
6071e9e6365SHugh Dickins 		print_symbol(KERN_ALERT "vma->vm_file->f_op->mmap: %s\n",
6083dc14741SHugh Dickins 				(unsigned long)vma->vm_file->f_op->mmap);
609b5810039SNick Piggin 	dump_stack();
6103dc14741SHugh Dickins 	add_taint(TAINT_BAD_PAGE);
611b5810039SNick Piggin }
612b5810039SNick Piggin 
61367121172SLinus Torvalds static inline int is_cow_mapping(unsigned int flags)
61467121172SLinus Torvalds {
61567121172SLinus Torvalds 	return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
61667121172SLinus Torvalds }
61767121172SLinus Torvalds 
61862eede62SHugh Dickins #ifndef is_zero_pfn
61962eede62SHugh Dickins static inline int is_zero_pfn(unsigned long pfn)
62062eede62SHugh Dickins {
62162eede62SHugh Dickins 	return pfn == zero_pfn;
62262eede62SHugh Dickins }
62362eede62SHugh Dickins #endif
62462eede62SHugh Dickins 
62562eede62SHugh Dickins #ifndef my_zero_pfn
62662eede62SHugh Dickins static inline unsigned long my_zero_pfn(unsigned long addr)
62762eede62SHugh Dickins {
62862eede62SHugh Dickins 	return zero_pfn;
62962eede62SHugh Dickins }
63062eede62SHugh Dickins #endif
63162eede62SHugh Dickins 
632b5810039SNick Piggin /*
6337e675137SNick Piggin  * vm_normal_page -- This function gets the "struct page" associated with a pte.
6346aab341eSLinus Torvalds  *
6357e675137SNick Piggin  * "Special" mappings do not wish to be associated with a "struct page" (either
6367e675137SNick Piggin  * it doesn't exist, or it exists but they don't want to touch it). In this
6377e675137SNick Piggin  * case, NULL is returned here. "Normal" mappings do have a struct page.
638b379d790SJared Hulbert  *
6397e675137SNick Piggin  * There are 2 broad cases. Firstly, an architecture may define a pte_special()
6407e675137SNick Piggin  * pte bit, in which case this function is trivial. Secondly, an architecture
6417e675137SNick Piggin  * may not have a spare pte bit, which requires a more complicated scheme,
6427e675137SNick Piggin  * described below.
6437e675137SNick Piggin  *
6447e675137SNick Piggin  * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a
6457e675137SNick Piggin  * special mapping (even if there are underlying and valid "struct pages").
6467e675137SNick Piggin  * COWed pages of a VM_PFNMAP are always normal.
6476aab341eSLinus Torvalds  *
648b379d790SJared Hulbert  * The way we recognize COWed pages within VM_PFNMAP mappings is through the
649b379d790SJared Hulbert  * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit
6507e675137SNick Piggin  * set, and the vm_pgoff will point to the first PFN mapped: thus every special
6517e675137SNick Piggin  * mapping will always honor the rule
6526aab341eSLinus Torvalds  *
6536aab341eSLinus Torvalds  *	pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
6546aab341eSLinus Torvalds  *
6557e675137SNick Piggin  * And for normal mappings this is false.
656b379d790SJared Hulbert  *
6577e675137SNick Piggin  * This restricts such mappings to be a linear translation from virtual address
6587e675137SNick Piggin  * to pfn. To get around this restriction, we allow arbitrary mappings so long
6597e675137SNick Piggin  * as the vma is not a COW mapping; in that case, we know that all ptes are
6607e675137SNick Piggin  * special (because none can have been COWed).
661b379d790SJared Hulbert  *
662b379d790SJared Hulbert  *
6637e675137SNick Piggin  * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP.
6647e675137SNick Piggin  *
665b379d790SJared Hulbert  * VM_MIXEDMAP mappings can likewise contain memory with or without "struct
666b379d790SJared Hulbert  * page" backing, however the difference is that _all_ pages with a struct
667b379d790SJared Hulbert  * page (that is, those where pfn_valid is true) are refcounted and considered
668b379d790SJared Hulbert  * normal pages by the VM. The disadvantage is that pages are refcounted
669b379d790SJared Hulbert  * (which can be slower and simply not an option for some PFNMAP users). The
670b379d790SJared Hulbert  * advantage is that we don't have to follow the strict linearity rule of
671b379d790SJared Hulbert  * PFNMAP mappings in order to support COWable mappings.
672b379d790SJared Hulbert  *
673ee498ed7SHugh Dickins  */
6747e675137SNick Piggin #ifdef __HAVE_ARCH_PTE_SPECIAL
6757e675137SNick Piggin # define HAVE_PTE_SPECIAL 1
6767e675137SNick Piggin #else
6777e675137SNick Piggin # define HAVE_PTE_SPECIAL 0
6787e675137SNick Piggin #endif
6797e675137SNick Piggin struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
6807e675137SNick Piggin 				pte_t pte)
681ee498ed7SHugh Dickins {
68222b31eecSHugh Dickins 	unsigned long pfn = pte_pfn(pte);
6837e675137SNick Piggin 
6847e675137SNick Piggin 	if (HAVE_PTE_SPECIAL) {
68522b31eecSHugh Dickins 		if (likely(!pte_special(pte)))
68622b31eecSHugh Dickins 			goto check_pfn;
687a13ea5b7SHugh Dickins 		if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
688a13ea5b7SHugh Dickins 			return NULL;
68962eede62SHugh Dickins 		if (!is_zero_pfn(pfn))
69022b31eecSHugh Dickins 			print_bad_pte(vma, addr, pte, NULL);
6917e675137SNick Piggin 		return NULL;
6927e675137SNick Piggin 	}
6937e675137SNick Piggin 
6947e675137SNick Piggin 	/* !HAVE_PTE_SPECIAL case follows: */
6957e675137SNick Piggin 
696b379d790SJared Hulbert 	if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
697b379d790SJared Hulbert 		if (vma->vm_flags & VM_MIXEDMAP) {
698b379d790SJared Hulbert 			if (!pfn_valid(pfn))
699b379d790SJared Hulbert 				return NULL;
700b379d790SJared Hulbert 			goto out;
701b379d790SJared Hulbert 		} else {
7027e675137SNick Piggin 			unsigned long off;
7037e675137SNick Piggin 			off = (addr - vma->vm_start) >> PAGE_SHIFT;
7046aab341eSLinus Torvalds 			if (pfn == vma->vm_pgoff + off)
7056aab341eSLinus Torvalds 				return NULL;
70667121172SLinus Torvalds 			if (!is_cow_mapping(vma->vm_flags))
707fb155c16SLinus Torvalds 				return NULL;
7086aab341eSLinus Torvalds 		}
709b379d790SJared Hulbert 	}
7106aab341eSLinus Torvalds 
71162eede62SHugh Dickins 	if (is_zero_pfn(pfn))
71262eede62SHugh Dickins 		return NULL;
71322b31eecSHugh Dickins check_pfn:
71422b31eecSHugh Dickins 	if (unlikely(pfn > highest_memmap_pfn)) {
71522b31eecSHugh Dickins 		print_bad_pte(vma, addr, pte, NULL);
71622b31eecSHugh Dickins 		return NULL;
71722b31eecSHugh Dickins 	}
7186aab341eSLinus Torvalds 
7196aab341eSLinus Torvalds 	/*
7207e675137SNick Piggin 	 * NOTE! We still have PageReserved() pages in the page tables.
7217e675137SNick Piggin 	 * eg. VDSO mappings can cause them to exist.
7226aab341eSLinus Torvalds 	 */
723b379d790SJared Hulbert out:
7246aab341eSLinus Torvalds 	return pfn_to_page(pfn);
725ee498ed7SHugh Dickins }
726ee498ed7SHugh Dickins 
727ee498ed7SHugh Dickins /*
7281da177e4SLinus Torvalds  * copy one vm_area from one task to the other. Assumes the page tables
7291da177e4SLinus Torvalds  * already present in the new task to be cleared in the whole range
7301da177e4SLinus Torvalds  * covered by this vma.
7311da177e4SLinus Torvalds  */
7321da177e4SLinus Torvalds 
733570a335bSHugh Dickins static inline unsigned long
7341da177e4SLinus Torvalds copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
735b5810039SNick Piggin 		pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma,
7368c103762SHugh Dickins 		unsigned long addr, int *rss)
7371da177e4SLinus Torvalds {
738b5810039SNick Piggin 	unsigned long vm_flags = vma->vm_flags;
7391da177e4SLinus Torvalds 	pte_t pte = *src_pte;
7401da177e4SLinus Torvalds 	struct page *page;
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds 	/* pte contains position in swap or file, so copy. */
7431da177e4SLinus Torvalds 	if (unlikely(!pte_present(pte))) {
7441da177e4SLinus Torvalds 		if (!pte_file(pte)) {
7450697212aSChristoph Lameter 			swp_entry_t entry = pte_to_swp_entry(pte);
7460697212aSChristoph Lameter 
747570a335bSHugh Dickins 			if (swap_duplicate(entry) < 0)
748570a335bSHugh Dickins 				return entry.val;
749570a335bSHugh Dickins 
7501da177e4SLinus Torvalds 			/* make sure dst_mm is on swapoff's mmlist. */
7511da177e4SLinus Torvalds 			if (unlikely(list_empty(&dst_mm->mmlist))) {
7521da177e4SLinus Torvalds 				spin_lock(&mmlist_lock);
753f412ac08SHugh Dickins 				if (list_empty(&dst_mm->mmlist))
754f412ac08SHugh Dickins 					list_add(&dst_mm->mmlist,
755f412ac08SHugh Dickins 						 &src_mm->mmlist);
7561da177e4SLinus Torvalds 				spin_unlock(&mmlist_lock);
7571da177e4SLinus Torvalds 			}
758b084d435SKAMEZAWA Hiroyuki 			if (likely(!non_swap_entry(entry)))
759b084d435SKAMEZAWA Hiroyuki 				rss[MM_SWAPENTS]++;
760b084d435SKAMEZAWA Hiroyuki 			else if (is_write_migration_entry(entry) &&
7610697212aSChristoph Lameter 					is_cow_mapping(vm_flags)) {
7620697212aSChristoph Lameter 				/*
7630697212aSChristoph Lameter 				 * COW mappings require pages in both parent
7640697212aSChristoph Lameter 				 * and child to be set to read.
7650697212aSChristoph Lameter 				 */
7660697212aSChristoph Lameter 				make_migration_entry_read(&entry);
7670697212aSChristoph Lameter 				pte = swp_entry_to_pte(entry);
7680697212aSChristoph Lameter 				set_pte_at(src_mm, addr, src_pte, pte);
7690697212aSChristoph Lameter 			}
7701da177e4SLinus Torvalds 		}
771ae859762SHugh Dickins 		goto out_set_pte;
7721da177e4SLinus Torvalds 	}
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 	/*
7751da177e4SLinus Torvalds 	 * If it's a COW mapping, write protect it both
7761da177e4SLinus Torvalds 	 * in the parent and the child
7771da177e4SLinus Torvalds 	 */
77867121172SLinus Torvalds 	if (is_cow_mapping(vm_flags)) {
7791da177e4SLinus Torvalds 		ptep_set_wrprotect(src_mm, addr, src_pte);
7803dc90795SZachary Amsden 		pte = pte_wrprotect(pte);
7811da177e4SLinus Torvalds 	}
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 	/*
7841da177e4SLinus Torvalds 	 * If it's a shared mapping, mark it clean in
7851da177e4SLinus Torvalds 	 * the child
7861da177e4SLinus Torvalds 	 */
7871da177e4SLinus Torvalds 	if (vm_flags & VM_SHARED)
7881da177e4SLinus Torvalds 		pte = pte_mkclean(pte);
7891da177e4SLinus Torvalds 	pte = pte_mkold(pte);
7906aab341eSLinus Torvalds 
7916aab341eSLinus Torvalds 	page = vm_normal_page(vma, addr, pte);
7926aab341eSLinus Torvalds 	if (page) {
7931da177e4SLinus Torvalds 		get_page(page);
79421333b2bSHugh Dickins 		page_dup_rmap(page);
795d559db08SKAMEZAWA Hiroyuki 		if (PageAnon(page))
796d559db08SKAMEZAWA Hiroyuki 			rss[MM_ANONPAGES]++;
797d559db08SKAMEZAWA Hiroyuki 		else
798d559db08SKAMEZAWA Hiroyuki 			rss[MM_FILEPAGES]++;
7996aab341eSLinus Torvalds 	}
800ae859762SHugh Dickins 
801ae859762SHugh Dickins out_set_pte:
802ae859762SHugh Dickins 	set_pte_at(dst_mm, addr, dst_pte, pte);
803570a335bSHugh Dickins 	return 0;
8041da177e4SLinus Torvalds }
8051da177e4SLinus Torvalds 
80671e3aac0SAndrea Arcangeli int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
8071da177e4SLinus Torvalds 		   pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
8081da177e4SLinus Torvalds 		   unsigned long addr, unsigned long end)
8091da177e4SLinus Torvalds {
810c36987e2SDaisuke Nishimura 	pte_t *orig_src_pte, *orig_dst_pte;
8111da177e4SLinus Torvalds 	pte_t *src_pte, *dst_pte;
812c74df32cSHugh Dickins 	spinlock_t *src_ptl, *dst_ptl;
813e040f218SHugh Dickins 	int progress = 0;
814d559db08SKAMEZAWA Hiroyuki 	int rss[NR_MM_COUNTERS];
815570a335bSHugh Dickins 	swp_entry_t entry = (swp_entry_t){0};
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds again:
818d559db08SKAMEZAWA Hiroyuki 	init_rss_vec(rss);
819d559db08SKAMEZAWA Hiroyuki 
820c74df32cSHugh Dickins 	dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
8211da177e4SLinus Torvalds 	if (!dst_pte)
8221da177e4SLinus Torvalds 		return -ENOMEM;
823ece0e2b6SPeter Zijlstra 	src_pte = pte_offset_map(src_pmd, addr);
8244c21e2f2SHugh Dickins 	src_ptl = pte_lockptr(src_mm, src_pmd);
825f20dc5f7SIngo Molnar 	spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
826c36987e2SDaisuke Nishimura 	orig_src_pte = src_pte;
827c36987e2SDaisuke Nishimura 	orig_dst_pte = dst_pte;
8286606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 	do {
8311da177e4SLinus Torvalds 		/*
8321da177e4SLinus Torvalds 		 * We are holding two locks at this point - either of them
8331da177e4SLinus Torvalds 		 * could generate latencies in another task on another CPU.
8341da177e4SLinus Torvalds 		 */
835e040f218SHugh Dickins 		if (progress >= 32) {
836e040f218SHugh Dickins 			progress = 0;
837e040f218SHugh Dickins 			if (need_resched() ||
83895c354feSNick Piggin 			    spin_needbreak(src_ptl) || spin_needbreak(dst_ptl))
8391da177e4SLinus Torvalds 				break;
840e040f218SHugh Dickins 		}
8411da177e4SLinus Torvalds 		if (pte_none(*src_pte)) {
8421da177e4SLinus Torvalds 			progress++;
8431da177e4SLinus Torvalds 			continue;
8441da177e4SLinus Torvalds 		}
845570a335bSHugh Dickins 		entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte,
846570a335bSHugh Dickins 							vma, addr, rss);
847570a335bSHugh Dickins 		if (entry.val)
848570a335bSHugh Dickins 			break;
8491da177e4SLinus Torvalds 		progress += 8;
8501da177e4SLinus Torvalds 	} while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
8511da177e4SLinus Torvalds 
8526606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
853c74df32cSHugh Dickins 	spin_unlock(src_ptl);
854ece0e2b6SPeter Zijlstra 	pte_unmap(orig_src_pte);
855d559db08SKAMEZAWA Hiroyuki 	add_mm_rss_vec(dst_mm, rss);
856c36987e2SDaisuke Nishimura 	pte_unmap_unlock(orig_dst_pte, dst_ptl);
857c74df32cSHugh Dickins 	cond_resched();
858570a335bSHugh Dickins 
859570a335bSHugh Dickins 	if (entry.val) {
860570a335bSHugh Dickins 		if (add_swap_count_continuation(entry, GFP_KERNEL) < 0)
861570a335bSHugh Dickins 			return -ENOMEM;
862570a335bSHugh Dickins 		progress = 0;
863570a335bSHugh Dickins 	}
8641da177e4SLinus Torvalds 	if (addr != end)
8651da177e4SLinus Torvalds 		goto again;
8661da177e4SLinus Torvalds 	return 0;
8671da177e4SLinus Torvalds }
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
8701da177e4SLinus Torvalds 		pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma,
8711da177e4SLinus Torvalds 		unsigned long addr, unsigned long end)
8721da177e4SLinus Torvalds {
8731da177e4SLinus Torvalds 	pmd_t *src_pmd, *dst_pmd;
8741da177e4SLinus Torvalds 	unsigned long next;
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 	dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
8771da177e4SLinus Torvalds 	if (!dst_pmd)
8781da177e4SLinus Torvalds 		return -ENOMEM;
8791da177e4SLinus Torvalds 	src_pmd = pmd_offset(src_pud, addr);
8801da177e4SLinus Torvalds 	do {
8811da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
88271e3aac0SAndrea Arcangeli 		if (pmd_trans_huge(*src_pmd)) {
88371e3aac0SAndrea Arcangeli 			int err;
88414d1a55cSAndrea Arcangeli 			VM_BUG_ON(next-addr != HPAGE_PMD_SIZE);
88571e3aac0SAndrea Arcangeli 			err = copy_huge_pmd(dst_mm, src_mm,
88671e3aac0SAndrea Arcangeli 					    dst_pmd, src_pmd, addr, vma);
88771e3aac0SAndrea Arcangeli 			if (err == -ENOMEM)
88871e3aac0SAndrea Arcangeli 				return -ENOMEM;
88971e3aac0SAndrea Arcangeli 			if (!err)
89071e3aac0SAndrea Arcangeli 				continue;
89171e3aac0SAndrea Arcangeli 			/* fall through */
89271e3aac0SAndrea Arcangeli 		}
8931da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(src_pmd))
8941da177e4SLinus Torvalds 			continue;
8951da177e4SLinus Torvalds 		if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd,
8961da177e4SLinus Torvalds 						vma, addr, next))
8971da177e4SLinus Torvalds 			return -ENOMEM;
8981da177e4SLinus Torvalds 	} while (dst_pmd++, src_pmd++, addr = next, addr != end);
8991da177e4SLinus Torvalds 	return 0;
9001da177e4SLinus Torvalds }
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
9031da177e4SLinus Torvalds 		pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma,
9041da177e4SLinus Torvalds 		unsigned long addr, unsigned long end)
9051da177e4SLinus Torvalds {
9061da177e4SLinus Torvalds 	pud_t *src_pud, *dst_pud;
9071da177e4SLinus Torvalds 	unsigned long next;
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds 	dst_pud = pud_alloc(dst_mm, dst_pgd, addr);
9101da177e4SLinus Torvalds 	if (!dst_pud)
9111da177e4SLinus Torvalds 		return -ENOMEM;
9121da177e4SLinus Torvalds 	src_pud = pud_offset(src_pgd, addr);
9131da177e4SLinus Torvalds 	do {
9141da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
9151da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(src_pud))
9161da177e4SLinus Torvalds 			continue;
9171da177e4SLinus Torvalds 		if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud,
9181da177e4SLinus Torvalds 						vma, addr, next))
9191da177e4SLinus Torvalds 			return -ENOMEM;
9201da177e4SLinus Torvalds 	} while (dst_pud++, src_pud++, addr = next, addr != end);
9211da177e4SLinus Torvalds 	return 0;
9221da177e4SLinus Torvalds }
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
9251da177e4SLinus Torvalds 		struct vm_area_struct *vma)
9261da177e4SLinus Torvalds {
9271da177e4SLinus Torvalds 	pgd_t *src_pgd, *dst_pgd;
9281da177e4SLinus Torvalds 	unsigned long next;
9291da177e4SLinus Torvalds 	unsigned long addr = vma->vm_start;
9301da177e4SLinus Torvalds 	unsigned long end = vma->vm_end;
931cddb8a5cSAndrea Arcangeli 	int ret;
9321da177e4SLinus Torvalds 
933d992895bSNick Piggin 	/*
934d992895bSNick Piggin 	 * Don't copy ptes where a page fault will fill them correctly.
935d992895bSNick Piggin 	 * Fork becomes much lighter when there are big shared or private
936d992895bSNick Piggin 	 * readonly mappings. The tradeoff is that copy_page_range is more
937d992895bSNick Piggin 	 * efficient than faulting.
938d992895bSNick Piggin 	 */
9394d7672b4SLinus Torvalds 	if (!(vma->vm_flags & (VM_HUGETLB|VM_NONLINEAR|VM_PFNMAP|VM_INSERTPAGE))) {
940d992895bSNick Piggin 		if (!vma->anon_vma)
941d992895bSNick Piggin 			return 0;
942d992895bSNick Piggin 	}
943d992895bSNick Piggin 
9441da177e4SLinus Torvalds 	if (is_vm_hugetlb_page(vma))
9451da177e4SLinus Torvalds 		return copy_hugetlb_page_range(dst_mm, src_mm, vma);
9461da177e4SLinus Torvalds 
94734801ba9Svenkatesh.pallipadi@intel.com 	if (unlikely(is_pfn_mapping(vma))) {
9482ab64037Svenkatesh.pallipadi@intel.com 		/*
9492ab64037Svenkatesh.pallipadi@intel.com 		 * We do not free on error cases below as remove_vma
9502ab64037Svenkatesh.pallipadi@intel.com 		 * gets called on error from higher level routine
9512ab64037Svenkatesh.pallipadi@intel.com 		 */
9522ab64037Svenkatesh.pallipadi@intel.com 		ret = track_pfn_vma_copy(vma);
9532ab64037Svenkatesh.pallipadi@intel.com 		if (ret)
9542ab64037Svenkatesh.pallipadi@intel.com 			return ret;
9552ab64037Svenkatesh.pallipadi@intel.com 	}
9562ab64037Svenkatesh.pallipadi@intel.com 
957cddb8a5cSAndrea Arcangeli 	/*
958cddb8a5cSAndrea Arcangeli 	 * We need to invalidate the secondary MMU mappings only when
959cddb8a5cSAndrea Arcangeli 	 * there could be a permission downgrade on the ptes of the
960cddb8a5cSAndrea Arcangeli 	 * parent mm. And a permission downgrade will only happen if
961cddb8a5cSAndrea Arcangeli 	 * is_cow_mapping() returns true.
962cddb8a5cSAndrea Arcangeli 	 */
963cddb8a5cSAndrea Arcangeli 	if (is_cow_mapping(vma->vm_flags))
964cddb8a5cSAndrea Arcangeli 		mmu_notifier_invalidate_range_start(src_mm, addr, end);
965cddb8a5cSAndrea Arcangeli 
966cddb8a5cSAndrea Arcangeli 	ret = 0;
9671da177e4SLinus Torvalds 	dst_pgd = pgd_offset(dst_mm, addr);
9681da177e4SLinus Torvalds 	src_pgd = pgd_offset(src_mm, addr);
9691da177e4SLinus Torvalds 	do {
9701da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
9711da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(src_pgd))
9721da177e4SLinus Torvalds 			continue;
973cddb8a5cSAndrea Arcangeli 		if (unlikely(copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd,
974cddb8a5cSAndrea Arcangeli 					    vma, addr, next))) {
975cddb8a5cSAndrea Arcangeli 			ret = -ENOMEM;
976cddb8a5cSAndrea Arcangeli 			break;
977cddb8a5cSAndrea Arcangeli 		}
9781da177e4SLinus Torvalds 	} while (dst_pgd++, src_pgd++, addr = next, addr != end);
979cddb8a5cSAndrea Arcangeli 
980cddb8a5cSAndrea Arcangeli 	if (is_cow_mapping(vma->vm_flags))
981cddb8a5cSAndrea Arcangeli 		mmu_notifier_invalidate_range_end(src_mm,
982cddb8a5cSAndrea Arcangeli 						  vma->vm_start, end);
983cddb8a5cSAndrea Arcangeli 	return ret;
9841da177e4SLinus Torvalds }
9851da177e4SLinus Torvalds 
98651c6f666SRobin Holt static unsigned long zap_pte_range(struct mmu_gather *tlb,
987b5810039SNick Piggin 				struct vm_area_struct *vma, pmd_t *pmd,
9881da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
98951c6f666SRobin Holt 				long *zap_work, struct zap_details *details)
9901da177e4SLinus Torvalds {
991b5810039SNick Piggin 	struct mm_struct *mm = tlb->mm;
992d16dfc55SPeter Zijlstra 	int force_flush = 0;
9931da177e4SLinus Torvalds 	pte_t *pte;
994508034a3SHugh Dickins 	spinlock_t *ptl;
995d559db08SKAMEZAWA Hiroyuki 	int rss[NR_MM_COUNTERS];
996d559db08SKAMEZAWA Hiroyuki 
997d16dfc55SPeter Zijlstra again:
998*e303297eSPeter Zijlstra 	init_rss_vec(rss);
999508034a3SHugh Dickins 	pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
10006606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
10011da177e4SLinus Torvalds 	do {
10021da177e4SLinus Torvalds 		pte_t ptent = *pte;
100351c6f666SRobin Holt 		if (pte_none(ptent)) {
100451c6f666SRobin Holt 			(*zap_work)--;
10051da177e4SLinus Torvalds 			continue;
100651c6f666SRobin Holt 		}
100751c6f666SRobin Holt 
100851c6f666SRobin Holt 		(*zap_work) -= PAGE_SIZE;
100951c6f666SRobin Holt 
10106f5e6b9eSHugh Dickins 		if (pte_present(ptent)) {
10116f5e6b9eSHugh Dickins 			struct page *page;
10126f5e6b9eSHugh Dickins 
10136aab341eSLinus Torvalds 			page = vm_normal_page(vma, addr, ptent);
10141da177e4SLinus Torvalds 			if (unlikely(details) && page) {
10151da177e4SLinus Torvalds 				/*
10161da177e4SLinus Torvalds 				 * unmap_shared_mapping_pages() wants to
10171da177e4SLinus Torvalds 				 * invalidate cache without truncating:
10181da177e4SLinus Torvalds 				 * unmap shared but keep private pages.
10191da177e4SLinus Torvalds 				 */
10201da177e4SLinus Torvalds 				if (details->check_mapping &&
10211da177e4SLinus Torvalds 				    details->check_mapping != page->mapping)
10221da177e4SLinus Torvalds 					continue;
10231da177e4SLinus Torvalds 				/*
10241da177e4SLinus Torvalds 				 * Each page->index must be checked when
10251da177e4SLinus Torvalds 				 * invalidating or truncating nonlinear.
10261da177e4SLinus Torvalds 				 */
10271da177e4SLinus Torvalds 				if (details->nonlinear_vma &&
10281da177e4SLinus Torvalds 				    (page->index < details->first_index ||
10291da177e4SLinus Torvalds 				     page->index > details->last_index))
10301da177e4SLinus Torvalds 					continue;
10311da177e4SLinus Torvalds 			}
1032b5810039SNick Piggin 			ptent = ptep_get_and_clear_full(mm, addr, pte,
1033a600388dSZachary Amsden 							tlb->fullmm);
10341da177e4SLinus Torvalds 			tlb_remove_tlb_entry(tlb, pte, addr);
10351da177e4SLinus Torvalds 			if (unlikely(!page))
10361da177e4SLinus Torvalds 				continue;
10371da177e4SLinus Torvalds 			if (unlikely(details) && details->nonlinear_vma
10381da177e4SLinus Torvalds 			    && linear_page_index(details->nonlinear_vma,
10391da177e4SLinus Torvalds 						addr) != page->index)
1040b5810039SNick Piggin 				set_pte_at(mm, addr, pte,
10411da177e4SLinus Torvalds 					   pgoff_to_pte(page->index));
10421da177e4SLinus Torvalds 			if (PageAnon(page))
1043d559db08SKAMEZAWA Hiroyuki 				rss[MM_ANONPAGES]--;
10446237bcd9SHugh Dickins 			else {
10456237bcd9SHugh Dickins 				if (pte_dirty(ptent))
10466237bcd9SHugh Dickins 					set_page_dirty(page);
10474917e5d0SJohannes Weiner 				if (pte_young(ptent) &&
10484917e5d0SJohannes Weiner 				    likely(!VM_SequentialReadHint(vma)))
1049bf3f3bc5SNick Piggin 					mark_page_accessed(page);
1050d559db08SKAMEZAWA Hiroyuki 				rss[MM_FILEPAGES]--;
10516237bcd9SHugh Dickins 			}
1052edc315fdSHugh Dickins 			page_remove_rmap(page);
10533dc14741SHugh Dickins 			if (unlikely(page_mapcount(page) < 0))
10543dc14741SHugh Dickins 				print_bad_pte(vma, addr, ptent, page);
1055d16dfc55SPeter Zijlstra 			force_flush = !__tlb_remove_page(tlb, page);
1056d16dfc55SPeter Zijlstra 			if (force_flush)
1057d16dfc55SPeter Zijlstra 				break;
10581da177e4SLinus Torvalds 			continue;
10591da177e4SLinus Torvalds 		}
10601da177e4SLinus Torvalds 		/*
10611da177e4SLinus Torvalds 		 * If details->check_mapping, we leave swap entries;
10621da177e4SLinus Torvalds 		 * if details->nonlinear_vma, we leave file entries.
10631da177e4SLinus Torvalds 		 */
10641da177e4SLinus Torvalds 		if (unlikely(details))
10651da177e4SLinus Torvalds 			continue;
10662509ef26SHugh Dickins 		if (pte_file(ptent)) {
10672509ef26SHugh Dickins 			if (unlikely(!(vma->vm_flags & VM_NONLINEAR)))
10682509ef26SHugh Dickins 				print_bad_pte(vma, addr, ptent, NULL);
1069b084d435SKAMEZAWA Hiroyuki 		} else {
1070b084d435SKAMEZAWA Hiroyuki 			swp_entry_t entry = pte_to_swp_entry(ptent);
1071b084d435SKAMEZAWA Hiroyuki 
1072b084d435SKAMEZAWA Hiroyuki 			if (!non_swap_entry(entry))
1073b084d435SKAMEZAWA Hiroyuki 				rss[MM_SWAPENTS]--;
1074b084d435SKAMEZAWA Hiroyuki 			if (unlikely(!free_swap_and_cache(entry)))
10752509ef26SHugh Dickins 				print_bad_pte(vma, addr, ptent, NULL);
1076b084d435SKAMEZAWA Hiroyuki 		}
10779888a1caSZachary Amsden 		pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
107851c6f666SRobin Holt 	} while (pte++, addr += PAGE_SIZE, (addr != end && *zap_work > 0));
1079ae859762SHugh Dickins 
1080d559db08SKAMEZAWA Hiroyuki 	add_mm_rss_vec(mm, rss);
10816606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
1082508034a3SHugh Dickins 	pte_unmap_unlock(pte - 1, ptl);
108351c6f666SRobin Holt 
1084d16dfc55SPeter Zijlstra 	/*
1085d16dfc55SPeter Zijlstra 	 * mmu_gather ran out of room to batch pages, we break out of
1086d16dfc55SPeter Zijlstra 	 * the PTE lock to avoid doing the potential expensive TLB invalidate
1087d16dfc55SPeter Zijlstra 	 * and page-free while holding it.
1088d16dfc55SPeter Zijlstra 	 */
1089d16dfc55SPeter Zijlstra 	if (force_flush) {
1090d16dfc55SPeter Zijlstra 		force_flush = 0;
1091d16dfc55SPeter Zijlstra 		tlb_flush_mmu(tlb);
1092d16dfc55SPeter Zijlstra 		if (addr != end)
1093d16dfc55SPeter Zijlstra 			goto again;
1094d16dfc55SPeter Zijlstra 	}
1095d16dfc55SPeter Zijlstra 
109651c6f666SRobin Holt 	return addr;
10971da177e4SLinus Torvalds }
10981da177e4SLinus Torvalds 
109951c6f666SRobin Holt static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
1100b5810039SNick Piggin 				struct vm_area_struct *vma, pud_t *pud,
11011da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
110251c6f666SRobin Holt 				long *zap_work, struct zap_details *details)
11031da177e4SLinus Torvalds {
11041da177e4SLinus Torvalds 	pmd_t *pmd;
11051da177e4SLinus Torvalds 	unsigned long next;
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
11081da177e4SLinus Torvalds 	do {
11091da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
111071e3aac0SAndrea Arcangeli 		if (pmd_trans_huge(*pmd)) {
111114d1a55cSAndrea Arcangeli 			if (next-addr != HPAGE_PMD_SIZE) {
111214d1a55cSAndrea Arcangeli 				VM_BUG_ON(!rwsem_is_locked(&tlb->mm->mmap_sem));
111371e3aac0SAndrea Arcangeli 				split_huge_page_pmd(vma->vm_mm, pmd);
111414d1a55cSAndrea Arcangeli 			} else if (zap_huge_pmd(tlb, vma, pmd)) {
111571e3aac0SAndrea Arcangeli 				(*zap_work)--;
111671e3aac0SAndrea Arcangeli 				continue;
111771e3aac0SAndrea Arcangeli 			}
111871e3aac0SAndrea Arcangeli 			/* fall through */
111971e3aac0SAndrea Arcangeli 		}
112051c6f666SRobin Holt 		if (pmd_none_or_clear_bad(pmd)) {
112151c6f666SRobin Holt 			(*zap_work)--;
11221da177e4SLinus Torvalds 			continue;
112351c6f666SRobin Holt 		}
112451c6f666SRobin Holt 		next = zap_pte_range(tlb, vma, pmd, addr, next,
112551c6f666SRobin Holt 						zap_work, details);
112651c6f666SRobin Holt 	} while (pmd++, addr = next, (addr != end && *zap_work > 0));
112751c6f666SRobin Holt 
112851c6f666SRobin Holt 	return addr;
11291da177e4SLinus Torvalds }
11301da177e4SLinus Torvalds 
113151c6f666SRobin Holt static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
1132b5810039SNick Piggin 				struct vm_area_struct *vma, pgd_t *pgd,
11331da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
113451c6f666SRobin Holt 				long *zap_work, struct zap_details *details)
11351da177e4SLinus Torvalds {
11361da177e4SLinus Torvalds 	pud_t *pud;
11371da177e4SLinus Torvalds 	unsigned long next;
11381da177e4SLinus Torvalds 
11391da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
11401da177e4SLinus Torvalds 	do {
11411da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
114251c6f666SRobin Holt 		if (pud_none_or_clear_bad(pud)) {
114351c6f666SRobin Holt 			(*zap_work)--;
11441da177e4SLinus Torvalds 			continue;
114551c6f666SRobin Holt 		}
114651c6f666SRobin Holt 		next = zap_pmd_range(tlb, vma, pud, addr, next,
114751c6f666SRobin Holt 						zap_work, details);
114851c6f666SRobin Holt 	} while (pud++, addr = next, (addr != end && *zap_work > 0));
114951c6f666SRobin Holt 
115051c6f666SRobin Holt 	return addr;
11511da177e4SLinus Torvalds }
11521da177e4SLinus Torvalds 
115351c6f666SRobin Holt static unsigned long unmap_page_range(struct mmu_gather *tlb,
115451c6f666SRobin Holt 				struct vm_area_struct *vma,
11551da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
115651c6f666SRobin Holt 				long *zap_work, struct zap_details *details)
11571da177e4SLinus Torvalds {
11581da177e4SLinus Torvalds 	pgd_t *pgd;
11591da177e4SLinus Torvalds 	unsigned long next;
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds 	if (details && !details->check_mapping && !details->nonlinear_vma)
11621da177e4SLinus Torvalds 		details = NULL;
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 	BUG_ON(addr >= end);
1165569b846dSKAMEZAWA Hiroyuki 	mem_cgroup_uncharge_start();
11661da177e4SLinus Torvalds 	tlb_start_vma(tlb, vma);
11671da177e4SLinus Torvalds 	pgd = pgd_offset(vma->vm_mm, addr);
11681da177e4SLinus Torvalds 	do {
11691da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
117051c6f666SRobin Holt 		if (pgd_none_or_clear_bad(pgd)) {
117151c6f666SRobin Holt 			(*zap_work)--;
11721da177e4SLinus Torvalds 			continue;
117351c6f666SRobin Holt 		}
117451c6f666SRobin Holt 		next = zap_pud_range(tlb, vma, pgd, addr, next,
117551c6f666SRobin Holt 						zap_work, details);
117651c6f666SRobin Holt 	} while (pgd++, addr = next, (addr != end && *zap_work > 0));
11771da177e4SLinus Torvalds 	tlb_end_vma(tlb, vma);
1178569b846dSKAMEZAWA Hiroyuki 	mem_cgroup_uncharge_end();
117951c6f666SRobin Holt 
118051c6f666SRobin Holt 	return addr;
11811da177e4SLinus Torvalds }
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds #ifdef CONFIG_PREEMPT
11841da177e4SLinus Torvalds # define ZAP_BLOCK_SIZE	(8 * PAGE_SIZE)
11851da177e4SLinus Torvalds #else
11861da177e4SLinus Torvalds /* No preempt: go for improved straight-line efficiency */
11871da177e4SLinus Torvalds # define ZAP_BLOCK_SIZE	(1024 * PAGE_SIZE)
11881da177e4SLinus Torvalds #endif
11891da177e4SLinus Torvalds 
11901da177e4SLinus Torvalds /**
11911da177e4SLinus Torvalds  * unmap_vmas - unmap a range of memory covered by a list of vma's
11921da177e4SLinus Torvalds  * @tlbp: address of the caller's struct mmu_gather
11931da177e4SLinus Torvalds  * @vma: the starting vma
11941da177e4SLinus Torvalds  * @start_addr: virtual address at which to start unmapping
11951da177e4SLinus Torvalds  * @end_addr: virtual address at which to end unmapping
11961da177e4SLinus Torvalds  * @nr_accounted: Place number of unmapped pages in vm-accountable vma's here
11971da177e4SLinus Torvalds  * @details: details of nonlinear truncation or shared cache invalidation
11981da177e4SLinus Torvalds  *
1199ee39b37bSHugh Dickins  * Returns the end address of the unmapping (restart addr if interrupted).
12001da177e4SLinus Torvalds  *
1201508034a3SHugh Dickins  * Unmap all pages in the vma list.
12021da177e4SLinus Torvalds  *
1203508034a3SHugh Dickins  * We aim to not hold locks for too long (for scheduling latency reasons).
1204508034a3SHugh Dickins  * So zap pages in ZAP_BLOCK_SIZE bytecounts.  This means we need to
12051da177e4SLinus Torvalds  * return the ending mmu_gather to the caller.
12061da177e4SLinus Torvalds  *
12071da177e4SLinus Torvalds  * Only addresses between `start' and `end' will be unmapped.
12081da177e4SLinus Torvalds  *
12091da177e4SLinus Torvalds  * The VMA list must be sorted in ascending virtual address order.
12101da177e4SLinus Torvalds  *
12111da177e4SLinus Torvalds  * unmap_vmas() assumes that the caller will flush the whole unmapped address
12121da177e4SLinus Torvalds  * range after unmap_vmas() returns.  So the only responsibility here is to
12131da177e4SLinus Torvalds  * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
12141da177e4SLinus Torvalds  * drops the lock and schedules.
12151da177e4SLinus Torvalds  */
1216d16dfc55SPeter Zijlstra unsigned long unmap_vmas(struct mmu_gather *tlb,
12171da177e4SLinus Torvalds 		struct vm_area_struct *vma, unsigned long start_addr,
12181da177e4SLinus Torvalds 		unsigned long end_addr, unsigned long *nr_accounted,
12191da177e4SLinus Torvalds 		struct zap_details *details)
12201da177e4SLinus Torvalds {
122151c6f666SRobin Holt 	long zap_work = ZAP_BLOCK_SIZE;
1222ee39b37bSHugh Dickins 	unsigned long start = start_addr;
12231da177e4SLinus Torvalds 	spinlock_t *i_mmap_lock = details? details->i_mmap_lock: NULL;
1224cddb8a5cSAndrea Arcangeli 	struct mm_struct *mm = vma->vm_mm;
12251da177e4SLinus Torvalds 
1226cddb8a5cSAndrea Arcangeli 	mmu_notifier_invalidate_range_start(mm, start_addr, end_addr);
12271da177e4SLinus Torvalds 	for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) {
12281da177e4SLinus Torvalds 		unsigned long end;
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds 		start = max(vma->vm_start, start_addr);
12311da177e4SLinus Torvalds 		if (start >= vma->vm_end)
12321da177e4SLinus Torvalds 			continue;
12331da177e4SLinus Torvalds 		end = min(vma->vm_end, end_addr);
12341da177e4SLinus Torvalds 		if (end <= vma->vm_start)
12351da177e4SLinus Torvalds 			continue;
12361da177e4SLinus Torvalds 
12371da177e4SLinus Torvalds 		if (vma->vm_flags & VM_ACCOUNT)
12381da177e4SLinus Torvalds 			*nr_accounted += (end - start) >> PAGE_SHIFT;
12391da177e4SLinus Torvalds 
124034801ba9Svenkatesh.pallipadi@intel.com 		if (unlikely(is_pfn_mapping(vma)))
12412ab64037Svenkatesh.pallipadi@intel.com 			untrack_pfn_vma(vma, 0, 0);
12422ab64037Svenkatesh.pallipadi@intel.com 
12431da177e4SLinus Torvalds 		while (start != end) {
124451c6f666SRobin Holt 			if (unlikely(is_vm_hugetlb_page(vma))) {
1245a137e1ccSAndi Kleen 				/*
1246a137e1ccSAndi Kleen 				 * It is undesirable to test vma->vm_file as it
1247a137e1ccSAndi Kleen 				 * should be non-null for valid hugetlb area.
1248a137e1ccSAndi Kleen 				 * However, vm_file will be NULL in the error
1249a137e1ccSAndi Kleen 				 * cleanup path of do_mmap_pgoff. When
1250a137e1ccSAndi Kleen 				 * hugetlbfs ->mmap method fails,
1251a137e1ccSAndi Kleen 				 * do_mmap_pgoff() nullifies vma->vm_file
1252a137e1ccSAndi Kleen 				 * before calling this function to clean up.
1253a137e1ccSAndi Kleen 				 * Since no pte has actually been setup, it is
1254a137e1ccSAndi Kleen 				 * safe to do nothing in this case.
1255a137e1ccSAndi Kleen 				 */
1256a137e1ccSAndi Kleen 				if (vma->vm_file) {
125704f2cbe3SMel Gorman 					unmap_hugepage_range(vma, start, end, NULL);
125851c6f666SRobin Holt 					zap_work -= (end - start) /
1259a5516438SAndi Kleen 					pages_per_huge_page(hstate_vma(vma));
1260a137e1ccSAndi Kleen 				}
1261a137e1ccSAndi Kleen 
126251c6f666SRobin Holt 				start = end;
126351c6f666SRobin Holt 			} else
1264d16dfc55SPeter Zijlstra 				start = unmap_page_range(tlb, vma,
126551c6f666SRobin Holt 						start, end, &zap_work, details);
12661da177e4SLinus Torvalds 
126751c6f666SRobin Holt 			if (zap_work > 0) {
126851c6f666SRobin Holt 				BUG_ON(start != end);
126951c6f666SRobin Holt 				break;
127051c6f666SRobin Holt 			}
12711da177e4SLinus Torvalds 
12721da177e4SLinus Torvalds 			if (need_resched() ||
127395c354feSNick Piggin 				(i_mmap_lock && spin_needbreak(i_mmap_lock))) {
1274d16dfc55SPeter Zijlstra 				if (i_mmap_lock)
12751da177e4SLinus Torvalds 					goto out;
12761da177e4SLinus Torvalds 				cond_resched();
12771da177e4SLinus Torvalds 			}
12781da177e4SLinus Torvalds 
127951c6f666SRobin Holt 			zap_work = ZAP_BLOCK_SIZE;
12801da177e4SLinus Torvalds 		}
12811da177e4SLinus Torvalds 	}
12821da177e4SLinus Torvalds out:
1283cddb8a5cSAndrea Arcangeli 	mmu_notifier_invalidate_range_end(mm, start_addr, end_addr);
1284ee39b37bSHugh Dickins 	return start;	/* which is now the end (or restart) address */
12851da177e4SLinus Torvalds }
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds /**
12881da177e4SLinus Torvalds  * zap_page_range - remove user pages in a given range
12891da177e4SLinus Torvalds  * @vma: vm_area_struct holding the applicable pages
12901da177e4SLinus Torvalds  * @address: starting address of pages to zap
12911da177e4SLinus Torvalds  * @size: number of bytes to zap
12921da177e4SLinus Torvalds  * @details: details of nonlinear truncation or shared cache invalidation
12931da177e4SLinus Torvalds  */
1294ee39b37bSHugh Dickins unsigned long zap_page_range(struct vm_area_struct *vma, unsigned long address,
12951da177e4SLinus Torvalds 		unsigned long size, struct zap_details *details)
12961da177e4SLinus Torvalds {
12971da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
1298d16dfc55SPeter Zijlstra 	struct mmu_gather tlb;
12991da177e4SLinus Torvalds 	unsigned long end = address + size;
13001da177e4SLinus Torvalds 	unsigned long nr_accounted = 0;
13011da177e4SLinus Torvalds 
13021da177e4SLinus Torvalds 	lru_add_drain();
1303d16dfc55SPeter Zijlstra 	tlb_gather_mmu(&tlb, mm, 0);
1304365e9c87SHugh Dickins 	update_hiwater_rss(mm);
1305508034a3SHugh Dickins 	end = unmap_vmas(&tlb, vma, address, end, &nr_accounted, details);
1306d16dfc55SPeter Zijlstra 	tlb_finish_mmu(&tlb, address, end);
1307ee39b37bSHugh Dickins 	return end;
13081da177e4SLinus Torvalds }
13091da177e4SLinus Torvalds 
1310c627f9ccSJack Steiner /**
1311c627f9ccSJack Steiner  * zap_vma_ptes - remove ptes mapping the vma
1312c627f9ccSJack Steiner  * @vma: vm_area_struct holding ptes to be zapped
1313c627f9ccSJack Steiner  * @address: starting address of pages to zap
1314c627f9ccSJack Steiner  * @size: number of bytes to zap
1315c627f9ccSJack Steiner  *
1316c627f9ccSJack Steiner  * This function only unmaps ptes assigned to VM_PFNMAP vmas.
1317c627f9ccSJack Steiner  *
1318c627f9ccSJack Steiner  * The entire address range must be fully contained within the vma.
1319c627f9ccSJack Steiner  *
1320c627f9ccSJack Steiner  * Returns 0 if successful.
1321c627f9ccSJack Steiner  */
1322c627f9ccSJack Steiner int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
1323c627f9ccSJack Steiner 		unsigned long size)
1324c627f9ccSJack Steiner {
1325c627f9ccSJack Steiner 	if (address < vma->vm_start || address + size > vma->vm_end ||
1326c627f9ccSJack Steiner 	    		!(vma->vm_flags & VM_PFNMAP))
1327c627f9ccSJack Steiner 		return -1;
1328c627f9ccSJack Steiner 	zap_page_range(vma, address, size, NULL);
1329c627f9ccSJack Steiner 	return 0;
1330c627f9ccSJack Steiner }
1331c627f9ccSJack Steiner EXPORT_SYMBOL_GPL(zap_vma_ptes);
1332c627f9ccSJack Steiner 
1333142762bdSJohannes Weiner /**
1334142762bdSJohannes Weiner  * follow_page - look up a page descriptor from a user-virtual address
1335142762bdSJohannes Weiner  * @vma: vm_area_struct mapping @address
1336142762bdSJohannes Weiner  * @address: virtual address to look up
1337142762bdSJohannes Weiner  * @flags: flags modifying lookup behaviour
1338142762bdSJohannes Weiner  *
1339142762bdSJohannes Weiner  * @flags can have FOLL_ flags set, defined in <linux/mm.h>
1340142762bdSJohannes Weiner  *
1341142762bdSJohannes Weiner  * Returns the mapped (struct page *), %NULL if no mapping exists, or
1342142762bdSJohannes Weiner  * an error pointer if there is a mapping to something not represented
1343142762bdSJohannes Weiner  * by a page descriptor (see also vm_normal_page()).
13441da177e4SLinus Torvalds  */
13456aab341eSLinus Torvalds struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
1346deceb6cdSHugh Dickins 			unsigned int flags)
13471da177e4SLinus Torvalds {
13481da177e4SLinus Torvalds 	pgd_t *pgd;
13491da177e4SLinus Torvalds 	pud_t *pud;
13501da177e4SLinus Torvalds 	pmd_t *pmd;
13511da177e4SLinus Torvalds 	pte_t *ptep, pte;
1352deceb6cdSHugh Dickins 	spinlock_t *ptl;
13531da177e4SLinus Torvalds 	struct page *page;
13546aab341eSLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
13551da177e4SLinus Torvalds 
1356deceb6cdSHugh Dickins 	page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
1357deceb6cdSHugh Dickins 	if (!IS_ERR(page)) {
1358deceb6cdSHugh Dickins 		BUG_ON(flags & FOLL_GET);
1359deceb6cdSHugh Dickins 		goto out;
1360deceb6cdSHugh Dickins 	}
13611da177e4SLinus Torvalds 
1362deceb6cdSHugh Dickins 	page = NULL;
13631da177e4SLinus Torvalds 	pgd = pgd_offset(mm, address);
13641da177e4SLinus Torvalds 	if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
1365deceb6cdSHugh Dickins 		goto no_page_table;
13661da177e4SLinus Torvalds 
13671da177e4SLinus Torvalds 	pud = pud_offset(pgd, address);
1368ceb86879SAndi Kleen 	if (pud_none(*pud))
1369ceb86879SAndi Kleen 		goto no_page_table;
13708a07651eSHugh Dickins 	if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) {
1371ceb86879SAndi Kleen 		BUG_ON(flags & FOLL_GET);
1372ceb86879SAndi Kleen 		page = follow_huge_pud(mm, address, pud, flags & FOLL_WRITE);
1373ceb86879SAndi Kleen 		goto out;
1374ceb86879SAndi Kleen 	}
1375ceb86879SAndi Kleen 	if (unlikely(pud_bad(*pud)))
1376deceb6cdSHugh Dickins 		goto no_page_table;
13771da177e4SLinus Torvalds 
13781da177e4SLinus Torvalds 	pmd = pmd_offset(pud, address);
1379aeed5fceSHugh Dickins 	if (pmd_none(*pmd))
1380deceb6cdSHugh Dickins 		goto no_page_table;
138171e3aac0SAndrea Arcangeli 	if (pmd_huge(*pmd) && vma->vm_flags & VM_HUGETLB) {
1382deceb6cdSHugh Dickins 		BUG_ON(flags & FOLL_GET);
1383deceb6cdSHugh Dickins 		page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE);
1384deceb6cdSHugh Dickins 		goto out;
1385deceb6cdSHugh Dickins 	}
138671e3aac0SAndrea Arcangeli 	if (pmd_trans_huge(*pmd)) {
1387500d65d4SAndrea Arcangeli 		if (flags & FOLL_SPLIT) {
1388500d65d4SAndrea Arcangeli 			split_huge_page_pmd(mm, pmd);
1389500d65d4SAndrea Arcangeli 			goto split_fallthrough;
1390500d65d4SAndrea Arcangeli 		}
139171e3aac0SAndrea Arcangeli 		spin_lock(&mm->page_table_lock);
139271e3aac0SAndrea Arcangeli 		if (likely(pmd_trans_huge(*pmd))) {
139371e3aac0SAndrea Arcangeli 			if (unlikely(pmd_trans_splitting(*pmd))) {
139471e3aac0SAndrea Arcangeli 				spin_unlock(&mm->page_table_lock);
139571e3aac0SAndrea Arcangeli 				wait_split_huge_page(vma->anon_vma, pmd);
139671e3aac0SAndrea Arcangeli 			} else {
139771e3aac0SAndrea Arcangeli 				page = follow_trans_huge_pmd(mm, address,
139871e3aac0SAndrea Arcangeli 							     pmd, flags);
139971e3aac0SAndrea Arcangeli 				spin_unlock(&mm->page_table_lock);
140071e3aac0SAndrea Arcangeli 				goto out;
140171e3aac0SAndrea Arcangeli 			}
140271e3aac0SAndrea Arcangeli 		} else
140371e3aac0SAndrea Arcangeli 			spin_unlock(&mm->page_table_lock);
140471e3aac0SAndrea Arcangeli 		/* fall through */
140571e3aac0SAndrea Arcangeli 	}
1406500d65d4SAndrea Arcangeli split_fallthrough:
1407aeed5fceSHugh Dickins 	if (unlikely(pmd_bad(*pmd)))
1408aeed5fceSHugh Dickins 		goto no_page_table;
1409aeed5fceSHugh Dickins 
1410deceb6cdSHugh Dickins 	ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
14111da177e4SLinus Torvalds 
14121da177e4SLinus Torvalds 	pte = *ptep;
1413deceb6cdSHugh Dickins 	if (!pte_present(pte))
141489f5b7daSLinus Torvalds 		goto no_page;
1415deceb6cdSHugh Dickins 	if ((flags & FOLL_WRITE) && !pte_write(pte))
1416deceb6cdSHugh Dickins 		goto unlock;
1417a13ea5b7SHugh Dickins 
14186aab341eSLinus Torvalds 	page = vm_normal_page(vma, address, pte);
1419a13ea5b7SHugh Dickins 	if (unlikely(!page)) {
1420a13ea5b7SHugh Dickins 		if ((flags & FOLL_DUMP) ||
142162eede62SHugh Dickins 		    !is_zero_pfn(pte_pfn(pte)))
142289f5b7daSLinus Torvalds 			goto bad_page;
1423a13ea5b7SHugh Dickins 		page = pte_page(pte);
1424a13ea5b7SHugh Dickins 	}
1425deceb6cdSHugh Dickins 
1426deceb6cdSHugh Dickins 	if (flags & FOLL_GET)
1427deceb6cdSHugh Dickins 		get_page(page);
1428deceb6cdSHugh Dickins 	if (flags & FOLL_TOUCH) {
1429deceb6cdSHugh Dickins 		if ((flags & FOLL_WRITE) &&
1430deceb6cdSHugh Dickins 		    !pte_dirty(pte) && !PageDirty(page))
1431f33ea7f4SNick Piggin 			set_page_dirty(page);
1432bd775c42SKOSAKI Motohiro 		/*
1433bd775c42SKOSAKI Motohiro 		 * pte_mkyoung() would be more correct here, but atomic care
1434bd775c42SKOSAKI Motohiro 		 * is needed to avoid losing the dirty bit: it is easier to use
1435bd775c42SKOSAKI Motohiro 		 * mark_page_accessed().
1436bd775c42SKOSAKI Motohiro 		 */
14371da177e4SLinus Torvalds 		mark_page_accessed(page);
14381da177e4SLinus Torvalds 	}
1439a1fde08cSLinus Torvalds 	if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
1440110d74a9SMichel Lespinasse 		/*
1441110d74a9SMichel Lespinasse 		 * The preliminary mapping check is mainly to avoid the
1442110d74a9SMichel Lespinasse 		 * pointless overhead of lock_page on the ZERO_PAGE
1443110d74a9SMichel Lespinasse 		 * which might bounce very badly if there is contention.
1444110d74a9SMichel Lespinasse 		 *
1445110d74a9SMichel Lespinasse 		 * If the page is already locked, we don't need to
1446110d74a9SMichel Lespinasse 		 * handle it now - vmscan will handle it later if and
1447110d74a9SMichel Lespinasse 		 * when it attempts to reclaim the page.
1448110d74a9SMichel Lespinasse 		 */
1449110d74a9SMichel Lespinasse 		if (page->mapping && trylock_page(page)) {
1450110d74a9SMichel Lespinasse 			lru_add_drain();  /* push cached pages to LRU */
1451110d74a9SMichel Lespinasse 			/*
1452110d74a9SMichel Lespinasse 			 * Because we lock page here and migration is
1453110d74a9SMichel Lespinasse 			 * blocked by the pte's page reference, we need
1454110d74a9SMichel Lespinasse 			 * only check for file-cache page truncation.
1455110d74a9SMichel Lespinasse 			 */
1456110d74a9SMichel Lespinasse 			if (page->mapping)
1457110d74a9SMichel Lespinasse 				mlock_vma_page(page);
1458110d74a9SMichel Lespinasse 			unlock_page(page);
1459110d74a9SMichel Lespinasse 		}
1460110d74a9SMichel Lespinasse 	}
1461deceb6cdSHugh Dickins unlock:
1462deceb6cdSHugh Dickins 	pte_unmap_unlock(ptep, ptl);
14631da177e4SLinus Torvalds out:
1464deceb6cdSHugh Dickins 	return page;
1465deceb6cdSHugh Dickins 
146689f5b7daSLinus Torvalds bad_page:
146789f5b7daSLinus Torvalds 	pte_unmap_unlock(ptep, ptl);
146889f5b7daSLinus Torvalds 	return ERR_PTR(-EFAULT);
146989f5b7daSLinus Torvalds 
147089f5b7daSLinus Torvalds no_page:
147189f5b7daSLinus Torvalds 	pte_unmap_unlock(ptep, ptl);
147289f5b7daSLinus Torvalds 	if (!pte_none(pte))
147389f5b7daSLinus Torvalds 		return page;
14748e4b9a60SHugh Dickins 
1475deceb6cdSHugh Dickins no_page_table:
1476deceb6cdSHugh Dickins 	/*
1477deceb6cdSHugh Dickins 	 * When core dumping an enormous anonymous area that nobody
14788e4b9a60SHugh Dickins 	 * has touched so far, we don't want to allocate unnecessary pages or
14798e4b9a60SHugh Dickins 	 * page tables.  Return error instead of NULL to skip handle_mm_fault,
14808e4b9a60SHugh Dickins 	 * then get_dump_page() will return NULL to leave a hole in the dump.
14818e4b9a60SHugh Dickins 	 * But we can only make this optimization where a hole would surely
14828e4b9a60SHugh Dickins 	 * be zero-filled if handle_mm_fault() actually did handle it.
1483deceb6cdSHugh Dickins 	 */
14848e4b9a60SHugh Dickins 	if ((flags & FOLL_DUMP) &&
14858e4b9a60SHugh Dickins 	    (!vma->vm_ops || !vma->vm_ops->fault))
14868e4b9a60SHugh Dickins 		return ERR_PTR(-EFAULT);
1487deceb6cdSHugh Dickins 	return page;
14881da177e4SLinus Torvalds }
14891da177e4SLinus Torvalds 
149095042f9eSLinus Torvalds static inline int stack_guard_page(struct vm_area_struct *vma, unsigned long addr)
149195042f9eSLinus Torvalds {
1492a09a79f6SMikulas Patocka 	return stack_guard_page_start(vma, addr) ||
1493a09a79f6SMikulas Patocka 	       stack_guard_page_end(vma, addr+PAGE_SIZE);
149495042f9eSLinus Torvalds }
149595042f9eSLinus Torvalds 
14960014bd99SHuang Ying /**
14970014bd99SHuang Ying  * __get_user_pages() - pin user pages in memory
14980014bd99SHuang Ying  * @tsk:	task_struct of target task
14990014bd99SHuang Ying  * @mm:		mm_struct of target mm
15000014bd99SHuang Ying  * @start:	starting user address
15010014bd99SHuang Ying  * @nr_pages:	number of pages from start to pin
15020014bd99SHuang Ying  * @gup_flags:	flags modifying pin behaviour
15030014bd99SHuang Ying  * @pages:	array that receives pointers to the pages pinned.
15040014bd99SHuang Ying  *		Should be at least nr_pages long. Or NULL, if caller
15050014bd99SHuang Ying  *		only intends to ensure the pages are faulted in.
15060014bd99SHuang Ying  * @vmas:	array of pointers to vmas corresponding to each page.
15070014bd99SHuang Ying  *		Or NULL if the caller does not require them.
15080014bd99SHuang Ying  * @nonblocking: whether waiting for disk IO or mmap_sem contention
15090014bd99SHuang Ying  *
15100014bd99SHuang Ying  * Returns number of pages pinned. This may be fewer than the number
15110014bd99SHuang Ying  * requested. If nr_pages is 0 or negative, returns 0. If no pages
15120014bd99SHuang Ying  * were pinned, returns -errno. Each page returned must be released
15130014bd99SHuang Ying  * with a put_page() call when it is finished with. vmas will only
15140014bd99SHuang Ying  * remain valid while mmap_sem is held.
15150014bd99SHuang Ying  *
15160014bd99SHuang Ying  * Must be called with mmap_sem held for read or write.
15170014bd99SHuang Ying  *
15180014bd99SHuang Ying  * __get_user_pages walks a process's page tables and takes a reference to
15190014bd99SHuang Ying  * each struct page that each user address corresponds to at a given
15200014bd99SHuang Ying  * instant. That is, it takes the page that would be accessed if a user
15210014bd99SHuang Ying  * thread accesses the given user virtual address at that instant.
15220014bd99SHuang Ying  *
15230014bd99SHuang Ying  * This does not guarantee that the page exists in the user mappings when
15240014bd99SHuang Ying  * __get_user_pages returns, and there may even be a completely different
15250014bd99SHuang Ying  * page there in some cases (eg. if mmapped pagecache has been invalidated
15260014bd99SHuang Ying  * and subsequently re faulted). However it does guarantee that the page
15270014bd99SHuang Ying  * won't be freed completely. And mostly callers simply care that the page
15280014bd99SHuang Ying  * contains data that was valid *at some point in time*. Typically, an IO
15290014bd99SHuang Ying  * or similar operation cannot guarantee anything stronger anyway because
15300014bd99SHuang Ying  * locks can't be held over the syscall boundary.
15310014bd99SHuang Ying  *
15320014bd99SHuang Ying  * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
15330014bd99SHuang Ying  * the page is written to, set_page_dirty (or set_page_dirty_lock, as
15340014bd99SHuang Ying  * appropriate) must be called after the page is finished with, and
15350014bd99SHuang Ying  * before put_page is called.
15360014bd99SHuang Ying  *
15370014bd99SHuang Ying  * If @nonblocking != NULL, __get_user_pages will not wait for disk IO
15380014bd99SHuang Ying  * or mmap_sem contention, and if waiting is needed to pin all pages,
15390014bd99SHuang Ying  * *@nonblocking will be set to 0.
15400014bd99SHuang Ying  *
15410014bd99SHuang Ying  * In most cases, get_user_pages or get_user_pages_fast should be used
15420014bd99SHuang Ying  * instead of __get_user_pages. __get_user_pages should be used only if
15430014bd99SHuang Ying  * you need some special @gup_flags.
15440014bd99SHuang Ying  */
1545b291f000SNick Piggin int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
154658fa879eSHugh Dickins 		     unsigned long start, int nr_pages, unsigned int gup_flags,
154753a7706dSMichel Lespinasse 		     struct page **pages, struct vm_area_struct **vmas,
154853a7706dSMichel Lespinasse 		     int *nonblocking)
15491da177e4SLinus Torvalds {
15501da177e4SLinus Torvalds 	int i;
155158fa879eSHugh Dickins 	unsigned long vm_flags;
15521da177e4SLinus Torvalds 
15539d73777eSPeter Zijlstra 	if (nr_pages <= 0)
1554900cf086SJonathan Corbet 		return 0;
155558fa879eSHugh Dickins 
155658fa879eSHugh Dickins 	VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET));
155758fa879eSHugh Dickins 
15581da177e4SLinus Torvalds 	/*
15591da177e4SLinus Torvalds 	 * Require read or write permissions.
156058fa879eSHugh Dickins 	 * If FOLL_FORCE is set, we only require the "MAY" flags.
15611da177e4SLinus Torvalds 	 */
156258fa879eSHugh Dickins 	vm_flags  = (gup_flags & FOLL_WRITE) ?
156358fa879eSHugh Dickins 			(VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
156458fa879eSHugh Dickins 	vm_flags &= (gup_flags & FOLL_FORCE) ?
156558fa879eSHugh Dickins 			(VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
15661da177e4SLinus Torvalds 	i = 0;
15671da177e4SLinus Torvalds 
15681da177e4SLinus Torvalds 	do {
15691da177e4SLinus Torvalds 		struct vm_area_struct *vma;
15701da177e4SLinus Torvalds 
15711da177e4SLinus Torvalds 		vma = find_extend_vma(mm, start);
1572e7f22e20SStephen Wilson 		if (!vma && in_gate_area(mm, start)) {
15731da177e4SLinus Torvalds 			unsigned long pg = start & PAGE_MASK;
15741da177e4SLinus Torvalds 			pgd_t *pgd;
15751da177e4SLinus Torvalds 			pud_t *pud;
15761da177e4SLinus Torvalds 			pmd_t *pmd;
15771da177e4SLinus Torvalds 			pte_t *pte;
1578b291f000SNick Piggin 
1579b291f000SNick Piggin 			/* user gate pages are read-only */
158058fa879eSHugh Dickins 			if (gup_flags & FOLL_WRITE)
15811da177e4SLinus Torvalds 				return i ? : -EFAULT;
15821da177e4SLinus Torvalds 			if (pg > TASK_SIZE)
15831da177e4SLinus Torvalds 				pgd = pgd_offset_k(pg);
15841da177e4SLinus Torvalds 			else
15851da177e4SLinus Torvalds 				pgd = pgd_offset_gate(mm, pg);
15861da177e4SLinus Torvalds 			BUG_ON(pgd_none(*pgd));
15871da177e4SLinus Torvalds 			pud = pud_offset(pgd, pg);
15881da177e4SLinus Torvalds 			BUG_ON(pud_none(*pud));
15891da177e4SLinus Torvalds 			pmd = pmd_offset(pud, pg);
1590690dbe1cSHugh Dickins 			if (pmd_none(*pmd))
1591690dbe1cSHugh Dickins 				return i ? : -EFAULT;
1592f66055abSAndrea Arcangeli 			VM_BUG_ON(pmd_trans_huge(*pmd));
15931da177e4SLinus Torvalds 			pte = pte_offset_map(pmd, pg);
1594690dbe1cSHugh Dickins 			if (pte_none(*pte)) {
1595690dbe1cSHugh Dickins 				pte_unmap(pte);
1596690dbe1cSHugh Dickins 				return i ? : -EFAULT;
1597690dbe1cSHugh Dickins 			}
159895042f9eSLinus Torvalds 			vma = get_gate_vma(mm);
15991da177e4SLinus Torvalds 			if (pages) {
1600de51257aSHugh Dickins 				struct page *page;
1601de51257aSHugh Dickins 
160295042f9eSLinus Torvalds 				page = vm_normal_page(vma, start, *pte);
1603de51257aSHugh Dickins 				if (!page) {
1604de51257aSHugh Dickins 					if (!(gup_flags & FOLL_DUMP) &&
1605de51257aSHugh Dickins 					     is_zero_pfn(pte_pfn(*pte)))
1606de51257aSHugh Dickins 						page = pte_page(*pte);
1607de51257aSHugh Dickins 					else {
1608de51257aSHugh Dickins 						pte_unmap(pte);
1609de51257aSHugh Dickins 						return i ? : -EFAULT;
1610de51257aSHugh Dickins 					}
1611de51257aSHugh Dickins 				}
16126aab341eSLinus Torvalds 				pages[i] = page;
16136aab341eSLinus Torvalds 				get_page(page);
16141da177e4SLinus Torvalds 			}
16151da177e4SLinus Torvalds 			pte_unmap(pte);
161695042f9eSLinus Torvalds 			goto next_page;
16171da177e4SLinus Torvalds 		}
16181da177e4SLinus Torvalds 
1619b291f000SNick Piggin 		if (!vma ||
1620b291f000SNick Piggin 		    (vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
16211c3aff1cSHugh Dickins 		    !(vm_flags & vma->vm_flags))
16221da177e4SLinus Torvalds 			return i ? : -EFAULT;
16231da177e4SLinus Torvalds 
16242a15efc9SHugh Dickins 		if (is_vm_hugetlb_page(vma)) {
16252a15efc9SHugh Dickins 			i = follow_hugetlb_page(mm, vma, pages, vmas,
162658fa879eSHugh Dickins 					&start, &nr_pages, i, gup_flags);
16272a15efc9SHugh Dickins 			continue;
16282a15efc9SHugh Dickins 		}
1629deceb6cdSHugh Dickins 
16301da177e4SLinus Torvalds 		do {
163108ef4729SHugh Dickins 			struct page *page;
163258fa879eSHugh Dickins 			unsigned int foll_flags = gup_flags;
16331da177e4SLinus Torvalds 
1634462e00ccSEthan Solomita 			/*
16354779280dSYing Han 			 * If we have a pending SIGKILL, don't keep faulting
16361c3aff1cSHugh Dickins 			 * pages and potentially allocating memory.
1637462e00ccSEthan Solomita 			 */
16381c3aff1cSHugh Dickins 			if (unlikely(fatal_signal_pending(current)))
16394779280dSYing Han 				return i ? i : -ERESTARTSYS;
1640462e00ccSEthan Solomita 
1641deceb6cdSHugh Dickins 			cond_resched();
16426aab341eSLinus Torvalds 			while (!(page = follow_page(vma, start, foll_flags))) {
1643a68d2ebcSLinus Torvalds 				int ret;
164453a7706dSMichel Lespinasse 				unsigned int fault_flags = 0;
164553a7706dSMichel Lespinasse 
1646a09a79f6SMikulas Patocka 				/* For mlock, just skip the stack guard page. */
1647a09a79f6SMikulas Patocka 				if (foll_flags & FOLL_MLOCK) {
1648a09a79f6SMikulas Patocka 					if (stack_guard_page(vma, start))
1649a09a79f6SMikulas Patocka 						goto next_page;
1650a09a79f6SMikulas Patocka 				}
165153a7706dSMichel Lespinasse 				if (foll_flags & FOLL_WRITE)
165253a7706dSMichel Lespinasse 					fault_flags |= FAULT_FLAG_WRITE;
165353a7706dSMichel Lespinasse 				if (nonblocking)
165453a7706dSMichel Lespinasse 					fault_flags |= FAULT_FLAG_ALLOW_RETRY;
1655318b275fSGleb Natapov 				if (foll_flags & FOLL_NOWAIT)
1656318b275fSGleb Natapov 					fault_flags |= (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT);
1657d06063ccSLinus Torvalds 
1658d26ed650SHugh Dickins 				ret = handle_mm_fault(mm, vma, start,
165953a7706dSMichel Lespinasse 							fault_flags);
1660d26ed650SHugh Dickins 
166183c54070SNick Piggin 				if (ret & VM_FAULT_ERROR) {
166283c54070SNick Piggin 					if (ret & VM_FAULT_OOM)
166383c54070SNick Piggin 						return i ? i : -ENOMEM;
166469ebb83eSHuang Ying 					if (ret & (VM_FAULT_HWPOISON |
166569ebb83eSHuang Ying 						   VM_FAULT_HWPOISON_LARGE)) {
166669ebb83eSHuang Ying 						if (i)
166769ebb83eSHuang Ying 							return i;
166869ebb83eSHuang Ying 						else if (gup_flags & FOLL_HWPOISON)
166969ebb83eSHuang Ying 							return -EHWPOISON;
167069ebb83eSHuang Ying 						else
167169ebb83eSHuang Ying 							return -EFAULT;
167269ebb83eSHuang Ying 					}
167369ebb83eSHuang Ying 					if (ret & VM_FAULT_SIGBUS)
167483c54070SNick Piggin 						return i ? i : -EFAULT;
167583c54070SNick Piggin 					BUG();
167683c54070SNick Piggin 				}
1677e7f22e20SStephen Wilson 
1678e7f22e20SStephen Wilson 				if (tsk) {
167983c54070SNick Piggin 					if (ret & VM_FAULT_MAJOR)
168083c54070SNick Piggin 						tsk->maj_flt++;
168183c54070SNick Piggin 					else
168283c54070SNick Piggin 						tsk->min_flt++;
1683e7f22e20SStephen Wilson 				}
168483c54070SNick Piggin 
168553a7706dSMichel Lespinasse 				if (ret & VM_FAULT_RETRY) {
1686318b275fSGleb Natapov 					if (nonblocking)
168753a7706dSMichel Lespinasse 						*nonblocking = 0;
168853a7706dSMichel Lespinasse 					return i;
168953a7706dSMichel Lespinasse 				}
169053a7706dSMichel Lespinasse 
1691f33ea7f4SNick Piggin 				/*
169283c54070SNick Piggin 				 * The VM_FAULT_WRITE bit tells us that
169383c54070SNick Piggin 				 * do_wp_page has broken COW when necessary,
169483c54070SNick Piggin 				 * even if maybe_mkwrite decided not to set
169583c54070SNick Piggin 				 * pte_write. We can thus safely do subsequent
1696878b63acSHugh Dickins 				 * page lookups as if they were reads. But only
1697878b63acSHugh Dickins 				 * do so when looping for pte_write is futile:
1698878b63acSHugh Dickins 				 * in some cases userspace may also be wanting
1699878b63acSHugh Dickins 				 * to write to the gotten user page, which a
1700878b63acSHugh Dickins 				 * read fault here might prevent (a readonly
1701878b63acSHugh Dickins 				 * page might get reCOWed by userspace write).
1702f33ea7f4SNick Piggin 				 */
1703878b63acSHugh Dickins 				if ((ret & VM_FAULT_WRITE) &&
1704878b63acSHugh Dickins 				    !(vma->vm_flags & VM_WRITE))
1705deceb6cdSHugh Dickins 					foll_flags &= ~FOLL_WRITE;
1706a68d2ebcSLinus Torvalds 
17077f7bbbe5SBenjamin Herrenschmidt 				cond_resched();
17081da177e4SLinus Torvalds 			}
170989f5b7daSLinus Torvalds 			if (IS_ERR(page))
171089f5b7daSLinus Torvalds 				return i ? i : PTR_ERR(page);
17111da177e4SLinus Torvalds 			if (pages) {
171208ef4729SHugh Dickins 				pages[i] = page;
171303beb076SJames Bottomley 
1714a6f36be3SRussell King 				flush_anon_page(vma, page, start);
171508ef4729SHugh Dickins 				flush_dcache_page(page);
17161da177e4SLinus Torvalds 			}
171795042f9eSLinus Torvalds next_page:
17181da177e4SLinus Torvalds 			if (vmas)
17191da177e4SLinus Torvalds 				vmas[i] = vma;
17201da177e4SLinus Torvalds 			i++;
17211da177e4SLinus Torvalds 			start += PAGE_SIZE;
17229d73777eSPeter Zijlstra 			nr_pages--;
17239d73777eSPeter Zijlstra 		} while (nr_pages && start < vma->vm_end);
17249d73777eSPeter Zijlstra 	} while (nr_pages);
17251da177e4SLinus Torvalds 	return i;
17261da177e4SLinus Torvalds }
17270014bd99SHuang Ying EXPORT_SYMBOL(__get_user_pages);
1728b291f000SNick Piggin 
1729d2bf6be8SNick Piggin /**
1730d2bf6be8SNick Piggin  * get_user_pages() - pin user pages in memory
1731e7f22e20SStephen Wilson  * @tsk:	the task_struct to use for page fault accounting, or
1732e7f22e20SStephen Wilson  *		NULL if faults are not to be recorded.
1733d2bf6be8SNick Piggin  * @mm:		mm_struct of target mm
1734d2bf6be8SNick Piggin  * @start:	starting user address
17359d73777eSPeter Zijlstra  * @nr_pages:	number of pages from start to pin
1736d2bf6be8SNick Piggin  * @write:	whether pages will be written to by the caller
1737d2bf6be8SNick Piggin  * @force:	whether to force write access even if user mapping is
1738d2bf6be8SNick Piggin  *		readonly. This will result in the page being COWed even
1739d2bf6be8SNick Piggin  *		in MAP_SHARED mappings. You do not want this.
1740d2bf6be8SNick Piggin  * @pages:	array that receives pointers to the pages pinned.
1741d2bf6be8SNick Piggin  *		Should be at least nr_pages long. Or NULL, if caller
1742d2bf6be8SNick Piggin  *		only intends to ensure the pages are faulted in.
1743d2bf6be8SNick Piggin  * @vmas:	array of pointers to vmas corresponding to each page.
1744d2bf6be8SNick Piggin  *		Or NULL if the caller does not require them.
1745d2bf6be8SNick Piggin  *
1746d2bf6be8SNick Piggin  * Returns number of pages pinned. This may be fewer than the number
17479d73777eSPeter Zijlstra  * requested. If nr_pages is 0 or negative, returns 0. If no pages
1748d2bf6be8SNick Piggin  * were pinned, returns -errno. Each page returned must be released
1749d2bf6be8SNick Piggin  * with a put_page() call when it is finished with. vmas will only
1750d2bf6be8SNick Piggin  * remain valid while mmap_sem is held.
1751d2bf6be8SNick Piggin  *
1752d2bf6be8SNick Piggin  * Must be called with mmap_sem held for read or write.
1753d2bf6be8SNick Piggin  *
1754d2bf6be8SNick Piggin  * get_user_pages walks a process's page tables and takes a reference to
1755d2bf6be8SNick Piggin  * each struct page that each user address corresponds to at a given
1756d2bf6be8SNick Piggin  * instant. That is, it takes the page that would be accessed if a user
1757d2bf6be8SNick Piggin  * thread accesses the given user virtual address at that instant.
1758d2bf6be8SNick Piggin  *
1759d2bf6be8SNick Piggin  * This does not guarantee that the page exists in the user mappings when
1760d2bf6be8SNick Piggin  * get_user_pages returns, and there may even be a completely different
1761d2bf6be8SNick Piggin  * page there in some cases (eg. if mmapped pagecache has been invalidated
1762d2bf6be8SNick Piggin  * and subsequently re faulted). However it does guarantee that the page
1763d2bf6be8SNick Piggin  * won't be freed completely. And mostly callers simply care that the page
1764d2bf6be8SNick Piggin  * contains data that was valid *at some point in time*. Typically, an IO
1765d2bf6be8SNick Piggin  * or similar operation cannot guarantee anything stronger anyway because
1766d2bf6be8SNick Piggin  * locks can't be held over the syscall boundary.
1767d2bf6be8SNick Piggin  *
1768d2bf6be8SNick Piggin  * If write=0, the page must not be written to. If the page is written to,
1769d2bf6be8SNick Piggin  * set_page_dirty (or set_page_dirty_lock, as appropriate) must be called
1770d2bf6be8SNick Piggin  * after the page is finished with, and before put_page is called.
1771d2bf6be8SNick Piggin  *
1772d2bf6be8SNick Piggin  * get_user_pages is typically used for fewer-copy IO operations, to get a
1773d2bf6be8SNick Piggin  * handle on the memory by some means other than accesses via the user virtual
1774d2bf6be8SNick Piggin  * addresses. The pages may be submitted for DMA to devices or accessed via
1775d2bf6be8SNick Piggin  * their kernel linear mapping (via the kmap APIs). Care should be taken to
1776d2bf6be8SNick Piggin  * use the correct cache flushing APIs.
1777d2bf6be8SNick Piggin  *
1778d2bf6be8SNick Piggin  * See also get_user_pages_fast, for performance critical applications.
1779d2bf6be8SNick Piggin  */
1780b291f000SNick Piggin int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
17819d73777eSPeter Zijlstra 		unsigned long start, int nr_pages, int write, int force,
1782b291f000SNick Piggin 		struct page **pages, struct vm_area_struct **vmas)
1783b291f000SNick Piggin {
178458fa879eSHugh Dickins 	int flags = FOLL_TOUCH;
1785b291f000SNick Piggin 
178658fa879eSHugh Dickins 	if (pages)
178758fa879eSHugh Dickins 		flags |= FOLL_GET;
1788b291f000SNick Piggin 	if (write)
178958fa879eSHugh Dickins 		flags |= FOLL_WRITE;
1790b291f000SNick Piggin 	if (force)
179158fa879eSHugh Dickins 		flags |= FOLL_FORCE;
1792b291f000SNick Piggin 
179353a7706dSMichel Lespinasse 	return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas,
179453a7706dSMichel Lespinasse 				NULL);
1795b291f000SNick Piggin }
17961da177e4SLinus Torvalds EXPORT_SYMBOL(get_user_pages);
17971da177e4SLinus Torvalds 
1798f3e8fccdSHugh Dickins /**
1799f3e8fccdSHugh Dickins  * get_dump_page() - pin user page in memory while writing it to core dump
1800f3e8fccdSHugh Dickins  * @addr: user address
1801f3e8fccdSHugh Dickins  *
1802f3e8fccdSHugh Dickins  * Returns struct page pointer of user page pinned for dump,
1803f3e8fccdSHugh Dickins  * to be freed afterwards by page_cache_release() or put_page().
1804f3e8fccdSHugh Dickins  *
1805f3e8fccdSHugh Dickins  * Returns NULL on any kind of failure - a hole must then be inserted into
1806f3e8fccdSHugh Dickins  * the corefile, to preserve alignment with its headers; and also returns
1807f3e8fccdSHugh Dickins  * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
1808f3e8fccdSHugh Dickins  * allowing a hole to be left in the corefile to save diskspace.
1809f3e8fccdSHugh Dickins  *
1810f3e8fccdSHugh Dickins  * Called without mmap_sem, but after all other threads have been killed.
1811f3e8fccdSHugh Dickins  */
1812f3e8fccdSHugh Dickins #ifdef CONFIG_ELF_CORE
1813f3e8fccdSHugh Dickins struct page *get_dump_page(unsigned long addr)
1814f3e8fccdSHugh Dickins {
1815f3e8fccdSHugh Dickins 	struct vm_area_struct *vma;
1816f3e8fccdSHugh Dickins 	struct page *page;
1817f3e8fccdSHugh Dickins 
1818f3e8fccdSHugh Dickins 	if (__get_user_pages(current, current->mm, addr, 1,
181953a7706dSMichel Lespinasse 			     FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma,
182053a7706dSMichel Lespinasse 			     NULL) < 1)
1821f3e8fccdSHugh Dickins 		return NULL;
1822f3e8fccdSHugh Dickins 	flush_cache_page(vma, addr, page_to_pfn(page));
1823f3e8fccdSHugh Dickins 	return page;
1824f3e8fccdSHugh Dickins }
1825f3e8fccdSHugh Dickins #endif /* CONFIG_ELF_CORE */
1826f3e8fccdSHugh Dickins 
182725ca1d6cSNamhyung Kim pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
1828920c7a5dSHarvey Harrison 			spinlock_t **ptl)
1829c9cfcddfSLinus Torvalds {
1830c9cfcddfSLinus Torvalds 	pgd_t * pgd = pgd_offset(mm, addr);
1831c9cfcddfSLinus Torvalds 	pud_t * pud = pud_alloc(mm, pgd, addr);
1832c9cfcddfSLinus Torvalds 	if (pud) {
183349c91fb0STrond Myklebust 		pmd_t * pmd = pmd_alloc(mm, pud, addr);
1834f66055abSAndrea Arcangeli 		if (pmd) {
1835f66055abSAndrea Arcangeli 			VM_BUG_ON(pmd_trans_huge(*pmd));
1836c9cfcddfSLinus Torvalds 			return pte_alloc_map_lock(mm, pmd, addr, ptl);
1837c9cfcddfSLinus Torvalds 		}
1838f66055abSAndrea Arcangeli 	}
1839c9cfcddfSLinus Torvalds 	return NULL;
1840c9cfcddfSLinus Torvalds }
1841c9cfcddfSLinus Torvalds 
18421da177e4SLinus Torvalds /*
1843238f58d8SLinus Torvalds  * This is the old fallback for page remapping.
1844238f58d8SLinus Torvalds  *
1845238f58d8SLinus Torvalds  * For historical reasons, it only allows reserved pages. Only
1846238f58d8SLinus Torvalds  * old drivers should use this, and they needed to mark their
1847238f58d8SLinus Torvalds  * pages reserved for the old functions anyway.
1848238f58d8SLinus Torvalds  */
1849423bad60SNick Piggin static int insert_page(struct vm_area_struct *vma, unsigned long addr,
1850423bad60SNick Piggin 			struct page *page, pgprot_t prot)
1851238f58d8SLinus Torvalds {
1852423bad60SNick Piggin 	struct mm_struct *mm = vma->vm_mm;
1853238f58d8SLinus Torvalds 	int retval;
1854238f58d8SLinus Torvalds 	pte_t *pte;
1855238f58d8SLinus Torvalds 	spinlock_t *ptl;
1856238f58d8SLinus Torvalds 
1857238f58d8SLinus Torvalds 	retval = -EINVAL;
1858a145dd41SLinus Torvalds 	if (PageAnon(page))
18595b4e655eSKAMEZAWA Hiroyuki 		goto out;
1860238f58d8SLinus Torvalds 	retval = -ENOMEM;
1861238f58d8SLinus Torvalds 	flush_dcache_page(page);
1862c9cfcddfSLinus Torvalds 	pte = get_locked_pte(mm, addr, &ptl);
1863238f58d8SLinus Torvalds 	if (!pte)
18645b4e655eSKAMEZAWA Hiroyuki 		goto out;
1865238f58d8SLinus Torvalds 	retval = -EBUSY;
1866238f58d8SLinus Torvalds 	if (!pte_none(*pte))
1867238f58d8SLinus Torvalds 		goto out_unlock;
1868238f58d8SLinus Torvalds 
1869238f58d8SLinus Torvalds 	/* Ok, finally just insert the thing.. */
1870238f58d8SLinus Torvalds 	get_page(page);
187134e55232SKAMEZAWA Hiroyuki 	inc_mm_counter_fast(mm, MM_FILEPAGES);
1872238f58d8SLinus Torvalds 	page_add_file_rmap(page);
1873238f58d8SLinus Torvalds 	set_pte_at(mm, addr, pte, mk_pte(page, prot));
1874238f58d8SLinus Torvalds 
1875238f58d8SLinus Torvalds 	retval = 0;
18768a9f3ccdSBalbir Singh 	pte_unmap_unlock(pte, ptl);
18778a9f3ccdSBalbir Singh 	return retval;
1878238f58d8SLinus Torvalds out_unlock:
1879238f58d8SLinus Torvalds 	pte_unmap_unlock(pte, ptl);
1880238f58d8SLinus Torvalds out:
1881238f58d8SLinus Torvalds 	return retval;
1882238f58d8SLinus Torvalds }
1883238f58d8SLinus Torvalds 
1884bfa5bf6dSRolf Eike Beer /**
1885bfa5bf6dSRolf Eike Beer  * vm_insert_page - insert single page into user vma
1886bfa5bf6dSRolf Eike Beer  * @vma: user vma to map to
1887bfa5bf6dSRolf Eike Beer  * @addr: target user address of this page
1888bfa5bf6dSRolf Eike Beer  * @page: source kernel page
1889bfa5bf6dSRolf Eike Beer  *
1890a145dd41SLinus Torvalds  * This allows drivers to insert individual pages they've allocated
1891a145dd41SLinus Torvalds  * into a user vma.
1892a145dd41SLinus Torvalds  *
1893a145dd41SLinus Torvalds  * The page has to be a nice clean _individual_ kernel allocation.
1894a145dd41SLinus Torvalds  * If you allocate a compound page, you need to have marked it as
1895a145dd41SLinus Torvalds  * such (__GFP_COMP), or manually just split the page up yourself
18968dfcc9baSNick Piggin  * (see split_page()).
1897a145dd41SLinus Torvalds  *
1898a145dd41SLinus Torvalds  * NOTE! Traditionally this was done with "remap_pfn_range()" which
1899a145dd41SLinus Torvalds  * took an arbitrary page protection parameter. This doesn't allow
1900a145dd41SLinus Torvalds  * that. Your vma protection will have to be set up correctly, which
1901a145dd41SLinus Torvalds  * means that if you want a shared writable mapping, you'd better
1902a145dd41SLinus Torvalds  * ask for a shared writable mapping!
1903a145dd41SLinus Torvalds  *
1904a145dd41SLinus Torvalds  * The page does not need to be reserved.
1905a145dd41SLinus Torvalds  */
1906423bad60SNick Piggin int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
1907423bad60SNick Piggin 			struct page *page)
1908a145dd41SLinus Torvalds {
1909a145dd41SLinus Torvalds 	if (addr < vma->vm_start || addr >= vma->vm_end)
1910a145dd41SLinus Torvalds 		return -EFAULT;
1911a145dd41SLinus Torvalds 	if (!page_count(page))
1912a145dd41SLinus Torvalds 		return -EINVAL;
19134d7672b4SLinus Torvalds 	vma->vm_flags |= VM_INSERTPAGE;
1914423bad60SNick Piggin 	return insert_page(vma, addr, page, vma->vm_page_prot);
1915a145dd41SLinus Torvalds }
1916e3c3374fSLinus Torvalds EXPORT_SYMBOL(vm_insert_page);
1917a145dd41SLinus Torvalds 
1918423bad60SNick Piggin static int insert_pfn(struct vm_area_struct *vma, unsigned long addr,
1919423bad60SNick Piggin 			unsigned long pfn, pgprot_t prot)
1920423bad60SNick Piggin {
1921423bad60SNick Piggin 	struct mm_struct *mm = vma->vm_mm;
1922423bad60SNick Piggin 	int retval;
1923423bad60SNick Piggin 	pte_t *pte, entry;
1924423bad60SNick Piggin 	spinlock_t *ptl;
1925423bad60SNick Piggin 
1926423bad60SNick Piggin 	retval = -ENOMEM;
1927423bad60SNick Piggin 	pte = get_locked_pte(mm, addr, &ptl);
1928423bad60SNick Piggin 	if (!pte)
1929423bad60SNick Piggin 		goto out;
1930423bad60SNick Piggin 	retval = -EBUSY;
1931423bad60SNick Piggin 	if (!pte_none(*pte))
1932423bad60SNick Piggin 		goto out_unlock;
1933423bad60SNick Piggin 
1934423bad60SNick Piggin 	/* Ok, finally just insert the thing.. */
1935423bad60SNick Piggin 	entry = pte_mkspecial(pfn_pte(pfn, prot));
1936423bad60SNick Piggin 	set_pte_at(mm, addr, pte, entry);
19374b3073e1SRussell King 	update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */
1938423bad60SNick Piggin 
1939423bad60SNick Piggin 	retval = 0;
1940423bad60SNick Piggin out_unlock:
1941423bad60SNick Piggin 	pte_unmap_unlock(pte, ptl);
1942423bad60SNick Piggin out:
1943423bad60SNick Piggin 	return retval;
1944423bad60SNick Piggin }
1945423bad60SNick Piggin 
1946e0dc0d8fSNick Piggin /**
1947e0dc0d8fSNick Piggin  * vm_insert_pfn - insert single pfn into user vma
1948e0dc0d8fSNick Piggin  * @vma: user vma to map to
1949e0dc0d8fSNick Piggin  * @addr: target user address of this page
1950e0dc0d8fSNick Piggin  * @pfn: source kernel pfn
1951e0dc0d8fSNick Piggin  *
1952e0dc0d8fSNick Piggin  * Similar to vm_inert_page, this allows drivers to insert individual pages
1953e0dc0d8fSNick Piggin  * they've allocated into a user vma. Same comments apply.
1954e0dc0d8fSNick Piggin  *
1955e0dc0d8fSNick Piggin  * This function should only be called from a vm_ops->fault handler, and
1956e0dc0d8fSNick Piggin  * in that case the handler should return NULL.
19570d71d10aSNick Piggin  *
19580d71d10aSNick Piggin  * vma cannot be a COW mapping.
19590d71d10aSNick Piggin  *
19600d71d10aSNick Piggin  * As this is called only for pages that do not currently exist, we
19610d71d10aSNick Piggin  * do not need to flush old virtual caches or the TLB.
1962e0dc0d8fSNick Piggin  */
1963e0dc0d8fSNick Piggin int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
1964e0dc0d8fSNick Piggin 			unsigned long pfn)
1965e0dc0d8fSNick Piggin {
19662ab64037Svenkatesh.pallipadi@intel.com 	int ret;
1967e4b866edSvenkatesh.pallipadi@intel.com 	pgprot_t pgprot = vma->vm_page_prot;
19687e675137SNick Piggin 	/*
19697e675137SNick Piggin 	 * Technically, architectures with pte_special can avoid all these
19707e675137SNick Piggin 	 * restrictions (same for remap_pfn_range).  However we would like
19717e675137SNick Piggin 	 * consistency in testing and feature parity among all, so we should
19727e675137SNick Piggin 	 * try to keep these invariants in place for everybody.
19737e675137SNick Piggin 	 */
1974b379d790SJared Hulbert 	BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
1975b379d790SJared Hulbert 	BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
1976b379d790SJared Hulbert 						(VM_PFNMAP|VM_MIXEDMAP));
1977b379d790SJared Hulbert 	BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
1978b379d790SJared Hulbert 	BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
1979e0dc0d8fSNick Piggin 
1980423bad60SNick Piggin 	if (addr < vma->vm_start || addr >= vma->vm_end)
1981423bad60SNick Piggin 		return -EFAULT;
1982e4b866edSvenkatesh.pallipadi@intel.com 	if (track_pfn_vma_new(vma, &pgprot, pfn, PAGE_SIZE))
19832ab64037Svenkatesh.pallipadi@intel.com 		return -EINVAL;
19842ab64037Svenkatesh.pallipadi@intel.com 
1985e4b866edSvenkatesh.pallipadi@intel.com 	ret = insert_pfn(vma, addr, pfn, pgprot);
19862ab64037Svenkatesh.pallipadi@intel.com 
19872ab64037Svenkatesh.pallipadi@intel.com 	if (ret)
19882ab64037Svenkatesh.pallipadi@intel.com 		untrack_pfn_vma(vma, pfn, PAGE_SIZE);
19892ab64037Svenkatesh.pallipadi@intel.com 
19902ab64037Svenkatesh.pallipadi@intel.com 	return ret;
1991e0dc0d8fSNick Piggin }
1992e0dc0d8fSNick Piggin EXPORT_SYMBOL(vm_insert_pfn);
1993e0dc0d8fSNick Piggin 
1994423bad60SNick Piggin int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
1995423bad60SNick Piggin 			unsigned long pfn)
1996423bad60SNick Piggin {
1997423bad60SNick Piggin 	BUG_ON(!(vma->vm_flags & VM_MIXEDMAP));
1998423bad60SNick Piggin 
1999423bad60SNick Piggin 	if (addr < vma->vm_start || addr >= vma->vm_end)
2000423bad60SNick Piggin 		return -EFAULT;
2001423bad60SNick Piggin 
2002423bad60SNick Piggin 	/*
2003423bad60SNick Piggin 	 * If we don't have pte special, then we have to use the pfn_valid()
2004423bad60SNick Piggin 	 * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must*
2005423bad60SNick Piggin 	 * refcount the page if pfn_valid is true (hence insert_page rather
200662eede62SHugh Dickins 	 * than insert_pfn).  If a zero_pfn were inserted into a VM_MIXEDMAP
200762eede62SHugh Dickins 	 * without pte special, it would there be refcounted as a normal page.
2008423bad60SNick Piggin 	 */
2009423bad60SNick Piggin 	if (!HAVE_PTE_SPECIAL && pfn_valid(pfn)) {
2010423bad60SNick Piggin 		struct page *page;
2011423bad60SNick Piggin 
2012423bad60SNick Piggin 		page = pfn_to_page(pfn);
2013423bad60SNick Piggin 		return insert_page(vma, addr, page, vma->vm_page_prot);
2014423bad60SNick Piggin 	}
2015423bad60SNick Piggin 	return insert_pfn(vma, addr, pfn, vma->vm_page_prot);
2016423bad60SNick Piggin }
2017423bad60SNick Piggin EXPORT_SYMBOL(vm_insert_mixed);
2018423bad60SNick Piggin 
2019a145dd41SLinus Torvalds /*
20201da177e4SLinus Torvalds  * maps a range of physical memory into the requested pages. the old
20211da177e4SLinus Torvalds  * mappings are removed. any references to nonexistent pages results
20221da177e4SLinus Torvalds  * in null mappings (currently treated as "copy-on-access")
20231da177e4SLinus Torvalds  */
20241da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
20251da177e4SLinus Torvalds 			unsigned long addr, unsigned long end,
20261da177e4SLinus Torvalds 			unsigned long pfn, pgprot_t prot)
20271da177e4SLinus Torvalds {
20281da177e4SLinus Torvalds 	pte_t *pte;
2029c74df32cSHugh Dickins 	spinlock_t *ptl;
20301da177e4SLinus Torvalds 
2031c74df32cSHugh Dickins 	pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
20321da177e4SLinus Torvalds 	if (!pte)
20331da177e4SLinus Torvalds 		return -ENOMEM;
20346606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
20351da177e4SLinus Torvalds 	do {
20361da177e4SLinus Torvalds 		BUG_ON(!pte_none(*pte));
20377e675137SNick Piggin 		set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot)));
20381da177e4SLinus Torvalds 		pfn++;
20391da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
20406606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
2041c74df32cSHugh Dickins 	pte_unmap_unlock(pte - 1, ptl);
20421da177e4SLinus Torvalds 	return 0;
20431da177e4SLinus Torvalds }
20441da177e4SLinus Torvalds 
20451da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
20461da177e4SLinus Torvalds 			unsigned long addr, unsigned long end,
20471da177e4SLinus Torvalds 			unsigned long pfn, pgprot_t prot)
20481da177e4SLinus Torvalds {
20491da177e4SLinus Torvalds 	pmd_t *pmd;
20501da177e4SLinus Torvalds 	unsigned long next;
20511da177e4SLinus Torvalds 
20521da177e4SLinus Torvalds 	pfn -= addr >> PAGE_SHIFT;
20531da177e4SLinus Torvalds 	pmd = pmd_alloc(mm, pud, addr);
20541da177e4SLinus Torvalds 	if (!pmd)
20551da177e4SLinus Torvalds 		return -ENOMEM;
2056f66055abSAndrea Arcangeli 	VM_BUG_ON(pmd_trans_huge(*pmd));
20571da177e4SLinus Torvalds 	do {
20581da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
20591da177e4SLinus Torvalds 		if (remap_pte_range(mm, pmd, addr, next,
20601da177e4SLinus Torvalds 				pfn + (addr >> PAGE_SHIFT), prot))
20611da177e4SLinus Torvalds 			return -ENOMEM;
20621da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
20631da177e4SLinus Torvalds 	return 0;
20641da177e4SLinus Torvalds }
20651da177e4SLinus Torvalds 
20661da177e4SLinus Torvalds static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd,
20671da177e4SLinus Torvalds 			unsigned long addr, unsigned long end,
20681da177e4SLinus Torvalds 			unsigned long pfn, pgprot_t prot)
20691da177e4SLinus Torvalds {
20701da177e4SLinus Torvalds 	pud_t *pud;
20711da177e4SLinus Torvalds 	unsigned long next;
20721da177e4SLinus Torvalds 
20731da177e4SLinus Torvalds 	pfn -= addr >> PAGE_SHIFT;
20741da177e4SLinus Torvalds 	pud = pud_alloc(mm, pgd, addr);
20751da177e4SLinus Torvalds 	if (!pud)
20761da177e4SLinus Torvalds 		return -ENOMEM;
20771da177e4SLinus Torvalds 	do {
20781da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
20791da177e4SLinus Torvalds 		if (remap_pmd_range(mm, pud, addr, next,
20801da177e4SLinus Torvalds 				pfn + (addr >> PAGE_SHIFT), prot))
20811da177e4SLinus Torvalds 			return -ENOMEM;
20821da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
20831da177e4SLinus Torvalds 	return 0;
20841da177e4SLinus Torvalds }
20851da177e4SLinus Torvalds 
2086bfa5bf6dSRolf Eike Beer /**
2087bfa5bf6dSRolf Eike Beer  * remap_pfn_range - remap kernel memory to userspace
2088bfa5bf6dSRolf Eike Beer  * @vma: user vma to map to
2089bfa5bf6dSRolf Eike Beer  * @addr: target user address to start at
2090bfa5bf6dSRolf Eike Beer  * @pfn: physical address of kernel memory
2091bfa5bf6dSRolf Eike Beer  * @size: size of map area
2092bfa5bf6dSRolf Eike Beer  * @prot: page protection flags for this mapping
2093bfa5bf6dSRolf Eike Beer  *
2094bfa5bf6dSRolf Eike Beer  *  Note: this is only safe if the mm semaphore is held when called.
2095bfa5bf6dSRolf Eike Beer  */
20961da177e4SLinus Torvalds int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
20971da177e4SLinus Torvalds 		    unsigned long pfn, unsigned long size, pgprot_t prot)
20981da177e4SLinus Torvalds {
20991da177e4SLinus Torvalds 	pgd_t *pgd;
21001da177e4SLinus Torvalds 	unsigned long next;
21012d15cab8SHugh Dickins 	unsigned long end = addr + PAGE_ALIGN(size);
21021da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
21031da177e4SLinus Torvalds 	int err;
21041da177e4SLinus Torvalds 
21051da177e4SLinus Torvalds 	/*
21061da177e4SLinus Torvalds 	 * Physically remapped pages are special. Tell the
21071da177e4SLinus Torvalds 	 * rest of the world about it:
21081da177e4SLinus Torvalds 	 *   VM_IO tells people not to look at these pages
21091da177e4SLinus Torvalds 	 *	(accesses can have side effects).
21100b14c179SHugh Dickins 	 *   VM_RESERVED is specified all over the place, because
21110b14c179SHugh Dickins 	 *	in 2.4 it kept swapout's vma scan off this vma; but
21120b14c179SHugh Dickins 	 *	in 2.6 the LRU scan won't even find its pages, so this
21130b14c179SHugh Dickins 	 *	flag means no more than count its pages in reserved_vm,
21140b14c179SHugh Dickins 	 * 	and omit it from core dump, even when VM_IO turned off.
21156aab341eSLinus Torvalds 	 *   VM_PFNMAP tells the core MM that the base pages are just
21166aab341eSLinus Torvalds 	 *	raw PFN mappings, and do not have a "struct page" associated
21176aab341eSLinus Torvalds 	 *	with them.
2118fb155c16SLinus Torvalds 	 *
2119fb155c16SLinus Torvalds 	 * There's a horrible special case to handle copy-on-write
2120fb155c16SLinus Torvalds 	 * behaviour that some programs depend on. We mark the "original"
2121fb155c16SLinus Torvalds 	 * un-COW'ed pages by matching them up with "vma->vm_pgoff".
21221da177e4SLinus Torvalds 	 */
21234bb9c5c0SPallipadi, Venkatesh 	if (addr == vma->vm_start && end == vma->vm_end) {
21246aab341eSLinus Torvalds 		vma->vm_pgoff = pfn;
2125895791daSPallipadi, Venkatesh 		vma->vm_flags |= VM_PFN_AT_MMAP;
21264bb9c5c0SPallipadi, Venkatesh 	} else if (is_cow_mapping(vma->vm_flags))
21273c8bb73aSvenkatesh.pallipadi@intel.com 		return -EINVAL;
2128fb155c16SLinus Torvalds 
2129fb155c16SLinus Torvalds 	vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP;
21301da177e4SLinus Torvalds 
2131e4b866edSvenkatesh.pallipadi@intel.com 	err = track_pfn_vma_new(vma, &prot, pfn, PAGE_ALIGN(size));
2132a3670613Svenkatesh.pallipadi@intel.com 	if (err) {
2133a3670613Svenkatesh.pallipadi@intel.com 		/*
2134a3670613Svenkatesh.pallipadi@intel.com 		 * To indicate that track_pfn related cleanup is not
2135a3670613Svenkatesh.pallipadi@intel.com 		 * needed from higher level routine calling unmap_vmas
2136a3670613Svenkatesh.pallipadi@intel.com 		 */
2137a3670613Svenkatesh.pallipadi@intel.com 		vma->vm_flags &= ~(VM_IO | VM_RESERVED | VM_PFNMAP);
2138895791daSPallipadi, Venkatesh 		vma->vm_flags &= ~VM_PFN_AT_MMAP;
21392ab64037Svenkatesh.pallipadi@intel.com 		return -EINVAL;
2140a3670613Svenkatesh.pallipadi@intel.com 	}
21412ab64037Svenkatesh.pallipadi@intel.com 
21421da177e4SLinus Torvalds 	BUG_ON(addr >= end);
21431da177e4SLinus Torvalds 	pfn -= addr >> PAGE_SHIFT;
21441da177e4SLinus Torvalds 	pgd = pgd_offset(mm, addr);
21451da177e4SLinus Torvalds 	flush_cache_range(vma, addr, end);
21461da177e4SLinus Torvalds 	do {
21471da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
21481da177e4SLinus Torvalds 		err = remap_pud_range(mm, pgd, addr, next,
21491da177e4SLinus Torvalds 				pfn + (addr >> PAGE_SHIFT), prot);
21501da177e4SLinus Torvalds 		if (err)
21511da177e4SLinus Torvalds 			break;
21521da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
21532ab64037Svenkatesh.pallipadi@intel.com 
21542ab64037Svenkatesh.pallipadi@intel.com 	if (err)
21552ab64037Svenkatesh.pallipadi@intel.com 		untrack_pfn_vma(vma, pfn, PAGE_ALIGN(size));
21562ab64037Svenkatesh.pallipadi@intel.com 
21571da177e4SLinus Torvalds 	return err;
21581da177e4SLinus Torvalds }
21591da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range);
21601da177e4SLinus Torvalds 
2161aee16b3cSJeremy Fitzhardinge static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
2162aee16b3cSJeremy Fitzhardinge 				     unsigned long addr, unsigned long end,
2163aee16b3cSJeremy Fitzhardinge 				     pte_fn_t fn, void *data)
2164aee16b3cSJeremy Fitzhardinge {
2165aee16b3cSJeremy Fitzhardinge 	pte_t *pte;
2166aee16b3cSJeremy Fitzhardinge 	int err;
21672f569afdSMartin Schwidefsky 	pgtable_t token;
216894909914SBorislav Petkov 	spinlock_t *uninitialized_var(ptl);
2169aee16b3cSJeremy Fitzhardinge 
2170aee16b3cSJeremy Fitzhardinge 	pte = (mm == &init_mm) ?
2171aee16b3cSJeremy Fitzhardinge 		pte_alloc_kernel(pmd, addr) :
2172aee16b3cSJeremy Fitzhardinge 		pte_alloc_map_lock(mm, pmd, addr, &ptl);
2173aee16b3cSJeremy Fitzhardinge 	if (!pte)
2174aee16b3cSJeremy Fitzhardinge 		return -ENOMEM;
2175aee16b3cSJeremy Fitzhardinge 
2176aee16b3cSJeremy Fitzhardinge 	BUG_ON(pmd_huge(*pmd));
2177aee16b3cSJeremy Fitzhardinge 
217838e0edb1SJeremy Fitzhardinge 	arch_enter_lazy_mmu_mode();
217938e0edb1SJeremy Fitzhardinge 
21802f569afdSMartin Schwidefsky 	token = pmd_pgtable(*pmd);
2181aee16b3cSJeremy Fitzhardinge 
2182aee16b3cSJeremy Fitzhardinge 	do {
2183c36987e2SDaisuke Nishimura 		err = fn(pte++, token, addr, data);
2184aee16b3cSJeremy Fitzhardinge 		if (err)
2185aee16b3cSJeremy Fitzhardinge 			break;
2186c36987e2SDaisuke Nishimura 	} while (addr += PAGE_SIZE, addr != end);
2187aee16b3cSJeremy Fitzhardinge 
218838e0edb1SJeremy Fitzhardinge 	arch_leave_lazy_mmu_mode();
218938e0edb1SJeremy Fitzhardinge 
2190aee16b3cSJeremy Fitzhardinge 	if (mm != &init_mm)
2191aee16b3cSJeremy Fitzhardinge 		pte_unmap_unlock(pte-1, ptl);
2192aee16b3cSJeremy Fitzhardinge 	return err;
2193aee16b3cSJeremy Fitzhardinge }
2194aee16b3cSJeremy Fitzhardinge 
2195aee16b3cSJeremy Fitzhardinge static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
2196aee16b3cSJeremy Fitzhardinge 				     unsigned long addr, unsigned long end,
2197aee16b3cSJeremy Fitzhardinge 				     pte_fn_t fn, void *data)
2198aee16b3cSJeremy Fitzhardinge {
2199aee16b3cSJeremy Fitzhardinge 	pmd_t *pmd;
2200aee16b3cSJeremy Fitzhardinge 	unsigned long next;
2201aee16b3cSJeremy Fitzhardinge 	int err;
2202aee16b3cSJeremy Fitzhardinge 
2203ceb86879SAndi Kleen 	BUG_ON(pud_huge(*pud));
2204ceb86879SAndi Kleen 
2205aee16b3cSJeremy Fitzhardinge 	pmd = pmd_alloc(mm, pud, addr);
2206aee16b3cSJeremy Fitzhardinge 	if (!pmd)
2207aee16b3cSJeremy Fitzhardinge 		return -ENOMEM;
2208aee16b3cSJeremy Fitzhardinge 	do {
2209aee16b3cSJeremy Fitzhardinge 		next = pmd_addr_end(addr, end);
2210aee16b3cSJeremy Fitzhardinge 		err = apply_to_pte_range(mm, pmd, addr, next, fn, data);
2211aee16b3cSJeremy Fitzhardinge 		if (err)
2212aee16b3cSJeremy Fitzhardinge 			break;
2213aee16b3cSJeremy Fitzhardinge 	} while (pmd++, addr = next, addr != end);
2214aee16b3cSJeremy Fitzhardinge 	return err;
2215aee16b3cSJeremy Fitzhardinge }
2216aee16b3cSJeremy Fitzhardinge 
2217aee16b3cSJeremy Fitzhardinge static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd,
2218aee16b3cSJeremy Fitzhardinge 				     unsigned long addr, unsigned long end,
2219aee16b3cSJeremy Fitzhardinge 				     pte_fn_t fn, void *data)
2220aee16b3cSJeremy Fitzhardinge {
2221aee16b3cSJeremy Fitzhardinge 	pud_t *pud;
2222aee16b3cSJeremy Fitzhardinge 	unsigned long next;
2223aee16b3cSJeremy Fitzhardinge 	int err;
2224aee16b3cSJeremy Fitzhardinge 
2225aee16b3cSJeremy Fitzhardinge 	pud = pud_alloc(mm, pgd, addr);
2226aee16b3cSJeremy Fitzhardinge 	if (!pud)
2227aee16b3cSJeremy Fitzhardinge 		return -ENOMEM;
2228aee16b3cSJeremy Fitzhardinge 	do {
2229aee16b3cSJeremy Fitzhardinge 		next = pud_addr_end(addr, end);
2230aee16b3cSJeremy Fitzhardinge 		err = apply_to_pmd_range(mm, pud, addr, next, fn, data);
2231aee16b3cSJeremy Fitzhardinge 		if (err)
2232aee16b3cSJeremy Fitzhardinge 			break;
2233aee16b3cSJeremy Fitzhardinge 	} while (pud++, addr = next, addr != end);
2234aee16b3cSJeremy Fitzhardinge 	return err;
2235aee16b3cSJeremy Fitzhardinge }
2236aee16b3cSJeremy Fitzhardinge 
2237aee16b3cSJeremy Fitzhardinge /*
2238aee16b3cSJeremy Fitzhardinge  * Scan a region of virtual memory, filling in page tables as necessary
2239aee16b3cSJeremy Fitzhardinge  * and calling a provided function on each leaf page table.
2240aee16b3cSJeremy Fitzhardinge  */
2241aee16b3cSJeremy Fitzhardinge int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
2242aee16b3cSJeremy Fitzhardinge 			unsigned long size, pte_fn_t fn, void *data)
2243aee16b3cSJeremy Fitzhardinge {
2244aee16b3cSJeremy Fitzhardinge 	pgd_t *pgd;
2245aee16b3cSJeremy Fitzhardinge 	unsigned long next;
224657250a5bSJeremy Fitzhardinge 	unsigned long end = addr + size;
2247aee16b3cSJeremy Fitzhardinge 	int err;
2248aee16b3cSJeremy Fitzhardinge 
2249aee16b3cSJeremy Fitzhardinge 	BUG_ON(addr >= end);
2250aee16b3cSJeremy Fitzhardinge 	pgd = pgd_offset(mm, addr);
2251aee16b3cSJeremy Fitzhardinge 	do {
2252aee16b3cSJeremy Fitzhardinge 		next = pgd_addr_end(addr, end);
2253aee16b3cSJeremy Fitzhardinge 		err = apply_to_pud_range(mm, pgd, addr, next, fn, data);
2254aee16b3cSJeremy Fitzhardinge 		if (err)
2255aee16b3cSJeremy Fitzhardinge 			break;
2256aee16b3cSJeremy Fitzhardinge 	} while (pgd++, addr = next, addr != end);
225757250a5bSJeremy Fitzhardinge 
2258aee16b3cSJeremy Fitzhardinge 	return err;
2259aee16b3cSJeremy Fitzhardinge }
2260aee16b3cSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(apply_to_page_range);
2261aee16b3cSJeremy Fitzhardinge 
22621da177e4SLinus Torvalds /*
22638f4e2101SHugh Dickins  * handle_pte_fault chooses page fault handler according to an entry
22648f4e2101SHugh Dickins  * which was read non-atomically.  Before making any commitment, on
22658f4e2101SHugh Dickins  * those architectures or configurations (e.g. i386 with PAE) which
2266a335b2e1SRyota Ozaki  * might give a mix of unmatched parts, do_swap_page and do_nonlinear_fault
22678f4e2101SHugh Dickins  * must check under lock before unmapping the pte and proceeding
22688f4e2101SHugh Dickins  * (but do_wp_page is only called after already making such a check;
2269a335b2e1SRyota Ozaki  * and do_anonymous_page can safely check later on).
22708f4e2101SHugh Dickins  */
22714c21e2f2SHugh Dickins static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
22728f4e2101SHugh Dickins 				pte_t *page_table, pte_t orig_pte)
22738f4e2101SHugh Dickins {
22748f4e2101SHugh Dickins 	int same = 1;
22758f4e2101SHugh Dickins #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
22768f4e2101SHugh Dickins 	if (sizeof(pte_t) > sizeof(unsigned long)) {
22774c21e2f2SHugh Dickins 		spinlock_t *ptl = pte_lockptr(mm, pmd);
22784c21e2f2SHugh Dickins 		spin_lock(ptl);
22798f4e2101SHugh Dickins 		same = pte_same(*page_table, orig_pte);
22804c21e2f2SHugh Dickins 		spin_unlock(ptl);
22818f4e2101SHugh Dickins 	}
22828f4e2101SHugh Dickins #endif
22838f4e2101SHugh Dickins 	pte_unmap(page_table);
22848f4e2101SHugh Dickins 	return same;
22858f4e2101SHugh Dickins }
22868f4e2101SHugh Dickins 
22879de455b2SAtsushi Nemoto static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma)
22886aab341eSLinus Torvalds {
22896aab341eSLinus Torvalds 	/*
22906aab341eSLinus Torvalds 	 * If the source page was a PFN mapping, we don't have
22916aab341eSLinus Torvalds 	 * a "struct page" for it. We do a best-effort copy by
22926aab341eSLinus Torvalds 	 * just copying from the original user address. If that
22936aab341eSLinus Torvalds 	 * fails, we just zero-fill it. Live with it.
22946aab341eSLinus Torvalds 	 */
22956aab341eSLinus Torvalds 	if (unlikely(!src)) {
22966aab341eSLinus Torvalds 		void *kaddr = kmap_atomic(dst, KM_USER0);
22975d2a2dbbSLinus Torvalds 		void __user *uaddr = (void __user *)(va & PAGE_MASK);
22985d2a2dbbSLinus Torvalds 
22995d2a2dbbSLinus Torvalds 		/*
23005d2a2dbbSLinus Torvalds 		 * This really shouldn't fail, because the page is there
23015d2a2dbbSLinus Torvalds 		 * in the page tables. But it might just be unreadable,
23025d2a2dbbSLinus Torvalds 		 * in which case we just give up and fill the result with
23035d2a2dbbSLinus Torvalds 		 * zeroes.
23045d2a2dbbSLinus Torvalds 		 */
23055d2a2dbbSLinus Torvalds 		if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE))
23063ecb01dfSJan Beulich 			clear_page(kaddr);
23076aab341eSLinus Torvalds 		kunmap_atomic(kaddr, KM_USER0);
2308c4ec7b0dSDmitriy Monakhov 		flush_dcache_page(dst);
23090ed361deSNick Piggin 	} else
23109de455b2SAtsushi Nemoto 		copy_user_highpage(dst, src, va, vma);
23116aab341eSLinus Torvalds }
23126aab341eSLinus Torvalds 
23131da177e4SLinus Torvalds /*
23141da177e4SLinus Torvalds  * This routine handles present pages, when users try to write
23151da177e4SLinus Torvalds  * to a shared page. It is done by copying the page to a new address
23161da177e4SLinus Torvalds  * and decrementing the shared-page counter for the old page.
23171da177e4SLinus Torvalds  *
23181da177e4SLinus Torvalds  * Note that this routine assumes that the protection checks have been
23191da177e4SLinus Torvalds  * done by the caller (the low-level page fault routine in most cases).
23201da177e4SLinus Torvalds  * Thus we can safely just mark it writable once we've done any necessary
23211da177e4SLinus Torvalds  * COW.
23221da177e4SLinus Torvalds  *
23231da177e4SLinus Torvalds  * We also mark the page dirty at this point even though the page will
23241da177e4SLinus Torvalds  * change only once the write actually happens. This avoids a few races,
23251da177e4SLinus Torvalds  * and potentially makes it more efficient.
23261da177e4SLinus Torvalds  *
23278f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
23288f4e2101SHugh Dickins  * but allow concurrent faults), with pte both mapped and locked.
23298f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
23301da177e4SLinus Torvalds  */
23311da177e4SLinus Torvalds static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
233265500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
23338f4e2101SHugh Dickins 		spinlock_t *ptl, pte_t orig_pte)
2334e6219ec8SNamhyung Kim 	__releases(ptl)
23351da177e4SLinus Torvalds {
2336e5bbe4dfSHugh Dickins 	struct page *old_page, *new_page;
23371da177e4SLinus Torvalds 	pte_t entry;
2338b009c024SMichel Lespinasse 	int ret = 0;
2339a200ee18SPeter Zijlstra 	int page_mkwrite = 0;
2340d08b3851SPeter Zijlstra 	struct page *dirty_page = NULL;
23411da177e4SLinus Torvalds 
23426aab341eSLinus Torvalds 	old_page = vm_normal_page(vma, address, orig_pte);
2343251b97f5SPeter Zijlstra 	if (!old_page) {
2344251b97f5SPeter Zijlstra 		/*
2345251b97f5SPeter Zijlstra 		 * VM_MIXEDMAP !pfn_valid() case
2346251b97f5SPeter Zijlstra 		 *
2347251b97f5SPeter Zijlstra 		 * We should not cow pages in a shared writeable mapping.
2348251b97f5SPeter Zijlstra 		 * Just mark the pages writable as we can't do any dirty
2349251b97f5SPeter Zijlstra 		 * accounting on raw pfn maps.
2350251b97f5SPeter Zijlstra 		 */
2351251b97f5SPeter Zijlstra 		if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
2352251b97f5SPeter Zijlstra 				     (VM_WRITE|VM_SHARED))
2353251b97f5SPeter Zijlstra 			goto reuse;
2354920fc356SHugh Dickins 		goto gotten;
2355251b97f5SPeter Zijlstra 	}
23561da177e4SLinus Torvalds 
2357d08b3851SPeter Zijlstra 	/*
2358ee6a6457SPeter Zijlstra 	 * Take out anonymous pages first, anonymous shared vmas are
2359ee6a6457SPeter Zijlstra 	 * not dirty accountable.
2360d08b3851SPeter Zijlstra 	 */
23619a840895SHugh Dickins 	if (PageAnon(old_page) && !PageKsm(old_page)) {
2362ab967d86SHugh Dickins 		if (!trylock_page(old_page)) {
2363ab967d86SHugh Dickins 			page_cache_get(old_page);
2364ab967d86SHugh Dickins 			pte_unmap_unlock(page_table, ptl);
2365ab967d86SHugh Dickins 			lock_page(old_page);
2366ab967d86SHugh Dickins 			page_table = pte_offset_map_lock(mm, pmd, address,
2367ab967d86SHugh Dickins 							 &ptl);
2368ab967d86SHugh Dickins 			if (!pte_same(*page_table, orig_pte)) {
2369ab967d86SHugh Dickins 				unlock_page(old_page);
2370ab967d86SHugh Dickins 				goto unlock;
2371ab967d86SHugh Dickins 			}
2372ab967d86SHugh Dickins 			page_cache_release(old_page);
2373ab967d86SHugh Dickins 		}
2374b009c024SMichel Lespinasse 		if (reuse_swap_page(old_page)) {
2375c44b6743SRik van Riel 			/*
2376c44b6743SRik van Riel 			 * The page is all ours.  Move it to our anon_vma so
2377c44b6743SRik van Riel 			 * the rmap code will not search our parent or siblings.
2378c44b6743SRik van Riel 			 * Protected against the rmap code by the page lock.
2379c44b6743SRik van Riel 			 */
2380c44b6743SRik van Riel 			page_move_anon_rmap(old_page, vma, address);
2381ee6a6457SPeter Zijlstra 			unlock_page(old_page);
2382b009c024SMichel Lespinasse 			goto reuse;
2383b009c024SMichel Lespinasse 		}
2384b009c024SMichel Lespinasse 		unlock_page(old_page);
2385ee6a6457SPeter Zijlstra 	} else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
2386d08b3851SPeter Zijlstra 					(VM_WRITE|VM_SHARED))) {
2387ee6a6457SPeter Zijlstra 		/*
2388ee6a6457SPeter Zijlstra 		 * Only catch write-faults on shared writable pages,
2389ee6a6457SPeter Zijlstra 		 * read-only shared pages can get COWed by
2390ee6a6457SPeter Zijlstra 		 * get_user_pages(.write=1, .force=1).
2391ee6a6457SPeter Zijlstra 		 */
23929637a5efSDavid Howells 		if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
2393c2ec175cSNick Piggin 			struct vm_fault vmf;
2394c2ec175cSNick Piggin 			int tmp;
2395c2ec175cSNick Piggin 
2396c2ec175cSNick Piggin 			vmf.virtual_address = (void __user *)(address &
2397c2ec175cSNick Piggin 								PAGE_MASK);
2398c2ec175cSNick Piggin 			vmf.pgoff = old_page->index;
2399c2ec175cSNick Piggin 			vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
2400c2ec175cSNick Piggin 			vmf.page = old_page;
2401c2ec175cSNick Piggin 
24029637a5efSDavid Howells 			/*
24039637a5efSDavid Howells 			 * Notify the address space that the page is about to
24049637a5efSDavid Howells 			 * become writable so that it can prohibit this or wait
24059637a5efSDavid Howells 			 * for the page to get into an appropriate state.
24069637a5efSDavid Howells 			 *
24079637a5efSDavid Howells 			 * We do this without the lock held, so that it can
24089637a5efSDavid Howells 			 * sleep if it needs to.
24099637a5efSDavid Howells 			 */
24109637a5efSDavid Howells 			page_cache_get(old_page);
24119637a5efSDavid Howells 			pte_unmap_unlock(page_table, ptl);
24129637a5efSDavid Howells 
2413c2ec175cSNick Piggin 			tmp = vma->vm_ops->page_mkwrite(vma, &vmf);
2414c2ec175cSNick Piggin 			if (unlikely(tmp &
2415c2ec175cSNick Piggin 					(VM_FAULT_ERROR | VM_FAULT_NOPAGE))) {
2416c2ec175cSNick Piggin 				ret = tmp;
24179637a5efSDavid Howells 				goto unwritable_page;
2418c2ec175cSNick Piggin 			}
2419b827e496SNick Piggin 			if (unlikely(!(tmp & VM_FAULT_LOCKED))) {
2420b827e496SNick Piggin 				lock_page(old_page);
2421b827e496SNick Piggin 				if (!old_page->mapping) {
2422b827e496SNick Piggin 					ret = 0; /* retry the fault */
2423b827e496SNick Piggin 					unlock_page(old_page);
2424b827e496SNick Piggin 					goto unwritable_page;
2425b827e496SNick Piggin 				}
2426b827e496SNick Piggin 			} else
2427b827e496SNick Piggin 				VM_BUG_ON(!PageLocked(old_page));
24289637a5efSDavid Howells 
24299637a5efSDavid Howells 			/*
24309637a5efSDavid Howells 			 * Since we dropped the lock we need to revalidate
24319637a5efSDavid Howells 			 * the PTE as someone else may have changed it.  If
24329637a5efSDavid Howells 			 * they did, we just return, as we can count on the
24339637a5efSDavid Howells 			 * MMU to tell us if they didn't also make it writable.
24349637a5efSDavid Howells 			 */
24359637a5efSDavid Howells 			page_table = pte_offset_map_lock(mm, pmd, address,
24369637a5efSDavid Howells 							 &ptl);
2437b827e496SNick Piggin 			if (!pte_same(*page_table, orig_pte)) {
2438b827e496SNick Piggin 				unlock_page(old_page);
24399637a5efSDavid Howells 				goto unlock;
2440b827e496SNick Piggin 			}
2441a200ee18SPeter Zijlstra 
2442a200ee18SPeter Zijlstra 			page_mkwrite = 1;
24439637a5efSDavid Howells 		}
2444d08b3851SPeter Zijlstra 		dirty_page = old_page;
2445d08b3851SPeter Zijlstra 		get_page(dirty_page);
24469637a5efSDavid Howells 
2447251b97f5SPeter Zijlstra reuse:
2448eca35133SBen Collins 		flush_cache_page(vma, address, pte_pfn(orig_pte));
244965500d23SHugh Dickins 		entry = pte_mkyoung(orig_pte);
245065500d23SHugh Dickins 		entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2451954ffcb3SKAMEZAWA Hiroyuki 		if (ptep_set_access_flags(vma, address, page_table, entry,1))
24524b3073e1SRussell King 			update_mmu_cache(vma, address, page_table);
245372ddc8f7SMichel Lespinasse 		pte_unmap_unlock(page_table, ptl);
245465500d23SHugh Dickins 		ret |= VM_FAULT_WRITE;
245572ddc8f7SMichel Lespinasse 
245672ddc8f7SMichel Lespinasse 		if (!dirty_page)
245772ddc8f7SMichel Lespinasse 			return ret;
245872ddc8f7SMichel Lespinasse 
245972ddc8f7SMichel Lespinasse 		/*
246072ddc8f7SMichel Lespinasse 		 * Yes, Virginia, this is actually required to prevent a race
246172ddc8f7SMichel Lespinasse 		 * with clear_page_dirty_for_io() from clearing the page dirty
246272ddc8f7SMichel Lespinasse 		 * bit after it clear all dirty ptes, but before a racing
246372ddc8f7SMichel Lespinasse 		 * do_wp_page installs a dirty pte.
246472ddc8f7SMichel Lespinasse 		 *
2465a335b2e1SRyota Ozaki 		 * __do_fault is protected similarly.
246672ddc8f7SMichel Lespinasse 		 */
246772ddc8f7SMichel Lespinasse 		if (!page_mkwrite) {
246872ddc8f7SMichel Lespinasse 			wait_on_page_locked(dirty_page);
246972ddc8f7SMichel Lespinasse 			set_page_dirty_balance(dirty_page, page_mkwrite);
247072ddc8f7SMichel Lespinasse 		}
247172ddc8f7SMichel Lespinasse 		put_page(dirty_page);
247272ddc8f7SMichel Lespinasse 		if (page_mkwrite) {
247372ddc8f7SMichel Lespinasse 			struct address_space *mapping = dirty_page->mapping;
247472ddc8f7SMichel Lespinasse 
247572ddc8f7SMichel Lespinasse 			set_page_dirty(dirty_page);
247672ddc8f7SMichel Lespinasse 			unlock_page(dirty_page);
247772ddc8f7SMichel Lespinasse 			page_cache_release(dirty_page);
247872ddc8f7SMichel Lespinasse 			if (mapping)	{
247972ddc8f7SMichel Lespinasse 				/*
248072ddc8f7SMichel Lespinasse 				 * Some device drivers do not set page.mapping
248172ddc8f7SMichel Lespinasse 				 * but still dirty their pages
248272ddc8f7SMichel Lespinasse 				 */
248372ddc8f7SMichel Lespinasse 				balance_dirty_pages_ratelimited(mapping);
248472ddc8f7SMichel Lespinasse 			}
248572ddc8f7SMichel Lespinasse 		}
248672ddc8f7SMichel Lespinasse 
248772ddc8f7SMichel Lespinasse 		/* file_update_time outside page_lock */
248872ddc8f7SMichel Lespinasse 		if (vma->vm_file)
248972ddc8f7SMichel Lespinasse 			file_update_time(vma->vm_file);
249072ddc8f7SMichel Lespinasse 
249172ddc8f7SMichel Lespinasse 		return ret;
24921da177e4SLinus Torvalds 	}
24931da177e4SLinus Torvalds 
24941da177e4SLinus Torvalds 	/*
24951da177e4SLinus Torvalds 	 * Ok, we need to copy. Oh, well..
24961da177e4SLinus Torvalds 	 */
24971da177e4SLinus Torvalds 	page_cache_get(old_page);
2498920fc356SHugh Dickins gotten:
24998f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
25001da177e4SLinus Torvalds 
25011da177e4SLinus Torvalds 	if (unlikely(anon_vma_prepare(vma)))
250265500d23SHugh Dickins 		goto oom;
2503a13ea5b7SHugh Dickins 
250462eede62SHugh Dickins 	if (is_zero_pfn(pte_pfn(orig_pte))) {
2505a13ea5b7SHugh Dickins 		new_page = alloc_zeroed_user_highpage_movable(vma, address);
2506a13ea5b7SHugh Dickins 		if (!new_page)
2507a13ea5b7SHugh Dickins 			goto oom;
2508a13ea5b7SHugh Dickins 	} else {
2509769848c0SMel Gorman 		new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
25101da177e4SLinus Torvalds 		if (!new_page)
251165500d23SHugh Dickins 			goto oom;
2512a13ea5b7SHugh Dickins 		cow_user_page(new_page, old_page, address, vma);
2513a13ea5b7SHugh Dickins 	}
2514a13ea5b7SHugh Dickins 	__SetPageUptodate(new_page);
2515a13ea5b7SHugh Dickins 
25162c26fdd7SKAMEZAWA Hiroyuki 	if (mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))
25178a9f3ccdSBalbir Singh 		goto oom_free_new;
25188a9f3ccdSBalbir Singh 
25191da177e4SLinus Torvalds 	/*
25201da177e4SLinus Torvalds 	 * Re-check the pte - we dropped the lock
25211da177e4SLinus Torvalds 	 */
25228f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
252365500d23SHugh Dickins 	if (likely(pte_same(*page_table, orig_pte))) {
2524920fc356SHugh Dickins 		if (old_page) {
25254294621fSHugh Dickins 			if (!PageAnon(old_page)) {
252634e55232SKAMEZAWA Hiroyuki 				dec_mm_counter_fast(mm, MM_FILEPAGES);
252734e55232SKAMEZAWA Hiroyuki 				inc_mm_counter_fast(mm, MM_ANONPAGES);
25284294621fSHugh Dickins 			}
2529920fc356SHugh Dickins 		} else
253034e55232SKAMEZAWA Hiroyuki 			inc_mm_counter_fast(mm, MM_ANONPAGES);
2531eca35133SBen Collins 		flush_cache_page(vma, address, pte_pfn(orig_pte));
253265500d23SHugh Dickins 		entry = mk_pte(new_page, vma->vm_page_prot);
253365500d23SHugh Dickins 		entry = maybe_mkwrite(pte_mkdirty(entry), vma);
25344ce072f1SSiddha, Suresh B 		/*
25354ce072f1SSiddha, Suresh B 		 * Clear the pte entry and flush it first, before updating the
25364ce072f1SSiddha, Suresh B 		 * pte with the new entry. This will avoid a race condition
25374ce072f1SSiddha, Suresh B 		 * seen in the presence of one thread doing SMC and another
25384ce072f1SSiddha, Suresh B 		 * thread doing COW.
25394ce072f1SSiddha, Suresh B 		 */
2540828502d3SIzik Eidus 		ptep_clear_flush(vma, address, page_table);
25419617d95eSNick Piggin 		page_add_new_anon_rmap(new_page, vma, address);
2542828502d3SIzik Eidus 		/*
2543828502d3SIzik Eidus 		 * We call the notify macro here because, when using secondary
2544828502d3SIzik Eidus 		 * mmu page tables (such as kvm shadow page tables), we want the
2545828502d3SIzik Eidus 		 * new page to be mapped directly into the secondary page table.
2546828502d3SIzik Eidus 		 */
2547828502d3SIzik Eidus 		set_pte_at_notify(mm, address, page_table, entry);
25484b3073e1SRussell King 		update_mmu_cache(vma, address, page_table);
2549945754a1SNick Piggin 		if (old_page) {
2550945754a1SNick Piggin 			/*
2551945754a1SNick Piggin 			 * Only after switching the pte to the new page may
2552945754a1SNick Piggin 			 * we remove the mapcount here. Otherwise another
2553945754a1SNick Piggin 			 * process may come and find the rmap count decremented
2554945754a1SNick Piggin 			 * before the pte is switched to the new page, and
2555945754a1SNick Piggin 			 * "reuse" the old page writing into it while our pte
2556945754a1SNick Piggin 			 * here still points into it and can be read by other
2557945754a1SNick Piggin 			 * threads.
2558945754a1SNick Piggin 			 *
2559945754a1SNick Piggin 			 * The critical issue is to order this
2560945754a1SNick Piggin 			 * page_remove_rmap with the ptp_clear_flush above.
2561945754a1SNick Piggin 			 * Those stores are ordered by (if nothing else,)
2562945754a1SNick Piggin 			 * the barrier present in the atomic_add_negative
2563945754a1SNick Piggin 			 * in page_remove_rmap.
2564945754a1SNick Piggin 			 *
2565945754a1SNick Piggin 			 * Then the TLB flush in ptep_clear_flush ensures that
2566945754a1SNick Piggin 			 * no process can access the old page before the
2567945754a1SNick Piggin 			 * decremented mapcount is visible. And the old page
2568945754a1SNick Piggin 			 * cannot be reused until after the decremented
2569945754a1SNick Piggin 			 * mapcount is visible. So transitively, TLBs to
2570945754a1SNick Piggin 			 * old page will be flushed before it can be reused.
2571945754a1SNick Piggin 			 */
2572edc315fdSHugh Dickins 			page_remove_rmap(old_page);
2573945754a1SNick Piggin 		}
2574945754a1SNick Piggin 
25751da177e4SLinus Torvalds 		/* Free the old page.. */
25761da177e4SLinus Torvalds 		new_page = old_page;
2577f33ea7f4SNick Piggin 		ret |= VM_FAULT_WRITE;
25788a9f3ccdSBalbir Singh 	} else
25798a9f3ccdSBalbir Singh 		mem_cgroup_uncharge_page(new_page);
25808a9f3ccdSBalbir Singh 
2581920fc356SHugh Dickins 	if (new_page)
25821da177e4SLinus Torvalds 		page_cache_release(new_page);
258365500d23SHugh Dickins unlock:
25848f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
2585e15f8c01SMichel Lespinasse 	if (old_page) {
2586e15f8c01SMichel Lespinasse 		/*
2587e15f8c01SMichel Lespinasse 		 * Don't let another task, with possibly unlocked vma,
2588e15f8c01SMichel Lespinasse 		 * keep the mlocked page.
2589e15f8c01SMichel Lespinasse 		 */
2590e15f8c01SMichel Lespinasse 		if ((ret & VM_FAULT_WRITE) && (vma->vm_flags & VM_LOCKED)) {
2591e15f8c01SMichel Lespinasse 			lock_page(old_page);	/* LRU manipulation */
2592e15f8c01SMichel Lespinasse 			munlock_vma_page(old_page);
2593e15f8c01SMichel Lespinasse 			unlock_page(old_page);
2594e15f8c01SMichel Lespinasse 		}
2595e15f8c01SMichel Lespinasse 		page_cache_release(old_page);
2596e15f8c01SMichel Lespinasse 	}
2597f33ea7f4SNick Piggin 	return ret;
25988a9f3ccdSBalbir Singh oom_free_new:
25996dbf6d3bSHugh Dickins 	page_cache_release(new_page);
260065500d23SHugh Dickins oom:
2601b827e496SNick Piggin 	if (old_page) {
2602b827e496SNick Piggin 		if (page_mkwrite) {
2603b827e496SNick Piggin 			unlock_page(old_page);
26041da177e4SLinus Torvalds 			page_cache_release(old_page);
2605b827e496SNick Piggin 		}
2606b827e496SNick Piggin 		page_cache_release(old_page);
2607b827e496SNick Piggin 	}
26081da177e4SLinus Torvalds 	return VM_FAULT_OOM;
26099637a5efSDavid Howells 
26109637a5efSDavid Howells unwritable_page:
26119637a5efSDavid Howells 	page_cache_release(old_page);
2612c2ec175cSNick Piggin 	return ret;
26131da177e4SLinus Torvalds }
26141da177e4SLinus Torvalds 
26151da177e4SLinus Torvalds /*
26161da177e4SLinus Torvalds  * Helper functions for unmap_mapping_range().
26171da177e4SLinus Torvalds  *
26181da177e4SLinus Torvalds  * __ Notes on dropping i_mmap_lock to reduce latency while unmapping __
26191da177e4SLinus Torvalds  *
26201da177e4SLinus Torvalds  * We have to restart searching the prio_tree whenever we drop the lock,
26211da177e4SLinus Torvalds  * since the iterator is only valid while the lock is held, and anyway
26221da177e4SLinus Torvalds  * a later vma might be split and reinserted earlier while lock dropped.
26231da177e4SLinus Torvalds  *
26241da177e4SLinus Torvalds  * The list of nonlinear vmas could be handled more efficiently, using
26251da177e4SLinus Torvalds  * a placeholder, but handle it in the same way until a need is shown.
26261da177e4SLinus Torvalds  * It is important to search the prio_tree before nonlinear list: a vma
26271da177e4SLinus Torvalds  * may become nonlinear and be shifted from prio_tree to nonlinear list
26281da177e4SLinus Torvalds  * while the lock is dropped; but never shifted from list to prio_tree.
26291da177e4SLinus Torvalds  *
26301da177e4SLinus Torvalds  * In order to make forward progress despite restarting the search,
26311da177e4SLinus Torvalds  * vm_truncate_count is used to mark a vma as now dealt with, so we can
26321da177e4SLinus Torvalds  * quickly skip it next time around.  Since the prio_tree search only
26331da177e4SLinus Torvalds  * shows us those vmas affected by unmapping the range in question, we
26341da177e4SLinus Torvalds  * can't efficiently keep all vmas in step with mapping->truncate_count:
26351da177e4SLinus Torvalds  * so instead reset them all whenever it wraps back to 0 (then go to 1).
26361da177e4SLinus Torvalds  * mapping->truncate_count and vma->vm_truncate_count are protected by
26371da177e4SLinus Torvalds  * i_mmap_lock.
26381da177e4SLinus Torvalds  *
26391da177e4SLinus Torvalds  * In order to make forward progress despite repeatedly restarting some
2640ee39b37bSHugh Dickins  * large vma, note the restart_addr from unmap_vmas when it breaks out:
26411da177e4SLinus Torvalds  * and restart from that address when we reach that vma again.  It might
26421da177e4SLinus Torvalds  * have been split or merged, shrunk or extended, but never shifted: so
26431da177e4SLinus Torvalds  * restart_addr remains valid so long as it remains in the vma's range.
26441da177e4SLinus Torvalds  * unmap_mapping_range forces truncate_count to leap over page-aligned
26451da177e4SLinus Torvalds  * values so we can save vma's restart_addr in its truncate_count field.
26461da177e4SLinus Torvalds  */
26471da177e4SLinus Torvalds #define is_restart_addr(truncate_count) (!((truncate_count) & ~PAGE_MASK))
26481da177e4SLinus Torvalds 
26491da177e4SLinus Torvalds static void reset_vma_truncate_counts(struct address_space *mapping)
26501da177e4SLinus Torvalds {
26511da177e4SLinus Torvalds 	struct vm_area_struct *vma;
26521da177e4SLinus Torvalds 	struct prio_tree_iter iter;
26531da177e4SLinus Torvalds 
26541da177e4SLinus Torvalds 	vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, ULONG_MAX)
26551da177e4SLinus Torvalds 		vma->vm_truncate_count = 0;
26561da177e4SLinus Torvalds 	list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
26571da177e4SLinus Torvalds 		vma->vm_truncate_count = 0;
26581da177e4SLinus Torvalds }
26591da177e4SLinus Torvalds 
26601da177e4SLinus Torvalds static int unmap_mapping_range_vma(struct vm_area_struct *vma,
26611da177e4SLinus Torvalds 		unsigned long start_addr, unsigned long end_addr,
26621da177e4SLinus Torvalds 		struct zap_details *details)
26631da177e4SLinus Torvalds {
26641da177e4SLinus Torvalds 	unsigned long restart_addr;
26651da177e4SLinus Torvalds 	int need_break;
26661da177e4SLinus Torvalds 
2667d00806b1SNick Piggin 	/*
2668d00806b1SNick Piggin 	 * files that support invalidating or truncating portions of the
2669d0217ac0SNick Piggin 	 * file from under mmaped areas must have their ->fault function
267083c54070SNick Piggin 	 * return a locked page (and set VM_FAULT_LOCKED in the return).
267183c54070SNick Piggin 	 * This provides synchronisation against concurrent unmapping here.
2672d00806b1SNick Piggin 	 */
2673d00806b1SNick Piggin 
26741da177e4SLinus Torvalds again:
26751da177e4SLinus Torvalds 	restart_addr = vma->vm_truncate_count;
26761da177e4SLinus Torvalds 	if (is_restart_addr(restart_addr) && start_addr < restart_addr) {
26771da177e4SLinus Torvalds 		start_addr = restart_addr;
26781da177e4SLinus Torvalds 		if (start_addr >= end_addr) {
26791da177e4SLinus Torvalds 			/* Top of vma has been split off since last time */
26801da177e4SLinus Torvalds 			vma->vm_truncate_count = details->truncate_count;
26811da177e4SLinus Torvalds 			return 0;
26821da177e4SLinus Torvalds 		}
26831da177e4SLinus Torvalds 	}
26841da177e4SLinus Torvalds 
2685ee39b37bSHugh Dickins 	restart_addr = zap_page_range(vma, start_addr,
2686ee39b37bSHugh Dickins 					end_addr - start_addr, details);
268795c354feSNick Piggin 	need_break = need_resched() || spin_needbreak(details->i_mmap_lock);
26881da177e4SLinus Torvalds 
2689ee39b37bSHugh Dickins 	if (restart_addr >= end_addr) {
26901da177e4SLinus Torvalds 		/* We have now completed this vma: mark it so */
26911da177e4SLinus Torvalds 		vma->vm_truncate_count = details->truncate_count;
26921da177e4SLinus Torvalds 		if (!need_break)
26931da177e4SLinus Torvalds 			return 0;
26941da177e4SLinus Torvalds 	} else {
26951da177e4SLinus Torvalds 		/* Note restart_addr in vma's truncate_count field */
2696ee39b37bSHugh Dickins 		vma->vm_truncate_count = restart_addr;
26971da177e4SLinus Torvalds 		if (!need_break)
26981da177e4SLinus Torvalds 			goto again;
26991da177e4SLinus Torvalds 	}
27001da177e4SLinus Torvalds 
27011da177e4SLinus Torvalds 	spin_unlock(details->i_mmap_lock);
27021da177e4SLinus Torvalds 	cond_resched();
27031da177e4SLinus Torvalds 	spin_lock(details->i_mmap_lock);
27041da177e4SLinus Torvalds 	return -EINTR;
27051da177e4SLinus Torvalds }
27061da177e4SLinus Torvalds 
27071da177e4SLinus Torvalds static inline void unmap_mapping_range_tree(struct prio_tree_root *root,
27081da177e4SLinus Torvalds 					    struct zap_details *details)
27091da177e4SLinus Torvalds {
27101da177e4SLinus Torvalds 	struct vm_area_struct *vma;
27111da177e4SLinus Torvalds 	struct prio_tree_iter iter;
27121da177e4SLinus Torvalds 	pgoff_t vba, vea, zba, zea;
27131da177e4SLinus Torvalds 
27141da177e4SLinus Torvalds restart:
27151da177e4SLinus Torvalds 	vma_prio_tree_foreach(vma, &iter, root,
27161da177e4SLinus Torvalds 			details->first_index, details->last_index) {
27171da177e4SLinus Torvalds 		/* Skip quickly over those we have already dealt with */
27181da177e4SLinus Torvalds 		if (vma->vm_truncate_count == details->truncate_count)
27191da177e4SLinus Torvalds 			continue;
27201da177e4SLinus Torvalds 
27211da177e4SLinus Torvalds 		vba = vma->vm_pgoff;
27221da177e4SLinus Torvalds 		vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1;
27231da177e4SLinus Torvalds 		/* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
27241da177e4SLinus Torvalds 		zba = details->first_index;
27251da177e4SLinus Torvalds 		if (zba < vba)
27261da177e4SLinus Torvalds 			zba = vba;
27271da177e4SLinus Torvalds 		zea = details->last_index;
27281da177e4SLinus Torvalds 		if (zea > vea)
27291da177e4SLinus Torvalds 			zea = vea;
27301da177e4SLinus Torvalds 
27311da177e4SLinus Torvalds 		if (unmap_mapping_range_vma(vma,
27321da177e4SLinus Torvalds 			((zba - vba) << PAGE_SHIFT) + vma->vm_start,
27331da177e4SLinus Torvalds 			((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
27341da177e4SLinus Torvalds 				details) < 0)
27351da177e4SLinus Torvalds 			goto restart;
27361da177e4SLinus Torvalds 	}
27371da177e4SLinus Torvalds }
27381da177e4SLinus Torvalds 
27391da177e4SLinus Torvalds static inline void unmap_mapping_range_list(struct list_head *head,
27401da177e4SLinus Torvalds 					    struct zap_details *details)
27411da177e4SLinus Torvalds {
27421da177e4SLinus Torvalds 	struct vm_area_struct *vma;
27431da177e4SLinus Torvalds 
27441da177e4SLinus Torvalds 	/*
27451da177e4SLinus Torvalds 	 * In nonlinear VMAs there is no correspondence between virtual address
27461da177e4SLinus Torvalds 	 * offset and file offset.  So we must perform an exhaustive search
27471da177e4SLinus Torvalds 	 * across *all* the pages in each nonlinear VMA, not just the pages
27481da177e4SLinus Torvalds 	 * whose virtual address lies outside the file truncation point.
27491da177e4SLinus Torvalds 	 */
27501da177e4SLinus Torvalds restart:
27511da177e4SLinus Torvalds 	list_for_each_entry(vma, head, shared.vm_set.list) {
27521da177e4SLinus Torvalds 		/* Skip quickly over those we have already dealt with */
27531da177e4SLinus Torvalds 		if (vma->vm_truncate_count == details->truncate_count)
27541da177e4SLinus Torvalds 			continue;
27551da177e4SLinus Torvalds 		details->nonlinear_vma = vma;
27561da177e4SLinus Torvalds 		if (unmap_mapping_range_vma(vma, vma->vm_start,
27571da177e4SLinus Torvalds 					vma->vm_end, details) < 0)
27581da177e4SLinus Torvalds 			goto restart;
27591da177e4SLinus Torvalds 	}
27601da177e4SLinus Torvalds }
27611da177e4SLinus Torvalds 
27621da177e4SLinus Torvalds /**
276372fd4a35SRobert P. J. Day  * unmap_mapping_range - unmap the portion of all mmaps in the specified address_space corresponding to the specified page range in the underlying file.
27643d41088fSMartin Waitz  * @mapping: the address space containing mmaps to be unmapped.
27651da177e4SLinus Torvalds  * @holebegin: byte in first page to unmap, relative to the start of
27661da177e4SLinus Torvalds  * the underlying file.  This will be rounded down to a PAGE_SIZE
276725d9e2d1Snpiggin@suse.de  * boundary.  Note that this is different from truncate_pagecache(), which
27681da177e4SLinus Torvalds  * must keep the partial page.  In contrast, we must get rid of
27691da177e4SLinus Torvalds  * partial pages.
27701da177e4SLinus Torvalds  * @holelen: size of prospective hole in bytes.  This will be rounded
27711da177e4SLinus Torvalds  * up to a PAGE_SIZE boundary.  A holelen of zero truncates to the
27721da177e4SLinus Torvalds  * end of the file.
27731da177e4SLinus Torvalds  * @even_cows: 1 when truncating a file, unmap even private COWed pages;
27741da177e4SLinus Torvalds  * but 0 when invalidating pagecache, don't throw away private data.
27751da177e4SLinus Torvalds  */
27761da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping,
27771da177e4SLinus Torvalds 		loff_t const holebegin, loff_t const holelen, int even_cows)
27781da177e4SLinus Torvalds {
27791da177e4SLinus Torvalds 	struct zap_details details;
27801da177e4SLinus Torvalds 	pgoff_t hba = holebegin >> PAGE_SHIFT;
27811da177e4SLinus Torvalds 	pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
27821da177e4SLinus Torvalds 
27831da177e4SLinus Torvalds 	/* Check for overflow. */
27841da177e4SLinus Torvalds 	if (sizeof(holelen) > sizeof(hlen)) {
27851da177e4SLinus Torvalds 		long long holeend =
27861da177e4SLinus Torvalds 			(holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
27871da177e4SLinus Torvalds 		if (holeend & ~(long long)ULONG_MAX)
27881da177e4SLinus Torvalds 			hlen = ULONG_MAX - hba + 1;
27891da177e4SLinus Torvalds 	}
27901da177e4SLinus Torvalds 
27911da177e4SLinus Torvalds 	details.check_mapping = even_cows? NULL: mapping;
27921da177e4SLinus Torvalds 	details.nonlinear_vma = NULL;
27931da177e4SLinus Torvalds 	details.first_index = hba;
27941da177e4SLinus Torvalds 	details.last_index = hba + hlen - 1;
27951da177e4SLinus Torvalds 	if (details.last_index < details.first_index)
27961da177e4SLinus Torvalds 		details.last_index = ULONG_MAX;
27971da177e4SLinus Torvalds 	details.i_mmap_lock = &mapping->i_mmap_lock;
27981da177e4SLinus Torvalds 
27992aa15890SMiklos Szeredi 	mutex_lock(&mapping->unmap_mutex);
28001da177e4SLinus Torvalds 	spin_lock(&mapping->i_mmap_lock);
28011da177e4SLinus Torvalds 
2802d00806b1SNick Piggin 	/* Protect against endless unmapping loops */
28031da177e4SLinus Torvalds 	mapping->truncate_count++;
28041da177e4SLinus Torvalds 	if (unlikely(is_restart_addr(mapping->truncate_count))) {
28051da177e4SLinus Torvalds 		if (mapping->truncate_count == 0)
28061da177e4SLinus Torvalds 			reset_vma_truncate_counts(mapping);
28071da177e4SLinus Torvalds 		mapping->truncate_count++;
28081da177e4SLinus Torvalds 	}
28091da177e4SLinus Torvalds 	details.truncate_count = mapping->truncate_count;
28101da177e4SLinus Torvalds 
28111da177e4SLinus Torvalds 	if (unlikely(!prio_tree_empty(&mapping->i_mmap)))
28121da177e4SLinus Torvalds 		unmap_mapping_range_tree(&mapping->i_mmap, &details);
28131da177e4SLinus Torvalds 	if (unlikely(!list_empty(&mapping->i_mmap_nonlinear)))
28141da177e4SLinus Torvalds 		unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details);
28151da177e4SLinus Torvalds 	spin_unlock(&mapping->i_mmap_lock);
28162aa15890SMiklos Szeredi 	mutex_unlock(&mapping->unmap_mutex);
28171da177e4SLinus Torvalds }
28181da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range);
28191da177e4SLinus Torvalds 
2820f6b3ec23SBadari Pulavarty int vmtruncate_range(struct inode *inode, loff_t offset, loff_t end)
2821f6b3ec23SBadari Pulavarty {
2822f6b3ec23SBadari Pulavarty 	struct address_space *mapping = inode->i_mapping;
2823f6b3ec23SBadari Pulavarty 
2824f6b3ec23SBadari Pulavarty 	/*
2825f6b3ec23SBadari Pulavarty 	 * If the underlying filesystem is not going to provide
2826f6b3ec23SBadari Pulavarty 	 * a way to truncate a range of blocks (punch a hole) -
2827f6b3ec23SBadari Pulavarty 	 * we should return failure right now.
2828f6b3ec23SBadari Pulavarty 	 */
2829acfa4380SAl Viro 	if (!inode->i_op->truncate_range)
2830f6b3ec23SBadari Pulavarty 		return -ENOSYS;
2831f6b3ec23SBadari Pulavarty 
28321b1dcc1bSJes Sorensen 	mutex_lock(&inode->i_mutex);
2833f6b3ec23SBadari Pulavarty 	down_write(&inode->i_alloc_sem);
2834f6b3ec23SBadari Pulavarty 	unmap_mapping_range(mapping, offset, (end - offset), 1);
2835f6b3ec23SBadari Pulavarty 	truncate_inode_pages_range(mapping, offset, end);
2836d00806b1SNick Piggin 	unmap_mapping_range(mapping, offset, (end - offset), 1);
2837f6b3ec23SBadari Pulavarty 	inode->i_op->truncate_range(inode, offset, end);
2838f6b3ec23SBadari Pulavarty 	up_write(&inode->i_alloc_sem);
28391b1dcc1bSJes Sorensen 	mutex_unlock(&inode->i_mutex);
2840f6b3ec23SBadari Pulavarty 
2841f6b3ec23SBadari Pulavarty 	return 0;
2842f6b3ec23SBadari Pulavarty }
2843f6b3ec23SBadari Pulavarty 
28441da177e4SLinus Torvalds /*
28458f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
28468f4e2101SHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
28478f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
28481da177e4SLinus Torvalds  */
284965500d23SHugh Dickins static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
285065500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
285130c9f3a9SLinus Torvalds 		unsigned int flags, pte_t orig_pte)
28521da177e4SLinus Torvalds {
28538f4e2101SHugh Dickins 	spinlock_t *ptl;
28544969c119SAndrea Arcangeli 	struct page *page, *swapcache = NULL;
285565500d23SHugh Dickins 	swp_entry_t entry;
28561da177e4SLinus Torvalds 	pte_t pte;
2857d065bd81SMichel Lespinasse 	int locked;
285856039efaSKAMEZAWA Hiroyuki 	struct mem_cgroup *ptr;
2859ad8c2ee8SRik van Riel 	int exclusive = 0;
286083c54070SNick Piggin 	int ret = 0;
28611da177e4SLinus Torvalds 
28624c21e2f2SHugh Dickins 	if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
28638f4e2101SHugh Dickins 		goto out;
286465500d23SHugh Dickins 
286565500d23SHugh Dickins 	entry = pte_to_swp_entry(orig_pte);
2866d1737fdbSAndi Kleen 	if (unlikely(non_swap_entry(entry))) {
28670697212aSChristoph Lameter 		if (is_migration_entry(entry)) {
28680697212aSChristoph Lameter 			migration_entry_wait(mm, pmd, address);
2869d1737fdbSAndi Kleen 		} else if (is_hwpoison_entry(entry)) {
2870d1737fdbSAndi Kleen 			ret = VM_FAULT_HWPOISON;
2871d1737fdbSAndi Kleen 		} else {
2872d1737fdbSAndi Kleen 			print_bad_pte(vma, address, orig_pte, NULL);
2873d99be1a8SHugh Dickins 			ret = VM_FAULT_SIGBUS;
2874d1737fdbSAndi Kleen 		}
28750697212aSChristoph Lameter 		goto out;
28760697212aSChristoph Lameter 	}
28770ff92245SShailabh Nagar 	delayacct_set_flag(DELAYACCT_PF_SWAPIN);
28781da177e4SLinus Torvalds 	page = lookup_swap_cache(entry);
28791da177e4SLinus Torvalds 	if (!page) {
2880a5c9b696SHugh Dickins 		grab_swap_token(mm); /* Contend for token _before_ read-in */
288102098feaSHugh Dickins 		page = swapin_readahead(entry,
288202098feaSHugh Dickins 					GFP_HIGHUSER_MOVABLE, vma, address);
28831da177e4SLinus Torvalds 		if (!page) {
28841da177e4SLinus Torvalds 			/*
28858f4e2101SHugh Dickins 			 * Back out if somebody else faulted in this pte
28868f4e2101SHugh Dickins 			 * while we released the pte lock.
28871da177e4SLinus Torvalds 			 */
28888f4e2101SHugh Dickins 			page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
28891da177e4SLinus Torvalds 			if (likely(pte_same(*page_table, orig_pte)))
28901da177e4SLinus Torvalds 				ret = VM_FAULT_OOM;
28910ff92245SShailabh Nagar 			delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
289265500d23SHugh Dickins 			goto unlock;
28931da177e4SLinus Torvalds 		}
28941da177e4SLinus Torvalds 
28951da177e4SLinus Torvalds 		/* Had to read the page from swap area: Major fault */
28961da177e4SLinus Torvalds 		ret = VM_FAULT_MAJOR;
2897f8891e5eSChristoph Lameter 		count_vm_event(PGMAJFAULT);
2898d1737fdbSAndi Kleen 	} else if (PageHWPoison(page)) {
289971f72525SWu Fengguang 		/*
290071f72525SWu Fengguang 		 * hwpoisoned dirty swapcache pages are kept for killing
290171f72525SWu Fengguang 		 * owner processes (which may be unknown at hwpoison time)
290271f72525SWu Fengguang 		 */
2903d1737fdbSAndi Kleen 		ret = VM_FAULT_HWPOISON;
2904d1737fdbSAndi Kleen 		delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
29054779cb31SAndi Kleen 		goto out_release;
29061da177e4SLinus Torvalds 	}
29071da177e4SLinus Torvalds 
2908d065bd81SMichel Lespinasse 	locked = lock_page_or_retry(page, mm, flags);
290920a1022dSBalbir Singh 	delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
2910d065bd81SMichel Lespinasse 	if (!locked) {
2911d065bd81SMichel Lespinasse 		ret |= VM_FAULT_RETRY;
2912d065bd81SMichel Lespinasse 		goto out_release;
2913d065bd81SMichel Lespinasse 	}
29141da177e4SLinus Torvalds 
29154969c119SAndrea Arcangeli 	/*
291631c4a3d3SHugh Dickins 	 * Make sure try_to_free_swap or reuse_swap_page or swapoff did not
291731c4a3d3SHugh Dickins 	 * release the swapcache from under us.  The page pin, and pte_same
291831c4a3d3SHugh Dickins 	 * test below, are not enough to exclude that.  Even if it is still
291931c4a3d3SHugh Dickins 	 * swapcache, we need to check that the page's swap has not changed.
29204969c119SAndrea Arcangeli 	 */
292131c4a3d3SHugh Dickins 	if (unlikely(!PageSwapCache(page) || page_private(page) != entry.val))
29224969c119SAndrea Arcangeli 		goto out_page;
29234969c119SAndrea Arcangeli 
29244969c119SAndrea Arcangeli 	if (ksm_might_need_to_copy(page, vma, address)) {
29254969c119SAndrea Arcangeli 		swapcache = page;
29264969c119SAndrea Arcangeli 		page = ksm_does_need_to_copy(page, vma, address);
29274969c119SAndrea Arcangeli 
29284969c119SAndrea Arcangeli 		if (unlikely(!page)) {
29295ad64688SHugh Dickins 			ret = VM_FAULT_OOM;
29304969c119SAndrea Arcangeli 			page = swapcache;
29314969c119SAndrea Arcangeli 			swapcache = NULL;
29324969c119SAndrea Arcangeli 			goto out_page;
29334969c119SAndrea Arcangeli 		}
29345ad64688SHugh Dickins 	}
29355ad64688SHugh Dickins 
29362c26fdd7SKAMEZAWA Hiroyuki 	if (mem_cgroup_try_charge_swapin(mm, page, GFP_KERNEL, &ptr)) {
2937073e587eSKAMEZAWA Hiroyuki 		ret = VM_FAULT_OOM;
2938bc43f75cSJohannes Weiner 		goto out_page;
2939073e587eSKAMEZAWA Hiroyuki 	}
2940073e587eSKAMEZAWA Hiroyuki 
29411da177e4SLinus Torvalds 	/*
29428f4e2101SHugh Dickins 	 * Back out if somebody else already faulted in this pte.
29431da177e4SLinus Torvalds 	 */
29448f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
29459e9bef07SHugh Dickins 	if (unlikely(!pte_same(*page_table, orig_pte)))
2946b8107480SKirill Korotaev 		goto out_nomap;
2947b8107480SKirill Korotaev 
2948b8107480SKirill Korotaev 	if (unlikely(!PageUptodate(page))) {
2949b8107480SKirill Korotaev 		ret = VM_FAULT_SIGBUS;
2950b8107480SKirill Korotaev 		goto out_nomap;
29511da177e4SLinus Torvalds 	}
29521da177e4SLinus Torvalds 
29538c7c6e34SKAMEZAWA Hiroyuki 	/*
29548c7c6e34SKAMEZAWA Hiroyuki 	 * The page isn't present yet, go ahead with the fault.
29558c7c6e34SKAMEZAWA Hiroyuki 	 *
29568c7c6e34SKAMEZAWA Hiroyuki 	 * Be careful about the sequence of operations here.
29578c7c6e34SKAMEZAWA Hiroyuki 	 * To get its accounting right, reuse_swap_page() must be called
29588c7c6e34SKAMEZAWA Hiroyuki 	 * while the page is counted on swap but not yet in mapcount i.e.
29598c7c6e34SKAMEZAWA Hiroyuki 	 * before page_add_anon_rmap() and swap_free(); try_to_free_swap()
29608c7c6e34SKAMEZAWA Hiroyuki 	 * must be called after the swap_free(), or it will never succeed.
296103f3c433SKAMEZAWA Hiroyuki 	 * Because delete_from_swap_page() may be called by reuse_swap_page(),
296203f3c433SKAMEZAWA Hiroyuki 	 * mem_cgroup_commit_charge_swapin() may not be able to find swp_entry
296303f3c433SKAMEZAWA Hiroyuki 	 * in page->private. In this case, a record in swap_cgroup  is silently
296403f3c433SKAMEZAWA Hiroyuki 	 * discarded at swap_free().
29658c7c6e34SKAMEZAWA Hiroyuki 	 */
29661da177e4SLinus Torvalds 
296734e55232SKAMEZAWA Hiroyuki 	inc_mm_counter_fast(mm, MM_ANONPAGES);
2968b084d435SKAMEZAWA Hiroyuki 	dec_mm_counter_fast(mm, MM_SWAPENTS);
29691da177e4SLinus Torvalds 	pte = mk_pte(page, vma->vm_page_prot);
297030c9f3a9SLinus Torvalds 	if ((flags & FAULT_FLAG_WRITE) && reuse_swap_page(page)) {
29711da177e4SLinus Torvalds 		pte = maybe_mkwrite(pte_mkdirty(pte), vma);
297230c9f3a9SLinus Torvalds 		flags &= ~FAULT_FLAG_WRITE;
29739a5b489bSAndrea Arcangeli 		ret |= VM_FAULT_WRITE;
2974ad8c2ee8SRik van Riel 		exclusive = 1;
29751da177e4SLinus Torvalds 	}
29761da177e4SLinus Torvalds 	flush_icache_page(vma, page);
29771da177e4SLinus Torvalds 	set_pte_at(mm, address, page_table, pte);
2978ad8c2ee8SRik van Riel 	do_page_add_anon_rmap(page, vma, address, exclusive);
297903f3c433SKAMEZAWA Hiroyuki 	/* It's better to call commit-charge after rmap is established */
298003f3c433SKAMEZAWA Hiroyuki 	mem_cgroup_commit_charge_swapin(page, ptr);
29811da177e4SLinus Torvalds 
2982c475a8abSHugh Dickins 	swap_free(entry);
2983b291f000SNick Piggin 	if (vm_swap_full() || (vma->vm_flags & VM_LOCKED) || PageMlocked(page))
2984a2c43eedSHugh Dickins 		try_to_free_swap(page);
2985c475a8abSHugh Dickins 	unlock_page(page);
29864969c119SAndrea Arcangeli 	if (swapcache) {
29874969c119SAndrea Arcangeli 		/*
29884969c119SAndrea Arcangeli 		 * Hold the lock to avoid the swap entry to be reused
29894969c119SAndrea Arcangeli 		 * until we take the PT lock for the pte_same() check
29904969c119SAndrea Arcangeli 		 * (to avoid false positives from pte_same). For
29914969c119SAndrea Arcangeli 		 * further safety release the lock after the swap_free
29924969c119SAndrea Arcangeli 		 * so that the swap count won't change under a
29934969c119SAndrea Arcangeli 		 * parallel locked swapcache.
29944969c119SAndrea Arcangeli 		 */
29954969c119SAndrea Arcangeli 		unlock_page(swapcache);
29964969c119SAndrea Arcangeli 		page_cache_release(swapcache);
29974969c119SAndrea Arcangeli 	}
2998c475a8abSHugh Dickins 
299930c9f3a9SLinus Torvalds 	if (flags & FAULT_FLAG_WRITE) {
300061469f1dSHugh Dickins 		ret |= do_wp_page(mm, vma, address, page_table, pmd, ptl, pte);
300161469f1dSHugh Dickins 		if (ret & VM_FAULT_ERROR)
300261469f1dSHugh Dickins 			ret &= VM_FAULT_ERROR;
30031da177e4SLinus Torvalds 		goto out;
30041da177e4SLinus Torvalds 	}
30051da177e4SLinus Torvalds 
30061da177e4SLinus Torvalds 	/* No need to invalidate - it was non-present before */
30074b3073e1SRussell King 	update_mmu_cache(vma, address, page_table);
300865500d23SHugh Dickins unlock:
30098f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
30101da177e4SLinus Torvalds out:
30111da177e4SLinus Torvalds 	return ret;
3012b8107480SKirill Korotaev out_nomap:
30137a81b88cSKAMEZAWA Hiroyuki 	mem_cgroup_cancel_charge_swapin(ptr);
30148f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
3015bc43f75cSJohannes Weiner out_page:
3016b8107480SKirill Korotaev 	unlock_page(page);
30174779cb31SAndi Kleen out_release:
3018b8107480SKirill Korotaev 	page_cache_release(page);
30194969c119SAndrea Arcangeli 	if (swapcache) {
30204969c119SAndrea Arcangeli 		unlock_page(swapcache);
30214969c119SAndrea Arcangeli 		page_cache_release(swapcache);
30224969c119SAndrea Arcangeli 	}
302365500d23SHugh Dickins 	return ret;
30241da177e4SLinus Torvalds }
30251da177e4SLinus Torvalds 
30261da177e4SLinus Torvalds /*
30278ca3eb08SLuck, Tony  * This is like a special single-page "expand_{down|up}wards()",
30288ca3eb08SLuck, Tony  * except we must first make sure that 'address{-|+}PAGE_SIZE'
3029320b2b8dSLinus Torvalds  * doesn't hit another vma.
3030320b2b8dSLinus Torvalds  */
3031320b2b8dSLinus Torvalds static inline int check_stack_guard_page(struct vm_area_struct *vma, unsigned long address)
3032320b2b8dSLinus Torvalds {
3033320b2b8dSLinus Torvalds 	address &= PAGE_MASK;
3034320b2b8dSLinus Torvalds 	if ((vma->vm_flags & VM_GROWSDOWN) && address == vma->vm_start) {
30350e8e50e2SLinus Torvalds 		struct vm_area_struct *prev = vma->vm_prev;
3036320b2b8dSLinus Torvalds 
30370e8e50e2SLinus Torvalds 		/*
30380e8e50e2SLinus Torvalds 		 * Is there a mapping abutting this one below?
30390e8e50e2SLinus Torvalds 		 *
30400e8e50e2SLinus Torvalds 		 * That's only ok if it's the same stack mapping
30410e8e50e2SLinus Torvalds 		 * that has gotten split..
30420e8e50e2SLinus Torvalds 		 */
30430e8e50e2SLinus Torvalds 		if (prev && prev->vm_end == address)
30440e8e50e2SLinus Torvalds 			return prev->vm_flags & VM_GROWSDOWN ? 0 : -ENOMEM;
30450e8e50e2SLinus Torvalds 
3046d05f3169SMichal Hocko 		expand_downwards(vma, address - PAGE_SIZE);
3047320b2b8dSLinus Torvalds 	}
30488ca3eb08SLuck, Tony 	if ((vma->vm_flags & VM_GROWSUP) && address + PAGE_SIZE == vma->vm_end) {
30498ca3eb08SLuck, Tony 		struct vm_area_struct *next = vma->vm_next;
30508ca3eb08SLuck, Tony 
30518ca3eb08SLuck, Tony 		/* As VM_GROWSDOWN but s/below/above/ */
30528ca3eb08SLuck, Tony 		if (next && next->vm_start == address + PAGE_SIZE)
30538ca3eb08SLuck, Tony 			return next->vm_flags & VM_GROWSUP ? 0 : -ENOMEM;
30548ca3eb08SLuck, Tony 
30558ca3eb08SLuck, Tony 		expand_upwards(vma, address + PAGE_SIZE);
30568ca3eb08SLuck, Tony 	}
3057320b2b8dSLinus Torvalds 	return 0;
3058320b2b8dSLinus Torvalds }
3059320b2b8dSLinus Torvalds 
3060320b2b8dSLinus Torvalds /*
30618f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
30628f4e2101SHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
30638f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
30641da177e4SLinus Torvalds  */
306565500d23SHugh Dickins static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
306665500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
306730c9f3a9SLinus Torvalds 		unsigned int flags)
30681da177e4SLinus Torvalds {
30698f4e2101SHugh Dickins 	struct page *page;
30708f4e2101SHugh Dickins 	spinlock_t *ptl;
30711da177e4SLinus Torvalds 	pte_t entry;
30721da177e4SLinus Torvalds 
30735528f913SLinus Torvalds 	pte_unmap(page_table);
3074320b2b8dSLinus Torvalds 
307511ac5524SLinus Torvalds 	/* Check if we need to add a guard page to the stack */
307611ac5524SLinus Torvalds 	if (check_stack_guard_page(vma, address) < 0)
307711ac5524SLinus Torvalds 		return VM_FAULT_SIGBUS;
307811ac5524SLinus Torvalds 
307911ac5524SLinus Torvalds 	/* Use the zero-page for reads */
308062eede62SHugh Dickins 	if (!(flags & FAULT_FLAG_WRITE)) {
308162eede62SHugh Dickins 		entry = pte_mkspecial(pfn_pte(my_zero_pfn(address),
308262eede62SHugh Dickins 						vma->vm_page_prot));
308311ac5524SLinus Torvalds 		page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
3084a13ea5b7SHugh Dickins 		if (!pte_none(*page_table))
3085a13ea5b7SHugh Dickins 			goto unlock;
3086a13ea5b7SHugh Dickins 		goto setpte;
3087a13ea5b7SHugh Dickins 	}
3088a13ea5b7SHugh Dickins 
30891da177e4SLinus Torvalds 	/* Allocate our own private page. */
30901da177e4SLinus Torvalds 	if (unlikely(anon_vma_prepare(vma)))
309165500d23SHugh Dickins 		goto oom;
3092769848c0SMel Gorman 	page = alloc_zeroed_user_highpage_movable(vma, address);
30931da177e4SLinus Torvalds 	if (!page)
309465500d23SHugh Dickins 		goto oom;
30950ed361deSNick Piggin 	__SetPageUptodate(page);
30961da177e4SLinus Torvalds 
30972c26fdd7SKAMEZAWA Hiroyuki 	if (mem_cgroup_newpage_charge(page, mm, GFP_KERNEL))
30988a9f3ccdSBalbir Singh 		goto oom_free_page;
30998a9f3ccdSBalbir Singh 
310065500d23SHugh Dickins 	entry = mk_pte(page, vma->vm_page_prot);
31011ac0cb5dSHugh Dickins 	if (vma->vm_flags & VM_WRITE)
31021ac0cb5dSHugh Dickins 		entry = pte_mkwrite(pte_mkdirty(entry));
31038f4e2101SHugh Dickins 
31048f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
31051c2fb7a4SAndrea Arcangeli 	if (!pte_none(*page_table))
31068f4e2101SHugh Dickins 		goto release;
31079ba69294SHugh Dickins 
310834e55232SKAMEZAWA Hiroyuki 	inc_mm_counter_fast(mm, MM_ANONPAGES);
31099617d95eSNick Piggin 	page_add_new_anon_rmap(page, vma, address);
3110a13ea5b7SHugh Dickins setpte:
311165500d23SHugh Dickins 	set_pte_at(mm, address, page_table, entry);
31121da177e4SLinus Torvalds 
31131da177e4SLinus Torvalds 	/* No need to invalidate - it was non-present before */
31144b3073e1SRussell King 	update_mmu_cache(vma, address, page_table);
311565500d23SHugh Dickins unlock:
31168f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
311783c54070SNick Piggin 	return 0;
31188f4e2101SHugh Dickins release:
31198a9f3ccdSBalbir Singh 	mem_cgroup_uncharge_page(page);
31208f4e2101SHugh Dickins 	page_cache_release(page);
31218f4e2101SHugh Dickins 	goto unlock;
31228a9f3ccdSBalbir Singh oom_free_page:
31236dbf6d3bSHugh Dickins 	page_cache_release(page);
312465500d23SHugh Dickins oom:
31251da177e4SLinus Torvalds 	return VM_FAULT_OOM;
31261da177e4SLinus Torvalds }
31271da177e4SLinus Torvalds 
31281da177e4SLinus Torvalds /*
312954cb8821SNick Piggin  * __do_fault() tries to create a new page mapping. It aggressively
31301da177e4SLinus Torvalds  * tries to share with existing pages, but makes a separate copy if
313154cb8821SNick Piggin  * the FAULT_FLAG_WRITE is set in the flags parameter in order to avoid
313254cb8821SNick Piggin  * the next page fault.
31331da177e4SLinus Torvalds  *
31341da177e4SLinus Torvalds  * As this is called only for pages that do not currently exist, we
31351da177e4SLinus Torvalds  * do not need to flush old virtual caches or the TLB.
31361da177e4SLinus Torvalds  *
31378f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
313816abfa08SHugh Dickins  * but allow concurrent faults), and pte neither mapped nor locked.
31398f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
31401da177e4SLinus Torvalds  */
314154cb8821SNick Piggin static int __do_fault(struct mm_struct *mm, struct vm_area_struct *vma,
314216abfa08SHugh Dickins 		unsigned long address, pmd_t *pmd,
314354cb8821SNick Piggin 		pgoff_t pgoff, unsigned int flags, pte_t orig_pte)
31441da177e4SLinus Torvalds {
314516abfa08SHugh Dickins 	pte_t *page_table;
31468f4e2101SHugh Dickins 	spinlock_t *ptl;
3147d0217ac0SNick Piggin 	struct page *page;
31481da177e4SLinus Torvalds 	pte_t entry;
31491da177e4SLinus Torvalds 	int anon = 0;
31505b4e655eSKAMEZAWA Hiroyuki 	int charged = 0;
3151d08b3851SPeter Zijlstra 	struct page *dirty_page = NULL;
3152d0217ac0SNick Piggin 	struct vm_fault vmf;
3153d0217ac0SNick Piggin 	int ret;
3154a200ee18SPeter Zijlstra 	int page_mkwrite = 0;
315554cb8821SNick Piggin 
3156d0217ac0SNick Piggin 	vmf.virtual_address = (void __user *)(address & PAGE_MASK);
3157d0217ac0SNick Piggin 	vmf.pgoff = pgoff;
3158d0217ac0SNick Piggin 	vmf.flags = flags;
3159d0217ac0SNick Piggin 	vmf.page = NULL;
31601da177e4SLinus Torvalds 
3161d0217ac0SNick Piggin 	ret = vma->vm_ops->fault(vma, &vmf);
3162d065bd81SMichel Lespinasse 	if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE |
3163d065bd81SMichel Lespinasse 			    VM_FAULT_RETRY)))
316483c54070SNick Piggin 		return ret;
31651da177e4SLinus Torvalds 
3166a3b947eaSAndi Kleen 	if (unlikely(PageHWPoison(vmf.page))) {
3167a3b947eaSAndi Kleen 		if (ret & VM_FAULT_LOCKED)
3168a3b947eaSAndi Kleen 			unlock_page(vmf.page);
3169a3b947eaSAndi Kleen 		return VM_FAULT_HWPOISON;
3170a3b947eaSAndi Kleen 	}
3171a3b947eaSAndi Kleen 
3172d00806b1SNick Piggin 	/*
3173d0217ac0SNick Piggin 	 * For consistency in subsequent calls, make the faulted page always
3174d00806b1SNick Piggin 	 * locked.
3175d00806b1SNick Piggin 	 */
317683c54070SNick Piggin 	if (unlikely(!(ret & VM_FAULT_LOCKED)))
3177d0217ac0SNick Piggin 		lock_page(vmf.page);
317854cb8821SNick Piggin 	else
3179d0217ac0SNick Piggin 		VM_BUG_ON(!PageLocked(vmf.page));
3180d00806b1SNick Piggin 
31811da177e4SLinus Torvalds 	/*
31821da177e4SLinus Torvalds 	 * Should we do an early C-O-W break?
31831da177e4SLinus Torvalds 	 */
3184d0217ac0SNick Piggin 	page = vmf.page;
318554cb8821SNick Piggin 	if (flags & FAULT_FLAG_WRITE) {
31869637a5efSDavid Howells 		if (!(vma->vm_flags & VM_SHARED)) {
318754cb8821SNick Piggin 			anon = 1;
3188d00806b1SNick Piggin 			if (unlikely(anon_vma_prepare(vma))) {
3189d0217ac0SNick Piggin 				ret = VM_FAULT_OOM;
319054cb8821SNick Piggin 				goto out;
3191d00806b1SNick Piggin 			}
319283c54070SNick Piggin 			page = alloc_page_vma(GFP_HIGHUSER_MOVABLE,
319383c54070SNick Piggin 						vma, address);
3194d00806b1SNick Piggin 			if (!page) {
3195d0217ac0SNick Piggin 				ret = VM_FAULT_OOM;
319654cb8821SNick Piggin 				goto out;
3197d00806b1SNick Piggin 			}
31982c26fdd7SKAMEZAWA Hiroyuki 			if (mem_cgroup_newpage_charge(page, mm, GFP_KERNEL)) {
31995b4e655eSKAMEZAWA Hiroyuki 				ret = VM_FAULT_OOM;
32005b4e655eSKAMEZAWA Hiroyuki 				page_cache_release(page);
32015b4e655eSKAMEZAWA Hiroyuki 				goto out;
32025b4e655eSKAMEZAWA Hiroyuki 			}
32035b4e655eSKAMEZAWA Hiroyuki 			charged = 1;
3204d0217ac0SNick Piggin 			copy_user_highpage(page, vmf.page, address, vma);
32050ed361deSNick Piggin 			__SetPageUptodate(page);
32069637a5efSDavid Howells 		} else {
320754cb8821SNick Piggin 			/*
320854cb8821SNick Piggin 			 * If the page will be shareable, see if the backing
32099637a5efSDavid Howells 			 * address space wants to know that the page is about
321054cb8821SNick Piggin 			 * to become writable
321154cb8821SNick Piggin 			 */
321269676147SMark Fasheh 			if (vma->vm_ops->page_mkwrite) {
3213c2ec175cSNick Piggin 				int tmp;
3214c2ec175cSNick Piggin 
321569676147SMark Fasheh 				unlock_page(page);
3216b827e496SNick Piggin 				vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
3217c2ec175cSNick Piggin 				tmp = vma->vm_ops->page_mkwrite(vma, &vmf);
3218c2ec175cSNick Piggin 				if (unlikely(tmp &
3219c2ec175cSNick Piggin 					  (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) {
3220c2ec175cSNick Piggin 					ret = tmp;
3221b827e496SNick Piggin 					goto unwritable_page;
322269676147SMark Fasheh 				}
3223b827e496SNick Piggin 				if (unlikely(!(tmp & VM_FAULT_LOCKED))) {
322469676147SMark Fasheh 					lock_page(page);
3225d0217ac0SNick Piggin 					if (!page->mapping) {
3226b827e496SNick Piggin 						ret = 0; /* retry the fault */
3227b827e496SNick Piggin 						unlock_page(page);
3228b827e496SNick Piggin 						goto unwritable_page;
3229d0217ac0SNick Piggin 					}
3230b827e496SNick Piggin 				} else
3231b827e496SNick Piggin 					VM_BUG_ON(!PageLocked(page));
3232a200ee18SPeter Zijlstra 				page_mkwrite = 1;
32339637a5efSDavid Howells 			}
32349637a5efSDavid Howells 		}
323554cb8821SNick Piggin 
32361da177e4SLinus Torvalds 	}
32371da177e4SLinus Torvalds 
32388f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
32391da177e4SLinus Torvalds 
32401da177e4SLinus Torvalds 	/*
32411da177e4SLinus Torvalds 	 * This silly early PAGE_DIRTY setting removes a race
32421da177e4SLinus Torvalds 	 * due to the bad i386 page protection. But it's valid
32431da177e4SLinus Torvalds 	 * for other architectures too.
32441da177e4SLinus Torvalds 	 *
324530c9f3a9SLinus Torvalds 	 * Note that if FAULT_FLAG_WRITE is set, we either now have
32461da177e4SLinus Torvalds 	 * an exclusive copy of the page, or this is a shared mapping,
32471da177e4SLinus Torvalds 	 * so we can make it writable and dirty to avoid having to
32481da177e4SLinus Torvalds 	 * handle that later.
32491da177e4SLinus Torvalds 	 */
32501da177e4SLinus Torvalds 	/* Only go through if we didn't race with anybody else... */
32511c2fb7a4SAndrea Arcangeli 	if (likely(pte_same(*page_table, orig_pte))) {
3252d00806b1SNick Piggin 		flush_icache_page(vma, page);
3253d00806b1SNick Piggin 		entry = mk_pte(page, vma->vm_page_prot);
325454cb8821SNick Piggin 		if (flags & FAULT_FLAG_WRITE)
32551da177e4SLinus Torvalds 			entry = maybe_mkwrite(pte_mkdirty(entry), vma);
32561da177e4SLinus Torvalds 		if (anon) {
325734e55232SKAMEZAWA Hiroyuki 			inc_mm_counter_fast(mm, MM_ANONPAGES);
3258d00806b1SNick Piggin 			page_add_new_anon_rmap(page, vma, address);
3259f57e88a8SHugh Dickins 		} else {
326034e55232SKAMEZAWA Hiroyuki 			inc_mm_counter_fast(mm, MM_FILEPAGES);
3261d00806b1SNick Piggin 			page_add_file_rmap(page);
326254cb8821SNick Piggin 			if (flags & FAULT_FLAG_WRITE) {
3263d00806b1SNick Piggin 				dirty_page = page;
3264d08b3851SPeter Zijlstra 				get_page(dirty_page);
3265d08b3851SPeter Zijlstra 			}
32664294621fSHugh Dickins 		}
326764d6519dSLee Schermerhorn 		set_pte_at(mm, address, page_table, entry);
32681da177e4SLinus Torvalds 
3269d00806b1SNick Piggin 		/* no need to invalidate: a not-present page won't be cached */
32704b3073e1SRussell King 		update_mmu_cache(vma, address, page_table);
3271d00806b1SNick Piggin 	} else {
32725b4e655eSKAMEZAWA Hiroyuki 		if (charged)
32738a9f3ccdSBalbir Singh 			mem_cgroup_uncharge_page(page);
3274d00806b1SNick Piggin 		if (anon)
3275d00806b1SNick Piggin 			page_cache_release(page);
3276d00806b1SNick Piggin 		else
327754cb8821SNick Piggin 			anon = 1; /* no anon but release faulted_page */
3278d00806b1SNick Piggin 	}
3279d00806b1SNick Piggin 
32808f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
3281d00806b1SNick Piggin 
3282d00806b1SNick Piggin out:
3283b827e496SNick Piggin 	if (dirty_page) {
3284b827e496SNick Piggin 		struct address_space *mapping = page->mapping;
32858f7b3d15SAnton Salikhmetov 
3286b827e496SNick Piggin 		if (set_page_dirty(dirty_page))
3287b827e496SNick Piggin 			page_mkwrite = 1;
3288b827e496SNick Piggin 		unlock_page(dirty_page);
3289d08b3851SPeter Zijlstra 		put_page(dirty_page);
3290b827e496SNick Piggin 		if (page_mkwrite && mapping) {
3291b827e496SNick Piggin 			/*
3292b827e496SNick Piggin 			 * Some device drivers do not set page.mapping but still
3293b827e496SNick Piggin 			 * dirty their pages
3294b827e496SNick Piggin 			 */
3295b827e496SNick Piggin 			balance_dirty_pages_ratelimited(mapping);
3296d08b3851SPeter Zijlstra 		}
3297d00806b1SNick Piggin 
3298b827e496SNick Piggin 		/* file_update_time outside page_lock */
3299b827e496SNick Piggin 		if (vma->vm_file)
3300b827e496SNick Piggin 			file_update_time(vma->vm_file);
3301b827e496SNick Piggin 	} else {
3302b827e496SNick Piggin 		unlock_page(vmf.page);
3303b827e496SNick Piggin 		if (anon)
3304b827e496SNick Piggin 			page_cache_release(vmf.page);
3305b827e496SNick Piggin 	}
3306b827e496SNick Piggin 
3307b827e496SNick Piggin 	return ret;
3308b827e496SNick Piggin 
3309b827e496SNick Piggin unwritable_page:
3310b827e496SNick Piggin 	page_cache_release(page);
331183c54070SNick Piggin 	return ret;
331254cb8821SNick Piggin }
3313d00806b1SNick Piggin 
331454cb8821SNick Piggin static int do_linear_fault(struct mm_struct *mm, struct vm_area_struct *vma,
331554cb8821SNick Piggin 		unsigned long address, pte_t *page_table, pmd_t *pmd,
331630c9f3a9SLinus Torvalds 		unsigned int flags, pte_t orig_pte)
331754cb8821SNick Piggin {
331854cb8821SNick Piggin 	pgoff_t pgoff = (((address & PAGE_MASK)
33190da7e01fSDean Nelson 			- vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
332054cb8821SNick Piggin 
332116abfa08SHugh Dickins 	pte_unmap(page_table);
332216abfa08SHugh Dickins 	return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte);
332354cb8821SNick Piggin }
332454cb8821SNick Piggin 
3325f4b81804SJes Sorensen /*
33261da177e4SLinus Torvalds  * Fault of a previously existing named mapping. Repopulate the pte
33271da177e4SLinus Torvalds  * from the encoded file_pte if possible. This enables swappable
33281da177e4SLinus Torvalds  * nonlinear vmas.
33298f4e2101SHugh Dickins  *
33308f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
33318f4e2101SHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
33328f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
33331da177e4SLinus Torvalds  */
3334d0217ac0SNick Piggin static int do_nonlinear_fault(struct mm_struct *mm, struct vm_area_struct *vma,
333565500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
333630c9f3a9SLinus Torvalds 		unsigned int flags, pte_t orig_pte)
33371da177e4SLinus Torvalds {
333865500d23SHugh Dickins 	pgoff_t pgoff;
33391da177e4SLinus Torvalds 
334030c9f3a9SLinus Torvalds 	flags |= FAULT_FLAG_NONLINEAR;
334130c9f3a9SLinus Torvalds 
33424c21e2f2SHugh Dickins 	if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
334383c54070SNick Piggin 		return 0;
33441da177e4SLinus Torvalds 
33452509ef26SHugh Dickins 	if (unlikely(!(vma->vm_flags & VM_NONLINEAR))) {
334665500d23SHugh Dickins 		/*
334765500d23SHugh Dickins 		 * Page table corrupted: show pte and kill process.
334865500d23SHugh Dickins 		 */
33493dc14741SHugh Dickins 		print_bad_pte(vma, address, orig_pte, NULL);
3350d99be1a8SHugh Dickins 		return VM_FAULT_SIGBUS;
335165500d23SHugh Dickins 	}
335265500d23SHugh Dickins 
335365500d23SHugh Dickins 	pgoff = pte_to_pgoff(orig_pte);
335416abfa08SHugh Dickins 	return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte);
33551da177e4SLinus Torvalds }
33561da177e4SLinus Torvalds 
33571da177e4SLinus Torvalds /*
33581da177e4SLinus Torvalds  * These routines also need to handle stuff like marking pages dirty
33591da177e4SLinus Torvalds  * and/or accessed for architectures that don't do it in hardware (most
33601da177e4SLinus Torvalds  * RISC architectures).  The early dirtying is also good on the i386.
33611da177e4SLinus Torvalds  *
33621da177e4SLinus Torvalds  * There is also a hook called "update_mmu_cache()" that architectures
33631da177e4SLinus Torvalds  * with external mmu caches can use to update those (ie the Sparc or
33641da177e4SLinus Torvalds  * PowerPC hashed page tables that act as extended TLBs).
33651da177e4SLinus Torvalds  *
3366c74df32cSHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
3367c74df32cSHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
3368c74df32cSHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
33691da177e4SLinus Torvalds  */
337071e3aac0SAndrea Arcangeli int handle_pte_fault(struct mm_struct *mm,
33711da177e4SLinus Torvalds 		     struct vm_area_struct *vma, unsigned long address,
337230c9f3a9SLinus Torvalds 		     pte_t *pte, pmd_t *pmd, unsigned int flags)
33731da177e4SLinus Torvalds {
33741da177e4SLinus Torvalds 	pte_t entry;
33758f4e2101SHugh Dickins 	spinlock_t *ptl;
33761da177e4SLinus Torvalds 
33778dab5241SBenjamin Herrenschmidt 	entry = *pte;
33781da177e4SLinus Torvalds 	if (!pte_present(entry)) {
337965500d23SHugh Dickins 		if (pte_none(entry)) {
3380f4b81804SJes Sorensen 			if (vma->vm_ops) {
33813c18ddd1SNick Piggin 				if (likely(vma->vm_ops->fault))
338254cb8821SNick Piggin 					return do_linear_fault(mm, vma, address,
338330c9f3a9SLinus Torvalds 						pte, pmd, flags, entry);
3384f4b81804SJes Sorensen 			}
3385f4b81804SJes Sorensen 			return do_anonymous_page(mm, vma, address,
338630c9f3a9SLinus Torvalds 						 pte, pmd, flags);
338765500d23SHugh Dickins 		}
33881da177e4SLinus Torvalds 		if (pte_file(entry))
3389d0217ac0SNick Piggin 			return do_nonlinear_fault(mm, vma, address,
339030c9f3a9SLinus Torvalds 					pte, pmd, flags, entry);
339165500d23SHugh Dickins 		return do_swap_page(mm, vma, address,
339230c9f3a9SLinus Torvalds 					pte, pmd, flags, entry);
33931da177e4SLinus Torvalds 	}
33941da177e4SLinus Torvalds 
33954c21e2f2SHugh Dickins 	ptl = pte_lockptr(mm, pmd);
33968f4e2101SHugh Dickins 	spin_lock(ptl);
33978f4e2101SHugh Dickins 	if (unlikely(!pte_same(*pte, entry)))
33988f4e2101SHugh Dickins 		goto unlock;
339930c9f3a9SLinus Torvalds 	if (flags & FAULT_FLAG_WRITE) {
34001da177e4SLinus Torvalds 		if (!pte_write(entry))
34018f4e2101SHugh Dickins 			return do_wp_page(mm, vma, address,
34028f4e2101SHugh Dickins 					pte, pmd, ptl, entry);
34031da177e4SLinus Torvalds 		entry = pte_mkdirty(entry);
34041da177e4SLinus Torvalds 	}
34051da177e4SLinus Torvalds 	entry = pte_mkyoung(entry);
340630c9f3a9SLinus Torvalds 	if (ptep_set_access_flags(vma, address, pte, entry, flags & FAULT_FLAG_WRITE)) {
34074b3073e1SRussell King 		update_mmu_cache(vma, address, pte);
34081a44e149SAndrea Arcangeli 	} else {
34091a44e149SAndrea Arcangeli 		/*
34101a44e149SAndrea Arcangeli 		 * This is needed only for protection faults but the arch code
34111a44e149SAndrea Arcangeli 		 * is not yet telling us if this is a protection fault or not.
34121a44e149SAndrea Arcangeli 		 * This still avoids useless tlb flushes for .text page faults
34131a44e149SAndrea Arcangeli 		 * with threads.
34141a44e149SAndrea Arcangeli 		 */
341530c9f3a9SLinus Torvalds 		if (flags & FAULT_FLAG_WRITE)
341661c77326SShaohua Li 			flush_tlb_fix_spurious_fault(vma, address);
34171a44e149SAndrea Arcangeli 	}
34188f4e2101SHugh Dickins unlock:
34198f4e2101SHugh Dickins 	pte_unmap_unlock(pte, ptl);
342083c54070SNick Piggin 	return 0;
34211da177e4SLinus Torvalds }
34221da177e4SLinus Torvalds 
34231da177e4SLinus Torvalds /*
34241da177e4SLinus Torvalds  * By the time we get here, we already hold the mm semaphore
34251da177e4SLinus Torvalds  */
342683c54070SNick Piggin int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
3427d06063ccSLinus Torvalds 		unsigned long address, unsigned int flags)
34281da177e4SLinus Torvalds {
34291da177e4SLinus Torvalds 	pgd_t *pgd;
34301da177e4SLinus Torvalds 	pud_t *pud;
34311da177e4SLinus Torvalds 	pmd_t *pmd;
34321da177e4SLinus Torvalds 	pte_t *pte;
34331da177e4SLinus Torvalds 
34341da177e4SLinus Torvalds 	__set_current_state(TASK_RUNNING);
34351da177e4SLinus Torvalds 
3436f8891e5eSChristoph Lameter 	count_vm_event(PGFAULT);
34371da177e4SLinus Torvalds 
343834e55232SKAMEZAWA Hiroyuki 	/* do counter updates before entering really critical section. */
343934e55232SKAMEZAWA Hiroyuki 	check_sync_rss_stat(current);
344034e55232SKAMEZAWA Hiroyuki 
3441ac9b9c66SHugh Dickins 	if (unlikely(is_vm_hugetlb_page(vma)))
344230c9f3a9SLinus Torvalds 		return hugetlb_fault(mm, vma, address, flags);
34431da177e4SLinus Torvalds 
34441da177e4SLinus Torvalds 	pgd = pgd_offset(mm, address);
34451da177e4SLinus Torvalds 	pud = pud_alloc(mm, pgd, address);
34461da177e4SLinus Torvalds 	if (!pud)
3447c74df32cSHugh Dickins 		return VM_FAULT_OOM;
34481da177e4SLinus Torvalds 	pmd = pmd_alloc(mm, pud, address);
34491da177e4SLinus Torvalds 	if (!pmd)
3450c74df32cSHugh Dickins 		return VM_FAULT_OOM;
345171e3aac0SAndrea Arcangeli 	if (pmd_none(*pmd) && transparent_hugepage_enabled(vma)) {
345271e3aac0SAndrea Arcangeli 		if (!vma->vm_ops)
345371e3aac0SAndrea Arcangeli 			return do_huge_pmd_anonymous_page(mm, vma, address,
345471e3aac0SAndrea Arcangeli 							  pmd, flags);
345571e3aac0SAndrea Arcangeli 	} else {
345671e3aac0SAndrea Arcangeli 		pmd_t orig_pmd = *pmd;
345771e3aac0SAndrea Arcangeli 		barrier();
345871e3aac0SAndrea Arcangeli 		if (pmd_trans_huge(orig_pmd)) {
345971e3aac0SAndrea Arcangeli 			if (flags & FAULT_FLAG_WRITE &&
346071e3aac0SAndrea Arcangeli 			    !pmd_write(orig_pmd) &&
346171e3aac0SAndrea Arcangeli 			    !pmd_trans_splitting(orig_pmd))
346271e3aac0SAndrea Arcangeli 				return do_huge_pmd_wp_page(mm, vma, address,
346371e3aac0SAndrea Arcangeli 							   pmd, orig_pmd);
346471e3aac0SAndrea Arcangeli 			return 0;
346571e3aac0SAndrea Arcangeli 		}
346671e3aac0SAndrea Arcangeli 	}
346771e3aac0SAndrea Arcangeli 
346871e3aac0SAndrea Arcangeli 	/*
346971e3aac0SAndrea Arcangeli 	 * Use __pte_alloc instead of pte_alloc_map, because we can't
347071e3aac0SAndrea Arcangeli 	 * run pte_offset_map on the pmd, if an huge pmd could
347171e3aac0SAndrea Arcangeli 	 * materialize from under us from a different thread.
347271e3aac0SAndrea Arcangeli 	 */
3473cc03638dSMel Gorman 	if (unlikely(pmd_none(*pmd)) && __pte_alloc(mm, vma, pmd, address))
3474c74df32cSHugh Dickins 		return VM_FAULT_OOM;
347571e3aac0SAndrea Arcangeli 	/* if an huge pmd materialized from under us just retry later */
347671e3aac0SAndrea Arcangeli 	if (unlikely(pmd_trans_huge(*pmd)))
347771e3aac0SAndrea Arcangeli 		return 0;
347871e3aac0SAndrea Arcangeli 	/*
347971e3aac0SAndrea Arcangeli 	 * A regular pmd is established and it can't morph into a huge pmd
348071e3aac0SAndrea Arcangeli 	 * from under us anymore at this point because we hold the mmap_sem
348171e3aac0SAndrea Arcangeli 	 * read mode and khugepaged takes it in write mode. So now it's
348271e3aac0SAndrea Arcangeli 	 * safe to run pte_offset_map().
348371e3aac0SAndrea Arcangeli 	 */
348471e3aac0SAndrea Arcangeli 	pte = pte_offset_map(pmd, address);
34851da177e4SLinus Torvalds 
348630c9f3a9SLinus Torvalds 	return handle_pte_fault(mm, vma, address, pte, pmd, flags);
34871da177e4SLinus Torvalds }
34881da177e4SLinus Torvalds 
34891da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED
34901da177e4SLinus Torvalds /*
34911da177e4SLinus Torvalds  * Allocate page upper directory.
3492872fec16SHugh Dickins  * We've already handled the fast-path in-line.
34931da177e4SLinus Torvalds  */
34941bb3630eSHugh Dickins int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
34951da177e4SLinus Torvalds {
3496c74df32cSHugh Dickins 	pud_t *new = pud_alloc_one(mm, address);
3497c74df32cSHugh Dickins 	if (!new)
34981bb3630eSHugh Dickins 		return -ENOMEM;
34991da177e4SLinus Torvalds 
3500362a61adSNick Piggin 	smp_wmb(); /* See comment in __pte_alloc */
3501362a61adSNick Piggin 
3502872fec16SHugh Dickins 	spin_lock(&mm->page_table_lock);
35031bb3630eSHugh Dickins 	if (pgd_present(*pgd))		/* Another has populated it */
35045e541973SBenjamin Herrenschmidt 		pud_free(mm, new);
35051bb3630eSHugh Dickins 	else
35061da177e4SLinus Torvalds 		pgd_populate(mm, pgd, new);
3507872fec16SHugh Dickins 	spin_unlock(&mm->page_table_lock);
35081bb3630eSHugh Dickins 	return 0;
35091da177e4SLinus Torvalds }
35101da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */
35111da177e4SLinus Torvalds 
35121da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED
35131da177e4SLinus Torvalds /*
35141da177e4SLinus Torvalds  * Allocate page middle directory.
3515872fec16SHugh Dickins  * We've already handled the fast-path in-line.
35161da177e4SLinus Torvalds  */
35171bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
35181da177e4SLinus Torvalds {
3519c74df32cSHugh Dickins 	pmd_t *new = pmd_alloc_one(mm, address);
3520c74df32cSHugh Dickins 	if (!new)
35211bb3630eSHugh Dickins 		return -ENOMEM;
35221da177e4SLinus Torvalds 
3523362a61adSNick Piggin 	smp_wmb(); /* See comment in __pte_alloc */
3524362a61adSNick Piggin 
3525872fec16SHugh Dickins 	spin_lock(&mm->page_table_lock);
35261da177e4SLinus Torvalds #ifndef __ARCH_HAS_4LEVEL_HACK
35271bb3630eSHugh Dickins 	if (pud_present(*pud))		/* Another has populated it */
35285e541973SBenjamin Herrenschmidt 		pmd_free(mm, new);
35291bb3630eSHugh Dickins 	else
35301da177e4SLinus Torvalds 		pud_populate(mm, pud, new);
35311da177e4SLinus Torvalds #else
35321bb3630eSHugh Dickins 	if (pgd_present(*pud))		/* Another has populated it */
35335e541973SBenjamin Herrenschmidt 		pmd_free(mm, new);
35341bb3630eSHugh Dickins 	else
35351da177e4SLinus Torvalds 		pgd_populate(mm, pud, new);
35361da177e4SLinus Torvalds #endif /* __ARCH_HAS_4LEVEL_HACK */
3537872fec16SHugh Dickins 	spin_unlock(&mm->page_table_lock);
35381bb3630eSHugh Dickins 	return 0;
35391da177e4SLinus Torvalds }
35401da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */
35411da177e4SLinus Torvalds 
35421da177e4SLinus Torvalds int make_pages_present(unsigned long addr, unsigned long end)
35431da177e4SLinus Torvalds {
35441da177e4SLinus Torvalds 	int ret, len, write;
35451da177e4SLinus Torvalds 	struct vm_area_struct * vma;
35461da177e4SLinus Torvalds 
35471da177e4SLinus Torvalds 	vma = find_vma(current->mm, addr);
35481da177e4SLinus Torvalds 	if (!vma)
3549a477097dSKOSAKI Motohiro 		return -ENOMEM;
35505ecfda04SMichel Lespinasse 	/*
35515ecfda04SMichel Lespinasse 	 * We want to touch writable mappings with a write fault in order
35525ecfda04SMichel Lespinasse 	 * to break COW, except for shared mappings because these don't COW
35535ecfda04SMichel Lespinasse 	 * and we would not want to dirty them for nothing.
35545ecfda04SMichel Lespinasse 	 */
35555ecfda04SMichel Lespinasse 	write = (vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE;
35565bcb28b1SEric Sesterhenn 	BUG_ON(addr >= end);
35575bcb28b1SEric Sesterhenn 	BUG_ON(end > vma->vm_end);
355868e116a3SRolf Eike Beer 	len = DIV_ROUND_UP(end, PAGE_SIZE) - addr/PAGE_SIZE;
35591da177e4SLinus Torvalds 	ret = get_user_pages(current, current->mm, addr,
35601da177e4SLinus Torvalds 			len, write, 0, NULL, NULL);
3561c11d69d8SLee Schermerhorn 	if (ret < 0)
35621da177e4SLinus Torvalds 		return ret;
35639978ad58SLee Schermerhorn 	return ret == len ? 0 : -EFAULT;
35641da177e4SLinus Torvalds }
35651da177e4SLinus Torvalds 
35661da177e4SLinus Torvalds #if !defined(__HAVE_ARCH_GATE_AREA)
35671da177e4SLinus Torvalds 
35681da177e4SLinus Torvalds #if defined(AT_SYSINFO_EHDR)
35695ce7852cSAdrian Bunk static struct vm_area_struct gate_vma;
35701da177e4SLinus Torvalds 
35711da177e4SLinus Torvalds static int __init gate_vma_init(void)
35721da177e4SLinus Torvalds {
35731da177e4SLinus Torvalds 	gate_vma.vm_mm = NULL;
35741da177e4SLinus Torvalds 	gate_vma.vm_start = FIXADDR_USER_START;
35751da177e4SLinus Torvalds 	gate_vma.vm_end = FIXADDR_USER_END;
3576b6558c4aSRoland McGrath 	gate_vma.vm_flags = VM_READ | VM_MAYREAD | VM_EXEC | VM_MAYEXEC;
3577b6558c4aSRoland McGrath 	gate_vma.vm_page_prot = __P101;
3578f47aef55SRoland McGrath 	/*
3579f47aef55SRoland McGrath 	 * Make sure the vDSO gets into every core dump.
3580f47aef55SRoland McGrath 	 * Dumping its contents makes post-mortem fully interpretable later
3581f47aef55SRoland McGrath 	 * without matching up the same kernel and hardware config to see
3582f47aef55SRoland McGrath 	 * what PC values meant.
3583f47aef55SRoland McGrath 	 */
3584f47aef55SRoland McGrath 	gate_vma.vm_flags |= VM_ALWAYSDUMP;
35851da177e4SLinus Torvalds 	return 0;
35861da177e4SLinus Torvalds }
35871da177e4SLinus Torvalds __initcall(gate_vma_init);
35881da177e4SLinus Torvalds #endif
35891da177e4SLinus Torvalds 
359031db58b3SStephen Wilson struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
35911da177e4SLinus Torvalds {
35921da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR
35931da177e4SLinus Torvalds 	return &gate_vma;
35941da177e4SLinus Torvalds #else
35951da177e4SLinus Torvalds 	return NULL;
35961da177e4SLinus Torvalds #endif
35971da177e4SLinus Torvalds }
35981da177e4SLinus Torvalds 
3599cae5d390SStephen Wilson int in_gate_area_no_mm(unsigned long addr)
36001da177e4SLinus Torvalds {
36011da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR
36021da177e4SLinus Torvalds 	if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))
36031da177e4SLinus Torvalds 		return 1;
36041da177e4SLinus Torvalds #endif
36051da177e4SLinus Torvalds 	return 0;
36061da177e4SLinus Torvalds }
36071da177e4SLinus Torvalds 
36081da177e4SLinus Torvalds #endif	/* __HAVE_ARCH_GATE_AREA */
36090ec76a11SDavid Howells 
36101b36ba81SNamhyung Kim static int __follow_pte(struct mm_struct *mm, unsigned long address,
3611f8ad0f49SJohannes Weiner 		pte_t **ptepp, spinlock_t **ptlp)
3612f8ad0f49SJohannes Weiner {
3613f8ad0f49SJohannes Weiner 	pgd_t *pgd;
3614f8ad0f49SJohannes Weiner 	pud_t *pud;
3615f8ad0f49SJohannes Weiner 	pmd_t *pmd;
3616f8ad0f49SJohannes Weiner 	pte_t *ptep;
3617f8ad0f49SJohannes Weiner 
3618f8ad0f49SJohannes Weiner 	pgd = pgd_offset(mm, address);
3619f8ad0f49SJohannes Weiner 	if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
3620f8ad0f49SJohannes Weiner 		goto out;
3621f8ad0f49SJohannes Weiner 
3622f8ad0f49SJohannes Weiner 	pud = pud_offset(pgd, address);
3623f8ad0f49SJohannes Weiner 	if (pud_none(*pud) || unlikely(pud_bad(*pud)))
3624f8ad0f49SJohannes Weiner 		goto out;
3625f8ad0f49SJohannes Weiner 
3626f8ad0f49SJohannes Weiner 	pmd = pmd_offset(pud, address);
3627f66055abSAndrea Arcangeli 	VM_BUG_ON(pmd_trans_huge(*pmd));
3628f8ad0f49SJohannes Weiner 	if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
3629f8ad0f49SJohannes Weiner 		goto out;
3630f8ad0f49SJohannes Weiner 
3631f8ad0f49SJohannes Weiner 	/* We cannot handle huge page PFN maps. Luckily they don't exist. */
3632f8ad0f49SJohannes Weiner 	if (pmd_huge(*pmd))
3633f8ad0f49SJohannes Weiner 		goto out;
3634f8ad0f49SJohannes Weiner 
3635f8ad0f49SJohannes Weiner 	ptep = pte_offset_map_lock(mm, pmd, address, ptlp);
3636f8ad0f49SJohannes Weiner 	if (!ptep)
3637f8ad0f49SJohannes Weiner 		goto out;
3638f8ad0f49SJohannes Weiner 	if (!pte_present(*ptep))
3639f8ad0f49SJohannes Weiner 		goto unlock;
3640f8ad0f49SJohannes Weiner 	*ptepp = ptep;
3641f8ad0f49SJohannes Weiner 	return 0;
3642f8ad0f49SJohannes Weiner unlock:
3643f8ad0f49SJohannes Weiner 	pte_unmap_unlock(ptep, *ptlp);
3644f8ad0f49SJohannes Weiner out:
3645f8ad0f49SJohannes Weiner 	return -EINVAL;
3646f8ad0f49SJohannes Weiner }
3647f8ad0f49SJohannes Weiner 
36481b36ba81SNamhyung Kim static inline int follow_pte(struct mm_struct *mm, unsigned long address,
36491b36ba81SNamhyung Kim 			     pte_t **ptepp, spinlock_t **ptlp)
36501b36ba81SNamhyung Kim {
36511b36ba81SNamhyung Kim 	int res;
36521b36ba81SNamhyung Kim 
36531b36ba81SNamhyung Kim 	/* (void) is needed to make gcc happy */
36541b36ba81SNamhyung Kim 	(void) __cond_lock(*ptlp,
36551b36ba81SNamhyung Kim 			   !(res = __follow_pte(mm, address, ptepp, ptlp)));
36561b36ba81SNamhyung Kim 	return res;
36571b36ba81SNamhyung Kim }
36581b36ba81SNamhyung Kim 
36593b6748e2SJohannes Weiner /**
36603b6748e2SJohannes Weiner  * follow_pfn - look up PFN at a user virtual address
36613b6748e2SJohannes Weiner  * @vma: memory mapping
36623b6748e2SJohannes Weiner  * @address: user virtual address
36633b6748e2SJohannes Weiner  * @pfn: location to store found PFN
36643b6748e2SJohannes Weiner  *
36653b6748e2SJohannes Weiner  * Only IO mappings and raw PFN mappings are allowed.
36663b6748e2SJohannes Weiner  *
36673b6748e2SJohannes Weiner  * Returns zero and the pfn at @pfn on success, -ve otherwise.
36683b6748e2SJohannes Weiner  */
36693b6748e2SJohannes Weiner int follow_pfn(struct vm_area_struct *vma, unsigned long address,
36703b6748e2SJohannes Weiner 	unsigned long *pfn)
36713b6748e2SJohannes Weiner {
36723b6748e2SJohannes Weiner 	int ret = -EINVAL;
36733b6748e2SJohannes Weiner 	spinlock_t *ptl;
36743b6748e2SJohannes Weiner 	pte_t *ptep;
36753b6748e2SJohannes Weiner 
36763b6748e2SJohannes Weiner 	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
36773b6748e2SJohannes Weiner 		return ret;
36783b6748e2SJohannes Weiner 
36793b6748e2SJohannes Weiner 	ret = follow_pte(vma->vm_mm, address, &ptep, &ptl);
36803b6748e2SJohannes Weiner 	if (ret)
36813b6748e2SJohannes Weiner 		return ret;
36823b6748e2SJohannes Weiner 	*pfn = pte_pfn(*ptep);
36833b6748e2SJohannes Weiner 	pte_unmap_unlock(ptep, ptl);
36843b6748e2SJohannes Weiner 	return 0;
36853b6748e2SJohannes Weiner }
36863b6748e2SJohannes Weiner EXPORT_SYMBOL(follow_pfn);
36873b6748e2SJohannes Weiner 
368828b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT
3689d87fe660Svenkatesh.pallipadi@intel.com int follow_phys(struct vm_area_struct *vma,
369028b2ee20SRik van Riel 		unsigned long address, unsigned int flags,
3691d87fe660Svenkatesh.pallipadi@intel.com 		unsigned long *prot, resource_size_t *phys)
369228b2ee20SRik van Riel {
369303668a4dSJohannes Weiner 	int ret = -EINVAL;
369428b2ee20SRik van Riel 	pte_t *ptep, pte;
369528b2ee20SRik van Riel 	spinlock_t *ptl;
369628b2ee20SRik van Riel 
3697d87fe660Svenkatesh.pallipadi@intel.com 	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
3698d87fe660Svenkatesh.pallipadi@intel.com 		goto out;
369928b2ee20SRik van Riel 
370003668a4dSJohannes Weiner 	if (follow_pte(vma->vm_mm, address, &ptep, &ptl))
3701d87fe660Svenkatesh.pallipadi@intel.com 		goto out;
370228b2ee20SRik van Riel 	pte = *ptep;
370303668a4dSJohannes Weiner 
370428b2ee20SRik van Riel 	if ((flags & FOLL_WRITE) && !pte_write(pte))
370528b2ee20SRik van Riel 		goto unlock;
370628b2ee20SRik van Riel 
370728b2ee20SRik van Riel 	*prot = pgprot_val(pte_pgprot(pte));
370803668a4dSJohannes Weiner 	*phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT;
370928b2ee20SRik van Riel 
371003668a4dSJohannes Weiner 	ret = 0;
371128b2ee20SRik van Riel unlock:
371228b2ee20SRik van Riel 	pte_unmap_unlock(ptep, ptl);
371328b2ee20SRik van Riel out:
3714d87fe660Svenkatesh.pallipadi@intel.com 	return ret;
371528b2ee20SRik van Riel }
371628b2ee20SRik van Riel 
371728b2ee20SRik van Riel int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
371828b2ee20SRik van Riel 			void *buf, int len, int write)
371928b2ee20SRik van Riel {
372028b2ee20SRik van Riel 	resource_size_t phys_addr;
372128b2ee20SRik van Riel 	unsigned long prot = 0;
37222bc7273bSKOSAKI Motohiro 	void __iomem *maddr;
372328b2ee20SRik van Riel 	int offset = addr & (PAGE_SIZE-1);
372428b2ee20SRik van Riel 
3725d87fe660Svenkatesh.pallipadi@intel.com 	if (follow_phys(vma, addr, write, &prot, &phys_addr))
372628b2ee20SRik van Riel 		return -EINVAL;
372728b2ee20SRik van Riel 
372828b2ee20SRik van Riel 	maddr = ioremap_prot(phys_addr, PAGE_SIZE, prot);
372928b2ee20SRik van Riel 	if (write)
373028b2ee20SRik van Riel 		memcpy_toio(maddr + offset, buf, len);
373128b2ee20SRik van Riel 	else
373228b2ee20SRik van Riel 		memcpy_fromio(buf, maddr + offset, len);
373328b2ee20SRik van Riel 	iounmap(maddr);
373428b2ee20SRik van Riel 
373528b2ee20SRik van Riel 	return len;
373628b2ee20SRik van Riel }
373728b2ee20SRik van Riel #endif
373828b2ee20SRik van Riel 
37390ec76a11SDavid Howells /*
3740206cb636SStephen Wilson  * Access another process' address space as given in mm.  If non-NULL, use the
3741206cb636SStephen Wilson  * given task for page fault accounting.
37420ec76a11SDavid Howells  */
3743206cb636SStephen Wilson static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
3744206cb636SStephen Wilson 		unsigned long addr, void *buf, int len, int write)
37450ec76a11SDavid Howells {
37460ec76a11SDavid Howells 	struct vm_area_struct *vma;
37470ec76a11SDavid Howells 	void *old_buf = buf;
37480ec76a11SDavid Howells 
37490ec76a11SDavid Howells 	down_read(&mm->mmap_sem);
3750183ff22bSSimon Arlott 	/* ignore errors, just check how much was successfully transferred */
37510ec76a11SDavid Howells 	while (len) {
37520ec76a11SDavid Howells 		int bytes, ret, offset;
37530ec76a11SDavid Howells 		void *maddr;
375428b2ee20SRik van Riel 		struct page *page = NULL;
37550ec76a11SDavid Howells 
37560ec76a11SDavid Howells 		ret = get_user_pages(tsk, mm, addr, 1,
37570ec76a11SDavid Howells 				write, 1, &page, &vma);
375828b2ee20SRik van Riel 		if (ret <= 0) {
375928b2ee20SRik van Riel 			/*
376028b2ee20SRik van Riel 			 * Check if this is a VM_IO | VM_PFNMAP VMA, which
376128b2ee20SRik van Riel 			 * we can access using slightly different code.
376228b2ee20SRik van Riel 			 */
376328b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT
376428b2ee20SRik van Riel 			vma = find_vma(mm, addr);
3765fe936dfcSMichael Ellerman 			if (!vma || vma->vm_start > addr)
37660ec76a11SDavid Howells 				break;
376728b2ee20SRik van Riel 			if (vma->vm_ops && vma->vm_ops->access)
376828b2ee20SRik van Riel 				ret = vma->vm_ops->access(vma, addr, buf,
376928b2ee20SRik van Riel 							  len, write);
377028b2ee20SRik van Riel 			if (ret <= 0)
377128b2ee20SRik van Riel #endif
377228b2ee20SRik van Riel 				break;
377328b2ee20SRik van Riel 			bytes = ret;
377428b2ee20SRik van Riel 		} else {
37750ec76a11SDavid Howells 			bytes = len;
37760ec76a11SDavid Howells 			offset = addr & (PAGE_SIZE-1);
37770ec76a11SDavid Howells 			if (bytes > PAGE_SIZE-offset)
37780ec76a11SDavid Howells 				bytes = PAGE_SIZE-offset;
37790ec76a11SDavid Howells 
37800ec76a11SDavid Howells 			maddr = kmap(page);
37810ec76a11SDavid Howells 			if (write) {
37820ec76a11SDavid Howells 				copy_to_user_page(vma, page, addr,
37830ec76a11SDavid Howells 						  maddr + offset, buf, bytes);
37840ec76a11SDavid Howells 				set_page_dirty_lock(page);
37850ec76a11SDavid Howells 			} else {
37860ec76a11SDavid Howells 				copy_from_user_page(vma, page, addr,
37870ec76a11SDavid Howells 						    buf, maddr + offset, bytes);
37880ec76a11SDavid Howells 			}
37890ec76a11SDavid Howells 			kunmap(page);
37900ec76a11SDavid Howells 			page_cache_release(page);
379128b2ee20SRik van Riel 		}
37920ec76a11SDavid Howells 		len -= bytes;
37930ec76a11SDavid Howells 		buf += bytes;
37940ec76a11SDavid Howells 		addr += bytes;
37950ec76a11SDavid Howells 	}
37960ec76a11SDavid Howells 	up_read(&mm->mmap_sem);
37970ec76a11SDavid Howells 
37980ec76a11SDavid Howells 	return buf - old_buf;
37990ec76a11SDavid Howells }
380003252919SAndi Kleen 
38015ddd36b9SStephen Wilson /**
3802ae91dbfcSRandy Dunlap  * access_remote_vm - access another process' address space
38035ddd36b9SStephen Wilson  * @mm:		the mm_struct of the target address space
38045ddd36b9SStephen Wilson  * @addr:	start address to access
38055ddd36b9SStephen Wilson  * @buf:	source or destination buffer
38065ddd36b9SStephen Wilson  * @len:	number of bytes to transfer
38075ddd36b9SStephen Wilson  * @write:	whether the access is a write
38085ddd36b9SStephen Wilson  *
38095ddd36b9SStephen Wilson  * The caller must hold a reference on @mm.
38105ddd36b9SStephen Wilson  */
38115ddd36b9SStephen Wilson int access_remote_vm(struct mm_struct *mm, unsigned long addr,
38125ddd36b9SStephen Wilson 		void *buf, int len, int write)
38135ddd36b9SStephen Wilson {
38145ddd36b9SStephen Wilson 	return __access_remote_vm(NULL, mm, addr, buf, len, write);
38155ddd36b9SStephen Wilson }
38165ddd36b9SStephen Wilson 
381703252919SAndi Kleen /*
3818206cb636SStephen Wilson  * Access another process' address space.
3819206cb636SStephen Wilson  * Source/target buffer must be kernel space,
3820206cb636SStephen Wilson  * Do not walk the page table directly, use get_user_pages
3821206cb636SStephen Wilson  */
3822206cb636SStephen Wilson int access_process_vm(struct task_struct *tsk, unsigned long addr,
3823206cb636SStephen Wilson 		void *buf, int len, int write)
3824206cb636SStephen Wilson {
3825206cb636SStephen Wilson 	struct mm_struct *mm;
3826206cb636SStephen Wilson 	int ret;
3827206cb636SStephen Wilson 
3828206cb636SStephen Wilson 	mm = get_task_mm(tsk);
3829206cb636SStephen Wilson 	if (!mm)
3830206cb636SStephen Wilson 		return 0;
3831206cb636SStephen Wilson 
3832206cb636SStephen Wilson 	ret = __access_remote_vm(tsk, mm, addr, buf, len, write);
3833206cb636SStephen Wilson 	mmput(mm);
3834206cb636SStephen Wilson 
3835206cb636SStephen Wilson 	return ret;
3836206cb636SStephen Wilson }
3837206cb636SStephen Wilson 
383803252919SAndi Kleen /*
383903252919SAndi Kleen  * Print the name of a VMA.
384003252919SAndi Kleen  */
384103252919SAndi Kleen void print_vma_addr(char *prefix, unsigned long ip)
384203252919SAndi Kleen {
384303252919SAndi Kleen 	struct mm_struct *mm = current->mm;
384403252919SAndi Kleen 	struct vm_area_struct *vma;
384503252919SAndi Kleen 
3846e8bff74aSIngo Molnar 	/*
3847e8bff74aSIngo Molnar 	 * Do not print if we are in atomic
3848e8bff74aSIngo Molnar 	 * contexts (in exception stacks, etc.):
3849e8bff74aSIngo Molnar 	 */
3850e8bff74aSIngo Molnar 	if (preempt_count())
3851e8bff74aSIngo Molnar 		return;
3852e8bff74aSIngo Molnar 
385303252919SAndi Kleen 	down_read(&mm->mmap_sem);
385403252919SAndi Kleen 	vma = find_vma(mm, ip);
385503252919SAndi Kleen 	if (vma && vma->vm_file) {
385603252919SAndi Kleen 		struct file *f = vma->vm_file;
385703252919SAndi Kleen 		char *buf = (char *)__get_free_page(GFP_KERNEL);
385803252919SAndi Kleen 		if (buf) {
385903252919SAndi Kleen 			char *p, *s;
386003252919SAndi Kleen 
3861cf28b486SJan Blunck 			p = d_path(&f->f_path, buf, PAGE_SIZE);
386203252919SAndi Kleen 			if (IS_ERR(p))
386303252919SAndi Kleen 				p = "?";
386403252919SAndi Kleen 			s = strrchr(p, '/');
386503252919SAndi Kleen 			if (s)
386603252919SAndi Kleen 				p = s+1;
386703252919SAndi Kleen 			printk("%s%s[%lx+%lx]", prefix, p,
386803252919SAndi Kleen 					vma->vm_start,
386903252919SAndi Kleen 					vma->vm_end - vma->vm_start);
387003252919SAndi Kleen 			free_page((unsigned long)buf);
387103252919SAndi Kleen 		}
387203252919SAndi Kleen 	}
387303252919SAndi Kleen 	up_read(&current->mm->mmap_sem);
387403252919SAndi Kleen }
38753ee1afa3SNick Piggin 
38763ee1afa3SNick Piggin #ifdef CONFIG_PROVE_LOCKING
38773ee1afa3SNick Piggin void might_fault(void)
38783ee1afa3SNick Piggin {
387995156f00SPeter Zijlstra 	/*
388095156f00SPeter Zijlstra 	 * Some code (nfs/sunrpc) uses socket ops on kernel memory while
388195156f00SPeter Zijlstra 	 * holding the mmap_sem, this is safe because kernel memory doesn't
388295156f00SPeter Zijlstra 	 * get paged out, therefore we'll never actually fault, and the
388395156f00SPeter Zijlstra 	 * below annotations will generate false positives.
388495156f00SPeter Zijlstra 	 */
388595156f00SPeter Zijlstra 	if (segment_eq(get_fs(), KERNEL_DS))
388695156f00SPeter Zijlstra 		return;
388795156f00SPeter Zijlstra 
38883ee1afa3SNick Piggin 	might_sleep();
38893ee1afa3SNick Piggin 	/*
38903ee1afa3SNick Piggin 	 * it would be nicer only to annotate paths which are not under
38913ee1afa3SNick Piggin 	 * pagefault_disable, however that requires a larger audit and
38923ee1afa3SNick Piggin 	 * providing helpers like get_user_atomic.
38933ee1afa3SNick Piggin 	 */
38943ee1afa3SNick Piggin 	if (!in_atomic() && current->mm)
38953ee1afa3SNick Piggin 		might_lock_read(&current->mm->mmap_sem);
38963ee1afa3SNick Piggin }
38973ee1afa3SNick Piggin EXPORT_SYMBOL(might_fault);
38983ee1afa3SNick Piggin #endif
389947ad8475SAndrea Arcangeli 
390047ad8475SAndrea Arcangeli #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
390147ad8475SAndrea Arcangeli static void clear_gigantic_page(struct page *page,
390247ad8475SAndrea Arcangeli 				unsigned long addr,
390347ad8475SAndrea Arcangeli 				unsigned int pages_per_huge_page)
390447ad8475SAndrea Arcangeli {
390547ad8475SAndrea Arcangeli 	int i;
390647ad8475SAndrea Arcangeli 	struct page *p = page;
390747ad8475SAndrea Arcangeli 
390847ad8475SAndrea Arcangeli 	might_sleep();
390947ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page;
391047ad8475SAndrea Arcangeli 	     i++, p = mem_map_next(p, page, i)) {
391147ad8475SAndrea Arcangeli 		cond_resched();
391247ad8475SAndrea Arcangeli 		clear_user_highpage(p, addr + i * PAGE_SIZE);
391347ad8475SAndrea Arcangeli 	}
391447ad8475SAndrea Arcangeli }
391547ad8475SAndrea Arcangeli void clear_huge_page(struct page *page,
391647ad8475SAndrea Arcangeli 		     unsigned long addr, unsigned int pages_per_huge_page)
391747ad8475SAndrea Arcangeli {
391847ad8475SAndrea Arcangeli 	int i;
391947ad8475SAndrea Arcangeli 
392047ad8475SAndrea Arcangeli 	if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
392147ad8475SAndrea Arcangeli 		clear_gigantic_page(page, addr, pages_per_huge_page);
392247ad8475SAndrea Arcangeli 		return;
392347ad8475SAndrea Arcangeli 	}
392447ad8475SAndrea Arcangeli 
392547ad8475SAndrea Arcangeli 	might_sleep();
392647ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page; i++) {
392747ad8475SAndrea Arcangeli 		cond_resched();
392847ad8475SAndrea Arcangeli 		clear_user_highpage(page + i, addr + i * PAGE_SIZE);
392947ad8475SAndrea Arcangeli 	}
393047ad8475SAndrea Arcangeli }
393147ad8475SAndrea Arcangeli 
393247ad8475SAndrea Arcangeli static void copy_user_gigantic_page(struct page *dst, struct page *src,
393347ad8475SAndrea Arcangeli 				    unsigned long addr,
393447ad8475SAndrea Arcangeli 				    struct vm_area_struct *vma,
393547ad8475SAndrea Arcangeli 				    unsigned int pages_per_huge_page)
393647ad8475SAndrea Arcangeli {
393747ad8475SAndrea Arcangeli 	int i;
393847ad8475SAndrea Arcangeli 	struct page *dst_base = dst;
393947ad8475SAndrea Arcangeli 	struct page *src_base = src;
394047ad8475SAndrea Arcangeli 
394147ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page; ) {
394247ad8475SAndrea Arcangeli 		cond_resched();
394347ad8475SAndrea Arcangeli 		copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma);
394447ad8475SAndrea Arcangeli 
394547ad8475SAndrea Arcangeli 		i++;
394647ad8475SAndrea Arcangeli 		dst = mem_map_next(dst, dst_base, i);
394747ad8475SAndrea Arcangeli 		src = mem_map_next(src, src_base, i);
394847ad8475SAndrea Arcangeli 	}
394947ad8475SAndrea Arcangeli }
395047ad8475SAndrea Arcangeli 
395147ad8475SAndrea Arcangeli void copy_user_huge_page(struct page *dst, struct page *src,
395247ad8475SAndrea Arcangeli 			 unsigned long addr, struct vm_area_struct *vma,
395347ad8475SAndrea Arcangeli 			 unsigned int pages_per_huge_page)
395447ad8475SAndrea Arcangeli {
395547ad8475SAndrea Arcangeli 	int i;
395647ad8475SAndrea Arcangeli 
395747ad8475SAndrea Arcangeli 	if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
395847ad8475SAndrea Arcangeli 		copy_user_gigantic_page(dst, src, addr, vma,
395947ad8475SAndrea Arcangeli 					pages_per_huge_page);
396047ad8475SAndrea Arcangeli 		return;
396147ad8475SAndrea Arcangeli 	}
396247ad8475SAndrea Arcangeli 
396347ad8475SAndrea Arcangeli 	might_sleep();
396447ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page; i++) {
396547ad8475SAndrea Arcangeli 		cond_resched();
396647ad8475SAndrea Arcangeli 		copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
396747ad8475SAndrea Arcangeli 	}
396847ad8475SAndrea Arcangeli }
396947ad8475SAndrea Arcangeli #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */
3970