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 111a62eaf15SAndi Kleen 1121da177e4SLinus Torvalds /* 1131da177e4SLinus Torvalds * If a p?d_bad entry is found while walking page tables, report 1141da177e4SLinus Torvalds * the error, before resetting entry to p?d_none. Usually (but 1151da177e4SLinus Torvalds * very seldom) called out from the p?d_none_or_clear_bad macros. 1161da177e4SLinus Torvalds */ 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds void pgd_clear_bad(pgd_t *pgd) 1191da177e4SLinus Torvalds { 1201da177e4SLinus Torvalds pgd_ERROR(*pgd); 1211da177e4SLinus Torvalds pgd_clear(pgd); 1221da177e4SLinus Torvalds } 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds void pud_clear_bad(pud_t *pud) 1251da177e4SLinus Torvalds { 1261da177e4SLinus Torvalds pud_ERROR(*pud); 1271da177e4SLinus Torvalds pud_clear(pud); 1281da177e4SLinus Torvalds } 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds void pmd_clear_bad(pmd_t *pmd) 1311da177e4SLinus Torvalds { 1321da177e4SLinus Torvalds pmd_ERROR(*pmd); 1331da177e4SLinus Torvalds pmd_clear(pmd); 1341da177e4SLinus Torvalds } 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds /* 1371da177e4SLinus Torvalds * Note: this doesn't free the actual pages themselves. That 1381da177e4SLinus Torvalds * has been handled earlier when unmapping all the memory regions. 1391da177e4SLinus Torvalds */ 1409e1b32caSBenjamin Herrenschmidt static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd, 1419e1b32caSBenjamin Herrenschmidt unsigned long addr) 1421da177e4SLinus Torvalds { 1432f569afdSMartin Schwidefsky pgtable_t token = pmd_pgtable(*pmd); 1441da177e4SLinus Torvalds pmd_clear(pmd); 1459e1b32caSBenjamin Herrenschmidt pte_free_tlb(tlb, token, addr); 1461da177e4SLinus Torvalds tlb->mm->nr_ptes--; 1471da177e4SLinus Torvalds } 1481da177e4SLinus Torvalds 149e0da382cSHugh Dickins static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud, 150e0da382cSHugh Dickins unsigned long addr, unsigned long end, 151e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 1521da177e4SLinus Torvalds { 1531da177e4SLinus Torvalds pmd_t *pmd; 1541da177e4SLinus Torvalds unsigned long next; 155e0da382cSHugh Dickins unsigned long start; 1561da177e4SLinus Torvalds 157e0da382cSHugh Dickins start = addr; 1581da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 1591da177e4SLinus Torvalds do { 1601da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 1611da177e4SLinus Torvalds if (pmd_none_or_clear_bad(pmd)) 1621da177e4SLinus Torvalds continue; 1639e1b32caSBenjamin Herrenschmidt free_pte_range(tlb, pmd, addr); 1641da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 1651da177e4SLinus Torvalds 166e0da382cSHugh Dickins start &= PUD_MASK; 167e0da382cSHugh Dickins if (start < floor) 168e0da382cSHugh Dickins return; 169e0da382cSHugh Dickins if (ceiling) { 170e0da382cSHugh Dickins ceiling &= PUD_MASK; 171e0da382cSHugh Dickins if (!ceiling) 172e0da382cSHugh Dickins return; 1731da177e4SLinus Torvalds } 174e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 175e0da382cSHugh Dickins return; 176e0da382cSHugh Dickins 177e0da382cSHugh Dickins pmd = pmd_offset(pud, start); 178e0da382cSHugh Dickins pud_clear(pud); 1799e1b32caSBenjamin Herrenschmidt pmd_free_tlb(tlb, pmd, start); 1801da177e4SLinus Torvalds } 1811da177e4SLinus Torvalds 182e0da382cSHugh Dickins static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd, 183e0da382cSHugh Dickins unsigned long addr, unsigned long end, 184e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 1851da177e4SLinus Torvalds { 1861da177e4SLinus Torvalds pud_t *pud; 1871da177e4SLinus Torvalds unsigned long next; 188e0da382cSHugh Dickins unsigned long start; 1891da177e4SLinus Torvalds 190e0da382cSHugh Dickins start = addr; 1911da177e4SLinus Torvalds pud = pud_offset(pgd, addr); 1921da177e4SLinus Torvalds do { 1931da177e4SLinus Torvalds next = pud_addr_end(addr, end); 1941da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 1951da177e4SLinus Torvalds continue; 196e0da382cSHugh Dickins free_pmd_range(tlb, pud, addr, next, floor, ceiling); 1971da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 1981da177e4SLinus Torvalds 199e0da382cSHugh Dickins start &= PGDIR_MASK; 200e0da382cSHugh Dickins if (start < floor) 201e0da382cSHugh Dickins return; 202e0da382cSHugh Dickins if (ceiling) { 203e0da382cSHugh Dickins ceiling &= PGDIR_MASK; 204e0da382cSHugh Dickins if (!ceiling) 205e0da382cSHugh Dickins return; 2061da177e4SLinus Torvalds } 207e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 208e0da382cSHugh Dickins return; 209e0da382cSHugh Dickins 210e0da382cSHugh Dickins pud = pud_offset(pgd, start); 211e0da382cSHugh Dickins pgd_clear(pgd); 2129e1b32caSBenjamin Herrenschmidt pud_free_tlb(tlb, pud, start); 2131da177e4SLinus Torvalds } 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds /* 216e0da382cSHugh Dickins * This function frees user-level page tables of a process. 217e0da382cSHugh Dickins * 2181da177e4SLinus Torvalds * Must be called with pagetable lock held. 2191da177e4SLinus Torvalds */ 22042b77728SJan Beulich void free_pgd_range(struct mmu_gather *tlb, 221e0da382cSHugh Dickins unsigned long addr, unsigned long end, 222e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 2231da177e4SLinus Torvalds { 2241da177e4SLinus Torvalds pgd_t *pgd; 2251da177e4SLinus Torvalds unsigned long next; 226e0da382cSHugh Dickins unsigned long start; 2271da177e4SLinus Torvalds 228e0da382cSHugh Dickins /* 229e0da382cSHugh Dickins * The next few lines have given us lots of grief... 230e0da382cSHugh Dickins * 231e0da382cSHugh Dickins * Why are we testing PMD* at this top level? Because often 232e0da382cSHugh Dickins * there will be no work to do at all, and we'd prefer not to 233e0da382cSHugh Dickins * go all the way down to the bottom just to discover that. 234e0da382cSHugh Dickins * 235e0da382cSHugh Dickins * Why all these "- 1"s? Because 0 represents both the bottom 236e0da382cSHugh Dickins * of the address space and the top of it (using -1 for the 237e0da382cSHugh Dickins * top wouldn't help much: the masks would do the wrong thing). 238e0da382cSHugh Dickins * The rule is that addr 0 and floor 0 refer to the bottom of 239e0da382cSHugh Dickins * the address space, but end 0 and ceiling 0 refer to the top 240e0da382cSHugh Dickins * Comparisons need to use "end - 1" and "ceiling - 1" (though 241e0da382cSHugh Dickins * that end 0 case should be mythical). 242e0da382cSHugh Dickins * 243e0da382cSHugh Dickins * Wherever addr is brought up or ceiling brought down, we must 244e0da382cSHugh Dickins * be careful to reject "the opposite 0" before it confuses the 245e0da382cSHugh Dickins * subsequent tests. But what about where end is brought down 246e0da382cSHugh Dickins * by PMD_SIZE below? no, end can't go down to 0 there. 247e0da382cSHugh Dickins * 248e0da382cSHugh Dickins * Whereas we round start (addr) and ceiling down, by different 249e0da382cSHugh Dickins * masks at different levels, in order to test whether a table 250e0da382cSHugh Dickins * now has no other vmas using it, so can be freed, we don't 251e0da382cSHugh Dickins * bother to round floor or end up - the tests don't need that. 252e0da382cSHugh Dickins */ 253e0da382cSHugh Dickins 254e0da382cSHugh Dickins addr &= PMD_MASK; 255e0da382cSHugh Dickins if (addr < floor) { 256e0da382cSHugh Dickins addr += PMD_SIZE; 257e0da382cSHugh Dickins if (!addr) 258e0da382cSHugh Dickins return; 259e0da382cSHugh Dickins } 260e0da382cSHugh Dickins if (ceiling) { 261e0da382cSHugh Dickins ceiling &= PMD_MASK; 262e0da382cSHugh Dickins if (!ceiling) 263e0da382cSHugh Dickins return; 264e0da382cSHugh Dickins } 265e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 266e0da382cSHugh Dickins end -= PMD_SIZE; 267e0da382cSHugh Dickins if (addr > end - 1) 268e0da382cSHugh Dickins return; 269e0da382cSHugh Dickins 270e0da382cSHugh Dickins start = addr; 27142b77728SJan Beulich pgd = pgd_offset(tlb->mm, addr); 2721da177e4SLinus Torvalds do { 2731da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 2741da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 2751da177e4SLinus Torvalds continue; 27642b77728SJan Beulich free_pud_range(tlb, pgd, addr, next, floor, ceiling); 2771da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 278e0da382cSHugh Dickins } 279e0da382cSHugh Dickins 28042b77728SJan Beulich void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma, 281e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 282e0da382cSHugh Dickins { 283e0da382cSHugh Dickins while (vma) { 284e0da382cSHugh Dickins struct vm_area_struct *next = vma->vm_next; 285e0da382cSHugh Dickins unsigned long addr = vma->vm_start; 286e0da382cSHugh Dickins 2878f4f8c16SHugh Dickins /* 2888f4f8c16SHugh Dickins * Hide vma from rmap and vmtruncate before freeing pgtables 2898f4f8c16SHugh Dickins */ 2908f4f8c16SHugh Dickins anon_vma_unlink(vma); 2918f4f8c16SHugh Dickins unlink_file_vma(vma); 2928f4f8c16SHugh Dickins 2939da61aefSDavid Gibson if (is_vm_hugetlb_page(vma)) { 2943bf5ee95SHugh Dickins hugetlb_free_pgd_range(tlb, addr, vma->vm_end, 2953bf5ee95SHugh Dickins floor, next? next->vm_start: ceiling); 2963bf5ee95SHugh Dickins } else { 2973bf5ee95SHugh Dickins /* 2983bf5ee95SHugh Dickins * Optimization: gather nearby vmas into one call down 2993bf5ee95SHugh Dickins */ 3003bf5ee95SHugh Dickins while (next && next->vm_start <= vma->vm_end + PMD_SIZE 3014866920bSDavid Gibson && !is_vm_hugetlb_page(next)) { 302e0da382cSHugh Dickins vma = next; 303e0da382cSHugh Dickins next = vma->vm_next; 3048f4f8c16SHugh Dickins anon_vma_unlink(vma); 3058f4f8c16SHugh Dickins unlink_file_vma(vma); 306e0da382cSHugh Dickins } 3073bf5ee95SHugh Dickins free_pgd_range(tlb, addr, vma->vm_end, 308e0da382cSHugh Dickins floor, next? next->vm_start: ceiling); 3093bf5ee95SHugh Dickins } 310e0da382cSHugh Dickins vma = next; 311e0da382cSHugh Dickins } 3121da177e4SLinus Torvalds } 3131da177e4SLinus Torvalds 3141bb3630eSHugh Dickins int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address) 3151da177e4SLinus Torvalds { 3162f569afdSMartin Schwidefsky pgtable_t new = pte_alloc_one(mm, address); 3171da177e4SLinus Torvalds if (!new) 3181bb3630eSHugh Dickins return -ENOMEM; 3191bb3630eSHugh Dickins 320362a61adSNick Piggin /* 321362a61adSNick Piggin * Ensure all pte setup (eg. pte page lock and page clearing) are 322362a61adSNick Piggin * visible before the pte is made visible to other CPUs by being 323362a61adSNick Piggin * put into page tables. 324362a61adSNick Piggin * 325362a61adSNick Piggin * The other side of the story is the pointer chasing in the page 326362a61adSNick Piggin * table walking code (when walking the page table without locking; 327362a61adSNick Piggin * ie. most of the time). Fortunately, these data accesses consist 328362a61adSNick Piggin * of a chain of data-dependent loads, meaning most CPUs (alpha 329362a61adSNick Piggin * being the notable exception) will already guarantee loads are 330362a61adSNick Piggin * seen in-order. See the alpha page table accessors for the 331362a61adSNick Piggin * smp_read_barrier_depends() barriers in page table walking code. 332362a61adSNick Piggin */ 333362a61adSNick Piggin smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */ 334362a61adSNick Piggin 335c74df32cSHugh Dickins spin_lock(&mm->page_table_lock); 3362f569afdSMartin Schwidefsky if (!pmd_present(*pmd)) { /* Has another populated it ? */ 3371da177e4SLinus Torvalds mm->nr_ptes++; 3381da177e4SLinus Torvalds pmd_populate(mm, pmd, new); 3392f569afdSMartin Schwidefsky new = NULL; 3401da177e4SLinus Torvalds } 341c74df32cSHugh Dickins spin_unlock(&mm->page_table_lock); 3422f569afdSMartin Schwidefsky if (new) 3432f569afdSMartin Schwidefsky pte_free(mm, new); 3441bb3630eSHugh Dickins return 0; 3451da177e4SLinus Torvalds } 3461da177e4SLinus Torvalds 3471bb3630eSHugh Dickins int __pte_alloc_kernel(pmd_t *pmd, unsigned long address) 3481da177e4SLinus Torvalds { 3491bb3630eSHugh Dickins pte_t *new = pte_alloc_one_kernel(&init_mm, address); 3501da177e4SLinus Torvalds if (!new) 3511bb3630eSHugh Dickins return -ENOMEM; 3521da177e4SLinus Torvalds 353362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 354362a61adSNick Piggin 355872fec16SHugh Dickins spin_lock(&init_mm.page_table_lock); 3562f569afdSMartin Schwidefsky if (!pmd_present(*pmd)) { /* Has another populated it ? */ 357872fec16SHugh Dickins pmd_populate_kernel(&init_mm, pmd, new); 3582f569afdSMartin Schwidefsky new = NULL; 3592f569afdSMartin Schwidefsky } 360872fec16SHugh Dickins spin_unlock(&init_mm.page_table_lock); 3612f569afdSMartin Schwidefsky if (new) 3622f569afdSMartin Schwidefsky pte_free_kernel(&init_mm, new); 3631bb3630eSHugh Dickins return 0; 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds 366ae859762SHugh Dickins static inline void add_mm_rss(struct mm_struct *mm, int file_rss, int anon_rss) 367ae859762SHugh Dickins { 368ae859762SHugh Dickins if (file_rss) 369ae859762SHugh Dickins add_mm_counter(mm, file_rss, file_rss); 370ae859762SHugh Dickins if (anon_rss) 371ae859762SHugh Dickins add_mm_counter(mm, anon_rss, anon_rss); 372ae859762SHugh Dickins } 373ae859762SHugh Dickins 3741da177e4SLinus Torvalds /* 3756aab341eSLinus Torvalds * This function is called to print an error when a bad pte 3766aab341eSLinus Torvalds * is found. For example, we might have a PFN-mapped pte in 3776aab341eSLinus Torvalds * a region that doesn't allow it. 378b5810039SNick Piggin * 379b5810039SNick Piggin * The calling function must still handle the error. 380b5810039SNick Piggin */ 3813dc14741SHugh Dickins static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr, 3823dc14741SHugh Dickins pte_t pte, struct page *page) 383b5810039SNick Piggin { 3843dc14741SHugh Dickins pgd_t *pgd = pgd_offset(vma->vm_mm, addr); 3853dc14741SHugh Dickins pud_t *pud = pud_offset(pgd, addr); 3863dc14741SHugh Dickins pmd_t *pmd = pmd_offset(pud, addr); 3873dc14741SHugh Dickins struct address_space *mapping; 3883dc14741SHugh Dickins pgoff_t index; 389d936cf9bSHugh Dickins static unsigned long resume; 390d936cf9bSHugh Dickins static unsigned long nr_shown; 391d936cf9bSHugh Dickins static unsigned long nr_unshown; 392d936cf9bSHugh Dickins 393d936cf9bSHugh Dickins /* 394d936cf9bSHugh Dickins * Allow a burst of 60 reports, then keep quiet for that minute; 395d936cf9bSHugh Dickins * or allow a steady drip of one report per second. 396d936cf9bSHugh Dickins */ 397d936cf9bSHugh Dickins if (nr_shown == 60) { 398d936cf9bSHugh Dickins if (time_before(jiffies, resume)) { 399d936cf9bSHugh Dickins nr_unshown++; 400d936cf9bSHugh Dickins return; 401d936cf9bSHugh Dickins } 402d936cf9bSHugh Dickins if (nr_unshown) { 4031e9e6365SHugh Dickins printk(KERN_ALERT 4041e9e6365SHugh Dickins "BUG: Bad page map: %lu messages suppressed\n", 405d936cf9bSHugh Dickins nr_unshown); 406d936cf9bSHugh Dickins nr_unshown = 0; 407d936cf9bSHugh Dickins } 408d936cf9bSHugh Dickins nr_shown = 0; 409d936cf9bSHugh Dickins } 410d936cf9bSHugh Dickins if (nr_shown++ == 0) 411d936cf9bSHugh Dickins resume = jiffies + 60 * HZ; 4123dc14741SHugh Dickins 4133dc14741SHugh Dickins mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL; 4143dc14741SHugh Dickins index = linear_page_index(vma, addr); 4153dc14741SHugh Dickins 4161e9e6365SHugh Dickins printk(KERN_ALERT 4171e9e6365SHugh Dickins "BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n", 4183dc14741SHugh Dickins current->comm, 4193dc14741SHugh Dickins (long long)pte_val(pte), (long long)pmd_val(*pmd)); 4203dc14741SHugh Dickins if (page) { 4211e9e6365SHugh Dickins printk(KERN_ALERT 4223dc14741SHugh Dickins "page:%p flags:%p count:%d mapcount:%d mapping:%p index:%lx\n", 4233dc14741SHugh Dickins page, (void *)page->flags, page_count(page), 4243dc14741SHugh Dickins page_mapcount(page), page->mapping, page->index); 4253dc14741SHugh Dickins } 4261e9e6365SHugh Dickins printk(KERN_ALERT 4273dc14741SHugh Dickins "addr:%p vm_flags:%08lx anon_vma:%p mapping:%p index:%lx\n", 4283dc14741SHugh Dickins (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index); 4293dc14741SHugh Dickins /* 4303dc14741SHugh Dickins * Choose text because data symbols depend on CONFIG_KALLSYMS_ALL=y 4313dc14741SHugh Dickins */ 4323dc14741SHugh Dickins if (vma->vm_ops) 4331e9e6365SHugh Dickins print_symbol(KERN_ALERT "vma->vm_ops->fault: %s\n", 4343dc14741SHugh Dickins (unsigned long)vma->vm_ops->fault); 4353dc14741SHugh Dickins if (vma->vm_file && vma->vm_file->f_op) 4361e9e6365SHugh Dickins print_symbol(KERN_ALERT "vma->vm_file->f_op->mmap: %s\n", 4373dc14741SHugh Dickins (unsigned long)vma->vm_file->f_op->mmap); 438b5810039SNick Piggin dump_stack(); 4393dc14741SHugh Dickins add_taint(TAINT_BAD_PAGE); 440b5810039SNick Piggin } 441b5810039SNick Piggin 44267121172SLinus Torvalds static inline int is_cow_mapping(unsigned int flags) 44367121172SLinus Torvalds { 44467121172SLinus Torvalds return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; 44567121172SLinus Torvalds } 44667121172SLinus Torvalds 447b5810039SNick Piggin /* 4487e675137SNick Piggin * vm_normal_page -- This function gets the "struct page" associated with a pte. 4496aab341eSLinus Torvalds * 4507e675137SNick Piggin * "Special" mappings do not wish to be associated with a "struct page" (either 4517e675137SNick Piggin * it doesn't exist, or it exists but they don't want to touch it). In this 4527e675137SNick Piggin * case, NULL is returned here. "Normal" mappings do have a struct page. 453b379d790SJared Hulbert * 4547e675137SNick Piggin * There are 2 broad cases. Firstly, an architecture may define a pte_special() 4557e675137SNick Piggin * pte bit, in which case this function is trivial. Secondly, an architecture 4567e675137SNick Piggin * may not have a spare pte bit, which requires a more complicated scheme, 4577e675137SNick Piggin * described below. 4587e675137SNick Piggin * 4597e675137SNick Piggin * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a 4607e675137SNick Piggin * special mapping (even if there are underlying and valid "struct pages"). 4617e675137SNick Piggin * COWed pages of a VM_PFNMAP are always normal. 4626aab341eSLinus Torvalds * 463b379d790SJared Hulbert * The way we recognize COWed pages within VM_PFNMAP mappings is through the 464b379d790SJared Hulbert * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit 4657e675137SNick Piggin * set, and the vm_pgoff will point to the first PFN mapped: thus every special 4667e675137SNick Piggin * mapping will always honor the rule 4676aab341eSLinus Torvalds * 4686aab341eSLinus Torvalds * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT) 4696aab341eSLinus Torvalds * 4707e675137SNick Piggin * And for normal mappings this is false. 471b379d790SJared Hulbert * 4727e675137SNick Piggin * This restricts such mappings to be a linear translation from virtual address 4737e675137SNick Piggin * to pfn. To get around this restriction, we allow arbitrary mappings so long 4747e675137SNick Piggin * as the vma is not a COW mapping; in that case, we know that all ptes are 4757e675137SNick Piggin * special (because none can have been COWed). 476b379d790SJared Hulbert * 477b379d790SJared Hulbert * 4787e675137SNick Piggin * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP. 4797e675137SNick Piggin * 480b379d790SJared Hulbert * VM_MIXEDMAP mappings can likewise contain memory with or without "struct 481b379d790SJared Hulbert * page" backing, however the difference is that _all_ pages with a struct 482b379d790SJared Hulbert * page (that is, those where pfn_valid is true) are refcounted and considered 483b379d790SJared Hulbert * normal pages by the VM. The disadvantage is that pages are refcounted 484b379d790SJared Hulbert * (which can be slower and simply not an option for some PFNMAP users). The 485b379d790SJared Hulbert * advantage is that we don't have to follow the strict linearity rule of 486b379d790SJared Hulbert * PFNMAP mappings in order to support COWable mappings. 487b379d790SJared Hulbert * 488ee498ed7SHugh Dickins */ 4897e675137SNick Piggin #ifdef __HAVE_ARCH_PTE_SPECIAL 4907e675137SNick Piggin # define HAVE_PTE_SPECIAL 1 4917e675137SNick Piggin #else 4927e675137SNick Piggin # define HAVE_PTE_SPECIAL 0 4937e675137SNick Piggin #endif 4947e675137SNick Piggin struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, 4957e675137SNick Piggin pte_t pte) 496ee498ed7SHugh Dickins { 49722b31eecSHugh Dickins unsigned long pfn = pte_pfn(pte); 4987e675137SNick Piggin 4997e675137SNick Piggin if (HAVE_PTE_SPECIAL) { 50022b31eecSHugh Dickins if (likely(!pte_special(pte))) 50122b31eecSHugh Dickins goto check_pfn; 50222b31eecSHugh Dickins if (!(vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))) 50322b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 5047e675137SNick Piggin return NULL; 5057e675137SNick Piggin } 5067e675137SNick Piggin 5077e675137SNick Piggin /* !HAVE_PTE_SPECIAL case follows: */ 5087e675137SNick Piggin 509b379d790SJared Hulbert if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { 510b379d790SJared Hulbert if (vma->vm_flags & VM_MIXEDMAP) { 511b379d790SJared Hulbert if (!pfn_valid(pfn)) 512b379d790SJared Hulbert return NULL; 513b379d790SJared Hulbert goto out; 514b379d790SJared Hulbert } else { 5157e675137SNick Piggin unsigned long off; 5167e675137SNick Piggin off = (addr - vma->vm_start) >> PAGE_SHIFT; 5176aab341eSLinus Torvalds if (pfn == vma->vm_pgoff + off) 5186aab341eSLinus Torvalds return NULL; 51967121172SLinus Torvalds if (!is_cow_mapping(vma->vm_flags)) 520fb155c16SLinus Torvalds return NULL; 5216aab341eSLinus Torvalds } 522b379d790SJared Hulbert } 5236aab341eSLinus Torvalds 52422b31eecSHugh Dickins check_pfn: 52522b31eecSHugh Dickins if (unlikely(pfn > highest_memmap_pfn)) { 52622b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 52722b31eecSHugh Dickins return NULL; 52822b31eecSHugh Dickins } 5296aab341eSLinus Torvalds 5306aab341eSLinus Torvalds /* 5317e675137SNick Piggin * NOTE! We still have PageReserved() pages in the page tables. 5327e675137SNick Piggin * eg. VDSO mappings can cause them to exist. 5336aab341eSLinus Torvalds */ 534b379d790SJared Hulbert out: 5356aab341eSLinus Torvalds return pfn_to_page(pfn); 536ee498ed7SHugh Dickins } 537ee498ed7SHugh Dickins 538ee498ed7SHugh Dickins /* 5391da177e4SLinus Torvalds * copy one vm_area from one task to the other. Assumes the page tables 5401da177e4SLinus Torvalds * already present in the new task to be cleared in the whole range 5411da177e4SLinus Torvalds * covered by this vma. 5421da177e4SLinus Torvalds */ 5431da177e4SLinus Torvalds 5448c103762SHugh Dickins static inline void 5451da177e4SLinus Torvalds copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, 546b5810039SNick Piggin pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma, 5478c103762SHugh Dickins unsigned long addr, int *rss) 5481da177e4SLinus Torvalds { 549b5810039SNick Piggin unsigned long vm_flags = vma->vm_flags; 5501da177e4SLinus Torvalds pte_t pte = *src_pte; 5511da177e4SLinus Torvalds struct page *page; 5521da177e4SLinus Torvalds 5531da177e4SLinus Torvalds /* pte contains position in swap or file, so copy. */ 5541da177e4SLinus Torvalds if (unlikely(!pte_present(pte))) { 5551da177e4SLinus Torvalds if (!pte_file(pte)) { 5560697212aSChristoph Lameter swp_entry_t entry = pte_to_swp_entry(pte); 5570697212aSChristoph Lameter 5580697212aSChristoph Lameter swap_duplicate(entry); 5591da177e4SLinus Torvalds /* make sure dst_mm is on swapoff's mmlist. */ 5601da177e4SLinus Torvalds if (unlikely(list_empty(&dst_mm->mmlist))) { 5611da177e4SLinus Torvalds spin_lock(&mmlist_lock); 562f412ac08SHugh Dickins if (list_empty(&dst_mm->mmlist)) 563f412ac08SHugh Dickins list_add(&dst_mm->mmlist, 564f412ac08SHugh Dickins &src_mm->mmlist); 5651da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 5661da177e4SLinus Torvalds } 5670697212aSChristoph Lameter if (is_write_migration_entry(entry) && 5680697212aSChristoph Lameter is_cow_mapping(vm_flags)) { 5690697212aSChristoph Lameter /* 5700697212aSChristoph Lameter * COW mappings require pages in both parent 5710697212aSChristoph Lameter * and child to be set to read. 5720697212aSChristoph Lameter */ 5730697212aSChristoph Lameter make_migration_entry_read(&entry); 5740697212aSChristoph Lameter pte = swp_entry_to_pte(entry); 5750697212aSChristoph Lameter set_pte_at(src_mm, addr, src_pte, pte); 5760697212aSChristoph Lameter } 5771da177e4SLinus Torvalds } 578ae859762SHugh Dickins goto out_set_pte; 5791da177e4SLinus Torvalds } 5801da177e4SLinus Torvalds 5811da177e4SLinus Torvalds /* 5821da177e4SLinus Torvalds * If it's a COW mapping, write protect it both 5831da177e4SLinus Torvalds * in the parent and the child 5841da177e4SLinus Torvalds */ 58567121172SLinus Torvalds if (is_cow_mapping(vm_flags)) { 5861da177e4SLinus Torvalds ptep_set_wrprotect(src_mm, addr, src_pte); 5873dc90795SZachary Amsden pte = pte_wrprotect(pte); 5881da177e4SLinus Torvalds } 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds /* 5911da177e4SLinus Torvalds * If it's a shared mapping, mark it clean in 5921da177e4SLinus Torvalds * the child 5931da177e4SLinus Torvalds */ 5941da177e4SLinus Torvalds if (vm_flags & VM_SHARED) 5951da177e4SLinus Torvalds pte = pte_mkclean(pte); 5961da177e4SLinus Torvalds pte = pte_mkold(pte); 5976aab341eSLinus Torvalds 5986aab341eSLinus Torvalds page = vm_normal_page(vma, addr, pte); 5996aab341eSLinus Torvalds if (page) { 6001da177e4SLinus Torvalds get_page(page); 60121333b2bSHugh Dickins page_dup_rmap(page); 6028c103762SHugh Dickins rss[!!PageAnon(page)]++; 6036aab341eSLinus Torvalds } 604ae859762SHugh Dickins 605ae859762SHugh Dickins out_set_pte: 606ae859762SHugh Dickins set_pte_at(dst_mm, addr, dst_pte, pte); 6071da177e4SLinus Torvalds } 6081da177e4SLinus Torvalds 6091da177e4SLinus Torvalds static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 6101da177e4SLinus Torvalds pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma, 6111da177e4SLinus Torvalds unsigned long addr, unsigned long end) 6121da177e4SLinus Torvalds { 6131da177e4SLinus Torvalds pte_t *src_pte, *dst_pte; 614c74df32cSHugh Dickins spinlock_t *src_ptl, *dst_ptl; 615e040f218SHugh Dickins int progress = 0; 6168c103762SHugh Dickins int rss[2]; 6171da177e4SLinus Torvalds 6181da177e4SLinus Torvalds again: 619ae859762SHugh Dickins rss[1] = rss[0] = 0; 620c74df32cSHugh Dickins dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl); 6211da177e4SLinus Torvalds if (!dst_pte) 6221da177e4SLinus Torvalds return -ENOMEM; 6231da177e4SLinus Torvalds src_pte = pte_offset_map_nested(src_pmd, addr); 6244c21e2f2SHugh Dickins src_ptl = pte_lockptr(src_mm, src_pmd); 625f20dc5f7SIngo Molnar spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 6266606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 6271da177e4SLinus Torvalds 6281da177e4SLinus Torvalds do { 6291da177e4SLinus Torvalds /* 6301da177e4SLinus Torvalds * We are holding two locks at this point - either of them 6311da177e4SLinus Torvalds * could generate latencies in another task on another CPU. 6321da177e4SLinus Torvalds */ 633e040f218SHugh Dickins if (progress >= 32) { 634e040f218SHugh Dickins progress = 0; 635e040f218SHugh Dickins if (need_resched() || 63695c354feSNick Piggin spin_needbreak(src_ptl) || spin_needbreak(dst_ptl)) 6371da177e4SLinus Torvalds break; 638e040f218SHugh Dickins } 6391da177e4SLinus Torvalds if (pte_none(*src_pte)) { 6401da177e4SLinus Torvalds progress++; 6411da177e4SLinus Torvalds continue; 6421da177e4SLinus Torvalds } 6438c103762SHugh Dickins copy_one_pte(dst_mm, src_mm, dst_pte, src_pte, vma, addr, rss); 6441da177e4SLinus Torvalds progress += 8; 6451da177e4SLinus Torvalds } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end); 6461da177e4SLinus Torvalds 6476606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 648c74df32cSHugh Dickins spin_unlock(src_ptl); 6491da177e4SLinus Torvalds pte_unmap_nested(src_pte - 1); 650ae859762SHugh Dickins add_mm_rss(dst_mm, rss[0], rss[1]); 651c74df32cSHugh Dickins pte_unmap_unlock(dst_pte - 1, dst_ptl); 652c74df32cSHugh Dickins cond_resched(); 6531da177e4SLinus Torvalds if (addr != end) 6541da177e4SLinus Torvalds goto again; 6551da177e4SLinus Torvalds return 0; 6561da177e4SLinus Torvalds } 6571da177e4SLinus Torvalds 6581da177e4SLinus Torvalds static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 6591da177e4SLinus Torvalds pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma, 6601da177e4SLinus Torvalds unsigned long addr, unsigned long end) 6611da177e4SLinus Torvalds { 6621da177e4SLinus Torvalds pmd_t *src_pmd, *dst_pmd; 6631da177e4SLinus Torvalds unsigned long next; 6641da177e4SLinus Torvalds 6651da177e4SLinus Torvalds dst_pmd = pmd_alloc(dst_mm, dst_pud, addr); 6661da177e4SLinus Torvalds if (!dst_pmd) 6671da177e4SLinus Torvalds return -ENOMEM; 6681da177e4SLinus Torvalds src_pmd = pmd_offset(src_pud, addr); 6691da177e4SLinus Torvalds do { 6701da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 6711da177e4SLinus Torvalds if (pmd_none_or_clear_bad(src_pmd)) 6721da177e4SLinus Torvalds continue; 6731da177e4SLinus Torvalds if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd, 6741da177e4SLinus Torvalds vma, addr, next)) 6751da177e4SLinus Torvalds return -ENOMEM; 6761da177e4SLinus Torvalds } while (dst_pmd++, src_pmd++, addr = next, addr != end); 6771da177e4SLinus Torvalds return 0; 6781da177e4SLinus Torvalds } 6791da177e4SLinus Torvalds 6801da177e4SLinus Torvalds static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 6811da177e4SLinus Torvalds pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma, 6821da177e4SLinus Torvalds unsigned long addr, unsigned long end) 6831da177e4SLinus Torvalds { 6841da177e4SLinus Torvalds pud_t *src_pud, *dst_pud; 6851da177e4SLinus Torvalds unsigned long next; 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds dst_pud = pud_alloc(dst_mm, dst_pgd, addr); 6881da177e4SLinus Torvalds if (!dst_pud) 6891da177e4SLinus Torvalds return -ENOMEM; 6901da177e4SLinus Torvalds src_pud = pud_offset(src_pgd, addr); 6911da177e4SLinus Torvalds do { 6921da177e4SLinus Torvalds next = pud_addr_end(addr, end); 6931da177e4SLinus Torvalds if (pud_none_or_clear_bad(src_pud)) 6941da177e4SLinus Torvalds continue; 6951da177e4SLinus Torvalds if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud, 6961da177e4SLinus Torvalds vma, addr, next)) 6971da177e4SLinus Torvalds return -ENOMEM; 6981da177e4SLinus Torvalds } while (dst_pud++, src_pud++, addr = next, addr != end); 6991da177e4SLinus Torvalds return 0; 7001da177e4SLinus Torvalds } 7011da177e4SLinus Torvalds 7021da177e4SLinus Torvalds int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 7031da177e4SLinus Torvalds struct vm_area_struct *vma) 7041da177e4SLinus Torvalds { 7051da177e4SLinus Torvalds pgd_t *src_pgd, *dst_pgd; 7061da177e4SLinus Torvalds unsigned long next; 7071da177e4SLinus Torvalds unsigned long addr = vma->vm_start; 7081da177e4SLinus Torvalds unsigned long end = vma->vm_end; 709cddb8a5cSAndrea Arcangeli int ret; 7101da177e4SLinus Torvalds 711d992895bSNick Piggin /* 712d992895bSNick Piggin * Don't copy ptes where a page fault will fill them correctly. 713d992895bSNick Piggin * Fork becomes much lighter when there are big shared or private 714d992895bSNick Piggin * readonly mappings. The tradeoff is that copy_page_range is more 715d992895bSNick Piggin * efficient than faulting. 716d992895bSNick Piggin */ 7174d7672b4SLinus Torvalds if (!(vma->vm_flags & (VM_HUGETLB|VM_NONLINEAR|VM_PFNMAP|VM_INSERTPAGE))) { 718d992895bSNick Piggin if (!vma->anon_vma) 719d992895bSNick Piggin return 0; 720d992895bSNick Piggin } 721d992895bSNick Piggin 7221da177e4SLinus Torvalds if (is_vm_hugetlb_page(vma)) 7231da177e4SLinus Torvalds return copy_hugetlb_page_range(dst_mm, src_mm, vma); 7241da177e4SLinus Torvalds 72534801ba9Svenkatesh.pallipadi@intel.com if (unlikely(is_pfn_mapping(vma))) { 7262ab64037Svenkatesh.pallipadi@intel.com /* 7272ab64037Svenkatesh.pallipadi@intel.com * We do not free on error cases below as remove_vma 7282ab64037Svenkatesh.pallipadi@intel.com * gets called on error from higher level routine 7292ab64037Svenkatesh.pallipadi@intel.com */ 7302ab64037Svenkatesh.pallipadi@intel.com ret = track_pfn_vma_copy(vma); 7312ab64037Svenkatesh.pallipadi@intel.com if (ret) 7322ab64037Svenkatesh.pallipadi@intel.com return ret; 7332ab64037Svenkatesh.pallipadi@intel.com } 7342ab64037Svenkatesh.pallipadi@intel.com 735cddb8a5cSAndrea Arcangeli /* 736cddb8a5cSAndrea Arcangeli * We need to invalidate the secondary MMU mappings only when 737cddb8a5cSAndrea Arcangeli * there could be a permission downgrade on the ptes of the 738cddb8a5cSAndrea Arcangeli * parent mm. And a permission downgrade will only happen if 739cddb8a5cSAndrea Arcangeli * is_cow_mapping() returns true. 740cddb8a5cSAndrea Arcangeli */ 741cddb8a5cSAndrea Arcangeli if (is_cow_mapping(vma->vm_flags)) 742cddb8a5cSAndrea Arcangeli mmu_notifier_invalidate_range_start(src_mm, addr, end); 743cddb8a5cSAndrea Arcangeli 744cddb8a5cSAndrea Arcangeli ret = 0; 7451da177e4SLinus Torvalds dst_pgd = pgd_offset(dst_mm, addr); 7461da177e4SLinus Torvalds src_pgd = pgd_offset(src_mm, addr); 7471da177e4SLinus Torvalds do { 7481da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 7491da177e4SLinus Torvalds if (pgd_none_or_clear_bad(src_pgd)) 7501da177e4SLinus Torvalds continue; 751cddb8a5cSAndrea Arcangeli if (unlikely(copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd, 752cddb8a5cSAndrea Arcangeli vma, addr, next))) { 753cddb8a5cSAndrea Arcangeli ret = -ENOMEM; 754cddb8a5cSAndrea Arcangeli break; 755cddb8a5cSAndrea Arcangeli } 7561da177e4SLinus Torvalds } while (dst_pgd++, src_pgd++, addr = next, addr != end); 757cddb8a5cSAndrea Arcangeli 758cddb8a5cSAndrea Arcangeli if (is_cow_mapping(vma->vm_flags)) 759cddb8a5cSAndrea Arcangeli mmu_notifier_invalidate_range_end(src_mm, 760cddb8a5cSAndrea Arcangeli vma->vm_start, end); 761cddb8a5cSAndrea Arcangeli return ret; 7621da177e4SLinus Torvalds } 7631da177e4SLinus Torvalds 76451c6f666SRobin Holt static unsigned long zap_pte_range(struct mmu_gather *tlb, 765b5810039SNick Piggin struct vm_area_struct *vma, pmd_t *pmd, 7661da177e4SLinus Torvalds unsigned long addr, unsigned long end, 76751c6f666SRobin Holt long *zap_work, struct zap_details *details) 7681da177e4SLinus Torvalds { 769b5810039SNick Piggin struct mm_struct *mm = tlb->mm; 7701da177e4SLinus Torvalds pte_t *pte; 771508034a3SHugh Dickins spinlock_t *ptl; 772ae859762SHugh Dickins int file_rss = 0; 773ae859762SHugh Dickins int anon_rss = 0; 7741da177e4SLinus Torvalds 775508034a3SHugh Dickins pte = pte_offset_map_lock(mm, pmd, addr, &ptl); 7766606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 7771da177e4SLinus Torvalds do { 7781da177e4SLinus Torvalds pte_t ptent = *pte; 77951c6f666SRobin Holt if (pte_none(ptent)) { 78051c6f666SRobin Holt (*zap_work)--; 7811da177e4SLinus Torvalds continue; 78251c6f666SRobin Holt } 78351c6f666SRobin Holt 78451c6f666SRobin Holt (*zap_work) -= PAGE_SIZE; 78551c6f666SRobin Holt 7866f5e6b9eSHugh Dickins if (pte_present(ptent)) { 7876f5e6b9eSHugh Dickins struct page *page; 7886f5e6b9eSHugh Dickins 7896aab341eSLinus Torvalds page = vm_normal_page(vma, addr, ptent); 7901da177e4SLinus Torvalds if (unlikely(details) && page) { 7911da177e4SLinus Torvalds /* 7921da177e4SLinus Torvalds * unmap_shared_mapping_pages() wants to 7931da177e4SLinus Torvalds * invalidate cache without truncating: 7941da177e4SLinus Torvalds * unmap shared but keep private pages. 7951da177e4SLinus Torvalds */ 7961da177e4SLinus Torvalds if (details->check_mapping && 7971da177e4SLinus Torvalds details->check_mapping != page->mapping) 7981da177e4SLinus Torvalds continue; 7991da177e4SLinus Torvalds /* 8001da177e4SLinus Torvalds * Each page->index must be checked when 8011da177e4SLinus Torvalds * invalidating or truncating nonlinear. 8021da177e4SLinus Torvalds */ 8031da177e4SLinus Torvalds if (details->nonlinear_vma && 8041da177e4SLinus Torvalds (page->index < details->first_index || 8051da177e4SLinus Torvalds page->index > details->last_index)) 8061da177e4SLinus Torvalds continue; 8071da177e4SLinus Torvalds } 808b5810039SNick Piggin ptent = ptep_get_and_clear_full(mm, addr, pte, 809a600388dSZachary Amsden tlb->fullmm); 8101da177e4SLinus Torvalds tlb_remove_tlb_entry(tlb, pte, addr); 8111da177e4SLinus Torvalds if (unlikely(!page)) 8121da177e4SLinus Torvalds continue; 8131da177e4SLinus Torvalds if (unlikely(details) && details->nonlinear_vma 8141da177e4SLinus Torvalds && linear_page_index(details->nonlinear_vma, 8151da177e4SLinus Torvalds addr) != page->index) 816b5810039SNick Piggin set_pte_at(mm, addr, pte, 8171da177e4SLinus Torvalds pgoff_to_pte(page->index)); 8181da177e4SLinus Torvalds if (PageAnon(page)) 81986d912f4SHugh Dickins anon_rss--; 8206237bcd9SHugh Dickins else { 8216237bcd9SHugh Dickins if (pte_dirty(ptent)) 8226237bcd9SHugh Dickins set_page_dirty(page); 8234917e5d0SJohannes Weiner if (pte_young(ptent) && 8244917e5d0SJohannes Weiner likely(!VM_SequentialReadHint(vma))) 825bf3f3bc5SNick Piggin mark_page_accessed(page); 82686d912f4SHugh Dickins file_rss--; 8276237bcd9SHugh Dickins } 828edc315fdSHugh Dickins page_remove_rmap(page); 8293dc14741SHugh Dickins if (unlikely(page_mapcount(page) < 0)) 8303dc14741SHugh Dickins print_bad_pte(vma, addr, ptent, page); 8311da177e4SLinus Torvalds tlb_remove_page(tlb, page); 8321da177e4SLinus Torvalds continue; 8331da177e4SLinus Torvalds } 8341da177e4SLinus Torvalds /* 8351da177e4SLinus Torvalds * If details->check_mapping, we leave swap entries; 8361da177e4SLinus Torvalds * if details->nonlinear_vma, we leave file entries. 8371da177e4SLinus Torvalds */ 8381da177e4SLinus Torvalds if (unlikely(details)) 8391da177e4SLinus Torvalds continue; 8402509ef26SHugh Dickins if (pte_file(ptent)) { 8412509ef26SHugh Dickins if (unlikely(!(vma->vm_flags & VM_NONLINEAR))) 8422509ef26SHugh Dickins print_bad_pte(vma, addr, ptent, NULL); 8432509ef26SHugh Dickins } else if 8442509ef26SHugh Dickins (unlikely(!free_swap_and_cache(pte_to_swp_entry(ptent)))) 8452509ef26SHugh Dickins print_bad_pte(vma, addr, ptent, NULL); 8469888a1caSZachary Amsden pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); 84751c6f666SRobin Holt } while (pte++, addr += PAGE_SIZE, (addr != end && *zap_work > 0)); 848ae859762SHugh Dickins 84986d912f4SHugh Dickins add_mm_rss(mm, file_rss, anon_rss); 8506606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 851508034a3SHugh Dickins pte_unmap_unlock(pte - 1, ptl); 85251c6f666SRobin Holt 85351c6f666SRobin Holt return addr; 8541da177e4SLinus Torvalds } 8551da177e4SLinus Torvalds 85651c6f666SRobin Holt static inline unsigned long zap_pmd_range(struct mmu_gather *tlb, 857b5810039SNick Piggin struct vm_area_struct *vma, pud_t *pud, 8581da177e4SLinus Torvalds unsigned long addr, unsigned long end, 85951c6f666SRobin Holt long *zap_work, struct zap_details *details) 8601da177e4SLinus Torvalds { 8611da177e4SLinus Torvalds pmd_t *pmd; 8621da177e4SLinus Torvalds unsigned long next; 8631da177e4SLinus Torvalds 8641da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 8651da177e4SLinus Torvalds do { 8661da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 86751c6f666SRobin Holt if (pmd_none_or_clear_bad(pmd)) { 86851c6f666SRobin Holt (*zap_work)--; 8691da177e4SLinus Torvalds continue; 87051c6f666SRobin Holt } 87151c6f666SRobin Holt next = zap_pte_range(tlb, vma, pmd, addr, next, 87251c6f666SRobin Holt zap_work, details); 87351c6f666SRobin Holt } while (pmd++, addr = next, (addr != end && *zap_work > 0)); 87451c6f666SRobin Holt 87551c6f666SRobin Holt return addr; 8761da177e4SLinus Torvalds } 8771da177e4SLinus Torvalds 87851c6f666SRobin Holt static inline unsigned long zap_pud_range(struct mmu_gather *tlb, 879b5810039SNick Piggin struct vm_area_struct *vma, pgd_t *pgd, 8801da177e4SLinus Torvalds unsigned long addr, unsigned long end, 88151c6f666SRobin Holt long *zap_work, struct zap_details *details) 8821da177e4SLinus Torvalds { 8831da177e4SLinus Torvalds pud_t *pud; 8841da177e4SLinus Torvalds unsigned long next; 8851da177e4SLinus Torvalds 8861da177e4SLinus Torvalds pud = pud_offset(pgd, addr); 8871da177e4SLinus Torvalds do { 8881da177e4SLinus Torvalds next = pud_addr_end(addr, end); 88951c6f666SRobin Holt if (pud_none_or_clear_bad(pud)) { 89051c6f666SRobin Holt (*zap_work)--; 8911da177e4SLinus Torvalds continue; 89251c6f666SRobin Holt } 89351c6f666SRobin Holt next = zap_pmd_range(tlb, vma, pud, addr, next, 89451c6f666SRobin Holt zap_work, details); 89551c6f666SRobin Holt } while (pud++, addr = next, (addr != end && *zap_work > 0)); 89651c6f666SRobin Holt 89751c6f666SRobin Holt return addr; 8981da177e4SLinus Torvalds } 8991da177e4SLinus Torvalds 90051c6f666SRobin Holt static unsigned long unmap_page_range(struct mmu_gather *tlb, 90151c6f666SRobin Holt struct vm_area_struct *vma, 9021da177e4SLinus Torvalds unsigned long addr, unsigned long end, 90351c6f666SRobin Holt long *zap_work, struct zap_details *details) 9041da177e4SLinus Torvalds { 9051da177e4SLinus Torvalds pgd_t *pgd; 9061da177e4SLinus Torvalds unsigned long next; 9071da177e4SLinus Torvalds 9081da177e4SLinus Torvalds if (details && !details->check_mapping && !details->nonlinear_vma) 9091da177e4SLinus Torvalds details = NULL; 9101da177e4SLinus Torvalds 9111da177e4SLinus Torvalds BUG_ON(addr >= end); 9121da177e4SLinus Torvalds tlb_start_vma(tlb, vma); 9131da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 9141da177e4SLinus Torvalds do { 9151da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 91651c6f666SRobin Holt if (pgd_none_or_clear_bad(pgd)) { 91751c6f666SRobin Holt (*zap_work)--; 9181da177e4SLinus Torvalds continue; 91951c6f666SRobin Holt } 92051c6f666SRobin Holt next = zap_pud_range(tlb, vma, pgd, addr, next, 92151c6f666SRobin Holt zap_work, details); 92251c6f666SRobin Holt } while (pgd++, addr = next, (addr != end && *zap_work > 0)); 9231da177e4SLinus Torvalds tlb_end_vma(tlb, vma); 92451c6f666SRobin Holt 92551c6f666SRobin Holt return addr; 9261da177e4SLinus Torvalds } 9271da177e4SLinus Torvalds 9281da177e4SLinus Torvalds #ifdef CONFIG_PREEMPT 9291da177e4SLinus Torvalds # define ZAP_BLOCK_SIZE (8 * PAGE_SIZE) 9301da177e4SLinus Torvalds #else 9311da177e4SLinus Torvalds /* No preempt: go for improved straight-line efficiency */ 9321da177e4SLinus Torvalds # define ZAP_BLOCK_SIZE (1024 * PAGE_SIZE) 9331da177e4SLinus Torvalds #endif 9341da177e4SLinus Torvalds 9351da177e4SLinus Torvalds /** 9361da177e4SLinus Torvalds * unmap_vmas - unmap a range of memory covered by a list of vma's 9371da177e4SLinus Torvalds * @tlbp: address of the caller's struct mmu_gather 9381da177e4SLinus Torvalds * @vma: the starting vma 9391da177e4SLinus Torvalds * @start_addr: virtual address at which to start unmapping 9401da177e4SLinus Torvalds * @end_addr: virtual address at which to end unmapping 9411da177e4SLinus Torvalds * @nr_accounted: Place number of unmapped pages in vm-accountable vma's here 9421da177e4SLinus Torvalds * @details: details of nonlinear truncation or shared cache invalidation 9431da177e4SLinus Torvalds * 944ee39b37bSHugh Dickins * Returns the end address of the unmapping (restart addr if interrupted). 9451da177e4SLinus Torvalds * 946508034a3SHugh Dickins * Unmap all pages in the vma list. 9471da177e4SLinus Torvalds * 948508034a3SHugh Dickins * We aim to not hold locks for too long (for scheduling latency reasons). 949508034a3SHugh Dickins * So zap pages in ZAP_BLOCK_SIZE bytecounts. This means we need to 9501da177e4SLinus Torvalds * return the ending mmu_gather to the caller. 9511da177e4SLinus Torvalds * 9521da177e4SLinus Torvalds * Only addresses between `start' and `end' will be unmapped. 9531da177e4SLinus Torvalds * 9541da177e4SLinus Torvalds * The VMA list must be sorted in ascending virtual address order. 9551da177e4SLinus Torvalds * 9561da177e4SLinus Torvalds * unmap_vmas() assumes that the caller will flush the whole unmapped address 9571da177e4SLinus Torvalds * range after unmap_vmas() returns. So the only responsibility here is to 9581da177e4SLinus Torvalds * ensure that any thus-far unmapped pages are flushed before unmap_vmas() 9591da177e4SLinus Torvalds * drops the lock and schedules. 9601da177e4SLinus Torvalds */ 961508034a3SHugh Dickins unsigned long unmap_vmas(struct mmu_gather **tlbp, 9621da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long start_addr, 9631da177e4SLinus Torvalds unsigned long end_addr, unsigned long *nr_accounted, 9641da177e4SLinus Torvalds struct zap_details *details) 9651da177e4SLinus Torvalds { 96651c6f666SRobin Holt long zap_work = ZAP_BLOCK_SIZE; 9671da177e4SLinus Torvalds unsigned long tlb_start = 0; /* For tlb_finish_mmu */ 9681da177e4SLinus Torvalds int tlb_start_valid = 0; 969ee39b37bSHugh Dickins unsigned long start = start_addr; 9701da177e4SLinus Torvalds spinlock_t *i_mmap_lock = details? details->i_mmap_lock: NULL; 9714d6ddfa9SHugh Dickins int fullmm = (*tlbp)->fullmm; 972cddb8a5cSAndrea Arcangeli struct mm_struct *mm = vma->vm_mm; 9731da177e4SLinus Torvalds 974cddb8a5cSAndrea Arcangeli mmu_notifier_invalidate_range_start(mm, start_addr, end_addr); 9751da177e4SLinus Torvalds for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) { 9761da177e4SLinus Torvalds unsigned long end; 9771da177e4SLinus Torvalds 9781da177e4SLinus Torvalds start = max(vma->vm_start, start_addr); 9791da177e4SLinus Torvalds if (start >= vma->vm_end) 9801da177e4SLinus Torvalds continue; 9811da177e4SLinus Torvalds end = min(vma->vm_end, end_addr); 9821da177e4SLinus Torvalds if (end <= vma->vm_start) 9831da177e4SLinus Torvalds continue; 9841da177e4SLinus Torvalds 9851da177e4SLinus Torvalds if (vma->vm_flags & VM_ACCOUNT) 9861da177e4SLinus Torvalds *nr_accounted += (end - start) >> PAGE_SHIFT; 9871da177e4SLinus Torvalds 98834801ba9Svenkatesh.pallipadi@intel.com if (unlikely(is_pfn_mapping(vma))) 9892ab64037Svenkatesh.pallipadi@intel.com untrack_pfn_vma(vma, 0, 0); 9902ab64037Svenkatesh.pallipadi@intel.com 9911da177e4SLinus Torvalds while (start != end) { 9921da177e4SLinus Torvalds if (!tlb_start_valid) { 9931da177e4SLinus Torvalds tlb_start = start; 9941da177e4SLinus Torvalds tlb_start_valid = 1; 9951da177e4SLinus Torvalds } 9961da177e4SLinus Torvalds 99751c6f666SRobin Holt if (unlikely(is_vm_hugetlb_page(vma))) { 998a137e1ccSAndi Kleen /* 999a137e1ccSAndi Kleen * It is undesirable to test vma->vm_file as it 1000a137e1ccSAndi Kleen * should be non-null for valid hugetlb area. 1001a137e1ccSAndi Kleen * However, vm_file will be NULL in the error 1002a137e1ccSAndi Kleen * cleanup path of do_mmap_pgoff. When 1003a137e1ccSAndi Kleen * hugetlbfs ->mmap method fails, 1004a137e1ccSAndi Kleen * do_mmap_pgoff() nullifies vma->vm_file 1005a137e1ccSAndi Kleen * before calling this function to clean up. 1006a137e1ccSAndi Kleen * Since no pte has actually been setup, it is 1007a137e1ccSAndi Kleen * safe to do nothing in this case. 1008a137e1ccSAndi Kleen */ 1009a137e1ccSAndi Kleen if (vma->vm_file) { 101004f2cbe3SMel Gorman unmap_hugepage_range(vma, start, end, NULL); 101151c6f666SRobin Holt zap_work -= (end - start) / 1012a5516438SAndi Kleen pages_per_huge_page(hstate_vma(vma)); 1013a137e1ccSAndi Kleen } 1014a137e1ccSAndi Kleen 101551c6f666SRobin Holt start = end; 101651c6f666SRobin Holt } else 101751c6f666SRobin Holt start = unmap_page_range(*tlbp, vma, 101851c6f666SRobin Holt start, end, &zap_work, details); 10191da177e4SLinus Torvalds 102051c6f666SRobin Holt if (zap_work > 0) { 102151c6f666SRobin Holt BUG_ON(start != end); 102251c6f666SRobin Holt break; 102351c6f666SRobin Holt } 10241da177e4SLinus Torvalds 10251da177e4SLinus Torvalds tlb_finish_mmu(*tlbp, tlb_start, start); 10261da177e4SLinus Torvalds 10271da177e4SLinus Torvalds if (need_resched() || 102895c354feSNick Piggin (i_mmap_lock && spin_needbreak(i_mmap_lock))) { 10291da177e4SLinus Torvalds if (i_mmap_lock) { 1030508034a3SHugh Dickins *tlbp = NULL; 10311da177e4SLinus Torvalds goto out; 10321da177e4SLinus Torvalds } 10331da177e4SLinus Torvalds cond_resched(); 10341da177e4SLinus Torvalds } 10351da177e4SLinus Torvalds 1036508034a3SHugh Dickins *tlbp = tlb_gather_mmu(vma->vm_mm, fullmm); 10371da177e4SLinus Torvalds tlb_start_valid = 0; 103851c6f666SRobin Holt zap_work = ZAP_BLOCK_SIZE; 10391da177e4SLinus Torvalds } 10401da177e4SLinus Torvalds } 10411da177e4SLinus Torvalds out: 1042cddb8a5cSAndrea Arcangeli mmu_notifier_invalidate_range_end(mm, start_addr, end_addr); 1043ee39b37bSHugh Dickins return start; /* which is now the end (or restart) address */ 10441da177e4SLinus Torvalds } 10451da177e4SLinus Torvalds 10461da177e4SLinus Torvalds /** 10471da177e4SLinus Torvalds * zap_page_range - remove user pages in a given range 10481da177e4SLinus Torvalds * @vma: vm_area_struct holding the applicable pages 10491da177e4SLinus Torvalds * @address: starting address of pages to zap 10501da177e4SLinus Torvalds * @size: number of bytes to zap 10511da177e4SLinus Torvalds * @details: details of nonlinear truncation or shared cache invalidation 10521da177e4SLinus Torvalds */ 1053ee39b37bSHugh Dickins unsigned long zap_page_range(struct vm_area_struct *vma, unsigned long address, 10541da177e4SLinus Torvalds unsigned long size, struct zap_details *details) 10551da177e4SLinus Torvalds { 10561da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 10571da177e4SLinus Torvalds struct mmu_gather *tlb; 10581da177e4SLinus Torvalds unsigned long end = address + size; 10591da177e4SLinus Torvalds unsigned long nr_accounted = 0; 10601da177e4SLinus Torvalds 10611da177e4SLinus Torvalds lru_add_drain(); 10621da177e4SLinus Torvalds tlb = tlb_gather_mmu(mm, 0); 1063365e9c87SHugh Dickins update_hiwater_rss(mm); 1064508034a3SHugh Dickins end = unmap_vmas(&tlb, vma, address, end, &nr_accounted, details); 1065508034a3SHugh Dickins if (tlb) 10668f4f8c16SHugh Dickins tlb_finish_mmu(tlb, address, end); 1067ee39b37bSHugh Dickins return end; 10681da177e4SLinus Torvalds } 10691da177e4SLinus Torvalds 1070c627f9ccSJack Steiner /** 1071c627f9ccSJack Steiner * zap_vma_ptes - remove ptes mapping the vma 1072c627f9ccSJack Steiner * @vma: vm_area_struct holding ptes to be zapped 1073c627f9ccSJack Steiner * @address: starting address of pages to zap 1074c627f9ccSJack Steiner * @size: number of bytes to zap 1075c627f9ccSJack Steiner * 1076c627f9ccSJack Steiner * This function only unmaps ptes assigned to VM_PFNMAP vmas. 1077c627f9ccSJack Steiner * 1078c627f9ccSJack Steiner * The entire address range must be fully contained within the vma. 1079c627f9ccSJack Steiner * 1080c627f9ccSJack Steiner * Returns 0 if successful. 1081c627f9ccSJack Steiner */ 1082c627f9ccSJack Steiner int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, 1083c627f9ccSJack Steiner unsigned long size) 1084c627f9ccSJack Steiner { 1085c627f9ccSJack Steiner if (address < vma->vm_start || address + size > vma->vm_end || 1086c627f9ccSJack Steiner !(vma->vm_flags & VM_PFNMAP)) 1087c627f9ccSJack Steiner return -1; 1088c627f9ccSJack Steiner zap_page_range(vma, address, size, NULL); 1089c627f9ccSJack Steiner return 0; 1090c627f9ccSJack Steiner } 1091c627f9ccSJack Steiner EXPORT_SYMBOL_GPL(zap_vma_ptes); 1092c627f9ccSJack Steiner 10931da177e4SLinus Torvalds /* 10941da177e4SLinus Torvalds * Do a quick page-table lookup for a single page. 10951da177e4SLinus Torvalds */ 10966aab341eSLinus Torvalds struct page *follow_page(struct vm_area_struct *vma, unsigned long address, 1097deceb6cdSHugh Dickins unsigned int flags) 10981da177e4SLinus Torvalds { 10991da177e4SLinus Torvalds pgd_t *pgd; 11001da177e4SLinus Torvalds pud_t *pud; 11011da177e4SLinus Torvalds pmd_t *pmd; 11021da177e4SLinus Torvalds pte_t *ptep, pte; 1103deceb6cdSHugh Dickins spinlock_t *ptl; 11041da177e4SLinus Torvalds struct page *page; 11056aab341eSLinus Torvalds struct mm_struct *mm = vma->vm_mm; 11061da177e4SLinus Torvalds 1107deceb6cdSHugh Dickins page = follow_huge_addr(mm, address, flags & FOLL_WRITE); 1108deceb6cdSHugh Dickins if (!IS_ERR(page)) { 1109deceb6cdSHugh Dickins BUG_ON(flags & FOLL_GET); 1110deceb6cdSHugh Dickins goto out; 1111deceb6cdSHugh Dickins } 11121da177e4SLinus Torvalds 1113deceb6cdSHugh Dickins page = NULL; 11141da177e4SLinus Torvalds pgd = pgd_offset(mm, address); 11151da177e4SLinus Torvalds if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) 1116deceb6cdSHugh Dickins goto no_page_table; 11171da177e4SLinus Torvalds 11181da177e4SLinus Torvalds pud = pud_offset(pgd, address); 1119ceb86879SAndi Kleen if (pud_none(*pud)) 1120ceb86879SAndi Kleen goto no_page_table; 1121ceb86879SAndi Kleen if (pud_huge(*pud)) { 1122ceb86879SAndi Kleen BUG_ON(flags & FOLL_GET); 1123ceb86879SAndi Kleen page = follow_huge_pud(mm, address, pud, flags & FOLL_WRITE); 1124ceb86879SAndi Kleen goto out; 1125ceb86879SAndi Kleen } 1126ceb86879SAndi Kleen if (unlikely(pud_bad(*pud))) 1127deceb6cdSHugh Dickins goto no_page_table; 11281da177e4SLinus Torvalds 11291da177e4SLinus Torvalds pmd = pmd_offset(pud, address); 1130aeed5fceSHugh Dickins if (pmd_none(*pmd)) 1131deceb6cdSHugh Dickins goto no_page_table; 1132deceb6cdSHugh Dickins if (pmd_huge(*pmd)) { 1133deceb6cdSHugh Dickins BUG_ON(flags & FOLL_GET); 1134deceb6cdSHugh Dickins page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE); 1135deceb6cdSHugh Dickins goto out; 1136deceb6cdSHugh Dickins } 1137aeed5fceSHugh Dickins if (unlikely(pmd_bad(*pmd))) 1138aeed5fceSHugh Dickins goto no_page_table; 1139aeed5fceSHugh Dickins 1140deceb6cdSHugh Dickins ptep = pte_offset_map_lock(mm, pmd, address, &ptl); 11411da177e4SLinus Torvalds 11421da177e4SLinus Torvalds pte = *ptep; 1143deceb6cdSHugh Dickins if (!pte_present(pte)) 114489f5b7daSLinus Torvalds goto no_page; 1145deceb6cdSHugh Dickins if ((flags & FOLL_WRITE) && !pte_write(pte)) 1146deceb6cdSHugh Dickins goto unlock; 11476aab341eSLinus Torvalds page = vm_normal_page(vma, address, pte); 11486aab341eSLinus Torvalds if (unlikely(!page)) 114989f5b7daSLinus Torvalds goto bad_page; 1150deceb6cdSHugh Dickins 1151deceb6cdSHugh Dickins if (flags & FOLL_GET) 1152deceb6cdSHugh Dickins get_page(page); 1153deceb6cdSHugh Dickins if (flags & FOLL_TOUCH) { 1154deceb6cdSHugh Dickins if ((flags & FOLL_WRITE) && 1155deceb6cdSHugh Dickins !pte_dirty(pte) && !PageDirty(page)) 1156f33ea7f4SNick Piggin set_page_dirty(page); 1157bd775c42SKOSAKI Motohiro /* 1158bd775c42SKOSAKI Motohiro * pte_mkyoung() would be more correct here, but atomic care 1159bd775c42SKOSAKI Motohiro * is needed to avoid losing the dirty bit: it is easier to use 1160bd775c42SKOSAKI Motohiro * mark_page_accessed(). 1161bd775c42SKOSAKI Motohiro */ 11621da177e4SLinus Torvalds mark_page_accessed(page); 11631da177e4SLinus Torvalds } 1164deceb6cdSHugh Dickins unlock: 1165deceb6cdSHugh Dickins pte_unmap_unlock(ptep, ptl); 11661da177e4SLinus Torvalds out: 1167deceb6cdSHugh Dickins return page; 1168deceb6cdSHugh Dickins 116989f5b7daSLinus Torvalds bad_page: 117089f5b7daSLinus Torvalds pte_unmap_unlock(ptep, ptl); 117189f5b7daSLinus Torvalds return ERR_PTR(-EFAULT); 117289f5b7daSLinus Torvalds 117389f5b7daSLinus Torvalds no_page: 117489f5b7daSLinus Torvalds pte_unmap_unlock(ptep, ptl); 117589f5b7daSLinus Torvalds if (!pte_none(pte)) 117689f5b7daSLinus Torvalds return page; 117789f5b7daSLinus Torvalds /* Fall through to ZERO_PAGE handling */ 1178deceb6cdSHugh Dickins no_page_table: 1179deceb6cdSHugh Dickins /* 1180deceb6cdSHugh Dickins * When core dumping an enormous anonymous area that nobody 1181deceb6cdSHugh Dickins * has touched so far, we don't want to allocate page tables. 1182deceb6cdSHugh Dickins */ 1183deceb6cdSHugh Dickins if (flags & FOLL_ANON) { 1184557ed1faSNick Piggin page = ZERO_PAGE(0); 1185deceb6cdSHugh Dickins if (flags & FOLL_GET) 1186deceb6cdSHugh Dickins get_page(page); 1187deceb6cdSHugh Dickins BUG_ON(flags & FOLL_WRITE); 11881da177e4SLinus Torvalds } 1189deceb6cdSHugh Dickins return page; 11901da177e4SLinus Torvalds } 11911da177e4SLinus Torvalds 1192672ca28eSLinus Torvalds /* Can we do the FOLL_ANON optimization? */ 1193672ca28eSLinus Torvalds static inline int use_zero_page(struct vm_area_struct *vma) 1194672ca28eSLinus Torvalds { 1195672ca28eSLinus Torvalds /* 1196672ca28eSLinus Torvalds * We don't want to optimize FOLL_ANON for make_pages_present() 1197672ca28eSLinus Torvalds * when it tries to page in a VM_LOCKED region. As to VM_SHARED, 1198672ca28eSLinus Torvalds * we want to get the page from the page tables to make sure 1199672ca28eSLinus Torvalds * that we serialize and update with any other user of that 1200672ca28eSLinus Torvalds * mapping. 1201672ca28eSLinus Torvalds */ 1202672ca28eSLinus Torvalds if (vma->vm_flags & (VM_LOCKED | VM_SHARED)) 1203672ca28eSLinus Torvalds return 0; 1204672ca28eSLinus Torvalds /* 12050d71d10aSNick Piggin * And if we have a fault routine, it's not an anonymous region. 1206672ca28eSLinus Torvalds */ 12070d71d10aSNick Piggin return !vma->vm_ops || !vma->vm_ops->fault; 1208672ca28eSLinus Torvalds } 1209672ca28eSLinus Torvalds 1210b291f000SNick Piggin 1211b291f000SNick Piggin 1212b291f000SNick Piggin int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm, 12139d73777eSPeter Zijlstra unsigned long start, int nr_pages, int flags, 12141da177e4SLinus Torvalds struct page **pages, struct vm_area_struct **vmas) 12151da177e4SLinus Torvalds { 12161da177e4SLinus Torvalds int i; 1217b291f000SNick Piggin unsigned int vm_flags = 0; 1218b291f000SNick Piggin int write = !!(flags & GUP_FLAGS_WRITE); 1219b291f000SNick Piggin int force = !!(flags & GUP_FLAGS_FORCE); 1220b291f000SNick Piggin int ignore = !!(flags & GUP_FLAGS_IGNORE_VMA_PERMISSIONS); 12214779280dSYing Han int ignore_sigkill = !!(flags & GUP_FLAGS_IGNORE_SIGKILL); 12221da177e4SLinus Torvalds 12239d73777eSPeter Zijlstra if (nr_pages <= 0) 1224900cf086SJonathan Corbet return 0; 12251da177e4SLinus Torvalds /* 12261da177e4SLinus Torvalds * Require read or write permissions. 12271da177e4SLinus Torvalds * If 'force' is set, we only require the "MAY" flags. 12281da177e4SLinus Torvalds */ 1229deceb6cdSHugh Dickins vm_flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD); 1230deceb6cdSHugh Dickins vm_flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE); 12311da177e4SLinus Torvalds i = 0; 12321da177e4SLinus Torvalds 12331da177e4SLinus Torvalds do { 12341da177e4SLinus Torvalds struct vm_area_struct *vma; 1235deceb6cdSHugh Dickins unsigned int foll_flags; 12361da177e4SLinus Torvalds 12371da177e4SLinus Torvalds vma = find_extend_vma(mm, start); 12381da177e4SLinus Torvalds if (!vma && in_gate_area(tsk, start)) { 12391da177e4SLinus Torvalds unsigned long pg = start & PAGE_MASK; 12401da177e4SLinus Torvalds struct vm_area_struct *gate_vma = get_gate_vma(tsk); 12411da177e4SLinus Torvalds pgd_t *pgd; 12421da177e4SLinus Torvalds pud_t *pud; 12431da177e4SLinus Torvalds pmd_t *pmd; 12441da177e4SLinus Torvalds pte_t *pte; 1245b291f000SNick Piggin 1246b291f000SNick Piggin /* user gate pages are read-only */ 1247b291f000SNick Piggin if (!ignore && write) 12481da177e4SLinus Torvalds return i ? : -EFAULT; 12491da177e4SLinus Torvalds if (pg > TASK_SIZE) 12501da177e4SLinus Torvalds pgd = pgd_offset_k(pg); 12511da177e4SLinus Torvalds else 12521da177e4SLinus Torvalds pgd = pgd_offset_gate(mm, pg); 12531da177e4SLinus Torvalds BUG_ON(pgd_none(*pgd)); 12541da177e4SLinus Torvalds pud = pud_offset(pgd, pg); 12551da177e4SLinus Torvalds BUG_ON(pud_none(*pud)); 12561da177e4SLinus Torvalds pmd = pmd_offset(pud, pg); 1257690dbe1cSHugh Dickins if (pmd_none(*pmd)) 1258690dbe1cSHugh Dickins return i ? : -EFAULT; 12591da177e4SLinus Torvalds pte = pte_offset_map(pmd, pg); 1260690dbe1cSHugh Dickins if (pte_none(*pte)) { 1261690dbe1cSHugh Dickins pte_unmap(pte); 1262690dbe1cSHugh Dickins return i ? : -EFAULT; 1263690dbe1cSHugh Dickins } 12641da177e4SLinus Torvalds if (pages) { 1265fa2a455bSNick Piggin struct page *page = vm_normal_page(gate_vma, start, *pte); 12666aab341eSLinus Torvalds pages[i] = page; 12676aab341eSLinus Torvalds if (page) 12686aab341eSLinus Torvalds get_page(page); 12691da177e4SLinus Torvalds } 12701da177e4SLinus Torvalds pte_unmap(pte); 12711da177e4SLinus Torvalds if (vmas) 12721da177e4SLinus Torvalds vmas[i] = gate_vma; 12731da177e4SLinus Torvalds i++; 12741da177e4SLinus Torvalds start += PAGE_SIZE; 12759d73777eSPeter Zijlstra nr_pages--; 12761da177e4SLinus Torvalds continue; 12771da177e4SLinus Torvalds } 12781da177e4SLinus Torvalds 1279b291f000SNick Piggin if (!vma || 1280b291f000SNick Piggin (vma->vm_flags & (VM_IO | VM_PFNMAP)) || 1281b291f000SNick Piggin (!ignore && !(vm_flags & vma->vm_flags))) 12821da177e4SLinus Torvalds return i ? : -EFAULT; 12831da177e4SLinus Torvalds 12841da177e4SLinus Torvalds if (is_vm_hugetlb_page(vma)) { 12851da177e4SLinus Torvalds i = follow_hugetlb_page(mm, vma, pages, vmas, 12869d73777eSPeter Zijlstra &start, &nr_pages, i, write); 12871da177e4SLinus Torvalds continue; 12881da177e4SLinus Torvalds } 1289deceb6cdSHugh Dickins 1290deceb6cdSHugh Dickins foll_flags = FOLL_TOUCH; 1291deceb6cdSHugh Dickins if (pages) 1292deceb6cdSHugh Dickins foll_flags |= FOLL_GET; 1293672ca28eSLinus Torvalds if (!write && use_zero_page(vma)) 1294deceb6cdSHugh Dickins foll_flags |= FOLL_ANON; 1295deceb6cdSHugh Dickins 12961da177e4SLinus Torvalds do { 129708ef4729SHugh Dickins struct page *page; 12981da177e4SLinus Torvalds 1299462e00ccSEthan Solomita /* 13004779280dSYing Han * If we have a pending SIGKILL, don't keep faulting 13014779280dSYing Han * pages and potentially allocating memory, unless 13024779280dSYing Han * current is handling munlock--e.g., on exit. In 13034779280dSYing Han * that case, we are not allocating memory. Rather, 13044779280dSYing Han * we're only unlocking already resident/mapped pages. 1305462e00ccSEthan Solomita */ 13064779280dSYing Han if (unlikely(!ignore_sigkill && 13074779280dSYing Han fatal_signal_pending(current))) 13084779280dSYing Han return i ? i : -ERESTARTSYS; 1309462e00ccSEthan Solomita 1310deceb6cdSHugh Dickins if (write) 1311deceb6cdSHugh Dickins foll_flags |= FOLL_WRITE; 1312deceb6cdSHugh Dickins 1313deceb6cdSHugh Dickins cond_resched(); 13146aab341eSLinus Torvalds while (!(page = follow_page(vma, start, foll_flags))) { 1315a68d2ebcSLinus Torvalds int ret; 1316d06063ccSLinus Torvalds 1317d26ed650SHugh Dickins ret = handle_mm_fault(mm, vma, start, 1318d26ed650SHugh Dickins (foll_flags & FOLL_WRITE) ? 1319d26ed650SHugh Dickins FAULT_FLAG_WRITE : 0); 1320d26ed650SHugh Dickins 132183c54070SNick Piggin if (ret & VM_FAULT_ERROR) { 132283c54070SNick Piggin if (ret & VM_FAULT_OOM) 132383c54070SNick Piggin return i ? i : -ENOMEM; 132483c54070SNick Piggin else if (ret & VM_FAULT_SIGBUS) 132583c54070SNick Piggin return i ? i : -EFAULT; 132683c54070SNick Piggin BUG(); 132783c54070SNick Piggin } 132883c54070SNick Piggin if (ret & VM_FAULT_MAJOR) 132983c54070SNick Piggin tsk->maj_flt++; 133083c54070SNick Piggin else 133183c54070SNick Piggin tsk->min_flt++; 133283c54070SNick Piggin 1333f33ea7f4SNick Piggin /* 133483c54070SNick Piggin * The VM_FAULT_WRITE bit tells us that 133583c54070SNick Piggin * do_wp_page has broken COW when necessary, 133683c54070SNick Piggin * even if maybe_mkwrite decided not to set 133783c54070SNick Piggin * pte_write. We can thus safely do subsequent 1338878b63acSHugh Dickins * page lookups as if they were reads. But only 1339878b63acSHugh Dickins * do so when looping for pte_write is futile: 1340878b63acSHugh Dickins * in some cases userspace may also be wanting 1341878b63acSHugh Dickins * to write to the gotten user page, which a 1342878b63acSHugh Dickins * read fault here might prevent (a readonly 1343878b63acSHugh Dickins * page might get reCOWed by userspace write). 1344f33ea7f4SNick Piggin */ 1345878b63acSHugh Dickins if ((ret & VM_FAULT_WRITE) && 1346878b63acSHugh Dickins !(vma->vm_flags & VM_WRITE)) 1347deceb6cdSHugh Dickins foll_flags &= ~FOLL_WRITE; 1348a68d2ebcSLinus Torvalds 13497f7bbbe5SBenjamin Herrenschmidt cond_resched(); 13501da177e4SLinus Torvalds } 135189f5b7daSLinus Torvalds if (IS_ERR(page)) 135289f5b7daSLinus Torvalds return i ? i : PTR_ERR(page); 13531da177e4SLinus Torvalds if (pages) { 135408ef4729SHugh Dickins pages[i] = page; 135503beb076SJames Bottomley 1356a6f36be3SRussell King flush_anon_page(vma, page, start); 135708ef4729SHugh Dickins flush_dcache_page(page); 13581da177e4SLinus Torvalds } 13591da177e4SLinus Torvalds if (vmas) 13601da177e4SLinus Torvalds vmas[i] = vma; 13611da177e4SLinus Torvalds i++; 13621da177e4SLinus Torvalds start += PAGE_SIZE; 13639d73777eSPeter Zijlstra nr_pages--; 13649d73777eSPeter Zijlstra } while (nr_pages && start < vma->vm_end); 13659d73777eSPeter Zijlstra } while (nr_pages); 13661da177e4SLinus Torvalds return i; 13671da177e4SLinus Torvalds } 1368b291f000SNick Piggin 1369d2bf6be8SNick Piggin /** 1370d2bf6be8SNick Piggin * get_user_pages() - pin user pages in memory 1371d2bf6be8SNick Piggin * @tsk: task_struct of target task 1372d2bf6be8SNick Piggin * @mm: mm_struct of target mm 1373d2bf6be8SNick Piggin * @start: starting user address 13749d73777eSPeter Zijlstra * @nr_pages: number of pages from start to pin 1375d2bf6be8SNick Piggin * @write: whether pages will be written to by the caller 1376d2bf6be8SNick Piggin * @force: whether to force write access even if user mapping is 1377d2bf6be8SNick Piggin * readonly. This will result in the page being COWed even 1378d2bf6be8SNick Piggin * in MAP_SHARED mappings. You do not want this. 1379d2bf6be8SNick Piggin * @pages: array that receives pointers to the pages pinned. 1380d2bf6be8SNick Piggin * Should be at least nr_pages long. Or NULL, if caller 1381d2bf6be8SNick Piggin * only intends to ensure the pages are faulted in. 1382d2bf6be8SNick Piggin * @vmas: array of pointers to vmas corresponding to each page. 1383d2bf6be8SNick Piggin * Or NULL if the caller does not require them. 1384d2bf6be8SNick Piggin * 1385d2bf6be8SNick Piggin * Returns number of pages pinned. This may be fewer than the number 13869d73777eSPeter Zijlstra * requested. If nr_pages is 0 or negative, returns 0. If no pages 1387d2bf6be8SNick Piggin * were pinned, returns -errno. Each page returned must be released 1388d2bf6be8SNick Piggin * with a put_page() call when it is finished with. vmas will only 1389d2bf6be8SNick Piggin * remain valid while mmap_sem is held. 1390d2bf6be8SNick Piggin * 1391d2bf6be8SNick Piggin * Must be called with mmap_sem held for read or write. 1392d2bf6be8SNick Piggin * 1393d2bf6be8SNick Piggin * get_user_pages walks a process's page tables and takes a reference to 1394d2bf6be8SNick Piggin * each struct page that each user address corresponds to at a given 1395d2bf6be8SNick Piggin * instant. That is, it takes the page that would be accessed if a user 1396d2bf6be8SNick Piggin * thread accesses the given user virtual address at that instant. 1397d2bf6be8SNick Piggin * 1398d2bf6be8SNick Piggin * This does not guarantee that the page exists in the user mappings when 1399d2bf6be8SNick Piggin * get_user_pages returns, and there may even be a completely different 1400d2bf6be8SNick Piggin * page there in some cases (eg. if mmapped pagecache has been invalidated 1401d2bf6be8SNick Piggin * and subsequently re faulted). However it does guarantee that the page 1402d2bf6be8SNick Piggin * won't be freed completely. And mostly callers simply care that the page 1403d2bf6be8SNick Piggin * contains data that was valid *at some point in time*. Typically, an IO 1404d2bf6be8SNick Piggin * or similar operation cannot guarantee anything stronger anyway because 1405d2bf6be8SNick Piggin * locks can't be held over the syscall boundary. 1406d2bf6be8SNick Piggin * 1407d2bf6be8SNick Piggin * If write=0, the page must not be written to. If the page is written to, 1408d2bf6be8SNick Piggin * set_page_dirty (or set_page_dirty_lock, as appropriate) must be called 1409d2bf6be8SNick Piggin * after the page is finished with, and before put_page is called. 1410d2bf6be8SNick Piggin * 1411d2bf6be8SNick Piggin * get_user_pages is typically used for fewer-copy IO operations, to get a 1412d2bf6be8SNick Piggin * handle on the memory by some means other than accesses via the user virtual 1413d2bf6be8SNick Piggin * addresses. The pages may be submitted for DMA to devices or accessed via 1414d2bf6be8SNick Piggin * their kernel linear mapping (via the kmap APIs). Care should be taken to 1415d2bf6be8SNick Piggin * use the correct cache flushing APIs. 1416d2bf6be8SNick Piggin * 1417d2bf6be8SNick Piggin * See also get_user_pages_fast, for performance critical applications. 1418d2bf6be8SNick Piggin */ 1419b291f000SNick Piggin int get_user_pages(struct task_struct *tsk, struct mm_struct *mm, 14209d73777eSPeter Zijlstra unsigned long start, int nr_pages, int write, int force, 1421b291f000SNick Piggin struct page **pages, struct vm_area_struct **vmas) 1422b291f000SNick Piggin { 1423b291f000SNick Piggin int flags = 0; 1424b291f000SNick Piggin 1425b291f000SNick Piggin if (write) 1426b291f000SNick Piggin flags |= GUP_FLAGS_WRITE; 1427b291f000SNick Piggin if (force) 1428b291f000SNick Piggin flags |= GUP_FLAGS_FORCE; 1429b291f000SNick Piggin 14309d73777eSPeter Zijlstra return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas); 1431b291f000SNick Piggin } 1432b291f000SNick Piggin 14331da177e4SLinus Torvalds EXPORT_SYMBOL(get_user_pages); 14341da177e4SLinus Torvalds 1435920c7a5dSHarvey Harrison pte_t *get_locked_pte(struct mm_struct *mm, unsigned long addr, 1436920c7a5dSHarvey Harrison spinlock_t **ptl) 1437c9cfcddfSLinus Torvalds { 1438c9cfcddfSLinus Torvalds pgd_t * pgd = pgd_offset(mm, addr); 1439c9cfcddfSLinus Torvalds pud_t * pud = pud_alloc(mm, pgd, addr); 1440c9cfcddfSLinus Torvalds if (pud) { 144149c91fb0STrond Myklebust pmd_t * pmd = pmd_alloc(mm, pud, addr); 1442c9cfcddfSLinus Torvalds if (pmd) 1443c9cfcddfSLinus Torvalds return pte_alloc_map_lock(mm, pmd, addr, ptl); 1444c9cfcddfSLinus Torvalds } 1445c9cfcddfSLinus Torvalds return NULL; 1446c9cfcddfSLinus Torvalds } 1447c9cfcddfSLinus Torvalds 14481da177e4SLinus Torvalds /* 1449238f58d8SLinus Torvalds * This is the old fallback for page remapping. 1450238f58d8SLinus Torvalds * 1451238f58d8SLinus Torvalds * For historical reasons, it only allows reserved pages. Only 1452238f58d8SLinus Torvalds * old drivers should use this, and they needed to mark their 1453238f58d8SLinus Torvalds * pages reserved for the old functions anyway. 1454238f58d8SLinus Torvalds */ 1455423bad60SNick Piggin static int insert_page(struct vm_area_struct *vma, unsigned long addr, 1456423bad60SNick Piggin struct page *page, pgprot_t prot) 1457238f58d8SLinus Torvalds { 1458423bad60SNick Piggin struct mm_struct *mm = vma->vm_mm; 1459238f58d8SLinus Torvalds int retval; 1460238f58d8SLinus Torvalds pte_t *pte; 1461238f58d8SLinus Torvalds spinlock_t *ptl; 1462238f58d8SLinus Torvalds 1463238f58d8SLinus Torvalds retval = -EINVAL; 1464a145dd41SLinus Torvalds if (PageAnon(page)) 14655b4e655eSKAMEZAWA Hiroyuki goto out; 1466238f58d8SLinus Torvalds retval = -ENOMEM; 1467238f58d8SLinus Torvalds flush_dcache_page(page); 1468c9cfcddfSLinus Torvalds pte = get_locked_pte(mm, addr, &ptl); 1469238f58d8SLinus Torvalds if (!pte) 14705b4e655eSKAMEZAWA Hiroyuki goto out; 1471238f58d8SLinus Torvalds retval = -EBUSY; 1472238f58d8SLinus Torvalds if (!pte_none(*pte)) 1473238f58d8SLinus Torvalds goto out_unlock; 1474238f58d8SLinus Torvalds 1475238f58d8SLinus Torvalds /* Ok, finally just insert the thing.. */ 1476238f58d8SLinus Torvalds get_page(page); 1477238f58d8SLinus Torvalds inc_mm_counter(mm, file_rss); 1478238f58d8SLinus Torvalds page_add_file_rmap(page); 1479238f58d8SLinus Torvalds set_pte_at(mm, addr, pte, mk_pte(page, prot)); 1480238f58d8SLinus Torvalds 1481238f58d8SLinus Torvalds retval = 0; 14828a9f3ccdSBalbir Singh pte_unmap_unlock(pte, ptl); 14838a9f3ccdSBalbir Singh return retval; 1484238f58d8SLinus Torvalds out_unlock: 1485238f58d8SLinus Torvalds pte_unmap_unlock(pte, ptl); 1486238f58d8SLinus Torvalds out: 1487238f58d8SLinus Torvalds return retval; 1488238f58d8SLinus Torvalds } 1489238f58d8SLinus Torvalds 1490bfa5bf6dSRolf Eike Beer /** 1491bfa5bf6dSRolf Eike Beer * vm_insert_page - insert single page into user vma 1492bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 1493bfa5bf6dSRolf Eike Beer * @addr: target user address of this page 1494bfa5bf6dSRolf Eike Beer * @page: source kernel page 1495bfa5bf6dSRolf Eike Beer * 1496a145dd41SLinus Torvalds * This allows drivers to insert individual pages they've allocated 1497a145dd41SLinus Torvalds * into a user vma. 1498a145dd41SLinus Torvalds * 1499a145dd41SLinus Torvalds * The page has to be a nice clean _individual_ kernel allocation. 1500a145dd41SLinus Torvalds * If you allocate a compound page, you need to have marked it as 1501a145dd41SLinus Torvalds * such (__GFP_COMP), or manually just split the page up yourself 15028dfcc9baSNick Piggin * (see split_page()). 1503a145dd41SLinus Torvalds * 1504a145dd41SLinus Torvalds * NOTE! Traditionally this was done with "remap_pfn_range()" which 1505a145dd41SLinus Torvalds * took an arbitrary page protection parameter. This doesn't allow 1506a145dd41SLinus Torvalds * that. Your vma protection will have to be set up correctly, which 1507a145dd41SLinus Torvalds * means that if you want a shared writable mapping, you'd better 1508a145dd41SLinus Torvalds * ask for a shared writable mapping! 1509a145dd41SLinus Torvalds * 1510a145dd41SLinus Torvalds * The page does not need to be reserved. 1511a145dd41SLinus Torvalds */ 1512423bad60SNick Piggin int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, 1513423bad60SNick Piggin struct page *page) 1514a145dd41SLinus Torvalds { 1515a145dd41SLinus Torvalds if (addr < vma->vm_start || addr >= vma->vm_end) 1516a145dd41SLinus Torvalds return -EFAULT; 1517a145dd41SLinus Torvalds if (!page_count(page)) 1518a145dd41SLinus Torvalds return -EINVAL; 15194d7672b4SLinus Torvalds vma->vm_flags |= VM_INSERTPAGE; 1520423bad60SNick Piggin return insert_page(vma, addr, page, vma->vm_page_prot); 1521a145dd41SLinus Torvalds } 1522e3c3374fSLinus Torvalds EXPORT_SYMBOL(vm_insert_page); 1523a145dd41SLinus Torvalds 1524423bad60SNick Piggin static int insert_pfn(struct vm_area_struct *vma, unsigned long addr, 1525423bad60SNick Piggin unsigned long pfn, pgprot_t prot) 1526423bad60SNick Piggin { 1527423bad60SNick Piggin struct mm_struct *mm = vma->vm_mm; 1528423bad60SNick Piggin int retval; 1529423bad60SNick Piggin pte_t *pte, entry; 1530423bad60SNick Piggin spinlock_t *ptl; 1531423bad60SNick Piggin 1532423bad60SNick Piggin retval = -ENOMEM; 1533423bad60SNick Piggin pte = get_locked_pte(mm, addr, &ptl); 1534423bad60SNick Piggin if (!pte) 1535423bad60SNick Piggin goto out; 1536423bad60SNick Piggin retval = -EBUSY; 1537423bad60SNick Piggin if (!pte_none(*pte)) 1538423bad60SNick Piggin goto out_unlock; 1539423bad60SNick Piggin 1540423bad60SNick Piggin /* Ok, finally just insert the thing.. */ 1541423bad60SNick Piggin entry = pte_mkspecial(pfn_pte(pfn, prot)); 1542423bad60SNick Piggin set_pte_at(mm, addr, pte, entry); 1543423bad60SNick Piggin update_mmu_cache(vma, addr, entry); /* XXX: why not for insert_page? */ 1544423bad60SNick Piggin 1545423bad60SNick Piggin retval = 0; 1546423bad60SNick Piggin out_unlock: 1547423bad60SNick Piggin pte_unmap_unlock(pte, ptl); 1548423bad60SNick Piggin out: 1549423bad60SNick Piggin return retval; 1550423bad60SNick Piggin } 1551423bad60SNick Piggin 1552e0dc0d8fSNick Piggin /** 1553e0dc0d8fSNick Piggin * vm_insert_pfn - insert single pfn into user vma 1554e0dc0d8fSNick Piggin * @vma: user vma to map to 1555e0dc0d8fSNick Piggin * @addr: target user address of this page 1556e0dc0d8fSNick Piggin * @pfn: source kernel pfn 1557e0dc0d8fSNick Piggin * 1558e0dc0d8fSNick Piggin * Similar to vm_inert_page, this allows drivers to insert individual pages 1559e0dc0d8fSNick Piggin * they've allocated into a user vma. Same comments apply. 1560e0dc0d8fSNick Piggin * 1561e0dc0d8fSNick Piggin * This function should only be called from a vm_ops->fault handler, and 1562e0dc0d8fSNick Piggin * in that case the handler should return NULL. 15630d71d10aSNick Piggin * 15640d71d10aSNick Piggin * vma cannot be a COW mapping. 15650d71d10aSNick Piggin * 15660d71d10aSNick Piggin * As this is called only for pages that do not currently exist, we 15670d71d10aSNick Piggin * do not need to flush old virtual caches or the TLB. 1568e0dc0d8fSNick Piggin */ 1569e0dc0d8fSNick Piggin int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr, 1570e0dc0d8fSNick Piggin unsigned long pfn) 1571e0dc0d8fSNick Piggin { 15722ab64037Svenkatesh.pallipadi@intel.com int ret; 1573e4b866edSvenkatesh.pallipadi@intel.com pgprot_t pgprot = vma->vm_page_prot; 15747e675137SNick Piggin /* 15757e675137SNick Piggin * Technically, architectures with pte_special can avoid all these 15767e675137SNick Piggin * restrictions (same for remap_pfn_range). However we would like 15777e675137SNick Piggin * consistency in testing and feature parity among all, so we should 15787e675137SNick Piggin * try to keep these invariants in place for everybody. 15797e675137SNick Piggin */ 1580b379d790SJared Hulbert BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))); 1581b379d790SJared Hulbert BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) == 1582b379d790SJared Hulbert (VM_PFNMAP|VM_MIXEDMAP)); 1583b379d790SJared Hulbert BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); 1584b379d790SJared Hulbert BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn)); 1585e0dc0d8fSNick Piggin 1586423bad60SNick Piggin if (addr < vma->vm_start || addr >= vma->vm_end) 1587423bad60SNick Piggin return -EFAULT; 1588e4b866edSvenkatesh.pallipadi@intel.com if (track_pfn_vma_new(vma, &pgprot, pfn, PAGE_SIZE)) 15892ab64037Svenkatesh.pallipadi@intel.com return -EINVAL; 15902ab64037Svenkatesh.pallipadi@intel.com 1591e4b866edSvenkatesh.pallipadi@intel.com ret = insert_pfn(vma, addr, pfn, pgprot); 15922ab64037Svenkatesh.pallipadi@intel.com 15932ab64037Svenkatesh.pallipadi@intel.com if (ret) 15942ab64037Svenkatesh.pallipadi@intel.com untrack_pfn_vma(vma, pfn, PAGE_SIZE); 15952ab64037Svenkatesh.pallipadi@intel.com 15962ab64037Svenkatesh.pallipadi@intel.com return ret; 1597e0dc0d8fSNick Piggin } 1598e0dc0d8fSNick Piggin EXPORT_SYMBOL(vm_insert_pfn); 1599e0dc0d8fSNick Piggin 1600423bad60SNick Piggin int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr, 1601423bad60SNick Piggin unsigned long pfn) 1602423bad60SNick Piggin { 1603423bad60SNick Piggin BUG_ON(!(vma->vm_flags & VM_MIXEDMAP)); 1604423bad60SNick Piggin 1605423bad60SNick Piggin if (addr < vma->vm_start || addr >= vma->vm_end) 1606423bad60SNick Piggin return -EFAULT; 1607423bad60SNick Piggin 1608423bad60SNick Piggin /* 1609423bad60SNick Piggin * If we don't have pte special, then we have to use the pfn_valid() 1610423bad60SNick Piggin * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must* 1611423bad60SNick Piggin * refcount the page if pfn_valid is true (hence insert_page rather 1612423bad60SNick Piggin * than insert_pfn). 1613423bad60SNick Piggin */ 1614423bad60SNick Piggin if (!HAVE_PTE_SPECIAL && pfn_valid(pfn)) { 1615423bad60SNick Piggin struct page *page; 1616423bad60SNick Piggin 1617423bad60SNick Piggin page = pfn_to_page(pfn); 1618423bad60SNick Piggin return insert_page(vma, addr, page, vma->vm_page_prot); 1619423bad60SNick Piggin } 1620423bad60SNick Piggin return insert_pfn(vma, addr, pfn, vma->vm_page_prot); 1621423bad60SNick Piggin } 1622423bad60SNick Piggin EXPORT_SYMBOL(vm_insert_mixed); 1623423bad60SNick Piggin 1624a145dd41SLinus Torvalds /* 16251da177e4SLinus Torvalds * maps a range of physical memory into the requested pages. the old 16261da177e4SLinus Torvalds * mappings are removed. any references to nonexistent pages results 16271da177e4SLinus Torvalds * in null mappings (currently treated as "copy-on-access") 16281da177e4SLinus Torvalds */ 16291da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd, 16301da177e4SLinus Torvalds unsigned long addr, unsigned long end, 16311da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 16321da177e4SLinus Torvalds { 16331da177e4SLinus Torvalds pte_t *pte; 1634c74df32cSHugh Dickins spinlock_t *ptl; 16351da177e4SLinus Torvalds 1636c74df32cSHugh Dickins pte = pte_alloc_map_lock(mm, pmd, addr, &ptl); 16371da177e4SLinus Torvalds if (!pte) 16381da177e4SLinus Torvalds return -ENOMEM; 16396606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 16401da177e4SLinus Torvalds do { 16411da177e4SLinus Torvalds BUG_ON(!pte_none(*pte)); 16427e675137SNick Piggin set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot))); 16431da177e4SLinus Torvalds pfn++; 16441da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 16456606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 1646c74df32cSHugh Dickins pte_unmap_unlock(pte - 1, ptl); 16471da177e4SLinus Torvalds return 0; 16481da177e4SLinus Torvalds } 16491da177e4SLinus Torvalds 16501da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud, 16511da177e4SLinus Torvalds unsigned long addr, unsigned long end, 16521da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 16531da177e4SLinus Torvalds { 16541da177e4SLinus Torvalds pmd_t *pmd; 16551da177e4SLinus Torvalds unsigned long next; 16561da177e4SLinus Torvalds 16571da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 16581da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, addr); 16591da177e4SLinus Torvalds if (!pmd) 16601da177e4SLinus Torvalds return -ENOMEM; 16611da177e4SLinus Torvalds do { 16621da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 16631da177e4SLinus Torvalds if (remap_pte_range(mm, pmd, addr, next, 16641da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot)) 16651da177e4SLinus Torvalds return -ENOMEM; 16661da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 16671da177e4SLinus Torvalds return 0; 16681da177e4SLinus Torvalds } 16691da177e4SLinus Torvalds 16701da177e4SLinus Torvalds static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd, 16711da177e4SLinus Torvalds unsigned long addr, unsigned long end, 16721da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 16731da177e4SLinus Torvalds { 16741da177e4SLinus Torvalds pud_t *pud; 16751da177e4SLinus Torvalds unsigned long next; 16761da177e4SLinus Torvalds 16771da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 16781da177e4SLinus Torvalds pud = pud_alloc(mm, pgd, addr); 16791da177e4SLinus Torvalds if (!pud) 16801da177e4SLinus Torvalds return -ENOMEM; 16811da177e4SLinus Torvalds do { 16821da177e4SLinus Torvalds next = pud_addr_end(addr, end); 16831da177e4SLinus Torvalds if (remap_pmd_range(mm, pud, addr, next, 16841da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot)) 16851da177e4SLinus Torvalds return -ENOMEM; 16861da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 16871da177e4SLinus Torvalds return 0; 16881da177e4SLinus Torvalds } 16891da177e4SLinus Torvalds 1690bfa5bf6dSRolf Eike Beer /** 1691bfa5bf6dSRolf Eike Beer * remap_pfn_range - remap kernel memory to userspace 1692bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 1693bfa5bf6dSRolf Eike Beer * @addr: target user address to start at 1694bfa5bf6dSRolf Eike Beer * @pfn: physical address of kernel memory 1695bfa5bf6dSRolf Eike Beer * @size: size of map area 1696bfa5bf6dSRolf Eike Beer * @prot: page protection flags for this mapping 1697bfa5bf6dSRolf Eike Beer * 1698bfa5bf6dSRolf Eike Beer * Note: this is only safe if the mm semaphore is held when called. 1699bfa5bf6dSRolf Eike Beer */ 17001da177e4SLinus Torvalds int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 17011da177e4SLinus Torvalds unsigned long pfn, unsigned long size, pgprot_t prot) 17021da177e4SLinus Torvalds { 17031da177e4SLinus Torvalds pgd_t *pgd; 17041da177e4SLinus Torvalds unsigned long next; 17052d15cab8SHugh Dickins unsigned long end = addr + PAGE_ALIGN(size); 17061da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 17071da177e4SLinus Torvalds int err; 17081da177e4SLinus Torvalds 17091da177e4SLinus Torvalds /* 17101da177e4SLinus Torvalds * Physically remapped pages are special. Tell the 17111da177e4SLinus Torvalds * rest of the world about it: 17121da177e4SLinus Torvalds * VM_IO tells people not to look at these pages 17131da177e4SLinus Torvalds * (accesses can have side effects). 17140b14c179SHugh Dickins * VM_RESERVED is specified all over the place, because 17150b14c179SHugh Dickins * in 2.4 it kept swapout's vma scan off this vma; but 17160b14c179SHugh Dickins * in 2.6 the LRU scan won't even find its pages, so this 17170b14c179SHugh Dickins * flag means no more than count its pages in reserved_vm, 17180b14c179SHugh Dickins * and omit it from core dump, even when VM_IO turned off. 17196aab341eSLinus Torvalds * VM_PFNMAP tells the core MM that the base pages are just 17206aab341eSLinus Torvalds * raw PFN mappings, and do not have a "struct page" associated 17216aab341eSLinus Torvalds * with them. 1722fb155c16SLinus Torvalds * 1723fb155c16SLinus Torvalds * There's a horrible special case to handle copy-on-write 1724fb155c16SLinus Torvalds * behaviour that some programs depend on. We mark the "original" 1725fb155c16SLinus Torvalds * un-COW'ed pages by matching them up with "vma->vm_pgoff". 17261da177e4SLinus Torvalds */ 17274bb9c5c0SPallipadi, Venkatesh if (addr == vma->vm_start && end == vma->vm_end) { 17286aab341eSLinus Torvalds vma->vm_pgoff = pfn; 1729895791daSPallipadi, Venkatesh vma->vm_flags |= VM_PFN_AT_MMAP; 17304bb9c5c0SPallipadi, Venkatesh } else if (is_cow_mapping(vma->vm_flags)) 17313c8bb73aSvenkatesh.pallipadi@intel.com return -EINVAL; 1732fb155c16SLinus Torvalds 1733fb155c16SLinus Torvalds vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP; 17341da177e4SLinus Torvalds 1735e4b866edSvenkatesh.pallipadi@intel.com err = track_pfn_vma_new(vma, &prot, pfn, PAGE_ALIGN(size)); 1736a3670613Svenkatesh.pallipadi@intel.com if (err) { 1737a3670613Svenkatesh.pallipadi@intel.com /* 1738a3670613Svenkatesh.pallipadi@intel.com * To indicate that track_pfn related cleanup is not 1739a3670613Svenkatesh.pallipadi@intel.com * needed from higher level routine calling unmap_vmas 1740a3670613Svenkatesh.pallipadi@intel.com */ 1741a3670613Svenkatesh.pallipadi@intel.com vma->vm_flags &= ~(VM_IO | VM_RESERVED | VM_PFNMAP); 1742895791daSPallipadi, Venkatesh vma->vm_flags &= ~VM_PFN_AT_MMAP; 17432ab64037Svenkatesh.pallipadi@intel.com return -EINVAL; 1744a3670613Svenkatesh.pallipadi@intel.com } 17452ab64037Svenkatesh.pallipadi@intel.com 17461da177e4SLinus Torvalds BUG_ON(addr >= end); 17471da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 17481da177e4SLinus Torvalds pgd = pgd_offset(mm, addr); 17491da177e4SLinus Torvalds flush_cache_range(vma, addr, end); 17501da177e4SLinus Torvalds do { 17511da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 17521da177e4SLinus Torvalds err = remap_pud_range(mm, pgd, addr, next, 17531da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot); 17541da177e4SLinus Torvalds if (err) 17551da177e4SLinus Torvalds break; 17561da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 17572ab64037Svenkatesh.pallipadi@intel.com 17582ab64037Svenkatesh.pallipadi@intel.com if (err) 17592ab64037Svenkatesh.pallipadi@intel.com untrack_pfn_vma(vma, pfn, PAGE_ALIGN(size)); 17602ab64037Svenkatesh.pallipadi@intel.com 17611da177e4SLinus Torvalds return err; 17621da177e4SLinus Torvalds } 17631da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range); 17641da177e4SLinus Torvalds 1765aee16b3cSJeremy Fitzhardinge static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd, 1766aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 1767aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 1768aee16b3cSJeremy Fitzhardinge { 1769aee16b3cSJeremy Fitzhardinge pte_t *pte; 1770aee16b3cSJeremy Fitzhardinge int err; 17712f569afdSMartin Schwidefsky pgtable_t token; 177294909914SBorislav Petkov spinlock_t *uninitialized_var(ptl); 1773aee16b3cSJeremy Fitzhardinge 1774aee16b3cSJeremy Fitzhardinge pte = (mm == &init_mm) ? 1775aee16b3cSJeremy Fitzhardinge pte_alloc_kernel(pmd, addr) : 1776aee16b3cSJeremy Fitzhardinge pte_alloc_map_lock(mm, pmd, addr, &ptl); 1777aee16b3cSJeremy Fitzhardinge if (!pte) 1778aee16b3cSJeremy Fitzhardinge return -ENOMEM; 1779aee16b3cSJeremy Fitzhardinge 1780aee16b3cSJeremy Fitzhardinge BUG_ON(pmd_huge(*pmd)); 1781aee16b3cSJeremy Fitzhardinge 178238e0edb1SJeremy Fitzhardinge arch_enter_lazy_mmu_mode(); 178338e0edb1SJeremy Fitzhardinge 17842f569afdSMartin Schwidefsky token = pmd_pgtable(*pmd); 1785aee16b3cSJeremy Fitzhardinge 1786aee16b3cSJeremy Fitzhardinge do { 17872f569afdSMartin Schwidefsky err = fn(pte, token, addr, data); 1788aee16b3cSJeremy Fitzhardinge if (err) 1789aee16b3cSJeremy Fitzhardinge break; 1790aee16b3cSJeremy Fitzhardinge } while (pte++, addr += PAGE_SIZE, addr != end); 1791aee16b3cSJeremy Fitzhardinge 179238e0edb1SJeremy Fitzhardinge arch_leave_lazy_mmu_mode(); 179338e0edb1SJeremy Fitzhardinge 1794aee16b3cSJeremy Fitzhardinge if (mm != &init_mm) 1795aee16b3cSJeremy Fitzhardinge pte_unmap_unlock(pte-1, ptl); 1796aee16b3cSJeremy Fitzhardinge return err; 1797aee16b3cSJeremy Fitzhardinge } 1798aee16b3cSJeremy Fitzhardinge 1799aee16b3cSJeremy Fitzhardinge static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud, 1800aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 1801aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 1802aee16b3cSJeremy Fitzhardinge { 1803aee16b3cSJeremy Fitzhardinge pmd_t *pmd; 1804aee16b3cSJeremy Fitzhardinge unsigned long next; 1805aee16b3cSJeremy Fitzhardinge int err; 1806aee16b3cSJeremy Fitzhardinge 1807ceb86879SAndi Kleen BUG_ON(pud_huge(*pud)); 1808ceb86879SAndi Kleen 1809aee16b3cSJeremy Fitzhardinge pmd = pmd_alloc(mm, pud, addr); 1810aee16b3cSJeremy Fitzhardinge if (!pmd) 1811aee16b3cSJeremy Fitzhardinge return -ENOMEM; 1812aee16b3cSJeremy Fitzhardinge do { 1813aee16b3cSJeremy Fitzhardinge next = pmd_addr_end(addr, end); 1814aee16b3cSJeremy Fitzhardinge err = apply_to_pte_range(mm, pmd, addr, next, fn, data); 1815aee16b3cSJeremy Fitzhardinge if (err) 1816aee16b3cSJeremy Fitzhardinge break; 1817aee16b3cSJeremy Fitzhardinge } while (pmd++, addr = next, addr != end); 1818aee16b3cSJeremy Fitzhardinge return err; 1819aee16b3cSJeremy Fitzhardinge } 1820aee16b3cSJeremy Fitzhardinge 1821aee16b3cSJeremy Fitzhardinge static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd, 1822aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 1823aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 1824aee16b3cSJeremy Fitzhardinge { 1825aee16b3cSJeremy Fitzhardinge pud_t *pud; 1826aee16b3cSJeremy Fitzhardinge unsigned long next; 1827aee16b3cSJeremy Fitzhardinge int err; 1828aee16b3cSJeremy Fitzhardinge 1829aee16b3cSJeremy Fitzhardinge pud = pud_alloc(mm, pgd, addr); 1830aee16b3cSJeremy Fitzhardinge if (!pud) 1831aee16b3cSJeremy Fitzhardinge return -ENOMEM; 1832aee16b3cSJeremy Fitzhardinge do { 1833aee16b3cSJeremy Fitzhardinge next = pud_addr_end(addr, end); 1834aee16b3cSJeremy Fitzhardinge err = apply_to_pmd_range(mm, pud, addr, next, fn, data); 1835aee16b3cSJeremy Fitzhardinge if (err) 1836aee16b3cSJeremy Fitzhardinge break; 1837aee16b3cSJeremy Fitzhardinge } while (pud++, addr = next, addr != end); 1838aee16b3cSJeremy Fitzhardinge return err; 1839aee16b3cSJeremy Fitzhardinge } 1840aee16b3cSJeremy Fitzhardinge 1841aee16b3cSJeremy Fitzhardinge /* 1842aee16b3cSJeremy Fitzhardinge * Scan a region of virtual memory, filling in page tables as necessary 1843aee16b3cSJeremy Fitzhardinge * and calling a provided function on each leaf page table. 1844aee16b3cSJeremy Fitzhardinge */ 1845aee16b3cSJeremy Fitzhardinge int apply_to_page_range(struct mm_struct *mm, unsigned long addr, 1846aee16b3cSJeremy Fitzhardinge unsigned long size, pte_fn_t fn, void *data) 1847aee16b3cSJeremy Fitzhardinge { 1848aee16b3cSJeremy Fitzhardinge pgd_t *pgd; 1849aee16b3cSJeremy Fitzhardinge unsigned long next; 1850cddb8a5cSAndrea Arcangeli unsigned long start = addr, end = addr + size; 1851aee16b3cSJeremy Fitzhardinge int err; 1852aee16b3cSJeremy Fitzhardinge 1853aee16b3cSJeremy Fitzhardinge BUG_ON(addr >= end); 1854cddb8a5cSAndrea Arcangeli mmu_notifier_invalidate_range_start(mm, start, end); 1855aee16b3cSJeremy Fitzhardinge pgd = pgd_offset(mm, addr); 1856aee16b3cSJeremy Fitzhardinge do { 1857aee16b3cSJeremy Fitzhardinge next = pgd_addr_end(addr, end); 1858aee16b3cSJeremy Fitzhardinge err = apply_to_pud_range(mm, pgd, addr, next, fn, data); 1859aee16b3cSJeremy Fitzhardinge if (err) 1860aee16b3cSJeremy Fitzhardinge break; 1861aee16b3cSJeremy Fitzhardinge } while (pgd++, addr = next, addr != end); 1862cddb8a5cSAndrea Arcangeli mmu_notifier_invalidate_range_end(mm, start, end); 1863aee16b3cSJeremy Fitzhardinge return err; 1864aee16b3cSJeremy Fitzhardinge } 1865aee16b3cSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(apply_to_page_range); 1866aee16b3cSJeremy Fitzhardinge 18671da177e4SLinus Torvalds /* 18688f4e2101SHugh Dickins * handle_pte_fault chooses page fault handler according to an entry 18698f4e2101SHugh Dickins * which was read non-atomically. Before making any commitment, on 18708f4e2101SHugh Dickins * those architectures or configurations (e.g. i386 with PAE) which 18718f4e2101SHugh Dickins * might give a mix of unmatched parts, do_swap_page and do_file_page 18728f4e2101SHugh Dickins * must check under lock before unmapping the pte and proceeding 18738f4e2101SHugh Dickins * (but do_wp_page is only called after already making such a check; 18748f4e2101SHugh Dickins * and do_anonymous_page and do_no_page can safely check later on). 18758f4e2101SHugh Dickins */ 18764c21e2f2SHugh Dickins static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd, 18778f4e2101SHugh Dickins pte_t *page_table, pte_t orig_pte) 18788f4e2101SHugh Dickins { 18798f4e2101SHugh Dickins int same = 1; 18808f4e2101SHugh Dickins #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT) 18818f4e2101SHugh Dickins if (sizeof(pte_t) > sizeof(unsigned long)) { 18824c21e2f2SHugh Dickins spinlock_t *ptl = pte_lockptr(mm, pmd); 18834c21e2f2SHugh Dickins spin_lock(ptl); 18848f4e2101SHugh Dickins same = pte_same(*page_table, orig_pte); 18854c21e2f2SHugh Dickins spin_unlock(ptl); 18868f4e2101SHugh Dickins } 18878f4e2101SHugh Dickins #endif 18888f4e2101SHugh Dickins pte_unmap(page_table); 18898f4e2101SHugh Dickins return same; 18908f4e2101SHugh Dickins } 18918f4e2101SHugh Dickins 18928f4e2101SHugh Dickins /* 18931da177e4SLinus Torvalds * Do pte_mkwrite, but only if the vma says VM_WRITE. We do this when 18941da177e4SLinus Torvalds * servicing faults for write access. In the normal case, do always want 18951da177e4SLinus Torvalds * pte_mkwrite. But get_user_pages can cause write faults for mappings 18961da177e4SLinus Torvalds * that do not have writing enabled, when used by access_process_vm. 18971da177e4SLinus Torvalds */ 18981da177e4SLinus Torvalds static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma) 18991da177e4SLinus Torvalds { 19001da177e4SLinus Torvalds if (likely(vma->vm_flags & VM_WRITE)) 19011da177e4SLinus Torvalds pte = pte_mkwrite(pte); 19021da177e4SLinus Torvalds return pte; 19031da177e4SLinus Torvalds } 19041da177e4SLinus Torvalds 19059de455b2SAtsushi Nemoto static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma) 19066aab341eSLinus Torvalds { 19076aab341eSLinus Torvalds /* 19086aab341eSLinus Torvalds * If the source page was a PFN mapping, we don't have 19096aab341eSLinus Torvalds * a "struct page" for it. We do a best-effort copy by 19106aab341eSLinus Torvalds * just copying from the original user address. If that 19116aab341eSLinus Torvalds * fails, we just zero-fill it. Live with it. 19126aab341eSLinus Torvalds */ 19136aab341eSLinus Torvalds if (unlikely(!src)) { 19146aab341eSLinus Torvalds void *kaddr = kmap_atomic(dst, KM_USER0); 19155d2a2dbbSLinus Torvalds void __user *uaddr = (void __user *)(va & PAGE_MASK); 19165d2a2dbbSLinus Torvalds 19175d2a2dbbSLinus Torvalds /* 19185d2a2dbbSLinus Torvalds * This really shouldn't fail, because the page is there 19195d2a2dbbSLinus Torvalds * in the page tables. But it might just be unreadable, 19205d2a2dbbSLinus Torvalds * in which case we just give up and fill the result with 19215d2a2dbbSLinus Torvalds * zeroes. 19225d2a2dbbSLinus Torvalds */ 19235d2a2dbbSLinus Torvalds if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) 19246aab341eSLinus Torvalds memset(kaddr, 0, PAGE_SIZE); 19256aab341eSLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 1926c4ec7b0dSDmitriy Monakhov flush_dcache_page(dst); 19270ed361deSNick Piggin } else 19289de455b2SAtsushi Nemoto copy_user_highpage(dst, src, va, vma); 19296aab341eSLinus Torvalds } 19306aab341eSLinus Torvalds 19311da177e4SLinus Torvalds /* 19321da177e4SLinus Torvalds * This routine handles present pages, when users try to write 19331da177e4SLinus Torvalds * to a shared page. It is done by copying the page to a new address 19341da177e4SLinus Torvalds * and decrementing the shared-page counter for the old page. 19351da177e4SLinus Torvalds * 19361da177e4SLinus Torvalds * Note that this routine assumes that the protection checks have been 19371da177e4SLinus Torvalds * done by the caller (the low-level page fault routine in most cases). 19381da177e4SLinus Torvalds * Thus we can safely just mark it writable once we've done any necessary 19391da177e4SLinus Torvalds * COW. 19401da177e4SLinus Torvalds * 19411da177e4SLinus Torvalds * We also mark the page dirty at this point even though the page will 19421da177e4SLinus Torvalds * change only once the write actually happens. This avoids a few races, 19431da177e4SLinus Torvalds * and potentially makes it more efficient. 19441da177e4SLinus Torvalds * 19458f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 19468f4e2101SHugh Dickins * but allow concurrent faults), with pte both mapped and locked. 19478f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 19481da177e4SLinus Torvalds */ 19491da177e4SLinus Torvalds static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma, 195065500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 19518f4e2101SHugh Dickins spinlock_t *ptl, pte_t orig_pte) 19521da177e4SLinus Torvalds { 1953e5bbe4dfSHugh Dickins struct page *old_page, *new_page; 19541da177e4SLinus Torvalds pte_t entry; 195583c54070SNick Piggin int reuse = 0, ret = 0; 1956a200ee18SPeter Zijlstra int page_mkwrite = 0; 1957d08b3851SPeter Zijlstra struct page *dirty_page = NULL; 19581da177e4SLinus Torvalds 19596aab341eSLinus Torvalds old_page = vm_normal_page(vma, address, orig_pte); 1960251b97f5SPeter Zijlstra if (!old_page) { 1961251b97f5SPeter Zijlstra /* 1962251b97f5SPeter Zijlstra * VM_MIXEDMAP !pfn_valid() case 1963251b97f5SPeter Zijlstra * 1964251b97f5SPeter Zijlstra * We should not cow pages in a shared writeable mapping. 1965251b97f5SPeter Zijlstra * Just mark the pages writable as we can't do any dirty 1966251b97f5SPeter Zijlstra * accounting on raw pfn maps. 1967251b97f5SPeter Zijlstra */ 1968251b97f5SPeter Zijlstra if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 1969251b97f5SPeter Zijlstra (VM_WRITE|VM_SHARED)) 1970251b97f5SPeter Zijlstra goto reuse; 1971920fc356SHugh Dickins goto gotten; 1972251b97f5SPeter Zijlstra } 19731da177e4SLinus Torvalds 1974d08b3851SPeter Zijlstra /* 1975ee6a6457SPeter Zijlstra * Take out anonymous pages first, anonymous shared vmas are 1976ee6a6457SPeter Zijlstra * not dirty accountable. 1977d08b3851SPeter Zijlstra */ 19789a840895SHugh Dickins if (PageAnon(old_page) && !PageKsm(old_page)) { 1979ab967d86SHugh Dickins if (!trylock_page(old_page)) { 1980ab967d86SHugh Dickins page_cache_get(old_page); 1981ab967d86SHugh Dickins pte_unmap_unlock(page_table, ptl); 1982ab967d86SHugh Dickins lock_page(old_page); 1983ab967d86SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, 1984ab967d86SHugh Dickins &ptl); 1985ab967d86SHugh Dickins if (!pte_same(*page_table, orig_pte)) { 1986ab967d86SHugh Dickins unlock_page(old_page); 1987ab967d86SHugh Dickins page_cache_release(old_page); 1988ab967d86SHugh Dickins goto unlock; 1989ab967d86SHugh Dickins } 1990ab967d86SHugh Dickins page_cache_release(old_page); 1991ab967d86SHugh Dickins } 19927b1fe597SHugh Dickins reuse = reuse_swap_page(old_page); 1993ee6a6457SPeter Zijlstra unlock_page(old_page); 1994ee6a6457SPeter Zijlstra } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 1995d08b3851SPeter Zijlstra (VM_WRITE|VM_SHARED))) { 1996ee6a6457SPeter Zijlstra /* 1997ee6a6457SPeter Zijlstra * Only catch write-faults on shared writable pages, 1998ee6a6457SPeter Zijlstra * read-only shared pages can get COWed by 1999ee6a6457SPeter Zijlstra * get_user_pages(.write=1, .force=1). 2000ee6a6457SPeter Zijlstra */ 20019637a5efSDavid Howells if (vma->vm_ops && vma->vm_ops->page_mkwrite) { 2002c2ec175cSNick Piggin struct vm_fault vmf; 2003c2ec175cSNick Piggin int tmp; 2004c2ec175cSNick Piggin 2005c2ec175cSNick Piggin vmf.virtual_address = (void __user *)(address & 2006c2ec175cSNick Piggin PAGE_MASK); 2007c2ec175cSNick Piggin vmf.pgoff = old_page->index; 2008c2ec175cSNick Piggin vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE; 2009c2ec175cSNick Piggin vmf.page = old_page; 2010c2ec175cSNick Piggin 20119637a5efSDavid Howells /* 20129637a5efSDavid Howells * Notify the address space that the page is about to 20139637a5efSDavid Howells * become writable so that it can prohibit this or wait 20149637a5efSDavid Howells * for the page to get into an appropriate state. 20159637a5efSDavid Howells * 20169637a5efSDavid Howells * We do this without the lock held, so that it can 20179637a5efSDavid Howells * sleep if it needs to. 20189637a5efSDavid Howells */ 20199637a5efSDavid Howells page_cache_get(old_page); 20209637a5efSDavid Howells pte_unmap_unlock(page_table, ptl); 20219637a5efSDavid Howells 2022c2ec175cSNick Piggin tmp = vma->vm_ops->page_mkwrite(vma, &vmf); 2023c2ec175cSNick Piggin if (unlikely(tmp & 2024c2ec175cSNick Piggin (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) { 2025c2ec175cSNick Piggin ret = tmp; 20269637a5efSDavid Howells goto unwritable_page; 2027c2ec175cSNick Piggin } 2028b827e496SNick Piggin if (unlikely(!(tmp & VM_FAULT_LOCKED))) { 2029b827e496SNick Piggin lock_page(old_page); 2030b827e496SNick Piggin if (!old_page->mapping) { 2031b827e496SNick Piggin ret = 0; /* retry the fault */ 2032b827e496SNick Piggin unlock_page(old_page); 2033b827e496SNick Piggin goto unwritable_page; 2034b827e496SNick Piggin } 2035b827e496SNick Piggin } else 2036b827e496SNick Piggin VM_BUG_ON(!PageLocked(old_page)); 20379637a5efSDavid Howells 20389637a5efSDavid Howells /* 20399637a5efSDavid Howells * Since we dropped the lock we need to revalidate 20409637a5efSDavid Howells * the PTE as someone else may have changed it. If 20419637a5efSDavid Howells * they did, we just return, as we can count on the 20429637a5efSDavid Howells * MMU to tell us if they didn't also make it writable. 20439637a5efSDavid Howells */ 20449637a5efSDavid Howells page_table = pte_offset_map_lock(mm, pmd, address, 20459637a5efSDavid Howells &ptl); 2046b827e496SNick Piggin if (!pte_same(*page_table, orig_pte)) { 2047b827e496SNick Piggin unlock_page(old_page); 2048c3704cebSHugh Dickins page_cache_release(old_page); 20499637a5efSDavid Howells goto unlock; 2050b827e496SNick Piggin } 2051a200ee18SPeter Zijlstra 2052a200ee18SPeter Zijlstra page_mkwrite = 1; 20539637a5efSDavid Howells } 2054d08b3851SPeter Zijlstra dirty_page = old_page; 2055d08b3851SPeter Zijlstra get_page(dirty_page); 20569637a5efSDavid Howells reuse = 1; 20579637a5efSDavid Howells } 20589637a5efSDavid Howells 20591da177e4SLinus Torvalds if (reuse) { 2060251b97f5SPeter Zijlstra reuse: 2061eca35133SBen Collins flush_cache_page(vma, address, pte_pfn(orig_pte)); 206265500d23SHugh Dickins entry = pte_mkyoung(orig_pte); 206365500d23SHugh Dickins entry = maybe_mkwrite(pte_mkdirty(entry), vma); 2064954ffcb3SKAMEZAWA Hiroyuki if (ptep_set_access_flags(vma, address, page_table, entry,1)) 20651da177e4SLinus Torvalds update_mmu_cache(vma, address, entry); 206665500d23SHugh Dickins ret |= VM_FAULT_WRITE; 206765500d23SHugh Dickins goto unlock; 20681da177e4SLinus Torvalds } 20691da177e4SLinus Torvalds 20701da177e4SLinus Torvalds /* 20711da177e4SLinus Torvalds * Ok, we need to copy. Oh, well.. 20721da177e4SLinus Torvalds */ 20731da177e4SLinus Torvalds page_cache_get(old_page); 2074920fc356SHugh Dickins gotten: 20758f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 20761da177e4SLinus Torvalds 20771da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 207865500d23SHugh Dickins goto oom; 2079557ed1faSNick Piggin VM_BUG_ON(old_page == ZERO_PAGE(0)); 2080769848c0SMel Gorman new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address); 20811da177e4SLinus Torvalds if (!new_page) 208265500d23SHugh Dickins goto oom; 2083b291f000SNick Piggin /* 2084b291f000SNick Piggin * Don't let another task, with possibly unlocked vma, 2085b291f000SNick Piggin * keep the mlocked page. 2086b291f000SNick Piggin */ 2087ab92661dSCarsten Otte if ((vma->vm_flags & VM_LOCKED) && old_page) { 2088b291f000SNick Piggin lock_page(old_page); /* for LRU manipulation */ 2089b291f000SNick Piggin clear_page_mlock(old_page); 2090b291f000SNick Piggin unlock_page(old_page); 2091b291f000SNick Piggin } 20929de455b2SAtsushi Nemoto cow_user_page(new_page, old_page, address, vma); 20930ed361deSNick Piggin __SetPageUptodate(new_page); 209465500d23SHugh Dickins 20952c26fdd7SKAMEZAWA Hiroyuki if (mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL)) 20968a9f3ccdSBalbir Singh goto oom_free_new; 20978a9f3ccdSBalbir Singh 20981da177e4SLinus Torvalds /* 20991da177e4SLinus Torvalds * Re-check the pte - we dropped the lock 21001da177e4SLinus Torvalds */ 21018f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 210265500d23SHugh Dickins if (likely(pte_same(*page_table, orig_pte))) { 2103920fc356SHugh Dickins if (old_page) { 21044294621fSHugh Dickins if (!PageAnon(old_page)) { 21054294621fSHugh Dickins dec_mm_counter(mm, file_rss); 2106920fc356SHugh Dickins inc_mm_counter(mm, anon_rss); 21074294621fSHugh Dickins } 2108920fc356SHugh Dickins } else 2109920fc356SHugh Dickins inc_mm_counter(mm, anon_rss); 2110eca35133SBen Collins flush_cache_page(vma, address, pte_pfn(orig_pte)); 211165500d23SHugh Dickins entry = mk_pte(new_page, vma->vm_page_prot); 211265500d23SHugh Dickins entry = maybe_mkwrite(pte_mkdirty(entry), vma); 21134ce072f1SSiddha, Suresh B /* 21144ce072f1SSiddha, Suresh B * Clear the pte entry and flush it first, before updating the 21154ce072f1SSiddha, Suresh B * pte with the new entry. This will avoid a race condition 21164ce072f1SSiddha, Suresh B * seen in the presence of one thread doing SMC and another 21174ce072f1SSiddha, Suresh B * thread doing COW. 21184ce072f1SSiddha, Suresh B */ 2119828502d3SIzik Eidus ptep_clear_flush(vma, address, page_table); 21209617d95eSNick Piggin page_add_new_anon_rmap(new_page, vma, address); 2121828502d3SIzik Eidus /* 2122828502d3SIzik Eidus * We call the notify macro here because, when using secondary 2123828502d3SIzik Eidus * mmu page tables (such as kvm shadow page tables), we want the 2124828502d3SIzik Eidus * new page to be mapped directly into the secondary page table. 2125828502d3SIzik Eidus */ 2126828502d3SIzik Eidus set_pte_at_notify(mm, address, page_table, entry); 212764d6519dSLee Schermerhorn update_mmu_cache(vma, address, entry); 2128945754a1SNick Piggin if (old_page) { 2129945754a1SNick Piggin /* 2130945754a1SNick Piggin * Only after switching the pte to the new page may 2131945754a1SNick Piggin * we remove the mapcount here. Otherwise another 2132945754a1SNick Piggin * process may come and find the rmap count decremented 2133945754a1SNick Piggin * before the pte is switched to the new page, and 2134945754a1SNick Piggin * "reuse" the old page writing into it while our pte 2135945754a1SNick Piggin * here still points into it and can be read by other 2136945754a1SNick Piggin * threads. 2137945754a1SNick Piggin * 2138945754a1SNick Piggin * The critical issue is to order this 2139945754a1SNick Piggin * page_remove_rmap with the ptp_clear_flush above. 2140945754a1SNick Piggin * Those stores are ordered by (if nothing else,) 2141945754a1SNick Piggin * the barrier present in the atomic_add_negative 2142945754a1SNick Piggin * in page_remove_rmap. 2143945754a1SNick Piggin * 2144945754a1SNick Piggin * Then the TLB flush in ptep_clear_flush ensures that 2145945754a1SNick Piggin * no process can access the old page before the 2146945754a1SNick Piggin * decremented mapcount is visible. And the old page 2147945754a1SNick Piggin * cannot be reused until after the decremented 2148945754a1SNick Piggin * mapcount is visible. So transitively, TLBs to 2149945754a1SNick Piggin * old page will be flushed before it can be reused. 2150945754a1SNick Piggin */ 2151edc315fdSHugh Dickins page_remove_rmap(old_page); 2152945754a1SNick Piggin } 2153945754a1SNick Piggin 21541da177e4SLinus Torvalds /* Free the old page.. */ 21551da177e4SLinus Torvalds new_page = old_page; 2156f33ea7f4SNick Piggin ret |= VM_FAULT_WRITE; 21578a9f3ccdSBalbir Singh } else 21588a9f3ccdSBalbir Singh mem_cgroup_uncharge_page(new_page); 21598a9f3ccdSBalbir Singh 2160920fc356SHugh Dickins if (new_page) 21611da177e4SLinus Torvalds page_cache_release(new_page); 2162920fc356SHugh Dickins if (old_page) 21631da177e4SLinus Torvalds page_cache_release(old_page); 216465500d23SHugh Dickins unlock: 21658f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 2166d08b3851SPeter Zijlstra if (dirty_page) { 216779352894SNick Piggin /* 216879352894SNick Piggin * Yes, Virginia, this is actually required to prevent a race 216979352894SNick Piggin * with clear_page_dirty_for_io() from clearing the page dirty 217079352894SNick Piggin * bit after it clear all dirty ptes, but before a racing 217179352894SNick Piggin * do_wp_page installs a dirty pte. 217279352894SNick Piggin * 217379352894SNick Piggin * do_no_page is protected similarly. 217479352894SNick Piggin */ 2175b827e496SNick Piggin if (!page_mkwrite) { 217679352894SNick Piggin wait_on_page_locked(dirty_page); 2177a200ee18SPeter Zijlstra set_page_dirty_balance(dirty_page, page_mkwrite); 2178b827e496SNick Piggin } 2179d08b3851SPeter Zijlstra put_page(dirty_page); 2180b827e496SNick Piggin if (page_mkwrite) { 2181b827e496SNick Piggin struct address_space *mapping = dirty_page->mapping; 2182b827e496SNick Piggin 2183b827e496SNick Piggin set_page_dirty(dirty_page); 2184b827e496SNick Piggin unlock_page(dirty_page); 2185b827e496SNick Piggin page_cache_release(dirty_page); 2186b827e496SNick Piggin if (mapping) { 2187b827e496SNick Piggin /* 2188b827e496SNick Piggin * Some device drivers do not set page.mapping 2189b827e496SNick Piggin * but still dirty their pages 2190b827e496SNick Piggin */ 2191b827e496SNick Piggin balance_dirty_pages_ratelimited(mapping); 2192b827e496SNick Piggin } 2193b827e496SNick Piggin } 2194b827e496SNick Piggin 2195b827e496SNick Piggin /* file_update_time outside page_lock */ 2196b827e496SNick Piggin if (vma->vm_file) 2197b827e496SNick Piggin file_update_time(vma->vm_file); 2198d08b3851SPeter Zijlstra } 2199f33ea7f4SNick Piggin return ret; 22008a9f3ccdSBalbir Singh oom_free_new: 22016dbf6d3bSHugh Dickins page_cache_release(new_page); 220265500d23SHugh Dickins oom: 2203b827e496SNick Piggin if (old_page) { 2204b827e496SNick Piggin if (page_mkwrite) { 2205b827e496SNick Piggin unlock_page(old_page); 22061da177e4SLinus Torvalds page_cache_release(old_page); 2207b827e496SNick Piggin } 2208b827e496SNick Piggin page_cache_release(old_page); 2209b827e496SNick Piggin } 22101da177e4SLinus Torvalds return VM_FAULT_OOM; 22119637a5efSDavid Howells 22129637a5efSDavid Howells unwritable_page: 22139637a5efSDavid Howells page_cache_release(old_page); 2214c2ec175cSNick Piggin return ret; 22151da177e4SLinus Torvalds } 22161da177e4SLinus Torvalds 22171da177e4SLinus Torvalds /* 22181da177e4SLinus Torvalds * Helper functions for unmap_mapping_range(). 22191da177e4SLinus Torvalds * 22201da177e4SLinus Torvalds * __ Notes on dropping i_mmap_lock to reduce latency while unmapping __ 22211da177e4SLinus Torvalds * 22221da177e4SLinus Torvalds * We have to restart searching the prio_tree whenever we drop the lock, 22231da177e4SLinus Torvalds * since the iterator is only valid while the lock is held, and anyway 22241da177e4SLinus Torvalds * a later vma might be split and reinserted earlier while lock dropped. 22251da177e4SLinus Torvalds * 22261da177e4SLinus Torvalds * The list of nonlinear vmas could be handled more efficiently, using 22271da177e4SLinus Torvalds * a placeholder, but handle it in the same way until a need is shown. 22281da177e4SLinus Torvalds * It is important to search the prio_tree before nonlinear list: a vma 22291da177e4SLinus Torvalds * may become nonlinear and be shifted from prio_tree to nonlinear list 22301da177e4SLinus Torvalds * while the lock is dropped; but never shifted from list to prio_tree. 22311da177e4SLinus Torvalds * 22321da177e4SLinus Torvalds * In order to make forward progress despite restarting the search, 22331da177e4SLinus Torvalds * vm_truncate_count is used to mark a vma as now dealt with, so we can 22341da177e4SLinus Torvalds * quickly skip it next time around. Since the prio_tree search only 22351da177e4SLinus Torvalds * shows us those vmas affected by unmapping the range in question, we 22361da177e4SLinus Torvalds * can't efficiently keep all vmas in step with mapping->truncate_count: 22371da177e4SLinus Torvalds * so instead reset them all whenever it wraps back to 0 (then go to 1). 22381da177e4SLinus Torvalds * mapping->truncate_count and vma->vm_truncate_count are protected by 22391da177e4SLinus Torvalds * i_mmap_lock. 22401da177e4SLinus Torvalds * 22411da177e4SLinus Torvalds * In order to make forward progress despite repeatedly restarting some 2242ee39b37bSHugh Dickins * large vma, note the restart_addr from unmap_vmas when it breaks out: 22431da177e4SLinus Torvalds * and restart from that address when we reach that vma again. It might 22441da177e4SLinus Torvalds * have been split or merged, shrunk or extended, but never shifted: so 22451da177e4SLinus Torvalds * restart_addr remains valid so long as it remains in the vma's range. 22461da177e4SLinus Torvalds * unmap_mapping_range forces truncate_count to leap over page-aligned 22471da177e4SLinus Torvalds * values so we can save vma's restart_addr in its truncate_count field. 22481da177e4SLinus Torvalds */ 22491da177e4SLinus Torvalds #define is_restart_addr(truncate_count) (!((truncate_count) & ~PAGE_MASK)) 22501da177e4SLinus Torvalds 22511da177e4SLinus Torvalds static void reset_vma_truncate_counts(struct address_space *mapping) 22521da177e4SLinus Torvalds { 22531da177e4SLinus Torvalds struct vm_area_struct *vma; 22541da177e4SLinus Torvalds struct prio_tree_iter iter; 22551da177e4SLinus Torvalds 22561da177e4SLinus Torvalds vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, ULONG_MAX) 22571da177e4SLinus Torvalds vma->vm_truncate_count = 0; 22581da177e4SLinus Torvalds list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list) 22591da177e4SLinus Torvalds vma->vm_truncate_count = 0; 22601da177e4SLinus Torvalds } 22611da177e4SLinus Torvalds 22621da177e4SLinus Torvalds static int unmap_mapping_range_vma(struct vm_area_struct *vma, 22631da177e4SLinus Torvalds unsigned long start_addr, unsigned long end_addr, 22641da177e4SLinus Torvalds struct zap_details *details) 22651da177e4SLinus Torvalds { 22661da177e4SLinus Torvalds unsigned long restart_addr; 22671da177e4SLinus Torvalds int need_break; 22681da177e4SLinus Torvalds 2269d00806b1SNick Piggin /* 2270d00806b1SNick Piggin * files that support invalidating or truncating portions of the 2271d0217ac0SNick Piggin * file from under mmaped areas must have their ->fault function 227283c54070SNick Piggin * return a locked page (and set VM_FAULT_LOCKED in the return). 227383c54070SNick Piggin * This provides synchronisation against concurrent unmapping here. 2274d00806b1SNick Piggin */ 2275d00806b1SNick Piggin 22761da177e4SLinus Torvalds again: 22771da177e4SLinus Torvalds restart_addr = vma->vm_truncate_count; 22781da177e4SLinus Torvalds if (is_restart_addr(restart_addr) && start_addr < restart_addr) { 22791da177e4SLinus Torvalds start_addr = restart_addr; 22801da177e4SLinus Torvalds if (start_addr >= end_addr) { 22811da177e4SLinus Torvalds /* Top of vma has been split off since last time */ 22821da177e4SLinus Torvalds vma->vm_truncate_count = details->truncate_count; 22831da177e4SLinus Torvalds return 0; 22841da177e4SLinus Torvalds } 22851da177e4SLinus Torvalds } 22861da177e4SLinus Torvalds 2287ee39b37bSHugh Dickins restart_addr = zap_page_range(vma, start_addr, 2288ee39b37bSHugh Dickins end_addr - start_addr, details); 228995c354feSNick Piggin need_break = need_resched() || spin_needbreak(details->i_mmap_lock); 22901da177e4SLinus Torvalds 2291ee39b37bSHugh Dickins if (restart_addr >= end_addr) { 22921da177e4SLinus Torvalds /* We have now completed this vma: mark it so */ 22931da177e4SLinus Torvalds vma->vm_truncate_count = details->truncate_count; 22941da177e4SLinus Torvalds if (!need_break) 22951da177e4SLinus Torvalds return 0; 22961da177e4SLinus Torvalds } else { 22971da177e4SLinus Torvalds /* Note restart_addr in vma's truncate_count field */ 2298ee39b37bSHugh Dickins vma->vm_truncate_count = restart_addr; 22991da177e4SLinus Torvalds if (!need_break) 23001da177e4SLinus Torvalds goto again; 23011da177e4SLinus Torvalds } 23021da177e4SLinus Torvalds 23031da177e4SLinus Torvalds spin_unlock(details->i_mmap_lock); 23041da177e4SLinus Torvalds cond_resched(); 23051da177e4SLinus Torvalds spin_lock(details->i_mmap_lock); 23061da177e4SLinus Torvalds return -EINTR; 23071da177e4SLinus Torvalds } 23081da177e4SLinus Torvalds 23091da177e4SLinus Torvalds static inline void unmap_mapping_range_tree(struct prio_tree_root *root, 23101da177e4SLinus Torvalds struct zap_details *details) 23111da177e4SLinus Torvalds { 23121da177e4SLinus Torvalds struct vm_area_struct *vma; 23131da177e4SLinus Torvalds struct prio_tree_iter iter; 23141da177e4SLinus Torvalds pgoff_t vba, vea, zba, zea; 23151da177e4SLinus Torvalds 23161da177e4SLinus Torvalds restart: 23171da177e4SLinus Torvalds vma_prio_tree_foreach(vma, &iter, root, 23181da177e4SLinus Torvalds details->first_index, details->last_index) { 23191da177e4SLinus Torvalds /* Skip quickly over those we have already dealt with */ 23201da177e4SLinus Torvalds if (vma->vm_truncate_count == details->truncate_count) 23211da177e4SLinus Torvalds continue; 23221da177e4SLinus Torvalds 23231da177e4SLinus Torvalds vba = vma->vm_pgoff; 23241da177e4SLinus Torvalds vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1; 23251da177e4SLinus Torvalds /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */ 23261da177e4SLinus Torvalds zba = details->first_index; 23271da177e4SLinus Torvalds if (zba < vba) 23281da177e4SLinus Torvalds zba = vba; 23291da177e4SLinus Torvalds zea = details->last_index; 23301da177e4SLinus Torvalds if (zea > vea) 23311da177e4SLinus Torvalds zea = vea; 23321da177e4SLinus Torvalds 23331da177e4SLinus Torvalds if (unmap_mapping_range_vma(vma, 23341da177e4SLinus Torvalds ((zba - vba) << PAGE_SHIFT) + vma->vm_start, 23351da177e4SLinus Torvalds ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start, 23361da177e4SLinus Torvalds details) < 0) 23371da177e4SLinus Torvalds goto restart; 23381da177e4SLinus Torvalds } 23391da177e4SLinus Torvalds } 23401da177e4SLinus Torvalds 23411da177e4SLinus Torvalds static inline void unmap_mapping_range_list(struct list_head *head, 23421da177e4SLinus Torvalds struct zap_details *details) 23431da177e4SLinus Torvalds { 23441da177e4SLinus Torvalds struct vm_area_struct *vma; 23451da177e4SLinus Torvalds 23461da177e4SLinus Torvalds /* 23471da177e4SLinus Torvalds * In nonlinear VMAs there is no correspondence between virtual address 23481da177e4SLinus Torvalds * offset and file offset. So we must perform an exhaustive search 23491da177e4SLinus Torvalds * across *all* the pages in each nonlinear VMA, not just the pages 23501da177e4SLinus Torvalds * whose virtual address lies outside the file truncation point. 23511da177e4SLinus Torvalds */ 23521da177e4SLinus Torvalds restart: 23531da177e4SLinus Torvalds list_for_each_entry(vma, head, shared.vm_set.list) { 23541da177e4SLinus Torvalds /* Skip quickly over those we have already dealt with */ 23551da177e4SLinus Torvalds if (vma->vm_truncate_count == details->truncate_count) 23561da177e4SLinus Torvalds continue; 23571da177e4SLinus Torvalds details->nonlinear_vma = vma; 23581da177e4SLinus Torvalds if (unmap_mapping_range_vma(vma, vma->vm_start, 23591da177e4SLinus Torvalds vma->vm_end, details) < 0) 23601da177e4SLinus Torvalds goto restart; 23611da177e4SLinus Torvalds } 23621da177e4SLinus Torvalds } 23631da177e4SLinus Torvalds 23641da177e4SLinus Torvalds /** 236572fd4a35SRobert 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. 23663d41088fSMartin Waitz * @mapping: the address space containing mmaps to be unmapped. 23671da177e4SLinus Torvalds * @holebegin: byte in first page to unmap, relative to the start of 23681da177e4SLinus Torvalds * the underlying file. This will be rounded down to a PAGE_SIZE 23691da177e4SLinus Torvalds * boundary. Note that this is different from vmtruncate(), which 23701da177e4SLinus Torvalds * must keep the partial page. In contrast, we must get rid of 23711da177e4SLinus Torvalds * partial pages. 23721da177e4SLinus Torvalds * @holelen: size of prospective hole in bytes. This will be rounded 23731da177e4SLinus Torvalds * up to a PAGE_SIZE boundary. A holelen of zero truncates to the 23741da177e4SLinus Torvalds * end of the file. 23751da177e4SLinus Torvalds * @even_cows: 1 when truncating a file, unmap even private COWed pages; 23761da177e4SLinus Torvalds * but 0 when invalidating pagecache, don't throw away private data. 23771da177e4SLinus Torvalds */ 23781da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping, 23791da177e4SLinus Torvalds loff_t const holebegin, loff_t const holelen, int even_cows) 23801da177e4SLinus Torvalds { 23811da177e4SLinus Torvalds struct zap_details details; 23821da177e4SLinus Torvalds pgoff_t hba = holebegin >> PAGE_SHIFT; 23831da177e4SLinus Torvalds pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 23841da177e4SLinus Torvalds 23851da177e4SLinus Torvalds /* Check for overflow. */ 23861da177e4SLinus Torvalds if (sizeof(holelen) > sizeof(hlen)) { 23871da177e4SLinus Torvalds long long holeend = 23881da177e4SLinus Torvalds (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 23891da177e4SLinus Torvalds if (holeend & ~(long long)ULONG_MAX) 23901da177e4SLinus Torvalds hlen = ULONG_MAX - hba + 1; 23911da177e4SLinus Torvalds } 23921da177e4SLinus Torvalds 23931da177e4SLinus Torvalds details.check_mapping = even_cows? NULL: mapping; 23941da177e4SLinus Torvalds details.nonlinear_vma = NULL; 23951da177e4SLinus Torvalds details.first_index = hba; 23961da177e4SLinus Torvalds details.last_index = hba + hlen - 1; 23971da177e4SLinus Torvalds if (details.last_index < details.first_index) 23981da177e4SLinus Torvalds details.last_index = ULONG_MAX; 23991da177e4SLinus Torvalds details.i_mmap_lock = &mapping->i_mmap_lock; 24001da177e4SLinus Torvalds 24011da177e4SLinus Torvalds spin_lock(&mapping->i_mmap_lock); 24021da177e4SLinus Torvalds 2403d00806b1SNick Piggin /* Protect against endless unmapping loops */ 24041da177e4SLinus Torvalds mapping->truncate_count++; 24051da177e4SLinus Torvalds if (unlikely(is_restart_addr(mapping->truncate_count))) { 24061da177e4SLinus Torvalds if (mapping->truncate_count == 0) 24071da177e4SLinus Torvalds reset_vma_truncate_counts(mapping); 24081da177e4SLinus Torvalds mapping->truncate_count++; 24091da177e4SLinus Torvalds } 24101da177e4SLinus Torvalds details.truncate_count = mapping->truncate_count; 24111da177e4SLinus Torvalds 24121da177e4SLinus Torvalds if (unlikely(!prio_tree_empty(&mapping->i_mmap))) 24131da177e4SLinus Torvalds unmap_mapping_range_tree(&mapping->i_mmap, &details); 24141da177e4SLinus Torvalds if (unlikely(!list_empty(&mapping->i_mmap_nonlinear))) 24151da177e4SLinus Torvalds unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details); 24161da177e4SLinus Torvalds spin_unlock(&mapping->i_mmap_lock); 24171da177e4SLinus Torvalds } 24181da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range); 24191da177e4SLinus Torvalds 2420bfa5bf6dSRolf Eike Beer /** 2421bfa5bf6dSRolf Eike Beer * vmtruncate - unmap mappings "freed" by truncate() syscall 2422bfa5bf6dSRolf Eike Beer * @inode: inode of the file used 2423bfa5bf6dSRolf Eike Beer * @offset: file offset to start truncating 24241da177e4SLinus Torvalds * 24251da177e4SLinus Torvalds * NOTE! We have to be ready to update the memory sharing 24261da177e4SLinus Torvalds * between the file and the memory map for a potential last 24271da177e4SLinus Torvalds * incomplete page. Ugly, but necessary. 24281da177e4SLinus Torvalds */ 24291da177e4SLinus Torvalds int vmtruncate(struct inode * inode, loff_t offset) 24301da177e4SLinus Torvalds { 243161d5048fSChristoph Hellwig if (inode->i_size < offset) { 24321da177e4SLinus Torvalds unsigned long limit; 24331da177e4SLinus Torvalds 24341da177e4SLinus Torvalds limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur; 24351da177e4SLinus Torvalds if (limit != RLIM_INFINITY && offset > limit) 24361da177e4SLinus Torvalds goto out_sig; 24371da177e4SLinus Torvalds if (offset > inode->i_sb->s_maxbytes) 24381da177e4SLinus Torvalds goto out_big; 24391da177e4SLinus Torvalds i_size_write(inode, offset); 244061d5048fSChristoph Hellwig } else { 244161d5048fSChristoph Hellwig struct address_space *mapping = inode->i_mapping; 24421da177e4SLinus Torvalds 244361d5048fSChristoph Hellwig /* 244461d5048fSChristoph Hellwig * truncation of in-use swapfiles is disallowed - it would 244561d5048fSChristoph Hellwig * cause subsequent swapout to scribble on the now-freed 244661d5048fSChristoph Hellwig * blocks. 244761d5048fSChristoph Hellwig */ 244861d5048fSChristoph Hellwig if (IS_SWAPFILE(inode)) 244961d5048fSChristoph Hellwig return -ETXTBSY; 245061d5048fSChristoph Hellwig i_size_write(inode, offset); 245161d5048fSChristoph Hellwig 245261d5048fSChristoph Hellwig /* 245361d5048fSChristoph Hellwig * unmap_mapping_range is called twice, first simply for 245461d5048fSChristoph Hellwig * efficiency so that truncate_inode_pages does fewer 245561d5048fSChristoph Hellwig * single-page unmaps. However after this first call, and 245661d5048fSChristoph Hellwig * before truncate_inode_pages finishes, it is possible for 245761d5048fSChristoph Hellwig * private pages to be COWed, which remain after 245861d5048fSChristoph Hellwig * truncate_inode_pages finishes, hence the second 245961d5048fSChristoph Hellwig * unmap_mapping_range call must be made for correctness. 246061d5048fSChristoph Hellwig */ 246161d5048fSChristoph Hellwig unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1); 246261d5048fSChristoph Hellwig truncate_inode_pages(mapping, offset); 246361d5048fSChristoph Hellwig unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1); 246461d5048fSChristoph Hellwig } 246561d5048fSChristoph Hellwig 2466acfa4380SAl Viro if (inode->i_op->truncate) 24671da177e4SLinus Torvalds inode->i_op->truncate(inode); 24681da177e4SLinus Torvalds return 0; 246961d5048fSChristoph Hellwig 24701da177e4SLinus Torvalds out_sig: 24711da177e4SLinus Torvalds send_sig(SIGXFSZ, current, 0); 24721da177e4SLinus Torvalds out_big: 24731da177e4SLinus Torvalds return -EFBIG; 24741da177e4SLinus Torvalds } 24751da177e4SLinus Torvalds EXPORT_SYMBOL(vmtruncate); 24761da177e4SLinus Torvalds 2477f6b3ec23SBadari Pulavarty int vmtruncate_range(struct inode *inode, loff_t offset, loff_t end) 2478f6b3ec23SBadari Pulavarty { 2479f6b3ec23SBadari Pulavarty struct address_space *mapping = inode->i_mapping; 2480f6b3ec23SBadari Pulavarty 2481f6b3ec23SBadari Pulavarty /* 2482f6b3ec23SBadari Pulavarty * If the underlying filesystem is not going to provide 2483f6b3ec23SBadari Pulavarty * a way to truncate a range of blocks (punch a hole) - 2484f6b3ec23SBadari Pulavarty * we should return failure right now. 2485f6b3ec23SBadari Pulavarty */ 2486acfa4380SAl Viro if (!inode->i_op->truncate_range) 2487f6b3ec23SBadari Pulavarty return -ENOSYS; 2488f6b3ec23SBadari Pulavarty 24891b1dcc1bSJes Sorensen mutex_lock(&inode->i_mutex); 2490f6b3ec23SBadari Pulavarty down_write(&inode->i_alloc_sem); 2491f6b3ec23SBadari Pulavarty unmap_mapping_range(mapping, offset, (end - offset), 1); 2492f6b3ec23SBadari Pulavarty truncate_inode_pages_range(mapping, offset, end); 2493d00806b1SNick Piggin unmap_mapping_range(mapping, offset, (end - offset), 1); 2494f6b3ec23SBadari Pulavarty inode->i_op->truncate_range(inode, offset, end); 2495f6b3ec23SBadari Pulavarty up_write(&inode->i_alloc_sem); 24961b1dcc1bSJes Sorensen mutex_unlock(&inode->i_mutex); 2497f6b3ec23SBadari Pulavarty 2498f6b3ec23SBadari Pulavarty return 0; 2499f6b3ec23SBadari Pulavarty } 2500f6b3ec23SBadari Pulavarty 25011da177e4SLinus Torvalds /* 25028f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 25038f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 25048f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 25051da177e4SLinus Torvalds */ 250665500d23SHugh Dickins static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma, 250765500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 250830c9f3a9SLinus Torvalds unsigned int flags, pte_t orig_pte) 25091da177e4SLinus Torvalds { 25108f4e2101SHugh Dickins spinlock_t *ptl; 25111da177e4SLinus Torvalds struct page *page; 251265500d23SHugh Dickins swp_entry_t entry; 25131da177e4SLinus Torvalds pte_t pte; 25147a81b88cSKAMEZAWA Hiroyuki struct mem_cgroup *ptr = NULL; 251583c54070SNick Piggin int ret = 0; 25161da177e4SLinus Torvalds 25174c21e2f2SHugh Dickins if (!pte_unmap_same(mm, pmd, page_table, orig_pte)) 25188f4e2101SHugh Dickins goto out; 251965500d23SHugh Dickins 252065500d23SHugh Dickins entry = pte_to_swp_entry(orig_pte); 25210697212aSChristoph Lameter if (is_migration_entry(entry)) { 25220697212aSChristoph Lameter migration_entry_wait(mm, pmd, address); 25230697212aSChristoph Lameter goto out; 25240697212aSChristoph Lameter } 25250ff92245SShailabh Nagar delayacct_set_flag(DELAYACCT_PF_SWAPIN); 25261da177e4SLinus Torvalds page = lookup_swap_cache(entry); 25271da177e4SLinus Torvalds if (!page) { 2528a5c9b696SHugh Dickins grab_swap_token(mm); /* Contend for token _before_ read-in */ 252902098feaSHugh Dickins page = swapin_readahead(entry, 253002098feaSHugh Dickins GFP_HIGHUSER_MOVABLE, vma, address); 25311da177e4SLinus Torvalds if (!page) { 25321da177e4SLinus Torvalds /* 25338f4e2101SHugh Dickins * Back out if somebody else faulted in this pte 25348f4e2101SHugh Dickins * while we released the pte lock. 25351da177e4SLinus Torvalds */ 25368f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 25371da177e4SLinus Torvalds if (likely(pte_same(*page_table, orig_pte))) 25381da177e4SLinus Torvalds ret = VM_FAULT_OOM; 25390ff92245SShailabh Nagar delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 254065500d23SHugh Dickins goto unlock; 25411da177e4SLinus Torvalds } 25421da177e4SLinus Torvalds 25431da177e4SLinus Torvalds /* Had to read the page from swap area: Major fault */ 25441da177e4SLinus Torvalds ret = VM_FAULT_MAJOR; 2545f8891e5eSChristoph Lameter count_vm_event(PGMAJFAULT); 25461da177e4SLinus Torvalds } 25471da177e4SLinus Torvalds 25481da177e4SLinus Torvalds lock_page(page); 254920a1022dSBalbir Singh delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 25501da177e4SLinus Torvalds 25512c26fdd7SKAMEZAWA Hiroyuki if (mem_cgroup_try_charge_swapin(mm, page, GFP_KERNEL, &ptr)) { 2552073e587eSKAMEZAWA Hiroyuki ret = VM_FAULT_OOM; 2553bc43f75cSJohannes Weiner goto out_page; 2554073e587eSKAMEZAWA Hiroyuki } 2555073e587eSKAMEZAWA Hiroyuki 25561da177e4SLinus Torvalds /* 25578f4e2101SHugh Dickins * Back out if somebody else already faulted in this pte. 25581da177e4SLinus Torvalds */ 25598f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 25609e9bef07SHugh Dickins if (unlikely(!pte_same(*page_table, orig_pte))) 2561b8107480SKirill Korotaev goto out_nomap; 2562b8107480SKirill Korotaev 2563b8107480SKirill Korotaev if (unlikely(!PageUptodate(page))) { 2564b8107480SKirill Korotaev ret = VM_FAULT_SIGBUS; 2565b8107480SKirill Korotaev goto out_nomap; 25661da177e4SLinus Torvalds } 25671da177e4SLinus Torvalds 25688c7c6e34SKAMEZAWA Hiroyuki /* 25698c7c6e34SKAMEZAWA Hiroyuki * The page isn't present yet, go ahead with the fault. 25708c7c6e34SKAMEZAWA Hiroyuki * 25718c7c6e34SKAMEZAWA Hiroyuki * Be careful about the sequence of operations here. 25728c7c6e34SKAMEZAWA Hiroyuki * To get its accounting right, reuse_swap_page() must be called 25738c7c6e34SKAMEZAWA Hiroyuki * while the page is counted on swap but not yet in mapcount i.e. 25748c7c6e34SKAMEZAWA Hiroyuki * before page_add_anon_rmap() and swap_free(); try_to_free_swap() 25758c7c6e34SKAMEZAWA Hiroyuki * must be called after the swap_free(), or it will never succeed. 257603f3c433SKAMEZAWA Hiroyuki * Because delete_from_swap_page() may be called by reuse_swap_page(), 257703f3c433SKAMEZAWA Hiroyuki * mem_cgroup_commit_charge_swapin() may not be able to find swp_entry 257803f3c433SKAMEZAWA Hiroyuki * in page->private. In this case, a record in swap_cgroup is silently 257903f3c433SKAMEZAWA Hiroyuki * discarded at swap_free(). 25808c7c6e34SKAMEZAWA Hiroyuki */ 25811da177e4SLinus Torvalds 25824294621fSHugh Dickins inc_mm_counter(mm, anon_rss); 25831da177e4SLinus Torvalds pte = mk_pte(page, vma->vm_page_prot); 258430c9f3a9SLinus Torvalds if ((flags & FAULT_FLAG_WRITE) && reuse_swap_page(page)) { 25851da177e4SLinus Torvalds pte = maybe_mkwrite(pte_mkdirty(pte), vma); 258630c9f3a9SLinus Torvalds flags &= ~FAULT_FLAG_WRITE; 25871da177e4SLinus Torvalds } 25881da177e4SLinus Torvalds flush_icache_page(vma, page); 25891da177e4SLinus Torvalds set_pte_at(mm, address, page_table, pte); 25901da177e4SLinus Torvalds page_add_anon_rmap(page, vma, address); 259103f3c433SKAMEZAWA Hiroyuki /* It's better to call commit-charge after rmap is established */ 259203f3c433SKAMEZAWA Hiroyuki mem_cgroup_commit_charge_swapin(page, ptr); 25931da177e4SLinus Torvalds 2594c475a8abSHugh Dickins swap_free(entry); 2595b291f000SNick Piggin if (vm_swap_full() || (vma->vm_flags & VM_LOCKED) || PageMlocked(page)) 2596a2c43eedSHugh Dickins try_to_free_swap(page); 2597c475a8abSHugh Dickins unlock_page(page); 2598c475a8abSHugh Dickins 259930c9f3a9SLinus Torvalds if (flags & FAULT_FLAG_WRITE) { 260061469f1dSHugh Dickins ret |= do_wp_page(mm, vma, address, page_table, pmd, ptl, pte); 260161469f1dSHugh Dickins if (ret & VM_FAULT_ERROR) 260261469f1dSHugh Dickins ret &= VM_FAULT_ERROR; 26031da177e4SLinus Torvalds goto out; 26041da177e4SLinus Torvalds } 26051da177e4SLinus Torvalds 26061da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 26071da177e4SLinus Torvalds update_mmu_cache(vma, address, pte); 260865500d23SHugh Dickins unlock: 26098f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 26101da177e4SLinus Torvalds out: 26111da177e4SLinus Torvalds return ret; 2612b8107480SKirill Korotaev out_nomap: 26137a81b88cSKAMEZAWA Hiroyuki mem_cgroup_cancel_charge_swapin(ptr); 26148f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 2615bc43f75cSJohannes Weiner out_page: 2616b8107480SKirill Korotaev unlock_page(page); 2617b8107480SKirill Korotaev page_cache_release(page); 261865500d23SHugh Dickins return ret; 26191da177e4SLinus Torvalds } 26201da177e4SLinus Torvalds 26211da177e4SLinus Torvalds /* 26228f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 26238f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 26248f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 26251da177e4SLinus Torvalds */ 262665500d23SHugh Dickins static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma, 262765500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 262830c9f3a9SLinus Torvalds unsigned int flags) 26291da177e4SLinus Torvalds { 26308f4e2101SHugh Dickins struct page *page; 26318f4e2101SHugh Dickins spinlock_t *ptl; 26321da177e4SLinus Torvalds pte_t entry; 26331da177e4SLinus Torvalds 26341da177e4SLinus Torvalds /* Allocate our own private page. */ 26351da177e4SLinus Torvalds pte_unmap(page_table); 26361da177e4SLinus Torvalds 26371da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 263865500d23SHugh Dickins goto oom; 2639769848c0SMel Gorman page = alloc_zeroed_user_highpage_movable(vma, address); 26401da177e4SLinus Torvalds if (!page) 264165500d23SHugh Dickins goto oom; 26420ed361deSNick Piggin __SetPageUptodate(page); 26431da177e4SLinus Torvalds 26442c26fdd7SKAMEZAWA Hiroyuki if (mem_cgroup_newpage_charge(page, mm, GFP_KERNEL)) 26458a9f3ccdSBalbir Singh goto oom_free_page; 26468a9f3ccdSBalbir Singh 264765500d23SHugh Dickins entry = mk_pte(page, vma->vm_page_prot); 264865500d23SHugh Dickins entry = maybe_mkwrite(pte_mkdirty(entry), vma); 26498f4e2101SHugh Dickins 26508f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 2651*9ba69294SHugh Dickins if (!pte_none(*page_table) || ksm_test_exit(mm)) 26528f4e2101SHugh Dickins goto release; 2653*9ba69294SHugh Dickins 26548f4e2101SHugh Dickins inc_mm_counter(mm, anon_rss); 26559617d95eSNick Piggin page_add_new_anon_rmap(page, vma, address); 265665500d23SHugh Dickins set_pte_at(mm, address, page_table, entry); 26571da177e4SLinus Torvalds 26581da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 265965500d23SHugh Dickins update_mmu_cache(vma, address, entry); 266065500d23SHugh Dickins unlock: 26618f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 266283c54070SNick Piggin return 0; 26638f4e2101SHugh Dickins release: 26648a9f3ccdSBalbir Singh mem_cgroup_uncharge_page(page); 26658f4e2101SHugh Dickins page_cache_release(page); 26668f4e2101SHugh Dickins goto unlock; 26678a9f3ccdSBalbir Singh oom_free_page: 26686dbf6d3bSHugh Dickins page_cache_release(page); 266965500d23SHugh Dickins oom: 26701da177e4SLinus Torvalds return VM_FAULT_OOM; 26711da177e4SLinus Torvalds } 26721da177e4SLinus Torvalds 26731da177e4SLinus Torvalds /* 267454cb8821SNick Piggin * __do_fault() tries to create a new page mapping. It aggressively 26751da177e4SLinus Torvalds * tries to share with existing pages, but makes a separate copy if 267654cb8821SNick Piggin * the FAULT_FLAG_WRITE is set in the flags parameter in order to avoid 267754cb8821SNick Piggin * the next page fault. 26781da177e4SLinus Torvalds * 26791da177e4SLinus Torvalds * As this is called only for pages that do not currently exist, we 26801da177e4SLinus Torvalds * do not need to flush old virtual caches or the TLB. 26811da177e4SLinus Torvalds * 26828f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 268316abfa08SHugh Dickins * but allow concurrent faults), and pte neither mapped nor locked. 26848f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 26851da177e4SLinus Torvalds */ 268654cb8821SNick Piggin static int __do_fault(struct mm_struct *mm, struct vm_area_struct *vma, 268716abfa08SHugh Dickins unsigned long address, pmd_t *pmd, 268854cb8821SNick Piggin pgoff_t pgoff, unsigned int flags, pte_t orig_pte) 26891da177e4SLinus Torvalds { 269016abfa08SHugh Dickins pte_t *page_table; 26918f4e2101SHugh Dickins spinlock_t *ptl; 2692d0217ac0SNick Piggin struct page *page; 26931da177e4SLinus Torvalds pte_t entry; 26941da177e4SLinus Torvalds int anon = 0; 26955b4e655eSKAMEZAWA Hiroyuki int charged = 0; 2696d08b3851SPeter Zijlstra struct page *dirty_page = NULL; 2697d0217ac0SNick Piggin struct vm_fault vmf; 2698d0217ac0SNick Piggin int ret; 2699a200ee18SPeter Zijlstra int page_mkwrite = 0; 270054cb8821SNick Piggin 2701d0217ac0SNick Piggin vmf.virtual_address = (void __user *)(address & PAGE_MASK); 2702d0217ac0SNick Piggin vmf.pgoff = pgoff; 2703d0217ac0SNick Piggin vmf.flags = flags; 2704d0217ac0SNick Piggin vmf.page = NULL; 27051da177e4SLinus Torvalds 2706d0217ac0SNick Piggin ret = vma->vm_ops->fault(vma, &vmf); 270783c54070SNick Piggin if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) 270883c54070SNick Piggin return ret; 27091da177e4SLinus Torvalds 2710d00806b1SNick Piggin /* 2711d0217ac0SNick Piggin * For consistency in subsequent calls, make the faulted page always 2712d00806b1SNick Piggin * locked. 2713d00806b1SNick Piggin */ 271483c54070SNick Piggin if (unlikely(!(ret & VM_FAULT_LOCKED))) 2715d0217ac0SNick Piggin lock_page(vmf.page); 271654cb8821SNick Piggin else 2717d0217ac0SNick Piggin VM_BUG_ON(!PageLocked(vmf.page)); 2718d00806b1SNick Piggin 27191da177e4SLinus Torvalds /* 27201da177e4SLinus Torvalds * Should we do an early C-O-W break? 27211da177e4SLinus Torvalds */ 2722d0217ac0SNick Piggin page = vmf.page; 272354cb8821SNick Piggin if (flags & FAULT_FLAG_WRITE) { 27249637a5efSDavid Howells if (!(vma->vm_flags & VM_SHARED)) { 272554cb8821SNick Piggin anon = 1; 2726d00806b1SNick Piggin if (unlikely(anon_vma_prepare(vma))) { 2727d0217ac0SNick Piggin ret = VM_FAULT_OOM; 272854cb8821SNick Piggin goto out; 2729d00806b1SNick Piggin } 273083c54070SNick Piggin page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, 273183c54070SNick Piggin vma, address); 2732d00806b1SNick Piggin if (!page) { 2733d0217ac0SNick Piggin ret = VM_FAULT_OOM; 273454cb8821SNick Piggin goto out; 2735d00806b1SNick Piggin } 27362c26fdd7SKAMEZAWA Hiroyuki if (mem_cgroup_newpage_charge(page, mm, GFP_KERNEL)) { 27375b4e655eSKAMEZAWA Hiroyuki ret = VM_FAULT_OOM; 27385b4e655eSKAMEZAWA Hiroyuki page_cache_release(page); 27395b4e655eSKAMEZAWA Hiroyuki goto out; 27405b4e655eSKAMEZAWA Hiroyuki } 27415b4e655eSKAMEZAWA Hiroyuki charged = 1; 2742b291f000SNick Piggin /* 2743b291f000SNick Piggin * Don't let another task, with possibly unlocked vma, 2744b291f000SNick Piggin * keep the mlocked page. 2745b291f000SNick Piggin */ 2746b291f000SNick Piggin if (vma->vm_flags & VM_LOCKED) 2747b291f000SNick Piggin clear_page_mlock(vmf.page); 2748d0217ac0SNick Piggin copy_user_highpage(page, vmf.page, address, vma); 27490ed361deSNick Piggin __SetPageUptodate(page); 27509637a5efSDavid Howells } else { 275154cb8821SNick Piggin /* 275254cb8821SNick Piggin * If the page will be shareable, see if the backing 27539637a5efSDavid Howells * address space wants to know that the page is about 275454cb8821SNick Piggin * to become writable 275554cb8821SNick Piggin */ 275669676147SMark Fasheh if (vma->vm_ops->page_mkwrite) { 2757c2ec175cSNick Piggin int tmp; 2758c2ec175cSNick Piggin 275969676147SMark Fasheh unlock_page(page); 2760b827e496SNick Piggin vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE; 2761c2ec175cSNick Piggin tmp = vma->vm_ops->page_mkwrite(vma, &vmf); 2762c2ec175cSNick Piggin if (unlikely(tmp & 2763c2ec175cSNick Piggin (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) { 2764c2ec175cSNick Piggin ret = tmp; 2765b827e496SNick Piggin goto unwritable_page; 276669676147SMark Fasheh } 2767b827e496SNick Piggin if (unlikely(!(tmp & VM_FAULT_LOCKED))) { 276869676147SMark Fasheh lock_page(page); 2769d0217ac0SNick Piggin if (!page->mapping) { 2770b827e496SNick Piggin ret = 0; /* retry the fault */ 2771b827e496SNick Piggin unlock_page(page); 2772b827e496SNick Piggin goto unwritable_page; 2773d0217ac0SNick Piggin } 2774b827e496SNick Piggin } else 2775b827e496SNick Piggin VM_BUG_ON(!PageLocked(page)); 2776a200ee18SPeter Zijlstra page_mkwrite = 1; 27779637a5efSDavid Howells } 27789637a5efSDavid Howells } 277954cb8821SNick Piggin 27801da177e4SLinus Torvalds } 27811da177e4SLinus Torvalds 27828f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 27831da177e4SLinus Torvalds 27841da177e4SLinus Torvalds /* 27851da177e4SLinus Torvalds * This silly early PAGE_DIRTY setting removes a race 27861da177e4SLinus Torvalds * due to the bad i386 page protection. But it's valid 27871da177e4SLinus Torvalds * for other architectures too. 27881da177e4SLinus Torvalds * 278930c9f3a9SLinus Torvalds * Note that if FAULT_FLAG_WRITE is set, we either now have 27901da177e4SLinus Torvalds * an exclusive copy of the page, or this is a shared mapping, 27911da177e4SLinus Torvalds * so we can make it writable and dirty to avoid having to 27921da177e4SLinus Torvalds * handle that later. 27931da177e4SLinus Torvalds */ 27941da177e4SLinus Torvalds /* Only go through if we didn't race with anybody else... */ 2795*9ba69294SHugh Dickins if (likely(pte_same(*page_table, orig_pte) && !ksm_test_exit(mm))) { 2796d00806b1SNick Piggin flush_icache_page(vma, page); 2797d00806b1SNick Piggin entry = mk_pte(page, vma->vm_page_prot); 279854cb8821SNick Piggin if (flags & FAULT_FLAG_WRITE) 27991da177e4SLinus Torvalds entry = maybe_mkwrite(pte_mkdirty(entry), vma); 28001da177e4SLinus Torvalds if (anon) { 28014294621fSHugh Dickins inc_mm_counter(mm, anon_rss); 2802d00806b1SNick Piggin page_add_new_anon_rmap(page, vma, address); 2803f57e88a8SHugh Dickins } else { 28044294621fSHugh Dickins inc_mm_counter(mm, file_rss); 2805d00806b1SNick Piggin page_add_file_rmap(page); 280654cb8821SNick Piggin if (flags & FAULT_FLAG_WRITE) { 2807d00806b1SNick Piggin dirty_page = page; 2808d08b3851SPeter Zijlstra get_page(dirty_page); 2809d08b3851SPeter Zijlstra } 28104294621fSHugh Dickins } 281164d6519dSLee Schermerhorn set_pte_at(mm, address, page_table, entry); 28121da177e4SLinus Torvalds 2813d00806b1SNick Piggin /* no need to invalidate: a not-present page won't be cached */ 28141da177e4SLinus Torvalds update_mmu_cache(vma, address, entry); 2815d00806b1SNick Piggin } else { 28165b4e655eSKAMEZAWA Hiroyuki if (charged) 28178a9f3ccdSBalbir Singh mem_cgroup_uncharge_page(page); 2818d00806b1SNick Piggin if (anon) 2819d00806b1SNick Piggin page_cache_release(page); 2820d00806b1SNick Piggin else 282154cb8821SNick Piggin anon = 1; /* no anon but release faulted_page */ 2822d00806b1SNick Piggin } 2823d00806b1SNick Piggin 28248f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 2825d00806b1SNick Piggin 2826d00806b1SNick Piggin out: 2827b827e496SNick Piggin if (dirty_page) { 2828b827e496SNick Piggin struct address_space *mapping = page->mapping; 28298f7b3d15SAnton Salikhmetov 2830b827e496SNick Piggin if (set_page_dirty(dirty_page)) 2831b827e496SNick Piggin page_mkwrite = 1; 2832b827e496SNick Piggin unlock_page(dirty_page); 2833d08b3851SPeter Zijlstra put_page(dirty_page); 2834b827e496SNick Piggin if (page_mkwrite && mapping) { 2835b827e496SNick Piggin /* 2836b827e496SNick Piggin * Some device drivers do not set page.mapping but still 2837b827e496SNick Piggin * dirty their pages 2838b827e496SNick Piggin */ 2839b827e496SNick Piggin balance_dirty_pages_ratelimited(mapping); 2840d08b3851SPeter Zijlstra } 2841d00806b1SNick Piggin 2842b827e496SNick Piggin /* file_update_time outside page_lock */ 2843b827e496SNick Piggin if (vma->vm_file) 2844b827e496SNick Piggin file_update_time(vma->vm_file); 2845b827e496SNick Piggin } else { 2846b827e496SNick Piggin unlock_page(vmf.page); 2847b827e496SNick Piggin if (anon) 2848b827e496SNick Piggin page_cache_release(vmf.page); 2849b827e496SNick Piggin } 2850b827e496SNick Piggin 2851b827e496SNick Piggin return ret; 2852b827e496SNick Piggin 2853b827e496SNick Piggin unwritable_page: 2854b827e496SNick Piggin page_cache_release(page); 285583c54070SNick Piggin return ret; 285654cb8821SNick Piggin } 2857d00806b1SNick Piggin 285854cb8821SNick Piggin static int do_linear_fault(struct mm_struct *mm, struct vm_area_struct *vma, 285954cb8821SNick Piggin unsigned long address, pte_t *page_table, pmd_t *pmd, 286030c9f3a9SLinus Torvalds unsigned int flags, pte_t orig_pte) 286154cb8821SNick Piggin { 286254cb8821SNick Piggin pgoff_t pgoff = (((address & PAGE_MASK) 28630da7e01fSDean Nelson - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; 286454cb8821SNick Piggin 286516abfa08SHugh Dickins pte_unmap(page_table); 286616abfa08SHugh Dickins return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte); 286754cb8821SNick Piggin } 286854cb8821SNick Piggin 2869f4b81804SJes Sorensen /* 28701da177e4SLinus Torvalds * Fault of a previously existing named mapping. Repopulate the pte 28711da177e4SLinus Torvalds * from the encoded file_pte if possible. This enables swappable 28721da177e4SLinus Torvalds * nonlinear vmas. 28738f4e2101SHugh Dickins * 28748f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 28758f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 28768f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 28771da177e4SLinus Torvalds */ 2878d0217ac0SNick Piggin static int do_nonlinear_fault(struct mm_struct *mm, struct vm_area_struct *vma, 287965500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 288030c9f3a9SLinus Torvalds unsigned int flags, pte_t orig_pte) 28811da177e4SLinus Torvalds { 288265500d23SHugh Dickins pgoff_t pgoff; 28831da177e4SLinus Torvalds 288430c9f3a9SLinus Torvalds flags |= FAULT_FLAG_NONLINEAR; 288530c9f3a9SLinus Torvalds 28864c21e2f2SHugh Dickins if (!pte_unmap_same(mm, pmd, page_table, orig_pte)) 288783c54070SNick Piggin return 0; 28881da177e4SLinus Torvalds 28892509ef26SHugh Dickins if (unlikely(!(vma->vm_flags & VM_NONLINEAR))) { 289065500d23SHugh Dickins /* 289165500d23SHugh Dickins * Page table corrupted: show pte and kill process. 289265500d23SHugh Dickins */ 28933dc14741SHugh Dickins print_bad_pte(vma, address, orig_pte, NULL); 289465500d23SHugh Dickins return VM_FAULT_OOM; 289565500d23SHugh Dickins } 289665500d23SHugh Dickins 289765500d23SHugh Dickins pgoff = pte_to_pgoff(orig_pte); 289816abfa08SHugh Dickins return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte); 28991da177e4SLinus Torvalds } 29001da177e4SLinus Torvalds 29011da177e4SLinus Torvalds /* 29021da177e4SLinus Torvalds * These routines also need to handle stuff like marking pages dirty 29031da177e4SLinus Torvalds * and/or accessed for architectures that don't do it in hardware (most 29041da177e4SLinus Torvalds * RISC architectures). The early dirtying is also good on the i386. 29051da177e4SLinus Torvalds * 29061da177e4SLinus Torvalds * There is also a hook called "update_mmu_cache()" that architectures 29071da177e4SLinus Torvalds * with external mmu caches can use to update those (ie the Sparc or 29081da177e4SLinus Torvalds * PowerPC hashed page tables that act as extended TLBs). 29091da177e4SLinus Torvalds * 2910c74df32cSHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 2911c74df32cSHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 2912c74df32cSHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 29131da177e4SLinus Torvalds */ 29141da177e4SLinus Torvalds static inline int handle_pte_fault(struct mm_struct *mm, 29151da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long address, 291630c9f3a9SLinus Torvalds pte_t *pte, pmd_t *pmd, unsigned int flags) 29171da177e4SLinus Torvalds { 29181da177e4SLinus Torvalds pte_t entry; 29198f4e2101SHugh Dickins spinlock_t *ptl; 29201da177e4SLinus Torvalds 29218dab5241SBenjamin Herrenschmidt entry = *pte; 29221da177e4SLinus Torvalds if (!pte_present(entry)) { 292365500d23SHugh Dickins if (pte_none(entry)) { 2924f4b81804SJes Sorensen if (vma->vm_ops) { 29253c18ddd1SNick Piggin if (likely(vma->vm_ops->fault)) 292654cb8821SNick Piggin return do_linear_fault(mm, vma, address, 292730c9f3a9SLinus Torvalds pte, pmd, flags, entry); 2928f4b81804SJes Sorensen } 2929f4b81804SJes Sorensen return do_anonymous_page(mm, vma, address, 293030c9f3a9SLinus Torvalds pte, pmd, flags); 293165500d23SHugh Dickins } 29321da177e4SLinus Torvalds if (pte_file(entry)) 2933d0217ac0SNick Piggin return do_nonlinear_fault(mm, vma, address, 293430c9f3a9SLinus Torvalds pte, pmd, flags, entry); 293565500d23SHugh Dickins return do_swap_page(mm, vma, address, 293630c9f3a9SLinus Torvalds pte, pmd, flags, entry); 29371da177e4SLinus Torvalds } 29381da177e4SLinus Torvalds 29394c21e2f2SHugh Dickins ptl = pte_lockptr(mm, pmd); 29408f4e2101SHugh Dickins spin_lock(ptl); 29418f4e2101SHugh Dickins if (unlikely(!pte_same(*pte, entry))) 29428f4e2101SHugh Dickins goto unlock; 294330c9f3a9SLinus Torvalds if (flags & FAULT_FLAG_WRITE) { 29441da177e4SLinus Torvalds if (!pte_write(entry)) 29458f4e2101SHugh Dickins return do_wp_page(mm, vma, address, 29468f4e2101SHugh Dickins pte, pmd, ptl, entry); 29471da177e4SLinus Torvalds entry = pte_mkdirty(entry); 29481da177e4SLinus Torvalds } 29491da177e4SLinus Torvalds entry = pte_mkyoung(entry); 295030c9f3a9SLinus Torvalds if (ptep_set_access_flags(vma, address, pte, entry, flags & FAULT_FLAG_WRITE)) { 29511da177e4SLinus Torvalds update_mmu_cache(vma, address, entry); 29521a44e149SAndrea Arcangeli } else { 29531a44e149SAndrea Arcangeli /* 29541a44e149SAndrea Arcangeli * This is needed only for protection faults but the arch code 29551a44e149SAndrea Arcangeli * is not yet telling us if this is a protection fault or not. 29561a44e149SAndrea Arcangeli * This still avoids useless tlb flushes for .text page faults 29571a44e149SAndrea Arcangeli * with threads. 29581a44e149SAndrea Arcangeli */ 295930c9f3a9SLinus Torvalds if (flags & FAULT_FLAG_WRITE) 29601a44e149SAndrea Arcangeli flush_tlb_page(vma, address); 29611a44e149SAndrea Arcangeli } 29628f4e2101SHugh Dickins unlock: 29638f4e2101SHugh Dickins pte_unmap_unlock(pte, ptl); 296483c54070SNick Piggin return 0; 29651da177e4SLinus Torvalds } 29661da177e4SLinus Torvalds 29671da177e4SLinus Torvalds /* 29681da177e4SLinus Torvalds * By the time we get here, we already hold the mm semaphore 29691da177e4SLinus Torvalds */ 297083c54070SNick Piggin int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma, 2971d06063ccSLinus Torvalds unsigned long address, unsigned int flags) 29721da177e4SLinus Torvalds { 29731da177e4SLinus Torvalds pgd_t *pgd; 29741da177e4SLinus Torvalds pud_t *pud; 29751da177e4SLinus Torvalds pmd_t *pmd; 29761da177e4SLinus Torvalds pte_t *pte; 29771da177e4SLinus Torvalds 29781da177e4SLinus Torvalds __set_current_state(TASK_RUNNING); 29791da177e4SLinus Torvalds 2980f8891e5eSChristoph Lameter count_vm_event(PGFAULT); 29811da177e4SLinus Torvalds 2982ac9b9c66SHugh Dickins if (unlikely(is_vm_hugetlb_page(vma))) 298330c9f3a9SLinus Torvalds return hugetlb_fault(mm, vma, address, flags); 29841da177e4SLinus Torvalds 29851da177e4SLinus Torvalds pgd = pgd_offset(mm, address); 29861da177e4SLinus Torvalds pud = pud_alloc(mm, pgd, address); 29871da177e4SLinus Torvalds if (!pud) 2988c74df32cSHugh Dickins return VM_FAULT_OOM; 29891da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, address); 29901da177e4SLinus Torvalds if (!pmd) 2991c74df32cSHugh Dickins return VM_FAULT_OOM; 29921da177e4SLinus Torvalds pte = pte_alloc_map(mm, pmd, address); 29931da177e4SLinus Torvalds if (!pte) 2994c74df32cSHugh Dickins return VM_FAULT_OOM; 29951da177e4SLinus Torvalds 299630c9f3a9SLinus Torvalds return handle_pte_fault(mm, vma, address, pte, pmd, flags); 29971da177e4SLinus Torvalds } 29981da177e4SLinus Torvalds 29991da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED 30001da177e4SLinus Torvalds /* 30011da177e4SLinus Torvalds * Allocate page upper directory. 3002872fec16SHugh Dickins * We've already handled the fast-path in-line. 30031da177e4SLinus Torvalds */ 30041bb3630eSHugh Dickins int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address) 30051da177e4SLinus Torvalds { 3006c74df32cSHugh Dickins pud_t *new = pud_alloc_one(mm, address); 3007c74df32cSHugh Dickins if (!new) 30081bb3630eSHugh Dickins return -ENOMEM; 30091da177e4SLinus Torvalds 3010362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 3011362a61adSNick Piggin 3012872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 30131bb3630eSHugh Dickins if (pgd_present(*pgd)) /* Another has populated it */ 30145e541973SBenjamin Herrenschmidt pud_free(mm, new); 30151bb3630eSHugh Dickins else 30161da177e4SLinus Torvalds pgd_populate(mm, pgd, new); 3017872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 30181bb3630eSHugh Dickins return 0; 30191da177e4SLinus Torvalds } 30201da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */ 30211da177e4SLinus Torvalds 30221da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED 30231da177e4SLinus Torvalds /* 30241da177e4SLinus Torvalds * Allocate page middle directory. 3025872fec16SHugh Dickins * We've already handled the fast-path in-line. 30261da177e4SLinus Torvalds */ 30271bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) 30281da177e4SLinus Torvalds { 3029c74df32cSHugh Dickins pmd_t *new = pmd_alloc_one(mm, address); 3030c74df32cSHugh Dickins if (!new) 30311bb3630eSHugh Dickins return -ENOMEM; 30321da177e4SLinus Torvalds 3033362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 3034362a61adSNick Piggin 3035872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 30361da177e4SLinus Torvalds #ifndef __ARCH_HAS_4LEVEL_HACK 30371bb3630eSHugh Dickins if (pud_present(*pud)) /* Another has populated it */ 30385e541973SBenjamin Herrenschmidt pmd_free(mm, new); 30391bb3630eSHugh Dickins else 30401da177e4SLinus Torvalds pud_populate(mm, pud, new); 30411da177e4SLinus Torvalds #else 30421bb3630eSHugh Dickins if (pgd_present(*pud)) /* Another has populated it */ 30435e541973SBenjamin Herrenschmidt pmd_free(mm, new); 30441bb3630eSHugh Dickins else 30451da177e4SLinus Torvalds pgd_populate(mm, pud, new); 30461da177e4SLinus Torvalds #endif /* __ARCH_HAS_4LEVEL_HACK */ 3047872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 30481bb3630eSHugh Dickins return 0; 30491da177e4SLinus Torvalds } 30501da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */ 30511da177e4SLinus Torvalds 30521da177e4SLinus Torvalds int make_pages_present(unsigned long addr, unsigned long end) 30531da177e4SLinus Torvalds { 30541da177e4SLinus Torvalds int ret, len, write; 30551da177e4SLinus Torvalds struct vm_area_struct * vma; 30561da177e4SLinus Torvalds 30571da177e4SLinus Torvalds vma = find_vma(current->mm, addr); 30581da177e4SLinus Torvalds if (!vma) 3059a477097dSKOSAKI Motohiro return -ENOMEM; 30601da177e4SLinus Torvalds write = (vma->vm_flags & VM_WRITE) != 0; 30615bcb28b1SEric Sesterhenn BUG_ON(addr >= end); 30625bcb28b1SEric Sesterhenn BUG_ON(end > vma->vm_end); 306368e116a3SRolf Eike Beer len = DIV_ROUND_UP(end, PAGE_SIZE) - addr/PAGE_SIZE; 30641da177e4SLinus Torvalds ret = get_user_pages(current, current->mm, addr, 30651da177e4SLinus Torvalds len, write, 0, NULL, NULL); 3066c11d69d8SLee Schermerhorn if (ret < 0) 30671da177e4SLinus Torvalds return ret; 30689978ad58SLee Schermerhorn return ret == len ? 0 : -EFAULT; 30691da177e4SLinus Torvalds } 30701da177e4SLinus Torvalds 30711da177e4SLinus Torvalds #if !defined(__HAVE_ARCH_GATE_AREA) 30721da177e4SLinus Torvalds 30731da177e4SLinus Torvalds #if defined(AT_SYSINFO_EHDR) 30745ce7852cSAdrian Bunk static struct vm_area_struct gate_vma; 30751da177e4SLinus Torvalds 30761da177e4SLinus Torvalds static int __init gate_vma_init(void) 30771da177e4SLinus Torvalds { 30781da177e4SLinus Torvalds gate_vma.vm_mm = NULL; 30791da177e4SLinus Torvalds gate_vma.vm_start = FIXADDR_USER_START; 30801da177e4SLinus Torvalds gate_vma.vm_end = FIXADDR_USER_END; 3081b6558c4aSRoland McGrath gate_vma.vm_flags = VM_READ | VM_MAYREAD | VM_EXEC | VM_MAYEXEC; 3082b6558c4aSRoland McGrath gate_vma.vm_page_prot = __P101; 3083f47aef55SRoland McGrath /* 3084f47aef55SRoland McGrath * Make sure the vDSO gets into every core dump. 3085f47aef55SRoland McGrath * Dumping its contents makes post-mortem fully interpretable later 3086f47aef55SRoland McGrath * without matching up the same kernel and hardware config to see 3087f47aef55SRoland McGrath * what PC values meant. 3088f47aef55SRoland McGrath */ 3089f47aef55SRoland McGrath gate_vma.vm_flags |= VM_ALWAYSDUMP; 30901da177e4SLinus Torvalds return 0; 30911da177e4SLinus Torvalds } 30921da177e4SLinus Torvalds __initcall(gate_vma_init); 30931da177e4SLinus Torvalds #endif 30941da177e4SLinus Torvalds 30951da177e4SLinus Torvalds struct vm_area_struct *get_gate_vma(struct task_struct *tsk) 30961da177e4SLinus Torvalds { 30971da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR 30981da177e4SLinus Torvalds return &gate_vma; 30991da177e4SLinus Torvalds #else 31001da177e4SLinus Torvalds return NULL; 31011da177e4SLinus Torvalds #endif 31021da177e4SLinus Torvalds } 31031da177e4SLinus Torvalds 31041da177e4SLinus Torvalds int in_gate_area_no_task(unsigned long addr) 31051da177e4SLinus Torvalds { 31061da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR 31071da177e4SLinus Torvalds if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END)) 31081da177e4SLinus Torvalds return 1; 31091da177e4SLinus Torvalds #endif 31101da177e4SLinus Torvalds return 0; 31111da177e4SLinus Torvalds } 31121da177e4SLinus Torvalds 31131da177e4SLinus Torvalds #endif /* __HAVE_ARCH_GATE_AREA */ 31140ec76a11SDavid Howells 3115f8ad0f49SJohannes Weiner static int follow_pte(struct mm_struct *mm, unsigned long address, 3116f8ad0f49SJohannes Weiner pte_t **ptepp, spinlock_t **ptlp) 3117f8ad0f49SJohannes Weiner { 3118f8ad0f49SJohannes Weiner pgd_t *pgd; 3119f8ad0f49SJohannes Weiner pud_t *pud; 3120f8ad0f49SJohannes Weiner pmd_t *pmd; 3121f8ad0f49SJohannes Weiner pte_t *ptep; 3122f8ad0f49SJohannes Weiner 3123f8ad0f49SJohannes Weiner pgd = pgd_offset(mm, address); 3124f8ad0f49SJohannes Weiner if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) 3125f8ad0f49SJohannes Weiner goto out; 3126f8ad0f49SJohannes Weiner 3127f8ad0f49SJohannes Weiner pud = pud_offset(pgd, address); 3128f8ad0f49SJohannes Weiner if (pud_none(*pud) || unlikely(pud_bad(*pud))) 3129f8ad0f49SJohannes Weiner goto out; 3130f8ad0f49SJohannes Weiner 3131f8ad0f49SJohannes Weiner pmd = pmd_offset(pud, address); 3132f8ad0f49SJohannes Weiner if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd))) 3133f8ad0f49SJohannes Weiner goto out; 3134f8ad0f49SJohannes Weiner 3135f8ad0f49SJohannes Weiner /* We cannot handle huge page PFN maps. Luckily they don't exist. */ 3136f8ad0f49SJohannes Weiner if (pmd_huge(*pmd)) 3137f8ad0f49SJohannes Weiner goto out; 3138f8ad0f49SJohannes Weiner 3139f8ad0f49SJohannes Weiner ptep = pte_offset_map_lock(mm, pmd, address, ptlp); 3140f8ad0f49SJohannes Weiner if (!ptep) 3141f8ad0f49SJohannes Weiner goto out; 3142f8ad0f49SJohannes Weiner if (!pte_present(*ptep)) 3143f8ad0f49SJohannes Weiner goto unlock; 3144f8ad0f49SJohannes Weiner *ptepp = ptep; 3145f8ad0f49SJohannes Weiner return 0; 3146f8ad0f49SJohannes Weiner unlock: 3147f8ad0f49SJohannes Weiner pte_unmap_unlock(ptep, *ptlp); 3148f8ad0f49SJohannes Weiner out: 3149f8ad0f49SJohannes Weiner return -EINVAL; 3150f8ad0f49SJohannes Weiner } 3151f8ad0f49SJohannes Weiner 31523b6748e2SJohannes Weiner /** 31533b6748e2SJohannes Weiner * follow_pfn - look up PFN at a user virtual address 31543b6748e2SJohannes Weiner * @vma: memory mapping 31553b6748e2SJohannes Weiner * @address: user virtual address 31563b6748e2SJohannes Weiner * @pfn: location to store found PFN 31573b6748e2SJohannes Weiner * 31583b6748e2SJohannes Weiner * Only IO mappings and raw PFN mappings are allowed. 31593b6748e2SJohannes Weiner * 31603b6748e2SJohannes Weiner * Returns zero and the pfn at @pfn on success, -ve otherwise. 31613b6748e2SJohannes Weiner */ 31623b6748e2SJohannes Weiner int follow_pfn(struct vm_area_struct *vma, unsigned long address, 31633b6748e2SJohannes Weiner unsigned long *pfn) 31643b6748e2SJohannes Weiner { 31653b6748e2SJohannes Weiner int ret = -EINVAL; 31663b6748e2SJohannes Weiner spinlock_t *ptl; 31673b6748e2SJohannes Weiner pte_t *ptep; 31683b6748e2SJohannes Weiner 31693b6748e2SJohannes Weiner if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 31703b6748e2SJohannes Weiner return ret; 31713b6748e2SJohannes Weiner 31723b6748e2SJohannes Weiner ret = follow_pte(vma->vm_mm, address, &ptep, &ptl); 31733b6748e2SJohannes Weiner if (ret) 31743b6748e2SJohannes Weiner return ret; 31753b6748e2SJohannes Weiner *pfn = pte_pfn(*ptep); 31763b6748e2SJohannes Weiner pte_unmap_unlock(ptep, ptl); 31773b6748e2SJohannes Weiner return 0; 31783b6748e2SJohannes Weiner } 31793b6748e2SJohannes Weiner EXPORT_SYMBOL(follow_pfn); 31803b6748e2SJohannes Weiner 318128b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT 3182d87fe660Svenkatesh.pallipadi@intel.com int follow_phys(struct vm_area_struct *vma, 318328b2ee20SRik van Riel unsigned long address, unsigned int flags, 3184d87fe660Svenkatesh.pallipadi@intel.com unsigned long *prot, resource_size_t *phys) 318528b2ee20SRik van Riel { 318603668a4dSJohannes Weiner int ret = -EINVAL; 318728b2ee20SRik van Riel pte_t *ptep, pte; 318828b2ee20SRik van Riel spinlock_t *ptl; 318928b2ee20SRik van Riel 3190d87fe660Svenkatesh.pallipadi@intel.com if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 3191d87fe660Svenkatesh.pallipadi@intel.com goto out; 319228b2ee20SRik van Riel 319303668a4dSJohannes Weiner if (follow_pte(vma->vm_mm, address, &ptep, &ptl)) 3194d87fe660Svenkatesh.pallipadi@intel.com goto out; 319528b2ee20SRik van Riel pte = *ptep; 319603668a4dSJohannes Weiner 319728b2ee20SRik van Riel if ((flags & FOLL_WRITE) && !pte_write(pte)) 319828b2ee20SRik van Riel goto unlock; 319928b2ee20SRik van Riel 320028b2ee20SRik van Riel *prot = pgprot_val(pte_pgprot(pte)); 320103668a4dSJohannes Weiner *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT; 320228b2ee20SRik van Riel 320303668a4dSJohannes Weiner ret = 0; 320428b2ee20SRik van Riel unlock: 320528b2ee20SRik van Riel pte_unmap_unlock(ptep, ptl); 320628b2ee20SRik van Riel out: 3207d87fe660Svenkatesh.pallipadi@intel.com return ret; 320828b2ee20SRik van Riel } 320928b2ee20SRik van Riel 321028b2ee20SRik van Riel int generic_access_phys(struct vm_area_struct *vma, unsigned long addr, 321128b2ee20SRik van Riel void *buf, int len, int write) 321228b2ee20SRik van Riel { 321328b2ee20SRik van Riel resource_size_t phys_addr; 321428b2ee20SRik van Riel unsigned long prot = 0; 32152bc7273bSKOSAKI Motohiro void __iomem *maddr; 321628b2ee20SRik van Riel int offset = addr & (PAGE_SIZE-1); 321728b2ee20SRik van Riel 3218d87fe660Svenkatesh.pallipadi@intel.com if (follow_phys(vma, addr, write, &prot, &phys_addr)) 321928b2ee20SRik van Riel return -EINVAL; 322028b2ee20SRik van Riel 322128b2ee20SRik van Riel maddr = ioremap_prot(phys_addr, PAGE_SIZE, prot); 322228b2ee20SRik van Riel if (write) 322328b2ee20SRik van Riel memcpy_toio(maddr + offset, buf, len); 322428b2ee20SRik van Riel else 322528b2ee20SRik van Riel memcpy_fromio(buf, maddr + offset, len); 322628b2ee20SRik van Riel iounmap(maddr); 322728b2ee20SRik van Riel 322828b2ee20SRik van Riel return len; 322928b2ee20SRik van Riel } 323028b2ee20SRik van Riel #endif 323128b2ee20SRik van Riel 32320ec76a11SDavid Howells /* 32330ec76a11SDavid Howells * Access another process' address space. 32340ec76a11SDavid Howells * Source/target buffer must be kernel space, 32350ec76a11SDavid Howells * Do not walk the page table directly, use get_user_pages 32360ec76a11SDavid Howells */ 32370ec76a11SDavid Howells int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write) 32380ec76a11SDavid Howells { 32390ec76a11SDavid Howells struct mm_struct *mm; 32400ec76a11SDavid Howells struct vm_area_struct *vma; 32410ec76a11SDavid Howells void *old_buf = buf; 32420ec76a11SDavid Howells 32430ec76a11SDavid Howells mm = get_task_mm(tsk); 32440ec76a11SDavid Howells if (!mm) 32450ec76a11SDavid Howells return 0; 32460ec76a11SDavid Howells 32470ec76a11SDavid Howells down_read(&mm->mmap_sem); 3248183ff22bSSimon Arlott /* ignore errors, just check how much was successfully transferred */ 32490ec76a11SDavid Howells while (len) { 32500ec76a11SDavid Howells int bytes, ret, offset; 32510ec76a11SDavid Howells void *maddr; 325228b2ee20SRik van Riel struct page *page = NULL; 32530ec76a11SDavid Howells 32540ec76a11SDavid Howells ret = get_user_pages(tsk, mm, addr, 1, 32550ec76a11SDavid Howells write, 1, &page, &vma); 325628b2ee20SRik van Riel if (ret <= 0) { 325728b2ee20SRik van Riel /* 325828b2ee20SRik van Riel * Check if this is a VM_IO | VM_PFNMAP VMA, which 325928b2ee20SRik van Riel * we can access using slightly different code. 326028b2ee20SRik van Riel */ 326128b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT 326228b2ee20SRik van Riel vma = find_vma(mm, addr); 326328b2ee20SRik van Riel if (!vma) 32640ec76a11SDavid Howells break; 326528b2ee20SRik van Riel if (vma->vm_ops && vma->vm_ops->access) 326628b2ee20SRik van Riel ret = vma->vm_ops->access(vma, addr, buf, 326728b2ee20SRik van Riel len, write); 326828b2ee20SRik van Riel if (ret <= 0) 326928b2ee20SRik van Riel #endif 327028b2ee20SRik van Riel break; 327128b2ee20SRik van Riel bytes = ret; 327228b2ee20SRik van Riel } else { 32730ec76a11SDavid Howells bytes = len; 32740ec76a11SDavid Howells offset = addr & (PAGE_SIZE-1); 32750ec76a11SDavid Howells if (bytes > PAGE_SIZE-offset) 32760ec76a11SDavid Howells bytes = PAGE_SIZE-offset; 32770ec76a11SDavid Howells 32780ec76a11SDavid Howells maddr = kmap(page); 32790ec76a11SDavid Howells if (write) { 32800ec76a11SDavid Howells copy_to_user_page(vma, page, addr, 32810ec76a11SDavid Howells maddr + offset, buf, bytes); 32820ec76a11SDavid Howells set_page_dirty_lock(page); 32830ec76a11SDavid Howells } else { 32840ec76a11SDavid Howells copy_from_user_page(vma, page, addr, 32850ec76a11SDavid Howells buf, maddr + offset, bytes); 32860ec76a11SDavid Howells } 32870ec76a11SDavid Howells kunmap(page); 32880ec76a11SDavid Howells page_cache_release(page); 328928b2ee20SRik van Riel } 32900ec76a11SDavid Howells len -= bytes; 32910ec76a11SDavid Howells buf += bytes; 32920ec76a11SDavid Howells addr += bytes; 32930ec76a11SDavid Howells } 32940ec76a11SDavid Howells up_read(&mm->mmap_sem); 32950ec76a11SDavid Howells mmput(mm); 32960ec76a11SDavid Howells 32970ec76a11SDavid Howells return buf - old_buf; 32980ec76a11SDavid Howells } 329903252919SAndi Kleen 330003252919SAndi Kleen /* 330103252919SAndi Kleen * Print the name of a VMA. 330203252919SAndi Kleen */ 330303252919SAndi Kleen void print_vma_addr(char *prefix, unsigned long ip) 330403252919SAndi Kleen { 330503252919SAndi Kleen struct mm_struct *mm = current->mm; 330603252919SAndi Kleen struct vm_area_struct *vma; 330703252919SAndi Kleen 3308e8bff74aSIngo Molnar /* 3309e8bff74aSIngo Molnar * Do not print if we are in atomic 3310e8bff74aSIngo Molnar * contexts (in exception stacks, etc.): 3311e8bff74aSIngo Molnar */ 3312e8bff74aSIngo Molnar if (preempt_count()) 3313e8bff74aSIngo Molnar return; 3314e8bff74aSIngo Molnar 331503252919SAndi Kleen down_read(&mm->mmap_sem); 331603252919SAndi Kleen vma = find_vma(mm, ip); 331703252919SAndi Kleen if (vma && vma->vm_file) { 331803252919SAndi Kleen struct file *f = vma->vm_file; 331903252919SAndi Kleen char *buf = (char *)__get_free_page(GFP_KERNEL); 332003252919SAndi Kleen if (buf) { 332103252919SAndi Kleen char *p, *s; 332203252919SAndi Kleen 3323cf28b486SJan Blunck p = d_path(&f->f_path, buf, PAGE_SIZE); 332403252919SAndi Kleen if (IS_ERR(p)) 332503252919SAndi Kleen p = "?"; 332603252919SAndi Kleen s = strrchr(p, '/'); 332703252919SAndi Kleen if (s) 332803252919SAndi Kleen p = s+1; 332903252919SAndi Kleen printk("%s%s[%lx+%lx]", prefix, p, 333003252919SAndi Kleen vma->vm_start, 333103252919SAndi Kleen vma->vm_end - vma->vm_start); 333203252919SAndi Kleen free_page((unsigned long)buf); 333303252919SAndi Kleen } 333403252919SAndi Kleen } 333503252919SAndi Kleen up_read(¤t->mm->mmap_sem); 333603252919SAndi Kleen } 33373ee1afa3SNick Piggin 33383ee1afa3SNick Piggin #ifdef CONFIG_PROVE_LOCKING 33393ee1afa3SNick Piggin void might_fault(void) 33403ee1afa3SNick Piggin { 334195156f00SPeter Zijlstra /* 334295156f00SPeter Zijlstra * Some code (nfs/sunrpc) uses socket ops on kernel memory while 334395156f00SPeter Zijlstra * holding the mmap_sem, this is safe because kernel memory doesn't 334495156f00SPeter Zijlstra * get paged out, therefore we'll never actually fault, and the 334595156f00SPeter Zijlstra * below annotations will generate false positives. 334695156f00SPeter Zijlstra */ 334795156f00SPeter Zijlstra if (segment_eq(get_fs(), KERNEL_DS)) 334895156f00SPeter Zijlstra return; 334995156f00SPeter Zijlstra 33503ee1afa3SNick Piggin might_sleep(); 33513ee1afa3SNick Piggin /* 33523ee1afa3SNick Piggin * it would be nicer only to annotate paths which are not under 33533ee1afa3SNick Piggin * pagefault_disable, however that requires a larger audit and 33543ee1afa3SNick Piggin * providing helpers like get_user_atomic. 33553ee1afa3SNick Piggin */ 33563ee1afa3SNick Piggin if (!in_atomic() && current->mm) 33573ee1afa3SNick Piggin might_lock_read(¤t->mm->mmap_sem); 33583ee1afa3SNick Piggin } 33593ee1afa3SNick Piggin EXPORT_SYMBOL(might_fault); 33603ee1afa3SNick Piggin #endif 3361