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 } 1859547d01bSPeter Zijlstra #else /* SPLIT_RSS_COUNTING */ 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 1949547d01bSPeter Zijlstra #endif /* SPLIT_RSS_COUNTING */ 1959547d01bSPeter Zijlstra 1969547d01bSPeter Zijlstra #ifdef HAVE_GENERIC_MMU_GATHER 1979547d01bSPeter Zijlstra 1989547d01bSPeter Zijlstra static int tlb_next_batch(struct mmu_gather *tlb) 1999547d01bSPeter Zijlstra { 2009547d01bSPeter Zijlstra struct mmu_gather_batch *batch; 2019547d01bSPeter Zijlstra 2029547d01bSPeter Zijlstra batch = tlb->active; 2039547d01bSPeter Zijlstra if (batch->next) { 2049547d01bSPeter Zijlstra tlb->active = batch->next; 2059547d01bSPeter Zijlstra return 1; 2069547d01bSPeter Zijlstra } 2079547d01bSPeter Zijlstra 2089547d01bSPeter Zijlstra batch = (void *)__get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0); 2099547d01bSPeter Zijlstra if (!batch) 2109547d01bSPeter Zijlstra return 0; 2119547d01bSPeter Zijlstra 2129547d01bSPeter Zijlstra batch->next = NULL; 2139547d01bSPeter Zijlstra batch->nr = 0; 2149547d01bSPeter Zijlstra batch->max = MAX_GATHER_BATCH; 2159547d01bSPeter Zijlstra 2169547d01bSPeter Zijlstra tlb->active->next = batch; 2179547d01bSPeter Zijlstra tlb->active = batch; 2189547d01bSPeter Zijlstra 2199547d01bSPeter Zijlstra return 1; 2209547d01bSPeter Zijlstra } 2219547d01bSPeter Zijlstra 2229547d01bSPeter Zijlstra /* tlb_gather_mmu 2239547d01bSPeter Zijlstra * Called to initialize an (on-stack) mmu_gather structure for page-table 2249547d01bSPeter Zijlstra * tear-down from @mm. The @fullmm argument is used when @mm is without 2259547d01bSPeter Zijlstra * users and we're going to destroy the full address space (exit/execve). 2269547d01bSPeter Zijlstra */ 2279547d01bSPeter Zijlstra void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, bool fullmm) 2289547d01bSPeter Zijlstra { 2299547d01bSPeter Zijlstra tlb->mm = mm; 2309547d01bSPeter Zijlstra 2319547d01bSPeter Zijlstra tlb->fullmm = fullmm; 2329547d01bSPeter Zijlstra tlb->need_flush = 0; 2339547d01bSPeter Zijlstra tlb->fast_mode = (num_possible_cpus() == 1); 2349547d01bSPeter Zijlstra tlb->local.next = NULL; 2359547d01bSPeter Zijlstra tlb->local.nr = 0; 2369547d01bSPeter Zijlstra tlb->local.max = ARRAY_SIZE(tlb->__pages); 2379547d01bSPeter Zijlstra tlb->active = &tlb->local; 2389547d01bSPeter Zijlstra 2399547d01bSPeter Zijlstra #ifdef CONFIG_HAVE_RCU_TABLE_FREE 2409547d01bSPeter Zijlstra tlb->batch = NULL; 24134e55232SKAMEZAWA Hiroyuki #endif 2429547d01bSPeter Zijlstra } 2439547d01bSPeter Zijlstra 2449547d01bSPeter Zijlstra void tlb_flush_mmu(struct mmu_gather *tlb) 2459547d01bSPeter Zijlstra { 2469547d01bSPeter Zijlstra struct mmu_gather_batch *batch; 2479547d01bSPeter Zijlstra 2489547d01bSPeter Zijlstra if (!tlb->need_flush) 2499547d01bSPeter Zijlstra return; 2509547d01bSPeter Zijlstra tlb->need_flush = 0; 2519547d01bSPeter Zijlstra tlb_flush(tlb); 2529547d01bSPeter Zijlstra #ifdef CONFIG_HAVE_RCU_TABLE_FREE 2539547d01bSPeter Zijlstra tlb_table_flush(tlb); 2549547d01bSPeter Zijlstra #endif 2559547d01bSPeter Zijlstra 2569547d01bSPeter Zijlstra if (tlb_fast_mode(tlb)) 2579547d01bSPeter Zijlstra return; 2589547d01bSPeter Zijlstra 2599547d01bSPeter Zijlstra for (batch = &tlb->local; batch; batch = batch->next) { 2609547d01bSPeter Zijlstra free_pages_and_swap_cache(batch->pages, batch->nr); 2619547d01bSPeter Zijlstra batch->nr = 0; 2629547d01bSPeter Zijlstra } 2639547d01bSPeter Zijlstra tlb->active = &tlb->local; 2649547d01bSPeter Zijlstra } 2659547d01bSPeter Zijlstra 2669547d01bSPeter Zijlstra /* tlb_finish_mmu 2679547d01bSPeter Zijlstra * Called at the end of the shootdown operation to free up any resources 2689547d01bSPeter Zijlstra * that were required. 2699547d01bSPeter Zijlstra */ 2709547d01bSPeter Zijlstra void tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end) 2719547d01bSPeter Zijlstra { 2729547d01bSPeter Zijlstra struct mmu_gather_batch *batch, *next; 2739547d01bSPeter Zijlstra 2749547d01bSPeter Zijlstra tlb_flush_mmu(tlb); 2759547d01bSPeter Zijlstra 2769547d01bSPeter Zijlstra /* keep the page table cache within bounds */ 2779547d01bSPeter Zijlstra check_pgt_cache(); 2789547d01bSPeter Zijlstra 2799547d01bSPeter Zijlstra for (batch = tlb->local.next; batch; batch = next) { 2809547d01bSPeter Zijlstra next = batch->next; 2819547d01bSPeter Zijlstra free_pages((unsigned long)batch, 0); 2829547d01bSPeter Zijlstra } 2839547d01bSPeter Zijlstra tlb->local.next = NULL; 2849547d01bSPeter Zijlstra } 2859547d01bSPeter Zijlstra 2869547d01bSPeter Zijlstra /* __tlb_remove_page 2879547d01bSPeter Zijlstra * Must perform the equivalent to __free_pte(pte_get_and_clear(ptep)), while 2889547d01bSPeter Zijlstra * handling the additional races in SMP caused by other CPUs caching valid 2899547d01bSPeter Zijlstra * mappings in their TLBs. Returns the number of free page slots left. 2909547d01bSPeter Zijlstra * When out of page slots we must call tlb_flush_mmu(). 2919547d01bSPeter Zijlstra */ 2929547d01bSPeter Zijlstra int __tlb_remove_page(struct mmu_gather *tlb, struct page *page) 2939547d01bSPeter Zijlstra { 2949547d01bSPeter Zijlstra struct mmu_gather_batch *batch; 2959547d01bSPeter Zijlstra 2969547d01bSPeter Zijlstra tlb->need_flush = 1; 2979547d01bSPeter Zijlstra 2989547d01bSPeter Zijlstra if (tlb_fast_mode(tlb)) { 2999547d01bSPeter Zijlstra free_page_and_swap_cache(page); 3009547d01bSPeter Zijlstra return 1; /* avoid calling tlb_flush_mmu() */ 3019547d01bSPeter Zijlstra } 3029547d01bSPeter Zijlstra 3039547d01bSPeter Zijlstra batch = tlb->active; 3049547d01bSPeter Zijlstra batch->pages[batch->nr++] = page; 3059547d01bSPeter Zijlstra if (batch->nr == batch->max) { 3069547d01bSPeter Zijlstra if (!tlb_next_batch(tlb)) 3079547d01bSPeter Zijlstra return 0; 3089547d01bSPeter Zijlstra } 3099547d01bSPeter Zijlstra VM_BUG_ON(batch->nr > batch->max); 3109547d01bSPeter Zijlstra 3119547d01bSPeter Zijlstra return batch->max - batch->nr; 3129547d01bSPeter Zijlstra } 3139547d01bSPeter Zijlstra 3149547d01bSPeter Zijlstra #endif /* HAVE_GENERIC_MMU_GATHER */ 31534e55232SKAMEZAWA Hiroyuki 31626723911SPeter Zijlstra #ifdef CONFIG_HAVE_RCU_TABLE_FREE 31726723911SPeter Zijlstra 31826723911SPeter Zijlstra /* 31926723911SPeter Zijlstra * See the comment near struct mmu_table_batch. 32026723911SPeter Zijlstra */ 32126723911SPeter Zijlstra 32226723911SPeter Zijlstra static void tlb_remove_table_smp_sync(void *arg) 32326723911SPeter Zijlstra { 32426723911SPeter Zijlstra /* Simply deliver the interrupt */ 32526723911SPeter Zijlstra } 32626723911SPeter Zijlstra 32726723911SPeter Zijlstra static void tlb_remove_table_one(void *table) 32826723911SPeter Zijlstra { 32926723911SPeter Zijlstra /* 33026723911SPeter Zijlstra * This isn't an RCU grace period and hence the page-tables cannot be 33126723911SPeter Zijlstra * assumed to be actually RCU-freed. 33226723911SPeter Zijlstra * 33326723911SPeter Zijlstra * It is however sufficient for software page-table walkers that rely on 33426723911SPeter Zijlstra * IRQ disabling. See the comment near struct mmu_table_batch. 33526723911SPeter Zijlstra */ 33626723911SPeter Zijlstra smp_call_function(tlb_remove_table_smp_sync, NULL, 1); 33726723911SPeter Zijlstra __tlb_remove_table(table); 33826723911SPeter Zijlstra } 33926723911SPeter Zijlstra 34026723911SPeter Zijlstra static void tlb_remove_table_rcu(struct rcu_head *head) 34126723911SPeter Zijlstra { 34226723911SPeter Zijlstra struct mmu_table_batch *batch; 34326723911SPeter Zijlstra int i; 34426723911SPeter Zijlstra 34526723911SPeter Zijlstra batch = container_of(head, struct mmu_table_batch, rcu); 34626723911SPeter Zijlstra 34726723911SPeter Zijlstra for (i = 0; i < batch->nr; i++) 34826723911SPeter Zijlstra __tlb_remove_table(batch->tables[i]); 34926723911SPeter Zijlstra 35026723911SPeter Zijlstra free_page((unsigned long)batch); 35126723911SPeter Zijlstra } 35226723911SPeter Zijlstra 35326723911SPeter Zijlstra void tlb_table_flush(struct mmu_gather *tlb) 35426723911SPeter Zijlstra { 35526723911SPeter Zijlstra struct mmu_table_batch **batch = &tlb->batch; 35626723911SPeter Zijlstra 35726723911SPeter Zijlstra if (*batch) { 35826723911SPeter Zijlstra call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu); 35926723911SPeter Zijlstra *batch = NULL; 36026723911SPeter Zijlstra } 36126723911SPeter Zijlstra } 36226723911SPeter Zijlstra 36326723911SPeter Zijlstra void tlb_remove_table(struct mmu_gather *tlb, void *table) 36426723911SPeter Zijlstra { 36526723911SPeter Zijlstra struct mmu_table_batch **batch = &tlb->batch; 36626723911SPeter Zijlstra 36726723911SPeter Zijlstra tlb->need_flush = 1; 36826723911SPeter Zijlstra 36926723911SPeter Zijlstra /* 37026723911SPeter Zijlstra * When there's less then two users of this mm there cannot be a 37126723911SPeter Zijlstra * concurrent page-table walk. 37226723911SPeter Zijlstra */ 37326723911SPeter Zijlstra if (atomic_read(&tlb->mm->mm_users) < 2) { 37426723911SPeter Zijlstra __tlb_remove_table(table); 37526723911SPeter Zijlstra return; 37626723911SPeter Zijlstra } 37726723911SPeter Zijlstra 37826723911SPeter Zijlstra if (*batch == NULL) { 37926723911SPeter Zijlstra *batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN); 38026723911SPeter Zijlstra if (*batch == NULL) { 38126723911SPeter Zijlstra tlb_remove_table_one(table); 38226723911SPeter Zijlstra return; 38326723911SPeter Zijlstra } 38426723911SPeter Zijlstra (*batch)->nr = 0; 38526723911SPeter Zijlstra } 38626723911SPeter Zijlstra (*batch)->tables[(*batch)->nr++] = table; 38726723911SPeter Zijlstra if ((*batch)->nr == MAX_TABLE_BATCH) 38826723911SPeter Zijlstra tlb_table_flush(tlb); 38926723911SPeter Zijlstra } 39026723911SPeter Zijlstra 3919547d01bSPeter Zijlstra #endif /* CONFIG_HAVE_RCU_TABLE_FREE */ 39226723911SPeter Zijlstra 3931da177e4SLinus Torvalds /* 3941da177e4SLinus Torvalds * If a p?d_bad entry is found while walking page tables, report 3951da177e4SLinus Torvalds * the error, before resetting entry to p?d_none. Usually (but 3961da177e4SLinus Torvalds * very seldom) called out from the p?d_none_or_clear_bad macros. 3971da177e4SLinus Torvalds */ 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds void pgd_clear_bad(pgd_t *pgd) 4001da177e4SLinus Torvalds { 4011da177e4SLinus Torvalds pgd_ERROR(*pgd); 4021da177e4SLinus Torvalds pgd_clear(pgd); 4031da177e4SLinus Torvalds } 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds void pud_clear_bad(pud_t *pud) 4061da177e4SLinus Torvalds { 4071da177e4SLinus Torvalds pud_ERROR(*pud); 4081da177e4SLinus Torvalds pud_clear(pud); 4091da177e4SLinus Torvalds } 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds void pmd_clear_bad(pmd_t *pmd) 4121da177e4SLinus Torvalds { 4131da177e4SLinus Torvalds pmd_ERROR(*pmd); 4141da177e4SLinus Torvalds pmd_clear(pmd); 4151da177e4SLinus Torvalds } 4161da177e4SLinus Torvalds 4171da177e4SLinus Torvalds /* 4181da177e4SLinus Torvalds * Note: this doesn't free the actual pages themselves. That 4191da177e4SLinus Torvalds * has been handled earlier when unmapping all the memory regions. 4201da177e4SLinus Torvalds */ 4219e1b32caSBenjamin Herrenschmidt static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd, 4229e1b32caSBenjamin Herrenschmidt unsigned long addr) 4231da177e4SLinus Torvalds { 4242f569afdSMartin Schwidefsky pgtable_t token = pmd_pgtable(*pmd); 4251da177e4SLinus Torvalds pmd_clear(pmd); 4269e1b32caSBenjamin Herrenschmidt pte_free_tlb(tlb, token, addr); 4271da177e4SLinus Torvalds tlb->mm->nr_ptes--; 4281da177e4SLinus Torvalds } 4291da177e4SLinus Torvalds 430e0da382cSHugh Dickins static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud, 431e0da382cSHugh Dickins unsigned long addr, unsigned long end, 432e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 4331da177e4SLinus Torvalds { 4341da177e4SLinus Torvalds pmd_t *pmd; 4351da177e4SLinus Torvalds unsigned long next; 436e0da382cSHugh Dickins unsigned long start; 4371da177e4SLinus Torvalds 438e0da382cSHugh Dickins start = addr; 4391da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 4401da177e4SLinus Torvalds do { 4411da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 4421da177e4SLinus Torvalds if (pmd_none_or_clear_bad(pmd)) 4431da177e4SLinus Torvalds continue; 4449e1b32caSBenjamin Herrenschmidt free_pte_range(tlb, pmd, addr); 4451da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 4461da177e4SLinus Torvalds 447e0da382cSHugh Dickins start &= PUD_MASK; 448e0da382cSHugh Dickins if (start < floor) 449e0da382cSHugh Dickins return; 450e0da382cSHugh Dickins if (ceiling) { 451e0da382cSHugh Dickins ceiling &= PUD_MASK; 452e0da382cSHugh Dickins if (!ceiling) 453e0da382cSHugh Dickins return; 4541da177e4SLinus Torvalds } 455e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 456e0da382cSHugh Dickins return; 457e0da382cSHugh Dickins 458e0da382cSHugh Dickins pmd = pmd_offset(pud, start); 459e0da382cSHugh Dickins pud_clear(pud); 4609e1b32caSBenjamin Herrenschmidt pmd_free_tlb(tlb, pmd, start); 4611da177e4SLinus Torvalds } 4621da177e4SLinus Torvalds 463e0da382cSHugh Dickins static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd, 464e0da382cSHugh Dickins unsigned long addr, unsigned long end, 465e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 4661da177e4SLinus Torvalds { 4671da177e4SLinus Torvalds pud_t *pud; 4681da177e4SLinus Torvalds unsigned long next; 469e0da382cSHugh Dickins unsigned long start; 4701da177e4SLinus Torvalds 471e0da382cSHugh Dickins start = addr; 4721da177e4SLinus Torvalds pud = pud_offset(pgd, addr); 4731da177e4SLinus Torvalds do { 4741da177e4SLinus Torvalds next = pud_addr_end(addr, end); 4751da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 4761da177e4SLinus Torvalds continue; 477e0da382cSHugh Dickins free_pmd_range(tlb, pud, addr, next, floor, ceiling); 4781da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 4791da177e4SLinus Torvalds 480e0da382cSHugh Dickins start &= PGDIR_MASK; 481e0da382cSHugh Dickins if (start < floor) 482e0da382cSHugh Dickins return; 483e0da382cSHugh Dickins if (ceiling) { 484e0da382cSHugh Dickins ceiling &= PGDIR_MASK; 485e0da382cSHugh Dickins if (!ceiling) 486e0da382cSHugh Dickins return; 4871da177e4SLinus Torvalds } 488e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 489e0da382cSHugh Dickins return; 490e0da382cSHugh Dickins 491e0da382cSHugh Dickins pud = pud_offset(pgd, start); 492e0da382cSHugh Dickins pgd_clear(pgd); 4939e1b32caSBenjamin Herrenschmidt pud_free_tlb(tlb, pud, start); 4941da177e4SLinus Torvalds } 4951da177e4SLinus Torvalds 4961da177e4SLinus Torvalds /* 497e0da382cSHugh Dickins * This function frees user-level page tables of a process. 498e0da382cSHugh Dickins * 4991da177e4SLinus Torvalds * Must be called with pagetable lock held. 5001da177e4SLinus Torvalds */ 50142b77728SJan Beulich void free_pgd_range(struct mmu_gather *tlb, 502e0da382cSHugh Dickins unsigned long addr, unsigned long end, 503e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 5041da177e4SLinus Torvalds { 5051da177e4SLinus Torvalds pgd_t *pgd; 5061da177e4SLinus Torvalds unsigned long next; 5071da177e4SLinus Torvalds 508e0da382cSHugh Dickins /* 509e0da382cSHugh Dickins * The next few lines have given us lots of grief... 510e0da382cSHugh Dickins * 511e0da382cSHugh Dickins * Why are we testing PMD* at this top level? Because often 512e0da382cSHugh Dickins * there will be no work to do at all, and we'd prefer not to 513e0da382cSHugh Dickins * go all the way down to the bottom just to discover that. 514e0da382cSHugh Dickins * 515e0da382cSHugh Dickins * Why all these "- 1"s? Because 0 represents both the bottom 516e0da382cSHugh Dickins * of the address space and the top of it (using -1 for the 517e0da382cSHugh Dickins * top wouldn't help much: the masks would do the wrong thing). 518e0da382cSHugh Dickins * The rule is that addr 0 and floor 0 refer to the bottom of 519e0da382cSHugh Dickins * the address space, but end 0 and ceiling 0 refer to the top 520e0da382cSHugh Dickins * Comparisons need to use "end - 1" and "ceiling - 1" (though 521e0da382cSHugh Dickins * that end 0 case should be mythical). 522e0da382cSHugh Dickins * 523e0da382cSHugh Dickins * Wherever addr is brought up or ceiling brought down, we must 524e0da382cSHugh Dickins * be careful to reject "the opposite 0" before it confuses the 525e0da382cSHugh Dickins * subsequent tests. But what about where end is brought down 526e0da382cSHugh Dickins * by PMD_SIZE below? no, end can't go down to 0 there. 527e0da382cSHugh Dickins * 528e0da382cSHugh Dickins * Whereas we round start (addr) and ceiling down, by different 529e0da382cSHugh Dickins * masks at different levels, in order to test whether a table 530e0da382cSHugh Dickins * now has no other vmas using it, so can be freed, we don't 531e0da382cSHugh Dickins * bother to round floor or end up - the tests don't need that. 532e0da382cSHugh Dickins */ 533e0da382cSHugh Dickins 534e0da382cSHugh Dickins addr &= PMD_MASK; 535e0da382cSHugh Dickins if (addr < floor) { 536e0da382cSHugh Dickins addr += PMD_SIZE; 537e0da382cSHugh Dickins if (!addr) 538e0da382cSHugh Dickins return; 539e0da382cSHugh Dickins } 540e0da382cSHugh Dickins if (ceiling) { 541e0da382cSHugh Dickins ceiling &= PMD_MASK; 542e0da382cSHugh Dickins if (!ceiling) 543e0da382cSHugh Dickins return; 544e0da382cSHugh Dickins } 545e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 546e0da382cSHugh Dickins end -= PMD_SIZE; 547e0da382cSHugh Dickins if (addr > end - 1) 548e0da382cSHugh Dickins return; 549e0da382cSHugh Dickins 55042b77728SJan Beulich pgd = pgd_offset(tlb->mm, addr); 5511da177e4SLinus Torvalds do { 5521da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 5531da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 5541da177e4SLinus Torvalds continue; 55542b77728SJan Beulich free_pud_range(tlb, pgd, addr, next, floor, ceiling); 5561da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 557e0da382cSHugh Dickins } 558e0da382cSHugh Dickins 55942b77728SJan Beulich void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma, 560e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 561e0da382cSHugh Dickins { 562e0da382cSHugh Dickins while (vma) { 563e0da382cSHugh Dickins struct vm_area_struct *next = vma->vm_next; 564e0da382cSHugh Dickins unsigned long addr = vma->vm_start; 565e0da382cSHugh Dickins 5668f4f8c16SHugh Dickins /* 56725d9e2d1Snpiggin@suse.de * Hide vma from rmap and truncate_pagecache before freeing 56825d9e2d1Snpiggin@suse.de * pgtables 5698f4f8c16SHugh Dickins */ 5705beb4930SRik van Riel unlink_anon_vmas(vma); 5718f4f8c16SHugh Dickins unlink_file_vma(vma); 5728f4f8c16SHugh Dickins 5739da61aefSDavid Gibson if (is_vm_hugetlb_page(vma)) { 5743bf5ee95SHugh Dickins hugetlb_free_pgd_range(tlb, addr, vma->vm_end, 5753bf5ee95SHugh Dickins floor, next? next->vm_start: ceiling); 5763bf5ee95SHugh Dickins } else { 5773bf5ee95SHugh Dickins /* 5783bf5ee95SHugh Dickins * Optimization: gather nearby vmas into one call down 5793bf5ee95SHugh Dickins */ 5803bf5ee95SHugh Dickins while (next && next->vm_start <= vma->vm_end + PMD_SIZE 5814866920bSDavid Gibson && !is_vm_hugetlb_page(next)) { 582e0da382cSHugh Dickins vma = next; 583e0da382cSHugh Dickins next = vma->vm_next; 5845beb4930SRik van Riel unlink_anon_vmas(vma); 5858f4f8c16SHugh Dickins unlink_file_vma(vma); 586e0da382cSHugh Dickins } 5873bf5ee95SHugh Dickins free_pgd_range(tlb, addr, vma->vm_end, 588e0da382cSHugh Dickins floor, next? next->vm_start: ceiling); 5893bf5ee95SHugh Dickins } 590e0da382cSHugh Dickins vma = next; 591e0da382cSHugh Dickins } 5921da177e4SLinus Torvalds } 5931da177e4SLinus Torvalds 5948ac1f832SAndrea Arcangeli int __pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma, 5958ac1f832SAndrea Arcangeli pmd_t *pmd, unsigned long address) 5961da177e4SLinus Torvalds { 5972f569afdSMartin Schwidefsky pgtable_t new = pte_alloc_one(mm, address); 5988ac1f832SAndrea Arcangeli int wait_split_huge_page; 5991da177e4SLinus Torvalds if (!new) 6001bb3630eSHugh Dickins return -ENOMEM; 6011bb3630eSHugh Dickins 602362a61adSNick Piggin /* 603362a61adSNick Piggin * Ensure all pte setup (eg. pte page lock and page clearing) are 604362a61adSNick Piggin * visible before the pte is made visible to other CPUs by being 605362a61adSNick Piggin * put into page tables. 606362a61adSNick Piggin * 607362a61adSNick Piggin * The other side of the story is the pointer chasing in the page 608362a61adSNick Piggin * table walking code (when walking the page table without locking; 609362a61adSNick Piggin * ie. most of the time). Fortunately, these data accesses consist 610362a61adSNick Piggin * of a chain of data-dependent loads, meaning most CPUs (alpha 611362a61adSNick Piggin * being the notable exception) will already guarantee loads are 612362a61adSNick Piggin * seen in-order. See the alpha page table accessors for the 613362a61adSNick Piggin * smp_read_barrier_depends() barriers in page table walking code. 614362a61adSNick Piggin */ 615362a61adSNick Piggin smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */ 616362a61adSNick Piggin 617c74df32cSHugh Dickins spin_lock(&mm->page_table_lock); 6188ac1f832SAndrea Arcangeli wait_split_huge_page = 0; 6198ac1f832SAndrea Arcangeli if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 6201da177e4SLinus Torvalds mm->nr_ptes++; 6211da177e4SLinus Torvalds pmd_populate(mm, pmd, new); 6222f569afdSMartin Schwidefsky new = NULL; 6238ac1f832SAndrea Arcangeli } else if (unlikely(pmd_trans_splitting(*pmd))) 6248ac1f832SAndrea Arcangeli wait_split_huge_page = 1; 625c74df32cSHugh Dickins spin_unlock(&mm->page_table_lock); 6262f569afdSMartin Schwidefsky if (new) 6272f569afdSMartin Schwidefsky pte_free(mm, new); 6288ac1f832SAndrea Arcangeli if (wait_split_huge_page) 6298ac1f832SAndrea Arcangeli wait_split_huge_page(vma->anon_vma, pmd); 6301bb3630eSHugh Dickins return 0; 6311da177e4SLinus Torvalds } 6321da177e4SLinus Torvalds 6331bb3630eSHugh Dickins int __pte_alloc_kernel(pmd_t *pmd, unsigned long address) 6341da177e4SLinus Torvalds { 6351bb3630eSHugh Dickins pte_t *new = pte_alloc_one_kernel(&init_mm, address); 6361da177e4SLinus Torvalds if (!new) 6371bb3630eSHugh Dickins return -ENOMEM; 6381da177e4SLinus Torvalds 639362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 640362a61adSNick Piggin 641872fec16SHugh Dickins spin_lock(&init_mm.page_table_lock); 6428ac1f832SAndrea Arcangeli if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 643872fec16SHugh Dickins pmd_populate_kernel(&init_mm, pmd, new); 6442f569afdSMartin Schwidefsky new = NULL; 6458ac1f832SAndrea Arcangeli } else 6468ac1f832SAndrea Arcangeli VM_BUG_ON(pmd_trans_splitting(*pmd)); 647872fec16SHugh Dickins spin_unlock(&init_mm.page_table_lock); 6482f569afdSMartin Schwidefsky if (new) 6492f569afdSMartin Schwidefsky pte_free_kernel(&init_mm, new); 6501bb3630eSHugh Dickins return 0; 6511da177e4SLinus Torvalds } 6521da177e4SLinus Torvalds 653d559db08SKAMEZAWA Hiroyuki static inline void init_rss_vec(int *rss) 654ae859762SHugh Dickins { 655d559db08SKAMEZAWA Hiroyuki memset(rss, 0, sizeof(int) * NR_MM_COUNTERS); 656d559db08SKAMEZAWA Hiroyuki } 657d559db08SKAMEZAWA Hiroyuki 658d559db08SKAMEZAWA Hiroyuki static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss) 659d559db08SKAMEZAWA Hiroyuki { 660d559db08SKAMEZAWA Hiroyuki int i; 661d559db08SKAMEZAWA Hiroyuki 66234e55232SKAMEZAWA Hiroyuki if (current->mm == mm) 66334e55232SKAMEZAWA Hiroyuki sync_mm_rss(current, mm); 664d559db08SKAMEZAWA Hiroyuki for (i = 0; i < NR_MM_COUNTERS; i++) 665d559db08SKAMEZAWA Hiroyuki if (rss[i]) 666d559db08SKAMEZAWA Hiroyuki add_mm_counter(mm, i, rss[i]); 667ae859762SHugh Dickins } 668ae859762SHugh Dickins 6691da177e4SLinus Torvalds /* 6706aab341eSLinus Torvalds * This function is called to print an error when a bad pte 6716aab341eSLinus Torvalds * is found. For example, we might have a PFN-mapped pte in 6726aab341eSLinus Torvalds * a region that doesn't allow it. 673b5810039SNick Piggin * 674b5810039SNick Piggin * The calling function must still handle the error. 675b5810039SNick Piggin */ 6763dc14741SHugh Dickins static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr, 6773dc14741SHugh Dickins pte_t pte, struct page *page) 678b5810039SNick Piggin { 6793dc14741SHugh Dickins pgd_t *pgd = pgd_offset(vma->vm_mm, addr); 6803dc14741SHugh Dickins pud_t *pud = pud_offset(pgd, addr); 6813dc14741SHugh Dickins pmd_t *pmd = pmd_offset(pud, addr); 6823dc14741SHugh Dickins struct address_space *mapping; 6833dc14741SHugh Dickins pgoff_t index; 684d936cf9bSHugh Dickins static unsigned long resume; 685d936cf9bSHugh Dickins static unsigned long nr_shown; 686d936cf9bSHugh Dickins static unsigned long nr_unshown; 687d936cf9bSHugh Dickins 688d936cf9bSHugh Dickins /* 689d936cf9bSHugh Dickins * Allow a burst of 60 reports, then keep quiet for that minute; 690d936cf9bSHugh Dickins * or allow a steady drip of one report per second. 691d936cf9bSHugh Dickins */ 692d936cf9bSHugh Dickins if (nr_shown == 60) { 693d936cf9bSHugh Dickins if (time_before(jiffies, resume)) { 694d936cf9bSHugh Dickins nr_unshown++; 695d936cf9bSHugh Dickins return; 696d936cf9bSHugh Dickins } 697d936cf9bSHugh Dickins if (nr_unshown) { 6981e9e6365SHugh Dickins printk(KERN_ALERT 6991e9e6365SHugh Dickins "BUG: Bad page map: %lu messages suppressed\n", 700d936cf9bSHugh Dickins nr_unshown); 701d936cf9bSHugh Dickins nr_unshown = 0; 702d936cf9bSHugh Dickins } 703d936cf9bSHugh Dickins nr_shown = 0; 704d936cf9bSHugh Dickins } 705d936cf9bSHugh Dickins if (nr_shown++ == 0) 706d936cf9bSHugh Dickins resume = jiffies + 60 * HZ; 7073dc14741SHugh Dickins 7083dc14741SHugh Dickins mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL; 7093dc14741SHugh Dickins index = linear_page_index(vma, addr); 7103dc14741SHugh Dickins 7111e9e6365SHugh Dickins printk(KERN_ALERT 7121e9e6365SHugh Dickins "BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n", 7133dc14741SHugh Dickins current->comm, 7143dc14741SHugh Dickins (long long)pte_val(pte), (long long)pmd_val(*pmd)); 715718a3821SWu Fengguang if (page) 716718a3821SWu Fengguang dump_page(page); 7171e9e6365SHugh Dickins printk(KERN_ALERT 7183dc14741SHugh Dickins "addr:%p vm_flags:%08lx anon_vma:%p mapping:%p index:%lx\n", 7193dc14741SHugh Dickins (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index); 7203dc14741SHugh Dickins /* 7213dc14741SHugh Dickins * Choose text because data symbols depend on CONFIG_KALLSYMS_ALL=y 7223dc14741SHugh Dickins */ 7233dc14741SHugh Dickins if (vma->vm_ops) 7241e9e6365SHugh Dickins print_symbol(KERN_ALERT "vma->vm_ops->fault: %s\n", 7253dc14741SHugh Dickins (unsigned long)vma->vm_ops->fault); 7263dc14741SHugh Dickins if (vma->vm_file && vma->vm_file->f_op) 7271e9e6365SHugh Dickins print_symbol(KERN_ALERT "vma->vm_file->f_op->mmap: %s\n", 7283dc14741SHugh Dickins (unsigned long)vma->vm_file->f_op->mmap); 729b5810039SNick Piggin dump_stack(); 7303dc14741SHugh Dickins add_taint(TAINT_BAD_PAGE); 731b5810039SNick Piggin } 732b5810039SNick Piggin 733ca16d140SKOSAKI Motohiro static inline int is_cow_mapping(vm_flags_t flags) 73467121172SLinus Torvalds { 73567121172SLinus Torvalds return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; 73667121172SLinus Torvalds } 73767121172SLinus Torvalds 73862eede62SHugh Dickins #ifndef is_zero_pfn 73962eede62SHugh Dickins static inline int is_zero_pfn(unsigned long pfn) 74062eede62SHugh Dickins { 74162eede62SHugh Dickins return pfn == zero_pfn; 74262eede62SHugh Dickins } 74362eede62SHugh Dickins #endif 74462eede62SHugh Dickins 74562eede62SHugh Dickins #ifndef my_zero_pfn 74662eede62SHugh Dickins static inline unsigned long my_zero_pfn(unsigned long addr) 74762eede62SHugh Dickins { 74862eede62SHugh Dickins return zero_pfn; 74962eede62SHugh Dickins } 75062eede62SHugh Dickins #endif 75162eede62SHugh Dickins 752b5810039SNick Piggin /* 7537e675137SNick Piggin * vm_normal_page -- This function gets the "struct page" associated with a pte. 7546aab341eSLinus Torvalds * 7557e675137SNick Piggin * "Special" mappings do not wish to be associated with a "struct page" (either 7567e675137SNick Piggin * it doesn't exist, or it exists but they don't want to touch it). In this 7577e675137SNick Piggin * case, NULL is returned here. "Normal" mappings do have a struct page. 758b379d790SJared Hulbert * 7597e675137SNick Piggin * There are 2 broad cases. Firstly, an architecture may define a pte_special() 7607e675137SNick Piggin * pte bit, in which case this function is trivial. Secondly, an architecture 7617e675137SNick Piggin * may not have a spare pte bit, which requires a more complicated scheme, 7627e675137SNick Piggin * described below. 7637e675137SNick Piggin * 7647e675137SNick Piggin * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a 7657e675137SNick Piggin * special mapping (even if there are underlying and valid "struct pages"). 7667e675137SNick Piggin * COWed pages of a VM_PFNMAP are always normal. 7676aab341eSLinus Torvalds * 768b379d790SJared Hulbert * The way we recognize COWed pages within VM_PFNMAP mappings is through the 769b379d790SJared Hulbert * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit 7707e675137SNick Piggin * set, and the vm_pgoff will point to the first PFN mapped: thus every special 7717e675137SNick Piggin * mapping will always honor the rule 7726aab341eSLinus Torvalds * 7736aab341eSLinus Torvalds * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT) 7746aab341eSLinus Torvalds * 7757e675137SNick Piggin * And for normal mappings this is false. 776b379d790SJared Hulbert * 7777e675137SNick Piggin * This restricts such mappings to be a linear translation from virtual address 7787e675137SNick Piggin * to pfn. To get around this restriction, we allow arbitrary mappings so long 7797e675137SNick Piggin * as the vma is not a COW mapping; in that case, we know that all ptes are 7807e675137SNick Piggin * special (because none can have been COWed). 781b379d790SJared Hulbert * 782b379d790SJared Hulbert * 7837e675137SNick Piggin * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP. 7847e675137SNick Piggin * 785b379d790SJared Hulbert * VM_MIXEDMAP mappings can likewise contain memory with or without "struct 786b379d790SJared Hulbert * page" backing, however the difference is that _all_ pages with a struct 787b379d790SJared Hulbert * page (that is, those where pfn_valid is true) are refcounted and considered 788b379d790SJared Hulbert * normal pages by the VM. The disadvantage is that pages are refcounted 789b379d790SJared Hulbert * (which can be slower and simply not an option for some PFNMAP users). The 790b379d790SJared Hulbert * advantage is that we don't have to follow the strict linearity rule of 791b379d790SJared Hulbert * PFNMAP mappings in order to support COWable mappings. 792b379d790SJared Hulbert * 793ee498ed7SHugh Dickins */ 7947e675137SNick Piggin #ifdef __HAVE_ARCH_PTE_SPECIAL 7957e675137SNick Piggin # define HAVE_PTE_SPECIAL 1 7967e675137SNick Piggin #else 7977e675137SNick Piggin # define HAVE_PTE_SPECIAL 0 7987e675137SNick Piggin #endif 7997e675137SNick Piggin struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, 8007e675137SNick Piggin pte_t pte) 801ee498ed7SHugh Dickins { 80222b31eecSHugh Dickins unsigned long pfn = pte_pfn(pte); 8037e675137SNick Piggin 8047e675137SNick Piggin if (HAVE_PTE_SPECIAL) { 80522b31eecSHugh Dickins if (likely(!pte_special(pte))) 80622b31eecSHugh Dickins goto check_pfn; 807a13ea5b7SHugh Dickins if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)) 808a13ea5b7SHugh Dickins return NULL; 80962eede62SHugh Dickins if (!is_zero_pfn(pfn)) 81022b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 8117e675137SNick Piggin return NULL; 8127e675137SNick Piggin } 8137e675137SNick Piggin 8147e675137SNick Piggin /* !HAVE_PTE_SPECIAL case follows: */ 8157e675137SNick Piggin 816b379d790SJared Hulbert if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { 817b379d790SJared Hulbert if (vma->vm_flags & VM_MIXEDMAP) { 818b379d790SJared Hulbert if (!pfn_valid(pfn)) 819b379d790SJared Hulbert return NULL; 820b379d790SJared Hulbert goto out; 821b379d790SJared Hulbert } else { 8227e675137SNick Piggin unsigned long off; 8237e675137SNick Piggin off = (addr - vma->vm_start) >> PAGE_SHIFT; 8246aab341eSLinus Torvalds if (pfn == vma->vm_pgoff + off) 8256aab341eSLinus Torvalds return NULL; 82667121172SLinus Torvalds if (!is_cow_mapping(vma->vm_flags)) 827fb155c16SLinus Torvalds return NULL; 8286aab341eSLinus Torvalds } 829b379d790SJared Hulbert } 8306aab341eSLinus Torvalds 83162eede62SHugh Dickins if (is_zero_pfn(pfn)) 83262eede62SHugh Dickins return NULL; 83322b31eecSHugh Dickins check_pfn: 83422b31eecSHugh Dickins if (unlikely(pfn > highest_memmap_pfn)) { 83522b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 83622b31eecSHugh Dickins return NULL; 83722b31eecSHugh Dickins } 8386aab341eSLinus Torvalds 8396aab341eSLinus Torvalds /* 8407e675137SNick Piggin * NOTE! We still have PageReserved() pages in the page tables. 8417e675137SNick Piggin * eg. VDSO mappings can cause them to exist. 8426aab341eSLinus Torvalds */ 843b379d790SJared Hulbert out: 8446aab341eSLinus Torvalds return pfn_to_page(pfn); 845ee498ed7SHugh Dickins } 846ee498ed7SHugh Dickins 847ee498ed7SHugh Dickins /* 8481da177e4SLinus Torvalds * copy one vm_area from one task to the other. Assumes the page tables 8491da177e4SLinus Torvalds * already present in the new task to be cleared in the whole range 8501da177e4SLinus Torvalds * covered by this vma. 8511da177e4SLinus Torvalds */ 8521da177e4SLinus Torvalds 853570a335bSHugh Dickins static inline unsigned long 8541da177e4SLinus Torvalds copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, 855b5810039SNick Piggin pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma, 8568c103762SHugh Dickins unsigned long addr, int *rss) 8571da177e4SLinus Torvalds { 858b5810039SNick Piggin unsigned long vm_flags = vma->vm_flags; 8591da177e4SLinus Torvalds pte_t pte = *src_pte; 8601da177e4SLinus Torvalds struct page *page; 8611da177e4SLinus Torvalds 8621da177e4SLinus Torvalds /* pte contains position in swap or file, so copy. */ 8631da177e4SLinus Torvalds if (unlikely(!pte_present(pte))) { 8641da177e4SLinus Torvalds if (!pte_file(pte)) { 8650697212aSChristoph Lameter swp_entry_t entry = pte_to_swp_entry(pte); 8660697212aSChristoph Lameter 867570a335bSHugh Dickins if (swap_duplicate(entry) < 0) 868570a335bSHugh Dickins return entry.val; 869570a335bSHugh Dickins 8701da177e4SLinus Torvalds /* make sure dst_mm is on swapoff's mmlist. */ 8711da177e4SLinus Torvalds if (unlikely(list_empty(&dst_mm->mmlist))) { 8721da177e4SLinus Torvalds spin_lock(&mmlist_lock); 873f412ac08SHugh Dickins if (list_empty(&dst_mm->mmlist)) 874f412ac08SHugh Dickins list_add(&dst_mm->mmlist, 875f412ac08SHugh Dickins &src_mm->mmlist); 8761da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 8771da177e4SLinus Torvalds } 878b084d435SKAMEZAWA Hiroyuki if (likely(!non_swap_entry(entry))) 879b084d435SKAMEZAWA Hiroyuki rss[MM_SWAPENTS]++; 880b084d435SKAMEZAWA Hiroyuki else if (is_write_migration_entry(entry) && 8810697212aSChristoph Lameter is_cow_mapping(vm_flags)) { 8820697212aSChristoph Lameter /* 8830697212aSChristoph Lameter * COW mappings require pages in both parent 8840697212aSChristoph Lameter * and child to be set to read. 8850697212aSChristoph Lameter */ 8860697212aSChristoph Lameter make_migration_entry_read(&entry); 8870697212aSChristoph Lameter pte = swp_entry_to_pte(entry); 8880697212aSChristoph Lameter set_pte_at(src_mm, addr, src_pte, pte); 8890697212aSChristoph Lameter } 8901da177e4SLinus Torvalds } 891ae859762SHugh Dickins goto out_set_pte; 8921da177e4SLinus Torvalds } 8931da177e4SLinus Torvalds 8941da177e4SLinus Torvalds /* 8951da177e4SLinus Torvalds * If it's a COW mapping, write protect it both 8961da177e4SLinus Torvalds * in the parent and the child 8971da177e4SLinus Torvalds */ 89867121172SLinus Torvalds if (is_cow_mapping(vm_flags)) { 8991da177e4SLinus Torvalds ptep_set_wrprotect(src_mm, addr, src_pte); 9003dc90795SZachary Amsden pte = pte_wrprotect(pte); 9011da177e4SLinus Torvalds } 9021da177e4SLinus Torvalds 9031da177e4SLinus Torvalds /* 9041da177e4SLinus Torvalds * If it's a shared mapping, mark it clean in 9051da177e4SLinus Torvalds * the child 9061da177e4SLinus Torvalds */ 9071da177e4SLinus Torvalds if (vm_flags & VM_SHARED) 9081da177e4SLinus Torvalds pte = pte_mkclean(pte); 9091da177e4SLinus Torvalds pte = pte_mkold(pte); 9106aab341eSLinus Torvalds 9116aab341eSLinus Torvalds page = vm_normal_page(vma, addr, pte); 9126aab341eSLinus Torvalds if (page) { 9131da177e4SLinus Torvalds get_page(page); 91421333b2bSHugh Dickins page_dup_rmap(page); 915d559db08SKAMEZAWA Hiroyuki if (PageAnon(page)) 916d559db08SKAMEZAWA Hiroyuki rss[MM_ANONPAGES]++; 917d559db08SKAMEZAWA Hiroyuki else 918d559db08SKAMEZAWA Hiroyuki rss[MM_FILEPAGES]++; 9196aab341eSLinus Torvalds } 920ae859762SHugh Dickins 921ae859762SHugh Dickins out_set_pte: 922ae859762SHugh Dickins set_pte_at(dst_mm, addr, dst_pte, pte); 923570a335bSHugh Dickins return 0; 9241da177e4SLinus Torvalds } 9251da177e4SLinus Torvalds 92671e3aac0SAndrea Arcangeli int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 9271da177e4SLinus Torvalds pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma, 9281da177e4SLinus Torvalds unsigned long addr, unsigned long end) 9291da177e4SLinus Torvalds { 930c36987e2SDaisuke Nishimura pte_t *orig_src_pte, *orig_dst_pte; 9311da177e4SLinus Torvalds pte_t *src_pte, *dst_pte; 932c74df32cSHugh Dickins spinlock_t *src_ptl, *dst_ptl; 933e040f218SHugh Dickins int progress = 0; 934d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 935570a335bSHugh Dickins swp_entry_t entry = (swp_entry_t){0}; 9361da177e4SLinus Torvalds 9371da177e4SLinus Torvalds again: 938d559db08SKAMEZAWA Hiroyuki init_rss_vec(rss); 939d559db08SKAMEZAWA Hiroyuki 940c74df32cSHugh Dickins dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl); 9411da177e4SLinus Torvalds if (!dst_pte) 9421da177e4SLinus Torvalds return -ENOMEM; 943ece0e2b6SPeter Zijlstra src_pte = pte_offset_map(src_pmd, addr); 9444c21e2f2SHugh Dickins src_ptl = pte_lockptr(src_mm, src_pmd); 945f20dc5f7SIngo Molnar spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 946c36987e2SDaisuke Nishimura orig_src_pte = src_pte; 947c36987e2SDaisuke Nishimura orig_dst_pte = dst_pte; 9486606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 9491da177e4SLinus Torvalds 9501da177e4SLinus Torvalds do { 9511da177e4SLinus Torvalds /* 9521da177e4SLinus Torvalds * We are holding two locks at this point - either of them 9531da177e4SLinus Torvalds * could generate latencies in another task on another CPU. 9541da177e4SLinus Torvalds */ 955e040f218SHugh Dickins if (progress >= 32) { 956e040f218SHugh Dickins progress = 0; 957e040f218SHugh Dickins if (need_resched() || 95895c354feSNick Piggin spin_needbreak(src_ptl) || spin_needbreak(dst_ptl)) 9591da177e4SLinus Torvalds break; 960e040f218SHugh Dickins } 9611da177e4SLinus Torvalds if (pte_none(*src_pte)) { 9621da177e4SLinus Torvalds progress++; 9631da177e4SLinus Torvalds continue; 9641da177e4SLinus Torvalds } 965570a335bSHugh Dickins entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte, 966570a335bSHugh Dickins vma, addr, rss); 967570a335bSHugh Dickins if (entry.val) 968570a335bSHugh Dickins break; 9691da177e4SLinus Torvalds progress += 8; 9701da177e4SLinus Torvalds } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end); 9711da177e4SLinus Torvalds 9726606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 973c74df32cSHugh Dickins spin_unlock(src_ptl); 974ece0e2b6SPeter Zijlstra pte_unmap(orig_src_pte); 975d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(dst_mm, rss); 976c36987e2SDaisuke Nishimura pte_unmap_unlock(orig_dst_pte, dst_ptl); 977c74df32cSHugh Dickins cond_resched(); 978570a335bSHugh Dickins 979570a335bSHugh Dickins if (entry.val) { 980570a335bSHugh Dickins if (add_swap_count_continuation(entry, GFP_KERNEL) < 0) 981570a335bSHugh Dickins return -ENOMEM; 982570a335bSHugh Dickins progress = 0; 983570a335bSHugh Dickins } 9841da177e4SLinus Torvalds if (addr != end) 9851da177e4SLinus Torvalds goto again; 9861da177e4SLinus Torvalds return 0; 9871da177e4SLinus Torvalds } 9881da177e4SLinus Torvalds 9891da177e4SLinus Torvalds static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 9901da177e4SLinus Torvalds pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma, 9911da177e4SLinus Torvalds unsigned long addr, unsigned long end) 9921da177e4SLinus Torvalds { 9931da177e4SLinus Torvalds pmd_t *src_pmd, *dst_pmd; 9941da177e4SLinus Torvalds unsigned long next; 9951da177e4SLinus Torvalds 9961da177e4SLinus Torvalds dst_pmd = pmd_alloc(dst_mm, dst_pud, addr); 9971da177e4SLinus Torvalds if (!dst_pmd) 9981da177e4SLinus Torvalds return -ENOMEM; 9991da177e4SLinus Torvalds src_pmd = pmd_offset(src_pud, addr); 10001da177e4SLinus Torvalds do { 10011da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 100271e3aac0SAndrea Arcangeli if (pmd_trans_huge(*src_pmd)) { 100371e3aac0SAndrea Arcangeli int err; 100414d1a55cSAndrea Arcangeli VM_BUG_ON(next-addr != HPAGE_PMD_SIZE); 100571e3aac0SAndrea Arcangeli err = copy_huge_pmd(dst_mm, src_mm, 100671e3aac0SAndrea Arcangeli dst_pmd, src_pmd, addr, vma); 100771e3aac0SAndrea Arcangeli if (err == -ENOMEM) 100871e3aac0SAndrea Arcangeli return -ENOMEM; 100971e3aac0SAndrea Arcangeli if (!err) 101071e3aac0SAndrea Arcangeli continue; 101171e3aac0SAndrea Arcangeli /* fall through */ 101271e3aac0SAndrea Arcangeli } 10131da177e4SLinus Torvalds if (pmd_none_or_clear_bad(src_pmd)) 10141da177e4SLinus Torvalds continue; 10151da177e4SLinus Torvalds if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd, 10161da177e4SLinus Torvalds vma, addr, next)) 10171da177e4SLinus Torvalds return -ENOMEM; 10181da177e4SLinus Torvalds } while (dst_pmd++, src_pmd++, addr = next, addr != end); 10191da177e4SLinus Torvalds return 0; 10201da177e4SLinus Torvalds } 10211da177e4SLinus Torvalds 10221da177e4SLinus Torvalds static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 10231da177e4SLinus Torvalds pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma, 10241da177e4SLinus Torvalds unsigned long addr, unsigned long end) 10251da177e4SLinus Torvalds { 10261da177e4SLinus Torvalds pud_t *src_pud, *dst_pud; 10271da177e4SLinus Torvalds unsigned long next; 10281da177e4SLinus Torvalds 10291da177e4SLinus Torvalds dst_pud = pud_alloc(dst_mm, dst_pgd, addr); 10301da177e4SLinus Torvalds if (!dst_pud) 10311da177e4SLinus Torvalds return -ENOMEM; 10321da177e4SLinus Torvalds src_pud = pud_offset(src_pgd, addr); 10331da177e4SLinus Torvalds do { 10341da177e4SLinus Torvalds next = pud_addr_end(addr, end); 10351da177e4SLinus Torvalds if (pud_none_or_clear_bad(src_pud)) 10361da177e4SLinus Torvalds continue; 10371da177e4SLinus Torvalds if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud, 10381da177e4SLinus Torvalds vma, addr, next)) 10391da177e4SLinus Torvalds return -ENOMEM; 10401da177e4SLinus Torvalds } while (dst_pud++, src_pud++, addr = next, addr != end); 10411da177e4SLinus Torvalds return 0; 10421da177e4SLinus Torvalds } 10431da177e4SLinus Torvalds 10441da177e4SLinus Torvalds int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 10451da177e4SLinus Torvalds struct vm_area_struct *vma) 10461da177e4SLinus Torvalds { 10471da177e4SLinus Torvalds pgd_t *src_pgd, *dst_pgd; 10481da177e4SLinus Torvalds unsigned long next; 10491da177e4SLinus Torvalds unsigned long addr = vma->vm_start; 10501da177e4SLinus Torvalds unsigned long end = vma->vm_end; 1051cddb8a5cSAndrea Arcangeli int ret; 10521da177e4SLinus Torvalds 1053d992895bSNick Piggin /* 1054d992895bSNick Piggin * Don't copy ptes where a page fault will fill them correctly. 1055d992895bSNick Piggin * Fork becomes much lighter when there are big shared or private 1056d992895bSNick Piggin * readonly mappings. The tradeoff is that copy_page_range is more 1057d992895bSNick Piggin * efficient than faulting. 1058d992895bSNick Piggin */ 10594d7672b4SLinus Torvalds if (!(vma->vm_flags & (VM_HUGETLB|VM_NONLINEAR|VM_PFNMAP|VM_INSERTPAGE))) { 1060d992895bSNick Piggin if (!vma->anon_vma) 1061d992895bSNick Piggin return 0; 1062d992895bSNick Piggin } 1063d992895bSNick Piggin 10641da177e4SLinus Torvalds if (is_vm_hugetlb_page(vma)) 10651da177e4SLinus Torvalds return copy_hugetlb_page_range(dst_mm, src_mm, vma); 10661da177e4SLinus Torvalds 106734801ba9Svenkatesh.pallipadi@intel.com if (unlikely(is_pfn_mapping(vma))) { 10682ab64037Svenkatesh.pallipadi@intel.com /* 10692ab64037Svenkatesh.pallipadi@intel.com * We do not free on error cases below as remove_vma 10702ab64037Svenkatesh.pallipadi@intel.com * gets called on error from higher level routine 10712ab64037Svenkatesh.pallipadi@intel.com */ 10722ab64037Svenkatesh.pallipadi@intel.com ret = track_pfn_vma_copy(vma); 10732ab64037Svenkatesh.pallipadi@intel.com if (ret) 10742ab64037Svenkatesh.pallipadi@intel.com return ret; 10752ab64037Svenkatesh.pallipadi@intel.com } 10762ab64037Svenkatesh.pallipadi@intel.com 1077cddb8a5cSAndrea Arcangeli /* 1078cddb8a5cSAndrea Arcangeli * We need to invalidate the secondary MMU mappings only when 1079cddb8a5cSAndrea Arcangeli * there could be a permission downgrade on the ptes of the 1080cddb8a5cSAndrea Arcangeli * parent mm. And a permission downgrade will only happen if 1081cddb8a5cSAndrea Arcangeli * is_cow_mapping() returns true. 1082cddb8a5cSAndrea Arcangeli */ 1083cddb8a5cSAndrea Arcangeli if (is_cow_mapping(vma->vm_flags)) 1084cddb8a5cSAndrea Arcangeli mmu_notifier_invalidate_range_start(src_mm, addr, end); 1085cddb8a5cSAndrea Arcangeli 1086cddb8a5cSAndrea Arcangeli ret = 0; 10871da177e4SLinus Torvalds dst_pgd = pgd_offset(dst_mm, addr); 10881da177e4SLinus Torvalds src_pgd = pgd_offset(src_mm, addr); 10891da177e4SLinus Torvalds do { 10901da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 10911da177e4SLinus Torvalds if (pgd_none_or_clear_bad(src_pgd)) 10921da177e4SLinus Torvalds continue; 1093cddb8a5cSAndrea Arcangeli if (unlikely(copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd, 1094cddb8a5cSAndrea Arcangeli vma, addr, next))) { 1095cddb8a5cSAndrea Arcangeli ret = -ENOMEM; 1096cddb8a5cSAndrea Arcangeli break; 1097cddb8a5cSAndrea Arcangeli } 10981da177e4SLinus Torvalds } while (dst_pgd++, src_pgd++, addr = next, addr != end); 1099cddb8a5cSAndrea Arcangeli 1100cddb8a5cSAndrea Arcangeli if (is_cow_mapping(vma->vm_flags)) 1101cddb8a5cSAndrea Arcangeli mmu_notifier_invalidate_range_end(src_mm, 1102cddb8a5cSAndrea Arcangeli vma->vm_start, end); 1103cddb8a5cSAndrea Arcangeli return ret; 11041da177e4SLinus Torvalds } 11051da177e4SLinus Torvalds 110651c6f666SRobin Holt static unsigned long zap_pte_range(struct mmu_gather *tlb, 1107b5810039SNick Piggin struct vm_area_struct *vma, pmd_t *pmd, 11081da177e4SLinus Torvalds unsigned long addr, unsigned long end, 110997a89413SPeter Zijlstra struct zap_details *details) 11101da177e4SLinus Torvalds { 1111b5810039SNick Piggin struct mm_struct *mm = tlb->mm; 1112d16dfc55SPeter Zijlstra int force_flush = 0; 1113d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 111497a89413SPeter Zijlstra spinlock_t *ptl; 1115*5f1a1907SSteven Rostedt pte_t *start_pte; 111697a89413SPeter Zijlstra pte_t *pte; 1117d559db08SKAMEZAWA Hiroyuki 1118d16dfc55SPeter Zijlstra again: 1119e303297eSPeter Zijlstra init_rss_vec(rss); 1120*5f1a1907SSteven Rostedt start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl); 1121*5f1a1907SSteven Rostedt pte = start_pte; 11226606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 11231da177e4SLinus Torvalds do { 11241da177e4SLinus Torvalds pte_t ptent = *pte; 112551c6f666SRobin Holt if (pte_none(ptent)) { 11261da177e4SLinus Torvalds continue; 112751c6f666SRobin Holt } 112851c6f666SRobin Holt 11296f5e6b9eSHugh Dickins if (pte_present(ptent)) { 11306f5e6b9eSHugh Dickins struct page *page; 11316f5e6b9eSHugh Dickins 11326aab341eSLinus Torvalds page = vm_normal_page(vma, addr, ptent); 11331da177e4SLinus Torvalds if (unlikely(details) && page) { 11341da177e4SLinus Torvalds /* 11351da177e4SLinus Torvalds * unmap_shared_mapping_pages() wants to 11361da177e4SLinus Torvalds * invalidate cache without truncating: 11371da177e4SLinus Torvalds * unmap shared but keep private pages. 11381da177e4SLinus Torvalds */ 11391da177e4SLinus Torvalds if (details->check_mapping && 11401da177e4SLinus Torvalds details->check_mapping != page->mapping) 11411da177e4SLinus Torvalds continue; 11421da177e4SLinus Torvalds /* 11431da177e4SLinus Torvalds * Each page->index must be checked when 11441da177e4SLinus Torvalds * invalidating or truncating nonlinear. 11451da177e4SLinus Torvalds */ 11461da177e4SLinus Torvalds if (details->nonlinear_vma && 11471da177e4SLinus Torvalds (page->index < details->first_index || 11481da177e4SLinus Torvalds page->index > details->last_index)) 11491da177e4SLinus Torvalds continue; 11501da177e4SLinus Torvalds } 1151b5810039SNick Piggin ptent = ptep_get_and_clear_full(mm, addr, pte, 1152a600388dSZachary Amsden tlb->fullmm); 11531da177e4SLinus Torvalds tlb_remove_tlb_entry(tlb, pte, addr); 11541da177e4SLinus Torvalds if (unlikely(!page)) 11551da177e4SLinus Torvalds continue; 11561da177e4SLinus Torvalds if (unlikely(details) && details->nonlinear_vma 11571da177e4SLinus Torvalds && linear_page_index(details->nonlinear_vma, 11581da177e4SLinus Torvalds addr) != page->index) 1159b5810039SNick Piggin set_pte_at(mm, addr, pte, 11601da177e4SLinus Torvalds pgoff_to_pte(page->index)); 11611da177e4SLinus Torvalds if (PageAnon(page)) 1162d559db08SKAMEZAWA Hiroyuki rss[MM_ANONPAGES]--; 11636237bcd9SHugh Dickins else { 11646237bcd9SHugh Dickins if (pte_dirty(ptent)) 11656237bcd9SHugh Dickins set_page_dirty(page); 11664917e5d0SJohannes Weiner if (pte_young(ptent) && 11674917e5d0SJohannes Weiner likely(!VM_SequentialReadHint(vma))) 1168bf3f3bc5SNick Piggin mark_page_accessed(page); 1169d559db08SKAMEZAWA Hiroyuki rss[MM_FILEPAGES]--; 11706237bcd9SHugh Dickins } 1171edc315fdSHugh Dickins page_remove_rmap(page); 11723dc14741SHugh Dickins if (unlikely(page_mapcount(page) < 0)) 11733dc14741SHugh Dickins print_bad_pte(vma, addr, ptent, page); 1174d16dfc55SPeter Zijlstra force_flush = !__tlb_remove_page(tlb, page); 1175d16dfc55SPeter Zijlstra if (force_flush) 1176d16dfc55SPeter Zijlstra break; 11771da177e4SLinus Torvalds continue; 11781da177e4SLinus Torvalds } 11791da177e4SLinus Torvalds /* 11801da177e4SLinus Torvalds * If details->check_mapping, we leave swap entries; 11811da177e4SLinus Torvalds * if details->nonlinear_vma, we leave file entries. 11821da177e4SLinus Torvalds */ 11831da177e4SLinus Torvalds if (unlikely(details)) 11841da177e4SLinus Torvalds continue; 11852509ef26SHugh Dickins if (pte_file(ptent)) { 11862509ef26SHugh Dickins if (unlikely(!(vma->vm_flags & VM_NONLINEAR))) 11872509ef26SHugh Dickins print_bad_pte(vma, addr, ptent, NULL); 1188b084d435SKAMEZAWA Hiroyuki } else { 1189b084d435SKAMEZAWA Hiroyuki swp_entry_t entry = pte_to_swp_entry(ptent); 1190b084d435SKAMEZAWA Hiroyuki 1191b084d435SKAMEZAWA Hiroyuki if (!non_swap_entry(entry)) 1192b084d435SKAMEZAWA Hiroyuki rss[MM_SWAPENTS]--; 1193b084d435SKAMEZAWA Hiroyuki if (unlikely(!free_swap_and_cache(entry))) 11942509ef26SHugh Dickins print_bad_pte(vma, addr, ptent, NULL); 1195b084d435SKAMEZAWA Hiroyuki } 11969888a1caSZachary Amsden pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); 119797a89413SPeter Zijlstra } while (pte++, addr += PAGE_SIZE, addr != end); 1198ae859762SHugh Dickins 1199d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(mm, rss); 12006606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 1201*5f1a1907SSteven Rostedt pte_unmap_unlock(start_pte, ptl); 120251c6f666SRobin Holt 1203d16dfc55SPeter Zijlstra /* 1204d16dfc55SPeter Zijlstra * mmu_gather ran out of room to batch pages, we break out of 1205d16dfc55SPeter Zijlstra * the PTE lock to avoid doing the potential expensive TLB invalidate 1206d16dfc55SPeter Zijlstra * and page-free while holding it. 1207d16dfc55SPeter Zijlstra */ 1208d16dfc55SPeter Zijlstra if (force_flush) { 1209d16dfc55SPeter Zijlstra force_flush = 0; 1210d16dfc55SPeter Zijlstra tlb_flush_mmu(tlb); 1211d16dfc55SPeter Zijlstra if (addr != end) 1212d16dfc55SPeter Zijlstra goto again; 1213d16dfc55SPeter Zijlstra } 1214d16dfc55SPeter Zijlstra 121551c6f666SRobin Holt return addr; 12161da177e4SLinus Torvalds } 12171da177e4SLinus Torvalds 121851c6f666SRobin Holt static inline unsigned long zap_pmd_range(struct mmu_gather *tlb, 1219b5810039SNick Piggin struct vm_area_struct *vma, pud_t *pud, 12201da177e4SLinus Torvalds unsigned long addr, unsigned long end, 122197a89413SPeter Zijlstra struct zap_details *details) 12221da177e4SLinus Torvalds { 12231da177e4SLinus Torvalds pmd_t *pmd; 12241da177e4SLinus Torvalds unsigned long next; 12251da177e4SLinus Torvalds 12261da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 12271da177e4SLinus Torvalds do { 12281da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 122971e3aac0SAndrea Arcangeli if (pmd_trans_huge(*pmd)) { 123014d1a55cSAndrea Arcangeli if (next-addr != HPAGE_PMD_SIZE) { 123114d1a55cSAndrea Arcangeli VM_BUG_ON(!rwsem_is_locked(&tlb->mm->mmap_sem)); 123271e3aac0SAndrea Arcangeli split_huge_page_pmd(vma->vm_mm, pmd); 123397a89413SPeter Zijlstra } else if (zap_huge_pmd(tlb, vma, pmd)) 123471e3aac0SAndrea Arcangeli continue; 123571e3aac0SAndrea Arcangeli /* fall through */ 123671e3aac0SAndrea Arcangeli } 123797a89413SPeter Zijlstra if (pmd_none_or_clear_bad(pmd)) 12381da177e4SLinus Torvalds continue; 123997a89413SPeter Zijlstra next = zap_pte_range(tlb, vma, pmd, addr, next, details); 124097a89413SPeter Zijlstra cond_resched(); 124197a89413SPeter Zijlstra } while (pmd++, addr = next, addr != end); 124251c6f666SRobin Holt 124351c6f666SRobin Holt return addr; 12441da177e4SLinus Torvalds } 12451da177e4SLinus Torvalds 124651c6f666SRobin Holt static inline unsigned long zap_pud_range(struct mmu_gather *tlb, 1247b5810039SNick Piggin struct vm_area_struct *vma, pgd_t *pgd, 12481da177e4SLinus Torvalds unsigned long addr, unsigned long end, 124997a89413SPeter Zijlstra struct zap_details *details) 12501da177e4SLinus Torvalds { 12511da177e4SLinus Torvalds pud_t *pud; 12521da177e4SLinus Torvalds unsigned long next; 12531da177e4SLinus Torvalds 12541da177e4SLinus Torvalds pud = pud_offset(pgd, addr); 12551da177e4SLinus Torvalds do { 12561da177e4SLinus Torvalds next = pud_addr_end(addr, end); 125797a89413SPeter Zijlstra if (pud_none_or_clear_bad(pud)) 12581da177e4SLinus Torvalds continue; 125997a89413SPeter Zijlstra next = zap_pmd_range(tlb, vma, pud, addr, next, details); 126097a89413SPeter Zijlstra } while (pud++, addr = next, addr != end); 126151c6f666SRobin Holt 126251c6f666SRobin Holt return addr; 12631da177e4SLinus Torvalds } 12641da177e4SLinus Torvalds 126551c6f666SRobin Holt static unsigned long unmap_page_range(struct mmu_gather *tlb, 126651c6f666SRobin Holt struct vm_area_struct *vma, 12671da177e4SLinus Torvalds unsigned long addr, unsigned long end, 126897a89413SPeter Zijlstra struct zap_details *details) 12691da177e4SLinus Torvalds { 12701da177e4SLinus Torvalds pgd_t *pgd; 12711da177e4SLinus Torvalds unsigned long next; 12721da177e4SLinus Torvalds 12731da177e4SLinus Torvalds if (details && !details->check_mapping && !details->nonlinear_vma) 12741da177e4SLinus Torvalds details = NULL; 12751da177e4SLinus Torvalds 12761da177e4SLinus Torvalds BUG_ON(addr >= end); 1277569b846dSKAMEZAWA Hiroyuki mem_cgroup_uncharge_start(); 12781da177e4SLinus Torvalds tlb_start_vma(tlb, vma); 12791da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 12801da177e4SLinus Torvalds do { 12811da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 128297a89413SPeter Zijlstra if (pgd_none_or_clear_bad(pgd)) 12831da177e4SLinus Torvalds continue; 128497a89413SPeter Zijlstra next = zap_pud_range(tlb, vma, pgd, addr, next, details); 128597a89413SPeter Zijlstra } while (pgd++, addr = next, addr != end); 12861da177e4SLinus Torvalds tlb_end_vma(tlb, vma); 1287569b846dSKAMEZAWA Hiroyuki mem_cgroup_uncharge_end(); 128851c6f666SRobin Holt 128951c6f666SRobin Holt return addr; 12901da177e4SLinus Torvalds } 12911da177e4SLinus Torvalds 12921da177e4SLinus Torvalds #ifdef CONFIG_PREEMPT 12931da177e4SLinus Torvalds # define ZAP_BLOCK_SIZE (8 * PAGE_SIZE) 12941da177e4SLinus Torvalds #else 12951da177e4SLinus Torvalds /* No preempt: go for improved straight-line efficiency */ 12961da177e4SLinus Torvalds # define ZAP_BLOCK_SIZE (1024 * PAGE_SIZE) 12971da177e4SLinus Torvalds #endif 12981da177e4SLinus Torvalds 12991da177e4SLinus Torvalds /** 13001da177e4SLinus Torvalds * unmap_vmas - unmap a range of memory covered by a list of vma's 13010164f69dSRandy Dunlap * @tlb: address of the caller's struct mmu_gather 13021da177e4SLinus Torvalds * @vma: the starting vma 13031da177e4SLinus Torvalds * @start_addr: virtual address at which to start unmapping 13041da177e4SLinus Torvalds * @end_addr: virtual address at which to end unmapping 13051da177e4SLinus Torvalds * @nr_accounted: Place number of unmapped pages in vm-accountable vma's here 13061da177e4SLinus Torvalds * @details: details of nonlinear truncation or shared cache invalidation 13071da177e4SLinus Torvalds * 1308ee39b37bSHugh Dickins * Returns the end address of the unmapping (restart addr if interrupted). 13091da177e4SLinus Torvalds * 1310508034a3SHugh Dickins * Unmap all pages in the vma list. 13111da177e4SLinus Torvalds * 1312508034a3SHugh Dickins * We aim to not hold locks for too long (for scheduling latency reasons). 1313508034a3SHugh Dickins * So zap pages in ZAP_BLOCK_SIZE bytecounts. This means we need to 13141da177e4SLinus Torvalds * return the ending mmu_gather to the caller. 13151da177e4SLinus Torvalds * 13161da177e4SLinus Torvalds * Only addresses between `start' and `end' will be unmapped. 13171da177e4SLinus Torvalds * 13181da177e4SLinus Torvalds * The VMA list must be sorted in ascending virtual address order. 13191da177e4SLinus Torvalds * 13201da177e4SLinus Torvalds * unmap_vmas() assumes that the caller will flush the whole unmapped address 13211da177e4SLinus Torvalds * range after unmap_vmas() returns. So the only responsibility here is to 13221da177e4SLinus Torvalds * ensure that any thus-far unmapped pages are flushed before unmap_vmas() 13231da177e4SLinus Torvalds * drops the lock and schedules. 13241da177e4SLinus Torvalds */ 1325d16dfc55SPeter Zijlstra unsigned long unmap_vmas(struct mmu_gather *tlb, 13261da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long start_addr, 13271da177e4SLinus Torvalds unsigned long end_addr, unsigned long *nr_accounted, 13281da177e4SLinus Torvalds struct zap_details *details) 13291da177e4SLinus Torvalds { 1330ee39b37bSHugh Dickins unsigned long start = start_addr; 1331cddb8a5cSAndrea Arcangeli struct mm_struct *mm = vma->vm_mm; 13321da177e4SLinus Torvalds 1333cddb8a5cSAndrea Arcangeli mmu_notifier_invalidate_range_start(mm, start_addr, end_addr); 13341da177e4SLinus Torvalds for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) { 13351da177e4SLinus Torvalds unsigned long end; 13361da177e4SLinus Torvalds 13371da177e4SLinus Torvalds start = max(vma->vm_start, start_addr); 13381da177e4SLinus Torvalds if (start >= vma->vm_end) 13391da177e4SLinus Torvalds continue; 13401da177e4SLinus Torvalds end = min(vma->vm_end, end_addr); 13411da177e4SLinus Torvalds if (end <= vma->vm_start) 13421da177e4SLinus Torvalds continue; 13431da177e4SLinus Torvalds 13441da177e4SLinus Torvalds if (vma->vm_flags & VM_ACCOUNT) 13451da177e4SLinus Torvalds *nr_accounted += (end - start) >> PAGE_SHIFT; 13461da177e4SLinus Torvalds 134734801ba9Svenkatesh.pallipadi@intel.com if (unlikely(is_pfn_mapping(vma))) 13482ab64037Svenkatesh.pallipadi@intel.com untrack_pfn_vma(vma, 0, 0); 13492ab64037Svenkatesh.pallipadi@intel.com 13501da177e4SLinus Torvalds while (start != end) { 135151c6f666SRobin Holt if (unlikely(is_vm_hugetlb_page(vma))) { 1352a137e1ccSAndi Kleen /* 1353a137e1ccSAndi Kleen * It is undesirable to test vma->vm_file as it 1354a137e1ccSAndi Kleen * should be non-null for valid hugetlb area. 1355a137e1ccSAndi Kleen * However, vm_file will be NULL in the error 1356a137e1ccSAndi Kleen * cleanup path of do_mmap_pgoff. When 1357a137e1ccSAndi Kleen * hugetlbfs ->mmap method fails, 1358a137e1ccSAndi Kleen * do_mmap_pgoff() nullifies vma->vm_file 1359a137e1ccSAndi Kleen * before calling this function to clean up. 1360a137e1ccSAndi Kleen * Since no pte has actually been setup, it is 1361a137e1ccSAndi Kleen * safe to do nothing in this case. 1362a137e1ccSAndi Kleen */ 136397a89413SPeter Zijlstra if (vma->vm_file) 136404f2cbe3SMel Gorman unmap_hugepage_range(vma, start, end, NULL); 1365a137e1ccSAndi Kleen 136651c6f666SRobin Holt start = end; 136751c6f666SRobin Holt } else 136897a89413SPeter Zijlstra start = unmap_page_range(tlb, vma, start, end, details); 136997a89413SPeter Zijlstra } 137051c6f666SRobin Holt } 13711da177e4SLinus Torvalds 1372cddb8a5cSAndrea Arcangeli mmu_notifier_invalidate_range_end(mm, start_addr, end_addr); 1373ee39b37bSHugh Dickins return start; /* which is now the end (or restart) address */ 13741da177e4SLinus Torvalds } 13751da177e4SLinus Torvalds 13761da177e4SLinus Torvalds /** 13771da177e4SLinus Torvalds * zap_page_range - remove user pages in a given range 13781da177e4SLinus Torvalds * @vma: vm_area_struct holding the applicable pages 13791da177e4SLinus Torvalds * @address: starting address of pages to zap 13801da177e4SLinus Torvalds * @size: number of bytes to zap 13811da177e4SLinus Torvalds * @details: details of nonlinear truncation or shared cache invalidation 13821da177e4SLinus Torvalds */ 1383ee39b37bSHugh Dickins unsigned long zap_page_range(struct vm_area_struct *vma, unsigned long address, 13841da177e4SLinus Torvalds unsigned long size, struct zap_details *details) 13851da177e4SLinus Torvalds { 13861da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 1387d16dfc55SPeter Zijlstra struct mmu_gather tlb; 13881da177e4SLinus Torvalds unsigned long end = address + size; 13891da177e4SLinus Torvalds unsigned long nr_accounted = 0; 13901da177e4SLinus Torvalds 13911da177e4SLinus Torvalds lru_add_drain(); 1392d16dfc55SPeter Zijlstra tlb_gather_mmu(&tlb, mm, 0); 1393365e9c87SHugh Dickins update_hiwater_rss(mm); 1394508034a3SHugh Dickins end = unmap_vmas(&tlb, vma, address, end, &nr_accounted, details); 1395d16dfc55SPeter Zijlstra tlb_finish_mmu(&tlb, address, end); 1396ee39b37bSHugh Dickins return end; 13971da177e4SLinus Torvalds } 13981da177e4SLinus Torvalds 1399c627f9ccSJack Steiner /** 1400c627f9ccSJack Steiner * zap_vma_ptes - remove ptes mapping the vma 1401c627f9ccSJack Steiner * @vma: vm_area_struct holding ptes to be zapped 1402c627f9ccSJack Steiner * @address: starting address of pages to zap 1403c627f9ccSJack Steiner * @size: number of bytes to zap 1404c627f9ccSJack Steiner * 1405c627f9ccSJack Steiner * This function only unmaps ptes assigned to VM_PFNMAP vmas. 1406c627f9ccSJack Steiner * 1407c627f9ccSJack Steiner * The entire address range must be fully contained within the vma. 1408c627f9ccSJack Steiner * 1409c627f9ccSJack Steiner * Returns 0 if successful. 1410c627f9ccSJack Steiner */ 1411c627f9ccSJack Steiner int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, 1412c627f9ccSJack Steiner unsigned long size) 1413c627f9ccSJack Steiner { 1414c627f9ccSJack Steiner if (address < vma->vm_start || address + size > vma->vm_end || 1415c627f9ccSJack Steiner !(vma->vm_flags & VM_PFNMAP)) 1416c627f9ccSJack Steiner return -1; 1417c627f9ccSJack Steiner zap_page_range(vma, address, size, NULL); 1418c627f9ccSJack Steiner return 0; 1419c627f9ccSJack Steiner } 1420c627f9ccSJack Steiner EXPORT_SYMBOL_GPL(zap_vma_ptes); 1421c627f9ccSJack Steiner 1422142762bdSJohannes Weiner /** 1423142762bdSJohannes Weiner * follow_page - look up a page descriptor from a user-virtual address 1424142762bdSJohannes Weiner * @vma: vm_area_struct mapping @address 1425142762bdSJohannes Weiner * @address: virtual address to look up 1426142762bdSJohannes Weiner * @flags: flags modifying lookup behaviour 1427142762bdSJohannes Weiner * 1428142762bdSJohannes Weiner * @flags can have FOLL_ flags set, defined in <linux/mm.h> 1429142762bdSJohannes Weiner * 1430142762bdSJohannes Weiner * Returns the mapped (struct page *), %NULL if no mapping exists, or 1431142762bdSJohannes Weiner * an error pointer if there is a mapping to something not represented 1432142762bdSJohannes Weiner * by a page descriptor (see also vm_normal_page()). 14331da177e4SLinus Torvalds */ 14346aab341eSLinus Torvalds struct page *follow_page(struct vm_area_struct *vma, unsigned long address, 1435deceb6cdSHugh Dickins unsigned int flags) 14361da177e4SLinus Torvalds { 14371da177e4SLinus Torvalds pgd_t *pgd; 14381da177e4SLinus Torvalds pud_t *pud; 14391da177e4SLinus Torvalds pmd_t *pmd; 14401da177e4SLinus Torvalds pte_t *ptep, pte; 1441deceb6cdSHugh Dickins spinlock_t *ptl; 14421da177e4SLinus Torvalds struct page *page; 14436aab341eSLinus Torvalds struct mm_struct *mm = vma->vm_mm; 14441da177e4SLinus Torvalds 1445deceb6cdSHugh Dickins page = follow_huge_addr(mm, address, flags & FOLL_WRITE); 1446deceb6cdSHugh Dickins if (!IS_ERR(page)) { 1447deceb6cdSHugh Dickins BUG_ON(flags & FOLL_GET); 1448deceb6cdSHugh Dickins goto out; 1449deceb6cdSHugh Dickins } 14501da177e4SLinus Torvalds 1451deceb6cdSHugh Dickins page = NULL; 14521da177e4SLinus Torvalds pgd = pgd_offset(mm, address); 14531da177e4SLinus Torvalds if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) 1454deceb6cdSHugh Dickins goto no_page_table; 14551da177e4SLinus Torvalds 14561da177e4SLinus Torvalds pud = pud_offset(pgd, address); 1457ceb86879SAndi Kleen if (pud_none(*pud)) 1458ceb86879SAndi Kleen goto no_page_table; 14598a07651eSHugh Dickins if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) { 1460ceb86879SAndi Kleen BUG_ON(flags & FOLL_GET); 1461ceb86879SAndi Kleen page = follow_huge_pud(mm, address, pud, flags & FOLL_WRITE); 1462ceb86879SAndi Kleen goto out; 1463ceb86879SAndi Kleen } 1464ceb86879SAndi Kleen if (unlikely(pud_bad(*pud))) 1465deceb6cdSHugh Dickins goto no_page_table; 14661da177e4SLinus Torvalds 14671da177e4SLinus Torvalds pmd = pmd_offset(pud, address); 1468aeed5fceSHugh Dickins if (pmd_none(*pmd)) 1469deceb6cdSHugh Dickins goto no_page_table; 147071e3aac0SAndrea Arcangeli if (pmd_huge(*pmd) && vma->vm_flags & VM_HUGETLB) { 1471deceb6cdSHugh Dickins BUG_ON(flags & FOLL_GET); 1472deceb6cdSHugh Dickins page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE); 1473deceb6cdSHugh Dickins goto out; 1474deceb6cdSHugh Dickins } 147571e3aac0SAndrea Arcangeli if (pmd_trans_huge(*pmd)) { 1476500d65d4SAndrea Arcangeli if (flags & FOLL_SPLIT) { 1477500d65d4SAndrea Arcangeli split_huge_page_pmd(mm, pmd); 1478500d65d4SAndrea Arcangeli goto split_fallthrough; 1479500d65d4SAndrea Arcangeli } 148071e3aac0SAndrea Arcangeli spin_lock(&mm->page_table_lock); 148171e3aac0SAndrea Arcangeli if (likely(pmd_trans_huge(*pmd))) { 148271e3aac0SAndrea Arcangeli if (unlikely(pmd_trans_splitting(*pmd))) { 148371e3aac0SAndrea Arcangeli spin_unlock(&mm->page_table_lock); 148471e3aac0SAndrea Arcangeli wait_split_huge_page(vma->anon_vma, pmd); 148571e3aac0SAndrea Arcangeli } else { 148671e3aac0SAndrea Arcangeli page = follow_trans_huge_pmd(mm, address, 148771e3aac0SAndrea Arcangeli pmd, flags); 148871e3aac0SAndrea Arcangeli spin_unlock(&mm->page_table_lock); 148971e3aac0SAndrea Arcangeli goto out; 149071e3aac0SAndrea Arcangeli } 149171e3aac0SAndrea Arcangeli } else 149271e3aac0SAndrea Arcangeli spin_unlock(&mm->page_table_lock); 149371e3aac0SAndrea Arcangeli /* fall through */ 149471e3aac0SAndrea Arcangeli } 1495500d65d4SAndrea Arcangeli split_fallthrough: 1496aeed5fceSHugh Dickins if (unlikely(pmd_bad(*pmd))) 1497aeed5fceSHugh Dickins goto no_page_table; 1498aeed5fceSHugh Dickins 1499deceb6cdSHugh Dickins ptep = pte_offset_map_lock(mm, pmd, address, &ptl); 15001da177e4SLinus Torvalds 15011da177e4SLinus Torvalds pte = *ptep; 1502deceb6cdSHugh Dickins if (!pte_present(pte)) 150389f5b7daSLinus Torvalds goto no_page; 1504deceb6cdSHugh Dickins if ((flags & FOLL_WRITE) && !pte_write(pte)) 1505deceb6cdSHugh Dickins goto unlock; 1506a13ea5b7SHugh Dickins 15076aab341eSLinus Torvalds page = vm_normal_page(vma, address, pte); 1508a13ea5b7SHugh Dickins if (unlikely(!page)) { 1509a13ea5b7SHugh Dickins if ((flags & FOLL_DUMP) || 151062eede62SHugh Dickins !is_zero_pfn(pte_pfn(pte))) 151189f5b7daSLinus Torvalds goto bad_page; 1512a13ea5b7SHugh Dickins page = pte_page(pte); 1513a13ea5b7SHugh Dickins } 1514deceb6cdSHugh Dickins 1515deceb6cdSHugh Dickins if (flags & FOLL_GET) 1516deceb6cdSHugh Dickins get_page(page); 1517deceb6cdSHugh Dickins if (flags & FOLL_TOUCH) { 1518deceb6cdSHugh Dickins if ((flags & FOLL_WRITE) && 1519deceb6cdSHugh Dickins !pte_dirty(pte) && !PageDirty(page)) 1520f33ea7f4SNick Piggin set_page_dirty(page); 1521bd775c42SKOSAKI Motohiro /* 1522bd775c42SKOSAKI Motohiro * pte_mkyoung() would be more correct here, but atomic care 1523bd775c42SKOSAKI Motohiro * is needed to avoid losing the dirty bit: it is easier to use 1524bd775c42SKOSAKI Motohiro * mark_page_accessed(). 1525bd775c42SKOSAKI Motohiro */ 15261da177e4SLinus Torvalds mark_page_accessed(page); 15271da177e4SLinus Torvalds } 1528a1fde08cSLinus Torvalds if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) { 1529110d74a9SMichel Lespinasse /* 1530110d74a9SMichel Lespinasse * The preliminary mapping check is mainly to avoid the 1531110d74a9SMichel Lespinasse * pointless overhead of lock_page on the ZERO_PAGE 1532110d74a9SMichel Lespinasse * which might bounce very badly if there is contention. 1533110d74a9SMichel Lespinasse * 1534110d74a9SMichel Lespinasse * If the page is already locked, we don't need to 1535110d74a9SMichel Lespinasse * handle it now - vmscan will handle it later if and 1536110d74a9SMichel Lespinasse * when it attempts to reclaim the page. 1537110d74a9SMichel Lespinasse */ 1538110d74a9SMichel Lespinasse if (page->mapping && trylock_page(page)) { 1539110d74a9SMichel Lespinasse lru_add_drain(); /* push cached pages to LRU */ 1540110d74a9SMichel Lespinasse /* 1541110d74a9SMichel Lespinasse * Because we lock page here and migration is 1542110d74a9SMichel Lespinasse * blocked by the pte's page reference, we need 1543110d74a9SMichel Lespinasse * only check for file-cache page truncation. 1544110d74a9SMichel Lespinasse */ 1545110d74a9SMichel Lespinasse if (page->mapping) 1546110d74a9SMichel Lespinasse mlock_vma_page(page); 1547110d74a9SMichel Lespinasse unlock_page(page); 1548110d74a9SMichel Lespinasse } 1549110d74a9SMichel Lespinasse } 1550deceb6cdSHugh Dickins unlock: 1551deceb6cdSHugh Dickins pte_unmap_unlock(ptep, ptl); 15521da177e4SLinus Torvalds out: 1553deceb6cdSHugh Dickins return page; 1554deceb6cdSHugh Dickins 155589f5b7daSLinus Torvalds bad_page: 155689f5b7daSLinus Torvalds pte_unmap_unlock(ptep, ptl); 155789f5b7daSLinus Torvalds return ERR_PTR(-EFAULT); 155889f5b7daSLinus Torvalds 155989f5b7daSLinus Torvalds no_page: 156089f5b7daSLinus Torvalds pte_unmap_unlock(ptep, ptl); 156189f5b7daSLinus Torvalds if (!pte_none(pte)) 156289f5b7daSLinus Torvalds return page; 15638e4b9a60SHugh Dickins 1564deceb6cdSHugh Dickins no_page_table: 1565deceb6cdSHugh Dickins /* 1566deceb6cdSHugh Dickins * When core dumping an enormous anonymous area that nobody 15678e4b9a60SHugh Dickins * has touched so far, we don't want to allocate unnecessary pages or 15688e4b9a60SHugh Dickins * page tables. Return error instead of NULL to skip handle_mm_fault, 15698e4b9a60SHugh Dickins * then get_dump_page() will return NULL to leave a hole in the dump. 15708e4b9a60SHugh Dickins * But we can only make this optimization where a hole would surely 15718e4b9a60SHugh Dickins * be zero-filled if handle_mm_fault() actually did handle it. 1572deceb6cdSHugh Dickins */ 15738e4b9a60SHugh Dickins if ((flags & FOLL_DUMP) && 15748e4b9a60SHugh Dickins (!vma->vm_ops || !vma->vm_ops->fault)) 15758e4b9a60SHugh Dickins return ERR_PTR(-EFAULT); 1576deceb6cdSHugh Dickins return page; 15771da177e4SLinus Torvalds } 15781da177e4SLinus Torvalds 157995042f9eSLinus Torvalds static inline int stack_guard_page(struct vm_area_struct *vma, unsigned long addr) 158095042f9eSLinus Torvalds { 1581a09a79f6SMikulas Patocka return stack_guard_page_start(vma, addr) || 1582a09a79f6SMikulas Patocka stack_guard_page_end(vma, addr+PAGE_SIZE); 158395042f9eSLinus Torvalds } 158495042f9eSLinus Torvalds 15850014bd99SHuang Ying /** 15860014bd99SHuang Ying * __get_user_pages() - pin user pages in memory 15870014bd99SHuang Ying * @tsk: task_struct of target task 15880014bd99SHuang Ying * @mm: mm_struct of target mm 15890014bd99SHuang Ying * @start: starting user address 15900014bd99SHuang Ying * @nr_pages: number of pages from start to pin 15910014bd99SHuang Ying * @gup_flags: flags modifying pin behaviour 15920014bd99SHuang Ying * @pages: array that receives pointers to the pages pinned. 15930014bd99SHuang Ying * Should be at least nr_pages long. Or NULL, if caller 15940014bd99SHuang Ying * only intends to ensure the pages are faulted in. 15950014bd99SHuang Ying * @vmas: array of pointers to vmas corresponding to each page. 15960014bd99SHuang Ying * Or NULL if the caller does not require them. 15970014bd99SHuang Ying * @nonblocking: whether waiting for disk IO or mmap_sem contention 15980014bd99SHuang Ying * 15990014bd99SHuang Ying * Returns number of pages pinned. This may be fewer than the number 16000014bd99SHuang Ying * requested. If nr_pages is 0 or negative, returns 0. If no pages 16010014bd99SHuang Ying * were pinned, returns -errno. Each page returned must be released 16020014bd99SHuang Ying * with a put_page() call when it is finished with. vmas will only 16030014bd99SHuang Ying * remain valid while mmap_sem is held. 16040014bd99SHuang Ying * 16050014bd99SHuang Ying * Must be called with mmap_sem held for read or write. 16060014bd99SHuang Ying * 16070014bd99SHuang Ying * __get_user_pages walks a process's page tables and takes a reference to 16080014bd99SHuang Ying * each struct page that each user address corresponds to at a given 16090014bd99SHuang Ying * instant. That is, it takes the page that would be accessed if a user 16100014bd99SHuang Ying * thread accesses the given user virtual address at that instant. 16110014bd99SHuang Ying * 16120014bd99SHuang Ying * This does not guarantee that the page exists in the user mappings when 16130014bd99SHuang Ying * __get_user_pages returns, and there may even be a completely different 16140014bd99SHuang Ying * page there in some cases (eg. if mmapped pagecache has been invalidated 16150014bd99SHuang Ying * and subsequently re faulted). However it does guarantee that the page 16160014bd99SHuang Ying * won't be freed completely. And mostly callers simply care that the page 16170014bd99SHuang Ying * contains data that was valid *at some point in time*. Typically, an IO 16180014bd99SHuang Ying * or similar operation cannot guarantee anything stronger anyway because 16190014bd99SHuang Ying * locks can't be held over the syscall boundary. 16200014bd99SHuang Ying * 16210014bd99SHuang Ying * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If 16220014bd99SHuang Ying * the page is written to, set_page_dirty (or set_page_dirty_lock, as 16230014bd99SHuang Ying * appropriate) must be called after the page is finished with, and 16240014bd99SHuang Ying * before put_page is called. 16250014bd99SHuang Ying * 16260014bd99SHuang Ying * If @nonblocking != NULL, __get_user_pages will not wait for disk IO 16270014bd99SHuang Ying * or mmap_sem contention, and if waiting is needed to pin all pages, 16280014bd99SHuang Ying * *@nonblocking will be set to 0. 16290014bd99SHuang Ying * 16300014bd99SHuang Ying * In most cases, get_user_pages or get_user_pages_fast should be used 16310014bd99SHuang Ying * instead of __get_user_pages. __get_user_pages should be used only if 16320014bd99SHuang Ying * you need some special @gup_flags. 16330014bd99SHuang Ying */ 1634b291f000SNick Piggin int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm, 163558fa879eSHugh Dickins unsigned long start, int nr_pages, unsigned int gup_flags, 163653a7706dSMichel Lespinasse struct page **pages, struct vm_area_struct **vmas, 163753a7706dSMichel Lespinasse int *nonblocking) 16381da177e4SLinus Torvalds { 16391da177e4SLinus Torvalds int i; 164058fa879eSHugh Dickins unsigned long vm_flags; 16411da177e4SLinus Torvalds 16429d73777eSPeter Zijlstra if (nr_pages <= 0) 1643900cf086SJonathan Corbet return 0; 164458fa879eSHugh Dickins 164558fa879eSHugh Dickins VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET)); 164658fa879eSHugh Dickins 16471da177e4SLinus Torvalds /* 16481da177e4SLinus Torvalds * Require read or write permissions. 164958fa879eSHugh Dickins * If FOLL_FORCE is set, we only require the "MAY" flags. 16501da177e4SLinus Torvalds */ 165158fa879eSHugh Dickins vm_flags = (gup_flags & FOLL_WRITE) ? 165258fa879eSHugh Dickins (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD); 165358fa879eSHugh Dickins vm_flags &= (gup_flags & FOLL_FORCE) ? 165458fa879eSHugh Dickins (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE); 16551da177e4SLinus Torvalds i = 0; 16561da177e4SLinus Torvalds 16571da177e4SLinus Torvalds do { 16581da177e4SLinus Torvalds struct vm_area_struct *vma; 16591da177e4SLinus Torvalds 16601da177e4SLinus Torvalds vma = find_extend_vma(mm, start); 1661e7f22e20SStephen Wilson if (!vma && in_gate_area(mm, start)) { 16621da177e4SLinus Torvalds unsigned long pg = start & PAGE_MASK; 16631da177e4SLinus Torvalds pgd_t *pgd; 16641da177e4SLinus Torvalds pud_t *pud; 16651da177e4SLinus Torvalds pmd_t *pmd; 16661da177e4SLinus Torvalds pte_t *pte; 1667b291f000SNick Piggin 1668b291f000SNick Piggin /* user gate pages are read-only */ 166958fa879eSHugh Dickins if (gup_flags & FOLL_WRITE) 16701da177e4SLinus Torvalds return i ? : -EFAULT; 16711da177e4SLinus Torvalds if (pg > TASK_SIZE) 16721da177e4SLinus Torvalds pgd = pgd_offset_k(pg); 16731da177e4SLinus Torvalds else 16741da177e4SLinus Torvalds pgd = pgd_offset_gate(mm, pg); 16751da177e4SLinus Torvalds BUG_ON(pgd_none(*pgd)); 16761da177e4SLinus Torvalds pud = pud_offset(pgd, pg); 16771da177e4SLinus Torvalds BUG_ON(pud_none(*pud)); 16781da177e4SLinus Torvalds pmd = pmd_offset(pud, pg); 1679690dbe1cSHugh Dickins if (pmd_none(*pmd)) 1680690dbe1cSHugh Dickins return i ? : -EFAULT; 1681f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 16821da177e4SLinus Torvalds pte = pte_offset_map(pmd, pg); 1683690dbe1cSHugh Dickins if (pte_none(*pte)) { 1684690dbe1cSHugh Dickins pte_unmap(pte); 1685690dbe1cSHugh Dickins return i ? : -EFAULT; 1686690dbe1cSHugh Dickins } 168795042f9eSLinus Torvalds vma = get_gate_vma(mm); 16881da177e4SLinus Torvalds if (pages) { 1689de51257aSHugh Dickins struct page *page; 1690de51257aSHugh Dickins 169195042f9eSLinus Torvalds page = vm_normal_page(vma, start, *pte); 1692de51257aSHugh Dickins if (!page) { 1693de51257aSHugh Dickins if (!(gup_flags & FOLL_DUMP) && 1694de51257aSHugh Dickins is_zero_pfn(pte_pfn(*pte))) 1695de51257aSHugh Dickins page = pte_page(*pte); 1696de51257aSHugh Dickins else { 1697de51257aSHugh Dickins pte_unmap(pte); 1698de51257aSHugh Dickins return i ? : -EFAULT; 1699de51257aSHugh Dickins } 1700de51257aSHugh Dickins } 17016aab341eSLinus Torvalds pages[i] = page; 17026aab341eSLinus Torvalds get_page(page); 17031da177e4SLinus Torvalds } 17041da177e4SLinus Torvalds pte_unmap(pte); 170595042f9eSLinus Torvalds goto next_page; 17061da177e4SLinus Torvalds } 17071da177e4SLinus Torvalds 1708b291f000SNick Piggin if (!vma || 1709b291f000SNick Piggin (vma->vm_flags & (VM_IO | VM_PFNMAP)) || 17101c3aff1cSHugh Dickins !(vm_flags & vma->vm_flags)) 17111da177e4SLinus Torvalds return i ? : -EFAULT; 17121da177e4SLinus Torvalds 17132a15efc9SHugh Dickins if (is_vm_hugetlb_page(vma)) { 17142a15efc9SHugh Dickins i = follow_hugetlb_page(mm, vma, pages, vmas, 171558fa879eSHugh Dickins &start, &nr_pages, i, gup_flags); 17162a15efc9SHugh Dickins continue; 17172a15efc9SHugh Dickins } 1718deceb6cdSHugh Dickins 17191da177e4SLinus Torvalds do { 172008ef4729SHugh Dickins struct page *page; 172158fa879eSHugh Dickins unsigned int foll_flags = gup_flags; 17221da177e4SLinus Torvalds 1723462e00ccSEthan Solomita /* 17244779280dSYing Han * If we have a pending SIGKILL, don't keep faulting 17251c3aff1cSHugh Dickins * pages and potentially allocating memory. 1726462e00ccSEthan Solomita */ 17271c3aff1cSHugh Dickins if (unlikely(fatal_signal_pending(current))) 17284779280dSYing Han return i ? i : -ERESTARTSYS; 1729462e00ccSEthan Solomita 1730deceb6cdSHugh Dickins cond_resched(); 17316aab341eSLinus Torvalds while (!(page = follow_page(vma, start, foll_flags))) { 1732a68d2ebcSLinus Torvalds int ret; 173353a7706dSMichel Lespinasse unsigned int fault_flags = 0; 173453a7706dSMichel Lespinasse 1735a09a79f6SMikulas Patocka /* For mlock, just skip the stack guard page. */ 1736a09a79f6SMikulas Patocka if (foll_flags & FOLL_MLOCK) { 1737a09a79f6SMikulas Patocka if (stack_guard_page(vma, start)) 1738a09a79f6SMikulas Patocka goto next_page; 1739a09a79f6SMikulas Patocka } 174053a7706dSMichel Lespinasse if (foll_flags & FOLL_WRITE) 174153a7706dSMichel Lespinasse fault_flags |= FAULT_FLAG_WRITE; 174253a7706dSMichel Lespinasse if (nonblocking) 174353a7706dSMichel Lespinasse fault_flags |= FAULT_FLAG_ALLOW_RETRY; 1744318b275fSGleb Natapov if (foll_flags & FOLL_NOWAIT) 1745318b275fSGleb Natapov fault_flags |= (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT); 1746d06063ccSLinus Torvalds 1747d26ed650SHugh Dickins ret = handle_mm_fault(mm, vma, start, 174853a7706dSMichel Lespinasse fault_flags); 1749d26ed650SHugh Dickins 175083c54070SNick Piggin if (ret & VM_FAULT_ERROR) { 175183c54070SNick Piggin if (ret & VM_FAULT_OOM) 175283c54070SNick Piggin return i ? i : -ENOMEM; 175369ebb83eSHuang Ying if (ret & (VM_FAULT_HWPOISON | 175469ebb83eSHuang Ying VM_FAULT_HWPOISON_LARGE)) { 175569ebb83eSHuang Ying if (i) 175669ebb83eSHuang Ying return i; 175769ebb83eSHuang Ying else if (gup_flags & FOLL_HWPOISON) 175869ebb83eSHuang Ying return -EHWPOISON; 175969ebb83eSHuang Ying else 176069ebb83eSHuang Ying return -EFAULT; 176169ebb83eSHuang Ying } 176269ebb83eSHuang Ying if (ret & VM_FAULT_SIGBUS) 176383c54070SNick Piggin return i ? i : -EFAULT; 176483c54070SNick Piggin BUG(); 176583c54070SNick Piggin } 1766e7f22e20SStephen Wilson 1767e7f22e20SStephen Wilson if (tsk) { 176883c54070SNick Piggin if (ret & VM_FAULT_MAJOR) 176983c54070SNick Piggin tsk->maj_flt++; 177083c54070SNick Piggin else 177183c54070SNick Piggin tsk->min_flt++; 1772e7f22e20SStephen Wilson } 177383c54070SNick Piggin 177453a7706dSMichel Lespinasse if (ret & VM_FAULT_RETRY) { 1775318b275fSGleb Natapov if (nonblocking) 177653a7706dSMichel Lespinasse *nonblocking = 0; 177753a7706dSMichel Lespinasse return i; 177853a7706dSMichel Lespinasse } 177953a7706dSMichel Lespinasse 1780f33ea7f4SNick Piggin /* 178183c54070SNick Piggin * The VM_FAULT_WRITE bit tells us that 178283c54070SNick Piggin * do_wp_page has broken COW when necessary, 178383c54070SNick Piggin * even if maybe_mkwrite decided not to set 178483c54070SNick Piggin * pte_write. We can thus safely do subsequent 1785878b63acSHugh Dickins * page lookups as if they were reads. But only 1786878b63acSHugh Dickins * do so when looping for pte_write is futile: 1787878b63acSHugh Dickins * in some cases userspace may also be wanting 1788878b63acSHugh Dickins * to write to the gotten user page, which a 1789878b63acSHugh Dickins * read fault here might prevent (a readonly 1790878b63acSHugh Dickins * page might get reCOWed by userspace write). 1791f33ea7f4SNick Piggin */ 1792878b63acSHugh Dickins if ((ret & VM_FAULT_WRITE) && 1793878b63acSHugh Dickins !(vma->vm_flags & VM_WRITE)) 1794deceb6cdSHugh Dickins foll_flags &= ~FOLL_WRITE; 1795a68d2ebcSLinus Torvalds 17967f7bbbe5SBenjamin Herrenschmidt cond_resched(); 17971da177e4SLinus Torvalds } 179889f5b7daSLinus Torvalds if (IS_ERR(page)) 179989f5b7daSLinus Torvalds return i ? i : PTR_ERR(page); 18001da177e4SLinus Torvalds if (pages) { 180108ef4729SHugh Dickins pages[i] = page; 180203beb076SJames Bottomley 1803a6f36be3SRussell King flush_anon_page(vma, page, start); 180408ef4729SHugh Dickins flush_dcache_page(page); 18051da177e4SLinus Torvalds } 180695042f9eSLinus Torvalds next_page: 18071da177e4SLinus Torvalds if (vmas) 18081da177e4SLinus Torvalds vmas[i] = vma; 18091da177e4SLinus Torvalds i++; 18101da177e4SLinus Torvalds start += PAGE_SIZE; 18119d73777eSPeter Zijlstra nr_pages--; 18129d73777eSPeter Zijlstra } while (nr_pages && start < vma->vm_end); 18139d73777eSPeter Zijlstra } while (nr_pages); 18141da177e4SLinus Torvalds return i; 18151da177e4SLinus Torvalds } 18160014bd99SHuang Ying EXPORT_SYMBOL(__get_user_pages); 1817b291f000SNick Piggin 1818d2bf6be8SNick Piggin /** 1819d2bf6be8SNick Piggin * get_user_pages() - pin user pages in memory 1820e7f22e20SStephen Wilson * @tsk: the task_struct to use for page fault accounting, or 1821e7f22e20SStephen Wilson * NULL if faults are not to be recorded. 1822d2bf6be8SNick Piggin * @mm: mm_struct of target mm 1823d2bf6be8SNick Piggin * @start: starting user address 18249d73777eSPeter Zijlstra * @nr_pages: number of pages from start to pin 1825d2bf6be8SNick Piggin * @write: whether pages will be written to by the caller 1826d2bf6be8SNick Piggin * @force: whether to force write access even if user mapping is 1827d2bf6be8SNick Piggin * readonly. This will result in the page being COWed even 1828d2bf6be8SNick Piggin * in MAP_SHARED mappings. You do not want this. 1829d2bf6be8SNick Piggin * @pages: array that receives pointers to the pages pinned. 1830d2bf6be8SNick Piggin * Should be at least nr_pages long. Or NULL, if caller 1831d2bf6be8SNick Piggin * only intends to ensure the pages are faulted in. 1832d2bf6be8SNick Piggin * @vmas: array of pointers to vmas corresponding to each page. 1833d2bf6be8SNick Piggin * Or NULL if the caller does not require them. 1834d2bf6be8SNick Piggin * 1835d2bf6be8SNick Piggin * Returns number of pages pinned. This may be fewer than the number 18369d73777eSPeter Zijlstra * requested. If nr_pages is 0 or negative, returns 0. If no pages 1837d2bf6be8SNick Piggin * were pinned, returns -errno. Each page returned must be released 1838d2bf6be8SNick Piggin * with a put_page() call when it is finished with. vmas will only 1839d2bf6be8SNick Piggin * remain valid while mmap_sem is held. 1840d2bf6be8SNick Piggin * 1841d2bf6be8SNick Piggin * Must be called with mmap_sem held for read or write. 1842d2bf6be8SNick Piggin * 1843d2bf6be8SNick Piggin * get_user_pages walks a process's page tables and takes a reference to 1844d2bf6be8SNick Piggin * each struct page that each user address corresponds to at a given 1845d2bf6be8SNick Piggin * instant. That is, it takes the page that would be accessed if a user 1846d2bf6be8SNick Piggin * thread accesses the given user virtual address at that instant. 1847d2bf6be8SNick Piggin * 1848d2bf6be8SNick Piggin * This does not guarantee that the page exists in the user mappings when 1849d2bf6be8SNick Piggin * get_user_pages returns, and there may even be a completely different 1850d2bf6be8SNick Piggin * page there in some cases (eg. if mmapped pagecache has been invalidated 1851d2bf6be8SNick Piggin * and subsequently re faulted). However it does guarantee that the page 1852d2bf6be8SNick Piggin * won't be freed completely. And mostly callers simply care that the page 1853d2bf6be8SNick Piggin * contains data that was valid *at some point in time*. Typically, an IO 1854d2bf6be8SNick Piggin * or similar operation cannot guarantee anything stronger anyway because 1855d2bf6be8SNick Piggin * locks can't be held over the syscall boundary. 1856d2bf6be8SNick Piggin * 1857d2bf6be8SNick Piggin * If write=0, the page must not be written to. If the page is written to, 1858d2bf6be8SNick Piggin * set_page_dirty (or set_page_dirty_lock, as appropriate) must be called 1859d2bf6be8SNick Piggin * after the page is finished with, and before put_page is called. 1860d2bf6be8SNick Piggin * 1861d2bf6be8SNick Piggin * get_user_pages is typically used for fewer-copy IO operations, to get a 1862d2bf6be8SNick Piggin * handle on the memory by some means other than accesses via the user virtual 1863d2bf6be8SNick Piggin * addresses. The pages may be submitted for DMA to devices or accessed via 1864d2bf6be8SNick Piggin * their kernel linear mapping (via the kmap APIs). Care should be taken to 1865d2bf6be8SNick Piggin * use the correct cache flushing APIs. 1866d2bf6be8SNick Piggin * 1867d2bf6be8SNick Piggin * See also get_user_pages_fast, for performance critical applications. 1868d2bf6be8SNick Piggin */ 1869b291f000SNick Piggin int get_user_pages(struct task_struct *tsk, struct mm_struct *mm, 18709d73777eSPeter Zijlstra unsigned long start, int nr_pages, int write, int force, 1871b291f000SNick Piggin struct page **pages, struct vm_area_struct **vmas) 1872b291f000SNick Piggin { 187358fa879eSHugh Dickins int flags = FOLL_TOUCH; 1874b291f000SNick Piggin 187558fa879eSHugh Dickins if (pages) 187658fa879eSHugh Dickins flags |= FOLL_GET; 1877b291f000SNick Piggin if (write) 187858fa879eSHugh Dickins flags |= FOLL_WRITE; 1879b291f000SNick Piggin if (force) 188058fa879eSHugh Dickins flags |= FOLL_FORCE; 1881b291f000SNick Piggin 188253a7706dSMichel Lespinasse return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas, 188353a7706dSMichel Lespinasse NULL); 1884b291f000SNick Piggin } 18851da177e4SLinus Torvalds EXPORT_SYMBOL(get_user_pages); 18861da177e4SLinus Torvalds 1887f3e8fccdSHugh Dickins /** 1888f3e8fccdSHugh Dickins * get_dump_page() - pin user page in memory while writing it to core dump 1889f3e8fccdSHugh Dickins * @addr: user address 1890f3e8fccdSHugh Dickins * 1891f3e8fccdSHugh Dickins * Returns struct page pointer of user page pinned for dump, 1892f3e8fccdSHugh Dickins * to be freed afterwards by page_cache_release() or put_page(). 1893f3e8fccdSHugh Dickins * 1894f3e8fccdSHugh Dickins * Returns NULL on any kind of failure - a hole must then be inserted into 1895f3e8fccdSHugh Dickins * the corefile, to preserve alignment with its headers; and also returns 1896f3e8fccdSHugh Dickins * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found - 1897f3e8fccdSHugh Dickins * allowing a hole to be left in the corefile to save diskspace. 1898f3e8fccdSHugh Dickins * 1899f3e8fccdSHugh Dickins * Called without mmap_sem, but after all other threads have been killed. 1900f3e8fccdSHugh Dickins */ 1901f3e8fccdSHugh Dickins #ifdef CONFIG_ELF_CORE 1902f3e8fccdSHugh Dickins struct page *get_dump_page(unsigned long addr) 1903f3e8fccdSHugh Dickins { 1904f3e8fccdSHugh Dickins struct vm_area_struct *vma; 1905f3e8fccdSHugh Dickins struct page *page; 1906f3e8fccdSHugh Dickins 1907f3e8fccdSHugh Dickins if (__get_user_pages(current, current->mm, addr, 1, 190853a7706dSMichel Lespinasse FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma, 190953a7706dSMichel Lespinasse NULL) < 1) 1910f3e8fccdSHugh Dickins return NULL; 1911f3e8fccdSHugh Dickins flush_cache_page(vma, addr, page_to_pfn(page)); 1912f3e8fccdSHugh Dickins return page; 1913f3e8fccdSHugh Dickins } 1914f3e8fccdSHugh Dickins #endif /* CONFIG_ELF_CORE */ 1915f3e8fccdSHugh Dickins 191625ca1d6cSNamhyung Kim pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr, 1917920c7a5dSHarvey Harrison spinlock_t **ptl) 1918c9cfcddfSLinus Torvalds { 1919c9cfcddfSLinus Torvalds pgd_t * pgd = pgd_offset(mm, addr); 1920c9cfcddfSLinus Torvalds pud_t * pud = pud_alloc(mm, pgd, addr); 1921c9cfcddfSLinus Torvalds if (pud) { 192249c91fb0STrond Myklebust pmd_t * pmd = pmd_alloc(mm, pud, addr); 1923f66055abSAndrea Arcangeli if (pmd) { 1924f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 1925c9cfcddfSLinus Torvalds return pte_alloc_map_lock(mm, pmd, addr, ptl); 1926c9cfcddfSLinus Torvalds } 1927f66055abSAndrea Arcangeli } 1928c9cfcddfSLinus Torvalds return NULL; 1929c9cfcddfSLinus Torvalds } 1930c9cfcddfSLinus Torvalds 19311da177e4SLinus Torvalds /* 1932238f58d8SLinus Torvalds * This is the old fallback for page remapping. 1933238f58d8SLinus Torvalds * 1934238f58d8SLinus Torvalds * For historical reasons, it only allows reserved pages. Only 1935238f58d8SLinus Torvalds * old drivers should use this, and they needed to mark their 1936238f58d8SLinus Torvalds * pages reserved for the old functions anyway. 1937238f58d8SLinus Torvalds */ 1938423bad60SNick Piggin static int insert_page(struct vm_area_struct *vma, unsigned long addr, 1939423bad60SNick Piggin struct page *page, pgprot_t prot) 1940238f58d8SLinus Torvalds { 1941423bad60SNick Piggin struct mm_struct *mm = vma->vm_mm; 1942238f58d8SLinus Torvalds int retval; 1943238f58d8SLinus Torvalds pte_t *pte; 1944238f58d8SLinus Torvalds spinlock_t *ptl; 1945238f58d8SLinus Torvalds 1946238f58d8SLinus Torvalds retval = -EINVAL; 1947a145dd41SLinus Torvalds if (PageAnon(page)) 19485b4e655eSKAMEZAWA Hiroyuki goto out; 1949238f58d8SLinus Torvalds retval = -ENOMEM; 1950238f58d8SLinus Torvalds flush_dcache_page(page); 1951c9cfcddfSLinus Torvalds pte = get_locked_pte(mm, addr, &ptl); 1952238f58d8SLinus Torvalds if (!pte) 19535b4e655eSKAMEZAWA Hiroyuki goto out; 1954238f58d8SLinus Torvalds retval = -EBUSY; 1955238f58d8SLinus Torvalds if (!pte_none(*pte)) 1956238f58d8SLinus Torvalds goto out_unlock; 1957238f58d8SLinus Torvalds 1958238f58d8SLinus Torvalds /* Ok, finally just insert the thing.. */ 1959238f58d8SLinus Torvalds get_page(page); 196034e55232SKAMEZAWA Hiroyuki inc_mm_counter_fast(mm, MM_FILEPAGES); 1961238f58d8SLinus Torvalds page_add_file_rmap(page); 1962238f58d8SLinus Torvalds set_pte_at(mm, addr, pte, mk_pte(page, prot)); 1963238f58d8SLinus Torvalds 1964238f58d8SLinus Torvalds retval = 0; 19658a9f3ccdSBalbir Singh pte_unmap_unlock(pte, ptl); 19668a9f3ccdSBalbir Singh return retval; 1967238f58d8SLinus Torvalds out_unlock: 1968238f58d8SLinus Torvalds pte_unmap_unlock(pte, ptl); 1969238f58d8SLinus Torvalds out: 1970238f58d8SLinus Torvalds return retval; 1971238f58d8SLinus Torvalds } 1972238f58d8SLinus Torvalds 1973bfa5bf6dSRolf Eike Beer /** 1974bfa5bf6dSRolf Eike Beer * vm_insert_page - insert single page into user vma 1975bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 1976bfa5bf6dSRolf Eike Beer * @addr: target user address of this page 1977bfa5bf6dSRolf Eike Beer * @page: source kernel page 1978bfa5bf6dSRolf Eike Beer * 1979a145dd41SLinus Torvalds * This allows drivers to insert individual pages they've allocated 1980a145dd41SLinus Torvalds * into a user vma. 1981a145dd41SLinus Torvalds * 1982a145dd41SLinus Torvalds * The page has to be a nice clean _individual_ kernel allocation. 1983a145dd41SLinus Torvalds * If you allocate a compound page, you need to have marked it as 1984a145dd41SLinus Torvalds * such (__GFP_COMP), or manually just split the page up yourself 19858dfcc9baSNick Piggin * (see split_page()). 1986a145dd41SLinus Torvalds * 1987a145dd41SLinus Torvalds * NOTE! Traditionally this was done with "remap_pfn_range()" which 1988a145dd41SLinus Torvalds * took an arbitrary page protection parameter. This doesn't allow 1989a145dd41SLinus Torvalds * that. Your vma protection will have to be set up correctly, which 1990a145dd41SLinus Torvalds * means that if you want a shared writable mapping, you'd better 1991a145dd41SLinus Torvalds * ask for a shared writable mapping! 1992a145dd41SLinus Torvalds * 1993a145dd41SLinus Torvalds * The page does not need to be reserved. 1994a145dd41SLinus Torvalds */ 1995423bad60SNick Piggin int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, 1996423bad60SNick Piggin struct page *page) 1997a145dd41SLinus Torvalds { 1998a145dd41SLinus Torvalds if (addr < vma->vm_start || addr >= vma->vm_end) 1999a145dd41SLinus Torvalds return -EFAULT; 2000a145dd41SLinus Torvalds if (!page_count(page)) 2001a145dd41SLinus Torvalds return -EINVAL; 20024d7672b4SLinus Torvalds vma->vm_flags |= VM_INSERTPAGE; 2003423bad60SNick Piggin return insert_page(vma, addr, page, vma->vm_page_prot); 2004a145dd41SLinus Torvalds } 2005e3c3374fSLinus Torvalds EXPORT_SYMBOL(vm_insert_page); 2006a145dd41SLinus Torvalds 2007423bad60SNick Piggin static int insert_pfn(struct vm_area_struct *vma, unsigned long addr, 2008423bad60SNick Piggin unsigned long pfn, pgprot_t prot) 2009423bad60SNick Piggin { 2010423bad60SNick Piggin struct mm_struct *mm = vma->vm_mm; 2011423bad60SNick Piggin int retval; 2012423bad60SNick Piggin pte_t *pte, entry; 2013423bad60SNick Piggin spinlock_t *ptl; 2014423bad60SNick Piggin 2015423bad60SNick Piggin retval = -ENOMEM; 2016423bad60SNick Piggin pte = get_locked_pte(mm, addr, &ptl); 2017423bad60SNick Piggin if (!pte) 2018423bad60SNick Piggin goto out; 2019423bad60SNick Piggin retval = -EBUSY; 2020423bad60SNick Piggin if (!pte_none(*pte)) 2021423bad60SNick Piggin goto out_unlock; 2022423bad60SNick Piggin 2023423bad60SNick Piggin /* Ok, finally just insert the thing.. */ 2024423bad60SNick Piggin entry = pte_mkspecial(pfn_pte(pfn, prot)); 2025423bad60SNick Piggin set_pte_at(mm, addr, pte, entry); 20264b3073e1SRussell King update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */ 2027423bad60SNick Piggin 2028423bad60SNick Piggin retval = 0; 2029423bad60SNick Piggin out_unlock: 2030423bad60SNick Piggin pte_unmap_unlock(pte, ptl); 2031423bad60SNick Piggin out: 2032423bad60SNick Piggin return retval; 2033423bad60SNick Piggin } 2034423bad60SNick Piggin 2035e0dc0d8fSNick Piggin /** 2036e0dc0d8fSNick Piggin * vm_insert_pfn - insert single pfn into user vma 2037e0dc0d8fSNick Piggin * @vma: user vma to map to 2038e0dc0d8fSNick Piggin * @addr: target user address of this page 2039e0dc0d8fSNick Piggin * @pfn: source kernel pfn 2040e0dc0d8fSNick Piggin * 2041e0dc0d8fSNick Piggin * Similar to vm_inert_page, this allows drivers to insert individual pages 2042e0dc0d8fSNick Piggin * they've allocated into a user vma. Same comments apply. 2043e0dc0d8fSNick Piggin * 2044e0dc0d8fSNick Piggin * This function should only be called from a vm_ops->fault handler, and 2045e0dc0d8fSNick Piggin * in that case the handler should return NULL. 20460d71d10aSNick Piggin * 20470d71d10aSNick Piggin * vma cannot be a COW mapping. 20480d71d10aSNick Piggin * 20490d71d10aSNick Piggin * As this is called only for pages that do not currently exist, we 20500d71d10aSNick Piggin * do not need to flush old virtual caches or the TLB. 2051e0dc0d8fSNick Piggin */ 2052e0dc0d8fSNick Piggin int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr, 2053e0dc0d8fSNick Piggin unsigned long pfn) 2054e0dc0d8fSNick Piggin { 20552ab64037Svenkatesh.pallipadi@intel.com int ret; 2056e4b866edSvenkatesh.pallipadi@intel.com pgprot_t pgprot = vma->vm_page_prot; 20577e675137SNick Piggin /* 20587e675137SNick Piggin * Technically, architectures with pte_special can avoid all these 20597e675137SNick Piggin * restrictions (same for remap_pfn_range). However we would like 20607e675137SNick Piggin * consistency in testing and feature parity among all, so we should 20617e675137SNick Piggin * try to keep these invariants in place for everybody. 20627e675137SNick Piggin */ 2063b379d790SJared Hulbert BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))); 2064b379d790SJared Hulbert BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) == 2065b379d790SJared Hulbert (VM_PFNMAP|VM_MIXEDMAP)); 2066b379d790SJared Hulbert BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); 2067b379d790SJared Hulbert BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn)); 2068e0dc0d8fSNick Piggin 2069423bad60SNick Piggin if (addr < vma->vm_start || addr >= vma->vm_end) 2070423bad60SNick Piggin return -EFAULT; 2071e4b866edSvenkatesh.pallipadi@intel.com if (track_pfn_vma_new(vma, &pgprot, pfn, PAGE_SIZE)) 20722ab64037Svenkatesh.pallipadi@intel.com return -EINVAL; 20732ab64037Svenkatesh.pallipadi@intel.com 2074e4b866edSvenkatesh.pallipadi@intel.com ret = insert_pfn(vma, addr, pfn, pgprot); 20752ab64037Svenkatesh.pallipadi@intel.com 20762ab64037Svenkatesh.pallipadi@intel.com if (ret) 20772ab64037Svenkatesh.pallipadi@intel.com untrack_pfn_vma(vma, pfn, PAGE_SIZE); 20782ab64037Svenkatesh.pallipadi@intel.com 20792ab64037Svenkatesh.pallipadi@intel.com return ret; 2080e0dc0d8fSNick Piggin } 2081e0dc0d8fSNick Piggin EXPORT_SYMBOL(vm_insert_pfn); 2082e0dc0d8fSNick Piggin 2083423bad60SNick Piggin int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr, 2084423bad60SNick Piggin unsigned long pfn) 2085423bad60SNick Piggin { 2086423bad60SNick Piggin BUG_ON(!(vma->vm_flags & VM_MIXEDMAP)); 2087423bad60SNick Piggin 2088423bad60SNick Piggin if (addr < vma->vm_start || addr >= vma->vm_end) 2089423bad60SNick Piggin return -EFAULT; 2090423bad60SNick Piggin 2091423bad60SNick Piggin /* 2092423bad60SNick Piggin * If we don't have pte special, then we have to use the pfn_valid() 2093423bad60SNick Piggin * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must* 2094423bad60SNick Piggin * refcount the page if pfn_valid is true (hence insert_page rather 209562eede62SHugh Dickins * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP 209662eede62SHugh Dickins * without pte special, it would there be refcounted as a normal page. 2097423bad60SNick Piggin */ 2098423bad60SNick Piggin if (!HAVE_PTE_SPECIAL && pfn_valid(pfn)) { 2099423bad60SNick Piggin struct page *page; 2100423bad60SNick Piggin 2101423bad60SNick Piggin page = pfn_to_page(pfn); 2102423bad60SNick Piggin return insert_page(vma, addr, page, vma->vm_page_prot); 2103423bad60SNick Piggin } 2104423bad60SNick Piggin return insert_pfn(vma, addr, pfn, vma->vm_page_prot); 2105423bad60SNick Piggin } 2106423bad60SNick Piggin EXPORT_SYMBOL(vm_insert_mixed); 2107423bad60SNick Piggin 2108a145dd41SLinus Torvalds /* 21091da177e4SLinus Torvalds * maps a range of physical memory into the requested pages. the old 21101da177e4SLinus Torvalds * mappings are removed. any references to nonexistent pages results 21111da177e4SLinus Torvalds * in null mappings (currently treated as "copy-on-access") 21121da177e4SLinus Torvalds */ 21131da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd, 21141da177e4SLinus Torvalds unsigned long addr, unsigned long end, 21151da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 21161da177e4SLinus Torvalds { 21171da177e4SLinus Torvalds pte_t *pte; 2118c74df32cSHugh Dickins spinlock_t *ptl; 21191da177e4SLinus Torvalds 2120c74df32cSHugh Dickins pte = pte_alloc_map_lock(mm, pmd, addr, &ptl); 21211da177e4SLinus Torvalds if (!pte) 21221da177e4SLinus Torvalds return -ENOMEM; 21236606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 21241da177e4SLinus Torvalds do { 21251da177e4SLinus Torvalds BUG_ON(!pte_none(*pte)); 21267e675137SNick Piggin set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot))); 21271da177e4SLinus Torvalds pfn++; 21281da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 21296606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 2130c74df32cSHugh Dickins pte_unmap_unlock(pte - 1, ptl); 21311da177e4SLinus Torvalds return 0; 21321da177e4SLinus Torvalds } 21331da177e4SLinus Torvalds 21341da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud, 21351da177e4SLinus Torvalds unsigned long addr, unsigned long end, 21361da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 21371da177e4SLinus Torvalds { 21381da177e4SLinus Torvalds pmd_t *pmd; 21391da177e4SLinus Torvalds unsigned long next; 21401da177e4SLinus Torvalds 21411da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 21421da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, addr); 21431da177e4SLinus Torvalds if (!pmd) 21441da177e4SLinus Torvalds return -ENOMEM; 2145f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 21461da177e4SLinus Torvalds do { 21471da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 21481da177e4SLinus Torvalds if (remap_pte_range(mm, pmd, addr, next, 21491da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot)) 21501da177e4SLinus Torvalds return -ENOMEM; 21511da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 21521da177e4SLinus Torvalds return 0; 21531da177e4SLinus Torvalds } 21541da177e4SLinus Torvalds 21551da177e4SLinus Torvalds static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd, 21561da177e4SLinus Torvalds unsigned long addr, unsigned long end, 21571da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 21581da177e4SLinus Torvalds { 21591da177e4SLinus Torvalds pud_t *pud; 21601da177e4SLinus Torvalds unsigned long next; 21611da177e4SLinus Torvalds 21621da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 21631da177e4SLinus Torvalds pud = pud_alloc(mm, pgd, addr); 21641da177e4SLinus Torvalds if (!pud) 21651da177e4SLinus Torvalds return -ENOMEM; 21661da177e4SLinus Torvalds do { 21671da177e4SLinus Torvalds next = pud_addr_end(addr, end); 21681da177e4SLinus Torvalds if (remap_pmd_range(mm, pud, addr, next, 21691da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot)) 21701da177e4SLinus Torvalds return -ENOMEM; 21711da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 21721da177e4SLinus Torvalds return 0; 21731da177e4SLinus Torvalds } 21741da177e4SLinus Torvalds 2175bfa5bf6dSRolf Eike Beer /** 2176bfa5bf6dSRolf Eike Beer * remap_pfn_range - remap kernel memory to userspace 2177bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 2178bfa5bf6dSRolf Eike Beer * @addr: target user address to start at 2179bfa5bf6dSRolf Eike Beer * @pfn: physical address of kernel memory 2180bfa5bf6dSRolf Eike Beer * @size: size of map area 2181bfa5bf6dSRolf Eike Beer * @prot: page protection flags for this mapping 2182bfa5bf6dSRolf Eike Beer * 2183bfa5bf6dSRolf Eike Beer * Note: this is only safe if the mm semaphore is held when called. 2184bfa5bf6dSRolf Eike Beer */ 21851da177e4SLinus Torvalds int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 21861da177e4SLinus Torvalds unsigned long pfn, unsigned long size, pgprot_t prot) 21871da177e4SLinus Torvalds { 21881da177e4SLinus Torvalds pgd_t *pgd; 21891da177e4SLinus Torvalds unsigned long next; 21902d15cab8SHugh Dickins unsigned long end = addr + PAGE_ALIGN(size); 21911da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 21921da177e4SLinus Torvalds int err; 21931da177e4SLinus Torvalds 21941da177e4SLinus Torvalds /* 21951da177e4SLinus Torvalds * Physically remapped pages are special. Tell the 21961da177e4SLinus Torvalds * rest of the world about it: 21971da177e4SLinus Torvalds * VM_IO tells people not to look at these pages 21981da177e4SLinus Torvalds * (accesses can have side effects). 21990b14c179SHugh Dickins * VM_RESERVED is specified all over the place, because 22000b14c179SHugh Dickins * in 2.4 it kept swapout's vma scan off this vma; but 22010b14c179SHugh Dickins * in 2.6 the LRU scan won't even find its pages, so this 22020b14c179SHugh Dickins * flag means no more than count its pages in reserved_vm, 22030b14c179SHugh Dickins * and omit it from core dump, even when VM_IO turned off. 22046aab341eSLinus Torvalds * VM_PFNMAP tells the core MM that the base pages are just 22056aab341eSLinus Torvalds * raw PFN mappings, and do not have a "struct page" associated 22066aab341eSLinus Torvalds * with them. 2207fb155c16SLinus Torvalds * 2208fb155c16SLinus Torvalds * There's a horrible special case to handle copy-on-write 2209fb155c16SLinus Torvalds * behaviour that some programs depend on. We mark the "original" 2210fb155c16SLinus Torvalds * un-COW'ed pages by matching them up with "vma->vm_pgoff". 22111da177e4SLinus Torvalds */ 22124bb9c5c0SPallipadi, Venkatesh if (addr == vma->vm_start && end == vma->vm_end) { 22136aab341eSLinus Torvalds vma->vm_pgoff = pfn; 2214895791daSPallipadi, Venkatesh vma->vm_flags |= VM_PFN_AT_MMAP; 22154bb9c5c0SPallipadi, Venkatesh } else if (is_cow_mapping(vma->vm_flags)) 22163c8bb73aSvenkatesh.pallipadi@intel.com return -EINVAL; 2217fb155c16SLinus Torvalds 2218fb155c16SLinus Torvalds vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP; 22191da177e4SLinus Torvalds 2220e4b866edSvenkatesh.pallipadi@intel.com err = track_pfn_vma_new(vma, &prot, pfn, PAGE_ALIGN(size)); 2221a3670613Svenkatesh.pallipadi@intel.com if (err) { 2222a3670613Svenkatesh.pallipadi@intel.com /* 2223a3670613Svenkatesh.pallipadi@intel.com * To indicate that track_pfn related cleanup is not 2224a3670613Svenkatesh.pallipadi@intel.com * needed from higher level routine calling unmap_vmas 2225a3670613Svenkatesh.pallipadi@intel.com */ 2226a3670613Svenkatesh.pallipadi@intel.com vma->vm_flags &= ~(VM_IO | VM_RESERVED | VM_PFNMAP); 2227895791daSPallipadi, Venkatesh vma->vm_flags &= ~VM_PFN_AT_MMAP; 22282ab64037Svenkatesh.pallipadi@intel.com return -EINVAL; 2229a3670613Svenkatesh.pallipadi@intel.com } 22302ab64037Svenkatesh.pallipadi@intel.com 22311da177e4SLinus Torvalds BUG_ON(addr >= end); 22321da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 22331da177e4SLinus Torvalds pgd = pgd_offset(mm, addr); 22341da177e4SLinus Torvalds flush_cache_range(vma, addr, end); 22351da177e4SLinus Torvalds do { 22361da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 22371da177e4SLinus Torvalds err = remap_pud_range(mm, pgd, addr, next, 22381da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot); 22391da177e4SLinus Torvalds if (err) 22401da177e4SLinus Torvalds break; 22411da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 22422ab64037Svenkatesh.pallipadi@intel.com 22432ab64037Svenkatesh.pallipadi@intel.com if (err) 22442ab64037Svenkatesh.pallipadi@intel.com untrack_pfn_vma(vma, pfn, PAGE_ALIGN(size)); 22452ab64037Svenkatesh.pallipadi@intel.com 22461da177e4SLinus Torvalds return err; 22471da177e4SLinus Torvalds } 22481da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range); 22491da177e4SLinus Torvalds 2250aee16b3cSJeremy Fitzhardinge static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd, 2251aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2252aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 2253aee16b3cSJeremy Fitzhardinge { 2254aee16b3cSJeremy Fitzhardinge pte_t *pte; 2255aee16b3cSJeremy Fitzhardinge int err; 22562f569afdSMartin Schwidefsky pgtable_t token; 225794909914SBorislav Petkov spinlock_t *uninitialized_var(ptl); 2258aee16b3cSJeremy Fitzhardinge 2259aee16b3cSJeremy Fitzhardinge pte = (mm == &init_mm) ? 2260aee16b3cSJeremy Fitzhardinge pte_alloc_kernel(pmd, addr) : 2261aee16b3cSJeremy Fitzhardinge pte_alloc_map_lock(mm, pmd, addr, &ptl); 2262aee16b3cSJeremy Fitzhardinge if (!pte) 2263aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2264aee16b3cSJeremy Fitzhardinge 2265aee16b3cSJeremy Fitzhardinge BUG_ON(pmd_huge(*pmd)); 2266aee16b3cSJeremy Fitzhardinge 226738e0edb1SJeremy Fitzhardinge arch_enter_lazy_mmu_mode(); 226838e0edb1SJeremy Fitzhardinge 22692f569afdSMartin Schwidefsky token = pmd_pgtable(*pmd); 2270aee16b3cSJeremy Fitzhardinge 2271aee16b3cSJeremy Fitzhardinge do { 2272c36987e2SDaisuke Nishimura err = fn(pte++, token, addr, data); 2273aee16b3cSJeremy Fitzhardinge if (err) 2274aee16b3cSJeremy Fitzhardinge break; 2275c36987e2SDaisuke Nishimura } while (addr += PAGE_SIZE, addr != end); 2276aee16b3cSJeremy Fitzhardinge 227738e0edb1SJeremy Fitzhardinge arch_leave_lazy_mmu_mode(); 227838e0edb1SJeremy Fitzhardinge 2279aee16b3cSJeremy Fitzhardinge if (mm != &init_mm) 2280aee16b3cSJeremy Fitzhardinge pte_unmap_unlock(pte-1, ptl); 2281aee16b3cSJeremy Fitzhardinge return err; 2282aee16b3cSJeremy Fitzhardinge } 2283aee16b3cSJeremy Fitzhardinge 2284aee16b3cSJeremy Fitzhardinge static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud, 2285aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2286aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 2287aee16b3cSJeremy Fitzhardinge { 2288aee16b3cSJeremy Fitzhardinge pmd_t *pmd; 2289aee16b3cSJeremy Fitzhardinge unsigned long next; 2290aee16b3cSJeremy Fitzhardinge int err; 2291aee16b3cSJeremy Fitzhardinge 2292ceb86879SAndi Kleen BUG_ON(pud_huge(*pud)); 2293ceb86879SAndi Kleen 2294aee16b3cSJeremy Fitzhardinge pmd = pmd_alloc(mm, pud, addr); 2295aee16b3cSJeremy Fitzhardinge if (!pmd) 2296aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2297aee16b3cSJeremy Fitzhardinge do { 2298aee16b3cSJeremy Fitzhardinge next = pmd_addr_end(addr, end); 2299aee16b3cSJeremy Fitzhardinge err = apply_to_pte_range(mm, pmd, addr, next, fn, data); 2300aee16b3cSJeremy Fitzhardinge if (err) 2301aee16b3cSJeremy Fitzhardinge break; 2302aee16b3cSJeremy Fitzhardinge } while (pmd++, addr = next, addr != end); 2303aee16b3cSJeremy Fitzhardinge return err; 2304aee16b3cSJeremy Fitzhardinge } 2305aee16b3cSJeremy Fitzhardinge 2306aee16b3cSJeremy Fitzhardinge static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd, 2307aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2308aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 2309aee16b3cSJeremy Fitzhardinge { 2310aee16b3cSJeremy Fitzhardinge pud_t *pud; 2311aee16b3cSJeremy Fitzhardinge unsigned long next; 2312aee16b3cSJeremy Fitzhardinge int err; 2313aee16b3cSJeremy Fitzhardinge 2314aee16b3cSJeremy Fitzhardinge pud = pud_alloc(mm, pgd, addr); 2315aee16b3cSJeremy Fitzhardinge if (!pud) 2316aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2317aee16b3cSJeremy Fitzhardinge do { 2318aee16b3cSJeremy Fitzhardinge next = pud_addr_end(addr, end); 2319aee16b3cSJeremy Fitzhardinge err = apply_to_pmd_range(mm, pud, addr, next, fn, data); 2320aee16b3cSJeremy Fitzhardinge if (err) 2321aee16b3cSJeremy Fitzhardinge break; 2322aee16b3cSJeremy Fitzhardinge } while (pud++, addr = next, addr != end); 2323aee16b3cSJeremy Fitzhardinge return err; 2324aee16b3cSJeremy Fitzhardinge } 2325aee16b3cSJeremy Fitzhardinge 2326aee16b3cSJeremy Fitzhardinge /* 2327aee16b3cSJeremy Fitzhardinge * Scan a region of virtual memory, filling in page tables as necessary 2328aee16b3cSJeremy Fitzhardinge * and calling a provided function on each leaf page table. 2329aee16b3cSJeremy Fitzhardinge */ 2330aee16b3cSJeremy Fitzhardinge int apply_to_page_range(struct mm_struct *mm, unsigned long addr, 2331aee16b3cSJeremy Fitzhardinge unsigned long size, pte_fn_t fn, void *data) 2332aee16b3cSJeremy Fitzhardinge { 2333aee16b3cSJeremy Fitzhardinge pgd_t *pgd; 2334aee16b3cSJeremy Fitzhardinge unsigned long next; 233557250a5bSJeremy Fitzhardinge unsigned long end = addr + size; 2336aee16b3cSJeremy Fitzhardinge int err; 2337aee16b3cSJeremy Fitzhardinge 2338aee16b3cSJeremy Fitzhardinge BUG_ON(addr >= end); 2339aee16b3cSJeremy Fitzhardinge pgd = pgd_offset(mm, addr); 2340aee16b3cSJeremy Fitzhardinge do { 2341aee16b3cSJeremy Fitzhardinge next = pgd_addr_end(addr, end); 2342aee16b3cSJeremy Fitzhardinge err = apply_to_pud_range(mm, pgd, addr, next, fn, data); 2343aee16b3cSJeremy Fitzhardinge if (err) 2344aee16b3cSJeremy Fitzhardinge break; 2345aee16b3cSJeremy Fitzhardinge } while (pgd++, addr = next, addr != end); 234657250a5bSJeremy Fitzhardinge 2347aee16b3cSJeremy Fitzhardinge return err; 2348aee16b3cSJeremy Fitzhardinge } 2349aee16b3cSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(apply_to_page_range); 2350aee16b3cSJeremy Fitzhardinge 23511da177e4SLinus Torvalds /* 23528f4e2101SHugh Dickins * handle_pte_fault chooses page fault handler according to an entry 23538f4e2101SHugh Dickins * which was read non-atomically. Before making any commitment, on 23548f4e2101SHugh Dickins * those architectures or configurations (e.g. i386 with PAE) which 2355a335b2e1SRyota Ozaki * might give a mix of unmatched parts, do_swap_page and do_nonlinear_fault 23568f4e2101SHugh Dickins * must check under lock before unmapping the pte and proceeding 23578f4e2101SHugh Dickins * (but do_wp_page is only called after already making such a check; 2358a335b2e1SRyota Ozaki * and do_anonymous_page can safely check later on). 23598f4e2101SHugh Dickins */ 23604c21e2f2SHugh Dickins static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd, 23618f4e2101SHugh Dickins pte_t *page_table, pte_t orig_pte) 23628f4e2101SHugh Dickins { 23638f4e2101SHugh Dickins int same = 1; 23648f4e2101SHugh Dickins #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT) 23658f4e2101SHugh Dickins if (sizeof(pte_t) > sizeof(unsigned long)) { 23664c21e2f2SHugh Dickins spinlock_t *ptl = pte_lockptr(mm, pmd); 23674c21e2f2SHugh Dickins spin_lock(ptl); 23688f4e2101SHugh Dickins same = pte_same(*page_table, orig_pte); 23694c21e2f2SHugh Dickins spin_unlock(ptl); 23708f4e2101SHugh Dickins } 23718f4e2101SHugh Dickins #endif 23728f4e2101SHugh Dickins pte_unmap(page_table); 23738f4e2101SHugh Dickins return same; 23748f4e2101SHugh Dickins } 23758f4e2101SHugh Dickins 23769de455b2SAtsushi Nemoto static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma) 23776aab341eSLinus Torvalds { 23786aab341eSLinus Torvalds /* 23796aab341eSLinus Torvalds * If the source page was a PFN mapping, we don't have 23806aab341eSLinus Torvalds * a "struct page" for it. We do a best-effort copy by 23816aab341eSLinus Torvalds * just copying from the original user address. If that 23826aab341eSLinus Torvalds * fails, we just zero-fill it. Live with it. 23836aab341eSLinus Torvalds */ 23846aab341eSLinus Torvalds if (unlikely(!src)) { 23856aab341eSLinus Torvalds void *kaddr = kmap_atomic(dst, KM_USER0); 23865d2a2dbbSLinus Torvalds void __user *uaddr = (void __user *)(va & PAGE_MASK); 23875d2a2dbbSLinus Torvalds 23885d2a2dbbSLinus Torvalds /* 23895d2a2dbbSLinus Torvalds * This really shouldn't fail, because the page is there 23905d2a2dbbSLinus Torvalds * in the page tables. But it might just be unreadable, 23915d2a2dbbSLinus Torvalds * in which case we just give up and fill the result with 23925d2a2dbbSLinus Torvalds * zeroes. 23935d2a2dbbSLinus Torvalds */ 23945d2a2dbbSLinus Torvalds if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) 23953ecb01dfSJan Beulich clear_page(kaddr); 23966aab341eSLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 2397c4ec7b0dSDmitriy Monakhov flush_dcache_page(dst); 23980ed361deSNick Piggin } else 23999de455b2SAtsushi Nemoto copy_user_highpage(dst, src, va, vma); 24006aab341eSLinus Torvalds } 24016aab341eSLinus Torvalds 24021da177e4SLinus Torvalds /* 24031da177e4SLinus Torvalds * This routine handles present pages, when users try to write 24041da177e4SLinus Torvalds * to a shared page. It is done by copying the page to a new address 24051da177e4SLinus Torvalds * and decrementing the shared-page counter for the old page. 24061da177e4SLinus Torvalds * 24071da177e4SLinus Torvalds * Note that this routine assumes that the protection checks have been 24081da177e4SLinus Torvalds * done by the caller (the low-level page fault routine in most cases). 24091da177e4SLinus Torvalds * Thus we can safely just mark it writable once we've done any necessary 24101da177e4SLinus Torvalds * COW. 24111da177e4SLinus Torvalds * 24121da177e4SLinus Torvalds * We also mark the page dirty at this point even though the page will 24131da177e4SLinus Torvalds * change only once the write actually happens. This avoids a few races, 24141da177e4SLinus Torvalds * and potentially makes it more efficient. 24151da177e4SLinus Torvalds * 24168f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 24178f4e2101SHugh Dickins * but allow concurrent faults), with pte both mapped and locked. 24188f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 24191da177e4SLinus Torvalds */ 24201da177e4SLinus Torvalds static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma, 242165500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 24228f4e2101SHugh Dickins spinlock_t *ptl, pte_t orig_pte) 2423e6219ec8SNamhyung Kim __releases(ptl) 24241da177e4SLinus Torvalds { 2425e5bbe4dfSHugh Dickins struct page *old_page, *new_page; 24261da177e4SLinus Torvalds pte_t entry; 2427b009c024SMichel Lespinasse int ret = 0; 2428a200ee18SPeter Zijlstra int page_mkwrite = 0; 2429d08b3851SPeter Zijlstra struct page *dirty_page = NULL; 24301da177e4SLinus Torvalds 24316aab341eSLinus Torvalds old_page = vm_normal_page(vma, address, orig_pte); 2432251b97f5SPeter Zijlstra if (!old_page) { 2433251b97f5SPeter Zijlstra /* 2434251b97f5SPeter Zijlstra * VM_MIXEDMAP !pfn_valid() case 2435251b97f5SPeter Zijlstra * 2436251b97f5SPeter Zijlstra * We should not cow pages in a shared writeable mapping. 2437251b97f5SPeter Zijlstra * Just mark the pages writable as we can't do any dirty 2438251b97f5SPeter Zijlstra * accounting on raw pfn maps. 2439251b97f5SPeter Zijlstra */ 2440251b97f5SPeter Zijlstra if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 2441251b97f5SPeter Zijlstra (VM_WRITE|VM_SHARED)) 2442251b97f5SPeter Zijlstra goto reuse; 2443920fc356SHugh Dickins goto gotten; 2444251b97f5SPeter Zijlstra } 24451da177e4SLinus Torvalds 2446d08b3851SPeter Zijlstra /* 2447ee6a6457SPeter Zijlstra * Take out anonymous pages first, anonymous shared vmas are 2448ee6a6457SPeter Zijlstra * not dirty accountable. 2449d08b3851SPeter Zijlstra */ 24509a840895SHugh Dickins if (PageAnon(old_page) && !PageKsm(old_page)) { 2451ab967d86SHugh Dickins if (!trylock_page(old_page)) { 2452ab967d86SHugh Dickins page_cache_get(old_page); 2453ab967d86SHugh Dickins pte_unmap_unlock(page_table, ptl); 2454ab967d86SHugh Dickins lock_page(old_page); 2455ab967d86SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, 2456ab967d86SHugh Dickins &ptl); 2457ab967d86SHugh Dickins if (!pte_same(*page_table, orig_pte)) { 2458ab967d86SHugh Dickins unlock_page(old_page); 2459ab967d86SHugh Dickins goto unlock; 2460ab967d86SHugh Dickins } 2461ab967d86SHugh Dickins page_cache_release(old_page); 2462ab967d86SHugh Dickins } 2463b009c024SMichel Lespinasse if (reuse_swap_page(old_page)) { 2464c44b6743SRik van Riel /* 2465c44b6743SRik van Riel * The page is all ours. Move it to our anon_vma so 2466c44b6743SRik van Riel * the rmap code will not search our parent or siblings. 2467c44b6743SRik van Riel * Protected against the rmap code by the page lock. 2468c44b6743SRik van Riel */ 2469c44b6743SRik van Riel page_move_anon_rmap(old_page, vma, address); 2470ee6a6457SPeter Zijlstra unlock_page(old_page); 2471b009c024SMichel Lespinasse goto reuse; 2472b009c024SMichel Lespinasse } 2473b009c024SMichel Lespinasse unlock_page(old_page); 2474ee6a6457SPeter Zijlstra } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 2475d08b3851SPeter Zijlstra (VM_WRITE|VM_SHARED))) { 2476ee6a6457SPeter Zijlstra /* 2477ee6a6457SPeter Zijlstra * Only catch write-faults on shared writable pages, 2478ee6a6457SPeter Zijlstra * read-only shared pages can get COWed by 2479ee6a6457SPeter Zijlstra * get_user_pages(.write=1, .force=1). 2480ee6a6457SPeter Zijlstra */ 24819637a5efSDavid Howells if (vma->vm_ops && vma->vm_ops->page_mkwrite) { 2482c2ec175cSNick Piggin struct vm_fault vmf; 2483c2ec175cSNick Piggin int tmp; 2484c2ec175cSNick Piggin 2485c2ec175cSNick Piggin vmf.virtual_address = (void __user *)(address & 2486c2ec175cSNick Piggin PAGE_MASK); 2487c2ec175cSNick Piggin vmf.pgoff = old_page->index; 2488c2ec175cSNick Piggin vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE; 2489c2ec175cSNick Piggin vmf.page = old_page; 2490c2ec175cSNick Piggin 24919637a5efSDavid Howells /* 24929637a5efSDavid Howells * Notify the address space that the page is about to 24939637a5efSDavid Howells * become writable so that it can prohibit this or wait 24949637a5efSDavid Howells * for the page to get into an appropriate state. 24959637a5efSDavid Howells * 24969637a5efSDavid Howells * We do this without the lock held, so that it can 24979637a5efSDavid Howells * sleep if it needs to. 24989637a5efSDavid Howells */ 24999637a5efSDavid Howells page_cache_get(old_page); 25009637a5efSDavid Howells pte_unmap_unlock(page_table, ptl); 25019637a5efSDavid Howells 2502c2ec175cSNick Piggin tmp = vma->vm_ops->page_mkwrite(vma, &vmf); 2503c2ec175cSNick Piggin if (unlikely(tmp & 2504c2ec175cSNick Piggin (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) { 2505c2ec175cSNick Piggin ret = tmp; 25069637a5efSDavid Howells goto unwritable_page; 2507c2ec175cSNick Piggin } 2508b827e496SNick Piggin if (unlikely(!(tmp & VM_FAULT_LOCKED))) { 2509b827e496SNick Piggin lock_page(old_page); 2510b827e496SNick Piggin if (!old_page->mapping) { 2511b827e496SNick Piggin ret = 0; /* retry the fault */ 2512b827e496SNick Piggin unlock_page(old_page); 2513b827e496SNick Piggin goto unwritable_page; 2514b827e496SNick Piggin } 2515b827e496SNick Piggin } else 2516b827e496SNick Piggin VM_BUG_ON(!PageLocked(old_page)); 25179637a5efSDavid Howells 25189637a5efSDavid Howells /* 25199637a5efSDavid Howells * Since we dropped the lock we need to revalidate 25209637a5efSDavid Howells * the PTE as someone else may have changed it. If 25219637a5efSDavid Howells * they did, we just return, as we can count on the 25229637a5efSDavid Howells * MMU to tell us if they didn't also make it writable. 25239637a5efSDavid Howells */ 25249637a5efSDavid Howells page_table = pte_offset_map_lock(mm, pmd, address, 25259637a5efSDavid Howells &ptl); 2526b827e496SNick Piggin if (!pte_same(*page_table, orig_pte)) { 2527b827e496SNick Piggin unlock_page(old_page); 25289637a5efSDavid Howells goto unlock; 2529b827e496SNick Piggin } 2530a200ee18SPeter Zijlstra 2531a200ee18SPeter Zijlstra page_mkwrite = 1; 25329637a5efSDavid Howells } 2533d08b3851SPeter Zijlstra dirty_page = old_page; 2534d08b3851SPeter Zijlstra get_page(dirty_page); 25359637a5efSDavid Howells 2536251b97f5SPeter Zijlstra reuse: 2537eca35133SBen Collins flush_cache_page(vma, address, pte_pfn(orig_pte)); 253865500d23SHugh Dickins entry = pte_mkyoung(orig_pte); 253965500d23SHugh Dickins entry = maybe_mkwrite(pte_mkdirty(entry), vma); 2540954ffcb3SKAMEZAWA Hiroyuki if (ptep_set_access_flags(vma, address, page_table, entry,1)) 25414b3073e1SRussell King update_mmu_cache(vma, address, page_table); 254272ddc8f7SMichel Lespinasse pte_unmap_unlock(page_table, ptl); 254365500d23SHugh Dickins ret |= VM_FAULT_WRITE; 254472ddc8f7SMichel Lespinasse 254572ddc8f7SMichel Lespinasse if (!dirty_page) 254672ddc8f7SMichel Lespinasse return ret; 254772ddc8f7SMichel Lespinasse 254872ddc8f7SMichel Lespinasse /* 254972ddc8f7SMichel Lespinasse * Yes, Virginia, this is actually required to prevent a race 255072ddc8f7SMichel Lespinasse * with clear_page_dirty_for_io() from clearing the page dirty 255172ddc8f7SMichel Lespinasse * bit after it clear all dirty ptes, but before a racing 255272ddc8f7SMichel Lespinasse * do_wp_page installs a dirty pte. 255372ddc8f7SMichel Lespinasse * 2554a335b2e1SRyota Ozaki * __do_fault is protected similarly. 255572ddc8f7SMichel Lespinasse */ 255672ddc8f7SMichel Lespinasse if (!page_mkwrite) { 255772ddc8f7SMichel Lespinasse wait_on_page_locked(dirty_page); 255872ddc8f7SMichel Lespinasse set_page_dirty_balance(dirty_page, page_mkwrite); 255972ddc8f7SMichel Lespinasse } 256072ddc8f7SMichel Lespinasse put_page(dirty_page); 256172ddc8f7SMichel Lespinasse if (page_mkwrite) { 256272ddc8f7SMichel Lespinasse struct address_space *mapping = dirty_page->mapping; 256372ddc8f7SMichel Lespinasse 256472ddc8f7SMichel Lespinasse set_page_dirty(dirty_page); 256572ddc8f7SMichel Lespinasse unlock_page(dirty_page); 256672ddc8f7SMichel Lespinasse page_cache_release(dirty_page); 256772ddc8f7SMichel Lespinasse if (mapping) { 256872ddc8f7SMichel Lespinasse /* 256972ddc8f7SMichel Lespinasse * Some device drivers do not set page.mapping 257072ddc8f7SMichel Lespinasse * but still dirty their pages 257172ddc8f7SMichel Lespinasse */ 257272ddc8f7SMichel Lespinasse balance_dirty_pages_ratelimited(mapping); 257372ddc8f7SMichel Lespinasse } 257472ddc8f7SMichel Lespinasse } 257572ddc8f7SMichel Lespinasse 257672ddc8f7SMichel Lespinasse /* file_update_time outside page_lock */ 257772ddc8f7SMichel Lespinasse if (vma->vm_file) 257872ddc8f7SMichel Lespinasse file_update_time(vma->vm_file); 257972ddc8f7SMichel Lespinasse 258072ddc8f7SMichel Lespinasse return ret; 25811da177e4SLinus Torvalds } 25821da177e4SLinus Torvalds 25831da177e4SLinus Torvalds /* 25841da177e4SLinus Torvalds * Ok, we need to copy. Oh, well.. 25851da177e4SLinus Torvalds */ 25861da177e4SLinus Torvalds page_cache_get(old_page); 2587920fc356SHugh Dickins gotten: 25888f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 25891da177e4SLinus Torvalds 25901da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 259165500d23SHugh Dickins goto oom; 2592a13ea5b7SHugh Dickins 259362eede62SHugh Dickins if (is_zero_pfn(pte_pfn(orig_pte))) { 2594a13ea5b7SHugh Dickins new_page = alloc_zeroed_user_highpage_movable(vma, address); 2595a13ea5b7SHugh Dickins if (!new_page) 2596a13ea5b7SHugh Dickins goto oom; 2597a13ea5b7SHugh Dickins } else { 2598769848c0SMel Gorman new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address); 25991da177e4SLinus Torvalds if (!new_page) 260065500d23SHugh Dickins goto oom; 2601a13ea5b7SHugh Dickins cow_user_page(new_page, old_page, address, vma); 2602a13ea5b7SHugh Dickins } 2603a13ea5b7SHugh Dickins __SetPageUptodate(new_page); 2604a13ea5b7SHugh Dickins 26052c26fdd7SKAMEZAWA Hiroyuki if (mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL)) 26068a9f3ccdSBalbir Singh goto oom_free_new; 26078a9f3ccdSBalbir Singh 26081da177e4SLinus Torvalds /* 26091da177e4SLinus Torvalds * Re-check the pte - we dropped the lock 26101da177e4SLinus Torvalds */ 26118f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 261265500d23SHugh Dickins if (likely(pte_same(*page_table, orig_pte))) { 2613920fc356SHugh Dickins if (old_page) { 26144294621fSHugh Dickins if (!PageAnon(old_page)) { 261534e55232SKAMEZAWA Hiroyuki dec_mm_counter_fast(mm, MM_FILEPAGES); 261634e55232SKAMEZAWA Hiroyuki inc_mm_counter_fast(mm, MM_ANONPAGES); 26174294621fSHugh Dickins } 2618920fc356SHugh Dickins } else 261934e55232SKAMEZAWA Hiroyuki inc_mm_counter_fast(mm, MM_ANONPAGES); 2620eca35133SBen Collins flush_cache_page(vma, address, pte_pfn(orig_pte)); 262165500d23SHugh Dickins entry = mk_pte(new_page, vma->vm_page_prot); 262265500d23SHugh Dickins entry = maybe_mkwrite(pte_mkdirty(entry), vma); 26234ce072f1SSiddha, Suresh B /* 26244ce072f1SSiddha, Suresh B * Clear the pte entry and flush it first, before updating the 26254ce072f1SSiddha, Suresh B * pte with the new entry. This will avoid a race condition 26264ce072f1SSiddha, Suresh B * seen in the presence of one thread doing SMC and another 26274ce072f1SSiddha, Suresh B * thread doing COW. 26284ce072f1SSiddha, Suresh B */ 2629828502d3SIzik Eidus ptep_clear_flush(vma, address, page_table); 26309617d95eSNick Piggin page_add_new_anon_rmap(new_page, vma, address); 2631828502d3SIzik Eidus /* 2632828502d3SIzik Eidus * We call the notify macro here because, when using secondary 2633828502d3SIzik Eidus * mmu page tables (such as kvm shadow page tables), we want the 2634828502d3SIzik Eidus * new page to be mapped directly into the secondary page table. 2635828502d3SIzik Eidus */ 2636828502d3SIzik Eidus set_pte_at_notify(mm, address, page_table, entry); 26374b3073e1SRussell King update_mmu_cache(vma, address, page_table); 2638945754a1SNick Piggin if (old_page) { 2639945754a1SNick Piggin /* 2640945754a1SNick Piggin * Only after switching the pte to the new page may 2641945754a1SNick Piggin * we remove the mapcount here. Otherwise another 2642945754a1SNick Piggin * process may come and find the rmap count decremented 2643945754a1SNick Piggin * before the pte is switched to the new page, and 2644945754a1SNick Piggin * "reuse" the old page writing into it while our pte 2645945754a1SNick Piggin * here still points into it and can be read by other 2646945754a1SNick Piggin * threads. 2647945754a1SNick Piggin * 2648945754a1SNick Piggin * The critical issue is to order this 2649945754a1SNick Piggin * page_remove_rmap with the ptp_clear_flush above. 2650945754a1SNick Piggin * Those stores are ordered by (if nothing else,) 2651945754a1SNick Piggin * the barrier present in the atomic_add_negative 2652945754a1SNick Piggin * in page_remove_rmap. 2653945754a1SNick Piggin * 2654945754a1SNick Piggin * Then the TLB flush in ptep_clear_flush ensures that 2655945754a1SNick Piggin * no process can access the old page before the 2656945754a1SNick Piggin * decremented mapcount is visible. And the old page 2657945754a1SNick Piggin * cannot be reused until after the decremented 2658945754a1SNick Piggin * mapcount is visible. So transitively, TLBs to 2659945754a1SNick Piggin * old page will be flushed before it can be reused. 2660945754a1SNick Piggin */ 2661edc315fdSHugh Dickins page_remove_rmap(old_page); 2662945754a1SNick Piggin } 2663945754a1SNick Piggin 26641da177e4SLinus Torvalds /* Free the old page.. */ 26651da177e4SLinus Torvalds new_page = old_page; 2666f33ea7f4SNick Piggin ret |= VM_FAULT_WRITE; 26678a9f3ccdSBalbir Singh } else 26688a9f3ccdSBalbir Singh mem_cgroup_uncharge_page(new_page); 26698a9f3ccdSBalbir Singh 2670920fc356SHugh Dickins if (new_page) 26711da177e4SLinus Torvalds page_cache_release(new_page); 267265500d23SHugh Dickins unlock: 26738f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 2674e15f8c01SMichel Lespinasse if (old_page) { 2675e15f8c01SMichel Lespinasse /* 2676e15f8c01SMichel Lespinasse * Don't let another task, with possibly unlocked vma, 2677e15f8c01SMichel Lespinasse * keep the mlocked page. 2678e15f8c01SMichel Lespinasse */ 2679e15f8c01SMichel Lespinasse if ((ret & VM_FAULT_WRITE) && (vma->vm_flags & VM_LOCKED)) { 2680e15f8c01SMichel Lespinasse lock_page(old_page); /* LRU manipulation */ 2681e15f8c01SMichel Lespinasse munlock_vma_page(old_page); 2682e15f8c01SMichel Lespinasse unlock_page(old_page); 2683e15f8c01SMichel Lespinasse } 2684e15f8c01SMichel Lespinasse page_cache_release(old_page); 2685e15f8c01SMichel Lespinasse } 2686f33ea7f4SNick Piggin return ret; 26878a9f3ccdSBalbir Singh oom_free_new: 26886dbf6d3bSHugh Dickins page_cache_release(new_page); 268965500d23SHugh Dickins oom: 2690b827e496SNick Piggin if (old_page) { 2691b827e496SNick Piggin if (page_mkwrite) { 2692b827e496SNick Piggin unlock_page(old_page); 26931da177e4SLinus Torvalds page_cache_release(old_page); 2694b827e496SNick Piggin } 2695b827e496SNick Piggin page_cache_release(old_page); 2696b827e496SNick Piggin } 26971da177e4SLinus Torvalds return VM_FAULT_OOM; 26989637a5efSDavid Howells 26999637a5efSDavid Howells unwritable_page: 27009637a5efSDavid Howells page_cache_release(old_page); 2701c2ec175cSNick Piggin return ret; 27021da177e4SLinus Torvalds } 27031da177e4SLinus Torvalds 270497a89413SPeter Zijlstra static void unmap_mapping_range_vma(struct vm_area_struct *vma, 27051da177e4SLinus Torvalds unsigned long start_addr, unsigned long end_addr, 27061da177e4SLinus Torvalds struct zap_details *details) 27071da177e4SLinus Torvalds { 270897a89413SPeter Zijlstra zap_page_range(vma, start_addr, end_addr - start_addr, details); 27091da177e4SLinus Torvalds } 27101da177e4SLinus Torvalds 27111da177e4SLinus Torvalds static inline void unmap_mapping_range_tree(struct prio_tree_root *root, 27121da177e4SLinus Torvalds struct zap_details *details) 27131da177e4SLinus Torvalds { 27141da177e4SLinus Torvalds struct vm_area_struct *vma; 27151da177e4SLinus Torvalds struct prio_tree_iter iter; 27161da177e4SLinus Torvalds pgoff_t vba, vea, zba, zea; 27171da177e4SLinus Torvalds 27181da177e4SLinus Torvalds vma_prio_tree_foreach(vma, &iter, root, 27191da177e4SLinus Torvalds details->first_index, details->last_index) { 27201da177e4SLinus Torvalds 27211da177e4SLinus Torvalds vba = vma->vm_pgoff; 27221da177e4SLinus Torvalds vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1; 27231da177e4SLinus Torvalds /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */ 27241da177e4SLinus Torvalds zba = details->first_index; 27251da177e4SLinus Torvalds if (zba < vba) 27261da177e4SLinus Torvalds zba = vba; 27271da177e4SLinus Torvalds zea = details->last_index; 27281da177e4SLinus Torvalds if (zea > vea) 27291da177e4SLinus Torvalds zea = vea; 27301da177e4SLinus Torvalds 273197a89413SPeter Zijlstra unmap_mapping_range_vma(vma, 27321da177e4SLinus Torvalds ((zba - vba) << PAGE_SHIFT) + vma->vm_start, 27331da177e4SLinus Torvalds ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start, 273497a89413SPeter Zijlstra details); 27351da177e4SLinus Torvalds } 27361da177e4SLinus Torvalds } 27371da177e4SLinus Torvalds 27381da177e4SLinus Torvalds static inline void unmap_mapping_range_list(struct list_head *head, 27391da177e4SLinus Torvalds struct zap_details *details) 27401da177e4SLinus Torvalds { 27411da177e4SLinus Torvalds struct vm_area_struct *vma; 27421da177e4SLinus Torvalds 27431da177e4SLinus Torvalds /* 27441da177e4SLinus Torvalds * In nonlinear VMAs there is no correspondence between virtual address 27451da177e4SLinus Torvalds * offset and file offset. So we must perform an exhaustive search 27461da177e4SLinus Torvalds * across *all* the pages in each nonlinear VMA, not just the pages 27471da177e4SLinus Torvalds * whose virtual address lies outside the file truncation point. 27481da177e4SLinus Torvalds */ 27491da177e4SLinus Torvalds list_for_each_entry(vma, head, shared.vm_set.list) { 27501da177e4SLinus Torvalds details->nonlinear_vma = vma; 275197a89413SPeter Zijlstra unmap_mapping_range_vma(vma, vma->vm_start, vma->vm_end, details); 27521da177e4SLinus Torvalds } 27531da177e4SLinus Torvalds } 27541da177e4SLinus Torvalds 27551da177e4SLinus Torvalds /** 275672fd4a35SRobert 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. 27573d41088fSMartin Waitz * @mapping: the address space containing mmaps to be unmapped. 27581da177e4SLinus Torvalds * @holebegin: byte in first page to unmap, relative to the start of 27591da177e4SLinus Torvalds * the underlying file. This will be rounded down to a PAGE_SIZE 276025d9e2d1Snpiggin@suse.de * boundary. Note that this is different from truncate_pagecache(), which 27611da177e4SLinus Torvalds * must keep the partial page. In contrast, we must get rid of 27621da177e4SLinus Torvalds * partial pages. 27631da177e4SLinus Torvalds * @holelen: size of prospective hole in bytes. This will be rounded 27641da177e4SLinus Torvalds * up to a PAGE_SIZE boundary. A holelen of zero truncates to the 27651da177e4SLinus Torvalds * end of the file. 27661da177e4SLinus Torvalds * @even_cows: 1 when truncating a file, unmap even private COWed pages; 27671da177e4SLinus Torvalds * but 0 when invalidating pagecache, don't throw away private data. 27681da177e4SLinus Torvalds */ 27691da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping, 27701da177e4SLinus Torvalds loff_t const holebegin, loff_t const holelen, int even_cows) 27711da177e4SLinus Torvalds { 27721da177e4SLinus Torvalds struct zap_details details; 27731da177e4SLinus Torvalds pgoff_t hba = holebegin >> PAGE_SHIFT; 27741da177e4SLinus Torvalds pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 27751da177e4SLinus Torvalds 27761da177e4SLinus Torvalds /* Check for overflow. */ 27771da177e4SLinus Torvalds if (sizeof(holelen) > sizeof(hlen)) { 27781da177e4SLinus Torvalds long long holeend = 27791da177e4SLinus Torvalds (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 27801da177e4SLinus Torvalds if (holeend & ~(long long)ULONG_MAX) 27811da177e4SLinus Torvalds hlen = ULONG_MAX - hba + 1; 27821da177e4SLinus Torvalds } 27831da177e4SLinus Torvalds 27841da177e4SLinus Torvalds details.check_mapping = even_cows? NULL: mapping; 27851da177e4SLinus Torvalds details.nonlinear_vma = NULL; 27861da177e4SLinus Torvalds details.first_index = hba; 27871da177e4SLinus Torvalds details.last_index = hba + hlen - 1; 27881da177e4SLinus Torvalds if (details.last_index < details.first_index) 27891da177e4SLinus Torvalds details.last_index = ULONG_MAX; 27901da177e4SLinus Torvalds 279197a89413SPeter Zijlstra 27923d48ae45SPeter Zijlstra mutex_lock(&mapping->i_mmap_mutex); 27931da177e4SLinus Torvalds if (unlikely(!prio_tree_empty(&mapping->i_mmap))) 27941da177e4SLinus Torvalds unmap_mapping_range_tree(&mapping->i_mmap, &details); 27951da177e4SLinus Torvalds if (unlikely(!list_empty(&mapping->i_mmap_nonlinear))) 27961da177e4SLinus Torvalds unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details); 27973d48ae45SPeter Zijlstra mutex_unlock(&mapping->i_mmap_mutex); 27981da177e4SLinus Torvalds } 27991da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range); 28001da177e4SLinus Torvalds 2801f6b3ec23SBadari Pulavarty int vmtruncate_range(struct inode *inode, loff_t offset, loff_t end) 2802f6b3ec23SBadari Pulavarty { 2803f6b3ec23SBadari Pulavarty struct address_space *mapping = inode->i_mapping; 2804f6b3ec23SBadari Pulavarty 2805f6b3ec23SBadari Pulavarty /* 2806f6b3ec23SBadari Pulavarty * If the underlying filesystem is not going to provide 2807f6b3ec23SBadari Pulavarty * a way to truncate a range of blocks (punch a hole) - 2808f6b3ec23SBadari Pulavarty * we should return failure right now. 2809f6b3ec23SBadari Pulavarty */ 2810acfa4380SAl Viro if (!inode->i_op->truncate_range) 2811f6b3ec23SBadari Pulavarty return -ENOSYS; 2812f6b3ec23SBadari Pulavarty 28131b1dcc1bSJes Sorensen mutex_lock(&inode->i_mutex); 2814f6b3ec23SBadari Pulavarty down_write(&inode->i_alloc_sem); 2815f6b3ec23SBadari Pulavarty unmap_mapping_range(mapping, offset, (end - offset), 1); 2816f6b3ec23SBadari Pulavarty truncate_inode_pages_range(mapping, offset, end); 2817d00806b1SNick Piggin unmap_mapping_range(mapping, offset, (end - offset), 1); 2818f6b3ec23SBadari Pulavarty inode->i_op->truncate_range(inode, offset, end); 2819f6b3ec23SBadari Pulavarty up_write(&inode->i_alloc_sem); 28201b1dcc1bSJes Sorensen mutex_unlock(&inode->i_mutex); 2821f6b3ec23SBadari Pulavarty 2822f6b3ec23SBadari Pulavarty return 0; 2823f6b3ec23SBadari Pulavarty } 2824f6b3ec23SBadari Pulavarty 28251da177e4SLinus Torvalds /* 28268f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 28278f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 28288f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 28291da177e4SLinus Torvalds */ 283065500d23SHugh Dickins static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma, 283165500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 283230c9f3a9SLinus Torvalds unsigned int flags, pte_t orig_pte) 28331da177e4SLinus Torvalds { 28348f4e2101SHugh Dickins spinlock_t *ptl; 28354969c119SAndrea Arcangeli struct page *page, *swapcache = NULL; 283665500d23SHugh Dickins swp_entry_t entry; 28371da177e4SLinus Torvalds pte_t pte; 2838d065bd81SMichel Lespinasse int locked; 283956039efaSKAMEZAWA Hiroyuki struct mem_cgroup *ptr; 2840ad8c2ee8SRik van Riel int exclusive = 0; 284183c54070SNick Piggin int ret = 0; 28421da177e4SLinus Torvalds 28434c21e2f2SHugh Dickins if (!pte_unmap_same(mm, pmd, page_table, orig_pte)) 28448f4e2101SHugh Dickins goto out; 284565500d23SHugh Dickins 284665500d23SHugh Dickins entry = pte_to_swp_entry(orig_pte); 2847d1737fdbSAndi Kleen if (unlikely(non_swap_entry(entry))) { 28480697212aSChristoph Lameter if (is_migration_entry(entry)) { 28490697212aSChristoph Lameter migration_entry_wait(mm, pmd, address); 2850d1737fdbSAndi Kleen } else if (is_hwpoison_entry(entry)) { 2851d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 2852d1737fdbSAndi Kleen } else { 2853d1737fdbSAndi Kleen print_bad_pte(vma, address, orig_pte, NULL); 2854d99be1a8SHugh Dickins ret = VM_FAULT_SIGBUS; 2855d1737fdbSAndi Kleen } 28560697212aSChristoph Lameter goto out; 28570697212aSChristoph Lameter } 28580ff92245SShailabh Nagar delayacct_set_flag(DELAYACCT_PF_SWAPIN); 28591da177e4SLinus Torvalds page = lookup_swap_cache(entry); 28601da177e4SLinus Torvalds if (!page) { 2861a5c9b696SHugh Dickins grab_swap_token(mm); /* Contend for token _before_ read-in */ 286202098feaSHugh Dickins page = swapin_readahead(entry, 286302098feaSHugh Dickins GFP_HIGHUSER_MOVABLE, vma, address); 28641da177e4SLinus Torvalds if (!page) { 28651da177e4SLinus Torvalds /* 28668f4e2101SHugh Dickins * Back out if somebody else faulted in this pte 28678f4e2101SHugh Dickins * while we released the pte lock. 28681da177e4SLinus Torvalds */ 28698f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 28701da177e4SLinus Torvalds if (likely(pte_same(*page_table, orig_pte))) 28711da177e4SLinus Torvalds ret = VM_FAULT_OOM; 28720ff92245SShailabh Nagar delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 287365500d23SHugh Dickins goto unlock; 28741da177e4SLinus Torvalds } 28751da177e4SLinus Torvalds 28761da177e4SLinus Torvalds /* Had to read the page from swap area: Major fault */ 28771da177e4SLinus Torvalds ret = VM_FAULT_MAJOR; 2878f8891e5eSChristoph Lameter count_vm_event(PGMAJFAULT); 2879456f998eSYing Han mem_cgroup_count_vm_event(mm, PGMAJFAULT); 2880d1737fdbSAndi Kleen } else if (PageHWPoison(page)) { 288171f72525SWu Fengguang /* 288271f72525SWu Fengguang * hwpoisoned dirty swapcache pages are kept for killing 288371f72525SWu Fengguang * owner processes (which may be unknown at hwpoison time) 288471f72525SWu Fengguang */ 2885d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 2886d1737fdbSAndi Kleen delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 28874779cb31SAndi Kleen goto out_release; 28881da177e4SLinus Torvalds } 28891da177e4SLinus Torvalds 2890d065bd81SMichel Lespinasse locked = lock_page_or_retry(page, mm, flags); 289120a1022dSBalbir Singh delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 2892d065bd81SMichel Lespinasse if (!locked) { 2893d065bd81SMichel Lespinasse ret |= VM_FAULT_RETRY; 2894d065bd81SMichel Lespinasse goto out_release; 2895d065bd81SMichel Lespinasse } 28961da177e4SLinus Torvalds 28974969c119SAndrea Arcangeli /* 289831c4a3d3SHugh Dickins * Make sure try_to_free_swap or reuse_swap_page or swapoff did not 289931c4a3d3SHugh Dickins * release the swapcache from under us. The page pin, and pte_same 290031c4a3d3SHugh Dickins * test below, are not enough to exclude that. Even if it is still 290131c4a3d3SHugh Dickins * swapcache, we need to check that the page's swap has not changed. 29024969c119SAndrea Arcangeli */ 290331c4a3d3SHugh Dickins if (unlikely(!PageSwapCache(page) || page_private(page) != entry.val)) 29044969c119SAndrea Arcangeli goto out_page; 29054969c119SAndrea Arcangeli 29064969c119SAndrea Arcangeli if (ksm_might_need_to_copy(page, vma, address)) { 29074969c119SAndrea Arcangeli swapcache = page; 29084969c119SAndrea Arcangeli page = ksm_does_need_to_copy(page, vma, address); 29094969c119SAndrea Arcangeli 29104969c119SAndrea Arcangeli if (unlikely(!page)) { 29115ad64688SHugh Dickins ret = VM_FAULT_OOM; 29124969c119SAndrea Arcangeli page = swapcache; 29134969c119SAndrea Arcangeli swapcache = NULL; 29144969c119SAndrea Arcangeli goto out_page; 29154969c119SAndrea Arcangeli } 29165ad64688SHugh Dickins } 29175ad64688SHugh Dickins 29182c26fdd7SKAMEZAWA Hiroyuki if (mem_cgroup_try_charge_swapin(mm, page, GFP_KERNEL, &ptr)) { 2919073e587eSKAMEZAWA Hiroyuki ret = VM_FAULT_OOM; 2920bc43f75cSJohannes Weiner goto out_page; 2921073e587eSKAMEZAWA Hiroyuki } 2922073e587eSKAMEZAWA Hiroyuki 29231da177e4SLinus Torvalds /* 29248f4e2101SHugh Dickins * Back out if somebody else already faulted in this pte. 29251da177e4SLinus Torvalds */ 29268f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 29279e9bef07SHugh Dickins if (unlikely(!pte_same(*page_table, orig_pte))) 2928b8107480SKirill Korotaev goto out_nomap; 2929b8107480SKirill Korotaev 2930b8107480SKirill Korotaev if (unlikely(!PageUptodate(page))) { 2931b8107480SKirill Korotaev ret = VM_FAULT_SIGBUS; 2932b8107480SKirill Korotaev goto out_nomap; 29331da177e4SLinus Torvalds } 29341da177e4SLinus Torvalds 29358c7c6e34SKAMEZAWA Hiroyuki /* 29368c7c6e34SKAMEZAWA Hiroyuki * The page isn't present yet, go ahead with the fault. 29378c7c6e34SKAMEZAWA Hiroyuki * 29388c7c6e34SKAMEZAWA Hiroyuki * Be careful about the sequence of operations here. 29398c7c6e34SKAMEZAWA Hiroyuki * To get its accounting right, reuse_swap_page() must be called 29408c7c6e34SKAMEZAWA Hiroyuki * while the page is counted on swap but not yet in mapcount i.e. 29418c7c6e34SKAMEZAWA Hiroyuki * before page_add_anon_rmap() and swap_free(); try_to_free_swap() 29428c7c6e34SKAMEZAWA Hiroyuki * must be called after the swap_free(), or it will never succeed. 294303f3c433SKAMEZAWA Hiroyuki * Because delete_from_swap_page() may be called by reuse_swap_page(), 294403f3c433SKAMEZAWA Hiroyuki * mem_cgroup_commit_charge_swapin() may not be able to find swp_entry 294503f3c433SKAMEZAWA Hiroyuki * in page->private. In this case, a record in swap_cgroup is silently 294603f3c433SKAMEZAWA Hiroyuki * discarded at swap_free(). 29478c7c6e34SKAMEZAWA Hiroyuki */ 29481da177e4SLinus Torvalds 294934e55232SKAMEZAWA Hiroyuki inc_mm_counter_fast(mm, MM_ANONPAGES); 2950b084d435SKAMEZAWA Hiroyuki dec_mm_counter_fast(mm, MM_SWAPENTS); 29511da177e4SLinus Torvalds pte = mk_pte(page, vma->vm_page_prot); 295230c9f3a9SLinus Torvalds if ((flags & FAULT_FLAG_WRITE) && reuse_swap_page(page)) { 29531da177e4SLinus Torvalds pte = maybe_mkwrite(pte_mkdirty(pte), vma); 295430c9f3a9SLinus Torvalds flags &= ~FAULT_FLAG_WRITE; 29559a5b489bSAndrea Arcangeli ret |= VM_FAULT_WRITE; 2956ad8c2ee8SRik van Riel exclusive = 1; 29571da177e4SLinus Torvalds } 29581da177e4SLinus Torvalds flush_icache_page(vma, page); 29591da177e4SLinus Torvalds set_pte_at(mm, address, page_table, pte); 2960ad8c2ee8SRik van Riel do_page_add_anon_rmap(page, vma, address, exclusive); 296103f3c433SKAMEZAWA Hiroyuki /* It's better to call commit-charge after rmap is established */ 296203f3c433SKAMEZAWA Hiroyuki mem_cgroup_commit_charge_swapin(page, ptr); 29631da177e4SLinus Torvalds 2964c475a8abSHugh Dickins swap_free(entry); 2965b291f000SNick Piggin if (vm_swap_full() || (vma->vm_flags & VM_LOCKED) || PageMlocked(page)) 2966a2c43eedSHugh Dickins try_to_free_swap(page); 2967c475a8abSHugh Dickins unlock_page(page); 29684969c119SAndrea Arcangeli if (swapcache) { 29694969c119SAndrea Arcangeli /* 29704969c119SAndrea Arcangeli * Hold the lock to avoid the swap entry to be reused 29714969c119SAndrea Arcangeli * until we take the PT lock for the pte_same() check 29724969c119SAndrea Arcangeli * (to avoid false positives from pte_same). For 29734969c119SAndrea Arcangeli * further safety release the lock after the swap_free 29744969c119SAndrea Arcangeli * so that the swap count won't change under a 29754969c119SAndrea Arcangeli * parallel locked swapcache. 29764969c119SAndrea Arcangeli */ 29774969c119SAndrea Arcangeli unlock_page(swapcache); 29784969c119SAndrea Arcangeli page_cache_release(swapcache); 29794969c119SAndrea Arcangeli } 2980c475a8abSHugh Dickins 298130c9f3a9SLinus Torvalds if (flags & FAULT_FLAG_WRITE) { 298261469f1dSHugh Dickins ret |= do_wp_page(mm, vma, address, page_table, pmd, ptl, pte); 298361469f1dSHugh Dickins if (ret & VM_FAULT_ERROR) 298461469f1dSHugh Dickins ret &= VM_FAULT_ERROR; 29851da177e4SLinus Torvalds goto out; 29861da177e4SLinus Torvalds } 29871da177e4SLinus Torvalds 29881da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 29894b3073e1SRussell King update_mmu_cache(vma, address, page_table); 299065500d23SHugh Dickins unlock: 29918f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 29921da177e4SLinus Torvalds out: 29931da177e4SLinus Torvalds return ret; 2994b8107480SKirill Korotaev out_nomap: 29957a81b88cSKAMEZAWA Hiroyuki mem_cgroup_cancel_charge_swapin(ptr); 29968f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 2997bc43f75cSJohannes Weiner out_page: 2998b8107480SKirill Korotaev unlock_page(page); 29994779cb31SAndi Kleen out_release: 3000b8107480SKirill Korotaev page_cache_release(page); 30014969c119SAndrea Arcangeli if (swapcache) { 30024969c119SAndrea Arcangeli unlock_page(swapcache); 30034969c119SAndrea Arcangeli page_cache_release(swapcache); 30044969c119SAndrea Arcangeli } 300565500d23SHugh Dickins return ret; 30061da177e4SLinus Torvalds } 30071da177e4SLinus Torvalds 30081da177e4SLinus Torvalds /* 30098ca3eb08SLuck, Tony * This is like a special single-page "expand_{down|up}wards()", 30108ca3eb08SLuck, Tony * except we must first make sure that 'address{-|+}PAGE_SIZE' 3011320b2b8dSLinus Torvalds * doesn't hit another vma. 3012320b2b8dSLinus Torvalds */ 3013320b2b8dSLinus Torvalds static inline int check_stack_guard_page(struct vm_area_struct *vma, unsigned long address) 3014320b2b8dSLinus Torvalds { 3015320b2b8dSLinus Torvalds address &= PAGE_MASK; 3016320b2b8dSLinus Torvalds if ((vma->vm_flags & VM_GROWSDOWN) && address == vma->vm_start) { 30170e8e50e2SLinus Torvalds struct vm_area_struct *prev = vma->vm_prev; 3018320b2b8dSLinus Torvalds 30190e8e50e2SLinus Torvalds /* 30200e8e50e2SLinus Torvalds * Is there a mapping abutting this one below? 30210e8e50e2SLinus Torvalds * 30220e8e50e2SLinus Torvalds * That's only ok if it's the same stack mapping 30230e8e50e2SLinus Torvalds * that has gotten split.. 30240e8e50e2SLinus Torvalds */ 30250e8e50e2SLinus Torvalds if (prev && prev->vm_end == address) 30260e8e50e2SLinus Torvalds return prev->vm_flags & VM_GROWSDOWN ? 0 : -ENOMEM; 30270e8e50e2SLinus Torvalds 3028d05f3169SMichal Hocko expand_downwards(vma, address - PAGE_SIZE); 3029320b2b8dSLinus Torvalds } 30308ca3eb08SLuck, Tony if ((vma->vm_flags & VM_GROWSUP) && address + PAGE_SIZE == vma->vm_end) { 30318ca3eb08SLuck, Tony struct vm_area_struct *next = vma->vm_next; 30328ca3eb08SLuck, Tony 30338ca3eb08SLuck, Tony /* As VM_GROWSDOWN but s/below/above/ */ 30348ca3eb08SLuck, Tony if (next && next->vm_start == address + PAGE_SIZE) 30358ca3eb08SLuck, Tony return next->vm_flags & VM_GROWSUP ? 0 : -ENOMEM; 30368ca3eb08SLuck, Tony 30378ca3eb08SLuck, Tony expand_upwards(vma, address + PAGE_SIZE); 30388ca3eb08SLuck, Tony } 3039320b2b8dSLinus Torvalds return 0; 3040320b2b8dSLinus Torvalds } 3041320b2b8dSLinus Torvalds 3042320b2b8dSLinus Torvalds /* 30438f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 30448f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 30458f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 30461da177e4SLinus Torvalds */ 304765500d23SHugh Dickins static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma, 304865500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 304930c9f3a9SLinus Torvalds unsigned int flags) 30501da177e4SLinus Torvalds { 30518f4e2101SHugh Dickins struct page *page; 30528f4e2101SHugh Dickins spinlock_t *ptl; 30531da177e4SLinus Torvalds pte_t entry; 30541da177e4SLinus Torvalds 30555528f913SLinus Torvalds pte_unmap(page_table); 3056320b2b8dSLinus Torvalds 305711ac5524SLinus Torvalds /* Check if we need to add a guard page to the stack */ 305811ac5524SLinus Torvalds if (check_stack_guard_page(vma, address) < 0) 305911ac5524SLinus Torvalds return VM_FAULT_SIGBUS; 306011ac5524SLinus Torvalds 306111ac5524SLinus Torvalds /* Use the zero-page for reads */ 306262eede62SHugh Dickins if (!(flags & FAULT_FLAG_WRITE)) { 306362eede62SHugh Dickins entry = pte_mkspecial(pfn_pte(my_zero_pfn(address), 306462eede62SHugh Dickins vma->vm_page_prot)); 306511ac5524SLinus Torvalds page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 3066a13ea5b7SHugh Dickins if (!pte_none(*page_table)) 3067a13ea5b7SHugh Dickins goto unlock; 3068a13ea5b7SHugh Dickins goto setpte; 3069a13ea5b7SHugh Dickins } 3070a13ea5b7SHugh Dickins 30711da177e4SLinus Torvalds /* Allocate our own private page. */ 30721da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 307365500d23SHugh Dickins goto oom; 3074769848c0SMel Gorman page = alloc_zeroed_user_highpage_movable(vma, address); 30751da177e4SLinus Torvalds if (!page) 307665500d23SHugh Dickins goto oom; 30770ed361deSNick Piggin __SetPageUptodate(page); 30781da177e4SLinus Torvalds 30792c26fdd7SKAMEZAWA Hiroyuki if (mem_cgroup_newpage_charge(page, mm, GFP_KERNEL)) 30808a9f3ccdSBalbir Singh goto oom_free_page; 30818a9f3ccdSBalbir Singh 308265500d23SHugh Dickins entry = mk_pte(page, vma->vm_page_prot); 30831ac0cb5dSHugh Dickins if (vma->vm_flags & VM_WRITE) 30841ac0cb5dSHugh Dickins entry = pte_mkwrite(pte_mkdirty(entry)); 30858f4e2101SHugh Dickins 30868f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 30871c2fb7a4SAndrea Arcangeli if (!pte_none(*page_table)) 30888f4e2101SHugh Dickins goto release; 30899ba69294SHugh Dickins 309034e55232SKAMEZAWA Hiroyuki inc_mm_counter_fast(mm, MM_ANONPAGES); 30919617d95eSNick Piggin page_add_new_anon_rmap(page, vma, address); 3092a13ea5b7SHugh Dickins setpte: 309365500d23SHugh Dickins set_pte_at(mm, address, page_table, entry); 30941da177e4SLinus Torvalds 30951da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 30964b3073e1SRussell King update_mmu_cache(vma, address, page_table); 309765500d23SHugh Dickins unlock: 30988f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 309983c54070SNick Piggin return 0; 31008f4e2101SHugh Dickins release: 31018a9f3ccdSBalbir Singh mem_cgroup_uncharge_page(page); 31028f4e2101SHugh Dickins page_cache_release(page); 31038f4e2101SHugh Dickins goto unlock; 31048a9f3ccdSBalbir Singh oom_free_page: 31056dbf6d3bSHugh Dickins page_cache_release(page); 310665500d23SHugh Dickins oom: 31071da177e4SLinus Torvalds return VM_FAULT_OOM; 31081da177e4SLinus Torvalds } 31091da177e4SLinus Torvalds 31101da177e4SLinus Torvalds /* 311154cb8821SNick Piggin * __do_fault() tries to create a new page mapping. It aggressively 31121da177e4SLinus Torvalds * tries to share with existing pages, but makes a separate copy if 311354cb8821SNick Piggin * the FAULT_FLAG_WRITE is set in the flags parameter in order to avoid 311454cb8821SNick Piggin * the next page fault. 31151da177e4SLinus Torvalds * 31161da177e4SLinus Torvalds * As this is called only for pages that do not currently exist, we 31171da177e4SLinus Torvalds * do not need to flush old virtual caches or the TLB. 31181da177e4SLinus Torvalds * 31198f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 312016abfa08SHugh Dickins * but allow concurrent faults), and pte neither mapped nor locked. 31218f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 31221da177e4SLinus Torvalds */ 312354cb8821SNick Piggin static int __do_fault(struct mm_struct *mm, struct vm_area_struct *vma, 312416abfa08SHugh Dickins unsigned long address, pmd_t *pmd, 312554cb8821SNick Piggin pgoff_t pgoff, unsigned int flags, pte_t orig_pte) 31261da177e4SLinus Torvalds { 312716abfa08SHugh Dickins pte_t *page_table; 31288f4e2101SHugh Dickins spinlock_t *ptl; 3129d0217ac0SNick Piggin struct page *page; 31301da177e4SLinus Torvalds pte_t entry; 31311da177e4SLinus Torvalds int anon = 0; 31325b4e655eSKAMEZAWA Hiroyuki int charged = 0; 3133d08b3851SPeter Zijlstra struct page *dirty_page = NULL; 3134d0217ac0SNick Piggin struct vm_fault vmf; 3135d0217ac0SNick Piggin int ret; 3136a200ee18SPeter Zijlstra int page_mkwrite = 0; 313754cb8821SNick Piggin 3138d0217ac0SNick Piggin vmf.virtual_address = (void __user *)(address & PAGE_MASK); 3139d0217ac0SNick Piggin vmf.pgoff = pgoff; 3140d0217ac0SNick Piggin vmf.flags = flags; 3141d0217ac0SNick Piggin vmf.page = NULL; 31421da177e4SLinus Torvalds 3143d0217ac0SNick Piggin ret = vma->vm_ops->fault(vma, &vmf); 3144d065bd81SMichel Lespinasse if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | 3145d065bd81SMichel Lespinasse VM_FAULT_RETRY))) 314683c54070SNick Piggin return ret; 31471da177e4SLinus Torvalds 3148a3b947eaSAndi Kleen if (unlikely(PageHWPoison(vmf.page))) { 3149a3b947eaSAndi Kleen if (ret & VM_FAULT_LOCKED) 3150a3b947eaSAndi Kleen unlock_page(vmf.page); 3151a3b947eaSAndi Kleen return VM_FAULT_HWPOISON; 3152a3b947eaSAndi Kleen } 3153a3b947eaSAndi Kleen 3154d00806b1SNick Piggin /* 3155d0217ac0SNick Piggin * For consistency in subsequent calls, make the faulted page always 3156d00806b1SNick Piggin * locked. 3157d00806b1SNick Piggin */ 315883c54070SNick Piggin if (unlikely(!(ret & VM_FAULT_LOCKED))) 3159d0217ac0SNick Piggin lock_page(vmf.page); 316054cb8821SNick Piggin else 3161d0217ac0SNick Piggin VM_BUG_ON(!PageLocked(vmf.page)); 3162d00806b1SNick Piggin 31631da177e4SLinus Torvalds /* 31641da177e4SLinus Torvalds * Should we do an early C-O-W break? 31651da177e4SLinus Torvalds */ 3166d0217ac0SNick Piggin page = vmf.page; 316754cb8821SNick Piggin if (flags & FAULT_FLAG_WRITE) { 31689637a5efSDavid Howells if (!(vma->vm_flags & VM_SHARED)) { 316954cb8821SNick Piggin anon = 1; 3170d00806b1SNick Piggin if (unlikely(anon_vma_prepare(vma))) { 3171d0217ac0SNick Piggin ret = VM_FAULT_OOM; 317254cb8821SNick Piggin goto out; 3173d00806b1SNick Piggin } 317483c54070SNick Piggin page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, 317583c54070SNick Piggin vma, address); 3176d00806b1SNick Piggin if (!page) { 3177d0217ac0SNick Piggin ret = VM_FAULT_OOM; 317854cb8821SNick Piggin goto out; 3179d00806b1SNick Piggin } 31802c26fdd7SKAMEZAWA Hiroyuki if (mem_cgroup_newpage_charge(page, mm, GFP_KERNEL)) { 31815b4e655eSKAMEZAWA Hiroyuki ret = VM_FAULT_OOM; 31825b4e655eSKAMEZAWA Hiroyuki page_cache_release(page); 31835b4e655eSKAMEZAWA Hiroyuki goto out; 31845b4e655eSKAMEZAWA Hiroyuki } 31855b4e655eSKAMEZAWA Hiroyuki charged = 1; 3186d0217ac0SNick Piggin copy_user_highpage(page, vmf.page, address, vma); 31870ed361deSNick Piggin __SetPageUptodate(page); 31889637a5efSDavid Howells } else { 318954cb8821SNick Piggin /* 319054cb8821SNick Piggin * If the page will be shareable, see if the backing 31919637a5efSDavid Howells * address space wants to know that the page is about 319254cb8821SNick Piggin * to become writable 319354cb8821SNick Piggin */ 319469676147SMark Fasheh if (vma->vm_ops->page_mkwrite) { 3195c2ec175cSNick Piggin int tmp; 3196c2ec175cSNick Piggin 319769676147SMark Fasheh unlock_page(page); 3198b827e496SNick Piggin vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE; 3199c2ec175cSNick Piggin tmp = vma->vm_ops->page_mkwrite(vma, &vmf); 3200c2ec175cSNick Piggin if (unlikely(tmp & 3201c2ec175cSNick Piggin (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) { 3202c2ec175cSNick Piggin ret = tmp; 3203b827e496SNick Piggin goto unwritable_page; 320469676147SMark Fasheh } 3205b827e496SNick Piggin if (unlikely(!(tmp & VM_FAULT_LOCKED))) { 320669676147SMark Fasheh lock_page(page); 3207d0217ac0SNick Piggin if (!page->mapping) { 3208b827e496SNick Piggin ret = 0; /* retry the fault */ 3209b827e496SNick Piggin unlock_page(page); 3210b827e496SNick Piggin goto unwritable_page; 3211d0217ac0SNick Piggin } 3212b827e496SNick Piggin } else 3213b827e496SNick Piggin VM_BUG_ON(!PageLocked(page)); 3214a200ee18SPeter Zijlstra page_mkwrite = 1; 32159637a5efSDavid Howells } 32169637a5efSDavid Howells } 321754cb8821SNick Piggin 32181da177e4SLinus Torvalds } 32191da177e4SLinus Torvalds 32208f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 32211da177e4SLinus Torvalds 32221da177e4SLinus Torvalds /* 32231da177e4SLinus Torvalds * This silly early PAGE_DIRTY setting removes a race 32241da177e4SLinus Torvalds * due to the bad i386 page protection. But it's valid 32251da177e4SLinus Torvalds * for other architectures too. 32261da177e4SLinus Torvalds * 322730c9f3a9SLinus Torvalds * Note that if FAULT_FLAG_WRITE is set, we either now have 32281da177e4SLinus Torvalds * an exclusive copy of the page, or this is a shared mapping, 32291da177e4SLinus Torvalds * so we can make it writable and dirty to avoid having to 32301da177e4SLinus Torvalds * handle that later. 32311da177e4SLinus Torvalds */ 32321da177e4SLinus Torvalds /* Only go through if we didn't race with anybody else... */ 32331c2fb7a4SAndrea Arcangeli if (likely(pte_same(*page_table, orig_pte))) { 3234d00806b1SNick Piggin flush_icache_page(vma, page); 3235d00806b1SNick Piggin entry = mk_pte(page, vma->vm_page_prot); 323654cb8821SNick Piggin if (flags & FAULT_FLAG_WRITE) 32371da177e4SLinus Torvalds entry = maybe_mkwrite(pte_mkdirty(entry), vma); 32381da177e4SLinus Torvalds if (anon) { 323934e55232SKAMEZAWA Hiroyuki inc_mm_counter_fast(mm, MM_ANONPAGES); 3240d00806b1SNick Piggin page_add_new_anon_rmap(page, vma, address); 3241f57e88a8SHugh Dickins } else { 324234e55232SKAMEZAWA Hiroyuki inc_mm_counter_fast(mm, MM_FILEPAGES); 3243d00806b1SNick Piggin page_add_file_rmap(page); 324454cb8821SNick Piggin if (flags & FAULT_FLAG_WRITE) { 3245d00806b1SNick Piggin dirty_page = page; 3246d08b3851SPeter Zijlstra get_page(dirty_page); 3247d08b3851SPeter Zijlstra } 32484294621fSHugh Dickins } 324964d6519dSLee Schermerhorn set_pte_at(mm, address, page_table, entry); 32501da177e4SLinus Torvalds 3251d00806b1SNick Piggin /* no need to invalidate: a not-present page won't be cached */ 32524b3073e1SRussell King update_mmu_cache(vma, address, page_table); 3253d00806b1SNick Piggin } else { 32545b4e655eSKAMEZAWA Hiroyuki if (charged) 32558a9f3ccdSBalbir Singh mem_cgroup_uncharge_page(page); 3256d00806b1SNick Piggin if (anon) 3257d00806b1SNick Piggin page_cache_release(page); 3258d00806b1SNick Piggin else 325954cb8821SNick Piggin anon = 1; /* no anon but release faulted_page */ 3260d00806b1SNick Piggin } 3261d00806b1SNick Piggin 32628f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 3263d00806b1SNick Piggin 3264d00806b1SNick Piggin out: 3265b827e496SNick Piggin if (dirty_page) { 3266b827e496SNick Piggin struct address_space *mapping = page->mapping; 32678f7b3d15SAnton Salikhmetov 3268b827e496SNick Piggin if (set_page_dirty(dirty_page)) 3269b827e496SNick Piggin page_mkwrite = 1; 3270b827e496SNick Piggin unlock_page(dirty_page); 3271d08b3851SPeter Zijlstra put_page(dirty_page); 3272b827e496SNick Piggin if (page_mkwrite && mapping) { 3273b827e496SNick Piggin /* 3274b827e496SNick Piggin * Some device drivers do not set page.mapping but still 3275b827e496SNick Piggin * dirty their pages 3276b827e496SNick Piggin */ 3277b827e496SNick Piggin balance_dirty_pages_ratelimited(mapping); 3278d08b3851SPeter Zijlstra } 3279d00806b1SNick Piggin 3280b827e496SNick Piggin /* file_update_time outside page_lock */ 3281b827e496SNick Piggin if (vma->vm_file) 3282b827e496SNick Piggin file_update_time(vma->vm_file); 3283b827e496SNick Piggin } else { 3284b827e496SNick Piggin unlock_page(vmf.page); 3285b827e496SNick Piggin if (anon) 3286b827e496SNick Piggin page_cache_release(vmf.page); 3287b827e496SNick Piggin } 3288b827e496SNick Piggin 3289b827e496SNick Piggin return ret; 3290b827e496SNick Piggin 3291b827e496SNick Piggin unwritable_page: 3292b827e496SNick Piggin page_cache_release(page); 329383c54070SNick Piggin return ret; 329454cb8821SNick Piggin } 3295d00806b1SNick Piggin 329654cb8821SNick Piggin static int do_linear_fault(struct mm_struct *mm, struct vm_area_struct *vma, 329754cb8821SNick Piggin unsigned long address, pte_t *page_table, pmd_t *pmd, 329830c9f3a9SLinus Torvalds unsigned int flags, pte_t orig_pte) 329954cb8821SNick Piggin { 330054cb8821SNick Piggin pgoff_t pgoff = (((address & PAGE_MASK) 33010da7e01fSDean Nelson - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; 330254cb8821SNick Piggin 330316abfa08SHugh Dickins pte_unmap(page_table); 330416abfa08SHugh Dickins return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte); 330554cb8821SNick Piggin } 330654cb8821SNick Piggin 3307f4b81804SJes Sorensen /* 33081da177e4SLinus Torvalds * Fault of a previously existing named mapping. Repopulate the pte 33091da177e4SLinus Torvalds * from the encoded file_pte if possible. This enables swappable 33101da177e4SLinus Torvalds * nonlinear vmas. 33118f4e2101SHugh Dickins * 33128f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 33138f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 33148f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 33151da177e4SLinus Torvalds */ 3316d0217ac0SNick Piggin static int do_nonlinear_fault(struct mm_struct *mm, struct vm_area_struct *vma, 331765500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 331830c9f3a9SLinus Torvalds unsigned int flags, pte_t orig_pte) 33191da177e4SLinus Torvalds { 332065500d23SHugh Dickins pgoff_t pgoff; 33211da177e4SLinus Torvalds 332230c9f3a9SLinus Torvalds flags |= FAULT_FLAG_NONLINEAR; 332330c9f3a9SLinus Torvalds 33244c21e2f2SHugh Dickins if (!pte_unmap_same(mm, pmd, page_table, orig_pte)) 332583c54070SNick Piggin return 0; 33261da177e4SLinus Torvalds 33272509ef26SHugh Dickins if (unlikely(!(vma->vm_flags & VM_NONLINEAR))) { 332865500d23SHugh Dickins /* 332965500d23SHugh Dickins * Page table corrupted: show pte and kill process. 333065500d23SHugh Dickins */ 33313dc14741SHugh Dickins print_bad_pte(vma, address, orig_pte, NULL); 3332d99be1a8SHugh Dickins return VM_FAULT_SIGBUS; 333365500d23SHugh Dickins } 333465500d23SHugh Dickins 333565500d23SHugh Dickins pgoff = pte_to_pgoff(orig_pte); 333616abfa08SHugh Dickins return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte); 33371da177e4SLinus Torvalds } 33381da177e4SLinus Torvalds 33391da177e4SLinus Torvalds /* 33401da177e4SLinus Torvalds * These routines also need to handle stuff like marking pages dirty 33411da177e4SLinus Torvalds * and/or accessed for architectures that don't do it in hardware (most 33421da177e4SLinus Torvalds * RISC architectures). The early dirtying is also good on the i386. 33431da177e4SLinus Torvalds * 33441da177e4SLinus Torvalds * There is also a hook called "update_mmu_cache()" that architectures 33451da177e4SLinus Torvalds * with external mmu caches can use to update those (ie the Sparc or 33461da177e4SLinus Torvalds * PowerPC hashed page tables that act as extended TLBs). 33471da177e4SLinus Torvalds * 3348c74df32cSHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 3349c74df32cSHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 3350c74df32cSHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 33511da177e4SLinus Torvalds */ 335271e3aac0SAndrea Arcangeli int handle_pte_fault(struct mm_struct *mm, 33531da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long address, 335430c9f3a9SLinus Torvalds pte_t *pte, pmd_t *pmd, unsigned int flags) 33551da177e4SLinus Torvalds { 33561da177e4SLinus Torvalds pte_t entry; 33578f4e2101SHugh Dickins spinlock_t *ptl; 33581da177e4SLinus Torvalds 33598dab5241SBenjamin Herrenschmidt entry = *pte; 33601da177e4SLinus Torvalds if (!pte_present(entry)) { 336165500d23SHugh Dickins if (pte_none(entry)) { 3362f4b81804SJes Sorensen if (vma->vm_ops) { 33633c18ddd1SNick Piggin if (likely(vma->vm_ops->fault)) 336454cb8821SNick Piggin return do_linear_fault(mm, vma, address, 336530c9f3a9SLinus Torvalds pte, pmd, flags, entry); 3366f4b81804SJes Sorensen } 3367f4b81804SJes Sorensen return do_anonymous_page(mm, vma, address, 336830c9f3a9SLinus Torvalds pte, pmd, flags); 336965500d23SHugh Dickins } 33701da177e4SLinus Torvalds if (pte_file(entry)) 3371d0217ac0SNick Piggin return do_nonlinear_fault(mm, vma, address, 337230c9f3a9SLinus Torvalds pte, pmd, flags, entry); 337365500d23SHugh Dickins return do_swap_page(mm, vma, address, 337430c9f3a9SLinus Torvalds pte, pmd, flags, entry); 33751da177e4SLinus Torvalds } 33761da177e4SLinus Torvalds 33774c21e2f2SHugh Dickins ptl = pte_lockptr(mm, pmd); 33788f4e2101SHugh Dickins spin_lock(ptl); 33798f4e2101SHugh Dickins if (unlikely(!pte_same(*pte, entry))) 33808f4e2101SHugh Dickins goto unlock; 338130c9f3a9SLinus Torvalds if (flags & FAULT_FLAG_WRITE) { 33821da177e4SLinus Torvalds if (!pte_write(entry)) 33838f4e2101SHugh Dickins return do_wp_page(mm, vma, address, 33848f4e2101SHugh Dickins pte, pmd, ptl, entry); 33851da177e4SLinus Torvalds entry = pte_mkdirty(entry); 33861da177e4SLinus Torvalds } 33871da177e4SLinus Torvalds entry = pte_mkyoung(entry); 338830c9f3a9SLinus Torvalds if (ptep_set_access_flags(vma, address, pte, entry, flags & FAULT_FLAG_WRITE)) { 33894b3073e1SRussell King update_mmu_cache(vma, address, pte); 33901a44e149SAndrea Arcangeli } else { 33911a44e149SAndrea Arcangeli /* 33921a44e149SAndrea Arcangeli * This is needed only for protection faults but the arch code 33931a44e149SAndrea Arcangeli * is not yet telling us if this is a protection fault or not. 33941a44e149SAndrea Arcangeli * This still avoids useless tlb flushes for .text page faults 33951a44e149SAndrea Arcangeli * with threads. 33961a44e149SAndrea Arcangeli */ 339730c9f3a9SLinus Torvalds if (flags & FAULT_FLAG_WRITE) 339861c77326SShaohua Li flush_tlb_fix_spurious_fault(vma, address); 33991a44e149SAndrea Arcangeli } 34008f4e2101SHugh Dickins unlock: 34018f4e2101SHugh Dickins pte_unmap_unlock(pte, ptl); 340283c54070SNick Piggin return 0; 34031da177e4SLinus Torvalds } 34041da177e4SLinus Torvalds 34051da177e4SLinus Torvalds /* 34061da177e4SLinus Torvalds * By the time we get here, we already hold the mm semaphore 34071da177e4SLinus Torvalds */ 340883c54070SNick Piggin int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma, 3409d06063ccSLinus Torvalds unsigned long address, unsigned int flags) 34101da177e4SLinus Torvalds { 34111da177e4SLinus Torvalds pgd_t *pgd; 34121da177e4SLinus Torvalds pud_t *pud; 34131da177e4SLinus Torvalds pmd_t *pmd; 34141da177e4SLinus Torvalds pte_t *pte; 34151da177e4SLinus Torvalds 34161da177e4SLinus Torvalds __set_current_state(TASK_RUNNING); 34171da177e4SLinus Torvalds 3418f8891e5eSChristoph Lameter count_vm_event(PGFAULT); 3419456f998eSYing Han mem_cgroup_count_vm_event(mm, PGFAULT); 34201da177e4SLinus Torvalds 342134e55232SKAMEZAWA Hiroyuki /* do counter updates before entering really critical section. */ 342234e55232SKAMEZAWA Hiroyuki check_sync_rss_stat(current); 342334e55232SKAMEZAWA Hiroyuki 3424ac9b9c66SHugh Dickins if (unlikely(is_vm_hugetlb_page(vma))) 342530c9f3a9SLinus Torvalds return hugetlb_fault(mm, vma, address, flags); 34261da177e4SLinus Torvalds 34271da177e4SLinus Torvalds pgd = pgd_offset(mm, address); 34281da177e4SLinus Torvalds pud = pud_alloc(mm, pgd, address); 34291da177e4SLinus Torvalds if (!pud) 3430c74df32cSHugh Dickins return VM_FAULT_OOM; 34311da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, address); 34321da177e4SLinus Torvalds if (!pmd) 3433c74df32cSHugh Dickins return VM_FAULT_OOM; 343471e3aac0SAndrea Arcangeli if (pmd_none(*pmd) && transparent_hugepage_enabled(vma)) { 343571e3aac0SAndrea Arcangeli if (!vma->vm_ops) 343671e3aac0SAndrea Arcangeli return do_huge_pmd_anonymous_page(mm, vma, address, 343771e3aac0SAndrea Arcangeli pmd, flags); 343871e3aac0SAndrea Arcangeli } else { 343971e3aac0SAndrea Arcangeli pmd_t orig_pmd = *pmd; 344071e3aac0SAndrea Arcangeli barrier(); 344171e3aac0SAndrea Arcangeli if (pmd_trans_huge(orig_pmd)) { 344271e3aac0SAndrea Arcangeli if (flags & FAULT_FLAG_WRITE && 344371e3aac0SAndrea Arcangeli !pmd_write(orig_pmd) && 344471e3aac0SAndrea Arcangeli !pmd_trans_splitting(orig_pmd)) 344571e3aac0SAndrea Arcangeli return do_huge_pmd_wp_page(mm, vma, address, 344671e3aac0SAndrea Arcangeli pmd, orig_pmd); 344771e3aac0SAndrea Arcangeli return 0; 344871e3aac0SAndrea Arcangeli } 344971e3aac0SAndrea Arcangeli } 345071e3aac0SAndrea Arcangeli 345171e3aac0SAndrea Arcangeli /* 345271e3aac0SAndrea Arcangeli * Use __pte_alloc instead of pte_alloc_map, because we can't 345371e3aac0SAndrea Arcangeli * run pte_offset_map on the pmd, if an huge pmd could 345471e3aac0SAndrea Arcangeli * materialize from under us from a different thread. 345571e3aac0SAndrea Arcangeli */ 3456cc03638dSMel Gorman if (unlikely(pmd_none(*pmd)) && __pte_alloc(mm, vma, pmd, address)) 3457c74df32cSHugh Dickins return VM_FAULT_OOM; 345871e3aac0SAndrea Arcangeli /* if an huge pmd materialized from under us just retry later */ 345971e3aac0SAndrea Arcangeli if (unlikely(pmd_trans_huge(*pmd))) 346071e3aac0SAndrea Arcangeli return 0; 346171e3aac0SAndrea Arcangeli /* 346271e3aac0SAndrea Arcangeli * A regular pmd is established and it can't morph into a huge pmd 346371e3aac0SAndrea Arcangeli * from under us anymore at this point because we hold the mmap_sem 346471e3aac0SAndrea Arcangeli * read mode and khugepaged takes it in write mode. So now it's 346571e3aac0SAndrea Arcangeli * safe to run pte_offset_map(). 346671e3aac0SAndrea Arcangeli */ 346771e3aac0SAndrea Arcangeli pte = pte_offset_map(pmd, address); 34681da177e4SLinus Torvalds 346930c9f3a9SLinus Torvalds return handle_pte_fault(mm, vma, address, pte, pmd, flags); 34701da177e4SLinus Torvalds } 34711da177e4SLinus Torvalds 34721da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED 34731da177e4SLinus Torvalds /* 34741da177e4SLinus Torvalds * Allocate page upper directory. 3475872fec16SHugh Dickins * We've already handled the fast-path in-line. 34761da177e4SLinus Torvalds */ 34771bb3630eSHugh Dickins int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address) 34781da177e4SLinus Torvalds { 3479c74df32cSHugh Dickins pud_t *new = pud_alloc_one(mm, address); 3480c74df32cSHugh Dickins if (!new) 34811bb3630eSHugh Dickins return -ENOMEM; 34821da177e4SLinus Torvalds 3483362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 3484362a61adSNick Piggin 3485872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 34861bb3630eSHugh Dickins if (pgd_present(*pgd)) /* Another has populated it */ 34875e541973SBenjamin Herrenschmidt pud_free(mm, new); 34881bb3630eSHugh Dickins else 34891da177e4SLinus Torvalds pgd_populate(mm, pgd, new); 3490872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 34911bb3630eSHugh Dickins return 0; 34921da177e4SLinus Torvalds } 34931da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */ 34941da177e4SLinus Torvalds 34951da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED 34961da177e4SLinus Torvalds /* 34971da177e4SLinus Torvalds * Allocate page middle directory. 3498872fec16SHugh Dickins * We've already handled the fast-path in-line. 34991da177e4SLinus Torvalds */ 35001bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) 35011da177e4SLinus Torvalds { 3502c74df32cSHugh Dickins pmd_t *new = pmd_alloc_one(mm, address); 3503c74df32cSHugh Dickins if (!new) 35041bb3630eSHugh Dickins return -ENOMEM; 35051da177e4SLinus Torvalds 3506362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 3507362a61adSNick Piggin 3508872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 35091da177e4SLinus Torvalds #ifndef __ARCH_HAS_4LEVEL_HACK 35101bb3630eSHugh Dickins if (pud_present(*pud)) /* Another has populated it */ 35115e541973SBenjamin Herrenschmidt pmd_free(mm, new); 35121bb3630eSHugh Dickins else 35131da177e4SLinus Torvalds pud_populate(mm, pud, new); 35141da177e4SLinus Torvalds #else 35151bb3630eSHugh Dickins if (pgd_present(*pud)) /* Another has populated it */ 35165e541973SBenjamin Herrenschmidt pmd_free(mm, new); 35171bb3630eSHugh Dickins else 35181da177e4SLinus Torvalds pgd_populate(mm, pud, new); 35191da177e4SLinus Torvalds #endif /* __ARCH_HAS_4LEVEL_HACK */ 3520872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 35211bb3630eSHugh Dickins return 0; 35221da177e4SLinus Torvalds } 35231da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */ 35241da177e4SLinus Torvalds 35251da177e4SLinus Torvalds int make_pages_present(unsigned long addr, unsigned long end) 35261da177e4SLinus Torvalds { 35271da177e4SLinus Torvalds int ret, len, write; 35281da177e4SLinus Torvalds struct vm_area_struct * vma; 35291da177e4SLinus Torvalds 35301da177e4SLinus Torvalds vma = find_vma(current->mm, addr); 35311da177e4SLinus Torvalds if (!vma) 3532a477097dSKOSAKI Motohiro return -ENOMEM; 35335ecfda04SMichel Lespinasse /* 35345ecfda04SMichel Lespinasse * We want to touch writable mappings with a write fault in order 35355ecfda04SMichel Lespinasse * to break COW, except for shared mappings because these don't COW 35365ecfda04SMichel Lespinasse * and we would not want to dirty them for nothing. 35375ecfda04SMichel Lespinasse */ 35385ecfda04SMichel Lespinasse write = (vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE; 35395bcb28b1SEric Sesterhenn BUG_ON(addr >= end); 35405bcb28b1SEric Sesterhenn BUG_ON(end > vma->vm_end); 354168e116a3SRolf Eike Beer len = DIV_ROUND_UP(end, PAGE_SIZE) - addr/PAGE_SIZE; 35421da177e4SLinus Torvalds ret = get_user_pages(current, current->mm, addr, 35431da177e4SLinus Torvalds len, write, 0, NULL, NULL); 3544c11d69d8SLee Schermerhorn if (ret < 0) 35451da177e4SLinus Torvalds return ret; 35469978ad58SLee Schermerhorn return ret == len ? 0 : -EFAULT; 35471da177e4SLinus Torvalds } 35481da177e4SLinus Torvalds 35491da177e4SLinus Torvalds #if !defined(__HAVE_ARCH_GATE_AREA) 35501da177e4SLinus Torvalds 35511da177e4SLinus Torvalds #if defined(AT_SYSINFO_EHDR) 35525ce7852cSAdrian Bunk static struct vm_area_struct gate_vma; 35531da177e4SLinus Torvalds 35541da177e4SLinus Torvalds static int __init gate_vma_init(void) 35551da177e4SLinus Torvalds { 35561da177e4SLinus Torvalds gate_vma.vm_mm = NULL; 35571da177e4SLinus Torvalds gate_vma.vm_start = FIXADDR_USER_START; 35581da177e4SLinus Torvalds gate_vma.vm_end = FIXADDR_USER_END; 3559b6558c4aSRoland McGrath gate_vma.vm_flags = VM_READ | VM_MAYREAD | VM_EXEC | VM_MAYEXEC; 3560b6558c4aSRoland McGrath gate_vma.vm_page_prot = __P101; 3561f47aef55SRoland McGrath /* 3562f47aef55SRoland McGrath * Make sure the vDSO gets into every core dump. 3563f47aef55SRoland McGrath * Dumping its contents makes post-mortem fully interpretable later 3564f47aef55SRoland McGrath * without matching up the same kernel and hardware config to see 3565f47aef55SRoland McGrath * what PC values meant. 3566f47aef55SRoland McGrath */ 3567f47aef55SRoland McGrath gate_vma.vm_flags |= VM_ALWAYSDUMP; 35681da177e4SLinus Torvalds return 0; 35691da177e4SLinus Torvalds } 35701da177e4SLinus Torvalds __initcall(gate_vma_init); 35711da177e4SLinus Torvalds #endif 35721da177e4SLinus Torvalds 357331db58b3SStephen Wilson struct vm_area_struct *get_gate_vma(struct mm_struct *mm) 35741da177e4SLinus Torvalds { 35751da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR 35761da177e4SLinus Torvalds return &gate_vma; 35771da177e4SLinus Torvalds #else 35781da177e4SLinus Torvalds return NULL; 35791da177e4SLinus Torvalds #endif 35801da177e4SLinus Torvalds } 35811da177e4SLinus Torvalds 3582cae5d390SStephen Wilson int in_gate_area_no_mm(unsigned long addr) 35831da177e4SLinus Torvalds { 35841da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR 35851da177e4SLinus Torvalds if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END)) 35861da177e4SLinus Torvalds return 1; 35871da177e4SLinus Torvalds #endif 35881da177e4SLinus Torvalds return 0; 35891da177e4SLinus Torvalds } 35901da177e4SLinus Torvalds 35911da177e4SLinus Torvalds #endif /* __HAVE_ARCH_GATE_AREA */ 35920ec76a11SDavid Howells 35931b36ba81SNamhyung Kim static int __follow_pte(struct mm_struct *mm, unsigned long address, 3594f8ad0f49SJohannes Weiner pte_t **ptepp, spinlock_t **ptlp) 3595f8ad0f49SJohannes Weiner { 3596f8ad0f49SJohannes Weiner pgd_t *pgd; 3597f8ad0f49SJohannes Weiner pud_t *pud; 3598f8ad0f49SJohannes Weiner pmd_t *pmd; 3599f8ad0f49SJohannes Weiner pte_t *ptep; 3600f8ad0f49SJohannes Weiner 3601f8ad0f49SJohannes Weiner pgd = pgd_offset(mm, address); 3602f8ad0f49SJohannes Weiner if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) 3603f8ad0f49SJohannes Weiner goto out; 3604f8ad0f49SJohannes Weiner 3605f8ad0f49SJohannes Weiner pud = pud_offset(pgd, address); 3606f8ad0f49SJohannes Weiner if (pud_none(*pud) || unlikely(pud_bad(*pud))) 3607f8ad0f49SJohannes Weiner goto out; 3608f8ad0f49SJohannes Weiner 3609f8ad0f49SJohannes Weiner pmd = pmd_offset(pud, address); 3610f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 3611f8ad0f49SJohannes Weiner if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd))) 3612f8ad0f49SJohannes Weiner goto out; 3613f8ad0f49SJohannes Weiner 3614f8ad0f49SJohannes Weiner /* We cannot handle huge page PFN maps. Luckily they don't exist. */ 3615f8ad0f49SJohannes Weiner if (pmd_huge(*pmd)) 3616f8ad0f49SJohannes Weiner goto out; 3617f8ad0f49SJohannes Weiner 3618f8ad0f49SJohannes Weiner ptep = pte_offset_map_lock(mm, pmd, address, ptlp); 3619f8ad0f49SJohannes Weiner if (!ptep) 3620f8ad0f49SJohannes Weiner goto out; 3621f8ad0f49SJohannes Weiner if (!pte_present(*ptep)) 3622f8ad0f49SJohannes Weiner goto unlock; 3623f8ad0f49SJohannes Weiner *ptepp = ptep; 3624f8ad0f49SJohannes Weiner return 0; 3625f8ad0f49SJohannes Weiner unlock: 3626f8ad0f49SJohannes Weiner pte_unmap_unlock(ptep, *ptlp); 3627f8ad0f49SJohannes Weiner out: 3628f8ad0f49SJohannes Weiner return -EINVAL; 3629f8ad0f49SJohannes Weiner } 3630f8ad0f49SJohannes Weiner 36311b36ba81SNamhyung Kim static inline int follow_pte(struct mm_struct *mm, unsigned long address, 36321b36ba81SNamhyung Kim pte_t **ptepp, spinlock_t **ptlp) 36331b36ba81SNamhyung Kim { 36341b36ba81SNamhyung Kim int res; 36351b36ba81SNamhyung Kim 36361b36ba81SNamhyung Kim /* (void) is needed to make gcc happy */ 36371b36ba81SNamhyung Kim (void) __cond_lock(*ptlp, 36381b36ba81SNamhyung Kim !(res = __follow_pte(mm, address, ptepp, ptlp))); 36391b36ba81SNamhyung Kim return res; 36401b36ba81SNamhyung Kim } 36411b36ba81SNamhyung Kim 36423b6748e2SJohannes Weiner /** 36433b6748e2SJohannes Weiner * follow_pfn - look up PFN at a user virtual address 36443b6748e2SJohannes Weiner * @vma: memory mapping 36453b6748e2SJohannes Weiner * @address: user virtual address 36463b6748e2SJohannes Weiner * @pfn: location to store found PFN 36473b6748e2SJohannes Weiner * 36483b6748e2SJohannes Weiner * Only IO mappings and raw PFN mappings are allowed. 36493b6748e2SJohannes Weiner * 36503b6748e2SJohannes Weiner * Returns zero and the pfn at @pfn on success, -ve otherwise. 36513b6748e2SJohannes Weiner */ 36523b6748e2SJohannes Weiner int follow_pfn(struct vm_area_struct *vma, unsigned long address, 36533b6748e2SJohannes Weiner unsigned long *pfn) 36543b6748e2SJohannes Weiner { 36553b6748e2SJohannes Weiner int ret = -EINVAL; 36563b6748e2SJohannes Weiner spinlock_t *ptl; 36573b6748e2SJohannes Weiner pte_t *ptep; 36583b6748e2SJohannes Weiner 36593b6748e2SJohannes Weiner if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 36603b6748e2SJohannes Weiner return ret; 36613b6748e2SJohannes Weiner 36623b6748e2SJohannes Weiner ret = follow_pte(vma->vm_mm, address, &ptep, &ptl); 36633b6748e2SJohannes Weiner if (ret) 36643b6748e2SJohannes Weiner return ret; 36653b6748e2SJohannes Weiner *pfn = pte_pfn(*ptep); 36663b6748e2SJohannes Weiner pte_unmap_unlock(ptep, ptl); 36673b6748e2SJohannes Weiner return 0; 36683b6748e2SJohannes Weiner } 36693b6748e2SJohannes Weiner EXPORT_SYMBOL(follow_pfn); 36703b6748e2SJohannes Weiner 367128b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT 3672d87fe660Svenkatesh.pallipadi@intel.com int follow_phys(struct vm_area_struct *vma, 367328b2ee20SRik van Riel unsigned long address, unsigned int flags, 3674d87fe660Svenkatesh.pallipadi@intel.com unsigned long *prot, resource_size_t *phys) 367528b2ee20SRik van Riel { 367603668a4dSJohannes Weiner int ret = -EINVAL; 367728b2ee20SRik van Riel pte_t *ptep, pte; 367828b2ee20SRik van Riel spinlock_t *ptl; 367928b2ee20SRik van Riel 3680d87fe660Svenkatesh.pallipadi@intel.com if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 3681d87fe660Svenkatesh.pallipadi@intel.com goto out; 368228b2ee20SRik van Riel 368303668a4dSJohannes Weiner if (follow_pte(vma->vm_mm, address, &ptep, &ptl)) 3684d87fe660Svenkatesh.pallipadi@intel.com goto out; 368528b2ee20SRik van Riel pte = *ptep; 368603668a4dSJohannes Weiner 368728b2ee20SRik van Riel if ((flags & FOLL_WRITE) && !pte_write(pte)) 368828b2ee20SRik van Riel goto unlock; 368928b2ee20SRik van Riel 369028b2ee20SRik van Riel *prot = pgprot_val(pte_pgprot(pte)); 369103668a4dSJohannes Weiner *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT; 369228b2ee20SRik van Riel 369303668a4dSJohannes Weiner ret = 0; 369428b2ee20SRik van Riel unlock: 369528b2ee20SRik van Riel pte_unmap_unlock(ptep, ptl); 369628b2ee20SRik van Riel out: 3697d87fe660Svenkatesh.pallipadi@intel.com return ret; 369828b2ee20SRik van Riel } 369928b2ee20SRik van Riel 370028b2ee20SRik van Riel int generic_access_phys(struct vm_area_struct *vma, unsigned long addr, 370128b2ee20SRik van Riel void *buf, int len, int write) 370228b2ee20SRik van Riel { 370328b2ee20SRik van Riel resource_size_t phys_addr; 370428b2ee20SRik van Riel unsigned long prot = 0; 37052bc7273bSKOSAKI Motohiro void __iomem *maddr; 370628b2ee20SRik van Riel int offset = addr & (PAGE_SIZE-1); 370728b2ee20SRik van Riel 3708d87fe660Svenkatesh.pallipadi@intel.com if (follow_phys(vma, addr, write, &prot, &phys_addr)) 370928b2ee20SRik van Riel return -EINVAL; 371028b2ee20SRik van Riel 371128b2ee20SRik van Riel maddr = ioremap_prot(phys_addr, PAGE_SIZE, prot); 371228b2ee20SRik van Riel if (write) 371328b2ee20SRik van Riel memcpy_toio(maddr + offset, buf, len); 371428b2ee20SRik van Riel else 371528b2ee20SRik van Riel memcpy_fromio(buf, maddr + offset, len); 371628b2ee20SRik van Riel iounmap(maddr); 371728b2ee20SRik van Riel 371828b2ee20SRik van Riel return len; 371928b2ee20SRik van Riel } 372028b2ee20SRik van Riel #endif 372128b2ee20SRik van Riel 37220ec76a11SDavid Howells /* 3723206cb636SStephen Wilson * Access another process' address space as given in mm. If non-NULL, use the 3724206cb636SStephen Wilson * given task for page fault accounting. 37250ec76a11SDavid Howells */ 3726206cb636SStephen Wilson static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm, 3727206cb636SStephen Wilson unsigned long addr, void *buf, int len, int write) 37280ec76a11SDavid Howells { 37290ec76a11SDavid Howells struct vm_area_struct *vma; 37300ec76a11SDavid Howells void *old_buf = buf; 37310ec76a11SDavid Howells 37320ec76a11SDavid Howells down_read(&mm->mmap_sem); 3733183ff22bSSimon Arlott /* ignore errors, just check how much was successfully transferred */ 37340ec76a11SDavid Howells while (len) { 37350ec76a11SDavid Howells int bytes, ret, offset; 37360ec76a11SDavid Howells void *maddr; 373728b2ee20SRik van Riel struct page *page = NULL; 37380ec76a11SDavid Howells 37390ec76a11SDavid Howells ret = get_user_pages(tsk, mm, addr, 1, 37400ec76a11SDavid Howells write, 1, &page, &vma); 374128b2ee20SRik van Riel if (ret <= 0) { 374228b2ee20SRik van Riel /* 374328b2ee20SRik van Riel * Check if this is a VM_IO | VM_PFNMAP VMA, which 374428b2ee20SRik van Riel * we can access using slightly different code. 374528b2ee20SRik van Riel */ 374628b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT 374728b2ee20SRik van Riel vma = find_vma(mm, addr); 3748fe936dfcSMichael Ellerman if (!vma || vma->vm_start > addr) 37490ec76a11SDavid Howells break; 375028b2ee20SRik van Riel if (vma->vm_ops && vma->vm_ops->access) 375128b2ee20SRik van Riel ret = vma->vm_ops->access(vma, addr, buf, 375228b2ee20SRik van Riel len, write); 375328b2ee20SRik van Riel if (ret <= 0) 375428b2ee20SRik van Riel #endif 375528b2ee20SRik van Riel break; 375628b2ee20SRik van Riel bytes = ret; 375728b2ee20SRik van Riel } else { 37580ec76a11SDavid Howells bytes = len; 37590ec76a11SDavid Howells offset = addr & (PAGE_SIZE-1); 37600ec76a11SDavid Howells if (bytes > PAGE_SIZE-offset) 37610ec76a11SDavid Howells bytes = PAGE_SIZE-offset; 37620ec76a11SDavid Howells 37630ec76a11SDavid Howells maddr = kmap(page); 37640ec76a11SDavid Howells if (write) { 37650ec76a11SDavid Howells copy_to_user_page(vma, page, addr, 37660ec76a11SDavid Howells maddr + offset, buf, bytes); 37670ec76a11SDavid Howells set_page_dirty_lock(page); 37680ec76a11SDavid Howells } else { 37690ec76a11SDavid Howells copy_from_user_page(vma, page, addr, 37700ec76a11SDavid Howells buf, maddr + offset, bytes); 37710ec76a11SDavid Howells } 37720ec76a11SDavid Howells kunmap(page); 37730ec76a11SDavid Howells page_cache_release(page); 377428b2ee20SRik van Riel } 37750ec76a11SDavid Howells len -= bytes; 37760ec76a11SDavid Howells buf += bytes; 37770ec76a11SDavid Howells addr += bytes; 37780ec76a11SDavid Howells } 37790ec76a11SDavid Howells up_read(&mm->mmap_sem); 37800ec76a11SDavid Howells 37810ec76a11SDavid Howells return buf - old_buf; 37820ec76a11SDavid Howells } 378303252919SAndi Kleen 37845ddd36b9SStephen Wilson /** 3785ae91dbfcSRandy Dunlap * access_remote_vm - access another process' address space 37865ddd36b9SStephen Wilson * @mm: the mm_struct of the target address space 37875ddd36b9SStephen Wilson * @addr: start address to access 37885ddd36b9SStephen Wilson * @buf: source or destination buffer 37895ddd36b9SStephen Wilson * @len: number of bytes to transfer 37905ddd36b9SStephen Wilson * @write: whether the access is a write 37915ddd36b9SStephen Wilson * 37925ddd36b9SStephen Wilson * The caller must hold a reference on @mm. 37935ddd36b9SStephen Wilson */ 37945ddd36b9SStephen Wilson int access_remote_vm(struct mm_struct *mm, unsigned long addr, 37955ddd36b9SStephen Wilson void *buf, int len, int write) 37965ddd36b9SStephen Wilson { 37975ddd36b9SStephen Wilson return __access_remote_vm(NULL, mm, addr, buf, len, write); 37985ddd36b9SStephen Wilson } 37995ddd36b9SStephen Wilson 380003252919SAndi Kleen /* 3801206cb636SStephen Wilson * Access another process' address space. 3802206cb636SStephen Wilson * Source/target buffer must be kernel space, 3803206cb636SStephen Wilson * Do not walk the page table directly, use get_user_pages 3804206cb636SStephen Wilson */ 3805206cb636SStephen Wilson int access_process_vm(struct task_struct *tsk, unsigned long addr, 3806206cb636SStephen Wilson void *buf, int len, int write) 3807206cb636SStephen Wilson { 3808206cb636SStephen Wilson struct mm_struct *mm; 3809206cb636SStephen Wilson int ret; 3810206cb636SStephen Wilson 3811206cb636SStephen Wilson mm = get_task_mm(tsk); 3812206cb636SStephen Wilson if (!mm) 3813206cb636SStephen Wilson return 0; 3814206cb636SStephen Wilson 3815206cb636SStephen Wilson ret = __access_remote_vm(tsk, mm, addr, buf, len, write); 3816206cb636SStephen Wilson mmput(mm); 3817206cb636SStephen Wilson 3818206cb636SStephen Wilson return ret; 3819206cb636SStephen Wilson } 3820206cb636SStephen Wilson 382103252919SAndi Kleen /* 382203252919SAndi Kleen * Print the name of a VMA. 382303252919SAndi Kleen */ 382403252919SAndi Kleen void print_vma_addr(char *prefix, unsigned long ip) 382503252919SAndi Kleen { 382603252919SAndi Kleen struct mm_struct *mm = current->mm; 382703252919SAndi Kleen struct vm_area_struct *vma; 382803252919SAndi Kleen 3829e8bff74aSIngo Molnar /* 3830e8bff74aSIngo Molnar * Do not print if we are in atomic 3831e8bff74aSIngo Molnar * contexts (in exception stacks, etc.): 3832e8bff74aSIngo Molnar */ 3833e8bff74aSIngo Molnar if (preempt_count()) 3834e8bff74aSIngo Molnar return; 3835e8bff74aSIngo Molnar 383603252919SAndi Kleen down_read(&mm->mmap_sem); 383703252919SAndi Kleen vma = find_vma(mm, ip); 383803252919SAndi Kleen if (vma && vma->vm_file) { 383903252919SAndi Kleen struct file *f = vma->vm_file; 384003252919SAndi Kleen char *buf = (char *)__get_free_page(GFP_KERNEL); 384103252919SAndi Kleen if (buf) { 384203252919SAndi Kleen char *p, *s; 384303252919SAndi Kleen 3844cf28b486SJan Blunck p = d_path(&f->f_path, buf, PAGE_SIZE); 384503252919SAndi Kleen if (IS_ERR(p)) 384603252919SAndi Kleen p = "?"; 384703252919SAndi Kleen s = strrchr(p, '/'); 384803252919SAndi Kleen if (s) 384903252919SAndi Kleen p = s+1; 385003252919SAndi Kleen printk("%s%s[%lx+%lx]", prefix, p, 385103252919SAndi Kleen vma->vm_start, 385203252919SAndi Kleen vma->vm_end - vma->vm_start); 385303252919SAndi Kleen free_page((unsigned long)buf); 385403252919SAndi Kleen } 385503252919SAndi Kleen } 385603252919SAndi Kleen up_read(¤t->mm->mmap_sem); 385703252919SAndi Kleen } 38583ee1afa3SNick Piggin 38593ee1afa3SNick Piggin #ifdef CONFIG_PROVE_LOCKING 38603ee1afa3SNick Piggin void might_fault(void) 38613ee1afa3SNick Piggin { 386295156f00SPeter Zijlstra /* 386395156f00SPeter Zijlstra * Some code (nfs/sunrpc) uses socket ops on kernel memory while 386495156f00SPeter Zijlstra * holding the mmap_sem, this is safe because kernel memory doesn't 386595156f00SPeter Zijlstra * get paged out, therefore we'll never actually fault, and the 386695156f00SPeter Zijlstra * below annotations will generate false positives. 386795156f00SPeter Zijlstra */ 386895156f00SPeter Zijlstra if (segment_eq(get_fs(), KERNEL_DS)) 386995156f00SPeter Zijlstra return; 387095156f00SPeter Zijlstra 38713ee1afa3SNick Piggin might_sleep(); 38723ee1afa3SNick Piggin /* 38733ee1afa3SNick Piggin * it would be nicer only to annotate paths which are not under 38743ee1afa3SNick Piggin * pagefault_disable, however that requires a larger audit and 38753ee1afa3SNick Piggin * providing helpers like get_user_atomic. 38763ee1afa3SNick Piggin */ 38773ee1afa3SNick Piggin if (!in_atomic() && current->mm) 38783ee1afa3SNick Piggin might_lock_read(¤t->mm->mmap_sem); 38793ee1afa3SNick Piggin } 38803ee1afa3SNick Piggin EXPORT_SYMBOL(might_fault); 38813ee1afa3SNick Piggin #endif 388247ad8475SAndrea Arcangeli 388347ad8475SAndrea Arcangeli #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS) 388447ad8475SAndrea Arcangeli static void clear_gigantic_page(struct page *page, 388547ad8475SAndrea Arcangeli unsigned long addr, 388647ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 388747ad8475SAndrea Arcangeli { 388847ad8475SAndrea Arcangeli int i; 388947ad8475SAndrea Arcangeli struct page *p = page; 389047ad8475SAndrea Arcangeli 389147ad8475SAndrea Arcangeli might_sleep(); 389247ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; 389347ad8475SAndrea Arcangeli i++, p = mem_map_next(p, page, i)) { 389447ad8475SAndrea Arcangeli cond_resched(); 389547ad8475SAndrea Arcangeli clear_user_highpage(p, addr + i * PAGE_SIZE); 389647ad8475SAndrea Arcangeli } 389747ad8475SAndrea Arcangeli } 389847ad8475SAndrea Arcangeli void clear_huge_page(struct page *page, 389947ad8475SAndrea Arcangeli unsigned long addr, unsigned int pages_per_huge_page) 390047ad8475SAndrea Arcangeli { 390147ad8475SAndrea Arcangeli int i; 390247ad8475SAndrea Arcangeli 390347ad8475SAndrea Arcangeli if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { 390447ad8475SAndrea Arcangeli clear_gigantic_page(page, addr, pages_per_huge_page); 390547ad8475SAndrea Arcangeli return; 390647ad8475SAndrea Arcangeli } 390747ad8475SAndrea Arcangeli 390847ad8475SAndrea Arcangeli might_sleep(); 390947ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; i++) { 391047ad8475SAndrea Arcangeli cond_resched(); 391147ad8475SAndrea Arcangeli clear_user_highpage(page + i, addr + i * PAGE_SIZE); 391247ad8475SAndrea Arcangeli } 391347ad8475SAndrea Arcangeli } 391447ad8475SAndrea Arcangeli 391547ad8475SAndrea Arcangeli static void copy_user_gigantic_page(struct page *dst, struct page *src, 391647ad8475SAndrea Arcangeli unsigned long addr, 391747ad8475SAndrea Arcangeli struct vm_area_struct *vma, 391847ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 391947ad8475SAndrea Arcangeli { 392047ad8475SAndrea Arcangeli int i; 392147ad8475SAndrea Arcangeli struct page *dst_base = dst; 392247ad8475SAndrea Arcangeli struct page *src_base = src; 392347ad8475SAndrea Arcangeli 392447ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; ) { 392547ad8475SAndrea Arcangeli cond_resched(); 392647ad8475SAndrea Arcangeli copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma); 392747ad8475SAndrea Arcangeli 392847ad8475SAndrea Arcangeli i++; 392947ad8475SAndrea Arcangeli dst = mem_map_next(dst, dst_base, i); 393047ad8475SAndrea Arcangeli src = mem_map_next(src, src_base, i); 393147ad8475SAndrea Arcangeli } 393247ad8475SAndrea Arcangeli } 393347ad8475SAndrea Arcangeli 393447ad8475SAndrea Arcangeli void copy_user_huge_page(struct page *dst, struct page *src, 393547ad8475SAndrea Arcangeli unsigned long addr, struct vm_area_struct *vma, 393647ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 393747ad8475SAndrea Arcangeli { 393847ad8475SAndrea Arcangeli int i; 393947ad8475SAndrea Arcangeli 394047ad8475SAndrea Arcangeli if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { 394147ad8475SAndrea Arcangeli copy_user_gigantic_page(dst, src, addr, vma, 394247ad8475SAndrea Arcangeli pages_per_huge_page); 394347ad8475SAndrea Arcangeli return; 394447ad8475SAndrea Arcangeli } 394547ad8475SAndrea Arcangeli 394647ad8475SAndrea Arcangeli might_sleep(); 394747ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; i++) { 394847ad8475SAndrea Arcangeli cond_resched(); 394947ad8475SAndrea Arcangeli copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma); 395047ad8475SAndrea Arcangeli } 395147ad8475SAndrea Arcangeli } 395247ad8475SAndrea Arcangeli #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */ 3953