xref: /linux/mm/memory.c (revision 6606c3e0da5360799e07ae24b05080cc85c68e72)
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>
481da177e4SLinus Torvalds #include <linux/rmap.h>
491da177e4SLinus Torvalds #include <linux/module.h>
500ff92245SShailabh Nagar #include <linux/delayacct.h>
511da177e4SLinus Torvalds #include <linux/init.h>
52edc79b2aSPeter Zijlstra #include <linux/writeback.h>
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds #include <asm/pgalloc.h>
551da177e4SLinus Torvalds #include <asm/uaccess.h>
561da177e4SLinus Torvalds #include <asm/tlb.h>
571da177e4SLinus Torvalds #include <asm/tlbflush.h>
581da177e4SLinus Torvalds #include <asm/pgtable.h>
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds #include <linux/swapops.h>
611da177e4SLinus Torvalds #include <linux/elf.h>
621da177e4SLinus Torvalds 
63d41dee36SAndy Whitcroft #ifndef CONFIG_NEED_MULTIPLE_NODES
641da177e4SLinus Torvalds /* use the per-pgdat data instead for discontigmem - mbligh */
651da177e4SLinus Torvalds unsigned long max_mapnr;
661da177e4SLinus Torvalds struct page *mem_map;
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds EXPORT_SYMBOL(max_mapnr);
691da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map);
701da177e4SLinus Torvalds #endif
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds unsigned long num_physpages;
731da177e4SLinus Torvalds /*
741da177e4SLinus Torvalds  * A number of key systems in x86 including ioremap() rely on the assumption
751da177e4SLinus Torvalds  * that high_memory defines the upper bound on direct map memory, then end
761da177e4SLinus Torvalds  * of ZONE_NORMAL.  Under CONFIG_DISCONTIG this means that max_low_pfn and
771da177e4SLinus Torvalds  * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL
781da177e4SLinus Torvalds  * and ZONE_HIGHMEM.
791da177e4SLinus Torvalds  */
801da177e4SLinus Torvalds void * high_memory;
811da177e4SLinus Torvalds unsigned long vmalloc_earlyreserve;
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds EXPORT_SYMBOL(num_physpages);
841da177e4SLinus Torvalds EXPORT_SYMBOL(high_memory);
851da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc_earlyreserve);
861da177e4SLinus Torvalds 
87a62eaf15SAndi Kleen int randomize_va_space __read_mostly = 1;
88a62eaf15SAndi Kleen 
89a62eaf15SAndi Kleen static int __init disable_randmaps(char *s)
90a62eaf15SAndi Kleen {
91a62eaf15SAndi Kleen 	randomize_va_space = 0;
929b41046cSOGAWA Hirofumi 	return 1;
93a62eaf15SAndi Kleen }
94a62eaf15SAndi Kleen __setup("norandmaps", disable_randmaps);
95a62eaf15SAndi Kleen 
96a62eaf15SAndi Kleen 
971da177e4SLinus Torvalds /*
981da177e4SLinus Torvalds  * If a p?d_bad entry is found while walking page tables, report
991da177e4SLinus Torvalds  * the error, before resetting entry to p?d_none.  Usually (but
1001da177e4SLinus Torvalds  * very seldom) called out from the p?d_none_or_clear_bad macros.
1011da177e4SLinus Torvalds  */
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds void pgd_clear_bad(pgd_t *pgd)
1041da177e4SLinus Torvalds {
1051da177e4SLinus Torvalds 	pgd_ERROR(*pgd);
1061da177e4SLinus Torvalds 	pgd_clear(pgd);
1071da177e4SLinus Torvalds }
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds void pud_clear_bad(pud_t *pud)
1101da177e4SLinus Torvalds {
1111da177e4SLinus Torvalds 	pud_ERROR(*pud);
1121da177e4SLinus Torvalds 	pud_clear(pud);
1131da177e4SLinus Torvalds }
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds void pmd_clear_bad(pmd_t *pmd)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	pmd_ERROR(*pmd);
1181da177e4SLinus Torvalds 	pmd_clear(pmd);
1191da177e4SLinus Torvalds }
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds /*
1221da177e4SLinus Torvalds  * Note: this doesn't free the actual pages themselves. That
1231da177e4SLinus Torvalds  * has been handled earlier when unmapping all the memory regions.
1241da177e4SLinus Torvalds  */
125e0da382cSHugh Dickins static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd)
1261da177e4SLinus Torvalds {
1271da177e4SLinus Torvalds 	struct page *page = pmd_page(*pmd);
1281da177e4SLinus Torvalds 	pmd_clear(pmd);
1294c21e2f2SHugh Dickins 	pte_lock_deinit(page);
130e0da382cSHugh Dickins 	pte_free_tlb(tlb, page);
131df849a15SChristoph Lameter 	dec_zone_page_state(page, NR_PAGETABLE);
1321da177e4SLinus Torvalds 	tlb->mm->nr_ptes--;
1331da177e4SLinus Torvalds }
1341da177e4SLinus Torvalds 
135e0da382cSHugh Dickins static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
136e0da382cSHugh Dickins 				unsigned long addr, unsigned long end,
137e0da382cSHugh Dickins 				unsigned long floor, unsigned long ceiling)
1381da177e4SLinus Torvalds {
1391da177e4SLinus Torvalds 	pmd_t *pmd;
1401da177e4SLinus Torvalds 	unsigned long next;
141e0da382cSHugh Dickins 	unsigned long start;
1421da177e4SLinus Torvalds 
143e0da382cSHugh Dickins 	start = addr;
1441da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
1451da177e4SLinus Torvalds 	do {
1461da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
1471da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(pmd))
1481da177e4SLinus Torvalds 			continue;
149e0da382cSHugh Dickins 		free_pte_range(tlb, pmd);
1501da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
1511da177e4SLinus Torvalds 
152e0da382cSHugh Dickins 	start &= PUD_MASK;
153e0da382cSHugh Dickins 	if (start < floor)
154e0da382cSHugh Dickins 		return;
155e0da382cSHugh Dickins 	if (ceiling) {
156e0da382cSHugh Dickins 		ceiling &= PUD_MASK;
157e0da382cSHugh Dickins 		if (!ceiling)
158e0da382cSHugh Dickins 			return;
1591da177e4SLinus Torvalds 	}
160e0da382cSHugh Dickins 	if (end - 1 > ceiling - 1)
161e0da382cSHugh Dickins 		return;
162e0da382cSHugh Dickins 
163e0da382cSHugh Dickins 	pmd = pmd_offset(pud, start);
164e0da382cSHugh Dickins 	pud_clear(pud);
165e0da382cSHugh Dickins 	pmd_free_tlb(tlb, pmd);
1661da177e4SLinus Torvalds }
1671da177e4SLinus Torvalds 
168e0da382cSHugh Dickins static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
169e0da382cSHugh Dickins 				unsigned long addr, unsigned long end,
170e0da382cSHugh Dickins 				unsigned long floor, unsigned long ceiling)
1711da177e4SLinus Torvalds {
1721da177e4SLinus Torvalds 	pud_t *pud;
1731da177e4SLinus Torvalds 	unsigned long next;
174e0da382cSHugh Dickins 	unsigned long start;
1751da177e4SLinus Torvalds 
176e0da382cSHugh Dickins 	start = addr;
1771da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
1781da177e4SLinus Torvalds 	do {
1791da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
1801da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
1811da177e4SLinus Torvalds 			continue;
182e0da382cSHugh Dickins 		free_pmd_range(tlb, pud, addr, next, floor, ceiling);
1831da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
1841da177e4SLinus Torvalds 
185e0da382cSHugh Dickins 	start &= PGDIR_MASK;
186e0da382cSHugh Dickins 	if (start < floor)
187e0da382cSHugh Dickins 		return;
188e0da382cSHugh Dickins 	if (ceiling) {
189e0da382cSHugh Dickins 		ceiling &= PGDIR_MASK;
190e0da382cSHugh Dickins 		if (!ceiling)
191e0da382cSHugh Dickins 			return;
1921da177e4SLinus Torvalds 	}
193e0da382cSHugh Dickins 	if (end - 1 > ceiling - 1)
194e0da382cSHugh Dickins 		return;
195e0da382cSHugh Dickins 
196e0da382cSHugh Dickins 	pud = pud_offset(pgd, start);
197e0da382cSHugh Dickins 	pgd_clear(pgd);
198e0da382cSHugh Dickins 	pud_free_tlb(tlb, pud);
1991da177e4SLinus Torvalds }
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds /*
202e0da382cSHugh Dickins  * This function frees user-level page tables of a process.
203e0da382cSHugh Dickins  *
2041da177e4SLinus Torvalds  * Must be called with pagetable lock held.
2051da177e4SLinus Torvalds  */
2063bf5ee95SHugh Dickins void free_pgd_range(struct mmu_gather **tlb,
207e0da382cSHugh Dickins 			unsigned long addr, unsigned long end,
208e0da382cSHugh Dickins 			unsigned long floor, unsigned long ceiling)
2091da177e4SLinus Torvalds {
2101da177e4SLinus Torvalds 	pgd_t *pgd;
2111da177e4SLinus Torvalds 	unsigned long next;
212e0da382cSHugh Dickins 	unsigned long start;
2131da177e4SLinus Torvalds 
214e0da382cSHugh Dickins 	/*
215e0da382cSHugh Dickins 	 * The next few lines have given us lots of grief...
216e0da382cSHugh Dickins 	 *
217e0da382cSHugh Dickins 	 * Why are we testing PMD* at this top level?  Because often
218e0da382cSHugh Dickins 	 * there will be no work to do at all, and we'd prefer not to
219e0da382cSHugh Dickins 	 * go all the way down to the bottom just to discover that.
220e0da382cSHugh Dickins 	 *
221e0da382cSHugh Dickins 	 * Why all these "- 1"s?  Because 0 represents both the bottom
222e0da382cSHugh Dickins 	 * of the address space and the top of it (using -1 for the
223e0da382cSHugh Dickins 	 * top wouldn't help much: the masks would do the wrong thing).
224e0da382cSHugh Dickins 	 * The rule is that addr 0 and floor 0 refer to the bottom of
225e0da382cSHugh Dickins 	 * the address space, but end 0 and ceiling 0 refer to the top
226e0da382cSHugh Dickins 	 * Comparisons need to use "end - 1" and "ceiling - 1" (though
227e0da382cSHugh Dickins 	 * that end 0 case should be mythical).
228e0da382cSHugh Dickins 	 *
229e0da382cSHugh Dickins 	 * Wherever addr is brought up or ceiling brought down, we must
230e0da382cSHugh Dickins 	 * be careful to reject "the opposite 0" before it confuses the
231e0da382cSHugh Dickins 	 * subsequent tests.  But what about where end is brought down
232e0da382cSHugh Dickins 	 * by PMD_SIZE below? no, end can't go down to 0 there.
233e0da382cSHugh Dickins 	 *
234e0da382cSHugh Dickins 	 * Whereas we round start (addr) and ceiling down, by different
235e0da382cSHugh Dickins 	 * masks at different levels, in order to test whether a table
236e0da382cSHugh Dickins 	 * now has no other vmas using it, so can be freed, we don't
237e0da382cSHugh Dickins 	 * bother to round floor or end up - the tests don't need that.
238e0da382cSHugh Dickins 	 */
239e0da382cSHugh Dickins 
240e0da382cSHugh Dickins 	addr &= PMD_MASK;
241e0da382cSHugh Dickins 	if (addr < floor) {
242e0da382cSHugh Dickins 		addr += PMD_SIZE;
243e0da382cSHugh Dickins 		if (!addr)
244e0da382cSHugh Dickins 			return;
245e0da382cSHugh Dickins 	}
246e0da382cSHugh Dickins 	if (ceiling) {
247e0da382cSHugh Dickins 		ceiling &= PMD_MASK;
248e0da382cSHugh Dickins 		if (!ceiling)
249e0da382cSHugh Dickins 			return;
250e0da382cSHugh Dickins 	}
251e0da382cSHugh Dickins 	if (end - 1 > ceiling - 1)
252e0da382cSHugh Dickins 		end -= PMD_SIZE;
253e0da382cSHugh Dickins 	if (addr > end - 1)
254e0da382cSHugh Dickins 		return;
255e0da382cSHugh Dickins 
256e0da382cSHugh Dickins 	start = addr;
2573bf5ee95SHugh Dickins 	pgd = pgd_offset((*tlb)->mm, addr);
2581da177e4SLinus Torvalds 	do {
2591da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
2601da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
2611da177e4SLinus Torvalds 			continue;
2623bf5ee95SHugh Dickins 		free_pud_range(*tlb, pgd, addr, next, floor, ceiling);
2631da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
264e0da382cSHugh Dickins 
2654d6ddfa9SHugh Dickins 	if (!(*tlb)->fullmm)
2663bf5ee95SHugh Dickins 		flush_tlb_pgtables((*tlb)->mm, start, end);
267e0da382cSHugh Dickins }
268e0da382cSHugh Dickins 
269e0da382cSHugh Dickins void free_pgtables(struct mmu_gather **tlb, struct vm_area_struct *vma,
270e0da382cSHugh Dickins 		unsigned long floor, unsigned long ceiling)
271e0da382cSHugh Dickins {
272e0da382cSHugh Dickins 	while (vma) {
273e0da382cSHugh Dickins 		struct vm_area_struct *next = vma->vm_next;
274e0da382cSHugh Dickins 		unsigned long addr = vma->vm_start;
275e0da382cSHugh Dickins 
2768f4f8c16SHugh Dickins 		/*
2778f4f8c16SHugh Dickins 		 * Hide vma from rmap and vmtruncate before freeing pgtables
2788f4f8c16SHugh Dickins 		 */
2798f4f8c16SHugh Dickins 		anon_vma_unlink(vma);
2808f4f8c16SHugh Dickins 		unlink_file_vma(vma);
2818f4f8c16SHugh Dickins 
2829da61aefSDavid Gibson 		if (is_vm_hugetlb_page(vma)) {
2833bf5ee95SHugh Dickins 			hugetlb_free_pgd_range(tlb, addr, vma->vm_end,
2843bf5ee95SHugh Dickins 				floor, next? next->vm_start: ceiling);
2853bf5ee95SHugh Dickins 		} else {
2863bf5ee95SHugh Dickins 			/*
2873bf5ee95SHugh Dickins 			 * Optimization: gather nearby vmas into one call down
2883bf5ee95SHugh Dickins 			 */
2893bf5ee95SHugh Dickins 			while (next && next->vm_start <= vma->vm_end + PMD_SIZE
2904866920bSDavid Gibson 			       && !is_vm_hugetlb_page(next)) {
291e0da382cSHugh Dickins 				vma = next;
292e0da382cSHugh Dickins 				next = vma->vm_next;
2938f4f8c16SHugh Dickins 				anon_vma_unlink(vma);
2948f4f8c16SHugh Dickins 				unlink_file_vma(vma);
295e0da382cSHugh Dickins 			}
2963bf5ee95SHugh Dickins 			free_pgd_range(tlb, addr, vma->vm_end,
297e0da382cSHugh Dickins 				floor, next? next->vm_start: ceiling);
2983bf5ee95SHugh Dickins 		}
299e0da382cSHugh Dickins 		vma = next;
300e0da382cSHugh Dickins 	}
3011da177e4SLinus Torvalds }
3021da177e4SLinus Torvalds 
3031bb3630eSHugh Dickins int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
3041da177e4SLinus Torvalds {
305c74df32cSHugh Dickins 	struct page *new = pte_alloc_one(mm, address);
3061da177e4SLinus Torvalds 	if (!new)
3071bb3630eSHugh Dickins 		return -ENOMEM;
3081bb3630eSHugh Dickins 
3094c21e2f2SHugh Dickins 	pte_lock_init(new);
310c74df32cSHugh Dickins 	spin_lock(&mm->page_table_lock);
3114c21e2f2SHugh Dickins 	if (pmd_present(*pmd)) {	/* Another has populated it */
3124c21e2f2SHugh Dickins 		pte_lock_deinit(new);
3131da177e4SLinus Torvalds 		pte_free(new);
3144c21e2f2SHugh Dickins 	} else {
3151da177e4SLinus Torvalds 		mm->nr_ptes++;
316df849a15SChristoph Lameter 		inc_zone_page_state(new, NR_PAGETABLE);
3171da177e4SLinus Torvalds 		pmd_populate(mm, pmd, new);
3181da177e4SLinus Torvalds 	}
319c74df32cSHugh Dickins 	spin_unlock(&mm->page_table_lock);
3201bb3630eSHugh Dickins 	return 0;
3211da177e4SLinus Torvalds }
3221da177e4SLinus Torvalds 
3231bb3630eSHugh Dickins int __pte_alloc_kernel(pmd_t *pmd, unsigned long address)
3241da177e4SLinus Torvalds {
3251bb3630eSHugh Dickins 	pte_t *new = pte_alloc_one_kernel(&init_mm, address);
3261da177e4SLinus Torvalds 	if (!new)
3271bb3630eSHugh Dickins 		return -ENOMEM;
3281da177e4SLinus Torvalds 
329872fec16SHugh Dickins 	spin_lock(&init_mm.page_table_lock);
3301bb3630eSHugh Dickins 	if (pmd_present(*pmd))		/* Another has populated it */
3311da177e4SLinus Torvalds 		pte_free_kernel(new);
332872fec16SHugh Dickins 	else
333872fec16SHugh Dickins 		pmd_populate_kernel(&init_mm, pmd, new);
334872fec16SHugh Dickins 	spin_unlock(&init_mm.page_table_lock);
3351bb3630eSHugh Dickins 	return 0;
3361da177e4SLinus Torvalds }
3371da177e4SLinus Torvalds 
338ae859762SHugh Dickins static inline void add_mm_rss(struct mm_struct *mm, int file_rss, int anon_rss)
339ae859762SHugh Dickins {
340ae859762SHugh Dickins 	if (file_rss)
341ae859762SHugh Dickins 		add_mm_counter(mm, file_rss, file_rss);
342ae859762SHugh Dickins 	if (anon_rss)
343ae859762SHugh Dickins 		add_mm_counter(mm, anon_rss, anon_rss);
344ae859762SHugh Dickins }
345ae859762SHugh Dickins 
3461da177e4SLinus Torvalds /*
3476aab341eSLinus Torvalds  * This function is called to print an error when a bad pte
3486aab341eSLinus Torvalds  * is found. For example, we might have a PFN-mapped pte in
3496aab341eSLinus Torvalds  * a region that doesn't allow it.
350b5810039SNick Piggin  *
351b5810039SNick Piggin  * The calling function must still handle the error.
352b5810039SNick Piggin  */
353b5810039SNick Piggin void print_bad_pte(struct vm_area_struct *vma, pte_t pte, unsigned long vaddr)
354b5810039SNick Piggin {
355b5810039SNick Piggin 	printk(KERN_ERR "Bad pte = %08llx, process = %s, "
356b5810039SNick Piggin 			"vm_flags = %lx, vaddr = %lx\n",
357b5810039SNick Piggin 		(long long)pte_val(pte),
358b5810039SNick Piggin 		(vma->vm_mm == current->mm ? current->comm : "???"),
359b5810039SNick Piggin 		vma->vm_flags, vaddr);
360b5810039SNick Piggin 	dump_stack();
361b5810039SNick Piggin }
362b5810039SNick Piggin 
36367121172SLinus Torvalds static inline int is_cow_mapping(unsigned int flags)
36467121172SLinus Torvalds {
36567121172SLinus Torvalds 	return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
36667121172SLinus Torvalds }
36767121172SLinus Torvalds 
368b5810039SNick Piggin /*
3696aab341eSLinus Torvalds  * This function gets the "struct page" associated with a pte.
3706aab341eSLinus Torvalds  *
3716aab341eSLinus Torvalds  * NOTE! Some mappings do not have "struct pages". A raw PFN mapping
3726aab341eSLinus Torvalds  * will have each page table entry just pointing to a raw page frame
3736aab341eSLinus Torvalds  * number, and as far as the VM layer is concerned, those do not have
3746aab341eSLinus Torvalds  * pages associated with them - even if the PFN might point to memory
3756aab341eSLinus Torvalds  * that otherwise is perfectly fine and has a "struct page".
3766aab341eSLinus Torvalds  *
3776aab341eSLinus Torvalds  * The way we recognize those mappings is through the rules set up
3786aab341eSLinus Torvalds  * by "remap_pfn_range()": the vma will have the VM_PFNMAP bit set,
3796aab341eSLinus Torvalds  * and the vm_pgoff will point to the first PFN mapped: thus every
3806aab341eSLinus Torvalds  * page that is a raw mapping will always honor the rule
3816aab341eSLinus Torvalds  *
3826aab341eSLinus Torvalds  *	pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
3836aab341eSLinus Torvalds  *
3846aab341eSLinus Torvalds  * and if that isn't true, the page has been COW'ed (in which case it
3856aab341eSLinus Torvalds  * _does_ have a "struct page" associated with it even if it is in a
3866aab341eSLinus Torvalds  * VM_PFNMAP range).
387ee498ed7SHugh Dickins  */
3886aab341eSLinus Torvalds struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, pte_t pte)
389ee498ed7SHugh Dickins {
3906aab341eSLinus Torvalds 	unsigned long pfn = pte_pfn(pte);
3916aab341eSLinus Torvalds 
392b7ab795bSNick Piggin 	if (unlikely(vma->vm_flags & VM_PFNMAP)) {
3936aab341eSLinus Torvalds 		unsigned long off = (addr - vma->vm_start) >> PAGE_SHIFT;
3946aab341eSLinus Torvalds 		if (pfn == vma->vm_pgoff + off)
3956aab341eSLinus Torvalds 			return NULL;
39667121172SLinus Torvalds 		if (!is_cow_mapping(vma->vm_flags))
397fb155c16SLinus Torvalds 			return NULL;
3986aab341eSLinus Torvalds 	}
3996aab341eSLinus Torvalds 
400315ab19aSNick Piggin 	/*
401315ab19aSNick Piggin 	 * Add some anal sanity checks for now. Eventually,
402315ab19aSNick Piggin 	 * we should just do "return pfn_to_page(pfn)", but
403315ab19aSNick Piggin 	 * in the meantime we check that we get a valid pfn,
404315ab19aSNick Piggin 	 * and that the resulting page looks ok.
405315ab19aSNick Piggin 	 */
4066aab341eSLinus Torvalds 	if (unlikely(!pfn_valid(pfn))) {
4076aab341eSLinus Torvalds 		print_bad_pte(vma, pte, addr);
4086aab341eSLinus Torvalds 		return NULL;
4096aab341eSLinus Torvalds 	}
4106aab341eSLinus Torvalds 
4116aab341eSLinus Torvalds 	/*
4126aab341eSLinus Torvalds 	 * NOTE! We still have PageReserved() pages in the page
4136aab341eSLinus Torvalds 	 * tables.
4146aab341eSLinus Torvalds 	 *
4156aab341eSLinus Torvalds 	 * The PAGE_ZERO() pages and various VDSO mappings can
4166aab341eSLinus Torvalds 	 * cause them to exist.
4176aab341eSLinus Torvalds 	 */
4186aab341eSLinus Torvalds 	return pfn_to_page(pfn);
419ee498ed7SHugh Dickins }
420ee498ed7SHugh Dickins 
421ee498ed7SHugh Dickins /*
4221da177e4SLinus Torvalds  * copy one vm_area from one task to the other. Assumes the page tables
4231da177e4SLinus Torvalds  * already present in the new task to be cleared in the whole range
4241da177e4SLinus Torvalds  * covered by this vma.
4251da177e4SLinus Torvalds  */
4261da177e4SLinus Torvalds 
4278c103762SHugh Dickins static inline void
4281da177e4SLinus Torvalds copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
429b5810039SNick Piggin 		pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma,
4308c103762SHugh Dickins 		unsigned long addr, int *rss)
4311da177e4SLinus Torvalds {
432b5810039SNick Piggin 	unsigned long vm_flags = vma->vm_flags;
4331da177e4SLinus Torvalds 	pte_t pte = *src_pte;
4341da177e4SLinus Torvalds 	struct page *page;
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	/* pte contains position in swap or file, so copy. */
4371da177e4SLinus Torvalds 	if (unlikely(!pte_present(pte))) {
4381da177e4SLinus Torvalds 		if (!pte_file(pte)) {
4390697212aSChristoph Lameter 			swp_entry_t entry = pte_to_swp_entry(pte);
4400697212aSChristoph Lameter 
4410697212aSChristoph Lameter 			swap_duplicate(entry);
4421da177e4SLinus Torvalds 			/* make sure dst_mm is on swapoff's mmlist. */
4431da177e4SLinus Torvalds 			if (unlikely(list_empty(&dst_mm->mmlist))) {
4441da177e4SLinus Torvalds 				spin_lock(&mmlist_lock);
445f412ac08SHugh Dickins 				if (list_empty(&dst_mm->mmlist))
446f412ac08SHugh Dickins 					list_add(&dst_mm->mmlist,
447f412ac08SHugh Dickins 						 &src_mm->mmlist);
4481da177e4SLinus Torvalds 				spin_unlock(&mmlist_lock);
4491da177e4SLinus Torvalds 			}
4500697212aSChristoph Lameter 			if (is_write_migration_entry(entry) &&
4510697212aSChristoph Lameter 					is_cow_mapping(vm_flags)) {
4520697212aSChristoph Lameter 				/*
4530697212aSChristoph Lameter 				 * COW mappings require pages in both parent
4540697212aSChristoph Lameter 				 * and child to be set to read.
4550697212aSChristoph Lameter 				 */
4560697212aSChristoph Lameter 				make_migration_entry_read(&entry);
4570697212aSChristoph Lameter 				pte = swp_entry_to_pte(entry);
4580697212aSChristoph Lameter 				set_pte_at(src_mm, addr, src_pte, pte);
4590697212aSChristoph Lameter 			}
4601da177e4SLinus Torvalds 		}
461ae859762SHugh Dickins 		goto out_set_pte;
4621da177e4SLinus Torvalds 	}
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	/*
4651da177e4SLinus Torvalds 	 * If it's a COW mapping, write protect it both
4661da177e4SLinus Torvalds 	 * in the parent and the child
4671da177e4SLinus Torvalds 	 */
46867121172SLinus Torvalds 	if (is_cow_mapping(vm_flags)) {
4691da177e4SLinus Torvalds 		ptep_set_wrprotect(src_mm, addr, src_pte);
4703dc90795SZachary Amsden 		pte = pte_wrprotect(pte);
4711da177e4SLinus Torvalds 	}
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 	/*
4741da177e4SLinus Torvalds 	 * If it's a shared mapping, mark it clean in
4751da177e4SLinus Torvalds 	 * the child
4761da177e4SLinus Torvalds 	 */
4771da177e4SLinus Torvalds 	if (vm_flags & VM_SHARED)
4781da177e4SLinus Torvalds 		pte = pte_mkclean(pte);
4791da177e4SLinus Torvalds 	pte = pte_mkold(pte);
4806aab341eSLinus Torvalds 
4816aab341eSLinus Torvalds 	page = vm_normal_page(vma, addr, pte);
4826aab341eSLinus Torvalds 	if (page) {
4831da177e4SLinus Torvalds 		get_page(page);
4841da177e4SLinus Torvalds 		page_dup_rmap(page);
4858c103762SHugh Dickins 		rss[!!PageAnon(page)]++;
4866aab341eSLinus Torvalds 	}
487ae859762SHugh Dickins 
488ae859762SHugh Dickins out_set_pte:
489ae859762SHugh Dickins 	set_pte_at(dst_mm, addr, dst_pte, pte);
4901da177e4SLinus Torvalds }
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
4931da177e4SLinus Torvalds 		pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
4941da177e4SLinus Torvalds 		unsigned long addr, unsigned long end)
4951da177e4SLinus Torvalds {
4961da177e4SLinus Torvalds 	pte_t *src_pte, *dst_pte;
497c74df32cSHugh Dickins 	spinlock_t *src_ptl, *dst_ptl;
498e040f218SHugh Dickins 	int progress = 0;
4998c103762SHugh Dickins 	int rss[2];
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds again:
502ae859762SHugh Dickins 	rss[1] = rss[0] = 0;
503c74df32cSHugh Dickins 	dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
5041da177e4SLinus Torvalds 	if (!dst_pte)
5051da177e4SLinus Torvalds 		return -ENOMEM;
5061da177e4SLinus Torvalds 	src_pte = pte_offset_map_nested(src_pmd, addr);
5074c21e2f2SHugh Dickins 	src_ptl = pte_lockptr(src_mm, src_pmd);
508f20dc5f7SIngo Molnar 	spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
509*6606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 	do {
5121da177e4SLinus Torvalds 		/*
5131da177e4SLinus Torvalds 		 * We are holding two locks at this point - either of them
5141da177e4SLinus Torvalds 		 * could generate latencies in another task on another CPU.
5151da177e4SLinus Torvalds 		 */
516e040f218SHugh Dickins 		if (progress >= 32) {
517e040f218SHugh Dickins 			progress = 0;
518e040f218SHugh Dickins 			if (need_resched() ||
519c74df32cSHugh Dickins 			    need_lockbreak(src_ptl) ||
520c74df32cSHugh Dickins 			    need_lockbreak(dst_ptl))
5211da177e4SLinus Torvalds 				break;
522e040f218SHugh Dickins 		}
5231da177e4SLinus Torvalds 		if (pte_none(*src_pte)) {
5241da177e4SLinus Torvalds 			progress++;
5251da177e4SLinus Torvalds 			continue;
5261da177e4SLinus Torvalds 		}
5278c103762SHugh Dickins 		copy_one_pte(dst_mm, src_mm, dst_pte, src_pte, vma, addr, rss);
5281da177e4SLinus Torvalds 		progress += 8;
5291da177e4SLinus Torvalds 	} while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
5301da177e4SLinus Torvalds 
531*6606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
532c74df32cSHugh Dickins 	spin_unlock(src_ptl);
5331da177e4SLinus Torvalds 	pte_unmap_nested(src_pte - 1);
534ae859762SHugh Dickins 	add_mm_rss(dst_mm, rss[0], rss[1]);
535c74df32cSHugh Dickins 	pte_unmap_unlock(dst_pte - 1, dst_ptl);
536c74df32cSHugh Dickins 	cond_resched();
5371da177e4SLinus Torvalds 	if (addr != end)
5381da177e4SLinus Torvalds 		goto again;
5391da177e4SLinus Torvalds 	return 0;
5401da177e4SLinus Torvalds }
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
5431da177e4SLinus Torvalds 		pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma,
5441da177e4SLinus Torvalds 		unsigned long addr, unsigned long end)
5451da177e4SLinus Torvalds {
5461da177e4SLinus Torvalds 	pmd_t *src_pmd, *dst_pmd;
5471da177e4SLinus Torvalds 	unsigned long next;
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds 	dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
5501da177e4SLinus Torvalds 	if (!dst_pmd)
5511da177e4SLinus Torvalds 		return -ENOMEM;
5521da177e4SLinus Torvalds 	src_pmd = pmd_offset(src_pud, addr);
5531da177e4SLinus Torvalds 	do {
5541da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
5551da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(src_pmd))
5561da177e4SLinus Torvalds 			continue;
5571da177e4SLinus Torvalds 		if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd,
5581da177e4SLinus Torvalds 						vma, addr, next))
5591da177e4SLinus Torvalds 			return -ENOMEM;
5601da177e4SLinus Torvalds 	} while (dst_pmd++, src_pmd++, addr = next, addr != end);
5611da177e4SLinus Torvalds 	return 0;
5621da177e4SLinus Torvalds }
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
5651da177e4SLinus Torvalds 		pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma,
5661da177e4SLinus Torvalds 		unsigned long addr, unsigned long end)
5671da177e4SLinus Torvalds {
5681da177e4SLinus Torvalds 	pud_t *src_pud, *dst_pud;
5691da177e4SLinus Torvalds 	unsigned long next;
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 	dst_pud = pud_alloc(dst_mm, dst_pgd, addr);
5721da177e4SLinus Torvalds 	if (!dst_pud)
5731da177e4SLinus Torvalds 		return -ENOMEM;
5741da177e4SLinus Torvalds 	src_pud = pud_offset(src_pgd, addr);
5751da177e4SLinus Torvalds 	do {
5761da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
5771da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(src_pud))
5781da177e4SLinus Torvalds 			continue;
5791da177e4SLinus Torvalds 		if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud,
5801da177e4SLinus Torvalds 						vma, addr, next))
5811da177e4SLinus Torvalds 			return -ENOMEM;
5821da177e4SLinus Torvalds 	} while (dst_pud++, src_pud++, addr = next, addr != end);
5831da177e4SLinus Torvalds 	return 0;
5841da177e4SLinus Torvalds }
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
5871da177e4SLinus Torvalds 		struct vm_area_struct *vma)
5881da177e4SLinus Torvalds {
5891da177e4SLinus Torvalds 	pgd_t *src_pgd, *dst_pgd;
5901da177e4SLinus Torvalds 	unsigned long next;
5911da177e4SLinus Torvalds 	unsigned long addr = vma->vm_start;
5921da177e4SLinus Torvalds 	unsigned long end = vma->vm_end;
5931da177e4SLinus Torvalds 
594d992895bSNick Piggin 	/*
595d992895bSNick Piggin 	 * Don't copy ptes where a page fault will fill them correctly.
596d992895bSNick Piggin 	 * Fork becomes much lighter when there are big shared or private
597d992895bSNick Piggin 	 * readonly mappings. The tradeoff is that copy_page_range is more
598d992895bSNick Piggin 	 * efficient than faulting.
599d992895bSNick Piggin 	 */
6004d7672b4SLinus Torvalds 	if (!(vma->vm_flags & (VM_HUGETLB|VM_NONLINEAR|VM_PFNMAP|VM_INSERTPAGE))) {
601d992895bSNick Piggin 		if (!vma->anon_vma)
602d992895bSNick Piggin 			return 0;
603d992895bSNick Piggin 	}
604d992895bSNick Piggin 
6051da177e4SLinus Torvalds 	if (is_vm_hugetlb_page(vma))
6061da177e4SLinus Torvalds 		return copy_hugetlb_page_range(dst_mm, src_mm, vma);
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 	dst_pgd = pgd_offset(dst_mm, addr);
6091da177e4SLinus Torvalds 	src_pgd = pgd_offset(src_mm, addr);
6101da177e4SLinus Torvalds 	do {
6111da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
6121da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(src_pgd))
6131da177e4SLinus Torvalds 			continue;
6141da177e4SLinus Torvalds 		if (copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd,
6151da177e4SLinus Torvalds 						vma, addr, next))
6161da177e4SLinus Torvalds 			return -ENOMEM;
6171da177e4SLinus Torvalds 	} while (dst_pgd++, src_pgd++, addr = next, addr != end);
6181da177e4SLinus Torvalds 	return 0;
6191da177e4SLinus Torvalds }
6201da177e4SLinus Torvalds 
62151c6f666SRobin Holt static unsigned long zap_pte_range(struct mmu_gather *tlb,
622b5810039SNick Piggin 				struct vm_area_struct *vma, pmd_t *pmd,
6231da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
62451c6f666SRobin Holt 				long *zap_work, struct zap_details *details)
6251da177e4SLinus Torvalds {
626b5810039SNick Piggin 	struct mm_struct *mm = tlb->mm;
6271da177e4SLinus Torvalds 	pte_t *pte;
628508034a3SHugh Dickins 	spinlock_t *ptl;
629ae859762SHugh Dickins 	int file_rss = 0;
630ae859762SHugh Dickins 	int anon_rss = 0;
6311da177e4SLinus Torvalds 
632508034a3SHugh Dickins 	pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
633*6606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
6341da177e4SLinus Torvalds 	do {
6351da177e4SLinus Torvalds 		pte_t ptent = *pte;
63651c6f666SRobin Holt 		if (pte_none(ptent)) {
63751c6f666SRobin Holt 			(*zap_work)--;
6381da177e4SLinus Torvalds 			continue;
63951c6f666SRobin Holt 		}
64051c6f666SRobin Holt 
64151c6f666SRobin Holt 		(*zap_work) -= PAGE_SIZE;
64251c6f666SRobin Holt 
6436f5e6b9eSHugh Dickins 		if (pte_present(ptent)) {
6446f5e6b9eSHugh Dickins 			struct page *page;
6456f5e6b9eSHugh Dickins 
6466aab341eSLinus Torvalds 			page = vm_normal_page(vma, addr, ptent);
6471da177e4SLinus Torvalds 			if (unlikely(details) && page) {
6481da177e4SLinus Torvalds 				/*
6491da177e4SLinus Torvalds 				 * unmap_shared_mapping_pages() wants to
6501da177e4SLinus Torvalds 				 * invalidate cache without truncating:
6511da177e4SLinus Torvalds 				 * unmap shared but keep private pages.
6521da177e4SLinus Torvalds 				 */
6531da177e4SLinus Torvalds 				if (details->check_mapping &&
6541da177e4SLinus Torvalds 				    details->check_mapping != page->mapping)
6551da177e4SLinus Torvalds 					continue;
6561da177e4SLinus Torvalds 				/*
6571da177e4SLinus Torvalds 				 * Each page->index must be checked when
6581da177e4SLinus Torvalds 				 * invalidating or truncating nonlinear.
6591da177e4SLinus Torvalds 				 */
6601da177e4SLinus Torvalds 				if (details->nonlinear_vma &&
6611da177e4SLinus Torvalds 				    (page->index < details->first_index ||
6621da177e4SLinus Torvalds 				     page->index > details->last_index))
6631da177e4SLinus Torvalds 					continue;
6641da177e4SLinus Torvalds 			}
665b5810039SNick Piggin 			ptent = ptep_get_and_clear_full(mm, addr, pte,
666a600388dSZachary Amsden 							tlb->fullmm);
6671da177e4SLinus Torvalds 			tlb_remove_tlb_entry(tlb, pte, addr);
6681da177e4SLinus Torvalds 			if (unlikely(!page))
6691da177e4SLinus Torvalds 				continue;
6701da177e4SLinus Torvalds 			if (unlikely(details) && details->nonlinear_vma
6711da177e4SLinus Torvalds 			    && linear_page_index(details->nonlinear_vma,
6721da177e4SLinus Torvalds 						addr) != page->index)
673b5810039SNick Piggin 				set_pte_at(mm, addr, pte,
6741da177e4SLinus Torvalds 					   pgoff_to_pte(page->index));
6751da177e4SLinus Torvalds 			if (PageAnon(page))
67686d912f4SHugh Dickins 				anon_rss--;
6776237bcd9SHugh Dickins 			else {
6786237bcd9SHugh Dickins 				if (pte_dirty(ptent))
6796237bcd9SHugh Dickins 					set_page_dirty(page);
6806237bcd9SHugh Dickins 				if (pte_young(ptent))
6811da177e4SLinus Torvalds 					mark_page_accessed(page);
68286d912f4SHugh Dickins 				file_rss--;
6836237bcd9SHugh Dickins 			}
6841da177e4SLinus Torvalds 			page_remove_rmap(page);
6851da177e4SLinus Torvalds 			tlb_remove_page(tlb, page);
6861da177e4SLinus Torvalds 			continue;
6871da177e4SLinus Torvalds 		}
6881da177e4SLinus Torvalds 		/*
6891da177e4SLinus Torvalds 		 * If details->check_mapping, we leave swap entries;
6901da177e4SLinus Torvalds 		 * if details->nonlinear_vma, we leave file entries.
6911da177e4SLinus Torvalds 		 */
6921da177e4SLinus Torvalds 		if (unlikely(details))
6931da177e4SLinus Torvalds 			continue;
6941da177e4SLinus Torvalds 		if (!pte_file(ptent))
6951da177e4SLinus Torvalds 			free_swap_and_cache(pte_to_swp_entry(ptent));
6969888a1caSZachary Amsden 		pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
69751c6f666SRobin Holt 	} while (pte++, addr += PAGE_SIZE, (addr != end && *zap_work > 0));
698ae859762SHugh Dickins 
69986d912f4SHugh Dickins 	add_mm_rss(mm, file_rss, anon_rss);
700*6606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
701508034a3SHugh Dickins 	pte_unmap_unlock(pte - 1, ptl);
70251c6f666SRobin Holt 
70351c6f666SRobin Holt 	return addr;
7041da177e4SLinus Torvalds }
7051da177e4SLinus Torvalds 
70651c6f666SRobin Holt static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
707b5810039SNick Piggin 				struct vm_area_struct *vma, pud_t *pud,
7081da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
70951c6f666SRobin Holt 				long *zap_work, struct zap_details *details)
7101da177e4SLinus Torvalds {
7111da177e4SLinus Torvalds 	pmd_t *pmd;
7121da177e4SLinus Torvalds 	unsigned long next;
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
7151da177e4SLinus Torvalds 	do {
7161da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
71751c6f666SRobin Holt 		if (pmd_none_or_clear_bad(pmd)) {
71851c6f666SRobin Holt 			(*zap_work)--;
7191da177e4SLinus Torvalds 			continue;
72051c6f666SRobin Holt 		}
72151c6f666SRobin Holt 		next = zap_pte_range(tlb, vma, pmd, addr, next,
72251c6f666SRobin Holt 						zap_work, details);
72351c6f666SRobin Holt 	} while (pmd++, addr = next, (addr != end && *zap_work > 0));
72451c6f666SRobin Holt 
72551c6f666SRobin Holt 	return addr;
7261da177e4SLinus Torvalds }
7271da177e4SLinus Torvalds 
72851c6f666SRobin Holt static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
729b5810039SNick Piggin 				struct vm_area_struct *vma, pgd_t *pgd,
7301da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
73151c6f666SRobin Holt 				long *zap_work, struct zap_details *details)
7321da177e4SLinus Torvalds {
7331da177e4SLinus Torvalds 	pud_t *pud;
7341da177e4SLinus Torvalds 	unsigned long next;
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
7371da177e4SLinus Torvalds 	do {
7381da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
73951c6f666SRobin Holt 		if (pud_none_or_clear_bad(pud)) {
74051c6f666SRobin Holt 			(*zap_work)--;
7411da177e4SLinus Torvalds 			continue;
74251c6f666SRobin Holt 		}
74351c6f666SRobin Holt 		next = zap_pmd_range(tlb, vma, pud, addr, next,
74451c6f666SRobin Holt 						zap_work, details);
74551c6f666SRobin Holt 	} while (pud++, addr = next, (addr != end && *zap_work > 0));
74651c6f666SRobin Holt 
74751c6f666SRobin Holt 	return addr;
7481da177e4SLinus Torvalds }
7491da177e4SLinus Torvalds 
75051c6f666SRobin Holt static unsigned long unmap_page_range(struct mmu_gather *tlb,
75151c6f666SRobin Holt 				struct vm_area_struct *vma,
7521da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
75351c6f666SRobin Holt 				long *zap_work, struct zap_details *details)
7541da177e4SLinus Torvalds {
7551da177e4SLinus Torvalds 	pgd_t *pgd;
7561da177e4SLinus Torvalds 	unsigned long next;
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds 	if (details && !details->check_mapping && !details->nonlinear_vma)
7591da177e4SLinus Torvalds 		details = NULL;
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 	BUG_ON(addr >= end);
7621da177e4SLinus Torvalds 	tlb_start_vma(tlb, vma);
7631da177e4SLinus Torvalds 	pgd = pgd_offset(vma->vm_mm, addr);
7641da177e4SLinus Torvalds 	do {
7651da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
76651c6f666SRobin Holt 		if (pgd_none_or_clear_bad(pgd)) {
76751c6f666SRobin Holt 			(*zap_work)--;
7681da177e4SLinus Torvalds 			continue;
76951c6f666SRobin Holt 		}
77051c6f666SRobin Holt 		next = zap_pud_range(tlb, vma, pgd, addr, next,
77151c6f666SRobin Holt 						zap_work, details);
77251c6f666SRobin Holt 	} while (pgd++, addr = next, (addr != end && *zap_work > 0));
7731da177e4SLinus Torvalds 	tlb_end_vma(tlb, vma);
77451c6f666SRobin Holt 
77551c6f666SRobin Holt 	return addr;
7761da177e4SLinus Torvalds }
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds #ifdef CONFIG_PREEMPT
7791da177e4SLinus Torvalds # define ZAP_BLOCK_SIZE	(8 * PAGE_SIZE)
7801da177e4SLinus Torvalds #else
7811da177e4SLinus Torvalds /* No preempt: go for improved straight-line efficiency */
7821da177e4SLinus Torvalds # define ZAP_BLOCK_SIZE	(1024 * PAGE_SIZE)
7831da177e4SLinus Torvalds #endif
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds /**
7861da177e4SLinus Torvalds  * unmap_vmas - unmap a range of memory covered by a list of vma's
7871da177e4SLinus Torvalds  * @tlbp: address of the caller's struct mmu_gather
7881da177e4SLinus Torvalds  * @vma: the starting vma
7891da177e4SLinus Torvalds  * @start_addr: virtual address at which to start unmapping
7901da177e4SLinus Torvalds  * @end_addr: virtual address at which to end unmapping
7911da177e4SLinus Torvalds  * @nr_accounted: Place number of unmapped pages in vm-accountable vma's here
7921da177e4SLinus Torvalds  * @details: details of nonlinear truncation or shared cache invalidation
7931da177e4SLinus Torvalds  *
794ee39b37bSHugh Dickins  * Returns the end address of the unmapping (restart addr if interrupted).
7951da177e4SLinus Torvalds  *
796508034a3SHugh Dickins  * Unmap all pages in the vma list.
7971da177e4SLinus Torvalds  *
798508034a3SHugh Dickins  * We aim to not hold locks for too long (for scheduling latency reasons).
799508034a3SHugh Dickins  * So zap pages in ZAP_BLOCK_SIZE bytecounts.  This means we need to
8001da177e4SLinus Torvalds  * return the ending mmu_gather to the caller.
8011da177e4SLinus Torvalds  *
8021da177e4SLinus Torvalds  * Only addresses between `start' and `end' will be unmapped.
8031da177e4SLinus Torvalds  *
8041da177e4SLinus Torvalds  * The VMA list must be sorted in ascending virtual address order.
8051da177e4SLinus Torvalds  *
8061da177e4SLinus Torvalds  * unmap_vmas() assumes that the caller will flush the whole unmapped address
8071da177e4SLinus Torvalds  * range after unmap_vmas() returns.  So the only responsibility here is to
8081da177e4SLinus Torvalds  * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
8091da177e4SLinus Torvalds  * drops the lock and schedules.
8101da177e4SLinus Torvalds  */
811508034a3SHugh Dickins unsigned long unmap_vmas(struct mmu_gather **tlbp,
8121da177e4SLinus Torvalds 		struct vm_area_struct *vma, unsigned long start_addr,
8131da177e4SLinus Torvalds 		unsigned long end_addr, unsigned long *nr_accounted,
8141da177e4SLinus Torvalds 		struct zap_details *details)
8151da177e4SLinus Torvalds {
81651c6f666SRobin Holt 	long zap_work = ZAP_BLOCK_SIZE;
8171da177e4SLinus Torvalds 	unsigned long tlb_start = 0;	/* For tlb_finish_mmu */
8181da177e4SLinus Torvalds 	int tlb_start_valid = 0;
819ee39b37bSHugh Dickins 	unsigned long start = start_addr;
8201da177e4SLinus Torvalds 	spinlock_t *i_mmap_lock = details? details->i_mmap_lock: NULL;
8214d6ddfa9SHugh Dickins 	int fullmm = (*tlbp)->fullmm;
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 	for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) {
8241da177e4SLinus Torvalds 		unsigned long end;
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 		start = max(vma->vm_start, start_addr);
8271da177e4SLinus Torvalds 		if (start >= vma->vm_end)
8281da177e4SLinus Torvalds 			continue;
8291da177e4SLinus Torvalds 		end = min(vma->vm_end, end_addr);
8301da177e4SLinus Torvalds 		if (end <= vma->vm_start)
8311da177e4SLinus Torvalds 			continue;
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds 		if (vma->vm_flags & VM_ACCOUNT)
8341da177e4SLinus Torvalds 			*nr_accounted += (end - start) >> PAGE_SHIFT;
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 		while (start != end) {
8371da177e4SLinus Torvalds 			if (!tlb_start_valid) {
8381da177e4SLinus Torvalds 				tlb_start = start;
8391da177e4SLinus Torvalds 				tlb_start_valid = 1;
8401da177e4SLinus Torvalds 			}
8411da177e4SLinus Torvalds 
84251c6f666SRobin Holt 			if (unlikely(is_vm_hugetlb_page(vma))) {
8431da177e4SLinus Torvalds 				unmap_hugepage_range(vma, start, end);
84451c6f666SRobin Holt 				zap_work -= (end - start) /
84551c6f666SRobin Holt 						(HPAGE_SIZE / PAGE_SIZE);
84651c6f666SRobin Holt 				start = end;
84751c6f666SRobin Holt 			} else
84851c6f666SRobin Holt 				start = unmap_page_range(*tlbp, vma,
84951c6f666SRobin Holt 						start, end, &zap_work, details);
8501da177e4SLinus Torvalds 
85151c6f666SRobin Holt 			if (zap_work > 0) {
85251c6f666SRobin Holt 				BUG_ON(start != end);
85351c6f666SRobin Holt 				break;
85451c6f666SRobin Holt 			}
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 			tlb_finish_mmu(*tlbp, tlb_start, start);
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds 			if (need_resched() ||
8591da177e4SLinus Torvalds 				(i_mmap_lock && need_lockbreak(i_mmap_lock))) {
8601da177e4SLinus Torvalds 				if (i_mmap_lock) {
861508034a3SHugh Dickins 					*tlbp = NULL;
8621da177e4SLinus Torvalds 					goto out;
8631da177e4SLinus Torvalds 				}
8641da177e4SLinus Torvalds 				cond_resched();
8651da177e4SLinus Torvalds 			}
8661da177e4SLinus Torvalds 
867508034a3SHugh Dickins 			*tlbp = tlb_gather_mmu(vma->vm_mm, fullmm);
8681da177e4SLinus Torvalds 			tlb_start_valid = 0;
86951c6f666SRobin Holt 			zap_work = ZAP_BLOCK_SIZE;
8701da177e4SLinus Torvalds 		}
8711da177e4SLinus Torvalds 	}
8721da177e4SLinus Torvalds out:
873ee39b37bSHugh Dickins 	return start;	/* which is now the end (or restart) address */
8741da177e4SLinus Torvalds }
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds /**
8771da177e4SLinus Torvalds  * zap_page_range - remove user pages in a given range
8781da177e4SLinus Torvalds  * @vma: vm_area_struct holding the applicable pages
8791da177e4SLinus Torvalds  * @address: starting address of pages to zap
8801da177e4SLinus Torvalds  * @size: number of bytes to zap
8811da177e4SLinus Torvalds  * @details: details of nonlinear truncation or shared cache invalidation
8821da177e4SLinus Torvalds  */
883ee39b37bSHugh Dickins unsigned long zap_page_range(struct vm_area_struct *vma, unsigned long address,
8841da177e4SLinus Torvalds 		unsigned long size, struct zap_details *details)
8851da177e4SLinus Torvalds {
8861da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
8871da177e4SLinus Torvalds 	struct mmu_gather *tlb;
8881da177e4SLinus Torvalds 	unsigned long end = address + size;
8891da177e4SLinus Torvalds 	unsigned long nr_accounted = 0;
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 	lru_add_drain();
8921da177e4SLinus Torvalds 	tlb = tlb_gather_mmu(mm, 0);
893365e9c87SHugh Dickins 	update_hiwater_rss(mm);
894508034a3SHugh Dickins 	end = unmap_vmas(&tlb, vma, address, end, &nr_accounted, details);
895508034a3SHugh Dickins 	if (tlb)
8968f4f8c16SHugh Dickins 		tlb_finish_mmu(tlb, address, end);
897ee39b37bSHugh Dickins 	return end;
8981da177e4SLinus Torvalds }
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds /*
9011da177e4SLinus Torvalds  * Do a quick page-table lookup for a single page.
9021da177e4SLinus Torvalds  */
9036aab341eSLinus Torvalds struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
904deceb6cdSHugh Dickins 			unsigned int flags)
9051da177e4SLinus Torvalds {
9061da177e4SLinus Torvalds 	pgd_t *pgd;
9071da177e4SLinus Torvalds 	pud_t *pud;
9081da177e4SLinus Torvalds 	pmd_t *pmd;
9091da177e4SLinus Torvalds 	pte_t *ptep, pte;
910deceb6cdSHugh Dickins 	spinlock_t *ptl;
9111da177e4SLinus Torvalds 	struct page *page;
9126aab341eSLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
9131da177e4SLinus Torvalds 
914deceb6cdSHugh Dickins 	page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
915deceb6cdSHugh Dickins 	if (!IS_ERR(page)) {
916deceb6cdSHugh Dickins 		BUG_ON(flags & FOLL_GET);
917deceb6cdSHugh Dickins 		goto out;
918deceb6cdSHugh Dickins 	}
9191da177e4SLinus Torvalds 
920deceb6cdSHugh Dickins 	page = NULL;
9211da177e4SLinus Torvalds 	pgd = pgd_offset(mm, address);
9221da177e4SLinus Torvalds 	if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
923deceb6cdSHugh Dickins 		goto no_page_table;
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 	pud = pud_offset(pgd, address);
9261da177e4SLinus Torvalds 	if (pud_none(*pud) || unlikely(pud_bad(*pud)))
927deceb6cdSHugh Dickins 		goto no_page_table;
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds 	pmd = pmd_offset(pud, address);
9301da177e4SLinus Torvalds 	if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
931deceb6cdSHugh Dickins 		goto no_page_table;
9321da177e4SLinus Torvalds 
933deceb6cdSHugh Dickins 	if (pmd_huge(*pmd)) {
934deceb6cdSHugh Dickins 		BUG_ON(flags & FOLL_GET);
935deceb6cdSHugh Dickins 		page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE);
936deceb6cdSHugh Dickins 		goto out;
937deceb6cdSHugh Dickins 	}
938deceb6cdSHugh Dickins 
939deceb6cdSHugh Dickins 	ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
9401da177e4SLinus Torvalds 	if (!ptep)
9411da177e4SLinus Torvalds 		goto out;
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 	pte = *ptep;
944deceb6cdSHugh Dickins 	if (!pte_present(pte))
945deceb6cdSHugh Dickins 		goto unlock;
946deceb6cdSHugh Dickins 	if ((flags & FOLL_WRITE) && !pte_write(pte))
947deceb6cdSHugh Dickins 		goto unlock;
9486aab341eSLinus Torvalds 	page = vm_normal_page(vma, address, pte);
9496aab341eSLinus Torvalds 	if (unlikely(!page))
950deceb6cdSHugh Dickins 		goto unlock;
951deceb6cdSHugh Dickins 
952deceb6cdSHugh Dickins 	if (flags & FOLL_GET)
953deceb6cdSHugh Dickins 		get_page(page);
954deceb6cdSHugh Dickins 	if (flags & FOLL_TOUCH) {
955deceb6cdSHugh Dickins 		if ((flags & FOLL_WRITE) &&
956deceb6cdSHugh Dickins 		    !pte_dirty(pte) && !PageDirty(page))
957f33ea7f4SNick Piggin 			set_page_dirty(page);
9581da177e4SLinus Torvalds 		mark_page_accessed(page);
9591da177e4SLinus Torvalds 	}
960deceb6cdSHugh Dickins unlock:
961deceb6cdSHugh Dickins 	pte_unmap_unlock(ptep, ptl);
9621da177e4SLinus Torvalds out:
963deceb6cdSHugh Dickins 	return page;
964deceb6cdSHugh Dickins 
965deceb6cdSHugh Dickins no_page_table:
966deceb6cdSHugh Dickins 	/*
967deceb6cdSHugh Dickins 	 * When core dumping an enormous anonymous area that nobody
968deceb6cdSHugh Dickins 	 * has touched so far, we don't want to allocate page tables.
969deceb6cdSHugh Dickins 	 */
970deceb6cdSHugh Dickins 	if (flags & FOLL_ANON) {
971deceb6cdSHugh Dickins 		page = ZERO_PAGE(address);
972deceb6cdSHugh Dickins 		if (flags & FOLL_GET)
973deceb6cdSHugh Dickins 			get_page(page);
974deceb6cdSHugh Dickins 		BUG_ON(flags & FOLL_WRITE);
9751da177e4SLinus Torvalds 	}
976deceb6cdSHugh Dickins 	return page;
9771da177e4SLinus Torvalds }
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
9801da177e4SLinus Torvalds 		unsigned long start, int len, int write, int force,
9811da177e4SLinus Torvalds 		struct page **pages, struct vm_area_struct **vmas)
9821da177e4SLinus Torvalds {
9831da177e4SLinus Torvalds 	int i;
984deceb6cdSHugh Dickins 	unsigned int vm_flags;
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds 	/*
9871da177e4SLinus Torvalds 	 * Require read or write permissions.
9881da177e4SLinus Torvalds 	 * If 'force' is set, we only require the "MAY" flags.
9891da177e4SLinus Torvalds 	 */
990deceb6cdSHugh Dickins 	vm_flags  = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
991deceb6cdSHugh Dickins 	vm_flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
9921da177e4SLinus Torvalds 	i = 0;
9931da177e4SLinus Torvalds 
9941da177e4SLinus Torvalds 	do {
9951da177e4SLinus Torvalds 		struct vm_area_struct *vma;
996deceb6cdSHugh Dickins 		unsigned int foll_flags;
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds 		vma = find_extend_vma(mm, start);
9991da177e4SLinus Torvalds 		if (!vma && in_gate_area(tsk, start)) {
10001da177e4SLinus Torvalds 			unsigned long pg = start & PAGE_MASK;
10011da177e4SLinus Torvalds 			struct vm_area_struct *gate_vma = get_gate_vma(tsk);
10021da177e4SLinus Torvalds 			pgd_t *pgd;
10031da177e4SLinus Torvalds 			pud_t *pud;
10041da177e4SLinus Torvalds 			pmd_t *pmd;
10051da177e4SLinus Torvalds 			pte_t *pte;
10061da177e4SLinus Torvalds 			if (write) /* user gate pages are read-only */
10071da177e4SLinus Torvalds 				return i ? : -EFAULT;
10081da177e4SLinus Torvalds 			if (pg > TASK_SIZE)
10091da177e4SLinus Torvalds 				pgd = pgd_offset_k(pg);
10101da177e4SLinus Torvalds 			else
10111da177e4SLinus Torvalds 				pgd = pgd_offset_gate(mm, pg);
10121da177e4SLinus Torvalds 			BUG_ON(pgd_none(*pgd));
10131da177e4SLinus Torvalds 			pud = pud_offset(pgd, pg);
10141da177e4SLinus Torvalds 			BUG_ON(pud_none(*pud));
10151da177e4SLinus Torvalds 			pmd = pmd_offset(pud, pg);
1016690dbe1cSHugh Dickins 			if (pmd_none(*pmd))
1017690dbe1cSHugh Dickins 				return i ? : -EFAULT;
10181da177e4SLinus Torvalds 			pte = pte_offset_map(pmd, pg);
1019690dbe1cSHugh Dickins 			if (pte_none(*pte)) {
1020690dbe1cSHugh Dickins 				pte_unmap(pte);
1021690dbe1cSHugh Dickins 				return i ? : -EFAULT;
1022690dbe1cSHugh Dickins 			}
10231da177e4SLinus Torvalds 			if (pages) {
1024fa2a455bSNick Piggin 				struct page *page = vm_normal_page(gate_vma, start, *pte);
10256aab341eSLinus Torvalds 				pages[i] = page;
10266aab341eSLinus Torvalds 				if (page)
10276aab341eSLinus Torvalds 					get_page(page);
10281da177e4SLinus Torvalds 			}
10291da177e4SLinus Torvalds 			pte_unmap(pte);
10301da177e4SLinus Torvalds 			if (vmas)
10311da177e4SLinus Torvalds 				vmas[i] = gate_vma;
10321da177e4SLinus Torvalds 			i++;
10331da177e4SLinus Torvalds 			start += PAGE_SIZE;
10341da177e4SLinus Torvalds 			len--;
10351da177e4SLinus Torvalds 			continue;
10361da177e4SLinus Torvalds 		}
10371da177e4SLinus Torvalds 
10381ff80389SLinus Torvalds 		if (!vma || (vma->vm_flags & (VM_IO | VM_PFNMAP))
1039deceb6cdSHugh Dickins 				|| !(vm_flags & vma->vm_flags))
10401da177e4SLinus Torvalds 			return i ? : -EFAULT;
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds 		if (is_vm_hugetlb_page(vma)) {
10431da177e4SLinus Torvalds 			i = follow_hugetlb_page(mm, vma, pages, vmas,
10441da177e4SLinus Torvalds 						&start, &len, i);
10451da177e4SLinus Torvalds 			continue;
10461da177e4SLinus Torvalds 		}
1047deceb6cdSHugh Dickins 
1048deceb6cdSHugh Dickins 		foll_flags = FOLL_TOUCH;
1049deceb6cdSHugh Dickins 		if (pages)
1050deceb6cdSHugh Dickins 			foll_flags |= FOLL_GET;
1051deceb6cdSHugh Dickins 		if (!write && !(vma->vm_flags & VM_LOCKED) &&
1052deceb6cdSHugh Dickins 		    (!vma->vm_ops || !vma->vm_ops->nopage))
1053deceb6cdSHugh Dickins 			foll_flags |= FOLL_ANON;
1054deceb6cdSHugh Dickins 
10551da177e4SLinus Torvalds 		do {
105608ef4729SHugh Dickins 			struct page *page;
10571da177e4SLinus Torvalds 
1058deceb6cdSHugh Dickins 			if (write)
1059deceb6cdSHugh Dickins 				foll_flags |= FOLL_WRITE;
1060deceb6cdSHugh Dickins 
1061deceb6cdSHugh Dickins 			cond_resched();
10626aab341eSLinus Torvalds 			while (!(page = follow_page(vma, start, foll_flags))) {
1063a68d2ebcSLinus Torvalds 				int ret;
1064deceb6cdSHugh Dickins 				ret = __handle_mm_fault(mm, vma, start,
1065deceb6cdSHugh Dickins 						foll_flags & FOLL_WRITE);
1066f33ea7f4SNick Piggin 				/*
1067a68d2ebcSLinus Torvalds 				 * The VM_FAULT_WRITE bit tells us that do_wp_page has
1068a68d2ebcSLinus Torvalds 				 * broken COW when necessary, even if maybe_mkwrite
1069a68d2ebcSLinus Torvalds 				 * decided not to set pte_write. We can thus safely do
1070a68d2ebcSLinus Torvalds 				 * subsequent page lookups as if they were reads.
1071f33ea7f4SNick Piggin 				 */
1072a68d2ebcSLinus Torvalds 				if (ret & VM_FAULT_WRITE)
1073deceb6cdSHugh Dickins 					foll_flags &= ~FOLL_WRITE;
1074a68d2ebcSLinus Torvalds 
1075a68d2ebcSLinus Torvalds 				switch (ret & ~VM_FAULT_WRITE) {
10761da177e4SLinus Torvalds 				case VM_FAULT_MINOR:
10771da177e4SLinus Torvalds 					tsk->min_flt++;
10781da177e4SLinus Torvalds 					break;
10791da177e4SLinus Torvalds 				case VM_FAULT_MAJOR:
10801da177e4SLinus Torvalds 					tsk->maj_flt++;
10811da177e4SLinus Torvalds 					break;
10821da177e4SLinus Torvalds 				case VM_FAULT_SIGBUS:
10831da177e4SLinus Torvalds 					return i ? i : -EFAULT;
10841da177e4SLinus Torvalds 				case VM_FAULT_OOM:
10851da177e4SLinus Torvalds 					return i ? i : -ENOMEM;
10861da177e4SLinus Torvalds 				default:
10871da177e4SLinus Torvalds 					BUG();
10881da177e4SLinus Torvalds 				}
10891da177e4SLinus Torvalds 			}
10901da177e4SLinus Torvalds 			if (pages) {
109108ef4729SHugh Dickins 				pages[i] = page;
109203beb076SJames Bottomley 
109303beb076SJames Bottomley 				flush_anon_page(page, start);
109408ef4729SHugh Dickins 				flush_dcache_page(page);
10951da177e4SLinus Torvalds 			}
10961da177e4SLinus Torvalds 			if (vmas)
10971da177e4SLinus Torvalds 				vmas[i] = vma;
10981da177e4SLinus Torvalds 			i++;
10991da177e4SLinus Torvalds 			start += PAGE_SIZE;
11001da177e4SLinus Torvalds 			len--;
11011da177e4SLinus Torvalds 		} while (len && start < vma->vm_end);
11021da177e4SLinus Torvalds 	} while (len);
11031da177e4SLinus Torvalds 	return i;
11041da177e4SLinus Torvalds }
11051da177e4SLinus Torvalds EXPORT_SYMBOL(get_user_pages);
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds static int zeromap_pte_range(struct mm_struct *mm, pmd_t *pmd,
11081da177e4SLinus Torvalds 			unsigned long addr, unsigned long end, pgprot_t prot)
11091da177e4SLinus Torvalds {
11101da177e4SLinus Torvalds 	pte_t *pte;
1111c74df32cSHugh Dickins 	spinlock_t *ptl;
11121da177e4SLinus Torvalds 
1113c74df32cSHugh Dickins 	pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
11141da177e4SLinus Torvalds 	if (!pte)
11151da177e4SLinus Torvalds 		return -ENOMEM;
1116*6606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
11171da177e4SLinus Torvalds 	do {
1118b5810039SNick Piggin 		struct page *page = ZERO_PAGE(addr);
1119b5810039SNick Piggin 		pte_t zero_pte = pte_wrprotect(mk_pte(page, prot));
1120b5810039SNick Piggin 		page_cache_get(page);
1121b5810039SNick Piggin 		page_add_file_rmap(page);
1122b5810039SNick Piggin 		inc_mm_counter(mm, file_rss);
11231da177e4SLinus Torvalds 		BUG_ON(!pte_none(*pte));
11241da177e4SLinus Torvalds 		set_pte_at(mm, addr, pte, zero_pte);
11251da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
1126*6606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
1127c74df32cSHugh Dickins 	pte_unmap_unlock(pte - 1, ptl);
11281da177e4SLinus Torvalds 	return 0;
11291da177e4SLinus Torvalds }
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds static inline int zeromap_pmd_range(struct mm_struct *mm, pud_t *pud,
11321da177e4SLinus Torvalds 			unsigned long addr, unsigned long end, pgprot_t prot)
11331da177e4SLinus Torvalds {
11341da177e4SLinus Torvalds 	pmd_t *pmd;
11351da177e4SLinus Torvalds 	unsigned long next;
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds 	pmd = pmd_alloc(mm, pud, addr);
11381da177e4SLinus Torvalds 	if (!pmd)
11391da177e4SLinus Torvalds 		return -ENOMEM;
11401da177e4SLinus Torvalds 	do {
11411da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
11421da177e4SLinus Torvalds 		if (zeromap_pte_range(mm, pmd, addr, next, prot))
11431da177e4SLinus Torvalds 			return -ENOMEM;
11441da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
11451da177e4SLinus Torvalds 	return 0;
11461da177e4SLinus Torvalds }
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds static inline int zeromap_pud_range(struct mm_struct *mm, pgd_t *pgd,
11491da177e4SLinus Torvalds 			unsigned long addr, unsigned long end, pgprot_t prot)
11501da177e4SLinus Torvalds {
11511da177e4SLinus Torvalds 	pud_t *pud;
11521da177e4SLinus Torvalds 	unsigned long next;
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds 	pud = pud_alloc(mm, pgd, addr);
11551da177e4SLinus Torvalds 	if (!pud)
11561da177e4SLinus Torvalds 		return -ENOMEM;
11571da177e4SLinus Torvalds 	do {
11581da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
11591da177e4SLinus Torvalds 		if (zeromap_pmd_range(mm, pud, addr, next, prot))
11601da177e4SLinus Torvalds 			return -ENOMEM;
11611da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
11621da177e4SLinus Torvalds 	return 0;
11631da177e4SLinus Torvalds }
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds int zeromap_page_range(struct vm_area_struct *vma,
11661da177e4SLinus Torvalds 			unsigned long addr, unsigned long size, pgprot_t prot)
11671da177e4SLinus Torvalds {
11681da177e4SLinus Torvalds 	pgd_t *pgd;
11691da177e4SLinus Torvalds 	unsigned long next;
11701da177e4SLinus Torvalds 	unsigned long end = addr + size;
11711da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
11721da177e4SLinus Torvalds 	int err;
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds 	BUG_ON(addr >= end);
11751da177e4SLinus Torvalds 	pgd = pgd_offset(mm, addr);
11761da177e4SLinus Torvalds 	flush_cache_range(vma, addr, end);
11771da177e4SLinus Torvalds 	do {
11781da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
11791da177e4SLinus Torvalds 		err = zeromap_pud_range(mm, pgd, addr, next, prot);
11801da177e4SLinus Torvalds 		if (err)
11811da177e4SLinus Torvalds 			break;
11821da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
11831da177e4SLinus Torvalds 	return err;
11841da177e4SLinus Torvalds }
11851da177e4SLinus Torvalds 
118649c91fb0STrond Myklebust pte_t * fastcall get_locked_pte(struct mm_struct *mm, unsigned long addr, spinlock_t **ptl)
1187c9cfcddfSLinus Torvalds {
1188c9cfcddfSLinus Torvalds 	pgd_t * pgd = pgd_offset(mm, addr);
1189c9cfcddfSLinus Torvalds 	pud_t * pud = pud_alloc(mm, pgd, addr);
1190c9cfcddfSLinus Torvalds 	if (pud) {
119149c91fb0STrond Myklebust 		pmd_t * pmd = pmd_alloc(mm, pud, addr);
1192c9cfcddfSLinus Torvalds 		if (pmd)
1193c9cfcddfSLinus Torvalds 			return pte_alloc_map_lock(mm, pmd, addr, ptl);
1194c9cfcddfSLinus Torvalds 	}
1195c9cfcddfSLinus Torvalds 	return NULL;
1196c9cfcddfSLinus Torvalds }
1197c9cfcddfSLinus Torvalds 
11981da177e4SLinus Torvalds /*
1199238f58d8SLinus Torvalds  * This is the old fallback for page remapping.
1200238f58d8SLinus Torvalds  *
1201238f58d8SLinus Torvalds  * For historical reasons, it only allows reserved pages. Only
1202238f58d8SLinus Torvalds  * old drivers should use this, and they needed to mark their
1203238f58d8SLinus Torvalds  * pages reserved for the old functions anyway.
1204238f58d8SLinus Torvalds  */
1205238f58d8SLinus Torvalds static int insert_page(struct mm_struct *mm, unsigned long addr, struct page *page, pgprot_t prot)
1206238f58d8SLinus Torvalds {
1207238f58d8SLinus Torvalds 	int retval;
1208238f58d8SLinus Torvalds 	pte_t *pte;
1209238f58d8SLinus Torvalds 	spinlock_t *ptl;
1210238f58d8SLinus Torvalds 
1211238f58d8SLinus Torvalds 	retval = -EINVAL;
1212a145dd41SLinus Torvalds 	if (PageAnon(page))
1213238f58d8SLinus Torvalds 		goto out;
1214238f58d8SLinus Torvalds 	retval = -ENOMEM;
1215238f58d8SLinus Torvalds 	flush_dcache_page(page);
1216c9cfcddfSLinus Torvalds 	pte = get_locked_pte(mm, addr, &ptl);
1217238f58d8SLinus Torvalds 	if (!pte)
1218238f58d8SLinus Torvalds 		goto out;
1219238f58d8SLinus Torvalds 	retval = -EBUSY;
1220238f58d8SLinus Torvalds 	if (!pte_none(*pte))
1221238f58d8SLinus Torvalds 		goto out_unlock;
1222238f58d8SLinus Torvalds 
1223238f58d8SLinus Torvalds 	/* Ok, finally just insert the thing.. */
1224238f58d8SLinus Torvalds 	get_page(page);
1225238f58d8SLinus Torvalds 	inc_mm_counter(mm, file_rss);
1226238f58d8SLinus Torvalds 	page_add_file_rmap(page);
1227238f58d8SLinus Torvalds 	set_pte_at(mm, addr, pte, mk_pte(page, prot));
1228238f58d8SLinus Torvalds 
1229238f58d8SLinus Torvalds 	retval = 0;
1230238f58d8SLinus Torvalds out_unlock:
1231238f58d8SLinus Torvalds 	pte_unmap_unlock(pte, ptl);
1232238f58d8SLinus Torvalds out:
1233238f58d8SLinus Torvalds 	return retval;
1234238f58d8SLinus Torvalds }
1235238f58d8SLinus Torvalds 
1236bfa5bf6dSRolf Eike Beer /**
1237bfa5bf6dSRolf Eike Beer  * vm_insert_page - insert single page into user vma
1238bfa5bf6dSRolf Eike Beer  * @vma: user vma to map to
1239bfa5bf6dSRolf Eike Beer  * @addr: target user address of this page
1240bfa5bf6dSRolf Eike Beer  * @page: source kernel page
1241bfa5bf6dSRolf Eike Beer  *
1242a145dd41SLinus Torvalds  * This allows drivers to insert individual pages they've allocated
1243a145dd41SLinus Torvalds  * into a user vma.
1244a145dd41SLinus Torvalds  *
1245a145dd41SLinus Torvalds  * The page has to be a nice clean _individual_ kernel allocation.
1246a145dd41SLinus Torvalds  * If you allocate a compound page, you need to have marked it as
1247a145dd41SLinus Torvalds  * such (__GFP_COMP), or manually just split the page up yourself
12488dfcc9baSNick Piggin  * (see split_page()).
1249a145dd41SLinus Torvalds  *
1250a145dd41SLinus Torvalds  * NOTE! Traditionally this was done with "remap_pfn_range()" which
1251a145dd41SLinus Torvalds  * took an arbitrary page protection parameter. This doesn't allow
1252a145dd41SLinus Torvalds  * that. Your vma protection will have to be set up correctly, which
1253a145dd41SLinus Torvalds  * means that if you want a shared writable mapping, you'd better
1254a145dd41SLinus Torvalds  * ask for a shared writable mapping!
1255a145dd41SLinus Torvalds  *
1256a145dd41SLinus Torvalds  * The page does not need to be reserved.
1257a145dd41SLinus Torvalds  */
1258a145dd41SLinus Torvalds int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, struct page *page)
1259a145dd41SLinus Torvalds {
1260a145dd41SLinus Torvalds 	if (addr < vma->vm_start || addr >= vma->vm_end)
1261a145dd41SLinus Torvalds 		return -EFAULT;
1262a145dd41SLinus Torvalds 	if (!page_count(page))
1263a145dd41SLinus Torvalds 		return -EINVAL;
12644d7672b4SLinus Torvalds 	vma->vm_flags |= VM_INSERTPAGE;
1265a145dd41SLinus Torvalds 	return insert_page(vma->vm_mm, addr, page, vma->vm_page_prot);
1266a145dd41SLinus Torvalds }
1267e3c3374fSLinus Torvalds EXPORT_SYMBOL(vm_insert_page);
1268a145dd41SLinus Torvalds 
1269a145dd41SLinus Torvalds /*
12701da177e4SLinus Torvalds  * maps a range of physical memory into the requested pages. the old
12711da177e4SLinus Torvalds  * mappings are removed. any references to nonexistent pages results
12721da177e4SLinus Torvalds  * in null mappings (currently treated as "copy-on-access")
12731da177e4SLinus Torvalds  */
12741da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
12751da177e4SLinus Torvalds 			unsigned long addr, unsigned long end,
12761da177e4SLinus Torvalds 			unsigned long pfn, pgprot_t prot)
12771da177e4SLinus Torvalds {
12781da177e4SLinus Torvalds 	pte_t *pte;
1279c74df32cSHugh Dickins 	spinlock_t *ptl;
12801da177e4SLinus Torvalds 
1281c74df32cSHugh Dickins 	pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
12821da177e4SLinus Torvalds 	if (!pte)
12831da177e4SLinus Torvalds 		return -ENOMEM;
1284*6606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
12851da177e4SLinus Torvalds 	do {
12861da177e4SLinus Torvalds 		BUG_ON(!pte_none(*pte));
12871da177e4SLinus Torvalds 		set_pte_at(mm, addr, pte, pfn_pte(pfn, prot));
12881da177e4SLinus Torvalds 		pfn++;
12891da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
1290*6606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
1291c74df32cSHugh Dickins 	pte_unmap_unlock(pte - 1, ptl);
12921da177e4SLinus Torvalds 	return 0;
12931da177e4SLinus Torvalds }
12941da177e4SLinus Torvalds 
12951da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
12961da177e4SLinus Torvalds 			unsigned long addr, unsigned long end,
12971da177e4SLinus Torvalds 			unsigned long pfn, pgprot_t prot)
12981da177e4SLinus Torvalds {
12991da177e4SLinus Torvalds 	pmd_t *pmd;
13001da177e4SLinus Torvalds 	unsigned long next;
13011da177e4SLinus Torvalds 
13021da177e4SLinus Torvalds 	pfn -= addr >> PAGE_SHIFT;
13031da177e4SLinus Torvalds 	pmd = pmd_alloc(mm, pud, addr);
13041da177e4SLinus Torvalds 	if (!pmd)
13051da177e4SLinus Torvalds 		return -ENOMEM;
13061da177e4SLinus Torvalds 	do {
13071da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
13081da177e4SLinus Torvalds 		if (remap_pte_range(mm, pmd, addr, next,
13091da177e4SLinus Torvalds 				pfn + (addr >> PAGE_SHIFT), prot))
13101da177e4SLinus Torvalds 			return -ENOMEM;
13111da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
13121da177e4SLinus Torvalds 	return 0;
13131da177e4SLinus Torvalds }
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd,
13161da177e4SLinus Torvalds 			unsigned long addr, unsigned long end,
13171da177e4SLinus Torvalds 			unsigned long pfn, pgprot_t prot)
13181da177e4SLinus Torvalds {
13191da177e4SLinus Torvalds 	pud_t *pud;
13201da177e4SLinus Torvalds 	unsigned long next;
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds 	pfn -= addr >> PAGE_SHIFT;
13231da177e4SLinus Torvalds 	pud = pud_alloc(mm, pgd, addr);
13241da177e4SLinus Torvalds 	if (!pud)
13251da177e4SLinus Torvalds 		return -ENOMEM;
13261da177e4SLinus Torvalds 	do {
13271da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
13281da177e4SLinus Torvalds 		if (remap_pmd_range(mm, pud, addr, next,
13291da177e4SLinus Torvalds 				pfn + (addr >> PAGE_SHIFT), prot))
13301da177e4SLinus Torvalds 			return -ENOMEM;
13311da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
13321da177e4SLinus Torvalds 	return 0;
13331da177e4SLinus Torvalds }
13341da177e4SLinus Torvalds 
1335bfa5bf6dSRolf Eike Beer /**
1336bfa5bf6dSRolf Eike Beer  * remap_pfn_range - remap kernel memory to userspace
1337bfa5bf6dSRolf Eike Beer  * @vma: user vma to map to
1338bfa5bf6dSRolf Eike Beer  * @addr: target user address to start at
1339bfa5bf6dSRolf Eike Beer  * @pfn: physical address of kernel memory
1340bfa5bf6dSRolf Eike Beer  * @size: size of map area
1341bfa5bf6dSRolf Eike Beer  * @prot: page protection flags for this mapping
1342bfa5bf6dSRolf Eike Beer  *
1343bfa5bf6dSRolf Eike Beer  *  Note: this is only safe if the mm semaphore is held when called.
1344bfa5bf6dSRolf Eike Beer  */
13451da177e4SLinus Torvalds int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
13461da177e4SLinus Torvalds 		    unsigned long pfn, unsigned long size, pgprot_t prot)
13471da177e4SLinus Torvalds {
13481da177e4SLinus Torvalds 	pgd_t *pgd;
13491da177e4SLinus Torvalds 	unsigned long next;
13502d15cab8SHugh Dickins 	unsigned long end = addr + PAGE_ALIGN(size);
13511da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
13521da177e4SLinus Torvalds 	int err;
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds 	/*
13551da177e4SLinus Torvalds 	 * Physically remapped pages are special. Tell the
13561da177e4SLinus Torvalds 	 * rest of the world about it:
13571da177e4SLinus Torvalds 	 *   VM_IO tells people not to look at these pages
13581da177e4SLinus Torvalds 	 *	(accesses can have side effects).
13590b14c179SHugh Dickins 	 *   VM_RESERVED is specified all over the place, because
13600b14c179SHugh Dickins 	 *	in 2.4 it kept swapout's vma scan off this vma; but
13610b14c179SHugh Dickins 	 *	in 2.6 the LRU scan won't even find its pages, so this
13620b14c179SHugh Dickins 	 *	flag means no more than count its pages in reserved_vm,
13630b14c179SHugh Dickins 	 * 	and omit it from core dump, even when VM_IO turned off.
13646aab341eSLinus Torvalds 	 *   VM_PFNMAP tells the core MM that the base pages are just
13656aab341eSLinus Torvalds 	 *	raw PFN mappings, and do not have a "struct page" associated
13666aab341eSLinus Torvalds 	 *	with them.
1367fb155c16SLinus Torvalds 	 *
1368fb155c16SLinus Torvalds 	 * There's a horrible special case to handle copy-on-write
1369fb155c16SLinus Torvalds 	 * behaviour that some programs depend on. We mark the "original"
1370fb155c16SLinus Torvalds 	 * un-COW'ed pages by matching them up with "vma->vm_pgoff".
13711da177e4SLinus Torvalds 	 */
137267121172SLinus Torvalds 	if (is_cow_mapping(vma->vm_flags)) {
1373fb155c16SLinus Torvalds 		if (addr != vma->vm_start || end != vma->vm_end)
13747fc7e2eeSLinus Torvalds 			return -EINVAL;
13756aab341eSLinus Torvalds 		vma->vm_pgoff = pfn;
1376fb155c16SLinus Torvalds 	}
1377fb155c16SLinus Torvalds 
1378fb155c16SLinus Torvalds 	vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP;
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds 	BUG_ON(addr >= end);
13811da177e4SLinus Torvalds 	pfn -= addr >> PAGE_SHIFT;
13821da177e4SLinus Torvalds 	pgd = pgd_offset(mm, addr);
13831da177e4SLinus Torvalds 	flush_cache_range(vma, addr, end);
13841da177e4SLinus Torvalds 	do {
13851da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
13861da177e4SLinus Torvalds 		err = remap_pud_range(mm, pgd, addr, next,
13871da177e4SLinus Torvalds 				pfn + (addr >> PAGE_SHIFT), prot);
13881da177e4SLinus Torvalds 		if (err)
13891da177e4SLinus Torvalds 			break;
13901da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
13911da177e4SLinus Torvalds 	return err;
13921da177e4SLinus Torvalds }
13931da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range);
13941da177e4SLinus Torvalds 
13951da177e4SLinus Torvalds /*
13968f4e2101SHugh Dickins  * handle_pte_fault chooses page fault handler according to an entry
13978f4e2101SHugh Dickins  * which was read non-atomically.  Before making any commitment, on
13988f4e2101SHugh Dickins  * those architectures or configurations (e.g. i386 with PAE) which
13998f4e2101SHugh Dickins  * might give a mix of unmatched parts, do_swap_page and do_file_page
14008f4e2101SHugh Dickins  * must check under lock before unmapping the pte and proceeding
14018f4e2101SHugh Dickins  * (but do_wp_page is only called after already making such a check;
14028f4e2101SHugh Dickins  * and do_anonymous_page and do_no_page can safely check later on).
14038f4e2101SHugh Dickins  */
14044c21e2f2SHugh Dickins static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
14058f4e2101SHugh Dickins 				pte_t *page_table, pte_t orig_pte)
14068f4e2101SHugh Dickins {
14078f4e2101SHugh Dickins 	int same = 1;
14088f4e2101SHugh Dickins #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
14098f4e2101SHugh Dickins 	if (sizeof(pte_t) > sizeof(unsigned long)) {
14104c21e2f2SHugh Dickins 		spinlock_t *ptl = pte_lockptr(mm, pmd);
14114c21e2f2SHugh Dickins 		spin_lock(ptl);
14128f4e2101SHugh Dickins 		same = pte_same(*page_table, orig_pte);
14134c21e2f2SHugh Dickins 		spin_unlock(ptl);
14148f4e2101SHugh Dickins 	}
14158f4e2101SHugh Dickins #endif
14168f4e2101SHugh Dickins 	pte_unmap(page_table);
14178f4e2101SHugh Dickins 	return same;
14188f4e2101SHugh Dickins }
14198f4e2101SHugh Dickins 
14208f4e2101SHugh Dickins /*
14211da177e4SLinus Torvalds  * Do pte_mkwrite, but only if the vma says VM_WRITE.  We do this when
14221da177e4SLinus Torvalds  * servicing faults for write access.  In the normal case, do always want
14231da177e4SLinus Torvalds  * pte_mkwrite.  But get_user_pages can cause write faults for mappings
14241da177e4SLinus Torvalds  * that do not have writing enabled, when used by access_process_vm.
14251da177e4SLinus Torvalds  */
14261da177e4SLinus Torvalds static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
14271da177e4SLinus Torvalds {
14281da177e4SLinus Torvalds 	if (likely(vma->vm_flags & VM_WRITE))
14291da177e4SLinus Torvalds 		pte = pte_mkwrite(pte);
14301da177e4SLinus Torvalds 	return pte;
14311da177e4SLinus Torvalds }
14321da177e4SLinus Torvalds 
14336aab341eSLinus Torvalds static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va)
14346aab341eSLinus Torvalds {
14356aab341eSLinus Torvalds 	/*
14366aab341eSLinus Torvalds 	 * If the source page was a PFN mapping, we don't have
14376aab341eSLinus Torvalds 	 * a "struct page" for it. We do a best-effort copy by
14386aab341eSLinus Torvalds 	 * just copying from the original user address. If that
14396aab341eSLinus Torvalds 	 * fails, we just zero-fill it. Live with it.
14406aab341eSLinus Torvalds 	 */
14416aab341eSLinus Torvalds 	if (unlikely(!src)) {
14426aab341eSLinus Torvalds 		void *kaddr = kmap_atomic(dst, KM_USER0);
14435d2a2dbbSLinus Torvalds 		void __user *uaddr = (void __user *)(va & PAGE_MASK);
14445d2a2dbbSLinus Torvalds 
14455d2a2dbbSLinus Torvalds 		/*
14465d2a2dbbSLinus Torvalds 		 * This really shouldn't fail, because the page is there
14475d2a2dbbSLinus Torvalds 		 * in the page tables. But it might just be unreadable,
14485d2a2dbbSLinus Torvalds 		 * in which case we just give up and fill the result with
14495d2a2dbbSLinus Torvalds 		 * zeroes.
14505d2a2dbbSLinus Torvalds 		 */
14515d2a2dbbSLinus Torvalds 		if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE))
14526aab341eSLinus Torvalds 			memset(kaddr, 0, PAGE_SIZE);
14536aab341eSLinus Torvalds 		kunmap_atomic(kaddr, KM_USER0);
14546aab341eSLinus Torvalds 		return;
14556aab341eSLinus Torvalds 
14566aab341eSLinus Torvalds 	}
14576aab341eSLinus Torvalds 	copy_user_highpage(dst, src, va);
14586aab341eSLinus Torvalds }
14596aab341eSLinus Torvalds 
14601da177e4SLinus Torvalds /*
14611da177e4SLinus Torvalds  * This routine handles present pages, when users try to write
14621da177e4SLinus Torvalds  * to a shared page. It is done by copying the page to a new address
14631da177e4SLinus Torvalds  * and decrementing the shared-page counter for the old page.
14641da177e4SLinus Torvalds  *
14651da177e4SLinus Torvalds  * Note that this routine assumes that the protection checks have been
14661da177e4SLinus Torvalds  * done by the caller (the low-level page fault routine in most cases).
14671da177e4SLinus Torvalds  * Thus we can safely just mark it writable once we've done any necessary
14681da177e4SLinus Torvalds  * COW.
14691da177e4SLinus Torvalds  *
14701da177e4SLinus Torvalds  * We also mark the page dirty at this point even though the page will
14711da177e4SLinus Torvalds  * change only once the write actually happens. This avoids a few races,
14721da177e4SLinus Torvalds  * and potentially makes it more efficient.
14731da177e4SLinus Torvalds  *
14748f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
14758f4e2101SHugh Dickins  * but allow concurrent faults), with pte both mapped and locked.
14768f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
14771da177e4SLinus Torvalds  */
14781da177e4SLinus Torvalds static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
147965500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
14808f4e2101SHugh Dickins 		spinlock_t *ptl, pte_t orig_pte)
14811da177e4SLinus Torvalds {
1482e5bbe4dfSHugh Dickins 	struct page *old_page, *new_page;
14831da177e4SLinus Torvalds 	pte_t entry;
1484d08b3851SPeter Zijlstra 	int reuse = 0, ret = VM_FAULT_MINOR;
1485d08b3851SPeter Zijlstra 	struct page *dirty_page = NULL;
14861da177e4SLinus Torvalds 
14876aab341eSLinus Torvalds 	old_page = vm_normal_page(vma, address, orig_pte);
14886aab341eSLinus Torvalds 	if (!old_page)
1489920fc356SHugh Dickins 		goto gotten;
14901da177e4SLinus Torvalds 
1491d08b3851SPeter Zijlstra 	/*
1492ee6a6457SPeter Zijlstra 	 * Take out anonymous pages first, anonymous shared vmas are
1493ee6a6457SPeter Zijlstra 	 * not dirty accountable.
1494d08b3851SPeter Zijlstra 	 */
1495ee6a6457SPeter Zijlstra 	if (PageAnon(old_page)) {
1496ee6a6457SPeter Zijlstra 		if (!TestSetPageLocked(old_page)) {
1497ee6a6457SPeter Zijlstra 			reuse = can_share_swap_page(old_page);
1498ee6a6457SPeter Zijlstra 			unlock_page(old_page);
1499ee6a6457SPeter Zijlstra 		}
1500ee6a6457SPeter Zijlstra 	} else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
1501d08b3851SPeter Zijlstra 					(VM_WRITE|VM_SHARED))) {
1502ee6a6457SPeter Zijlstra 		/*
1503ee6a6457SPeter Zijlstra 		 * Only catch write-faults on shared writable pages,
1504ee6a6457SPeter Zijlstra 		 * read-only shared pages can get COWed by
1505ee6a6457SPeter Zijlstra 		 * get_user_pages(.write=1, .force=1).
1506ee6a6457SPeter Zijlstra 		 */
15079637a5efSDavid Howells 		if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
15089637a5efSDavid Howells 			/*
15099637a5efSDavid Howells 			 * Notify the address space that the page is about to
15109637a5efSDavid Howells 			 * become writable so that it can prohibit this or wait
15119637a5efSDavid Howells 			 * for the page to get into an appropriate state.
15129637a5efSDavid Howells 			 *
15139637a5efSDavid Howells 			 * We do this without the lock held, so that it can
15149637a5efSDavid Howells 			 * sleep if it needs to.
15159637a5efSDavid Howells 			 */
15169637a5efSDavid Howells 			page_cache_get(old_page);
15179637a5efSDavid Howells 			pte_unmap_unlock(page_table, ptl);
15189637a5efSDavid Howells 
15199637a5efSDavid Howells 			if (vma->vm_ops->page_mkwrite(vma, old_page) < 0)
15209637a5efSDavid Howells 				goto unwritable_page;
15219637a5efSDavid Howells 
15229637a5efSDavid Howells 			page_cache_release(old_page);
15239637a5efSDavid Howells 
15249637a5efSDavid Howells 			/*
15259637a5efSDavid Howells 			 * Since we dropped the lock we need to revalidate
15269637a5efSDavid Howells 			 * the PTE as someone else may have changed it.  If
15279637a5efSDavid Howells 			 * they did, we just return, as we can count on the
15289637a5efSDavid Howells 			 * MMU to tell us if they didn't also make it writable.
15299637a5efSDavid Howells 			 */
15309637a5efSDavid Howells 			page_table = pte_offset_map_lock(mm, pmd, address,
15319637a5efSDavid Howells 							 &ptl);
15329637a5efSDavid Howells 			if (!pte_same(*page_table, orig_pte))
15339637a5efSDavid Howells 				goto unlock;
15349637a5efSDavid Howells 		}
1535d08b3851SPeter Zijlstra 		dirty_page = old_page;
1536d08b3851SPeter Zijlstra 		get_page(dirty_page);
15379637a5efSDavid Howells 		reuse = 1;
15389637a5efSDavid Howells 	}
15399637a5efSDavid Howells 
15401da177e4SLinus Torvalds 	if (reuse) {
1541eca35133SBen Collins 		flush_cache_page(vma, address, pte_pfn(orig_pte));
154265500d23SHugh Dickins 		entry = pte_mkyoung(orig_pte);
154365500d23SHugh Dickins 		entry = maybe_mkwrite(pte_mkdirty(entry), vma);
15441da177e4SLinus Torvalds 		ptep_set_access_flags(vma, address, page_table, entry, 1);
15451da177e4SLinus Torvalds 		update_mmu_cache(vma, address, entry);
15461da177e4SLinus Torvalds 		lazy_mmu_prot_update(entry);
154765500d23SHugh Dickins 		ret |= VM_FAULT_WRITE;
154865500d23SHugh Dickins 		goto unlock;
15491da177e4SLinus Torvalds 	}
15501da177e4SLinus Torvalds 
15511da177e4SLinus Torvalds 	/*
15521da177e4SLinus Torvalds 	 * Ok, we need to copy. Oh, well..
15531da177e4SLinus Torvalds 	 */
15541da177e4SLinus Torvalds 	page_cache_get(old_page);
1555920fc356SHugh Dickins gotten:
15568f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
15571da177e4SLinus Torvalds 
15581da177e4SLinus Torvalds 	if (unlikely(anon_vma_prepare(vma)))
155965500d23SHugh Dickins 		goto oom;
1560e5bbe4dfSHugh Dickins 	if (old_page == ZERO_PAGE(address)) {
15611da177e4SLinus Torvalds 		new_page = alloc_zeroed_user_highpage(vma, address);
15621da177e4SLinus Torvalds 		if (!new_page)
156365500d23SHugh Dickins 			goto oom;
15641da177e4SLinus Torvalds 	} else {
15651da177e4SLinus Torvalds 		new_page = alloc_page_vma(GFP_HIGHUSER, vma, address);
15661da177e4SLinus Torvalds 		if (!new_page)
156765500d23SHugh Dickins 			goto oom;
1568e5bbe4dfSHugh Dickins 		cow_user_page(new_page, old_page, address);
15691da177e4SLinus Torvalds 	}
157065500d23SHugh Dickins 
15711da177e4SLinus Torvalds 	/*
15721da177e4SLinus Torvalds 	 * Re-check the pte - we dropped the lock
15731da177e4SLinus Torvalds 	 */
15748f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
157565500d23SHugh Dickins 	if (likely(pte_same(*page_table, orig_pte))) {
1576920fc356SHugh Dickins 		if (old_page) {
15771da177e4SLinus Torvalds 			page_remove_rmap(old_page);
15784294621fSHugh Dickins 			if (!PageAnon(old_page)) {
15794294621fSHugh Dickins 				dec_mm_counter(mm, file_rss);
1580920fc356SHugh Dickins 				inc_mm_counter(mm, anon_rss);
15814294621fSHugh Dickins 			}
1582920fc356SHugh Dickins 		} else
1583920fc356SHugh Dickins 			inc_mm_counter(mm, anon_rss);
1584eca35133SBen Collins 		flush_cache_page(vma, address, pte_pfn(orig_pte));
158565500d23SHugh Dickins 		entry = mk_pte(new_page, vma->vm_page_prot);
158665500d23SHugh Dickins 		entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1587c38c8db7SAnil Keshavamurthy 		lazy_mmu_prot_update(entry);
15884ce072f1SSiddha, Suresh B 		/*
15894ce072f1SSiddha, Suresh B 		 * Clear the pte entry and flush it first, before updating the
15904ce072f1SSiddha, Suresh B 		 * pte with the new entry. This will avoid a race condition
15914ce072f1SSiddha, Suresh B 		 * seen in the presence of one thread doing SMC and another
15924ce072f1SSiddha, Suresh B 		 * thread doing COW.
15934ce072f1SSiddha, Suresh B 		 */
15944ce072f1SSiddha, Suresh B 		ptep_clear_flush(vma, address, page_table);
15954ce072f1SSiddha, Suresh B 		set_pte_at(mm, address, page_table, entry);
159665500d23SHugh Dickins 		update_mmu_cache(vma, address, entry);
15971da177e4SLinus Torvalds 		lru_cache_add_active(new_page);
15989617d95eSNick Piggin 		page_add_new_anon_rmap(new_page, vma, address);
15991da177e4SLinus Torvalds 
16001da177e4SLinus Torvalds 		/* Free the old page.. */
16011da177e4SLinus Torvalds 		new_page = old_page;
1602f33ea7f4SNick Piggin 		ret |= VM_FAULT_WRITE;
16031da177e4SLinus Torvalds 	}
1604920fc356SHugh Dickins 	if (new_page)
16051da177e4SLinus Torvalds 		page_cache_release(new_page);
1606920fc356SHugh Dickins 	if (old_page)
16071da177e4SLinus Torvalds 		page_cache_release(old_page);
160865500d23SHugh Dickins unlock:
16098f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
1610d08b3851SPeter Zijlstra 	if (dirty_page) {
1611edc79b2aSPeter Zijlstra 		set_page_dirty_balance(dirty_page);
1612d08b3851SPeter Zijlstra 		put_page(dirty_page);
1613d08b3851SPeter Zijlstra 	}
1614f33ea7f4SNick Piggin 	return ret;
161565500d23SHugh Dickins oom:
1616920fc356SHugh Dickins 	if (old_page)
16171da177e4SLinus Torvalds 		page_cache_release(old_page);
16181da177e4SLinus Torvalds 	return VM_FAULT_OOM;
16199637a5efSDavid Howells 
16209637a5efSDavid Howells unwritable_page:
16219637a5efSDavid Howells 	page_cache_release(old_page);
16229637a5efSDavid Howells 	return VM_FAULT_SIGBUS;
16231da177e4SLinus Torvalds }
16241da177e4SLinus Torvalds 
16251da177e4SLinus Torvalds /*
16261da177e4SLinus Torvalds  * Helper functions for unmap_mapping_range().
16271da177e4SLinus Torvalds  *
16281da177e4SLinus Torvalds  * __ Notes on dropping i_mmap_lock to reduce latency while unmapping __
16291da177e4SLinus Torvalds  *
16301da177e4SLinus Torvalds  * We have to restart searching the prio_tree whenever we drop the lock,
16311da177e4SLinus Torvalds  * since the iterator is only valid while the lock is held, and anyway
16321da177e4SLinus Torvalds  * a later vma might be split and reinserted earlier while lock dropped.
16331da177e4SLinus Torvalds  *
16341da177e4SLinus Torvalds  * The list of nonlinear vmas could be handled more efficiently, using
16351da177e4SLinus Torvalds  * a placeholder, but handle it in the same way until a need is shown.
16361da177e4SLinus Torvalds  * It is important to search the prio_tree before nonlinear list: a vma
16371da177e4SLinus Torvalds  * may become nonlinear and be shifted from prio_tree to nonlinear list
16381da177e4SLinus Torvalds  * while the lock is dropped; but never shifted from list to prio_tree.
16391da177e4SLinus Torvalds  *
16401da177e4SLinus Torvalds  * In order to make forward progress despite restarting the search,
16411da177e4SLinus Torvalds  * vm_truncate_count is used to mark a vma as now dealt with, so we can
16421da177e4SLinus Torvalds  * quickly skip it next time around.  Since the prio_tree search only
16431da177e4SLinus Torvalds  * shows us those vmas affected by unmapping the range in question, we
16441da177e4SLinus Torvalds  * can't efficiently keep all vmas in step with mapping->truncate_count:
16451da177e4SLinus Torvalds  * so instead reset them all whenever it wraps back to 0 (then go to 1).
16461da177e4SLinus Torvalds  * mapping->truncate_count and vma->vm_truncate_count are protected by
16471da177e4SLinus Torvalds  * i_mmap_lock.
16481da177e4SLinus Torvalds  *
16491da177e4SLinus Torvalds  * In order to make forward progress despite repeatedly restarting some
1650ee39b37bSHugh Dickins  * large vma, note the restart_addr from unmap_vmas when it breaks out:
16511da177e4SLinus Torvalds  * and restart from that address when we reach that vma again.  It might
16521da177e4SLinus Torvalds  * have been split or merged, shrunk or extended, but never shifted: so
16531da177e4SLinus Torvalds  * restart_addr remains valid so long as it remains in the vma's range.
16541da177e4SLinus Torvalds  * unmap_mapping_range forces truncate_count to leap over page-aligned
16551da177e4SLinus Torvalds  * values so we can save vma's restart_addr in its truncate_count field.
16561da177e4SLinus Torvalds  */
16571da177e4SLinus Torvalds #define is_restart_addr(truncate_count) (!((truncate_count) & ~PAGE_MASK))
16581da177e4SLinus Torvalds 
16591da177e4SLinus Torvalds static void reset_vma_truncate_counts(struct address_space *mapping)
16601da177e4SLinus Torvalds {
16611da177e4SLinus Torvalds 	struct vm_area_struct *vma;
16621da177e4SLinus Torvalds 	struct prio_tree_iter iter;
16631da177e4SLinus Torvalds 
16641da177e4SLinus Torvalds 	vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, ULONG_MAX)
16651da177e4SLinus Torvalds 		vma->vm_truncate_count = 0;
16661da177e4SLinus Torvalds 	list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
16671da177e4SLinus Torvalds 		vma->vm_truncate_count = 0;
16681da177e4SLinus Torvalds }
16691da177e4SLinus Torvalds 
16701da177e4SLinus Torvalds static int unmap_mapping_range_vma(struct vm_area_struct *vma,
16711da177e4SLinus Torvalds 		unsigned long start_addr, unsigned long end_addr,
16721da177e4SLinus Torvalds 		struct zap_details *details)
16731da177e4SLinus Torvalds {
16741da177e4SLinus Torvalds 	unsigned long restart_addr;
16751da177e4SLinus Torvalds 	int need_break;
16761da177e4SLinus Torvalds 
16771da177e4SLinus Torvalds again:
16781da177e4SLinus Torvalds 	restart_addr = vma->vm_truncate_count;
16791da177e4SLinus Torvalds 	if (is_restart_addr(restart_addr) && start_addr < restart_addr) {
16801da177e4SLinus Torvalds 		start_addr = restart_addr;
16811da177e4SLinus Torvalds 		if (start_addr >= end_addr) {
16821da177e4SLinus Torvalds 			/* Top of vma has been split off since last time */
16831da177e4SLinus Torvalds 			vma->vm_truncate_count = details->truncate_count;
16841da177e4SLinus Torvalds 			return 0;
16851da177e4SLinus Torvalds 		}
16861da177e4SLinus Torvalds 	}
16871da177e4SLinus Torvalds 
1688ee39b37bSHugh Dickins 	restart_addr = zap_page_range(vma, start_addr,
1689ee39b37bSHugh Dickins 					end_addr - start_addr, details);
16901da177e4SLinus Torvalds 	need_break = need_resched() ||
16911da177e4SLinus Torvalds 			need_lockbreak(details->i_mmap_lock);
16921da177e4SLinus Torvalds 
1693ee39b37bSHugh Dickins 	if (restart_addr >= end_addr) {
16941da177e4SLinus Torvalds 		/* We have now completed this vma: mark it so */
16951da177e4SLinus Torvalds 		vma->vm_truncate_count = details->truncate_count;
16961da177e4SLinus Torvalds 		if (!need_break)
16971da177e4SLinus Torvalds 			return 0;
16981da177e4SLinus Torvalds 	} else {
16991da177e4SLinus Torvalds 		/* Note restart_addr in vma's truncate_count field */
1700ee39b37bSHugh Dickins 		vma->vm_truncate_count = restart_addr;
17011da177e4SLinus Torvalds 		if (!need_break)
17021da177e4SLinus Torvalds 			goto again;
17031da177e4SLinus Torvalds 	}
17041da177e4SLinus Torvalds 
17051da177e4SLinus Torvalds 	spin_unlock(details->i_mmap_lock);
17061da177e4SLinus Torvalds 	cond_resched();
17071da177e4SLinus Torvalds 	spin_lock(details->i_mmap_lock);
17081da177e4SLinus Torvalds 	return -EINTR;
17091da177e4SLinus Torvalds }
17101da177e4SLinus Torvalds 
17111da177e4SLinus Torvalds static inline void unmap_mapping_range_tree(struct prio_tree_root *root,
17121da177e4SLinus Torvalds 					    struct zap_details *details)
17131da177e4SLinus Torvalds {
17141da177e4SLinus Torvalds 	struct vm_area_struct *vma;
17151da177e4SLinus Torvalds 	struct prio_tree_iter iter;
17161da177e4SLinus Torvalds 	pgoff_t vba, vea, zba, zea;
17171da177e4SLinus Torvalds 
17181da177e4SLinus Torvalds restart:
17191da177e4SLinus Torvalds 	vma_prio_tree_foreach(vma, &iter, root,
17201da177e4SLinus Torvalds 			details->first_index, details->last_index) {
17211da177e4SLinus Torvalds 		/* Skip quickly over those we have already dealt with */
17221da177e4SLinus Torvalds 		if (vma->vm_truncate_count == details->truncate_count)
17231da177e4SLinus Torvalds 			continue;
17241da177e4SLinus Torvalds 
17251da177e4SLinus Torvalds 		vba = vma->vm_pgoff;
17261da177e4SLinus Torvalds 		vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1;
17271da177e4SLinus Torvalds 		/* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
17281da177e4SLinus Torvalds 		zba = details->first_index;
17291da177e4SLinus Torvalds 		if (zba < vba)
17301da177e4SLinus Torvalds 			zba = vba;
17311da177e4SLinus Torvalds 		zea = details->last_index;
17321da177e4SLinus Torvalds 		if (zea > vea)
17331da177e4SLinus Torvalds 			zea = vea;
17341da177e4SLinus Torvalds 
17351da177e4SLinus Torvalds 		if (unmap_mapping_range_vma(vma,
17361da177e4SLinus Torvalds 			((zba - vba) << PAGE_SHIFT) + vma->vm_start,
17371da177e4SLinus Torvalds 			((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
17381da177e4SLinus Torvalds 				details) < 0)
17391da177e4SLinus Torvalds 			goto restart;
17401da177e4SLinus Torvalds 	}
17411da177e4SLinus Torvalds }
17421da177e4SLinus Torvalds 
17431da177e4SLinus Torvalds static inline void unmap_mapping_range_list(struct list_head *head,
17441da177e4SLinus Torvalds 					    struct zap_details *details)
17451da177e4SLinus Torvalds {
17461da177e4SLinus Torvalds 	struct vm_area_struct *vma;
17471da177e4SLinus Torvalds 
17481da177e4SLinus Torvalds 	/*
17491da177e4SLinus Torvalds 	 * In nonlinear VMAs there is no correspondence between virtual address
17501da177e4SLinus Torvalds 	 * offset and file offset.  So we must perform an exhaustive search
17511da177e4SLinus Torvalds 	 * across *all* the pages in each nonlinear VMA, not just the pages
17521da177e4SLinus Torvalds 	 * whose virtual address lies outside the file truncation point.
17531da177e4SLinus Torvalds 	 */
17541da177e4SLinus Torvalds restart:
17551da177e4SLinus Torvalds 	list_for_each_entry(vma, head, shared.vm_set.list) {
17561da177e4SLinus Torvalds 		/* Skip quickly over those we have already dealt with */
17571da177e4SLinus Torvalds 		if (vma->vm_truncate_count == details->truncate_count)
17581da177e4SLinus Torvalds 			continue;
17591da177e4SLinus Torvalds 		details->nonlinear_vma = vma;
17601da177e4SLinus Torvalds 		if (unmap_mapping_range_vma(vma, vma->vm_start,
17611da177e4SLinus Torvalds 					vma->vm_end, details) < 0)
17621da177e4SLinus Torvalds 			goto restart;
17631da177e4SLinus Torvalds 	}
17641da177e4SLinus Torvalds }
17651da177e4SLinus Torvalds 
17661da177e4SLinus Torvalds /**
17671da177e4SLinus Torvalds  * unmap_mapping_range - unmap the portion of all mmaps
17681da177e4SLinus Torvalds  * in the specified address_space corresponding to the specified
17691da177e4SLinus Torvalds  * page range in the underlying file.
17703d41088fSMartin Waitz  * @mapping: the address space containing mmaps to be unmapped.
17711da177e4SLinus Torvalds  * @holebegin: byte in first page to unmap, relative to the start of
17721da177e4SLinus Torvalds  * the underlying file.  This will be rounded down to a PAGE_SIZE
17731da177e4SLinus Torvalds  * boundary.  Note that this is different from vmtruncate(), which
17741da177e4SLinus Torvalds  * must keep the partial page.  In contrast, we must get rid of
17751da177e4SLinus Torvalds  * partial pages.
17761da177e4SLinus Torvalds  * @holelen: size of prospective hole in bytes.  This will be rounded
17771da177e4SLinus Torvalds  * up to a PAGE_SIZE boundary.  A holelen of zero truncates to the
17781da177e4SLinus Torvalds  * end of the file.
17791da177e4SLinus Torvalds  * @even_cows: 1 when truncating a file, unmap even private COWed pages;
17801da177e4SLinus Torvalds  * but 0 when invalidating pagecache, don't throw away private data.
17811da177e4SLinus Torvalds  */
17821da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping,
17831da177e4SLinus Torvalds 		loff_t const holebegin, loff_t const holelen, int even_cows)
17841da177e4SLinus Torvalds {
17851da177e4SLinus Torvalds 	struct zap_details details;
17861da177e4SLinus Torvalds 	pgoff_t hba = holebegin >> PAGE_SHIFT;
17871da177e4SLinus Torvalds 	pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
17881da177e4SLinus Torvalds 
17891da177e4SLinus Torvalds 	/* Check for overflow. */
17901da177e4SLinus Torvalds 	if (sizeof(holelen) > sizeof(hlen)) {
17911da177e4SLinus Torvalds 		long long holeend =
17921da177e4SLinus Torvalds 			(holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
17931da177e4SLinus Torvalds 		if (holeend & ~(long long)ULONG_MAX)
17941da177e4SLinus Torvalds 			hlen = ULONG_MAX - hba + 1;
17951da177e4SLinus Torvalds 	}
17961da177e4SLinus Torvalds 
17971da177e4SLinus Torvalds 	details.check_mapping = even_cows? NULL: mapping;
17981da177e4SLinus Torvalds 	details.nonlinear_vma = NULL;
17991da177e4SLinus Torvalds 	details.first_index = hba;
18001da177e4SLinus Torvalds 	details.last_index = hba + hlen - 1;
18011da177e4SLinus Torvalds 	if (details.last_index < details.first_index)
18021da177e4SLinus Torvalds 		details.last_index = ULONG_MAX;
18031da177e4SLinus Torvalds 	details.i_mmap_lock = &mapping->i_mmap_lock;
18041da177e4SLinus Torvalds 
18051da177e4SLinus Torvalds 	spin_lock(&mapping->i_mmap_lock);
18061da177e4SLinus Torvalds 
18071da177e4SLinus Torvalds 	/* serialize i_size write against truncate_count write */
18081da177e4SLinus Torvalds 	smp_wmb();
18091da177e4SLinus Torvalds 	/* Protect against page faults, and endless unmapping loops */
18101da177e4SLinus Torvalds 	mapping->truncate_count++;
18111da177e4SLinus Torvalds 	/*
18121da177e4SLinus Torvalds 	 * For archs where spin_lock has inclusive semantics like ia64
18131da177e4SLinus Torvalds 	 * this smp_mb() will prevent to read pagetable contents
18141da177e4SLinus Torvalds 	 * before the truncate_count increment is visible to
18151da177e4SLinus Torvalds 	 * other cpus.
18161da177e4SLinus Torvalds 	 */
18171da177e4SLinus Torvalds 	smp_mb();
18181da177e4SLinus Torvalds 	if (unlikely(is_restart_addr(mapping->truncate_count))) {
18191da177e4SLinus Torvalds 		if (mapping->truncate_count == 0)
18201da177e4SLinus Torvalds 			reset_vma_truncate_counts(mapping);
18211da177e4SLinus Torvalds 		mapping->truncate_count++;
18221da177e4SLinus Torvalds 	}
18231da177e4SLinus Torvalds 	details.truncate_count = mapping->truncate_count;
18241da177e4SLinus Torvalds 
18251da177e4SLinus Torvalds 	if (unlikely(!prio_tree_empty(&mapping->i_mmap)))
18261da177e4SLinus Torvalds 		unmap_mapping_range_tree(&mapping->i_mmap, &details);
18271da177e4SLinus Torvalds 	if (unlikely(!list_empty(&mapping->i_mmap_nonlinear)))
18281da177e4SLinus Torvalds 		unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details);
18291da177e4SLinus Torvalds 	spin_unlock(&mapping->i_mmap_lock);
18301da177e4SLinus Torvalds }
18311da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range);
18321da177e4SLinus Torvalds 
1833bfa5bf6dSRolf Eike Beer /**
1834bfa5bf6dSRolf Eike Beer  * vmtruncate - unmap mappings "freed" by truncate() syscall
1835bfa5bf6dSRolf Eike Beer  * @inode: inode of the file used
1836bfa5bf6dSRolf Eike Beer  * @offset: file offset to start truncating
18371da177e4SLinus Torvalds  *
18381da177e4SLinus Torvalds  * NOTE! We have to be ready to update the memory sharing
18391da177e4SLinus Torvalds  * between the file and the memory map for a potential last
18401da177e4SLinus Torvalds  * incomplete page.  Ugly, but necessary.
18411da177e4SLinus Torvalds  */
18421da177e4SLinus Torvalds int vmtruncate(struct inode * inode, loff_t offset)
18431da177e4SLinus Torvalds {
18441da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
18451da177e4SLinus Torvalds 	unsigned long limit;
18461da177e4SLinus Torvalds 
18471da177e4SLinus Torvalds 	if (inode->i_size < offset)
18481da177e4SLinus Torvalds 		goto do_expand;
18491da177e4SLinus Torvalds 	/*
18501da177e4SLinus Torvalds 	 * truncation of in-use swapfiles is disallowed - it would cause
18511da177e4SLinus Torvalds 	 * subsequent swapout to scribble on the now-freed blocks.
18521da177e4SLinus Torvalds 	 */
18531da177e4SLinus Torvalds 	if (IS_SWAPFILE(inode))
18541da177e4SLinus Torvalds 		goto out_busy;
18551da177e4SLinus Torvalds 	i_size_write(inode, offset);
18561da177e4SLinus Torvalds 	unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
18571da177e4SLinus Torvalds 	truncate_inode_pages(mapping, offset);
18581da177e4SLinus Torvalds 	goto out_truncate;
18591da177e4SLinus Torvalds 
18601da177e4SLinus Torvalds do_expand:
18611da177e4SLinus Torvalds 	limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
18621da177e4SLinus Torvalds 	if (limit != RLIM_INFINITY && offset > limit)
18631da177e4SLinus Torvalds 		goto out_sig;
18641da177e4SLinus Torvalds 	if (offset > inode->i_sb->s_maxbytes)
18651da177e4SLinus Torvalds 		goto out_big;
18661da177e4SLinus Torvalds 	i_size_write(inode, offset);
18671da177e4SLinus Torvalds 
18681da177e4SLinus Torvalds out_truncate:
18691da177e4SLinus Torvalds 	if (inode->i_op && inode->i_op->truncate)
18701da177e4SLinus Torvalds 		inode->i_op->truncate(inode);
18711da177e4SLinus Torvalds 	return 0;
18721da177e4SLinus Torvalds out_sig:
18731da177e4SLinus Torvalds 	send_sig(SIGXFSZ, current, 0);
18741da177e4SLinus Torvalds out_big:
18751da177e4SLinus Torvalds 	return -EFBIG;
18761da177e4SLinus Torvalds out_busy:
18771da177e4SLinus Torvalds 	return -ETXTBSY;
18781da177e4SLinus Torvalds }
18791da177e4SLinus Torvalds EXPORT_SYMBOL(vmtruncate);
18801da177e4SLinus Torvalds 
1881f6b3ec23SBadari Pulavarty int vmtruncate_range(struct inode *inode, loff_t offset, loff_t end)
1882f6b3ec23SBadari Pulavarty {
1883f6b3ec23SBadari Pulavarty 	struct address_space *mapping = inode->i_mapping;
1884f6b3ec23SBadari Pulavarty 
1885f6b3ec23SBadari Pulavarty 	/*
1886f6b3ec23SBadari Pulavarty 	 * If the underlying filesystem is not going to provide
1887f6b3ec23SBadari Pulavarty 	 * a way to truncate a range of blocks (punch a hole) -
1888f6b3ec23SBadari Pulavarty 	 * we should return failure right now.
1889f6b3ec23SBadari Pulavarty 	 */
1890f6b3ec23SBadari Pulavarty 	if (!inode->i_op || !inode->i_op->truncate_range)
1891f6b3ec23SBadari Pulavarty 		return -ENOSYS;
1892f6b3ec23SBadari Pulavarty 
18931b1dcc1bSJes Sorensen 	mutex_lock(&inode->i_mutex);
1894f6b3ec23SBadari Pulavarty 	down_write(&inode->i_alloc_sem);
1895f6b3ec23SBadari Pulavarty 	unmap_mapping_range(mapping, offset, (end - offset), 1);
1896f6b3ec23SBadari Pulavarty 	truncate_inode_pages_range(mapping, offset, end);
1897f6b3ec23SBadari Pulavarty 	inode->i_op->truncate_range(inode, offset, end);
1898f6b3ec23SBadari Pulavarty 	up_write(&inode->i_alloc_sem);
18991b1dcc1bSJes Sorensen 	mutex_unlock(&inode->i_mutex);
1900f6b3ec23SBadari Pulavarty 
1901f6b3ec23SBadari Pulavarty 	return 0;
1902f6b3ec23SBadari Pulavarty }
190326fc5236SAdrian Bunk EXPORT_UNUSED_SYMBOL(vmtruncate_range);  /*  June 2006  */
1904f6b3ec23SBadari Pulavarty 
1905bfa5bf6dSRolf Eike Beer /**
1906bfa5bf6dSRolf Eike Beer  * swapin_readahead - swap in pages in hope we need them soon
1907bfa5bf6dSRolf Eike Beer  * @entry: swap entry of this memory
1908bfa5bf6dSRolf Eike Beer  * @addr: address to start
1909bfa5bf6dSRolf Eike Beer  * @vma: user vma this addresses belong to
1910bfa5bf6dSRolf Eike Beer  *
19111da177e4SLinus Torvalds  * Primitive swap readahead code. We simply read an aligned block of
19121da177e4SLinus Torvalds  * (1 << page_cluster) entries in the swap area. This method is chosen
19131da177e4SLinus Torvalds  * because it doesn't cost us any seek time.  We also make sure to queue
19141da177e4SLinus Torvalds  * the 'original' request together with the readahead ones...
19151da177e4SLinus Torvalds  *
19161da177e4SLinus Torvalds  * This has been extended to use the NUMA policies from the mm triggering
19171da177e4SLinus Torvalds  * the readahead.
19181da177e4SLinus Torvalds  *
19191da177e4SLinus Torvalds  * Caller must hold down_read on the vma->vm_mm if vma is not NULL.
19201da177e4SLinus Torvalds  */
19211da177e4SLinus Torvalds void swapin_readahead(swp_entry_t entry, unsigned long addr,struct vm_area_struct *vma)
19221da177e4SLinus Torvalds {
19231da177e4SLinus Torvalds #ifdef CONFIG_NUMA
19241da177e4SLinus Torvalds 	struct vm_area_struct *next_vma = vma ? vma->vm_next : NULL;
19251da177e4SLinus Torvalds #endif
19261da177e4SLinus Torvalds 	int i, num;
19271da177e4SLinus Torvalds 	struct page *new_page;
19281da177e4SLinus Torvalds 	unsigned long offset;
19291da177e4SLinus Torvalds 
19301da177e4SLinus Torvalds 	/*
19311da177e4SLinus Torvalds 	 * Get the number of handles we should do readahead io to.
19321da177e4SLinus Torvalds 	 */
19331da177e4SLinus Torvalds 	num = valid_swaphandles(entry, &offset);
19341da177e4SLinus Torvalds 	for (i = 0; i < num; offset++, i++) {
19351da177e4SLinus Torvalds 		/* Ok, do the async read-ahead now */
19361da177e4SLinus Torvalds 		new_page = read_swap_cache_async(swp_entry(swp_type(entry),
19371da177e4SLinus Torvalds 							   offset), vma, addr);
19381da177e4SLinus Torvalds 		if (!new_page)
19391da177e4SLinus Torvalds 			break;
19401da177e4SLinus Torvalds 		page_cache_release(new_page);
19411da177e4SLinus Torvalds #ifdef CONFIG_NUMA
19421da177e4SLinus Torvalds 		/*
19431da177e4SLinus Torvalds 		 * Find the next applicable VMA for the NUMA policy.
19441da177e4SLinus Torvalds 		 */
19451da177e4SLinus Torvalds 		addr += PAGE_SIZE;
19461da177e4SLinus Torvalds 		if (addr == 0)
19471da177e4SLinus Torvalds 			vma = NULL;
19481da177e4SLinus Torvalds 		if (vma) {
19491da177e4SLinus Torvalds 			if (addr >= vma->vm_end) {
19501da177e4SLinus Torvalds 				vma = next_vma;
19511da177e4SLinus Torvalds 				next_vma = vma ? vma->vm_next : NULL;
19521da177e4SLinus Torvalds 			}
19531da177e4SLinus Torvalds 			if (vma && addr < vma->vm_start)
19541da177e4SLinus Torvalds 				vma = NULL;
19551da177e4SLinus Torvalds 		} else {
19561da177e4SLinus Torvalds 			if (next_vma && addr >= next_vma->vm_start) {
19571da177e4SLinus Torvalds 				vma = next_vma;
19581da177e4SLinus Torvalds 				next_vma = vma->vm_next;
19591da177e4SLinus Torvalds 			}
19601da177e4SLinus Torvalds 		}
19611da177e4SLinus Torvalds #endif
19621da177e4SLinus Torvalds 	}
19631da177e4SLinus Torvalds 	lru_add_drain();	/* Push any new pages onto the LRU now */
19641da177e4SLinus Torvalds }
19651da177e4SLinus Torvalds 
19661da177e4SLinus Torvalds /*
19678f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
19688f4e2101SHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
19698f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
19701da177e4SLinus Torvalds  */
197165500d23SHugh Dickins static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
197265500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
197365500d23SHugh Dickins 		int write_access, pte_t orig_pte)
19741da177e4SLinus Torvalds {
19758f4e2101SHugh Dickins 	spinlock_t *ptl;
19761da177e4SLinus Torvalds 	struct page *page;
197765500d23SHugh Dickins 	swp_entry_t entry;
19781da177e4SLinus Torvalds 	pte_t pte;
19791da177e4SLinus Torvalds 	int ret = VM_FAULT_MINOR;
19801da177e4SLinus Torvalds 
19814c21e2f2SHugh Dickins 	if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
19828f4e2101SHugh Dickins 		goto out;
198365500d23SHugh Dickins 
198465500d23SHugh Dickins 	entry = pte_to_swp_entry(orig_pte);
19850697212aSChristoph Lameter 	if (is_migration_entry(entry)) {
19860697212aSChristoph Lameter 		migration_entry_wait(mm, pmd, address);
19870697212aSChristoph Lameter 		goto out;
19880697212aSChristoph Lameter 	}
19890ff92245SShailabh Nagar 	delayacct_set_flag(DELAYACCT_PF_SWAPIN);
19901da177e4SLinus Torvalds 	page = lookup_swap_cache(entry);
19911da177e4SLinus Torvalds 	if (!page) {
19921da177e4SLinus Torvalds  		swapin_readahead(entry, address, vma);
19931da177e4SLinus Torvalds  		page = read_swap_cache_async(entry, vma, address);
19941da177e4SLinus Torvalds 		if (!page) {
19951da177e4SLinus Torvalds 			/*
19968f4e2101SHugh Dickins 			 * Back out if somebody else faulted in this pte
19978f4e2101SHugh Dickins 			 * while we released the pte lock.
19981da177e4SLinus Torvalds 			 */
19998f4e2101SHugh Dickins 			page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
20001da177e4SLinus Torvalds 			if (likely(pte_same(*page_table, orig_pte)))
20011da177e4SLinus Torvalds 				ret = VM_FAULT_OOM;
20020ff92245SShailabh Nagar 			delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
200365500d23SHugh Dickins 			goto unlock;
20041da177e4SLinus Torvalds 		}
20051da177e4SLinus Torvalds 
20061da177e4SLinus Torvalds 		/* Had to read the page from swap area: Major fault */
20071da177e4SLinus Torvalds 		ret = VM_FAULT_MAJOR;
2008f8891e5eSChristoph Lameter 		count_vm_event(PGMAJFAULT);
20091da177e4SLinus Torvalds 		grab_swap_token();
20101da177e4SLinus Torvalds 	}
20111da177e4SLinus Torvalds 
20120ff92245SShailabh Nagar 	delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
20131da177e4SLinus Torvalds 	mark_page_accessed(page);
20141da177e4SLinus Torvalds 	lock_page(page);
20151da177e4SLinus Torvalds 
20161da177e4SLinus Torvalds 	/*
20178f4e2101SHugh Dickins 	 * Back out if somebody else already faulted in this pte.
20181da177e4SLinus Torvalds 	 */
20198f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
20209e9bef07SHugh Dickins 	if (unlikely(!pte_same(*page_table, orig_pte)))
2021b8107480SKirill Korotaev 		goto out_nomap;
2022b8107480SKirill Korotaev 
2023b8107480SKirill Korotaev 	if (unlikely(!PageUptodate(page))) {
2024b8107480SKirill Korotaev 		ret = VM_FAULT_SIGBUS;
2025b8107480SKirill Korotaev 		goto out_nomap;
20261da177e4SLinus Torvalds 	}
20271da177e4SLinus Torvalds 
20281da177e4SLinus Torvalds 	/* The page isn't present yet, go ahead with the fault. */
20291da177e4SLinus Torvalds 
20304294621fSHugh Dickins 	inc_mm_counter(mm, anon_rss);
20311da177e4SLinus Torvalds 	pte = mk_pte(page, vma->vm_page_prot);
20321da177e4SLinus Torvalds 	if (write_access && can_share_swap_page(page)) {
20331da177e4SLinus Torvalds 		pte = maybe_mkwrite(pte_mkdirty(pte), vma);
20341da177e4SLinus Torvalds 		write_access = 0;
20351da177e4SLinus Torvalds 	}
20361da177e4SLinus Torvalds 
20371da177e4SLinus Torvalds 	flush_icache_page(vma, page);
20381da177e4SLinus Torvalds 	set_pte_at(mm, address, page_table, pte);
20391da177e4SLinus Torvalds 	page_add_anon_rmap(page, vma, address);
20401da177e4SLinus Torvalds 
2041c475a8abSHugh Dickins 	swap_free(entry);
2042c475a8abSHugh Dickins 	if (vm_swap_full())
2043c475a8abSHugh Dickins 		remove_exclusive_swap_page(page);
2044c475a8abSHugh Dickins 	unlock_page(page);
2045c475a8abSHugh Dickins 
20461da177e4SLinus Torvalds 	if (write_access) {
20471da177e4SLinus Torvalds 		if (do_wp_page(mm, vma, address,
20488f4e2101SHugh Dickins 				page_table, pmd, ptl, pte) == VM_FAULT_OOM)
20491da177e4SLinus Torvalds 			ret = VM_FAULT_OOM;
20501da177e4SLinus Torvalds 		goto out;
20511da177e4SLinus Torvalds 	}
20521da177e4SLinus Torvalds 
20531da177e4SLinus Torvalds 	/* No need to invalidate - it was non-present before */
20541da177e4SLinus Torvalds 	update_mmu_cache(vma, address, pte);
20551da177e4SLinus Torvalds 	lazy_mmu_prot_update(pte);
205665500d23SHugh Dickins unlock:
20578f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
20581da177e4SLinus Torvalds out:
20591da177e4SLinus Torvalds 	return ret;
2060b8107480SKirill Korotaev out_nomap:
20618f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
2062b8107480SKirill Korotaev 	unlock_page(page);
2063b8107480SKirill Korotaev 	page_cache_release(page);
206465500d23SHugh Dickins 	return ret;
20651da177e4SLinus Torvalds }
20661da177e4SLinus Torvalds 
20671da177e4SLinus Torvalds /*
20688f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
20698f4e2101SHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
20708f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
20711da177e4SLinus Torvalds  */
207265500d23SHugh Dickins static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
207365500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
207465500d23SHugh Dickins 		int write_access)
20751da177e4SLinus Torvalds {
20768f4e2101SHugh Dickins 	struct page *page;
20778f4e2101SHugh Dickins 	spinlock_t *ptl;
20781da177e4SLinus Torvalds 	pte_t entry;
20791da177e4SLinus Torvalds 
20806aab341eSLinus Torvalds 	if (write_access) {
20811da177e4SLinus Torvalds 		/* Allocate our own private page. */
20821da177e4SLinus Torvalds 		pte_unmap(page_table);
20831da177e4SLinus Torvalds 
20841da177e4SLinus Torvalds 		if (unlikely(anon_vma_prepare(vma)))
208565500d23SHugh Dickins 			goto oom;
208665500d23SHugh Dickins 		page = alloc_zeroed_user_highpage(vma, address);
20871da177e4SLinus Torvalds 		if (!page)
208865500d23SHugh Dickins 			goto oom;
20891da177e4SLinus Torvalds 
209065500d23SHugh Dickins 		entry = mk_pte(page, vma->vm_page_prot);
209165500d23SHugh Dickins 		entry = maybe_mkwrite(pte_mkdirty(entry), vma);
20928f4e2101SHugh Dickins 
20938f4e2101SHugh Dickins 		page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
20948f4e2101SHugh Dickins 		if (!pte_none(*page_table))
20958f4e2101SHugh Dickins 			goto release;
20968f4e2101SHugh Dickins 		inc_mm_counter(mm, anon_rss);
20971da177e4SLinus Torvalds 		lru_cache_add_active(page);
20989617d95eSNick Piggin 		page_add_new_anon_rmap(page, vma, address);
2099b5810039SNick Piggin 	} else {
21008f4e2101SHugh Dickins 		/* Map the ZERO_PAGE - vm_page_prot is readonly */
21018f4e2101SHugh Dickins 		page = ZERO_PAGE(address);
21028f4e2101SHugh Dickins 		page_cache_get(page);
21038f4e2101SHugh Dickins 		entry = mk_pte(page, vma->vm_page_prot);
21048f4e2101SHugh Dickins 
21054c21e2f2SHugh Dickins 		ptl = pte_lockptr(mm, pmd);
21068f4e2101SHugh Dickins 		spin_lock(ptl);
21078f4e2101SHugh Dickins 		if (!pte_none(*page_table))
21088f4e2101SHugh Dickins 			goto release;
2109b5810039SNick Piggin 		inc_mm_counter(mm, file_rss);
2110b5810039SNick Piggin 		page_add_file_rmap(page);
21111da177e4SLinus Torvalds 	}
21121da177e4SLinus Torvalds 
211365500d23SHugh Dickins 	set_pte_at(mm, address, page_table, entry);
21141da177e4SLinus Torvalds 
21151da177e4SLinus Torvalds 	/* No need to invalidate - it was non-present before */
211665500d23SHugh Dickins 	update_mmu_cache(vma, address, entry);
21171da177e4SLinus Torvalds 	lazy_mmu_prot_update(entry);
211865500d23SHugh Dickins unlock:
21198f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
21201da177e4SLinus Torvalds 	return VM_FAULT_MINOR;
21218f4e2101SHugh Dickins release:
21228f4e2101SHugh Dickins 	page_cache_release(page);
21238f4e2101SHugh Dickins 	goto unlock;
212465500d23SHugh Dickins oom:
21251da177e4SLinus Torvalds 	return VM_FAULT_OOM;
21261da177e4SLinus Torvalds }
21271da177e4SLinus Torvalds 
21281da177e4SLinus Torvalds /*
21291da177e4SLinus Torvalds  * do_no_page() tries to create a new page mapping. It aggressively
21301da177e4SLinus Torvalds  * tries to share with existing pages, but makes a separate copy if
21311da177e4SLinus Torvalds  * the "write_access" parameter is true in order to avoid the next
21321da177e4SLinus Torvalds  * page fault.
21331da177e4SLinus Torvalds  *
21341da177e4SLinus Torvalds  * As this is called only for pages that do not currently exist, we
21351da177e4SLinus Torvalds  * do not need to flush old virtual caches or the TLB.
21361da177e4SLinus Torvalds  *
21378f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
21388f4e2101SHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
21398f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
21401da177e4SLinus Torvalds  */
214165500d23SHugh Dickins static int do_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
214265500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
214365500d23SHugh Dickins 		int write_access)
21441da177e4SLinus Torvalds {
21458f4e2101SHugh Dickins 	spinlock_t *ptl;
21461da177e4SLinus Torvalds 	struct page *new_page;
21471da177e4SLinus Torvalds 	struct address_space *mapping = NULL;
21481da177e4SLinus Torvalds 	pte_t entry;
21491da177e4SLinus Torvalds 	unsigned int sequence = 0;
21501da177e4SLinus Torvalds 	int ret = VM_FAULT_MINOR;
21511da177e4SLinus Torvalds 	int anon = 0;
2152d08b3851SPeter Zijlstra 	struct page *dirty_page = NULL;
21531da177e4SLinus Torvalds 
21541da177e4SLinus Torvalds 	pte_unmap(page_table);
2155325f04dbSHugh Dickins 	BUG_ON(vma->vm_flags & VM_PFNMAP);
2156325f04dbSHugh Dickins 
21571da177e4SLinus Torvalds 	if (vma->vm_file) {
21581da177e4SLinus Torvalds 		mapping = vma->vm_file->f_mapping;
21591da177e4SLinus Torvalds 		sequence = mapping->truncate_count;
21601da177e4SLinus Torvalds 		smp_rmb(); /* serializes i_size against truncate_count */
21611da177e4SLinus Torvalds 	}
21621da177e4SLinus Torvalds retry:
21631da177e4SLinus Torvalds 	new_page = vma->vm_ops->nopage(vma, address & PAGE_MASK, &ret);
21641da177e4SLinus Torvalds 	/*
21651da177e4SLinus Torvalds 	 * No smp_rmb is needed here as long as there's a full
21661da177e4SLinus Torvalds 	 * spin_lock/unlock sequence inside the ->nopage callback
21671da177e4SLinus Torvalds 	 * (for the pagecache lookup) that acts as an implicit
21681da177e4SLinus Torvalds 	 * smp_mb() and prevents the i_size read to happen
21691da177e4SLinus Torvalds 	 * after the next truncate_count read.
21701da177e4SLinus Torvalds 	 */
21711da177e4SLinus Torvalds 
21721da177e4SLinus Torvalds 	/* no page was available -- either SIGBUS or OOM */
21731da177e4SLinus Torvalds 	if (new_page == NOPAGE_SIGBUS)
21741da177e4SLinus Torvalds 		return VM_FAULT_SIGBUS;
21751da177e4SLinus Torvalds 	if (new_page == NOPAGE_OOM)
21761da177e4SLinus Torvalds 		return VM_FAULT_OOM;
21771da177e4SLinus Torvalds 
21781da177e4SLinus Torvalds 	/*
21791da177e4SLinus Torvalds 	 * Should we do an early C-O-W break?
21801da177e4SLinus Torvalds 	 */
21819637a5efSDavid Howells 	if (write_access) {
21829637a5efSDavid Howells 		if (!(vma->vm_flags & VM_SHARED)) {
21831da177e4SLinus Torvalds 			struct page *page;
21841da177e4SLinus Torvalds 
21851da177e4SLinus Torvalds 			if (unlikely(anon_vma_prepare(vma)))
21861da177e4SLinus Torvalds 				goto oom;
21871da177e4SLinus Torvalds 			page = alloc_page_vma(GFP_HIGHUSER, vma, address);
21881da177e4SLinus Torvalds 			if (!page)
21891da177e4SLinus Torvalds 				goto oom;
2190325f04dbSHugh Dickins 			copy_user_highpage(page, new_page, address);
21911da177e4SLinus Torvalds 			page_cache_release(new_page);
21921da177e4SLinus Torvalds 			new_page = page;
21931da177e4SLinus Torvalds 			anon = 1;
21949637a5efSDavid Howells 
21959637a5efSDavid Howells 		} else {
21969637a5efSDavid Howells 			/* if the page will be shareable, see if the backing
21979637a5efSDavid Howells 			 * address space wants to know that the page is about
21989637a5efSDavid Howells 			 * to become writable */
21999637a5efSDavid Howells 			if (vma->vm_ops->page_mkwrite &&
22009637a5efSDavid Howells 			    vma->vm_ops->page_mkwrite(vma, new_page) < 0
22019637a5efSDavid Howells 			    ) {
22029637a5efSDavid Howells 				page_cache_release(new_page);
22039637a5efSDavid Howells 				return VM_FAULT_SIGBUS;
22049637a5efSDavid Howells 			}
22059637a5efSDavid Howells 		}
22061da177e4SLinus Torvalds 	}
22071da177e4SLinus Torvalds 
22088f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
22091da177e4SLinus Torvalds 	/*
22101da177e4SLinus Torvalds 	 * For a file-backed vma, someone could have truncated or otherwise
22111da177e4SLinus Torvalds 	 * invalidated this page.  If unmap_mapping_range got called,
22121da177e4SLinus Torvalds 	 * retry getting the page.
22131da177e4SLinus Torvalds 	 */
22141da177e4SLinus Torvalds 	if (mapping && unlikely(sequence != mapping->truncate_count)) {
22158f4e2101SHugh Dickins 		pte_unmap_unlock(page_table, ptl);
22161da177e4SLinus Torvalds 		page_cache_release(new_page);
221765500d23SHugh Dickins 		cond_resched();
221865500d23SHugh Dickins 		sequence = mapping->truncate_count;
221965500d23SHugh Dickins 		smp_rmb();
22201da177e4SLinus Torvalds 		goto retry;
22211da177e4SLinus Torvalds 	}
22221da177e4SLinus Torvalds 
22231da177e4SLinus Torvalds 	/*
22241da177e4SLinus Torvalds 	 * This silly early PAGE_DIRTY setting removes a race
22251da177e4SLinus Torvalds 	 * due to the bad i386 page protection. But it's valid
22261da177e4SLinus Torvalds 	 * for other architectures too.
22271da177e4SLinus Torvalds 	 *
22281da177e4SLinus Torvalds 	 * Note that if write_access is true, we either now have
22291da177e4SLinus Torvalds 	 * an exclusive copy of the page, or this is a shared mapping,
22301da177e4SLinus Torvalds 	 * so we can make it writable and dirty to avoid having to
22311da177e4SLinus Torvalds 	 * handle that later.
22321da177e4SLinus Torvalds 	 */
22331da177e4SLinus Torvalds 	/* Only go through if we didn't race with anybody else... */
22341da177e4SLinus Torvalds 	if (pte_none(*page_table)) {
22351da177e4SLinus Torvalds 		flush_icache_page(vma, new_page);
22361da177e4SLinus Torvalds 		entry = mk_pte(new_page, vma->vm_page_prot);
22371da177e4SLinus Torvalds 		if (write_access)
22381da177e4SLinus Torvalds 			entry = maybe_mkwrite(pte_mkdirty(entry), vma);
22391da177e4SLinus Torvalds 		set_pte_at(mm, address, page_table, entry);
22401da177e4SLinus Torvalds 		if (anon) {
22414294621fSHugh Dickins 			inc_mm_counter(mm, anon_rss);
22421da177e4SLinus Torvalds 			lru_cache_add_active(new_page);
22439617d95eSNick Piggin 			page_add_new_anon_rmap(new_page, vma, address);
2244f57e88a8SHugh Dickins 		} else {
22454294621fSHugh Dickins 			inc_mm_counter(mm, file_rss);
22461da177e4SLinus Torvalds 			page_add_file_rmap(new_page);
2247d08b3851SPeter Zijlstra 			if (write_access) {
2248d08b3851SPeter Zijlstra 				dirty_page = new_page;
2249d08b3851SPeter Zijlstra 				get_page(dirty_page);
2250d08b3851SPeter Zijlstra 			}
22514294621fSHugh Dickins 		}
22521da177e4SLinus Torvalds 	} else {
22531da177e4SLinus Torvalds 		/* One of our sibling threads was faster, back out. */
22541da177e4SLinus Torvalds 		page_cache_release(new_page);
225565500d23SHugh Dickins 		goto unlock;
22561da177e4SLinus Torvalds 	}
22571da177e4SLinus Torvalds 
22581da177e4SLinus Torvalds 	/* no need to invalidate: a not-present page shouldn't be cached */
22591da177e4SLinus Torvalds 	update_mmu_cache(vma, address, entry);
22601da177e4SLinus Torvalds 	lazy_mmu_prot_update(entry);
226165500d23SHugh Dickins unlock:
22628f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
2263d08b3851SPeter Zijlstra 	if (dirty_page) {
2264edc79b2aSPeter Zijlstra 		set_page_dirty_balance(dirty_page);
2265d08b3851SPeter Zijlstra 		put_page(dirty_page);
2266d08b3851SPeter Zijlstra 	}
22671da177e4SLinus Torvalds 	return ret;
22681da177e4SLinus Torvalds oom:
22691da177e4SLinus Torvalds 	page_cache_release(new_page);
227065500d23SHugh Dickins 	return VM_FAULT_OOM;
22711da177e4SLinus Torvalds }
22721da177e4SLinus Torvalds 
22731da177e4SLinus Torvalds /*
2274f4b81804SJes Sorensen  * do_no_pfn() tries to create a new page mapping for a page without
2275f4b81804SJes Sorensen  * a struct_page backing it
2276f4b81804SJes Sorensen  *
2277f4b81804SJes Sorensen  * As this is called only for pages that do not currently exist, we
2278f4b81804SJes Sorensen  * do not need to flush old virtual caches or the TLB.
2279f4b81804SJes Sorensen  *
2280f4b81804SJes Sorensen  * We enter with non-exclusive mmap_sem (to exclude vma changes,
2281f4b81804SJes Sorensen  * but allow concurrent faults), and pte mapped but not yet locked.
2282f4b81804SJes Sorensen  * We return with mmap_sem still held, but pte unmapped and unlocked.
2283f4b81804SJes Sorensen  *
2284f4b81804SJes Sorensen  * It is expected that the ->nopfn handler always returns the same pfn
2285f4b81804SJes Sorensen  * for a given virtual mapping.
2286f4b81804SJes Sorensen  *
2287f4b81804SJes Sorensen  * Mark this `noinline' to prevent it from bloating the main pagefault code.
2288f4b81804SJes Sorensen  */
2289f4b81804SJes Sorensen static noinline int do_no_pfn(struct mm_struct *mm, struct vm_area_struct *vma,
2290f4b81804SJes Sorensen 		     unsigned long address, pte_t *page_table, pmd_t *pmd,
2291f4b81804SJes Sorensen 		     int write_access)
2292f4b81804SJes Sorensen {
2293f4b81804SJes Sorensen 	spinlock_t *ptl;
2294f4b81804SJes Sorensen 	pte_t entry;
2295f4b81804SJes Sorensen 	unsigned long pfn;
2296f4b81804SJes Sorensen 	int ret = VM_FAULT_MINOR;
2297f4b81804SJes Sorensen 
2298f4b81804SJes Sorensen 	pte_unmap(page_table);
2299f4b81804SJes Sorensen 	BUG_ON(!(vma->vm_flags & VM_PFNMAP));
2300f4b81804SJes Sorensen 	BUG_ON(is_cow_mapping(vma->vm_flags));
2301f4b81804SJes Sorensen 
2302f4b81804SJes Sorensen 	pfn = vma->vm_ops->nopfn(vma, address & PAGE_MASK);
2303f4b81804SJes Sorensen 	if (pfn == NOPFN_OOM)
2304f4b81804SJes Sorensen 		return VM_FAULT_OOM;
2305f4b81804SJes Sorensen 	if (pfn == NOPFN_SIGBUS)
2306f4b81804SJes Sorensen 		return VM_FAULT_SIGBUS;
2307f4b81804SJes Sorensen 
2308f4b81804SJes Sorensen 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
2309f4b81804SJes Sorensen 
2310f4b81804SJes Sorensen 	/* Only go through if we didn't race with anybody else... */
2311f4b81804SJes Sorensen 	if (pte_none(*page_table)) {
2312f4b81804SJes Sorensen 		entry = pfn_pte(pfn, vma->vm_page_prot);
2313f4b81804SJes Sorensen 		if (write_access)
2314f4b81804SJes Sorensen 			entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2315f4b81804SJes Sorensen 		set_pte_at(mm, address, page_table, entry);
2316f4b81804SJes Sorensen 	}
2317f4b81804SJes Sorensen 	pte_unmap_unlock(page_table, ptl);
2318f4b81804SJes Sorensen 	return ret;
2319f4b81804SJes Sorensen }
2320f4b81804SJes Sorensen 
2321f4b81804SJes Sorensen /*
23221da177e4SLinus Torvalds  * Fault of a previously existing named mapping. Repopulate the pte
23231da177e4SLinus Torvalds  * from the encoded file_pte if possible. This enables swappable
23241da177e4SLinus Torvalds  * nonlinear vmas.
23258f4e2101SHugh Dickins  *
23268f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
23278f4e2101SHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
23288f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
23291da177e4SLinus Torvalds  */
23301da177e4SLinus Torvalds static int do_file_page(struct mm_struct *mm, struct vm_area_struct *vma,
233165500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
233265500d23SHugh Dickins 		int write_access, pte_t orig_pte)
23331da177e4SLinus Torvalds {
233465500d23SHugh Dickins 	pgoff_t pgoff;
23351da177e4SLinus Torvalds 	int err;
23361da177e4SLinus Torvalds 
23374c21e2f2SHugh Dickins 	if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
23388f4e2101SHugh Dickins 		return VM_FAULT_MINOR;
23391da177e4SLinus Torvalds 
234065500d23SHugh Dickins 	if (unlikely(!(vma->vm_flags & VM_NONLINEAR))) {
234165500d23SHugh Dickins 		/*
234265500d23SHugh Dickins 		 * Page table corrupted: show pte and kill process.
234365500d23SHugh Dickins 		 */
2344b5810039SNick Piggin 		print_bad_pte(vma, orig_pte, address);
234565500d23SHugh Dickins 		return VM_FAULT_OOM;
234665500d23SHugh Dickins 	}
234765500d23SHugh Dickins 	/* We can then assume vm->vm_ops && vma->vm_ops->populate */
234865500d23SHugh Dickins 
234965500d23SHugh Dickins 	pgoff = pte_to_pgoff(orig_pte);
235065500d23SHugh Dickins 	err = vma->vm_ops->populate(vma, address & PAGE_MASK, PAGE_SIZE,
235165500d23SHugh Dickins 					vma->vm_page_prot, pgoff, 0);
23521da177e4SLinus Torvalds 	if (err == -ENOMEM)
23531da177e4SLinus Torvalds 		return VM_FAULT_OOM;
23541da177e4SLinus Torvalds 	if (err)
23551da177e4SLinus Torvalds 		return VM_FAULT_SIGBUS;
23561da177e4SLinus Torvalds 	return VM_FAULT_MAJOR;
23571da177e4SLinus Torvalds }
23581da177e4SLinus Torvalds 
23591da177e4SLinus Torvalds /*
23601da177e4SLinus Torvalds  * These routines also need to handle stuff like marking pages dirty
23611da177e4SLinus Torvalds  * and/or accessed for architectures that don't do it in hardware (most
23621da177e4SLinus Torvalds  * RISC architectures).  The early dirtying is also good on the i386.
23631da177e4SLinus Torvalds  *
23641da177e4SLinus Torvalds  * There is also a hook called "update_mmu_cache()" that architectures
23651da177e4SLinus Torvalds  * with external mmu caches can use to update those (ie the Sparc or
23661da177e4SLinus Torvalds  * PowerPC hashed page tables that act as extended TLBs).
23671da177e4SLinus Torvalds  *
2368c74df32cSHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
2369c74df32cSHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
2370c74df32cSHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
23711da177e4SLinus Torvalds  */
23721da177e4SLinus Torvalds static inline int handle_pte_fault(struct mm_struct *mm,
23731da177e4SLinus Torvalds 		struct vm_area_struct *vma, unsigned long address,
237465500d23SHugh Dickins 		pte_t *pte, pmd_t *pmd, int write_access)
23751da177e4SLinus Torvalds {
23761da177e4SLinus Torvalds 	pte_t entry;
23771a44e149SAndrea Arcangeli 	pte_t old_entry;
23788f4e2101SHugh Dickins 	spinlock_t *ptl;
23791da177e4SLinus Torvalds 
23801a44e149SAndrea Arcangeli 	old_entry = entry = *pte;
23811da177e4SLinus Torvalds 	if (!pte_present(entry)) {
238265500d23SHugh Dickins 		if (pte_none(entry)) {
2383f4b81804SJes Sorensen 			if (vma->vm_ops) {
2384f4b81804SJes Sorensen 				if (vma->vm_ops->nopage)
238565500d23SHugh Dickins 					return do_no_page(mm, vma, address,
2386f4b81804SJes Sorensen 							  pte, pmd,
2387f4b81804SJes Sorensen 							  write_access);
2388f4b81804SJes Sorensen 				if (unlikely(vma->vm_ops->nopfn))
2389f4b81804SJes Sorensen 					return do_no_pfn(mm, vma, address, pte,
2390f4b81804SJes Sorensen 							 pmd, write_access);
2391f4b81804SJes Sorensen 			}
2392f4b81804SJes Sorensen 			return do_anonymous_page(mm, vma, address,
239365500d23SHugh Dickins 						 pte, pmd, write_access);
239465500d23SHugh Dickins 		}
23951da177e4SLinus Torvalds 		if (pte_file(entry))
239665500d23SHugh Dickins 			return do_file_page(mm, vma, address,
239765500d23SHugh Dickins 					pte, pmd, write_access, entry);
239865500d23SHugh Dickins 		return do_swap_page(mm, vma, address,
239965500d23SHugh Dickins 					pte, pmd, write_access, entry);
24001da177e4SLinus Torvalds 	}
24011da177e4SLinus Torvalds 
24024c21e2f2SHugh Dickins 	ptl = pte_lockptr(mm, pmd);
24038f4e2101SHugh Dickins 	spin_lock(ptl);
24048f4e2101SHugh Dickins 	if (unlikely(!pte_same(*pte, entry)))
24058f4e2101SHugh Dickins 		goto unlock;
24061da177e4SLinus Torvalds 	if (write_access) {
24071da177e4SLinus Torvalds 		if (!pte_write(entry))
24088f4e2101SHugh Dickins 			return do_wp_page(mm, vma, address,
24098f4e2101SHugh Dickins 					pte, pmd, ptl, entry);
24101da177e4SLinus Torvalds 		entry = pte_mkdirty(entry);
24111da177e4SLinus Torvalds 	}
24121da177e4SLinus Torvalds 	entry = pte_mkyoung(entry);
24131a44e149SAndrea Arcangeli 	if (!pte_same(old_entry, entry)) {
24141da177e4SLinus Torvalds 		ptep_set_access_flags(vma, address, pte, entry, write_access);
24151da177e4SLinus Torvalds 		update_mmu_cache(vma, address, entry);
24161da177e4SLinus Torvalds 		lazy_mmu_prot_update(entry);
24171a44e149SAndrea Arcangeli 	} else {
24181a44e149SAndrea Arcangeli 		/*
24191a44e149SAndrea Arcangeli 		 * This is needed only for protection faults but the arch code
24201a44e149SAndrea Arcangeli 		 * is not yet telling us if this is a protection fault or not.
24211a44e149SAndrea Arcangeli 		 * This still avoids useless tlb flushes for .text page faults
24221a44e149SAndrea Arcangeli 		 * with threads.
24231a44e149SAndrea Arcangeli 		 */
24241a44e149SAndrea Arcangeli 		if (write_access)
24251a44e149SAndrea Arcangeli 			flush_tlb_page(vma, address);
24261a44e149SAndrea Arcangeli 	}
24278f4e2101SHugh Dickins unlock:
24288f4e2101SHugh Dickins 	pte_unmap_unlock(pte, ptl);
24291da177e4SLinus Torvalds 	return VM_FAULT_MINOR;
24301da177e4SLinus Torvalds }
24311da177e4SLinus Torvalds 
24321da177e4SLinus Torvalds /*
24331da177e4SLinus Torvalds  * By the time we get here, we already hold the mm semaphore
24341da177e4SLinus Torvalds  */
2435f33ea7f4SNick Piggin int __handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
24361da177e4SLinus Torvalds 		unsigned long address, int write_access)
24371da177e4SLinus Torvalds {
24381da177e4SLinus Torvalds 	pgd_t *pgd;
24391da177e4SLinus Torvalds 	pud_t *pud;
24401da177e4SLinus Torvalds 	pmd_t *pmd;
24411da177e4SLinus Torvalds 	pte_t *pte;
24421da177e4SLinus Torvalds 
24431da177e4SLinus Torvalds 	__set_current_state(TASK_RUNNING);
24441da177e4SLinus Torvalds 
2445f8891e5eSChristoph Lameter 	count_vm_event(PGFAULT);
24461da177e4SLinus Torvalds 
2447ac9b9c66SHugh Dickins 	if (unlikely(is_vm_hugetlb_page(vma)))
2448ac9b9c66SHugh Dickins 		return hugetlb_fault(mm, vma, address, write_access);
24491da177e4SLinus Torvalds 
24501da177e4SLinus Torvalds 	pgd = pgd_offset(mm, address);
24511da177e4SLinus Torvalds 	pud = pud_alloc(mm, pgd, address);
24521da177e4SLinus Torvalds 	if (!pud)
2453c74df32cSHugh Dickins 		return VM_FAULT_OOM;
24541da177e4SLinus Torvalds 	pmd = pmd_alloc(mm, pud, address);
24551da177e4SLinus Torvalds 	if (!pmd)
2456c74df32cSHugh Dickins 		return VM_FAULT_OOM;
24571da177e4SLinus Torvalds 	pte = pte_alloc_map(mm, pmd, address);
24581da177e4SLinus Torvalds 	if (!pte)
2459c74df32cSHugh Dickins 		return VM_FAULT_OOM;
24601da177e4SLinus Torvalds 
246165500d23SHugh Dickins 	return handle_pte_fault(mm, vma, address, pte, pmd, write_access);
24621da177e4SLinus Torvalds }
24631da177e4SLinus Torvalds 
246467207b96SArnd Bergmann EXPORT_SYMBOL_GPL(__handle_mm_fault);
246567207b96SArnd Bergmann 
24661da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED
24671da177e4SLinus Torvalds /*
24681da177e4SLinus Torvalds  * Allocate page upper directory.
2469872fec16SHugh Dickins  * We've already handled the fast-path in-line.
24701da177e4SLinus Torvalds  */
24711bb3630eSHugh Dickins int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
24721da177e4SLinus Torvalds {
2473c74df32cSHugh Dickins 	pud_t *new = pud_alloc_one(mm, address);
2474c74df32cSHugh Dickins 	if (!new)
24751bb3630eSHugh Dickins 		return -ENOMEM;
24761da177e4SLinus Torvalds 
2477872fec16SHugh Dickins 	spin_lock(&mm->page_table_lock);
24781bb3630eSHugh Dickins 	if (pgd_present(*pgd))		/* Another has populated it */
24791da177e4SLinus Torvalds 		pud_free(new);
24801bb3630eSHugh Dickins 	else
24811da177e4SLinus Torvalds 		pgd_populate(mm, pgd, new);
2482872fec16SHugh Dickins 	spin_unlock(&mm->page_table_lock);
24831bb3630eSHugh Dickins 	return 0;
24841da177e4SLinus Torvalds }
2485e0f39591SAlan Stern #else
2486e0f39591SAlan Stern /* Workaround for gcc 2.96 */
2487e0f39591SAlan Stern int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
2488e0f39591SAlan Stern {
2489e0f39591SAlan Stern 	return 0;
2490e0f39591SAlan Stern }
24911da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */
24921da177e4SLinus Torvalds 
24931da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED
24941da177e4SLinus Torvalds /*
24951da177e4SLinus Torvalds  * Allocate page middle directory.
2496872fec16SHugh Dickins  * We've already handled the fast-path in-line.
24971da177e4SLinus Torvalds  */
24981bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
24991da177e4SLinus Torvalds {
2500c74df32cSHugh Dickins 	pmd_t *new = pmd_alloc_one(mm, address);
2501c74df32cSHugh Dickins 	if (!new)
25021bb3630eSHugh Dickins 		return -ENOMEM;
25031da177e4SLinus Torvalds 
2504872fec16SHugh Dickins 	spin_lock(&mm->page_table_lock);
25051da177e4SLinus Torvalds #ifndef __ARCH_HAS_4LEVEL_HACK
25061bb3630eSHugh Dickins 	if (pud_present(*pud))		/* Another has populated it */
25071da177e4SLinus Torvalds 		pmd_free(new);
25081bb3630eSHugh Dickins 	else
25091da177e4SLinus Torvalds 		pud_populate(mm, pud, new);
25101da177e4SLinus Torvalds #else
25111bb3630eSHugh Dickins 	if (pgd_present(*pud))		/* Another has populated it */
25121da177e4SLinus Torvalds 		pmd_free(new);
25131bb3630eSHugh Dickins 	else
25141da177e4SLinus Torvalds 		pgd_populate(mm, pud, new);
25151da177e4SLinus Torvalds #endif /* __ARCH_HAS_4LEVEL_HACK */
2516872fec16SHugh Dickins 	spin_unlock(&mm->page_table_lock);
25171bb3630eSHugh Dickins 	return 0;
25181da177e4SLinus Torvalds }
2519e0f39591SAlan Stern #else
2520e0f39591SAlan Stern /* Workaround for gcc 2.96 */
2521e0f39591SAlan Stern int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
2522e0f39591SAlan Stern {
2523e0f39591SAlan Stern 	return 0;
2524e0f39591SAlan Stern }
25251da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */
25261da177e4SLinus Torvalds 
25271da177e4SLinus Torvalds int make_pages_present(unsigned long addr, unsigned long end)
25281da177e4SLinus Torvalds {
25291da177e4SLinus Torvalds 	int ret, len, write;
25301da177e4SLinus Torvalds 	struct vm_area_struct * vma;
25311da177e4SLinus Torvalds 
25321da177e4SLinus Torvalds 	vma = find_vma(current->mm, addr);
25331da177e4SLinus Torvalds 	if (!vma)
25341da177e4SLinus Torvalds 		return -1;
25351da177e4SLinus Torvalds 	write = (vma->vm_flags & VM_WRITE) != 0;
25365bcb28b1SEric Sesterhenn 	BUG_ON(addr >= end);
25375bcb28b1SEric Sesterhenn 	BUG_ON(end > vma->vm_end);
25381da177e4SLinus Torvalds 	len = (end+PAGE_SIZE-1)/PAGE_SIZE-addr/PAGE_SIZE;
25391da177e4SLinus Torvalds 	ret = get_user_pages(current, current->mm, addr,
25401da177e4SLinus Torvalds 			len, write, 0, NULL, NULL);
25411da177e4SLinus Torvalds 	if (ret < 0)
25421da177e4SLinus Torvalds 		return ret;
25431da177e4SLinus Torvalds 	return ret == len ? 0 : -1;
25441da177e4SLinus Torvalds }
25451da177e4SLinus Torvalds 
25461da177e4SLinus Torvalds /*
25471da177e4SLinus Torvalds  * Map a vmalloc()-space virtual address to the physical page.
25481da177e4SLinus Torvalds  */
25491da177e4SLinus Torvalds struct page * vmalloc_to_page(void * vmalloc_addr)
25501da177e4SLinus Torvalds {
25511da177e4SLinus Torvalds 	unsigned long addr = (unsigned long) vmalloc_addr;
25521da177e4SLinus Torvalds 	struct page *page = NULL;
25531da177e4SLinus Torvalds 	pgd_t *pgd = pgd_offset_k(addr);
25541da177e4SLinus Torvalds 	pud_t *pud;
25551da177e4SLinus Torvalds 	pmd_t *pmd;
25561da177e4SLinus Torvalds 	pte_t *ptep, pte;
25571da177e4SLinus Torvalds 
25581da177e4SLinus Torvalds 	if (!pgd_none(*pgd)) {
25591da177e4SLinus Torvalds 		pud = pud_offset(pgd, addr);
25601da177e4SLinus Torvalds 		if (!pud_none(*pud)) {
25611da177e4SLinus Torvalds 			pmd = pmd_offset(pud, addr);
25621da177e4SLinus Torvalds 			if (!pmd_none(*pmd)) {
25631da177e4SLinus Torvalds 				ptep = pte_offset_map(pmd, addr);
25641da177e4SLinus Torvalds 				pte = *ptep;
25651da177e4SLinus Torvalds 				if (pte_present(pte))
25661da177e4SLinus Torvalds 					page = pte_page(pte);
25671da177e4SLinus Torvalds 				pte_unmap(ptep);
25681da177e4SLinus Torvalds 			}
25691da177e4SLinus Torvalds 		}
25701da177e4SLinus Torvalds 	}
25711da177e4SLinus Torvalds 	return page;
25721da177e4SLinus Torvalds }
25731da177e4SLinus Torvalds 
25741da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc_to_page);
25751da177e4SLinus Torvalds 
25761da177e4SLinus Torvalds /*
25771da177e4SLinus Torvalds  * Map a vmalloc()-space virtual address to the physical page frame number.
25781da177e4SLinus Torvalds  */
25791da177e4SLinus Torvalds unsigned long vmalloc_to_pfn(void * vmalloc_addr)
25801da177e4SLinus Torvalds {
25811da177e4SLinus Torvalds 	return page_to_pfn(vmalloc_to_page(vmalloc_addr));
25821da177e4SLinus Torvalds }
25831da177e4SLinus Torvalds 
25841da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc_to_pfn);
25851da177e4SLinus Torvalds 
25861da177e4SLinus Torvalds #if !defined(__HAVE_ARCH_GATE_AREA)
25871da177e4SLinus Torvalds 
25881da177e4SLinus Torvalds #if defined(AT_SYSINFO_EHDR)
25895ce7852cSAdrian Bunk static struct vm_area_struct gate_vma;
25901da177e4SLinus Torvalds 
25911da177e4SLinus Torvalds static int __init gate_vma_init(void)
25921da177e4SLinus Torvalds {
25931da177e4SLinus Torvalds 	gate_vma.vm_mm = NULL;
25941da177e4SLinus Torvalds 	gate_vma.vm_start = FIXADDR_USER_START;
25951da177e4SLinus Torvalds 	gate_vma.vm_end = FIXADDR_USER_END;
25961da177e4SLinus Torvalds 	gate_vma.vm_page_prot = PAGE_READONLY;
25970b14c179SHugh Dickins 	gate_vma.vm_flags = 0;
25981da177e4SLinus Torvalds 	return 0;
25991da177e4SLinus Torvalds }
26001da177e4SLinus Torvalds __initcall(gate_vma_init);
26011da177e4SLinus Torvalds #endif
26021da177e4SLinus Torvalds 
26031da177e4SLinus Torvalds struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
26041da177e4SLinus Torvalds {
26051da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR
26061da177e4SLinus Torvalds 	return &gate_vma;
26071da177e4SLinus Torvalds #else
26081da177e4SLinus Torvalds 	return NULL;
26091da177e4SLinus Torvalds #endif
26101da177e4SLinus Torvalds }
26111da177e4SLinus Torvalds 
26121da177e4SLinus Torvalds int in_gate_area_no_task(unsigned long addr)
26131da177e4SLinus Torvalds {
26141da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR
26151da177e4SLinus Torvalds 	if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))
26161da177e4SLinus Torvalds 		return 1;
26171da177e4SLinus Torvalds #endif
26181da177e4SLinus Torvalds 	return 0;
26191da177e4SLinus Torvalds }
26201da177e4SLinus Torvalds 
26211da177e4SLinus Torvalds #endif	/* __HAVE_ARCH_GATE_AREA */
26220ec76a11SDavid Howells 
26230ec76a11SDavid Howells /*
26240ec76a11SDavid Howells  * Access another process' address space.
26250ec76a11SDavid Howells  * Source/target buffer must be kernel space,
26260ec76a11SDavid Howells  * Do not walk the page table directly, use get_user_pages
26270ec76a11SDavid Howells  */
26280ec76a11SDavid Howells int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
26290ec76a11SDavid Howells {
26300ec76a11SDavid Howells 	struct mm_struct *mm;
26310ec76a11SDavid Howells 	struct vm_area_struct *vma;
26320ec76a11SDavid Howells 	struct page *page;
26330ec76a11SDavid Howells 	void *old_buf = buf;
26340ec76a11SDavid Howells 
26350ec76a11SDavid Howells 	mm = get_task_mm(tsk);
26360ec76a11SDavid Howells 	if (!mm)
26370ec76a11SDavid Howells 		return 0;
26380ec76a11SDavid Howells 
26390ec76a11SDavid Howells 	down_read(&mm->mmap_sem);
26400ec76a11SDavid Howells 	/* ignore errors, just check how much was sucessfully transfered */
26410ec76a11SDavid Howells 	while (len) {
26420ec76a11SDavid Howells 		int bytes, ret, offset;
26430ec76a11SDavid Howells 		void *maddr;
26440ec76a11SDavid Howells 
26450ec76a11SDavid Howells 		ret = get_user_pages(tsk, mm, addr, 1,
26460ec76a11SDavid Howells 				write, 1, &page, &vma);
26470ec76a11SDavid Howells 		if (ret <= 0)
26480ec76a11SDavid Howells 			break;
26490ec76a11SDavid Howells 
26500ec76a11SDavid Howells 		bytes = len;
26510ec76a11SDavid Howells 		offset = addr & (PAGE_SIZE-1);
26520ec76a11SDavid Howells 		if (bytes > PAGE_SIZE-offset)
26530ec76a11SDavid Howells 			bytes = PAGE_SIZE-offset;
26540ec76a11SDavid Howells 
26550ec76a11SDavid Howells 		maddr = kmap(page);
26560ec76a11SDavid Howells 		if (write) {
26570ec76a11SDavid Howells 			copy_to_user_page(vma, page, addr,
26580ec76a11SDavid Howells 					  maddr + offset, buf, bytes);
26590ec76a11SDavid Howells 			set_page_dirty_lock(page);
26600ec76a11SDavid Howells 		} else {
26610ec76a11SDavid Howells 			copy_from_user_page(vma, page, addr,
26620ec76a11SDavid Howells 					    buf, maddr + offset, bytes);
26630ec76a11SDavid Howells 		}
26640ec76a11SDavid Howells 		kunmap(page);
26650ec76a11SDavid Howells 		page_cache_release(page);
26660ec76a11SDavid Howells 		len -= bytes;
26670ec76a11SDavid Howells 		buf += bytes;
26680ec76a11SDavid Howells 		addr += bytes;
26690ec76a11SDavid Howells 	}
26700ec76a11SDavid Howells 	up_read(&mm->mmap_sem);
26710ec76a11SDavid Howells 	mmput(mm);
26720ec76a11SDavid Howells 
26730ec76a11SDavid Howells 	return buf - old_buf;
26740ec76a11SDavid Howells }
2675