11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/mm/memory.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds 51da177e4SLinus Torvalds */ 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds /* 81da177e4SLinus Torvalds * demand-loading started 01.12.91 - seems it is high on the list of 91da177e4SLinus Torvalds * things wanted, and it should be easy to implement. - Linus 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds /* 131da177e4SLinus Torvalds * Ok, demand-loading was easy, shared pages a little bit tricker. Shared 141da177e4SLinus Torvalds * pages started 02.12.91, seems to work. - Linus. 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * Tested sharing by executing about 30 /bin/sh: under the old kernel it 171da177e4SLinus Torvalds * would have taken more than the 6M I have free, but it worked well as 181da177e4SLinus Torvalds * far as I could see. 191da177e4SLinus Torvalds * 201da177e4SLinus Torvalds * Also corrected some "invalidate()"s - I wasn't doing enough of them. 211da177e4SLinus Torvalds */ 221da177e4SLinus Torvalds 231da177e4SLinus Torvalds /* 241da177e4SLinus Torvalds * Real VM (paging to/from disk) started 18.12.91. Much more work and 251da177e4SLinus Torvalds * thought has to go into this. Oh, well.. 261da177e4SLinus Torvalds * 19.12.91 - works, somewhat. Sometimes I get faults, don't know why. 271da177e4SLinus Torvalds * Found it. Everything seems to work now. 281da177e4SLinus Torvalds * 20.12.91 - Ok, making the swap-device changeable like the root. 291da177e4SLinus Torvalds */ 301da177e4SLinus Torvalds 311da177e4SLinus Torvalds /* 321da177e4SLinus Torvalds * 05.04.94 - Multi-page memory management added for v1.1. 331da177e4SLinus Torvalds * Idea by Alex Bligh (alex@cconcepts.co.uk) 341da177e4SLinus Torvalds * 351da177e4SLinus Torvalds * 16.07.99 - Support of BIGMEM added by Gerhard Wichert, Siemens AG 361da177e4SLinus Torvalds * (Gerhard.Wichert@pdb.siemens.de) 371da177e4SLinus Torvalds * 381da177e4SLinus Torvalds * Aug/Sep 2004 Changed to four level page tables (Andi Kleen) 391da177e4SLinus Torvalds */ 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds #include <linux/kernel_stat.h> 421da177e4SLinus Torvalds #include <linux/mm.h> 431da177e4SLinus Torvalds #include <linux/hugetlb.h> 441da177e4SLinus Torvalds #include <linux/mman.h> 451da177e4SLinus Torvalds #include <linux/swap.h> 461da177e4SLinus Torvalds #include <linux/highmem.h> 471da177e4SLinus Torvalds #include <linux/pagemap.h> 481da177e4SLinus Torvalds #include <linux/rmap.h> 491da177e4SLinus Torvalds #include <linux/module.h> 500ff92245SShailabh Nagar #include <linux/delayacct.h> 511da177e4SLinus Torvalds #include <linux/init.h> 52edc79b2aSPeter Zijlstra #include <linux/writeback.h> 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds #include <asm/pgalloc.h> 551da177e4SLinus Torvalds #include <asm/uaccess.h> 561da177e4SLinus Torvalds #include <asm/tlb.h> 571da177e4SLinus Torvalds #include <asm/tlbflush.h> 581da177e4SLinus Torvalds #include <asm/pgtable.h> 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds #include <linux/swapops.h> 611da177e4SLinus Torvalds #include <linux/elf.h> 621da177e4SLinus Torvalds 63d41dee36SAndy Whitcroft #ifndef CONFIG_NEED_MULTIPLE_NODES 641da177e4SLinus Torvalds /* use the per-pgdat data instead for discontigmem - mbligh */ 651da177e4SLinus Torvalds unsigned long max_mapnr; 661da177e4SLinus Torvalds struct page *mem_map; 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds EXPORT_SYMBOL(max_mapnr); 691da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map); 701da177e4SLinus Torvalds #endif 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds unsigned long num_physpages; 731da177e4SLinus Torvalds /* 741da177e4SLinus Torvalds * A number of key systems in x86 including ioremap() rely on the assumption 751da177e4SLinus Torvalds * that high_memory defines the upper bound on direct map memory, then end 761da177e4SLinus Torvalds * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and 771da177e4SLinus Torvalds * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL 781da177e4SLinus Torvalds * and ZONE_HIGHMEM. 791da177e4SLinus Torvalds */ 801da177e4SLinus Torvalds void * high_memory; 811da177e4SLinus Torvalds unsigned long vmalloc_earlyreserve; 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds EXPORT_SYMBOL(num_physpages); 841da177e4SLinus Torvalds EXPORT_SYMBOL(high_memory); 851da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc_earlyreserve); 861da177e4SLinus Torvalds 87a62eaf15SAndi Kleen int randomize_va_space __read_mostly = 1; 88a62eaf15SAndi Kleen 89a62eaf15SAndi Kleen static int __init disable_randmaps(char *s) 90a62eaf15SAndi Kleen { 91a62eaf15SAndi Kleen randomize_va_space = 0; 929b41046cSOGAWA Hirofumi return 1; 93a62eaf15SAndi Kleen } 94a62eaf15SAndi Kleen __setup("norandmaps", disable_randmaps); 95a62eaf15SAndi Kleen 96a62eaf15SAndi Kleen 971da177e4SLinus Torvalds /* 981da177e4SLinus Torvalds * If a p?d_bad entry is found while walking page tables, report 991da177e4SLinus Torvalds * the error, before resetting entry to p?d_none. Usually (but 1001da177e4SLinus Torvalds * very seldom) called out from the p?d_none_or_clear_bad macros. 1011da177e4SLinus Torvalds */ 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds void pgd_clear_bad(pgd_t *pgd) 1041da177e4SLinus Torvalds { 1051da177e4SLinus Torvalds pgd_ERROR(*pgd); 1061da177e4SLinus Torvalds pgd_clear(pgd); 1071da177e4SLinus Torvalds } 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds void pud_clear_bad(pud_t *pud) 1101da177e4SLinus Torvalds { 1111da177e4SLinus Torvalds pud_ERROR(*pud); 1121da177e4SLinus Torvalds pud_clear(pud); 1131da177e4SLinus Torvalds } 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds void pmd_clear_bad(pmd_t *pmd) 1161da177e4SLinus Torvalds { 1171da177e4SLinus Torvalds pmd_ERROR(*pmd); 1181da177e4SLinus Torvalds pmd_clear(pmd); 1191da177e4SLinus Torvalds } 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds /* 1221da177e4SLinus Torvalds * Note: this doesn't free the actual pages themselves. That 1231da177e4SLinus Torvalds * has been handled earlier when unmapping all the memory regions. 1241da177e4SLinus Torvalds */ 125e0da382cSHugh Dickins static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd) 1261da177e4SLinus Torvalds { 1271da177e4SLinus Torvalds struct page *page = pmd_page(*pmd); 1281da177e4SLinus Torvalds pmd_clear(pmd); 1294c21e2f2SHugh Dickins pte_lock_deinit(page); 130e0da382cSHugh Dickins pte_free_tlb(tlb, page); 131df849a15SChristoph Lameter dec_zone_page_state(page, NR_PAGETABLE); 1321da177e4SLinus Torvalds tlb->mm->nr_ptes--; 1331da177e4SLinus Torvalds } 1341da177e4SLinus Torvalds 135e0da382cSHugh Dickins static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud, 136e0da382cSHugh Dickins unsigned long addr, unsigned long end, 137e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 1381da177e4SLinus Torvalds { 1391da177e4SLinus Torvalds pmd_t *pmd; 1401da177e4SLinus Torvalds unsigned long next; 141e0da382cSHugh Dickins unsigned long start; 1421da177e4SLinus Torvalds 143e0da382cSHugh Dickins start = addr; 1441da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 1451da177e4SLinus Torvalds do { 1461da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 1471da177e4SLinus Torvalds if (pmd_none_or_clear_bad(pmd)) 1481da177e4SLinus Torvalds continue; 149e0da382cSHugh Dickins free_pte_range(tlb, pmd); 1501da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 1511da177e4SLinus Torvalds 152e0da382cSHugh Dickins start &= PUD_MASK; 153e0da382cSHugh Dickins if (start < floor) 154e0da382cSHugh Dickins return; 155e0da382cSHugh Dickins if (ceiling) { 156e0da382cSHugh Dickins ceiling &= PUD_MASK; 157e0da382cSHugh Dickins if (!ceiling) 158e0da382cSHugh Dickins return; 1591da177e4SLinus Torvalds } 160e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 161e0da382cSHugh Dickins return; 162e0da382cSHugh Dickins 163e0da382cSHugh Dickins pmd = pmd_offset(pud, start); 164e0da382cSHugh Dickins pud_clear(pud); 165e0da382cSHugh Dickins pmd_free_tlb(tlb, pmd); 1661da177e4SLinus Torvalds } 1671da177e4SLinus Torvalds 168e0da382cSHugh Dickins static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd, 169e0da382cSHugh Dickins unsigned long addr, unsigned long end, 170e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 1711da177e4SLinus Torvalds { 1721da177e4SLinus Torvalds pud_t *pud; 1731da177e4SLinus Torvalds unsigned long next; 174e0da382cSHugh Dickins unsigned long start; 1751da177e4SLinus Torvalds 176e0da382cSHugh Dickins start = addr; 1771da177e4SLinus Torvalds pud = pud_offset(pgd, addr); 1781da177e4SLinus Torvalds do { 1791da177e4SLinus Torvalds next = pud_addr_end(addr, end); 1801da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 1811da177e4SLinus Torvalds continue; 182e0da382cSHugh Dickins free_pmd_range(tlb, pud, addr, next, floor, ceiling); 1831da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 1841da177e4SLinus Torvalds 185e0da382cSHugh Dickins start &= PGDIR_MASK; 186e0da382cSHugh Dickins if (start < floor) 187e0da382cSHugh Dickins return; 188e0da382cSHugh Dickins if (ceiling) { 189e0da382cSHugh Dickins ceiling &= PGDIR_MASK; 190e0da382cSHugh Dickins if (!ceiling) 191e0da382cSHugh Dickins return; 1921da177e4SLinus Torvalds } 193e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 194e0da382cSHugh Dickins return; 195e0da382cSHugh Dickins 196e0da382cSHugh Dickins pud = pud_offset(pgd, start); 197e0da382cSHugh Dickins pgd_clear(pgd); 198e0da382cSHugh Dickins pud_free_tlb(tlb, pud); 1991da177e4SLinus Torvalds } 2001da177e4SLinus Torvalds 2011da177e4SLinus Torvalds /* 202e0da382cSHugh Dickins * This function frees user-level page tables of a process. 203e0da382cSHugh Dickins * 2041da177e4SLinus Torvalds * Must be called with pagetable lock held. 2051da177e4SLinus Torvalds */ 2063bf5ee95SHugh Dickins void free_pgd_range(struct mmu_gather **tlb, 207e0da382cSHugh Dickins unsigned long addr, unsigned long end, 208e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 2091da177e4SLinus Torvalds { 2101da177e4SLinus Torvalds pgd_t *pgd; 2111da177e4SLinus Torvalds unsigned long next; 212e0da382cSHugh Dickins unsigned long start; 2131da177e4SLinus Torvalds 214e0da382cSHugh Dickins /* 215e0da382cSHugh Dickins * The next few lines have given us lots of grief... 216e0da382cSHugh Dickins * 217e0da382cSHugh Dickins * Why are we testing PMD* at this top level? Because often 218e0da382cSHugh Dickins * there will be no work to do at all, and we'd prefer not to 219e0da382cSHugh Dickins * go all the way down to the bottom just to discover that. 220e0da382cSHugh Dickins * 221e0da382cSHugh Dickins * Why all these "- 1"s? Because 0 represents both the bottom 222e0da382cSHugh Dickins * of the address space and the top of it (using -1 for the 223e0da382cSHugh Dickins * top wouldn't help much: the masks would do the wrong thing). 224e0da382cSHugh Dickins * The rule is that addr 0 and floor 0 refer to the bottom of 225e0da382cSHugh Dickins * the address space, but end 0 and ceiling 0 refer to the top 226e0da382cSHugh Dickins * Comparisons need to use "end - 1" and "ceiling - 1" (though 227e0da382cSHugh Dickins * that end 0 case should be mythical). 228e0da382cSHugh Dickins * 229e0da382cSHugh Dickins * Wherever addr is brought up or ceiling brought down, we must 230e0da382cSHugh Dickins * be careful to reject "the opposite 0" before it confuses the 231e0da382cSHugh Dickins * subsequent tests. But what about where end is brought down 232e0da382cSHugh Dickins * by PMD_SIZE below? no, end can't go down to 0 there. 233e0da382cSHugh Dickins * 234e0da382cSHugh Dickins * Whereas we round start (addr) and ceiling down, by different 235e0da382cSHugh Dickins * masks at different levels, in order to test whether a table 236e0da382cSHugh Dickins * now has no other vmas using it, so can be freed, we don't 237e0da382cSHugh Dickins * bother to round floor or end up - the tests don't need that. 238e0da382cSHugh Dickins */ 239e0da382cSHugh Dickins 240e0da382cSHugh Dickins addr &= PMD_MASK; 241e0da382cSHugh Dickins if (addr < floor) { 242e0da382cSHugh Dickins addr += PMD_SIZE; 243e0da382cSHugh Dickins if (!addr) 244e0da382cSHugh Dickins return; 245e0da382cSHugh Dickins } 246e0da382cSHugh Dickins if (ceiling) { 247e0da382cSHugh Dickins ceiling &= PMD_MASK; 248e0da382cSHugh Dickins if (!ceiling) 249e0da382cSHugh Dickins return; 250e0da382cSHugh Dickins } 251e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 252e0da382cSHugh Dickins end -= PMD_SIZE; 253e0da382cSHugh Dickins if (addr > end - 1) 254e0da382cSHugh Dickins return; 255e0da382cSHugh Dickins 256e0da382cSHugh Dickins start = addr; 2573bf5ee95SHugh Dickins pgd = pgd_offset((*tlb)->mm, addr); 2581da177e4SLinus Torvalds do { 2591da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 2601da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 2611da177e4SLinus Torvalds continue; 2623bf5ee95SHugh Dickins free_pud_range(*tlb, pgd, addr, next, floor, ceiling); 2631da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 264e0da382cSHugh Dickins 2654d6ddfa9SHugh Dickins if (!(*tlb)->fullmm) 2663bf5ee95SHugh Dickins flush_tlb_pgtables((*tlb)->mm, start, end); 267e0da382cSHugh Dickins } 268e0da382cSHugh Dickins 269e0da382cSHugh Dickins void free_pgtables(struct mmu_gather **tlb, struct vm_area_struct *vma, 270e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 271e0da382cSHugh Dickins { 272e0da382cSHugh Dickins while (vma) { 273e0da382cSHugh Dickins struct vm_area_struct *next = vma->vm_next; 274e0da382cSHugh Dickins unsigned long addr = vma->vm_start; 275e0da382cSHugh Dickins 2768f4f8c16SHugh Dickins /* 2778f4f8c16SHugh Dickins * Hide vma from rmap and vmtruncate before freeing pgtables 2788f4f8c16SHugh Dickins */ 2798f4f8c16SHugh Dickins anon_vma_unlink(vma); 2808f4f8c16SHugh Dickins unlink_file_vma(vma); 2818f4f8c16SHugh Dickins 2829da61aefSDavid Gibson if (is_vm_hugetlb_page(vma)) { 2833bf5ee95SHugh Dickins hugetlb_free_pgd_range(tlb, addr, vma->vm_end, 2843bf5ee95SHugh Dickins floor, next? next->vm_start: ceiling); 2853bf5ee95SHugh Dickins } else { 2863bf5ee95SHugh Dickins /* 2873bf5ee95SHugh Dickins * Optimization: gather nearby vmas into one call down 2883bf5ee95SHugh Dickins */ 2893bf5ee95SHugh Dickins while (next && next->vm_start <= vma->vm_end + PMD_SIZE 2904866920bSDavid Gibson && !is_vm_hugetlb_page(next)) { 291e0da382cSHugh Dickins vma = next; 292e0da382cSHugh Dickins next = vma->vm_next; 2938f4f8c16SHugh Dickins anon_vma_unlink(vma); 2948f4f8c16SHugh Dickins unlink_file_vma(vma); 295e0da382cSHugh Dickins } 2963bf5ee95SHugh Dickins free_pgd_range(tlb, addr, vma->vm_end, 297e0da382cSHugh Dickins floor, next? next->vm_start: ceiling); 2983bf5ee95SHugh Dickins } 299e0da382cSHugh Dickins vma = next; 300e0da382cSHugh Dickins } 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds 3031bb3630eSHugh Dickins int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address) 3041da177e4SLinus Torvalds { 305c74df32cSHugh Dickins struct page *new = pte_alloc_one(mm, address); 3061da177e4SLinus Torvalds if (!new) 3071bb3630eSHugh Dickins return -ENOMEM; 3081bb3630eSHugh Dickins 3094c21e2f2SHugh Dickins pte_lock_init(new); 310c74df32cSHugh Dickins spin_lock(&mm->page_table_lock); 3114c21e2f2SHugh Dickins if (pmd_present(*pmd)) { /* Another has populated it */ 3124c21e2f2SHugh Dickins pte_lock_deinit(new); 3131da177e4SLinus Torvalds pte_free(new); 3144c21e2f2SHugh Dickins } else { 3151da177e4SLinus Torvalds mm->nr_ptes++; 316df849a15SChristoph Lameter inc_zone_page_state(new, NR_PAGETABLE); 3171da177e4SLinus Torvalds pmd_populate(mm, pmd, new); 3181da177e4SLinus Torvalds } 319c74df32cSHugh Dickins spin_unlock(&mm->page_table_lock); 3201bb3630eSHugh Dickins return 0; 3211da177e4SLinus Torvalds } 3221da177e4SLinus Torvalds 3231bb3630eSHugh Dickins int __pte_alloc_kernel(pmd_t *pmd, unsigned long address) 3241da177e4SLinus Torvalds { 3251bb3630eSHugh Dickins pte_t *new = pte_alloc_one_kernel(&init_mm, address); 3261da177e4SLinus Torvalds if (!new) 3271bb3630eSHugh Dickins return -ENOMEM; 3281da177e4SLinus Torvalds 329872fec16SHugh Dickins spin_lock(&init_mm.page_table_lock); 3301bb3630eSHugh Dickins if (pmd_present(*pmd)) /* Another has populated it */ 3311da177e4SLinus Torvalds pte_free_kernel(new); 332872fec16SHugh Dickins else 333872fec16SHugh Dickins pmd_populate_kernel(&init_mm, pmd, new); 334872fec16SHugh Dickins spin_unlock(&init_mm.page_table_lock); 3351bb3630eSHugh Dickins return 0; 3361da177e4SLinus Torvalds } 3371da177e4SLinus Torvalds 338ae859762SHugh Dickins static inline void add_mm_rss(struct mm_struct *mm, int file_rss, int anon_rss) 339ae859762SHugh Dickins { 340ae859762SHugh Dickins if (file_rss) 341ae859762SHugh Dickins add_mm_counter(mm, file_rss, file_rss); 342ae859762SHugh Dickins if (anon_rss) 343ae859762SHugh Dickins add_mm_counter(mm, anon_rss, anon_rss); 344ae859762SHugh Dickins } 345ae859762SHugh Dickins 3461da177e4SLinus Torvalds /* 3476aab341eSLinus Torvalds * This function is called to print an error when a bad pte 3486aab341eSLinus Torvalds * is found. For example, we might have a PFN-mapped pte in 3496aab341eSLinus Torvalds * a region that doesn't allow it. 350b5810039SNick Piggin * 351b5810039SNick Piggin * The calling function must still handle the error. 352b5810039SNick Piggin */ 353b5810039SNick Piggin void print_bad_pte(struct vm_area_struct *vma, pte_t pte, unsigned long vaddr) 354b5810039SNick Piggin { 355b5810039SNick Piggin printk(KERN_ERR "Bad pte = %08llx, process = %s, " 356b5810039SNick Piggin "vm_flags = %lx, vaddr = %lx\n", 357b5810039SNick Piggin (long long)pte_val(pte), 358b5810039SNick Piggin (vma->vm_mm == current->mm ? current->comm : "???"), 359b5810039SNick Piggin vma->vm_flags, vaddr); 360b5810039SNick Piggin dump_stack(); 361b5810039SNick Piggin } 362b5810039SNick Piggin 36367121172SLinus Torvalds static inline int is_cow_mapping(unsigned int flags) 36467121172SLinus Torvalds { 36567121172SLinus Torvalds return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; 36667121172SLinus Torvalds } 36767121172SLinus Torvalds 368b5810039SNick Piggin /* 3696aab341eSLinus Torvalds * This function gets the "struct page" associated with a pte. 3706aab341eSLinus Torvalds * 3716aab341eSLinus Torvalds * NOTE! Some mappings do not have "struct pages". A raw PFN mapping 3726aab341eSLinus Torvalds * will have each page table entry just pointing to a raw page frame 3736aab341eSLinus Torvalds * number, and as far as the VM layer is concerned, those do not have 3746aab341eSLinus Torvalds * pages associated with them - even if the PFN might point to memory 3756aab341eSLinus Torvalds * that otherwise is perfectly fine and has a "struct page". 3766aab341eSLinus Torvalds * 3776aab341eSLinus Torvalds * The way we recognize those mappings is through the rules set up 3786aab341eSLinus Torvalds * by "remap_pfn_range()": the vma will have the VM_PFNMAP bit set, 3796aab341eSLinus Torvalds * and the vm_pgoff will point to the first PFN mapped: thus every 3806aab341eSLinus Torvalds * page that is a raw mapping will always honor the rule 3816aab341eSLinus Torvalds * 3826aab341eSLinus Torvalds * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT) 3836aab341eSLinus Torvalds * 3846aab341eSLinus Torvalds * and if that isn't true, the page has been COW'ed (in which case it 3856aab341eSLinus Torvalds * _does_ have a "struct page" associated with it even if it is in a 3866aab341eSLinus Torvalds * VM_PFNMAP range). 387ee498ed7SHugh Dickins */ 3886aab341eSLinus Torvalds struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, pte_t pte) 389ee498ed7SHugh Dickins { 3906aab341eSLinus Torvalds unsigned long pfn = pte_pfn(pte); 3916aab341eSLinus Torvalds 392b7ab795bSNick Piggin if (unlikely(vma->vm_flags & VM_PFNMAP)) { 3936aab341eSLinus Torvalds unsigned long off = (addr - vma->vm_start) >> PAGE_SHIFT; 3946aab341eSLinus Torvalds if (pfn == vma->vm_pgoff + off) 3956aab341eSLinus Torvalds return NULL; 39667121172SLinus Torvalds if (!is_cow_mapping(vma->vm_flags)) 397fb155c16SLinus Torvalds return NULL; 3986aab341eSLinus Torvalds } 3996aab341eSLinus Torvalds 400315ab19aSNick Piggin /* 401315ab19aSNick Piggin * Add some anal sanity checks for now. Eventually, 402315ab19aSNick Piggin * we should just do "return pfn_to_page(pfn)", but 403315ab19aSNick Piggin * in the meantime we check that we get a valid pfn, 404315ab19aSNick Piggin * and that the resulting page looks ok. 405315ab19aSNick Piggin */ 4066aab341eSLinus Torvalds if (unlikely(!pfn_valid(pfn))) { 4076aab341eSLinus Torvalds print_bad_pte(vma, pte, addr); 4086aab341eSLinus Torvalds return NULL; 4096aab341eSLinus Torvalds } 4106aab341eSLinus Torvalds 4116aab341eSLinus Torvalds /* 4126aab341eSLinus Torvalds * NOTE! We still have PageReserved() pages in the page 4136aab341eSLinus Torvalds * tables. 4146aab341eSLinus Torvalds * 4156aab341eSLinus Torvalds * The PAGE_ZERO() pages and various VDSO mappings can 4166aab341eSLinus Torvalds * cause them to exist. 4176aab341eSLinus Torvalds */ 4186aab341eSLinus Torvalds return pfn_to_page(pfn); 419ee498ed7SHugh Dickins } 420ee498ed7SHugh Dickins 421ee498ed7SHugh Dickins /* 4221da177e4SLinus Torvalds * copy one vm_area from one task to the other. Assumes the page tables 4231da177e4SLinus Torvalds * already present in the new task to be cleared in the whole range 4241da177e4SLinus Torvalds * covered by this vma. 4251da177e4SLinus Torvalds */ 4261da177e4SLinus Torvalds 4278c103762SHugh Dickins static inline void 4281da177e4SLinus Torvalds copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, 429b5810039SNick Piggin pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma, 4308c103762SHugh Dickins unsigned long addr, int *rss) 4311da177e4SLinus Torvalds { 432b5810039SNick Piggin unsigned long vm_flags = vma->vm_flags; 4331da177e4SLinus Torvalds pte_t pte = *src_pte; 4341da177e4SLinus Torvalds struct page *page; 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds /* pte contains position in swap or file, so copy. */ 4371da177e4SLinus Torvalds if (unlikely(!pte_present(pte))) { 4381da177e4SLinus Torvalds if (!pte_file(pte)) { 4390697212aSChristoph Lameter swp_entry_t entry = pte_to_swp_entry(pte); 4400697212aSChristoph Lameter 4410697212aSChristoph Lameter swap_duplicate(entry); 4421da177e4SLinus Torvalds /* make sure dst_mm is on swapoff's mmlist. */ 4431da177e4SLinus Torvalds if (unlikely(list_empty(&dst_mm->mmlist))) { 4441da177e4SLinus Torvalds spin_lock(&mmlist_lock); 445f412ac08SHugh Dickins if (list_empty(&dst_mm->mmlist)) 446f412ac08SHugh Dickins list_add(&dst_mm->mmlist, 447f412ac08SHugh Dickins &src_mm->mmlist); 4481da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 4491da177e4SLinus Torvalds } 4500697212aSChristoph Lameter if (is_write_migration_entry(entry) && 4510697212aSChristoph Lameter is_cow_mapping(vm_flags)) { 4520697212aSChristoph Lameter /* 4530697212aSChristoph Lameter * COW mappings require pages in both parent 4540697212aSChristoph Lameter * and child to be set to read. 4550697212aSChristoph Lameter */ 4560697212aSChristoph Lameter make_migration_entry_read(&entry); 4570697212aSChristoph Lameter pte = swp_entry_to_pte(entry); 4580697212aSChristoph Lameter set_pte_at(src_mm, addr, src_pte, pte); 4590697212aSChristoph Lameter } 4601da177e4SLinus Torvalds } 461ae859762SHugh Dickins goto out_set_pte; 4621da177e4SLinus Torvalds } 4631da177e4SLinus Torvalds 4641da177e4SLinus Torvalds /* 4651da177e4SLinus Torvalds * If it's a COW mapping, write protect it both 4661da177e4SLinus Torvalds * in the parent and the child 4671da177e4SLinus Torvalds */ 46867121172SLinus Torvalds if (is_cow_mapping(vm_flags)) { 4691da177e4SLinus Torvalds ptep_set_wrprotect(src_mm, addr, src_pte); 4703dc90795SZachary Amsden pte = pte_wrprotect(pte); 4711da177e4SLinus Torvalds } 4721da177e4SLinus Torvalds 4731da177e4SLinus Torvalds /* 4741da177e4SLinus Torvalds * If it's a shared mapping, mark it clean in 4751da177e4SLinus Torvalds * the child 4761da177e4SLinus Torvalds */ 4771da177e4SLinus Torvalds if (vm_flags & VM_SHARED) 4781da177e4SLinus Torvalds pte = pte_mkclean(pte); 4791da177e4SLinus Torvalds pte = pte_mkold(pte); 4806aab341eSLinus Torvalds 4816aab341eSLinus Torvalds page = vm_normal_page(vma, addr, pte); 4826aab341eSLinus Torvalds if (page) { 4831da177e4SLinus Torvalds get_page(page); 4841da177e4SLinus Torvalds page_dup_rmap(page); 4858c103762SHugh Dickins rss[!!PageAnon(page)]++; 4866aab341eSLinus Torvalds } 487ae859762SHugh Dickins 488ae859762SHugh Dickins out_set_pte: 489ae859762SHugh Dickins set_pte_at(dst_mm, addr, dst_pte, pte); 4901da177e4SLinus Torvalds } 4911da177e4SLinus Torvalds 4921da177e4SLinus Torvalds static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 4931da177e4SLinus Torvalds pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma, 4941da177e4SLinus Torvalds unsigned long addr, unsigned long end) 4951da177e4SLinus Torvalds { 4961da177e4SLinus Torvalds pte_t *src_pte, *dst_pte; 497c74df32cSHugh Dickins spinlock_t *src_ptl, *dst_ptl; 498e040f218SHugh Dickins int progress = 0; 4998c103762SHugh Dickins int rss[2]; 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds again: 502ae859762SHugh Dickins rss[1] = rss[0] = 0; 503c74df32cSHugh Dickins dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl); 5041da177e4SLinus Torvalds if (!dst_pte) 5051da177e4SLinus Torvalds return -ENOMEM; 5061da177e4SLinus Torvalds src_pte = pte_offset_map_nested(src_pmd, addr); 5074c21e2f2SHugh Dickins src_ptl = pte_lockptr(src_mm, src_pmd); 508f20dc5f7SIngo Molnar spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 5096606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds do { 5121da177e4SLinus Torvalds /* 5131da177e4SLinus Torvalds * We are holding two locks at this point - either of them 5141da177e4SLinus Torvalds * could generate latencies in another task on another CPU. 5151da177e4SLinus Torvalds */ 516e040f218SHugh Dickins if (progress >= 32) { 517e040f218SHugh Dickins progress = 0; 518e040f218SHugh Dickins if (need_resched() || 519c74df32cSHugh Dickins need_lockbreak(src_ptl) || 520c74df32cSHugh Dickins need_lockbreak(dst_ptl)) 5211da177e4SLinus Torvalds break; 522e040f218SHugh Dickins } 5231da177e4SLinus Torvalds if (pte_none(*src_pte)) { 5241da177e4SLinus Torvalds progress++; 5251da177e4SLinus Torvalds continue; 5261da177e4SLinus Torvalds } 5278c103762SHugh Dickins copy_one_pte(dst_mm, src_mm, dst_pte, src_pte, vma, addr, rss); 5281da177e4SLinus Torvalds progress += 8; 5291da177e4SLinus Torvalds } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end); 5301da177e4SLinus Torvalds 5316606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 532c74df32cSHugh Dickins spin_unlock(src_ptl); 5331da177e4SLinus Torvalds pte_unmap_nested(src_pte - 1); 534ae859762SHugh Dickins add_mm_rss(dst_mm, rss[0], rss[1]); 535c74df32cSHugh Dickins pte_unmap_unlock(dst_pte - 1, dst_ptl); 536c74df32cSHugh Dickins cond_resched(); 5371da177e4SLinus Torvalds if (addr != end) 5381da177e4SLinus Torvalds goto again; 5391da177e4SLinus Torvalds return 0; 5401da177e4SLinus Torvalds } 5411da177e4SLinus Torvalds 5421da177e4SLinus Torvalds static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 5431da177e4SLinus Torvalds pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma, 5441da177e4SLinus Torvalds unsigned long addr, unsigned long end) 5451da177e4SLinus Torvalds { 5461da177e4SLinus Torvalds pmd_t *src_pmd, *dst_pmd; 5471da177e4SLinus Torvalds unsigned long next; 5481da177e4SLinus Torvalds 5491da177e4SLinus Torvalds dst_pmd = pmd_alloc(dst_mm, dst_pud, addr); 5501da177e4SLinus Torvalds if (!dst_pmd) 5511da177e4SLinus Torvalds return -ENOMEM; 5521da177e4SLinus Torvalds src_pmd = pmd_offset(src_pud, addr); 5531da177e4SLinus Torvalds do { 5541da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 5551da177e4SLinus Torvalds if (pmd_none_or_clear_bad(src_pmd)) 5561da177e4SLinus Torvalds continue; 5571da177e4SLinus Torvalds if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd, 5581da177e4SLinus Torvalds vma, addr, next)) 5591da177e4SLinus Torvalds return -ENOMEM; 5601da177e4SLinus Torvalds } while (dst_pmd++, src_pmd++, addr = next, addr != end); 5611da177e4SLinus Torvalds return 0; 5621da177e4SLinus Torvalds } 5631da177e4SLinus Torvalds 5641da177e4SLinus Torvalds static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 5651da177e4SLinus Torvalds pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma, 5661da177e4SLinus Torvalds unsigned long addr, unsigned long end) 5671da177e4SLinus Torvalds { 5681da177e4SLinus Torvalds pud_t *src_pud, *dst_pud; 5691da177e4SLinus Torvalds unsigned long next; 5701da177e4SLinus Torvalds 5711da177e4SLinus Torvalds dst_pud = pud_alloc(dst_mm, dst_pgd, addr); 5721da177e4SLinus Torvalds if (!dst_pud) 5731da177e4SLinus Torvalds return -ENOMEM; 5741da177e4SLinus Torvalds src_pud = pud_offset(src_pgd, addr); 5751da177e4SLinus Torvalds do { 5761da177e4SLinus Torvalds next = pud_addr_end(addr, end); 5771da177e4SLinus Torvalds if (pud_none_or_clear_bad(src_pud)) 5781da177e4SLinus Torvalds continue; 5791da177e4SLinus Torvalds if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud, 5801da177e4SLinus Torvalds vma, addr, next)) 5811da177e4SLinus Torvalds return -ENOMEM; 5821da177e4SLinus Torvalds } while (dst_pud++, src_pud++, addr = next, addr != end); 5831da177e4SLinus Torvalds return 0; 5841da177e4SLinus Torvalds } 5851da177e4SLinus Torvalds 5861da177e4SLinus Torvalds int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 5871da177e4SLinus Torvalds struct vm_area_struct *vma) 5881da177e4SLinus Torvalds { 5891da177e4SLinus Torvalds pgd_t *src_pgd, *dst_pgd; 5901da177e4SLinus Torvalds unsigned long next; 5911da177e4SLinus Torvalds unsigned long addr = vma->vm_start; 5921da177e4SLinus Torvalds unsigned long end = vma->vm_end; 5931da177e4SLinus Torvalds 594d992895bSNick Piggin /* 595d992895bSNick Piggin * Don't copy ptes where a page fault will fill them correctly. 596d992895bSNick Piggin * Fork becomes much lighter when there are big shared or private 597d992895bSNick Piggin * readonly mappings. The tradeoff is that copy_page_range is more 598d992895bSNick Piggin * efficient than faulting. 599d992895bSNick Piggin */ 6004d7672b4SLinus Torvalds if (!(vma->vm_flags & (VM_HUGETLB|VM_NONLINEAR|VM_PFNMAP|VM_INSERTPAGE))) { 601d992895bSNick Piggin if (!vma->anon_vma) 602d992895bSNick Piggin return 0; 603d992895bSNick Piggin } 604d992895bSNick Piggin 6051da177e4SLinus Torvalds if (is_vm_hugetlb_page(vma)) 6061da177e4SLinus Torvalds return copy_hugetlb_page_range(dst_mm, src_mm, vma); 6071da177e4SLinus Torvalds 6081da177e4SLinus Torvalds dst_pgd = pgd_offset(dst_mm, addr); 6091da177e4SLinus Torvalds src_pgd = pgd_offset(src_mm, addr); 6101da177e4SLinus Torvalds do { 6111da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 6121da177e4SLinus Torvalds if (pgd_none_or_clear_bad(src_pgd)) 6131da177e4SLinus Torvalds continue; 6141da177e4SLinus Torvalds if (copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd, 6151da177e4SLinus Torvalds vma, addr, next)) 6161da177e4SLinus Torvalds return -ENOMEM; 6171da177e4SLinus Torvalds } while (dst_pgd++, src_pgd++, addr = next, addr != end); 6181da177e4SLinus Torvalds return 0; 6191da177e4SLinus Torvalds } 6201da177e4SLinus Torvalds 62151c6f666SRobin Holt static unsigned long zap_pte_range(struct mmu_gather *tlb, 622b5810039SNick Piggin struct vm_area_struct *vma, pmd_t *pmd, 6231da177e4SLinus Torvalds unsigned long addr, unsigned long end, 62451c6f666SRobin Holt long *zap_work, struct zap_details *details) 6251da177e4SLinus Torvalds { 626b5810039SNick Piggin struct mm_struct *mm = tlb->mm; 6271da177e4SLinus Torvalds pte_t *pte; 628508034a3SHugh Dickins spinlock_t *ptl; 629ae859762SHugh Dickins int file_rss = 0; 630ae859762SHugh Dickins int anon_rss = 0; 6311da177e4SLinus Torvalds 632508034a3SHugh Dickins pte = pte_offset_map_lock(mm, pmd, addr, &ptl); 6336606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 6341da177e4SLinus Torvalds do { 6351da177e4SLinus Torvalds pte_t ptent = *pte; 63651c6f666SRobin Holt if (pte_none(ptent)) { 63751c6f666SRobin Holt (*zap_work)--; 6381da177e4SLinus Torvalds continue; 63951c6f666SRobin Holt } 64051c6f666SRobin Holt 64151c6f666SRobin Holt (*zap_work) -= PAGE_SIZE; 64251c6f666SRobin Holt 6436f5e6b9eSHugh Dickins if (pte_present(ptent)) { 6446f5e6b9eSHugh Dickins struct page *page; 6456f5e6b9eSHugh Dickins 6466aab341eSLinus Torvalds page = vm_normal_page(vma, addr, ptent); 6471da177e4SLinus Torvalds if (unlikely(details) && page) { 6481da177e4SLinus Torvalds /* 6491da177e4SLinus Torvalds * unmap_shared_mapping_pages() wants to 6501da177e4SLinus Torvalds * invalidate cache without truncating: 6511da177e4SLinus Torvalds * unmap shared but keep private pages. 6521da177e4SLinus Torvalds */ 6531da177e4SLinus Torvalds if (details->check_mapping && 6541da177e4SLinus Torvalds details->check_mapping != page->mapping) 6551da177e4SLinus Torvalds continue; 6561da177e4SLinus Torvalds /* 6571da177e4SLinus Torvalds * Each page->index must be checked when 6581da177e4SLinus Torvalds * invalidating or truncating nonlinear. 6591da177e4SLinus Torvalds */ 6601da177e4SLinus Torvalds if (details->nonlinear_vma && 6611da177e4SLinus Torvalds (page->index < details->first_index || 6621da177e4SLinus Torvalds page->index > details->last_index)) 6631da177e4SLinus Torvalds continue; 6641da177e4SLinus Torvalds } 665b5810039SNick Piggin ptent = ptep_get_and_clear_full(mm, addr, pte, 666a600388dSZachary Amsden tlb->fullmm); 6671da177e4SLinus Torvalds tlb_remove_tlb_entry(tlb, pte, addr); 6681da177e4SLinus Torvalds if (unlikely(!page)) 6691da177e4SLinus Torvalds continue; 6701da177e4SLinus Torvalds if (unlikely(details) && details->nonlinear_vma 6711da177e4SLinus Torvalds && linear_page_index(details->nonlinear_vma, 6721da177e4SLinus Torvalds addr) != page->index) 673b5810039SNick Piggin set_pte_at(mm, addr, pte, 6741da177e4SLinus Torvalds pgoff_to_pte(page->index)); 6751da177e4SLinus Torvalds if (PageAnon(page)) 67686d912f4SHugh Dickins anon_rss--; 6776237bcd9SHugh Dickins else { 6786237bcd9SHugh Dickins if (pte_dirty(ptent)) 6796237bcd9SHugh Dickins set_page_dirty(page); 6806237bcd9SHugh Dickins if (pte_young(ptent)) 6811da177e4SLinus Torvalds mark_page_accessed(page); 68286d912f4SHugh Dickins file_rss--; 6836237bcd9SHugh Dickins } 6841da177e4SLinus Torvalds page_remove_rmap(page); 6851da177e4SLinus Torvalds tlb_remove_page(tlb, page); 6861da177e4SLinus Torvalds continue; 6871da177e4SLinus Torvalds } 6881da177e4SLinus Torvalds /* 6891da177e4SLinus Torvalds * If details->check_mapping, we leave swap entries; 6901da177e4SLinus Torvalds * if details->nonlinear_vma, we leave file entries. 6911da177e4SLinus Torvalds */ 6921da177e4SLinus Torvalds if (unlikely(details)) 6931da177e4SLinus Torvalds continue; 6941da177e4SLinus Torvalds if (!pte_file(ptent)) 6951da177e4SLinus Torvalds free_swap_and_cache(pte_to_swp_entry(ptent)); 6969888a1caSZachary Amsden pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); 69751c6f666SRobin Holt } while (pte++, addr += PAGE_SIZE, (addr != end && *zap_work > 0)); 698ae859762SHugh Dickins 69986d912f4SHugh Dickins add_mm_rss(mm, file_rss, anon_rss); 7006606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 701508034a3SHugh Dickins pte_unmap_unlock(pte - 1, ptl); 70251c6f666SRobin Holt 70351c6f666SRobin Holt return addr; 7041da177e4SLinus Torvalds } 7051da177e4SLinus Torvalds 70651c6f666SRobin Holt static inline unsigned long zap_pmd_range(struct mmu_gather *tlb, 707b5810039SNick Piggin struct vm_area_struct *vma, pud_t *pud, 7081da177e4SLinus Torvalds unsigned long addr, unsigned long end, 70951c6f666SRobin Holt long *zap_work, struct zap_details *details) 7101da177e4SLinus Torvalds { 7111da177e4SLinus Torvalds pmd_t *pmd; 7121da177e4SLinus Torvalds unsigned long next; 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 7151da177e4SLinus Torvalds do { 7161da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 71751c6f666SRobin Holt if (pmd_none_or_clear_bad(pmd)) { 71851c6f666SRobin Holt (*zap_work)--; 7191da177e4SLinus Torvalds continue; 72051c6f666SRobin Holt } 72151c6f666SRobin Holt next = zap_pte_range(tlb, vma, pmd, addr, next, 72251c6f666SRobin Holt zap_work, details); 72351c6f666SRobin Holt } while (pmd++, addr = next, (addr != end && *zap_work > 0)); 72451c6f666SRobin Holt 72551c6f666SRobin Holt return addr; 7261da177e4SLinus Torvalds } 7271da177e4SLinus Torvalds 72851c6f666SRobin Holt static inline unsigned long zap_pud_range(struct mmu_gather *tlb, 729b5810039SNick Piggin struct vm_area_struct *vma, pgd_t *pgd, 7301da177e4SLinus Torvalds unsigned long addr, unsigned long end, 73151c6f666SRobin Holt long *zap_work, struct zap_details *details) 7321da177e4SLinus Torvalds { 7331da177e4SLinus Torvalds pud_t *pud; 7341da177e4SLinus Torvalds unsigned long next; 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds pud = pud_offset(pgd, addr); 7371da177e4SLinus Torvalds do { 7381da177e4SLinus Torvalds next = pud_addr_end(addr, end); 73951c6f666SRobin Holt if (pud_none_or_clear_bad(pud)) { 74051c6f666SRobin Holt (*zap_work)--; 7411da177e4SLinus Torvalds continue; 74251c6f666SRobin Holt } 74351c6f666SRobin Holt next = zap_pmd_range(tlb, vma, pud, addr, next, 74451c6f666SRobin Holt zap_work, details); 74551c6f666SRobin Holt } while (pud++, addr = next, (addr != end && *zap_work > 0)); 74651c6f666SRobin Holt 74751c6f666SRobin Holt return addr; 7481da177e4SLinus Torvalds } 7491da177e4SLinus Torvalds 75051c6f666SRobin Holt static unsigned long unmap_page_range(struct mmu_gather *tlb, 75151c6f666SRobin Holt struct vm_area_struct *vma, 7521da177e4SLinus Torvalds unsigned long addr, unsigned long end, 75351c6f666SRobin Holt long *zap_work, struct zap_details *details) 7541da177e4SLinus Torvalds { 7551da177e4SLinus Torvalds pgd_t *pgd; 7561da177e4SLinus Torvalds unsigned long next; 7571da177e4SLinus Torvalds 7581da177e4SLinus Torvalds if (details && !details->check_mapping && !details->nonlinear_vma) 7591da177e4SLinus Torvalds details = NULL; 7601da177e4SLinus Torvalds 7611da177e4SLinus Torvalds BUG_ON(addr >= end); 7621da177e4SLinus Torvalds tlb_start_vma(tlb, vma); 7631da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 7641da177e4SLinus Torvalds do { 7651da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 76651c6f666SRobin Holt if (pgd_none_or_clear_bad(pgd)) { 76751c6f666SRobin Holt (*zap_work)--; 7681da177e4SLinus Torvalds continue; 76951c6f666SRobin Holt } 77051c6f666SRobin Holt next = zap_pud_range(tlb, vma, pgd, addr, next, 77151c6f666SRobin Holt zap_work, details); 77251c6f666SRobin Holt } while (pgd++, addr = next, (addr != end && *zap_work > 0)); 7731da177e4SLinus Torvalds tlb_end_vma(tlb, vma); 77451c6f666SRobin Holt 77551c6f666SRobin Holt return addr; 7761da177e4SLinus Torvalds } 7771da177e4SLinus Torvalds 7781da177e4SLinus Torvalds #ifdef CONFIG_PREEMPT 7791da177e4SLinus Torvalds # define ZAP_BLOCK_SIZE (8 * PAGE_SIZE) 7801da177e4SLinus Torvalds #else 7811da177e4SLinus Torvalds /* No preempt: go for improved straight-line efficiency */ 7821da177e4SLinus Torvalds # define ZAP_BLOCK_SIZE (1024 * PAGE_SIZE) 7831da177e4SLinus Torvalds #endif 7841da177e4SLinus Torvalds 7851da177e4SLinus Torvalds /** 7861da177e4SLinus Torvalds * unmap_vmas - unmap a range of memory covered by a list of vma's 7871da177e4SLinus Torvalds * @tlbp: address of the caller's struct mmu_gather 7881da177e4SLinus Torvalds * @vma: the starting vma 7891da177e4SLinus Torvalds * @start_addr: virtual address at which to start unmapping 7901da177e4SLinus Torvalds * @end_addr: virtual address at which to end unmapping 7911da177e4SLinus Torvalds * @nr_accounted: Place number of unmapped pages in vm-accountable vma's here 7921da177e4SLinus Torvalds * @details: details of nonlinear truncation or shared cache invalidation 7931da177e4SLinus Torvalds * 794ee39b37bSHugh Dickins * Returns the end address of the unmapping (restart addr if interrupted). 7951da177e4SLinus Torvalds * 796508034a3SHugh Dickins * Unmap all pages in the vma list. 7971da177e4SLinus Torvalds * 798508034a3SHugh Dickins * We aim to not hold locks for too long (for scheduling latency reasons). 799508034a3SHugh Dickins * So zap pages in ZAP_BLOCK_SIZE bytecounts. This means we need to 8001da177e4SLinus Torvalds * return the ending mmu_gather to the caller. 8011da177e4SLinus Torvalds * 8021da177e4SLinus Torvalds * Only addresses between `start' and `end' will be unmapped. 8031da177e4SLinus Torvalds * 8041da177e4SLinus Torvalds * The VMA list must be sorted in ascending virtual address order. 8051da177e4SLinus Torvalds * 8061da177e4SLinus Torvalds * unmap_vmas() assumes that the caller will flush the whole unmapped address 8071da177e4SLinus Torvalds * range after unmap_vmas() returns. So the only responsibility here is to 8081da177e4SLinus Torvalds * ensure that any thus-far unmapped pages are flushed before unmap_vmas() 8091da177e4SLinus Torvalds * drops the lock and schedules. 8101da177e4SLinus Torvalds */ 811508034a3SHugh Dickins unsigned long unmap_vmas(struct mmu_gather **tlbp, 8121da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long start_addr, 8131da177e4SLinus Torvalds unsigned long end_addr, unsigned long *nr_accounted, 8141da177e4SLinus Torvalds struct zap_details *details) 8151da177e4SLinus Torvalds { 81651c6f666SRobin Holt long zap_work = ZAP_BLOCK_SIZE; 8171da177e4SLinus Torvalds unsigned long tlb_start = 0; /* For tlb_finish_mmu */ 8181da177e4SLinus Torvalds int tlb_start_valid = 0; 819ee39b37bSHugh Dickins unsigned long start = start_addr; 8201da177e4SLinus Torvalds spinlock_t *i_mmap_lock = details? details->i_mmap_lock: NULL; 8214d6ddfa9SHugh Dickins int fullmm = (*tlbp)->fullmm; 8221da177e4SLinus Torvalds 8231da177e4SLinus Torvalds for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) { 8241da177e4SLinus Torvalds unsigned long end; 8251da177e4SLinus Torvalds 8261da177e4SLinus Torvalds start = max(vma->vm_start, start_addr); 8271da177e4SLinus Torvalds if (start >= vma->vm_end) 8281da177e4SLinus Torvalds continue; 8291da177e4SLinus Torvalds end = min(vma->vm_end, end_addr); 8301da177e4SLinus Torvalds if (end <= vma->vm_start) 8311da177e4SLinus Torvalds continue; 8321da177e4SLinus Torvalds 8331da177e4SLinus Torvalds if (vma->vm_flags & VM_ACCOUNT) 8341da177e4SLinus Torvalds *nr_accounted += (end - start) >> PAGE_SHIFT; 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds while (start != end) { 8371da177e4SLinus Torvalds if (!tlb_start_valid) { 8381da177e4SLinus Torvalds tlb_start = start; 8391da177e4SLinus Torvalds tlb_start_valid = 1; 8401da177e4SLinus Torvalds } 8411da177e4SLinus Torvalds 84251c6f666SRobin Holt if (unlikely(is_vm_hugetlb_page(vma))) { 8431da177e4SLinus Torvalds unmap_hugepage_range(vma, start, end); 84451c6f666SRobin Holt zap_work -= (end - start) / 84551c6f666SRobin Holt (HPAGE_SIZE / PAGE_SIZE); 84651c6f666SRobin Holt start = end; 84751c6f666SRobin Holt } else 84851c6f666SRobin Holt start = unmap_page_range(*tlbp, vma, 84951c6f666SRobin Holt start, end, &zap_work, details); 8501da177e4SLinus Torvalds 85151c6f666SRobin Holt if (zap_work > 0) { 85251c6f666SRobin Holt BUG_ON(start != end); 85351c6f666SRobin Holt break; 85451c6f666SRobin Holt } 8551da177e4SLinus Torvalds 8561da177e4SLinus Torvalds tlb_finish_mmu(*tlbp, tlb_start, start); 8571da177e4SLinus Torvalds 8581da177e4SLinus Torvalds if (need_resched() || 8591da177e4SLinus Torvalds (i_mmap_lock && need_lockbreak(i_mmap_lock))) { 8601da177e4SLinus Torvalds if (i_mmap_lock) { 861508034a3SHugh Dickins *tlbp = NULL; 8621da177e4SLinus Torvalds goto out; 8631da177e4SLinus Torvalds } 8641da177e4SLinus Torvalds cond_resched(); 8651da177e4SLinus Torvalds } 8661da177e4SLinus Torvalds 867508034a3SHugh Dickins *tlbp = tlb_gather_mmu(vma->vm_mm, fullmm); 8681da177e4SLinus Torvalds tlb_start_valid = 0; 86951c6f666SRobin Holt zap_work = ZAP_BLOCK_SIZE; 8701da177e4SLinus Torvalds } 8711da177e4SLinus Torvalds } 8721da177e4SLinus Torvalds out: 873ee39b37bSHugh Dickins return start; /* which is now the end (or restart) address */ 8741da177e4SLinus Torvalds } 8751da177e4SLinus Torvalds 8761da177e4SLinus Torvalds /** 8771da177e4SLinus Torvalds * zap_page_range - remove user pages in a given range 8781da177e4SLinus Torvalds * @vma: vm_area_struct holding the applicable pages 8791da177e4SLinus Torvalds * @address: starting address of pages to zap 8801da177e4SLinus Torvalds * @size: number of bytes to zap 8811da177e4SLinus Torvalds * @details: details of nonlinear truncation or shared cache invalidation 8821da177e4SLinus Torvalds */ 883ee39b37bSHugh Dickins unsigned long zap_page_range(struct vm_area_struct *vma, unsigned long address, 8841da177e4SLinus Torvalds unsigned long size, struct zap_details *details) 8851da177e4SLinus Torvalds { 8861da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 8871da177e4SLinus Torvalds struct mmu_gather *tlb; 8881da177e4SLinus Torvalds unsigned long end = address + size; 8891da177e4SLinus Torvalds unsigned long nr_accounted = 0; 8901da177e4SLinus Torvalds 8911da177e4SLinus Torvalds lru_add_drain(); 8921da177e4SLinus Torvalds tlb = tlb_gather_mmu(mm, 0); 893365e9c87SHugh Dickins update_hiwater_rss(mm); 894508034a3SHugh Dickins end = unmap_vmas(&tlb, vma, address, end, &nr_accounted, details); 895508034a3SHugh Dickins if (tlb) 8968f4f8c16SHugh Dickins tlb_finish_mmu(tlb, address, end); 897ee39b37bSHugh Dickins return end; 8981da177e4SLinus Torvalds } 8991da177e4SLinus Torvalds 9001da177e4SLinus Torvalds /* 9011da177e4SLinus Torvalds * Do a quick page-table lookup for a single page. 9021da177e4SLinus Torvalds */ 9036aab341eSLinus Torvalds struct page *follow_page(struct vm_area_struct *vma, unsigned long address, 904deceb6cdSHugh Dickins unsigned int flags) 9051da177e4SLinus Torvalds { 9061da177e4SLinus Torvalds pgd_t *pgd; 9071da177e4SLinus Torvalds pud_t *pud; 9081da177e4SLinus Torvalds pmd_t *pmd; 9091da177e4SLinus Torvalds pte_t *ptep, pte; 910deceb6cdSHugh Dickins spinlock_t *ptl; 9111da177e4SLinus Torvalds struct page *page; 9126aab341eSLinus Torvalds struct mm_struct *mm = vma->vm_mm; 9131da177e4SLinus Torvalds 914deceb6cdSHugh Dickins page = follow_huge_addr(mm, address, flags & FOLL_WRITE); 915deceb6cdSHugh Dickins if (!IS_ERR(page)) { 916deceb6cdSHugh Dickins BUG_ON(flags & FOLL_GET); 917deceb6cdSHugh Dickins goto out; 918deceb6cdSHugh Dickins } 9191da177e4SLinus Torvalds 920deceb6cdSHugh Dickins page = NULL; 9211da177e4SLinus Torvalds pgd = pgd_offset(mm, address); 9221da177e4SLinus Torvalds if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) 923deceb6cdSHugh Dickins goto no_page_table; 9241da177e4SLinus Torvalds 9251da177e4SLinus Torvalds pud = pud_offset(pgd, address); 9261da177e4SLinus Torvalds if (pud_none(*pud) || unlikely(pud_bad(*pud))) 927deceb6cdSHugh Dickins goto no_page_table; 9281da177e4SLinus Torvalds 9291da177e4SLinus Torvalds pmd = pmd_offset(pud, address); 9301da177e4SLinus Torvalds if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd))) 931deceb6cdSHugh Dickins goto no_page_table; 9321da177e4SLinus Torvalds 933deceb6cdSHugh Dickins if (pmd_huge(*pmd)) { 934deceb6cdSHugh Dickins BUG_ON(flags & FOLL_GET); 935deceb6cdSHugh Dickins page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE); 936deceb6cdSHugh Dickins goto out; 937deceb6cdSHugh Dickins } 938deceb6cdSHugh Dickins 939deceb6cdSHugh Dickins ptep = pte_offset_map_lock(mm, pmd, address, &ptl); 9401da177e4SLinus Torvalds if (!ptep) 9411da177e4SLinus Torvalds goto out; 9421da177e4SLinus Torvalds 9431da177e4SLinus Torvalds pte = *ptep; 944deceb6cdSHugh Dickins if (!pte_present(pte)) 945deceb6cdSHugh Dickins goto unlock; 946deceb6cdSHugh Dickins if ((flags & FOLL_WRITE) && !pte_write(pte)) 947deceb6cdSHugh Dickins goto unlock; 9486aab341eSLinus Torvalds page = vm_normal_page(vma, address, pte); 9496aab341eSLinus Torvalds if (unlikely(!page)) 950deceb6cdSHugh Dickins goto unlock; 951deceb6cdSHugh Dickins 952deceb6cdSHugh Dickins if (flags & FOLL_GET) 953deceb6cdSHugh Dickins get_page(page); 954deceb6cdSHugh Dickins if (flags & FOLL_TOUCH) { 955deceb6cdSHugh Dickins if ((flags & FOLL_WRITE) && 956deceb6cdSHugh Dickins !pte_dirty(pte) && !PageDirty(page)) 957f33ea7f4SNick Piggin set_page_dirty(page); 9581da177e4SLinus Torvalds mark_page_accessed(page); 9591da177e4SLinus Torvalds } 960deceb6cdSHugh Dickins unlock: 961deceb6cdSHugh Dickins pte_unmap_unlock(ptep, ptl); 9621da177e4SLinus Torvalds out: 963deceb6cdSHugh Dickins return page; 964deceb6cdSHugh Dickins 965deceb6cdSHugh Dickins no_page_table: 966deceb6cdSHugh Dickins /* 967deceb6cdSHugh Dickins * When core dumping an enormous anonymous area that nobody 968deceb6cdSHugh Dickins * has touched so far, we don't want to allocate page tables. 969deceb6cdSHugh Dickins */ 970deceb6cdSHugh Dickins if (flags & FOLL_ANON) { 971deceb6cdSHugh Dickins page = ZERO_PAGE(address); 972deceb6cdSHugh Dickins if (flags & FOLL_GET) 973deceb6cdSHugh Dickins get_page(page); 974deceb6cdSHugh Dickins BUG_ON(flags & FOLL_WRITE); 9751da177e4SLinus Torvalds } 976deceb6cdSHugh Dickins return page; 9771da177e4SLinus Torvalds } 9781da177e4SLinus Torvalds 9791da177e4SLinus Torvalds int get_user_pages(struct task_struct *tsk, struct mm_struct *mm, 9801da177e4SLinus Torvalds unsigned long start, int len, int write, int force, 9811da177e4SLinus Torvalds struct page **pages, struct vm_area_struct **vmas) 9821da177e4SLinus Torvalds { 9831da177e4SLinus Torvalds int i; 984deceb6cdSHugh Dickins unsigned int vm_flags; 9851da177e4SLinus Torvalds 9861da177e4SLinus Torvalds /* 9871da177e4SLinus Torvalds * Require read or write permissions. 9881da177e4SLinus Torvalds * If 'force' is set, we only require the "MAY" flags. 9891da177e4SLinus Torvalds */ 990deceb6cdSHugh Dickins vm_flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD); 991deceb6cdSHugh Dickins vm_flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE); 9921da177e4SLinus Torvalds i = 0; 9931da177e4SLinus Torvalds 9941da177e4SLinus Torvalds do { 9951da177e4SLinus Torvalds struct vm_area_struct *vma; 996deceb6cdSHugh Dickins unsigned int foll_flags; 9971da177e4SLinus Torvalds 9981da177e4SLinus Torvalds vma = find_extend_vma(mm, start); 9991da177e4SLinus Torvalds if (!vma && in_gate_area(tsk, start)) { 10001da177e4SLinus Torvalds unsigned long pg = start & PAGE_MASK; 10011da177e4SLinus Torvalds struct vm_area_struct *gate_vma = get_gate_vma(tsk); 10021da177e4SLinus Torvalds pgd_t *pgd; 10031da177e4SLinus Torvalds pud_t *pud; 10041da177e4SLinus Torvalds pmd_t *pmd; 10051da177e4SLinus Torvalds pte_t *pte; 10061da177e4SLinus Torvalds if (write) /* user gate pages are read-only */ 10071da177e4SLinus Torvalds return i ? : -EFAULT; 10081da177e4SLinus Torvalds if (pg > TASK_SIZE) 10091da177e4SLinus Torvalds pgd = pgd_offset_k(pg); 10101da177e4SLinus Torvalds else 10111da177e4SLinus Torvalds pgd = pgd_offset_gate(mm, pg); 10121da177e4SLinus Torvalds BUG_ON(pgd_none(*pgd)); 10131da177e4SLinus Torvalds pud = pud_offset(pgd, pg); 10141da177e4SLinus Torvalds BUG_ON(pud_none(*pud)); 10151da177e4SLinus Torvalds pmd = pmd_offset(pud, pg); 1016690dbe1cSHugh Dickins if (pmd_none(*pmd)) 1017690dbe1cSHugh Dickins return i ? : -EFAULT; 10181da177e4SLinus Torvalds pte = pte_offset_map(pmd, pg); 1019690dbe1cSHugh Dickins if (pte_none(*pte)) { 1020690dbe1cSHugh Dickins pte_unmap(pte); 1021690dbe1cSHugh Dickins return i ? : -EFAULT; 1022690dbe1cSHugh Dickins } 10231da177e4SLinus Torvalds if (pages) { 1024fa2a455bSNick Piggin struct page *page = vm_normal_page(gate_vma, start, *pte); 10256aab341eSLinus Torvalds pages[i] = page; 10266aab341eSLinus Torvalds if (page) 10276aab341eSLinus Torvalds get_page(page); 10281da177e4SLinus Torvalds } 10291da177e4SLinus Torvalds pte_unmap(pte); 10301da177e4SLinus Torvalds if (vmas) 10311da177e4SLinus Torvalds vmas[i] = gate_vma; 10321da177e4SLinus Torvalds i++; 10331da177e4SLinus Torvalds start += PAGE_SIZE; 10341da177e4SLinus Torvalds len--; 10351da177e4SLinus Torvalds continue; 10361da177e4SLinus Torvalds } 10371da177e4SLinus Torvalds 10381ff80389SLinus Torvalds if (!vma || (vma->vm_flags & (VM_IO | VM_PFNMAP)) 1039deceb6cdSHugh Dickins || !(vm_flags & vma->vm_flags)) 10401da177e4SLinus Torvalds return i ? : -EFAULT; 10411da177e4SLinus Torvalds 10421da177e4SLinus Torvalds if (is_vm_hugetlb_page(vma)) { 10431da177e4SLinus Torvalds i = follow_hugetlb_page(mm, vma, pages, vmas, 10441da177e4SLinus Torvalds &start, &len, i); 10451da177e4SLinus Torvalds continue; 10461da177e4SLinus Torvalds } 1047deceb6cdSHugh Dickins 1048deceb6cdSHugh Dickins foll_flags = FOLL_TOUCH; 1049deceb6cdSHugh Dickins if (pages) 1050deceb6cdSHugh Dickins foll_flags |= FOLL_GET; 1051deceb6cdSHugh Dickins if (!write && !(vma->vm_flags & VM_LOCKED) && 1052deceb6cdSHugh Dickins (!vma->vm_ops || !vma->vm_ops->nopage)) 1053deceb6cdSHugh Dickins foll_flags |= FOLL_ANON; 1054deceb6cdSHugh Dickins 10551da177e4SLinus Torvalds do { 105608ef4729SHugh Dickins struct page *page; 10571da177e4SLinus Torvalds 1058deceb6cdSHugh Dickins if (write) 1059deceb6cdSHugh Dickins foll_flags |= FOLL_WRITE; 1060deceb6cdSHugh Dickins 1061deceb6cdSHugh Dickins cond_resched(); 10626aab341eSLinus Torvalds while (!(page = follow_page(vma, start, foll_flags))) { 1063a68d2ebcSLinus Torvalds int ret; 1064deceb6cdSHugh Dickins ret = __handle_mm_fault(mm, vma, start, 1065deceb6cdSHugh Dickins foll_flags & FOLL_WRITE); 1066f33ea7f4SNick Piggin /* 1067a68d2ebcSLinus Torvalds * The VM_FAULT_WRITE bit tells us that do_wp_page has 1068a68d2ebcSLinus Torvalds * broken COW when necessary, even if maybe_mkwrite 1069a68d2ebcSLinus Torvalds * decided not to set pte_write. We can thus safely do 1070a68d2ebcSLinus Torvalds * subsequent page lookups as if they were reads. 1071f33ea7f4SNick Piggin */ 1072a68d2ebcSLinus Torvalds if (ret & VM_FAULT_WRITE) 1073deceb6cdSHugh Dickins foll_flags &= ~FOLL_WRITE; 1074a68d2ebcSLinus Torvalds 1075a68d2ebcSLinus Torvalds switch (ret & ~VM_FAULT_WRITE) { 10761da177e4SLinus Torvalds case VM_FAULT_MINOR: 10771da177e4SLinus Torvalds tsk->min_flt++; 10781da177e4SLinus Torvalds break; 10791da177e4SLinus Torvalds case VM_FAULT_MAJOR: 10801da177e4SLinus Torvalds tsk->maj_flt++; 10811da177e4SLinus Torvalds break; 10821da177e4SLinus Torvalds case VM_FAULT_SIGBUS: 10831da177e4SLinus Torvalds return i ? i : -EFAULT; 10841da177e4SLinus Torvalds case VM_FAULT_OOM: 10851da177e4SLinus Torvalds return i ? i : -ENOMEM; 10861da177e4SLinus Torvalds default: 10871da177e4SLinus Torvalds BUG(); 10881da177e4SLinus Torvalds } 10897f7bbbe5SBenjamin Herrenschmidt cond_resched(); 10901da177e4SLinus Torvalds } 10911da177e4SLinus Torvalds if (pages) { 109208ef4729SHugh Dickins pages[i] = page; 109303beb076SJames Bottomley 109403beb076SJames Bottomley flush_anon_page(page, start); 109508ef4729SHugh Dickins flush_dcache_page(page); 10961da177e4SLinus Torvalds } 10971da177e4SLinus Torvalds if (vmas) 10981da177e4SLinus Torvalds vmas[i] = vma; 10991da177e4SLinus Torvalds i++; 11001da177e4SLinus Torvalds start += PAGE_SIZE; 11011da177e4SLinus Torvalds len--; 11021da177e4SLinus Torvalds } while (len && start < vma->vm_end); 11031da177e4SLinus Torvalds } while (len); 11041da177e4SLinus Torvalds return i; 11051da177e4SLinus Torvalds } 11061da177e4SLinus Torvalds EXPORT_SYMBOL(get_user_pages); 11071da177e4SLinus Torvalds 11081da177e4SLinus Torvalds static int zeromap_pte_range(struct mm_struct *mm, pmd_t *pmd, 11091da177e4SLinus Torvalds unsigned long addr, unsigned long end, pgprot_t prot) 11101da177e4SLinus Torvalds { 11111da177e4SLinus Torvalds pte_t *pte; 1112c74df32cSHugh Dickins spinlock_t *ptl; 11131da177e4SLinus Torvalds 1114c74df32cSHugh Dickins pte = pte_alloc_map_lock(mm, pmd, addr, &ptl); 11151da177e4SLinus Torvalds if (!pte) 11161da177e4SLinus Torvalds return -ENOMEM; 11176606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 11181da177e4SLinus Torvalds do { 1119b5810039SNick Piggin struct page *page = ZERO_PAGE(addr); 1120b5810039SNick Piggin pte_t zero_pte = pte_wrprotect(mk_pte(page, prot)); 1121b5810039SNick Piggin page_cache_get(page); 1122b5810039SNick Piggin page_add_file_rmap(page); 1123b5810039SNick Piggin inc_mm_counter(mm, file_rss); 11241da177e4SLinus Torvalds BUG_ON(!pte_none(*pte)); 11251da177e4SLinus Torvalds set_pte_at(mm, addr, pte, zero_pte); 11261da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 11276606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 1128c74df32cSHugh Dickins pte_unmap_unlock(pte - 1, ptl); 11291da177e4SLinus Torvalds return 0; 11301da177e4SLinus Torvalds } 11311da177e4SLinus Torvalds 11321da177e4SLinus Torvalds static inline int zeromap_pmd_range(struct mm_struct *mm, pud_t *pud, 11331da177e4SLinus Torvalds unsigned long addr, unsigned long end, pgprot_t prot) 11341da177e4SLinus Torvalds { 11351da177e4SLinus Torvalds pmd_t *pmd; 11361da177e4SLinus Torvalds unsigned long next; 11371da177e4SLinus Torvalds 11381da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, addr); 11391da177e4SLinus Torvalds if (!pmd) 11401da177e4SLinus Torvalds return -ENOMEM; 11411da177e4SLinus Torvalds do { 11421da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 11431da177e4SLinus Torvalds if (zeromap_pte_range(mm, pmd, addr, next, prot)) 11441da177e4SLinus Torvalds return -ENOMEM; 11451da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 11461da177e4SLinus Torvalds return 0; 11471da177e4SLinus Torvalds } 11481da177e4SLinus Torvalds 11491da177e4SLinus Torvalds static inline int zeromap_pud_range(struct mm_struct *mm, pgd_t *pgd, 11501da177e4SLinus Torvalds unsigned long addr, unsigned long end, pgprot_t prot) 11511da177e4SLinus Torvalds { 11521da177e4SLinus Torvalds pud_t *pud; 11531da177e4SLinus Torvalds unsigned long next; 11541da177e4SLinus Torvalds 11551da177e4SLinus Torvalds pud = pud_alloc(mm, pgd, addr); 11561da177e4SLinus Torvalds if (!pud) 11571da177e4SLinus Torvalds return -ENOMEM; 11581da177e4SLinus Torvalds do { 11591da177e4SLinus Torvalds next = pud_addr_end(addr, end); 11601da177e4SLinus Torvalds if (zeromap_pmd_range(mm, pud, addr, next, prot)) 11611da177e4SLinus Torvalds return -ENOMEM; 11621da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 11631da177e4SLinus Torvalds return 0; 11641da177e4SLinus Torvalds } 11651da177e4SLinus Torvalds 11661da177e4SLinus Torvalds int zeromap_page_range(struct vm_area_struct *vma, 11671da177e4SLinus Torvalds unsigned long addr, unsigned long size, pgprot_t prot) 11681da177e4SLinus Torvalds { 11691da177e4SLinus Torvalds pgd_t *pgd; 11701da177e4SLinus Torvalds unsigned long next; 11711da177e4SLinus Torvalds unsigned long end = addr + size; 11721da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 11731da177e4SLinus Torvalds int err; 11741da177e4SLinus Torvalds 11751da177e4SLinus Torvalds BUG_ON(addr >= end); 11761da177e4SLinus Torvalds pgd = pgd_offset(mm, addr); 11771da177e4SLinus Torvalds flush_cache_range(vma, addr, end); 11781da177e4SLinus Torvalds do { 11791da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 11801da177e4SLinus Torvalds err = zeromap_pud_range(mm, pgd, addr, next, prot); 11811da177e4SLinus Torvalds if (err) 11821da177e4SLinus Torvalds break; 11831da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 11841da177e4SLinus Torvalds return err; 11851da177e4SLinus Torvalds } 11861da177e4SLinus Torvalds 118749c91fb0STrond Myklebust pte_t * fastcall get_locked_pte(struct mm_struct *mm, unsigned long addr, spinlock_t **ptl) 1188c9cfcddfSLinus Torvalds { 1189c9cfcddfSLinus Torvalds pgd_t * pgd = pgd_offset(mm, addr); 1190c9cfcddfSLinus Torvalds pud_t * pud = pud_alloc(mm, pgd, addr); 1191c9cfcddfSLinus Torvalds if (pud) { 119249c91fb0STrond Myklebust pmd_t * pmd = pmd_alloc(mm, pud, addr); 1193c9cfcddfSLinus Torvalds if (pmd) 1194c9cfcddfSLinus Torvalds return pte_alloc_map_lock(mm, pmd, addr, ptl); 1195c9cfcddfSLinus Torvalds } 1196c9cfcddfSLinus Torvalds return NULL; 1197c9cfcddfSLinus Torvalds } 1198c9cfcddfSLinus Torvalds 11991da177e4SLinus Torvalds /* 1200238f58d8SLinus Torvalds * This is the old fallback for page remapping. 1201238f58d8SLinus Torvalds * 1202238f58d8SLinus Torvalds * For historical reasons, it only allows reserved pages. Only 1203238f58d8SLinus Torvalds * old drivers should use this, and they needed to mark their 1204238f58d8SLinus Torvalds * pages reserved for the old functions anyway. 1205238f58d8SLinus Torvalds */ 1206238f58d8SLinus Torvalds static int insert_page(struct mm_struct *mm, unsigned long addr, struct page *page, pgprot_t prot) 1207238f58d8SLinus Torvalds { 1208238f58d8SLinus Torvalds int retval; 1209238f58d8SLinus Torvalds pte_t *pte; 1210238f58d8SLinus Torvalds spinlock_t *ptl; 1211238f58d8SLinus Torvalds 1212238f58d8SLinus Torvalds retval = -EINVAL; 1213a145dd41SLinus Torvalds if (PageAnon(page)) 1214238f58d8SLinus Torvalds goto out; 1215238f58d8SLinus Torvalds retval = -ENOMEM; 1216238f58d8SLinus Torvalds flush_dcache_page(page); 1217c9cfcddfSLinus Torvalds pte = get_locked_pte(mm, addr, &ptl); 1218238f58d8SLinus Torvalds if (!pte) 1219238f58d8SLinus Torvalds goto out; 1220238f58d8SLinus Torvalds retval = -EBUSY; 1221238f58d8SLinus Torvalds if (!pte_none(*pte)) 1222238f58d8SLinus Torvalds goto out_unlock; 1223238f58d8SLinus Torvalds 1224238f58d8SLinus Torvalds /* Ok, finally just insert the thing.. */ 1225238f58d8SLinus Torvalds get_page(page); 1226238f58d8SLinus Torvalds inc_mm_counter(mm, file_rss); 1227238f58d8SLinus Torvalds page_add_file_rmap(page); 1228238f58d8SLinus Torvalds set_pte_at(mm, addr, pte, mk_pte(page, prot)); 1229238f58d8SLinus Torvalds 1230238f58d8SLinus Torvalds retval = 0; 1231238f58d8SLinus Torvalds out_unlock: 1232238f58d8SLinus Torvalds pte_unmap_unlock(pte, ptl); 1233238f58d8SLinus Torvalds out: 1234238f58d8SLinus Torvalds return retval; 1235238f58d8SLinus Torvalds } 1236238f58d8SLinus Torvalds 1237bfa5bf6dSRolf Eike Beer /** 1238bfa5bf6dSRolf Eike Beer * vm_insert_page - insert single page into user vma 1239bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 1240bfa5bf6dSRolf Eike Beer * @addr: target user address of this page 1241bfa5bf6dSRolf Eike Beer * @page: source kernel page 1242bfa5bf6dSRolf Eike Beer * 1243a145dd41SLinus Torvalds * This allows drivers to insert individual pages they've allocated 1244a145dd41SLinus Torvalds * into a user vma. 1245a145dd41SLinus Torvalds * 1246a145dd41SLinus Torvalds * The page has to be a nice clean _individual_ kernel allocation. 1247a145dd41SLinus Torvalds * If you allocate a compound page, you need to have marked it as 1248a145dd41SLinus Torvalds * such (__GFP_COMP), or manually just split the page up yourself 12498dfcc9baSNick Piggin * (see split_page()). 1250a145dd41SLinus Torvalds * 1251a145dd41SLinus Torvalds * NOTE! Traditionally this was done with "remap_pfn_range()" which 1252a145dd41SLinus Torvalds * took an arbitrary page protection parameter. This doesn't allow 1253a145dd41SLinus Torvalds * that. Your vma protection will have to be set up correctly, which 1254a145dd41SLinus Torvalds * means that if you want a shared writable mapping, you'd better 1255a145dd41SLinus Torvalds * ask for a shared writable mapping! 1256a145dd41SLinus Torvalds * 1257a145dd41SLinus Torvalds * The page does not need to be reserved. 1258a145dd41SLinus Torvalds */ 1259a145dd41SLinus Torvalds int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, struct page *page) 1260a145dd41SLinus Torvalds { 1261a145dd41SLinus Torvalds if (addr < vma->vm_start || addr >= vma->vm_end) 1262a145dd41SLinus Torvalds return -EFAULT; 1263a145dd41SLinus Torvalds if (!page_count(page)) 1264a145dd41SLinus Torvalds return -EINVAL; 12654d7672b4SLinus Torvalds vma->vm_flags |= VM_INSERTPAGE; 1266a145dd41SLinus Torvalds return insert_page(vma->vm_mm, addr, page, vma->vm_page_prot); 1267a145dd41SLinus Torvalds } 1268e3c3374fSLinus Torvalds EXPORT_SYMBOL(vm_insert_page); 1269a145dd41SLinus Torvalds 1270a145dd41SLinus Torvalds /* 12711da177e4SLinus Torvalds * maps a range of physical memory into the requested pages. the old 12721da177e4SLinus Torvalds * mappings are removed. any references to nonexistent pages results 12731da177e4SLinus Torvalds * in null mappings (currently treated as "copy-on-access") 12741da177e4SLinus Torvalds */ 12751da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd, 12761da177e4SLinus Torvalds unsigned long addr, unsigned long end, 12771da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 12781da177e4SLinus Torvalds { 12791da177e4SLinus Torvalds pte_t *pte; 1280c74df32cSHugh Dickins spinlock_t *ptl; 12811da177e4SLinus Torvalds 1282c74df32cSHugh Dickins pte = pte_alloc_map_lock(mm, pmd, addr, &ptl); 12831da177e4SLinus Torvalds if (!pte) 12841da177e4SLinus Torvalds return -ENOMEM; 12856606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 12861da177e4SLinus Torvalds do { 12871da177e4SLinus Torvalds BUG_ON(!pte_none(*pte)); 12881da177e4SLinus Torvalds set_pte_at(mm, addr, pte, pfn_pte(pfn, prot)); 12891da177e4SLinus Torvalds pfn++; 12901da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 12916606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 1292c74df32cSHugh Dickins pte_unmap_unlock(pte - 1, ptl); 12931da177e4SLinus Torvalds return 0; 12941da177e4SLinus Torvalds } 12951da177e4SLinus Torvalds 12961da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud, 12971da177e4SLinus Torvalds unsigned long addr, unsigned long end, 12981da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 12991da177e4SLinus Torvalds { 13001da177e4SLinus Torvalds pmd_t *pmd; 13011da177e4SLinus Torvalds unsigned long next; 13021da177e4SLinus Torvalds 13031da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 13041da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, addr); 13051da177e4SLinus Torvalds if (!pmd) 13061da177e4SLinus Torvalds return -ENOMEM; 13071da177e4SLinus Torvalds do { 13081da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 13091da177e4SLinus Torvalds if (remap_pte_range(mm, pmd, addr, next, 13101da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot)) 13111da177e4SLinus Torvalds return -ENOMEM; 13121da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 13131da177e4SLinus Torvalds return 0; 13141da177e4SLinus Torvalds } 13151da177e4SLinus Torvalds 13161da177e4SLinus Torvalds static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd, 13171da177e4SLinus Torvalds unsigned long addr, unsigned long end, 13181da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 13191da177e4SLinus Torvalds { 13201da177e4SLinus Torvalds pud_t *pud; 13211da177e4SLinus Torvalds unsigned long next; 13221da177e4SLinus Torvalds 13231da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 13241da177e4SLinus Torvalds pud = pud_alloc(mm, pgd, addr); 13251da177e4SLinus Torvalds if (!pud) 13261da177e4SLinus Torvalds return -ENOMEM; 13271da177e4SLinus Torvalds do { 13281da177e4SLinus Torvalds next = pud_addr_end(addr, end); 13291da177e4SLinus Torvalds if (remap_pmd_range(mm, pud, addr, next, 13301da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot)) 13311da177e4SLinus Torvalds return -ENOMEM; 13321da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 13331da177e4SLinus Torvalds return 0; 13341da177e4SLinus Torvalds } 13351da177e4SLinus Torvalds 1336bfa5bf6dSRolf Eike Beer /** 1337bfa5bf6dSRolf Eike Beer * remap_pfn_range - remap kernel memory to userspace 1338bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 1339bfa5bf6dSRolf Eike Beer * @addr: target user address to start at 1340bfa5bf6dSRolf Eike Beer * @pfn: physical address of kernel memory 1341bfa5bf6dSRolf Eike Beer * @size: size of map area 1342bfa5bf6dSRolf Eike Beer * @prot: page protection flags for this mapping 1343bfa5bf6dSRolf Eike Beer * 1344bfa5bf6dSRolf Eike Beer * Note: this is only safe if the mm semaphore is held when called. 1345bfa5bf6dSRolf Eike Beer */ 13461da177e4SLinus Torvalds int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 13471da177e4SLinus Torvalds unsigned long pfn, unsigned long size, pgprot_t prot) 13481da177e4SLinus Torvalds { 13491da177e4SLinus Torvalds pgd_t *pgd; 13501da177e4SLinus Torvalds unsigned long next; 13512d15cab8SHugh Dickins unsigned long end = addr + PAGE_ALIGN(size); 13521da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 13531da177e4SLinus Torvalds int err; 13541da177e4SLinus Torvalds 13551da177e4SLinus Torvalds /* 13561da177e4SLinus Torvalds * Physically remapped pages are special. Tell the 13571da177e4SLinus Torvalds * rest of the world about it: 13581da177e4SLinus Torvalds * VM_IO tells people not to look at these pages 13591da177e4SLinus Torvalds * (accesses can have side effects). 13600b14c179SHugh Dickins * VM_RESERVED is specified all over the place, because 13610b14c179SHugh Dickins * in 2.4 it kept swapout's vma scan off this vma; but 13620b14c179SHugh Dickins * in 2.6 the LRU scan won't even find its pages, so this 13630b14c179SHugh Dickins * flag means no more than count its pages in reserved_vm, 13640b14c179SHugh Dickins * and omit it from core dump, even when VM_IO turned off. 13656aab341eSLinus Torvalds * VM_PFNMAP tells the core MM that the base pages are just 13666aab341eSLinus Torvalds * raw PFN mappings, and do not have a "struct page" associated 13676aab341eSLinus Torvalds * with them. 1368fb155c16SLinus Torvalds * 1369fb155c16SLinus Torvalds * There's a horrible special case to handle copy-on-write 1370fb155c16SLinus Torvalds * behaviour that some programs depend on. We mark the "original" 1371fb155c16SLinus Torvalds * un-COW'ed pages by matching them up with "vma->vm_pgoff". 13721da177e4SLinus Torvalds */ 137367121172SLinus Torvalds if (is_cow_mapping(vma->vm_flags)) { 1374fb155c16SLinus Torvalds if (addr != vma->vm_start || end != vma->vm_end) 13757fc7e2eeSLinus Torvalds return -EINVAL; 13766aab341eSLinus Torvalds vma->vm_pgoff = pfn; 1377fb155c16SLinus Torvalds } 1378fb155c16SLinus Torvalds 1379fb155c16SLinus Torvalds vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP; 13801da177e4SLinus Torvalds 13811da177e4SLinus Torvalds BUG_ON(addr >= end); 13821da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 13831da177e4SLinus Torvalds pgd = pgd_offset(mm, addr); 13841da177e4SLinus Torvalds flush_cache_range(vma, addr, end); 13851da177e4SLinus Torvalds do { 13861da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 13871da177e4SLinus Torvalds err = remap_pud_range(mm, pgd, addr, next, 13881da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot); 13891da177e4SLinus Torvalds if (err) 13901da177e4SLinus Torvalds break; 13911da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 13921da177e4SLinus Torvalds return err; 13931da177e4SLinus Torvalds } 13941da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range); 13951da177e4SLinus Torvalds 13961da177e4SLinus Torvalds /* 13978f4e2101SHugh Dickins * handle_pte_fault chooses page fault handler according to an entry 13988f4e2101SHugh Dickins * which was read non-atomically. Before making any commitment, on 13998f4e2101SHugh Dickins * those architectures or configurations (e.g. i386 with PAE) which 14008f4e2101SHugh Dickins * might give a mix of unmatched parts, do_swap_page and do_file_page 14018f4e2101SHugh Dickins * must check under lock before unmapping the pte and proceeding 14028f4e2101SHugh Dickins * (but do_wp_page is only called after already making such a check; 14038f4e2101SHugh Dickins * and do_anonymous_page and do_no_page can safely check later on). 14048f4e2101SHugh Dickins */ 14054c21e2f2SHugh Dickins static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd, 14068f4e2101SHugh Dickins pte_t *page_table, pte_t orig_pte) 14078f4e2101SHugh Dickins { 14088f4e2101SHugh Dickins int same = 1; 14098f4e2101SHugh Dickins #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT) 14108f4e2101SHugh Dickins if (sizeof(pte_t) > sizeof(unsigned long)) { 14114c21e2f2SHugh Dickins spinlock_t *ptl = pte_lockptr(mm, pmd); 14124c21e2f2SHugh Dickins spin_lock(ptl); 14138f4e2101SHugh Dickins same = pte_same(*page_table, orig_pte); 14144c21e2f2SHugh Dickins spin_unlock(ptl); 14158f4e2101SHugh Dickins } 14168f4e2101SHugh Dickins #endif 14178f4e2101SHugh Dickins pte_unmap(page_table); 14188f4e2101SHugh Dickins return same; 14198f4e2101SHugh Dickins } 14208f4e2101SHugh Dickins 14218f4e2101SHugh Dickins /* 14221da177e4SLinus Torvalds * Do pte_mkwrite, but only if the vma says VM_WRITE. We do this when 14231da177e4SLinus Torvalds * servicing faults for write access. In the normal case, do always want 14241da177e4SLinus Torvalds * pte_mkwrite. But get_user_pages can cause write faults for mappings 14251da177e4SLinus Torvalds * that do not have writing enabled, when used by access_process_vm. 14261da177e4SLinus Torvalds */ 14271da177e4SLinus Torvalds static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma) 14281da177e4SLinus Torvalds { 14291da177e4SLinus Torvalds if (likely(vma->vm_flags & VM_WRITE)) 14301da177e4SLinus Torvalds pte = pte_mkwrite(pte); 14311da177e4SLinus Torvalds return pte; 14321da177e4SLinus Torvalds } 14331da177e4SLinus Torvalds 14346aab341eSLinus Torvalds static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va) 14356aab341eSLinus Torvalds { 14366aab341eSLinus Torvalds /* 14376aab341eSLinus Torvalds * If the source page was a PFN mapping, we don't have 14386aab341eSLinus Torvalds * a "struct page" for it. We do a best-effort copy by 14396aab341eSLinus Torvalds * just copying from the original user address. If that 14406aab341eSLinus Torvalds * fails, we just zero-fill it. Live with it. 14416aab341eSLinus Torvalds */ 14426aab341eSLinus Torvalds if (unlikely(!src)) { 14436aab341eSLinus Torvalds void *kaddr = kmap_atomic(dst, KM_USER0); 14445d2a2dbbSLinus Torvalds void __user *uaddr = (void __user *)(va & PAGE_MASK); 14455d2a2dbbSLinus Torvalds 14465d2a2dbbSLinus Torvalds /* 14475d2a2dbbSLinus Torvalds * This really shouldn't fail, because the page is there 14485d2a2dbbSLinus Torvalds * in the page tables. But it might just be unreadable, 14495d2a2dbbSLinus Torvalds * in which case we just give up and fill the result with 14505d2a2dbbSLinus Torvalds * zeroes. 14515d2a2dbbSLinus Torvalds */ 14525d2a2dbbSLinus Torvalds if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) 14536aab341eSLinus Torvalds memset(kaddr, 0, PAGE_SIZE); 14546aab341eSLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 1455*c4ec7b0dSDmitriy Monakhov flush_dcache_page(dst); 14566aab341eSLinus Torvalds return; 14576aab341eSLinus Torvalds 14586aab341eSLinus Torvalds } 14596aab341eSLinus Torvalds copy_user_highpage(dst, src, va); 14606aab341eSLinus Torvalds } 14616aab341eSLinus Torvalds 14621da177e4SLinus Torvalds /* 14631da177e4SLinus Torvalds * This routine handles present pages, when users try to write 14641da177e4SLinus Torvalds * to a shared page. It is done by copying the page to a new address 14651da177e4SLinus Torvalds * and decrementing the shared-page counter for the old page. 14661da177e4SLinus Torvalds * 14671da177e4SLinus Torvalds * Note that this routine assumes that the protection checks have been 14681da177e4SLinus Torvalds * done by the caller (the low-level page fault routine in most cases). 14691da177e4SLinus Torvalds * Thus we can safely just mark it writable once we've done any necessary 14701da177e4SLinus Torvalds * COW. 14711da177e4SLinus Torvalds * 14721da177e4SLinus Torvalds * We also mark the page dirty at this point even though the page will 14731da177e4SLinus Torvalds * change only once the write actually happens. This avoids a few races, 14741da177e4SLinus Torvalds * and potentially makes it more efficient. 14751da177e4SLinus Torvalds * 14768f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 14778f4e2101SHugh Dickins * but allow concurrent faults), with pte both mapped and locked. 14788f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 14791da177e4SLinus Torvalds */ 14801da177e4SLinus Torvalds static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma, 148165500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 14828f4e2101SHugh Dickins spinlock_t *ptl, pte_t orig_pte) 14831da177e4SLinus Torvalds { 1484e5bbe4dfSHugh Dickins struct page *old_page, *new_page; 14851da177e4SLinus Torvalds pte_t entry; 1486d08b3851SPeter Zijlstra int reuse = 0, ret = VM_FAULT_MINOR; 1487d08b3851SPeter Zijlstra struct page *dirty_page = NULL; 14881da177e4SLinus Torvalds 14896aab341eSLinus Torvalds old_page = vm_normal_page(vma, address, orig_pte); 14906aab341eSLinus Torvalds if (!old_page) 1491920fc356SHugh Dickins goto gotten; 14921da177e4SLinus Torvalds 1493d08b3851SPeter Zijlstra /* 1494ee6a6457SPeter Zijlstra * Take out anonymous pages first, anonymous shared vmas are 1495ee6a6457SPeter Zijlstra * not dirty accountable. 1496d08b3851SPeter Zijlstra */ 1497ee6a6457SPeter Zijlstra if (PageAnon(old_page)) { 1498ee6a6457SPeter Zijlstra if (!TestSetPageLocked(old_page)) { 1499ee6a6457SPeter Zijlstra reuse = can_share_swap_page(old_page); 1500ee6a6457SPeter Zijlstra unlock_page(old_page); 1501ee6a6457SPeter Zijlstra } 1502ee6a6457SPeter Zijlstra } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 1503d08b3851SPeter Zijlstra (VM_WRITE|VM_SHARED))) { 1504ee6a6457SPeter Zijlstra /* 1505ee6a6457SPeter Zijlstra * Only catch write-faults on shared writable pages, 1506ee6a6457SPeter Zijlstra * read-only shared pages can get COWed by 1507ee6a6457SPeter Zijlstra * get_user_pages(.write=1, .force=1). 1508ee6a6457SPeter Zijlstra */ 15099637a5efSDavid Howells if (vma->vm_ops && vma->vm_ops->page_mkwrite) { 15109637a5efSDavid Howells /* 15119637a5efSDavid Howells * Notify the address space that the page is about to 15129637a5efSDavid Howells * become writable so that it can prohibit this or wait 15139637a5efSDavid Howells * for the page to get into an appropriate state. 15149637a5efSDavid Howells * 15159637a5efSDavid Howells * We do this without the lock held, so that it can 15169637a5efSDavid Howells * sleep if it needs to. 15179637a5efSDavid Howells */ 15189637a5efSDavid Howells page_cache_get(old_page); 15199637a5efSDavid Howells pte_unmap_unlock(page_table, ptl); 15209637a5efSDavid Howells 15219637a5efSDavid Howells if (vma->vm_ops->page_mkwrite(vma, old_page) < 0) 15229637a5efSDavid Howells goto unwritable_page; 15239637a5efSDavid Howells 15249637a5efSDavid Howells page_cache_release(old_page); 15259637a5efSDavid Howells 15269637a5efSDavid Howells /* 15279637a5efSDavid Howells * Since we dropped the lock we need to revalidate 15289637a5efSDavid Howells * the PTE as someone else may have changed it. If 15299637a5efSDavid Howells * they did, we just return, as we can count on the 15309637a5efSDavid Howells * MMU to tell us if they didn't also make it writable. 15319637a5efSDavid Howells */ 15329637a5efSDavid Howells page_table = pte_offset_map_lock(mm, pmd, address, 15339637a5efSDavid Howells &ptl); 15349637a5efSDavid Howells if (!pte_same(*page_table, orig_pte)) 15359637a5efSDavid Howells goto unlock; 15369637a5efSDavid Howells } 1537d08b3851SPeter Zijlstra dirty_page = old_page; 1538d08b3851SPeter Zijlstra get_page(dirty_page); 15399637a5efSDavid Howells reuse = 1; 15409637a5efSDavid Howells } 15419637a5efSDavid Howells 15421da177e4SLinus Torvalds if (reuse) { 1543eca35133SBen Collins flush_cache_page(vma, address, pte_pfn(orig_pte)); 154465500d23SHugh Dickins entry = pte_mkyoung(orig_pte); 154565500d23SHugh Dickins entry = maybe_mkwrite(pte_mkdirty(entry), vma); 15461da177e4SLinus Torvalds ptep_set_access_flags(vma, address, page_table, entry, 1); 15471da177e4SLinus Torvalds update_mmu_cache(vma, address, entry); 15481da177e4SLinus Torvalds lazy_mmu_prot_update(entry); 154965500d23SHugh Dickins ret |= VM_FAULT_WRITE; 155065500d23SHugh Dickins goto unlock; 15511da177e4SLinus Torvalds } 15521da177e4SLinus Torvalds 15531da177e4SLinus Torvalds /* 15541da177e4SLinus Torvalds * Ok, we need to copy. Oh, well.. 15551da177e4SLinus Torvalds */ 15561da177e4SLinus Torvalds page_cache_get(old_page); 1557920fc356SHugh Dickins gotten: 15588f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 15591da177e4SLinus Torvalds 15601da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 156165500d23SHugh Dickins goto oom; 1562e5bbe4dfSHugh Dickins if (old_page == ZERO_PAGE(address)) { 15631da177e4SLinus Torvalds new_page = alloc_zeroed_user_highpage(vma, address); 15641da177e4SLinus Torvalds if (!new_page) 156565500d23SHugh Dickins goto oom; 15661da177e4SLinus Torvalds } else { 15671da177e4SLinus Torvalds new_page = alloc_page_vma(GFP_HIGHUSER, vma, address); 15681da177e4SLinus Torvalds if (!new_page) 156965500d23SHugh Dickins goto oom; 1570e5bbe4dfSHugh Dickins cow_user_page(new_page, old_page, address); 15711da177e4SLinus Torvalds } 157265500d23SHugh Dickins 15731da177e4SLinus Torvalds /* 15741da177e4SLinus Torvalds * Re-check the pte - we dropped the lock 15751da177e4SLinus Torvalds */ 15768f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 157765500d23SHugh Dickins if (likely(pte_same(*page_table, orig_pte))) { 1578920fc356SHugh Dickins if (old_page) { 15791da177e4SLinus Torvalds page_remove_rmap(old_page); 15804294621fSHugh Dickins if (!PageAnon(old_page)) { 15814294621fSHugh Dickins dec_mm_counter(mm, file_rss); 1582920fc356SHugh Dickins inc_mm_counter(mm, anon_rss); 15834294621fSHugh Dickins } 1584920fc356SHugh Dickins } else 1585920fc356SHugh Dickins inc_mm_counter(mm, anon_rss); 1586eca35133SBen Collins flush_cache_page(vma, address, pte_pfn(orig_pte)); 158765500d23SHugh Dickins entry = mk_pte(new_page, vma->vm_page_prot); 158865500d23SHugh Dickins entry = maybe_mkwrite(pte_mkdirty(entry), vma); 1589c38c8db7SAnil Keshavamurthy lazy_mmu_prot_update(entry); 15904ce072f1SSiddha, Suresh B /* 15914ce072f1SSiddha, Suresh B * Clear the pte entry and flush it first, before updating the 15924ce072f1SSiddha, Suresh B * pte with the new entry. This will avoid a race condition 15934ce072f1SSiddha, Suresh B * seen in the presence of one thread doing SMC and another 15944ce072f1SSiddha, Suresh B * thread doing COW. 15954ce072f1SSiddha, Suresh B */ 15964ce072f1SSiddha, Suresh B ptep_clear_flush(vma, address, page_table); 15974ce072f1SSiddha, Suresh B set_pte_at(mm, address, page_table, entry); 159865500d23SHugh Dickins update_mmu_cache(vma, address, entry); 15991da177e4SLinus Torvalds lru_cache_add_active(new_page); 16009617d95eSNick Piggin page_add_new_anon_rmap(new_page, vma, address); 16011da177e4SLinus Torvalds 16021da177e4SLinus Torvalds /* Free the old page.. */ 16031da177e4SLinus Torvalds new_page = old_page; 1604f33ea7f4SNick Piggin ret |= VM_FAULT_WRITE; 16051da177e4SLinus Torvalds } 1606920fc356SHugh Dickins if (new_page) 16071da177e4SLinus Torvalds page_cache_release(new_page); 1608920fc356SHugh Dickins if (old_page) 16091da177e4SLinus Torvalds page_cache_release(old_page); 161065500d23SHugh Dickins unlock: 16118f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 1612d08b3851SPeter Zijlstra if (dirty_page) { 1613edc79b2aSPeter Zijlstra set_page_dirty_balance(dirty_page); 1614d08b3851SPeter Zijlstra put_page(dirty_page); 1615d08b3851SPeter Zijlstra } 1616f33ea7f4SNick Piggin return ret; 161765500d23SHugh Dickins oom: 1618920fc356SHugh Dickins if (old_page) 16191da177e4SLinus Torvalds page_cache_release(old_page); 16201da177e4SLinus Torvalds return VM_FAULT_OOM; 16219637a5efSDavid Howells 16229637a5efSDavid Howells unwritable_page: 16239637a5efSDavid Howells page_cache_release(old_page); 16249637a5efSDavid Howells return VM_FAULT_SIGBUS; 16251da177e4SLinus Torvalds } 16261da177e4SLinus Torvalds 16271da177e4SLinus Torvalds /* 16281da177e4SLinus Torvalds * Helper functions for unmap_mapping_range(). 16291da177e4SLinus Torvalds * 16301da177e4SLinus Torvalds * __ Notes on dropping i_mmap_lock to reduce latency while unmapping __ 16311da177e4SLinus Torvalds * 16321da177e4SLinus Torvalds * We have to restart searching the prio_tree whenever we drop the lock, 16331da177e4SLinus Torvalds * since the iterator is only valid while the lock is held, and anyway 16341da177e4SLinus Torvalds * a later vma might be split and reinserted earlier while lock dropped. 16351da177e4SLinus Torvalds * 16361da177e4SLinus Torvalds * The list of nonlinear vmas could be handled more efficiently, using 16371da177e4SLinus Torvalds * a placeholder, but handle it in the same way until a need is shown. 16381da177e4SLinus Torvalds * It is important to search the prio_tree before nonlinear list: a vma 16391da177e4SLinus Torvalds * may become nonlinear and be shifted from prio_tree to nonlinear list 16401da177e4SLinus Torvalds * while the lock is dropped; but never shifted from list to prio_tree. 16411da177e4SLinus Torvalds * 16421da177e4SLinus Torvalds * In order to make forward progress despite restarting the search, 16431da177e4SLinus Torvalds * vm_truncate_count is used to mark a vma as now dealt with, so we can 16441da177e4SLinus Torvalds * quickly skip it next time around. Since the prio_tree search only 16451da177e4SLinus Torvalds * shows us those vmas affected by unmapping the range in question, we 16461da177e4SLinus Torvalds * can't efficiently keep all vmas in step with mapping->truncate_count: 16471da177e4SLinus Torvalds * so instead reset them all whenever it wraps back to 0 (then go to 1). 16481da177e4SLinus Torvalds * mapping->truncate_count and vma->vm_truncate_count are protected by 16491da177e4SLinus Torvalds * i_mmap_lock. 16501da177e4SLinus Torvalds * 16511da177e4SLinus Torvalds * In order to make forward progress despite repeatedly restarting some 1652ee39b37bSHugh Dickins * large vma, note the restart_addr from unmap_vmas when it breaks out: 16531da177e4SLinus Torvalds * and restart from that address when we reach that vma again. It might 16541da177e4SLinus Torvalds * have been split or merged, shrunk or extended, but never shifted: so 16551da177e4SLinus Torvalds * restart_addr remains valid so long as it remains in the vma's range. 16561da177e4SLinus Torvalds * unmap_mapping_range forces truncate_count to leap over page-aligned 16571da177e4SLinus Torvalds * values so we can save vma's restart_addr in its truncate_count field. 16581da177e4SLinus Torvalds */ 16591da177e4SLinus Torvalds #define is_restart_addr(truncate_count) (!((truncate_count) & ~PAGE_MASK)) 16601da177e4SLinus Torvalds 16611da177e4SLinus Torvalds static void reset_vma_truncate_counts(struct address_space *mapping) 16621da177e4SLinus Torvalds { 16631da177e4SLinus Torvalds struct vm_area_struct *vma; 16641da177e4SLinus Torvalds struct prio_tree_iter iter; 16651da177e4SLinus Torvalds 16661da177e4SLinus Torvalds vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, ULONG_MAX) 16671da177e4SLinus Torvalds vma->vm_truncate_count = 0; 16681da177e4SLinus Torvalds list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list) 16691da177e4SLinus Torvalds vma->vm_truncate_count = 0; 16701da177e4SLinus Torvalds } 16711da177e4SLinus Torvalds 16721da177e4SLinus Torvalds static int unmap_mapping_range_vma(struct vm_area_struct *vma, 16731da177e4SLinus Torvalds unsigned long start_addr, unsigned long end_addr, 16741da177e4SLinus Torvalds struct zap_details *details) 16751da177e4SLinus Torvalds { 16761da177e4SLinus Torvalds unsigned long restart_addr; 16771da177e4SLinus Torvalds int need_break; 16781da177e4SLinus Torvalds 16791da177e4SLinus Torvalds again: 16801da177e4SLinus Torvalds restart_addr = vma->vm_truncate_count; 16811da177e4SLinus Torvalds if (is_restart_addr(restart_addr) && start_addr < restart_addr) { 16821da177e4SLinus Torvalds start_addr = restart_addr; 16831da177e4SLinus Torvalds if (start_addr >= end_addr) { 16841da177e4SLinus Torvalds /* Top of vma has been split off since last time */ 16851da177e4SLinus Torvalds vma->vm_truncate_count = details->truncate_count; 16861da177e4SLinus Torvalds return 0; 16871da177e4SLinus Torvalds } 16881da177e4SLinus Torvalds } 16891da177e4SLinus Torvalds 1690ee39b37bSHugh Dickins restart_addr = zap_page_range(vma, start_addr, 1691ee39b37bSHugh Dickins end_addr - start_addr, details); 16921da177e4SLinus Torvalds need_break = need_resched() || 16931da177e4SLinus Torvalds need_lockbreak(details->i_mmap_lock); 16941da177e4SLinus Torvalds 1695ee39b37bSHugh Dickins if (restart_addr >= end_addr) { 16961da177e4SLinus Torvalds /* We have now completed this vma: mark it so */ 16971da177e4SLinus Torvalds vma->vm_truncate_count = details->truncate_count; 16981da177e4SLinus Torvalds if (!need_break) 16991da177e4SLinus Torvalds return 0; 17001da177e4SLinus Torvalds } else { 17011da177e4SLinus Torvalds /* Note restart_addr in vma's truncate_count field */ 1702ee39b37bSHugh Dickins vma->vm_truncate_count = restart_addr; 17031da177e4SLinus Torvalds if (!need_break) 17041da177e4SLinus Torvalds goto again; 17051da177e4SLinus Torvalds } 17061da177e4SLinus Torvalds 17071da177e4SLinus Torvalds spin_unlock(details->i_mmap_lock); 17081da177e4SLinus Torvalds cond_resched(); 17091da177e4SLinus Torvalds spin_lock(details->i_mmap_lock); 17101da177e4SLinus Torvalds return -EINTR; 17111da177e4SLinus Torvalds } 17121da177e4SLinus Torvalds 17131da177e4SLinus Torvalds static inline void unmap_mapping_range_tree(struct prio_tree_root *root, 17141da177e4SLinus Torvalds struct zap_details *details) 17151da177e4SLinus Torvalds { 17161da177e4SLinus Torvalds struct vm_area_struct *vma; 17171da177e4SLinus Torvalds struct prio_tree_iter iter; 17181da177e4SLinus Torvalds pgoff_t vba, vea, zba, zea; 17191da177e4SLinus Torvalds 17201da177e4SLinus Torvalds restart: 17211da177e4SLinus Torvalds vma_prio_tree_foreach(vma, &iter, root, 17221da177e4SLinus Torvalds details->first_index, details->last_index) { 17231da177e4SLinus Torvalds /* Skip quickly over those we have already dealt with */ 17241da177e4SLinus Torvalds if (vma->vm_truncate_count == details->truncate_count) 17251da177e4SLinus Torvalds continue; 17261da177e4SLinus Torvalds 17271da177e4SLinus Torvalds vba = vma->vm_pgoff; 17281da177e4SLinus Torvalds vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1; 17291da177e4SLinus Torvalds /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */ 17301da177e4SLinus Torvalds zba = details->first_index; 17311da177e4SLinus Torvalds if (zba < vba) 17321da177e4SLinus Torvalds zba = vba; 17331da177e4SLinus Torvalds zea = details->last_index; 17341da177e4SLinus Torvalds if (zea > vea) 17351da177e4SLinus Torvalds zea = vea; 17361da177e4SLinus Torvalds 17371da177e4SLinus Torvalds if (unmap_mapping_range_vma(vma, 17381da177e4SLinus Torvalds ((zba - vba) << PAGE_SHIFT) + vma->vm_start, 17391da177e4SLinus Torvalds ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start, 17401da177e4SLinus Torvalds details) < 0) 17411da177e4SLinus Torvalds goto restart; 17421da177e4SLinus Torvalds } 17431da177e4SLinus Torvalds } 17441da177e4SLinus Torvalds 17451da177e4SLinus Torvalds static inline void unmap_mapping_range_list(struct list_head *head, 17461da177e4SLinus Torvalds struct zap_details *details) 17471da177e4SLinus Torvalds { 17481da177e4SLinus Torvalds struct vm_area_struct *vma; 17491da177e4SLinus Torvalds 17501da177e4SLinus Torvalds /* 17511da177e4SLinus Torvalds * In nonlinear VMAs there is no correspondence between virtual address 17521da177e4SLinus Torvalds * offset and file offset. So we must perform an exhaustive search 17531da177e4SLinus Torvalds * across *all* the pages in each nonlinear VMA, not just the pages 17541da177e4SLinus Torvalds * whose virtual address lies outside the file truncation point. 17551da177e4SLinus Torvalds */ 17561da177e4SLinus Torvalds restart: 17571da177e4SLinus Torvalds list_for_each_entry(vma, head, shared.vm_set.list) { 17581da177e4SLinus Torvalds /* Skip quickly over those we have already dealt with */ 17591da177e4SLinus Torvalds if (vma->vm_truncate_count == details->truncate_count) 17601da177e4SLinus Torvalds continue; 17611da177e4SLinus Torvalds details->nonlinear_vma = vma; 17621da177e4SLinus Torvalds if (unmap_mapping_range_vma(vma, vma->vm_start, 17631da177e4SLinus Torvalds vma->vm_end, details) < 0) 17641da177e4SLinus Torvalds goto restart; 17651da177e4SLinus Torvalds } 17661da177e4SLinus Torvalds } 17671da177e4SLinus Torvalds 17681da177e4SLinus Torvalds /** 17691da177e4SLinus Torvalds * unmap_mapping_range - unmap the portion of all mmaps 17701da177e4SLinus Torvalds * in the specified address_space corresponding to the specified 17711da177e4SLinus Torvalds * page range in the underlying file. 17723d41088fSMartin Waitz * @mapping: the address space containing mmaps to be unmapped. 17731da177e4SLinus Torvalds * @holebegin: byte in first page to unmap, relative to the start of 17741da177e4SLinus Torvalds * the underlying file. This will be rounded down to a PAGE_SIZE 17751da177e4SLinus Torvalds * boundary. Note that this is different from vmtruncate(), which 17761da177e4SLinus Torvalds * must keep the partial page. In contrast, we must get rid of 17771da177e4SLinus Torvalds * partial pages. 17781da177e4SLinus Torvalds * @holelen: size of prospective hole in bytes. This will be rounded 17791da177e4SLinus Torvalds * up to a PAGE_SIZE boundary. A holelen of zero truncates to the 17801da177e4SLinus Torvalds * end of the file. 17811da177e4SLinus Torvalds * @even_cows: 1 when truncating a file, unmap even private COWed pages; 17821da177e4SLinus Torvalds * but 0 when invalidating pagecache, don't throw away private data. 17831da177e4SLinus Torvalds */ 17841da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping, 17851da177e4SLinus Torvalds loff_t const holebegin, loff_t const holelen, int even_cows) 17861da177e4SLinus Torvalds { 17871da177e4SLinus Torvalds struct zap_details details; 17881da177e4SLinus Torvalds pgoff_t hba = holebegin >> PAGE_SHIFT; 17891da177e4SLinus Torvalds pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 17901da177e4SLinus Torvalds 17911da177e4SLinus Torvalds /* Check for overflow. */ 17921da177e4SLinus Torvalds if (sizeof(holelen) > sizeof(hlen)) { 17931da177e4SLinus Torvalds long long holeend = 17941da177e4SLinus Torvalds (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 17951da177e4SLinus Torvalds if (holeend & ~(long long)ULONG_MAX) 17961da177e4SLinus Torvalds hlen = ULONG_MAX - hba + 1; 17971da177e4SLinus Torvalds } 17981da177e4SLinus Torvalds 17991da177e4SLinus Torvalds details.check_mapping = even_cows? NULL: mapping; 18001da177e4SLinus Torvalds details.nonlinear_vma = NULL; 18011da177e4SLinus Torvalds details.first_index = hba; 18021da177e4SLinus Torvalds details.last_index = hba + hlen - 1; 18031da177e4SLinus Torvalds if (details.last_index < details.first_index) 18041da177e4SLinus Torvalds details.last_index = ULONG_MAX; 18051da177e4SLinus Torvalds details.i_mmap_lock = &mapping->i_mmap_lock; 18061da177e4SLinus Torvalds 18071da177e4SLinus Torvalds spin_lock(&mapping->i_mmap_lock); 18081da177e4SLinus Torvalds 18091da177e4SLinus Torvalds /* serialize i_size write against truncate_count write */ 18101da177e4SLinus Torvalds smp_wmb(); 18111da177e4SLinus Torvalds /* Protect against page faults, and endless unmapping loops */ 18121da177e4SLinus Torvalds mapping->truncate_count++; 18131da177e4SLinus Torvalds /* 18141da177e4SLinus Torvalds * For archs where spin_lock has inclusive semantics like ia64 18151da177e4SLinus Torvalds * this smp_mb() will prevent to read pagetable contents 18161da177e4SLinus Torvalds * before the truncate_count increment is visible to 18171da177e4SLinus Torvalds * other cpus. 18181da177e4SLinus Torvalds */ 18191da177e4SLinus Torvalds smp_mb(); 18201da177e4SLinus Torvalds if (unlikely(is_restart_addr(mapping->truncate_count))) { 18211da177e4SLinus Torvalds if (mapping->truncate_count == 0) 18221da177e4SLinus Torvalds reset_vma_truncate_counts(mapping); 18231da177e4SLinus Torvalds mapping->truncate_count++; 18241da177e4SLinus Torvalds } 18251da177e4SLinus Torvalds details.truncate_count = mapping->truncate_count; 18261da177e4SLinus Torvalds 18271da177e4SLinus Torvalds if (unlikely(!prio_tree_empty(&mapping->i_mmap))) 18281da177e4SLinus Torvalds unmap_mapping_range_tree(&mapping->i_mmap, &details); 18291da177e4SLinus Torvalds if (unlikely(!list_empty(&mapping->i_mmap_nonlinear))) 18301da177e4SLinus Torvalds unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details); 18311da177e4SLinus Torvalds spin_unlock(&mapping->i_mmap_lock); 18321da177e4SLinus Torvalds } 18331da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range); 18341da177e4SLinus Torvalds 1835bfa5bf6dSRolf Eike Beer /** 1836bfa5bf6dSRolf Eike Beer * vmtruncate - unmap mappings "freed" by truncate() syscall 1837bfa5bf6dSRolf Eike Beer * @inode: inode of the file used 1838bfa5bf6dSRolf Eike Beer * @offset: file offset to start truncating 18391da177e4SLinus Torvalds * 18401da177e4SLinus Torvalds * NOTE! We have to be ready to update the memory sharing 18411da177e4SLinus Torvalds * between the file and the memory map for a potential last 18421da177e4SLinus Torvalds * incomplete page. Ugly, but necessary. 18431da177e4SLinus Torvalds */ 18441da177e4SLinus Torvalds int vmtruncate(struct inode * inode, loff_t offset) 18451da177e4SLinus Torvalds { 18461da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 18471da177e4SLinus Torvalds unsigned long limit; 18481da177e4SLinus Torvalds 18491da177e4SLinus Torvalds if (inode->i_size < offset) 18501da177e4SLinus Torvalds goto do_expand; 18511da177e4SLinus Torvalds /* 18521da177e4SLinus Torvalds * truncation of in-use swapfiles is disallowed - it would cause 18531da177e4SLinus Torvalds * subsequent swapout to scribble on the now-freed blocks. 18541da177e4SLinus Torvalds */ 18551da177e4SLinus Torvalds if (IS_SWAPFILE(inode)) 18561da177e4SLinus Torvalds goto out_busy; 18571da177e4SLinus Torvalds i_size_write(inode, offset); 18581da177e4SLinus Torvalds unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1); 18591da177e4SLinus Torvalds truncate_inode_pages(mapping, offset); 18601da177e4SLinus Torvalds goto out_truncate; 18611da177e4SLinus Torvalds 18621da177e4SLinus Torvalds do_expand: 18631da177e4SLinus Torvalds limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur; 18641da177e4SLinus Torvalds if (limit != RLIM_INFINITY && offset > limit) 18651da177e4SLinus Torvalds goto out_sig; 18661da177e4SLinus Torvalds if (offset > inode->i_sb->s_maxbytes) 18671da177e4SLinus Torvalds goto out_big; 18681da177e4SLinus Torvalds i_size_write(inode, offset); 18691da177e4SLinus Torvalds 18701da177e4SLinus Torvalds out_truncate: 18711da177e4SLinus Torvalds if (inode->i_op && inode->i_op->truncate) 18721da177e4SLinus Torvalds inode->i_op->truncate(inode); 18731da177e4SLinus Torvalds return 0; 18741da177e4SLinus Torvalds out_sig: 18751da177e4SLinus Torvalds send_sig(SIGXFSZ, current, 0); 18761da177e4SLinus Torvalds out_big: 18771da177e4SLinus Torvalds return -EFBIG; 18781da177e4SLinus Torvalds out_busy: 18791da177e4SLinus Torvalds return -ETXTBSY; 18801da177e4SLinus Torvalds } 18811da177e4SLinus Torvalds EXPORT_SYMBOL(vmtruncate); 18821da177e4SLinus Torvalds 1883f6b3ec23SBadari Pulavarty int vmtruncate_range(struct inode *inode, loff_t offset, loff_t end) 1884f6b3ec23SBadari Pulavarty { 1885f6b3ec23SBadari Pulavarty struct address_space *mapping = inode->i_mapping; 1886f6b3ec23SBadari Pulavarty 1887f6b3ec23SBadari Pulavarty /* 1888f6b3ec23SBadari Pulavarty * If the underlying filesystem is not going to provide 1889f6b3ec23SBadari Pulavarty * a way to truncate a range of blocks (punch a hole) - 1890f6b3ec23SBadari Pulavarty * we should return failure right now. 1891f6b3ec23SBadari Pulavarty */ 1892f6b3ec23SBadari Pulavarty if (!inode->i_op || !inode->i_op->truncate_range) 1893f6b3ec23SBadari Pulavarty return -ENOSYS; 1894f6b3ec23SBadari Pulavarty 18951b1dcc1bSJes Sorensen mutex_lock(&inode->i_mutex); 1896f6b3ec23SBadari Pulavarty down_write(&inode->i_alloc_sem); 1897f6b3ec23SBadari Pulavarty unmap_mapping_range(mapping, offset, (end - offset), 1); 1898f6b3ec23SBadari Pulavarty truncate_inode_pages_range(mapping, offset, end); 1899f6b3ec23SBadari Pulavarty inode->i_op->truncate_range(inode, offset, end); 1900f6b3ec23SBadari Pulavarty up_write(&inode->i_alloc_sem); 19011b1dcc1bSJes Sorensen mutex_unlock(&inode->i_mutex); 1902f6b3ec23SBadari Pulavarty 1903f6b3ec23SBadari Pulavarty return 0; 1904f6b3ec23SBadari Pulavarty } 190526fc5236SAdrian Bunk EXPORT_UNUSED_SYMBOL(vmtruncate_range); /* June 2006 */ 1906f6b3ec23SBadari Pulavarty 1907bfa5bf6dSRolf Eike Beer /** 1908bfa5bf6dSRolf Eike Beer * swapin_readahead - swap in pages in hope we need them soon 1909bfa5bf6dSRolf Eike Beer * @entry: swap entry of this memory 1910bfa5bf6dSRolf Eike Beer * @addr: address to start 1911bfa5bf6dSRolf Eike Beer * @vma: user vma this addresses belong to 1912bfa5bf6dSRolf Eike Beer * 19131da177e4SLinus Torvalds * Primitive swap readahead code. We simply read an aligned block of 19141da177e4SLinus Torvalds * (1 << page_cluster) entries in the swap area. This method is chosen 19151da177e4SLinus Torvalds * because it doesn't cost us any seek time. We also make sure to queue 19161da177e4SLinus Torvalds * the 'original' request together with the readahead ones... 19171da177e4SLinus Torvalds * 19181da177e4SLinus Torvalds * This has been extended to use the NUMA policies from the mm triggering 19191da177e4SLinus Torvalds * the readahead. 19201da177e4SLinus Torvalds * 19211da177e4SLinus Torvalds * Caller must hold down_read on the vma->vm_mm if vma is not NULL. 19221da177e4SLinus Torvalds */ 19231da177e4SLinus Torvalds void swapin_readahead(swp_entry_t entry, unsigned long addr,struct vm_area_struct *vma) 19241da177e4SLinus Torvalds { 19251da177e4SLinus Torvalds #ifdef CONFIG_NUMA 19261da177e4SLinus Torvalds struct vm_area_struct *next_vma = vma ? vma->vm_next : NULL; 19271da177e4SLinus Torvalds #endif 19281da177e4SLinus Torvalds int i, num; 19291da177e4SLinus Torvalds struct page *new_page; 19301da177e4SLinus Torvalds unsigned long offset; 19311da177e4SLinus Torvalds 19321da177e4SLinus Torvalds /* 19331da177e4SLinus Torvalds * Get the number of handles we should do readahead io to. 19341da177e4SLinus Torvalds */ 19351da177e4SLinus Torvalds num = valid_swaphandles(entry, &offset); 19361da177e4SLinus Torvalds for (i = 0; i < num; offset++, i++) { 19371da177e4SLinus Torvalds /* Ok, do the async read-ahead now */ 19381da177e4SLinus Torvalds new_page = read_swap_cache_async(swp_entry(swp_type(entry), 19391da177e4SLinus Torvalds offset), vma, addr); 19401da177e4SLinus Torvalds if (!new_page) 19411da177e4SLinus Torvalds break; 19421da177e4SLinus Torvalds page_cache_release(new_page); 19431da177e4SLinus Torvalds #ifdef CONFIG_NUMA 19441da177e4SLinus Torvalds /* 19451da177e4SLinus Torvalds * Find the next applicable VMA for the NUMA policy. 19461da177e4SLinus Torvalds */ 19471da177e4SLinus Torvalds addr += PAGE_SIZE; 19481da177e4SLinus Torvalds if (addr == 0) 19491da177e4SLinus Torvalds vma = NULL; 19501da177e4SLinus Torvalds if (vma) { 19511da177e4SLinus Torvalds if (addr >= vma->vm_end) { 19521da177e4SLinus Torvalds vma = next_vma; 19531da177e4SLinus Torvalds next_vma = vma ? vma->vm_next : NULL; 19541da177e4SLinus Torvalds } 19551da177e4SLinus Torvalds if (vma && addr < vma->vm_start) 19561da177e4SLinus Torvalds vma = NULL; 19571da177e4SLinus Torvalds } else { 19581da177e4SLinus Torvalds if (next_vma && addr >= next_vma->vm_start) { 19591da177e4SLinus Torvalds vma = next_vma; 19601da177e4SLinus Torvalds next_vma = vma->vm_next; 19611da177e4SLinus Torvalds } 19621da177e4SLinus Torvalds } 19631da177e4SLinus Torvalds #endif 19641da177e4SLinus Torvalds } 19651da177e4SLinus Torvalds lru_add_drain(); /* Push any new pages onto the LRU now */ 19661da177e4SLinus Torvalds } 19671da177e4SLinus Torvalds 19681da177e4SLinus Torvalds /* 19698f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 19708f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 19718f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 19721da177e4SLinus Torvalds */ 197365500d23SHugh Dickins static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma, 197465500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 197565500d23SHugh Dickins int write_access, pte_t orig_pte) 19761da177e4SLinus Torvalds { 19778f4e2101SHugh Dickins spinlock_t *ptl; 19781da177e4SLinus Torvalds struct page *page; 197965500d23SHugh Dickins swp_entry_t entry; 19801da177e4SLinus Torvalds pte_t pte; 19811da177e4SLinus Torvalds int ret = VM_FAULT_MINOR; 19821da177e4SLinus Torvalds 19834c21e2f2SHugh Dickins if (!pte_unmap_same(mm, pmd, page_table, orig_pte)) 19848f4e2101SHugh Dickins goto out; 198565500d23SHugh Dickins 198665500d23SHugh Dickins entry = pte_to_swp_entry(orig_pte); 19870697212aSChristoph Lameter if (is_migration_entry(entry)) { 19880697212aSChristoph Lameter migration_entry_wait(mm, pmd, address); 19890697212aSChristoph Lameter goto out; 19900697212aSChristoph Lameter } 19910ff92245SShailabh Nagar delayacct_set_flag(DELAYACCT_PF_SWAPIN); 19921da177e4SLinus Torvalds page = lookup_swap_cache(entry); 19931da177e4SLinus Torvalds if (!page) { 19941da177e4SLinus Torvalds swapin_readahead(entry, address, vma); 19951da177e4SLinus Torvalds page = read_swap_cache_async(entry, vma, address); 19961da177e4SLinus Torvalds if (!page) { 19971da177e4SLinus Torvalds /* 19988f4e2101SHugh Dickins * Back out if somebody else faulted in this pte 19998f4e2101SHugh Dickins * while we released the pte lock. 20001da177e4SLinus Torvalds */ 20018f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 20021da177e4SLinus Torvalds if (likely(pte_same(*page_table, orig_pte))) 20031da177e4SLinus Torvalds ret = VM_FAULT_OOM; 20040ff92245SShailabh Nagar delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 200565500d23SHugh Dickins goto unlock; 20061da177e4SLinus Torvalds } 20071da177e4SLinus Torvalds 20081da177e4SLinus Torvalds /* Had to read the page from swap area: Major fault */ 20091da177e4SLinus Torvalds ret = VM_FAULT_MAJOR; 2010f8891e5eSChristoph Lameter count_vm_event(PGMAJFAULT); 20111da177e4SLinus Torvalds grab_swap_token(); 20121da177e4SLinus Torvalds } 20131da177e4SLinus Torvalds 20140ff92245SShailabh Nagar delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 20151da177e4SLinus Torvalds mark_page_accessed(page); 20161da177e4SLinus Torvalds lock_page(page); 20171da177e4SLinus Torvalds 20181da177e4SLinus Torvalds /* 20198f4e2101SHugh Dickins * Back out if somebody else already faulted in this pte. 20201da177e4SLinus Torvalds */ 20218f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 20229e9bef07SHugh Dickins if (unlikely(!pte_same(*page_table, orig_pte))) 2023b8107480SKirill Korotaev goto out_nomap; 2024b8107480SKirill Korotaev 2025b8107480SKirill Korotaev if (unlikely(!PageUptodate(page))) { 2026b8107480SKirill Korotaev ret = VM_FAULT_SIGBUS; 2027b8107480SKirill Korotaev goto out_nomap; 20281da177e4SLinus Torvalds } 20291da177e4SLinus Torvalds 20301da177e4SLinus Torvalds /* The page isn't present yet, go ahead with the fault. */ 20311da177e4SLinus Torvalds 20324294621fSHugh Dickins inc_mm_counter(mm, anon_rss); 20331da177e4SLinus Torvalds pte = mk_pte(page, vma->vm_page_prot); 20341da177e4SLinus Torvalds if (write_access && can_share_swap_page(page)) { 20351da177e4SLinus Torvalds pte = maybe_mkwrite(pte_mkdirty(pte), vma); 20361da177e4SLinus Torvalds write_access = 0; 20371da177e4SLinus Torvalds } 20381da177e4SLinus Torvalds 20391da177e4SLinus Torvalds flush_icache_page(vma, page); 20401da177e4SLinus Torvalds set_pte_at(mm, address, page_table, pte); 20411da177e4SLinus Torvalds page_add_anon_rmap(page, vma, address); 20421da177e4SLinus Torvalds 2043c475a8abSHugh Dickins swap_free(entry); 2044c475a8abSHugh Dickins if (vm_swap_full()) 2045c475a8abSHugh Dickins remove_exclusive_swap_page(page); 2046c475a8abSHugh Dickins unlock_page(page); 2047c475a8abSHugh Dickins 20481da177e4SLinus Torvalds if (write_access) { 20491da177e4SLinus Torvalds if (do_wp_page(mm, vma, address, 20508f4e2101SHugh Dickins page_table, pmd, ptl, pte) == VM_FAULT_OOM) 20511da177e4SLinus Torvalds ret = VM_FAULT_OOM; 20521da177e4SLinus Torvalds goto out; 20531da177e4SLinus Torvalds } 20541da177e4SLinus Torvalds 20551da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 20561da177e4SLinus Torvalds update_mmu_cache(vma, address, pte); 20571da177e4SLinus Torvalds lazy_mmu_prot_update(pte); 205865500d23SHugh Dickins unlock: 20598f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 20601da177e4SLinus Torvalds out: 20611da177e4SLinus Torvalds return ret; 2062b8107480SKirill Korotaev out_nomap: 20638f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 2064b8107480SKirill Korotaev unlock_page(page); 2065b8107480SKirill Korotaev page_cache_release(page); 206665500d23SHugh Dickins return ret; 20671da177e4SLinus Torvalds } 20681da177e4SLinus Torvalds 20691da177e4SLinus Torvalds /* 20708f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 20718f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 20728f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 20731da177e4SLinus Torvalds */ 207465500d23SHugh Dickins static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma, 207565500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 207665500d23SHugh Dickins int write_access) 20771da177e4SLinus Torvalds { 20788f4e2101SHugh Dickins struct page *page; 20798f4e2101SHugh Dickins spinlock_t *ptl; 20801da177e4SLinus Torvalds pte_t entry; 20811da177e4SLinus Torvalds 20826aab341eSLinus Torvalds if (write_access) { 20831da177e4SLinus Torvalds /* Allocate our own private page. */ 20841da177e4SLinus Torvalds pte_unmap(page_table); 20851da177e4SLinus Torvalds 20861da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 208765500d23SHugh Dickins goto oom; 208865500d23SHugh Dickins page = alloc_zeroed_user_highpage(vma, address); 20891da177e4SLinus Torvalds if (!page) 209065500d23SHugh Dickins goto oom; 20911da177e4SLinus Torvalds 209265500d23SHugh Dickins entry = mk_pte(page, vma->vm_page_prot); 209365500d23SHugh Dickins entry = maybe_mkwrite(pte_mkdirty(entry), vma); 20948f4e2101SHugh Dickins 20958f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 20968f4e2101SHugh Dickins if (!pte_none(*page_table)) 20978f4e2101SHugh Dickins goto release; 20988f4e2101SHugh Dickins inc_mm_counter(mm, anon_rss); 20991da177e4SLinus Torvalds lru_cache_add_active(page); 21009617d95eSNick Piggin page_add_new_anon_rmap(page, vma, address); 2101b5810039SNick Piggin } else { 21028f4e2101SHugh Dickins /* Map the ZERO_PAGE - vm_page_prot is readonly */ 21038f4e2101SHugh Dickins page = ZERO_PAGE(address); 21048f4e2101SHugh Dickins page_cache_get(page); 21058f4e2101SHugh Dickins entry = mk_pte(page, vma->vm_page_prot); 21068f4e2101SHugh Dickins 21074c21e2f2SHugh Dickins ptl = pte_lockptr(mm, pmd); 21088f4e2101SHugh Dickins spin_lock(ptl); 21098f4e2101SHugh Dickins if (!pte_none(*page_table)) 21108f4e2101SHugh Dickins goto release; 2111b5810039SNick Piggin inc_mm_counter(mm, file_rss); 2112b5810039SNick Piggin page_add_file_rmap(page); 21131da177e4SLinus Torvalds } 21141da177e4SLinus Torvalds 211565500d23SHugh Dickins set_pte_at(mm, address, page_table, entry); 21161da177e4SLinus Torvalds 21171da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 211865500d23SHugh Dickins update_mmu_cache(vma, address, entry); 21191da177e4SLinus Torvalds lazy_mmu_prot_update(entry); 212065500d23SHugh Dickins unlock: 21218f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 21221da177e4SLinus Torvalds return VM_FAULT_MINOR; 21238f4e2101SHugh Dickins release: 21248f4e2101SHugh Dickins page_cache_release(page); 21258f4e2101SHugh Dickins goto unlock; 212665500d23SHugh Dickins oom: 21271da177e4SLinus Torvalds return VM_FAULT_OOM; 21281da177e4SLinus Torvalds } 21291da177e4SLinus Torvalds 21301da177e4SLinus Torvalds /* 21311da177e4SLinus Torvalds * do_no_page() tries to create a new page mapping. It aggressively 21321da177e4SLinus Torvalds * tries to share with existing pages, but makes a separate copy if 21331da177e4SLinus Torvalds * the "write_access" parameter is true in order to avoid the next 21341da177e4SLinus Torvalds * page fault. 21351da177e4SLinus Torvalds * 21361da177e4SLinus Torvalds * As this is called only for pages that do not currently exist, we 21371da177e4SLinus Torvalds * do not need to flush old virtual caches or the TLB. 21381da177e4SLinus Torvalds * 21398f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 21408f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 21418f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 21421da177e4SLinus Torvalds */ 214365500d23SHugh Dickins static int do_no_page(struct mm_struct *mm, struct vm_area_struct *vma, 214465500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 214565500d23SHugh Dickins int write_access) 21461da177e4SLinus Torvalds { 21478f4e2101SHugh Dickins spinlock_t *ptl; 21481da177e4SLinus Torvalds struct page *new_page; 21491da177e4SLinus Torvalds struct address_space *mapping = NULL; 21501da177e4SLinus Torvalds pte_t entry; 21511da177e4SLinus Torvalds unsigned int sequence = 0; 21521da177e4SLinus Torvalds int ret = VM_FAULT_MINOR; 21531da177e4SLinus Torvalds int anon = 0; 2154d08b3851SPeter Zijlstra struct page *dirty_page = NULL; 21551da177e4SLinus Torvalds 21561da177e4SLinus Torvalds pte_unmap(page_table); 2157325f04dbSHugh Dickins BUG_ON(vma->vm_flags & VM_PFNMAP); 2158325f04dbSHugh Dickins 21591da177e4SLinus Torvalds if (vma->vm_file) { 21601da177e4SLinus Torvalds mapping = vma->vm_file->f_mapping; 21611da177e4SLinus Torvalds sequence = mapping->truncate_count; 21621da177e4SLinus Torvalds smp_rmb(); /* serializes i_size against truncate_count */ 21631da177e4SLinus Torvalds } 21641da177e4SLinus Torvalds retry: 21651da177e4SLinus Torvalds new_page = vma->vm_ops->nopage(vma, address & PAGE_MASK, &ret); 21661da177e4SLinus Torvalds /* 21671da177e4SLinus Torvalds * No smp_rmb is needed here as long as there's a full 21681da177e4SLinus Torvalds * spin_lock/unlock sequence inside the ->nopage callback 21691da177e4SLinus Torvalds * (for the pagecache lookup) that acts as an implicit 21701da177e4SLinus Torvalds * smp_mb() and prevents the i_size read to happen 21711da177e4SLinus Torvalds * after the next truncate_count read. 21721da177e4SLinus Torvalds */ 21731da177e4SLinus Torvalds 21747f7bbbe5SBenjamin Herrenschmidt /* no page was available -- either SIGBUS, OOM or REFAULT */ 21757f7bbbe5SBenjamin Herrenschmidt if (unlikely(new_page == NOPAGE_SIGBUS)) 21761da177e4SLinus Torvalds return VM_FAULT_SIGBUS; 21777f7bbbe5SBenjamin Herrenschmidt else if (unlikely(new_page == NOPAGE_OOM)) 21781da177e4SLinus Torvalds return VM_FAULT_OOM; 21797f7bbbe5SBenjamin Herrenschmidt else if (unlikely(new_page == NOPAGE_REFAULT)) 21807f7bbbe5SBenjamin Herrenschmidt return VM_FAULT_MINOR; 21811da177e4SLinus Torvalds 21821da177e4SLinus Torvalds /* 21831da177e4SLinus Torvalds * Should we do an early C-O-W break? 21841da177e4SLinus Torvalds */ 21859637a5efSDavid Howells if (write_access) { 21869637a5efSDavid Howells if (!(vma->vm_flags & VM_SHARED)) { 21871da177e4SLinus Torvalds struct page *page; 21881da177e4SLinus Torvalds 21891da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 21901da177e4SLinus Torvalds goto oom; 21911da177e4SLinus Torvalds page = alloc_page_vma(GFP_HIGHUSER, vma, address); 21921da177e4SLinus Torvalds if (!page) 21931da177e4SLinus Torvalds goto oom; 2194325f04dbSHugh Dickins copy_user_highpage(page, new_page, address); 21951da177e4SLinus Torvalds page_cache_release(new_page); 21961da177e4SLinus Torvalds new_page = page; 21971da177e4SLinus Torvalds anon = 1; 21989637a5efSDavid Howells 21999637a5efSDavid Howells } else { 22009637a5efSDavid Howells /* if the page will be shareable, see if the backing 22019637a5efSDavid Howells * address space wants to know that the page is about 22029637a5efSDavid Howells * to become writable */ 22039637a5efSDavid Howells if (vma->vm_ops->page_mkwrite && 22049637a5efSDavid Howells vma->vm_ops->page_mkwrite(vma, new_page) < 0 22059637a5efSDavid Howells ) { 22069637a5efSDavid Howells page_cache_release(new_page); 22079637a5efSDavid Howells return VM_FAULT_SIGBUS; 22089637a5efSDavid Howells } 22099637a5efSDavid Howells } 22101da177e4SLinus Torvalds } 22111da177e4SLinus Torvalds 22128f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 22131da177e4SLinus Torvalds /* 22141da177e4SLinus Torvalds * For a file-backed vma, someone could have truncated or otherwise 22151da177e4SLinus Torvalds * invalidated this page. If unmap_mapping_range got called, 22161da177e4SLinus Torvalds * retry getting the page. 22171da177e4SLinus Torvalds */ 22181da177e4SLinus Torvalds if (mapping && unlikely(sequence != mapping->truncate_count)) { 22198f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 22201da177e4SLinus Torvalds page_cache_release(new_page); 222165500d23SHugh Dickins cond_resched(); 222265500d23SHugh Dickins sequence = mapping->truncate_count; 222365500d23SHugh Dickins smp_rmb(); 22241da177e4SLinus Torvalds goto retry; 22251da177e4SLinus Torvalds } 22261da177e4SLinus Torvalds 22271da177e4SLinus Torvalds /* 22281da177e4SLinus Torvalds * This silly early PAGE_DIRTY setting removes a race 22291da177e4SLinus Torvalds * due to the bad i386 page protection. But it's valid 22301da177e4SLinus Torvalds * for other architectures too. 22311da177e4SLinus Torvalds * 22321da177e4SLinus Torvalds * Note that if write_access is true, we either now have 22331da177e4SLinus Torvalds * an exclusive copy of the page, or this is a shared mapping, 22341da177e4SLinus Torvalds * so we can make it writable and dirty to avoid having to 22351da177e4SLinus Torvalds * handle that later. 22361da177e4SLinus Torvalds */ 22371da177e4SLinus Torvalds /* Only go through if we didn't race with anybody else... */ 22381da177e4SLinus Torvalds if (pte_none(*page_table)) { 22391da177e4SLinus Torvalds flush_icache_page(vma, new_page); 22401da177e4SLinus Torvalds entry = mk_pte(new_page, vma->vm_page_prot); 22411da177e4SLinus Torvalds if (write_access) 22421da177e4SLinus Torvalds entry = maybe_mkwrite(pte_mkdirty(entry), vma); 22431da177e4SLinus Torvalds set_pte_at(mm, address, page_table, entry); 22441da177e4SLinus Torvalds if (anon) { 22454294621fSHugh Dickins inc_mm_counter(mm, anon_rss); 22461da177e4SLinus Torvalds lru_cache_add_active(new_page); 22479617d95eSNick Piggin page_add_new_anon_rmap(new_page, vma, address); 2248f57e88a8SHugh Dickins } else { 22494294621fSHugh Dickins inc_mm_counter(mm, file_rss); 22501da177e4SLinus Torvalds page_add_file_rmap(new_page); 2251d08b3851SPeter Zijlstra if (write_access) { 2252d08b3851SPeter Zijlstra dirty_page = new_page; 2253d08b3851SPeter Zijlstra get_page(dirty_page); 2254d08b3851SPeter Zijlstra } 22554294621fSHugh Dickins } 22561da177e4SLinus Torvalds } else { 22571da177e4SLinus Torvalds /* One of our sibling threads was faster, back out. */ 22581da177e4SLinus Torvalds page_cache_release(new_page); 225965500d23SHugh Dickins goto unlock; 22601da177e4SLinus Torvalds } 22611da177e4SLinus Torvalds 22621da177e4SLinus Torvalds /* no need to invalidate: a not-present page shouldn't be cached */ 22631da177e4SLinus Torvalds update_mmu_cache(vma, address, entry); 22641da177e4SLinus Torvalds lazy_mmu_prot_update(entry); 226565500d23SHugh Dickins unlock: 22668f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 2267d08b3851SPeter Zijlstra if (dirty_page) { 2268edc79b2aSPeter Zijlstra set_page_dirty_balance(dirty_page); 2269d08b3851SPeter Zijlstra put_page(dirty_page); 2270d08b3851SPeter Zijlstra } 22711da177e4SLinus Torvalds return ret; 22721da177e4SLinus Torvalds oom: 22731da177e4SLinus Torvalds page_cache_release(new_page); 227465500d23SHugh Dickins return VM_FAULT_OOM; 22751da177e4SLinus Torvalds } 22761da177e4SLinus Torvalds 22771da177e4SLinus Torvalds /* 2278f4b81804SJes Sorensen * do_no_pfn() tries to create a new page mapping for a page without 2279f4b81804SJes Sorensen * a struct_page backing it 2280f4b81804SJes Sorensen * 2281f4b81804SJes Sorensen * As this is called only for pages that do not currently exist, we 2282f4b81804SJes Sorensen * do not need to flush old virtual caches or the TLB. 2283f4b81804SJes Sorensen * 2284f4b81804SJes Sorensen * We enter with non-exclusive mmap_sem (to exclude vma changes, 2285f4b81804SJes Sorensen * but allow concurrent faults), and pte mapped but not yet locked. 2286f4b81804SJes Sorensen * We return with mmap_sem still held, but pte unmapped and unlocked. 2287f4b81804SJes Sorensen * 2288f4b81804SJes Sorensen * It is expected that the ->nopfn handler always returns the same pfn 2289f4b81804SJes Sorensen * for a given virtual mapping. 2290f4b81804SJes Sorensen * 2291f4b81804SJes Sorensen * Mark this `noinline' to prevent it from bloating the main pagefault code. 2292f4b81804SJes Sorensen */ 2293f4b81804SJes Sorensen static noinline int do_no_pfn(struct mm_struct *mm, struct vm_area_struct *vma, 2294f4b81804SJes Sorensen unsigned long address, pte_t *page_table, pmd_t *pmd, 2295f4b81804SJes Sorensen int write_access) 2296f4b81804SJes Sorensen { 2297f4b81804SJes Sorensen spinlock_t *ptl; 2298f4b81804SJes Sorensen pte_t entry; 2299f4b81804SJes Sorensen unsigned long pfn; 2300f4b81804SJes Sorensen int ret = VM_FAULT_MINOR; 2301f4b81804SJes Sorensen 2302f4b81804SJes Sorensen pte_unmap(page_table); 2303f4b81804SJes Sorensen BUG_ON(!(vma->vm_flags & VM_PFNMAP)); 2304f4b81804SJes Sorensen BUG_ON(is_cow_mapping(vma->vm_flags)); 2305f4b81804SJes Sorensen 2306f4b81804SJes Sorensen pfn = vma->vm_ops->nopfn(vma, address & PAGE_MASK); 2307f4b81804SJes Sorensen if (pfn == NOPFN_OOM) 2308f4b81804SJes Sorensen return VM_FAULT_OOM; 2309f4b81804SJes Sorensen if (pfn == NOPFN_SIGBUS) 2310f4b81804SJes Sorensen return VM_FAULT_SIGBUS; 2311f4b81804SJes Sorensen 2312f4b81804SJes Sorensen page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 2313f4b81804SJes Sorensen 2314f4b81804SJes Sorensen /* Only go through if we didn't race with anybody else... */ 2315f4b81804SJes Sorensen if (pte_none(*page_table)) { 2316f4b81804SJes Sorensen entry = pfn_pte(pfn, vma->vm_page_prot); 2317f4b81804SJes Sorensen if (write_access) 2318f4b81804SJes Sorensen entry = maybe_mkwrite(pte_mkdirty(entry), vma); 2319f4b81804SJes Sorensen set_pte_at(mm, address, page_table, entry); 2320f4b81804SJes Sorensen } 2321f4b81804SJes Sorensen pte_unmap_unlock(page_table, ptl); 2322f4b81804SJes Sorensen return ret; 2323f4b81804SJes Sorensen } 2324f4b81804SJes Sorensen 2325f4b81804SJes Sorensen /* 23261da177e4SLinus Torvalds * Fault of a previously existing named mapping. Repopulate the pte 23271da177e4SLinus Torvalds * from the encoded file_pte if possible. This enables swappable 23281da177e4SLinus Torvalds * nonlinear vmas. 23298f4e2101SHugh Dickins * 23308f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 23318f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 23328f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 23331da177e4SLinus Torvalds */ 23341da177e4SLinus Torvalds static int do_file_page(struct mm_struct *mm, struct vm_area_struct *vma, 233565500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 233665500d23SHugh Dickins int write_access, pte_t orig_pte) 23371da177e4SLinus Torvalds { 233865500d23SHugh Dickins pgoff_t pgoff; 23391da177e4SLinus Torvalds int err; 23401da177e4SLinus Torvalds 23414c21e2f2SHugh Dickins if (!pte_unmap_same(mm, pmd, page_table, orig_pte)) 23428f4e2101SHugh Dickins return VM_FAULT_MINOR; 23431da177e4SLinus Torvalds 234465500d23SHugh Dickins if (unlikely(!(vma->vm_flags & VM_NONLINEAR))) { 234565500d23SHugh Dickins /* 234665500d23SHugh Dickins * Page table corrupted: show pte and kill process. 234765500d23SHugh Dickins */ 2348b5810039SNick Piggin print_bad_pte(vma, orig_pte, address); 234965500d23SHugh Dickins return VM_FAULT_OOM; 235065500d23SHugh Dickins } 235165500d23SHugh Dickins /* We can then assume vm->vm_ops && vma->vm_ops->populate */ 235265500d23SHugh Dickins 235365500d23SHugh Dickins pgoff = pte_to_pgoff(orig_pte); 235465500d23SHugh Dickins err = vma->vm_ops->populate(vma, address & PAGE_MASK, PAGE_SIZE, 235565500d23SHugh Dickins vma->vm_page_prot, pgoff, 0); 23561da177e4SLinus Torvalds if (err == -ENOMEM) 23571da177e4SLinus Torvalds return VM_FAULT_OOM; 23581da177e4SLinus Torvalds if (err) 23591da177e4SLinus Torvalds return VM_FAULT_SIGBUS; 23601da177e4SLinus Torvalds return VM_FAULT_MAJOR; 23611da177e4SLinus Torvalds } 23621da177e4SLinus Torvalds 23631da177e4SLinus Torvalds /* 23641da177e4SLinus Torvalds * These routines also need to handle stuff like marking pages dirty 23651da177e4SLinus Torvalds * and/or accessed for architectures that don't do it in hardware (most 23661da177e4SLinus Torvalds * RISC architectures). The early dirtying is also good on the i386. 23671da177e4SLinus Torvalds * 23681da177e4SLinus Torvalds * There is also a hook called "update_mmu_cache()" that architectures 23691da177e4SLinus Torvalds * with external mmu caches can use to update those (ie the Sparc or 23701da177e4SLinus Torvalds * PowerPC hashed page tables that act as extended TLBs). 23711da177e4SLinus Torvalds * 2372c74df32cSHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 2373c74df32cSHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 2374c74df32cSHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 23751da177e4SLinus Torvalds */ 23761da177e4SLinus Torvalds static inline int handle_pte_fault(struct mm_struct *mm, 23771da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long address, 237865500d23SHugh Dickins pte_t *pte, pmd_t *pmd, int write_access) 23791da177e4SLinus Torvalds { 23801da177e4SLinus Torvalds pte_t entry; 23811a44e149SAndrea Arcangeli pte_t old_entry; 23828f4e2101SHugh Dickins spinlock_t *ptl; 23831da177e4SLinus Torvalds 23841a44e149SAndrea Arcangeli old_entry = entry = *pte; 23851da177e4SLinus Torvalds if (!pte_present(entry)) { 238665500d23SHugh Dickins if (pte_none(entry)) { 2387f4b81804SJes Sorensen if (vma->vm_ops) { 2388f4b81804SJes Sorensen if (vma->vm_ops->nopage) 238965500d23SHugh Dickins return do_no_page(mm, vma, address, 2390f4b81804SJes Sorensen pte, pmd, 2391f4b81804SJes Sorensen write_access); 2392f4b81804SJes Sorensen if (unlikely(vma->vm_ops->nopfn)) 2393f4b81804SJes Sorensen return do_no_pfn(mm, vma, address, pte, 2394f4b81804SJes Sorensen pmd, write_access); 2395f4b81804SJes Sorensen } 2396f4b81804SJes Sorensen return do_anonymous_page(mm, vma, address, 239765500d23SHugh Dickins pte, pmd, write_access); 239865500d23SHugh Dickins } 23991da177e4SLinus Torvalds if (pte_file(entry)) 240065500d23SHugh Dickins return do_file_page(mm, vma, address, 240165500d23SHugh Dickins pte, pmd, write_access, entry); 240265500d23SHugh Dickins return do_swap_page(mm, vma, address, 240365500d23SHugh Dickins pte, pmd, write_access, entry); 24041da177e4SLinus Torvalds } 24051da177e4SLinus Torvalds 24064c21e2f2SHugh Dickins ptl = pte_lockptr(mm, pmd); 24078f4e2101SHugh Dickins spin_lock(ptl); 24088f4e2101SHugh Dickins if (unlikely(!pte_same(*pte, entry))) 24098f4e2101SHugh Dickins goto unlock; 24101da177e4SLinus Torvalds if (write_access) { 24111da177e4SLinus Torvalds if (!pte_write(entry)) 24128f4e2101SHugh Dickins return do_wp_page(mm, vma, address, 24138f4e2101SHugh Dickins pte, pmd, ptl, entry); 24141da177e4SLinus Torvalds entry = pte_mkdirty(entry); 24151da177e4SLinus Torvalds } 24161da177e4SLinus Torvalds entry = pte_mkyoung(entry); 24171a44e149SAndrea Arcangeli if (!pte_same(old_entry, entry)) { 24181da177e4SLinus Torvalds ptep_set_access_flags(vma, address, pte, entry, write_access); 24191da177e4SLinus Torvalds update_mmu_cache(vma, address, entry); 24201da177e4SLinus Torvalds lazy_mmu_prot_update(entry); 24211a44e149SAndrea Arcangeli } else { 24221a44e149SAndrea Arcangeli /* 24231a44e149SAndrea Arcangeli * This is needed only for protection faults but the arch code 24241a44e149SAndrea Arcangeli * is not yet telling us if this is a protection fault or not. 24251a44e149SAndrea Arcangeli * This still avoids useless tlb flushes for .text page faults 24261a44e149SAndrea Arcangeli * with threads. 24271a44e149SAndrea Arcangeli */ 24281a44e149SAndrea Arcangeli if (write_access) 24291a44e149SAndrea Arcangeli flush_tlb_page(vma, address); 24301a44e149SAndrea Arcangeli } 24318f4e2101SHugh Dickins unlock: 24328f4e2101SHugh Dickins pte_unmap_unlock(pte, ptl); 24331da177e4SLinus Torvalds return VM_FAULT_MINOR; 24341da177e4SLinus Torvalds } 24351da177e4SLinus Torvalds 24361da177e4SLinus Torvalds /* 24371da177e4SLinus Torvalds * By the time we get here, we already hold the mm semaphore 24381da177e4SLinus Torvalds */ 2439f33ea7f4SNick Piggin int __handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma, 24401da177e4SLinus Torvalds unsigned long address, int write_access) 24411da177e4SLinus Torvalds { 24421da177e4SLinus Torvalds pgd_t *pgd; 24431da177e4SLinus Torvalds pud_t *pud; 24441da177e4SLinus Torvalds pmd_t *pmd; 24451da177e4SLinus Torvalds pte_t *pte; 24461da177e4SLinus Torvalds 24471da177e4SLinus Torvalds __set_current_state(TASK_RUNNING); 24481da177e4SLinus Torvalds 2449f8891e5eSChristoph Lameter count_vm_event(PGFAULT); 24501da177e4SLinus Torvalds 2451ac9b9c66SHugh Dickins if (unlikely(is_vm_hugetlb_page(vma))) 2452ac9b9c66SHugh Dickins return hugetlb_fault(mm, vma, address, write_access); 24531da177e4SLinus Torvalds 24541da177e4SLinus Torvalds pgd = pgd_offset(mm, address); 24551da177e4SLinus Torvalds pud = pud_alloc(mm, pgd, address); 24561da177e4SLinus Torvalds if (!pud) 2457c74df32cSHugh Dickins return VM_FAULT_OOM; 24581da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, address); 24591da177e4SLinus Torvalds if (!pmd) 2460c74df32cSHugh Dickins return VM_FAULT_OOM; 24611da177e4SLinus Torvalds pte = pte_alloc_map(mm, pmd, address); 24621da177e4SLinus Torvalds if (!pte) 2463c74df32cSHugh Dickins return VM_FAULT_OOM; 24641da177e4SLinus Torvalds 246565500d23SHugh Dickins return handle_pte_fault(mm, vma, address, pte, pmd, write_access); 24661da177e4SLinus Torvalds } 24671da177e4SLinus Torvalds 246867207b96SArnd Bergmann EXPORT_SYMBOL_GPL(__handle_mm_fault); 246967207b96SArnd Bergmann 24701da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED 24711da177e4SLinus Torvalds /* 24721da177e4SLinus Torvalds * Allocate page upper directory. 2473872fec16SHugh Dickins * We've already handled the fast-path in-line. 24741da177e4SLinus Torvalds */ 24751bb3630eSHugh Dickins int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address) 24761da177e4SLinus Torvalds { 2477c74df32cSHugh Dickins pud_t *new = pud_alloc_one(mm, address); 2478c74df32cSHugh Dickins if (!new) 24791bb3630eSHugh Dickins return -ENOMEM; 24801da177e4SLinus Torvalds 2481872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 24821bb3630eSHugh Dickins if (pgd_present(*pgd)) /* Another has populated it */ 24831da177e4SLinus Torvalds pud_free(new); 24841bb3630eSHugh Dickins else 24851da177e4SLinus Torvalds pgd_populate(mm, pgd, new); 2486872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 24871bb3630eSHugh Dickins return 0; 24881da177e4SLinus Torvalds } 2489e0f39591SAlan Stern #else 2490e0f39591SAlan Stern /* Workaround for gcc 2.96 */ 2491e0f39591SAlan Stern int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address) 2492e0f39591SAlan Stern { 2493e0f39591SAlan Stern return 0; 2494e0f39591SAlan Stern } 24951da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */ 24961da177e4SLinus Torvalds 24971da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED 24981da177e4SLinus Torvalds /* 24991da177e4SLinus Torvalds * Allocate page middle directory. 2500872fec16SHugh Dickins * We've already handled the fast-path in-line. 25011da177e4SLinus Torvalds */ 25021bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) 25031da177e4SLinus Torvalds { 2504c74df32cSHugh Dickins pmd_t *new = pmd_alloc_one(mm, address); 2505c74df32cSHugh Dickins if (!new) 25061bb3630eSHugh Dickins return -ENOMEM; 25071da177e4SLinus Torvalds 2508872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 25091da177e4SLinus Torvalds #ifndef __ARCH_HAS_4LEVEL_HACK 25101bb3630eSHugh Dickins if (pud_present(*pud)) /* Another has populated it */ 25111da177e4SLinus Torvalds pmd_free(new); 25121bb3630eSHugh Dickins else 25131da177e4SLinus Torvalds pud_populate(mm, pud, new); 25141da177e4SLinus Torvalds #else 25151bb3630eSHugh Dickins if (pgd_present(*pud)) /* Another has populated it */ 25161da177e4SLinus Torvalds pmd_free(new); 25171bb3630eSHugh Dickins else 25181da177e4SLinus Torvalds pgd_populate(mm, pud, new); 25191da177e4SLinus Torvalds #endif /* __ARCH_HAS_4LEVEL_HACK */ 2520872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 25211bb3630eSHugh Dickins return 0; 25221da177e4SLinus Torvalds } 2523e0f39591SAlan Stern #else 2524e0f39591SAlan Stern /* Workaround for gcc 2.96 */ 2525e0f39591SAlan Stern int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) 2526e0f39591SAlan Stern { 2527e0f39591SAlan Stern return 0; 2528e0f39591SAlan Stern } 25291da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */ 25301da177e4SLinus Torvalds 25311da177e4SLinus Torvalds int make_pages_present(unsigned long addr, unsigned long end) 25321da177e4SLinus Torvalds { 25331da177e4SLinus Torvalds int ret, len, write; 25341da177e4SLinus Torvalds struct vm_area_struct * vma; 25351da177e4SLinus Torvalds 25361da177e4SLinus Torvalds vma = find_vma(current->mm, addr); 25371da177e4SLinus Torvalds if (!vma) 25381da177e4SLinus Torvalds return -1; 25391da177e4SLinus Torvalds write = (vma->vm_flags & VM_WRITE) != 0; 25405bcb28b1SEric Sesterhenn BUG_ON(addr >= end); 25415bcb28b1SEric Sesterhenn BUG_ON(end > vma->vm_end); 25421da177e4SLinus Torvalds len = (end+PAGE_SIZE-1)/PAGE_SIZE-addr/PAGE_SIZE; 25431da177e4SLinus Torvalds ret = get_user_pages(current, current->mm, addr, 25441da177e4SLinus Torvalds len, write, 0, NULL, NULL); 25451da177e4SLinus Torvalds if (ret < 0) 25461da177e4SLinus Torvalds return ret; 25471da177e4SLinus Torvalds return ret == len ? 0 : -1; 25481da177e4SLinus Torvalds } 25491da177e4SLinus Torvalds 25501da177e4SLinus Torvalds /* 25511da177e4SLinus Torvalds * Map a vmalloc()-space virtual address to the physical page. 25521da177e4SLinus Torvalds */ 25531da177e4SLinus Torvalds struct page * vmalloc_to_page(void * vmalloc_addr) 25541da177e4SLinus Torvalds { 25551da177e4SLinus Torvalds unsigned long addr = (unsigned long) vmalloc_addr; 25561da177e4SLinus Torvalds struct page *page = NULL; 25571da177e4SLinus Torvalds pgd_t *pgd = pgd_offset_k(addr); 25581da177e4SLinus Torvalds pud_t *pud; 25591da177e4SLinus Torvalds pmd_t *pmd; 25601da177e4SLinus Torvalds pte_t *ptep, pte; 25611da177e4SLinus Torvalds 25621da177e4SLinus Torvalds if (!pgd_none(*pgd)) { 25631da177e4SLinus Torvalds pud = pud_offset(pgd, addr); 25641da177e4SLinus Torvalds if (!pud_none(*pud)) { 25651da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 25661da177e4SLinus Torvalds if (!pmd_none(*pmd)) { 25671da177e4SLinus Torvalds ptep = pte_offset_map(pmd, addr); 25681da177e4SLinus Torvalds pte = *ptep; 25691da177e4SLinus Torvalds if (pte_present(pte)) 25701da177e4SLinus Torvalds page = pte_page(pte); 25711da177e4SLinus Torvalds pte_unmap(ptep); 25721da177e4SLinus Torvalds } 25731da177e4SLinus Torvalds } 25741da177e4SLinus Torvalds } 25751da177e4SLinus Torvalds return page; 25761da177e4SLinus Torvalds } 25771da177e4SLinus Torvalds 25781da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc_to_page); 25791da177e4SLinus Torvalds 25801da177e4SLinus Torvalds /* 25811da177e4SLinus Torvalds * Map a vmalloc()-space virtual address to the physical page frame number. 25821da177e4SLinus Torvalds */ 25831da177e4SLinus Torvalds unsigned long vmalloc_to_pfn(void * vmalloc_addr) 25841da177e4SLinus Torvalds { 25851da177e4SLinus Torvalds return page_to_pfn(vmalloc_to_page(vmalloc_addr)); 25861da177e4SLinus Torvalds } 25871da177e4SLinus Torvalds 25881da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc_to_pfn); 25891da177e4SLinus Torvalds 25901da177e4SLinus Torvalds #if !defined(__HAVE_ARCH_GATE_AREA) 25911da177e4SLinus Torvalds 25921da177e4SLinus Torvalds #if defined(AT_SYSINFO_EHDR) 25935ce7852cSAdrian Bunk static struct vm_area_struct gate_vma; 25941da177e4SLinus Torvalds 25951da177e4SLinus Torvalds static int __init gate_vma_init(void) 25961da177e4SLinus Torvalds { 25971da177e4SLinus Torvalds gate_vma.vm_mm = NULL; 25981da177e4SLinus Torvalds gate_vma.vm_start = FIXADDR_USER_START; 25991da177e4SLinus Torvalds gate_vma.vm_end = FIXADDR_USER_END; 26001da177e4SLinus Torvalds gate_vma.vm_page_prot = PAGE_READONLY; 26010b14c179SHugh Dickins gate_vma.vm_flags = 0; 26021da177e4SLinus Torvalds return 0; 26031da177e4SLinus Torvalds } 26041da177e4SLinus Torvalds __initcall(gate_vma_init); 26051da177e4SLinus Torvalds #endif 26061da177e4SLinus Torvalds 26071da177e4SLinus Torvalds struct vm_area_struct *get_gate_vma(struct task_struct *tsk) 26081da177e4SLinus Torvalds { 26091da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR 26101da177e4SLinus Torvalds return &gate_vma; 26111da177e4SLinus Torvalds #else 26121da177e4SLinus Torvalds return NULL; 26131da177e4SLinus Torvalds #endif 26141da177e4SLinus Torvalds } 26151da177e4SLinus Torvalds 26161da177e4SLinus Torvalds int in_gate_area_no_task(unsigned long addr) 26171da177e4SLinus Torvalds { 26181da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR 26191da177e4SLinus Torvalds if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END)) 26201da177e4SLinus Torvalds return 1; 26211da177e4SLinus Torvalds #endif 26221da177e4SLinus Torvalds return 0; 26231da177e4SLinus Torvalds } 26241da177e4SLinus Torvalds 26251da177e4SLinus Torvalds #endif /* __HAVE_ARCH_GATE_AREA */ 26260ec76a11SDavid Howells 26270ec76a11SDavid Howells /* 26280ec76a11SDavid Howells * Access another process' address space. 26290ec76a11SDavid Howells * Source/target buffer must be kernel space, 26300ec76a11SDavid Howells * Do not walk the page table directly, use get_user_pages 26310ec76a11SDavid Howells */ 26320ec76a11SDavid Howells int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write) 26330ec76a11SDavid Howells { 26340ec76a11SDavid Howells struct mm_struct *mm; 26350ec76a11SDavid Howells struct vm_area_struct *vma; 26360ec76a11SDavid Howells struct page *page; 26370ec76a11SDavid Howells void *old_buf = buf; 26380ec76a11SDavid Howells 26390ec76a11SDavid Howells mm = get_task_mm(tsk); 26400ec76a11SDavid Howells if (!mm) 26410ec76a11SDavid Howells return 0; 26420ec76a11SDavid Howells 26430ec76a11SDavid Howells down_read(&mm->mmap_sem); 26440ec76a11SDavid Howells /* ignore errors, just check how much was sucessfully transfered */ 26450ec76a11SDavid Howells while (len) { 26460ec76a11SDavid Howells int bytes, ret, offset; 26470ec76a11SDavid Howells void *maddr; 26480ec76a11SDavid Howells 26490ec76a11SDavid Howells ret = get_user_pages(tsk, mm, addr, 1, 26500ec76a11SDavid Howells write, 1, &page, &vma); 26510ec76a11SDavid Howells if (ret <= 0) 26520ec76a11SDavid Howells break; 26530ec76a11SDavid Howells 26540ec76a11SDavid Howells bytes = len; 26550ec76a11SDavid Howells offset = addr & (PAGE_SIZE-1); 26560ec76a11SDavid Howells if (bytes > PAGE_SIZE-offset) 26570ec76a11SDavid Howells bytes = PAGE_SIZE-offset; 26580ec76a11SDavid Howells 26590ec76a11SDavid Howells maddr = kmap(page); 26600ec76a11SDavid Howells if (write) { 26610ec76a11SDavid Howells copy_to_user_page(vma, page, addr, 26620ec76a11SDavid Howells maddr + offset, buf, bytes); 26630ec76a11SDavid Howells set_page_dirty_lock(page); 26640ec76a11SDavid Howells } else { 26650ec76a11SDavid Howells copy_from_user_page(vma, page, addr, 26660ec76a11SDavid Howells buf, maddr + offset, bytes); 26670ec76a11SDavid Howells } 26680ec76a11SDavid Howells kunmap(page); 26690ec76a11SDavid Howells page_cache_release(page); 26700ec76a11SDavid Howells len -= bytes; 26710ec76a11SDavid Howells buf += bytes; 26720ec76a11SDavid Howells addr += bytes; 26730ec76a11SDavid Howells } 26740ec76a11SDavid Howells up_read(&mm->mmap_sem); 26750ec76a11SDavid Howells mmput(mm); 26760ec76a11SDavid Howells 26770ec76a11SDavid Howells return buf - old_buf; 26780ec76a11SDavid Howells } 2679