xref: /linux/mm/memory.c (revision d00806b183152af6d24f46f0c33f14162ca1262a)
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 
821da177e4SLinus Torvalds EXPORT_SYMBOL(num_physpages);
831da177e4SLinus Torvalds EXPORT_SYMBOL(high_memory);
841da177e4SLinus Torvalds 
85a62eaf15SAndi Kleen int randomize_va_space __read_mostly = 1;
86a62eaf15SAndi Kleen 
87a62eaf15SAndi Kleen static int __init disable_randmaps(char *s)
88a62eaf15SAndi Kleen {
89a62eaf15SAndi Kleen 	randomize_va_space = 0;
909b41046cSOGAWA Hirofumi 	return 1;
91a62eaf15SAndi Kleen }
92a62eaf15SAndi Kleen __setup("norandmaps", disable_randmaps);
93a62eaf15SAndi Kleen 
94a62eaf15SAndi Kleen 
951da177e4SLinus Torvalds /*
961da177e4SLinus Torvalds  * If a p?d_bad entry is found while walking page tables, report
971da177e4SLinus Torvalds  * the error, before resetting entry to p?d_none.  Usually (but
981da177e4SLinus Torvalds  * very seldom) called out from the p?d_none_or_clear_bad macros.
991da177e4SLinus Torvalds  */
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds void pgd_clear_bad(pgd_t *pgd)
1021da177e4SLinus Torvalds {
1031da177e4SLinus Torvalds 	pgd_ERROR(*pgd);
1041da177e4SLinus Torvalds 	pgd_clear(pgd);
1051da177e4SLinus Torvalds }
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds void pud_clear_bad(pud_t *pud)
1081da177e4SLinus Torvalds {
1091da177e4SLinus Torvalds 	pud_ERROR(*pud);
1101da177e4SLinus Torvalds 	pud_clear(pud);
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds void pmd_clear_bad(pmd_t *pmd)
1141da177e4SLinus Torvalds {
1151da177e4SLinus Torvalds 	pmd_ERROR(*pmd);
1161da177e4SLinus Torvalds 	pmd_clear(pmd);
1171da177e4SLinus Torvalds }
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds /*
1201da177e4SLinus Torvalds  * Note: this doesn't free the actual pages themselves. That
1211da177e4SLinus Torvalds  * has been handled earlier when unmapping all the memory regions.
1221da177e4SLinus Torvalds  */
123e0da382cSHugh Dickins static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd)
1241da177e4SLinus Torvalds {
1251da177e4SLinus Torvalds 	struct page *page = pmd_page(*pmd);
1261da177e4SLinus Torvalds 	pmd_clear(pmd);
1274c21e2f2SHugh Dickins 	pte_lock_deinit(page);
128e0da382cSHugh Dickins 	pte_free_tlb(tlb, page);
129df849a15SChristoph Lameter 	dec_zone_page_state(page, NR_PAGETABLE);
1301da177e4SLinus Torvalds 	tlb->mm->nr_ptes--;
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
133e0da382cSHugh Dickins static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
134e0da382cSHugh Dickins 				unsigned long addr, unsigned long end,
135e0da382cSHugh Dickins 				unsigned long floor, unsigned long ceiling)
1361da177e4SLinus Torvalds {
1371da177e4SLinus Torvalds 	pmd_t *pmd;
1381da177e4SLinus Torvalds 	unsigned long next;
139e0da382cSHugh Dickins 	unsigned long start;
1401da177e4SLinus Torvalds 
141e0da382cSHugh Dickins 	start = addr;
1421da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
1431da177e4SLinus Torvalds 	do {
1441da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
1451da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(pmd))
1461da177e4SLinus Torvalds 			continue;
147e0da382cSHugh Dickins 		free_pte_range(tlb, pmd);
1481da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
1491da177e4SLinus Torvalds 
150e0da382cSHugh Dickins 	start &= PUD_MASK;
151e0da382cSHugh Dickins 	if (start < floor)
152e0da382cSHugh Dickins 		return;
153e0da382cSHugh Dickins 	if (ceiling) {
154e0da382cSHugh Dickins 		ceiling &= PUD_MASK;
155e0da382cSHugh Dickins 		if (!ceiling)
156e0da382cSHugh Dickins 			return;
1571da177e4SLinus Torvalds 	}
158e0da382cSHugh Dickins 	if (end - 1 > ceiling - 1)
159e0da382cSHugh Dickins 		return;
160e0da382cSHugh Dickins 
161e0da382cSHugh Dickins 	pmd = pmd_offset(pud, start);
162e0da382cSHugh Dickins 	pud_clear(pud);
163e0da382cSHugh Dickins 	pmd_free_tlb(tlb, pmd);
1641da177e4SLinus Torvalds }
1651da177e4SLinus Torvalds 
166e0da382cSHugh Dickins static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
167e0da382cSHugh Dickins 				unsigned long addr, unsigned long end,
168e0da382cSHugh Dickins 				unsigned long floor, unsigned long ceiling)
1691da177e4SLinus Torvalds {
1701da177e4SLinus Torvalds 	pud_t *pud;
1711da177e4SLinus Torvalds 	unsigned long next;
172e0da382cSHugh Dickins 	unsigned long start;
1731da177e4SLinus Torvalds 
174e0da382cSHugh Dickins 	start = addr;
1751da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
1761da177e4SLinus Torvalds 	do {
1771da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
1781da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
1791da177e4SLinus Torvalds 			continue;
180e0da382cSHugh Dickins 		free_pmd_range(tlb, pud, addr, next, floor, ceiling);
1811da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
1821da177e4SLinus Torvalds 
183e0da382cSHugh Dickins 	start &= PGDIR_MASK;
184e0da382cSHugh Dickins 	if (start < floor)
185e0da382cSHugh Dickins 		return;
186e0da382cSHugh Dickins 	if (ceiling) {
187e0da382cSHugh Dickins 		ceiling &= PGDIR_MASK;
188e0da382cSHugh Dickins 		if (!ceiling)
189e0da382cSHugh Dickins 			return;
1901da177e4SLinus Torvalds 	}
191e0da382cSHugh Dickins 	if (end - 1 > ceiling - 1)
192e0da382cSHugh Dickins 		return;
193e0da382cSHugh Dickins 
194e0da382cSHugh Dickins 	pud = pud_offset(pgd, start);
195e0da382cSHugh Dickins 	pgd_clear(pgd);
196e0da382cSHugh Dickins 	pud_free_tlb(tlb, pud);
1971da177e4SLinus Torvalds }
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds /*
200e0da382cSHugh Dickins  * This function frees user-level page tables of a process.
201e0da382cSHugh Dickins  *
2021da177e4SLinus Torvalds  * Must be called with pagetable lock held.
2031da177e4SLinus Torvalds  */
2043bf5ee95SHugh Dickins void free_pgd_range(struct mmu_gather **tlb,
205e0da382cSHugh Dickins 			unsigned long addr, unsigned long end,
206e0da382cSHugh Dickins 			unsigned long floor, unsigned long ceiling)
2071da177e4SLinus Torvalds {
2081da177e4SLinus Torvalds 	pgd_t *pgd;
2091da177e4SLinus Torvalds 	unsigned long next;
210e0da382cSHugh Dickins 	unsigned long start;
2111da177e4SLinus Torvalds 
212e0da382cSHugh Dickins 	/*
213e0da382cSHugh Dickins 	 * The next few lines have given us lots of grief...
214e0da382cSHugh Dickins 	 *
215e0da382cSHugh Dickins 	 * Why are we testing PMD* at this top level?  Because often
216e0da382cSHugh Dickins 	 * there will be no work to do at all, and we'd prefer not to
217e0da382cSHugh Dickins 	 * go all the way down to the bottom just to discover that.
218e0da382cSHugh Dickins 	 *
219e0da382cSHugh Dickins 	 * Why all these "- 1"s?  Because 0 represents both the bottom
220e0da382cSHugh Dickins 	 * of the address space and the top of it (using -1 for the
221e0da382cSHugh Dickins 	 * top wouldn't help much: the masks would do the wrong thing).
222e0da382cSHugh Dickins 	 * The rule is that addr 0 and floor 0 refer to the bottom of
223e0da382cSHugh Dickins 	 * the address space, but end 0 and ceiling 0 refer to the top
224e0da382cSHugh Dickins 	 * Comparisons need to use "end - 1" and "ceiling - 1" (though
225e0da382cSHugh Dickins 	 * that end 0 case should be mythical).
226e0da382cSHugh Dickins 	 *
227e0da382cSHugh Dickins 	 * Wherever addr is brought up or ceiling brought down, we must
228e0da382cSHugh Dickins 	 * be careful to reject "the opposite 0" before it confuses the
229e0da382cSHugh Dickins 	 * subsequent tests.  But what about where end is brought down
230e0da382cSHugh Dickins 	 * by PMD_SIZE below? no, end can't go down to 0 there.
231e0da382cSHugh Dickins 	 *
232e0da382cSHugh Dickins 	 * Whereas we round start (addr) and ceiling down, by different
233e0da382cSHugh Dickins 	 * masks at different levels, in order to test whether a table
234e0da382cSHugh Dickins 	 * now has no other vmas using it, so can be freed, we don't
235e0da382cSHugh Dickins 	 * bother to round floor or end up - the tests don't need that.
236e0da382cSHugh Dickins 	 */
237e0da382cSHugh Dickins 
238e0da382cSHugh Dickins 	addr &= PMD_MASK;
239e0da382cSHugh Dickins 	if (addr < floor) {
240e0da382cSHugh Dickins 		addr += PMD_SIZE;
241e0da382cSHugh Dickins 		if (!addr)
242e0da382cSHugh Dickins 			return;
243e0da382cSHugh Dickins 	}
244e0da382cSHugh Dickins 	if (ceiling) {
245e0da382cSHugh Dickins 		ceiling &= PMD_MASK;
246e0da382cSHugh Dickins 		if (!ceiling)
247e0da382cSHugh Dickins 			return;
248e0da382cSHugh Dickins 	}
249e0da382cSHugh Dickins 	if (end - 1 > ceiling - 1)
250e0da382cSHugh Dickins 		end -= PMD_SIZE;
251e0da382cSHugh Dickins 	if (addr > end - 1)
252e0da382cSHugh Dickins 		return;
253e0da382cSHugh Dickins 
254e0da382cSHugh Dickins 	start = addr;
2553bf5ee95SHugh Dickins 	pgd = pgd_offset((*tlb)->mm, addr);
2561da177e4SLinus Torvalds 	do {
2571da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
2581da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
2591da177e4SLinus Torvalds 			continue;
2603bf5ee95SHugh Dickins 		free_pud_range(*tlb, pgd, addr, next, floor, ceiling);
2611da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
262e0da382cSHugh Dickins 
2634d6ddfa9SHugh Dickins 	if (!(*tlb)->fullmm)
2643bf5ee95SHugh Dickins 		flush_tlb_pgtables((*tlb)->mm, start, end);
265e0da382cSHugh Dickins }
266e0da382cSHugh Dickins 
267e0da382cSHugh Dickins void free_pgtables(struct mmu_gather **tlb, struct vm_area_struct *vma,
268e0da382cSHugh Dickins 		unsigned long floor, unsigned long ceiling)
269e0da382cSHugh Dickins {
270e0da382cSHugh Dickins 	while (vma) {
271e0da382cSHugh Dickins 		struct vm_area_struct *next = vma->vm_next;
272e0da382cSHugh Dickins 		unsigned long addr = vma->vm_start;
273e0da382cSHugh Dickins 
2748f4f8c16SHugh Dickins 		/*
2758f4f8c16SHugh Dickins 		 * Hide vma from rmap and vmtruncate before freeing pgtables
2768f4f8c16SHugh Dickins 		 */
2778f4f8c16SHugh Dickins 		anon_vma_unlink(vma);
2788f4f8c16SHugh Dickins 		unlink_file_vma(vma);
2798f4f8c16SHugh Dickins 
2809da61aefSDavid Gibson 		if (is_vm_hugetlb_page(vma)) {
2813bf5ee95SHugh Dickins 			hugetlb_free_pgd_range(tlb, addr, vma->vm_end,
2823bf5ee95SHugh Dickins 				floor, next? next->vm_start: ceiling);
2833bf5ee95SHugh Dickins 		} else {
2843bf5ee95SHugh Dickins 			/*
2853bf5ee95SHugh Dickins 			 * Optimization: gather nearby vmas into one call down
2863bf5ee95SHugh Dickins 			 */
2873bf5ee95SHugh Dickins 			while (next && next->vm_start <= vma->vm_end + PMD_SIZE
2884866920bSDavid Gibson 			       && !is_vm_hugetlb_page(next)) {
289e0da382cSHugh Dickins 				vma = next;
290e0da382cSHugh Dickins 				next = vma->vm_next;
2918f4f8c16SHugh Dickins 				anon_vma_unlink(vma);
2928f4f8c16SHugh Dickins 				unlink_file_vma(vma);
293e0da382cSHugh Dickins 			}
2943bf5ee95SHugh Dickins 			free_pgd_range(tlb, addr, vma->vm_end,
295e0da382cSHugh Dickins 				floor, next? next->vm_start: ceiling);
2963bf5ee95SHugh Dickins 		}
297e0da382cSHugh Dickins 		vma = next;
298e0da382cSHugh Dickins 	}
2991da177e4SLinus Torvalds }
3001da177e4SLinus Torvalds 
3011bb3630eSHugh Dickins int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
3021da177e4SLinus Torvalds {
303c74df32cSHugh Dickins 	struct page *new = pte_alloc_one(mm, address);
3041da177e4SLinus Torvalds 	if (!new)
3051bb3630eSHugh Dickins 		return -ENOMEM;
3061bb3630eSHugh Dickins 
3074c21e2f2SHugh Dickins 	pte_lock_init(new);
308c74df32cSHugh Dickins 	spin_lock(&mm->page_table_lock);
3094c21e2f2SHugh Dickins 	if (pmd_present(*pmd)) {	/* Another has populated it */
3104c21e2f2SHugh Dickins 		pte_lock_deinit(new);
3111da177e4SLinus Torvalds 		pte_free(new);
3124c21e2f2SHugh Dickins 	} else {
3131da177e4SLinus Torvalds 		mm->nr_ptes++;
314df849a15SChristoph Lameter 		inc_zone_page_state(new, NR_PAGETABLE);
3151da177e4SLinus Torvalds 		pmd_populate(mm, pmd, new);
3161da177e4SLinus Torvalds 	}
317c74df32cSHugh Dickins 	spin_unlock(&mm->page_table_lock);
3181bb3630eSHugh Dickins 	return 0;
3191da177e4SLinus Torvalds }
3201da177e4SLinus Torvalds 
3211bb3630eSHugh Dickins int __pte_alloc_kernel(pmd_t *pmd, unsigned long address)
3221da177e4SLinus Torvalds {
3231bb3630eSHugh Dickins 	pte_t *new = pte_alloc_one_kernel(&init_mm, address);
3241da177e4SLinus Torvalds 	if (!new)
3251bb3630eSHugh Dickins 		return -ENOMEM;
3261da177e4SLinus Torvalds 
327872fec16SHugh Dickins 	spin_lock(&init_mm.page_table_lock);
3281bb3630eSHugh Dickins 	if (pmd_present(*pmd))		/* Another has populated it */
3291da177e4SLinus Torvalds 		pte_free_kernel(new);
330872fec16SHugh Dickins 	else
331872fec16SHugh Dickins 		pmd_populate_kernel(&init_mm, pmd, new);
332872fec16SHugh Dickins 	spin_unlock(&init_mm.page_table_lock);
3331bb3630eSHugh Dickins 	return 0;
3341da177e4SLinus Torvalds }
3351da177e4SLinus Torvalds 
336ae859762SHugh Dickins static inline void add_mm_rss(struct mm_struct *mm, int file_rss, int anon_rss)
337ae859762SHugh Dickins {
338ae859762SHugh Dickins 	if (file_rss)
339ae859762SHugh Dickins 		add_mm_counter(mm, file_rss, file_rss);
340ae859762SHugh Dickins 	if (anon_rss)
341ae859762SHugh Dickins 		add_mm_counter(mm, anon_rss, anon_rss);
342ae859762SHugh Dickins }
343ae859762SHugh Dickins 
3441da177e4SLinus Torvalds /*
3456aab341eSLinus Torvalds  * This function is called to print an error when a bad pte
3466aab341eSLinus Torvalds  * is found. For example, we might have a PFN-mapped pte in
3476aab341eSLinus Torvalds  * a region that doesn't allow it.
348b5810039SNick Piggin  *
349b5810039SNick Piggin  * The calling function must still handle the error.
350b5810039SNick Piggin  */
351b5810039SNick Piggin void print_bad_pte(struct vm_area_struct *vma, pte_t pte, unsigned long vaddr)
352b5810039SNick Piggin {
353b5810039SNick Piggin 	printk(KERN_ERR "Bad pte = %08llx, process = %s, "
354b5810039SNick Piggin 			"vm_flags = %lx, vaddr = %lx\n",
355b5810039SNick Piggin 		(long long)pte_val(pte),
356b5810039SNick Piggin 		(vma->vm_mm == current->mm ? current->comm : "???"),
357b5810039SNick Piggin 		vma->vm_flags, vaddr);
358b5810039SNick Piggin 	dump_stack();
359b5810039SNick Piggin }
360b5810039SNick Piggin 
36167121172SLinus Torvalds static inline int is_cow_mapping(unsigned int flags)
36267121172SLinus Torvalds {
36367121172SLinus Torvalds 	return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
36467121172SLinus Torvalds }
36567121172SLinus Torvalds 
366b5810039SNick Piggin /*
3676aab341eSLinus Torvalds  * This function gets the "struct page" associated with a pte.
3686aab341eSLinus Torvalds  *
3696aab341eSLinus Torvalds  * NOTE! Some mappings do not have "struct pages". A raw PFN mapping
3706aab341eSLinus Torvalds  * will have each page table entry just pointing to a raw page frame
3716aab341eSLinus Torvalds  * number, and as far as the VM layer is concerned, those do not have
3726aab341eSLinus Torvalds  * pages associated with them - even if the PFN might point to memory
3736aab341eSLinus Torvalds  * that otherwise is perfectly fine and has a "struct page".
3746aab341eSLinus Torvalds  *
3756aab341eSLinus Torvalds  * The way we recognize those mappings is through the rules set up
3766aab341eSLinus Torvalds  * by "remap_pfn_range()": the vma will have the VM_PFNMAP bit set,
3776aab341eSLinus Torvalds  * and the vm_pgoff will point to the first PFN mapped: thus every
3786aab341eSLinus Torvalds  * page that is a raw mapping will always honor the rule
3796aab341eSLinus Torvalds  *
3806aab341eSLinus Torvalds  *	pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
3816aab341eSLinus Torvalds  *
3826aab341eSLinus Torvalds  * and if that isn't true, the page has been COW'ed (in which case it
3836aab341eSLinus Torvalds  * _does_ have a "struct page" associated with it even if it is in a
3846aab341eSLinus Torvalds  * VM_PFNMAP range).
385ee498ed7SHugh Dickins  */
3866aab341eSLinus Torvalds struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, pte_t pte)
387ee498ed7SHugh Dickins {
3886aab341eSLinus Torvalds 	unsigned long pfn = pte_pfn(pte);
3896aab341eSLinus Torvalds 
390b7ab795bSNick Piggin 	if (unlikely(vma->vm_flags & VM_PFNMAP)) {
3916aab341eSLinus Torvalds 		unsigned long off = (addr - vma->vm_start) >> PAGE_SHIFT;
3926aab341eSLinus Torvalds 		if (pfn == vma->vm_pgoff + off)
3936aab341eSLinus Torvalds 			return NULL;
39467121172SLinus Torvalds 		if (!is_cow_mapping(vma->vm_flags))
395fb155c16SLinus Torvalds 			return NULL;
3966aab341eSLinus Torvalds 	}
3976aab341eSLinus Torvalds 
398315ab19aSNick Piggin 	/*
399315ab19aSNick Piggin 	 * Add some anal sanity checks for now. Eventually,
400315ab19aSNick Piggin 	 * we should just do "return pfn_to_page(pfn)", but
401315ab19aSNick Piggin 	 * in the meantime we check that we get a valid pfn,
402315ab19aSNick Piggin 	 * and that the resulting page looks ok.
403315ab19aSNick Piggin 	 */
4046aab341eSLinus Torvalds 	if (unlikely(!pfn_valid(pfn))) {
4056aab341eSLinus Torvalds 		print_bad_pte(vma, pte, addr);
4066aab341eSLinus Torvalds 		return NULL;
4076aab341eSLinus Torvalds 	}
4086aab341eSLinus Torvalds 
4096aab341eSLinus Torvalds 	/*
4106aab341eSLinus Torvalds 	 * NOTE! We still have PageReserved() pages in the page
4116aab341eSLinus Torvalds 	 * tables.
4126aab341eSLinus Torvalds 	 *
4136aab341eSLinus Torvalds 	 * The PAGE_ZERO() pages and various VDSO mappings can
4146aab341eSLinus Torvalds 	 * cause them to exist.
4156aab341eSLinus Torvalds 	 */
4166aab341eSLinus Torvalds 	return pfn_to_page(pfn);
417ee498ed7SHugh Dickins }
418ee498ed7SHugh Dickins 
419ee498ed7SHugh Dickins /*
4201da177e4SLinus Torvalds  * copy one vm_area from one task to the other. Assumes the page tables
4211da177e4SLinus Torvalds  * already present in the new task to be cleared in the whole range
4221da177e4SLinus Torvalds  * covered by this vma.
4231da177e4SLinus Torvalds  */
4241da177e4SLinus Torvalds 
4258c103762SHugh Dickins static inline void
4261da177e4SLinus Torvalds copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
427b5810039SNick Piggin 		pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma,
4288c103762SHugh Dickins 		unsigned long addr, int *rss)
4291da177e4SLinus Torvalds {
430b5810039SNick Piggin 	unsigned long vm_flags = vma->vm_flags;
4311da177e4SLinus Torvalds 	pte_t pte = *src_pte;
4321da177e4SLinus Torvalds 	struct page *page;
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds 	/* pte contains position in swap or file, so copy. */
4351da177e4SLinus Torvalds 	if (unlikely(!pte_present(pte))) {
4361da177e4SLinus Torvalds 		if (!pte_file(pte)) {
4370697212aSChristoph Lameter 			swp_entry_t entry = pte_to_swp_entry(pte);
4380697212aSChristoph Lameter 
4390697212aSChristoph Lameter 			swap_duplicate(entry);
4401da177e4SLinus Torvalds 			/* make sure dst_mm is on swapoff's mmlist. */
4411da177e4SLinus Torvalds 			if (unlikely(list_empty(&dst_mm->mmlist))) {
4421da177e4SLinus Torvalds 				spin_lock(&mmlist_lock);
443f412ac08SHugh Dickins 				if (list_empty(&dst_mm->mmlist))
444f412ac08SHugh Dickins 					list_add(&dst_mm->mmlist,
445f412ac08SHugh Dickins 						 &src_mm->mmlist);
4461da177e4SLinus Torvalds 				spin_unlock(&mmlist_lock);
4471da177e4SLinus Torvalds 			}
4480697212aSChristoph Lameter 			if (is_write_migration_entry(entry) &&
4490697212aSChristoph Lameter 					is_cow_mapping(vm_flags)) {
4500697212aSChristoph Lameter 				/*
4510697212aSChristoph Lameter 				 * COW mappings require pages in both parent
4520697212aSChristoph Lameter 				 * and child to be set to read.
4530697212aSChristoph Lameter 				 */
4540697212aSChristoph Lameter 				make_migration_entry_read(&entry);
4550697212aSChristoph Lameter 				pte = swp_entry_to_pte(entry);
4560697212aSChristoph Lameter 				set_pte_at(src_mm, addr, src_pte, pte);
4570697212aSChristoph Lameter 			}
4581da177e4SLinus Torvalds 		}
459ae859762SHugh Dickins 		goto out_set_pte;
4601da177e4SLinus Torvalds 	}
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 	/*
4631da177e4SLinus Torvalds 	 * If it's a COW mapping, write protect it both
4641da177e4SLinus Torvalds 	 * in the parent and the child
4651da177e4SLinus Torvalds 	 */
46667121172SLinus Torvalds 	if (is_cow_mapping(vm_flags)) {
4671da177e4SLinus Torvalds 		ptep_set_wrprotect(src_mm, addr, src_pte);
4683dc90795SZachary Amsden 		pte = pte_wrprotect(pte);
4691da177e4SLinus Torvalds 	}
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 	/*
4721da177e4SLinus Torvalds 	 * If it's a shared mapping, mark it clean in
4731da177e4SLinus Torvalds 	 * the child
4741da177e4SLinus Torvalds 	 */
4751da177e4SLinus Torvalds 	if (vm_flags & VM_SHARED)
4761da177e4SLinus Torvalds 		pte = pte_mkclean(pte);
4771da177e4SLinus Torvalds 	pte = pte_mkold(pte);
4786aab341eSLinus Torvalds 
4796aab341eSLinus Torvalds 	page = vm_normal_page(vma, addr, pte);
4806aab341eSLinus Torvalds 	if (page) {
4811da177e4SLinus Torvalds 		get_page(page);
482c97a9e10SNick Piggin 		page_dup_rmap(page, vma, addr);
4838c103762SHugh Dickins 		rss[!!PageAnon(page)]++;
4846aab341eSLinus Torvalds 	}
485ae859762SHugh Dickins 
486ae859762SHugh Dickins out_set_pte:
487ae859762SHugh Dickins 	set_pte_at(dst_mm, addr, dst_pte, pte);
4881da177e4SLinus Torvalds }
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
4911da177e4SLinus Torvalds 		pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
4921da177e4SLinus Torvalds 		unsigned long addr, unsigned long end)
4931da177e4SLinus Torvalds {
4941da177e4SLinus Torvalds 	pte_t *src_pte, *dst_pte;
495c74df32cSHugh Dickins 	spinlock_t *src_ptl, *dst_ptl;
496e040f218SHugh Dickins 	int progress = 0;
4978c103762SHugh Dickins 	int rss[2];
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds again:
500ae859762SHugh Dickins 	rss[1] = rss[0] = 0;
501c74df32cSHugh Dickins 	dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
5021da177e4SLinus Torvalds 	if (!dst_pte)
5031da177e4SLinus Torvalds 		return -ENOMEM;
5041da177e4SLinus Torvalds 	src_pte = pte_offset_map_nested(src_pmd, addr);
5054c21e2f2SHugh Dickins 	src_ptl = pte_lockptr(src_mm, src_pmd);
506f20dc5f7SIngo Molnar 	spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
5076606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	do {
5101da177e4SLinus Torvalds 		/*
5111da177e4SLinus Torvalds 		 * We are holding two locks at this point - either of them
5121da177e4SLinus Torvalds 		 * could generate latencies in another task on another CPU.
5131da177e4SLinus Torvalds 		 */
514e040f218SHugh Dickins 		if (progress >= 32) {
515e040f218SHugh Dickins 			progress = 0;
516e040f218SHugh Dickins 			if (need_resched() ||
517c74df32cSHugh Dickins 			    need_lockbreak(src_ptl) ||
518c74df32cSHugh Dickins 			    need_lockbreak(dst_ptl))
5191da177e4SLinus Torvalds 				break;
520e040f218SHugh Dickins 		}
5211da177e4SLinus Torvalds 		if (pte_none(*src_pte)) {
5221da177e4SLinus Torvalds 			progress++;
5231da177e4SLinus Torvalds 			continue;
5241da177e4SLinus Torvalds 		}
5258c103762SHugh Dickins 		copy_one_pte(dst_mm, src_mm, dst_pte, src_pte, vma, addr, rss);
5261da177e4SLinus Torvalds 		progress += 8;
5271da177e4SLinus Torvalds 	} while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
5281da177e4SLinus Torvalds 
5296606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
530c74df32cSHugh Dickins 	spin_unlock(src_ptl);
5311da177e4SLinus Torvalds 	pte_unmap_nested(src_pte - 1);
532ae859762SHugh Dickins 	add_mm_rss(dst_mm, rss[0], rss[1]);
533c74df32cSHugh Dickins 	pte_unmap_unlock(dst_pte - 1, dst_ptl);
534c74df32cSHugh Dickins 	cond_resched();
5351da177e4SLinus Torvalds 	if (addr != end)
5361da177e4SLinus Torvalds 		goto again;
5371da177e4SLinus Torvalds 	return 0;
5381da177e4SLinus Torvalds }
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
5411da177e4SLinus Torvalds 		pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma,
5421da177e4SLinus Torvalds 		unsigned long addr, unsigned long end)
5431da177e4SLinus Torvalds {
5441da177e4SLinus Torvalds 	pmd_t *src_pmd, *dst_pmd;
5451da177e4SLinus Torvalds 	unsigned long next;
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 	dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
5481da177e4SLinus Torvalds 	if (!dst_pmd)
5491da177e4SLinus Torvalds 		return -ENOMEM;
5501da177e4SLinus Torvalds 	src_pmd = pmd_offset(src_pud, addr);
5511da177e4SLinus Torvalds 	do {
5521da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
5531da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(src_pmd))
5541da177e4SLinus Torvalds 			continue;
5551da177e4SLinus Torvalds 		if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd,
5561da177e4SLinus Torvalds 						vma, addr, next))
5571da177e4SLinus Torvalds 			return -ENOMEM;
5581da177e4SLinus Torvalds 	} while (dst_pmd++, src_pmd++, addr = next, addr != end);
5591da177e4SLinus Torvalds 	return 0;
5601da177e4SLinus Torvalds }
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
5631da177e4SLinus Torvalds 		pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma,
5641da177e4SLinus Torvalds 		unsigned long addr, unsigned long end)
5651da177e4SLinus Torvalds {
5661da177e4SLinus Torvalds 	pud_t *src_pud, *dst_pud;
5671da177e4SLinus Torvalds 	unsigned long next;
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds 	dst_pud = pud_alloc(dst_mm, dst_pgd, addr);
5701da177e4SLinus Torvalds 	if (!dst_pud)
5711da177e4SLinus Torvalds 		return -ENOMEM;
5721da177e4SLinus Torvalds 	src_pud = pud_offset(src_pgd, addr);
5731da177e4SLinus Torvalds 	do {
5741da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
5751da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(src_pud))
5761da177e4SLinus Torvalds 			continue;
5771da177e4SLinus Torvalds 		if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud,
5781da177e4SLinus Torvalds 						vma, addr, next))
5791da177e4SLinus Torvalds 			return -ENOMEM;
5801da177e4SLinus Torvalds 	} while (dst_pud++, src_pud++, addr = next, addr != end);
5811da177e4SLinus Torvalds 	return 0;
5821da177e4SLinus Torvalds }
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
5851da177e4SLinus Torvalds 		struct vm_area_struct *vma)
5861da177e4SLinus Torvalds {
5871da177e4SLinus Torvalds 	pgd_t *src_pgd, *dst_pgd;
5881da177e4SLinus Torvalds 	unsigned long next;
5891da177e4SLinus Torvalds 	unsigned long addr = vma->vm_start;
5901da177e4SLinus Torvalds 	unsigned long end = vma->vm_end;
5911da177e4SLinus Torvalds 
592d992895bSNick Piggin 	/*
593d992895bSNick Piggin 	 * Don't copy ptes where a page fault will fill them correctly.
594d992895bSNick Piggin 	 * Fork becomes much lighter when there are big shared or private
595d992895bSNick Piggin 	 * readonly mappings. The tradeoff is that copy_page_range is more
596d992895bSNick Piggin 	 * efficient than faulting.
597d992895bSNick Piggin 	 */
5984d7672b4SLinus Torvalds 	if (!(vma->vm_flags & (VM_HUGETLB|VM_NONLINEAR|VM_PFNMAP|VM_INSERTPAGE))) {
599d992895bSNick Piggin 		if (!vma->anon_vma)
600d992895bSNick Piggin 			return 0;
601d992895bSNick Piggin 	}
602d992895bSNick Piggin 
6031da177e4SLinus Torvalds 	if (is_vm_hugetlb_page(vma))
6041da177e4SLinus Torvalds 		return copy_hugetlb_page_range(dst_mm, src_mm, vma);
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds 	dst_pgd = pgd_offset(dst_mm, addr);
6071da177e4SLinus Torvalds 	src_pgd = pgd_offset(src_mm, addr);
6081da177e4SLinus Torvalds 	do {
6091da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
6101da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(src_pgd))
6111da177e4SLinus Torvalds 			continue;
6121da177e4SLinus Torvalds 		if (copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd,
6131da177e4SLinus Torvalds 						vma, addr, next))
6141da177e4SLinus Torvalds 			return -ENOMEM;
6151da177e4SLinus Torvalds 	} while (dst_pgd++, src_pgd++, addr = next, addr != end);
6161da177e4SLinus Torvalds 	return 0;
6171da177e4SLinus Torvalds }
6181da177e4SLinus Torvalds 
61951c6f666SRobin Holt static unsigned long zap_pte_range(struct mmu_gather *tlb,
620b5810039SNick Piggin 				struct vm_area_struct *vma, pmd_t *pmd,
6211da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
62251c6f666SRobin Holt 				long *zap_work, struct zap_details *details)
6231da177e4SLinus Torvalds {
624b5810039SNick Piggin 	struct mm_struct *mm = tlb->mm;
6251da177e4SLinus Torvalds 	pte_t *pte;
626508034a3SHugh Dickins 	spinlock_t *ptl;
627ae859762SHugh Dickins 	int file_rss = 0;
628ae859762SHugh Dickins 	int anon_rss = 0;
6291da177e4SLinus Torvalds 
630508034a3SHugh Dickins 	pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
6316606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
6321da177e4SLinus Torvalds 	do {
6331da177e4SLinus Torvalds 		pte_t ptent = *pte;
63451c6f666SRobin Holt 		if (pte_none(ptent)) {
63551c6f666SRobin Holt 			(*zap_work)--;
6361da177e4SLinus Torvalds 			continue;
63751c6f666SRobin Holt 		}
63851c6f666SRobin Holt 
63951c6f666SRobin Holt 		(*zap_work) -= PAGE_SIZE;
64051c6f666SRobin Holt 
6416f5e6b9eSHugh Dickins 		if (pte_present(ptent)) {
6426f5e6b9eSHugh Dickins 			struct page *page;
6436f5e6b9eSHugh Dickins 
6446aab341eSLinus Torvalds 			page = vm_normal_page(vma, addr, ptent);
6451da177e4SLinus Torvalds 			if (unlikely(details) && page) {
6461da177e4SLinus Torvalds 				/*
6471da177e4SLinus Torvalds 				 * unmap_shared_mapping_pages() wants to
6481da177e4SLinus Torvalds 				 * invalidate cache without truncating:
6491da177e4SLinus Torvalds 				 * unmap shared but keep private pages.
6501da177e4SLinus Torvalds 				 */
6511da177e4SLinus Torvalds 				if (details->check_mapping &&
6521da177e4SLinus Torvalds 				    details->check_mapping != page->mapping)
6531da177e4SLinus Torvalds 					continue;
6541da177e4SLinus Torvalds 				/*
6551da177e4SLinus Torvalds 				 * Each page->index must be checked when
6561da177e4SLinus Torvalds 				 * invalidating or truncating nonlinear.
6571da177e4SLinus Torvalds 				 */
6581da177e4SLinus Torvalds 				if (details->nonlinear_vma &&
6591da177e4SLinus Torvalds 				    (page->index < details->first_index ||
6601da177e4SLinus Torvalds 				     page->index > details->last_index))
6611da177e4SLinus Torvalds 					continue;
6621da177e4SLinus Torvalds 			}
663b5810039SNick Piggin 			ptent = ptep_get_and_clear_full(mm, addr, pte,
664a600388dSZachary Amsden 							tlb->fullmm);
6651da177e4SLinus Torvalds 			tlb_remove_tlb_entry(tlb, pte, addr);
6661da177e4SLinus Torvalds 			if (unlikely(!page))
6671da177e4SLinus Torvalds 				continue;
6681da177e4SLinus Torvalds 			if (unlikely(details) && details->nonlinear_vma
6691da177e4SLinus Torvalds 			    && linear_page_index(details->nonlinear_vma,
6701da177e4SLinus Torvalds 						addr) != page->index)
671b5810039SNick Piggin 				set_pte_at(mm, addr, pte,
6721da177e4SLinus Torvalds 					   pgoff_to_pte(page->index));
6731da177e4SLinus Torvalds 			if (PageAnon(page))
67486d912f4SHugh Dickins 				anon_rss--;
6756237bcd9SHugh Dickins 			else {
6766237bcd9SHugh Dickins 				if (pte_dirty(ptent))
6776237bcd9SHugh Dickins 					set_page_dirty(page);
6786237bcd9SHugh Dickins 				if (pte_young(ptent))
679daa88c8dSKen Chen 					SetPageReferenced(page);
68086d912f4SHugh Dickins 				file_rss--;
6816237bcd9SHugh Dickins 			}
6827de6b805SNick Piggin 			page_remove_rmap(page, vma);
6831da177e4SLinus Torvalds 			tlb_remove_page(tlb, page);
6841da177e4SLinus Torvalds 			continue;
6851da177e4SLinus Torvalds 		}
6861da177e4SLinus Torvalds 		/*
6871da177e4SLinus Torvalds 		 * If details->check_mapping, we leave swap entries;
6881da177e4SLinus Torvalds 		 * if details->nonlinear_vma, we leave file entries.
6891da177e4SLinus Torvalds 		 */
6901da177e4SLinus Torvalds 		if (unlikely(details))
6911da177e4SLinus Torvalds 			continue;
6921da177e4SLinus Torvalds 		if (!pte_file(ptent))
6931da177e4SLinus Torvalds 			free_swap_and_cache(pte_to_swp_entry(ptent));
6949888a1caSZachary Amsden 		pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
69551c6f666SRobin Holt 	} while (pte++, addr += PAGE_SIZE, (addr != end && *zap_work > 0));
696ae859762SHugh Dickins 
69786d912f4SHugh Dickins 	add_mm_rss(mm, file_rss, anon_rss);
6986606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
699508034a3SHugh Dickins 	pte_unmap_unlock(pte - 1, ptl);
70051c6f666SRobin Holt 
70151c6f666SRobin Holt 	return addr;
7021da177e4SLinus Torvalds }
7031da177e4SLinus Torvalds 
70451c6f666SRobin Holt static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
705b5810039SNick Piggin 				struct vm_area_struct *vma, pud_t *pud,
7061da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
70751c6f666SRobin Holt 				long *zap_work, struct zap_details *details)
7081da177e4SLinus Torvalds {
7091da177e4SLinus Torvalds 	pmd_t *pmd;
7101da177e4SLinus Torvalds 	unsigned long next;
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
7131da177e4SLinus Torvalds 	do {
7141da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
71551c6f666SRobin Holt 		if (pmd_none_or_clear_bad(pmd)) {
71651c6f666SRobin Holt 			(*zap_work)--;
7171da177e4SLinus Torvalds 			continue;
71851c6f666SRobin Holt 		}
71951c6f666SRobin Holt 		next = zap_pte_range(tlb, vma, pmd, addr, next,
72051c6f666SRobin Holt 						zap_work, details);
72151c6f666SRobin Holt 	} while (pmd++, addr = next, (addr != end && *zap_work > 0));
72251c6f666SRobin Holt 
72351c6f666SRobin Holt 	return addr;
7241da177e4SLinus Torvalds }
7251da177e4SLinus Torvalds 
72651c6f666SRobin Holt static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
727b5810039SNick Piggin 				struct vm_area_struct *vma, pgd_t *pgd,
7281da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
72951c6f666SRobin Holt 				long *zap_work, struct zap_details *details)
7301da177e4SLinus Torvalds {
7311da177e4SLinus Torvalds 	pud_t *pud;
7321da177e4SLinus Torvalds 	unsigned long next;
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
7351da177e4SLinus Torvalds 	do {
7361da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
73751c6f666SRobin Holt 		if (pud_none_or_clear_bad(pud)) {
73851c6f666SRobin Holt 			(*zap_work)--;
7391da177e4SLinus Torvalds 			continue;
74051c6f666SRobin Holt 		}
74151c6f666SRobin Holt 		next = zap_pmd_range(tlb, vma, pud, addr, next,
74251c6f666SRobin Holt 						zap_work, details);
74351c6f666SRobin Holt 	} while (pud++, addr = next, (addr != end && *zap_work > 0));
74451c6f666SRobin Holt 
74551c6f666SRobin Holt 	return addr;
7461da177e4SLinus Torvalds }
7471da177e4SLinus Torvalds 
74851c6f666SRobin Holt static unsigned long unmap_page_range(struct mmu_gather *tlb,
74951c6f666SRobin Holt 				struct vm_area_struct *vma,
7501da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
75151c6f666SRobin Holt 				long *zap_work, struct zap_details *details)
7521da177e4SLinus Torvalds {
7531da177e4SLinus Torvalds 	pgd_t *pgd;
7541da177e4SLinus Torvalds 	unsigned long next;
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds 	if (details && !details->check_mapping && !details->nonlinear_vma)
7571da177e4SLinus Torvalds 		details = NULL;
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 	BUG_ON(addr >= end);
7601da177e4SLinus Torvalds 	tlb_start_vma(tlb, vma);
7611da177e4SLinus Torvalds 	pgd = pgd_offset(vma->vm_mm, addr);
7621da177e4SLinus Torvalds 	do {
7631da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
76451c6f666SRobin Holt 		if (pgd_none_or_clear_bad(pgd)) {
76551c6f666SRobin Holt 			(*zap_work)--;
7661da177e4SLinus Torvalds 			continue;
76751c6f666SRobin Holt 		}
76851c6f666SRobin Holt 		next = zap_pud_range(tlb, vma, pgd, addr, next,
76951c6f666SRobin Holt 						zap_work, details);
77051c6f666SRobin Holt 	} while (pgd++, addr = next, (addr != end && *zap_work > 0));
7711da177e4SLinus Torvalds 	tlb_end_vma(tlb, vma);
77251c6f666SRobin Holt 
77351c6f666SRobin Holt 	return addr;
7741da177e4SLinus Torvalds }
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds #ifdef CONFIG_PREEMPT
7771da177e4SLinus Torvalds # define ZAP_BLOCK_SIZE	(8 * PAGE_SIZE)
7781da177e4SLinus Torvalds #else
7791da177e4SLinus Torvalds /* No preempt: go for improved straight-line efficiency */
7801da177e4SLinus Torvalds # define ZAP_BLOCK_SIZE	(1024 * PAGE_SIZE)
7811da177e4SLinus Torvalds #endif
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds /**
7841da177e4SLinus Torvalds  * unmap_vmas - unmap a range of memory covered by a list of vma's
7851da177e4SLinus Torvalds  * @tlbp: address of the caller's struct mmu_gather
7861da177e4SLinus Torvalds  * @vma: the starting vma
7871da177e4SLinus Torvalds  * @start_addr: virtual address at which to start unmapping
7881da177e4SLinus Torvalds  * @end_addr: virtual address at which to end unmapping
7891da177e4SLinus Torvalds  * @nr_accounted: Place number of unmapped pages in vm-accountable vma's here
7901da177e4SLinus Torvalds  * @details: details of nonlinear truncation or shared cache invalidation
7911da177e4SLinus Torvalds  *
792ee39b37bSHugh Dickins  * Returns the end address of the unmapping (restart addr if interrupted).
7931da177e4SLinus Torvalds  *
794508034a3SHugh Dickins  * Unmap all pages in the vma list.
7951da177e4SLinus Torvalds  *
796508034a3SHugh Dickins  * We aim to not hold locks for too long (for scheduling latency reasons).
797508034a3SHugh Dickins  * So zap pages in ZAP_BLOCK_SIZE bytecounts.  This means we need to
7981da177e4SLinus Torvalds  * return the ending mmu_gather to the caller.
7991da177e4SLinus Torvalds  *
8001da177e4SLinus Torvalds  * Only addresses between `start' and `end' will be unmapped.
8011da177e4SLinus Torvalds  *
8021da177e4SLinus Torvalds  * The VMA list must be sorted in ascending virtual address order.
8031da177e4SLinus Torvalds  *
8041da177e4SLinus Torvalds  * unmap_vmas() assumes that the caller will flush the whole unmapped address
8051da177e4SLinus Torvalds  * range after unmap_vmas() returns.  So the only responsibility here is to
8061da177e4SLinus Torvalds  * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
8071da177e4SLinus Torvalds  * drops the lock and schedules.
8081da177e4SLinus Torvalds  */
809508034a3SHugh Dickins unsigned long unmap_vmas(struct mmu_gather **tlbp,
8101da177e4SLinus Torvalds 		struct vm_area_struct *vma, unsigned long start_addr,
8111da177e4SLinus Torvalds 		unsigned long end_addr, unsigned long *nr_accounted,
8121da177e4SLinus Torvalds 		struct zap_details *details)
8131da177e4SLinus Torvalds {
81451c6f666SRobin Holt 	long zap_work = ZAP_BLOCK_SIZE;
8151da177e4SLinus Torvalds 	unsigned long tlb_start = 0;	/* For tlb_finish_mmu */
8161da177e4SLinus Torvalds 	int tlb_start_valid = 0;
817ee39b37bSHugh Dickins 	unsigned long start = start_addr;
8181da177e4SLinus Torvalds 	spinlock_t *i_mmap_lock = details? details->i_mmap_lock: NULL;
8194d6ddfa9SHugh Dickins 	int fullmm = (*tlbp)->fullmm;
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) {
8221da177e4SLinus Torvalds 		unsigned long end;
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 		start = max(vma->vm_start, start_addr);
8251da177e4SLinus Torvalds 		if (start >= vma->vm_end)
8261da177e4SLinus Torvalds 			continue;
8271da177e4SLinus Torvalds 		end = min(vma->vm_end, end_addr);
8281da177e4SLinus Torvalds 		if (end <= vma->vm_start)
8291da177e4SLinus Torvalds 			continue;
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds 		if (vma->vm_flags & VM_ACCOUNT)
8321da177e4SLinus Torvalds 			*nr_accounted += (end - start) >> PAGE_SHIFT;
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 		while (start != end) {
8351da177e4SLinus Torvalds 			if (!tlb_start_valid) {
8361da177e4SLinus Torvalds 				tlb_start = start;
8371da177e4SLinus Torvalds 				tlb_start_valid = 1;
8381da177e4SLinus Torvalds 			}
8391da177e4SLinus Torvalds 
84051c6f666SRobin Holt 			if (unlikely(is_vm_hugetlb_page(vma))) {
8411da177e4SLinus Torvalds 				unmap_hugepage_range(vma, start, end);
84251c6f666SRobin Holt 				zap_work -= (end - start) /
84351c6f666SRobin Holt 						(HPAGE_SIZE / PAGE_SIZE);
84451c6f666SRobin Holt 				start = end;
84551c6f666SRobin Holt 			} else
84651c6f666SRobin Holt 				start = unmap_page_range(*tlbp, vma,
84751c6f666SRobin Holt 						start, end, &zap_work, details);
8481da177e4SLinus Torvalds 
84951c6f666SRobin Holt 			if (zap_work > 0) {
85051c6f666SRobin Holt 				BUG_ON(start != end);
85151c6f666SRobin Holt 				break;
85251c6f666SRobin Holt 			}
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 			tlb_finish_mmu(*tlbp, tlb_start, start);
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 			if (need_resched() ||
8571da177e4SLinus Torvalds 				(i_mmap_lock && need_lockbreak(i_mmap_lock))) {
8581da177e4SLinus Torvalds 				if (i_mmap_lock) {
859508034a3SHugh Dickins 					*tlbp = NULL;
8601da177e4SLinus Torvalds 					goto out;
8611da177e4SLinus Torvalds 				}
8621da177e4SLinus Torvalds 				cond_resched();
8631da177e4SLinus Torvalds 			}
8641da177e4SLinus Torvalds 
865508034a3SHugh Dickins 			*tlbp = tlb_gather_mmu(vma->vm_mm, fullmm);
8661da177e4SLinus Torvalds 			tlb_start_valid = 0;
86751c6f666SRobin Holt 			zap_work = ZAP_BLOCK_SIZE;
8681da177e4SLinus Torvalds 		}
8691da177e4SLinus Torvalds 	}
8701da177e4SLinus Torvalds out:
871ee39b37bSHugh Dickins 	return start;	/* which is now the end (or restart) address */
8721da177e4SLinus Torvalds }
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds /**
8751da177e4SLinus Torvalds  * zap_page_range - remove user pages in a given range
8761da177e4SLinus Torvalds  * @vma: vm_area_struct holding the applicable pages
8771da177e4SLinus Torvalds  * @address: starting address of pages to zap
8781da177e4SLinus Torvalds  * @size: number of bytes to zap
8791da177e4SLinus Torvalds  * @details: details of nonlinear truncation or shared cache invalidation
8801da177e4SLinus Torvalds  */
881ee39b37bSHugh Dickins unsigned long zap_page_range(struct vm_area_struct *vma, unsigned long address,
8821da177e4SLinus Torvalds 		unsigned long size, struct zap_details *details)
8831da177e4SLinus Torvalds {
8841da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
8851da177e4SLinus Torvalds 	struct mmu_gather *tlb;
8861da177e4SLinus Torvalds 	unsigned long end = address + size;
8871da177e4SLinus Torvalds 	unsigned long nr_accounted = 0;
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 	lru_add_drain();
8901da177e4SLinus Torvalds 	tlb = tlb_gather_mmu(mm, 0);
891365e9c87SHugh Dickins 	update_hiwater_rss(mm);
892508034a3SHugh Dickins 	end = unmap_vmas(&tlb, vma, address, end, &nr_accounted, details);
893508034a3SHugh Dickins 	if (tlb)
8948f4f8c16SHugh Dickins 		tlb_finish_mmu(tlb, address, end);
895ee39b37bSHugh Dickins 	return end;
8961da177e4SLinus Torvalds }
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds /*
8991da177e4SLinus Torvalds  * Do a quick page-table lookup for a single page.
9001da177e4SLinus Torvalds  */
9016aab341eSLinus Torvalds struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
902deceb6cdSHugh Dickins 			unsigned int flags)
9031da177e4SLinus Torvalds {
9041da177e4SLinus Torvalds 	pgd_t *pgd;
9051da177e4SLinus Torvalds 	pud_t *pud;
9061da177e4SLinus Torvalds 	pmd_t *pmd;
9071da177e4SLinus Torvalds 	pte_t *ptep, pte;
908deceb6cdSHugh Dickins 	spinlock_t *ptl;
9091da177e4SLinus Torvalds 	struct page *page;
9106aab341eSLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
9111da177e4SLinus Torvalds 
912deceb6cdSHugh Dickins 	page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
913deceb6cdSHugh Dickins 	if (!IS_ERR(page)) {
914deceb6cdSHugh Dickins 		BUG_ON(flags & FOLL_GET);
915deceb6cdSHugh Dickins 		goto out;
916deceb6cdSHugh Dickins 	}
9171da177e4SLinus Torvalds 
918deceb6cdSHugh Dickins 	page = NULL;
9191da177e4SLinus Torvalds 	pgd = pgd_offset(mm, address);
9201da177e4SLinus Torvalds 	if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
921deceb6cdSHugh Dickins 		goto no_page_table;
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds 	pud = pud_offset(pgd, address);
9241da177e4SLinus Torvalds 	if (pud_none(*pud) || unlikely(pud_bad(*pud)))
925deceb6cdSHugh Dickins 		goto no_page_table;
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 	pmd = pmd_offset(pud, address);
9281da177e4SLinus Torvalds 	if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
929deceb6cdSHugh Dickins 		goto no_page_table;
9301da177e4SLinus Torvalds 
931deceb6cdSHugh Dickins 	if (pmd_huge(*pmd)) {
932deceb6cdSHugh Dickins 		BUG_ON(flags & FOLL_GET);
933deceb6cdSHugh Dickins 		page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE);
934deceb6cdSHugh Dickins 		goto out;
935deceb6cdSHugh Dickins 	}
936deceb6cdSHugh Dickins 
937deceb6cdSHugh Dickins 	ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
9381da177e4SLinus Torvalds 	if (!ptep)
9391da177e4SLinus Torvalds 		goto out;
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds 	pte = *ptep;
942deceb6cdSHugh Dickins 	if (!pte_present(pte))
943deceb6cdSHugh Dickins 		goto unlock;
944deceb6cdSHugh Dickins 	if ((flags & FOLL_WRITE) && !pte_write(pte))
945deceb6cdSHugh Dickins 		goto unlock;
9466aab341eSLinus Torvalds 	page = vm_normal_page(vma, address, pte);
9476aab341eSLinus Torvalds 	if (unlikely(!page))
948deceb6cdSHugh Dickins 		goto unlock;
949deceb6cdSHugh Dickins 
950deceb6cdSHugh Dickins 	if (flags & FOLL_GET)
951deceb6cdSHugh Dickins 		get_page(page);
952deceb6cdSHugh Dickins 	if (flags & FOLL_TOUCH) {
953deceb6cdSHugh Dickins 		if ((flags & FOLL_WRITE) &&
954deceb6cdSHugh Dickins 		    !pte_dirty(pte) && !PageDirty(page))
955f33ea7f4SNick Piggin 			set_page_dirty(page);
9561da177e4SLinus Torvalds 		mark_page_accessed(page);
9571da177e4SLinus Torvalds 	}
958deceb6cdSHugh Dickins unlock:
959deceb6cdSHugh Dickins 	pte_unmap_unlock(ptep, ptl);
9601da177e4SLinus Torvalds out:
961deceb6cdSHugh Dickins 	return page;
962deceb6cdSHugh Dickins 
963deceb6cdSHugh Dickins no_page_table:
964deceb6cdSHugh Dickins 	/*
965deceb6cdSHugh Dickins 	 * When core dumping an enormous anonymous area that nobody
966deceb6cdSHugh Dickins 	 * has touched so far, we don't want to allocate page tables.
967deceb6cdSHugh Dickins 	 */
968deceb6cdSHugh Dickins 	if (flags & FOLL_ANON) {
969deceb6cdSHugh Dickins 		page = ZERO_PAGE(address);
970deceb6cdSHugh Dickins 		if (flags & FOLL_GET)
971deceb6cdSHugh Dickins 			get_page(page);
972deceb6cdSHugh Dickins 		BUG_ON(flags & FOLL_WRITE);
9731da177e4SLinus Torvalds 	}
974deceb6cdSHugh Dickins 	return page;
9751da177e4SLinus Torvalds }
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
9781da177e4SLinus Torvalds 		unsigned long start, int len, int write, int force,
9791da177e4SLinus Torvalds 		struct page **pages, struct vm_area_struct **vmas)
9801da177e4SLinus Torvalds {
9811da177e4SLinus Torvalds 	int i;
982deceb6cdSHugh Dickins 	unsigned int vm_flags;
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds 	/*
9851da177e4SLinus Torvalds 	 * Require read or write permissions.
9861da177e4SLinus Torvalds 	 * If 'force' is set, we only require the "MAY" flags.
9871da177e4SLinus Torvalds 	 */
988deceb6cdSHugh Dickins 	vm_flags  = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
989deceb6cdSHugh Dickins 	vm_flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
9901da177e4SLinus Torvalds 	i = 0;
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds 	do {
9931da177e4SLinus Torvalds 		struct vm_area_struct *vma;
994deceb6cdSHugh Dickins 		unsigned int foll_flags;
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds 		vma = find_extend_vma(mm, start);
9971da177e4SLinus Torvalds 		if (!vma && in_gate_area(tsk, start)) {
9981da177e4SLinus Torvalds 			unsigned long pg = start & PAGE_MASK;
9991da177e4SLinus Torvalds 			struct vm_area_struct *gate_vma = get_gate_vma(tsk);
10001da177e4SLinus Torvalds 			pgd_t *pgd;
10011da177e4SLinus Torvalds 			pud_t *pud;
10021da177e4SLinus Torvalds 			pmd_t *pmd;
10031da177e4SLinus Torvalds 			pte_t *pte;
10041da177e4SLinus Torvalds 			if (write) /* user gate pages are read-only */
10051da177e4SLinus Torvalds 				return i ? : -EFAULT;
10061da177e4SLinus Torvalds 			if (pg > TASK_SIZE)
10071da177e4SLinus Torvalds 				pgd = pgd_offset_k(pg);
10081da177e4SLinus Torvalds 			else
10091da177e4SLinus Torvalds 				pgd = pgd_offset_gate(mm, pg);
10101da177e4SLinus Torvalds 			BUG_ON(pgd_none(*pgd));
10111da177e4SLinus Torvalds 			pud = pud_offset(pgd, pg);
10121da177e4SLinus Torvalds 			BUG_ON(pud_none(*pud));
10131da177e4SLinus Torvalds 			pmd = pmd_offset(pud, pg);
1014690dbe1cSHugh Dickins 			if (pmd_none(*pmd))
1015690dbe1cSHugh Dickins 				return i ? : -EFAULT;
10161da177e4SLinus Torvalds 			pte = pte_offset_map(pmd, pg);
1017690dbe1cSHugh Dickins 			if (pte_none(*pte)) {
1018690dbe1cSHugh Dickins 				pte_unmap(pte);
1019690dbe1cSHugh Dickins 				return i ? : -EFAULT;
1020690dbe1cSHugh Dickins 			}
10211da177e4SLinus Torvalds 			if (pages) {
1022fa2a455bSNick Piggin 				struct page *page = vm_normal_page(gate_vma, start, *pte);
10236aab341eSLinus Torvalds 				pages[i] = page;
10246aab341eSLinus Torvalds 				if (page)
10256aab341eSLinus Torvalds 					get_page(page);
10261da177e4SLinus Torvalds 			}
10271da177e4SLinus Torvalds 			pte_unmap(pte);
10281da177e4SLinus Torvalds 			if (vmas)
10291da177e4SLinus Torvalds 				vmas[i] = gate_vma;
10301da177e4SLinus Torvalds 			i++;
10311da177e4SLinus Torvalds 			start += PAGE_SIZE;
10321da177e4SLinus Torvalds 			len--;
10331da177e4SLinus Torvalds 			continue;
10341da177e4SLinus Torvalds 		}
10351da177e4SLinus Torvalds 
10361ff80389SLinus Torvalds 		if (!vma || (vma->vm_flags & (VM_IO | VM_PFNMAP))
1037deceb6cdSHugh Dickins 				|| !(vm_flags & vma->vm_flags))
10381da177e4SLinus Torvalds 			return i ? : -EFAULT;
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds 		if (is_vm_hugetlb_page(vma)) {
10411da177e4SLinus Torvalds 			i = follow_hugetlb_page(mm, vma, pages, vmas,
10421da177e4SLinus Torvalds 						&start, &len, i);
10431da177e4SLinus Torvalds 			continue;
10441da177e4SLinus Torvalds 		}
1045deceb6cdSHugh Dickins 
1046deceb6cdSHugh Dickins 		foll_flags = FOLL_TOUCH;
1047deceb6cdSHugh Dickins 		if (pages)
1048deceb6cdSHugh Dickins 			foll_flags |= FOLL_GET;
1049deceb6cdSHugh Dickins 		if (!write && !(vma->vm_flags & VM_LOCKED) &&
1050deceb6cdSHugh Dickins 		    (!vma->vm_ops || !vma->vm_ops->nopage))
1051deceb6cdSHugh Dickins 			foll_flags |= FOLL_ANON;
1052deceb6cdSHugh Dickins 
10531da177e4SLinus Torvalds 		do {
105408ef4729SHugh Dickins 			struct page *page;
10551da177e4SLinus Torvalds 
1056462e00ccSEthan Solomita 			/*
1057462e00ccSEthan Solomita 			 * If tsk is ooming, cut off its access to large memory
1058462e00ccSEthan Solomita 			 * allocations. It has a pending SIGKILL, but it can't
1059462e00ccSEthan Solomita 			 * be processed until returning to user space.
1060462e00ccSEthan Solomita 			 */
1061462e00ccSEthan Solomita 			if (unlikely(test_tsk_thread_flag(tsk, TIF_MEMDIE)))
1062462e00ccSEthan Solomita 				return -ENOMEM;
1063462e00ccSEthan Solomita 
1064deceb6cdSHugh Dickins 			if (write)
1065deceb6cdSHugh Dickins 				foll_flags |= FOLL_WRITE;
1066deceb6cdSHugh Dickins 
1067deceb6cdSHugh Dickins 			cond_resched();
10686aab341eSLinus Torvalds 			while (!(page = follow_page(vma, start, foll_flags))) {
1069a68d2ebcSLinus Torvalds 				int ret;
1070deceb6cdSHugh Dickins 				ret = __handle_mm_fault(mm, vma, start,
1071deceb6cdSHugh Dickins 						foll_flags & FOLL_WRITE);
1072f33ea7f4SNick Piggin 				/*
1073a68d2ebcSLinus Torvalds 				 * The VM_FAULT_WRITE bit tells us that do_wp_page has
1074a68d2ebcSLinus Torvalds 				 * broken COW when necessary, even if maybe_mkwrite
1075a68d2ebcSLinus Torvalds 				 * decided not to set pte_write. We can thus safely do
1076a68d2ebcSLinus Torvalds 				 * subsequent page lookups as if they were reads.
1077f33ea7f4SNick Piggin 				 */
1078a68d2ebcSLinus Torvalds 				if (ret & VM_FAULT_WRITE)
1079deceb6cdSHugh Dickins 					foll_flags &= ~FOLL_WRITE;
1080a68d2ebcSLinus Torvalds 
1081a68d2ebcSLinus Torvalds 				switch (ret & ~VM_FAULT_WRITE) {
10821da177e4SLinus Torvalds 				case VM_FAULT_MINOR:
10831da177e4SLinus Torvalds 					tsk->min_flt++;
10841da177e4SLinus Torvalds 					break;
10851da177e4SLinus Torvalds 				case VM_FAULT_MAJOR:
10861da177e4SLinus Torvalds 					tsk->maj_flt++;
10871da177e4SLinus Torvalds 					break;
10881da177e4SLinus Torvalds 				case VM_FAULT_SIGBUS:
10891da177e4SLinus Torvalds 					return i ? i : -EFAULT;
10901da177e4SLinus Torvalds 				case VM_FAULT_OOM:
10911da177e4SLinus Torvalds 					return i ? i : -ENOMEM;
10921da177e4SLinus Torvalds 				default:
10931da177e4SLinus Torvalds 					BUG();
10941da177e4SLinus Torvalds 				}
10957f7bbbe5SBenjamin Herrenschmidt 				cond_resched();
10961da177e4SLinus Torvalds 			}
10971da177e4SLinus Torvalds 			if (pages) {
109808ef4729SHugh Dickins 				pages[i] = page;
109903beb076SJames Bottomley 
1100a6f36be3SRussell King 				flush_anon_page(vma, page, start);
110108ef4729SHugh Dickins 				flush_dcache_page(page);
11021da177e4SLinus Torvalds 			}
11031da177e4SLinus Torvalds 			if (vmas)
11041da177e4SLinus Torvalds 				vmas[i] = vma;
11051da177e4SLinus Torvalds 			i++;
11061da177e4SLinus Torvalds 			start += PAGE_SIZE;
11071da177e4SLinus Torvalds 			len--;
11081da177e4SLinus Torvalds 		} while (len && start < vma->vm_end);
11091da177e4SLinus Torvalds 	} while (len);
11101da177e4SLinus Torvalds 	return i;
11111da177e4SLinus Torvalds }
11121da177e4SLinus Torvalds EXPORT_SYMBOL(get_user_pages);
11131da177e4SLinus Torvalds 
11141da177e4SLinus Torvalds static int zeromap_pte_range(struct mm_struct *mm, pmd_t *pmd,
11151da177e4SLinus Torvalds 			unsigned long addr, unsigned long end, pgprot_t prot)
11161da177e4SLinus Torvalds {
11171da177e4SLinus Torvalds 	pte_t *pte;
1118c74df32cSHugh Dickins 	spinlock_t *ptl;
11195fcf7bb7SHugh Dickins 	int err = 0;
11201da177e4SLinus Torvalds 
1121c74df32cSHugh Dickins 	pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
11221da177e4SLinus Torvalds 	if (!pte)
11235fcf7bb7SHugh Dickins 		return -EAGAIN;
11246606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
11251da177e4SLinus Torvalds 	do {
1126b5810039SNick Piggin 		struct page *page = ZERO_PAGE(addr);
1127b5810039SNick Piggin 		pte_t zero_pte = pte_wrprotect(mk_pte(page, prot));
11285fcf7bb7SHugh Dickins 
11295fcf7bb7SHugh Dickins 		if (unlikely(!pte_none(*pte))) {
11305fcf7bb7SHugh Dickins 			err = -EEXIST;
11315fcf7bb7SHugh Dickins 			pte++;
11325fcf7bb7SHugh Dickins 			break;
11335fcf7bb7SHugh Dickins 		}
1134b5810039SNick Piggin 		page_cache_get(page);
1135b5810039SNick Piggin 		page_add_file_rmap(page);
1136b5810039SNick Piggin 		inc_mm_counter(mm, file_rss);
11371da177e4SLinus Torvalds 		set_pte_at(mm, addr, pte, zero_pte);
11381da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
11396606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
1140c74df32cSHugh Dickins 	pte_unmap_unlock(pte - 1, ptl);
11415fcf7bb7SHugh Dickins 	return err;
11421da177e4SLinus Torvalds }
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds static inline int zeromap_pmd_range(struct mm_struct *mm, pud_t *pud,
11451da177e4SLinus Torvalds 			unsigned long addr, unsigned long end, pgprot_t prot)
11461da177e4SLinus Torvalds {
11471da177e4SLinus Torvalds 	pmd_t *pmd;
11481da177e4SLinus Torvalds 	unsigned long next;
11495fcf7bb7SHugh Dickins 	int err;
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds 	pmd = pmd_alloc(mm, pud, addr);
11521da177e4SLinus Torvalds 	if (!pmd)
11535fcf7bb7SHugh Dickins 		return -EAGAIN;
11541da177e4SLinus Torvalds 	do {
11551da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
11565fcf7bb7SHugh Dickins 		err = zeromap_pte_range(mm, pmd, addr, next, prot);
11575fcf7bb7SHugh Dickins 		if (err)
11585fcf7bb7SHugh Dickins 			break;
11591da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
11605fcf7bb7SHugh Dickins 	return err;
11611da177e4SLinus Torvalds }
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds static inline int zeromap_pud_range(struct mm_struct *mm, pgd_t *pgd,
11641da177e4SLinus Torvalds 			unsigned long addr, unsigned long end, pgprot_t prot)
11651da177e4SLinus Torvalds {
11661da177e4SLinus Torvalds 	pud_t *pud;
11671da177e4SLinus Torvalds 	unsigned long next;
11685fcf7bb7SHugh Dickins 	int err;
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 	pud = pud_alloc(mm, pgd, addr);
11711da177e4SLinus Torvalds 	if (!pud)
11725fcf7bb7SHugh Dickins 		return -EAGAIN;
11731da177e4SLinus Torvalds 	do {
11741da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
11755fcf7bb7SHugh Dickins 		err = zeromap_pmd_range(mm, pud, addr, next, prot);
11765fcf7bb7SHugh Dickins 		if (err)
11775fcf7bb7SHugh Dickins 			break;
11781da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
11795fcf7bb7SHugh Dickins 	return err;
11801da177e4SLinus Torvalds }
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds int zeromap_page_range(struct vm_area_struct *vma,
11831da177e4SLinus Torvalds 			unsigned long addr, unsigned long size, pgprot_t prot)
11841da177e4SLinus Torvalds {
11851da177e4SLinus Torvalds 	pgd_t *pgd;
11861da177e4SLinus Torvalds 	unsigned long next;
11871da177e4SLinus Torvalds 	unsigned long end = addr + size;
11881da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
11891da177e4SLinus Torvalds 	int err;
11901da177e4SLinus Torvalds 
11911da177e4SLinus Torvalds 	BUG_ON(addr >= end);
11921da177e4SLinus Torvalds 	pgd = pgd_offset(mm, addr);
11931da177e4SLinus Torvalds 	flush_cache_range(vma, addr, end);
11941da177e4SLinus Torvalds 	do {
11951da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
11961da177e4SLinus Torvalds 		err = zeromap_pud_range(mm, pgd, addr, next, prot);
11971da177e4SLinus Torvalds 		if (err)
11981da177e4SLinus Torvalds 			break;
11991da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
12001da177e4SLinus Torvalds 	return err;
12011da177e4SLinus Torvalds }
12021da177e4SLinus Torvalds 
120349c91fb0STrond Myklebust pte_t * fastcall get_locked_pte(struct mm_struct *mm, unsigned long addr, spinlock_t **ptl)
1204c9cfcddfSLinus Torvalds {
1205c9cfcddfSLinus Torvalds 	pgd_t * pgd = pgd_offset(mm, addr);
1206c9cfcddfSLinus Torvalds 	pud_t * pud = pud_alloc(mm, pgd, addr);
1207c9cfcddfSLinus Torvalds 	if (pud) {
120849c91fb0STrond Myklebust 		pmd_t * pmd = pmd_alloc(mm, pud, addr);
1209c9cfcddfSLinus Torvalds 		if (pmd)
1210c9cfcddfSLinus Torvalds 			return pte_alloc_map_lock(mm, pmd, addr, ptl);
1211c9cfcddfSLinus Torvalds 	}
1212c9cfcddfSLinus Torvalds 	return NULL;
1213c9cfcddfSLinus Torvalds }
1214c9cfcddfSLinus Torvalds 
12151da177e4SLinus Torvalds /*
1216238f58d8SLinus Torvalds  * This is the old fallback for page remapping.
1217238f58d8SLinus Torvalds  *
1218238f58d8SLinus Torvalds  * For historical reasons, it only allows reserved pages. Only
1219238f58d8SLinus Torvalds  * old drivers should use this, and they needed to mark their
1220238f58d8SLinus Torvalds  * pages reserved for the old functions anyway.
1221238f58d8SLinus Torvalds  */
1222238f58d8SLinus Torvalds static int insert_page(struct mm_struct *mm, unsigned long addr, struct page *page, pgprot_t prot)
1223238f58d8SLinus Torvalds {
1224238f58d8SLinus Torvalds 	int retval;
1225238f58d8SLinus Torvalds 	pte_t *pte;
1226238f58d8SLinus Torvalds 	spinlock_t *ptl;
1227238f58d8SLinus Torvalds 
1228238f58d8SLinus Torvalds 	retval = -EINVAL;
1229a145dd41SLinus Torvalds 	if (PageAnon(page))
1230238f58d8SLinus Torvalds 		goto out;
1231238f58d8SLinus Torvalds 	retval = -ENOMEM;
1232238f58d8SLinus Torvalds 	flush_dcache_page(page);
1233c9cfcddfSLinus Torvalds 	pte = get_locked_pte(mm, addr, &ptl);
1234238f58d8SLinus Torvalds 	if (!pte)
1235238f58d8SLinus Torvalds 		goto out;
1236238f58d8SLinus Torvalds 	retval = -EBUSY;
1237238f58d8SLinus Torvalds 	if (!pte_none(*pte))
1238238f58d8SLinus Torvalds 		goto out_unlock;
1239238f58d8SLinus Torvalds 
1240238f58d8SLinus Torvalds 	/* Ok, finally just insert the thing.. */
1241238f58d8SLinus Torvalds 	get_page(page);
1242238f58d8SLinus Torvalds 	inc_mm_counter(mm, file_rss);
1243238f58d8SLinus Torvalds 	page_add_file_rmap(page);
1244238f58d8SLinus Torvalds 	set_pte_at(mm, addr, pte, mk_pte(page, prot));
1245238f58d8SLinus Torvalds 
1246238f58d8SLinus Torvalds 	retval = 0;
1247238f58d8SLinus Torvalds out_unlock:
1248238f58d8SLinus Torvalds 	pte_unmap_unlock(pte, ptl);
1249238f58d8SLinus Torvalds out:
1250238f58d8SLinus Torvalds 	return retval;
1251238f58d8SLinus Torvalds }
1252238f58d8SLinus Torvalds 
1253bfa5bf6dSRolf Eike Beer /**
1254bfa5bf6dSRolf Eike Beer  * vm_insert_page - insert single page into user vma
1255bfa5bf6dSRolf Eike Beer  * @vma: user vma to map to
1256bfa5bf6dSRolf Eike Beer  * @addr: target user address of this page
1257bfa5bf6dSRolf Eike Beer  * @page: source kernel page
1258bfa5bf6dSRolf Eike Beer  *
1259a145dd41SLinus Torvalds  * This allows drivers to insert individual pages they've allocated
1260a145dd41SLinus Torvalds  * into a user vma.
1261a145dd41SLinus Torvalds  *
1262a145dd41SLinus Torvalds  * The page has to be a nice clean _individual_ kernel allocation.
1263a145dd41SLinus Torvalds  * If you allocate a compound page, you need to have marked it as
1264a145dd41SLinus Torvalds  * such (__GFP_COMP), or manually just split the page up yourself
12658dfcc9baSNick Piggin  * (see split_page()).
1266a145dd41SLinus Torvalds  *
1267a145dd41SLinus Torvalds  * NOTE! Traditionally this was done with "remap_pfn_range()" which
1268a145dd41SLinus Torvalds  * took an arbitrary page protection parameter. This doesn't allow
1269a145dd41SLinus Torvalds  * that. Your vma protection will have to be set up correctly, which
1270a145dd41SLinus Torvalds  * means that if you want a shared writable mapping, you'd better
1271a145dd41SLinus Torvalds  * ask for a shared writable mapping!
1272a145dd41SLinus Torvalds  *
1273a145dd41SLinus Torvalds  * The page does not need to be reserved.
1274a145dd41SLinus Torvalds  */
1275a145dd41SLinus Torvalds int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, struct page *page)
1276a145dd41SLinus Torvalds {
1277a145dd41SLinus Torvalds 	if (addr < vma->vm_start || addr >= vma->vm_end)
1278a145dd41SLinus Torvalds 		return -EFAULT;
1279a145dd41SLinus Torvalds 	if (!page_count(page))
1280a145dd41SLinus Torvalds 		return -EINVAL;
12814d7672b4SLinus Torvalds 	vma->vm_flags |= VM_INSERTPAGE;
1282a145dd41SLinus Torvalds 	return insert_page(vma->vm_mm, addr, page, vma->vm_page_prot);
1283a145dd41SLinus Torvalds }
1284e3c3374fSLinus Torvalds EXPORT_SYMBOL(vm_insert_page);
1285a145dd41SLinus Torvalds 
1286e0dc0d8fSNick Piggin /**
1287e0dc0d8fSNick Piggin  * vm_insert_pfn - insert single pfn into user vma
1288e0dc0d8fSNick Piggin  * @vma: user vma to map to
1289e0dc0d8fSNick Piggin  * @addr: target user address of this page
1290e0dc0d8fSNick Piggin  * @pfn: source kernel pfn
1291e0dc0d8fSNick Piggin  *
1292e0dc0d8fSNick Piggin  * Similar to vm_inert_page, this allows drivers to insert individual pages
1293e0dc0d8fSNick Piggin  * they've allocated into a user vma. Same comments apply.
1294e0dc0d8fSNick Piggin  *
1295e0dc0d8fSNick Piggin  * This function should only be called from a vm_ops->fault handler, and
1296e0dc0d8fSNick Piggin  * in that case the handler should return NULL.
1297e0dc0d8fSNick Piggin  */
1298e0dc0d8fSNick Piggin int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
1299e0dc0d8fSNick Piggin 		unsigned long pfn)
1300e0dc0d8fSNick Piggin {
1301e0dc0d8fSNick Piggin 	struct mm_struct *mm = vma->vm_mm;
1302e0dc0d8fSNick Piggin 	int retval;
1303e0dc0d8fSNick Piggin 	pte_t *pte, entry;
1304e0dc0d8fSNick Piggin 	spinlock_t *ptl;
1305e0dc0d8fSNick Piggin 
1306e0dc0d8fSNick Piggin 	BUG_ON(!(vma->vm_flags & VM_PFNMAP));
1307e0dc0d8fSNick Piggin 	BUG_ON(is_cow_mapping(vma->vm_flags));
1308e0dc0d8fSNick Piggin 
1309e0dc0d8fSNick Piggin 	retval = -ENOMEM;
1310e0dc0d8fSNick Piggin 	pte = get_locked_pte(mm, addr, &ptl);
1311e0dc0d8fSNick Piggin 	if (!pte)
1312e0dc0d8fSNick Piggin 		goto out;
1313e0dc0d8fSNick Piggin 	retval = -EBUSY;
1314e0dc0d8fSNick Piggin 	if (!pte_none(*pte))
1315e0dc0d8fSNick Piggin 		goto out_unlock;
1316e0dc0d8fSNick Piggin 
1317e0dc0d8fSNick Piggin 	/* Ok, finally just insert the thing.. */
1318e0dc0d8fSNick Piggin 	entry = pfn_pte(pfn, vma->vm_page_prot);
1319e0dc0d8fSNick Piggin 	set_pte_at(mm, addr, pte, entry);
1320e0dc0d8fSNick Piggin 	update_mmu_cache(vma, addr, entry);
1321e0dc0d8fSNick Piggin 
1322e0dc0d8fSNick Piggin 	retval = 0;
1323e0dc0d8fSNick Piggin out_unlock:
1324e0dc0d8fSNick Piggin 	pte_unmap_unlock(pte, ptl);
1325e0dc0d8fSNick Piggin 
1326e0dc0d8fSNick Piggin out:
1327e0dc0d8fSNick Piggin 	return retval;
1328e0dc0d8fSNick Piggin }
1329e0dc0d8fSNick Piggin EXPORT_SYMBOL(vm_insert_pfn);
1330e0dc0d8fSNick Piggin 
1331a145dd41SLinus Torvalds /*
13321da177e4SLinus Torvalds  * maps a range of physical memory into the requested pages. the old
13331da177e4SLinus Torvalds  * mappings are removed. any references to nonexistent pages results
13341da177e4SLinus Torvalds  * in null mappings (currently treated as "copy-on-access")
13351da177e4SLinus Torvalds  */
13361da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
13371da177e4SLinus Torvalds 			unsigned long addr, unsigned long end,
13381da177e4SLinus Torvalds 			unsigned long pfn, pgprot_t prot)
13391da177e4SLinus Torvalds {
13401da177e4SLinus Torvalds 	pte_t *pte;
1341c74df32cSHugh Dickins 	spinlock_t *ptl;
13421da177e4SLinus Torvalds 
1343c74df32cSHugh Dickins 	pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
13441da177e4SLinus Torvalds 	if (!pte)
13451da177e4SLinus Torvalds 		return -ENOMEM;
13466606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
13471da177e4SLinus Torvalds 	do {
13481da177e4SLinus Torvalds 		BUG_ON(!pte_none(*pte));
13491da177e4SLinus Torvalds 		set_pte_at(mm, addr, pte, pfn_pte(pfn, prot));
13501da177e4SLinus Torvalds 		pfn++;
13511da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
13526606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
1353c74df32cSHugh Dickins 	pte_unmap_unlock(pte - 1, ptl);
13541da177e4SLinus Torvalds 	return 0;
13551da177e4SLinus Torvalds }
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
13581da177e4SLinus Torvalds 			unsigned long addr, unsigned long end,
13591da177e4SLinus Torvalds 			unsigned long pfn, pgprot_t prot)
13601da177e4SLinus Torvalds {
13611da177e4SLinus Torvalds 	pmd_t *pmd;
13621da177e4SLinus Torvalds 	unsigned long next;
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds 	pfn -= addr >> PAGE_SHIFT;
13651da177e4SLinus Torvalds 	pmd = pmd_alloc(mm, pud, addr);
13661da177e4SLinus Torvalds 	if (!pmd)
13671da177e4SLinus Torvalds 		return -ENOMEM;
13681da177e4SLinus Torvalds 	do {
13691da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
13701da177e4SLinus Torvalds 		if (remap_pte_range(mm, pmd, addr, next,
13711da177e4SLinus Torvalds 				pfn + (addr >> PAGE_SHIFT), prot))
13721da177e4SLinus Torvalds 			return -ENOMEM;
13731da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
13741da177e4SLinus Torvalds 	return 0;
13751da177e4SLinus Torvalds }
13761da177e4SLinus Torvalds 
13771da177e4SLinus Torvalds static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd,
13781da177e4SLinus Torvalds 			unsigned long addr, unsigned long end,
13791da177e4SLinus Torvalds 			unsigned long pfn, pgprot_t prot)
13801da177e4SLinus Torvalds {
13811da177e4SLinus Torvalds 	pud_t *pud;
13821da177e4SLinus Torvalds 	unsigned long next;
13831da177e4SLinus Torvalds 
13841da177e4SLinus Torvalds 	pfn -= addr >> PAGE_SHIFT;
13851da177e4SLinus Torvalds 	pud = pud_alloc(mm, pgd, addr);
13861da177e4SLinus Torvalds 	if (!pud)
13871da177e4SLinus Torvalds 		return -ENOMEM;
13881da177e4SLinus Torvalds 	do {
13891da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
13901da177e4SLinus Torvalds 		if (remap_pmd_range(mm, pud, addr, next,
13911da177e4SLinus Torvalds 				pfn + (addr >> PAGE_SHIFT), prot))
13921da177e4SLinus Torvalds 			return -ENOMEM;
13931da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
13941da177e4SLinus Torvalds 	return 0;
13951da177e4SLinus Torvalds }
13961da177e4SLinus Torvalds 
1397bfa5bf6dSRolf Eike Beer /**
1398bfa5bf6dSRolf Eike Beer  * remap_pfn_range - remap kernel memory to userspace
1399bfa5bf6dSRolf Eike Beer  * @vma: user vma to map to
1400bfa5bf6dSRolf Eike Beer  * @addr: target user address to start at
1401bfa5bf6dSRolf Eike Beer  * @pfn: physical address of kernel memory
1402bfa5bf6dSRolf Eike Beer  * @size: size of map area
1403bfa5bf6dSRolf Eike Beer  * @prot: page protection flags for this mapping
1404bfa5bf6dSRolf Eike Beer  *
1405bfa5bf6dSRolf Eike Beer  *  Note: this is only safe if the mm semaphore is held when called.
1406bfa5bf6dSRolf Eike Beer  */
14071da177e4SLinus Torvalds int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
14081da177e4SLinus Torvalds 		    unsigned long pfn, unsigned long size, pgprot_t prot)
14091da177e4SLinus Torvalds {
14101da177e4SLinus Torvalds 	pgd_t *pgd;
14111da177e4SLinus Torvalds 	unsigned long next;
14122d15cab8SHugh Dickins 	unsigned long end = addr + PAGE_ALIGN(size);
14131da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
14141da177e4SLinus Torvalds 	int err;
14151da177e4SLinus Torvalds 
14161da177e4SLinus Torvalds 	/*
14171da177e4SLinus Torvalds 	 * Physically remapped pages are special. Tell the
14181da177e4SLinus Torvalds 	 * rest of the world about it:
14191da177e4SLinus Torvalds 	 *   VM_IO tells people not to look at these pages
14201da177e4SLinus Torvalds 	 *	(accesses can have side effects).
14210b14c179SHugh Dickins 	 *   VM_RESERVED is specified all over the place, because
14220b14c179SHugh Dickins 	 *	in 2.4 it kept swapout's vma scan off this vma; but
14230b14c179SHugh Dickins 	 *	in 2.6 the LRU scan won't even find its pages, so this
14240b14c179SHugh Dickins 	 *	flag means no more than count its pages in reserved_vm,
14250b14c179SHugh Dickins 	 * 	and omit it from core dump, even when VM_IO turned off.
14266aab341eSLinus Torvalds 	 *   VM_PFNMAP tells the core MM that the base pages are just
14276aab341eSLinus Torvalds 	 *	raw PFN mappings, and do not have a "struct page" associated
14286aab341eSLinus Torvalds 	 *	with them.
1429fb155c16SLinus Torvalds 	 *
1430fb155c16SLinus Torvalds 	 * There's a horrible special case to handle copy-on-write
1431fb155c16SLinus Torvalds 	 * behaviour that some programs depend on. We mark the "original"
1432fb155c16SLinus Torvalds 	 * un-COW'ed pages by matching them up with "vma->vm_pgoff".
14331da177e4SLinus Torvalds 	 */
143467121172SLinus Torvalds 	if (is_cow_mapping(vma->vm_flags)) {
1435fb155c16SLinus Torvalds 		if (addr != vma->vm_start || end != vma->vm_end)
14367fc7e2eeSLinus Torvalds 			return -EINVAL;
14376aab341eSLinus Torvalds 		vma->vm_pgoff = pfn;
1438fb155c16SLinus Torvalds 	}
1439fb155c16SLinus Torvalds 
1440fb155c16SLinus Torvalds 	vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP;
14411da177e4SLinus Torvalds 
14421da177e4SLinus Torvalds 	BUG_ON(addr >= end);
14431da177e4SLinus Torvalds 	pfn -= addr >> PAGE_SHIFT;
14441da177e4SLinus Torvalds 	pgd = pgd_offset(mm, addr);
14451da177e4SLinus Torvalds 	flush_cache_range(vma, addr, end);
14461da177e4SLinus Torvalds 	do {
14471da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
14481da177e4SLinus Torvalds 		err = remap_pud_range(mm, pgd, addr, next,
14491da177e4SLinus Torvalds 				pfn + (addr >> PAGE_SHIFT), prot);
14501da177e4SLinus Torvalds 		if (err)
14511da177e4SLinus Torvalds 			break;
14521da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
14531da177e4SLinus Torvalds 	return err;
14541da177e4SLinus Torvalds }
14551da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range);
14561da177e4SLinus Torvalds 
1457aee16b3cSJeremy Fitzhardinge static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
1458aee16b3cSJeremy Fitzhardinge 				     unsigned long addr, unsigned long end,
1459aee16b3cSJeremy Fitzhardinge 				     pte_fn_t fn, void *data)
1460aee16b3cSJeremy Fitzhardinge {
1461aee16b3cSJeremy Fitzhardinge 	pte_t *pte;
1462aee16b3cSJeremy Fitzhardinge 	int err;
1463aee16b3cSJeremy Fitzhardinge 	struct page *pmd_page;
146494909914SBorislav Petkov 	spinlock_t *uninitialized_var(ptl);
1465aee16b3cSJeremy Fitzhardinge 
1466aee16b3cSJeremy Fitzhardinge 	pte = (mm == &init_mm) ?
1467aee16b3cSJeremy Fitzhardinge 		pte_alloc_kernel(pmd, addr) :
1468aee16b3cSJeremy Fitzhardinge 		pte_alloc_map_lock(mm, pmd, addr, &ptl);
1469aee16b3cSJeremy Fitzhardinge 	if (!pte)
1470aee16b3cSJeremy Fitzhardinge 		return -ENOMEM;
1471aee16b3cSJeremy Fitzhardinge 
1472aee16b3cSJeremy Fitzhardinge 	BUG_ON(pmd_huge(*pmd));
1473aee16b3cSJeremy Fitzhardinge 
1474aee16b3cSJeremy Fitzhardinge 	pmd_page = pmd_page(*pmd);
1475aee16b3cSJeremy Fitzhardinge 
1476aee16b3cSJeremy Fitzhardinge 	do {
1477aee16b3cSJeremy Fitzhardinge 		err = fn(pte, pmd_page, addr, data);
1478aee16b3cSJeremy Fitzhardinge 		if (err)
1479aee16b3cSJeremy Fitzhardinge 			break;
1480aee16b3cSJeremy Fitzhardinge 	} while (pte++, addr += PAGE_SIZE, addr != end);
1481aee16b3cSJeremy Fitzhardinge 
1482aee16b3cSJeremy Fitzhardinge 	if (mm != &init_mm)
1483aee16b3cSJeremy Fitzhardinge 		pte_unmap_unlock(pte-1, ptl);
1484aee16b3cSJeremy Fitzhardinge 	return err;
1485aee16b3cSJeremy Fitzhardinge }
1486aee16b3cSJeremy Fitzhardinge 
1487aee16b3cSJeremy Fitzhardinge static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
1488aee16b3cSJeremy Fitzhardinge 				     unsigned long addr, unsigned long end,
1489aee16b3cSJeremy Fitzhardinge 				     pte_fn_t fn, void *data)
1490aee16b3cSJeremy Fitzhardinge {
1491aee16b3cSJeremy Fitzhardinge 	pmd_t *pmd;
1492aee16b3cSJeremy Fitzhardinge 	unsigned long next;
1493aee16b3cSJeremy Fitzhardinge 	int err;
1494aee16b3cSJeremy Fitzhardinge 
1495aee16b3cSJeremy Fitzhardinge 	pmd = pmd_alloc(mm, pud, addr);
1496aee16b3cSJeremy Fitzhardinge 	if (!pmd)
1497aee16b3cSJeremy Fitzhardinge 		return -ENOMEM;
1498aee16b3cSJeremy Fitzhardinge 	do {
1499aee16b3cSJeremy Fitzhardinge 		next = pmd_addr_end(addr, end);
1500aee16b3cSJeremy Fitzhardinge 		err = apply_to_pte_range(mm, pmd, addr, next, fn, data);
1501aee16b3cSJeremy Fitzhardinge 		if (err)
1502aee16b3cSJeremy Fitzhardinge 			break;
1503aee16b3cSJeremy Fitzhardinge 	} while (pmd++, addr = next, addr != end);
1504aee16b3cSJeremy Fitzhardinge 	return err;
1505aee16b3cSJeremy Fitzhardinge }
1506aee16b3cSJeremy Fitzhardinge 
1507aee16b3cSJeremy Fitzhardinge static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd,
1508aee16b3cSJeremy Fitzhardinge 				     unsigned long addr, unsigned long end,
1509aee16b3cSJeremy Fitzhardinge 				     pte_fn_t fn, void *data)
1510aee16b3cSJeremy Fitzhardinge {
1511aee16b3cSJeremy Fitzhardinge 	pud_t *pud;
1512aee16b3cSJeremy Fitzhardinge 	unsigned long next;
1513aee16b3cSJeremy Fitzhardinge 	int err;
1514aee16b3cSJeremy Fitzhardinge 
1515aee16b3cSJeremy Fitzhardinge 	pud = pud_alloc(mm, pgd, addr);
1516aee16b3cSJeremy Fitzhardinge 	if (!pud)
1517aee16b3cSJeremy Fitzhardinge 		return -ENOMEM;
1518aee16b3cSJeremy Fitzhardinge 	do {
1519aee16b3cSJeremy Fitzhardinge 		next = pud_addr_end(addr, end);
1520aee16b3cSJeremy Fitzhardinge 		err = apply_to_pmd_range(mm, pud, addr, next, fn, data);
1521aee16b3cSJeremy Fitzhardinge 		if (err)
1522aee16b3cSJeremy Fitzhardinge 			break;
1523aee16b3cSJeremy Fitzhardinge 	} while (pud++, addr = next, addr != end);
1524aee16b3cSJeremy Fitzhardinge 	return err;
1525aee16b3cSJeremy Fitzhardinge }
1526aee16b3cSJeremy Fitzhardinge 
1527aee16b3cSJeremy Fitzhardinge /*
1528aee16b3cSJeremy Fitzhardinge  * Scan a region of virtual memory, filling in page tables as necessary
1529aee16b3cSJeremy Fitzhardinge  * and calling a provided function on each leaf page table.
1530aee16b3cSJeremy Fitzhardinge  */
1531aee16b3cSJeremy Fitzhardinge int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
1532aee16b3cSJeremy Fitzhardinge 			unsigned long size, pte_fn_t fn, void *data)
1533aee16b3cSJeremy Fitzhardinge {
1534aee16b3cSJeremy Fitzhardinge 	pgd_t *pgd;
1535aee16b3cSJeremy Fitzhardinge 	unsigned long next;
1536aee16b3cSJeremy Fitzhardinge 	unsigned long end = addr + size;
1537aee16b3cSJeremy Fitzhardinge 	int err;
1538aee16b3cSJeremy Fitzhardinge 
1539aee16b3cSJeremy Fitzhardinge 	BUG_ON(addr >= end);
1540aee16b3cSJeremy Fitzhardinge 	pgd = pgd_offset(mm, addr);
1541aee16b3cSJeremy Fitzhardinge 	do {
1542aee16b3cSJeremy Fitzhardinge 		next = pgd_addr_end(addr, end);
1543aee16b3cSJeremy Fitzhardinge 		err = apply_to_pud_range(mm, pgd, addr, next, fn, data);
1544aee16b3cSJeremy Fitzhardinge 		if (err)
1545aee16b3cSJeremy Fitzhardinge 			break;
1546aee16b3cSJeremy Fitzhardinge 	} while (pgd++, addr = next, addr != end);
1547aee16b3cSJeremy Fitzhardinge 	return err;
1548aee16b3cSJeremy Fitzhardinge }
1549aee16b3cSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(apply_to_page_range);
1550aee16b3cSJeremy Fitzhardinge 
15511da177e4SLinus Torvalds /*
15528f4e2101SHugh Dickins  * handle_pte_fault chooses page fault handler according to an entry
15538f4e2101SHugh Dickins  * which was read non-atomically.  Before making any commitment, on
15548f4e2101SHugh Dickins  * those architectures or configurations (e.g. i386 with PAE) which
15558f4e2101SHugh Dickins  * might give a mix of unmatched parts, do_swap_page and do_file_page
15568f4e2101SHugh Dickins  * must check under lock before unmapping the pte and proceeding
15578f4e2101SHugh Dickins  * (but do_wp_page is only called after already making such a check;
15588f4e2101SHugh Dickins  * and do_anonymous_page and do_no_page can safely check later on).
15598f4e2101SHugh Dickins  */
15604c21e2f2SHugh Dickins static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
15618f4e2101SHugh Dickins 				pte_t *page_table, pte_t orig_pte)
15628f4e2101SHugh Dickins {
15638f4e2101SHugh Dickins 	int same = 1;
15648f4e2101SHugh Dickins #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
15658f4e2101SHugh Dickins 	if (sizeof(pte_t) > sizeof(unsigned long)) {
15664c21e2f2SHugh Dickins 		spinlock_t *ptl = pte_lockptr(mm, pmd);
15674c21e2f2SHugh Dickins 		spin_lock(ptl);
15688f4e2101SHugh Dickins 		same = pte_same(*page_table, orig_pte);
15694c21e2f2SHugh Dickins 		spin_unlock(ptl);
15708f4e2101SHugh Dickins 	}
15718f4e2101SHugh Dickins #endif
15728f4e2101SHugh Dickins 	pte_unmap(page_table);
15738f4e2101SHugh Dickins 	return same;
15748f4e2101SHugh Dickins }
15758f4e2101SHugh Dickins 
15768f4e2101SHugh Dickins /*
15771da177e4SLinus Torvalds  * Do pte_mkwrite, but only if the vma says VM_WRITE.  We do this when
15781da177e4SLinus Torvalds  * servicing faults for write access.  In the normal case, do always want
15791da177e4SLinus Torvalds  * pte_mkwrite.  But get_user_pages can cause write faults for mappings
15801da177e4SLinus Torvalds  * that do not have writing enabled, when used by access_process_vm.
15811da177e4SLinus Torvalds  */
15821da177e4SLinus Torvalds static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
15831da177e4SLinus Torvalds {
15841da177e4SLinus Torvalds 	if (likely(vma->vm_flags & VM_WRITE))
15851da177e4SLinus Torvalds 		pte = pte_mkwrite(pte);
15861da177e4SLinus Torvalds 	return pte;
15871da177e4SLinus Torvalds }
15881da177e4SLinus Torvalds 
15899de455b2SAtsushi Nemoto static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma)
15906aab341eSLinus Torvalds {
15916aab341eSLinus Torvalds 	/*
15926aab341eSLinus Torvalds 	 * If the source page was a PFN mapping, we don't have
15936aab341eSLinus Torvalds 	 * a "struct page" for it. We do a best-effort copy by
15946aab341eSLinus Torvalds 	 * just copying from the original user address. If that
15956aab341eSLinus Torvalds 	 * fails, we just zero-fill it. Live with it.
15966aab341eSLinus Torvalds 	 */
15976aab341eSLinus Torvalds 	if (unlikely(!src)) {
15986aab341eSLinus Torvalds 		void *kaddr = kmap_atomic(dst, KM_USER0);
15995d2a2dbbSLinus Torvalds 		void __user *uaddr = (void __user *)(va & PAGE_MASK);
16005d2a2dbbSLinus Torvalds 
16015d2a2dbbSLinus Torvalds 		/*
16025d2a2dbbSLinus Torvalds 		 * This really shouldn't fail, because the page is there
16035d2a2dbbSLinus Torvalds 		 * in the page tables. But it might just be unreadable,
16045d2a2dbbSLinus Torvalds 		 * in which case we just give up and fill the result with
16055d2a2dbbSLinus Torvalds 		 * zeroes.
16065d2a2dbbSLinus Torvalds 		 */
16075d2a2dbbSLinus Torvalds 		if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE))
16086aab341eSLinus Torvalds 			memset(kaddr, 0, PAGE_SIZE);
16096aab341eSLinus Torvalds 		kunmap_atomic(kaddr, KM_USER0);
1610c4ec7b0dSDmitriy Monakhov 		flush_dcache_page(dst);
16116aab341eSLinus Torvalds 		return;
16126aab341eSLinus Torvalds 
16136aab341eSLinus Torvalds 	}
16149de455b2SAtsushi Nemoto 	copy_user_highpage(dst, src, va, vma);
16156aab341eSLinus Torvalds }
16166aab341eSLinus Torvalds 
16171da177e4SLinus Torvalds /*
16181da177e4SLinus Torvalds  * This routine handles present pages, when users try to write
16191da177e4SLinus Torvalds  * to a shared page. It is done by copying the page to a new address
16201da177e4SLinus Torvalds  * and decrementing the shared-page counter for the old page.
16211da177e4SLinus Torvalds  *
16221da177e4SLinus Torvalds  * Note that this routine assumes that the protection checks have been
16231da177e4SLinus Torvalds  * done by the caller (the low-level page fault routine in most cases).
16241da177e4SLinus Torvalds  * Thus we can safely just mark it writable once we've done any necessary
16251da177e4SLinus Torvalds  * COW.
16261da177e4SLinus Torvalds  *
16271da177e4SLinus Torvalds  * We also mark the page dirty at this point even though the page will
16281da177e4SLinus Torvalds  * change only once the write actually happens. This avoids a few races,
16291da177e4SLinus Torvalds  * and potentially makes it more efficient.
16301da177e4SLinus Torvalds  *
16318f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
16328f4e2101SHugh Dickins  * but allow concurrent faults), with pte both mapped and locked.
16338f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
16341da177e4SLinus Torvalds  */
16351da177e4SLinus Torvalds static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
163665500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
16378f4e2101SHugh Dickins 		spinlock_t *ptl, pte_t orig_pte)
16381da177e4SLinus Torvalds {
1639e5bbe4dfSHugh Dickins 	struct page *old_page, *new_page;
16401da177e4SLinus Torvalds 	pte_t entry;
1641d08b3851SPeter Zijlstra 	int reuse = 0, ret = VM_FAULT_MINOR;
1642d08b3851SPeter Zijlstra 	struct page *dirty_page = NULL;
16431da177e4SLinus Torvalds 
16446aab341eSLinus Torvalds 	old_page = vm_normal_page(vma, address, orig_pte);
16456aab341eSLinus Torvalds 	if (!old_page)
1646920fc356SHugh Dickins 		goto gotten;
16471da177e4SLinus Torvalds 
1648d08b3851SPeter Zijlstra 	/*
1649ee6a6457SPeter Zijlstra 	 * Take out anonymous pages first, anonymous shared vmas are
1650ee6a6457SPeter Zijlstra 	 * not dirty accountable.
1651d08b3851SPeter Zijlstra 	 */
1652ee6a6457SPeter Zijlstra 	if (PageAnon(old_page)) {
1653ee6a6457SPeter Zijlstra 		if (!TestSetPageLocked(old_page)) {
1654ee6a6457SPeter Zijlstra 			reuse = can_share_swap_page(old_page);
1655ee6a6457SPeter Zijlstra 			unlock_page(old_page);
1656ee6a6457SPeter Zijlstra 		}
1657ee6a6457SPeter Zijlstra 	} else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
1658d08b3851SPeter Zijlstra 					(VM_WRITE|VM_SHARED))) {
1659ee6a6457SPeter Zijlstra 		/*
1660ee6a6457SPeter Zijlstra 		 * Only catch write-faults on shared writable pages,
1661ee6a6457SPeter Zijlstra 		 * read-only shared pages can get COWed by
1662ee6a6457SPeter Zijlstra 		 * get_user_pages(.write=1, .force=1).
1663ee6a6457SPeter Zijlstra 		 */
16649637a5efSDavid Howells 		if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
16659637a5efSDavid Howells 			/*
16669637a5efSDavid Howells 			 * Notify the address space that the page is about to
16679637a5efSDavid Howells 			 * become writable so that it can prohibit this or wait
16689637a5efSDavid Howells 			 * for the page to get into an appropriate state.
16699637a5efSDavid Howells 			 *
16709637a5efSDavid Howells 			 * We do this without the lock held, so that it can
16719637a5efSDavid Howells 			 * sleep if it needs to.
16729637a5efSDavid Howells 			 */
16739637a5efSDavid Howells 			page_cache_get(old_page);
16749637a5efSDavid Howells 			pte_unmap_unlock(page_table, ptl);
16759637a5efSDavid Howells 
16769637a5efSDavid Howells 			if (vma->vm_ops->page_mkwrite(vma, old_page) < 0)
16779637a5efSDavid Howells 				goto unwritable_page;
16789637a5efSDavid Howells 
16799637a5efSDavid Howells 			/*
16809637a5efSDavid Howells 			 * Since we dropped the lock we need to revalidate
16819637a5efSDavid Howells 			 * the PTE as someone else may have changed it.  If
16829637a5efSDavid Howells 			 * they did, we just return, as we can count on the
16839637a5efSDavid Howells 			 * MMU to tell us if they didn't also make it writable.
16849637a5efSDavid Howells 			 */
16859637a5efSDavid Howells 			page_table = pte_offset_map_lock(mm, pmd, address,
16869637a5efSDavid Howells 							 &ptl);
1687c3704cebSHugh Dickins 			page_cache_release(old_page);
16889637a5efSDavid Howells 			if (!pte_same(*page_table, orig_pte))
16899637a5efSDavid Howells 				goto unlock;
16909637a5efSDavid Howells 		}
1691d08b3851SPeter Zijlstra 		dirty_page = old_page;
1692d08b3851SPeter Zijlstra 		get_page(dirty_page);
16939637a5efSDavid Howells 		reuse = 1;
16949637a5efSDavid Howells 	}
16959637a5efSDavid Howells 
16961da177e4SLinus Torvalds 	if (reuse) {
1697eca35133SBen Collins 		flush_cache_page(vma, address, pte_pfn(orig_pte));
169865500d23SHugh Dickins 		entry = pte_mkyoung(orig_pte);
169965500d23SHugh Dickins 		entry = maybe_mkwrite(pte_mkdirty(entry), vma);
17008dab5241SBenjamin Herrenschmidt 		if (ptep_set_access_flags(vma, address, page_table, entry,1)) {
17011da177e4SLinus Torvalds 			update_mmu_cache(vma, address, entry);
17021da177e4SLinus Torvalds 			lazy_mmu_prot_update(entry);
17038dab5241SBenjamin Herrenschmidt 		}
170465500d23SHugh Dickins 		ret |= VM_FAULT_WRITE;
170565500d23SHugh Dickins 		goto unlock;
17061da177e4SLinus Torvalds 	}
17071da177e4SLinus Torvalds 
17081da177e4SLinus Torvalds 	/*
17091da177e4SLinus Torvalds 	 * Ok, we need to copy. Oh, well..
17101da177e4SLinus Torvalds 	 */
17111da177e4SLinus Torvalds 	page_cache_get(old_page);
1712920fc356SHugh Dickins gotten:
17138f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
17141da177e4SLinus Torvalds 
17151da177e4SLinus Torvalds 	if (unlikely(anon_vma_prepare(vma)))
171665500d23SHugh Dickins 		goto oom;
1717e5bbe4dfSHugh Dickins 	if (old_page == ZERO_PAGE(address)) {
1718769848c0SMel Gorman 		new_page = alloc_zeroed_user_highpage_movable(vma, address);
17191da177e4SLinus Torvalds 		if (!new_page)
172065500d23SHugh Dickins 			goto oom;
17211da177e4SLinus Torvalds 	} else {
1722769848c0SMel Gorman 		new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
17231da177e4SLinus Torvalds 		if (!new_page)
172465500d23SHugh Dickins 			goto oom;
17259de455b2SAtsushi Nemoto 		cow_user_page(new_page, old_page, address, vma);
17261da177e4SLinus Torvalds 	}
172765500d23SHugh Dickins 
17281da177e4SLinus Torvalds 	/*
17291da177e4SLinus Torvalds 	 * Re-check the pte - we dropped the lock
17301da177e4SLinus Torvalds 	 */
17318f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
173265500d23SHugh Dickins 	if (likely(pte_same(*page_table, orig_pte))) {
1733920fc356SHugh Dickins 		if (old_page) {
17347de6b805SNick Piggin 			page_remove_rmap(old_page, vma);
17354294621fSHugh Dickins 			if (!PageAnon(old_page)) {
17364294621fSHugh Dickins 				dec_mm_counter(mm, file_rss);
1737920fc356SHugh Dickins 				inc_mm_counter(mm, anon_rss);
17384294621fSHugh Dickins 			}
1739920fc356SHugh Dickins 		} else
1740920fc356SHugh Dickins 			inc_mm_counter(mm, anon_rss);
1741eca35133SBen Collins 		flush_cache_page(vma, address, pte_pfn(orig_pte));
174265500d23SHugh Dickins 		entry = mk_pte(new_page, vma->vm_page_prot);
174365500d23SHugh Dickins 		entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1744c38c8db7SAnil Keshavamurthy 		lazy_mmu_prot_update(entry);
17454ce072f1SSiddha, Suresh B 		/*
17464ce072f1SSiddha, Suresh B 		 * Clear the pte entry and flush it first, before updating the
17474ce072f1SSiddha, Suresh B 		 * pte with the new entry. This will avoid a race condition
17484ce072f1SSiddha, Suresh B 		 * seen in the presence of one thread doing SMC and another
17494ce072f1SSiddha, Suresh B 		 * thread doing COW.
17504ce072f1SSiddha, Suresh B 		 */
17514ce072f1SSiddha, Suresh B 		ptep_clear_flush(vma, address, page_table);
17524ce072f1SSiddha, Suresh B 		set_pte_at(mm, address, page_table, entry);
175365500d23SHugh Dickins 		update_mmu_cache(vma, address, entry);
17541da177e4SLinus Torvalds 		lru_cache_add_active(new_page);
17559617d95eSNick Piggin 		page_add_new_anon_rmap(new_page, vma, address);
17561da177e4SLinus Torvalds 
17571da177e4SLinus Torvalds 		/* Free the old page.. */
17581da177e4SLinus Torvalds 		new_page = old_page;
1759f33ea7f4SNick Piggin 		ret |= VM_FAULT_WRITE;
17601da177e4SLinus Torvalds 	}
1761920fc356SHugh Dickins 	if (new_page)
17621da177e4SLinus Torvalds 		page_cache_release(new_page);
1763920fc356SHugh Dickins 	if (old_page)
17641da177e4SLinus Torvalds 		page_cache_release(old_page);
176565500d23SHugh Dickins unlock:
17668f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
1767d08b3851SPeter Zijlstra 	if (dirty_page) {
1768edc79b2aSPeter Zijlstra 		set_page_dirty_balance(dirty_page);
1769d08b3851SPeter Zijlstra 		put_page(dirty_page);
1770d08b3851SPeter Zijlstra 	}
1771f33ea7f4SNick Piggin 	return ret;
177265500d23SHugh Dickins oom:
1773920fc356SHugh Dickins 	if (old_page)
17741da177e4SLinus Torvalds 		page_cache_release(old_page);
17751da177e4SLinus Torvalds 	return VM_FAULT_OOM;
17769637a5efSDavid Howells 
17779637a5efSDavid Howells unwritable_page:
17789637a5efSDavid Howells 	page_cache_release(old_page);
17799637a5efSDavid Howells 	return VM_FAULT_SIGBUS;
17801da177e4SLinus Torvalds }
17811da177e4SLinus Torvalds 
17821da177e4SLinus Torvalds /*
17831da177e4SLinus Torvalds  * Helper functions for unmap_mapping_range().
17841da177e4SLinus Torvalds  *
17851da177e4SLinus Torvalds  * __ Notes on dropping i_mmap_lock to reduce latency while unmapping __
17861da177e4SLinus Torvalds  *
17871da177e4SLinus Torvalds  * We have to restart searching the prio_tree whenever we drop the lock,
17881da177e4SLinus Torvalds  * since the iterator is only valid while the lock is held, and anyway
17891da177e4SLinus Torvalds  * a later vma might be split and reinserted earlier while lock dropped.
17901da177e4SLinus Torvalds  *
17911da177e4SLinus Torvalds  * The list of nonlinear vmas could be handled more efficiently, using
17921da177e4SLinus Torvalds  * a placeholder, but handle it in the same way until a need is shown.
17931da177e4SLinus Torvalds  * It is important to search the prio_tree before nonlinear list: a vma
17941da177e4SLinus Torvalds  * may become nonlinear and be shifted from prio_tree to nonlinear list
17951da177e4SLinus Torvalds  * while the lock is dropped; but never shifted from list to prio_tree.
17961da177e4SLinus Torvalds  *
17971da177e4SLinus Torvalds  * In order to make forward progress despite restarting the search,
17981da177e4SLinus Torvalds  * vm_truncate_count is used to mark a vma as now dealt with, so we can
17991da177e4SLinus Torvalds  * quickly skip it next time around.  Since the prio_tree search only
18001da177e4SLinus Torvalds  * shows us those vmas affected by unmapping the range in question, we
18011da177e4SLinus Torvalds  * can't efficiently keep all vmas in step with mapping->truncate_count:
18021da177e4SLinus Torvalds  * so instead reset them all whenever it wraps back to 0 (then go to 1).
18031da177e4SLinus Torvalds  * mapping->truncate_count and vma->vm_truncate_count are protected by
18041da177e4SLinus Torvalds  * i_mmap_lock.
18051da177e4SLinus Torvalds  *
18061da177e4SLinus Torvalds  * In order to make forward progress despite repeatedly restarting some
1807ee39b37bSHugh Dickins  * large vma, note the restart_addr from unmap_vmas when it breaks out:
18081da177e4SLinus Torvalds  * and restart from that address when we reach that vma again.  It might
18091da177e4SLinus Torvalds  * have been split or merged, shrunk or extended, but never shifted: so
18101da177e4SLinus Torvalds  * restart_addr remains valid so long as it remains in the vma's range.
18111da177e4SLinus Torvalds  * unmap_mapping_range forces truncate_count to leap over page-aligned
18121da177e4SLinus Torvalds  * values so we can save vma's restart_addr in its truncate_count field.
18131da177e4SLinus Torvalds  */
18141da177e4SLinus Torvalds #define is_restart_addr(truncate_count) (!((truncate_count) & ~PAGE_MASK))
18151da177e4SLinus Torvalds 
18161da177e4SLinus Torvalds static void reset_vma_truncate_counts(struct address_space *mapping)
18171da177e4SLinus Torvalds {
18181da177e4SLinus Torvalds 	struct vm_area_struct *vma;
18191da177e4SLinus Torvalds 	struct prio_tree_iter iter;
18201da177e4SLinus Torvalds 
18211da177e4SLinus Torvalds 	vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, ULONG_MAX)
18221da177e4SLinus Torvalds 		vma->vm_truncate_count = 0;
18231da177e4SLinus Torvalds 	list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
18241da177e4SLinus Torvalds 		vma->vm_truncate_count = 0;
18251da177e4SLinus Torvalds }
18261da177e4SLinus Torvalds 
18271da177e4SLinus Torvalds static int unmap_mapping_range_vma(struct vm_area_struct *vma,
18281da177e4SLinus Torvalds 		unsigned long start_addr, unsigned long end_addr,
18291da177e4SLinus Torvalds 		struct zap_details *details)
18301da177e4SLinus Torvalds {
18311da177e4SLinus Torvalds 	unsigned long restart_addr;
18321da177e4SLinus Torvalds 	int need_break;
18331da177e4SLinus Torvalds 
1834*d00806b1SNick Piggin 	/*
1835*d00806b1SNick Piggin 	 * files that support invalidating or truncating portions of the
1836*d00806b1SNick Piggin 	 * file from under mmaped areas must set the VM_CAN_INVALIDATE flag, and
1837*d00806b1SNick Piggin 	 * have their .nopage function return the page locked.
1838*d00806b1SNick Piggin 	 */
1839*d00806b1SNick Piggin 	BUG_ON(!(vma->vm_flags & VM_CAN_INVALIDATE));
1840*d00806b1SNick Piggin 
18411da177e4SLinus Torvalds again:
18421da177e4SLinus Torvalds 	restart_addr = vma->vm_truncate_count;
18431da177e4SLinus Torvalds 	if (is_restart_addr(restart_addr) && start_addr < restart_addr) {
18441da177e4SLinus Torvalds 		start_addr = restart_addr;
18451da177e4SLinus Torvalds 		if (start_addr >= end_addr) {
18461da177e4SLinus Torvalds 			/* Top of vma has been split off since last time */
18471da177e4SLinus Torvalds 			vma->vm_truncate_count = details->truncate_count;
18481da177e4SLinus Torvalds 			return 0;
18491da177e4SLinus Torvalds 		}
18501da177e4SLinus Torvalds 	}
18511da177e4SLinus Torvalds 
1852ee39b37bSHugh Dickins 	restart_addr = zap_page_range(vma, start_addr,
1853ee39b37bSHugh Dickins 					end_addr - start_addr, details);
18541da177e4SLinus Torvalds 	need_break = need_resched() ||
18551da177e4SLinus Torvalds 			need_lockbreak(details->i_mmap_lock);
18561da177e4SLinus Torvalds 
1857ee39b37bSHugh Dickins 	if (restart_addr >= end_addr) {
18581da177e4SLinus Torvalds 		/* We have now completed this vma: mark it so */
18591da177e4SLinus Torvalds 		vma->vm_truncate_count = details->truncate_count;
18601da177e4SLinus Torvalds 		if (!need_break)
18611da177e4SLinus Torvalds 			return 0;
18621da177e4SLinus Torvalds 	} else {
18631da177e4SLinus Torvalds 		/* Note restart_addr in vma's truncate_count field */
1864ee39b37bSHugh Dickins 		vma->vm_truncate_count = restart_addr;
18651da177e4SLinus Torvalds 		if (!need_break)
18661da177e4SLinus Torvalds 			goto again;
18671da177e4SLinus Torvalds 	}
18681da177e4SLinus Torvalds 
18691da177e4SLinus Torvalds 	spin_unlock(details->i_mmap_lock);
18701da177e4SLinus Torvalds 	cond_resched();
18711da177e4SLinus Torvalds 	spin_lock(details->i_mmap_lock);
18721da177e4SLinus Torvalds 	return -EINTR;
18731da177e4SLinus Torvalds }
18741da177e4SLinus Torvalds 
18751da177e4SLinus Torvalds static inline void unmap_mapping_range_tree(struct prio_tree_root *root,
18761da177e4SLinus Torvalds 					    struct zap_details *details)
18771da177e4SLinus Torvalds {
18781da177e4SLinus Torvalds 	struct vm_area_struct *vma;
18791da177e4SLinus Torvalds 	struct prio_tree_iter iter;
18801da177e4SLinus Torvalds 	pgoff_t vba, vea, zba, zea;
18811da177e4SLinus Torvalds 
18821da177e4SLinus Torvalds restart:
18831da177e4SLinus Torvalds 	vma_prio_tree_foreach(vma, &iter, root,
18841da177e4SLinus Torvalds 			details->first_index, details->last_index) {
18851da177e4SLinus Torvalds 		/* Skip quickly over those we have already dealt with */
18861da177e4SLinus Torvalds 		if (vma->vm_truncate_count == details->truncate_count)
18871da177e4SLinus Torvalds 			continue;
18881da177e4SLinus Torvalds 
18891da177e4SLinus Torvalds 		vba = vma->vm_pgoff;
18901da177e4SLinus Torvalds 		vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1;
18911da177e4SLinus Torvalds 		/* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
18921da177e4SLinus Torvalds 		zba = details->first_index;
18931da177e4SLinus Torvalds 		if (zba < vba)
18941da177e4SLinus Torvalds 			zba = vba;
18951da177e4SLinus Torvalds 		zea = details->last_index;
18961da177e4SLinus Torvalds 		if (zea > vea)
18971da177e4SLinus Torvalds 			zea = vea;
18981da177e4SLinus Torvalds 
18991da177e4SLinus Torvalds 		if (unmap_mapping_range_vma(vma,
19001da177e4SLinus Torvalds 			((zba - vba) << PAGE_SHIFT) + vma->vm_start,
19011da177e4SLinus Torvalds 			((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
19021da177e4SLinus Torvalds 				details) < 0)
19031da177e4SLinus Torvalds 			goto restart;
19041da177e4SLinus Torvalds 	}
19051da177e4SLinus Torvalds }
19061da177e4SLinus Torvalds 
19071da177e4SLinus Torvalds static inline void unmap_mapping_range_list(struct list_head *head,
19081da177e4SLinus Torvalds 					    struct zap_details *details)
19091da177e4SLinus Torvalds {
19101da177e4SLinus Torvalds 	struct vm_area_struct *vma;
19111da177e4SLinus Torvalds 
19121da177e4SLinus Torvalds 	/*
19131da177e4SLinus Torvalds 	 * In nonlinear VMAs there is no correspondence between virtual address
19141da177e4SLinus Torvalds 	 * offset and file offset.  So we must perform an exhaustive search
19151da177e4SLinus Torvalds 	 * across *all* the pages in each nonlinear VMA, not just the pages
19161da177e4SLinus Torvalds 	 * whose virtual address lies outside the file truncation point.
19171da177e4SLinus Torvalds 	 */
19181da177e4SLinus Torvalds restart:
19191da177e4SLinus Torvalds 	list_for_each_entry(vma, head, shared.vm_set.list) {
19201da177e4SLinus Torvalds 		/* Skip quickly over those we have already dealt with */
19211da177e4SLinus Torvalds 		if (vma->vm_truncate_count == details->truncate_count)
19221da177e4SLinus Torvalds 			continue;
19231da177e4SLinus Torvalds 		details->nonlinear_vma = vma;
19241da177e4SLinus Torvalds 		if (unmap_mapping_range_vma(vma, vma->vm_start,
19251da177e4SLinus Torvalds 					vma->vm_end, details) < 0)
19261da177e4SLinus Torvalds 			goto restart;
19271da177e4SLinus Torvalds 	}
19281da177e4SLinus Torvalds }
19291da177e4SLinus Torvalds 
19301da177e4SLinus Torvalds /**
193172fd4a35SRobert P. J. Day  * unmap_mapping_range - unmap the portion of all mmaps in the specified address_space corresponding to the specified page range in the underlying file.
19323d41088fSMartin Waitz  * @mapping: the address space containing mmaps to be unmapped.
19331da177e4SLinus Torvalds  * @holebegin: byte in first page to unmap, relative to the start of
19341da177e4SLinus Torvalds  * the underlying file.  This will be rounded down to a PAGE_SIZE
19351da177e4SLinus Torvalds  * boundary.  Note that this is different from vmtruncate(), which
19361da177e4SLinus Torvalds  * must keep the partial page.  In contrast, we must get rid of
19371da177e4SLinus Torvalds  * partial pages.
19381da177e4SLinus Torvalds  * @holelen: size of prospective hole in bytes.  This will be rounded
19391da177e4SLinus Torvalds  * up to a PAGE_SIZE boundary.  A holelen of zero truncates to the
19401da177e4SLinus Torvalds  * end of the file.
19411da177e4SLinus Torvalds  * @even_cows: 1 when truncating a file, unmap even private COWed pages;
19421da177e4SLinus Torvalds  * but 0 when invalidating pagecache, don't throw away private data.
19431da177e4SLinus Torvalds  */
19441da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping,
19451da177e4SLinus Torvalds 		loff_t const holebegin, loff_t const holelen, int even_cows)
19461da177e4SLinus Torvalds {
19471da177e4SLinus Torvalds 	struct zap_details details;
19481da177e4SLinus Torvalds 	pgoff_t hba = holebegin >> PAGE_SHIFT;
19491da177e4SLinus Torvalds 	pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
19501da177e4SLinus Torvalds 
19511da177e4SLinus Torvalds 	/* Check for overflow. */
19521da177e4SLinus Torvalds 	if (sizeof(holelen) > sizeof(hlen)) {
19531da177e4SLinus Torvalds 		long long holeend =
19541da177e4SLinus Torvalds 			(holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
19551da177e4SLinus Torvalds 		if (holeend & ~(long long)ULONG_MAX)
19561da177e4SLinus Torvalds 			hlen = ULONG_MAX - hba + 1;
19571da177e4SLinus Torvalds 	}
19581da177e4SLinus Torvalds 
19591da177e4SLinus Torvalds 	details.check_mapping = even_cows? NULL: mapping;
19601da177e4SLinus Torvalds 	details.nonlinear_vma = NULL;
19611da177e4SLinus Torvalds 	details.first_index = hba;
19621da177e4SLinus Torvalds 	details.last_index = hba + hlen - 1;
19631da177e4SLinus Torvalds 	if (details.last_index < details.first_index)
19641da177e4SLinus Torvalds 		details.last_index = ULONG_MAX;
19651da177e4SLinus Torvalds 	details.i_mmap_lock = &mapping->i_mmap_lock;
19661da177e4SLinus Torvalds 
19671da177e4SLinus Torvalds 	spin_lock(&mapping->i_mmap_lock);
19681da177e4SLinus Torvalds 
1969*d00806b1SNick Piggin 	/* Protect against endless unmapping loops */
19701da177e4SLinus Torvalds 	mapping->truncate_count++;
19711da177e4SLinus Torvalds 	if (unlikely(is_restart_addr(mapping->truncate_count))) {
19721da177e4SLinus Torvalds 		if (mapping->truncate_count == 0)
19731da177e4SLinus Torvalds 			reset_vma_truncate_counts(mapping);
19741da177e4SLinus Torvalds 		mapping->truncate_count++;
19751da177e4SLinus Torvalds 	}
19761da177e4SLinus Torvalds 	details.truncate_count = mapping->truncate_count;
19771da177e4SLinus Torvalds 
19781da177e4SLinus Torvalds 	if (unlikely(!prio_tree_empty(&mapping->i_mmap)))
19791da177e4SLinus Torvalds 		unmap_mapping_range_tree(&mapping->i_mmap, &details);
19801da177e4SLinus Torvalds 	if (unlikely(!list_empty(&mapping->i_mmap_nonlinear)))
19811da177e4SLinus Torvalds 		unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details);
19821da177e4SLinus Torvalds 	spin_unlock(&mapping->i_mmap_lock);
19831da177e4SLinus Torvalds }
19841da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range);
19851da177e4SLinus Torvalds 
1986bfa5bf6dSRolf Eike Beer /**
1987bfa5bf6dSRolf Eike Beer  * vmtruncate - unmap mappings "freed" by truncate() syscall
1988bfa5bf6dSRolf Eike Beer  * @inode: inode of the file used
1989bfa5bf6dSRolf Eike Beer  * @offset: file offset to start truncating
19901da177e4SLinus Torvalds  *
19911da177e4SLinus Torvalds  * NOTE! We have to be ready to update the memory sharing
19921da177e4SLinus Torvalds  * between the file and the memory map for a potential last
19931da177e4SLinus Torvalds  * incomplete page.  Ugly, but necessary.
19941da177e4SLinus Torvalds  */
19951da177e4SLinus Torvalds int vmtruncate(struct inode * inode, loff_t offset)
19961da177e4SLinus Torvalds {
19971da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
19981da177e4SLinus Torvalds 	unsigned long limit;
19991da177e4SLinus Torvalds 
20001da177e4SLinus Torvalds 	if (inode->i_size < offset)
20011da177e4SLinus Torvalds 		goto do_expand;
20021da177e4SLinus Torvalds 	/*
20031da177e4SLinus Torvalds 	 * truncation of in-use swapfiles is disallowed - it would cause
20041da177e4SLinus Torvalds 	 * subsequent swapout to scribble on the now-freed blocks.
20051da177e4SLinus Torvalds 	 */
20061da177e4SLinus Torvalds 	if (IS_SWAPFILE(inode))
20071da177e4SLinus Torvalds 		goto out_busy;
20081da177e4SLinus Torvalds 	i_size_write(inode, offset);
2009*d00806b1SNick Piggin 
2010*d00806b1SNick Piggin 	/*
2011*d00806b1SNick Piggin 	 * unmap_mapping_range is called twice, first simply for efficiency
2012*d00806b1SNick Piggin 	 * so that truncate_inode_pages does fewer single-page unmaps. However
2013*d00806b1SNick Piggin 	 * after this first call, and before truncate_inode_pages finishes,
2014*d00806b1SNick Piggin 	 * it is possible for private pages to be COWed, which remain after
2015*d00806b1SNick Piggin 	 * truncate_inode_pages finishes, hence the second unmap_mapping_range
2016*d00806b1SNick Piggin 	 * call must be made for correctness.
2017*d00806b1SNick Piggin 	 */
20181da177e4SLinus Torvalds 	unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
20191da177e4SLinus Torvalds 	truncate_inode_pages(mapping, offset);
2020*d00806b1SNick Piggin 	unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
20211da177e4SLinus Torvalds 	goto out_truncate;
20221da177e4SLinus Torvalds 
20231da177e4SLinus Torvalds do_expand:
20241da177e4SLinus Torvalds 	limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
20251da177e4SLinus Torvalds 	if (limit != RLIM_INFINITY && offset > limit)
20261da177e4SLinus Torvalds 		goto out_sig;
20271da177e4SLinus Torvalds 	if (offset > inode->i_sb->s_maxbytes)
20281da177e4SLinus Torvalds 		goto out_big;
20291da177e4SLinus Torvalds 	i_size_write(inode, offset);
20301da177e4SLinus Torvalds 
20311da177e4SLinus Torvalds out_truncate:
20321da177e4SLinus Torvalds 	if (inode->i_op && inode->i_op->truncate)
20331da177e4SLinus Torvalds 		inode->i_op->truncate(inode);
20341da177e4SLinus Torvalds 	return 0;
20351da177e4SLinus Torvalds out_sig:
20361da177e4SLinus Torvalds 	send_sig(SIGXFSZ, current, 0);
20371da177e4SLinus Torvalds out_big:
20381da177e4SLinus Torvalds 	return -EFBIG;
20391da177e4SLinus Torvalds out_busy:
20401da177e4SLinus Torvalds 	return -ETXTBSY;
20411da177e4SLinus Torvalds }
20421da177e4SLinus Torvalds EXPORT_SYMBOL(vmtruncate);
20431da177e4SLinus Torvalds 
2044f6b3ec23SBadari Pulavarty int vmtruncate_range(struct inode *inode, loff_t offset, loff_t end)
2045f6b3ec23SBadari Pulavarty {
2046f6b3ec23SBadari Pulavarty 	struct address_space *mapping = inode->i_mapping;
2047f6b3ec23SBadari Pulavarty 
2048f6b3ec23SBadari Pulavarty 	/*
2049f6b3ec23SBadari Pulavarty 	 * If the underlying filesystem is not going to provide
2050f6b3ec23SBadari Pulavarty 	 * a way to truncate a range of blocks (punch a hole) -
2051f6b3ec23SBadari Pulavarty 	 * we should return failure right now.
2052f6b3ec23SBadari Pulavarty 	 */
2053f6b3ec23SBadari Pulavarty 	if (!inode->i_op || !inode->i_op->truncate_range)
2054f6b3ec23SBadari Pulavarty 		return -ENOSYS;
2055f6b3ec23SBadari Pulavarty 
20561b1dcc1bSJes Sorensen 	mutex_lock(&inode->i_mutex);
2057f6b3ec23SBadari Pulavarty 	down_write(&inode->i_alloc_sem);
2058f6b3ec23SBadari Pulavarty 	unmap_mapping_range(mapping, offset, (end - offset), 1);
2059f6b3ec23SBadari Pulavarty 	truncate_inode_pages_range(mapping, offset, end);
2060*d00806b1SNick Piggin 	unmap_mapping_range(mapping, offset, (end - offset), 1);
2061f6b3ec23SBadari Pulavarty 	inode->i_op->truncate_range(inode, offset, end);
2062f6b3ec23SBadari Pulavarty 	up_write(&inode->i_alloc_sem);
20631b1dcc1bSJes Sorensen 	mutex_unlock(&inode->i_mutex);
2064f6b3ec23SBadari Pulavarty 
2065f6b3ec23SBadari Pulavarty 	return 0;
2066f6b3ec23SBadari Pulavarty }
2067f6b3ec23SBadari Pulavarty 
2068bfa5bf6dSRolf Eike Beer /**
2069bfa5bf6dSRolf Eike Beer  * swapin_readahead - swap in pages in hope we need them soon
2070bfa5bf6dSRolf Eike Beer  * @entry: swap entry of this memory
2071bfa5bf6dSRolf Eike Beer  * @addr: address to start
2072bfa5bf6dSRolf Eike Beer  * @vma: user vma this addresses belong to
2073bfa5bf6dSRolf Eike Beer  *
20741da177e4SLinus Torvalds  * Primitive swap readahead code. We simply read an aligned block of
20751da177e4SLinus Torvalds  * (1 << page_cluster) entries in the swap area. This method is chosen
20761da177e4SLinus Torvalds  * because it doesn't cost us any seek time.  We also make sure to queue
20771da177e4SLinus Torvalds  * the 'original' request together with the readahead ones...
20781da177e4SLinus Torvalds  *
20791da177e4SLinus Torvalds  * This has been extended to use the NUMA policies from the mm triggering
20801da177e4SLinus Torvalds  * the readahead.
20811da177e4SLinus Torvalds  *
20821da177e4SLinus Torvalds  * Caller must hold down_read on the vma->vm_mm if vma is not NULL.
20831da177e4SLinus Torvalds  */
20841da177e4SLinus Torvalds void swapin_readahead(swp_entry_t entry, unsigned long addr,struct vm_area_struct *vma)
20851da177e4SLinus Torvalds {
20861da177e4SLinus Torvalds #ifdef CONFIG_NUMA
20871da177e4SLinus Torvalds 	struct vm_area_struct *next_vma = vma ? vma->vm_next : NULL;
20881da177e4SLinus Torvalds #endif
20891da177e4SLinus Torvalds 	int i, num;
20901da177e4SLinus Torvalds 	struct page *new_page;
20911da177e4SLinus Torvalds 	unsigned long offset;
20921da177e4SLinus Torvalds 
20931da177e4SLinus Torvalds 	/*
20941da177e4SLinus Torvalds 	 * Get the number of handles we should do readahead io to.
20951da177e4SLinus Torvalds 	 */
20961da177e4SLinus Torvalds 	num = valid_swaphandles(entry, &offset);
20971da177e4SLinus Torvalds 	for (i = 0; i < num; offset++, i++) {
20981da177e4SLinus Torvalds 		/* Ok, do the async read-ahead now */
20991da177e4SLinus Torvalds 		new_page = read_swap_cache_async(swp_entry(swp_type(entry),
21001da177e4SLinus Torvalds 							   offset), vma, addr);
21011da177e4SLinus Torvalds 		if (!new_page)
21021da177e4SLinus Torvalds 			break;
21031da177e4SLinus Torvalds 		page_cache_release(new_page);
21041da177e4SLinus Torvalds #ifdef CONFIG_NUMA
21051da177e4SLinus Torvalds 		/*
21061da177e4SLinus Torvalds 		 * Find the next applicable VMA for the NUMA policy.
21071da177e4SLinus Torvalds 		 */
21081da177e4SLinus Torvalds 		addr += PAGE_SIZE;
21091da177e4SLinus Torvalds 		if (addr == 0)
21101da177e4SLinus Torvalds 			vma = NULL;
21111da177e4SLinus Torvalds 		if (vma) {
21121da177e4SLinus Torvalds 			if (addr >= vma->vm_end) {
21131da177e4SLinus Torvalds 				vma = next_vma;
21141da177e4SLinus Torvalds 				next_vma = vma ? vma->vm_next : NULL;
21151da177e4SLinus Torvalds 			}
21161da177e4SLinus Torvalds 			if (vma && addr < vma->vm_start)
21171da177e4SLinus Torvalds 				vma = NULL;
21181da177e4SLinus Torvalds 		} else {
21191da177e4SLinus Torvalds 			if (next_vma && addr >= next_vma->vm_start) {
21201da177e4SLinus Torvalds 				vma = next_vma;
21211da177e4SLinus Torvalds 				next_vma = vma->vm_next;
21221da177e4SLinus Torvalds 			}
21231da177e4SLinus Torvalds 		}
21241da177e4SLinus Torvalds #endif
21251da177e4SLinus Torvalds 	}
21261da177e4SLinus Torvalds 	lru_add_drain();	/* Push any new pages onto the LRU now */
21271da177e4SLinus Torvalds }
21281da177e4SLinus Torvalds 
21291da177e4SLinus Torvalds /*
21308f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
21318f4e2101SHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
21328f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
21331da177e4SLinus Torvalds  */
213465500d23SHugh Dickins static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
213565500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
213665500d23SHugh Dickins 		int write_access, pte_t orig_pte)
21371da177e4SLinus Torvalds {
21388f4e2101SHugh Dickins 	spinlock_t *ptl;
21391da177e4SLinus Torvalds 	struct page *page;
214065500d23SHugh Dickins 	swp_entry_t entry;
21411da177e4SLinus Torvalds 	pte_t pte;
21421da177e4SLinus Torvalds 	int ret = VM_FAULT_MINOR;
21431da177e4SLinus Torvalds 
21444c21e2f2SHugh Dickins 	if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
21458f4e2101SHugh Dickins 		goto out;
214665500d23SHugh Dickins 
214765500d23SHugh Dickins 	entry = pte_to_swp_entry(orig_pte);
21480697212aSChristoph Lameter 	if (is_migration_entry(entry)) {
21490697212aSChristoph Lameter 		migration_entry_wait(mm, pmd, address);
21500697212aSChristoph Lameter 		goto out;
21510697212aSChristoph Lameter 	}
21520ff92245SShailabh Nagar 	delayacct_set_flag(DELAYACCT_PF_SWAPIN);
21531da177e4SLinus Torvalds 	page = lookup_swap_cache(entry);
21541da177e4SLinus Torvalds 	if (!page) {
2155098fe651SAshwin Chaugule 		grab_swap_token(); /* Contend for token _before_ read-in */
21561da177e4SLinus Torvalds  		swapin_readahead(entry, address, vma);
21571da177e4SLinus Torvalds  		page = read_swap_cache_async(entry, vma, address);
21581da177e4SLinus Torvalds 		if (!page) {
21591da177e4SLinus Torvalds 			/*
21608f4e2101SHugh Dickins 			 * Back out if somebody else faulted in this pte
21618f4e2101SHugh Dickins 			 * while we released the pte lock.
21621da177e4SLinus Torvalds 			 */
21638f4e2101SHugh Dickins 			page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
21641da177e4SLinus Torvalds 			if (likely(pte_same(*page_table, orig_pte)))
21651da177e4SLinus Torvalds 				ret = VM_FAULT_OOM;
21660ff92245SShailabh Nagar 			delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
216765500d23SHugh Dickins 			goto unlock;
21681da177e4SLinus Torvalds 		}
21691da177e4SLinus Torvalds 
21701da177e4SLinus Torvalds 		/* Had to read the page from swap area: Major fault */
21711da177e4SLinus Torvalds 		ret = VM_FAULT_MAJOR;
2172f8891e5eSChristoph Lameter 		count_vm_event(PGMAJFAULT);
21731da177e4SLinus Torvalds 	}
21741da177e4SLinus Torvalds 
21750ff92245SShailabh Nagar 	delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
21761da177e4SLinus Torvalds 	mark_page_accessed(page);
21771da177e4SLinus Torvalds 	lock_page(page);
21781da177e4SLinus Torvalds 
21791da177e4SLinus Torvalds 	/*
21808f4e2101SHugh Dickins 	 * Back out if somebody else already faulted in this pte.
21811da177e4SLinus Torvalds 	 */
21828f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
21839e9bef07SHugh Dickins 	if (unlikely(!pte_same(*page_table, orig_pte)))
2184b8107480SKirill Korotaev 		goto out_nomap;
2185b8107480SKirill Korotaev 
2186b8107480SKirill Korotaev 	if (unlikely(!PageUptodate(page))) {
2187b8107480SKirill Korotaev 		ret = VM_FAULT_SIGBUS;
2188b8107480SKirill Korotaev 		goto out_nomap;
21891da177e4SLinus Torvalds 	}
21901da177e4SLinus Torvalds 
21911da177e4SLinus Torvalds 	/* The page isn't present yet, go ahead with the fault. */
21921da177e4SLinus Torvalds 
21934294621fSHugh Dickins 	inc_mm_counter(mm, anon_rss);
21941da177e4SLinus Torvalds 	pte = mk_pte(page, vma->vm_page_prot);
21951da177e4SLinus Torvalds 	if (write_access && can_share_swap_page(page)) {
21961da177e4SLinus Torvalds 		pte = maybe_mkwrite(pte_mkdirty(pte), vma);
21971da177e4SLinus Torvalds 		write_access = 0;
21981da177e4SLinus Torvalds 	}
21991da177e4SLinus Torvalds 
22001da177e4SLinus Torvalds 	flush_icache_page(vma, page);
22011da177e4SLinus Torvalds 	set_pte_at(mm, address, page_table, pte);
22021da177e4SLinus Torvalds 	page_add_anon_rmap(page, vma, address);
22031da177e4SLinus Torvalds 
2204c475a8abSHugh Dickins 	swap_free(entry);
2205c475a8abSHugh Dickins 	if (vm_swap_full())
2206c475a8abSHugh Dickins 		remove_exclusive_swap_page(page);
2207c475a8abSHugh Dickins 	unlock_page(page);
2208c475a8abSHugh Dickins 
22091da177e4SLinus Torvalds 	if (write_access) {
22101da177e4SLinus Torvalds 		if (do_wp_page(mm, vma, address,
22118f4e2101SHugh Dickins 				page_table, pmd, ptl, pte) == VM_FAULT_OOM)
22121da177e4SLinus Torvalds 			ret = VM_FAULT_OOM;
22131da177e4SLinus Torvalds 		goto out;
22141da177e4SLinus Torvalds 	}
22151da177e4SLinus Torvalds 
22161da177e4SLinus Torvalds 	/* No need to invalidate - it was non-present before */
22171da177e4SLinus Torvalds 	update_mmu_cache(vma, address, pte);
221865500d23SHugh Dickins unlock:
22198f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
22201da177e4SLinus Torvalds out:
22211da177e4SLinus Torvalds 	return ret;
2222b8107480SKirill Korotaev out_nomap:
22238f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
2224b8107480SKirill Korotaev 	unlock_page(page);
2225b8107480SKirill Korotaev 	page_cache_release(page);
222665500d23SHugh Dickins 	return ret;
22271da177e4SLinus Torvalds }
22281da177e4SLinus Torvalds 
22291da177e4SLinus Torvalds /*
22308f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
22318f4e2101SHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
22328f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
22331da177e4SLinus Torvalds  */
223465500d23SHugh Dickins static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
223565500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
223665500d23SHugh Dickins 		int write_access)
22371da177e4SLinus Torvalds {
22388f4e2101SHugh Dickins 	struct page *page;
22398f4e2101SHugh Dickins 	spinlock_t *ptl;
22401da177e4SLinus Torvalds 	pte_t entry;
22411da177e4SLinus Torvalds 
22426aab341eSLinus Torvalds 	if (write_access) {
22431da177e4SLinus Torvalds 		/* Allocate our own private page. */
22441da177e4SLinus Torvalds 		pte_unmap(page_table);
22451da177e4SLinus Torvalds 
22461da177e4SLinus Torvalds 		if (unlikely(anon_vma_prepare(vma)))
224765500d23SHugh Dickins 			goto oom;
2248769848c0SMel Gorman 		page = alloc_zeroed_user_highpage_movable(vma, address);
22491da177e4SLinus Torvalds 		if (!page)
225065500d23SHugh Dickins 			goto oom;
22511da177e4SLinus Torvalds 
225265500d23SHugh Dickins 		entry = mk_pte(page, vma->vm_page_prot);
225365500d23SHugh Dickins 		entry = maybe_mkwrite(pte_mkdirty(entry), vma);
22548f4e2101SHugh Dickins 
22558f4e2101SHugh Dickins 		page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
22568f4e2101SHugh Dickins 		if (!pte_none(*page_table))
22578f4e2101SHugh Dickins 			goto release;
22588f4e2101SHugh Dickins 		inc_mm_counter(mm, anon_rss);
22591da177e4SLinus Torvalds 		lru_cache_add_active(page);
22609617d95eSNick Piggin 		page_add_new_anon_rmap(page, vma, address);
2261b5810039SNick Piggin 	} else {
22628f4e2101SHugh Dickins 		/* Map the ZERO_PAGE - vm_page_prot is readonly */
22638f4e2101SHugh Dickins 		page = ZERO_PAGE(address);
22648f4e2101SHugh Dickins 		page_cache_get(page);
22658f4e2101SHugh Dickins 		entry = mk_pte(page, vma->vm_page_prot);
22668f4e2101SHugh Dickins 
22674c21e2f2SHugh Dickins 		ptl = pte_lockptr(mm, pmd);
22688f4e2101SHugh Dickins 		spin_lock(ptl);
22698f4e2101SHugh Dickins 		if (!pte_none(*page_table))
22708f4e2101SHugh Dickins 			goto release;
2271b5810039SNick Piggin 		inc_mm_counter(mm, file_rss);
2272b5810039SNick Piggin 		page_add_file_rmap(page);
22731da177e4SLinus Torvalds 	}
22741da177e4SLinus Torvalds 
227565500d23SHugh Dickins 	set_pte_at(mm, address, page_table, entry);
22761da177e4SLinus Torvalds 
22771da177e4SLinus Torvalds 	/* No need to invalidate - it was non-present before */
227865500d23SHugh Dickins 	update_mmu_cache(vma, address, entry);
22791da177e4SLinus Torvalds 	lazy_mmu_prot_update(entry);
228065500d23SHugh Dickins unlock:
22818f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
22821da177e4SLinus Torvalds 	return VM_FAULT_MINOR;
22838f4e2101SHugh Dickins release:
22848f4e2101SHugh Dickins 	page_cache_release(page);
22858f4e2101SHugh Dickins 	goto unlock;
228665500d23SHugh Dickins oom:
22871da177e4SLinus Torvalds 	return VM_FAULT_OOM;
22881da177e4SLinus Torvalds }
22891da177e4SLinus Torvalds 
22901da177e4SLinus Torvalds /*
22911da177e4SLinus Torvalds  * do_no_page() tries to create a new page mapping. It aggressively
22921da177e4SLinus Torvalds  * tries to share with existing pages, but makes a separate copy if
22931da177e4SLinus Torvalds  * the "write_access" parameter is true in order to avoid the next
22941da177e4SLinus Torvalds  * page fault.
22951da177e4SLinus Torvalds  *
22961da177e4SLinus Torvalds  * As this is called only for pages that do not currently exist, we
22971da177e4SLinus Torvalds  * do not need to flush old virtual caches or the TLB.
22981da177e4SLinus Torvalds  *
22998f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
23008f4e2101SHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
23018f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
23021da177e4SLinus Torvalds  */
230365500d23SHugh Dickins static int do_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
230465500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
230565500d23SHugh Dickins 		int write_access)
23061da177e4SLinus Torvalds {
23078f4e2101SHugh Dickins 	spinlock_t *ptl;
2308*d00806b1SNick Piggin 	struct page *page, *nopage_page;
23091da177e4SLinus Torvalds 	pte_t entry;
23101da177e4SLinus Torvalds 	int ret = VM_FAULT_MINOR;
23111da177e4SLinus Torvalds 	int anon = 0;
2312d08b3851SPeter Zijlstra 	struct page *dirty_page = NULL;
23131da177e4SLinus Torvalds 
23141da177e4SLinus Torvalds 	pte_unmap(page_table);
2315325f04dbSHugh Dickins 	BUG_ON(vma->vm_flags & VM_PFNMAP);
2316325f04dbSHugh Dickins 
2317*d00806b1SNick Piggin 	nopage_page = vma->vm_ops->nopage(vma, address & PAGE_MASK, &ret);
23187f7bbbe5SBenjamin Herrenschmidt 	/* no page was available -- either SIGBUS, OOM or REFAULT */
2319*d00806b1SNick Piggin 	if (unlikely(nopage_page == NOPAGE_SIGBUS))
23201da177e4SLinus Torvalds 		return VM_FAULT_SIGBUS;
2321*d00806b1SNick Piggin 	else if (unlikely(nopage_page == NOPAGE_OOM))
23221da177e4SLinus Torvalds 		return VM_FAULT_OOM;
2323*d00806b1SNick Piggin 	else if (unlikely(nopage_page == NOPAGE_REFAULT))
23247f7bbbe5SBenjamin Herrenschmidt 		return VM_FAULT_MINOR;
23251da177e4SLinus Torvalds 
2326*d00806b1SNick Piggin 	BUG_ON(vma->vm_flags & VM_CAN_INVALIDATE && !PageLocked(nopage_page));
2327*d00806b1SNick Piggin 	/*
2328*d00806b1SNick Piggin 	 * For consistency in subsequent calls, make the nopage_page always
2329*d00806b1SNick Piggin 	 * locked.
2330*d00806b1SNick Piggin 	 */
2331*d00806b1SNick Piggin 	if (unlikely(!(vma->vm_flags & VM_CAN_INVALIDATE)))
2332*d00806b1SNick Piggin 		lock_page(nopage_page);
2333*d00806b1SNick Piggin 
23341da177e4SLinus Torvalds 	/*
23351da177e4SLinus Torvalds 	 * Should we do an early C-O-W break?
23361da177e4SLinus Torvalds 	 */
2337*d00806b1SNick Piggin 	page = nopage_page;
23389637a5efSDavid Howells 	if (write_access) {
23399637a5efSDavid Howells 		if (!(vma->vm_flags & VM_SHARED)) {
2340*d00806b1SNick Piggin 			if (unlikely(anon_vma_prepare(vma))) {
2341*d00806b1SNick Piggin 				ret = VM_FAULT_OOM;
2342*d00806b1SNick Piggin 				goto out_error;
2343*d00806b1SNick Piggin 			}
2344*d00806b1SNick Piggin 			page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
2345*d00806b1SNick Piggin 			if (!page) {
2346*d00806b1SNick Piggin 				ret = VM_FAULT_OOM;
2347*d00806b1SNick Piggin 				goto out_error;
2348*d00806b1SNick Piggin 			}
2349*d00806b1SNick Piggin 			copy_user_highpage(page, nopage_page, address, vma);
23501da177e4SLinus Torvalds 			anon = 1;
23519637a5efSDavid Howells 		} else {
23529637a5efSDavid Howells 			/* if the page will be shareable, see if the backing
23539637a5efSDavid Howells 			 * address space wants to know that the page is about
23549637a5efSDavid Howells 			 * to become writable */
23559637a5efSDavid Howells 			if (vma->vm_ops->page_mkwrite &&
2356*d00806b1SNick Piggin 			    vma->vm_ops->page_mkwrite(vma, page) < 0) {
2357*d00806b1SNick Piggin 				ret = VM_FAULT_SIGBUS;
2358*d00806b1SNick Piggin 				goto out_error;
23599637a5efSDavid Howells 			}
23609637a5efSDavid Howells 		}
23611da177e4SLinus Torvalds 	}
23621da177e4SLinus Torvalds 
23638f4e2101SHugh Dickins 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
23641da177e4SLinus Torvalds 
23651da177e4SLinus Torvalds 	/*
23661da177e4SLinus Torvalds 	 * This silly early PAGE_DIRTY setting removes a race
23671da177e4SLinus Torvalds 	 * due to the bad i386 page protection. But it's valid
23681da177e4SLinus Torvalds 	 * for other architectures too.
23691da177e4SLinus Torvalds 	 *
23701da177e4SLinus Torvalds 	 * Note that if write_access is true, we either now have
23711da177e4SLinus Torvalds 	 * an exclusive copy of the page, or this is a shared mapping,
23721da177e4SLinus Torvalds 	 * so we can make it writable and dirty to avoid having to
23731da177e4SLinus Torvalds 	 * handle that later.
23741da177e4SLinus Torvalds 	 */
23751da177e4SLinus Torvalds 	/* Only go through if we didn't race with anybody else... */
2376*d00806b1SNick Piggin 	if (likely(pte_none(*page_table))) {
2377*d00806b1SNick Piggin 		flush_icache_page(vma, page);
2378*d00806b1SNick Piggin 		entry = mk_pte(page, vma->vm_page_prot);
23791da177e4SLinus Torvalds 		if (write_access)
23801da177e4SLinus Torvalds 			entry = maybe_mkwrite(pte_mkdirty(entry), vma);
23811da177e4SLinus Torvalds 		set_pte_at(mm, address, page_table, entry);
23821da177e4SLinus Torvalds 		if (anon) {
23834294621fSHugh Dickins                         inc_mm_counter(mm, anon_rss);
2384*d00806b1SNick Piggin                         lru_cache_add_active(page);
2385*d00806b1SNick Piggin                         page_add_new_anon_rmap(page, vma, address);
2386f57e88a8SHugh Dickins 		} else {
23874294621fSHugh Dickins 			inc_mm_counter(mm, file_rss);
2388*d00806b1SNick Piggin 			page_add_file_rmap(page);
2389d08b3851SPeter Zijlstra 			if (write_access) {
2390*d00806b1SNick Piggin 				dirty_page = page;
2391d08b3851SPeter Zijlstra 				get_page(dirty_page);
2392d08b3851SPeter Zijlstra 			}
23934294621fSHugh Dickins 		}
23941da177e4SLinus Torvalds 
2395*d00806b1SNick Piggin 		/* no need to invalidate: a not-present page won't be cached */
23961da177e4SLinus Torvalds 		update_mmu_cache(vma, address, entry);
23971da177e4SLinus Torvalds 		lazy_mmu_prot_update(entry);
2398*d00806b1SNick Piggin 	} else {
2399*d00806b1SNick Piggin 		if (anon)
2400*d00806b1SNick Piggin 			page_cache_release(page);
2401*d00806b1SNick Piggin 		else
2402*d00806b1SNick Piggin 			anon = 1; /* not anon, but release nopage_page */
2403*d00806b1SNick Piggin 	}
2404*d00806b1SNick Piggin 
24058f4e2101SHugh Dickins 	pte_unmap_unlock(page_table, ptl);
2406*d00806b1SNick Piggin 
2407*d00806b1SNick Piggin out:
2408*d00806b1SNick Piggin 	unlock_page(nopage_page);
2409*d00806b1SNick Piggin 	if (anon)
2410*d00806b1SNick Piggin 		page_cache_release(nopage_page);
2411*d00806b1SNick Piggin 	else if (dirty_page) {
2412edc79b2aSPeter Zijlstra 		set_page_dirty_balance(dirty_page);
2413d08b3851SPeter Zijlstra 		put_page(dirty_page);
2414d08b3851SPeter Zijlstra 	}
2415*d00806b1SNick Piggin 
24161da177e4SLinus Torvalds 	return ret;
2417*d00806b1SNick Piggin 
2418*d00806b1SNick Piggin out_error:
2419*d00806b1SNick Piggin 	anon = 1; /* relase nopage_page */
2420*d00806b1SNick Piggin 	goto out;
24211da177e4SLinus Torvalds }
24221da177e4SLinus Torvalds 
24231da177e4SLinus Torvalds /*
2424f4b81804SJes Sorensen  * do_no_pfn() tries to create a new page mapping for a page without
2425f4b81804SJes Sorensen  * a struct_page backing it
2426f4b81804SJes Sorensen  *
2427f4b81804SJes Sorensen  * As this is called only for pages that do not currently exist, we
2428f4b81804SJes Sorensen  * do not need to flush old virtual caches or the TLB.
2429f4b81804SJes Sorensen  *
2430f4b81804SJes Sorensen  * We enter with non-exclusive mmap_sem (to exclude vma changes,
2431f4b81804SJes Sorensen  * but allow concurrent faults), and pte mapped but not yet locked.
2432f4b81804SJes Sorensen  * We return with mmap_sem still held, but pte unmapped and unlocked.
2433f4b81804SJes Sorensen  *
2434f4b81804SJes Sorensen  * It is expected that the ->nopfn handler always returns the same pfn
2435f4b81804SJes Sorensen  * for a given virtual mapping.
2436f4b81804SJes Sorensen  *
2437f4b81804SJes Sorensen  * Mark this `noinline' to prevent it from bloating the main pagefault code.
2438f4b81804SJes Sorensen  */
2439f4b81804SJes Sorensen static noinline int do_no_pfn(struct mm_struct *mm, struct vm_area_struct *vma,
2440f4b81804SJes Sorensen 		     unsigned long address, pte_t *page_table, pmd_t *pmd,
2441f4b81804SJes Sorensen 		     int write_access)
2442f4b81804SJes Sorensen {
2443f4b81804SJes Sorensen 	spinlock_t *ptl;
2444f4b81804SJes Sorensen 	pte_t entry;
2445f4b81804SJes Sorensen 	unsigned long pfn;
2446f4b81804SJes Sorensen 	int ret = VM_FAULT_MINOR;
2447f4b81804SJes Sorensen 
2448f4b81804SJes Sorensen 	pte_unmap(page_table);
2449f4b81804SJes Sorensen 	BUG_ON(!(vma->vm_flags & VM_PFNMAP));
2450f4b81804SJes Sorensen 	BUG_ON(is_cow_mapping(vma->vm_flags));
2451f4b81804SJes Sorensen 
2452f4b81804SJes Sorensen 	pfn = vma->vm_ops->nopfn(vma, address & PAGE_MASK);
245322cd25edSBenjamin Herrenschmidt 	if (unlikely(pfn == NOPFN_OOM))
2454f4b81804SJes Sorensen 		return VM_FAULT_OOM;
245522cd25edSBenjamin Herrenschmidt 	else if (unlikely(pfn == NOPFN_SIGBUS))
2456f4b81804SJes Sorensen 		return VM_FAULT_SIGBUS;
245722cd25edSBenjamin Herrenschmidt 	else if (unlikely(pfn == NOPFN_REFAULT))
245822cd25edSBenjamin Herrenschmidt 		return VM_FAULT_MINOR;
2459f4b81804SJes Sorensen 
2460f4b81804SJes Sorensen 	page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
2461f4b81804SJes Sorensen 
2462f4b81804SJes Sorensen 	/* Only go through if we didn't race with anybody else... */
2463f4b81804SJes Sorensen 	if (pte_none(*page_table)) {
2464f4b81804SJes Sorensen 		entry = pfn_pte(pfn, vma->vm_page_prot);
2465f4b81804SJes Sorensen 		if (write_access)
2466f4b81804SJes Sorensen 			entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2467f4b81804SJes Sorensen 		set_pte_at(mm, address, page_table, entry);
2468f4b81804SJes Sorensen 	}
2469f4b81804SJes Sorensen 	pte_unmap_unlock(page_table, ptl);
2470f4b81804SJes Sorensen 	return ret;
2471f4b81804SJes Sorensen }
2472f4b81804SJes Sorensen 
2473f4b81804SJes Sorensen /*
24741da177e4SLinus Torvalds  * Fault of a previously existing named mapping. Repopulate the pte
24751da177e4SLinus Torvalds  * from the encoded file_pte if possible. This enables swappable
24761da177e4SLinus Torvalds  * nonlinear vmas.
24778f4e2101SHugh Dickins  *
24788f4e2101SHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
24798f4e2101SHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
24808f4e2101SHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
24811da177e4SLinus Torvalds  */
24821da177e4SLinus Torvalds static int do_file_page(struct mm_struct *mm, struct vm_area_struct *vma,
248365500d23SHugh Dickins 		unsigned long address, pte_t *page_table, pmd_t *pmd,
248465500d23SHugh Dickins 		int write_access, pte_t orig_pte)
24851da177e4SLinus Torvalds {
248665500d23SHugh Dickins 	pgoff_t pgoff;
24871da177e4SLinus Torvalds 	int err;
24881da177e4SLinus Torvalds 
24894c21e2f2SHugh Dickins 	if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
24908f4e2101SHugh Dickins 		return VM_FAULT_MINOR;
24911da177e4SLinus Torvalds 
249265500d23SHugh Dickins 	if (unlikely(!(vma->vm_flags & VM_NONLINEAR))) {
249365500d23SHugh Dickins 		/*
249465500d23SHugh Dickins 		 * Page table corrupted: show pte and kill process.
249565500d23SHugh Dickins 		 */
2496b5810039SNick Piggin 		print_bad_pte(vma, orig_pte, address);
249765500d23SHugh Dickins 		return VM_FAULT_OOM;
249865500d23SHugh Dickins 	}
249965500d23SHugh Dickins 	/* We can then assume vm->vm_ops && vma->vm_ops->populate */
250065500d23SHugh Dickins 
250165500d23SHugh Dickins 	pgoff = pte_to_pgoff(orig_pte);
250265500d23SHugh Dickins 	err = vma->vm_ops->populate(vma, address & PAGE_MASK, PAGE_SIZE,
250365500d23SHugh Dickins 					vma->vm_page_prot, pgoff, 0);
25041da177e4SLinus Torvalds 	if (err == -ENOMEM)
25051da177e4SLinus Torvalds 		return VM_FAULT_OOM;
25061da177e4SLinus Torvalds 	if (err)
25071da177e4SLinus Torvalds 		return VM_FAULT_SIGBUS;
25081da177e4SLinus Torvalds 	return VM_FAULT_MAJOR;
25091da177e4SLinus Torvalds }
25101da177e4SLinus Torvalds 
25111da177e4SLinus Torvalds /*
25121da177e4SLinus Torvalds  * These routines also need to handle stuff like marking pages dirty
25131da177e4SLinus Torvalds  * and/or accessed for architectures that don't do it in hardware (most
25141da177e4SLinus Torvalds  * RISC architectures).  The early dirtying is also good on the i386.
25151da177e4SLinus Torvalds  *
25161da177e4SLinus Torvalds  * There is also a hook called "update_mmu_cache()" that architectures
25171da177e4SLinus Torvalds  * with external mmu caches can use to update those (ie the Sparc or
25181da177e4SLinus Torvalds  * PowerPC hashed page tables that act as extended TLBs).
25191da177e4SLinus Torvalds  *
2520c74df32cSHugh Dickins  * We enter with non-exclusive mmap_sem (to exclude vma changes,
2521c74df32cSHugh Dickins  * but allow concurrent faults), and pte mapped but not yet locked.
2522c74df32cSHugh Dickins  * We return with mmap_sem still held, but pte unmapped and unlocked.
25231da177e4SLinus Torvalds  */
25241da177e4SLinus Torvalds static inline int handle_pte_fault(struct mm_struct *mm,
25251da177e4SLinus Torvalds 		struct vm_area_struct *vma, unsigned long address,
252665500d23SHugh Dickins 		pte_t *pte, pmd_t *pmd, int write_access)
25271da177e4SLinus Torvalds {
25281da177e4SLinus Torvalds 	pte_t entry;
25298f4e2101SHugh Dickins 	spinlock_t *ptl;
25301da177e4SLinus Torvalds 
25318dab5241SBenjamin Herrenschmidt 	entry = *pte;
25321da177e4SLinus Torvalds 	if (!pte_present(entry)) {
253365500d23SHugh Dickins 		if (pte_none(entry)) {
2534f4b81804SJes Sorensen 			if (vma->vm_ops) {
2535f4b81804SJes Sorensen 				if (vma->vm_ops->nopage)
253665500d23SHugh Dickins 					return do_no_page(mm, vma, address,
2537f4b81804SJes Sorensen 							  pte, pmd,
2538f4b81804SJes Sorensen 							  write_access);
2539f4b81804SJes Sorensen 				if (unlikely(vma->vm_ops->nopfn))
2540f4b81804SJes Sorensen 					return do_no_pfn(mm, vma, address, pte,
2541f4b81804SJes Sorensen 							 pmd, write_access);
2542f4b81804SJes Sorensen 			}
2543f4b81804SJes Sorensen 			return do_anonymous_page(mm, vma, address,
254465500d23SHugh Dickins 						 pte, pmd, write_access);
254565500d23SHugh Dickins 		}
25461da177e4SLinus Torvalds 		if (pte_file(entry))
254765500d23SHugh Dickins 			return do_file_page(mm, vma, address,
254865500d23SHugh Dickins 					pte, pmd, write_access, entry);
254965500d23SHugh Dickins 		return do_swap_page(mm, vma, address,
255065500d23SHugh Dickins 					pte, pmd, write_access, entry);
25511da177e4SLinus Torvalds 	}
25521da177e4SLinus Torvalds 
25534c21e2f2SHugh Dickins 	ptl = pte_lockptr(mm, pmd);
25548f4e2101SHugh Dickins 	spin_lock(ptl);
25558f4e2101SHugh Dickins 	if (unlikely(!pte_same(*pte, entry)))
25568f4e2101SHugh Dickins 		goto unlock;
25571da177e4SLinus Torvalds 	if (write_access) {
25581da177e4SLinus Torvalds 		if (!pte_write(entry))
25598f4e2101SHugh Dickins 			return do_wp_page(mm, vma, address,
25608f4e2101SHugh Dickins 					pte, pmd, ptl, entry);
25611da177e4SLinus Torvalds 		entry = pte_mkdirty(entry);
25621da177e4SLinus Torvalds 	}
25631da177e4SLinus Torvalds 	entry = pte_mkyoung(entry);
25648dab5241SBenjamin Herrenschmidt 	if (ptep_set_access_flags(vma, address, pte, entry, write_access)) {
25651da177e4SLinus Torvalds 		update_mmu_cache(vma, address, entry);
25661da177e4SLinus Torvalds 		lazy_mmu_prot_update(entry);
25671a44e149SAndrea Arcangeli 	} else {
25681a44e149SAndrea Arcangeli 		/*
25691a44e149SAndrea Arcangeli 		 * This is needed only for protection faults but the arch code
25701a44e149SAndrea Arcangeli 		 * is not yet telling us if this is a protection fault or not.
25711a44e149SAndrea Arcangeli 		 * This still avoids useless tlb flushes for .text page faults
25721a44e149SAndrea Arcangeli 		 * with threads.
25731a44e149SAndrea Arcangeli 		 */
25741a44e149SAndrea Arcangeli 		if (write_access)
25751a44e149SAndrea Arcangeli 			flush_tlb_page(vma, address);
25761a44e149SAndrea Arcangeli 	}
25778f4e2101SHugh Dickins unlock:
25788f4e2101SHugh Dickins 	pte_unmap_unlock(pte, ptl);
25791da177e4SLinus Torvalds 	return VM_FAULT_MINOR;
25801da177e4SLinus Torvalds }
25811da177e4SLinus Torvalds 
25821da177e4SLinus Torvalds /*
25831da177e4SLinus Torvalds  * By the time we get here, we already hold the mm semaphore
25841da177e4SLinus Torvalds  */
2585f33ea7f4SNick Piggin int __handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
25861da177e4SLinus Torvalds 		unsigned long address, int write_access)
25871da177e4SLinus Torvalds {
25881da177e4SLinus Torvalds 	pgd_t *pgd;
25891da177e4SLinus Torvalds 	pud_t *pud;
25901da177e4SLinus Torvalds 	pmd_t *pmd;
25911da177e4SLinus Torvalds 	pte_t *pte;
25921da177e4SLinus Torvalds 
25931da177e4SLinus Torvalds 	__set_current_state(TASK_RUNNING);
25941da177e4SLinus Torvalds 
2595f8891e5eSChristoph Lameter 	count_vm_event(PGFAULT);
25961da177e4SLinus Torvalds 
2597ac9b9c66SHugh Dickins 	if (unlikely(is_vm_hugetlb_page(vma)))
2598ac9b9c66SHugh Dickins 		return hugetlb_fault(mm, vma, address, write_access);
25991da177e4SLinus Torvalds 
26001da177e4SLinus Torvalds 	pgd = pgd_offset(mm, address);
26011da177e4SLinus Torvalds 	pud = pud_alloc(mm, pgd, address);
26021da177e4SLinus Torvalds 	if (!pud)
2603c74df32cSHugh Dickins 		return VM_FAULT_OOM;
26041da177e4SLinus Torvalds 	pmd = pmd_alloc(mm, pud, address);
26051da177e4SLinus Torvalds 	if (!pmd)
2606c74df32cSHugh Dickins 		return VM_FAULT_OOM;
26071da177e4SLinus Torvalds 	pte = pte_alloc_map(mm, pmd, address);
26081da177e4SLinus Torvalds 	if (!pte)
2609c74df32cSHugh Dickins 		return VM_FAULT_OOM;
26101da177e4SLinus Torvalds 
261165500d23SHugh Dickins 	return handle_pte_fault(mm, vma, address, pte, pmd, write_access);
26121da177e4SLinus Torvalds }
26131da177e4SLinus Torvalds 
261467207b96SArnd Bergmann EXPORT_SYMBOL_GPL(__handle_mm_fault);
261567207b96SArnd Bergmann 
26161da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED
26171da177e4SLinus Torvalds /*
26181da177e4SLinus Torvalds  * Allocate page upper directory.
2619872fec16SHugh Dickins  * We've already handled the fast-path in-line.
26201da177e4SLinus Torvalds  */
26211bb3630eSHugh Dickins int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
26221da177e4SLinus Torvalds {
2623c74df32cSHugh Dickins 	pud_t *new = pud_alloc_one(mm, address);
2624c74df32cSHugh Dickins 	if (!new)
26251bb3630eSHugh Dickins 		return -ENOMEM;
26261da177e4SLinus Torvalds 
2627872fec16SHugh Dickins 	spin_lock(&mm->page_table_lock);
26281bb3630eSHugh Dickins 	if (pgd_present(*pgd))		/* Another has populated it */
26291da177e4SLinus Torvalds 		pud_free(new);
26301bb3630eSHugh Dickins 	else
26311da177e4SLinus Torvalds 		pgd_populate(mm, pgd, new);
2632872fec16SHugh Dickins 	spin_unlock(&mm->page_table_lock);
26331bb3630eSHugh Dickins 	return 0;
26341da177e4SLinus Torvalds }
26351da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */
26361da177e4SLinus Torvalds 
26371da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED
26381da177e4SLinus Torvalds /*
26391da177e4SLinus Torvalds  * Allocate page middle directory.
2640872fec16SHugh Dickins  * We've already handled the fast-path in-line.
26411da177e4SLinus Torvalds  */
26421bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
26431da177e4SLinus Torvalds {
2644c74df32cSHugh Dickins 	pmd_t *new = pmd_alloc_one(mm, address);
2645c74df32cSHugh Dickins 	if (!new)
26461bb3630eSHugh Dickins 		return -ENOMEM;
26471da177e4SLinus Torvalds 
2648872fec16SHugh Dickins 	spin_lock(&mm->page_table_lock);
26491da177e4SLinus Torvalds #ifndef __ARCH_HAS_4LEVEL_HACK
26501bb3630eSHugh Dickins 	if (pud_present(*pud))		/* Another has populated it */
26511da177e4SLinus Torvalds 		pmd_free(new);
26521bb3630eSHugh Dickins 	else
26531da177e4SLinus Torvalds 		pud_populate(mm, pud, new);
26541da177e4SLinus Torvalds #else
26551bb3630eSHugh Dickins 	if (pgd_present(*pud))		/* Another has populated it */
26561da177e4SLinus Torvalds 		pmd_free(new);
26571bb3630eSHugh Dickins 	else
26581da177e4SLinus Torvalds 		pgd_populate(mm, pud, new);
26591da177e4SLinus Torvalds #endif /* __ARCH_HAS_4LEVEL_HACK */
2660872fec16SHugh Dickins 	spin_unlock(&mm->page_table_lock);
26611bb3630eSHugh Dickins 	return 0;
26621da177e4SLinus Torvalds }
26631da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */
26641da177e4SLinus Torvalds 
26651da177e4SLinus Torvalds int make_pages_present(unsigned long addr, unsigned long end)
26661da177e4SLinus Torvalds {
26671da177e4SLinus Torvalds 	int ret, len, write;
26681da177e4SLinus Torvalds 	struct vm_area_struct * vma;
26691da177e4SLinus Torvalds 
26701da177e4SLinus Torvalds 	vma = find_vma(current->mm, addr);
26711da177e4SLinus Torvalds 	if (!vma)
26721da177e4SLinus Torvalds 		return -1;
26731da177e4SLinus Torvalds 	write = (vma->vm_flags & VM_WRITE) != 0;
26745bcb28b1SEric Sesterhenn 	BUG_ON(addr >= end);
26755bcb28b1SEric Sesterhenn 	BUG_ON(end > vma->vm_end);
267668e116a3SRolf Eike Beer 	len = DIV_ROUND_UP(end, PAGE_SIZE) - addr/PAGE_SIZE;
26771da177e4SLinus Torvalds 	ret = get_user_pages(current, current->mm, addr,
26781da177e4SLinus Torvalds 			len, write, 0, NULL, NULL);
26791da177e4SLinus Torvalds 	if (ret < 0)
26801da177e4SLinus Torvalds 		return ret;
26811da177e4SLinus Torvalds 	return ret == len ? 0 : -1;
26821da177e4SLinus Torvalds }
26831da177e4SLinus Torvalds 
26841da177e4SLinus Torvalds /*
26851da177e4SLinus Torvalds  * Map a vmalloc()-space virtual address to the physical page.
26861da177e4SLinus Torvalds  */
26871da177e4SLinus Torvalds struct page * vmalloc_to_page(void * vmalloc_addr)
26881da177e4SLinus Torvalds {
26891da177e4SLinus Torvalds 	unsigned long addr = (unsigned long) vmalloc_addr;
26901da177e4SLinus Torvalds 	struct page *page = NULL;
26911da177e4SLinus Torvalds 	pgd_t *pgd = pgd_offset_k(addr);
26921da177e4SLinus Torvalds 	pud_t *pud;
26931da177e4SLinus Torvalds 	pmd_t *pmd;
26941da177e4SLinus Torvalds 	pte_t *ptep, pte;
26951da177e4SLinus Torvalds 
26961da177e4SLinus Torvalds 	if (!pgd_none(*pgd)) {
26971da177e4SLinus Torvalds 		pud = pud_offset(pgd, addr);
26981da177e4SLinus Torvalds 		if (!pud_none(*pud)) {
26991da177e4SLinus Torvalds 			pmd = pmd_offset(pud, addr);
27001da177e4SLinus Torvalds 			if (!pmd_none(*pmd)) {
27011da177e4SLinus Torvalds 				ptep = pte_offset_map(pmd, addr);
27021da177e4SLinus Torvalds 				pte = *ptep;
27031da177e4SLinus Torvalds 				if (pte_present(pte))
27041da177e4SLinus Torvalds 					page = pte_page(pte);
27051da177e4SLinus Torvalds 				pte_unmap(ptep);
27061da177e4SLinus Torvalds 			}
27071da177e4SLinus Torvalds 		}
27081da177e4SLinus Torvalds 	}
27091da177e4SLinus Torvalds 	return page;
27101da177e4SLinus Torvalds }
27111da177e4SLinus Torvalds 
27121da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc_to_page);
27131da177e4SLinus Torvalds 
27141da177e4SLinus Torvalds /*
27151da177e4SLinus Torvalds  * Map a vmalloc()-space virtual address to the physical page frame number.
27161da177e4SLinus Torvalds  */
27171da177e4SLinus Torvalds unsigned long vmalloc_to_pfn(void * vmalloc_addr)
27181da177e4SLinus Torvalds {
27191da177e4SLinus Torvalds 	return page_to_pfn(vmalloc_to_page(vmalloc_addr));
27201da177e4SLinus Torvalds }
27211da177e4SLinus Torvalds 
27221da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc_to_pfn);
27231da177e4SLinus Torvalds 
27241da177e4SLinus Torvalds #if !defined(__HAVE_ARCH_GATE_AREA)
27251da177e4SLinus Torvalds 
27261da177e4SLinus Torvalds #if defined(AT_SYSINFO_EHDR)
27275ce7852cSAdrian Bunk static struct vm_area_struct gate_vma;
27281da177e4SLinus Torvalds 
27291da177e4SLinus Torvalds static int __init gate_vma_init(void)
27301da177e4SLinus Torvalds {
27311da177e4SLinus Torvalds 	gate_vma.vm_mm = NULL;
27321da177e4SLinus Torvalds 	gate_vma.vm_start = FIXADDR_USER_START;
27331da177e4SLinus Torvalds 	gate_vma.vm_end = FIXADDR_USER_END;
2734b6558c4aSRoland McGrath 	gate_vma.vm_flags = VM_READ | VM_MAYREAD | VM_EXEC | VM_MAYEXEC;
2735b6558c4aSRoland McGrath 	gate_vma.vm_page_prot = __P101;
2736f47aef55SRoland McGrath 	/*
2737f47aef55SRoland McGrath 	 * Make sure the vDSO gets into every core dump.
2738f47aef55SRoland McGrath 	 * Dumping its contents makes post-mortem fully interpretable later
2739f47aef55SRoland McGrath 	 * without matching up the same kernel and hardware config to see
2740f47aef55SRoland McGrath 	 * what PC values meant.
2741f47aef55SRoland McGrath 	 */
2742f47aef55SRoland McGrath 	gate_vma.vm_flags |= VM_ALWAYSDUMP;
27431da177e4SLinus Torvalds 	return 0;
27441da177e4SLinus Torvalds }
27451da177e4SLinus Torvalds __initcall(gate_vma_init);
27461da177e4SLinus Torvalds #endif
27471da177e4SLinus Torvalds 
27481da177e4SLinus Torvalds struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
27491da177e4SLinus Torvalds {
27501da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR
27511da177e4SLinus Torvalds 	return &gate_vma;
27521da177e4SLinus Torvalds #else
27531da177e4SLinus Torvalds 	return NULL;
27541da177e4SLinus Torvalds #endif
27551da177e4SLinus Torvalds }
27561da177e4SLinus Torvalds 
27571da177e4SLinus Torvalds int in_gate_area_no_task(unsigned long addr)
27581da177e4SLinus Torvalds {
27591da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR
27601da177e4SLinus Torvalds 	if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))
27611da177e4SLinus Torvalds 		return 1;
27621da177e4SLinus Torvalds #endif
27631da177e4SLinus Torvalds 	return 0;
27641da177e4SLinus Torvalds }
27651da177e4SLinus Torvalds 
27661da177e4SLinus Torvalds #endif	/* __HAVE_ARCH_GATE_AREA */
27670ec76a11SDavid Howells 
27680ec76a11SDavid Howells /*
27690ec76a11SDavid Howells  * Access another process' address space.
27700ec76a11SDavid Howells  * Source/target buffer must be kernel space,
27710ec76a11SDavid Howells  * Do not walk the page table directly, use get_user_pages
27720ec76a11SDavid Howells  */
27730ec76a11SDavid Howells int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
27740ec76a11SDavid Howells {
27750ec76a11SDavid Howells 	struct mm_struct *mm;
27760ec76a11SDavid Howells 	struct vm_area_struct *vma;
27770ec76a11SDavid Howells 	struct page *page;
27780ec76a11SDavid Howells 	void *old_buf = buf;
27790ec76a11SDavid Howells 
27800ec76a11SDavid Howells 	mm = get_task_mm(tsk);
27810ec76a11SDavid Howells 	if (!mm)
27820ec76a11SDavid Howells 		return 0;
27830ec76a11SDavid Howells 
27840ec76a11SDavid Howells 	down_read(&mm->mmap_sem);
27850ec76a11SDavid Howells 	/* ignore errors, just check how much was sucessfully transfered */
27860ec76a11SDavid Howells 	while (len) {
27870ec76a11SDavid Howells 		int bytes, ret, offset;
27880ec76a11SDavid Howells 		void *maddr;
27890ec76a11SDavid Howells 
27900ec76a11SDavid Howells 		ret = get_user_pages(tsk, mm, addr, 1,
27910ec76a11SDavid Howells 				write, 1, &page, &vma);
27920ec76a11SDavid Howells 		if (ret <= 0)
27930ec76a11SDavid Howells 			break;
27940ec76a11SDavid Howells 
27950ec76a11SDavid Howells 		bytes = len;
27960ec76a11SDavid Howells 		offset = addr & (PAGE_SIZE-1);
27970ec76a11SDavid Howells 		if (bytes > PAGE_SIZE-offset)
27980ec76a11SDavid Howells 			bytes = PAGE_SIZE-offset;
27990ec76a11SDavid Howells 
28000ec76a11SDavid Howells 		maddr = kmap(page);
28010ec76a11SDavid Howells 		if (write) {
28020ec76a11SDavid Howells 			copy_to_user_page(vma, page, addr,
28030ec76a11SDavid Howells 					  maddr + offset, buf, bytes);
28040ec76a11SDavid Howells 			set_page_dirty_lock(page);
28050ec76a11SDavid Howells 		} else {
28060ec76a11SDavid Howells 			copy_from_user_page(vma, page, addr,
28070ec76a11SDavid Howells 					    buf, maddr + offset, bytes);
28080ec76a11SDavid Howells 		}
28090ec76a11SDavid Howells 		kunmap(page);
28100ec76a11SDavid Howells 		page_cache_release(page);
28110ec76a11SDavid Howells 		len -= bytes;
28120ec76a11SDavid Howells 		buf += bytes;
28130ec76a11SDavid Howells 		addr += bytes;
28140ec76a11SDavid Howells 	}
28150ec76a11SDavid Howells 	up_read(&mm->mmap_sem);
28160ec76a11SDavid Howells 	mmput(mm);
28170ec76a11SDavid Howells 
28180ec76a11SDavid Howells 	return buf - old_buf;
28190ec76a11SDavid Howells }
2820