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*7e675137SNick Piggin * vm_normal_page -- This function gets the "struct page" associated with a pte. 3756aab341eSLinus Torvalds * 376*7e675137SNick Piggin * "Special" mappings do not wish to be associated with a "struct page" (either 377*7e675137SNick Piggin * it doesn't exist, or it exists but they don't want to touch it). In this 378*7e675137SNick Piggin * case, NULL is returned here. "Normal" mappings do have a struct page. 379b379d790SJared Hulbert * 380*7e675137SNick Piggin * There are 2 broad cases. Firstly, an architecture may define a pte_special() 381*7e675137SNick Piggin * pte bit, in which case this function is trivial. Secondly, an architecture 382*7e675137SNick Piggin * may not have a spare pte bit, which requires a more complicated scheme, 383*7e675137SNick Piggin * described below. 384*7e675137SNick Piggin * 385*7e675137SNick Piggin * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a 386*7e675137SNick Piggin * special mapping (even if there are underlying and valid "struct pages"). 387*7e675137SNick Piggin * COWed pages of a VM_PFNMAP are always normal. 3886aab341eSLinus Torvalds * 389b379d790SJared Hulbert * The way we recognize COWed pages within VM_PFNMAP mappings is through the 390b379d790SJared Hulbert * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit 391*7e675137SNick Piggin * set, and the vm_pgoff will point to the first PFN mapped: thus every special 392*7e675137SNick Piggin * mapping will always honor the rule 3936aab341eSLinus Torvalds * 3946aab341eSLinus Torvalds * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT) 3956aab341eSLinus Torvalds * 396*7e675137SNick Piggin * And for normal mappings this is false. 397b379d790SJared Hulbert * 398*7e675137SNick Piggin * This restricts such mappings to be a linear translation from virtual address 399*7e675137SNick Piggin * to pfn. To get around this restriction, we allow arbitrary mappings so long 400*7e675137SNick Piggin * as the vma is not a COW mapping; in that case, we know that all ptes are 401*7e675137SNick Piggin * special (because none can have been COWed). 402b379d790SJared Hulbert * 403b379d790SJared Hulbert * 404*7e675137SNick Piggin * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP. 405*7e675137SNick Piggin * 406b379d790SJared Hulbert * VM_MIXEDMAP mappings can likewise contain memory with or without "struct 407b379d790SJared Hulbert * page" backing, however the difference is that _all_ pages with a struct 408b379d790SJared Hulbert * page (that is, those where pfn_valid is true) are refcounted and considered 409b379d790SJared Hulbert * normal pages by the VM. The disadvantage is that pages are refcounted 410b379d790SJared Hulbert * (which can be slower and simply not an option for some PFNMAP users). The 411b379d790SJared Hulbert * advantage is that we don't have to follow the strict linearity rule of 412b379d790SJared Hulbert * PFNMAP mappings in order to support COWable mappings. 413b379d790SJared Hulbert * 414ee498ed7SHugh Dickins */ 415*7e675137SNick Piggin #ifdef __HAVE_ARCH_PTE_SPECIAL 416*7e675137SNick Piggin # define HAVE_PTE_SPECIAL 1 417*7e675137SNick Piggin #else 418*7e675137SNick Piggin # define HAVE_PTE_SPECIAL 0 419*7e675137SNick Piggin #endif 420*7e675137SNick Piggin struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, 421*7e675137SNick Piggin pte_t pte) 422ee498ed7SHugh Dickins { 423*7e675137SNick Piggin unsigned long pfn; 424*7e675137SNick Piggin 425*7e675137SNick Piggin if (HAVE_PTE_SPECIAL) { 426*7e675137SNick Piggin if (likely(!pte_special(pte))) { 427*7e675137SNick Piggin VM_BUG_ON(!pfn_valid(pte_pfn(pte))); 428*7e675137SNick Piggin return pte_page(pte); 429*7e675137SNick Piggin } 430*7e675137SNick Piggin VM_BUG_ON(!(vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))); 431*7e675137SNick Piggin return NULL; 432*7e675137SNick Piggin } 433*7e675137SNick Piggin 434*7e675137SNick Piggin /* !HAVE_PTE_SPECIAL case follows: */ 435*7e675137SNick Piggin 436*7e675137SNick Piggin pfn = pte_pfn(pte); 4376aab341eSLinus Torvalds 438b379d790SJared Hulbert if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { 439b379d790SJared Hulbert if (vma->vm_flags & VM_MIXEDMAP) { 440b379d790SJared Hulbert if (!pfn_valid(pfn)) 441b379d790SJared Hulbert return NULL; 442b379d790SJared Hulbert goto out; 443b379d790SJared Hulbert } else { 444*7e675137SNick Piggin unsigned long off; 445*7e675137SNick Piggin off = (addr - vma->vm_start) >> PAGE_SHIFT; 4466aab341eSLinus Torvalds if (pfn == vma->vm_pgoff + off) 4476aab341eSLinus Torvalds return NULL; 44867121172SLinus Torvalds if (!is_cow_mapping(vma->vm_flags)) 449fb155c16SLinus Torvalds return NULL; 4506aab341eSLinus Torvalds } 451b379d790SJared Hulbert } 4526aab341eSLinus Torvalds 453*7e675137SNick Piggin VM_BUG_ON(!pfn_valid(pfn)); 4546aab341eSLinus Torvalds 4556aab341eSLinus Torvalds /* 456*7e675137SNick Piggin * NOTE! We still have PageReserved() pages in the page tables. 4576aab341eSLinus Torvalds * 458*7e675137SNick Piggin * eg. VDSO mappings can cause them to exist. 4596aab341eSLinus Torvalds */ 460b379d790SJared Hulbert out: 4616aab341eSLinus Torvalds return pfn_to_page(pfn); 462ee498ed7SHugh Dickins } 463ee498ed7SHugh Dickins 464ee498ed7SHugh Dickins /* 4651da177e4SLinus Torvalds * copy one vm_area from one task to the other. Assumes the page tables 4661da177e4SLinus Torvalds * already present in the new task to be cleared in the whole range 4671da177e4SLinus Torvalds * covered by this vma. 4681da177e4SLinus Torvalds */ 4691da177e4SLinus Torvalds 4708c103762SHugh Dickins static inline void 4711da177e4SLinus Torvalds copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, 472b5810039SNick Piggin pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma, 4738c103762SHugh Dickins unsigned long addr, int *rss) 4741da177e4SLinus Torvalds { 475b5810039SNick Piggin unsigned long vm_flags = vma->vm_flags; 4761da177e4SLinus Torvalds pte_t pte = *src_pte; 4771da177e4SLinus Torvalds struct page *page; 4781da177e4SLinus Torvalds 4791da177e4SLinus Torvalds /* pte contains position in swap or file, so copy. */ 4801da177e4SLinus Torvalds if (unlikely(!pte_present(pte))) { 4811da177e4SLinus Torvalds if (!pte_file(pte)) { 4820697212aSChristoph Lameter swp_entry_t entry = pte_to_swp_entry(pte); 4830697212aSChristoph Lameter 4840697212aSChristoph Lameter swap_duplicate(entry); 4851da177e4SLinus Torvalds /* make sure dst_mm is on swapoff's mmlist. */ 4861da177e4SLinus Torvalds if (unlikely(list_empty(&dst_mm->mmlist))) { 4871da177e4SLinus Torvalds spin_lock(&mmlist_lock); 488f412ac08SHugh Dickins if (list_empty(&dst_mm->mmlist)) 489f412ac08SHugh Dickins list_add(&dst_mm->mmlist, 490f412ac08SHugh Dickins &src_mm->mmlist); 4911da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 4921da177e4SLinus Torvalds } 4930697212aSChristoph Lameter if (is_write_migration_entry(entry) && 4940697212aSChristoph Lameter is_cow_mapping(vm_flags)) { 4950697212aSChristoph Lameter /* 4960697212aSChristoph Lameter * COW mappings require pages in both parent 4970697212aSChristoph Lameter * and child to be set to read. 4980697212aSChristoph Lameter */ 4990697212aSChristoph Lameter make_migration_entry_read(&entry); 5000697212aSChristoph Lameter pte = swp_entry_to_pte(entry); 5010697212aSChristoph Lameter set_pte_at(src_mm, addr, src_pte, pte); 5020697212aSChristoph Lameter } 5031da177e4SLinus Torvalds } 504ae859762SHugh Dickins goto out_set_pte; 5051da177e4SLinus Torvalds } 5061da177e4SLinus Torvalds 5071da177e4SLinus Torvalds /* 5081da177e4SLinus Torvalds * If it's a COW mapping, write protect it both 5091da177e4SLinus Torvalds * in the parent and the child 5101da177e4SLinus Torvalds */ 51167121172SLinus Torvalds if (is_cow_mapping(vm_flags)) { 5121da177e4SLinus Torvalds ptep_set_wrprotect(src_mm, addr, src_pte); 5133dc90795SZachary Amsden pte = pte_wrprotect(pte); 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds /* 5171da177e4SLinus Torvalds * If it's a shared mapping, mark it clean in 5181da177e4SLinus Torvalds * the child 5191da177e4SLinus Torvalds */ 5201da177e4SLinus Torvalds if (vm_flags & VM_SHARED) 5211da177e4SLinus Torvalds pte = pte_mkclean(pte); 5221da177e4SLinus Torvalds pte = pte_mkold(pte); 5236aab341eSLinus Torvalds 5246aab341eSLinus Torvalds page = vm_normal_page(vma, addr, pte); 5256aab341eSLinus Torvalds if (page) { 5261da177e4SLinus Torvalds get_page(page); 527c97a9e10SNick Piggin page_dup_rmap(page, vma, addr); 5288c103762SHugh Dickins rss[!!PageAnon(page)]++; 5296aab341eSLinus Torvalds } 530ae859762SHugh Dickins 531ae859762SHugh Dickins out_set_pte: 532ae859762SHugh Dickins set_pte_at(dst_mm, addr, dst_pte, pte); 5331da177e4SLinus Torvalds } 5341da177e4SLinus Torvalds 5351da177e4SLinus Torvalds static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 5361da177e4SLinus Torvalds pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma, 5371da177e4SLinus Torvalds unsigned long addr, unsigned long end) 5381da177e4SLinus Torvalds { 5391da177e4SLinus Torvalds pte_t *src_pte, *dst_pte; 540c74df32cSHugh Dickins spinlock_t *src_ptl, *dst_ptl; 541e040f218SHugh Dickins int progress = 0; 5428c103762SHugh Dickins int rss[2]; 5431da177e4SLinus Torvalds 5441da177e4SLinus Torvalds again: 545ae859762SHugh Dickins rss[1] = rss[0] = 0; 546c74df32cSHugh Dickins dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl); 5471da177e4SLinus Torvalds if (!dst_pte) 5481da177e4SLinus Torvalds return -ENOMEM; 5491da177e4SLinus Torvalds src_pte = pte_offset_map_nested(src_pmd, addr); 5504c21e2f2SHugh Dickins src_ptl = pte_lockptr(src_mm, src_pmd); 551f20dc5f7SIngo Molnar spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 5526606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 5531da177e4SLinus Torvalds 5541da177e4SLinus Torvalds do { 5551da177e4SLinus Torvalds /* 5561da177e4SLinus Torvalds * We are holding two locks at this point - either of them 5571da177e4SLinus Torvalds * could generate latencies in another task on another CPU. 5581da177e4SLinus Torvalds */ 559e040f218SHugh Dickins if (progress >= 32) { 560e040f218SHugh Dickins progress = 0; 561e040f218SHugh Dickins if (need_resched() || 56295c354feSNick Piggin spin_needbreak(src_ptl) || spin_needbreak(dst_ptl)) 5631da177e4SLinus Torvalds break; 564e040f218SHugh Dickins } 5651da177e4SLinus Torvalds if (pte_none(*src_pte)) { 5661da177e4SLinus Torvalds progress++; 5671da177e4SLinus Torvalds continue; 5681da177e4SLinus Torvalds } 5698c103762SHugh Dickins copy_one_pte(dst_mm, src_mm, dst_pte, src_pte, vma, addr, rss); 5701da177e4SLinus Torvalds progress += 8; 5711da177e4SLinus Torvalds } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end); 5721da177e4SLinus Torvalds 5736606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 574c74df32cSHugh Dickins spin_unlock(src_ptl); 5751da177e4SLinus Torvalds pte_unmap_nested(src_pte - 1); 576ae859762SHugh Dickins add_mm_rss(dst_mm, rss[0], rss[1]); 577c74df32cSHugh Dickins pte_unmap_unlock(dst_pte - 1, dst_ptl); 578c74df32cSHugh Dickins cond_resched(); 5791da177e4SLinus Torvalds if (addr != end) 5801da177e4SLinus Torvalds goto again; 5811da177e4SLinus Torvalds return 0; 5821da177e4SLinus Torvalds } 5831da177e4SLinus Torvalds 5841da177e4SLinus Torvalds static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 5851da177e4SLinus Torvalds pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma, 5861da177e4SLinus Torvalds unsigned long addr, unsigned long end) 5871da177e4SLinus Torvalds { 5881da177e4SLinus Torvalds pmd_t *src_pmd, *dst_pmd; 5891da177e4SLinus Torvalds unsigned long next; 5901da177e4SLinus Torvalds 5911da177e4SLinus Torvalds dst_pmd = pmd_alloc(dst_mm, dst_pud, addr); 5921da177e4SLinus Torvalds if (!dst_pmd) 5931da177e4SLinus Torvalds return -ENOMEM; 5941da177e4SLinus Torvalds src_pmd = pmd_offset(src_pud, addr); 5951da177e4SLinus Torvalds do { 5961da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 5971da177e4SLinus Torvalds if (pmd_none_or_clear_bad(src_pmd)) 5981da177e4SLinus Torvalds continue; 5991da177e4SLinus Torvalds if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd, 6001da177e4SLinus Torvalds vma, addr, next)) 6011da177e4SLinus Torvalds return -ENOMEM; 6021da177e4SLinus Torvalds } while (dst_pmd++, src_pmd++, addr = next, addr != end); 6031da177e4SLinus Torvalds return 0; 6041da177e4SLinus Torvalds } 6051da177e4SLinus Torvalds 6061da177e4SLinus Torvalds static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 6071da177e4SLinus Torvalds pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma, 6081da177e4SLinus Torvalds unsigned long addr, unsigned long end) 6091da177e4SLinus Torvalds { 6101da177e4SLinus Torvalds pud_t *src_pud, *dst_pud; 6111da177e4SLinus Torvalds unsigned long next; 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds dst_pud = pud_alloc(dst_mm, dst_pgd, addr); 6141da177e4SLinus Torvalds if (!dst_pud) 6151da177e4SLinus Torvalds return -ENOMEM; 6161da177e4SLinus Torvalds src_pud = pud_offset(src_pgd, addr); 6171da177e4SLinus Torvalds do { 6181da177e4SLinus Torvalds next = pud_addr_end(addr, end); 6191da177e4SLinus Torvalds if (pud_none_or_clear_bad(src_pud)) 6201da177e4SLinus Torvalds continue; 6211da177e4SLinus Torvalds if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud, 6221da177e4SLinus Torvalds vma, addr, next)) 6231da177e4SLinus Torvalds return -ENOMEM; 6241da177e4SLinus Torvalds } while (dst_pud++, src_pud++, addr = next, addr != end); 6251da177e4SLinus Torvalds return 0; 6261da177e4SLinus Torvalds } 6271da177e4SLinus Torvalds 6281da177e4SLinus Torvalds int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 6291da177e4SLinus Torvalds struct vm_area_struct *vma) 6301da177e4SLinus Torvalds { 6311da177e4SLinus Torvalds pgd_t *src_pgd, *dst_pgd; 6321da177e4SLinus Torvalds unsigned long next; 6331da177e4SLinus Torvalds unsigned long addr = vma->vm_start; 6341da177e4SLinus Torvalds unsigned long end = vma->vm_end; 6351da177e4SLinus Torvalds 636d992895bSNick Piggin /* 637d992895bSNick Piggin * Don't copy ptes where a page fault will fill them correctly. 638d992895bSNick Piggin * Fork becomes much lighter when there are big shared or private 639d992895bSNick Piggin * readonly mappings. The tradeoff is that copy_page_range is more 640d992895bSNick Piggin * efficient than faulting. 641d992895bSNick Piggin */ 6424d7672b4SLinus Torvalds if (!(vma->vm_flags & (VM_HUGETLB|VM_NONLINEAR|VM_PFNMAP|VM_INSERTPAGE))) { 643d992895bSNick Piggin if (!vma->anon_vma) 644d992895bSNick Piggin return 0; 645d992895bSNick Piggin } 646d992895bSNick Piggin 6471da177e4SLinus Torvalds if (is_vm_hugetlb_page(vma)) 6481da177e4SLinus Torvalds return copy_hugetlb_page_range(dst_mm, src_mm, vma); 6491da177e4SLinus Torvalds 6501da177e4SLinus Torvalds dst_pgd = pgd_offset(dst_mm, addr); 6511da177e4SLinus Torvalds src_pgd = pgd_offset(src_mm, addr); 6521da177e4SLinus Torvalds do { 6531da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 6541da177e4SLinus Torvalds if (pgd_none_or_clear_bad(src_pgd)) 6551da177e4SLinus Torvalds continue; 6561da177e4SLinus Torvalds if (copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd, 6571da177e4SLinus Torvalds vma, addr, next)) 6581da177e4SLinus Torvalds return -ENOMEM; 6591da177e4SLinus Torvalds } while (dst_pgd++, src_pgd++, addr = next, addr != end); 6601da177e4SLinus Torvalds return 0; 6611da177e4SLinus Torvalds } 6621da177e4SLinus Torvalds 66351c6f666SRobin Holt static unsigned long zap_pte_range(struct mmu_gather *tlb, 664b5810039SNick Piggin struct vm_area_struct *vma, pmd_t *pmd, 6651da177e4SLinus Torvalds unsigned long addr, unsigned long end, 66651c6f666SRobin Holt long *zap_work, struct zap_details *details) 6671da177e4SLinus Torvalds { 668b5810039SNick Piggin struct mm_struct *mm = tlb->mm; 6691da177e4SLinus Torvalds pte_t *pte; 670508034a3SHugh Dickins spinlock_t *ptl; 671ae859762SHugh Dickins int file_rss = 0; 672ae859762SHugh Dickins int anon_rss = 0; 6731da177e4SLinus Torvalds 674508034a3SHugh Dickins pte = pte_offset_map_lock(mm, pmd, addr, &ptl); 6756606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 6761da177e4SLinus Torvalds do { 6771da177e4SLinus Torvalds pte_t ptent = *pte; 67851c6f666SRobin Holt if (pte_none(ptent)) { 67951c6f666SRobin Holt (*zap_work)--; 6801da177e4SLinus Torvalds continue; 68151c6f666SRobin Holt } 68251c6f666SRobin Holt 68351c6f666SRobin Holt (*zap_work) -= PAGE_SIZE; 68451c6f666SRobin Holt 6856f5e6b9eSHugh Dickins if (pte_present(ptent)) { 6866f5e6b9eSHugh Dickins struct page *page; 6876f5e6b9eSHugh Dickins 6886aab341eSLinus Torvalds page = vm_normal_page(vma, addr, ptent); 6891da177e4SLinus Torvalds if (unlikely(details) && page) { 6901da177e4SLinus Torvalds /* 6911da177e4SLinus Torvalds * unmap_shared_mapping_pages() wants to 6921da177e4SLinus Torvalds * invalidate cache without truncating: 6931da177e4SLinus Torvalds * unmap shared but keep private pages. 6941da177e4SLinus Torvalds */ 6951da177e4SLinus Torvalds if (details->check_mapping && 6961da177e4SLinus Torvalds details->check_mapping != page->mapping) 6971da177e4SLinus Torvalds continue; 6981da177e4SLinus Torvalds /* 6991da177e4SLinus Torvalds * Each page->index must be checked when 7001da177e4SLinus Torvalds * invalidating or truncating nonlinear. 7011da177e4SLinus Torvalds */ 7021da177e4SLinus Torvalds if (details->nonlinear_vma && 7031da177e4SLinus Torvalds (page->index < details->first_index || 7041da177e4SLinus Torvalds page->index > details->last_index)) 7051da177e4SLinus Torvalds continue; 7061da177e4SLinus Torvalds } 707b5810039SNick Piggin ptent = ptep_get_and_clear_full(mm, addr, pte, 708a600388dSZachary Amsden tlb->fullmm); 7091da177e4SLinus Torvalds tlb_remove_tlb_entry(tlb, pte, addr); 7101da177e4SLinus Torvalds if (unlikely(!page)) 7111da177e4SLinus Torvalds continue; 7121da177e4SLinus Torvalds if (unlikely(details) && details->nonlinear_vma 7131da177e4SLinus Torvalds && linear_page_index(details->nonlinear_vma, 7141da177e4SLinus Torvalds addr) != page->index) 715b5810039SNick Piggin set_pte_at(mm, addr, pte, 7161da177e4SLinus Torvalds pgoff_to_pte(page->index)); 7171da177e4SLinus Torvalds if (PageAnon(page)) 71886d912f4SHugh Dickins anon_rss--; 7196237bcd9SHugh Dickins else { 7206237bcd9SHugh Dickins if (pte_dirty(ptent)) 7216237bcd9SHugh Dickins set_page_dirty(page); 7226237bcd9SHugh Dickins if (pte_young(ptent)) 723daa88c8dSKen Chen SetPageReferenced(page); 72486d912f4SHugh Dickins file_rss--; 7256237bcd9SHugh Dickins } 7267de6b805SNick Piggin page_remove_rmap(page, vma); 7271da177e4SLinus Torvalds tlb_remove_page(tlb, page); 7281da177e4SLinus Torvalds continue; 7291da177e4SLinus Torvalds } 7301da177e4SLinus Torvalds /* 7311da177e4SLinus Torvalds * If details->check_mapping, we leave swap entries; 7321da177e4SLinus Torvalds * if details->nonlinear_vma, we leave file entries. 7331da177e4SLinus Torvalds */ 7341da177e4SLinus Torvalds if (unlikely(details)) 7351da177e4SLinus Torvalds continue; 7361da177e4SLinus Torvalds if (!pte_file(ptent)) 7371da177e4SLinus Torvalds free_swap_and_cache(pte_to_swp_entry(ptent)); 7389888a1caSZachary Amsden pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); 73951c6f666SRobin Holt } while (pte++, addr += PAGE_SIZE, (addr != end && *zap_work > 0)); 740ae859762SHugh Dickins 74186d912f4SHugh Dickins add_mm_rss(mm, file_rss, anon_rss); 7426606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 743508034a3SHugh Dickins pte_unmap_unlock(pte - 1, ptl); 74451c6f666SRobin Holt 74551c6f666SRobin Holt return addr; 7461da177e4SLinus Torvalds } 7471da177e4SLinus Torvalds 74851c6f666SRobin Holt static inline unsigned long zap_pmd_range(struct mmu_gather *tlb, 749b5810039SNick Piggin struct vm_area_struct *vma, pud_t *pud, 7501da177e4SLinus Torvalds unsigned long addr, unsigned long end, 75151c6f666SRobin Holt long *zap_work, struct zap_details *details) 7521da177e4SLinus Torvalds { 7531da177e4SLinus Torvalds pmd_t *pmd; 7541da177e4SLinus Torvalds unsigned long next; 7551da177e4SLinus Torvalds 7561da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 7571da177e4SLinus Torvalds do { 7581da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 75951c6f666SRobin Holt if (pmd_none_or_clear_bad(pmd)) { 76051c6f666SRobin Holt (*zap_work)--; 7611da177e4SLinus Torvalds continue; 76251c6f666SRobin Holt } 76351c6f666SRobin Holt next = zap_pte_range(tlb, vma, pmd, addr, next, 76451c6f666SRobin Holt zap_work, details); 76551c6f666SRobin Holt } while (pmd++, addr = next, (addr != end && *zap_work > 0)); 76651c6f666SRobin Holt 76751c6f666SRobin Holt return addr; 7681da177e4SLinus Torvalds } 7691da177e4SLinus Torvalds 77051c6f666SRobin Holt static inline unsigned long zap_pud_range(struct mmu_gather *tlb, 771b5810039SNick Piggin struct vm_area_struct *vma, pgd_t *pgd, 7721da177e4SLinus Torvalds unsigned long addr, unsigned long end, 77351c6f666SRobin Holt long *zap_work, struct zap_details *details) 7741da177e4SLinus Torvalds { 7751da177e4SLinus Torvalds pud_t *pud; 7761da177e4SLinus Torvalds unsigned long next; 7771da177e4SLinus Torvalds 7781da177e4SLinus Torvalds pud = pud_offset(pgd, addr); 7791da177e4SLinus Torvalds do { 7801da177e4SLinus Torvalds next = pud_addr_end(addr, end); 78151c6f666SRobin Holt if (pud_none_or_clear_bad(pud)) { 78251c6f666SRobin Holt (*zap_work)--; 7831da177e4SLinus Torvalds continue; 78451c6f666SRobin Holt } 78551c6f666SRobin Holt next = zap_pmd_range(tlb, vma, pud, addr, next, 78651c6f666SRobin Holt zap_work, details); 78751c6f666SRobin Holt } while (pud++, addr = next, (addr != end && *zap_work > 0)); 78851c6f666SRobin Holt 78951c6f666SRobin Holt return addr; 7901da177e4SLinus Torvalds } 7911da177e4SLinus Torvalds 79251c6f666SRobin Holt static unsigned long unmap_page_range(struct mmu_gather *tlb, 79351c6f666SRobin Holt struct vm_area_struct *vma, 7941da177e4SLinus Torvalds unsigned long addr, unsigned long end, 79551c6f666SRobin Holt long *zap_work, struct zap_details *details) 7961da177e4SLinus Torvalds { 7971da177e4SLinus Torvalds pgd_t *pgd; 7981da177e4SLinus Torvalds unsigned long next; 7991da177e4SLinus Torvalds 8001da177e4SLinus Torvalds if (details && !details->check_mapping && !details->nonlinear_vma) 8011da177e4SLinus Torvalds details = NULL; 8021da177e4SLinus Torvalds 8031da177e4SLinus Torvalds BUG_ON(addr >= end); 8041da177e4SLinus Torvalds tlb_start_vma(tlb, vma); 8051da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 8061da177e4SLinus Torvalds do { 8071da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 80851c6f666SRobin Holt if (pgd_none_or_clear_bad(pgd)) { 80951c6f666SRobin Holt (*zap_work)--; 8101da177e4SLinus Torvalds continue; 81151c6f666SRobin Holt } 81251c6f666SRobin Holt next = zap_pud_range(tlb, vma, pgd, addr, next, 81351c6f666SRobin Holt zap_work, details); 81451c6f666SRobin Holt } while (pgd++, addr = next, (addr != end && *zap_work > 0)); 8151da177e4SLinus Torvalds tlb_end_vma(tlb, vma); 81651c6f666SRobin Holt 81751c6f666SRobin Holt return addr; 8181da177e4SLinus Torvalds } 8191da177e4SLinus Torvalds 8201da177e4SLinus Torvalds #ifdef CONFIG_PREEMPT 8211da177e4SLinus Torvalds # define ZAP_BLOCK_SIZE (8 * PAGE_SIZE) 8221da177e4SLinus Torvalds #else 8231da177e4SLinus Torvalds /* No preempt: go for improved straight-line efficiency */ 8241da177e4SLinus Torvalds # define ZAP_BLOCK_SIZE (1024 * PAGE_SIZE) 8251da177e4SLinus Torvalds #endif 8261da177e4SLinus Torvalds 8271da177e4SLinus Torvalds /** 8281da177e4SLinus Torvalds * unmap_vmas - unmap a range of memory covered by a list of vma's 8291da177e4SLinus Torvalds * @tlbp: address of the caller's struct mmu_gather 8301da177e4SLinus Torvalds * @vma: the starting vma 8311da177e4SLinus Torvalds * @start_addr: virtual address at which to start unmapping 8321da177e4SLinus Torvalds * @end_addr: virtual address at which to end unmapping 8331da177e4SLinus Torvalds * @nr_accounted: Place number of unmapped pages in vm-accountable vma's here 8341da177e4SLinus Torvalds * @details: details of nonlinear truncation or shared cache invalidation 8351da177e4SLinus Torvalds * 836ee39b37bSHugh Dickins * Returns the end address of the unmapping (restart addr if interrupted). 8371da177e4SLinus Torvalds * 838508034a3SHugh Dickins * Unmap all pages in the vma list. 8391da177e4SLinus Torvalds * 840508034a3SHugh Dickins * We aim to not hold locks for too long (for scheduling latency reasons). 841508034a3SHugh Dickins * So zap pages in ZAP_BLOCK_SIZE bytecounts. This means we need to 8421da177e4SLinus Torvalds * return the ending mmu_gather to the caller. 8431da177e4SLinus Torvalds * 8441da177e4SLinus Torvalds * Only addresses between `start' and `end' will be unmapped. 8451da177e4SLinus Torvalds * 8461da177e4SLinus Torvalds * The VMA list must be sorted in ascending virtual address order. 8471da177e4SLinus Torvalds * 8481da177e4SLinus Torvalds * unmap_vmas() assumes that the caller will flush the whole unmapped address 8491da177e4SLinus Torvalds * range after unmap_vmas() returns. So the only responsibility here is to 8501da177e4SLinus Torvalds * ensure that any thus-far unmapped pages are flushed before unmap_vmas() 8511da177e4SLinus Torvalds * drops the lock and schedules. 8521da177e4SLinus Torvalds */ 853508034a3SHugh Dickins unsigned long unmap_vmas(struct mmu_gather **tlbp, 8541da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long start_addr, 8551da177e4SLinus Torvalds unsigned long end_addr, unsigned long *nr_accounted, 8561da177e4SLinus Torvalds struct zap_details *details) 8571da177e4SLinus Torvalds { 85851c6f666SRobin Holt long zap_work = ZAP_BLOCK_SIZE; 8591da177e4SLinus Torvalds unsigned long tlb_start = 0; /* For tlb_finish_mmu */ 8601da177e4SLinus Torvalds int tlb_start_valid = 0; 861ee39b37bSHugh Dickins unsigned long start = start_addr; 8621da177e4SLinus Torvalds spinlock_t *i_mmap_lock = details? details->i_mmap_lock: NULL; 8634d6ddfa9SHugh Dickins int fullmm = (*tlbp)->fullmm; 8641da177e4SLinus Torvalds 8651da177e4SLinus Torvalds for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) { 8661da177e4SLinus Torvalds unsigned long end; 8671da177e4SLinus Torvalds 8681da177e4SLinus Torvalds start = max(vma->vm_start, start_addr); 8691da177e4SLinus Torvalds if (start >= vma->vm_end) 8701da177e4SLinus Torvalds continue; 8711da177e4SLinus Torvalds end = min(vma->vm_end, end_addr); 8721da177e4SLinus Torvalds if (end <= vma->vm_start) 8731da177e4SLinus Torvalds continue; 8741da177e4SLinus Torvalds 8751da177e4SLinus Torvalds if (vma->vm_flags & VM_ACCOUNT) 8761da177e4SLinus Torvalds *nr_accounted += (end - start) >> PAGE_SHIFT; 8771da177e4SLinus Torvalds 8781da177e4SLinus Torvalds while (start != end) { 8791da177e4SLinus Torvalds if (!tlb_start_valid) { 8801da177e4SLinus Torvalds tlb_start = start; 8811da177e4SLinus Torvalds tlb_start_valid = 1; 8821da177e4SLinus Torvalds } 8831da177e4SLinus Torvalds 88451c6f666SRobin Holt if (unlikely(is_vm_hugetlb_page(vma))) { 8851da177e4SLinus Torvalds unmap_hugepage_range(vma, start, end); 88651c6f666SRobin Holt zap_work -= (end - start) / 88751c6f666SRobin Holt (HPAGE_SIZE / PAGE_SIZE); 88851c6f666SRobin Holt start = end; 88951c6f666SRobin Holt } else 89051c6f666SRobin Holt start = unmap_page_range(*tlbp, vma, 89151c6f666SRobin Holt start, end, &zap_work, details); 8921da177e4SLinus Torvalds 89351c6f666SRobin Holt if (zap_work > 0) { 89451c6f666SRobin Holt BUG_ON(start != end); 89551c6f666SRobin Holt break; 89651c6f666SRobin Holt } 8971da177e4SLinus Torvalds 8981da177e4SLinus Torvalds tlb_finish_mmu(*tlbp, tlb_start, start); 8991da177e4SLinus Torvalds 9001da177e4SLinus Torvalds if (need_resched() || 90195c354feSNick Piggin (i_mmap_lock && spin_needbreak(i_mmap_lock))) { 9021da177e4SLinus Torvalds if (i_mmap_lock) { 903508034a3SHugh Dickins *tlbp = NULL; 9041da177e4SLinus Torvalds goto out; 9051da177e4SLinus Torvalds } 9061da177e4SLinus Torvalds cond_resched(); 9071da177e4SLinus Torvalds } 9081da177e4SLinus Torvalds 909508034a3SHugh Dickins *tlbp = tlb_gather_mmu(vma->vm_mm, fullmm); 9101da177e4SLinus Torvalds tlb_start_valid = 0; 91151c6f666SRobin Holt zap_work = ZAP_BLOCK_SIZE; 9121da177e4SLinus Torvalds } 9131da177e4SLinus Torvalds } 9141da177e4SLinus Torvalds out: 915ee39b37bSHugh Dickins return start; /* which is now the end (or restart) address */ 9161da177e4SLinus Torvalds } 9171da177e4SLinus Torvalds 9181da177e4SLinus Torvalds /** 9191da177e4SLinus Torvalds * zap_page_range - remove user pages in a given range 9201da177e4SLinus Torvalds * @vma: vm_area_struct holding the applicable pages 9211da177e4SLinus Torvalds * @address: starting address of pages to zap 9221da177e4SLinus Torvalds * @size: number of bytes to zap 9231da177e4SLinus Torvalds * @details: details of nonlinear truncation or shared cache invalidation 9241da177e4SLinus Torvalds */ 925ee39b37bSHugh Dickins unsigned long zap_page_range(struct vm_area_struct *vma, unsigned long address, 9261da177e4SLinus Torvalds unsigned long size, struct zap_details *details) 9271da177e4SLinus Torvalds { 9281da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 9291da177e4SLinus Torvalds struct mmu_gather *tlb; 9301da177e4SLinus Torvalds unsigned long end = address + size; 9311da177e4SLinus Torvalds unsigned long nr_accounted = 0; 9321da177e4SLinus Torvalds 9331da177e4SLinus Torvalds lru_add_drain(); 9341da177e4SLinus Torvalds tlb = tlb_gather_mmu(mm, 0); 935365e9c87SHugh Dickins update_hiwater_rss(mm); 936508034a3SHugh Dickins end = unmap_vmas(&tlb, vma, address, end, &nr_accounted, details); 937508034a3SHugh Dickins if (tlb) 9388f4f8c16SHugh Dickins tlb_finish_mmu(tlb, address, end); 939ee39b37bSHugh Dickins return end; 9401da177e4SLinus Torvalds } 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds /* 9431da177e4SLinus Torvalds * Do a quick page-table lookup for a single page. 9441da177e4SLinus Torvalds */ 9456aab341eSLinus Torvalds struct page *follow_page(struct vm_area_struct *vma, unsigned long address, 946deceb6cdSHugh Dickins unsigned int flags) 9471da177e4SLinus Torvalds { 9481da177e4SLinus Torvalds pgd_t *pgd; 9491da177e4SLinus Torvalds pud_t *pud; 9501da177e4SLinus Torvalds pmd_t *pmd; 9511da177e4SLinus Torvalds pte_t *ptep, pte; 952deceb6cdSHugh Dickins spinlock_t *ptl; 9531da177e4SLinus Torvalds struct page *page; 9546aab341eSLinus Torvalds struct mm_struct *mm = vma->vm_mm; 9551da177e4SLinus Torvalds 956deceb6cdSHugh Dickins page = follow_huge_addr(mm, address, flags & FOLL_WRITE); 957deceb6cdSHugh Dickins if (!IS_ERR(page)) { 958deceb6cdSHugh Dickins BUG_ON(flags & FOLL_GET); 959deceb6cdSHugh Dickins goto out; 960deceb6cdSHugh Dickins } 9611da177e4SLinus Torvalds 962deceb6cdSHugh Dickins page = NULL; 9631da177e4SLinus Torvalds pgd = pgd_offset(mm, address); 9641da177e4SLinus Torvalds if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) 965deceb6cdSHugh Dickins goto no_page_table; 9661da177e4SLinus Torvalds 9671da177e4SLinus Torvalds pud = pud_offset(pgd, address); 9681da177e4SLinus Torvalds if (pud_none(*pud) || unlikely(pud_bad(*pud))) 969deceb6cdSHugh Dickins goto no_page_table; 9701da177e4SLinus Torvalds 9711da177e4SLinus Torvalds pmd = pmd_offset(pud, address); 9721da177e4SLinus Torvalds if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd))) 973deceb6cdSHugh Dickins goto no_page_table; 9741da177e4SLinus Torvalds 975deceb6cdSHugh Dickins if (pmd_huge(*pmd)) { 976deceb6cdSHugh Dickins BUG_ON(flags & FOLL_GET); 977deceb6cdSHugh Dickins page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE); 978deceb6cdSHugh Dickins goto out; 979deceb6cdSHugh Dickins } 980deceb6cdSHugh Dickins 981deceb6cdSHugh Dickins ptep = pte_offset_map_lock(mm, pmd, address, &ptl); 9821da177e4SLinus Torvalds if (!ptep) 9831da177e4SLinus Torvalds goto out; 9841da177e4SLinus Torvalds 9851da177e4SLinus Torvalds pte = *ptep; 986deceb6cdSHugh Dickins if (!pte_present(pte)) 987deceb6cdSHugh Dickins goto unlock; 988deceb6cdSHugh Dickins if ((flags & FOLL_WRITE) && !pte_write(pte)) 989deceb6cdSHugh Dickins goto unlock; 9906aab341eSLinus Torvalds page = vm_normal_page(vma, address, pte); 9916aab341eSLinus Torvalds if (unlikely(!page)) 992deceb6cdSHugh Dickins goto unlock; 993deceb6cdSHugh Dickins 994deceb6cdSHugh Dickins if (flags & FOLL_GET) 995deceb6cdSHugh Dickins get_page(page); 996deceb6cdSHugh Dickins if (flags & FOLL_TOUCH) { 997deceb6cdSHugh Dickins if ((flags & FOLL_WRITE) && 998deceb6cdSHugh Dickins !pte_dirty(pte) && !PageDirty(page)) 999f33ea7f4SNick Piggin set_page_dirty(page); 10001da177e4SLinus Torvalds mark_page_accessed(page); 10011da177e4SLinus Torvalds } 1002deceb6cdSHugh Dickins unlock: 1003deceb6cdSHugh Dickins pte_unmap_unlock(ptep, ptl); 10041da177e4SLinus Torvalds out: 1005deceb6cdSHugh Dickins return page; 1006deceb6cdSHugh Dickins 1007deceb6cdSHugh Dickins no_page_table: 1008deceb6cdSHugh Dickins /* 1009deceb6cdSHugh Dickins * When core dumping an enormous anonymous area that nobody 1010deceb6cdSHugh Dickins * has touched so far, we don't want to allocate page tables. 1011deceb6cdSHugh Dickins */ 1012deceb6cdSHugh Dickins if (flags & FOLL_ANON) { 1013557ed1faSNick Piggin page = ZERO_PAGE(0); 1014deceb6cdSHugh Dickins if (flags & FOLL_GET) 1015deceb6cdSHugh Dickins get_page(page); 1016deceb6cdSHugh Dickins BUG_ON(flags & FOLL_WRITE); 10171da177e4SLinus Torvalds } 1018deceb6cdSHugh Dickins return page; 10191da177e4SLinus Torvalds } 10201da177e4SLinus Torvalds 10211da177e4SLinus Torvalds int get_user_pages(struct task_struct *tsk, struct mm_struct *mm, 10221da177e4SLinus Torvalds unsigned long start, int len, int write, int force, 10231da177e4SLinus Torvalds struct page **pages, struct vm_area_struct **vmas) 10241da177e4SLinus Torvalds { 10251da177e4SLinus Torvalds int i; 1026deceb6cdSHugh Dickins unsigned int vm_flags; 10271da177e4SLinus Torvalds 1028900cf086SJonathan Corbet if (len <= 0) 1029900cf086SJonathan Corbet return 0; 10301da177e4SLinus Torvalds /* 10311da177e4SLinus Torvalds * Require read or write permissions. 10321da177e4SLinus Torvalds * If 'force' is set, we only require the "MAY" flags. 10331da177e4SLinus Torvalds */ 1034deceb6cdSHugh Dickins vm_flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD); 1035deceb6cdSHugh Dickins vm_flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE); 10361da177e4SLinus Torvalds i = 0; 10371da177e4SLinus Torvalds 10381da177e4SLinus Torvalds do { 10391da177e4SLinus Torvalds struct vm_area_struct *vma; 1040deceb6cdSHugh Dickins unsigned int foll_flags; 10411da177e4SLinus Torvalds 10421da177e4SLinus Torvalds vma = find_extend_vma(mm, start); 10431da177e4SLinus Torvalds if (!vma && in_gate_area(tsk, start)) { 10441da177e4SLinus Torvalds unsigned long pg = start & PAGE_MASK; 10451da177e4SLinus Torvalds struct vm_area_struct *gate_vma = get_gate_vma(tsk); 10461da177e4SLinus Torvalds pgd_t *pgd; 10471da177e4SLinus Torvalds pud_t *pud; 10481da177e4SLinus Torvalds pmd_t *pmd; 10491da177e4SLinus Torvalds pte_t *pte; 10501da177e4SLinus Torvalds if (write) /* user gate pages are read-only */ 10511da177e4SLinus Torvalds return i ? : -EFAULT; 10521da177e4SLinus Torvalds if (pg > TASK_SIZE) 10531da177e4SLinus Torvalds pgd = pgd_offset_k(pg); 10541da177e4SLinus Torvalds else 10551da177e4SLinus Torvalds pgd = pgd_offset_gate(mm, pg); 10561da177e4SLinus Torvalds BUG_ON(pgd_none(*pgd)); 10571da177e4SLinus Torvalds pud = pud_offset(pgd, pg); 10581da177e4SLinus Torvalds BUG_ON(pud_none(*pud)); 10591da177e4SLinus Torvalds pmd = pmd_offset(pud, pg); 1060690dbe1cSHugh Dickins if (pmd_none(*pmd)) 1061690dbe1cSHugh Dickins return i ? : -EFAULT; 10621da177e4SLinus Torvalds pte = pte_offset_map(pmd, pg); 1063690dbe1cSHugh Dickins if (pte_none(*pte)) { 1064690dbe1cSHugh Dickins pte_unmap(pte); 1065690dbe1cSHugh Dickins return i ? : -EFAULT; 1066690dbe1cSHugh Dickins } 10671da177e4SLinus Torvalds if (pages) { 1068fa2a455bSNick Piggin struct page *page = vm_normal_page(gate_vma, start, *pte); 10696aab341eSLinus Torvalds pages[i] = page; 10706aab341eSLinus Torvalds if (page) 10716aab341eSLinus Torvalds get_page(page); 10721da177e4SLinus Torvalds } 10731da177e4SLinus Torvalds pte_unmap(pte); 10741da177e4SLinus Torvalds if (vmas) 10751da177e4SLinus Torvalds vmas[i] = gate_vma; 10761da177e4SLinus Torvalds i++; 10771da177e4SLinus Torvalds start += PAGE_SIZE; 10781da177e4SLinus Torvalds len--; 10791da177e4SLinus Torvalds continue; 10801da177e4SLinus Torvalds } 10811da177e4SLinus Torvalds 10821ff80389SLinus Torvalds if (!vma || (vma->vm_flags & (VM_IO | VM_PFNMAP)) 1083deceb6cdSHugh Dickins || !(vm_flags & vma->vm_flags)) 10841da177e4SLinus Torvalds return i ? : -EFAULT; 10851da177e4SLinus Torvalds 10861da177e4SLinus Torvalds if (is_vm_hugetlb_page(vma)) { 10871da177e4SLinus Torvalds i = follow_hugetlb_page(mm, vma, pages, vmas, 10885b23dbe8SAdam Litke &start, &len, i, write); 10891da177e4SLinus Torvalds continue; 10901da177e4SLinus Torvalds } 1091deceb6cdSHugh Dickins 1092deceb6cdSHugh Dickins foll_flags = FOLL_TOUCH; 1093deceb6cdSHugh Dickins if (pages) 1094deceb6cdSHugh Dickins foll_flags |= FOLL_GET; 1095deceb6cdSHugh Dickins if (!write && !(vma->vm_flags & VM_LOCKED) && 10963c18ddd1SNick Piggin (!vma->vm_ops || !vma->vm_ops->fault)) 1097deceb6cdSHugh Dickins foll_flags |= FOLL_ANON; 1098deceb6cdSHugh Dickins 10991da177e4SLinus Torvalds do { 110008ef4729SHugh Dickins struct page *page; 11011da177e4SLinus Torvalds 1102462e00ccSEthan Solomita /* 1103462e00ccSEthan Solomita * If tsk is ooming, cut off its access to large memory 1104462e00ccSEthan Solomita * allocations. It has a pending SIGKILL, but it can't 1105462e00ccSEthan Solomita * be processed until returning to user space. 1106462e00ccSEthan Solomita */ 1107462e00ccSEthan Solomita if (unlikely(test_tsk_thread_flag(tsk, TIF_MEMDIE))) 1108462e00ccSEthan Solomita return -ENOMEM; 1109462e00ccSEthan Solomita 1110deceb6cdSHugh Dickins if (write) 1111deceb6cdSHugh Dickins foll_flags |= FOLL_WRITE; 1112deceb6cdSHugh Dickins 1113deceb6cdSHugh Dickins cond_resched(); 11146aab341eSLinus Torvalds while (!(page = follow_page(vma, start, foll_flags))) { 1115a68d2ebcSLinus Torvalds int ret; 111683c54070SNick Piggin ret = handle_mm_fault(mm, vma, start, 1117deceb6cdSHugh Dickins foll_flags & FOLL_WRITE); 111883c54070SNick Piggin if (ret & VM_FAULT_ERROR) { 111983c54070SNick Piggin if (ret & VM_FAULT_OOM) 112083c54070SNick Piggin return i ? i : -ENOMEM; 112183c54070SNick Piggin else if (ret & VM_FAULT_SIGBUS) 112283c54070SNick Piggin return i ? i : -EFAULT; 112383c54070SNick Piggin BUG(); 112483c54070SNick Piggin } 112583c54070SNick Piggin if (ret & VM_FAULT_MAJOR) 112683c54070SNick Piggin tsk->maj_flt++; 112783c54070SNick Piggin else 112883c54070SNick Piggin tsk->min_flt++; 112983c54070SNick Piggin 1130f33ea7f4SNick Piggin /* 113183c54070SNick Piggin * The VM_FAULT_WRITE bit tells us that 113283c54070SNick Piggin * do_wp_page has broken COW when necessary, 113383c54070SNick Piggin * even if maybe_mkwrite decided not to set 113483c54070SNick Piggin * pte_write. We can thus safely do subsequent 113583c54070SNick Piggin * page lookups as if they were reads. 1136f33ea7f4SNick Piggin */ 1137a68d2ebcSLinus Torvalds if (ret & VM_FAULT_WRITE) 1138deceb6cdSHugh Dickins foll_flags &= ~FOLL_WRITE; 1139a68d2ebcSLinus Torvalds 11407f7bbbe5SBenjamin Herrenschmidt cond_resched(); 11411da177e4SLinus Torvalds } 11421da177e4SLinus Torvalds if (pages) { 114308ef4729SHugh Dickins pages[i] = page; 114403beb076SJames Bottomley 1145a6f36be3SRussell King flush_anon_page(vma, page, start); 114608ef4729SHugh Dickins flush_dcache_page(page); 11471da177e4SLinus Torvalds } 11481da177e4SLinus Torvalds if (vmas) 11491da177e4SLinus Torvalds vmas[i] = vma; 11501da177e4SLinus Torvalds i++; 11511da177e4SLinus Torvalds start += PAGE_SIZE; 11521da177e4SLinus Torvalds len--; 11531da177e4SLinus Torvalds } while (len && start < vma->vm_end); 11541da177e4SLinus Torvalds } while (len); 11551da177e4SLinus Torvalds return i; 11561da177e4SLinus Torvalds } 11571da177e4SLinus Torvalds EXPORT_SYMBOL(get_user_pages); 11581da177e4SLinus Torvalds 1159920c7a5dSHarvey Harrison pte_t *get_locked_pte(struct mm_struct *mm, unsigned long addr, 1160920c7a5dSHarvey Harrison spinlock_t **ptl) 1161c9cfcddfSLinus Torvalds { 1162c9cfcddfSLinus Torvalds pgd_t * pgd = pgd_offset(mm, addr); 1163c9cfcddfSLinus Torvalds pud_t * pud = pud_alloc(mm, pgd, addr); 1164c9cfcddfSLinus Torvalds if (pud) { 116549c91fb0STrond Myklebust pmd_t * pmd = pmd_alloc(mm, pud, addr); 1166c9cfcddfSLinus Torvalds if (pmd) 1167c9cfcddfSLinus Torvalds return pte_alloc_map_lock(mm, pmd, addr, ptl); 1168c9cfcddfSLinus Torvalds } 1169c9cfcddfSLinus Torvalds return NULL; 1170c9cfcddfSLinus Torvalds } 1171c9cfcddfSLinus Torvalds 11721da177e4SLinus Torvalds /* 1173238f58d8SLinus Torvalds * This is the old fallback for page remapping. 1174238f58d8SLinus Torvalds * 1175238f58d8SLinus Torvalds * For historical reasons, it only allows reserved pages. Only 1176238f58d8SLinus Torvalds * old drivers should use this, and they needed to mark their 1177238f58d8SLinus Torvalds * pages reserved for the old functions anyway. 1178238f58d8SLinus Torvalds */ 1179238f58d8SLinus Torvalds static int insert_page(struct mm_struct *mm, unsigned long addr, struct page *page, pgprot_t prot) 1180238f58d8SLinus Torvalds { 1181238f58d8SLinus Torvalds int retval; 1182238f58d8SLinus Torvalds pte_t *pte; 1183238f58d8SLinus Torvalds spinlock_t *ptl; 1184238f58d8SLinus Torvalds 1185e1a1cd59SBalbir Singh retval = mem_cgroup_charge(page, mm, GFP_KERNEL); 11868a9f3ccdSBalbir Singh if (retval) 11878a9f3ccdSBalbir Singh goto out; 11888a9f3ccdSBalbir Singh 1189238f58d8SLinus Torvalds retval = -EINVAL; 1190a145dd41SLinus Torvalds if (PageAnon(page)) 11918a9f3ccdSBalbir Singh goto out_uncharge; 1192238f58d8SLinus Torvalds retval = -ENOMEM; 1193238f58d8SLinus Torvalds flush_dcache_page(page); 1194c9cfcddfSLinus Torvalds pte = get_locked_pte(mm, addr, &ptl); 1195238f58d8SLinus Torvalds if (!pte) 11968a9f3ccdSBalbir Singh goto out_uncharge; 1197238f58d8SLinus Torvalds retval = -EBUSY; 1198238f58d8SLinus Torvalds if (!pte_none(*pte)) 1199238f58d8SLinus Torvalds goto out_unlock; 1200238f58d8SLinus Torvalds 1201238f58d8SLinus Torvalds /* Ok, finally just insert the thing.. */ 1202238f58d8SLinus Torvalds get_page(page); 1203238f58d8SLinus Torvalds inc_mm_counter(mm, file_rss); 1204238f58d8SLinus Torvalds page_add_file_rmap(page); 1205238f58d8SLinus Torvalds set_pte_at(mm, addr, pte, mk_pte(page, prot)); 1206238f58d8SLinus Torvalds 1207238f58d8SLinus Torvalds retval = 0; 12088a9f3ccdSBalbir Singh pte_unmap_unlock(pte, ptl); 12098a9f3ccdSBalbir Singh return retval; 1210238f58d8SLinus Torvalds out_unlock: 1211238f58d8SLinus Torvalds pte_unmap_unlock(pte, ptl); 12128a9f3ccdSBalbir Singh out_uncharge: 12138a9f3ccdSBalbir Singh mem_cgroup_uncharge_page(page); 1214238f58d8SLinus Torvalds out: 1215238f58d8SLinus Torvalds return retval; 1216238f58d8SLinus Torvalds } 1217238f58d8SLinus Torvalds 1218bfa5bf6dSRolf Eike Beer /** 1219bfa5bf6dSRolf Eike Beer * vm_insert_page - insert single page into user vma 1220bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 1221bfa5bf6dSRolf Eike Beer * @addr: target user address of this page 1222bfa5bf6dSRolf Eike Beer * @page: source kernel page 1223bfa5bf6dSRolf Eike Beer * 1224a145dd41SLinus Torvalds * This allows drivers to insert individual pages they've allocated 1225a145dd41SLinus Torvalds * into a user vma. 1226a145dd41SLinus Torvalds * 1227a145dd41SLinus Torvalds * The page has to be a nice clean _individual_ kernel allocation. 1228a145dd41SLinus Torvalds * If you allocate a compound page, you need to have marked it as 1229a145dd41SLinus Torvalds * such (__GFP_COMP), or manually just split the page up yourself 12308dfcc9baSNick Piggin * (see split_page()). 1231a145dd41SLinus Torvalds * 1232a145dd41SLinus Torvalds * NOTE! Traditionally this was done with "remap_pfn_range()" which 1233a145dd41SLinus Torvalds * took an arbitrary page protection parameter. This doesn't allow 1234a145dd41SLinus Torvalds * that. Your vma protection will have to be set up correctly, which 1235a145dd41SLinus Torvalds * means that if you want a shared writable mapping, you'd better 1236a145dd41SLinus Torvalds * ask for a shared writable mapping! 1237a145dd41SLinus Torvalds * 1238a145dd41SLinus Torvalds * The page does not need to be reserved. 1239a145dd41SLinus Torvalds */ 1240a145dd41SLinus Torvalds int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, struct page *page) 1241a145dd41SLinus Torvalds { 1242a145dd41SLinus Torvalds if (addr < vma->vm_start || addr >= vma->vm_end) 1243a145dd41SLinus Torvalds return -EFAULT; 1244a145dd41SLinus Torvalds if (!page_count(page)) 1245a145dd41SLinus Torvalds return -EINVAL; 12464d7672b4SLinus Torvalds vma->vm_flags |= VM_INSERTPAGE; 1247a145dd41SLinus Torvalds return insert_page(vma->vm_mm, addr, page, vma->vm_page_prot); 1248a145dd41SLinus Torvalds } 1249e3c3374fSLinus Torvalds EXPORT_SYMBOL(vm_insert_page); 1250a145dd41SLinus Torvalds 1251e0dc0d8fSNick Piggin /** 1252e0dc0d8fSNick Piggin * vm_insert_pfn - insert single pfn into user vma 1253e0dc0d8fSNick Piggin * @vma: user vma to map to 1254e0dc0d8fSNick Piggin * @addr: target user address of this page 1255e0dc0d8fSNick Piggin * @pfn: source kernel pfn 1256e0dc0d8fSNick Piggin * 1257e0dc0d8fSNick Piggin * Similar to vm_inert_page, this allows drivers to insert individual pages 1258e0dc0d8fSNick Piggin * they've allocated into a user vma. Same comments apply. 1259e0dc0d8fSNick Piggin * 1260e0dc0d8fSNick Piggin * This function should only be called from a vm_ops->fault handler, and 1261e0dc0d8fSNick Piggin * in that case the handler should return NULL. 1262e0dc0d8fSNick Piggin */ 1263e0dc0d8fSNick Piggin int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr, 1264e0dc0d8fSNick Piggin unsigned long pfn) 1265e0dc0d8fSNick Piggin { 1266e0dc0d8fSNick Piggin struct mm_struct *mm = vma->vm_mm; 1267e0dc0d8fSNick Piggin int retval; 1268e0dc0d8fSNick Piggin pte_t *pte, entry; 1269e0dc0d8fSNick Piggin spinlock_t *ptl; 1270e0dc0d8fSNick Piggin 1271*7e675137SNick Piggin /* 1272*7e675137SNick Piggin * Technically, architectures with pte_special can avoid all these 1273*7e675137SNick Piggin * restrictions (same for remap_pfn_range). However we would like 1274*7e675137SNick Piggin * consistency in testing and feature parity among all, so we should 1275*7e675137SNick Piggin * try to keep these invariants in place for everybody. 1276*7e675137SNick Piggin */ 1277b379d790SJared Hulbert BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))); 1278b379d790SJared Hulbert BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) == 1279b379d790SJared Hulbert (VM_PFNMAP|VM_MIXEDMAP)); 1280b379d790SJared Hulbert BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); 1281b379d790SJared Hulbert BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn)); 1282e0dc0d8fSNick Piggin 1283e0dc0d8fSNick Piggin retval = -ENOMEM; 1284e0dc0d8fSNick Piggin pte = get_locked_pte(mm, addr, &ptl); 1285e0dc0d8fSNick Piggin if (!pte) 1286e0dc0d8fSNick Piggin goto out; 1287e0dc0d8fSNick Piggin retval = -EBUSY; 1288e0dc0d8fSNick Piggin if (!pte_none(*pte)) 1289e0dc0d8fSNick Piggin goto out_unlock; 1290e0dc0d8fSNick Piggin 1291e0dc0d8fSNick Piggin /* Ok, finally just insert the thing.. */ 1292*7e675137SNick Piggin entry = pte_mkspecial(pfn_pte(pfn, vma->vm_page_prot)); 1293e0dc0d8fSNick Piggin set_pte_at(mm, addr, pte, entry); 1294e0dc0d8fSNick Piggin update_mmu_cache(vma, addr, entry); 1295e0dc0d8fSNick Piggin 1296e0dc0d8fSNick Piggin retval = 0; 1297e0dc0d8fSNick Piggin out_unlock: 1298e0dc0d8fSNick Piggin pte_unmap_unlock(pte, ptl); 1299e0dc0d8fSNick Piggin 1300e0dc0d8fSNick Piggin out: 1301e0dc0d8fSNick Piggin return retval; 1302e0dc0d8fSNick Piggin } 1303e0dc0d8fSNick Piggin EXPORT_SYMBOL(vm_insert_pfn); 1304e0dc0d8fSNick Piggin 1305a145dd41SLinus Torvalds /* 13061da177e4SLinus Torvalds * maps a range of physical memory into the requested pages. the old 13071da177e4SLinus Torvalds * mappings are removed. any references to nonexistent pages results 13081da177e4SLinus Torvalds * in null mappings (currently treated as "copy-on-access") 13091da177e4SLinus Torvalds */ 13101da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd, 13111da177e4SLinus Torvalds unsigned long addr, unsigned long end, 13121da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 13131da177e4SLinus Torvalds { 13141da177e4SLinus Torvalds pte_t *pte; 1315c74df32cSHugh Dickins spinlock_t *ptl; 13161da177e4SLinus Torvalds 1317c74df32cSHugh Dickins pte = pte_alloc_map_lock(mm, pmd, addr, &ptl); 13181da177e4SLinus Torvalds if (!pte) 13191da177e4SLinus Torvalds return -ENOMEM; 13206606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 13211da177e4SLinus Torvalds do { 13221da177e4SLinus Torvalds BUG_ON(!pte_none(*pte)); 1323*7e675137SNick Piggin set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot))); 13241da177e4SLinus Torvalds pfn++; 13251da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 13266606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 1327c74df32cSHugh Dickins pte_unmap_unlock(pte - 1, ptl); 13281da177e4SLinus Torvalds return 0; 13291da177e4SLinus Torvalds } 13301da177e4SLinus Torvalds 13311da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud, 13321da177e4SLinus Torvalds unsigned long addr, unsigned long end, 13331da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 13341da177e4SLinus Torvalds { 13351da177e4SLinus Torvalds pmd_t *pmd; 13361da177e4SLinus Torvalds unsigned long next; 13371da177e4SLinus Torvalds 13381da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 13391da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, addr); 13401da177e4SLinus Torvalds if (!pmd) 13411da177e4SLinus Torvalds return -ENOMEM; 13421da177e4SLinus Torvalds do { 13431da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 13441da177e4SLinus Torvalds if (remap_pte_range(mm, pmd, addr, next, 13451da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot)) 13461da177e4SLinus Torvalds return -ENOMEM; 13471da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 13481da177e4SLinus Torvalds return 0; 13491da177e4SLinus Torvalds } 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd, 13521da177e4SLinus Torvalds unsigned long addr, unsigned long end, 13531da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 13541da177e4SLinus Torvalds { 13551da177e4SLinus Torvalds pud_t *pud; 13561da177e4SLinus Torvalds unsigned long next; 13571da177e4SLinus Torvalds 13581da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 13591da177e4SLinus Torvalds pud = pud_alloc(mm, pgd, addr); 13601da177e4SLinus Torvalds if (!pud) 13611da177e4SLinus Torvalds return -ENOMEM; 13621da177e4SLinus Torvalds do { 13631da177e4SLinus Torvalds next = pud_addr_end(addr, end); 13641da177e4SLinus Torvalds if (remap_pmd_range(mm, pud, addr, next, 13651da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot)) 13661da177e4SLinus Torvalds return -ENOMEM; 13671da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 13681da177e4SLinus Torvalds return 0; 13691da177e4SLinus Torvalds } 13701da177e4SLinus Torvalds 1371bfa5bf6dSRolf Eike Beer /** 1372bfa5bf6dSRolf Eike Beer * remap_pfn_range - remap kernel memory to userspace 1373bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 1374bfa5bf6dSRolf Eike Beer * @addr: target user address to start at 1375bfa5bf6dSRolf Eike Beer * @pfn: physical address of kernel memory 1376bfa5bf6dSRolf Eike Beer * @size: size of map area 1377bfa5bf6dSRolf Eike Beer * @prot: page protection flags for this mapping 1378bfa5bf6dSRolf Eike Beer * 1379bfa5bf6dSRolf Eike Beer * Note: this is only safe if the mm semaphore is held when called. 1380bfa5bf6dSRolf Eike Beer */ 13811da177e4SLinus Torvalds int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 13821da177e4SLinus Torvalds unsigned long pfn, unsigned long size, pgprot_t prot) 13831da177e4SLinus Torvalds { 13841da177e4SLinus Torvalds pgd_t *pgd; 13851da177e4SLinus Torvalds unsigned long next; 13862d15cab8SHugh Dickins unsigned long end = addr + PAGE_ALIGN(size); 13871da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 13881da177e4SLinus Torvalds int err; 13891da177e4SLinus Torvalds 13901da177e4SLinus Torvalds /* 13911da177e4SLinus Torvalds * Physically remapped pages are special. Tell the 13921da177e4SLinus Torvalds * rest of the world about it: 13931da177e4SLinus Torvalds * VM_IO tells people not to look at these pages 13941da177e4SLinus Torvalds * (accesses can have side effects). 13950b14c179SHugh Dickins * VM_RESERVED is specified all over the place, because 13960b14c179SHugh Dickins * in 2.4 it kept swapout's vma scan off this vma; but 13970b14c179SHugh Dickins * in 2.6 the LRU scan won't even find its pages, so this 13980b14c179SHugh Dickins * flag means no more than count its pages in reserved_vm, 13990b14c179SHugh Dickins * and omit it from core dump, even when VM_IO turned off. 14006aab341eSLinus Torvalds * VM_PFNMAP tells the core MM that the base pages are just 14016aab341eSLinus Torvalds * raw PFN mappings, and do not have a "struct page" associated 14026aab341eSLinus Torvalds * with them. 1403fb155c16SLinus Torvalds * 1404fb155c16SLinus Torvalds * There's a horrible special case to handle copy-on-write 1405fb155c16SLinus Torvalds * behaviour that some programs depend on. We mark the "original" 1406fb155c16SLinus Torvalds * un-COW'ed pages by matching them up with "vma->vm_pgoff". 14071da177e4SLinus Torvalds */ 140867121172SLinus Torvalds if (is_cow_mapping(vma->vm_flags)) { 1409fb155c16SLinus Torvalds if (addr != vma->vm_start || end != vma->vm_end) 14107fc7e2eeSLinus Torvalds return -EINVAL; 14116aab341eSLinus Torvalds vma->vm_pgoff = pfn; 1412fb155c16SLinus Torvalds } 1413fb155c16SLinus Torvalds 1414fb155c16SLinus Torvalds vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP; 14151da177e4SLinus Torvalds 14161da177e4SLinus Torvalds BUG_ON(addr >= end); 14171da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 14181da177e4SLinus Torvalds pgd = pgd_offset(mm, addr); 14191da177e4SLinus Torvalds flush_cache_range(vma, addr, end); 14201da177e4SLinus Torvalds do { 14211da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 14221da177e4SLinus Torvalds err = remap_pud_range(mm, pgd, addr, next, 14231da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot); 14241da177e4SLinus Torvalds if (err) 14251da177e4SLinus Torvalds break; 14261da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 14271da177e4SLinus Torvalds return err; 14281da177e4SLinus Torvalds } 14291da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range); 14301da177e4SLinus Torvalds 1431aee16b3cSJeremy Fitzhardinge static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd, 1432aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 1433aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 1434aee16b3cSJeremy Fitzhardinge { 1435aee16b3cSJeremy Fitzhardinge pte_t *pte; 1436aee16b3cSJeremy Fitzhardinge int err; 14372f569afdSMartin Schwidefsky pgtable_t token; 143894909914SBorislav Petkov spinlock_t *uninitialized_var(ptl); 1439aee16b3cSJeremy Fitzhardinge 1440aee16b3cSJeremy Fitzhardinge pte = (mm == &init_mm) ? 1441aee16b3cSJeremy Fitzhardinge pte_alloc_kernel(pmd, addr) : 1442aee16b3cSJeremy Fitzhardinge pte_alloc_map_lock(mm, pmd, addr, &ptl); 1443aee16b3cSJeremy Fitzhardinge if (!pte) 1444aee16b3cSJeremy Fitzhardinge return -ENOMEM; 1445aee16b3cSJeremy Fitzhardinge 1446aee16b3cSJeremy Fitzhardinge BUG_ON(pmd_huge(*pmd)); 1447aee16b3cSJeremy Fitzhardinge 14482f569afdSMartin Schwidefsky token = pmd_pgtable(*pmd); 1449aee16b3cSJeremy Fitzhardinge 1450aee16b3cSJeremy Fitzhardinge do { 14512f569afdSMartin Schwidefsky err = fn(pte, token, addr, data); 1452aee16b3cSJeremy Fitzhardinge if (err) 1453aee16b3cSJeremy Fitzhardinge break; 1454aee16b3cSJeremy Fitzhardinge } while (pte++, addr += PAGE_SIZE, addr != end); 1455aee16b3cSJeremy Fitzhardinge 1456aee16b3cSJeremy Fitzhardinge if (mm != &init_mm) 1457aee16b3cSJeremy Fitzhardinge pte_unmap_unlock(pte-1, ptl); 1458aee16b3cSJeremy Fitzhardinge return err; 1459aee16b3cSJeremy Fitzhardinge } 1460aee16b3cSJeremy Fitzhardinge 1461aee16b3cSJeremy Fitzhardinge static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud, 1462aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 1463aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 1464aee16b3cSJeremy Fitzhardinge { 1465aee16b3cSJeremy Fitzhardinge pmd_t *pmd; 1466aee16b3cSJeremy Fitzhardinge unsigned long next; 1467aee16b3cSJeremy Fitzhardinge int err; 1468aee16b3cSJeremy Fitzhardinge 1469aee16b3cSJeremy Fitzhardinge pmd = pmd_alloc(mm, pud, addr); 1470aee16b3cSJeremy Fitzhardinge if (!pmd) 1471aee16b3cSJeremy Fitzhardinge return -ENOMEM; 1472aee16b3cSJeremy Fitzhardinge do { 1473aee16b3cSJeremy Fitzhardinge next = pmd_addr_end(addr, end); 1474aee16b3cSJeremy Fitzhardinge err = apply_to_pte_range(mm, pmd, addr, next, fn, data); 1475aee16b3cSJeremy Fitzhardinge if (err) 1476aee16b3cSJeremy Fitzhardinge break; 1477aee16b3cSJeremy Fitzhardinge } while (pmd++, addr = next, addr != end); 1478aee16b3cSJeremy Fitzhardinge return err; 1479aee16b3cSJeremy Fitzhardinge } 1480aee16b3cSJeremy Fitzhardinge 1481aee16b3cSJeremy Fitzhardinge static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd, 1482aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 1483aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 1484aee16b3cSJeremy Fitzhardinge { 1485aee16b3cSJeremy Fitzhardinge pud_t *pud; 1486aee16b3cSJeremy Fitzhardinge unsigned long next; 1487aee16b3cSJeremy Fitzhardinge int err; 1488aee16b3cSJeremy Fitzhardinge 1489aee16b3cSJeremy Fitzhardinge pud = pud_alloc(mm, pgd, addr); 1490aee16b3cSJeremy Fitzhardinge if (!pud) 1491aee16b3cSJeremy Fitzhardinge return -ENOMEM; 1492aee16b3cSJeremy Fitzhardinge do { 1493aee16b3cSJeremy Fitzhardinge next = pud_addr_end(addr, end); 1494aee16b3cSJeremy Fitzhardinge err = apply_to_pmd_range(mm, pud, addr, next, fn, data); 1495aee16b3cSJeremy Fitzhardinge if (err) 1496aee16b3cSJeremy Fitzhardinge break; 1497aee16b3cSJeremy Fitzhardinge } while (pud++, addr = next, addr != end); 1498aee16b3cSJeremy Fitzhardinge return err; 1499aee16b3cSJeremy Fitzhardinge } 1500aee16b3cSJeremy Fitzhardinge 1501aee16b3cSJeremy Fitzhardinge /* 1502aee16b3cSJeremy Fitzhardinge * Scan a region of virtual memory, filling in page tables as necessary 1503aee16b3cSJeremy Fitzhardinge * and calling a provided function on each leaf page table. 1504aee16b3cSJeremy Fitzhardinge */ 1505aee16b3cSJeremy Fitzhardinge int apply_to_page_range(struct mm_struct *mm, unsigned long addr, 1506aee16b3cSJeremy Fitzhardinge unsigned long size, pte_fn_t fn, void *data) 1507aee16b3cSJeremy Fitzhardinge { 1508aee16b3cSJeremy Fitzhardinge pgd_t *pgd; 1509aee16b3cSJeremy Fitzhardinge unsigned long next; 1510aee16b3cSJeremy Fitzhardinge unsigned long end = addr + size; 1511aee16b3cSJeremy Fitzhardinge int err; 1512aee16b3cSJeremy Fitzhardinge 1513aee16b3cSJeremy Fitzhardinge BUG_ON(addr >= end); 1514aee16b3cSJeremy Fitzhardinge pgd = pgd_offset(mm, addr); 1515aee16b3cSJeremy Fitzhardinge do { 1516aee16b3cSJeremy Fitzhardinge next = pgd_addr_end(addr, end); 1517aee16b3cSJeremy Fitzhardinge err = apply_to_pud_range(mm, pgd, addr, next, fn, data); 1518aee16b3cSJeremy Fitzhardinge if (err) 1519aee16b3cSJeremy Fitzhardinge break; 1520aee16b3cSJeremy Fitzhardinge } while (pgd++, addr = next, addr != end); 1521aee16b3cSJeremy Fitzhardinge return err; 1522aee16b3cSJeremy Fitzhardinge } 1523aee16b3cSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(apply_to_page_range); 1524aee16b3cSJeremy Fitzhardinge 15251da177e4SLinus Torvalds /* 15268f4e2101SHugh Dickins * handle_pte_fault chooses page fault handler according to an entry 15278f4e2101SHugh Dickins * which was read non-atomically. Before making any commitment, on 15288f4e2101SHugh Dickins * those architectures or configurations (e.g. i386 with PAE) which 15298f4e2101SHugh Dickins * might give a mix of unmatched parts, do_swap_page and do_file_page 15308f4e2101SHugh Dickins * must check under lock before unmapping the pte and proceeding 15318f4e2101SHugh Dickins * (but do_wp_page is only called after already making such a check; 15328f4e2101SHugh Dickins * and do_anonymous_page and do_no_page can safely check later on). 15338f4e2101SHugh Dickins */ 15344c21e2f2SHugh Dickins static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd, 15358f4e2101SHugh Dickins pte_t *page_table, pte_t orig_pte) 15368f4e2101SHugh Dickins { 15378f4e2101SHugh Dickins int same = 1; 15388f4e2101SHugh Dickins #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT) 15398f4e2101SHugh Dickins if (sizeof(pte_t) > sizeof(unsigned long)) { 15404c21e2f2SHugh Dickins spinlock_t *ptl = pte_lockptr(mm, pmd); 15414c21e2f2SHugh Dickins spin_lock(ptl); 15428f4e2101SHugh Dickins same = pte_same(*page_table, orig_pte); 15434c21e2f2SHugh Dickins spin_unlock(ptl); 15448f4e2101SHugh Dickins } 15458f4e2101SHugh Dickins #endif 15468f4e2101SHugh Dickins pte_unmap(page_table); 15478f4e2101SHugh Dickins return same; 15488f4e2101SHugh Dickins } 15498f4e2101SHugh Dickins 15508f4e2101SHugh Dickins /* 15511da177e4SLinus Torvalds * Do pte_mkwrite, but only if the vma says VM_WRITE. We do this when 15521da177e4SLinus Torvalds * servicing faults for write access. In the normal case, do always want 15531da177e4SLinus Torvalds * pte_mkwrite. But get_user_pages can cause write faults for mappings 15541da177e4SLinus Torvalds * that do not have writing enabled, when used by access_process_vm. 15551da177e4SLinus Torvalds */ 15561da177e4SLinus Torvalds static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma) 15571da177e4SLinus Torvalds { 15581da177e4SLinus Torvalds if (likely(vma->vm_flags & VM_WRITE)) 15591da177e4SLinus Torvalds pte = pte_mkwrite(pte); 15601da177e4SLinus Torvalds return pte; 15611da177e4SLinus Torvalds } 15621da177e4SLinus Torvalds 15639de455b2SAtsushi Nemoto static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma) 15646aab341eSLinus Torvalds { 15656aab341eSLinus Torvalds /* 15666aab341eSLinus Torvalds * If the source page was a PFN mapping, we don't have 15676aab341eSLinus Torvalds * a "struct page" for it. We do a best-effort copy by 15686aab341eSLinus Torvalds * just copying from the original user address. If that 15696aab341eSLinus Torvalds * fails, we just zero-fill it. Live with it. 15706aab341eSLinus Torvalds */ 15716aab341eSLinus Torvalds if (unlikely(!src)) { 15726aab341eSLinus Torvalds void *kaddr = kmap_atomic(dst, KM_USER0); 15735d2a2dbbSLinus Torvalds void __user *uaddr = (void __user *)(va & PAGE_MASK); 15745d2a2dbbSLinus Torvalds 15755d2a2dbbSLinus Torvalds /* 15765d2a2dbbSLinus Torvalds * This really shouldn't fail, because the page is there 15775d2a2dbbSLinus Torvalds * in the page tables. But it might just be unreadable, 15785d2a2dbbSLinus Torvalds * in which case we just give up and fill the result with 15795d2a2dbbSLinus Torvalds * zeroes. 15805d2a2dbbSLinus Torvalds */ 15815d2a2dbbSLinus Torvalds if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) 15826aab341eSLinus Torvalds memset(kaddr, 0, PAGE_SIZE); 15836aab341eSLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 1584c4ec7b0dSDmitriy Monakhov flush_dcache_page(dst); 15850ed361deSNick Piggin } else 15869de455b2SAtsushi Nemoto copy_user_highpage(dst, src, va, vma); 15876aab341eSLinus Torvalds } 15886aab341eSLinus Torvalds 15891da177e4SLinus Torvalds /* 15901da177e4SLinus Torvalds * This routine handles present pages, when users try to write 15911da177e4SLinus Torvalds * to a shared page. It is done by copying the page to a new address 15921da177e4SLinus Torvalds * and decrementing the shared-page counter for the old page. 15931da177e4SLinus Torvalds * 15941da177e4SLinus Torvalds * Note that this routine assumes that the protection checks have been 15951da177e4SLinus Torvalds * done by the caller (the low-level page fault routine in most cases). 15961da177e4SLinus Torvalds * Thus we can safely just mark it writable once we've done any necessary 15971da177e4SLinus Torvalds * COW. 15981da177e4SLinus Torvalds * 15991da177e4SLinus Torvalds * We also mark the page dirty at this point even though the page will 16001da177e4SLinus Torvalds * change only once the write actually happens. This avoids a few races, 16011da177e4SLinus Torvalds * and potentially makes it more efficient. 16021da177e4SLinus Torvalds * 16038f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 16048f4e2101SHugh Dickins * but allow concurrent faults), with pte both mapped and locked. 16058f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 16061da177e4SLinus Torvalds */ 16071da177e4SLinus Torvalds static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma, 160865500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 16098f4e2101SHugh Dickins spinlock_t *ptl, pte_t orig_pte) 16101da177e4SLinus Torvalds { 1611e5bbe4dfSHugh Dickins struct page *old_page, *new_page; 16121da177e4SLinus Torvalds pte_t entry; 161383c54070SNick Piggin int reuse = 0, ret = 0; 1614a200ee18SPeter Zijlstra int page_mkwrite = 0; 1615d08b3851SPeter Zijlstra struct page *dirty_page = NULL; 16161da177e4SLinus Torvalds 16176aab341eSLinus Torvalds old_page = vm_normal_page(vma, address, orig_pte); 16186aab341eSLinus Torvalds if (!old_page) 1619920fc356SHugh Dickins goto gotten; 16201da177e4SLinus Torvalds 1621d08b3851SPeter Zijlstra /* 1622ee6a6457SPeter Zijlstra * Take out anonymous pages first, anonymous shared vmas are 1623ee6a6457SPeter Zijlstra * not dirty accountable. 1624d08b3851SPeter Zijlstra */ 1625ee6a6457SPeter Zijlstra if (PageAnon(old_page)) { 1626ee6a6457SPeter Zijlstra if (!TestSetPageLocked(old_page)) { 1627ee6a6457SPeter Zijlstra reuse = can_share_swap_page(old_page); 1628ee6a6457SPeter Zijlstra unlock_page(old_page); 1629ee6a6457SPeter Zijlstra } 1630ee6a6457SPeter Zijlstra } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 1631d08b3851SPeter Zijlstra (VM_WRITE|VM_SHARED))) { 1632ee6a6457SPeter Zijlstra /* 1633ee6a6457SPeter Zijlstra * Only catch write-faults on shared writable pages, 1634ee6a6457SPeter Zijlstra * read-only shared pages can get COWed by 1635ee6a6457SPeter Zijlstra * get_user_pages(.write=1, .force=1). 1636ee6a6457SPeter Zijlstra */ 16379637a5efSDavid Howells if (vma->vm_ops && vma->vm_ops->page_mkwrite) { 16389637a5efSDavid Howells /* 16399637a5efSDavid Howells * Notify the address space that the page is about to 16409637a5efSDavid Howells * become writable so that it can prohibit this or wait 16419637a5efSDavid Howells * for the page to get into an appropriate state. 16429637a5efSDavid Howells * 16439637a5efSDavid Howells * We do this without the lock held, so that it can 16449637a5efSDavid Howells * sleep if it needs to. 16459637a5efSDavid Howells */ 16469637a5efSDavid Howells page_cache_get(old_page); 16479637a5efSDavid Howells pte_unmap_unlock(page_table, ptl); 16489637a5efSDavid Howells 16499637a5efSDavid Howells if (vma->vm_ops->page_mkwrite(vma, old_page) < 0) 16509637a5efSDavid Howells goto unwritable_page; 16519637a5efSDavid Howells 16529637a5efSDavid Howells /* 16539637a5efSDavid Howells * Since we dropped the lock we need to revalidate 16549637a5efSDavid Howells * the PTE as someone else may have changed it. If 16559637a5efSDavid Howells * they did, we just return, as we can count on the 16569637a5efSDavid Howells * MMU to tell us if they didn't also make it writable. 16579637a5efSDavid Howells */ 16589637a5efSDavid Howells page_table = pte_offset_map_lock(mm, pmd, address, 16599637a5efSDavid Howells &ptl); 1660c3704cebSHugh Dickins page_cache_release(old_page); 16619637a5efSDavid Howells if (!pte_same(*page_table, orig_pte)) 16629637a5efSDavid Howells goto unlock; 1663a200ee18SPeter Zijlstra 1664a200ee18SPeter Zijlstra page_mkwrite = 1; 16659637a5efSDavid Howells } 1666d08b3851SPeter Zijlstra dirty_page = old_page; 1667d08b3851SPeter Zijlstra get_page(dirty_page); 16689637a5efSDavid Howells reuse = 1; 16699637a5efSDavid Howells } 16709637a5efSDavid Howells 16711da177e4SLinus Torvalds if (reuse) { 1672eca35133SBen Collins flush_cache_page(vma, address, pte_pfn(orig_pte)); 167365500d23SHugh Dickins entry = pte_mkyoung(orig_pte); 167465500d23SHugh Dickins entry = maybe_mkwrite(pte_mkdirty(entry), vma); 1675954ffcb3SKAMEZAWA Hiroyuki if (ptep_set_access_flags(vma, address, page_table, entry,1)) 16761da177e4SLinus Torvalds update_mmu_cache(vma, address, entry); 167765500d23SHugh Dickins ret |= VM_FAULT_WRITE; 167865500d23SHugh Dickins goto unlock; 16791da177e4SLinus Torvalds } 16801da177e4SLinus Torvalds 16811da177e4SLinus Torvalds /* 16821da177e4SLinus Torvalds * Ok, we need to copy. Oh, well.. 16831da177e4SLinus Torvalds */ 16841da177e4SLinus Torvalds page_cache_get(old_page); 1685920fc356SHugh Dickins gotten: 16868f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 16871da177e4SLinus Torvalds 16881da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 168965500d23SHugh Dickins goto oom; 1690557ed1faSNick Piggin VM_BUG_ON(old_page == ZERO_PAGE(0)); 1691769848c0SMel Gorman new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address); 16921da177e4SLinus Torvalds if (!new_page) 169365500d23SHugh Dickins goto oom; 16949de455b2SAtsushi Nemoto cow_user_page(new_page, old_page, address, vma); 16950ed361deSNick Piggin __SetPageUptodate(new_page); 169665500d23SHugh Dickins 1697e1a1cd59SBalbir Singh if (mem_cgroup_charge(new_page, mm, GFP_KERNEL)) 16988a9f3ccdSBalbir Singh goto oom_free_new; 16998a9f3ccdSBalbir Singh 17001da177e4SLinus Torvalds /* 17011da177e4SLinus Torvalds * Re-check the pte - we dropped the lock 17021da177e4SLinus Torvalds */ 17038f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 170465500d23SHugh Dickins if (likely(pte_same(*page_table, orig_pte))) { 1705920fc356SHugh Dickins if (old_page) { 17067de6b805SNick Piggin page_remove_rmap(old_page, vma); 17074294621fSHugh Dickins if (!PageAnon(old_page)) { 17084294621fSHugh Dickins dec_mm_counter(mm, file_rss); 1709920fc356SHugh Dickins inc_mm_counter(mm, anon_rss); 17104294621fSHugh Dickins } 1711920fc356SHugh Dickins } else 1712920fc356SHugh Dickins inc_mm_counter(mm, anon_rss); 1713eca35133SBen Collins flush_cache_page(vma, address, pte_pfn(orig_pte)); 171465500d23SHugh Dickins entry = mk_pte(new_page, vma->vm_page_prot); 171565500d23SHugh Dickins entry = maybe_mkwrite(pte_mkdirty(entry), vma); 17164ce072f1SSiddha, Suresh B /* 17174ce072f1SSiddha, Suresh B * Clear the pte entry and flush it first, before updating the 17184ce072f1SSiddha, Suresh B * pte with the new entry. This will avoid a race condition 17194ce072f1SSiddha, Suresh B * seen in the presence of one thread doing SMC and another 17204ce072f1SSiddha, Suresh B * thread doing COW. 17214ce072f1SSiddha, Suresh B */ 17224ce072f1SSiddha, Suresh B ptep_clear_flush(vma, address, page_table); 17234ce072f1SSiddha, Suresh B set_pte_at(mm, address, page_table, entry); 172465500d23SHugh Dickins update_mmu_cache(vma, address, entry); 17251da177e4SLinus Torvalds lru_cache_add_active(new_page); 17269617d95eSNick Piggin page_add_new_anon_rmap(new_page, vma, address); 17271da177e4SLinus Torvalds 17281da177e4SLinus Torvalds /* Free the old page.. */ 17291da177e4SLinus Torvalds new_page = old_page; 1730f33ea7f4SNick Piggin ret |= VM_FAULT_WRITE; 17318a9f3ccdSBalbir Singh } else 17328a9f3ccdSBalbir Singh mem_cgroup_uncharge_page(new_page); 17338a9f3ccdSBalbir Singh 1734920fc356SHugh Dickins if (new_page) 17351da177e4SLinus Torvalds page_cache_release(new_page); 1736920fc356SHugh Dickins if (old_page) 17371da177e4SLinus Torvalds page_cache_release(old_page); 173865500d23SHugh Dickins unlock: 17398f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 1740d08b3851SPeter Zijlstra if (dirty_page) { 17418f7b3d15SAnton Salikhmetov if (vma->vm_file) 17428f7b3d15SAnton Salikhmetov file_update_time(vma->vm_file); 17438f7b3d15SAnton Salikhmetov 174479352894SNick Piggin /* 174579352894SNick Piggin * Yes, Virginia, this is actually required to prevent a race 174679352894SNick Piggin * with clear_page_dirty_for_io() from clearing the page dirty 174779352894SNick Piggin * bit after it clear all dirty ptes, but before a racing 174879352894SNick Piggin * do_wp_page installs a dirty pte. 174979352894SNick Piggin * 175079352894SNick Piggin * do_no_page is protected similarly. 175179352894SNick Piggin */ 175279352894SNick Piggin wait_on_page_locked(dirty_page); 1753a200ee18SPeter Zijlstra set_page_dirty_balance(dirty_page, page_mkwrite); 1754d08b3851SPeter Zijlstra put_page(dirty_page); 1755d08b3851SPeter Zijlstra } 1756f33ea7f4SNick Piggin return ret; 17578a9f3ccdSBalbir Singh oom_free_new: 17586dbf6d3bSHugh Dickins page_cache_release(new_page); 175965500d23SHugh Dickins oom: 1760920fc356SHugh Dickins if (old_page) 17611da177e4SLinus Torvalds page_cache_release(old_page); 17621da177e4SLinus Torvalds return VM_FAULT_OOM; 17639637a5efSDavid Howells 17649637a5efSDavid Howells unwritable_page: 17659637a5efSDavid Howells page_cache_release(old_page); 17669637a5efSDavid Howells return VM_FAULT_SIGBUS; 17671da177e4SLinus Torvalds } 17681da177e4SLinus Torvalds 17691da177e4SLinus Torvalds /* 17701da177e4SLinus Torvalds * Helper functions for unmap_mapping_range(). 17711da177e4SLinus Torvalds * 17721da177e4SLinus Torvalds * __ Notes on dropping i_mmap_lock to reduce latency while unmapping __ 17731da177e4SLinus Torvalds * 17741da177e4SLinus Torvalds * We have to restart searching the prio_tree whenever we drop the lock, 17751da177e4SLinus Torvalds * since the iterator is only valid while the lock is held, and anyway 17761da177e4SLinus Torvalds * a later vma might be split and reinserted earlier while lock dropped. 17771da177e4SLinus Torvalds * 17781da177e4SLinus Torvalds * The list of nonlinear vmas could be handled more efficiently, using 17791da177e4SLinus Torvalds * a placeholder, but handle it in the same way until a need is shown. 17801da177e4SLinus Torvalds * It is important to search the prio_tree before nonlinear list: a vma 17811da177e4SLinus Torvalds * may become nonlinear and be shifted from prio_tree to nonlinear list 17821da177e4SLinus Torvalds * while the lock is dropped; but never shifted from list to prio_tree. 17831da177e4SLinus Torvalds * 17841da177e4SLinus Torvalds * In order to make forward progress despite restarting the search, 17851da177e4SLinus Torvalds * vm_truncate_count is used to mark a vma as now dealt with, so we can 17861da177e4SLinus Torvalds * quickly skip it next time around. Since the prio_tree search only 17871da177e4SLinus Torvalds * shows us those vmas affected by unmapping the range in question, we 17881da177e4SLinus Torvalds * can't efficiently keep all vmas in step with mapping->truncate_count: 17891da177e4SLinus Torvalds * so instead reset them all whenever it wraps back to 0 (then go to 1). 17901da177e4SLinus Torvalds * mapping->truncate_count and vma->vm_truncate_count are protected by 17911da177e4SLinus Torvalds * i_mmap_lock. 17921da177e4SLinus Torvalds * 17931da177e4SLinus Torvalds * In order to make forward progress despite repeatedly restarting some 1794ee39b37bSHugh Dickins * large vma, note the restart_addr from unmap_vmas when it breaks out: 17951da177e4SLinus Torvalds * and restart from that address when we reach that vma again. It might 17961da177e4SLinus Torvalds * have been split or merged, shrunk or extended, but never shifted: so 17971da177e4SLinus Torvalds * restart_addr remains valid so long as it remains in the vma's range. 17981da177e4SLinus Torvalds * unmap_mapping_range forces truncate_count to leap over page-aligned 17991da177e4SLinus Torvalds * values so we can save vma's restart_addr in its truncate_count field. 18001da177e4SLinus Torvalds */ 18011da177e4SLinus Torvalds #define is_restart_addr(truncate_count) (!((truncate_count) & ~PAGE_MASK)) 18021da177e4SLinus Torvalds 18031da177e4SLinus Torvalds static void reset_vma_truncate_counts(struct address_space *mapping) 18041da177e4SLinus Torvalds { 18051da177e4SLinus Torvalds struct vm_area_struct *vma; 18061da177e4SLinus Torvalds struct prio_tree_iter iter; 18071da177e4SLinus Torvalds 18081da177e4SLinus Torvalds vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, ULONG_MAX) 18091da177e4SLinus Torvalds vma->vm_truncate_count = 0; 18101da177e4SLinus Torvalds list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list) 18111da177e4SLinus Torvalds vma->vm_truncate_count = 0; 18121da177e4SLinus Torvalds } 18131da177e4SLinus Torvalds 18141da177e4SLinus Torvalds static int unmap_mapping_range_vma(struct vm_area_struct *vma, 18151da177e4SLinus Torvalds unsigned long start_addr, unsigned long end_addr, 18161da177e4SLinus Torvalds struct zap_details *details) 18171da177e4SLinus Torvalds { 18181da177e4SLinus Torvalds unsigned long restart_addr; 18191da177e4SLinus Torvalds int need_break; 18201da177e4SLinus Torvalds 1821d00806b1SNick Piggin /* 1822d00806b1SNick Piggin * files that support invalidating or truncating portions of the 1823d0217ac0SNick Piggin * file from under mmaped areas must have their ->fault function 182483c54070SNick Piggin * return a locked page (and set VM_FAULT_LOCKED in the return). 182583c54070SNick Piggin * This provides synchronisation against concurrent unmapping here. 1826d00806b1SNick Piggin */ 1827d00806b1SNick Piggin 18281da177e4SLinus Torvalds again: 18291da177e4SLinus Torvalds restart_addr = vma->vm_truncate_count; 18301da177e4SLinus Torvalds if (is_restart_addr(restart_addr) && start_addr < restart_addr) { 18311da177e4SLinus Torvalds start_addr = restart_addr; 18321da177e4SLinus Torvalds if (start_addr >= end_addr) { 18331da177e4SLinus Torvalds /* Top of vma has been split off since last time */ 18341da177e4SLinus Torvalds vma->vm_truncate_count = details->truncate_count; 18351da177e4SLinus Torvalds return 0; 18361da177e4SLinus Torvalds } 18371da177e4SLinus Torvalds } 18381da177e4SLinus Torvalds 1839ee39b37bSHugh Dickins restart_addr = zap_page_range(vma, start_addr, 1840ee39b37bSHugh Dickins end_addr - start_addr, details); 184195c354feSNick Piggin need_break = need_resched() || spin_needbreak(details->i_mmap_lock); 18421da177e4SLinus Torvalds 1843ee39b37bSHugh Dickins if (restart_addr >= end_addr) { 18441da177e4SLinus Torvalds /* We have now completed this vma: mark it so */ 18451da177e4SLinus Torvalds vma->vm_truncate_count = details->truncate_count; 18461da177e4SLinus Torvalds if (!need_break) 18471da177e4SLinus Torvalds return 0; 18481da177e4SLinus Torvalds } else { 18491da177e4SLinus Torvalds /* Note restart_addr in vma's truncate_count field */ 1850ee39b37bSHugh Dickins vma->vm_truncate_count = restart_addr; 18511da177e4SLinus Torvalds if (!need_break) 18521da177e4SLinus Torvalds goto again; 18531da177e4SLinus Torvalds } 18541da177e4SLinus Torvalds 18551da177e4SLinus Torvalds spin_unlock(details->i_mmap_lock); 18561da177e4SLinus Torvalds cond_resched(); 18571da177e4SLinus Torvalds spin_lock(details->i_mmap_lock); 18581da177e4SLinus Torvalds return -EINTR; 18591da177e4SLinus Torvalds } 18601da177e4SLinus Torvalds 18611da177e4SLinus Torvalds static inline void unmap_mapping_range_tree(struct prio_tree_root *root, 18621da177e4SLinus Torvalds struct zap_details *details) 18631da177e4SLinus Torvalds { 18641da177e4SLinus Torvalds struct vm_area_struct *vma; 18651da177e4SLinus Torvalds struct prio_tree_iter iter; 18661da177e4SLinus Torvalds pgoff_t vba, vea, zba, zea; 18671da177e4SLinus Torvalds 18681da177e4SLinus Torvalds restart: 18691da177e4SLinus Torvalds vma_prio_tree_foreach(vma, &iter, root, 18701da177e4SLinus Torvalds details->first_index, details->last_index) { 18711da177e4SLinus Torvalds /* Skip quickly over those we have already dealt with */ 18721da177e4SLinus Torvalds if (vma->vm_truncate_count == details->truncate_count) 18731da177e4SLinus Torvalds continue; 18741da177e4SLinus Torvalds 18751da177e4SLinus Torvalds vba = vma->vm_pgoff; 18761da177e4SLinus Torvalds vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1; 18771da177e4SLinus Torvalds /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */ 18781da177e4SLinus Torvalds zba = details->first_index; 18791da177e4SLinus Torvalds if (zba < vba) 18801da177e4SLinus Torvalds zba = vba; 18811da177e4SLinus Torvalds zea = details->last_index; 18821da177e4SLinus Torvalds if (zea > vea) 18831da177e4SLinus Torvalds zea = vea; 18841da177e4SLinus Torvalds 18851da177e4SLinus Torvalds if (unmap_mapping_range_vma(vma, 18861da177e4SLinus Torvalds ((zba - vba) << PAGE_SHIFT) + vma->vm_start, 18871da177e4SLinus Torvalds ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start, 18881da177e4SLinus Torvalds details) < 0) 18891da177e4SLinus Torvalds goto restart; 18901da177e4SLinus Torvalds } 18911da177e4SLinus Torvalds } 18921da177e4SLinus Torvalds 18931da177e4SLinus Torvalds static inline void unmap_mapping_range_list(struct list_head *head, 18941da177e4SLinus Torvalds struct zap_details *details) 18951da177e4SLinus Torvalds { 18961da177e4SLinus Torvalds struct vm_area_struct *vma; 18971da177e4SLinus Torvalds 18981da177e4SLinus Torvalds /* 18991da177e4SLinus Torvalds * In nonlinear VMAs there is no correspondence between virtual address 19001da177e4SLinus Torvalds * offset and file offset. So we must perform an exhaustive search 19011da177e4SLinus Torvalds * across *all* the pages in each nonlinear VMA, not just the pages 19021da177e4SLinus Torvalds * whose virtual address lies outside the file truncation point. 19031da177e4SLinus Torvalds */ 19041da177e4SLinus Torvalds restart: 19051da177e4SLinus Torvalds list_for_each_entry(vma, head, shared.vm_set.list) { 19061da177e4SLinus Torvalds /* Skip quickly over those we have already dealt with */ 19071da177e4SLinus Torvalds if (vma->vm_truncate_count == details->truncate_count) 19081da177e4SLinus Torvalds continue; 19091da177e4SLinus Torvalds details->nonlinear_vma = vma; 19101da177e4SLinus Torvalds if (unmap_mapping_range_vma(vma, vma->vm_start, 19111da177e4SLinus Torvalds vma->vm_end, details) < 0) 19121da177e4SLinus Torvalds goto restart; 19131da177e4SLinus Torvalds } 19141da177e4SLinus Torvalds } 19151da177e4SLinus Torvalds 19161da177e4SLinus Torvalds /** 191772fd4a35SRobert 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. 19183d41088fSMartin Waitz * @mapping: the address space containing mmaps to be unmapped. 19191da177e4SLinus Torvalds * @holebegin: byte in first page to unmap, relative to the start of 19201da177e4SLinus Torvalds * the underlying file. This will be rounded down to a PAGE_SIZE 19211da177e4SLinus Torvalds * boundary. Note that this is different from vmtruncate(), which 19221da177e4SLinus Torvalds * must keep the partial page. In contrast, we must get rid of 19231da177e4SLinus Torvalds * partial pages. 19241da177e4SLinus Torvalds * @holelen: size of prospective hole in bytes. This will be rounded 19251da177e4SLinus Torvalds * up to a PAGE_SIZE boundary. A holelen of zero truncates to the 19261da177e4SLinus Torvalds * end of the file. 19271da177e4SLinus Torvalds * @even_cows: 1 when truncating a file, unmap even private COWed pages; 19281da177e4SLinus Torvalds * but 0 when invalidating pagecache, don't throw away private data. 19291da177e4SLinus Torvalds */ 19301da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping, 19311da177e4SLinus Torvalds loff_t const holebegin, loff_t const holelen, int even_cows) 19321da177e4SLinus Torvalds { 19331da177e4SLinus Torvalds struct zap_details details; 19341da177e4SLinus Torvalds pgoff_t hba = holebegin >> PAGE_SHIFT; 19351da177e4SLinus Torvalds pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 19361da177e4SLinus Torvalds 19371da177e4SLinus Torvalds /* Check for overflow. */ 19381da177e4SLinus Torvalds if (sizeof(holelen) > sizeof(hlen)) { 19391da177e4SLinus Torvalds long long holeend = 19401da177e4SLinus Torvalds (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 19411da177e4SLinus Torvalds if (holeend & ~(long long)ULONG_MAX) 19421da177e4SLinus Torvalds hlen = ULONG_MAX - hba + 1; 19431da177e4SLinus Torvalds } 19441da177e4SLinus Torvalds 19451da177e4SLinus Torvalds details.check_mapping = even_cows? NULL: mapping; 19461da177e4SLinus Torvalds details.nonlinear_vma = NULL; 19471da177e4SLinus Torvalds details.first_index = hba; 19481da177e4SLinus Torvalds details.last_index = hba + hlen - 1; 19491da177e4SLinus Torvalds if (details.last_index < details.first_index) 19501da177e4SLinus Torvalds details.last_index = ULONG_MAX; 19511da177e4SLinus Torvalds details.i_mmap_lock = &mapping->i_mmap_lock; 19521da177e4SLinus Torvalds 19531da177e4SLinus Torvalds spin_lock(&mapping->i_mmap_lock); 19541da177e4SLinus Torvalds 1955d00806b1SNick Piggin /* Protect against endless unmapping loops */ 19561da177e4SLinus Torvalds mapping->truncate_count++; 19571da177e4SLinus Torvalds if (unlikely(is_restart_addr(mapping->truncate_count))) { 19581da177e4SLinus Torvalds if (mapping->truncate_count == 0) 19591da177e4SLinus Torvalds reset_vma_truncate_counts(mapping); 19601da177e4SLinus Torvalds mapping->truncate_count++; 19611da177e4SLinus Torvalds } 19621da177e4SLinus Torvalds details.truncate_count = mapping->truncate_count; 19631da177e4SLinus Torvalds 19641da177e4SLinus Torvalds if (unlikely(!prio_tree_empty(&mapping->i_mmap))) 19651da177e4SLinus Torvalds unmap_mapping_range_tree(&mapping->i_mmap, &details); 19661da177e4SLinus Torvalds if (unlikely(!list_empty(&mapping->i_mmap_nonlinear))) 19671da177e4SLinus Torvalds unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details); 19681da177e4SLinus Torvalds spin_unlock(&mapping->i_mmap_lock); 19691da177e4SLinus Torvalds } 19701da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range); 19711da177e4SLinus Torvalds 1972bfa5bf6dSRolf Eike Beer /** 1973bfa5bf6dSRolf Eike Beer * vmtruncate - unmap mappings "freed" by truncate() syscall 1974bfa5bf6dSRolf Eike Beer * @inode: inode of the file used 1975bfa5bf6dSRolf Eike Beer * @offset: file offset to start truncating 19761da177e4SLinus Torvalds * 19771da177e4SLinus Torvalds * NOTE! We have to be ready to update the memory sharing 19781da177e4SLinus Torvalds * between the file and the memory map for a potential last 19791da177e4SLinus Torvalds * incomplete page. Ugly, but necessary. 19801da177e4SLinus Torvalds */ 19811da177e4SLinus Torvalds int vmtruncate(struct inode * inode, loff_t offset) 19821da177e4SLinus Torvalds { 198361d5048fSChristoph Hellwig if (inode->i_size < offset) { 19841da177e4SLinus Torvalds unsigned long limit; 19851da177e4SLinus Torvalds 19861da177e4SLinus Torvalds limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur; 19871da177e4SLinus Torvalds if (limit != RLIM_INFINITY && offset > limit) 19881da177e4SLinus Torvalds goto out_sig; 19891da177e4SLinus Torvalds if (offset > inode->i_sb->s_maxbytes) 19901da177e4SLinus Torvalds goto out_big; 19911da177e4SLinus Torvalds i_size_write(inode, offset); 199261d5048fSChristoph Hellwig } else { 199361d5048fSChristoph Hellwig struct address_space *mapping = inode->i_mapping; 19941da177e4SLinus Torvalds 199561d5048fSChristoph Hellwig /* 199661d5048fSChristoph Hellwig * truncation of in-use swapfiles is disallowed - it would 199761d5048fSChristoph Hellwig * cause subsequent swapout to scribble on the now-freed 199861d5048fSChristoph Hellwig * blocks. 199961d5048fSChristoph Hellwig */ 200061d5048fSChristoph Hellwig if (IS_SWAPFILE(inode)) 200161d5048fSChristoph Hellwig return -ETXTBSY; 200261d5048fSChristoph Hellwig i_size_write(inode, offset); 200361d5048fSChristoph Hellwig 200461d5048fSChristoph Hellwig /* 200561d5048fSChristoph Hellwig * unmap_mapping_range is called twice, first simply for 200661d5048fSChristoph Hellwig * efficiency so that truncate_inode_pages does fewer 200761d5048fSChristoph Hellwig * single-page unmaps. However after this first call, and 200861d5048fSChristoph Hellwig * before truncate_inode_pages finishes, it is possible for 200961d5048fSChristoph Hellwig * private pages to be COWed, which remain after 201061d5048fSChristoph Hellwig * truncate_inode_pages finishes, hence the second 201161d5048fSChristoph Hellwig * unmap_mapping_range call must be made for correctness. 201261d5048fSChristoph Hellwig */ 201361d5048fSChristoph Hellwig unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1); 201461d5048fSChristoph Hellwig truncate_inode_pages(mapping, offset); 201561d5048fSChristoph Hellwig unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1); 201661d5048fSChristoph Hellwig } 201761d5048fSChristoph Hellwig 20181da177e4SLinus Torvalds if (inode->i_op && inode->i_op->truncate) 20191da177e4SLinus Torvalds inode->i_op->truncate(inode); 20201da177e4SLinus Torvalds return 0; 202161d5048fSChristoph Hellwig 20221da177e4SLinus Torvalds out_sig: 20231da177e4SLinus Torvalds send_sig(SIGXFSZ, current, 0); 20241da177e4SLinus Torvalds out_big: 20251da177e4SLinus Torvalds return -EFBIG; 20261da177e4SLinus Torvalds } 20271da177e4SLinus Torvalds EXPORT_SYMBOL(vmtruncate); 20281da177e4SLinus Torvalds 2029f6b3ec23SBadari Pulavarty int vmtruncate_range(struct inode *inode, loff_t offset, loff_t end) 2030f6b3ec23SBadari Pulavarty { 2031f6b3ec23SBadari Pulavarty struct address_space *mapping = inode->i_mapping; 2032f6b3ec23SBadari Pulavarty 2033f6b3ec23SBadari Pulavarty /* 2034f6b3ec23SBadari Pulavarty * If the underlying filesystem is not going to provide 2035f6b3ec23SBadari Pulavarty * a way to truncate a range of blocks (punch a hole) - 2036f6b3ec23SBadari Pulavarty * we should return failure right now. 2037f6b3ec23SBadari Pulavarty */ 2038f6b3ec23SBadari Pulavarty if (!inode->i_op || !inode->i_op->truncate_range) 2039f6b3ec23SBadari Pulavarty return -ENOSYS; 2040f6b3ec23SBadari Pulavarty 20411b1dcc1bSJes Sorensen mutex_lock(&inode->i_mutex); 2042f6b3ec23SBadari Pulavarty down_write(&inode->i_alloc_sem); 2043f6b3ec23SBadari Pulavarty unmap_mapping_range(mapping, offset, (end - offset), 1); 2044f6b3ec23SBadari Pulavarty truncate_inode_pages_range(mapping, offset, end); 2045d00806b1SNick Piggin unmap_mapping_range(mapping, offset, (end - offset), 1); 2046f6b3ec23SBadari Pulavarty inode->i_op->truncate_range(inode, offset, end); 2047f6b3ec23SBadari Pulavarty up_write(&inode->i_alloc_sem); 20481b1dcc1bSJes Sorensen mutex_unlock(&inode->i_mutex); 2049f6b3ec23SBadari Pulavarty 2050f6b3ec23SBadari Pulavarty return 0; 2051f6b3ec23SBadari Pulavarty } 2052f6b3ec23SBadari Pulavarty 20531da177e4SLinus Torvalds /* 20548f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 20558f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 20568f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 20571da177e4SLinus Torvalds */ 205865500d23SHugh Dickins static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma, 205965500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 206065500d23SHugh Dickins int write_access, pte_t orig_pte) 20611da177e4SLinus Torvalds { 20628f4e2101SHugh Dickins spinlock_t *ptl; 20631da177e4SLinus Torvalds struct page *page; 206465500d23SHugh Dickins swp_entry_t entry; 20651da177e4SLinus Torvalds pte_t pte; 206683c54070SNick Piggin int ret = 0; 20671da177e4SLinus Torvalds 20684c21e2f2SHugh Dickins if (!pte_unmap_same(mm, pmd, page_table, orig_pte)) 20698f4e2101SHugh Dickins goto out; 207065500d23SHugh Dickins 207165500d23SHugh Dickins entry = pte_to_swp_entry(orig_pte); 20720697212aSChristoph Lameter if (is_migration_entry(entry)) { 20730697212aSChristoph Lameter migration_entry_wait(mm, pmd, address); 20740697212aSChristoph Lameter goto out; 20750697212aSChristoph Lameter } 20760ff92245SShailabh Nagar delayacct_set_flag(DELAYACCT_PF_SWAPIN); 20771da177e4SLinus Torvalds page = lookup_swap_cache(entry); 20781da177e4SLinus Torvalds if (!page) { 2079098fe651SAshwin Chaugule grab_swap_token(); /* Contend for token _before_ read-in */ 208002098feaSHugh Dickins page = swapin_readahead(entry, 208102098feaSHugh Dickins GFP_HIGHUSER_MOVABLE, vma, address); 20821da177e4SLinus Torvalds if (!page) { 20831da177e4SLinus Torvalds /* 20848f4e2101SHugh Dickins * Back out if somebody else faulted in this pte 20858f4e2101SHugh Dickins * while we released the pte lock. 20861da177e4SLinus Torvalds */ 20878f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 20881da177e4SLinus Torvalds if (likely(pte_same(*page_table, orig_pte))) 20891da177e4SLinus Torvalds ret = VM_FAULT_OOM; 20900ff92245SShailabh Nagar delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 209165500d23SHugh Dickins goto unlock; 20921da177e4SLinus Torvalds } 20931da177e4SLinus Torvalds 20941da177e4SLinus Torvalds /* Had to read the page from swap area: Major fault */ 20951da177e4SLinus Torvalds ret = VM_FAULT_MAJOR; 2096f8891e5eSChristoph Lameter count_vm_event(PGMAJFAULT); 20971da177e4SLinus Torvalds } 20981da177e4SLinus Torvalds 2099e1a1cd59SBalbir Singh if (mem_cgroup_charge(page, mm, GFP_KERNEL)) { 21008a9f3ccdSBalbir Singh delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 21018a9f3ccdSBalbir Singh ret = VM_FAULT_OOM; 21028a9f3ccdSBalbir Singh goto out; 21038a9f3ccdSBalbir Singh } 21048a9f3ccdSBalbir Singh 21051da177e4SLinus Torvalds mark_page_accessed(page); 21061da177e4SLinus Torvalds lock_page(page); 210720a1022dSBalbir Singh delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 21081da177e4SLinus Torvalds 21091da177e4SLinus Torvalds /* 21108f4e2101SHugh Dickins * Back out if somebody else already faulted in this pte. 21111da177e4SLinus Torvalds */ 21128f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 21139e9bef07SHugh Dickins if (unlikely(!pte_same(*page_table, orig_pte))) 2114b8107480SKirill Korotaev goto out_nomap; 2115b8107480SKirill Korotaev 2116b8107480SKirill Korotaev if (unlikely(!PageUptodate(page))) { 2117b8107480SKirill Korotaev ret = VM_FAULT_SIGBUS; 2118b8107480SKirill Korotaev goto out_nomap; 21191da177e4SLinus Torvalds } 21201da177e4SLinus Torvalds 21211da177e4SLinus Torvalds /* The page isn't present yet, go ahead with the fault. */ 21221da177e4SLinus Torvalds 21234294621fSHugh Dickins inc_mm_counter(mm, anon_rss); 21241da177e4SLinus Torvalds pte = mk_pte(page, vma->vm_page_prot); 21251da177e4SLinus Torvalds if (write_access && can_share_swap_page(page)) { 21261da177e4SLinus Torvalds pte = maybe_mkwrite(pte_mkdirty(pte), vma); 21271da177e4SLinus Torvalds write_access = 0; 21281da177e4SLinus Torvalds } 21291da177e4SLinus Torvalds 21301da177e4SLinus Torvalds flush_icache_page(vma, page); 21311da177e4SLinus Torvalds set_pte_at(mm, address, page_table, pte); 21321da177e4SLinus Torvalds page_add_anon_rmap(page, vma, address); 21331da177e4SLinus Torvalds 2134c475a8abSHugh Dickins swap_free(entry); 2135c475a8abSHugh Dickins if (vm_swap_full()) 2136c475a8abSHugh Dickins remove_exclusive_swap_page(page); 2137c475a8abSHugh Dickins unlock_page(page); 2138c475a8abSHugh Dickins 21391da177e4SLinus Torvalds if (write_access) { 214061469f1dSHugh Dickins ret |= do_wp_page(mm, vma, address, page_table, pmd, ptl, pte); 214161469f1dSHugh Dickins if (ret & VM_FAULT_ERROR) 214261469f1dSHugh Dickins ret &= VM_FAULT_ERROR; 21431da177e4SLinus Torvalds goto out; 21441da177e4SLinus Torvalds } 21451da177e4SLinus Torvalds 21461da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 21471da177e4SLinus Torvalds update_mmu_cache(vma, address, pte); 214865500d23SHugh Dickins unlock: 21498f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 21501da177e4SLinus Torvalds out: 21511da177e4SLinus Torvalds return ret; 2152b8107480SKirill Korotaev out_nomap: 21538a9f3ccdSBalbir Singh mem_cgroup_uncharge_page(page); 21548f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 2155b8107480SKirill Korotaev unlock_page(page); 2156b8107480SKirill Korotaev page_cache_release(page); 215765500d23SHugh Dickins return ret; 21581da177e4SLinus Torvalds } 21591da177e4SLinus Torvalds 21601da177e4SLinus Torvalds /* 21618f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 21628f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 21638f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 21641da177e4SLinus Torvalds */ 216565500d23SHugh Dickins static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma, 216665500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 216765500d23SHugh Dickins int write_access) 21681da177e4SLinus Torvalds { 21698f4e2101SHugh Dickins struct page *page; 21708f4e2101SHugh Dickins spinlock_t *ptl; 21711da177e4SLinus Torvalds pte_t entry; 21721da177e4SLinus Torvalds 21731da177e4SLinus Torvalds /* Allocate our own private page. */ 21741da177e4SLinus Torvalds pte_unmap(page_table); 21751da177e4SLinus Torvalds 21761da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 217765500d23SHugh Dickins goto oom; 2178769848c0SMel Gorman page = alloc_zeroed_user_highpage_movable(vma, address); 21791da177e4SLinus Torvalds if (!page) 218065500d23SHugh Dickins goto oom; 21810ed361deSNick Piggin __SetPageUptodate(page); 21821da177e4SLinus Torvalds 2183e1a1cd59SBalbir Singh if (mem_cgroup_charge(page, mm, GFP_KERNEL)) 21848a9f3ccdSBalbir Singh goto oom_free_page; 21858a9f3ccdSBalbir Singh 218665500d23SHugh Dickins entry = mk_pte(page, vma->vm_page_prot); 218765500d23SHugh Dickins entry = maybe_mkwrite(pte_mkdirty(entry), vma); 21888f4e2101SHugh Dickins 21898f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 21908f4e2101SHugh Dickins if (!pte_none(*page_table)) 21918f4e2101SHugh Dickins goto release; 21928f4e2101SHugh Dickins inc_mm_counter(mm, anon_rss); 21931da177e4SLinus Torvalds lru_cache_add_active(page); 21949617d95eSNick Piggin page_add_new_anon_rmap(page, vma, address); 219565500d23SHugh Dickins set_pte_at(mm, address, page_table, entry); 21961da177e4SLinus Torvalds 21971da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 219865500d23SHugh Dickins update_mmu_cache(vma, address, entry); 219965500d23SHugh Dickins unlock: 22008f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 220183c54070SNick Piggin return 0; 22028f4e2101SHugh Dickins release: 22038a9f3ccdSBalbir Singh mem_cgroup_uncharge_page(page); 22048f4e2101SHugh Dickins page_cache_release(page); 22058f4e2101SHugh Dickins goto unlock; 22068a9f3ccdSBalbir Singh oom_free_page: 22076dbf6d3bSHugh Dickins page_cache_release(page); 220865500d23SHugh Dickins oom: 22091da177e4SLinus Torvalds return VM_FAULT_OOM; 22101da177e4SLinus Torvalds } 22111da177e4SLinus Torvalds 22121da177e4SLinus Torvalds /* 221354cb8821SNick Piggin * __do_fault() tries to create a new page mapping. It aggressively 22141da177e4SLinus Torvalds * tries to share with existing pages, but makes a separate copy if 221554cb8821SNick Piggin * the FAULT_FLAG_WRITE is set in the flags parameter in order to avoid 221654cb8821SNick Piggin * the next page fault. 22171da177e4SLinus Torvalds * 22181da177e4SLinus Torvalds * As this is called only for pages that do not currently exist, we 22191da177e4SLinus Torvalds * do not need to flush old virtual caches or the TLB. 22201da177e4SLinus Torvalds * 22218f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 222216abfa08SHugh Dickins * but allow concurrent faults), and pte neither mapped nor locked. 22238f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 22241da177e4SLinus Torvalds */ 222554cb8821SNick Piggin static int __do_fault(struct mm_struct *mm, struct vm_area_struct *vma, 222616abfa08SHugh Dickins unsigned long address, pmd_t *pmd, 222754cb8821SNick Piggin pgoff_t pgoff, unsigned int flags, pte_t orig_pte) 22281da177e4SLinus Torvalds { 222916abfa08SHugh Dickins pte_t *page_table; 22308f4e2101SHugh Dickins spinlock_t *ptl; 2231d0217ac0SNick Piggin struct page *page; 22321da177e4SLinus Torvalds pte_t entry; 22331da177e4SLinus Torvalds int anon = 0; 2234d08b3851SPeter Zijlstra struct page *dirty_page = NULL; 2235d0217ac0SNick Piggin struct vm_fault vmf; 2236d0217ac0SNick Piggin int ret; 2237a200ee18SPeter Zijlstra int page_mkwrite = 0; 223854cb8821SNick Piggin 2239d0217ac0SNick Piggin vmf.virtual_address = (void __user *)(address & PAGE_MASK); 2240d0217ac0SNick Piggin vmf.pgoff = pgoff; 2241d0217ac0SNick Piggin vmf.flags = flags; 2242d0217ac0SNick Piggin vmf.page = NULL; 22431da177e4SLinus Torvalds 2244325f04dbSHugh Dickins BUG_ON(vma->vm_flags & VM_PFNMAP); 2245325f04dbSHugh Dickins 2246d0217ac0SNick Piggin ret = vma->vm_ops->fault(vma, &vmf); 224783c54070SNick Piggin if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) 224883c54070SNick Piggin return ret; 22491da177e4SLinus Torvalds 2250d00806b1SNick Piggin /* 2251d0217ac0SNick Piggin * For consistency in subsequent calls, make the faulted page always 2252d00806b1SNick Piggin * locked. 2253d00806b1SNick Piggin */ 225483c54070SNick Piggin if (unlikely(!(ret & VM_FAULT_LOCKED))) 2255d0217ac0SNick Piggin lock_page(vmf.page); 225654cb8821SNick Piggin else 2257d0217ac0SNick Piggin VM_BUG_ON(!PageLocked(vmf.page)); 2258d00806b1SNick Piggin 22591da177e4SLinus Torvalds /* 22601da177e4SLinus Torvalds * Should we do an early C-O-W break? 22611da177e4SLinus Torvalds */ 2262d0217ac0SNick Piggin page = vmf.page; 226354cb8821SNick Piggin if (flags & FAULT_FLAG_WRITE) { 22649637a5efSDavid Howells if (!(vma->vm_flags & VM_SHARED)) { 226554cb8821SNick Piggin anon = 1; 2266d00806b1SNick Piggin if (unlikely(anon_vma_prepare(vma))) { 2267d0217ac0SNick Piggin ret = VM_FAULT_OOM; 226854cb8821SNick Piggin goto out; 2269d00806b1SNick Piggin } 227083c54070SNick Piggin page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, 227183c54070SNick Piggin vma, address); 2272d00806b1SNick Piggin if (!page) { 2273d0217ac0SNick Piggin ret = VM_FAULT_OOM; 227454cb8821SNick Piggin goto out; 2275d00806b1SNick Piggin } 2276d0217ac0SNick Piggin copy_user_highpage(page, vmf.page, address, vma); 22770ed361deSNick Piggin __SetPageUptodate(page); 22789637a5efSDavid Howells } else { 227954cb8821SNick Piggin /* 228054cb8821SNick Piggin * If the page will be shareable, see if the backing 22819637a5efSDavid Howells * address space wants to know that the page is about 228254cb8821SNick Piggin * to become writable 228354cb8821SNick Piggin */ 228469676147SMark Fasheh if (vma->vm_ops->page_mkwrite) { 228569676147SMark Fasheh unlock_page(page); 228669676147SMark Fasheh if (vma->vm_ops->page_mkwrite(vma, page) < 0) { 2287d0217ac0SNick Piggin ret = VM_FAULT_SIGBUS; 2288d0217ac0SNick Piggin anon = 1; /* no anon but release vmf.page */ 228969676147SMark Fasheh goto out_unlocked; 229069676147SMark Fasheh } 229169676147SMark Fasheh lock_page(page); 2292d0217ac0SNick Piggin /* 2293d0217ac0SNick Piggin * XXX: this is not quite right (racy vs 2294d0217ac0SNick Piggin * invalidate) to unlock and relock the page 2295d0217ac0SNick Piggin * like this, however a better fix requires 2296d0217ac0SNick Piggin * reworking page_mkwrite locking API, which 2297d0217ac0SNick Piggin * is better done later. 2298d0217ac0SNick Piggin */ 2299d0217ac0SNick Piggin if (!page->mapping) { 230083c54070SNick Piggin ret = 0; 2301d0217ac0SNick Piggin anon = 1; /* no anon but release vmf.page */ 2302d0217ac0SNick Piggin goto out; 2303d0217ac0SNick Piggin } 2304a200ee18SPeter Zijlstra page_mkwrite = 1; 23059637a5efSDavid Howells } 23069637a5efSDavid Howells } 230754cb8821SNick Piggin 23081da177e4SLinus Torvalds } 23091da177e4SLinus Torvalds 2310e1a1cd59SBalbir Singh if (mem_cgroup_charge(page, mm, GFP_KERNEL)) { 23118a9f3ccdSBalbir Singh ret = VM_FAULT_OOM; 23128a9f3ccdSBalbir Singh goto out; 23138a9f3ccdSBalbir Singh } 23148a9f3ccdSBalbir Singh 23158f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 23161da177e4SLinus Torvalds 23171da177e4SLinus Torvalds /* 23181da177e4SLinus Torvalds * This silly early PAGE_DIRTY setting removes a race 23191da177e4SLinus Torvalds * due to the bad i386 page protection. But it's valid 23201da177e4SLinus Torvalds * for other architectures too. 23211da177e4SLinus Torvalds * 23221da177e4SLinus Torvalds * Note that if write_access is true, we either now have 23231da177e4SLinus Torvalds * an exclusive copy of the page, or this is a shared mapping, 23241da177e4SLinus Torvalds * so we can make it writable and dirty to avoid having to 23251da177e4SLinus Torvalds * handle that later. 23261da177e4SLinus Torvalds */ 23271da177e4SLinus Torvalds /* Only go through if we didn't race with anybody else... */ 232854cb8821SNick Piggin if (likely(pte_same(*page_table, orig_pte))) { 2329d00806b1SNick Piggin flush_icache_page(vma, page); 2330d00806b1SNick Piggin entry = mk_pte(page, vma->vm_page_prot); 233154cb8821SNick Piggin if (flags & FAULT_FLAG_WRITE) 23321da177e4SLinus Torvalds entry = maybe_mkwrite(pte_mkdirty(entry), vma); 23331da177e4SLinus Torvalds set_pte_at(mm, address, page_table, entry); 23341da177e4SLinus Torvalds if (anon) { 23354294621fSHugh Dickins inc_mm_counter(mm, anon_rss); 2336d00806b1SNick Piggin lru_cache_add_active(page); 2337d00806b1SNick Piggin page_add_new_anon_rmap(page, vma, address); 2338f57e88a8SHugh Dickins } else { 23394294621fSHugh Dickins inc_mm_counter(mm, file_rss); 2340d00806b1SNick Piggin page_add_file_rmap(page); 234154cb8821SNick Piggin if (flags & FAULT_FLAG_WRITE) { 2342d00806b1SNick Piggin dirty_page = page; 2343d08b3851SPeter Zijlstra get_page(dirty_page); 2344d08b3851SPeter Zijlstra } 23454294621fSHugh Dickins } 23461da177e4SLinus Torvalds 2347d00806b1SNick Piggin /* no need to invalidate: a not-present page won't be cached */ 23481da177e4SLinus Torvalds update_mmu_cache(vma, address, entry); 2349d00806b1SNick Piggin } else { 23508a9f3ccdSBalbir Singh mem_cgroup_uncharge_page(page); 2351d00806b1SNick Piggin if (anon) 2352d00806b1SNick Piggin page_cache_release(page); 2353d00806b1SNick Piggin else 235454cb8821SNick Piggin anon = 1; /* no anon but release faulted_page */ 2355d00806b1SNick Piggin } 2356d00806b1SNick Piggin 23578f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 2358d00806b1SNick Piggin 2359d00806b1SNick Piggin out: 2360d0217ac0SNick Piggin unlock_page(vmf.page); 236169676147SMark Fasheh out_unlocked: 2362d00806b1SNick Piggin if (anon) 2363d0217ac0SNick Piggin page_cache_release(vmf.page); 2364d00806b1SNick Piggin else if (dirty_page) { 23658f7b3d15SAnton Salikhmetov if (vma->vm_file) 23668f7b3d15SAnton Salikhmetov file_update_time(vma->vm_file); 23678f7b3d15SAnton Salikhmetov 2368a200ee18SPeter Zijlstra set_page_dirty_balance(dirty_page, page_mkwrite); 2369d08b3851SPeter Zijlstra put_page(dirty_page); 2370d08b3851SPeter Zijlstra } 2371d00806b1SNick Piggin 237283c54070SNick Piggin return ret; 237354cb8821SNick Piggin } 2374d00806b1SNick Piggin 237554cb8821SNick Piggin static int do_linear_fault(struct mm_struct *mm, struct vm_area_struct *vma, 237654cb8821SNick Piggin unsigned long address, pte_t *page_table, pmd_t *pmd, 237754cb8821SNick Piggin int write_access, pte_t orig_pte) 237854cb8821SNick Piggin { 237954cb8821SNick Piggin pgoff_t pgoff = (((address & PAGE_MASK) 23800da7e01fSDean Nelson - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; 238154cb8821SNick Piggin unsigned int flags = (write_access ? FAULT_FLAG_WRITE : 0); 238254cb8821SNick Piggin 238316abfa08SHugh Dickins pte_unmap(page_table); 238416abfa08SHugh Dickins return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte); 238554cb8821SNick Piggin } 238654cb8821SNick Piggin 23871da177e4SLinus Torvalds 23881da177e4SLinus Torvalds /* 2389f4b81804SJes Sorensen * do_no_pfn() tries to create a new page mapping for a page without 2390f4b81804SJes Sorensen * a struct_page backing it 2391f4b81804SJes Sorensen * 2392f4b81804SJes Sorensen * As this is called only for pages that do not currently exist, we 2393f4b81804SJes Sorensen * do not need to flush old virtual caches or the TLB. 2394f4b81804SJes Sorensen * 2395f4b81804SJes Sorensen * We enter with non-exclusive mmap_sem (to exclude vma changes, 2396f4b81804SJes Sorensen * but allow concurrent faults), and pte mapped but not yet locked. 2397f4b81804SJes Sorensen * We return with mmap_sem still held, but pte unmapped and unlocked. 2398f4b81804SJes Sorensen * 2399f4b81804SJes Sorensen * It is expected that the ->nopfn handler always returns the same pfn 2400f4b81804SJes Sorensen * for a given virtual mapping. 2401f4b81804SJes Sorensen * 2402f4b81804SJes Sorensen * Mark this `noinline' to prevent it from bloating the main pagefault code. 2403f4b81804SJes Sorensen */ 2404f4b81804SJes Sorensen static noinline int do_no_pfn(struct mm_struct *mm, struct vm_area_struct *vma, 2405f4b81804SJes Sorensen unsigned long address, pte_t *page_table, pmd_t *pmd, 2406f4b81804SJes Sorensen int write_access) 2407f4b81804SJes Sorensen { 2408f4b81804SJes Sorensen spinlock_t *ptl; 2409f4b81804SJes Sorensen pte_t entry; 2410f4b81804SJes Sorensen unsigned long pfn; 2411f4b81804SJes Sorensen 2412f4b81804SJes Sorensen pte_unmap(page_table); 2413b379d790SJared Hulbert BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))); 2414b379d790SJared Hulbert BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); 2415f4b81804SJes Sorensen 2416f4b81804SJes Sorensen pfn = vma->vm_ops->nopfn(vma, address & PAGE_MASK); 2417b379d790SJared Hulbert 2418b379d790SJared Hulbert BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn)); 2419b379d790SJared Hulbert 242022cd25edSBenjamin Herrenschmidt if (unlikely(pfn == NOPFN_OOM)) 2421f4b81804SJes Sorensen return VM_FAULT_OOM; 242222cd25edSBenjamin Herrenschmidt else if (unlikely(pfn == NOPFN_SIGBUS)) 2423f4b81804SJes Sorensen return VM_FAULT_SIGBUS; 242422cd25edSBenjamin Herrenschmidt else if (unlikely(pfn == NOPFN_REFAULT)) 242583c54070SNick Piggin return 0; 2426f4b81804SJes Sorensen 2427f4b81804SJes Sorensen page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 2428f4b81804SJes Sorensen 2429f4b81804SJes Sorensen /* Only go through if we didn't race with anybody else... */ 2430f4b81804SJes Sorensen if (pte_none(*page_table)) { 2431f4b81804SJes Sorensen entry = pfn_pte(pfn, vma->vm_page_prot); 2432f4b81804SJes Sorensen if (write_access) 2433f4b81804SJes Sorensen entry = maybe_mkwrite(pte_mkdirty(entry), vma); 2434f4b81804SJes Sorensen set_pte_at(mm, address, page_table, entry); 2435f4b81804SJes Sorensen } 2436f4b81804SJes Sorensen pte_unmap_unlock(page_table, ptl); 243783c54070SNick Piggin return 0; 2438f4b81804SJes Sorensen } 2439f4b81804SJes Sorensen 2440f4b81804SJes Sorensen /* 24411da177e4SLinus Torvalds * Fault of a previously existing named mapping. Repopulate the pte 24421da177e4SLinus Torvalds * from the encoded file_pte if possible. This enables swappable 24431da177e4SLinus Torvalds * nonlinear vmas. 24448f4e2101SHugh Dickins * 24458f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 24468f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 24478f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 24481da177e4SLinus Torvalds */ 2449d0217ac0SNick Piggin static int do_nonlinear_fault(struct mm_struct *mm, struct vm_area_struct *vma, 245065500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 245165500d23SHugh Dickins int write_access, pte_t orig_pte) 24521da177e4SLinus Torvalds { 2453d0217ac0SNick Piggin unsigned int flags = FAULT_FLAG_NONLINEAR | 2454d0217ac0SNick Piggin (write_access ? FAULT_FLAG_WRITE : 0); 245565500d23SHugh Dickins pgoff_t pgoff; 24561da177e4SLinus Torvalds 24574c21e2f2SHugh Dickins if (!pte_unmap_same(mm, pmd, page_table, orig_pte)) 245883c54070SNick Piggin return 0; 24591da177e4SLinus Torvalds 2460d0217ac0SNick Piggin if (unlikely(!(vma->vm_flags & VM_NONLINEAR) || 2461d0217ac0SNick Piggin !(vma->vm_flags & VM_CAN_NONLINEAR))) { 246265500d23SHugh Dickins /* 246365500d23SHugh Dickins * Page table corrupted: show pte and kill process. 246465500d23SHugh Dickins */ 2465b5810039SNick Piggin print_bad_pte(vma, orig_pte, address); 246665500d23SHugh Dickins return VM_FAULT_OOM; 246765500d23SHugh Dickins } 246865500d23SHugh Dickins 246965500d23SHugh Dickins pgoff = pte_to_pgoff(orig_pte); 247016abfa08SHugh Dickins return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte); 24711da177e4SLinus Torvalds } 24721da177e4SLinus Torvalds 24731da177e4SLinus Torvalds /* 24741da177e4SLinus Torvalds * These routines also need to handle stuff like marking pages dirty 24751da177e4SLinus Torvalds * and/or accessed for architectures that don't do it in hardware (most 24761da177e4SLinus Torvalds * RISC architectures). The early dirtying is also good on the i386. 24771da177e4SLinus Torvalds * 24781da177e4SLinus Torvalds * There is also a hook called "update_mmu_cache()" that architectures 24791da177e4SLinus Torvalds * with external mmu caches can use to update those (ie the Sparc or 24801da177e4SLinus Torvalds * PowerPC hashed page tables that act as extended TLBs). 24811da177e4SLinus Torvalds * 2482c74df32cSHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 2483c74df32cSHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 2484c74df32cSHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 24851da177e4SLinus Torvalds */ 24861da177e4SLinus Torvalds static inline int handle_pte_fault(struct mm_struct *mm, 24871da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long address, 248865500d23SHugh Dickins pte_t *pte, pmd_t *pmd, int write_access) 24891da177e4SLinus Torvalds { 24901da177e4SLinus Torvalds pte_t entry; 24918f4e2101SHugh Dickins spinlock_t *ptl; 24921da177e4SLinus Torvalds 24938dab5241SBenjamin Herrenschmidt entry = *pte; 24941da177e4SLinus Torvalds if (!pte_present(entry)) { 249565500d23SHugh Dickins if (pte_none(entry)) { 2496f4b81804SJes Sorensen if (vma->vm_ops) { 24973c18ddd1SNick Piggin if (likely(vma->vm_ops->fault)) 249854cb8821SNick Piggin return do_linear_fault(mm, vma, address, 249954cb8821SNick Piggin pte, pmd, write_access, entry); 2500f4b81804SJes Sorensen if (unlikely(vma->vm_ops->nopfn)) 2501f4b81804SJes Sorensen return do_no_pfn(mm, vma, address, pte, 2502f4b81804SJes Sorensen pmd, write_access); 2503f4b81804SJes Sorensen } 2504f4b81804SJes Sorensen return do_anonymous_page(mm, vma, address, 250565500d23SHugh Dickins pte, pmd, write_access); 250665500d23SHugh Dickins } 25071da177e4SLinus Torvalds if (pte_file(entry)) 2508d0217ac0SNick Piggin return do_nonlinear_fault(mm, vma, address, 250965500d23SHugh Dickins pte, pmd, write_access, entry); 251065500d23SHugh Dickins return do_swap_page(mm, vma, address, 251165500d23SHugh Dickins pte, pmd, write_access, entry); 25121da177e4SLinus Torvalds } 25131da177e4SLinus Torvalds 25144c21e2f2SHugh Dickins ptl = pte_lockptr(mm, pmd); 25158f4e2101SHugh Dickins spin_lock(ptl); 25168f4e2101SHugh Dickins if (unlikely(!pte_same(*pte, entry))) 25178f4e2101SHugh Dickins goto unlock; 25181da177e4SLinus Torvalds if (write_access) { 25191da177e4SLinus Torvalds if (!pte_write(entry)) 25208f4e2101SHugh Dickins return do_wp_page(mm, vma, address, 25218f4e2101SHugh Dickins pte, pmd, ptl, entry); 25221da177e4SLinus Torvalds entry = pte_mkdirty(entry); 25231da177e4SLinus Torvalds } 25241da177e4SLinus Torvalds entry = pte_mkyoung(entry); 25258dab5241SBenjamin Herrenschmidt if (ptep_set_access_flags(vma, address, pte, entry, write_access)) { 25261da177e4SLinus Torvalds update_mmu_cache(vma, address, entry); 25271a44e149SAndrea Arcangeli } else { 25281a44e149SAndrea Arcangeli /* 25291a44e149SAndrea Arcangeli * This is needed only for protection faults but the arch code 25301a44e149SAndrea Arcangeli * is not yet telling us if this is a protection fault or not. 25311a44e149SAndrea Arcangeli * This still avoids useless tlb flushes for .text page faults 25321a44e149SAndrea Arcangeli * with threads. 25331a44e149SAndrea Arcangeli */ 25341a44e149SAndrea Arcangeli if (write_access) 25351a44e149SAndrea Arcangeli flush_tlb_page(vma, address); 25361a44e149SAndrea Arcangeli } 25378f4e2101SHugh Dickins unlock: 25388f4e2101SHugh Dickins pte_unmap_unlock(pte, ptl); 253983c54070SNick Piggin return 0; 25401da177e4SLinus Torvalds } 25411da177e4SLinus Torvalds 25421da177e4SLinus Torvalds /* 25431da177e4SLinus Torvalds * By the time we get here, we already hold the mm semaphore 25441da177e4SLinus Torvalds */ 254583c54070SNick Piggin int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma, 25461da177e4SLinus Torvalds unsigned long address, int write_access) 25471da177e4SLinus Torvalds { 25481da177e4SLinus Torvalds pgd_t *pgd; 25491da177e4SLinus Torvalds pud_t *pud; 25501da177e4SLinus Torvalds pmd_t *pmd; 25511da177e4SLinus Torvalds pte_t *pte; 25521da177e4SLinus Torvalds 25531da177e4SLinus Torvalds __set_current_state(TASK_RUNNING); 25541da177e4SLinus Torvalds 2555f8891e5eSChristoph Lameter count_vm_event(PGFAULT); 25561da177e4SLinus Torvalds 2557ac9b9c66SHugh Dickins if (unlikely(is_vm_hugetlb_page(vma))) 2558ac9b9c66SHugh Dickins return hugetlb_fault(mm, vma, address, write_access); 25591da177e4SLinus Torvalds 25601da177e4SLinus Torvalds pgd = pgd_offset(mm, address); 25611da177e4SLinus Torvalds pud = pud_alloc(mm, pgd, address); 25621da177e4SLinus Torvalds if (!pud) 2563c74df32cSHugh Dickins return VM_FAULT_OOM; 25641da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, address); 25651da177e4SLinus Torvalds if (!pmd) 2566c74df32cSHugh Dickins return VM_FAULT_OOM; 25671da177e4SLinus Torvalds pte = pte_alloc_map(mm, pmd, address); 25681da177e4SLinus Torvalds if (!pte) 2569c74df32cSHugh Dickins return VM_FAULT_OOM; 25701da177e4SLinus Torvalds 257165500d23SHugh Dickins return handle_pte_fault(mm, vma, address, pte, pmd, write_access); 25721da177e4SLinus Torvalds } 25731da177e4SLinus Torvalds 25741da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED 25751da177e4SLinus Torvalds /* 25761da177e4SLinus Torvalds * Allocate page upper directory. 2577872fec16SHugh Dickins * We've already handled the fast-path in-line. 25781da177e4SLinus Torvalds */ 25791bb3630eSHugh Dickins int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address) 25801da177e4SLinus Torvalds { 2581c74df32cSHugh Dickins pud_t *new = pud_alloc_one(mm, address); 2582c74df32cSHugh Dickins if (!new) 25831bb3630eSHugh Dickins return -ENOMEM; 25841da177e4SLinus Torvalds 2585872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 25861bb3630eSHugh Dickins if (pgd_present(*pgd)) /* Another has populated it */ 25875e541973SBenjamin Herrenschmidt pud_free(mm, new); 25881bb3630eSHugh Dickins else 25891da177e4SLinus Torvalds pgd_populate(mm, pgd, new); 2590872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 25911bb3630eSHugh Dickins return 0; 25921da177e4SLinus Torvalds } 25931da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */ 25941da177e4SLinus Torvalds 25951da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED 25961da177e4SLinus Torvalds /* 25971da177e4SLinus Torvalds * Allocate page middle directory. 2598872fec16SHugh Dickins * We've already handled the fast-path in-line. 25991da177e4SLinus Torvalds */ 26001bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) 26011da177e4SLinus Torvalds { 2602c74df32cSHugh Dickins pmd_t *new = pmd_alloc_one(mm, address); 2603c74df32cSHugh Dickins if (!new) 26041bb3630eSHugh Dickins return -ENOMEM; 26051da177e4SLinus Torvalds 2606872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 26071da177e4SLinus Torvalds #ifndef __ARCH_HAS_4LEVEL_HACK 26081bb3630eSHugh Dickins if (pud_present(*pud)) /* Another has populated it */ 26095e541973SBenjamin Herrenschmidt pmd_free(mm, new); 26101bb3630eSHugh Dickins else 26111da177e4SLinus Torvalds pud_populate(mm, pud, new); 26121da177e4SLinus Torvalds #else 26131bb3630eSHugh Dickins if (pgd_present(*pud)) /* Another has populated it */ 26145e541973SBenjamin Herrenschmidt pmd_free(mm, new); 26151bb3630eSHugh Dickins else 26161da177e4SLinus Torvalds pgd_populate(mm, pud, new); 26171da177e4SLinus Torvalds #endif /* __ARCH_HAS_4LEVEL_HACK */ 2618872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 26191bb3630eSHugh Dickins return 0; 26201da177e4SLinus Torvalds } 26211da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */ 26221da177e4SLinus Torvalds 26231da177e4SLinus Torvalds int make_pages_present(unsigned long addr, unsigned long end) 26241da177e4SLinus Torvalds { 26251da177e4SLinus Torvalds int ret, len, write; 26261da177e4SLinus Torvalds struct vm_area_struct * vma; 26271da177e4SLinus Torvalds 26281da177e4SLinus Torvalds vma = find_vma(current->mm, addr); 26291da177e4SLinus Torvalds if (!vma) 26301da177e4SLinus Torvalds return -1; 26311da177e4SLinus Torvalds write = (vma->vm_flags & VM_WRITE) != 0; 26325bcb28b1SEric Sesterhenn BUG_ON(addr >= end); 26335bcb28b1SEric Sesterhenn BUG_ON(end > vma->vm_end); 263468e116a3SRolf Eike Beer len = DIV_ROUND_UP(end, PAGE_SIZE) - addr/PAGE_SIZE; 26351da177e4SLinus Torvalds ret = get_user_pages(current, current->mm, addr, 26361da177e4SLinus Torvalds len, write, 0, NULL, NULL); 26371da177e4SLinus Torvalds if (ret < 0) 26381da177e4SLinus Torvalds return ret; 26391da177e4SLinus Torvalds return ret == len ? 0 : -1; 26401da177e4SLinus Torvalds } 26411da177e4SLinus Torvalds 26421da177e4SLinus Torvalds #if !defined(__HAVE_ARCH_GATE_AREA) 26431da177e4SLinus Torvalds 26441da177e4SLinus Torvalds #if defined(AT_SYSINFO_EHDR) 26455ce7852cSAdrian Bunk static struct vm_area_struct gate_vma; 26461da177e4SLinus Torvalds 26471da177e4SLinus Torvalds static int __init gate_vma_init(void) 26481da177e4SLinus Torvalds { 26491da177e4SLinus Torvalds gate_vma.vm_mm = NULL; 26501da177e4SLinus Torvalds gate_vma.vm_start = FIXADDR_USER_START; 26511da177e4SLinus Torvalds gate_vma.vm_end = FIXADDR_USER_END; 2652b6558c4aSRoland McGrath gate_vma.vm_flags = VM_READ | VM_MAYREAD | VM_EXEC | VM_MAYEXEC; 2653b6558c4aSRoland McGrath gate_vma.vm_page_prot = __P101; 2654f47aef55SRoland McGrath /* 2655f47aef55SRoland McGrath * Make sure the vDSO gets into every core dump. 2656f47aef55SRoland McGrath * Dumping its contents makes post-mortem fully interpretable later 2657f47aef55SRoland McGrath * without matching up the same kernel and hardware config to see 2658f47aef55SRoland McGrath * what PC values meant. 2659f47aef55SRoland McGrath */ 2660f47aef55SRoland McGrath gate_vma.vm_flags |= VM_ALWAYSDUMP; 26611da177e4SLinus Torvalds return 0; 26621da177e4SLinus Torvalds } 26631da177e4SLinus Torvalds __initcall(gate_vma_init); 26641da177e4SLinus Torvalds #endif 26651da177e4SLinus Torvalds 26661da177e4SLinus Torvalds struct vm_area_struct *get_gate_vma(struct task_struct *tsk) 26671da177e4SLinus Torvalds { 26681da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR 26691da177e4SLinus Torvalds return &gate_vma; 26701da177e4SLinus Torvalds #else 26711da177e4SLinus Torvalds return NULL; 26721da177e4SLinus Torvalds #endif 26731da177e4SLinus Torvalds } 26741da177e4SLinus Torvalds 26751da177e4SLinus Torvalds int in_gate_area_no_task(unsigned long addr) 26761da177e4SLinus Torvalds { 26771da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR 26781da177e4SLinus Torvalds if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END)) 26791da177e4SLinus Torvalds return 1; 26801da177e4SLinus Torvalds #endif 26811da177e4SLinus Torvalds return 0; 26821da177e4SLinus Torvalds } 26831da177e4SLinus Torvalds 26841da177e4SLinus Torvalds #endif /* __HAVE_ARCH_GATE_AREA */ 26850ec76a11SDavid Howells 26860ec76a11SDavid Howells /* 26870ec76a11SDavid Howells * Access another process' address space. 26880ec76a11SDavid Howells * Source/target buffer must be kernel space, 26890ec76a11SDavid Howells * Do not walk the page table directly, use get_user_pages 26900ec76a11SDavid Howells */ 26910ec76a11SDavid Howells int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write) 26920ec76a11SDavid Howells { 26930ec76a11SDavid Howells struct mm_struct *mm; 26940ec76a11SDavid Howells struct vm_area_struct *vma; 26950ec76a11SDavid Howells struct page *page; 26960ec76a11SDavid Howells void *old_buf = buf; 26970ec76a11SDavid Howells 26980ec76a11SDavid Howells mm = get_task_mm(tsk); 26990ec76a11SDavid Howells if (!mm) 27000ec76a11SDavid Howells return 0; 27010ec76a11SDavid Howells 27020ec76a11SDavid Howells down_read(&mm->mmap_sem); 2703183ff22bSSimon Arlott /* ignore errors, just check how much was successfully transferred */ 27040ec76a11SDavid Howells while (len) { 27050ec76a11SDavid Howells int bytes, ret, offset; 27060ec76a11SDavid Howells void *maddr; 27070ec76a11SDavid Howells 27080ec76a11SDavid Howells ret = get_user_pages(tsk, mm, addr, 1, 27090ec76a11SDavid Howells write, 1, &page, &vma); 27100ec76a11SDavid Howells if (ret <= 0) 27110ec76a11SDavid Howells break; 27120ec76a11SDavid Howells 27130ec76a11SDavid Howells bytes = len; 27140ec76a11SDavid Howells offset = addr & (PAGE_SIZE-1); 27150ec76a11SDavid Howells if (bytes > PAGE_SIZE-offset) 27160ec76a11SDavid Howells bytes = PAGE_SIZE-offset; 27170ec76a11SDavid Howells 27180ec76a11SDavid Howells maddr = kmap(page); 27190ec76a11SDavid Howells if (write) { 27200ec76a11SDavid Howells copy_to_user_page(vma, page, addr, 27210ec76a11SDavid Howells maddr + offset, buf, bytes); 27220ec76a11SDavid Howells set_page_dirty_lock(page); 27230ec76a11SDavid Howells } else { 27240ec76a11SDavid Howells copy_from_user_page(vma, page, addr, 27250ec76a11SDavid Howells buf, maddr + offset, bytes); 27260ec76a11SDavid Howells } 27270ec76a11SDavid Howells kunmap(page); 27280ec76a11SDavid Howells page_cache_release(page); 27290ec76a11SDavid Howells len -= bytes; 27300ec76a11SDavid Howells buf += bytes; 27310ec76a11SDavid Howells addr += bytes; 27320ec76a11SDavid Howells } 27330ec76a11SDavid Howells up_read(&mm->mmap_sem); 27340ec76a11SDavid Howells mmput(mm); 27350ec76a11SDavid Howells 27360ec76a11SDavid Howells return buf - old_buf; 27370ec76a11SDavid Howells } 273803252919SAndi Kleen 273903252919SAndi Kleen /* 274003252919SAndi Kleen * Print the name of a VMA. 274103252919SAndi Kleen */ 274203252919SAndi Kleen void print_vma_addr(char *prefix, unsigned long ip) 274303252919SAndi Kleen { 274403252919SAndi Kleen struct mm_struct *mm = current->mm; 274503252919SAndi Kleen struct vm_area_struct *vma; 274603252919SAndi Kleen 2747e8bff74aSIngo Molnar /* 2748e8bff74aSIngo Molnar * Do not print if we are in atomic 2749e8bff74aSIngo Molnar * contexts (in exception stacks, etc.): 2750e8bff74aSIngo Molnar */ 2751e8bff74aSIngo Molnar if (preempt_count()) 2752e8bff74aSIngo Molnar return; 2753e8bff74aSIngo Molnar 275403252919SAndi Kleen down_read(&mm->mmap_sem); 275503252919SAndi Kleen vma = find_vma(mm, ip); 275603252919SAndi Kleen if (vma && vma->vm_file) { 275703252919SAndi Kleen struct file *f = vma->vm_file; 275803252919SAndi Kleen char *buf = (char *)__get_free_page(GFP_KERNEL); 275903252919SAndi Kleen if (buf) { 276003252919SAndi Kleen char *p, *s; 276103252919SAndi Kleen 2762cf28b486SJan Blunck p = d_path(&f->f_path, buf, PAGE_SIZE); 276303252919SAndi Kleen if (IS_ERR(p)) 276403252919SAndi Kleen p = "?"; 276503252919SAndi Kleen s = strrchr(p, '/'); 276603252919SAndi Kleen if (s) 276703252919SAndi Kleen p = s+1; 276803252919SAndi Kleen printk("%s%s[%lx+%lx]", prefix, p, 276903252919SAndi Kleen vma->vm_start, 277003252919SAndi Kleen vma->vm_end - vma->vm_start); 277103252919SAndi Kleen free_page((unsigned long)buf); 277203252919SAndi Kleen } 277303252919SAndi Kleen } 277403252919SAndi Kleen up_read(¤t->mm->mmap_sem); 277503252919SAndi Kleen } 2776