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> 538a9f3ccdSBalbir Singh #include <linux/memcontrol.h> 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds #include <asm/pgalloc.h> 561da177e4SLinus Torvalds #include <asm/uaccess.h> 571da177e4SLinus Torvalds #include <asm/tlb.h> 581da177e4SLinus Torvalds #include <asm/tlbflush.h> 591da177e4SLinus Torvalds #include <asm/pgtable.h> 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds #include <linux/swapops.h> 621da177e4SLinus Torvalds #include <linux/elf.h> 631da177e4SLinus Torvalds 64d41dee36SAndy Whitcroft #ifndef CONFIG_NEED_MULTIPLE_NODES 651da177e4SLinus Torvalds /* use the per-pgdat data instead for discontigmem - mbligh */ 661da177e4SLinus Torvalds unsigned long max_mapnr; 671da177e4SLinus Torvalds struct page *mem_map; 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds EXPORT_SYMBOL(max_mapnr); 701da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map); 711da177e4SLinus Torvalds #endif 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds unsigned long num_physpages; 741da177e4SLinus Torvalds /* 751da177e4SLinus Torvalds * A number of key systems in x86 including ioremap() rely on the assumption 761da177e4SLinus Torvalds * that high_memory defines the upper bound on direct map memory, then end 771da177e4SLinus Torvalds * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and 781da177e4SLinus Torvalds * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL 791da177e4SLinus Torvalds * and ZONE_HIGHMEM. 801da177e4SLinus Torvalds */ 811da177e4SLinus Torvalds void * high_memory; 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds EXPORT_SYMBOL(num_physpages); 841da177e4SLinus Torvalds EXPORT_SYMBOL(high_memory); 851da177e4SLinus Torvalds 8632a93233SIngo Molnar /* 8732a93233SIngo Molnar * Randomize the address space (stacks, mmaps, brk, etc.). 8832a93233SIngo Molnar * 8932a93233SIngo Molnar * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization, 9032a93233SIngo Molnar * as ancient (libc5 based) binaries can segfault. ) 9132a93233SIngo Molnar */ 9232a93233SIngo Molnar int randomize_va_space __read_mostly = 9332a93233SIngo Molnar #ifdef CONFIG_COMPAT_BRK 9432a93233SIngo Molnar 1; 9532a93233SIngo Molnar #else 9632a93233SIngo Molnar 2; 9732a93233SIngo Molnar #endif 98a62eaf15SAndi Kleen 99a62eaf15SAndi Kleen static int __init disable_randmaps(char *s) 100a62eaf15SAndi Kleen { 101a62eaf15SAndi Kleen randomize_va_space = 0; 1029b41046cSOGAWA Hirofumi return 1; 103a62eaf15SAndi Kleen } 104a62eaf15SAndi Kleen __setup("norandmaps", disable_randmaps); 105a62eaf15SAndi Kleen 106a62eaf15SAndi Kleen 1071da177e4SLinus Torvalds /* 1081da177e4SLinus Torvalds * If a p?d_bad entry is found while walking page tables, report 1091da177e4SLinus Torvalds * the error, before resetting entry to p?d_none. Usually (but 1101da177e4SLinus Torvalds * very seldom) called out from the p?d_none_or_clear_bad macros. 1111da177e4SLinus Torvalds */ 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds void pgd_clear_bad(pgd_t *pgd) 1141da177e4SLinus Torvalds { 1151da177e4SLinus Torvalds pgd_ERROR(*pgd); 1161da177e4SLinus Torvalds pgd_clear(pgd); 1171da177e4SLinus Torvalds } 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds void pud_clear_bad(pud_t *pud) 1201da177e4SLinus Torvalds { 1211da177e4SLinus Torvalds pud_ERROR(*pud); 1221da177e4SLinus Torvalds pud_clear(pud); 1231da177e4SLinus Torvalds } 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds void pmd_clear_bad(pmd_t *pmd) 1261da177e4SLinus Torvalds { 1271da177e4SLinus Torvalds pmd_ERROR(*pmd); 1281da177e4SLinus Torvalds pmd_clear(pmd); 1291da177e4SLinus Torvalds } 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds /* 1321da177e4SLinus Torvalds * Note: this doesn't free the actual pages themselves. That 1331da177e4SLinus Torvalds * has been handled earlier when unmapping all the memory regions. 1341da177e4SLinus Torvalds */ 135e0da382cSHugh Dickins static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd) 1361da177e4SLinus Torvalds { 1372f569afdSMartin Schwidefsky pgtable_t token = pmd_pgtable(*pmd); 1381da177e4SLinus Torvalds pmd_clear(pmd); 1392f569afdSMartin Schwidefsky pte_free_tlb(tlb, token); 1401da177e4SLinus Torvalds tlb->mm->nr_ptes--; 1411da177e4SLinus Torvalds } 1421da177e4SLinus Torvalds 143e0da382cSHugh Dickins static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud, 144e0da382cSHugh Dickins unsigned long addr, unsigned long end, 145e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 1461da177e4SLinus Torvalds { 1471da177e4SLinus Torvalds pmd_t *pmd; 1481da177e4SLinus Torvalds unsigned long next; 149e0da382cSHugh Dickins unsigned long start; 1501da177e4SLinus Torvalds 151e0da382cSHugh Dickins start = addr; 1521da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 1531da177e4SLinus Torvalds do { 1541da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 1551da177e4SLinus Torvalds if (pmd_none_or_clear_bad(pmd)) 1561da177e4SLinus Torvalds continue; 157e0da382cSHugh Dickins free_pte_range(tlb, pmd); 1581da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 1591da177e4SLinus Torvalds 160e0da382cSHugh Dickins start &= PUD_MASK; 161e0da382cSHugh Dickins if (start < floor) 162e0da382cSHugh Dickins return; 163e0da382cSHugh Dickins if (ceiling) { 164e0da382cSHugh Dickins ceiling &= PUD_MASK; 165e0da382cSHugh Dickins if (!ceiling) 166e0da382cSHugh Dickins return; 1671da177e4SLinus Torvalds } 168e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 169e0da382cSHugh Dickins return; 170e0da382cSHugh Dickins 171e0da382cSHugh Dickins pmd = pmd_offset(pud, start); 172e0da382cSHugh Dickins pud_clear(pud); 173e0da382cSHugh Dickins pmd_free_tlb(tlb, pmd); 1741da177e4SLinus Torvalds } 1751da177e4SLinus Torvalds 176e0da382cSHugh Dickins static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd, 177e0da382cSHugh Dickins unsigned long addr, unsigned long end, 178e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 1791da177e4SLinus Torvalds { 1801da177e4SLinus Torvalds pud_t *pud; 1811da177e4SLinus Torvalds unsigned long next; 182e0da382cSHugh Dickins unsigned long start; 1831da177e4SLinus Torvalds 184e0da382cSHugh Dickins start = addr; 1851da177e4SLinus Torvalds pud = pud_offset(pgd, addr); 1861da177e4SLinus Torvalds do { 1871da177e4SLinus Torvalds next = pud_addr_end(addr, end); 1881da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 1891da177e4SLinus Torvalds continue; 190e0da382cSHugh Dickins free_pmd_range(tlb, pud, addr, next, floor, ceiling); 1911da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 1921da177e4SLinus Torvalds 193e0da382cSHugh Dickins start &= PGDIR_MASK; 194e0da382cSHugh Dickins if (start < floor) 195e0da382cSHugh Dickins return; 196e0da382cSHugh Dickins if (ceiling) { 197e0da382cSHugh Dickins ceiling &= PGDIR_MASK; 198e0da382cSHugh Dickins if (!ceiling) 199e0da382cSHugh Dickins return; 2001da177e4SLinus Torvalds } 201e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 202e0da382cSHugh Dickins return; 203e0da382cSHugh Dickins 204e0da382cSHugh Dickins pud = pud_offset(pgd, start); 205e0da382cSHugh Dickins pgd_clear(pgd); 206e0da382cSHugh Dickins pud_free_tlb(tlb, pud); 2071da177e4SLinus Torvalds } 2081da177e4SLinus Torvalds 2091da177e4SLinus Torvalds /* 210e0da382cSHugh Dickins * This function frees user-level page tables of a process. 211e0da382cSHugh Dickins * 2121da177e4SLinus Torvalds * Must be called with pagetable lock held. 2131da177e4SLinus Torvalds */ 2143bf5ee95SHugh Dickins void free_pgd_range(struct mmu_gather **tlb, 215e0da382cSHugh Dickins unsigned long addr, unsigned long end, 216e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 2171da177e4SLinus Torvalds { 2181da177e4SLinus Torvalds pgd_t *pgd; 2191da177e4SLinus Torvalds unsigned long next; 220e0da382cSHugh Dickins unsigned long start; 2211da177e4SLinus Torvalds 222e0da382cSHugh Dickins /* 223e0da382cSHugh Dickins * The next few lines have given us lots of grief... 224e0da382cSHugh Dickins * 225e0da382cSHugh Dickins * Why are we testing PMD* at this top level? Because often 226e0da382cSHugh Dickins * there will be no work to do at all, and we'd prefer not to 227e0da382cSHugh Dickins * go all the way down to the bottom just to discover that. 228e0da382cSHugh Dickins * 229e0da382cSHugh Dickins * Why all these "- 1"s? Because 0 represents both the bottom 230e0da382cSHugh Dickins * of the address space and the top of it (using -1 for the 231e0da382cSHugh Dickins * top wouldn't help much: the masks would do the wrong thing). 232e0da382cSHugh Dickins * The rule is that addr 0 and floor 0 refer to the bottom of 233e0da382cSHugh Dickins * the address space, but end 0 and ceiling 0 refer to the top 234e0da382cSHugh Dickins * Comparisons need to use "end - 1" and "ceiling - 1" (though 235e0da382cSHugh Dickins * that end 0 case should be mythical). 236e0da382cSHugh Dickins * 237e0da382cSHugh Dickins * Wherever addr is brought up or ceiling brought down, we must 238e0da382cSHugh Dickins * be careful to reject "the opposite 0" before it confuses the 239e0da382cSHugh Dickins * subsequent tests. But what about where end is brought down 240e0da382cSHugh Dickins * by PMD_SIZE below? no, end can't go down to 0 there. 241e0da382cSHugh Dickins * 242e0da382cSHugh Dickins * Whereas we round start (addr) and ceiling down, by different 243e0da382cSHugh Dickins * masks at different levels, in order to test whether a table 244e0da382cSHugh Dickins * now has no other vmas using it, so can be freed, we don't 245e0da382cSHugh Dickins * bother to round floor or end up - the tests don't need that. 246e0da382cSHugh Dickins */ 247e0da382cSHugh Dickins 248e0da382cSHugh Dickins addr &= PMD_MASK; 249e0da382cSHugh Dickins if (addr < floor) { 250e0da382cSHugh Dickins addr += PMD_SIZE; 251e0da382cSHugh Dickins if (!addr) 252e0da382cSHugh Dickins return; 253e0da382cSHugh Dickins } 254e0da382cSHugh Dickins if (ceiling) { 255e0da382cSHugh Dickins ceiling &= PMD_MASK; 256e0da382cSHugh Dickins if (!ceiling) 257e0da382cSHugh Dickins return; 258e0da382cSHugh Dickins } 259e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 260e0da382cSHugh Dickins end -= PMD_SIZE; 261e0da382cSHugh Dickins if (addr > end - 1) 262e0da382cSHugh Dickins return; 263e0da382cSHugh Dickins 264e0da382cSHugh Dickins start = addr; 2653bf5ee95SHugh Dickins pgd = pgd_offset((*tlb)->mm, addr); 2661da177e4SLinus Torvalds do { 2671da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 2681da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 2691da177e4SLinus Torvalds continue; 2703bf5ee95SHugh Dickins free_pud_range(*tlb, pgd, addr, next, floor, ceiling); 2711da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 272e0da382cSHugh Dickins } 273e0da382cSHugh Dickins 274e0da382cSHugh Dickins void free_pgtables(struct mmu_gather **tlb, struct vm_area_struct *vma, 275e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 276e0da382cSHugh Dickins { 277e0da382cSHugh Dickins while (vma) { 278e0da382cSHugh Dickins struct vm_area_struct *next = vma->vm_next; 279e0da382cSHugh Dickins unsigned long addr = vma->vm_start; 280e0da382cSHugh Dickins 2818f4f8c16SHugh Dickins /* 2828f4f8c16SHugh Dickins * Hide vma from rmap and vmtruncate before freeing pgtables 2838f4f8c16SHugh Dickins */ 2848f4f8c16SHugh Dickins anon_vma_unlink(vma); 2858f4f8c16SHugh Dickins unlink_file_vma(vma); 2868f4f8c16SHugh Dickins 2879da61aefSDavid Gibson if (is_vm_hugetlb_page(vma)) { 2883bf5ee95SHugh Dickins hugetlb_free_pgd_range(tlb, addr, vma->vm_end, 2893bf5ee95SHugh Dickins floor, next? next->vm_start: ceiling); 2903bf5ee95SHugh Dickins } else { 2913bf5ee95SHugh Dickins /* 2923bf5ee95SHugh Dickins * Optimization: gather nearby vmas into one call down 2933bf5ee95SHugh Dickins */ 2943bf5ee95SHugh Dickins while (next && next->vm_start <= vma->vm_end + PMD_SIZE 2954866920bSDavid Gibson && !is_vm_hugetlb_page(next)) { 296e0da382cSHugh Dickins vma = next; 297e0da382cSHugh Dickins next = vma->vm_next; 2988f4f8c16SHugh Dickins anon_vma_unlink(vma); 2998f4f8c16SHugh Dickins unlink_file_vma(vma); 300e0da382cSHugh Dickins } 3013bf5ee95SHugh Dickins free_pgd_range(tlb, addr, vma->vm_end, 302e0da382cSHugh Dickins floor, next? next->vm_start: ceiling); 3033bf5ee95SHugh Dickins } 304e0da382cSHugh Dickins vma = next; 305e0da382cSHugh Dickins } 3061da177e4SLinus Torvalds } 3071da177e4SLinus Torvalds 3081bb3630eSHugh Dickins int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address) 3091da177e4SLinus Torvalds { 3102f569afdSMartin Schwidefsky pgtable_t new = pte_alloc_one(mm, address); 3111da177e4SLinus Torvalds if (!new) 3121bb3630eSHugh Dickins return -ENOMEM; 3131bb3630eSHugh Dickins 314c74df32cSHugh Dickins spin_lock(&mm->page_table_lock); 3152f569afdSMartin Schwidefsky if (!pmd_present(*pmd)) { /* Has another populated it ? */ 3161da177e4SLinus Torvalds mm->nr_ptes++; 3171da177e4SLinus Torvalds pmd_populate(mm, pmd, new); 3182f569afdSMartin Schwidefsky new = NULL; 3191da177e4SLinus Torvalds } 320c74df32cSHugh Dickins spin_unlock(&mm->page_table_lock); 3212f569afdSMartin Schwidefsky if (new) 3222f569afdSMartin Schwidefsky pte_free(mm, new); 3231bb3630eSHugh Dickins return 0; 3241da177e4SLinus Torvalds } 3251da177e4SLinus Torvalds 3261bb3630eSHugh Dickins int __pte_alloc_kernel(pmd_t *pmd, unsigned long address) 3271da177e4SLinus Torvalds { 3281bb3630eSHugh Dickins pte_t *new = pte_alloc_one_kernel(&init_mm, address); 3291da177e4SLinus Torvalds if (!new) 3301bb3630eSHugh Dickins return -ENOMEM; 3311da177e4SLinus Torvalds 332872fec16SHugh Dickins spin_lock(&init_mm.page_table_lock); 3332f569afdSMartin Schwidefsky if (!pmd_present(*pmd)) { /* Has another populated it ? */ 334872fec16SHugh Dickins pmd_populate_kernel(&init_mm, pmd, new); 3352f569afdSMartin Schwidefsky new = NULL; 3362f569afdSMartin Schwidefsky } 337872fec16SHugh Dickins spin_unlock(&init_mm.page_table_lock); 3382f569afdSMartin Schwidefsky if (new) 3392f569afdSMartin Schwidefsky pte_free_kernel(&init_mm, new); 3401bb3630eSHugh Dickins return 0; 3411da177e4SLinus Torvalds } 3421da177e4SLinus Torvalds 343ae859762SHugh Dickins static inline void add_mm_rss(struct mm_struct *mm, int file_rss, int anon_rss) 344ae859762SHugh Dickins { 345ae859762SHugh Dickins if (file_rss) 346ae859762SHugh Dickins add_mm_counter(mm, file_rss, file_rss); 347ae859762SHugh Dickins if (anon_rss) 348ae859762SHugh Dickins add_mm_counter(mm, anon_rss, anon_rss); 349ae859762SHugh Dickins } 350ae859762SHugh Dickins 3511da177e4SLinus Torvalds /* 3526aab341eSLinus Torvalds * This function is called to print an error when a bad pte 3536aab341eSLinus Torvalds * is found. For example, we might have a PFN-mapped pte in 3546aab341eSLinus Torvalds * a region that doesn't allow it. 355b5810039SNick Piggin * 356b5810039SNick Piggin * The calling function must still handle the error. 357b5810039SNick Piggin */ 358b5810039SNick Piggin void print_bad_pte(struct vm_area_struct *vma, pte_t pte, unsigned long vaddr) 359b5810039SNick Piggin { 360b5810039SNick Piggin printk(KERN_ERR "Bad pte = %08llx, process = %s, " 361b5810039SNick Piggin "vm_flags = %lx, vaddr = %lx\n", 362b5810039SNick Piggin (long long)pte_val(pte), 363b5810039SNick Piggin (vma->vm_mm == current->mm ? current->comm : "???"), 364b5810039SNick Piggin vma->vm_flags, vaddr); 365b5810039SNick Piggin dump_stack(); 366b5810039SNick Piggin } 367b5810039SNick Piggin 36867121172SLinus Torvalds static inline int is_cow_mapping(unsigned int flags) 36967121172SLinus Torvalds { 37067121172SLinus Torvalds return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; 37167121172SLinus Torvalds } 37267121172SLinus Torvalds 373b5810039SNick Piggin /* 374*b379d790SJared Hulbert * This function gets the "struct page" associated with a pte or returns 375*b379d790SJared Hulbert * NULL if no "struct page" is associated with the pte. 3766aab341eSLinus Torvalds * 377*b379d790SJared Hulbert * A raw VM_PFNMAP mapping (ie. one that is not COWed) may not have any "struct 378*b379d790SJared Hulbert * page" backing, and even if they do, they are not refcounted. COWed pages of 379*b379d790SJared Hulbert * a VM_PFNMAP do always have a struct page, and they are normally refcounted 380*b379d790SJared Hulbert * (they are _normal_ pages). 381*b379d790SJared Hulbert * 382*b379d790SJared Hulbert * So a raw PFNMAP mapping will have each page table entry just pointing 383*b379d790SJared Hulbert * to a page frame number, and as far as the VM layer is concerned, those do 384*b379d790SJared Hulbert * not have pages associated with them - even if the PFN might point to memory 3856aab341eSLinus Torvalds * that otherwise is perfectly fine and has a "struct page". 3866aab341eSLinus Torvalds * 387*b379d790SJared Hulbert * The way we recognize COWed pages within VM_PFNMAP mappings is through the 388*b379d790SJared Hulbert * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit 389*b379d790SJared Hulbert * set, and the vm_pgoff will point to the first PFN mapped: thus every 3906aab341eSLinus Torvalds * page that is a raw mapping will always honor the rule 3916aab341eSLinus Torvalds * 3926aab341eSLinus Torvalds * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT) 3936aab341eSLinus Torvalds * 394*b379d790SJared Hulbert * A call to vm_normal_page() will return NULL for such a page. 395*b379d790SJared Hulbert * 396*b379d790SJared Hulbert * If the page doesn't follow the "remap_pfn_range()" rule in a VM_PFNMAP 397*b379d790SJared Hulbert * then the page has been COW'ed. A COW'ed page _does_ have a "struct page" 398*b379d790SJared Hulbert * associated with it even if it is in a VM_PFNMAP range. Calling 399*b379d790SJared Hulbert * vm_normal_page() on such a page will therefore return the "struct page". 400*b379d790SJared Hulbert * 401*b379d790SJared Hulbert * 402*b379d790SJared Hulbert * VM_MIXEDMAP mappings can likewise contain memory with or without "struct 403*b379d790SJared Hulbert * page" backing, however the difference is that _all_ pages with a struct 404*b379d790SJared Hulbert * page (that is, those where pfn_valid is true) are refcounted and considered 405*b379d790SJared Hulbert * normal pages by the VM. The disadvantage is that pages are refcounted 406*b379d790SJared Hulbert * (which can be slower and simply not an option for some PFNMAP users). The 407*b379d790SJared Hulbert * advantage is that we don't have to follow the strict linearity rule of 408*b379d790SJared Hulbert * PFNMAP mappings in order to support COWable mappings. 409*b379d790SJared Hulbert * 410*b379d790SJared Hulbert * A call to vm_normal_page() with a VM_MIXEDMAP mapping will return the 411*b379d790SJared Hulbert * associated "struct page" or NULL for memory not backed by a "struct page". 412*b379d790SJared Hulbert * 413*b379d790SJared Hulbert * 414*b379d790SJared Hulbert * All other mappings should have a valid struct page, which will be 415*b379d790SJared Hulbert * returned by a call to vm_normal_page(). 416ee498ed7SHugh Dickins */ 4176aab341eSLinus Torvalds struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, pte_t pte) 418ee498ed7SHugh Dickins { 4196aab341eSLinus Torvalds unsigned long pfn = pte_pfn(pte); 4206aab341eSLinus Torvalds 421*b379d790SJared Hulbert if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { 422*b379d790SJared Hulbert if (vma->vm_flags & VM_MIXEDMAP) { 423*b379d790SJared Hulbert if (!pfn_valid(pfn)) 424*b379d790SJared Hulbert return NULL; 425*b379d790SJared Hulbert goto out; 426*b379d790SJared Hulbert } else { 4276aab341eSLinus Torvalds unsigned long off = (addr-vma->vm_start) >> PAGE_SHIFT; 4286aab341eSLinus Torvalds if (pfn == vma->vm_pgoff + off) 4296aab341eSLinus Torvalds return NULL; 43067121172SLinus Torvalds if (!is_cow_mapping(vma->vm_flags)) 431fb155c16SLinus Torvalds return NULL; 4326aab341eSLinus Torvalds } 433*b379d790SJared Hulbert } 4346aab341eSLinus Torvalds 4359723198cSCarsten Otte #ifdef CONFIG_DEBUG_VM 436315ab19aSNick Piggin /* 437315ab19aSNick Piggin * Add some anal sanity checks for now. Eventually, 438315ab19aSNick Piggin * we should just do "return pfn_to_page(pfn)", but 439315ab19aSNick Piggin * in the meantime we check that we get a valid pfn, 440315ab19aSNick Piggin * and that the resulting page looks ok. 441315ab19aSNick Piggin */ 4426aab341eSLinus Torvalds if (unlikely(!pfn_valid(pfn))) { 4436aab341eSLinus Torvalds print_bad_pte(vma, pte, addr); 4446aab341eSLinus Torvalds return NULL; 4456aab341eSLinus Torvalds } 4469723198cSCarsten Otte #endif 4476aab341eSLinus Torvalds 4486aab341eSLinus Torvalds /* 4496aab341eSLinus Torvalds * NOTE! We still have PageReserved() pages in the page 4506aab341eSLinus Torvalds * tables. 4516aab341eSLinus Torvalds * 4526aab341eSLinus Torvalds * The PAGE_ZERO() pages and various VDSO mappings can 4536aab341eSLinus Torvalds * cause them to exist. 4546aab341eSLinus Torvalds */ 455*b379d790SJared Hulbert out: 4566aab341eSLinus Torvalds return pfn_to_page(pfn); 457ee498ed7SHugh Dickins } 458ee498ed7SHugh Dickins 459ee498ed7SHugh Dickins /* 4601da177e4SLinus Torvalds * copy one vm_area from one task to the other. Assumes the page tables 4611da177e4SLinus Torvalds * already present in the new task to be cleared in the whole range 4621da177e4SLinus Torvalds * covered by this vma. 4631da177e4SLinus Torvalds */ 4641da177e4SLinus Torvalds 4658c103762SHugh Dickins static inline void 4661da177e4SLinus Torvalds copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, 467b5810039SNick Piggin pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma, 4688c103762SHugh Dickins unsigned long addr, int *rss) 4691da177e4SLinus Torvalds { 470b5810039SNick Piggin unsigned long vm_flags = vma->vm_flags; 4711da177e4SLinus Torvalds pte_t pte = *src_pte; 4721da177e4SLinus Torvalds struct page *page; 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds /* pte contains position in swap or file, so copy. */ 4751da177e4SLinus Torvalds if (unlikely(!pte_present(pte))) { 4761da177e4SLinus Torvalds if (!pte_file(pte)) { 4770697212aSChristoph Lameter swp_entry_t entry = pte_to_swp_entry(pte); 4780697212aSChristoph Lameter 4790697212aSChristoph Lameter swap_duplicate(entry); 4801da177e4SLinus Torvalds /* make sure dst_mm is on swapoff's mmlist. */ 4811da177e4SLinus Torvalds if (unlikely(list_empty(&dst_mm->mmlist))) { 4821da177e4SLinus Torvalds spin_lock(&mmlist_lock); 483f412ac08SHugh Dickins if (list_empty(&dst_mm->mmlist)) 484f412ac08SHugh Dickins list_add(&dst_mm->mmlist, 485f412ac08SHugh Dickins &src_mm->mmlist); 4861da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 4871da177e4SLinus Torvalds } 4880697212aSChristoph Lameter if (is_write_migration_entry(entry) && 4890697212aSChristoph Lameter is_cow_mapping(vm_flags)) { 4900697212aSChristoph Lameter /* 4910697212aSChristoph Lameter * COW mappings require pages in both parent 4920697212aSChristoph Lameter * and child to be set to read. 4930697212aSChristoph Lameter */ 4940697212aSChristoph Lameter make_migration_entry_read(&entry); 4950697212aSChristoph Lameter pte = swp_entry_to_pte(entry); 4960697212aSChristoph Lameter set_pte_at(src_mm, addr, src_pte, pte); 4970697212aSChristoph Lameter } 4981da177e4SLinus Torvalds } 499ae859762SHugh Dickins goto out_set_pte; 5001da177e4SLinus Torvalds } 5011da177e4SLinus Torvalds 5021da177e4SLinus Torvalds /* 5031da177e4SLinus Torvalds * If it's a COW mapping, write protect it both 5041da177e4SLinus Torvalds * in the parent and the child 5051da177e4SLinus Torvalds */ 50667121172SLinus Torvalds if (is_cow_mapping(vm_flags)) { 5071da177e4SLinus Torvalds ptep_set_wrprotect(src_mm, addr, src_pte); 5083dc90795SZachary Amsden pte = pte_wrprotect(pte); 5091da177e4SLinus Torvalds } 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds /* 5121da177e4SLinus Torvalds * If it's a shared mapping, mark it clean in 5131da177e4SLinus Torvalds * the child 5141da177e4SLinus Torvalds */ 5151da177e4SLinus Torvalds if (vm_flags & VM_SHARED) 5161da177e4SLinus Torvalds pte = pte_mkclean(pte); 5171da177e4SLinus Torvalds pte = pte_mkold(pte); 5186aab341eSLinus Torvalds 5196aab341eSLinus Torvalds page = vm_normal_page(vma, addr, pte); 5206aab341eSLinus Torvalds if (page) { 5211da177e4SLinus Torvalds get_page(page); 522c97a9e10SNick Piggin page_dup_rmap(page, vma, addr); 5238c103762SHugh Dickins rss[!!PageAnon(page)]++; 5246aab341eSLinus Torvalds } 525ae859762SHugh Dickins 526ae859762SHugh Dickins out_set_pte: 527ae859762SHugh Dickins set_pte_at(dst_mm, addr, dst_pte, pte); 5281da177e4SLinus Torvalds } 5291da177e4SLinus Torvalds 5301da177e4SLinus Torvalds static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 5311da177e4SLinus Torvalds pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma, 5321da177e4SLinus Torvalds unsigned long addr, unsigned long end) 5331da177e4SLinus Torvalds { 5341da177e4SLinus Torvalds pte_t *src_pte, *dst_pte; 535c74df32cSHugh Dickins spinlock_t *src_ptl, *dst_ptl; 536e040f218SHugh Dickins int progress = 0; 5378c103762SHugh Dickins int rss[2]; 5381da177e4SLinus Torvalds 5391da177e4SLinus Torvalds again: 540ae859762SHugh Dickins rss[1] = rss[0] = 0; 541c74df32cSHugh Dickins dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl); 5421da177e4SLinus Torvalds if (!dst_pte) 5431da177e4SLinus Torvalds return -ENOMEM; 5441da177e4SLinus Torvalds src_pte = pte_offset_map_nested(src_pmd, addr); 5454c21e2f2SHugh Dickins src_ptl = pte_lockptr(src_mm, src_pmd); 546f20dc5f7SIngo Molnar spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 5476606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 5481da177e4SLinus Torvalds 5491da177e4SLinus Torvalds do { 5501da177e4SLinus Torvalds /* 5511da177e4SLinus Torvalds * We are holding two locks at this point - either of them 5521da177e4SLinus Torvalds * could generate latencies in another task on another CPU. 5531da177e4SLinus Torvalds */ 554e040f218SHugh Dickins if (progress >= 32) { 555e040f218SHugh Dickins progress = 0; 556e040f218SHugh Dickins if (need_resched() || 55795c354feSNick Piggin spin_needbreak(src_ptl) || spin_needbreak(dst_ptl)) 5581da177e4SLinus Torvalds break; 559e040f218SHugh Dickins } 5601da177e4SLinus Torvalds if (pte_none(*src_pte)) { 5611da177e4SLinus Torvalds progress++; 5621da177e4SLinus Torvalds continue; 5631da177e4SLinus Torvalds } 5648c103762SHugh Dickins copy_one_pte(dst_mm, src_mm, dst_pte, src_pte, vma, addr, rss); 5651da177e4SLinus Torvalds progress += 8; 5661da177e4SLinus Torvalds } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end); 5671da177e4SLinus Torvalds 5686606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 569c74df32cSHugh Dickins spin_unlock(src_ptl); 5701da177e4SLinus Torvalds pte_unmap_nested(src_pte - 1); 571ae859762SHugh Dickins add_mm_rss(dst_mm, rss[0], rss[1]); 572c74df32cSHugh Dickins pte_unmap_unlock(dst_pte - 1, dst_ptl); 573c74df32cSHugh Dickins cond_resched(); 5741da177e4SLinus Torvalds if (addr != end) 5751da177e4SLinus Torvalds goto again; 5761da177e4SLinus Torvalds return 0; 5771da177e4SLinus Torvalds } 5781da177e4SLinus Torvalds 5791da177e4SLinus Torvalds static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 5801da177e4SLinus Torvalds pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma, 5811da177e4SLinus Torvalds unsigned long addr, unsigned long end) 5821da177e4SLinus Torvalds { 5831da177e4SLinus Torvalds pmd_t *src_pmd, *dst_pmd; 5841da177e4SLinus Torvalds unsigned long next; 5851da177e4SLinus Torvalds 5861da177e4SLinus Torvalds dst_pmd = pmd_alloc(dst_mm, dst_pud, addr); 5871da177e4SLinus Torvalds if (!dst_pmd) 5881da177e4SLinus Torvalds return -ENOMEM; 5891da177e4SLinus Torvalds src_pmd = pmd_offset(src_pud, addr); 5901da177e4SLinus Torvalds do { 5911da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 5921da177e4SLinus Torvalds if (pmd_none_or_clear_bad(src_pmd)) 5931da177e4SLinus Torvalds continue; 5941da177e4SLinus Torvalds if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd, 5951da177e4SLinus Torvalds vma, addr, next)) 5961da177e4SLinus Torvalds return -ENOMEM; 5971da177e4SLinus Torvalds } while (dst_pmd++, src_pmd++, addr = next, addr != end); 5981da177e4SLinus Torvalds return 0; 5991da177e4SLinus Torvalds } 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 6021da177e4SLinus Torvalds pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma, 6031da177e4SLinus Torvalds unsigned long addr, unsigned long end) 6041da177e4SLinus Torvalds { 6051da177e4SLinus Torvalds pud_t *src_pud, *dst_pud; 6061da177e4SLinus Torvalds unsigned long next; 6071da177e4SLinus Torvalds 6081da177e4SLinus Torvalds dst_pud = pud_alloc(dst_mm, dst_pgd, addr); 6091da177e4SLinus Torvalds if (!dst_pud) 6101da177e4SLinus Torvalds return -ENOMEM; 6111da177e4SLinus Torvalds src_pud = pud_offset(src_pgd, addr); 6121da177e4SLinus Torvalds do { 6131da177e4SLinus Torvalds next = pud_addr_end(addr, end); 6141da177e4SLinus Torvalds if (pud_none_or_clear_bad(src_pud)) 6151da177e4SLinus Torvalds continue; 6161da177e4SLinus Torvalds if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud, 6171da177e4SLinus Torvalds vma, addr, next)) 6181da177e4SLinus Torvalds return -ENOMEM; 6191da177e4SLinus Torvalds } while (dst_pud++, src_pud++, addr = next, addr != end); 6201da177e4SLinus Torvalds return 0; 6211da177e4SLinus Torvalds } 6221da177e4SLinus Torvalds 6231da177e4SLinus Torvalds int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 6241da177e4SLinus Torvalds struct vm_area_struct *vma) 6251da177e4SLinus Torvalds { 6261da177e4SLinus Torvalds pgd_t *src_pgd, *dst_pgd; 6271da177e4SLinus Torvalds unsigned long next; 6281da177e4SLinus Torvalds unsigned long addr = vma->vm_start; 6291da177e4SLinus Torvalds unsigned long end = vma->vm_end; 6301da177e4SLinus Torvalds 631d992895bSNick Piggin /* 632d992895bSNick Piggin * Don't copy ptes where a page fault will fill them correctly. 633d992895bSNick Piggin * Fork becomes much lighter when there are big shared or private 634d992895bSNick Piggin * readonly mappings. The tradeoff is that copy_page_range is more 635d992895bSNick Piggin * efficient than faulting. 636d992895bSNick Piggin */ 6374d7672b4SLinus Torvalds if (!(vma->vm_flags & (VM_HUGETLB|VM_NONLINEAR|VM_PFNMAP|VM_INSERTPAGE))) { 638d992895bSNick Piggin if (!vma->anon_vma) 639d992895bSNick Piggin return 0; 640d992895bSNick Piggin } 641d992895bSNick Piggin 6421da177e4SLinus Torvalds if (is_vm_hugetlb_page(vma)) 6431da177e4SLinus Torvalds return copy_hugetlb_page_range(dst_mm, src_mm, vma); 6441da177e4SLinus Torvalds 6451da177e4SLinus Torvalds dst_pgd = pgd_offset(dst_mm, addr); 6461da177e4SLinus Torvalds src_pgd = pgd_offset(src_mm, addr); 6471da177e4SLinus Torvalds do { 6481da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 6491da177e4SLinus Torvalds if (pgd_none_or_clear_bad(src_pgd)) 6501da177e4SLinus Torvalds continue; 6511da177e4SLinus Torvalds if (copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd, 6521da177e4SLinus Torvalds vma, addr, next)) 6531da177e4SLinus Torvalds return -ENOMEM; 6541da177e4SLinus Torvalds } while (dst_pgd++, src_pgd++, addr = next, addr != end); 6551da177e4SLinus Torvalds return 0; 6561da177e4SLinus Torvalds } 6571da177e4SLinus Torvalds 65851c6f666SRobin Holt static unsigned long zap_pte_range(struct mmu_gather *tlb, 659b5810039SNick Piggin struct vm_area_struct *vma, pmd_t *pmd, 6601da177e4SLinus Torvalds unsigned long addr, unsigned long end, 66151c6f666SRobin Holt long *zap_work, struct zap_details *details) 6621da177e4SLinus Torvalds { 663b5810039SNick Piggin struct mm_struct *mm = tlb->mm; 6641da177e4SLinus Torvalds pte_t *pte; 665508034a3SHugh Dickins spinlock_t *ptl; 666ae859762SHugh Dickins int file_rss = 0; 667ae859762SHugh Dickins int anon_rss = 0; 6681da177e4SLinus Torvalds 669508034a3SHugh Dickins pte = pte_offset_map_lock(mm, pmd, addr, &ptl); 6706606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 6711da177e4SLinus Torvalds do { 6721da177e4SLinus Torvalds pte_t ptent = *pte; 67351c6f666SRobin Holt if (pte_none(ptent)) { 67451c6f666SRobin Holt (*zap_work)--; 6751da177e4SLinus Torvalds continue; 67651c6f666SRobin Holt } 67751c6f666SRobin Holt 67851c6f666SRobin Holt (*zap_work) -= PAGE_SIZE; 67951c6f666SRobin Holt 6806f5e6b9eSHugh Dickins if (pte_present(ptent)) { 6816f5e6b9eSHugh Dickins struct page *page; 6826f5e6b9eSHugh Dickins 6836aab341eSLinus Torvalds page = vm_normal_page(vma, addr, ptent); 6841da177e4SLinus Torvalds if (unlikely(details) && page) { 6851da177e4SLinus Torvalds /* 6861da177e4SLinus Torvalds * unmap_shared_mapping_pages() wants to 6871da177e4SLinus Torvalds * invalidate cache without truncating: 6881da177e4SLinus Torvalds * unmap shared but keep private pages. 6891da177e4SLinus Torvalds */ 6901da177e4SLinus Torvalds if (details->check_mapping && 6911da177e4SLinus Torvalds details->check_mapping != page->mapping) 6921da177e4SLinus Torvalds continue; 6931da177e4SLinus Torvalds /* 6941da177e4SLinus Torvalds * Each page->index must be checked when 6951da177e4SLinus Torvalds * invalidating or truncating nonlinear. 6961da177e4SLinus Torvalds */ 6971da177e4SLinus Torvalds if (details->nonlinear_vma && 6981da177e4SLinus Torvalds (page->index < details->first_index || 6991da177e4SLinus Torvalds page->index > details->last_index)) 7001da177e4SLinus Torvalds continue; 7011da177e4SLinus Torvalds } 702b5810039SNick Piggin ptent = ptep_get_and_clear_full(mm, addr, pte, 703a600388dSZachary Amsden tlb->fullmm); 7041da177e4SLinus Torvalds tlb_remove_tlb_entry(tlb, pte, addr); 7051da177e4SLinus Torvalds if (unlikely(!page)) 7061da177e4SLinus Torvalds continue; 7071da177e4SLinus Torvalds if (unlikely(details) && details->nonlinear_vma 7081da177e4SLinus Torvalds && linear_page_index(details->nonlinear_vma, 7091da177e4SLinus Torvalds addr) != page->index) 710b5810039SNick Piggin set_pte_at(mm, addr, pte, 7111da177e4SLinus Torvalds pgoff_to_pte(page->index)); 7121da177e4SLinus Torvalds if (PageAnon(page)) 71386d912f4SHugh Dickins anon_rss--; 7146237bcd9SHugh Dickins else { 7156237bcd9SHugh Dickins if (pte_dirty(ptent)) 7166237bcd9SHugh Dickins set_page_dirty(page); 7176237bcd9SHugh Dickins if (pte_young(ptent)) 718daa88c8dSKen Chen SetPageReferenced(page); 71986d912f4SHugh Dickins file_rss--; 7206237bcd9SHugh Dickins } 7217de6b805SNick Piggin page_remove_rmap(page, vma); 7221da177e4SLinus Torvalds tlb_remove_page(tlb, page); 7231da177e4SLinus Torvalds continue; 7241da177e4SLinus Torvalds } 7251da177e4SLinus Torvalds /* 7261da177e4SLinus Torvalds * If details->check_mapping, we leave swap entries; 7271da177e4SLinus Torvalds * if details->nonlinear_vma, we leave file entries. 7281da177e4SLinus Torvalds */ 7291da177e4SLinus Torvalds if (unlikely(details)) 7301da177e4SLinus Torvalds continue; 7311da177e4SLinus Torvalds if (!pte_file(ptent)) 7321da177e4SLinus Torvalds free_swap_and_cache(pte_to_swp_entry(ptent)); 7339888a1caSZachary Amsden pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); 73451c6f666SRobin Holt } while (pte++, addr += PAGE_SIZE, (addr != end && *zap_work > 0)); 735ae859762SHugh Dickins 73686d912f4SHugh Dickins add_mm_rss(mm, file_rss, anon_rss); 7376606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 738508034a3SHugh Dickins pte_unmap_unlock(pte - 1, ptl); 73951c6f666SRobin Holt 74051c6f666SRobin Holt return addr; 7411da177e4SLinus Torvalds } 7421da177e4SLinus Torvalds 74351c6f666SRobin Holt static inline unsigned long zap_pmd_range(struct mmu_gather *tlb, 744b5810039SNick Piggin struct vm_area_struct *vma, pud_t *pud, 7451da177e4SLinus Torvalds unsigned long addr, unsigned long end, 74651c6f666SRobin Holt long *zap_work, struct zap_details *details) 7471da177e4SLinus Torvalds { 7481da177e4SLinus Torvalds pmd_t *pmd; 7491da177e4SLinus Torvalds unsigned long next; 7501da177e4SLinus Torvalds 7511da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 7521da177e4SLinus Torvalds do { 7531da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 75451c6f666SRobin Holt if (pmd_none_or_clear_bad(pmd)) { 75551c6f666SRobin Holt (*zap_work)--; 7561da177e4SLinus Torvalds continue; 75751c6f666SRobin Holt } 75851c6f666SRobin Holt next = zap_pte_range(tlb, vma, pmd, addr, next, 75951c6f666SRobin Holt zap_work, details); 76051c6f666SRobin Holt } while (pmd++, addr = next, (addr != end && *zap_work > 0)); 76151c6f666SRobin Holt 76251c6f666SRobin Holt return addr; 7631da177e4SLinus Torvalds } 7641da177e4SLinus Torvalds 76551c6f666SRobin Holt static inline unsigned long zap_pud_range(struct mmu_gather *tlb, 766b5810039SNick Piggin struct vm_area_struct *vma, pgd_t *pgd, 7671da177e4SLinus Torvalds unsigned long addr, unsigned long end, 76851c6f666SRobin Holt long *zap_work, struct zap_details *details) 7691da177e4SLinus Torvalds { 7701da177e4SLinus Torvalds pud_t *pud; 7711da177e4SLinus Torvalds unsigned long next; 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds pud = pud_offset(pgd, addr); 7741da177e4SLinus Torvalds do { 7751da177e4SLinus Torvalds next = pud_addr_end(addr, end); 77651c6f666SRobin Holt if (pud_none_or_clear_bad(pud)) { 77751c6f666SRobin Holt (*zap_work)--; 7781da177e4SLinus Torvalds continue; 77951c6f666SRobin Holt } 78051c6f666SRobin Holt next = zap_pmd_range(tlb, vma, pud, addr, next, 78151c6f666SRobin Holt zap_work, details); 78251c6f666SRobin Holt } while (pud++, addr = next, (addr != end && *zap_work > 0)); 78351c6f666SRobin Holt 78451c6f666SRobin Holt return addr; 7851da177e4SLinus Torvalds } 7861da177e4SLinus Torvalds 78751c6f666SRobin Holt static unsigned long unmap_page_range(struct mmu_gather *tlb, 78851c6f666SRobin Holt struct vm_area_struct *vma, 7891da177e4SLinus Torvalds unsigned long addr, unsigned long end, 79051c6f666SRobin Holt long *zap_work, struct zap_details *details) 7911da177e4SLinus Torvalds { 7921da177e4SLinus Torvalds pgd_t *pgd; 7931da177e4SLinus Torvalds unsigned long next; 7941da177e4SLinus Torvalds 7951da177e4SLinus Torvalds if (details && !details->check_mapping && !details->nonlinear_vma) 7961da177e4SLinus Torvalds details = NULL; 7971da177e4SLinus Torvalds 7981da177e4SLinus Torvalds BUG_ON(addr >= end); 7991da177e4SLinus Torvalds tlb_start_vma(tlb, vma); 8001da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 8011da177e4SLinus Torvalds do { 8021da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 80351c6f666SRobin Holt if (pgd_none_or_clear_bad(pgd)) { 80451c6f666SRobin Holt (*zap_work)--; 8051da177e4SLinus Torvalds continue; 80651c6f666SRobin Holt } 80751c6f666SRobin Holt next = zap_pud_range(tlb, vma, pgd, addr, next, 80851c6f666SRobin Holt zap_work, details); 80951c6f666SRobin Holt } while (pgd++, addr = next, (addr != end && *zap_work > 0)); 8101da177e4SLinus Torvalds tlb_end_vma(tlb, vma); 81151c6f666SRobin Holt 81251c6f666SRobin Holt return addr; 8131da177e4SLinus Torvalds } 8141da177e4SLinus Torvalds 8151da177e4SLinus Torvalds #ifdef CONFIG_PREEMPT 8161da177e4SLinus Torvalds # define ZAP_BLOCK_SIZE (8 * PAGE_SIZE) 8171da177e4SLinus Torvalds #else 8181da177e4SLinus Torvalds /* No preempt: go for improved straight-line efficiency */ 8191da177e4SLinus Torvalds # define ZAP_BLOCK_SIZE (1024 * PAGE_SIZE) 8201da177e4SLinus Torvalds #endif 8211da177e4SLinus Torvalds 8221da177e4SLinus Torvalds /** 8231da177e4SLinus Torvalds * unmap_vmas - unmap a range of memory covered by a list of vma's 8241da177e4SLinus Torvalds * @tlbp: address of the caller's struct mmu_gather 8251da177e4SLinus Torvalds * @vma: the starting vma 8261da177e4SLinus Torvalds * @start_addr: virtual address at which to start unmapping 8271da177e4SLinus Torvalds * @end_addr: virtual address at which to end unmapping 8281da177e4SLinus Torvalds * @nr_accounted: Place number of unmapped pages in vm-accountable vma's here 8291da177e4SLinus Torvalds * @details: details of nonlinear truncation or shared cache invalidation 8301da177e4SLinus Torvalds * 831ee39b37bSHugh Dickins * Returns the end address of the unmapping (restart addr if interrupted). 8321da177e4SLinus Torvalds * 833508034a3SHugh Dickins * Unmap all pages in the vma list. 8341da177e4SLinus Torvalds * 835508034a3SHugh Dickins * We aim to not hold locks for too long (for scheduling latency reasons). 836508034a3SHugh Dickins * So zap pages in ZAP_BLOCK_SIZE bytecounts. This means we need to 8371da177e4SLinus Torvalds * return the ending mmu_gather to the caller. 8381da177e4SLinus Torvalds * 8391da177e4SLinus Torvalds * Only addresses between `start' and `end' will be unmapped. 8401da177e4SLinus Torvalds * 8411da177e4SLinus Torvalds * The VMA list must be sorted in ascending virtual address order. 8421da177e4SLinus Torvalds * 8431da177e4SLinus Torvalds * unmap_vmas() assumes that the caller will flush the whole unmapped address 8441da177e4SLinus Torvalds * range after unmap_vmas() returns. So the only responsibility here is to 8451da177e4SLinus Torvalds * ensure that any thus-far unmapped pages are flushed before unmap_vmas() 8461da177e4SLinus Torvalds * drops the lock and schedules. 8471da177e4SLinus Torvalds */ 848508034a3SHugh Dickins unsigned long unmap_vmas(struct mmu_gather **tlbp, 8491da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long start_addr, 8501da177e4SLinus Torvalds unsigned long end_addr, unsigned long *nr_accounted, 8511da177e4SLinus Torvalds struct zap_details *details) 8521da177e4SLinus Torvalds { 85351c6f666SRobin Holt long zap_work = ZAP_BLOCK_SIZE; 8541da177e4SLinus Torvalds unsigned long tlb_start = 0; /* For tlb_finish_mmu */ 8551da177e4SLinus Torvalds int tlb_start_valid = 0; 856ee39b37bSHugh Dickins unsigned long start = start_addr; 8571da177e4SLinus Torvalds spinlock_t *i_mmap_lock = details? details->i_mmap_lock: NULL; 8584d6ddfa9SHugh Dickins int fullmm = (*tlbp)->fullmm; 8591da177e4SLinus Torvalds 8601da177e4SLinus Torvalds for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) { 8611da177e4SLinus Torvalds unsigned long end; 8621da177e4SLinus Torvalds 8631da177e4SLinus Torvalds start = max(vma->vm_start, start_addr); 8641da177e4SLinus Torvalds if (start >= vma->vm_end) 8651da177e4SLinus Torvalds continue; 8661da177e4SLinus Torvalds end = min(vma->vm_end, end_addr); 8671da177e4SLinus Torvalds if (end <= vma->vm_start) 8681da177e4SLinus Torvalds continue; 8691da177e4SLinus Torvalds 8701da177e4SLinus Torvalds if (vma->vm_flags & VM_ACCOUNT) 8711da177e4SLinus Torvalds *nr_accounted += (end - start) >> PAGE_SHIFT; 8721da177e4SLinus Torvalds 8731da177e4SLinus Torvalds while (start != end) { 8741da177e4SLinus Torvalds if (!tlb_start_valid) { 8751da177e4SLinus Torvalds tlb_start = start; 8761da177e4SLinus Torvalds tlb_start_valid = 1; 8771da177e4SLinus Torvalds } 8781da177e4SLinus Torvalds 87951c6f666SRobin Holt if (unlikely(is_vm_hugetlb_page(vma))) { 8801da177e4SLinus Torvalds unmap_hugepage_range(vma, start, end); 88151c6f666SRobin Holt zap_work -= (end - start) / 88251c6f666SRobin Holt (HPAGE_SIZE / PAGE_SIZE); 88351c6f666SRobin Holt start = end; 88451c6f666SRobin Holt } else 88551c6f666SRobin Holt start = unmap_page_range(*tlbp, vma, 88651c6f666SRobin Holt start, end, &zap_work, details); 8871da177e4SLinus Torvalds 88851c6f666SRobin Holt if (zap_work > 0) { 88951c6f666SRobin Holt BUG_ON(start != end); 89051c6f666SRobin Holt break; 89151c6f666SRobin Holt } 8921da177e4SLinus Torvalds 8931da177e4SLinus Torvalds tlb_finish_mmu(*tlbp, tlb_start, start); 8941da177e4SLinus Torvalds 8951da177e4SLinus Torvalds if (need_resched() || 89695c354feSNick Piggin (i_mmap_lock && spin_needbreak(i_mmap_lock))) { 8971da177e4SLinus Torvalds if (i_mmap_lock) { 898508034a3SHugh Dickins *tlbp = NULL; 8991da177e4SLinus Torvalds goto out; 9001da177e4SLinus Torvalds } 9011da177e4SLinus Torvalds cond_resched(); 9021da177e4SLinus Torvalds } 9031da177e4SLinus Torvalds 904508034a3SHugh Dickins *tlbp = tlb_gather_mmu(vma->vm_mm, fullmm); 9051da177e4SLinus Torvalds tlb_start_valid = 0; 90651c6f666SRobin Holt zap_work = ZAP_BLOCK_SIZE; 9071da177e4SLinus Torvalds } 9081da177e4SLinus Torvalds } 9091da177e4SLinus Torvalds out: 910ee39b37bSHugh Dickins return start; /* which is now the end (or restart) address */ 9111da177e4SLinus Torvalds } 9121da177e4SLinus Torvalds 9131da177e4SLinus Torvalds /** 9141da177e4SLinus Torvalds * zap_page_range - remove user pages in a given range 9151da177e4SLinus Torvalds * @vma: vm_area_struct holding the applicable pages 9161da177e4SLinus Torvalds * @address: starting address of pages to zap 9171da177e4SLinus Torvalds * @size: number of bytes to zap 9181da177e4SLinus Torvalds * @details: details of nonlinear truncation or shared cache invalidation 9191da177e4SLinus Torvalds */ 920ee39b37bSHugh Dickins unsigned long zap_page_range(struct vm_area_struct *vma, unsigned long address, 9211da177e4SLinus Torvalds unsigned long size, struct zap_details *details) 9221da177e4SLinus Torvalds { 9231da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 9241da177e4SLinus Torvalds struct mmu_gather *tlb; 9251da177e4SLinus Torvalds unsigned long end = address + size; 9261da177e4SLinus Torvalds unsigned long nr_accounted = 0; 9271da177e4SLinus Torvalds 9281da177e4SLinus Torvalds lru_add_drain(); 9291da177e4SLinus Torvalds tlb = tlb_gather_mmu(mm, 0); 930365e9c87SHugh Dickins update_hiwater_rss(mm); 931508034a3SHugh Dickins end = unmap_vmas(&tlb, vma, address, end, &nr_accounted, details); 932508034a3SHugh Dickins if (tlb) 9338f4f8c16SHugh Dickins tlb_finish_mmu(tlb, address, end); 934ee39b37bSHugh Dickins return end; 9351da177e4SLinus Torvalds } 9361da177e4SLinus Torvalds 9371da177e4SLinus Torvalds /* 9381da177e4SLinus Torvalds * Do a quick page-table lookup for a single page. 9391da177e4SLinus Torvalds */ 9406aab341eSLinus Torvalds struct page *follow_page(struct vm_area_struct *vma, unsigned long address, 941deceb6cdSHugh Dickins unsigned int flags) 9421da177e4SLinus Torvalds { 9431da177e4SLinus Torvalds pgd_t *pgd; 9441da177e4SLinus Torvalds pud_t *pud; 9451da177e4SLinus Torvalds pmd_t *pmd; 9461da177e4SLinus Torvalds pte_t *ptep, pte; 947deceb6cdSHugh Dickins spinlock_t *ptl; 9481da177e4SLinus Torvalds struct page *page; 9496aab341eSLinus Torvalds struct mm_struct *mm = vma->vm_mm; 9501da177e4SLinus Torvalds 951deceb6cdSHugh Dickins page = follow_huge_addr(mm, address, flags & FOLL_WRITE); 952deceb6cdSHugh Dickins if (!IS_ERR(page)) { 953deceb6cdSHugh Dickins BUG_ON(flags & FOLL_GET); 954deceb6cdSHugh Dickins goto out; 955deceb6cdSHugh Dickins } 9561da177e4SLinus Torvalds 957deceb6cdSHugh Dickins page = NULL; 9581da177e4SLinus Torvalds pgd = pgd_offset(mm, address); 9591da177e4SLinus Torvalds if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) 960deceb6cdSHugh Dickins goto no_page_table; 9611da177e4SLinus Torvalds 9621da177e4SLinus Torvalds pud = pud_offset(pgd, address); 9631da177e4SLinus Torvalds if (pud_none(*pud) || unlikely(pud_bad(*pud))) 964deceb6cdSHugh Dickins goto no_page_table; 9651da177e4SLinus Torvalds 9661da177e4SLinus Torvalds pmd = pmd_offset(pud, address); 9671da177e4SLinus Torvalds if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd))) 968deceb6cdSHugh Dickins goto no_page_table; 9691da177e4SLinus Torvalds 970deceb6cdSHugh Dickins if (pmd_huge(*pmd)) { 971deceb6cdSHugh Dickins BUG_ON(flags & FOLL_GET); 972deceb6cdSHugh Dickins page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE); 973deceb6cdSHugh Dickins goto out; 974deceb6cdSHugh Dickins } 975deceb6cdSHugh Dickins 976deceb6cdSHugh Dickins ptep = pte_offset_map_lock(mm, pmd, address, &ptl); 9771da177e4SLinus Torvalds if (!ptep) 9781da177e4SLinus Torvalds goto out; 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds pte = *ptep; 981deceb6cdSHugh Dickins if (!pte_present(pte)) 982deceb6cdSHugh Dickins goto unlock; 983deceb6cdSHugh Dickins if ((flags & FOLL_WRITE) && !pte_write(pte)) 984deceb6cdSHugh Dickins goto unlock; 9856aab341eSLinus Torvalds page = vm_normal_page(vma, address, pte); 9866aab341eSLinus Torvalds if (unlikely(!page)) 987deceb6cdSHugh Dickins goto unlock; 988deceb6cdSHugh Dickins 989deceb6cdSHugh Dickins if (flags & FOLL_GET) 990deceb6cdSHugh Dickins get_page(page); 991deceb6cdSHugh Dickins if (flags & FOLL_TOUCH) { 992deceb6cdSHugh Dickins if ((flags & FOLL_WRITE) && 993deceb6cdSHugh Dickins !pte_dirty(pte) && !PageDirty(page)) 994f33ea7f4SNick Piggin set_page_dirty(page); 9951da177e4SLinus Torvalds mark_page_accessed(page); 9961da177e4SLinus Torvalds } 997deceb6cdSHugh Dickins unlock: 998deceb6cdSHugh Dickins pte_unmap_unlock(ptep, ptl); 9991da177e4SLinus Torvalds out: 1000deceb6cdSHugh Dickins return page; 1001deceb6cdSHugh Dickins 1002deceb6cdSHugh Dickins no_page_table: 1003deceb6cdSHugh Dickins /* 1004deceb6cdSHugh Dickins * When core dumping an enormous anonymous area that nobody 1005deceb6cdSHugh Dickins * has touched so far, we don't want to allocate page tables. 1006deceb6cdSHugh Dickins */ 1007deceb6cdSHugh Dickins if (flags & FOLL_ANON) { 1008557ed1faSNick Piggin page = ZERO_PAGE(0); 1009deceb6cdSHugh Dickins if (flags & FOLL_GET) 1010deceb6cdSHugh Dickins get_page(page); 1011deceb6cdSHugh Dickins BUG_ON(flags & FOLL_WRITE); 10121da177e4SLinus Torvalds } 1013deceb6cdSHugh Dickins return page; 10141da177e4SLinus Torvalds } 10151da177e4SLinus Torvalds 10161da177e4SLinus Torvalds int get_user_pages(struct task_struct *tsk, struct mm_struct *mm, 10171da177e4SLinus Torvalds unsigned long start, int len, int write, int force, 10181da177e4SLinus Torvalds struct page **pages, struct vm_area_struct **vmas) 10191da177e4SLinus Torvalds { 10201da177e4SLinus Torvalds int i; 1021deceb6cdSHugh Dickins unsigned int vm_flags; 10221da177e4SLinus Torvalds 1023900cf086SJonathan Corbet if (len <= 0) 1024900cf086SJonathan Corbet return 0; 10251da177e4SLinus Torvalds /* 10261da177e4SLinus Torvalds * Require read or write permissions. 10271da177e4SLinus Torvalds * If 'force' is set, we only require the "MAY" flags. 10281da177e4SLinus Torvalds */ 1029deceb6cdSHugh Dickins vm_flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD); 1030deceb6cdSHugh Dickins vm_flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE); 10311da177e4SLinus Torvalds i = 0; 10321da177e4SLinus Torvalds 10331da177e4SLinus Torvalds do { 10341da177e4SLinus Torvalds struct vm_area_struct *vma; 1035deceb6cdSHugh Dickins unsigned int foll_flags; 10361da177e4SLinus Torvalds 10371da177e4SLinus Torvalds vma = find_extend_vma(mm, start); 10381da177e4SLinus Torvalds if (!vma && in_gate_area(tsk, start)) { 10391da177e4SLinus Torvalds unsigned long pg = start & PAGE_MASK; 10401da177e4SLinus Torvalds struct vm_area_struct *gate_vma = get_gate_vma(tsk); 10411da177e4SLinus Torvalds pgd_t *pgd; 10421da177e4SLinus Torvalds pud_t *pud; 10431da177e4SLinus Torvalds pmd_t *pmd; 10441da177e4SLinus Torvalds pte_t *pte; 10451da177e4SLinus Torvalds if (write) /* user gate pages are read-only */ 10461da177e4SLinus Torvalds return i ? : -EFAULT; 10471da177e4SLinus Torvalds if (pg > TASK_SIZE) 10481da177e4SLinus Torvalds pgd = pgd_offset_k(pg); 10491da177e4SLinus Torvalds else 10501da177e4SLinus Torvalds pgd = pgd_offset_gate(mm, pg); 10511da177e4SLinus Torvalds BUG_ON(pgd_none(*pgd)); 10521da177e4SLinus Torvalds pud = pud_offset(pgd, pg); 10531da177e4SLinus Torvalds BUG_ON(pud_none(*pud)); 10541da177e4SLinus Torvalds pmd = pmd_offset(pud, pg); 1055690dbe1cSHugh Dickins if (pmd_none(*pmd)) 1056690dbe1cSHugh Dickins return i ? : -EFAULT; 10571da177e4SLinus Torvalds pte = pte_offset_map(pmd, pg); 1058690dbe1cSHugh Dickins if (pte_none(*pte)) { 1059690dbe1cSHugh Dickins pte_unmap(pte); 1060690dbe1cSHugh Dickins return i ? : -EFAULT; 1061690dbe1cSHugh Dickins } 10621da177e4SLinus Torvalds if (pages) { 1063fa2a455bSNick Piggin struct page *page = vm_normal_page(gate_vma, start, *pte); 10646aab341eSLinus Torvalds pages[i] = page; 10656aab341eSLinus Torvalds if (page) 10666aab341eSLinus Torvalds get_page(page); 10671da177e4SLinus Torvalds } 10681da177e4SLinus Torvalds pte_unmap(pte); 10691da177e4SLinus Torvalds if (vmas) 10701da177e4SLinus Torvalds vmas[i] = gate_vma; 10711da177e4SLinus Torvalds i++; 10721da177e4SLinus Torvalds start += PAGE_SIZE; 10731da177e4SLinus Torvalds len--; 10741da177e4SLinus Torvalds continue; 10751da177e4SLinus Torvalds } 10761da177e4SLinus Torvalds 10771ff80389SLinus Torvalds if (!vma || (vma->vm_flags & (VM_IO | VM_PFNMAP)) 1078deceb6cdSHugh Dickins || !(vm_flags & vma->vm_flags)) 10791da177e4SLinus Torvalds return i ? : -EFAULT; 10801da177e4SLinus Torvalds 10811da177e4SLinus Torvalds if (is_vm_hugetlb_page(vma)) { 10821da177e4SLinus Torvalds i = follow_hugetlb_page(mm, vma, pages, vmas, 10835b23dbe8SAdam Litke &start, &len, i, write); 10841da177e4SLinus Torvalds continue; 10851da177e4SLinus Torvalds } 1086deceb6cdSHugh Dickins 1087deceb6cdSHugh Dickins foll_flags = FOLL_TOUCH; 1088deceb6cdSHugh Dickins if (pages) 1089deceb6cdSHugh Dickins foll_flags |= FOLL_GET; 1090deceb6cdSHugh Dickins if (!write && !(vma->vm_flags & VM_LOCKED) && 10913c18ddd1SNick Piggin (!vma->vm_ops || !vma->vm_ops->fault)) 1092deceb6cdSHugh Dickins foll_flags |= FOLL_ANON; 1093deceb6cdSHugh Dickins 10941da177e4SLinus Torvalds do { 109508ef4729SHugh Dickins struct page *page; 10961da177e4SLinus Torvalds 1097462e00ccSEthan Solomita /* 1098462e00ccSEthan Solomita * If tsk is ooming, cut off its access to large memory 1099462e00ccSEthan Solomita * allocations. It has a pending SIGKILL, but it can't 1100462e00ccSEthan Solomita * be processed until returning to user space. 1101462e00ccSEthan Solomita */ 1102462e00ccSEthan Solomita if (unlikely(test_tsk_thread_flag(tsk, TIF_MEMDIE))) 1103462e00ccSEthan Solomita return -ENOMEM; 1104462e00ccSEthan Solomita 1105deceb6cdSHugh Dickins if (write) 1106deceb6cdSHugh Dickins foll_flags |= FOLL_WRITE; 1107deceb6cdSHugh Dickins 1108deceb6cdSHugh Dickins cond_resched(); 11096aab341eSLinus Torvalds while (!(page = follow_page(vma, start, foll_flags))) { 1110a68d2ebcSLinus Torvalds int ret; 111183c54070SNick Piggin ret = handle_mm_fault(mm, vma, start, 1112deceb6cdSHugh Dickins foll_flags & FOLL_WRITE); 111383c54070SNick Piggin if (ret & VM_FAULT_ERROR) { 111483c54070SNick Piggin if (ret & VM_FAULT_OOM) 111583c54070SNick Piggin return i ? i : -ENOMEM; 111683c54070SNick Piggin else if (ret & VM_FAULT_SIGBUS) 111783c54070SNick Piggin return i ? i : -EFAULT; 111883c54070SNick Piggin BUG(); 111983c54070SNick Piggin } 112083c54070SNick Piggin if (ret & VM_FAULT_MAJOR) 112183c54070SNick Piggin tsk->maj_flt++; 112283c54070SNick Piggin else 112383c54070SNick Piggin tsk->min_flt++; 112483c54070SNick Piggin 1125f33ea7f4SNick Piggin /* 112683c54070SNick Piggin * The VM_FAULT_WRITE bit tells us that 112783c54070SNick Piggin * do_wp_page has broken COW when necessary, 112883c54070SNick Piggin * even if maybe_mkwrite decided not to set 112983c54070SNick Piggin * pte_write. We can thus safely do subsequent 113083c54070SNick Piggin * page lookups as if they were reads. 1131f33ea7f4SNick Piggin */ 1132a68d2ebcSLinus Torvalds if (ret & VM_FAULT_WRITE) 1133deceb6cdSHugh Dickins foll_flags &= ~FOLL_WRITE; 1134a68d2ebcSLinus Torvalds 11357f7bbbe5SBenjamin Herrenschmidt cond_resched(); 11361da177e4SLinus Torvalds } 11371da177e4SLinus Torvalds if (pages) { 113808ef4729SHugh Dickins pages[i] = page; 113903beb076SJames Bottomley 1140a6f36be3SRussell King flush_anon_page(vma, page, start); 114108ef4729SHugh Dickins flush_dcache_page(page); 11421da177e4SLinus Torvalds } 11431da177e4SLinus Torvalds if (vmas) 11441da177e4SLinus Torvalds vmas[i] = vma; 11451da177e4SLinus Torvalds i++; 11461da177e4SLinus Torvalds start += PAGE_SIZE; 11471da177e4SLinus Torvalds len--; 11481da177e4SLinus Torvalds } while (len && start < vma->vm_end); 11491da177e4SLinus Torvalds } while (len); 11501da177e4SLinus Torvalds return i; 11511da177e4SLinus Torvalds } 11521da177e4SLinus Torvalds EXPORT_SYMBOL(get_user_pages); 11531da177e4SLinus Torvalds 1154920c7a5dSHarvey Harrison pte_t *get_locked_pte(struct mm_struct *mm, unsigned long addr, 1155920c7a5dSHarvey Harrison spinlock_t **ptl) 1156c9cfcddfSLinus Torvalds { 1157c9cfcddfSLinus Torvalds pgd_t * pgd = pgd_offset(mm, addr); 1158c9cfcddfSLinus Torvalds pud_t * pud = pud_alloc(mm, pgd, addr); 1159c9cfcddfSLinus Torvalds if (pud) { 116049c91fb0STrond Myklebust pmd_t * pmd = pmd_alloc(mm, pud, addr); 1161c9cfcddfSLinus Torvalds if (pmd) 1162c9cfcddfSLinus Torvalds return pte_alloc_map_lock(mm, pmd, addr, ptl); 1163c9cfcddfSLinus Torvalds } 1164c9cfcddfSLinus Torvalds return NULL; 1165c9cfcddfSLinus Torvalds } 1166c9cfcddfSLinus Torvalds 11671da177e4SLinus Torvalds /* 1168238f58d8SLinus Torvalds * This is the old fallback for page remapping. 1169238f58d8SLinus Torvalds * 1170238f58d8SLinus Torvalds * For historical reasons, it only allows reserved pages. Only 1171238f58d8SLinus Torvalds * old drivers should use this, and they needed to mark their 1172238f58d8SLinus Torvalds * pages reserved for the old functions anyway. 1173238f58d8SLinus Torvalds */ 1174238f58d8SLinus Torvalds static int insert_page(struct mm_struct *mm, unsigned long addr, struct page *page, pgprot_t prot) 1175238f58d8SLinus Torvalds { 1176238f58d8SLinus Torvalds int retval; 1177238f58d8SLinus Torvalds pte_t *pte; 1178238f58d8SLinus Torvalds spinlock_t *ptl; 1179238f58d8SLinus Torvalds 1180e1a1cd59SBalbir Singh retval = mem_cgroup_charge(page, mm, GFP_KERNEL); 11818a9f3ccdSBalbir Singh if (retval) 11828a9f3ccdSBalbir Singh goto out; 11838a9f3ccdSBalbir Singh 1184238f58d8SLinus Torvalds retval = -EINVAL; 1185a145dd41SLinus Torvalds if (PageAnon(page)) 11868a9f3ccdSBalbir Singh goto out_uncharge; 1187238f58d8SLinus Torvalds retval = -ENOMEM; 1188238f58d8SLinus Torvalds flush_dcache_page(page); 1189c9cfcddfSLinus Torvalds pte = get_locked_pte(mm, addr, &ptl); 1190238f58d8SLinus Torvalds if (!pte) 11918a9f3ccdSBalbir Singh goto out_uncharge; 1192238f58d8SLinus Torvalds retval = -EBUSY; 1193238f58d8SLinus Torvalds if (!pte_none(*pte)) 1194238f58d8SLinus Torvalds goto out_unlock; 1195238f58d8SLinus Torvalds 1196238f58d8SLinus Torvalds /* Ok, finally just insert the thing.. */ 1197238f58d8SLinus Torvalds get_page(page); 1198238f58d8SLinus Torvalds inc_mm_counter(mm, file_rss); 1199238f58d8SLinus Torvalds page_add_file_rmap(page); 1200238f58d8SLinus Torvalds set_pte_at(mm, addr, pte, mk_pte(page, prot)); 1201238f58d8SLinus Torvalds 1202238f58d8SLinus Torvalds retval = 0; 12038a9f3ccdSBalbir Singh pte_unmap_unlock(pte, ptl); 12048a9f3ccdSBalbir Singh return retval; 1205238f58d8SLinus Torvalds out_unlock: 1206238f58d8SLinus Torvalds pte_unmap_unlock(pte, ptl); 12078a9f3ccdSBalbir Singh out_uncharge: 12088a9f3ccdSBalbir Singh mem_cgroup_uncharge_page(page); 1209238f58d8SLinus Torvalds out: 1210238f58d8SLinus Torvalds return retval; 1211238f58d8SLinus Torvalds } 1212238f58d8SLinus Torvalds 1213bfa5bf6dSRolf Eike Beer /** 1214bfa5bf6dSRolf Eike Beer * vm_insert_page - insert single page into user vma 1215bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 1216bfa5bf6dSRolf Eike Beer * @addr: target user address of this page 1217bfa5bf6dSRolf Eike Beer * @page: source kernel page 1218bfa5bf6dSRolf Eike Beer * 1219a145dd41SLinus Torvalds * This allows drivers to insert individual pages they've allocated 1220a145dd41SLinus Torvalds * into a user vma. 1221a145dd41SLinus Torvalds * 1222a145dd41SLinus Torvalds * The page has to be a nice clean _individual_ kernel allocation. 1223a145dd41SLinus Torvalds * If you allocate a compound page, you need to have marked it as 1224a145dd41SLinus Torvalds * such (__GFP_COMP), or manually just split the page up yourself 12258dfcc9baSNick Piggin * (see split_page()). 1226a145dd41SLinus Torvalds * 1227a145dd41SLinus Torvalds * NOTE! Traditionally this was done with "remap_pfn_range()" which 1228a145dd41SLinus Torvalds * took an arbitrary page protection parameter. This doesn't allow 1229a145dd41SLinus Torvalds * that. Your vma protection will have to be set up correctly, which 1230a145dd41SLinus Torvalds * means that if you want a shared writable mapping, you'd better 1231a145dd41SLinus Torvalds * ask for a shared writable mapping! 1232a145dd41SLinus Torvalds * 1233a145dd41SLinus Torvalds * The page does not need to be reserved. 1234a145dd41SLinus Torvalds */ 1235a145dd41SLinus Torvalds int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, struct page *page) 1236a145dd41SLinus Torvalds { 1237a145dd41SLinus Torvalds if (addr < vma->vm_start || addr >= vma->vm_end) 1238a145dd41SLinus Torvalds return -EFAULT; 1239a145dd41SLinus Torvalds if (!page_count(page)) 1240a145dd41SLinus Torvalds return -EINVAL; 12414d7672b4SLinus Torvalds vma->vm_flags |= VM_INSERTPAGE; 1242a145dd41SLinus Torvalds return insert_page(vma->vm_mm, addr, page, vma->vm_page_prot); 1243a145dd41SLinus Torvalds } 1244e3c3374fSLinus Torvalds EXPORT_SYMBOL(vm_insert_page); 1245a145dd41SLinus Torvalds 1246e0dc0d8fSNick Piggin /** 1247e0dc0d8fSNick Piggin * vm_insert_pfn - insert single pfn into user vma 1248e0dc0d8fSNick Piggin * @vma: user vma to map to 1249e0dc0d8fSNick Piggin * @addr: target user address of this page 1250e0dc0d8fSNick Piggin * @pfn: source kernel pfn 1251e0dc0d8fSNick Piggin * 1252e0dc0d8fSNick Piggin * Similar to vm_inert_page, this allows drivers to insert individual pages 1253e0dc0d8fSNick Piggin * they've allocated into a user vma. Same comments apply. 1254e0dc0d8fSNick Piggin * 1255e0dc0d8fSNick Piggin * This function should only be called from a vm_ops->fault handler, and 1256e0dc0d8fSNick Piggin * in that case the handler should return NULL. 1257e0dc0d8fSNick Piggin */ 1258e0dc0d8fSNick Piggin int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr, 1259e0dc0d8fSNick Piggin unsigned long pfn) 1260e0dc0d8fSNick Piggin { 1261e0dc0d8fSNick Piggin struct mm_struct *mm = vma->vm_mm; 1262e0dc0d8fSNick Piggin int retval; 1263e0dc0d8fSNick Piggin pte_t *pte, entry; 1264e0dc0d8fSNick Piggin spinlock_t *ptl; 1265e0dc0d8fSNick Piggin 1266*b379d790SJared Hulbert BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))); 1267*b379d790SJared Hulbert BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) == 1268*b379d790SJared Hulbert (VM_PFNMAP|VM_MIXEDMAP)); 1269*b379d790SJared Hulbert BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); 1270*b379d790SJared Hulbert BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn)); 1271e0dc0d8fSNick Piggin 1272e0dc0d8fSNick Piggin retval = -ENOMEM; 1273e0dc0d8fSNick Piggin pte = get_locked_pte(mm, addr, &ptl); 1274e0dc0d8fSNick Piggin if (!pte) 1275e0dc0d8fSNick Piggin goto out; 1276e0dc0d8fSNick Piggin retval = -EBUSY; 1277e0dc0d8fSNick Piggin if (!pte_none(*pte)) 1278e0dc0d8fSNick Piggin goto out_unlock; 1279e0dc0d8fSNick Piggin 1280e0dc0d8fSNick Piggin /* Ok, finally just insert the thing.. */ 1281e0dc0d8fSNick Piggin entry = pfn_pte(pfn, vma->vm_page_prot); 1282e0dc0d8fSNick Piggin set_pte_at(mm, addr, pte, entry); 1283e0dc0d8fSNick Piggin update_mmu_cache(vma, addr, entry); 1284e0dc0d8fSNick Piggin 1285e0dc0d8fSNick Piggin retval = 0; 1286e0dc0d8fSNick Piggin out_unlock: 1287e0dc0d8fSNick Piggin pte_unmap_unlock(pte, ptl); 1288e0dc0d8fSNick Piggin 1289e0dc0d8fSNick Piggin out: 1290e0dc0d8fSNick Piggin return retval; 1291e0dc0d8fSNick Piggin } 1292e0dc0d8fSNick Piggin EXPORT_SYMBOL(vm_insert_pfn); 1293e0dc0d8fSNick Piggin 1294a145dd41SLinus Torvalds /* 12951da177e4SLinus Torvalds * maps a range of physical memory into the requested pages. the old 12961da177e4SLinus Torvalds * mappings are removed. any references to nonexistent pages results 12971da177e4SLinus Torvalds * in null mappings (currently treated as "copy-on-access") 12981da177e4SLinus Torvalds */ 12991da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd, 13001da177e4SLinus Torvalds unsigned long addr, unsigned long end, 13011da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 13021da177e4SLinus Torvalds { 13031da177e4SLinus Torvalds pte_t *pte; 1304c74df32cSHugh Dickins spinlock_t *ptl; 13051da177e4SLinus Torvalds 1306c74df32cSHugh Dickins pte = pte_alloc_map_lock(mm, pmd, addr, &ptl); 13071da177e4SLinus Torvalds if (!pte) 13081da177e4SLinus Torvalds return -ENOMEM; 13096606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 13101da177e4SLinus Torvalds do { 13111da177e4SLinus Torvalds BUG_ON(!pte_none(*pte)); 13121da177e4SLinus Torvalds set_pte_at(mm, addr, pte, pfn_pte(pfn, prot)); 13131da177e4SLinus Torvalds pfn++; 13141da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 13156606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 1316c74df32cSHugh Dickins pte_unmap_unlock(pte - 1, ptl); 13171da177e4SLinus Torvalds return 0; 13181da177e4SLinus Torvalds } 13191da177e4SLinus Torvalds 13201da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud, 13211da177e4SLinus Torvalds unsigned long addr, unsigned long end, 13221da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 13231da177e4SLinus Torvalds { 13241da177e4SLinus Torvalds pmd_t *pmd; 13251da177e4SLinus Torvalds unsigned long next; 13261da177e4SLinus Torvalds 13271da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 13281da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, addr); 13291da177e4SLinus Torvalds if (!pmd) 13301da177e4SLinus Torvalds return -ENOMEM; 13311da177e4SLinus Torvalds do { 13321da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 13331da177e4SLinus Torvalds if (remap_pte_range(mm, pmd, addr, next, 13341da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot)) 13351da177e4SLinus Torvalds return -ENOMEM; 13361da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 13371da177e4SLinus Torvalds return 0; 13381da177e4SLinus Torvalds } 13391da177e4SLinus Torvalds 13401da177e4SLinus Torvalds static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd, 13411da177e4SLinus Torvalds unsigned long addr, unsigned long end, 13421da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 13431da177e4SLinus Torvalds { 13441da177e4SLinus Torvalds pud_t *pud; 13451da177e4SLinus Torvalds unsigned long next; 13461da177e4SLinus Torvalds 13471da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 13481da177e4SLinus Torvalds pud = pud_alloc(mm, pgd, addr); 13491da177e4SLinus Torvalds if (!pud) 13501da177e4SLinus Torvalds return -ENOMEM; 13511da177e4SLinus Torvalds do { 13521da177e4SLinus Torvalds next = pud_addr_end(addr, end); 13531da177e4SLinus Torvalds if (remap_pmd_range(mm, pud, addr, next, 13541da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot)) 13551da177e4SLinus Torvalds return -ENOMEM; 13561da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 13571da177e4SLinus Torvalds return 0; 13581da177e4SLinus Torvalds } 13591da177e4SLinus Torvalds 1360bfa5bf6dSRolf Eike Beer /** 1361bfa5bf6dSRolf Eike Beer * remap_pfn_range - remap kernel memory to userspace 1362bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 1363bfa5bf6dSRolf Eike Beer * @addr: target user address to start at 1364bfa5bf6dSRolf Eike Beer * @pfn: physical address of kernel memory 1365bfa5bf6dSRolf Eike Beer * @size: size of map area 1366bfa5bf6dSRolf Eike Beer * @prot: page protection flags for this mapping 1367bfa5bf6dSRolf Eike Beer * 1368bfa5bf6dSRolf Eike Beer * Note: this is only safe if the mm semaphore is held when called. 1369bfa5bf6dSRolf Eike Beer */ 13701da177e4SLinus Torvalds int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 13711da177e4SLinus Torvalds unsigned long pfn, unsigned long size, pgprot_t prot) 13721da177e4SLinus Torvalds { 13731da177e4SLinus Torvalds pgd_t *pgd; 13741da177e4SLinus Torvalds unsigned long next; 13752d15cab8SHugh Dickins unsigned long end = addr + PAGE_ALIGN(size); 13761da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 13771da177e4SLinus Torvalds int err; 13781da177e4SLinus Torvalds 13791da177e4SLinus Torvalds /* 13801da177e4SLinus Torvalds * Physically remapped pages are special. Tell the 13811da177e4SLinus Torvalds * rest of the world about it: 13821da177e4SLinus Torvalds * VM_IO tells people not to look at these pages 13831da177e4SLinus Torvalds * (accesses can have side effects). 13840b14c179SHugh Dickins * VM_RESERVED is specified all over the place, because 13850b14c179SHugh Dickins * in 2.4 it kept swapout's vma scan off this vma; but 13860b14c179SHugh Dickins * in 2.6 the LRU scan won't even find its pages, so this 13870b14c179SHugh Dickins * flag means no more than count its pages in reserved_vm, 13880b14c179SHugh Dickins * and omit it from core dump, even when VM_IO turned off. 13896aab341eSLinus Torvalds * VM_PFNMAP tells the core MM that the base pages are just 13906aab341eSLinus Torvalds * raw PFN mappings, and do not have a "struct page" associated 13916aab341eSLinus Torvalds * with them. 1392fb155c16SLinus Torvalds * 1393fb155c16SLinus Torvalds * There's a horrible special case to handle copy-on-write 1394fb155c16SLinus Torvalds * behaviour that some programs depend on. We mark the "original" 1395fb155c16SLinus Torvalds * un-COW'ed pages by matching them up with "vma->vm_pgoff". 13961da177e4SLinus Torvalds */ 139767121172SLinus Torvalds if (is_cow_mapping(vma->vm_flags)) { 1398fb155c16SLinus Torvalds if (addr != vma->vm_start || end != vma->vm_end) 13997fc7e2eeSLinus Torvalds return -EINVAL; 14006aab341eSLinus Torvalds vma->vm_pgoff = pfn; 1401fb155c16SLinus Torvalds } 1402fb155c16SLinus Torvalds 1403fb155c16SLinus Torvalds vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP; 14041da177e4SLinus Torvalds 14051da177e4SLinus Torvalds BUG_ON(addr >= end); 14061da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 14071da177e4SLinus Torvalds pgd = pgd_offset(mm, addr); 14081da177e4SLinus Torvalds flush_cache_range(vma, addr, end); 14091da177e4SLinus Torvalds do { 14101da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 14111da177e4SLinus Torvalds err = remap_pud_range(mm, pgd, addr, next, 14121da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot); 14131da177e4SLinus Torvalds if (err) 14141da177e4SLinus Torvalds break; 14151da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 14161da177e4SLinus Torvalds return err; 14171da177e4SLinus Torvalds } 14181da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range); 14191da177e4SLinus Torvalds 1420aee16b3cSJeremy Fitzhardinge static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd, 1421aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 1422aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 1423aee16b3cSJeremy Fitzhardinge { 1424aee16b3cSJeremy Fitzhardinge pte_t *pte; 1425aee16b3cSJeremy Fitzhardinge int err; 14262f569afdSMartin Schwidefsky pgtable_t token; 142794909914SBorislav Petkov spinlock_t *uninitialized_var(ptl); 1428aee16b3cSJeremy Fitzhardinge 1429aee16b3cSJeremy Fitzhardinge pte = (mm == &init_mm) ? 1430aee16b3cSJeremy Fitzhardinge pte_alloc_kernel(pmd, addr) : 1431aee16b3cSJeremy Fitzhardinge pte_alloc_map_lock(mm, pmd, addr, &ptl); 1432aee16b3cSJeremy Fitzhardinge if (!pte) 1433aee16b3cSJeremy Fitzhardinge return -ENOMEM; 1434aee16b3cSJeremy Fitzhardinge 1435aee16b3cSJeremy Fitzhardinge BUG_ON(pmd_huge(*pmd)); 1436aee16b3cSJeremy Fitzhardinge 14372f569afdSMartin Schwidefsky token = pmd_pgtable(*pmd); 1438aee16b3cSJeremy Fitzhardinge 1439aee16b3cSJeremy Fitzhardinge do { 14402f569afdSMartin Schwidefsky err = fn(pte, token, addr, data); 1441aee16b3cSJeremy Fitzhardinge if (err) 1442aee16b3cSJeremy Fitzhardinge break; 1443aee16b3cSJeremy Fitzhardinge } while (pte++, addr += PAGE_SIZE, addr != end); 1444aee16b3cSJeremy Fitzhardinge 1445aee16b3cSJeremy Fitzhardinge if (mm != &init_mm) 1446aee16b3cSJeremy Fitzhardinge pte_unmap_unlock(pte-1, ptl); 1447aee16b3cSJeremy Fitzhardinge return err; 1448aee16b3cSJeremy Fitzhardinge } 1449aee16b3cSJeremy Fitzhardinge 1450aee16b3cSJeremy Fitzhardinge static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud, 1451aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 1452aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 1453aee16b3cSJeremy Fitzhardinge { 1454aee16b3cSJeremy Fitzhardinge pmd_t *pmd; 1455aee16b3cSJeremy Fitzhardinge unsigned long next; 1456aee16b3cSJeremy Fitzhardinge int err; 1457aee16b3cSJeremy Fitzhardinge 1458aee16b3cSJeremy Fitzhardinge pmd = pmd_alloc(mm, pud, addr); 1459aee16b3cSJeremy Fitzhardinge if (!pmd) 1460aee16b3cSJeremy Fitzhardinge return -ENOMEM; 1461aee16b3cSJeremy Fitzhardinge do { 1462aee16b3cSJeremy Fitzhardinge next = pmd_addr_end(addr, end); 1463aee16b3cSJeremy Fitzhardinge err = apply_to_pte_range(mm, pmd, addr, next, fn, data); 1464aee16b3cSJeremy Fitzhardinge if (err) 1465aee16b3cSJeremy Fitzhardinge break; 1466aee16b3cSJeremy Fitzhardinge } while (pmd++, addr = next, addr != end); 1467aee16b3cSJeremy Fitzhardinge return err; 1468aee16b3cSJeremy Fitzhardinge } 1469aee16b3cSJeremy Fitzhardinge 1470aee16b3cSJeremy Fitzhardinge static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd, 1471aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 1472aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 1473aee16b3cSJeremy Fitzhardinge { 1474aee16b3cSJeremy Fitzhardinge pud_t *pud; 1475aee16b3cSJeremy Fitzhardinge unsigned long next; 1476aee16b3cSJeremy Fitzhardinge int err; 1477aee16b3cSJeremy Fitzhardinge 1478aee16b3cSJeremy Fitzhardinge pud = pud_alloc(mm, pgd, addr); 1479aee16b3cSJeremy Fitzhardinge if (!pud) 1480aee16b3cSJeremy Fitzhardinge return -ENOMEM; 1481aee16b3cSJeremy Fitzhardinge do { 1482aee16b3cSJeremy Fitzhardinge next = pud_addr_end(addr, end); 1483aee16b3cSJeremy Fitzhardinge err = apply_to_pmd_range(mm, pud, addr, next, fn, data); 1484aee16b3cSJeremy Fitzhardinge if (err) 1485aee16b3cSJeremy Fitzhardinge break; 1486aee16b3cSJeremy Fitzhardinge } while (pud++, addr = next, addr != end); 1487aee16b3cSJeremy Fitzhardinge return err; 1488aee16b3cSJeremy Fitzhardinge } 1489aee16b3cSJeremy Fitzhardinge 1490aee16b3cSJeremy Fitzhardinge /* 1491aee16b3cSJeremy Fitzhardinge * Scan a region of virtual memory, filling in page tables as necessary 1492aee16b3cSJeremy Fitzhardinge * and calling a provided function on each leaf page table. 1493aee16b3cSJeremy Fitzhardinge */ 1494aee16b3cSJeremy Fitzhardinge int apply_to_page_range(struct mm_struct *mm, unsigned long addr, 1495aee16b3cSJeremy Fitzhardinge unsigned long size, pte_fn_t fn, void *data) 1496aee16b3cSJeremy Fitzhardinge { 1497aee16b3cSJeremy Fitzhardinge pgd_t *pgd; 1498aee16b3cSJeremy Fitzhardinge unsigned long next; 1499aee16b3cSJeremy Fitzhardinge unsigned long end = addr + size; 1500aee16b3cSJeremy Fitzhardinge int err; 1501aee16b3cSJeremy Fitzhardinge 1502aee16b3cSJeremy Fitzhardinge BUG_ON(addr >= end); 1503aee16b3cSJeremy Fitzhardinge pgd = pgd_offset(mm, addr); 1504aee16b3cSJeremy Fitzhardinge do { 1505aee16b3cSJeremy Fitzhardinge next = pgd_addr_end(addr, end); 1506aee16b3cSJeremy Fitzhardinge err = apply_to_pud_range(mm, pgd, addr, next, fn, data); 1507aee16b3cSJeremy Fitzhardinge if (err) 1508aee16b3cSJeremy Fitzhardinge break; 1509aee16b3cSJeremy Fitzhardinge } while (pgd++, addr = next, addr != end); 1510aee16b3cSJeremy Fitzhardinge return err; 1511aee16b3cSJeremy Fitzhardinge } 1512aee16b3cSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(apply_to_page_range); 1513aee16b3cSJeremy Fitzhardinge 15141da177e4SLinus Torvalds /* 15158f4e2101SHugh Dickins * handle_pte_fault chooses page fault handler according to an entry 15168f4e2101SHugh Dickins * which was read non-atomically. Before making any commitment, on 15178f4e2101SHugh Dickins * those architectures or configurations (e.g. i386 with PAE) which 15188f4e2101SHugh Dickins * might give a mix of unmatched parts, do_swap_page and do_file_page 15198f4e2101SHugh Dickins * must check under lock before unmapping the pte and proceeding 15208f4e2101SHugh Dickins * (but do_wp_page is only called after already making such a check; 15218f4e2101SHugh Dickins * and do_anonymous_page and do_no_page can safely check later on). 15228f4e2101SHugh Dickins */ 15234c21e2f2SHugh Dickins static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd, 15248f4e2101SHugh Dickins pte_t *page_table, pte_t orig_pte) 15258f4e2101SHugh Dickins { 15268f4e2101SHugh Dickins int same = 1; 15278f4e2101SHugh Dickins #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT) 15288f4e2101SHugh Dickins if (sizeof(pte_t) > sizeof(unsigned long)) { 15294c21e2f2SHugh Dickins spinlock_t *ptl = pte_lockptr(mm, pmd); 15304c21e2f2SHugh Dickins spin_lock(ptl); 15318f4e2101SHugh Dickins same = pte_same(*page_table, orig_pte); 15324c21e2f2SHugh Dickins spin_unlock(ptl); 15338f4e2101SHugh Dickins } 15348f4e2101SHugh Dickins #endif 15358f4e2101SHugh Dickins pte_unmap(page_table); 15368f4e2101SHugh Dickins return same; 15378f4e2101SHugh Dickins } 15388f4e2101SHugh Dickins 15398f4e2101SHugh Dickins /* 15401da177e4SLinus Torvalds * Do pte_mkwrite, but only if the vma says VM_WRITE. We do this when 15411da177e4SLinus Torvalds * servicing faults for write access. In the normal case, do always want 15421da177e4SLinus Torvalds * pte_mkwrite. But get_user_pages can cause write faults for mappings 15431da177e4SLinus Torvalds * that do not have writing enabled, when used by access_process_vm. 15441da177e4SLinus Torvalds */ 15451da177e4SLinus Torvalds static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma) 15461da177e4SLinus Torvalds { 15471da177e4SLinus Torvalds if (likely(vma->vm_flags & VM_WRITE)) 15481da177e4SLinus Torvalds pte = pte_mkwrite(pte); 15491da177e4SLinus Torvalds return pte; 15501da177e4SLinus Torvalds } 15511da177e4SLinus Torvalds 15529de455b2SAtsushi Nemoto static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma) 15536aab341eSLinus Torvalds { 15546aab341eSLinus Torvalds /* 15556aab341eSLinus Torvalds * If the source page was a PFN mapping, we don't have 15566aab341eSLinus Torvalds * a "struct page" for it. We do a best-effort copy by 15576aab341eSLinus Torvalds * just copying from the original user address. If that 15586aab341eSLinus Torvalds * fails, we just zero-fill it. Live with it. 15596aab341eSLinus Torvalds */ 15606aab341eSLinus Torvalds if (unlikely(!src)) { 15616aab341eSLinus Torvalds void *kaddr = kmap_atomic(dst, KM_USER0); 15625d2a2dbbSLinus Torvalds void __user *uaddr = (void __user *)(va & PAGE_MASK); 15635d2a2dbbSLinus Torvalds 15645d2a2dbbSLinus Torvalds /* 15655d2a2dbbSLinus Torvalds * This really shouldn't fail, because the page is there 15665d2a2dbbSLinus Torvalds * in the page tables. But it might just be unreadable, 15675d2a2dbbSLinus Torvalds * in which case we just give up and fill the result with 15685d2a2dbbSLinus Torvalds * zeroes. 15695d2a2dbbSLinus Torvalds */ 15705d2a2dbbSLinus Torvalds if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) 15716aab341eSLinus Torvalds memset(kaddr, 0, PAGE_SIZE); 15726aab341eSLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 1573c4ec7b0dSDmitriy Monakhov flush_dcache_page(dst); 15740ed361deSNick Piggin } else 15759de455b2SAtsushi Nemoto copy_user_highpage(dst, src, va, vma); 15766aab341eSLinus Torvalds } 15776aab341eSLinus Torvalds 15781da177e4SLinus Torvalds /* 15791da177e4SLinus Torvalds * This routine handles present pages, when users try to write 15801da177e4SLinus Torvalds * to a shared page. It is done by copying the page to a new address 15811da177e4SLinus Torvalds * and decrementing the shared-page counter for the old page. 15821da177e4SLinus Torvalds * 15831da177e4SLinus Torvalds * Note that this routine assumes that the protection checks have been 15841da177e4SLinus Torvalds * done by the caller (the low-level page fault routine in most cases). 15851da177e4SLinus Torvalds * Thus we can safely just mark it writable once we've done any necessary 15861da177e4SLinus Torvalds * COW. 15871da177e4SLinus Torvalds * 15881da177e4SLinus Torvalds * We also mark the page dirty at this point even though the page will 15891da177e4SLinus Torvalds * change only once the write actually happens. This avoids a few races, 15901da177e4SLinus Torvalds * and potentially makes it more efficient. 15911da177e4SLinus Torvalds * 15928f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 15938f4e2101SHugh Dickins * but allow concurrent faults), with pte both mapped and locked. 15948f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 15951da177e4SLinus Torvalds */ 15961da177e4SLinus Torvalds static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma, 159765500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 15988f4e2101SHugh Dickins spinlock_t *ptl, pte_t orig_pte) 15991da177e4SLinus Torvalds { 1600e5bbe4dfSHugh Dickins struct page *old_page, *new_page; 16011da177e4SLinus Torvalds pte_t entry; 160283c54070SNick Piggin int reuse = 0, ret = 0; 1603a200ee18SPeter Zijlstra int page_mkwrite = 0; 1604d08b3851SPeter Zijlstra struct page *dirty_page = NULL; 16051da177e4SLinus Torvalds 16066aab341eSLinus Torvalds old_page = vm_normal_page(vma, address, orig_pte); 16076aab341eSLinus Torvalds if (!old_page) 1608920fc356SHugh Dickins goto gotten; 16091da177e4SLinus Torvalds 1610d08b3851SPeter Zijlstra /* 1611ee6a6457SPeter Zijlstra * Take out anonymous pages first, anonymous shared vmas are 1612ee6a6457SPeter Zijlstra * not dirty accountable. 1613d08b3851SPeter Zijlstra */ 1614ee6a6457SPeter Zijlstra if (PageAnon(old_page)) { 1615ee6a6457SPeter Zijlstra if (!TestSetPageLocked(old_page)) { 1616ee6a6457SPeter Zijlstra reuse = can_share_swap_page(old_page); 1617ee6a6457SPeter Zijlstra unlock_page(old_page); 1618ee6a6457SPeter Zijlstra } 1619ee6a6457SPeter Zijlstra } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 1620d08b3851SPeter Zijlstra (VM_WRITE|VM_SHARED))) { 1621ee6a6457SPeter Zijlstra /* 1622ee6a6457SPeter Zijlstra * Only catch write-faults on shared writable pages, 1623ee6a6457SPeter Zijlstra * read-only shared pages can get COWed by 1624ee6a6457SPeter Zijlstra * get_user_pages(.write=1, .force=1). 1625ee6a6457SPeter Zijlstra */ 16269637a5efSDavid Howells if (vma->vm_ops && vma->vm_ops->page_mkwrite) { 16279637a5efSDavid Howells /* 16289637a5efSDavid Howells * Notify the address space that the page is about to 16299637a5efSDavid Howells * become writable so that it can prohibit this or wait 16309637a5efSDavid Howells * for the page to get into an appropriate state. 16319637a5efSDavid Howells * 16329637a5efSDavid Howells * We do this without the lock held, so that it can 16339637a5efSDavid Howells * sleep if it needs to. 16349637a5efSDavid Howells */ 16359637a5efSDavid Howells page_cache_get(old_page); 16369637a5efSDavid Howells pte_unmap_unlock(page_table, ptl); 16379637a5efSDavid Howells 16389637a5efSDavid Howells if (vma->vm_ops->page_mkwrite(vma, old_page) < 0) 16399637a5efSDavid Howells goto unwritable_page; 16409637a5efSDavid Howells 16419637a5efSDavid Howells /* 16429637a5efSDavid Howells * Since we dropped the lock we need to revalidate 16439637a5efSDavid Howells * the PTE as someone else may have changed it. If 16449637a5efSDavid Howells * they did, we just return, as we can count on the 16459637a5efSDavid Howells * MMU to tell us if they didn't also make it writable. 16469637a5efSDavid Howells */ 16479637a5efSDavid Howells page_table = pte_offset_map_lock(mm, pmd, address, 16489637a5efSDavid Howells &ptl); 1649c3704cebSHugh Dickins page_cache_release(old_page); 16509637a5efSDavid Howells if (!pte_same(*page_table, orig_pte)) 16519637a5efSDavid Howells goto unlock; 1652a200ee18SPeter Zijlstra 1653a200ee18SPeter Zijlstra page_mkwrite = 1; 16549637a5efSDavid Howells } 1655d08b3851SPeter Zijlstra dirty_page = old_page; 1656d08b3851SPeter Zijlstra get_page(dirty_page); 16579637a5efSDavid Howells reuse = 1; 16589637a5efSDavid Howells } 16599637a5efSDavid Howells 16601da177e4SLinus Torvalds if (reuse) { 1661eca35133SBen Collins flush_cache_page(vma, address, pte_pfn(orig_pte)); 166265500d23SHugh Dickins entry = pte_mkyoung(orig_pte); 166365500d23SHugh Dickins entry = maybe_mkwrite(pte_mkdirty(entry), vma); 1664954ffcb3SKAMEZAWA Hiroyuki if (ptep_set_access_flags(vma, address, page_table, entry,1)) 16651da177e4SLinus Torvalds update_mmu_cache(vma, address, entry); 166665500d23SHugh Dickins ret |= VM_FAULT_WRITE; 166765500d23SHugh Dickins goto unlock; 16681da177e4SLinus Torvalds } 16691da177e4SLinus Torvalds 16701da177e4SLinus Torvalds /* 16711da177e4SLinus Torvalds * Ok, we need to copy. Oh, well.. 16721da177e4SLinus Torvalds */ 16731da177e4SLinus Torvalds page_cache_get(old_page); 1674920fc356SHugh Dickins gotten: 16758f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 16761da177e4SLinus Torvalds 16771da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 167865500d23SHugh Dickins goto oom; 1679557ed1faSNick Piggin VM_BUG_ON(old_page == ZERO_PAGE(0)); 1680769848c0SMel Gorman new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address); 16811da177e4SLinus Torvalds if (!new_page) 168265500d23SHugh Dickins goto oom; 16839de455b2SAtsushi Nemoto cow_user_page(new_page, old_page, address, vma); 16840ed361deSNick Piggin __SetPageUptodate(new_page); 168565500d23SHugh Dickins 1686e1a1cd59SBalbir Singh if (mem_cgroup_charge(new_page, mm, GFP_KERNEL)) 16878a9f3ccdSBalbir Singh goto oom_free_new; 16888a9f3ccdSBalbir Singh 16891da177e4SLinus Torvalds /* 16901da177e4SLinus Torvalds * Re-check the pte - we dropped the lock 16911da177e4SLinus Torvalds */ 16928f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 169365500d23SHugh Dickins if (likely(pte_same(*page_table, orig_pte))) { 1694920fc356SHugh Dickins if (old_page) { 16957de6b805SNick Piggin page_remove_rmap(old_page, vma); 16964294621fSHugh Dickins if (!PageAnon(old_page)) { 16974294621fSHugh Dickins dec_mm_counter(mm, file_rss); 1698920fc356SHugh Dickins inc_mm_counter(mm, anon_rss); 16994294621fSHugh Dickins } 1700920fc356SHugh Dickins } else 1701920fc356SHugh Dickins inc_mm_counter(mm, anon_rss); 1702eca35133SBen Collins flush_cache_page(vma, address, pte_pfn(orig_pte)); 170365500d23SHugh Dickins entry = mk_pte(new_page, vma->vm_page_prot); 170465500d23SHugh Dickins entry = maybe_mkwrite(pte_mkdirty(entry), vma); 17054ce072f1SSiddha, Suresh B /* 17064ce072f1SSiddha, Suresh B * Clear the pte entry and flush it first, before updating the 17074ce072f1SSiddha, Suresh B * pte with the new entry. This will avoid a race condition 17084ce072f1SSiddha, Suresh B * seen in the presence of one thread doing SMC and another 17094ce072f1SSiddha, Suresh B * thread doing COW. 17104ce072f1SSiddha, Suresh B */ 17114ce072f1SSiddha, Suresh B ptep_clear_flush(vma, address, page_table); 17124ce072f1SSiddha, Suresh B set_pte_at(mm, address, page_table, entry); 171365500d23SHugh Dickins update_mmu_cache(vma, address, entry); 17141da177e4SLinus Torvalds lru_cache_add_active(new_page); 17159617d95eSNick Piggin page_add_new_anon_rmap(new_page, vma, address); 17161da177e4SLinus Torvalds 17171da177e4SLinus Torvalds /* Free the old page.. */ 17181da177e4SLinus Torvalds new_page = old_page; 1719f33ea7f4SNick Piggin ret |= VM_FAULT_WRITE; 17208a9f3ccdSBalbir Singh } else 17218a9f3ccdSBalbir Singh mem_cgroup_uncharge_page(new_page); 17228a9f3ccdSBalbir Singh 1723920fc356SHugh Dickins if (new_page) 17241da177e4SLinus Torvalds page_cache_release(new_page); 1725920fc356SHugh Dickins if (old_page) 17261da177e4SLinus Torvalds page_cache_release(old_page); 172765500d23SHugh Dickins unlock: 17288f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 1729d08b3851SPeter Zijlstra if (dirty_page) { 17308f7b3d15SAnton Salikhmetov if (vma->vm_file) 17318f7b3d15SAnton Salikhmetov file_update_time(vma->vm_file); 17328f7b3d15SAnton Salikhmetov 173379352894SNick Piggin /* 173479352894SNick Piggin * Yes, Virginia, this is actually required to prevent a race 173579352894SNick Piggin * with clear_page_dirty_for_io() from clearing the page dirty 173679352894SNick Piggin * bit after it clear all dirty ptes, but before a racing 173779352894SNick Piggin * do_wp_page installs a dirty pte. 173879352894SNick Piggin * 173979352894SNick Piggin * do_no_page is protected similarly. 174079352894SNick Piggin */ 174179352894SNick Piggin wait_on_page_locked(dirty_page); 1742a200ee18SPeter Zijlstra set_page_dirty_balance(dirty_page, page_mkwrite); 1743d08b3851SPeter Zijlstra put_page(dirty_page); 1744d08b3851SPeter Zijlstra } 1745f33ea7f4SNick Piggin return ret; 17468a9f3ccdSBalbir Singh oom_free_new: 17476dbf6d3bSHugh Dickins page_cache_release(new_page); 174865500d23SHugh Dickins oom: 1749920fc356SHugh Dickins if (old_page) 17501da177e4SLinus Torvalds page_cache_release(old_page); 17511da177e4SLinus Torvalds return VM_FAULT_OOM; 17529637a5efSDavid Howells 17539637a5efSDavid Howells unwritable_page: 17549637a5efSDavid Howells page_cache_release(old_page); 17559637a5efSDavid Howells return VM_FAULT_SIGBUS; 17561da177e4SLinus Torvalds } 17571da177e4SLinus Torvalds 17581da177e4SLinus Torvalds /* 17591da177e4SLinus Torvalds * Helper functions for unmap_mapping_range(). 17601da177e4SLinus Torvalds * 17611da177e4SLinus Torvalds * __ Notes on dropping i_mmap_lock to reduce latency while unmapping __ 17621da177e4SLinus Torvalds * 17631da177e4SLinus Torvalds * We have to restart searching the prio_tree whenever we drop the lock, 17641da177e4SLinus Torvalds * since the iterator is only valid while the lock is held, and anyway 17651da177e4SLinus Torvalds * a later vma might be split and reinserted earlier while lock dropped. 17661da177e4SLinus Torvalds * 17671da177e4SLinus Torvalds * The list of nonlinear vmas could be handled more efficiently, using 17681da177e4SLinus Torvalds * a placeholder, but handle it in the same way until a need is shown. 17691da177e4SLinus Torvalds * It is important to search the prio_tree before nonlinear list: a vma 17701da177e4SLinus Torvalds * may become nonlinear and be shifted from prio_tree to nonlinear list 17711da177e4SLinus Torvalds * while the lock is dropped; but never shifted from list to prio_tree. 17721da177e4SLinus Torvalds * 17731da177e4SLinus Torvalds * In order to make forward progress despite restarting the search, 17741da177e4SLinus Torvalds * vm_truncate_count is used to mark a vma as now dealt with, so we can 17751da177e4SLinus Torvalds * quickly skip it next time around. Since the prio_tree search only 17761da177e4SLinus Torvalds * shows us those vmas affected by unmapping the range in question, we 17771da177e4SLinus Torvalds * can't efficiently keep all vmas in step with mapping->truncate_count: 17781da177e4SLinus Torvalds * so instead reset them all whenever it wraps back to 0 (then go to 1). 17791da177e4SLinus Torvalds * mapping->truncate_count and vma->vm_truncate_count are protected by 17801da177e4SLinus Torvalds * i_mmap_lock. 17811da177e4SLinus Torvalds * 17821da177e4SLinus Torvalds * In order to make forward progress despite repeatedly restarting some 1783ee39b37bSHugh Dickins * large vma, note the restart_addr from unmap_vmas when it breaks out: 17841da177e4SLinus Torvalds * and restart from that address when we reach that vma again. It might 17851da177e4SLinus Torvalds * have been split or merged, shrunk or extended, but never shifted: so 17861da177e4SLinus Torvalds * restart_addr remains valid so long as it remains in the vma's range. 17871da177e4SLinus Torvalds * unmap_mapping_range forces truncate_count to leap over page-aligned 17881da177e4SLinus Torvalds * values so we can save vma's restart_addr in its truncate_count field. 17891da177e4SLinus Torvalds */ 17901da177e4SLinus Torvalds #define is_restart_addr(truncate_count) (!((truncate_count) & ~PAGE_MASK)) 17911da177e4SLinus Torvalds 17921da177e4SLinus Torvalds static void reset_vma_truncate_counts(struct address_space *mapping) 17931da177e4SLinus Torvalds { 17941da177e4SLinus Torvalds struct vm_area_struct *vma; 17951da177e4SLinus Torvalds struct prio_tree_iter iter; 17961da177e4SLinus Torvalds 17971da177e4SLinus Torvalds vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, ULONG_MAX) 17981da177e4SLinus Torvalds vma->vm_truncate_count = 0; 17991da177e4SLinus Torvalds list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list) 18001da177e4SLinus Torvalds vma->vm_truncate_count = 0; 18011da177e4SLinus Torvalds } 18021da177e4SLinus Torvalds 18031da177e4SLinus Torvalds static int unmap_mapping_range_vma(struct vm_area_struct *vma, 18041da177e4SLinus Torvalds unsigned long start_addr, unsigned long end_addr, 18051da177e4SLinus Torvalds struct zap_details *details) 18061da177e4SLinus Torvalds { 18071da177e4SLinus Torvalds unsigned long restart_addr; 18081da177e4SLinus Torvalds int need_break; 18091da177e4SLinus Torvalds 1810d00806b1SNick Piggin /* 1811d00806b1SNick Piggin * files that support invalidating or truncating portions of the 1812d0217ac0SNick Piggin * file from under mmaped areas must have their ->fault function 181383c54070SNick Piggin * return a locked page (and set VM_FAULT_LOCKED in the return). 181483c54070SNick Piggin * This provides synchronisation against concurrent unmapping here. 1815d00806b1SNick Piggin */ 1816d00806b1SNick Piggin 18171da177e4SLinus Torvalds again: 18181da177e4SLinus Torvalds restart_addr = vma->vm_truncate_count; 18191da177e4SLinus Torvalds if (is_restart_addr(restart_addr) && start_addr < restart_addr) { 18201da177e4SLinus Torvalds start_addr = restart_addr; 18211da177e4SLinus Torvalds if (start_addr >= end_addr) { 18221da177e4SLinus Torvalds /* Top of vma has been split off since last time */ 18231da177e4SLinus Torvalds vma->vm_truncate_count = details->truncate_count; 18241da177e4SLinus Torvalds return 0; 18251da177e4SLinus Torvalds } 18261da177e4SLinus Torvalds } 18271da177e4SLinus Torvalds 1828ee39b37bSHugh Dickins restart_addr = zap_page_range(vma, start_addr, 1829ee39b37bSHugh Dickins end_addr - start_addr, details); 183095c354feSNick Piggin need_break = need_resched() || spin_needbreak(details->i_mmap_lock); 18311da177e4SLinus Torvalds 1832ee39b37bSHugh Dickins if (restart_addr >= end_addr) { 18331da177e4SLinus Torvalds /* We have now completed this vma: mark it so */ 18341da177e4SLinus Torvalds vma->vm_truncate_count = details->truncate_count; 18351da177e4SLinus Torvalds if (!need_break) 18361da177e4SLinus Torvalds return 0; 18371da177e4SLinus Torvalds } else { 18381da177e4SLinus Torvalds /* Note restart_addr in vma's truncate_count field */ 1839ee39b37bSHugh Dickins vma->vm_truncate_count = restart_addr; 18401da177e4SLinus Torvalds if (!need_break) 18411da177e4SLinus Torvalds goto again; 18421da177e4SLinus Torvalds } 18431da177e4SLinus Torvalds 18441da177e4SLinus Torvalds spin_unlock(details->i_mmap_lock); 18451da177e4SLinus Torvalds cond_resched(); 18461da177e4SLinus Torvalds spin_lock(details->i_mmap_lock); 18471da177e4SLinus Torvalds return -EINTR; 18481da177e4SLinus Torvalds } 18491da177e4SLinus Torvalds 18501da177e4SLinus Torvalds static inline void unmap_mapping_range_tree(struct prio_tree_root *root, 18511da177e4SLinus Torvalds struct zap_details *details) 18521da177e4SLinus Torvalds { 18531da177e4SLinus Torvalds struct vm_area_struct *vma; 18541da177e4SLinus Torvalds struct prio_tree_iter iter; 18551da177e4SLinus Torvalds pgoff_t vba, vea, zba, zea; 18561da177e4SLinus Torvalds 18571da177e4SLinus Torvalds restart: 18581da177e4SLinus Torvalds vma_prio_tree_foreach(vma, &iter, root, 18591da177e4SLinus Torvalds details->first_index, details->last_index) { 18601da177e4SLinus Torvalds /* Skip quickly over those we have already dealt with */ 18611da177e4SLinus Torvalds if (vma->vm_truncate_count == details->truncate_count) 18621da177e4SLinus Torvalds continue; 18631da177e4SLinus Torvalds 18641da177e4SLinus Torvalds vba = vma->vm_pgoff; 18651da177e4SLinus Torvalds vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1; 18661da177e4SLinus Torvalds /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */ 18671da177e4SLinus Torvalds zba = details->first_index; 18681da177e4SLinus Torvalds if (zba < vba) 18691da177e4SLinus Torvalds zba = vba; 18701da177e4SLinus Torvalds zea = details->last_index; 18711da177e4SLinus Torvalds if (zea > vea) 18721da177e4SLinus Torvalds zea = vea; 18731da177e4SLinus Torvalds 18741da177e4SLinus Torvalds if (unmap_mapping_range_vma(vma, 18751da177e4SLinus Torvalds ((zba - vba) << PAGE_SHIFT) + vma->vm_start, 18761da177e4SLinus Torvalds ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start, 18771da177e4SLinus Torvalds details) < 0) 18781da177e4SLinus Torvalds goto restart; 18791da177e4SLinus Torvalds } 18801da177e4SLinus Torvalds } 18811da177e4SLinus Torvalds 18821da177e4SLinus Torvalds static inline void unmap_mapping_range_list(struct list_head *head, 18831da177e4SLinus Torvalds struct zap_details *details) 18841da177e4SLinus Torvalds { 18851da177e4SLinus Torvalds struct vm_area_struct *vma; 18861da177e4SLinus Torvalds 18871da177e4SLinus Torvalds /* 18881da177e4SLinus Torvalds * In nonlinear VMAs there is no correspondence between virtual address 18891da177e4SLinus Torvalds * offset and file offset. So we must perform an exhaustive search 18901da177e4SLinus Torvalds * across *all* the pages in each nonlinear VMA, not just the pages 18911da177e4SLinus Torvalds * whose virtual address lies outside the file truncation point. 18921da177e4SLinus Torvalds */ 18931da177e4SLinus Torvalds restart: 18941da177e4SLinus Torvalds list_for_each_entry(vma, head, shared.vm_set.list) { 18951da177e4SLinus Torvalds /* Skip quickly over those we have already dealt with */ 18961da177e4SLinus Torvalds if (vma->vm_truncate_count == details->truncate_count) 18971da177e4SLinus Torvalds continue; 18981da177e4SLinus Torvalds details->nonlinear_vma = vma; 18991da177e4SLinus Torvalds if (unmap_mapping_range_vma(vma, vma->vm_start, 19001da177e4SLinus Torvalds vma->vm_end, details) < 0) 19011da177e4SLinus Torvalds goto restart; 19021da177e4SLinus Torvalds } 19031da177e4SLinus Torvalds } 19041da177e4SLinus Torvalds 19051da177e4SLinus Torvalds /** 190672fd4a35SRobert 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. 19073d41088fSMartin Waitz * @mapping: the address space containing mmaps to be unmapped. 19081da177e4SLinus Torvalds * @holebegin: byte in first page to unmap, relative to the start of 19091da177e4SLinus Torvalds * the underlying file. This will be rounded down to a PAGE_SIZE 19101da177e4SLinus Torvalds * boundary. Note that this is different from vmtruncate(), which 19111da177e4SLinus Torvalds * must keep the partial page. In contrast, we must get rid of 19121da177e4SLinus Torvalds * partial pages. 19131da177e4SLinus Torvalds * @holelen: size of prospective hole in bytes. This will be rounded 19141da177e4SLinus Torvalds * up to a PAGE_SIZE boundary. A holelen of zero truncates to the 19151da177e4SLinus Torvalds * end of the file. 19161da177e4SLinus Torvalds * @even_cows: 1 when truncating a file, unmap even private COWed pages; 19171da177e4SLinus Torvalds * but 0 when invalidating pagecache, don't throw away private data. 19181da177e4SLinus Torvalds */ 19191da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping, 19201da177e4SLinus Torvalds loff_t const holebegin, loff_t const holelen, int even_cows) 19211da177e4SLinus Torvalds { 19221da177e4SLinus Torvalds struct zap_details details; 19231da177e4SLinus Torvalds pgoff_t hba = holebegin >> PAGE_SHIFT; 19241da177e4SLinus Torvalds pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 19251da177e4SLinus Torvalds 19261da177e4SLinus Torvalds /* Check for overflow. */ 19271da177e4SLinus Torvalds if (sizeof(holelen) > sizeof(hlen)) { 19281da177e4SLinus Torvalds long long holeend = 19291da177e4SLinus Torvalds (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 19301da177e4SLinus Torvalds if (holeend & ~(long long)ULONG_MAX) 19311da177e4SLinus Torvalds hlen = ULONG_MAX - hba + 1; 19321da177e4SLinus Torvalds } 19331da177e4SLinus Torvalds 19341da177e4SLinus Torvalds details.check_mapping = even_cows? NULL: mapping; 19351da177e4SLinus Torvalds details.nonlinear_vma = NULL; 19361da177e4SLinus Torvalds details.first_index = hba; 19371da177e4SLinus Torvalds details.last_index = hba + hlen - 1; 19381da177e4SLinus Torvalds if (details.last_index < details.first_index) 19391da177e4SLinus Torvalds details.last_index = ULONG_MAX; 19401da177e4SLinus Torvalds details.i_mmap_lock = &mapping->i_mmap_lock; 19411da177e4SLinus Torvalds 19421da177e4SLinus Torvalds spin_lock(&mapping->i_mmap_lock); 19431da177e4SLinus Torvalds 1944d00806b1SNick Piggin /* Protect against endless unmapping loops */ 19451da177e4SLinus Torvalds mapping->truncate_count++; 19461da177e4SLinus Torvalds if (unlikely(is_restart_addr(mapping->truncate_count))) { 19471da177e4SLinus Torvalds if (mapping->truncate_count == 0) 19481da177e4SLinus Torvalds reset_vma_truncate_counts(mapping); 19491da177e4SLinus Torvalds mapping->truncate_count++; 19501da177e4SLinus Torvalds } 19511da177e4SLinus Torvalds details.truncate_count = mapping->truncate_count; 19521da177e4SLinus Torvalds 19531da177e4SLinus Torvalds if (unlikely(!prio_tree_empty(&mapping->i_mmap))) 19541da177e4SLinus Torvalds unmap_mapping_range_tree(&mapping->i_mmap, &details); 19551da177e4SLinus Torvalds if (unlikely(!list_empty(&mapping->i_mmap_nonlinear))) 19561da177e4SLinus Torvalds unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details); 19571da177e4SLinus Torvalds spin_unlock(&mapping->i_mmap_lock); 19581da177e4SLinus Torvalds } 19591da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range); 19601da177e4SLinus Torvalds 1961bfa5bf6dSRolf Eike Beer /** 1962bfa5bf6dSRolf Eike Beer * vmtruncate - unmap mappings "freed" by truncate() syscall 1963bfa5bf6dSRolf Eike Beer * @inode: inode of the file used 1964bfa5bf6dSRolf Eike Beer * @offset: file offset to start truncating 19651da177e4SLinus Torvalds * 19661da177e4SLinus Torvalds * NOTE! We have to be ready to update the memory sharing 19671da177e4SLinus Torvalds * between the file and the memory map for a potential last 19681da177e4SLinus Torvalds * incomplete page. Ugly, but necessary. 19691da177e4SLinus Torvalds */ 19701da177e4SLinus Torvalds int vmtruncate(struct inode * inode, loff_t offset) 19711da177e4SLinus Torvalds { 197261d5048fSChristoph Hellwig if (inode->i_size < offset) { 19731da177e4SLinus Torvalds unsigned long limit; 19741da177e4SLinus Torvalds 19751da177e4SLinus Torvalds limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur; 19761da177e4SLinus Torvalds if (limit != RLIM_INFINITY && offset > limit) 19771da177e4SLinus Torvalds goto out_sig; 19781da177e4SLinus Torvalds if (offset > inode->i_sb->s_maxbytes) 19791da177e4SLinus Torvalds goto out_big; 19801da177e4SLinus Torvalds i_size_write(inode, offset); 198161d5048fSChristoph Hellwig } else { 198261d5048fSChristoph Hellwig struct address_space *mapping = inode->i_mapping; 19831da177e4SLinus Torvalds 198461d5048fSChristoph Hellwig /* 198561d5048fSChristoph Hellwig * truncation of in-use swapfiles is disallowed - it would 198661d5048fSChristoph Hellwig * cause subsequent swapout to scribble on the now-freed 198761d5048fSChristoph Hellwig * blocks. 198861d5048fSChristoph Hellwig */ 198961d5048fSChristoph Hellwig if (IS_SWAPFILE(inode)) 199061d5048fSChristoph Hellwig return -ETXTBSY; 199161d5048fSChristoph Hellwig i_size_write(inode, offset); 199261d5048fSChristoph Hellwig 199361d5048fSChristoph Hellwig /* 199461d5048fSChristoph Hellwig * unmap_mapping_range is called twice, first simply for 199561d5048fSChristoph Hellwig * efficiency so that truncate_inode_pages does fewer 199661d5048fSChristoph Hellwig * single-page unmaps. However after this first call, and 199761d5048fSChristoph Hellwig * before truncate_inode_pages finishes, it is possible for 199861d5048fSChristoph Hellwig * private pages to be COWed, which remain after 199961d5048fSChristoph Hellwig * truncate_inode_pages finishes, hence the second 200061d5048fSChristoph Hellwig * unmap_mapping_range call must be made for correctness. 200161d5048fSChristoph Hellwig */ 200261d5048fSChristoph Hellwig unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1); 200361d5048fSChristoph Hellwig truncate_inode_pages(mapping, offset); 200461d5048fSChristoph Hellwig unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1); 200561d5048fSChristoph Hellwig } 200661d5048fSChristoph Hellwig 20071da177e4SLinus Torvalds if (inode->i_op && inode->i_op->truncate) 20081da177e4SLinus Torvalds inode->i_op->truncate(inode); 20091da177e4SLinus Torvalds return 0; 201061d5048fSChristoph Hellwig 20111da177e4SLinus Torvalds out_sig: 20121da177e4SLinus Torvalds send_sig(SIGXFSZ, current, 0); 20131da177e4SLinus Torvalds out_big: 20141da177e4SLinus Torvalds return -EFBIG; 20151da177e4SLinus Torvalds } 20161da177e4SLinus Torvalds EXPORT_SYMBOL(vmtruncate); 20171da177e4SLinus Torvalds 2018f6b3ec23SBadari Pulavarty int vmtruncate_range(struct inode *inode, loff_t offset, loff_t end) 2019f6b3ec23SBadari Pulavarty { 2020f6b3ec23SBadari Pulavarty struct address_space *mapping = inode->i_mapping; 2021f6b3ec23SBadari Pulavarty 2022f6b3ec23SBadari Pulavarty /* 2023f6b3ec23SBadari Pulavarty * If the underlying filesystem is not going to provide 2024f6b3ec23SBadari Pulavarty * a way to truncate a range of blocks (punch a hole) - 2025f6b3ec23SBadari Pulavarty * we should return failure right now. 2026f6b3ec23SBadari Pulavarty */ 2027f6b3ec23SBadari Pulavarty if (!inode->i_op || !inode->i_op->truncate_range) 2028f6b3ec23SBadari Pulavarty return -ENOSYS; 2029f6b3ec23SBadari Pulavarty 20301b1dcc1bSJes Sorensen mutex_lock(&inode->i_mutex); 2031f6b3ec23SBadari Pulavarty down_write(&inode->i_alloc_sem); 2032f6b3ec23SBadari Pulavarty unmap_mapping_range(mapping, offset, (end - offset), 1); 2033f6b3ec23SBadari Pulavarty truncate_inode_pages_range(mapping, offset, end); 2034d00806b1SNick Piggin unmap_mapping_range(mapping, offset, (end - offset), 1); 2035f6b3ec23SBadari Pulavarty inode->i_op->truncate_range(inode, offset, end); 2036f6b3ec23SBadari Pulavarty up_write(&inode->i_alloc_sem); 20371b1dcc1bSJes Sorensen mutex_unlock(&inode->i_mutex); 2038f6b3ec23SBadari Pulavarty 2039f6b3ec23SBadari Pulavarty return 0; 2040f6b3ec23SBadari Pulavarty } 2041f6b3ec23SBadari Pulavarty 20421da177e4SLinus Torvalds /* 20438f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 20448f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 20458f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 20461da177e4SLinus Torvalds */ 204765500d23SHugh Dickins static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma, 204865500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 204965500d23SHugh Dickins int write_access, pte_t orig_pte) 20501da177e4SLinus Torvalds { 20518f4e2101SHugh Dickins spinlock_t *ptl; 20521da177e4SLinus Torvalds struct page *page; 205365500d23SHugh Dickins swp_entry_t entry; 20541da177e4SLinus Torvalds pte_t pte; 205583c54070SNick Piggin int ret = 0; 20561da177e4SLinus Torvalds 20574c21e2f2SHugh Dickins if (!pte_unmap_same(mm, pmd, page_table, orig_pte)) 20588f4e2101SHugh Dickins goto out; 205965500d23SHugh Dickins 206065500d23SHugh Dickins entry = pte_to_swp_entry(orig_pte); 20610697212aSChristoph Lameter if (is_migration_entry(entry)) { 20620697212aSChristoph Lameter migration_entry_wait(mm, pmd, address); 20630697212aSChristoph Lameter goto out; 20640697212aSChristoph Lameter } 20650ff92245SShailabh Nagar delayacct_set_flag(DELAYACCT_PF_SWAPIN); 20661da177e4SLinus Torvalds page = lookup_swap_cache(entry); 20671da177e4SLinus Torvalds if (!page) { 2068098fe651SAshwin Chaugule grab_swap_token(); /* Contend for token _before_ read-in */ 206902098feaSHugh Dickins page = swapin_readahead(entry, 207002098feaSHugh Dickins GFP_HIGHUSER_MOVABLE, vma, address); 20711da177e4SLinus Torvalds if (!page) { 20721da177e4SLinus Torvalds /* 20738f4e2101SHugh Dickins * Back out if somebody else faulted in this pte 20748f4e2101SHugh Dickins * while we released the pte lock. 20751da177e4SLinus Torvalds */ 20768f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 20771da177e4SLinus Torvalds if (likely(pte_same(*page_table, orig_pte))) 20781da177e4SLinus Torvalds ret = VM_FAULT_OOM; 20790ff92245SShailabh Nagar delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 208065500d23SHugh Dickins goto unlock; 20811da177e4SLinus Torvalds } 20821da177e4SLinus Torvalds 20831da177e4SLinus Torvalds /* Had to read the page from swap area: Major fault */ 20841da177e4SLinus Torvalds ret = VM_FAULT_MAJOR; 2085f8891e5eSChristoph Lameter count_vm_event(PGMAJFAULT); 20861da177e4SLinus Torvalds } 20871da177e4SLinus Torvalds 2088e1a1cd59SBalbir Singh if (mem_cgroup_charge(page, mm, GFP_KERNEL)) { 20898a9f3ccdSBalbir Singh delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 20908a9f3ccdSBalbir Singh ret = VM_FAULT_OOM; 20918a9f3ccdSBalbir Singh goto out; 20928a9f3ccdSBalbir Singh } 20938a9f3ccdSBalbir Singh 20941da177e4SLinus Torvalds mark_page_accessed(page); 20951da177e4SLinus Torvalds lock_page(page); 209620a1022dSBalbir Singh delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 20971da177e4SLinus Torvalds 20981da177e4SLinus Torvalds /* 20998f4e2101SHugh Dickins * Back out if somebody else already faulted in this pte. 21001da177e4SLinus Torvalds */ 21018f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 21029e9bef07SHugh Dickins if (unlikely(!pte_same(*page_table, orig_pte))) 2103b8107480SKirill Korotaev goto out_nomap; 2104b8107480SKirill Korotaev 2105b8107480SKirill Korotaev if (unlikely(!PageUptodate(page))) { 2106b8107480SKirill Korotaev ret = VM_FAULT_SIGBUS; 2107b8107480SKirill Korotaev goto out_nomap; 21081da177e4SLinus Torvalds } 21091da177e4SLinus Torvalds 21101da177e4SLinus Torvalds /* The page isn't present yet, go ahead with the fault. */ 21111da177e4SLinus Torvalds 21124294621fSHugh Dickins inc_mm_counter(mm, anon_rss); 21131da177e4SLinus Torvalds pte = mk_pte(page, vma->vm_page_prot); 21141da177e4SLinus Torvalds if (write_access && can_share_swap_page(page)) { 21151da177e4SLinus Torvalds pte = maybe_mkwrite(pte_mkdirty(pte), vma); 21161da177e4SLinus Torvalds write_access = 0; 21171da177e4SLinus Torvalds } 21181da177e4SLinus Torvalds 21191da177e4SLinus Torvalds flush_icache_page(vma, page); 21201da177e4SLinus Torvalds set_pte_at(mm, address, page_table, pte); 21211da177e4SLinus Torvalds page_add_anon_rmap(page, vma, address); 21221da177e4SLinus Torvalds 2123c475a8abSHugh Dickins swap_free(entry); 2124c475a8abSHugh Dickins if (vm_swap_full()) 2125c475a8abSHugh Dickins remove_exclusive_swap_page(page); 2126c475a8abSHugh Dickins unlock_page(page); 2127c475a8abSHugh Dickins 21281da177e4SLinus Torvalds if (write_access) { 212961469f1dSHugh Dickins ret |= do_wp_page(mm, vma, address, page_table, pmd, ptl, pte); 213061469f1dSHugh Dickins if (ret & VM_FAULT_ERROR) 213161469f1dSHugh Dickins ret &= VM_FAULT_ERROR; 21321da177e4SLinus Torvalds goto out; 21331da177e4SLinus Torvalds } 21341da177e4SLinus Torvalds 21351da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 21361da177e4SLinus Torvalds update_mmu_cache(vma, address, pte); 213765500d23SHugh Dickins unlock: 21388f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 21391da177e4SLinus Torvalds out: 21401da177e4SLinus Torvalds return ret; 2141b8107480SKirill Korotaev out_nomap: 21428a9f3ccdSBalbir Singh mem_cgroup_uncharge_page(page); 21438f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 2144b8107480SKirill Korotaev unlock_page(page); 2145b8107480SKirill Korotaev page_cache_release(page); 214665500d23SHugh Dickins return ret; 21471da177e4SLinus Torvalds } 21481da177e4SLinus Torvalds 21491da177e4SLinus Torvalds /* 21508f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 21518f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 21528f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 21531da177e4SLinus Torvalds */ 215465500d23SHugh Dickins static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma, 215565500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 215665500d23SHugh Dickins int write_access) 21571da177e4SLinus Torvalds { 21588f4e2101SHugh Dickins struct page *page; 21598f4e2101SHugh Dickins spinlock_t *ptl; 21601da177e4SLinus Torvalds pte_t entry; 21611da177e4SLinus Torvalds 21621da177e4SLinus Torvalds /* Allocate our own private page. */ 21631da177e4SLinus Torvalds pte_unmap(page_table); 21641da177e4SLinus Torvalds 21651da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 216665500d23SHugh Dickins goto oom; 2167769848c0SMel Gorman page = alloc_zeroed_user_highpage_movable(vma, address); 21681da177e4SLinus Torvalds if (!page) 216965500d23SHugh Dickins goto oom; 21700ed361deSNick Piggin __SetPageUptodate(page); 21711da177e4SLinus Torvalds 2172e1a1cd59SBalbir Singh if (mem_cgroup_charge(page, mm, GFP_KERNEL)) 21738a9f3ccdSBalbir Singh goto oom_free_page; 21748a9f3ccdSBalbir Singh 217565500d23SHugh Dickins entry = mk_pte(page, vma->vm_page_prot); 217665500d23SHugh Dickins entry = maybe_mkwrite(pte_mkdirty(entry), vma); 21778f4e2101SHugh Dickins 21788f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 21798f4e2101SHugh Dickins if (!pte_none(*page_table)) 21808f4e2101SHugh Dickins goto release; 21818f4e2101SHugh Dickins inc_mm_counter(mm, anon_rss); 21821da177e4SLinus Torvalds lru_cache_add_active(page); 21839617d95eSNick Piggin page_add_new_anon_rmap(page, vma, address); 218465500d23SHugh Dickins set_pte_at(mm, address, page_table, entry); 21851da177e4SLinus Torvalds 21861da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 218765500d23SHugh Dickins update_mmu_cache(vma, address, entry); 218865500d23SHugh Dickins unlock: 21898f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 219083c54070SNick Piggin return 0; 21918f4e2101SHugh Dickins release: 21928a9f3ccdSBalbir Singh mem_cgroup_uncharge_page(page); 21938f4e2101SHugh Dickins page_cache_release(page); 21948f4e2101SHugh Dickins goto unlock; 21958a9f3ccdSBalbir Singh oom_free_page: 21966dbf6d3bSHugh Dickins page_cache_release(page); 219765500d23SHugh Dickins oom: 21981da177e4SLinus Torvalds return VM_FAULT_OOM; 21991da177e4SLinus Torvalds } 22001da177e4SLinus Torvalds 22011da177e4SLinus Torvalds /* 220254cb8821SNick Piggin * __do_fault() tries to create a new page mapping. It aggressively 22031da177e4SLinus Torvalds * tries to share with existing pages, but makes a separate copy if 220454cb8821SNick Piggin * the FAULT_FLAG_WRITE is set in the flags parameter in order to avoid 220554cb8821SNick Piggin * the next page fault. 22061da177e4SLinus Torvalds * 22071da177e4SLinus Torvalds * As this is called only for pages that do not currently exist, we 22081da177e4SLinus Torvalds * do not need to flush old virtual caches or the TLB. 22091da177e4SLinus Torvalds * 22108f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 221116abfa08SHugh Dickins * but allow concurrent faults), and pte neither mapped nor locked. 22128f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 22131da177e4SLinus Torvalds */ 221454cb8821SNick Piggin static int __do_fault(struct mm_struct *mm, struct vm_area_struct *vma, 221516abfa08SHugh Dickins unsigned long address, pmd_t *pmd, 221654cb8821SNick Piggin pgoff_t pgoff, unsigned int flags, pte_t orig_pte) 22171da177e4SLinus Torvalds { 221816abfa08SHugh Dickins pte_t *page_table; 22198f4e2101SHugh Dickins spinlock_t *ptl; 2220d0217ac0SNick Piggin struct page *page; 22211da177e4SLinus Torvalds pte_t entry; 22221da177e4SLinus Torvalds int anon = 0; 2223d08b3851SPeter Zijlstra struct page *dirty_page = NULL; 2224d0217ac0SNick Piggin struct vm_fault vmf; 2225d0217ac0SNick Piggin int ret; 2226a200ee18SPeter Zijlstra int page_mkwrite = 0; 222754cb8821SNick Piggin 2228d0217ac0SNick Piggin vmf.virtual_address = (void __user *)(address & PAGE_MASK); 2229d0217ac0SNick Piggin vmf.pgoff = pgoff; 2230d0217ac0SNick Piggin vmf.flags = flags; 2231d0217ac0SNick Piggin vmf.page = NULL; 22321da177e4SLinus Torvalds 2233325f04dbSHugh Dickins BUG_ON(vma->vm_flags & VM_PFNMAP); 2234325f04dbSHugh Dickins 2235d0217ac0SNick Piggin ret = vma->vm_ops->fault(vma, &vmf); 223683c54070SNick Piggin if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) 223783c54070SNick Piggin return ret; 22381da177e4SLinus Torvalds 2239d00806b1SNick Piggin /* 2240d0217ac0SNick Piggin * For consistency in subsequent calls, make the faulted page always 2241d00806b1SNick Piggin * locked. 2242d00806b1SNick Piggin */ 224383c54070SNick Piggin if (unlikely(!(ret & VM_FAULT_LOCKED))) 2244d0217ac0SNick Piggin lock_page(vmf.page); 224554cb8821SNick Piggin else 2246d0217ac0SNick Piggin VM_BUG_ON(!PageLocked(vmf.page)); 2247d00806b1SNick Piggin 22481da177e4SLinus Torvalds /* 22491da177e4SLinus Torvalds * Should we do an early C-O-W break? 22501da177e4SLinus Torvalds */ 2251d0217ac0SNick Piggin page = vmf.page; 225254cb8821SNick Piggin if (flags & FAULT_FLAG_WRITE) { 22539637a5efSDavid Howells if (!(vma->vm_flags & VM_SHARED)) { 225454cb8821SNick Piggin anon = 1; 2255d00806b1SNick Piggin if (unlikely(anon_vma_prepare(vma))) { 2256d0217ac0SNick Piggin ret = VM_FAULT_OOM; 225754cb8821SNick Piggin goto out; 2258d00806b1SNick Piggin } 225983c54070SNick Piggin page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, 226083c54070SNick Piggin vma, address); 2261d00806b1SNick Piggin if (!page) { 2262d0217ac0SNick Piggin ret = VM_FAULT_OOM; 226354cb8821SNick Piggin goto out; 2264d00806b1SNick Piggin } 2265d0217ac0SNick Piggin copy_user_highpage(page, vmf.page, address, vma); 22660ed361deSNick Piggin __SetPageUptodate(page); 22679637a5efSDavid Howells } else { 226854cb8821SNick Piggin /* 226954cb8821SNick Piggin * If the page will be shareable, see if the backing 22709637a5efSDavid Howells * address space wants to know that the page is about 227154cb8821SNick Piggin * to become writable 227254cb8821SNick Piggin */ 227369676147SMark Fasheh if (vma->vm_ops->page_mkwrite) { 227469676147SMark Fasheh unlock_page(page); 227569676147SMark Fasheh if (vma->vm_ops->page_mkwrite(vma, page) < 0) { 2276d0217ac0SNick Piggin ret = VM_FAULT_SIGBUS; 2277d0217ac0SNick Piggin anon = 1; /* no anon but release vmf.page */ 227869676147SMark Fasheh goto out_unlocked; 227969676147SMark Fasheh } 228069676147SMark Fasheh lock_page(page); 2281d0217ac0SNick Piggin /* 2282d0217ac0SNick Piggin * XXX: this is not quite right (racy vs 2283d0217ac0SNick Piggin * invalidate) to unlock and relock the page 2284d0217ac0SNick Piggin * like this, however a better fix requires 2285d0217ac0SNick Piggin * reworking page_mkwrite locking API, which 2286d0217ac0SNick Piggin * is better done later. 2287d0217ac0SNick Piggin */ 2288d0217ac0SNick Piggin if (!page->mapping) { 228983c54070SNick Piggin ret = 0; 2290d0217ac0SNick Piggin anon = 1; /* no anon but release vmf.page */ 2291d0217ac0SNick Piggin goto out; 2292d0217ac0SNick Piggin } 2293a200ee18SPeter Zijlstra page_mkwrite = 1; 22949637a5efSDavid Howells } 22959637a5efSDavid Howells } 229654cb8821SNick Piggin 22971da177e4SLinus Torvalds } 22981da177e4SLinus Torvalds 2299e1a1cd59SBalbir Singh if (mem_cgroup_charge(page, mm, GFP_KERNEL)) { 23008a9f3ccdSBalbir Singh ret = VM_FAULT_OOM; 23018a9f3ccdSBalbir Singh goto out; 23028a9f3ccdSBalbir Singh } 23038a9f3ccdSBalbir Singh 23048f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 23051da177e4SLinus Torvalds 23061da177e4SLinus Torvalds /* 23071da177e4SLinus Torvalds * This silly early PAGE_DIRTY setting removes a race 23081da177e4SLinus Torvalds * due to the bad i386 page protection. But it's valid 23091da177e4SLinus Torvalds * for other architectures too. 23101da177e4SLinus Torvalds * 23111da177e4SLinus Torvalds * Note that if write_access is true, we either now have 23121da177e4SLinus Torvalds * an exclusive copy of the page, or this is a shared mapping, 23131da177e4SLinus Torvalds * so we can make it writable and dirty to avoid having to 23141da177e4SLinus Torvalds * handle that later. 23151da177e4SLinus Torvalds */ 23161da177e4SLinus Torvalds /* Only go through if we didn't race with anybody else... */ 231754cb8821SNick Piggin if (likely(pte_same(*page_table, orig_pte))) { 2318d00806b1SNick Piggin flush_icache_page(vma, page); 2319d00806b1SNick Piggin entry = mk_pte(page, vma->vm_page_prot); 232054cb8821SNick Piggin if (flags & FAULT_FLAG_WRITE) 23211da177e4SLinus Torvalds entry = maybe_mkwrite(pte_mkdirty(entry), vma); 23221da177e4SLinus Torvalds set_pte_at(mm, address, page_table, entry); 23231da177e4SLinus Torvalds if (anon) { 23244294621fSHugh Dickins inc_mm_counter(mm, anon_rss); 2325d00806b1SNick Piggin lru_cache_add_active(page); 2326d00806b1SNick Piggin page_add_new_anon_rmap(page, vma, address); 2327f57e88a8SHugh Dickins } else { 23284294621fSHugh Dickins inc_mm_counter(mm, file_rss); 2329d00806b1SNick Piggin page_add_file_rmap(page); 233054cb8821SNick Piggin if (flags & FAULT_FLAG_WRITE) { 2331d00806b1SNick Piggin dirty_page = page; 2332d08b3851SPeter Zijlstra get_page(dirty_page); 2333d08b3851SPeter Zijlstra } 23344294621fSHugh Dickins } 23351da177e4SLinus Torvalds 2336d00806b1SNick Piggin /* no need to invalidate: a not-present page won't be cached */ 23371da177e4SLinus Torvalds update_mmu_cache(vma, address, entry); 2338d00806b1SNick Piggin } else { 23398a9f3ccdSBalbir Singh mem_cgroup_uncharge_page(page); 2340d00806b1SNick Piggin if (anon) 2341d00806b1SNick Piggin page_cache_release(page); 2342d00806b1SNick Piggin else 234354cb8821SNick Piggin anon = 1; /* no anon but release faulted_page */ 2344d00806b1SNick Piggin } 2345d00806b1SNick Piggin 23468f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 2347d00806b1SNick Piggin 2348d00806b1SNick Piggin out: 2349d0217ac0SNick Piggin unlock_page(vmf.page); 235069676147SMark Fasheh out_unlocked: 2351d00806b1SNick Piggin if (anon) 2352d0217ac0SNick Piggin page_cache_release(vmf.page); 2353d00806b1SNick Piggin else if (dirty_page) { 23548f7b3d15SAnton Salikhmetov if (vma->vm_file) 23558f7b3d15SAnton Salikhmetov file_update_time(vma->vm_file); 23568f7b3d15SAnton Salikhmetov 2357a200ee18SPeter Zijlstra set_page_dirty_balance(dirty_page, page_mkwrite); 2358d08b3851SPeter Zijlstra put_page(dirty_page); 2359d08b3851SPeter Zijlstra } 2360d00806b1SNick Piggin 236183c54070SNick Piggin return ret; 236254cb8821SNick Piggin } 2363d00806b1SNick Piggin 236454cb8821SNick Piggin static int do_linear_fault(struct mm_struct *mm, struct vm_area_struct *vma, 236554cb8821SNick Piggin unsigned long address, pte_t *page_table, pmd_t *pmd, 236654cb8821SNick Piggin int write_access, pte_t orig_pte) 236754cb8821SNick Piggin { 236854cb8821SNick Piggin pgoff_t pgoff = (((address & PAGE_MASK) 23690da7e01fSDean Nelson - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; 237054cb8821SNick Piggin unsigned int flags = (write_access ? FAULT_FLAG_WRITE : 0); 237154cb8821SNick Piggin 237216abfa08SHugh Dickins pte_unmap(page_table); 237316abfa08SHugh Dickins return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte); 237454cb8821SNick Piggin } 237554cb8821SNick Piggin 23761da177e4SLinus Torvalds 23771da177e4SLinus Torvalds /* 2378f4b81804SJes Sorensen * do_no_pfn() tries to create a new page mapping for a page without 2379f4b81804SJes Sorensen * a struct_page backing it 2380f4b81804SJes Sorensen * 2381f4b81804SJes Sorensen * As this is called only for pages that do not currently exist, we 2382f4b81804SJes Sorensen * do not need to flush old virtual caches or the TLB. 2383f4b81804SJes Sorensen * 2384f4b81804SJes Sorensen * We enter with non-exclusive mmap_sem (to exclude vma changes, 2385f4b81804SJes Sorensen * but allow concurrent faults), and pte mapped but not yet locked. 2386f4b81804SJes Sorensen * We return with mmap_sem still held, but pte unmapped and unlocked. 2387f4b81804SJes Sorensen * 2388f4b81804SJes Sorensen * It is expected that the ->nopfn handler always returns the same pfn 2389f4b81804SJes Sorensen * for a given virtual mapping. 2390f4b81804SJes Sorensen * 2391f4b81804SJes Sorensen * Mark this `noinline' to prevent it from bloating the main pagefault code. 2392f4b81804SJes Sorensen */ 2393f4b81804SJes Sorensen static noinline int do_no_pfn(struct mm_struct *mm, struct vm_area_struct *vma, 2394f4b81804SJes Sorensen unsigned long address, pte_t *page_table, pmd_t *pmd, 2395f4b81804SJes Sorensen int write_access) 2396f4b81804SJes Sorensen { 2397f4b81804SJes Sorensen spinlock_t *ptl; 2398f4b81804SJes Sorensen pte_t entry; 2399f4b81804SJes Sorensen unsigned long pfn; 2400f4b81804SJes Sorensen 2401f4b81804SJes Sorensen pte_unmap(page_table); 2402*b379d790SJared Hulbert BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))); 2403*b379d790SJared Hulbert BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); 2404f4b81804SJes Sorensen 2405f4b81804SJes Sorensen pfn = vma->vm_ops->nopfn(vma, address & PAGE_MASK); 2406*b379d790SJared Hulbert 2407*b379d790SJared Hulbert BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn)); 2408*b379d790SJared Hulbert 240922cd25edSBenjamin Herrenschmidt if (unlikely(pfn == NOPFN_OOM)) 2410f4b81804SJes Sorensen return VM_FAULT_OOM; 241122cd25edSBenjamin Herrenschmidt else if (unlikely(pfn == NOPFN_SIGBUS)) 2412f4b81804SJes Sorensen return VM_FAULT_SIGBUS; 241322cd25edSBenjamin Herrenschmidt else if (unlikely(pfn == NOPFN_REFAULT)) 241483c54070SNick Piggin return 0; 2415f4b81804SJes Sorensen 2416f4b81804SJes Sorensen page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 2417f4b81804SJes Sorensen 2418f4b81804SJes Sorensen /* Only go through if we didn't race with anybody else... */ 2419f4b81804SJes Sorensen if (pte_none(*page_table)) { 2420f4b81804SJes Sorensen entry = pfn_pte(pfn, vma->vm_page_prot); 2421f4b81804SJes Sorensen if (write_access) 2422f4b81804SJes Sorensen entry = maybe_mkwrite(pte_mkdirty(entry), vma); 2423f4b81804SJes Sorensen set_pte_at(mm, address, page_table, entry); 2424f4b81804SJes Sorensen } 2425f4b81804SJes Sorensen pte_unmap_unlock(page_table, ptl); 242683c54070SNick Piggin return 0; 2427f4b81804SJes Sorensen } 2428f4b81804SJes Sorensen 2429f4b81804SJes Sorensen /* 24301da177e4SLinus Torvalds * Fault of a previously existing named mapping. Repopulate the pte 24311da177e4SLinus Torvalds * from the encoded file_pte if possible. This enables swappable 24321da177e4SLinus Torvalds * nonlinear vmas. 24338f4e2101SHugh Dickins * 24348f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 24358f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 24368f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 24371da177e4SLinus Torvalds */ 2438d0217ac0SNick Piggin static int do_nonlinear_fault(struct mm_struct *mm, struct vm_area_struct *vma, 243965500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 244065500d23SHugh Dickins int write_access, pte_t orig_pte) 24411da177e4SLinus Torvalds { 2442d0217ac0SNick Piggin unsigned int flags = FAULT_FLAG_NONLINEAR | 2443d0217ac0SNick Piggin (write_access ? FAULT_FLAG_WRITE : 0); 244465500d23SHugh Dickins pgoff_t pgoff; 24451da177e4SLinus Torvalds 24464c21e2f2SHugh Dickins if (!pte_unmap_same(mm, pmd, page_table, orig_pte)) 244783c54070SNick Piggin return 0; 24481da177e4SLinus Torvalds 2449d0217ac0SNick Piggin if (unlikely(!(vma->vm_flags & VM_NONLINEAR) || 2450d0217ac0SNick Piggin !(vma->vm_flags & VM_CAN_NONLINEAR))) { 245165500d23SHugh Dickins /* 245265500d23SHugh Dickins * Page table corrupted: show pte and kill process. 245365500d23SHugh Dickins */ 2454b5810039SNick Piggin print_bad_pte(vma, orig_pte, address); 245565500d23SHugh Dickins return VM_FAULT_OOM; 245665500d23SHugh Dickins } 245765500d23SHugh Dickins 245865500d23SHugh Dickins pgoff = pte_to_pgoff(orig_pte); 245916abfa08SHugh Dickins return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte); 24601da177e4SLinus Torvalds } 24611da177e4SLinus Torvalds 24621da177e4SLinus Torvalds /* 24631da177e4SLinus Torvalds * These routines also need to handle stuff like marking pages dirty 24641da177e4SLinus Torvalds * and/or accessed for architectures that don't do it in hardware (most 24651da177e4SLinus Torvalds * RISC architectures). The early dirtying is also good on the i386. 24661da177e4SLinus Torvalds * 24671da177e4SLinus Torvalds * There is also a hook called "update_mmu_cache()" that architectures 24681da177e4SLinus Torvalds * with external mmu caches can use to update those (ie the Sparc or 24691da177e4SLinus Torvalds * PowerPC hashed page tables that act as extended TLBs). 24701da177e4SLinus Torvalds * 2471c74df32cSHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 2472c74df32cSHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 2473c74df32cSHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 24741da177e4SLinus Torvalds */ 24751da177e4SLinus Torvalds static inline int handle_pte_fault(struct mm_struct *mm, 24761da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long address, 247765500d23SHugh Dickins pte_t *pte, pmd_t *pmd, int write_access) 24781da177e4SLinus Torvalds { 24791da177e4SLinus Torvalds pte_t entry; 24808f4e2101SHugh Dickins spinlock_t *ptl; 24811da177e4SLinus Torvalds 24828dab5241SBenjamin Herrenschmidt entry = *pte; 24831da177e4SLinus Torvalds if (!pte_present(entry)) { 248465500d23SHugh Dickins if (pte_none(entry)) { 2485f4b81804SJes Sorensen if (vma->vm_ops) { 24863c18ddd1SNick Piggin if (likely(vma->vm_ops->fault)) 248754cb8821SNick Piggin return do_linear_fault(mm, vma, address, 248854cb8821SNick Piggin pte, pmd, write_access, entry); 2489f4b81804SJes Sorensen if (unlikely(vma->vm_ops->nopfn)) 2490f4b81804SJes Sorensen return do_no_pfn(mm, vma, address, pte, 2491f4b81804SJes Sorensen pmd, write_access); 2492f4b81804SJes Sorensen } 2493f4b81804SJes Sorensen return do_anonymous_page(mm, vma, address, 249465500d23SHugh Dickins pte, pmd, write_access); 249565500d23SHugh Dickins } 24961da177e4SLinus Torvalds if (pte_file(entry)) 2497d0217ac0SNick Piggin return do_nonlinear_fault(mm, vma, address, 249865500d23SHugh Dickins pte, pmd, write_access, entry); 249965500d23SHugh Dickins return do_swap_page(mm, vma, address, 250065500d23SHugh Dickins pte, pmd, write_access, entry); 25011da177e4SLinus Torvalds } 25021da177e4SLinus Torvalds 25034c21e2f2SHugh Dickins ptl = pte_lockptr(mm, pmd); 25048f4e2101SHugh Dickins spin_lock(ptl); 25058f4e2101SHugh Dickins if (unlikely(!pte_same(*pte, entry))) 25068f4e2101SHugh Dickins goto unlock; 25071da177e4SLinus Torvalds if (write_access) { 25081da177e4SLinus Torvalds if (!pte_write(entry)) 25098f4e2101SHugh Dickins return do_wp_page(mm, vma, address, 25108f4e2101SHugh Dickins pte, pmd, ptl, entry); 25111da177e4SLinus Torvalds entry = pte_mkdirty(entry); 25121da177e4SLinus Torvalds } 25131da177e4SLinus Torvalds entry = pte_mkyoung(entry); 25148dab5241SBenjamin Herrenschmidt if (ptep_set_access_flags(vma, address, pte, entry, write_access)) { 25151da177e4SLinus Torvalds update_mmu_cache(vma, address, entry); 25161a44e149SAndrea Arcangeli } else { 25171a44e149SAndrea Arcangeli /* 25181a44e149SAndrea Arcangeli * This is needed only for protection faults but the arch code 25191a44e149SAndrea Arcangeli * is not yet telling us if this is a protection fault or not. 25201a44e149SAndrea Arcangeli * This still avoids useless tlb flushes for .text page faults 25211a44e149SAndrea Arcangeli * with threads. 25221a44e149SAndrea Arcangeli */ 25231a44e149SAndrea Arcangeli if (write_access) 25241a44e149SAndrea Arcangeli flush_tlb_page(vma, address); 25251a44e149SAndrea Arcangeli } 25268f4e2101SHugh Dickins unlock: 25278f4e2101SHugh Dickins pte_unmap_unlock(pte, ptl); 252883c54070SNick Piggin return 0; 25291da177e4SLinus Torvalds } 25301da177e4SLinus Torvalds 25311da177e4SLinus Torvalds /* 25321da177e4SLinus Torvalds * By the time we get here, we already hold the mm semaphore 25331da177e4SLinus Torvalds */ 253483c54070SNick Piggin int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma, 25351da177e4SLinus Torvalds unsigned long address, int write_access) 25361da177e4SLinus Torvalds { 25371da177e4SLinus Torvalds pgd_t *pgd; 25381da177e4SLinus Torvalds pud_t *pud; 25391da177e4SLinus Torvalds pmd_t *pmd; 25401da177e4SLinus Torvalds pte_t *pte; 25411da177e4SLinus Torvalds 25421da177e4SLinus Torvalds __set_current_state(TASK_RUNNING); 25431da177e4SLinus Torvalds 2544f8891e5eSChristoph Lameter count_vm_event(PGFAULT); 25451da177e4SLinus Torvalds 2546ac9b9c66SHugh Dickins if (unlikely(is_vm_hugetlb_page(vma))) 2547ac9b9c66SHugh Dickins return hugetlb_fault(mm, vma, address, write_access); 25481da177e4SLinus Torvalds 25491da177e4SLinus Torvalds pgd = pgd_offset(mm, address); 25501da177e4SLinus Torvalds pud = pud_alloc(mm, pgd, address); 25511da177e4SLinus Torvalds if (!pud) 2552c74df32cSHugh Dickins return VM_FAULT_OOM; 25531da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, address); 25541da177e4SLinus Torvalds if (!pmd) 2555c74df32cSHugh Dickins return VM_FAULT_OOM; 25561da177e4SLinus Torvalds pte = pte_alloc_map(mm, pmd, address); 25571da177e4SLinus Torvalds if (!pte) 2558c74df32cSHugh Dickins return VM_FAULT_OOM; 25591da177e4SLinus Torvalds 256065500d23SHugh Dickins return handle_pte_fault(mm, vma, address, pte, pmd, write_access); 25611da177e4SLinus Torvalds } 25621da177e4SLinus Torvalds 25631da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED 25641da177e4SLinus Torvalds /* 25651da177e4SLinus Torvalds * Allocate page upper directory. 2566872fec16SHugh Dickins * We've already handled the fast-path in-line. 25671da177e4SLinus Torvalds */ 25681bb3630eSHugh Dickins int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address) 25691da177e4SLinus Torvalds { 2570c74df32cSHugh Dickins pud_t *new = pud_alloc_one(mm, address); 2571c74df32cSHugh Dickins if (!new) 25721bb3630eSHugh Dickins return -ENOMEM; 25731da177e4SLinus Torvalds 2574872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 25751bb3630eSHugh Dickins if (pgd_present(*pgd)) /* Another has populated it */ 25765e541973SBenjamin Herrenschmidt pud_free(mm, new); 25771bb3630eSHugh Dickins else 25781da177e4SLinus Torvalds pgd_populate(mm, pgd, new); 2579872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 25801bb3630eSHugh Dickins return 0; 25811da177e4SLinus Torvalds } 25821da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */ 25831da177e4SLinus Torvalds 25841da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED 25851da177e4SLinus Torvalds /* 25861da177e4SLinus Torvalds * Allocate page middle directory. 2587872fec16SHugh Dickins * We've already handled the fast-path in-line. 25881da177e4SLinus Torvalds */ 25891bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) 25901da177e4SLinus Torvalds { 2591c74df32cSHugh Dickins pmd_t *new = pmd_alloc_one(mm, address); 2592c74df32cSHugh Dickins if (!new) 25931bb3630eSHugh Dickins return -ENOMEM; 25941da177e4SLinus Torvalds 2595872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 25961da177e4SLinus Torvalds #ifndef __ARCH_HAS_4LEVEL_HACK 25971bb3630eSHugh Dickins if (pud_present(*pud)) /* Another has populated it */ 25985e541973SBenjamin Herrenschmidt pmd_free(mm, new); 25991bb3630eSHugh Dickins else 26001da177e4SLinus Torvalds pud_populate(mm, pud, new); 26011da177e4SLinus Torvalds #else 26021bb3630eSHugh Dickins if (pgd_present(*pud)) /* Another has populated it */ 26035e541973SBenjamin Herrenschmidt pmd_free(mm, new); 26041bb3630eSHugh Dickins else 26051da177e4SLinus Torvalds pgd_populate(mm, pud, new); 26061da177e4SLinus Torvalds #endif /* __ARCH_HAS_4LEVEL_HACK */ 2607872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 26081bb3630eSHugh Dickins return 0; 26091da177e4SLinus Torvalds } 26101da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */ 26111da177e4SLinus Torvalds 26121da177e4SLinus Torvalds int make_pages_present(unsigned long addr, unsigned long end) 26131da177e4SLinus Torvalds { 26141da177e4SLinus Torvalds int ret, len, write; 26151da177e4SLinus Torvalds struct vm_area_struct * vma; 26161da177e4SLinus Torvalds 26171da177e4SLinus Torvalds vma = find_vma(current->mm, addr); 26181da177e4SLinus Torvalds if (!vma) 26191da177e4SLinus Torvalds return -1; 26201da177e4SLinus Torvalds write = (vma->vm_flags & VM_WRITE) != 0; 26215bcb28b1SEric Sesterhenn BUG_ON(addr >= end); 26225bcb28b1SEric Sesterhenn BUG_ON(end > vma->vm_end); 262368e116a3SRolf Eike Beer len = DIV_ROUND_UP(end, PAGE_SIZE) - addr/PAGE_SIZE; 26241da177e4SLinus Torvalds ret = get_user_pages(current, current->mm, addr, 26251da177e4SLinus Torvalds len, write, 0, NULL, NULL); 26261da177e4SLinus Torvalds if (ret < 0) 26271da177e4SLinus Torvalds return ret; 26281da177e4SLinus Torvalds return ret == len ? 0 : -1; 26291da177e4SLinus Torvalds } 26301da177e4SLinus Torvalds 26311da177e4SLinus Torvalds #if !defined(__HAVE_ARCH_GATE_AREA) 26321da177e4SLinus Torvalds 26331da177e4SLinus Torvalds #if defined(AT_SYSINFO_EHDR) 26345ce7852cSAdrian Bunk static struct vm_area_struct gate_vma; 26351da177e4SLinus Torvalds 26361da177e4SLinus Torvalds static int __init gate_vma_init(void) 26371da177e4SLinus Torvalds { 26381da177e4SLinus Torvalds gate_vma.vm_mm = NULL; 26391da177e4SLinus Torvalds gate_vma.vm_start = FIXADDR_USER_START; 26401da177e4SLinus Torvalds gate_vma.vm_end = FIXADDR_USER_END; 2641b6558c4aSRoland McGrath gate_vma.vm_flags = VM_READ | VM_MAYREAD | VM_EXEC | VM_MAYEXEC; 2642b6558c4aSRoland McGrath gate_vma.vm_page_prot = __P101; 2643f47aef55SRoland McGrath /* 2644f47aef55SRoland McGrath * Make sure the vDSO gets into every core dump. 2645f47aef55SRoland McGrath * Dumping its contents makes post-mortem fully interpretable later 2646f47aef55SRoland McGrath * without matching up the same kernel and hardware config to see 2647f47aef55SRoland McGrath * what PC values meant. 2648f47aef55SRoland McGrath */ 2649f47aef55SRoland McGrath gate_vma.vm_flags |= VM_ALWAYSDUMP; 26501da177e4SLinus Torvalds return 0; 26511da177e4SLinus Torvalds } 26521da177e4SLinus Torvalds __initcall(gate_vma_init); 26531da177e4SLinus Torvalds #endif 26541da177e4SLinus Torvalds 26551da177e4SLinus Torvalds struct vm_area_struct *get_gate_vma(struct task_struct *tsk) 26561da177e4SLinus Torvalds { 26571da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR 26581da177e4SLinus Torvalds return &gate_vma; 26591da177e4SLinus Torvalds #else 26601da177e4SLinus Torvalds return NULL; 26611da177e4SLinus Torvalds #endif 26621da177e4SLinus Torvalds } 26631da177e4SLinus Torvalds 26641da177e4SLinus Torvalds int in_gate_area_no_task(unsigned long addr) 26651da177e4SLinus Torvalds { 26661da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR 26671da177e4SLinus Torvalds if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END)) 26681da177e4SLinus Torvalds return 1; 26691da177e4SLinus Torvalds #endif 26701da177e4SLinus Torvalds return 0; 26711da177e4SLinus Torvalds } 26721da177e4SLinus Torvalds 26731da177e4SLinus Torvalds #endif /* __HAVE_ARCH_GATE_AREA */ 26740ec76a11SDavid Howells 26750ec76a11SDavid Howells /* 26760ec76a11SDavid Howells * Access another process' address space. 26770ec76a11SDavid Howells * Source/target buffer must be kernel space, 26780ec76a11SDavid Howells * Do not walk the page table directly, use get_user_pages 26790ec76a11SDavid Howells */ 26800ec76a11SDavid Howells int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write) 26810ec76a11SDavid Howells { 26820ec76a11SDavid Howells struct mm_struct *mm; 26830ec76a11SDavid Howells struct vm_area_struct *vma; 26840ec76a11SDavid Howells struct page *page; 26850ec76a11SDavid Howells void *old_buf = buf; 26860ec76a11SDavid Howells 26870ec76a11SDavid Howells mm = get_task_mm(tsk); 26880ec76a11SDavid Howells if (!mm) 26890ec76a11SDavid Howells return 0; 26900ec76a11SDavid Howells 26910ec76a11SDavid Howells down_read(&mm->mmap_sem); 2692183ff22bSSimon Arlott /* ignore errors, just check how much was successfully transferred */ 26930ec76a11SDavid Howells while (len) { 26940ec76a11SDavid Howells int bytes, ret, offset; 26950ec76a11SDavid Howells void *maddr; 26960ec76a11SDavid Howells 26970ec76a11SDavid Howells ret = get_user_pages(tsk, mm, addr, 1, 26980ec76a11SDavid Howells write, 1, &page, &vma); 26990ec76a11SDavid Howells if (ret <= 0) 27000ec76a11SDavid Howells break; 27010ec76a11SDavid Howells 27020ec76a11SDavid Howells bytes = len; 27030ec76a11SDavid Howells offset = addr & (PAGE_SIZE-1); 27040ec76a11SDavid Howells if (bytes > PAGE_SIZE-offset) 27050ec76a11SDavid Howells bytes = PAGE_SIZE-offset; 27060ec76a11SDavid Howells 27070ec76a11SDavid Howells maddr = kmap(page); 27080ec76a11SDavid Howells if (write) { 27090ec76a11SDavid Howells copy_to_user_page(vma, page, addr, 27100ec76a11SDavid Howells maddr + offset, buf, bytes); 27110ec76a11SDavid Howells set_page_dirty_lock(page); 27120ec76a11SDavid Howells } else { 27130ec76a11SDavid Howells copy_from_user_page(vma, page, addr, 27140ec76a11SDavid Howells buf, maddr + offset, bytes); 27150ec76a11SDavid Howells } 27160ec76a11SDavid Howells kunmap(page); 27170ec76a11SDavid Howells page_cache_release(page); 27180ec76a11SDavid Howells len -= bytes; 27190ec76a11SDavid Howells buf += bytes; 27200ec76a11SDavid Howells addr += bytes; 27210ec76a11SDavid Howells } 27220ec76a11SDavid Howells up_read(&mm->mmap_sem); 27230ec76a11SDavid Howells mmput(mm); 27240ec76a11SDavid Howells 27250ec76a11SDavid Howells return buf - old_buf; 27260ec76a11SDavid Howells } 272703252919SAndi Kleen 272803252919SAndi Kleen /* 272903252919SAndi Kleen * Print the name of a VMA. 273003252919SAndi Kleen */ 273103252919SAndi Kleen void print_vma_addr(char *prefix, unsigned long ip) 273203252919SAndi Kleen { 273303252919SAndi Kleen struct mm_struct *mm = current->mm; 273403252919SAndi Kleen struct vm_area_struct *vma; 273503252919SAndi Kleen 2736e8bff74aSIngo Molnar /* 2737e8bff74aSIngo Molnar * Do not print if we are in atomic 2738e8bff74aSIngo Molnar * contexts (in exception stacks, etc.): 2739e8bff74aSIngo Molnar */ 2740e8bff74aSIngo Molnar if (preempt_count()) 2741e8bff74aSIngo Molnar return; 2742e8bff74aSIngo Molnar 274303252919SAndi Kleen down_read(&mm->mmap_sem); 274403252919SAndi Kleen vma = find_vma(mm, ip); 274503252919SAndi Kleen if (vma && vma->vm_file) { 274603252919SAndi Kleen struct file *f = vma->vm_file; 274703252919SAndi Kleen char *buf = (char *)__get_free_page(GFP_KERNEL); 274803252919SAndi Kleen if (buf) { 274903252919SAndi Kleen char *p, *s; 275003252919SAndi Kleen 2751cf28b486SJan Blunck p = d_path(&f->f_path, buf, PAGE_SIZE); 275203252919SAndi Kleen if (IS_ERR(p)) 275303252919SAndi Kleen p = "?"; 275403252919SAndi Kleen s = strrchr(p, '/'); 275503252919SAndi Kleen if (s) 275603252919SAndi Kleen p = s+1; 275703252919SAndi Kleen printk("%s%s[%lx+%lx]", prefix, p, 275803252919SAndi Kleen vma->vm_start, 275903252919SAndi Kleen vma->vm_end - vma->vm_start); 276003252919SAndi Kleen free_page((unsigned long)buf); 276103252919SAndi Kleen } 276203252919SAndi Kleen } 276303252919SAndi Kleen up_read(¤t->mm->mmap_sem); 276403252919SAndi Kleen } 2765