11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/mm/memory.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds 51da177e4SLinus Torvalds */ 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds /* 81da177e4SLinus Torvalds * demand-loading started 01.12.91 - seems it is high on the list of 91da177e4SLinus Torvalds * things wanted, and it should be easy to implement. - Linus 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds /* 131da177e4SLinus Torvalds * Ok, demand-loading was easy, shared pages a little bit tricker. Shared 141da177e4SLinus Torvalds * pages started 02.12.91, seems to work. - Linus. 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * Tested sharing by executing about 30 /bin/sh: under the old kernel it 171da177e4SLinus Torvalds * would have taken more than the 6M I have free, but it worked well as 181da177e4SLinus Torvalds * far as I could see. 191da177e4SLinus Torvalds * 201da177e4SLinus Torvalds * Also corrected some "invalidate()"s - I wasn't doing enough of them. 211da177e4SLinus Torvalds */ 221da177e4SLinus Torvalds 231da177e4SLinus Torvalds /* 241da177e4SLinus Torvalds * Real VM (paging to/from disk) started 18.12.91. Much more work and 251da177e4SLinus Torvalds * thought has to go into this. Oh, well.. 261da177e4SLinus Torvalds * 19.12.91 - works, somewhat. Sometimes I get faults, don't know why. 271da177e4SLinus Torvalds * Found it. Everything seems to work now. 281da177e4SLinus Torvalds * 20.12.91 - Ok, making the swap-device changeable like the root. 291da177e4SLinus Torvalds */ 301da177e4SLinus Torvalds 311da177e4SLinus Torvalds /* 321da177e4SLinus Torvalds * 05.04.94 - Multi-page memory management added for v1.1. 331da177e4SLinus Torvalds * Idea by Alex Bligh (alex@cconcepts.co.uk) 341da177e4SLinus Torvalds * 351da177e4SLinus Torvalds * 16.07.99 - Support of BIGMEM added by Gerhard Wichert, Siemens AG 361da177e4SLinus Torvalds * (Gerhard.Wichert@pdb.siemens.de) 371da177e4SLinus Torvalds * 381da177e4SLinus Torvalds * Aug/Sep 2004 Changed to four level page tables (Andi Kleen) 391da177e4SLinus Torvalds */ 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds #include <linux/kernel_stat.h> 421da177e4SLinus Torvalds #include <linux/mm.h> 431da177e4SLinus Torvalds #include <linux/hugetlb.h> 441da177e4SLinus Torvalds #include <linux/mman.h> 451da177e4SLinus Torvalds #include <linux/swap.h> 461da177e4SLinus Torvalds #include <linux/highmem.h> 471da177e4SLinus Torvalds #include <linux/pagemap.h> 489a840895SHugh Dickins #include <linux/ksm.h> 491da177e4SLinus Torvalds #include <linux/rmap.h> 501da177e4SLinus Torvalds #include <linux/module.h> 510ff92245SShailabh Nagar #include <linux/delayacct.h> 521da177e4SLinus Torvalds #include <linux/init.h> 53edc79b2aSPeter Zijlstra #include <linux/writeback.h> 548a9f3ccdSBalbir Singh #include <linux/memcontrol.h> 55cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h> 563dc14741SHugh Dickins #include <linux/kallsyms.h> 573dc14741SHugh Dickins #include <linux/swapops.h> 583dc14741SHugh Dickins #include <linux/elf.h> 595a0e3ad6STejun Heo #include <linux/gfp.h> 601da177e4SLinus Torvalds 616952b61dSAlexey Dobriyan #include <asm/io.h> 621da177e4SLinus Torvalds #include <asm/pgalloc.h> 631da177e4SLinus Torvalds #include <asm/uaccess.h> 641da177e4SLinus Torvalds #include <asm/tlb.h> 651da177e4SLinus Torvalds #include <asm/tlbflush.h> 661da177e4SLinus Torvalds #include <asm/pgtable.h> 671da177e4SLinus Torvalds 6842b77728SJan Beulich #include "internal.h" 6942b77728SJan Beulich 70d41dee36SAndy Whitcroft #ifndef CONFIG_NEED_MULTIPLE_NODES 711da177e4SLinus Torvalds /* use the per-pgdat data instead for discontigmem - mbligh */ 721da177e4SLinus Torvalds unsigned long max_mapnr; 731da177e4SLinus Torvalds struct page *mem_map; 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds EXPORT_SYMBOL(max_mapnr); 761da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map); 771da177e4SLinus Torvalds #endif 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds unsigned long num_physpages; 801da177e4SLinus Torvalds /* 811da177e4SLinus Torvalds * A number of key systems in x86 including ioremap() rely on the assumption 821da177e4SLinus Torvalds * that high_memory defines the upper bound on direct map memory, then end 831da177e4SLinus Torvalds * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and 841da177e4SLinus Torvalds * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL 851da177e4SLinus Torvalds * and ZONE_HIGHMEM. 861da177e4SLinus Torvalds */ 871da177e4SLinus Torvalds void * high_memory; 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds EXPORT_SYMBOL(num_physpages); 901da177e4SLinus Torvalds EXPORT_SYMBOL(high_memory); 911da177e4SLinus Torvalds 9232a93233SIngo Molnar /* 9332a93233SIngo Molnar * Randomize the address space (stacks, mmaps, brk, etc.). 9432a93233SIngo Molnar * 9532a93233SIngo Molnar * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization, 9632a93233SIngo Molnar * as ancient (libc5 based) binaries can segfault. ) 9732a93233SIngo Molnar */ 9832a93233SIngo Molnar int randomize_va_space __read_mostly = 9932a93233SIngo Molnar #ifdef CONFIG_COMPAT_BRK 10032a93233SIngo Molnar 1; 10132a93233SIngo Molnar #else 10232a93233SIngo Molnar 2; 10332a93233SIngo Molnar #endif 104a62eaf15SAndi Kleen 105a62eaf15SAndi Kleen static int __init disable_randmaps(char *s) 106a62eaf15SAndi Kleen { 107a62eaf15SAndi Kleen randomize_va_space = 0; 1089b41046cSOGAWA Hirofumi return 1; 109a62eaf15SAndi Kleen } 110a62eaf15SAndi Kleen __setup("norandmaps", disable_randmaps); 111a62eaf15SAndi Kleen 11262eede62SHugh Dickins unsigned long zero_pfn __read_mostly; 11303f6462aSHugh Dickins unsigned long highest_memmap_pfn __read_mostly; 114a13ea5b7SHugh Dickins 115a13ea5b7SHugh Dickins /* 116a13ea5b7SHugh Dickins * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init() 117a13ea5b7SHugh Dickins */ 118a13ea5b7SHugh Dickins static int __init init_zero_pfn(void) 119a13ea5b7SHugh Dickins { 120a13ea5b7SHugh Dickins zero_pfn = page_to_pfn(ZERO_PAGE(0)); 121a13ea5b7SHugh Dickins return 0; 122a13ea5b7SHugh Dickins } 123a13ea5b7SHugh Dickins core_initcall(init_zero_pfn); 124a62eaf15SAndi Kleen 125d559db08SKAMEZAWA Hiroyuki 12634e55232SKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING) 12734e55232SKAMEZAWA Hiroyuki 128a3a2e76cSKAMEZAWA Hiroyuki static void __sync_task_rss_stat(struct task_struct *task, struct mm_struct *mm) 12934e55232SKAMEZAWA Hiroyuki { 13034e55232SKAMEZAWA Hiroyuki int i; 13134e55232SKAMEZAWA Hiroyuki 13234e55232SKAMEZAWA Hiroyuki for (i = 0; i < NR_MM_COUNTERS; i++) { 13334e55232SKAMEZAWA Hiroyuki if (task->rss_stat.count[i]) { 13434e55232SKAMEZAWA Hiroyuki add_mm_counter(mm, i, task->rss_stat.count[i]); 13534e55232SKAMEZAWA Hiroyuki task->rss_stat.count[i] = 0; 13634e55232SKAMEZAWA Hiroyuki } 13734e55232SKAMEZAWA Hiroyuki } 13834e55232SKAMEZAWA Hiroyuki task->rss_stat.events = 0; 13934e55232SKAMEZAWA Hiroyuki } 14034e55232SKAMEZAWA Hiroyuki 14134e55232SKAMEZAWA Hiroyuki static void add_mm_counter_fast(struct mm_struct *mm, int member, int val) 14234e55232SKAMEZAWA Hiroyuki { 14334e55232SKAMEZAWA Hiroyuki struct task_struct *task = current; 14434e55232SKAMEZAWA Hiroyuki 14534e55232SKAMEZAWA Hiroyuki if (likely(task->mm == mm)) 14634e55232SKAMEZAWA Hiroyuki task->rss_stat.count[member] += val; 14734e55232SKAMEZAWA Hiroyuki else 14834e55232SKAMEZAWA Hiroyuki add_mm_counter(mm, member, val); 14934e55232SKAMEZAWA Hiroyuki } 15034e55232SKAMEZAWA Hiroyuki #define inc_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, 1) 15134e55232SKAMEZAWA Hiroyuki #define dec_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, -1) 15234e55232SKAMEZAWA Hiroyuki 15334e55232SKAMEZAWA Hiroyuki /* sync counter once per 64 page faults */ 15434e55232SKAMEZAWA Hiroyuki #define TASK_RSS_EVENTS_THRESH (64) 15534e55232SKAMEZAWA Hiroyuki static void check_sync_rss_stat(struct task_struct *task) 15634e55232SKAMEZAWA Hiroyuki { 15734e55232SKAMEZAWA Hiroyuki if (unlikely(task != current)) 15834e55232SKAMEZAWA Hiroyuki return; 15934e55232SKAMEZAWA Hiroyuki if (unlikely(task->rss_stat.events++ > TASK_RSS_EVENTS_THRESH)) 16034e55232SKAMEZAWA Hiroyuki __sync_task_rss_stat(task, task->mm); 16134e55232SKAMEZAWA Hiroyuki } 16234e55232SKAMEZAWA Hiroyuki 16334e55232SKAMEZAWA Hiroyuki unsigned long get_mm_counter(struct mm_struct *mm, int member) 16434e55232SKAMEZAWA Hiroyuki { 16534e55232SKAMEZAWA Hiroyuki long val = 0; 16634e55232SKAMEZAWA Hiroyuki 16734e55232SKAMEZAWA Hiroyuki /* 16834e55232SKAMEZAWA Hiroyuki * Don't use task->mm here...for avoiding to use task_get_mm().. 16934e55232SKAMEZAWA Hiroyuki * The caller must guarantee task->mm is not invalid. 17034e55232SKAMEZAWA Hiroyuki */ 17134e55232SKAMEZAWA Hiroyuki val = atomic_long_read(&mm->rss_stat.count[member]); 17234e55232SKAMEZAWA Hiroyuki /* 17334e55232SKAMEZAWA Hiroyuki * counter is updated in asynchronous manner and may go to minus. 17434e55232SKAMEZAWA Hiroyuki * But it's never be expected number for users. 17534e55232SKAMEZAWA Hiroyuki */ 17634e55232SKAMEZAWA Hiroyuki if (val < 0) 17734e55232SKAMEZAWA Hiroyuki return 0; 17834e55232SKAMEZAWA Hiroyuki return (unsigned long)val; 17934e55232SKAMEZAWA Hiroyuki } 18034e55232SKAMEZAWA Hiroyuki 18134e55232SKAMEZAWA Hiroyuki void sync_mm_rss(struct task_struct *task, struct mm_struct *mm) 18234e55232SKAMEZAWA Hiroyuki { 18334e55232SKAMEZAWA Hiroyuki __sync_task_rss_stat(task, mm); 18434e55232SKAMEZAWA Hiroyuki } 18534e55232SKAMEZAWA Hiroyuki #else 18634e55232SKAMEZAWA Hiroyuki 18734e55232SKAMEZAWA Hiroyuki #define inc_mm_counter_fast(mm, member) inc_mm_counter(mm, member) 18834e55232SKAMEZAWA Hiroyuki #define dec_mm_counter_fast(mm, member) dec_mm_counter(mm, member) 18934e55232SKAMEZAWA Hiroyuki 19034e55232SKAMEZAWA Hiroyuki static void check_sync_rss_stat(struct task_struct *task) 19134e55232SKAMEZAWA Hiroyuki { 19234e55232SKAMEZAWA Hiroyuki } 19334e55232SKAMEZAWA Hiroyuki 19434e55232SKAMEZAWA Hiroyuki #endif 19534e55232SKAMEZAWA Hiroyuki 1961da177e4SLinus Torvalds /* 1971da177e4SLinus Torvalds * If a p?d_bad entry is found while walking page tables, report 1981da177e4SLinus Torvalds * the error, before resetting entry to p?d_none. Usually (but 1991da177e4SLinus Torvalds * very seldom) called out from the p?d_none_or_clear_bad macros. 2001da177e4SLinus Torvalds */ 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds void pgd_clear_bad(pgd_t *pgd) 2031da177e4SLinus Torvalds { 2041da177e4SLinus Torvalds pgd_ERROR(*pgd); 2051da177e4SLinus Torvalds pgd_clear(pgd); 2061da177e4SLinus Torvalds } 2071da177e4SLinus Torvalds 2081da177e4SLinus Torvalds void pud_clear_bad(pud_t *pud) 2091da177e4SLinus Torvalds { 2101da177e4SLinus Torvalds pud_ERROR(*pud); 2111da177e4SLinus Torvalds pud_clear(pud); 2121da177e4SLinus Torvalds } 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds void pmd_clear_bad(pmd_t *pmd) 2151da177e4SLinus Torvalds { 2161da177e4SLinus Torvalds pmd_ERROR(*pmd); 2171da177e4SLinus Torvalds pmd_clear(pmd); 2181da177e4SLinus Torvalds } 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds /* 2211da177e4SLinus Torvalds * Note: this doesn't free the actual pages themselves. That 2221da177e4SLinus Torvalds * has been handled earlier when unmapping all the memory regions. 2231da177e4SLinus Torvalds */ 2249e1b32caSBenjamin Herrenschmidt static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd, 2259e1b32caSBenjamin Herrenschmidt unsigned long addr) 2261da177e4SLinus Torvalds { 2272f569afdSMartin Schwidefsky pgtable_t token = pmd_pgtable(*pmd); 2281da177e4SLinus Torvalds pmd_clear(pmd); 2299e1b32caSBenjamin Herrenschmidt pte_free_tlb(tlb, token, addr); 2301da177e4SLinus Torvalds tlb->mm->nr_ptes--; 2311da177e4SLinus Torvalds } 2321da177e4SLinus Torvalds 233e0da382cSHugh Dickins static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud, 234e0da382cSHugh Dickins unsigned long addr, unsigned long end, 235e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 2361da177e4SLinus Torvalds { 2371da177e4SLinus Torvalds pmd_t *pmd; 2381da177e4SLinus Torvalds unsigned long next; 239e0da382cSHugh Dickins unsigned long start; 2401da177e4SLinus Torvalds 241e0da382cSHugh Dickins start = addr; 2421da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 2431da177e4SLinus Torvalds do { 2441da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 2451da177e4SLinus Torvalds if (pmd_none_or_clear_bad(pmd)) 2461da177e4SLinus Torvalds continue; 2479e1b32caSBenjamin Herrenschmidt free_pte_range(tlb, pmd, addr); 2481da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 2491da177e4SLinus Torvalds 250e0da382cSHugh Dickins start &= PUD_MASK; 251e0da382cSHugh Dickins if (start < floor) 252e0da382cSHugh Dickins return; 253e0da382cSHugh Dickins if (ceiling) { 254e0da382cSHugh Dickins ceiling &= PUD_MASK; 255e0da382cSHugh Dickins if (!ceiling) 256e0da382cSHugh Dickins return; 2571da177e4SLinus Torvalds } 258e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 259e0da382cSHugh Dickins return; 260e0da382cSHugh Dickins 261e0da382cSHugh Dickins pmd = pmd_offset(pud, start); 262e0da382cSHugh Dickins pud_clear(pud); 2639e1b32caSBenjamin Herrenschmidt pmd_free_tlb(tlb, pmd, start); 2641da177e4SLinus Torvalds } 2651da177e4SLinus Torvalds 266e0da382cSHugh Dickins static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd, 267e0da382cSHugh Dickins unsigned long addr, unsigned long end, 268e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 2691da177e4SLinus Torvalds { 2701da177e4SLinus Torvalds pud_t *pud; 2711da177e4SLinus Torvalds unsigned long next; 272e0da382cSHugh Dickins unsigned long start; 2731da177e4SLinus Torvalds 274e0da382cSHugh Dickins start = addr; 2751da177e4SLinus Torvalds pud = pud_offset(pgd, addr); 2761da177e4SLinus Torvalds do { 2771da177e4SLinus Torvalds next = pud_addr_end(addr, end); 2781da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 2791da177e4SLinus Torvalds continue; 280e0da382cSHugh Dickins free_pmd_range(tlb, pud, addr, next, floor, ceiling); 2811da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 2821da177e4SLinus Torvalds 283e0da382cSHugh Dickins start &= PGDIR_MASK; 284e0da382cSHugh Dickins if (start < floor) 285e0da382cSHugh Dickins return; 286e0da382cSHugh Dickins if (ceiling) { 287e0da382cSHugh Dickins ceiling &= PGDIR_MASK; 288e0da382cSHugh Dickins if (!ceiling) 289e0da382cSHugh Dickins return; 2901da177e4SLinus Torvalds } 291e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 292e0da382cSHugh Dickins return; 293e0da382cSHugh Dickins 294e0da382cSHugh Dickins pud = pud_offset(pgd, start); 295e0da382cSHugh Dickins pgd_clear(pgd); 2969e1b32caSBenjamin Herrenschmidt pud_free_tlb(tlb, pud, start); 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds /* 300e0da382cSHugh Dickins * This function frees user-level page tables of a process. 301e0da382cSHugh Dickins * 3021da177e4SLinus Torvalds * Must be called with pagetable lock held. 3031da177e4SLinus Torvalds */ 30442b77728SJan Beulich void free_pgd_range(struct mmu_gather *tlb, 305e0da382cSHugh Dickins unsigned long addr, unsigned long end, 306e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 3071da177e4SLinus Torvalds { 3081da177e4SLinus Torvalds pgd_t *pgd; 3091da177e4SLinus Torvalds unsigned long next; 3101da177e4SLinus Torvalds 311e0da382cSHugh Dickins /* 312e0da382cSHugh Dickins * The next few lines have given us lots of grief... 313e0da382cSHugh Dickins * 314e0da382cSHugh Dickins * Why are we testing PMD* at this top level? Because often 315e0da382cSHugh Dickins * there will be no work to do at all, and we'd prefer not to 316e0da382cSHugh Dickins * go all the way down to the bottom just to discover that. 317e0da382cSHugh Dickins * 318e0da382cSHugh Dickins * Why all these "- 1"s? Because 0 represents both the bottom 319e0da382cSHugh Dickins * of the address space and the top of it (using -1 for the 320e0da382cSHugh Dickins * top wouldn't help much: the masks would do the wrong thing). 321e0da382cSHugh Dickins * The rule is that addr 0 and floor 0 refer to the bottom of 322e0da382cSHugh Dickins * the address space, but end 0 and ceiling 0 refer to the top 323e0da382cSHugh Dickins * Comparisons need to use "end - 1" and "ceiling - 1" (though 324e0da382cSHugh Dickins * that end 0 case should be mythical). 325e0da382cSHugh Dickins * 326e0da382cSHugh Dickins * Wherever addr is brought up or ceiling brought down, we must 327e0da382cSHugh Dickins * be careful to reject "the opposite 0" before it confuses the 328e0da382cSHugh Dickins * subsequent tests. But what about where end is brought down 329e0da382cSHugh Dickins * by PMD_SIZE below? no, end can't go down to 0 there. 330e0da382cSHugh Dickins * 331e0da382cSHugh Dickins * Whereas we round start (addr) and ceiling down, by different 332e0da382cSHugh Dickins * masks at different levels, in order to test whether a table 333e0da382cSHugh Dickins * now has no other vmas using it, so can be freed, we don't 334e0da382cSHugh Dickins * bother to round floor or end up - the tests don't need that. 335e0da382cSHugh Dickins */ 336e0da382cSHugh Dickins 337e0da382cSHugh Dickins addr &= PMD_MASK; 338e0da382cSHugh Dickins if (addr < floor) { 339e0da382cSHugh Dickins addr += PMD_SIZE; 340e0da382cSHugh Dickins if (!addr) 341e0da382cSHugh Dickins return; 342e0da382cSHugh Dickins } 343e0da382cSHugh Dickins if (ceiling) { 344e0da382cSHugh Dickins ceiling &= PMD_MASK; 345e0da382cSHugh Dickins if (!ceiling) 346e0da382cSHugh Dickins return; 347e0da382cSHugh Dickins } 348e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 349e0da382cSHugh Dickins end -= PMD_SIZE; 350e0da382cSHugh Dickins if (addr > end - 1) 351e0da382cSHugh Dickins return; 352e0da382cSHugh Dickins 35342b77728SJan Beulich pgd = pgd_offset(tlb->mm, addr); 3541da177e4SLinus Torvalds do { 3551da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 3561da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 3571da177e4SLinus Torvalds continue; 35842b77728SJan Beulich free_pud_range(tlb, pgd, addr, next, floor, ceiling); 3591da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 360e0da382cSHugh Dickins } 361e0da382cSHugh Dickins 36242b77728SJan Beulich void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma, 363e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 364e0da382cSHugh Dickins { 365e0da382cSHugh Dickins while (vma) { 366e0da382cSHugh Dickins struct vm_area_struct *next = vma->vm_next; 367e0da382cSHugh Dickins unsigned long addr = vma->vm_start; 368e0da382cSHugh Dickins 3698f4f8c16SHugh Dickins /* 37025d9e2d1Snpiggin@suse.de * Hide vma from rmap and truncate_pagecache before freeing 37125d9e2d1Snpiggin@suse.de * pgtables 3728f4f8c16SHugh Dickins */ 3735beb4930SRik van Riel unlink_anon_vmas(vma); 3748f4f8c16SHugh Dickins unlink_file_vma(vma); 3758f4f8c16SHugh Dickins 3769da61aefSDavid Gibson if (is_vm_hugetlb_page(vma)) { 3773bf5ee95SHugh Dickins hugetlb_free_pgd_range(tlb, addr, vma->vm_end, 3783bf5ee95SHugh Dickins floor, next? next->vm_start: ceiling); 3793bf5ee95SHugh Dickins } else { 3803bf5ee95SHugh Dickins /* 3813bf5ee95SHugh Dickins * Optimization: gather nearby vmas into one call down 3823bf5ee95SHugh Dickins */ 3833bf5ee95SHugh Dickins while (next && next->vm_start <= vma->vm_end + PMD_SIZE 3844866920bSDavid Gibson && !is_vm_hugetlb_page(next)) { 385e0da382cSHugh Dickins vma = next; 386e0da382cSHugh Dickins next = vma->vm_next; 3875beb4930SRik van Riel unlink_anon_vmas(vma); 3888f4f8c16SHugh Dickins unlink_file_vma(vma); 389e0da382cSHugh Dickins } 3903bf5ee95SHugh Dickins free_pgd_range(tlb, addr, vma->vm_end, 391e0da382cSHugh Dickins floor, next? next->vm_start: ceiling); 3923bf5ee95SHugh Dickins } 393e0da382cSHugh Dickins vma = next; 394e0da382cSHugh Dickins } 3951da177e4SLinus Torvalds } 3961da177e4SLinus Torvalds 3978ac1f832SAndrea Arcangeli int __pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma, 3988ac1f832SAndrea Arcangeli pmd_t *pmd, unsigned long address) 3991da177e4SLinus Torvalds { 4002f569afdSMartin Schwidefsky pgtable_t new = pte_alloc_one(mm, address); 4018ac1f832SAndrea Arcangeli int wait_split_huge_page; 4021da177e4SLinus Torvalds if (!new) 4031bb3630eSHugh Dickins return -ENOMEM; 4041bb3630eSHugh Dickins 405362a61adSNick Piggin /* 406362a61adSNick Piggin * Ensure all pte setup (eg. pte page lock and page clearing) are 407362a61adSNick Piggin * visible before the pte is made visible to other CPUs by being 408362a61adSNick Piggin * put into page tables. 409362a61adSNick Piggin * 410362a61adSNick Piggin * The other side of the story is the pointer chasing in the page 411362a61adSNick Piggin * table walking code (when walking the page table without locking; 412362a61adSNick Piggin * ie. most of the time). Fortunately, these data accesses consist 413362a61adSNick Piggin * of a chain of data-dependent loads, meaning most CPUs (alpha 414362a61adSNick Piggin * being the notable exception) will already guarantee loads are 415362a61adSNick Piggin * seen in-order. See the alpha page table accessors for the 416362a61adSNick Piggin * smp_read_barrier_depends() barriers in page table walking code. 417362a61adSNick Piggin */ 418362a61adSNick Piggin smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */ 419362a61adSNick Piggin 420c74df32cSHugh Dickins spin_lock(&mm->page_table_lock); 4218ac1f832SAndrea Arcangeli wait_split_huge_page = 0; 4228ac1f832SAndrea Arcangeli if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 4231da177e4SLinus Torvalds mm->nr_ptes++; 4241da177e4SLinus Torvalds pmd_populate(mm, pmd, new); 4252f569afdSMartin Schwidefsky new = NULL; 4268ac1f832SAndrea Arcangeli } else if (unlikely(pmd_trans_splitting(*pmd))) 4278ac1f832SAndrea Arcangeli wait_split_huge_page = 1; 428c74df32cSHugh Dickins spin_unlock(&mm->page_table_lock); 4292f569afdSMartin Schwidefsky if (new) 4302f569afdSMartin Schwidefsky pte_free(mm, new); 4318ac1f832SAndrea Arcangeli if (wait_split_huge_page) 4328ac1f832SAndrea Arcangeli wait_split_huge_page(vma->anon_vma, pmd); 4331bb3630eSHugh Dickins return 0; 4341da177e4SLinus Torvalds } 4351da177e4SLinus Torvalds 4361bb3630eSHugh Dickins int __pte_alloc_kernel(pmd_t *pmd, unsigned long address) 4371da177e4SLinus Torvalds { 4381bb3630eSHugh Dickins pte_t *new = pte_alloc_one_kernel(&init_mm, address); 4391da177e4SLinus Torvalds if (!new) 4401bb3630eSHugh Dickins return -ENOMEM; 4411da177e4SLinus Torvalds 442362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 443362a61adSNick Piggin 444872fec16SHugh Dickins spin_lock(&init_mm.page_table_lock); 4458ac1f832SAndrea Arcangeli if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 446872fec16SHugh Dickins pmd_populate_kernel(&init_mm, pmd, new); 4472f569afdSMartin Schwidefsky new = NULL; 4488ac1f832SAndrea Arcangeli } else 4498ac1f832SAndrea Arcangeli VM_BUG_ON(pmd_trans_splitting(*pmd)); 450872fec16SHugh Dickins spin_unlock(&init_mm.page_table_lock); 4512f569afdSMartin Schwidefsky if (new) 4522f569afdSMartin Schwidefsky pte_free_kernel(&init_mm, new); 4531bb3630eSHugh Dickins return 0; 4541da177e4SLinus Torvalds } 4551da177e4SLinus Torvalds 456d559db08SKAMEZAWA Hiroyuki static inline void init_rss_vec(int *rss) 457ae859762SHugh Dickins { 458d559db08SKAMEZAWA Hiroyuki memset(rss, 0, sizeof(int) * NR_MM_COUNTERS); 459d559db08SKAMEZAWA Hiroyuki } 460d559db08SKAMEZAWA Hiroyuki 461d559db08SKAMEZAWA Hiroyuki static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss) 462d559db08SKAMEZAWA Hiroyuki { 463d559db08SKAMEZAWA Hiroyuki int i; 464d559db08SKAMEZAWA Hiroyuki 46534e55232SKAMEZAWA Hiroyuki if (current->mm == mm) 46634e55232SKAMEZAWA Hiroyuki sync_mm_rss(current, mm); 467d559db08SKAMEZAWA Hiroyuki for (i = 0; i < NR_MM_COUNTERS; i++) 468d559db08SKAMEZAWA Hiroyuki if (rss[i]) 469d559db08SKAMEZAWA Hiroyuki add_mm_counter(mm, i, rss[i]); 470ae859762SHugh Dickins } 471ae859762SHugh Dickins 4721da177e4SLinus Torvalds /* 4736aab341eSLinus Torvalds * This function is called to print an error when a bad pte 4746aab341eSLinus Torvalds * is found. For example, we might have a PFN-mapped pte in 4756aab341eSLinus Torvalds * a region that doesn't allow it. 476b5810039SNick Piggin * 477b5810039SNick Piggin * The calling function must still handle the error. 478b5810039SNick Piggin */ 4793dc14741SHugh Dickins static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr, 4803dc14741SHugh Dickins pte_t pte, struct page *page) 481b5810039SNick Piggin { 4823dc14741SHugh Dickins pgd_t *pgd = pgd_offset(vma->vm_mm, addr); 4833dc14741SHugh Dickins pud_t *pud = pud_offset(pgd, addr); 4843dc14741SHugh Dickins pmd_t *pmd = pmd_offset(pud, addr); 4853dc14741SHugh Dickins struct address_space *mapping; 4863dc14741SHugh Dickins pgoff_t index; 487d936cf9bSHugh Dickins static unsigned long resume; 488d936cf9bSHugh Dickins static unsigned long nr_shown; 489d936cf9bSHugh Dickins static unsigned long nr_unshown; 490d936cf9bSHugh Dickins 491d936cf9bSHugh Dickins /* 492d936cf9bSHugh Dickins * Allow a burst of 60 reports, then keep quiet for that minute; 493d936cf9bSHugh Dickins * or allow a steady drip of one report per second. 494d936cf9bSHugh Dickins */ 495d936cf9bSHugh Dickins if (nr_shown == 60) { 496d936cf9bSHugh Dickins if (time_before(jiffies, resume)) { 497d936cf9bSHugh Dickins nr_unshown++; 498d936cf9bSHugh Dickins return; 499d936cf9bSHugh Dickins } 500d936cf9bSHugh Dickins if (nr_unshown) { 5011e9e6365SHugh Dickins printk(KERN_ALERT 5021e9e6365SHugh Dickins "BUG: Bad page map: %lu messages suppressed\n", 503d936cf9bSHugh Dickins nr_unshown); 504d936cf9bSHugh Dickins nr_unshown = 0; 505d936cf9bSHugh Dickins } 506d936cf9bSHugh Dickins nr_shown = 0; 507d936cf9bSHugh Dickins } 508d936cf9bSHugh Dickins if (nr_shown++ == 0) 509d936cf9bSHugh Dickins resume = jiffies + 60 * HZ; 5103dc14741SHugh Dickins 5113dc14741SHugh Dickins mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL; 5123dc14741SHugh Dickins index = linear_page_index(vma, addr); 5133dc14741SHugh Dickins 5141e9e6365SHugh Dickins printk(KERN_ALERT 5151e9e6365SHugh Dickins "BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n", 5163dc14741SHugh Dickins current->comm, 5173dc14741SHugh Dickins (long long)pte_val(pte), (long long)pmd_val(*pmd)); 518718a3821SWu Fengguang if (page) 519718a3821SWu Fengguang dump_page(page); 5201e9e6365SHugh Dickins printk(KERN_ALERT 5213dc14741SHugh Dickins "addr:%p vm_flags:%08lx anon_vma:%p mapping:%p index:%lx\n", 5223dc14741SHugh Dickins (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index); 5233dc14741SHugh Dickins /* 5243dc14741SHugh Dickins * Choose text because data symbols depend on CONFIG_KALLSYMS_ALL=y 5253dc14741SHugh Dickins */ 5263dc14741SHugh Dickins if (vma->vm_ops) 5271e9e6365SHugh Dickins print_symbol(KERN_ALERT "vma->vm_ops->fault: %s\n", 5283dc14741SHugh Dickins (unsigned long)vma->vm_ops->fault); 5293dc14741SHugh Dickins if (vma->vm_file && vma->vm_file->f_op) 5301e9e6365SHugh Dickins print_symbol(KERN_ALERT "vma->vm_file->f_op->mmap: %s\n", 5313dc14741SHugh Dickins (unsigned long)vma->vm_file->f_op->mmap); 532b5810039SNick Piggin dump_stack(); 5333dc14741SHugh Dickins add_taint(TAINT_BAD_PAGE); 534b5810039SNick Piggin } 535b5810039SNick Piggin 53667121172SLinus Torvalds static inline int is_cow_mapping(unsigned int flags) 53767121172SLinus Torvalds { 53867121172SLinus Torvalds return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; 53967121172SLinus Torvalds } 54067121172SLinus Torvalds 54162eede62SHugh Dickins #ifndef is_zero_pfn 54262eede62SHugh Dickins static inline int is_zero_pfn(unsigned long pfn) 54362eede62SHugh Dickins { 54462eede62SHugh Dickins return pfn == zero_pfn; 54562eede62SHugh Dickins } 54662eede62SHugh Dickins #endif 54762eede62SHugh Dickins 54862eede62SHugh Dickins #ifndef my_zero_pfn 54962eede62SHugh Dickins static inline unsigned long my_zero_pfn(unsigned long addr) 55062eede62SHugh Dickins { 55162eede62SHugh Dickins return zero_pfn; 55262eede62SHugh Dickins } 55362eede62SHugh Dickins #endif 55462eede62SHugh Dickins 555b5810039SNick Piggin /* 5567e675137SNick Piggin * vm_normal_page -- This function gets the "struct page" associated with a pte. 5576aab341eSLinus Torvalds * 5587e675137SNick Piggin * "Special" mappings do not wish to be associated with a "struct page" (either 5597e675137SNick Piggin * it doesn't exist, or it exists but they don't want to touch it). In this 5607e675137SNick Piggin * case, NULL is returned here. "Normal" mappings do have a struct page. 561b379d790SJared Hulbert * 5627e675137SNick Piggin * There are 2 broad cases. Firstly, an architecture may define a pte_special() 5637e675137SNick Piggin * pte bit, in which case this function is trivial. Secondly, an architecture 5647e675137SNick Piggin * may not have a spare pte bit, which requires a more complicated scheme, 5657e675137SNick Piggin * described below. 5667e675137SNick Piggin * 5677e675137SNick Piggin * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a 5687e675137SNick Piggin * special mapping (even if there are underlying and valid "struct pages"). 5697e675137SNick Piggin * COWed pages of a VM_PFNMAP are always normal. 5706aab341eSLinus Torvalds * 571b379d790SJared Hulbert * The way we recognize COWed pages within VM_PFNMAP mappings is through the 572b379d790SJared Hulbert * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit 5737e675137SNick Piggin * set, and the vm_pgoff will point to the first PFN mapped: thus every special 5747e675137SNick Piggin * mapping will always honor the rule 5756aab341eSLinus Torvalds * 5766aab341eSLinus Torvalds * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT) 5776aab341eSLinus Torvalds * 5787e675137SNick Piggin * And for normal mappings this is false. 579b379d790SJared Hulbert * 5807e675137SNick Piggin * This restricts such mappings to be a linear translation from virtual address 5817e675137SNick Piggin * to pfn. To get around this restriction, we allow arbitrary mappings so long 5827e675137SNick Piggin * as the vma is not a COW mapping; in that case, we know that all ptes are 5837e675137SNick Piggin * special (because none can have been COWed). 584b379d790SJared Hulbert * 585b379d790SJared Hulbert * 5867e675137SNick Piggin * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP. 5877e675137SNick Piggin * 588b379d790SJared Hulbert * VM_MIXEDMAP mappings can likewise contain memory with or without "struct 589b379d790SJared Hulbert * page" backing, however the difference is that _all_ pages with a struct 590b379d790SJared Hulbert * page (that is, those where pfn_valid is true) are refcounted and considered 591b379d790SJared Hulbert * normal pages by the VM. The disadvantage is that pages are refcounted 592b379d790SJared Hulbert * (which can be slower and simply not an option for some PFNMAP users). The 593b379d790SJared Hulbert * advantage is that we don't have to follow the strict linearity rule of 594b379d790SJared Hulbert * PFNMAP mappings in order to support COWable mappings. 595b379d790SJared Hulbert * 596ee498ed7SHugh Dickins */ 5977e675137SNick Piggin #ifdef __HAVE_ARCH_PTE_SPECIAL 5987e675137SNick Piggin # define HAVE_PTE_SPECIAL 1 5997e675137SNick Piggin #else 6007e675137SNick Piggin # define HAVE_PTE_SPECIAL 0 6017e675137SNick Piggin #endif 6027e675137SNick Piggin struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, 6037e675137SNick Piggin pte_t pte) 604ee498ed7SHugh Dickins { 60522b31eecSHugh Dickins unsigned long pfn = pte_pfn(pte); 6067e675137SNick Piggin 6077e675137SNick Piggin if (HAVE_PTE_SPECIAL) { 60822b31eecSHugh Dickins if (likely(!pte_special(pte))) 60922b31eecSHugh Dickins goto check_pfn; 610a13ea5b7SHugh Dickins if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)) 611a13ea5b7SHugh Dickins return NULL; 61262eede62SHugh Dickins if (!is_zero_pfn(pfn)) 61322b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 6147e675137SNick Piggin return NULL; 6157e675137SNick Piggin } 6167e675137SNick Piggin 6177e675137SNick Piggin /* !HAVE_PTE_SPECIAL case follows: */ 6187e675137SNick Piggin 619b379d790SJared Hulbert if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { 620b379d790SJared Hulbert if (vma->vm_flags & VM_MIXEDMAP) { 621b379d790SJared Hulbert if (!pfn_valid(pfn)) 622b379d790SJared Hulbert return NULL; 623b379d790SJared Hulbert goto out; 624b379d790SJared Hulbert } else { 6257e675137SNick Piggin unsigned long off; 6267e675137SNick Piggin off = (addr - vma->vm_start) >> PAGE_SHIFT; 6276aab341eSLinus Torvalds if (pfn == vma->vm_pgoff + off) 6286aab341eSLinus Torvalds return NULL; 62967121172SLinus Torvalds if (!is_cow_mapping(vma->vm_flags)) 630fb155c16SLinus Torvalds return NULL; 6316aab341eSLinus Torvalds } 632b379d790SJared Hulbert } 6336aab341eSLinus Torvalds 63462eede62SHugh Dickins if (is_zero_pfn(pfn)) 63562eede62SHugh Dickins return NULL; 63622b31eecSHugh Dickins check_pfn: 63722b31eecSHugh Dickins if (unlikely(pfn > highest_memmap_pfn)) { 63822b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 63922b31eecSHugh Dickins return NULL; 64022b31eecSHugh Dickins } 6416aab341eSLinus Torvalds 6426aab341eSLinus Torvalds /* 6437e675137SNick Piggin * NOTE! We still have PageReserved() pages in the page tables. 6447e675137SNick Piggin * eg. VDSO mappings can cause them to exist. 6456aab341eSLinus Torvalds */ 646b379d790SJared Hulbert out: 6476aab341eSLinus Torvalds return pfn_to_page(pfn); 648ee498ed7SHugh Dickins } 649ee498ed7SHugh Dickins 650ee498ed7SHugh Dickins /* 6511da177e4SLinus Torvalds * copy one vm_area from one task to the other. Assumes the page tables 6521da177e4SLinus Torvalds * already present in the new task to be cleared in the whole range 6531da177e4SLinus Torvalds * covered by this vma. 6541da177e4SLinus Torvalds */ 6551da177e4SLinus Torvalds 656570a335bSHugh Dickins static inline unsigned long 6571da177e4SLinus Torvalds copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, 658b5810039SNick Piggin pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma, 6598c103762SHugh Dickins unsigned long addr, int *rss) 6601da177e4SLinus Torvalds { 661b5810039SNick Piggin unsigned long vm_flags = vma->vm_flags; 6621da177e4SLinus Torvalds pte_t pte = *src_pte; 6631da177e4SLinus Torvalds struct page *page; 6641da177e4SLinus Torvalds 6651da177e4SLinus Torvalds /* pte contains position in swap or file, so copy. */ 6661da177e4SLinus Torvalds if (unlikely(!pte_present(pte))) { 6671da177e4SLinus Torvalds if (!pte_file(pte)) { 6680697212aSChristoph Lameter swp_entry_t entry = pte_to_swp_entry(pte); 6690697212aSChristoph Lameter 670570a335bSHugh Dickins if (swap_duplicate(entry) < 0) 671570a335bSHugh Dickins return entry.val; 672570a335bSHugh Dickins 6731da177e4SLinus Torvalds /* make sure dst_mm is on swapoff's mmlist. */ 6741da177e4SLinus Torvalds if (unlikely(list_empty(&dst_mm->mmlist))) { 6751da177e4SLinus Torvalds spin_lock(&mmlist_lock); 676f412ac08SHugh Dickins if (list_empty(&dst_mm->mmlist)) 677f412ac08SHugh Dickins list_add(&dst_mm->mmlist, 678f412ac08SHugh Dickins &src_mm->mmlist); 6791da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 6801da177e4SLinus Torvalds } 681b084d435SKAMEZAWA Hiroyuki if (likely(!non_swap_entry(entry))) 682b084d435SKAMEZAWA Hiroyuki rss[MM_SWAPENTS]++; 683b084d435SKAMEZAWA Hiroyuki else if (is_write_migration_entry(entry) && 6840697212aSChristoph Lameter is_cow_mapping(vm_flags)) { 6850697212aSChristoph Lameter /* 6860697212aSChristoph Lameter * COW mappings require pages in both parent 6870697212aSChristoph Lameter * and child to be set to read. 6880697212aSChristoph Lameter */ 6890697212aSChristoph Lameter make_migration_entry_read(&entry); 6900697212aSChristoph Lameter pte = swp_entry_to_pte(entry); 6910697212aSChristoph Lameter set_pte_at(src_mm, addr, src_pte, pte); 6920697212aSChristoph Lameter } 6931da177e4SLinus Torvalds } 694ae859762SHugh Dickins goto out_set_pte; 6951da177e4SLinus Torvalds } 6961da177e4SLinus Torvalds 6971da177e4SLinus Torvalds /* 6981da177e4SLinus Torvalds * If it's a COW mapping, write protect it both 6991da177e4SLinus Torvalds * in the parent and the child 7001da177e4SLinus Torvalds */ 70167121172SLinus Torvalds if (is_cow_mapping(vm_flags)) { 7021da177e4SLinus Torvalds ptep_set_wrprotect(src_mm, addr, src_pte); 7033dc90795SZachary Amsden pte = pte_wrprotect(pte); 7041da177e4SLinus Torvalds } 7051da177e4SLinus Torvalds 7061da177e4SLinus Torvalds /* 7071da177e4SLinus Torvalds * If it's a shared mapping, mark it clean in 7081da177e4SLinus Torvalds * the child 7091da177e4SLinus Torvalds */ 7101da177e4SLinus Torvalds if (vm_flags & VM_SHARED) 7111da177e4SLinus Torvalds pte = pte_mkclean(pte); 7121da177e4SLinus Torvalds pte = pte_mkold(pte); 7136aab341eSLinus Torvalds 7146aab341eSLinus Torvalds page = vm_normal_page(vma, addr, pte); 7156aab341eSLinus Torvalds if (page) { 7161da177e4SLinus Torvalds get_page(page); 71721333b2bSHugh Dickins page_dup_rmap(page); 718d559db08SKAMEZAWA Hiroyuki if (PageAnon(page)) 719d559db08SKAMEZAWA Hiroyuki rss[MM_ANONPAGES]++; 720d559db08SKAMEZAWA Hiroyuki else 721d559db08SKAMEZAWA Hiroyuki rss[MM_FILEPAGES]++; 7226aab341eSLinus Torvalds } 723ae859762SHugh Dickins 724ae859762SHugh Dickins out_set_pte: 725ae859762SHugh Dickins set_pte_at(dst_mm, addr, dst_pte, pte); 726570a335bSHugh Dickins return 0; 7271da177e4SLinus Torvalds } 7281da177e4SLinus Torvalds 72971e3aac0SAndrea Arcangeli int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 7301da177e4SLinus Torvalds pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma, 7311da177e4SLinus Torvalds unsigned long addr, unsigned long end) 7321da177e4SLinus Torvalds { 733c36987e2SDaisuke Nishimura pte_t *orig_src_pte, *orig_dst_pte; 7341da177e4SLinus Torvalds pte_t *src_pte, *dst_pte; 735c74df32cSHugh Dickins spinlock_t *src_ptl, *dst_ptl; 736e040f218SHugh Dickins int progress = 0; 737d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 738570a335bSHugh Dickins swp_entry_t entry = (swp_entry_t){0}; 7391da177e4SLinus Torvalds 7401da177e4SLinus Torvalds again: 741d559db08SKAMEZAWA Hiroyuki init_rss_vec(rss); 742d559db08SKAMEZAWA Hiroyuki 743c74df32cSHugh Dickins dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl); 7441da177e4SLinus Torvalds if (!dst_pte) 7451da177e4SLinus Torvalds return -ENOMEM; 746ece0e2b6SPeter Zijlstra src_pte = pte_offset_map(src_pmd, addr); 7474c21e2f2SHugh Dickins src_ptl = pte_lockptr(src_mm, src_pmd); 748f20dc5f7SIngo Molnar spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 749c36987e2SDaisuke Nishimura orig_src_pte = src_pte; 750c36987e2SDaisuke Nishimura orig_dst_pte = dst_pte; 7516606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 7521da177e4SLinus Torvalds 7531da177e4SLinus Torvalds do { 7541da177e4SLinus Torvalds /* 7551da177e4SLinus Torvalds * We are holding two locks at this point - either of them 7561da177e4SLinus Torvalds * could generate latencies in another task on another CPU. 7571da177e4SLinus Torvalds */ 758e040f218SHugh Dickins if (progress >= 32) { 759e040f218SHugh Dickins progress = 0; 760e040f218SHugh Dickins if (need_resched() || 76195c354feSNick Piggin spin_needbreak(src_ptl) || spin_needbreak(dst_ptl)) 7621da177e4SLinus Torvalds break; 763e040f218SHugh Dickins } 7641da177e4SLinus Torvalds if (pte_none(*src_pte)) { 7651da177e4SLinus Torvalds progress++; 7661da177e4SLinus Torvalds continue; 7671da177e4SLinus Torvalds } 768570a335bSHugh Dickins entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte, 769570a335bSHugh Dickins vma, addr, rss); 770570a335bSHugh Dickins if (entry.val) 771570a335bSHugh Dickins break; 7721da177e4SLinus Torvalds progress += 8; 7731da177e4SLinus Torvalds } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end); 7741da177e4SLinus Torvalds 7756606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 776c74df32cSHugh Dickins spin_unlock(src_ptl); 777ece0e2b6SPeter Zijlstra pte_unmap(orig_src_pte); 778d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(dst_mm, rss); 779c36987e2SDaisuke Nishimura pte_unmap_unlock(orig_dst_pte, dst_ptl); 780c74df32cSHugh Dickins cond_resched(); 781570a335bSHugh Dickins 782570a335bSHugh Dickins if (entry.val) { 783570a335bSHugh Dickins if (add_swap_count_continuation(entry, GFP_KERNEL) < 0) 784570a335bSHugh Dickins return -ENOMEM; 785570a335bSHugh Dickins progress = 0; 786570a335bSHugh Dickins } 7871da177e4SLinus Torvalds if (addr != end) 7881da177e4SLinus Torvalds goto again; 7891da177e4SLinus Torvalds return 0; 7901da177e4SLinus Torvalds } 7911da177e4SLinus Torvalds 7921da177e4SLinus Torvalds static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 7931da177e4SLinus Torvalds pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma, 7941da177e4SLinus Torvalds unsigned long addr, unsigned long end) 7951da177e4SLinus Torvalds { 7961da177e4SLinus Torvalds pmd_t *src_pmd, *dst_pmd; 7971da177e4SLinus Torvalds unsigned long next; 7981da177e4SLinus Torvalds 7991da177e4SLinus Torvalds dst_pmd = pmd_alloc(dst_mm, dst_pud, addr); 8001da177e4SLinus Torvalds if (!dst_pmd) 8011da177e4SLinus Torvalds return -ENOMEM; 8021da177e4SLinus Torvalds src_pmd = pmd_offset(src_pud, addr); 8031da177e4SLinus Torvalds do { 8041da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 80571e3aac0SAndrea Arcangeli if (pmd_trans_huge(*src_pmd)) { 80671e3aac0SAndrea Arcangeli int err; 80771e3aac0SAndrea Arcangeli err = copy_huge_pmd(dst_mm, src_mm, 80871e3aac0SAndrea Arcangeli dst_pmd, src_pmd, addr, vma); 80971e3aac0SAndrea Arcangeli if (err == -ENOMEM) 81071e3aac0SAndrea Arcangeli return -ENOMEM; 81171e3aac0SAndrea Arcangeli if (!err) 81271e3aac0SAndrea Arcangeli continue; 81371e3aac0SAndrea Arcangeli /* fall through */ 81471e3aac0SAndrea Arcangeli } 8151da177e4SLinus Torvalds if (pmd_none_or_clear_bad(src_pmd)) 8161da177e4SLinus Torvalds continue; 8171da177e4SLinus Torvalds if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd, 8181da177e4SLinus Torvalds vma, addr, next)) 8191da177e4SLinus Torvalds return -ENOMEM; 8201da177e4SLinus Torvalds } while (dst_pmd++, src_pmd++, addr = next, addr != end); 8211da177e4SLinus Torvalds return 0; 8221da177e4SLinus Torvalds } 8231da177e4SLinus Torvalds 8241da177e4SLinus Torvalds static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 8251da177e4SLinus Torvalds pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma, 8261da177e4SLinus Torvalds unsigned long addr, unsigned long end) 8271da177e4SLinus Torvalds { 8281da177e4SLinus Torvalds pud_t *src_pud, *dst_pud; 8291da177e4SLinus Torvalds unsigned long next; 8301da177e4SLinus Torvalds 8311da177e4SLinus Torvalds dst_pud = pud_alloc(dst_mm, dst_pgd, addr); 8321da177e4SLinus Torvalds if (!dst_pud) 8331da177e4SLinus Torvalds return -ENOMEM; 8341da177e4SLinus Torvalds src_pud = pud_offset(src_pgd, addr); 8351da177e4SLinus Torvalds do { 8361da177e4SLinus Torvalds next = pud_addr_end(addr, end); 8371da177e4SLinus Torvalds if (pud_none_or_clear_bad(src_pud)) 8381da177e4SLinus Torvalds continue; 8391da177e4SLinus Torvalds if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud, 8401da177e4SLinus Torvalds vma, addr, next)) 8411da177e4SLinus Torvalds return -ENOMEM; 8421da177e4SLinus Torvalds } while (dst_pud++, src_pud++, addr = next, addr != end); 8431da177e4SLinus Torvalds return 0; 8441da177e4SLinus Torvalds } 8451da177e4SLinus Torvalds 8461da177e4SLinus Torvalds int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 8471da177e4SLinus Torvalds struct vm_area_struct *vma) 8481da177e4SLinus Torvalds { 8491da177e4SLinus Torvalds pgd_t *src_pgd, *dst_pgd; 8501da177e4SLinus Torvalds unsigned long next; 8511da177e4SLinus Torvalds unsigned long addr = vma->vm_start; 8521da177e4SLinus Torvalds unsigned long end = vma->vm_end; 853cddb8a5cSAndrea Arcangeli int ret; 8541da177e4SLinus Torvalds 855d992895bSNick Piggin /* 856d992895bSNick Piggin * Don't copy ptes where a page fault will fill them correctly. 857d992895bSNick Piggin * Fork becomes much lighter when there are big shared or private 858d992895bSNick Piggin * readonly mappings. The tradeoff is that copy_page_range is more 859d992895bSNick Piggin * efficient than faulting. 860d992895bSNick Piggin */ 8614d7672b4SLinus Torvalds if (!(vma->vm_flags & (VM_HUGETLB|VM_NONLINEAR|VM_PFNMAP|VM_INSERTPAGE))) { 862d992895bSNick Piggin if (!vma->anon_vma) 863d992895bSNick Piggin return 0; 864d992895bSNick Piggin } 865d992895bSNick Piggin 8661da177e4SLinus Torvalds if (is_vm_hugetlb_page(vma)) 8671da177e4SLinus Torvalds return copy_hugetlb_page_range(dst_mm, src_mm, vma); 8681da177e4SLinus Torvalds 86934801ba9Svenkatesh.pallipadi@intel.com if (unlikely(is_pfn_mapping(vma))) { 8702ab64037Svenkatesh.pallipadi@intel.com /* 8712ab64037Svenkatesh.pallipadi@intel.com * We do not free on error cases below as remove_vma 8722ab64037Svenkatesh.pallipadi@intel.com * gets called on error from higher level routine 8732ab64037Svenkatesh.pallipadi@intel.com */ 8742ab64037Svenkatesh.pallipadi@intel.com ret = track_pfn_vma_copy(vma); 8752ab64037Svenkatesh.pallipadi@intel.com if (ret) 8762ab64037Svenkatesh.pallipadi@intel.com return ret; 8772ab64037Svenkatesh.pallipadi@intel.com } 8782ab64037Svenkatesh.pallipadi@intel.com 879cddb8a5cSAndrea Arcangeli /* 880cddb8a5cSAndrea Arcangeli * We need to invalidate the secondary MMU mappings only when 881cddb8a5cSAndrea Arcangeli * there could be a permission downgrade on the ptes of the 882cddb8a5cSAndrea Arcangeli * parent mm. And a permission downgrade will only happen if 883cddb8a5cSAndrea Arcangeli * is_cow_mapping() returns true. 884cddb8a5cSAndrea Arcangeli */ 885cddb8a5cSAndrea Arcangeli if (is_cow_mapping(vma->vm_flags)) 886cddb8a5cSAndrea Arcangeli mmu_notifier_invalidate_range_start(src_mm, addr, end); 887cddb8a5cSAndrea Arcangeli 888cddb8a5cSAndrea Arcangeli ret = 0; 8891da177e4SLinus Torvalds dst_pgd = pgd_offset(dst_mm, addr); 8901da177e4SLinus Torvalds src_pgd = pgd_offset(src_mm, addr); 8911da177e4SLinus Torvalds do { 8921da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 8931da177e4SLinus Torvalds if (pgd_none_or_clear_bad(src_pgd)) 8941da177e4SLinus Torvalds continue; 895cddb8a5cSAndrea Arcangeli if (unlikely(copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd, 896cddb8a5cSAndrea Arcangeli vma, addr, next))) { 897cddb8a5cSAndrea Arcangeli ret = -ENOMEM; 898cddb8a5cSAndrea Arcangeli break; 899cddb8a5cSAndrea Arcangeli } 9001da177e4SLinus Torvalds } while (dst_pgd++, src_pgd++, addr = next, addr != end); 901cddb8a5cSAndrea Arcangeli 902cddb8a5cSAndrea Arcangeli if (is_cow_mapping(vma->vm_flags)) 903cddb8a5cSAndrea Arcangeli mmu_notifier_invalidate_range_end(src_mm, 904cddb8a5cSAndrea Arcangeli vma->vm_start, end); 905cddb8a5cSAndrea Arcangeli return ret; 9061da177e4SLinus Torvalds } 9071da177e4SLinus Torvalds 90851c6f666SRobin Holt static unsigned long zap_pte_range(struct mmu_gather *tlb, 909b5810039SNick Piggin struct vm_area_struct *vma, pmd_t *pmd, 9101da177e4SLinus Torvalds unsigned long addr, unsigned long end, 91151c6f666SRobin Holt long *zap_work, struct zap_details *details) 9121da177e4SLinus Torvalds { 913b5810039SNick Piggin struct mm_struct *mm = tlb->mm; 9141da177e4SLinus Torvalds pte_t *pte; 915508034a3SHugh Dickins spinlock_t *ptl; 916d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 917d559db08SKAMEZAWA Hiroyuki 918d559db08SKAMEZAWA Hiroyuki init_rss_vec(rss); 9191da177e4SLinus Torvalds 920508034a3SHugh Dickins pte = pte_offset_map_lock(mm, pmd, addr, &ptl); 9216606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 9221da177e4SLinus Torvalds do { 9231da177e4SLinus Torvalds pte_t ptent = *pte; 92451c6f666SRobin Holt if (pte_none(ptent)) { 92551c6f666SRobin Holt (*zap_work)--; 9261da177e4SLinus Torvalds continue; 92751c6f666SRobin Holt } 92851c6f666SRobin Holt 92951c6f666SRobin Holt (*zap_work) -= PAGE_SIZE; 93051c6f666SRobin Holt 9316f5e6b9eSHugh Dickins if (pte_present(ptent)) { 9326f5e6b9eSHugh Dickins struct page *page; 9336f5e6b9eSHugh Dickins 9346aab341eSLinus Torvalds page = vm_normal_page(vma, addr, ptent); 9351da177e4SLinus Torvalds if (unlikely(details) && page) { 9361da177e4SLinus Torvalds /* 9371da177e4SLinus Torvalds * unmap_shared_mapping_pages() wants to 9381da177e4SLinus Torvalds * invalidate cache without truncating: 9391da177e4SLinus Torvalds * unmap shared but keep private pages. 9401da177e4SLinus Torvalds */ 9411da177e4SLinus Torvalds if (details->check_mapping && 9421da177e4SLinus Torvalds details->check_mapping != page->mapping) 9431da177e4SLinus Torvalds continue; 9441da177e4SLinus Torvalds /* 9451da177e4SLinus Torvalds * Each page->index must be checked when 9461da177e4SLinus Torvalds * invalidating or truncating nonlinear. 9471da177e4SLinus Torvalds */ 9481da177e4SLinus Torvalds if (details->nonlinear_vma && 9491da177e4SLinus Torvalds (page->index < details->first_index || 9501da177e4SLinus Torvalds page->index > details->last_index)) 9511da177e4SLinus Torvalds continue; 9521da177e4SLinus Torvalds } 953b5810039SNick Piggin ptent = ptep_get_and_clear_full(mm, addr, pte, 954a600388dSZachary Amsden tlb->fullmm); 9551da177e4SLinus Torvalds tlb_remove_tlb_entry(tlb, pte, addr); 9561da177e4SLinus Torvalds if (unlikely(!page)) 9571da177e4SLinus Torvalds continue; 9581da177e4SLinus Torvalds if (unlikely(details) && details->nonlinear_vma 9591da177e4SLinus Torvalds && linear_page_index(details->nonlinear_vma, 9601da177e4SLinus Torvalds addr) != page->index) 961b5810039SNick Piggin set_pte_at(mm, addr, pte, 9621da177e4SLinus Torvalds pgoff_to_pte(page->index)); 9631da177e4SLinus Torvalds if (PageAnon(page)) 964d559db08SKAMEZAWA Hiroyuki rss[MM_ANONPAGES]--; 9656237bcd9SHugh Dickins else { 9666237bcd9SHugh Dickins if (pte_dirty(ptent)) 9676237bcd9SHugh Dickins set_page_dirty(page); 9684917e5d0SJohannes Weiner if (pte_young(ptent) && 9694917e5d0SJohannes Weiner likely(!VM_SequentialReadHint(vma))) 970bf3f3bc5SNick Piggin mark_page_accessed(page); 971d559db08SKAMEZAWA Hiroyuki rss[MM_FILEPAGES]--; 9726237bcd9SHugh Dickins } 973edc315fdSHugh Dickins page_remove_rmap(page); 9743dc14741SHugh Dickins if (unlikely(page_mapcount(page) < 0)) 9753dc14741SHugh Dickins print_bad_pte(vma, addr, ptent, page); 9761da177e4SLinus Torvalds tlb_remove_page(tlb, page); 9771da177e4SLinus Torvalds continue; 9781da177e4SLinus Torvalds } 9791da177e4SLinus Torvalds /* 9801da177e4SLinus Torvalds * If details->check_mapping, we leave swap entries; 9811da177e4SLinus Torvalds * if details->nonlinear_vma, we leave file entries. 9821da177e4SLinus Torvalds */ 9831da177e4SLinus Torvalds if (unlikely(details)) 9841da177e4SLinus Torvalds continue; 9852509ef26SHugh Dickins if (pte_file(ptent)) { 9862509ef26SHugh Dickins if (unlikely(!(vma->vm_flags & VM_NONLINEAR))) 9872509ef26SHugh Dickins print_bad_pte(vma, addr, ptent, NULL); 988b084d435SKAMEZAWA Hiroyuki } else { 989b084d435SKAMEZAWA Hiroyuki swp_entry_t entry = pte_to_swp_entry(ptent); 990b084d435SKAMEZAWA Hiroyuki 991b084d435SKAMEZAWA Hiroyuki if (!non_swap_entry(entry)) 992b084d435SKAMEZAWA Hiroyuki rss[MM_SWAPENTS]--; 993b084d435SKAMEZAWA Hiroyuki if (unlikely(!free_swap_and_cache(entry))) 9942509ef26SHugh Dickins print_bad_pte(vma, addr, ptent, NULL); 995b084d435SKAMEZAWA Hiroyuki } 9969888a1caSZachary Amsden pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); 99751c6f666SRobin Holt } while (pte++, addr += PAGE_SIZE, (addr != end && *zap_work > 0)); 998ae859762SHugh Dickins 999d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(mm, rss); 10006606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 1001508034a3SHugh Dickins pte_unmap_unlock(pte - 1, ptl); 100251c6f666SRobin Holt 100351c6f666SRobin Holt return addr; 10041da177e4SLinus Torvalds } 10051da177e4SLinus Torvalds 100651c6f666SRobin Holt static inline unsigned long zap_pmd_range(struct mmu_gather *tlb, 1007b5810039SNick Piggin struct vm_area_struct *vma, pud_t *pud, 10081da177e4SLinus Torvalds unsigned long addr, unsigned long end, 100951c6f666SRobin Holt long *zap_work, struct zap_details *details) 10101da177e4SLinus Torvalds { 10111da177e4SLinus Torvalds pmd_t *pmd; 10121da177e4SLinus Torvalds unsigned long next; 10131da177e4SLinus Torvalds 10141da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 10151da177e4SLinus Torvalds do { 10161da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 101771e3aac0SAndrea Arcangeli if (pmd_trans_huge(*pmd)) { 101871e3aac0SAndrea Arcangeli if (next-addr != HPAGE_PMD_SIZE) 101971e3aac0SAndrea Arcangeli split_huge_page_pmd(vma->vm_mm, pmd); 102071e3aac0SAndrea Arcangeli else if (zap_huge_pmd(tlb, vma, pmd)) { 102171e3aac0SAndrea Arcangeli (*zap_work)--; 102271e3aac0SAndrea Arcangeli continue; 102371e3aac0SAndrea Arcangeli } 102471e3aac0SAndrea Arcangeli /* fall through */ 102571e3aac0SAndrea Arcangeli } 102651c6f666SRobin Holt if (pmd_none_or_clear_bad(pmd)) { 102751c6f666SRobin Holt (*zap_work)--; 10281da177e4SLinus Torvalds continue; 102951c6f666SRobin Holt } 103051c6f666SRobin Holt next = zap_pte_range(tlb, vma, pmd, addr, next, 103151c6f666SRobin Holt zap_work, details); 103251c6f666SRobin Holt } while (pmd++, addr = next, (addr != end && *zap_work > 0)); 103351c6f666SRobin Holt 103451c6f666SRobin Holt return addr; 10351da177e4SLinus Torvalds } 10361da177e4SLinus Torvalds 103751c6f666SRobin Holt static inline unsigned long zap_pud_range(struct mmu_gather *tlb, 1038b5810039SNick Piggin struct vm_area_struct *vma, pgd_t *pgd, 10391da177e4SLinus Torvalds unsigned long addr, unsigned long end, 104051c6f666SRobin Holt long *zap_work, struct zap_details *details) 10411da177e4SLinus Torvalds { 10421da177e4SLinus Torvalds pud_t *pud; 10431da177e4SLinus Torvalds unsigned long next; 10441da177e4SLinus Torvalds 10451da177e4SLinus Torvalds pud = pud_offset(pgd, addr); 10461da177e4SLinus Torvalds do { 10471da177e4SLinus Torvalds next = pud_addr_end(addr, end); 104851c6f666SRobin Holt if (pud_none_or_clear_bad(pud)) { 104951c6f666SRobin Holt (*zap_work)--; 10501da177e4SLinus Torvalds continue; 105151c6f666SRobin Holt } 105251c6f666SRobin Holt next = zap_pmd_range(tlb, vma, pud, addr, next, 105351c6f666SRobin Holt zap_work, details); 105451c6f666SRobin Holt } while (pud++, addr = next, (addr != end && *zap_work > 0)); 105551c6f666SRobin Holt 105651c6f666SRobin Holt return addr; 10571da177e4SLinus Torvalds } 10581da177e4SLinus Torvalds 105951c6f666SRobin Holt static unsigned long unmap_page_range(struct mmu_gather *tlb, 106051c6f666SRobin Holt struct vm_area_struct *vma, 10611da177e4SLinus Torvalds unsigned long addr, unsigned long end, 106251c6f666SRobin Holt long *zap_work, struct zap_details *details) 10631da177e4SLinus Torvalds { 10641da177e4SLinus Torvalds pgd_t *pgd; 10651da177e4SLinus Torvalds unsigned long next; 10661da177e4SLinus Torvalds 10671da177e4SLinus Torvalds if (details && !details->check_mapping && !details->nonlinear_vma) 10681da177e4SLinus Torvalds details = NULL; 10691da177e4SLinus Torvalds 10701da177e4SLinus Torvalds BUG_ON(addr >= end); 1071569b846dSKAMEZAWA Hiroyuki mem_cgroup_uncharge_start(); 10721da177e4SLinus Torvalds tlb_start_vma(tlb, vma); 10731da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 10741da177e4SLinus Torvalds do { 10751da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 107651c6f666SRobin Holt if (pgd_none_or_clear_bad(pgd)) { 107751c6f666SRobin Holt (*zap_work)--; 10781da177e4SLinus Torvalds continue; 107951c6f666SRobin Holt } 108051c6f666SRobin Holt next = zap_pud_range(tlb, vma, pgd, addr, next, 108151c6f666SRobin Holt zap_work, details); 108251c6f666SRobin Holt } while (pgd++, addr = next, (addr != end && *zap_work > 0)); 10831da177e4SLinus Torvalds tlb_end_vma(tlb, vma); 1084569b846dSKAMEZAWA Hiroyuki mem_cgroup_uncharge_end(); 108551c6f666SRobin Holt 108651c6f666SRobin Holt return addr; 10871da177e4SLinus Torvalds } 10881da177e4SLinus Torvalds 10891da177e4SLinus Torvalds #ifdef CONFIG_PREEMPT 10901da177e4SLinus Torvalds # define ZAP_BLOCK_SIZE (8 * PAGE_SIZE) 10911da177e4SLinus Torvalds #else 10921da177e4SLinus Torvalds /* No preempt: go for improved straight-line efficiency */ 10931da177e4SLinus Torvalds # define ZAP_BLOCK_SIZE (1024 * PAGE_SIZE) 10941da177e4SLinus Torvalds #endif 10951da177e4SLinus Torvalds 10961da177e4SLinus Torvalds /** 10971da177e4SLinus Torvalds * unmap_vmas - unmap a range of memory covered by a list of vma's 10981da177e4SLinus Torvalds * @tlbp: address of the caller's struct mmu_gather 10991da177e4SLinus Torvalds * @vma: the starting vma 11001da177e4SLinus Torvalds * @start_addr: virtual address at which to start unmapping 11011da177e4SLinus Torvalds * @end_addr: virtual address at which to end unmapping 11021da177e4SLinus Torvalds * @nr_accounted: Place number of unmapped pages in vm-accountable vma's here 11031da177e4SLinus Torvalds * @details: details of nonlinear truncation or shared cache invalidation 11041da177e4SLinus Torvalds * 1105ee39b37bSHugh Dickins * Returns the end address of the unmapping (restart addr if interrupted). 11061da177e4SLinus Torvalds * 1107508034a3SHugh Dickins * Unmap all pages in the vma list. 11081da177e4SLinus Torvalds * 1109508034a3SHugh Dickins * We aim to not hold locks for too long (for scheduling latency reasons). 1110508034a3SHugh Dickins * So zap pages in ZAP_BLOCK_SIZE bytecounts. This means we need to 11111da177e4SLinus Torvalds * return the ending mmu_gather to the caller. 11121da177e4SLinus Torvalds * 11131da177e4SLinus Torvalds * Only addresses between `start' and `end' will be unmapped. 11141da177e4SLinus Torvalds * 11151da177e4SLinus Torvalds * The VMA list must be sorted in ascending virtual address order. 11161da177e4SLinus Torvalds * 11171da177e4SLinus Torvalds * unmap_vmas() assumes that the caller will flush the whole unmapped address 11181da177e4SLinus Torvalds * range after unmap_vmas() returns. So the only responsibility here is to 11191da177e4SLinus Torvalds * ensure that any thus-far unmapped pages are flushed before unmap_vmas() 11201da177e4SLinus Torvalds * drops the lock and schedules. 11211da177e4SLinus Torvalds */ 1122508034a3SHugh Dickins unsigned long unmap_vmas(struct mmu_gather **tlbp, 11231da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long start_addr, 11241da177e4SLinus Torvalds unsigned long end_addr, unsigned long *nr_accounted, 11251da177e4SLinus Torvalds struct zap_details *details) 11261da177e4SLinus Torvalds { 112751c6f666SRobin Holt long zap_work = ZAP_BLOCK_SIZE; 11281da177e4SLinus Torvalds unsigned long tlb_start = 0; /* For tlb_finish_mmu */ 11291da177e4SLinus Torvalds int tlb_start_valid = 0; 1130ee39b37bSHugh Dickins unsigned long start = start_addr; 11311da177e4SLinus Torvalds spinlock_t *i_mmap_lock = details? details->i_mmap_lock: NULL; 11324d6ddfa9SHugh Dickins int fullmm = (*tlbp)->fullmm; 1133cddb8a5cSAndrea Arcangeli struct mm_struct *mm = vma->vm_mm; 11341da177e4SLinus Torvalds 1135cddb8a5cSAndrea Arcangeli mmu_notifier_invalidate_range_start(mm, start_addr, end_addr); 11361da177e4SLinus Torvalds for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) { 11371da177e4SLinus Torvalds unsigned long end; 11381da177e4SLinus Torvalds 11391da177e4SLinus Torvalds start = max(vma->vm_start, start_addr); 11401da177e4SLinus Torvalds if (start >= vma->vm_end) 11411da177e4SLinus Torvalds continue; 11421da177e4SLinus Torvalds end = min(vma->vm_end, end_addr); 11431da177e4SLinus Torvalds if (end <= vma->vm_start) 11441da177e4SLinus Torvalds continue; 11451da177e4SLinus Torvalds 11461da177e4SLinus Torvalds if (vma->vm_flags & VM_ACCOUNT) 11471da177e4SLinus Torvalds *nr_accounted += (end - start) >> PAGE_SHIFT; 11481da177e4SLinus Torvalds 114934801ba9Svenkatesh.pallipadi@intel.com if (unlikely(is_pfn_mapping(vma))) 11502ab64037Svenkatesh.pallipadi@intel.com untrack_pfn_vma(vma, 0, 0); 11512ab64037Svenkatesh.pallipadi@intel.com 11521da177e4SLinus Torvalds while (start != end) { 11531da177e4SLinus Torvalds if (!tlb_start_valid) { 11541da177e4SLinus Torvalds tlb_start = start; 11551da177e4SLinus Torvalds tlb_start_valid = 1; 11561da177e4SLinus Torvalds } 11571da177e4SLinus Torvalds 115851c6f666SRobin Holt if (unlikely(is_vm_hugetlb_page(vma))) { 1159a137e1ccSAndi Kleen /* 1160a137e1ccSAndi Kleen * It is undesirable to test vma->vm_file as it 1161a137e1ccSAndi Kleen * should be non-null for valid hugetlb area. 1162a137e1ccSAndi Kleen * However, vm_file will be NULL in the error 1163a137e1ccSAndi Kleen * cleanup path of do_mmap_pgoff. When 1164a137e1ccSAndi Kleen * hugetlbfs ->mmap method fails, 1165a137e1ccSAndi Kleen * do_mmap_pgoff() nullifies vma->vm_file 1166a137e1ccSAndi Kleen * before calling this function to clean up. 1167a137e1ccSAndi Kleen * Since no pte has actually been setup, it is 1168a137e1ccSAndi Kleen * safe to do nothing in this case. 1169a137e1ccSAndi Kleen */ 1170a137e1ccSAndi Kleen if (vma->vm_file) { 117104f2cbe3SMel Gorman unmap_hugepage_range(vma, start, end, NULL); 117251c6f666SRobin Holt zap_work -= (end - start) / 1173a5516438SAndi Kleen pages_per_huge_page(hstate_vma(vma)); 1174a137e1ccSAndi Kleen } 1175a137e1ccSAndi Kleen 117651c6f666SRobin Holt start = end; 117751c6f666SRobin Holt } else 117851c6f666SRobin Holt start = unmap_page_range(*tlbp, vma, 117951c6f666SRobin Holt start, end, &zap_work, details); 11801da177e4SLinus Torvalds 118151c6f666SRobin Holt if (zap_work > 0) { 118251c6f666SRobin Holt BUG_ON(start != end); 118351c6f666SRobin Holt break; 118451c6f666SRobin Holt } 11851da177e4SLinus Torvalds 11861da177e4SLinus Torvalds tlb_finish_mmu(*tlbp, tlb_start, start); 11871da177e4SLinus Torvalds 11881da177e4SLinus Torvalds if (need_resched() || 118995c354feSNick Piggin (i_mmap_lock && spin_needbreak(i_mmap_lock))) { 11901da177e4SLinus Torvalds if (i_mmap_lock) { 1191508034a3SHugh Dickins *tlbp = NULL; 11921da177e4SLinus Torvalds goto out; 11931da177e4SLinus Torvalds } 11941da177e4SLinus Torvalds cond_resched(); 11951da177e4SLinus Torvalds } 11961da177e4SLinus Torvalds 1197508034a3SHugh Dickins *tlbp = tlb_gather_mmu(vma->vm_mm, fullmm); 11981da177e4SLinus Torvalds tlb_start_valid = 0; 119951c6f666SRobin Holt zap_work = ZAP_BLOCK_SIZE; 12001da177e4SLinus Torvalds } 12011da177e4SLinus Torvalds } 12021da177e4SLinus Torvalds out: 1203cddb8a5cSAndrea Arcangeli mmu_notifier_invalidate_range_end(mm, start_addr, end_addr); 1204ee39b37bSHugh Dickins return start; /* which is now the end (or restart) address */ 12051da177e4SLinus Torvalds } 12061da177e4SLinus Torvalds 12071da177e4SLinus Torvalds /** 12081da177e4SLinus Torvalds * zap_page_range - remove user pages in a given range 12091da177e4SLinus Torvalds * @vma: vm_area_struct holding the applicable pages 12101da177e4SLinus Torvalds * @address: starting address of pages to zap 12111da177e4SLinus Torvalds * @size: number of bytes to zap 12121da177e4SLinus Torvalds * @details: details of nonlinear truncation or shared cache invalidation 12131da177e4SLinus Torvalds */ 1214ee39b37bSHugh Dickins unsigned long zap_page_range(struct vm_area_struct *vma, unsigned long address, 12151da177e4SLinus Torvalds unsigned long size, struct zap_details *details) 12161da177e4SLinus Torvalds { 12171da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 12181da177e4SLinus Torvalds struct mmu_gather *tlb; 12191da177e4SLinus Torvalds unsigned long end = address + size; 12201da177e4SLinus Torvalds unsigned long nr_accounted = 0; 12211da177e4SLinus Torvalds 12221da177e4SLinus Torvalds lru_add_drain(); 12231da177e4SLinus Torvalds tlb = tlb_gather_mmu(mm, 0); 1224365e9c87SHugh Dickins update_hiwater_rss(mm); 1225508034a3SHugh Dickins end = unmap_vmas(&tlb, vma, address, end, &nr_accounted, details); 1226508034a3SHugh Dickins if (tlb) 12278f4f8c16SHugh Dickins tlb_finish_mmu(tlb, address, end); 1228ee39b37bSHugh Dickins return end; 12291da177e4SLinus Torvalds } 12301da177e4SLinus Torvalds 1231c627f9ccSJack Steiner /** 1232c627f9ccSJack Steiner * zap_vma_ptes - remove ptes mapping the vma 1233c627f9ccSJack Steiner * @vma: vm_area_struct holding ptes to be zapped 1234c627f9ccSJack Steiner * @address: starting address of pages to zap 1235c627f9ccSJack Steiner * @size: number of bytes to zap 1236c627f9ccSJack Steiner * 1237c627f9ccSJack Steiner * This function only unmaps ptes assigned to VM_PFNMAP vmas. 1238c627f9ccSJack Steiner * 1239c627f9ccSJack Steiner * The entire address range must be fully contained within the vma. 1240c627f9ccSJack Steiner * 1241c627f9ccSJack Steiner * Returns 0 if successful. 1242c627f9ccSJack Steiner */ 1243c627f9ccSJack Steiner int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, 1244c627f9ccSJack Steiner unsigned long size) 1245c627f9ccSJack Steiner { 1246c627f9ccSJack Steiner if (address < vma->vm_start || address + size > vma->vm_end || 1247c627f9ccSJack Steiner !(vma->vm_flags & VM_PFNMAP)) 1248c627f9ccSJack Steiner return -1; 1249c627f9ccSJack Steiner zap_page_range(vma, address, size, NULL); 1250c627f9ccSJack Steiner return 0; 1251c627f9ccSJack Steiner } 1252c627f9ccSJack Steiner EXPORT_SYMBOL_GPL(zap_vma_ptes); 1253c627f9ccSJack Steiner 1254142762bdSJohannes Weiner /** 1255142762bdSJohannes Weiner * follow_page - look up a page descriptor from a user-virtual address 1256142762bdSJohannes Weiner * @vma: vm_area_struct mapping @address 1257142762bdSJohannes Weiner * @address: virtual address to look up 1258142762bdSJohannes Weiner * @flags: flags modifying lookup behaviour 1259142762bdSJohannes Weiner * 1260142762bdSJohannes Weiner * @flags can have FOLL_ flags set, defined in <linux/mm.h> 1261142762bdSJohannes Weiner * 1262142762bdSJohannes Weiner * Returns the mapped (struct page *), %NULL if no mapping exists, or 1263142762bdSJohannes Weiner * an error pointer if there is a mapping to something not represented 1264142762bdSJohannes Weiner * by a page descriptor (see also vm_normal_page()). 12651da177e4SLinus Torvalds */ 12666aab341eSLinus Torvalds struct page *follow_page(struct vm_area_struct *vma, unsigned long address, 1267deceb6cdSHugh Dickins unsigned int flags) 12681da177e4SLinus Torvalds { 12691da177e4SLinus Torvalds pgd_t *pgd; 12701da177e4SLinus Torvalds pud_t *pud; 12711da177e4SLinus Torvalds pmd_t *pmd; 12721da177e4SLinus Torvalds pte_t *ptep, pte; 1273deceb6cdSHugh Dickins spinlock_t *ptl; 12741da177e4SLinus Torvalds struct page *page; 12756aab341eSLinus Torvalds struct mm_struct *mm = vma->vm_mm; 12761da177e4SLinus Torvalds 1277deceb6cdSHugh Dickins page = follow_huge_addr(mm, address, flags & FOLL_WRITE); 1278deceb6cdSHugh Dickins if (!IS_ERR(page)) { 1279deceb6cdSHugh Dickins BUG_ON(flags & FOLL_GET); 1280deceb6cdSHugh Dickins goto out; 1281deceb6cdSHugh Dickins } 12821da177e4SLinus Torvalds 1283deceb6cdSHugh Dickins page = NULL; 12841da177e4SLinus Torvalds pgd = pgd_offset(mm, address); 12851da177e4SLinus Torvalds if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) 1286deceb6cdSHugh Dickins goto no_page_table; 12871da177e4SLinus Torvalds 12881da177e4SLinus Torvalds pud = pud_offset(pgd, address); 1289ceb86879SAndi Kleen if (pud_none(*pud)) 1290ceb86879SAndi Kleen goto no_page_table; 12918a07651eSHugh Dickins if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) { 1292ceb86879SAndi Kleen BUG_ON(flags & FOLL_GET); 1293ceb86879SAndi Kleen page = follow_huge_pud(mm, address, pud, flags & FOLL_WRITE); 1294ceb86879SAndi Kleen goto out; 1295ceb86879SAndi Kleen } 1296ceb86879SAndi Kleen if (unlikely(pud_bad(*pud))) 1297deceb6cdSHugh Dickins goto no_page_table; 12981da177e4SLinus Torvalds 12991da177e4SLinus Torvalds pmd = pmd_offset(pud, address); 1300aeed5fceSHugh Dickins if (pmd_none(*pmd)) 1301deceb6cdSHugh Dickins goto no_page_table; 130271e3aac0SAndrea Arcangeli if (pmd_huge(*pmd) && vma->vm_flags & VM_HUGETLB) { 1303deceb6cdSHugh Dickins BUG_ON(flags & FOLL_GET); 1304deceb6cdSHugh Dickins page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE); 1305deceb6cdSHugh Dickins goto out; 1306deceb6cdSHugh Dickins } 130771e3aac0SAndrea Arcangeli if (pmd_trans_huge(*pmd)) { 130871e3aac0SAndrea Arcangeli spin_lock(&mm->page_table_lock); 130971e3aac0SAndrea Arcangeli if (likely(pmd_trans_huge(*pmd))) { 131071e3aac0SAndrea Arcangeli if (unlikely(pmd_trans_splitting(*pmd))) { 131171e3aac0SAndrea Arcangeli spin_unlock(&mm->page_table_lock); 131271e3aac0SAndrea Arcangeli wait_split_huge_page(vma->anon_vma, pmd); 131371e3aac0SAndrea Arcangeli } else { 131471e3aac0SAndrea Arcangeli page = follow_trans_huge_pmd(mm, address, 131571e3aac0SAndrea Arcangeli pmd, flags); 131671e3aac0SAndrea Arcangeli spin_unlock(&mm->page_table_lock); 131771e3aac0SAndrea Arcangeli goto out; 131871e3aac0SAndrea Arcangeli } 131971e3aac0SAndrea Arcangeli } else 132071e3aac0SAndrea Arcangeli spin_unlock(&mm->page_table_lock); 132171e3aac0SAndrea Arcangeli /* fall through */ 132271e3aac0SAndrea Arcangeli } 1323aeed5fceSHugh Dickins if (unlikely(pmd_bad(*pmd))) 1324aeed5fceSHugh Dickins goto no_page_table; 1325aeed5fceSHugh Dickins 1326deceb6cdSHugh Dickins ptep = pte_offset_map_lock(mm, pmd, address, &ptl); 13271da177e4SLinus Torvalds 13281da177e4SLinus Torvalds pte = *ptep; 1329deceb6cdSHugh Dickins if (!pte_present(pte)) 133089f5b7daSLinus Torvalds goto no_page; 1331deceb6cdSHugh Dickins if ((flags & FOLL_WRITE) && !pte_write(pte)) 1332deceb6cdSHugh Dickins goto unlock; 1333a13ea5b7SHugh Dickins 13346aab341eSLinus Torvalds page = vm_normal_page(vma, address, pte); 1335a13ea5b7SHugh Dickins if (unlikely(!page)) { 1336a13ea5b7SHugh Dickins if ((flags & FOLL_DUMP) || 133762eede62SHugh Dickins !is_zero_pfn(pte_pfn(pte))) 133889f5b7daSLinus Torvalds goto bad_page; 1339a13ea5b7SHugh Dickins page = pte_page(pte); 1340a13ea5b7SHugh Dickins } 1341deceb6cdSHugh Dickins 1342deceb6cdSHugh Dickins if (flags & FOLL_GET) 1343deceb6cdSHugh Dickins get_page(page); 1344deceb6cdSHugh Dickins if (flags & FOLL_TOUCH) { 1345deceb6cdSHugh Dickins if ((flags & FOLL_WRITE) && 1346deceb6cdSHugh Dickins !pte_dirty(pte) && !PageDirty(page)) 1347f33ea7f4SNick Piggin set_page_dirty(page); 1348bd775c42SKOSAKI Motohiro /* 1349bd775c42SKOSAKI Motohiro * pte_mkyoung() would be more correct here, but atomic care 1350bd775c42SKOSAKI Motohiro * is needed to avoid losing the dirty bit: it is easier to use 1351bd775c42SKOSAKI Motohiro * mark_page_accessed(). 1352bd775c42SKOSAKI Motohiro */ 13531da177e4SLinus Torvalds mark_page_accessed(page); 13541da177e4SLinus Torvalds } 1355110d74a9SMichel Lespinasse if (flags & FOLL_MLOCK) { 1356110d74a9SMichel Lespinasse /* 1357110d74a9SMichel Lespinasse * The preliminary mapping check is mainly to avoid the 1358110d74a9SMichel Lespinasse * pointless overhead of lock_page on the ZERO_PAGE 1359110d74a9SMichel Lespinasse * which might bounce very badly if there is contention. 1360110d74a9SMichel Lespinasse * 1361110d74a9SMichel Lespinasse * If the page is already locked, we don't need to 1362110d74a9SMichel Lespinasse * handle it now - vmscan will handle it later if and 1363110d74a9SMichel Lespinasse * when it attempts to reclaim the page. 1364110d74a9SMichel Lespinasse */ 1365110d74a9SMichel Lespinasse if (page->mapping && trylock_page(page)) { 1366110d74a9SMichel Lespinasse lru_add_drain(); /* push cached pages to LRU */ 1367110d74a9SMichel Lespinasse /* 1368110d74a9SMichel Lespinasse * Because we lock page here and migration is 1369110d74a9SMichel Lespinasse * blocked by the pte's page reference, we need 1370110d74a9SMichel Lespinasse * only check for file-cache page truncation. 1371110d74a9SMichel Lespinasse */ 1372110d74a9SMichel Lespinasse if (page->mapping) 1373110d74a9SMichel Lespinasse mlock_vma_page(page); 1374110d74a9SMichel Lespinasse unlock_page(page); 1375110d74a9SMichel Lespinasse } 1376110d74a9SMichel Lespinasse } 1377deceb6cdSHugh Dickins unlock: 1378deceb6cdSHugh Dickins pte_unmap_unlock(ptep, ptl); 13791da177e4SLinus Torvalds out: 1380deceb6cdSHugh Dickins return page; 1381deceb6cdSHugh Dickins 138289f5b7daSLinus Torvalds bad_page: 138389f5b7daSLinus Torvalds pte_unmap_unlock(ptep, ptl); 138489f5b7daSLinus Torvalds return ERR_PTR(-EFAULT); 138589f5b7daSLinus Torvalds 138689f5b7daSLinus Torvalds no_page: 138789f5b7daSLinus Torvalds pte_unmap_unlock(ptep, ptl); 138889f5b7daSLinus Torvalds if (!pte_none(pte)) 138989f5b7daSLinus Torvalds return page; 13908e4b9a60SHugh Dickins 1391deceb6cdSHugh Dickins no_page_table: 1392deceb6cdSHugh Dickins /* 1393deceb6cdSHugh Dickins * When core dumping an enormous anonymous area that nobody 13948e4b9a60SHugh Dickins * has touched so far, we don't want to allocate unnecessary pages or 13958e4b9a60SHugh Dickins * page tables. Return error instead of NULL to skip handle_mm_fault, 13968e4b9a60SHugh Dickins * then get_dump_page() will return NULL to leave a hole in the dump. 13978e4b9a60SHugh Dickins * But we can only make this optimization where a hole would surely 13988e4b9a60SHugh Dickins * be zero-filled if handle_mm_fault() actually did handle it. 1399deceb6cdSHugh Dickins */ 14008e4b9a60SHugh Dickins if ((flags & FOLL_DUMP) && 14018e4b9a60SHugh Dickins (!vma->vm_ops || !vma->vm_ops->fault)) 14028e4b9a60SHugh Dickins return ERR_PTR(-EFAULT); 1403deceb6cdSHugh Dickins return page; 14041da177e4SLinus Torvalds } 14051da177e4SLinus Torvalds 1406b291f000SNick Piggin int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm, 140758fa879eSHugh Dickins unsigned long start, int nr_pages, unsigned int gup_flags, 140853a7706dSMichel Lespinasse struct page **pages, struct vm_area_struct **vmas, 140953a7706dSMichel Lespinasse int *nonblocking) 14101da177e4SLinus Torvalds { 14111da177e4SLinus Torvalds int i; 141258fa879eSHugh Dickins unsigned long vm_flags; 14131da177e4SLinus Torvalds 14149d73777eSPeter Zijlstra if (nr_pages <= 0) 1415900cf086SJonathan Corbet return 0; 141658fa879eSHugh Dickins 141758fa879eSHugh Dickins VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET)); 141858fa879eSHugh Dickins 14191da177e4SLinus Torvalds /* 14201da177e4SLinus Torvalds * Require read or write permissions. 142158fa879eSHugh Dickins * If FOLL_FORCE is set, we only require the "MAY" flags. 14221da177e4SLinus Torvalds */ 142358fa879eSHugh Dickins vm_flags = (gup_flags & FOLL_WRITE) ? 142458fa879eSHugh Dickins (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD); 142558fa879eSHugh Dickins vm_flags &= (gup_flags & FOLL_FORCE) ? 142658fa879eSHugh Dickins (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE); 14271da177e4SLinus Torvalds i = 0; 14281da177e4SLinus Torvalds 14291da177e4SLinus Torvalds do { 14301da177e4SLinus Torvalds struct vm_area_struct *vma; 14311da177e4SLinus Torvalds 14321da177e4SLinus Torvalds vma = find_extend_vma(mm, start); 14331da177e4SLinus Torvalds if (!vma && in_gate_area(tsk, start)) { 14341da177e4SLinus Torvalds unsigned long pg = start & PAGE_MASK; 14351da177e4SLinus Torvalds struct vm_area_struct *gate_vma = get_gate_vma(tsk); 14361da177e4SLinus Torvalds pgd_t *pgd; 14371da177e4SLinus Torvalds pud_t *pud; 14381da177e4SLinus Torvalds pmd_t *pmd; 14391da177e4SLinus Torvalds pte_t *pte; 1440b291f000SNick Piggin 1441b291f000SNick Piggin /* user gate pages are read-only */ 144258fa879eSHugh Dickins if (gup_flags & FOLL_WRITE) 14431da177e4SLinus Torvalds return i ? : -EFAULT; 14441da177e4SLinus Torvalds if (pg > TASK_SIZE) 14451da177e4SLinus Torvalds pgd = pgd_offset_k(pg); 14461da177e4SLinus Torvalds else 14471da177e4SLinus Torvalds pgd = pgd_offset_gate(mm, pg); 14481da177e4SLinus Torvalds BUG_ON(pgd_none(*pgd)); 14491da177e4SLinus Torvalds pud = pud_offset(pgd, pg); 14501da177e4SLinus Torvalds BUG_ON(pud_none(*pud)); 14511da177e4SLinus Torvalds pmd = pmd_offset(pud, pg); 1452690dbe1cSHugh Dickins if (pmd_none(*pmd)) 1453690dbe1cSHugh Dickins return i ? : -EFAULT; 1454*f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 14551da177e4SLinus Torvalds pte = pte_offset_map(pmd, pg); 1456690dbe1cSHugh Dickins if (pte_none(*pte)) { 1457690dbe1cSHugh Dickins pte_unmap(pte); 1458690dbe1cSHugh Dickins return i ? : -EFAULT; 1459690dbe1cSHugh Dickins } 14601da177e4SLinus Torvalds if (pages) { 1461de51257aSHugh Dickins struct page *page; 1462de51257aSHugh Dickins 1463de51257aSHugh Dickins page = vm_normal_page(gate_vma, start, *pte); 1464de51257aSHugh Dickins if (!page) { 1465de51257aSHugh Dickins if (!(gup_flags & FOLL_DUMP) && 1466de51257aSHugh Dickins is_zero_pfn(pte_pfn(*pte))) 1467de51257aSHugh Dickins page = pte_page(*pte); 1468de51257aSHugh Dickins else { 1469de51257aSHugh Dickins pte_unmap(pte); 1470de51257aSHugh Dickins return i ? : -EFAULT; 1471de51257aSHugh Dickins } 1472de51257aSHugh Dickins } 14736aab341eSLinus Torvalds pages[i] = page; 14746aab341eSLinus Torvalds get_page(page); 14751da177e4SLinus Torvalds } 14761da177e4SLinus Torvalds pte_unmap(pte); 14771da177e4SLinus Torvalds if (vmas) 14781da177e4SLinus Torvalds vmas[i] = gate_vma; 14791da177e4SLinus Torvalds i++; 14801da177e4SLinus Torvalds start += PAGE_SIZE; 14819d73777eSPeter Zijlstra nr_pages--; 14821da177e4SLinus Torvalds continue; 14831da177e4SLinus Torvalds } 14841da177e4SLinus Torvalds 1485b291f000SNick Piggin if (!vma || 1486b291f000SNick Piggin (vma->vm_flags & (VM_IO | VM_PFNMAP)) || 14871c3aff1cSHugh Dickins !(vm_flags & vma->vm_flags)) 14881da177e4SLinus Torvalds return i ? : -EFAULT; 14891da177e4SLinus Torvalds 14902a15efc9SHugh Dickins if (is_vm_hugetlb_page(vma)) { 14912a15efc9SHugh Dickins i = follow_hugetlb_page(mm, vma, pages, vmas, 149258fa879eSHugh Dickins &start, &nr_pages, i, gup_flags); 14932a15efc9SHugh Dickins continue; 14942a15efc9SHugh Dickins } 1495deceb6cdSHugh Dickins 14961da177e4SLinus Torvalds do { 149708ef4729SHugh Dickins struct page *page; 149858fa879eSHugh Dickins unsigned int foll_flags = gup_flags; 14991da177e4SLinus Torvalds 1500462e00ccSEthan Solomita /* 15014779280dSYing Han * If we have a pending SIGKILL, don't keep faulting 15021c3aff1cSHugh Dickins * pages and potentially allocating memory. 1503462e00ccSEthan Solomita */ 15041c3aff1cSHugh Dickins if (unlikely(fatal_signal_pending(current))) 15054779280dSYing Han return i ? i : -ERESTARTSYS; 1506462e00ccSEthan Solomita 1507deceb6cdSHugh Dickins cond_resched(); 15086aab341eSLinus Torvalds while (!(page = follow_page(vma, start, foll_flags))) { 1509a68d2ebcSLinus Torvalds int ret; 151053a7706dSMichel Lespinasse unsigned int fault_flags = 0; 151153a7706dSMichel Lespinasse 151253a7706dSMichel Lespinasse if (foll_flags & FOLL_WRITE) 151353a7706dSMichel Lespinasse fault_flags |= FAULT_FLAG_WRITE; 151453a7706dSMichel Lespinasse if (nonblocking) 151553a7706dSMichel Lespinasse fault_flags |= FAULT_FLAG_ALLOW_RETRY; 1516d06063ccSLinus Torvalds 1517d26ed650SHugh Dickins ret = handle_mm_fault(mm, vma, start, 151853a7706dSMichel Lespinasse fault_flags); 1519d26ed650SHugh Dickins 152083c54070SNick Piggin if (ret & VM_FAULT_ERROR) { 152183c54070SNick Piggin if (ret & VM_FAULT_OOM) 152283c54070SNick Piggin return i ? i : -ENOMEM; 1523d1737fdbSAndi Kleen if (ret & 1524aa50d3a7SAndi Kleen (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE| 1525aa50d3a7SAndi Kleen VM_FAULT_SIGBUS)) 152683c54070SNick Piggin return i ? i : -EFAULT; 152783c54070SNick Piggin BUG(); 152883c54070SNick Piggin } 152983c54070SNick Piggin if (ret & VM_FAULT_MAJOR) 153083c54070SNick Piggin tsk->maj_flt++; 153183c54070SNick Piggin else 153283c54070SNick Piggin tsk->min_flt++; 153383c54070SNick Piggin 153453a7706dSMichel Lespinasse if (ret & VM_FAULT_RETRY) { 153553a7706dSMichel Lespinasse *nonblocking = 0; 153653a7706dSMichel Lespinasse return i; 153753a7706dSMichel Lespinasse } 153853a7706dSMichel Lespinasse 1539f33ea7f4SNick Piggin /* 154083c54070SNick Piggin * The VM_FAULT_WRITE bit tells us that 154183c54070SNick Piggin * do_wp_page has broken COW when necessary, 154283c54070SNick Piggin * even if maybe_mkwrite decided not to set 154383c54070SNick Piggin * pte_write. We can thus safely do subsequent 1544878b63acSHugh Dickins * page lookups as if they were reads. But only 1545878b63acSHugh Dickins * do so when looping for pte_write is futile: 1546878b63acSHugh Dickins * in some cases userspace may also be wanting 1547878b63acSHugh Dickins * to write to the gotten user page, which a 1548878b63acSHugh Dickins * read fault here might prevent (a readonly 1549878b63acSHugh Dickins * page might get reCOWed by userspace write). 1550f33ea7f4SNick Piggin */ 1551878b63acSHugh Dickins if ((ret & VM_FAULT_WRITE) && 1552878b63acSHugh Dickins !(vma->vm_flags & VM_WRITE)) 1553deceb6cdSHugh Dickins foll_flags &= ~FOLL_WRITE; 1554a68d2ebcSLinus Torvalds 15557f7bbbe5SBenjamin Herrenschmidt cond_resched(); 15561da177e4SLinus Torvalds } 155789f5b7daSLinus Torvalds if (IS_ERR(page)) 155889f5b7daSLinus Torvalds return i ? i : PTR_ERR(page); 15591da177e4SLinus Torvalds if (pages) { 156008ef4729SHugh Dickins pages[i] = page; 156103beb076SJames Bottomley 1562a6f36be3SRussell King flush_anon_page(vma, page, start); 156308ef4729SHugh Dickins flush_dcache_page(page); 15641da177e4SLinus Torvalds } 15651da177e4SLinus Torvalds if (vmas) 15661da177e4SLinus Torvalds vmas[i] = vma; 15671da177e4SLinus Torvalds i++; 15681da177e4SLinus Torvalds start += PAGE_SIZE; 15699d73777eSPeter Zijlstra nr_pages--; 15709d73777eSPeter Zijlstra } while (nr_pages && start < vma->vm_end); 15719d73777eSPeter Zijlstra } while (nr_pages); 15721da177e4SLinus Torvalds return i; 15731da177e4SLinus Torvalds } 1574b291f000SNick Piggin 1575d2bf6be8SNick Piggin /** 1576d2bf6be8SNick Piggin * get_user_pages() - pin user pages in memory 1577d2bf6be8SNick Piggin * @tsk: task_struct of target task 1578d2bf6be8SNick Piggin * @mm: mm_struct of target mm 1579d2bf6be8SNick Piggin * @start: starting user address 15809d73777eSPeter Zijlstra * @nr_pages: number of pages from start to pin 1581d2bf6be8SNick Piggin * @write: whether pages will be written to by the caller 1582d2bf6be8SNick Piggin * @force: whether to force write access even if user mapping is 1583d2bf6be8SNick Piggin * readonly. This will result in the page being COWed even 1584d2bf6be8SNick Piggin * in MAP_SHARED mappings. You do not want this. 1585d2bf6be8SNick Piggin * @pages: array that receives pointers to the pages pinned. 1586d2bf6be8SNick Piggin * Should be at least nr_pages long. Or NULL, if caller 1587d2bf6be8SNick Piggin * only intends to ensure the pages are faulted in. 1588d2bf6be8SNick Piggin * @vmas: array of pointers to vmas corresponding to each page. 1589d2bf6be8SNick Piggin * Or NULL if the caller does not require them. 1590d2bf6be8SNick Piggin * 1591d2bf6be8SNick Piggin * Returns number of pages pinned. This may be fewer than the number 15929d73777eSPeter Zijlstra * requested. If nr_pages is 0 or negative, returns 0. If no pages 1593d2bf6be8SNick Piggin * were pinned, returns -errno. Each page returned must be released 1594d2bf6be8SNick Piggin * with a put_page() call when it is finished with. vmas will only 1595d2bf6be8SNick Piggin * remain valid while mmap_sem is held. 1596d2bf6be8SNick Piggin * 1597d2bf6be8SNick Piggin * Must be called with mmap_sem held for read or write. 1598d2bf6be8SNick Piggin * 1599d2bf6be8SNick Piggin * get_user_pages walks a process's page tables and takes a reference to 1600d2bf6be8SNick Piggin * each struct page that each user address corresponds to at a given 1601d2bf6be8SNick Piggin * instant. That is, it takes the page that would be accessed if a user 1602d2bf6be8SNick Piggin * thread accesses the given user virtual address at that instant. 1603d2bf6be8SNick Piggin * 1604d2bf6be8SNick Piggin * This does not guarantee that the page exists in the user mappings when 1605d2bf6be8SNick Piggin * get_user_pages returns, and there may even be a completely different 1606d2bf6be8SNick Piggin * page there in some cases (eg. if mmapped pagecache has been invalidated 1607d2bf6be8SNick Piggin * and subsequently re faulted). However it does guarantee that the page 1608d2bf6be8SNick Piggin * won't be freed completely. And mostly callers simply care that the page 1609d2bf6be8SNick Piggin * contains data that was valid *at some point in time*. Typically, an IO 1610d2bf6be8SNick Piggin * or similar operation cannot guarantee anything stronger anyway because 1611d2bf6be8SNick Piggin * locks can't be held over the syscall boundary. 1612d2bf6be8SNick Piggin * 1613d2bf6be8SNick Piggin * If write=0, the page must not be written to. If the page is written to, 1614d2bf6be8SNick Piggin * set_page_dirty (or set_page_dirty_lock, as appropriate) must be called 1615d2bf6be8SNick Piggin * after the page is finished with, and before put_page is called. 1616d2bf6be8SNick Piggin * 1617d2bf6be8SNick Piggin * get_user_pages is typically used for fewer-copy IO operations, to get a 1618d2bf6be8SNick Piggin * handle on the memory by some means other than accesses via the user virtual 1619d2bf6be8SNick Piggin * addresses. The pages may be submitted for DMA to devices or accessed via 1620d2bf6be8SNick Piggin * their kernel linear mapping (via the kmap APIs). Care should be taken to 1621d2bf6be8SNick Piggin * use the correct cache flushing APIs. 1622d2bf6be8SNick Piggin * 1623d2bf6be8SNick Piggin * See also get_user_pages_fast, for performance critical applications. 1624d2bf6be8SNick Piggin */ 1625b291f000SNick Piggin int get_user_pages(struct task_struct *tsk, struct mm_struct *mm, 16269d73777eSPeter Zijlstra unsigned long start, int nr_pages, int write, int force, 1627b291f000SNick Piggin struct page **pages, struct vm_area_struct **vmas) 1628b291f000SNick Piggin { 162958fa879eSHugh Dickins int flags = FOLL_TOUCH; 1630b291f000SNick Piggin 163158fa879eSHugh Dickins if (pages) 163258fa879eSHugh Dickins flags |= FOLL_GET; 1633b291f000SNick Piggin if (write) 163458fa879eSHugh Dickins flags |= FOLL_WRITE; 1635b291f000SNick Piggin if (force) 163658fa879eSHugh Dickins flags |= FOLL_FORCE; 1637b291f000SNick Piggin 163853a7706dSMichel Lespinasse return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas, 163953a7706dSMichel Lespinasse NULL); 1640b291f000SNick Piggin } 16411da177e4SLinus Torvalds EXPORT_SYMBOL(get_user_pages); 16421da177e4SLinus Torvalds 1643f3e8fccdSHugh Dickins /** 1644f3e8fccdSHugh Dickins * get_dump_page() - pin user page in memory while writing it to core dump 1645f3e8fccdSHugh Dickins * @addr: user address 1646f3e8fccdSHugh Dickins * 1647f3e8fccdSHugh Dickins * Returns struct page pointer of user page pinned for dump, 1648f3e8fccdSHugh Dickins * to be freed afterwards by page_cache_release() or put_page(). 1649f3e8fccdSHugh Dickins * 1650f3e8fccdSHugh Dickins * Returns NULL on any kind of failure - a hole must then be inserted into 1651f3e8fccdSHugh Dickins * the corefile, to preserve alignment with its headers; and also returns 1652f3e8fccdSHugh Dickins * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found - 1653f3e8fccdSHugh Dickins * allowing a hole to be left in the corefile to save diskspace. 1654f3e8fccdSHugh Dickins * 1655f3e8fccdSHugh Dickins * Called without mmap_sem, but after all other threads have been killed. 1656f3e8fccdSHugh Dickins */ 1657f3e8fccdSHugh Dickins #ifdef CONFIG_ELF_CORE 1658f3e8fccdSHugh Dickins struct page *get_dump_page(unsigned long addr) 1659f3e8fccdSHugh Dickins { 1660f3e8fccdSHugh Dickins struct vm_area_struct *vma; 1661f3e8fccdSHugh Dickins struct page *page; 1662f3e8fccdSHugh Dickins 1663f3e8fccdSHugh Dickins if (__get_user_pages(current, current->mm, addr, 1, 166453a7706dSMichel Lespinasse FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma, 166553a7706dSMichel Lespinasse NULL) < 1) 1666f3e8fccdSHugh Dickins return NULL; 1667f3e8fccdSHugh Dickins flush_cache_page(vma, addr, page_to_pfn(page)); 1668f3e8fccdSHugh Dickins return page; 1669f3e8fccdSHugh Dickins } 1670f3e8fccdSHugh Dickins #endif /* CONFIG_ELF_CORE */ 1671f3e8fccdSHugh Dickins 167225ca1d6cSNamhyung Kim pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr, 1673920c7a5dSHarvey Harrison spinlock_t **ptl) 1674c9cfcddfSLinus Torvalds { 1675c9cfcddfSLinus Torvalds pgd_t * pgd = pgd_offset(mm, addr); 1676c9cfcddfSLinus Torvalds pud_t * pud = pud_alloc(mm, pgd, addr); 1677c9cfcddfSLinus Torvalds if (pud) { 167849c91fb0STrond Myklebust pmd_t * pmd = pmd_alloc(mm, pud, addr); 1679*f66055abSAndrea Arcangeli if (pmd) { 1680*f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 1681c9cfcddfSLinus Torvalds return pte_alloc_map_lock(mm, pmd, addr, ptl); 1682c9cfcddfSLinus Torvalds } 1683*f66055abSAndrea Arcangeli } 1684c9cfcddfSLinus Torvalds return NULL; 1685c9cfcddfSLinus Torvalds } 1686c9cfcddfSLinus Torvalds 16871da177e4SLinus Torvalds /* 1688238f58d8SLinus Torvalds * This is the old fallback for page remapping. 1689238f58d8SLinus Torvalds * 1690238f58d8SLinus Torvalds * For historical reasons, it only allows reserved pages. Only 1691238f58d8SLinus Torvalds * old drivers should use this, and they needed to mark their 1692238f58d8SLinus Torvalds * pages reserved for the old functions anyway. 1693238f58d8SLinus Torvalds */ 1694423bad60SNick Piggin static int insert_page(struct vm_area_struct *vma, unsigned long addr, 1695423bad60SNick Piggin struct page *page, pgprot_t prot) 1696238f58d8SLinus Torvalds { 1697423bad60SNick Piggin struct mm_struct *mm = vma->vm_mm; 1698238f58d8SLinus Torvalds int retval; 1699238f58d8SLinus Torvalds pte_t *pte; 1700238f58d8SLinus Torvalds spinlock_t *ptl; 1701238f58d8SLinus Torvalds 1702238f58d8SLinus Torvalds retval = -EINVAL; 1703a145dd41SLinus Torvalds if (PageAnon(page)) 17045b4e655eSKAMEZAWA Hiroyuki goto out; 1705238f58d8SLinus Torvalds retval = -ENOMEM; 1706238f58d8SLinus Torvalds flush_dcache_page(page); 1707c9cfcddfSLinus Torvalds pte = get_locked_pte(mm, addr, &ptl); 1708238f58d8SLinus Torvalds if (!pte) 17095b4e655eSKAMEZAWA Hiroyuki goto out; 1710238f58d8SLinus Torvalds retval = -EBUSY; 1711238f58d8SLinus Torvalds if (!pte_none(*pte)) 1712238f58d8SLinus Torvalds goto out_unlock; 1713238f58d8SLinus Torvalds 1714238f58d8SLinus Torvalds /* Ok, finally just insert the thing.. */ 1715238f58d8SLinus Torvalds get_page(page); 171634e55232SKAMEZAWA Hiroyuki inc_mm_counter_fast(mm, MM_FILEPAGES); 1717238f58d8SLinus Torvalds page_add_file_rmap(page); 1718238f58d8SLinus Torvalds set_pte_at(mm, addr, pte, mk_pte(page, prot)); 1719238f58d8SLinus Torvalds 1720238f58d8SLinus Torvalds retval = 0; 17218a9f3ccdSBalbir Singh pte_unmap_unlock(pte, ptl); 17228a9f3ccdSBalbir Singh return retval; 1723238f58d8SLinus Torvalds out_unlock: 1724238f58d8SLinus Torvalds pte_unmap_unlock(pte, ptl); 1725238f58d8SLinus Torvalds out: 1726238f58d8SLinus Torvalds return retval; 1727238f58d8SLinus Torvalds } 1728238f58d8SLinus Torvalds 1729bfa5bf6dSRolf Eike Beer /** 1730bfa5bf6dSRolf Eike Beer * vm_insert_page - insert single page into user vma 1731bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 1732bfa5bf6dSRolf Eike Beer * @addr: target user address of this page 1733bfa5bf6dSRolf Eike Beer * @page: source kernel page 1734bfa5bf6dSRolf Eike Beer * 1735a145dd41SLinus Torvalds * This allows drivers to insert individual pages they've allocated 1736a145dd41SLinus Torvalds * into a user vma. 1737a145dd41SLinus Torvalds * 1738a145dd41SLinus Torvalds * The page has to be a nice clean _individual_ kernel allocation. 1739a145dd41SLinus Torvalds * If you allocate a compound page, you need to have marked it as 1740a145dd41SLinus Torvalds * such (__GFP_COMP), or manually just split the page up yourself 17418dfcc9baSNick Piggin * (see split_page()). 1742a145dd41SLinus Torvalds * 1743a145dd41SLinus Torvalds * NOTE! Traditionally this was done with "remap_pfn_range()" which 1744a145dd41SLinus Torvalds * took an arbitrary page protection parameter. This doesn't allow 1745a145dd41SLinus Torvalds * that. Your vma protection will have to be set up correctly, which 1746a145dd41SLinus Torvalds * means that if you want a shared writable mapping, you'd better 1747a145dd41SLinus Torvalds * ask for a shared writable mapping! 1748a145dd41SLinus Torvalds * 1749a145dd41SLinus Torvalds * The page does not need to be reserved. 1750a145dd41SLinus Torvalds */ 1751423bad60SNick Piggin int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, 1752423bad60SNick Piggin struct page *page) 1753a145dd41SLinus Torvalds { 1754a145dd41SLinus Torvalds if (addr < vma->vm_start || addr >= vma->vm_end) 1755a145dd41SLinus Torvalds return -EFAULT; 1756a145dd41SLinus Torvalds if (!page_count(page)) 1757a145dd41SLinus Torvalds return -EINVAL; 17584d7672b4SLinus Torvalds vma->vm_flags |= VM_INSERTPAGE; 1759423bad60SNick Piggin return insert_page(vma, addr, page, vma->vm_page_prot); 1760a145dd41SLinus Torvalds } 1761e3c3374fSLinus Torvalds EXPORT_SYMBOL(vm_insert_page); 1762a145dd41SLinus Torvalds 1763423bad60SNick Piggin static int insert_pfn(struct vm_area_struct *vma, unsigned long addr, 1764423bad60SNick Piggin unsigned long pfn, pgprot_t prot) 1765423bad60SNick Piggin { 1766423bad60SNick Piggin struct mm_struct *mm = vma->vm_mm; 1767423bad60SNick Piggin int retval; 1768423bad60SNick Piggin pte_t *pte, entry; 1769423bad60SNick Piggin spinlock_t *ptl; 1770423bad60SNick Piggin 1771423bad60SNick Piggin retval = -ENOMEM; 1772423bad60SNick Piggin pte = get_locked_pte(mm, addr, &ptl); 1773423bad60SNick Piggin if (!pte) 1774423bad60SNick Piggin goto out; 1775423bad60SNick Piggin retval = -EBUSY; 1776423bad60SNick Piggin if (!pte_none(*pte)) 1777423bad60SNick Piggin goto out_unlock; 1778423bad60SNick Piggin 1779423bad60SNick Piggin /* Ok, finally just insert the thing.. */ 1780423bad60SNick Piggin entry = pte_mkspecial(pfn_pte(pfn, prot)); 1781423bad60SNick Piggin set_pte_at(mm, addr, pte, entry); 17824b3073e1SRussell King update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */ 1783423bad60SNick Piggin 1784423bad60SNick Piggin retval = 0; 1785423bad60SNick Piggin out_unlock: 1786423bad60SNick Piggin pte_unmap_unlock(pte, ptl); 1787423bad60SNick Piggin out: 1788423bad60SNick Piggin return retval; 1789423bad60SNick Piggin } 1790423bad60SNick Piggin 1791e0dc0d8fSNick Piggin /** 1792e0dc0d8fSNick Piggin * vm_insert_pfn - insert single pfn into user vma 1793e0dc0d8fSNick Piggin * @vma: user vma to map to 1794e0dc0d8fSNick Piggin * @addr: target user address of this page 1795e0dc0d8fSNick Piggin * @pfn: source kernel pfn 1796e0dc0d8fSNick Piggin * 1797e0dc0d8fSNick Piggin * Similar to vm_inert_page, this allows drivers to insert individual pages 1798e0dc0d8fSNick Piggin * they've allocated into a user vma. Same comments apply. 1799e0dc0d8fSNick Piggin * 1800e0dc0d8fSNick Piggin * This function should only be called from a vm_ops->fault handler, and 1801e0dc0d8fSNick Piggin * in that case the handler should return NULL. 18020d71d10aSNick Piggin * 18030d71d10aSNick Piggin * vma cannot be a COW mapping. 18040d71d10aSNick Piggin * 18050d71d10aSNick Piggin * As this is called only for pages that do not currently exist, we 18060d71d10aSNick Piggin * do not need to flush old virtual caches or the TLB. 1807e0dc0d8fSNick Piggin */ 1808e0dc0d8fSNick Piggin int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr, 1809e0dc0d8fSNick Piggin unsigned long pfn) 1810e0dc0d8fSNick Piggin { 18112ab64037Svenkatesh.pallipadi@intel.com int ret; 1812e4b866edSvenkatesh.pallipadi@intel.com pgprot_t pgprot = vma->vm_page_prot; 18137e675137SNick Piggin /* 18147e675137SNick Piggin * Technically, architectures with pte_special can avoid all these 18157e675137SNick Piggin * restrictions (same for remap_pfn_range). However we would like 18167e675137SNick Piggin * consistency in testing and feature parity among all, so we should 18177e675137SNick Piggin * try to keep these invariants in place for everybody. 18187e675137SNick Piggin */ 1819b379d790SJared Hulbert BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))); 1820b379d790SJared Hulbert BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) == 1821b379d790SJared Hulbert (VM_PFNMAP|VM_MIXEDMAP)); 1822b379d790SJared Hulbert BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); 1823b379d790SJared Hulbert BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn)); 1824e0dc0d8fSNick Piggin 1825423bad60SNick Piggin if (addr < vma->vm_start || addr >= vma->vm_end) 1826423bad60SNick Piggin return -EFAULT; 1827e4b866edSvenkatesh.pallipadi@intel.com if (track_pfn_vma_new(vma, &pgprot, pfn, PAGE_SIZE)) 18282ab64037Svenkatesh.pallipadi@intel.com return -EINVAL; 18292ab64037Svenkatesh.pallipadi@intel.com 1830e4b866edSvenkatesh.pallipadi@intel.com ret = insert_pfn(vma, addr, pfn, pgprot); 18312ab64037Svenkatesh.pallipadi@intel.com 18322ab64037Svenkatesh.pallipadi@intel.com if (ret) 18332ab64037Svenkatesh.pallipadi@intel.com untrack_pfn_vma(vma, pfn, PAGE_SIZE); 18342ab64037Svenkatesh.pallipadi@intel.com 18352ab64037Svenkatesh.pallipadi@intel.com return ret; 1836e0dc0d8fSNick Piggin } 1837e0dc0d8fSNick Piggin EXPORT_SYMBOL(vm_insert_pfn); 1838e0dc0d8fSNick Piggin 1839423bad60SNick Piggin int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr, 1840423bad60SNick Piggin unsigned long pfn) 1841423bad60SNick Piggin { 1842423bad60SNick Piggin BUG_ON(!(vma->vm_flags & VM_MIXEDMAP)); 1843423bad60SNick Piggin 1844423bad60SNick Piggin if (addr < vma->vm_start || addr >= vma->vm_end) 1845423bad60SNick Piggin return -EFAULT; 1846423bad60SNick Piggin 1847423bad60SNick Piggin /* 1848423bad60SNick Piggin * If we don't have pte special, then we have to use the pfn_valid() 1849423bad60SNick Piggin * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must* 1850423bad60SNick Piggin * refcount the page if pfn_valid is true (hence insert_page rather 185162eede62SHugh Dickins * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP 185262eede62SHugh Dickins * without pte special, it would there be refcounted as a normal page. 1853423bad60SNick Piggin */ 1854423bad60SNick Piggin if (!HAVE_PTE_SPECIAL && pfn_valid(pfn)) { 1855423bad60SNick Piggin struct page *page; 1856423bad60SNick Piggin 1857423bad60SNick Piggin page = pfn_to_page(pfn); 1858423bad60SNick Piggin return insert_page(vma, addr, page, vma->vm_page_prot); 1859423bad60SNick Piggin } 1860423bad60SNick Piggin return insert_pfn(vma, addr, pfn, vma->vm_page_prot); 1861423bad60SNick Piggin } 1862423bad60SNick Piggin EXPORT_SYMBOL(vm_insert_mixed); 1863423bad60SNick Piggin 1864a145dd41SLinus Torvalds /* 18651da177e4SLinus Torvalds * maps a range of physical memory into the requested pages. the old 18661da177e4SLinus Torvalds * mappings are removed. any references to nonexistent pages results 18671da177e4SLinus Torvalds * in null mappings (currently treated as "copy-on-access") 18681da177e4SLinus Torvalds */ 18691da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd, 18701da177e4SLinus Torvalds unsigned long addr, unsigned long end, 18711da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 18721da177e4SLinus Torvalds { 18731da177e4SLinus Torvalds pte_t *pte; 1874c74df32cSHugh Dickins spinlock_t *ptl; 18751da177e4SLinus Torvalds 1876c74df32cSHugh Dickins pte = pte_alloc_map_lock(mm, pmd, addr, &ptl); 18771da177e4SLinus Torvalds if (!pte) 18781da177e4SLinus Torvalds return -ENOMEM; 18796606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 18801da177e4SLinus Torvalds do { 18811da177e4SLinus Torvalds BUG_ON(!pte_none(*pte)); 18827e675137SNick Piggin set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot))); 18831da177e4SLinus Torvalds pfn++; 18841da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 18856606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 1886c74df32cSHugh Dickins pte_unmap_unlock(pte - 1, ptl); 18871da177e4SLinus Torvalds return 0; 18881da177e4SLinus Torvalds } 18891da177e4SLinus Torvalds 18901da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud, 18911da177e4SLinus Torvalds unsigned long addr, unsigned long end, 18921da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 18931da177e4SLinus Torvalds { 18941da177e4SLinus Torvalds pmd_t *pmd; 18951da177e4SLinus Torvalds unsigned long next; 18961da177e4SLinus Torvalds 18971da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 18981da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, addr); 18991da177e4SLinus Torvalds if (!pmd) 19001da177e4SLinus Torvalds return -ENOMEM; 1901*f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 19021da177e4SLinus Torvalds do { 19031da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 19041da177e4SLinus Torvalds if (remap_pte_range(mm, pmd, addr, next, 19051da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot)) 19061da177e4SLinus Torvalds return -ENOMEM; 19071da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 19081da177e4SLinus Torvalds return 0; 19091da177e4SLinus Torvalds } 19101da177e4SLinus Torvalds 19111da177e4SLinus Torvalds static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd, 19121da177e4SLinus Torvalds unsigned long addr, unsigned long end, 19131da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 19141da177e4SLinus Torvalds { 19151da177e4SLinus Torvalds pud_t *pud; 19161da177e4SLinus Torvalds unsigned long next; 19171da177e4SLinus Torvalds 19181da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 19191da177e4SLinus Torvalds pud = pud_alloc(mm, pgd, addr); 19201da177e4SLinus Torvalds if (!pud) 19211da177e4SLinus Torvalds return -ENOMEM; 19221da177e4SLinus Torvalds do { 19231da177e4SLinus Torvalds next = pud_addr_end(addr, end); 19241da177e4SLinus Torvalds if (remap_pmd_range(mm, pud, addr, next, 19251da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot)) 19261da177e4SLinus Torvalds return -ENOMEM; 19271da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 19281da177e4SLinus Torvalds return 0; 19291da177e4SLinus Torvalds } 19301da177e4SLinus Torvalds 1931bfa5bf6dSRolf Eike Beer /** 1932bfa5bf6dSRolf Eike Beer * remap_pfn_range - remap kernel memory to userspace 1933bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 1934bfa5bf6dSRolf Eike Beer * @addr: target user address to start at 1935bfa5bf6dSRolf Eike Beer * @pfn: physical address of kernel memory 1936bfa5bf6dSRolf Eike Beer * @size: size of map area 1937bfa5bf6dSRolf Eike Beer * @prot: page protection flags for this mapping 1938bfa5bf6dSRolf Eike Beer * 1939bfa5bf6dSRolf Eike Beer * Note: this is only safe if the mm semaphore is held when called. 1940bfa5bf6dSRolf Eike Beer */ 19411da177e4SLinus Torvalds int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 19421da177e4SLinus Torvalds unsigned long pfn, unsigned long size, pgprot_t prot) 19431da177e4SLinus Torvalds { 19441da177e4SLinus Torvalds pgd_t *pgd; 19451da177e4SLinus Torvalds unsigned long next; 19462d15cab8SHugh Dickins unsigned long end = addr + PAGE_ALIGN(size); 19471da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 19481da177e4SLinus Torvalds int err; 19491da177e4SLinus Torvalds 19501da177e4SLinus Torvalds /* 19511da177e4SLinus Torvalds * Physically remapped pages are special. Tell the 19521da177e4SLinus Torvalds * rest of the world about it: 19531da177e4SLinus Torvalds * VM_IO tells people not to look at these pages 19541da177e4SLinus Torvalds * (accesses can have side effects). 19550b14c179SHugh Dickins * VM_RESERVED is specified all over the place, because 19560b14c179SHugh Dickins * in 2.4 it kept swapout's vma scan off this vma; but 19570b14c179SHugh Dickins * in 2.6 the LRU scan won't even find its pages, so this 19580b14c179SHugh Dickins * flag means no more than count its pages in reserved_vm, 19590b14c179SHugh Dickins * and omit it from core dump, even when VM_IO turned off. 19606aab341eSLinus Torvalds * VM_PFNMAP tells the core MM that the base pages are just 19616aab341eSLinus Torvalds * raw PFN mappings, and do not have a "struct page" associated 19626aab341eSLinus Torvalds * with them. 1963fb155c16SLinus Torvalds * 1964fb155c16SLinus Torvalds * There's a horrible special case to handle copy-on-write 1965fb155c16SLinus Torvalds * behaviour that some programs depend on. We mark the "original" 1966fb155c16SLinus Torvalds * un-COW'ed pages by matching them up with "vma->vm_pgoff". 19671da177e4SLinus Torvalds */ 19684bb9c5c0SPallipadi, Venkatesh if (addr == vma->vm_start && end == vma->vm_end) { 19696aab341eSLinus Torvalds vma->vm_pgoff = pfn; 1970895791daSPallipadi, Venkatesh vma->vm_flags |= VM_PFN_AT_MMAP; 19714bb9c5c0SPallipadi, Venkatesh } else if (is_cow_mapping(vma->vm_flags)) 19723c8bb73aSvenkatesh.pallipadi@intel.com return -EINVAL; 1973fb155c16SLinus Torvalds 1974fb155c16SLinus Torvalds vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP; 19751da177e4SLinus Torvalds 1976e4b866edSvenkatesh.pallipadi@intel.com err = track_pfn_vma_new(vma, &prot, pfn, PAGE_ALIGN(size)); 1977a3670613Svenkatesh.pallipadi@intel.com if (err) { 1978a3670613Svenkatesh.pallipadi@intel.com /* 1979a3670613Svenkatesh.pallipadi@intel.com * To indicate that track_pfn related cleanup is not 1980a3670613Svenkatesh.pallipadi@intel.com * needed from higher level routine calling unmap_vmas 1981a3670613Svenkatesh.pallipadi@intel.com */ 1982a3670613Svenkatesh.pallipadi@intel.com vma->vm_flags &= ~(VM_IO | VM_RESERVED | VM_PFNMAP); 1983895791daSPallipadi, Venkatesh vma->vm_flags &= ~VM_PFN_AT_MMAP; 19842ab64037Svenkatesh.pallipadi@intel.com return -EINVAL; 1985a3670613Svenkatesh.pallipadi@intel.com } 19862ab64037Svenkatesh.pallipadi@intel.com 19871da177e4SLinus Torvalds BUG_ON(addr >= end); 19881da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 19891da177e4SLinus Torvalds pgd = pgd_offset(mm, addr); 19901da177e4SLinus Torvalds flush_cache_range(vma, addr, end); 19911da177e4SLinus Torvalds do { 19921da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 19931da177e4SLinus Torvalds err = remap_pud_range(mm, pgd, addr, next, 19941da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot); 19951da177e4SLinus Torvalds if (err) 19961da177e4SLinus Torvalds break; 19971da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 19982ab64037Svenkatesh.pallipadi@intel.com 19992ab64037Svenkatesh.pallipadi@intel.com if (err) 20002ab64037Svenkatesh.pallipadi@intel.com untrack_pfn_vma(vma, pfn, PAGE_ALIGN(size)); 20012ab64037Svenkatesh.pallipadi@intel.com 20021da177e4SLinus Torvalds return err; 20031da177e4SLinus Torvalds } 20041da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range); 20051da177e4SLinus Torvalds 2006aee16b3cSJeremy Fitzhardinge static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd, 2007aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2008aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 2009aee16b3cSJeremy Fitzhardinge { 2010aee16b3cSJeremy Fitzhardinge pte_t *pte; 2011aee16b3cSJeremy Fitzhardinge int err; 20122f569afdSMartin Schwidefsky pgtable_t token; 201394909914SBorislav Petkov spinlock_t *uninitialized_var(ptl); 2014aee16b3cSJeremy Fitzhardinge 2015aee16b3cSJeremy Fitzhardinge pte = (mm == &init_mm) ? 2016aee16b3cSJeremy Fitzhardinge pte_alloc_kernel(pmd, addr) : 2017aee16b3cSJeremy Fitzhardinge pte_alloc_map_lock(mm, pmd, addr, &ptl); 2018aee16b3cSJeremy Fitzhardinge if (!pte) 2019aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2020aee16b3cSJeremy Fitzhardinge 2021aee16b3cSJeremy Fitzhardinge BUG_ON(pmd_huge(*pmd)); 2022aee16b3cSJeremy Fitzhardinge 202338e0edb1SJeremy Fitzhardinge arch_enter_lazy_mmu_mode(); 202438e0edb1SJeremy Fitzhardinge 20252f569afdSMartin Schwidefsky token = pmd_pgtable(*pmd); 2026aee16b3cSJeremy Fitzhardinge 2027aee16b3cSJeremy Fitzhardinge do { 2028c36987e2SDaisuke Nishimura err = fn(pte++, token, addr, data); 2029aee16b3cSJeremy Fitzhardinge if (err) 2030aee16b3cSJeremy Fitzhardinge break; 2031c36987e2SDaisuke Nishimura } while (addr += PAGE_SIZE, addr != end); 2032aee16b3cSJeremy Fitzhardinge 203338e0edb1SJeremy Fitzhardinge arch_leave_lazy_mmu_mode(); 203438e0edb1SJeremy Fitzhardinge 2035aee16b3cSJeremy Fitzhardinge if (mm != &init_mm) 2036aee16b3cSJeremy Fitzhardinge pte_unmap_unlock(pte-1, ptl); 2037aee16b3cSJeremy Fitzhardinge return err; 2038aee16b3cSJeremy Fitzhardinge } 2039aee16b3cSJeremy Fitzhardinge 2040aee16b3cSJeremy Fitzhardinge static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud, 2041aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2042aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 2043aee16b3cSJeremy Fitzhardinge { 2044aee16b3cSJeremy Fitzhardinge pmd_t *pmd; 2045aee16b3cSJeremy Fitzhardinge unsigned long next; 2046aee16b3cSJeremy Fitzhardinge int err; 2047aee16b3cSJeremy Fitzhardinge 2048ceb86879SAndi Kleen BUG_ON(pud_huge(*pud)); 2049ceb86879SAndi Kleen 2050aee16b3cSJeremy Fitzhardinge pmd = pmd_alloc(mm, pud, addr); 2051aee16b3cSJeremy Fitzhardinge if (!pmd) 2052aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2053aee16b3cSJeremy Fitzhardinge do { 2054aee16b3cSJeremy Fitzhardinge next = pmd_addr_end(addr, end); 2055aee16b3cSJeremy Fitzhardinge err = apply_to_pte_range(mm, pmd, addr, next, fn, data); 2056aee16b3cSJeremy Fitzhardinge if (err) 2057aee16b3cSJeremy Fitzhardinge break; 2058aee16b3cSJeremy Fitzhardinge } while (pmd++, addr = next, addr != end); 2059aee16b3cSJeremy Fitzhardinge return err; 2060aee16b3cSJeremy Fitzhardinge } 2061aee16b3cSJeremy Fitzhardinge 2062aee16b3cSJeremy Fitzhardinge static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd, 2063aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2064aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 2065aee16b3cSJeremy Fitzhardinge { 2066aee16b3cSJeremy Fitzhardinge pud_t *pud; 2067aee16b3cSJeremy Fitzhardinge unsigned long next; 2068aee16b3cSJeremy Fitzhardinge int err; 2069aee16b3cSJeremy Fitzhardinge 2070aee16b3cSJeremy Fitzhardinge pud = pud_alloc(mm, pgd, addr); 2071aee16b3cSJeremy Fitzhardinge if (!pud) 2072aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2073aee16b3cSJeremy Fitzhardinge do { 2074aee16b3cSJeremy Fitzhardinge next = pud_addr_end(addr, end); 2075aee16b3cSJeremy Fitzhardinge err = apply_to_pmd_range(mm, pud, addr, next, fn, data); 2076aee16b3cSJeremy Fitzhardinge if (err) 2077aee16b3cSJeremy Fitzhardinge break; 2078aee16b3cSJeremy Fitzhardinge } while (pud++, addr = next, addr != end); 2079aee16b3cSJeremy Fitzhardinge return err; 2080aee16b3cSJeremy Fitzhardinge } 2081aee16b3cSJeremy Fitzhardinge 2082aee16b3cSJeremy Fitzhardinge /* 2083aee16b3cSJeremy Fitzhardinge * Scan a region of virtual memory, filling in page tables as necessary 2084aee16b3cSJeremy Fitzhardinge * and calling a provided function on each leaf page table. 2085aee16b3cSJeremy Fitzhardinge */ 2086aee16b3cSJeremy Fitzhardinge int apply_to_page_range(struct mm_struct *mm, unsigned long addr, 2087aee16b3cSJeremy Fitzhardinge unsigned long size, pte_fn_t fn, void *data) 2088aee16b3cSJeremy Fitzhardinge { 2089aee16b3cSJeremy Fitzhardinge pgd_t *pgd; 2090aee16b3cSJeremy Fitzhardinge unsigned long next; 209157250a5bSJeremy Fitzhardinge unsigned long end = addr + size; 2092aee16b3cSJeremy Fitzhardinge int err; 2093aee16b3cSJeremy Fitzhardinge 2094aee16b3cSJeremy Fitzhardinge BUG_ON(addr >= end); 2095aee16b3cSJeremy Fitzhardinge pgd = pgd_offset(mm, addr); 2096aee16b3cSJeremy Fitzhardinge do { 2097aee16b3cSJeremy Fitzhardinge next = pgd_addr_end(addr, end); 2098aee16b3cSJeremy Fitzhardinge err = apply_to_pud_range(mm, pgd, addr, next, fn, data); 2099aee16b3cSJeremy Fitzhardinge if (err) 2100aee16b3cSJeremy Fitzhardinge break; 2101aee16b3cSJeremy Fitzhardinge } while (pgd++, addr = next, addr != end); 210257250a5bSJeremy Fitzhardinge 2103aee16b3cSJeremy Fitzhardinge return err; 2104aee16b3cSJeremy Fitzhardinge } 2105aee16b3cSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(apply_to_page_range); 2106aee16b3cSJeremy Fitzhardinge 21071da177e4SLinus Torvalds /* 21088f4e2101SHugh Dickins * handle_pte_fault chooses page fault handler according to an entry 21098f4e2101SHugh Dickins * which was read non-atomically. Before making any commitment, on 21108f4e2101SHugh Dickins * those architectures or configurations (e.g. i386 with PAE) which 21118f4e2101SHugh Dickins * might give a mix of unmatched parts, do_swap_page and do_file_page 21128f4e2101SHugh Dickins * must check under lock before unmapping the pte and proceeding 21138f4e2101SHugh Dickins * (but do_wp_page is only called after already making such a check; 21148f4e2101SHugh Dickins * and do_anonymous_page and do_no_page can safely check later on). 21158f4e2101SHugh Dickins */ 21164c21e2f2SHugh Dickins static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd, 21178f4e2101SHugh Dickins pte_t *page_table, pte_t orig_pte) 21188f4e2101SHugh Dickins { 21198f4e2101SHugh Dickins int same = 1; 21208f4e2101SHugh Dickins #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT) 21218f4e2101SHugh Dickins if (sizeof(pte_t) > sizeof(unsigned long)) { 21224c21e2f2SHugh Dickins spinlock_t *ptl = pte_lockptr(mm, pmd); 21234c21e2f2SHugh Dickins spin_lock(ptl); 21248f4e2101SHugh Dickins same = pte_same(*page_table, orig_pte); 21254c21e2f2SHugh Dickins spin_unlock(ptl); 21268f4e2101SHugh Dickins } 21278f4e2101SHugh Dickins #endif 21288f4e2101SHugh Dickins pte_unmap(page_table); 21298f4e2101SHugh Dickins return same; 21308f4e2101SHugh Dickins } 21318f4e2101SHugh Dickins 21329de455b2SAtsushi Nemoto static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma) 21336aab341eSLinus Torvalds { 21346aab341eSLinus Torvalds /* 21356aab341eSLinus Torvalds * If the source page was a PFN mapping, we don't have 21366aab341eSLinus Torvalds * a "struct page" for it. We do a best-effort copy by 21376aab341eSLinus Torvalds * just copying from the original user address. If that 21386aab341eSLinus Torvalds * fails, we just zero-fill it. Live with it. 21396aab341eSLinus Torvalds */ 21406aab341eSLinus Torvalds if (unlikely(!src)) { 21416aab341eSLinus Torvalds void *kaddr = kmap_atomic(dst, KM_USER0); 21425d2a2dbbSLinus Torvalds void __user *uaddr = (void __user *)(va & PAGE_MASK); 21435d2a2dbbSLinus Torvalds 21445d2a2dbbSLinus Torvalds /* 21455d2a2dbbSLinus Torvalds * This really shouldn't fail, because the page is there 21465d2a2dbbSLinus Torvalds * in the page tables. But it might just be unreadable, 21475d2a2dbbSLinus Torvalds * in which case we just give up and fill the result with 21485d2a2dbbSLinus Torvalds * zeroes. 21495d2a2dbbSLinus Torvalds */ 21505d2a2dbbSLinus Torvalds if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) 21513ecb01dfSJan Beulich clear_page(kaddr); 21526aab341eSLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 2153c4ec7b0dSDmitriy Monakhov flush_dcache_page(dst); 21540ed361deSNick Piggin } else 21559de455b2SAtsushi Nemoto copy_user_highpage(dst, src, va, vma); 21566aab341eSLinus Torvalds } 21576aab341eSLinus Torvalds 21581da177e4SLinus Torvalds /* 21591da177e4SLinus Torvalds * This routine handles present pages, when users try to write 21601da177e4SLinus Torvalds * to a shared page. It is done by copying the page to a new address 21611da177e4SLinus Torvalds * and decrementing the shared-page counter for the old page. 21621da177e4SLinus Torvalds * 21631da177e4SLinus Torvalds * Note that this routine assumes that the protection checks have been 21641da177e4SLinus Torvalds * done by the caller (the low-level page fault routine in most cases). 21651da177e4SLinus Torvalds * Thus we can safely just mark it writable once we've done any necessary 21661da177e4SLinus Torvalds * COW. 21671da177e4SLinus Torvalds * 21681da177e4SLinus Torvalds * We also mark the page dirty at this point even though the page will 21691da177e4SLinus Torvalds * change only once the write actually happens. This avoids a few races, 21701da177e4SLinus Torvalds * and potentially makes it more efficient. 21711da177e4SLinus Torvalds * 21728f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 21738f4e2101SHugh Dickins * but allow concurrent faults), with pte both mapped and locked. 21748f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 21751da177e4SLinus Torvalds */ 21761da177e4SLinus Torvalds static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma, 217765500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 21788f4e2101SHugh Dickins spinlock_t *ptl, pte_t orig_pte) 2179e6219ec8SNamhyung Kim __releases(ptl) 21801da177e4SLinus Torvalds { 2181e5bbe4dfSHugh Dickins struct page *old_page, *new_page; 21821da177e4SLinus Torvalds pte_t entry; 2183b009c024SMichel Lespinasse int ret = 0; 2184a200ee18SPeter Zijlstra int page_mkwrite = 0; 2185d08b3851SPeter Zijlstra struct page *dirty_page = NULL; 21861da177e4SLinus Torvalds 21876aab341eSLinus Torvalds old_page = vm_normal_page(vma, address, orig_pte); 2188251b97f5SPeter Zijlstra if (!old_page) { 2189251b97f5SPeter Zijlstra /* 2190251b97f5SPeter Zijlstra * VM_MIXEDMAP !pfn_valid() case 2191251b97f5SPeter Zijlstra * 2192251b97f5SPeter Zijlstra * We should not cow pages in a shared writeable mapping. 2193251b97f5SPeter Zijlstra * Just mark the pages writable as we can't do any dirty 2194251b97f5SPeter Zijlstra * accounting on raw pfn maps. 2195251b97f5SPeter Zijlstra */ 2196251b97f5SPeter Zijlstra if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 2197251b97f5SPeter Zijlstra (VM_WRITE|VM_SHARED)) 2198251b97f5SPeter Zijlstra goto reuse; 2199920fc356SHugh Dickins goto gotten; 2200251b97f5SPeter Zijlstra } 22011da177e4SLinus Torvalds 2202d08b3851SPeter Zijlstra /* 2203ee6a6457SPeter Zijlstra * Take out anonymous pages first, anonymous shared vmas are 2204ee6a6457SPeter Zijlstra * not dirty accountable. 2205d08b3851SPeter Zijlstra */ 22069a840895SHugh Dickins if (PageAnon(old_page) && !PageKsm(old_page)) { 2207ab967d86SHugh Dickins if (!trylock_page(old_page)) { 2208ab967d86SHugh Dickins page_cache_get(old_page); 2209ab967d86SHugh Dickins pte_unmap_unlock(page_table, ptl); 2210ab967d86SHugh Dickins lock_page(old_page); 2211ab967d86SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, 2212ab967d86SHugh Dickins &ptl); 2213ab967d86SHugh Dickins if (!pte_same(*page_table, orig_pte)) { 2214ab967d86SHugh Dickins unlock_page(old_page); 2215ab967d86SHugh Dickins page_cache_release(old_page); 2216ab967d86SHugh Dickins goto unlock; 2217ab967d86SHugh Dickins } 2218ab967d86SHugh Dickins page_cache_release(old_page); 2219ab967d86SHugh Dickins } 2220b009c024SMichel Lespinasse if (reuse_swap_page(old_page)) { 2221c44b6743SRik van Riel /* 2222c44b6743SRik van Riel * The page is all ours. Move it to our anon_vma so 2223c44b6743SRik van Riel * the rmap code will not search our parent or siblings. 2224c44b6743SRik van Riel * Protected against the rmap code by the page lock. 2225c44b6743SRik van Riel */ 2226c44b6743SRik van Riel page_move_anon_rmap(old_page, vma, address); 2227ee6a6457SPeter Zijlstra unlock_page(old_page); 2228b009c024SMichel Lespinasse goto reuse; 2229b009c024SMichel Lespinasse } 2230b009c024SMichel Lespinasse unlock_page(old_page); 2231ee6a6457SPeter Zijlstra } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 2232d08b3851SPeter Zijlstra (VM_WRITE|VM_SHARED))) { 2233ee6a6457SPeter Zijlstra /* 2234ee6a6457SPeter Zijlstra * Only catch write-faults on shared writable pages, 2235ee6a6457SPeter Zijlstra * read-only shared pages can get COWed by 2236ee6a6457SPeter Zijlstra * get_user_pages(.write=1, .force=1). 2237ee6a6457SPeter Zijlstra */ 22389637a5efSDavid Howells if (vma->vm_ops && vma->vm_ops->page_mkwrite) { 2239c2ec175cSNick Piggin struct vm_fault vmf; 2240c2ec175cSNick Piggin int tmp; 2241c2ec175cSNick Piggin 2242c2ec175cSNick Piggin vmf.virtual_address = (void __user *)(address & 2243c2ec175cSNick Piggin PAGE_MASK); 2244c2ec175cSNick Piggin vmf.pgoff = old_page->index; 2245c2ec175cSNick Piggin vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE; 2246c2ec175cSNick Piggin vmf.page = old_page; 2247c2ec175cSNick Piggin 22489637a5efSDavid Howells /* 22499637a5efSDavid Howells * Notify the address space that the page is about to 22509637a5efSDavid Howells * become writable so that it can prohibit this or wait 22519637a5efSDavid Howells * for the page to get into an appropriate state. 22529637a5efSDavid Howells * 22539637a5efSDavid Howells * We do this without the lock held, so that it can 22549637a5efSDavid Howells * sleep if it needs to. 22559637a5efSDavid Howells */ 22569637a5efSDavid Howells page_cache_get(old_page); 22579637a5efSDavid Howells pte_unmap_unlock(page_table, ptl); 22589637a5efSDavid Howells 2259c2ec175cSNick Piggin tmp = vma->vm_ops->page_mkwrite(vma, &vmf); 2260c2ec175cSNick Piggin if (unlikely(tmp & 2261c2ec175cSNick Piggin (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) { 2262c2ec175cSNick Piggin ret = tmp; 22639637a5efSDavid Howells goto unwritable_page; 2264c2ec175cSNick Piggin } 2265b827e496SNick Piggin if (unlikely(!(tmp & VM_FAULT_LOCKED))) { 2266b827e496SNick Piggin lock_page(old_page); 2267b827e496SNick Piggin if (!old_page->mapping) { 2268b827e496SNick Piggin ret = 0; /* retry the fault */ 2269b827e496SNick Piggin unlock_page(old_page); 2270b827e496SNick Piggin goto unwritable_page; 2271b827e496SNick Piggin } 2272b827e496SNick Piggin } else 2273b827e496SNick Piggin VM_BUG_ON(!PageLocked(old_page)); 22749637a5efSDavid Howells 22759637a5efSDavid Howells /* 22769637a5efSDavid Howells * Since we dropped the lock we need to revalidate 22779637a5efSDavid Howells * the PTE as someone else may have changed it. If 22789637a5efSDavid Howells * they did, we just return, as we can count on the 22799637a5efSDavid Howells * MMU to tell us if they didn't also make it writable. 22809637a5efSDavid Howells */ 22819637a5efSDavid Howells page_table = pte_offset_map_lock(mm, pmd, address, 22829637a5efSDavid Howells &ptl); 2283b827e496SNick Piggin if (!pte_same(*page_table, orig_pte)) { 2284b827e496SNick Piggin unlock_page(old_page); 2285c3704cebSHugh Dickins page_cache_release(old_page); 22869637a5efSDavid Howells goto unlock; 2287b827e496SNick Piggin } 2288a200ee18SPeter Zijlstra 2289a200ee18SPeter Zijlstra page_mkwrite = 1; 22909637a5efSDavid Howells } 2291d08b3851SPeter Zijlstra dirty_page = old_page; 2292d08b3851SPeter Zijlstra get_page(dirty_page); 22939637a5efSDavid Howells 2294251b97f5SPeter Zijlstra reuse: 2295eca35133SBen Collins flush_cache_page(vma, address, pte_pfn(orig_pte)); 229665500d23SHugh Dickins entry = pte_mkyoung(orig_pte); 229765500d23SHugh Dickins entry = maybe_mkwrite(pte_mkdirty(entry), vma); 2298954ffcb3SKAMEZAWA Hiroyuki if (ptep_set_access_flags(vma, address, page_table, entry,1)) 22994b3073e1SRussell King update_mmu_cache(vma, address, page_table); 230072ddc8f7SMichel Lespinasse pte_unmap_unlock(page_table, ptl); 230165500d23SHugh Dickins ret |= VM_FAULT_WRITE; 230272ddc8f7SMichel Lespinasse 230372ddc8f7SMichel Lespinasse if (!dirty_page) 230472ddc8f7SMichel Lespinasse return ret; 230572ddc8f7SMichel Lespinasse 230672ddc8f7SMichel Lespinasse /* 230772ddc8f7SMichel Lespinasse * Yes, Virginia, this is actually required to prevent a race 230872ddc8f7SMichel Lespinasse * with clear_page_dirty_for_io() from clearing the page dirty 230972ddc8f7SMichel Lespinasse * bit after it clear all dirty ptes, but before a racing 231072ddc8f7SMichel Lespinasse * do_wp_page installs a dirty pte. 231172ddc8f7SMichel Lespinasse * 231272ddc8f7SMichel Lespinasse * do_no_page is protected similarly. 231372ddc8f7SMichel Lespinasse */ 231472ddc8f7SMichel Lespinasse if (!page_mkwrite) { 231572ddc8f7SMichel Lespinasse wait_on_page_locked(dirty_page); 231672ddc8f7SMichel Lespinasse set_page_dirty_balance(dirty_page, page_mkwrite); 231772ddc8f7SMichel Lespinasse } 231872ddc8f7SMichel Lespinasse put_page(dirty_page); 231972ddc8f7SMichel Lespinasse if (page_mkwrite) { 232072ddc8f7SMichel Lespinasse struct address_space *mapping = dirty_page->mapping; 232172ddc8f7SMichel Lespinasse 232272ddc8f7SMichel Lespinasse set_page_dirty(dirty_page); 232372ddc8f7SMichel Lespinasse unlock_page(dirty_page); 232472ddc8f7SMichel Lespinasse page_cache_release(dirty_page); 232572ddc8f7SMichel Lespinasse if (mapping) { 232672ddc8f7SMichel Lespinasse /* 232772ddc8f7SMichel Lespinasse * Some device drivers do not set page.mapping 232872ddc8f7SMichel Lespinasse * but still dirty their pages 232972ddc8f7SMichel Lespinasse */ 233072ddc8f7SMichel Lespinasse balance_dirty_pages_ratelimited(mapping); 233172ddc8f7SMichel Lespinasse } 233272ddc8f7SMichel Lespinasse } 233372ddc8f7SMichel Lespinasse 233472ddc8f7SMichel Lespinasse /* file_update_time outside page_lock */ 233572ddc8f7SMichel Lespinasse if (vma->vm_file) 233672ddc8f7SMichel Lespinasse file_update_time(vma->vm_file); 233772ddc8f7SMichel Lespinasse 233872ddc8f7SMichel Lespinasse return ret; 23391da177e4SLinus Torvalds } 23401da177e4SLinus Torvalds 23411da177e4SLinus Torvalds /* 23421da177e4SLinus Torvalds * Ok, we need to copy. Oh, well.. 23431da177e4SLinus Torvalds */ 23441da177e4SLinus Torvalds page_cache_get(old_page); 2345920fc356SHugh Dickins gotten: 23468f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 23471da177e4SLinus Torvalds 23481da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 234965500d23SHugh Dickins goto oom; 2350a13ea5b7SHugh Dickins 235162eede62SHugh Dickins if (is_zero_pfn(pte_pfn(orig_pte))) { 2352a13ea5b7SHugh Dickins new_page = alloc_zeroed_user_highpage_movable(vma, address); 2353a13ea5b7SHugh Dickins if (!new_page) 2354a13ea5b7SHugh Dickins goto oom; 2355a13ea5b7SHugh Dickins } else { 2356769848c0SMel Gorman new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address); 23571da177e4SLinus Torvalds if (!new_page) 235865500d23SHugh Dickins goto oom; 2359a13ea5b7SHugh Dickins cow_user_page(new_page, old_page, address, vma); 2360a13ea5b7SHugh Dickins } 2361a13ea5b7SHugh Dickins __SetPageUptodate(new_page); 2362a13ea5b7SHugh Dickins 2363b291f000SNick Piggin /* 2364b291f000SNick Piggin * Don't let another task, with possibly unlocked vma, 2365b291f000SNick Piggin * keep the mlocked page. 2366b291f000SNick Piggin */ 2367ab92661dSCarsten Otte if ((vma->vm_flags & VM_LOCKED) && old_page) { 2368b291f000SNick Piggin lock_page(old_page); /* for LRU manipulation */ 2369b291f000SNick Piggin clear_page_mlock(old_page); 2370b291f000SNick Piggin unlock_page(old_page); 2371b291f000SNick Piggin } 237265500d23SHugh Dickins 23732c26fdd7SKAMEZAWA Hiroyuki if (mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL)) 23748a9f3ccdSBalbir Singh goto oom_free_new; 23758a9f3ccdSBalbir Singh 23761da177e4SLinus Torvalds /* 23771da177e4SLinus Torvalds * Re-check the pte - we dropped the lock 23781da177e4SLinus Torvalds */ 23798f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 238065500d23SHugh Dickins if (likely(pte_same(*page_table, orig_pte))) { 2381920fc356SHugh Dickins if (old_page) { 23824294621fSHugh Dickins if (!PageAnon(old_page)) { 238334e55232SKAMEZAWA Hiroyuki dec_mm_counter_fast(mm, MM_FILEPAGES); 238434e55232SKAMEZAWA Hiroyuki inc_mm_counter_fast(mm, MM_ANONPAGES); 23854294621fSHugh Dickins } 2386920fc356SHugh Dickins } else 238734e55232SKAMEZAWA Hiroyuki inc_mm_counter_fast(mm, MM_ANONPAGES); 2388eca35133SBen Collins flush_cache_page(vma, address, pte_pfn(orig_pte)); 238965500d23SHugh Dickins entry = mk_pte(new_page, vma->vm_page_prot); 239065500d23SHugh Dickins entry = maybe_mkwrite(pte_mkdirty(entry), vma); 23914ce072f1SSiddha, Suresh B /* 23924ce072f1SSiddha, Suresh B * Clear the pte entry and flush it first, before updating the 23934ce072f1SSiddha, Suresh B * pte with the new entry. This will avoid a race condition 23944ce072f1SSiddha, Suresh B * seen in the presence of one thread doing SMC and another 23954ce072f1SSiddha, Suresh B * thread doing COW. 23964ce072f1SSiddha, Suresh B */ 2397828502d3SIzik Eidus ptep_clear_flush(vma, address, page_table); 23989617d95eSNick Piggin page_add_new_anon_rmap(new_page, vma, address); 2399828502d3SIzik Eidus /* 2400828502d3SIzik Eidus * We call the notify macro here because, when using secondary 2401828502d3SIzik Eidus * mmu page tables (such as kvm shadow page tables), we want the 2402828502d3SIzik Eidus * new page to be mapped directly into the secondary page table. 2403828502d3SIzik Eidus */ 2404828502d3SIzik Eidus set_pte_at_notify(mm, address, page_table, entry); 24054b3073e1SRussell King update_mmu_cache(vma, address, page_table); 2406945754a1SNick Piggin if (old_page) { 2407945754a1SNick Piggin /* 2408945754a1SNick Piggin * Only after switching the pte to the new page may 2409945754a1SNick Piggin * we remove the mapcount here. Otherwise another 2410945754a1SNick Piggin * process may come and find the rmap count decremented 2411945754a1SNick Piggin * before the pte is switched to the new page, and 2412945754a1SNick Piggin * "reuse" the old page writing into it while our pte 2413945754a1SNick Piggin * here still points into it and can be read by other 2414945754a1SNick Piggin * threads. 2415945754a1SNick Piggin * 2416945754a1SNick Piggin * The critical issue is to order this 2417945754a1SNick Piggin * page_remove_rmap with the ptp_clear_flush above. 2418945754a1SNick Piggin * Those stores are ordered by (if nothing else,) 2419945754a1SNick Piggin * the barrier present in the atomic_add_negative 2420945754a1SNick Piggin * in page_remove_rmap. 2421945754a1SNick Piggin * 2422945754a1SNick Piggin * Then the TLB flush in ptep_clear_flush ensures that 2423945754a1SNick Piggin * no process can access the old page before the 2424945754a1SNick Piggin * decremented mapcount is visible. And the old page 2425945754a1SNick Piggin * cannot be reused until after the decremented 2426945754a1SNick Piggin * mapcount is visible. So transitively, TLBs to 2427945754a1SNick Piggin * old page will be flushed before it can be reused. 2428945754a1SNick Piggin */ 2429edc315fdSHugh Dickins page_remove_rmap(old_page); 2430945754a1SNick Piggin } 2431945754a1SNick Piggin 24321da177e4SLinus Torvalds /* Free the old page.. */ 24331da177e4SLinus Torvalds new_page = old_page; 2434f33ea7f4SNick Piggin ret |= VM_FAULT_WRITE; 24358a9f3ccdSBalbir Singh } else 24368a9f3ccdSBalbir Singh mem_cgroup_uncharge_page(new_page); 24378a9f3ccdSBalbir Singh 2438920fc356SHugh Dickins if (new_page) 24391da177e4SLinus Torvalds page_cache_release(new_page); 2440920fc356SHugh Dickins if (old_page) 24411da177e4SLinus Torvalds page_cache_release(old_page); 244265500d23SHugh Dickins unlock: 24438f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 2444f33ea7f4SNick Piggin return ret; 24458a9f3ccdSBalbir Singh oom_free_new: 24466dbf6d3bSHugh Dickins page_cache_release(new_page); 244765500d23SHugh Dickins oom: 2448b827e496SNick Piggin if (old_page) { 2449b827e496SNick Piggin if (page_mkwrite) { 2450b827e496SNick Piggin unlock_page(old_page); 24511da177e4SLinus Torvalds page_cache_release(old_page); 2452b827e496SNick Piggin } 2453b827e496SNick Piggin page_cache_release(old_page); 2454b827e496SNick Piggin } 24551da177e4SLinus Torvalds return VM_FAULT_OOM; 24569637a5efSDavid Howells 24579637a5efSDavid Howells unwritable_page: 24589637a5efSDavid Howells page_cache_release(old_page); 2459c2ec175cSNick Piggin return ret; 24601da177e4SLinus Torvalds } 24611da177e4SLinus Torvalds 24621da177e4SLinus Torvalds /* 24631da177e4SLinus Torvalds * Helper functions for unmap_mapping_range(). 24641da177e4SLinus Torvalds * 24651da177e4SLinus Torvalds * __ Notes on dropping i_mmap_lock to reduce latency while unmapping __ 24661da177e4SLinus Torvalds * 24671da177e4SLinus Torvalds * We have to restart searching the prio_tree whenever we drop the lock, 24681da177e4SLinus Torvalds * since the iterator is only valid while the lock is held, and anyway 24691da177e4SLinus Torvalds * a later vma might be split and reinserted earlier while lock dropped. 24701da177e4SLinus Torvalds * 24711da177e4SLinus Torvalds * The list of nonlinear vmas could be handled more efficiently, using 24721da177e4SLinus Torvalds * a placeholder, but handle it in the same way until a need is shown. 24731da177e4SLinus Torvalds * It is important to search the prio_tree before nonlinear list: a vma 24741da177e4SLinus Torvalds * may become nonlinear and be shifted from prio_tree to nonlinear list 24751da177e4SLinus Torvalds * while the lock is dropped; but never shifted from list to prio_tree. 24761da177e4SLinus Torvalds * 24771da177e4SLinus Torvalds * In order to make forward progress despite restarting the search, 24781da177e4SLinus Torvalds * vm_truncate_count is used to mark a vma as now dealt with, so we can 24791da177e4SLinus Torvalds * quickly skip it next time around. Since the prio_tree search only 24801da177e4SLinus Torvalds * shows us those vmas affected by unmapping the range in question, we 24811da177e4SLinus Torvalds * can't efficiently keep all vmas in step with mapping->truncate_count: 24821da177e4SLinus Torvalds * so instead reset them all whenever it wraps back to 0 (then go to 1). 24831da177e4SLinus Torvalds * mapping->truncate_count and vma->vm_truncate_count are protected by 24841da177e4SLinus Torvalds * i_mmap_lock. 24851da177e4SLinus Torvalds * 24861da177e4SLinus Torvalds * In order to make forward progress despite repeatedly restarting some 2487ee39b37bSHugh Dickins * large vma, note the restart_addr from unmap_vmas when it breaks out: 24881da177e4SLinus Torvalds * and restart from that address when we reach that vma again. It might 24891da177e4SLinus Torvalds * have been split or merged, shrunk or extended, but never shifted: so 24901da177e4SLinus Torvalds * restart_addr remains valid so long as it remains in the vma's range. 24911da177e4SLinus Torvalds * unmap_mapping_range forces truncate_count to leap over page-aligned 24921da177e4SLinus Torvalds * values so we can save vma's restart_addr in its truncate_count field. 24931da177e4SLinus Torvalds */ 24941da177e4SLinus Torvalds #define is_restart_addr(truncate_count) (!((truncate_count) & ~PAGE_MASK)) 24951da177e4SLinus Torvalds 24961da177e4SLinus Torvalds static void reset_vma_truncate_counts(struct address_space *mapping) 24971da177e4SLinus Torvalds { 24981da177e4SLinus Torvalds struct vm_area_struct *vma; 24991da177e4SLinus Torvalds struct prio_tree_iter iter; 25001da177e4SLinus Torvalds 25011da177e4SLinus Torvalds vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, ULONG_MAX) 25021da177e4SLinus Torvalds vma->vm_truncate_count = 0; 25031da177e4SLinus Torvalds list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list) 25041da177e4SLinus Torvalds vma->vm_truncate_count = 0; 25051da177e4SLinus Torvalds } 25061da177e4SLinus Torvalds 25071da177e4SLinus Torvalds static int unmap_mapping_range_vma(struct vm_area_struct *vma, 25081da177e4SLinus Torvalds unsigned long start_addr, unsigned long end_addr, 25091da177e4SLinus Torvalds struct zap_details *details) 25101da177e4SLinus Torvalds { 25111da177e4SLinus Torvalds unsigned long restart_addr; 25121da177e4SLinus Torvalds int need_break; 25131da177e4SLinus Torvalds 2514d00806b1SNick Piggin /* 2515d00806b1SNick Piggin * files that support invalidating or truncating portions of the 2516d0217ac0SNick Piggin * file from under mmaped areas must have their ->fault function 251783c54070SNick Piggin * return a locked page (and set VM_FAULT_LOCKED in the return). 251883c54070SNick Piggin * This provides synchronisation against concurrent unmapping here. 2519d00806b1SNick Piggin */ 2520d00806b1SNick Piggin 25211da177e4SLinus Torvalds again: 25221da177e4SLinus Torvalds restart_addr = vma->vm_truncate_count; 25231da177e4SLinus Torvalds if (is_restart_addr(restart_addr) && start_addr < restart_addr) { 25241da177e4SLinus Torvalds start_addr = restart_addr; 25251da177e4SLinus Torvalds if (start_addr >= end_addr) { 25261da177e4SLinus Torvalds /* Top of vma has been split off since last time */ 25271da177e4SLinus Torvalds vma->vm_truncate_count = details->truncate_count; 25281da177e4SLinus Torvalds return 0; 25291da177e4SLinus Torvalds } 25301da177e4SLinus Torvalds } 25311da177e4SLinus Torvalds 2532ee39b37bSHugh Dickins restart_addr = zap_page_range(vma, start_addr, 2533ee39b37bSHugh Dickins end_addr - start_addr, details); 253495c354feSNick Piggin need_break = need_resched() || spin_needbreak(details->i_mmap_lock); 25351da177e4SLinus Torvalds 2536ee39b37bSHugh Dickins if (restart_addr >= end_addr) { 25371da177e4SLinus Torvalds /* We have now completed this vma: mark it so */ 25381da177e4SLinus Torvalds vma->vm_truncate_count = details->truncate_count; 25391da177e4SLinus Torvalds if (!need_break) 25401da177e4SLinus Torvalds return 0; 25411da177e4SLinus Torvalds } else { 25421da177e4SLinus Torvalds /* Note restart_addr in vma's truncate_count field */ 2543ee39b37bSHugh Dickins vma->vm_truncate_count = restart_addr; 25441da177e4SLinus Torvalds if (!need_break) 25451da177e4SLinus Torvalds goto again; 25461da177e4SLinus Torvalds } 25471da177e4SLinus Torvalds 25481da177e4SLinus Torvalds spin_unlock(details->i_mmap_lock); 25491da177e4SLinus Torvalds cond_resched(); 25501da177e4SLinus Torvalds spin_lock(details->i_mmap_lock); 25511da177e4SLinus Torvalds return -EINTR; 25521da177e4SLinus Torvalds } 25531da177e4SLinus Torvalds 25541da177e4SLinus Torvalds static inline void unmap_mapping_range_tree(struct prio_tree_root *root, 25551da177e4SLinus Torvalds struct zap_details *details) 25561da177e4SLinus Torvalds { 25571da177e4SLinus Torvalds struct vm_area_struct *vma; 25581da177e4SLinus Torvalds struct prio_tree_iter iter; 25591da177e4SLinus Torvalds pgoff_t vba, vea, zba, zea; 25601da177e4SLinus Torvalds 25611da177e4SLinus Torvalds restart: 25621da177e4SLinus Torvalds vma_prio_tree_foreach(vma, &iter, root, 25631da177e4SLinus Torvalds details->first_index, details->last_index) { 25641da177e4SLinus Torvalds /* Skip quickly over those we have already dealt with */ 25651da177e4SLinus Torvalds if (vma->vm_truncate_count == details->truncate_count) 25661da177e4SLinus Torvalds continue; 25671da177e4SLinus Torvalds 25681da177e4SLinus Torvalds vba = vma->vm_pgoff; 25691da177e4SLinus Torvalds vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1; 25701da177e4SLinus Torvalds /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */ 25711da177e4SLinus Torvalds zba = details->first_index; 25721da177e4SLinus Torvalds if (zba < vba) 25731da177e4SLinus Torvalds zba = vba; 25741da177e4SLinus Torvalds zea = details->last_index; 25751da177e4SLinus Torvalds if (zea > vea) 25761da177e4SLinus Torvalds zea = vea; 25771da177e4SLinus Torvalds 25781da177e4SLinus Torvalds if (unmap_mapping_range_vma(vma, 25791da177e4SLinus Torvalds ((zba - vba) << PAGE_SHIFT) + vma->vm_start, 25801da177e4SLinus Torvalds ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start, 25811da177e4SLinus Torvalds details) < 0) 25821da177e4SLinus Torvalds goto restart; 25831da177e4SLinus Torvalds } 25841da177e4SLinus Torvalds } 25851da177e4SLinus Torvalds 25861da177e4SLinus Torvalds static inline void unmap_mapping_range_list(struct list_head *head, 25871da177e4SLinus Torvalds struct zap_details *details) 25881da177e4SLinus Torvalds { 25891da177e4SLinus Torvalds struct vm_area_struct *vma; 25901da177e4SLinus Torvalds 25911da177e4SLinus Torvalds /* 25921da177e4SLinus Torvalds * In nonlinear VMAs there is no correspondence between virtual address 25931da177e4SLinus Torvalds * offset and file offset. So we must perform an exhaustive search 25941da177e4SLinus Torvalds * across *all* the pages in each nonlinear VMA, not just the pages 25951da177e4SLinus Torvalds * whose virtual address lies outside the file truncation point. 25961da177e4SLinus Torvalds */ 25971da177e4SLinus Torvalds restart: 25981da177e4SLinus Torvalds list_for_each_entry(vma, head, shared.vm_set.list) { 25991da177e4SLinus Torvalds /* Skip quickly over those we have already dealt with */ 26001da177e4SLinus Torvalds if (vma->vm_truncate_count == details->truncate_count) 26011da177e4SLinus Torvalds continue; 26021da177e4SLinus Torvalds details->nonlinear_vma = vma; 26031da177e4SLinus Torvalds if (unmap_mapping_range_vma(vma, vma->vm_start, 26041da177e4SLinus Torvalds vma->vm_end, details) < 0) 26051da177e4SLinus Torvalds goto restart; 26061da177e4SLinus Torvalds } 26071da177e4SLinus Torvalds } 26081da177e4SLinus Torvalds 26091da177e4SLinus Torvalds /** 261072fd4a35SRobert P. J. Day * unmap_mapping_range - unmap the portion of all mmaps in the specified address_space corresponding to the specified page range in the underlying file. 26113d41088fSMartin Waitz * @mapping: the address space containing mmaps to be unmapped. 26121da177e4SLinus Torvalds * @holebegin: byte in first page to unmap, relative to the start of 26131da177e4SLinus Torvalds * the underlying file. This will be rounded down to a PAGE_SIZE 261425d9e2d1Snpiggin@suse.de * boundary. Note that this is different from truncate_pagecache(), which 26151da177e4SLinus Torvalds * must keep the partial page. In contrast, we must get rid of 26161da177e4SLinus Torvalds * partial pages. 26171da177e4SLinus Torvalds * @holelen: size of prospective hole in bytes. This will be rounded 26181da177e4SLinus Torvalds * up to a PAGE_SIZE boundary. A holelen of zero truncates to the 26191da177e4SLinus Torvalds * end of the file. 26201da177e4SLinus Torvalds * @even_cows: 1 when truncating a file, unmap even private COWed pages; 26211da177e4SLinus Torvalds * but 0 when invalidating pagecache, don't throw away private data. 26221da177e4SLinus Torvalds */ 26231da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping, 26241da177e4SLinus Torvalds loff_t const holebegin, loff_t const holelen, int even_cows) 26251da177e4SLinus Torvalds { 26261da177e4SLinus Torvalds struct zap_details details; 26271da177e4SLinus Torvalds pgoff_t hba = holebegin >> PAGE_SHIFT; 26281da177e4SLinus Torvalds pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 26291da177e4SLinus Torvalds 26301da177e4SLinus Torvalds /* Check for overflow. */ 26311da177e4SLinus Torvalds if (sizeof(holelen) > sizeof(hlen)) { 26321da177e4SLinus Torvalds long long holeend = 26331da177e4SLinus Torvalds (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 26341da177e4SLinus Torvalds if (holeend & ~(long long)ULONG_MAX) 26351da177e4SLinus Torvalds hlen = ULONG_MAX - hba + 1; 26361da177e4SLinus Torvalds } 26371da177e4SLinus Torvalds 26381da177e4SLinus Torvalds details.check_mapping = even_cows? NULL: mapping; 26391da177e4SLinus Torvalds details.nonlinear_vma = NULL; 26401da177e4SLinus Torvalds details.first_index = hba; 26411da177e4SLinus Torvalds details.last_index = hba + hlen - 1; 26421da177e4SLinus Torvalds if (details.last_index < details.first_index) 26431da177e4SLinus Torvalds details.last_index = ULONG_MAX; 26441da177e4SLinus Torvalds details.i_mmap_lock = &mapping->i_mmap_lock; 26451da177e4SLinus Torvalds 26461da177e4SLinus Torvalds spin_lock(&mapping->i_mmap_lock); 26471da177e4SLinus Torvalds 2648d00806b1SNick Piggin /* Protect against endless unmapping loops */ 26491da177e4SLinus Torvalds mapping->truncate_count++; 26501da177e4SLinus Torvalds if (unlikely(is_restart_addr(mapping->truncate_count))) { 26511da177e4SLinus Torvalds if (mapping->truncate_count == 0) 26521da177e4SLinus Torvalds reset_vma_truncate_counts(mapping); 26531da177e4SLinus Torvalds mapping->truncate_count++; 26541da177e4SLinus Torvalds } 26551da177e4SLinus Torvalds details.truncate_count = mapping->truncate_count; 26561da177e4SLinus Torvalds 26571da177e4SLinus Torvalds if (unlikely(!prio_tree_empty(&mapping->i_mmap))) 26581da177e4SLinus Torvalds unmap_mapping_range_tree(&mapping->i_mmap, &details); 26591da177e4SLinus Torvalds if (unlikely(!list_empty(&mapping->i_mmap_nonlinear))) 26601da177e4SLinus Torvalds unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details); 26611da177e4SLinus Torvalds spin_unlock(&mapping->i_mmap_lock); 26621da177e4SLinus Torvalds } 26631da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range); 26641da177e4SLinus Torvalds 2665f6b3ec23SBadari Pulavarty int vmtruncate_range(struct inode *inode, loff_t offset, loff_t end) 2666f6b3ec23SBadari Pulavarty { 2667f6b3ec23SBadari Pulavarty struct address_space *mapping = inode->i_mapping; 2668f6b3ec23SBadari Pulavarty 2669f6b3ec23SBadari Pulavarty /* 2670f6b3ec23SBadari Pulavarty * If the underlying filesystem is not going to provide 2671f6b3ec23SBadari Pulavarty * a way to truncate a range of blocks (punch a hole) - 2672f6b3ec23SBadari Pulavarty * we should return failure right now. 2673f6b3ec23SBadari Pulavarty */ 2674acfa4380SAl Viro if (!inode->i_op->truncate_range) 2675f6b3ec23SBadari Pulavarty return -ENOSYS; 2676f6b3ec23SBadari Pulavarty 26771b1dcc1bSJes Sorensen mutex_lock(&inode->i_mutex); 2678f6b3ec23SBadari Pulavarty down_write(&inode->i_alloc_sem); 2679f6b3ec23SBadari Pulavarty unmap_mapping_range(mapping, offset, (end - offset), 1); 2680f6b3ec23SBadari Pulavarty truncate_inode_pages_range(mapping, offset, end); 2681d00806b1SNick Piggin unmap_mapping_range(mapping, offset, (end - offset), 1); 2682f6b3ec23SBadari Pulavarty inode->i_op->truncate_range(inode, offset, end); 2683f6b3ec23SBadari Pulavarty up_write(&inode->i_alloc_sem); 26841b1dcc1bSJes Sorensen mutex_unlock(&inode->i_mutex); 2685f6b3ec23SBadari Pulavarty 2686f6b3ec23SBadari Pulavarty return 0; 2687f6b3ec23SBadari Pulavarty } 2688f6b3ec23SBadari Pulavarty 26891da177e4SLinus Torvalds /* 26908f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 26918f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 26928f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 26931da177e4SLinus Torvalds */ 269465500d23SHugh Dickins static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma, 269565500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 269630c9f3a9SLinus Torvalds unsigned int flags, pte_t orig_pte) 26971da177e4SLinus Torvalds { 26988f4e2101SHugh Dickins spinlock_t *ptl; 26994969c119SAndrea Arcangeli struct page *page, *swapcache = NULL; 270065500d23SHugh Dickins swp_entry_t entry; 27011da177e4SLinus Torvalds pte_t pte; 2702d065bd81SMichel Lespinasse int locked; 27037a81b88cSKAMEZAWA Hiroyuki struct mem_cgroup *ptr = NULL; 2704ad8c2ee8SRik van Riel int exclusive = 0; 270583c54070SNick Piggin int ret = 0; 27061da177e4SLinus Torvalds 27074c21e2f2SHugh Dickins if (!pte_unmap_same(mm, pmd, page_table, orig_pte)) 27088f4e2101SHugh Dickins goto out; 270965500d23SHugh Dickins 271065500d23SHugh Dickins entry = pte_to_swp_entry(orig_pte); 2711d1737fdbSAndi Kleen if (unlikely(non_swap_entry(entry))) { 27120697212aSChristoph Lameter if (is_migration_entry(entry)) { 27130697212aSChristoph Lameter migration_entry_wait(mm, pmd, address); 2714d1737fdbSAndi Kleen } else if (is_hwpoison_entry(entry)) { 2715d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 2716d1737fdbSAndi Kleen } else { 2717d1737fdbSAndi Kleen print_bad_pte(vma, address, orig_pte, NULL); 2718d99be1a8SHugh Dickins ret = VM_FAULT_SIGBUS; 2719d1737fdbSAndi Kleen } 27200697212aSChristoph Lameter goto out; 27210697212aSChristoph Lameter } 27220ff92245SShailabh Nagar delayacct_set_flag(DELAYACCT_PF_SWAPIN); 27231da177e4SLinus Torvalds page = lookup_swap_cache(entry); 27241da177e4SLinus Torvalds if (!page) { 2725a5c9b696SHugh Dickins grab_swap_token(mm); /* Contend for token _before_ read-in */ 272602098feaSHugh Dickins page = swapin_readahead(entry, 272702098feaSHugh Dickins GFP_HIGHUSER_MOVABLE, vma, address); 27281da177e4SLinus Torvalds if (!page) { 27291da177e4SLinus Torvalds /* 27308f4e2101SHugh Dickins * Back out if somebody else faulted in this pte 27318f4e2101SHugh Dickins * while we released the pte lock. 27321da177e4SLinus Torvalds */ 27338f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 27341da177e4SLinus Torvalds if (likely(pte_same(*page_table, orig_pte))) 27351da177e4SLinus Torvalds ret = VM_FAULT_OOM; 27360ff92245SShailabh Nagar delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 273765500d23SHugh Dickins goto unlock; 27381da177e4SLinus Torvalds } 27391da177e4SLinus Torvalds 27401da177e4SLinus Torvalds /* Had to read the page from swap area: Major fault */ 27411da177e4SLinus Torvalds ret = VM_FAULT_MAJOR; 2742f8891e5eSChristoph Lameter count_vm_event(PGMAJFAULT); 2743d1737fdbSAndi Kleen } else if (PageHWPoison(page)) { 274471f72525SWu Fengguang /* 274571f72525SWu Fengguang * hwpoisoned dirty swapcache pages are kept for killing 274671f72525SWu Fengguang * owner processes (which may be unknown at hwpoison time) 274771f72525SWu Fengguang */ 2748d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 2749d1737fdbSAndi Kleen delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 27504779cb31SAndi Kleen goto out_release; 27511da177e4SLinus Torvalds } 27521da177e4SLinus Torvalds 2753d065bd81SMichel Lespinasse locked = lock_page_or_retry(page, mm, flags); 275420a1022dSBalbir Singh delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 2755d065bd81SMichel Lespinasse if (!locked) { 2756d065bd81SMichel Lespinasse ret |= VM_FAULT_RETRY; 2757d065bd81SMichel Lespinasse goto out_release; 2758d065bd81SMichel Lespinasse } 27591da177e4SLinus Torvalds 27604969c119SAndrea Arcangeli /* 276131c4a3d3SHugh Dickins * Make sure try_to_free_swap or reuse_swap_page or swapoff did not 276231c4a3d3SHugh Dickins * release the swapcache from under us. The page pin, and pte_same 276331c4a3d3SHugh Dickins * test below, are not enough to exclude that. Even if it is still 276431c4a3d3SHugh Dickins * swapcache, we need to check that the page's swap has not changed. 27654969c119SAndrea Arcangeli */ 276631c4a3d3SHugh Dickins if (unlikely(!PageSwapCache(page) || page_private(page) != entry.val)) 27674969c119SAndrea Arcangeli goto out_page; 27684969c119SAndrea Arcangeli 27694969c119SAndrea Arcangeli if (ksm_might_need_to_copy(page, vma, address)) { 27704969c119SAndrea Arcangeli swapcache = page; 27714969c119SAndrea Arcangeli page = ksm_does_need_to_copy(page, vma, address); 27724969c119SAndrea Arcangeli 27734969c119SAndrea Arcangeli if (unlikely(!page)) { 27745ad64688SHugh Dickins ret = VM_FAULT_OOM; 27754969c119SAndrea Arcangeli page = swapcache; 27764969c119SAndrea Arcangeli swapcache = NULL; 27774969c119SAndrea Arcangeli goto out_page; 27784969c119SAndrea Arcangeli } 27795ad64688SHugh Dickins } 27805ad64688SHugh Dickins 27812c26fdd7SKAMEZAWA Hiroyuki if (mem_cgroup_try_charge_swapin(mm, page, GFP_KERNEL, &ptr)) { 2782073e587eSKAMEZAWA Hiroyuki ret = VM_FAULT_OOM; 2783bc43f75cSJohannes Weiner goto out_page; 2784073e587eSKAMEZAWA Hiroyuki } 2785073e587eSKAMEZAWA Hiroyuki 27861da177e4SLinus Torvalds /* 27878f4e2101SHugh Dickins * Back out if somebody else already faulted in this pte. 27881da177e4SLinus Torvalds */ 27898f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 27909e9bef07SHugh Dickins if (unlikely(!pte_same(*page_table, orig_pte))) 2791b8107480SKirill Korotaev goto out_nomap; 2792b8107480SKirill Korotaev 2793b8107480SKirill Korotaev if (unlikely(!PageUptodate(page))) { 2794b8107480SKirill Korotaev ret = VM_FAULT_SIGBUS; 2795b8107480SKirill Korotaev goto out_nomap; 27961da177e4SLinus Torvalds } 27971da177e4SLinus Torvalds 27988c7c6e34SKAMEZAWA Hiroyuki /* 27998c7c6e34SKAMEZAWA Hiroyuki * The page isn't present yet, go ahead with the fault. 28008c7c6e34SKAMEZAWA Hiroyuki * 28018c7c6e34SKAMEZAWA Hiroyuki * Be careful about the sequence of operations here. 28028c7c6e34SKAMEZAWA Hiroyuki * To get its accounting right, reuse_swap_page() must be called 28038c7c6e34SKAMEZAWA Hiroyuki * while the page is counted on swap but not yet in mapcount i.e. 28048c7c6e34SKAMEZAWA Hiroyuki * before page_add_anon_rmap() and swap_free(); try_to_free_swap() 28058c7c6e34SKAMEZAWA Hiroyuki * must be called after the swap_free(), or it will never succeed. 280603f3c433SKAMEZAWA Hiroyuki * Because delete_from_swap_page() may be called by reuse_swap_page(), 280703f3c433SKAMEZAWA Hiroyuki * mem_cgroup_commit_charge_swapin() may not be able to find swp_entry 280803f3c433SKAMEZAWA Hiroyuki * in page->private. In this case, a record in swap_cgroup is silently 280903f3c433SKAMEZAWA Hiroyuki * discarded at swap_free(). 28108c7c6e34SKAMEZAWA Hiroyuki */ 28111da177e4SLinus Torvalds 281234e55232SKAMEZAWA Hiroyuki inc_mm_counter_fast(mm, MM_ANONPAGES); 2813b084d435SKAMEZAWA Hiroyuki dec_mm_counter_fast(mm, MM_SWAPENTS); 28141da177e4SLinus Torvalds pte = mk_pte(page, vma->vm_page_prot); 281530c9f3a9SLinus Torvalds if ((flags & FAULT_FLAG_WRITE) && reuse_swap_page(page)) { 28161da177e4SLinus Torvalds pte = maybe_mkwrite(pte_mkdirty(pte), vma); 281730c9f3a9SLinus Torvalds flags &= ~FAULT_FLAG_WRITE; 28189a5b489bSAndrea Arcangeli ret |= VM_FAULT_WRITE; 2819ad8c2ee8SRik van Riel exclusive = 1; 28201da177e4SLinus Torvalds } 28211da177e4SLinus Torvalds flush_icache_page(vma, page); 28221da177e4SLinus Torvalds set_pte_at(mm, address, page_table, pte); 2823ad8c2ee8SRik van Riel do_page_add_anon_rmap(page, vma, address, exclusive); 282403f3c433SKAMEZAWA Hiroyuki /* It's better to call commit-charge after rmap is established */ 282503f3c433SKAMEZAWA Hiroyuki mem_cgroup_commit_charge_swapin(page, ptr); 28261da177e4SLinus Torvalds 2827c475a8abSHugh Dickins swap_free(entry); 2828b291f000SNick Piggin if (vm_swap_full() || (vma->vm_flags & VM_LOCKED) || PageMlocked(page)) 2829a2c43eedSHugh Dickins try_to_free_swap(page); 2830c475a8abSHugh Dickins unlock_page(page); 28314969c119SAndrea Arcangeli if (swapcache) { 28324969c119SAndrea Arcangeli /* 28334969c119SAndrea Arcangeli * Hold the lock to avoid the swap entry to be reused 28344969c119SAndrea Arcangeli * until we take the PT lock for the pte_same() check 28354969c119SAndrea Arcangeli * (to avoid false positives from pte_same). For 28364969c119SAndrea Arcangeli * further safety release the lock after the swap_free 28374969c119SAndrea Arcangeli * so that the swap count won't change under a 28384969c119SAndrea Arcangeli * parallel locked swapcache. 28394969c119SAndrea Arcangeli */ 28404969c119SAndrea Arcangeli unlock_page(swapcache); 28414969c119SAndrea Arcangeli page_cache_release(swapcache); 28424969c119SAndrea Arcangeli } 2843c475a8abSHugh Dickins 284430c9f3a9SLinus Torvalds if (flags & FAULT_FLAG_WRITE) { 284561469f1dSHugh Dickins ret |= do_wp_page(mm, vma, address, page_table, pmd, ptl, pte); 284661469f1dSHugh Dickins if (ret & VM_FAULT_ERROR) 284761469f1dSHugh Dickins ret &= VM_FAULT_ERROR; 28481da177e4SLinus Torvalds goto out; 28491da177e4SLinus Torvalds } 28501da177e4SLinus Torvalds 28511da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 28524b3073e1SRussell King update_mmu_cache(vma, address, page_table); 285365500d23SHugh Dickins unlock: 28548f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 28551da177e4SLinus Torvalds out: 28561da177e4SLinus Torvalds return ret; 2857b8107480SKirill Korotaev out_nomap: 28587a81b88cSKAMEZAWA Hiroyuki mem_cgroup_cancel_charge_swapin(ptr); 28598f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 2860bc43f75cSJohannes Weiner out_page: 2861b8107480SKirill Korotaev unlock_page(page); 28624779cb31SAndi Kleen out_release: 2863b8107480SKirill Korotaev page_cache_release(page); 28644969c119SAndrea Arcangeli if (swapcache) { 28654969c119SAndrea Arcangeli unlock_page(swapcache); 28664969c119SAndrea Arcangeli page_cache_release(swapcache); 28674969c119SAndrea Arcangeli } 286865500d23SHugh Dickins return ret; 28691da177e4SLinus Torvalds } 28701da177e4SLinus Torvalds 28711da177e4SLinus Torvalds /* 28728ca3eb08SLuck, Tony * This is like a special single-page "expand_{down|up}wards()", 28738ca3eb08SLuck, Tony * except we must first make sure that 'address{-|+}PAGE_SIZE' 2874320b2b8dSLinus Torvalds * doesn't hit another vma. 2875320b2b8dSLinus Torvalds */ 2876320b2b8dSLinus Torvalds static inline int check_stack_guard_page(struct vm_area_struct *vma, unsigned long address) 2877320b2b8dSLinus Torvalds { 2878320b2b8dSLinus Torvalds address &= PAGE_MASK; 2879320b2b8dSLinus Torvalds if ((vma->vm_flags & VM_GROWSDOWN) && address == vma->vm_start) { 28800e8e50e2SLinus Torvalds struct vm_area_struct *prev = vma->vm_prev; 2881320b2b8dSLinus Torvalds 28820e8e50e2SLinus Torvalds /* 28830e8e50e2SLinus Torvalds * Is there a mapping abutting this one below? 28840e8e50e2SLinus Torvalds * 28850e8e50e2SLinus Torvalds * That's only ok if it's the same stack mapping 28860e8e50e2SLinus Torvalds * that has gotten split.. 28870e8e50e2SLinus Torvalds */ 28880e8e50e2SLinus Torvalds if (prev && prev->vm_end == address) 28890e8e50e2SLinus Torvalds return prev->vm_flags & VM_GROWSDOWN ? 0 : -ENOMEM; 28900e8e50e2SLinus Torvalds 28910e8e50e2SLinus Torvalds expand_stack(vma, address - PAGE_SIZE); 2892320b2b8dSLinus Torvalds } 28938ca3eb08SLuck, Tony if ((vma->vm_flags & VM_GROWSUP) && address + PAGE_SIZE == vma->vm_end) { 28948ca3eb08SLuck, Tony struct vm_area_struct *next = vma->vm_next; 28958ca3eb08SLuck, Tony 28968ca3eb08SLuck, Tony /* As VM_GROWSDOWN but s/below/above/ */ 28978ca3eb08SLuck, Tony if (next && next->vm_start == address + PAGE_SIZE) 28988ca3eb08SLuck, Tony return next->vm_flags & VM_GROWSUP ? 0 : -ENOMEM; 28998ca3eb08SLuck, Tony 29008ca3eb08SLuck, Tony expand_upwards(vma, address + PAGE_SIZE); 29018ca3eb08SLuck, Tony } 2902320b2b8dSLinus Torvalds return 0; 2903320b2b8dSLinus Torvalds } 2904320b2b8dSLinus Torvalds 2905320b2b8dSLinus Torvalds /* 29068f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 29078f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 29088f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 29091da177e4SLinus Torvalds */ 291065500d23SHugh Dickins static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma, 291165500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 291230c9f3a9SLinus Torvalds unsigned int flags) 29131da177e4SLinus Torvalds { 29148f4e2101SHugh Dickins struct page *page; 29158f4e2101SHugh Dickins spinlock_t *ptl; 29161da177e4SLinus Torvalds pte_t entry; 29171da177e4SLinus Torvalds 29185528f913SLinus Torvalds pte_unmap(page_table); 2919320b2b8dSLinus Torvalds 292011ac5524SLinus Torvalds /* Check if we need to add a guard page to the stack */ 292111ac5524SLinus Torvalds if (check_stack_guard_page(vma, address) < 0) 292211ac5524SLinus Torvalds return VM_FAULT_SIGBUS; 292311ac5524SLinus Torvalds 292411ac5524SLinus Torvalds /* Use the zero-page for reads */ 292562eede62SHugh Dickins if (!(flags & FAULT_FLAG_WRITE)) { 292662eede62SHugh Dickins entry = pte_mkspecial(pfn_pte(my_zero_pfn(address), 292762eede62SHugh Dickins vma->vm_page_prot)); 292811ac5524SLinus Torvalds page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 2929a13ea5b7SHugh Dickins if (!pte_none(*page_table)) 2930a13ea5b7SHugh Dickins goto unlock; 2931a13ea5b7SHugh Dickins goto setpte; 2932a13ea5b7SHugh Dickins } 2933a13ea5b7SHugh Dickins 29341da177e4SLinus Torvalds /* Allocate our own private page. */ 29351da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 293665500d23SHugh Dickins goto oom; 2937769848c0SMel Gorman page = alloc_zeroed_user_highpage_movable(vma, address); 29381da177e4SLinus Torvalds if (!page) 293965500d23SHugh Dickins goto oom; 29400ed361deSNick Piggin __SetPageUptodate(page); 29411da177e4SLinus Torvalds 29422c26fdd7SKAMEZAWA Hiroyuki if (mem_cgroup_newpage_charge(page, mm, GFP_KERNEL)) 29438a9f3ccdSBalbir Singh goto oom_free_page; 29448a9f3ccdSBalbir Singh 294565500d23SHugh Dickins entry = mk_pte(page, vma->vm_page_prot); 29461ac0cb5dSHugh Dickins if (vma->vm_flags & VM_WRITE) 29471ac0cb5dSHugh Dickins entry = pte_mkwrite(pte_mkdirty(entry)); 29488f4e2101SHugh Dickins 29498f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 29501c2fb7a4SAndrea Arcangeli if (!pte_none(*page_table)) 29518f4e2101SHugh Dickins goto release; 29529ba69294SHugh Dickins 295334e55232SKAMEZAWA Hiroyuki inc_mm_counter_fast(mm, MM_ANONPAGES); 29549617d95eSNick Piggin page_add_new_anon_rmap(page, vma, address); 2955a13ea5b7SHugh Dickins setpte: 295665500d23SHugh Dickins set_pte_at(mm, address, page_table, entry); 29571da177e4SLinus Torvalds 29581da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 29594b3073e1SRussell King update_mmu_cache(vma, address, page_table); 296065500d23SHugh Dickins unlock: 29618f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 296283c54070SNick Piggin return 0; 29638f4e2101SHugh Dickins release: 29648a9f3ccdSBalbir Singh mem_cgroup_uncharge_page(page); 29658f4e2101SHugh Dickins page_cache_release(page); 29668f4e2101SHugh Dickins goto unlock; 29678a9f3ccdSBalbir Singh oom_free_page: 29686dbf6d3bSHugh Dickins page_cache_release(page); 296965500d23SHugh Dickins oom: 29701da177e4SLinus Torvalds return VM_FAULT_OOM; 29711da177e4SLinus Torvalds } 29721da177e4SLinus Torvalds 29731da177e4SLinus Torvalds /* 297454cb8821SNick Piggin * __do_fault() tries to create a new page mapping. It aggressively 29751da177e4SLinus Torvalds * tries to share with existing pages, but makes a separate copy if 297654cb8821SNick Piggin * the FAULT_FLAG_WRITE is set in the flags parameter in order to avoid 297754cb8821SNick Piggin * the next page fault. 29781da177e4SLinus Torvalds * 29791da177e4SLinus Torvalds * As this is called only for pages that do not currently exist, we 29801da177e4SLinus Torvalds * do not need to flush old virtual caches or the TLB. 29811da177e4SLinus Torvalds * 29828f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 298316abfa08SHugh Dickins * but allow concurrent faults), and pte neither mapped nor locked. 29848f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 29851da177e4SLinus Torvalds */ 298654cb8821SNick Piggin static int __do_fault(struct mm_struct *mm, struct vm_area_struct *vma, 298716abfa08SHugh Dickins unsigned long address, pmd_t *pmd, 298854cb8821SNick Piggin pgoff_t pgoff, unsigned int flags, pte_t orig_pte) 29891da177e4SLinus Torvalds { 299016abfa08SHugh Dickins pte_t *page_table; 29918f4e2101SHugh Dickins spinlock_t *ptl; 2992d0217ac0SNick Piggin struct page *page; 29931da177e4SLinus Torvalds pte_t entry; 29941da177e4SLinus Torvalds int anon = 0; 29955b4e655eSKAMEZAWA Hiroyuki int charged = 0; 2996d08b3851SPeter Zijlstra struct page *dirty_page = NULL; 2997d0217ac0SNick Piggin struct vm_fault vmf; 2998d0217ac0SNick Piggin int ret; 2999a200ee18SPeter Zijlstra int page_mkwrite = 0; 300054cb8821SNick Piggin 3001d0217ac0SNick Piggin vmf.virtual_address = (void __user *)(address & PAGE_MASK); 3002d0217ac0SNick Piggin vmf.pgoff = pgoff; 3003d0217ac0SNick Piggin vmf.flags = flags; 3004d0217ac0SNick Piggin vmf.page = NULL; 30051da177e4SLinus Torvalds 3006d0217ac0SNick Piggin ret = vma->vm_ops->fault(vma, &vmf); 3007d065bd81SMichel Lespinasse if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | 3008d065bd81SMichel Lespinasse VM_FAULT_RETRY))) 300983c54070SNick Piggin return ret; 30101da177e4SLinus Torvalds 3011a3b947eaSAndi Kleen if (unlikely(PageHWPoison(vmf.page))) { 3012a3b947eaSAndi Kleen if (ret & VM_FAULT_LOCKED) 3013a3b947eaSAndi Kleen unlock_page(vmf.page); 3014a3b947eaSAndi Kleen return VM_FAULT_HWPOISON; 3015a3b947eaSAndi Kleen } 3016a3b947eaSAndi Kleen 3017d00806b1SNick Piggin /* 3018d0217ac0SNick Piggin * For consistency in subsequent calls, make the faulted page always 3019d00806b1SNick Piggin * locked. 3020d00806b1SNick Piggin */ 302183c54070SNick Piggin if (unlikely(!(ret & VM_FAULT_LOCKED))) 3022d0217ac0SNick Piggin lock_page(vmf.page); 302354cb8821SNick Piggin else 3024d0217ac0SNick Piggin VM_BUG_ON(!PageLocked(vmf.page)); 3025d00806b1SNick Piggin 30261da177e4SLinus Torvalds /* 30271da177e4SLinus Torvalds * Should we do an early C-O-W break? 30281da177e4SLinus Torvalds */ 3029d0217ac0SNick Piggin page = vmf.page; 303054cb8821SNick Piggin if (flags & FAULT_FLAG_WRITE) { 30319637a5efSDavid Howells if (!(vma->vm_flags & VM_SHARED)) { 303254cb8821SNick Piggin anon = 1; 3033d00806b1SNick Piggin if (unlikely(anon_vma_prepare(vma))) { 3034d0217ac0SNick Piggin ret = VM_FAULT_OOM; 303554cb8821SNick Piggin goto out; 3036d00806b1SNick Piggin } 303783c54070SNick Piggin page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, 303883c54070SNick Piggin vma, address); 3039d00806b1SNick Piggin if (!page) { 3040d0217ac0SNick Piggin ret = VM_FAULT_OOM; 304154cb8821SNick Piggin goto out; 3042d00806b1SNick Piggin } 30432c26fdd7SKAMEZAWA Hiroyuki if (mem_cgroup_newpage_charge(page, mm, GFP_KERNEL)) { 30445b4e655eSKAMEZAWA Hiroyuki ret = VM_FAULT_OOM; 30455b4e655eSKAMEZAWA Hiroyuki page_cache_release(page); 30465b4e655eSKAMEZAWA Hiroyuki goto out; 30475b4e655eSKAMEZAWA Hiroyuki } 30485b4e655eSKAMEZAWA Hiroyuki charged = 1; 3049b291f000SNick Piggin /* 3050b291f000SNick Piggin * Don't let another task, with possibly unlocked vma, 3051b291f000SNick Piggin * keep the mlocked page. 3052b291f000SNick Piggin */ 3053b291f000SNick Piggin if (vma->vm_flags & VM_LOCKED) 3054b291f000SNick Piggin clear_page_mlock(vmf.page); 3055d0217ac0SNick Piggin copy_user_highpage(page, vmf.page, address, vma); 30560ed361deSNick Piggin __SetPageUptodate(page); 30579637a5efSDavid Howells } else { 305854cb8821SNick Piggin /* 305954cb8821SNick Piggin * If the page will be shareable, see if the backing 30609637a5efSDavid Howells * address space wants to know that the page is about 306154cb8821SNick Piggin * to become writable 306254cb8821SNick Piggin */ 306369676147SMark Fasheh if (vma->vm_ops->page_mkwrite) { 3064c2ec175cSNick Piggin int tmp; 3065c2ec175cSNick Piggin 306669676147SMark Fasheh unlock_page(page); 3067b827e496SNick Piggin vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE; 3068c2ec175cSNick Piggin tmp = vma->vm_ops->page_mkwrite(vma, &vmf); 3069c2ec175cSNick Piggin if (unlikely(tmp & 3070c2ec175cSNick Piggin (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) { 3071c2ec175cSNick Piggin ret = tmp; 3072b827e496SNick Piggin goto unwritable_page; 307369676147SMark Fasheh } 3074b827e496SNick Piggin if (unlikely(!(tmp & VM_FAULT_LOCKED))) { 307569676147SMark Fasheh lock_page(page); 3076d0217ac0SNick Piggin if (!page->mapping) { 3077b827e496SNick Piggin ret = 0; /* retry the fault */ 3078b827e496SNick Piggin unlock_page(page); 3079b827e496SNick Piggin goto unwritable_page; 3080d0217ac0SNick Piggin } 3081b827e496SNick Piggin } else 3082b827e496SNick Piggin VM_BUG_ON(!PageLocked(page)); 3083a200ee18SPeter Zijlstra page_mkwrite = 1; 30849637a5efSDavid Howells } 30859637a5efSDavid Howells } 308654cb8821SNick Piggin 30871da177e4SLinus Torvalds } 30881da177e4SLinus Torvalds 30898f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 30901da177e4SLinus Torvalds 30911da177e4SLinus Torvalds /* 30921da177e4SLinus Torvalds * This silly early PAGE_DIRTY setting removes a race 30931da177e4SLinus Torvalds * due to the bad i386 page protection. But it's valid 30941da177e4SLinus Torvalds * for other architectures too. 30951da177e4SLinus Torvalds * 309630c9f3a9SLinus Torvalds * Note that if FAULT_FLAG_WRITE is set, we either now have 30971da177e4SLinus Torvalds * an exclusive copy of the page, or this is a shared mapping, 30981da177e4SLinus Torvalds * so we can make it writable and dirty to avoid having to 30991da177e4SLinus Torvalds * handle that later. 31001da177e4SLinus Torvalds */ 31011da177e4SLinus Torvalds /* Only go through if we didn't race with anybody else... */ 31021c2fb7a4SAndrea Arcangeli if (likely(pte_same(*page_table, orig_pte))) { 3103d00806b1SNick Piggin flush_icache_page(vma, page); 3104d00806b1SNick Piggin entry = mk_pte(page, vma->vm_page_prot); 310554cb8821SNick Piggin if (flags & FAULT_FLAG_WRITE) 31061da177e4SLinus Torvalds entry = maybe_mkwrite(pte_mkdirty(entry), vma); 31071da177e4SLinus Torvalds if (anon) { 310834e55232SKAMEZAWA Hiroyuki inc_mm_counter_fast(mm, MM_ANONPAGES); 3109d00806b1SNick Piggin page_add_new_anon_rmap(page, vma, address); 3110f57e88a8SHugh Dickins } else { 311134e55232SKAMEZAWA Hiroyuki inc_mm_counter_fast(mm, MM_FILEPAGES); 3112d00806b1SNick Piggin page_add_file_rmap(page); 311354cb8821SNick Piggin if (flags & FAULT_FLAG_WRITE) { 3114d00806b1SNick Piggin dirty_page = page; 3115d08b3851SPeter Zijlstra get_page(dirty_page); 3116d08b3851SPeter Zijlstra } 31174294621fSHugh Dickins } 311864d6519dSLee Schermerhorn set_pte_at(mm, address, page_table, entry); 31191da177e4SLinus Torvalds 3120d00806b1SNick Piggin /* no need to invalidate: a not-present page won't be cached */ 31214b3073e1SRussell King update_mmu_cache(vma, address, page_table); 3122d00806b1SNick Piggin } else { 31235b4e655eSKAMEZAWA Hiroyuki if (charged) 31248a9f3ccdSBalbir Singh mem_cgroup_uncharge_page(page); 3125d00806b1SNick Piggin if (anon) 3126d00806b1SNick Piggin page_cache_release(page); 3127d00806b1SNick Piggin else 312854cb8821SNick Piggin anon = 1; /* no anon but release faulted_page */ 3129d00806b1SNick Piggin } 3130d00806b1SNick Piggin 31318f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 3132d00806b1SNick Piggin 3133d00806b1SNick Piggin out: 3134b827e496SNick Piggin if (dirty_page) { 3135b827e496SNick Piggin struct address_space *mapping = page->mapping; 31368f7b3d15SAnton Salikhmetov 3137b827e496SNick Piggin if (set_page_dirty(dirty_page)) 3138b827e496SNick Piggin page_mkwrite = 1; 3139b827e496SNick Piggin unlock_page(dirty_page); 3140d08b3851SPeter Zijlstra put_page(dirty_page); 3141b827e496SNick Piggin if (page_mkwrite && mapping) { 3142b827e496SNick Piggin /* 3143b827e496SNick Piggin * Some device drivers do not set page.mapping but still 3144b827e496SNick Piggin * dirty their pages 3145b827e496SNick Piggin */ 3146b827e496SNick Piggin balance_dirty_pages_ratelimited(mapping); 3147d08b3851SPeter Zijlstra } 3148d00806b1SNick Piggin 3149b827e496SNick Piggin /* file_update_time outside page_lock */ 3150b827e496SNick Piggin if (vma->vm_file) 3151b827e496SNick Piggin file_update_time(vma->vm_file); 3152b827e496SNick Piggin } else { 3153b827e496SNick Piggin unlock_page(vmf.page); 3154b827e496SNick Piggin if (anon) 3155b827e496SNick Piggin page_cache_release(vmf.page); 3156b827e496SNick Piggin } 3157b827e496SNick Piggin 3158b827e496SNick Piggin return ret; 3159b827e496SNick Piggin 3160b827e496SNick Piggin unwritable_page: 3161b827e496SNick Piggin page_cache_release(page); 316283c54070SNick Piggin return ret; 316354cb8821SNick Piggin } 3164d00806b1SNick Piggin 316554cb8821SNick Piggin static int do_linear_fault(struct mm_struct *mm, struct vm_area_struct *vma, 316654cb8821SNick Piggin unsigned long address, pte_t *page_table, pmd_t *pmd, 316730c9f3a9SLinus Torvalds unsigned int flags, pte_t orig_pte) 316854cb8821SNick Piggin { 316954cb8821SNick Piggin pgoff_t pgoff = (((address & PAGE_MASK) 31700da7e01fSDean Nelson - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; 317154cb8821SNick Piggin 317216abfa08SHugh Dickins pte_unmap(page_table); 317316abfa08SHugh Dickins return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte); 317454cb8821SNick Piggin } 317554cb8821SNick Piggin 3176f4b81804SJes Sorensen /* 31771da177e4SLinus Torvalds * Fault of a previously existing named mapping. Repopulate the pte 31781da177e4SLinus Torvalds * from the encoded file_pte if possible. This enables swappable 31791da177e4SLinus Torvalds * nonlinear vmas. 31808f4e2101SHugh Dickins * 31818f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 31828f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 31838f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 31841da177e4SLinus Torvalds */ 3185d0217ac0SNick Piggin static int do_nonlinear_fault(struct mm_struct *mm, struct vm_area_struct *vma, 318665500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 318730c9f3a9SLinus Torvalds unsigned int flags, pte_t orig_pte) 31881da177e4SLinus Torvalds { 318965500d23SHugh Dickins pgoff_t pgoff; 31901da177e4SLinus Torvalds 319130c9f3a9SLinus Torvalds flags |= FAULT_FLAG_NONLINEAR; 319230c9f3a9SLinus Torvalds 31934c21e2f2SHugh Dickins if (!pte_unmap_same(mm, pmd, page_table, orig_pte)) 319483c54070SNick Piggin return 0; 31951da177e4SLinus Torvalds 31962509ef26SHugh Dickins if (unlikely(!(vma->vm_flags & VM_NONLINEAR))) { 319765500d23SHugh Dickins /* 319865500d23SHugh Dickins * Page table corrupted: show pte and kill process. 319965500d23SHugh Dickins */ 32003dc14741SHugh Dickins print_bad_pte(vma, address, orig_pte, NULL); 3201d99be1a8SHugh Dickins return VM_FAULT_SIGBUS; 320265500d23SHugh Dickins } 320365500d23SHugh Dickins 320465500d23SHugh Dickins pgoff = pte_to_pgoff(orig_pte); 320516abfa08SHugh Dickins return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte); 32061da177e4SLinus Torvalds } 32071da177e4SLinus Torvalds 32081da177e4SLinus Torvalds /* 32091da177e4SLinus Torvalds * These routines also need to handle stuff like marking pages dirty 32101da177e4SLinus Torvalds * and/or accessed for architectures that don't do it in hardware (most 32111da177e4SLinus Torvalds * RISC architectures). The early dirtying is also good on the i386. 32121da177e4SLinus Torvalds * 32131da177e4SLinus Torvalds * There is also a hook called "update_mmu_cache()" that architectures 32141da177e4SLinus Torvalds * with external mmu caches can use to update those (ie the Sparc or 32151da177e4SLinus Torvalds * PowerPC hashed page tables that act as extended TLBs). 32161da177e4SLinus Torvalds * 3217c74df32cSHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 3218c74df32cSHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 3219c74df32cSHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 32201da177e4SLinus Torvalds */ 322171e3aac0SAndrea Arcangeli int handle_pte_fault(struct mm_struct *mm, 32221da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long address, 322330c9f3a9SLinus Torvalds pte_t *pte, pmd_t *pmd, unsigned int flags) 32241da177e4SLinus Torvalds { 32251da177e4SLinus Torvalds pte_t entry; 32268f4e2101SHugh Dickins spinlock_t *ptl; 32271da177e4SLinus Torvalds 32288dab5241SBenjamin Herrenschmidt entry = *pte; 32291da177e4SLinus Torvalds if (!pte_present(entry)) { 323065500d23SHugh Dickins if (pte_none(entry)) { 3231f4b81804SJes Sorensen if (vma->vm_ops) { 32323c18ddd1SNick Piggin if (likely(vma->vm_ops->fault)) 323354cb8821SNick Piggin return do_linear_fault(mm, vma, address, 323430c9f3a9SLinus Torvalds pte, pmd, flags, entry); 3235f4b81804SJes Sorensen } 3236f4b81804SJes Sorensen return do_anonymous_page(mm, vma, address, 323730c9f3a9SLinus Torvalds pte, pmd, flags); 323865500d23SHugh Dickins } 32391da177e4SLinus Torvalds if (pte_file(entry)) 3240d0217ac0SNick Piggin return do_nonlinear_fault(mm, vma, address, 324130c9f3a9SLinus Torvalds pte, pmd, flags, entry); 324265500d23SHugh Dickins return do_swap_page(mm, vma, address, 324330c9f3a9SLinus Torvalds pte, pmd, flags, entry); 32441da177e4SLinus Torvalds } 32451da177e4SLinus Torvalds 32464c21e2f2SHugh Dickins ptl = pte_lockptr(mm, pmd); 32478f4e2101SHugh Dickins spin_lock(ptl); 32488f4e2101SHugh Dickins if (unlikely(!pte_same(*pte, entry))) 32498f4e2101SHugh Dickins goto unlock; 325030c9f3a9SLinus Torvalds if (flags & FAULT_FLAG_WRITE) { 32511da177e4SLinus Torvalds if (!pte_write(entry)) 32528f4e2101SHugh Dickins return do_wp_page(mm, vma, address, 32538f4e2101SHugh Dickins pte, pmd, ptl, entry); 32541da177e4SLinus Torvalds entry = pte_mkdirty(entry); 32551da177e4SLinus Torvalds } 32561da177e4SLinus Torvalds entry = pte_mkyoung(entry); 325730c9f3a9SLinus Torvalds if (ptep_set_access_flags(vma, address, pte, entry, flags & FAULT_FLAG_WRITE)) { 32584b3073e1SRussell King update_mmu_cache(vma, address, pte); 32591a44e149SAndrea Arcangeli } else { 32601a44e149SAndrea Arcangeli /* 32611a44e149SAndrea Arcangeli * This is needed only for protection faults but the arch code 32621a44e149SAndrea Arcangeli * is not yet telling us if this is a protection fault or not. 32631a44e149SAndrea Arcangeli * This still avoids useless tlb flushes for .text page faults 32641a44e149SAndrea Arcangeli * with threads. 32651a44e149SAndrea Arcangeli */ 326630c9f3a9SLinus Torvalds if (flags & FAULT_FLAG_WRITE) 326761c77326SShaohua Li flush_tlb_fix_spurious_fault(vma, address); 32681a44e149SAndrea Arcangeli } 32698f4e2101SHugh Dickins unlock: 32708f4e2101SHugh Dickins pte_unmap_unlock(pte, ptl); 327183c54070SNick Piggin return 0; 32721da177e4SLinus Torvalds } 32731da177e4SLinus Torvalds 32741da177e4SLinus Torvalds /* 32751da177e4SLinus Torvalds * By the time we get here, we already hold the mm semaphore 32761da177e4SLinus Torvalds */ 327783c54070SNick Piggin int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma, 3278d06063ccSLinus Torvalds unsigned long address, unsigned int flags) 32791da177e4SLinus Torvalds { 32801da177e4SLinus Torvalds pgd_t *pgd; 32811da177e4SLinus Torvalds pud_t *pud; 32821da177e4SLinus Torvalds pmd_t *pmd; 32831da177e4SLinus Torvalds pte_t *pte; 32841da177e4SLinus Torvalds 32851da177e4SLinus Torvalds __set_current_state(TASK_RUNNING); 32861da177e4SLinus Torvalds 3287f8891e5eSChristoph Lameter count_vm_event(PGFAULT); 32881da177e4SLinus Torvalds 328934e55232SKAMEZAWA Hiroyuki /* do counter updates before entering really critical section. */ 329034e55232SKAMEZAWA Hiroyuki check_sync_rss_stat(current); 329134e55232SKAMEZAWA Hiroyuki 3292ac9b9c66SHugh Dickins if (unlikely(is_vm_hugetlb_page(vma))) 329330c9f3a9SLinus Torvalds return hugetlb_fault(mm, vma, address, flags); 32941da177e4SLinus Torvalds 32951da177e4SLinus Torvalds pgd = pgd_offset(mm, address); 32961da177e4SLinus Torvalds pud = pud_alloc(mm, pgd, address); 32971da177e4SLinus Torvalds if (!pud) 3298c74df32cSHugh Dickins return VM_FAULT_OOM; 32991da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, address); 33001da177e4SLinus Torvalds if (!pmd) 3301c74df32cSHugh Dickins return VM_FAULT_OOM; 330271e3aac0SAndrea Arcangeli if (pmd_none(*pmd) && transparent_hugepage_enabled(vma)) { 330371e3aac0SAndrea Arcangeli if (!vma->vm_ops) 330471e3aac0SAndrea Arcangeli return do_huge_pmd_anonymous_page(mm, vma, address, 330571e3aac0SAndrea Arcangeli pmd, flags); 330671e3aac0SAndrea Arcangeli } else { 330771e3aac0SAndrea Arcangeli pmd_t orig_pmd = *pmd; 330871e3aac0SAndrea Arcangeli barrier(); 330971e3aac0SAndrea Arcangeli if (pmd_trans_huge(orig_pmd)) { 331071e3aac0SAndrea Arcangeli if (flags & FAULT_FLAG_WRITE && 331171e3aac0SAndrea Arcangeli !pmd_write(orig_pmd) && 331271e3aac0SAndrea Arcangeli !pmd_trans_splitting(orig_pmd)) 331371e3aac0SAndrea Arcangeli return do_huge_pmd_wp_page(mm, vma, address, 331471e3aac0SAndrea Arcangeli pmd, orig_pmd); 331571e3aac0SAndrea Arcangeli return 0; 331671e3aac0SAndrea Arcangeli } 331771e3aac0SAndrea Arcangeli } 331871e3aac0SAndrea Arcangeli 331971e3aac0SAndrea Arcangeli /* 332071e3aac0SAndrea Arcangeli * Use __pte_alloc instead of pte_alloc_map, because we can't 332171e3aac0SAndrea Arcangeli * run pte_offset_map on the pmd, if an huge pmd could 332271e3aac0SAndrea Arcangeli * materialize from under us from a different thread. 332371e3aac0SAndrea Arcangeli */ 332471e3aac0SAndrea Arcangeli if (unlikely(__pte_alloc(mm, vma, pmd, address))) 3325c74df32cSHugh Dickins return VM_FAULT_OOM; 332671e3aac0SAndrea Arcangeli /* if an huge pmd materialized from under us just retry later */ 332771e3aac0SAndrea Arcangeli if (unlikely(pmd_trans_huge(*pmd))) 332871e3aac0SAndrea Arcangeli return 0; 332971e3aac0SAndrea Arcangeli /* 333071e3aac0SAndrea Arcangeli * A regular pmd is established and it can't morph into a huge pmd 333171e3aac0SAndrea Arcangeli * from under us anymore at this point because we hold the mmap_sem 333271e3aac0SAndrea Arcangeli * read mode and khugepaged takes it in write mode. So now it's 333371e3aac0SAndrea Arcangeli * safe to run pte_offset_map(). 333471e3aac0SAndrea Arcangeli */ 333571e3aac0SAndrea Arcangeli pte = pte_offset_map(pmd, address); 33361da177e4SLinus Torvalds 333730c9f3a9SLinus Torvalds return handle_pte_fault(mm, vma, address, pte, pmd, flags); 33381da177e4SLinus Torvalds } 33391da177e4SLinus Torvalds 33401da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED 33411da177e4SLinus Torvalds /* 33421da177e4SLinus Torvalds * Allocate page upper directory. 3343872fec16SHugh Dickins * We've already handled the fast-path in-line. 33441da177e4SLinus Torvalds */ 33451bb3630eSHugh Dickins int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address) 33461da177e4SLinus Torvalds { 3347c74df32cSHugh Dickins pud_t *new = pud_alloc_one(mm, address); 3348c74df32cSHugh Dickins if (!new) 33491bb3630eSHugh Dickins return -ENOMEM; 33501da177e4SLinus Torvalds 3351362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 3352362a61adSNick Piggin 3353872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 33541bb3630eSHugh Dickins if (pgd_present(*pgd)) /* Another has populated it */ 33555e541973SBenjamin Herrenschmidt pud_free(mm, new); 33561bb3630eSHugh Dickins else 33571da177e4SLinus Torvalds pgd_populate(mm, pgd, new); 3358872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 33591bb3630eSHugh Dickins return 0; 33601da177e4SLinus Torvalds } 33611da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */ 33621da177e4SLinus Torvalds 33631da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED 33641da177e4SLinus Torvalds /* 33651da177e4SLinus Torvalds * Allocate page middle directory. 3366872fec16SHugh Dickins * We've already handled the fast-path in-line. 33671da177e4SLinus Torvalds */ 33681bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) 33691da177e4SLinus Torvalds { 3370c74df32cSHugh Dickins pmd_t *new = pmd_alloc_one(mm, address); 3371c74df32cSHugh Dickins if (!new) 33721bb3630eSHugh Dickins return -ENOMEM; 33731da177e4SLinus Torvalds 3374362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 3375362a61adSNick Piggin 3376872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 33771da177e4SLinus Torvalds #ifndef __ARCH_HAS_4LEVEL_HACK 33781bb3630eSHugh Dickins if (pud_present(*pud)) /* Another has populated it */ 33795e541973SBenjamin Herrenschmidt pmd_free(mm, new); 33801bb3630eSHugh Dickins else 33811da177e4SLinus Torvalds pud_populate(mm, pud, new); 33821da177e4SLinus Torvalds #else 33831bb3630eSHugh Dickins if (pgd_present(*pud)) /* Another has populated it */ 33845e541973SBenjamin Herrenschmidt pmd_free(mm, new); 33851bb3630eSHugh Dickins else 33861da177e4SLinus Torvalds pgd_populate(mm, pud, new); 33871da177e4SLinus Torvalds #endif /* __ARCH_HAS_4LEVEL_HACK */ 3388872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 33891bb3630eSHugh Dickins return 0; 33901da177e4SLinus Torvalds } 33911da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */ 33921da177e4SLinus Torvalds 33931da177e4SLinus Torvalds int make_pages_present(unsigned long addr, unsigned long end) 33941da177e4SLinus Torvalds { 33951da177e4SLinus Torvalds int ret, len, write; 33961da177e4SLinus Torvalds struct vm_area_struct * vma; 33971da177e4SLinus Torvalds 33981da177e4SLinus Torvalds vma = find_vma(current->mm, addr); 33991da177e4SLinus Torvalds if (!vma) 3400a477097dSKOSAKI Motohiro return -ENOMEM; 34015ecfda04SMichel Lespinasse /* 34025ecfda04SMichel Lespinasse * We want to touch writable mappings with a write fault in order 34035ecfda04SMichel Lespinasse * to break COW, except for shared mappings because these don't COW 34045ecfda04SMichel Lespinasse * and we would not want to dirty them for nothing. 34055ecfda04SMichel Lespinasse */ 34065ecfda04SMichel Lespinasse write = (vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE; 34075bcb28b1SEric Sesterhenn BUG_ON(addr >= end); 34085bcb28b1SEric Sesterhenn BUG_ON(end > vma->vm_end); 340968e116a3SRolf Eike Beer len = DIV_ROUND_UP(end, PAGE_SIZE) - addr/PAGE_SIZE; 34101da177e4SLinus Torvalds ret = get_user_pages(current, current->mm, addr, 34111da177e4SLinus Torvalds len, write, 0, NULL, NULL); 3412c11d69d8SLee Schermerhorn if (ret < 0) 34131da177e4SLinus Torvalds return ret; 34149978ad58SLee Schermerhorn return ret == len ? 0 : -EFAULT; 34151da177e4SLinus Torvalds } 34161da177e4SLinus Torvalds 34171da177e4SLinus Torvalds #if !defined(__HAVE_ARCH_GATE_AREA) 34181da177e4SLinus Torvalds 34191da177e4SLinus Torvalds #if defined(AT_SYSINFO_EHDR) 34205ce7852cSAdrian Bunk static struct vm_area_struct gate_vma; 34211da177e4SLinus Torvalds 34221da177e4SLinus Torvalds static int __init gate_vma_init(void) 34231da177e4SLinus Torvalds { 34241da177e4SLinus Torvalds gate_vma.vm_mm = NULL; 34251da177e4SLinus Torvalds gate_vma.vm_start = FIXADDR_USER_START; 34261da177e4SLinus Torvalds gate_vma.vm_end = FIXADDR_USER_END; 3427b6558c4aSRoland McGrath gate_vma.vm_flags = VM_READ | VM_MAYREAD | VM_EXEC | VM_MAYEXEC; 3428b6558c4aSRoland McGrath gate_vma.vm_page_prot = __P101; 3429f47aef55SRoland McGrath /* 3430f47aef55SRoland McGrath * Make sure the vDSO gets into every core dump. 3431f47aef55SRoland McGrath * Dumping its contents makes post-mortem fully interpretable later 3432f47aef55SRoland McGrath * without matching up the same kernel and hardware config to see 3433f47aef55SRoland McGrath * what PC values meant. 3434f47aef55SRoland McGrath */ 3435f47aef55SRoland McGrath gate_vma.vm_flags |= VM_ALWAYSDUMP; 34361da177e4SLinus Torvalds return 0; 34371da177e4SLinus Torvalds } 34381da177e4SLinus Torvalds __initcall(gate_vma_init); 34391da177e4SLinus Torvalds #endif 34401da177e4SLinus Torvalds 34411da177e4SLinus Torvalds struct vm_area_struct *get_gate_vma(struct task_struct *tsk) 34421da177e4SLinus Torvalds { 34431da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR 34441da177e4SLinus Torvalds return &gate_vma; 34451da177e4SLinus Torvalds #else 34461da177e4SLinus Torvalds return NULL; 34471da177e4SLinus Torvalds #endif 34481da177e4SLinus Torvalds } 34491da177e4SLinus Torvalds 34501da177e4SLinus Torvalds int in_gate_area_no_task(unsigned long addr) 34511da177e4SLinus Torvalds { 34521da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR 34531da177e4SLinus Torvalds if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END)) 34541da177e4SLinus Torvalds return 1; 34551da177e4SLinus Torvalds #endif 34561da177e4SLinus Torvalds return 0; 34571da177e4SLinus Torvalds } 34581da177e4SLinus Torvalds 34591da177e4SLinus Torvalds #endif /* __HAVE_ARCH_GATE_AREA */ 34600ec76a11SDavid Howells 34611b36ba81SNamhyung Kim static int __follow_pte(struct mm_struct *mm, unsigned long address, 3462f8ad0f49SJohannes Weiner pte_t **ptepp, spinlock_t **ptlp) 3463f8ad0f49SJohannes Weiner { 3464f8ad0f49SJohannes Weiner pgd_t *pgd; 3465f8ad0f49SJohannes Weiner pud_t *pud; 3466f8ad0f49SJohannes Weiner pmd_t *pmd; 3467f8ad0f49SJohannes Weiner pte_t *ptep; 3468f8ad0f49SJohannes Weiner 3469f8ad0f49SJohannes Weiner pgd = pgd_offset(mm, address); 3470f8ad0f49SJohannes Weiner if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) 3471f8ad0f49SJohannes Weiner goto out; 3472f8ad0f49SJohannes Weiner 3473f8ad0f49SJohannes Weiner pud = pud_offset(pgd, address); 3474f8ad0f49SJohannes Weiner if (pud_none(*pud) || unlikely(pud_bad(*pud))) 3475f8ad0f49SJohannes Weiner goto out; 3476f8ad0f49SJohannes Weiner 3477f8ad0f49SJohannes Weiner pmd = pmd_offset(pud, address); 3478*f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 3479f8ad0f49SJohannes Weiner if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd))) 3480f8ad0f49SJohannes Weiner goto out; 3481f8ad0f49SJohannes Weiner 3482f8ad0f49SJohannes Weiner /* We cannot handle huge page PFN maps. Luckily they don't exist. */ 3483f8ad0f49SJohannes Weiner if (pmd_huge(*pmd)) 3484f8ad0f49SJohannes Weiner goto out; 3485f8ad0f49SJohannes Weiner 3486f8ad0f49SJohannes Weiner ptep = pte_offset_map_lock(mm, pmd, address, ptlp); 3487f8ad0f49SJohannes Weiner if (!ptep) 3488f8ad0f49SJohannes Weiner goto out; 3489f8ad0f49SJohannes Weiner if (!pte_present(*ptep)) 3490f8ad0f49SJohannes Weiner goto unlock; 3491f8ad0f49SJohannes Weiner *ptepp = ptep; 3492f8ad0f49SJohannes Weiner return 0; 3493f8ad0f49SJohannes Weiner unlock: 3494f8ad0f49SJohannes Weiner pte_unmap_unlock(ptep, *ptlp); 3495f8ad0f49SJohannes Weiner out: 3496f8ad0f49SJohannes Weiner return -EINVAL; 3497f8ad0f49SJohannes Weiner } 3498f8ad0f49SJohannes Weiner 34991b36ba81SNamhyung Kim static inline int follow_pte(struct mm_struct *mm, unsigned long address, 35001b36ba81SNamhyung Kim pte_t **ptepp, spinlock_t **ptlp) 35011b36ba81SNamhyung Kim { 35021b36ba81SNamhyung Kim int res; 35031b36ba81SNamhyung Kim 35041b36ba81SNamhyung Kim /* (void) is needed to make gcc happy */ 35051b36ba81SNamhyung Kim (void) __cond_lock(*ptlp, 35061b36ba81SNamhyung Kim !(res = __follow_pte(mm, address, ptepp, ptlp))); 35071b36ba81SNamhyung Kim return res; 35081b36ba81SNamhyung Kim } 35091b36ba81SNamhyung Kim 35103b6748e2SJohannes Weiner /** 35113b6748e2SJohannes Weiner * follow_pfn - look up PFN at a user virtual address 35123b6748e2SJohannes Weiner * @vma: memory mapping 35133b6748e2SJohannes Weiner * @address: user virtual address 35143b6748e2SJohannes Weiner * @pfn: location to store found PFN 35153b6748e2SJohannes Weiner * 35163b6748e2SJohannes Weiner * Only IO mappings and raw PFN mappings are allowed. 35173b6748e2SJohannes Weiner * 35183b6748e2SJohannes Weiner * Returns zero and the pfn at @pfn on success, -ve otherwise. 35193b6748e2SJohannes Weiner */ 35203b6748e2SJohannes Weiner int follow_pfn(struct vm_area_struct *vma, unsigned long address, 35213b6748e2SJohannes Weiner unsigned long *pfn) 35223b6748e2SJohannes Weiner { 35233b6748e2SJohannes Weiner int ret = -EINVAL; 35243b6748e2SJohannes Weiner spinlock_t *ptl; 35253b6748e2SJohannes Weiner pte_t *ptep; 35263b6748e2SJohannes Weiner 35273b6748e2SJohannes Weiner if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 35283b6748e2SJohannes Weiner return ret; 35293b6748e2SJohannes Weiner 35303b6748e2SJohannes Weiner ret = follow_pte(vma->vm_mm, address, &ptep, &ptl); 35313b6748e2SJohannes Weiner if (ret) 35323b6748e2SJohannes Weiner return ret; 35333b6748e2SJohannes Weiner *pfn = pte_pfn(*ptep); 35343b6748e2SJohannes Weiner pte_unmap_unlock(ptep, ptl); 35353b6748e2SJohannes Weiner return 0; 35363b6748e2SJohannes Weiner } 35373b6748e2SJohannes Weiner EXPORT_SYMBOL(follow_pfn); 35383b6748e2SJohannes Weiner 353928b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT 3540d87fe660Svenkatesh.pallipadi@intel.com int follow_phys(struct vm_area_struct *vma, 354128b2ee20SRik van Riel unsigned long address, unsigned int flags, 3542d87fe660Svenkatesh.pallipadi@intel.com unsigned long *prot, resource_size_t *phys) 354328b2ee20SRik van Riel { 354403668a4dSJohannes Weiner int ret = -EINVAL; 354528b2ee20SRik van Riel pte_t *ptep, pte; 354628b2ee20SRik van Riel spinlock_t *ptl; 354728b2ee20SRik van Riel 3548d87fe660Svenkatesh.pallipadi@intel.com if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 3549d87fe660Svenkatesh.pallipadi@intel.com goto out; 355028b2ee20SRik van Riel 355103668a4dSJohannes Weiner if (follow_pte(vma->vm_mm, address, &ptep, &ptl)) 3552d87fe660Svenkatesh.pallipadi@intel.com goto out; 355328b2ee20SRik van Riel pte = *ptep; 355403668a4dSJohannes Weiner 355528b2ee20SRik van Riel if ((flags & FOLL_WRITE) && !pte_write(pte)) 355628b2ee20SRik van Riel goto unlock; 355728b2ee20SRik van Riel 355828b2ee20SRik van Riel *prot = pgprot_val(pte_pgprot(pte)); 355903668a4dSJohannes Weiner *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT; 356028b2ee20SRik van Riel 356103668a4dSJohannes Weiner ret = 0; 356228b2ee20SRik van Riel unlock: 356328b2ee20SRik van Riel pte_unmap_unlock(ptep, ptl); 356428b2ee20SRik van Riel out: 3565d87fe660Svenkatesh.pallipadi@intel.com return ret; 356628b2ee20SRik van Riel } 356728b2ee20SRik van Riel 356828b2ee20SRik van Riel int generic_access_phys(struct vm_area_struct *vma, unsigned long addr, 356928b2ee20SRik van Riel void *buf, int len, int write) 357028b2ee20SRik van Riel { 357128b2ee20SRik van Riel resource_size_t phys_addr; 357228b2ee20SRik van Riel unsigned long prot = 0; 35732bc7273bSKOSAKI Motohiro void __iomem *maddr; 357428b2ee20SRik van Riel int offset = addr & (PAGE_SIZE-1); 357528b2ee20SRik van Riel 3576d87fe660Svenkatesh.pallipadi@intel.com if (follow_phys(vma, addr, write, &prot, &phys_addr)) 357728b2ee20SRik van Riel return -EINVAL; 357828b2ee20SRik van Riel 357928b2ee20SRik van Riel maddr = ioremap_prot(phys_addr, PAGE_SIZE, prot); 358028b2ee20SRik van Riel if (write) 358128b2ee20SRik van Riel memcpy_toio(maddr + offset, buf, len); 358228b2ee20SRik van Riel else 358328b2ee20SRik van Riel memcpy_fromio(buf, maddr + offset, len); 358428b2ee20SRik van Riel iounmap(maddr); 358528b2ee20SRik van Riel 358628b2ee20SRik van Riel return len; 358728b2ee20SRik van Riel } 358828b2ee20SRik van Riel #endif 358928b2ee20SRik van Riel 35900ec76a11SDavid Howells /* 35910ec76a11SDavid Howells * Access another process' address space. 35920ec76a11SDavid Howells * Source/target buffer must be kernel space, 35930ec76a11SDavid Howells * Do not walk the page table directly, use get_user_pages 35940ec76a11SDavid Howells */ 35950ec76a11SDavid Howells int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write) 35960ec76a11SDavid Howells { 35970ec76a11SDavid Howells struct mm_struct *mm; 35980ec76a11SDavid Howells struct vm_area_struct *vma; 35990ec76a11SDavid Howells void *old_buf = buf; 36000ec76a11SDavid Howells 36010ec76a11SDavid Howells mm = get_task_mm(tsk); 36020ec76a11SDavid Howells if (!mm) 36030ec76a11SDavid Howells return 0; 36040ec76a11SDavid Howells 36050ec76a11SDavid Howells down_read(&mm->mmap_sem); 3606183ff22bSSimon Arlott /* ignore errors, just check how much was successfully transferred */ 36070ec76a11SDavid Howells while (len) { 36080ec76a11SDavid Howells int bytes, ret, offset; 36090ec76a11SDavid Howells void *maddr; 361028b2ee20SRik van Riel struct page *page = NULL; 36110ec76a11SDavid Howells 36120ec76a11SDavid Howells ret = get_user_pages(tsk, mm, addr, 1, 36130ec76a11SDavid Howells write, 1, &page, &vma); 361428b2ee20SRik van Riel if (ret <= 0) { 361528b2ee20SRik van Riel /* 361628b2ee20SRik van Riel * Check if this is a VM_IO | VM_PFNMAP VMA, which 361728b2ee20SRik van Riel * we can access using slightly different code. 361828b2ee20SRik van Riel */ 361928b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT 362028b2ee20SRik van Riel vma = find_vma(mm, addr); 362128b2ee20SRik van Riel if (!vma) 36220ec76a11SDavid Howells break; 362328b2ee20SRik van Riel if (vma->vm_ops && vma->vm_ops->access) 362428b2ee20SRik van Riel ret = vma->vm_ops->access(vma, addr, buf, 362528b2ee20SRik van Riel len, write); 362628b2ee20SRik van Riel if (ret <= 0) 362728b2ee20SRik van Riel #endif 362828b2ee20SRik van Riel break; 362928b2ee20SRik van Riel bytes = ret; 363028b2ee20SRik van Riel } else { 36310ec76a11SDavid Howells bytes = len; 36320ec76a11SDavid Howells offset = addr & (PAGE_SIZE-1); 36330ec76a11SDavid Howells if (bytes > PAGE_SIZE-offset) 36340ec76a11SDavid Howells bytes = PAGE_SIZE-offset; 36350ec76a11SDavid Howells 36360ec76a11SDavid Howells maddr = kmap(page); 36370ec76a11SDavid Howells if (write) { 36380ec76a11SDavid Howells copy_to_user_page(vma, page, addr, 36390ec76a11SDavid Howells maddr + offset, buf, bytes); 36400ec76a11SDavid Howells set_page_dirty_lock(page); 36410ec76a11SDavid Howells } else { 36420ec76a11SDavid Howells copy_from_user_page(vma, page, addr, 36430ec76a11SDavid Howells buf, maddr + offset, bytes); 36440ec76a11SDavid Howells } 36450ec76a11SDavid Howells kunmap(page); 36460ec76a11SDavid Howells page_cache_release(page); 364728b2ee20SRik van Riel } 36480ec76a11SDavid Howells len -= bytes; 36490ec76a11SDavid Howells buf += bytes; 36500ec76a11SDavid Howells addr += bytes; 36510ec76a11SDavid Howells } 36520ec76a11SDavid Howells up_read(&mm->mmap_sem); 36530ec76a11SDavid Howells mmput(mm); 36540ec76a11SDavid Howells 36550ec76a11SDavid Howells return buf - old_buf; 36560ec76a11SDavid Howells } 365703252919SAndi Kleen 365803252919SAndi Kleen /* 365903252919SAndi Kleen * Print the name of a VMA. 366003252919SAndi Kleen */ 366103252919SAndi Kleen void print_vma_addr(char *prefix, unsigned long ip) 366203252919SAndi Kleen { 366303252919SAndi Kleen struct mm_struct *mm = current->mm; 366403252919SAndi Kleen struct vm_area_struct *vma; 366503252919SAndi Kleen 3666e8bff74aSIngo Molnar /* 3667e8bff74aSIngo Molnar * Do not print if we are in atomic 3668e8bff74aSIngo Molnar * contexts (in exception stacks, etc.): 3669e8bff74aSIngo Molnar */ 3670e8bff74aSIngo Molnar if (preempt_count()) 3671e8bff74aSIngo Molnar return; 3672e8bff74aSIngo Molnar 367303252919SAndi Kleen down_read(&mm->mmap_sem); 367403252919SAndi Kleen vma = find_vma(mm, ip); 367503252919SAndi Kleen if (vma && vma->vm_file) { 367603252919SAndi Kleen struct file *f = vma->vm_file; 367703252919SAndi Kleen char *buf = (char *)__get_free_page(GFP_KERNEL); 367803252919SAndi Kleen if (buf) { 367903252919SAndi Kleen char *p, *s; 368003252919SAndi Kleen 3681cf28b486SJan Blunck p = d_path(&f->f_path, buf, PAGE_SIZE); 368203252919SAndi Kleen if (IS_ERR(p)) 368303252919SAndi Kleen p = "?"; 368403252919SAndi Kleen s = strrchr(p, '/'); 368503252919SAndi Kleen if (s) 368603252919SAndi Kleen p = s+1; 368703252919SAndi Kleen printk("%s%s[%lx+%lx]", prefix, p, 368803252919SAndi Kleen vma->vm_start, 368903252919SAndi Kleen vma->vm_end - vma->vm_start); 369003252919SAndi Kleen free_page((unsigned long)buf); 369103252919SAndi Kleen } 369203252919SAndi Kleen } 369303252919SAndi Kleen up_read(¤t->mm->mmap_sem); 369403252919SAndi Kleen } 36953ee1afa3SNick Piggin 36963ee1afa3SNick Piggin #ifdef CONFIG_PROVE_LOCKING 36973ee1afa3SNick Piggin void might_fault(void) 36983ee1afa3SNick Piggin { 369995156f00SPeter Zijlstra /* 370095156f00SPeter Zijlstra * Some code (nfs/sunrpc) uses socket ops on kernel memory while 370195156f00SPeter Zijlstra * holding the mmap_sem, this is safe because kernel memory doesn't 370295156f00SPeter Zijlstra * get paged out, therefore we'll never actually fault, and the 370395156f00SPeter Zijlstra * below annotations will generate false positives. 370495156f00SPeter Zijlstra */ 370595156f00SPeter Zijlstra if (segment_eq(get_fs(), KERNEL_DS)) 370695156f00SPeter Zijlstra return; 370795156f00SPeter Zijlstra 37083ee1afa3SNick Piggin might_sleep(); 37093ee1afa3SNick Piggin /* 37103ee1afa3SNick Piggin * it would be nicer only to annotate paths which are not under 37113ee1afa3SNick Piggin * pagefault_disable, however that requires a larger audit and 37123ee1afa3SNick Piggin * providing helpers like get_user_atomic. 37133ee1afa3SNick Piggin */ 37143ee1afa3SNick Piggin if (!in_atomic() && current->mm) 37153ee1afa3SNick Piggin might_lock_read(¤t->mm->mmap_sem); 37163ee1afa3SNick Piggin } 37173ee1afa3SNick Piggin EXPORT_SYMBOL(might_fault); 37183ee1afa3SNick Piggin #endif 371947ad8475SAndrea Arcangeli 372047ad8475SAndrea Arcangeli #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS) 372147ad8475SAndrea Arcangeli static void clear_gigantic_page(struct page *page, 372247ad8475SAndrea Arcangeli unsigned long addr, 372347ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 372447ad8475SAndrea Arcangeli { 372547ad8475SAndrea Arcangeli int i; 372647ad8475SAndrea Arcangeli struct page *p = page; 372747ad8475SAndrea Arcangeli 372847ad8475SAndrea Arcangeli might_sleep(); 372947ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; 373047ad8475SAndrea Arcangeli i++, p = mem_map_next(p, page, i)) { 373147ad8475SAndrea Arcangeli cond_resched(); 373247ad8475SAndrea Arcangeli clear_user_highpage(p, addr + i * PAGE_SIZE); 373347ad8475SAndrea Arcangeli } 373447ad8475SAndrea Arcangeli } 373547ad8475SAndrea Arcangeli void clear_huge_page(struct page *page, 373647ad8475SAndrea Arcangeli unsigned long addr, unsigned int pages_per_huge_page) 373747ad8475SAndrea Arcangeli { 373847ad8475SAndrea Arcangeli int i; 373947ad8475SAndrea Arcangeli 374047ad8475SAndrea Arcangeli if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { 374147ad8475SAndrea Arcangeli clear_gigantic_page(page, addr, pages_per_huge_page); 374247ad8475SAndrea Arcangeli return; 374347ad8475SAndrea Arcangeli } 374447ad8475SAndrea Arcangeli 374547ad8475SAndrea Arcangeli might_sleep(); 374647ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; i++) { 374747ad8475SAndrea Arcangeli cond_resched(); 374847ad8475SAndrea Arcangeli clear_user_highpage(page + i, addr + i * PAGE_SIZE); 374947ad8475SAndrea Arcangeli } 375047ad8475SAndrea Arcangeli } 375147ad8475SAndrea Arcangeli 375247ad8475SAndrea Arcangeli static void copy_user_gigantic_page(struct page *dst, struct page *src, 375347ad8475SAndrea Arcangeli unsigned long addr, 375447ad8475SAndrea Arcangeli struct vm_area_struct *vma, 375547ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 375647ad8475SAndrea Arcangeli { 375747ad8475SAndrea Arcangeli int i; 375847ad8475SAndrea Arcangeli struct page *dst_base = dst; 375947ad8475SAndrea Arcangeli struct page *src_base = src; 376047ad8475SAndrea Arcangeli 376147ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; ) { 376247ad8475SAndrea Arcangeli cond_resched(); 376347ad8475SAndrea Arcangeli copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma); 376447ad8475SAndrea Arcangeli 376547ad8475SAndrea Arcangeli i++; 376647ad8475SAndrea Arcangeli dst = mem_map_next(dst, dst_base, i); 376747ad8475SAndrea Arcangeli src = mem_map_next(src, src_base, i); 376847ad8475SAndrea Arcangeli } 376947ad8475SAndrea Arcangeli } 377047ad8475SAndrea Arcangeli 377147ad8475SAndrea Arcangeli void copy_user_huge_page(struct page *dst, struct page *src, 377247ad8475SAndrea Arcangeli unsigned long addr, struct vm_area_struct *vma, 377347ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 377447ad8475SAndrea Arcangeli { 377547ad8475SAndrea Arcangeli int i; 377647ad8475SAndrea Arcangeli 377747ad8475SAndrea Arcangeli if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { 377847ad8475SAndrea Arcangeli copy_user_gigantic_page(dst, src, addr, vma, 377947ad8475SAndrea Arcangeli pages_per_huge_page); 378047ad8475SAndrea Arcangeli return; 378147ad8475SAndrea Arcangeli } 378247ad8475SAndrea Arcangeli 378347ad8475SAndrea Arcangeli might_sleep(); 378447ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; i++) { 378547ad8475SAndrea Arcangeli cond_resched(); 378647ad8475SAndrea Arcangeli copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma); 378747ad8475SAndrea Arcangeli } 378847ad8475SAndrea Arcangeli } 378947ad8475SAndrea Arcangeli #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */ 3790