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; 209597e1c35SAlex Shi tlb->start = -1UL; 210597e1c35SAlex Shi tlb->end = 0; 2119547d01bSPeter Zijlstra tlb->need_flush = 0; 2129547d01bSPeter Zijlstra tlb->fast_mode = (num_possible_cpus() == 1); 2139547d01bSPeter Zijlstra tlb->local.next = NULL; 2149547d01bSPeter Zijlstra tlb->local.nr = 0; 2159547d01bSPeter Zijlstra tlb->local.max = ARRAY_SIZE(tlb->__pages); 2169547d01bSPeter Zijlstra tlb->active = &tlb->local; 2179547d01bSPeter Zijlstra 2189547d01bSPeter Zijlstra #ifdef CONFIG_HAVE_RCU_TABLE_FREE 2199547d01bSPeter Zijlstra tlb->batch = NULL; 22034e55232SKAMEZAWA Hiroyuki #endif 2219547d01bSPeter Zijlstra } 2229547d01bSPeter Zijlstra 2239547d01bSPeter Zijlstra void tlb_flush_mmu(struct mmu_gather *tlb) 2249547d01bSPeter Zijlstra { 2259547d01bSPeter Zijlstra struct mmu_gather_batch *batch; 2269547d01bSPeter Zijlstra 2279547d01bSPeter Zijlstra if (!tlb->need_flush) 2289547d01bSPeter Zijlstra return; 2299547d01bSPeter Zijlstra tlb->need_flush = 0; 2309547d01bSPeter Zijlstra tlb_flush(tlb); 2319547d01bSPeter Zijlstra #ifdef CONFIG_HAVE_RCU_TABLE_FREE 2329547d01bSPeter Zijlstra tlb_table_flush(tlb); 2339547d01bSPeter Zijlstra #endif 2349547d01bSPeter Zijlstra 2359547d01bSPeter Zijlstra if (tlb_fast_mode(tlb)) 2369547d01bSPeter Zijlstra return; 2379547d01bSPeter Zijlstra 2389547d01bSPeter Zijlstra for (batch = &tlb->local; batch; batch = batch->next) { 2399547d01bSPeter Zijlstra free_pages_and_swap_cache(batch->pages, batch->nr); 2409547d01bSPeter Zijlstra batch->nr = 0; 2419547d01bSPeter Zijlstra } 2429547d01bSPeter Zijlstra tlb->active = &tlb->local; 2439547d01bSPeter Zijlstra } 2449547d01bSPeter Zijlstra 2459547d01bSPeter Zijlstra /* tlb_finish_mmu 2469547d01bSPeter Zijlstra * Called at the end of the shootdown operation to free up any resources 2479547d01bSPeter Zijlstra * that were required. 2489547d01bSPeter Zijlstra */ 2499547d01bSPeter Zijlstra void tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end) 2509547d01bSPeter Zijlstra { 2519547d01bSPeter Zijlstra struct mmu_gather_batch *batch, *next; 2529547d01bSPeter Zijlstra 253597e1c35SAlex Shi tlb->start = start; 254597e1c35SAlex Shi tlb->end = end; 2559547d01bSPeter Zijlstra tlb_flush_mmu(tlb); 2569547d01bSPeter Zijlstra 2579547d01bSPeter Zijlstra /* keep the page table cache within bounds */ 2589547d01bSPeter Zijlstra check_pgt_cache(); 2599547d01bSPeter Zijlstra 2609547d01bSPeter Zijlstra for (batch = tlb->local.next; batch; batch = next) { 2619547d01bSPeter Zijlstra next = batch->next; 2629547d01bSPeter Zijlstra free_pages((unsigned long)batch, 0); 2639547d01bSPeter Zijlstra } 2649547d01bSPeter Zijlstra tlb->local.next = NULL; 2659547d01bSPeter Zijlstra } 2669547d01bSPeter Zijlstra 2679547d01bSPeter Zijlstra /* __tlb_remove_page 2689547d01bSPeter Zijlstra * Must perform the equivalent to __free_pte(pte_get_and_clear(ptep)), while 2699547d01bSPeter Zijlstra * handling the additional races in SMP caused by other CPUs caching valid 2709547d01bSPeter Zijlstra * mappings in their TLBs. Returns the number of free page slots left. 2719547d01bSPeter Zijlstra * When out of page slots we must call tlb_flush_mmu(). 2729547d01bSPeter Zijlstra */ 2739547d01bSPeter Zijlstra int __tlb_remove_page(struct mmu_gather *tlb, struct page *page) 2749547d01bSPeter Zijlstra { 2759547d01bSPeter Zijlstra struct mmu_gather_batch *batch; 2769547d01bSPeter Zijlstra 277f21760b1SShaohua Li VM_BUG_ON(!tlb->need_flush); 2789547d01bSPeter Zijlstra 2799547d01bSPeter Zijlstra if (tlb_fast_mode(tlb)) { 2809547d01bSPeter Zijlstra free_page_and_swap_cache(page); 2819547d01bSPeter Zijlstra return 1; /* avoid calling tlb_flush_mmu() */ 2829547d01bSPeter Zijlstra } 2839547d01bSPeter Zijlstra 2849547d01bSPeter Zijlstra batch = tlb->active; 2859547d01bSPeter Zijlstra batch->pages[batch->nr++] = page; 2869547d01bSPeter Zijlstra if (batch->nr == batch->max) { 2879547d01bSPeter Zijlstra if (!tlb_next_batch(tlb)) 2889547d01bSPeter Zijlstra return 0; 2890b43c3aaSShaohua Li batch = tlb->active; 2909547d01bSPeter Zijlstra } 2919547d01bSPeter Zijlstra VM_BUG_ON(batch->nr > batch->max); 2929547d01bSPeter Zijlstra 2939547d01bSPeter Zijlstra return batch->max - batch->nr; 2949547d01bSPeter Zijlstra } 2959547d01bSPeter Zijlstra 2969547d01bSPeter Zijlstra #endif /* HAVE_GENERIC_MMU_GATHER */ 29734e55232SKAMEZAWA Hiroyuki 29826723911SPeter Zijlstra #ifdef CONFIG_HAVE_RCU_TABLE_FREE 29926723911SPeter Zijlstra 30026723911SPeter Zijlstra /* 30126723911SPeter Zijlstra * See the comment near struct mmu_table_batch. 30226723911SPeter Zijlstra */ 30326723911SPeter Zijlstra 30426723911SPeter Zijlstra static void tlb_remove_table_smp_sync(void *arg) 30526723911SPeter Zijlstra { 30626723911SPeter Zijlstra /* Simply deliver the interrupt */ 30726723911SPeter Zijlstra } 30826723911SPeter Zijlstra 30926723911SPeter Zijlstra static void tlb_remove_table_one(void *table) 31026723911SPeter Zijlstra { 31126723911SPeter Zijlstra /* 31226723911SPeter Zijlstra * This isn't an RCU grace period and hence the page-tables cannot be 31326723911SPeter Zijlstra * assumed to be actually RCU-freed. 31426723911SPeter Zijlstra * 31526723911SPeter Zijlstra * It is however sufficient for software page-table walkers that rely on 31626723911SPeter Zijlstra * IRQ disabling. See the comment near struct mmu_table_batch. 31726723911SPeter Zijlstra */ 31826723911SPeter Zijlstra smp_call_function(tlb_remove_table_smp_sync, NULL, 1); 31926723911SPeter Zijlstra __tlb_remove_table(table); 32026723911SPeter Zijlstra } 32126723911SPeter Zijlstra 32226723911SPeter Zijlstra static void tlb_remove_table_rcu(struct rcu_head *head) 32326723911SPeter Zijlstra { 32426723911SPeter Zijlstra struct mmu_table_batch *batch; 32526723911SPeter Zijlstra int i; 32626723911SPeter Zijlstra 32726723911SPeter Zijlstra batch = container_of(head, struct mmu_table_batch, rcu); 32826723911SPeter Zijlstra 32926723911SPeter Zijlstra for (i = 0; i < batch->nr; i++) 33026723911SPeter Zijlstra __tlb_remove_table(batch->tables[i]); 33126723911SPeter Zijlstra 33226723911SPeter Zijlstra free_page((unsigned long)batch); 33326723911SPeter Zijlstra } 33426723911SPeter Zijlstra 33526723911SPeter Zijlstra void tlb_table_flush(struct mmu_gather *tlb) 33626723911SPeter Zijlstra { 33726723911SPeter Zijlstra struct mmu_table_batch **batch = &tlb->batch; 33826723911SPeter Zijlstra 33926723911SPeter Zijlstra if (*batch) { 34026723911SPeter Zijlstra call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu); 34126723911SPeter Zijlstra *batch = NULL; 34226723911SPeter Zijlstra } 34326723911SPeter Zijlstra } 34426723911SPeter Zijlstra 34526723911SPeter Zijlstra void tlb_remove_table(struct mmu_gather *tlb, void *table) 34626723911SPeter Zijlstra { 34726723911SPeter Zijlstra struct mmu_table_batch **batch = &tlb->batch; 34826723911SPeter Zijlstra 34926723911SPeter Zijlstra tlb->need_flush = 1; 35026723911SPeter Zijlstra 35126723911SPeter Zijlstra /* 35226723911SPeter Zijlstra * When there's less then two users of this mm there cannot be a 35326723911SPeter Zijlstra * concurrent page-table walk. 35426723911SPeter Zijlstra */ 35526723911SPeter Zijlstra if (atomic_read(&tlb->mm->mm_users) < 2) { 35626723911SPeter Zijlstra __tlb_remove_table(table); 35726723911SPeter Zijlstra return; 35826723911SPeter Zijlstra } 35926723911SPeter Zijlstra 36026723911SPeter Zijlstra if (*batch == NULL) { 36126723911SPeter Zijlstra *batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN); 36226723911SPeter Zijlstra if (*batch == NULL) { 36326723911SPeter Zijlstra tlb_remove_table_one(table); 36426723911SPeter Zijlstra return; 36526723911SPeter Zijlstra } 36626723911SPeter Zijlstra (*batch)->nr = 0; 36726723911SPeter Zijlstra } 36826723911SPeter Zijlstra (*batch)->tables[(*batch)->nr++] = table; 36926723911SPeter Zijlstra if ((*batch)->nr == MAX_TABLE_BATCH) 37026723911SPeter Zijlstra tlb_table_flush(tlb); 37126723911SPeter Zijlstra } 37226723911SPeter Zijlstra 3739547d01bSPeter Zijlstra #endif /* CONFIG_HAVE_RCU_TABLE_FREE */ 37426723911SPeter Zijlstra 3751da177e4SLinus Torvalds /* 3761da177e4SLinus Torvalds * If a p?d_bad entry is found while walking page tables, report 3771da177e4SLinus Torvalds * the error, before resetting entry to p?d_none. Usually (but 3781da177e4SLinus Torvalds * very seldom) called out from the p?d_none_or_clear_bad macros. 3791da177e4SLinus Torvalds */ 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds void pgd_clear_bad(pgd_t *pgd) 3821da177e4SLinus Torvalds { 3831da177e4SLinus Torvalds pgd_ERROR(*pgd); 3841da177e4SLinus Torvalds pgd_clear(pgd); 3851da177e4SLinus Torvalds } 3861da177e4SLinus Torvalds 3871da177e4SLinus Torvalds void pud_clear_bad(pud_t *pud) 3881da177e4SLinus Torvalds { 3891da177e4SLinus Torvalds pud_ERROR(*pud); 3901da177e4SLinus Torvalds pud_clear(pud); 3911da177e4SLinus Torvalds } 3921da177e4SLinus Torvalds 3931da177e4SLinus Torvalds void pmd_clear_bad(pmd_t *pmd) 3941da177e4SLinus Torvalds { 3951da177e4SLinus Torvalds pmd_ERROR(*pmd); 3961da177e4SLinus Torvalds pmd_clear(pmd); 3971da177e4SLinus Torvalds } 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds /* 4001da177e4SLinus Torvalds * Note: this doesn't free the actual pages themselves. That 4011da177e4SLinus Torvalds * has been handled earlier when unmapping all the memory regions. 4021da177e4SLinus Torvalds */ 4039e1b32caSBenjamin Herrenschmidt static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd, 4049e1b32caSBenjamin Herrenschmidt unsigned long addr) 4051da177e4SLinus Torvalds { 4062f569afdSMartin Schwidefsky pgtable_t token = pmd_pgtable(*pmd); 4071da177e4SLinus Torvalds pmd_clear(pmd); 4089e1b32caSBenjamin Herrenschmidt pte_free_tlb(tlb, token, addr); 4091da177e4SLinus Torvalds tlb->mm->nr_ptes--; 4101da177e4SLinus Torvalds } 4111da177e4SLinus Torvalds 412e0da382cSHugh Dickins static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud, 413e0da382cSHugh Dickins unsigned long addr, unsigned long end, 414e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 4151da177e4SLinus Torvalds { 4161da177e4SLinus Torvalds pmd_t *pmd; 4171da177e4SLinus Torvalds unsigned long next; 418e0da382cSHugh Dickins unsigned long start; 4191da177e4SLinus Torvalds 420e0da382cSHugh Dickins start = addr; 4211da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 4221da177e4SLinus Torvalds do { 4231da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 4241da177e4SLinus Torvalds if (pmd_none_or_clear_bad(pmd)) 4251da177e4SLinus Torvalds continue; 4269e1b32caSBenjamin Herrenschmidt free_pte_range(tlb, pmd, addr); 4271da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 4281da177e4SLinus Torvalds 429e0da382cSHugh Dickins start &= PUD_MASK; 430e0da382cSHugh Dickins if (start < floor) 431e0da382cSHugh Dickins return; 432e0da382cSHugh Dickins if (ceiling) { 433e0da382cSHugh Dickins ceiling &= PUD_MASK; 434e0da382cSHugh Dickins if (!ceiling) 435e0da382cSHugh Dickins return; 4361da177e4SLinus Torvalds } 437e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 438e0da382cSHugh Dickins return; 439e0da382cSHugh Dickins 440e0da382cSHugh Dickins pmd = pmd_offset(pud, start); 441e0da382cSHugh Dickins pud_clear(pud); 4429e1b32caSBenjamin Herrenschmidt pmd_free_tlb(tlb, pmd, start); 4431da177e4SLinus Torvalds } 4441da177e4SLinus Torvalds 445e0da382cSHugh Dickins static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd, 446e0da382cSHugh Dickins unsigned long addr, unsigned long end, 447e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 4481da177e4SLinus Torvalds { 4491da177e4SLinus Torvalds pud_t *pud; 4501da177e4SLinus Torvalds unsigned long next; 451e0da382cSHugh Dickins unsigned long start; 4521da177e4SLinus Torvalds 453e0da382cSHugh Dickins start = addr; 4541da177e4SLinus Torvalds pud = pud_offset(pgd, addr); 4551da177e4SLinus Torvalds do { 4561da177e4SLinus Torvalds next = pud_addr_end(addr, end); 4571da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 4581da177e4SLinus Torvalds continue; 459e0da382cSHugh Dickins free_pmd_range(tlb, pud, addr, next, floor, ceiling); 4601da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 4611da177e4SLinus Torvalds 462e0da382cSHugh Dickins start &= PGDIR_MASK; 463e0da382cSHugh Dickins if (start < floor) 464e0da382cSHugh Dickins return; 465e0da382cSHugh Dickins if (ceiling) { 466e0da382cSHugh Dickins ceiling &= PGDIR_MASK; 467e0da382cSHugh Dickins if (!ceiling) 468e0da382cSHugh Dickins return; 4691da177e4SLinus Torvalds } 470e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 471e0da382cSHugh Dickins return; 472e0da382cSHugh Dickins 473e0da382cSHugh Dickins pud = pud_offset(pgd, start); 474e0da382cSHugh Dickins pgd_clear(pgd); 4759e1b32caSBenjamin Herrenschmidt pud_free_tlb(tlb, pud, start); 4761da177e4SLinus Torvalds } 4771da177e4SLinus Torvalds 4781da177e4SLinus Torvalds /* 479e0da382cSHugh Dickins * This function frees user-level page tables of a process. 480e0da382cSHugh Dickins * 4811da177e4SLinus Torvalds * Must be called with pagetable lock held. 4821da177e4SLinus Torvalds */ 48342b77728SJan Beulich void free_pgd_range(struct mmu_gather *tlb, 484e0da382cSHugh Dickins unsigned long addr, unsigned long end, 485e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 4861da177e4SLinus Torvalds { 4871da177e4SLinus Torvalds pgd_t *pgd; 4881da177e4SLinus Torvalds unsigned long next; 4891da177e4SLinus Torvalds 490e0da382cSHugh Dickins /* 491e0da382cSHugh Dickins * The next few lines have given us lots of grief... 492e0da382cSHugh Dickins * 493e0da382cSHugh Dickins * Why are we testing PMD* at this top level? Because often 494e0da382cSHugh Dickins * there will be no work to do at all, and we'd prefer not to 495e0da382cSHugh Dickins * go all the way down to the bottom just to discover that. 496e0da382cSHugh Dickins * 497e0da382cSHugh Dickins * Why all these "- 1"s? Because 0 represents both the bottom 498e0da382cSHugh Dickins * of the address space and the top of it (using -1 for the 499e0da382cSHugh Dickins * top wouldn't help much: the masks would do the wrong thing). 500e0da382cSHugh Dickins * The rule is that addr 0 and floor 0 refer to the bottom of 501e0da382cSHugh Dickins * the address space, but end 0 and ceiling 0 refer to the top 502e0da382cSHugh Dickins * Comparisons need to use "end - 1" and "ceiling - 1" (though 503e0da382cSHugh Dickins * that end 0 case should be mythical). 504e0da382cSHugh Dickins * 505e0da382cSHugh Dickins * Wherever addr is brought up or ceiling brought down, we must 506e0da382cSHugh Dickins * be careful to reject "the opposite 0" before it confuses the 507e0da382cSHugh Dickins * subsequent tests. But what about where end is brought down 508e0da382cSHugh Dickins * by PMD_SIZE below? no, end can't go down to 0 there. 509e0da382cSHugh Dickins * 510e0da382cSHugh Dickins * Whereas we round start (addr) and ceiling down, by different 511e0da382cSHugh Dickins * masks at different levels, in order to test whether a table 512e0da382cSHugh Dickins * now has no other vmas using it, so can be freed, we don't 513e0da382cSHugh Dickins * bother to round floor or end up - the tests don't need that. 514e0da382cSHugh Dickins */ 515e0da382cSHugh Dickins 516e0da382cSHugh Dickins addr &= PMD_MASK; 517e0da382cSHugh Dickins if (addr < floor) { 518e0da382cSHugh Dickins addr += PMD_SIZE; 519e0da382cSHugh Dickins if (!addr) 520e0da382cSHugh Dickins return; 521e0da382cSHugh Dickins } 522e0da382cSHugh Dickins if (ceiling) { 523e0da382cSHugh Dickins ceiling &= PMD_MASK; 524e0da382cSHugh Dickins if (!ceiling) 525e0da382cSHugh Dickins return; 526e0da382cSHugh Dickins } 527e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 528e0da382cSHugh Dickins end -= PMD_SIZE; 529e0da382cSHugh Dickins if (addr > end - 1) 530e0da382cSHugh Dickins return; 531e0da382cSHugh Dickins 53242b77728SJan Beulich pgd = pgd_offset(tlb->mm, addr); 5331da177e4SLinus Torvalds do { 5341da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 5351da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 5361da177e4SLinus Torvalds continue; 53742b77728SJan Beulich free_pud_range(tlb, pgd, addr, next, floor, ceiling); 5381da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 539e0da382cSHugh Dickins } 540e0da382cSHugh Dickins 54142b77728SJan Beulich void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma, 542e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 543e0da382cSHugh Dickins { 544e0da382cSHugh Dickins while (vma) { 545e0da382cSHugh Dickins struct vm_area_struct *next = vma->vm_next; 546e0da382cSHugh Dickins unsigned long addr = vma->vm_start; 547e0da382cSHugh Dickins 5488f4f8c16SHugh Dickins /* 54925d9e2d1Snpiggin@suse.de * Hide vma from rmap and truncate_pagecache before freeing 55025d9e2d1Snpiggin@suse.de * pgtables 5518f4f8c16SHugh Dickins */ 5525beb4930SRik van Riel unlink_anon_vmas(vma); 5538f4f8c16SHugh Dickins unlink_file_vma(vma); 5548f4f8c16SHugh Dickins 5559da61aefSDavid Gibson if (is_vm_hugetlb_page(vma)) { 5563bf5ee95SHugh Dickins hugetlb_free_pgd_range(tlb, addr, vma->vm_end, 5573bf5ee95SHugh Dickins floor, next? next->vm_start: ceiling); 5583bf5ee95SHugh Dickins } else { 5593bf5ee95SHugh Dickins /* 5603bf5ee95SHugh Dickins * Optimization: gather nearby vmas into one call down 5613bf5ee95SHugh Dickins */ 5623bf5ee95SHugh Dickins while (next && next->vm_start <= vma->vm_end + PMD_SIZE 5634866920bSDavid Gibson && !is_vm_hugetlb_page(next)) { 564e0da382cSHugh Dickins vma = next; 565e0da382cSHugh Dickins next = vma->vm_next; 5665beb4930SRik van Riel unlink_anon_vmas(vma); 5678f4f8c16SHugh Dickins unlink_file_vma(vma); 568e0da382cSHugh Dickins } 5693bf5ee95SHugh Dickins free_pgd_range(tlb, addr, vma->vm_end, 570e0da382cSHugh Dickins floor, next? next->vm_start: ceiling); 5713bf5ee95SHugh Dickins } 572e0da382cSHugh Dickins vma = next; 573e0da382cSHugh Dickins } 5741da177e4SLinus Torvalds } 5751da177e4SLinus Torvalds 5768ac1f832SAndrea Arcangeli int __pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma, 5778ac1f832SAndrea Arcangeli pmd_t *pmd, unsigned long address) 5781da177e4SLinus Torvalds { 5792f569afdSMartin Schwidefsky pgtable_t new = pte_alloc_one(mm, address); 5808ac1f832SAndrea Arcangeli int wait_split_huge_page; 5811da177e4SLinus Torvalds if (!new) 5821bb3630eSHugh Dickins return -ENOMEM; 5831bb3630eSHugh Dickins 584362a61adSNick Piggin /* 585362a61adSNick Piggin * Ensure all pte setup (eg. pte page lock and page clearing) are 586362a61adSNick Piggin * visible before the pte is made visible to other CPUs by being 587362a61adSNick Piggin * put into page tables. 588362a61adSNick Piggin * 589362a61adSNick Piggin * The other side of the story is the pointer chasing in the page 590362a61adSNick Piggin * table walking code (when walking the page table without locking; 591362a61adSNick Piggin * ie. most of the time). Fortunately, these data accesses consist 592362a61adSNick Piggin * of a chain of data-dependent loads, meaning most CPUs (alpha 593362a61adSNick Piggin * being the notable exception) will already guarantee loads are 594362a61adSNick Piggin * seen in-order. See the alpha page table accessors for the 595362a61adSNick Piggin * smp_read_barrier_depends() barriers in page table walking code. 596362a61adSNick Piggin */ 597362a61adSNick Piggin smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */ 598362a61adSNick Piggin 599c74df32cSHugh Dickins spin_lock(&mm->page_table_lock); 6008ac1f832SAndrea Arcangeli wait_split_huge_page = 0; 6018ac1f832SAndrea Arcangeli if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 6021da177e4SLinus Torvalds mm->nr_ptes++; 6031da177e4SLinus Torvalds pmd_populate(mm, pmd, new); 6042f569afdSMartin Schwidefsky new = NULL; 6058ac1f832SAndrea Arcangeli } else if (unlikely(pmd_trans_splitting(*pmd))) 6068ac1f832SAndrea Arcangeli wait_split_huge_page = 1; 607c74df32cSHugh Dickins spin_unlock(&mm->page_table_lock); 6082f569afdSMartin Schwidefsky if (new) 6092f569afdSMartin Schwidefsky pte_free(mm, new); 6108ac1f832SAndrea Arcangeli if (wait_split_huge_page) 6118ac1f832SAndrea Arcangeli wait_split_huge_page(vma->anon_vma, pmd); 6121bb3630eSHugh Dickins return 0; 6131da177e4SLinus Torvalds } 6141da177e4SLinus Torvalds 6151bb3630eSHugh Dickins int __pte_alloc_kernel(pmd_t *pmd, unsigned long address) 6161da177e4SLinus Torvalds { 6171bb3630eSHugh Dickins pte_t *new = pte_alloc_one_kernel(&init_mm, address); 6181da177e4SLinus Torvalds if (!new) 6191bb3630eSHugh Dickins return -ENOMEM; 6201da177e4SLinus Torvalds 621362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 622362a61adSNick Piggin 623872fec16SHugh Dickins spin_lock(&init_mm.page_table_lock); 6248ac1f832SAndrea Arcangeli if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 625872fec16SHugh Dickins pmd_populate_kernel(&init_mm, pmd, new); 6262f569afdSMartin Schwidefsky new = NULL; 6278ac1f832SAndrea Arcangeli } else 6288ac1f832SAndrea Arcangeli VM_BUG_ON(pmd_trans_splitting(*pmd)); 629872fec16SHugh Dickins spin_unlock(&init_mm.page_table_lock); 6302f569afdSMartin Schwidefsky if (new) 6312f569afdSMartin Schwidefsky pte_free_kernel(&init_mm, new); 6321bb3630eSHugh Dickins return 0; 6331da177e4SLinus Torvalds } 6341da177e4SLinus Torvalds 635d559db08SKAMEZAWA Hiroyuki static inline void init_rss_vec(int *rss) 636ae859762SHugh Dickins { 637d559db08SKAMEZAWA Hiroyuki memset(rss, 0, sizeof(int) * NR_MM_COUNTERS); 638d559db08SKAMEZAWA Hiroyuki } 639d559db08SKAMEZAWA Hiroyuki 640d559db08SKAMEZAWA Hiroyuki static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss) 641d559db08SKAMEZAWA Hiroyuki { 642d559db08SKAMEZAWA Hiroyuki int i; 643d559db08SKAMEZAWA Hiroyuki 64434e55232SKAMEZAWA Hiroyuki if (current->mm == mm) 64505af2e10SDavid Rientjes sync_mm_rss(mm); 646d559db08SKAMEZAWA Hiroyuki for (i = 0; i < NR_MM_COUNTERS; i++) 647d559db08SKAMEZAWA Hiroyuki if (rss[i]) 648d559db08SKAMEZAWA Hiroyuki add_mm_counter(mm, i, rss[i]); 649ae859762SHugh Dickins } 650ae859762SHugh Dickins 6511da177e4SLinus Torvalds /* 6526aab341eSLinus Torvalds * This function is called to print an error when a bad pte 6536aab341eSLinus Torvalds * is found. For example, we might have a PFN-mapped pte in 6546aab341eSLinus Torvalds * a region that doesn't allow it. 655b5810039SNick Piggin * 656b5810039SNick Piggin * The calling function must still handle the error. 657b5810039SNick Piggin */ 6583dc14741SHugh Dickins static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr, 6593dc14741SHugh Dickins pte_t pte, struct page *page) 660b5810039SNick Piggin { 6613dc14741SHugh Dickins pgd_t *pgd = pgd_offset(vma->vm_mm, addr); 6623dc14741SHugh Dickins pud_t *pud = pud_offset(pgd, addr); 6633dc14741SHugh Dickins pmd_t *pmd = pmd_offset(pud, addr); 6643dc14741SHugh Dickins struct address_space *mapping; 6653dc14741SHugh Dickins pgoff_t index; 666d936cf9bSHugh Dickins static unsigned long resume; 667d936cf9bSHugh Dickins static unsigned long nr_shown; 668d936cf9bSHugh Dickins static unsigned long nr_unshown; 669d936cf9bSHugh Dickins 670d936cf9bSHugh Dickins /* 671d936cf9bSHugh Dickins * Allow a burst of 60 reports, then keep quiet for that minute; 672d936cf9bSHugh Dickins * or allow a steady drip of one report per second. 673d936cf9bSHugh Dickins */ 674d936cf9bSHugh Dickins if (nr_shown == 60) { 675d936cf9bSHugh Dickins if (time_before(jiffies, resume)) { 676d936cf9bSHugh Dickins nr_unshown++; 677d936cf9bSHugh Dickins return; 678d936cf9bSHugh Dickins } 679d936cf9bSHugh Dickins if (nr_unshown) { 6801e9e6365SHugh Dickins printk(KERN_ALERT 6811e9e6365SHugh Dickins "BUG: Bad page map: %lu messages suppressed\n", 682d936cf9bSHugh Dickins nr_unshown); 683d936cf9bSHugh Dickins nr_unshown = 0; 684d936cf9bSHugh Dickins } 685d936cf9bSHugh Dickins nr_shown = 0; 686d936cf9bSHugh Dickins } 687d936cf9bSHugh Dickins if (nr_shown++ == 0) 688d936cf9bSHugh Dickins resume = jiffies + 60 * HZ; 6893dc14741SHugh Dickins 6903dc14741SHugh Dickins mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL; 6913dc14741SHugh Dickins index = linear_page_index(vma, addr); 6923dc14741SHugh Dickins 6931e9e6365SHugh Dickins printk(KERN_ALERT 6941e9e6365SHugh Dickins "BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n", 6953dc14741SHugh Dickins current->comm, 6963dc14741SHugh Dickins (long long)pte_val(pte), (long long)pmd_val(*pmd)); 697718a3821SWu Fengguang if (page) 698718a3821SWu Fengguang dump_page(page); 6991e9e6365SHugh Dickins printk(KERN_ALERT 7003dc14741SHugh Dickins "addr:%p vm_flags:%08lx anon_vma:%p mapping:%p index:%lx\n", 7013dc14741SHugh Dickins (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index); 7023dc14741SHugh Dickins /* 7033dc14741SHugh Dickins * Choose text because data symbols depend on CONFIG_KALLSYMS_ALL=y 7043dc14741SHugh Dickins */ 7053dc14741SHugh Dickins if (vma->vm_ops) 7061e9e6365SHugh Dickins print_symbol(KERN_ALERT "vma->vm_ops->fault: %s\n", 7073dc14741SHugh Dickins (unsigned long)vma->vm_ops->fault); 7083dc14741SHugh Dickins if (vma->vm_file && vma->vm_file->f_op) 7091e9e6365SHugh Dickins print_symbol(KERN_ALERT "vma->vm_file->f_op->mmap: %s\n", 7103dc14741SHugh Dickins (unsigned long)vma->vm_file->f_op->mmap); 711b5810039SNick Piggin dump_stack(); 7123dc14741SHugh Dickins add_taint(TAINT_BAD_PAGE); 713b5810039SNick Piggin } 714b5810039SNick Piggin 715ca16d140SKOSAKI Motohiro static inline int is_cow_mapping(vm_flags_t flags) 71667121172SLinus Torvalds { 71767121172SLinus Torvalds return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; 71867121172SLinus Torvalds } 71967121172SLinus Torvalds 72062eede62SHugh Dickins #ifndef is_zero_pfn 72162eede62SHugh Dickins static inline int is_zero_pfn(unsigned long pfn) 72262eede62SHugh Dickins { 72362eede62SHugh Dickins return pfn == zero_pfn; 72462eede62SHugh Dickins } 72562eede62SHugh Dickins #endif 72662eede62SHugh Dickins 72762eede62SHugh Dickins #ifndef my_zero_pfn 72862eede62SHugh Dickins static inline unsigned long my_zero_pfn(unsigned long addr) 72962eede62SHugh Dickins { 73062eede62SHugh Dickins return zero_pfn; 73162eede62SHugh Dickins } 73262eede62SHugh Dickins #endif 73362eede62SHugh Dickins 734b5810039SNick Piggin /* 7357e675137SNick Piggin * vm_normal_page -- This function gets the "struct page" associated with a pte. 7366aab341eSLinus Torvalds * 7377e675137SNick Piggin * "Special" mappings do not wish to be associated with a "struct page" (either 7387e675137SNick Piggin * it doesn't exist, or it exists but they don't want to touch it). In this 7397e675137SNick Piggin * case, NULL is returned here. "Normal" mappings do have a struct page. 740b379d790SJared Hulbert * 7417e675137SNick Piggin * There are 2 broad cases. Firstly, an architecture may define a pte_special() 7427e675137SNick Piggin * pte bit, in which case this function is trivial. Secondly, an architecture 7437e675137SNick Piggin * may not have a spare pte bit, which requires a more complicated scheme, 7447e675137SNick Piggin * described below. 7457e675137SNick Piggin * 7467e675137SNick Piggin * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a 7477e675137SNick Piggin * special mapping (even if there are underlying and valid "struct pages"). 7487e675137SNick Piggin * COWed pages of a VM_PFNMAP are always normal. 7496aab341eSLinus Torvalds * 750b379d790SJared Hulbert * The way we recognize COWed pages within VM_PFNMAP mappings is through the 751b379d790SJared Hulbert * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit 7527e675137SNick Piggin * set, and the vm_pgoff will point to the first PFN mapped: thus every special 7537e675137SNick Piggin * mapping will always honor the rule 7546aab341eSLinus Torvalds * 7556aab341eSLinus Torvalds * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT) 7566aab341eSLinus Torvalds * 7577e675137SNick Piggin * And for normal mappings this is false. 758b379d790SJared Hulbert * 7597e675137SNick Piggin * This restricts such mappings to be a linear translation from virtual address 7607e675137SNick Piggin * to pfn. To get around this restriction, we allow arbitrary mappings so long 7617e675137SNick Piggin * as the vma is not a COW mapping; in that case, we know that all ptes are 7627e675137SNick Piggin * special (because none can have been COWed). 763b379d790SJared Hulbert * 764b379d790SJared Hulbert * 7657e675137SNick Piggin * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP. 7667e675137SNick Piggin * 767b379d790SJared Hulbert * VM_MIXEDMAP mappings can likewise contain memory with or without "struct 768b379d790SJared Hulbert * page" backing, however the difference is that _all_ pages with a struct 769b379d790SJared Hulbert * page (that is, those where pfn_valid is true) are refcounted and considered 770b379d790SJared Hulbert * normal pages by the VM. The disadvantage is that pages are refcounted 771b379d790SJared Hulbert * (which can be slower and simply not an option for some PFNMAP users). The 772b379d790SJared Hulbert * advantage is that we don't have to follow the strict linearity rule of 773b379d790SJared Hulbert * PFNMAP mappings in order to support COWable mappings. 774b379d790SJared Hulbert * 775ee498ed7SHugh Dickins */ 7767e675137SNick Piggin #ifdef __HAVE_ARCH_PTE_SPECIAL 7777e675137SNick Piggin # define HAVE_PTE_SPECIAL 1 7787e675137SNick Piggin #else 7797e675137SNick Piggin # define HAVE_PTE_SPECIAL 0 7807e675137SNick Piggin #endif 7817e675137SNick Piggin struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, 7827e675137SNick Piggin pte_t pte) 783ee498ed7SHugh Dickins { 78422b31eecSHugh Dickins unsigned long pfn = pte_pfn(pte); 7857e675137SNick Piggin 7867e675137SNick Piggin if (HAVE_PTE_SPECIAL) { 78722b31eecSHugh Dickins if (likely(!pte_special(pte))) 78822b31eecSHugh Dickins goto check_pfn; 789a13ea5b7SHugh Dickins if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)) 790a13ea5b7SHugh Dickins return NULL; 79162eede62SHugh Dickins if (!is_zero_pfn(pfn)) 79222b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 7937e675137SNick Piggin return NULL; 7947e675137SNick Piggin } 7957e675137SNick Piggin 7967e675137SNick Piggin /* !HAVE_PTE_SPECIAL case follows: */ 7977e675137SNick Piggin 798b379d790SJared Hulbert if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { 799b379d790SJared Hulbert if (vma->vm_flags & VM_MIXEDMAP) { 800b379d790SJared Hulbert if (!pfn_valid(pfn)) 801b379d790SJared Hulbert return NULL; 802b379d790SJared Hulbert goto out; 803b379d790SJared Hulbert } else { 8047e675137SNick Piggin unsigned long off; 8057e675137SNick Piggin off = (addr - vma->vm_start) >> PAGE_SHIFT; 8066aab341eSLinus Torvalds if (pfn == vma->vm_pgoff + off) 8076aab341eSLinus Torvalds return NULL; 80867121172SLinus Torvalds if (!is_cow_mapping(vma->vm_flags)) 809fb155c16SLinus Torvalds return NULL; 8106aab341eSLinus Torvalds } 811b379d790SJared Hulbert } 8126aab341eSLinus Torvalds 81362eede62SHugh Dickins if (is_zero_pfn(pfn)) 81462eede62SHugh Dickins return NULL; 81522b31eecSHugh Dickins check_pfn: 81622b31eecSHugh Dickins if (unlikely(pfn > highest_memmap_pfn)) { 81722b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 81822b31eecSHugh Dickins return NULL; 81922b31eecSHugh Dickins } 8206aab341eSLinus Torvalds 8216aab341eSLinus Torvalds /* 8227e675137SNick Piggin * NOTE! We still have PageReserved() pages in the page tables. 8237e675137SNick Piggin * eg. VDSO mappings can cause them to exist. 8246aab341eSLinus Torvalds */ 825b379d790SJared Hulbert out: 8266aab341eSLinus Torvalds return pfn_to_page(pfn); 827ee498ed7SHugh Dickins } 828ee498ed7SHugh Dickins 829ee498ed7SHugh Dickins /* 8301da177e4SLinus Torvalds * copy one vm_area from one task to the other. Assumes the page tables 8311da177e4SLinus Torvalds * already present in the new task to be cleared in the whole range 8321da177e4SLinus Torvalds * covered by this vma. 8331da177e4SLinus Torvalds */ 8341da177e4SLinus Torvalds 835570a335bSHugh Dickins static inline unsigned long 8361da177e4SLinus Torvalds copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, 837b5810039SNick Piggin pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma, 8388c103762SHugh Dickins unsigned long addr, int *rss) 8391da177e4SLinus Torvalds { 840b5810039SNick Piggin unsigned long vm_flags = vma->vm_flags; 8411da177e4SLinus Torvalds pte_t pte = *src_pte; 8421da177e4SLinus Torvalds struct page *page; 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds /* pte contains position in swap or file, so copy. */ 8451da177e4SLinus Torvalds if (unlikely(!pte_present(pte))) { 8461da177e4SLinus Torvalds if (!pte_file(pte)) { 8470697212aSChristoph Lameter swp_entry_t entry = pte_to_swp_entry(pte); 8480697212aSChristoph Lameter 849570a335bSHugh Dickins if (swap_duplicate(entry) < 0) 850570a335bSHugh Dickins return entry.val; 851570a335bSHugh Dickins 8521da177e4SLinus Torvalds /* make sure dst_mm is on swapoff's mmlist. */ 8531da177e4SLinus Torvalds if (unlikely(list_empty(&dst_mm->mmlist))) { 8541da177e4SLinus Torvalds spin_lock(&mmlist_lock); 855f412ac08SHugh Dickins if (list_empty(&dst_mm->mmlist)) 856f412ac08SHugh Dickins list_add(&dst_mm->mmlist, 857f412ac08SHugh Dickins &src_mm->mmlist); 8581da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 8591da177e4SLinus Torvalds } 860b084d435SKAMEZAWA Hiroyuki if (likely(!non_swap_entry(entry))) 861b084d435SKAMEZAWA Hiroyuki rss[MM_SWAPENTS]++; 8629f9f1acdSKonstantin Khlebnikov else if (is_migration_entry(entry)) { 8639f9f1acdSKonstantin Khlebnikov page = migration_entry_to_page(entry); 8649f9f1acdSKonstantin Khlebnikov 8659f9f1acdSKonstantin Khlebnikov if (PageAnon(page)) 8669f9f1acdSKonstantin Khlebnikov rss[MM_ANONPAGES]++; 8679f9f1acdSKonstantin Khlebnikov else 8689f9f1acdSKonstantin Khlebnikov rss[MM_FILEPAGES]++; 8699f9f1acdSKonstantin Khlebnikov 8709f9f1acdSKonstantin Khlebnikov if (is_write_migration_entry(entry) && 8710697212aSChristoph Lameter is_cow_mapping(vm_flags)) { 8720697212aSChristoph Lameter /* 8739f9f1acdSKonstantin Khlebnikov * COW mappings require pages in both 8749f9f1acdSKonstantin Khlebnikov * parent and child to be set to read. 8750697212aSChristoph Lameter */ 8760697212aSChristoph Lameter make_migration_entry_read(&entry); 8770697212aSChristoph Lameter pte = swp_entry_to_pte(entry); 8780697212aSChristoph Lameter set_pte_at(src_mm, addr, src_pte, pte); 8790697212aSChristoph Lameter } 8801da177e4SLinus Torvalds } 8819f9f1acdSKonstantin Khlebnikov } 882ae859762SHugh Dickins goto out_set_pte; 8831da177e4SLinus Torvalds } 8841da177e4SLinus Torvalds 8851da177e4SLinus Torvalds /* 8861da177e4SLinus Torvalds * If it's a COW mapping, write protect it both 8871da177e4SLinus Torvalds * in the parent and the child 8881da177e4SLinus Torvalds */ 88967121172SLinus Torvalds if (is_cow_mapping(vm_flags)) { 8901da177e4SLinus Torvalds ptep_set_wrprotect(src_mm, addr, src_pte); 8913dc90795SZachary Amsden pte = pte_wrprotect(pte); 8921da177e4SLinus Torvalds } 8931da177e4SLinus Torvalds 8941da177e4SLinus Torvalds /* 8951da177e4SLinus Torvalds * If it's a shared mapping, mark it clean in 8961da177e4SLinus Torvalds * the child 8971da177e4SLinus Torvalds */ 8981da177e4SLinus Torvalds if (vm_flags & VM_SHARED) 8991da177e4SLinus Torvalds pte = pte_mkclean(pte); 9001da177e4SLinus Torvalds pte = pte_mkold(pte); 9016aab341eSLinus Torvalds 9026aab341eSLinus Torvalds page = vm_normal_page(vma, addr, pte); 9036aab341eSLinus Torvalds if (page) { 9041da177e4SLinus Torvalds get_page(page); 90521333b2bSHugh Dickins page_dup_rmap(page); 906d559db08SKAMEZAWA Hiroyuki if (PageAnon(page)) 907d559db08SKAMEZAWA Hiroyuki rss[MM_ANONPAGES]++; 908d559db08SKAMEZAWA Hiroyuki else 909d559db08SKAMEZAWA Hiroyuki rss[MM_FILEPAGES]++; 9106aab341eSLinus Torvalds } 911ae859762SHugh Dickins 912ae859762SHugh Dickins out_set_pte: 913ae859762SHugh Dickins set_pte_at(dst_mm, addr, dst_pte, pte); 914570a335bSHugh Dickins return 0; 9151da177e4SLinus Torvalds } 9161da177e4SLinus Torvalds 91771e3aac0SAndrea Arcangeli int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 9181da177e4SLinus Torvalds pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma, 9191da177e4SLinus Torvalds unsigned long addr, unsigned long end) 9201da177e4SLinus Torvalds { 921c36987e2SDaisuke Nishimura pte_t *orig_src_pte, *orig_dst_pte; 9221da177e4SLinus Torvalds pte_t *src_pte, *dst_pte; 923c74df32cSHugh Dickins spinlock_t *src_ptl, *dst_ptl; 924e040f218SHugh Dickins int progress = 0; 925d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 926570a335bSHugh Dickins swp_entry_t entry = (swp_entry_t){0}; 9271da177e4SLinus Torvalds 9281da177e4SLinus Torvalds again: 929d559db08SKAMEZAWA Hiroyuki init_rss_vec(rss); 930d559db08SKAMEZAWA Hiroyuki 931c74df32cSHugh Dickins dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl); 9321da177e4SLinus Torvalds if (!dst_pte) 9331da177e4SLinus Torvalds return -ENOMEM; 934ece0e2b6SPeter Zijlstra src_pte = pte_offset_map(src_pmd, addr); 9354c21e2f2SHugh Dickins src_ptl = pte_lockptr(src_mm, src_pmd); 936f20dc5f7SIngo Molnar spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 937c36987e2SDaisuke Nishimura orig_src_pte = src_pte; 938c36987e2SDaisuke Nishimura orig_dst_pte = dst_pte; 9396606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 9401da177e4SLinus Torvalds 9411da177e4SLinus Torvalds do { 9421da177e4SLinus Torvalds /* 9431da177e4SLinus Torvalds * We are holding two locks at this point - either of them 9441da177e4SLinus Torvalds * could generate latencies in another task on another CPU. 9451da177e4SLinus Torvalds */ 946e040f218SHugh Dickins if (progress >= 32) { 947e040f218SHugh Dickins progress = 0; 948e040f218SHugh Dickins if (need_resched() || 94995c354feSNick Piggin spin_needbreak(src_ptl) || spin_needbreak(dst_ptl)) 9501da177e4SLinus Torvalds break; 951e040f218SHugh Dickins } 9521da177e4SLinus Torvalds if (pte_none(*src_pte)) { 9531da177e4SLinus Torvalds progress++; 9541da177e4SLinus Torvalds continue; 9551da177e4SLinus Torvalds } 956570a335bSHugh Dickins entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte, 957570a335bSHugh Dickins vma, addr, rss); 958570a335bSHugh Dickins if (entry.val) 959570a335bSHugh Dickins break; 9601da177e4SLinus Torvalds progress += 8; 9611da177e4SLinus Torvalds } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end); 9621da177e4SLinus Torvalds 9636606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 964c74df32cSHugh Dickins spin_unlock(src_ptl); 965ece0e2b6SPeter Zijlstra pte_unmap(orig_src_pte); 966d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(dst_mm, rss); 967c36987e2SDaisuke Nishimura pte_unmap_unlock(orig_dst_pte, dst_ptl); 968c74df32cSHugh Dickins cond_resched(); 969570a335bSHugh Dickins 970570a335bSHugh Dickins if (entry.val) { 971570a335bSHugh Dickins if (add_swap_count_continuation(entry, GFP_KERNEL) < 0) 972570a335bSHugh Dickins return -ENOMEM; 973570a335bSHugh Dickins progress = 0; 974570a335bSHugh Dickins } 9751da177e4SLinus Torvalds if (addr != end) 9761da177e4SLinus Torvalds goto again; 9771da177e4SLinus Torvalds return 0; 9781da177e4SLinus Torvalds } 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 9811da177e4SLinus Torvalds pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma, 9821da177e4SLinus Torvalds unsigned long addr, unsigned long end) 9831da177e4SLinus Torvalds { 9841da177e4SLinus Torvalds pmd_t *src_pmd, *dst_pmd; 9851da177e4SLinus Torvalds unsigned long next; 9861da177e4SLinus Torvalds 9871da177e4SLinus Torvalds dst_pmd = pmd_alloc(dst_mm, dst_pud, addr); 9881da177e4SLinus Torvalds if (!dst_pmd) 9891da177e4SLinus Torvalds return -ENOMEM; 9901da177e4SLinus Torvalds src_pmd = pmd_offset(src_pud, addr); 9911da177e4SLinus Torvalds do { 9921da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 99371e3aac0SAndrea Arcangeli if (pmd_trans_huge(*src_pmd)) { 99471e3aac0SAndrea Arcangeli int err; 99514d1a55cSAndrea Arcangeli VM_BUG_ON(next-addr != HPAGE_PMD_SIZE); 99671e3aac0SAndrea Arcangeli err = copy_huge_pmd(dst_mm, src_mm, 99771e3aac0SAndrea Arcangeli dst_pmd, src_pmd, addr, vma); 99871e3aac0SAndrea Arcangeli if (err == -ENOMEM) 99971e3aac0SAndrea Arcangeli return -ENOMEM; 100071e3aac0SAndrea Arcangeli if (!err) 100171e3aac0SAndrea Arcangeli continue; 100271e3aac0SAndrea Arcangeli /* fall through */ 100371e3aac0SAndrea Arcangeli } 10041da177e4SLinus Torvalds if (pmd_none_or_clear_bad(src_pmd)) 10051da177e4SLinus Torvalds continue; 10061da177e4SLinus Torvalds if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd, 10071da177e4SLinus Torvalds vma, addr, next)) 10081da177e4SLinus Torvalds return -ENOMEM; 10091da177e4SLinus Torvalds } while (dst_pmd++, src_pmd++, addr = next, addr != end); 10101da177e4SLinus Torvalds return 0; 10111da177e4SLinus Torvalds } 10121da177e4SLinus Torvalds 10131da177e4SLinus Torvalds static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 10141da177e4SLinus Torvalds pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma, 10151da177e4SLinus Torvalds unsigned long addr, unsigned long end) 10161da177e4SLinus Torvalds { 10171da177e4SLinus Torvalds pud_t *src_pud, *dst_pud; 10181da177e4SLinus Torvalds unsigned long next; 10191da177e4SLinus Torvalds 10201da177e4SLinus Torvalds dst_pud = pud_alloc(dst_mm, dst_pgd, addr); 10211da177e4SLinus Torvalds if (!dst_pud) 10221da177e4SLinus Torvalds return -ENOMEM; 10231da177e4SLinus Torvalds src_pud = pud_offset(src_pgd, addr); 10241da177e4SLinus Torvalds do { 10251da177e4SLinus Torvalds next = pud_addr_end(addr, end); 10261da177e4SLinus Torvalds if (pud_none_or_clear_bad(src_pud)) 10271da177e4SLinus Torvalds continue; 10281da177e4SLinus Torvalds if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud, 10291da177e4SLinus Torvalds vma, addr, next)) 10301da177e4SLinus Torvalds return -ENOMEM; 10311da177e4SLinus Torvalds } while (dst_pud++, src_pud++, addr = next, addr != end); 10321da177e4SLinus Torvalds return 0; 10331da177e4SLinus Torvalds } 10341da177e4SLinus Torvalds 10351da177e4SLinus Torvalds int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 10361da177e4SLinus Torvalds struct vm_area_struct *vma) 10371da177e4SLinus Torvalds { 10381da177e4SLinus Torvalds pgd_t *src_pgd, *dst_pgd; 10391da177e4SLinus Torvalds unsigned long next; 10401da177e4SLinus Torvalds unsigned long addr = vma->vm_start; 10411da177e4SLinus Torvalds unsigned long end = vma->vm_end; 1042cddb8a5cSAndrea Arcangeli int ret; 10431da177e4SLinus Torvalds 1044d992895bSNick Piggin /* 1045d992895bSNick Piggin * Don't copy ptes where a page fault will fill them correctly. 1046d992895bSNick Piggin * Fork becomes much lighter when there are big shared or private 1047d992895bSNick Piggin * readonly mappings. The tradeoff is that copy_page_range is more 1048d992895bSNick Piggin * efficient than faulting. 1049d992895bSNick Piggin */ 1050*4b6e1e37SKonstantin Khlebnikov if (!(vma->vm_flags & (VM_HUGETLB | VM_NONLINEAR | 1051*4b6e1e37SKonstantin Khlebnikov VM_PFNMAP | VM_MIXEDMAP))) { 1052d992895bSNick Piggin if (!vma->anon_vma) 1053d992895bSNick Piggin return 0; 1054d992895bSNick Piggin } 1055d992895bSNick Piggin 10561da177e4SLinus Torvalds if (is_vm_hugetlb_page(vma)) 10571da177e4SLinus Torvalds return copy_hugetlb_page_range(dst_mm, src_mm, vma); 10581da177e4SLinus Torvalds 1059b3b9c293SKonstantin Khlebnikov if (unlikely(vma->vm_flags & VM_PFNMAP)) { 10602ab64037Svenkatesh.pallipadi@intel.com /* 10612ab64037Svenkatesh.pallipadi@intel.com * We do not free on error cases below as remove_vma 10622ab64037Svenkatesh.pallipadi@intel.com * gets called on error from higher level routine 10632ab64037Svenkatesh.pallipadi@intel.com */ 10645180da41SSuresh Siddha ret = track_pfn_copy(vma); 10652ab64037Svenkatesh.pallipadi@intel.com if (ret) 10662ab64037Svenkatesh.pallipadi@intel.com return ret; 10672ab64037Svenkatesh.pallipadi@intel.com } 10682ab64037Svenkatesh.pallipadi@intel.com 1069cddb8a5cSAndrea Arcangeli /* 1070cddb8a5cSAndrea Arcangeli * We need to invalidate the secondary MMU mappings only when 1071cddb8a5cSAndrea Arcangeli * there could be a permission downgrade on the ptes of the 1072cddb8a5cSAndrea Arcangeli * parent mm. And a permission downgrade will only happen if 1073cddb8a5cSAndrea Arcangeli * is_cow_mapping() returns true. 1074cddb8a5cSAndrea Arcangeli */ 1075cddb8a5cSAndrea Arcangeli if (is_cow_mapping(vma->vm_flags)) 1076cddb8a5cSAndrea Arcangeli mmu_notifier_invalidate_range_start(src_mm, addr, end); 1077cddb8a5cSAndrea Arcangeli 1078cddb8a5cSAndrea Arcangeli ret = 0; 10791da177e4SLinus Torvalds dst_pgd = pgd_offset(dst_mm, addr); 10801da177e4SLinus Torvalds src_pgd = pgd_offset(src_mm, addr); 10811da177e4SLinus Torvalds do { 10821da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 10831da177e4SLinus Torvalds if (pgd_none_or_clear_bad(src_pgd)) 10841da177e4SLinus Torvalds continue; 1085cddb8a5cSAndrea Arcangeli if (unlikely(copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd, 1086cddb8a5cSAndrea Arcangeli vma, addr, next))) { 1087cddb8a5cSAndrea Arcangeli ret = -ENOMEM; 1088cddb8a5cSAndrea Arcangeli break; 1089cddb8a5cSAndrea Arcangeli } 10901da177e4SLinus Torvalds } while (dst_pgd++, src_pgd++, addr = next, addr != end); 1091cddb8a5cSAndrea Arcangeli 1092cddb8a5cSAndrea Arcangeli if (is_cow_mapping(vma->vm_flags)) 1093cddb8a5cSAndrea Arcangeli mmu_notifier_invalidate_range_end(src_mm, 1094cddb8a5cSAndrea Arcangeli vma->vm_start, end); 1095cddb8a5cSAndrea Arcangeli return ret; 10961da177e4SLinus Torvalds } 10971da177e4SLinus Torvalds 109851c6f666SRobin Holt static unsigned long zap_pte_range(struct mmu_gather *tlb, 1099b5810039SNick Piggin struct vm_area_struct *vma, pmd_t *pmd, 11001da177e4SLinus Torvalds unsigned long addr, unsigned long end, 110197a89413SPeter Zijlstra struct zap_details *details) 11021da177e4SLinus Torvalds { 1103b5810039SNick Piggin struct mm_struct *mm = tlb->mm; 1104d16dfc55SPeter Zijlstra int force_flush = 0; 1105d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 110697a89413SPeter Zijlstra spinlock_t *ptl; 11075f1a1907SSteven Rostedt pte_t *start_pte; 110897a89413SPeter Zijlstra pte_t *pte; 1109d559db08SKAMEZAWA Hiroyuki 1110d16dfc55SPeter Zijlstra again: 1111e303297eSPeter Zijlstra init_rss_vec(rss); 11125f1a1907SSteven Rostedt start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl); 11135f1a1907SSteven Rostedt pte = start_pte; 11146606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 11151da177e4SLinus Torvalds do { 11161da177e4SLinus Torvalds pte_t ptent = *pte; 111751c6f666SRobin Holt if (pte_none(ptent)) { 11181da177e4SLinus Torvalds continue; 111951c6f666SRobin Holt } 112051c6f666SRobin Holt 11216f5e6b9eSHugh Dickins if (pte_present(ptent)) { 11226f5e6b9eSHugh Dickins struct page *page; 11236f5e6b9eSHugh Dickins 11246aab341eSLinus Torvalds page = vm_normal_page(vma, addr, ptent); 11251da177e4SLinus Torvalds if (unlikely(details) && page) { 11261da177e4SLinus Torvalds /* 11271da177e4SLinus Torvalds * unmap_shared_mapping_pages() wants to 11281da177e4SLinus Torvalds * invalidate cache without truncating: 11291da177e4SLinus Torvalds * unmap shared but keep private pages. 11301da177e4SLinus Torvalds */ 11311da177e4SLinus Torvalds if (details->check_mapping && 11321da177e4SLinus Torvalds details->check_mapping != page->mapping) 11331da177e4SLinus Torvalds continue; 11341da177e4SLinus Torvalds /* 11351da177e4SLinus Torvalds * Each page->index must be checked when 11361da177e4SLinus Torvalds * invalidating or truncating nonlinear. 11371da177e4SLinus Torvalds */ 11381da177e4SLinus Torvalds if (details->nonlinear_vma && 11391da177e4SLinus Torvalds (page->index < details->first_index || 11401da177e4SLinus Torvalds page->index > details->last_index)) 11411da177e4SLinus Torvalds continue; 11421da177e4SLinus Torvalds } 1143b5810039SNick Piggin ptent = ptep_get_and_clear_full(mm, addr, pte, 1144a600388dSZachary Amsden tlb->fullmm); 11451da177e4SLinus Torvalds tlb_remove_tlb_entry(tlb, pte, addr); 11461da177e4SLinus Torvalds if (unlikely(!page)) 11471da177e4SLinus Torvalds continue; 11481da177e4SLinus Torvalds if (unlikely(details) && details->nonlinear_vma 11491da177e4SLinus Torvalds && linear_page_index(details->nonlinear_vma, 11501da177e4SLinus Torvalds addr) != page->index) 1151b5810039SNick Piggin set_pte_at(mm, addr, pte, 11521da177e4SLinus Torvalds pgoff_to_pte(page->index)); 11531da177e4SLinus Torvalds if (PageAnon(page)) 1154d559db08SKAMEZAWA Hiroyuki rss[MM_ANONPAGES]--; 11556237bcd9SHugh Dickins else { 11566237bcd9SHugh Dickins if (pte_dirty(ptent)) 11576237bcd9SHugh Dickins set_page_dirty(page); 11584917e5d0SJohannes Weiner if (pte_young(ptent) && 11594917e5d0SJohannes Weiner likely(!VM_SequentialReadHint(vma))) 1160bf3f3bc5SNick Piggin mark_page_accessed(page); 1161d559db08SKAMEZAWA Hiroyuki rss[MM_FILEPAGES]--; 11626237bcd9SHugh Dickins } 1163edc315fdSHugh Dickins page_remove_rmap(page); 11643dc14741SHugh Dickins if (unlikely(page_mapcount(page) < 0)) 11653dc14741SHugh Dickins print_bad_pte(vma, addr, ptent, page); 1166d16dfc55SPeter Zijlstra force_flush = !__tlb_remove_page(tlb, page); 1167d16dfc55SPeter Zijlstra if (force_flush) 1168d16dfc55SPeter Zijlstra break; 11691da177e4SLinus Torvalds continue; 11701da177e4SLinus Torvalds } 11711da177e4SLinus Torvalds /* 11721da177e4SLinus Torvalds * If details->check_mapping, we leave swap entries; 11731da177e4SLinus Torvalds * if details->nonlinear_vma, we leave file entries. 11741da177e4SLinus Torvalds */ 11751da177e4SLinus Torvalds if (unlikely(details)) 11761da177e4SLinus Torvalds continue; 11772509ef26SHugh Dickins if (pte_file(ptent)) { 11782509ef26SHugh Dickins if (unlikely(!(vma->vm_flags & VM_NONLINEAR))) 11792509ef26SHugh Dickins print_bad_pte(vma, addr, ptent, NULL); 1180b084d435SKAMEZAWA Hiroyuki } else { 1181b084d435SKAMEZAWA Hiroyuki swp_entry_t entry = pte_to_swp_entry(ptent); 1182b084d435SKAMEZAWA Hiroyuki 1183b084d435SKAMEZAWA Hiroyuki if (!non_swap_entry(entry)) 1184b084d435SKAMEZAWA Hiroyuki rss[MM_SWAPENTS]--; 11859f9f1acdSKonstantin Khlebnikov else if (is_migration_entry(entry)) { 11869f9f1acdSKonstantin Khlebnikov struct page *page; 11879f9f1acdSKonstantin Khlebnikov 11889f9f1acdSKonstantin Khlebnikov page = migration_entry_to_page(entry); 11899f9f1acdSKonstantin Khlebnikov 11909f9f1acdSKonstantin Khlebnikov if (PageAnon(page)) 11919f9f1acdSKonstantin Khlebnikov rss[MM_ANONPAGES]--; 11929f9f1acdSKonstantin Khlebnikov else 11939f9f1acdSKonstantin Khlebnikov rss[MM_FILEPAGES]--; 11949f9f1acdSKonstantin Khlebnikov } 1195b084d435SKAMEZAWA Hiroyuki if (unlikely(!free_swap_and_cache(entry))) 11962509ef26SHugh Dickins print_bad_pte(vma, addr, ptent, NULL); 1197b084d435SKAMEZAWA Hiroyuki } 11989888a1caSZachary Amsden pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); 119997a89413SPeter Zijlstra } while (pte++, addr += PAGE_SIZE, addr != end); 1200ae859762SHugh Dickins 1201d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(mm, rss); 12026606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 12035f1a1907SSteven Rostedt pte_unmap_unlock(start_pte, ptl); 120451c6f666SRobin Holt 1205d16dfc55SPeter Zijlstra /* 1206d16dfc55SPeter Zijlstra * mmu_gather ran out of room to batch pages, we break out of 1207d16dfc55SPeter Zijlstra * the PTE lock to avoid doing the potential expensive TLB invalidate 1208d16dfc55SPeter Zijlstra * and page-free while holding it. 1209d16dfc55SPeter Zijlstra */ 1210d16dfc55SPeter Zijlstra if (force_flush) { 1211d16dfc55SPeter Zijlstra force_flush = 0; 1212597e1c35SAlex Shi 1213597e1c35SAlex Shi #ifdef HAVE_GENERIC_MMU_GATHER 1214597e1c35SAlex Shi tlb->start = addr; 1215597e1c35SAlex Shi tlb->end = end; 1216597e1c35SAlex Shi #endif 1217d16dfc55SPeter Zijlstra tlb_flush_mmu(tlb); 1218d16dfc55SPeter Zijlstra if (addr != end) 1219d16dfc55SPeter Zijlstra goto again; 1220d16dfc55SPeter Zijlstra } 1221d16dfc55SPeter Zijlstra 122251c6f666SRobin Holt return addr; 12231da177e4SLinus Torvalds } 12241da177e4SLinus Torvalds 122551c6f666SRobin Holt static inline unsigned long zap_pmd_range(struct mmu_gather *tlb, 1226b5810039SNick Piggin struct vm_area_struct *vma, pud_t *pud, 12271da177e4SLinus Torvalds unsigned long addr, unsigned long end, 122897a89413SPeter Zijlstra struct zap_details *details) 12291da177e4SLinus Torvalds { 12301da177e4SLinus Torvalds pmd_t *pmd; 12311da177e4SLinus Torvalds unsigned long next; 12321da177e4SLinus Torvalds 12331da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 12341da177e4SLinus Torvalds do { 12351da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 123671e3aac0SAndrea Arcangeli if (pmd_trans_huge(*pmd)) { 123714d1a55cSAndrea Arcangeli if (next - addr != HPAGE_PMD_SIZE) { 1238e0897d75SDavid Rientjes #ifdef CONFIG_DEBUG_VM 1239e0897d75SDavid Rientjes if (!rwsem_is_locked(&tlb->mm->mmap_sem)) { 1240e0897d75SDavid Rientjes pr_err("%s: mmap_sem is unlocked! addr=0x%lx end=0x%lx vma->vm_start=0x%lx vma->vm_end=0x%lx\n", 1241e0897d75SDavid Rientjes __func__, addr, end, 1242e0897d75SDavid Rientjes vma->vm_start, 1243e0897d75SDavid Rientjes vma->vm_end); 1244e0897d75SDavid Rientjes BUG(); 1245e0897d75SDavid Rientjes } 1246e0897d75SDavid Rientjes #endif 124771e3aac0SAndrea Arcangeli split_huge_page_pmd(vma->vm_mm, pmd); 1248f21760b1SShaohua Li } else if (zap_huge_pmd(tlb, vma, pmd, addr)) 12491a5a9906SAndrea Arcangeli goto next; 125071e3aac0SAndrea Arcangeli /* fall through */ 125171e3aac0SAndrea Arcangeli } 12521a5a9906SAndrea Arcangeli /* 12531a5a9906SAndrea Arcangeli * Here there can be other concurrent MADV_DONTNEED or 12541a5a9906SAndrea Arcangeli * trans huge page faults running, and if the pmd is 12551a5a9906SAndrea Arcangeli * none or trans huge it can change under us. This is 12561a5a9906SAndrea Arcangeli * because MADV_DONTNEED holds the mmap_sem in read 12571a5a9906SAndrea Arcangeli * mode. 12581a5a9906SAndrea Arcangeli */ 12591a5a9906SAndrea Arcangeli if (pmd_none_or_trans_huge_or_clear_bad(pmd)) 12601a5a9906SAndrea Arcangeli goto next; 126197a89413SPeter Zijlstra next = zap_pte_range(tlb, vma, pmd, addr, next, details); 12621a5a9906SAndrea Arcangeli next: 126397a89413SPeter Zijlstra cond_resched(); 126497a89413SPeter Zijlstra } while (pmd++, addr = next, addr != end); 126551c6f666SRobin Holt 126651c6f666SRobin Holt return addr; 12671da177e4SLinus Torvalds } 12681da177e4SLinus Torvalds 126951c6f666SRobin Holt static inline unsigned long zap_pud_range(struct mmu_gather *tlb, 1270b5810039SNick Piggin struct vm_area_struct *vma, pgd_t *pgd, 12711da177e4SLinus Torvalds unsigned long addr, unsigned long end, 127297a89413SPeter Zijlstra struct zap_details *details) 12731da177e4SLinus Torvalds { 12741da177e4SLinus Torvalds pud_t *pud; 12751da177e4SLinus Torvalds unsigned long next; 12761da177e4SLinus Torvalds 12771da177e4SLinus Torvalds pud = pud_offset(pgd, addr); 12781da177e4SLinus Torvalds do { 12791da177e4SLinus Torvalds next = pud_addr_end(addr, end); 128097a89413SPeter Zijlstra if (pud_none_or_clear_bad(pud)) 12811da177e4SLinus Torvalds continue; 128297a89413SPeter Zijlstra next = zap_pmd_range(tlb, vma, pud, addr, next, details); 128397a89413SPeter Zijlstra } while (pud++, addr = next, addr != end); 128451c6f666SRobin Holt 128551c6f666SRobin Holt return addr; 12861da177e4SLinus Torvalds } 12871da177e4SLinus Torvalds 1288038c7aa1SAl Viro static void unmap_page_range(struct mmu_gather *tlb, 128951c6f666SRobin Holt struct vm_area_struct *vma, 12901da177e4SLinus Torvalds unsigned long addr, unsigned long end, 129197a89413SPeter Zijlstra struct zap_details *details) 12921da177e4SLinus Torvalds { 12931da177e4SLinus Torvalds pgd_t *pgd; 12941da177e4SLinus Torvalds unsigned long next; 12951da177e4SLinus Torvalds 12961da177e4SLinus Torvalds if (details && !details->check_mapping && !details->nonlinear_vma) 12971da177e4SLinus Torvalds details = NULL; 12981da177e4SLinus Torvalds 12991da177e4SLinus Torvalds BUG_ON(addr >= end); 1300569b846dSKAMEZAWA Hiroyuki mem_cgroup_uncharge_start(); 13011da177e4SLinus Torvalds tlb_start_vma(tlb, vma); 13021da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 13031da177e4SLinus Torvalds do { 13041da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 130597a89413SPeter Zijlstra if (pgd_none_or_clear_bad(pgd)) 13061da177e4SLinus Torvalds continue; 130797a89413SPeter Zijlstra next = zap_pud_range(tlb, vma, pgd, addr, next, details); 130897a89413SPeter Zijlstra } while (pgd++, addr = next, addr != end); 13091da177e4SLinus Torvalds tlb_end_vma(tlb, vma); 1310569b846dSKAMEZAWA Hiroyuki mem_cgroup_uncharge_end(); 13111da177e4SLinus Torvalds } 13121da177e4SLinus Torvalds 1313f5cc4eefSAl Viro 1314f5cc4eefSAl Viro static void unmap_single_vma(struct mmu_gather *tlb, 13151da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long start_addr, 13164f74d2c8SLinus Torvalds unsigned long end_addr, 13171da177e4SLinus Torvalds struct zap_details *details) 13181da177e4SLinus Torvalds { 1319f5cc4eefSAl Viro unsigned long start = max(vma->vm_start, start_addr); 13201da177e4SLinus Torvalds unsigned long end; 13211da177e4SLinus Torvalds 13221da177e4SLinus Torvalds if (start >= vma->vm_end) 1323f5cc4eefSAl Viro return; 13241da177e4SLinus Torvalds end = min(vma->vm_end, end_addr); 13251da177e4SLinus Torvalds if (end <= vma->vm_start) 1326f5cc4eefSAl Viro return; 13271da177e4SLinus Torvalds 1328cbc91f71SSrikar Dronamraju if (vma->vm_file) 1329cbc91f71SSrikar Dronamraju uprobe_munmap(vma, start, end); 1330cbc91f71SSrikar Dronamraju 1331b3b9c293SKonstantin Khlebnikov if (unlikely(vma->vm_flags & VM_PFNMAP)) 13325180da41SSuresh Siddha untrack_pfn(vma, 0, 0); 13332ab64037Svenkatesh.pallipadi@intel.com 13348b2a1238SAl Viro if (start != end) { 133551c6f666SRobin Holt if (unlikely(is_vm_hugetlb_page(vma))) { 1336a137e1ccSAndi Kleen /* 1337a137e1ccSAndi Kleen * It is undesirable to test vma->vm_file as it 1338a137e1ccSAndi Kleen * should be non-null for valid hugetlb area. 1339a137e1ccSAndi Kleen * However, vm_file will be NULL in the error 1340a137e1ccSAndi Kleen * cleanup path of do_mmap_pgoff. When 1341a137e1ccSAndi Kleen * hugetlbfs ->mmap method fails, 1342a137e1ccSAndi Kleen * do_mmap_pgoff() nullifies vma->vm_file 1343a137e1ccSAndi Kleen * before calling this function to clean up. 1344a137e1ccSAndi Kleen * Since no pte has actually been setup, it is 1345a137e1ccSAndi Kleen * safe to do nothing in this case. 1346a137e1ccSAndi Kleen */ 134724669e58SAneesh Kumar K.V if (vma->vm_file) { 134824669e58SAneesh Kumar K.V mutex_lock(&vma->vm_file->f_mapping->i_mmap_mutex); 1349d833352aSMel Gorman __unmap_hugepage_range_final(tlb, vma, start, end, NULL); 135024669e58SAneesh Kumar K.V mutex_unlock(&vma->vm_file->f_mapping->i_mmap_mutex); 135124669e58SAneesh Kumar K.V } 135251c6f666SRobin Holt } else 1353038c7aa1SAl Viro unmap_page_range(tlb, vma, start, end, details); 135497a89413SPeter Zijlstra } 135551c6f666SRobin Holt } 13561da177e4SLinus Torvalds 1357f5cc4eefSAl Viro /** 1358f5cc4eefSAl Viro * unmap_vmas - unmap a range of memory covered by a list of vma's 1359f5cc4eefSAl Viro * @tlb: address of the caller's struct mmu_gather 1360f5cc4eefSAl Viro * @vma: the starting vma 1361f5cc4eefSAl Viro * @start_addr: virtual address at which to start unmapping 1362f5cc4eefSAl Viro * @end_addr: virtual address at which to end unmapping 1363f5cc4eefSAl Viro * 1364f5cc4eefSAl Viro * Unmap all pages in the vma list. 1365f5cc4eefSAl Viro * 1366f5cc4eefSAl Viro * Only addresses between `start' and `end' will be unmapped. 1367f5cc4eefSAl Viro * 1368f5cc4eefSAl Viro * The VMA list must be sorted in ascending virtual address order. 1369f5cc4eefSAl Viro * 1370f5cc4eefSAl Viro * unmap_vmas() assumes that the caller will flush the whole unmapped address 1371f5cc4eefSAl Viro * range after unmap_vmas() returns. So the only responsibility here is to 1372f5cc4eefSAl Viro * ensure that any thus-far unmapped pages are flushed before unmap_vmas() 1373f5cc4eefSAl Viro * drops the lock and schedules. 1374f5cc4eefSAl Viro */ 1375f5cc4eefSAl Viro void unmap_vmas(struct mmu_gather *tlb, 1376f5cc4eefSAl Viro struct vm_area_struct *vma, unsigned long start_addr, 13774f74d2c8SLinus Torvalds unsigned long end_addr) 1378f5cc4eefSAl Viro { 1379f5cc4eefSAl Viro struct mm_struct *mm = vma->vm_mm; 1380f5cc4eefSAl Viro 1381f5cc4eefSAl Viro mmu_notifier_invalidate_range_start(mm, start_addr, end_addr); 1382f5cc4eefSAl Viro for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) 13834f74d2c8SLinus Torvalds unmap_single_vma(tlb, vma, start_addr, end_addr, NULL); 1384cddb8a5cSAndrea Arcangeli mmu_notifier_invalidate_range_end(mm, start_addr, end_addr); 13851da177e4SLinus Torvalds } 13861da177e4SLinus Torvalds 13871da177e4SLinus Torvalds /** 13881da177e4SLinus Torvalds * zap_page_range - remove user pages in a given range 13891da177e4SLinus Torvalds * @vma: vm_area_struct holding the applicable pages 1390eb4546bbSRandy Dunlap * @start: starting address of pages to zap 13911da177e4SLinus Torvalds * @size: number of bytes to zap 13921da177e4SLinus Torvalds * @details: details of nonlinear truncation or shared cache invalidation 1393f5cc4eefSAl Viro * 1394f5cc4eefSAl Viro * Caller must protect the VMA list 13951da177e4SLinus Torvalds */ 13967e027b14SLinus Torvalds void zap_page_range(struct vm_area_struct *vma, unsigned long start, 13971da177e4SLinus Torvalds unsigned long size, struct zap_details *details) 13981da177e4SLinus Torvalds { 13991da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 1400d16dfc55SPeter Zijlstra struct mmu_gather tlb; 14017e027b14SLinus Torvalds unsigned long end = start + size; 14021da177e4SLinus Torvalds 14031da177e4SLinus Torvalds lru_add_drain(); 1404d16dfc55SPeter Zijlstra tlb_gather_mmu(&tlb, mm, 0); 1405365e9c87SHugh Dickins update_hiwater_rss(mm); 14067e027b14SLinus Torvalds mmu_notifier_invalidate_range_start(mm, start, end); 14077e027b14SLinus Torvalds for ( ; vma && vma->vm_start < end; vma = vma->vm_next) 14084f74d2c8SLinus Torvalds unmap_single_vma(&tlb, vma, start, end, details); 14097e027b14SLinus Torvalds mmu_notifier_invalidate_range_end(mm, start, end); 14107e027b14SLinus Torvalds tlb_finish_mmu(&tlb, start, end); 14111da177e4SLinus Torvalds } 14121da177e4SLinus Torvalds 1413c627f9ccSJack Steiner /** 1414f5cc4eefSAl Viro * zap_page_range_single - remove user pages in a given range 1415f5cc4eefSAl Viro * @vma: vm_area_struct holding the applicable pages 1416f5cc4eefSAl Viro * @address: starting address of pages to zap 1417f5cc4eefSAl Viro * @size: number of bytes to zap 1418f5cc4eefSAl Viro * @details: details of nonlinear truncation or shared cache invalidation 1419f5cc4eefSAl Viro * 1420f5cc4eefSAl Viro * The range must fit into one VMA. 1421f5cc4eefSAl Viro */ 1422f5cc4eefSAl Viro static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address, 1423f5cc4eefSAl Viro unsigned long size, struct zap_details *details) 1424f5cc4eefSAl Viro { 1425f5cc4eefSAl Viro struct mm_struct *mm = vma->vm_mm; 1426f5cc4eefSAl Viro struct mmu_gather tlb; 1427f5cc4eefSAl Viro unsigned long end = address + size; 1428f5cc4eefSAl Viro 1429f5cc4eefSAl Viro lru_add_drain(); 1430f5cc4eefSAl Viro tlb_gather_mmu(&tlb, mm, 0); 1431f5cc4eefSAl Viro update_hiwater_rss(mm); 1432f5cc4eefSAl Viro mmu_notifier_invalidate_range_start(mm, address, end); 14334f74d2c8SLinus Torvalds unmap_single_vma(&tlb, vma, address, end, details); 1434f5cc4eefSAl Viro mmu_notifier_invalidate_range_end(mm, address, end); 1435f5cc4eefSAl Viro tlb_finish_mmu(&tlb, address, end); 14361da177e4SLinus Torvalds } 14371da177e4SLinus Torvalds 1438c627f9ccSJack Steiner /** 1439c627f9ccSJack Steiner * zap_vma_ptes - remove ptes mapping the vma 1440c627f9ccSJack Steiner * @vma: vm_area_struct holding ptes to be zapped 1441c627f9ccSJack Steiner * @address: starting address of pages to zap 1442c627f9ccSJack Steiner * @size: number of bytes to zap 1443c627f9ccSJack Steiner * 1444c627f9ccSJack Steiner * This function only unmaps ptes assigned to VM_PFNMAP vmas. 1445c627f9ccSJack Steiner * 1446c627f9ccSJack Steiner * The entire address range must be fully contained within the vma. 1447c627f9ccSJack Steiner * 1448c627f9ccSJack Steiner * Returns 0 if successful. 1449c627f9ccSJack Steiner */ 1450c627f9ccSJack Steiner int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, 1451c627f9ccSJack Steiner unsigned long size) 1452c627f9ccSJack Steiner { 1453c627f9ccSJack Steiner if (address < vma->vm_start || address + size > vma->vm_end || 1454c627f9ccSJack Steiner !(vma->vm_flags & VM_PFNMAP)) 1455c627f9ccSJack Steiner return -1; 1456f5cc4eefSAl Viro zap_page_range_single(vma, address, size, NULL); 1457c627f9ccSJack Steiner return 0; 1458c627f9ccSJack Steiner } 1459c627f9ccSJack Steiner EXPORT_SYMBOL_GPL(zap_vma_ptes); 1460c627f9ccSJack Steiner 1461142762bdSJohannes Weiner /** 1462142762bdSJohannes Weiner * follow_page - look up a page descriptor from a user-virtual address 1463142762bdSJohannes Weiner * @vma: vm_area_struct mapping @address 1464142762bdSJohannes Weiner * @address: virtual address to look up 1465142762bdSJohannes Weiner * @flags: flags modifying lookup behaviour 1466142762bdSJohannes Weiner * 1467142762bdSJohannes Weiner * @flags can have FOLL_ flags set, defined in <linux/mm.h> 1468142762bdSJohannes Weiner * 1469142762bdSJohannes Weiner * Returns the mapped (struct page *), %NULL if no mapping exists, or 1470142762bdSJohannes Weiner * an error pointer if there is a mapping to something not represented 1471142762bdSJohannes Weiner * by a page descriptor (see also vm_normal_page()). 14721da177e4SLinus Torvalds */ 14736aab341eSLinus Torvalds struct page *follow_page(struct vm_area_struct *vma, unsigned long address, 1474deceb6cdSHugh Dickins unsigned int flags) 14751da177e4SLinus Torvalds { 14761da177e4SLinus Torvalds pgd_t *pgd; 14771da177e4SLinus Torvalds pud_t *pud; 14781da177e4SLinus Torvalds pmd_t *pmd; 14791da177e4SLinus Torvalds pte_t *ptep, pte; 1480deceb6cdSHugh Dickins spinlock_t *ptl; 14811da177e4SLinus Torvalds struct page *page; 14826aab341eSLinus Torvalds struct mm_struct *mm = vma->vm_mm; 14831da177e4SLinus Torvalds 1484deceb6cdSHugh Dickins page = follow_huge_addr(mm, address, flags & FOLL_WRITE); 1485deceb6cdSHugh Dickins if (!IS_ERR(page)) { 1486deceb6cdSHugh Dickins BUG_ON(flags & FOLL_GET); 1487deceb6cdSHugh Dickins goto out; 1488deceb6cdSHugh Dickins } 14891da177e4SLinus Torvalds 1490deceb6cdSHugh Dickins page = NULL; 14911da177e4SLinus Torvalds pgd = pgd_offset(mm, address); 14921da177e4SLinus Torvalds if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) 1493deceb6cdSHugh Dickins goto no_page_table; 14941da177e4SLinus Torvalds 14951da177e4SLinus Torvalds pud = pud_offset(pgd, address); 1496ceb86879SAndi Kleen if (pud_none(*pud)) 1497ceb86879SAndi Kleen goto no_page_table; 14988a07651eSHugh Dickins if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) { 1499ceb86879SAndi Kleen BUG_ON(flags & FOLL_GET); 1500ceb86879SAndi Kleen page = follow_huge_pud(mm, address, pud, flags & FOLL_WRITE); 1501ceb86879SAndi Kleen goto out; 1502ceb86879SAndi Kleen } 1503ceb86879SAndi Kleen if (unlikely(pud_bad(*pud))) 1504deceb6cdSHugh Dickins goto no_page_table; 15051da177e4SLinus Torvalds 15061da177e4SLinus Torvalds pmd = pmd_offset(pud, address); 1507aeed5fceSHugh Dickins if (pmd_none(*pmd)) 1508deceb6cdSHugh Dickins goto no_page_table; 150971e3aac0SAndrea Arcangeli if (pmd_huge(*pmd) && vma->vm_flags & VM_HUGETLB) { 1510deceb6cdSHugh Dickins BUG_ON(flags & FOLL_GET); 1511deceb6cdSHugh Dickins page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE); 1512deceb6cdSHugh Dickins goto out; 1513deceb6cdSHugh Dickins } 151471e3aac0SAndrea Arcangeli if (pmd_trans_huge(*pmd)) { 1515500d65d4SAndrea Arcangeli if (flags & FOLL_SPLIT) { 1516500d65d4SAndrea Arcangeli split_huge_page_pmd(mm, pmd); 1517500d65d4SAndrea Arcangeli goto split_fallthrough; 1518500d65d4SAndrea Arcangeli } 151971e3aac0SAndrea Arcangeli spin_lock(&mm->page_table_lock); 152071e3aac0SAndrea Arcangeli if (likely(pmd_trans_huge(*pmd))) { 152171e3aac0SAndrea Arcangeli if (unlikely(pmd_trans_splitting(*pmd))) { 152271e3aac0SAndrea Arcangeli spin_unlock(&mm->page_table_lock); 152371e3aac0SAndrea Arcangeli wait_split_huge_page(vma->anon_vma, pmd); 152471e3aac0SAndrea Arcangeli } else { 152571e3aac0SAndrea Arcangeli page = follow_trans_huge_pmd(mm, address, 152671e3aac0SAndrea Arcangeli pmd, flags); 152771e3aac0SAndrea Arcangeli spin_unlock(&mm->page_table_lock); 152871e3aac0SAndrea Arcangeli goto out; 152971e3aac0SAndrea Arcangeli } 153071e3aac0SAndrea Arcangeli } else 153171e3aac0SAndrea Arcangeli spin_unlock(&mm->page_table_lock); 153271e3aac0SAndrea Arcangeli /* fall through */ 153371e3aac0SAndrea Arcangeli } 1534500d65d4SAndrea Arcangeli split_fallthrough: 1535aeed5fceSHugh Dickins if (unlikely(pmd_bad(*pmd))) 1536aeed5fceSHugh Dickins goto no_page_table; 1537aeed5fceSHugh Dickins 1538deceb6cdSHugh Dickins ptep = pte_offset_map_lock(mm, pmd, address, &ptl); 15391da177e4SLinus Torvalds 15401da177e4SLinus Torvalds pte = *ptep; 1541deceb6cdSHugh Dickins if (!pte_present(pte)) 154289f5b7daSLinus Torvalds goto no_page; 1543deceb6cdSHugh Dickins if ((flags & FOLL_WRITE) && !pte_write(pte)) 1544deceb6cdSHugh Dickins goto unlock; 1545a13ea5b7SHugh Dickins 15466aab341eSLinus Torvalds page = vm_normal_page(vma, address, pte); 1547a13ea5b7SHugh Dickins if (unlikely(!page)) { 1548a13ea5b7SHugh Dickins if ((flags & FOLL_DUMP) || 154962eede62SHugh Dickins !is_zero_pfn(pte_pfn(pte))) 155089f5b7daSLinus Torvalds goto bad_page; 1551a13ea5b7SHugh Dickins page = pte_page(pte); 1552a13ea5b7SHugh Dickins } 1553deceb6cdSHugh Dickins 1554deceb6cdSHugh Dickins if (flags & FOLL_GET) 155570b50f94SAndrea Arcangeli get_page_foll(page); 1556deceb6cdSHugh Dickins if (flags & FOLL_TOUCH) { 1557deceb6cdSHugh Dickins if ((flags & FOLL_WRITE) && 1558deceb6cdSHugh Dickins !pte_dirty(pte) && !PageDirty(page)) 1559f33ea7f4SNick Piggin set_page_dirty(page); 1560bd775c42SKOSAKI Motohiro /* 1561bd775c42SKOSAKI Motohiro * pte_mkyoung() would be more correct here, but atomic care 1562bd775c42SKOSAKI Motohiro * is needed to avoid losing the dirty bit: it is easier to use 1563bd775c42SKOSAKI Motohiro * mark_page_accessed(). 1564bd775c42SKOSAKI Motohiro */ 15651da177e4SLinus Torvalds mark_page_accessed(page); 15661da177e4SLinus Torvalds } 1567a1fde08cSLinus Torvalds if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) { 1568110d74a9SMichel Lespinasse /* 1569110d74a9SMichel Lespinasse * The preliminary mapping check is mainly to avoid the 1570110d74a9SMichel Lespinasse * pointless overhead of lock_page on the ZERO_PAGE 1571110d74a9SMichel Lespinasse * which might bounce very badly if there is contention. 1572110d74a9SMichel Lespinasse * 1573110d74a9SMichel Lespinasse * If the page is already locked, we don't need to 1574110d74a9SMichel Lespinasse * handle it now - vmscan will handle it later if and 1575110d74a9SMichel Lespinasse * when it attempts to reclaim the page. 1576110d74a9SMichel Lespinasse */ 1577110d74a9SMichel Lespinasse if (page->mapping && trylock_page(page)) { 1578110d74a9SMichel Lespinasse lru_add_drain(); /* push cached pages to LRU */ 1579110d74a9SMichel Lespinasse /* 1580110d74a9SMichel Lespinasse * Because we lock page here and migration is 1581110d74a9SMichel Lespinasse * blocked by the pte's page reference, we need 1582110d74a9SMichel Lespinasse * only check for file-cache page truncation. 1583110d74a9SMichel Lespinasse */ 1584110d74a9SMichel Lespinasse if (page->mapping) 1585110d74a9SMichel Lespinasse mlock_vma_page(page); 1586110d74a9SMichel Lespinasse unlock_page(page); 1587110d74a9SMichel Lespinasse } 1588110d74a9SMichel Lespinasse } 1589deceb6cdSHugh Dickins unlock: 1590deceb6cdSHugh Dickins pte_unmap_unlock(ptep, ptl); 15911da177e4SLinus Torvalds out: 1592deceb6cdSHugh Dickins return page; 1593deceb6cdSHugh Dickins 159489f5b7daSLinus Torvalds bad_page: 159589f5b7daSLinus Torvalds pte_unmap_unlock(ptep, ptl); 159689f5b7daSLinus Torvalds return ERR_PTR(-EFAULT); 159789f5b7daSLinus Torvalds 159889f5b7daSLinus Torvalds no_page: 159989f5b7daSLinus Torvalds pte_unmap_unlock(ptep, ptl); 160089f5b7daSLinus Torvalds if (!pte_none(pte)) 160189f5b7daSLinus Torvalds return page; 16028e4b9a60SHugh Dickins 1603deceb6cdSHugh Dickins no_page_table: 1604deceb6cdSHugh Dickins /* 1605deceb6cdSHugh Dickins * When core dumping an enormous anonymous area that nobody 16068e4b9a60SHugh Dickins * has touched so far, we don't want to allocate unnecessary pages or 16078e4b9a60SHugh Dickins * page tables. Return error instead of NULL to skip handle_mm_fault, 16088e4b9a60SHugh Dickins * then get_dump_page() will return NULL to leave a hole in the dump. 16098e4b9a60SHugh Dickins * But we can only make this optimization where a hole would surely 16108e4b9a60SHugh Dickins * be zero-filled if handle_mm_fault() actually did handle it. 1611deceb6cdSHugh Dickins */ 16128e4b9a60SHugh Dickins if ((flags & FOLL_DUMP) && 16138e4b9a60SHugh Dickins (!vma->vm_ops || !vma->vm_ops->fault)) 16148e4b9a60SHugh Dickins return ERR_PTR(-EFAULT); 1615deceb6cdSHugh Dickins return page; 16161da177e4SLinus Torvalds } 16171da177e4SLinus Torvalds 161895042f9eSLinus Torvalds static inline int stack_guard_page(struct vm_area_struct *vma, unsigned long addr) 161995042f9eSLinus Torvalds { 1620a09a79f6SMikulas Patocka return stack_guard_page_start(vma, addr) || 1621a09a79f6SMikulas Patocka stack_guard_page_end(vma, addr+PAGE_SIZE); 162295042f9eSLinus Torvalds } 162395042f9eSLinus Torvalds 16240014bd99SHuang Ying /** 16250014bd99SHuang Ying * __get_user_pages() - pin user pages in memory 16260014bd99SHuang Ying * @tsk: task_struct of target task 16270014bd99SHuang Ying * @mm: mm_struct of target mm 16280014bd99SHuang Ying * @start: starting user address 16290014bd99SHuang Ying * @nr_pages: number of pages from start to pin 16300014bd99SHuang Ying * @gup_flags: flags modifying pin behaviour 16310014bd99SHuang Ying * @pages: array that receives pointers to the pages pinned. 16320014bd99SHuang Ying * Should be at least nr_pages long. Or NULL, if caller 16330014bd99SHuang Ying * only intends to ensure the pages are faulted in. 16340014bd99SHuang Ying * @vmas: array of pointers to vmas corresponding to each page. 16350014bd99SHuang Ying * Or NULL if the caller does not require them. 16360014bd99SHuang Ying * @nonblocking: whether waiting for disk IO or mmap_sem contention 16370014bd99SHuang Ying * 16380014bd99SHuang Ying * Returns number of pages pinned. This may be fewer than the number 16390014bd99SHuang Ying * requested. If nr_pages is 0 or negative, returns 0. If no pages 16400014bd99SHuang Ying * were pinned, returns -errno. Each page returned must be released 16410014bd99SHuang Ying * with a put_page() call when it is finished with. vmas will only 16420014bd99SHuang Ying * remain valid while mmap_sem is held. 16430014bd99SHuang Ying * 16440014bd99SHuang Ying * Must be called with mmap_sem held for read or write. 16450014bd99SHuang Ying * 16460014bd99SHuang Ying * __get_user_pages walks a process's page tables and takes a reference to 16470014bd99SHuang Ying * each struct page that each user address corresponds to at a given 16480014bd99SHuang Ying * instant. That is, it takes the page that would be accessed if a user 16490014bd99SHuang Ying * thread accesses the given user virtual address at that instant. 16500014bd99SHuang Ying * 16510014bd99SHuang Ying * This does not guarantee that the page exists in the user mappings when 16520014bd99SHuang Ying * __get_user_pages returns, and there may even be a completely different 16530014bd99SHuang Ying * page there in some cases (eg. if mmapped pagecache has been invalidated 16540014bd99SHuang Ying * and subsequently re faulted). However it does guarantee that the page 16550014bd99SHuang Ying * won't be freed completely. And mostly callers simply care that the page 16560014bd99SHuang Ying * contains data that was valid *at some point in time*. Typically, an IO 16570014bd99SHuang Ying * or similar operation cannot guarantee anything stronger anyway because 16580014bd99SHuang Ying * locks can't be held over the syscall boundary. 16590014bd99SHuang Ying * 16600014bd99SHuang Ying * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If 16610014bd99SHuang Ying * the page is written to, set_page_dirty (or set_page_dirty_lock, as 16620014bd99SHuang Ying * appropriate) must be called after the page is finished with, and 16630014bd99SHuang Ying * before put_page is called. 16640014bd99SHuang Ying * 16650014bd99SHuang Ying * If @nonblocking != NULL, __get_user_pages will not wait for disk IO 16660014bd99SHuang Ying * or mmap_sem contention, and if waiting is needed to pin all pages, 16670014bd99SHuang Ying * *@nonblocking will be set to 0. 16680014bd99SHuang Ying * 16690014bd99SHuang Ying * In most cases, get_user_pages or get_user_pages_fast should be used 16700014bd99SHuang Ying * instead of __get_user_pages. __get_user_pages should be used only if 16710014bd99SHuang Ying * you need some special @gup_flags. 16720014bd99SHuang Ying */ 1673b291f000SNick Piggin int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm, 167458fa879eSHugh Dickins unsigned long start, int nr_pages, unsigned int gup_flags, 167553a7706dSMichel Lespinasse struct page **pages, struct vm_area_struct **vmas, 167653a7706dSMichel Lespinasse int *nonblocking) 16771da177e4SLinus Torvalds { 16781da177e4SLinus Torvalds int i; 167958fa879eSHugh Dickins unsigned long vm_flags; 16801da177e4SLinus Torvalds 16819d73777eSPeter Zijlstra if (nr_pages <= 0) 1682900cf086SJonathan Corbet return 0; 168358fa879eSHugh Dickins 168458fa879eSHugh Dickins VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET)); 168558fa879eSHugh Dickins 16861da177e4SLinus Torvalds /* 16871da177e4SLinus Torvalds * Require read or write permissions. 168858fa879eSHugh Dickins * If FOLL_FORCE is set, we only require the "MAY" flags. 16891da177e4SLinus Torvalds */ 169058fa879eSHugh Dickins vm_flags = (gup_flags & FOLL_WRITE) ? 169158fa879eSHugh Dickins (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD); 169258fa879eSHugh Dickins vm_flags &= (gup_flags & FOLL_FORCE) ? 169358fa879eSHugh Dickins (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE); 16941da177e4SLinus Torvalds i = 0; 16951da177e4SLinus Torvalds 16961da177e4SLinus Torvalds do { 16971da177e4SLinus Torvalds struct vm_area_struct *vma; 16981da177e4SLinus Torvalds 16991da177e4SLinus Torvalds vma = find_extend_vma(mm, start); 1700e7f22e20SStephen Wilson if (!vma && in_gate_area(mm, start)) { 17011da177e4SLinus Torvalds unsigned long pg = start & PAGE_MASK; 17021da177e4SLinus Torvalds pgd_t *pgd; 17031da177e4SLinus Torvalds pud_t *pud; 17041da177e4SLinus Torvalds pmd_t *pmd; 17051da177e4SLinus Torvalds pte_t *pte; 1706b291f000SNick Piggin 1707b291f000SNick Piggin /* user gate pages are read-only */ 170858fa879eSHugh Dickins if (gup_flags & FOLL_WRITE) 17091da177e4SLinus Torvalds return i ? : -EFAULT; 17101da177e4SLinus Torvalds if (pg > TASK_SIZE) 17111da177e4SLinus Torvalds pgd = pgd_offset_k(pg); 17121da177e4SLinus Torvalds else 17131da177e4SLinus Torvalds pgd = pgd_offset_gate(mm, pg); 17141da177e4SLinus Torvalds BUG_ON(pgd_none(*pgd)); 17151da177e4SLinus Torvalds pud = pud_offset(pgd, pg); 17161da177e4SLinus Torvalds BUG_ON(pud_none(*pud)); 17171da177e4SLinus Torvalds pmd = pmd_offset(pud, pg); 1718690dbe1cSHugh Dickins if (pmd_none(*pmd)) 1719690dbe1cSHugh Dickins return i ? : -EFAULT; 1720f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 17211da177e4SLinus Torvalds pte = pte_offset_map(pmd, pg); 1722690dbe1cSHugh Dickins if (pte_none(*pte)) { 1723690dbe1cSHugh Dickins pte_unmap(pte); 1724690dbe1cSHugh Dickins return i ? : -EFAULT; 1725690dbe1cSHugh Dickins } 172695042f9eSLinus Torvalds vma = get_gate_vma(mm); 17271da177e4SLinus Torvalds if (pages) { 1728de51257aSHugh Dickins struct page *page; 1729de51257aSHugh Dickins 173095042f9eSLinus Torvalds page = vm_normal_page(vma, start, *pte); 1731de51257aSHugh Dickins if (!page) { 1732de51257aSHugh Dickins if (!(gup_flags & FOLL_DUMP) && 1733de51257aSHugh Dickins is_zero_pfn(pte_pfn(*pte))) 1734de51257aSHugh Dickins page = pte_page(*pte); 1735de51257aSHugh Dickins else { 1736de51257aSHugh Dickins pte_unmap(pte); 1737de51257aSHugh Dickins return i ? : -EFAULT; 1738de51257aSHugh Dickins } 1739de51257aSHugh Dickins } 17406aab341eSLinus Torvalds pages[i] = page; 17416aab341eSLinus Torvalds get_page(page); 17421da177e4SLinus Torvalds } 17431da177e4SLinus Torvalds pte_unmap(pte); 174495042f9eSLinus Torvalds goto next_page; 17451da177e4SLinus Torvalds } 17461da177e4SLinus Torvalds 1747b291f000SNick Piggin if (!vma || 1748b291f000SNick Piggin (vma->vm_flags & (VM_IO | VM_PFNMAP)) || 17491c3aff1cSHugh Dickins !(vm_flags & vma->vm_flags)) 17501da177e4SLinus Torvalds return i ? : -EFAULT; 17511da177e4SLinus Torvalds 17522a15efc9SHugh Dickins if (is_vm_hugetlb_page(vma)) { 17532a15efc9SHugh Dickins i = follow_hugetlb_page(mm, vma, pages, vmas, 175458fa879eSHugh Dickins &start, &nr_pages, i, gup_flags); 17552a15efc9SHugh Dickins continue; 17562a15efc9SHugh Dickins } 1757deceb6cdSHugh Dickins 17581da177e4SLinus Torvalds do { 175908ef4729SHugh Dickins struct page *page; 176058fa879eSHugh Dickins unsigned int foll_flags = gup_flags; 17611da177e4SLinus Torvalds 1762462e00ccSEthan Solomita /* 17634779280dSYing Han * If we have a pending SIGKILL, don't keep faulting 17641c3aff1cSHugh Dickins * pages and potentially allocating memory. 1765462e00ccSEthan Solomita */ 17661c3aff1cSHugh Dickins if (unlikely(fatal_signal_pending(current))) 17674779280dSYing Han return i ? i : -ERESTARTSYS; 1768462e00ccSEthan Solomita 1769deceb6cdSHugh Dickins cond_resched(); 17706aab341eSLinus Torvalds while (!(page = follow_page(vma, start, foll_flags))) { 1771a68d2ebcSLinus Torvalds int ret; 177253a7706dSMichel Lespinasse unsigned int fault_flags = 0; 177353a7706dSMichel Lespinasse 1774a09a79f6SMikulas Patocka /* For mlock, just skip the stack guard page. */ 1775a09a79f6SMikulas Patocka if (foll_flags & FOLL_MLOCK) { 1776a09a79f6SMikulas Patocka if (stack_guard_page(vma, start)) 1777a09a79f6SMikulas Patocka goto next_page; 1778a09a79f6SMikulas Patocka } 177953a7706dSMichel Lespinasse if (foll_flags & FOLL_WRITE) 178053a7706dSMichel Lespinasse fault_flags |= FAULT_FLAG_WRITE; 178153a7706dSMichel Lespinasse if (nonblocking) 178253a7706dSMichel Lespinasse fault_flags |= FAULT_FLAG_ALLOW_RETRY; 1783318b275fSGleb Natapov if (foll_flags & FOLL_NOWAIT) 1784318b275fSGleb Natapov fault_flags |= (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT); 1785d06063ccSLinus Torvalds 1786d26ed650SHugh Dickins ret = handle_mm_fault(mm, vma, start, 178753a7706dSMichel Lespinasse fault_flags); 1788d26ed650SHugh Dickins 178983c54070SNick Piggin if (ret & VM_FAULT_ERROR) { 179083c54070SNick Piggin if (ret & VM_FAULT_OOM) 179183c54070SNick Piggin return i ? i : -ENOMEM; 179269ebb83eSHuang Ying if (ret & (VM_FAULT_HWPOISON | 179369ebb83eSHuang Ying VM_FAULT_HWPOISON_LARGE)) { 179469ebb83eSHuang Ying if (i) 179569ebb83eSHuang Ying return i; 179669ebb83eSHuang Ying else if (gup_flags & FOLL_HWPOISON) 179769ebb83eSHuang Ying return -EHWPOISON; 179869ebb83eSHuang Ying else 179969ebb83eSHuang Ying return -EFAULT; 180069ebb83eSHuang Ying } 180169ebb83eSHuang Ying if (ret & VM_FAULT_SIGBUS) 180283c54070SNick Piggin return i ? i : -EFAULT; 180383c54070SNick Piggin BUG(); 180483c54070SNick Piggin } 1805e7f22e20SStephen Wilson 1806e7f22e20SStephen Wilson if (tsk) { 180783c54070SNick Piggin if (ret & VM_FAULT_MAJOR) 180883c54070SNick Piggin tsk->maj_flt++; 180983c54070SNick Piggin else 181083c54070SNick Piggin tsk->min_flt++; 1811e7f22e20SStephen Wilson } 181283c54070SNick Piggin 181353a7706dSMichel Lespinasse if (ret & VM_FAULT_RETRY) { 1814318b275fSGleb Natapov if (nonblocking) 181553a7706dSMichel Lespinasse *nonblocking = 0; 181653a7706dSMichel Lespinasse return i; 181753a7706dSMichel Lespinasse } 181853a7706dSMichel Lespinasse 1819f33ea7f4SNick Piggin /* 182083c54070SNick Piggin * The VM_FAULT_WRITE bit tells us that 182183c54070SNick Piggin * do_wp_page has broken COW when necessary, 182283c54070SNick Piggin * even if maybe_mkwrite decided not to set 182383c54070SNick Piggin * pte_write. We can thus safely do subsequent 1824878b63acSHugh Dickins * page lookups as if they were reads. But only 1825878b63acSHugh Dickins * do so when looping for pte_write is futile: 1826878b63acSHugh Dickins * in some cases userspace may also be wanting 1827878b63acSHugh Dickins * to write to the gotten user page, which a 1828878b63acSHugh Dickins * read fault here might prevent (a readonly 1829878b63acSHugh Dickins * page might get reCOWed by userspace write). 1830f33ea7f4SNick Piggin */ 1831878b63acSHugh Dickins if ((ret & VM_FAULT_WRITE) && 1832878b63acSHugh Dickins !(vma->vm_flags & VM_WRITE)) 1833deceb6cdSHugh Dickins foll_flags &= ~FOLL_WRITE; 1834a68d2ebcSLinus Torvalds 18357f7bbbe5SBenjamin Herrenschmidt cond_resched(); 18361da177e4SLinus Torvalds } 183789f5b7daSLinus Torvalds if (IS_ERR(page)) 183889f5b7daSLinus Torvalds return i ? i : PTR_ERR(page); 18391da177e4SLinus Torvalds if (pages) { 184008ef4729SHugh Dickins pages[i] = page; 184103beb076SJames Bottomley 1842a6f36be3SRussell King flush_anon_page(vma, page, start); 184308ef4729SHugh Dickins flush_dcache_page(page); 18441da177e4SLinus Torvalds } 184595042f9eSLinus Torvalds next_page: 18461da177e4SLinus Torvalds if (vmas) 18471da177e4SLinus Torvalds vmas[i] = vma; 18481da177e4SLinus Torvalds i++; 18491da177e4SLinus Torvalds start += PAGE_SIZE; 18509d73777eSPeter Zijlstra nr_pages--; 18519d73777eSPeter Zijlstra } while (nr_pages && start < vma->vm_end); 18529d73777eSPeter Zijlstra } while (nr_pages); 18531da177e4SLinus Torvalds return i; 18541da177e4SLinus Torvalds } 18550014bd99SHuang Ying EXPORT_SYMBOL(__get_user_pages); 1856b291f000SNick Piggin 18572efaca92SBenjamin Herrenschmidt /* 18582efaca92SBenjamin Herrenschmidt * fixup_user_fault() - manually resolve a user page fault 18592efaca92SBenjamin Herrenschmidt * @tsk: the task_struct to use for page fault accounting, or 18602efaca92SBenjamin Herrenschmidt * NULL if faults are not to be recorded. 18612efaca92SBenjamin Herrenschmidt * @mm: mm_struct of target mm 18622efaca92SBenjamin Herrenschmidt * @address: user address 18632efaca92SBenjamin Herrenschmidt * @fault_flags:flags to pass down to handle_mm_fault() 18642efaca92SBenjamin Herrenschmidt * 18652efaca92SBenjamin Herrenschmidt * This is meant to be called in the specific scenario where for locking reasons 18662efaca92SBenjamin Herrenschmidt * we try to access user memory in atomic context (within a pagefault_disable() 18672efaca92SBenjamin Herrenschmidt * section), this returns -EFAULT, and we want to resolve the user fault before 18682efaca92SBenjamin Herrenschmidt * trying again. 18692efaca92SBenjamin Herrenschmidt * 18702efaca92SBenjamin Herrenschmidt * Typically this is meant to be used by the futex code. 18712efaca92SBenjamin Herrenschmidt * 18722efaca92SBenjamin Herrenschmidt * The main difference with get_user_pages() is that this function will 18732efaca92SBenjamin Herrenschmidt * unconditionally call handle_mm_fault() which will in turn perform all the 18742efaca92SBenjamin Herrenschmidt * necessary SW fixup of the dirty and young bits in the PTE, while 18752efaca92SBenjamin Herrenschmidt * handle_mm_fault() only guarantees to update these in the struct page. 18762efaca92SBenjamin Herrenschmidt * 18772efaca92SBenjamin Herrenschmidt * This is important for some architectures where those bits also gate the 18782efaca92SBenjamin Herrenschmidt * access permission to the page because they are maintained in software. On 18792efaca92SBenjamin Herrenschmidt * such architectures, gup() will not be enough to make a subsequent access 18802efaca92SBenjamin Herrenschmidt * succeed. 18812efaca92SBenjamin Herrenschmidt * 18822efaca92SBenjamin Herrenschmidt * This should be called with the mm_sem held for read. 18832efaca92SBenjamin Herrenschmidt */ 18842efaca92SBenjamin Herrenschmidt int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm, 18852efaca92SBenjamin Herrenschmidt unsigned long address, unsigned int fault_flags) 18862efaca92SBenjamin Herrenschmidt { 18872efaca92SBenjamin Herrenschmidt struct vm_area_struct *vma; 18882efaca92SBenjamin Herrenschmidt int ret; 18892efaca92SBenjamin Herrenschmidt 18902efaca92SBenjamin Herrenschmidt vma = find_extend_vma(mm, address); 18912efaca92SBenjamin Herrenschmidt if (!vma || address < vma->vm_start) 18922efaca92SBenjamin Herrenschmidt return -EFAULT; 18932efaca92SBenjamin Herrenschmidt 18942efaca92SBenjamin Herrenschmidt ret = handle_mm_fault(mm, vma, address, fault_flags); 18952efaca92SBenjamin Herrenschmidt if (ret & VM_FAULT_ERROR) { 18962efaca92SBenjamin Herrenschmidt if (ret & VM_FAULT_OOM) 18972efaca92SBenjamin Herrenschmidt return -ENOMEM; 18982efaca92SBenjamin Herrenschmidt if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE)) 18992efaca92SBenjamin Herrenschmidt return -EHWPOISON; 19002efaca92SBenjamin Herrenschmidt if (ret & VM_FAULT_SIGBUS) 19012efaca92SBenjamin Herrenschmidt return -EFAULT; 19022efaca92SBenjamin Herrenschmidt BUG(); 19032efaca92SBenjamin Herrenschmidt } 19042efaca92SBenjamin Herrenschmidt if (tsk) { 19052efaca92SBenjamin Herrenschmidt if (ret & VM_FAULT_MAJOR) 19062efaca92SBenjamin Herrenschmidt tsk->maj_flt++; 19072efaca92SBenjamin Herrenschmidt else 19082efaca92SBenjamin Herrenschmidt tsk->min_flt++; 19092efaca92SBenjamin Herrenschmidt } 19102efaca92SBenjamin Herrenschmidt return 0; 19112efaca92SBenjamin Herrenschmidt } 19122efaca92SBenjamin Herrenschmidt 19132efaca92SBenjamin Herrenschmidt /* 1914d2bf6be8SNick Piggin * get_user_pages() - pin user pages in memory 1915e7f22e20SStephen Wilson * @tsk: the task_struct to use for page fault accounting, or 1916e7f22e20SStephen Wilson * NULL if faults are not to be recorded. 1917d2bf6be8SNick Piggin * @mm: mm_struct of target mm 1918d2bf6be8SNick Piggin * @start: starting user address 19199d73777eSPeter Zijlstra * @nr_pages: number of pages from start to pin 1920d2bf6be8SNick Piggin * @write: whether pages will be written to by the caller 1921d2bf6be8SNick Piggin * @force: whether to force write access even if user mapping is 1922d2bf6be8SNick Piggin * readonly. This will result in the page being COWed even 1923d2bf6be8SNick Piggin * in MAP_SHARED mappings. You do not want this. 1924d2bf6be8SNick Piggin * @pages: array that receives pointers to the pages pinned. 1925d2bf6be8SNick Piggin * Should be at least nr_pages long. Or NULL, if caller 1926d2bf6be8SNick Piggin * only intends to ensure the pages are faulted in. 1927d2bf6be8SNick Piggin * @vmas: array of pointers to vmas corresponding to each page. 1928d2bf6be8SNick Piggin * Or NULL if the caller does not require them. 1929d2bf6be8SNick Piggin * 1930d2bf6be8SNick Piggin * Returns number of pages pinned. This may be fewer than the number 19319d73777eSPeter Zijlstra * requested. If nr_pages is 0 or negative, returns 0. If no pages 1932d2bf6be8SNick Piggin * were pinned, returns -errno. Each page returned must be released 1933d2bf6be8SNick Piggin * with a put_page() call when it is finished with. vmas will only 1934d2bf6be8SNick Piggin * remain valid while mmap_sem is held. 1935d2bf6be8SNick Piggin * 1936d2bf6be8SNick Piggin * Must be called with mmap_sem held for read or write. 1937d2bf6be8SNick Piggin * 1938d2bf6be8SNick Piggin * get_user_pages walks a process's page tables and takes a reference to 1939d2bf6be8SNick Piggin * each struct page that each user address corresponds to at a given 1940d2bf6be8SNick Piggin * instant. That is, it takes the page that would be accessed if a user 1941d2bf6be8SNick Piggin * thread accesses the given user virtual address at that instant. 1942d2bf6be8SNick Piggin * 1943d2bf6be8SNick Piggin * This does not guarantee that the page exists in the user mappings when 1944d2bf6be8SNick Piggin * get_user_pages returns, and there may even be a completely different 1945d2bf6be8SNick Piggin * page there in some cases (eg. if mmapped pagecache has been invalidated 1946d2bf6be8SNick Piggin * and subsequently re faulted). However it does guarantee that the page 1947d2bf6be8SNick Piggin * won't be freed completely. And mostly callers simply care that the page 1948d2bf6be8SNick Piggin * contains data that was valid *at some point in time*. Typically, an IO 1949d2bf6be8SNick Piggin * or similar operation cannot guarantee anything stronger anyway because 1950d2bf6be8SNick Piggin * locks can't be held over the syscall boundary. 1951d2bf6be8SNick Piggin * 1952d2bf6be8SNick Piggin * If write=0, the page must not be written to. If the page is written to, 1953d2bf6be8SNick Piggin * set_page_dirty (or set_page_dirty_lock, as appropriate) must be called 1954d2bf6be8SNick Piggin * after the page is finished with, and before put_page is called. 1955d2bf6be8SNick Piggin * 1956d2bf6be8SNick Piggin * get_user_pages is typically used for fewer-copy IO operations, to get a 1957d2bf6be8SNick Piggin * handle on the memory by some means other than accesses via the user virtual 1958d2bf6be8SNick Piggin * addresses. The pages may be submitted for DMA to devices or accessed via 1959d2bf6be8SNick Piggin * their kernel linear mapping (via the kmap APIs). Care should be taken to 1960d2bf6be8SNick Piggin * use the correct cache flushing APIs. 1961d2bf6be8SNick Piggin * 1962d2bf6be8SNick Piggin * See also get_user_pages_fast, for performance critical applications. 1963d2bf6be8SNick Piggin */ 1964b291f000SNick Piggin int get_user_pages(struct task_struct *tsk, struct mm_struct *mm, 19659d73777eSPeter Zijlstra unsigned long start, int nr_pages, int write, int force, 1966b291f000SNick Piggin struct page **pages, struct vm_area_struct **vmas) 1967b291f000SNick Piggin { 196858fa879eSHugh Dickins int flags = FOLL_TOUCH; 1969b291f000SNick Piggin 197058fa879eSHugh Dickins if (pages) 197158fa879eSHugh Dickins flags |= FOLL_GET; 1972b291f000SNick Piggin if (write) 197358fa879eSHugh Dickins flags |= FOLL_WRITE; 1974b291f000SNick Piggin if (force) 197558fa879eSHugh Dickins flags |= FOLL_FORCE; 1976b291f000SNick Piggin 197753a7706dSMichel Lespinasse return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas, 197853a7706dSMichel Lespinasse NULL); 1979b291f000SNick Piggin } 19801da177e4SLinus Torvalds EXPORT_SYMBOL(get_user_pages); 19811da177e4SLinus Torvalds 1982f3e8fccdSHugh Dickins /** 1983f3e8fccdSHugh Dickins * get_dump_page() - pin user page in memory while writing it to core dump 1984f3e8fccdSHugh Dickins * @addr: user address 1985f3e8fccdSHugh Dickins * 1986f3e8fccdSHugh Dickins * Returns struct page pointer of user page pinned for dump, 1987f3e8fccdSHugh Dickins * to be freed afterwards by page_cache_release() or put_page(). 1988f3e8fccdSHugh Dickins * 1989f3e8fccdSHugh Dickins * Returns NULL on any kind of failure - a hole must then be inserted into 1990f3e8fccdSHugh Dickins * the corefile, to preserve alignment with its headers; and also returns 1991f3e8fccdSHugh Dickins * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found - 1992f3e8fccdSHugh Dickins * allowing a hole to be left in the corefile to save diskspace. 1993f3e8fccdSHugh Dickins * 1994f3e8fccdSHugh Dickins * Called without mmap_sem, but after all other threads have been killed. 1995f3e8fccdSHugh Dickins */ 1996f3e8fccdSHugh Dickins #ifdef CONFIG_ELF_CORE 1997f3e8fccdSHugh Dickins struct page *get_dump_page(unsigned long addr) 1998f3e8fccdSHugh Dickins { 1999f3e8fccdSHugh Dickins struct vm_area_struct *vma; 2000f3e8fccdSHugh Dickins struct page *page; 2001f3e8fccdSHugh Dickins 2002f3e8fccdSHugh Dickins if (__get_user_pages(current, current->mm, addr, 1, 200353a7706dSMichel Lespinasse FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma, 200453a7706dSMichel Lespinasse NULL) < 1) 2005f3e8fccdSHugh Dickins return NULL; 2006f3e8fccdSHugh Dickins flush_cache_page(vma, addr, page_to_pfn(page)); 2007f3e8fccdSHugh Dickins return page; 2008f3e8fccdSHugh Dickins } 2009f3e8fccdSHugh Dickins #endif /* CONFIG_ELF_CORE */ 2010f3e8fccdSHugh Dickins 201125ca1d6cSNamhyung Kim pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr, 2012920c7a5dSHarvey Harrison spinlock_t **ptl) 2013c9cfcddfSLinus Torvalds { 2014c9cfcddfSLinus Torvalds pgd_t * pgd = pgd_offset(mm, addr); 2015c9cfcddfSLinus Torvalds pud_t * pud = pud_alloc(mm, pgd, addr); 2016c9cfcddfSLinus Torvalds if (pud) { 201749c91fb0STrond Myklebust pmd_t * pmd = pmd_alloc(mm, pud, addr); 2018f66055abSAndrea Arcangeli if (pmd) { 2019f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 2020c9cfcddfSLinus Torvalds return pte_alloc_map_lock(mm, pmd, addr, ptl); 2021c9cfcddfSLinus Torvalds } 2022f66055abSAndrea Arcangeli } 2023c9cfcddfSLinus Torvalds return NULL; 2024c9cfcddfSLinus Torvalds } 2025c9cfcddfSLinus Torvalds 20261da177e4SLinus Torvalds /* 2027238f58d8SLinus Torvalds * This is the old fallback for page remapping. 2028238f58d8SLinus Torvalds * 2029238f58d8SLinus Torvalds * For historical reasons, it only allows reserved pages. Only 2030238f58d8SLinus Torvalds * old drivers should use this, and they needed to mark their 2031238f58d8SLinus Torvalds * pages reserved for the old functions anyway. 2032238f58d8SLinus Torvalds */ 2033423bad60SNick Piggin static int insert_page(struct vm_area_struct *vma, unsigned long addr, 2034423bad60SNick Piggin struct page *page, pgprot_t prot) 2035238f58d8SLinus Torvalds { 2036423bad60SNick Piggin struct mm_struct *mm = vma->vm_mm; 2037238f58d8SLinus Torvalds int retval; 2038238f58d8SLinus Torvalds pte_t *pte; 2039238f58d8SLinus Torvalds spinlock_t *ptl; 2040238f58d8SLinus Torvalds 2041238f58d8SLinus Torvalds retval = -EINVAL; 2042a145dd41SLinus Torvalds if (PageAnon(page)) 20435b4e655eSKAMEZAWA Hiroyuki goto out; 2044238f58d8SLinus Torvalds retval = -ENOMEM; 2045238f58d8SLinus Torvalds flush_dcache_page(page); 2046c9cfcddfSLinus Torvalds pte = get_locked_pte(mm, addr, &ptl); 2047238f58d8SLinus Torvalds if (!pte) 20485b4e655eSKAMEZAWA Hiroyuki goto out; 2049238f58d8SLinus Torvalds retval = -EBUSY; 2050238f58d8SLinus Torvalds if (!pte_none(*pte)) 2051238f58d8SLinus Torvalds goto out_unlock; 2052238f58d8SLinus Torvalds 2053238f58d8SLinus Torvalds /* Ok, finally just insert the thing.. */ 2054238f58d8SLinus Torvalds get_page(page); 205534e55232SKAMEZAWA Hiroyuki inc_mm_counter_fast(mm, MM_FILEPAGES); 2056238f58d8SLinus Torvalds page_add_file_rmap(page); 2057238f58d8SLinus Torvalds set_pte_at(mm, addr, pte, mk_pte(page, prot)); 2058238f58d8SLinus Torvalds 2059238f58d8SLinus Torvalds retval = 0; 20608a9f3ccdSBalbir Singh pte_unmap_unlock(pte, ptl); 20618a9f3ccdSBalbir Singh return retval; 2062238f58d8SLinus Torvalds out_unlock: 2063238f58d8SLinus Torvalds pte_unmap_unlock(pte, ptl); 2064238f58d8SLinus Torvalds out: 2065238f58d8SLinus Torvalds return retval; 2066238f58d8SLinus Torvalds } 2067238f58d8SLinus Torvalds 2068bfa5bf6dSRolf Eike Beer /** 2069bfa5bf6dSRolf Eike Beer * vm_insert_page - insert single page into user vma 2070bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 2071bfa5bf6dSRolf Eike Beer * @addr: target user address of this page 2072bfa5bf6dSRolf Eike Beer * @page: source kernel page 2073bfa5bf6dSRolf Eike Beer * 2074a145dd41SLinus Torvalds * This allows drivers to insert individual pages they've allocated 2075a145dd41SLinus Torvalds * into a user vma. 2076a145dd41SLinus Torvalds * 2077a145dd41SLinus Torvalds * The page has to be a nice clean _individual_ kernel allocation. 2078a145dd41SLinus Torvalds * If you allocate a compound page, you need to have marked it as 2079a145dd41SLinus Torvalds * such (__GFP_COMP), or manually just split the page up yourself 20808dfcc9baSNick Piggin * (see split_page()). 2081a145dd41SLinus Torvalds * 2082a145dd41SLinus Torvalds * NOTE! Traditionally this was done with "remap_pfn_range()" which 2083a145dd41SLinus Torvalds * took an arbitrary page protection parameter. This doesn't allow 2084a145dd41SLinus Torvalds * that. Your vma protection will have to be set up correctly, which 2085a145dd41SLinus Torvalds * means that if you want a shared writable mapping, you'd better 2086a145dd41SLinus Torvalds * ask for a shared writable mapping! 2087a145dd41SLinus Torvalds * 2088a145dd41SLinus Torvalds * The page does not need to be reserved. 2089*4b6e1e37SKonstantin Khlebnikov * 2090*4b6e1e37SKonstantin Khlebnikov * Usually this function is called from f_op->mmap() handler 2091*4b6e1e37SKonstantin Khlebnikov * under mm->mmap_sem write-lock, so it can change vma->vm_flags. 2092*4b6e1e37SKonstantin Khlebnikov * Caller must set VM_MIXEDMAP on vma if it wants to call this 2093*4b6e1e37SKonstantin Khlebnikov * function from other places, for example from page-fault handler. 2094a145dd41SLinus Torvalds */ 2095423bad60SNick Piggin int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, 2096423bad60SNick Piggin struct page *page) 2097a145dd41SLinus Torvalds { 2098a145dd41SLinus Torvalds if (addr < vma->vm_start || addr >= vma->vm_end) 2099a145dd41SLinus Torvalds return -EFAULT; 2100a145dd41SLinus Torvalds if (!page_count(page)) 2101a145dd41SLinus Torvalds return -EINVAL; 2102*4b6e1e37SKonstantin Khlebnikov if (!(vma->vm_flags & VM_MIXEDMAP)) { 2103*4b6e1e37SKonstantin Khlebnikov BUG_ON(down_read_trylock(&vma->vm_mm->mmap_sem)); 2104*4b6e1e37SKonstantin Khlebnikov BUG_ON(vma->vm_flags & VM_PFNMAP); 2105*4b6e1e37SKonstantin Khlebnikov vma->vm_flags |= VM_MIXEDMAP; 2106*4b6e1e37SKonstantin Khlebnikov } 2107423bad60SNick Piggin return insert_page(vma, addr, page, vma->vm_page_prot); 2108a145dd41SLinus Torvalds } 2109e3c3374fSLinus Torvalds EXPORT_SYMBOL(vm_insert_page); 2110a145dd41SLinus Torvalds 2111423bad60SNick Piggin static int insert_pfn(struct vm_area_struct *vma, unsigned long addr, 2112423bad60SNick Piggin unsigned long pfn, pgprot_t prot) 2113423bad60SNick Piggin { 2114423bad60SNick Piggin struct mm_struct *mm = vma->vm_mm; 2115423bad60SNick Piggin int retval; 2116423bad60SNick Piggin pte_t *pte, entry; 2117423bad60SNick Piggin spinlock_t *ptl; 2118423bad60SNick Piggin 2119423bad60SNick Piggin retval = -ENOMEM; 2120423bad60SNick Piggin pte = get_locked_pte(mm, addr, &ptl); 2121423bad60SNick Piggin if (!pte) 2122423bad60SNick Piggin goto out; 2123423bad60SNick Piggin retval = -EBUSY; 2124423bad60SNick Piggin if (!pte_none(*pte)) 2125423bad60SNick Piggin goto out_unlock; 2126423bad60SNick Piggin 2127423bad60SNick Piggin /* Ok, finally just insert the thing.. */ 2128423bad60SNick Piggin entry = pte_mkspecial(pfn_pte(pfn, prot)); 2129423bad60SNick Piggin set_pte_at(mm, addr, pte, entry); 21304b3073e1SRussell King update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */ 2131423bad60SNick Piggin 2132423bad60SNick Piggin retval = 0; 2133423bad60SNick Piggin out_unlock: 2134423bad60SNick Piggin pte_unmap_unlock(pte, ptl); 2135423bad60SNick Piggin out: 2136423bad60SNick Piggin return retval; 2137423bad60SNick Piggin } 2138423bad60SNick Piggin 2139e0dc0d8fSNick Piggin /** 2140e0dc0d8fSNick Piggin * vm_insert_pfn - insert single pfn into user vma 2141e0dc0d8fSNick Piggin * @vma: user vma to map to 2142e0dc0d8fSNick Piggin * @addr: target user address of this page 2143e0dc0d8fSNick Piggin * @pfn: source kernel pfn 2144e0dc0d8fSNick Piggin * 2145e0dc0d8fSNick Piggin * Similar to vm_inert_page, this allows drivers to insert individual pages 2146e0dc0d8fSNick Piggin * they've allocated into a user vma. Same comments apply. 2147e0dc0d8fSNick Piggin * 2148e0dc0d8fSNick Piggin * This function should only be called from a vm_ops->fault handler, and 2149e0dc0d8fSNick Piggin * in that case the handler should return NULL. 21500d71d10aSNick Piggin * 21510d71d10aSNick Piggin * vma cannot be a COW mapping. 21520d71d10aSNick Piggin * 21530d71d10aSNick Piggin * As this is called only for pages that do not currently exist, we 21540d71d10aSNick Piggin * do not need to flush old virtual caches or the TLB. 2155e0dc0d8fSNick Piggin */ 2156e0dc0d8fSNick Piggin int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr, 2157e0dc0d8fSNick Piggin unsigned long pfn) 2158e0dc0d8fSNick Piggin { 21592ab64037Svenkatesh.pallipadi@intel.com int ret; 2160e4b866edSvenkatesh.pallipadi@intel.com pgprot_t pgprot = vma->vm_page_prot; 21617e675137SNick Piggin /* 21627e675137SNick Piggin * Technically, architectures with pte_special can avoid all these 21637e675137SNick Piggin * restrictions (same for remap_pfn_range). However we would like 21647e675137SNick Piggin * consistency in testing and feature parity among all, so we should 21657e675137SNick Piggin * try to keep these invariants in place for everybody. 21667e675137SNick Piggin */ 2167b379d790SJared Hulbert BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))); 2168b379d790SJared Hulbert BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) == 2169b379d790SJared Hulbert (VM_PFNMAP|VM_MIXEDMAP)); 2170b379d790SJared Hulbert BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); 2171b379d790SJared Hulbert BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn)); 2172e0dc0d8fSNick Piggin 2173423bad60SNick Piggin if (addr < vma->vm_start || addr >= vma->vm_end) 2174423bad60SNick Piggin return -EFAULT; 21755180da41SSuresh Siddha if (track_pfn_insert(vma, &pgprot, pfn)) 21762ab64037Svenkatesh.pallipadi@intel.com return -EINVAL; 21772ab64037Svenkatesh.pallipadi@intel.com 2178e4b866edSvenkatesh.pallipadi@intel.com ret = insert_pfn(vma, addr, pfn, pgprot); 21792ab64037Svenkatesh.pallipadi@intel.com 21802ab64037Svenkatesh.pallipadi@intel.com return ret; 2181e0dc0d8fSNick Piggin } 2182e0dc0d8fSNick Piggin EXPORT_SYMBOL(vm_insert_pfn); 2183e0dc0d8fSNick Piggin 2184423bad60SNick Piggin int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr, 2185423bad60SNick Piggin unsigned long pfn) 2186423bad60SNick Piggin { 2187423bad60SNick Piggin BUG_ON(!(vma->vm_flags & VM_MIXEDMAP)); 2188423bad60SNick Piggin 2189423bad60SNick Piggin if (addr < vma->vm_start || addr >= vma->vm_end) 2190423bad60SNick Piggin return -EFAULT; 2191423bad60SNick Piggin 2192423bad60SNick Piggin /* 2193423bad60SNick Piggin * If we don't have pte special, then we have to use the pfn_valid() 2194423bad60SNick Piggin * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must* 2195423bad60SNick Piggin * refcount the page if pfn_valid is true (hence insert_page rather 219662eede62SHugh Dickins * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP 219762eede62SHugh Dickins * without pte special, it would there be refcounted as a normal page. 2198423bad60SNick Piggin */ 2199423bad60SNick Piggin if (!HAVE_PTE_SPECIAL && pfn_valid(pfn)) { 2200423bad60SNick Piggin struct page *page; 2201423bad60SNick Piggin 2202423bad60SNick Piggin page = pfn_to_page(pfn); 2203423bad60SNick Piggin return insert_page(vma, addr, page, vma->vm_page_prot); 2204423bad60SNick Piggin } 2205423bad60SNick Piggin return insert_pfn(vma, addr, pfn, vma->vm_page_prot); 2206423bad60SNick Piggin } 2207423bad60SNick Piggin EXPORT_SYMBOL(vm_insert_mixed); 2208423bad60SNick Piggin 2209a145dd41SLinus Torvalds /* 22101da177e4SLinus Torvalds * maps a range of physical memory into the requested pages. the old 22111da177e4SLinus Torvalds * mappings are removed. any references to nonexistent pages results 22121da177e4SLinus Torvalds * in null mappings (currently treated as "copy-on-access") 22131da177e4SLinus Torvalds */ 22141da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd, 22151da177e4SLinus Torvalds unsigned long addr, unsigned long end, 22161da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 22171da177e4SLinus Torvalds { 22181da177e4SLinus Torvalds pte_t *pte; 2219c74df32cSHugh Dickins spinlock_t *ptl; 22201da177e4SLinus Torvalds 2221c74df32cSHugh Dickins pte = pte_alloc_map_lock(mm, pmd, addr, &ptl); 22221da177e4SLinus Torvalds if (!pte) 22231da177e4SLinus Torvalds return -ENOMEM; 22246606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 22251da177e4SLinus Torvalds do { 22261da177e4SLinus Torvalds BUG_ON(!pte_none(*pte)); 22277e675137SNick Piggin set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot))); 22281da177e4SLinus Torvalds pfn++; 22291da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 22306606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 2231c74df32cSHugh Dickins pte_unmap_unlock(pte - 1, ptl); 22321da177e4SLinus Torvalds return 0; 22331da177e4SLinus Torvalds } 22341da177e4SLinus Torvalds 22351da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud, 22361da177e4SLinus Torvalds unsigned long addr, unsigned long end, 22371da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 22381da177e4SLinus Torvalds { 22391da177e4SLinus Torvalds pmd_t *pmd; 22401da177e4SLinus Torvalds unsigned long next; 22411da177e4SLinus Torvalds 22421da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 22431da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, addr); 22441da177e4SLinus Torvalds if (!pmd) 22451da177e4SLinus Torvalds return -ENOMEM; 2246f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 22471da177e4SLinus Torvalds do { 22481da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 22491da177e4SLinus Torvalds if (remap_pte_range(mm, pmd, addr, next, 22501da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot)) 22511da177e4SLinus Torvalds return -ENOMEM; 22521da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 22531da177e4SLinus Torvalds return 0; 22541da177e4SLinus Torvalds } 22551da177e4SLinus Torvalds 22561da177e4SLinus Torvalds static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd, 22571da177e4SLinus Torvalds unsigned long addr, unsigned long end, 22581da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 22591da177e4SLinus Torvalds { 22601da177e4SLinus Torvalds pud_t *pud; 22611da177e4SLinus Torvalds unsigned long next; 22621da177e4SLinus Torvalds 22631da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 22641da177e4SLinus Torvalds pud = pud_alloc(mm, pgd, addr); 22651da177e4SLinus Torvalds if (!pud) 22661da177e4SLinus Torvalds return -ENOMEM; 22671da177e4SLinus Torvalds do { 22681da177e4SLinus Torvalds next = pud_addr_end(addr, end); 22691da177e4SLinus Torvalds if (remap_pmd_range(mm, pud, addr, next, 22701da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot)) 22711da177e4SLinus Torvalds return -ENOMEM; 22721da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 22731da177e4SLinus Torvalds return 0; 22741da177e4SLinus Torvalds } 22751da177e4SLinus Torvalds 2276bfa5bf6dSRolf Eike Beer /** 2277bfa5bf6dSRolf Eike Beer * remap_pfn_range - remap kernel memory to userspace 2278bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 2279bfa5bf6dSRolf Eike Beer * @addr: target user address to start at 2280bfa5bf6dSRolf Eike Beer * @pfn: physical address of kernel memory 2281bfa5bf6dSRolf Eike Beer * @size: size of map area 2282bfa5bf6dSRolf Eike Beer * @prot: page protection flags for this mapping 2283bfa5bf6dSRolf Eike Beer * 2284bfa5bf6dSRolf Eike Beer * Note: this is only safe if the mm semaphore is held when called. 2285bfa5bf6dSRolf Eike Beer */ 22861da177e4SLinus Torvalds int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 22871da177e4SLinus Torvalds unsigned long pfn, unsigned long size, pgprot_t prot) 22881da177e4SLinus Torvalds { 22891da177e4SLinus Torvalds pgd_t *pgd; 22901da177e4SLinus Torvalds unsigned long next; 22912d15cab8SHugh Dickins unsigned long end = addr + PAGE_ALIGN(size); 22921da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 22931da177e4SLinus Torvalds int err; 22941da177e4SLinus Torvalds 22951da177e4SLinus Torvalds /* 22961da177e4SLinus Torvalds * Physically remapped pages are special. Tell the 22971da177e4SLinus Torvalds * rest of the world about it: 22981da177e4SLinus Torvalds * VM_IO tells people not to look at these pages 22991da177e4SLinus Torvalds * (accesses can have side effects). 23000b14c179SHugh Dickins * VM_RESERVED is specified all over the place, because 23010b14c179SHugh Dickins * in 2.4 it kept swapout's vma scan off this vma; but 23020b14c179SHugh Dickins * in 2.6 the LRU scan won't even find its pages, so this 23030b14c179SHugh Dickins * flag means no more than count its pages in reserved_vm, 23040b14c179SHugh Dickins * and omit it from core dump, even when VM_IO turned off. 23056aab341eSLinus Torvalds * VM_PFNMAP tells the core MM that the base pages are just 23066aab341eSLinus Torvalds * raw PFN mappings, and do not have a "struct page" associated 23076aab341eSLinus Torvalds * with them. 2308fb155c16SLinus Torvalds * 2309fb155c16SLinus Torvalds * There's a horrible special case to handle copy-on-write 2310fb155c16SLinus Torvalds * behaviour that some programs depend on. We mark the "original" 2311fb155c16SLinus Torvalds * un-COW'ed pages by matching them up with "vma->vm_pgoff". 2312b3b9c293SKonstantin Khlebnikov * See vm_normal_page() for details. 23131da177e4SLinus Torvalds */ 2314b3b9c293SKonstantin Khlebnikov if (is_cow_mapping(vma->vm_flags)) { 2315b3b9c293SKonstantin Khlebnikov if (addr != vma->vm_start || end != vma->vm_end) 2316b3b9c293SKonstantin Khlebnikov return -EINVAL; 23176aab341eSLinus Torvalds vma->vm_pgoff = pfn; 2318b3b9c293SKonstantin Khlebnikov } 2319b3b9c293SKonstantin Khlebnikov 2320b3b9c293SKonstantin Khlebnikov err = track_pfn_remap(vma, &prot, pfn, addr, PAGE_ALIGN(size)); 2321b3b9c293SKonstantin Khlebnikov if (err) 23223c8bb73aSvenkatesh.pallipadi@intel.com return -EINVAL; 2323fb155c16SLinus Torvalds 2324fb155c16SLinus Torvalds vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP; 23251da177e4SLinus Torvalds 23261da177e4SLinus Torvalds BUG_ON(addr >= end); 23271da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 23281da177e4SLinus Torvalds pgd = pgd_offset(mm, addr); 23291da177e4SLinus Torvalds flush_cache_range(vma, addr, end); 23301da177e4SLinus Torvalds do { 23311da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 23321da177e4SLinus Torvalds err = remap_pud_range(mm, pgd, addr, next, 23331da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot); 23341da177e4SLinus Torvalds if (err) 23351da177e4SLinus Torvalds break; 23361da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 23372ab64037Svenkatesh.pallipadi@intel.com 23382ab64037Svenkatesh.pallipadi@intel.com if (err) 23395180da41SSuresh Siddha untrack_pfn(vma, pfn, PAGE_ALIGN(size)); 23402ab64037Svenkatesh.pallipadi@intel.com 23411da177e4SLinus Torvalds return err; 23421da177e4SLinus Torvalds } 23431da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range); 23441da177e4SLinus Torvalds 2345aee16b3cSJeremy Fitzhardinge static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd, 2346aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2347aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 2348aee16b3cSJeremy Fitzhardinge { 2349aee16b3cSJeremy Fitzhardinge pte_t *pte; 2350aee16b3cSJeremy Fitzhardinge int err; 23512f569afdSMartin Schwidefsky pgtable_t token; 235294909914SBorislav Petkov spinlock_t *uninitialized_var(ptl); 2353aee16b3cSJeremy Fitzhardinge 2354aee16b3cSJeremy Fitzhardinge pte = (mm == &init_mm) ? 2355aee16b3cSJeremy Fitzhardinge pte_alloc_kernel(pmd, addr) : 2356aee16b3cSJeremy Fitzhardinge pte_alloc_map_lock(mm, pmd, addr, &ptl); 2357aee16b3cSJeremy Fitzhardinge if (!pte) 2358aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2359aee16b3cSJeremy Fitzhardinge 2360aee16b3cSJeremy Fitzhardinge BUG_ON(pmd_huge(*pmd)); 2361aee16b3cSJeremy Fitzhardinge 236238e0edb1SJeremy Fitzhardinge arch_enter_lazy_mmu_mode(); 236338e0edb1SJeremy Fitzhardinge 23642f569afdSMartin Schwidefsky token = pmd_pgtable(*pmd); 2365aee16b3cSJeremy Fitzhardinge 2366aee16b3cSJeremy Fitzhardinge do { 2367c36987e2SDaisuke Nishimura err = fn(pte++, token, addr, data); 2368aee16b3cSJeremy Fitzhardinge if (err) 2369aee16b3cSJeremy Fitzhardinge break; 2370c36987e2SDaisuke Nishimura } while (addr += PAGE_SIZE, addr != end); 2371aee16b3cSJeremy Fitzhardinge 237238e0edb1SJeremy Fitzhardinge arch_leave_lazy_mmu_mode(); 237338e0edb1SJeremy Fitzhardinge 2374aee16b3cSJeremy Fitzhardinge if (mm != &init_mm) 2375aee16b3cSJeremy Fitzhardinge pte_unmap_unlock(pte-1, ptl); 2376aee16b3cSJeremy Fitzhardinge return err; 2377aee16b3cSJeremy Fitzhardinge } 2378aee16b3cSJeremy Fitzhardinge 2379aee16b3cSJeremy Fitzhardinge static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud, 2380aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2381aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 2382aee16b3cSJeremy Fitzhardinge { 2383aee16b3cSJeremy Fitzhardinge pmd_t *pmd; 2384aee16b3cSJeremy Fitzhardinge unsigned long next; 2385aee16b3cSJeremy Fitzhardinge int err; 2386aee16b3cSJeremy Fitzhardinge 2387ceb86879SAndi Kleen BUG_ON(pud_huge(*pud)); 2388ceb86879SAndi Kleen 2389aee16b3cSJeremy Fitzhardinge pmd = pmd_alloc(mm, pud, addr); 2390aee16b3cSJeremy Fitzhardinge if (!pmd) 2391aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2392aee16b3cSJeremy Fitzhardinge do { 2393aee16b3cSJeremy Fitzhardinge next = pmd_addr_end(addr, end); 2394aee16b3cSJeremy Fitzhardinge err = apply_to_pte_range(mm, pmd, addr, next, fn, data); 2395aee16b3cSJeremy Fitzhardinge if (err) 2396aee16b3cSJeremy Fitzhardinge break; 2397aee16b3cSJeremy Fitzhardinge } while (pmd++, addr = next, addr != end); 2398aee16b3cSJeremy Fitzhardinge return err; 2399aee16b3cSJeremy Fitzhardinge } 2400aee16b3cSJeremy Fitzhardinge 2401aee16b3cSJeremy Fitzhardinge static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd, 2402aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2403aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 2404aee16b3cSJeremy Fitzhardinge { 2405aee16b3cSJeremy Fitzhardinge pud_t *pud; 2406aee16b3cSJeremy Fitzhardinge unsigned long next; 2407aee16b3cSJeremy Fitzhardinge int err; 2408aee16b3cSJeremy Fitzhardinge 2409aee16b3cSJeremy Fitzhardinge pud = pud_alloc(mm, pgd, addr); 2410aee16b3cSJeremy Fitzhardinge if (!pud) 2411aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2412aee16b3cSJeremy Fitzhardinge do { 2413aee16b3cSJeremy Fitzhardinge next = pud_addr_end(addr, end); 2414aee16b3cSJeremy Fitzhardinge err = apply_to_pmd_range(mm, pud, addr, next, fn, data); 2415aee16b3cSJeremy Fitzhardinge if (err) 2416aee16b3cSJeremy Fitzhardinge break; 2417aee16b3cSJeremy Fitzhardinge } while (pud++, addr = next, addr != end); 2418aee16b3cSJeremy Fitzhardinge return err; 2419aee16b3cSJeremy Fitzhardinge } 2420aee16b3cSJeremy Fitzhardinge 2421aee16b3cSJeremy Fitzhardinge /* 2422aee16b3cSJeremy Fitzhardinge * Scan a region of virtual memory, filling in page tables as necessary 2423aee16b3cSJeremy Fitzhardinge * and calling a provided function on each leaf page table. 2424aee16b3cSJeremy Fitzhardinge */ 2425aee16b3cSJeremy Fitzhardinge int apply_to_page_range(struct mm_struct *mm, unsigned long addr, 2426aee16b3cSJeremy Fitzhardinge unsigned long size, pte_fn_t fn, void *data) 2427aee16b3cSJeremy Fitzhardinge { 2428aee16b3cSJeremy Fitzhardinge pgd_t *pgd; 2429aee16b3cSJeremy Fitzhardinge unsigned long next; 243057250a5bSJeremy Fitzhardinge unsigned long end = addr + size; 2431aee16b3cSJeremy Fitzhardinge int err; 2432aee16b3cSJeremy Fitzhardinge 2433aee16b3cSJeremy Fitzhardinge BUG_ON(addr >= end); 2434aee16b3cSJeremy Fitzhardinge pgd = pgd_offset(mm, addr); 2435aee16b3cSJeremy Fitzhardinge do { 2436aee16b3cSJeremy Fitzhardinge next = pgd_addr_end(addr, end); 2437aee16b3cSJeremy Fitzhardinge err = apply_to_pud_range(mm, pgd, addr, next, fn, data); 2438aee16b3cSJeremy Fitzhardinge if (err) 2439aee16b3cSJeremy Fitzhardinge break; 2440aee16b3cSJeremy Fitzhardinge } while (pgd++, addr = next, addr != end); 244157250a5bSJeremy Fitzhardinge 2442aee16b3cSJeremy Fitzhardinge return err; 2443aee16b3cSJeremy Fitzhardinge } 2444aee16b3cSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(apply_to_page_range); 2445aee16b3cSJeremy Fitzhardinge 24461da177e4SLinus Torvalds /* 24478f4e2101SHugh Dickins * handle_pte_fault chooses page fault handler according to an entry 24488f4e2101SHugh Dickins * which was read non-atomically. Before making any commitment, on 24498f4e2101SHugh Dickins * those architectures or configurations (e.g. i386 with PAE) which 2450a335b2e1SRyota Ozaki * might give a mix of unmatched parts, do_swap_page and do_nonlinear_fault 24518f4e2101SHugh Dickins * must check under lock before unmapping the pte and proceeding 24528f4e2101SHugh Dickins * (but do_wp_page is only called after already making such a check; 2453a335b2e1SRyota Ozaki * and do_anonymous_page can safely check later on). 24548f4e2101SHugh Dickins */ 24554c21e2f2SHugh Dickins static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd, 24568f4e2101SHugh Dickins pte_t *page_table, pte_t orig_pte) 24578f4e2101SHugh Dickins { 24588f4e2101SHugh Dickins int same = 1; 24598f4e2101SHugh Dickins #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT) 24608f4e2101SHugh Dickins if (sizeof(pte_t) > sizeof(unsigned long)) { 24614c21e2f2SHugh Dickins spinlock_t *ptl = pte_lockptr(mm, pmd); 24624c21e2f2SHugh Dickins spin_lock(ptl); 24638f4e2101SHugh Dickins same = pte_same(*page_table, orig_pte); 24644c21e2f2SHugh Dickins spin_unlock(ptl); 24658f4e2101SHugh Dickins } 24668f4e2101SHugh Dickins #endif 24678f4e2101SHugh Dickins pte_unmap(page_table); 24688f4e2101SHugh Dickins return same; 24698f4e2101SHugh Dickins } 24708f4e2101SHugh Dickins 24719de455b2SAtsushi Nemoto static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma) 24726aab341eSLinus Torvalds { 24736aab341eSLinus Torvalds /* 24746aab341eSLinus Torvalds * If the source page was a PFN mapping, we don't have 24756aab341eSLinus Torvalds * a "struct page" for it. We do a best-effort copy by 24766aab341eSLinus Torvalds * just copying from the original user address. If that 24776aab341eSLinus Torvalds * fails, we just zero-fill it. Live with it. 24786aab341eSLinus Torvalds */ 24796aab341eSLinus Torvalds if (unlikely(!src)) { 24809b04c5feSCong Wang void *kaddr = kmap_atomic(dst); 24815d2a2dbbSLinus Torvalds void __user *uaddr = (void __user *)(va & PAGE_MASK); 24825d2a2dbbSLinus Torvalds 24835d2a2dbbSLinus Torvalds /* 24845d2a2dbbSLinus Torvalds * This really shouldn't fail, because the page is there 24855d2a2dbbSLinus Torvalds * in the page tables. But it might just be unreadable, 24865d2a2dbbSLinus Torvalds * in which case we just give up and fill the result with 24875d2a2dbbSLinus Torvalds * zeroes. 24885d2a2dbbSLinus Torvalds */ 24895d2a2dbbSLinus Torvalds if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) 24903ecb01dfSJan Beulich clear_page(kaddr); 24919b04c5feSCong Wang kunmap_atomic(kaddr); 2492c4ec7b0dSDmitriy Monakhov flush_dcache_page(dst); 24930ed361deSNick Piggin } else 24949de455b2SAtsushi Nemoto copy_user_highpage(dst, src, va, vma); 24956aab341eSLinus Torvalds } 24966aab341eSLinus Torvalds 24971da177e4SLinus Torvalds /* 24981da177e4SLinus Torvalds * This routine handles present pages, when users try to write 24991da177e4SLinus Torvalds * to a shared page. It is done by copying the page to a new address 25001da177e4SLinus Torvalds * and decrementing the shared-page counter for the old page. 25011da177e4SLinus Torvalds * 25021da177e4SLinus Torvalds * Note that this routine assumes that the protection checks have been 25031da177e4SLinus Torvalds * done by the caller (the low-level page fault routine in most cases). 25041da177e4SLinus Torvalds * Thus we can safely just mark it writable once we've done any necessary 25051da177e4SLinus Torvalds * COW. 25061da177e4SLinus Torvalds * 25071da177e4SLinus Torvalds * We also mark the page dirty at this point even though the page will 25081da177e4SLinus Torvalds * change only once the write actually happens. This avoids a few races, 25091da177e4SLinus Torvalds * and potentially makes it more efficient. 25101da177e4SLinus Torvalds * 25118f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 25128f4e2101SHugh Dickins * but allow concurrent faults), with pte both mapped and locked. 25138f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 25141da177e4SLinus Torvalds */ 25151da177e4SLinus Torvalds static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma, 251665500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 25178f4e2101SHugh Dickins spinlock_t *ptl, pte_t orig_pte) 2518e6219ec8SNamhyung Kim __releases(ptl) 25191da177e4SLinus Torvalds { 2520e5bbe4dfSHugh Dickins struct page *old_page, *new_page; 25211da177e4SLinus Torvalds pte_t entry; 2522b009c024SMichel Lespinasse int ret = 0; 2523a200ee18SPeter Zijlstra int page_mkwrite = 0; 2524d08b3851SPeter Zijlstra struct page *dirty_page = NULL; 25251da177e4SLinus Torvalds 25266aab341eSLinus Torvalds old_page = vm_normal_page(vma, address, orig_pte); 2527251b97f5SPeter Zijlstra if (!old_page) { 2528251b97f5SPeter Zijlstra /* 2529251b97f5SPeter Zijlstra * VM_MIXEDMAP !pfn_valid() case 2530251b97f5SPeter Zijlstra * 2531251b97f5SPeter Zijlstra * We should not cow pages in a shared writeable mapping. 2532251b97f5SPeter Zijlstra * Just mark the pages writable as we can't do any dirty 2533251b97f5SPeter Zijlstra * accounting on raw pfn maps. 2534251b97f5SPeter Zijlstra */ 2535251b97f5SPeter Zijlstra if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 2536251b97f5SPeter Zijlstra (VM_WRITE|VM_SHARED)) 2537251b97f5SPeter Zijlstra goto reuse; 2538920fc356SHugh Dickins goto gotten; 2539251b97f5SPeter Zijlstra } 25401da177e4SLinus Torvalds 2541d08b3851SPeter Zijlstra /* 2542ee6a6457SPeter Zijlstra * Take out anonymous pages first, anonymous shared vmas are 2543ee6a6457SPeter Zijlstra * not dirty accountable. 2544d08b3851SPeter Zijlstra */ 25459a840895SHugh Dickins if (PageAnon(old_page) && !PageKsm(old_page)) { 2546ab967d86SHugh Dickins if (!trylock_page(old_page)) { 2547ab967d86SHugh Dickins page_cache_get(old_page); 2548ab967d86SHugh Dickins pte_unmap_unlock(page_table, ptl); 2549ab967d86SHugh Dickins lock_page(old_page); 2550ab967d86SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, 2551ab967d86SHugh Dickins &ptl); 2552ab967d86SHugh Dickins if (!pte_same(*page_table, orig_pte)) { 2553ab967d86SHugh Dickins unlock_page(old_page); 2554ab967d86SHugh Dickins goto unlock; 2555ab967d86SHugh Dickins } 2556ab967d86SHugh Dickins page_cache_release(old_page); 2557ab967d86SHugh Dickins } 2558b009c024SMichel Lespinasse if (reuse_swap_page(old_page)) { 2559c44b6743SRik van Riel /* 2560c44b6743SRik van Riel * The page is all ours. Move it to our anon_vma so 2561c44b6743SRik van Riel * the rmap code will not search our parent or siblings. 2562c44b6743SRik van Riel * Protected against the rmap code by the page lock. 2563c44b6743SRik van Riel */ 2564c44b6743SRik van Riel page_move_anon_rmap(old_page, vma, address); 2565ee6a6457SPeter Zijlstra unlock_page(old_page); 2566b009c024SMichel Lespinasse goto reuse; 2567b009c024SMichel Lespinasse } 2568b009c024SMichel Lespinasse unlock_page(old_page); 2569ee6a6457SPeter Zijlstra } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 2570d08b3851SPeter Zijlstra (VM_WRITE|VM_SHARED))) { 2571ee6a6457SPeter Zijlstra /* 2572ee6a6457SPeter Zijlstra * Only catch write-faults on shared writable pages, 2573ee6a6457SPeter Zijlstra * read-only shared pages can get COWed by 2574ee6a6457SPeter Zijlstra * get_user_pages(.write=1, .force=1). 2575ee6a6457SPeter Zijlstra */ 25769637a5efSDavid Howells if (vma->vm_ops && vma->vm_ops->page_mkwrite) { 2577c2ec175cSNick Piggin struct vm_fault vmf; 2578c2ec175cSNick Piggin int tmp; 2579c2ec175cSNick Piggin 2580c2ec175cSNick Piggin vmf.virtual_address = (void __user *)(address & 2581c2ec175cSNick Piggin PAGE_MASK); 2582c2ec175cSNick Piggin vmf.pgoff = old_page->index; 2583c2ec175cSNick Piggin vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE; 2584c2ec175cSNick Piggin vmf.page = old_page; 2585c2ec175cSNick Piggin 25869637a5efSDavid Howells /* 25879637a5efSDavid Howells * Notify the address space that the page is about to 25889637a5efSDavid Howells * become writable so that it can prohibit this or wait 25899637a5efSDavid Howells * for the page to get into an appropriate state. 25909637a5efSDavid Howells * 25919637a5efSDavid Howells * We do this without the lock held, so that it can 25929637a5efSDavid Howells * sleep if it needs to. 25939637a5efSDavid Howells */ 25949637a5efSDavid Howells page_cache_get(old_page); 25959637a5efSDavid Howells pte_unmap_unlock(page_table, ptl); 25969637a5efSDavid Howells 2597c2ec175cSNick Piggin tmp = vma->vm_ops->page_mkwrite(vma, &vmf); 2598c2ec175cSNick Piggin if (unlikely(tmp & 2599c2ec175cSNick Piggin (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) { 2600c2ec175cSNick Piggin ret = tmp; 26019637a5efSDavid Howells goto unwritable_page; 2602c2ec175cSNick Piggin } 2603b827e496SNick Piggin if (unlikely(!(tmp & VM_FAULT_LOCKED))) { 2604b827e496SNick Piggin lock_page(old_page); 2605b827e496SNick Piggin if (!old_page->mapping) { 2606b827e496SNick Piggin ret = 0; /* retry the fault */ 2607b827e496SNick Piggin unlock_page(old_page); 2608b827e496SNick Piggin goto unwritable_page; 2609b827e496SNick Piggin } 2610b827e496SNick Piggin } else 2611b827e496SNick Piggin VM_BUG_ON(!PageLocked(old_page)); 26129637a5efSDavid Howells 26139637a5efSDavid Howells /* 26149637a5efSDavid Howells * Since we dropped the lock we need to revalidate 26159637a5efSDavid Howells * the PTE as someone else may have changed it. If 26169637a5efSDavid Howells * they did, we just return, as we can count on the 26179637a5efSDavid Howells * MMU to tell us if they didn't also make it writable. 26189637a5efSDavid Howells */ 26199637a5efSDavid Howells page_table = pte_offset_map_lock(mm, pmd, address, 26209637a5efSDavid Howells &ptl); 2621b827e496SNick Piggin if (!pte_same(*page_table, orig_pte)) { 2622b827e496SNick Piggin unlock_page(old_page); 26239637a5efSDavid Howells goto unlock; 2624b827e496SNick Piggin } 2625a200ee18SPeter Zijlstra 2626a200ee18SPeter Zijlstra page_mkwrite = 1; 26279637a5efSDavid Howells } 2628d08b3851SPeter Zijlstra dirty_page = old_page; 2629d08b3851SPeter Zijlstra get_page(dirty_page); 26309637a5efSDavid Howells 2631251b97f5SPeter Zijlstra reuse: 2632eca35133SBen Collins flush_cache_page(vma, address, pte_pfn(orig_pte)); 263365500d23SHugh Dickins entry = pte_mkyoung(orig_pte); 263465500d23SHugh Dickins entry = maybe_mkwrite(pte_mkdirty(entry), vma); 2635954ffcb3SKAMEZAWA Hiroyuki if (ptep_set_access_flags(vma, address, page_table, entry,1)) 26364b3073e1SRussell King update_mmu_cache(vma, address, page_table); 263772ddc8f7SMichel Lespinasse pte_unmap_unlock(page_table, ptl); 263865500d23SHugh Dickins ret |= VM_FAULT_WRITE; 263972ddc8f7SMichel Lespinasse 264072ddc8f7SMichel Lespinasse if (!dirty_page) 264172ddc8f7SMichel Lespinasse return ret; 264272ddc8f7SMichel Lespinasse 264372ddc8f7SMichel Lespinasse /* 264472ddc8f7SMichel Lespinasse * Yes, Virginia, this is actually required to prevent a race 264572ddc8f7SMichel Lespinasse * with clear_page_dirty_for_io() from clearing the page dirty 264672ddc8f7SMichel Lespinasse * bit after it clear all dirty ptes, but before a racing 264772ddc8f7SMichel Lespinasse * do_wp_page installs a dirty pte. 264872ddc8f7SMichel Lespinasse * 2649a335b2e1SRyota Ozaki * __do_fault is protected similarly. 265072ddc8f7SMichel Lespinasse */ 265172ddc8f7SMichel Lespinasse if (!page_mkwrite) { 265272ddc8f7SMichel Lespinasse wait_on_page_locked(dirty_page); 265372ddc8f7SMichel Lespinasse set_page_dirty_balance(dirty_page, page_mkwrite); 265441c4d25fSJan Kara /* file_update_time outside page_lock */ 265541c4d25fSJan Kara if (vma->vm_file) 265641c4d25fSJan Kara file_update_time(vma->vm_file); 265772ddc8f7SMichel Lespinasse } 265872ddc8f7SMichel Lespinasse put_page(dirty_page); 265972ddc8f7SMichel Lespinasse if (page_mkwrite) { 266072ddc8f7SMichel Lespinasse struct address_space *mapping = dirty_page->mapping; 266172ddc8f7SMichel Lespinasse 266272ddc8f7SMichel Lespinasse set_page_dirty(dirty_page); 266372ddc8f7SMichel Lespinasse unlock_page(dirty_page); 266472ddc8f7SMichel Lespinasse page_cache_release(dirty_page); 266572ddc8f7SMichel Lespinasse if (mapping) { 266672ddc8f7SMichel Lespinasse /* 266772ddc8f7SMichel Lespinasse * Some device drivers do not set page.mapping 266872ddc8f7SMichel Lespinasse * but still dirty their pages 266972ddc8f7SMichel Lespinasse */ 267072ddc8f7SMichel Lespinasse balance_dirty_pages_ratelimited(mapping); 267172ddc8f7SMichel Lespinasse } 267272ddc8f7SMichel Lespinasse } 267372ddc8f7SMichel Lespinasse 267472ddc8f7SMichel Lespinasse return ret; 26751da177e4SLinus Torvalds } 26761da177e4SLinus Torvalds 26771da177e4SLinus Torvalds /* 26781da177e4SLinus Torvalds * Ok, we need to copy. Oh, well.. 26791da177e4SLinus Torvalds */ 26801da177e4SLinus Torvalds page_cache_get(old_page); 2681920fc356SHugh Dickins gotten: 26828f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 26831da177e4SLinus Torvalds 26841da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 268565500d23SHugh Dickins goto oom; 2686a13ea5b7SHugh Dickins 268762eede62SHugh Dickins if (is_zero_pfn(pte_pfn(orig_pte))) { 2688a13ea5b7SHugh Dickins new_page = alloc_zeroed_user_highpage_movable(vma, address); 2689a13ea5b7SHugh Dickins if (!new_page) 2690a13ea5b7SHugh Dickins goto oom; 2691a13ea5b7SHugh Dickins } else { 2692769848c0SMel Gorman new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address); 26931da177e4SLinus Torvalds if (!new_page) 269465500d23SHugh Dickins goto oom; 2695a13ea5b7SHugh Dickins cow_user_page(new_page, old_page, address, vma); 2696a13ea5b7SHugh Dickins } 2697a13ea5b7SHugh Dickins __SetPageUptodate(new_page); 2698a13ea5b7SHugh Dickins 26992c26fdd7SKAMEZAWA Hiroyuki if (mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL)) 27008a9f3ccdSBalbir Singh goto oom_free_new; 27018a9f3ccdSBalbir Singh 27021da177e4SLinus Torvalds /* 27031da177e4SLinus Torvalds * Re-check the pte - we dropped the lock 27041da177e4SLinus Torvalds */ 27058f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 270665500d23SHugh Dickins if (likely(pte_same(*page_table, orig_pte))) { 2707920fc356SHugh Dickins if (old_page) { 27084294621fSHugh Dickins if (!PageAnon(old_page)) { 270934e55232SKAMEZAWA Hiroyuki dec_mm_counter_fast(mm, MM_FILEPAGES); 271034e55232SKAMEZAWA Hiroyuki inc_mm_counter_fast(mm, MM_ANONPAGES); 27114294621fSHugh Dickins } 2712920fc356SHugh Dickins } else 271334e55232SKAMEZAWA Hiroyuki inc_mm_counter_fast(mm, MM_ANONPAGES); 2714eca35133SBen Collins flush_cache_page(vma, address, pte_pfn(orig_pte)); 271565500d23SHugh Dickins entry = mk_pte(new_page, vma->vm_page_prot); 271665500d23SHugh Dickins entry = maybe_mkwrite(pte_mkdirty(entry), vma); 27174ce072f1SSiddha, Suresh B /* 27184ce072f1SSiddha, Suresh B * Clear the pte entry and flush it first, before updating the 27194ce072f1SSiddha, Suresh B * pte with the new entry. This will avoid a race condition 27204ce072f1SSiddha, Suresh B * seen in the presence of one thread doing SMC and another 27214ce072f1SSiddha, Suresh B * thread doing COW. 27224ce072f1SSiddha, Suresh B */ 2723828502d3SIzik Eidus ptep_clear_flush(vma, address, page_table); 27249617d95eSNick Piggin page_add_new_anon_rmap(new_page, vma, address); 2725828502d3SIzik Eidus /* 2726828502d3SIzik Eidus * We call the notify macro here because, when using secondary 2727828502d3SIzik Eidus * mmu page tables (such as kvm shadow page tables), we want the 2728828502d3SIzik Eidus * new page to be mapped directly into the secondary page table. 2729828502d3SIzik Eidus */ 2730828502d3SIzik Eidus set_pte_at_notify(mm, address, page_table, entry); 27314b3073e1SRussell King update_mmu_cache(vma, address, page_table); 2732945754a1SNick Piggin if (old_page) { 2733945754a1SNick Piggin /* 2734945754a1SNick Piggin * Only after switching the pte to the new page may 2735945754a1SNick Piggin * we remove the mapcount here. Otherwise another 2736945754a1SNick Piggin * process may come and find the rmap count decremented 2737945754a1SNick Piggin * before the pte is switched to the new page, and 2738945754a1SNick Piggin * "reuse" the old page writing into it while our pte 2739945754a1SNick Piggin * here still points into it and can be read by other 2740945754a1SNick Piggin * threads. 2741945754a1SNick Piggin * 2742945754a1SNick Piggin * The critical issue is to order this 2743945754a1SNick Piggin * page_remove_rmap with the ptp_clear_flush above. 2744945754a1SNick Piggin * Those stores are ordered by (if nothing else,) 2745945754a1SNick Piggin * the barrier present in the atomic_add_negative 2746945754a1SNick Piggin * in page_remove_rmap. 2747945754a1SNick Piggin * 2748945754a1SNick Piggin * Then the TLB flush in ptep_clear_flush ensures that 2749945754a1SNick Piggin * no process can access the old page before the 2750945754a1SNick Piggin * decremented mapcount is visible. And the old page 2751945754a1SNick Piggin * cannot be reused until after the decremented 2752945754a1SNick Piggin * mapcount is visible. So transitively, TLBs to 2753945754a1SNick Piggin * old page will be flushed before it can be reused. 2754945754a1SNick Piggin */ 2755edc315fdSHugh Dickins page_remove_rmap(old_page); 2756945754a1SNick Piggin } 2757945754a1SNick Piggin 27581da177e4SLinus Torvalds /* Free the old page.. */ 27591da177e4SLinus Torvalds new_page = old_page; 2760f33ea7f4SNick Piggin ret |= VM_FAULT_WRITE; 27618a9f3ccdSBalbir Singh } else 27628a9f3ccdSBalbir Singh mem_cgroup_uncharge_page(new_page); 27638a9f3ccdSBalbir Singh 2764920fc356SHugh Dickins if (new_page) 27651da177e4SLinus Torvalds page_cache_release(new_page); 276665500d23SHugh Dickins unlock: 27678f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 2768e15f8c01SMichel Lespinasse if (old_page) { 2769e15f8c01SMichel Lespinasse /* 2770e15f8c01SMichel Lespinasse * Don't let another task, with possibly unlocked vma, 2771e15f8c01SMichel Lespinasse * keep the mlocked page. 2772e15f8c01SMichel Lespinasse */ 2773e15f8c01SMichel Lespinasse if ((ret & VM_FAULT_WRITE) && (vma->vm_flags & VM_LOCKED)) { 2774e15f8c01SMichel Lespinasse lock_page(old_page); /* LRU manipulation */ 2775e15f8c01SMichel Lespinasse munlock_vma_page(old_page); 2776e15f8c01SMichel Lespinasse unlock_page(old_page); 2777e15f8c01SMichel Lespinasse } 2778e15f8c01SMichel Lespinasse page_cache_release(old_page); 2779e15f8c01SMichel Lespinasse } 2780f33ea7f4SNick Piggin return ret; 27818a9f3ccdSBalbir Singh oom_free_new: 27826dbf6d3bSHugh Dickins page_cache_release(new_page); 278365500d23SHugh Dickins oom: 2784b827e496SNick Piggin if (old_page) { 2785b827e496SNick Piggin if (page_mkwrite) { 2786b827e496SNick Piggin unlock_page(old_page); 27871da177e4SLinus Torvalds page_cache_release(old_page); 2788b827e496SNick Piggin } 2789b827e496SNick Piggin page_cache_release(old_page); 2790b827e496SNick Piggin } 27911da177e4SLinus Torvalds return VM_FAULT_OOM; 27929637a5efSDavid Howells 27939637a5efSDavid Howells unwritable_page: 27949637a5efSDavid Howells page_cache_release(old_page); 2795c2ec175cSNick Piggin return ret; 27961da177e4SLinus Torvalds } 27971da177e4SLinus Torvalds 279897a89413SPeter Zijlstra static void unmap_mapping_range_vma(struct vm_area_struct *vma, 27991da177e4SLinus Torvalds unsigned long start_addr, unsigned long end_addr, 28001da177e4SLinus Torvalds struct zap_details *details) 28011da177e4SLinus Torvalds { 2802f5cc4eefSAl Viro zap_page_range_single(vma, start_addr, end_addr - start_addr, details); 28031da177e4SLinus Torvalds } 28041da177e4SLinus Torvalds 28051da177e4SLinus Torvalds static inline void unmap_mapping_range_tree(struct prio_tree_root *root, 28061da177e4SLinus Torvalds struct zap_details *details) 28071da177e4SLinus Torvalds { 28081da177e4SLinus Torvalds struct vm_area_struct *vma; 28091da177e4SLinus Torvalds struct prio_tree_iter iter; 28101da177e4SLinus Torvalds pgoff_t vba, vea, zba, zea; 28111da177e4SLinus Torvalds 28121da177e4SLinus Torvalds vma_prio_tree_foreach(vma, &iter, root, 28131da177e4SLinus Torvalds details->first_index, details->last_index) { 28141da177e4SLinus Torvalds 28151da177e4SLinus Torvalds vba = vma->vm_pgoff; 28161da177e4SLinus Torvalds vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1; 28171da177e4SLinus Torvalds /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */ 28181da177e4SLinus Torvalds zba = details->first_index; 28191da177e4SLinus Torvalds if (zba < vba) 28201da177e4SLinus Torvalds zba = vba; 28211da177e4SLinus Torvalds zea = details->last_index; 28221da177e4SLinus Torvalds if (zea > vea) 28231da177e4SLinus Torvalds zea = vea; 28241da177e4SLinus Torvalds 282597a89413SPeter Zijlstra unmap_mapping_range_vma(vma, 28261da177e4SLinus Torvalds ((zba - vba) << PAGE_SHIFT) + vma->vm_start, 28271da177e4SLinus Torvalds ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start, 282897a89413SPeter Zijlstra details); 28291da177e4SLinus Torvalds } 28301da177e4SLinus Torvalds } 28311da177e4SLinus Torvalds 28321da177e4SLinus Torvalds static inline void unmap_mapping_range_list(struct list_head *head, 28331da177e4SLinus Torvalds struct zap_details *details) 28341da177e4SLinus Torvalds { 28351da177e4SLinus Torvalds struct vm_area_struct *vma; 28361da177e4SLinus Torvalds 28371da177e4SLinus Torvalds /* 28381da177e4SLinus Torvalds * In nonlinear VMAs there is no correspondence between virtual address 28391da177e4SLinus Torvalds * offset and file offset. So we must perform an exhaustive search 28401da177e4SLinus Torvalds * across *all* the pages in each nonlinear VMA, not just the pages 28411da177e4SLinus Torvalds * whose virtual address lies outside the file truncation point. 28421da177e4SLinus Torvalds */ 28431da177e4SLinus Torvalds list_for_each_entry(vma, head, shared.vm_set.list) { 28441da177e4SLinus Torvalds details->nonlinear_vma = vma; 284597a89413SPeter Zijlstra unmap_mapping_range_vma(vma, vma->vm_start, vma->vm_end, details); 28461da177e4SLinus Torvalds } 28471da177e4SLinus Torvalds } 28481da177e4SLinus Torvalds 28491da177e4SLinus Torvalds /** 285072fd4a35SRobert 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. 28513d41088fSMartin Waitz * @mapping: the address space containing mmaps to be unmapped. 28521da177e4SLinus Torvalds * @holebegin: byte in first page to unmap, relative to the start of 28531da177e4SLinus Torvalds * the underlying file. This will be rounded down to a PAGE_SIZE 285425d9e2d1Snpiggin@suse.de * boundary. Note that this is different from truncate_pagecache(), which 28551da177e4SLinus Torvalds * must keep the partial page. In contrast, we must get rid of 28561da177e4SLinus Torvalds * partial pages. 28571da177e4SLinus Torvalds * @holelen: size of prospective hole in bytes. This will be rounded 28581da177e4SLinus Torvalds * up to a PAGE_SIZE boundary. A holelen of zero truncates to the 28591da177e4SLinus Torvalds * end of the file. 28601da177e4SLinus Torvalds * @even_cows: 1 when truncating a file, unmap even private COWed pages; 28611da177e4SLinus Torvalds * but 0 when invalidating pagecache, don't throw away private data. 28621da177e4SLinus Torvalds */ 28631da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping, 28641da177e4SLinus Torvalds loff_t const holebegin, loff_t const holelen, int even_cows) 28651da177e4SLinus Torvalds { 28661da177e4SLinus Torvalds struct zap_details details; 28671da177e4SLinus Torvalds pgoff_t hba = holebegin >> PAGE_SHIFT; 28681da177e4SLinus Torvalds pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 28691da177e4SLinus Torvalds 28701da177e4SLinus Torvalds /* Check for overflow. */ 28711da177e4SLinus Torvalds if (sizeof(holelen) > sizeof(hlen)) { 28721da177e4SLinus Torvalds long long holeend = 28731da177e4SLinus Torvalds (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 28741da177e4SLinus Torvalds if (holeend & ~(long long)ULONG_MAX) 28751da177e4SLinus Torvalds hlen = ULONG_MAX - hba + 1; 28761da177e4SLinus Torvalds } 28771da177e4SLinus Torvalds 28781da177e4SLinus Torvalds details.check_mapping = even_cows? NULL: mapping; 28791da177e4SLinus Torvalds details.nonlinear_vma = NULL; 28801da177e4SLinus Torvalds details.first_index = hba; 28811da177e4SLinus Torvalds details.last_index = hba + hlen - 1; 28821da177e4SLinus Torvalds if (details.last_index < details.first_index) 28831da177e4SLinus Torvalds details.last_index = ULONG_MAX; 28841da177e4SLinus Torvalds 288597a89413SPeter Zijlstra 28863d48ae45SPeter Zijlstra mutex_lock(&mapping->i_mmap_mutex); 28871da177e4SLinus Torvalds if (unlikely(!prio_tree_empty(&mapping->i_mmap))) 28881da177e4SLinus Torvalds unmap_mapping_range_tree(&mapping->i_mmap, &details); 28891da177e4SLinus Torvalds if (unlikely(!list_empty(&mapping->i_mmap_nonlinear))) 28901da177e4SLinus Torvalds unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details); 28913d48ae45SPeter Zijlstra mutex_unlock(&mapping->i_mmap_mutex); 28921da177e4SLinus Torvalds } 28931da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range); 28941da177e4SLinus Torvalds 28951da177e4SLinus Torvalds /* 28968f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 28978f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 28988f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 28991da177e4SLinus Torvalds */ 290065500d23SHugh Dickins static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma, 290165500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 290230c9f3a9SLinus Torvalds unsigned int flags, pte_t orig_pte) 29031da177e4SLinus Torvalds { 29048f4e2101SHugh Dickins spinlock_t *ptl; 29054969c119SAndrea Arcangeli struct page *page, *swapcache = NULL; 290665500d23SHugh Dickins swp_entry_t entry; 29071da177e4SLinus Torvalds pte_t pte; 2908d065bd81SMichel Lespinasse int locked; 290956039efaSKAMEZAWA Hiroyuki struct mem_cgroup *ptr; 2910ad8c2ee8SRik van Riel int exclusive = 0; 291183c54070SNick Piggin int ret = 0; 29121da177e4SLinus Torvalds 29134c21e2f2SHugh Dickins if (!pte_unmap_same(mm, pmd, page_table, orig_pte)) 29148f4e2101SHugh Dickins goto out; 291565500d23SHugh Dickins 291665500d23SHugh Dickins entry = pte_to_swp_entry(orig_pte); 2917d1737fdbSAndi Kleen if (unlikely(non_swap_entry(entry))) { 29180697212aSChristoph Lameter if (is_migration_entry(entry)) { 29190697212aSChristoph Lameter migration_entry_wait(mm, pmd, address); 2920d1737fdbSAndi Kleen } else if (is_hwpoison_entry(entry)) { 2921d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 2922d1737fdbSAndi Kleen } else { 2923d1737fdbSAndi Kleen print_bad_pte(vma, address, orig_pte, NULL); 2924d99be1a8SHugh Dickins ret = VM_FAULT_SIGBUS; 2925d1737fdbSAndi Kleen } 29260697212aSChristoph Lameter goto out; 29270697212aSChristoph Lameter } 29280ff92245SShailabh Nagar delayacct_set_flag(DELAYACCT_PF_SWAPIN); 29291da177e4SLinus Torvalds page = lookup_swap_cache(entry); 29301da177e4SLinus Torvalds if (!page) { 293102098feaSHugh Dickins page = swapin_readahead(entry, 293202098feaSHugh Dickins GFP_HIGHUSER_MOVABLE, vma, address); 29331da177e4SLinus Torvalds if (!page) { 29341da177e4SLinus Torvalds /* 29358f4e2101SHugh Dickins * Back out if somebody else faulted in this pte 29368f4e2101SHugh Dickins * while we released the pte lock. 29371da177e4SLinus Torvalds */ 29388f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 29391da177e4SLinus Torvalds if (likely(pte_same(*page_table, orig_pte))) 29401da177e4SLinus Torvalds ret = VM_FAULT_OOM; 29410ff92245SShailabh Nagar delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 294265500d23SHugh Dickins goto unlock; 29431da177e4SLinus Torvalds } 29441da177e4SLinus Torvalds 29451da177e4SLinus Torvalds /* Had to read the page from swap area: Major fault */ 29461da177e4SLinus Torvalds ret = VM_FAULT_MAJOR; 2947f8891e5eSChristoph Lameter count_vm_event(PGMAJFAULT); 2948456f998eSYing Han mem_cgroup_count_vm_event(mm, PGMAJFAULT); 2949d1737fdbSAndi Kleen } else if (PageHWPoison(page)) { 295071f72525SWu Fengguang /* 295171f72525SWu Fengguang * hwpoisoned dirty swapcache pages are kept for killing 295271f72525SWu Fengguang * owner processes (which may be unknown at hwpoison time) 295371f72525SWu Fengguang */ 2954d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 2955d1737fdbSAndi Kleen delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 29564779cb31SAndi Kleen goto out_release; 29571da177e4SLinus Torvalds } 29581da177e4SLinus Torvalds 2959d065bd81SMichel Lespinasse locked = lock_page_or_retry(page, mm, flags); 2960e709ffd6SRik van Riel 296120a1022dSBalbir Singh delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 2962d065bd81SMichel Lespinasse if (!locked) { 2963d065bd81SMichel Lespinasse ret |= VM_FAULT_RETRY; 2964d065bd81SMichel Lespinasse goto out_release; 2965d065bd81SMichel Lespinasse } 29661da177e4SLinus Torvalds 29674969c119SAndrea Arcangeli /* 296831c4a3d3SHugh Dickins * Make sure try_to_free_swap or reuse_swap_page or swapoff did not 296931c4a3d3SHugh Dickins * release the swapcache from under us. The page pin, and pte_same 297031c4a3d3SHugh Dickins * test below, are not enough to exclude that. Even if it is still 297131c4a3d3SHugh Dickins * swapcache, we need to check that the page's swap has not changed. 29724969c119SAndrea Arcangeli */ 297331c4a3d3SHugh Dickins if (unlikely(!PageSwapCache(page) || page_private(page) != entry.val)) 29744969c119SAndrea Arcangeli goto out_page; 29754969c119SAndrea Arcangeli 29764969c119SAndrea Arcangeli if (ksm_might_need_to_copy(page, vma, address)) { 29774969c119SAndrea Arcangeli swapcache = page; 29784969c119SAndrea Arcangeli page = ksm_does_need_to_copy(page, vma, address); 29794969c119SAndrea Arcangeli 29804969c119SAndrea Arcangeli if (unlikely(!page)) { 29815ad64688SHugh Dickins ret = VM_FAULT_OOM; 29824969c119SAndrea Arcangeli page = swapcache; 29834969c119SAndrea Arcangeli swapcache = NULL; 29844969c119SAndrea Arcangeli goto out_page; 29854969c119SAndrea Arcangeli } 29865ad64688SHugh Dickins } 29875ad64688SHugh Dickins 29882c26fdd7SKAMEZAWA Hiroyuki if (mem_cgroup_try_charge_swapin(mm, page, GFP_KERNEL, &ptr)) { 2989073e587eSKAMEZAWA Hiroyuki ret = VM_FAULT_OOM; 2990bc43f75cSJohannes Weiner goto out_page; 2991073e587eSKAMEZAWA Hiroyuki } 2992073e587eSKAMEZAWA Hiroyuki 29931da177e4SLinus Torvalds /* 29948f4e2101SHugh Dickins * Back out if somebody else already faulted in this pte. 29951da177e4SLinus Torvalds */ 29968f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 29979e9bef07SHugh Dickins if (unlikely(!pte_same(*page_table, orig_pte))) 2998b8107480SKirill Korotaev goto out_nomap; 2999b8107480SKirill Korotaev 3000b8107480SKirill Korotaev if (unlikely(!PageUptodate(page))) { 3001b8107480SKirill Korotaev ret = VM_FAULT_SIGBUS; 3002b8107480SKirill Korotaev goto out_nomap; 30031da177e4SLinus Torvalds } 30041da177e4SLinus Torvalds 30058c7c6e34SKAMEZAWA Hiroyuki /* 30068c7c6e34SKAMEZAWA Hiroyuki * The page isn't present yet, go ahead with the fault. 30078c7c6e34SKAMEZAWA Hiroyuki * 30088c7c6e34SKAMEZAWA Hiroyuki * Be careful about the sequence of operations here. 30098c7c6e34SKAMEZAWA Hiroyuki * To get its accounting right, reuse_swap_page() must be called 30108c7c6e34SKAMEZAWA Hiroyuki * while the page is counted on swap but not yet in mapcount i.e. 30118c7c6e34SKAMEZAWA Hiroyuki * before page_add_anon_rmap() and swap_free(); try_to_free_swap() 30128c7c6e34SKAMEZAWA Hiroyuki * must be called after the swap_free(), or it will never succeed. 301303f3c433SKAMEZAWA Hiroyuki * Because delete_from_swap_page() may be called by reuse_swap_page(), 301403f3c433SKAMEZAWA Hiroyuki * mem_cgroup_commit_charge_swapin() may not be able to find swp_entry 301503f3c433SKAMEZAWA Hiroyuki * in page->private. In this case, a record in swap_cgroup is silently 301603f3c433SKAMEZAWA Hiroyuki * discarded at swap_free(). 30178c7c6e34SKAMEZAWA Hiroyuki */ 30181da177e4SLinus Torvalds 301934e55232SKAMEZAWA Hiroyuki inc_mm_counter_fast(mm, MM_ANONPAGES); 3020b084d435SKAMEZAWA Hiroyuki dec_mm_counter_fast(mm, MM_SWAPENTS); 30211da177e4SLinus Torvalds pte = mk_pte(page, vma->vm_page_prot); 302230c9f3a9SLinus Torvalds if ((flags & FAULT_FLAG_WRITE) && reuse_swap_page(page)) { 30231da177e4SLinus Torvalds pte = maybe_mkwrite(pte_mkdirty(pte), vma); 302430c9f3a9SLinus Torvalds flags &= ~FAULT_FLAG_WRITE; 30259a5b489bSAndrea Arcangeli ret |= VM_FAULT_WRITE; 3026ad8c2ee8SRik van Riel exclusive = 1; 30271da177e4SLinus Torvalds } 30281da177e4SLinus Torvalds flush_icache_page(vma, page); 30291da177e4SLinus Torvalds set_pte_at(mm, address, page_table, pte); 3030ad8c2ee8SRik van Riel do_page_add_anon_rmap(page, vma, address, exclusive); 303103f3c433SKAMEZAWA Hiroyuki /* It's better to call commit-charge after rmap is established */ 303203f3c433SKAMEZAWA Hiroyuki mem_cgroup_commit_charge_swapin(page, ptr); 30331da177e4SLinus Torvalds 3034c475a8abSHugh Dickins swap_free(entry); 3035b291f000SNick Piggin if (vm_swap_full() || (vma->vm_flags & VM_LOCKED) || PageMlocked(page)) 3036a2c43eedSHugh Dickins try_to_free_swap(page); 3037c475a8abSHugh Dickins unlock_page(page); 30384969c119SAndrea Arcangeli if (swapcache) { 30394969c119SAndrea Arcangeli /* 30404969c119SAndrea Arcangeli * Hold the lock to avoid the swap entry to be reused 30414969c119SAndrea Arcangeli * until we take the PT lock for the pte_same() check 30424969c119SAndrea Arcangeli * (to avoid false positives from pte_same). For 30434969c119SAndrea Arcangeli * further safety release the lock after the swap_free 30444969c119SAndrea Arcangeli * so that the swap count won't change under a 30454969c119SAndrea Arcangeli * parallel locked swapcache. 30464969c119SAndrea Arcangeli */ 30474969c119SAndrea Arcangeli unlock_page(swapcache); 30484969c119SAndrea Arcangeli page_cache_release(swapcache); 30494969c119SAndrea Arcangeli } 3050c475a8abSHugh Dickins 305130c9f3a9SLinus Torvalds if (flags & FAULT_FLAG_WRITE) { 305261469f1dSHugh Dickins ret |= do_wp_page(mm, vma, address, page_table, pmd, ptl, pte); 305361469f1dSHugh Dickins if (ret & VM_FAULT_ERROR) 305461469f1dSHugh Dickins ret &= VM_FAULT_ERROR; 30551da177e4SLinus Torvalds goto out; 30561da177e4SLinus Torvalds } 30571da177e4SLinus Torvalds 30581da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 30594b3073e1SRussell King update_mmu_cache(vma, address, page_table); 306065500d23SHugh Dickins unlock: 30618f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 30621da177e4SLinus Torvalds out: 30631da177e4SLinus Torvalds return ret; 3064b8107480SKirill Korotaev out_nomap: 30657a81b88cSKAMEZAWA Hiroyuki mem_cgroup_cancel_charge_swapin(ptr); 30668f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 3067bc43f75cSJohannes Weiner out_page: 3068b8107480SKirill Korotaev unlock_page(page); 30694779cb31SAndi Kleen out_release: 3070b8107480SKirill Korotaev page_cache_release(page); 30714969c119SAndrea Arcangeli if (swapcache) { 30724969c119SAndrea Arcangeli unlock_page(swapcache); 30734969c119SAndrea Arcangeli page_cache_release(swapcache); 30744969c119SAndrea Arcangeli } 307565500d23SHugh Dickins return ret; 30761da177e4SLinus Torvalds } 30771da177e4SLinus Torvalds 30781da177e4SLinus Torvalds /* 30798ca3eb08SLuck, Tony * This is like a special single-page "expand_{down|up}wards()", 30808ca3eb08SLuck, Tony * except we must first make sure that 'address{-|+}PAGE_SIZE' 3081320b2b8dSLinus Torvalds * doesn't hit another vma. 3082320b2b8dSLinus Torvalds */ 3083320b2b8dSLinus Torvalds static inline int check_stack_guard_page(struct vm_area_struct *vma, unsigned long address) 3084320b2b8dSLinus Torvalds { 3085320b2b8dSLinus Torvalds address &= PAGE_MASK; 3086320b2b8dSLinus Torvalds if ((vma->vm_flags & VM_GROWSDOWN) && address == vma->vm_start) { 30870e8e50e2SLinus Torvalds struct vm_area_struct *prev = vma->vm_prev; 3088320b2b8dSLinus Torvalds 30890e8e50e2SLinus Torvalds /* 30900e8e50e2SLinus Torvalds * Is there a mapping abutting this one below? 30910e8e50e2SLinus Torvalds * 30920e8e50e2SLinus Torvalds * That's only ok if it's the same stack mapping 30930e8e50e2SLinus Torvalds * that has gotten split.. 30940e8e50e2SLinus Torvalds */ 30950e8e50e2SLinus Torvalds if (prev && prev->vm_end == address) 30960e8e50e2SLinus Torvalds return prev->vm_flags & VM_GROWSDOWN ? 0 : -ENOMEM; 30970e8e50e2SLinus Torvalds 3098d05f3169SMichal Hocko expand_downwards(vma, address - PAGE_SIZE); 3099320b2b8dSLinus Torvalds } 31008ca3eb08SLuck, Tony if ((vma->vm_flags & VM_GROWSUP) && address + PAGE_SIZE == vma->vm_end) { 31018ca3eb08SLuck, Tony struct vm_area_struct *next = vma->vm_next; 31028ca3eb08SLuck, Tony 31038ca3eb08SLuck, Tony /* As VM_GROWSDOWN but s/below/above/ */ 31048ca3eb08SLuck, Tony if (next && next->vm_start == address + PAGE_SIZE) 31058ca3eb08SLuck, Tony return next->vm_flags & VM_GROWSUP ? 0 : -ENOMEM; 31068ca3eb08SLuck, Tony 31078ca3eb08SLuck, Tony expand_upwards(vma, address + PAGE_SIZE); 31088ca3eb08SLuck, Tony } 3109320b2b8dSLinus Torvalds return 0; 3110320b2b8dSLinus Torvalds } 3111320b2b8dSLinus Torvalds 3112320b2b8dSLinus Torvalds /* 31138f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 31148f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 31158f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 31161da177e4SLinus Torvalds */ 311765500d23SHugh Dickins static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma, 311865500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 311930c9f3a9SLinus Torvalds unsigned int flags) 31201da177e4SLinus Torvalds { 31218f4e2101SHugh Dickins struct page *page; 31228f4e2101SHugh Dickins spinlock_t *ptl; 31231da177e4SLinus Torvalds pte_t entry; 31241da177e4SLinus Torvalds 31255528f913SLinus Torvalds pte_unmap(page_table); 3126320b2b8dSLinus Torvalds 312711ac5524SLinus Torvalds /* Check if we need to add a guard page to the stack */ 312811ac5524SLinus Torvalds if (check_stack_guard_page(vma, address) < 0) 312911ac5524SLinus Torvalds return VM_FAULT_SIGBUS; 313011ac5524SLinus Torvalds 313111ac5524SLinus Torvalds /* Use the zero-page for reads */ 313262eede62SHugh Dickins if (!(flags & FAULT_FLAG_WRITE)) { 313362eede62SHugh Dickins entry = pte_mkspecial(pfn_pte(my_zero_pfn(address), 313462eede62SHugh Dickins vma->vm_page_prot)); 313511ac5524SLinus Torvalds page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 3136a13ea5b7SHugh Dickins if (!pte_none(*page_table)) 3137a13ea5b7SHugh Dickins goto unlock; 3138a13ea5b7SHugh Dickins goto setpte; 3139a13ea5b7SHugh Dickins } 3140a13ea5b7SHugh Dickins 31411da177e4SLinus Torvalds /* Allocate our own private page. */ 31421da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 314365500d23SHugh Dickins goto oom; 3144769848c0SMel Gorman page = alloc_zeroed_user_highpage_movable(vma, address); 31451da177e4SLinus Torvalds if (!page) 314665500d23SHugh Dickins goto oom; 31470ed361deSNick Piggin __SetPageUptodate(page); 31481da177e4SLinus Torvalds 31492c26fdd7SKAMEZAWA Hiroyuki if (mem_cgroup_newpage_charge(page, mm, GFP_KERNEL)) 31508a9f3ccdSBalbir Singh goto oom_free_page; 31518a9f3ccdSBalbir Singh 315265500d23SHugh Dickins entry = mk_pte(page, vma->vm_page_prot); 31531ac0cb5dSHugh Dickins if (vma->vm_flags & VM_WRITE) 31541ac0cb5dSHugh Dickins entry = pte_mkwrite(pte_mkdirty(entry)); 31558f4e2101SHugh Dickins 31568f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 31571c2fb7a4SAndrea Arcangeli if (!pte_none(*page_table)) 31588f4e2101SHugh Dickins goto release; 31599ba69294SHugh Dickins 316034e55232SKAMEZAWA Hiroyuki inc_mm_counter_fast(mm, MM_ANONPAGES); 31619617d95eSNick Piggin page_add_new_anon_rmap(page, vma, address); 3162a13ea5b7SHugh Dickins setpte: 316365500d23SHugh Dickins set_pte_at(mm, address, page_table, entry); 31641da177e4SLinus Torvalds 31651da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 31664b3073e1SRussell King update_mmu_cache(vma, address, page_table); 316765500d23SHugh Dickins unlock: 31688f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 316983c54070SNick Piggin return 0; 31708f4e2101SHugh Dickins release: 31718a9f3ccdSBalbir Singh mem_cgroup_uncharge_page(page); 31728f4e2101SHugh Dickins page_cache_release(page); 31738f4e2101SHugh Dickins goto unlock; 31748a9f3ccdSBalbir Singh oom_free_page: 31756dbf6d3bSHugh Dickins page_cache_release(page); 317665500d23SHugh Dickins oom: 31771da177e4SLinus Torvalds return VM_FAULT_OOM; 31781da177e4SLinus Torvalds } 31791da177e4SLinus Torvalds 31801da177e4SLinus Torvalds /* 318154cb8821SNick Piggin * __do_fault() tries to create a new page mapping. It aggressively 31821da177e4SLinus Torvalds * tries to share with existing pages, but makes a separate copy if 318354cb8821SNick Piggin * the FAULT_FLAG_WRITE is set in the flags parameter in order to avoid 318454cb8821SNick Piggin * the next page fault. 31851da177e4SLinus Torvalds * 31861da177e4SLinus Torvalds * As this is called only for pages that do not currently exist, we 31871da177e4SLinus Torvalds * do not need to flush old virtual caches or the TLB. 31881da177e4SLinus Torvalds * 31898f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 319016abfa08SHugh Dickins * but allow concurrent faults), and pte neither mapped nor locked. 31918f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 31921da177e4SLinus Torvalds */ 319354cb8821SNick Piggin static int __do_fault(struct mm_struct *mm, struct vm_area_struct *vma, 319416abfa08SHugh Dickins unsigned long address, pmd_t *pmd, 319554cb8821SNick Piggin pgoff_t pgoff, unsigned int flags, pte_t orig_pte) 31961da177e4SLinus Torvalds { 319716abfa08SHugh Dickins pte_t *page_table; 31988f4e2101SHugh Dickins spinlock_t *ptl; 3199d0217ac0SNick Piggin struct page *page; 32001d65f86dSKAMEZAWA Hiroyuki struct page *cow_page; 32011da177e4SLinus Torvalds pte_t entry; 32021da177e4SLinus Torvalds int anon = 0; 3203d08b3851SPeter Zijlstra struct page *dirty_page = NULL; 3204d0217ac0SNick Piggin struct vm_fault vmf; 3205d0217ac0SNick Piggin int ret; 3206a200ee18SPeter Zijlstra int page_mkwrite = 0; 320754cb8821SNick Piggin 32081d65f86dSKAMEZAWA Hiroyuki /* 32091d65f86dSKAMEZAWA Hiroyuki * If we do COW later, allocate page befor taking lock_page() 32101d65f86dSKAMEZAWA Hiroyuki * on the file cache page. This will reduce lock holding time. 32111d65f86dSKAMEZAWA Hiroyuki */ 32121d65f86dSKAMEZAWA Hiroyuki if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) { 32131d65f86dSKAMEZAWA Hiroyuki 32141d65f86dSKAMEZAWA Hiroyuki if (unlikely(anon_vma_prepare(vma))) 32151d65f86dSKAMEZAWA Hiroyuki return VM_FAULT_OOM; 32161d65f86dSKAMEZAWA Hiroyuki 32171d65f86dSKAMEZAWA Hiroyuki cow_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address); 32181d65f86dSKAMEZAWA Hiroyuki if (!cow_page) 32191d65f86dSKAMEZAWA Hiroyuki return VM_FAULT_OOM; 32201d65f86dSKAMEZAWA Hiroyuki 32211d65f86dSKAMEZAWA Hiroyuki if (mem_cgroup_newpage_charge(cow_page, mm, GFP_KERNEL)) { 32221d65f86dSKAMEZAWA Hiroyuki page_cache_release(cow_page); 32231d65f86dSKAMEZAWA Hiroyuki return VM_FAULT_OOM; 32241d65f86dSKAMEZAWA Hiroyuki } 32251d65f86dSKAMEZAWA Hiroyuki } else 32261d65f86dSKAMEZAWA Hiroyuki cow_page = NULL; 32271d65f86dSKAMEZAWA Hiroyuki 3228d0217ac0SNick Piggin vmf.virtual_address = (void __user *)(address & PAGE_MASK); 3229d0217ac0SNick Piggin vmf.pgoff = pgoff; 3230d0217ac0SNick Piggin vmf.flags = flags; 3231d0217ac0SNick Piggin vmf.page = NULL; 32321da177e4SLinus Torvalds 3233d0217ac0SNick Piggin ret = vma->vm_ops->fault(vma, &vmf); 3234d065bd81SMichel Lespinasse if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | 3235d065bd81SMichel Lespinasse VM_FAULT_RETRY))) 32361d65f86dSKAMEZAWA Hiroyuki goto uncharge_out; 32371da177e4SLinus Torvalds 3238a3b947eaSAndi Kleen if (unlikely(PageHWPoison(vmf.page))) { 3239a3b947eaSAndi Kleen if (ret & VM_FAULT_LOCKED) 3240a3b947eaSAndi Kleen unlock_page(vmf.page); 32411d65f86dSKAMEZAWA Hiroyuki ret = VM_FAULT_HWPOISON; 32421d65f86dSKAMEZAWA Hiroyuki goto uncharge_out; 3243a3b947eaSAndi Kleen } 3244a3b947eaSAndi Kleen 3245d00806b1SNick Piggin /* 3246d0217ac0SNick Piggin * For consistency in subsequent calls, make the faulted page always 3247d00806b1SNick Piggin * locked. 3248d00806b1SNick Piggin */ 324983c54070SNick Piggin if (unlikely(!(ret & VM_FAULT_LOCKED))) 3250d0217ac0SNick Piggin lock_page(vmf.page); 325154cb8821SNick Piggin else 3252d0217ac0SNick Piggin VM_BUG_ON(!PageLocked(vmf.page)); 3253d00806b1SNick Piggin 32541da177e4SLinus Torvalds /* 32551da177e4SLinus Torvalds * Should we do an early C-O-W break? 32561da177e4SLinus Torvalds */ 3257d0217ac0SNick Piggin page = vmf.page; 325854cb8821SNick Piggin if (flags & FAULT_FLAG_WRITE) { 32599637a5efSDavid Howells if (!(vma->vm_flags & VM_SHARED)) { 32601d65f86dSKAMEZAWA Hiroyuki page = cow_page; 326154cb8821SNick Piggin anon = 1; 3262d0217ac0SNick Piggin copy_user_highpage(page, vmf.page, address, vma); 32630ed361deSNick Piggin __SetPageUptodate(page); 32649637a5efSDavid Howells } else { 326554cb8821SNick Piggin /* 326654cb8821SNick Piggin * If the page will be shareable, see if the backing 32679637a5efSDavid Howells * address space wants to know that the page is about 326854cb8821SNick Piggin * to become writable 326954cb8821SNick Piggin */ 327069676147SMark Fasheh if (vma->vm_ops->page_mkwrite) { 3271c2ec175cSNick Piggin int tmp; 3272c2ec175cSNick Piggin 327369676147SMark Fasheh unlock_page(page); 3274b827e496SNick Piggin vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE; 3275c2ec175cSNick Piggin tmp = vma->vm_ops->page_mkwrite(vma, &vmf); 3276c2ec175cSNick Piggin if (unlikely(tmp & 3277c2ec175cSNick Piggin (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) { 3278c2ec175cSNick Piggin ret = tmp; 3279b827e496SNick Piggin goto unwritable_page; 328069676147SMark Fasheh } 3281b827e496SNick Piggin if (unlikely(!(tmp & VM_FAULT_LOCKED))) { 328269676147SMark Fasheh lock_page(page); 3283d0217ac0SNick Piggin if (!page->mapping) { 3284b827e496SNick Piggin ret = 0; /* retry the fault */ 3285b827e496SNick Piggin unlock_page(page); 3286b827e496SNick Piggin goto unwritable_page; 3287d0217ac0SNick Piggin } 3288b827e496SNick Piggin } else 3289b827e496SNick Piggin VM_BUG_ON(!PageLocked(page)); 3290a200ee18SPeter Zijlstra page_mkwrite = 1; 32919637a5efSDavid Howells } 32929637a5efSDavid Howells } 329354cb8821SNick Piggin 32941da177e4SLinus Torvalds } 32951da177e4SLinus Torvalds 32968f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 32971da177e4SLinus Torvalds 32981da177e4SLinus Torvalds /* 32991da177e4SLinus Torvalds * This silly early PAGE_DIRTY setting removes a race 33001da177e4SLinus Torvalds * due to the bad i386 page protection. But it's valid 33011da177e4SLinus Torvalds * for other architectures too. 33021da177e4SLinus Torvalds * 330330c9f3a9SLinus Torvalds * Note that if FAULT_FLAG_WRITE is set, we either now have 33041da177e4SLinus Torvalds * an exclusive copy of the page, or this is a shared mapping, 33051da177e4SLinus Torvalds * so we can make it writable and dirty to avoid having to 33061da177e4SLinus Torvalds * handle that later. 33071da177e4SLinus Torvalds */ 33081da177e4SLinus Torvalds /* Only go through if we didn't race with anybody else... */ 33091c2fb7a4SAndrea Arcangeli if (likely(pte_same(*page_table, orig_pte))) { 3310d00806b1SNick Piggin flush_icache_page(vma, page); 3311d00806b1SNick Piggin entry = mk_pte(page, vma->vm_page_prot); 331254cb8821SNick Piggin if (flags & FAULT_FLAG_WRITE) 33131da177e4SLinus Torvalds entry = maybe_mkwrite(pte_mkdirty(entry), vma); 33141da177e4SLinus Torvalds if (anon) { 331534e55232SKAMEZAWA Hiroyuki inc_mm_counter_fast(mm, MM_ANONPAGES); 3316d00806b1SNick Piggin page_add_new_anon_rmap(page, vma, address); 3317f57e88a8SHugh Dickins } else { 331834e55232SKAMEZAWA Hiroyuki inc_mm_counter_fast(mm, MM_FILEPAGES); 3319d00806b1SNick Piggin page_add_file_rmap(page); 332054cb8821SNick Piggin if (flags & FAULT_FLAG_WRITE) { 3321d00806b1SNick Piggin dirty_page = page; 3322d08b3851SPeter Zijlstra get_page(dirty_page); 3323d08b3851SPeter Zijlstra } 33244294621fSHugh Dickins } 332564d6519dSLee Schermerhorn set_pte_at(mm, address, page_table, entry); 33261da177e4SLinus Torvalds 3327d00806b1SNick Piggin /* no need to invalidate: a not-present page won't be cached */ 33284b3073e1SRussell King update_mmu_cache(vma, address, page_table); 3329d00806b1SNick Piggin } else { 33301d65f86dSKAMEZAWA Hiroyuki if (cow_page) 33311d65f86dSKAMEZAWA Hiroyuki mem_cgroup_uncharge_page(cow_page); 3332d00806b1SNick Piggin if (anon) 3333d00806b1SNick Piggin page_cache_release(page); 3334d00806b1SNick Piggin else 333554cb8821SNick Piggin anon = 1; /* no anon but release faulted_page */ 3336d00806b1SNick Piggin } 3337d00806b1SNick Piggin 33388f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 3339d00806b1SNick Piggin 3340b827e496SNick Piggin if (dirty_page) { 3341b827e496SNick Piggin struct address_space *mapping = page->mapping; 334241c4d25fSJan Kara int dirtied = 0; 33438f7b3d15SAnton Salikhmetov 3344b827e496SNick Piggin if (set_page_dirty(dirty_page)) 334541c4d25fSJan Kara dirtied = 1; 3346b827e496SNick Piggin unlock_page(dirty_page); 3347d08b3851SPeter Zijlstra put_page(dirty_page); 334841c4d25fSJan Kara if ((dirtied || page_mkwrite) && mapping) { 3349b827e496SNick Piggin /* 3350b827e496SNick Piggin * Some device drivers do not set page.mapping but still 3351b827e496SNick Piggin * dirty their pages 3352b827e496SNick Piggin */ 3353b827e496SNick Piggin balance_dirty_pages_ratelimited(mapping); 3354d08b3851SPeter Zijlstra } 3355d00806b1SNick Piggin 3356b827e496SNick Piggin /* file_update_time outside page_lock */ 335741c4d25fSJan Kara if (vma->vm_file && !page_mkwrite) 3358b827e496SNick Piggin file_update_time(vma->vm_file); 3359b827e496SNick Piggin } else { 3360b827e496SNick Piggin unlock_page(vmf.page); 3361b827e496SNick Piggin if (anon) 3362b827e496SNick Piggin page_cache_release(vmf.page); 3363b827e496SNick Piggin } 3364b827e496SNick Piggin 3365b827e496SNick Piggin return ret; 3366b827e496SNick Piggin 3367b827e496SNick Piggin unwritable_page: 3368b827e496SNick Piggin page_cache_release(page); 336983c54070SNick Piggin return ret; 33701d65f86dSKAMEZAWA Hiroyuki uncharge_out: 33711d65f86dSKAMEZAWA Hiroyuki /* fs's fault handler get error */ 33721d65f86dSKAMEZAWA Hiroyuki if (cow_page) { 33731d65f86dSKAMEZAWA Hiroyuki mem_cgroup_uncharge_page(cow_page); 33741d65f86dSKAMEZAWA Hiroyuki page_cache_release(cow_page); 33751d65f86dSKAMEZAWA Hiroyuki } 33761d65f86dSKAMEZAWA Hiroyuki return ret; 337754cb8821SNick Piggin } 3378d00806b1SNick Piggin 337954cb8821SNick Piggin static int do_linear_fault(struct mm_struct *mm, struct vm_area_struct *vma, 338054cb8821SNick Piggin unsigned long address, pte_t *page_table, pmd_t *pmd, 338130c9f3a9SLinus Torvalds unsigned int flags, pte_t orig_pte) 338254cb8821SNick Piggin { 338354cb8821SNick Piggin pgoff_t pgoff = (((address & PAGE_MASK) 33840da7e01fSDean Nelson - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; 338554cb8821SNick Piggin 338616abfa08SHugh Dickins pte_unmap(page_table); 338716abfa08SHugh Dickins return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte); 338854cb8821SNick Piggin } 338954cb8821SNick Piggin 3390f4b81804SJes Sorensen /* 33911da177e4SLinus Torvalds * Fault of a previously existing named mapping. Repopulate the pte 33921da177e4SLinus Torvalds * from the encoded file_pte if possible. This enables swappable 33931da177e4SLinus Torvalds * nonlinear vmas. 33948f4e2101SHugh Dickins * 33958f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 33968f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 33978f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 33981da177e4SLinus Torvalds */ 3399d0217ac0SNick Piggin static int do_nonlinear_fault(struct mm_struct *mm, struct vm_area_struct *vma, 340065500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 340130c9f3a9SLinus Torvalds unsigned int flags, pte_t orig_pte) 34021da177e4SLinus Torvalds { 340365500d23SHugh Dickins pgoff_t pgoff; 34041da177e4SLinus Torvalds 340530c9f3a9SLinus Torvalds flags |= FAULT_FLAG_NONLINEAR; 340630c9f3a9SLinus Torvalds 34074c21e2f2SHugh Dickins if (!pte_unmap_same(mm, pmd, page_table, orig_pte)) 340883c54070SNick Piggin return 0; 34091da177e4SLinus Torvalds 34102509ef26SHugh Dickins if (unlikely(!(vma->vm_flags & VM_NONLINEAR))) { 341165500d23SHugh Dickins /* 341265500d23SHugh Dickins * Page table corrupted: show pte and kill process. 341365500d23SHugh Dickins */ 34143dc14741SHugh Dickins print_bad_pte(vma, address, orig_pte, NULL); 3415d99be1a8SHugh Dickins return VM_FAULT_SIGBUS; 341665500d23SHugh Dickins } 341765500d23SHugh Dickins 341865500d23SHugh Dickins pgoff = pte_to_pgoff(orig_pte); 341916abfa08SHugh Dickins return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte); 34201da177e4SLinus Torvalds } 34211da177e4SLinus Torvalds 34221da177e4SLinus Torvalds /* 34231da177e4SLinus Torvalds * These routines also need to handle stuff like marking pages dirty 34241da177e4SLinus Torvalds * and/or accessed for architectures that don't do it in hardware (most 34251da177e4SLinus Torvalds * RISC architectures). The early dirtying is also good on the i386. 34261da177e4SLinus Torvalds * 34271da177e4SLinus Torvalds * There is also a hook called "update_mmu_cache()" that architectures 34281da177e4SLinus Torvalds * with external mmu caches can use to update those (ie the Sparc or 34291da177e4SLinus Torvalds * PowerPC hashed page tables that act as extended TLBs). 34301da177e4SLinus Torvalds * 3431c74df32cSHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 3432c74df32cSHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 3433c74df32cSHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 34341da177e4SLinus Torvalds */ 343571e3aac0SAndrea Arcangeli int handle_pte_fault(struct mm_struct *mm, 34361da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long address, 343730c9f3a9SLinus Torvalds pte_t *pte, pmd_t *pmd, unsigned int flags) 34381da177e4SLinus Torvalds { 34391da177e4SLinus Torvalds pte_t entry; 34408f4e2101SHugh Dickins spinlock_t *ptl; 34411da177e4SLinus Torvalds 34428dab5241SBenjamin Herrenschmidt entry = *pte; 34431da177e4SLinus Torvalds if (!pte_present(entry)) { 344465500d23SHugh Dickins if (pte_none(entry)) { 3445f4b81804SJes Sorensen if (vma->vm_ops) { 34463c18ddd1SNick Piggin if (likely(vma->vm_ops->fault)) 344754cb8821SNick Piggin return do_linear_fault(mm, vma, address, 344830c9f3a9SLinus Torvalds pte, pmd, flags, entry); 3449f4b81804SJes Sorensen } 3450f4b81804SJes Sorensen return do_anonymous_page(mm, vma, address, 345130c9f3a9SLinus Torvalds pte, pmd, flags); 345265500d23SHugh Dickins } 34531da177e4SLinus Torvalds if (pte_file(entry)) 3454d0217ac0SNick Piggin return do_nonlinear_fault(mm, vma, address, 345530c9f3a9SLinus Torvalds pte, pmd, flags, entry); 345665500d23SHugh Dickins return do_swap_page(mm, vma, address, 345730c9f3a9SLinus Torvalds pte, pmd, flags, entry); 34581da177e4SLinus Torvalds } 34591da177e4SLinus Torvalds 34604c21e2f2SHugh Dickins ptl = pte_lockptr(mm, pmd); 34618f4e2101SHugh Dickins spin_lock(ptl); 34628f4e2101SHugh Dickins if (unlikely(!pte_same(*pte, entry))) 34638f4e2101SHugh Dickins goto unlock; 346430c9f3a9SLinus Torvalds if (flags & FAULT_FLAG_WRITE) { 34651da177e4SLinus Torvalds if (!pte_write(entry)) 34668f4e2101SHugh Dickins return do_wp_page(mm, vma, address, 34678f4e2101SHugh Dickins pte, pmd, ptl, entry); 34681da177e4SLinus Torvalds entry = pte_mkdirty(entry); 34691da177e4SLinus Torvalds } 34701da177e4SLinus Torvalds entry = pte_mkyoung(entry); 347130c9f3a9SLinus Torvalds if (ptep_set_access_flags(vma, address, pte, entry, flags & FAULT_FLAG_WRITE)) { 34724b3073e1SRussell King update_mmu_cache(vma, address, pte); 34731a44e149SAndrea Arcangeli } else { 34741a44e149SAndrea Arcangeli /* 34751a44e149SAndrea Arcangeli * This is needed only for protection faults but the arch code 34761a44e149SAndrea Arcangeli * is not yet telling us if this is a protection fault or not. 34771a44e149SAndrea Arcangeli * This still avoids useless tlb flushes for .text page faults 34781a44e149SAndrea Arcangeli * with threads. 34791a44e149SAndrea Arcangeli */ 348030c9f3a9SLinus Torvalds if (flags & FAULT_FLAG_WRITE) 348161c77326SShaohua Li flush_tlb_fix_spurious_fault(vma, address); 34821a44e149SAndrea Arcangeli } 34838f4e2101SHugh Dickins unlock: 34848f4e2101SHugh Dickins pte_unmap_unlock(pte, ptl); 348583c54070SNick Piggin return 0; 34861da177e4SLinus Torvalds } 34871da177e4SLinus Torvalds 34881da177e4SLinus Torvalds /* 34891da177e4SLinus Torvalds * By the time we get here, we already hold the mm semaphore 34901da177e4SLinus Torvalds */ 349183c54070SNick Piggin int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma, 3492d06063ccSLinus Torvalds unsigned long address, unsigned int flags) 34931da177e4SLinus Torvalds { 34941da177e4SLinus Torvalds pgd_t *pgd; 34951da177e4SLinus Torvalds pud_t *pud; 34961da177e4SLinus Torvalds pmd_t *pmd; 34971da177e4SLinus Torvalds pte_t *pte; 34981da177e4SLinus Torvalds 34991da177e4SLinus Torvalds __set_current_state(TASK_RUNNING); 35001da177e4SLinus Torvalds 3501f8891e5eSChristoph Lameter count_vm_event(PGFAULT); 3502456f998eSYing Han mem_cgroup_count_vm_event(mm, PGFAULT); 35031da177e4SLinus Torvalds 350434e55232SKAMEZAWA Hiroyuki /* do counter updates before entering really critical section. */ 350534e55232SKAMEZAWA Hiroyuki check_sync_rss_stat(current); 350634e55232SKAMEZAWA Hiroyuki 3507ac9b9c66SHugh Dickins if (unlikely(is_vm_hugetlb_page(vma))) 350830c9f3a9SLinus Torvalds return hugetlb_fault(mm, vma, address, flags); 35091da177e4SLinus Torvalds 35101f1d06c3SDavid Rientjes retry: 35111da177e4SLinus Torvalds pgd = pgd_offset(mm, address); 35121da177e4SLinus Torvalds pud = pud_alloc(mm, pgd, address); 35131da177e4SLinus Torvalds if (!pud) 3514c74df32cSHugh Dickins return VM_FAULT_OOM; 35151da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, address); 35161da177e4SLinus Torvalds if (!pmd) 3517c74df32cSHugh Dickins return VM_FAULT_OOM; 351871e3aac0SAndrea Arcangeli if (pmd_none(*pmd) && transparent_hugepage_enabled(vma)) { 351971e3aac0SAndrea Arcangeli if (!vma->vm_ops) 352071e3aac0SAndrea Arcangeli return do_huge_pmd_anonymous_page(mm, vma, address, 352171e3aac0SAndrea Arcangeli pmd, flags); 352271e3aac0SAndrea Arcangeli } else { 352371e3aac0SAndrea Arcangeli pmd_t orig_pmd = *pmd; 35241f1d06c3SDavid Rientjes int ret; 35251f1d06c3SDavid Rientjes 352671e3aac0SAndrea Arcangeli barrier(); 352771e3aac0SAndrea Arcangeli if (pmd_trans_huge(orig_pmd)) { 352871e3aac0SAndrea Arcangeli if (flags & FAULT_FLAG_WRITE && 352971e3aac0SAndrea Arcangeli !pmd_write(orig_pmd) && 35301f1d06c3SDavid Rientjes !pmd_trans_splitting(orig_pmd)) { 35311f1d06c3SDavid Rientjes ret = do_huge_pmd_wp_page(mm, vma, address, pmd, 35321f1d06c3SDavid Rientjes orig_pmd); 35331f1d06c3SDavid Rientjes /* 35341f1d06c3SDavid Rientjes * If COW results in an oom, the huge pmd will 35351f1d06c3SDavid Rientjes * have been split, so retry the fault on the 35361f1d06c3SDavid Rientjes * pte for a smaller charge. 35371f1d06c3SDavid Rientjes */ 35381f1d06c3SDavid Rientjes if (unlikely(ret & VM_FAULT_OOM)) 35391f1d06c3SDavid Rientjes goto retry; 35401f1d06c3SDavid Rientjes return ret; 35411f1d06c3SDavid Rientjes } 354271e3aac0SAndrea Arcangeli return 0; 354371e3aac0SAndrea Arcangeli } 354471e3aac0SAndrea Arcangeli } 354571e3aac0SAndrea Arcangeli 354671e3aac0SAndrea Arcangeli /* 354771e3aac0SAndrea Arcangeli * Use __pte_alloc instead of pte_alloc_map, because we can't 354871e3aac0SAndrea Arcangeli * run pte_offset_map on the pmd, if an huge pmd could 354971e3aac0SAndrea Arcangeli * materialize from under us from a different thread. 355071e3aac0SAndrea Arcangeli */ 3551cc03638dSMel Gorman if (unlikely(pmd_none(*pmd)) && __pte_alloc(mm, vma, pmd, address)) 3552c74df32cSHugh Dickins return VM_FAULT_OOM; 355371e3aac0SAndrea Arcangeli /* if an huge pmd materialized from under us just retry later */ 355471e3aac0SAndrea Arcangeli if (unlikely(pmd_trans_huge(*pmd))) 355571e3aac0SAndrea Arcangeli return 0; 355671e3aac0SAndrea Arcangeli /* 355771e3aac0SAndrea Arcangeli * A regular pmd is established and it can't morph into a huge pmd 355871e3aac0SAndrea Arcangeli * from under us anymore at this point because we hold the mmap_sem 355971e3aac0SAndrea Arcangeli * read mode and khugepaged takes it in write mode. So now it's 356071e3aac0SAndrea Arcangeli * safe to run pte_offset_map(). 356171e3aac0SAndrea Arcangeli */ 356271e3aac0SAndrea Arcangeli pte = pte_offset_map(pmd, address); 35631da177e4SLinus Torvalds 356430c9f3a9SLinus Torvalds return handle_pte_fault(mm, vma, address, pte, pmd, flags); 35651da177e4SLinus Torvalds } 35661da177e4SLinus Torvalds 35671da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED 35681da177e4SLinus Torvalds /* 35691da177e4SLinus Torvalds * Allocate page upper directory. 3570872fec16SHugh Dickins * We've already handled the fast-path in-line. 35711da177e4SLinus Torvalds */ 35721bb3630eSHugh Dickins int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address) 35731da177e4SLinus Torvalds { 3574c74df32cSHugh Dickins pud_t *new = pud_alloc_one(mm, address); 3575c74df32cSHugh Dickins if (!new) 35761bb3630eSHugh Dickins return -ENOMEM; 35771da177e4SLinus Torvalds 3578362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 3579362a61adSNick Piggin 3580872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 35811bb3630eSHugh Dickins if (pgd_present(*pgd)) /* Another has populated it */ 35825e541973SBenjamin Herrenschmidt pud_free(mm, new); 35831bb3630eSHugh Dickins else 35841da177e4SLinus Torvalds pgd_populate(mm, pgd, new); 3585872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 35861bb3630eSHugh Dickins return 0; 35871da177e4SLinus Torvalds } 35881da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */ 35891da177e4SLinus Torvalds 35901da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED 35911da177e4SLinus Torvalds /* 35921da177e4SLinus Torvalds * Allocate page middle directory. 3593872fec16SHugh Dickins * We've already handled the fast-path in-line. 35941da177e4SLinus Torvalds */ 35951bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) 35961da177e4SLinus Torvalds { 3597c74df32cSHugh Dickins pmd_t *new = pmd_alloc_one(mm, address); 3598c74df32cSHugh Dickins if (!new) 35991bb3630eSHugh Dickins return -ENOMEM; 36001da177e4SLinus Torvalds 3601362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 3602362a61adSNick Piggin 3603872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 36041da177e4SLinus Torvalds #ifndef __ARCH_HAS_4LEVEL_HACK 36051bb3630eSHugh Dickins if (pud_present(*pud)) /* Another has populated it */ 36065e541973SBenjamin Herrenschmidt pmd_free(mm, new); 36071bb3630eSHugh Dickins else 36081da177e4SLinus Torvalds pud_populate(mm, pud, new); 36091da177e4SLinus Torvalds #else 36101bb3630eSHugh Dickins if (pgd_present(*pud)) /* Another has populated it */ 36115e541973SBenjamin Herrenschmidt pmd_free(mm, new); 36121bb3630eSHugh Dickins else 36131da177e4SLinus Torvalds pgd_populate(mm, pud, new); 36141da177e4SLinus Torvalds #endif /* __ARCH_HAS_4LEVEL_HACK */ 3615872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 36161bb3630eSHugh Dickins return 0; 36171da177e4SLinus Torvalds } 36181da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */ 36191da177e4SLinus Torvalds 36201da177e4SLinus Torvalds int make_pages_present(unsigned long addr, unsigned long end) 36211da177e4SLinus Torvalds { 36221da177e4SLinus Torvalds int ret, len, write; 36231da177e4SLinus Torvalds struct vm_area_struct * vma; 36241da177e4SLinus Torvalds 36251da177e4SLinus Torvalds vma = find_vma(current->mm, addr); 36261da177e4SLinus Torvalds if (!vma) 3627a477097dSKOSAKI Motohiro return -ENOMEM; 36285ecfda04SMichel Lespinasse /* 36295ecfda04SMichel Lespinasse * We want to touch writable mappings with a write fault in order 36305ecfda04SMichel Lespinasse * to break COW, except for shared mappings because these don't COW 36315ecfda04SMichel Lespinasse * and we would not want to dirty them for nothing. 36325ecfda04SMichel Lespinasse */ 36335ecfda04SMichel Lespinasse write = (vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE; 36345bcb28b1SEric Sesterhenn BUG_ON(addr >= end); 36355bcb28b1SEric Sesterhenn BUG_ON(end > vma->vm_end); 363668e116a3SRolf Eike Beer len = DIV_ROUND_UP(end, PAGE_SIZE) - addr/PAGE_SIZE; 36371da177e4SLinus Torvalds ret = get_user_pages(current, current->mm, addr, 36381da177e4SLinus Torvalds len, write, 0, NULL, NULL); 3639c11d69d8SLee Schermerhorn if (ret < 0) 36401da177e4SLinus Torvalds return ret; 36419978ad58SLee Schermerhorn return ret == len ? 0 : -EFAULT; 36421da177e4SLinus Torvalds } 36431da177e4SLinus Torvalds 36441da177e4SLinus Torvalds #if !defined(__HAVE_ARCH_GATE_AREA) 36451da177e4SLinus Torvalds 36461da177e4SLinus Torvalds #if defined(AT_SYSINFO_EHDR) 36475ce7852cSAdrian Bunk static struct vm_area_struct gate_vma; 36481da177e4SLinus Torvalds 36491da177e4SLinus Torvalds static int __init gate_vma_init(void) 36501da177e4SLinus Torvalds { 36511da177e4SLinus Torvalds gate_vma.vm_mm = NULL; 36521da177e4SLinus Torvalds gate_vma.vm_start = FIXADDR_USER_START; 36531da177e4SLinus Torvalds gate_vma.vm_end = FIXADDR_USER_END; 3654b6558c4aSRoland McGrath gate_vma.vm_flags = VM_READ | VM_MAYREAD | VM_EXEC | VM_MAYEXEC; 3655b6558c4aSRoland McGrath gate_vma.vm_page_prot = __P101; 3656909af768SJason Baron 36571da177e4SLinus Torvalds return 0; 36581da177e4SLinus Torvalds } 36591da177e4SLinus Torvalds __initcall(gate_vma_init); 36601da177e4SLinus Torvalds #endif 36611da177e4SLinus Torvalds 366231db58b3SStephen Wilson struct vm_area_struct *get_gate_vma(struct mm_struct *mm) 36631da177e4SLinus Torvalds { 36641da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR 36651da177e4SLinus Torvalds return &gate_vma; 36661da177e4SLinus Torvalds #else 36671da177e4SLinus Torvalds return NULL; 36681da177e4SLinus Torvalds #endif 36691da177e4SLinus Torvalds } 36701da177e4SLinus Torvalds 3671cae5d390SStephen Wilson int in_gate_area_no_mm(unsigned long addr) 36721da177e4SLinus Torvalds { 36731da177e4SLinus Torvalds #ifdef AT_SYSINFO_EHDR 36741da177e4SLinus Torvalds if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END)) 36751da177e4SLinus Torvalds return 1; 36761da177e4SLinus Torvalds #endif 36771da177e4SLinus Torvalds return 0; 36781da177e4SLinus Torvalds } 36791da177e4SLinus Torvalds 36801da177e4SLinus Torvalds #endif /* __HAVE_ARCH_GATE_AREA */ 36810ec76a11SDavid Howells 36821b36ba81SNamhyung Kim static int __follow_pte(struct mm_struct *mm, unsigned long address, 3683f8ad0f49SJohannes Weiner pte_t **ptepp, spinlock_t **ptlp) 3684f8ad0f49SJohannes Weiner { 3685f8ad0f49SJohannes Weiner pgd_t *pgd; 3686f8ad0f49SJohannes Weiner pud_t *pud; 3687f8ad0f49SJohannes Weiner pmd_t *pmd; 3688f8ad0f49SJohannes Weiner pte_t *ptep; 3689f8ad0f49SJohannes Weiner 3690f8ad0f49SJohannes Weiner pgd = pgd_offset(mm, address); 3691f8ad0f49SJohannes Weiner if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) 3692f8ad0f49SJohannes Weiner goto out; 3693f8ad0f49SJohannes Weiner 3694f8ad0f49SJohannes Weiner pud = pud_offset(pgd, address); 3695f8ad0f49SJohannes Weiner if (pud_none(*pud) || unlikely(pud_bad(*pud))) 3696f8ad0f49SJohannes Weiner goto out; 3697f8ad0f49SJohannes Weiner 3698f8ad0f49SJohannes Weiner pmd = pmd_offset(pud, address); 3699f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 3700f8ad0f49SJohannes Weiner if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd))) 3701f8ad0f49SJohannes Weiner goto out; 3702f8ad0f49SJohannes Weiner 3703f8ad0f49SJohannes Weiner /* We cannot handle huge page PFN maps. Luckily they don't exist. */ 3704f8ad0f49SJohannes Weiner if (pmd_huge(*pmd)) 3705f8ad0f49SJohannes Weiner goto out; 3706f8ad0f49SJohannes Weiner 3707f8ad0f49SJohannes Weiner ptep = pte_offset_map_lock(mm, pmd, address, ptlp); 3708f8ad0f49SJohannes Weiner if (!ptep) 3709f8ad0f49SJohannes Weiner goto out; 3710f8ad0f49SJohannes Weiner if (!pte_present(*ptep)) 3711f8ad0f49SJohannes Weiner goto unlock; 3712f8ad0f49SJohannes Weiner *ptepp = ptep; 3713f8ad0f49SJohannes Weiner return 0; 3714f8ad0f49SJohannes Weiner unlock: 3715f8ad0f49SJohannes Weiner pte_unmap_unlock(ptep, *ptlp); 3716f8ad0f49SJohannes Weiner out: 3717f8ad0f49SJohannes Weiner return -EINVAL; 3718f8ad0f49SJohannes Weiner } 3719f8ad0f49SJohannes Weiner 37201b36ba81SNamhyung Kim static inline int follow_pte(struct mm_struct *mm, unsigned long address, 37211b36ba81SNamhyung Kim pte_t **ptepp, spinlock_t **ptlp) 37221b36ba81SNamhyung Kim { 37231b36ba81SNamhyung Kim int res; 37241b36ba81SNamhyung Kim 37251b36ba81SNamhyung Kim /* (void) is needed to make gcc happy */ 37261b36ba81SNamhyung Kim (void) __cond_lock(*ptlp, 37271b36ba81SNamhyung Kim !(res = __follow_pte(mm, address, ptepp, ptlp))); 37281b36ba81SNamhyung Kim return res; 37291b36ba81SNamhyung Kim } 37301b36ba81SNamhyung Kim 37313b6748e2SJohannes Weiner /** 37323b6748e2SJohannes Weiner * follow_pfn - look up PFN at a user virtual address 37333b6748e2SJohannes Weiner * @vma: memory mapping 37343b6748e2SJohannes Weiner * @address: user virtual address 37353b6748e2SJohannes Weiner * @pfn: location to store found PFN 37363b6748e2SJohannes Weiner * 37373b6748e2SJohannes Weiner * Only IO mappings and raw PFN mappings are allowed. 37383b6748e2SJohannes Weiner * 37393b6748e2SJohannes Weiner * Returns zero and the pfn at @pfn on success, -ve otherwise. 37403b6748e2SJohannes Weiner */ 37413b6748e2SJohannes Weiner int follow_pfn(struct vm_area_struct *vma, unsigned long address, 37423b6748e2SJohannes Weiner unsigned long *pfn) 37433b6748e2SJohannes Weiner { 37443b6748e2SJohannes Weiner int ret = -EINVAL; 37453b6748e2SJohannes Weiner spinlock_t *ptl; 37463b6748e2SJohannes Weiner pte_t *ptep; 37473b6748e2SJohannes Weiner 37483b6748e2SJohannes Weiner if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 37493b6748e2SJohannes Weiner return ret; 37503b6748e2SJohannes Weiner 37513b6748e2SJohannes Weiner ret = follow_pte(vma->vm_mm, address, &ptep, &ptl); 37523b6748e2SJohannes Weiner if (ret) 37533b6748e2SJohannes Weiner return ret; 37543b6748e2SJohannes Weiner *pfn = pte_pfn(*ptep); 37553b6748e2SJohannes Weiner pte_unmap_unlock(ptep, ptl); 37563b6748e2SJohannes Weiner return 0; 37573b6748e2SJohannes Weiner } 37583b6748e2SJohannes Weiner EXPORT_SYMBOL(follow_pfn); 37593b6748e2SJohannes Weiner 376028b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT 3761d87fe660Svenkatesh.pallipadi@intel.com int follow_phys(struct vm_area_struct *vma, 376228b2ee20SRik van Riel unsigned long address, unsigned int flags, 3763d87fe660Svenkatesh.pallipadi@intel.com unsigned long *prot, resource_size_t *phys) 376428b2ee20SRik van Riel { 376503668a4dSJohannes Weiner int ret = -EINVAL; 376628b2ee20SRik van Riel pte_t *ptep, pte; 376728b2ee20SRik van Riel spinlock_t *ptl; 376828b2ee20SRik van Riel 3769d87fe660Svenkatesh.pallipadi@intel.com if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 3770d87fe660Svenkatesh.pallipadi@intel.com goto out; 377128b2ee20SRik van Riel 377203668a4dSJohannes Weiner if (follow_pte(vma->vm_mm, address, &ptep, &ptl)) 3773d87fe660Svenkatesh.pallipadi@intel.com goto out; 377428b2ee20SRik van Riel pte = *ptep; 377503668a4dSJohannes Weiner 377628b2ee20SRik van Riel if ((flags & FOLL_WRITE) && !pte_write(pte)) 377728b2ee20SRik van Riel goto unlock; 377828b2ee20SRik van Riel 377928b2ee20SRik van Riel *prot = pgprot_val(pte_pgprot(pte)); 378003668a4dSJohannes Weiner *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT; 378128b2ee20SRik van Riel 378203668a4dSJohannes Weiner ret = 0; 378328b2ee20SRik van Riel unlock: 378428b2ee20SRik van Riel pte_unmap_unlock(ptep, ptl); 378528b2ee20SRik van Riel out: 3786d87fe660Svenkatesh.pallipadi@intel.com return ret; 378728b2ee20SRik van Riel } 378828b2ee20SRik van Riel 378928b2ee20SRik van Riel int generic_access_phys(struct vm_area_struct *vma, unsigned long addr, 379028b2ee20SRik van Riel void *buf, int len, int write) 379128b2ee20SRik van Riel { 379228b2ee20SRik van Riel resource_size_t phys_addr; 379328b2ee20SRik van Riel unsigned long prot = 0; 37942bc7273bSKOSAKI Motohiro void __iomem *maddr; 379528b2ee20SRik van Riel int offset = addr & (PAGE_SIZE-1); 379628b2ee20SRik van Riel 3797d87fe660Svenkatesh.pallipadi@intel.com if (follow_phys(vma, addr, write, &prot, &phys_addr)) 379828b2ee20SRik van Riel return -EINVAL; 379928b2ee20SRik van Riel 380028b2ee20SRik van Riel maddr = ioremap_prot(phys_addr, PAGE_SIZE, prot); 380128b2ee20SRik van Riel if (write) 380228b2ee20SRik van Riel memcpy_toio(maddr + offset, buf, len); 380328b2ee20SRik van Riel else 380428b2ee20SRik van Riel memcpy_fromio(buf, maddr + offset, len); 380528b2ee20SRik van Riel iounmap(maddr); 380628b2ee20SRik van Riel 380728b2ee20SRik van Riel return len; 380828b2ee20SRik van Riel } 380928b2ee20SRik van Riel #endif 381028b2ee20SRik van Riel 38110ec76a11SDavid Howells /* 3812206cb636SStephen Wilson * Access another process' address space as given in mm. If non-NULL, use the 3813206cb636SStephen Wilson * given task for page fault accounting. 38140ec76a11SDavid Howells */ 3815206cb636SStephen Wilson static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm, 3816206cb636SStephen Wilson unsigned long addr, void *buf, int len, int write) 38170ec76a11SDavid Howells { 38180ec76a11SDavid Howells struct vm_area_struct *vma; 38190ec76a11SDavid Howells void *old_buf = buf; 38200ec76a11SDavid Howells 38210ec76a11SDavid Howells down_read(&mm->mmap_sem); 3822183ff22bSSimon Arlott /* ignore errors, just check how much was successfully transferred */ 38230ec76a11SDavid Howells while (len) { 38240ec76a11SDavid Howells int bytes, ret, offset; 38250ec76a11SDavid Howells void *maddr; 382628b2ee20SRik van Riel struct page *page = NULL; 38270ec76a11SDavid Howells 38280ec76a11SDavid Howells ret = get_user_pages(tsk, mm, addr, 1, 38290ec76a11SDavid Howells write, 1, &page, &vma); 383028b2ee20SRik van Riel if (ret <= 0) { 383128b2ee20SRik van Riel /* 383228b2ee20SRik van Riel * Check if this is a VM_IO | VM_PFNMAP VMA, which 383328b2ee20SRik van Riel * we can access using slightly different code. 383428b2ee20SRik van Riel */ 383528b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT 383628b2ee20SRik van Riel vma = find_vma(mm, addr); 3837fe936dfcSMichael Ellerman if (!vma || vma->vm_start > addr) 38380ec76a11SDavid Howells break; 383928b2ee20SRik van Riel if (vma->vm_ops && vma->vm_ops->access) 384028b2ee20SRik van Riel ret = vma->vm_ops->access(vma, addr, buf, 384128b2ee20SRik van Riel len, write); 384228b2ee20SRik van Riel if (ret <= 0) 384328b2ee20SRik van Riel #endif 384428b2ee20SRik van Riel break; 384528b2ee20SRik van Riel bytes = ret; 384628b2ee20SRik van Riel } else { 38470ec76a11SDavid Howells bytes = len; 38480ec76a11SDavid Howells offset = addr & (PAGE_SIZE-1); 38490ec76a11SDavid Howells if (bytes > PAGE_SIZE-offset) 38500ec76a11SDavid Howells bytes = PAGE_SIZE-offset; 38510ec76a11SDavid Howells 38520ec76a11SDavid Howells maddr = kmap(page); 38530ec76a11SDavid Howells if (write) { 38540ec76a11SDavid Howells copy_to_user_page(vma, page, addr, 38550ec76a11SDavid Howells maddr + offset, buf, bytes); 38560ec76a11SDavid Howells set_page_dirty_lock(page); 38570ec76a11SDavid Howells } else { 38580ec76a11SDavid Howells copy_from_user_page(vma, page, addr, 38590ec76a11SDavid Howells buf, maddr + offset, bytes); 38600ec76a11SDavid Howells } 38610ec76a11SDavid Howells kunmap(page); 38620ec76a11SDavid Howells page_cache_release(page); 386328b2ee20SRik van Riel } 38640ec76a11SDavid Howells len -= bytes; 38650ec76a11SDavid Howells buf += bytes; 38660ec76a11SDavid Howells addr += bytes; 38670ec76a11SDavid Howells } 38680ec76a11SDavid Howells up_read(&mm->mmap_sem); 38690ec76a11SDavid Howells 38700ec76a11SDavid Howells return buf - old_buf; 38710ec76a11SDavid Howells } 387203252919SAndi Kleen 38735ddd36b9SStephen Wilson /** 3874ae91dbfcSRandy Dunlap * access_remote_vm - access another process' address space 38755ddd36b9SStephen Wilson * @mm: the mm_struct of the target address space 38765ddd36b9SStephen Wilson * @addr: start address to access 38775ddd36b9SStephen Wilson * @buf: source or destination buffer 38785ddd36b9SStephen Wilson * @len: number of bytes to transfer 38795ddd36b9SStephen Wilson * @write: whether the access is a write 38805ddd36b9SStephen Wilson * 38815ddd36b9SStephen Wilson * The caller must hold a reference on @mm. 38825ddd36b9SStephen Wilson */ 38835ddd36b9SStephen Wilson int access_remote_vm(struct mm_struct *mm, unsigned long addr, 38845ddd36b9SStephen Wilson void *buf, int len, int write) 38855ddd36b9SStephen Wilson { 38865ddd36b9SStephen Wilson return __access_remote_vm(NULL, mm, addr, buf, len, write); 38875ddd36b9SStephen Wilson } 38885ddd36b9SStephen Wilson 388903252919SAndi Kleen /* 3890206cb636SStephen Wilson * Access another process' address space. 3891206cb636SStephen Wilson * Source/target buffer must be kernel space, 3892206cb636SStephen Wilson * Do not walk the page table directly, use get_user_pages 3893206cb636SStephen Wilson */ 3894206cb636SStephen Wilson int access_process_vm(struct task_struct *tsk, unsigned long addr, 3895206cb636SStephen Wilson void *buf, int len, int write) 3896206cb636SStephen Wilson { 3897206cb636SStephen Wilson struct mm_struct *mm; 3898206cb636SStephen Wilson int ret; 3899206cb636SStephen Wilson 3900206cb636SStephen Wilson mm = get_task_mm(tsk); 3901206cb636SStephen Wilson if (!mm) 3902206cb636SStephen Wilson return 0; 3903206cb636SStephen Wilson 3904206cb636SStephen Wilson ret = __access_remote_vm(tsk, mm, addr, buf, len, write); 3905206cb636SStephen Wilson mmput(mm); 3906206cb636SStephen Wilson 3907206cb636SStephen Wilson return ret; 3908206cb636SStephen Wilson } 3909206cb636SStephen Wilson 391003252919SAndi Kleen /* 391103252919SAndi Kleen * Print the name of a VMA. 391203252919SAndi Kleen */ 391303252919SAndi Kleen void print_vma_addr(char *prefix, unsigned long ip) 391403252919SAndi Kleen { 391503252919SAndi Kleen struct mm_struct *mm = current->mm; 391603252919SAndi Kleen struct vm_area_struct *vma; 391703252919SAndi Kleen 3918e8bff74aSIngo Molnar /* 3919e8bff74aSIngo Molnar * Do not print if we are in atomic 3920e8bff74aSIngo Molnar * contexts (in exception stacks, etc.): 3921e8bff74aSIngo Molnar */ 3922e8bff74aSIngo Molnar if (preempt_count()) 3923e8bff74aSIngo Molnar return; 3924e8bff74aSIngo Molnar 392503252919SAndi Kleen down_read(&mm->mmap_sem); 392603252919SAndi Kleen vma = find_vma(mm, ip); 392703252919SAndi Kleen if (vma && vma->vm_file) { 392803252919SAndi Kleen struct file *f = vma->vm_file; 392903252919SAndi Kleen char *buf = (char *)__get_free_page(GFP_KERNEL); 393003252919SAndi Kleen if (buf) { 393103252919SAndi Kleen char *p, *s; 393203252919SAndi Kleen 3933cf28b486SJan Blunck p = d_path(&f->f_path, buf, PAGE_SIZE); 393403252919SAndi Kleen if (IS_ERR(p)) 393503252919SAndi Kleen p = "?"; 393603252919SAndi Kleen s = strrchr(p, '/'); 393703252919SAndi Kleen if (s) 393803252919SAndi Kleen p = s+1; 393903252919SAndi Kleen printk("%s%s[%lx+%lx]", prefix, p, 394003252919SAndi Kleen vma->vm_start, 394103252919SAndi Kleen vma->vm_end - vma->vm_start); 394203252919SAndi Kleen free_page((unsigned long)buf); 394303252919SAndi Kleen } 394403252919SAndi Kleen } 394551a07e50SJeff Liu up_read(&mm->mmap_sem); 394603252919SAndi Kleen } 39473ee1afa3SNick Piggin 39483ee1afa3SNick Piggin #ifdef CONFIG_PROVE_LOCKING 39493ee1afa3SNick Piggin void might_fault(void) 39503ee1afa3SNick Piggin { 395195156f00SPeter Zijlstra /* 395295156f00SPeter Zijlstra * Some code (nfs/sunrpc) uses socket ops on kernel memory while 395395156f00SPeter Zijlstra * holding the mmap_sem, this is safe because kernel memory doesn't 395495156f00SPeter Zijlstra * get paged out, therefore we'll never actually fault, and the 395595156f00SPeter Zijlstra * below annotations will generate false positives. 395695156f00SPeter Zijlstra */ 395795156f00SPeter Zijlstra if (segment_eq(get_fs(), KERNEL_DS)) 395895156f00SPeter Zijlstra return; 395995156f00SPeter Zijlstra 39603ee1afa3SNick Piggin might_sleep(); 39613ee1afa3SNick Piggin /* 39623ee1afa3SNick Piggin * it would be nicer only to annotate paths which are not under 39633ee1afa3SNick Piggin * pagefault_disable, however that requires a larger audit and 39643ee1afa3SNick Piggin * providing helpers like get_user_atomic. 39653ee1afa3SNick Piggin */ 39663ee1afa3SNick Piggin if (!in_atomic() && current->mm) 39673ee1afa3SNick Piggin might_lock_read(¤t->mm->mmap_sem); 39683ee1afa3SNick Piggin } 39693ee1afa3SNick Piggin EXPORT_SYMBOL(might_fault); 39703ee1afa3SNick Piggin #endif 397147ad8475SAndrea Arcangeli 397247ad8475SAndrea Arcangeli #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS) 397347ad8475SAndrea Arcangeli static void clear_gigantic_page(struct page *page, 397447ad8475SAndrea Arcangeli unsigned long addr, 397547ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 397647ad8475SAndrea Arcangeli { 397747ad8475SAndrea Arcangeli int i; 397847ad8475SAndrea Arcangeli struct page *p = page; 397947ad8475SAndrea Arcangeli 398047ad8475SAndrea Arcangeli might_sleep(); 398147ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; 398247ad8475SAndrea Arcangeli i++, p = mem_map_next(p, page, i)) { 398347ad8475SAndrea Arcangeli cond_resched(); 398447ad8475SAndrea Arcangeli clear_user_highpage(p, addr + i * PAGE_SIZE); 398547ad8475SAndrea Arcangeli } 398647ad8475SAndrea Arcangeli } 398747ad8475SAndrea Arcangeli void clear_huge_page(struct page *page, 398847ad8475SAndrea Arcangeli unsigned long addr, unsigned int pages_per_huge_page) 398947ad8475SAndrea Arcangeli { 399047ad8475SAndrea Arcangeli int i; 399147ad8475SAndrea Arcangeli 399247ad8475SAndrea Arcangeli if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { 399347ad8475SAndrea Arcangeli clear_gigantic_page(page, addr, pages_per_huge_page); 399447ad8475SAndrea Arcangeli return; 399547ad8475SAndrea Arcangeli } 399647ad8475SAndrea Arcangeli 399747ad8475SAndrea Arcangeli might_sleep(); 399847ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; i++) { 399947ad8475SAndrea Arcangeli cond_resched(); 400047ad8475SAndrea Arcangeli clear_user_highpage(page + i, addr + i * PAGE_SIZE); 400147ad8475SAndrea Arcangeli } 400247ad8475SAndrea Arcangeli } 400347ad8475SAndrea Arcangeli 400447ad8475SAndrea Arcangeli static void copy_user_gigantic_page(struct page *dst, struct page *src, 400547ad8475SAndrea Arcangeli unsigned long addr, 400647ad8475SAndrea Arcangeli struct vm_area_struct *vma, 400747ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 400847ad8475SAndrea Arcangeli { 400947ad8475SAndrea Arcangeli int i; 401047ad8475SAndrea Arcangeli struct page *dst_base = dst; 401147ad8475SAndrea Arcangeli struct page *src_base = src; 401247ad8475SAndrea Arcangeli 401347ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; ) { 401447ad8475SAndrea Arcangeli cond_resched(); 401547ad8475SAndrea Arcangeli copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma); 401647ad8475SAndrea Arcangeli 401747ad8475SAndrea Arcangeli i++; 401847ad8475SAndrea Arcangeli dst = mem_map_next(dst, dst_base, i); 401947ad8475SAndrea Arcangeli src = mem_map_next(src, src_base, i); 402047ad8475SAndrea Arcangeli } 402147ad8475SAndrea Arcangeli } 402247ad8475SAndrea Arcangeli 402347ad8475SAndrea Arcangeli void copy_user_huge_page(struct page *dst, struct page *src, 402447ad8475SAndrea Arcangeli unsigned long addr, struct vm_area_struct *vma, 402547ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 402647ad8475SAndrea Arcangeli { 402747ad8475SAndrea Arcangeli int i; 402847ad8475SAndrea Arcangeli 402947ad8475SAndrea Arcangeli if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { 403047ad8475SAndrea Arcangeli copy_user_gigantic_page(dst, src, addr, vma, 403147ad8475SAndrea Arcangeli pages_per_huge_page); 403247ad8475SAndrea Arcangeli return; 403347ad8475SAndrea Arcangeli } 403447ad8475SAndrea Arcangeli 403547ad8475SAndrea Arcangeli might_sleep(); 403647ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; i++) { 403747ad8475SAndrea Arcangeli cond_resched(); 403847ad8475SAndrea Arcangeli copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma); 403947ad8475SAndrea Arcangeli } 404047ad8475SAndrea Arcangeli } 404147ad8475SAndrea Arcangeli #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */ 4042