11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/mm/memory.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds 51da177e4SLinus Torvalds */ 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds /* 81da177e4SLinus Torvalds * demand-loading started 01.12.91 - seems it is high on the list of 91da177e4SLinus Torvalds * things wanted, and it should be easy to implement. - Linus 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds /* 131da177e4SLinus Torvalds * Ok, demand-loading was easy, shared pages a little bit tricker. Shared 141da177e4SLinus Torvalds * pages started 02.12.91, seems to work. - Linus. 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * Tested sharing by executing about 30 /bin/sh: under the old kernel it 171da177e4SLinus Torvalds * would have taken more than the 6M I have free, but it worked well as 181da177e4SLinus Torvalds * far as I could see. 191da177e4SLinus Torvalds * 201da177e4SLinus Torvalds * Also corrected some "invalidate()"s - I wasn't doing enough of them. 211da177e4SLinus Torvalds */ 221da177e4SLinus Torvalds 231da177e4SLinus Torvalds /* 241da177e4SLinus Torvalds * Real VM (paging to/from disk) started 18.12.91. Much more work and 251da177e4SLinus Torvalds * thought has to go into this. Oh, well.. 261da177e4SLinus Torvalds * 19.12.91 - works, somewhat. Sometimes I get faults, don't know why. 271da177e4SLinus Torvalds * Found it. Everything seems to work now. 281da177e4SLinus Torvalds * 20.12.91 - Ok, making the swap-device changeable like the root. 291da177e4SLinus Torvalds */ 301da177e4SLinus Torvalds 311da177e4SLinus Torvalds /* 321da177e4SLinus Torvalds * 05.04.94 - Multi-page memory management added for v1.1. 331da177e4SLinus Torvalds * Idea by Alex Bligh (alex@cconcepts.co.uk) 341da177e4SLinus Torvalds * 351da177e4SLinus Torvalds * 16.07.99 - Support of BIGMEM added by Gerhard Wichert, Siemens AG 361da177e4SLinus Torvalds * (Gerhard.Wichert@pdb.siemens.de) 371da177e4SLinus Torvalds * 381da177e4SLinus Torvalds * Aug/Sep 2004 Changed to four level page tables (Andi Kleen) 391da177e4SLinus Torvalds */ 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds #include <linux/kernel_stat.h> 421da177e4SLinus Torvalds #include <linux/mm.h> 431da177e4SLinus Torvalds #include <linux/hugetlb.h> 441da177e4SLinus Torvalds #include <linux/mman.h> 451da177e4SLinus Torvalds #include <linux/swap.h> 461da177e4SLinus Torvalds #include <linux/highmem.h> 471da177e4SLinus Torvalds #include <linux/pagemap.h> 489a840895SHugh Dickins #include <linux/ksm.h> 491da177e4SLinus Torvalds #include <linux/rmap.h> 501da177e4SLinus Torvalds #include <linux/module.h> 510ff92245SShailabh Nagar #include <linux/delayacct.h> 521da177e4SLinus Torvalds #include <linux/init.h> 53edc79b2aSPeter Zijlstra #include <linux/writeback.h> 548a9f3ccdSBalbir Singh #include <linux/memcontrol.h> 55cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h> 563dc14741SHugh Dickins #include <linux/kallsyms.h> 573dc14741SHugh Dickins #include <linux/swapops.h> 583dc14741SHugh Dickins #include <linux/elf.h> 591da177e4SLinus Torvalds 606952b61dSAlexey Dobriyan #include <asm/io.h> 611da177e4SLinus Torvalds #include <asm/pgalloc.h> 621da177e4SLinus Torvalds #include <asm/uaccess.h> 631da177e4SLinus Torvalds #include <asm/tlb.h> 641da177e4SLinus Torvalds #include <asm/tlbflush.h> 651da177e4SLinus Torvalds #include <asm/pgtable.h> 661da177e4SLinus Torvalds 6742b77728SJan Beulich #include "internal.h" 6842b77728SJan Beulich 69d41dee36SAndy Whitcroft #ifndef CONFIG_NEED_MULTIPLE_NODES 701da177e4SLinus Torvalds /* use the per-pgdat data instead for discontigmem - mbligh */ 711da177e4SLinus Torvalds unsigned long max_mapnr; 721da177e4SLinus Torvalds struct page *mem_map; 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds EXPORT_SYMBOL(max_mapnr); 751da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map); 761da177e4SLinus Torvalds #endif 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds unsigned long num_physpages; 791da177e4SLinus Torvalds /* 801da177e4SLinus Torvalds * A number of key systems in x86 including ioremap() rely on the assumption 811da177e4SLinus Torvalds * that high_memory defines the upper bound on direct map memory, then end 821da177e4SLinus Torvalds * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and 831da177e4SLinus Torvalds * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL 841da177e4SLinus Torvalds * and ZONE_HIGHMEM. 851da177e4SLinus Torvalds */ 861da177e4SLinus Torvalds void * high_memory; 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds EXPORT_SYMBOL(num_physpages); 891da177e4SLinus Torvalds EXPORT_SYMBOL(high_memory); 901da177e4SLinus Torvalds 9132a93233SIngo Molnar /* 9232a93233SIngo Molnar * Randomize the address space (stacks, mmaps, brk, etc.). 9332a93233SIngo Molnar * 9432a93233SIngo Molnar * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization, 9532a93233SIngo Molnar * as ancient (libc5 based) binaries can segfault. ) 9632a93233SIngo Molnar */ 9732a93233SIngo Molnar int randomize_va_space __read_mostly = 9832a93233SIngo Molnar #ifdef CONFIG_COMPAT_BRK 9932a93233SIngo Molnar 1; 10032a93233SIngo Molnar #else 10132a93233SIngo Molnar 2; 10232a93233SIngo Molnar #endif 103a62eaf15SAndi Kleen 104a62eaf15SAndi Kleen static int __init disable_randmaps(char *s) 105a62eaf15SAndi Kleen { 106a62eaf15SAndi Kleen randomize_va_space = 0; 1079b41046cSOGAWA Hirofumi return 1; 108a62eaf15SAndi Kleen } 109a62eaf15SAndi Kleen __setup("norandmaps", disable_randmaps); 110a62eaf15SAndi Kleen 111*62eede62SHugh Dickins unsigned long zero_pfn __read_mostly; 112a13ea5b7SHugh Dickins 113a13ea5b7SHugh Dickins /* 114a13ea5b7SHugh Dickins * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init() 115a13ea5b7SHugh Dickins */ 116a13ea5b7SHugh Dickins static int __init init_zero_pfn(void) 117a13ea5b7SHugh Dickins { 118a13ea5b7SHugh Dickins zero_pfn = page_to_pfn(ZERO_PAGE(0)); 119a13ea5b7SHugh Dickins return 0; 120a13ea5b7SHugh Dickins } 121a13ea5b7SHugh Dickins core_initcall(init_zero_pfn); 122a62eaf15SAndi Kleen 1231da177e4SLinus Torvalds /* 1241da177e4SLinus Torvalds * If a p?d_bad entry is found while walking page tables, report 1251da177e4SLinus Torvalds * the error, before resetting entry to p?d_none. Usually (but 1261da177e4SLinus Torvalds * very seldom) called out from the p?d_none_or_clear_bad macros. 1271da177e4SLinus Torvalds */ 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds void pgd_clear_bad(pgd_t *pgd) 1301da177e4SLinus Torvalds { 1311da177e4SLinus Torvalds pgd_ERROR(*pgd); 1321da177e4SLinus Torvalds pgd_clear(pgd); 1331da177e4SLinus Torvalds } 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds void pud_clear_bad(pud_t *pud) 1361da177e4SLinus Torvalds { 1371da177e4SLinus Torvalds pud_ERROR(*pud); 1381da177e4SLinus Torvalds pud_clear(pud); 1391da177e4SLinus Torvalds } 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds void pmd_clear_bad(pmd_t *pmd) 1421da177e4SLinus Torvalds { 1431da177e4SLinus Torvalds pmd_ERROR(*pmd); 1441da177e4SLinus Torvalds pmd_clear(pmd); 1451da177e4SLinus Torvalds } 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds /* 1481da177e4SLinus Torvalds * Note: this doesn't free the actual pages themselves. That 1491da177e4SLinus Torvalds * has been handled earlier when unmapping all the memory regions. 1501da177e4SLinus Torvalds */ 1519e1b32caSBenjamin Herrenschmidt static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd, 1529e1b32caSBenjamin Herrenschmidt unsigned long addr) 1531da177e4SLinus Torvalds { 1542f569afdSMartin Schwidefsky pgtable_t token = pmd_pgtable(*pmd); 1551da177e4SLinus Torvalds pmd_clear(pmd); 1569e1b32caSBenjamin Herrenschmidt pte_free_tlb(tlb, token, addr); 1571da177e4SLinus Torvalds tlb->mm->nr_ptes--; 1581da177e4SLinus Torvalds } 1591da177e4SLinus Torvalds 160e0da382cSHugh Dickins static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud, 161e0da382cSHugh Dickins unsigned long addr, unsigned long end, 162e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 1631da177e4SLinus Torvalds { 1641da177e4SLinus Torvalds pmd_t *pmd; 1651da177e4SLinus Torvalds unsigned long next; 166e0da382cSHugh Dickins unsigned long start; 1671da177e4SLinus Torvalds 168e0da382cSHugh Dickins start = addr; 1691da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 1701da177e4SLinus Torvalds do { 1711da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 1721da177e4SLinus Torvalds if (pmd_none_or_clear_bad(pmd)) 1731da177e4SLinus Torvalds continue; 1749e1b32caSBenjamin Herrenschmidt free_pte_range(tlb, pmd, addr); 1751da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 1761da177e4SLinus Torvalds 177e0da382cSHugh Dickins start &= PUD_MASK; 178e0da382cSHugh Dickins if (start < floor) 179e0da382cSHugh Dickins return; 180e0da382cSHugh Dickins if (ceiling) { 181e0da382cSHugh Dickins ceiling &= PUD_MASK; 182e0da382cSHugh Dickins if (!ceiling) 183e0da382cSHugh Dickins return; 1841da177e4SLinus Torvalds } 185e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 186e0da382cSHugh Dickins return; 187e0da382cSHugh Dickins 188e0da382cSHugh Dickins pmd = pmd_offset(pud, start); 189e0da382cSHugh Dickins pud_clear(pud); 1909e1b32caSBenjamin Herrenschmidt pmd_free_tlb(tlb, pmd, start); 1911da177e4SLinus Torvalds } 1921da177e4SLinus Torvalds 193e0da382cSHugh Dickins static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd, 194e0da382cSHugh Dickins unsigned long addr, unsigned long end, 195e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 1961da177e4SLinus Torvalds { 1971da177e4SLinus Torvalds pud_t *pud; 1981da177e4SLinus Torvalds unsigned long next; 199e0da382cSHugh Dickins unsigned long start; 2001da177e4SLinus Torvalds 201e0da382cSHugh Dickins start = addr; 2021da177e4SLinus Torvalds pud = pud_offset(pgd, addr); 2031da177e4SLinus Torvalds do { 2041da177e4SLinus Torvalds next = pud_addr_end(addr, end); 2051da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 2061da177e4SLinus Torvalds continue; 207e0da382cSHugh Dickins free_pmd_range(tlb, pud, addr, next, floor, ceiling); 2081da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 2091da177e4SLinus Torvalds 210e0da382cSHugh Dickins start &= PGDIR_MASK; 211e0da382cSHugh Dickins if (start < floor) 212e0da382cSHugh Dickins return; 213e0da382cSHugh Dickins if (ceiling) { 214e0da382cSHugh Dickins ceiling &= PGDIR_MASK; 215e0da382cSHugh Dickins if (!ceiling) 216e0da382cSHugh Dickins return; 2171da177e4SLinus Torvalds } 218e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 219e0da382cSHugh Dickins return; 220e0da382cSHugh Dickins 221e0da382cSHugh Dickins pud = pud_offset(pgd, start); 222e0da382cSHugh Dickins pgd_clear(pgd); 2239e1b32caSBenjamin Herrenschmidt pud_free_tlb(tlb, pud, start); 2241da177e4SLinus Torvalds } 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds /* 227e0da382cSHugh Dickins * This function frees user-level page tables of a process. 228e0da382cSHugh Dickins * 2291da177e4SLinus Torvalds * Must be called with pagetable lock held. 2301da177e4SLinus Torvalds */ 23142b77728SJan Beulich void free_pgd_range(struct mmu_gather *tlb, 232e0da382cSHugh Dickins unsigned long addr, unsigned long end, 233e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 2341da177e4SLinus Torvalds { 2351da177e4SLinus Torvalds pgd_t *pgd; 2361da177e4SLinus Torvalds unsigned long next; 237e0da382cSHugh Dickins unsigned long start; 2381da177e4SLinus Torvalds 239e0da382cSHugh Dickins /* 240e0da382cSHugh Dickins * The next few lines have given us lots of grief... 241e0da382cSHugh Dickins * 242e0da382cSHugh Dickins * Why are we testing PMD* at this top level? Because often 243e0da382cSHugh Dickins * there will be no work to do at all, and we'd prefer not to 244e0da382cSHugh Dickins * go all the way down to the bottom just to discover that. 245e0da382cSHugh Dickins * 246e0da382cSHugh Dickins * Why all these "- 1"s? Because 0 represents both the bottom 247e0da382cSHugh Dickins * of the address space and the top of it (using -1 for the 248e0da382cSHugh Dickins * top wouldn't help much: the masks would do the wrong thing). 249e0da382cSHugh Dickins * The rule is that addr 0 and floor 0 refer to the bottom of 250e0da382cSHugh Dickins * the address space, but end 0 and ceiling 0 refer to the top 251e0da382cSHugh Dickins * Comparisons need to use "end - 1" and "ceiling - 1" (though 252e0da382cSHugh Dickins * that end 0 case should be mythical). 253e0da382cSHugh Dickins * 254e0da382cSHugh Dickins * Wherever addr is brought up or ceiling brought down, we must 255e0da382cSHugh Dickins * be careful to reject "the opposite 0" before it confuses the 256e0da382cSHugh Dickins * subsequent tests. But what about where end is brought down 257e0da382cSHugh Dickins * by PMD_SIZE below? no, end can't go down to 0 there. 258e0da382cSHugh Dickins * 259e0da382cSHugh Dickins * Whereas we round start (addr) and ceiling down, by different 260e0da382cSHugh Dickins * masks at different levels, in order to test whether a table 261e0da382cSHugh Dickins * now has no other vmas using it, so can be freed, we don't 262e0da382cSHugh Dickins * bother to round floor or end up - the tests don't need that. 263e0da382cSHugh Dickins */ 264e0da382cSHugh Dickins 265e0da382cSHugh Dickins addr &= PMD_MASK; 266e0da382cSHugh Dickins if (addr < floor) { 267e0da382cSHugh Dickins addr += PMD_SIZE; 268e0da382cSHugh Dickins if (!addr) 269e0da382cSHugh Dickins return; 270e0da382cSHugh Dickins } 271e0da382cSHugh Dickins if (ceiling) { 272e0da382cSHugh Dickins ceiling &= PMD_MASK; 273e0da382cSHugh Dickins if (!ceiling) 274e0da382cSHugh Dickins return; 275e0da382cSHugh Dickins } 276e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 277e0da382cSHugh Dickins end -= PMD_SIZE; 278e0da382cSHugh Dickins if (addr > end - 1) 279e0da382cSHugh Dickins return; 280e0da382cSHugh Dickins 281e0da382cSHugh Dickins start = addr; 28242b77728SJan Beulich pgd = pgd_offset(tlb->mm, addr); 2831da177e4SLinus Torvalds do { 2841da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 2851da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 2861da177e4SLinus Torvalds continue; 28742b77728SJan Beulich free_pud_range(tlb, pgd, addr, next, floor, ceiling); 2881da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 289e0da382cSHugh Dickins } 290e0da382cSHugh Dickins 29142b77728SJan Beulich void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma, 292e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 293e0da382cSHugh Dickins { 294e0da382cSHugh Dickins while (vma) { 295e0da382cSHugh Dickins struct vm_area_struct *next = vma->vm_next; 296e0da382cSHugh Dickins unsigned long addr = vma->vm_start; 297e0da382cSHugh Dickins 2988f4f8c16SHugh Dickins /* 2998f4f8c16SHugh Dickins * Hide vma from rmap and vmtruncate before freeing pgtables 3008f4f8c16SHugh Dickins */ 3018f4f8c16SHugh Dickins anon_vma_unlink(vma); 3028f4f8c16SHugh Dickins unlink_file_vma(vma); 3038f4f8c16SHugh Dickins 3049da61aefSDavid Gibson if (is_vm_hugetlb_page(vma)) { 3053bf5ee95SHugh Dickins hugetlb_free_pgd_range(tlb, addr, vma->vm_end, 3063bf5ee95SHugh Dickins floor, next? next->vm_start: ceiling); 3073bf5ee95SHugh Dickins } else { 3083bf5ee95SHugh Dickins /* 3093bf5ee95SHugh Dickins * Optimization: gather nearby vmas into one call down 3103bf5ee95SHugh Dickins */ 3113bf5ee95SHugh Dickins while (next && next->vm_start <= vma->vm_end + PMD_SIZE 3124866920bSDavid Gibson && !is_vm_hugetlb_page(next)) { 313e0da382cSHugh Dickins vma = next; 314e0da382cSHugh Dickins next = vma->vm_next; 3158f4f8c16SHugh Dickins anon_vma_unlink(vma); 3168f4f8c16SHugh Dickins unlink_file_vma(vma); 317e0da382cSHugh Dickins } 3183bf5ee95SHugh Dickins free_pgd_range(tlb, addr, vma->vm_end, 319e0da382cSHugh Dickins floor, next? next->vm_start: ceiling); 3203bf5ee95SHugh Dickins } 321e0da382cSHugh Dickins vma = next; 322e0da382cSHugh Dickins } 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds 3251bb3630eSHugh Dickins int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address) 3261da177e4SLinus Torvalds { 3272f569afdSMartin Schwidefsky pgtable_t new = pte_alloc_one(mm, address); 3281da177e4SLinus Torvalds if (!new) 3291bb3630eSHugh Dickins return -ENOMEM; 3301bb3630eSHugh Dickins 331362a61adSNick Piggin /* 332362a61adSNick Piggin * Ensure all pte setup (eg. pte page lock and page clearing) are 333362a61adSNick Piggin * visible before the pte is made visible to other CPUs by being 334362a61adSNick Piggin * put into page tables. 335362a61adSNick Piggin * 336362a61adSNick Piggin * The other side of the story is the pointer chasing in the page 337362a61adSNick Piggin * table walking code (when walking the page table without locking; 338362a61adSNick Piggin * ie. most of the time). Fortunately, these data accesses consist 339362a61adSNick Piggin * of a chain of data-dependent loads, meaning most CPUs (alpha 340362a61adSNick Piggin * being the notable exception) will already guarantee loads are 341362a61adSNick Piggin * seen in-order. See the alpha page table accessors for the 342362a61adSNick Piggin * smp_read_barrier_depends() barriers in page table walking code. 343362a61adSNick Piggin */ 344362a61adSNick Piggin smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */ 345362a61adSNick Piggin 346c74df32cSHugh Dickins spin_lock(&mm->page_table_lock); 3472f569afdSMartin Schwidefsky if (!pmd_present(*pmd)) { /* Has another populated it ? */ 3481da177e4SLinus Torvalds mm->nr_ptes++; 3491da177e4SLinus Torvalds pmd_populate(mm, pmd, new); 3502f569afdSMartin Schwidefsky new = NULL; 3511da177e4SLinus Torvalds } 352c74df32cSHugh Dickins spin_unlock(&mm->page_table_lock); 3532f569afdSMartin Schwidefsky if (new) 3542f569afdSMartin Schwidefsky pte_free(mm, new); 3551bb3630eSHugh Dickins return 0; 3561da177e4SLinus Torvalds } 3571da177e4SLinus Torvalds 3581bb3630eSHugh Dickins int __pte_alloc_kernel(pmd_t *pmd, unsigned long address) 3591da177e4SLinus Torvalds { 3601bb3630eSHugh Dickins pte_t *new = pte_alloc_one_kernel(&init_mm, address); 3611da177e4SLinus Torvalds if (!new) 3621bb3630eSHugh Dickins return -ENOMEM; 3631da177e4SLinus Torvalds 364362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 365362a61adSNick Piggin 366872fec16SHugh Dickins spin_lock(&init_mm.page_table_lock); 3672f569afdSMartin Schwidefsky if (!pmd_present(*pmd)) { /* Has another populated it ? */ 368872fec16SHugh Dickins pmd_populate_kernel(&init_mm, pmd, new); 3692f569afdSMartin Schwidefsky new = NULL; 3702f569afdSMartin Schwidefsky } 371872fec16SHugh Dickins spin_unlock(&init_mm.page_table_lock); 3722f569afdSMartin Schwidefsky if (new) 3732f569afdSMartin Schwidefsky pte_free_kernel(&init_mm, new); 3741bb3630eSHugh Dickins return 0; 3751da177e4SLinus Torvalds } 3761da177e4SLinus Torvalds 377ae859762SHugh Dickins static inline void add_mm_rss(struct mm_struct *mm, int file_rss, int anon_rss) 378ae859762SHugh Dickins { 379ae859762SHugh Dickins if (file_rss) 380ae859762SHugh Dickins add_mm_counter(mm, file_rss, file_rss); 381ae859762SHugh Dickins if (anon_rss) 382ae859762SHugh Dickins add_mm_counter(mm, anon_rss, anon_rss); 383ae859762SHugh Dickins } 384ae859762SHugh Dickins 3851da177e4SLinus Torvalds /* 3866aab341eSLinus Torvalds * This function is called to print an error when a bad pte 3876aab341eSLinus Torvalds * is found. For example, we might have a PFN-mapped pte in 3886aab341eSLinus Torvalds * a region that doesn't allow it. 389b5810039SNick Piggin * 390b5810039SNick Piggin * The calling function must still handle the error. 391b5810039SNick Piggin */ 3923dc14741SHugh Dickins static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr, 3933dc14741SHugh Dickins pte_t pte, struct page *page) 394b5810039SNick Piggin { 3953dc14741SHugh Dickins pgd_t *pgd = pgd_offset(vma->vm_mm, addr); 3963dc14741SHugh Dickins pud_t *pud = pud_offset(pgd, addr); 3973dc14741SHugh Dickins pmd_t *pmd = pmd_offset(pud, addr); 3983dc14741SHugh Dickins struct address_space *mapping; 3993dc14741SHugh Dickins pgoff_t index; 400d936cf9bSHugh Dickins static unsigned long resume; 401d936cf9bSHugh Dickins static unsigned long nr_shown; 402d936cf9bSHugh Dickins static unsigned long nr_unshown; 403d936cf9bSHugh Dickins 404d936cf9bSHugh Dickins /* 405d936cf9bSHugh Dickins * Allow a burst of 60 reports, then keep quiet for that minute; 406d936cf9bSHugh Dickins * or allow a steady drip of one report per second. 407d936cf9bSHugh Dickins */ 408d936cf9bSHugh Dickins if (nr_shown == 60) { 409d936cf9bSHugh Dickins if (time_before(jiffies, resume)) { 410d936cf9bSHugh Dickins nr_unshown++; 411d936cf9bSHugh Dickins return; 412d936cf9bSHugh Dickins } 413d936cf9bSHugh Dickins if (nr_unshown) { 4141e9e6365SHugh Dickins printk(KERN_ALERT 4151e9e6365SHugh Dickins "BUG: Bad page map: %lu messages suppressed\n", 416d936cf9bSHugh Dickins nr_unshown); 417d936cf9bSHugh Dickins nr_unshown = 0; 418d936cf9bSHugh Dickins } 419d936cf9bSHugh Dickins nr_shown = 0; 420d936cf9bSHugh Dickins } 421d936cf9bSHugh Dickins if (nr_shown++ == 0) 422d936cf9bSHugh Dickins resume = jiffies + 60 * HZ; 4233dc14741SHugh Dickins 4243dc14741SHugh Dickins mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL; 4253dc14741SHugh Dickins index = linear_page_index(vma, addr); 4263dc14741SHugh Dickins 4271e9e6365SHugh Dickins printk(KERN_ALERT 4281e9e6365SHugh Dickins "BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n", 4293dc14741SHugh Dickins current->comm, 4303dc14741SHugh Dickins (long long)pte_val(pte), (long long)pmd_val(*pmd)); 4313dc14741SHugh Dickins if (page) { 4321e9e6365SHugh Dickins printk(KERN_ALERT 4333dc14741SHugh Dickins "page:%p flags:%p count:%d mapcount:%d mapping:%p index:%lx\n", 4343dc14741SHugh Dickins page, (void *)page->flags, page_count(page), 4353dc14741SHugh Dickins page_mapcount(page), page->mapping, page->index); 4363dc14741SHugh Dickins } 4371e9e6365SHugh Dickins printk(KERN_ALERT 4383dc14741SHugh Dickins "addr:%p vm_flags:%08lx anon_vma:%p mapping:%p index:%lx\n", 4393dc14741SHugh Dickins (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index); 4403dc14741SHugh Dickins /* 4413dc14741SHugh Dickins * Choose text because data symbols depend on CONFIG_KALLSYMS_ALL=y 4423dc14741SHugh Dickins */ 4433dc14741SHugh Dickins if (vma->vm_ops) 4441e9e6365SHugh Dickins print_symbol(KERN_ALERT "vma->vm_ops->fault: %s\n", 4453dc14741SHugh Dickins (unsigned long)vma->vm_ops->fault); 4463dc14741SHugh Dickins if (vma->vm_file && vma->vm_file->f_op) 4471e9e6365SHugh Dickins print_symbol(KERN_ALERT "vma->vm_file->f_op->mmap: %s\n", 4483dc14741SHugh Dickins (unsigned long)vma->vm_file->f_op->mmap); 449b5810039SNick Piggin dump_stack(); 4503dc14741SHugh Dickins add_taint(TAINT_BAD_PAGE); 451b5810039SNick Piggin } 452b5810039SNick Piggin 45367121172SLinus Torvalds static inline int is_cow_mapping(unsigned int flags) 45467121172SLinus Torvalds { 45567121172SLinus Torvalds return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; 45667121172SLinus Torvalds } 45767121172SLinus Torvalds 458*62eede62SHugh Dickins #ifndef is_zero_pfn 459*62eede62SHugh Dickins static inline int is_zero_pfn(unsigned long pfn) 460*62eede62SHugh Dickins { 461*62eede62SHugh Dickins return pfn == zero_pfn; 462*62eede62SHugh Dickins } 463*62eede62SHugh Dickins #endif 464*62eede62SHugh Dickins 465*62eede62SHugh Dickins #ifndef my_zero_pfn 466*62eede62SHugh Dickins static inline unsigned long my_zero_pfn(unsigned long addr) 467*62eede62SHugh Dickins { 468*62eede62SHugh Dickins return zero_pfn; 469*62eede62SHugh Dickins } 470*62eede62SHugh Dickins #endif 471*62eede62SHugh Dickins 472b5810039SNick Piggin /* 4737e675137SNick Piggin * vm_normal_page -- This function gets the "struct page" associated with a pte. 4746aab341eSLinus Torvalds * 4757e675137SNick Piggin * "Special" mappings do not wish to be associated with a "struct page" (either 4767e675137SNick Piggin * it doesn't exist, or it exists but they don't want to touch it). In this 4777e675137SNick Piggin * case, NULL is returned here. "Normal" mappings do have a struct page. 478b379d790SJared Hulbert * 4797e675137SNick Piggin * There are 2 broad cases. Firstly, an architecture may define a pte_special() 4807e675137SNick Piggin * pte bit, in which case this function is trivial. Secondly, an architecture 4817e675137SNick Piggin * may not have a spare pte bit, which requires a more complicated scheme, 4827e675137SNick Piggin * described below. 4837e675137SNick Piggin * 4847e675137SNick Piggin * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a 4857e675137SNick Piggin * special mapping (even if there are underlying and valid "struct pages"). 4867e675137SNick Piggin * COWed pages of a VM_PFNMAP are always normal. 4876aab341eSLinus Torvalds * 488b379d790SJared Hulbert * The way we recognize COWed pages within VM_PFNMAP mappings is through the 489b379d790SJared Hulbert * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit 4907e675137SNick Piggin * set, and the vm_pgoff will point to the first PFN mapped: thus every special 4917e675137SNick Piggin * mapping will always honor the rule 4926aab341eSLinus Torvalds * 4936aab341eSLinus Torvalds * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT) 4946aab341eSLinus Torvalds * 4957e675137SNick Piggin * And for normal mappings this is false. 496b379d790SJared Hulbert * 4977e675137SNick Piggin * This restricts such mappings to be a linear translation from virtual address 4987e675137SNick Piggin * to pfn. To get around this restriction, we allow arbitrary mappings so long 4997e675137SNick Piggin * as the vma is not a COW mapping; in that case, we know that all ptes are 5007e675137SNick Piggin * special (because none can have been COWed). 501b379d790SJared Hulbert * 502b379d790SJared Hulbert * 5037e675137SNick Piggin * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP. 5047e675137SNick Piggin * 505b379d790SJared Hulbert * VM_MIXEDMAP mappings can likewise contain memory with or without "struct 506b379d790SJared Hulbert * page" backing, however the difference is that _all_ pages with a struct 507b379d790SJared Hulbert * page (that is, those where pfn_valid is true) are refcounted and considered 508b379d790SJared Hulbert * normal pages by the VM. The disadvantage is that pages are refcounted 509b379d790SJared Hulbert * (which can be slower and simply not an option for some PFNMAP users). The 510b379d790SJared Hulbert * advantage is that we don't have to follow the strict linearity rule of 511b379d790SJared Hulbert * PFNMAP mappings in order to support COWable mappings. 512b379d790SJared Hulbert * 513ee498ed7SHugh Dickins */ 5147e675137SNick Piggin #ifdef __HAVE_ARCH_PTE_SPECIAL 5157e675137SNick Piggin # define HAVE_PTE_SPECIAL 1 5167e675137SNick Piggin #else 5177e675137SNick Piggin # define HAVE_PTE_SPECIAL 0 5187e675137SNick Piggin #endif 5197e675137SNick Piggin struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, 5207e675137SNick Piggin pte_t pte) 521ee498ed7SHugh Dickins { 52222b31eecSHugh Dickins unsigned long pfn = pte_pfn(pte); 5237e675137SNick Piggin 5247e675137SNick Piggin if (HAVE_PTE_SPECIAL) { 52522b31eecSHugh Dickins if (likely(!pte_special(pte))) 52622b31eecSHugh Dickins goto check_pfn; 527a13ea5b7SHugh Dickins if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)) 528a13ea5b7SHugh Dickins return NULL; 529*62eede62SHugh Dickins if (!is_zero_pfn(pfn)) 53022b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 5317e675137SNick Piggin return NULL; 5327e675137SNick Piggin } 5337e675137SNick Piggin 5347e675137SNick Piggin /* !HAVE_PTE_SPECIAL case follows: */ 5357e675137SNick Piggin 536b379d790SJared Hulbert if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { 537b379d790SJared Hulbert if (vma->vm_flags & VM_MIXEDMAP) { 538b379d790SJared Hulbert if (!pfn_valid(pfn)) 539b379d790SJared Hulbert return NULL; 540b379d790SJared Hulbert goto out; 541b379d790SJared Hulbert } else { 5427e675137SNick Piggin unsigned long off; 5437e675137SNick Piggin off = (addr - vma->vm_start) >> PAGE_SHIFT; 5446aab341eSLinus Torvalds if (pfn == vma->vm_pgoff + off) 5456aab341eSLinus Torvalds return NULL; 54667121172SLinus Torvalds if (!is_cow_mapping(vma->vm_flags)) 547fb155c16SLinus Torvalds return NULL; 5486aab341eSLinus Torvalds } 549b379d790SJared Hulbert } 5506aab341eSLinus Torvalds 551*62eede62SHugh Dickins if (is_zero_pfn(pfn)) 552*62eede62SHugh Dickins return NULL; 55322b31eecSHugh Dickins check_pfn: 55422b31eecSHugh Dickins if (unlikely(pfn > highest_memmap_pfn)) { 55522b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 55622b31eecSHugh Dickins return NULL; 55722b31eecSHugh Dickins } 5586aab341eSLinus Torvalds 5596aab341eSLinus Torvalds /* 5607e675137SNick Piggin * NOTE! We still have PageReserved() pages in the page tables. 5617e675137SNick Piggin * eg. VDSO mappings can cause them to exist. 5626aab341eSLinus Torvalds */ 563b379d790SJared Hulbert out: 5646aab341eSLinus Torvalds return pfn_to_page(pfn); 565ee498ed7SHugh Dickins } 566ee498ed7SHugh Dickins 567ee498ed7SHugh Dickins /* 5681da177e4SLinus Torvalds * copy one vm_area from one task to the other. Assumes the page tables 5691da177e4SLinus Torvalds * already present in the new task to be cleared in the whole range 5701da177e4SLinus Torvalds * covered by this vma. 5711da177e4SLinus Torvalds */ 5721da177e4SLinus Torvalds 5738c103762SHugh Dickins static inline void 5741da177e4SLinus Torvalds copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, 575b5810039SNick Piggin pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma, 5768c103762SHugh Dickins unsigned long addr, int *rss) 5771da177e4SLinus Torvalds { 578b5810039SNick Piggin unsigned long vm_flags = vma->vm_flags; 5791da177e4SLinus Torvalds pte_t pte = *src_pte; 5801da177e4SLinus Torvalds struct page *page; 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds /* pte contains position in swap or file, so copy. */ 5831da177e4SLinus Torvalds if (unlikely(!pte_present(pte))) { 5841da177e4SLinus Torvalds if (!pte_file(pte)) { 5850697212aSChristoph Lameter swp_entry_t entry = pte_to_swp_entry(pte); 5860697212aSChristoph Lameter 5870697212aSChristoph Lameter swap_duplicate(entry); 5881da177e4SLinus Torvalds /* make sure dst_mm is on swapoff's mmlist. */ 5891da177e4SLinus Torvalds if (unlikely(list_empty(&dst_mm->mmlist))) { 5901da177e4SLinus Torvalds spin_lock(&mmlist_lock); 591f412ac08SHugh Dickins if (list_empty(&dst_mm->mmlist)) 592f412ac08SHugh Dickins list_add(&dst_mm->mmlist, 593f412ac08SHugh Dickins &src_mm->mmlist); 5941da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 5951da177e4SLinus Torvalds } 5960697212aSChristoph Lameter if (is_write_migration_entry(entry) && 5970697212aSChristoph Lameter is_cow_mapping(vm_flags)) { 5980697212aSChristoph Lameter /* 5990697212aSChristoph Lameter * COW mappings require pages in both parent 6000697212aSChristoph Lameter * and child to be set to read. 6010697212aSChristoph Lameter */ 6020697212aSChristoph Lameter make_migration_entry_read(&entry); 6030697212aSChristoph Lameter pte = swp_entry_to_pte(entry); 6040697212aSChristoph Lameter set_pte_at(src_mm, addr, src_pte, pte); 6050697212aSChristoph Lameter } 6061da177e4SLinus Torvalds } 607ae859762SHugh Dickins goto out_set_pte; 6081da177e4SLinus Torvalds } 6091da177e4SLinus Torvalds 6101da177e4SLinus Torvalds /* 6111da177e4SLinus Torvalds * If it's a COW mapping, write protect it both 6121da177e4SLinus Torvalds * in the parent and the child 6131da177e4SLinus Torvalds */ 61467121172SLinus Torvalds if (is_cow_mapping(vm_flags)) { 6151da177e4SLinus Torvalds ptep_set_wrprotect(src_mm, addr, src_pte); 6163dc90795SZachary Amsden pte = pte_wrprotect(pte); 6171da177e4SLinus Torvalds } 6181da177e4SLinus Torvalds 6191da177e4SLinus Torvalds /* 6201da177e4SLinus Torvalds * If it's a shared mapping, mark it clean in 6211da177e4SLinus Torvalds * the child 6221da177e4SLinus Torvalds */ 6231da177e4SLinus Torvalds if (vm_flags & VM_SHARED) 6241da177e4SLinus Torvalds pte = pte_mkclean(pte); 6251da177e4SLinus Torvalds pte = pte_mkold(pte); 6266aab341eSLinus Torvalds 6276aab341eSLinus Torvalds page = vm_normal_page(vma, addr, pte); 6286aab341eSLinus Torvalds if (page) { 6291da177e4SLinus Torvalds get_page(page); 63021333b2bSHugh Dickins page_dup_rmap(page); 631b7c46d15SJohannes Weiner rss[PageAnon(page)]++; 6326aab341eSLinus Torvalds } 633ae859762SHugh Dickins 634ae859762SHugh Dickins out_set_pte: 635ae859762SHugh Dickins set_pte_at(dst_mm, addr, dst_pte, pte); 6361da177e4SLinus Torvalds } 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 6391da177e4SLinus Torvalds pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma, 6401da177e4SLinus Torvalds unsigned long addr, unsigned long end) 6411da177e4SLinus Torvalds { 6421da177e4SLinus Torvalds pte_t *src_pte, *dst_pte; 643c74df32cSHugh Dickins spinlock_t *src_ptl, *dst_ptl; 644e040f218SHugh Dickins int progress = 0; 6458c103762SHugh Dickins int rss[2]; 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds again: 648ae859762SHugh Dickins rss[1] = rss[0] = 0; 649c74df32cSHugh Dickins dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl); 6501da177e4SLinus Torvalds if (!dst_pte) 6511da177e4SLinus Torvalds return -ENOMEM; 6521da177e4SLinus Torvalds src_pte = pte_offset_map_nested(src_pmd, addr); 6534c21e2f2SHugh Dickins src_ptl = pte_lockptr(src_mm, src_pmd); 654f20dc5f7SIngo Molnar spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 6556606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 6561da177e4SLinus Torvalds 6571da177e4SLinus Torvalds do { 6581da177e4SLinus Torvalds /* 6591da177e4SLinus Torvalds * We are holding two locks at this point - either of them 6601da177e4SLinus Torvalds * could generate latencies in another task on another CPU. 6611da177e4SLinus Torvalds */ 662e040f218SHugh Dickins if (progress >= 32) { 663e040f218SHugh Dickins progress = 0; 664e040f218SHugh Dickins if (need_resched() || 66595c354feSNick Piggin spin_needbreak(src_ptl) || spin_needbreak(dst_ptl)) 6661da177e4SLinus Torvalds break; 667e040f218SHugh Dickins } 6681da177e4SLinus Torvalds if (pte_none(*src_pte)) { 6691da177e4SLinus Torvalds progress++; 6701da177e4SLinus Torvalds continue; 6711da177e4SLinus Torvalds } 6728c103762SHugh Dickins copy_one_pte(dst_mm, src_mm, dst_pte, src_pte, vma, addr, rss); 6731da177e4SLinus Torvalds progress += 8; 6741da177e4SLinus Torvalds } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end); 6751da177e4SLinus Torvalds 6766606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 677c74df32cSHugh Dickins spin_unlock(src_ptl); 6781da177e4SLinus Torvalds pte_unmap_nested(src_pte - 1); 679ae859762SHugh Dickins add_mm_rss(dst_mm, rss[0], rss[1]); 680c74df32cSHugh Dickins pte_unmap_unlock(dst_pte - 1, dst_ptl); 681c74df32cSHugh Dickins cond_resched(); 6821da177e4SLinus Torvalds if (addr != end) 6831da177e4SLinus Torvalds goto again; 6841da177e4SLinus Torvalds return 0; 6851da177e4SLinus Torvalds } 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 6881da177e4SLinus Torvalds pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma, 6891da177e4SLinus Torvalds unsigned long addr, unsigned long end) 6901da177e4SLinus Torvalds { 6911da177e4SLinus Torvalds pmd_t *src_pmd, *dst_pmd; 6921da177e4SLinus Torvalds unsigned long next; 6931da177e4SLinus Torvalds 6941da177e4SLinus Torvalds dst_pmd = pmd_alloc(dst_mm, dst_pud, addr); 6951da177e4SLinus Torvalds if (!dst_pmd) 6961da177e4SLinus Torvalds return -ENOMEM; 6971da177e4SLinus Torvalds src_pmd = pmd_offset(src_pud, addr); 6981da177e4SLinus Torvalds do { 6991da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 7001da177e4SLinus Torvalds if (pmd_none_or_clear_bad(src_pmd)) 7011da177e4SLinus Torvalds continue; 7021da177e4SLinus Torvalds if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd, 7031da177e4SLinus Torvalds vma, addr, next)) 7041da177e4SLinus Torvalds return -ENOMEM; 7051da177e4SLinus Torvalds } while (dst_pmd++, src_pmd++, addr = next, addr != end); 7061da177e4SLinus Torvalds return 0; 7071da177e4SLinus Torvalds } 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 7101da177e4SLinus Torvalds pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma, 7111da177e4SLinus Torvalds unsigned long addr, unsigned long end) 7121da177e4SLinus Torvalds { 7131da177e4SLinus Torvalds pud_t *src_pud, *dst_pud; 7141da177e4SLinus Torvalds unsigned long next; 7151da177e4SLinus Torvalds 7161da177e4SLinus Torvalds dst_pud = pud_alloc(dst_mm, dst_pgd, addr); 7171da177e4SLinus Torvalds if (!dst_pud) 7181da177e4SLinus Torvalds return -ENOMEM; 7191da177e4SLinus Torvalds src_pud = pud_offset(src_pgd, addr); 7201da177e4SLinus Torvalds do { 7211da177e4SLinus Torvalds next = pud_addr_end(addr, end); 7221da177e4SLinus Torvalds if (pud_none_or_clear_bad(src_pud)) 7231da177e4SLinus Torvalds continue; 7241da177e4SLinus Torvalds if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud, 7251da177e4SLinus Torvalds vma, addr, next)) 7261da177e4SLinus Torvalds return -ENOMEM; 7271da177e4SLinus Torvalds } while (dst_pud++, src_pud++, addr = next, addr != end); 7281da177e4SLinus Torvalds return 0; 7291da177e4SLinus Torvalds } 7301da177e4SLinus Torvalds 7311da177e4SLinus Torvalds int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 7321da177e4SLinus Torvalds struct vm_area_struct *vma) 7331da177e4SLinus Torvalds { 7341da177e4SLinus Torvalds pgd_t *src_pgd, *dst_pgd; 7351da177e4SLinus Torvalds unsigned long next; 7361da177e4SLinus Torvalds unsigned long addr = vma->vm_start; 7371da177e4SLinus Torvalds unsigned long end = vma->vm_end; 738cddb8a5cSAndrea Arcangeli int ret; 7391da177e4SLinus Torvalds 740d992895bSNick Piggin /* 741d992895bSNick Piggin * Don't copy ptes where a page fault will fill them correctly. 742d992895bSNick Piggin * Fork becomes much lighter when there are big shared or private 743d992895bSNick Piggin * readonly mappings. The tradeoff is that copy_page_range is more 744d992895bSNick Piggin * efficient than faulting. 745d992895bSNick Piggin */ 7464d7672b4SLinus Torvalds if (!(vma->vm_flags & (VM_HUGETLB|VM_NONLINEAR|VM_PFNMAP|VM_INSERTPAGE))) { 747d992895bSNick Piggin if (!vma->anon_vma) 748d992895bSNick Piggin return 0; 749d992895bSNick Piggin } 750d992895bSNick Piggin 7511da177e4SLinus Torvalds if (is_vm_hugetlb_page(vma)) 7521da177e4SLinus Torvalds return copy_hugetlb_page_range(dst_mm, src_mm, vma); 7531da177e4SLinus Torvalds 75434801ba9Svenkatesh.pallipadi@intel.com if (unlikely(is_pfn_mapping(vma))) { 7552ab64037Svenkatesh.pallipadi@intel.com /* 7562ab64037Svenkatesh.pallipadi@intel.com * We do not free on error cases below as remove_vma 7572ab64037Svenkatesh.pallipadi@intel.com * gets called on error from higher level routine 7582ab64037Svenkatesh.pallipadi@intel.com */ 7592ab64037Svenkatesh.pallipadi@intel.com ret = track_pfn_vma_copy(vma); 7602ab64037Svenkatesh.pallipadi@intel.com if (ret) 7612ab64037Svenkatesh.pallipadi@intel.com return ret; 7622ab64037Svenkatesh.pallipadi@intel.com } 7632ab64037Svenkatesh.pallipadi@intel.com 764cddb8a5cSAndrea Arcangeli /* 765cddb8a5cSAndrea Arcangeli * We need to invalidate the secondary MMU mappings only when 766cddb8a5cSAndrea Arcangeli * there could be a permission downgrade on the ptes of the 767cddb8a5cSAndrea Arcangeli * parent mm. And a permission downgrade will only happen if 768cddb8a5cSAndrea Arcangeli * is_cow_mapping() returns true. 769cddb8a5cSAndrea Arcangeli */ 770cddb8a5cSAndrea Arcangeli if (is_cow_mapping(vma->vm_flags)) 771cddb8a5cSAndrea Arcangeli mmu_notifier_invalidate_range_start(src_mm, addr, end); 772cddb8a5cSAndrea Arcangeli 773cddb8a5cSAndrea Arcangeli ret = 0; 7741da177e4SLinus Torvalds dst_pgd = pgd_offset(dst_mm, addr); 7751da177e4SLinus Torvalds src_pgd = pgd_offset(src_mm, addr); 7761da177e4SLinus Torvalds do { 7771da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 7781da177e4SLinus Torvalds if (pgd_none_or_clear_bad(src_pgd)) 7791da177e4SLinus Torvalds continue; 780cddb8a5cSAndrea Arcangeli if (unlikely(copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd, 781cddb8a5cSAndrea Arcangeli vma, addr, next))) { 782cddb8a5cSAndrea Arcangeli ret = -ENOMEM; 783cddb8a5cSAndrea Arcangeli break; 784cddb8a5cSAndrea Arcangeli } 7851da177e4SLinus Torvalds } while (dst_pgd++, src_pgd++, addr = next, addr != end); 786cddb8a5cSAndrea Arcangeli 787cddb8a5cSAndrea Arcangeli if (is_cow_mapping(vma->vm_flags)) 788cddb8a5cSAndrea Arcangeli mmu_notifier_invalidate_range_end(src_mm, 789cddb8a5cSAndrea Arcangeli vma->vm_start, end); 790cddb8a5cSAndrea Arcangeli return ret; 7911da177e4SLinus Torvalds } 7921da177e4SLinus Torvalds 79351c6f666SRobin Holt static unsigned long zap_pte_range(struct mmu_gather *tlb, 794b5810039SNick Piggin struct vm_area_struct *vma, pmd_t *pmd, 7951da177e4SLinus Torvalds unsigned long addr, unsigned long end, 79651c6f666SRobin Holt long *zap_work, struct zap_details *details) 7971da177e4SLinus Torvalds { 798b5810039SNick Piggin struct mm_struct *mm = tlb->mm; 7991da177e4SLinus Torvalds pte_t *pte; 800508034a3SHugh Dickins spinlock_t *ptl; 801ae859762SHugh Dickins int file_rss = 0; 802ae859762SHugh Dickins int anon_rss = 0; 8031da177e4SLinus Torvalds 804508034a3SHugh Dickins pte = pte_offset_map_lock(mm, pmd, addr, &ptl); 8056606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 8061da177e4SLinus Torvalds do { 8071da177e4SLinus Torvalds pte_t ptent = *pte; 80851c6f666SRobin Holt if (pte_none(ptent)) { 80951c6f666SRobin Holt (*zap_work)--; 8101da177e4SLinus Torvalds continue; 81151c6f666SRobin Holt } 81251c6f666SRobin Holt 81351c6f666SRobin Holt (*zap_work) -= PAGE_SIZE; 81451c6f666SRobin Holt 8156f5e6b9eSHugh Dickins if (pte_present(ptent)) { 8166f5e6b9eSHugh Dickins struct page *page; 8176f5e6b9eSHugh Dickins 8186aab341eSLinus Torvalds page = vm_normal_page(vma, addr, ptent); 8191da177e4SLinus Torvalds if (unlikely(details) && page) { 8201da177e4SLinus Torvalds /* 8211da177e4SLinus Torvalds * unmap_shared_mapping_pages() wants to 8221da177e4SLinus Torvalds * invalidate cache without truncating: 8231da177e4SLinus Torvalds * unmap shared but keep private pages. 8241da177e4SLinus Torvalds */ 8251da177e4SLinus Torvalds if (details->check_mapping && 8261da177e4SLinus Torvalds details->check_mapping != page->mapping) 8271da177e4SLinus Torvalds continue; 8281da177e4SLinus Torvalds /* 8291da177e4SLinus Torvalds * Each page->index must be checked when 8301da177e4SLinus Torvalds * invalidating or truncating nonlinear. 8311da177e4SLinus Torvalds */ 8321da177e4SLinus Torvalds if (details->nonlinear_vma && 8331da177e4SLinus Torvalds (page->index < details->first_index || 8341da177e4SLinus Torvalds page->index > details->last_index)) 8351da177e4SLinus Torvalds continue; 8361da177e4SLinus Torvalds } 837b5810039SNick Piggin ptent = ptep_get_and_clear_full(mm, addr, pte, 838a600388dSZachary Amsden tlb->fullmm); 8391da177e4SLinus Torvalds tlb_remove_tlb_entry(tlb, pte, addr); 8401da177e4SLinus Torvalds if (unlikely(!page)) 8411da177e4SLinus Torvalds continue; 8421da177e4SLinus Torvalds if (unlikely(details) && details->nonlinear_vma 8431da177e4SLinus Torvalds && linear_page_index(details->nonlinear_vma, 8441da177e4SLinus Torvalds addr) != page->index) 845b5810039SNick Piggin set_pte_at(mm, addr, pte, 8461da177e4SLinus Torvalds pgoff_to_pte(page->index)); 8471da177e4SLinus Torvalds if (PageAnon(page)) 84886d912f4SHugh Dickins anon_rss--; 8496237bcd9SHugh Dickins else { 8506237bcd9SHugh Dickins if (pte_dirty(ptent)) 8516237bcd9SHugh Dickins set_page_dirty(page); 8524917e5d0SJohannes Weiner if (pte_young(ptent) && 8534917e5d0SJohannes Weiner likely(!VM_SequentialReadHint(vma))) 854bf3f3bc5SNick Piggin mark_page_accessed(page); 85586d912f4SHugh Dickins file_rss--; 8566237bcd9SHugh Dickins } 857edc315fdSHugh Dickins page_remove_rmap(page); 8583dc14741SHugh Dickins if (unlikely(page_mapcount(page) < 0)) 8593dc14741SHugh Dickins print_bad_pte(vma, addr, ptent, page); 8601da177e4SLinus Torvalds tlb_remove_page(tlb, page); 8611da177e4SLinus Torvalds continue; 8621da177e4SLinus Torvalds } 8631da177e4SLinus Torvalds /* 8641da177e4SLinus Torvalds * If details->check_mapping, we leave swap entries; 8651da177e4SLinus Torvalds * if details->nonlinear_vma, we leave file entries. 8661da177e4SLinus Torvalds */ 8671da177e4SLinus Torvalds if (unlikely(details)) 8681da177e4SLinus Torvalds continue; 8692509ef26SHugh Dickins if (pte_file(ptent)) { 8702509ef26SHugh Dickins if (unlikely(!(vma->vm_flags & VM_NONLINEAR))) 8712509ef26SHugh Dickins print_bad_pte(vma, addr, ptent, NULL); 8722509ef26SHugh Dickins } else if 8732509ef26SHugh Dickins (unlikely(!free_swap_and_cache(pte_to_swp_entry(ptent)))) 8742509ef26SHugh Dickins print_bad_pte(vma, addr, ptent, NULL); 8759888a1caSZachary Amsden pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); 87651c6f666SRobin Holt } while (pte++, addr += PAGE_SIZE, (addr != end && *zap_work > 0)); 877ae859762SHugh Dickins 87886d912f4SHugh Dickins add_mm_rss(mm, file_rss, anon_rss); 8796606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 880508034a3SHugh Dickins pte_unmap_unlock(pte - 1, ptl); 88151c6f666SRobin Holt 88251c6f666SRobin Holt return addr; 8831da177e4SLinus Torvalds } 8841da177e4SLinus Torvalds 88551c6f666SRobin Holt static inline unsigned long zap_pmd_range(struct mmu_gather *tlb, 886b5810039SNick Piggin struct vm_area_struct *vma, pud_t *pud, 8871da177e4SLinus Torvalds unsigned long addr, unsigned long end, 88851c6f666SRobin Holt long *zap_work, struct zap_details *details) 8891da177e4SLinus Torvalds { 8901da177e4SLinus Torvalds pmd_t *pmd; 8911da177e4SLinus Torvalds unsigned long next; 8921da177e4SLinus Torvalds 8931da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 8941da177e4SLinus Torvalds do { 8951da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 89651c6f666SRobin Holt if (pmd_none_or_clear_bad(pmd)) { 89751c6f666SRobin Holt (*zap_work)--; 8981da177e4SLinus Torvalds continue; 89951c6f666SRobin Holt } 90051c6f666SRobin Holt next = zap_pte_range(tlb, vma, pmd, addr, next, 90151c6f666SRobin Holt zap_work, details); 90251c6f666SRobin Holt } while (pmd++, addr = next, (addr != end && *zap_work > 0)); 90351c6f666SRobin Holt 90451c6f666SRobin Holt return addr; 9051da177e4SLinus Torvalds } 9061da177e4SLinus Torvalds 90751c6f666SRobin Holt static inline unsigned long zap_pud_range(struct mmu_gather *tlb, 908b5810039SNick Piggin struct vm_area_struct *vma, pgd_t *pgd, 9091da177e4SLinus Torvalds unsigned long addr, unsigned long end, 91051c6f666SRobin Holt long *zap_work, struct zap_details *details) 9111da177e4SLinus Torvalds { 9121da177e4SLinus Torvalds pud_t *pud; 9131da177e4SLinus Torvalds unsigned long next; 9141da177e4SLinus Torvalds 9151da177e4SLinus Torvalds pud = pud_offset(pgd, addr); 9161da177e4SLinus Torvalds do { 9171da177e4SLinus Torvalds next = pud_addr_end(addr, end); 91851c6f666SRobin Holt if (pud_none_or_clear_bad(pud)) { 91951c6f666SRobin Holt (*zap_work)--; 9201da177e4SLinus Torvalds continue; 92151c6f666SRobin Holt } 92251c6f666SRobin Holt next = zap_pmd_range(tlb, vma, pud, addr, next, 92351c6f666SRobin Holt zap_work, details); 92451c6f666SRobin Holt } while (pud++, addr = next, (addr != end && *zap_work > 0)); 92551c6f666SRobin Holt 92651c6f666SRobin Holt return addr; 9271da177e4SLinus Torvalds } 9281da177e4SLinus Torvalds 92951c6f666SRobin Holt static unsigned long unmap_page_range(struct mmu_gather *tlb, 93051c6f666SRobin Holt struct vm_area_struct *vma, 9311da177e4SLinus Torvalds unsigned long addr, unsigned long end, 93251c6f666SRobin Holt long *zap_work, struct zap_details *details) 9331da177e4SLinus Torvalds { 9341da177e4SLinus Torvalds pgd_t *pgd; 9351da177e4SLinus Torvalds unsigned long next; 9361da177e4SLinus Torvalds 9371da177e4SLinus Torvalds if (details && !details->check_mapping && !details->nonlinear_vma) 9381da177e4SLinus Torvalds details = NULL; 9391da177e4SLinus Torvalds 9401da177e4SLinus Torvalds BUG_ON(addr >= end); 9411da177e4SLinus Torvalds tlb_start_vma(tlb, vma); 9421da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 9431da177e4SLinus Torvalds do { 9441da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 94551c6f666SRobin Holt if (pgd_none_or_clear_bad(pgd)) { 94651c6f666SRobin Holt (*zap_work)--; 9471da177e4SLinus Torvalds continue; 94851c6f666SRobin Holt } 94951c6f666SRobin Holt next = zap_pud_range(tlb, vma, pgd, addr, next, 95051c6f666SRobin Holt zap_work, details); 95151c6f666SRobin Holt } while (pgd++, addr = next, (addr != end && *zap_work > 0)); 9521da177e4SLinus Torvalds tlb_end_vma(tlb, vma); 95351c6f666SRobin Holt 95451c6f666SRobin Holt return addr; 9551da177e4SLinus Torvalds } 9561da177e4SLinus Torvalds 9571da177e4SLinus Torvalds #ifdef CONFIG_PREEMPT 9581da177e4SLinus Torvalds # define ZAP_BLOCK_SIZE (8 * PAGE_SIZE) 9591da177e4SLinus Torvalds #else 9601da177e4SLinus Torvalds /* No preempt: go for improved straight-line efficiency */ 9611da177e4SLinus Torvalds # define ZAP_BLOCK_SIZE (1024 * PAGE_SIZE) 9621da177e4SLinus Torvalds #endif 9631da177e4SLinus Torvalds 9641da177e4SLinus Torvalds /** 9651da177e4SLinus Torvalds * unmap_vmas - unmap a range of memory covered by a list of vma's 9661da177e4SLinus Torvalds * @tlbp: address of the caller's struct mmu_gather 9671da177e4SLinus Torvalds * @vma: the starting vma 9681da177e4SLinus Torvalds * @start_addr: virtual address at which to start unmapping 9691da177e4SLinus Torvalds * @end_addr: virtual address at which to end unmapping 9701da177e4SLinus Torvalds * @nr_accounted: Place number of unmapped pages in vm-accountable vma's here 9711da177e4SLinus Torvalds * @details: details of nonlinear truncation or shared cache invalidation 9721da177e4SLinus Torvalds * 973ee39b37bSHugh Dickins * Returns the end address of the unmapping (restart addr if interrupted). 9741da177e4SLinus Torvalds * 975508034a3SHugh Dickins * Unmap all pages in the vma list. 9761da177e4SLinus Torvalds * 977508034a3SHugh Dickins * We aim to not hold locks for too long (for scheduling latency reasons). 978508034a3SHugh Dickins * So zap pages in ZAP_BLOCK_SIZE bytecounts. This means we need to 9791da177e4SLinus Torvalds * return the ending mmu_gather to the caller. 9801da177e4SLinus Torvalds * 9811da177e4SLinus Torvalds * Only addresses between `start' and `end' will be unmapped. 9821da177e4SLinus Torvalds * 9831da177e4SLinus Torvalds * The VMA list must be sorted in ascending virtual address order. 9841da177e4SLinus Torvalds * 9851da177e4SLinus Torvalds * unmap_vmas() assumes that the caller will flush the whole unmapped address 9861da177e4SLinus Torvalds * range after unmap_vmas() returns. So the only responsibility here is to 9871da177e4SLinus Torvalds * ensure that any thus-far unmapped pages are flushed before unmap_vmas() 9881da177e4SLinus Torvalds * drops the lock and schedules. 9891da177e4SLinus Torvalds */ 990508034a3SHugh Dickins unsigned long unmap_vmas(struct mmu_gather **tlbp, 9911da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long start_addr, 9921da177e4SLinus Torvalds unsigned long end_addr, unsigned long *nr_accounted, 9931da177e4SLinus Torvalds struct zap_details *details) 9941da177e4SLinus Torvalds { 99551c6f666SRobin Holt long zap_work = ZAP_BLOCK_SIZE; 9961da177e4SLinus Torvalds unsigned long tlb_start = 0; /* For tlb_finish_mmu */ 9971da177e4SLinus Torvalds int tlb_start_valid = 0; 998ee39b37bSHugh Dickins unsigned long start = start_addr; 9991da177e4SLinus Torvalds spinlock_t *i_mmap_lock = details? details->i_mmap_lock: NULL; 10004d6ddfa9SHugh Dickins int fullmm = (*tlbp)->fullmm; 1001cddb8a5cSAndrea Arcangeli struct mm_struct *mm = vma->vm_mm; 10021da177e4SLinus Torvalds 1003cddb8a5cSAndrea Arcangeli mmu_notifier_invalidate_range_start(mm, start_addr, end_addr); 10041da177e4SLinus Torvalds for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) { 10051da177e4SLinus Torvalds unsigned long end; 10061da177e4SLinus Torvalds 10071da177e4SLinus Torvalds start = max(vma->vm_start, start_addr); 10081da177e4SLinus Torvalds if (start >= vma->vm_end) 10091da177e4SLinus Torvalds continue; 10101da177e4SLinus Torvalds end = min(vma->vm_end, end_addr); 10111da177e4SLinus Torvalds if (end <= vma->vm_start) 10121da177e4SLinus Torvalds continue; 10131da177e4SLinus Torvalds 10141da177e4SLinus Torvalds if (vma->vm_flags & VM_ACCOUNT) 10151da177e4SLinus Torvalds *nr_accounted += (end - start) >> PAGE_SHIFT; 10161da177e4SLinus Torvalds 101734801ba9Svenkatesh.pallipadi@intel.com if (unlikely(is_pfn_mapping(vma))) 10182ab64037Svenkatesh.pallipadi@intel.com untrack_pfn_vma(vma, 0, 0); 10192ab64037Svenkatesh.pallipadi@intel.com 10201da177e4SLinus Torvalds while (start != end) { 10211da177e4SLinus Torvalds if (!tlb_start_valid) { 10221da177e4SLinus Torvalds tlb_start = start; 10231da177e4SLinus Torvalds tlb_start_valid = 1; 10241da177e4SLinus Torvalds } 10251da177e4SLinus Torvalds 102651c6f666SRobin Holt if (unlikely(is_vm_hugetlb_page(vma))) { 1027a137e1ccSAndi Kleen /* 1028a137e1ccSAndi Kleen * It is undesirable to test vma->vm_file as it 1029a137e1ccSAndi Kleen * should be non-null for valid hugetlb area. 1030a137e1ccSAndi Kleen * However, vm_file will be NULL in the error 1031a137e1ccSAndi Kleen * cleanup path of do_mmap_pgoff. When 1032a137e1ccSAndi Kleen * hugetlbfs ->mmap method fails, 1033a137e1ccSAndi Kleen * do_mmap_pgoff() nullifies vma->vm_file 1034a137e1ccSAndi Kleen * before calling this function to clean up. 1035a137e1ccSAndi Kleen * Since no pte has actually been setup, it is 1036a137e1ccSAndi Kleen * safe to do nothing in this case. 1037a137e1ccSAndi Kleen */ 1038a137e1ccSAndi Kleen if (vma->vm_file) { 103904f2cbe3SMel Gorman unmap_hugepage_range(vma, start, end, NULL); 104051c6f666SRobin Holt zap_work -= (end - start) / 1041a5516438SAndi Kleen pages_per_huge_page(hstate_vma(vma)); 1042a137e1ccSAndi Kleen } 1043a137e1ccSAndi Kleen 104451c6f666SRobin Holt start = end; 104551c6f666SRobin Holt } else 104651c6f666SRobin Holt start = unmap_page_range(*tlbp, vma, 104751c6f666SRobin Holt start, end, &zap_work, details); 10481da177e4SLinus Torvalds 104951c6f666SRobin Holt if (zap_work > 0) { 105051c6f666SRobin Holt BUG_ON(start != end); 105151c6f666SRobin Holt break; 105251c6f666SRobin Holt } 10531da177e4SLinus Torvalds 10541da177e4SLinus Torvalds tlb_finish_mmu(*tlbp, tlb_start, start); 10551da177e4SLinus Torvalds 10561da177e4SLinus Torvalds if (need_resched() || 105795c354feSNick Piggin (i_mmap_lock && spin_needbreak(i_mmap_lock))) { 10581da177e4SLinus Torvalds if (i_mmap_lock) { 1059508034a3SHugh Dickins *tlbp = NULL; 10601da177e4SLinus Torvalds goto out; 10611da177e4SLinus Torvalds } 10621da177e4SLinus Torvalds cond_resched(); 10631da177e4SLinus Torvalds } 10641da177e4SLinus Torvalds 1065508034a3SHugh Dickins *tlbp = tlb_gather_mmu(vma->vm_mm, fullmm); 10661da177e4SLinus Torvalds tlb_start_valid = 0; 106751c6f666SRobin Holt zap_work = ZAP_BLOCK_SIZE; 10681da177e4SLinus Torvalds } 10691da177e4SLinus Torvalds } 10701da177e4SLinus Torvalds out: 1071cddb8a5cSAndrea Arcangeli mmu_notifier_invalidate_range_end(mm, start_addr, end_addr); 1072ee39b37bSHugh Dickins return start; /* which is now the end (or restart) address */ 10731da177e4SLinus Torvalds } 10741da177e4SLinus Torvalds 10751da177e4SLinus Torvalds /** 10761da177e4SLinus Torvalds * zap_page_range - remove user pages in a given range 10771da177e4SLinus Torvalds * @vma: vm_area_struct holding the applicable pages 10781da177e4SLinus Torvalds * @address: starting address of pages to zap 10791da177e4SLinus Torvalds * @size: number of bytes to zap 10801da177e4SLinus Torvalds * @details: details of nonlinear truncation or shared cache invalidation 10811da177e4SLinus Torvalds */ 1082ee39b37bSHugh Dickins unsigned long zap_page_range(struct vm_area_struct *vma, unsigned long address, 10831da177e4SLinus Torvalds unsigned long size, struct zap_details *details) 10841da177e4SLinus Torvalds { 10851da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 10861da177e4SLinus Torvalds struct mmu_gather *tlb; 10871da177e4SLinus Torvalds unsigned long end = address + size; 10881da177e4SLinus Torvalds unsigned long nr_accounted = 0; 10891da177e4SLinus Torvalds 10901da177e4SLinus Torvalds lru_add_drain(); 10911da177e4SLinus Torvalds tlb = tlb_gather_mmu(mm, 0); 1092365e9c87SHugh Dickins update_hiwater_rss(mm); 1093508034a3SHugh Dickins end = unmap_vmas(&tlb, vma, address, end, &nr_accounted, details); 1094508034a3SHugh Dickins if (tlb) 10958f4f8c16SHugh Dickins tlb_finish_mmu(tlb, address, end); 1096ee39b37bSHugh Dickins return end; 10971da177e4SLinus Torvalds } 10981da177e4SLinus Torvalds 1099c627f9ccSJack Steiner /** 1100c627f9ccSJack Steiner * zap_vma_ptes - remove ptes mapping the vma 1101c627f9ccSJack Steiner * @vma: vm_area_struct holding ptes to be zapped 1102c627f9ccSJack Steiner * @address: starting address of pages to zap 1103c627f9ccSJack Steiner * @size: number of bytes to zap 1104c627f9ccSJack Steiner * 1105c627f9ccSJack Steiner * This function only unmaps ptes assigned to VM_PFNMAP vmas. 1106c627f9ccSJack Steiner * 1107c627f9ccSJack Steiner * The entire address range must be fully contained within the vma. 1108c627f9ccSJack Steiner * 1109c627f9ccSJack Steiner * Returns 0 if successful. 1110c627f9ccSJack Steiner */ 1111c627f9ccSJack Steiner int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, 1112c627f9ccSJack Steiner unsigned long size) 1113c627f9ccSJack Steiner { 1114c627f9ccSJack Steiner if (address < vma->vm_start || address + size > vma->vm_end || 1115c627f9ccSJack Steiner !(vma->vm_flags & VM_PFNMAP)) 1116c627f9ccSJack Steiner return -1; 1117c627f9ccSJack Steiner zap_page_range(vma, address, size, NULL); 1118c627f9ccSJack Steiner return 0; 1119c627f9ccSJack Steiner } 1120c627f9ccSJack Steiner EXPORT_SYMBOL_GPL(zap_vma_ptes); 1121c627f9ccSJack Steiner 11221da177e4SLinus Torvalds /* 11231da177e4SLinus Torvalds * Do a quick page-table lookup for a single page. 11241da177e4SLinus Torvalds */ 11256aab341eSLinus Torvalds struct page *follow_page(struct vm_area_struct *vma, unsigned long address, 1126deceb6cdSHugh Dickins unsigned int flags) 11271da177e4SLinus Torvalds { 11281da177e4SLinus Torvalds pgd_t *pgd; 11291da177e4SLinus Torvalds pud_t *pud; 11301da177e4SLinus Torvalds pmd_t *pmd; 11311da177e4SLinus Torvalds pte_t *ptep, pte; 1132deceb6cdSHugh Dickins spinlock_t *ptl; 11331da177e4SLinus Torvalds struct page *page; 11346aab341eSLinus Torvalds struct mm_struct *mm = vma->vm_mm; 11351da177e4SLinus Torvalds 1136deceb6cdSHugh Dickins page = follow_huge_addr(mm, address, flags & FOLL_WRITE); 1137deceb6cdSHugh Dickins if (!IS_ERR(page)) { 1138deceb6cdSHugh Dickins BUG_ON(flags & FOLL_GET); 1139deceb6cdSHugh Dickins goto out; 1140deceb6cdSHugh Dickins } 11411da177e4SLinus Torvalds 1142deceb6cdSHugh Dickins page = NULL; 11431da177e4SLinus Torvalds pgd = pgd_offset(mm, address); 11441da177e4SLinus Torvalds if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) 1145deceb6cdSHugh Dickins goto no_page_table; 11461da177e4SLinus Torvalds 11471da177e4SLinus Torvalds pud = pud_offset(pgd, address); 1148ceb86879SAndi Kleen if (pud_none(*pud)) 1149ceb86879SAndi Kleen goto no_page_table; 1150ceb86879SAndi Kleen if (pud_huge(*pud)) { 1151ceb86879SAndi Kleen BUG_ON(flags & FOLL_GET); 1152ceb86879SAndi Kleen page = follow_huge_pud(mm, address, pud, flags & FOLL_WRITE); 1153ceb86879SAndi Kleen goto out; 1154ceb86879SAndi Kleen } 1155ceb86879SAndi Kleen if (unlikely(pud_bad(*pud))) 1156deceb6cdSHugh Dickins goto no_page_table; 11571da177e4SLinus Torvalds 11581da177e4SLinus Torvalds pmd = pmd_offset(pud, address); 1159aeed5fceSHugh Dickins if (pmd_none(*pmd)) 1160deceb6cdSHugh Dickins goto no_page_table; 1161deceb6cdSHugh Dickins if (pmd_huge(*pmd)) { 1162deceb6cdSHugh Dickins BUG_ON(flags & FOLL_GET); 1163deceb6cdSHugh Dickins page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE); 1164deceb6cdSHugh Dickins goto out; 1165deceb6cdSHugh Dickins } 1166aeed5fceSHugh Dickins if (unlikely(pmd_bad(*pmd))) 1167aeed5fceSHugh Dickins goto no_page_table; 1168aeed5fceSHugh Dickins 1169deceb6cdSHugh Dickins ptep = pte_offset_map_lock(mm, pmd, address, &ptl); 11701da177e4SLinus Torvalds 11711da177e4SLinus Torvalds pte = *ptep; 1172deceb6cdSHugh Dickins if (!pte_present(pte)) 117389f5b7daSLinus Torvalds goto no_page; 1174deceb6cdSHugh Dickins if ((flags & FOLL_WRITE) && !pte_write(pte)) 1175deceb6cdSHugh Dickins goto unlock; 1176a13ea5b7SHugh Dickins 11776aab341eSLinus Torvalds page = vm_normal_page(vma, address, pte); 1178a13ea5b7SHugh Dickins if (unlikely(!page)) { 1179a13ea5b7SHugh Dickins if ((flags & FOLL_DUMP) || 1180*62eede62SHugh Dickins !is_zero_pfn(pte_pfn(pte))) 118189f5b7daSLinus Torvalds goto bad_page; 1182a13ea5b7SHugh Dickins page = pte_page(pte); 1183a13ea5b7SHugh Dickins } 1184deceb6cdSHugh Dickins 1185deceb6cdSHugh Dickins if (flags & FOLL_GET) 1186deceb6cdSHugh Dickins get_page(page); 1187deceb6cdSHugh Dickins if (flags & FOLL_TOUCH) { 1188deceb6cdSHugh Dickins if ((flags & FOLL_WRITE) && 1189deceb6cdSHugh Dickins !pte_dirty(pte) && !PageDirty(page)) 1190f33ea7f4SNick Piggin set_page_dirty(page); 1191bd775c42SKOSAKI Motohiro /* 1192bd775c42SKOSAKI Motohiro * pte_mkyoung() would be more correct here, but atomic care 1193bd775c42SKOSAKI Motohiro * is needed to avoid losing the dirty bit: it is easier to use 1194bd775c42SKOSAKI Motohiro * mark_page_accessed(). 1195bd775c42SKOSAKI Motohiro */ 11961da177e4SLinus Torvalds mark_page_accessed(page); 11971da177e4SLinus Torvalds } 1198deceb6cdSHugh Dickins unlock: 1199deceb6cdSHugh Dickins pte_unmap_unlock(ptep, ptl); 12001da177e4SLinus Torvalds out: 1201deceb6cdSHugh Dickins return page; 1202deceb6cdSHugh Dickins 120389f5b7daSLinus Torvalds bad_page: 120489f5b7daSLinus Torvalds pte_unmap_unlock(ptep, ptl); 120589f5b7daSLinus Torvalds return ERR_PTR(-EFAULT); 120689f5b7daSLinus Torvalds 120789f5b7daSLinus Torvalds no_page: 120889f5b7daSLinus Torvalds pte_unmap_unlock(ptep, ptl); 120989f5b7daSLinus Torvalds if (!pte_none(pte)) 121089f5b7daSLinus Torvalds return page; 12118e4b9a60SHugh Dickins 1212deceb6cdSHugh Dickins no_page_table: 1213deceb6cdSHugh Dickins /* 1214deceb6cdSHugh Dickins * When core dumping an enormous anonymous area that nobody 12158e4b9a60SHugh Dickins * has touched so far, we don't want to allocate unnecessary pages or 12168e4b9a60SHugh Dickins * page tables. Return error instead of NULL to skip handle_mm_fault, 12178e4b9a60SHugh Dickins * then get_dump_page() will return NULL to leave a hole in the dump. 12188e4b9a60SHugh Dickins * But we can only make this optimization where a hole would surely 12198e4b9a60SHugh Dickins * be zero-filled if handle_mm_fault() actually did handle it. 1220deceb6cdSHugh Dickins */ 12218e4b9a60SHugh Dickins if ((flags & FOLL_DUMP) && 12228e4b9a60SHugh Dickins (!vma->vm_ops || !vma->vm_ops->fault)) 12238e4b9a60SHugh Dickins return ERR_PTR(-EFAULT); 1224deceb6cdSHugh Dickins return page; 12251da177e4SLinus Torvalds } 12261da177e4SLinus Torvalds 1227b291f000SNick Piggin int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm, 122858fa879eSHugh Dickins unsigned long start, int nr_pages, unsigned int gup_flags, 12291da177e4SLinus Torvalds struct page **pages, struct vm_area_struct **vmas) 12301da177e4SLinus Torvalds { 12311da177e4SLinus Torvalds int i; 123258fa879eSHugh Dickins unsigned long vm_flags; 12331da177e4SLinus Torvalds 12349d73777eSPeter Zijlstra if (nr_pages <= 0) 1235900cf086SJonathan Corbet return 0; 123658fa879eSHugh Dickins 123758fa879eSHugh Dickins VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET)); 123858fa879eSHugh Dickins 12391da177e4SLinus Torvalds /* 12401da177e4SLinus Torvalds * Require read or write permissions. 124158fa879eSHugh Dickins * If FOLL_FORCE is set, we only require the "MAY" flags. 12421da177e4SLinus Torvalds */ 124358fa879eSHugh Dickins vm_flags = (gup_flags & FOLL_WRITE) ? 124458fa879eSHugh Dickins (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD); 124558fa879eSHugh Dickins vm_flags &= (gup_flags & FOLL_FORCE) ? 124658fa879eSHugh Dickins (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE); 12471da177e4SLinus Torvalds i = 0; 12481da177e4SLinus Torvalds 12491da177e4SLinus Torvalds do { 12501da177e4SLinus Torvalds struct vm_area_struct *vma; 12511da177e4SLinus Torvalds 12521da177e4SLinus Torvalds vma = find_extend_vma(mm, start); 12531da177e4SLinus Torvalds if (!vma && in_gate_area(tsk, start)) { 12541da177e4SLinus Torvalds unsigned long pg = start & PAGE_MASK; 12551da177e4SLinus Torvalds struct vm_area_struct *gate_vma = get_gate_vma(tsk); 12561da177e4SLinus Torvalds pgd_t *pgd; 12571da177e4SLinus Torvalds pud_t *pud; 12581da177e4SLinus Torvalds pmd_t *pmd; 12591da177e4SLinus Torvalds pte_t *pte; 1260b291f000SNick Piggin 1261b291f000SNick Piggin /* user gate pages are read-only */ 126258fa879eSHugh Dickins if (gup_flags & FOLL_WRITE) 12631da177e4SLinus Torvalds return i ? : -EFAULT; 12641da177e4SLinus Torvalds if (pg > TASK_SIZE) 12651da177e4SLinus Torvalds pgd = pgd_offset_k(pg); 12661da177e4SLinus Torvalds else 12671da177e4SLinus Torvalds pgd = pgd_offset_gate(mm, pg); 12681da177e4SLinus Torvalds BUG_ON(pgd_none(*pgd)); 12691da177e4SLinus Torvalds pud = pud_offset(pgd, pg); 12701da177e4SLinus Torvalds BUG_ON(pud_none(*pud)); 12711da177e4SLinus Torvalds pmd = pmd_offset(pud, pg); 1272690dbe1cSHugh Dickins if (pmd_none(*pmd)) 1273690dbe1cSHugh Dickins return i ? : -EFAULT; 12741da177e4SLinus Torvalds pte = pte_offset_map(pmd, pg); 1275690dbe1cSHugh Dickins if (pte_none(*pte)) { 1276690dbe1cSHugh Dickins pte_unmap(pte); 1277690dbe1cSHugh Dickins return i ? : -EFAULT; 1278690dbe1cSHugh Dickins } 12791da177e4SLinus Torvalds if (pages) { 1280fa2a455bSNick Piggin struct page *page = vm_normal_page(gate_vma, start, *pte); 12816aab341eSLinus Torvalds pages[i] = page; 12826aab341eSLinus Torvalds if (page) 12836aab341eSLinus Torvalds get_page(page); 12841da177e4SLinus Torvalds } 12851da177e4SLinus Torvalds pte_unmap(pte); 12861da177e4SLinus Torvalds if (vmas) 12871da177e4SLinus Torvalds vmas[i] = gate_vma; 12881da177e4SLinus Torvalds i++; 12891da177e4SLinus Torvalds start += PAGE_SIZE; 12909d73777eSPeter Zijlstra nr_pages--; 12911da177e4SLinus Torvalds continue; 12921da177e4SLinus Torvalds } 12931da177e4SLinus Torvalds 1294b291f000SNick Piggin if (!vma || 1295b291f000SNick Piggin (vma->vm_flags & (VM_IO | VM_PFNMAP)) || 12961c3aff1cSHugh Dickins !(vm_flags & vma->vm_flags)) 12971da177e4SLinus Torvalds return i ? : -EFAULT; 12981da177e4SLinus Torvalds 12992a15efc9SHugh Dickins if (is_vm_hugetlb_page(vma)) { 13002a15efc9SHugh Dickins i = follow_hugetlb_page(mm, vma, pages, vmas, 130158fa879eSHugh Dickins &start, &nr_pages, i, gup_flags); 13022a15efc9SHugh Dickins continue; 13032a15efc9SHugh Dickins } 1304deceb6cdSHugh Dickins 13051da177e4SLinus Torvalds do { 130608ef4729SHugh Dickins struct page *page; 130758fa879eSHugh Dickins unsigned int foll_flags = gup_flags; 13081da177e4SLinus Torvalds 1309462e00ccSEthan Solomita /* 13104779280dSYing Han * If we have a pending SIGKILL, don't keep faulting 13111c3aff1cSHugh Dickins * pages and potentially allocating memory. 1312462e00ccSEthan Solomita */ 13131c3aff1cSHugh Dickins if (unlikely(fatal_signal_pending(current))) 13144779280dSYing Han return i ? i : -ERESTARTSYS; 1315462e00ccSEthan Solomita 1316deceb6cdSHugh Dickins cond_resched(); 13176aab341eSLinus Torvalds while (!(page = follow_page(vma, start, foll_flags))) { 1318a68d2ebcSLinus Torvalds int ret; 1319d06063ccSLinus Torvalds 1320d26ed650SHugh Dickins ret = handle_mm_fault(mm, vma, start, 1321d26ed650SHugh Dickins (foll_flags & FOLL_WRITE) ? 1322d26ed650SHugh Dickins FAULT_FLAG_WRITE : 0); 1323d26ed650SHugh Dickins 132483c54070SNick Piggin if (ret & VM_FAULT_ERROR) { 132583c54070SNick Piggin if (ret & VM_FAULT_OOM) 132683c54070SNick Piggin return i ? i : -ENOMEM; 132783c54070SNick Piggin else if (ret & VM_FAULT_SIGBUS) 132883c54070SNick Piggin return i ? i : -EFAULT; 132983c54070SNick Piggin BUG(); 133083c54070SNick Piggin } 133183c54070SNick Piggin if (ret & VM_FAULT_MAJOR) 133283c54070SNick Piggin tsk->maj_flt++; 133383c54070SNick Piggin else 133483c54070SNick Piggin tsk->min_flt++; 133583c54070SNick Piggin 1336f33ea7f4SNick Piggin /* 133783c54070SNick Piggin * The VM_FAULT_WRITE bit tells us that 133883c54070SNick Piggin * do_wp_page has broken COW when necessary, 133983c54070SNick Piggin * even if maybe_mkwrite decided not to set 134083c54070SNick Piggin * pte_write. We can thus safely do subsequent 1341878b63acSHugh Dickins * page lookups as if they were reads. But only 1342878b63acSHugh Dickins * do so when looping for pte_write is futile: 1343878b63acSHugh Dickins * in some cases userspace may also be wanting 1344878b63acSHugh Dickins * to write to the gotten user page, which a 1345878b63acSHugh Dickins * read fault here might prevent (a readonly 1346878b63acSHugh Dickins * page might get reCOWed by userspace write). 1347f33ea7f4SNick Piggin */ 1348878b63acSHugh Dickins if ((ret & VM_FAULT_WRITE) && 1349878b63acSHugh Dickins !(vma->vm_flags & VM_WRITE)) 1350deceb6cdSHugh Dickins foll_flags &= ~FOLL_WRITE; 1351a68d2ebcSLinus Torvalds 13527f7bbbe5SBenjamin Herrenschmidt cond_resched(); 13531da177e4SLinus Torvalds } 135489f5b7daSLinus Torvalds if (IS_ERR(page)) 135589f5b7daSLinus Torvalds return i ? i : PTR_ERR(page); 13561da177e4SLinus Torvalds if (pages) { 135708ef4729SHugh Dickins pages[i] = page; 135803beb076SJames Bottomley 1359a6f36be3SRussell King flush_anon_page(vma, page, start); 136008ef4729SHugh Dickins flush_dcache_page(page); 13611da177e4SLinus Torvalds } 13621da177e4SLinus Torvalds if (vmas) 13631da177e4SLinus Torvalds vmas[i] = vma; 13641da177e4SLinus Torvalds i++; 13651da177e4SLinus Torvalds start += PAGE_SIZE; 13669d73777eSPeter Zijlstra nr_pages--; 13679d73777eSPeter Zijlstra } while (nr_pages && start < vma->vm_end); 13689d73777eSPeter Zijlstra } while (nr_pages); 13691da177e4SLinus Torvalds return i; 13701da177e4SLinus Torvalds } 1371b291f000SNick Piggin 1372d2bf6be8SNick Piggin /** 1373d2bf6be8SNick Piggin * get_user_pages() - pin user pages in memory 1374d2bf6be8SNick Piggin * @tsk: task_struct of target task 1375d2bf6be8SNick Piggin * @mm: mm_struct of target mm 1376d2bf6be8SNick Piggin * @start: starting user address 13779d73777eSPeter Zijlstra * @nr_pages: number of pages from start to pin 1378d2bf6be8SNick Piggin * @write: whether pages will be written to by the caller 1379d2bf6be8SNick Piggin * @force: whether to force write access even if user mapping is 1380d2bf6be8SNick Piggin * readonly. This will result in the page being COWed even 1381d2bf6be8SNick Piggin * in MAP_SHARED mappings. You do not want this. 1382d2bf6be8SNick Piggin * @pages: array that receives pointers to the pages pinned. 1383d2bf6be8SNick Piggin * Should be at least nr_pages long. Or NULL, if caller 1384d2bf6be8SNick Piggin * only intends to ensure the pages are faulted in. 1385d2bf6be8SNick Piggin * @vmas: array of pointers to vmas corresponding to each page. 1386d2bf6be8SNick Piggin * Or NULL if the caller does not require them. 1387d2bf6be8SNick Piggin * 1388d2bf6be8SNick Piggin * Returns number of pages pinned. This may be fewer than the number 13899d73777eSPeter Zijlstra * requested. If nr_pages is 0 or negative, returns 0. If no pages 1390d2bf6be8SNick Piggin * were pinned, returns -errno. Each page returned must be released 1391d2bf6be8SNick Piggin * with a put_page() call when it is finished with. vmas will only 1392d2bf6be8SNick Piggin * remain valid while mmap_sem is held. 1393d2bf6be8SNick Piggin * 1394d2bf6be8SNick Piggin * Must be called with mmap_sem held for read or write. 1395d2bf6be8SNick Piggin * 1396d2bf6be8SNick Piggin * get_user_pages walks a process's page tables and takes a reference to 1397d2bf6be8SNick Piggin * each struct page that each user address corresponds to at a given 1398d2bf6be8SNick Piggin * instant. That is, it takes the page that would be accessed if a user 1399d2bf6be8SNick Piggin * thread accesses the given user virtual address at that instant. 1400d2bf6be8SNick Piggin * 1401d2bf6be8SNick Piggin * This does not guarantee that the page exists in the user mappings when 1402d2bf6be8SNick Piggin * get_user_pages returns, and there may even be a completely different 1403d2bf6be8SNick Piggin * page there in some cases (eg. if mmapped pagecache has been invalidated 1404d2bf6be8SNick Piggin * and subsequently re faulted). However it does guarantee that the page 1405d2bf6be8SNick Piggin * won't be freed completely. And mostly callers simply care that the page 1406d2bf6be8SNick Piggin * contains data that was valid *at some point in time*. Typically, an IO 1407d2bf6be8SNick Piggin * or similar operation cannot guarantee anything stronger anyway because 1408d2bf6be8SNick Piggin * locks can't be held over the syscall boundary. 1409d2bf6be8SNick Piggin * 1410d2bf6be8SNick Piggin * If write=0, the page must not be written to. If the page is written to, 1411d2bf6be8SNick Piggin * set_page_dirty (or set_page_dirty_lock, as appropriate) must be called 1412d2bf6be8SNick Piggin * after the page is finished with, and before put_page is called. 1413d2bf6be8SNick Piggin * 1414d2bf6be8SNick Piggin * get_user_pages is typically used for fewer-copy IO operations, to get a 1415d2bf6be8SNick Piggin * handle on the memory by some means other than accesses via the user virtual 1416d2bf6be8SNick Piggin * addresses. The pages may be submitted for DMA to devices or accessed via 1417d2bf6be8SNick Piggin * their kernel linear mapping (via the kmap APIs). Care should be taken to 1418d2bf6be8SNick Piggin * use the correct cache flushing APIs. 1419d2bf6be8SNick Piggin * 1420d2bf6be8SNick Piggin * See also get_user_pages_fast, for performance critical applications. 1421d2bf6be8SNick Piggin */ 1422b291f000SNick Piggin int get_user_pages(struct task_struct *tsk, struct mm_struct *mm, 14239d73777eSPeter Zijlstra unsigned long start, int nr_pages, int write, int force, 1424b291f000SNick Piggin struct page **pages, struct vm_area_struct **vmas) 1425b291f000SNick Piggin { 142658fa879eSHugh Dickins int flags = FOLL_TOUCH; 1427b291f000SNick Piggin 142858fa879eSHugh Dickins if (pages) 142958fa879eSHugh Dickins flags |= FOLL_GET; 1430b291f000SNick Piggin if (write) 143158fa879eSHugh Dickins flags |= FOLL_WRITE; 1432b291f000SNick Piggin if (force) 143358fa879eSHugh Dickins flags |= FOLL_FORCE; 1434b291f000SNick Piggin 14359d73777eSPeter Zijlstra return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas); 1436b291f000SNick Piggin } 14371da177e4SLinus Torvalds EXPORT_SYMBOL(get_user_pages); 14381da177e4SLinus Torvalds 1439f3e8fccdSHugh Dickins /** 1440f3e8fccdSHugh Dickins * get_dump_page() - pin user page in memory while writing it to core dump 1441f3e8fccdSHugh Dickins * @addr: user address 1442f3e8fccdSHugh Dickins * 1443f3e8fccdSHugh Dickins * Returns struct page pointer of user page pinned for dump, 1444f3e8fccdSHugh Dickins * to be freed afterwards by page_cache_release() or put_page(). 1445f3e8fccdSHugh Dickins * 1446f3e8fccdSHugh Dickins * Returns NULL on any kind of failure - a hole must then be inserted into 1447f3e8fccdSHugh Dickins * the corefile, to preserve alignment with its headers; and also returns 1448f3e8fccdSHugh Dickins * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found - 1449f3e8fccdSHugh Dickins * allowing a hole to be left in the corefile to save diskspace. 1450f3e8fccdSHugh Dickins * 1451f3e8fccdSHugh Dickins * Called without mmap_sem, but after all other threads have been killed. 1452f3e8fccdSHugh Dickins */ 1453f3e8fccdSHugh Dickins #ifdef CONFIG_ELF_CORE 1454f3e8fccdSHugh Dickins struct page *get_dump_page(unsigned long addr) 1455f3e8fccdSHugh Dickins { 1456f3e8fccdSHugh Dickins struct vm_area_struct *vma; 1457f3e8fccdSHugh Dickins struct page *page; 1458f3e8fccdSHugh Dickins 1459f3e8fccdSHugh Dickins if (__get_user_pages(current, current->mm, addr, 1, 146058fa879eSHugh Dickins FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma) < 1) 1461f3e8fccdSHugh Dickins return NULL; 1462f3e8fccdSHugh Dickins flush_cache_page(vma, addr, page_to_pfn(page)); 1463f3e8fccdSHugh Dickins return page; 1464f3e8fccdSHugh Dickins } 1465f3e8fccdSHugh Dickins #endif /* CONFIG_ELF_CORE */ 1466f3e8fccdSHugh Dickins 1467920c7a5dSHarvey Harrison pte_t *get_locked_pte(struct mm_struct *mm, unsigned long addr, 1468920c7a5dSHarvey Harrison spinlock_t **ptl) 1469c9cfcddfSLinus Torvalds { 1470c9cfcddfSLinus Torvalds pgd_t * pgd = pgd_offset(mm, addr); 1471c9cfcddfSLinus Torvalds pud_t * pud = pud_alloc(mm, pgd, addr); 1472c9cfcddfSLinus Torvalds if (pud) { 147349c91fb0STrond Myklebust pmd_t * pmd = pmd_alloc(mm, pud, addr); 1474c9cfcddfSLinus Torvalds if (pmd) 1475c9cfcddfSLinus Torvalds return pte_alloc_map_lock(mm, pmd, addr, ptl); 1476c9cfcddfSLinus Torvalds } 1477c9cfcddfSLinus Torvalds return NULL; 1478c9cfcddfSLinus Torvalds } 1479c9cfcddfSLinus Torvalds 14801da177e4SLinus Torvalds /* 1481238f58d8SLinus Torvalds * This is the old fallback for page remapping. 1482238f58d8SLinus Torvalds * 1483238f58d8SLinus Torvalds * For historical reasons, it only allows reserved pages. Only 1484238f58d8SLinus Torvalds * old drivers should use this, and they needed to mark their 1485238f58d8SLinus Torvalds * pages reserved for the old functions anyway. 1486238f58d8SLinus Torvalds */ 1487423bad60SNick Piggin static int insert_page(struct vm_area_struct *vma, unsigned long addr, 1488423bad60SNick Piggin struct page *page, pgprot_t prot) 1489238f58d8SLinus Torvalds { 1490423bad60SNick Piggin struct mm_struct *mm = vma->vm_mm; 1491238f58d8SLinus Torvalds int retval; 1492238f58d8SLinus Torvalds pte_t *pte; 1493238f58d8SLinus Torvalds spinlock_t *ptl; 1494238f58d8SLinus Torvalds 1495238f58d8SLinus Torvalds retval = -EINVAL; 1496a145dd41SLinus Torvalds if (PageAnon(page)) 14975b4e655eSKAMEZAWA Hiroyuki goto out; 1498238f58d8SLinus Torvalds retval = -ENOMEM; 1499238f58d8SLinus Torvalds flush_dcache_page(page); 1500c9cfcddfSLinus Torvalds pte = get_locked_pte(mm, addr, &ptl); 1501238f58d8SLinus Torvalds if (!pte) 15025b4e655eSKAMEZAWA Hiroyuki goto out; 1503238f58d8SLinus Torvalds retval = -EBUSY; 1504238f58d8SLinus Torvalds if (!pte_none(*pte)) 1505238f58d8SLinus Torvalds goto out_unlock; 1506238f58d8SLinus Torvalds 1507238f58d8SLinus Torvalds /* Ok, finally just insert the thing.. */ 1508238f58d8SLinus Torvalds get_page(page); 1509238f58d8SLinus Torvalds inc_mm_counter(mm, file_rss); 1510238f58d8SLinus Torvalds page_add_file_rmap(page); 1511238f58d8SLinus Torvalds set_pte_at(mm, addr, pte, mk_pte(page, prot)); 1512238f58d8SLinus Torvalds 1513238f58d8SLinus Torvalds retval = 0; 15148a9f3ccdSBalbir Singh pte_unmap_unlock(pte, ptl); 15158a9f3ccdSBalbir Singh return retval; 1516238f58d8SLinus Torvalds out_unlock: 1517238f58d8SLinus Torvalds pte_unmap_unlock(pte, ptl); 1518238f58d8SLinus Torvalds out: 1519238f58d8SLinus Torvalds return retval; 1520238f58d8SLinus Torvalds } 1521238f58d8SLinus Torvalds 1522bfa5bf6dSRolf Eike Beer /** 1523bfa5bf6dSRolf Eike Beer * vm_insert_page - insert single page into user vma 1524bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 1525bfa5bf6dSRolf Eike Beer * @addr: target user address of this page 1526bfa5bf6dSRolf Eike Beer * @page: source kernel page 1527bfa5bf6dSRolf Eike Beer * 1528a145dd41SLinus Torvalds * This allows drivers to insert individual pages they've allocated 1529a145dd41SLinus Torvalds * into a user vma. 1530a145dd41SLinus Torvalds * 1531a145dd41SLinus Torvalds * The page has to be a nice clean _individual_ kernel allocation. 1532a145dd41SLinus Torvalds * If you allocate a compound page, you need to have marked it as 1533a145dd41SLinus Torvalds * such (__GFP_COMP), or manually just split the page up yourself 15348dfcc9baSNick Piggin * (see split_page()). 1535a145dd41SLinus Torvalds * 1536a145dd41SLinus Torvalds * NOTE! Traditionally this was done with "remap_pfn_range()" which 1537a145dd41SLinus Torvalds * took an arbitrary page protection parameter. This doesn't allow 1538a145dd41SLinus Torvalds * that. Your vma protection will have to be set up correctly, which 1539a145dd41SLinus Torvalds * means that if you want a shared writable mapping, you'd better 1540a145dd41SLinus Torvalds * ask for a shared writable mapping! 1541a145dd41SLinus Torvalds * 1542a145dd41SLinus Torvalds * The page does not need to be reserved. 1543a145dd41SLinus Torvalds */ 1544423bad60SNick Piggin int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, 1545423bad60SNick Piggin struct page *page) 1546a145dd41SLinus Torvalds { 1547a145dd41SLinus Torvalds if (addr < vma->vm_start || addr >= vma->vm_end) 1548a145dd41SLinus Torvalds return -EFAULT; 1549a145dd41SLinus Torvalds if (!page_count(page)) 1550a145dd41SLinus Torvalds return -EINVAL; 15514d7672b4SLinus Torvalds vma->vm_flags |= VM_INSERTPAGE; 1552423bad60SNick Piggin return insert_page(vma, addr, page, vma->vm_page_prot); 1553a145dd41SLinus Torvalds } 1554e3c3374fSLinus Torvalds EXPORT_SYMBOL(vm_insert_page); 1555a145dd41SLinus Torvalds 1556423bad60SNick Piggin static int insert_pfn(struct vm_area_struct *vma, unsigned long addr, 1557423bad60SNick Piggin unsigned long pfn, pgprot_t prot) 1558423bad60SNick Piggin { 1559423bad60SNick Piggin struct mm_struct *mm = vma->vm_mm; 1560423bad60SNick Piggin int retval; 1561423bad60SNick Piggin pte_t *pte, entry; 1562423bad60SNick Piggin spinlock_t *ptl; 1563423bad60SNick Piggin 1564423bad60SNick Piggin retval = -ENOMEM; 1565423bad60SNick Piggin pte = get_locked_pte(mm, addr, &ptl); 1566423bad60SNick Piggin if (!pte) 1567423bad60SNick Piggin goto out; 1568423bad60SNick Piggin retval = -EBUSY; 1569423bad60SNick Piggin if (!pte_none(*pte)) 1570423bad60SNick Piggin goto out_unlock; 1571423bad60SNick Piggin 1572423bad60SNick Piggin /* Ok, finally just insert the thing.. */ 1573423bad60SNick Piggin entry = pte_mkspecial(pfn_pte(pfn, prot)); 1574423bad60SNick Piggin set_pte_at(mm, addr, pte, entry); 1575423bad60SNick Piggin update_mmu_cache(vma, addr, entry); /* XXX: why not for insert_page? */ 1576423bad60SNick Piggin 1577423bad60SNick Piggin retval = 0; 1578423bad60SNick Piggin out_unlock: 1579423bad60SNick Piggin pte_unmap_unlock(pte, ptl); 1580423bad60SNick Piggin out: 1581423bad60SNick Piggin return retval; 1582423bad60SNick Piggin } 1583423bad60SNick Piggin 1584e0dc0d8fSNick Piggin /** 1585e0dc0d8fSNick Piggin * vm_insert_pfn - insert single pfn into user vma 1586e0dc0d8fSNick Piggin * @vma: user vma to map to 1587e0dc0d8fSNick Piggin * @addr: target user address of this page 1588e0dc0d8fSNick Piggin * @pfn: source kernel pfn 1589e0dc0d8fSNick Piggin * 1590e0dc0d8fSNick Piggin * Similar to vm_inert_page, this allows drivers to insert individual pages 1591e0dc0d8fSNick Piggin * they've allocated into a user vma. Same comments apply. 1592e0dc0d8fSNick Piggin * 1593e0dc0d8fSNick Piggin * This function should only be called from a vm_ops->fault handler, and 1594e0dc0d8fSNick Piggin * in that case the handler should return NULL. 15950d71d10aSNick Piggin * 15960d71d10aSNick Piggin * vma cannot be a COW mapping. 15970d71d10aSNick Piggin * 15980d71d10aSNick Piggin * As this is called only for pages that do not currently exist, we 15990d71d10aSNick Piggin * do not need to flush old virtual caches or the TLB. 1600e0dc0d8fSNick Piggin */ 1601e0dc0d8fSNick Piggin int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr, 1602e0dc0d8fSNick Piggin unsigned long pfn) 1603e0dc0d8fSNick Piggin { 16042ab64037Svenkatesh.pallipadi@intel.com int ret; 1605e4b866edSvenkatesh.pallipadi@intel.com pgprot_t pgprot = vma->vm_page_prot; 16067e675137SNick Piggin /* 16077e675137SNick Piggin * Technically, architectures with pte_special can avoid all these 16087e675137SNick Piggin * restrictions (same for remap_pfn_range). However we would like 16097e675137SNick Piggin * consistency in testing and feature parity among all, so we should 16107e675137SNick Piggin * try to keep these invariants in place for everybody. 16117e675137SNick Piggin */ 1612b379d790SJared Hulbert BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))); 1613b379d790SJared Hulbert BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) == 1614b379d790SJared Hulbert (VM_PFNMAP|VM_MIXEDMAP)); 1615b379d790SJared Hulbert BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); 1616b379d790SJared Hulbert BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn)); 1617e0dc0d8fSNick Piggin 1618423bad60SNick Piggin if (addr < vma->vm_start || addr >= vma->vm_end) 1619423bad60SNick Piggin return -EFAULT; 1620e4b866edSvenkatesh.pallipadi@intel.com if (track_pfn_vma_new(vma, &pgprot, pfn, PAGE_SIZE)) 16212ab64037Svenkatesh.pallipadi@intel.com return -EINVAL; 16222ab64037Svenkatesh.pallipadi@intel.com 1623e4b866edSvenkatesh.pallipadi@intel.com ret = insert_pfn(vma, addr, pfn, pgprot); 16242ab64037Svenkatesh.pallipadi@intel.com 16252ab64037Svenkatesh.pallipadi@intel.com if (ret) 16262ab64037Svenkatesh.pallipadi@intel.com untrack_pfn_vma(vma, pfn, PAGE_SIZE); 16272ab64037Svenkatesh.pallipadi@intel.com 16282ab64037Svenkatesh.pallipadi@intel.com return ret; 1629e0dc0d8fSNick Piggin } 1630e0dc0d8fSNick Piggin EXPORT_SYMBOL(vm_insert_pfn); 1631e0dc0d8fSNick Piggin 1632423bad60SNick Piggin int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr, 1633423bad60SNick Piggin unsigned long pfn) 1634423bad60SNick Piggin { 1635423bad60SNick Piggin BUG_ON(!(vma->vm_flags & VM_MIXEDMAP)); 1636423bad60SNick Piggin 1637423bad60SNick Piggin if (addr < vma->vm_start || addr >= vma->vm_end) 1638423bad60SNick Piggin return -EFAULT; 1639423bad60SNick Piggin 1640423bad60SNick Piggin /* 1641423bad60SNick Piggin * If we don't have pte special, then we have to use the pfn_valid() 1642423bad60SNick Piggin * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must* 1643423bad60SNick Piggin * refcount the page if pfn_valid is true (hence insert_page rather 1644*62eede62SHugh Dickins * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP 1645*62eede62SHugh Dickins * without pte special, it would there be refcounted as a normal page. 1646423bad60SNick Piggin */ 1647423bad60SNick Piggin if (!HAVE_PTE_SPECIAL && pfn_valid(pfn)) { 1648423bad60SNick Piggin struct page *page; 1649423bad60SNick Piggin 1650423bad60SNick Piggin page = pfn_to_page(pfn); 1651423bad60SNick Piggin return insert_page(vma, addr, page, vma->vm_page_prot); 1652423bad60SNick Piggin } 1653423bad60SNick Piggin return insert_pfn(vma, addr, pfn, vma->vm_page_prot); 1654423bad60SNick Piggin } 1655423bad60SNick Piggin EXPORT_SYMBOL(vm_insert_mixed); 1656423bad60SNick Piggin 1657a145dd41SLinus Torvalds /* 16581da177e4SLinus Torvalds * maps a range of physical memory into the requested pages. the old 16591da177e4SLinus Torvalds * mappings are removed. any references to nonexistent pages results 16601da177e4SLinus Torvalds * in null mappings (currently treated as "copy-on-access") 16611da177e4SLinus Torvalds */ 16621da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd, 16631da177e4SLinus Torvalds unsigned long addr, unsigned long end, 16641da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 16651da177e4SLinus Torvalds { 16661da177e4SLinus Torvalds pte_t *pte; 1667c74df32cSHugh Dickins spinlock_t *ptl; 16681da177e4SLinus Torvalds 1669c74df32cSHugh Dickins pte = pte_alloc_map_lock(mm, pmd, addr, &ptl); 16701da177e4SLinus Torvalds if (!pte) 16711da177e4SLinus Torvalds return -ENOMEM; 16726606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 16731da177e4SLinus Torvalds do { 16741da177e4SLinus Torvalds BUG_ON(!pte_none(*pte)); 16757e675137SNick Piggin set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot))); 16761da177e4SLinus Torvalds pfn++; 16771da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 16786606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 1679c74df32cSHugh Dickins pte_unmap_unlock(pte - 1, ptl); 16801da177e4SLinus Torvalds return 0; 16811da177e4SLinus Torvalds } 16821da177e4SLinus Torvalds 16831da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud, 16841da177e4SLinus Torvalds unsigned long addr, unsigned long end, 16851da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 16861da177e4SLinus Torvalds { 16871da177e4SLinus Torvalds pmd_t *pmd; 16881da177e4SLinus Torvalds unsigned long next; 16891da177e4SLinus Torvalds 16901da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 16911da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, addr); 16921da177e4SLinus Torvalds if (!pmd) 16931da177e4SLinus Torvalds return -ENOMEM; 16941da177e4SLinus Torvalds do { 16951da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 16961da177e4SLinus Torvalds if (remap_pte_range(mm, pmd, addr, next, 16971da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot)) 16981da177e4SLinus Torvalds return -ENOMEM; 16991da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 17001da177e4SLinus Torvalds return 0; 17011da177e4SLinus Torvalds } 17021da177e4SLinus Torvalds 17031da177e4SLinus Torvalds static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd, 17041da177e4SLinus Torvalds unsigned long addr, unsigned long end, 17051da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 17061da177e4SLinus Torvalds { 17071da177e4SLinus Torvalds pud_t *pud; 17081da177e4SLinus Torvalds unsigned long next; 17091da177e4SLinus Torvalds 17101da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 17111da177e4SLinus Torvalds pud = pud_alloc(mm, pgd, addr); 17121da177e4SLinus Torvalds if (!pud) 17131da177e4SLinus Torvalds return -ENOMEM; 17141da177e4SLinus Torvalds do { 17151da177e4SLinus Torvalds next = pud_addr_end(addr, end); 17161da177e4SLinus Torvalds if (remap_pmd_range(mm, pud, addr, next, 17171da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot)) 17181da177e4SLinus Torvalds return -ENOMEM; 17191da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 17201da177e4SLinus Torvalds return 0; 17211da177e4SLinus Torvalds } 17221da177e4SLinus Torvalds 1723bfa5bf6dSRolf Eike Beer /** 1724bfa5bf6dSRolf Eike Beer * remap_pfn_range - remap kernel memory to userspace 1725bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 1726bfa5bf6dSRolf Eike Beer * @addr: target user address to start at 1727bfa5bf6dSRolf Eike Beer * @pfn: physical address of kernel memory 1728bfa5bf6dSRolf Eike Beer * @size: size of map area 1729bfa5bf6dSRolf Eike Beer * @prot: page protection flags for this mapping 1730bfa5bf6dSRolf Eike Beer * 1731bfa5bf6dSRolf Eike Beer * Note: this is only safe if the mm semaphore is held when called. 1732bfa5bf6dSRolf Eike Beer */ 17331da177e4SLinus Torvalds int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 17341da177e4SLinus Torvalds unsigned long pfn, unsigned long size, pgprot_t prot) 17351da177e4SLinus Torvalds { 17361da177e4SLinus Torvalds pgd_t *pgd; 17371da177e4SLinus Torvalds unsigned long next; 17382d15cab8SHugh Dickins unsigned long end = addr + PAGE_ALIGN(size); 17391da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 17401da177e4SLinus Torvalds int err; 17411da177e4SLinus Torvalds 17421da177e4SLinus Torvalds /* 17431da177e4SLinus Torvalds * Physically remapped pages are special. Tell the 17441da177e4SLinus Torvalds * rest of the world about it: 17451da177e4SLinus Torvalds * VM_IO tells people not to look at these pages 17461da177e4SLinus Torvalds * (accesses can have side effects). 17470b14c179SHugh Dickins * VM_RESERVED is specified all over the place, because 17480b14c179SHugh Dickins * in 2.4 it kept swapout's vma scan off this vma; but 17490b14c179SHugh Dickins * in 2.6 the LRU scan won't even find its pages, so this 17500b14c179SHugh Dickins * flag means no more than count its pages in reserved_vm, 17510b14c179SHugh Dickins * and omit it from core dump, even when VM_IO turned off. 17526aab341eSLinus Torvalds * VM_PFNMAP tells the core MM that the base pages are just 17536aab341eSLinus Torvalds * raw PFN mappings, and do not have a "struct page" associated 17546aab341eSLinus Torvalds * with them. 1755fb155c16SLinus Torvalds * 1756fb155c16SLinus Torvalds * There's a horrible special case to handle copy-on-write 1757fb155c16SLinus Torvalds * behaviour that some programs depend on. We mark the "original" 1758fb155c16SLinus Torvalds * un-COW'ed pages by matching them up with "vma->vm_pgoff". 17591da177e4SLinus Torvalds */ 17604bb9c5c0SPallipadi, Venkatesh if (addr == vma->vm_start && end == vma->vm_end) { 17616aab341eSLinus Torvalds vma->vm_pgoff = pfn; 1762895791daSPallipadi, Venkatesh vma->vm_flags |= VM_PFN_AT_MMAP; 17634bb9c5c0SPallipadi, Venkatesh } else if (is_cow_mapping(vma->vm_flags)) 17643c8bb73aSvenkatesh.pallipadi@intel.com return -EINVAL; 1765fb155c16SLinus Torvalds 1766fb155c16SLinus Torvalds vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP; 17671da177e4SLinus Torvalds 1768e4b866edSvenkatesh.pallipadi@intel.com err = track_pfn_vma_new(vma, &prot, pfn, PAGE_ALIGN(size)); 1769a3670613Svenkatesh.pallipadi@intel.com if (err) { 1770a3670613Svenkatesh.pallipadi@intel.com /* 1771a3670613Svenkatesh.pallipadi@intel.com * To indicate that track_pfn related cleanup is not 1772a3670613Svenkatesh.pallipadi@intel.com * needed from higher level routine calling unmap_vmas 1773a3670613Svenkatesh.pallipadi@intel.com */ 1774a3670613Svenkatesh.pallipadi@intel.com vma->vm_flags &= ~(VM_IO | VM_RESERVED | VM_PFNMAP); 1775895791daSPallipadi, Venkatesh vma->vm_flags &= ~VM_PFN_AT_MMAP; 17762ab64037Svenkatesh.pallipadi@intel.com return -EINVAL; 1777a3670613Svenkatesh.pallipadi@intel.com } 17782ab64037Svenkatesh.pallipadi@intel.com 17791da177e4SLinus Torvalds BUG_ON(addr >= end); 17801da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 17811da177e4SLinus Torvalds pgd = pgd_offset(mm, addr); 17821da177e4SLinus Torvalds flush_cache_range(vma, addr, end); 17831da177e4SLinus Torvalds do { 17841da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 17851da177e4SLinus Torvalds err = remap_pud_range(mm, pgd, addr, next, 17861da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot); 17871da177e4SLinus Torvalds if (err) 17881da177e4SLinus Torvalds break; 17891da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 17902ab64037Svenkatesh.pallipadi@intel.com 17912ab64037Svenkatesh.pallipadi@intel.com if (err) 17922ab64037Svenkatesh.pallipadi@intel.com untrack_pfn_vma(vma, pfn, PAGE_ALIGN(size)); 17932ab64037Svenkatesh.pallipadi@intel.com 17941da177e4SLinus Torvalds return err; 17951da177e4SLinus Torvalds } 17961da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range); 17971da177e4SLinus Torvalds 1798aee16b3cSJeremy Fitzhardinge static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd, 1799aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 1800aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 1801aee16b3cSJeremy Fitzhardinge { 1802aee16b3cSJeremy Fitzhardinge pte_t *pte; 1803aee16b3cSJeremy Fitzhardinge int err; 18042f569afdSMartin Schwidefsky pgtable_t token; 180594909914SBorislav Petkov spinlock_t *uninitialized_var(ptl); 1806aee16b3cSJeremy Fitzhardinge 1807aee16b3cSJeremy Fitzhardinge pte = (mm == &init_mm) ? 1808aee16b3cSJeremy Fitzhardinge pte_alloc_kernel(pmd, addr) : 1809aee16b3cSJeremy Fitzhardinge pte_alloc_map_lock(mm, pmd, addr, &ptl); 1810aee16b3cSJeremy Fitzhardinge if (!pte) 1811aee16b3cSJeremy Fitzhardinge return -ENOMEM; 1812aee16b3cSJeremy Fitzhardinge 1813aee16b3cSJeremy Fitzhardinge BUG_ON(pmd_huge(*pmd)); 1814aee16b3cSJeremy Fitzhardinge 181538e0edb1SJeremy Fitzhardinge arch_enter_lazy_mmu_mode(); 181638e0edb1SJeremy Fitzhardinge 18172f569afdSMartin Schwidefsky token = pmd_pgtable(*pmd); 1818aee16b3cSJeremy Fitzhardinge 1819aee16b3cSJeremy Fitzhardinge do { 18202f569afdSMartin Schwidefsky err = fn(pte, token, addr, data); 1821aee16b3cSJeremy Fitzhardinge if (err) 1822aee16b3cSJeremy Fitzhardinge break; 1823aee16b3cSJeremy Fitzhardinge } while (pte++, addr += PAGE_SIZE, addr != end); 1824aee16b3cSJeremy Fitzhardinge 182538e0edb1SJeremy Fitzhardinge arch_leave_lazy_mmu_mode(); 182638e0edb1SJeremy Fitzhardinge 1827aee16b3cSJeremy Fitzhardinge if (mm != &init_mm) 1828aee16b3cSJeremy Fitzhardinge pte_unmap_unlock(pte-1, ptl); 1829aee16b3cSJeremy Fitzhardinge return err; 1830aee16b3cSJeremy Fitzhardinge } 1831aee16b3cSJeremy Fitzhardinge 1832aee16b3cSJeremy Fitzhardinge static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud, 1833aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 1834aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 1835aee16b3cSJeremy Fitzhardinge { 1836aee16b3cSJeremy Fitzhardinge pmd_t *pmd; 1837aee16b3cSJeremy Fitzhardinge unsigned long next; 1838aee16b3cSJeremy Fitzhardinge int err; 1839aee16b3cSJeremy Fitzhardinge 1840ceb86879SAndi Kleen BUG_ON(pud_huge(*pud)); 1841ceb86879SAndi Kleen 1842aee16b3cSJeremy Fitzhardinge pmd = pmd_alloc(mm, pud, addr); 1843aee16b3cSJeremy Fitzhardinge if (!pmd) 1844aee16b3cSJeremy Fitzhardinge return -ENOMEM; 1845aee16b3cSJeremy Fitzhardinge do { 1846aee16b3cSJeremy Fitzhardinge next = pmd_addr_end(addr, end); 1847aee16b3cSJeremy Fitzhardinge err = apply_to_pte_range(mm, pmd, addr, next, fn, data); 1848aee16b3cSJeremy Fitzhardinge if (err) 1849aee16b3cSJeremy Fitzhardinge break; 1850aee16b3cSJeremy Fitzhardinge } while (pmd++, addr = next, addr != end); 1851aee16b3cSJeremy Fitzhardinge return err; 1852aee16b3cSJeremy Fitzhardinge } 1853aee16b3cSJeremy Fitzhardinge 1854aee16b3cSJeremy Fitzhardinge static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd, 1855aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 1856aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 1857aee16b3cSJeremy Fitzhardinge { 1858aee16b3cSJeremy Fitzhardinge pud_t *pud; 1859aee16b3cSJeremy Fitzhardinge unsigned long next; 1860aee16b3cSJeremy Fitzhardinge int err; 1861aee16b3cSJeremy Fitzhardinge 1862aee16b3cSJeremy Fitzhardinge pud = pud_alloc(mm, pgd, addr); 1863aee16b3cSJeremy Fitzhardinge if (!pud) 1864aee16b3cSJeremy Fitzhardinge return -ENOMEM; 1865aee16b3cSJeremy Fitzhardinge do { 1866aee16b3cSJeremy Fitzhardinge next = pud_addr_end(addr, end); 1867aee16b3cSJeremy Fitzhardinge err = apply_to_pmd_range(mm, pud, addr, next, fn, data); 1868aee16b3cSJeremy Fitzhardinge if (err) 1869aee16b3cSJeremy Fitzhardinge break; 1870aee16b3cSJeremy Fitzhardinge } while (pud++, addr = next, addr != end); 1871aee16b3cSJeremy Fitzhardinge return err; 1872aee16b3cSJeremy Fitzhardinge } 1873aee16b3cSJeremy Fitzhardinge 1874aee16b3cSJeremy Fitzhardinge /* 1875aee16b3cSJeremy Fitzhardinge * Scan a region of virtual memory, filling in page tables as necessary 1876aee16b3cSJeremy Fitzhardinge * and calling a provided function on each leaf page table. 1877aee16b3cSJeremy Fitzhardinge */ 1878aee16b3cSJeremy Fitzhardinge int apply_to_page_range(struct mm_struct *mm, unsigned long addr, 1879aee16b3cSJeremy Fitzhardinge unsigned long size, pte_fn_t fn, void *data) 1880aee16b3cSJeremy Fitzhardinge { 1881aee16b3cSJeremy Fitzhardinge pgd_t *pgd; 1882aee16b3cSJeremy Fitzhardinge unsigned long next; 1883cddb8a5cSAndrea Arcangeli unsigned long start = addr, end = addr + size; 1884aee16b3cSJeremy Fitzhardinge int err; 1885aee16b3cSJeremy Fitzhardinge 1886aee16b3cSJeremy Fitzhardinge BUG_ON(addr >= end); 1887cddb8a5cSAndrea Arcangeli mmu_notifier_invalidate_range_start(mm, start, end); 1888aee16b3cSJeremy Fitzhardinge pgd = pgd_offset(mm, addr); 1889aee16b3cSJeremy Fitzhardinge do { 1890aee16b3cSJeremy Fitzhardinge next = pgd_addr_end(addr, end); 1891aee16b3cSJeremy Fitzhardinge err = apply_to_pud_range(mm, pgd, addr, next, fn, data); 1892aee16b3cSJeremy Fitzhardinge if (err) 1893aee16b3cSJeremy Fitzhardinge break; 1894aee16b3cSJeremy Fitzhardinge } while (pgd++, addr = next, addr != end); 1895cddb8a5cSAndrea Arcangeli mmu_notifier_invalidate_range_end(mm, start, end); 1896aee16b3cSJeremy Fitzhardinge return err; 1897aee16b3cSJeremy Fitzhardinge } 1898aee16b3cSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(apply_to_page_range); 1899aee16b3cSJeremy Fitzhardinge 19001da177e4SLinus Torvalds /* 19018f4e2101SHugh Dickins * handle_pte_fault chooses page fault handler according to an entry 19028f4e2101SHugh Dickins * which was read non-atomically. Before making any commitment, on 19038f4e2101SHugh Dickins * those architectures or configurations (e.g. i386 with PAE) which 19048f4e2101SHugh Dickins * might give a mix of unmatched parts, do_swap_page and do_file_page 19058f4e2101SHugh Dickins * must check under lock before unmapping the pte and proceeding 19068f4e2101SHugh Dickins * (but do_wp_page is only called after already making such a check; 19078f4e2101SHugh Dickins * and do_anonymous_page and do_no_page can safely check later on). 19088f4e2101SHugh Dickins */ 19094c21e2f2SHugh Dickins static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd, 19108f4e2101SHugh Dickins pte_t *page_table, pte_t orig_pte) 19118f4e2101SHugh Dickins { 19128f4e2101SHugh Dickins int same = 1; 19138f4e2101SHugh Dickins #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT) 19148f4e2101SHugh Dickins if (sizeof(pte_t) > sizeof(unsigned long)) { 19154c21e2f2SHugh Dickins spinlock_t *ptl = pte_lockptr(mm, pmd); 19164c21e2f2SHugh Dickins spin_lock(ptl); 19178f4e2101SHugh Dickins same = pte_same(*page_table, orig_pte); 19184c21e2f2SHugh Dickins spin_unlock(ptl); 19198f4e2101SHugh Dickins } 19208f4e2101SHugh Dickins #endif 19218f4e2101SHugh Dickins pte_unmap(page_table); 19228f4e2101SHugh Dickins return same; 19238f4e2101SHugh Dickins } 19248f4e2101SHugh Dickins 19258f4e2101SHugh Dickins /* 19261da177e4SLinus Torvalds * Do pte_mkwrite, but only if the vma says VM_WRITE. We do this when 19271da177e4SLinus Torvalds * servicing faults for write access. In the normal case, do always want 19281da177e4SLinus Torvalds * pte_mkwrite. But get_user_pages can cause write faults for mappings 19291da177e4SLinus Torvalds * that do not have writing enabled, when used by access_process_vm. 19301da177e4SLinus Torvalds */ 19311da177e4SLinus Torvalds static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma) 19321da177e4SLinus Torvalds { 19331da177e4SLinus Torvalds if (likely(vma->vm_flags & VM_WRITE)) 19341da177e4SLinus Torvalds pte = pte_mkwrite(pte); 19351da177e4SLinus Torvalds return pte; 19361da177e4SLinus Torvalds } 19371da177e4SLinus Torvalds 19389de455b2SAtsushi Nemoto static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma) 19396aab341eSLinus Torvalds { 19406aab341eSLinus Torvalds /* 19416aab341eSLinus Torvalds * If the source page was a PFN mapping, we don't have 19426aab341eSLinus Torvalds * a "struct page" for it. We do a best-effort copy by 19436aab341eSLinus Torvalds * just copying from the original user address. If that 19446aab341eSLinus Torvalds * fails, we just zero-fill it. Live with it. 19456aab341eSLinus Torvalds */ 19466aab341eSLinus Torvalds if (unlikely(!src)) { 19476aab341eSLinus Torvalds void *kaddr = kmap_atomic(dst, KM_USER0); 19485d2a2dbbSLinus Torvalds void __user *uaddr = (void __user *)(va & PAGE_MASK); 19495d2a2dbbSLinus Torvalds 19505d2a2dbbSLinus Torvalds /* 19515d2a2dbbSLinus Torvalds * This really shouldn't fail, because the page is there 19525d2a2dbbSLinus Torvalds * in the page tables. But it might just be unreadable, 19535d2a2dbbSLinus Torvalds * in which case we just give up and fill the result with 19545d2a2dbbSLinus Torvalds * zeroes. 19555d2a2dbbSLinus Torvalds */ 19565d2a2dbbSLinus Torvalds if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) 19576aab341eSLinus Torvalds memset(kaddr, 0, PAGE_SIZE); 19586aab341eSLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 1959c4ec7b0dSDmitriy Monakhov flush_dcache_page(dst); 19600ed361deSNick Piggin } else 19619de455b2SAtsushi Nemoto copy_user_highpage(dst, src, va, vma); 19626aab341eSLinus Torvalds } 19636aab341eSLinus Torvalds 19641da177e4SLinus Torvalds /* 19651da177e4SLinus Torvalds * This routine handles present pages, when users try to write 19661da177e4SLinus Torvalds * to a shared page. It is done by copying the page to a new address 19671da177e4SLinus Torvalds * and decrementing the shared-page counter for the old page. 19681da177e4SLinus Torvalds * 19691da177e4SLinus Torvalds * Note that this routine assumes that the protection checks have been 19701da177e4SLinus Torvalds * done by the caller (the low-level page fault routine in most cases). 19711da177e4SLinus Torvalds * Thus we can safely just mark it writable once we've done any necessary 19721da177e4SLinus Torvalds * COW. 19731da177e4SLinus Torvalds * 19741da177e4SLinus Torvalds * We also mark the page dirty at this point even though the page will 19751da177e4SLinus Torvalds * change only once the write actually happens. This avoids a few races, 19761da177e4SLinus Torvalds * and potentially makes it more efficient. 19771da177e4SLinus Torvalds * 19788f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 19798f4e2101SHugh Dickins * but allow concurrent faults), with pte both mapped and locked. 19808f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 19811da177e4SLinus Torvalds */ 19821da177e4SLinus Torvalds static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma, 198365500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 19848f4e2101SHugh Dickins spinlock_t *ptl, pte_t orig_pte) 19851da177e4SLinus Torvalds { 1986e5bbe4dfSHugh Dickins struct page *old_page, *new_page; 19871da177e4SLinus Torvalds pte_t entry; 198883c54070SNick Piggin int reuse = 0, ret = 0; 1989a200ee18SPeter Zijlstra int page_mkwrite = 0; 1990d08b3851SPeter Zijlstra struct page *dirty_page = NULL; 19911da177e4SLinus Torvalds 19926aab341eSLinus Torvalds old_page = vm_normal_page(vma, address, orig_pte); 1993251b97f5SPeter Zijlstra if (!old_page) { 1994251b97f5SPeter Zijlstra /* 1995251b97f5SPeter Zijlstra * VM_MIXEDMAP !pfn_valid() case 1996251b97f5SPeter Zijlstra * 1997251b97f5SPeter Zijlstra * We should not cow pages in a shared writeable mapping. 1998251b97f5SPeter Zijlstra * Just mark the pages writable as we can't do any dirty 1999251b97f5SPeter Zijlstra * accounting on raw pfn maps. 2000251b97f5SPeter Zijlstra */ 2001251b97f5SPeter Zijlstra if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 2002251b97f5SPeter Zijlstra (VM_WRITE|VM_SHARED)) 2003251b97f5SPeter Zijlstra goto reuse; 2004920fc356SHugh Dickins goto gotten; 2005251b97f5SPeter Zijlstra } 20061da177e4SLinus Torvalds 2007d08b3851SPeter Zijlstra /* 2008ee6a6457SPeter Zijlstra * Take out anonymous pages first, anonymous shared vmas are 2009ee6a6457SPeter Zijlstra * not dirty accountable. 2010d08b3851SPeter Zijlstra */ 20119a840895SHugh Dickins if (PageAnon(old_page) && !PageKsm(old_page)) { 2012ab967d86SHugh Dickins if (!trylock_page(old_page)) { 2013ab967d86SHugh Dickins page_cache_get(old_page); 2014ab967d86SHugh Dickins pte_unmap_unlock(page_table, ptl); 2015ab967d86SHugh Dickins lock_page(old_page); 2016ab967d86SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, 2017ab967d86SHugh Dickins &ptl); 2018ab967d86SHugh Dickins if (!pte_same(*page_table, orig_pte)) { 2019ab967d86SHugh Dickins unlock_page(old_page); 2020ab967d86SHugh Dickins page_cache_release(old_page); 2021ab967d86SHugh Dickins goto unlock; 2022ab967d86SHugh Dickins } 2023ab967d86SHugh Dickins page_cache_release(old_page); 2024ab967d86SHugh Dickins } 20257b1fe597SHugh Dickins reuse = reuse_swap_page(old_page); 2026ee6a6457SPeter Zijlstra unlock_page(old_page); 2027ee6a6457SPeter Zijlstra } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 2028d08b3851SPeter Zijlstra (VM_WRITE|VM_SHARED))) { 2029ee6a6457SPeter Zijlstra /* 2030ee6a6457SPeter Zijlstra * Only catch write-faults on shared writable pages, 2031ee6a6457SPeter Zijlstra * read-only shared pages can get COWed by 2032ee6a6457SPeter Zijlstra * get_user_pages(.write=1, .force=1). 2033ee6a6457SPeter Zijlstra */ 20349637a5efSDavid Howells if (vma->vm_ops && vma->vm_ops->page_mkwrite) { 2035c2ec175cSNick Piggin struct vm_fault vmf; 2036c2ec175cSNick Piggin int tmp; 2037c2ec175cSNick Piggin 2038c2ec175cSNick Piggin vmf.virtual_address = (void __user *)(address & 2039c2ec175cSNick Piggin PAGE_MASK); 2040c2ec175cSNick Piggin vmf.pgoff = old_page->index; 2041c2ec175cSNick Piggin vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE; 2042c2ec175cSNick Piggin vmf.page = old_page; 2043c2ec175cSNick Piggin 20449637a5efSDavid Howells /* 20459637a5efSDavid Howells * Notify the address space that the page is about to 20469637a5efSDavid Howells * become writable so that it can prohibit this or wait 20479637a5efSDavid Howells * for the page to get into an appropriate state. 20489637a5efSDavid Howells * 20499637a5efSDavid Howells * We do this without the lock held, so that it can 20509637a5efSDavid Howells * sleep if it needs to. 20519637a5efSDavid Howells */ 20529637a5efSDavid Howells page_cache_get(old_page); 20539637a5efSDavid Howells pte_unmap_unlock(page_table, ptl); 20549637a5efSDavid Howells 2055c2ec175cSNick Piggin tmp = vma->vm_ops->page_mkwrite(vma, &vmf); 2056c2ec175cSNick Piggin if (unlikely(tmp & 2057c2ec175cSNick Piggin (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) { 2058c2ec175cSNick Piggin ret = tmp; 20599637a5efSDavid Howells goto unwritable_page; 2060c2ec175cSNick Piggin } 2061b827e496SNick Piggin if (unlikely(!(tmp & VM_FAULT_LOCKED))) { 2062b827e496SNick Piggin lock_page(old_page); 2063b827e496SNick Piggin if (!old_page->mapping) { 2064b827e496SNick Piggin ret = 0; /* retry the fault */ 2065b827e496SNick Piggin unlock_page(old_page); 2066b827e496SNick Piggin goto unwritable_page; 2067b827e496SNick Piggin } 2068b827e496SNick Piggin } else 2069b827e496SNick Piggin VM_BUG_ON(!PageLocked(old_page)); 20709637a5efSDavid Howells 20719637a5efSDavid Howells /* 20729637a5efSDavid Howells * Since we dropped the lock we need to revalidate 20739637a5efSDavid Howells * the PTE as someone else may have changed it. If 20749637a5efSDavid Howells * they did, we just return, as we can count on the 20759637a5efSDavid Howells * MMU to tell us if they didn't also make it writable. 20769637a5efSDavid Howells */ 20779637a5efSDavid Howells page_table = pte_offset_map_lock(mm, pmd, address, 20789637a5efSDavid Howells &ptl); 2079b827e496SNick Piggin if (!pte_same(*page_table, orig_pte)) { 2080b827e496SNick Piggin unlock_page(old_page); 2081c3704cebSHugh Dickins page_cache_release(old_page); 20829637a5efSDavid Howells goto unlock; 2083b827e496SNick Piggin } 2084a200ee18SPeter Zijlstra 2085a200ee18SPeter Zijlstra page_mkwrite = 1; 20869637a5efSDavid Howells } 2087d08b3851SPeter Zijlstra dirty_page = old_page; 2088d08b3851SPeter Zijlstra get_page(dirty_page); 20899637a5efSDavid Howells reuse = 1; 20909637a5efSDavid Howells } 20919637a5efSDavid Howells 20921da177e4SLinus Torvalds if (reuse) { 2093251b97f5SPeter Zijlstra reuse: 2094eca35133SBen Collins flush_cache_page(vma, address, pte_pfn(orig_pte)); 209565500d23SHugh Dickins entry = pte_mkyoung(orig_pte); 209665500d23SHugh Dickins entry = maybe_mkwrite(pte_mkdirty(entry), vma); 2097954ffcb3SKAMEZAWA Hiroyuki if (ptep_set_access_flags(vma, address, page_table, entry,1)) 20981da177e4SLinus Torvalds update_mmu_cache(vma, address, entry); 209965500d23SHugh Dickins ret |= VM_FAULT_WRITE; 210065500d23SHugh Dickins goto unlock; 21011da177e4SLinus Torvalds } 21021da177e4SLinus Torvalds 21031da177e4SLinus Torvalds /* 21041da177e4SLinus Torvalds * Ok, we need to copy. Oh, well.. 21051da177e4SLinus Torvalds */ 21061da177e4SLinus Torvalds page_cache_get(old_page); 2107920fc356SHugh Dickins gotten: 21088f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 21091da177e4SLinus Torvalds 21101da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 211165500d23SHugh Dickins goto oom; 2112a13ea5b7SHugh Dickins 2113*62eede62SHugh Dickins if (is_zero_pfn(pte_pfn(orig_pte))) { 2114a13ea5b7SHugh Dickins new_page = alloc_zeroed_user_highpage_movable(vma, address); 2115a13ea5b7SHugh Dickins if (!new_page) 2116a13ea5b7SHugh Dickins goto oom; 2117a13ea5b7SHugh Dickins } else { 2118769848c0SMel Gorman new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address); 21191da177e4SLinus Torvalds if (!new_page) 212065500d23SHugh Dickins goto oom; 2121a13ea5b7SHugh Dickins cow_user_page(new_page, old_page, address, vma); 2122a13ea5b7SHugh Dickins } 2123a13ea5b7SHugh Dickins __SetPageUptodate(new_page); 2124a13ea5b7SHugh Dickins 2125b291f000SNick Piggin /* 2126b291f000SNick Piggin * Don't let another task, with possibly unlocked vma, 2127b291f000SNick Piggin * keep the mlocked page. 2128b291f000SNick Piggin */ 2129ab92661dSCarsten Otte if ((vma->vm_flags & VM_LOCKED) && old_page) { 2130b291f000SNick Piggin lock_page(old_page); /* for LRU manipulation */ 2131b291f000SNick Piggin clear_page_mlock(old_page); 2132b291f000SNick Piggin unlock_page(old_page); 2133b291f000SNick Piggin } 213465500d23SHugh Dickins 21352c26fdd7SKAMEZAWA Hiroyuki if (mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL)) 21368a9f3ccdSBalbir Singh goto oom_free_new; 21378a9f3ccdSBalbir Singh 21381da177e4SLinus Torvalds /* 21391da177e4SLinus Torvalds * Re-check the pte - we dropped the lock 21401da177e4SLinus Torvalds */ 21418f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 214265500d23SHugh Dickins if (likely(pte_same(*page_table, orig_pte))) { 2143920fc356SHugh Dickins if (old_page) { 21444294621fSHugh Dickins if (!PageAnon(old_page)) { 21454294621fSHugh Dickins dec_mm_counter(mm, file_rss); 2146920fc356SHugh Dickins inc_mm_counter(mm, anon_rss); 21474294621fSHugh Dickins } 2148920fc356SHugh Dickins } else 2149920fc356SHugh Dickins inc_mm_counter(mm, anon_rss); 2150eca35133SBen Collins flush_cache_page(vma, address, pte_pfn(orig_pte)); 215165500d23SHugh Dickins entry = mk_pte(new_page, vma->vm_page_prot); 215265500d23SHugh Dickins entry = maybe_mkwrite(pte_mkdirty(entry), vma); 21534ce072f1SSiddha, Suresh B /* 21544ce072f1SSiddha, Suresh B * Clear the pte entry and flush it first, before updating the 21554ce072f1SSiddha, Suresh B * pte with the new entry. This will avoid a race condition 21564ce072f1SSiddha, Suresh B * seen in the presence of one thread doing SMC and another 21574ce072f1SSiddha, Suresh B * thread doing COW. 21584ce072f1SSiddha, Suresh B */ 2159828502d3SIzik Eidus ptep_clear_flush(vma, address, page_table); 21609617d95eSNick Piggin page_add_new_anon_rmap(new_page, vma, address); 2161828502d3SIzik Eidus /* 2162828502d3SIzik Eidus * We call the notify macro here because, when using secondary 2163828502d3SIzik Eidus * mmu page tables (such as kvm shadow page tables), we want the 2164828502d3SIzik Eidus * new page to be mapped directly into the secondary page table. 2165828502d3SIzik Eidus */ 2166828502d3SIzik Eidus set_pte_at_notify(mm, address, page_table, entry); 216764d6519dSLee Schermerhorn update_mmu_cache(vma, address, entry); 2168945754a1SNick Piggin if (old_page) { 2169945754a1SNick Piggin /* 2170945754a1SNick Piggin * Only after switching the pte to the new page may 2171945754a1SNick Piggin * we remove the mapcount here. Otherwise another 2172945754a1SNick Piggin * process may come and find the rmap count decremented 2173945754a1SNick Piggin * before the pte is switched to the new page, and 2174945754a1SNick Piggin * "reuse" the old page writing into it while our pte 2175945754a1SNick Piggin * here still points into it and can be read by other 2176945754a1SNick Piggin * threads. 2177945754a1SNick Piggin * 2178945754a1SNick Piggin * The critical issue is to order this 2179945754a1SNick Piggin * page_remove_rmap with the ptp_clear_flush above. 2180945754a1SNick Piggin * Those stores are ordered by (if nothing else,) 2181945754a1SNick Piggin * the barrier present in the atomic_add_negative 2182945754a1SNick Piggin * in page_remove_rmap. 2183945754a1SNick Piggin * 2184945754a1SNick Piggin * Then the TLB flush in ptep_clear_flush ensures that 2185945754a1SNick Piggin * no process can access the old page before the 2186945754a1SNick Piggin * decremented mapcount is visible. And the old page 2187945754a1SNick Piggin * cannot be reused until after the decremented 2188945754a1SNick Piggin * mapcount is visible. So transitively, TLBs to 2189945754a1SNick Piggin * old page will be flushed before it can be reused. 2190945754a1SNick Piggin */ 2191edc315fdSHugh Dickins page_remove_rmap(old_page); 2192945754a1SNick Piggin } 2193945754a1SNick Piggin 21941da177e4SLinus Torvalds /* Free the old page.. */ 21951da177e4SLinus Torvalds new_page = old_page; 2196f33ea7f4SNick Piggin ret |= VM_FAULT_WRITE; 21978a9f3ccdSBalbir Singh } else 21988a9f3ccdSBalbir Singh mem_cgroup_uncharge_page(new_page); 21998a9f3ccdSBalbir Singh 2200920fc356SHugh Dickins if (new_page) 22011da177e4SLinus Torvalds page_cache_release(new_page); 2202920fc356SHugh Dickins if (old_page) 22031da177e4SLinus Torvalds page_cache_release(old_page); 220465500d23SHugh Dickins unlock: 22058f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 2206d08b3851SPeter Zijlstra if (dirty_page) { 220779352894SNick Piggin /* 220879352894SNick Piggin * Yes, Virginia, this is actually required to prevent a race 220979352894SNick Piggin * with clear_page_dirty_for_io() from clearing the page dirty 221079352894SNick Piggin * bit after it clear all dirty ptes, but before a racing 221179352894SNick Piggin * do_wp_page installs a dirty pte. 221279352894SNick Piggin * 221379352894SNick Piggin * do_no_page is protected similarly. 221479352894SNick Piggin */ 2215b827e496SNick Piggin if (!page_mkwrite) { 221679352894SNick Piggin wait_on_page_locked(dirty_page); 2217a200ee18SPeter Zijlstra set_page_dirty_balance(dirty_page, page_mkwrite); 2218b827e496SNick Piggin } 2219d08b3851SPeter Zijlstra put_page(dirty_page); 2220b827e496SNick Piggin if (page_mkwrite) { 2221b827e496SNick Piggin struct address_space *mapping = dirty_page->mapping; 2222b827e496SNick Piggin 2223b827e496SNick Piggin set_page_dirty(dirty_page); 2224b827e496SNick Piggin unlock_page(dirty_page); 2225b827e496SNick Piggin page_cache_release(dirty_page); 2226b827e496SNick Piggin if (mapping) { 2227b827e496SNick Piggin /* 2228b827e496SNick Piggin * Some device drivers do not set page.mapping 2229b827e496SNick Piggin * but still dirty their pages 2230b827e496SNick Piggin */ 2231b827e496SNick Piggin balance_dirty_pages_ratelimited(mapping); 2232b827e496SNick Piggin } 2233b827e496SNick Piggin } 2234b827e496SNick Piggin 2235b827e496SNick Piggin /* file_update_time outside page_lock */ 2236b827e496SNick Piggin if (vma->vm_file) 2237b827e496SNick Piggin file_update_time(vma->vm_file); 2238d08b3851SPeter Zijlstra } 2239f33ea7f4SNick Piggin return ret; 22408a9f3ccdSBalbir Singh oom_free_new: 22416dbf6d3bSHugh Dickins page_cache_release(new_page); 224265500d23SHugh Dickins oom: 2243b827e496SNick Piggin if (old_page) { 2244b827e496SNick Piggin if (page_mkwrite) { 2245b827e496SNick Piggin unlock_page(old_page); 22461da177e4SLinus Torvalds page_cache_release(old_page); 2247b827e496SNick Piggin } 2248b827e496SNick Piggin page_cache_release(old_page); 2249b827e496SNick Piggin } 22501da177e4SLinus Torvalds return VM_FAULT_OOM; 22519637a5efSDavid Howells 22529637a5efSDavid Howells unwritable_page: 22539637a5efSDavid Howells page_cache_release(old_page); 2254c2ec175cSNick Piggin return ret; 22551da177e4SLinus Torvalds } 22561da177e4SLinus Torvalds 22571da177e4SLinus Torvalds /* 22581da177e4SLinus Torvalds * Helper functions for unmap_mapping_range(). 22591da177e4SLinus Torvalds * 22601da177e4SLinus Torvalds * __ Notes on dropping i_mmap_lock to reduce latency while unmapping __ 22611da177e4SLinus Torvalds * 22621da177e4SLinus Torvalds * We have to restart searching the prio_tree whenever we drop the lock, 22631da177e4SLinus Torvalds * since the iterator is only valid while the lock is held, and anyway 22641da177e4SLinus Torvalds * a later vma might be split and reinserted earlier while lock dropped. 22651da177e4SLinus Torvalds * 22661da177e4SLinus Torvalds * The list of nonlinear vmas could be handled more efficiently, using 22671da177e4SLinus Torvalds * a placeholder, but handle it in the same way until a need is shown. 22681da177e4SLinus Torvalds * It is important to search the prio_tree before nonlinear list: a vma 22691da177e4SLinus Torvalds * may become nonlinear and be shifted from prio_tree to nonlinear list 22701da177e4SLinus Torvalds * while the lock is dropped; but never shifted from list to prio_tree. 22711da177e4SLinus Torvalds * 22721da177e4SLinus Torvalds * In order to make forward progress despite restarting the search, 22731da177e4SLinus Torvalds * vm_truncate_count is used to mark a vma as now dealt with, so we can 22741da177e4SLinus Torvalds * quickly skip it next time around. Since the prio_tree search only 22751da177e4SLinus Torvalds * shows us those vmas affected by unmapping the range in question, we 22761da177e4SLinus Torvalds * can't efficiently keep all vmas in step with mapping->truncate_count: 22771da177e4SLinus Torvalds * so instead reset them all whenever it wraps back to 0 (then go to 1). 22781da177e4SLinus Torvalds * mapping->truncate_count and vma->vm_truncate_count are protected by 22791da177e4SLinus Torvalds * i_mmap_lock. 22801da177e4SLinus Torvalds * 22811da177e4SLinus Torvalds * In order to make forward progress despite repeatedly restarting some 2282ee39b37bSHugh Dickins * large vma, note the restart_addr from unmap_vmas when it breaks out: 22831da177e4SLinus Torvalds * and restart from that address when we reach that vma again. It might 22841da177e4SLinus Torvalds * have been split or merged, shrunk or extended, but never shifted: so 22851da177e4SLinus Torvalds * restart_addr remains valid so long as it remains in the vma's range. 22861da177e4SLinus Torvalds * unmap_mapping_range forces truncate_count to leap over page-aligned 22871da177e4SLinus Torvalds * values so we can save vma's restart_addr in its truncate_count field. 22881da177e4SLinus Torvalds */ 22891da177e4SLinus Torvalds #define is_restart_addr(truncate_count) (!((truncate_count) & ~PAGE_MASK)) 22901da177e4SLinus Torvalds 22911da177e4SLinus Torvalds static void reset_vma_truncate_counts(struct address_space *mapping) 22921da177e4SLinus Torvalds { 22931da177e4SLinus Torvalds struct vm_area_struct *vma; 22941da177e4SLinus Torvalds struct prio_tree_iter iter; 22951da177e4SLinus Torvalds 22961da177e4SLinus Torvalds vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, ULONG_MAX) 22971da177e4SLinus Torvalds vma->vm_truncate_count = 0; 22981da177e4SLinus Torvalds list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list) 22991da177e4SLinus Torvalds vma->vm_truncate_count = 0; 23001da177e4SLinus Torvalds } 23011da177e4SLinus Torvalds 23021da177e4SLinus Torvalds static int unmap_mapping_range_vma(struct vm_area_struct *vma, 23031da177e4SLinus Torvalds unsigned long start_addr, unsigned long end_addr, 23041da177e4SLinus Torvalds struct zap_details *details) 23051da177e4SLinus Torvalds { 23061da177e4SLinus Torvalds unsigned long restart_addr; 23071da177e4SLinus Torvalds int need_break; 23081da177e4SLinus Torvalds 2309d00806b1SNick Piggin /* 2310d00806b1SNick Piggin * files that support invalidating or truncating portions of the 2311d0217ac0SNick Piggin * file from under mmaped areas must have their ->fault function 231283c54070SNick Piggin * return a locked page (and set VM_FAULT_LOCKED in the return). 231383c54070SNick Piggin * This provides synchronisation against concurrent unmapping here. 2314d00806b1SNick Piggin */ 2315d00806b1SNick Piggin 23161da177e4SLinus Torvalds again: 23171da177e4SLinus Torvalds restart_addr = vma->vm_truncate_count; 23181da177e4SLinus Torvalds if (is_restart_addr(restart_addr) && start_addr < restart_addr) { 23191da177e4SLinus Torvalds start_addr = restart_addr; 23201da177e4SLinus Torvalds if (start_addr >= end_addr) { 23211da177e4SLinus Torvalds /* Top of vma has been split off since last time */ 23221da177e4SLinus Torvalds vma->vm_truncate_count = details->truncate_count; 23231da177e4SLinus Torvalds return 0; 23241da177e4SLinus Torvalds } 23251da177e4SLinus Torvalds } 23261da177e4SLinus Torvalds 2327ee39b37bSHugh Dickins restart_addr = zap_page_range(vma, start_addr, 2328ee39b37bSHugh Dickins end_addr - start_addr, details); 232995c354feSNick Piggin need_break = need_resched() || spin_needbreak(details->i_mmap_lock); 23301da177e4SLinus Torvalds 2331ee39b37bSHugh Dickins if (restart_addr >= end_addr) { 23321da177e4SLinus Torvalds /* We have now completed this vma: mark it so */ 23331da177e4SLinus Torvalds vma->vm_truncate_count = details->truncate_count; 23341da177e4SLinus Torvalds if (!need_break) 23351da177e4SLinus Torvalds return 0; 23361da177e4SLinus Torvalds } else { 23371da177e4SLinus Torvalds /* Note restart_addr in vma's truncate_count field */ 2338ee39b37bSHugh Dickins vma->vm_truncate_count = restart_addr; 23391da177e4SLinus Torvalds if (!need_break) 23401da177e4SLinus Torvalds goto again; 23411da177e4SLinus Torvalds } 23421da177e4SLinus Torvalds 23431da177e4SLinus Torvalds spin_unlock(details->i_mmap_lock); 23441da177e4SLinus Torvalds cond_resched(); 23451da177e4SLinus Torvalds spin_lock(details->i_mmap_lock); 23461da177e4SLinus Torvalds return -EINTR; 23471da177e4SLinus Torvalds } 23481da177e4SLinus Torvalds 23491da177e4SLinus Torvalds static inline void unmap_mapping_range_tree(struct prio_tree_root *root, 23501da177e4SLinus Torvalds struct zap_details *details) 23511da177e4SLinus Torvalds { 23521da177e4SLinus Torvalds struct vm_area_struct *vma; 23531da177e4SLinus Torvalds struct prio_tree_iter iter; 23541da177e4SLinus Torvalds pgoff_t vba, vea, zba, zea; 23551da177e4SLinus Torvalds 23561da177e4SLinus Torvalds restart: 23571da177e4SLinus Torvalds vma_prio_tree_foreach(vma, &iter, root, 23581da177e4SLinus Torvalds details->first_index, details->last_index) { 23591da177e4SLinus Torvalds /* Skip quickly over those we have already dealt with */ 23601da177e4SLinus Torvalds if (vma->vm_truncate_count == details->truncate_count) 23611da177e4SLinus Torvalds continue; 23621da177e4SLinus Torvalds 23631da177e4SLinus Torvalds vba = vma->vm_pgoff; 23641da177e4SLinus Torvalds vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1; 23651da177e4SLinus Torvalds /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */ 23661da177e4SLinus Torvalds zba = details->first_index; 23671da177e4SLinus Torvalds if (zba < vba) 23681da177e4SLinus Torvalds zba = vba; 23691da177e4SLinus Torvalds zea = details->last_index; 23701da177e4SLinus Torvalds if (zea > vea) 23711da177e4SLinus Torvalds zea = vea; 23721da177e4SLinus Torvalds 23731da177e4SLinus Torvalds if (unmap_mapping_range_vma(vma, 23741da177e4SLinus Torvalds ((zba - vba) << PAGE_SHIFT) + vma->vm_start, 23751da177e4SLinus Torvalds ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start, 23761da177e4SLinus Torvalds details) < 0) 23771da177e4SLinus Torvalds goto restart; 23781da177e4SLinus Torvalds } 23791da177e4SLinus Torvalds } 23801da177e4SLinus Torvalds 23811da177e4SLinus Torvalds static inline void unmap_mapping_range_list(struct list_head *head, 23821da177e4SLinus Torvalds struct zap_details *details) 23831da177e4SLinus Torvalds { 23841da177e4SLinus Torvalds struct vm_area_struct *vma; 23851da177e4SLinus Torvalds 23861da177e4SLinus Torvalds /* 23871da177e4SLinus Torvalds * In nonlinear VMAs there is no correspondence between virtual address 23881da177e4SLinus Torvalds * offset and file offset. So we must perform an exhaustive search 23891da177e4SLinus Torvalds * across *all* the pages in each nonlinear VMA, not just the pages 23901da177e4SLinus Torvalds * whose virtual address lies outside the file truncation point. 23911da177e4SLinus Torvalds */ 23921da177e4SLinus Torvalds restart: 23931da177e4SLinus Torvalds list_for_each_entry(vma, head, shared.vm_set.list) { 23941da177e4SLinus Torvalds /* Skip quickly over those we have already dealt with */ 23951da177e4SLinus Torvalds if (vma->vm_truncate_count == details->truncate_count) 23961da177e4SLinus Torvalds continue; 23971da177e4SLinus Torvalds details->nonlinear_vma = vma; 23981da177e4SLinus Torvalds if (unmap_mapping_range_vma(vma, vma->vm_start, 23991da177e4SLinus Torvalds vma->vm_end, details) < 0) 24001da177e4SLinus Torvalds goto restart; 24011da177e4SLinus Torvalds } 24021da177e4SLinus Torvalds } 24031da177e4SLinus Torvalds 24041da177e4SLinus Torvalds /** 240572fd4a35SRobert 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. 24063d41088fSMartin Waitz * @mapping: the address space containing mmaps to be unmapped. 24071da177e4SLinus Torvalds * @holebegin: byte in first page to unmap, relative to the start of 24081da177e4SLinus Torvalds * the underlying file. This will be rounded down to a PAGE_SIZE 24091da177e4SLinus Torvalds * boundary. Note that this is different from vmtruncate(), which 24101da177e4SLinus Torvalds * must keep the partial page. In contrast, we must get rid of 24111da177e4SLinus Torvalds * partial pages. 24121da177e4SLinus Torvalds * @holelen: size of prospective hole in bytes. This will be rounded 24131da177e4SLinus Torvalds * up to a PAGE_SIZE boundary. A holelen of zero truncates to the 24141da177e4SLinus Torvalds * end of the file. 24151da177e4SLinus Torvalds * @even_cows: 1 when truncating a file, unmap even private COWed pages; 24161da177e4SLinus Torvalds * but 0 when invalidating pagecache, don't throw away private data. 24171da177e4SLinus Torvalds */ 24181da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping, 24191da177e4SLinus Torvalds loff_t const holebegin, loff_t const holelen, int even_cows) 24201da177e4SLinus Torvalds { 24211da177e4SLinus Torvalds struct zap_details details; 24221da177e4SLinus Torvalds pgoff_t hba = holebegin >> PAGE_SHIFT; 24231da177e4SLinus Torvalds pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 24241da177e4SLinus Torvalds 24251da177e4SLinus Torvalds /* Check for overflow. */ 24261da177e4SLinus Torvalds if (sizeof(holelen) > sizeof(hlen)) { 24271da177e4SLinus Torvalds long long holeend = 24281da177e4SLinus Torvalds (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 24291da177e4SLinus Torvalds if (holeend & ~(long long)ULONG_MAX) 24301da177e4SLinus Torvalds hlen = ULONG_MAX - hba + 1; 24311da177e4SLinus Torvalds } 24321da177e4SLinus Torvalds 24331da177e4SLinus Torvalds details.check_mapping = even_cows? NULL: mapping; 24341da177e4SLinus Torvalds details.nonlinear_vma = NULL; 24351da177e4SLinus Torvalds details.first_index = hba; 24361da177e4SLinus Torvalds details.last_index = hba + hlen - 1; 24371da177e4SLinus Torvalds if (details.last_index < details.first_index) 24381da177e4SLinus Torvalds details.last_index = ULONG_MAX; 24391da177e4SLinus Torvalds details.i_mmap_lock = &mapping->i_mmap_lock; 24401da177e4SLinus Torvalds 24411da177e4SLinus Torvalds spin_lock(&mapping->i_mmap_lock); 24421da177e4SLinus Torvalds 2443d00806b1SNick Piggin /* Protect against endless unmapping loops */ 24441da177e4SLinus Torvalds mapping->truncate_count++; 24451da177e4SLinus Torvalds if (unlikely(is_restart_addr(mapping->truncate_count))) { 24461da177e4SLinus Torvalds if (mapping->truncate_count == 0) 24471da177e4SLinus Torvalds reset_vma_truncate_counts(mapping); 24481da177e4SLinus Torvalds mapping->truncate_count++; 24491da177e4SLinus Torvalds } 24501da177e4SLinus Torvalds details.truncate_count = mapping->truncate_count; 24511da177e4SLinus Torvalds 24521da177e4SLinus Torvalds if (unlikely(!prio_tree_empty(&mapping->i_mmap))) 24531da177e4SLinus Torvalds unmap_mapping_range_tree(&mapping->i_mmap, &details); 24541da177e4SLinus Torvalds if (unlikely(!list_empty(&mapping->i_mmap_nonlinear))) 24551da177e4SLinus Torvalds unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details); 24561da177e4SLinus Torvalds spin_unlock(&mapping->i_mmap_lock); 24571da177e4SLinus Torvalds } 24581da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range); 24591da177e4SLinus Torvalds 2460bfa5bf6dSRolf Eike Beer /** 2461bfa5bf6dSRolf Eike Beer * vmtruncate - unmap mappings "freed" by truncate() syscall 2462bfa5bf6dSRolf Eike Beer * @inode: inode of the file used 2463bfa5bf6dSRolf Eike Beer * @offset: file offset to start truncating 24641da177e4SLinus Torvalds * 24651da177e4SLinus Torvalds * NOTE! We have to be ready to update the memory sharing 24661da177e4SLinus Torvalds * between the file and the memory map for a potential last 24671da177e4SLinus Torvalds * incomplete page. Ugly, but necessary. 24681da177e4SLinus Torvalds */ 24691da177e4SLinus Torvalds int vmtruncate(struct inode * inode, loff_t offset) 24701da177e4SLinus Torvalds { 247161d5048fSChristoph Hellwig if (inode->i_size < offset) { 24721da177e4SLinus Torvalds unsigned long limit; 24731da177e4SLinus Torvalds 24741da177e4SLinus Torvalds limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur; 24751da177e4SLinus Torvalds if (limit != RLIM_INFINITY && offset > limit) 24761da177e4SLinus Torvalds goto out_sig; 24771da177e4SLinus Torvalds if (offset > inode->i_sb->s_maxbytes) 24781da177e4SLinus Torvalds goto out_big; 24791da177e4SLinus Torvalds i_size_write(inode, offset); 248061d5048fSChristoph Hellwig } else { 248161d5048fSChristoph Hellwig struct address_space *mapping = inode->i_mapping; 24821da177e4SLinus Torvalds 248361d5048fSChristoph Hellwig /* 248461d5048fSChristoph Hellwig * truncation of in-use swapfiles is disallowed - it would 248561d5048fSChristoph Hellwig * cause subsequent swapout to scribble on the now-freed 248661d5048fSChristoph Hellwig * blocks. 248761d5048fSChristoph Hellwig */ 248861d5048fSChristoph Hellwig if (IS_SWAPFILE(inode)) 248961d5048fSChristoph Hellwig return -ETXTBSY; 249061d5048fSChristoph Hellwig i_size_write(inode, offset); 249161d5048fSChristoph Hellwig 249261d5048fSChristoph Hellwig /* 249361d5048fSChristoph Hellwig * unmap_mapping_range is called twice, first simply for 249461d5048fSChristoph Hellwig * efficiency so that truncate_inode_pages does fewer 249561d5048fSChristoph Hellwig * single-page unmaps. However after this first call, and 249661d5048fSChristoph Hellwig * before truncate_inode_pages finishes, it is possible for 249761d5048fSChristoph Hellwig * private pages to be COWed, which remain after 249861d5048fSChristoph Hellwig * truncate_inode_pages finishes, hence the second 249961d5048fSChristoph Hellwig * unmap_mapping_range call must be made for correctness. 250061d5048fSChristoph Hellwig */ 250161d5048fSChristoph Hellwig unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1); 250261d5048fSChristoph Hellwig truncate_inode_pages(mapping, offset); 250361d5048fSChristoph Hellwig unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1); 250461d5048fSChristoph Hellwig } 250561d5048fSChristoph Hellwig 2506acfa4380SAl Viro if (inode->i_op->truncate) 25071da177e4SLinus Torvalds inode->i_op->truncate(inode); 25081da177e4SLinus Torvalds return 0; 250961d5048fSChristoph Hellwig 25101da177e4SLinus Torvalds out_sig: 25111da177e4SLinus Torvalds send_sig(SIGXFSZ, current, 0); 25121da177e4SLinus Torvalds out_big: 25131da177e4SLinus Torvalds return -EFBIG; 25141da177e4SLinus Torvalds } 25151da177e4SLinus Torvalds EXPORT_SYMBOL(vmtruncate); 25161da177e4SLinus Torvalds 2517f6b3ec23SBadari Pulavarty int vmtruncate_range(struct inode *inode, loff_t offset, loff_t end) 2518f6b3ec23SBadari Pulavarty { 2519f6b3ec23SBadari Pulavarty struct address_space *mapping = inode->i_mapping; 2520f6b3ec23SBadari Pulavarty 2521f6b3ec23SBadari Pulavarty /* 2522f6b3ec23SBadari Pulavarty * If the underlying filesystem is not going to provide 2523f6b3ec23SBadari Pulavarty * a way to truncate a range of blocks (punch a hole) - 2524f6b3ec23SBadari Pulavarty * we should return failure right now. 2525f6b3ec23SBadari Pulavarty */ 2526acfa4380SAl Viro if (!inode->i_op->truncate_range) 2527f6b3ec23SBadari Pulavarty return -ENOSYS; 2528f6b3ec23SBadari Pulavarty 25291b1dcc1bSJes Sorensen mutex_lock(&inode->i_mutex); 2530f6b3ec23SBadari Pulavarty down_write(&inode->i_alloc_sem); 2531f6b3ec23SBadari Pulavarty unmap_mapping_range(mapping, offset, (end - offset), 1); 2532f6b3ec23SBadari Pulavarty truncate_inode_pages_range(mapping, offset, end); 2533d00806b1SNick Piggin unmap_mapping_range(mapping, offset, (end - offset), 1); 2534f6b3ec23SBadari Pulavarty inode->i_op->truncate_range(inode, offset, end); 2535f6b3ec23SBadari Pulavarty up_write(&inode->i_alloc_sem); 25361b1dcc1bSJes Sorensen mutex_unlock(&inode->i_mutex); 2537f6b3ec23SBadari Pulavarty 2538f6b3ec23SBadari Pulavarty return 0; 2539f6b3ec23SBadari Pulavarty } 2540f6b3ec23SBadari Pulavarty 25411da177e4SLinus Torvalds /* 25428f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 25438f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 25448f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 25451da177e4SLinus Torvalds */ 254665500d23SHugh Dickins static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma, 254765500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 254830c9f3a9SLinus Torvalds unsigned int flags, pte_t orig_pte) 25491da177e4SLinus Torvalds { 25508f4e2101SHugh Dickins spinlock_t *ptl; 25511da177e4SLinus Torvalds struct page *page; 255265500d23SHugh Dickins swp_entry_t entry; 25531da177e4SLinus Torvalds pte_t pte; 25547a81b88cSKAMEZAWA Hiroyuki struct mem_cgroup *ptr = NULL; 255583c54070SNick Piggin int ret = 0; 25561da177e4SLinus Torvalds 25574c21e2f2SHugh Dickins if (!pte_unmap_same(mm, pmd, page_table, orig_pte)) 25588f4e2101SHugh Dickins goto out; 255965500d23SHugh Dickins 256065500d23SHugh Dickins entry = pte_to_swp_entry(orig_pte); 25610697212aSChristoph Lameter if (is_migration_entry(entry)) { 25620697212aSChristoph Lameter migration_entry_wait(mm, pmd, address); 25630697212aSChristoph Lameter goto out; 25640697212aSChristoph Lameter } 25650ff92245SShailabh Nagar delayacct_set_flag(DELAYACCT_PF_SWAPIN); 25661da177e4SLinus Torvalds page = lookup_swap_cache(entry); 25671da177e4SLinus Torvalds if (!page) { 2568a5c9b696SHugh Dickins grab_swap_token(mm); /* Contend for token _before_ read-in */ 256902098feaSHugh Dickins page = swapin_readahead(entry, 257002098feaSHugh Dickins GFP_HIGHUSER_MOVABLE, vma, address); 25711da177e4SLinus Torvalds if (!page) { 25721da177e4SLinus Torvalds /* 25738f4e2101SHugh Dickins * Back out if somebody else faulted in this pte 25748f4e2101SHugh Dickins * while we released the pte lock. 25751da177e4SLinus Torvalds */ 25768f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 25771da177e4SLinus Torvalds if (likely(pte_same(*page_table, orig_pte))) 25781da177e4SLinus Torvalds ret = VM_FAULT_OOM; 25790ff92245SShailabh Nagar delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 258065500d23SHugh Dickins goto unlock; 25811da177e4SLinus Torvalds } 25821da177e4SLinus Torvalds 25831da177e4SLinus Torvalds /* Had to read the page from swap area: Major fault */ 25841da177e4SLinus Torvalds ret = VM_FAULT_MAJOR; 2585f8891e5eSChristoph Lameter count_vm_event(PGMAJFAULT); 25861da177e4SLinus Torvalds } 25871da177e4SLinus Torvalds 25881da177e4SLinus Torvalds lock_page(page); 258920a1022dSBalbir Singh delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 25901da177e4SLinus Torvalds 25912c26fdd7SKAMEZAWA Hiroyuki if (mem_cgroup_try_charge_swapin(mm, page, GFP_KERNEL, &ptr)) { 2592073e587eSKAMEZAWA Hiroyuki ret = VM_FAULT_OOM; 2593bc43f75cSJohannes Weiner goto out_page; 2594073e587eSKAMEZAWA Hiroyuki } 2595073e587eSKAMEZAWA Hiroyuki 25961da177e4SLinus Torvalds /* 25978f4e2101SHugh Dickins * Back out if somebody else already faulted in this pte. 25981da177e4SLinus Torvalds */ 25998f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 26009e9bef07SHugh Dickins if (unlikely(!pte_same(*page_table, orig_pte))) 2601b8107480SKirill Korotaev goto out_nomap; 2602b8107480SKirill Korotaev 2603b8107480SKirill Korotaev if (unlikely(!PageUptodate(page))) { 2604b8107480SKirill Korotaev ret = VM_FAULT_SIGBUS; 2605b8107480SKirill Korotaev goto out_nomap; 26061da177e4SLinus Torvalds } 26071da177e4SLinus Torvalds 26088c7c6e34SKAMEZAWA Hiroyuki /* 26098c7c6e34SKAMEZAWA Hiroyuki * The page isn't present yet, go ahead with the fault. 26108c7c6e34SKAMEZAWA Hiroyuki * 26118c7c6e34SKAMEZAWA Hiroyuki * Be careful about the sequence of operations here. 26128c7c6e34SKAMEZAWA Hiroyuki * To get its accounting right, reuse_swap_page() must be called 26138c7c6e34SKAMEZAWA Hiroyuki * while the page is counted on swap but not yet in mapcount i.e. 26148c7c6e34SKAMEZAWA Hiroyuki * before page_add_anon_rmap() and swap_free(); try_to_free_swap() 26158c7c6e34SKAMEZAWA Hiroyuki * must be called after the swap_free(), or it will never succeed. 261603f3c433SKAMEZAWA Hiroyuki * Because delete_from_swap_page() may be called by reuse_swap_page(), 261703f3c433SKAMEZAWA Hiroyuki * mem_cgroup_commit_charge_swapin() may not be able to find swp_entry 261803f3c433SKAMEZAWA Hiroyuki * in page->private. In this case, a record in swap_cgroup is silently 261903f3c433SKAMEZAWA Hiroyuki * discarded at swap_free(). 26208c7c6e34SKAMEZAWA Hiroyuki */ 26211da177e4SLinus Torvalds 26224294621fSHugh Dickins inc_mm_counter(mm, anon_rss); 26231da177e4SLinus Torvalds pte = mk_pte(page, vma->vm_page_prot); 262430c9f3a9SLinus Torvalds if ((flags & FAULT_FLAG_WRITE) && reuse_swap_page(page)) { 26251da177e4SLinus Torvalds pte = maybe_mkwrite(pte_mkdirty(pte), vma); 262630c9f3a9SLinus Torvalds flags &= ~FAULT_FLAG_WRITE; 26271da177e4SLinus Torvalds } 26281da177e4SLinus Torvalds flush_icache_page(vma, page); 26291da177e4SLinus Torvalds set_pte_at(mm, address, page_table, pte); 26301da177e4SLinus Torvalds page_add_anon_rmap(page, vma, address); 263103f3c433SKAMEZAWA Hiroyuki /* It's better to call commit-charge after rmap is established */ 263203f3c433SKAMEZAWA Hiroyuki mem_cgroup_commit_charge_swapin(page, ptr); 26331da177e4SLinus Torvalds 2634c475a8abSHugh Dickins swap_free(entry); 2635b291f000SNick Piggin if (vm_swap_full() || (vma->vm_flags & VM_LOCKED) || PageMlocked(page)) 2636a2c43eedSHugh Dickins try_to_free_swap(page); 2637c475a8abSHugh Dickins unlock_page(page); 2638c475a8abSHugh Dickins 263930c9f3a9SLinus Torvalds if (flags & FAULT_FLAG_WRITE) { 264061469f1dSHugh Dickins ret |= do_wp_page(mm, vma, address, page_table, pmd, ptl, pte); 264161469f1dSHugh Dickins if (ret & VM_FAULT_ERROR) 264261469f1dSHugh Dickins ret &= VM_FAULT_ERROR; 26431da177e4SLinus Torvalds goto out; 26441da177e4SLinus Torvalds } 26451da177e4SLinus Torvalds 26461da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 26471da177e4SLinus Torvalds update_mmu_cache(vma, address, pte); 264865500d23SHugh Dickins unlock: 26498f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 26501da177e4SLinus Torvalds out: 26511da177e4SLinus Torvalds return ret; 2652b8107480SKirill Korotaev out_nomap: 26537a81b88cSKAMEZAWA Hiroyuki mem_cgroup_cancel_charge_swapin(ptr); 26548f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 2655bc43f75cSJohannes Weiner out_page: 2656b8107480SKirill Korotaev unlock_page(page); 2657b8107480SKirill Korotaev page_cache_release(page); 265865500d23SHugh Dickins return ret; 26591da177e4SLinus Torvalds } 26601da177e4SLinus Torvalds 26611da177e4SLinus Torvalds /* 26628f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 26638f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 26648f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 26651da177e4SLinus Torvalds */ 266665500d23SHugh Dickins static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma, 266765500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 266830c9f3a9SLinus Torvalds unsigned int flags) 26691da177e4SLinus Torvalds { 26708f4e2101SHugh Dickins struct page *page; 26718f4e2101SHugh Dickins spinlock_t *ptl; 26721da177e4SLinus Torvalds pte_t entry; 26731da177e4SLinus Torvalds 2674*62eede62SHugh Dickins if (!(flags & FAULT_FLAG_WRITE)) { 2675*62eede62SHugh Dickins entry = pte_mkspecial(pfn_pte(my_zero_pfn(address), 2676*62eede62SHugh Dickins vma->vm_page_prot)); 2677a13ea5b7SHugh Dickins ptl = pte_lockptr(mm, pmd); 2678a13ea5b7SHugh Dickins spin_lock(ptl); 2679a13ea5b7SHugh Dickins if (!pte_none(*page_table)) 2680a13ea5b7SHugh Dickins goto unlock; 2681a13ea5b7SHugh Dickins goto setpte; 2682a13ea5b7SHugh Dickins } 2683a13ea5b7SHugh Dickins 26841da177e4SLinus Torvalds /* Allocate our own private page. */ 26851da177e4SLinus Torvalds pte_unmap(page_table); 26861da177e4SLinus Torvalds 26871da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 268865500d23SHugh Dickins goto oom; 2689769848c0SMel Gorman page = alloc_zeroed_user_highpage_movable(vma, address); 26901da177e4SLinus Torvalds if (!page) 269165500d23SHugh Dickins goto oom; 26920ed361deSNick Piggin __SetPageUptodate(page); 26931da177e4SLinus Torvalds 26942c26fdd7SKAMEZAWA Hiroyuki if (mem_cgroup_newpage_charge(page, mm, GFP_KERNEL)) 26958a9f3ccdSBalbir Singh goto oom_free_page; 26968a9f3ccdSBalbir Singh 269765500d23SHugh Dickins entry = mk_pte(page, vma->vm_page_prot); 26981ac0cb5dSHugh Dickins if (vma->vm_flags & VM_WRITE) 26991ac0cb5dSHugh Dickins entry = pte_mkwrite(pte_mkdirty(entry)); 27008f4e2101SHugh Dickins 27018f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 27021c2fb7a4SAndrea Arcangeli if (!pte_none(*page_table)) 27038f4e2101SHugh Dickins goto release; 27049ba69294SHugh Dickins 27058f4e2101SHugh Dickins inc_mm_counter(mm, anon_rss); 27069617d95eSNick Piggin page_add_new_anon_rmap(page, vma, address); 2707a13ea5b7SHugh Dickins setpte: 270865500d23SHugh Dickins set_pte_at(mm, address, page_table, entry); 27091da177e4SLinus Torvalds 27101da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 271165500d23SHugh Dickins update_mmu_cache(vma, address, entry); 271265500d23SHugh Dickins unlock: 27138f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 271483c54070SNick Piggin return 0; 27158f4e2101SHugh Dickins release: 27168a9f3ccdSBalbir Singh mem_cgroup_uncharge_page(page); 27178f4e2101SHugh Dickins page_cache_release(page); 27188f4e2101SHugh Dickins goto unlock; 27198a9f3ccdSBalbir Singh oom_free_page: 27206dbf6d3bSHugh Dickins page_cache_release(page); 272165500d23SHugh Dickins oom: 27221da177e4SLinus Torvalds return VM_FAULT_OOM; 27231da177e4SLinus Torvalds } 27241da177e4SLinus Torvalds 27251da177e4SLinus Torvalds /* 272654cb8821SNick Piggin * __do_fault() tries to create a new page mapping. It aggressively 27271da177e4SLinus Torvalds * tries to share with existing pages, but makes a separate copy if 272854cb8821SNick Piggin * the FAULT_FLAG_WRITE is set in the flags parameter in order to avoid 272954cb8821SNick Piggin * the next page fault. 27301da177e4SLinus Torvalds * 27311da177e4SLinus Torvalds * As this is called only for pages that do not currently exist, we 27321da177e4SLinus Torvalds * do not need to flush old virtual caches or the TLB. 27331da177e4SLinus Torvalds * 27348f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 273516abfa08SHugh Dickins * but allow concurrent faults), and pte neither mapped nor locked. 27368f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 27371da177e4SLinus Torvalds */ 273854cb8821SNick Piggin static int __do_fault(struct mm_struct *mm, struct vm_area_struct *vma, 273916abfa08SHugh Dickins unsigned long address, pmd_t *pmd, 274054cb8821SNick Piggin pgoff_t pgoff, unsigned int flags, pte_t orig_pte) 27411da177e4SLinus Torvalds { 274216abfa08SHugh Dickins pte_t *page_table; 27438f4e2101SHugh Dickins spinlock_t *ptl; 2744d0217ac0SNick Piggin struct page *page; 27451da177e4SLinus Torvalds pte_t entry; 27461da177e4SLinus Torvalds int anon = 0; 27475b4e655eSKAMEZAWA Hiroyuki int charged = 0; 2748d08b3851SPeter Zijlstra struct page *dirty_page = NULL; 2749d0217ac0SNick Piggin struct vm_fault vmf; 2750d0217ac0SNick Piggin int ret; 2751a200ee18SPeter Zijlstra int page_mkwrite = 0; 275254cb8821SNick Piggin 2753d0217ac0SNick Piggin vmf.virtual_address = (void __user *)(address & PAGE_MASK); 2754d0217ac0SNick Piggin vmf.pgoff = pgoff; 2755d0217ac0SNick Piggin vmf.flags = flags; 2756d0217ac0SNick Piggin vmf.page = NULL; 27571da177e4SLinus Torvalds 2758d0217ac0SNick Piggin ret = vma->vm_ops->fault(vma, &vmf); 275983c54070SNick Piggin if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) 276083c54070SNick Piggin return ret; 27611da177e4SLinus Torvalds 2762d00806b1SNick Piggin /* 2763d0217ac0SNick Piggin * For consistency in subsequent calls, make the faulted page always 2764d00806b1SNick Piggin * locked. 2765d00806b1SNick Piggin */ 276683c54070SNick Piggin if (unlikely(!(ret & VM_FAULT_LOCKED))) 2767d0217ac0SNick Piggin lock_page(vmf.page); 276854cb8821SNick Piggin else 2769d0217ac0SNick Piggin VM_BUG_ON(!PageLocked(vmf.page)); 2770d00806b1SNick Piggin 27711da177e4SLinus Torvalds /* 27721da177e4SLinus Torvalds * Should we do an early C-O-W break? 27731da177e4SLinus Torvalds */ 2774d0217ac0SNick Piggin page = vmf.page; 277554cb8821SNick Piggin if (flags & FAULT_FLAG_WRITE) { 27769637a5efSDavid Howells if (!(vma->vm_flags & VM_SHARED)) { 277754cb8821SNick Piggin anon = 1; 2778d00806b1SNick Piggin if (unlikely(anon_vma_prepare(vma))) { 2779d0217ac0SNick Piggin ret = VM_FAULT_OOM; 278054cb8821SNick Piggin goto out; 2781d00806b1SNick Piggin } 278283c54070SNick Piggin page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, 278383c54070SNick Piggin vma, address); 2784d00806b1SNick Piggin if (!page) { 2785d0217ac0SNick Piggin ret = VM_FAULT_OOM; 278654cb8821SNick Piggin goto out; 2787d00806b1SNick Piggin } 27882c26fdd7SKAMEZAWA Hiroyuki if (mem_cgroup_newpage_charge(page, mm, GFP_KERNEL)) { 27895b4e655eSKAMEZAWA Hiroyuki ret = VM_FAULT_OOM; 27905b4e655eSKAMEZAWA Hiroyuki page_cache_release(page); 27915b4e655eSKAMEZAWA Hiroyuki goto out; 27925b4e655eSKAMEZAWA Hiroyuki } 27935b4e655eSKAMEZAWA Hiroyuki charged = 1; 2794b291f000SNick Piggin /* 2795b291f000SNick Piggin * Don't let another task, with possibly unlocked vma, 2796b291f000SNick Piggin * keep the mlocked page. 2797b291f000SNick Piggin */ 2798b291f000SNick Piggin if (vma->vm_flags & VM_LOCKED) 2799b291f000SNick Piggin clear_page_mlock(vmf.page); 2800d0217ac0SNick Piggin copy_user_highpage(page, vmf.page, address, vma); 28010ed361deSNick Piggin __SetPageUptodate(page); 28029637a5efSDavid Howells } else { 280354cb8821SNick Piggin /* 280454cb8821SNick Piggin * If the page will be shareable, see if the backing 28059637a5efSDavid Howells * address space wants to know that the page is about 280654cb8821SNick Piggin * to become writable 280754cb8821SNick Piggin */ 280869676147SMark Fasheh if (vma->vm_ops->page_mkwrite) { 2809c2ec175cSNick Piggin int tmp; 2810c2ec175cSNick Piggin 281169676147SMark Fasheh unlock_page(page); 2812b827e496SNick Piggin vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE; 2813c2ec175cSNick Piggin tmp = vma->vm_ops->page_mkwrite(vma, &vmf); 2814c2ec175cSNick Piggin if (unlikely(tmp & 2815c2ec175cSNick Piggin (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) { 2816c2ec175cSNick Piggin ret = tmp; 2817b827e496SNick Piggin goto unwritable_page; 281869676147SMark Fasheh } 2819b827e496SNick Piggin if (unlikely(!(tmp & VM_FAULT_LOCKED))) { 282069676147SMark Fasheh lock_page(page); 2821d0217ac0SNick Piggin if (!page->mapping) { 2822b827e496SNick Piggin ret = 0; /* retry the fault */ 2823b827e496SNick Piggin unlock_page(page); 2824b827e496SNick Piggin goto unwritable_page; 2825d0217ac0SNick Piggin } 2826b827e496SNick Piggin } else 2827b827e496SNick Piggin VM_BUG_ON(!PageLocked(page)); 2828a200ee18SPeter Zijlstra page_mkwrite = 1; 28299637a5efSDavid Howells } 28309637a5efSDavid Howells } 283154cb8821SNick Piggin 28321da177e4SLinus Torvalds } 28331da177e4SLinus Torvalds 28348f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 28351da177e4SLinus Torvalds 28361da177e4SLinus Torvalds /* 28371da177e4SLinus Torvalds * This silly early PAGE_DIRTY setting removes a race 28381da177e4SLinus Torvalds * due to the bad i386 page protection. But it's valid 28391da177e4SLinus Torvalds * for other architectures too. 28401da177e4SLinus Torvalds * 284130c9f3a9SLinus Torvalds * Note that if FAULT_FLAG_WRITE is set, we either now have 28421da177e4SLinus Torvalds * an exclusive copy of the page, or this is a shared mapping, 28431da177e4SLinus Torvalds * so we can make it writable and dirty to avoid having to 28441da177e4SLinus Torvalds * handle that later. 28451da177e4SLinus Torvalds */ 28461da177e4SLinus Torvalds /* Only go through if we didn't race with anybody else... */ 28471c2fb7a4SAndrea Arcangeli if (likely(pte_same(*page_table, orig_pte))) { 2848d00806b1SNick Piggin flush_icache_page(vma, page); 2849d00806b1SNick Piggin entry = mk_pte(page, vma->vm_page_prot); 285054cb8821SNick Piggin if (flags & FAULT_FLAG_WRITE) 28511da177e4SLinus Torvalds entry = maybe_mkwrite(pte_mkdirty(entry), vma); 28521da177e4SLinus Torvalds if (anon) { 28534294621fSHugh Dickins inc_mm_counter(mm, anon_rss); 2854d00806b1SNick Piggin page_add_new_anon_rmap(page, vma, address); 2855f57e88a8SHugh Dickins } else { 28564294621fSHugh Dickins inc_mm_counter(mm, file_rss); 2857d00806b1SNick Piggin page_add_file_rmap(page); 285854cb8821SNick Piggin if (flags & FAULT_FLAG_WRITE) { 2859d00806b1SNick Piggin dirty_page = page; 2860d08b3851SPeter Zijlstra get_page(dirty_page); 2861d08b3851SPeter Zijlstra } 28624294621fSHugh Dickins } 286364d6519dSLee Schermerhorn set_pte_at(mm, address, page_table, entry); 28641da177e4SLinus Torvalds 2865d00806b1SNick Piggin /* no need to invalidate: a not-present page won't be cached */ 28661da177e4SLinus Torvalds update_mmu_cache(vma, address, entry); 2867d00806b1SNick Piggin } else { 28685b4e655eSKAMEZAWA Hiroyuki if (charged) 28698a9f3ccdSBalbir Singh mem_cgroup_uncharge_page(page); 2870d00806b1SNick Piggin if (anon) 2871d00806b1SNick Piggin page_cache_release(page); 2872d00806b1SNick Piggin else 287354cb8821SNick Piggin anon = 1; /* no anon but release faulted_page */ 2874d00806b1SNick Piggin } 2875d00806b1SNick Piggin 28768f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 2877d00806b1SNick Piggin 2878d00806b1SNick Piggin out: 2879b827e496SNick Piggin if (dirty_page) { 2880b827e496SNick Piggin struct address_space *mapping = page->mapping; 28818f7b3d15SAnton Salikhmetov 2882b827e496SNick Piggin if (set_page_dirty(dirty_page)) 2883b827e496SNick Piggin page_mkwrite = 1; 2884b827e496SNick Piggin unlock_page(dirty_page); 2885d08b3851SPeter Zijlstra put_page(dirty_page); 2886b827e496SNick Piggin if (page_mkwrite && mapping) { 2887b827e496SNick Piggin /* 2888b827e496SNick Piggin * Some device drivers do not set page.mapping but still 2889b827e496SNick Piggin * dirty their pages 2890b827e496SNick Piggin */ 2891b827e496SNick Piggin balance_dirty_pages_ratelimited(mapping); 2892d08b3851SPeter Zijlstra } 2893d00806b1SNick Piggin 2894b827e496SNick Piggin /* file_update_time outside page_lock */ 2895b827e496SNick Piggin if (vma->vm_file) 2896b827e496SNick Piggin file_update_time(vma->vm_file); 2897b827e496SNick Piggin } else { 2898b827e496SNick Piggin unlock_page(vmf.page); 2899b827e496SNick Piggin if (anon) 2900b827e496SNick Piggin page_cache_release(vmf.page); 2901b827e496SNick Piggin } 2902b827e496SNick Piggin 2903b827e496SNick Piggin return ret; 2904b827e496SNick Piggin 2905b827e496SNick Piggin unwritable_page: 2906b827e496SNick Piggin page_cache_release(page); 290783c54070SNick Piggin return ret; 290854cb8821SNick Piggin } 2909d00806b1SNick Piggin 291054cb8821SNick Piggin static int do_linear_fault(struct mm_struct *mm, struct vm_area_struct *vma, 291154cb8821SNick Piggin unsigned long address, pte_t *page_table, pmd_t *pmd, 291230c9f3a9SLinus Torvalds unsigned int flags, pte_t orig_pte) 291354cb8821SNick Piggin { 291454cb8821SNick Piggin pgoff_t pgoff = (((address & PAGE_MASK) 29150da7e01fSDean Nelson - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; 291654cb8821SNick Piggin 291716abfa08SHugh Dickins pte_unmap(page_table); 291816abfa08SHugh Dickins return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte); 291954cb8821SNick Piggin } 292054cb8821SNick Piggin 2921f4b81804SJes Sorensen /* 29221da177e4SLinus Torvalds * Fault of a previously existing named mapping. Repopulate the pte 29231da177e4SLinus Torvalds * from the encoded file_pte if possible. This enables swappable 29241da177e4SLinus Torvalds * nonlinear vmas. 29258f4e2101SHugh Dickins * 29268f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 29278f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 29288f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 29291da177e4SLinus Torvalds */ 2930d0217ac0SNick Piggin static int do_nonlinear_fault(struct mm_struct *mm, struct vm_area_struct *vma, 293165500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 293230c9f3a9SLinus Torvalds unsigned int flags, pte_t orig_pte) 29331da177e4SLinus Torvalds { 293465500d23SHugh Dickins pgoff_t pgoff; 29351da177e4SLinus Torvalds 293630c9f3a9SLinus Torvalds flags |= FAULT_FLAG_NONLINEAR; 293730c9f3a9SLinus Torvalds 29384c21e2f2SHugh Dickins if (!pte_unmap_same(mm, pmd, page_table, orig_pte)) 293983c54070SNick Piggin return 0; 29401da177e4SLinus Torvalds 29412509ef26SHugh Dickins if (unlikely(!(vma->vm_flags & VM_NONLINEAR))) { 294265500d23SHugh Dickins /* 294365500d23SHugh Dickins * Page table corrupted: show pte and kill process. 294465500d23SHugh Dickins */ 29453dc14741SHugh Dickins print_bad_pte(vma, address, orig_pte, NULL); 294665500d23SHugh Dickins return VM_FAULT_OOM; 294765500d23SHugh Dickins } 294865500d23SHugh Dickins 294965500d23SHugh Dickins pgoff = pte_to_pgoff(orig_pte); 295016abfa08SHugh Dickins return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte); 29511da177e4SLinus Torvalds } 29521da177e4SLinus Torvalds 29531da177e4SLinus Torvalds /* 29541da177e4SLinus Torvalds * These routines also need to handle stuff like marking pages dirty 29551da177e4SLinus Torvalds * and/or accessed for architectures that don't do it in hardware (most 29561da177e4SLinus Torvalds * RISC architectures). The early dirtying is also good on the i386. 29571da177e4SLinus Torvalds * 29581da177e4SLinus Torvalds * There is also a hook called "update_mmu_cache()" that architectures 29591da177e4SLinus Torvalds * with external mmu caches can use to update those (ie the Sparc or 29601da177e4SLinus Torvalds * PowerPC hashed page tables that act as extended TLBs). 29611da177e4SLinus Torvalds * 2962c74df32cSHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 2963c74df32cSHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 2964c74df32cSHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 29651da177e4SLinus Torvalds */ 29661da177e4SLinus Torvalds static inline int handle_pte_fault(struct mm_struct *mm, 29671da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long address, 296830c9f3a9SLinus Torvalds pte_t *pte, pmd_t *pmd, unsigned int flags) 29691da177e4SLinus Torvalds { 29701da177e4SLinus Torvalds pte_t entry; 29718f4e2101SHugh Dickins spinlock_t *ptl; 29721da177e4SLinus Torvalds 29738dab5241SBenjamin Herrenschmidt entry = *pte; 29741da177e4SLinus Torvalds if (!pte_present(entry)) { 297565500d23SHugh Dickins if (pte_none(entry)) { 2976f4b81804SJes Sorensen if (vma->vm_ops) { 29773c18ddd1SNick Piggin if (likely(vma->vm_ops->fault)) 297854cb8821SNick Piggin return do_linear_fault(mm, vma, address, 297930c9f3a9SLinus Torvalds pte, pmd, flags, entry); 2980f4b81804SJes Sorensen } 2981f4b81804SJes Sorensen return do_anonymous_page(mm, vma, address, 298230c9f3a9SLinus Torvalds pte, pmd, flags); 298365500d23SHugh Dickins } 29841da177e4SLinus Torvalds if (pte_file(entry)) 2985d0217ac0SNick Piggin return do_nonlinear_fault(mm, vma, address, 298630c9f3a9SLinus Torvalds pte, pmd, flags, entry); 298765500d23SHugh Dickins return do_swap_page(mm, vma, address, 298830c9f3a9SLinus Torvalds pte, pmd, flags, entry); 29891da177e4SLinus Torvalds } 29901da177e4SLinus Torvalds 29914c21e2f2SHugh Dickins ptl = pte_lockptr(mm, pmd); 29928f4e2101SHugh Dickins spin_lock(ptl); 29938f4e2101SHugh Dickins if (unlikely(!pte_same(*pte, entry))) 29948f4e2101SHugh Dickins goto unlock; 299530c9f3a9SLinus Torvalds if (flags & FAULT_FLAG_WRITE) { 29961da177e4SLinus Torvalds if (!pte_write(entry)) 29978f4e2101SHugh Dickins return do_wp_page(mm, vma, address, 29988f4e2101SHugh Dickins pte, pmd, ptl, entry); 29991da177e4SLinus Torvalds entry = pte_mkdirty(entry); 30001da177e4SLinus Torvalds } 30011da177e4SLinus Torvalds entry = pte_mkyoung(entry); 300230c9f3a9SLinus Torvalds if (ptep_set_access_flags(vma, address, pte, entry, flags & FAULT_FLAG_WRITE)) { 30031da177e4SLinus Torvalds update_mmu_cache(vma, address, entry); 30041a44e149SAndrea Arcangeli } else { 30051a44e149SAndrea Arcangeli /* 30061a44e149SAndrea Arcangeli * This is needed only for protection faults but the arch code 30071a44e149SAndrea Arcangeli * is not yet telling us if this is a protection fault or not. 30081a44e149SAndrea Arcangeli * This still avoids useless tlb flushes for .text page faults 30091a44e149SAndrea Arcangeli * with threads. 30101a44e149SAndrea Arcangeli */ 301130c9f3a9SLinus Torvalds if (flags & FAULT_FLAG_WRITE) 30121a44e149SAndrea Arcangeli flush_tlb_page(vma, address); 30131a44e149SAndrea Arcangeli } 30148f4e2101SHugh Dickins unlock: 30158f4e2101SHugh Dickins pte_unmap_unlock(pte, ptl); 301683c54070SNick Piggin return 0; 30171da177e4SLinus Torvalds } 30181da177e4SLinus Torvalds 30191da177e4SLinus Torvalds /* 30201da177e4SLinus Torvalds * By the time we get here, we already hold the mm semaphore 30211da177e4SLinus Torvalds */ 302283c54070SNick Piggin int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma, 3023d06063ccSLinus Torvalds unsigned long address, unsigned int flags) 30241da177e4SLinus Torvalds { 30251da177e4SLinus Torvalds pgd_t *pgd; 30261da177e4SLinus Torvalds pud_t *pud; 30271da177e4SLinus Torvalds pmd_t *pmd; 30281da177e4SLinus Torvalds pte_t *pte; 30291da177e4SLinus Torvalds 30301da177e4SLinus Torvalds __set_current_state(TASK_RUNNING); 30311da177e4SLinus Torvalds 3032f8891e5eSChristoph Lameter count_vm_event(PGFAULT); 30331da177e4SLinus Torvalds 3034ac9b9c66SHugh Dickins if (unlikely(is_vm_hugetlb_page(vma))) 303530c9f3a9SLinus Torvalds return hugetlb_fault(mm, vma, address, flags); 30361da177e4SLinus Torvalds 30371da177e4SLinus Torvalds pgd = pgd_offset(mm, address); 30381da177e4SLinus Torvalds pud = pud_alloc(mm, pgd, address); 30391da177e4SLinus Torvalds if (!pud) 3040c74df32cSHugh Dickins return VM_FAULT_OOM; 30411da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, address); 30421da177e4SLinus Torvalds if (!pmd) 3043c74df32cSHugh Dickins return VM_FAULT_OOM; 30441da177e4SLinus Torvalds pte = pte_alloc_map(mm, pmd, address); 30451da177e4SLinus Torvalds if (!pte) 3046c74df32cSHugh Dickins return VM_FAULT_OOM; 30471da177e4SLinus Torvalds 304830c9f3a9SLinus Torvalds return handle_pte_fault(mm, vma, address, pte, pmd, flags); 30491da177e4SLinus Torvalds } 30501da177e4SLinus Torvalds 30511da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED 30521da177e4SLinus Torvalds /* 30531da177e4SLinus Torvalds * Allocate page upper directory. 3054872fec16SHugh Dickins * We've already handled the fast-path in-line. 30551da177e4SLinus Torvalds */ 30561bb3630eSHugh Dickins int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address) 30571da177e4SLinus Torvalds { 3058c74df32cSHugh Dickins pud_t *new = pud_alloc_one(mm, address); 3059c74df32cSHugh Dickins if (!new) 30601bb3630eSHugh Dickins return -ENOMEM; 30611da177e4SLinus Torvalds 3062362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 3063362a61adSNick Piggin 3064872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 30651bb3630eSHugh Dickins if (pgd_present(*pgd)) /* Another has populated it */ 30665e541973SBenjamin Herrenschmidt pud_free(mm, new); 30671bb3630eSHugh Dickins else 30681da177e4SLinus Torvalds pgd_populate(mm, pgd, new); 3069872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 30701bb3630eSHugh Dickins return 0; 30711da177e4SLinus Torvalds } 30721da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */ 30731da177e4SLinus Torvalds 30741da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED 30751da177e4SLinus Torvalds /* 30761da177e4SLinus Torvalds * Allocate page middle directory. 3077872fec16SHugh Dickins * We've already handled the fast-path in-line. 30781da177e4SLinus Torvalds */ 30791bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) 30801da177e4SLinus Torvalds { 3081c74df32cSHugh Dickins pmd_t *new = pmd_alloc_one(mm, address); 3082c74df32cSHugh Dickins if (!new) 30831bb3630eSHugh Dickins return -ENOMEM; 30841da177e4SLinus Torvalds 3085362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 3086362a61adSNick Piggin 3087872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 30881da177e4SLinus Torvalds #ifndef __ARCH_HAS_4LEVEL_HACK 30891bb3630eSHugh Dickins if (pud_present(*pud)) /* Another has populated it */ 30905e541973SBenjamin Herrenschmidt pmd_free(mm, new); 30911bb3630eSHugh Dickins else 30921da177e4SLinus Torvalds pud_populate(mm, pud, new); 30931da177e4SLinus Torvalds #else 30941bb3630eSHugh Dickins if (pgd_present(*pud)) /* Another has populated it */ 30955e541973SBenjamin Herrenschmidt pmd_free(mm, new); 30961bb3630eSHugh Dickins else 30971da177e4SLinus Torvalds pgd_populate(mm, pud, new); 30981da177e4SLinus Torvalds #endif /* __ARCH_HAS_4LEVEL_HACK */ 3099872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 31001bb3630eSHugh Dickins return 0; 31011da177e4SLinus Torvalds } 31021da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */ 31031da177e4SLinus Torvalds 31041da177e4SLinus Torvalds int make_pages_present(unsigned long addr, unsigned long end) 31051da177e4SLinus Torvalds { 31061da177e4SLinus Torvalds int ret, len, write; 31071da177e4SLinus Torvalds struct vm_area_struct * vma; 31081da177e4SLinus Torvalds 31091da177e4SLinus Torvalds vma = find_vma(current->mm, addr); 31101da177e4SLinus Torvalds if (!vma) 3111a477097dSKOSAKI Motohiro return -ENOMEM; 31121da177e4SLinus Torvalds write = (vma->vm_flags & VM_WRITE) != 0; 31135bcb28b1SEric Sesterhenn BUG_ON(addr >= end); 31145bcb28b1SEric Sesterhenn BUG_ON(end > vma->vm_end); 311568e116a3SRolf Eike Beer len = DIV_ROUND_UP(end, PAGE_SIZE) - addr/PAGE_SIZE; 31161da177e4SLinus Torvalds ret = get_user_pages(current, current->mm, addr, 31171da177e4SLinus Torvalds len, write, 0, NULL, NULL); 3118c11d69d8SLee Schermerhorn if (ret < 0) 31191da177e4SLinus Torvalds return ret; 31209978ad58SLee Schermerhorn return ret == len ? 0 : -EFAULT; 31211da177e4SLinus Torvalds } 31221da177e4SLinus Torvalds 31231da177e4SLinus Torvalds #if !defined(__HAVE_ARCH_GATE_AREA) 31241da177e4SLinus Torvalds 31251da177e4SLinus Torvalds #if defined(AT_SYSINFO_EHDR) 31265ce7852cSAdrian Bunk static struct vm_area_struct gate_vma; 31271da177e4SLinus Torvalds 31281da177e4SLinus Torvalds static int __init gate_vma_init(void) 31291da177e4SLinus Torvalds { 31301da177e4SLinus Torvalds gate_vma.vm_mm = NULL; 31311da177e4SLinus Torvalds gate_vma.vm_start = FIXADDR_USER_START; 31321da177e4SLinus Torvalds gate_vma.vm_end = FIXADDR_USER_END; 3133b6558c4aSRoland McGrath gate_vma.vm_flags = VM_READ | VM_MAYREAD | VM_EXEC | VM_MAYEXEC; 3134b6558c4aSRoland McGrath gate_vma.vm_page_prot = __P101; 3135f47aef55SRoland McGrath /* 3136f47aef55SRoland McGrath * Make sure the vDSO gets into every core dump. 3137f47aef55SRoland McGrath * Dumping its contents makes post-mortem fully interpretable later 3138f47aef55SRoland McGrath * without matching up the same kernel and hardware config to see 3139f47aef55SRoland McGrath * what PC values meant. 3140f47aef55SRoland McGrath */ 3141f47aef55SRoland McGrath gate_vma.vm_flags |= VM_ALWAYSDUMP; 31421da177e4SLinus Torvalds return 0; 31431da177e4SLinus Torvalds } 31441da177e4SLinus Torvalds __initcall(gate_vma_init); 31451da177e4SLinus Torvalds #endif 31461da177e4SLinus Torvalds 31471da177e4SLinus Torvalds struct vm_area_struct *get_gate_vma(struct task_struct *tsk) 31481da177e4SLinus Torvalds { 31491da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR 31501da177e4SLinus Torvalds return &gate_vma; 31511da177e4SLinus Torvalds #else 31521da177e4SLinus Torvalds return NULL; 31531da177e4SLinus Torvalds #endif 31541da177e4SLinus Torvalds } 31551da177e4SLinus Torvalds 31561da177e4SLinus Torvalds int in_gate_area_no_task(unsigned long addr) 31571da177e4SLinus Torvalds { 31581da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR 31591da177e4SLinus Torvalds if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END)) 31601da177e4SLinus Torvalds return 1; 31611da177e4SLinus Torvalds #endif 31621da177e4SLinus Torvalds return 0; 31631da177e4SLinus Torvalds } 31641da177e4SLinus Torvalds 31651da177e4SLinus Torvalds #endif /* __HAVE_ARCH_GATE_AREA */ 31660ec76a11SDavid Howells 3167f8ad0f49SJohannes Weiner static int follow_pte(struct mm_struct *mm, unsigned long address, 3168f8ad0f49SJohannes Weiner pte_t **ptepp, spinlock_t **ptlp) 3169f8ad0f49SJohannes Weiner { 3170f8ad0f49SJohannes Weiner pgd_t *pgd; 3171f8ad0f49SJohannes Weiner pud_t *pud; 3172f8ad0f49SJohannes Weiner pmd_t *pmd; 3173f8ad0f49SJohannes Weiner pte_t *ptep; 3174f8ad0f49SJohannes Weiner 3175f8ad0f49SJohannes Weiner pgd = pgd_offset(mm, address); 3176f8ad0f49SJohannes Weiner if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) 3177f8ad0f49SJohannes Weiner goto out; 3178f8ad0f49SJohannes Weiner 3179f8ad0f49SJohannes Weiner pud = pud_offset(pgd, address); 3180f8ad0f49SJohannes Weiner if (pud_none(*pud) || unlikely(pud_bad(*pud))) 3181f8ad0f49SJohannes Weiner goto out; 3182f8ad0f49SJohannes Weiner 3183f8ad0f49SJohannes Weiner pmd = pmd_offset(pud, address); 3184f8ad0f49SJohannes Weiner if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd))) 3185f8ad0f49SJohannes Weiner goto out; 3186f8ad0f49SJohannes Weiner 3187f8ad0f49SJohannes Weiner /* We cannot handle huge page PFN maps. Luckily they don't exist. */ 3188f8ad0f49SJohannes Weiner if (pmd_huge(*pmd)) 3189f8ad0f49SJohannes Weiner goto out; 3190f8ad0f49SJohannes Weiner 3191f8ad0f49SJohannes Weiner ptep = pte_offset_map_lock(mm, pmd, address, ptlp); 3192f8ad0f49SJohannes Weiner if (!ptep) 3193f8ad0f49SJohannes Weiner goto out; 3194f8ad0f49SJohannes Weiner if (!pte_present(*ptep)) 3195f8ad0f49SJohannes Weiner goto unlock; 3196f8ad0f49SJohannes Weiner *ptepp = ptep; 3197f8ad0f49SJohannes Weiner return 0; 3198f8ad0f49SJohannes Weiner unlock: 3199f8ad0f49SJohannes Weiner pte_unmap_unlock(ptep, *ptlp); 3200f8ad0f49SJohannes Weiner out: 3201f8ad0f49SJohannes Weiner return -EINVAL; 3202f8ad0f49SJohannes Weiner } 3203f8ad0f49SJohannes Weiner 32043b6748e2SJohannes Weiner /** 32053b6748e2SJohannes Weiner * follow_pfn - look up PFN at a user virtual address 32063b6748e2SJohannes Weiner * @vma: memory mapping 32073b6748e2SJohannes Weiner * @address: user virtual address 32083b6748e2SJohannes Weiner * @pfn: location to store found PFN 32093b6748e2SJohannes Weiner * 32103b6748e2SJohannes Weiner * Only IO mappings and raw PFN mappings are allowed. 32113b6748e2SJohannes Weiner * 32123b6748e2SJohannes Weiner * Returns zero and the pfn at @pfn on success, -ve otherwise. 32133b6748e2SJohannes Weiner */ 32143b6748e2SJohannes Weiner int follow_pfn(struct vm_area_struct *vma, unsigned long address, 32153b6748e2SJohannes Weiner unsigned long *pfn) 32163b6748e2SJohannes Weiner { 32173b6748e2SJohannes Weiner int ret = -EINVAL; 32183b6748e2SJohannes Weiner spinlock_t *ptl; 32193b6748e2SJohannes Weiner pte_t *ptep; 32203b6748e2SJohannes Weiner 32213b6748e2SJohannes Weiner if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 32223b6748e2SJohannes Weiner return ret; 32233b6748e2SJohannes Weiner 32243b6748e2SJohannes Weiner ret = follow_pte(vma->vm_mm, address, &ptep, &ptl); 32253b6748e2SJohannes Weiner if (ret) 32263b6748e2SJohannes Weiner return ret; 32273b6748e2SJohannes Weiner *pfn = pte_pfn(*ptep); 32283b6748e2SJohannes Weiner pte_unmap_unlock(ptep, ptl); 32293b6748e2SJohannes Weiner return 0; 32303b6748e2SJohannes Weiner } 32313b6748e2SJohannes Weiner EXPORT_SYMBOL(follow_pfn); 32323b6748e2SJohannes Weiner 323328b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT 3234d87fe660Svenkatesh.pallipadi@intel.com int follow_phys(struct vm_area_struct *vma, 323528b2ee20SRik van Riel unsigned long address, unsigned int flags, 3236d87fe660Svenkatesh.pallipadi@intel.com unsigned long *prot, resource_size_t *phys) 323728b2ee20SRik van Riel { 323803668a4dSJohannes Weiner int ret = -EINVAL; 323928b2ee20SRik van Riel pte_t *ptep, pte; 324028b2ee20SRik van Riel spinlock_t *ptl; 324128b2ee20SRik van Riel 3242d87fe660Svenkatesh.pallipadi@intel.com if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 3243d87fe660Svenkatesh.pallipadi@intel.com goto out; 324428b2ee20SRik van Riel 324503668a4dSJohannes Weiner if (follow_pte(vma->vm_mm, address, &ptep, &ptl)) 3246d87fe660Svenkatesh.pallipadi@intel.com goto out; 324728b2ee20SRik van Riel pte = *ptep; 324803668a4dSJohannes Weiner 324928b2ee20SRik van Riel if ((flags & FOLL_WRITE) && !pte_write(pte)) 325028b2ee20SRik van Riel goto unlock; 325128b2ee20SRik van Riel 325228b2ee20SRik van Riel *prot = pgprot_val(pte_pgprot(pte)); 325303668a4dSJohannes Weiner *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT; 325428b2ee20SRik van Riel 325503668a4dSJohannes Weiner ret = 0; 325628b2ee20SRik van Riel unlock: 325728b2ee20SRik van Riel pte_unmap_unlock(ptep, ptl); 325828b2ee20SRik van Riel out: 3259d87fe660Svenkatesh.pallipadi@intel.com return ret; 326028b2ee20SRik van Riel } 326128b2ee20SRik van Riel 326228b2ee20SRik van Riel int generic_access_phys(struct vm_area_struct *vma, unsigned long addr, 326328b2ee20SRik van Riel void *buf, int len, int write) 326428b2ee20SRik van Riel { 326528b2ee20SRik van Riel resource_size_t phys_addr; 326628b2ee20SRik van Riel unsigned long prot = 0; 32672bc7273bSKOSAKI Motohiro void __iomem *maddr; 326828b2ee20SRik van Riel int offset = addr & (PAGE_SIZE-1); 326928b2ee20SRik van Riel 3270d87fe660Svenkatesh.pallipadi@intel.com if (follow_phys(vma, addr, write, &prot, &phys_addr)) 327128b2ee20SRik van Riel return -EINVAL; 327228b2ee20SRik van Riel 327328b2ee20SRik van Riel maddr = ioremap_prot(phys_addr, PAGE_SIZE, prot); 327428b2ee20SRik van Riel if (write) 327528b2ee20SRik van Riel memcpy_toio(maddr + offset, buf, len); 327628b2ee20SRik van Riel else 327728b2ee20SRik van Riel memcpy_fromio(buf, maddr + offset, len); 327828b2ee20SRik van Riel iounmap(maddr); 327928b2ee20SRik van Riel 328028b2ee20SRik van Riel return len; 328128b2ee20SRik van Riel } 328228b2ee20SRik van Riel #endif 328328b2ee20SRik van Riel 32840ec76a11SDavid Howells /* 32850ec76a11SDavid Howells * Access another process' address space. 32860ec76a11SDavid Howells * Source/target buffer must be kernel space, 32870ec76a11SDavid Howells * Do not walk the page table directly, use get_user_pages 32880ec76a11SDavid Howells */ 32890ec76a11SDavid Howells int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write) 32900ec76a11SDavid Howells { 32910ec76a11SDavid Howells struct mm_struct *mm; 32920ec76a11SDavid Howells struct vm_area_struct *vma; 32930ec76a11SDavid Howells void *old_buf = buf; 32940ec76a11SDavid Howells 32950ec76a11SDavid Howells mm = get_task_mm(tsk); 32960ec76a11SDavid Howells if (!mm) 32970ec76a11SDavid Howells return 0; 32980ec76a11SDavid Howells 32990ec76a11SDavid Howells down_read(&mm->mmap_sem); 3300183ff22bSSimon Arlott /* ignore errors, just check how much was successfully transferred */ 33010ec76a11SDavid Howells while (len) { 33020ec76a11SDavid Howells int bytes, ret, offset; 33030ec76a11SDavid Howells void *maddr; 330428b2ee20SRik van Riel struct page *page = NULL; 33050ec76a11SDavid Howells 33060ec76a11SDavid Howells ret = get_user_pages(tsk, mm, addr, 1, 33070ec76a11SDavid Howells write, 1, &page, &vma); 330828b2ee20SRik van Riel if (ret <= 0) { 330928b2ee20SRik van Riel /* 331028b2ee20SRik van Riel * Check if this is a VM_IO | VM_PFNMAP VMA, which 331128b2ee20SRik van Riel * we can access using slightly different code. 331228b2ee20SRik van Riel */ 331328b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT 331428b2ee20SRik van Riel vma = find_vma(mm, addr); 331528b2ee20SRik van Riel if (!vma) 33160ec76a11SDavid Howells break; 331728b2ee20SRik van Riel if (vma->vm_ops && vma->vm_ops->access) 331828b2ee20SRik van Riel ret = vma->vm_ops->access(vma, addr, buf, 331928b2ee20SRik van Riel len, write); 332028b2ee20SRik van Riel if (ret <= 0) 332128b2ee20SRik van Riel #endif 332228b2ee20SRik van Riel break; 332328b2ee20SRik van Riel bytes = ret; 332428b2ee20SRik van Riel } else { 33250ec76a11SDavid Howells bytes = len; 33260ec76a11SDavid Howells offset = addr & (PAGE_SIZE-1); 33270ec76a11SDavid Howells if (bytes > PAGE_SIZE-offset) 33280ec76a11SDavid Howells bytes = PAGE_SIZE-offset; 33290ec76a11SDavid Howells 33300ec76a11SDavid Howells maddr = kmap(page); 33310ec76a11SDavid Howells if (write) { 33320ec76a11SDavid Howells copy_to_user_page(vma, page, addr, 33330ec76a11SDavid Howells maddr + offset, buf, bytes); 33340ec76a11SDavid Howells set_page_dirty_lock(page); 33350ec76a11SDavid Howells } else { 33360ec76a11SDavid Howells copy_from_user_page(vma, page, addr, 33370ec76a11SDavid Howells buf, maddr + offset, bytes); 33380ec76a11SDavid Howells } 33390ec76a11SDavid Howells kunmap(page); 33400ec76a11SDavid Howells page_cache_release(page); 334128b2ee20SRik van Riel } 33420ec76a11SDavid Howells len -= bytes; 33430ec76a11SDavid Howells buf += bytes; 33440ec76a11SDavid Howells addr += bytes; 33450ec76a11SDavid Howells } 33460ec76a11SDavid Howells up_read(&mm->mmap_sem); 33470ec76a11SDavid Howells mmput(mm); 33480ec76a11SDavid Howells 33490ec76a11SDavid Howells return buf - old_buf; 33500ec76a11SDavid Howells } 335103252919SAndi Kleen 335203252919SAndi Kleen /* 335303252919SAndi Kleen * Print the name of a VMA. 335403252919SAndi Kleen */ 335503252919SAndi Kleen void print_vma_addr(char *prefix, unsigned long ip) 335603252919SAndi Kleen { 335703252919SAndi Kleen struct mm_struct *mm = current->mm; 335803252919SAndi Kleen struct vm_area_struct *vma; 335903252919SAndi Kleen 3360e8bff74aSIngo Molnar /* 3361e8bff74aSIngo Molnar * Do not print if we are in atomic 3362e8bff74aSIngo Molnar * contexts (in exception stacks, etc.): 3363e8bff74aSIngo Molnar */ 3364e8bff74aSIngo Molnar if (preempt_count()) 3365e8bff74aSIngo Molnar return; 3366e8bff74aSIngo Molnar 336703252919SAndi Kleen down_read(&mm->mmap_sem); 336803252919SAndi Kleen vma = find_vma(mm, ip); 336903252919SAndi Kleen if (vma && vma->vm_file) { 337003252919SAndi Kleen struct file *f = vma->vm_file; 337103252919SAndi Kleen char *buf = (char *)__get_free_page(GFP_KERNEL); 337203252919SAndi Kleen if (buf) { 337303252919SAndi Kleen char *p, *s; 337403252919SAndi Kleen 3375cf28b486SJan Blunck p = d_path(&f->f_path, buf, PAGE_SIZE); 337603252919SAndi Kleen if (IS_ERR(p)) 337703252919SAndi Kleen p = "?"; 337803252919SAndi Kleen s = strrchr(p, '/'); 337903252919SAndi Kleen if (s) 338003252919SAndi Kleen p = s+1; 338103252919SAndi Kleen printk("%s%s[%lx+%lx]", prefix, p, 338203252919SAndi Kleen vma->vm_start, 338303252919SAndi Kleen vma->vm_end - vma->vm_start); 338403252919SAndi Kleen free_page((unsigned long)buf); 338503252919SAndi Kleen } 338603252919SAndi Kleen } 338703252919SAndi Kleen up_read(¤t->mm->mmap_sem); 338803252919SAndi Kleen } 33893ee1afa3SNick Piggin 33903ee1afa3SNick Piggin #ifdef CONFIG_PROVE_LOCKING 33913ee1afa3SNick Piggin void might_fault(void) 33923ee1afa3SNick Piggin { 339395156f00SPeter Zijlstra /* 339495156f00SPeter Zijlstra * Some code (nfs/sunrpc) uses socket ops on kernel memory while 339595156f00SPeter Zijlstra * holding the mmap_sem, this is safe because kernel memory doesn't 339695156f00SPeter Zijlstra * get paged out, therefore we'll never actually fault, and the 339795156f00SPeter Zijlstra * below annotations will generate false positives. 339895156f00SPeter Zijlstra */ 339995156f00SPeter Zijlstra if (segment_eq(get_fs(), KERNEL_DS)) 340095156f00SPeter Zijlstra return; 340195156f00SPeter Zijlstra 34023ee1afa3SNick Piggin might_sleep(); 34033ee1afa3SNick Piggin /* 34043ee1afa3SNick Piggin * it would be nicer only to annotate paths which are not under 34053ee1afa3SNick Piggin * pagefault_disable, however that requires a larger audit and 34063ee1afa3SNick Piggin * providing helpers like get_user_atomic. 34073ee1afa3SNick Piggin */ 34083ee1afa3SNick Piggin if (!in_atomic() && current->mm) 34093ee1afa3SNick Piggin might_lock_read(¤t->mm->mmap_sem); 34103ee1afa3SNick Piggin } 34113ee1afa3SNick Piggin EXPORT_SYMBOL(might_fault); 34123ee1afa3SNick Piggin #endif 3413