xref: /linux/mm/memory.c (revision 71e3aac0724ffe8918992d76acfe3aad7d8724a5)
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 
1961da177e4SLinus Torvalds /*
1971da177e4SLinus Torvalds  * If a p?d_bad entry is found while walking page tables, report
1981da177e4SLinus Torvalds  * the error, before resetting entry to p?d_none.  Usually (but
1991da177e4SLinus Torvalds  * very seldom) called out from the p?d_none_or_clear_bad macros.
2001da177e4SLinus Torvalds  */
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds void pgd_clear_bad(pgd_t *pgd)
2031da177e4SLinus Torvalds {
2041da177e4SLinus Torvalds 	pgd_ERROR(*pgd);
2051da177e4SLinus Torvalds 	pgd_clear(pgd);
2061da177e4SLinus Torvalds }
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds void pud_clear_bad(pud_t *pud)
2091da177e4SLinus Torvalds {
2101da177e4SLinus Torvalds 	pud_ERROR(*pud);
2111da177e4SLinus Torvalds 	pud_clear(pud);
2121da177e4SLinus Torvalds }
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds void pmd_clear_bad(pmd_t *pmd)
2151da177e4SLinus Torvalds {
2161da177e4SLinus Torvalds 	pmd_ERROR(*pmd);
2171da177e4SLinus Torvalds 	pmd_clear(pmd);
2181da177e4SLinus Torvalds }
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds /*
2211da177e4SLinus Torvalds  * Note: this doesn't free the actual pages themselves. That
2221da177e4SLinus Torvalds  * has been handled earlier when unmapping all the memory regions.
2231da177e4SLinus Torvalds  */
2249e1b32caSBenjamin Herrenschmidt static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd,
2259e1b32caSBenjamin Herrenschmidt 			   unsigned long addr)
2261da177e4SLinus Torvalds {
2272f569afdSMartin Schwidefsky 	pgtable_t token = pmd_pgtable(*pmd);
2281da177e4SLinus Torvalds 	pmd_clear(pmd);
2299e1b32caSBenjamin Herrenschmidt 	pte_free_tlb(tlb, token, addr);
2301da177e4SLinus Torvalds 	tlb->mm->nr_ptes--;
2311da177e4SLinus Torvalds }
2321da177e4SLinus Torvalds 
233e0da382cSHugh Dickins static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
234e0da382cSHugh Dickins 				unsigned long addr, unsigned long end,
235e0da382cSHugh Dickins 				unsigned long floor, unsigned long ceiling)
2361da177e4SLinus Torvalds {
2371da177e4SLinus Torvalds 	pmd_t *pmd;
2381da177e4SLinus Torvalds 	unsigned long next;
239e0da382cSHugh Dickins 	unsigned long start;
2401da177e4SLinus Torvalds 
241e0da382cSHugh Dickins 	start = addr;
2421da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
2431da177e4SLinus Torvalds 	do {
2441da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
2451da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(pmd))
2461da177e4SLinus Torvalds 			continue;
2479e1b32caSBenjamin Herrenschmidt 		free_pte_range(tlb, pmd, addr);
2481da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
2491da177e4SLinus Torvalds 
250e0da382cSHugh Dickins 	start &= PUD_MASK;
251e0da382cSHugh Dickins 	if (start < floor)
252e0da382cSHugh Dickins 		return;
253e0da382cSHugh Dickins 	if (ceiling) {
254e0da382cSHugh Dickins 		ceiling &= PUD_MASK;
255e0da382cSHugh Dickins 		if (!ceiling)
256e0da382cSHugh Dickins 			return;
2571da177e4SLinus Torvalds 	}
258e0da382cSHugh Dickins 	if (end - 1 > ceiling - 1)
259e0da382cSHugh Dickins 		return;
260e0da382cSHugh Dickins 
261e0da382cSHugh Dickins 	pmd = pmd_offset(pud, start);
262e0da382cSHugh Dickins 	pud_clear(pud);
2639e1b32caSBenjamin Herrenschmidt 	pmd_free_tlb(tlb, pmd, start);
2641da177e4SLinus Torvalds }
2651da177e4SLinus Torvalds 
266e0da382cSHugh Dickins static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
267e0da382cSHugh Dickins 				unsigned long addr, unsigned long end,
268e0da382cSHugh Dickins 				unsigned long floor, unsigned long ceiling)
2691da177e4SLinus Torvalds {
2701da177e4SLinus Torvalds 	pud_t *pud;
2711da177e4SLinus Torvalds 	unsigned long next;
272e0da382cSHugh Dickins 	unsigned long start;
2731da177e4SLinus Torvalds 
274e0da382cSHugh Dickins 	start = addr;
2751da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
2761da177e4SLinus Torvalds 	do {
2771da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
2781da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
2791da177e4SLinus Torvalds 			continue;
280e0da382cSHugh Dickins 		free_pmd_range(tlb, pud, addr, next, floor, ceiling);
2811da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
2821da177e4SLinus Torvalds 
283e0da382cSHugh Dickins 	start &= PGDIR_MASK;
284e0da382cSHugh Dickins 	if (start < floor)
285e0da382cSHugh Dickins 		return;
286e0da382cSHugh Dickins 	if (ceiling) {
287e0da382cSHugh Dickins 		ceiling &= PGDIR_MASK;
288e0da382cSHugh Dickins 		if (!ceiling)
289e0da382cSHugh Dickins 			return;
2901da177e4SLinus Torvalds 	}
291e0da382cSHugh Dickins 	if (end - 1 > ceiling - 1)
292e0da382cSHugh Dickins 		return;
293e0da382cSHugh Dickins 
294e0da382cSHugh Dickins 	pud = pud_offset(pgd, start);
295e0da382cSHugh Dickins 	pgd_clear(pgd);
2969e1b32caSBenjamin Herrenschmidt 	pud_free_tlb(tlb, pud, start);
2971da177e4SLinus Torvalds }
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds /*
300e0da382cSHugh Dickins  * This function frees user-level page tables of a process.
301e0da382cSHugh Dickins  *
3021da177e4SLinus Torvalds  * Must be called with pagetable lock held.
3031da177e4SLinus Torvalds  */
30442b77728SJan Beulich void free_pgd_range(struct mmu_gather *tlb,
305e0da382cSHugh Dickins 			unsigned long addr, unsigned long end,
306e0da382cSHugh Dickins 			unsigned long floor, unsigned long ceiling)
3071da177e4SLinus Torvalds {
3081da177e4SLinus Torvalds 	pgd_t *pgd;
3091da177e4SLinus Torvalds 	unsigned long next;
3101da177e4SLinus Torvalds 
311e0da382cSHugh Dickins 	/*
312e0da382cSHugh Dickins 	 * The next few lines have given us lots of grief...
313e0da382cSHugh Dickins 	 *
314e0da382cSHugh Dickins 	 * Why are we testing PMD* at this top level?  Because often
315e0da382cSHugh Dickins 	 * there will be no work to do at all, and we'd prefer not to
316e0da382cSHugh Dickins 	 * go all the way down to the bottom just to discover that.
317e0da382cSHugh Dickins 	 *
318e0da382cSHugh Dickins 	 * Why all these "- 1"s?  Because 0 represents both the bottom
319e0da382cSHugh Dickins 	 * of the address space and the top of it (using -1 for the
320e0da382cSHugh Dickins 	 * top wouldn't help much: the masks would do the wrong thing).
321e0da382cSHugh Dickins 	 * The rule is that addr 0 and floor 0 refer to the bottom of
322e0da382cSHugh Dickins 	 * the address space, but end 0 and ceiling 0 refer to the top
323e0da382cSHugh Dickins 	 * Comparisons need to use "end - 1" and "ceiling - 1" (though
324e0da382cSHugh Dickins 	 * that end 0 case should be mythical).
325e0da382cSHugh Dickins 	 *
326e0da382cSHugh Dickins 	 * Wherever addr is brought up or ceiling brought down, we must
327e0da382cSHugh Dickins 	 * be careful to reject "the opposite 0" before it confuses the
328e0da382cSHugh Dickins 	 * subsequent tests.  But what about where end is brought down
329e0da382cSHugh Dickins 	 * by PMD_SIZE below? no, end can't go down to 0 there.
330e0da382cSHugh Dickins 	 *
331e0da382cSHugh Dickins 	 * Whereas we round start (addr) and ceiling down, by different
332e0da382cSHugh Dickins 	 * masks at different levels, in order to test whether a table
333e0da382cSHugh Dickins 	 * now has no other vmas using it, so can be freed, we don't
334e0da382cSHugh Dickins 	 * bother to round floor or end up - the tests don't need that.
335e0da382cSHugh Dickins 	 */
336e0da382cSHugh Dickins 
337e0da382cSHugh Dickins 	addr &= PMD_MASK;
338e0da382cSHugh Dickins 	if (addr < floor) {
339e0da382cSHugh Dickins 		addr += PMD_SIZE;
340e0da382cSHugh Dickins 		if (!addr)
341e0da382cSHugh Dickins 			return;
342e0da382cSHugh Dickins 	}
343e0da382cSHugh Dickins 	if (ceiling) {
344e0da382cSHugh Dickins 		ceiling &= PMD_MASK;
345e0da382cSHugh Dickins 		if (!ceiling)
346e0da382cSHugh Dickins 			return;
347e0da382cSHugh Dickins 	}
348e0da382cSHugh Dickins 	if (end - 1 > ceiling - 1)
349e0da382cSHugh Dickins 		end -= PMD_SIZE;
350e0da382cSHugh Dickins 	if (addr > end - 1)
351e0da382cSHugh Dickins 		return;
352e0da382cSHugh Dickins 
35342b77728SJan Beulich 	pgd = pgd_offset(tlb->mm, addr);
3541da177e4SLinus Torvalds 	do {
3551da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
3561da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
3571da177e4SLinus Torvalds 			continue;
35842b77728SJan Beulich 		free_pud_range(tlb, pgd, addr, next, floor, ceiling);
3591da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
360e0da382cSHugh Dickins }
361e0da382cSHugh Dickins 
36242b77728SJan Beulich void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma,
363e0da382cSHugh Dickins 		unsigned long floor, unsigned long ceiling)
364e0da382cSHugh Dickins {
365e0da382cSHugh Dickins 	while (vma) {
366e0da382cSHugh Dickins 		struct vm_area_struct *next = vma->vm_next;
367e0da382cSHugh Dickins 		unsigned long addr = vma->vm_start;
368e0da382cSHugh Dickins 
3698f4f8c16SHugh Dickins 		/*
37025d9e2d1Snpiggin@suse.de 		 * Hide vma from rmap and truncate_pagecache before freeing
37125d9e2d1Snpiggin@suse.de 		 * pgtables
3728f4f8c16SHugh Dickins 		 */
3735beb4930SRik van Riel 		unlink_anon_vmas(vma);
3748f4f8c16SHugh Dickins 		unlink_file_vma(vma);
3758f4f8c16SHugh Dickins 
3769da61aefSDavid Gibson 		if (is_vm_hugetlb_page(vma)) {
3773bf5ee95SHugh Dickins 			hugetlb_free_pgd_range(tlb, addr, vma->vm_end,
3783bf5ee95SHugh Dickins 				floor, next? next->vm_start: ceiling);
3793bf5ee95SHugh Dickins 		} else {
3803bf5ee95SHugh Dickins 			/*
3813bf5ee95SHugh Dickins 			 * Optimization: gather nearby vmas into one call down
3823bf5ee95SHugh Dickins 			 */
3833bf5ee95SHugh Dickins 			while (next && next->vm_start <= vma->vm_end + PMD_SIZE
3844866920bSDavid Gibson 			       && !is_vm_hugetlb_page(next)) {
385e0da382cSHugh Dickins 				vma = next;
386e0da382cSHugh Dickins 				next = vma->vm_next;
3875beb4930SRik van Riel 				unlink_anon_vmas(vma);
3888f4f8c16SHugh Dickins 				unlink_file_vma(vma);
389e0da382cSHugh Dickins 			}
3903bf5ee95SHugh Dickins 			free_pgd_range(tlb, addr, vma->vm_end,
391e0da382cSHugh Dickins 				floor, next? next->vm_start: ceiling);
3923bf5ee95SHugh Dickins 		}
393e0da382cSHugh Dickins 		vma = next;
394e0da382cSHugh Dickins 	}
3951da177e4SLinus Torvalds }
3961da177e4SLinus Torvalds 
3978ac1f832SAndrea Arcangeli int __pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma,
3988ac1f832SAndrea Arcangeli 		pmd_t *pmd, unsigned long address)
3991da177e4SLinus Torvalds {
4002f569afdSMartin Schwidefsky 	pgtable_t new = pte_alloc_one(mm, address);
4018ac1f832SAndrea Arcangeli 	int wait_split_huge_page;
4021da177e4SLinus Torvalds 	if (!new)
4031bb3630eSHugh Dickins 		return -ENOMEM;
4041bb3630eSHugh Dickins 
405362a61adSNick Piggin 	/*
406362a61adSNick Piggin 	 * Ensure all pte setup (eg. pte page lock and page clearing) are
407362a61adSNick Piggin 	 * visible before the pte is made visible to other CPUs by being
408362a61adSNick Piggin 	 * put into page tables.
409362a61adSNick Piggin 	 *
410362a61adSNick Piggin 	 * The other side of the story is the pointer chasing in the page
411362a61adSNick Piggin 	 * table walking code (when walking the page table without locking;
412362a61adSNick Piggin 	 * ie. most of the time). Fortunately, these data accesses consist
413362a61adSNick Piggin 	 * of a chain of data-dependent loads, meaning most CPUs (alpha
414362a61adSNick Piggin 	 * being the notable exception) will already guarantee loads are
415362a61adSNick Piggin 	 * seen in-order. See the alpha page table accessors for the
416362a61adSNick Piggin 	 * smp_read_barrier_depends() barriers in page table walking code.
417362a61adSNick Piggin 	 */
418362a61adSNick Piggin 	smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */
419362a61adSNick Piggin 
420c74df32cSHugh Dickins 	spin_lock(&mm->page_table_lock);
4218ac1f832SAndrea Arcangeli 	wait_split_huge_page = 0;
4228ac1f832SAndrea Arcangeli 	if (likely(pmd_none(*pmd))) {	/* Has another populated it ? */
4231da177e4SLinus Torvalds 		mm->nr_ptes++;
4241da177e4SLinus Torvalds 		pmd_populate(mm, pmd, new);
4252f569afdSMartin Schwidefsky 		new = NULL;
4268ac1f832SAndrea Arcangeli 	} else if (unlikely(pmd_trans_splitting(*pmd)))
4278ac1f832SAndrea Arcangeli 		wait_split_huge_page = 1;
428c74df32cSHugh Dickins 	spin_unlock(&mm->page_table_lock);
4292f569afdSMartin Schwidefsky 	if (new)
4302f569afdSMartin Schwidefsky 		pte_free(mm, new);
4318ac1f832SAndrea Arcangeli 	if (wait_split_huge_page)
4328ac1f832SAndrea Arcangeli 		wait_split_huge_page(vma->anon_vma, pmd);
4331bb3630eSHugh Dickins 	return 0;
4341da177e4SLinus Torvalds }
4351da177e4SLinus Torvalds 
4361bb3630eSHugh Dickins int __pte_alloc_kernel(pmd_t *pmd, unsigned long address)
4371da177e4SLinus Torvalds {
4381bb3630eSHugh Dickins 	pte_t *new = pte_alloc_one_kernel(&init_mm, address);
4391da177e4SLinus Torvalds 	if (!new)
4401bb3630eSHugh Dickins 		return -ENOMEM;
4411da177e4SLinus Torvalds 
442362a61adSNick Piggin 	smp_wmb(); /* See comment in __pte_alloc */
443362a61adSNick Piggin 
444872fec16SHugh Dickins 	spin_lock(&init_mm.page_table_lock);
4458ac1f832SAndrea Arcangeli 	if (likely(pmd_none(*pmd))) {	/* Has another populated it ? */
446872fec16SHugh Dickins 		pmd_populate_kernel(&init_mm, pmd, new);
4472f569afdSMartin Schwidefsky 		new = NULL;
4488ac1f832SAndrea Arcangeli 	} else
4498ac1f832SAndrea Arcangeli 		VM_BUG_ON(pmd_trans_splitting(*pmd));
450872fec16SHugh Dickins 	spin_unlock(&init_mm.page_table_lock);
4512f569afdSMartin Schwidefsky 	if (new)
4522f569afdSMartin Schwidefsky 		pte_free_kernel(&init_mm, new);
4531bb3630eSHugh Dickins 	return 0;
4541da177e4SLinus Torvalds }
4551da177e4SLinus Torvalds 
456d559db08SKAMEZAWA Hiroyuki static inline void init_rss_vec(int *rss)
457ae859762SHugh Dickins {
458d559db08SKAMEZAWA Hiroyuki 	memset(rss, 0, sizeof(int) * NR_MM_COUNTERS);
459d559db08SKAMEZAWA Hiroyuki }
460d559db08SKAMEZAWA Hiroyuki 
461d559db08SKAMEZAWA Hiroyuki static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss)
462d559db08SKAMEZAWA Hiroyuki {
463d559db08SKAMEZAWA Hiroyuki 	int i;
464d559db08SKAMEZAWA Hiroyuki 
46534e55232SKAMEZAWA Hiroyuki 	if (current->mm == mm)
46634e55232SKAMEZAWA Hiroyuki 		sync_mm_rss(current, mm);
467d559db08SKAMEZAWA Hiroyuki 	for (i = 0; i < NR_MM_COUNTERS; i++)
468d559db08SKAMEZAWA Hiroyuki 		if (rss[i])
469d559db08SKAMEZAWA Hiroyuki 			add_mm_counter(mm, i, rss[i]);
470ae859762SHugh Dickins }
471ae859762SHugh Dickins 
4721da177e4SLinus Torvalds /*
4736aab341eSLinus Torvalds  * This function is called to print an error when a bad pte
4746aab341eSLinus Torvalds  * is found. For example, we might have a PFN-mapped pte in
4756aab341eSLinus Torvalds  * a region that doesn't allow it.
476b5810039SNick Piggin  *
477b5810039SNick Piggin  * The calling function must still handle the error.
478b5810039SNick Piggin  */
4793dc14741SHugh Dickins static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
4803dc14741SHugh Dickins 			  pte_t pte, struct page *page)
481b5810039SNick Piggin {
4823dc14741SHugh Dickins 	pgd_t *pgd = pgd_offset(vma->vm_mm, addr);
4833dc14741SHugh Dickins 	pud_t *pud = pud_offset(pgd, addr);
4843dc14741SHugh Dickins 	pmd_t *pmd = pmd_offset(pud, addr);
4853dc14741SHugh Dickins 	struct address_space *mapping;
4863dc14741SHugh Dickins 	pgoff_t index;
487d936cf9bSHugh Dickins 	static unsigned long resume;
488d936cf9bSHugh Dickins 	static unsigned long nr_shown;
489d936cf9bSHugh Dickins 	static unsigned long nr_unshown;
490d936cf9bSHugh Dickins 
491d936cf9bSHugh Dickins 	/*
492d936cf9bSHugh Dickins 	 * Allow a burst of 60 reports, then keep quiet for that minute;
493d936cf9bSHugh Dickins 	 * or allow a steady drip of one report per second.
494d936cf9bSHugh Dickins 	 */
495d936cf9bSHugh Dickins 	if (nr_shown == 60) {
496d936cf9bSHugh Dickins 		if (time_before(jiffies, resume)) {
497d936cf9bSHugh Dickins 			nr_unshown++;
498d936cf9bSHugh Dickins 			return;
499d936cf9bSHugh Dickins 		}
500d936cf9bSHugh Dickins 		if (nr_unshown) {
5011e9e6365SHugh Dickins 			printk(KERN_ALERT
5021e9e6365SHugh Dickins 				"BUG: Bad page map: %lu messages suppressed\n",
503d936cf9bSHugh Dickins 				nr_unshown);
504d936cf9bSHugh Dickins 			nr_unshown = 0;
505d936cf9bSHugh Dickins 		}
506d936cf9bSHugh Dickins 		nr_shown = 0;
507d936cf9bSHugh Dickins 	}
508d936cf9bSHugh Dickins 	if (nr_shown++ == 0)
509d936cf9bSHugh Dickins 		resume = jiffies + 60 * HZ;
5103dc14741SHugh Dickins 
5113dc14741SHugh Dickins 	mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL;
5123dc14741SHugh Dickins 	index = linear_page_index(vma, addr);
5133dc14741SHugh Dickins 
5141e9e6365SHugh Dickins 	printk(KERN_ALERT
5151e9e6365SHugh Dickins 		"BUG: Bad page map in process %s  pte:%08llx pmd:%08llx\n",
5163dc14741SHugh Dickins 		current->comm,
5173dc14741SHugh Dickins 		(long long)pte_val(pte), (long long)pmd_val(*pmd));
518718a3821SWu Fengguang 	if (page)
519718a3821SWu Fengguang 		dump_page(page);
5201e9e6365SHugh Dickins 	printk(KERN_ALERT
5213dc14741SHugh Dickins 		"addr:%p vm_flags:%08lx anon_vma:%p mapping:%p index:%lx\n",
5223dc14741SHugh Dickins 		(void *)addr, vma->vm_flags, vma->anon_vma, mapping, index);
5233dc14741SHugh Dickins 	/*
5243dc14741SHugh Dickins 	 * Choose text because data symbols depend on CONFIG_KALLSYMS_ALL=y
5253dc14741SHugh Dickins 	 */
5263dc14741SHugh Dickins 	if (vma->vm_ops)
5271e9e6365SHugh Dickins 		print_symbol(KERN_ALERT "vma->vm_ops->fault: %s\n",
5283dc14741SHugh Dickins 				(unsigned long)vma->vm_ops->fault);
5293dc14741SHugh Dickins 	if (vma->vm_file && vma->vm_file->f_op)
5301e9e6365SHugh Dickins 		print_symbol(KERN_ALERT "vma->vm_file->f_op->mmap: %s\n",
5313dc14741SHugh Dickins 				(unsigned long)vma->vm_file->f_op->mmap);
532b5810039SNick Piggin 	dump_stack();
5333dc14741SHugh Dickins 	add_taint(TAINT_BAD_PAGE);
534b5810039SNick Piggin }
535b5810039SNick Piggin 
53667121172SLinus Torvalds static inline int is_cow_mapping(unsigned int flags)
53767121172SLinus Torvalds {
53867121172SLinus Torvalds 	return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
53967121172SLinus Torvalds }
54067121172SLinus Torvalds 
54162eede62SHugh Dickins #ifndef is_zero_pfn
54262eede62SHugh Dickins static inline int is_zero_pfn(unsigned long pfn)
54362eede62SHugh Dickins {
54462eede62SHugh Dickins 	return pfn == zero_pfn;
54562eede62SHugh Dickins }
54662eede62SHugh Dickins #endif
54762eede62SHugh Dickins 
54862eede62SHugh Dickins #ifndef my_zero_pfn
54962eede62SHugh Dickins static inline unsigned long my_zero_pfn(unsigned long addr)
55062eede62SHugh Dickins {
55162eede62SHugh Dickins 	return zero_pfn;
55262eede62SHugh Dickins }
55362eede62SHugh Dickins #endif
55462eede62SHugh Dickins 
555b5810039SNick Piggin /*
5567e675137SNick Piggin  * vm_normal_page -- This function gets the "struct page" associated with a pte.
5576aab341eSLinus Torvalds  *
5587e675137SNick Piggin  * "Special" mappings do not wish to be associated with a "struct page" (either
5597e675137SNick Piggin  * it doesn't exist, or it exists but they don't want to touch it). In this
5607e675137SNick Piggin  * case, NULL is returned here. "Normal" mappings do have a struct page.
561b379d790SJared Hulbert  *
5627e675137SNick Piggin  * There are 2 broad cases. Firstly, an architecture may define a pte_special()
5637e675137SNick Piggin  * pte bit, in which case this function is trivial. Secondly, an architecture
5647e675137SNick Piggin  * may not have a spare pte bit, which requires a more complicated scheme,
5657e675137SNick Piggin  * described below.
5667e675137SNick Piggin  *
5677e675137SNick Piggin  * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a
5687e675137SNick Piggin  * special mapping (even if there are underlying and valid "struct pages").
5697e675137SNick Piggin  * COWed pages of a VM_PFNMAP are always normal.
5706aab341eSLinus Torvalds  *
571b379d790SJared Hulbert  * The way we recognize COWed pages within VM_PFNMAP mappings is through the
572b379d790SJared Hulbert  * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit
5737e675137SNick Piggin  * set, and the vm_pgoff will point to the first PFN mapped: thus every special
5747e675137SNick Piggin  * mapping will always honor the rule
5756aab341eSLinus Torvalds  *
5766aab341eSLinus Torvalds  *	pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
5776aab341eSLinus Torvalds  *
5787e675137SNick Piggin  * And for normal mappings this is false.
579b379d790SJared Hulbert  *
5807e675137SNick Piggin  * This restricts such mappings to be a linear translation from virtual address
5817e675137SNick Piggin  * to pfn. To get around this restriction, we allow arbitrary mappings so long
5827e675137SNick Piggin  * as the vma is not a COW mapping; in that case, we know that all ptes are
5837e675137SNick Piggin  * special (because none can have been COWed).
584b379d790SJared Hulbert  *
585b379d790SJared Hulbert  *
5867e675137SNick Piggin  * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP.
5877e675137SNick Piggin  *
588b379d790SJared Hulbert  * VM_MIXEDMAP mappings can likewise contain memory with or without "struct
589b379d790SJared Hulbert  * page" backing, however the difference is that _all_ pages with a struct
590b379d790SJared Hulbert  * page (that is, those where pfn_valid is true) are refcounted and considered
591b379d790SJared Hulbert  * normal pages by the VM. The disadvantage is that pages are refcounted
592b379d790SJared Hulbert  * (which can be slower and simply not an option for some PFNMAP users). The
593b379d790SJared Hulbert  * advantage is that we don't have to follow the strict linearity rule of
594b379d790SJared Hulbert  * PFNMAP mappings in order to support COWable mappings.
595b379d790SJared Hulbert  *
596ee498ed7SHugh Dickins  */
5977e675137SNick Piggin #ifdef __HAVE_ARCH_PTE_SPECIAL
5987e675137SNick Piggin # define HAVE_PTE_SPECIAL 1
5997e675137SNick Piggin #else
6007e675137SNick Piggin # define HAVE_PTE_SPECIAL 0
6017e675137SNick Piggin #endif
6027e675137SNick Piggin struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
6037e675137SNick Piggin 				pte_t pte)
604ee498ed7SHugh Dickins {
60522b31eecSHugh Dickins 	unsigned long pfn = pte_pfn(pte);
6067e675137SNick Piggin 
6077e675137SNick Piggin 	if (HAVE_PTE_SPECIAL) {
60822b31eecSHugh Dickins 		if (likely(!pte_special(pte)))
60922b31eecSHugh Dickins 			goto check_pfn;
610a13ea5b7SHugh Dickins 		if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
611a13ea5b7SHugh Dickins 			return NULL;
61262eede62SHugh Dickins 		if (!is_zero_pfn(pfn))
61322b31eecSHugh Dickins 			print_bad_pte(vma, addr, pte, NULL);
6147e675137SNick Piggin 		return NULL;
6157e675137SNick Piggin 	}
6167e675137SNick Piggin 
6177e675137SNick Piggin 	/* !HAVE_PTE_SPECIAL case follows: */
6187e675137SNick Piggin 
619b379d790SJared Hulbert 	if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
620b379d790SJared Hulbert 		if (vma->vm_flags & VM_MIXEDMAP) {
621b379d790SJared Hulbert 			if (!pfn_valid(pfn))
622b379d790SJared Hulbert 				return NULL;
623b379d790SJared Hulbert 			goto out;
624b379d790SJared Hulbert 		} else {
6257e675137SNick Piggin 			unsigned long off;
6267e675137SNick Piggin 			off = (addr - vma->vm_start) >> PAGE_SHIFT;
6276aab341eSLinus Torvalds 			if (pfn == vma->vm_pgoff + off)
6286aab341eSLinus Torvalds 				return NULL;
62967121172SLinus Torvalds 			if (!is_cow_mapping(vma->vm_flags))
630fb155c16SLinus Torvalds 				return NULL;
6316aab341eSLinus Torvalds 		}
632b379d790SJared Hulbert 	}
6336aab341eSLinus Torvalds 
63462eede62SHugh Dickins 	if (is_zero_pfn(pfn))
63562eede62SHugh Dickins 		return NULL;
63622b31eecSHugh Dickins check_pfn:
63722b31eecSHugh Dickins 	if (unlikely(pfn > highest_memmap_pfn)) {
63822b31eecSHugh Dickins 		print_bad_pte(vma, addr, pte, NULL);
63922b31eecSHugh Dickins 		return NULL;
64022b31eecSHugh Dickins 	}
6416aab341eSLinus Torvalds 
6426aab341eSLinus Torvalds 	/*
6437e675137SNick Piggin 	 * NOTE! We still have PageReserved() pages in the page tables.
6447e675137SNick Piggin 	 * eg. VDSO mappings can cause them to exist.
6456aab341eSLinus Torvalds 	 */
646b379d790SJared Hulbert out:
6476aab341eSLinus Torvalds 	return pfn_to_page(pfn);
648ee498ed7SHugh Dickins }
649ee498ed7SHugh Dickins 
650ee498ed7SHugh Dickins /*
6511da177e4SLinus Torvalds  * copy one vm_area from one task to the other. Assumes the page tables
6521da177e4SLinus Torvalds  * already present in the new task to be cleared in the whole range
6531da177e4SLinus Torvalds  * covered by this vma.
6541da177e4SLinus Torvalds  */
6551da177e4SLinus Torvalds 
656570a335bSHugh Dickins static inline unsigned long
6571da177e4SLinus Torvalds copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
658b5810039SNick Piggin 		pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma,
6598c103762SHugh Dickins 		unsigned long addr, int *rss)
6601da177e4SLinus Torvalds {
661b5810039SNick Piggin 	unsigned long vm_flags = vma->vm_flags;
6621da177e4SLinus Torvalds 	pte_t pte = *src_pte;
6631da177e4SLinus Torvalds 	struct page *page;
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds 	/* pte contains position in swap or file, so copy. */
6661da177e4SLinus Torvalds 	if (unlikely(!pte_present(pte))) {
6671da177e4SLinus Torvalds 		if (!pte_file(pte)) {
6680697212aSChristoph Lameter 			swp_entry_t entry = pte_to_swp_entry(pte);
6690697212aSChristoph Lameter 
670570a335bSHugh Dickins 			if (swap_duplicate(entry) < 0)
671570a335bSHugh Dickins 				return entry.val;
672570a335bSHugh Dickins 
6731da177e4SLinus Torvalds 			/* make sure dst_mm is on swapoff's mmlist. */
6741da177e4SLinus Torvalds 			if (unlikely(list_empty(&dst_mm->mmlist))) {
6751da177e4SLinus Torvalds 				spin_lock(&mmlist_lock);
676f412ac08SHugh Dickins 				if (list_empty(&dst_mm->mmlist))
677f412ac08SHugh Dickins 					list_add(&dst_mm->mmlist,
678f412ac08SHugh Dickins 						 &src_mm->mmlist);
6791da177e4SLinus Torvalds 				spin_unlock(&mmlist_lock);
6801da177e4SLinus Torvalds 			}
681b084d435SKAMEZAWA Hiroyuki 			if (likely(!non_swap_entry(entry)))
682b084d435SKAMEZAWA Hiroyuki 				rss[MM_SWAPENTS]++;
683b084d435SKAMEZAWA Hiroyuki 			else if (is_write_migration_entry(entry) &&
6840697212aSChristoph Lameter 					is_cow_mapping(vm_flags)) {
6850697212aSChristoph Lameter 				/*
6860697212aSChristoph Lameter 				 * COW mappings require pages in both parent
6870697212aSChristoph Lameter 				 * and child to be set to read.
6880697212aSChristoph Lameter 				 */
6890697212aSChristoph Lameter 				make_migration_entry_read(&entry);
6900697212aSChristoph Lameter 				pte = swp_entry_to_pte(entry);
6910697212aSChristoph Lameter 				set_pte_at(src_mm, addr, src_pte, pte);
6920697212aSChristoph Lameter 			}
6931da177e4SLinus Torvalds 		}
694ae859762SHugh Dickins 		goto out_set_pte;
6951da177e4SLinus Torvalds 	}
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 	/*
6981da177e4SLinus Torvalds 	 * If it's a COW mapping, write protect it both
6991da177e4SLinus Torvalds 	 * in the parent and the child
7001da177e4SLinus Torvalds 	 */
70167121172SLinus Torvalds 	if (is_cow_mapping(vm_flags)) {
7021da177e4SLinus Torvalds 		ptep_set_wrprotect(src_mm, addr, src_pte);
7033dc90795SZachary Amsden 		pte = pte_wrprotect(pte);
7041da177e4SLinus Torvalds 	}
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 	/*
7071da177e4SLinus Torvalds 	 * If it's a shared mapping, mark it clean in
7081da177e4SLinus Torvalds 	 * the child
7091da177e4SLinus Torvalds 	 */
7101da177e4SLinus Torvalds 	if (vm_flags & VM_SHARED)
7111da177e4SLinus Torvalds 		pte = pte_mkclean(pte);
7121da177e4SLinus Torvalds 	pte = pte_mkold(pte);
7136aab341eSLinus Torvalds 
7146aab341eSLinus Torvalds 	page = vm_normal_page(vma, addr, pte);
7156aab341eSLinus Torvalds 	if (page) {
7161da177e4SLinus Torvalds 		get_page(page);
71721333b2bSHugh Dickins 		page_dup_rmap(page);
718d559db08SKAMEZAWA Hiroyuki 		if (PageAnon(page))
719d559db08SKAMEZAWA Hiroyuki 			rss[MM_ANONPAGES]++;
720d559db08SKAMEZAWA Hiroyuki 		else
721d559db08SKAMEZAWA Hiroyuki 			rss[MM_FILEPAGES]++;
7226aab341eSLinus Torvalds 	}
723ae859762SHugh Dickins 
724ae859762SHugh Dickins out_set_pte:
725ae859762SHugh Dickins 	set_pte_at(dst_mm, addr, dst_pte, pte);
726570a335bSHugh Dickins 	return 0;
7271da177e4SLinus Torvalds }
7281da177e4SLinus Torvalds 
729*71e3aac0SAndrea Arcangeli int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
7301da177e4SLinus Torvalds 		   pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
7311da177e4SLinus Torvalds 		   unsigned long addr, unsigned long end)
7321da177e4SLinus Torvalds {
733c36987e2SDaisuke Nishimura 	pte_t *orig_src_pte, *orig_dst_pte;
7341da177e4SLinus Torvalds 	pte_t *src_pte, *dst_pte;
735c74df32cSHugh Dickins 	spinlock_t *src_ptl, *dst_ptl;
736e040f218SHugh Dickins 	int progress = 0;
737d559db08SKAMEZAWA Hiroyuki 	int rss[NR_MM_COUNTERS];
738570a335bSHugh Dickins 	swp_entry_t entry = (swp_entry_t){0};
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds again:
741d559db08SKAMEZAWA Hiroyuki 	init_rss_vec(rss);
742d559db08SKAMEZAWA Hiroyuki 
743c74df32cSHugh Dickins 	dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
7441da177e4SLinus Torvalds 	if (!dst_pte)
7451da177e4SLinus Torvalds 		return -ENOMEM;
746ece0e2b6SPeter Zijlstra 	src_pte = pte_offset_map(src_pmd, addr);
7474c21e2f2SHugh Dickins 	src_ptl = pte_lockptr(src_mm, src_pmd);
748f20dc5f7SIngo Molnar 	spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
749c36987e2SDaisuke Nishimura 	orig_src_pte = src_pte;
750c36987e2SDaisuke Nishimura 	orig_dst_pte = dst_pte;
7516606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds 	do {
7541da177e4SLinus Torvalds 		/*
7551da177e4SLinus Torvalds 		 * We are holding two locks at this point - either of them
7561da177e4SLinus Torvalds 		 * could generate latencies in another task on another CPU.
7571da177e4SLinus Torvalds 		 */
758e040f218SHugh Dickins 		if (progress >= 32) {
759e040f218SHugh Dickins 			progress = 0;
760e040f218SHugh Dickins 			if (need_resched() ||
76195c354feSNick Piggin 			    spin_needbreak(src_ptl) || spin_needbreak(dst_ptl))
7621da177e4SLinus Torvalds 				break;
763e040f218SHugh Dickins 		}
7641da177e4SLinus Torvalds 		if (pte_none(*src_pte)) {
7651da177e4SLinus Torvalds 			progress++;
7661da177e4SLinus Torvalds 			continue;
7671da177e4SLinus Torvalds 		}
768570a335bSHugh Dickins 		entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte,
769570a335bSHugh Dickins 							vma, addr, rss);
770570a335bSHugh Dickins 		if (entry.val)
771570a335bSHugh Dickins 			break;
7721da177e4SLinus Torvalds 		progress += 8;
7731da177e4SLinus Torvalds 	} while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
7741da177e4SLinus Torvalds 
7756606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
776c74df32cSHugh Dickins 	spin_unlock(src_ptl);
777ece0e2b6SPeter Zijlstra 	pte_unmap(orig_src_pte);
778d559db08SKAMEZAWA Hiroyuki 	add_mm_rss_vec(dst_mm, rss);
779c36987e2SDaisuke Nishimura 	pte_unmap_unlock(orig_dst_pte, dst_ptl);
780c74df32cSHugh Dickins 	cond_resched();
781570a335bSHugh Dickins 
782570a335bSHugh Dickins 	if (entry.val) {
783570a335bSHugh Dickins 		if (add_swap_count_continuation(entry, GFP_KERNEL) < 0)
784570a335bSHugh Dickins 			return -ENOMEM;
785570a335bSHugh Dickins 		progress = 0;
786570a335bSHugh Dickins 	}
7871da177e4SLinus Torvalds 	if (addr != end)
7881da177e4SLinus Torvalds 		goto again;
7891da177e4SLinus Torvalds 	return 0;
7901da177e4SLinus Torvalds }
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
7931da177e4SLinus Torvalds 		pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma,
7941da177e4SLinus Torvalds 		unsigned long addr, unsigned long end)
7951da177e4SLinus Torvalds {
7961da177e4SLinus Torvalds 	pmd_t *src_pmd, *dst_pmd;
7971da177e4SLinus Torvalds 	unsigned long next;
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 	dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
8001da177e4SLinus Torvalds 	if (!dst_pmd)
8011da177e4SLinus Torvalds 		return -ENOMEM;
8021da177e4SLinus Torvalds 	src_pmd = pmd_offset(src_pud, addr);
8031da177e4SLinus Torvalds 	do {
8041da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
805*71e3aac0SAndrea Arcangeli 		if (pmd_trans_huge(*src_pmd)) {
806*71e3aac0SAndrea Arcangeli 			int err;
807*71e3aac0SAndrea Arcangeli 			err = copy_huge_pmd(dst_mm, src_mm,
808*71e3aac0SAndrea Arcangeli 					    dst_pmd, src_pmd, addr, vma);
809*71e3aac0SAndrea Arcangeli 			if (err == -ENOMEM)
810*71e3aac0SAndrea Arcangeli 				return -ENOMEM;
811*71e3aac0SAndrea Arcangeli 			if (!err)
812*71e3aac0SAndrea Arcangeli 				continue;
813*71e3aac0SAndrea Arcangeli 			/* fall through */
814*71e3aac0SAndrea Arcangeli 		}
8151da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(src_pmd))
8161da177e4SLinus Torvalds 			continue;
8171da177e4SLinus Torvalds 		if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd,
8181da177e4SLinus Torvalds 						vma, addr, next))
8191da177e4SLinus Torvalds 			return -ENOMEM;
8201da177e4SLinus Torvalds 	} while (dst_pmd++, src_pmd++, addr = next, addr != end);
8211da177e4SLinus Torvalds 	return 0;
8221da177e4SLinus Torvalds }
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
8251da177e4SLinus Torvalds 		pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma,
8261da177e4SLinus Torvalds 		unsigned long addr, unsigned long end)
8271da177e4SLinus Torvalds {
8281da177e4SLinus Torvalds 	pud_t *src_pud, *dst_pud;
8291da177e4SLinus Torvalds 	unsigned long next;
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds 	dst_pud = pud_alloc(dst_mm, dst_pgd, addr);
8321da177e4SLinus Torvalds 	if (!dst_pud)
8331da177e4SLinus Torvalds 		return -ENOMEM;
8341da177e4SLinus Torvalds 	src_pud = pud_offset(src_pgd, addr);
8351da177e4SLinus Torvalds 	do {
8361da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
8371da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(src_pud))
8381da177e4SLinus Torvalds 			continue;
8391da177e4SLinus Torvalds 		if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud,
8401da177e4SLinus Torvalds 						vma, addr, next))
8411da177e4SLinus Torvalds 			return -ENOMEM;
8421da177e4SLinus Torvalds 	} while (dst_pud++, src_pud++, addr = next, addr != end);
8431da177e4SLinus Torvalds 	return 0;
8441da177e4SLinus Torvalds }
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
8471da177e4SLinus Torvalds 		struct vm_area_struct *vma)
8481da177e4SLinus Torvalds {
8491da177e4SLinus Torvalds 	pgd_t *src_pgd, *dst_pgd;
8501da177e4SLinus Torvalds 	unsigned long next;
8511da177e4SLinus Torvalds 	unsigned long addr = vma->vm_start;
8521da177e4SLinus Torvalds 	unsigned long end = vma->vm_end;
853cddb8a5cSAndrea Arcangeli 	int ret;
8541da177e4SLinus Torvalds 
855d992895bSNick Piggin 	/*
856d992895bSNick Piggin 	 * Don't copy ptes where a page fault will fill them correctly.
857d992895bSNick Piggin 	 * Fork becomes much lighter when there are big shared or private
858d992895bSNick Piggin 	 * readonly mappings. The tradeoff is that copy_page_range is more
859d992895bSNick Piggin 	 * efficient than faulting.
860d992895bSNick Piggin 	 */
8614d7672b4SLinus Torvalds 	if (!(vma->vm_flags & (VM_HUGETLB|VM_NONLINEAR|VM_PFNMAP|VM_INSERTPAGE))) {
862d992895bSNick Piggin 		if (!vma->anon_vma)
863d992895bSNick Piggin 			return 0;
864d992895bSNick Piggin 	}
865d992895bSNick Piggin 
8661da177e4SLinus Torvalds 	if (is_vm_hugetlb_page(vma))
8671da177e4SLinus Torvalds 		return copy_hugetlb_page_range(dst_mm, src_mm, vma);
8681da177e4SLinus Torvalds 
86934801ba9Svenkatesh.pallipadi@intel.com 	if (unlikely(is_pfn_mapping(vma))) {
8702ab64037Svenkatesh.pallipadi@intel.com 		/*
8712ab64037Svenkatesh.pallipadi@intel.com 		 * We do not free on error cases below as remove_vma
8722ab64037Svenkatesh.pallipadi@intel.com 		 * gets called on error from higher level routine
8732ab64037Svenkatesh.pallipadi@intel.com 		 */
8742ab64037Svenkatesh.pallipadi@intel.com 		ret = track_pfn_vma_copy(vma);
8752ab64037Svenkatesh.pallipadi@intel.com 		if (ret)
8762ab64037Svenkatesh.pallipadi@intel.com 			return ret;
8772ab64037Svenkatesh.pallipadi@intel.com 	}
8782ab64037Svenkatesh.pallipadi@intel.com 
879cddb8a5cSAndrea Arcangeli 	/*
880cddb8a5cSAndrea Arcangeli 	 * We need to invalidate the secondary MMU mappings only when
881cddb8a5cSAndrea Arcangeli 	 * there could be a permission downgrade on the ptes of the
882cddb8a5cSAndrea Arcangeli 	 * parent mm. And a permission downgrade will only happen if
883cddb8a5cSAndrea Arcangeli 	 * is_cow_mapping() returns true.
884cddb8a5cSAndrea Arcangeli 	 */
885cddb8a5cSAndrea Arcangeli 	if (is_cow_mapping(vma->vm_flags))
886cddb8a5cSAndrea Arcangeli 		mmu_notifier_invalidate_range_start(src_mm, addr, end);
887cddb8a5cSAndrea Arcangeli 
888cddb8a5cSAndrea Arcangeli 	ret = 0;
8891da177e4SLinus Torvalds 	dst_pgd = pgd_offset(dst_mm, addr);
8901da177e4SLinus Torvalds 	src_pgd = pgd_offset(src_mm, addr);
8911da177e4SLinus Torvalds 	do {
8921da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
8931da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(src_pgd))
8941da177e4SLinus Torvalds 			continue;
895cddb8a5cSAndrea Arcangeli 		if (unlikely(copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd,
896cddb8a5cSAndrea Arcangeli 					    vma, addr, next))) {
897cddb8a5cSAndrea Arcangeli 			ret = -ENOMEM;
898cddb8a5cSAndrea Arcangeli 			break;
899cddb8a5cSAndrea Arcangeli 		}
9001da177e4SLinus Torvalds 	} while (dst_pgd++, src_pgd++, addr = next, addr != end);
901cddb8a5cSAndrea Arcangeli 
902cddb8a5cSAndrea Arcangeli 	if (is_cow_mapping(vma->vm_flags))
903cddb8a5cSAndrea Arcangeli 		mmu_notifier_invalidate_range_end(src_mm,
904cddb8a5cSAndrea Arcangeli 						  vma->vm_start, end);
905cddb8a5cSAndrea Arcangeli 	return ret;
9061da177e4SLinus Torvalds }
9071da177e4SLinus Torvalds 
90851c6f666SRobin Holt static unsigned long zap_pte_range(struct mmu_gather *tlb,
909b5810039SNick Piggin 				struct vm_area_struct *vma, pmd_t *pmd,
9101da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
91151c6f666SRobin Holt 				long *zap_work, struct zap_details *details)
9121da177e4SLinus Torvalds {
913b5810039SNick Piggin 	struct mm_struct *mm = tlb->mm;
9141da177e4SLinus Torvalds 	pte_t *pte;
915508034a3SHugh Dickins 	spinlock_t *ptl;
916d559db08SKAMEZAWA Hiroyuki 	int rss[NR_MM_COUNTERS];
917d559db08SKAMEZAWA Hiroyuki 
918d559db08SKAMEZAWA Hiroyuki 	init_rss_vec(rss);
9191da177e4SLinus Torvalds 
920508034a3SHugh Dickins 	pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
9216606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
9221da177e4SLinus Torvalds 	do {
9231da177e4SLinus Torvalds 		pte_t ptent = *pte;
92451c6f666SRobin Holt 		if (pte_none(ptent)) {
92551c6f666SRobin Holt 			(*zap_work)--;
9261da177e4SLinus Torvalds 			continue;
92751c6f666SRobin Holt 		}
92851c6f666SRobin Holt 
92951c6f666SRobin Holt 		(*zap_work) -= PAGE_SIZE;
93051c6f666SRobin Holt 
9316f5e6b9eSHugh Dickins 		if (pte_present(ptent)) {
9326f5e6b9eSHugh Dickins 			struct page *page;
9336f5e6b9eSHugh Dickins 
9346aab341eSLinus Torvalds 			page = vm_normal_page(vma, addr, ptent);
9351da177e4SLinus Torvalds 			if (unlikely(details) && page) {
9361da177e4SLinus Torvalds 				/*
9371da177e4SLinus Torvalds 				 * unmap_shared_mapping_pages() wants to
9381da177e4SLinus Torvalds 				 * invalidate cache without truncating:
9391da177e4SLinus Torvalds 				 * unmap shared but keep private pages.
9401da177e4SLinus Torvalds 				 */
9411da177e4SLinus Torvalds 				if (details->check_mapping &&
9421da177e4SLinus Torvalds 				    details->check_mapping != page->mapping)
9431da177e4SLinus Torvalds 					continue;
9441da177e4SLinus Torvalds 				/*
9451da177e4SLinus Torvalds 				 * Each page->index must be checked when
9461da177e4SLinus Torvalds 				 * invalidating or truncating nonlinear.
9471da177e4SLinus Torvalds 				 */
9481da177e4SLinus Torvalds 				if (details->nonlinear_vma &&
9491da177e4SLinus Torvalds 				    (page->index < details->first_index ||
9501da177e4SLinus Torvalds 				     page->index > details->last_index))
9511da177e4SLinus Torvalds 					continue;
9521da177e4SLinus Torvalds 			}
953b5810039SNick Piggin 			ptent = ptep_get_and_clear_full(mm, addr, pte,
954a600388dSZachary Amsden 							tlb->fullmm);
9551da177e4SLinus Torvalds 			tlb_remove_tlb_entry(tlb, pte, addr);
9561da177e4SLinus Torvalds 			if (unlikely(!page))
9571da177e4SLinus Torvalds 				continue;
9581da177e4SLinus Torvalds 			if (unlikely(details) && details->nonlinear_vma
9591da177e4SLinus Torvalds 			    && linear_page_index(details->nonlinear_vma,
9601da177e4SLinus Torvalds 						addr) != page->index)
961b5810039SNick Piggin 				set_pte_at(mm, addr, pte,
9621da177e4SLinus Torvalds 					   pgoff_to_pte(page->index));
9631da177e4SLinus Torvalds 			if (PageAnon(page))
964d559db08SKAMEZAWA Hiroyuki 				rss[MM_ANONPAGES]--;
9656237bcd9SHugh Dickins 			else {
9666237bcd9SHugh Dickins 				if (pte_dirty(ptent))
9676237bcd9SHugh Dickins 					set_page_dirty(page);
9684917e5d0SJohannes Weiner 				if (pte_young(ptent) &&
9694917e5d0SJohannes Weiner 				    likely(!VM_SequentialReadHint(vma)))
970bf3f3bc5SNick Piggin 					mark_page_accessed(page);
971d559db08SKAMEZAWA Hiroyuki 				rss[MM_FILEPAGES]--;
9726237bcd9SHugh Dickins 			}
973edc315fdSHugh Dickins 			page_remove_rmap(page);
9743dc14741SHugh Dickins 			if (unlikely(page_mapcount(page) < 0))
9753dc14741SHugh Dickins 				print_bad_pte(vma, addr, ptent, page);
9761da177e4SLinus Torvalds 			tlb_remove_page(tlb, page);
9771da177e4SLinus Torvalds 			continue;
9781da177e4SLinus Torvalds 		}
9791da177e4SLinus Torvalds 		/*
9801da177e4SLinus Torvalds 		 * If details->check_mapping, we leave swap entries;
9811da177e4SLinus Torvalds 		 * if details->nonlinear_vma, we leave file entries.
9821da177e4SLinus Torvalds 		 */
9831da177e4SLinus Torvalds 		if (unlikely(details))
9841da177e4SLinus Torvalds 			continue;
9852509ef26SHugh Dickins 		if (pte_file(ptent)) {
9862509ef26SHugh Dickins 			if (unlikely(!(vma->vm_flags & VM_NONLINEAR)))
9872509ef26SHugh Dickins 				print_bad_pte(vma, addr, ptent, NULL);
988b084d435SKAMEZAWA Hiroyuki 		} else {
989b084d435SKAMEZAWA Hiroyuki 			swp_entry_t entry = pte_to_swp_entry(ptent);
990b084d435SKAMEZAWA Hiroyuki 
991b084d435SKAMEZAWA Hiroyuki 			if (!non_swap_entry(entry))
992b084d435SKAMEZAWA Hiroyuki 				rss[MM_SWAPENTS]--;
993b084d435SKAMEZAWA Hiroyuki 			if (unlikely(!free_swap_and_cache(entry)))
9942509ef26SHugh Dickins 				print_bad_pte(vma, addr, ptent, NULL);
995b084d435SKAMEZAWA Hiroyuki 		}
9969888a1caSZachary Amsden 		pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
99751c6f666SRobin Holt 	} while (pte++, addr += PAGE_SIZE, (addr != end && *zap_work > 0));
998ae859762SHugh Dickins 
999d559db08SKAMEZAWA Hiroyuki 	add_mm_rss_vec(mm, rss);
10006606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
1001508034a3SHugh Dickins 	pte_unmap_unlock(pte - 1, ptl);
100251c6f666SRobin Holt 
100351c6f666SRobin Holt 	return addr;
10041da177e4SLinus Torvalds }
10051da177e4SLinus Torvalds 
100651c6f666SRobin Holt static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
1007b5810039SNick Piggin 				struct vm_area_struct *vma, pud_t *pud,
10081da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
100951c6f666SRobin Holt 				long *zap_work, struct zap_details *details)
10101da177e4SLinus Torvalds {
10111da177e4SLinus Torvalds 	pmd_t *pmd;
10121da177e4SLinus Torvalds 	unsigned long next;
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
10151da177e4SLinus Torvalds 	do {
10161da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
1017*71e3aac0SAndrea Arcangeli 		if (pmd_trans_huge(*pmd)) {
1018*71e3aac0SAndrea Arcangeli 			if (next-addr != HPAGE_PMD_SIZE)
1019*71e3aac0SAndrea Arcangeli 				split_huge_page_pmd(vma->vm_mm, pmd);
1020*71e3aac0SAndrea Arcangeli 			else if (zap_huge_pmd(tlb, vma, pmd)) {
1021*71e3aac0SAndrea Arcangeli 				(*zap_work)--;
1022*71e3aac0SAndrea Arcangeli 				continue;
1023*71e3aac0SAndrea Arcangeli 			}
1024*71e3aac0SAndrea Arcangeli 			/* fall through */
1025*71e3aac0SAndrea Arcangeli 		}
102651c6f666SRobin Holt 		if (pmd_none_or_clear_bad(pmd)) {
102751c6f666SRobin Holt 			(*zap_work)--;
10281da177e4SLinus Torvalds 			continue;
102951c6f666SRobin Holt 		}
103051c6f666SRobin Holt 		next = zap_pte_range(tlb, vma, pmd, addr, next,
103151c6f666SRobin Holt 						zap_work, details);
103251c6f666SRobin Holt 	} while (pmd++, addr = next, (addr != end && *zap_work > 0));
103351c6f666SRobin Holt 
103451c6f666SRobin Holt 	return addr;
10351da177e4SLinus Torvalds }
10361da177e4SLinus Torvalds 
103751c6f666SRobin Holt static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
1038b5810039SNick Piggin 				struct vm_area_struct *vma, pgd_t *pgd,
10391da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
104051c6f666SRobin Holt 				long *zap_work, struct zap_details *details)
10411da177e4SLinus Torvalds {
10421da177e4SLinus Torvalds 	pud_t *pud;
10431da177e4SLinus Torvalds 	unsigned long next;
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
10461da177e4SLinus Torvalds 	do {
10471da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
104851c6f666SRobin Holt 		if (pud_none_or_clear_bad(pud)) {
104951c6f666SRobin Holt 			(*zap_work)--;
10501da177e4SLinus Torvalds 			continue;
105151c6f666SRobin Holt 		}
105251c6f666SRobin Holt 		next = zap_pmd_range(tlb, vma, pud, addr, next,
105351c6f666SRobin Holt 						zap_work, details);
105451c6f666SRobin Holt 	} while (pud++, addr = next, (addr != end && *zap_work > 0));
105551c6f666SRobin Holt 
105651c6f666SRobin Holt 	return addr;
10571da177e4SLinus Torvalds }
10581da177e4SLinus Torvalds 
105951c6f666SRobin Holt static unsigned long unmap_page_range(struct mmu_gather *tlb,
106051c6f666SRobin Holt 				struct vm_area_struct *vma,
10611da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
106251c6f666SRobin Holt 				long *zap_work, struct zap_details *details)
10631da177e4SLinus Torvalds {
10641da177e4SLinus Torvalds 	pgd_t *pgd;
10651da177e4SLinus Torvalds 	unsigned long next;
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds 	if (details && !details->check_mapping && !details->nonlinear_vma)
10681da177e4SLinus Torvalds 		details = NULL;
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds 	BUG_ON(addr >= end);
1071569b846dSKAMEZAWA Hiroyuki 	mem_cgroup_uncharge_start();
10721da177e4SLinus Torvalds 	tlb_start_vma(tlb, vma);
10731da177e4SLinus Torvalds 	pgd = pgd_offset(vma->vm_mm, addr);
10741da177e4SLinus Torvalds 	do {
10751da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
107651c6f666SRobin Holt 		if (pgd_none_or_clear_bad(pgd)) {
107751c6f666SRobin Holt 			(*zap_work)--;
10781da177e4SLinus Torvalds 			continue;
107951c6f666SRobin Holt 		}
108051c6f666SRobin Holt 		next = zap_pud_range(tlb, vma, pgd, addr, next,
108151c6f666SRobin Holt 						zap_work, details);
108251c6f666SRobin Holt 	} while (pgd++, addr = next, (addr != end && *zap_work > 0));
10831da177e4SLinus Torvalds 	tlb_end_vma(tlb, vma);
1084569b846dSKAMEZAWA Hiroyuki 	mem_cgroup_uncharge_end();
108551c6f666SRobin Holt 
108651c6f666SRobin Holt 	return addr;
10871da177e4SLinus Torvalds }
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds #ifdef CONFIG_PREEMPT
10901da177e4SLinus Torvalds # define ZAP_BLOCK_SIZE	(8 * PAGE_SIZE)
10911da177e4SLinus Torvalds #else
10921da177e4SLinus Torvalds /* No preempt: go for improved straight-line efficiency */
10931da177e4SLinus Torvalds # define ZAP_BLOCK_SIZE	(1024 * PAGE_SIZE)
10941da177e4SLinus Torvalds #endif
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds /**
10971da177e4SLinus Torvalds  * unmap_vmas - unmap a range of memory covered by a list of vma's
10981da177e4SLinus Torvalds  * @tlbp: address of the caller's struct mmu_gather
10991da177e4SLinus Torvalds  * @vma: the starting vma
11001da177e4SLinus Torvalds  * @start_addr: virtual address at which to start unmapping
11011da177e4SLinus Torvalds  * @end_addr: virtual address at which to end unmapping
11021da177e4SLinus Torvalds  * @nr_accounted: Place number of unmapped pages in vm-accountable vma's here
11031da177e4SLinus Torvalds  * @details: details of nonlinear truncation or shared cache invalidation
11041da177e4SLinus Torvalds  *
1105ee39b37bSHugh Dickins  * Returns the end address of the unmapping (restart addr if interrupted).
11061da177e4SLinus Torvalds  *
1107508034a3SHugh Dickins  * Unmap all pages in the vma list.
11081da177e4SLinus Torvalds  *
1109508034a3SHugh Dickins  * We aim to not hold locks for too long (for scheduling latency reasons).
1110508034a3SHugh Dickins  * So zap pages in ZAP_BLOCK_SIZE bytecounts.  This means we need to
11111da177e4SLinus Torvalds  * return the ending mmu_gather to the caller.
11121da177e4SLinus Torvalds  *
11131da177e4SLinus Torvalds  * Only addresses between `start' and `end' will be unmapped.
11141da177e4SLinus Torvalds  *
11151da177e4SLinus Torvalds  * The VMA list must be sorted in ascending virtual address order.
11161da177e4SLinus Torvalds  *
11171da177e4SLinus Torvalds  * unmap_vmas() assumes that the caller will flush the whole unmapped address
11181da177e4SLinus Torvalds  * range after unmap_vmas() returns.  So the only responsibility here is to
11191da177e4SLinus Torvalds  * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
11201da177e4SLinus Torvalds  * drops the lock and schedules.
11211da177e4SLinus Torvalds  */
1122508034a3SHugh Dickins unsigned long unmap_vmas(struct mmu_gather **tlbp,
11231da177e4SLinus Torvalds 		struct vm_area_struct *vma, unsigned long start_addr,
11241da177e4SLinus Torvalds 		unsigned long end_addr, unsigned long *nr_accounted,
11251da177e4SLinus Torvalds 		struct zap_details *details)
11261da177e4SLinus Torvalds {
112751c6f666SRobin Holt 	long zap_work = ZAP_BLOCK_SIZE;
11281da177e4SLinus Torvalds 	unsigned long tlb_start = 0;	/* For tlb_finish_mmu */
11291da177e4SLinus Torvalds 	int tlb_start_valid = 0;
1130ee39b37bSHugh Dickins 	unsigned long start = start_addr;
11311da177e4SLinus Torvalds 	spinlock_t *i_mmap_lock = details? details->i_mmap_lock: NULL;
11324d6ddfa9SHugh Dickins 	int fullmm = (*tlbp)->fullmm;
1133cddb8a5cSAndrea Arcangeli 	struct mm_struct *mm = vma->vm_mm;
11341da177e4SLinus Torvalds 
1135cddb8a5cSAndrea Arcangeli 	mmu_notifier_invalidate_range_start(mm, start_addr, end_addr);
11361da177e4SLinus Torvalds 	for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) {
11371da177e4SLinus Torvalds 		unsigned long end;
11381da177e4SLinus Torvalds 
11391da177e4SLinus Torvalds 		start = max(vma->vm_start, start_addr);
11401da177e4SLinus Torvalds 		if (start >= vma->vm_end)
11411da177e4SLinus Torvalds 			continue;
11421da177e4SLinus Torvalds 		end = min(vma->vm_end, end_addr);
11431da177e4SLinus Torvalds 		if (end <= vma->vm_start)
11441da177e4SLinus Torvalds 			continue;
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 		if (vma->vm_flags & VM_ACCOUNT)
11471da177e4SLinus Torvalds 			*nr_accounted += (end - start) >> PAGE_SHIFT;
11481da177e4SLinus Torvalds 
114934801ba9Svenkatesh.pallipadi@intel.com 		if (unlikely(is_pfn_mapping(vma)))
11502ab64037Svenkatesh.pallipadi@intel.com 			untrack_pfn_vma(vma, 0, 0);
11512ab64037Svenkatesh.pallipadi@intel.com 
11521da177e4SLinus Torvalds 		while (start != end) {
11531da177e4SLinus Torvalds 			if (!tlb_start_valid) {
11541da177e4SLinus Torvalds 				tlb_start = start;
11551da177e4SLinus Torvalds 				tlb_start_valid = 1;
11561da177e4SLinus Torvalds 			}
11571da177e4SLinus Torvalds 
115851c6f666SRobin Holt 			if (unlikely(is_vm_hugetlb_page(vma))) {
1159a137e1ccSAndi Kleen 				/*
1160a137e1ccSAndi Kleen 				 * It is undesirable to test vma->vm_file as it
1161a137e1ccSAndi Kleen 				 * should be non-null for valid hugetlb area.
1162a137e1ccSAndi Kleen 				 * However, vm_file will be NULL in the error
1163a137e1ccSAndi Kleen 				 * cleanup path of do_mmap_pgoff. When
1164a137e1ccSAndi Kleen 				 * hugetlbfs ->mmap method fails,
1165a137e1ccSAndi Kleen 				 * do_mmap_pgoff() nullifies vma->vm_file
1166a137e1ccSAndi Kleen 				 * before calling this function to clean up.
1167a137e1ccSAndi Kleen 				 * Since no pte has actually been setup, it is
1168a137e1ccSAndi Kleen 				 * safe to do nothing in this case.
1169a137e1ccSAndi Kleen 				 */
1170a137e1ccSAndi Kleen 				if (vma->vm_file) {
117104f2cbe3SMel Gorman 					unmap_hugepage_range(vma, start, end, NULL);
117251c6f666SRobin Holt 					zap_work -= (end - start) /
1173a5516438SAndi Kleen 					pages_per_huge_page(hstate_vma(vma));
1174a137e1ccSAndi Kleen 				}
1175a137e1ccSAndi Kleen 
117651c6f666SRobin Holt 				start = end;
117751c6f666SRobin Holt 			} else
117851c6f666SRobin Holt 				start = unmap_page_range(*tlbp, vma,
117951c6f666SRobin Holt 						start, end, &zap_work, details);
11801da177e4SLinus Torvalds 
118151c6f666SRobin Holt 			if (zap_work > 0) {
118251c6f666SRobin Holt 				BUG_ON(start != end);
118351c6f666SRobin Holt 				break;
118451c6f666SRobin Holt 			}
11851da177e4SLinus Torvalds 
11861da177e4SLinus Torvalds 			tlb_finish_mmu(*tlbp, tlb_start, start);
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds 			if (need_resched() ||
118995c354feSNick Piggin 				(i_mmap_lock && spin_needbreak(i_mmap_lock))) {
11901da177e4SLinus Torvalds 				if (i_mmap_lock) {
1191508034a3SHugh Dickins 					*tlbp = NULL;
11921da177e4SLinus Torvalds 					goto out;
11931da177e4SLinus Torvalds 				}
11941da177e4SLinus Torvalds 				cond_resched();
11951da177e4SLinus Torvalds 			}
11961da177e4SLinus Torvalds 
1197508034a3SHugh Dickins 			*tlbp = tlb_gather_mmu(vma->vm_mm, fullmm);
11981da177e4SLinus Torvalds 			tlb_start_valid = 0;
119951c6f666SRobin Holt 			zap_work = ZAP_BLOCK_SIZE;
12001da177e4SLinus Torvalds 		}
12011da177e4SLinus Torvalds 	}
12021da177e4SLinus Torvalds out:
1203cddb8a5cSAndrea Arcangeli 	mmu_notifier_invalidate_range_end(mm, start_addr, end_addr);
1204ee39b37bSHugh Dickins 	return start;	/* which is now the end (or restart) address */
12051da177e4SLinus Torvalds }
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds /**
12081da177e4SLinus Torvalds  * zap_page_range - remove user pages in a given range
12091da177e4SLinus Torvalds  * @vma: vm_area_struct holding the applicable pages
12101da177e4SLinus Torvalds  * @address: starting address of pages to zap
12111da177e4SLinus Torvalds  * @size: number of bytes to zap
12121da177e4SLinus Torvalds  * @details: details of nonlinear truncation or shared cache invalidation
12131da177e4SLinus Torvalds  */
1214ee39b37bSHugh Dickins unsigned long zap_page_range(struct vm_area_struct *vma, unsigned long address,
12151da177e4SLinus Torvalds 		unsigned long size, struct zap_details *details)
12161da177e4SLinus Torvalds {
12171da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
12181da177e4SLinus Torvalds 	struct mmu_gather *tlb;
12191da177e4SLinus Torvalds 	unsigned long end = address + size;
12201da177e4SLinus Torvalds 	unsigned long nr_accounted = 0;
12211da177e4SLinus Torvalds 
12221da177e4SLinus Torvalds 	lru_add_drain();
12231da177e4SLinus Torvalds 	tlb = tlb_gather_mmu(mm, 0);
1224365e9c87SHugh Dickins 	update_hiwater_rss(mm);
1225508034a3SHugh Dickins 	end = unmap_vmas(&tlb, vma, address, end, &nr_accounted, details);
1226508034a3SHugh Dickins 	if (tlb)
12278f4f8c16SHugh Dickins 		tlb_finish_mmu(tlb, address, end);
1228ee39b37bSHugh Dickins 	return end;
12291da177e4SLinus Torvalds }
12301da177e4SLinus Torvalds 
1231c627f9ccSJack Steiner /**
1232c627f9ccSJack Steiner  * zap_vma_ptes - remove ptes mapping the vma
1233c627f9ccSJack Steiner  * @vma: vm_area_struct holding ptes to be zapped
1234c627f9ccSJack Steiner  * @address: starting address of pages to zap
1235c627f9ccSJack Steiner  * @size: number of bytes to zap
1236c627f9ccSJack Steiner  *
1237c627f9ccSJack Steiner  * This function only unmaps ptes assigned to VM_PFNMAP vmas.
1238c627f9ccSJack Steiner  *
1239c627f9ccSJack Steiner  * The entire address range must be fully contained within the vma.
1240c627f9ccSJack Steiner  *
1241c627f9ccSJack Steiner  * Returns 0 if successful.
1242c627f9ccSJack Steiner  */
1243c627f9ccSJack Steiner int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
1244c627f9ccSJack Steiner 		unsigned long size)
1245c627f9ccSJack Steiner {
1246c627f9ccSJack Steiner 	if (address < vma->vm_start || address + size > vma->vm_end ||
1247c627f9ccSJack Steiner 	    		!(vma->vm_flags & VM_PFNMAP))
1248c627f9ccSJack Steiner 		return -1;
1249c627f9ccSJack Steiner 	zap_page_range(vma, address, size, NULL);
1250c627f9ccSJack Steiner 	return 0;
1251c627f9ccSJack Steiner }
1252c627f9ccSJack Steiner EXPORT_SYMBOL_GPL(zap_vma_ptes);
1253c627f9ccSJack Steiner 
1254142762bdSJohannes Weiner /**
1255142762bdSJohannes Weiner  * follow_page - look up a page descriptor from a user-virtual address
1256142762bdSJohannes Weiner  * @vma: vm_area_struct mapping @address
1257142762bdSJohannes Weiner  * @address: virtual address to look up
1258142762bdSJohannes Weiner  * @flags: flags modifying lookup behaviour
1259142762bdSJohannes Weiner  *
1260142762bdSJohannes Weiner  * @flags can have FOLL_ flags set, defined in <linux/mm.h>
1261142762bdSJohannes Weiner  *
1262142762bdSJohannes Weiner  * Returns the mapped (struct page *), %NULL if no mapping exists, or
1263142762bdSJohannes Weiner  * an error pointer if there is a mapping to something not represented
1264142762bdSJohannes Weiner  * by a page descriptor (see also vm_normal_page()).
12651da177e4SLinus Torvalds  */
12666aab341eSLinus Torvalds struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
1267deceb6cdSHugh Dickins 			unsigned int flags)
12681da177e4SLinus Torvalds {
12691da177e4SLinus Torvalds 	pgd_t *pgd;
12701da177e4SLinus Torvalds 	pud_t *pud;
12711da177e4SLinus Torvalds 	pmd_t *pmd;
12721da177e4SLinus Torvalds 	pte_t *ptep, pte;
1273deceb6cdSHugh Dickins 	spinlock_t *ptl;
12741da177e4SLinus Torvalds 	struct page *page;
12756aab341eSLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
12761da177e4SLinus Torvalds 
1277deceb6cdSHugh Dickins 	page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
1278deceb6cdSHugh Dickins 	if (!IS_ERR(page)) {
1279deceb6cdSHugh Dickins 		BUG_ON(flags & FOLL_GET);
1280deceb6cdSHugh Dickins 		goto out;
1281deceb6cdSHugh Dickins 	}
12821da177e4SLinus Torvalds 
1283deceb6cdSHugh Dickins 	page = NULL;
12841da177e4SLinus Torvalds 	pgd = pgd_offset(mm, address);
12851da177e4SLinus Torvalds 	if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
1286deceb6cdSHugh Dickins 		goto no_page_table;
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds 	pud = pud_offset(pgd, address);
1289ceb86879SAndi Kleen 	if (pud_none(*pud))
1290ceb86879SAndi Kleen 		goto no_page_table;
1291ceb86879SAndi Kleen 	if (pud_huge(*pud)) {
1292ceb86879SAndi Kleen 		BUG_ON(flags & FOLL_GET);
1293ceb86879SAndi Kleen 		page = follow_huge_pud(mm, address, pud, flags & FOLL_WRITE);
1294ceb86879SAndi Kleen 		goto out;
1295ceb86879SAndi Kleen 	}
1296ceb86879SAndi Kleen 	if (unlikely(pud_bad(*pud)))
1297deceb6cdSHugh Dickins 		goto no_page_table;
12981da177e4SLinus Torvalds 
12991da177e4SLinus Torvalds 	pmd = pmd_offset(pud, address);
1300aeed5fceSHugh Dickins 	if (pmd_none(*pmd))
1301deceb6cdSHugh Dickins 		goto no_page_table;
1302*71e3aac0SAndrea Arcangeli 	if (pmd_huge(*pmd) && vma->vm_flags & VM_HUGETLB) {
1303deceb6cdSHugh Dickins 		BUG_ON(flags & FOLL_GET);
1304deceb6cdSHugh Dickins 		page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE);
1305deceb6cdSHugh Dickins 		goto out;
1306deceb6cdSHugh Dickins 	}
1307*71e3aac0SAndrea Arcangeli 	if (pmd_trans_huge(*pmd)) {
1308*71e3aac0SAndrea Arcangeli 		spin_lock(&mm->page_table_lock);
1309*71e3aac0SAndrea Arcangeli 		if (likely(pmd_trans_huge(*pmd))) {
1310*71e3aac0SAndrea Arcangeli 			if (unlikely(pmd_trans_splitting(*pmd))) {
1311*71e3aac0SAndrea Arcangeli 				spin_unlock(&mm->page_table_lock);
1312*71e3aac0SAndrea Arcangeli 				wait_split_huge_page(vma->anon_vma, pmd);
1313*71e3aac0SAndrea Arcangeli 			} else {
1314*71e3aac0SAndrea Arcangeli 				page = follow_trans_huge_pmd(mm, address,
1315*71e3aac0SAndrea Arcangeli 							     pmd, flags);
1316*71e3aac0SAndrea Arcangeli 				spin_unlock(&mm->page_table_lock);
1317*71e3aac0SAndrea Arcangeli 				goto out;
1318*71e3aac0SAndrea Arcangeli 			}
1319*71e3aac0SAndrea Arcangeli 		} else
1320*71e3aac0SAndrea Arcangeli 			spin_unlock(&mm->page_table_lock);
1321*71e3aac0SAndrea Arcangeli 		/* fall through */
1322*71e3aac0SAndrea Arcangeli 	}
1323aeed5fceSHugh Dickins 	if (unlikely(pmd_bad(*pmd)))
1324aeed5fceSHugh Dickins 		goto no_page_table;
1325aeed5fceSHugh Dickins 
1326deceb6cdSHugh Dickins 	ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
13271da177e4SLinus Torvalds 
13281da177e4SLinus Torvalds 	pte = *ptep;
1329deceb6cdSHugh Dickins 	if (!pte_present(pte))
133089f5b7daSLinus Torvalds 		goto no_page;
1331deceb6cdSHugh Dickins 	if ((flags & FOLL_WRITE) && !pte_write(pte))
1332deceb6cdSHugh Dickins 		goto unlock;
1333a13ea5b7SHugh Dickins 
13346aab341eSLinus Torvalds 	page = vm_normal_page(vma, address, pte);
1335a13ea5b7SHugh Dickins 	if (unlikely(!page)) {
1336a13ea5b7SHugh Dickins 		if ((flags & FOLL_DUMP) ||
133762eede62SHugh Dickins 		    !is_zero_pfn(pte_pfn(pte)))
133889f5b7daSLinus Torvalds 			goto bad_page;
1339a13ea5b7SHugh Dickins 		page = pte_page(pte);
1340a13ea5b7SHugh Dickins 	}
1341deceb6cdSHugh Dickins 
1342deceb6cdSHugh Dickins 	if (flags & FOLL_GET)
1343deceb6cdSHugh Dickins 		get_page(page);
1344deceb6cdSHugh Dickins 	if (flags & FOLL_TOUCH) {
1345deceb6cdSHugh Dickins 		if ((flags & FOLL_WRITE) &&
1346deceb6cdSHugh Dickins 		    !pte_dirty(pte) && !PageDirty(page))
1347f33ea7f4SNick Piggin 			set_page_dirty(page);
1348bd775c42SKOSAKI Motohiro 		/*
1349bd775c42SKOSAKI Motohiro 		 * pte_mkyoung() would be more correct here, but atomic care
1350bd775c42SKOSAKI Motohiro 		 * is needed to avoid losing the dirty bit: it is easier to use
1351bd775c42SKOSAKI Motohiro 		 * mark_page_accessed().
1352bd775c42SKOSAKI Motohiro 		 */
13531da177e4SLinus Torvalds 		mark_page_accessed(page);
13541da177e4SLinus Torvalds 	}
1355110d74a9SMichel Lespinasse 	if (flags & FOLL_MLOCK) {
1356110d74a9SMichel Lespinasse 		/*
1357110d74a9SMichel Lespinasse 		 * The preliminary mapping check is mainly to avoid the
1358110d74a9SMichel Lespinasse 		 * pointless overhead of lock_page on the ZERO_PAGE
1359110d74a9SMichel Lespinasse 		 * which might bounce very badly if there is contention.
1360110d74a9SMichel Lespinasse 		 *
1361110d74a9SMichel Lespinasse 		 * If the page is already locked, we don't need to
1362110d74a9SMichel Lespinasse 		 * handle it now - vmscan will handle it later if and
1363110d74a9SMichel Lespinasse 		 * when it attempts to reclaim the page.
1364110d74a9SMichel Lespinasse 		 */
1365110d74a9SMichel Lespinasse 		if (page->mapping && trylock_page(page)) {
1366110d74a9SMichel Lespinasse 			lru_add_drain();  /* push cached pages to LRU */
1367110d74a9SMichel Lespinasse 			/*
1368110d74a9SMichel Lespinasse 			 * Because we lock page here and migration is
1369110d74a9SMichel Lespinasse 			 * blocked by the pte's page reference, we need
1370110d74a9SMichel Lespinasse 			 * only check for file-cache page truncation.
1371110d74a9SMichel Lespinasse 			 */
1372110d74a9SMichel Lespinasse 			if (page->mapping)
1373110d74a9SMichel Lespinasse 				mlock_vma_page(page);
1374110d74a9SMichel Lespinasse 			unlock_page(page);
1375110d74a9SMichel Lespinasse 		}
1376110d74a9SMichel Lespinasse 	}
1377deceb6cdSHugh Dickins unlock:
1378deceb6cdSHugh Dickins 	pte_unmap_unlock(ptep, ptl);
13791da177e4SLinus Torvalds out:
1380deceb6cdSHugh Dickins 	return page;
1381deceb6cdSHugh Dickins 
138289f5b7daSLinus Torvalds bad_page:
138389f5b7daSLinus Torvalds 	pte_unmap_unlock(ptep, ptl);
138489f5b7daSLinus Torvalds 	return ERR_PTR(-EFAULT);
138589f5b7daSLinus Torvalds 
138689f5b7daSLinus Torvalds no_page:
138789f5b7daSLinus Torvalds 	pte_unmap_unlock(ptep, ptl);
138889f5b7daSLinus Torvalds 	if (!pte_none(pte))
138989f5b7daSLinus Torvalds 		return page;
13908e4b9a60SHugh Dickins 
1391deceb6cdSHugh Dickins no_page_table:
1392deceb6cdSHugh Dickins 	/*
1393deceb6cdSHugh Dickins 	 * When core dumping an enormous anonymous area that nobody
13948e4b9a60SHugh Dickins 	 * has touched so far, we don't want to allocate unnecessary pages or
13958e4b9a60SHugh Dickins 	 * page tables.  Return error instead of NULL to skip handle_mm_fault,
13968e4b9a60SHugh Dickins 	 * then get_dump_page() will return NULL to leave a hole in the dump.
13978e4b9a60SHugh Dickins 	 * But we can only make this optimization where a hole would surely
13988e4b9a60SHugh Dickins 	 * be zero-filled if handle_mm_fault() actually did handle it.
1399deceb6cdSHugh Dickins 	 */
14008e4b9a60SHugh Dickins 	if ((flags & FOLL_DUMP) &&
14018e4b9a60SHugh Dickins 	    (!vma->vm_ops || !vma->vm_ops->fault))
14028e4b9a60SHugh Dickins 		return ERR_PTR(-EFAULT);
1403deceb6cdSHugh Dickins 	return page;
14041da177e4SLinus Torvalds }
14051da177e4SLinus Torvalds 
1406b291f000SNick Piggin int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
140758fa879eSHugh Dickins 		     unsigned long start, int nr_pages, unsigned int gup_flags,
140853a7706dSMichel Lespinasse 		     struct page **pages, struct vm_area_struct **vmas,
140953a7706dSMichel Lespinasse 		     int *nonblocking)
14101da177e4SLinus Torvalds {
14111da177e4SLinus Torvalds 	int i;
141258fa879eSHugh Dickins 	unsigned long vm_flags;
14131da177e4SLinus Torvalds 
14149d73777eSPeter Zijlstra 	if (nr_pages <= 0)
1415900cf086SJonathan Corbet 		return 0;
141658fa879eSHugh Dickins 
141758fa879eSHugh Dickins 	VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET));
141858fa879eSHugh Dickins 
14191da177e4SLinus Torvalds 	/*
14201da177e4SLinus Torvalds 	 * Require read or write permissions.
142158fa879eSHugh Dickins 	 * If FOLL_FORCE is set, we only require the "MAY" flags.
14221da177e4SLinus Torvalds 	 */
142358fa879eSHugh Dickins 	vm_flags  = (gup_flags & FOLL_WRITE) ?
142458fa879eSHugh Dickins 			(VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
142558fa879eSHugh Dickins 	vm_flags &= (gup_flags & FOLL_FORCE) ?
142658fa879eSHugh Dickins 			(VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
14271da177e4SLinus Torvalds 	i = 0;
14281da177e4SLinus Torvalds 
14291da177e4SLinus Torvalds 	do {
14301da177e4SLinus Torvalds 		struct vm_area_struct *vma;
14311da177e4SLinus Torvalds 
14321da177e4SLinus Torvalds 		vma = find_extend_vma(mm, start);
14331da177e4SLinus Torvalds 		if (!vma && in_gate_area(tsk, start)) {
14341da177e4SLinus Torvalds 			unsigned long pg = start & PAGE_MASK;
14351da177e4SLinus Torvalds 			struct vm_area_struct *gate_vma = get_gate_vma(tsk);
14361da177e4SLinus Torvalds 			pgd_t *pgd;
14371da177e4SLinus Torvalds 			pud_t *pud;
14381da177e4SLinus Torvalds 			pmd_t *pmd;
14391da177e4SLinus Torvalds 			pte_t *pte;
1440b291f000SNick Piggin 
1441b291f000SNick Piggin 			/* user gate pages are read-only */
144258fa879eSHugh Dickins 			if (gup_flags & FOLL_WRITE)
14431da177e4SLinus Torvalds 				return i ? : -EFAULT;
14441da177e4SLinus Torvalds 			if (pg > TASK_SIZE)
14451da177e4SLinus Torvalds 				pgd = pgd_offset_k(pg);
14461da177e4SLinus Torvalds 			else
14471da177e4SLinus Torvalds 				pgd = pgd_offset_gate(mm, pg);
14481da177e4SLinus Torvalds 			BUG_ON(pgd_none(*pgd));
14491da177e4SLinus Torvalds 			pud = pud_offset(pgd, pg);
14501da177e4SLinus Torvalds 			BUG_ON(pud_none(*pud));
14511da177e4SLinus Torvalds 			pmd = pmd_offset(pud, pg);
1452690dbe1cSHugh Dickins 			if (pmd_none(*pmd))
1453690dbe1cSHugh Dickins 				return i ? : -EFAULT;
14541da177e4SLinus Torvalds 			pte = pte_offset_map(pmd, pg);
1455690dbe1cSHugh Dickins 			if (pte_none(*pte)) {
1456690dbe1cSHugh Dickins 				pte_unmap(pte);
1457690dbe1cSHugh Dickins 				return i ? : -EFAULT;
1458690dbe1cSHugh Dickins 			}
14591da177e4SLinus Torvalds 			if (pages) {
1460de51257aSHugh Dickins 				struct page *page;
1461de51257aSHugh Dickins 
1462de51257aSHugh Dickins 				page = vm_normal_page(gate_vma, start, *pte);
1463de51257aSHugh Dickins 				if (!page) {
1464de51257aSHugh Dickins 					if (!(gup_flags & FOLL_DUMP) &&
1465de51257aSHugh Dickins 					     is_zero_pfn(pte_pfn(*pte)))
1466de51257aSHugh Dickins 						page = pte_page(*pte);
1467de51257aSHugh Dickins 					else {
1468de51257aSHugh Dickins 						pte_unmap(pte);
1469de51257aSHugh Dickins 						return i ? : -EFAULT;
1470de51257aSHugh Dickins 					}
1471de51257aSHugh Dickins 				}
14726aab341eSLinus Torvalds 				pages[i] = page;
14736aab341eSLinus Torvalds 				get_page(page);
14741da177e4SLinus Torvalds 			}
14751da177e4SLinus Torvalds 			pte_unmap(pte);
14761da177e4SLinus Torvalds 			if (vmas)
14771da177e4SLinus Torvalds 				vmas[i] = gate_vma;
14781da177e4SLinus Torvalds 			i++;
14791da177e4SLinus Torvalds 			start += PAGE_SIZE;
14809d73777eSPeter Zijlstra 			nr_pages--;
14811da177e4SLinus Torvalds 			continue;
14821da177e4SLinus Torvalds 		}
14831da177e4SLinus Torvalds 
1484b291f000SNick Piggin 		if (!vma ||
1485b291f000SNick Piggin 		    (vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
14861c3aff1cSHugh Dickins 		    !(vm_flags & vma->vm_flags))
14871da177e4SLinus Torvalds 			return i ? : -EFAULT;
14881da177e4SLinus Torvalds 
14892a15efc9SHugh Dickins 		if (is_vm_hugetlb_page(vma)) {
14902a15efc9SHugh Dickins 			i = follow_hugetlb_page(mm, vma, pages, vmas,
149158fa879eSHugh Dickins 					&start, &nr_pages, i, gup_flags);
14922a15efc9SHugh Dickins 			continue;
14932a15efc9SHugh Dickins 		}
1494deceb6cdSHugh Dickins 
14951da177e4SLinus Torvalds 		do {
149608ef4729SHugh Dickins 			struct page *page;
149758fa879eSHugh Dickins 			unsigned int foll_flags = gup_flags;
14981da177e4SLinus Torvalds 
1499462e00ccSEthan Solomita 			/*
15004779280dSYing Han 			 * If we have a pending SIGKILL, don't keep faulting
15011c3aff1cSHugh Dickins 			 * pages and potentially allocating memory.
1502462e00ccSEthan Solomita 			 */
15031c3aff1cSHugh Dickins 			if (unlikely(fatal_signal_pending(current)))
15044779280dSYing Han 				return i ? i : -ERESTARTSYS;
1505462e00ccSEthan Solomita 
1506deceb6cdSHugh Dickins 			cond_resched();
15076aab341eSLinus Torvalds 			while (!(page = follow_page(vma, start, foll_flags))) {
1508a68d2ebcSLinus Torvalds 				int ret;
150953a7706dSMichel Lespinasse 				unsigned int fault_flags = 0;
151053a7706dSMichel Lespinasse 
151153a7706dSMichel Lespinasse 				if (foll_flags & FOLL_WRITE)
151253a7706dSMichel Lespinasse 					fault_flags |= FAULT_FLAG_WRITE;
151353a7706dSMichel Lespinasse 				if (nonblocking)
151453a7706dSMichel Lespinasse 					fault_flags |= FAULT_FLAG_ALLOW_RETRY;
1515d06063ccSLinus Torvalds 
1516d26ed650SHugh Dickins 				ret = handle_mm_fault(mm, vma, start,
151753a7706dSMichel Lespinasse 							fault_flags);
1518d26ed650SHugh Dickins 
151983c54070SNick Piggin 				if (ret & VM_FAULT_ERROR) {
152083c54070SNick Piggin 					if (ret & VM_FAULT_OOM)
152183c54070SNick Piggin 						return i ? i : -ENOMEM;
1522d1737fdbSAndi Kleen 					if (ret &
1523aa50d3a7SAndi Kleen 					    (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE|
1524aa50d3a7SAndi Kleen 					     VM_FAULT_SIGBUS))
152583c54070SNick Piggin 						return i ? i : -EFAULT;
152683c54070SNick Piggin 					BUG();
152783c54070SNick Piggin 				}
152883c54070SNick Piggin 				if (ret & VM_FAULT_MAJOR)
152983c54070SNick Piggin 					tsk->maj_flt++;
153083c54070SNick Piggin 				else
153183c54070SNick Piggin 					tsk->min_flt++;
153283c54070SNick Piggin 
153353a7706dSMichel Lespinasse 				if (ret & VM_FAULT_RETRY) {
153453a7706dSMichel Lespinasse 					*nonblocking = 0;
153553a7706dSMichel Lespinasse 					return i;
153653a7706dSMichel Lespinasse 				}
153753a7706dSMichel Lespinasse 
1538f33ea7f4SNick Piggin 				/*
153983c54070SNick Piggin 				 * The VM_FAULT_WRITE bit tells us that
154083c54070SNick Piggin 				 * do_wp_page has broken COW when necessary,
154183c54070SNick Piggin 				 * even if maybe_mkwrite decided not to set
154283c54070SNick Piggin 				 * pte_write. We can thus safely do subsequent
1543878b63acSHugh Dickins 				 * page lookups as if they were reads. But only
1544878b63acSHugh Dickins 				 * do so when looping for pte_write is futile:
1545878b63acSHugh Dickins 				 * in some cases userspace may also be wanting
1546878b63acSHugh Dickins 				 * to write to the gotten user page, which a
1547878b63acSHugh Dickins 				 * read fault here might prevent (a readonly
1548878b63acSHugh Dickins 				 * page might get reCOWed by userspace write).
1549f33ea7f4SNick Piggin 				 */
1550878b63acSHugh Dickins 				if ((ret & VM_FAULT_WRITE) &&
1551878b63acSHugh Dickins 				    !(vma->vm_flags & VM_WRITE))
1552deceb6cdSHugh Dickins 					foll_flags &= ~FOLL_WRITE;
1553a68d2ebcSLinus Torvalds 
15547f7bbbe5SBenjamin Herrenschmidt 				cond_resched();
15551da177e4SLinus Torvalds 			}
155689f5b7daSLinus Torvalds 			if (IS_ERR(page))
155789f5b7daSLinus Torvalds 				return i ? i : PTR_ERR(page);
15581da177e4SLinus Torvalds 			if (pages) {
155908ef4729SHugh Dickins 				pages[i] = page;
156003beb076SJames Bottomley 
1561a6f36be3SRussell King 				flush_anon_page(vma, page, start);
156208ef4729SHugh Dickins 				flush_dcache_page(page);
15631da177e4SLinus Torvalds 			}
15641da177e4SLinus Torvalds 			if (vmas)
15651da177e4SLinus Torvalds 				vmas[i] = vma;
15661da177e4SLinus Torvalds 			i++;
15671da177e4SLinus Torvalds 			start += PAGE_SIZE;
15689d73777eSPeter Zijlstra 			nr_pages--;
15699d73777eSPeter Zijlstra 		} while (nr_pages && start < vma->vm_end);
15709d73777eSPeter Zijlstra 	} while (nr_pages);
15711da177e4SLinus Torvalds 	return i;
15721da177e4SLinus Torvalds }
1573b291f000SNick Piggin 
1574d2bf6be8SNick Piggin /**
1575d2bf6be8SNick Piggin  * get_user_pages() - pin user pages in memory
1576d2bf6be8SNick Piggin  * @tsk:	task_struct of target task
1577d2bf6be8SNick Piggin  * @mm:		mm_struct of target mm
1578d2bf6be8SNick Piggin  * @start:	starting user address
15799d73777eSPeter Zijlstra  * @nr_pages:	number of pages from start to pin
1580d2bf6be8SNick Piggin  * @write:	whether pages will be written to by the caller
1581d2bf6be8SNick Piggin  * @force:	whether to force write access even if user mapping is
1582d2bf6be8SNick Piggin  *		readonly. This will result in the page being COWed even
1583d2bf6be8SNick Piggin  *		in MAP_SHARED mappings. You do not want this.
1584d2bf6be8SNick Piggin  * @pages:	array that receives pointers to the pages pinned.
1585d2bf6be8SNick Piggin  *		Should be at least nr_pages long. Or NULL, if caller
1586d2bf6be8SNick Piggin  *		only intends to ensure the pages are faulted in.
1587d2bf6be8SNick Piggin  * @vmas:	array of pointers to vmas corresponding to each page.
1588d2bf6be8SNick Piggin  *		Or NULL if the caller does not require them.
1589d2bf6be8SNick Piggin  *
1590d2bf6be8SNick Piggin  * Returns number of pages pinned. This may be fewer than the number
15919d73777eSPeter Zijlstra  * requested. If nr_pages is 0 or negative, returns 0. If no pages
1592d2bf6be8SNick Piggin  * were pinned, returns -errno. Each page returned must be released
1593d2bf6be8SNick Piggin  * with a put_page() call when it is finished with. vmas will only
1594d2bf6be8SNick Piggin  * remain valid while mmap_sem is held.
1595d2bf6be8SNick Piggin  *
1596d2bf6be8SNick Piggin  * Must be called with mmap_sem held for read or write.
1597d2bf6be8SNick Piggin  *
1598d2bf6be8SNick Piggin  * get_user_pages walks a process's page tables and takes a reference to
1599d2bf6be8SNick Piggin  * each struct page that each user address corresponds to at a given
1600d2bf6be8SNick Piggin  * instant. That is, it takes the page that would be accessed if a user
1601d2bf6be8SNick Piggin  * thread accesses the given user virtual address at that instant.
1602d2bf6be8SNick Piggin  *
1603d2bf6be8SNick Piggin  * This does not guarantee that the page exists in the user mappings when
1604d2bf6be8SNick Piggin  * get_user_pages returns, and there may even be a completely different
1605d2bf6be8SNick Piggin  * page there in some cases (eg. if mmapped pagecache has been invalidated
1606d2bf6be8SNick Piggin  * and subsequently re faulted). However it does guarantee that the page
1607d2bf6be8SNick Piggin  * won't be freed completely. And mostly callers simply care that the page
1608d2bf6be8SNick Piggin  * contains data that was valid *at some point in time*. Typically, an IO
1609d2bf6be8SNick Piggin  * or similar operation cannot guarantee anything stronger anyway because
1610d2bf6be8SNick Piggin  * locks can't be held over the syscall boundary.
1611d2bf6be8SNick Piggin  *
1612d2bf6be8SNick Piggin  * If write=0, the page must not be written to. If the page is written to,
1613d2bf6be8SNick Piggin  * set_page_dirty (or set_page_dirty_lock, as appropriate) must be called
1614d2bf6be8SNick Piggin  * after the page is finished with, and before put_page is called.
1615d2bf6be8SNick Piggin  *
1616d2bf6be8SNick Piggin  * get_user_pages is typically used for fewer-copy IO operations, to get a
1617d2bf6be8SNick Piggin  * handle on the memory by some means other than accesses via the user virtual
1618d2bf6be8SNick Piggin  * addresses. The pages may be submitted for DMA to devices or accessed via
1619d2bf6be8SNick Piggin  * their kernel linear mapping (via the kmap APIs). Care should be taken to
1620d2bf6be8SNick Piggin  * use the correct cache flushing APIs.
1621d2bf6be8SNick Piggin  *
1622d2bf6be8SNick Piggin  * See also get_user_pages_fast, for performance critical applications.
1623d2bf6be8SNick Piggin  */
1624b291f000SNick Piggin int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
16259d73777eSPeter Zijlstra 		unsigned long start, int nr_pages, int write, int force,
1626b291f000SNick Piggin 		struct page **pages, struct vm_area_struct **vmas)
1627b291f000SNick Piggin {
162858fa879eSHugh Dickins 	int flags = FOLL_TOUCH;
1629b291f000SNick Piggin 
163058fa879eSHugh Dickins 	if (pages)
163158fa879eSHugh Dickins 		flags |= FOLL_GET;
1632b291f000SNick Piggin 	if (write)
163358fa879eSHugh Dickins 		flags |= FOLL_WRITE;
1634b291f000SNick Piggin 	if (force)
163558fa879eSHugh Dickins 		flags |= FOLL_FORCE;
1636b291f000SNick Piggin 
163753a7706dSMichel Lespinasse 	return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas,
163853a7706dSMichel Lespinasse 				NULL);
1639b291f000SNick Piggin }
16401da177e4SLinus Torvalds EXPORT_SYMBOL(get_user_pages);
16411da177e4SLinus Torvalds 
1642f3e8fccdSHugh Dickins /**
1643f3e8fccdSHugh Dickins  * get_dump_page() - pin user page in memory while writing it to core dump
1644f3e8fccdSHugh Dickins  * @addr: user address
1645f3e8fccdSHugh Dickins  *
1646f3e8fccdSHugh Dickins  * Returns struct page pointer of user page pinned for dump,
1647f3e8fccdSHugh Dickins  * to be freed afterwards by page_cache_release() or put_page().
1648f3e8fccdSHugh Dickins  *
1649f3e8fccdSHugh Dickins  * Returns NULL on any kind of failure - a hole must then be inserted into
1650f3e8fccdSHugh Dickins  * the corefile, to preserve alignment with its headers; and also returns
1651f3e8fccdSHugh Dickins  * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
1652f3e8fccdSHugh Dickins  * allowing a hole to be left in the corefile to save diskspace.
1653f3e8fccdSHugh Dickins  *
1654f3e8fccdSHugh Dickins  * Called without mmap_sem, but after all other threads have been killed.
1655f3e8fccdSHugh Dickins  */
1656f3e8fccdSHugh Dickins #ifdef CONFIG_ELF_CORE
1657f3e8fccdSHugh Dickins struct page *get_dump_page(unsigned long addr)
1658f3e8fccdSHugh Dickins {
1659f3e8fccdSHugh Dickins 	struct vm_area_struct *vma;
1660f3e8fccdSHugh Dickins 	struct page *page;
1661f3e8fccdSHugh Dickins 
1662f3e8fccdSHugh Dickins 	if (__get_user_pages(current, current->mm, addr, 1,
166353a7706dSMichel Lespinasse 			     FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma,
166453a7706dSMichel Lespinasse 			     NULL) < 1)
1665f3e8fccdSHugh Dickins 		return NULL;
1666f3e8fccdSHugh Dickins 	flush_cache_page(vma, addr, page_to_pfn(page));
1667f3e8fccdSHugh Dickins 	return page;
1668f3e8fccdSHugh Dickins }
1669f3e8fccdSHugh Dickins #endif /* CONFIG_ELF_CORE */
1670f3e8fccdSHugh Dickins 
167125ca1d6cSNamhyung Kim pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
1672920c7a5dSHarvey Harrison 			spinlock_t **ptl)
1673c9cfcddfSLinus Torvalds {
1674c9cfcddfSLinus Torvalds 	pgd_t * pgd = pgd_offset(mm, addr);
1675c9cfcddfSLinus Torvalds 	pud_t * pud = pud_alloc(mm, pgd, addr);
1676c9cfcddfSLinus Torvalds 	if (pud) {
167749c91fb0STrond Myklebust 		pmd_t * pmd = pmd_alloc(mm, pud, addr);
1678c9cfcddfSLinus Torvalds 		if (pmd)
1679c9cfcddfSLinus Torvalds 			return pte_alloc_map_lock(mm, pmd, addr, ptl);
1680c9cfcddfSLinus Torvalds 	}
1681c9cfcddfSLinus Torvalds 	return NULL;
1682c9cfcddfSLinus Torvalds }
1683c9cfcddfSLinus Torvalds 
16841da177e4SLinus Torvalds /*
1685238f58d8SLinus Torvalds  * This is the old fallback for page remapping.
1686238f58d8SLinus Torvalds  *
1687238f58d8SLinus Torvalds  * For historical reasons, it only allows reserved pages. Only
1688238f58d8SLinus Torvalds  * old drivers should use this, and they needed to mark their
1689238f58d8SLinus Torvalds  * pages reserved for the old functions anyway.
1690238f58d8SLinus Torvalds  */
1691423bad60SNick Piggin static int insert_page(struct vm_area_struct *vma, unsigned long addr,
1692423bad60SNick Piggin 			struct page *page, pgprot_t prot)
1693238f58d8SLinus Torvalds {
1694423bad60SNick Piggin 	struct mm_struct *mm = vma->vm_mm;
1695238f58d8SLinus Torvalds 	int retval;
1696238f58d8SLinus Torvalds 	pte_t *pte;
1697238f58d8SLinus Torvalds 	spinlock_t *ptl;
1698238f58d8SLinus Torvalds 
1699238f58d8SLinus Torvalds 	retval = -EINVAL;
1700a145dd41SLinus Torvalds 	if (PageAnon(page))
17015b4e655eSKAMEZAWA Hiroyuki 		goto out;
1702238f58d8SLinus Torvalds 	retval = -ENOMEM;
1703238f58d8SLinus Torvalds 	flush_dcache_page(page);
1704c9cfcddfSLinus Torvalds 	pte = get_locked_pte(mm, addr, &ptl);
1705238f58d8SLinus Torvalds 	if (!pte)
17065b4e655eSKAMEZAWA Hiroyuki 		goto out;
1707238f58d8SLinus Torvalds 	retval = -EBUSY;
1708238f58d8SLinus Torvalds 	if (!pte_none(*pte))
1709238f58d8SLinus Torvalds 		goto out_unlock;
1710238f58d8SLinus Torvalds 
1711238f58d8SLinus Torvalds 	/* Ok, finally just insert the thing.. */
1712238f58d8SLinus Torvalds 	get_page(page);
171334e55232SKAMEZAWA Hiroyuki 	inc_mm_counter_fast(mm, MM_FILEPAGES);
1714238f58d8SLinus Torvalds 	page_add_file_rmap(page);
1715238f58d8SLinus Torvalds 	set_pte_at(mm, addr, pte, mk_pte(page, prot));
1716238f58d8SLinus Torvalds 
1717238f58d8SLinus Torvalds 	retval = 0;
17188a9f3ccdSBalbir Singh 	pte_unmap_unlock(pte, ptl);
17198a9f3ccdSBalbir Singh 	return retval;
1720238f58d8SLinus Torvalds out_unlock:
1721238f58d8SLinus Torvalds 	pte_unmap_unlock(pte, ptl);
1722238f58d8SLinus Torvalds out:
1723238f58d8SLinus Torvalds 	return retval;
1724238f58d8SLinus Torvalds }
1725238f58d8SLinus Torvalds 
1726bfa5bf6dSRolf Eike Beer /**
1727bfa5bf6dSRolf Eike Beer  * vm_insert_page - insert single page into user vma
1728bfa5bf6dSRolf Eike Beer  * @vma: user vma to map to
1729bfa5bf6dSRolf Eike Beer  * @addr: target user address of this page
1730bfa5bf6dSRolf Eike Beer  * @page: source kernel page
1731bfa5bf6dSRolf Eike Beer  *
1732a145dd41SLinus Torvalds  * This allows drivers to insert individual pages they've allocated
1733a145dd41SLinus Torvalds  * into a user vma.
1734a145dd41SLinus Torvalds  *
1735a145dd41SLinus Torvalds  * The page has to be a nice clean _individual_ kernel allocation.
1736a145dd41SLinus Torvalds  * If you allocate a compound page, you need to have marked it as
1737a145dd41SLinus Torvalds  * such (__GFP_COMP), or manually just split the page up yourself
17388dfcc9baSNick Piggin  * (see split_page()).
1739a145dd41SLinus Torvalds  *
1740a145dd41SLinus Torvalds  * NOTE! Traditionally this was done with "remap_pfn_range()" which
1741a145dd41SLinus Torvalds  * took an arbitrary page protection parameter. This doesn't allow
1742a145dd41SLinus Torvalds  * that. Your vma protection will have to be set up correctly, which
1743a145dd41SLinus Torvalds  * means that if you want a shared writable mapping, you'd better
1744a145dd41SLinus Torvalds  * ask for a shared writable mapping!
1745a145dd41SLinus Torvalds  *
1746a145dd41SLinus Torvalds  * The page does not need to be reserved.
1747a145dd41SLinus Torvalds  */
1748423bad60SNick Piggin int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
1749423bad60SNick Piggin 			struct page *page)
1750a145dd41SLinus Torvalds {
1751a145dd41SLinus Torvalds 	if (addr < vma->vm_start || addr >= vma->vm_end)
1752a145dd41SLinus Torvalds 		return -EFAULT;
1753a145dd41SLinus Torvalds 	if (!page_count(page))
1754a145dd41SLinus Torvalds 		return -EINVAL;
17554d7672b4SLinus Torvalds 	vma->vm_flags |= VM_INSERTPAGE;
1756423bad60SNick Piggin 	return insert_page(vma, addr, page, vma->vm_page_prot);
1757a145dd41SLinus Torvalds }
1758e3c3374fSLinus Torvalds EXPORT_SYMBOL(vm_insert_page);
1759a145dd41SLinus Torvalds 
1760423bad60SNick Piggin static int insert_pfn(struct vm_area_struct *vma, unsigned long addr,
1761423bad60SNick Piggin 			unsigned long pfn, pgprot_t prot)
1762423bad60SNick Piggin {
1763423bad60SNick Piggin 	struct mm_struct *mm = vma->vm_mm;
1764423bad60SNick Piggin 	int retval;
1765423bad60SNick Piggin 	pte_t *pte, entry;
1766423bad60SNick Piggin 	spinlock_t *ptl;
1767423bad60SNick Piggin 
1768423bad60SNick Piggin 	retval = -ENOMEM;
1769423bad60SNick Piggin 	pte = get_locked_pte(mm, addr, &ptl);
1770423bad60SNick Piggin 	if (!pte)
1771423bad60SNick Piggin 		goto out;
1772423bad60SNick Piggin 	retval = -EBUSY;
1773423bad60SNick Piggin 	if (!pte_none(*pte))
1774423bad60SNick Piggin 		goto out_unlock;
1775423bad60SNick Piggin 
1776423bad60SNick Piggin 	/* Ok, finally just insert the thing.. */
1777423bad60SNick Piggin 	entry = pte_mkspecial(pfn_pte(pfn, prot));
1778423bad60SNick Piggin 	set_pte_at(mm, addr, pte, entry);
17794b3073e1SRussell King 	update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */
1780423bad60SNick Piggin 
1781423bad60SNick Piggin 	retval = 0;
1782423bad60SNick Piggin out_unlock:
1783423bad60SNick Piggin 	pte_unmap_unlock(pte, ptl);
1784423bad60SNick Piggin out:
1785423bad60SNick Piggin 	return retval;
1786423bad60SNick Piggin }
1787423bad60SNick Piggin 
1788e0dc0d8fSNick Piggin /**
1789e0dc0d8fSNick Piggin  * vm_insert_pfn - insert single pfn into user vma
1790e0dc0d8fSNick Piggin  * @vma: user vma to map to
1791e0dc0d8fSNick Piggin  * @addr: target user address of this page
1792e0dc0d8fSNick Piggin  * @pfn: source kernel pfn
1793e0dc0d8fSNick Piggin  *
1794e0dc0d8fSNick Piggin  * Similar to vm_inert_page, this allows drivers to insert individual pages
1795e0dc0d8fSNick Piggin  * they've allocated into a user vma. Same comments apply.
1796e0dc0d8fSNick Piggin  *
1797e0dc0d8fSNick Piggin  * This function should only be called from a vm_ops->fault handler, and
1798e0dc0d8fSNick Piggin  * in that case the handler should return NULL.
17990d71d10aSNick Piggin  *
18000d71d10aSNick Piggin  * vma cannot be a COW mapping.
18010d71d10aSNick Piggin  *
18020d71d10aSNick Piggin  * As this is called only for pages that do not currently exist, we
18030d71d10aSNick Piggin  * do not need to flush old virtual caches or the TLB.
1804e0dc0d8fSNick Piggin  */
1805e0dc0d8fSNick Piggin int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
1806e0dc0d8fSNick Piggin 			unsigned long pfn)
1807e0dc0d8fSNick Piggin {
18082ab64037Svenkatesh.pallipadi@intel.com 	int ret;
1809e4b866edSvenkatesh.pallipadi@intel.com 	pgprot_t pgprot = vma->vm_page_prot;
18107e675137SNick Piggin 	/*
18117e675137SNick Piggin 	 * Technically, architectures with pte_special can avoid all these
18127e675137SNick Piggin 	 * restrictions (same for remap_pfn_range).  However we would like
18137e675137SNick Piggin 	 * consistency in testing and feature parity among all, so we should
18147e675137SNick Piggin 	 * try to keep these invariants in place for everybody.
18157e675137SNick Piggin 	 */
1816b379d790SJared Hulbert 	BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
1817b379d790SJared Hulbert 	BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
1818b379d790SJared Hulbert 						(VM_PFNMAP|VM_MIXEDMAP));
1819b379d790SJared Hulbert 	BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
1820b379d790SJared Hulbert 	BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
1821e0dc0d8fSNick Piggin 
1822423bad60SNick Piggin 	if (addr < vma->vm_start || addr >= vma->vm_end)
1823423bad60SNick Piggin 		return -EFAULT;
1824e4b866edSvenkatesh.pallipadi@intel.com 	if (track_pfn_vma_new(vma, &pgprot, pfn, PAGE_SIZE))
18252ab64037Svenkatesh.pallipadi@intel.com 		return -EINVAL;
18262ab64037Svenkatesh.pallipadi@intel.com 
1827e4b866edSvenkatesh.pallipadi@intel.com 	ret = insert_pfn(vma, addr, pfn, pgprot);
18282ab64037Svenkatesh.pallipadi@intel.com 
18292ab64037Svenkatesh.pallipadi@intel.com 	if (ret)
18302ab64037Svenkatesh.pallipadi@intel.com 		untrack_pfn_vma(vma, pfn, PAGE_SIZE);
18312ab64037Svenkatesh.pallipadi@intel.com 
18322ab64037Svenkatesh.pallipadi@intel.com 	return ret;
1833e0dc0d8fSNick Piggin }
1834e0dc0d8fSNick Piggin EXPORT_SYMBOL(vm_insert_pfn);
1835e0dc0d8fSNick Piggin 
1836423bad60SNick Piggin int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
1837423bad60SNick Piggin 			unsigned long pfn)
1838423bad60SNick Piggin {
1839423bad60SNick Piggin 	BUG_ON(!(vma->vm_flags & VM_MIXEDMAP));
1840423bad60SNick Piggin 
1841423bad60SNick Piggin 	if (addr < vma->vm_start || addr >= vma->vm_end)
1842423bad60SNick Piggin 		return -EFAULT;
1843423bad60SNick Piggin 
1844423bad60SNick Piggin 	/*
1845423bad60SNick Piggin 	 * If we don't have pte special, then we have to use the pfn_valid()
1846423bad60SNick Piggin 	 * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must*
1847423bad60SNick Piggin 	 * refcount the page if pfn_valid is true (hence insert_page rather
184862eede62SHugh Dickins 	 * than insert_pfn).  If a zero_pfn were inserted into a VM_MIXEDMAP
184962eede62SHugh Dickins 	 * without pte special, it would there be refcounted as a normal page.
1850423bad60SNick Piggin 	 */
1851423bad60SNick Piggin 	if (!HAVE_PTE_SPECIAL && pfn_valid(pfn)) {
1852423bad60SNick Piggin 		struct page *page;
1853423bad60SNick Piggin 
1854423bad60SNick Piggin 		page = pfn_to_page(pfn);
1855423bad60SNick Piggin 		return insert_page(vma, addr, page, vma->vm_page_prot);
1856423bad60SNick Piggin 	}
1857423bad60SNick Piggin 	return insert_pfn(vma, addr, pfn, vma->vm_page_prot);
1858423bad60SNick Piggin }
1859423bad60SNick Piggin EXPORT_SYMBOL(vm_insert_mixed);
1860423bad60SNick Piggin 
1861a145dd41SLinus Torvalds /*
18621da177e4SLinus Torvalds  * maps a range of physical memory into the requested pages. the old
18631da177e4SLinus Torvalds  * mappings are removed. any references to nonexistent pages results
18641da177e4SLinus Torvalds  * in null mappings (currently treated as "copy-on-access")
18651da177e4SLinus Torvalds  */
18661da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
18671da177e4SLinus Torvalds 			unsigned long addr, unsigned long end,
18681da177e4SLinus Torvalds 			unsigned long pfn, pgprot_t prot)
18691da177e4SLinus Torvalds {
18701da177e4SLinus Torvalds 	pte_t *pte;
1871c74df32cSHugh Dickins 	spinlock_t *ptl;
18721da177e4SLinus Torvalds 
1873c74df32cSHugh Dickins 	pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
18741da177e4SLinus Torvalds 	if (!pte)
18751da177e4SLinus Torvalds 		return -ENOMEM;
18766606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
18771da177e4SLinus Torvalds 	do {
18781da177e4SLinus Torvalds 		BUG_ON(!pte_none(*pte));
18797e675137SNick Piggin 		set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot)));
18801da177e4SLinus Torvalds 		pfn++;
18811da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
18826606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
1883c74df32cSHugh Dickins 	pte_unmap_unlock(pte - 1, ptl);
18841da177e4SLinus Torvalds 	return 0;
18851da177e4SLinus Torvalds }
18861da177e4SLinus Torvalds 
18871da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
18881da177e4SLinus Torvalds 			unsigned long addr, unsigned long end,
18891da177e4SLinus Torvalds 			unsigned long pfn, pgprot_t prot)
18901da177e4SLinus Torvalds {
18911da177e4SLinus Torvalds 	pmd_t *pmd;
18921da177e4SLinus Torvalds 	unsigned long next;
18931da177e4SLinus Torvalds 
18941da177e4SLinus Torvalds 	pfn -= addr >> PAGE_SHIFT;
18951da177e4SLinus Torvalds 	pmd = pmd_alloc(mm, pud, addr);
18961da177e4SLinus Torvalds 	if (!pmd)
18971da177e4SLinus Torvalds 		return -ENOMEM;
18981da177e4SLinus Torvalds 	do {
18991da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
19001da177e4SLinus Torvalds 		if (remap_pte_range(mm, pmd, addr, next,
19011da177e4SLinus Torvalds 				pfn + (addr >> PAGE_SHIFT), prot))
19021da177e4SLinus Torvalds 			return -ENOMEM;
19031da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
19041da177e4SLinus Torvalds 	return 0;
19051da177e4SLinus Torvalds }
19061da177e4SLinus Torvalds 
19071da177e4SLinus Torvalds static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd,
19081da177e4SLinus Torvalds 			unsigned long addr, unsigned long end,
19091da177e4SLinus Torvalds 			unsigned long pfn, pgprot_t prot)
19101da177e4SLinus Torvalds {
19111da177e4SLinus Torvalds 	pud_t *pud;
19121da177e4SLinus Torvalds 	unsigned long next;
19131da177e4SLinus Torvalds 
19141da177e4SLinus Torvalds 	pfn -= addr >> PAGE_SHIFT;
19151da177e4SLinus Torvalds 	pud = pud_alloc(mm, pgd, addr);
19161da177e4SLinus Torvalds 	if (!pud)
19171da177e4SLinus Torvalds 		return -ENOMEM;
19181da177e4SLinus Torvalds 	do {
19191da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
19201da177e4SLinus Torvalds 		if (remap_pmd_range(mm, pud, addr, next,
19211da177e4SLinus Torvalds 				pfn + (addr >> PAGE_SHIFT), prot))
19221da177e4SLinus Torvalds 			return -ENOMEM;
19231da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
19241da177e4SLinus Torvalds 	return 0;
19251da177e4SLinus Torvalds }
19261da177e4SLinus Torvalds 
1927bfa5bf6dSRolf Eike Beer /**
1928bfa5bf6dSRolf Eike Beer  * remap_pfn_range - remap kernel memory to userspace
1929bfa5bf6dSRolf Eike Beer  * @vma: user vma to map to
1930bfa5bf6dSRolf Eike Beer  * @addr: target user address to start at
1931bfa5bf6dSRolf Eike Beer  * @pfn: physical address of kernel memory
1932bfa5bf6dSRolf Eike Beer  * @size: size of map area
1933bfa5bf6dSRolf Eike Beer  * @prot: page protection flags for this mapping
1934bfa5bf6dSRolf Eike Beer  *
1935bfa5bf6dSRolf Eike Beer  *  Note: this is only safe if the mm semaphore is held when called.
1936bfa5bf6dSRolf Eike Beer  */
19371da177e4SLinus Torvalds int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
19381da177e4SLinus Torvalds 		    unsigned long pfn, unsigned long size, pgprot_t prot)
19391da177e4SLinus Torvalds {
19401da177e4SLinus Torvalds 	pgd_t *pgd;
19411da177e4SLinus Torvalds 	unsigned long next;
19422d15cab8SHugh Dickins 	unsigned long end = addr + PAGE_ALIGN(size);
19431da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
19441da177e4SLinus Torvalds 	int err;
19451da177e4SLinus Torvalds 
19461da177e4SLinus Torvalds 	/*
19471da177e4SLinus Torvalds 	 * Physically remapped pages are special. Tell the
19481da177e4SLinus Torvalds 	 * rest of the world about it:
19491da177e4SLinus Torvalds 	 *   VM_IO tells people not to look at these pages
19501da177e4SLinus Torvalds 	 *	(accesses can have side effects).
19510b14c179SHugh Dickins 	 *   VM_RESERVED is specified all over the place, because
19520b14c179SHugh Dickins 	 *	in 2.4 it kept swapout's vma scan off this vma; but
19530b14c179SHugh Dickins 	 *	in 2.6 the LRU scan won't even find its pages, so this
19540b14c179SHugh Dickins 	 *	flag means no more than count its pages in reserved_vm,
19550b14c179SHugh Dickins 	 * 	and omit it from core dump, even when VM_IO turned off.
19566aab341eSLinus Torvalds 	 *   VM_PFNMAP tells the core MM that the base pages are just
19576aab341eSLinus Torvalds 	 *	raw PFN mappings, and do not have a "struct page" associated
19586aab341eSLinus Torvalds 	 *	with them.
1959fb155c16SLinus Torvalds 	 *
1960fb155c16SLinus Torvalds 	 * There's a horrible special case to handle copy-on-write
1961fb155c16SLinus Torvalds 	 * behaviour that some programs depend on. We mark the "original"
1962fb155c16SLinus Torvalds 	 * un-COW'ed pages by matching them up with "vma->vm_pgoff".
19631da177e4SLinus Torvalds 	 */
19644bb9c5c0SPallipadi, Venkatesh 	if (addr == vma->vm_start && end == vma->vm_end) {
19656aab341eSLinus Torvalds 		vma->vm_pgoff = pfn;
1966895791daSPallipadi, Venkatesh 		vma->vm_flags |= VM_PFN_AT_MMAP;
19674bb9c5c0SPallipadi, Venkatesh 	} else if (is_cow_mapping(vma->vm_flags))
19683c8bb73aSvenkatesh.pallipadi@intel.com 		return -EINVAL;
1969fb155c16SLinus Torvalds 
1970fb155c16SLinus Torvalds 	vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP;
19711da177e4SLinus Torvalds 
1972e4b866edSvenkatesh.pallipadi@intel.com 	err = track_pfn_vma_new(vma, &prot, pfn, PAGE_ALIGN(size));
1973a3670613Svenkatesh.pallipadi@intel.com 	if (err) {
1974a3670613Svenkatesh.pallipadi@intel.com 		/*
1975a3670613Svenkatesh.pallipadi@intel.com 		 * To indicate that track_pfn related cleanup is not
1976a3670613Svenkatesh.pallipadi@intel.com 		 * needed from higher level routine calling unmap_vmas
1977a3670613Svenkatesh.pallipadi@intel.com 		 */
1978a3670613Svenkatesh.pallipadi@intel.com 		vma->vm_flags &= ~(VM_IO | VM_RESERVED | VM_PFNMAP);
1979895791daSPallipadi, Venkatesh 		vma->vm_flags &= ~VM_PFN_AT_MMAP;
19802ab64037Svenkatesh.pallipadi@intel.com 		return -EINVAL;
1981a3670613Svenkatesh.pallipadi@intel.com 	}
19822ab64037Svenkatesh.pallipadi@intel.com 
19831da177e4SLinus Torvalds 	BUG_ON(addr >= end);
19841da177e4SLinus Torvalds 	pfn -= addr >> PAGE_SHIFT;
19851da177e4SLinus Torvalds 	pgd = pgd_offset(mm, addr);
19861da177e4SLinus Torvalds 	flush_cache_range(vma, addr, end);
19871da177e4SLinus Torvalds 	do {
19881da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
19891da177e4SLinus Torvalds 		err = remap_pud_range(mm, pgd, addr, next,
19901da177e4SLinus Torvalds 				pfn + (addr >> PAGE_SHIFT), prot);
19911da177e4SLinus Torvalds 		if (err)
19921da177e4SLinus Torvalds 			break;
19931da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
19942ab64037Svenkatesh.pallipadi@intel.com 
19952ab64037Svenkatesh.pallipadi@intel.com 	if (err)
19962ab64037Svenkatesh.pallipadi@intel.com 		untrack_pfn_vma(vma, pfn, PAGE_ALIGN(size));
19972ab64037Svenkatesh.pallipadi@intel.com 
19981da177e4SLinus Torvalds 	return err;
19991da177e4SLinus Torvalds }
20001da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range);
20011da177e4SLinus Torvalds 
2002aee16b3cSJeremy Fitzhardinge static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
2003aee16b3cSJeremy Fitzhardinge 				     unsigned long addr, unsigned long end,
2004aee16b3cSJeremy Fitzhardinge 				     pte_fn_t fn, void *data)
2005aee16b3cSJeremy Fitzhardinge {
2006aee16b3cSJeremy Fitzhardinge 	pte_t *pte;
2007aee16b3cSJeremy Fitzhardinge 	int err;
20082f569afdSMartin Schwidefsky 	pgtable_t token;
200994909914SBorislav Petkov 	spinlock_t *uninitialized_var(ptl);
2010aee16b3cSJeremy Fitzhardinge 
2011aee16b3cSJeremy Fitzhardinge 	pte = (mm == &init_mm) ?
2012aee16b3cSJeremy Fitzhardinge 		pte_alloc_kernel(pmd, addr) :
2013aee16b3cSJeremy Fitzhardinge 		pte_alloc_map_lock(mm, pmd, addr, &ptl);
2014aee16b3cSJeremy Fitzhardinge 	if (!pte)
2015aee16b3cSJeremy Fitzhardinge 		return -ENOMEM;
2016aee16b3cSJeremy Fitzhardinge 
2017aee16b3cSJeremy Fitzhardinge 	BUG_ON(pmd_huge(*pmd));
2018aee16b3cSJeremy Fitzhardinge 
201938e0edb1SJeremy Fitzhardinge 	arch_enter_lazy_mmu_mode();
202038e0edb1SJeremy Fitzhardinge 
20212f569afdSMartin Schwidefsky 	token = pmd_pgtable(*pmd);
2022aee16b3cSJeremy Fitzhardinge 
2023aee16b3cSJeremy Fitzhardinge 	do {
2024c36987e2SDaisuke Nishimura 		err = fn(pte++, token, addr, data);
2025aee16b3cSJeremy Fitzhardinge 		if (err)
2026aee16b3cSJeremy Fitzhardinge 			break;
2027c36987e2SDaisuke Nishimura 	} while (addr += PAGE_SIZE, addr != end);
2028aee16b3cSJeremy Fitzhardinge 
202938e0edb1SJeremy Fitzhardinge 	arch_leave_lazy_mmu_mode();
203038e0edb1SJeremy Fitzhardinge 
2031aee16b3cSJeremy Fitzhardinge 	if (mm != &init_mm)
2032aee16b3cSJeremy Fitzhardinge 		pte_unmap_unlock(pte-1, ptl);
2033aee16b3cSJeremy Fitzhardinge 	return err;
2034aee16b3cSJeremy Fitzhardinge }
2035aee16b3cSJeremy Fitzhardinge 
2036aee16b3cSJeremy Fitzhardinge static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
2037aee16b3cSJeremy Fitzhardinge 				     unsigned long addr, unsigned long end,
2038aee16b3cSJeremy Fitzhardinge 				     pte_fn_t fn, void *data)
2039aee16b3cSJeremy Fitzhardinge {
2040aee16b3cSJeremy Fitzhardinge 	pmd_t *pmd;
2041aee16b3cSJeremy Fitzhardinge 	unsigned long next;
2042aee16b3cSJeremy Fitzhardinge 	int err;
2043aee16b3cSJeremy Fitzhardinge 
2044ceb86879SAndi Kleen 	BUG_ON(pud_huge(*pud));
2045ceb86879SAndi Kleen 
2046aee16b3cSJeremy Fitzhardinge 	pmd = pmd_alloc(mm, pud, addr);
2047aee16b3cSJeremy Fitzhardinge 	if (!pmd)
2048aee16b3cSJeremy Fitzhardinge 		return -ENOMEM;
2049aee16b3cSJeremy Fitzhardinge 	do {
2050aee16b3cSJeremy Fitzhardinge 		next = pmd_addr_end(addr, end);
2051aee16b3cSJeremy Fitzhardinge 		err = apply_to_pte_range(mm, pmd, addr, next, fn, data);
2052aee16b3cSJeremy Fitzhardinge 		if (err)
2053aee16b3cSJeremy Fitzhardinge 			break;
2054aee16b3cSJeremy Fitzhardinge 	} while (pmd++, addr = next, addr != end);
2055aee16b3cSJeremy Fitzhardinge 	return err;
2056aee16b3cSJeremy Fitzhardinge }
2057aee16b3cSJeremy Fitzhardinge 
2058aee16b3cSJeremy Fitzhardinge static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd,
2059aee16b3cSJeremy Fitzhardinge 				     unsigned long addr, unsigned long end,
2060aee16b3cSJeremy Fitzhardinge 				     pte_fn_t fn, void *data)
2061aee16b3cSJeremy Fitzhardinge {
2062aee16b3cSJeremy Fitzhardinge 	pud_t *pud;
2063aee16b3cSJeremy Fitzhardinge 	unsigned long next;
2064aee16b3cSJeremy Fitzhardinge 	int err;
2065aee16b3cSJeremy Fitzhardinge 
2066aee16b3cSJeremy Fitzhardinge 	pud = pud_alloc(mm, pgd, addr);
2067aee16b3cSJeremy Fitzhardinge 	if (!pud)
2068aee16b3cSJeremy Fitzhardinge 		return -ENOMEM;
2069aee16b3cSJeremy Fitzhardinge 	do {
2070aee16b3cSJeremy Fitzhardinge 		next = pud_addr_end(addr, end);
2071aee16b3cSJeremy Fitzhardinge 		err = apply_to_pmd_range(mm, pud, addr, next, fn, data);
2072aee16b3cSJeremy Fitzhardinge 		if (err)
2073aee16b3cSJeremy Fitzhardinge 			break;
2074aee16b3cSJeremy Fitzhardinge 	} while (pud++, addr = next, addr != end);
2075aee16b3cSJeremy Fitzhardinge 	return err;
2076aee16b3cSJeremy Fitzhardinge }
2077aee16b3cSJeremy Fitzhardinge 
2078aee16b3cSJeremy Fitzhardinge /*
2079aee16b3cSJeremy Fitzhardinge  * Scan a region of virtual memory, filling in page tables as necessary
2080aee16b3cSJeremy Fitzhardinge  * and calling a provided function on each leaf page table.
2081aee16b3cSJeremy Fitzhardinge  */
2082aee16b3cSJeremy Fitzhardinge int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
2083aee16b3cSJeremy Fitzhardinge 			unsigned long size, pte_fn_t fn, void *data)
2084aee16b3cSJeremy Fitzhardinge {
2085aee16b3cSJeremy Fitzhardinge 	pgd_t *pgd;
2086aee16b3cSJeremy Fitzhardinge 	unsigned long next;
208757250a5bSJeremy Fitzhardinge 	unsigned long end = addr + size;
2088aee16b3cSJeremy Fitzhardinge 	int err;
2089aee16b3cSJeremy Fitzhardinge 
2090aee16b3cSJeremy Fitzhardinge 	BUG_ON(addr >= end);
2091aee16b3cSJeremy Fitzhardinge 	pgd = pgd_offset(mm, addr);
2092aee16b3cSJeremy Fitzhardinge 	do {
2093aee16b3cSJeremy Fitzhardinge 		next = pgd_addr_end(addr, end);
2094aee16b3cSJeremy Fitzhardinge 		err = apply_to_pud_range(mm, pgd, addr, next, fn, data);
2095aee16b3cSJeremy Fitzhardinge 		if (err)
2096aee16b3cSJeremy Fitzhardinge 			break;
2097aee16b3cSJeremy Fitzhardinge 	} while (pgd++, addr = next, addr != end);
209857250a5bSJeremy Fitzhardinge 
2099aee16b3cSJeremy Fitzhardinge 	return err;
2100aee16b3cSJeremy Fitzhardinge }
2101aee16b3cSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(apply_to_page_range);
2102aee16b3cSJeremy Fitzhardinge 
21031da177e4SLinus Torvalds /*
21048f4e2101SHugh Dickins  * handle_pte_fault chooses page fault handler according to an entry
21058f4e2101SHugh Dickins  * which was read non-atomically.  Before making any commitment, on
21068f4e2101SHugh Dickins  * those architectures or configurations (e.g. i386 with PAE) which
21078f4e2101SHugh Dickins  * might give a mix of unmatched parts, do_swap_page and do_file_page
21088f4e2101SHugh Dickins  * must check under lock before unmapping the pte and proceeding
21098f4e2101SHugh Dickins  * (but do_wp_page is only called after already making such a check;
21108f4e2101SHugh Dickins  * and do_anonymous_page and do_no_page can safely check later on).
21118f4e2101SHugh Dickins  */
21124c21e2f2SHugh Dickins static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
21138f4e2101SHugh Dickins 				pte_t *page_table, pte_t orig_pte)
21148f4e2101SHugh Dickins {
21158f4e2101SHugh Dickins 	int same = 1;
21168f4e2101SHugh Dickins #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
21178f4e2101SHugh Dickins 	if (sizeof(pte_t) > sizeof(unsigned long)) {
21184c21e2f2SHugh Dickins 		spinlock_t *ptl = pte_lockptr(mm, pmd);
21194c21e2f2SHugh Dickins 		spin_lock(ptl);
21208f4e2101SHugh Dickins 		same = pte_same(*page_table, orig_pte);
21214c21e2f2SHugh Dickins 		spin_unlock(ptl);
21228f4e2101SHugh Dickins 	}
21238f4e2101SHugh Dickins #endif
21248f4e2101SHugh Dickins 	pte_unmap(page_table);
21258f4e2101SHugh Dickins 	return same;
21268f4e2101SHugh Dickins }
21278f4e2101SHugh Dickins 
21289de455b2SAtsushi Nemoto static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma)
21296aab341eSLinus Torvalds {
21306aab341eSLinus Torvalds 	/*
21316aab341eSLinus Torvalds 	 * If the source page was a PFN mapping, we don't have
21326aab341eSLinus Torvalds 	 * a "struct page" for it. We do a best-effort copy by
21336aab341eSLinus Torvalds 	 * just copying from the original user address. If that
21346aab341eSLinus Torvalds 	 * fails, we just zero-fill it. Live with it.
21356aab341eSLinus Torvalds 	 */
21366aab341eSLinus Torvalds 	if (unlikely(!src)) {
21376aab341eSLinus Torvalds 		void *kaddr = kmap_atomic(dst, KM_USER0);
21385d2a2dbbSLinus Torvalds 		void __user *uaddr = (void __user *)(va & PAGE_MASK);
21395d2a2dbbSLinus Torvalds 
21405d2a2dbbSLinus Torvalds 		/*
21415d2a2dbbSLinus Torvalds 		 * This really shouldn't fail, because the page is there
21425d2a2dbbSLinus Torvalds 		 * in the page tables. But it might just be unreadable,
21435d2a2dbbSLinus Torvalds 		 * in which case we just give up and fill the result with
21445d2a2dbbSLinus Torvalds 		 * zeroes.
21455d2a2dbbSLinus Torvalds 		 */
21465d2a2dbbSLinus Torvalds 		if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE))
21473ecb01dfSJan Beulich 			clear_page(kaddr);
21486aab341eSLinus Torvalds 		kunmap_atomic(kaddr, KM_USER0);
2149c4ec7b0dSDmitriy Monakhov 		flush_dcache_page(dst);
21500ed361deSNick Piggin 	} else
21519de455b2SAtsushi Nemoto 		copy_user_highpage(dst, src, va, vma);
21526aab341eSLinus Torvalds }
21536aab341eSLinus Torvalds 
21541da177e4SLinus Torvalds /*
21551da177e4SLinus Torvalds  * This routine handles present pages, when users try to write
21561da177e4SLinus Torvalds  * to a shared page. It is done by copying the page to a new address
21571da177e4SLinus Torvalds  * and decrementing the shared-page counter for the old page.
21581da177e4SLinus Torvalds  *
21591da177e4SLinus Torvalds  * Note that this routine assumes that the protection checks have been
21601da177e4SLinus Torvalds  * done by the caller (the low-level page fault routine in most cases).
21611da177e4SLinus Torvalds  * Thus we can safely just mark it writable once we've done any necessary
21621da177e4SLinus Torvalds  * COW.
21631da177e4SLinus Torvalds  *
21641da177e4SLinus Torvalds  * We also mark the page dirty at this point even though the page will
21651da177e4SLinus Torvalds  * change only once the write actually happens. This avoids a few races,
21661da177e4SLinus Torvalds  * and potentially makes it more efficient.
21671da177e4SLinus Torvalds  *
21688f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
21698f4e2101SHugh Dickins  * but allow concurrent faults), with pte both mapped and locked.
21708f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
21711da177e4SLinus Torvalds  */
21721da177e4SLinus Torvalds static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
217365500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
21748f4e2101SHugh Dickins 		spinlock_t *ptl, pte_t orig_pte)
2175e6219ec8SNamhyung Kim 	__releases(ptl)
21761da177e4SLinus Torvalds {
2177e5bbe4dfSHugh Dickins 	struct page *old_page, *new_page;
21781da177e4SLinus Torvalds 	pte_t entry;
2179b009c024SMichel Lespinasse 	int ret = 0;
2180a200ee18SPeter Zijlstra 	int page_mkwrite = 0;
2181d08b3851SPeter Zijlstra 	struct page *dirty_page = NULL;
21821da177e4SLinus Torvalds 
21836aab341eSLinus Torvalds 	old_page = vm_normal_page(vma, address, orig_pte);
2184251b97f5SPeter Zijlstra 	if (!old_page) {
2185251b97f5SPeter Zijlstra 		/*
2186251b97f5SPeter Zijlstra 		 * VM_MIXEDMAP !pfn_valid() case
2187251b97f5SPeter Zijlstra 		 *
2188251b97f5SPeter Zijlstra 		 * We should not cow pages in a shared writeable mapping.
2189251b97f5SPeter Zijlstra 		 * Just mark the pages writable as we can't do any dirty
2190251b97f5SPeter Zijlstra 		 * accounting on raw pfn maps.
2191251b97f5SPeter Zijlstra 		 */
2192251b97f5SPeter Zijlstra 		if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
2193251b97f5SPeter Zijlstra 				     (VM_WRITE|VM_SHARED))
2194251b97f5SPeter Zijlstra 			goto reuse;
2195920fc356SHugh Dickins 		goto gotten;
2196251b97f5SPeter Zijlstra 	}
21971da177e4SLinus Torvalds 
2198d08b3851SPeter Zijlstra 	/*
2199ee6a6457SPeter Zijlstra 	 * Take out anonymous pages first, anonymous shared vmas are
2200ee6a6457SPeter Zijlstra 	 * not dirty accountable.
2201d08b3851SPeter Zijlstra 	 */
22029a840895SHugh Dickins 	if (PageAnon(old_page) && !PageKsm(old_page)) {
2203ab967d86SHugh Dickins 		if (!trylock_page(old_page)) {
2204ab967d86SHugh Dickins 			page_cache_get(old_page);
2205ab967d86SHugh Dickins 			pte_unmap_unlock(page_table, ptl);
2206ab967d86SHugh Dickins 			lock_page(old_page);
2207ab967d86SHugh Dickins 			page_table = pte_offset_map_lock(mm, pmd, address,
2208ab967d86SHugh Dickins 							 &ptl);
2209ab967d86SHugh Dickins 			if (!pte_same(*page_table, orig_pte)) {
2210ab967d86SHugh Dickins 				unlock_page(old_page);
2211ab967d86SHugh Dickins 				page_cache_release(old_page);
2212ab967d86SHugh Dickins 				goto unlock;
2213ab967d86SHugh Dickins 			}
2214ab967d86SHugh Dickins 			page_cache_release(old_page);
2215ab967d86SHugh Dickins 		}
2216b009c024SMichel Lespinasse 		if (reuse_swap_page(old_page)) {
2217c44b6743SRik van Riel 			/*
2218c44b6743SRik van Riel 			 * The page is all ours.  Move it to our anon_vma so
2219c44b6743SRik van Riel 			 * the rmap code will not search our parent or siblings.
2220c44b6743SRik van Riel 			 * Protected against the rmap code by the page lock.
2221c44b6743SRik van Riel 			 */
2222c44b6743SRik van Riel 			page_move_anon_rmap(old_page, vma, address);
2223ee6a6457SPeter Zijlstra 			unlock_page(old_page);
2224b009c024SMichel Lespinasse 			goto reuse;
2225b009c024SMichel Lespinasse 		}
2226b009c024SMichel Lespinasse 		unlock_page(old_page);
2227ee6a6457SPeter Zijlstra 	} else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
2228d08b3851SPeter Zijlstra 					(VM_WRITE|VM_SHARED))) {
2229ee6a6457SPeter Zijlstra 		/*
2230ee6a6457SPeter Zijlstra 		 * Only catch write-faults on shared writable pages,
2231ee6a6457SPeter Zijlstra 		 * read-only shared pages can get COWed by
2232ee6a6457SPeter Zijlstra 		 * get_user_pages(.write=1, .force=1).
2233ee6a6457SPeter Zijlstra 		 */
22349637a5efSDavid Howells 		if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
2235c2ec175cSNick Piggin 			struct vm_fault vmf;
2236c2ec175cSNick Piggin 			int tmp;
2237c2ec175cSNick Piggin 
2238c2ec175cSNick Piggin 			vmf.virtual_address = (void __user *)(address &
2239c2ec175cSNick Piggin 								PAGE_MASK);
2240c2ec175cSNick Piggin 			vmf.pgoff = old_page->index;
2241c2ec175cSNick Piggin 			vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
2242c2ec175cSNick Piggin 			vmf.page = old_page;
2243c2ec175cSNick Piggin 
22449637a5efSDavid Howells 			/*
22459637a5efSDavid Howells 			 * Notify the address space that the page is about to
22469637a5efSDavid Howells 			 * become writable so that it can prohibit this or wait
22479637a5efSDavid Howells 			 * for the page to get into an appropriate state.
22489637a5efSDavid Howells 			 *
22499637a5efSDavid Howells 			 * We do this without the lock held, so that it can
22509637a5efSDavid Howells 			 * sleep if it needs to.
22519637a5efSDavid Howells 			 */
22529637a5efSDavid Howells 			page_cache_get(old_page);
22539637a5efSDavid Howells 			pte_unmap_unlock(page_table, ptl);
22549637a5efSDavid Howells 
2255c2ec175cSNick Piggin 			tmp = vma->vm_ops->page_mkwrite(vma, &vmf);
2256c2ec175cSNick Piggin 			if (unlikely(tmp &
2257c2ec175cSNick Piggin 					(VM_FAULT_ERROR | VM_FAULT_NOPAGE))) {
2258c2ec175cSNick Piggin 				ret = tmp;
22599637a5efSDavid Howells 				goto unwritable_page;
2260c2ec175cSNick Piggin 			}
2261b827e496SNick Piggin 			if (unlikely(!(tmp & VM_FAULT_LOCKED))) {
2262b827e496SNick Piggin 				lock_page(old_page);
2263b827e496SNick Piggin 				if (!old_page->mapping) {
2264b827e496SNick Piggin 					ret = 0; /* retry the fault */
2265b827e496SNick Piggin 					unlock_page(old_page);
2266b827e496SNick Piggin 					goto unwritable_page;
2267b827e496SNick Piggin 				}
2268b827e496SNick Piggin 			} else
2269b827e496SNick Piggin 				VM_BUG_ON(!PageLocked(old_page));
22709637a5efSDavid Howells 
22719637a5efSDavid Howells 			/*
22729637a5efSDavid Howells 			 * Since we dropped the lock we need to revalidate
22739637a5efSDavid Howells 			 * the PTE as someone else may have changed it.  If
22749637a5efSDavid Howells 			 * they did, we just return, as we can count on the
22759637a5efSDavid Howells 			 * MMU to tell us if they didn't also make it writable.
22769637a5efSDavid Howells 			 */
22779637a5efSDavid Howells 			page_table = pte_offset_map_lock(mm, pmd, address,
22789637a5efSDavid Howells 							 &ptl);
2279b827e496SNick Piggin 			if (!pte_same(*page_table, orig_pte)) {
2280b827e496SNick Piggin 				unlock_page(old_page);
2281c3704cebSHugh Dickins 				page_cache_release(old_page);
22829637a5efSDavid Howells 				goto unlock;
2283b827e496SNick Piggin 			}
2284a200ee18SPeter Zijlstra 
2285a200ee18SPeter Zijlstra 			page_mkwrite = 1;
22869637a5efSDavid Howells 		}
2287d08b3851SPeter Zijlstra 		dirty_page = old_page;
2288d08b3851SPeter Zijlstra 		get_page(dirty_page);
22899637a5efSDavid Howells 
2290251b97f5SPeter Zijlstra reuse:
2291eca35133SBen Collins 		flush_cache_page(vma, address, pte_pfn(orig_pte));
229265500d23SHugh Dickins 		entry = pte_mkyoung(orig_pte);
229365500d23SHugh Dickins 		entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2294954ffcb3SKAMEZAWA Hiroyuki 		if (ptep_set_access_flags(vma, address, page_table, entry,1))
22954b3073e1SRussell King 			update_mmu_cache(vma, address, page_table);
229672ddc8f7SMichel Lespinasse 		pte_unmap_unlock(page_table, ptl);
229765500d23SHugh Dickins 		ret |= VM_FAULT_WRITE;
229872ddc8f7SMichel Lespinasse 
229972ddc8f7SMichel Lespinasse 		if (!dirty_page)
230072ddc8f7SMichel Lespinasse 			return ret;
230172ddc8f7SMichel Lespinasse 
230272ddc8f7SMichel Lespinasse 		/*
230372ddc8f7SMichel Lespinasse 		 * Yes, Virginia, this is actually required to prevent a race
230472ddc8f7SMichel Lespinasse 		 * with clear_page_dirty_for_io() from clearing the page dirty
230572ddc8f7SMichel Lespinasse 		 * bit after it clear all dirty ptes, but before a racing
230672ddc8f7SMichel Lespinasse 		 * do_wp_page installs a dirty pte.
230772ddc8f7SMichel Lespinasse 		 *
230872ddc8f7SMichel Lespinasse 		 * do_no_page is protected similarly.
230972ddc8f7SMichel Lespinasse 		 */
231072ddc8f7SMichel Lespinasse 		if (!page_mkwrite) {
231172ddc8f7SMichel Lespinasse 			wait_on_page_locked(dirty_page);
231272ddc8f7SMichel Lespinasse 			set_page_dirty_balance(dirty_page, page_mkwrite);
231372ddc8f7SMichel Lespinasse 		}
231472ddc8f7SMichel Lespinasse 		put_page(dirty_page);
231572ddc8f7SMichel Lespinasse 		if (page_mkwrite) {
231672ddc8f7SMichel Lespinasse 			struct address_space *mapping = dirty_page->mapping;
231772ddc8f7SMichel Lespinasse 
231872ddc8f7SMichel Lespinasse 			set_page_dirty(dirty_page);
231972ddc8f7SMichel Lespinasse 			unlock_page(dirty_page);
232072ddc8f7SMichel Lespinasse 			page_cache_release(dirty_page);
232172ddc8f7SMichel Lespinasse 			if (mapping)	{
232272ddc8f7SMichel Lespinasse 				/*
232372ddc8f7SMichel Lespinasse 				 * Some device drivers do not set page.mapping
232472ddc8f7SMichel Lespinasse 				 * but still dirty their pages
232572ddc8f7SMichel Lespinasse 				 */
232672ddc8f7SMichel Lespinasse 				balance_dirty_pages_ratelimited(mapping);
232772ddc8f7SMichel Lespinasse 			}
232872ddc8f7SMichel Lespinasse 		}
232972ddc8f7SMichel Lespinasse 
233072ddc8f7SMichel Lespinasse 		/* file_update_time outside page_lock */
233172ddc8f7SMichel Lespinasse 		if (vma->vm_file)
233272ddc8f7SMichel Lespinasse 			file_update_time(vma->vm_file);
233372ddc8f7SMichel Lespinasse 
233472ddc8f7SMichel Lespinasse 		return ret;
23351da177e4SLinus Torvalds 	}
23361da177e4SLinus Torvalds 
23371da177e4SLinus Torvalds 	/*
23381da177e4SLinus Torvalds 	 * Ok, we need to copy. Oh, well..
23391da177e4SLinus Torvalds 	 */
23401da177e4SLinus Torvalds 	page_cache_get(old_page);
2341920fc356SHugh Dickins gotten:
23428f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
23431da177e4SLinus Torvalds 
23441da177e4SLinus Torvalds 	if (unlikely(anon_vma_prepare(vma)))
234565500d23SHugh Dickins 		goto oom;
2346a13ea5b7SHugh Dickins 
234762eede62SHugh Dickins 	if (is_zero_pfn(pte_pfn(orig_pte))) {
2348a13ea5b7SHugh Dickins 		new_page = alloc_zeroed_user_highpage_movable(vma, address);
2349a13ea5b7SHugh Dickins 		if (!new_page)
2350a13ea5b7SHugh Dickins 			goto oom;
2351a13ea5b7SHugh Dickins 	} else {
2352769848c0SMel Gorman 		new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
23531da177e4SLinus Torvalds 		if (!new_page)
235465500d23SHugh Dickins 			goto oom;
2355a13ea5b7SHugh Dickins 		cow_user_page(new_page, old_page, address, vma);
2356a13ea5b7SHugh Dickins 	}
2357a13ea5b7SHugh Dickins 	__SetPageUptodate(new_page);
2358a13ea5b7SHugh Dickins 
2359b291f000SNick Piggin 	/*
2360b291f000SNick Piggin 	 * Don't let another task, with possibly unlocked vma,
2361b291f000SNick Piggin 	 * keep the mlocked page.
2362b291f000SNick Piggin 	 */
2363ab92661dSCarsten Otte 	if ((vma->vm_flags & VM_LOCKED) && old_page) {
2364b291f000SNick Piggin 		lock_page(old_page);	/* for LRU manipulation */
2365b291f000SNick Piggin 		clear_page_mlock(old_page);
2366b291f000SNick Piggin 		unlock_page(old_page);
2367b291f000SNick Piggin 	}
236865500d23SHugh Dickins 
23692c26fdd7SKAMEZAWA Hiroyuki 	if (mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))
23708a9f3ccdSBalbir Singh 		goto oom_free_new;
23718a9f3ccdSBalbir Singh 
23721da177e4SLinus Torvalds 	/*
23731da177e4SLinus Torvalds 	 * Re-check the pte - we dropped the lock
23741da177e4SLinus Torvalds 	 */
23758f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
237665500d23SHugh Dickins 	if (likely(pte_same(*page_table, orig_pte))) {
2377920fc356SHugh Dickins 		if (old_page) {
23784294621fSHugh Dickins 			if (!PageAnon(old_page)) {
237934e55232SKAMEZAWA Hiroyuki 				dec_mm_counter_fast(mm, MM_FILEPAGES);
238034e55232SKAMEZAWA Hiroyuki 				inc_mm_counter_fast(mm, MM_ANONPAGES);
23814294621fSHugh Dickins 			}
2382920fc356SHugh Dickins 		} else
238334e55232SKAMEZAWA Hiroyuki 			inc_mm_counter_fast(mm, MM_ANONPAGES);
2384eca35133SBen Collins 		flush_cache_page(vma, address, pte_pfn(orig_pte));
238565500d23SHugh Dickins 		entry = mk_pte(new_page, vma->vm_page_prot);
238665500d23SHugh Dickins 		entry = maybe_mkwrite(pte_mkdirty(entry), vma);
23874ce072f1SSiddha, Suresh B 		/*
23884ce072f1SSiddha, Suresh B 		 * Clear the pte entry and flush it first, before updating the
23894ce072f1SSiddha, Suresh B 		 * pte with the new entry. This will avoid a race condition
23904ce072f1SSiddha, Suresh B 		 * seen in the presence of one thread doing SMC and another
23914ce072f1SSiddha, Suresh B 		 * thread doing COW.
23924ce072f1SSiddha, Suresh B 		 */
2393828502d3SIzik Eidus 		ptep_clear_flush(vma, address, page_table);
23949617d95eSNick Piggin 		page_add_new_anon_rmap(new_page, vma, address);
2395828502d3SIzik Eidus 		/*
2396828502d3SIzik Eidus 		 * We call the notify macro here because, when using secondary
2397828502d3SIzik Eidus 		 * mmu page tables (such as kvm shadow page tables), we want the
2398828502d3SIzik Eidus 		 * new page to be mapped directly into the secondary page table.
2399828502d3SIzik Eidus 		 */
2400828502d3SIzik Eidus 		set_pte_at_notify(mm, address, page_table, entry);
24014b3073e1SRussell King 		update_mmu_cache(vma, address, page_table);
2402945754a1SNick Piggin 		if (old_page) {
2403945754a1SNick Piggin 			/*
2404945754a1SNick Piggin 			 * Only after switching the pte to the new page may
2405945754a1SNick Piggin 			 * we remove the mapcount here. Otherwise another
2406945754a1SNick Piggin 			 * process may come and find the rmap count decremented
2407945754a1SNick Piggin 			 * before the pte is switched to the new page, and
2408945754a1SNick Piggin 			 * "reuse" the old page writing into it while our pte
2409945754a1SNick Piggin 			 * here still points into it and can be read by other
2410945754a1SNick Piggin 			 * threads.
2411945754a1SNick Piggin 			 *
2412945754a1SNick Piggin 			 * The critical issue is to order this
2413945754a1SNick Piggin 			 * page_remove_rmap with the ptp_clear_flush above.
2414945754a1SNick Piggin 			 * Those stores are ordered by (if nothing else,)
2415945754a1SNick Piggin 			 * the barrier present in the atomic_add_negative
2416945754a1SNick Piggin 			 * in page_remove_rmap.
2417945754a1SNick Piggin 			 *
2418945754a1SNick Piggin 			 * Then the TLB flush in ptep_clear_flush ensures that
2419945754a1SNick Piggin 			 * no process can access the old page before the
2420945754a1SNick Piggin 			 * decremented mapcount is visible. And the old page
2421945754a1SNick Piggin 			 * cannot be reused until after the decremented
2422945754a1SNick Piggin 			 * mapcount is visible. So transitively, TLBs to
2423945754a1SNick Piggin 			 * old page will be flushed before it can be reused.
2424945754a1SNick Piggin 			 */
2425edc315fdSHugh Dickins 			page_remove_rmap(old_page);
2426945754a1SNick Piggin 		}
2427945754a1SNick Piggin 
24281da177e4SLinus Torvalds 		/* Free the old page.. */
24291da177e4SLinus Torvalds 		new_page = old_page;
2430f33ea7f4SNick Piggin 		ret |= VM_FAULT_WRITE;
24318a9f3ccdSBalbir Singh 	} else
24328a9f3ccdSBalbir Singh 		mem_cgroup_uncharge_page(new_page);
24338a9f3ccdSBalbir Singh 
2434920fc356SHugh Dickins 	if (new_page)
24351da177e4SLinus Torvalds 		page_cache_release(new_page);
2436920fc356SHugh Dickins 	if (old_page)
24371da177e4SLinus Torvalds 		page_cache_release(old_page);
243865500d23SHugh Dickins unlock:
24398f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
2440f33ea7f4SNick Piggin 	return ret;
24418a9f3ccdSBalbir Singh oom_free_new:
24426dbf6d3bSHugh Dickins 	page_cache_release(new_page);
244365500d23SHugh Dickins oom:
2444b827e496SNick Piggin 	if (old_page) {
2445b827e496SNick Piggin 		if (page_mkwrite) {
2446b827e496SNick Piggin 			unlock_page(old_page);
24471da177e4SLinus Torvalds 			page_cache_release(old_page);
2448b827e496SNick Piggin 		}
2449b827e496SNick Piggin 		page_cache_release(old_page);
2450b827e496SNick Piggin 	}
24511da177e4SLinus Torvalds 	return VM_FAULT_OOM;
24529637a5efSDavid Howells 
24539637a5efSDavid Howells unwritable_page:
24549637a5efSDavid Howells 	page_cache_release(old_page);
2455c2ec175cSNick Piggin 	return ret;
24561da177e4SLinus Torvalds }
24571da177e4SLinus Torvalds 
24581da177e4SLinus Torvalds /*
24591da177e4SLinus Torvalds  * Helper functions for unmap_mapping_range().
24601da177e4SLinus Torvalds  *
24611da177e4SLinus Torvalds  * __ Notes on dropping i_mmap_lock to reduce latency while unmapping __
24621da177e4SLinus Torvalds  *
24631da177e4SLinus Torvalds  * We have to restart searching the prio_tree whenever we drop the lock,
24641da177e4SLinus Torvalds  * since the iterator is only valid while the lock is held, and anyway
24651da177e4SLinus Torvalds  * a later vma might be split and reinserted earlier while lock dropped.
24661da177e4SLinus Torvalds  *
24671da177e4SLinus Torvalds  * The list of nonlinear vmas could be handled more efficiently, using
24681da177e4SLinus Torvalds  * a placeholder, but handle it in the same way until a need is shown.
24691da177e4SLinus Torvalds  * It is important to search the prio_tree before nonlinear list: a vma
24701da177e4SLinus Torvalds  * may become nonlinear and be shifted from prio_tree to nonlinear list
24711da177e4SLinus Torvalds  * while the lock is dropped; but never shifted from list to prio_tree.
24721da177e4SLinus Torvalds  *
24731da177e4SLinus Torvalds  * In order to make forward progress despite restarting the search,
24741da177e4SLinus Torvalds  * vm_truncate_count is used to mark a vma as now dealt with, so we can
24751da177e4SLinus Torvalds  * quickly skip it next time around.  Since the prio_tree search only
24761da177e4SLinus Torvalds  * shows us those vmas affected by unmapping the range in question, we
24771da177e4SLinus Torvalds  * can't efficiently keep all vmas in step with mapping->truncate_count:
24781da177e4SLinus Torvalds  * so instead reset them all whenever it wraps back to 0 (then go to 1).
24791da177e4SLinus Torvalds  * mapping->truncate_count and vma->vm_truncate_count are protected by
24801da177e4SLinus Torvalds  * i_mmap_lock.
24811da177e4SLinus Torvalds  *
24821da177e4SLinus Torvalds  * In order to make forward progress despite repeatedly restarting some
2483ee39b37bSHugh Dickins  * large vma, note the restart_addr from unmap_vmas when it breaks out:
24841da177e4SLinus Torvalds  * and restart from that address when we reach that vma again.  It might
24851da177e4SLinus Torvalds  * have been split or merged, shrunk or extended, but never shifted: so
24861da177e4SLinus Torvalds  * restart_addr remains valid so long as it remains in the vma's range.
24871da177e4SLinus Torvalds  * unmap_mapping_range forces truncate_count to leap over page-aligned
24881da177e4SLinus Torvalds  * values so we can save vma's restart_addr in its truncate_count field.
24891da177e4SLinus Torvalds  */
24901da177e4SLinus Torvalds #define is_restart_addr(truncate_count) (!((truncate_count) & ~PAGE_MASK))
24911da177e4SLinus Torvalds 
24921da177e4SLinus Torvalds static void reset_vma_truncate_counts(struct address_space *mapping)
24931da177e4SLinus Torvalds {
24941da177e4SLinus Torvalds 	struct vm_area_struct *vma;
24951da177e4SLinus Torvalds 	struct prio_tree_iter iter;
24961da177e4SLinus Torvalds 
24971da177e4SLinus Torvalds 	vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, ULONG_MAX)
24981da177e4SLinus Torvalds 		vma->vm_truncate_count = 0;
24991da177e4SLinus Torvalds 	list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
25001da177e4SLinus Torvalds 		vma->vm_truncate_count = 0;
25011da177e4SLinus Torvalds }
25021da177e4SLinus Torvalds 
25031da177e4SLinus Torvalds static int unmap_mapping_range_vma(struct vm_area_struct *vma,
25041da177e4SLinus Torvalds 		unsigned long start_addr, unsigned long end_addr,
25051da177e4SLinus Torvalds 		struct zap_details *details)
25061da177e4SLinus Torvalds {
25071da177e4SLinus Torvalds 	unsigned long restart_addr;
25081da177e4SLinus Torvalds 	int need_break;
25091da177e4SLinus Torvalds 
2510d00806b1SNick Piggin 	/*
2511d00806b1SNick Piggin 	 * files that support invalidating or truncating portions of the
2512d0217ac0SNick Piggin 	 * file from under mmaped areas must have their ->fault function
251383c54070SNick Piggin 	 * return a locked page (and set VM_FAULT_LOCKED in the return).
251483c54070SNick Piggin 	 * This provides synchronisation against concurrent unmapping here.
2515d00806b1SNick Piggin 	 */
2516d00806b1SNick Piggin 
25171da177e4SLinus Torvalds again:
25181da177e4SLinus Torvalds 	restart_addr = vma->vm_truncate_count;
25191da177e4SLinus Torvalds 	if (is_restart_addr(restart_addr) && start_addr < restart_addr) {
25201da177e4SLinus Torvalds 		start_addr = restart_addr;
25211da177e4SLinus Torvalds 		if (start_addr >= end_addr) {
25221da177e4SLinus Torvalds 			/* Top of vma has been split off since last time */
25231da177e4SLinus Torvalds 			vma->vm_truncate_count = details->truncate_count;
25241da177e4SLinus Torvalds 			return 0;
25251da177e4SLinus Torvalds 		}
25261da177e4SLinus Torvalds 	}
25271da177e4SLinus Torvalds 
2528ee39b37bSHugh Dickins 	restart_addr = zap_page_range(vma, start_addr,
2529ee39b37bSHugh Dickins 					end_addr - start_addr, details);
253095c354feSNick Piggin 	need_break = need_resched() || spin_needbreak(details->i_mmap_lock);
25311da177e4SLinus Torvalds 
2532ee39b37bSHugh Dickins 	if (restart_addr >= end_addr) {
25331da177e4SLinus Torvalds 		/* We have now completed this vma: mark it so */
25341da177e4SLinus Torvalds 		vma->vm_truncate_count = details->truncate_count;
25351da177e4SLinus Torvalds 		if (!need_break)
25361da177e4SLinus Torvalds 			return 0;
25371da177e4SLinus Torvalds 	} else {
25381da177e4SLinus Torvalds 		/* Note restart_addr in vma's truncate_count field */
2539ee39b37bSHugh Dickins 		vma->vm_truncate_count = restart_addr;
25401da177e4SLinus Torvalds 		if (!need_break)
25411da177e4SLinus Torvalds 			goto again;
25421da177e4SLinus Torvalds 	}
25431da177e4SLinus Torvalds 
25441da177e4SLinus Torvalds 	spin_unlock(details->i_mmap_lock);
25451da177e4SLinus Torvalds 	cond_resched();
25461da177e4SLinus Torvalds 	spin_lock(details->i_mmap_lock);
25471da177e4SLinus Torvalds 	return -EINTR;
25481da177e4SLinus Torvalds }
25491da177e4SLinus Torvalds 
25501da177e4SLinus Torvalds static inline void unmap_mapping_range_tree(struct prio_tree_root *root,
25511da177e4SLinus Torvalds 					    struct zap_details *details)
25521da177e4SLinus Torvalds {
25531da177e4SLinus Torvalds 	struct vm_area_struct *vma;
25541da177e4SLinus Torvalds 	struct prio_tree_iter iter;
25551da177e4SLinus Torvalds 	pgoff_t vba, vea, zba, zea;
25561da177e4SLinus Torvalds 
25571da177e4SLinus Torvalds restart:
25581da177e4SLinus Torvalds 	vma_prio_tree_foreach(vma, &iter, root,
25591da177e4SLinus Torvalds 			details->first_index, details->last_index) {
25601da177e4SLinus Torvalds 		/* Skip quickly over those we have already dealt with */
25611da177e4SLinus Torvalds 		if (vma->vm_truncate_count == details->truncate_count)
25621da177e4SLinus Torvalds 			continue;
25631da177e4SLinus Torvalds 
25641da177e4SLinus Torvalds 		vba = vma->vm_pgoff;
25651da177e4SLinus Torvalds 		vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1;
25661da177e4SLinus Torvalds 		/* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
25671da177e4SLinus Torvalds 		zba = details->first_index;
25681da177e4SLinus Torvalds 		if (zba < vba)
25691da177e4SLinus Torvalds 			zba = vba;
25701da177e4SLinus Torvalds 		zea = details->last_index;
25711da177e4SLinus Torvalds 		if (zea > vea)
25721da177e4SLinus Torvalds 			zea = vea;
25731da177e4SLinus Torvalds 
25741da177e4SLinus Torvalds 		if (unmap_mapping_range_vma(vma,
25751da177e4SLinus Torvalds 			((zba - vba) << PAGE_SHIFT) + vma->vm_start,
25761da177e4SLinus Torvalds 			((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
25771da177e4SLinus Torvalds 				details) < 0)
25781da177e4SLinus Torvalds 			goto restart;
25791da177e4SLinus Torvalds 	}
25801da177e4SLinus Torvalds }
25811da177e4SLinus Torvalds 
25821da177e4SLinus Torvalds static inline void unmap_mapping_range_list(struct list_head *head,
25831da177e4SLinus Torvalds 					    struct zap_details *details)
25841da177e4SLinus Torvalds {
25851da177e4SLinus Torvalds 	struct vm_area_struct *vma;
25861da177e4SLinus Torvalds 
25871da177e4SLinus Torvalds 	/*
25881da177e4SLinus Torvalds 	 * In nonlinear VMAs there is no correspondence between virtual address
25891da177e4SLinus Torvalds 	 * offset and file offset.  So we must perform an exhaustive search
25901da177e4SLinus Torvalds 	 * across *all* the pages in each nonlinear VMA, not just the pages
25911da177e4SLinus Torvalds 	 * whose virtual address lies outside the file truncation point.
25921da177e4SLinus Torvalds 	 */
25931da177e4SLinus Torvalds restart:
25941da177e4SLinus Torvalds 	list_for_each_entry(vma, head, shared.vm_set.list) {
25951da177e4SLinus Torvalds 		/* Skip quickly over those we have already dealt with */
25961da177e4SLinus Torvalds 		if (vma->vm_truncate_count == details->truncate_count)
25971da177e4SLinus Torvalds 			continue;
25981da177e4SLinus Torvalds 		details->nonlinear_vma = vma;
25991da177e4SLinus Torvalds 		if (unmap_mapping_range_vma(vma, vma->vm_start,
26001da177e4SLinus Torvalds 					vma->vm_end, details) < 0)
26011da177e4SLinus Torvalds 			goto restart;
26021da177e4SLinus Torvalds 	}
26031da177e4SLinus Torvalds }
26041da177e4SLinus Torvalds 
26051da177e4SLinus Torvalds /**
260672fd4a35SRobert 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.
26073d41088fSMartin Waitz  * @mapping: the address space containing mmaps to be unmapped.
26081da177e4SLinus Torvalds  * @holebegin: byte in first page to unmap, relative to the start of
26091da177e4SLinus Torvalds  * the underlying file.  This will be rounded down to a PAGE_SIZE
261025d9e2d1Snpiggin@suse.de  * boundary.  Note that this is different from truncate_pagecache(), which
26111da177e4SLinus Torvalds  * must keep the partial page.  In contrast, we must get rid of
26121da177e4SLinus Torvalds  * partial pages.
26131da177e4SLinus Torvalds  * @holelen: size of prospective hole in bytes.  This will be rounded
26141da177e4SLinus Torvalds  * up to a PAGE_SIZE boundary.  A holelen of zero truncates to the
26151da177e4SLinus Torvalds  * end of the file.
26161da177e4SLinus Torvalds  * @even_cows: 1 when truncating a file, unmap even private COWed pages;
26171da177e4SLinus Torvalds  * but 0 when invalidating pagecache, don't throw away private data.
26181da177e4SLinus Torvalds  */
26191da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping,
26201da177e4SLinus Torvalds 		loff_t const holebegin, loff_t const holelen, int even_cows)
26211da177e4SLinus Torvalds {
26221da177e4SLinus Torvalds 	struct zap_details details;
26231da177e4SLinus Torvalds 	pgoff_t hba = holebegin >> PAGE_SHIFT;
26241da177e4SLinus Torvalds 	pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
26251da177e4SLinus Torvalds 
26261da177e4SLinus Torvalds 	/* Check for overflow. */
26271da177e4SLinus Torvalds 	if (sizeof(holelen) > sizeof(hlen)) {
26281da177e4SLinus Torvalds 		long long holeend =
26291da177e4SLinus Torvalds 			(holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
26301da177e4SLinus Torvalds 		if (holeend & ~(long long)ULONG_MAX)
26311da177e4SLinus Torvalds 			hlen = ULONG_MAX - hba + 1;
26321da177e4SLinus Torvalds 	}
26331da177e4SLinus Torvalds 
26341da177e4SLinus Torvalds 	details.check_mapping = even_cows? NULL: mapping;
26351da177e4SLinus Torvalds 	details.nonlinear_vma = NULL;
26361da177e4SLinus Torvalds 	details.first_index = hba;
26371da177e4SLinus Torvalds 	details.last_index = hba + hlen - 1;
26381da177e4SLinus Torvalds 	if (details.last_index < details.first_index)
26391da177e4SLinus Torvalds 		details.last_index = ULONG_MAX;
26401da177e4SLinus Torvalds 	details.i_mmap_lock = &mapping->i_mmap_lock;
26411da177e4SLinus Torvalds 
26421da177e4SLinus Torvalds 	spin_lock(&mapping->i_mmap_lock);
26431da177e4SLinus Torvalds 
2644d00806b1SNick Piggin 	/* Protect against endless unmapping loops */
26451da177e4SLinus Torvalds 	mapping->truncate_count++;
26461da177e4SLinus Torvalds 	if (unlikely(is_restart_addr(mapping->truncate_count))) {
26471da177e4SLinus Torvalds 		if (mapping->truncate_count == 0)
26481da177e4SLinus Torvalds 			reset_vma_truncate_counts(mapping);
26491da177e4SLinus Torvalds 		mapping->truncate_count++;
26501da177e4SLinus Torvalds 	}
26511da177e4SLinus Torvalds 	details.truncate_count = mapping->truncate_count;
26521da177e4SLinus Torvalds 
26531da177e4SLinus Torvalds 	if (unlikely(!prio_tree_empty(&mapping->i_mmap)))
26541da177e4SLinus Torvalds 		unmap_mapping_range_tree(&mapping->i_mmap, &details);
26551da177e4SLinus Torvalds 	if (unlikely(!list_empty(&mapping->i_mmap_nonlinear)))
26561da177e4SLinus Torvalds 		unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details);
26571da177e4SLinus Torvalds 	spin_unlock(&mapping->i_mmap_lock);
26581da177e4SLinus Torvalds }
26591da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range);
26601da177e4SLinus Torvalds 
2661f6b3ec23SBadari Pulavarty int vmtruncate_range(struct inode *inode, loff_t offset, loff_t end)
2662f6b3ec23SBadari Pulavarty {
2663f6b3ec23SBadari Pulavarty 	struct address_space *mapping = inode->i_mapping;
2664f6b3ec23SBadari Pulavarty 
2665f6b3ec23SBadari Pulavarty 	/*
2666f6b3ec23SBadari Pulavarty 	 * If the underlying filesystem is not going to provide
2667f6b3ec23SBadari Pulavarty 	 * a way to truncate a range of blocks (punch a hole) -
2668f6b3ec23SBadari Pulavarty 	 * we should return failure right now.
2669f6b3ec23SBadari Pulavarty 	 */
2670acfa4380SAl Viro 	if (!inode->i_op->truncate_range)
2671f6b3ec23SBadari Pulavarty 		return -ENOSYS;
2672f6b3ec23SBadari Pulavarty 
26731b1dcc1bSJes Sorensen 	mutex_lock(&inode->i_mutex);
2674f6b3ec23SBadari Pulavarty 	down_write(&inode->i_alloc_sem);
2675f6b3ec23SBadari Pulavarty 	unmap_mapping_range(mapping, offset, (end - offset), 1);
2676f6b3ec23SBadari Pulavarty 	truncate_inode_pages_range(mapping, offset, end);
2677d00806b1SNick Piggin 	unmap_mapping_range(mapping, offset, (end - offset), 1);
2678f6b3ec23SBadari Pulavarty 	inode->i_op->truncate_range(inode, offset, end);
2679f6b3ec23SBadari Pulavarty 	up_write(&inode->i_alloc_sem);
26801b1dcc1bSJes Sorensen 	mutex_unlock(&inode->i_mutex);
2681f6b3ec23SBadari Pulavarty 
2682f6b3ec23SBadari Pulavarty 	return 0;
2683f6b3ec23SBadari Pulavarty }
2684f6b3ec23SBadari Pulavarty 
26851da177e4SLinus Torvalds /*
26868f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
26878f4e2101SHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
26888f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
26891da177e4SLinus Torvalds  */
269065500d23SHugh Dickins static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
269165500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
269230c9f3a9SLinus Torvalds 		unsigned int flags, pte_t orig_pte)
26931da177e4SLinus Torvalds {
26948f4e2101SHugh Dickins 	spinlock_t *ptl;
26954969c119SAndrea Arcangeli 	struct page *page, *swapcache = NULL;
269665500d23SHugh Dickins 	swp_entry_t entry;
26971da177e4SLinus Torvalds 	pte_t pte;
2698d065bd81SMichel Lespinasse 	int locked;
26997a81b88cSKAMEZAWA Hiroyuki 	struct mem_cgroup *ptr = NULL;
2700ad8c2ee8SRik van Riel 	int exclusive = 0;
270183c54070SNick Piggin 	int ret = 0;
27021da177e4SLinus Torvalds 
27034c21e2f2SHugh Dickins 	if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
27048f4e2101SHugh Dickins 		goto out;
270565500d23SHugh Dickins 
270665500d23SHugh Dickins 	entry = pte_to_swp_entry(orig_pte);
2707d1737fdbSAndi Kleen 	if (unlikely(non_swap_entry(entry))) {
27080697212aSChristoph Lameter 		if (is_migration_entry(entry)) {
27090697212aSChristoph Lameter 			migration_entry_wait(mm, pmd, address);
2710d1737fdbSAndi Kleen 		} else if (is_hwpoison_entry(entry)) {
2711d1737fdbSAndi Kleen 			ret = VM_FAULT_HWPOISON;
2712d1737fdbSAndi Kleen 		} else {
2713d1737fdbSAndi Kleen 			print_bad_pte(vma, address, orig_pte, NULL);
2714d99be1a8SHugh Dickins 			ret = VM_FAULT_SIGBUS;
2715d1737fdbSAndi Kleen 		}
27160697212aSChristoph Lameter 		goto out;
27170697212aSChristoph Lameter 	}
27180ff92245SShailabh Nagar 	delayacct_set_flag(DELAYACCT_PF_SWAPIN);
27191da177e4SLinus Torvalds 	page = lookup_swap_cache(entry);
27201da177e4SLinus Torvalds 	if (!page) {
2721a5c9b696SHugh Dickins 		grab_swap_token(mm); /* Contend for token _before_ read-in */
272202098feaSHugh Dickins 		page = swapin_readahead(entry,
272302098feaSHugh Dickins 					GFP_HIGHUSER_MOVABLE, vma, address);
27241da177e4SLinus Torvalds 		if (!page) {
27251da177e4SLinus Torvalds 			/*
27268f4e2101SHugh Dickins 			 * Back out if somebody else faulted in this pte
27278f4e2101SHugh Dickins 			 * while we released the pte lock.
27281da177e4SLinus Torvalds 			 */
27298f4e2101SHugh Dickins 			page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
27301da177e4SLinus Torvalds 			if (likely(pte_same(*page_table, orig_pte)))
27311da177e4SLinus Torvalds 				ret = VM_FAULT_OOM;
27320ff92245SShailabh Nagar 			delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
273365500d23SHugh Dickins 			goto unlock;
27341da177e4SLinus Torvalds 		}
27351da177e4SLinus Torvalds 
27361da177e4SLinus Torvalds 		/* Had to read the page from swap area: Major fault */
27371da177e4SLinus Torvalds 		ret = VM_FAULT_MAJOR;
2738f8891e5eSChristoph Lameter 		count_vm_event(PGMAJFAULT);
2739d1737fdbSAndi Kleen 	} else if (PageHWPoison(page)) {
274071f72525SWu Fengguang 		/*
274171f72525SWu Fengguang 		 * hwpoisoned dirty swapcache pages are kept for killing
274271f72525SWu Fengguang 		 * owner processes (which may be unknown at hwpoison time)
274371f72525SWu Fengguang 		 */
2744d1737fdbSAndi Kleen 		ret = VM_FAULT_HWPOISON;
2745d1737fdbSAndi Kleen 		delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
27464779cb31SAndi Kleen 		goto out_release;
27471da177e4SLinus Torvalds 	}
27481da177e4SLinus Torvalds 
2749d065bd81SMichel Lespinasse 	locked = lock_page_or_retry(page, mm, flags);
275020a1022dSBalbir Singh 	delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
2751d065bd81SMichel Lespinasse 	if (!locked) {
2752d065bd81SMichel Lespinasse 		ret |= VM_FAULT_RETRY;
2753d065bd81SMichel Lespinasse 		goto out_release;
2754d065bd81SMichel Lespinasse 	}
27551da177e4SLinus Torvalds 
27564969c119SAndrea Arcangeli 	/*
275731c4a3d3SHugh Dickins 	 * Make sure try_to_free_swap or reuse_swap_page or swapoff did not
275831c4a3d3SHugh Dickins 	 * release the swapcache from under us.  The page pin, and pte_same
275931c4a3d3SHugh Dickins 	 * test below, are not enough to exclude that.  Even if it is still
276031c4a3d3SHugh Dickins 	 * swapcache, we need to check that the page's swap has not changed.
27614969c119SAndrea Arcangeli 	 */
276231c4a3d3SHugh Dickins 	if (unlikely(!PageSwapCache(page) || page_private(page) != entry.val))
27634969c119SAndrea Arcangeli 		goto out_page;
27644969c119SAndrea Arcangeli 
27654969c119SAndrea Arcangeli 	if (ksm_might_need_to_copy(page, vma, address)) {
27664969c119SAndrea Arcangeli 		swapcache = page;
27674969c119SAndrea Arcangeli 		page = ksm_does_need_to_copy(page, vma, address);
27684969c119SAndrea Arcangeli 
27694969c119SAndrea Arcangeli 		if (unlikely(!page)) {
27705ad64688SHugh Dickins 			ret = VM_FAULT_OOM;
27714969c119SAndrea Arcangeli 			page = swapcache;
27724969c119SAndrea Arcangeli 			swapcache = NULL;
27734969c119SAndrea Arcangeli 			goto out_page;
27744969c119SAndrea Arcangeli 		}
27755ad64688SHugh Dickins 	}
27765ad64688SHugh Dickins 
27772c26fdd7SKAMEZAWA Hiroyuki 	if (mem_cgroup_try_charge_swapin(mm, page, GFP_KERNEL, &ptr)) {
2778073e587eSKAMEZAWA Hiroyuki 		ret = VM_FAULT_OOM;
2779bc43f75cSJohannes Weiner 		goto out_page;
2780073e587eSKAMEZAWA Hiroyuki 	}
2781073e587eSKAMEZAWA Hiroyuki 
27821da177e4SLinus Torvalds 	/*
27838f4e2101SHugh Dickins 	 * Back out if somebody else already faulted in this pte.
27841da177e4SLinus Torvalds 	 */
27858f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
27869e9bef07SHugh Dickins 	if (unlikely(!pte_same(*page_table, orig_pte)))
2787b8107480SKirill Korotaev 		goto out_nomap;
2788b8107480SKirill Korotaev 
2789b8107480SKirill Korotaev 	if (unlikely(!PageUptodate(page))) {
2790b8107480SKirill Korotaev 		ret = VM_FAULT_SIGBUS;
2791b8107480SKirill Korotaev 		goto out_nomap;
27921da177e4SLinus Torvalds 	}
27931da177e4SLinus Torvalds 
27948c7c6e34SKAMEZAWA Hiroyuki 	/*
27958c7c6e34SKAMEZAWA Hiroyuki 	 * The page isn't present yet, go ahead with the fault.
27968c7c6e34SKAMEZAWA Hiroyuki 	 *
27978c7c6e34SKAMEZAWA Hiroyuki 	 * Be careful about the sequence of operations here.
27988c7c6e34SKAMEZAWA Hiroyuki 	 * To get its accounting right, reuse_swap_page() must be called
27998c7c6e34SKAMEZAWA Hiroyuki 	 * while the page is counted on swap but not yet in mapcount i.e.
28008c7c6e34SKAMEZAWA Hiroyuki 	 * before page_add_anon_rmap() and swap_free(); try_to_free_swap()
28018c7c6e34SKAMEZAWA Hiroyuki 	 * must be called after the swap_free(), or it will never succeed.
280203f3c433SKAMEZAWA Hiroyuki 	 * Because delete_from_swap_page() may be called by reuse_swap_page(),
280303f3c433SKAMEZAWA Hiroyuki 	 * mem_cgroup_commit_charge_swapin() may not be able to find swp_entry
280403f3c433SKAMEZAWA Hiroyuki 	 * in page->private. In this case, a record in swap_cgroup  is silently
280503f3c433SKAMEZAWA Hiroyuki 	 * discarded at swap_free().
28068c7c6e34SKAMEZAWA Hiroyuki 	 */
28071da177e4SLinus Torvalds 
280834e55232SKAMEZAWA Hiroyuki 	inc_mm_counter_fast(mm, MM_ANONPAGES);
2809b084d435SKAMEZAWA Hiroyuki 	dec_mm_counter_fast(mm, MM_SWAPENTS);
28101da177e4SLinus Torvalds 	pte = mk_pte(page, vma->vm_page_prot);
281130c9f3a9SLinus Torvalds 	if ((flags & FAULT_FLAG_WRITE) && reuse_swap_page(page)) {
28121da177e4SLinus Torvalds 		pte = maybe_mkwrite(pte_mkdirty(pte), vma);
281330c9f3a9SLinus Torvalds 		flags &= ~FAULT_FLAG_WRITE;
28149a5b489bSAndrea Arcangeli 		ret |= VM_FAULT_WRITE;
2815ad8c2ee8SRik van Riel 		exclusive = 1;
28161da177e4SLinus Torvalds 	}
28171da177e4SLinus Torvalds 	flush_icache_page(vma, page);
28181da177e4SLinus Torvalds 	set_pte_at(mm, address, page_table, pte);
2819ad8c2ee8SRik van Riel 	do_page_add_anon_rmap(page, vma, address, exclusive);
282003f3c433SKAMEZAWA Hiroyuki 	/* It's better to call commit-charge after rmap is established */
282103f3c433SKAMEZAWA Hiroyuki 	mem_cgroup_commit_charge_swapin(page, ptr);
28221da177e4SLinus Torvalds 
2823c475a8abSHugh Dickins 	swap_free(entry);
2824b291f000SNick Piggin 	if (vm_swap_full() || (vma->vm_flags & VM_LOCKED) || PageMlocked(page))
2825a2c43eedSHugh Dickins 		try_to_free_swap(page);
2826c475a8abSHugh Dickins 	unlock_page(page);
28274969c119SAndrea Arcangeli 	if (swapcache) {
28284969c119SAndrea Arcangeli 		/*
28294969c119SAndrea Arcangeli 		 * Hold the lock to avoid the swap entry to be reused
28304969c119SAndrea Arcangeli 		 * until we take the PT lock for the pte_same() check
28314969c119SAndrea Arcangeli 		 * (to avoid false positives from pte_same). For
28324969c119SAndrea Arcangeli 		 * further safety release the lock after the swap_free
28334969c119SAndrea Arcangeli 		 * so that the swap count won't change under a
28344969c119SAndrea Arcangeli 		 * parallel locked swapcache.
28354969c119SAndrea Arcangeli 		 */
28364969c119SAndrea Arcangeli 		unlock_page(swapcache);
28374969c119SAndrea Arcangeli 		page_cache_release(swapcache);
28384969c119SAndrea Arcangeli 	}
2839c475a8abSHugh Dickins 
284030c9f3a9SLinus Torvalds 	if (flags & FAULT_FLAG_WRITE) {
284161469f1dSHugh Dickins 		ret |= do_wp_page(mm, vma, address, page_table, pmd, ptl, pte);
284261469f1dSHugh Dickins 		if (ret & VM_FAULT_ERROR)
284361469f1dSHugh Dickins 			ret &= VM_FAULT_ERROR;
28441da177e4SLinus Torvalds 		goto out;
28451da177e4SLinus Torvalds 	}
28461da177e4SLinus Torvalds 
28471da177e4SLinus Torvalds 	/* No need to invalidate - it was non-present before */
28484b3073e1SRussell King 	update_mmu_cache(vma, address, page_table);
284965500d23SHugh Dickins unlock:
28508f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
28511da177e4SLinus Torvalds out:
28521da177e4SLinus Torvalds 	return ret;
2853b8107480SKirill Korotaev out_nomap:
28547a81b88cSKAMEZAWA Hiroyuki 	mem_cgroup_cancel_charge_swapin(ptr);
28558f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
2856bc43f75cSJohannes Weiner out_page:
2857b8107480SKirill Korotaev 	unlock_page(page);
28584779cb31SAndi Kleen out_release:
2859b8107480SKirill Korotaev 	page_cache_release(page);
28604969c119SAndrea Arcangeli 	if (swapcache) {
28614969c119SAndrea Arcangeli 		unlock_page(swapcache);
28624969c119SAndrea Arcangeli 		page_cache_release(swapcache);
28634969c119SAndrea Arcangeli 	}
286465500d23SHugh Dickins 	return ret;
28651da177e4SLinus Torvalds }
28661da177e4SLinus Torvalds 
28671da177e4SLinus Torvalds /*
28688ca3eb08SLuck, Tony  * This is like a special single-page "expand_{down|up}wards()",
28698ca3eb08SLuck, Tony  * except we must first make sure that 'address{-|+}PAGE_SIZE'
2870320b2b8dSLinus Torvalds  * doesn't hit another vma.
2871320b2b8dSLinus Torvalds  */
2872320b2b8dSLinus Torvalds static inline int check_stack_guard_page(struct vm_area_struct *vma, unsigned long address)
2873320b2b8dSLinus Torvalds {
2874320b2b8dSLinus Torvalds 	address &= PAGE_MASK;
2875320b2b8dSLinus Torvalds 	if ((vma->vm_flags & VM_GROWSDOWN) && address == vma->vm_start) {
28760e8e50e2SLinus Torvalds 		struct vm_area_struct *prev = vma->vm_prev;
2877320b2b8dSLinus Torvalds 
28780e8e50e2SLinus Torvalds 		/*
28790e8e50e2SLinus Torvalds 		 * Is there a mapping abutting this one below?
28800e8e50e2SLinus Torvalds 		 *
28810e8e50e2SLinus Torvalds 		 * That's only ok if it's the same stack mapping
28820e8e50e2SLinus Torvalds 		 * that has gotten split..
28830e8e50e2SLinus Torvalds 		 */
28840e8e50e2SLinus Torvalds 		if (prev && prev->vm_end == address)
28850e8e50e2SLinus Torvalds 			return prev->vm_flags & VM_GROWSDOWN ? 0 : -ENOMEM;
28860e8e50e2SLinus Torvalds 
28870e8e50e2SLinus Torvalds 		expand_stack(vma, address - PAGE_SIZE);
2888320b2b8dSLinus Torvalds 	}
28898ca3eb08SLuck, Tony 	if ((vma->vm_flags & VM_GROWSUP) && address + PAGE_SIZE == vma->vm_end) {
28908ca3eb08SLuck, Tony 		struct vm_area_struct *next = vma->vm_next;
28918ca3eb08SLuck, Tony 
28928ca3eb08SLuck, Tony 		/* As VM_GROWSDOWN but s/below/above/ */
28938ca3eb08SLuck, Tony 		if (next && next->vm_start == address + PAGE_SIZE)
28948ca3eb08SLuck, Tony 			return next->vm_flags & VM_GROWSUP ? 0 : -ENOMEM;
28958ca3eb08SLuck, Tony 
28968ca3eb08SLuck, Tony 		expand_upwards(vma, address + PAGE_SIZE);
28978ca3eb08SLuck, Tony 	}
2898320b2b8dSLinus Torvalds 	return 0;
2899320b2b8dSLinus Torvalds }
2900320b2b8dSLinus Torvalds 
2901320b2b8dSLinus Torvalds /*
29028f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
29038f4e2101SHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
29048f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
29051da177e4SLinus Torvalds  */
290665500d23SHugh Dickins static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
290765500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
290830c9f3a9SLinus Torvalds 		unsigned int flags)
29091da177e4SLinus Torvalds {
29108f4e2101SHugh Dickins 	struct page *page;
29118f4e2101SHugh Dickins 	spinlock_t *ptl;
29121da177e4SLinus Torvalds 	pte_t entry;
29131da177e4SLinus Torvalds 
29145528f913SLinus Torvalds 	pte_unmap(page_table);
2915320b2b8dSLinus Torvalds 
291611ac5524SLinus Torvalds 	/* Check if we need to add a guard page to the stack */
291711ac5524SLinus Torvalds 	if (check_stack_guard_page(vma, address) < 0)
291811ac5524SLinus Torvalds 		return VM_FAULT_SIGBUS;
291911ac5524SLinus Torvalds 
292011ac5524SLinus Torvalds 	/* Use the zero-page for reads */
292162eede62SHugh Dickins 	if (!(flags & FAULT_FLAG_WRITE)) {
292262eede62SHugh Dickins 		entry = pte_mkspecial(pfn_pte(my_zero_pfn(address),
292362eede62SHugh Dickins 						vma->vm_page_prot));
292411ac5524SLinus Torvalds 		page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
2925a13ea5b7SHugh Dickins 		if (!pte_none(*page_table))
2926a13ea5b7SHugh Dickins 			goto unlock;
2927a13ea5b7SHugh Dickins 		goto setpte;
2928a13ea5b7SHugh Dickins 	}
2929a13ea5b7SHugh Dickins 
29301da177e4SLinus Torvalds 	/* Allocate our own private page. */
29311da177e4SLinus Torvalds 	if (unlikely(anon_vma_prepare(vma)))
293265500d23SHugh Dickins 		goto oom;
2933769848c0SMel Gorman 	page = alloc_zeroed_user_highpage_movable(vma, address);
29341da177e4SLinus Torvalds 	if (!page)
293565500d23SHugh Dickins 		goto oom;
29360ed361deSNick Piggin 	__SetPageUptodate(page);
29371da177e4SLinus Torvalds 
29382c26fdd7SKAMEZAWA Hiroyuki 	if (mem_cgroup_newpage_charge(page, mm, GFP_KERNEL))
29398a9f3ccdSBalbir Singh 		goto oom_free_page;
29408a9f3ccdSBalbir Singh 
294165500d23SHugh Dickins 	entry = mk_pte(page, vma->vm_page_prot);
29421ac0cb5dSHugh Dickins 	if (vma->vm_flags & VM_WRITE)
29431ac0cb5dSHugh Dickins 		entry = pte_mkwrite(pte_mkdirty(entry));
29448f4e2101SHugh Dickins 
29458f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
29461c2fb7a4SAndrea Arcangeli 	if (!pte_none(*page_table))
29478f4e2101SHugh Dickins 		goto release;
29489ba69294SHugh Dickins 
294934e55232SKAMEZAWA Hiroyuki 	inc_mm_counter_fast(mm, MM_ANONPAGES);
29509617d95eSNick Piggin 	page_add_new_anon_rmap(page, vma, address);
2951a13ea5b7SHugh Dickins setpte:
295265500d23SHugh Dickins 	set_pte_at(mm, address, page_table, entry);
29531da177e4SLinus Torvalds 
29541da177e4SLinus Torvalds 	/* No need to invalidate - it was non-present before */
29554b3073e1SRussell King 	update_mmu_cache(vma, address, page_table);
295665500d23SHugh Dickins unlock:
29578f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
295883c54070SNick Piggin 	return 0;
29598f4e2101SHugh Dickins release:
29608a9f3ccdSBalbir Singh 	mem_cgroup_uncharge_page(page);
29618f4e2101SHugh Dickins 	page_cache_release(page);
29628f4e2101SHugh Dickins 	goto unlock;
29638a9f3ccdSBalbir Singh oom_free_page:
29646dbf6d3bSHugh Dickins 	page_cache_release(page);
296565500d23SHugh Dickins oom:
29661da177e4SLinus Torvalds 	return VM_FAULT_OOM;
29671da177e4SLinus Torvalds }
29681da177e4SLinus Torvalds 
29691da177e4SLinus Torvalds /*
297054cb8821SNick Piggin  * __do_fault() tries to create a new page mapping. It aggressively
29711da177e4SLinus Torvalds  * tries to share with existing pages, but makes a separate copy if
297254cb8821SNick Piggin  * the FAULT_FLAG_WRITE is set in the flags parameter in order to avoid
297354cb8821SNick Piggin  * the next page fault.
29741da177e4SLinus Torvalds  *
29751da177e4SLinus Torvalds  * As this is called only for pages that do not currently exist, we
29761da177e4SLinus Torvalds  * do not need to flush old virtual caches or the TLB.
29771da177e4SLinus Torvalds  *
29788f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
297916abfa08SHugh Dickins  * but allow concurrent faults), and pte neither mapped nor locked.
29808f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
29811da177e4SLinus Torvalds  */
298254cb8821SNick Piggin static int __do_fault(struct mm_struct *mm, struct vm_area_struct *vma,
298316abfa08SHugh Dickins 		unsigned long address, pmd_t *pmd,
298454cb8821SNick Piggin 		pgoff_t pgoff, unsigned int flags, pte_t orig_pte)
29851da177e4SLinus Torvalds {
298616abfa08SHugh Dickins 	pte_t *page_table;
29878f4e2101SHugh Dickins 	spinlock_t *ptl;
2988d0217ac0SNick Piggin 	struct page *page;
29891da177e4SLinus Torvalds 	pte_t entry;
29901da177e4SLinus Torvalds 	int anon = 0;
29915b4e655eSKAMEZAWA Hiroyuki 	int charged = 0;
2992d08b3851SPeter Zijlstra 	struct page *dirty_page = NULL;
2993d0217ac0SNick Piggin 	struct vm_fault vmf;
2994d0217ac0SNick Piggin 	int ret;
2995a200ee18SPeter Zijlstra 	int page_mkwrite = 0;
299654cb8821SNick Piggin 
2997d0217ac0SNick Piggin 	vmf.virtual_address = (void __user *)(address & PAGE_MASK);
2998d0217ac0SNick Piggin 	vmf.pgoff = pgoff;
2999d0217ac0SNick Piggin 	vmf.flags = flags;
3000d0217ac0SNick Piggin 	vmf.page = NULL;
30011da177e4SLinus Torvalds 
3002d0217ac0SNick Piggin 	ret = vma->vm_ops->fault(vma, &vmf);
3003d065bd81SMichel Lespinasse 	if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE |
3004d065bd81SMichel Lespinasse 			    VM_FAULT_RETRY)))
300583c54070SNick Piggin 		return ret;
30061da177e4SLinus Torvalds 
3007a3b947eaSAndi Kleen 	if (unlikely(PageHWPoison(vmf.page))) {
3008a3b947eaSAndi Kleen 		if (ret & VM_FAULT_LOCKED)
3009a3b947eaSAndi Kleen 			unlock_page(vmf.page);
3010a3b947eaSAndi Kleen 		return VM_FAULT_HWPOISON;
3011a3b947eaSAndi Kleen 	}
3012a3b947eaSAndi Kleen 
3013d00806b1SNick Piggin 	/*
3014d0217ac0SNick Piggin 	 * For consistency in subsequent calls, make the faulted page always
3015d00806b1SNick Piggin 	 * locked.
3016d00806b1SNick Piggin 	 */
301783c54070SNick Piggin 	if (unlikely(!(ret & VM_FAULT_LOCKED)))
3018d0217ac0SNick Piggin 		lock_page(vmf.page);
301954cb8821SNick Piggin 	else
3020d0217ac0SNick Piggin 		VM_BUG_ON(!PageLocked(vmf.page));
3021d00806b1SNick Piggin 
30221da177e4SLinus Torvalds 	/*
30231da177e4SLinus Torvalds 	 * Should we do an early C-O-W break?
30241da177e4SLinus Torvalds 	 */
3025d0217ac0SNick Piggin 	page = vmf.page;
302654cb8821SNick Piggin 	if (flags & FAULT_FLAG_WRITE) {
30279637a5efSDavid Howells 		if (!(vma->vm_flags & VM_SHARED)) {
302854cb8821SNick Piggin 			anon = 1;
3029d00806b1SNick Piggin 			if (unlikely(anon_vma_prepare(vma))) {
3030d0217ac0SNick Piggin 				ret = VM_FAULT_OOM;
303154cb8821SNick Piggin 				goto out;
3032d00806b1SNick Piggin 			}
303383c54070SNick Piggin 			page = alloc_page_vma(GFP_HIGHUSER_MOVABLE,
303483c54070SNick Piggin 						vma, address);
3035d00806b1SNick Piggin 			if (!page) {
3036d0217ac0SNick Piggin 				ret = VM_FAULT_OOM;
303754cb8821SNick Piggin 				goto out;
3038d00806b1SNick Piggin 			}
30392c26fdd7SKAMEZAWA Hiroyuki 			if (mem_cgroup_newpage_charge(page, mm, GFP_KERNEL)) {
30405b4e655eSKAMEZAWA Hiroyuki 				ret = VM_FAULT_OOM;
30415b4e655eSKAMEZAWA Hiroyuki 				page_cache_release(page);
30425b4e655eSKAMEZAWA Hiroyuki 				goto out;
30435b4e655eSKAMEZAWA Hiroyuki 			}
30445b4e655eSKAMEZAWA Hiroyuki 			charged = 1;
3045b291f000SNick Piggin 			/*
3046b291f000SNick Piggin 			 * Don't let another task, with possibly unlocked vma,
3047b291f000SNick Piggin 			 * keep the mlocked page.
3048b291f000SNick Piggin 			 */
3049b291f000SNick Piggin 			if (vma->vm_flags & VM_LOCKED)
3050b291f000SNick Piggin 				clear_page_mlock(vmf.page);
3051d0217ac0SNick Piggin 			copy_user_highpage(page, vmf.page, address, vma);
30520ed361deSNick Piggin 			__SetPageUptodate(page);
30539637a5efSDavid Howells 		} else {
305454cb8821SNick Piggin 			/*
305554cb8821SNick Piggin 			 * If the page will be shareable, see if the backing
30569637a5efSDavid Howells 			 * address space wants to know that the page is about
305754cb8821SNick Piggin 			 * to become writable
305854cb8821SNick Piggin 			 */
305969676147SMark Fasheh 			if (vma->vm_ops->page_mkwrite) {
3060c2ec175cSNick Piggin 				int tmp;
3061c2ec175cSNick Piggin 
306269676147SMark Fasheh 				unlock_page(page);
3063b827e496SNick Piggin 				vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
3064c2ec175cSNick Piggin 				tmp = vma->vm_ops->page_mkwrite(vma, &vmf);
3065c2ec175cSNick Piggin 				if (unlikely(tmp &
3066c2ec175cSNick Piggin 					  (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) {
3067c2ec175cSNick Piggin 					ret = tmp;
3068b827e496SNick Piggin 					goto unwritable_page;
306969676147SMark Fasheh 				}
3070b827e496SNick Piggin 				if (unlikely(!(tmp & VM_FAULT_LOCKED))) {
307169676147SMark Fasheh 					lock_page(page);
3072d0217ac0SNick Piggin 					if (!page->mapping) {
3073b827e496SNick Piggin 						ret = 0; /* retry the fault */
3074b827e496SNick Piggin 						unlock_page(page);
3075b827e496SNick Piggin 						goto unwritable_page;
3076d0217ac0SNick Piggin 					}
3077b827e496SNick Piggin 				} else
3078b827e496SNick Piggin 					VM_BUG_ON(!PageLocked(page));
3079a200ee18SPeter Zijlstra 				page_mkwrite = 1;
30809637a5efSDavid Howells 			}
30819637a5efSDavid Howells 		}
308254cb8821SNick Piggin 
30831da177e4SLinus Torvalds 	}
30841da177e4SLinus Torvalds 
30858f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
30861da177e4SLinus Torvalds 
30871da177e4SLinus Torvalds 	/*
30881da177e4SLinus Torvalds 	 * This silly early PAGE_DIRTY setting removes a race
30891da177e4SLinus Torvalds 	 * due to the bad i386 page protection. But it's valid
30901da177e4SLinus Torvalds 	 * for other architectures too.
30911da177e4SLinus Torvalds 	 *
309230c9f3a9SLinus Torvalds 	 * Note that if FAULT_FLAG_WRITE is set, we either now have
30931da177e4SLinus Torvalds 	 * an exclusive copy of the page, or this is a shared mapping,
30941da177e4SLinus Torvalds 	 * so we can make it writable and dirty to avoid having to
30951da177e4SLinus Torvalds 	 * handle that later.
30961da177e4SLinus Torvalds 	 */
30971da177e4SLinus Torvalds 	/* Only go through if we didn't race with anybody else... */
30981c2fb7a4SAndrea Arcangeli 	if (likely(pte_same(*page_table, orig_pte))) {
3099d00806b1SNick Piggin 		flush_icache_page(vma, page);
3100d00806b1SNick Piggin 		entry = mk_pte(page, vma->vm_page_prot);
310154cb8821SNick Piggin 		if (flags & FAULT_FLAG_WRITE)
31021da177e4SLinus Torvalds 			entry = maybe_mkwrite(pte_mkdirty(entry), vma);
31031da177e4SLinus Torvalds 		if (anon) {
310434e55232SKAMEZAWA Hiroyuki 			inc_mm_counter_fast(mm, MM_ANONPAGES);
3105d00806b1SNick Piggin 			page_add_new_anon_rmap(page, vma, address);
3106f57e88a8SHugh Dickins 		} else {
310734e55232SKAMEZAWA Hiroyuki 			inc_mm_counter_fast(mm, MM_FILEPAGES);
3108d00806b1SNick Piggin 			page_add_file_rmap(page);
310954cb8821SNick Piggin 			if (flags & FAULT_FLAG_WRITE) {
3110d00806b1SNick Piggin 				dirty_page = page;
3111d08b3851SPeter Zijlstra 				get_page(dirty_page);
3112d08b3851SPeter Zijlstra 			}
31134294621fSHugh Dickins 		}
311464d6519dSLee Schermerhorn 		set_pte_at(mm, address, page_table, entry);
31151da177e4SLinus Torvalds 
3116d00806b1SNick Piggin 		/* no need to invalidate: a not-present page won't be cached */
31174b3073e1SRussell King 		update_mmu_cache(vma, address, page_table);
3118d00806b1SNick Piggin 	} else {
31195b4e655eSKAMEZAWA Hiroyuki 		if (charged)
31208a9f3ccdSBalbir Singh 			mem_cgroup_uncharge_page(page);
3121d00806b1SNick Piggin 		if (anon)
3122d00806b1SNick Piggin 			page_cache_release(page);
3123d00806b1SNick Piggin 		else
312454cb8821SNick Piggin 			anon = 1; /* no anon but release faulted_page */
3125d00806b1SNick Piggin 	}
3126d00806b1SNick Piggin 
31278f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
3128d00806b1SNick Piggin 
3129d00806b1SNick Piggin out:
3130b827e496SNick Piggin 	if (dirty_page) {
3131b827e496SNick Piggin 		struct address_space *mapping = page->mapping;
31328f7b3d15SAnton Salikhmetov 
3133b827e496SNick Piggin 		if (set_page_dirty(dirty_page))
3134b827e496SNick Piggin 			page_mkwrite = 1;
3135b827e496SNick Piggin 		unlock_page(dirty_page);
3136d08b3851SPeter Zijlstra 		put_page(dirty_page);
3137b827e496SNick Piggin 		if (page_mkwrite && mapping) {
3138b827e496SNick Piggin 			/*
3139b827e496SNick Piggin 			 * Some device drivers do not set page.mapping but still
3140b827e496SNick Piggin 			 * dirty their pages
3141b827e496SNick Piggin 			 */
3142b827e496SNick Piggin 			balance_dirty_pages_ratelimited(mapping);
3143d08b3851SPeter Zijlstra 		}
3144d00806b1SNick Piggin 
3145b827e496SNick Piggin 		/* file_update_time outside page_lock */
3146b827e496SNick Piggin 		if (vma->vm_file)
3147b827e496SNick Piggin 			file_update_time(vma->vm_file);
3148b827e496SNick Piggin 	} else {
3149b827e496SNick Piggin 		unlock_page(vmf.page);
3150b827e496SNick Piggin 		if (anon)
3151b827e496SNick Piggin 			page_cache_release(vmf.page);
3152b827e496SNick Piggin 	}
3153b827e496SNick Piggin 
3154b827e496SNick Piggin 	return ret;
3155b827e496SNick Piggin 
3156b827e496SNick Piggin unwritable_page:
3157b827e496SNick Piggin 	page_cache_release(page);
315883c54070SNick Piggin 	return ret;
315954cb8821SNick Piggin }
3160d00806b1SNick Piggin 
316154cb8821SNick Piggin static int do_linear_fault(struct mm_struct *mm, struct vm_area_struct *vma,
316254cb8821SNick Piggin 		unsigned long address, pte_t *page_table, pmd_t *pmd,
316330c9f3a9SLinus Torvalds 		unsigned int flags, pte_t orig_pte)
316454cb8821SNick Piggin {
316554cb8821SNick Piggin 	pgoff_t pgoff = (((address & PAGE_MASK)
31660da7e01fSDean Nelson 			- vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
316754cb8821SNick Piggin 
316816abfa08SHugh Dickins 	pte_unmap(page_table);
316916abfa08SHugh Dickins 	return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte);
317054cb8821SNick Piggin }
317154cb8821SNick Piggin 
3172f4b81804SJes Sorensen /*
31731da177e4SLinus Torvalds  * Fault of a previously existing named mapping. Repopulate the pte
31741da177e4SLinus Torvalds  * from the encoded file_pte if possible. This enables swappable
31751da177e4SLinus Torvalds  * nonlinear vmas.
31768f4e2101SHugh Dickins  *
31778f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
31788f4e2101SHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
31798f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
31801da177e4SLinus Torvalds  */
3181d0217ac0SNick Piggin static int do_nonlinear_fault(struct mm_struct *mm, struct vm_area_struct *vma,
318265500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
318330c9f3a9SLinus Torvalds 		unsigned int flags, pte_t orig_pte)
31841da177e4SLinus Torvalds {
318565500d23SHugh Dickins 	pgoff_t pgoff;
31861da177e4SLinus Torvalds 
318730c9f3a9SLinus Torvalds 	flags |= FAULT_FLAG_NONLINEAR;
318830c9f3a9SLinus Torvalds 
31894c21e2f2SHugh Dickins 	if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
319083c54070SNick Piggin 		return 0;
31911da177e4SLinus Torvalds 
31922509ef26SHugh Dickins 	if (unlikely(!(vma->vm_flags & VM_NONLINEAR))) {
319365500d23SHugh Dickins 		/*
319465500d23SHugh Dickins 		 * Page table corrupted: show pte and kill process.
319565500d23SHugh Dickins 		 */
31963dc14741SHugh Dickins 		print_bad_pte(vma, address, orig_pte, NULL);
3197d99be1a8SHugh Dickins 		return VM_FAULT_SIGBUS;
319865500d23SHugh Dickins 	}
319965500d23SHugh Dickins 
320065500d23SHugh Dickins 	pgoff = pte_to_pgoff(orig_pte);
320116abfa08SHugh Dickins 	return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte);
32021da177e4SLinus Torvalds }
32031da177e4SLinus Torvalds 
32041da177e4SLinus Torvalds /*
32051da177e4SLinus Torvalds  * These routines also need to handle stuff like marking pages dirty
32061da177e4SLinus Torvalds  * and/or accessed for architectures that don't do it in hardware (most
32071da177e4SLinus Torvalds  * RISC architectures).  The early dirtying is also good on the i386.
32081da177e4SLinus Torvalds  *
32091da177e4SLinus Torvalds  * There is also a hook called "update_mmu_cache()" that architectures
32101da177e4SLinus Torvalds  * with external mmu caches can use to update those (ie the Sparc or
32111da177e4SLinus Torvalds  * PowerPC hashed page tables that act as extended TLBs).
32121da177e4SLinus Torvalds  *
3213c74df32cSHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
3214c74df32cSHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
3215c74df32cSHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
32161da177e4SLinus Torvalds  */
3217*71e3aac0SAndrea Arcangeli int handle_pte_fault(struct mm_struct *mm,
32181da177e4SLinus Torvalds 		     struct vm_area_struct *vma, unsigned long address,
321930c9f3a9SLinus Torvalds 		     pte_t *pte, pmd_t *pmd, unsigned int flags)
32201da177e4SLinus Torvalds {
32211da177e4SLinus Torvalds 	pte_t entry;
32228f4e2101SHugh Dickins 	spinlock_t *ptl;
32231da177e4SLinus Torvalds 
32248dab5241SBenjamin Herrenschmidt 	entry = *pte;
32251da177e4SLinus Torvalds 	if (!pte_present(entry)) {
322665500d23SHugh Dickins 		if (pte_none(entry)) {
3227f4b81804SJes Sorensen 			if (vma->vm_ops) {
32283c18ddd1SNick Piggin 				if (likely(vma->vm_ops->fault))
322954cb8821SNick Piggin 					return do_linear_fault(mm, vma, address,
323030c9f3a9SLinus Torvalds 						pte, pmd, flags, entry);
3231f4b81804SJes Sorensen 			}
3232f4b81804SJes Sorensen 			return do_anonymous_page(mm, vma, address,
323330c9f3a9SLinus Torvalds 						 pte, pmd, flags);
323465500d23SHugh Dickins 		}
32351da177e4SLinus Torvalds 		if (pte_file(entry))
3236d0217ac0SNick Piggin 			return do_nonlinear_fault(mm, vma, address,
323730c9f3a9SLinus Torvalds 					pte, pmd, flags, entry);
323865500d23SHugh Dickins 		return do_swap_page(mm, vma, address,
323930c9f3a9SLinus Torvalds 					pte, pmd, flags, entry);
32401da177e4SLinus Torvalds 	}
32411da177e4SLinus Torvalds 
32424c21e2f2SHugh Dickins 	ptl = pte_lockptr(mm, pmd);
32438f4e2101SHugh Dickins 	spin_lock(ptl);
32448f4e2101SHugh Dickins 	if (unlikely(!pte_same(*pte, entry)))
32458f4e2101SHugh Dickins 		goto unlock;
324630c9f3a9SLinus Torvalds 	if (flags & FAULT_FLAG_WRITE) {
32471da177e4SLinus Torvalds 		if (!pte_write(entry))
32488f4e2101SHugh Dickins 			return do_wp_page(mm, vma, address,
32498f4e2101SHugh Dickins 					pte, pmd, ptl, entry);
32501da177e4SLinus Torvalds 		entry = pte_mkdirty(entry);
32511da177e4SLinus Torvalds 	}
32521da177e4SLinus Torvalds 	entry = pte_mkyoung(entry);
325330c9f3a9SLinus Torvalds 	if (ptep_set_access_flags(vma, address, pte, entry, flags & FAULT_FLAG_WRITE)) {
32544b3073e1SRussell King 		update_mmu_cache(vma, address, pte);
32551a44e149SAndrea Arcangeli 	} else {
32561a44e149SAndrea Arcangeli 		/*
32571a44e149SAndrea Arcangeli 		 * This is needed only for protection faults but the arch code
32581a44e149SAndrea Arcangeli 		 * is not yet telling us if this is a protection fault or not.
32591a44e149SAndrea Arcangeli 		 * This still avoids useless tlb flushes for .text page faults
32601a44e149SAndrea Arcangeli 		 * with threads.
32611a44e149SAndrea Arcangeli 		 */
326230c9f3a9SLinus Torvalds 		if (flags & FAULT_FLAG_WRITE)
326361c77326SShaohua Li 			flush_tlb_fix_spurious_fault(vma, address);
32641a44e149SAndrea Arcangeli 	}
32658f4e2101SHugh Dickins unlock:
32668f4e2101SHugh Dickins 	pte_unmap_unlock(pte, ptl);
326783c54070SNick Piggin 	return 0;
32681da177e4SLinus Torvalds }
32691da177e4SLinus Torvalds 
32701da177e4SLinus Torvalds /*
32711da177e4SLinus Torvalds  * By the time we get here, we already hold the mm semaphore
32721da177e4SLinus Torvalds  */
327383c54070SNick Piggin int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
3274d06063ccSLinus Torvalds 		unsigned long address, unsigned int flags)
32751da177e4SLinus Torvalds {
32761da177e4SLinus Torvalds 	pgd_t *pgd;
32771da177e4SLinus Torvalds 	pud_t *pud;
32781da177e4SLinus Torvalds 	pmd_t *pmd;
32791da177e4SLinus Torvalds 	pte_t *pte;
32801da177e4SLinus Torvalds 
32811da177e4SLinus Torvalds 	__set_current_state(TASK_RUNNING);
32821da177e4SLinus Torvalds 
3283f8891e5eSChristoph Lameter 	count_vm_event(PGFAULT);
32841da177e4SLinus Torvalds 
328534e55232SKAMEZAWA Hiroyuki 	/* do counter updates before entering really critical section. */
328634e55232SKAMEZAWA Hiroyuki 	check_sync_rss_stat(current);
328734e55232SKAMEZAWA Hiroyuki 
3288ac9b9c66SHugh Dickins 	if (unlikely(is_vm_hugetlb_page(vma)))
328930c9f3a9SLinus Torvalds 		return hugetlb_fault(mm, vma, address, flags);
32901da177e4SLinus Torvalds 
32911da177e4SLinus Torvalds 	pgd = pgd_offset(mm, address);
32921da177e4SLinus Torvalds 	pud = pud_alloc(mm, pgd, address);
32931da177e4SLinus Torvalds 	if (!pud)
3294c74df32cSHugh Dickins 		return VM_FAULT_OOM;
32951da177e4SLinus Torvalds 	pmd = pmd_alloc(mm, pud, address);
32961da177e4SLinus Torvalds 	if (!pmd)
3297c74df32cSHugh Dickins 		return VM_FAULT_OOM;
3298*71e3aac0SAndrea Arcangeli 	if (pmd_none(*pmd) && transparent_hugepage_enabled(vma)) {
3299*71e3aac0SAndrea Arcangeli 		if (!vma->vm_ops)
3300*71e3aac0SAndrea Arcangeli 			return do_huge_pmd_anonymous_page(mm, vma, address,
3301*71e3aac0SAndrea Arcangeli 							  pmd, flags);
3302*71e3aac0SAndrea Arcangeli 	} else {
3303*71e3aac0SAndrea Arcangeli 		pmd_t orig_pmd = *pmd;
3304*71e3aac0SAndrea Arcangeli 		barrier();
3305*71e3aac0SAndrea Arcangeli 		if (pmd_trans_huge(orig_pmd)) {
3306*71e3aac0SAndrea Arcangeli 			if (flags & FAULT_FLAG_WRITE &&
3307*71e3aac0SAndrea Arcangeli 			    !pmd_write(orig_pmd) &&
3308*71e3aac0SAndrea Arcangeli 			    !pmd_trans_splitting(orig_pmd))
3309*71e3aac0SAndrea Arcangeli 				return do_huge_pmd_wp_page(mm, vma, address,
3310*71e3aac0SAndrea Arcangeli 							   pmd, orig_pmd);
3311*71e3aac0SAndrea Arcangeli 			return 0;
3312*71e3aac0SAndrea Arcangeli 		}
3313*71e3aac0SAndrea Arcangeli 	}
3314*71e3aac0SAndrea Arcangeli 
3315*71e3aac0SAndrea Arcangeli 	/*
3316*71e3aac0SAndrea Arcangeli 	 * Use __pte_alloc instead of pte_alloc_map, because we can't
3317*71e3aac0SAndrea Arcangeli 	 * run pte_offset_map on the pmd, if an huge pmd could
3318*71e3aac0SAndrea Arcangeli 	 * materialize from under us from a different thread.
3319*71e3aac0SAndrea Arcangeli 	 */
3320*71e3aac0SAndrea Arcangeli 	if (unlikely(__pte_alloc(mm, vma, pmd, address)))
3321c74df32cSHugh Dickins 		return VM_FAULT_OOM;
3322*71e3aac0SAndrea Arcangeli 	/* if an huge pmd materialized from under us just retry later */
3323*71e3aac0SAndrea Arcangeli 	if (unlikely(pmd_trans_huge(*pmd)))
3324*71e3aac0SAndrea Arcangeli 		return 0;
3325*71e3aac0SAndrea Arcangeli 	/*
3326*71e3aac0SAndrea Arcangeli 	 * A regular pmd is established and it can't morph into a huge pmd
3327*71e3aac0SAndrea Arcangeli 	 * from under us anymore at this point because we hold the mmap_sem
3328*71e3aac0SAndrea Arcangeli 	 * read mode and khugepaged takes it in write mode. So now it's
3329*71e3aac0SAndrea Arcangeli 	 * safe to run pte_offset_map().
3330*71e3aac0SAndrea Arcangeli 	 */
3331*71e3aac0SAndrea Arcangeli 	pte = pte_offset_map(pmd, address);
33321da177e4SLinus Torvalds 
333330c9f3a9SLinus Torvalds 	return handle_pte_fault(mm, vma, address, pte, pmd, flags);
33341da177e4SLinus Torvalds }
33351da177e4SLinus Torvalds 
33361da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED
33371da177e4SLinus Torvalds /*
33381da177e4SLinus Torvalds  * Allocate page upper directory.
3339872fec16SHugh Dickins  * We've already handled the fast-path in-line.
33401da177e4SLinus Torvalds  */
33411bb3630eSHugh Dickins int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
33421da177e4SLinus Torvalds {
3343c74df32cSHugh Dickins 	pud_t *new = pud_alloc_one(mm, address);
3344c74df32cSHugh Dickins 	if (!new)
33451bb3630eSHugh Dickins 		return -ENOMEM;
33461da177e4SLinus Torvalds 
3347362a61adSNick Piggin 	smp_wmb(); /* See comment in __pte_alloc */
3348362a61adSNick Piggin 
3349872fec16SHugh Dickins 	spin_lock(&mm->page_table_lock);
33501bb3630eSHugh Dickins 	if (pgd_present(*pgd))		/* Another has populated it */
33515e541973SBenjamin Herrenschmidt 		pud_free(mm, new);
33521bb3630eSHugh Dickins 	else
33531da177e4SLinus Torvalds 		pgd_populate(mm, pgd, new);
3354872fec16SHugh Dickins 	spin_unlock(&mm->page_table_lock);
33551bb3630eSHugh Dickins 	return 0;
33561da177e4SLinus Torvalds }
33571da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */
33581da177e4SLinus Torvalds 
33591da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED
33601da177e4SLinus Torvalds /*
33611da177e4SLinus Torvalds  * Allocate page middle directory.
3362872fec16SHugh Dickins  * We've already handled the fast-path in-line.
33631da177e4SLinus Torvalds  */
33641bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
33651da177e4SLinus Torvalds {
3366c74df32cSHugh Dickins 	pmd_t *new = pmd_alloc_one(mm, address);
3367c74df32cSHugh Dickins 	if (!new)
33681bb3630eSHugh Dickins 		return -ENOMEM;
33691da177e4SLinus Torvalds 
3370362a61adSNick Piggin 	smp_wmb(); /* See comment in __pte_alloc */
3371362a61adSNick Piggin 
3372872fec16SHugh Dickins 	spin_lock(&mm->page_table_lock);
33731da177e4SLinus Torvalds #ifndef __ARCH_HAS_4LEVEL_HACK
33741bb3630eSHugh Dickins 	if (pud_present(*pud))		/* Another has populated it */
33755e541973SBenjamin Herrenschmidt 		pmd_free(mm, new);
33761bb3630eSHugh Dickins 	else
33771da177e4SLinus Torvalds 		pud_populate(mm, pud, new);
33781da177e4SLinus Torvalds #else
33791bb3630eSHugh Dickins 	if (pgd_present(*pud))		/* Another has populated it */
33805e541973SBenjamin Herrenschmidt 		pmd_free(mm, new);
33811bb3630eSHugh Dickins 	else
33821da177e4SLinus Torvalds 		pgd_populate(mm, pud, new);
33831da177e4SLinus Torvalds #endif /* __ARCH_HAS_4LEVEL_HACK */
3384872fec16SHugh Dickins 	spin_unlock(&mm->page_table_lock);
33851bb3630eSHugh Dickins 	return 0;
33861da177e4SLinus Torvalds }
33871da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */
33881da177e4SLinus Torvalds 
33891da177e4SLinus Torvalds int make_pages_present(unsigned long addr, unsigned long end)
33901da177e4SLinus Torvalds {
33911da177e4SLinus Torvalds 	int ret, len, write;
33921da177e4SLinus Torvalds 	struct vm_area_struct * vma;
33931da177e4SLinus Torvalds 
33941da177e4SLinus Torvalds 	vma = find_vma(current->mm, addr);
33951da177e4SLinus Torvalds 	if (!vma)
3396a477097dSKOSAKI Motohiro 		return -ENOMEM;
33975ecfda04SMichel Lespinasse 	/*
33985ecfda04SMichel Lespinasse 	 * We want to touch writable mappings with a write fault in order
33995ecfda04SMichel Lespinasse 	 * to break COW, except for shared mappings because these don't COW
34005ecfda04SMichel Lespinasse 	 * and we would not want to dirty them for nothing.
34015ecfda04SMichel Lespinasse 	 */
34025ecfda04SMichel Lespinasse 	write = (vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE;
34035bcb28b1SEric Sesterhenn 	BUG_ON(addr >= end);
34045bcb28b1SEric Sesterhenn 	BUG_ON(end > vma->vm_end);
340568e116a3SRolf Eike Beer 	len = DIV_ROUND_UP(end, PAGE_SIZE) - addr/PAGE_SIZE;
34061da177e4SLinus Torvalds 	ret = get_user_pages(current, current->mm, addr,
34071da177e4SLinus Torvalds 			len, write, 0, NULL, NULL);
3408c11d69d8SLee Schermerhorn 	if (ret < 0)
34091da177e4SLinus Torvalds 		return ret;
34109978ad58SLee Schermerhorn 	return ret == len ? 0 : -EFAULT;
34111da177e4SLinus Torvalds }
34121da177e4SLinus Torvalds 
34131da177e4SLinus Torvalds #if !defined(__HAVE_ARCH_GATE_AREA)
34141da177e4SLinus Torvalds 
34151da177e4SLinus Torvalds #if defined(AT_SYSINFO_EHDR)
34165ce7852cSAdrian Bunk static struct vm_area_struct gate_vma;
34171da177e4SLinus Torvalds 
34181da177e4SLinus Torvalds static int __init gate_vma_init(void)
34191da177e4SLinus Torvalds {
34201da177e4SLinus Torvalds 	gate_vma.vm_mm = NULL;
34211da177e4SLinus Torvalds 	gate_vma.vm_start = FIXADDR_USER_START;
34221da177e4SLinus Torvalds 	gate_vma.vm_end = FIXADDR_USER_END;
3423b6558c4aSRoland McGrath 	gate_vma.vm_flags = VM_READ | VM_MAYREAD | VM_EXEC | VM_MAYEXEC;
3424b6558c4aSRoland McGrath 	gate_vma.vm_page_prot = __P101;
3425f47aef55SRoland McGrath 	/*
3426f47aef55SRoland McGrath 	 * Make sure the vDSO gets into every core dump.
3427f47aef55SRoland McGrath 	 * Dumping its contents makes post-mortem fully interpretable later
3428f47aef55SRoland McGrath 	 * without matching up the same kernel and hardware config to see
3429f47aef55SRoland McGrath 	 * what PC values meant.
3430f47aef55SRoland McGrath 	 */
3431f47aef55SRoland McGrath 	gate_vma.vm_flags |= VM_ALWAYSDUMP;
34321da177e4SLinus Torvalds 	return 0;
34331da177e4SLinus Torvalds }
34341da177e4SLinus Torvalds __initcall(gate_vma_init);
34351da177e4SLinus Torvalds #endif
34361da177e4SLinus Torvalds 
34371da177e4SLinus Torvalds struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
34381da177e4SLinus Torvalds {
34391da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR
34401da177e4SLinus Torvalds 	return &gate_vma;
34411da177e4SLinus Torvalds #else
34421da177e4SLinus Torvalds 	return NULL;
34431da177e4SLinus Torvalds #endif
34441da177e4SLinus Torvalds }
34451da177e4SLinus Torvalds 
34461da177e4SLinus Torvalds int in_gate_area_no_task(unsigned long addr)
34471da177e4SLinus Torvalds {
34481da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR
34491da177e4SLinus Torvalds 	if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))
34501da177e4SLinus Torvalds 		return 1;
34511da177e4SLinus Torvalds #endif
34521da177e4SLinus Torvalds 	return 0;
34531da177e4SLinus Torvalds }
34541da177e4SLinus Torvalds 
34551da177e4SLinus Torvalds #endif	/* __HAVE_ARCH_GATE_AREA */
34560ec76a11SDavid Howells 
34571b36ba81SNamhyung Kim static int __follow_pte(struct mm_struct *mm, unsigned long address,
3458f8ad0f49SJohannes Weiner 		pte_t **ptepp, spinlock_t **ptlp)
3459f8ad0f49SJohannes Weiner {
3460f8ad0f49SJohannes Weiner 	pgd_t *pgd;
3461f8ad0f49SJohannes Weiner 	pud_t *pud;
3462f8ad0f49SJohannes Weiner 	pmd_t *pmd;
3463f8ad0f49SJohannes Weiner 	pte_t *ptep;
3464f8ad0f49SJohannes Weiner 
3465f8ad0f49SJohannes Weiner 	pgd = pgd_offset(mm, address);
3466f8ad0f49SJohannes Weiner 	if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
3467f8ad0f49SJohannes Weiner 		goto out;
3468f8ad0f49SJohannes Weiner 
3469f8ad0f49SJohannes Weiner 	pud = pud_offset(pgd, address);
3470f8ad0f49SJohannes Weiner 	if (pud_none(*pud) || unlikely(pud_bad(*pud)))
3471f8ad0f49SJohannes Weiner 		goto out;
3472f8ad0f49SJohannes Weiner 
3473f8ad0f49SJohannes Weiner 	pmd = pmd_offset(pud, address);
3474f8ad0f49SJohannes Weiner 	if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
3475f8ad0f49SJohannes Weiner 		goto out;
3476f8ad0f49SJohannes Weiner 
3477f8ad0f49SJohannes Weiner 	/* We cannot handle huge page PFN maps. Luckily they don't exist. */
3478f8ad0f49SJohannes Weiner 	if (pmd_huge(*pmd))
3479f8ad0f49SJohannes Weiner 		goto out;
3480f8ad0f49SJohannes Weiner 
3481f8ad0f49SJohannes Weiner 	ptep = pte_offset_map_lock(mm, pmd, address, ptlp);
3482f8ad0f49SJohannes Weiner 	if (!ptep)
3483f8ad0f49SJohannes Weiner 		goto out;
3484f8ad0f49SJohannes Weiner 	if (!pte_present(*ptep))
3485f8ad0f49SJohannes Weiner 		goto unlock;
3486f8ad0f49SJohannes Weiner 	*ptepp = ptep;
3487f8ad0f49SJohannes Weiner 	return 0;
3488f8ad0f49SJohannes Weiner unlock:
3489f8ad0f49SJohannes Weiner 	pte_unmap_unlock(ptep, *ptlp);
3490f8ad0f49SJohannes Weiner out:
3491f8ad0f49SJohannes Weiner 	return -EINVAL;
3492f8ad0f49SJohannes Weiner }
3493f8ad0f49SJohannes Weiner 
34941b36ba81SNamhyung Kim static inline int follow_pte(struct mm_struct *mm, unsigned long address,
34951b36ba81SNamhyung Kim 			     pte_t **ptepp, spinlock_t **ptlp)
34961b36ba81SNamhyung Kim {
34971b36ba81SNamhyung Kim 	int res;
34981b36ba81SNamhyung Kim 
34991b36ba81SNamhyung Kim 	/* (void) is needed to make gcc happy */
35001b36ba81SNamhyung Kim 	(void) __cond_lock(*ptlp,
35011b36ba81SNamhyung Kim 			   !(res = __follow_pte(mm, address, ptepp, ptlp)));
35021b36ba81SNamhyung Kim 	return res;
35031b36ba81SNamhyung Kim }
35041b36ba81SNamhyung Kim 
35053b6748e2SJohannes Weiner /**
35063b6748e2SJohannes Weiner  * follow_pfn - look up PFN at a user virtual address
35073b6748e2SJohannes Weiner  * @vma: memory mapping
35083b6748e2SJohannes Weiner  * @address: user virtual address
35093b6748e2SJohannes Weiner  * @pfn: location to store found PFN
35103b6748e2SJohannes Weiner  *
35113b6748e2SJohannes Weiner  * Only IO mappings and raw PFN mappings are allowed.
35123b6748e2SJohannes Weiner  *
35133b6748e2SJohannes Weiner  * Returns zero and the pfn at @pfn on success, -ve otherwise.
35143b6748e2SJohannes Weiner  */
35153b6748e2SJohannes Weiner int follow_pfn(struct vm_area_struct *vma, unsigned long address,
35163b6748e2SJohannes Weiner 	unsigned long *pfn)
35173b6748e2SJohannes Weiner {
35183b6748e2SJohannes Weiner 	int ret = -EINVAL;
35193b6748e2SJohannes Weiner 	spinlock_t *ptl;
35203b6748e2SJohannes Weiner 	pte_t *ptep;
35213b6748e2SJohannes Weiner 
35223b6748e2SJohannes Weiner 	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
35233b6748e2SJohannes Weiner 		return ret;
35243b6748e2SJohannes Weiner 
35253b6748e2SJohannes Weiner 	ret = follow_pte(vma->vm_mm, address, &ptep, &ptl);
35263b6748e2SJohannes Weiner 	if (ret)
35273b6748e2SJohannes Weiner 		return ret;
35283b6748e2SJohannes Weiner 	*pfn = pte_pfn(*ptep);
35293b6748e2SJohannes Weiner 	pte_unmap_unlock(ptep, ptl);
35303b6748e2SJohannes Weiner 	return 0;
35313b6748e2SJohannes Weiner }
35323b6748e2SJohannes Weiner EXPORT_SYMBOL(follow_pfn);
35333b6748e2SJohannes Weiner 
353428b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT
3535d87fe660Svenkatesh.pallipadi@intel.com int follow_phys(struct vm_area_struct *vma,
353628b2ee20SRik van Riel 		unsigned long address, unsigned int flags,
3537d87fe660Svenkatesh.pallipadi@intel.com 		unsigned long *prot, resource_size_t *phys)
353828b2ee20SRik van Riel {
353903668a4dSJohannes Weiner 	int ret = -EINVAL;
354028b2ee20SRik van Riel 	pte_t *ptep, pte;
354128b2ee20SRik van Riel 	spinlock_t *ptl;
354228b2ee20SRik van Riel 
3543d87fe660Svenkatesh.pallipadi@intel.com 	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
3544d87fe660Svenkatesh.pallipadi@intel.com 		goto out;
354528b2ee20SRik van Riel 
354603668a4dSJohannes Weiner 	if (follow_pte(vma->vm_mm, address, &ptep, &ptl))
3547d87fe660Svenkatesh.pallipadi@intel.com 		goto out;
354828b2ee20SRik van Riel 	pte = *ptep;
354903668a4dSJohannes Weiner 
355028b2ee20SRik van Riel 	if ((flags & FOLL_WRITE) && !pte_write(pte))
355128b2ee20SRik van Riel 		goto unlock;
355228b2ee20SRik van Riel 
355328b2ee20SRik van Riel 	*prot = pgprot_val(pte_pgprot(pte));
355403668a4dSJohannes Weiner 	*phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT;
355528b2ee20SRik van Riel 
355603668a4dSJohannes Weiner 	ret = 0;
355728b2ee20SRik van Riel unlock:
355828b2ee20SRik van Riel 	pte_unmap_unlock(ptep, ptl);
355928b2ee20SRik van Riel out:
3560d87fe660Svenkatesh.pallipadi@intel.com 	return ret;
356128b2ee20SRik van Riel }
356228b2ee20SRik van Riel 
356328b2ee20SRik van Riel int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
356428b2ee20SRik van Riel 			void *buf, int len, int write)
356528b2ee20SRik van Riel {
356628b2ee20SRik van Riel 	resource_size_t phys_addr;
356728b2ee20SRik van Riel 	unsigned long prot = 0;
35682bc7273bSKOSAKI Motohiro 	void __iomem *maddr;
356928b2ee20SRik van Riel 	int offset = addr & (PAGE_SIZE-1);
357028b2ee20SRik van Riel 
3571d87fe660Svenkatesh.pallipadi@intel.com 	if (follow_phys(vma, addr, write, &prot, &phys_addr))
357228b2ee20SRik van Riel 		return -EINVAL;
357328b2ee20SRik van Riel 
357428b2ee20SRik van Riel 	maddr = ioremap_prot(phys_addr, PAGE_SIZE, prot);
357528b2ee20SRik van Riel 	if (write)
357628b2ee20SRik van Riel 		memcpy_toio(maddr + offset, buf, len);
357728b2ee20SRik van Riel 	else
357828b2ee20SRik van Riel 		memcpy_fromio(buf, maddr + offset, len);
357928b2ee20SRik van Riel 	iounmap(maddr);
358028b2ee20SRik van Riel 
358128b2ee20SRik van Riel 	return len;
358228b2ee20SRik van Riel }
358328b2ee20SRik van Riel #endif
358428b2ee20SRik van Riel 
35850ec76a11SDavid Howells /*
35860ec76a11SDavid Howells  * Access another process' address space.
35870ec76a11SDavid Howells  * Source/target buffer must be kernel space,
35880ec76a11SDavid Howells  * Do not walk the page table directly, use get_user_pages
35890ec76a11SDavid Howells  */
35900ec76a11SDavid Howells int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
35910ec76a11SDavid Howells {
35920ec76a11SDavid Howells 	struct mm_struct *mm;
35930ec76a11SDavid Howells 	struct vm_area_struct *vma;
35940ec76a11SDavid Howells 	void *old_buf = buf;
35950ec76a11SDavid Howells 
35960ec76a11SDavid Howells 	mm = get_task_mm(tsk);
35970ec76a11SDavid Howells 	if (!mm)
35980ec76a11SDavid Howells 		return 0;
35990ec76a11SDavid Howells 
36000ec76a11SDavid Howells 	down_read(&mm->mmap_sem);
3601183ff22bSSimon Arlott 	/* ignore errors, just check how much was successfully transferred */
36020ec76a11SDavid Howells 	while (len) {
36030ec76a11SDavid Howells 		int bytes, ret, offset;
36040ec76a11SDavid Howells 		void *maddr;
360528b2ee20SRik van Riel 		struct page *page = NULL;
36060ec76a11SDavid Howells 
36070ec76a11SDavid Howells 		ret = get_user_pages(tsk, mm, addr, 1,
36080ec76a11SDavid Howells 				write, 1, &page, &vma);
360928b2ee20SRik van Riel 		if (ret <= 0) {
361028b2ee20SRik van Riel 			/*
361128b2ee20SRik van Riel 			 * Check if this is a VM_IO | VM_PFNMAP VMA, which
361228b2ee20SRik van Riel 			 * we can access using slightly different code.
361328b2ee20SRik van Riel 			 */
361428b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT
361528b2ee20SRik van Riel 			vma = find_vma(mm, addr);
361628b2ee20SRik van Riel 			if (!vma)
36170ec76a11SDavid Howells 				break;
361828b2ee20SRik van Riel 			if (vma->vm_ops && vma->vm_ops->access)
361928b2ee20SRik van Riel 				ret = vma->vm_ops->access(vma, addr, buf,
362028b2ee20SRik van Riel 							  len, write);
362128b2ee20SRik van Riel 			if (ret <= 0)
362228b2ee20SRik van Riel #endif
362328b2ee20SRik van Riel 				break;
362428b2ee20SRik van Riel 			bytes = ret;
362528b2ee20SRik van Riel 		} else {
36260ec76a11SDavid Howells 			bytes = len;
36270ec76a11SDavid Howells 			offset = addr & (PAGE_SIZE-1);
36280ec76a11SDavid Howells 			if (bytes > PAGE_SIZE-offset)
36290ec76a11SDavid Howells 				bytes = PAGE_SIZE-offset;
36300ec76a11SDavid Howells 
36310ec76a11SDavid Howells 			maddr = kmap(page);
36320ec76a11SDavid Howells 			if (write) {
36330ec76a11SDavid Howells 				copy_to_user_page(vma, page, addr,
36340ec76a11SDavid Howells 						  maddr + offset, buf, bytes);
36350ec76a11SDavid Howells 				set_page_dirty_lock(page);
36360ec76a11SDavid Howells 			} else {
36370ec76a11SDavid Howells 				copy_from_user_page(vma, page, addr,
36380ec76a11SDavid Howells 						    buf, maddr + offset, bytes);
36390ec76a11SDavid Howells 			}
36400ec76a11SDavid Howells 			kunmap(page);
36410ec76a11SDavid Howells 			page_cache_release(page);
364228b2ee20SRik van Riel 		}
36430ec76a11SDavid Howells 		len -= bytes;
36440ec76a11SDavid Howells 		buf += bytes;
36450ec76a11SDavid Howells 		addr += bytes;
36460ec76a11SDavid Howells 	}
36470ec76a11SDavid Howells 	up_read(&mm->mmap_sem);
36480ec76a11SDavid Howells 	mmput(mm);
36490ec76a11SDavid Howells 
36500ec76a11SDavid Howells 	return buf - old_buf;
36510ec76a11SDavid Howells }
365203252919SAndi Kleen 
365303252919SAndi Kleen /*
365403252919SAndi Kleen  * Print the name of a VMA.
365503252919SAndi Kleen  */
365603252919SAndi Kleen void print_vma_addr(char *prefix, unsigned long ip)
365703252919SAndi Kleen {
365803252919SAndi Kleen 	struct mm_struct *mm = current->mm;
365903252919SAndi Kleen 	struct vm_area_struct *vma;
366003252919SAndi Kleen 
3661e8bff74aSIngo Molnar 	/*
3662e8bff74aSIngo Molnar 	 * Do not print if we are in atomic
3663e8bff74aSIngo Molnar 	 * contexts (in exception stacks, etc.):
3664e8bff74aSIngo Molnar 	 */
3665e8bff74aSIngo Molnar 	if (preempt_count())
3666e8bff74aSIngo Molnar 		return;
3667e8bff74aSIngo Molnar 
366803252919SAndi Kleen 	down_read(&mm->mmap_sem);
366903252919SAndi Kleen 	vma = find_vma(mm, ip);
367003252919SAndi Kleen 	if (vma && vma->vm_file) {
367103252919SAndi Kleen 		struct file *f = vma->vm_file;
367203252919SAndi Kleen 		char *buf = (char *)__get_free_page(GFP_KERNEL);
367303252919SAndi Kleen 		if (buf) {
367403252919SAndi Kleen 			char *p, *s;
367503252919SAndi Kleen 
3676cf28b486SJan Blunck 			p = d_path(&f->f_path, buf, PAGE_SIZE);
367703252919SAndi Kleen 			if (IS_ERR(p))
367803252919SAndi Kleen 				p = "?";
367903252919SAndi Kleen 			s = strrchr(p, '/');
368003252919SAndi Kleen 			if (s)
368103252919SAndi Kleen 				p = s+1;
368203252919SAndi Kleen 			printk("%s%s[%lx+%lx]", prefix, p,
368303252919SAndi Kleen 					vma->vm_start,
368403252919SAndi Kleen 					vma->vm_end - vma->vm_start);
368503252919SAndi Kleen 			free_page((unsigned long)buf);
368603252919SAndi Kleen 		}
368703252919SAndi Kleen 	}
368803252919SAndi Kleen 	up_read(&current->mm->mmap_sem);
368903252919SAndi Kleen }
36903ee1afa3SNick Piggin 
36913ee1afa3SNick Piggin #ifdef CONFIG_PROVE_LOCKING
36923ee1afa3SNick Piggin void might_fault(void)
36933ee1afa3SNick Piggin {
369495156f00SPeter Zijlstra 	/*
369595156f00SPeter Zijlstra 	 * Some code (nfs/sunrpc) uses socket ops on kernel memory while
369695156f00SPeter Zijlstra 	 * holding the mmap_sem, this is safe because kernel memory doesn't
369795156f00SPeter Zijlstra 	 * get paged out, therefore we'll never actually fault, and the
369895156f00SPeter Zijlstra 	 * below annotations will generate false positives.
369995156f00SPeter Zijlstra 	 */
370095156f00SPeter Zijlstra 	if (segment_eq(get_fs(), KERNEL_DS))
370195156f00SPeter Zijlstra 		return;
370295156f00SPeter Zijlstra 
37033ee1afa3SNick Piggin 	might_sleep();
37043ee1afa3SNick Piggin 	/*
37053ee1afa3SNick Piggin 	 * it would be nicer only to annotate paths which are not under
37063ee1afa3SNick Piggin 	 * pagefault_disable, however that requires a larger audit and
37073ee1afa3SNick Piggin 	 * providing helpers like get_user_atomic.
37083ee1afa3SNick Piggin 	 */
37093ee1afa3SNick Piggin 	if (!in_atomic() && current->mm)
37103ee1afa3SNick Piggin 		might_lock_read(&current->mm->mmap_sem);
37113ee1afa3SNick Piggin }
37123ee1afa3SNick Piggin EXPORT_SYMBOL(might_fault);
37133ee1afa3SNick Piggin #endif
371447ad8475SAndrea Arcangeli 
371547ad8475SAndrea Arcangeli #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
371647ad8475SAndrea Arcangeli static void clear_gigantic_page(struct page *page,
371747ad8475SAndrea Arcangeli 				unsigned long addr,
371847ad8475SAndrea Arcangeli 				unsigned int pages_per_huge_page)
371947ad8475SAndrea Arcangeli {
372047ad8475SAndrea Arcangeli 	int i;
372147ad8475SAndrea Arcangeli 	struct page *p = page;
372247ad8475SAndrea Arcangeli 
372347ad8475SAndrea Arcangeli 	might_sleep();
372447ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page;
372547ad8475SAndrea Arcangeli 	     i++, p = mem_map_next(p, page, i)) {
372647ad8475SAndrea Arcangeli 		cond_resched();
372747ad8475SAndrea Arcangeli 		clear_user_highpage(p, addr + i * PAGE_SIZE);
372847ad8475SAndrea Arcangeli 	}
372947ad8475SAndrea Arcangeli }
373047ad8475SAndrea Arcangeli void clear_huge_page(struct page *page,
373147ad8475SAndrea Arcangeli 		     unsigned long addr, unsigned int pages_per_huge_page)
373247ad8475SAndrea Arcangeli {
373347ad8475SAndrea Arcangeli 	int i;
373447ad8475SAndrea Arcangeli 
373547ad8475SAndrea Arcangeli 	if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
373647ad8475SAndrea Arcangeli 		clear_gigantic_page(page, addr, pages_per_huge_page);
373747ad8475SAndrea Arcangeli 		return;
373847ad8475SAndrea Arcangeli 	}
373947ad8475SAndrea Arcangeli 
374047ad8475SAndrea Arcangeli 	might_sleep();
374147ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page; i++) {
374247ad8475SAndrea Arcangeli 		cond_resched();
374347ad8475SAndrea Arcangeli 		clear_user_highpage(page + i, addr + i * PAGE_SIZE);
374447ad8475SAndrea Arcangeli 	}
374547ad8475SAndrea Arcangeli }
374647ad8475SAndrea Arcangeli 
374747ad8475SAndrea Arcangeli static void copy_user_gigantic_page(struct page *dst, struct page *src,
374847ad8475SAndrea Arcangeli 				    unsigned long addr,
374947ad8475SAndrea Arcangeli 				    struct vm_area_struct *vma,
375047ad8475SAndrea Arcangeli 				    unsigned int pages_per_huge_page)
375147ad8475SAndrea Arcangeli {
375247ad8475SAndrea Arcangeli 	int i;
375347ad8475SAndrea Arcangeli 	struct page *dst_base = dst;
375447ad8475SAndrea Arcangeli 	struct page *src_base = src;
375547ad8475SAndrea Arcangeli 
375647ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page; ) {
375747ad8475SAndrea Arcangeli 		cond_resched();
375847ad8475SAndrea Arcangeli 		copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma);
375947ad8475SAndrea Arcangeli 
376047ad8475SAndrea Arcangeli 		i++;
376147ad8475SAndrea Arcangeli 		dst = mem_map_next(dst, dst_base, i);
376247ad8475SAndrea Arcangeli 		src = mem_map_next(src, src_base, i);
376347ad8475SAndrea Arcangeli 	}
376447ad8475SAndrea Arcangeli }
376547ad8475SAndrea Arcangeli 
376647ad8475SAndrea Arcangeli void copy_user_huge_page(struct page *dst, struct page *src,
376747ad8475SAndrea Arcangeli 			 unsigned long addr, struct vm_area_struct *vma,
376847ad8475SAndrea Arcangeli 			 unsigned int pages_per_huge_page)
376947ad8475SAndrea Arcangeli {
377047ad8475SAndrea Arcangeli 	int i;
377147ad8475SAndrea Arcangeli 
377247ad8475SAndrea Arcangeli 	if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
377347ad8475SAndrea Arcangeli 		copy_user_gigantic_page(dst, src, addr, vma,
377447ad8475SAndrea Arcangeli 					pages_per_huge_page);
377547ad8475SAndrea Arcangeli 		return;
377647ad8475SAndrea Arcangeli 	}
377747ad8475SAndrea Arcangeli 
377847ad8475SAndrea Arcangeli 	might_sleep();
377947ad8475SAndrea Arcangeli 	for (i = 0; i < pages_per_huge_page; i++) {
378047ad8475SAndrea Arcangeli 		cond_resched();
378147ad8475SAndrea Arcangeli 		copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
378247ad8475SAndrea Arcangeli 	}
378347ad8475SAndrea Arcangeli }
378447ad8475SAndrea Arcangeli #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */
3785