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> 50b95f1b31SPaul Gortmaker #include <linux/export.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 128ea48cf78SDavid Rientjes void sync_mm_rss(struct mm_struct *mm) 12934e55232SKAMEZAWA Hiroyuki { 13034e55232SKAMEZAWA Hiroyuki int i; 13134e55232SKAMEZAWA Hiroyuki 13234e55232SKAMEZAWA Hiroyuki for (i = 0; i < NR_MM_COUNTERS; i++) { 13305af2e10SDavid Rientjes if (current->rss_stat.count[i]) { 13405af2e10SDavid Rientjes add_mm_counter(mm, i, current->rss_stat.count[i]); 13505af2e10SDavid Rientjes current->rss_stat.count[i] = 0; 13634e55232SKAMEZAWA Hiroyuki } 13734e55232SKAMEZAWA Hiroyuki } 13805af2e10SDavid Rientjes current->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)) 160ea48cf78SDavid Rientjes sync_mm_rss(task->mm); 16134e55232SKAMEZAWA Hiroyuki } 1629547d01bSPeter Zijlstra #else /* SPLIT_RSS_COUNTING */ 16334e55232SKAMEZAWA Hiroyuki 16434e55232SKAMEZAWA Hiroyuki #define inc_mm_counter_fast(mm, member) inc_mm_counter(mm, member) 16534e55232SKAMEZAWA Hiroyuki #define dec_mm_counter_fast(mm, member) dec_mm_counter(mm, member) 16634e55232SKAMEZAWA Hiroyuki 16734e55232SKAMEZAWA Hiroyuki static void check_sync_rss_stat(struct task_struct *task) 16834e55232SKAMEZAWA Hiroyuki { 16934e55232SKAMEZAWA Hiroyuki } 17034e55232SKAMEZAWA Hiroyuki 1719547d01bSPeter Zijlstra #endif /* SPLIT_RSS_COUNTING */ 1729547d01bSPeter Zijlstra 1739547d01bSPeter Zijlstra #ifdef HAVE_GENERIC_MMU_GATHER 1749547d01bSPeter Zijlstra 1759547d01bSPeter Zijlstra static int tlb_next_batch(struct mmu_gather *tlb) 1769547d01bSPeter Zijlstra { 1779547d01bSPeter Zijlstra struct mmu_gather_batch *batch; 1789547d01bSPeter Zijlstra 1799547d01bSPeter Zijlstra batch = tlb->active; 1809547d01bSPeter Zijlstra if (batch->next) { 1819547d01bSPeter Zijlstra tlb->active = batch->next; 1829547d01bSPeter Zijlstra return 1; 1839547d01bSPeter Zijlstra } 1849547d01bSPeter Zijlstra 1859547d01bSPeter Zijlstra batch = (void *)__get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0); 1869547d01bSPeter Zijlstra if (!batch) 1879547d01bSPeter Zijlstra return 0; 1889547d01bSPeter Zijlstra 1899547d01bSPeter Zijlstra batch->next = NULL; 1909547d01bSPeter Zijlstra batch->nr = 0; 1919547d01bSPeter Zijlstra batch->max = MAX_GATHER_BATCH; 1929547d01bSPeter Zijlstra 1939547d01bSPeter Zijlstra tlb->active->next = batch; 1949547d01bSPeter Zijlstra tlb->active = batch; 1959547d01bSPeter Zijlstra 1969547d01bSPeter Zijlstra return 1; 1979547d01bSPeter Zijlstra } 1989547d01bSPeter Zijlstra 1999547d01bSPeter Zijlstra /* tlb_gather_mmu 2009547d01bSPeter Zijlstra * Called to initialize an (on-stack) mmu_gather structure for page-table 2019547d01bSPeter Zijlstra * tear-down from @mm. The @fullmm argument is used when @mm is without 2029547d01bSPeter Zijlstra * users and we're going to destroy the full address space (exit/execve). 2039547d01bSPeter Zijlstra */ 2049547d01bSPeter Zijlstra void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, bool fullmm) 2059547d01bSPeter Zijlstra { 2069547d01bSPeter Zijlstra tlb->mm = mm; 2079547d01bSPeter Zijlstra 2089547d01bSPeter Zijlstra tlb->fullmm = fullmm; 2099547d01bSPeter Zijlstra tlb->need_flush = 0; 2109547d01bSPeter Zijlstra tlb->fast_mode = (num_possible_cpus() == 1); 2119547d01bSPeter Zijlstra tlb->local.next = NULL; 2129547d01bSPeter Zijlstra tlb->local.nr = 0; 2139547d01bSPeter Zijlstra tlb->local.max = ARRAY_SIZE(tlb->__pages); 2149547d01bSPeter Zijlstra tlb->active = &tlb->local; 2159547d01bSPeter Zijlstra 2169547d01bSPeter Zijlstra #ifdef CONFIG_HAVE_RCU_TABLE_FREE 2179547d01bSPeter Zijlstra tlb->batch = NULL; 21834e55232SKAMEZAWA Hiroyuki #endif 2199547d01bSPeter Zijlstra } 2209547d01bSPeter Zijlstra 2219547d01bSPeter Zijlstra void tlb_flush_mmu(struct mmu_gather *tlb) 2229547d01bSPeter Zijlstra { 2239547d01bSPeter Zijlstra struct mmu_gather_batch *batch; 2249547d01bSPeter Zijlstra 2259547d01bSPeter Zijlstra if (!tlb->need_flush) 2269547d01bSPeter Zijlstra return; 2279547d01bSPeter Zijlstra tlb->need_flush = 0; 2289547d01bSPeter Zijlstra tlb_flush(tlb); 2299547d01bSPeter Zijlstra #ifdef CONFIG_HAVE_RCU_TABLE_FREE 2309547d01bSPeter Zijlstra tlb_table_flush(tlb); 2319547d01bSPeter Zijlstra #endif 2329547d01bSPeter Zijlstra 2339547d01bSPeter Zijlstra if (tlb_fast_mode(tlb)) 2349547d01bSPeter Zijlstra return; 2359547d01bSPeter Zijlstra 2369547d01bSPeter Zijlstra for (batch = &tlb->local; batch; batch = batch->next) { 2379547d01bSPeter Zijlstra free_pages_and_swap_cache(batch->pages, batch->nr); 2389547d01bSPeter Zijlstra batch->nr = 0; 2399547d01bSPeter Zijlstra } 2409547d01bSPeter Zijlstra tlb->active = &tlb->local; 2419547d01bSPeter Zijlstra } 2429547d01bSPeter Zijlstra 2439547d01bSPeter Zijlstra /* tlb_finish_mmu 2449547d01bSPeter Zijlstra * Called at the end of the shootdown operation to free up any resources 2459547d01bSPeter Zijlstra * that were required. 2469547d01bSPeter Zijlstra */ 2479547d01bSPeter Zijlstra void tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end) 2489547d01bSPeter Zijlstra { 2499547d01bSPeter Zijlstra struct mmu_gather_batch *batch, *next; 2509547d01bSPeter Zijlstra 2519547d01bSPeter Zijlstra tlb_flush_mmu(tlb); 2529547d01bSPeter Zijlstra 2539547d01bSPeter Zijlstra /* keep the page table cache within bounds */ 2549547d01bSPeter Zijlstra check_pgt_cache(); 2559547d01bSPeter Zijlstra 2569547d01bSPeter Zijlstra for (batch = tlb->local.next; batch; batch = next) { 2579547d01bSPeter Zijlstra next = batch->next; 2589547d01bSPeter Zijlstra free_pages((unsigned long)batch, 0); 2599547d01bSPeter Zijlstra } 2609547d01bSPeter Zijlstra tlb->local.next = NULL; 2619547d01bSPeter Zijlstra } 2629547d01bSPeter Zijlstra 2639547d01bSPeter Zijlstra /* __tlb_remove_page 2649547d01bSPeter Zijlstra * Must perform the equivalent to __free_pte(pte_get_and_clear(ptep)), while 2659547d01bSPeter Zijlstra * handling the additional races in SMP caused by other CPUs caching valid 2669547d01bSPeter Zijlstra * mappings in their TLBs. Returns the number of free page slots left. 2679547d01bSPeter Zijlstra * When out of page slots we must call tlb_flush_mmu(). 2689547d01bSPeter Zijlstra */ 2699547d01bSPeter Zijlstra int __tlb_remove_page(struct mmu_gather *tlb, struct page *page) 2709547d01bSPeter Zijlstra { 2719547d01bSPeter Zijlstra struct mmu_gather_batch *batch; 2729547d01bSPeter Zijlstra 273f21760b1SShaohua Li VM_BUG_ON(!tlb->need_flush); 2749547d01bSPeter Zijlstra 2759547d01bSPeter Zijlstra if (tlb_fast_mode(tlb)) { 2769547d01bSPeter Zijlstra free_page_and_swap_cache(page); 2779547d01bSPeter Zijlstra return 1; /* avoid calling tlb_flush_mmu() */ 2789547d01bSPeter Zijlstra } 2799547d01bSPeter Zijlstra 2809547d01bSPeter Zijlstra batch = tlb->active; 2819547d01bSPeter Zijlstra batch->pages[batch->nr++] = page; 2829547d01bSPeter Zijlstra if (batch->nr == batch->max) { 2839547d01bSPeter Zijlstra if (!tlb_next_batch(tlb)) 2849547d01bSPeter Zijlstra return 0; 2850b43c3aaSShaohua Li batch = tlb->active; 2869547d01bSPeter Zijlstra } 2879547d01bSPeter Zijlstra VM_BUG_ON(batch->nr > batch->max); 2889547d01bSPeter Zijlstra 2899547d01bSPeter Zijlstra return batch->max - batch->nr; 2909547d01bSPeter Zijlstra } 2919547d01bSPeter Zijlstra 2929547d01bSPeter Zijlstra #endif /* HAVE_GENERIC_MMU_GATHER */ 29334e55232SKAMEZAWA Hiroyuki 29426723911SPeter Zijlstra #ifdef CONFIG_HAVE_RCU_TABLE_FREE 29526723911SPeter Zijlstra 29626723911SPeter Zijlstra /* 29726723911SPeter Zijlstra * See the comment near struct mmu_table_batch. 29826723911SPeter Zijlstra */ 29926723911SPeter Zijlstra 30026723911SPeter Zijlstra static void tlb_remove_table_smp_sync(void *arg) 30126723911SPeter Zijlstra { 30226723911SPeter Zijlstra /* Simply deliver the interrupt */ 30326723911SPeter Zijlstra } 30426723911SPeter Zijlstra 30526723911SPeter Zijlstra static void tlb_remove_table_one(void *table) 30626723911SPeter Zijlstra { 30726723911SPeter Zijlstra /* 30826723911SPeter Zijlstra * This isn't an RCU grace period and hence the page-tables cannot be 30926723911SPeter Zijlstra * assumed to be actually RCU-freed. 31026723911SPeter Zijlstra * 31126723911SPeter Zijlstra * It is however sufficient for software page-table walkers that rely on 31226723911SPeter Zijlstra * IRQ disabling. See the comment near struct mmu_table_batch. 31326723911SPeter Zijlstra */ 31426723911SPeter Zijlstra smp_call_function(tlb_remove_table_smp_sync, NULL, 1); 31526723911SPeter Zijlstra __tlb_remove_table(table); 31626723911SPeter Zijlstra } 31726723911SPeter Zijlstra 31826723911SPeter Zijlstra static void tlb_remove_table_rcu(struct rcu_head *head) 31926723911SPeter Zijlstra { 32026723911SPeter Zijlstra struct mmu_table_batch *batch; 32126723911SPeter Zijlstra int i; 32226723911SPeter Zijlstra 32326723911SPeter Zijlstra batch = container_of(head, struct mmu_table_batch, rcu); 32426723911SPeter Zijlstra 32526723911SPeter Zijlstra for (i = 0; i < batch->nr; i++) 32626723911SPeter Zijlstra __tlb_remove_table(batch->tables[i]); 32726723911SPeter Zijlstra 32826723911SPeter Zijlstra free_page((unsigned long)batch); 32926723911SPeter Zijlstra } 33026723911SPeter Zijlstra 33126723911SPeter Zijlstra void tlb_table_flush(struct mmu_gather *tlb) 33226723911SPeter Zijlstra { 33326723911SPeter Zijlstra struct mmu_table_batch **batch = &tlb->batch; 33426723911SPeter Zijlstra 33526723911SPeter Zijlstra if (*batch) { 33626723911SPeter Zijlstra call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu); 33726723911SPeter Zijlstra *batch = NULL; 33826723911SPeter Zijlstra } 33926723911SPeter Zijlstra } 34026723911SPeter Zijlstra 34126723911SPeter Zijlstra void tlb_remove_table(struct mmu_gather *tlb, void *table) 34226723911SPeter Zijlstra { 34326723911SPeter Zijlstra struct mmu_table_batch **batch = &tlb->batch; 34426723911SPeter Zijlstra 34526723911SPeter Zijlstra tlb->need_flush = 1; 34626723911SPeter Zijlstra 34726723911SPeter Zijlstra /* 34826723911SPeter Zijlstra * When there's less then two users of this mm there cannot be a 34926723911SPeter Zijlstra * concurrent page-table walk. 35026723911SPeter Zijlstra */ 35126723911SPeter Zijlstra if (atomic_read(&tlb->mm->mm_users) < 2) { 35226723911SPeter Zijlstra __tlb_remove_table(table); 35326723911SPeter Zijlstra return; 35426723911SPeter Zijlstra } 35526723911SPeter Zijlstra 35626723911SPeter Zijlstra if (*batch == NULL) { 35726723911SPeter Zijlstra *batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN); 35826723911SPeter Zijlstra if (*batch == NULL) { 35926723911SPeter Zijlstra tlb_remove_table_one(table); 36026723911SPeter Zijlstra return; 36126723911SPeter Zijlstra } 36226723911SPeter Zijlstra (*batch)->nr = 0; 36326723911SPeter Zijlstra } 36426723911SPeter Zijlstra (*batch)->tables[(*batch)->nr++] = table; 36526723911SPeter Zijlstra if ((*batch)->nr == MAX_TABLE_BATCH) 36626723911SPeter Zijlstra tlb_table_flush(tlb); 36726723911SPeter Zijlstra } 36826723911SPeter Zijlstra 3699547d01bSPeter Zijlstra #endif /* CONFIG_HAVE_RCU_TABLE_FREE */ 37026723911SPeter Zijlstra 3711da177e4SLinus Torvalds /* 3721da177e4SLinus Torvalds * If a p?d_bad entry is found while walking page tables, report 3731da177e4SLinus Torvalds * the error, before resetting entry to p?d_none. Usually (but 3741da177e4SLinus Torvalds * very seldom) called out from the p?d_none_or_clear_bad macros. 3751da177e4SLinus Torvalds */ 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds void pgd_clear_bad(pgd_t *pgd) 3781da177e4SLinus Torvalds { 3791da177e4SLinus Torvalds pgd_ERROR(*pgd); 3801da177e4SLinus Torvalds pgd_clear(pgd); 3811da177e4SLinus Torvalds } 3821da177e4SLinus Torvalds 3831da177e4SLinus Torvalds void pud_clear_bad(pud_t *pud) 3841da177e4SLinus Torvalds { 3851da177e4SLinus Torvalds pud_ERROR(*pud); 3861da177e4SLinus Torvalds pud_clear(pud); 3871da177e4SLinus Torvalds } 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds void pmd_clear_bad(pmd_t *pmd) 3901da177e4SLinus Torvalds { 3911da177e4SLinus Torvalds pmd_ERROR(*pmd); 3921da177e4SLinus Torvalds pmd_clear(pmd); 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds /* 3961da177e4SLinus Torvalds * Note: this doesn't free the actual pages themselves. That 3971da177e4SLinus Torvalds * has been handled earlier when unmapping all the memory regions. 3981da177e4SLinus Torvalds */ 3999e1b32caSBenjamin Herrenschmidt static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd, 4009e1b32caSBenjamin Herrenschmidt unsigned long addr) 4011da177e4SLinus Torvalds { 4022f569afdSMartin Schwidefsky pgtable_t token = pmd_pgtable(*pmd); 4031da177e4SLinus Torvalds pmd_clear(pmd); 4049e1b32caSBenjamin Herrenschmidt pte_free_tlb(tlb, token, addr); 4051da177e4SLinus Torvalds tlb->mm->nr_ptes--; 4061da177e4SLinus Torvalds } 4071da177e4SLinus Torvalds 408e0da382cSHugh Dickins static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud, 409e0da382cSHugh Dickins unsigned long addr, unsigned long end, 410e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 4111da177e4SLinus Torvalds { 4121da177e4SLinus Torvalds pmd_t *pmd; 4131da177e4SLinus Torvalds unsigned long next; 414e0da382cSHugh Dickins unsigned long start; 4151da177e4SLinus Torvalds 416e0da382cSHugh Dickins start = addr; 4171da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 4181da177e4SLinus Torvalds do { 4191da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 4201da177e4SLinus Torvalds if (pmd_none_or_clear_bad(pmd)) 4211da177e4SLinus Torvalds continue; 4229e1b32caSBenjamin Herrenschmidt free_pte_range(tlb, pmd, addr); 4231da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 4241da177e4SLinus Torvalds 425e0da382cSHugh Dickins start &= PUD_MASK; 426e0da382cSHugh Dickins if (start < floor) 427e0da382cSHugh Dickins return; 428e0da382cSHugh Dickins if (ceiling) { 429e0da382cSHugh Dickins ceiling &= PUD_MASK; 430e0da382cSHugh Dickins if (!ceiling) 431e0da382cSHugh Dickins return; 4321da177e4SLinus Torvalds } 433e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 434e0da382cSHugh Dickins return; 435e0da382cSHugh Dickins 436e0da382cSHugh Dickins pmd = pmd_offset(pud, start); 437e0da382cSHugh Dickins pud_clear(pud); 4389e1b32caSBenjamin Herrenschmidt pmd_free_tlb(tlb, pmd, start); 4391da177e4SLinus Torvalds } 4401da177e4SLinus Torvalds 441e0da382cSHugh Dickins static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd, 442e0da382cSHugh Dickins unsigned long addr, unsigned long end, 443e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 4441da177e4SLinus Torvalds { 4451da177e4SLinus Torvalds pud_t *pud; 4461da177e4SLinus Torvalds unsigned long next; 447e0da382cSHugh Dickins unsigned long start; 4481da177e4SLinus Torvalds 449e0da382cSHugh Dickins start = addr; 4501da177e4SLinus Torvalds pud = pud_offset(pgd, addr); 4511da177e4SLinus Torvalds do { 4521da177e4SLinus Torvalds next = pud_addr_end(addr, end); 4531da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 4541da177e4SLinus Torvalds continue; 455e0da382cSHugh Dickins free_pmd_range(tlb, pud, addr, next, floor, ceiling); 4561da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 4571da177e4SLinus Torvalds 458e0da382cSHugh Dickins start &= PGDIR_MASK; 459e0da382cSHugh Dickins if (start < floor) 460e0da382cSHugh Dickins return; 461e0da382cSHugh Dickins if (ceiling) { 462e0da382cSHugh Dickins ceiling &= PGDIR_MASK; 463e0da382cSHugh Dickins if (!ceiling) 464e0da382cSHugh Dickins return; 4651da177e4SLinus Torvalds } 466e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 467e0da382cSHugh Dickins return; 468e0da382cSHugh Dickins 469e0da382cSHugh Dickins pud = pud_offset(pgd, start); 470e0da382cSHugh Dickins pgd_clear(pgd); 4719e1b32caSBenjamin Herrenschmidt pud_free_tlb(tlb, pud, start); 4721da177e4SLinus Torvalds } 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds /* 475e0da382cSHugh Dickins * This function frees user-level page tables of a process. 476e0da382cSHugh Dickins * 4771da177e4SLinus Torvalds * Must be called with pagetable lock held. 4781da177e4SLinus Torvalds */ 47942b77728SJan Beulich void free_pgd_range(struct mmu_gather *tlb, 480e0da382cSHugh Dickins unsigned long addr, unsigned long end, 481e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 4821da177e4SLinus Torvalds { 4831da177e4SLinus Torvalds pgd_t *pgd; 4841da177e4SLinus Torvalds unsigned long next; 4851da177e4SLinus Torvalds 486e0da382cSHugh Dickins /* 487e0da382cSHugh Dickins * The next few lines have given us lots of grief... 488e0da382cSHugh Dickins * 489e0da382cSHugh Dickins * Why are we testing PMD* at this top level? Because often 490e0da382cSHugh Dickins * there will be no work to do at all, and we'd prefer not to 491e0da382cSHugh Dickins * go all the way down to the bottom just to discover that. 492e0da382cSHugh Dickins * 493e0da382cSHugh Dickins * Why all these "- 1"s? Because 0 represents both the bottom 494e0da382cSHugh Dickins * of the address space and the top of it (using -1 for the 495e0da382cSHugh Dickins * top wouldn't help much: the masks would do the wrong thing). 496e0da382cSHugh Dickins * The rule is that addr 0 and floor 0 refer to the bottom of 497e0da382cSHugh Dickins * the address space, but end 0 and ceiling 0 refer to the top 498e0da382cSHugh Dickins * Comparisons need to use "end - 1" and "ceiling - 1" (though 499e0da382cSHugh Dickins * that end 0 case should be mythical). 500e0da382cSHugh Dickins * 501e0da382cSHugh Dickins * Wherever addr is brought up or ceiling brought down, we must 502e0da382cSHugh Dickins * be careful to reject "the opposite 0" before it confuses the 503e0da382cSHugh Dickins * subsequent tests. But what about where end is brought down 504e0da382cSHugh Dickins * by PMD_SIZE below? no, end can't go down to 0 there. 505e0da382cSHugh Dickins * 506e0da382cSHugh Dickins * Whereas we round start (addr) and ceiling down, by different 507e0da382cSHugh Dickins * masks at different levels, in order to test whether a table 508e0da382cSHugh Dickins * now has no other vmas using it, so can be freed, we don't 509e0da382cSHugh Dickins * bother to round floor or end up - the tests don't need that. 510e0da382cSHugh Dickins */ 511e0da382cSHugh Dickins 512e0da382cSHugh Dickins addr &= PMD_MASK; 513e0da382cSHugh Dickins if (addr < floor) { 514e0da382cSHugh Dickins addr += PMD_SIZE; 515e0da382cSHugh Dickins if (!addr) 516e0da382cSHugh Dickins return; 517e0da382cSHugh Dickins } 518e0da382cSHugh Dickins if (ceiling) { 519e0da382cSHugh Dickins ceiling &= PMD_MASK; 520e0da382cSHugh Dickins if (!ceiling) 521e0da382cSHugh Dickins return; 522e0da382cSHugh Dickins } 523e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 524e0da382cSHugh Dickins end -= PMD_SIZE; 525e0da382cSHugh Dickins if (addr > end - 1) 526e0da382cSHugh Dickins return; 527e0da382cSHugh Dickins 52842b77728SJan Beulich pgd = pgd_offset(tlb->mm, addr); 5291da177e4SLinus Torvalds do { 5301da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 5311da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 5321da177e4SLinus Torvalds continue; 53342b77728SJan Beulich free_pud_range(tlb, pgd, addr, next, floor, ceiling); 5341da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 535e0da382cSHugh Dickins } 536e0da382cSHugh Dickins 53742b77728SJan Beulich void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma, 538e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 539e0da382cSHugh Dickins { 540e0da382cSHugh Dickins while (vma) { 541e0da382cSHugh Dickins struct vm_area_struct *next = vma->vm_next; 542e0da382cSHugh Dickins unsigned long addr = vma->vm_start; 543e0da382cSHugh Dickins 5448f4f8c16SHugh Dickins /* 54525d9e2d1Snpiggin@suse.de * Hide vma from rmap and truncate_pagecache before freeing 54625d9e2d1Snpiggin@suse.de * pgtables 5478f4f8c16SHugh Dickins */ 5485beb4930SRik van Riel unlink_anon_vmas(vma); 5498f4f8c16SHugh Dickins unlink_file_vma(vma); 5508f4f8c16SHugh Dickins 5519da61aefSDavid Gibson if (is_vm_hugetlb_page(vma)) { 5523bf5ee95SHugh Dickins hugetlb_free_pgd_range(tlb, addr, vma->vm_end, 5533bf5ee95SHugh Dickins floor, next? next->vm_start: ceiling); 5543bf5ee95SHugh Dickins } else { 5553bf5ee95SHugh Dickins /* 5563bf5ee95SHugh Dickins * Optimization: gather nearby vmas into one call down 5573bf5ee95SHugh Dickins */ 5583bf5ee95SHugh Dickins while (next && next->vm_start <= vma->vm_end + PMD_SIZE 5594866920bSDavid Gibson && !is_vm_hugetlb_page(next)) { 560e0da382cSHugh Dickins vma = next; 561e0da382cSHugh Dickins next = vma->vm_next; 5625beb4930SRik van Riel unlink_anon_vmas(vma); 5638f4f8c16SHugh Dickins unlink_file_vma(vma); 564e0da382cSHugh Dickins } 5653bf5ee95SHugh Dickins free_pgd_range(tlb, addr, vma->vm_end, 566e0da382cSHugh Dickins floor, next? next->vm_start: ceiling); 5673bf5ee95SHugh Dickins } 568e0da382cSHugh Dickins vma = next; 569e0da382cSHugh Dickins } 5701da177e4SLinus Torvalds } 5711da177e4SLinus Torvalds 5728ac1f832SAndrea Arcangeli int __pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma, 5738ac1f832SAndrea Arcangeli pmd_t *pmd, unsigned long address) 5741da177e4SLinus Torvalds { 5752f569afdSMartin Schwidefsky pgtable_t new = pte_alloc_one(mm, address); 5768ac1f832SAndrea Arcangeli int wait_split_huge_page; 5771da177e4SLinus Torvalds if (!new) 5781bb3630eSHugh Dickins return -ENOMEM; 5791bb3630eSHugh Dickins 580362a61adSNick Piggin /* 581362a61adSNick Piggin * Ensure all pte setup (eg. pte page lock and page clearing) are 582362a61adSNick Piggin * visible before the pte is made visible to other CPUs by being 583362a61adSNick Piggin * put into page tables. 584362a61adSNick Piggin * 585362a61adSNick Piggin * The other side of the story is the pointer chasing in the page 586362a61adSNick Piggin * table walking code (when walking the page table without locking; 587362a61adSNick Piggin * ie. most of the time). Fortunately, these data accesses consist 588362a61adSNick Piggin * of a chain of data-dependent loads, meaning most CPUs (alpha 589362a61adSNick Piggin * being the notable exception) will already guarantee loads are 590362a61adSNick Piggin * seen in-order. See the alpha page table accessors for the 591362a61adSNick Piggin * smp_read_barrier_depends() barriers in page table walking code. 592362a61adSNick Piggin */ 593362a61adSNick Piggin smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */ 594362a61adSNick Piggin 595c74df32cSHugh Dickins spin_lock(&mm->page_table_lock); 5968ac1f832SAndrea Arcangeli wait_split_huge_page = 0; 5978ac1f832SAndrea Arcangeli if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 5981da177e4SLinus Torvalds mm->nr_ptes++; 5991da177e4SLinus Torvalds pmd_populate(mm, pmd, new); 6002f569afdSMartin Schwidefsky new = NULL; 6018ac1f832SAndrea Arcangeli } else if (unlikely(pmd_trans_splitting(*pmd))) 6028ac1f832SAndrea Arcangeli wait_split_huge_page = 1; 603c74df32cSHugh Dickins spin_unlock(&mm->page_table_lock); 6042f569afdSMartin Schwidefsky if (new) 6052f569afdSMartin Schwidefsky pte_free(mm, new); 6068ac1f832SAndrea Arcangeli if (wait_split_huge_page) 6078ac1f832SAndrea Arcangeli wait_split_huge_page(vma->anon_vma, pmd); 6081bb3630eSHugh Dickins return 0; 6091da177e4SLinus Torvalds } 6101da177e4SLinus Torvalds 6111bb3630eSHugh Dickins int __pte_alloc_kernel(pmd_t *pmd, unsigned long address) 6121da177e4SLinus Torvalds { 6131bb3630eSHugh Dickins pte_t *new = pte_alloc_one_kernel(&init_mm, address); 6141da177e4SLinus Torvalds if (!new) 6151bb3630eSHugh Dickins return -ENOMEM; 6161da177e4SLinus Torvalds 617362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 618362a61adSNick Piggin 619872fec16SHugh Dickins spin_lock(&init_mm.page_table_lock); 6208ac1f832SAndrea Arcangeli if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 621872fec16SHugh Dickins pmd_populate_kernel(&init_mm, pmd, new); 6222f569afdSMartin Schwidefsky new = NULL; 6238ac1f832SAndrea Arcangeli } else 6248ac1f832SAndrea Arcangeli VM_BUG_ON(pmd_trans_splitting(*pmd)); 625872fec16SHugh Dickins spin_unlock(&init_mm.page_table_lock); 6262f569afdSMartin Schwidefsky if (new) 6272f569afdSMartin Schwidefsky pte_free_kernel(&init_mm, new); 6281bb3630eSHugh Dickins return 0; 6291da177e4SLinus Torvalds } 6301da177e4SLinus Torvalds 631d559db08SKAMEZAWA Hiroyuki static inline void init_rss_vec(int *rss) 632ae859762SHugh Dickins { 633d559db08SKAMEZAWA Hiroyuki memset(rss, 0, sizeof(int) * NR_MM_COUNTERS); 634d559db08SKAMEZAWA Hiroyuki } 635d559db08SKAMEZAWA Hiroyuki 636d559db08SKAMEZAWA Hiroyuki static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss) 637d559db08SKAMEZAWA Hiroyuki { 638d559db08SKAMEZAWA Hiroyuki int i; 639d559db08SKAMEZAWA Hiroyuki 64034e55232SKAMEZAWA Hiroyuki if (current->mm == mm) 64105af2e10SDavid Rientjes sync_mm_rss(mm); 642d559db08SKAMEZAWA Hiroyuki for (i = 0; i < NR_MM_COUNTERS; i++) 643d559db08SKAMEZAWA Hiroyuki if (rss[i]) 644d559db08SKAMEZAWA Hiroyuki add_mm_counter(mm, i, rss[i]); 645ae859762SHugh Dickins } 646ae859762SHugh Dickins 6471da177e4SLinus Torvalds /* 6486aab341eSLinus Torvalds * This function is called to print an error when a bad pte 6496aab341eSLinus Torvalds * is found. For example, we might have a PFN-mapped pte in 6506aab341eSLinus Torvalds * a region that doesn't allow it. 651b5810039SNick Piggin * 652b5810039SNick Piggin * The calling function must still handle the error. 653b5810039SNick Piggin */ 6543dc14741SHugh Dickins static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr, 6553dc14741SHugh Dickins pte_t pte, struct page *page) 656b5810039SNick Piggin { 6573dc14741SHugh Dickins pgd_t *pgd = pgd_offset(vma->vm_mm, addr); 6583dc14741SHugh Dickins pud_t *pud = pud_offset(pgd, addr); 6593dc14741SHugh Dickins pmd_t *pmd = pmd_offset(pud, addr); 6603dc14741SHugh Dickins struct address_space *mapping; 6613dc14741SHugh Dickins pgoff_t index; 662d936cf9bSHugh Dickins static unsigned long resume; 663d936cf9bSHugh Dickins static unsigned long nr_shown; 664d936cf9bSHugh Dickins static unsigned long nr_unshown; 665d936cf9bSHugh Dickins 666d936cf9bSHugh Dickins /* 667d936cf9bSHugh Dickins * Allow a burst of 60 reports, then keep quiet for that minute; 668d936cf9bSHugh Dickins * or allow a steady drip of one report per second. 669d936cf9bSHugh Dickins */ 670d936cf9bSHugh Dickins if (nr_shown == 60) { 671d936cf9bSHugh Dickins if (time_before(jiffies, resume)) { 672d936cf9bSHugh Dickins nr_unshown++; 673d936cf9bSHugh Dickins return; 674d936cf9bSHugh Dickins } 675d936cf9bSHugh Dickins if (nr_unshown) { 6761e9e6365SHugh Dickins printk(KERN_ALERT 6771e9e6365SHugh Dickins "BUG: Bad page map: %lu messages suppressed\n", 678d936cf9bSHugh Dickins nr_unshown); 679d936cf9bSHugh Dickins nr_unshown = 0; 680d936cf9bSHugh Dickins } 681d936cf9bSHugh Dickins nr_shown = 0; 682d936cf9bSHugh Dickins } 683d936cf9bSHugh Dickins if (nr_shown++ == 0) 684d936cf9bSHugh Dickins resume = jiffies + 60 * HZ; 6853dc14741SHugh Dickins 6863dc14741SHugh Dickins mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL; 6873dc14741SHugh Dickins index = linear_page_index(vma, addr); 6883dc14741SHugh Dickins 6891e9e6365SHugh Dickins printk(KERN_ALERT 6901e9e6365SHugh Dickins "BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n", 6913dc14741SHugh Dickins current->comm, 6923dc14741SHugh Dickins (long long)pte_val(pte), (long long)pmd_val(*pmd)); 693718a3821SWu Fengguang if (page) 694718a3821SWu Fengguang dump_page(page); 6951e9e6365SHugh Dickins printk(KERN_ALERT 6963dc14741SHugh Dickins "addr:%p vm_flags:%08lx anon_vma:%p mapping:%p index:%lx\n", 6973dc14741SHugh Dickins (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index); 6983dc14741SHugh Dickins /* 6993dc14741SHugh Dickins * Choose text because data symbols depend on CONFIG_KALLSYMS_ALL=y 7003dc14741SHugh Dickins */ 7013dc14741SHugh Dickins if (vma->vm_ops) 7021e9e6365SHugh Dickins print_symbol(KERN_ALERT "vma->vm_ops->fault: %s\n", 7033dc14741SHugh Dickins (unsigned long)vma->vm_ops->fault); 7043dc14741SHugh Dickins if (vma->vm_file && vma->vm_file->f_op) 7051e9e6365SHugh Dickins print_symbol(KERN_ALERT "vma->vm_file->f_op->mmap: %s\n", 7063dc14741SHugh Dickins (unsigned long)vma->vm_file->f_op->mmap); 707b5810039SNick Piggin dump_stack(); 7083dc14741SHugh Dickins add_taint(TAINT_BAD_PAGE); 709b5810039SNick Piggin } 710b5810039SNick Piggin 711ca16d140SKOSAKI Motohiro static inline int is_cow_mapping(vm_flags_t flags) 71267121172SLinus Torvalds { 71367121172SLinus Torvalds return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; 71467121172SLinus Torvalds } 71567121172SLinus Torvalds 71662eede62SHugh Dickins #ifndef is_zero_pfn 71762eede62SHugh Dickins static inline int is_zero_pfn(unsigned long pfn) 71862eede62SHugh Dickins { 71962eede62SHugh Dickins return pfn == zero_pfn; 72062eede62SHugh Dickins } 72162eede62SHugh Dickins #endif 72262eede62SHugh Dickins 72362eede62SHugh Dickins #ifndef my_zero_pfn 72462eede62SHugh Dickins static inline unsigned long my_zero_pfn(unsigned long addr) 72562eede62SHugh Dickins { 72662eede62SHugh Dickins return zero_pfn; 72762eede62SHugh Dickins } 72862eede62SHugh Dickins #endif 72962eede62SHugh Dickins 730b5810039SNick Piggin /* 7317e675137SNick Piggin * vm_normal_page -- This function gets the "struct page" associated with a pte. 7326aab341eSLinus Torvalds * 7337e675137SNick Piggin * "Special" mappings do not wish to be associated with a "struct page" (either 7347e675137SNick Piggin * it doesn't exist, or it exists but they don't want to touch it). In this 7357e675137SNick Piggin * case, NULL is returned here. "Normal" mappings do have a struct page. 736b379d790SJared Hulbert * 7377e675137SNick Piggin * There are 2 broad cases. Firstly, an architecture may define a pte_special() 7387e675137SNick Piggin * pte bit, in which case this function is trivial. Secondly, an architecture 7397e675137SNick Piggin * may not have a spare pte bit, which requires a more complicated scheme, 7407e675137SNick Piggin * described below. 7417e675137SNick Piggin * 7427e675137SNick Piggin * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a 7437e675137SNick Piggin * special mapping (even if there are underlying and valid "struct pages"). 7447e675137SNick Piggin * COWed pages of a VM_PFNMAP are always normal. 7456aab341eSLinus Torvalds * 746b379d790SJared Hulbert * The way we recognize COWed pages within VM_PFNMAP mappings is through the 747b379d790SJared Hulbert * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit 7487e675137SNick Piggin * set, and the vm_pgoff will point to the first PFN mapped: thus every special 7497e675137SNick Piggin * mapping will always honor the rule 7506aab341eSLinus Torvalds * 7516aab341eSLinus Torvalds * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT) 7526aab341eSLinus Torvalds * 7537e675137SNick Piggin * And for normal mappings this is false. 754b379d790SJared Hulbert * 7557e675137SNick Piggin * This restricts such mappings to be a linear translation from virtual address 7567e675137SNick Piggin * to pfn. To get around this restriction, we allow arbitrary mappings so long 7577e675137SNick Piggin * as the vma is not a COW mapping; in that case, we know that all ptes are 7587e675137SNick Piggin * special (because none can have been COWed). 759b379d790SJared Hulbert * 760b379d790SJared Hulbert * 7617e675137SNick Piggin * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP. 7627e675137SNick Piggin * 763b379d790SJared Hulbert * VM_MIXEDMAP mappings can likewise contain memory with or without "struct 764b379d790SJared Hulbert * page" backing, however the difference is that _all_ pages with a struct 765b379d790SJared Hulbert * page (that is, those where pfn_valid is true) are refcounted and considered 766b379d790SJared Hulbert * normal pages by the VM. The disadvantage is that pages are refcounted 767b379d790SJared Hulbert * (which can be slower and simply not an option for some PFNMAP users). The 768b379d790SJared Hulbert * advantage is that we don't have to follow the strict linearity rule of 769b379d790SJared Hulbert * PFNMAP mappings in order to support COWable mappings. 770b379d790SJared Hulbert * 771ee498ed7SHugh Dickins */ 7727e675137SNick Piggin #ifdef __HAVE_ARCH_PTE_SPECIAL 7737e675137SNick Piggin # define HAVE_PTE_SPECIAL 1 7747e675137SNick Piggin #else 7757e675137SNick Piggin # define HAVE_PTE_SPECIAL 0 7767e675137SNick Piggin #endif 7777e675137SNick Piggin struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, 7787e675137SNick Piggin pte_t pte) 779ee498ed7SHugh Dickins { 78022b31eecSHugh Dickins unsigned long pfn = pte_pfn(pte); 7817e675137SNick Piggin 7827e675137SNick Piggin if (HAVE_PTE_SPECIAL) { 78322b31eecSHugh Dickins if (likely(!pte_special(pte))) 78422b31eecSHugh Dickins goto check_pfn; 785a13ea5b7SHugh Dickins if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)) 786a13ea5b7SHugh Dickins return NULL; 78762eede62SHugh Dickins if (!is_zero_pfn(pfn)) 78822b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 7897e675137SNick Piggin return NULL; 7907e675137SNick Piggin } 7917e675137SNick Piggin 7927e675137SNick Piggin /* !HAVE_PTE_SPECIAL case follows: */ 7937e675137SNick Piggin 794b379d790SJared Hulbert if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { 795b379d790SJared Hulbert if (vma->vm_flags & VM_MIXEDMAP) { 796b379d790SJared Hulbert if (!pfn_valid(pfn)) 797b379d790SJared Hulbert return NULL; 798b379d790SJared Hulbert goto out; 799b379d790SJared Hulbert } else { 8007e675137SNick Piggin unsigned long off; 8017e675137SNick Piggin off = (addr - vma->vm_start) >> PAGE_SHIFT; 8026aab341eSLinus Torvalds if (pfn == vma->vm_pgoff + off) 8036aab341eSLinus Torvalds return NULL; 80467121172SLinus Torvalds if (!is_cow_mapping(vma->vm_flags)) 805fb155c16SLinus Torvalds return NULL; 8066aab341eSLinus Torvalds } 807b379d790SJared Hulbert } 8086aab341eSLinus Torvalds 80962eede62SHugh Dickins if (is_zero_pfn(pfn)) 81062eede62SHugh Dickins return NULL; 81122b31eecSHugh Dickins check_pfn: 81222b31eecSHugh Dickins if (unlikely(pfn > highest_memmap_pfn)) { 81322b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 81422b31eecSHugh Dickins return NULL; 81522b31eecSHugh Dickins } 8166aab341eSLinus Torvalds 8176aab341eSLinus Torvalds /* 8187e675137SNick Piggin * NOTE! We still have PageReserved() pages in the page tables. 8197e675137SNick Piggin * eg. VDSO mappings can cause them to exist. 8206aab341eSLinus Torvalds */ 821b379d790SJared Hulbert out: 8226aab341eSLinus Torvalds return pfn_to_page(pfn); 823ee498ed7SHugh Dickins } 824ee498ed7SHugh Dickins 825ee498ed7SHugh Dickins /* 8261da177e4SLinus Torvalds * copy one vm_area from one task to the other. Assumes the page tables 8271da177e4SLinus Torvalds * already present in the new task to be cleared in the whole range 8281da177e4SLinus Torvalds * covered by this vma. 8291da177e4SLinus Torvalds */ 8301da177e4SLinus Torvalds 831570a335bSHugh Dickins static inline unsigned long 8321da177e4SLinus Torvalds copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, 833b5810039SNick Piggin pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma, 8348c103762SHugh Dickins unsigned long addr, int *rss) 8351da177e4SLinus Torvalds { 836b5810039SNick Piggin unsigned long vm_flags = vma->vm_flags; 8371da177e4SLinus Torvalds pte_t pte = *src_pte; 8381da177e4SLinus Torvalds struct page *page; 8391da177e4SLinus Torvalds 8401da177e4SLinus Torvalds /* pte contains position in swap or file, so copy. */ 8411da177e4SLinus Torvalds if (unlikely(!pte_present(pte))) { 8421da177e4SLinus Torvalds if (!pte_file(pte)) { 8430697212aSChristoph Lameter swp_entry_t entry = pte_to_swp_entry(pte); 8440697212aSChristoph Lameter 845570a335bSHugh Dickins if (swap_duplicate(entry) < 0) 846570a335bSHugh Dickins return entry.val; 847570a335bSHugh Dickins 8481da177e4SLinus Torvalds /* make sure dst_mm is on swapoff's mmlist. */ 8491da177e4SLinus Torvalds if (unlikely(list_empty(&dst_mm->mmlist))) { 8501da177e4SLinus Torvalds spin_lock(&mmlist_lock); 851f412ac08SHugh Dickins if (list_empty(&dst_mm->mmlist)) 852f412ac08SHugh Dickins list_add(&dst_mm->mmlist, 853f412ac08SHugh Dickins &src_mm->mmlist); 8541da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 8551da177e4SLinus Torvalds } 856b084d435SKAMEZAWA Hiroyuki if (likely(!non_swap_entry(entry))) 857b084d435SKAMEZAWA Hiroyuki rss[MM_SWAPENTS]++; 8589f9f1acdSKonstantin Khlebnikov else if (is_migration_entry(entry)) { 8599f9f1acdSKonstantin Khlebnikov page = migration_entry_to_page(entry); 8609f9f1acdSKonstantin Khlebnikov 8619f9f1acdSKonstantin Khlebnikov if (PageAnon(page)) 8629f9f1acdSKonstantin Khlebnikov rss[MM_ANONPAGES]++; 8639f9f1acdSKonstantin Khlebnikov else 8649f9f1acdSKonstantin Khlebnikov rss[MM_FILEPAGES]++; 8659f9f1acdSKonstantin Khlebnikov 8669f9f1acdSKonstantin Khlebnikov if (is_write_migration_entry(entry) && 8670697212aSChristoph Lameter is_cow_mapping(vm_flags)) { 8680697212aSChristoph Lameter /* 8699f9f1acdSKonstantin Khlebnikov * COW mappings require pages in both 8709f9f1acdSKonstantin Khlebnikov * parent and child to be set to read. 8710697212aSChristoph Lameter */ 8720697212aSChristoph Lameter make_migration_entry_read(&entry); 8730697212aSChristoph Lameter pte = swp_entry_to_pte(entry); 8740697212aSChristoph Lameter set_pte_at(src_mm, addr, src_pte, pte); 8750697212aSChristoph Lameter } 8761da177e4SLinus Torvalds } 8779f9f1acdSKonstantin Khlebnikov } 878ae859762SHugh Dickins goto out_set_pte; 8791da177e4SLinus Torvalds } 8801da177e4SLinus Torvalds 8811da177e4SLinus Torvalds /* 8821da177e4SLinus Torvalds * If it's a COW mapping, write protect it both 8831da177e4SLinus Torvalds * in the parent and the child 8841da177e4SLinus Torvalds */ 88567121172SLinus Torvalds if (is_cow_mapping(vm_flags)) { 8861da177e4SLinus Torvalds ptep_set_wrprotect(src_mm, addr, src_pte); 8873dc90795SZachary Amsden pte = pte_wrprotect(pte); 8881da177e4SLinus Torvalds } 8891da177e4SLinus Torvalds 8901da177e4SLinus Torvalds /* 8911da177e4SLinus Torvalds * If it's a shared mapping, mark it clean in 8921da177e4SLinus Torvalds * the child 8931da177e4SLinus Torvalds */ 8941da177e4SLinus Torvalds if (vm_flags & VM_SHARED) 8951da177e4SLinus Torvalds pte = pte_mkclean(pte); 8961da177e4SLinus Torvalds pte = pte_mkold(pte); 8976aab341eSLinus Torvalds 8986aab341eSLinus Torvalds page = vm_normal_page(vma, addr, pte); 8996aab341eSLinus Torvalds if (page) { 9001da177e4SLinus Torvalds get_page(page); 90121333b2bSHugh Dickins page_dup_rmap(page); 902d559db08SKAMEZAWA Hiroyuki if (PageAnon(page)) 903d559db08SKAMEZAWA Hiroyuki rss[MM_ANONPAGES]++; 904d559db08SKAMEZAWA Hiroyuki else 905d559db08SKAMEZAWA Hiroyuki rss[MM_FILEPAGES]++; 9066aab341eSLinus Torvalds } 907ae859762SHugh Dickins 908ae859762SHugh Dickins out_set_pte: 909ae859762SHugh Dickins set_pte_at(dst_mm, addr, dst_pte, pte); 910570a335bSHugh Dickins return 0; 9111da177e4SLinus Torvalds } 9121da177e4SLinus Torvalds 91371e3aac0SAndrea Arcangeli int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 9141da177e4SLinus Torvalds pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma, 9151da177e4SLinus Torvalds unsigned long addr, unsigned long end) 9161da177e4SLinus Torvalds { 917c36987e2SDaisuke Nishimura pte_t *orig_src_pte, *orig_dst_pte; 9181da177e4SLinus Torvalds pte_t *src_pte, *dst_pte; 919c74df32cSHugh Dickins spinlock_t *src_ptl, *dst_ptl; 920e040f218SHugh Dickins int progress = 0; 921d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 922570a335bSHugh Dickins swp_entry_t entry = (swp_entry_t){0}; 9231da177e4SLinus Torvalds 9241da177e4SLinus Torvalds again: 925d559db08SKAMEZAWA Hiroyuki init_rss_vec(rss); 926d559db08SKAMEZAWA Hiroyuki 927c74df32cSHugh Dickins dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl); 9281da177e4SLinus Torvalds if (!dst_pte) 9291da177e4SLinus Torvalds return -ENOMEM; 930ece0e2b6SPeter Zijlstra src_pte = pte_offset_map(src_pmd, addr); 9314c21e2f2SHugh Dickins src_ptl = pte_lockptr(src_mm, src_pmd); 932f20dc5f7SIngo Molnar spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 933c36987e2SDaisuke Nishimura orig_src_pte = src_pte; 934c36987e2SDaisuke Nishimura orig_dst_pte = dst_pte; 9356606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 9361da177e4SLinus Torvalds 9371da177e4SLinus Torvalds do { 9381da177e4SLinus Torvalds /* 9391da177e4SLinus Torvalds * We are holding two locks at this point - either of them 9401da177e4SLinus Torvalds * could generate latencies in another task on another CPU. 9411da177e4SLinus Torvalds */ 942e040f218SHugh Dickins if (progress >= 32) { 943e040f218SHugh Dickins progress = 0; 944e040f218SHugh Dickins if (need_resched() || 94595c354feSNick Piggin spin_needbreak(src_ptl) || spin_needbreak(dst_ptl)) 9461da177e4SLinus Torvalds break; 947e040f218SHugh Dickins } 9481da177e4SLinus Torvalds if (pte_none(*src_pte)) { 9491da177e4SLinus Torvalds progress++; 9501da177e4SLinus Torvalds continue; 9511da177e4SLinus Torvalds } 952570a335bSHugh Dickins entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte, 953570a335bSHugh Dickins vma, addr, rss); 954570a335bSHugh Dickins if (entry.val) 955570a335bSHugh Dickins break; 9561da177e4SLinus Torvalds progress += 8; 9571da177e4SLinus Torvalds } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end); 9581da177e4SLinus Torvalds 9596606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 960c74df32cSHugh Dickins spin_unlock(src_ptl); 961ece0e2b6SPeter Zijlstra pte_unmap(orig_src_pte); 962d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(dst_mm, rss); 963c36987e2SDaisuke Nishimura pte_unmap_unlock(orig_dst_pte, dst_ptl); 964c74df32cSHugh Dickins cond_resched(); 965570a335bSHugh Dickins 966570a335bSHugh Dickins if (entry.val) { 967570a335bSHugh Dickins if (add_swap_count_continuation(entry, GFP_KERNEL) < 0) 968570a335bSHugh Dickins return -ENOMEM; 969570a335bSHugh Dickins progress = 0; 970570a335bSHugh Dickins } 9711da177e4SLinus Torvalds if (addr != end) 9721da177e4SLinus Torvalds goto again; 9731da177e4SLinus Torvalds return 0; 9741da177e4SLinus Torvalds } 9751da177e4SLinus Torvalds 9761da177e4SLinus Torvalds static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 9771da177e4SLinus Torvalds pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma, 9781da177e4SLinus Torvalds unsigned long addr, unsigned long end) 9791da177e4SLinus Torvalds { 9801da177e4SLinus Torvalds pmd_t *src_pmd, *dst_pmd; 9811da177e4SLinus Torvalds unsigned long next; 9821da177e4SLinus Torvalds 9831da177e4SLinus Torvalds dst_pmd = pmd_alloc(dst_mm, dst_pud, addr); 9841da177e4SLinus Torvalds if (!dst_pmd) 9851da177e4SLinus Torvalds return -ENOMEM; 9861da177e4SLinus Torvalds src_pmd = pmd_offset(src_pud, addr); 9871da177e4SLinus Torvalds do { 9881da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 98971e3aac0SAndrea Arcangeli if (pmd_trans_huge(*src_pmd)) { 99071e3aac0SAndrea Arcangeli int err; 99114d1a55cSAndrea Arcangeli VM_BUG_ON(next-addr != HPAGE_PMD_SIZE); 99271e3aac0SAndrea Arcangeli err = copy_huge_pmd(dst_mm, src_mm, 99371e3aac0SAndrea Arcangeli dst_pmd, src_pmd, addr, vma); 99471e3aac0SAndrea Arcangeli if (err == -ENOMEM) 99571e3aac0SAndrea Arcangeli return -ENOMEM; 99671e3aac0SAndrea Arcangeli if (!err) 99771e3aac0SAndrea Arcangeli continue; 99871e3aac0SAndrea Arcangeli /* fall through */ 99971e3aac0SAndrea Arcangeli } 10001da177e4SLinus Torvalds if (pmd_none_or_clear_bad(src_pmd)) 10011da177e4SLinus Torvalds continue; 10021da177e4SLinus Torvalds if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd, 10031da177e4SLinus Torvalds vma, addr, next)) 10041da177e4SLinus Torvalds return -ENOMEM; 10051da177e4SLinus Torvalds } while (dst_pmd++, src_pmd++, addr = next, addr != end); 10061da177e4SLinus Torvalds return 0; 10071da177e4SLinus Torvalds } 10081da177e4SLinus Torvalds 10091da177e4SLinus Torvalds static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 10101da177e4SLinus Torvalds pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma, 10111da177e4SLinus Torvalds unsigned long addr, unsigned long end) 10121da177e4SLinus Torvalds { 10131da177e4SLinus Torvalds pud_t *src_pud, *dst_pud; 10141da177e4SLinus Torvalds unsigned long next; 10151da177e4SLinus Torvalds 10161da177e4SLinus Torvalds dst_pud = pud_alloc(dst_mm, dst_pgd, addr); 10171da177e4SLinus Torvalds if (!dst_pud) 10181da177e4SLinus Torvalds return -ENOMEM; 10191da177e4SLinus Torvalds src_pud = pud_offset(src_pgd, addr); 10201da177e4SLinus Torvalds do { 10211da177e4SLinus Torvalds next = pud_addr_end(addr, end); 10221da177e4SLinus Torvalds if (pud_none_or_clear_bad(src_pud)) 10231da177e4SLinus Torvalds continue; 10241da177e4SLinus Torvalds if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud, 10251da177e4SLinus Torvalds vma, addr, next)) 10261da177e4SLinus Torvalds return -ENOMEM; 10271da177e4SLinus Torvalds } while (dst_pud++, src_pud++, addr = next, addr != end); 10281da177e4SLinus Torvalds return 0; 10291da177e4SLinus Torvalds } 10301da177e4SLinus Torvalds 10311da177e4SLinus Torvalds int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 10321da177e4SLinus Torvalds struct vm_area_struct *vma) 10331da177e4SLinus Torvalds { 10341da177e4SLinus Torvalds pgd_t *src_pgd, *dst_pgd; 10351da177e4SLinus Torvalds unsigned long next; 10361da177e4SLinus Torvalds unsigned long addr = vma->vm_start; 10371da177e4SLinus Torvalds unsigned long end = vma->vm_end; 1038cddb8a5cSAndrea Arcangeli int ret; 10391da177e4SLinus Torvalds 1040d992895bSNick Piggin /* 1041d992895bSNick Piggin * Don't copy ptes where a page fault will fill them correctly. 1042d992895bSNick Piggin * Fork becomes much lighter when there are big shared or private 1043d992895bSNick Piggin * readonly mappings. The tradeoff is that copy_page_range is more 1044d992895bSNick Piggin * efficient than faulting. 1045d992895bSNick Piggin */ 10464d7672b4SLinus Torvalds if (!(vma->vm_flags & (VM_HUGETLB|VM_NONLINEAR|VM_PFNMAP|VM_INSERTPAGE))) { 1047d992895bSNick Piggin if (!vma->anon_vma) 1048d992895bSNick Piggin return 0; 1049d992895bSNick Piggin } 1050d992895bSNick Piggin 10511da177e4SLinus Torvalds if (is_vm_hugetlb_page(vma)) 10521da177e4SLinus Torvalds return copy_hugetlb_page_range(dst_mm, src_mm, vma); 10531da177e4SLinus Torvalds 105434801ba9Svenkatesh.pallipadi@intel.com if (unlikely(is_pfn_mapping(vma))) { 10552ab64037Svenkatesh.pallipadi@intel.com /* 10562ab64037Svenkatesh.pallipadi@intel.com * We do not free on error cases below as remove_vma 10572ab64037Svenkatesh.pallipadi@intel.com * gets called on error from higher level routine 10582ab64037Svenkatesh.pallipadi@intel.com */ 10592ab64037Svenkatesh.pallipadi@intel.com ret = track_pfn_vma_copy(vma); 10602ab64037Svenkatesh.pallipadi@intel.com if (ret) 10612ab64037Svenkatesh.pallipadi@intel.com return ret; 10622ab64037Svenkatesh.pallipadi@intel.com } 10632ab64037Svenkatesh.pallipadi@intel.com 1064cddb8a5cSAndrea Arcangeli /* 1065cddb8a5cSAndrea Arcangeli * We need to invalidate the secondary MMU mappings only when 1066cddb8a5cSAndrea Arcangeli * there could be a permission downgrade on the ptes of the 1067cddb8a5cSAndrea Arcangeli * parent mm. And a permission downgrade will only happen if 1068cddb8a5cSAndrea Arcangeli * is_cow_mapping() returns true. 1069cddb8a5cSAndrea Arcangeli */ 1070cddb8a5cSAndrea Arcangeli if (is_cow_mapping(vma->vm_flags)) 1071cddb8a5cSAndrea Arcangeli mmu_notifier_invalidate_range_start(src_mm, addr, end); 1072cddb8a5cSAndrea Arcangeli 1073cddb8a5cSAndrea Arcangeli ret = 0; 10741da177e4SLinus Torvalds dst_pgd = pgd_offset(dst_mm, addr); 10751da177e4SLinus Torvalds src_pgd = pgd_offset(src_mm, addr); 10761da177e4SLinus Torvalds do { 10771da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 10781da177e4SLinus Torvalds if (pgd_none_or_clear_bad(src_pgd)) 10791da177e4SLinus Torvalds continue; 1080cddb8a5cSAndrea Arcangeli if (unlikely(copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd, 1081cddb8a5cSAndrea Arcangeli vma, addr, next))) { 1082cddb8a5cSAndrea Arcangeli ret = -ENOMEM; 1083cddb8a5cSAndrea Arcangeli break; 1084cddb8a5cSAndrea Arcangeli } 10851da177e4SLinus Torvalds } while (dst_pgd++, src_pgd++, addr = next, addr != end); 1086cddb8a5cSAndrea Arcangeli 1087cddb8a5cSAndrea Arcangeli if (is_cow_mapping(vma->vm_flags)) 1088cddb8a5cSAndrea Arcangeli mmu_notifier_invalidate_range_end(src_mm, 1089cddb8a5cSAndrea Arcangeli vma->vm_start, end); 1090cddb8a5cSAndrea Arcangeli return ret; 10911da177e4SLinus Torvalds } 10921da177e4SLinus Torvalds 109351c6f666SRobin Holt static unsigned long zap_pte_range(struct mmu_gather *tlb, 1094b5810039SNick Piggin struct vm_area_struct *vma, pmd_t *pmd, 10951da177e4SLinus Torvalds unsigned long addr, unsigned long end, 109697a89413SPeter Zijlstra struct zap_details *details) 10971da177e4SLinus Torvalds { 1098b5810039SNick Piggin struct mm_struct *mm = tlb->mm; 1099d16dfc55SPeter Zijlstra int force_flush = 0; 1100d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 110197a89413SPeter Zijlstra spinlock_t *ptl; 11025f1a1907SSteven Rostedt pte_t *start_pte; 110397a89413SPeter Zijlstra pte_t *pte; 1104d559db08SKAMEZAWA Hiroyuki 1105d16dfc55SPeter Zijlstra again: 1106e303297eSPeter Zijlstra init_rss_vec(rss); 11075f1a1907SSteven Rostedt start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl); 11085f1a1907SSteven Rostedt pte = start_pte; 11096606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 11101da177e4SLinus Torvalds do { 11111da177e4SLinus Torvalds pte_t ptent = *pte; 111251c6f666SRobin Holt if (pte_none(ptent)) { 11131da177e4SLinus Torvalds continue; 111451c6f666SRobin Holt } 111551c6f666SRobin Holt 11166f5e6b9eSHugh Dickins if (pte_present(ptent)) { 11176f5e6b9eSHugh Dickins struct page *page; 11186f5e6b9eSHugh Dickins 11196aab341eSLinus Torvalds page = vm_normal_page(vma, addr, ptent); 11201da177e4SLinus Torvalds if (unlikely(details) && page) { 11211da177e4SLinus Torvalds /* 11221da177e4SLinus Torvalds * unmap_shared_mapping_pages() wants to 11231da177e4SLinus Torvalds * invalidate cache without truncating: 11241da177e4SLinus Torvalds * unmap shared but keep private pages. 11251da177e4SLinus Torvalds */ 11261da177e4SLinus Torvalds if (details->check_mapping && 11271da177e4SLinus Torvalds details->check_mapping != page->mapping) 11281da177e4SLinus Torvalds continue; 11291da177e4SLinus Torvalds /* 11301da177e4SLinus Torvalds * Each page->index must be checked when 11311da177e4SLinus Torvalds * invalidating or truncating nonlinear. 11321da177e4SLinus Torvalds */ 11331da177e4SLinus Torvalds if (details->nonlinear_vma && 11341da177e4SLinus Torvalds (page->index < details->first_index || 11351da177e4SLinus Torvalds page->index > details->last_index)) 11361da177e4SLinus Torvalds continue; 11371da177e4SLinus Torvalds } 1138b5810039SNick Piggin ptent = ptep_get_and_clear_full(mm, addr, pte, 1139a600388dSZachary Amsden tlb->fullmm); 11401da177e4SLinus Torvalds tlb_remove_tlb_entry(tlb, pte, addr); 11411da177e4SLinus Torvalds if (unlikely(!page)) 11421da177e4SLinus Torvalds continue; 11431da177e4SLinus Torvalds if (unlikely(details) && details->nonlinear_vma 11441da177e4SLinus Torvalds && linear_page_index(details->nonlinear_vma, 11451da177e4SLinus Torvalds addr) != page->index) 1146b5810039SNick Piggin set_pte_at(mm, addr, pte, 11471da177e4SLinus Torvalds pgoff_to_pte(page->index)); 11481da177e4SLinus Torvalds if (PageAnon(page)) 1149d559db08SKAMEZAWA Hiroyuki rss[MM_ANONPAGES]--; 11506237bcd9SHugh Dickins else { 11516237bcd9SHugh Dickins if (pte_dirty(ptent)) 11526237bcd9SHugh Dickins set_page_dirty(page); 11534917e5d0SJohannes Weiner if (pte_young(ptent) && 11544917e5d0SJohannes Weiner likely(!VM_SequentialReadHint(vma))) 1155bf3f3bc5SNick Piggin mark_page_accessed(page); 1156d559db08SKAMEZAWA Hiroyuki rss[MM_FILEPAGES]--; 11576237bcd9SHugh Dickins } 1158edc315fdSHugh Dickins page_remove_rmap(page); 11593dc14741SHugh Dickins if (unlikely(page_mapcount(page) < 0)) 11603dc14741SHugh Dickins print_bad_pte(vma, addr, ptent, page); 1161d16dfc55SPeter Zijlstra force_flush = !__tlb_remove_page(tlb, page); 1162d16dfc55SPeter Zijlstra if (force_flush) 1163d16dfc55SPeter Zijlstra break; 11641da177e4SLinus Torvalds continue; 11651da177e4SLinus Torvalds } 11661da177e4SLinus Torvalds /* 11671da177e4SLinus Torvalds * If details->check_mapping, we leave swap entries; 11681da177e4SLinus Torvalds * if details->nonlinear_vma, we leave file entries. 11691da177e4SLinus Torvalds */ 11701da177e4SLinus Torvalds if (unlikely(details)) 11711da177e4SLinus Torvalds continue; 11722509ef26SHugh Dickins if (pte_file(ptent)) { 11732509ef26SHugh Dickins if (unlikely(!(vma->vm_flags & VM_NONLINEAR))) 11742509ef26SHugh Dickins print_bad_pte(vma, addr, ptent, NULL); 1175b084d435SKAMEZAWA Hiroyuki } else { 1176b084d435SKAMEZAWA Hiroyuki swp_entry_t entry = pte_to_swp_entry(ptent); 1177b084d435SKAMEZAWA Hiroyuki 1178b084d435SKAMEZAWA Hiroyuki if (!non_swap_entry(entry)) 1179b084d435SKAMEZAWA Hiroyuki rss[MM_SWAPENTS]--; 11809f9f1acdSKonstantin Khlebnikov else if (is_migration_entry(entry)) { 11819f9f1acdSKonstantin Khlebnikov struct page *page; 11829f9f1acdSKonstantin Khlebnikov 11839f9f1acdSKonstantin Khlebnikov page = migration_entry_to_page(entry); 11849f9f1acdSKonstantin Khlebnikov 11859f9f1acdSKonstantin Khlebnikov if (PageAnon(page)) 11869f9f1acdSKonstantin Khlebnikov rss[MM_ANONPAGES]--; 11879f9f1acdSKonstantin Khlebnikov else 11889f9f1acdSKonstantin Khlebnikov rss[MM_FILEPAGES]--; 11899f9f1acdSKonstantin Khlebnikov } 1190b084d435SKAMEZAWA Hiroyuki if (unlikely(!free_swap_and_cache(entry))) 11912509ef26SHugh Dickins print_bad_pte(vma, addr, ptent, NULL); 1192b084d435SKAMEZAWA Hiroyuki } 11939888a1caSZachary Amsden pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); 119497a89413SPeter Zijlstra } while (pte++, addr += PAGE_SIZE, addr != end); 1195ae859762SHugh Dickins 1196d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(mm, rss); 11976606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 11985f1a1907SSteven Rostedt pte_unmap_unlock(start_pte, ptl); 119951c6f666SRobin Holt 1200d16dfc55SPeter Zijlstra /* 1201d16dfc55SPeter Zijlstra * mmu_gather ran out of room to batch pages, we break out of 1202d16dfc55SPeter Zijlstra * the PTE lock to avoid doing the potential expensive TLB invalidate 1203d16dfc55SPeter Zijlstra * and page-free while holding it. 1204d16dfc55SPeter Zijlstra */ 1205d16dfc55SPeter Zijlstra if (force_flush) { 1206d16dfc55SPeter Zijlstra force_flush = 0; 1207d16dfc55SPeter Zijlstra tlb_flush_mmu(tlb); 1208d16dfc55SPeter Zijlstra if (addr != end) 1209d16dfc55SPeter Zijlstra goto again; 1210d16dfc55SPeter Zijlstra } 1211d16dfc55SPeter Zijlstra 121251c6f666SRobin Holt return addr; 12131da177e4SLinus Torvalds } 12141da177e4SLinus Torvalds 121551c6f666SRobin Holt static inline unsigned long zap_pmd_range(struct mmu_gather *tlb, 1216b5810039SNick Piggin struct vm_area_struct *vma, pud_t *pud, 12171da177e4SLinus Torvalds unsigned long addr, unsigned long end, 121897a89413SPeter Zijlstra struct zap_details *details) 12191da177e4SLinus Torvalds { 12201da177e4SLinus Torvalds pmd_t *pmd; 12211da177e4SLinus Torvalds unsigned long next; 12221da177e4SLinus Torvalds 12231da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 12241da177e4SLinus Torvalds do { 12251da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 122671e3aac0SAndrea Arcangeli if (pmd_trans_huge(*pmd)) { 122714d1a55cSAndrea Arcangeli if (next - addr != HPAGE_PMD_SIZE) { 122814d1a55cSAndrea Arcangeli VM_BUG_ON(!rwsem_is_locked(&tlb->mm->mmap_sem)); 122971e3aac0SAndrea Arcangeli split_huge_page_pmd(vma->vm_mm, pmd); 1230f21760b1SShaohua Li } else if (zap_huge_pmd(tlb, vma, pmd, addr)) 12311a5a9906SAndrea Arcangeli goto next; 123271e3aac0SAndrea Arcangeli /* fall through */ 123371e3aac0SAndrea Arcangeli } 12341a5a9906SAndrea Arcangeli /* 12351a5a9906SAndrea Arcangeli * Here there can be other concurrent MADV_DONTNEED or 12361a5a9906SAndrea Arcangeli * trans huge page faults running, and if the pmd is 12371a5a9906SAndrea Arcangeli * none or trans huge it can change under us. This is 12381a5a9906SAndrea Arcangeli * because MADV_DONTNEED holds the mmap_sem in read 12391a5a9906SAndrea Arcangeli * mode. 12401a5a9906SAndrea Arcangeli */ 12411a5a9906SAndrea Arcangeli if (pmd_none_or_trans_huge_or_clear_bad(pmd)) 12421a5a9906SAndrea Arcangeli goto next; 124397a89413SPeter Zijlstra next = zap_pte_range(tlb, vma, pmd, addr, next, details); 12441a5a9906SAndrea Arcangeli next: 124597a89413SPeter Zijlstra cond_resched(); 124697a89413SPeter Zijlstra } while (pmd++, addr = next, addr != end); 124751c6f666SRobin Holt 124851c6f666SRobin Holt return addr; 12491da177e4SLinus Torvalds } 12501da177e4SLinus Torvalds 125151c6f666SRobin Holt static inline unsigned long zap_pud_range(struct mmu_gather *tlb, 1252b5810039SNick Piggin struct vm_area_struct *vma, pgd_t *pgd, 12531da177e4SLinus Torvalds unsigned long addr, unsigned long end, 125497a89413SPeter Zijlstra struct zap_details *details) 12551da177e4SLinus Torvalds { 12561da177e4SLinus Torvalds pud_t *pud; 12571da177e4SLinus Torvalds unsigned long next; 12581da177e4SLinus Torvalds 12591da177e4SLinus Torvalds pud = pud_offset(pgd, addr); 12601da177e4SLinus Torvalds do { 12611da177e4SLinus Torvalds next = pud_addr_end(addr, end); 126297a89413SPeter Zijlstra if (pud_none_or_clear_bad(pud)) 12631da177e4SLinus Torvalds continue; 126497a89413SPeter Zijlstra next = zap_pmd_range(tlb, vma, pud, addr, next, details); 126597a89413SPeter Zijlstra } while (pud++, addr = next, addr != end); 126651c6f666SRobin Holt 126751c6f666SRobin Holt return addr; 12681da177e4SLinus Torvalds } 12691da177e4SLinus Torvalds 1270038c7aa1SAl Viro static void unmap_page_range(struct mmu_gather *tlb, 127151c6f666SRobin Holt struct vm_area_struct *vma, 12721da177e4SLinus Torvalds unsigned long addr, unsigned long end, 127397a89413SPeter Zijlstra struct zap_details *details) 12741da177e4SLinus Torvalds { 12751da177e4SLinus Torvalds pgd_t *pgd; 12761da177e4SLinus Torvalds unsigned long next; 12771da177e4SLinus Torvalds 12781da177e4SLinus Torvalds if (details && !details->check_mapping && !details->nonlinear_vma) 12791da177e4SLinus Torvalds details = NULL; 12801da177e4SLinus Torvalds 12811da177e4SLinus Torvalds BUG_ON(addr >= end); 1282569b846dSKAMEZAWA Hiroyuki mem_cgroup_uncharge_start(); 12831da177e4SLinus Torvalds tlb_start_vma(tlb, vma); 12841da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 12851da177e4SLinus Torvalds do { 12861da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 128797a89413SPeter Zijlstra if (pgd_none_or_clear_bad(pgd)) 12881da177e4SLinus Torvalds continue; 128997a89413SPeter Zijlstra next = zap_pud_range(tlb, vma, pgd, addr, next, details); 129097a89413SPeter Zijlstra } while (pgd++, addr = next, addr != end); 12911da177e4SLinus Torvalds tlb_end_vma(tlb, vma); 1292569b846dSKAMEZAWA Hiroyuki mem_cgroup_uncharge_end(); 12931da177e4SLinus Torvalds } 12941da177e4SLinus Torvalds 1295f5cc4eefSAl Viro 1296f5cc4eefSAl Viro static void unmap_single_vma(struct mmu_gather *tlb, 12971da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long start_addr, 12981da177e4SLinus Torvalds unsigned long end_addr, unsigned long *nr_accounted, 12991da177e4SLinus Torvalds struct zap_details *details) 13001da177e4SLinus Torvalds { 1301f5cc4eefSAl Viro unsigned long start = max(vma->vm_start, start_addr); 13021da177e4SLinus Torvalds unsigned long end; 13031da177e4SLinus Torvalds 13041da177e4SLinus Torvalds if (start >= vma->vm_end) 1305f5cc4eefSAl Viro return; 13061da177e4SLinus Torvalds end = min(vma->vm_end, end_addr); 13071da177e4SLinus Torvalds if (end <= vma->vm_start) 1308f5cc4eefSAl Viro return; 13091da177e4SLinus Torvalds 1310*cbc91f71SSrikar Dronamraju if (vma->vm_file) 1311*cbc91f71SSrikar Dronamraju uprobe_munmap(vma, start, end); 1312*cbc91f71SSrikar Dronamraju 13131da177e4SLinus Torvalds if (vma->vm_flags & VM_ACCOUNT) 13141da177e4SLinus Torvalds *nr_accounted += (end - start) >> PAGE_SHIFT; 13151da177e4SLinus Torvalds 131634801ba9Svenkatesh.pallipadi@intel.com if (unlikely(is_pfn_mapping(vma))) 13172ab64037Svenkatesh.pallipadi@intel.com untrack_pfn_vma(vma, 0, 0); 13182ab64037Svenkatesh.pallipadi@intel.com 13198b2a1238SAl Viro if (start != end) { 132051c6f666SRobin Holt if (unlikely(is_vm_hugetlb_page(vma))) { 1321a137e1ccSAndi Kleen /* 1322a137e1ccSAndi Kleen * It is undesirable to test vma->vm_file as it 1323a137e1ccSAndi Kleen * should be non-null for valid hugetlb area. 1324a137e1ccSAndi Kleen * However, vm_file will be NULL in the error 1325a137e1ccSAndi Kleen * cleanup path of do_mmap_pgoff. When 1326a137e1ccSAndi Kleen * hugetlbfs ->mmap method fails, 1327a137e1ccSAndi Kleen * do_mmap_pgoff() nullifies vma->vm_file 1328a137e1ccSAndi Kleen * before calling this function to clean up. 1329a137e1ccSAndi Kleen * Since no pte has actually been setup, it is 1330a137e1ccSAndi Kleen * safe to do nothing in this case. 1331a137e1ccSAndi Kleen */ 133297a89413SPeter Zijlstra if (vma->vm_file) 133304f2cbe3SMel Gorman unmap_hugepage_range(vma, start, end, NULL); 133451c6f666SRobin Holt } else 1335038c7aa1SAl Viro unmap_page_range(tlb, vma, start, end, details); 133697a89413SPeter Zijlstra } 133751c6f666SRobin Holt } 13381da177e4SLinus Torvalds 1339f5cc4eefSAl Viro /** 1340f5cc4eefSAl Viro * unmap_vmas - unmap a range of memory covered by a list of vma's 1341f5cc4eefSAl Viro * @tlb: address of the caller's struct mmu_gather 1342f5cc4eefSAl Viro * @vma: the starting vma 1343f5cc4eefSAl Viro * @start_addr: virtual address at which to start unmapping 1344f5cc4eefSAl Viro * @end_addr: virtual address at which to end unmapping 1345f5cc4eefSAl Viro * @nr_accounted: Place number of unmapped pages in vm-accountable vma's here 1346f5cc4eefSAl Viro * @details: details of nonlinear truncation or shared cache invalidation 1347f5cc4eefSAl Viro * 1348f5cc4eefSAl Viro * Unmap all pages in the vma list. 1349f5cc4eefSAl Viro * 1350f5cc4eefSAl Viro * Only addresses between `start' and `end' will be unmapped. 1351f5cc4eefSAl Viro * 1352f5cc4eefSAl Viro * The VMA list must be sorted in ascending virtual address order. 1353f5cc4eefSAl Viro * 1354f5cc4eefSAl Viro * unmap_vmas() assumes that the caller will flush the whole unmapped address 1355f5cc4eefSAl Viro * range after unmap_vmas() returns. So the only responsibility here is to 1356f5cc4eefSAl Viro * ensure that any thus-far unmapped pages are flushed before unmap_vmas() 1357f5cc4eefSAl Viro * drops the lock and schedules. 1358f5cc4eefSAl Viro */ 1359f5cc4eefSAl Viro void unmap_vmas(struct mmu_gather *tlb, 1360f5cc4eefSAl Viro struct vm_area_struct *vma, unsigned long start_addr, 1361f5cc4eefSAl Viro unsigned long end_addr, unsigned long *nr_accounted, 1362f5cc4eefSAl Viro struct zap_details *details) 1363f5cc4eefSAl Viro { 1364f5cc4eefSAl Viro struct mm_struct *mm = vma->vm_mm; 1365f5cc4eefSAl Viro 1366f5cc4eefSAl Viro mmu_notifier_invalidate_range_start(mm, start_addr, end_addr); 1367f5cc4eefSAl Viro for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) 1368f5cc4eefSAl Viro unmap_single_vma(tlb, vma, start_addr, end_addr, nr_accounted, 1369f5cc4eefSAl Viro details); 1370cddb8a5cSAndrea Arcangeli mmu_notifier_invalidate_range_end(mm, start_addr, end_addr); 13711da177e4SLinus Torvalds } 13721da177e4SLinus Torvalds 13731da177e4SLinus Torvalds /** 13741da177e4SLinus Torvalds * zap_page_range - remove user pages in a given range 13751da177e4SLinus Torvalds * @vma: vm_area_struct holding the applicable pages 13761da177e4SLinus Torvalds * @address: starting address of pages to zap 13771da177e4SLinus Torvalds * @size: number of bytes to zap 13781da177e4SLinus Torvalds * @details: details of nonlinear truncation or shared cache invalidation 1379f5cc4eefSAl Viro * 1380f5cc4eefSAl Viro * Caller must protect the VMA list 13811da177e4SLinus Torvalds */ 138214f5ff5dSAl Viro void zap_page_range(struct vm_area_struct *vma, unsigned long address, 13831da177e4SLinus Torvalds unsigned long size, struct zap_details *details) 13841da177e4SLinus Torvalds { 13851da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 1386d16dfc55SPeter Zijlstra struct mmu_gather tlb; 13871da177e4SLinus Torvalds unsigned long end = address + size; 13881da177e4SLinus Torvalds unsigned long nr_accounted = 0; 13891da177e4SLinus Torvalds 13901da177e4SLinus Torvalds lru_add_drain(); 1391d16dfc55SPeter Zijlstra tlb_gather_mmu(&tlb, mm, 0); 1392365e9c87SHugh Dickins update_hiwater_rss(mm); 139314f5ff5dSAl Viro unmap_vmas(&tlb, vma, address, end, &nr_accounted, details); 1394d16dfc55SPeter Zijlstra tlb_finish_mmu(&tlb, address, end); 13951da177e4SLinus Torvalds } 13961da177e4SLinus Torvalds 1397c627f9ccSJack Steiner /** 1398f5cc4eefSAl Viro * zap_page_range_single - remove user pages in a given range 1399f5cc4eefSAl Viro * @vma: vm_area_struct holding the applicable pages 1400f5cc4eefSAl Viro * @address: starting address of pages to zap 1401f5cc4eefSAl Viro * @size: number of bytes to zap 1402f5cc4eefSAl Viro * @details: details of nonlinear truncation or shared cache invalidation 1403f5cc4eefSAl Viro * 1404f5cc4eefSAl Viro * The range must fit into one VMA. 1405f5cc4eefSAl Viro */ 1406f5cc4eefSAl Viro static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address, 1407f5cc4eefSAl Viro unsigned long size, struct zap_details *details) 1408f5cc4eefSAl Viro { 1409f5cc4eefSAl Viro struct mm_struct *mm = vma->vm_mm; 1410f5cc4eefSAl Viro struct mmu_gather tlb; 1411f5cc4eefSAl Viro unsigned long end = address + size; 1412f5cc4eefSAl Viro unsigned long nr_accounted = 0; 1413f5cc4eefSAl Viro 1414f5cc4eefSAl Viro lru_add_drain(); 1415f5cc4eefSAl Viro tlb_gather_mmu(&tlb, mm, 0); 1416f5cc4eefSAl Viro update_hiwater_rss(mm); 1417f5cc4eefSAl Viro mmu_notifier_invalidate_range_start(mm, address, end); 1418f5cc4eefSAl Viro unmap_single_vma(&tlb, vma, address, end, &nr_accounted, details); 1419f5cc4eefSAl Viro mmu_notifier_invalidate_range_end(mm, address, end); 1420f5cc4eefSAl Viro tlb_finish_mmu(&tlb, address, end); 14211da177e4SLinus Torvalds } 14221da177e4SLinus Torvalds 1423c627f9ccSJack Steiner /** 1424c627f9ccSJack Steiner * zap_vma_ptes - remove ptes mapping the vma 1425c627f9ccSJack Steiner * @vma: vm_area_struct holding ptes to be zapped 1426c627f9ccSJack Steiner * @address: starting address of pages to zap 1427c627f9ccSJack Steiner * @size: number of bytes to zap 1428c627f9ccSJack Steiner * 1429c627f9ccSJack Steiner * This function only unmaps ptes assigned to VM_PFNMAP vmas. 1430c627f9ccSJack Steiner * 1431c627f9ccSJack Steiner * The entire address range must be fully contained within the vma. 1432c627f9ccSJack Steiner * 1433c627f9ccSJack Steiner * Returns 0 if successful. 1434c627f9ccSJack Steiner */ 1435c627f9ccSJack Steiner int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, 1436c627f9ccSJack Steiner unsigned long size) 1437c627f9ccSJack Steiner { 1438c627f9ccSJack Steiner if (address < vma->vm_start || address + size > vma->vm_end || 1439c627f9ccSJack Steiner !(vma->vm_flags & VM_PFNMAP)) 1440c627f9ccSJack Steiner return -1; 1441f5cc4eefSAl Viro zap_page_range_single(vma, address, size, NULL); 1442c627f9ccSJack Steiner return 0; 1443c627f9ccSJack Steiner } 1444c627f9ccSJack Steiner EXPORT_SYMBOL_GPL(zap_vma_ptes); 1445c627f9ccSJack Steiner 1446142762bdSJohannes Weiner /** 1447142762bdSJohannes Weiner * follow_page - look up a page descriptor from a user-virtual address 1448142762bdSJohannes Weiner * @vma: vm_area_struct mapping @address 1449142762bdSJohannes Weiner * @address: virtual address to look up 1450142762bdSJohannes Weiner * @flags: flags modifying lookup behaviour 1451142762bdSJohannes Weiner * 1452142762bdSJohannes Weiner * @flags can have FOLL_ flags set, defined in <linux/mm.h> 1453142762bdSJohannes Weiner * 1454142762bdSJohannes Weiner * Returns the mapped (struct page *), %NULL if no mapping exists, or 1455142762bdSJohannes Weiner * an error pointer if there is a mapping to something not represented 1456142762bdSJohannes Weiner * by a page descriptor (see also vm_normal_page()). 14571da177e4SLinus Torvalds */ 14586aab341eSLinus Torvalds struct page *follow_page(struct vm_area_struct *vma, unsigned long address, 1459deceb6cdSHugh Dickins unsigned int flags) 14601da177e4SLinus Torvalds { 14611da177e4SLinus Torvalds pgd_t *pgd; 14621da177e4SLinus Torvalds pud_t *pud; 14631da177e4SLinus Torvalds pmd_t *pmd; 14641da177e4SLinus Torvalds pte_t *ptep, pte; 1465deceb6cdSHugh Dickins spinlock_t *ptl; 14661da177e4SLinus Torvalds struct page *page; 14676aab341eSLinus Torvalds struct mm_struct *mm = vma->vm_mm; 14681da177e4SLinus Torvalds 1469deceb6cdSHugh Dickins page = follow_huge_addr(mm, address, flags & FOLL_WRITE); 1470deceb6cdSHugh Dickins if (!IS_ERR(page)) { 1471deceb6cdSHugh Dickins BUG_ON(flags & FOLL_GET); 1472deceb6cdSHugh Dickins goto out; 1473deceb6cdSHugh Dickins } 14741da177e4SLinus Torvalds 1475deceb6cdSHugh Dickins page = NULL; 14761da177e4SLinus Torvalds pgd = pgd_offset(mm, address); 14771da177e4SLinus Torvalds if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) 1478deceb6cdSHugh Dickins goto no_page_table; 14791da177e4SLinus Torvalds 14801da177e4SLinus Torvalds pud = pud_offset(pgd, address); 1481ceb86879SAndi Kleen if (pud_none(*pud)) 1482ceb86879SAndi Kleen goto no_page_table; 14838a07651eSHugh Dickins if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) { 1484ceb86879SAndi Kleen BUG_ON(flags & FOLL_GET); 1485ceb86879SAndi Kleen page = follow_huge_pud(mm, address, pud, flags & FOLL_WRITE); 1486ceb86879SAndi Kleen goto out; 1487ceb86879SAndi Kleen } 1488ceb86879SAndi Kleen if (unlikely(pud_bad(*pud))) 1489deceb6cdSHugh Dickins goto no_page_table; 14901da177e4SLinus Torvalds 14911da177e4SLinus Torvalds pmd = pmd_offset(pud, address); 1492aeed5fceSHugh Dickins if (pmd_none(*pmd)) 1493deceb6cdSHugh Dickins goto no_page_table; 149471e3aac0SAndrea Arcangeli if (pmd_huge(*pmd) && vma->vm_flags & VM_HUGETLB) { 1495deceb6cdSHugh Dickins BUG_ON(flags & FOLL_GET); 1496deceb6cdSHugh Dickins page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE); 1497deceb6cdSHugh Dickins goto out; 1498deceb6cdSHugh Dickins } 149971e3aac0SAndrea Arcangeli if (pmd_trans_huge(*pmd)) { 1500500d65d4SAndrea Arcangeli if (flags & FOLL_SPLIT) { 1501500d65d4SAndrea Arcangeli split_huge_page_pmd(mm, pmd); 1502500d65d4SAndrea Arcangeli goto split_fallthrough; 1503500d65d4SAndrea Arcangeli } 150471e3aac0SAndrea Arcangeli spin_lock(&mm->page_table_lock); 150571e3aac0SAndrea Arcangeli if (likely(pmd_trans_huge(*pmd))) { 150671e3aac0SAndrea Arcangeli if (unlikely(pmd_trans_splitting(*pmd))) { 150771e3aac0SAndrea Arcangeli spin_unlock(&mm->page_table_lock); 150871e3aac0SAndrea Arcangeli wait_split_huge_page(vma->anon_vma, pmd); 150971e3aac0SAndrea Arcangeli } else { 151071e3aac0SAndrea Arcangeli page = follow_trans_huge_pmd(mm, address, 151171e3aac0SAndrea Arcangeli pmd, flags); 151271e3aac0SAndrea Arcangeli spin_unlock(&mm->page_table_lock); 151371e3aac0SAndrea Arcangeli goto out; 151471e3aac0SAndrea Arcangeli } 151571e3aac0SAndrea Arcangeli } else 151671e3aac0SAndrea Arcangeli spin_unlock(&mm->page_table_lock); 151771e3aac0SAndrea Arcangeli /* fall through */ 151871e3aac0SAndrea Arcangeli } 1519500d65d4SAndrea Arcangeli split_fallthrough: 1520aeed5fceSHugh Dickins if (unlikely(pmd_bad(*pmd))) 1521aeed5fceSHugh Dickins goto no_page_table; 1522aeed5fceSHugh Dickins 1523deceb6cdSHugh Dickins ptep = pte_offset_map_lock(mm, pmd, address, &ptl); 15241da177e4SLinus Torvalds 15251da177e4SLinus Torvalds pte = *ptep; 1526deceb6cdSHugh Dickins if (!pte_present(pte)) 152789f5b7daSLinus Torvalds goto no_page; 1528deceb6cdSHugh Dickins if ((flags & FOLL_WRITE) && !pte_write(pte)) 1529deceb6cdSHugh Dickins goto unlock; 1530a13ea5b7SHugh Dickins 15316aab341eSLinus Torvalds page = vm_normal_page(vma, address, pte); 1532a13ea5b7SHugh Dickins if (unlikely(!page)) { 1533a13ea5b7SHugh Dickins if ((flags & FOLL_DUMP) || 153462eede62SHugh Dickins !is_zero_pfn(pte_pfn(pte))) 153589f5b7daSLinus Torvalds goto bad_page; 1536a13ea5b7SHugh Dickins page = pte_page(pte); 1537a13ea5b7SHugh Dickins } 1538deceb6cdSHugh Dickins 1539deceb6cdSHugh Dickins if (flags & FOLL_GET) 154070b50f94SAndrea Arcangeli get_page_foll(page); 1541deceb6cdSHugh Dickins if (flags & FOLL_TOUCH) { 1542deceb6cdSHugh Dickins if ((flags & FOLL_WRITE) && 1543deceb6cdSHugh Dickins !pte_dirty(pte) && !PageDirty(page)) 1544f33ea7f4SNick Piggin set_page_dirty(page); 1545bd775c42SKOSAKI Motohiro /* 1546bd775c42SKOSAKI Motohiro * pte_mkyoung() would be more correct here, but atomic care 1547bd775c42SKOSAKI Motohiro * is needed to avoid losing the dirty bit: it is easier to use 1548bd775c42SKOSAKI Motohiro * mark_page_accessed(). 1549bd775c42SKOSAKI Motohiro */ 15501da177e4SLinus Torvalds mark_page_accessed(page); 15511da177e4SLinus Torvalds } 1552a1fde08cSLinus Torvalds if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) { 1553110d74a9SMichel Lespinasse /* 1554110d74a9SMichel Lespinasse * The preliminary mapping check is mainly to avoid the 1555110d74a9SMichel Lespinasse * pointless overhead of lock_page on the ZERO_PAGE 1556110d74a9SMichel Lespinasse * which might bounce very badly if there is contention. 1557110d74a9SMichel Lespinasse * 1558110d74a9SMichel Lespinasse * If the page is already locked, we don't need to 1559110d74a9SMichel Lespinasse * handle it now - vmscan will handle it later if and 1560110d74a9SMichel Lespinasse * when it attempts to reclaim the page. 1561110d74a9SMichel Lespinasse */ 1562110d74a9SMichel Lespinasse if (page->mapping && trylock_page(page)) { 1563110d74a9SMichel Lespinasse lru_add_drain(); /* push cached pages to LRU */ 1564110d74a9SMichel Lespinasse /* 1565110d74a9SMichel Lespinasse * Because we lock page here and migration is 1566110d74a9SMichel Lespinasse * blocked by the pte's page reference, we need 1567110d74a9SMichel Lespinasse * only check for file-cache page truncation. 1568110d74a9SMichel Lespinasse */ 1569110d74a9SMichel Lespinasse if (page->mapping) 1570110d74a9SMichel Lespinasse mlock_vma_page(page); 1571110d74a9SMichel Lespinasse unlock_page(page); 1572110d74a9SMichel Lespinasse } 1573110d74a9SMichel Lespinasse } 1574deceb6cdSHugh Dickins unlock: 1575deceb6cdSHugh Dickins pte_unmap_unlock(ptep, ptl); 15761da177e4SLinus Torvalds out: 1577deceb6cdSHugh Dickins return page; 1578deceb6cdSHugh Dickins 157989f5b7daSLinus Torvalds bad_page: 158089f5b7daSLinus Torvalds pte_unmap_unlock(ptep, ptl); 158189f5b7daSLinus Torvalds return ERR_PTR(-EFAULT); 158289f5b7daSLinus Torvalds 158389f5b7daSLinus Torvalds no_page: 158489f5b7daSLinus Torvalds pte_unmap_unlock(ptep, ptl); 158589f5b7daSLinus Torvalds if (!pte_none(pte)) 158689f5b7daSLinus Torvalds return page; 15878e4b9a60SHugh Dickins 1588deceb6cdSHugh Dickins no_page_table: 1589deceb6cdSHugh Dickins /* 1590deceb6cdSHugh Dickins * When core dumping an enormous anonymous area that nobody 15918e4b9a60SHugh Dickins * has touched so far, we don't want to allocate unnecessary pages or 15928e4b9a60SHugh Dickins * page tables. Return error instead of NULL to skip handle_mm_fault, 15938e4b9a60SHugh Dickins * then get_dump_page() will return NULL to leave a hole in the dump. 15948e4b9a60SHugh Dickins * But we can only make this optimization where a hole would surely 15958e4b9a60SHugh Dickins * be zero-filled if handle_mm_fault() actually did handle it. 1596deceb6cdSHugh Dickins */ 15978e4b9a60SHugh Dickins if ((flags & FOLL_DUMP) && 15988e4b9a60SHugh Dickins (!vma->vm_ops || !vma->vm_ops->fault)) 15998e4b9a60SHugh Dickins return ERR_PTR(-EFAULT); 1600deceb6cdSHugh Dickins return page; 16011da177e4SLinus Torvalds } 16021da177e4SLinus Torvalds 160395042f9eSLinus Torvalds static inline int stack_guard_page(struct vm_area_struct *vma, unsigned long addr) 160495042f9eSLinus Torvalds { 1605a09a79f6SMikulas Patocka return stack_guard_page_start(vma, addr) || 1606a09a79f6SMikulas Patocka stack_guard_page_end(vma, addr+PAGE_SIZE); 160795042f9eSLinus Torvalds } 160895042f9eSLinus Torvalds 16090014bd99SHuang Ying /** 16100014bd99SHuang Ying * __get_user_pages() - pin user pages in memory 16110014bd99SHuang Ying * @tsk: task_struct of target task 16120014bd99SHuang Ying * @mm: mm_struct of target mm 16130014bd99SHuang Ying * @start: starting user address 16140014bd99SHuang Ying * @nr_pages: number of pages from start to pin 16150014bd99SHuang Ying * @gup_flags: flags modifying pin behaviour 16160014bd99SHuang Ying * @pages: array that receives pointers to the pages pinned. 16170014bd99SHuang Ying * Should be at least nr_pages long. Or NULL, if caller 16180014bd99SHuang Ying * only intends to ensure the pages are faulted in. 16190014bd99SHuang Ying * @vmas: array of pointers to vmas corresponding to each page. 16200014bd99SHuang Ying * Or NULL if the caller does not require them. 16210014bd99SHuang Ying * @nonblocking: whether waiting for disk IO or mmap_sem contention 16220014bd99SHuang Ying * 16230014bd99SHuang Ying * Returns number of pages pinned. This may be fewer than the number 16240014bd99SHuang Ying * requested. If nr_pages is 0 or negative, returns 0. If no pages 16250014bd99SHuang Ying * were pinned, returns -errno. Each page returned must be released 16260014bd99SHuang Ying * with a put_page() call when it is finished with. vmas will only 16270014bd99SHuang Ying * remain valid while mmap_sem is held. 16280014bd99SHuang Ying * 16290014bd99SHuang Ying * Must be called with mmap_sem held for read or write. 16300014bd99SHuang Ying * 16310014bd99SHuang Ying * __get_user_pages walks a process's page tables and takes a reference to 16320014bd99SHuang Ying * each struct page that each user address corresponds to at a given 16330014bd99SHuang Ying * instant. That is, it takes the page that would be accessed if a user 16340014bd99SHuang Ying * thread accesses the given user virtual address at that instant. 16350014bd99SHuang Ying * 16360014bd99SHuang Ying * This does not guarantee that the page exists in the user mappings when 16370014bd99SHuang Ying * __get_user_pages returns, and there may even be a completely different 16380014bd99SHuang Ying * page there in some cases (eg. if mmapped pagecache has been invalidated 16390014bd99SHuang Ying * and subsequently re faulted). However it does guarantee that the page 16400014bd99SHuang Ying * won't be freed completely. And mostly callers simply care that the page 16410014bd99SHuang Ying * contains data that was valid *at some point in time*. Typically, an IO 16420014bd99SHuang Ying * or similar operation cannot guarantee anything stronger anyway because 16430014bd99SHuang Ying * locks can't be held over the syscall boundary. 16440014bd99SHuang Ying * 16450014bd99SHuang Ying * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If 16460014bd99SHuang Ying * the page is written to, set_page_dirty (or set_page_dirty_lock, as 16470014bd99SHuang Ying * appropriate) must be called after the page is finished with, and 16480014bd99SHuang Ying * before put_page is called. 16490014bd99SHuang Ying * 16500014bd99SHuang Ying * If @nonblocking != NULL, __get_user_pages will not wait for disk IO 16510014bd99SHuang Ying * or mmap_sem contention, and if waiting is needed to pin all pages, 16520014bd99SHuang Ying * *@nonblocking will be set to 0. 16530014bd99SHuang Ying * 16540014bd99SHuang Ying * In most cases, get_user_pages or get_user_pages_fast should be used 16550014bd99SHuang Ying * instead of __get_user_pages. __get_user_pages should be used only if 16560014bd99SHuang Ying * you need some special @gup_flags. 16570014bd99SHuang Ying */ 1658b291f000SNick Piggin int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm, 165958fa879eSHugh Dickins unsigned long start, int nr_pages, unsigned int gup_flags, 166053a7706dSMichel Lespinasse struct page **pages, struct vm_area_struct **vmas, 166153a7706dSMichel Lespinasse int *nonblocking) 16621da177e4SLinus Torvalds { 16631da177e4SLinus Torvalds int i; 166458fa879eSHugh Dickins unsigned long vm_flags; 16651da177e4SLinus Torvalds 16669d73777eSPeter Zijlstra if (nr_pages <= 0) 1667900cf086SJonathan Corbet return 0; 166858fa879eSHugh Dickins 166958fa879eSHugh Dickins VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET)); 167058fa879eSHugh Dickins 16711da177e4SLinus Torvalds /* 16721da177e4SLinus Torvalds * Require read or write permissions. 167358fa879eSHugh Dickins * If FOLL_FORCE is set, we only require the "MAY" flags. 16741da177e4SLinus Torvalds */ 167558fa879eSHugh Dickins vm_flags = (gup_flags & FOLL_WRITE) ? 167658fa879eSHugh Dickins (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD); 167758fa879eSHugh Dickins vm_flags &= (gup_flags & FOLL_FORCE) ? 167858fa879eSHugh Dickins (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE); 16791da177e4SLinus Torvalds i = 0; 16801da177e4SLinus Torvalds 16811da177e4SLinus Torvalds do { 16821da177e4SLinus Torvalds struct vm_area_struct *vma; 16831da177e4SLinus Torvalds 16841da177e4SLinus Torvalds vma = find_extend_vma(mm, start); 1685e7f22e20SStephen Wilson if (!vma && in_gate_area(mm, start)) { 16861da177e4SLinus Torvalds unsigned long pg = start & PAGE_MASK; 16871da177e4SLinus Torvalds pgd_t *pgd; 16881da177e4SLinus Torvalds pud_t *pud; 16891da177e4SLinus Torvalds pmd_t *pmd; 16901da177e4SLinus Torvalds pte_t *pte; 1691b291f000SNick Piggin 1692b291f000SNick Piggin /* user gate pages are read-only */ 169358fa879eSHugh Dickins if (gup_flags & FOLL_WRITE) 16941da177e4SLinus Torvalds return i ? : -EFAULT; 16951da177e4SLinus Torvalds if (pg > TASK_SIZE) 16961da177e4SLinus Torvalds pgd = pgd_offset_k(pg); 16971da177e4SLinus Torvalds else 16981da177e4SLinus Torvalds pgd = pgd_offset_gate(mm, pg); 16991da177e4SLinus Torvalds BUG_ON(pgd_none(*pgd)); 17001da177e4SLinus Torvalds pud = pud_offset(pgd, pg); 17011da177e4SLinus Torvalds BUG_ON(pud_none(*pud)); 17021da177e4SLinus Torvalds pmd = pmd_offset(pud, pg); 1703690dbe1cSHugh Dickins if (pmd_none(*pmd)) 1704690dbe1cSHugh Dickins return i ? : -EFAULT; 1705f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 17061da177e4SLinus Torvalds pte = pte_offset_map(pmd, pg); 1707690dbe1cSHugh Dickins if (pte_none(*pte)) { 1708690dbe1cSHugh Dickins pte_unmap(pte); 1709690dbe1cSHugh Dickins return i ? : -EFAULT; 1710690dbe1cSHugh Dickins } 171195042f9eSLinus Torvalds vma = get_gate_vma(mm); 17121da177e4SLinus Torvalds if (pages) { 1713de51257aSHugh Dickins struct page *page; 1714de51257aSHugh Dickins 171595042f9eSLinus Torvalds page = vm_normal_page(vma, start, *pte); 1716de51257aSHugh Dickins if (!page) { 1717de51257aSHugh Dickins if (!(gup_flags & FOLL_DUMP) && 1718de51257aSHugh Dickins is_zero_pfn(pte_pfn(*pte))) 1719de51257aSHugh Dickins page = pte_page(*pte); 1720de51257aSHugh Dickins else { 1721de51257aSHugh Dickins pte_unmap(pte); 1722de51257aSHugh Dickins return i ? : -EFAULT; 1723de51257aSHugh Dickins } 1724de51257aSHugh Dickins } 17256aab341eSLinus Torvalds pages[i] = page; 17266aab341eSLinus Torvalds get_page(page); 17271da177e4SLinus Torvalds } 17281da177e4SLinus Torvalds pte_unmap(pte); 172995042f9eSLinus Torvalds goto next_page; 17301da177e4SLinus Torvalds } 17311da177e4SLinus Torvalds 1732b291f000SNick Piggin if (!vma || 1733b291f000SNick Piggin (vma->vm_flags & (VM_IO | VM_PFNMAP)) || 17341c3aff1cSHugh Dickins !(vm_flags & vma->vm_flags)) 17351da177e4SLinus Torvalds return i ? : -EFAULT; 17361da177e4SLinus Torvalds 17372a15efc9SHugh Dickins if (is_vm_hugetlb_page(vma)) { 17382a15efc9SHugh Dickins i = follow_hugetlb_page(mm, vma, pages, vmas, 173958fa879eSHugh Dickins &start, &nr_pages, i, gup_flags); 17402a15efc9SHugh Dickins continue; 17412a15efc9SHugh Dickins } 1742deceb6cdSHugh Dickins 17431da177e4SLinus Torvalds do { 174408ef4729SHugh Dickins struct page *page; 174558fa879eSHugh Dickins unsigned int foll_flags = gup_flags; 17461da177e4SLinus Torvalds 1747462e00ccSEthan Solomita /* 17484779280dSYing Han * If we have a pending SIGKILL, don't keep faulting 17491c3aff1cSHugh Dickins * pages and potentially allocating memory. 1750462e00ccSEthan Solomita */ 17511c3aff1cSHugh Dickins if (unlikely(fatal_signal_pending(current))) 17524779280dSYing Han return i ? i : -ERESTARTSYS; 1753462e00ccSEthan Solomita 1754deceb6cdSHugh Dickins cond_resched(); 17556aab341eSLinus Torvalds while (!(page = follow_page(vma, start, foll_flags))) { 1756a68d2ebcSLinus Torvalds int ret; 175753a7706dSMichel Lespinasse unsigned int fault_flags = 0; 175853a7706dSMichel Lespinasse 1759a09a79f6SMikulas Patocka /* For mlock, just skip the stack guard page. */ 1760a09a79f6SMikulas Patocka if (foll_flags & FOLL_MLOCK) { 1761a09a79f6SMikulas Patocka if (stack_guard_page(vma, start)) 1762a09a79f6SMikulas Patocka goto next_page; 1763a09a79f6SMikulas Patocka } 176453a7706dSMichel Lespinasse if (foll_flags & FOLL_WRITE) 176553a7706dSMichel Lespinasse fault_flags |= FAULT_FLAG_WRITE; 176653a7706dSMichel Lespinasse if (nonblocking) 176753a7706dSMichel Lespinasse fault_flags |= FAULT_FLAG_ALLOW_RETRY; 1768318b275fSGleb Natapov if (foll_flags & FOLL_NOWAIT) 1769318b275fSGleb Natapov fault_flags |= (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT); 1770d06063ccSLinus Torvalds 1771d26ed650SHugh Dickins ret = handle_mm_fault(mm, vma, start, 177253a7706dSMichel Lespinasse fault_flags); 1773d26ed650SHugh Dickins 177483c54070SNick Piggin if (ret & VM_FAULT_ERROR) { 177583c54070SNick Piggin if (ret & VM_FAULT_OOM) 177683c54070SNick Piggin return i ? i : -ENOMEM; 177769ebb83eSHuang Ying if (ret & (VM_FAULT_HWPOISON | 177869ebb83eSHuang Ying VM_FAULT_HWPOISON_LARGE)) { 177969ebb83eSHuang Ying if (i) 178069ebb83eSHuang Ying return i; 178169ebb83eSHuang Ying else if (gup_flags & FOLL_HWPOISON) 178269ebb83eSHuang Ying return -EHWPOISON; 178369ebb83eSHuang Ying else 178469ebb83eSHuang Ying return -EFAULT; 178569ebb83eSHuang Ying } 178669ebb83eSHuang Ying if (ret & VM_FAULT_SIGBUS) 178783c54070SNick Piggin return i ? i : -EFAULT; 178883c54070SNick Piggin BUG(); 178983c54070SNick Piggin } 1790e7f22e20SStephen Wilson 1791e7f22e20SStephen Wilson if (tsk) { 179283c54070SNick Piggin if (ret & VM_FAULT_MAJOR) 179383c54070SNick Piggin tsk->maj_flt++; 179483c54070SNick Piggin else 179583c54070SNick Piggin tsk->min_flt++; 1796e7f22e20SStephen Wilson } 179783c54070SNick Piggin 179853a7706dSMichel Lespinasse if (ret & VM_FAULT_RETRY) { 1799318b275fSGleb Natapov if (nonblocking) 180053a7706dSMichel Lespinasse *nonblocking = 0; 180153a7706dSMichel Lespinasse return i; 180253a7706dSMichel Lespinasse } 180353a7706dSMichel Lespinasse 1804f33ea7f4SNick Piggin /* 180583c54070SNick Piggin * The VM_FAULT_WRITE bit tells us that 180683c54070SNick Piggin * do_wp_page has broken COW when necessary, 180783c54070SNick Piggin * even if maybe_mkwrite decided not to set 180883c54070SNick Piggin * pte_write. We can thus safely do subsequent 1809878b63acSHugh Dickins * page lookups as if they were reads. But only 1810878b63acSHugh Dickins * do so when looping for pte_write is futile: 1811878b63acSHugh Dickins * in some cases userspace may also be wanting 1812878b63acSHugh Dickins * to write to the gotten user page, which a 1813878b63acSHugh Dickins * read fault here might prevent (a readonly 1814878b63acSHugh Dickins * page might get reCOWed by userspace write). 1815f33ea7f4SNick Piggin */ 1816878b63acSHugh Dickins if ((ret & VM_FAULT_WRITE) && 1817878b63acSHugh Dickins !(vma->vm_flags & VM_WRITE)) 1818deceb6cdSHugh Dickins foll_flags &= ~FOLL_WRITE; 1819a68d2ebcSLinus Torvalds 18207f7bbbe5SBenjamin Herrenschmidt cond_resched(); 18211da177e4SLinus Torvalds } 182289f5b7daSLinus Torvalds if (IS_ERR(page)) 182389f5b7daSLinus Torvalds return i ? i : PTR_ERR(page); 18241da177e4SLinus Torvalds if (pages) { 182508ef4729SHugh Dickins pages[i] = page; 182603beb076SJames Bottomley 1827a6f36be3SRussell King flush_anon_page(vma, page, start); 182808ef4729SHugh Dickins flush_dcache_page(page); 18291da177e4SLinus Torvalds } 183095042f9eSLinus Torvalds next_page: 18311da177e4SLinus Torvalds if (vmas) 18321da177e4SLinus Torvalds vmas[i] = vma; 18331da177e4SLinus Torvalds i++; 18341da177e4SLinus Torvalds start += PAGE_SIZE; 18359d73777eSPeter Zijlstra nr_pages--; 18369d73777eSPeter Zijlstra } while (nr_pages && start < vma->vm_end); 18379d73777eSPeter Zijlstra } while (nr_pages); 18381da177e4SLinus Torvalds return i; 18391da177e4SLinus Torvalds } 18400014bd99SHuang Ying EXPORT_SYMBOL(__get_user_pages); 1841b291f000SNick Piggin 18422efaca92SBenjamin Herrenschmidt /* 18432efaca92SBenjamin Herrenschmidt * fixup_user_fault() - manually resolve a user page fault 18442efaca92SBenjamin Herrenschmidt * @tsk: the task_struct to use for page fault accounting, or 18452efaca92SBenjamin Herrenschmidt * NULL if faults are not to be recorded. 18462efaca92SBenjamin Herrenschmidt * @mm: mm_struct of target mm 18472efaca92SBenjamin Herrenschmidt * @address: user address 18482efaca92SBenjamin Herrenschmidt * @fault_flags:flags to pass down to handle_mm_fault() 18492efaca92SBenjamin Herrenschmidt * 18502efaca92SBenjamin Herrenschmidt * This is meant to be called in the specific scenario where for locking reasons 18512efaca92SBenjamin Herrenschmidt * we try to access user memory in atomic context (within a pagefault_disable() 18522efaca92SBenjamin Herrenschmidt * section), this returns -EFAULT, and we want to resolve the user fault before 18532efaca92SBenjamin Herrenschmidt * trying again. 18542efaca92SBenjamin Herrenschmidt * 18552efaca92SBenjamin Herrenschmidt * Typically this is meant to be used by the futex code. 18562efaca92SBenjamin Herrenschmidt * 18572efaca92SBenjamin Herrenschmidt * The main difference with get_user_pages() is that this function will 18582efaca92SBenjamin Herrenschmidt * unconditionally call handle_mm_fault() which will in turn perform all the 18592efaca92SBenjamin Herrenschmidt * necessary SW fixup of the dirty and young bits in the PTE, while 18602efaca92SBenjamin Herrenschmidt * handle_mm_fault() only guarantees to update these in the struct page. 18612efaca92SBenjamin Herrenschmidt * 18622efaca92SBenjamin Herrenschmidt * This is important for some architectures where those bits also gate the 18632efaca92SBenjamin Herrenschmidt * access permission to the page because they are maintained in software. On 18642efaca92SBenjamin Herrenschmidt * such architectures, gup() will not be enough to make a subsequent access 18652efaca92SBenjamin Herrenschmidt * succeed. 18662efaca92SBenjamin Herrenschmidt * 18672efaca92SBenjamin Herrenschmidt * This should be called with the mm_sem held for read. 18682efaca92SBenjamin Herrenschmidt */ 18692efaca92SBenjamin Herrenschmidt int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm, 18702efaca92SBenjamin Herrenschmidt unsigned long address, unsigned int fault_flags) 18712efaca92SBenjamin Herrenschmidt { 18722efaca92SBenjamin Herrenschmidt struct vm_area_struct *vma; 18732efaca92SBenjamin Herrenschmidt int ret; 18742efaca92SBenjamin Herrenschmidt 18752efaca92SBenjamin Herrenschmidt vma = find_extend_vma(mm, address); 18762efaca92SBenjamin Herrenschmidt if (!vma || address < vma->vm_start) 18772efaca92SBenjamin Herrenschmidt return -EFAULT; 18782efaca92SBenjamin Herrenschmidt 18792efaca92SBenjamin Herrenschmidt ret = handle_mm_fault(mm, vma, address, fault_flags); 18802efaca92SBenjamin Herrenschmidt if (ret & VM_FAULT_ERROR) { 18812efaca92SBenjamin Herrenschmidt if (ret & VM_FAULT_OOM) 18822efaca92SBenjamin Herrenschmidt return -ENOMEM; 18832efaca92SBenjamin Herrenschmidt if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE)) 18842efaca92SBenjamin Herrenschmidt return -EHWPOISON; 18852efaca92SBenjamin Herrenschmidt if (ret & VM_FAULT_SIGBUS) 18862efaca92SBenjamin Herrenschmidt return -EFAULT; 18872efaca92SBenjamin Herrenschmidt BUG(); 18882efaca92SBenjamin Herrenschmidt } 18892efaca92SBenjamin Herrenschmidt if (tsk) { 18902efaca92SBenjamin Herrenschmidt if (ret & VM_FAULT_MAJOR) 18912efaca92SBenjamin Herrenschmidt tsk->maj_flt++; 18922efaca92SBenjamin Herrenschmidt else 18932efaca92SBenjamin Herrenschmidt tsk->min_flt++; 18942efaca92SBenjamin Herrenschmidt } 18952efaca92SBenjamin Herrenschmidt return 0; 18962efaca92SBenjamin Herrenschmidt } 18972efaca92SBenjamin Herrenschmidt 18982efaca92SBenjamin Herrenschmidt /* 1899d2bf6be8SNick Piggin * get_user_pages() - pin user pages in memory 1900e7f22e20SStephen Wilson * @tsk: the task_struct to use for page fault accounting, or 1901e7f22e20SStephen Wilson * NULL if faults are not to be recorded. 1902d2bf6be8SNick Piggin * @mm: mm_struct of target mm 1903d2bf6be8SNick Piggin * @start: starting user address 19049d73777eSPeter Zijlstra * @nr_pages: number of pages from start to pin 1905d2bf6be8SNick Piggin * @write: whether pages will be written to by the caller 1906d2bf6be8SNick Piggin * @force: whether to force write access even if user mapping is 1907d2bf6be8SNick Piggin * readonly. This will result in the page being COWed even 1908d2bf6be8SNick Piggin * in MAP_SHARED mappings. You do not want this. 1909d2bf6be8SNick Piggin * @pages: array that receives pointers to the pages pinned. 1910d2bf6be8SNick Piggin * Should be at least nr_pages long. Or NULL, if caller 1911d2bf6be8SNick Piggin * only intends to ensure the pages are faulted in. 1912d2bf6be8SNick Piggin * @vmas: array of pointers to vmas corresponding to each page. 1913d2bf6be8SNick Piggin * Or NULL if the caller does not require them. 1914d2bf6be8SNick Piggin * 1915d2bf6be8SNick Piggin * Returns number of pages pinned. This may be fewer than the number 19169d73777eSPeter Zijlstra * requested. If nr_pages is 0 or negative, returns 0. If no pages 1917d2bf6be8SNick Piggin * were pinned, returns -errno. Each page returned must be released 1918d2bf6be8SNick Piggin * with a put_page() call when it is finished with. vmas will only 1919d2bf6be8SNick Piggin * remain valid while mmap_sem is held. 1920d2bf6be8SNick Piggin * 1921d2bf6be8SNick Piggin * Must be called with mmap_sem held for read or write. 1922d2bf6be8SNick Piggin * 1923d2bf6be8SNick Piggin * get_user_pages walks a process's page tables and takes a reference to 1924d2bf6be8SNick Piggin * each struct page that each user address corresponds to at a given 1925d2bf6be8SNick Piggin * instant. That is, it takes the page that would be accessed if a user 1926d2bf6be8SNick Piggin * thread accesses the given user virtual address at that instant. 1927d2bf6be8SNick Piggin * 1928d2bf6be8SNick Piggin * This does not guarantee that the page exists in the user mappings when 1929d2bf6be8SNick Piggin * get_user_pages returns, and there may even be a completely different 1930d2bf6be8SNick Piggin * page there in some cases (eg. if mmapped pagecache has been invalidated 1931d2bf6be8SNick Piggin * and subsequently re faulted). However it does guarantee that the page 1932d2bf6be8SNick Piggin * won't be freed completely. And mostly callers simply care that the page 1933d2bf6be8SNick Piggin * contains data that was valid *at some point in time*. Typically, an IO 1934d2bf6be8SNick Piggin * or similar operation cannot guarantee anything stronger anyway because 1935d2bf6be8SNick Piggin * locks can't be held over the syscall boundary. 1936d2bf6be8SNick Piggin * 1937d2bf6be8SNick Piggin * If write=0, the page must not be written to. If the page is written to, 1938d2bf6be8SNick Piggin * set_page_dirty (or set_page_dirty_lock, as appropriate) must be called 1939d2bf6be8SNick Piggin * after the page is finished with, and before put_page is called. 1940d2bf6be8SNick Piggin * 1941d2bf6be8SNick Piggin * get_user_pages is typically used for fewer-copy IO operations, to get a 1942d2bf6be8SNick Piggin * handle on the memory by some means other than accesses via the user virtual 1943d2bf6be8SNick Piggin * addresses. The pages may be submitted for DMA to devices or accessed via 1944d2bf6be8SNick Piggin * their kernel linear mapping (via the kmap APIs). Care should be taken to 1945d2bf6be8SNick Piggin * use the correct cache flushing APIs. 1946d2bf6be8SNick Piggin * 1947d2bf6be8SNick Piggin * See also get_user_pages_fast, for performance critical applications. 1948d2bf6be8SNick Piggin */ 1949b291f000SNick Piggin int get_user_pages(struct task_struct *tsk, struct mm_struct *mm, 19509d73777eSPeter Zijlstra unsigned long start, int nr_pages, int write, int force, 1951b291f000SNick Piggin struct page **pages, struct vm_area_struct **vmas) 1952b291f000SNick Piggin { 195358fa879eSHugh Dickins int flags = FOLL_TOUCH; 1954b291f000SNick Piggin 195558fa879eSHugh Dickins if (pages) 195658fa879eSHugh Dickins flags |= FOLL_GET; 1957b291f000SNick Piggin if (write) 195858fa879eSHugh Dickins flags |= FOLL_WRITE; 1959b291f000SNick Piggin if (force) 196058fa879eSHugh Dickins flags |= FOLL_FORCE; 1961b291f000SNick Piggin 196253a7706dSMichel Lespinasse return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas, 196353a7706dSMichel Lespinasse NULL); 1964b291f000SNick Piggin } 19651da177e4SLinus Torvalds EXPORT_SYMBOL(get_user_pages); 19661da177e4SLinus Torvalds 1967f3e8fccdSHugh Dickins /** 1968f3e8fccdSHugh Dickins * get_dump_page() - pin user page in memory while writing it to core dump 1969f3e8fccdSHugh Dickins * @addr: user address 1970f3e8fccdSHugh Dickins * 1971f3e8fccdSHugh Dickins * Returns struct page pointer of user page pinned for dump, 1972f3e8fccdSHugh Dickins * to be freed afterwards by page_cache_release() or put_page(). 1973f3e8fccdSHugh Dickins * 1974f3e8fccdSHugh Dickins * Returns NULL on any kind of failure - a hole must then be inserted into 1975f3e8fccdSHugh Dickins * the corefile, to preserve alignment with its headers; and also returns 1976f3e8fccdSHugh Dickins * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found - 1977f3e8fccdSHugh Dickins * allowing a hole to be left in the corefile to save diskspace. 1978f3e8fccdSHugh Dickins * 1979f3e8fccdSHugh Dickins * Called without mmap_sem, but after all other threads have been killed. 1980f3e8fccdSHugh Dickins */ 1981f3e8fccdSHugh Dickins #ifdef CONFIG_ELF_CORE 1982f3e8fccdSHugh Dickins struct page *get_dump_page(unsigned long addr) 1983f3e8fccdSHugh Dickins { 1984f3e8fccdSHugh Dickins struct vm_area_struct *vma; 1985f3e8fccdSHugh Dickins struct page *page; 1986f3e8fccdSHugh Dickins 1987f3e8fccdSHugh Dickins if (__get_user_pages(current, current->mm, addr, 1, 198853a7706dSMichel Lespinasse FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma, 198953a7706dSMichel Lespinasse NULL) < 1) 1990f3e8fccdSHugh Dickins return NULL; 1991f3e8fccdSHugh Dickins flush_cache_page(vma, addr, page_to_pfn(page)); 1992f3e8fccdSHugh Dickins return page; 1993f3e8fccdSHugh Dickins } 1994f3e8fccdSHugh Dickins #endif /* CONFIG_ELF_CORE */ 1995f3e8fccdSHugh Dickins 199625ca1d6cSNamhyung Kim pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr, 1997920c7a5dSHarvey Harrison spinlock_t **ptl) 1998c9cfcddfSLinus Torvalds { 1999c9cfcddfSLinus Torvalds pgd_t * pgd = pgd_offset(mm, addr); 2000c9cfcddfSLinus Torvalds pud_t * pud = pud_alloc(mm, pgd, addr); 2001c9cfcddfSLinus Torvalds if (pud) { 200249c91fb0STrond Myklebust pmd_t * pmd = pmd_alloc(mm, pud, addr); 2003f66055abSAndrea Arcangeli if (pmd) { 2004f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 2005c9cfcddfSLinus Torvalds return pte_alloc_map_lock(mm, pmd, addr, ptl); 2006c9cfcddfSLinus Torvalds } 2007f66055abSAndrea Arcangeli } 2008c9cfcddfSLinus Torvalds return NULL; 2009c9cfcddfSLinus Torvalds } 2010c9cfcddfSLinus Torvalds 20111da177e4SLinus Torvalds /* 2012238f58d8SLinus Torvalds * This is the old fallback for page remapping. 2013238f58d8SLinus Torvalds * 2014238f58d8SLinus Torvalds * For historical reasons, it only allows reserved pages. Only 2015238f58d8SLinus Torvalds * old drivers should use this, and they needed to mark their 2016238f58d8SLinus Torvalds * pages reserved for the old functions anyway. 2017238f58d8SLinus Torvalds */ 2018423bad60SNick Piggin static int insert_page(struct vm_area_struct *vma, unsigned long addr, 2019423bad60SNick Piggin struct page *page, pgprot_t prot) 2020238f58d8SLinus Torvalds { 2021423bad60SNick Piggin struct mm_struct *mm = vma->vm_mm; 2022238f58d8SLinus Torvalds int retval; 2023238f58d8SLinus Torvalds pte_t *pte; 2024238f58d8SLinus Torvalds spinlock_t *ptl; 2025238f58d8SLinus Torvalds 2026238f58d8SLinus Torvalds retval = -EINVAL; 2027a145dd41SLinus Torvalds if (PageAnon(page)) 20285b4e655eSKAMEZAWA Hiroyuki goto out; 2029238f58d8SLinus Torvalds retval = -ENOMEM; 2030238f58d8SLinus Torvalds flush_dcache_page(page); 2031c9cfcddfSLinus Torvalds pte = get_locked_pte(mm, addr, &ptl); 2032238f58d8SLinus Torvalds if (!pte) 20335b4e655eSKAMEZAWA Hiroyuki goto out; 2034238f58d8SLinus Torvalds retval = -EBUSY; 2035238f58d8SLinus Torvalds if (!pte_none(*pte)) 2036238f58d8SLinus Torvalds goto out_unlock; 2037238f58d8SLinus Torvalds 2038238f58d8SLinus Torvalds /* Ok, finally just insert the thing.. */ 2039238f58d8SLinus Torvalds get_page(page); 204034e55232SKAMEZAWA Hiroyuki inc_mm_counter_fast(mm, MM_FILEPAGES); 2041238f58d8SLinus Torvalds page_add_file_rmap(page); 2042238f58d8SLinus Torvalds set_pte_at(mm, addr, pte, mk_pte(page, prot)); 2043238f58d8SLinus Torvalds 2044238f58d8SLinus Torvalds retval = 0; 20458a9f3ccdSBalbir Singh pte_unmap_unlock(pte, ptl); 20468a9f3ccdSBalbir Singh return retval; 2047238f58d8SLinus Torvalds out_unlock: 2048238f58d8SLinus Torvalds pte_unmap_unlock(pte, ptl); 2049238f58d8SLinus Torvalds out: 2050238f58d8SLinus Torvalds return retval; 2051238f58d8SLinus Torvalds } 2052238f58d8SLinus Torvalds 2053bfa5bf6dSRolf Eike Beer /** 2054bfa5bf6dSRolf Eike Beer * vm_insert_page - insert single page into user vma 2055bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 2056bfa5bf6dSRolf Eike Beer * @addr: target user address of this page 2057bfa5bf6dSRolf Eike Beer * @page: source kernel page 2058bfa5bf6dSRolf Eike Beer * 2059a145dd41SLinus Torvalds * This allows drivers to insert individual pages they've allocated 2060a145dd41SLinus Torvalds * into a user vma. 2061a145dd41SLinus Torvalds * 2062a145dd41SLinus Torvalds * The page has to be a nice clean _individual_ kernel allocation. 2063a145dd41SLinus Torvalds * If you allocate a compound page, you need to have marked it as 2064a145dd41SLinus Torvalds * such (__GFP_COMP), or manually just split the page up yourself 20658dfcc9baSNick Piggin * (see split_page()). 2066a145dd41SLinus Torvalds * 2067a145dd41SLinus Torvalds * NOTE! Traditionally this was done with "remap_pfn_range()" which 2068a145dd41SLinus Torvalds * took an arbitrary page protection parameter. This doesn't allow 2069a145dd41SLinus Torvalds * that. Your vma protection will have to be set up correctly, which 2070a145dd41SLinus Torvalds * means that if you want a shared writable mapping, you'd better 2071a145dd41SLinus Torvalds * ask for a shared writable mapping! 2072a145dd41SLinus Torvalds * 2073a145dd41SLinus Torvalds * The page does not need to be reserved. 2074a145dd41SLinus Torvalds */ 2075423bad60SNick Piggin int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, 2076423bad60SNick Piggin struct page *page) 2077a145dd41SLinus Torvalds { 2078a145dd41SLinus Torvalds if (addr < vma->vm_start || addr >= vma->vm_end) 2079a145dd41SLinus Torvalds return -EFAULT; 2080a145dd41SLinus Torvalds if (!page_count(page)) 2081a145dd41SLinus Torvalds return -EINVAL; 20824d7672b4SLinus Torvalds vma->vm_flags |= VM_INSERTPAGE; 2083423bad60SNick Piggin return insert_page(vma, addr, page, vma->vm_page_prot); 2084a145dd41SLinus Torvalds } 2085e3c3374fSLinus Torvalds EXPORT_SYMBOL(vm_insert_page); 2086a145dd41SLinus Torvalds 2087423bad60SNick Piggin static int insert_pfn(struct vm_area_struct *vma, unsigned long addr, 2088423bad60SNick Piggin unsigned long pfn, pgprot_t prot) 2089423bad60SNick Piggin { 2090423bad60SNick Piggin struct mm_struct *mm = vma->vm_mm; 2091423bad60SNick Piggin int retval; 2092423bad60SNick Piggin pte_t *pte, entry; 2093423bad60SNick Piggin spinlock_t *ptl; 2094423bad60SNick Piggin 2095423bad60SNick Piggin retval = -ENOMEM; 2096423bad60SNick Piggin pte = get_locked_pte(mm, addr, &ptl); 2097423bad60SNick Piggin if (!pte) 2098423bad60SNick Piggin goto out; 2099423bad60SNick Piggin retval = -EBUSY; 2100423bad60SNick Piggin if (!pte_none(*pte)) 2101423bad60SNick Piggin goto out_unlock; 2102423bad60SNick Piggin 2103423bad60SNick Piggin /* Ok, finally just insert the thing.. */ 2104423bad60SNick Piggin entry = pte_mkspecial(pfn_pte(pfn, prot)); 2105423bad60SNick Piggin set_pte_at(mm, addr, pte, entry); 21064b3073e1SRussell King update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */ 2107423bad60SNick Piggin 2108423bad60SNick Piggin retval = 0; 2109423bad60SNick Piggin out_unlock: 2110423bad60SNick Piggin pte_unmap_unlock(pte, ptl); 2111423bad60SNick Piggin out: 2112423bad60SNick Piggin return retval; 2113423bad60SNick Piggin } 2114423bad60SNick Piggin 2115e0dc0d8fSNick Piggin /** 2116e0dc0d8fSNick Piggin * vm_insert_pfn - insert single pfn into user vma 2117e0dc0d8fSNick Piggin * @vma: user vma to map to 2118e0dc0d8fSNick Piggin * @addr: target user address of this page 2119e0dc0d8fSNick Piggin * @pfn: source kernel pfn 2120e0dc0d8fSNick Piggin * 2121e0dc0d8fSNick Piggin * Similar to vm_inert_page, this allows drivers to insert individual pages 2122e0dc0d8fSNick Piggin * they've allocated into a user vma. Same comments apply. 2123e0dc0d8fSNick Piggin * 2124e0dc0d8fSNick Piggin * This function should only be called from a vm_ops->fault handler, and 2125e0dc0d8fSNick Piggin * in that case the handler should return NULL. 21260d71d10aSNick Piggin * 21270d71d10aSNick Piggin * vma cannot be a COW mapping. 21280d71d10aSNick Piggin * 21290d71d10aSNick Piggin * As this is called only for pages that do not currently exist, we 21300d71d10aSNick Piggin * do not need to flush old virtual caches or the TLB. 2131e0dc0d8fSNick Piggin */ 2132e0dc0d8fSNick Piggin int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr, 2133e0dc0d8fSNick Piggin unsigned long pfn) 2134e0dc0d8fSNick Piggin { 21352ab64037Svenkatesh.pallipadi@intel.com int ret; 2136e4b866edSvenkatesh.pallipadi@intel.com pgprot_t pgprot = vma->vm_page_prot; 21377e675137SNick Piggin /* 21387e675137SNick Piggin * Technically, architectures with pte_special can avoid all these 21397e675137SNick Piggin * restrictions (same for remap_pfn_range). However we would like 21407e675137SNick Piggin * consistency in testing and feature parity among all, so we should 21417e675137SNick Piggin * try to keep these invariants in place for everybody. 21427e675137SNick Piggin */ 2143b379d790SJared Hulbert BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))); 2144b379d790SJared Hulbert BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) == 2145b379d790SJared Hulbert (VM_PFNMAP|VM_MIXEDMAP)); 2146b379d790SJared Hulbert BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); 2147b379d790SJared Hulbert BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn)); 2148e0dc0d8fSNick Piggin 2149423bad60SNick Piggin if (addr < vma->vm_start || addr >= vma->vm_end) 2150423bad60SNick Piggin return -EFAULT; 2151e4b866edSvenkatesh.pallipadi@intel.com if (track_pfn_vma_new(vma, &pgprot, pfn, PAGE_SIZE)) 21522ab64037Svenkatesh.pallipadi@intel.com return -EINVAL; 21532ab64037Svenkatesh.pallipadi@intel.com 2154e4b866edSvenkatesh.pallipadi@intel.com ret = insert_pfn(vma, addr, pfn, pgprot); 21552ab64037Svenkatesh.pallipadi@intel.com 21562ab64037Svenkatesh.pallipadi@intel.com if (ret) 21572ab64037Svenkatesh.pallipadi@intel.com untrack_pfn_vma(vma, pfn, PAGE_SIZE); 21582ab64037Svenkatesh.pallipadi@intel.com 21592ab64037Svenkatesh.pallipadi@intel.com return ret; 2160e0dc0d8fSNick Piggin } 2161e0dc0d8fSNick Piggin EXPORT_SYMBOL(vm_insert_pfn); 2162e0dc0d8fSNick Piggin 2163423bad60SNick Piggin int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr, 2164423bad60SNick Piggin unsigned long pfn) 2165423bad60SNick Piggin { 2166423bad60SNick Piggin BUG_ON(!(vma->vm_flags & VM_MIXEDMAP)); 2167423bad60SNick Piggin 2168423bad60SNick Piggin if (addr < vma->vm_start || addr >= vma->vm_end) 2169423bad60SNick Piggin return -EFAULT; 2170423bad60SNick Piggin 2171423bad60SNick Piggin /* 2172423bad60SNick Piggin * If we don't have pte special, then we have to use the pfn_valid() 2173423bad60SNick Piggin * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must* 2174423bad60SNick Piggin * refcount the page if pfn_valid is true (hence insert_page rather 217562eede62SHugh Dickins * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP 217662eede62SHugh Dickins * without pte special, it would there be refcounted as a normal page. 2177423bad60SNick Piggin */ 2178423bad60SNick Piggin if (!HAVE_PTE_SPECIAL && pfn_valid(pfn)) { 2179423bad60SNick Piggin struct page *page; 2180423bad60SNick Piggin 2181423bad60SNick Piggin page = pfn_to_page(pfn); 2182423bad60SNick Piggin return insert_page(vma, addr, page, vma->vm_page_prot); 2183423bad60SNick Piggin } 2184423bad60SNick Piggin return insert_pfn(vma, addr, pfn, vma->vm_page_prot); 2185423bad60SNick Piggin } 2186423bad60SNick Piggin EXPORT_SYMBOL(vm_insert_mixed); 2187423bad60SNick Piggin 2188a145dd41SLinus Torvalds /* 21891da177e4SLinus Torvalds * maps a range of physical memory into the requested pages. the old 21901da177e4SLinus Torvalds * mappings are removed. any references to nonexistent pages results 21911da177e4SLinus Torvalds * in null mappings (currently treated as "copy-on-access") 21921da177e4SLinus Torvalds */ 21931da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd, 21941da177e4SLinus Torvalds unsigned long addr, unsigned long end, 21951da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 21961da177e4SLinus Torvalds { 21971da177e4SLinus Torvalds pte_t *pte; 2198c74df32cSHugh Dickins spinlock_t *ptl; 21991da177e4SLinus Torvalds 2200c74df32cSHugh Dickins pte = pte_alloc_map_lock(mm, pmd, addr, &ptl); 22011da177e4SLinus Torvalds if (!pte) 22021da177e4SLinus Torvalds return -ENOMEM; 22036606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 22041da177e4SLinus Torvalds do { 22051da177e4SLinus Torvalds BUG_ON(!pte_none(*pte)); 22067e675137SNick Piggin set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot))); 22071da177e4SLinus Torvalds pfn++; 22081da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 22096606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 2210c74df32cSHugh Dickins pte_unmap_unlock(pte - 1, ptl); 22111da177e4SLinus Torvalds return 0; 22121da177e4SLinus Torvalds } 22131da177e4SLinus Torvalds 22141da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud, 22151da177e4SLinus Torvalds unsigned long addr, unsigned long end, 22161da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 22171da177e4SLinus Torvalds { 22181da177e4SLinus Torvalds pmd_t *pmd; 22191da177e4SLinus Torvalds unsigned long next; 22201da177e4SLinus Torvalds 22211da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 22221da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, addr); 22231da177e4SLinus Torvalds if (!pmd) 22241da177e4SLinus Torvalds return -ENOMEM; 2225f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 22261da177e4SLinus Torvalds do { 22271da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 22281da177e4SLinus Torvalds if (remap_pte_range(mm, pmd, addr, next, 22291da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot)) 22301da177e4SLinus Torvalds return -ENOMEM; 22311da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 22321da177e4SLinus Torvalds return 0; 22331da177e4SLinus Torvalds } 22341da177e4SLinus Torvalds 22351da177e4SLinus Torvalds static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd, 22361da177e4SLinus Torvalds unsigned long addr, unsigned long end, 22371da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 22381da177e4SLinus Torvalds { 22391da177e4SLinus Torvalds pud_t *pud; 22401da177e4SLinus Torvalds unsigned long next; 22411da177e4SLinus Torvalds 22421da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 22431da177e4SLinus Torvalds pud = pud_alloc(mm, pgd, addr); 22441da177e4SLinus Torvalds if (!pud) 22451da177e4SLinus Torvalds return -ENOMEM; 22461da177e4SLinus Torvalds do { 22471da177e4SLinus Torvalds next = pud_addr_end(addr, end); 22481da177e4SLinus Torvalds if (remap_pmd_range(mm, pud, addr, next, 22491da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot)) 22501da177e4SLinus Torvalds return -ENOMEM; 22511da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 22521da177e4SLinus Torvalds return 0; 22531da177e4SLinus Torvalds } 22541da177e4SLinus Torvalds 2255bfa5bf6dSRolf Eike Beer /** 2256bfa5bf6dSRolf Eike Beer * remap_pfn_range - remap kernel memory to userspace 2257bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 2258bfa5bf6dSRolf Eike Beer * @addr: target user address to start at 2259bfa5bf6dSRolf Eike Beer * @pfn: physical address of kernel memory 2260bfa5bf6dSRolf Eike Beer * @size: size of map area 2261bfa5bf6dSRolf Eike Beer * @prot: page protection flags for this mapping 2262bfa5bf6dSRolf Eike Beer * 2263bfa5bf6dSRolf Eike Beer * Note: this is only safe if the mm semaphore is held when called. 2264bfa5bf6dSRolf Eike Beer */ 22651da177e4SLinus Torvalds int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 22661da177e4SLinus Torvalds unsigned long pfn, unsigned long size, pgprot_t prot) 22671da177e4SLinus Torvalds { 22681da177e4SLinus Torvalds pgd_t *pgd; 22691da177e4SLinus Torvalds unsigned long next; 22702d15cab8SHugh Dickins unsigned long end = addr + PAGE_ALIGN(size); 22711da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 22721da177e4SLinus Torvalds int err; 22731da177e4SLinus Torvalds 22741da177e4SLinus Torvalds /* 22751da177e4SLinus Torvalds * Physically remapped pages are special. Tell the 22761da177e4SLinus Torvalds * rest of the world about it: 22771da177e4SLinus Torvalds * VM_IO tells people not to look at these pages 22781da177e4SLinus Torvalds * (accesses can have side effects). 22790b14c179SHugh Dickins * VM_RESERVED is specified all over the place, because 22800b14c179SHugh Dickins * in 2.4 it kept swapout's vma scan off this vma; but 22810b14c179SHugh Dickins * in 2.6 the LRU scan won't even find its pages, so this 22820b14c179SHugh Dickins * flag means no more than count its pages in reserved_vm, 22830b14c179SHugh Dickins * and omit it from core dump, even when VM_IO turned off. 22846aab341eSLinus Torvalds * VM_PFNMAP tells the core MM that the base pages are just 22856aab341eSLinus Torvalds * raw PFN mappings, and do not have a "struct page" associated 22866aab341eSLinus Torvalds * with them. 2287fb155c16SLinus Torvalds * 2288fb155c16SLinus Torvalds * There's a horrible special case to handle copy-on-write 2289fb155c16SLinus Torvalds * behaviour that some programs depend on. We mark the "original" 2290fb155c16SLinus Torvalds * un-COW'ed pages by matching them up with "vma->vm_pgoff". 22911da177e4SLinus Torvalds */ 22924bb9c5c0SPallipadi, Venkatesh if (addr == vma->vm_start && end == vma->vm_end) { 22936aab341eSLinus Torvalds vma->vm_pgoff = pfn; 2294895791daSPallipadi, Venkatesh vma->vm_flags |= VM_PFN_AT_MMAP; 22954bb9c5c0SPallipadi, Venkatesh } else if (is_cow_mapping(vma->vm_flags)) 22963c8bb73aSvenkatesh.pallipadi@intel.com return -EINVAL; 2297fb155c16SLinus Torvalds 2298fb155c16SLinus Torvalds vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP; 22991da177e4SLinus Torvalds 2300e4b866edSvenkatesh.pallipadi@intel.com err = track_pfn_vma_new(vma, &prot, pfn, PAGE_ALIGN(size)); 2301a3670613Svenkatesh.pallipadi@intel.com if (err) { 2302a3670613Svenkatesh.pallipadi@intel.com /* 2303a3670613Svenkatesh.pallipadi@intel.com * To indicate that track_pfn related cleanup is not 2304a3670613Svenkatesh.pallipadi@intel.com * needed from higher level routine calling unmap_vmas 2305a3670613Svenkatesh.pallipadi@intel.com */ 2306a3670613Svenkatesh.pallipadi@intel.com vma->vm_flags &= ~(VM_IO | VM_RESERVED | VM_PFNMAP); 2307895791daSPallipadi, Venkatesh vma->vm_flags &= ~VM_PFN_AT_MMAP; 23082ab64037Svenkatesh.pallipadi@intel.com return -EINVAL; 2309a3670613Svenkatesh.pallipadi@intel.com } 23102ab64037Svenkatesh.pallipadi@intel.com 23111da177e4SLinus Torvalds BUG_ON(addr >= end); 23121da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 23131da177e4SLinus Torvalds pgd = pgd_offset(mm, addr); 23141da177e4SLinus Torvalds flush_cache_range(vma, addr, end); 23151da177e4SLinus Torvalds do { 23161da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 23171da177e4SLinus Torvalds err = remap_pud_range(mm, pgd, addr, next, 23181da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot); 23191da177e4SLinus Torvalds if (err) 23201da177e4SLinus Torvalds break; 23211da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 23222ab64037Svenkatesh.pallipadi@intel.com 23232ab64037Svenkatesh.pallipadi@intel.com if (err) 23242ab64037Svenkatesh.pallipadi@intel.com untrack_pfn_vma(vma, pfn, PAGE_ALIGN(size)); 23252ab64037Svenkatesh.pallipadi@intel.com 23261da177e4SLinus Torvalds return err; 23271da177e4SLinus Torvalds } 23281da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range); 23291da177e4SLinus Torvalds 2330aee16b3cSJeremy Fitzhardinge static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd, 2331aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2332aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 2333aee16b3cSJeremy Fitzhardinge { 2334aee16b3cSJeremy Fitzhardinge pte_t *pte; 2335aee16b3cSJeremy Fitzhardinge int err; 23362f569afdSMartin Schwidefsky pgtable_t token; 233794909914SBorislav Petkov spinlock_t *uninitialized_var(ptl); 2338aee16b3cSJeremy Fitzhardinge 2339aee16b3cSJeremy Fitzhardinge pte = (mm == &init_mm) ? 2340aee16b3cSJeremy Fitzhardinge pte_alloc_kernel(pmd, addr) : 2341aee16b3cSJeremy Fitzhardinge pte_alloc_map_lock(mm, pmd, addr, &ptl); 2342aee16b3cSJeremy Fitzhardinge if (!pte) 2343aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2344aee16b3cSJeremy Fitzhardinge 2345aee16b3cSJeremy Fitzhardinge BUG_ON(pmd_huge(*pmd)); 2346aee16b3cSJeremy Fitzhardinge 234738e0edb1SJeremy Fitzhardinge arch_enter_lazy_mmu_mode(); 234838e0edb1SJeremy Fitzhardinge 23492f569afdSMartin Schwidefsky token = pmd_pgtable(*pmd); 2350aee16b3cSJeremy Fitzhardinge 2351aee16b3cSJeremy Fitzhardinge do { 2352c36987e2SDaisuke Nishimura err = fn(pte++, token, addr, data); 2353aee16b3cSJeremy Fitzhardinge if (err) 2354aee16b3cSJeremy Fitzhardinge break; 2355c36987e2SDaisuke Nishimura } while (addr += PAGE_SIZE, addr != end); 2356aee16b3cSJeremy Fitzhardinge 235738e0edb1SJeremy Fitzhardinge arch_leave_lazy_mmu_mode(); 235838e0edb1SJeremy Fitzhardinge 2359aee16b3cSJeremy Fitzhardinge if (mm != &init_mm) 2360aee16b3cSJeremy Fitzhardinge pte_unmap_unlock(pte-1, ptl); 2361aee16b3cSJeremy Fitzhardinge return err; 2362aee16b3cSJeremy Fitzhardinge } 2363aee16b3cSJeremy Fitzhardinge 2364aee16b3cSJeremy Fitzhardinge static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud, 2365aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2366aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 2367aee16b3cSJeremy Fitzhardinge { 2368aee16b3cSJeremy Fitzhardinge pmd_t *pmd; 2369aee16b3cSJeremy Fitzhardinge unsigned long next; 2370aee16b3cSJeremy Fitzhardinge int err; 2371aee16b3cSJeremy Fitzhardinge 2372ceb86879SAndi Kleen BUG_ON(pud_huge(*pud)); 2373ceb86879SAndi Kleen 2374aee16b3cSJeremy Fitzhardinge pmd = pmd_alloc(mm, pud, addr); 2375aee16b3cSJeremy Fitzhardinge if (!pmd) 2376aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2377aee16b3cSJeremy Fitzhardinge do { 2378aee16b3cSJeremy Fitzhardinge next = pmd_addr_end(addr, end); 2379aee16b3cSJeremy Fitzhardinge err = apply_to_pte_range(mm, pmd, addr, next, fn, data); 2380aee16b3cSJeremy Fitzhardinge if (err) 2381aee16b3cSJeremy Fitzhardinge break; 2382aee16b3cSJeremy Fitzhardinge } while (pmd++, addr = next, addr != end); 2383aee16b3cSJeremy Fitzhardinge return err; 2384aee16b3cSJeremy Fitzhardinge } 2385aee16b3cSJeremy Fitzhardinge 2386aee16b3cSJeremy Fitzhardinge static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd, 2387aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2388aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 2389aee16b3cSJeremy Fitzhardinge { 2390aee16b3cSJeremy Fitzhardinge pud_t *pud; 2391aee16b3cSJeremy Fitzhardinge unsigned long next; 2392aee16b3cSJeremy Fitzhardinge int err; 2393aee16b3cSJeremy Fitzhardinge 2394aee16b3cSJeremy Fitzhardinge pud = pud_alloc(mm, pgd, addr); 2395aee16b3cSJeremy Fitzhardinge if (!pud) 2396aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2397aee16b3cSJeremy Fitzhardinge do { 2398aee16b3cSJeremy Fitzhardinge next = pud_addr_end(addr, end); 2399aee16b3cSJeremy Fitzhardinge err = apply_to_pmd_range(mm, pud, addr, next, fn, data); 2400aee16b3cSJeremy Fitzhardinge if (err) 2401aee16b3cSJeremy Fitzhardinge break; 2402aee16b3cSJeremy Fitzhardinge } while (pud++, addr = next, addr != end); 2403aee16b3cSJeremy Fitzhardinge return err; 2404aee16b3cSJeremy Fitzhardinge } 2405aee16b3cSJeremy Fitzhardinge 2406aee16b3cSJeremy Fitzhardinge /* 2407aee16b3cSJeremy Fitzhardinge * Scan a region of virtual memory, filling in page tables as necessary 2408aee16b3cSJeremy Fitzhardinge * and calling a provided function on each leaf page table. 2409aee16b3cSJeremy Fitzhardinge */ 2410aee16b3cSJeremy Fitzhardinge int apply_to_page_range(struct mm_struct *mm, unsigned long addr, 2411aee16b3cSJeremy Fitzhardinge unsigned long size, pte_fn_t fn, void *data) 2412aee16b3cSJeremy Fitzhardinge { 2413aee16b3cSJeremy Fitzhardinge pgd_t *pgd; 2414aee16b3cSJeremy Fitzhardinge unsigned long next; 241557250a5bSJeremy Fitzhardinge unsigned long end = addr + size; 2416aee16b3cSJeremy Fitzhardinge int err; 2417aee16b3cSJeremy Fitzhardinge 2418aee16b3cSJeremy Fitzhardinge BUG_ON(addr >= end); 2419aee16b3cSJeremy Fitzhardinge pgd = pgd_offset(mm, addr); 2420aee16b3cSJeremy Fitzhardinge do { 2421aee16b3cSJeremy Fitzhardinge next = pgd_addr_end(addr, end); 2422aee16b3cSJeremy Fitzhardinge err = apply_to_pud_range(mm, pgd, addr, next, fn, data); 2423aee16b3cSJeremy Fitzhardinge if (err) 2424aee16b3cSJeremy Fitzhardinge break; 2425aee16b3cSJeremy Fitzhardinge } while (pgd++, addr = next, addr != end); 242657250a5bSJeremy Fitzhardinge 2427aee16b3cSJeremy Fitzhardinge return err; 2428aee16b3cSJeremy Fitzhardinge } 2429aee16b3cSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(apply_to_page_range); 2430aee16b3cSJeremy Fitzhardinge 24311da177e4SLinus Torvalds /* 24328f4e2101SHugh Dickins * handle_pte_fault chooses page fault handler according to an entry 24338f4e2101SHugh Dickins * which was read non-atomically. Before making any commitment, on 24348f4e2101SHugh Dickins * those architectures or configurations (e.g. i386 with PAE) which 2435a335b2e1SRyota Ozaki * might give a mix of unmatched parts, do_swap_page and do_nonlinear_fault 24368f4e2101SHugh Dickins * must check under lock before unmapping the pte and proceeding 24378f4e2101SHugh Dickins * (but do_wp_page is only called after already making such a check; 2438a335b2e1SRyota Ozaki * and do_anonymous_page can safely check later on). 24398f4e2101SHugh Dickins */ 24404c21e2f2SHugh Dickins static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd, 24418f4e2101SHugh Dickins pte_t *page_table, pte_t orig_pte) 24428f4e2101SHugh Dickins { 24438f4e2101SHugh Dickins int same = 1; 24448f4e2101SHugh Dickins #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT) 24458f4e2101SHugh Dickins if (sizeof(pte_t) > sizeof(unsigned long)) { 24464c21e2f2SHugh Dickins spinlock_t *ptl = pte_lockptr(mm, pmd); 24474c21e2f2SHugh Dickins spin_lock(ptl); 24488f4e2101SHugh Dickins same = pte_same(*page_table, orig_pte); 24494c21e2f2SHugh Dickins spin_unlock(ptl); 24508f4e2101SHugh Dickins } 24518f4e2101SHugh Dickins #endif 24528f4e2101SHugh Dickins pte_unmap(page_table); 24538f4e2101SHugh Dickins return same; 24548f4e2101SHugh Dickins } 24558f4e2101SHugh Dickins 24569de455b2SAtsushi Nemoto static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma) 24576aab341eSLinus Torvalds { 24586aab341eSLinus Torvalds /* 24596aab341eSLinus Torvalds * If the source page was a PFN mapping, we don't have 24606aab341eSLinus Torvalds * a "struct page" for it. We do a best-effort copy by 24616aab341eSLinus Torvalds * just copying from the original user address. If that 24626aab341eSLinus Torvalds * fails, we just zero-fill it. Live with it. 24636aab341eSLinus Torvalds */ 24646aab341eSLinus Torvalds if (unlikely(!src)) { 24659b04c5feSCong Wang void *kaddr = kmap_atomic(dst); 24665d2a2dbbSLinus Torvalds void __user *uaddr = (void __user *)(va & PAGE_MASK); 24675d2a2dbbSLinus Torvalds 24685d2a2dbbSLinus Torvalds /* 24695d2a2dbbSLinus Torvalds * This really shouldn't fail, because the page is there 24705d2a2dbbSLinus Torvalds * in the page tables. But it might just be unreadable, 24715d2a2dbbSLinus Torvalds * in which case we just give up and fill the result with 24725d2a2dbbSLinus Torvalds * zeroes. 24735d2a2dbbSLinus Torvalds */ 24745d2a2dbbSLinus Torvalds if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) 24753ecb01dfSJan Beulich clear_page(kaddr); 24769b04c5feSCong Wang kunmap_atomic(kaddr); 2477c4ec7b0dSDmitriy Monakhov flush_dcache_page(dst); 24780ed361deSNick Piggin } else 24799de455b2SAtsushi Nemoto copy_user_highpage(dst, src, va, vma); 24806aab341eSLinus Torvalds } 24816aab341eSLinus Torvalds 24821da177e4SLinus Torvalds /* 24831da177e4SLinus Torvalds * This routine handles present pages, when users try to write 24841da177e4SLinus Torvalds * to a shared page. It is done by copying the page to a new address 24851da177e4SLinus Torvalds * and decrementing the shared-page counter for the old page. 24861da177e4SLinus Torvalds * 24871da177e4SLinus Torvalds * Note that this routine assumes that the protection checks have been 24881da177e4SLinus Torvalds * done by the caller (the low-level page fault routine in most cases). 24891da177e4SLinus Torvalds * Thus we can safely just mark it writable once we've done any necessary 24901da177e4SLinus Torvalds * COW. 24911da177e4SLinus Torvalds * 24921da177e4SLinus Torvalds * We also mark the page dirty at this point even though the page will 24931da177e4SLinus Torvalds * change only once the write actually happens. This avoids a few races, 24941da177e4SLinus Torvalds * and potentially makes it more efficient. 24951da177e4SLinus Torvalds * 24968f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 24978f4e2101SHugh Dickins * but allow concurrent faults), with pte both mapped and locked. 24988f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 24991da177e4SLinus Torvalds */ 25001da177e4SLinus Torvalds static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma, 250165500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 25028f4e2101SHugh Dickins spinlock_t *ptl, pte_t orig_pte) 2503e6219ec8SNamhyung Kim __releases(ptl) 25041da177e4SLinus Torvalds { 2505e5bbe4dfSHugh Dickins struct page *old_page, *new_page; 25061da177e4SLinus Torvalds pte_t entry; 2507b009c024SMichel Lespinasse int ret = 0; 2508a200ee18SPeter Zijlstra int page_mkwrite = 0; 2509d08b3851SPeter Zijlstra struct page *dirty_page = NULL; 25101da177e4SLinus Torvalds 25116aab341eSLinus Torvalds old_page = vm_normal_page(vma, address, orig_pte); 2512251b97f5SPeter Zijlstra if (!old_page) { 2513251b97f5SPeter Zijlstra /* 2514251b97f5SPeter Zijlstra * VM_MIXEDMAP !pfn_valid() case 2515251b97f5SPeter Zijlstra * 2516251b97f5SPeter Zijlstra * We should not cow pages in a shared writeable mapping. 2517251b97f5SPeter Zijlstra * Just mark the pages writable as we can't do any dirty 2518251b97f5SPeter Zijlstra * accounting on raw pfn maps. 2519251b97f5SPeter Zijlstra */ 2520251b97f5SPeter Zijlstra if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 2521251b97f5SPeter Zijlstra (VM_WRITE|VM_SHARED)) 2522251b97f5SPeter Zijlstra goto reuse; 2523920fc356SHugh Dickins goto gotten; 2524251b97f5SPeter Zijlstra } 25251da177e4SLinus Torvalds 2526d08b3851SPeter Zijlstra /* 2527ee6a6457SPeter Zijlstra * Take out anonymous pages first, anonymous shared vmas are 2528ee6a6457SPeter Zijlstra * not dirty accountable. 2529d08b3851SPeter Zijlstra */ 25309a840895SHugh Dickins if (PageAnon(old_page) && !PageKsm(old_page)) { 2531ab967d86SHugh Dickins if (!trylock_page(old_page)) { 2532ab967d86SHugh Dickins page_cache_get(old_page); 2533ab967d86SHugh Dickins pte_unmap_unlock(page_table, ptl); 2534ab967d86SHugh Dickins lock_page(old_page); 2535ab967d86SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, 2536ab967d86SHugh Dickins &ptl); 2537ab967d86SHugh Dickins if (!pte_same(*page_table, orig_pte)) { 2538ab967d86SHugh Dickins unlock_page(old_page); 2539ab967d86SHugh Dickins goto unlock; 2540ab967d86SHugh Dickins } 2541ab967d86SHugh Dickins page_cache_release(old_page); 2542ab967d86SHugh Dickins } 2543b009c024SMichel Lespinasse if (reuse_swap_page(old_page)) { 2544c44b6743SRik van Riel /* 2545c44b6743SRik van Riel * The page is all ours. Move it to our anon_vma so 2546c44b6743SRik van Riel * the rmap code will not search our parent or siblings. 2547c44b6743SRik van Riel * Protected against the rmap code by the page lock. 2548c44b6743SRik van Riel */ 2549c44b6743SRik van Riel page_move_anon_rmap(old_page, vma, address); 2550ee6a6457SPeter Zijlstra unlock_page(old_page); 2551b009c024SMichel Lespinasse goto reuse; 2552b009c024SMichel Lespinasse } 2553b009c024SMichel Lespinasse unlock_page(old_page); 2554ee6a6457SPeter Zijlstra } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 2555d08b3851SPeter Zijlstra (VM_WRITE|VM_SHARED))) { 2556ee6a6457SPeter Zijlstra /* 2557ee6a6457SPeter Zijlstra * Only catch write-faults on shared writable pages, 2558ee6a6457SPeter Zijlstra * read-only shared pages can get COWed by 2559ee6a6457SPeter Zijlstra * get_user_pages(.write=1, .force=1). 2560ee6a6457SPeter Zijlstra */ 25619637a5efSDavid Howells if (vma->vm_ops && vma->vm_ops->page_mkwrite) { 2562c2ec175cSNick Piggin struct vm_fault vmf; 2563c2ec175cSNick Piggin int tmp; 2564c2ec175cSNick Piggin 2565c2ec175cSNick Piggin vmf.virtual_address = (void __user *)(address & 2566c2ec175cSNick Piggin PAGE_MASK); 2567c2ec175cSNick Piggin vmf.pgoff = old_page->index; 2568c2ec175cSNick Piggin vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE; 2569c2ec175cSNick Piggin vmf.page = old_page; 2570c2ec175cSNick Piggin 25719637a5efSDavid Howells /* 25729637a5efSDavid Howells * Notify the address space that the page is about to 25739637a5efSDavid Howells * become writable so that it can prohibit this or wait 25749637a5efSDavid Howells * for the page to get into an appropriate state. 25759637a5efSDavid Howells * 25769637a5efSDavid Howells * We do this without the lock held, so that it can 25779637a5efSDavid Howells * sleep if it needs to. 25789637a5efSDavid Howells */ 25799637a5efSDavid Howells page_cache_get(old_page); 25809637a5efSDavid Howells pte_unmap_unlock(page_table, ptl); 25819637a5efSDavid Howells 2582c2ec175cSNick Piggin tmp = vma->vm_ops->page_mkwrite(vma, &vmf); 2583c2ec175cSNick Piggin if (unlikely(tmp & 2584c2ec175cSNick Piggin (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) { 2585c2ec175cSNick Piggin ret = tmp; 25869637a5efSDavid Howells goto unwritable_page; 2587c2ec175cSNick Piggin } 2588b827e496SNick Piggin if (unlikely(!(tmp & VM_FAULT_LOCKED))) { 2589b827e496SNick Piggin lock_page(old_page); 2590b827e496SNick Piggin if (!old_page->mapping) { 2591b827e496SNick Piggin ret = 0; /* retry the fault */ 2592b827e496SNick Piggin unlock_page(old_page); 2593b827e496SNick Piggin goto unwritable_page; 2594b827e496SNick Piggin } 2595b827e496SNick Piggin } else 2596b827e496SNick Piggin VM_BUG_ON(!PageLocked(old_page)); 25979637a5efSDavid Howells 25989637a5efSDavid Howells /* 25999637a5efSDavid Howells * Since we dropped the lock we need to revalidate 26009637a5efSDavid Howells * the PTE as someone else may have changed it. If 26019637a5efSDavid Howells * they did, we just return, as we can count on the 26029637a5efSDavid Howells * MMU to tell us if they didn't also make it writable. 26039637a5efSDavid Howells */ 26049637a5efSDavid Howells page_table = pte_offset_map_lock(mm, pmd, address, 26059637a5efSDavid Howells &ptl); 2606b827e496SNick Piggin if (!pte_same(*page_table, orig_pte)) { 2607b827e496SNick Piggin unlock_page(old_page); 26089637a5efSDavid Howells goto unlock; 2609b827e496SNick Piggin } 2610a200ee18SPeter Zijlstra 2611a200ee18SPeter Zijlstra page_mkwrite = 1; 26129637a5efSDavid Howells } 2613d08b3851SPeter Zijlstra dirty_page = old_page; 2614d08b3851SPeter Zijlstra get_page(dirty_page); 26159637a5efSDavid Howells 2616251b97f5SPeter Zijlstra reuse: 2617eca35133SBen Collins flush_cache_page(vma, address, pte_pfn(orig_pte)); 261865500d23SHugh Dickins entry = pte_mkyoung(orig_pte); 261965500d23SHugh Dickins entry = maybe_mkwrite(pte_mkdirty(entry), vma); 2620954ffcb3SKAMEZAWA Hiroyuki if (ptep_set_access_flags(vma, address, page_table, entry,1)) 26214b3073e1SRussell King update_mmu_cache(vma, address, page_table); 262272ddc8f7SMichel Lespinasse pte_unmap_unlock(page_table, ptl); 262365500d23SHugh Dickins ret |= VM_FAULT_WRITE; 262472ddc8f7SMichel Lespinasse 262572ddc8f7SMichel Lespinasse if (!dirty_page) 262672ddc8f7SMichel Lespinasse return ret; 262772ddc8f7SMichel Lespinasse 262872ddc8f7SMichel Lespinasse /* 262972ddc8f7SMichel Lespinasse * Yes, Virginia, this is actually required to prevent a race 263072ddc8f7SMichel Lespinasse * with clear_page_dirty_for_io() from clearing the page dirty 263172ddc8f7SMichel Lespinasse * bit after it clear all dirty ptes, but before a racing 263272ddc8f7SMichel Lespinasse * do_wp_page installs a dirty pte. 263372ddc8f7SMichel Lespinasse * 2634a335b2e1SRyota Ozaki * __do_fault is protected similarly. 263572ddc8f7SMichel Lespinasse */ 263672ddc8f7SMichel Lespinasse if (!page_mkwrite) { 263772ddc8f7SMichel Lespinasse wait_on_page_locked(dirty_page); 263872ddc8f7SMichel Lespinasse set_page_dirty_balance(dirty_page, page_mkwrite); 263972ddc8f7SMichel Lespinasse } 264072ddc8f7SMichel Lespinasse put_page(dirty_page); 264172ddc8f7SMichel Lespinasse if (page_mkwrite) { 264272ddc8f7SMichel Lespinasse struct address_space *mapping = dirty_page->mapping; 264372ddc8f7SMichel Lespinasse 264472ddc8f7SMichel Lespinasse set_page_dirty(dirty_page); 264572ddc8f7SMichel Lespinasse unlock_page(dirty_page); 264672ddc8f7SMichel Lespinasse page_cache_release(dirty_page); 264772ddc8f7SMichel Lespinasse if (mapping) { 264872ddc8f7SMichel Lespinasse /* 264972ddc8f7SMichel Lespinasse * Some device drivers do not set page.mapping 265072ddc8f7SMichel Lespinasse * but still dirty their pages 265172ddc8f7SMichel Lespinasse */ 265272ddc8f7SMichel Lespinasse balance_dirty_pages_ratelimited(mapping); 265372ddc8f7SMichel Lespinasse } 265472ddc8f7SMichel Lespinasse } 265572ddc8f7SMichel Lespinasse 265672ddc8f7SMichel Lespinasse /* file_update_time outside page_lock */ 265772ddc8f7SMichel Lespinasse if (vma->vm_file) 265872ddc8f7SMichel Lespinasse file_update_time(vma->vm_file); 265972ddc8f7SMichel Lespinasse 266072ddc8f7SMichel Lespinasse return ret; 26611da177e4SLinus Torvalds } 26621da177e4SLinus Torvalds 26631da177e4SLinus Torvalds /* 26641da177e4SLinus Torvalds * Ok, we need to copy. Oh, well.. 26651da177e4SLinus Torvalds */ 26661da177e4SLinus Torvalds page_cache_get(old_page); 2667920fc356SHugh Dickins gotten: 26688f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 26691da177e4SLinus Torvalds 26701da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 267165500d23SHugh Dickins goto oom; 2672a13ea5b7SHugh Dickins 267362eede62SHugh Dickins if (is_zero_pfn(pte_pfn(orig_pte))) { 2674a13ea5b7SHugh Dickins new_page = alloc_zeroed_user_highpage_movable(vma, address); 2675a13ea5b7SHugh Dickins if (!new_page) 2676a13ea5b7SHugh Dickins goto oom; 2677a13ea5b7SHugh Dickins } else { 2678769848c0SMel Gorman new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address); 26791da177e4SLinus Torvalds if (!new_page) 268065500d23SHugh Dickins goto oom; 2681a13ea5b7SHugh Dickins cow_user_page(new_page, old_page, address, vma); 2682a13ea5b7SHugh Dickins } 2683a13ea5b7SHugh Dickins __SetPageUptodate(new_page); 2684a13ea5b7SHugh Dickins 26852c26fdd7SKAMEZAWA Hiroyuki if (mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL)) 26868a9f3ccdSBalbir Singh goto oom_free_new; 26878a9f3ccdSBalbir Singh 26881da177e4SLinus Torvalds /* 26891da177e4SLinus Torvalds * Re-check the pte - we dropped the lock 26901da177e4SLinus Torvalds */ 26918f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 269265500d23SHugh Dickins if (likely(pte_same(*page_table, orig_pte))) { 2693920fc356SHugh Dickins if (old_page) { 26944294621fSHugh Dickins if (!PageAnon(old_page)) { 269534e55232SKAMEZAWA Hiroyuki dec_mm_counter_fast(mm, MM_FILEPAGES); 269634e55232SKAMEZAWA Hiroyuki inc_mm_counter_fast(mm, MM_ANONPAGES); 26974294621fSHugh Dickins } 2698920fc356SHugh Dickins } else 269934e55232SKAMEZAWA Hiroyuki inc_mm_counter_fast(mm, MM_ANONPAGES); 2700eca35133SBen Collins flush_cache_page(vma, address, pte_pfn(orig_pte)); 270165500d23SHugh Dickins entry = mk_pte(new_page, vma->vm_page_prot); 270265500d23SHugh Dickins entry = maybe_mkwrite(pte_mkdirty(entry), vma); 27034ce072f1SSiddha, Suresh B /* 27044ce072f1SSiddha, Suresh B * Clear the pte entry and flush it first, before updating the 27054ce072f1SSiddha, Suresh B * pte with the new entry. This will avoid a race condition 27064ce072f1SSiddha, Suresh B * seen in the presence of one thread doing SMC and another 27074ce072f1SSiddha, Suresh B * thread doing COW. 27084ce072f1SSiddha, Suresh B */ 2709828502d3SIzik Eidus ptep_clear_flush(vma, address, page_table); 27109617d95eSNick Piggin page_add_new_anon_rmap(new_page, vma, address); 2711828502d3SIzik Eidus /* 2712828502d3SIzik Eidus * We call the notify macro here because, when using secondary 2713828502d3SIzik Eidus * mmu page tables (such as kvm shadow page tables), we want the 2714828502d3SIzik Eidus * new page to be mapped directly into the secondary page table. 2715828502d3SIzik Eidus */ 2716828502d3SIzik Eidus set_pte_at_notify(mm, address, page_table, entry); 27174b3073e1SRussell King update_mmu_cache(vma, address, page_table); 2718945754a1SNick Piggin if (old_page) { 2719945754a1SNick Piggin /* 2720945754a1SNick Piggin * Only after switching the pte to the new page may 2721945754a1SNick Piggin * we remove the mapcount here. Otherwise another 2722945754a1SNick Piggin * process may come and find the rmap count decremented 2723945754a1SNick Piggin * before the pte is switched to the new page, and 2724945754a1SNick Piggin * "reuse" the old page writing into it while our pte 2725945754a1SNick Piggin * here still points into it and can be read by other 2726945754a1SNick Piggin * threads. 2727945754a1SNick Piggin * 2728945754a1SNick Piggin * The critical issue is to order this 2729945754a1SNick Piggin * page_remove_rmap with the ptp_clear_flush above. 2730945754a1SNick Piggin * Those stores are ordered by (if nothing else,) 2731945754a1SNick Piggin * the barrier present in the atomic_add_negative 2732945754a1SNick Piggin * in page_remove_rmap. 2733945754a1SNick Piggin * 2734945754a1SNick Piggin * Then the TLB flush in ptep_clear_flush ensures that 2735945754a1SNick Piggin * no process can access the old page before the 2736945754a1SNick Piggin * decremented mapcount is visible. And the old page 2737945754a1SNick Piggin * cannot be reused until after the decremented 2738945754a1SNick Piggin * mapcount is visible. So transitively, TLBs to 2739945754a1SNick Piggin * old page will be flushed before it can be reused. 2740945754a1SNick Piggin */ 2741edc315fdSHugh Dickins page_remove_rmap(old_page); 2742945754a1SNick Piggin } 2743945754a1SNick Piggin 27441da177e4SLinus Torvalds /* Free the old page.. */ 27451da177e4SLinus Torvalds new_page = old_page; 2746f33ea7f4SNick Piggin ret |= VM_FAULT_WRITE; 27478a9f3ccdSBalbir Singh } else 27488a9f3ccdSBalbir Singh mem_cgroup_uncharge_page(new_page); 27498a9f3ccdSBalbir Singh 2750920fc356SHugh Dickins if (new_page) 27511da177e4SLinus Torvalds page_cache_release(new_page); 275265500d23SHugh Dickins unlock: 27538f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 2754e15f8c01SMichel Lespinasse if (old_page) { 2755e15f8c01SMichel Lespinasse /* 2756e15f8c01SMichel Lespinasse * Don't let another task, with possibly unlocked vma, 2757e15f8c01SMichel Lespinasse * keep the mlocked page. 2758e15f8c01SMichel Lespinasse */ 2759e15f8c01SMichel Lespinasse if ((ret & VM_FAULT_WRITE) && (vma->vm_flags & VM_LOCKED)) { 2760e15f8c01SMichel Lespinasse lock_page(old_page); /* LRU manipulation */ 2761e15f8c01SMichel Lespinasse munlock_vma_page(old_page); 2762e15f8c01SMichel Lespinasse unlock_page(old_page); 2763e15f8c01SMichel Lespinasse } 2764e15f8c01SMichel Lespinasse page_cache_release(old_page); 2765e15f8c01SMichel Lespinasse } 2766f33ea7f4SNick Piggin return ret; 27678a9f3ccdSBalbir Singh oom_free_new: 27686dbf6d3bSHugh Dickins page_cache_release(new_page); 276965500d23SHugh Dickins oom: 2770b827e496SNick Piggin if (old_page) { 2771b827e496SNick Piggin if (page_mkwrite) { 2772b827e496SNick Piggin unlock_page(old_page); 27731da177e4SLinus Torvalds page_cache_release(old_page); 2774b827e496SNick Piggin } 2775b827e496SNick Piggin page_cache_release(old_page); 2776b827e496SNick Piggin } 27771da177e4SLinus Torvalds return VM_FAULT_OOM; 27789637a5efSDavid Howells 27799637a5efSDavid Howells unwritable_page: 27809637a5efSDavid Howells page_cache_release(old_page); 2781c2ec175cSNick Piggin return ret; 27821da177e4SLinus Torvalds } 27831da177e4SLinus Torvalds 278497a89413SPeter Zijlstra static void unmap_mapping_range_vma(struct vm_area_struct *vma, 27851da177e4SLinus Torvalds unsigned long start_addr, unsigned long end_addr, 27861da177e4SLinus Torvalds struct zap_details *details) 27871da177e4SLinus Torvalds { 2788f5cc4eefSAl Viro zap_page_range_single(vma, start_addr, end_addr - start_addr, details); 27891da177e4SLinus Torvalds } 27901da177e4SLinus Torvalds 27911da177e4SLinus Torvalds static inline void unmap_mapping_range_tree(struct prio_tree_root *root, 27921da177e4SLinus Torvalds struct zap_details *details) 27931da177e4SLinus Torvalds { 27941da177e4SLinus Torvalds struct vm_area_struct *vma; 27951da177e4SLinus Torvalds struct prio_tree_iter iter; 27961da177e4SLinus Torvalds pgoff_t vba, vea, zba, zea; 27971da177e4SLinus Torvalds 27981da177e4SLinus Torvalds vma_prio_tree_foreach(vma, &iter, root, 27991da177e4SLinus Torvalds details->first_index, details->last_index) { 28001da177e4SLinus Torvalds 28011da177e4SLinus Torvalds vba = vma->vm_pgoff; 28021da177e4SLinus Torvalds vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1; 28031da177e4SLinus Torvalds /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */ 28041da177e4SLinus Torvalds zba = details->first_index; 28051da177e4SLinus Torvalds if (zba < vba) 28061da177e4SLinus Torvalds zba = vba; 28071da177e4SLinus Torvalds zea = details->last_index; 28081da177e4SLinus Torvalds if (zea > vea) 28091da177e4SLinus Torvalds zea = vea; 28101da177e4SLinus Torvalds 281197a89413SPeter Zijlstra unmap_mapping_range_vma(vma, 28121da177e4SLinus Torvalds ((zba - vba) << PAGE_SHIFT) + vma->vm_start, 28131da177e4SLinus Torvalds ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start, 281497a89413SPeter Zijlstra details); 28151da177e4SLinus Torvalds } 28161da177e4SLinus Torvalds } 28171da177e4SLinus Torvalds 28181da177e4SLinus Torvalds static inline void unmap_mapping_range_list(struct list_head *head, 28191da177e4SLinus Torvalds struct zap_details *details) 28201da177e4SLinus Torvalds { 28211da177e4SLinus Torvalds struct vm_area_struct *vma; 28221da177e4SLinus Torvalds 28231da177e4SLinus Torvalds /* 28241da177e4SLinus Torvalds * In nonlinear VMAs there is no correspondence between virtual address 28251da177e4SLinus Torvalds * offset and file offset. So we must perform an exhaustive search 28261da177e4SLinus Torvalds * across *all* the pages in each nonlinear VMA, not just the pages 28271da177e4SLinus Torvalds * whose virtual address lies outside the file truncation point. 28281da177e4SLinus Torvalds */ 28291da177e4SLinus Torvalds list_for_each_entry(vma, head, shared.vm_set.list) { 28301da177e4SLinus Torvalds details->nonlinear_vma = vma; 283197a89413SPeter Zijlstra unmap_mapping_range_vma(vma, vma->vm_start, vma->vm_end, details); 28321da177e4SLinus Torvalds } 28331da177e4SLinus Torvalds } 28341da177e4SLinus Torvalds 28351da177e4SLinus Torvalds /** 283672fd4a35SRobert 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. 28373d41088fSMartin Waitz * @mapping: the address space containing mmaps to be unmapped. 28381da177e4SLinus Torvalds * @holebegin: byte in first page to unmap, relative to the start of 28391da177e4SLinus Torvalds * the underlying file. This will be rounded down to a PAGE_SIZE 284025d9e2d1Snpiggin@suse.de * boundary. Note that this is different from truncate_pagecache(), which 28411da177e4SLinus Torvalds * must keep the partial page. In contrast, we must get rid of 28421da177e4SLinus Torvalds * partial pages. 28431da177e4SLinus Torvalds * @holelen: size of prospective hole in bytes. This will be rounded 28441da177e4SLinus Torvalds * up to a PAGE_SIZE boundary. A holelen of zero truncates to the 28451da177e4SLinus Torvalds * end of the file. 28461da177e4SLinus Torvalds * @even_cows: 1 when truncating a file, unmap even private COWed pages; 28471da177e4SLinus Torvalds * but 0 when invalidating pagecache, don't throw away private data. 28481da177e4SLinus Torvalds */ 28491da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping, 28501da177e4SLinus Torvalds loff_t const holebegin, loff_t const holelen, int even_cows) 28511da177e4SLinus Torvalds { 28521da177e4SLinus Torvalds struct zap_details details; 28531da177e4SLinus Torvalds pgoff_t hba = holebegin >> PAGE_SHIFT; 28541da177e4SLinus Torvalds pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 28551da177e4SLinus Torvalds 28561da177e4SLinus Torvalds /* Check for overflow. */ 28571da177e4SLinus Torvalds if (sizeof(holelen) > sizeof(hlen)) { 28581da177e4SLinus Torvalds long long holeend = 28591da177e4SLinus Torvalds (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 28601da177e4SLinus Torvalds if (holeend & ~(long long)ULONG_MAX) 28611da177e4SLinus Torvalds hlen = ULONG_MAX - hba + 1; 28621da177e4SLinus Torvalds } 28631da177e4SLinus Torvalds 28641da177e4SLinus Torvalds details.check_mapping = even_cows? NULL: mapping; 28651da177e4SLinus Torvalds details.nonlinear_vma = NULL; 28661da177e4SLinus Torvalds details.first_index = hba; 28671da177e4SLinus Torvalds details.last_index = hba + hlen - 1; 28681da177e4SLinus Torvalds if (details.last_index < details.first_index) 28691da177e4SLinus Torvalds details.last_index = ULONG_MAX; 28701da177e4SLinus Torvalds 287197a89413SPeter Zijlstra 28723d48ae45SPeter Zijlstra mutex_lock(&mapping->i_mmap_mutex); 28731da177e4SLinus Torvalds if (unlikely(!prio_tree_empty(&mapping->i_mmap))) 28741da177e4SLinus Torvalds unmap_mapping_range_tree(&mapping->i_mmap, &details); 28751da177e4SLinus Torvalds if (unlikely(!list_empty(&mapping->i_mmap_nonlinear))) 28761da177e4SLinus Torvalds unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details); 28773d48ae45SPeter Zijlstra mutex_unlock(&mapping->i_mmap_mutex); 28781da177e4SLinus Torvalds } 28791da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range); 28801da177e4SLinus Torvalds 28811da177e4SLinus Torvalds /* 28828f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 28838f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 28848f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 28851da177e4SLinus Torvalds */ 288665500d23SHugh Dickins static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma, 288765500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 288830c9f3a9SLinus Torvalds unsigned int flags, pte_t orig_pte) 28891da177e4SLinus Torvalds { 28908f4e2101SHugh Dickins spinlock_t *ptl; 28914969c119SAndrea Arcangeli struct page *page, *swapcache = NULL; 289265500d23SHugh Dickins swp_entry_t entry; 28931da177e4SLinus Torvalds pte_t pte; 2894d065bd81SMichel Lespinasse int locked; 289556039efaSKAMEZAWA Hiroyuki struct mem_cgroup *ptr; 2896ad8c2ee8SRik van Riel int exclusive = 0; 289783c54070SNick Piggin int ret = 0; 28981da177e4SLinus Torvalds 28994c21e2f2SHugh Dickins if (!pte_unmap_same(mm, pmd, page_table, orig_pte)) 29008f4e2101SHugh Dickins goto out; 290165500d23SHugh Dickins 290265500d23SHugh Dickins entry = pte_to_swp_entry(orig_pte); 2903d1737fdbSAndi Kleen if (unlikely(non_swap_entry(entry))) { 29040697212aSChristoph Lameter if (is_migration_entry(entry)) { 29050697212aSChristoph Lameter migration_entry_wait(mm, pmd, address); 2906d1737fdbSAndi Kleen } else if (is_hwpoison_entry(entry)) { 2907d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 2908d1737fdbSAndi Kleen } else { 2909d1737fdbSAndi Kleen print_bad_pte(vma, address, orig_pte, NULL); 2910d99be1a8SHugh Dickins ret = VM_FAULT_SIGBUS; 2911d1737fdbSAndi Kleen } 29120697212aSChristoph Lameter goto out; 29130697212aSChristoph Lameter } 29140ff92245SShailabh Nagar delayacct_set_flag(DELAYACCT_PF_SWAPIN); 29151da177e4SLinus Torvalds page = lookup_swap_cache(entry); 29161da177e4SLinus Torvalds if (!page) { 2917a5c9b696SHugh Dickins grab_swap_token(mm); /* Contend for token _before_ read-in */ 291802098feaSHugh Dickins page = swapin_readahead(entry, 291902098feaSHugh Dickins GFP_HIGHUSER_MOVABLE, vma, address); 29201da177e4SLinus Torvalds if (!page) { 29211da177e4SLinus Torvalds /* 29228f4e2101SHugh Dickins * Back out if somebody else faulted in this pte 29238f4e2101SHugh Dickins * while we released the pte lock. 29241da177e4SLinus Torvalds */ 29258f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 29261da177e4SLinus Torvalds if (likely(pte_same(*page_table, orig_pte))) 29271da177e4SLinus Torvalds ret = VM_FAULT_OOM; 29280ff92245SShailabh Nagar delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 292965500d23SHugh Dickins goto unlock; 29301da177e4SLinus Torvalds } 29311da177e4SLinus Torvalds 29321da177e4SLinus Torvalds /* Had to read the page from swap area: Major fault */ 29331da177e4SLinus Torvalds ret = VM_FAULT_MAJOR; 2934f8891e5eSChristoph Lameter count_vm_event(PGMAJFAULT); 2935456f998eSYing Han mem_cgroup_count_vm_event(mm, PGMAJFAULT); 2936d1737fdbSAndi Kleen } else if (PageHWPoison(page)) { 293771f72525SWu Fengguang /* 293871f72525SWu Fengguang * hwpoisoned dirty swapcache pages are kept for killing 293971f72525SWu Fengguang * owner processes (which may be unknown at hwpoison time) 294071f72525SWu Fengguang */ 2941d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 2942d1737fdbSAndi Kleen delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 29434779cb31SAndi Kleen goto out_release; 29441da177e4SLinus Torvalds } 29451da177e4SLinus Torvalds 2946d065bd81SMichel Lespinasse locked = lock_page_or_retry(page, mm, flags); 294720a1022dSBalbir Singh delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 2948d065bd81SMichel Lespinasse if (!locked) { 2949d065bd81SMichel Lespinasse ret |= VM_FAULT_RETRY; 2950d065bd81SMichel Lespinasse goto out_release; 2951d065bd81SMichel Lespinasse } 29521da177e4SLinus Torvalds 29534969c119SAndrea Arcangeli /* 295431c4a3d3SHugh Dickins * Make sure try_to_free_swap or reuse_swap_page or swapoff did not 295531c4a3d3SHugh Dickins * release the swapcache from under us. The page pin, and pte_same 295631c4a3d3SHugh Dickins * test below, are not enough to exclude that. Even if it is still 295731c4a3d3SHugh Dickins * swapcache, we need to check that the page's swap has not changed. 29584969c119SAndrea Arcangeli */ 295931c4a3d3SHugh Dickins if (unlikely(!PageSwapCache(page) || page_private(page) != entry.val)) 29604969c119SAndrea Arcangeli goto out_page; 29614969c119SAndrea Arcangeli 29624969c119SAndrea Arcangeli if (ksm_might_need_to_copy(page, vma, address)) { 29634969c119SAndrea Arcangeli swapcache = page; 29644969c119SAndrea Arcangeli page = ksm_does_need_to_copy(page, vma, address); 29654969c119SAndrea Arcangeli 29664969c119SAndrea Arcangeli if (unlikely(!page)) { 29675ad64688SHugh Dickins ret = VM_FAULT_OOM; 29684969c119SAndrea Arcangeli page = swapcache; 29694969c119SAndrea Arcangeli swapcache = NULL; 29704969c119SAndrea Arcangeli goto out_page; 29714969c119SAndrea Arcangeli } 29725ad64688SHugh Dickins } 29735ad64688SHugh Dickins 29742c26fdd7SKAMEZAWA Hiroyuki if (mem_cgroup_try_charge_swapin(mm, page, GFP_KERNEL, &ptr)) { 2975073e587eSKAMEZAWA Hiroyuki ret = VM_FAULT_OOM; 2976bc43f75cSJohannes Weiner goto out_page; 2977073e587eSKAMEZAWA Hiroyuki } 2978073e587eSKAMEZAWA Hiroyuki 29791da177e4SLinus Torvalds /* 29808f4e2101SHugh Dickins * Back out if somebody else already faulted in this pte. 29811da177e4SLinus Torvalds */ 29828f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 29839e9bef07SHugh Dickins if (unlikely(!pte_same(*page_table, orig_pte))) 2984b8107480SKirill Korotaev goto out_nomap; 2985b8107480SKirill Korotaev 2986b8107480SKirill Korotaev if (unlikely(!PageUptodate(page))) { 2987b8107480SKirill Korotaev ret = VM_FAULT_SIGBUS; 2988b8107480SKirill Korotaev goto out_nomap; 29891da177e4SLinus Torvalds } 29901da177e4SLinus Torvalds 29918c7c6e34SKAMEZAWA Hiroyuki /* 29928c7c6e34SKAMEZAWA Hiroyuki * The page isn't present yet, go ahead with the fault. 29938c7c6e34SKAMEZAWA Hiroyuki * 29948c7c6e34SKAMEZAWA Hiroyuki * Be careful about the sequence of operations here. 29958c7c6e34SKAMEZAWA Hiroyuki * To get its accounting right, reuse_swap_page() must be called 29968c7c6e34SKAMEZAWA Hiroyuki * while the page is counted on swap but not yet in mapcount i.e. 29978c7c6e34SKAMEZAWA Hiroyuki * before page_add_anon_rmap() and swap_free(); try_to_free_swap() 29988c7c6e34SKAMEZAWA Hiroyuki * must be called after the swap_free(), or it will never succeed. 299903f3c433SKAMEZAWA Hiroyuki * Because delete_from_swap_page() may be called by reuse_swap_page(), 300003f3c433SKAMEZAWA Hiroyuki * mem_cgroup_commit_charge_swapin() may not be able to find swp_entry 300103f3c433SKAMEZAWA Hiroyuki * in page->private. In this case, a record in swap_cgroup is silently 300203f3c433SKAMEZAWA Hiroyuki * discarded at swap_free(). 30038c7c6e34SKAMEZAWA Hiroyuki */ 30041da177e4SLinus Torvalds 300534e55232SKAMEZAWA Hiroyuki inc_mm_counter_fast(mm, MM_ANONPAGES); 3006b084d435SKAMEZAWA Hiroyuki dec_mm_counter_fast(mm, MM_SWAPENTS); 30071da177e4SLinus Torvalds pte = mk_pte(page, vma->vm_page_prot); 300830c9f3a9SLinus Torvalds if ((flags & FAULT_FLAG_WRITE) && reuse_swap_page(page)) { 30091da177e4SLinus Torvalds pte = maybe_mkwrite(pte_mkdirty(pte), vma); 301030c9f3a9SLinus Torvalds flags &= ~FAULT_FLAG_WRITE; 30119a5b489bSAndrea Arcangeli ret |= VM_FAULT_WRITE; 3012ad8c2ee8SRik van Riel exclusive = 1; 30131da177e4SLinus Torvalds } 30141da177e4SLinus Torvalds flush_icache_page(vma, page); 30151da177e4SLinus Torvalds set_pte_at(mm, address, page_table, pte); 3016ad8c2ee8SRik van Riel do_page_add_anon_rmap(page, vma, address, exclusive); 301703f3c433SKAMEZAWA Hiroyuki /* It's better to call commit-charge after rmap is established */ 301803f3c433SKAMEZAWA Hiroyuki mem_cgroup_commit_charge_swapin(page, ptr); 30191da177e4SLinus Torvalds 3020c475a8abSHugh Dickins swap_free(entry); 3021b291f000SNick Piggin if (vm_swap_full() || (vma->vm_flags & VM_LOCKED) || PageMlocked(page)) 3022a2c43eedSHugh Dickins try_to_free_swap(page); 3023c475a8abSHugh Dickins unlock_page(page); 30244969c119SAndrea Arcangeli if (swapcache) { 30254969c119SAndrea Arcangeli /* 30264969c119SAndrea Arcangeli * Hold the lock to avoid the swap entry to be reused 30274969c119SAndrea Arcangeli * until we take the PT lock for the pte_same() check 30284969c119SAndrea Arcangeli * (to avoid false positives from pte_same). For 30294969c119SAndrea Arcangeli * further safety release the lock after the swap_free 30304969c119SAndrea Arcangeli * so that the swap count won't change under a 30314969c119SAndrea Arcangeli * parallel locked swapcache. 30324969c119SAndrea Arcangeli */ 30334969c119SAndrea Arcangeli unlock_page(swapcache); 30344969c119SAndrea Arcangeli page_cache_release(swapcache); 30354969c119SAndrea Arcangeli } 3036c475a8abSHugh Dickins 303730c9f3a9SLinus Torvalds if (flags & FAULT_FLAG_WRITE) { 303861469f1dSHugh Dickins ret |= do_wp_page(mm, vma, address, page_table, pmd, ptl, pte); 303961469f1dSHugh Dickins if (ret & VM_FAULT_ERROR) 304061469f1dSHugh Dickins ret &= VM_FAULT_ERROR; 30411da177e4SLinus Torvalds goto out; 30421da177e4SLinus Torvalds } 30431da177e4SLinus Torvalds 30441da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 30454b3073e1SRussell King update_mmu_cache(vma, address, page_table); 304665500d23SHugh Dickins unlock: 30478f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 30481da177e4SLinus Torvalds out: 30491da177e4SLinus Torvalds return ret; 3050b8107480SKirill Korotaev out_nomap: 30517a81b88cSKAMEZAWA Hiroyuki mem_cgroup_cancel_charge_swapin(ptr); 30528f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 3053bc43f75cSJohannes Weiner out_page: 3054b8107480SKirill Korotaev unlock_page(page); 30554779cb31SAndi Kleen out_release: 3056b8107480SKirill Korotaev page_cache_release(page); 30574969c119SAndrea Arcangeli if (swapcache) { 30584969c119SAndrea Arcangeli unlock_page(swapcache); 30594969c119SAndrea Arcangeli page_cache_release(swapcache); 30604969c119SAndrea Arcangeli } 306165500d23SHugh Dickins return ret; 30621da177e4SLinus Torvalds } 30631da177e4SLinus Torvalds 30641da177e4SLinus Torvalds /* 30658ca3eb08SLuck, Tony * This is like a special single-page "expand_{down|up}wards()", 30668ca3eb08SLuck, Tony * except we must first make sure that 'address{-|+}PAGE_SIZE' 3067320b2b8dSLinus Torvalds * doesn't hit another vma. 3068320b2b8dSLinus Torvalds */ 3069320b2b8dSLinus Torvalds static inline int check_stack_guard_page(struct vm_area_struct *vma, unsigned long address) 3070320b2b8dSLinus Torvalds { 3071320b2b8dSLinus Torvalds address &= PAGE_MASK; 3072320b2b8dSLinus Torvalds if ((vma->vm_flags & VM_GROWSDOWN) && address == vma->vm_start) { 30730e8e50e2SLinus Torvalds struct vm_area_struct *prev = vma->vm_prev; 3074320b2b8dSLinus Torvalds 30750e8e50e2SLinus Torvalds /* 30760e8e50e2SLinus Torvalds * Is there a mapping abutting this one below? 30770e8e50e2SLinus Torvalds * 30780e8e50e2SLinus Torvalds * That's only ok if it's the same stack mapping 30790e8e50e2SLinus Torvalds * that has gotten split.. 30800e8e50e2SLinus Torvalds */ 30810e8e50e2SLinus Torvalds if (prev && prev->vm_end == address) 30820e8e50e2SLinus Torvalds return prev->vm_flags & VM_GROWSDOWN ? 0 : -ENOMEM; 30830e8e50e2SLinus Torvalds 3084d05f3169SMichal Hocko expand_downwards(vma, address - PAGE_SIZE); 3085320b2b8dSLinus Torvalds } 30868ca3eb08SLuck, Tony if ((vma->vm_flags & VM_GROWSUP) && address + PAGE_SIZE == vma->vm_end) { 30878ca3eb08SLuck, Tony struct vm_area_struct *next = vma->vm_next; 30888ca3eb08SLuck, Tony 30898ca3eb08SLuck, Tony /* As VM_GROWSDOWN but s/below/above/ */ 30908ca3eb08SLuck, Tony if (next && next->vm_start == address + PAGE_SIZE) 30918ca3eb08SLuck, Tony return next->vm_flags & VM_GROWSUP ? 0 : -ENOMEM; 30928ca3eb08SLuck, Tony 30938ca3eb08SLuck, Tony expand_upwards(vma, address + PAGE_SIZE); 30948ca3eb08SLuck, Tony } 3095320b2b8dSLinus Torvalds return 0; 3096320b2b8dSLinus Torvalds } 3097320b2b8dSLinus Torvalds 3098320b2b8dSLinus Torvalds /* 30998f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 31008f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 31018f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 31021da177e4SLinus Torvalds */ 310365500d23SHugh Dickins static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma, 310465500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 310530c9f3a9SLinus Torvalds unsigned int flags) 31061da177e4SLinus Torvalds { 31078f4e2101SHugh Dickins struct page *page; 31088f4e2101SHugh Dickins spinlock_t *ptl; 31091da177e4SLinus Torvalds pte_t entry; 31101da177e4SLinus Torvalds 31115528f913SLinus Torvalds pte_unmap(page_table); 3112320b2b8dSLinus Torvalds 311311ac5524SLinus Torvalds /* Check if we need to add a guard page to the stack */ 311411ac5524SLinus Torvalds if (check_stack_guard_page(vma, address) < 0) 311511ac5524SLinus Torvalds return VM_FAULT_SIGBUS; 311611ac5524SLinus Torvalds 311711ac5524SLinus Torvalds /* Use the zero-page for reads */ 311862eede62SHugh Dickins if (!(flags & FAULT_FLAG_WRITE)) { 311962eede62SHugh Dickins entry = pte_mkspecial(pfn_pte(my_zero_pfn(address), 312062eede62SHugh Dickins vma->vm_page_prot)); 312111ac5524SLinus Torvalds page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 3122a13ea5b7SHugh Dickins if (!pte_none(*page_table)) 3123a13ea5b7SHugh Dickins goto unlock; 3124a13ea5b7SHugh Dickins goto setpte; 3125a13ea5b7SHugh Dickins } 3126a13ea5b7SHugh Dickins 31271da177e4SLinus Torvalds /* Allocate our own private page. */ 31281da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 312965500d23SHugh Dickins goto oom; 3130769848c0SMel Gorman page = alloc_zeroed_user_highpage_movable(vma, address); 31311da177e4SLinus Torvalds if (!page) 313265500d23SHugh Dickins goto oom; 31330ed361deSNick Piggin __SetPageUptodate(page); 31341da177e4SLinus Torvalds 31352c26fdd7SKAMEZAWA Hiroyuki if (mem_cgroup_newpage_charge(page, mm, GFP_KERNEL)) 31368a9f3ccdSBalbir Singh goto oom_free_page; 31378a9f3ccdSBalbir Singh 313865500d23SHugh Dickins entry = mk_pte(page, vma->vm_page_prot); 31391ac0cb5dSHugh Dickins if (vma->vm_flags & VM_WRITE) 31401ac0cb5dSHugh Dickins entry = pte_mkwrite(pte_mkdirty(entry)); 31418f4e2101SHugh Dickins 31428f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 31431c2fb7a4SAndrea Arcangeli if (!pte_none(*page_table)) 31448f4e2101SHugh Dickins goto release; 31459ba69294SHugh Dickins 314634e55232SKAMEZAWA Hiroyuki inc_mm_counter_fast(mm, MM_ANONPAGES); 31479617d95eSNick Piggin page_add_new_anon_rmap(page, vma, address); 3148a13ea5b7SHugh Dickins setpte: 314965500d23SHugh Dickins set_pte_at(mm, address, page_table, entry); 31501da177e4SLinus Torvalds 31511da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 31524b3073e1SRussell King update_mmu_cache(vma, address, page_table); 315365500d23SHugh Dickins unlock: 31548f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 315583c54070SNick Piggin return 0; 31568f4e2101SHugh Dickins release: 31578a9f3ccdSBalbir Singh mem_cgroup_uncharge_page(page); 31588f4e2101SHugh Dickins page_cache_release(page); 31598f4e2101SHugh Dickins goto unlock; 31608a9f3ccdSBalbir Singh oom_free_page: 31616dbf6d3bSHugh Dickins page_cache_release(page); 316265500d23SHugh Dickins oom: 31631da177e4SLinus Torvalds return VM_FAULT_OOM; 31641da177e4SLinus Torvalds } 31651da177e4SLinus Torvalds 31661da177e4SLinus Torvalds /* 316754cb8821SNick Piggin * __do_fault() tries to create a new page mapping. It aggressively 31681da177e4SLinus Torvalds * tries to share with existing pages, but makes a separate copy if 316954cb8821SNick Piggin * the FAULT_FLAG_WRITE is set in the flags parameter in order to avoid 317054cb8821SNick Piggin * the next page fault. 31711da177e4SLinus Torvalds * 31721da177e4SLinus Torvalds * As this is called only for pages that do not currently exist, we 31731da177e4SLinus Torvalds * do not need to flush old virtual caches or the TLB. 31741da177e4SLinus Torvalds * 31758f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 317616abfa08SHugh Dickins * but allow concurrent faults), and pte neither mapped nor locked. 31778f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 31781da177e4SLinus Torvalds */ 317954cb8821SNick Piggin static int __do_fault(struct mm_struct *mm, struct vm_area_struct *vma, 318016abfa08SHugh Dickins unsigned long address, pmd_t *pmd, 318154cb8821SNick Piggin pgoff_t pgoff, unsigned int flags, pte_t orig_pte) 31821da177e4SLinus Torvalds { 318316abfa08SHugh Dickins pte_t *page_table; 31848f4e2101SHugh Dickins spinlock_t *ptl; 3185d0217ac0SNick Piggin struct page *page; 31861d65f86dSKAMEZAWA Hiroyuki struct page *cow_page; 31871da177e4SLinus Torvalds pte_t entry; 31881da177e4SLinus Torvalds int anon = 0; 3189d08b3851SPeter Zijlstra struct page *dirty_page = NULL; 3190d0217ac0SNick Piggin struct vm_fault vmf; 3191d0217ac0SNick Piggin int ret; 3192a200ee18SPeter Zijlstra int page_mkwrite = 0; 319354cb8821SNick Piggin 31941d65f86dSKAMEZAWA Hiroyuki /* 31951d65f86dSKAMEZAWA Hiroyuki * If we do COW later, allocate page befor taking lock_page() 31961d65f86dSKAMEZAWA Hiroyuki * on the file cache page. This will reduce lock holding time. 31971d65f86dSKAMEZAWA Hiroyuki */ 31981d65f86dSKAMEZAWA Hiroyuki if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) { 31991d65f86dSKAMEZAWA Hiroyuki 32001d65f86dSKAMEZAWA Hiroyuki if (unlikely(anon_vma_prepare(vma))) 32011d65f86dSKAMEZAWA Hiroyuki return VM_FAULT_OOM; 32021d65f86dSKAMEZAWA Hiroyuki 32031d65f86dSKAMEZAWA Hiroyuki cow_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address); 32041d65f86dSKAMEZAWA Hiroyuki if (!cow_page) 32051d65f86dSKAMEZAWA Hiroyuki return VM_FAULT_OOM; 32061d65f86dSKAMEZAWA Hiroyuki 32071d65f86dSKAMEZAWA Hiroyuki if (mem_cgroup_newpage_charge(cow_page, mm, GFP_KERNEL)) { 32081d65f86dSKAMEZAWA Hiroyuki page_cache_release(cow_page); 32091d65f86dSKAMEZAWA Hiroyuki return VM_FAULT_OOM; 32101d65f86dSKAMEZAWA Hiroyuki } 32111d65f86dSKAMEZAWA Hiroyuki } else 32121d65f86dSKAMEZAWA Hiroyuki cow_page = NULL; 32131d65f86dSKAMEZAWA Hiroyuki 3214d0217ac0SNick Piggin vmf.virtual_address = (void __user *)(address & PAGE_MASK); 3215d0217ac0SNick Piggin vmf.pgoff = pgoff; 3216d0217ac0SNick Piggin vmf.flags = flags; 3217d0217ac0SNick Piggin vmf.page = NULL; 32181da177e4SLinus Torvalds 3219d0217ac0SNick Piggin ret = vma->vm_ops->fault(vma, &vmf); 3220d065bd81SMichel Lespinasse if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | 3221d065bd81SMichel Lespinasse VM_FAULT_RETRY))) 32221d65f86dSKAMEZAWA Hiroyuki goto uncharge_out; 32231da177e4SLinus Torvalds 3224a3b947eaSAndi Kleen if (unlikely(PageHWPoison(vmf.page))) { 3225a3b947eaSAndi Kleen if (ret & VM_FAULT_LOCKED) 3226a3b947eaSAndi Kleen unlock_page(vmf.page); 32271d65f86dSKAMEZAWA Hiroyuki ret = VM_FAULT_HWPOISON; 32281d65f86dSKAMEZAWA Hiroyuki goto uncharge_out; 3229a3b947eaSAndi Kleen } 3230a3b947eaSAndi Kleen 3231d00806b1SNick Piggin /* 3232d0217ac0SNick Piggin * For consistency in subsequent calls, make the faulted page always 3233d00806b1SNick Piggin * locked. 3234d00806b1SNick Piggin */ 323583c54070SNick Piggin if (unlikely(!(ret & VM_FAULT_LOCKED))) 3236d0217ac0SNick Piggin lock_page(vmf.page); 323754cb8821SNick Piggin else 3238d0217ac0SNick Piggin VM_BUG_ON(!PageLocked(vmf.page)); 3239d00806b1SNick Piggin 32401da177e4SLinus Torvalds /* 32411da177e4SLinus Torvalds * Should we do an early C-O-W break? 32421da177e4SLinus Torvalds */ 3243d0217ac0SNick Piggin page = vmf.page; 324454cb8821SNick Piggin if (flags & FAULT_FLAG_WRITE) { 32459637a5efSDavid Howells if (!(vma->vm_flags & VM_SHARED)) { 32461d65f86dSKAMEZAWA Hiroyuki page = cow_page; 324754cb8821SNick Piggin anon = 1; 3248d0217ac0SNick Piggin copy_user_highpage(page, vmf.page, address, vma); 32490ed361deSNick Piggin __SetPageUptodate(page); 32509637a5efSDavid Howells } else { 325154cb8821SNick Piggin /* 325254cb8821SNick Piggin * If the page will be shareable, see if the backing 32539637a5efSDavid Howells * address space wants to know that the page is about 325454cb8821SNick Piggin * to become writable 325554cb8821SNick Piggin */ 325669676147SMark Fasheh if (vma->vm_ops->page_mkwrite) { 3257c2ec175cSNick Piggin int tmp; 3258c2ec175cSNick Piggin 325969676147SMark Fasheh unlock_page(page); 3260b827e496SNick Piggin vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE; 3261c2ec175cSNick Piggin tmp = vma->vm_ops->page_mkwrite(vma, &vmf); 3262c2ec175cSNick Piggin if (unlikely(tmp & 3263c2ec175cSNick Piggin (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) { 3264c2ec175cSNick Piggin ret = tmp; 3265b827e496SNick Piggin goto unwritable_page; 326669676147SMark Fasheh } 3267b827e496SNick Piggin if (unlikely(!(tmp & VM_FAULT_LOCKED))) { 326869676147SMark Fasheh lock_page(page); 3269d0217ac0SNick Piggin if (!page->mapping) { 3270b827e496SNick Piggin ret = 0; /* retry the fault */ 3271b827e496SNick Piggin unlock_page(page); 3272b827e496SNick Piggin goto unwritable_page; 3273d0217ac0SNick Piggin } 3274b827e496SNick Piggin } else 3275b827e496SNick Piggin VM_BUG_ON(!PageLocked(page)); 3276a200ee18SPeter Zijlstra page_mkwrite = 1; 32779637a5efSDavid Howells } 32789637a5efSDavid Howells } 327954cb8821SNick Piggin 32801da177e4SLinus Torvalds } 32811da177e4SLinus Torvalds 32828f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 32831da177e4SLinus Torvalds 32841da177e4SLinus Torvalds /* 32851da177e4SLinus Torvalds * This silly early PAGE_DIRTY setting removes a race 32861da177e4SLinus Torvalds * due to the bad i386 page protection. But it's valid 32871da177e4SLinus Torvalds * for other architectures too. 32881da177e4SLinus Torvalds * 328930c9f3a9SLinus Torvalds * Note that if FAULT_FLAG_WRITE is set, we either now have 32901da177e4SLinus Torvalds * an exclusive copy of the page, or this is a shared mapping, 32911da177e4SLinus Torvalds * so we can make it writable and dirty to avoid having to 32921da177e4SLinus Torvalds * handle that later. 32931da177e4SLinus Torvalds */ 32941da177e4SLinus Torvalds /* Only go through if we didn't race with anybody else... */ 32951c2fb7a4SAndrea Arcangeli if (likely(pte_same(*page_table, orig_pte))) { 3296d00806b1SNick Piggin flush_icache_page(vma, page); 3297d00806b1SNick Piggin entry = mk_pte(page, vma->vm_page_prot); 329854cb8821SNick Piggin if (flags & FAULT_FLAG_WRITE) 32991da177e4SLinus Torvalds entry = maybe_mkwrite(pte_mkdirty(entry), vma); 33001da177e4SLinus Torvalds if (anon) { 330134e55232SKAMEZAWA Hiroyuki inc_mm_counter_fast(mm, MM_ANONPAGES); 3302d00806b1SNick Piggin page_add_new_anon_rmap(page, vma, address); 3303f57e88a8SHugh Dickins } else { 330434e55232SKAMEZAWA Hiroyuki inc_mm_counter_fast(mm, MM_FILEPAGES); 3305d00806b1SNick Piggin page_add_file_rmap(page); 330654cb8821SNick Piggin if (flags & FAULT_FLAG_WRITE) { 3307d00806b1SNick Piggin dirty_page = page; 3308d08b3851SPeter Zijlstra get_page(dirty_page); 3309d08b3851SPeter Zijlstra } 33104294621fSHugh Dickins } 331164d6519dSLee Schermerhorn set_pte_at(mm, address, page_table, entry); 33121da177e4SLinus Torvalds 3313d00806b1SNick Piggin /* no need to invalidate: a not-present page won't be cached */ 33144b3073e1SRussell King update_mmu_cache(vma, address, page_table); 3315d00806b1SNick Piggin } else { 33161d65f86dSKAMEZAWA Hiroyuki if (cow_page) 33171d65f86dSKAMEZAWA Hiroyuki mem_cgroup_uncharge_page(cow_page); 3318d00806b1SNick Piggin if (anon) 3319d00806b1SNick Piggin page_cache_release(page); 3320d00806b1SNick Piggin else 332154cb8821SNick Piggin anon = 1; /* no anon but release faulted_page */ 3322d00806b1SNick Piggin } 3323d00806b1SNick Piggin 33248f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 3325d00806b1SNick Piggin 3326b827e496SNick Piggin if (dirty_page) { 3327b827e496SNick Piggin struct address_space *mapping = page->mapping; 33288f7b3d15SAnton Salikhmetov 3329b827e496SNick Piggin if (set_page_dirty(dirty_page)) 3330b827e496SNick Piggin page_mkwrite = 1; 3331b827e496SNick Piggin unlock_page(dirty_page); 3332d08b3851SPeter Zijlstra put_page(dirty_page); 3333b827e496SNick Piggin if (page_mkwrite && mapping) { 3334b827e496SNick Piggin /* 3335b827e496SNick Piggin * Some device drivers do not set page.mapping but still 3336b827e496SNick Piggin * dirty their pages 3337b827e496SNick Piggin */ 3338b827e496SNick Piggin balance_dirty_pages_ratelimited(mapping); 3339d08b3851SPeter Zijlstra } 3340d00806b1SNick Piggin 3341b827e496SNick Piggin /* file_update_time outside page_lock */ 3342b827e496SNick Piggin if (vma->vm_file) 3343b827e496SNick Piggin file_update_time(vma->vm_file); 3344b827e496SNick Piggin } else { 3345b827e496SNick Piggin unlock_page(vmf.page); 3346b827e496SNick Piggin if (anon) 3347b827e496SNick Piggin page_cache_release(vmf.page); 3348b827e496SNick Piggin } 3349b827e496SNick Piggin 3350b827e496SNick Piggin return ret; 3351b827e496SNick Piggin 3352b827e496SNick Piggin unwritable_page: 3353b827e496SNick Piggin page_cache_release(page); 335483c54070SNick Piggin return ret; 33551d65f86dSKAMEZAWA Hiroyuki uncharge_out: 33561d65f86dSKAMEZAWA Hiroyuki /* fs's fault handler get error */ 33571d65f86dSKAMEZAWA Hiroyuki if (cow_page) { 33581d65f86dSKAMEZAWA Hiroyuki mem_cgroup_uncharge_page(cow_page); 33591d65f86dSKAMEZAWA Hiroyuki page_cache_release(cow_page); 33601d65f86dSKAMEZAWA Hiroyuki } 33611d65f86dSKAMEZAWA Hiroyuki return ret; 336254cb8821SNick Piggin } 3363d00806b1SNick Piggin 336454cb8821SNick Piggin static int do_linear_fault(struct mm_struct *mm, struct vm_area_struct *vma, 336554cb8821SNick Piggin unsigned long address, pte_t *page_table, pmd_t *pmd, 336630c9f3a9SLinus Torvalds unsigned int flags, pte_t orig_pte) 336754cb8821SNick Piggin { 336854cb8821SNick Piggin pgoff_t pgoff = (((address & PAGE_MASK) 33690da7e01fSDean Nelson - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; 337054cb8821SNick Piggin 337116abfa08SHugh Dickins pte_unmap(page_table); 337216abfa08SHugh Dickins return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte); 337354cb8821SNick Piggin } 337454cb8821SNick Piggin 3375f4b81804SJes Sorensen /* 33761da177e4SLinus Torvalds * Fault of a previously existing named mapping. Repopulate the pte 33771da177e4SLinus Torvalds * from the encoded file_pte if possible. This enables swappable 33781da177e4SLinus Torvalds * nonlinear vmas. 33798f4e2101SHugh Dickins * 33808f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 33818f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 33828f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 33831da177e4SLinus Torvalds */ 3384d0217ac0SNick Piggin static int do_nonlinear_fault(struct mm_struct *mm, struct vm_area_struct *vma, 338565500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 338630c9f3a9SLinus Torvalds unsigned int flags, pte_t orig_pte) 33871da177e4SLinus Torvalds { 338865500d23SHugh Dickins pgoff_t pgoff; 33891da177e4SLinus Torvalds 339030c9f3a9SLinus Torvalds flags |= FAULT_FLAG_NONLINEAR; 339130c9f3a9SLinus Torvalds 33924c21e2f2SHugh Dickins if (!pte_unmap_same(mm, pmd, page_table, orig_pte)) 339383c54070SNick Piggin return 0; 33941da177e4SLinus Torvalds 33952509ef26SHugh Dickins if (unlikely(!(vma->vm_flags & VM_NONLINEAR))) { 339665500d23SHugh Dickins /* 339765500d23SHugh Dickins * Page table corrupted: show pte and kill process. 339865500d23SHugh Dickins */ 33993dc14741SHugh Dickins print_bad_pte(vma, address, orig_pte, NULL); 3400d99be1a8SHugh Dickins return VM_FAULT_SIGBUS; 340165500d23SHugh Dickins } 340265500d23SHugh Dickins 340365500d23SHugh Dickins pgoff = pte_to_pgoff(orig_pte); 340416abfa08SHugh Dickins return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte); 34051da177e4SLinus Torvalds } 34061da177e4SLinus Torvalds 34071da177e4SLinus Torvalds /* 34081da177e4SLinus Torvalds * These routines also need to handle stuff like marking pages dirty 34091da177e4SLinus Torvalds * and/or accessed for architectures that don't do it in hardware (most 34101da177e4SLinus Torvalds * RISC architectures). The early dirtying is also good on the i386. 34111da177e4SLinus Torvalds * 34121da177e4SLinus Torvalds * There is also a hook called "update_mmu_cache()" that architectures 34131da177e4SLinus Torvalds * with external mmu caches can use to update those (ie the Sparc or 34141da177e4SLinus Torvalds * PowerPC hashed page tables that act as extended TLBs). 34151da177e4SLinus Torvalds * 3416c74df32cSHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 3417c74df32cSHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 3418c74df32cSHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 34191da177e4SLinus Torvalds */ 342071e3aac0SAndrea Arcangeli int handle_pte_fault(struct mm_struct *mm, 34211da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long address, 342230c9f3a9SLinus Torvalds pte_t *pte, pmd_t *pmd, unsigned int flags) 34231da177e4SLinus Torvalds { 34241da177e4SLinus Torvalds pte_t entry; 34258f4e2101SHugh Dickins spinlock_t *ptl; 34261da177e4SLinus Torvalds 34278dab5241SBenjamin Herrenschmidt entry = *pte; 34281da177e4SLinus Torvalds if (!pte_present(entry)) { 342965500d23SHugh Dickins if (pte_none(entry)) { 3430f4b81804SJes Sorensen if (vma->vm_ops) { 34313c18ddd1SNick Piggin if (likely(vma->vm_ops->fault)) 343254cb8821SNick Piggin return do_linear_fault(mm, vma, address, 343330c9f3a9SLinus Torvalds pte, pmd, flags, entry); 3434f4b81804SJes Sorensen } 3435f4b81804SJes Sorensen return do_anonymous_page(mm, vma, address, 343630c9f3a9SLinus Torvalds pte, pmd, flags); 343765500d23SHugh Dickins } 34381da177e4SLinus Torvalds if (pte_file(entry)) 3439d0217ac0SNick Piggin return do_nonlinear_fault(mm, vma, address, 344030c9f3a9SLinus Torvalds pte, pmd, flags, entry); 344165500d23SHugh Dickins return do_swap_page(mm, vma, address, 344230c9f3a9SLinus Torvalds pte, pmd, flags, entry); 34431da177e4SLinus Torvalds } 34441da177e4SLinus Torvalds 34454c21e2f2SHugh Dickins ptl = pte_lockptr(mm, pmd); 34468f4e2101SHugh Dickins spin_lock(ptl); 34478f4e2101SHugh Dickins if (unlikely(!pte_same(*pte, entry))) 34488f4e2101SHugh Dickins goto unlock; 344930c9f3a9SLinus Torvalds if (flags & FAULT_FLAG_WRITE) { 34501da177e4SLinus Torvalds if (!pte_write(entry)) 34518f4e2101SHugh Dickins return do_wp_page(mm, vma, address, 34528f4e2101SHugh Dickins pte, pmd, ptl, entry); 34531da177e4SLinus Torvalds entry = pte_mkdirty(entry); 34541da177e4SLinus Torvalds } 34551da177e4SLinus Torvalds entry = pte_mkyoung(entry); 345630c9f3a9SLinus Torvalds if (ptep_set_access_flags(vma, address, pte, entry, flags & FAULT_FLAG_WRITE)) { 34574b3073e1SRussell King update_mmu_cache(vma, address, pte); 34581a44e149SAndrea Arcangeli } else { 34591a44e149SAndrea Arcangeli /* 34601a44e149SAndrea Arcangeli * This is needed only for protection faults but the arch code 34611a44e149SAndrea Arcangeli * is not yet telling us if this is a protection fault or not. 34621a44e149SAndrea Arcangeli * This still avoids useless tlb flushes for .text page faults 34631a44e149SAndrea Arcangeli * with threads. 34641a44e149SAndrea Arcangeli */ 346530c9f3a9SLinus Torvalds if (flags & FAULT_FLAG_WRITE) 346661c77326SShaohua Li flush_tlb_fix_spurious_fault(vma, address); 34671a44e149SAndrea Arcangeli } 34688f4e2101SHugh Dickins unlock: 34698f4e2101SHugh Dickins pte_unmap_unlock(pte, ptl); 347083c54070SNick Piggin return 0; 34711da177e4SLinus Torvalds } 34721da177e4SLinus Torvalds 34731da177e4SLinus Torvalds /* 34741da177e4SLinus Torvalds * By the time we get here, we already hold the mm semaphore 34751da177e4SLinus Torvalds */ 347683c54070SNick Piggin int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma, 3477d06063ccSLinus Torvalds unsigned long address, unsigned int flags) 34781da177e4SLinus Torvalds { 34791da177e4SLinus Torvalds pgd_t *pgd; 34801da177e4SLinus Torvalds pud_t *pud; 34811da177e4SLinus Torvalds pmd_t *pmd; 34821da177e4SLinus Torvalds pte_t *pte; 34831da177e4SLinus Torvalds 34841da177e4SLinus Torvalds __set_current_state(TASK_RUNNING); 34851da177e4SLinus Torvalds 3486f8891e5eSChristoph Lameter count_vm_event(PGFAULT); 3487456f998eSYing Han mem_cgroup_count_vm_event(mm, PGFAULT); 34881da177e4SLinus Torvalds 348934e55232SKAMEZAWA Hiroyuki /* do counter updates before entering really critical section. */ 349034e55232SKAMEZAWA Hiroyuki check_sync_rss_stat(current); 349134e55232SKAMEZAWA Hiroyuki 3492ac9b9c66SHugh Dickins if (unlikely(is_vm_hugetlb_page(vma))) 349330c9f3a9SLinus Torvalds return hugetlb_fault(mm, vma, address, flags); 34941da177e4SLinus Torvalds 34951da177e4SLinus Torvalds pgd = pgd_offset(mm, address); 34961da177e4SLinus Torvalds pud = pud_alloc(mm, pgd, address); 34971da177e4SLinus Torvalds if (!pud) 3498c74df32cSHugh Dickins return VM_FAULT_OOM; 34991da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, address); 35001da177e4SLinus Torvalds if (!pmd) 3501c74df32cSHugh Dickins return VM_FAULT_OOM; 350271e3aac0SAndrea Arcangeli if (pmd_none(*pmd) && transparent_hugepage_enabled(vma)) { 350371e3aac0SAndrea Arcangeli if (!vma->vm_ops) 350471e3aac0SAndrea Arcangeli return do_huge_pmd_anonymous_page(mm, vma, address, 350571e3aac0SAndrea Arcangeli pmd, flags); 350671e3aac0SAndrea Arcangeli } else { 350771e3aac0SAndrea Arcangeli pmd_t orig_pmd = *pmd; 350871e3aac0SAndrea Arcangeli barrier(); 350971e3aac0SAndrea Arcangeli if (pmd_trans_huge(orig_pmd)) { 351071e3aac0SAndrea Arcangeli if (flags & FAULT_FLAG_WRITE && 351171e3aac0SAndrea Arcangeli !pmd_write(orig_pmd) && 351271e3aac0SAndrea Arcangeli !pmd_trans_splitting(orig_pmd)) 351371e3aac0SAndrea Arcangeli return do_huge_pmd_wp_page(mm, vma, address, 351471e3aac0SAndrea Arcangeli pmd, orig_pmd); 351571e3aac0SAndrea Arcangeli return 0; 351671e3aac0SAndrea Arcangeli } 351771e3aac0SAndrea Arcangeli } 351871e3aac0SAndrea Arcangeli 351971e3aac0SAndrea Arcangeli /* 352071e3aac0SAndrea Arcangeli * Use __pte_alloc instead of pte_alloc_map, because we can't 352171e3aac0SAndrea Arcangeli * run pte_offset_map on the pmd, if an huge pmd could 352271e3aac0SAndrea Arcangeli * materialize from under us from a different thread. 352371e3aac0SAndrea Arcangeli */ 3524cc03638dSMel Gorman if (unlikely(pmd_none(*pmd)) && __pte_alloc(mm, vma, pmd, address)) 3525c74df32cSHugh Dickins return VM_FAULT_OOM; 352671e3aac0SAndrea Arcangeli /* if an huge pmd materialized from under us just retry later */ 352771e3aac0SAndrea Arcangeli if (unlikely(pmd_trans_huge(*pmd))) 352871e3aac0SAndrea Arcangeli return 0; 352971e3aac0SAndrea Arcangeli /* 353071e3aac0SAndrea Arcangeli * A regular pmd is established and it can't morph into a huge pmd 353171e3aac0SAndrea Arcangeli * from under us anymore at this point because we hold the mmap_sem 353271e3aac0SAndrea Arcangeli * read mode and khugepaged takes it in write mode. So now it's 353371e3aac0SAndrea Arcangeli * safe to run pte_offset_map(). 353471e3aac0SAndrea Arcangeli */ 353571e3aac0SAndrea Arcangeli pte = pte_offset_map(pmd, address); 35361da177e4SLinus Torvalds 353730c9f3a9SLinus Torvalds return handle_pte_fault(mm, vma, address, pte, pmd, flags); 35381da177e4SLinus Torvalds } 35391da177e4SLinus Torvalds 35401da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED 35411da177e4SLinus Torvalds /* 35421da177e4SLinus Torvalds * Allocate page upper directory. 3543872fec16SHugh Dickins * We've already handled the fast-path in-line. 35441da177e4SLinus Torvalds */ 35451bb3630eSHugh Dickins int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address) 35461da177e4SLinus Torvalds { 3547c74df32cSHugh Dickins pud_t *new = pud_alloc_one(mm, address); 3548c74df32cSHugh Dickins if (!new) 35491bb3630eSHugh Dickins return -ENOMEM; 35501da177e4SLinus Torvalds 3551362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 3552362a61adSNick Piggin 3553872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 35541bb3630eSHugh Dickins if (pgd_present(*pgd)) /* Another has populated it */ 35555e541973SBenjamin Herrenschmidt pud_free(mm, new); 35561bb3630eSHugh Dickins else 35571da177e4SLinus Torvalds pgd_populate(mm, pgd, new); 3558872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 35591bb3630eSHugh Dickins return 0; 35601da177e4SLinus Torvalds } 35611da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */ 35621da177e4SLinus Torvalds 35631da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED 35641da177e4SLinus Torvalds /* 35651da177e4SLinus Torvalds * Allocate page middle directory. 3566872fec16SHugh Dickins * We've already handled the fast-path in-line. 35671da177e4SLinus Torvalds */ 35681bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) 35691da177e4SLinus Torvalds { 3570c74df32cSHugh Dickins pmd_t *new = pmd_alloc_one(mm, address); 3571c74df32cSHugh Dickins if (!new) 35721bb3630eSHugh Dickins return -ENOMEM; 35731da177e4SLinus Torvalds 3574362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 3575362a61adSNick Piggin 3576872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 35771da177e4SLinus Torvalds #ifndef __ARCH_HAS_4LEVEL_HACK 35781bb3630eSHugh Dickins if (pud_present(*pud)) /* Another has populated it */ 35795e541973SBenjamin Herrenschmidt pmd_free(mm, new); 35801bb3630eSHugh Dickins else 35811da177e4SLinus Torvalds pud_populate(mm, pud, new); 35821da177e4SLinus Torvalds #else 35831bb3630eSHugh Dickins if (pgd_present(*pud)) /* Another has populated it */ 35845e541973SBenjamin Herrenschmidt pmd_free(mm, new); 35851bb3630eSHugh Dickins else 35861da177e4SLinus Torvalds pgd_populate(mm, pud, new); 35871da177e4SLinus Torvalds #endif /* __ARCH_HAS_4LEVEL_HACK */ 3588872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 35891bb3630eSHugh Dickins return 0; 35901da177e4SLinus Torvalds } 35911da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */ 35921da177e4SLinus Torvalds 35931da177e4SLinus Torvalds int make_pages_present(unsigned long addr, unsigned long end) 35941da177e4SLinus Torvalds { 35951da177e4SLinus Torvalds int ret, len, write; 35961da177e4SLinus Torvalds struct vm_area_struct * vma; 35971da177e4SLinus Torvalds 35981da177e4SLinus Torvalds vma = find_vma(current->mm, addr); 35991da177e4SLinus Torvalds if (!vma) 3600a477097dSKOSAKI Motohiro return -ENOMEM; 36015ecfda04SMichel Lespinasse /* 36025ecfda04SMichel Lespinasse * We want to touch writable mappings with a write fault in order 36035ecfda04SMichel Lespinasse * to break COW, except for shared mappings because these don't COW 36045ecfda04SMichel Lespinasse * and we would not want to dirty them for nothing. 36055ecfda04SMichel Lespinasse */ 36065ecfda04SMichel Lespinasse write = (vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE; 36075bcb28b1SEric Sesterhenn BUG_ON(addr >= end); 36085bcb28b1SEric Sesterhenn BUG_ON(end > vma->vm_end); 360968e116a3SRolf Eike Beer len = DIV_ROUND_UP(end, PAGE_SIZE) - addr/PAGE_SIZE; 36101da177e4SLinus Torvalds ret = get_user_pages(current, current->mm, addr, 36111da177e4SLinus Torvalds len, write, 0, NULL, NULL); 3612c11d69d8SLee Schermerhorn if (ret < 0) 36131da177e4SLinus Torvalds return ret; 36149978ad58SLee Schermerhorn return ret == len ? 0 : -EFAULT; 36151da177e4SLinus Torvalds } 36161da177e4SLinus Torvalds 36171da177e4SLinus Torvalds #if !defined(__HAVE_ARCH_GATE_AREA) 36181da177e4SLinus Torvalds 36191da177e4SLinus Torvalds #if defined(AT_SYSINFO_EHDR) 36205ce7852cSAdrian Bunk static struct vm_area_struct gate_vma; 36211da177e4SLinus Torvalds 36221da177e4SLinus Torvalds static int __init gate_vma_init(void) 36231da177e4SLinus Torvalds { 36241da177e4SLinus Torvalds gate_vma.vm_mm = NULL; 36251da177e4SLinus Torvalds gate_vma.vm_start = FIXADDR_USER_START; 36261da177e4SLinus Torvalds gate_vma.vm_end = FIXADDR_USER_END; 3627b6558c4aSRoland McGrath gate_vma.vm_flags = VM_READ | VM_MAYREAD | VM_EXEC | VM_MAYEXEC; 3628b6558c4aSRoland McGrath gate_vma.vm_page_prot = __P101; 3629909af768SJason Baron 36301da177e4SLinus Torvalds return 0; 36311da177e4SLinus Torvalds } 36321da177e4SLinus Torvalds __initcall(gate_vma_init); 36331da177e4SLinus Torvalds #endif 36341da177e4SLinus Torvalds 363531db58b3SStephen Wilson struct vm_area_struct *get_gate_vma(struct mm_struct *mm) 36361da177e4SLinus Torvalds { 36371da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR 36381da177e4SLinus Torvalds return &gate_vma; 36391da177e4SLinus Torvalds #else 36401da177e4SLinus Torvalds return NULL; 36411da177e4SLinus Torvalds #endif 36421da177e4SLinus Torvalds } 36431da177e4SLinus Torvalds 3644cae5d390SStephen Wilson int in_gate_area_no_mm(unsigned long addr) 36451da177e4SLinus Torvalds { 36461da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR 36471da177e4SLinus Torvalds if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END)) 36481da177e4SLinus Torvalds return 1; 36491da177e4SLinus Torvalds #endif 36501da177e4SLinus Torvalds return 0; 36511da177e4SLinus Torvalds } 36521da177e4SLinus Torvalds 36531da177e4SLinus Torvalds #endif /* __HAVE_ARCH_GATE_AREA */ 36540ec76a11SDavid Howells 36551b36ba81SNamhyung Kim static int __follow_pte(struct mm_struct *mm, unsigned long address, 3656f8ad0f49SJohannes Weiner pte_t **ptepp, spinlock_t **ptlp) 3657f8ad0f49SJohannes Weiner { 3658f8ad0f49SJohannes Weiner pgd_t *pgd; 3659f8ad0f49SJohannes Weiner pud_t *pud; 3660f8ad0f49SJohannes Weiner pmd_t *pmd; 3661f8ad0f49SJohannes Weiner pte_t *ptep; 3662f8ad0f49SJohannes Weiner 3663f8ad0f49SJohannes Weiner pgd = pgd_offset(mm, address); 3664f8ad0f49SJohannes Weiner if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) 3665f8ad0f49SJohannes Weiner goto out; 3666f8ad0f49SJohannes Weiner 3667f8ad0f49SJohannes Weiner pud = pud_offset(pgd, address); 3668f8ad0f49SJohannes Weiner if (pud_none(*pud) || unlikely(pud_bad(*pud))) 3669f8ad0f49SJohannes Weiner goto out; 3670f8ad0f49SJohannes Weiner 3671f8ad0f49SJohannes Weiner pmd = pmd_offset(pud, address); 3672f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 3673f8ad0f49SJohannes Weiner if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd))) 3674f8ad0f49SJohannes Weiner goto out; 3675f8ad0f49SJohannes Weiner 3676f8ad0f49SJohannes Weiner /* We cannot handle huge page PFN maps. Luckily they don't exist. */ 3677f8ad0f49SJohannes Weiner if (pmd_huge(*pmd)) 3678f8ad0f49SJohannes Weiner goto out; 3679f8ad0f49SJohannes Weiner 3680f8ad0f49SJohannes Weiner ptep = pte_offset_map_lock(mm, pmd, address, ptlp); 3681f8ad0f49SJohannes Weiner if (!ptep) 3682f8ad0f49SJohannes Weiner goto out; 3683f8ad0f49SJohannes Weiner if (!pte_present(*ptep)) 3684f8ad0f49SJohannes Weiner goto unlock; 3685f8ad0f49SJohannes Weiner *ptepp = ptep; 3686f8ad0f49SJohannes Weiner return 0; 3687f8ad0f49SJohannes Weiner unlock: 3688f8ad0f49SJohannes Weiner pte_unmap_unlock(ptep, *ptlp); 3689f8ad0f49SJohannes Weiner out: 3690f8ad0f49SJohannes Weiner return -EINVAL; 3691f8ad0f49SJohannes Weiner } 3692f8ad0f49SJohannes Weiner 36931b36ba81SNamhyung Kim static inline int follow_pte(struct mm_struct *mm, unsigned long address, 36941b36ba81SNamhyung Kim pte_t **ptepp, spinlock_t **ptlp) 36951b36ba81SNamhyung Kim { 36961b36ba81SNamhyung Kim int res; 36971b36ba81SNamhyung Kim 36981b36ba81SNamhyung Kim /* (void) is needed to make gcc happy */ 36991b36ba81SNamhyung Kim (void) __cond_lock(*ptlp, 37001b36ba81SNamhyung Kim !(res = __follow_pte(mm, address, ptepp, ptlp))); 37011b36ba81SNamhyung Kim return res; 37021b36ba81SNamhyung Kim } 37031b36ba81SNamhyung Kim 37043b6748e2SJohannes Weiner /** 37053b6748e2SJohannes Weiner * follow_pfn - look up PFN at a user virtual address 37063b6748e2SJohannes Weiner * @vma: memory mapping 37073b6748e2SJohannes Weiner * @address: user virtual address 37083b6748e2SJohannes Weiner * @pfn: location to store found PFN 37093b6748e2SJohannes Weiner * 37103b6748e2SJohannes Weiner * Only IO mappings and raw PFN mappings are allowed. 37113b6748e2SJohannes Weiner * 37123b6748e2SJohannes Weiner * Returns zero and the pfn at @pfn on success, -ve otherwise. 37133b6748e2SJohannes Weiner */ 37143b6748e2SJohannes Weiner int follow_pfn(struct vm_area_struct *vma, unsigned long address, 37153b6748e2SJohannes Weiner unsigned long *pfn) 37163b6748e2SJohannes Weiner { 37173b6748e2SJohannes Weiner int ret = -EINVAL; 37183b6748e2SJohannes Weiner spinlock_t *ptl; 37193b6748e2SJohannes Weiner pte_t *ptep; 37203b6748e2SJohannes Weiner 37213b6748e2SJohannes Weiner if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 37223b6748e2SJohannes Weiner return ret; 37233b6748e2SJohannes Weiner 37243b6748e2SJohannes Weiner ret = follow_pte(vma->vm_mm, address, &ptep, &ptl); 37253b6748e2SJohannes Weiner if (ret) 37263b6748e2SJohannes Weiner return ret; 37273b6748e2SJohannes Weiner *pfn = pte_pfn(*ptep); 37283b6748e2SJohannes Weiner pte_unmap_unlock(ptep, ptl); 37293b6748e2SJohannes Weiner return 0; 37303b6748e2SJohannes Weiner } 37313b6748e2SJohannes Weiner EXPORT_SYMBOL(follow_pfn); 37323b6748e2SJohannes Weiner 373328b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT 3734d87fe660Svenkatesh.pallipadi@intel.com int follow_phys(struct vm_area_struct *vma, 373528b2ee20SRik van Riel unsigned long address, unsigned int flags, 3736d87fe660Svenkatesh.pallipadi@intel.com unsigned long *prot, resource_size_t *phys) 373728b2ee20SRik van Riel { 373803668a4dSJohannes Weiner int ret = -EINVAL; 373928b2ee20SRik van Riel pte_t *ptep, pte; 374028b2ee20SRik van Riel spinlock_t *ptl; 374128b2ee20SRik van Riel 3742d87fe660Svenkatesh.pallipadi@intel.com if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 3743d87fe660Svenkatesh.pallipadi@intel.com goto out; 374428b2ee20SRik van Riel 374503668a4dSJohannes Weiner if (follow_pte(vma->vm_mm, address, &ptep, &ptl)) 3746d87fe660Svenkatesh.pallipadi@intel.com goto out; 374728b2ee20SRik van Riel pte = *ptep; 374803668a4dSJohannes Weiner 374928b2ee20SRik van Riel if ((flags & FOLL_WRITE) && !pte_write(pte)) 375028b2ee20SRik van Riel goto unlock; 375128b2ee20SRik van Riel 375228b2ee20SRik van Riel *prot = pgprot_val(pte_pgprot(pte)); 375303668a4dSJohannes Weiner *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT; 375428b2ee20SRik van Riel 375503668a4dSJohannes Weiner ret = 0; 375628b2ee20SRik van Riel unlock: 375728b2ee20SRik van Riel pte_unmap_unlock(ptep, ptl); 375828b2ee20SRik van Riel out: 3759d87fe660Svenkatesh.pallipadi@intel.com return ret; 376028b2ee20SRik van Riel } 376128b2ee20SRik van Riel 376228b2ee20SRik van Riel int generic_access_phys(struct vm_area_struct *vma, unsigned long addr, 376328b2ee20SRik van Riel void *buf, int len, int write) 376428b2ee20SRik van Riel { 376528b2ee20SRik van Riel resource_size_t phys_addr; 376628b2ee20SRik van Riel unsigned long prot = 0; 37672bc7273bSKOSAKI Motohiro void __iomem *maddr; 376828b2ee20SRik van Riel int offset = addr & (PAGE_SIZE-1); 376928b2ee20SRik van Riel 3770d87fe660Svenkatesh.pallipadi@intel.com if (follow_phys(vma, addr, write, &prot, &phys_addr)) 377128b2ee20SRik van Riel return -EINVAL; 377228b2ee20SRik van Riel 377328b2ee20SRik van Riel maddr = ioremap_prot(phys_addr, PAGE_SIZE, prot); 377428b2ee20SRik van Riel if (write) 377528b2ee20SRik van Riel memcpy_toio(maddr + offset, buf, len); 377628b2ee20SRik van Riel else 377728b2ee20SRik van Riel memcpy_fromio(buf, maddr + offset, len); 377828b2ee20SRik van Riel iounmap(maddr); 377928b2ee20SRik van Riel 378028b2ee20SRik van Riel return len; 378128b2ee20SRik van Riel } 378228b2ee20SRik van Riel #endif 378328b2ee20SRik van Riel 37840ec76a11SDavid Howells /* 3785206cb636SStephen Wilson * Access another process' address space as given in mm. If non-NULL, use the 3786206cb636SStephen Wilson * given task for page fault accounting. 37870ec76a11SDavid Howells */ 3788206cb636SStephen Wilson static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm, 3789206cb636SStephen Wilson unsigned long addr, void *buf, int len, int write) 37900ec76a11SDavid Howells { 37910ec76a11SDavid Howells struct vm_area_struct *vma; 37920ec76a11SDavid Howells void *old_buf = buf; 37930ec76a11SDavid Howells 37940ec76a11SDavid Howells down_read(&mm->mmap_sem); 3795183ff22bSSimon Arlott /* ignore errors, just check how much was successfully transferred */ 37960ec76a11SDavid Howells while (len) { 37970ec76a11SDavid Howells int bytes, ret, offset; 37980ec76a11SDavid Howells void *maddr; 379928b2ee20SRik van Riel struct page *page = NULL; 38000ec76a11SDavid Howells 38010ec76a11SDavid Howells ret = get_user_pages(tsk, mm, addr, 1, 38020ec76a11SDavid Howells write, 1, &page, &vma); 380328b2ee20SRik van Riel if (ret <= 0) { 380428b2ee20SRik van Riel /* 380528b2ee20SRik van Riel * Check if this is a VM_IO | VM_PFNMAP VMA, which 380628b2ee20SRik van Riel * we can access using slightly different code. 380728b2ee20SRik van Riel */ 380828b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT 380928b2ee20SRik van Riel vma = find_vma(mm, addr); 3810fe936dfcSMichael Ellerman if (!vma || vma->vm_start > addr) 38110ec76a11SDavid Howells break; 381228b2ee20SRik van Riel if (vma->vm_ops && vma->vm_ops->access) 381328b2ee20SRik van Riel ret = vma->vm_ops->access(vma, addr, buf, 381428b2ee20SRik van Riel len, write); 381528b2ee20SRik van Riel if (ret <= 0) 381628b2ee20SRik van Riel #endif 381728b2ee20SRik van Riel break; 381828b2ee20SRik van Riel bytes = ret; 381928b2ee20SRik van Riel } else { 38200ec76a11SDavid Howells bytes = len; 38210ec76a11SDavid Howells offset = addr & (PAGE_SIZE-1); 38220ec76a11SDavid Howells if (bytes > PAGE_SIZE-offset) 38230ec76a11SDavid Howells bytes = PAGE_SIZE-offset; 38240ec76a11SDavid Howells 38250ec76a11SDavid Howells maddr = kmap(page); 38260ec76a11SDavid Howells if (write) { 38270ec76a11SDavid Howells copy_to_user_page(vma, page, addr, 38280ec76a11SDavid Howells maddr + offset, buf, bytes); 38290ec76a11SDavid Howells set_page_dirty_lock(page); 38300ec76a11SDavid Howells } else { 38310ec76a11SDavid Howells copy_from_user_page(vma, page, addr, 38320ec76a11SDavid Howells buf, maddr + offset, bytes); 38330ec76a11SDavid Howells } 38340ec76a11SDavid Howells kunmap(page); 38350ec76a11SDavid Howells page_cache_release(page); 383628b2ee20SRik van Riel } 38370ec76a11SDavid Howells len -= bytes; 38380ec76a11SDavid Howells buf += bytes; 38390ec76a11SDavid Howells addr += bytes; 38400ec76a11SDavid Howells } 38410ec76a11SDavid Howells up_read(&mm->mmap_sem); 38420ec76a11SDavid Howells 38430ec76a11SDavid Howells return buf - old_buf; 38440ec76a11SDavid Howells } 384503252919SAndi Kleen 38465ddd36b9SStephen Wilson /** 3847ae91dbfcSRandy Dunlap * access_remote_vm - access another process' address space 38485ddd36b9SStephen Wilson * @mm: the mm_struct of the target address space 38495ddd36b9SStephen Wilson * @addr: start address to access 38505ddd36b9SStephen Wilson * @buf: source or destination buffer 38515ddd36b9SStephen Wilson * @len: number of bytes to transfer 38525ddd36b9SStephen Wilson * @write: whether the access is a write 38535ddd36b9SStephen Wilson * 38545ddd36b9SStephen Wilson * The caller must hold a reference on @mm. 38555ddd36b9SStephen Wilson */ 38565ddd36b9SStephen Wilson int access_remote_vm(struct mm_struct *mm, unsigned long addr, 38575ddd36b9SStephen Wilson void *buf, int len, int write) 38585ddd36b9SStephen Wilson { 38595ddd36b9SStephen Wilson return __access_remote_vm(NULL, mm, addr, buf, len, write); 38605ddd36b9SStephen Wilson } 38615ddd36b9SStephen Wilson 386203252919SAndi Kleen /* 3863206cb636SStephen Wilson * Access another process' address space. 3864206cb636SStephen Wilson * Source/target buffer must be kernel space, 3865206cb636SStephen Wilson * Do not walk the page table directly, use get_user_pages 3866206cb636SStephen Wilson */ 3867206cb636SStephen Wilson int access_process_vm(struct task_struct *tsk, unsigned long addr, 3868206cb636SStephen Wilson void *buf, int len, int write) 3869206cb636SStephen Wilson { 3870206cb636SStephen Wilson struct mm_struct *mm; 3871206cb636SStephen Wilson int ret; 3872206cb636SStephen Wilson 3873206cb636SStephen Wilson mm = get_task_mm(tsk); 3874206cb636SStephen Wilson if (!mm) 3875206cb636SStephen Wilson return 0; 3876206cb636SStephen Wilson 3877206cb636SStephen Wilson ret = __access_remote_vm(tsk, mm, addr, buf, len, write); 3878206cb636SStephen Wilson mmput(mm); 3879206cb636SStephen Wilson 3880206cb636SStephen Wilson return ret; 3881206cb636SStephen Wilson } 3882206cb636SStephen Wilson 388303252919SAndi Kleen /* 388403252919SAndi Kleen * Print the name of a VMA. 388503252919SAndi Kleen */ 388603252919SAndi Kleen void print_vma_addr(char *prefix, unsigned long ip) 388703252919SAndi Kleen { 388803252919SAndi Kleen struct mm_struct *mm = current->mm; 388903252919SAndi Kleen struct vm_area_struct *vma; 389003252919SAndi Kleen 3891e8bff74aSIngo Molnar /* 3892e8bff74aSIngo Molnar * Do not print if we are in atomic 3893e8bff74aSIngo Molnar * contexts (in exception stacks, etc.): 3894e8bff74aSIngo Molnar */ 3895e8bff74aSIngo Molnar if (preempt_count()) 3896e8bff74aSIngo Molnar return; 3897e8bff74aSIngo Molnar 389803252919SAndi Kleen down_read(&mm->mmap_sem); 389903252919SAndi Kleen vma = find_vma(mm, ip); 390003252919SAndi Kleen if (vma && vma->vm_file) { 390103252919SAndi Kleen struct file *f = vma->vm_file; 390203252919SAndi Kleen char *buf = (char *)__get_free_page(GFP_KERNEL); 390303252919SAndi Kleen if (buf) { 390403252919SAndi Kleen char *p, *s; 390503252919SAndi Kleen 3906cf28b486SJan Blunck p = d_path(&f->f_path, buf, PAGE_SIZE); 390703252919SAndi Kleen if (IS_ERR(p)) 390803252919SAndi Kleen p = "?"; 390903252919SAndi Kleen s = strrchr(p, '/'); 391003252919SAndi Kleen if (s) 391103252919SAndi Kleen p = s+1; 391203252919SAndi Kleen printk("%s%s[%lx+%lx]", prefix, p, 391303252919SAndi Kleen vma->vm_start, 391403252919SAndi Kleen vma->vm_end - vma->vm_start); 391503252919SAndi Kleen free_page((unsigned long)buf); 391603252919SAndi Kleen } 391703252919SAndi Kleen } 391803252919SAndi Kleen up_read(¤t->mm->mmap_sem); 391903252919SAndi Kleen } 39203ee1afa3SNick Piggin 39213ee1afa3SNick Piggin #ifdef CONFIG_PROVE_LOCKING 39223ee1afa3SNick Piggin void might_fault(void) 39233ee1afa3SNick Piggin { 392495156f00SPeter Zijlstra /* 392595156f00SPeter Zijlstra * Some code (nfs/sunrpc) uses socket ops on kernel memory while 392695156f00SPeter Zijlstra * holding the mmap_sem, this is safe because kernel memory doesn't 392795156f00SPeter Zijlstra * get paged out, therefore we'll never actually fault, and the 392895156f00SPeter Zijlstra * below annotations will generate false positives. 392995156f00SPeter Zijlstra */ 393095156f00SPeter Zijlstra if (segment_eq(get_fs(), KERNEL_DS)) 393195156f00SPeter Zijlstra return; 393295156f00SPeter Zijlstra 39333ee1afa3SNick Piggin might_sleep(); 39343ee1afa3SNick Piggin /* 39353ee1afa3SNick Piggin * it would be nicer only to annotate paths which are not under 39363ee1afa3SNick Piggin * pagefault_disable, however that requires a larger audit and 39373ee1afa3SNick Piggin * providing helpers like get_user_atomic. 39383ee1afa3SNick Piggin */ 39393ee1afa3SNick Piggin if (!in_atomic() && current->mm) 39403ee1afa3SNick Piggin might_lock_read(¤t->mm->mmap_sem); 39413ee1afa3SNick Piggin } 39423ee1afa3SNick Piggin EXPORT_SYMBOL(might_fault); 39433ee1afa3SNick Piggin #endif 394447ad8475SAndrea Arcangeli 394547ad8475SAndrea Arcangeli #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS) 394647ad8475SAndrea Arcangeli static void clear_gigantic_page(struct page *page, 394747ad8475SAndrea Arcangeli unsigned long addr, 394847ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 394947ad8475SAndrea Arcangeli { 395047ad8475SAndrea Arcangeli int i; 395147ad8475SAndrea Arcangeli struct page *p = page; 395247ad8475SAndrea Arcangeli 395347ad8475SAndrea Arcangeli might_sleep(); 395447ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; 395547ad8475SAndrea Arcangeli i++, p = mem_map_next(p, page, i)) { 395647ad8475SAndrea Arcangeli cond_resched(); 395747ad8475SAndrea Arcangeli clear_user_highpage(p, addr + i * PAGE_SIZE); 395847ad8475SAndrea Arcangeli } 395947ad8475SAndrea Arcangeli } 396047ad8475SAndrea Arcangeli void clear_huge_page(struct page *page, 396147ad8475SAndrea Arcangeli unsigned long addr, unsigned int pages_per_huge_page) 396247ad8475SAndrea Arcangeli { 396347ad8475SAndrea Arcangeli int i; 396447ad8475SAndrea Arcangeli 396547ad8475SAndrea Arcangeli if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { 396647ad8475SAndrea Arcangeli clear_gigantic_page(page, addr, pages_per_huge_page); 396747ad8475SAndrea Arcangeli return; 396847ad8475SAndrea Arcangeli } 396947ad8475SAndrea Arcangeli 397047ad8475SAndrea Arcangeli might_sleep(); 397147ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; i++) { 397247ad8475SAndrea Arcangeli cond_resched(); 397347ad8475SAndrea Arcangeli clear_user_highpage(page + i, addr + i * PAGE_SIZE); 397447ad8475SAndrea Arcangeli } 397547ad8475SAndrea Arcangeli } 397647ad8475SAndrea Arcangeli 397747ad8475SAndrea Arcangeli static void copy_user_gigantic_page(struct page *dst, struct page *src, 397847ad8475SAndrea Arcangeli unsigned long addr, 397947ad8475SAndrea Arcangeli struct vm_area_struct *vma, 398047ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 398147ad8475SAndrea Arcangeli { 398247ad8475SAndrea Arcangeli int i; 398347ad8475SAndrea Arcangeli struct page *dst_base = dst; 398447ad8475SAndrea Arcangeli struct page *src_base = src; 398547ad8475SAndrea Arcangeli 398647ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; ) { 398747ad8475SAndrea Arcangeli cond_resched(); 398847ad8475SAndrea Arcangeli copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma); 398947ad8475SAndrea Arcangeli 399047ad8475SAndrea Arcangeli i++; 399147ad8475SAndrea Arcangeli dst = mem_map_next(dst, dst_base, i); 399247ad8475SAndrea Arcangeli src = mem_map_next(src, src_base, i); 399347ad8475SAndrea Arcangeli } 399447ad8475SAndrea Arcangeli } 399547ad8475SAndrea Arcangeli 399647ad8475SAndrea Arcangeli void copy_user_huge_page(struct page *dst, struct page *src, 399747ad8475SAndrea Arcangeli unsigned long addr, struct vm_area_struct *vma, 399847ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 399947ad8475SAndrea Arcangeli { 400047ad8475SAndrea Arcangeli int i; 400147ad8475SAndrea Arcangeli 400247ad8475SAndrea Arcangeli if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { 400347ad8475SAndrea Arcangeli copy_user_gigantic_page(dst, src, addr, vma, 400447ad8475SAndrea Arcangeli pages_per_huge_page); 400547ad8475SAndrea Arcangeli return; 400647ad8475SAndrea Arcangeli } 400747ad8475SAndrea Arcangeli 400847ad8475SAndrea Arcangeli might_sleep(); 400947ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; i++) { 401047ad8475SAndrea Arcangeli cond_resched(); 401147ad8475SAndrea Arcangeli copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma); 401247ad8475SAndrea Arcangeli } 401347ad8475SAndrea Arcangeli } 401447ad8475SAndrea Arcangeli #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */ 4015