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> 5301c8f1c4SDan Williams #include <linux/pfn_t.h> 54edc79b2aSPeter Zijlstra #include <linux/writeback.h> 558a9f3ccdSBalbir Singh #include <linux/memcontrol.h> 56cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h> 573dc14741SHugh Dickins #include <linux/kallsyms.h> 583dc14741SHugh Dickins #include <linux/swapops.h> 593dc14741SHugh Dickins #include <linux/elf.h> 605a0e3ad6STejun Heo #include <linux/gfp.h> 614daae3b4SMel Gorman #include <linux/migrate.h> 622fbc57c5SAndy Shevchenko #include <linux/string.h> 630abdd7a8SDan Williams #include <linux/dma-debug.h> 641592eef0SKirill A. Shutemov #include <linux/debugfs.h> 656b251fc9SAndrea Arcangeli #include <linux/userfaultfd_k.h> 66bc2466e4SJan Kara #include <linux/dax.h> 671da177e4SLinus Torvalds 686952b61dSAlexey Dobriyan #include <asm/io.h> 6933a709b2SDave Hansen #include <asm/mmu_context.h> 701da177e4SLinus Torvalds #include <asm/pgalloc.h> 711da177e4SLinus Torvalds #include <asm/uaccess.h> 721da177e4SLinus Torvalds #include <asm/tlb.h> 731da177e4SLinus Torvalds #include <asm/tlbflush.h> 741da177e4SLinus Torvalds #include <asm/pgtable.h> 751da177e4SLinus Torvalds 7642b77728SJan Beulich #include "internal.h" 7742b77728SJan Beulich 7890572890SPeter Zijlstra #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS 7990572890SPeter Zijlstra #warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid. 8075980e97SPeter Zijlstra #endif 8175980e97SPeter Zijlstra 82d41dee36SAndy Whitcroft #ifndef CONFIG_NEED_MULTIPLE_NODES 831da177e4SLinus Torvalds /* use the per-pgdat data instead for discontigmem - mbligh */ 841da177e4SLinus Torvalds unsigned long max_mapnr; 851da177e4SLinus Torvalds struct page *mem_map; 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds EXPORT_SYMBOL(max_mapnr); 881da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map); 891da177e4SLinus Torvalds #endif 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds /* 921da177e4SLinus Torvalds * A number of key systems in x86 including ioremap() rely on the assumption 931da177e4SLinus Torvalds * that high_memory defines the upper bound on direct map memory, then end 941da177e4SLinus Torvalds * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and 951da177e4SLinus Torvalds * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL 961da177e4SLinus Torvalds * and ZONE_HIGHMEM. 971da177e4SLinus Torvalds */ 981da177e4SLinus Torvalds void * high_memory; 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds EXPORT_SYMBOL(high_memory); 1011da177e4SLinus Torvalds 10232a93233SIngo Molnar /* 10332a93233SIngo Molnar * Randomize the address space (stacks, mmaps, brk, etc.). 10432a93233SIngo Molnar * 10532a93233SIngo Molnar * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization, 10632a93233SIngo Molnar * as ancient (libc5 based) binaries can segfault. ) 10732a93233SIngo Molnar */ 10832a93233SIngo Molnar int randomize_va_space __read_mostly = 10932a93233SIngo Molnar #ifdef CONFIG_COMPAT_BRK 11032a93233SIngo Molnar 1; 11132a93233SIngo Molnar #else 11232a93233SIngo Molnar 2; 11332a93233SIngo Molnar #endif 114a62eaf15SAndi Kleen 115a62eaf15SAndi Kleen static int __init disable_randmaps(char *s) 116a62eaf15SAndi Kleen { 117a62eaf15SAndi Kleen randomize_va_space = 0; 1189b41046cSOGAWA Hirofumi return 1; 119a62eaf15SAndi Kleen } 120a62eaf15SAndi Kleen __setup("norandmaps", disable_randmaps); 121a62eaf15SAndi Kleen 12262eede62SHugh Dickins unsigned long zero_pfn __read_mostly; 12303f6462aSHugh Dickins unsigned long highest_memmap_pfn __read_mostly; 124a13ea5b7SHugh Dickins 1250b70068eSArd Biesheuvel EXPORT_SYMBOL(zero_pfn); 1260b70068eSArd Biesheuvel 127a13ea5b7SHugh Dickins /* 128a13ea5b7SHugh Dickins * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init() 129a13ea5b7SHugh Dickins */ 130a13ea5b7SHugh Dickins static int __init init_zero_pfn(void) 131a13ea5b7SHugh Dickins { 132a13ea5b7SHugh Dickins zero_pfn = page_to_pfn(ZERO_PAGE(0)); 133a13ea5b7SHugh Dickins return 0; 134a13ea5b7SHugh Dickins } 135a13ea5b7SHugh Dickins core_initcall(init_zero_pfn); 136a62eaf15SAndi Kleen 137d559db08SKAMEZAWA Hiroyuki 13834e55232SKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING) 13934e55232SKAMEZAWA Hiroyuki 140ea48cf78SDavid Rientjes void sync_mm_rss(struct mm_struct *mm) 14134e55232SKAMEZAWA Hiroyuki { 14234e55232SKAMEZAWA Hiroyuki int i; 14334e55232SKAMEZAWA Hiroyuki 14434e55232SKAMEZAWA Hiroyuki for (i = 0; i < NR_MM_COUNTERS; i++) { 14505af2e10SDavid Rientjes if (current->rss_stat.count[i]) { 14605af2e10SDavid Rientjes add_mm_counter(mm, i, current->rss_stat.count[i]); 14705af2e10SDavid Rientjes current->rss_stat.count[i] = 0; 14834e55232SKAMEZAWA Hiroyuki } 14934e55232SKAMEZAWA Hiroyuki } 15005af2e10SDavid Rientjes current->rss_stat.events = 0; 15134e55232SKAMEZAWA Hiroyuki } 15234e55232SKAMEZAWA Hiroyuki 15334e55232SKAMEZAWA Hiroyuki static void add_mm_counter_fast(struct mm_struct *mm, int member, int val) 15434e55232SKAMEZAWA Hiroyuki { 15534e55232SKAMEZAWA Hiroyuki struct task_struct *task = current; 15634e55232SKAMEZAWA Hiroyuki 15734e55232SKAMEZAWA Hiroyuki if (likely(task->mm == mm)) 15834e55232SKAMEZAWA Hiroyuki task->rss_stat.count[member] += val; 15934e55232SKAMEZAWA Hiroyuki else 16034e55232SKAMEZAWA Hiroyuki add_mm_counter(mm, member, val); 16134e55232SKAMEZAWA Hiroyuki } 16234e55232SKAMEZAWA Hiroyuki #define inc_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, 1) 16334e55232SKAMEZAWA Hiroyuki #define dec_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, -1) 16434e55232SKAMEZAWA Hiroyuki 16534e55232SKAMEZAWA Hiroyuki /* sync counter once per 64 page faults */ 16634e55232SKAMEZAWA Hiroyuki #define TASK_RSS_EVENTS_THRESH (64) 16734e55232SKAMEZAWA Hiroyuki static void check_sync_rss_stat(struct task_struct *task) 16834e55232SKAMEZAWA Hiroyuki { 16934e55232SKAMEZAWA Hiroyuki if (unlikely(task != current)) 17034e55232SKAMEZAWA Hiroyuki return; 17134e55232SKAMEZAWA Hiroyuki if (unlikely(task->rss_stat.events++ > TASK_RSS_EVENTS_THRESH)) 172ea48cf78SDavid Rientjes sync_mm_rss(task->mm); 17334e55232SKAMEZAWA Hiroyuki } 1749547d01bSPeter Zijlstra #else /* SPLIT_RSS_COUNTING */ 17534e55232SKAMEZAWA Hiroyuki 17634e55232SKAMEZAWA Hiroyuki #define inc_mm_counter_fast(mm, member) inc_mm_counter(mm, member) 17734e55232SKAMEZAWA Hiroyuki #define dec_mm_counter_fast(mm, member) dec_mm_counter(mm, member) 17834e55232SKAMEZAWA Hiroyuki 17934e55232SKAMEZAWA Hiroyuki static void check_sync_rss_stat(struct task_struct *task) 18034e55232SKAMEZAWA Hiroyuki { 18134e55232SKAMEZAWA Hiroyuki } 18234e55232SKAMEZAWA Hiroyuki 1839547d01bSPeter Zijlstra #endif /* SPLIT_RSS_COUNTING */ 1849547d01bSPeter Zijlstra 1859547d01bSPeter Zijlstra #ifdef HAVE_GENERIC_MMU_GATHER 1869547d01bSPeter Zijlstra 187ca1d6c7dSNicholas Krause static bool tlb_next_batch(struct mmu_gather *tlb) 1889547d01bSPeter Zijlstra { 1899547d01bSPeter Zijlstra struct mmu_gather_batch *batch; 1909547d01bSPeter Zijlstra 1919547d01bSPeter Zijlstra batch = tlb->active; 1929547d01bSPeter Zijlstra if (batch->next) { 1939547d01bSPeter Zijlstra tlb->active = batch->next; 194ca1d6c7dSNicholas Krause return true; 1959547d01bSPeter Zijlstra } 1969547d01bSPeter Zijlstra 19753a59fc6SMichal Hocko if (tlb->batch_count == MAX_GATHER_BATCH_COUNT) 198ca1d6c7dSNicholas Krause return false; 19953a59fc6SMichal Hocko 2009547d01bSPeter Zijlstra batch = (void *)__get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0); 2019547d01bSPeter Zijlstra if (!batch) 202ca1d6c7dSNicholas Krause return false; 2039547d01bSPeter Zijlstra 20453a59fc6SMichal Hocko tlb->batch_count++; 2059547d01bSPeter Zijlstra batch->next = NULL; 2069547d01bSPeter Zijlstra batch->nr = 0; 2079547d01bSPeter Zijlstra batch->max = MAX_GATHER_BATCH; 2089547d01bSPeter Zijlstra 2099547d01bSPeter Zijlstra tlb->active->next = batch; 2109547d01bSPeter Zijlstra tlb->active = batch; 2119547d01bSPeter Zijlstra 212ca1d6c7dSNicholas Krause return true; 2139547d01bSPeter Zijlstra } 2149547d01bSPeter Zijlstra 2159547d01bSPeter Zijlstra /* tlb_gather_mmu 2169547d01bSPeter Zijlstra * Called to initialize an (on-stack) mmu_gather structure for page-table 2179547d01bSPeter Zijlstra * tear-down from @mm. The @fullmm argument is used when @mm is without 2189547d01bSPeter Zijlstra * users and we're going to destroy the full address space (exit/execve). 2199547d01bSPeter Zijlstra */ 2202b047252SLinus Torvalds void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long start, unsigned long end) 2219547d01bSPeter Zijlstra { 2229547d01bSPeter Zijlstra tlb->mm = mm; 2239547d01bSPeter Zijlstra 2242b047252SLinus Torvalds /* Is it from 0 to ~0? */ 2252b047252SLinus Torvalds tlb->fullmm = !(start | (end+1)); 2261de14c3cSDave Hansen tlb->need_flush_all = 0; 2279547d01bSPeter Zijlstra tlb->local.next = NULL; 2289547d01bSPeter Zijlstra tlb->local.nr = 0; 2299547d01bSPeter Zijlstra tlb->local.max = ARRAY_SIZE(tlb->__pages); 2309547d01bSPeter Zijlstra tlb->active = &tlb->local; 23153a59fc6SMichal Hocko tlb->batch_count = 0; 2329547d01bSPeter Zijlstra 2339547d01bSPeter Zijlstra #ifdef CONFIG_HAVE_RCU_TABLE_FREE 2349547d01bSPeter Zijlstra tlb->batch = NULL; 23534e55232SKAMEZAWA Hiroyuki #endif 236e77b0852SAneesh Kumar K.V tlb->page_size = 0; 237fb7332a9SWill Deacon 238fb7332a9SWill Deacon __tlb_reset_range(tlb); 2399547d01bSPeter Zijlstra } 2409547d01bSPeter Zijlstra 2411cf35d47SLinus Torvalds static void tlb_flush_mmu_tlbonly(struct mmu_gather *tlb) 2429547d01bSPeter Zijlstra { 243721c21c1SWill Deacon if (!tlb->end) 244721c21c1SWill Deacon return; 245721c21c1SWill Deacon 2469547d01bSPeter Zijlstra tlb_flush(tlb); 24734ee645eSJoerg Roedel mmu_notifier_invalidate_range(tlb->mm, tlb->start, tlb->end); 2489547d01bSPeter Zijlstra #ifdef CONFIG_HAVE_RCU_TABLE_FREE 2499547d01bSPeter Zijlstra tlb_table_flush(tlb); 2509547d01bSPeter Zijlstra #endif 251fb7332a9SWill Deacon __tlb_reset_range(tlb); 2521cf35d47SLinus Torvalds } 2531cf35d47SLinus Torvalds 2541cf35d47SLinus Torvalds static void tlb_flush_mmu_free(struct mmu_gather *tlb) 2551cf35d47SLinus Torvalds { 2561cf35d47SLinus Torvalds struct mmu_gather_batch *batch; 2579547d01bSPeter Zijlstra 258721c21c1SWill Deacon for (batch = &tlb->local; batch && batch->nr; batch = batch->next) { 2599547d01bSPeter Zijlstra free_pages_and_swap_cache(batch->pages, batch->nr); 2609547d01bSPeter Zijlstra batch->nr = 0; 2619547d01bSPeter Zijlstra } 2629547d01bSPeter Zijlstra tlb->active = &tlb->local; 2639547d01bSPeter Zijlstra } 2649547d01bSPeter Zijlstra 2651cf35d47SLinus Torvalds void tlb_flush_mmu(struct mmu_gather *tlb) 2661cf35d47SLinus Torvalds { 2671cf35d47SLinus Torvalds tlb_flush_mmu_tlbonly(tlb); 2681cf35d47SLinus Torvalds tlb_flush_mmu_free(tlb); 2691cf35d47SLinus Torvalds } 2701cf35d47SLinus Torvalds 2719547d01bSPeter Zijlstra /* tlb_finish_mmu 2729547d01bSPeter Zijlstra * Called at the end of the shootdown operation to free up any resources 2739547d01bSPeter Zijlstra * that were required. 2749547d01bSPeter Zijlstra */ 2759547d01bSPeter Zijlstra void tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end) 2769547d01bSPeter Zijlstra { 2779547d01bSPeter Zijlstra struct mmu_gather_batch *batch, *next; 2789547d01bSPeter Zijlstra 2799547d01bSPeter Zijlstra tlb_flush_mmu(tlb); 2809547d01bSPeter Zijlstra 2819547d01bSPeter Zijlstra /* keep the page table cache within bounds */ 2829547d01bSPeter Zijlstra check_pgt_cache(); 2839547d01bSPeter Zijlstra 2849547d01bSPeter Zijlstra for (batch = tlb->local.next; batch; batch = next) { 2859547d01bSPeter Zijlstra next = batch->next; 2869547d01bSPeter Zijlstra free_pages((unsigned long)batch, 0); 2879547d01bSPeter Zijlstra } 2889547d01bSPeter Zijlstra tlb->local.next = NULL; 2899547d01bSPeter Zijlstra } 2909547d01bSPeter Zijlstra 2919547d01bSPeter Zijlstra /* __tlb_remove_page 2929547d01bSPeter Zijlstra * Must perform the equivalent to __free_pte(pte_get_and_clear(ptep)), while 2939547d01bSPeter Zijlstra * handling the additional races in SMP caused by other CPUs caching valid 2949547d01bSPeter Zijlstra * mappings in their TLBs. Returns the number of free page slots left. 2959547d01bSPeter Zijlstra * When out of page slots we must call tlb_flush_mmu(). 296e9d55e15SAneesh Kumar K.V *returns true if the caller should flush. 2979547d01bSPeter Zijlstra */ 298e77b0852SAneesh Kumar K.V bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page, int page_size) 2999547d01bSPeter Zijlstra { 3009547d01bSPeter Zijlstra struct mmu_gather_batch *batch; 3019547d01bSPeter Zijlstra 302fb7332a9SWill Deacon VM_BUG_ON(!tlb->end); 303692a68c1SAneesh Kumar K.V VM_WARN_ON(tlb->page_size != page_size); 304e77b0852SAneesh Kumar K.V 3059547d01bSPeter Zijlstra batch = tlb->active; 306692a68c1SAneesh Kumar K.V /* 307692a68c1SAneesh Kumar K.V * Add the page and check if we are full. If so 308692a68c1SAneesh Kumar K.V * force a flush. 309692a68c1SAneesh Kumar K.V */ 310692a68c1SAneesh Kumar K.V batch->pages[batch->nr++] = page; 3119547d01bSPeter Zijlstra if (batch->nr == batch->max) { 3129547d01bSPeter Zijlstra if (!tlb_next_batch(tlb)) 313e9d55e15SAneesh Kumar K.V return true; 3140b43c3aaSShaohua Li batch = tlb->active; 3159547d01bSPeter Zijlstra } 316309381feSSasha Levin VM_BUG_ON_PAGE(batch->nr > batch->max, page); 3179547d01bSPeter Zijlstra 318e9d55e15SAneesh Kumar K.V return false; 3199547d01bSPeter Zijlstra } 3209547d01bSPeter Zijlstra 3219547d01bSPeter Zijlstra #endif /* HAVE_GENERIC_MMU_GATHER */ 32234e55232SKAMEZAWA Hiroyuki 32326723911SPeter Zijlstra #ifdef CONFIG_HAVE_RCU_TABLE_FREE 32426723911SPeter Zijlstra 32526723911SPeter Zijlstra /* 32626723911SPeter Zijlstra * See the comment near struct mmu_table_batch. 32726723911SPeter Zijlstra */ 32826723911SPeter Zijlstra 32926723911SPeter Zijlstra static void tlb_remove_table_smp_sync(void *arg) 33026723911SPeter Zijlstra { 33126723911SPeter Zijlstra /* Simply deliver the interrupt */ 33226723911SPeter Zijlstra } 33326723911SPeter Zijlstra 33426723911SPeter Zijlstra static void tlb_remove_table_one(void *table) 33526723911SPeter Zijlstra { 33626723911SPeter Zijlstra /* 33726723911SPeter Zijlstra * This isn't an RCU grace period and hence the page-tables cannot be 33826723911SPeter Zijlstra * assumed to be actually RCU-freed. 33926723911SPeter Zijlstra * 34026723911SPeter Zijlstra * It is however sufficient for software page-table walkers that rely on 34126723911SPeter Zijlstra * IRQ disabling. See the comment near struct mmu_table_batch. 34226723911SPeter Zijlstra */ 34326723911SPeter Zijlstra smp_call_function(tlb_remove_table_smp_sync, NULL, 1); 34426723911SPeter Zijlstra __tlb_remove_table(table); 34526723911SPeter Zijlstra } 34626723911SPeter Zijlstra 34726723911SPeter Zijlstra static void tlb_remove_table_rcu(struct rcu_head *head) 34826723911SPeter Zijlstra { 34926723911SPeter Zijlstra struct mmu_table_batch *batch; 35026723911SPeter Zijlstra int i; 35126723911SPeter Zijlstra 35226723911SPeter Zijlstra batch = container_of(head, struct mmu_table_batch, rcu); 35326723911SPeter Zijlstra 35426723911SPeter Zijlstra for (i = 0; i < batch->nr; i++) 35526723911SPeter Zijlstra __tlb_remove_table(batch->tables[i]); 35626723911SPeter Zijlstra 35726723911SPeter Zijlstra free_page((unsigned long)batch); 35826723911SPeter Zijlstra } 35926723911SPeter Zijlstra 36026723911SPeter Zijlstra void tlb_table_flush(struct mmu_gather *tlb) 36126723911SPeter Zijlstra { 36226723911SPeter Zijlstra struct mmu_table_batch **batch = &tlb->batch; 36326723911SPeter Zijlstra 36426723911SPeter Zijlstra if (*batch) { 36526723911SPeter Zijlstra call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu); 36626723911SPeter Zijlstra *batch = NULL; 36726723911SPeter Zijlstra } 36826723911SPeter Zijlstra } 36926723911SPeter Zijlstra 37026723911SPeter Zijlstra void tlb_remove_table(struct mmu_gather *tlb, void *table) 37126723911SPeter Zijlstra { 37226723911SPeter Zijlstra struct mmu_table_batch **batch = &tlb->batch; 37326723911SPeter Zijlstra 37426723911SPeter Zijlstra /* 37526723911SPeter Zijlstra * When there's less then two users of this mm there cannot be a 37626723911SPeter Zijlstra * concurrent page-table walk. 37726723911SPeter Zijlstra */ 37826723911SPeter Zijlstra if (atomic_read(&tlb->mm->mm_users) < 2) { 37926723911SPeter Zijlstra __tlb_remove_table(table); 38026723911SPeter Zijlstra return; 38126723911SPeter Zijlstra } 38226723911SPeter Zijlstra 38326723911SPeter Zijlstra if (*batch == NULL) { 38426723911SPeter Zijlstra *batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN); 38526723911SPeter Zijlstra if (*batch == NULL) { 38626723911SPeter Zijlstra tlb_remove_table_one(table); 38726723911SPeter Zijlstra return; 38826723911SPeter Zijlstra } 38926723911SPeter Zijlstra (*batch)->nr = 0; 39026723911SPeter Zijlstra } 39126723911SPeter Zijlstra (*batch)->tables[(*batch)->nr++] = table; 39226723911SPeter Zijlstra if ((*batch)->nr == MAX_TABLE_BATCH) 39326723911SPeter Zijlstra tlb_table_flush(tlb); 39426723911SPeter Zijlstra } 39526723911SPeter Zijlstra 3969547d01bSPeter Zijlstra #endif /* CONFIG_HAVE_RCU_TABLE_FREE */ 39726723911SPeter Zijlstra 3981da177e4SLinus Torvalds /* 3991da177e4SLinus Torvalds * Note: this doesn't free the actual pages themselves. That 4001da177e4SLinus Torvalds * has been handled earlier when unmapping all the memory regions. 4011da177e4SLinus Torvalds */ 4029e1b32caSBenjamin Herrenschmidt static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd, 4039e1b32caSBenjamin Herrenschmidt unsigned long addr) 4041da177e4SLinus Torvalds { 4052f569afdSMartin Schwidefsky pgtable_t token = pmd_pgtable(*pmd); 4061da177e4SLinus Torvalds pmd_clear(pmd); 4079e1b32caSBenjamin Herrenschmidt pte_free_tlb(tlb, token, addr); 408e1f56c89SKirill A. Shutemov atomic_long_dec(&tlb->mm->nr_ptes); 4091da177e4SLinus Torvalds } 4101da177e4SLinus Torvalds 411e0da382cSHugh Dickins static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud, 412e0da382cSHugh Dickins unsigned long addr, unsigned long end, 413e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 4141da177e4SLinus Torvalds { 4151da177e4SLinus Torvalds pmd_t *pmd; 4161da177e4SLinus Torvalds unsigned long next; 417e0da382cSHugh Dickins unsigned long start; 4181da177e4SLinus Torvalds 419e0da382cSHugh Dickins start = addr; 4201da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 4211da177e4SLinus Torvalds do { 4221da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 4231da177e4SLinus Torvalds if (pmd_none_or_clear_bad(pmd)) 4241da177e4SLinus Torvalds continue; 4259e1b32caSBenjamin Herrenschmidt free_pte_range(tlb, pmd, addr); 4261da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 4271da177e4SLinus Torvalds 428e0da382cSHugh Dickins start &= PUD_MASK; 429e0da382cSHugh Dickins if (start < floor) 430e0da382cSHugh Dickins return; 431e0da382cSHugh Dickins if (ceiling) { 432e0da382cSHugh Dickins ceiling &= PUD_MASK; 433e0da382cSHugh Dickins if (!ceiling) 434e0da382cSHugh Dickins return; 4351da177e4SLinus Torvalds } 436e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 437e0da382cSHugh Dickins return; 438e0da382cSHugh Dickins 439e0da382cSHugh Dickins pmd = pmd_offset(pud, start); 440e0da382cSHugh Dickins pud_clear(pud); 4419e1b32caSBenjamin Herrenschmidt pmd_free_tlb(tlb, pmd, start); 442dc6c9a35SKirill A. Shutemov mm_dec_nr_pmds(tlb->mm); 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. 4801da177e4SLinus Torvalds */ 48142b77728SJan Beulich void free_pgd_range(struct mmu_gather *tlb, 482e0da382cSHugh Dickins unsigned long addr, unsigned long end, 483e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 4841da177e4SLinus Torvalds { 4851da177e4SLinus Torvalds pgd_t *pgd; 4861da177e4SLinus Torvalds unsigned long next; 4871da177e4SLinus Torvalds 488e0da382cSHugh Dickins /* 489e0da382cSHugh Dickins * The next few lines have given us lots of grief... 490e0da382cSHugh Dickins * 491e0da382cSHugh Dickins * Why are we testing PMD* at this top level? Because often 492e0da382cSHugh Dickins * there will be no work to do at all, and we'd prefer not to 493e0da382cSHugh Dickins * go all the way down to the bottom just to discover that. 494e0da382cSHugh Dickins * 495e0da382cSHugh Dickins * Why all these "- 1"s? Because 0 represents both the bottom 496e0da382cSHugh Dickins * of the address space and the top of it (using -1 for the 497e0da382cSHugh Dickins * top wouldn't help much: the masks would do the wrong thing). 498e0da382cSHugh Dickins * The rule is that addr 0 and floor 0 refer to the bottom of 499e0da382cSHugh Dickins * the address space, but end 0 and ceiling 0 refer to the top 500e0da382cSHugh Dickins * Comparisons need to use "end - 1" and "ceiling - 1" (though 501e0da382cSHugh Dickins * that end 0 case should be mythical). 502e0da382cSHugh Dickins * 503e0da382cSHugh Dickins * Wherever addr is brought up or ceiling brought down, we must 504e0da382cSHugh Dickins * be careful to reject "the opposite 0" before it confuses the 505e0da382cSHugh Dickins * subsequent tests. But what about where end is brought down 506e0da382cSHugh Dickins * by PMD_SIZE below? no, end can't go down to 0 there. 507e0da382cSHugh Dickins * 508e0da382cSHugh Dickins * Whereas we round start (addr) and ceiling down, by different 509e0da382cSHugh Dickins * masks at different levels, in order to test whether a table 510e0da382cSHugh Dickins * now has no other vmas using it, so can be freed, we don't 511e0da382cSHugh Dickins * bother to round floor or end up - the tests don't need that. 512e0da382cSHugh Dickins */ 513e0da382cSHugh Dickins 514e0da382cSHugh Dickins addr &= PMD_MASK; 515e0da382cSHugh Dickins if (addr < floor) { 516e0da382cSHugh Dickins addr += PMD_SIZE; 517e0da382cSHugh Dickins if (!addr) 518e0da382cSHugh Dickins return; 519e0da382cSHugh Dickins } 520e0da382cSHugh Dickins if (ceiling) { 521e0da382cSHugh Dickins ceiling &= PMD_MASK; 522e0da382cSHugh Dickins if (!ceiling) 523e0da382cSHugh Dickins return; 524e0da382cSHugh Dickins } 525e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 526e0da382cSHugh Dickins end -= PMD_SIZE; 527e0da382cSHugh Dickins if (addr > end - 1) 528e0da382cSHugh Dickins return; 52907e32661SAneesh Kumar K.V /* 53007e32661SAneesh Kumar K.V * We add page table cache pages with PAGE_SIZE, 53107e32661SAneesh Kumar K.V * (see pte_free_tlb()), flush the tlb if we need 53207e32661SAneesh Kumar K.V */ 53307e32661SAneesh Kumar K.V tlb_remove_check_page_size_change(tlb, PAGE_SIZE); 53442b77728SJan Beulich pgd = pgd_offset(tlb->mm, addr); 5351da177e4SLinus Torvalds do { 5361da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 5371da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 5381da177e4SLinus Torvalds continue; 53942b77728SJan Beulich free_pud_range(tlb, pgd, addr, next, floor, ceiling); 5401da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 541e0da382cSHugh Dickins } 542e0da382cSHugh Dickins 54342b77728SJan Beulich void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma, 544e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 545e0da382cSHugh Dickins { 546e0da382cSHugh Dickins while (vma) { 547e0da382cSHugh Dickins struct vm_area_struct *next = vma->vm_next; 548e0da382cSHugh Dickins unsigned long addr = vma->vm_start; 549e0da382cSHugh Dickins 5508f4f8c16SHugh Dickins /* 55125d9e2d1Snpiggin@suse.de * Hide vma from rmap and truncate_pagecache before freeing 55225d9e2d1Snpiggin@suse.de * pgtables 5538f4f8c16SHugh Dickins */ 5545beb4930SRik van Riel unlink_anon_vmas(vma); 5558f4f8c16SHugh Dickins unlink_file_vma(vma); 5568f4f8c16SHugh Dickins 5579da61aefSDavid Gibson if (is_vm_hugetlb_page(vma)) { 5583bf5ee95SHugh Dickins hugetlb_free_pgd_range(tlb, addr, vma->vm_end, 5593bf5ee95SHugh Dickins floor, next? next->vm_start: ceiling); 5603bf5ee95SHugh Dickins } else { 5613bf5ee95SHugh Dickins /* 5623bf5ee95SHugh Dickins * Optimization: gather nearby vmas into one call down 5633bf5ee95SHugh Dickins */ 5643bf5ee95SHugh Dickins while (next && next->vm_start <= vma->vm_end + PMD_SIZE 5654866920bSDavid Gibson && !is_vm_hugetlb_page(next)) { 566e0da382cSHugh Dickins vma = next; 567e0da382cSHugh Dickins next = vma->vm_next; 5685beb4930SRik van Riel unlink_anon_vmas(vma); 5698f4f8c16SHugh Dickins unlink_file_vma(vma); 570e0da382cSHugh Dickins } 5713bf5ee95SHugh Dickins free_pgd_range(tlb, addr, vma->vm_end, 572e0da382cSHugh Dickins floor, next? next->vm_start: ceiling); 5733bf5ee95SHugh Dickins } 574e0da382cSHugh Dickins vma = next; 575e0da382cSHugh Dickins } 5761da177e4SLinus Torvalds } 5771da177e4SLinus Torvalds 5783ed3a4f0SKirill A. Shutemov int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address) 5791da177e4SLinus Torvalds { 580c4088ebdSKirill A. Shutemov spinlock_t *ptl; 5812f569afdSMartin Schwidefsky pgtable_t new = pte_alloc_one(mm, address); 5821da177e4SLinus Torvalds if (!new) 5831bb3630eSHugh Dickins return -ENOMEM; 5841bb3630eSHugh Dickins 585362a61adSNick Piggin /* 586362a61adSNick Piggin * Ensure all pte setup (eg. pte page lock and page clearing) are 587362a61adSNick Piggin * visible before the pte is made visible to other CPUs by being 588362a61adSNick Piggin * put into page tables. 589362a61adSNick Piggin * 590362a61adSNick Piggin * The other side of the story is the pointer chasing in the page 591362a61adSNick Piggin * table walking code (when walking the page table without locking; 592362a61adSNick Piggin * ie. most of the time). Fortunately, these data accesses consist 593362a61adSNick Piggin * of a chain of data-dependent loads, meaning most CPUs (alpha 594362a61adSNick Piggin * being the notable exception) will already guarantee loads are 595362a61adSNick Piggin * seen in-order. See the alpha page table accessors for the 596362a61adSNick Piggin * smp_read_barrier_depends() barriers in page table walking code. 597362a61adSNick Piggin */ 598362a61adSNick Piggin smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */ 599362a61adSNick Piggin 600c4088ebdSKirill A. Shutemov ptl = pmd_lock(mm, pmd); 6018ac1f832SAndrea Arcangeli if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 602e1f56c89SKirill A. Shutemov atomic_long_inc(&mm->nr_ptes); 6031da177e4SLinus Torvalds pmd_populate(mm, pmd, new); 6042f569afdSMartin Schwidefsky new = NULL; 6054b471e88SKirill A. Shutemov } 606c4088ebdSKirill A. Shutemov spin_unlock(ptl); 6072f569afdSMartin Schwidefsky if (new) 6082f569afdSMartin Schwidefsky pte_free(mm, new); 6091bb3630eSHugh Dickins return 0; 6101da177e4SLinus Torvalds } 6111da177e4SLinus Torvalds 6121bb3630eSHugh Dickins int __pte_alloc_kernel(pmd_t *pmd, unsigned long address) 6131da177e4SLinus Torvalds { 6141bb3630eSHugh Dickins pte_t *new = pte_alloc_one_kernel(&init_mm, address); 6151da177e4SLinus Torvalds if (!new) 6161bb3630eSHugh Dickins return -ENOMEM; 6171da177e4SLinus Torvalds 618362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 619362a61adSNick Piggin 620872fec16SHugh Dickins spin_lock(&init_mm.page_table_lock); 6218ac1f832SAndrea Arcangeli if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 622872fec16SHugh Dickins pmd_populate_kernel(&init_mm, pmd, new); 6232f569afdSMartin Schwidefsky new = NULL; 6244b471e88SKirill A. Shutemov } 625872fec16SHugh Dickins spin_unlock(&init_mm.page_table_lock); 6262f569afdSMartin Schwidefsky if (new) 6272f569afdSMartin Schwidefsky pte_free_kernel(&init_mm, new); 6281bb3630eSHugh Dickins return 0; 6291da177e4SLinus Torvalds } 6301da177e4SLinus Torvalds 631d559db08SKAMEZAWA Hiroyuki static inline void init_rss_vec(int *rss) 632ae859762SHugh Dickins { 633d559db08SKAMEZAWA Hiroyuki memset(rss, 0, sizeof(int) * NR_MM_COUNTERS); 634d559db08SKAMEZAWA Hiroyuki } 635d559db08SKAMEZAWA Hiroyuki 636d559db08SKAMEZAWA Hiroyuki static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss) 637d559db08SKAMEZAWA Hiroyuki { 638d559db08SKAMEZAWA Hiroyuki int i; 639d559db08SKAMEZAWA Hiroyuki 64034e55232SKAMEZAWA Hiroyuki if (current->mm == mm) 64105af2e10SDavid Rientjes sync_mm_rss(mm); 642d559db08SKAMEZAWA Hiroyuki for (i = 0; i < NR_MM_COUNTERS; i++) 643d559db08SKAMEZAWA Hiroyuki if (rss[i]) 644d559db08SKAMEZAWA Hiroyuki add_mm_counter(mm, i, rss[i]); 645ae859762SHugh Dickins } 646ae859762SHugh Dickins 6471da177e4SLinus Torvalds /* 6486aab341eSLinus Torvalds * This function is called to print an error when a bad pte 6496aab341eSLinus Torvalds * is found. For example, we might have a PFN-mapped pte in 6506aab341eSLinus Torvalds * a region that doesn't allow it. 651b5810039SNick Piggin * 652b5810039SNick Piggin * The calling function must still handle the error. 653b5810039SNick Piggin */ 6543dc14741SHugh Dickins static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr, 6553dc14741SHugh Dickins pte_t pte, struct page *page) 656b5810039SNick Piggin { 6573dc14741SHugh Dickins pgd_t *pgd = pgd_offset(vma->vm_mm, addr); 6583dc14741SHugh Dickins pud_t *pud = pud_offset(pgd, addr); 6593dc14741SHugh Dickins pmd_t *pmd = pmd_offset(pud, addr); 6603dc14741SHugh Dickins struct address_space *mapping; 6613dc14741SHugh Dickins pgoff_t index; 662d936cf9bSHugh Dickins static unsigned long resume; 663d936cf9bSHugh Dickins static unsigned long nr_shown; 664d936cf9bSHugh Dickins static unsigned long nr_unshown; 665d936cf9bSHugh Dickins 666d936cf9bSHugh Dickins /* 667d936cf9bSHugh Dickins * Allow a burst of 60 reports, then keep quiet for that minute; 668d936cf9bSHugh Dickins * or allow a steady drip of one report per second. 669d936cf9bSHugh Dickins */ 670d936cf9bSHugh Dickins if (nr_shown == 60) { 671d936cf9bSHugh Dickins if (time_before(jiffies, resume)) { 672d936cf9bSHugh Dickins nr_unshown++; 673d936cf9bSHugh Dickins return; 674d936cf9bSHugh Dickins } 675d936cf9bSHugh Dickins if (nr_unshown) { 6761170532bSJoe Perches pr_alert("BUG: Bad page map: %lu messages suppressed\n", 677d936cf9bSHugh Dickins nr_unshown); 678d936cf9bSHugh Dickins nr_unshown = 0; 679d936cf9bSHugh Dickins } 680d936cf9bSHugh Dickins nr_shown = 0; 681d936cf9bSHugh Dickins } 682d936cf9bSHugh Dickins if (nr_shown++ == 0) 683d936cf9bSHugh Dickins resume = jiffies + 60 * HZ; 6843dc14741SHugh Dickins 6853dc14741SHugh Dickins mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL; 6863dc14741SHugh Dickins index = linear_page_index(vma, addr); 6873dc14741SHugh Dickins 6881170532bSJoe Perches pr_alert("BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n", 6893dc14741SHugh Dickins current->comm, 6903dc14741SHugh Dickins (long long)pte_val(pte), (long long)pmd_val(*pmd)); 691718a3821SWu Fengguang if (page) 692f0b791a3SDave Hansen dump_page(page, "bad pte"); 6931170532bSJoe Perches pr_alert("addr:%p vm_flags:%08lx anon_vma:%p mapping:%p index:%lx\n", 6943dc14741SHugh Dickins (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index); 6953dc14741SHugh Dickins /* 6963dc14741SHugh Dickins * Choose text because data symbols depend on CONFIG_KALLSYMS_ALL=y 6973dc14741SHugh Dickins */ 6982682582aSKonstantin Khlebnikov pr_alert("file:%pD fault:%pf mmap:%pf readpage:%pf\n", 6992682582aSKonstantin Khlebnikov vma->vm_file, 7002682582aSKonstantin Khlebnikov vma->vm_ops ? vma->vm_ops->fault : NULL, 7012682582aSKonstantin Khlebnikov vma->vm_file ? vma->vm_file->f_op->mmap : NULL, 7022682582aSKonstantin Khlebnikov mapping ? mapping->a_ops->readpage : NULL); 703b5810039SNick Piggin dump_stack(); 704373d4d09SRusty Russell add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); 705b5810039SNick Piggin } 706b5810039SNick Piggin 707b5810039SNick Piggin /* 7087e675137SNick Piggin * vm_normal_page -- This function gets the "struct page" associated with a pte. 7096aab341eSLinus Torvalds * 7107e675137SNick Piggin * "Special" mappings do not wish to be associated with a "struct page" (either 7117e675137SNick Piggin * it doesn't exist, or it exists but they don't want to touch it). In this 7127e675137SNick Piggin * case, NULL is returned here. "Normal" mappings do have a struct page. 713b379d790SJared Hulbert * 7147e675137SNick Piggin * There are 2 broad cases. Firstly, an architecture may define a pte_special() 7157e675137SNick Piggin * pte bit, in which case this function is trivial. Secondly, an architecture 7167e675137SNick Piggin * may not have a spare pte bit, which requires a more complicated scheme, 7177e675137SNick Piggin * described below. 7187e675137SNick Piggin * 7197e675137SNick Piggin * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a 7207e675137SNick Piggin * special mapping (even if there are underlying and valid "struct pages"). 7217e675137SNick Piggin * COWed pages of a VM_PFNMAP are always normal. 7226aab341eSLinus Torvalds * 723b379d790SJared Hulbert * The way we recognize COWed pages within VM_PFNMAP mappings is through the 724b379d790SJared Hulbert * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit 7257e675137SNick Piggin * set, and the vm_pgoff will point to the first PFN mapped: thus every special 7267e675137SNick Piggin * mapping will always honor the rule 7276aab341eSLinus Torvalds * 7286aab341eSLinus Torvalds * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT) 7296aab341eSLinus Torvalds * 7307e675137SNick Piggin * And for normal mappings this is false. 731b379d790SJared Hulbert * 7327e675137SNick Piggin * This restricts such mappings to be a linear translation from virtual address 7337e675137SNick Piggin * to pfn. To get around this restriction, we allow arbitrary mappings so long 7347e675137SNick Piggin * as the vma is not a COW mapping; in that case, we know that all ptes are 7357e675137SNick Piggin * special (because none can have been COWed). 736b379d790SJared Hulbert * 737b379d790SJared Hulbert * 7387e675137SNick Piggin * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP. 7397e675137SNick Piggin * 740b379d790SJared Hulbert * VM_MIXEDMAP mappings can likewise contain memory with or without "struct 741b379d790SJared Hulbert * page" backing, however the difference is that _all_ pages with a struct 742b379d790SJared Hulbert * page (that is, those where pfn_valid is true) are refcounted and considered 743b379d790SJared Hulbert * normal pages by the VM. The disadvantage is that pages are refcounted 744b379d790SJared Hulbert * (which can be slower and simply not an option for some PFNMAP users). The 745b379d790SJared Hulbert * advantage is that we don't have to follow the strict linearity rule of 746b379d790SJared Hulbert * PFNMAP mappings in order to support COWable mappings. 747b379d790SJared Hulbert * 748ee498ed7SHugh Dickins */ 7497e675137SNick Piggin #ifdef __HAVE_ARCH_PTE_SPECIAL 7507e675137SNick Piggin # define HAVE_PTE_SPECIAL 1 7517e675137SNick Piggin #else 7527e675137SNick Piggin # define HAVE_PTE_SPECIAL 0 7537e675137SNick Piggin #endif 7547e675137SNick Piggin struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, 7557e675137SNick Piggin pte_t pte) 756ee498ed7SHugh Dickins { 75722b31eecSHugh Dickins unsigned long pfn = pte_pfn(pte); 7587e675137SNick Piggin 7597e675137SNick Piggin if (HAVE_PTE_SPECIAL) { 760b38af472SHugh Dickins if (likely(!pte_special(pte))) 76122b31eecSHugh Dickins goto check_pfn; 762667a0a06SDavid Vrabel if (vma->vm_ops && vma->vm_ops->find_special_page) 763667a0a06SDavid Vrabel return vma->vm_ops->find_special_page(vma, addr); 764a13ea5b7SHugh Dickins if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)) 765a13ea5b7SHugh Dickins return NULL; 76662eede62SHugh Dickins if (!is_zero_pfn(pfn)) 76722b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 7687e675137SNick Piggin return NULL; 7697e675137SNick Piggin } 7707e675137SNick Piggin 7717e675137SNick Piggin /* !HAVE_PTE_SPECIAL case follows: */ 7727e675137SNick Piggin 773b379d790SJared Hulbert if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { 774b379d790SJared Hulbert if (vma->vm_flags & VM_MIXEDMAP) { 775b379d790SJared Hulbert if (!pfn_valid(pfn)) 776b379d790SJared Hulbert return NULL; 777b379d790SJared Hulbert goto out; 778b379d790SJared Hulbert } else { 7797e675137SNick Piggin unsigned long off; 7807e675137SNick Piggin off = (addr - vma->vm_start) >> PAGE_SHIFT; 7816aab341eSLinus Torvalds if (pfn == vma->vm_pgoff + off) 7826aab341eSLinus Torvalds return NULL; 78367121172SLinus Torvalds if (!is_cow_mapping(vma->vm_flags)) 784fb155c16SLinus Torvalds return NULL; 7856aab341eSLinus Torvalds } 786b379d790SJared Hulbert } 7876aab341eSLinus Torvalds 788b38af472SHugh Dickins if (is_zero_pfn(pfn)) 789b38af472SHugh Dickins return NULL; 79022b31eecSHugh Dickins check_pfn: 79122b31eecSHugh Dickins if (unlikely(pfn > highest_memmap_pfn)) { 79222b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 79322b31eecSHugh Dickins return NULL; 79422b31eecSHugh Dickins } 7956aab341eSLinus Torvalds 7966aab341eSLinus Torvalds /* 7977e675137SNick Piggin * NOTE! We still have PageReserved() pages in the page tables. 7987e675137SNick Piggin * eg. VDSO mappings can cause them to exist. 7996aab341eSLinus Torvalds */ 800b379d790SJared Hulbert out: 8016aab341eSLinus Torvalds return pfn_to_page(pfn); 802ee498ed7SHugh Dickins } 803ee498ed7SHugh Dickins 80428093f9fSGerald Schaefer #ifdef CONFIG_TRANSPARENT_HUGEPAGE 80528093f9fSGerald Schaefer struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr, 80628093f9fSGerald Schaefer pmd_t pmd) 80728093f9fSGerald Schaefer { 80828093f9fSGerald Schaefer unsigned long pfn = pmd_pfn(pmd); 80928093f9fSGerald Schaefer 81028093f9fSGerald Schaefer /* 81128093f9fSGerald Schaefer * There is no pmd_special() but there may be special pmds, e.g. 81228093f9fSGerald Schaefer * in a direct-access (dax) mapping, so let's just replicate the 81328093f9fSGerald Schaefer * !HAVE_PTE_SPECIAL case from vm_normal_page() here. 81428093f9fSGerald Schaefer */ 81528093f9fSGerald Schaefer if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { 81628093f9fSGerald Schaefer if (vma->vm_flags & VM_MIXEDMAP) { 81728093f9fSGerald Schaefer if (!pfn_valid(pfn)) 81828093f9fSGerald Schaefer return NULL; 81928093f9fSGerald Schaefer goto out; 82028093f9fSGerald Schaefer } else { 82128093f9fSGerald Schaefer unsigned long off; 82228093f9fSGerald Schaefer off = (addr - vma->vm_start) >> PAGE_SHIFT; 82328093f9fSGerald Schaefer if (pfn == vma->vm_pgoff + off) 82428093f9fSGerald Schaefer return NULL; 82528093f9fSGerald Schaefer if (!is_cow_mapping(vma->vm_flags)) 82628093f9fSGerald Schaefer return NULL; 82728093f9fSGerald Schaefer } 82828093f9fSGerald Schaefer } 82928093f9fSGerald Schaefer 83028093f9fSGerald Schaefer if (is_zero_pfn(pfn)) 83128093f9fSGerald Schaefer return NULL; 83228093f9fSGerald Schaefer if (unlikely(pfn > highest_memmap_pfn)) 83328093f9fSGerald Schaefer return NULL; 83428093f9fSGerald Schaefer 83528093f9fSGerald Schaefer /* 83628093f9fSGerald Schaefer * NOTE! We still have PageReserved() pages in the page tables. 83728093f9fSGerald Schaefer * eg. VDSO mappings can cause them to exist. 83828093f9fSGerald Schaefer */ 83928093f9fSGerald Schaefer out: 84028093f9fSGerald Schaefer return pfn_to_page(pfn); 84128093f9fSGerald Schaefer } 84228093f9fSGerald Schaefer #endif 84328093f9fSGerald Schaefer 844ee498ed7SHugh Dickins /* 8451da177e4SLinus Torvalds * copy one vm_area from one task to the other. Assumes the page tables 8461da177e4SLinus Torvalds * already present in the new task to be cleared in the whole range 8471da177e4SLinus Torvalds * covered by this vma. 8481da177e4SLinus Torvalds */ 8491da177e4SLinus Torvalds 850570a335bSHugh Dickins static inline unsigned long 8511da177e4SLinus Torvalds copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, 852b5810039SNick Piggin pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma, 8538c103762SHugh Dickins unsigned long addr, int *rss) 8541da177e4SLinus Torvalds { 855b5810039SNick Piggin unsigned long vm_flags = vma->vm_flags; 8561da177e4SLinus Torvalds pte_t pte = *src_pte; 8571da177e4SLinus Torvalds struct page *page; 8581da177e4SLinus Torvalds 8591da177e4SLinus Torvalds /* pte contains position in swap or file, so copy. */ 8601da177e4SLinus Torvalds if (unlikely(!pte_present(pte))) { 8610697212aSChristoph Lameter swp_entry_t entry = pte_to_swp_entry(pte); 8620697212aSChristoph Lameter 8632022b4d1SHugh Dickins if (likely(!non_swap_entry(entry))) { 864570a335bSHugh Dickins if (swap_duplicate(entry) < 0) 865570a335bSHugh Dickins return entry.val; 866570a335bSHugh Dickins 8671da177e4SLinus Torvalds /* make sure dst_mm is on swapoff's mmlist. */ 8681da177e4SLinus Torvalds if (unlikely(list_empty(&dst_mm->mmlist))) { 8691da177e4SLinus Torvalds spin_lock(&mmlist_lock); 870f412ac08SHugh Dickins if (list_empty(&dst_mm->mmlist)) 871f412ac08SHugh Dickins list_add(&dst_mm->mmlist, 872f412ac08SHugh Dickins &src_mm->mmlist); 8731da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 8741da177e4SLinus Torvalds } 875b084d435SKAMEZAWA Hiroyuki rss[MM_SWAPENTS]++; 8762022b4d1SHugh Dickins } else if (is_migration_entry(entry)) { 8779f9f1acdSKonstantin Khlebnikov page = migration_entry_to_page(entry); 8789f9f1acdSKonstantin Khlebnikov 879eca56ff9SJerome Marchand rss[mm_counter(page)]++; 8809f9f1acdSKonstantin Khlebnikov 8819f9f1acdSKonstantin Khlebnikov if (is_write_migration_entry(entry) && 8820697212aSChristoph Lameter is_cow_mapping(vm_flags)) { 8830697212aSChristoph Lameter /* 8849f9f1acdSKonstantin Khlebnikov * COW mappings require pages in both 8859f9f1acdSKonstantin Khlebnikov * parent and child to be set to read. 8860697212aSChristoph Lameter */ 8870697212aSChristoph Lameter make_migration_entry_read(&entry); 8880697212aSChristoph Lameter pte = swp_entry_to_pte(entry); 889c3d16e16SCyrill Gorcunov if (pte_swp_soft_dirty(*src_pte)) 890c3d16e16SCyrill Gorcunov pte = pte_swp_mksoft_dirty(pte); 8910697212aSChristoph Lameter set_pte_at(src_mm, addr, src_pte, pte); 8920697212aSChristoph Lameter } 8931da177e4SLinus Torvalds } 894ae859762SHugh Dickins goto out_set_pte; 8951da177e4SLinus Torvalds } 8961da177e4SLinus Torvalds 8971da177e4SLinus Torvalds /* 8981da177e4SLinus Torvalds * If it's a COW mapping, write protect it both 8991da177e4SLinus Torvalds * in the parent and the child 9001da177e4SLinus Torvalds */ 90167121172SLinus Torvalds if (is_cow_mapping(vm_flags)) { 9021da177e4SLinus Torvalds ptep_set_wrprotect(src_mm, addr, src_pte); 9033dc90795SZachary Amsden pte = pte_wrprotect(pte); 9041da177e4SLinus Torvalds } 9051da177e4SLinus Torvalds 9061da177e4SLinus Torvalds /* 9071da177e4SLinus Torvalds * If it's a shared mapping, mark it clean in 9081da177e4SLinus Torvalds * the child 9091da177e4SLinus Torvalds */ 9101da177e4SLinus Torvalds if (vm_flags & VM_SHARED) 9111da177e4SLinus Torvalds pte = pte_mkclean(pte); 9121da177e4SLinus Torvalds pte = pte_mkold(pte); 9136aab341eSLinus Torvalds 9146aab341eSLinus Torvalds page = vm_normal_page(vma, addr, pte); 9156aab341eSLinus Torvalds if (page) { 9161da177e4SLinus Torvalds get_page(page); 91753f9263bSKirill A. Shutemov page_dup_rmap(page, false); 918eca56ff9SJerome Marchand rss[mm_counter(page)]++; 9196aab341eSLinus Torvalds } 920ae859762SHugh Dickins 921ae859762SHugh Dickins out_set_pte: 922ae859762SHugh Dickins set_pte_at(dst_mm, addr, dst_pte, pte); 923570a335bSHugh Dickins return 0; 9241da177e4SLinus Torvalds } 9251da177e4SLinus Torvalds 92621bda264SJerome Marchand static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 9271da177e4SLinus Torvalds pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma, 9281da177e4SLinus Torvalds unsigned long addr, unsigned long end) 9291da177e4SLinus Torvalds { 930c36987e2SDaisuke Nishimura pte_t *orig_src_pte, *orig_dst_pte; 9311da177e4SLinus Torvalds pte_t *src_pte, *dst_pte; 932c74df32cSHugh Dickins spinlock_t *src_ptl, *dst_ptl; 933e040f218SHugh Dickins int progress = 0; 934d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 935570a335bSHugh Dickins swp_entry_t entry = (swp_entry_t){0}; 9361da177e4SLinus Torvalds 9371da177e4SLinus Torvalds again: 938d559db08SKAMEZAWA Hiroyuki init_rss_vec(rss); 939d559db08SKAMEZAWA Hiroyuki 940c74df32cSHugh Dickins dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl); 9411da177e4SLinus Torvalds if (!dst_pte) 9421da177e4SLinus Torvalds return -ENOMEM; 943ece0e2b6SPeter Zijlstra src_pte = pte_offset_map(src_pmd, addr); 9444c21e2f2SHugh Dickins src_ptl = pte_lockptr(src_mm, src_pmd); 945f20dc5f7SIngo Molnar spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 946c36987e2SDaisuke Nishimura orig_src_pte = src_pte; 947c36987e2SDaisuke Nishimura orig_dst_pte = dst_pte; 9486606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 9491da177e4SLinus Torvalds 9501da177e4SLinus Torvalds do { 9511da177e4SLinus Torvalds /* 9521da177e4SLinus Torvalds * We are holding two locks at this point - either of them 9531da177e4SLinus Torvalds * could generate latencies in another task on another CPU. 9541da177e4SLinus Torvalds */ 955e040f218SHugh Dickins if (progress >= 32) { 956e040f218SHugh Dickins progress = 0; 957e040f218SHugh Dickins if (need_resched() || 95895c354feSNick Piggin spin_needbreak(src_ptl) || spin_needbreak(dst_ptl)) 9591da177e4SLinus Torvalds break; 960e040f218SHugh Dickins } 9611da177e4SLinus Torvalds if (pte_none(*src_pte)) { 9621da177e4SLinus Torvalds progress++; 9631da177e4SLinus Torvalds continue; 9641da177e4SLinus Torvalds } 965570a335bSHugh Dickins entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte, 966570a335bSHugh Dickins vma, addr, rss); 967570a335bSHugh Dickins if (entry.val) 968570a335bSHugh Dickins break; 9691da177e4SLinus Torvalds progress += 8; 9701da177e4SLinus Torvalds } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end); 9711da177e4SLinus Torvalds 9726606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 973c74df32cSHugh Dickins spin_unlock(src_ptl); 974ece0e2b6SPeter Zijlstra pte_unmap(orig_src_pte); 975d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(dst_mm, rss); 976c36987e2SDaisuke Nishimura pte_unmap_unlock(orig_dst_pte, dst_ptl); 977c74df32cSHugh Dickins cond_resched(); 978570a335bSHugh Dickins 979570a335bSHugh Dickins if (entry.val) { 980570a335bSHugh Dickins if (add_swap_count_continuation(entry, GFP_KERNEL) < 0) 981570a335bSHugh Dickins return -ENOMEM; 982570a335bSHugh Dickins progress = 0; 983570a335bSHugh Dickins } 9841da177e4SLinus Torvalds if (addr != end) 9851da177e4SLinus Torvalds goto again; 9861da177e4SLinus Torvalds return 0; 9871da177e4SLinus Torvalds } 9881da177e4SLinus Torvalds 9891da177e4SLinus Torvalds static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 9901da177e4SLinus Torvalds pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma, 9911da177e4SLinus Torvalds unsigned long addr, unsigned long end) 9921da177e4SLinus Torvalds { 9931da177e4SLinus Torvalds pmd_t *src_pmd, *dst_pmd; 9941da177e4SLinus Torvalds unsigned long next; 9951da177e4SLinus Torvalds 9961da177e4SLinus Torvalds dst_pmd = pmd_alloc(dst_mm, dst_pud, addr); 9971da177e4SLinus Torvalds if (!dst_pmd) 9981da177e4SLinus Torvalds return -ENOMEM; 9991da177e4SLinus Torvalds src_pmd = pmd_offset(src_pud, addr); 10001da177e4SLinus Torvalds do { 10011da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 10025c7fb56eSDan Williams if (pmd_trans_huge(*src_pmd) || pmd_devmap(*src_pmd)) { 100371e3aac0SAndrea Arcangeli int err; 100414d1a55cSAndrea Arcangeli VM_BUG_ON(next-addr != HPAGE_PMD_SIZE); 100571e3aac0SAndrea Arcangeli err = copy_huge_pmd(dst_mm, src_mm, 100671e3aac0SAndrea Arcangeli dst_pmd, src_pmd, addr, vma); 100771e3aac0SAndrea Arcangeli if (err == -ENOMEM) 100871e3aac0SAndrea Arcangeli return -ENOMEM; 100971e3aac0SAndrea Arcangeli if (!err) 101071e3aac0SAndrea Arcangeli continue; 101171e3aac0SAndrea Arcangeli /* fall through */ 101271e3aac0SAndrea Arcangeli } 10131da177e4SLinus Torvalds if (pmd_none_or_clear_bad(src_pmd)) 10141da177e4SLinus Torvalds continue; 10151da177e4SLinus Torvalds if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd, 10161da177e4SLinus Torvalds vma, addr, next)) 10171da177e4SLinus Torvalds return -ENOMEM; 10181da177e4SLinus Torvalds } while (dst_pmd++, src_pmd++, addr = next, addr != end); 10191da177e4SLinus Torvalds return 0; 10201da177e4SLinus Torvalds } 10211da177e4SLinus Torvalds 10221da177e4SLinus Torvalds static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 10231da177e4SLinus Torvalds pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma, 10241da177e4SLinus Torvalds unsigned long addr, unsigned long end) 10251da177e4SLinus Torvalds { 10261da177e4SLinus Torvalds pud_t *src_pud, *dst_pud; 10271da177e4SLinus Torvalds unsigned long next; 10281da177e4SLinus Torvalds 10291da177e4SLinus Torvalds dst_pud = pud_alloc(dst_mm, dst_pgd, addr); 10301da177e4SLinus Torvalds if (!dst_pud) 10311da177e4SLinus Torvalds return -ENOMEM; 10321da177e4SLinus Torvalds src_pud = pud_offset(src_pgd, addr); 10331da177e4SLinus Torvalds do { 10341da177e4SLinus Torvalds next = pud_addr_end(addr, end); 10351da177e4SLinus Torvalds if (pud_none_or_clear_bad(src_pud)) 10361da177e4SLinus Torvalds continue; 10371da177e4SLinus Torvalds if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud, 10381da177e4SLinus Torvalds vma, addr, next)) 10391da177e4SLinus Torvalds return -ENOMEM; 10401da177e4SLinus Torvalds } while (dst_pud++, src_pud++, addr = next, addr != end); 10411da177e4SLinus Torvalds return 0; 10421da177e4SLinus Torvalds } 10431da177e4SLinus Torvalds 10441da177e4SLinus Torvalds int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 10451da177e4SLinus Torvalds struct vm_area_struct *vma) 10461da177e4SLinus Torvalds { 10471da177e4SLinus Torvalds pgd_t *src_pgd, *dst_pgd; 10481da177e4SLinus Torvalds unsigned long next; 10491da177e4SLinus Torvalds unsigned long addr = vma->vm_start; 10501da177e4SLinus Torvalds unsigned long end = vma->vm_end; 10512ec74c3eSSagi Grimberg unsigned long mmun_start; /* For mmu_notifiers */ 10522ec74c3eSSagi Grimberg unsigned long mmun_end; /* For mmu_notifiers */ 10532ec74c3eSSagi Grimberg bool is_cow; 1054cddb8a5cSAndrea Arcangeli int ret; 10551da177e4SLinus Torvalds 1056d992895bSNick Piggin /* 1057d992895bSNick Piggin * Don't copy ptes where a page fault will fill them correctly. 1058d992895bSNick Piggin * Fork becomes much lighter when there are big shared or private 1059d992895bSNick Piggin * readonly mappings. The tradeoff is that copy_page_range is more 1060d992895bSNick Piggin * efficient than faulting. 1061d992895bSNick Piggin */ 10620661a336SKirill A. Shutemov if (!(vma->vm_flags & (VM_HUGETLB | VM_PFNMAP | VM_MIXEDMAP)) && 10630661a336SKirill A. Shutemov !vma->anon_vma) 1064d992895bSNick Piggin return 0; 1065d992895bSNick Piggin 10661da177e4SLinus Torvalds if (is_vm_hugetlb_page(vma)) 10671da177e4SLinus Torvalds return copy_hugetlb_page_range(dst_mm, src_mm, vma); 10681da177e4SLinus Torvalds 1069b3b9c293SKonstantin Khlebnikov if (unlikely(vma->vm_flags & VM_PFNMAP)) { 10702ab64037Svenkatesh.pallipadi@intel.com /* 10712ab64037Svenkatesh.pallipadi@intel.com * We do not free on error cases below as remove_vma 10722ab64037Svenkatesh.pallipadi@intel.com * gets called on error from higher level routine 10732ab64037Svenkatesh.pallipadi@intel.com */ 10745180da41SSuresh Siddha ret = track_pfn_copy(vma); 10752ab64037Svenkatesh.pallipadi@intel.com if (ret) 10762ab64037Svenkatesh.pallipadi@intel.com return ret; 10772ab64037Svenkatesh.pallipadi@intel.com } 10782ab64037Svenkatesh.pallipadi@intel.com 1079cddb8a5cSAndrea Arcangeli /* 1080cddb8a5cSAndrea Arcangeli * We need to invalidate the secondary MMU mappings only when 1081cddb8a5cSAndrea Arcangeli * there could be a permission downgrade on the ptes of the 1082cddb8a5cSAndrea Arcangeli * parent mm. And a permission downgrade will only happen if 1083cddb8a5cSAndrea Arcangeli * is_cow_mapping() returns true. 1084cddb8a5cSAndrea Arcangeli */ 10852ec74c3eSSagi Grimberg is_cow = is_cow_mapping(vma->vm_flags); 10862ec74c3eSSagi Grimberg mmun_start = addr; 10872ec74c3eSSagi Grimberg mmun_end = end; 10882ec74c3eSSagi Grimberg if (is_cow) 10892ec74c3eSSagi Grimberg mmu_notifier_invalidate_range_start(src_mm, mmun_start, 10902ec74c3eSSagi Grimberg mmun_end); 1091cddb8a5cSAndrea Arcangeli 1092cddb8a5cSAndrea Arcangeli ret = 0; 10931da177e4SLinus Torvalds dst_pgd = pgd_offset(dst_mm, addr); 10941da177e4SLinus Torvalds src_pgd = pgd_offset(src_mm, addr); 10951da177e4SLinus Torvalds do { 10961da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 10971da177e4SLinus Torvalds if (pgd_none_or_clear_bad(src_pgd)) 10981da177e4SLinus Torvalds continue; 1099cddb8a5cSAndrea Arcangeli if (unlikely(copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd, 1100cddb8a5cSAndrea Arcangeli vma, addr, next))) { 1101cddb8a5cSAndrea Arcangeli ret = -ENOMEM; 1102cddb8a5cSAndrea Arcangeli break; 1103cddb8a5cSAndrea Arcangeli } 11041da177e4SLinus Torvalds } while (dst_pgd++, src_pgd++, addr = next, addr != end); 1105cddb8a5cSAndrea Arcangeli 11062ec74c3eSSagi Grimberg if (is_cow) 11072ec74c3eSSagi Grimberg mmu_notifier_invalidate_range_end(src_mm, mmun_start, mmun_end); 1108cddb8a5cSAndrea Arcangeli return ret; 11091da177e4SLinus Torvalds } 11101da177e4SLinus Torvalds 111151c6f666SRobin Holt static unsigned long zap_pte_range(struct mmu_gather *tlb, 1112b5810039SNick Piggin struct vm_area_struct *vma, pmd_t *pmd, 11131da177e4SLinus Torvalds unsigned long addr, unsigned long end, 111497a89413SPeter Zijlstra struct zap_details *details) 11151da177e4SLinus Torvalds { 1116b5810039SNick Piggin struct mm_struct *mm = tlb->mm; 1117d16dfc55SPeter Zijlstra int force_flush = 0; 1118d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 111997a89413SPeter Zijlstra spinlock_t *ptl; 11205f1a1907SSteven Rostedt pte_t *start_pte; 112197a89413SPeter Zijlstra pte_t *pte; 11228a5f14a2SKirill A. Shutemov swp_entry_t entry; 1123d559db08SKAMEZAWA Hiroyuki 112407e32661SAneesh Kumar K.V tlb_remove_check_page_size_change(tlb, PAGE_SIZE); 1125d16dfc55SPeter Zijlstra again: 1126e303297eSPeter Zijlstra init_rss_vec(rss); 11275f1a1907SSteven Rostedt start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl); 11285f1a1907SSteven Rostedt pte = start_pte; 11296606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 11301da177e4SLinus Torvalds do { 11311da177e4SLinus Torvalds pte_t ptent = *pte; 113251c6f666SRobin Holt if (pte_none(ptent)) { 11331da177e4SLinus Torvalds continue; 113451c6f666SRobin Holt } 113551c6f666SRobin Holt 11366f5e6b9eSHugh Dickins if (pte_present(ptent)) { 11376f5e6b9eSHugh Dickins struct page *page; 11386f5e6b9eSHugh Dickins 11396aab341eSLinus Torvalds page = vm_normal_page(vma, addr, ptent); 11401da177e4SLinus Torvalds if (unlikely(details) && page) { 11411da177e4SLinus Torvalds /* 11421da177e4SLinus Torvalds * unmap_shared_mapping_pages() wants to 11431da177e4SLinus Torvalds * invalidate cache without truncating: 11441da177e4SLinus Torvalds * unmap shared but keep private pages. 11451da177e4SLinus Torvalds */ 11461da177e4SLinus Torvalds if (details->check_mapping && 1147800d8c63SKirill A. Shutemov details->check_mapping != page_rmapping(page)) 11481da177e4SLinus Torvalds continue; 11491da177e4SLinus Torvalds } 1150b5810039SNick Piggin ptent = ptep_get_and_clear_full(mm, addr, pte, 1151a600388dSZachary Amsden tlb->fullmm); 11521da177e4SLinus Torvalds tlb_remove_tlb_entry(tlb, pte, addr); 11531da177e4SLinus Torvalds if (unlikely(!page)) 11541da177e4SLinus Torvalds continue; 1155eca56ff9SJerome Marchand 1156eca56ff9SJerome Marchand if (!PageAnon(page)) { 11571cf35d47SLinus Torvalds if (pte_dirty(ptent)) { 1158aac45363SMichal Hocko /* 1159aac45363SMichal Hocko * oom_reaper cannot tear down dirty 1160aac45363SMichal Hocko * pages 1161aac45363SMichal Hocko */ 1162aac45363SMichal Hocko if (unlikely(details && details->ignore_dirty)) 1163aac45363SMichal Hocko continue; 11641cf35d47SLinus Torvalds force_flush = 1; 11656237bcd9SHugh Dickins set_page_dirty(page); 11661cf35d47SLinus Torvalds } 11674917e5d0SJohannes Weiner if (pte_young(ptent) && 116864363aadSJoe Perches likely(!(vma->vm_flags & VM_SEQ_READ))) 1169bf3f3bc5SNick Piggin mark_page_accessed(page); 11706237bcd9SHugh Dickins } 1171eca56ff9SJerome Marchand rss[mm_counter(page)]--; 1172d281ee61SKirill A. Shutemov page_remove_rmap(page, false); 11733dc14741SHugh Dickins if (unlikely(page_mapcount(page) < 0)) 11743dc14741SHugh Dickins print_bad_pte(vma, addr, ptent, page); 1175e9d55e15SAneesh Kumar K.V if (unlikely(__tlb_remove_page(tlb, page))) { 11761cf35d47SLinus Torvalds force_flush = 1; 1177ce9ec37bSWill Deacon addr += PAGE_SIZE; 1178d16dfc55SPeter Zijlstra break; 11791cf35d47SLinus Torvalds } 11801da177e4SLinus Torvalds continue; 11811da177e4SLinus Torvalds } 1182aac45363SMichal Hocko /* only check swap_entries if explicitly asked for in details */ 1183aac45363SMichal Hocko if (unlikely(details && !details->check_swap_entries)) 11841da177e4SLinus Torvalds continue; 1185b084d435SKAMEZAWA Hiroyuki 11868a5f14a2SKirill A. Shutemov entry = pte_to_swp_entry(ptent); 1187b084d435SKAMEZAWA Hiroyuki if (!non_swap_entry(entry)) 1188b084d435SKAMEZAWA Hiroyuki rss[MM_SWAPENTS]--; 11899f9f1acdSKonstantin Khlebnikov else if (is_migration_entry(entry)) { 11909f9f1acdSKonstantin Khlebnikov struct page *page; 11919f9f1acdSKonstantin Khlebnikov 11929f9f1acdSKonstantin Khlebnikov page = migration_entry_to_page(entry); 1193eca56ff9SJerome Marchand rss[mm_counter(page)]--; 11949f9f1acdSKonstantin Khlebnikov } 1195b084d435SKAMEZAWA Hiroyuki if (unlikely(!free_swap_and_cache(entry))) 11962509ef26SHugh Dickins print_bad_pte(vma, addr, ptent, NULL); 11979888a1caSZachary Amsden pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); 119897a89413SPeter Zijlstra } while (pte++, addr += PAGE_SIZE, addr != end); 1199ae859762SHugh Dickins 1200d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(mm, rss); 12016606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 120251c6f666SRobin Holt 12031cf35d47SLinus Torvalds /* Do the actual TLB flush before dropping ptl */ 1204fb7332a9SWill Deacon if (force_flush) 12051cf35d47SLinus Torvalds tlb_flush_mmu_tlbonly(tlb); 12061cf35d47SLinus Torvalds pte_unmap_unlock(start_pte, ptl); 12071cf35d47SLinus Torvalds 12081cf35d47SLinus Torvalds /* 12091cf35d47SLinus Torvalds * If we forced a TLB flush (either due to running out of 12101cf35d47SLinus Torvalds * batch buffers or because we needed to flush dirty TLB 12111cf35d47SLinus Torvalds * entries before releasing the ptl), free the batched 12121cf35d47SLinus Torvalds * memory too. Restart if we didn't do everything. 12131cf35d47SLinus Torvalds */ 12141cf35d47SLinus Torvalds if (force_flush) { 12151cf35d47SLinus Torvalds force_flush = 0; 12161cf35d47SLinus Torvalds tlb_flush_mmu_free(tlb); 12172b047252SLinus Torvalds if (addr != end) 1218d16dfc55SPeter Zijlstra goto again; 1219d16dfc55SPeter Zijlstra } 1220d16dfc55SPeter Zijlstra 122151c6f666SRobin Holt return addr; 12221da177e4SLinus Torvalds } 12231da177e4SLinus Torvalds 122451c6f666SRobin Holt static inline unsigned long zap_pmd_range(struct mmu_gather *tlb, 1225b5810039SNick Piggin struct vm_area_struct *vma, pud_t *pud, 12261da177e4SLinus Torvalds unsigned long addr, unsigned long end, 122797a89413SPeter Zijlstra struct zap_details *details) 12281da177e4SLinus Torvalds { 12291da177e4SLinus Torvalds pmd_t *pmd; 12301da177e4SLinus Torvalds unsigned long next; 12311da177e4SLinus Torvalds 12321da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 12331da177e4SLinus Torvalds do { 12341da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 12355c7fb56eSDan Williams if (pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) { 123614d1a55cSAndrea Arcangeli if (next - addr != HPAGE_PMD_SIZE) { 123768428398SHugh Dickins VM_BUG_ON_VMA(vma_is_anonymous(vma) && 123868428398SHugh Dickins !rwsem_is_locked(&tlb->mm->mmap_sem), vma); 1239fd60775aSDavid Rientjes __split_huge_pmd(vma, pmd, addr, false, NULL); 1240f21760b1SShaohua Li } else if (zap_huge_pmd(tlb, vma, pmd, addr)) 12411a5a9906SAndrea Arcangeli goto next; 124271e3aac0SAndrea Arcangeli /* fall through */ 124371e3aac0SAndrea Arcangeli } 12441a5a9906SAndrea Arcangeli /* 12451a5a9906SAndrea Arcangeli * Here there can be other concurrent MADV_DONTNEED or 12461a5a9906SAndrea Arcangeli * trans huge page faults running, and if the pmd is 12471a5a9906SAndrea Arcangeli * none or trans huge it can change under us. This is 12481a5a9906SAndrea Arcangeli * because MADV_DONTNEED holds the mmap_sem in read 12491a5a9906SAndrea Arcangeli * mode. 12501a5a9906SAndrea Arcangeli */ 12511a5a9906SAndrea Arcangeli if (pmd_none_or_trans_huge_or_clear_bad(pmd)) 12521a5a9906SAndrea Arcangeli goto next; 125397a89413SPeter Zijlstra next = zap_pte_range(tlb, vma, pmd, addr, next, details); 12541a5a9906SAndrea Arcangeli next: 125597a89413SPeter Zijlstra cond_resched(); 125697a89413SPeter Zijlstra } while (pmd++, addr = next, addr != end); 125751c6f666SRobin Holt 125851c6f666SRobin Holt return addr; 12591da177e4SLinus Torvalds } 12601da177e4SLinus Torvalds 126151c6f666SRobin Holt static inline unsigned long zap_pud_range(struct mmu_gather *tlb, 1262b5810039SNick Piggin struct vm_area_struct *vma, pgd_t *pgd, 12631da177e4SLinus Torvalds unsigned long addr, unsigned long end, 126497a89413SPeter Zijlstra struct zap_details *details) 12651da177e4SLinus Torvalds { 12661da177e4SLinus Torvalds pud_t *pud; 12671da177e4SLinus Torvalds unsigned long next; 12681da177e4SLinus Torvalds 12691da177e4SLinus Torvalds pud = pud_offset(pgd, addr); 12701da177e4SLinus Torvalds do { 12711da177e4SLinus Torvalds next = pud_addr_end(addr, end); 127297a89413SPeter Zijlstra if (pud_none_or_clear_bad(pud)) 12731da177e4SLinus Torvalds continue; 127497a89413SPeter Zijlstra next = zap_pmd_range(tlb, vma, pud, addr, next, details); 127597a89413SPeter Zijlstra } while (pud++, addr = next, addr != end); 127651c6f666SRobin Holt 127751c6f666SRobin Holt return addr; 12781da177e4SLinus Torvalds } 12791da177e4SLinus Torvalds 1280aac45363SMichal Hocko void unmap_page_range(struct mmu_gather *tlb, 128151c6f666SRobin Holt struct vm_area_struct *vma, 12821da177e4SLinus Torvalds unsigned long addr, unsigned long end, 128397a89413SPeter Zijlstra struct zap_details *details) 12841da177e4SLinus Torvalds { 12851da177e4SLinus Torvalds pgd_t *pgd; 12861da177e4SLinus Torvalds unsigned long next; 12871da177e4SLinus Torvalds 12881da177e4SLinus Torvalds BUG_ON(addr >= end); 12891da177e4SLinus Torvalds tlb_start_vma(tlb, vma); 12901da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 12911da177e4SLinus Torvalds do { 12921da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 129397a89413SPeter Zijlstra if (pgd_none_or_clear_bad(pgd)) 12941da177e4SLinus Torvalds continue; 129597a89413SPeter Zijlstra next = zap_pud_range(tlb, vma, pgd, addr, next, details); 129697a89413SPeter Zijlstra } while (pgd++, addr = next, addr != end); 12971da177e4SLinus Torvalds tlb_end_vma(tlb, vma); 12981da177e4SLinus Torvalds } 12991da177e4SLinus Torvalds 1300f5cc4eefSAl Viro 1301f5cc4eefSAl Viro static void unmap_single_vma(struct mmu_gather *tlb, 13021da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long start_addr, 13034f74d2c8SLinus Torvalds unsigned long end_addr, 13041da177e4SLinus Torvalds struct zap_details *details) 13051da177e4SLinus Torvalds { 1306f5cc4eefSAl Viro unsigned long start = max(vma->vm_start, start_addr); 13071da177e4SLinus Torvalds unsigned long end; 13081da177e4SLinus Torvalds 13091da177e4SLinus Torvalds if (start >= vma->vm_end) 1310f5cc4eefSAl Viro return; 13111da177e4SLinus Torvalds end = min(vma->vm_end, end_addr); 13121da177e4SLinus Torvalds if (end <= vma->vm_start) 1313f5cc4eefSAl Viro return; 13141da177e4SLinus Torvalds 1315cbc91f71SSrikar Dronamraju if (vma->vm_file) 1316cbc91f71SSrikar Dronamraju uprobe_munmap(vma, start, end); 1317cbc91f71SSrikar Dronamraju 1318b3b9c293SKonstantin Khlebnikov if (unlikely(vma->vm_flags & VM_PFNMAP)) 13195180da41SSuresh Siddha untrack_pfn(vma, 0, 0); 13202ab64037Svenkatesh.pallipadi@intel.com 13218b2a1238SAl Viro if (start != end) { 132251c6f666SRobin Holt if (unlikely(is_vm_hugetlb_page(vma))) { 1323a137e1ccSAndi Kleen /* 1324a137e1ccSAndi Kleen * It is undesirable to test vma->vm_file as it 1325a137e1ccSAndi Kleen * should be non-null for valid hugetlb area. 1326a137e1ccSAndi Kleen * However, vm_file will be NULL in the error 13277aa6b4adSDavidlohr Bueso * cleanup path of mmap_region. When 1328a137e1ccSAndi Kleen * hugetlbfs ->mmap method fails, 13297aa6b4adSDavidlohr Bueso * mmap_region() nullifies vma->vm_file 1330a137e1ccSAndi Kleen * before calling this function to clean up. 1331a137e1ccSAndi Kleen * Since no pte has actually been setup, it is 1332a137e1ccSAndi Kleen * safe to do nothing in this case. 1333a137e1ccSAndi Kleen */ 133424669e58SAneesh Kumar K.V if (vma->vm_file) { 133583cde9e8SDavidlohr Bueso i_mmap_lock_write(vma->vm_file->f_mapping); 1336d833352aSMel Gorman __unmap_hugepage_range_final(tlb, vma, start, end, NULL); 133783cde9e8SDavidlohr Bueso i_mmap_unlock_write(vma->vm_file->f_mapping); 133824669e58SAneesh Kumar K.V } 133951c6f666SRobin Holt } else 1340038c7aa1SAl Viro unmap_page_range(tlb, vma, start, end, details); 134197a89413SPeter Zijlstra } 134251c6f666SRobin Holt } 13431da177e4SLinus Torvalds 1344f5cc4eefSAl Viro /** 1345f5cc4eefSAl Viro * unmap_vmas - unmap a range of memory covered by a list of vma's 1346f5cc4eefSAl Viro * @tlb: address of the caller's struct mmu_gather 1347f5cc4eefSAl Viro * @vma: the starting vma 1348f5cc4eefSAl Viro * @start_addr: virtual address at which to start unmapping 1349f5cc4eefSAl Viro * @end_addr: virtual address at which to end unmapping 1350f5cc4eefSAl Viro * 1351f5cc4eefSAl Viro * Unmap all pages in the vma list. 1352f5cc4eefSAl Viro * 1353f5cc4eefSAl Viro * Only addresses between `start' and `end' will be unmapped. 1354f5cc4eefSAl Viro * 1355f5cc4eefSAl Viro * The VMA list must be sorted in ascending virtual address order. 1356f5cc4eefSAl Viro * 1357f5cc4eefSAl Viro * unmap_vmas() assumes that the caller will flush the whole unmapped address 1358f5cc4eefSAl Viro * range after unmap_vmas() returns. So the only responsibility here is to 1359f5cc4eefSAl Viro * ensure that any thus-far unmapped pages are flushed before unmap_vmas() 1360f5cc4eefSAl Viro * drops the lock and schedules. 1361f5cc4eefSAl Viro */ 1362f5cc4eefSAl Viro void unmap_vmas(struct mmu_gather *tlb, 1363f5cc4eefSAl Viro struct vm_area_struct *vma, unsigned long start_addr, 13644f74d2c8SLinus Torvalds unsigned long end_addr) 1365f5cc4eefSAl Viro { 1366f5cc4eefSAl Viro struct mm_struct *mm = vma->vm_mm; 1367f5cc4eefSAl Viro 1368f5cc4eefSAl Viro mmu_notifier_invalidate_range_start(mm, start_addr, end_addr); 1369f5cc4eefSAl Viro for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) 13704f74d2c8SLinus Torvalds unmap_single_vma(tlb, vma, start_addr, end_addr, NULL); 1371cddb8a5cSAndrea Arcangeli mmu_notifier_invalidate_range_end(mm, start_addr, end_addr); 13721da177e4SLinus Torvalds } 13731da177e4SLinus Torvalds 13741da177e4SLinus Torvalds /** 13751da177e4SLinus Torvalds * zap_page_range - remove user pages in a given range 13761da177e4SLinus Torvalds * @vma: vm_area_struct holding the applicable pages 1377eb4546bbSRandy Dunlap * @start: starting address of pages to zap 13781da177e4SLinus Torvalds * @size: number of bytes to zap 13798a5f14a2SKirill A. Shutemov * @details: details of shared cache invalidation 1380f5cc4eefSAl Viro * 1381f5cc4eefSAl Viro * Caller must protect the VMA list 13821da177e4SLinus Torvalds */ 13837e027b14SLinus Torvalds void zap_page_range(struct vm_area_struct *vma, unsigned long start, 13841da177e4SLinus Torvalds unsigned long size, struct zap_details *details) 13851da177e4SLinus Torvalds { 13861da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 1387d16dfc55SPeter Zijlstra struct mmu_gather tlb; 13887e027b14SLinus Torvalds unsigned long end = start + size; 13891da177e4SLinus Torvalds 13901da177e4SLinus Torvalds lru_add_drain(); 13912b047252SLinus Torvalds tlb_gather_mmu(&tlb, mm, start, end); 1392365e9c87SHugh Dickins update_hiwater_rss(mm); 13937e027b14SLinus Torvalds mmu_notifier_invalidate_range_start(mm, start, end); 13947e027b14SLinus Torvalds for ( ; vma && vma->vm_start < end; vma = vma->vm_next) 13954f74d2c8SLinus Torvalds unmap_single_vma(&tlb, vma, start, end, details); 13967e027b14SLinus Torvalds mmu_notifier_invalidate_range_end(mm, start, end); 13977e027b14SLinus Torvalds tlb_finish_mmu(&tlb, start, end); 13981da177e4SLinus Torvalds } 13991da177e4SLinus Torvalds 1400c627f9ccSJack Steiner /** 1401f5cc4eefSAl Viro * zap_page_range_single - remove user pages in a given range 1402f5cc4eefSAl Viro * @vma: vm_area_struct holding the applicable pages 1403f5cc4eefSAl Viro * @address: starting address of pages to zap 1404f5cc4eefSAl Viro * @size: number of bytes to zap 14058a5f14a2SKirill A. Shutemov * @details: details of shared cache invalidation 1406f5cc4eefSAl Viro * 1407f5cc4eefSAl Viro * The range must fit into one VMA. 1408f5cc4eefSAl Viro */ 1409f5cc4eefSAl Viro static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address, 1410f5cc4eefSAl Viro unsigned long size, struct zap_details *details) 1411f5cc4eefSAl Viro { 1412f5cc4eefSAl Viro struct mm_struct *mm = vma->vm_mm; 1413f5cc4eefSAl Viro struct mmu_gather tlb; 1414f5cc4eefSAl Viro unsigned long end = address + size; 1415f5cc4eefSAl Viro 1416f5cc4eefSAl Viro lru_add_drain(); 14172b047252SLinus Torvalds tlb_gather_mmu(&tlb, mm, address, end); 1418f5cc4eefSAl Viro update_hiwater_rss(mm); 1419f5cc4eefSAl Viro mmu_notifier_invalidate_range_start(mm, address, end); 14204f74d2c8SLinus Torvalds unmap_single_vma(&tlb, vma, address, end, details); 1421f5cc4eefSAl Viro mmu_notifier_invalidate_range_end(mm, address, end); 1422f5cc4eefSAl Viro tlb_finish_mmu(&tlb, address, end); 14231da177e4SLinus Torvalds } 14241da177e4SLinus Torvalds 1425c627f9ccSJack Steiner /** 1426c627f9ccSJack Steiner * zap_vma_ptes - remove ptes mapping the vma 1427c627f9ccSJack Steiner * @vma: vm_area_struct holding ptes to be zapped 1428c627f9ccSJack Steiner * @address: starting address of pages to zap 1429c627f9ccSJack Steiner * @size: number of bytes to zap 1430c627f9ccSJack Steiner * 1431c627f9ccSJack Steiner * This function only unmaps ptes assigned to VM_PFNMAP vmas. 1432c627f9ccSJack Steiner * 1433c627f9ccSJack Steiner * The entire address range must be fully contained within the vma. 1434c627f9ccSJack Steiner * 1435c627f9ccSJack Steiner * Returns 0 if successful. 1436c627f9ccSJack Steiner */ 1437c627f9ccSJack Steiner int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, 1438c627f9ccSJack Steiner unsigned long size) 1439c627f9ccSJack Steiner { 1440c627f9ccSJack Steiner if (address < vma->vm_start || address + size > vma->vm_end || 1441c627f9ccSJack Steiner !(vma->vm_flags & VM_PFNMAP)) 1442c627f9ccSJack Steiner return -1; 1443f5cc4eefSAl Viro zap_page_range_single(vma, address, size, NULL); 1444c627f9ccSJack Steiner return 0; 1445c627f9ccSJack Steiner } 1446c627f9ccSJack Steiner EXPORT_SYMBOL_GPL(zap_vma_ptes); 1447c627f9ccSJack Steiner 144825ca1d6cSNamhyung Kim pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr, 1449920c7a5dSHarvey Harrison spinlock_t **ptl) 1450c9cfcddfSLinus Torvalds { 1451c9cfcddfSLinus Torvalds pgd_t * pgd = pgd_offset(mm, addr); 1452c9cfcddfSLinus Torvalds pud_t * pud = pud_alloc(mm, pgd, addr); 1453c9cfcddfSLinus Torvalds if (pud) { 145449c91fb0STrond Myklebust pmd_t * pmd = pmd_alloc(mm, pud, addr); 1455f66055abSAndrea Arcangeli if (pmd) { 1456f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 1457c9cfcddfSLinus Torvalds return pte_alloc_map_lock(mm, pmd, addr, ptl); 1458c9cfcddfSLinus Torvalds } 1459f66055abSAndrea Arcangeli } 1460c9cfcddfSLinus Torvalds return NULL; 1461c9cfcddfSLinus Torvalds } 1462c9cfcddfSLinus Torvalds 14631da177e4SLinus Torvalds /* 1464238f58d8SLinus Torvalds * This is the old fallback for page remapping. 1465238f58d8SLinus Torvalds * 1466238f58d8SLinus Torvalds * For historical reasons, it only allows reserved pages. Only 1467238f58d8SLinus Torvalds * old drivers should use this, and they needed to mark their 1468238f58d8SLinus Torvalds * pages reserved for the old functions anyway. 1469238f58d8SLinus Torvalds */ 1470423bad60SNick Piggin static int insert_page(struct vm_area_struct *vma, unsigned long addr, 1471423bad60SNick Piggin struct page *page, pgprot_t prot) 1472238f58d8SLinus Torvalds { 1473423bad60SNick Piggin struct mm_struct *mm = vma->vm_mm; 1474238f58d8SLinus Torvalds int retval; 1475238f58d8SLinus Torvalds pte_t *pte; 1476238f58d8SLinus Torvalds spinlock_t *ptl; 1477238f58d8SLinus Torvalds 1478238f58d8SLinus Torvalds retval = -EINVAL; 1479a145dd41SLinus Torvalds if (PageAnon(page)) 14805b4e655eSKAMEZAWA Hiroyuki goto out; 1481238f58d8SLinus Torvalds retval = -ENOMEM; 1482238f58d8SLinus Torvalds flush_dcache_page(page); 1483c9cfcddfSLinus Torvalds pte = get_locked_pte(mm, addr, &ptl); 1484238f58d8SLinus Torvalds if (!pte) 14855b4e655eSKAMEZAWA Hiroyuki goto out; 1486238f58d8SLinus Torvalds retval = -EBUSY; 1487238f58d8SLinus Torvalds if (!pte_none(*pte)) 1488238f58d8SLinus Torvalds goto out_unlock; 1489238f58d8SLinus Torvalds 1490238f58d8SLinus Torvalds /* Ok, finally just insert the thing.. */ 1491238f58d8SLinus Torvalds get_page(page); 1492eca56ff9SJerome Marchand inc_mm_counter_fast(mm, mm_counter_file(page)); 1493dd78feddSKirill A. Shutemov page_add_file_rmap(page, false); 1494238f58d8SLinus Torvalds set_pte_at(mm, addr, pte, mk_pte(page, prot)); 1495238f58d8SLinus Torvalds 1496238f58d8SLinus Torvalds retval = 0; 14978a9f3ccdSBalbir Singh pte_unmap_unlock(pte, ptl); 14988a9f3ccdSBalbir Singh return retval; 1499238f58d8SLinus Torvalds out_unlock: 1500238f58d8SLinus Torvalds pte_unmap_unlock(pte, ptl); 1501238f58d8SLinus Torvalds out: 1502238f58d8SLinus Torvalds return retval; 1503238f58d8SLinus Torvalds } 1504238f58d8SLinus Torvalds 1505bfa5bf6dSRolf Eike Beer /** 1506bfa5bf6dSRolf Eike Beer * vm_insert_page - insert single page into user vma 1507bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 1508bfa5bf6dSRolf Eike Beer * @addr: target user address of this page 1509bfa5bf6dSRolf Eike Beer * @page: source kernel page 1510bfa5bf6dSRolf Eike Beer * 1511a145dd41SLinus Torvalds * This allows drivers to insert individual pages they've allocated 1512a145dd41SLinus Torvalds * into a user vma. 1513a145dd41SLinus Torvalds * 1514a145dd41SLinus Torvalds * The page has to be a nice clean _individual_ kernel allocation. 1515a145dd41SLinus Torvalds * If you allocate a compound page, you need to have marked it as 1516a145dd41SLinus Torvalds * such (__GFP_COMP), or manually just split the page up yourself 15178dfcc9baSNick Piggin * (see split_page()). 1518a145dd41SLinus Torvalds * 1519a145dd41SLinus Torvalds * NOTE! Traditionally this was done with "remap_pfn_range()" which 1520a145dd41SLinus Torvalds * took an arbitrary page protection parameter. This doesn't allow 1521a145dd41SLinus Torvalds * that. Your vma protection will have to be set up correctly, which 1522a145dd41SLinus Torvalds * means that if you want a shared writable mapping, you'd better 1523a145dd41SLinus Torvalds * ask for a shared writable mapping! 1524a145dd41SLinus Torvalds * 1525a145dd41SLinus Torvalds * The page does not need to be reserved. 15264b6e1e37SKonstantin Khlebnikov * 15274b6e1e37SKonstantin Khlebnikov * Usually this function is called from f_op->mmap() handler 15284b6e1e37SKonstantin Khlebnikov * under mm->mmap_sem write-lock, so it can change vma->vm_flags. 15294b6e1e37SKonstantin Khlebnikov * Caller must set VM_MIXEDMAP on vma if it wants to call this 15304b6e1e37SKonstantin Khlebnikov * function from other places, for example from page-fault handler. 1531a145dd41SLinus Torvalds */ 1532423bad60SNick Piggin int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, 1533423bad60SNick Piggin struct page *page) 1534a145dd41SLinus Torvalds { 1535a145dd41SLinus Torvalds if (addr < vma->vm_start || addr >= vma->vm_end) 1536a145dd41SLinus Torvalds return -EFAULT; 1537a145dd41SLinus Torvalds if (!page_count(page)) 1538a145dd41SLinus Torvalds return -EINVAL; 15394b6e1e37SKonstantin Khlebnikov if (!(vma->vm_flags & VM_MIXEDMAP)) { 15404b6e1e37SKonstantin Khlebnikov BUG_ON(down_read_trylock(&vma->vm_mm->mmap_sem)); 15414b6e1e37SKonstantin Khlebnikov BUG_ON(vma->vm_flags & VM_PFNMAP); 15424b6e1e37SKonstantin Khlebnikov vma->vm_flags |= VM_MIXEDMAP; 15434b6e1e37SKonstantin Khlebnikov } 1544423bad60SNick Piggin return insert_page(vma, addr, page, vma->vm_page_prot); 1545a145dd41SLinus Torvalds } 1546e3c3374fSLinus Torvalds EXPORT_SYMBOL(vm_insert_page); 1547a145dd41SLinus Torvalds 1548423bad60SNick Piggin static int insert_pfn(struct vm_area_struct *vma, unsigned long addr, 154901c8f1c4SDan Williams pfn_t pfn, pgprot_t prot) 1550423bad60SNick Piggin { 1551423bad60SNick Piggin struct mm_struct *mm = vma->vm_mm; 1552423bad60SNick Piggin int retval; 1553423bad60SNick Piggin pte_t *pte, entry; 1554423bad60SNick Piggin spinlock_t *ptl; 1555423bad60SNick Piggin 1556423bad60SNick Piggin retval = -ENOMEM; 1557423bad60SNick Piggin pte = get_locked_pte(mm, addr, &ptl); 1558423bad60SNick Piggin if (!pte) 1559423bad60SNick Piggin goto out; 1560423bad60SNick Piggin retval = -EBUSY; 1561423bad60SNick Piggin if (!pte_none(*pte)) 1562423bad60SNick Piggin goto out_unlock; 1563423bad60SNick Piggin 1564423bad60SNick Piggin /* Ok, finally just insert the thing.. */ 156501c8f1c4SDan Williams if (pfn_t_devmap(pfn)) 156601c8f1c4SDan Williams entry = pte_mkdevmap(pfn_t_pte(pfn, prot)); 156701c8f1c4SDan Williams else 156801c8f1c4SDan Williams entry = pte_mkspecial(pfn_t_pte(pfn, prot)); 1569423bad60SNick Piggin set_pte_at(mm, addr, pte, entry); 15704b3073e1SRussell King update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */ 1571423bad60SNick Piggin 1572423bad60SNick Piggin retval = 0; 1573423bad60SNick Piggin out_unlock: 1574423bad60SNick Piggin pte_unmap_unlock(pte, ptl); 1575423bad60SNick Piggin out: 1576423bad60SNick Piggin return retval; 1577423bad60SNick Piggin } 1578423bad60SNick Piggin 1579e0dc0d8fSNick Piggin /** 1580e0dc0d8fSNick Piggin * vm_insert_pfn - insert single pfn into user vma 1581e0dc0d8fSNick Piggin * @vma: user vma to map to 1582e0dc0d8fSNick Piggin * @addr: target user address of this page 1583e0dc0d8fSNick Piggin * @pfn: source kernel pfn 1584e0dc0d8fSNick Piggin * 1585c462f179SRobert P. J. Day * Similar to vm_insert_page, this allows drivers to insert individual pages 1586e0dc0d8fSNick Piggin * they've allocated into a user vma. Same comments apply. 1587e0dc0d8fSNick Piggin * 1588e0dc0d8fSNick Piggin * This function should only be called from a vm_ops->fault handler, and 1589e0dc0d8fSNick Piggin * in that case the handler should return NULL. 15900d71d10aSNick Piggin * 15910d71d10aSNick Piggin * vma cannot be a COW mapping. 15920d71d10aSNick Piggin * 15930d71d10aSNick Piggin * As this is called only for pages that do not currently exist, we 15940d71d10aSNick Piggin * do not need to flush old virtual caches or the TLB. 1595e0dc0d8fSNick Piggin */ 1596e0dc0d8fSNick Piggin int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr, 1597e0dc0d8fSNick Piggin unsigned long pfn) 1598e0dc0d8fSNick Piggin { 15991745cbc5SAndy Lutomirski return vm_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot); 16001745cbc5SAndy Lutomirski } 16011745cbc5SAndy Lutomirski EXPORT_SYMBOL(vm_insert_pfn); 16021745cbc5SAndy Lutomirski 16031745cbc5SAndy Lutomirski /** 16041745cbc5SAndy Lutomirski * vm_insert_pfn_prot - insert single pfn into user vma with specified pgprot 16051745cbc5SAndy Lutomirski * @vma: user vma to map to 16061745cbc5SAndy Lutomirski * @addr: target user address of this page 16071745cbc5SAndy Lutomirski * @pfn: source kernel pfn 16081745cbc5SAndy Lutomirski * @pgprot: pgprot flags for the inserted page 16091745cbc5SAndy Lutomirski * 16101745cbc5SAndy Lutomirski * This is exactly like vm_insert_pfn, except that it allows drivers to 16111745cbc5SAndy Lutomirski * to override pgprot on a per-page basis. 16121745cbc5SAndy Lutomirski * 16131745cbc5SAndy Lutomirski * This only makes sense for IO mappings, and it makes no sense for 16141745cbc5SAndy Lutomirski * cow mappings. In general, using multiple vmas is preferable; 16151745cbc5SAndy Lutomirski * vm_insert_pfn_prot should only be used if using multiple VMAs is 16161745cbc5SAndy Lutomirski * impractical. 16171745cbc5SAndy Lutomirski */ 16181745cbc5SAndy Lutomirski int vm_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr, 16191745cbc5SAndy Lutomirski unsigned long pfn, pgprot_t pgprot) 16201745cbc5SAndy Lutomirski { 16212ab64037Svenkatesh.pallipadi@intel.com int ret; 16227e675137SNick Piggin /* 16237e675137SNick Piggin * Technically, architectures with pte_special can avoid all these 16247e675137SNick Piggin * restrictions (same for remap_pfn_range). However we would like 16257e675137SNick Piggin * consistency in testing and feature parity among all, so we should 16267e675137SNick Piggin * try to keep these invariants in place for everybody. 16277e675137SNick Piggin */ 1628b379d790SJared Hulbert BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))); 1629b379d790SJared Hulbert BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) == 1630b379d790SJared Hulbert (VM_PFNMAP|VM_MIXEDMAP)); 1631b379d790SJared Hulbert BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); 1632b379d790SJared Hulbert BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn)); 1633e0dc0d8fSNick Piggin 1634423bad60SNick Piggin if (addr < vma->vm_start || addr >= vma->vm_end) 1635423bad60SNick Piggin return -EFAULT; 1636308a047cSBorislav Petkov 1637308a047cSBorislav Petkov track_pfn_insert(vma, &pgprot, __pfn_to_pfn_t(pfn, PFN_DEV)); 16382ab64037Svenkatesh.pallipadi@intel.com 163901c8f1c4SDan Williams ret = insert_pfn(vma, addr, __pfn_to_pfn_t(pfn, PFN_DEV), pgprot); 16402ab64037Svenkatesh.pallipadi@intel.com 16412ab64037Svenkatesh.pallipadi@intel.com return ret; 1642e0dc0d8fSNick Piggin } 16431745cbc5SAndy Lutomirski EXPORT_SYMBOL(vm_insert_pfn_prot); 1644e0dc0d8fSNick Piggin 1645423bad60SNick Piggin int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr, 164601c8f1c4SDan Williams pfn_t pfn) 1647423bad60SNick Piggin { 164887744ab3SDan Williams pgprot_t pgprot = vma->vm_page_prot; 164987744ab3SDan Williams 1650423bad60SNick Piggin BUG_ON(!(vma->vm_flags & VM_MIXEDMAP)); 1651423bad60SNick Piggin 1652423bad60SNick Piggin if (addr < vma->vm_start || addr >= vma->vm_end) 1653423bad60SNick Piggin return -EFAULT; 1654308a047cSBorislav Petkov 1655308a047cSBorislav Petkov track_pfn_insert(vma, &pgprot, pfn); 1656423bad60SNick Piggin 1657423bad60SNick Piggin /* 1658423bad60SNick Piggin * If we don't have pte special, then we have to use the pfn_valid() 1659423bad60SNick Piggin * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must* 1660423bad60SNick Piggin * refcount the page if pfn_valid is true (hence insert_page rather 166162eede62SHugh Dickins * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP 166262eede62SHugh Dickins * without pte special, it would there be refcounted as a normal page. 1663423bad60SNick Piggin */ 166403fc2da6SDan Williams if (!HAVE_PTE_SPECIAL && !pfn_t_devmap(pfn) && pfn_t_valid(pfn)) { 1665423bad60SNick Piggin struct page *page; 1666423bad60SNick Piggin 166703fc2da6SDan Williams /* 166803fc2da6SDan Williams * At this point we are committed to insert_page() 166903fc2da6SDan Williams * regardless of whether the caller specified flags that 167003fc2da6SDan Williams * result in pfn_t_has_page() == false. 167103fc2da6SDan Williams */ 167203fc2da6SDan Williams page = pfn_to_page(pfn_t_to_pfn(pfn)); 167387744ab3SDan Williams return insert_page(vma, addr, page, pgprot); 1674423bad60SNick Piggin } 167587744ab3SDan Williams return insert_pfn(vma, addr, pfn, pgprot); 1676423bad60SNick Piggin } 1677423bad60SNick Piggin EXPORT_SYMBOL(vm_insert_mixed); 1678423bad60SNick Piggin 1679a145dd41SLinus Torvalds /* 16801da177e4SLinus Torvalds * maps a range of physical memory into the requested pages. the old 16811da177e4SLinus Torvalds * mappings are removed. any references to nonexistent pages results 16821da177e4SLinus Torvalds * in null mappings (currently treated as "copy-on-access") 16831da177e4SLinus Torvalds */ 16841da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd, 16851da177e4SLinus Torvalds unsigned long addr, unsigned long end, 16861da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 16871da177e4SLinus Torvalds { 16881da177e4SLinus Torvalds pte_t *pte; 1689c74df32cSHugh Dickins spinlock_t *ptl; 16901da177e4SLinus Torvalds 1691c74df32cSHugh Dickins pte = pte_alloc_map_lock(mm, pmd, addr, &ptl); 16921da177e4SLinus Torvalds if (!pte) 16931da177e4SLinus Torvalds return -ENOMEM; 16946606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 16951da177e4SLinus Torvalds do { 16961da177e4SLinus Torvalds BUG_ON(!pte_none(*pte)); 16977e675137SNick Piggin set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot))); 16981da177e4SLinus Torvalds pfn++; 16991da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 17006606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 1701c74df32cSHugh Dickins pte_unmap_unlock(pte - 1, ptl); 17021da177e4SLinus Torvalds return 0; 17031da177e4SLinus Torvalds } 17041da177e4SLinus Torvalds 17051da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud, 17061da177e4SLinus Torvalds unsigned long addr, unsigned long end, 17071da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 17081da177e4SLinus Torvalds { 17091da177e4SLinus Torvalds pmd_t *pmd; 17101da177e4SLinus Torvalds unsigned long next; 17111da177e4SLinus Torvalds 17121da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 17131da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, addr); 17141da177e4SLinus Torvalds if (!pmd) 17151da177e4SLinus Torvalds return -ENOMEM; 1716f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 17171da177e4SLinus Torvalds do { 17181da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 17191da177e4SLinus Torvalds if (remap_pte_range(mm, pmd, addr, next, 17201da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot)) 17211da177e4SLinus Torvalds return -ENOMEM; 17221da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 17231da177e4SLinus Torvalds return 0; 17241da177e4SLinus Torvalds } 17251da177e4SLinus Torvalds 17261da177e4SLinus Torvalds static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd, 17271da177e4SLinus Torvalds unsigned long addr, unsigned long end, 17281da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 17291da177e4SLinus Torvalds { 17301da177e4SLinus Torvalds pud_t *pud; 17311da177e4SLinus Torvalds unsigned long next; 17321da177e4SLinus Torvalds 17331da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 17341da177e4SLinus Torvalds pud = pud_alloc(mm, pgd, addr); 17351da177e4SLinus Torvalds if (!pud) 17361da177e4SLinus Torvalds return -ENOMEM; 17371da177e4SLinus Torvalds do { 17381da177e4SLinus Torvalds next = pud_addr_end(addr, end); 17391da177e4SLinus Torvalds if (remap_pmd_range(mm, pud, addr, next, 17401da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot)) 17411da177e4SLinus Torvalds return -ENOMEM; 17421da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 17431da177e4SLinus Torvalds return 0; 17441da177e4SLinus Torvalds } 17451da177e4SLinus Torvalds 1746bfa5bf6dSRolf Eike Beer /** 1747bfa5bf6dSRolf Eike Beer * remap_pfn_range - remap kernel memory to userspace 1748bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 1749bfa5bf6dSRolf Eike Beer * @addr: target user address to start at 1750bfa5bf6dSRolf Eike Beer * @pfn: physical address of kernel memory 1751bfa5bf6dSRolf Eike Beer * @size: size of map area 1752bfa5bf6dSRolf Eike Beer * @prot: page protection flags for this mapping 1753bfa5bf6dSRolf Eike Beer * 1754bfa5bf6dSRolf Eike Beer * Note: this is only safe if the mm semaphore is held when called. 1755bfa5bf6dSRolf Eike Beer */ 17561da177e4SLinus Torvalds int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 17571da177e4SLinus Torvalds unsigned long pfn, unsigned long size, pgprot_t prot) 17581da177e4SLinus Torvalds { 17591da177e4SLinus Torvalds pgd_t *pgd; 17601da177e4SLinus Torvalds unsigned long next; 17612d15cab8SHugh Dickins unsigned long end = addr + PAGE_ALIGN(size); 17621da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 1763d5957d2fSYongji Xie unsigned long remap_pfn = pfn; 17641da177e4SLinus Torvalds int err; 17651da177e4SLinus Torvalds 17661da177e4SLinus Torvalds /* 17671da177e4SLinus Torvalds * Physically remapped pages are special. Tell the 17681da177e4SLinus Torvalds * rest of the world about it: 17691da177e4SLinus Torvalds * VM_IO tells people not to look at these pages 17701da177e4SLinus Torvalds * (accesses can have side effects). 17716aab341eSLinus Torvalds * VM_PFNMAP tells the core MM that the base pages are just 17726aab341eSLinus Torvalds * raw PFN mappings, and do not have a "struct page" associated 17736aab341eSLinus Torvalds * with them. 1774314e51b9SKonstantin Khlebnikov * VM_DONTEXPAND 1775314e51b9SKonstantin Khlebnikov * Disable vma merging and expanding with mremap(). 1776314e51b9SKonstantin Khlebnikov * VM_DONTDUMP 1777314e51b9SKonstantin Khlebnikov * Omit vma from core dump, even when VM_IO turned off. 1778fb155c16SLinus Torvalds * 1779fb155c16SLinus Torvalds * There's a horrible special case to handle copy-on-write 1780fb155c16SLinus Torvalds * behaviour that some programs depend on. We mark the "original" 1781fb155c16SLinus Torvalds * un-COW'ed pages by matching them up with "vma->vm_pgoff". 1782b3b9c293SKonstantin Khlebnikov * See vm_normal_page() for details. 17831da177e4SLinus Torvalds */ 1784b3b9c293SKonstantin Khlebnikov if (is_cow_mapping(vma->vm_flags)) { 1785b3b9c293SKonstantin Khlebnikov if (addr != vma->vm_start || end != vma->vm_end) 1786b3b9c293SKonstantin Khlebnikov return -EINVAL; 17876aab341eSLinus Torvalds vma->vm_pgoff = pfn; 1788b3b9c293SKonstantin Khlebnikov } 1789b3b9c293SKonstantin Khlebnikov 1790d5957d2fSYongji Xie err = track_pfn_remap(vma, &prot, remap_pfn, addr, PAGE_ALIGN(size)); 1791b3b9c293SKonstantin Khlebnikov if (err) 17923c8bb73aSvenkatesh.pallipadi@intel.com return -EINVAL; 1793fb155c16SLinus Torvalds 1794314e51b9SKonstantin Khlebnikov vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; 17951da177e4SLinus Torvalds 17961da177e4SLinus Torvalds BUG_ON(addr >= end); 17971da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 17981da177e4SLinus Torvalds pgd = pgd_offset(mm, addr); 17991da177e4SLinus Torvalds flush_cache_range(vma, addr, end); 18001da177e4SLinus Torvalds do { 18011da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 18021da177e4SLinus Torvalds err = remap_pud_range(mm, pgd, addr, next, 18031da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot); 18041da177e4SLinus Torvalds if (err) 18051da177e4SLinus Torvalds break; 18061da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 18072ab64037Svenkatesh.pallipadi@intel.com 18082ab64037Svenkatesh.pallipadi@intel.com if (err) 1809d5957d2fSYongji Xie untrack_pfn(vma, remap_pfn, PAGE_ALIGN(size)); 18102ab64037Svenkatesh.pallipadi@intel.com 18111da177e4SLinus Torvalds return err; 18121da177e4SLinus Torvalds } 18131da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range); 18141da177e4SLinus Torvalds 1815b4cbb197SLinus Torvalds /** 1816b4cbb197SLinus Torvalds * vm_iomap_memory - remap memory to userspace 1817b4cbb197SLinus Torvalds * @vma: user vma to map to 1818b4cbb197SLinus Torvalds * @start: start of area 1819b4cbb197SLinus Torvalds * @len: size of area 1820b4cbb197SLinus Torvalds * 1821b4cbb197SLinus Torvalds * This is a simplified io_remap_pfn_range() for common driver use. The 1822b4cbb197SLinus Torvalds * driver just needs to give us the physical memory range to be mapped, 1823b4cbb197SLinus Torvalds * we'll figure out the rest from the vma information. 1824b4cbb197SLinus Torvalds * 1825b4cbb197SLinus Torvalds * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get 1826b4cbb197SLinus Torvalds * whatever write-combining details or similar. 1827b4cbb197SLinus Torvalds */ 1828b4cbb197SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len) 1829b4cbb197SLinus Torvalds { 1830b4cbb197SLinus Torvalds unsigned long vm_len, pfn, pages; 1831b4cbb197SLinus Torvalds 1832b4cbb197SLinus Torvalds /* Check that the physical memory area passed in looks valid */ 1833b4cbb197SLinus Torvalds if (start + len < start) 1834b4cbb197SLinus Torvalds return -EINVAL; 1835b4cbb197SLinus Torvalds /* 1836b4cbb197SLinus Torvalds * You *really* shouldn't map things that aren't page-aligned, 1837b4cbb197SLinus Torvalds * but we've historically allowed it because IO memory might 1838b4cbb197SLinus Torvalds * just have smaller alignment. 1839b4cbb197SLinus Torvalds */ 1840b4cbb197SLinus Torvalds len += start & ~PAGE_MASK; 1841b4cbb197SLinus Torvalds pfn = start >> PAGE_SHIFT; 1842b4cbb197SLinus Torvalds pages = (len + ~PAGE_MASK) >> PAGE_SHIFT; 1843b4cbb197SLinus Torvalds if (pfn + pages < pfn) 1844b4cbb197SLinus Torvalds return -EINVAL; 1845b4cbb197SLinus Torvalds 1846b4cbb197SLinus Torvalds /* We start the mapping 'vm_pgoff' pages into the area */ 1847b4cbb197SLinus Torvalds if (vma->vm_pgoff > pages) 1848b4cbb197SLinus Torvalds return -EINVAL; 1849b4cbb197SLinus Torvalds pfn += vma->vm_pgoff; 1850b4cbb197SLinus Torvalds pages -= vma->vm_pgoff; 1851b4cbb197SLinus Torvalds 1852b4cbb197SLinus Torvalds /* Can we fit all of the mapping? */ 1853b4cbb197SLinus Torvalds vm_len = vma->vm_end - vma->vm_start; 1854b4cbb197SLinus Torvalds if (vm_len >> PAGE_SHIFT > pages) 1855b4cbb197SLinus Torvalds return -EINVAL; 1856b4cbb197SLinus Torvalds 1857b4cbb197SLinus Torvalds /* Ok, let it rip */ 1858b4cbb197SLinus Torvalds return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot); 1859b4cbb197SLinus Torvalds } 1860b4cbb197SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory); 1861b4cbb197SLinus Torvalds 1862aee16b3cSJeremy Fitzhardinge static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd, 1863aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 1864aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 1865aee16b3cSJeremy Fitzhardinge { 1866aee16b3cSJeremy Fitzhardinge pte_t *pte; 1867aee16b3cSJeremy Fitzhardinge int err; 18682f569afdSMartin Schwidefsky pgtable_t token; 186994909914SBorislav Petkov spinlock_t *uninitialized_var(ptl); 1870aee16b3cSJeremy Fitzhardinge 1871aee16b3cSJeremy Fitzhardinge pte = (mm == &init_mm) ? 1872aee16b3cSJeremy Fitzhardinge pte_alloc_kernel(pmd, addr) : 1873aee16b3cSJeremy Fitzhardinge pte_alloc_map_lock(mm, pmd, addr, &ptl); 1874aee16b3cSJeremy Fitzhardinge if (!pte) 1875aee16b3cSJeremy Fitzhardinge return -ENOMEM; 1876aee16b3cSJeremy Fitzhardinge 1877aee16b3cSJeremy Fitzhardinge BUG_ON(pmd_huge(*pmd)); 1878aee16b3cSJeremy Fitzhardinge 187938e0edb1SJeremy Fitzhardinge arch_enter_lazy_mmu_mode(); 188038e0edb1SJeremy Fitzhardinge 18812f569afdSMartin Schwidefsky token = pmd_pgtable(*pmd); 1882aee16b3cSJeremy Fitzhardinge 1883aee16b3cSJeremy Fitzhardinge do { 1884c36987e2SDaisuke Nishimura err = fn(pte++, token, addr, data); 1885aee16b3cSJeremy Fitzhardinge if (err) 1886aee16b3cSJeremy Fitzhardinge break; 1887c36987e2SDaisuke Nishimura } while (addr += PAGE_SIZE, addr != end); 1888aee16b3cSJeremy Fitzhardinge 188938e0edb1SJeremy Fitzhardinge arch_leave_lazy_mmu_mode(); 189038e0edb1SJeremy Fitzhardinge 1891aee16b3cSJeremy Fitzhardinge if (mm != &init_mm) 1892aee16b3cSJeremy Fitzhardinge pte_unmap_unlock(pte-1, ptl); 1893aee16b3cSJeremy Fitzhardinge return err; 1894aee16b3cSJeremy Fitzhardinge } 1895aee16b3cSJeremy Fitzhardinge 1896aee16b3cSJeremy Fitzhardinge static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud, 1897aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 1898aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 1899aee16b3cSJeremy Fitzhardinge { 1900aee16b3cSJeremy Fitzhardinge pmd_t *pmd; 1901aee16b3cSJeremy Fitzhardinge unsigned long next; 1902aee16b3cSJeremy Fitzhardinge int err; 1903aee16b3cSJeremy Fitzhardinge 1904ceb86879SAndi Kleen BUG_ON(pud_huge(*pud)); 1905ceb86879SAndi Kleen 1906aee16b3cSJeremy Fitzhardinge pmd = pmd_alloc(mm, pud, addr); 1907aee16b3cSJeremy Fitzhardinge if (!pmd) 1908aee16b3cSJeremy Fitzhardinge return -ENOMEM; 1909aee16b3cSJeremy Fitzhardinge do { 1910aee16b3cSJeremy Fitzhardinge next = pmd_addr_end(addr, end); 1911aee16b3cSJeremy Fitzhardinge err = apply_to_pte_range(mm, pmd, addr, next, fn, data); 1912aee16b3cSJeremy Fitzhardinge if (err) 1913aee16b3cSJeremy Fitzhardinge break; 1914aee16b3cSJeremy Fitzhardinge } while (pmd++, addr = next, addr != end); 1915aee16b3cSJeremy Fitzhardinge return err; 1916aee16b3cSJeremy Fitzhardinge } 1917aee16b3cSJeremy Fitzhardinge 1918aee16b3cSJeremy Fitzhardinge static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd, 1919aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 1920aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 1921aee16b3cSJeremy Fitzhardinge { 1922aee16b3cSJeremy Fitzhardinge pud_t *pud; 1923aee16b3cSJeremy Fitzhardinge unsigned long next; 1924aee16b3cSJeremy Fitzhardinge int err; 1925aee16b3cSJeremy Fitzhardinge 1926aee16b3cSJeremy Fitzhardinge pud = pud_alloc(mm, pgd, addr); 1927aee16b3cSJeremy Fitzhardinge if (!pud) 1928aee16b3cSJeremy Fitzhardinge return -ENOMEM; 1929aee16b3cSJeremy Fitzhardinge do { 1930aee16b3cSJeremy Fitzhardinge next = pud_addr_end(addr, end); 1931aee16b3cSJeremy Fitzhardinge err = apply_to_pmd_range(mm, pud, addr, next, fn, data); 1932aee16b3cSJeremy Fitzhardinge if (err) 1933aee16b3cSJeremy Fitzhardinge break; 1934aee16b3cSJeremy Fitzhardinge } while (pud++, addr = next, addr != end); 1935aee16b3cSJeremy Fitzhardinge return err; 1936aee16b3cSJeremy Fitzhardinge } 1937aee16b3cSJeremy Fitzhardinge 1938aee16b3cSJeremy Fitzhardinge /* 1939aee16b3cSJeremy Fitzhardinge * Scan a region of virtual memory, filling in page tables as necessary 1940aee16b3cSJeremy Fitzhardinge * and calling a provided function on each leaf page table. 1941aee16b3cSJeremy Fitzhardinge */ 1942aee16b3cSJeremy Fitzhardinge int apply_to_page_range(struct mm_struct *mm, unsigned long addr, 1943aee16b3cSJeremy Fitzhardinge unsigned long size, pte_fn_t fn, void *data) 1944aee16b3cSJeremy Fitzhardinge { 1945aee16b3cSJeremy Fitzhardinge pgd_t *pgd; 1946aee16b3cSJeremy Fitzhardinge unsigned long next; 194757250a5bSJeremy Fitzhardinge unsigned long end = addr + size; 1948aee16b3cSJeremy Fitzhardinge int err; 1949aee16b3cSJeremy Fitzhardinge 19509cb65bc3SMika Penttilä if (WARN_ON(addr >= end)) 19519cb65bc3SMika Penttilä return -EINVAL; 19529cb65bc3SMika Penttilä 1953aee16b3cSJeremy Fitzhardinge pgd = pgd_offset(mm, addr); 1954aee16b3cSJeremy Fitzhardinge do { 1955aee16b3cSJeremy Fitzhardinge next = pgd_addr_end(addr, end); 1956aee16b3cSJeremy Fitzhardinge err = apply_to_pud_range(mm, pgd, addr, next, fn, data); 1957aee16b3cSJeremy Fitzhardinge if (err) 1958aee16b3cSJeremy Fitzhardinge break; 1959aee16b3cSJeremy Fitzhardinge } while (pgd++, addr = next, addr != end); 196057250a5bSJeremy Fitzhardinge 1961aee16b3cSJeremy Fitzhardinge return err; 1962aee16b3cSJeremy Fitzhardinge } 1963aee16b3cSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(apply_to_page_range); 1964aee16b3cSJeremy Fitzhardinge 19651da177e4SLinus Torvalds /* 19669b4bdd2fSKirill A. Shutemov * handle_pte_fault chooses page fault handler according to an entry which was 19679b4bdd2fSKirill A. Shutemov * read non-atomically. Before making any commitment, on those architectures 19689b4bdd2fSKirill A. Shutemov * or configurations (e.g. i386 with PAE) which might give a mix of unmatched 19699b4bdd2fSKirill A. Shutemov * parts, do_swap_page must check under lock before unmapping the pte and 19709b4bdd2fSKirill A. Shutemov * proceeding (but do_wp_page is only called after already making such a check; 1971a335b2e1SRyota Ozaki * and do_anonymous_page can safely check later on). 19728f4e2101SHugh Dickins */ 19734c21e2f2SHugh Dickins static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd, 19748f4e2101SHugh Dickins pte_t *page_table, pte_t orig_pte) 19758f4e2101SHugh Dickins { 19768f4e2101SHugh Dickins int same = 1; 19778f4e2101SHugh Dickins #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT) 19788f4e2101SHugh Dickins if (sizeof(pte_t) > sizeof(unsigned long)) { 19794c21e2f2SHugh Dickins spinlock_t *ptl = pte_lockptr(mm, pmd); 19804c21e2f2SHugh Dickins spin_lock(ptl); 19818f4e2101SHugh Dickins same = pte_same(*page_table, orig_pte); 19824c21e2f2SHugh Dickins spin_unlock(ptl); 19838f4e2101SHugh Dickins } 19848f4e2101SHugh Dickins #endif 19858f4e2101SHugh Dickins pte_unmap(page_table); 19868f4e2101SHugh Dickins return same; 19878f4e2101SHugh Dickins } 19888f4e2101SHugh Dickins 19899de455b2SAtsushi Nemoto static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma) 19906aab341eSLinus Torvalds { 19910abdd7a8SDan Williams debug_dma_assert_idle(src); 19920abdd7a8SDan Williams 19936aab341eSLinus Torvalds /* 19946aab341eSLinus Torvalds * If the source page was a PFN mapping, we don't have 19956aab341eSLinus Torvalds * a "struct page" for it. We do a best-effort copy by 19966aab341eSLinus Torvalds * just copying from the original user address. If that 19976aab341eSLinus Torvalds * fails, we just zero-fill it. Live with it. 19986aab341eSLinus Torvalds */ 19996aab341eSLinus Torvalds if (unlikely(!src)) { 20009b04c5feSCong Wang void *kaddr = kmap_atomic(dst); 20015d2a2dbbSLinus Torvalds void __user *uaddr = (void __user *)(va & PAGE_MASK); 20025d2a2dbbSLinus Torvalds 20035d2a2dbbSLinus Torvalds /* 20045d2a2dbbSLinus Torvalds * This really shouldn't fail, because the page is there 20055d2a2dbbSLinus Torvalds * in the page tables. But it might just be unreadable, 20065d2a2dbbSLinus Torvalds * in which case we just give up and fill the result with 20075d2a2dbbSLinus Torvalds * zeroes. 20085d2a2dbbSLinus Torvalds */ 20095d2a2dbbSLinus Torvalds if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) 20103ecb01dfSJan Beulich clear_page(kaddr); 20119b04c5feSCong Wang kunmap_atomic(kaddr); 2012c4ec7b0dSDmitriy Monakhov flush_dcache_page(dst); 20130ed361deSNick Piggin } else 20149de455b2SAtsushi Nemoto copy_user_highpage(dst, src, va, vma); 20156aab341eSLinus Torvalds } 20166aab341eSLinus Torvalds 2017c20cd45eSMichal Hocko static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma) 2018c20cd45eSMichal Hocko { 2019c20cd45eSMichal Hocko struct file *vm_file = vma->vm_file; 2020c20cd45eSMichal Hocko 2021c20cd45eSMichal Hocko if (vm_file) 2022c20cd45eSMichal Hocko return mapping_gfp_mask(vm_file->f_mapping) | __GFP_FS | __GFP_IO; 2023c20cd45eSMichal Hocko 2024c20cd45eSMichal Hocko /* 2025c20cd45eSMichal Hocko * Special mappings (e.g. VDSO) do not have any file so fake 2026c20cd45eSMichal Hocko * a default GFP_KERNEL for them. 2027c20cd45eSMichal Hocko */ 2028c20cd45eSMichal Hocko return GFP_KERNEL; 2029c20cd45eSMichal Hocko } 2030c20cd45eSMichal Hocko 20311da177e4SLinus Torvalds /* 2032fb09a464SKirill A. Shutemov * Notify the address space that the page is about to become writable so that 2033fb09a464SKirill A. Shutemov * it can prohibit this or wait for the page to get into an appropriate state. 2034fb09a464SKirill A. Shutemov * 2035fb09a464SKirill A. Shutemov * We do this without the lock held, so that it can sleep if it needs to. 2036fb09a464SKirill A. Shutemov */ 2037fb09a464SKirill A. Shutemov static int do_page_mkwrite(struct vm_area_struct *vma, struct page *page, 2038fb09a464SKirill A. Shutemov unsigned long address) 2039fb09a464SKirill A. Shutemov { 2040fb09a464SKirill A. Shutemov struct vm_fault vmf; 2041fb09a464SKirill A. Shutemov int ret; 2042fb09a464SKirill A. Shutemov 20431a29d85eSJan Kara vmf.address = address & PAGE_MASK; 2044fb09a464SKirill A. Shutemov vmf.pgoff = page->index; 2045fb09a464SKirill A. Shutemov vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE; 2046c20cd45eSMichal Hocko vmf.gfp_mask = __get_fault_gfp_mask(vma); 2047fb09a464SKirill A. Shutemov vmf.page = page; 20482e4cdab0SMatthew Wilcox vmf.cow_page = NULL; 2049fb09a464SKirill A. Shutemov 2050fb09a464SKirill A. Shutemov ret = vma->vm_ops->page_mkwrite(vma, &vmf); 2051fb09a464SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) 2052fb09a464SKirill A. Shutemov return ret; 2053fb09a464SKirill A. Shutemov if (unlikely(!(ret & VM_FAULT_LOCKED))) { 2054fb09a464SKirill A. Shutemov lock_page(page); 2055fb09a464SKirill A. Shutemov if (!page->mapping) { 2056fb09a464SKirill A. Shutemov unlock_page(page); 2057fb09a464SKirill A. Shutemov return 0; /* retry */ 2058fb09a464SKirill A. Shutemov } 2059fb09a464SKirill A. Shutemov ret |= VM_FAULT_LOCKED; 2060fb09a464SKirill A. Shutemov } else 2061fb09a464SKirill A. Shutemov VM_BUG_ON_PAGE(!PageLocked(page), page); 2062fb09a464SKirill A. Shutemov return ret; 2063fb09a464SKirill A. Shutemov } 2064fb09a464SKirill A. Shutemov 2065fb09a464SKirill A. Shutemov /* 20664e047f89SShachar Raindel * Handle write page faults for pages that can be reused in the current vma 20674e047f89SShachar Raindel * 20684e047f89SShachar Raindel * This can happen either due to the mapping being with the VM_SHARED flag, 20694e047f89SShachar Raindel * or due to us being the last reference standing to the page. In either 20704e047f89SShachar Raindel * case, all we need to do here is to mark the page as writable and update 20714e047f89SShachar Raindel * any related book-keeping. 20724e047f89SShachar Raindel */ 207382b0f8c3SJan Kara static inline int wp_page_reuse(struct vm_fault *vmf, pte_t orig_pte, 2074bae473a4SKirill A. Shutemov struct page *page, int page_mkwrite, int dirty_shared) 207582b0f8c3SJan Kara __releases(vmf->ptl) 20764e047f89SShachar Raindel { 207782b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 20784e047f89SShachar Raindel pte_t entry; 20794e047f89SShachar Raindel /* 20804e047f89SShachar Raindel * Clear the pages cpupid information as the existing 20814e047f89SShachar Raindel * information potentially belongs to a now completely 20824e047f89SShachar Raindel * unrelated process. 20834e047f89SShachar Raindel */ 20844e047f89SShachar Raindel if (page) 20854e047f89SShachar Raindel page_cpupid_xchg_last(page, (1 << LAST_CPUPID_SHIFT) - 1); 20864e047f89SShachar Raindel 208782b0f8c3SJan Kara flush_cache_page(vma, vmf->address, pte_pfn(orig_pte)); 20884e047f89SShachar Raindel entry = pte_mkyoung(orig_pte); 20894e047f89SShachar Raindel entry = maybe_mkwrite(pte_mkdirty(entry), vma); 209082b0f8c3SJan Kara if (ptep_set_access_flags(vma, vmf->address, vmf->pte, entry, 1)) 209182b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 209282b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 20934e047f89SShachar Raindel 20944e047f89SShachar Raindel if (dirty_shared) { 20954e047f89SShachar Raindel struct address_space *mapping; 20964e047f89SShachar Raindel int dirtied; 20974e047f89SShachar Raindel 20984e047f89SShachar Raindel if (!page_mkwrite) 20994e047f89SShachar Raindel lock_page(page); 21004e047f89SShachar Raindel 21014e047f89SShachar Raindel dirtied = set_page_dirty(page); 21024e047f89SShachar Raindel VM_BUG_ON_PAGE(PageAnon(page), page); 21034e047f89SShachar Raindel mapping = page->mapping; 21044e047f89SShachar Raindel unlock_page(page); 210509cbfeafSKirill A. Shutemov put_page(page); 21064e047f89SShachar Raindel 21074e047f89SShachar Raindel if ((dirtied || page_mkwrite) && mapping) { 21084e047f89SShachar Raindel /* 21094e047f89SShachar Raindel * Some device drivers do not set page.mapping 21104e047f89SShachar Raindel * but still dirty their pages 21114e047f89SShachar Raindel */ 21124e047f89SShachar Raindel balance_dirty_pages_ratelimited(mapping); 21134e047f89SShachar Raindel } 21144e047f89SShachar Raindel 21154e047f89SShachar Raindel if (!page_mkwrite) 21164e047f89SShachar Raindel file_update_time(vma->vm_file); 21174e047f89SShachar Raindel } 21184e047f89SShachar Raindel 21194e047f89SShachar Raindel return VM_FAULT_WRITE; 21204e047f89SShachar Raindel } 21214e047f89SShachar Raindel 21224e047f89SShachar Raindel /* 21232f38ab2cSShachar Raindel * Handle the case of a page which we actually need to copy to a new page. 21242f38ab2cSShachar Raindel * 21252f38ab2cSShachar Raindel * Called with mmap_sem locked and the old page referenced, but 21262f38ab2cSShachar Raindel * without the ptl held. 21272f38ab2cSShachar Raindel * 21282f38ab2cSShachar Raindel * High level logic flow: 21292f38ab2cSShachar Raindel * 21302f38ab2cSShachar Raindel * - Allocate a page, copy the content of the old page to the new one. 21312f38ab2cSShachar Raindel * - Handle book keeping and accounting - cgroups, mmu-notifiers, etc. 21322f38ab2cSShachar Raindel * - Take the PTL. If the pte changed, bail out and release the allocated page 21332f38ab2cSShachar Raindel * - If the pte is still the way we remember it, update the page table and all 21342f38ab2cSShachar Raindel * relevant references. This includes dropping the reference the page-table 21352f38ab2cSShachar Raindel * held to the old page, as well as updating the rmap. 21362f38ab2cSShachar Raindel * - In any case, unlock the PTL and drop the reference we took to the old page. 21372f38ab2cSShachar Raindel */ 213882b0f8c3SJan Kara static int wp_page_copy(struct vm_fault *vmf, pte_t orig_pte, 2139bae473a4SKirill A. Shutemov struct page *old_page) 21402f38ab2cSShachar Raindel { 214182b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 2142bae473a4SKirill A. Shutemov struct mm_struct *mm = vma->vm_mm; 21432f38ab2cSShachar Raindel struct page *new_page = NULL; 21442f38ab2cSShachar Raindel pte_t entry; 21452f38ab2cSShachar Raindel int page_copied = 0; 214682b0f8c3SJan Kara const unsigned long mmun_start = vmf->address & PAGE_MASK; 2147bae473a4SKirill A. Shutemov const unsigned long mmun_end = mmun_start + PAGE_SIZE; 21482f38ab2cSShachar Raindel struct mem_cgroup *memcg; 21492f38ab2cSShachar Raindel 21502f38ab2cSShachar Raindel if (unlikely(anon_vma_prepare(vma))) 21512f38ab2cSShachar Raindel goto oom; 21522f38ab2cSShachar Raindel 21532f38ab2cSShachar Raindel if (is_zero_pfn(pte_pfn(orig_pte))) { 215482b0f8c3SJan Kara new_page = alloc_zeroed_user_highpage_movable(vma, 215582b0f8c3SJan Kara vmf->address); 21562f38ab2cSShachar Raindel if (!new_page) 21572f38ab2cSShachar Raindel goto oom; 21582f38ab2cSShachar Raindel } else { 2159bae473a4SKirill A. Shutemov new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, 216082b0f8c3SJan Kara vmf->address); 21612f38ab2cSShachar Raindel if (!new_page) 21622f38ab2cSShachar Raindel goto oom; 216382b0f8c3SJan Kara cow_user_page(new_page, old_page, vmf->address, vma); 21642f38ab2cSShachar Raindel } 21652f38ab2cSShachar Raindel 2166f627c2f5SKirill A. Shutemov if (mem_cgroup_try_charge(new_page, mm, GFP_KERNEL, &memcg, false)) 21672f38ab2cSShachar Raindel goto oom_free_new; 21682f38ab2cSShachar Raindel 2169eb3c24f3SMel Gorman __SetPageUptodate(new_page); 2170eb3c24f3SMel Gorman 21712f38ab2cSShachar Raindel mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); 21722f38ab2cSShachar Raindel 21732f38ab2cSShachar Raindel /* 21742f38ab2cSShachar Raindel * Re-check the pte - we dropped the lock 21752f38ab2cSShachar Raindel */ 217682b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(mm, vmf->pmd, vmf->address, &vmf->ptl); 217782b0f8c3SJan Kara if (likely(pte_same(*vmf->pte, orig_pte))) { 21782f38ab2cSShachar Raindel if (old_page) { 21792f38ab2cSShachar Raindel if (!PageAnon(old_page)) { 2180eca56ff9SJerome Marchand dec_mm_counter_fast(mm, 2181eca56ff9SJerome Marchand mm_counter_file(old_page)); 21822f38ab2cSShachar Raindel inc_mm_counter_fast(mm, MM_ANONPAGES); 21832f38ab2cSShachar Raindel } 21842f38ab2cSShachar Raindel } else { 21852f38ab2cSShachar Raindel inc_mm_counter_fast(mm, MM_ANONPAGES); 21862f38ab2cSShachar Raindel } 218782b0f8c3SJan Kara flush_cache_page(vma, vmf->address, pte_pfn(orig_pte)); 21882f38ab2cSShachar Raindel entry = mk_pte(new_page, vma->vm_page_prot); 21892f38ab2cSShachar Raindel entry = maybe_mkwrite(pte_mkdirty(entry), vma); 21902f38ab2cSShachar Raindel /* 21912f38ab2cSShachar Raindel * Clear the pte entry and flush it first, before updating the 21922f38ab2cSShachar Raindel * pte with the new entry. This will avoid a race condition 21932f38ab2cSShachar Raindel * seen in the presence of one thread doing SMC and another 21942f38ab2cSShachar Raindel * thread doing COW. 21952f38ab2cSShachar Raindel */ 219682b0f8c3SJan Kara ptep_clear_flush_notify(vma, vmf->address, vmf->pte); 219782b0f8c3SJan Kara page_add_new_anon_rmap(new_page, vma, vmf->address, false); 2198f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(new_page, memcg, false, false); 21992f38ab2cSShachar Raindel lru_cache_add_active_or_unevictable(new_page, vma); 22002f38ab2cSShachar Raindel /* 22012f38ab2cSShachar Raindel * We call the notify macro here because, when using secondary 22022f38ab2cSShachar Raindel * mmu page tables (such as kvm shadow page tables), we want the 22032f38ab2cSShachar Raindel * new page to be mapped directly into the secondary page table. 22042f38ab2cSShachar Raindel */ 220582b0f8c3SJan Kara set_pte_at_notify(mm, vmf->address, vmf->pte, entry); 220682b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 22072f38ab2cSShachar Raindel if (old_page) { 22082f38ab2cSShachar Raindel /* 22092f38ab2cSShachar Raindel * Only after switching the pte to the new page may 22102f38ab2cSShachar Raindel * we remove the mapcount here. Otherwise another 22112f38ab2cSShachar Raindel * process may come and find the rmap count decremented 22122f38ab2cSShachar Raindel * before the pte is switched to the new page, and 22132f38ab2cSShachar Raindel * "reuse" the old page writing into it while our pte 22142f38ab2cSShachar Raindel * here still points into it and can be read by other 22152f38ab2cSShachar Raindel * threads. 22162f38ab2cSShachar Raindel * 22172f38ab2cSShachar Raindel * The critical issue is to order this 22182f38ab2cSShachar Raindel * page_remove_rmap with the ptp_clear_flush above. 22192f38ab2cSShachar Raindel * Those stores are ordered by (if nothing else,) 22202f38ab2cSShachar Raindel * the barrier present in the atomic_add_negative 22212f38ab2cSShachar Raindel * in page_remove_rmap. 22222f38ab2cSShachar Raindel * 22232f38ab2cSShachar Raindel * Then the TLB flush in ptep_clear_flush ensures that 22242f38ab2cSShachar Raindel * no process can access the old page before the 22252f38ab2cSShachar Raindel * decremented mapcount is visible. And the old page 22262f38ab2cSShachar Raindel * cannot be reused until after the decremented 22272f38ab2cSShachar Raindel * mapcount is visible. So transitively, TLBs to 22282f38ab2cSShachar Raindel * old page will be flushed before it can be reused. 22292f38ab2cSShachar Raindel */ 2230d281ee61SKirill A. Shutemov page_remove_rmap(old_page, false); 22312f38ab2cSShachar Raindel } 22322f38ab2cSShachar Raindel 22332f38ab2cSShachar Raindel /* Free the old page.. */ 22342f38ab2cSShachar Raindel new_page = old_page; 22352f38ab2cSShachar Raindel page_copied = 1; 22362f38ab2cSShachar Raindel } else { 2237f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(new_page, memcg, false); 22382f38ab2cSShachar Raindel } 22392f38ab2cSShachar Raindel 22402f38ab2cSShachar Raindel if (new_page) 224109cbfeafSKirill A. Shutemov put_page(new_page); 22422f38ab2cSShachar Raindel 224382b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 22442f38ab2cSShachar Raindel mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); 22452f38ab2cSShachar Raindel if (old_page) { 22462f38ab2cSShachar Raindel /* 22472f38ab2cSShachar Raindel * Don't let another task, with possibly unlocked vma, 22482f38ab2cSShachar Raindel * keep the mlocked page. 22492f38ab2cSShachar Raindel */ 22502f38ab2cSShachar Raindel if (page_copied && (vma->vm_flags & VM_LOCKED)) { 22512f38ab2cSShachar Raindel lock_page(old_page); /* LRU manipulation */ 2252e90309c9SKirill A. Shutemov if (PageMlocked(old_page)) 22532f38ab2cSShachar Raindel munlock_vma_page(old_page); 22542f38ab2cSShachar Raindel unlock_page(old_page); 22552f38ab2cSShachar Raindel } 225609cbfeafSKirill A. Shutemov put_page(old_page); 22572f38ab2cSShachar Raindel } 22582f38ab2cSShachar Raindel return page_copied ? VM_FAULT_WRITE : 0; 22592f38ab2cSShachar Raindel oom_free_new: 226009cbfeafSKirill A. Shutemov put_page(new_page); 22612f38ab2cSShachar Raindel oom: 22622f38ab2cSShachar Raindel if (old_page) 226309cbfeafSKirill A. Shutemov put_page(old_page); 22642f38ab2cSShachar Raindel return VM_FAULT_OOM; 22652f38ab2cSShachar Raindel } 22662f38ab2cSShachar Raindel 2267dd906184SBoaz Harrosh /* 2268dd906184SBoaz Harrosh * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED 2269dd906184SBoaz Harrosh * mapping 2270dd906184SBoaz Harrosh */ 227182b0f8c3SJan Kara static int wp_pfn_shared(struct vm_fault *vmf, pte_t orig_pte) 2272dd906184SBoaz Harrosh { 227382b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 2274bae473a4SKirill A. Shutemov 2275dd906184SBoaz Harrosh if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) { 2276dd906184SBoaz Harrosh int ret; 2277dd906184SBoaz Harrosh 227882b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2279*fe82221fSJan Kara vmf->flags |= FAULT_FLAG_MKWRITE; 2280*fe82221fSJan Kara ret = vma->vm_ops->pfn_mkwrite(vma, vmf); 2281dd906184SBoaz Harrosh if (ret & VM_FAULT_ERROR) 2282dd906184SBoaz Harrosh return ret; 228382b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 228482b0f8c3SJan Kara vmf->address, &vmf->ptl); 2285dd906184SBoaz Harrosh /* 2286dd906184SBoaz Harrosh * We might have raced with another page fault while we 2287dd906184SBoaz Harrosh * released the pte_offset_map_lock. 2288dd906184SBoaz Harrosh */ 228982b0f8c3SJan Kara if (!pte_same(*vmf->pte, orig_pte)) { 229082b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2291dd906184SBoaz Harrosh return 0; 2292dd906184SBoaz Harrosh } 2293dd906184SBoaz Harrosh } 229482b0f8c3SJan Kara return wp_page_reuse(vmf, orig_pte, NULL, 0, 0); 2295dd906184SBoaz Harrosh } 2296dd906184SBoaz Harrosh 229782b0f8c3SJan Kara static int wp_page_shared(struct vm_fault *vmf, pte_t orig_pte, 229893e478d4SShachar Raindel struct page *old_page) 229982b0f8c3SJan Kara __releases(vmf->ptl) 230093e478d4SShachar Raindel { 230182b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 230293e478d4SShachar Raindel int page_mkwrite = 0; 230393e478d4SShachar Raindel 230409cbfeafSKirill A. Shutemov get_page(old_page); 230593e478d4SShachar Raindel 230693e478d4SShachar Raindel if (vma->vm_ops && vma->vm_ops->page_mkwrite) { 230793e478d4SShachar Raindel int tmp; 230893e478d4SShachar Raindel 230982b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 231082b0f8c3SJan Kara tmp = do_page_mkwrite(vma, old_page, vmf->address); 231193e478d4SShachar Raindel if (unlikely(!tmp || (tmp & 231293e478d4SShachar Raindel (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { 231309cbfeafSKirill A. Shutemov put_page(old_page); 231493e478d4SShachar Raindel return tmp; 231593e478d4SShachar Raindel } 231693e478d4SShachar Raindel /* 231793e478d4SShachar Raindel * Since we dropped the lock we need to revalidate 231893e478d4SShachar Raindel * the PTE as someone else may have changed it. If 231993e478d4SShachar Raindel * they did, we just return, as we can count on the 232093e478d4SShachar Raindel * MMU to tell us if they didn't also make it writable. 232193e478d4SShachar Raindel */ 232282b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 232382b0f8c3SJan Kara vmf->address, &vmf->ptl); 232482b0f8c3SJan Kara if (!pte_same(*vmf->pte, orig_pte)) { 232593e478d4SShachar Raindel unlock_page(old_page); 232682b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 232709cbfeafSKirill A. Shutemov put_page(old_page); 232893e478d4SShachar Raindel return 0; 232993e478d4SShachar Raindel } 233093e478d4SShachar Raindel page_mkwrite = 1; 233193e478d4SShachar Raindel } 233293e478d4SShachar Raindel 233382b0f8c3SJan Kara return wp_page_reuse(vmf, orig_pte, old_page, page_mkwrite, 1); 233493e478d4SShachar Raindel } 233593e478d4SShachar Raindel 23362f38ab2cSShachar Raindel /* 23371da177e4SLinus Torvalds * This routine handles present pages, when users try to write 23381da177e4SLinus Torvalds * to a shared page. It is done by copying the page to a new address 23391da177e4SLinus Torvalds * and decrementing the shared-page counter for the old page. 23401da177e4SLinus Torvalds * 23411da177e4SLinus Torvalds * Note that this routine assumes that the protection checks have been 23421da177e4SLinus Torvalds * done by the caller (the low-level page fault routine in most cases). 23431da177e4SLinus Torvalds * Thus we can safely just mark it writable once we've done any necessary 23441da177e4SLinus Torvalds * COW. 23451da177e4SLinus Torvalds * 23461da177e4SLinus Torvalds * We also mark the page dirty at this point even though the page will 23471da177e4SLinus Torvalds * change only once the write actually happens. This avoids a few races, 23481da177e4SLinus Torvalds * and potentially makes it more efficient. 23491da177e4SLinus Torvalds * 23508f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 23518f4e2101SHugh Dickins * but allow concurrent faults), with pte both mapped and locked. 23528f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 23531da177e4SLinus Torvalds */ 235482b0f8c3SJan Kara static int do_wp_page(struct vm_fault *vmf, pte_t orig_pte) 235582b0f8c3SJan Kara __releases(vmf->ptl) 23561da177e4SLinus Torvalds { 235782b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 23582f38ab2cSShachar Raindel struct page *old_page; 23591da177e4SLinus Torvalds 236082b0f8c3SJan Kara old_page = vm_normal_page(vma, vmf->address, orig_pte); 2361251b97f5SPeter Zijlstra if (!old_page) { 2362251b97f5SPeter Zijlstra /* 236364e45507SPeter Feiner * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a 236464e45507SPeter Feiner * VM_PFNMAP VMA. 2365251b97f5SPeter Zijlstra * 2366251b97f5SPeter Zijlstra * We should not cow pages in a shared writeable mapping. 2367dd906184SBoaz Harrosh * Just mark the pages writable and/or call ops->pfn_mkwrite. 2368251b97f5SPeter Zijlstra */ 2369251b97f5SPeter Zijlstra if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 2370251b97f5SPeter Zijlstra (VM_WRITE|VM_SHARED)) 237182b0f8c3SJan Kara return wp_pfn_shared(vmf, orig_pte); 23722f38ab2cSShachar Raindel 237382b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 237482b0f8c3SJan Kara return wp_page_copy(vmf, orig_pte, old_page); 2375251b97f5SPeter Zijlstra } 23761da177e4SLinus Torvalds 2377d08b3851SPeter Zijlstra /* 2378ee6a6457SPeter Zijlstra * Take out anonymous pages first, anonymous shared vmas are 2379ee6a6457SPeter Zijlstra * not dirty accountable. 2380d08b3851SPeter Zijlstra */ 23819a840895SHugh Dickins if (PageAnon(old_page) && !PageKsm(old_page)) { 23826d0a07edSAndrea Arcangeli int total_mapcount; 2383ab967d86SHugh Dickins if (!trylock_page(old_page)) { 238409cbfeafSKirill A. Shutemov get_page(old_page); 238582b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2386ab967d86SHugh Dickins lock_page(old_page); 238782b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 238882b0f8c3SJan Kara vmf->address, &vmf->ptl); 238982b0f8c3SJan Kara if (!pte_same(*vmf->pte, orig_pte)) { 2390ab967d86SHugh Dickins unlock_page(old_page); 239182b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 239209cbfeafSKirill A. Shutemov put_page(old_page); 239328766805SShachar Raindel return 0; 2394ab967d86SHugh Dickins } 239509cbfeafSKirill A. Shutemov put_page(old_page); 2396ab967d86SHugh Dickins } 23976d0a07edSAndrea Arcangeli if (reuse_swap_page(old_page, &total_mapcount)) { 23986d0a07edSAndrea Arcangeli if (total_mapcount == 1) { 2399c44b6743SRik van Riel /* 24006d0a07edSAndrea Arcangeli * The page is all ours. Move it to 24016d0a07edSAndrea Arcangeli * our anon_vma so the rmap code will 24026d0a07edSAndrea Arcangeli * not search our parent or siblings. 24036d0a07edSAndrea Arcangeli * Protected against the rmap code by 24046d0a07edSAndrea Arcangeli * the page lock. 2405c44b6743SRik van Riel */ 24065a49973dSHugh Dickins page_move_anon_rmap(old_page, vma); 24076d0a07edSAndrea Arcangeli } 2408ee6a6457SPeter Zijlstra unlock_page(old_page); 240982b0f8c3SJan Kara return wp_page_reuse(vmf, orig_pte, old_page, 0, 0); 2410b009c024SMichel Lespinasse } 2411b009c024SMichel Lespinasse unlock_page(old_page); 2412ee6a6457SPeter Zijlstra } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 2413d08b3851SPeter Zijlstra (VM_WRITE|VM_SHARED))) { 241482b0f8c3SJan Kara return wp_page_shared(vmf, orig_pte, old_page); 24151da177e4SLinus Torvalds } 24161da177e4SLinus Torvalds 24171da177e4SLinus Torvalds /* 24181da177e4SLinus Torvalds * Ok, we need to copy. Oh, well.. 24191da177e4SLinus Torvalds */ 242009cbfeafSKirill A. Shutemov get_page(old_page); 242128766805SShachar Raindel 242282b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 242382b0f8c3SJan Kara return wp_page_copy(vmf, orig_pte, old_page); 24241da177e4SLinus Torvalds } 24251da177e4SLinus Torvalds 242697a89413SPeter Zijlstra static void unmap_mapping_range_vma(struct vm_area_struct *vma, 24271da177e4SLinus Torvalds unsigned long start_addr, unsigned long end_addr, 24281da177e4SLinus Torvalds struct zap_details *details) 24291da177e4SLinus Torvalds { 2430f5cc4eefSAl Viro zap_page_range_single(vma, start_addr, end_addr - start_addr, details); 24311da177e4SLinus Torvalds } 24321da177e4SLinus Torvalds 24336b2dbba8SMichel Lespinasse static inline void unmap_mapping_range_tree(struct rb_root *root, 24341da177e4SLinus Torvalds struct zap_details *details) 24351da177e4SLinus Torvalds { 24361da177e4SLinus Torvalds struct vm_area_struct *vma; 24371da177e4SLinus Torvalds pgoff_t vba, vea, zba, zea; 24381da177e4SLinus Torvalds 24396b2dbba8SMichel Lespinasse vma_interval_tree_foreach(vma, root, 24401da177e4SLinus Torvalds details->first_index, details->last_index) { 24411da177e4SLinus Torvalds 24421da177e4SLinus Torvalds vba = vma->vm_pgoff; 2443d6e93217SLibin vea = vba + vma_pages(vma) - 1; 24441da177e4SLinus Torvalds zba = details->first_index; 24451da177e4SLinus Torvalds if (zba < vba) 24461da177e4SLinus Torvalds zba = vba; 24471da177e4SLinus Torvalds zea = details->last_index; 24481da177e4SLinus Torvalds if (zea > vea) 24491da177e4SLinus Torvalds zea = vea; 24501da177e4SLinus Torvalds 245197a89413SPeter Zijlstra unmap_mapping_range_vma(vma, 24521da177e4SLinus Torvalds ((zba - vba) << PAGE_SHIFT) + vma->vm_start, 24531da177e4SLinus Torvalds ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start, 245497a89413SPeter Zijlstra details); 24551da177e4SLinus Torvalds } 24561da177e4SLinus Torvalds } 24571da177e4SLinus Torvalds 24581da177e4SLinus Torvalds /** 24598a5f14a2SKirill A. Shutemov * unmap_mapping_range - unmap the portion of all mmaps in the specified 24608a5f14a2SKirill A. Shutemov * address_space corresponding to the specified page range in the underlying 24618a5f14a2SKirill A. Shutemov * file. 24628a5f14a2SKirill A. Shutemov * 24633d41088fSMartin Waitz * @mapping: the address space containing mmaps to be unmapped. 24641da177e4SLinus Torvalds * @holebegin: byte in first page to unmap, relative to the start of 24651da177e4SLinus Torvalds * the underlying file. This will be rounded down to a PAGE_SIZE 246625d9e2d1Snpiggin@suse.de * boundary. Note that this is different from truncate_pagecache(), which 24671da177e4SLinus Torvalds * must keep the partial page. In contrast, we must get rid of 24681da177e4SLinus Torvalds * partial pages. 24691da177e4SLinus Torvalds * @holelen: size of prospective hole in bytes. This will be rounded 24701da177e4SLinus Torvalds * up to a PAGE_SIZE boundary. A holelen of zero truncates to the 24711da177e4SLinus Torvalds * end of the file. 24721da177e4SLinus Torvalds * @even_cows: 1 when truncating a file, unmap even private COWed pages; 24731da177e4SLinus Torvalds * but 0 when invalidating pagecache, don't throw away private data. 24741da177e4SLinus Torvalds */ 24751da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping, 24761da177e4SLinus Torvalds loff_t const holebegin, loff_t const holelen, int even_cows) 24771da177e4SLinus Torvalds { 2478aac45363SMichal Hocko struct zap_details details = { }; 24791da177e4SLinus Torvalds pgoff_t hba = holebegin >> PAGE_SHIFT; 24801da177e4SLinus Torvalds pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 24811da177e4SLinus Torvalds 24821da177e4SLinus Torvalds /* Check for overflow. */ 24831da177e4SLinus Torvalds if (sizeof(holelen) > sizeof(hlen)) { 24841da177e4SLinus Torvalds long long holeend = 24851da177e4SLinus Torvalds (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 24861da177e4SLinus Torvalds if (holeend & ~(long long)ULONG_MAX) 24871da177e4SLinus Torvalds hlen = ULONG_MAX - hba + 1; 24881da177e4SLinus Torvalds } 24891da177e4SLinus Torvalds 24901da177e4SLinus Torvalds details.check_mapping = even_cows? NULL: mapping; 24911da177e4SLinus Torvalds details.first_index = hba; 24921da177e4SLinus Torvalds details.last_index = hba + hlen - 1; 24931da177e4SLinus Torvalds if (details.last_index < details.first_index) 24941da177e4SLinus Torvalds details.last_index = ULONG_MAX; 24951da177e4SLinus Torvalds 249648ec833bSKirill A. Shutemov i_mmap_lock_write(mapping); 24976b2dbba8SMichel Lespinasse if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap))) 24981da177e4SLinus Torvalds unmap_mapping_range_tree(&mapping->i_mmap, &details); 249948ec833bSKirill A. Shutemov i_mmap_unlock_write(mapping); 25001da177e4SLinus Torvalds } 25011da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range); 25021da177e4SLinus Torvalds 25031da177e4SLinus Torvalds /* 25048f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 25058f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 25069a95f3cfSPaul Cassella * We return with pte unmapped and unlocked. 25079a95f3cfSPaul Cassella * 25089a95f3cfSPaul Cassella * We return with the mmap_sem locked or unlocked in the same cases 25099a95f3cfSPaul Cassella * as does filemap_fault(). 25101da177e4SLinus Torvalds */ 251182b0f8c3SJan Kara int do_swap_page(struct vm_fault *vmf, pte_t orig_pte) 25121da177e4SLinus Torvalds { 251382b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 251456f31801SHugh Dickins struct page *page, *swapcache; 251500501b53SJohannes Weiner struct mem_cgroup *memcg; 251665500d23SHugh Dickins swp_entry_t entry; 25171da177e4SLinus Torvalds pte_t pte; 2518d065bd81SMichel Lespinasse int locked; 2519ad8c2ee8SRik van Riel int exclusive = 0; 252083c54070SNick Piggin int ret = 0; 25211da177e4SLinus Torvalds 252282b0f8c3SJan Kara if (!pte_unmap_same(vma->vm_mm, vmf->pmd, vmf->pte, orig_pte)) 25238f4e2101SHugh Dickins goto out; 252465500d23SHugh Dickins 252565500d23SHugh Dickins entry = pte_to_swp_entry(orig_pte); 2526d1737fdbSAndi Kleen if (unlikely(non_swap_entry(entry))) { 25270697212aSChristoph Lameter if (is_migration_entry(entry)) { 252882b0f8c3SJan Kara migration_entry_wait(vma->vm_mm, vmf->pmd, 252982b0f8c3SJan Kara vmf->address); 2530d1737fdbSAndi Kleen } else if (is_hwpoison_entry(entry)) { 2531d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 2532d1737fdbSAndi Kleen } else { 253382b0f8c3SJan Kara print_bad_pte(vma, vmf->address, orig_pte, NULL); 2534d99be1a8SHugh Dickins ret = VM_FAULT_SIGBUS; 2535d1737fdbSAndi Kleen } 25360697212aSChristoph Lameter goto out; 25370697212aSChristoph Lameter } 25380ff92245SShailabh Nagar delayacct_set_flag(DELAYACCT_PF_SWAPIN); 25391da177e4SLinus Torvalds page = lookup_swap_cache(entry); 25401da177e4SLinus Torvalds if (!page) { 254182b0f8c3SJan Kara page = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, vma, 254282b0f8c3SJan Kara vmf->address); 25431da177e4SLinus Torvalds if (!page) { 25441da177e4SLinus Torvalds /* 25458f4e2101SHugh Dickins * Back out if somebody else faulted in this pte 25468f4e2101SHugh Dickins * while we released the pte lock. 25471da177e4SLinus Torvalds */ 254882b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 254982b0f8c3SJan Kara vmf->address, &vmf->ptl); 255082b0f8c3SJan Kara if (likely(pte_same(*vmf->pte, orig_pte))) 25511da177e4SLinus Torvalds ret = VM_FAULT_OOM; 25520ff92245SShailabh Nagar delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 255365500d23SHugh Dickins goto unlock; 25541da177e4SLinus Torvalds } 25551da177e4SLinus Torvalds 25561da177e4SLinus Torvalds /* Had to read the page from swap area: Major fault */ 25571da177e4SLinus Torvalds ret = VM_FAULT_MAJOR; 2558f8891e5eSChristoph Lameter count_vm_event(PGMAJFAULT); 2559bae473a4SKirill A. Shutemov mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT); 2560d1737fdbSAndi Kleen } else if (PageHWPoison(page)) { 256171f72525SWu Fengguang /* 256271f72525SWu Fengguang * hwpoisoned dirty swapcache pages are kept for killing 256371f72525SWu Fengguang * owner processes (which may be unknown at hwpoison time) 256471f72525SWu Fengguang */ 2565d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 2566d1737fdbSAndi Kleen delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 256756f31801SHugh Dickins swapcache = page; 25684779cb31SAndi Kleen goto out_release; 25691da177e4SLinus Torvalds } 25701da177e4SLinus Torvalds 257156f31801SHugh Dickins swapcache = page; 257282b0f8c3SJan Kara locked = lock_page_or_retry(page, vma->vm_mm, vmf->flags); 2573e709ffd6SRik van Riel 257420a1022dSBalbir Singh delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 2575d065bd81SMichel Lespinasse if (!locked) { 2576d065bd81SMichel Lespinasse ret |= VM_FAULT_RETRY; 2577d065bd81SMichel Lespinasse goto out_release; 2578d065bd81SMichel Lespinasse } 25791da177e4SLinus Torvalds 25804969c119SAndrea Arcangeli /* 258131c4a3d3SHugh Dickins * Make sure try_to_free_swap or reuse_swap_page or swapoff did not 258231c4a3d3SHugh Dickins * release the swapcache from under us. The page pin, and pte_same 258331c4a3d3SHugh Dickins * test below, are not enough to exclude that. Even if it is still 258431c4a3d3SHugh Dickins * swapcache, we need to check that the page's swap has not changed. 25854969c119SAndrea Arcangeli */ 258631c4a3d3SHugh Dickins if (unlikely(!PageSwapCache(page) || page_private(page) != entry.val)) 25874969c119SAndrea Arcangeli goto out_page; 25884969c119SAndrea Arcangeli 258982b0f8c3SJan Kara page = ksm_might_need_to_copy(page, vma, vmf->address); 25904969c119SAndrea Arcangeli if (unlikely(!page)) { 25915ad64688SHugh Dickins ret = VM_FAULT_OOM; 25924969c119SAndrea Arcangeli page = swapcache; 25934969c119SAndrea Arcangeli goto out_page; 25944969c119SAndrea Arcangeli } 25955ad64688SHugh Dickins 2596bae473a4SKirill A. Shutemov if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL, 2597bae473a4SKirill A. Shutemov &memcg, false)) { 2598073e587eSKAMEZAWA Hiroyuki ret = VM_FAULT_OOM; 2599bc43f75cSJohannes Weiner goto out_page; 2600073e587eSKAMEZAWA Hiroyuki } 2601073e587eSKAMEZAWA Hiroyuki 26021da177e4SLinus Torvalds /* 26038f4e2101SHugh Dickins * Back out if somebody else already faulted in this pte. 26041da177e4SLinus Torvalds */ 260582b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 260682b0f8c3SJan Kara &vmf->ptl); 260782b0f8c3SJan Kara if (unlikely(!pte_same(*vmf->pte, orig_pte))) 2608b8107480SKirill Korotaev goto out_nomap; 2609b8107480SKirill Korotaev 2610b8107480SKirill Korotaev if (unlikely(!PageUptodate(page))) { 2611b8107480SKirill Korotaev ret = VM_FAULT_SIGBUS; 2612b8107480SKirill Korotaev goto out_nomap; 26131da177e4SLinus Torvalds } 26141da177e4SLinus Torvalds 26158c7c6e34SKAMEZAWA Hiroyuki /* 26168c7c6e34SKAMEZAWA Hiroyuki * The page isn't present yet, go ahead with the fault. 26178c7c6e34SKAMEZAWA Hiroyuki * 26188c7c6e34SKAMEZAWA Hiroyuki * Be careful about the sequence of operations here. 26198c7c6e34SKAMEZAWA Hiroyuki * To get its accounting right, reuse_swap_page() must be called 26208c7c6e34SKAMEZAWA Hiroyuki * while the page is counted on swap but not yet in mapcount i.e. 26218c7c6e34SKAMEZAWA Hiroyuki * before page_add_anon_rmap() and swap_free(); try_to_free_swap() 26228c7c6e34SKAMEZAWA Hiroyuki * must be called after the swap_free(), or it will never succeed. 26238c7c6e34SKAMEZAWA Hiroyuki */ 26241da177e4SLinus Torvalds 2625bae473a4SKirill A. Shutemov inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); 2626bae473a4SKirill A. Shutemov dec_mm_counter_fast(vma->vm_mm, MM_SWAPENTS); 26271da177e4SLinus Torvalds pte = mk_pte(page, vma->vm_page_prot); 262882b0f8c3SJan Kara if ((vmf->flags & FAULT_FLAG_WRITE) && reuse_swap_page(page, NULL)) { 26291da177e4SLinus Torvalds pte = maybe_mkwrite(pte_mkdirty(pte), vma); 263082b0f8c3SJan Kara vmf->flags &= ~FAULT_FLAG_WRITE; 26319a5b489bSAndrea Arcangeli ret |= VM_FAULT_WRITE; 2632d281ee61SKirill A. Shutemov exclusive = RMAP_EXCLUSIVE; 26331da177e4SLinus Torvalds } 26341da177e4SLinus Torvalds flush_icache_page(vma, page); 2635179ef71cSCyrill Gorcunov if (pte_swp_soft_dirty(orig_pte)) 2636179ef71cSCyrill Gorcunov pte = pte_mksoft_dirty(pte); 263782b0f8c3SJan Kara set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte); 263800501b53SJohannes Weiner if (page == swapcache) { 263982b0f8c3SJan Kara do_page_add_anon_rmap(page, vma, vmf->address, exclusive); 2640f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, true, false); 26411a8018fbSMinchan Kim activate_page(page); 264200501b53SJohannes Weiner } else { /* ksm created a completely new copy */ 264382b0f8c3SJan Kara page_add_new_anon_rmap(page, vma, vmf->address, false); 2644f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, false); 264500501b53SJohannes Weiner lru_cache_add_active_or_unevictable(page, vma); 264600501b53SJohannes Weiner } 26471da177e4SLinus Torvalds 2648c475a8abSHugh Dickins swap_free(entry); 26495ccc5abaSVladimir Davydov if (mem_cgroup_swap_full(page) || 26505ccc5abaSVladimir Davydov (vma->vm_flags & VM_LOCKED) || PageMlocked(page)) 2651a2c43eedSHugh Dickins try_to_free_swap(page); 2652c475a8abSHugh Dickins unlock_page(page); 265356f31801SHugh Dickins if (page != swapcache) { 26544969c119SAndrea Arcangeli /* 26554969c119SAndrea Arcangeli * Hold the lock to avoid the swap entry to be reused 26564969c119SAndrea Arcangeli * until we take the PT lock for the pte_same() check 26574969c119SAndrea Arcangeli * (to avoid false positives from pte_same). For 26584969c119SAndrea Arcangeli * further safety release the lock after the swap_free 26594969c119SAndrea Arcangeli * so that the swap count won't change under a 26604969c119SAndrea Arcangeli * parallel locked swapcache. 26614969c119SAndrea Arcangeli */ 26624969c119SAndrea Arcangeli unlock_page(swapcache); 266309cbfeafSKirill A. Shutemov put_page(swapcache); 26644969c119SAndrea Arcangeli } 2665c475a8abSHugh Dickins 266682b0f8c3SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) { 266782b0f8c3SJan Kara ret |= do_wp_page(vmf, pte); 266861469f1dSHugh Dickins if (ret & VM_FAULT_ERROR) 266961469f1dSHugh Dickins ret &= VM_FAULT_ERROR; 26701da177e4SLinus Torvalds goto out; 26711da177e4SLinus Torvalds } 26721da177e4SLinus Torvalds 26731da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 267482b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 267565500d23SHugh Dickins unlock: 267682b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 26771da177e4SLinus Torvalds out: 26781da177e4SLinus Torvalds return ret; 2679b8107480SKirill Korotaev out_nomap: 2680f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 268182b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2682bc43f75cSJohannes Weiner out_page: 2683b8107480SKirill Korotaev unlock_page(page); 26844779cb31SAndi Kleen out_release: 268509cbfeafSKirill A. Shutemov put_page(page); 268656f31801SHugh Dickins if (page != swapcache) { 26874969c119SAndrea Arcangeli unlock_page(swapcache); 268809cbfeafSKirill A. Shutemov put_page(swapcache); 26894969c119SAndrea Arcangeli } 269065500d23SHugh Dickins return ret; 26911da177e4SLinus Torvalds } 26921da177e4SLinus Torvalds 26931da177e4SLinus Torvalds /* 26948ca3eb08SLuck, Tony * This is like a special single-page "expand_{down|up}wards()", 26958ca3eb08SLuck, Tony * except we must first make sure that 'address{-|+}PAGE_SIZE' 2696320b2b8dSLinus Torvalds * doesn't hit another vma. 2697320b2b8dSLinus Torvalds */ 2698320b2b8dSLinus Torvalds static inline int check_stack_guard_page(struct vm_area_struct *vma, unsigned long address) 2699320b2b8dSLinus Torvalds { 2700320b2b8dSLinus Torvalds address &= PAGE_MASK; 2701320b2b8dSLinus Torvalds if ((vma->vm_flags & VM_GROWSDOWN) && address == vma->vm_start) { 27020e8e50e2SLinus Torvalds struct vm_area_struct *prev = vma->vm_prev; 2703320b2b8dSLinus Torvalds 27040e8e50e2SLinus Torvalds /* 27050e8e50e2SLinus Torvalds * Is there a mapping abutting this one below? 27060e8e50e2SLinus Torvalds * 27070e8e50e2SLinus Torvalds * That's only ok if it's the same stack mapping 27080e8e50e2SLinus Torvalds * that has gotten split.. 27090e8e50e2SLinus Torvalds */ 27100e8e50e2SLinus Torvalds if (prev && prev->vm_end == address) 27110e8e50e2SLinus Torvalds return prev->vm_flags & VM_GROWSDOWN ? 0 : -ENOMEM; 27120e8e50e2SLinus Torvalds 2713fee7e49dSLinus Torvalds return expand_downwards(vma, address - PAGE_SIZE); 2714320b2b8dSLinus Torvalds } 27158ca3eb08SLuck, Tony if ((vma->vm_flags & VM_GROWSUP) && address + PAGE_SIZE == vma->vm_end) { 27168ca3eb08SLuck, Tony struct vm_area_struct *next = vma->vm_next; 27178ca3eb08SLuck, Tony 27188ca3eb08SLuck, Tony /* As VM_GROWSDOWN but s/below/above/ */ 27198ca3eb08SLuck, Tony if (next && next->vm_start == address + PAGE_SIZE) 27208ca3eb08SLuck, Tony return next->vm_flags & VM_GROWSUP ? 0 : -ENOMEM; 27218ca3eb08SLuck, Tony 2722fee7e49dSLinus Torvalds return expand_upwards(vma, address + PAGE_SIZE); 27238ca3eb08SLuck, Tony } 2724320b2b8dSLinus Torvalds return 0; 2725320b2b8dSLinus Torvalds } 2726320b2b8dSLinus Torvalds 2727320b2b8dSLinus Torvalds /* 27288f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 27298f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 27308f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 27311da177e4SLinus Torvalds */ 273282b0f8c3SJan Kara static int do_anonymous_page(struct vm_fault *vmf) 27331da177e4SLinus Torvalds { 273482b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 273500501b53SJohannes Weiner struct mem_cgroup *memcg; 27368f4e2101SHugh Dickins struct page *page; 27371da177e4SLinus Torvalds pte_t entry; 27381da177e4SLinus Torvalds 27396b7339f4SKirill A. Shutemov /* File mapping without ->vm_ops ? */ 27406b7339f4SKirill A. Shutemov if (vma->vm_flags & VM_SHARED) 27416b7339f4SKirill A. Shutemov return VM_FAULT_SIGBUS; 27426b7339f4SKirill A. Shutemov 274311ac5524SLinus Torvalds /* Check if we need to add a guard page to the stack */ 274482b0f8c3SJan Kara if (check_stack_guard_page(vma, vmf->address) < 0) 27459c145c56SLinus Torvalds return VM_FAULT_SIGSEGV; 274611ac5524SLinus Torvalds 27477267ec00SKirill A. Shutemov /* 27487267ec00SKirill A. Shutemov * Use pte_alloc() instead of pte_alloc_map(). We can't run 27497267ec00SKirill A. Shutemov * pte_offset_map() on pmds where a huge pmd might be created 27507267ec00SKirill A. Shutemov * from a different thread. 27517267ec00SKirill A. Shutemov * 27527267ec00SKirill A. Shutemov * pte_alloc_map() is safe to use under down_write(mmap_sem) or when 27537267ec00SKirill A. Shutemov * parallel threads are excluded by other means. 27547267ec00SKirill A. Shutemov * 27557267ec00SKirill A. Shutemov * Here we only have down_read(mmap_sem). 27567267ec00SKirill A. Shutemov */ 275782b0f8c3SJan Kara if (pte_alloc(vma->vm_mm, vmf->pmd, vmf->address)) 27587267ec00SKirill A. Shutemov return VM_FAULT_OOM; 27597267ec00SKirill A. Shutemov 27607267ec00SKirill A. Shutemov /* See the comment in pte_alloc_one_map() */ 276182b0f8c3SJan Kara if (unlikely(pmd_trans_unstable(vmf->pmd))) 27627267ec00SKirill A. Shutemov return 0; 27637267ec00SKirill A. Shutemov 276411ac5524SLinus Torvalds /* Use the zero-page for reads */ 276582b0f8c3SJan Kara if (!(vmf->flags & FAULT_FLAG_WRITE) && 2766bae473a4SKirill A. Shutemov !mm_forbids_zeropage(vma->vm_mm)) { 276782b0f8c3SJan Kara entry = pte_mkspecial(pfn_pte(my_zero_pfn(vmf->address), 276862eede62SHugh Dickins vma->vm_page_prot)); 276982b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 277082b0f8c3SJan Kara vmf->address, &vmf->ptl); 277182b0f8c3SJan Kara if (!pte_none(*vmf->pte)) 2772a13ea5b7SHugh Dickins goto unlock; 27736b251fc9SAndrea Arcangeli /* Deliver the page fault to userland, check inside PT lock */ 27746b251fc9SAndrea Arcangeli if (userfaultfd_missing(vma)) { 277582b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 277682b0f8c3SJan Kara return handle_userfault(vmf, VM_UFFD_MISSING); 27776b251fc9SAndrea Arcangeli } 2778a13ea5b7SHugh Dickins goto setpte; 2779a13ea5b7SHugh Dickins } 2780a13ea5b7SHugh Dickins 27811da177e4SLinus Torvalds /* Allocate our own private page. */ 27821da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 278365500d23SHugh Dickins goto oom; 278482b0f8c3SJan Kara page = alloc_zeroed_user_highpage_movable(vma, vmf->address); 27851da177e4SLinus Torvalds if (!page) 278665500d23SHugh Dickins goto oom; 2787eb3c24f3SMel Gorman 2788bae473a4SKirill A. Shutemov if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL, &memcg, false)) 2789eb3c24f3SMel Gorman goto oom_free_page; 2790eb3c24f3SMel Gorman 279152f37629SMinchan Kim /* 279252f37629SMinchan Kim * The memory barrier inside __SetPageUptodate makes sure that 279352f37629SMinchan Kim * preceeding stores to the page contents become visible before 279452f37629SMinchan Kim * the set_pte_at() write. 279552f37629SMinchan Kim */ 27960ed361deSNick Piggin __SetPageUptodate(page); 27971da177e4SLinus Torvalds 279865500d23SHugh Dickins entry = mk_pte(page, vma->vm_page_prot); 27991ac0cb5dSHugh Dickins if (vma->vm_flags & VM_WRITE) 28001ac0cb5dSHugh Dickins entry = pte_mkwrite(pte_mkdirty(entry)); 28018f4e2101SHugh Dickins 280282b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 280382b0f8c3SJan Kara &vmf->ptl); 280482b0f8c3SJan Kara if (!pte_none(*vmf->pte)) 28058f4e2101SHugh Dickins goto release; 28069ba69294SHugh Dickins 28076b251fc9SAndrea Arcangeli /* Deliver the page fault to userland, check inside PT lock */ 28086b251fc9SAndrea Arcangeli if (userfaultfd_missing(vma)) { 280982b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2810f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 281109cbfeafSKirill A. Shutemov put_page(page); 281282b0f8c3SJan Kara return handle_userfault(vmf, VM_UFFD_MISSING); 28136b251fc9SAndrea Arcangeli } 28146b251fc9SAndrea Arcangeli 2815bae473a4SKirill A. Shutemov inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); 281682b0f8c3SJan Kara page_add_new_anon_rmap(page, vma, vmf->address, false); 2817f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, false); 281800501b53SJohannes Weiner lru_cache_add_active_or_unevictable(page, vma); 2819a13ea5b7SHugh Dickins setpte: 282082b0f8c3SJan Kara set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry); 28211da177e4SLinus Torvalds 28221da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 282382b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 282465500d23SHugh Dickins unlock: 282582b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 282683c54070SNick Piggin return 0; 28278f4e2101SHugh Dickins release: 2828f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 282909cbfeafSKirill A. Shutemov put_page(page); 28308f4e2101SHugh Dickins goto unlock; 28318a9f3ccdSBalbir Singh oom_free_page: 283209cbfeafSKirill A. Shutemov put_page(page); 283365500d23SHugh Dickins oom: 28341da177e4SLinus Torvalds return VM_FAULT_OOM; 28351da177e4SLinus Torvalds } 28361da177e4SLinus Torvalds 28379a95f3cfSPaul Cassella /* 28389a95f3cfSPaul Cassella * The mmap_sem must have been held on entry, and may have been 28399a95f3cfSPaul Cassella * released depending on flags and vma->vm_ops->fault() return value. 28409a95f3cfSPaul Cassella * See filemap_fault() and __lock_page_retry(). 28419a95f3cfSPaul Cassella */ 2842936ca80dSJan Kara static int __do_fault(struct vm_fault *vmf) 28437eae74afSKirill A. Shutemov { 284482b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 28457eae74afSKirill A. Shutemov int ret; 28467eae74afSKirill A. Shutemov 2847667240e0SJan Kara ret = vma->vm_ops->fault(vma, vmf); 28487eae74afSKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 28497eae74afSKirill A. Shutemov return ret; 2850936ca80dSJan Kara if (ret & VM_FAULT_DAX_LOCKED) 2851bc2466e4SJan Kara return ret; 28527eae74afSKirill A. Shutemov 2853667240e0SJan Kara if (unlikely(PageHWPoison(vmf->page))) { 28547eae74afSKirill A. Shutemov if (ret & VM_FAULT_LOCKED) 2855667240e0SJan Kara unlock_page(vmf->page); 2856667240e0SJan Kara put_page(vmf->page); 2857936ca80dSJan Kara vmf->page = NULL; 28587eae74afSKirill A. Shutemov return VM_FAULT_HWPOISON; 28597eae74afSKirill A. Shutemov } 28607eae74afSKirill A. Shutemov 28617eae74afSKirill A. Shutemov if (unlikely(!(ret & VM_FAULT_LOCKED))) 2862667240e0SJan Kara lock_page(vmf->page); 28637eae74afSKirill A. Shutemov else 2864667240e0SJan Kara VM_BUG_ON_PAGE(!PageLocked(vmf->page), vmf->page); 28657eae74afSKirill A. Shutemov 28667eae74afSKirill A. Shutemov return ret; 28677eae74afSKirill A. Shutemov } 28687eae74afSKirill A. Shutemov 286982b0f8c3SJan Kara static int pte_alloc_one_map(struct vm_fault *vmf) 28707267ec00SKirill A. Shutemov { 287182b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 28727267ec00SKirill A. Shutemov 287382b0f8c3SJan Kara if (!pmd_none(*vmf->pmd)) 28747267ec00SKirill A. Shutemov goto map_pte; 287582b0f8c3SJan Kara if (vmf->prealloc_pte) { 287682b0f8c3SJan Kara vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); 287782b0f8c3SJan Kara if (unlikely(!pmd_none(*vmf->pmd))) { 287882b0f8c3SJan Kara spin_unlock(vmf->ptl); 28797267ec00SKirill A. Shutemov goto map_pte; 28807267ec00SKirill A. Shutemov } 28817267ec00SKirill A. Shutemov 28827267ec00SKirill A. Shutemov atomic_long_inc(&vma->vm_mm->nr_ptes); 288382b0f8c3SJan Kara pmd_populate(vma->vm_mm, vmf->pmd, vmf->prealloc_pte); 288482b0f8c3SJan Kara spin_unlock(vmf->ptl); 288582b0f8c3SJan Kara vmf->prealloc_pte = 0; 288682b0f8c3SJan Kara } else if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd, vmf->address))) { 28877267ec00SKirill A. Shutemov return VM_FAULT_OOM; 28887267ec00SKirill A. Shutemov } 28897267ec00SKirill A. Shutemov map_pte: 28907267ec00SKirill A. Shutemov /* 28917267ec00SKirill A. Shutemov * If a huge pmd materialized under us just retry later. Use 28927267ec00SKirill A. Shutemov * pmd_trans_unstable() instead of pmd_trans_huge() to ensure the pmd 28937267ec00SKirill A. Shutemov * didn't become pmd_trans_huge under us and then back to pmd_none, as 28947267ec00SKirill A. Shutemov * a result of MADV_DONTNEED running immediately after a huge pmd fault 28957267ec00SKirill A. Shutemov * in a different thread of this mm, in turn leading to a misleading 28967267ec00SKirill A. Shutemov * pmd_trans_huge() retval. All we have to ensure is that it is a 28977267ec00SKirill A. Shutemov * regular pmd that we can walk with pte_offset_map() and we can do that 28987267ec00SKirill A. Shutemov * through an atomic read in C, which is what pmd_trans_unstable() 28997267ec00SKirill A. Shutemov * provides. 29007267ec00SKirill A. Shutemov */ 290182b0f8c3SJan Kara if (pmd_trans_unstable(vmf->pmd) || pmd_devmap(*vmf->pmd)) 29027267ec00SKirill A. Shutemov return VM_FAULT_NOPAGE; 29037267ec00SKirill A. Shutemov 290482b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 290582b0f8c3SJan Kara &vmf->ptl); 29067267ec00SKirill A. Shutemov return 0; 29077267ec00SKirill A. Shutemov } 29087267ec00SKirill A. Shutemov 2909e496cf3dSKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 291010102459SKirill A. Shutemov 291110102459SKirill A. Shutemov #define HPAGE_CACHE_INDEX_MASK (HPAGE_PMD_NR - 1) 291210102459SKirill A. Shutemov static inline bool transhuge_vma_suitable(struct vm_area_struct *vma, 291310102459SKirill A. Shutemov unsigned long haddr) 291410102459SKirill A. Shutemov { 291510102459SKirill A. Shutemov if (((vma->vm_start >> PAGE_SHIFT) & HPAGE_CACHE_INDEX_MASK) != 291610102459SKirill A. Shutemov (vma->vm_pgoff & HPAGE_CACHE_INDEX_MASK)) 291710102459SKirill A. Shutemov return false; 291810102459SKirill A. Shutemov if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end) 291910102459SKirill A. Shutemov return false; 292010102459SKirill A. Shutemov return true; 292110102459SKirill A. Shutemov } 292210102459SKirill A. Shutemov 292382b0f8c3SJan Kara static void deposit_prealloc_pte(struct vm_fault *vmf) 2924953c66c2SAneesh Kumar K.V { 292582b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 2926953c66c2SAneesh Kumar K.V 292782b0f8c3SJan Kara pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, vmf->prealloc_pte); 2928953c66c2SAneesh Kumar K.V /* 2929953c66c2SAneesh Kumar K.V * We are going to consume the prealloc table, 2930953c66c2SAneesh Kumar K.V * count that as nr_ptes. 2931953c66c2SAneesh Kumar K.V */ 2932953c66c2SAneesh Kumar K.V atomic_long_inc(&vma->vm_mm->nr_ptes); 293382b0f8c3SJan Kara vmf->prealloc_pte = 0; 2934953c66c2SAneesh Kumar K.V } 2935953c66c2SAneesh Kumar K.V 293682b0f8c3SJan Kara static int do_set_pmd(struct vm_fault *vmf, struct page *page) 293710102459SKirill A. Shutemov { 293882b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 293982b0f8c3SJan Kara bool write = vmf->flags & FAULT_FLAG_WRITE; 294082b0f8c3SJan Kara unsigned long haddr = vmf->address & HPAGE_PMD_MASK; 294110102459SKirill A. Shutemov pmd_t entry; 294210102459SKirill A. Shutemov int i, ret; 294310102459SKirill A. Shutemov 294410102459SKirill A. Shutemov if (!transhuge_vma_suitable(vma, haddr)) 294510102459SKirill A. Shutemov return VM_FAULT_FALLBACK; 294610102459SKirill A. Shutemov 294710102459SKirill A. Shutemov ret = VM_FAULT_FALLBACK; 294810102459SKirill A. Shutemov page = compound_head(page); 294910102459SKirill A. Shutemov 2950953c66c2SAneesh Kumar K.V /* 2951953c66c2SAneesh Kumar K.V * Archs like ppc64 need additonal space to store information 2952953c66c2SAneesh Kumar K.V * related to pte entry. Use the preallocated table for that. 2953953c66c2SAneesh Kumar K.V */ 295482b0f8c3SJan Kara if (arch_needs_pgtable_deposit() && !vmf->prealloc_pte) { 295582b0f8c3SJan Kara vmf->prealloc_pte = pte_alloc_one(vma->vm_mm, vmf->address); 295682b0f8c3SJan Kara if (!vmf->prealloc_pte) 2957953c66c2SAneesh Kumar K.V return VM_FAULT_OOM; 2958953c66c2SAneesh Kumar K.V smp_wmb(); /* See comment in __pte_alloc() */ 2959953c66c2SAneesh Kumar K.V } 2960953c66c2SAneesh Kumar K.V 296182b0f8c3SJan Kara vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); 296282b0f8c3SJan Kara if (unlikely(!pmd_none(*vmf->pmd))) 296310102459SKirill A. Shutemov goto out; 296410102459SKirill A. Shutemov 296510102459SKirill A. Shutemov for (i = 0; i < HPAGE_PMD_NR; i++) 296610102459SKirill A. Shutemov flush_icache_page(vma, page + i); 296710102459SKirill A. Shutemov 296810102459SKirill A. Shutemov entry = mk_huge_pmd(page, vma->vm_page_prot); 296910102459SKirill A. Shutemov if (write) 297010102459SKirill A. Shutemov entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); 297110102459SKirill A. Shutemov 297210102459SKirill A. Shutemov add_mm_counter(vma->vm_mm, MM_FILEPAGES, HPAGE_PMD_NR); 297310102459SKirill A. Shutemov page_add_file_rmap(page, true); 2974953c66c2SAneesh Kumar K.V /* 2975953c66c2SAneesh Kumar K.V * deposit and withdraw with pmd lock held 2976953c66c2SAneesh Kumar K.V */ 2977953c66c2SAneesh Kumar K.V if (arch_needs_pgtable_deposit()) 297882b0f8c3SJan Kara deposit_prealloc_pte(vmf); 297910102459SKirill A. Shutemov 298082b0f8c3SJan Kara set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry); 298110102459SKirill A. Shutemov 298282b0f8c3SJan Kara update_mmu_cache_pmd(vma, haddr, vmf->pmd); 298310102459SKirill A. Shutemov 298410102459SKirill A. Shutemov /* fault is handled */ 298510102459SKirill A. Shutemov ret = 0; 298695ecedcdSKirill A. Shutemov count_vm_event(THP_FILE_MAPPED); 298710102459SKirill A. Shutemov out: 2988953c66c2SAneesh Kumar K.V /* 2989953c66c2SAneesh Kumar K.V * If we are going to fallback to pte mapping, do a 2990953c66c2SAneesh Kumar K.V * withdraw with pmd lock held. 2991953c66c2SAneesh Kumar K.V */ 2992953c66c2SAneesh Kumar K.V if (arch_needs_pgtable_deposit() && ret == VM_FAULT_FALLBACK) 299382b0f8c3SJan Kara vmf->prealloc_pte = pgtable_trans_huge_withdraw(vma->vm_mm, 299482b0f8c3SJan Kara vmf->pmd); 299582b0f8c3SJan Kara spin_unlock(vmf->ptl); 299610102459SKirill A. Shutemov return ret; 299710102459SKirill A. Shutemov } 299810102459SKirill A. Shutemov #else 299982b0f8c3SJan Kara static int do_set_pmd(struct vm_fault *vmf, struct page *page) 300010102459SKirill A. Shutemov { 300110102459SKirill A. Shutemov BUILD_BUG(); 300210102459SKirill A. Shutemov return 0; 300310102459SKirill A. Shutemov } 300410102459SKirill A. Shutemov #endif 300510102459SKirill A. Shutemov 30068c6e50b0SKirill A. Shutemov /** 30077267ec00SKirill A. Shutemov * alloc_set_pte - setup new PTE entry for given page and add reverse page 30087267ec00SKirill A. Shutemov * mapping. If needed, the fucntion allocates page table or use pre-allocated. 30098c6e50b0SKirill A. Shutemov * 301082b0f8c3SJan Kara * @vmf: fault environment 30117267ec00SKirill A. Shutemov * @memcg: memcg to charge page (only for private mappings) 30128c6e50b0SKirill A. Shutemov * @page: page to map 30138c6e50b0SKirill A. Shutemov * 301482b0f8c3SJan Kara * Caller must take care of unlocking vmf->ptl, if vmf->pte is non-NULL on 301582b0f8c3SJan Kara * return. 30168c6e50b0SKirill A. Shutemov * 30178c6e50b0SKirill A. Shutemov * Target users are page handler itself and implementations of 30188c6e50b0SKirill A. Shutemov * vm_ops->map_pages. 30198c6e50b0SKirill A. Shutemov */ 302082b0f8c3SJan Kara int alloc_set_pte(struct vm_fault *vmf, struct mem_cgroup *memcg, 30217267ec00SKirill A. Shutemov struct page *page) 30223bb97794SKirill A. Shutemov { 302382b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 302482b0f8c3SJan Kara bool write = vmf->flags & FAULT_FLAG_WRITE; 30253bb97794SKirill A. Shutemov pte_t entry; 302610102459SKirill A. Shutemov int ret; 302710102459SKirill A. Shutemov 302882b0f8c3SJan Kara if (pmd_none(*vmf->pmd) && PageTransCompound(page) && 3029e496cf3dSKirill A. Shutemov IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) { 303010102459SKirill A. Shutemov /* THP on COW? */ 303110102459SKirill A. Shutemov VM_BUG_ON_PAGE(memcg, page); 303210102459SKirill A. Shutemov 303382b0f8c3SJan Kara ret = do_set_pmd(vmf, page); 303410102459SKirill A. Shutemov if (ret != VM_FAULT_FALLBACK) 3035953c66c2SAneesh Kumar K.V goto fault_handled; 303610102459SKirill A. Shutemov } 30373bb97794SKirill A. Shutemov 303882b0f8c3SJan Kara if (!vmf->pte) { 303982b0f8c3SJan Kara ret = pte_alloc_one_map(vmf); 30407267ec00SKirill A. Shutemov if (ret) 3041953c66c2SAneesh Kumar K.V goto fault_handled; 30427267ec00SKirill A. Shutemov } 30437267ec00SKirill A. Shutemov 30447267ec00SKirill A. Shutemov /* Re-check under ptl */ 304582b0f8c3SJan Kara if (unlikely(!pte_none(*vmf->pte))) { 3046953c66c2SAneesh Kumar K.V ret = VM_FAULT_NOPAGE; 3047953c66c2SAneesh Kumar K.V goto fault_handled; 3048953c66c2SAneesh Kumar K.V } 30497267ec00SKirill A. Shutemov 30503bb97794SKirill A. Shutemov flush_icache_page(vma, page); 30513bb97794SKirill A. Shutemov entry = mk_pte(page, vma->vm_page_prot); 30523bb97794SKirill A. Shutemov if (write) 30533bb97794SKirill A. Shutemov entry = maybe_mkwrite(pte_mkdirty(entry), vma); 3054bae473a4SKirill A. Shutemov /* copy-on-write page */ 3055bae473a4SKirill A. Shutemov if (write && !(vma->vm_flags & VM_SHARED)) { 30563bb97794SKirill A. Shutemov inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); 305782b0f8c3SJan Kara page_add_new_anon_rmap(page, vma, vmf->address, false); 30587267ec00SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, false); 30597267ec00SKirill A. Shutemov lru_cache_add_active_or_unevictable(page, vma); 30603bb97794SKirill A. Shutemov } else { 3061eca56ff9SJerome Marchand inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page)); 3062dd78feddSKirill A. Shutemov page_add_file_rmap(page, false); 30633bb97794SKirill A. Shutemov } 306482b0f8c3SJan Kara set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry); 30653bb97794SKirill A. Shutemov 30663bb97794SKirill A. Shutemov /* no need to invalidate: a not-present page won't be cached */ 306782b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 3068953c66c2SAneesh Kumar K.V ret = 0; 30697267ec00SKirill A. Shutemov 3070953c66c2SAneesh Kumar K.V fault_handled: 3071953c66c2SAneesh Kumar K.V /* preallocated pagetable is unused: free it */ 307282b0f8c3SJan Kara if (vmf->prealloc_pte) { 307382b0f8c3SJan Kara pte_free(vmf->vma->vm_mm, vmf->prealloc_pte); 307482b0f8c3SJan Kara vmf->prealloc_pte = 0; 3075953c66c2SAneesh Kumar K.V } 3076953c66c2SAneesh Kumar K.V return ret; 30773bb97794SKirill A. Shutemov } 30783bb97794SKirill A. Shutemov 30793a91053aSKirill A. Shutemov static unsigned long fault_around_bytes __read_mostly = 30803a91053aSKirill A. Shutemov rounddown_pow_of_two(65536); 3081a9b0f861SKirill A. Shutemov 30821592eef0SKirill A. Shutemov #ifdef CONFIG_DEBUG_FS 3083a9b0f861SKirill A. Shutemov static int fault_around_bytes_get(void *data, u64 *val) 30841592eef0SKirill A. Shutemov { 3085a9b0f861SKirill A. Shutemov *val = fault_around_bytes; 30861592eef0SKirill A. Shutemov return 0; 30871592eef0SKirill A. Shutemov } 30881592eef0SKirill A. Shutemov 3089b4903d6eSAndrey Ryabinin /* 3090b4903d6eSAndrey Ryabinin * fault_around_pages() and fault_around_mask() expects fault_around_bytes 3091b4903d6eSAndrey Ryabinin * rounded down to nearest page order. It's what do_fault_around() expects to 3092b4903d6eSAndrey Ryabinin * see. 3093b4903d6eSAndrey Ryabinin */ 3094a9b0f861SKirill A. Shutemov static int fault_around_bytes_set(void *data, u64 val) 30951592eef0SKirill A. Shutemov { 3096a9b0f861SKirill A. Shutemov if (val / PAGE_SIZE > PTRS_PER_PTE) 30971592eef0SKirill A. Shutemov return -EINVAL; 3098b4903d6eSAndrey Ryabinin if (val > PAGE_SIZE) 3099b4903d6eSAndrey Ryabinin fault_around_bytes = rounddown_pow_of_two(val); 3100b4903d6eSAndrey Ryabinin else 3101b4903d6eSAndrey Ryabinin fault_around_bytes = PAGE_SIZE; /* rounddown_pow_of_two(0) is undefined */ 31021592eef0SKirill A. Shutemov return 0; 31031592eef0SKirill A. Shutemov } 3104a9b0f861SKirill A. Shutemov DEFINE_SIMPLE_ATTRIBUTE(fault_around_bytes_fops, 3105a9b0f861SKirill A. Shutemov fault_around_bytes_get, fault_around_bytes_set, "%llu\n"); 31061592eef0SKirill A. Shutemov 31071592eef0SKirill A. Shutemov static int __init fault_around_debugfs(void) 31081592eef0SKirill A. Shutemov { 31091592eef0SKirill A. Shutemov void *ret; 31101592eef0SKirill A. Shutemov 3111a9b0f861SKirill A. Shutemov ret = debugfs_create_file("fault_around_bytes", 0644, NULL, NULL, 3112a9b0f861SKirill A. Shutemov &fault_around_bytes_fops); 31131592eef0SKirill A. Shutemov if (!ret) 3114a9b0f861SKirill A. Shutemov pr_warn("Failed to create fault_around_bytes in debugfs"); 31151592eef0SKirill A. Shutemov return 0; 31161592eef0SKirill A. Shutemov } 31171592eef0SKirill A. Shutemov late_initcall(fault_around_debugfs); 31181592eef0SKirill A. Shutemov #endif 31198c6e50b0SKirill A. Shutemov 31201fdb412bSKirill A. Shutemov /* 31211fdb412bSKirill A. Shutemov * do_fault_around() tries to map few pages around the fault address. The hope 31221fdb412bSKirill A. Shutemov * is that the pages will be needed soon and this will lower the number of 31231fdb412bSKirill A. Shutemov * faults to handle. 31241fdb412bSKirill A. Shutemov * 31251fdb412bSKirill A. Shutemov * It uses vm_ops->map_pages() to map the pages, which skips the page if it's 31261fdb412bSKirill A. Shutemov * not ready to be mapped: not up-to-date, locked, etc. 31271fdb412bSKirill A. Shutemov * 31281fdb412bSKirill A. Shutemov * This function is called with the page table lock taken. In the split ptlock 31291fdb412bSKirill A. Shutemov * case the page table lock only protects only those entries which belong to 31301fdb412bSKirill A. Shutemov * the page table corresponding to the fault address. 31311fdb412bSKirill A. Shutemov * 31321fdb412bSKirill A. Shutemov * This function doesn't cross the VMA boundaries, in order to call map_pages() 31331fdb412bSKirill A. Shutemov * only once. 31341fdb412bSKirill A. Shutemov * 31351fdb412bSKirill A. Shutemov * fault_around_pages() defines how many pages we'll try to map. 31361fdb412bSKirill A. Shutemov * do_fault_around() expects it to return a power of two less than or equal to 31371fdb412bSKirill A. Shutemov * PTRS_PER_PTE. 31381fdb412bSKirill A. Shutemov * 31391fdb412bSKirill A. Shutemov * The virtual address of the area that we map is naturally aligned to the 31401fdb412bSKirill A. Shutemov * fault_around_pages() value (and therefore to page order). This way it's 31411fdb412bSKirill A. Shutemov * easier to guarantee that we don't cross page table boundaries. 31421fdb412bSKirill A. Shutemov */ 31430721ec8bSJan Kara static int do_fault_around(struct vm_fault *vmf) 31448c6e50b0SKirill A. Shutemov { 314582b0f8c3SJan Kara unsigned long address = vmf->address, nr_pages, mask; 31460721ec8bSJan Kara pgoff_t start_pgoff = vmf->pgoff; 3147bae473a4SKirill A. Shutemov pgoff_t end_pgoff; 31487267ec00SKirill A. Shutemov int off, ret = 0; 31498c6e50b0SKirill A. Shutemov 31504db0c3c2SJason Low nr_pages = READ_ONCE(fault_around_bytes) >> PAGE_SHIFT; 3151aecd6f44SKirill A. Shutemov mask = ~(nr_pages * PAGE_SIZE - 1) & PAGE_MASK; 3152aecd6f44SKirill A. Shutemov 315382b0f8c3SJan Kara vmf->address = max(address & mask, vmf->vma->vm_start); 315482b0f8c3SJan Kara off = ((address - vmf->address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1); 3155bae473a4SKirill A. Shutemov start_pgoff -= off; 31568c6e50b0SKirill A. Shutemov 31578c6e50b0SKirill A. Shutemov /* 3158bae473a4SKirill A. Shutemov * end_pgoff is either end of page table or end of vma 3159bae473a4SKirill A. Shutemov * or fault_around_pages() from start_pgoff, depending what is nearest. 31608c6e50b0SKirill A. Shutemov */ 3161bae473a4SKirill A. Shutemov end_pgoff = start_pgoff - 316282b0f8c3SJan Kara ((vmf->address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) + 31638c6e50b0SKirill A. Shutemov PTRS_PER_PTE - 1; 316482b0f8c3SJan Kara end_pgoff = min3(end_pgoff, vma_pages(vmf->vma) + vmf->vma->vm_pgoff - 1, 3165bae473a4SKirill A. Shutemov start_pgoff + nr_pages - 1); 31668c6e50b0SKirill A. Shutemov 316782b0f8c3SJan Kara if (pmd_none(*vmf->pmd)) { 316882b0f8c3SJan Kara vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm, 316982b0f8c3SJan Kara vmf->address); 317082b0f8c3SJan Kara if (!vmf->prealloc_pte) 3171c5f88bd2SVegard Nossum goto out; 31727267ec00SKirill A. Shutemov smp_wmb(); /* See comment in __pte_alloc() */ 31738c6e50b0SKirill A. Shutemov } 31748c6e50b0SKirill A. Shutemov 317582b0f8c3SJan Kara vmf->vma->vm_ops->map_pages(vmf, start_pgoff, end_pgoff); 31767267ec00SKirill A. Shutemov 31777267ec00SKirill A. Shutemov /* Huge page is mapped? Page fault is solved */ 317882b0f8c3SJan Kara if (pmd_trans_huge(*vmf->pmd)) { 31797267ec00SKirill A. Shutemov ret = VM_FAULT_NOPAGE; 31807267ec00SKirill A. Shutemov goto out; 31818c6e50b0SKirill A. Shutemov } 31828c6e50b0SKirill A. Shutemov 31837267ec00SKirill A. Shutemov /* ->map_pages() haven't done anything useful. Cold page cache? */ 318482b0f8c3SJan Kara if (!vmf->pte) 31857267ec00SKirill A. Shutemov goto out; 31867267ec00SKirill A. Shutemov 31877267ec00SKirill A. Shutemov /* check if the page fault is solved */ 318882b0f8c3SJan Kara vmf->pte -= (vmf->address >> PAGE_SHIFT) - (address >> PAGE_SHIFT); 318982b0f8c3SJan Kara if (!pte_none(*vmf->pte)) 31907267ec00SKirill A. Shutemov ret = VM_FAULT_NOPAGE; 319182b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 31927267ec00SKirill A. Shutemov out: 319382b0f8c3SJan Kara vmf->address = address; 319482b0f8c3SJan Kara vmf->pte = NULL; 31957267ec00SKirill A. Shutemov return ret; 31967267ec00SKirill A. Shutemov } 31977267ec00SKirill A. Shutemov 31980721ec8bSJan Kara static int do_read_fault(struct vm_fault *vmf) 3199e655fb29SKirill A. Shutemov { 320082b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 32018c6e50b0SKirill A. Shutemov int ret = 0; 32028c6e50b0SKirill A. Shutemov 32038c6e50b0SKirill A. Shutemov /* 32048c6e50b0SKirill A. Shutemov * Let's call ->map_pages() first and use ->fault() as fallback 32058c6e50b0SKirill A. Shutemov * if page by the offset is not ready to be mapped (cold cache or 32068c6e50b0SKirill A. Shutemov * something). 32078c6e50b0SKirill A. Shutemov */ 32089b4bdd2fSKirill A. Shutemov if (vma->vm_ops->map_pages && fault_around_bytes >> PAGE_SHIFT > 1) { 32090721ec8bSJan Kara ret = do_fault_around(vmf); 32107267ec00SKirill A. Shutemov if (ret) 32117267ec00SKirill A. Shutemov return ret; 32128c6e50b0SKirill A. Shutemov } 3213e655fb29SKirill A. Shutemov 3214936ca80dSJan Kara ret = __do_fault(vmf); 3215e655fb29SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3216e655fb29SKirill A. Shutemov return ret; 3217e655fb29SKirill A. Shutemov 3218936ca80dSJan Kara ret |= alloc_set_pte(vmf, NULL, vmf->page); 321982b0f8c3SJan Kara if (vmf->pte) 322082b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3221936ca80dSJan Kara unlock_page(vmf->page); 32227267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3223936ca80dSJan Kara put_page(vmf->page); 3224e655fb29SKirill A. Shutemov return ret; 3225e655fb29SKirill A. Shutemov } 3226e655fb29SKirill A. Shutemov 32270721ec8bSJan Kara static int do_cow_fault(struct vm_fault *vmf) 3228ec47c3b9SKirill A. Shutemov { 322982b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 323000501b53SJohannes Weiner struct mem_cgroup *memcg; 3231ec47c3b9SKirill A. Shutemov int ret; 3232ec47c3b9SKirill A. Shutemov 3233ec47c3b9SKirill A. Shutemov if (unlikely(anon_vma_prepare(vma))) 3234ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 3235ec47c3b9SKirill A. Shutemov 3236936ca80dSJan Kara vmf->cow_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vmf->address); 3237936ca80dSJan Kara if (!vmf->cow_page) 3238ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 3239ec47c3b9SKirill A. Shutemov 3240936ca80dSJan Kara if (mem_cgroup_try_charge(vmf->cow_page, vma->vm_mm, GFP_KERNEL, 3241bae473a4SKirill A. Shutemov &memcg, false)) { 3242936ca80dSJan Kara put_page(vmf->cow_page); 3243ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 3244ec47c3b9SKirill A. Shutemov } 3245ec47c3b9SKirill A. Shutemov 3246936ca80dSJan Kara ret = __do_fault(vmf); 3247ec47c3b9SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3248ec47c3b9SKirill A. Shutemov goto uncharge_out; 3249ec47c3b9SKirill A. Shutemov 3250bc2466e4SJan Kara if (!(ret & VM_FAULT_DAX_LOCKED)) 3251936ca80dSJan Kara copy_user_highpage(vmf->cow_page, vmf->page, vmf->address, vma); 3252936ca80dSJan Kara __SetPageUptodate(vmf->cow_page); 3253ec47c3b9SKirill A. Shutemov 3254936ca80dSJan Kara ret |= alloc_set_pte(vmf, memcg, vmf->cow_page); 325582b0f8c3SJan Kara if (vmf->pte) 325682b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3257bc2466e4SJan Kara if (!(ret & VM_FAULT_DAX_LOCKED)) { 3258936ca80dSJan Kara unlock_page(vmf->page); 3259936ca80dSJan Kara put_page(vmf->page); 32602e4cdab0SMatthew Wilcox } else { 32610721ec8bSJan Kara dax_unlock_mapping_entry(vma->vm_file->f_mapping, vmf->pgoff); 32622e4cdab0SMatthew Wilcox } 32637267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 32647267ec00SKirill A. Shutemov goto uncharge_out; 3265ec47c3b9SKirill A. Shutemov return ret; 3266ec47c3b9SKirill A. Shutemov uncharge_out: 3267936ca80dSJan Kara mem_cgroup_cancel_charge(vmf->cow_page, memcg, false); 3268936ca80dSJan Kara put_page(vmf->cow_page); 3269ec47c3b9SKirill A. Shutemov return ret; 3270ec47c3b9SKirill A. Shutemov } 3271ec47c3b9SKirill A. Shutemov 32720721ec8bSJan Kara static int do_shared_fault(struct vm_fault *vmf) 32731da177e4SLinus Torvalds { 327482b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3275f0c6d4d2SKirill A. Shutemov struct address_space *mapping; 3276f0c6d4d2SKirill A. Shutemov int dirtied = 0; 3277f0c6d4d2SKirill A. Shutemov int ret, tmp; 32781d65f86dSKAMEZAWA Hiroyuki 3279936ca80dSJan Kara ret = __do_fault(vmf); 32807eae74afSKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3281f0c6d4d2SKirill A. Shutemov return ret; 32821da177e4SLinus Torvalds 32831da177e4SLinus Torvalds /* 3284f0c6d4d2SKirill A. Shutemov * Check if the backing address space wants to know that the page is 3285f0c6d4d2SKirill A. Shutemov * about to become writable 32861da177e4SLinus Torvalds */ 3287fb09a464SKirill A. Shutemov if (vma->vm_ops->page_mkwrite) { 3288936ca80dSJan Kara unlock_page(vmf->page); 3289936ca80dSJan Kara tmp = do_page_mkwrite(vma, vmf->page, vmf->address); 3290fb09a464SKirill A. Shutemov if (unlikely(!tmp || 3291fb09a464SKirill A. Shutemov (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { 3292936ca80dSJan Kara put_page(vmf->page); 3293f0c6d4d2SKirill A. Shutemov return tmp; 329469676147SMark Fasheh } 3295d0217ac0SNick Piggin } 3296fb09a464SKirill A. Shutemov 3297936ca80dSJan Kara ret |= alloc_set_pte(vmf, NULL, vmf->page); 329882b0f8c3SJan Kara if (vmf->pte) 329982b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 33007267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | 33017267ec00SKirill A. Shutemov VM_FAULT_RETRY))) { 3302936ca80dSJan Kara unlock_page(vmf->page); 3303936ca80dSJan Kara put_page(vmf->page); 3304f0c6d4d2SKirill A. Shutemov return ret; 33059637a5efSDavid Howells } 3306d00806b1SNick Piggin 3307936ca80dSJan Kara if (set_page_dirty(vmf->page)) 330841c4d25fSJan Kara dirtied = 1; 3309d82fa87dSAndrew Morton /* 3310d82fa87dSAndrew Morton * Take a local copy of the address_space - page.mapping may be zeroed 3311d82fa87dSAndrew Morton * by truncate after unlock_page(). The address_space itself remains 3312d82fa87dSAndrew Morton * pinned by vma->vm_file's reference. We rely on unlock_page()'s 3313d82fa87dSAndrew Morton * release semantics to prevent the compiler from undoing this copying. 3314d82fa87dSAndrew Morton */ 3315936ca80dSJan Kara mapping = page_rmapping(vmf->page); 3316936ca80dSJan Kara unlock_page(vmf->page); 3317f0c6d4d2SKirill A. Shutemov if ((dirtied || vma->vm_ops->page_mkwrite) && mapping) { 3318b827e496SNick Piggin /* 3319b827e496SNick Piggin * Some device drivers do not set page.mapping but still 3320b827e496SNick Piggin * dirty their pages 3321b827e496SNick Piggin */ 3322b827e496SNick Piggin balance_dirty_pages_ratelimited(mapping); 3323d08b3851SPeter Zijlstra } 3324d00806b1SNick Piggin 332574ec6751SJohannes Weiner if (!vma->vm_ops->page_mkwrite) 3326b827e496SNick Piggin file_update_time(vma->vm_file); 3327b827e496SNick Piggin 3328b827e496SNick Piggin return ret; 332954cb8821SNick Piggin } 3330d00806b1SNick Piggin 33319a95f3cfSPaul Cassella /* 33329a95f3cfSPaul Cassella * We enter with non-exclusive mmap_sem (to exclude vma changes, 33339a95f3cfSPaul Cassella * but allow concurrent faults). 33349a95f3cfSPaul Cassella * The mmap_sem may have been released depending on flags and our 33359a95f3cfSPaul Cassella * return value. See filemap_fault() and __lock_page_or_retry(). 33369a95f3cfSPaul Cassella */ 333782b0f8c3SJan Kara static int do_fault(struct vm_fault *vmf) 333854cb8821SNick Piggin { 333982b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 334054cb8821SNick Piggin 33416b7339f4SKirill A. Shutemov /* The VMA was not fully populated on mmap() or missing VM_DONTEXPAND */ 33426b7339f4SKirill A. Shutemov if (!vma->vm_ops->fault) 33436b7339f4SKirill A. Shutemov return VM_FAULT_SIGBUS; 334482b0f8c3SJan Kara if (!(vmf->flags & FAULT_FLAG_WRITE)) 33450721ec8bSJan Kara return do_read_fault(vmf); 3346ec47c3b9SKirill A. Shutemov if (!(vma->vm_flags & VM_SHARED)) 33470721ec8bSJan Kara return do_cow_fault(vmf); 33480721ec8bSJan Kara return do_shared_fault(vmf); 334954cb8821SNick Piggin } 335054cb8821SNick Piggin 3351b19a9939SRashika Kheria static int numa_migrate_prep(struct page *page, struct vm_area_struct *vma, 335204bb2f94SRik van Riel unsigned long addr, int page_nid, 335304bb2f94SRik van Riel int *flags) 33549532fec1SMel Gorman { 33559532fec1SMel Gorman get_page(page); 33569532fec1SMel Gorman 33579532fec1SMel Gorman count_vm_numa_event(NUMA_HINT_FAULTS); 335804bb2f94SRik van Riel if (page_nid == numa_node_id()) { 33599532fec1SMel Gorman count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL); 336004bb2f94SRik van Riel *flags |= TNF_FAULT_LOCAL; 336104bb2f94SRik van Riel } 33629532fec1SMel Gorman 33639532fec1SMel Gorman return mpol_misplaced(page, vma, addr); 33649532fec1SMel Gorman } 33659532fec1SMel Gorman 336682b0f8c3SJan Kara static int do_numa_page(struct vm_fault *vmf, pte_t pte) 3367d10e63f2SMel Gorman { 336882b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 33694daae3b4SMel Gorman struct page *page = NULL; 33708191acbdSMel Gorman int page_nid = -1; 337190572890SPeter Zijlstra int last_cpupid; 3372cbee9f88SPeter Zijlstra int target_nid; 3373b8593bfdSMel Gorman bool migrated = false; 3374b191f9b1SMel Gorman bool was_writable = pte_write(pte); 33756688cc05SPeter Zijlstra int flags = 0; 3376d10e63f2SMel Gorman 3377d10e63f2SMel Gorman /* 3378d10e63f2SMel Gorman * The "pte" at this point cannot be used safely without 3379d10e63f2SMel Gorman * validation through pte_unmap_same(). It's of NUMA type but 3380d10e63f2SMel Gorman * the pfn may be screwed if the read is non atomic. 3381d10e63f2SMel Gorman * 33824d942466SMel Gorman * We can safely just do a "set_pte_at()", because the old 33834d942466SMel Gorman * page table entry is not accessible, so there would be no 33844d942466SMel Gorman * concurrent hardware modifications to the PTE. 3385d10e63f2SMel Gorman */ 338682b0f8c3SJan Kara vmf->ptl = pte_lockptr(vma->vm_mm, vmf->pmd); 338782b0f8c3SJan Kara spin_lock(vmf->ptl); 338882b0f8c3SJan Kara if (unlikely(!pte_same(*vmf->pte, pte))) { 338982b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 33904daae3b4SMel Gorman goto out; 33914daae3b4SMel Gorman } 33924daae3b4SMel Gorman 33934d942466SMel Gorman /* Make it present again */ 33944d942466SMel Gorman pte = pte_modify(pte, vma->vm_page_prot); 33954d942466SMel Gorman pte = pte_mkyoung(pte); 3396b191f9b1SMel Gorman if (was_writable) 3397b191f9b1SMel Gorman pte = pte_mkwrite(pte); 339882b0f8c3SJan Kara set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte); 339982b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 3400d10e63f2SMel Gorman 340182b0f8c3SJan Kara page = vm_normal_page(vma, vmf->address, pte); 3402d10e63f2SMel Gorman if (!page) { 340382b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3404d10e63f2SMel Gorman return 0; 3405d10e63f2SMel Gorman } 3406d10e63f2SMel Gorman 3407e81c4802SKirill A. Shutemov /* TODO: handle PTE-mapped THP */ 3408e81c4802SKirill A. Shutemov if (PageCompound(page)) { 340982b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3410e81c4802SKirill A. Shutemov return 0; 3411e81c4802SKirill A. Shutemov } 3412e81c4802SKirill A. Shutemov 34136688cc05SPeter Zijlstra /* 3414bea66fbdSMel Gorman * Avoid grouping on RO pages in general. RO pages shouldn't hurt as 3415bea66fbdSMel Gorman * much anyway since they can be in shared cache state. This misses 3416bea66fbdSMel Gorman * the case where a mapping is writable but the process never writes 3417bea66fbdSMel Gorman * to it but pte_write gets cleared during protection updates and 3418bea66fbdSMel Gorman * pte_dirty has unpredictable behaviour between PTE scan updates, 3419bea66fbdSMel Gorman * background writeback, dirty balancing and application behaviour. 34206688cc05SPeter Zijlstra */ 3421d59dc7bcSRik van Riel if (!pte_write(pte)) 34226688cc05SPeter Zijlstra flags |= TNF_NO_GROUP; 34236688cc05SPeter Zijlstra 3424dabe1d99SRik van Riel /* 3425dabe1d99SRik van Riel * Flag if the page is shared between multiple address spaces. This 3426dabe1d99SRik van Riel * is later used when determining whether to group tasks together 3427dabe1d99SRik van Riel */ 3428dabe1d99SRik van Riel if (page_mapcount(page) > 1 && (vma->vm_flags & VM_SHARED)) 3429dabe1d99SRik van Riel flags |= TNF_SHARED; 3430dabe1d99SRik van Riel 343190572890SPeter Zijlstra last_cpupid = page_cpupid_last(page); 34328191acbdSMel Gorman page_nid = page_to_nid(page); 343382b0f8c3SJan Kara target_nid = numa_migrate_prep(page, vma, vmf->address, page_nid, 3434bae473a4SKirill A. Shutemov &flags); 343582b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 34364daae3b4SMel Gorman if (target_nid == -1) { 34374daae3b4SMel Gorman put_page(page); 34384daae3b4SMel Gorman goto out; 34394daae3b4SMel Gorman } 34404daae3b4SMel Gorman 34414daae3b4SMel Gorman /* Migrate to the requested node */ 34421bc115d8SMel Gorman migrated = migrate_misplaced_page(page, vma, target_nid); 34436688cc05SPeter Zijlstra if (migrated) { 34448191acbdSMel Gorman page_nid = target_nid; 34456688cc05SPeter Zijlstra flags |= TNF_MIGRATED; 3446074c2381SMel Gorman } else 3447074c2381SMel Gorman flags |= TNF_MIGRATE_FAIL; 34484daae3b4SMel Gorman 34494daae3b4SMel Gorman out: 34508191acbdSMel Gorman if (page_nid != -1) 34516688cc05SPeter Zijlstra task_numa_fault(last_cpupid, page_nid, 1, flags); 3452d10e63f2SMel Gorman return 0; 3453d10e63f2SMel Gorman } 3454d10e63f2SMel Gorman 345582b0f8c3SJan Kara static int create_huge_pmd(struct vm_fault *vmf) 3456b96375f7SMatthew Wilcox { 345782b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3458fb6dd5faSKirill A. Shutemov if (vma_is_anonymous(vma)) 345982b0f8c3SJan Kara return do_huge_pmd_anonymous_page(vmf); 3460b96375f7SMatthew Wilcox if (vma->vm_ops->pmd_fault) 346182b0f8c3SJan Kara return vma->vm_ops->pmd_fault(vma, vmf->address, vmf->pmd, 346282b0f8c3SJan Kara vmf->flags); 3463b96375f7SMatthew Wilcox return VM_FAULT_FALLBACK; 3464b96375f7SMatthew Wilcox } 3465b96375f7SMatthew Wilcox 346682b0f8c3SJan Kara static int wp_huge_pmd(struct vm_fault *vmf, pmd_t orig_pmd) 3467b96375f7SMatthew Wilcox { 346882b0f8c3SJan Kara if (vma_is_anonymous(vmf->vma)) 346982b0f8c3SJan Kara return do_huge_pmd_wp_page(vmf, orig_pmd); 347082b0f8c3SJan Kara if (vmf->vma->vm_ops->pmd_fault) 347182b0f8c3SJan Kara return vmf->vma->vm_ops->pmd_fault(vmf->vma, vmf->address, 347282b0f8c3SJan Kara vmf->pmd, vmf->flags); 3473af9e4d5fSKirill A. Shutemov 3474af9e4d5fSKirill A. Shutemov /* COW handled on pte level: split pmd */ 347582b0f8c3SJan Kara VM_BUG_ON_VMA(vmf->vma->vm_flags & VM_SHARED, vmf->vma); 347682b0f8c3SJan Kara __split_huge_pmd(vmf->vma, vmf->pmd, vmf->address, false, NULL); 3477af9e4d5fSKirill A. Shutemov 3478b96375f7SMatthew Wilcox return VM_FAULT_FALLBACK; 3479b96375f7SMatthew Wilcox } 3480b96375f7SMatthew Wilcox 348138e08854SLorenzo Stoakes static inline bool vma_is_accessible(struct vm_area_struct *vma) 348238e08854SLorenzo Stoakes { 348338e08854SLorenzo Stoakes return vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE); 348438e08854SLorenzo Stoakes } 348538e08854SLorenzo Stoakes 34861da177e4SLinus Torvalds /* 34871da177e4SLinus Torvalds * These routines also need to handle stuff like marking pages dirty 34881da177e4SLinus Torvalds * and/or accessed for architectures that don't do it in hardware (most 34891da177e4SLinus Torvalds * RISC architectures). The early dirtying is also good on the i386. 34901da177e4SLinus Torvalds * 34911da177e4SLinus Torvalds * There is also a hook called "update_mmu_cache()" that architectures 34921da177e4SLinus Torvalds * with external mmu caches can use to update those (ie the Sparc or 34931da177e4SLinus Torvalds * PowerPC hashed page tables that act as extended TLBs). 34941da177e4SLinus Torvalds * 34957267ec00SKirill A. Shutemov * We enter with non-exclusive mmap_sem (to exclude vma changes, but allow 34967267ec00SKirill A. Shutemov * concurrent faults). 34979a95f3cfSPaul Cassella * 34987267ec00SKirill A. Shutemov * The mmap_sem may have been released depending on flags and our return value. 34997267ec00SKirill A. Shutemov * See filemap_fault() and __lock_page_or_retry(). 35001da177e4SLinus Torvalds */ 350182b0f8c3SJan Kara static int handle_pte_fault(struct vm_fault *vmf) 35021da177e4SLinus Torvalds { 35031da177e4SLinus Torvalds pte_t entry; 35041da177e4SLinus Torvalds 350582b0f8c3SJan Kara if (unlikely(pmd_none(*vmf->pmd))) { 35067267ec00SKirill A. Shutemov /* 35077267ec00SKirill A. Shutemov * Leave __pte_alloc() until later: because vm_ops->fault may 35087267ec00SKirill A. Shutemov * want to allocate huge page, and if we expose page table 35097267ec00SKirill A. Shutemov * for an instant, it will be difficult to retract from 35107267ec00SKirill A. Shutemov * concurrent faults and from rmap lookups. 35117267ec00SKirill A. Shutemov */ 351282b0f8c3SJan Kara vmf->pte = NULL; 35137267ec00SKirill A. Shutemov } else { 35147267ec00SKirill A. Shutemov /* See comment in pte_alloc_one_map() */ 351582b0f8c3SJan Kara if (pmd_trans_unstable(vmf->pmd) || pmd_devmap(*vmf->pmd)) 35167267ec00SKirill A. Shutemov return 0; 35177267ec00SKirill A. Shutemov /* 35187267ec00SKirill A. Shutemov * A regular pmd is established and it can't morph into a huge 35197267ec00SKirill A. Shutemov * pmd from under us anymore at this point because we hold the 35207267ec00SKirill A. Shutemov * mmap_sem read mode and khugepaged takes it in write mode. 35217267ec00SKirill A. Shutemov * So now it's safe to run pte_offset_map(). 35227267ec00SKirill A. Shutemov */ 352382b0f8c3SJan Kara vmf->pte = pte_offset_map(vmf->pmd, vmf->address); 35247267ec00SKirill A. Shutemov 352582b0f8c3SJan Kara entry = *vmf->pte; 35267267ec00SKirill A. Shutemov 3527e37c6982SChristian Borntraeger /* 3528e37c6982SChristian Borntraeger * some architectures can have larger ptes than wordsize, 35297267ec00SKirill A. Shutemov * e.g.ppc44x-defconfig has CONFIG_PTE_64BIT=y and 35307267ec00SKirill A. Shutemov * CONFIG_32BIT=y, so READ_ONCE or ACCESS_ONCE cannot guarantee 35317267ec00SKirill A. Shutemov * atomic accesses. The code below just needs a consistent 35327267ec00SKirill A. Shutemov * view for the ifs and we later double check anyway with the 35337267ec00SKirill A. Shutemov * ptl lock held. So here a barrier will do. 3534e37c6982SChristian Borntraeger */ 3535e37c6982SChristian Borntraeger barrier(); 353665500d23SHugh Dickins if (pte_none(entry)) { 353782b0f8c3SJan Kara pte_unmap(vmf->pte); 353882b0f8c3SJan Kara vmf->pte = NULL; 35397267ec00SKirill A. Shutemov } 35407267ec00SKirill A. Shutemov } 35417267ec00SKirill A. Shutemov 354282b0f8c3SJan Kara if (!vmf->pte) { 354382b0f8c3SJan Kara if (vma_is_anonymous(vmf->vma)) 354482b0f8c3SJan Kara return do_anonymous_page(vmf); 3545b5330628SOleg Nesterov else 354682b0f8c3SJan Kara return do_fault(vmf); 354765500d23SHugh Dickins } 35487267ec00SKirill A. Shutemov 35497267ec00SKirill A. Shutemov if (!pte_present(entry)) 355082b0f8c3SJan Kara return do_swap_page(vmf, entry); 35511da177e4SLinus Torvalds 355282b0f8c3SJan Kara if (pte_protnone(entry) && vma_is_accessible(vmf->vma)) 355382b0f8c3SJan Kara return do_numa_page(vmf, entry); 3554d10e63f2SMel Gorman 355582b0f8c3SJan Kara vmf->ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd); 355682b0f8c3SJan Kara spin_lock(vmf->ptl); 355782b0f8c3SJan Kara if (unlikely(!pte_same(*vmf->pte, entry))) 35588f4e2101SHugh Dickins goto unlock; 355982b0f8c3SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) { 35601da177e4SLinus Torvalds if (!pte_write(entry)) 356182b0f8c3SJan Kara return do_wp_page(vmf, entry); 35621da177e4SLinus Torvalds entry = pte_mkdirty(entry); 35631da177e4SLinus Torvalds } 35641da177e4SLinus Torvalds entry = pte_mkyoung(entry); 356582b0f8c3SJan Kara if (ptep_set_access_flags(vmf->vma, vmf->address, vmf->pte, entry, 356682b0f8c3SJan Kara vmf->flags & FAULT_FLAG_WRITE)) { 356782b0f8c3SJan Kara update_mmu_cache(vmf->vma, vmf->address, vmf->pte); 35681a44e149SAndrea Arcangeli } else { 35691a44e149SAndrea Arcangeli /* 35701a44e149SAndrea Arcangeli * This is needed only for protection faults but the arch code 35711a44e149SAndrea Arcangeli * is not yet telling us if this is a protection fault or not. 35721a44e149SAndrea Arcangeli * This still avoids useless tlb flushes for .text page faults 35731a44e149SAndrea Arcangeli * with threads. 35741a44e149SAndrea Arcangeli */ 357582b0f8c3SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) 357682b0f8c3SJan Kara flush_tlb_fix_spurious_fault(vmf->vma, vmf->address); 35771a44e149SAndrea Arcangeli } 35788f4e2101SHugh Dickins unlock: 357982b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 358083c54070SNick Piggin return 0; 35811da177e4SLinus Torvalds } 35821da177e4SLinus Torvalds 35831da177e4SLinus Torvalds /* 35841da177e4SLinus Torvalds * By the time we get here, we already hold the mm semaphore 35859a95f3cfSPaul Cassella * 35869a95f3cfSPaul Cassella * The mmap_sem may have been released depending on flags and our 35879a95f3cfSPaul Cassella * return value. See filemap_fault() and __lock_page_or_retry(). 35881da177e4SLinus Torvalds */ 3589dcddffd4SKirill A. Shutemov static int __handle_mm_fault(struct vm_area_struct *vma, unsigned long address, 3590dcddffd4SKirill A. Shutemov unsigned int flags) 35911da177e4SLinus Torvalds { 359282b0f8c3SJan Kara struct vm_fault vmf = { 3593bae473a4SKirill A. Shutemov .vma = vma, 35941a29d85eSJan Kara .address = address & PAGE_MASK, 3595bae473a4SKirill A. Shutemov .flags = flags, 35960721ec8bSJan Kara .pgoff = linear_page_index(vma, address), 3597667240e0SJan Kara .gfp_mask = __get_fault_gfp_mask(vma), 3598bae473a4SKirill A. Shutemov }; 3599dcddffd4SKirill A. Shutemov struct mm_struct *mm = vma->vm_mm; 36001da177e4SLinus Torvalds pgd_t *pgd; 36011da177e4SLinus Torvalds pud_t *pud; 36021da177e4SLinus Torvalds 36031da177e4SLinus Torvalds pgd = pgd_offset(mm, address); 36041da177e4SLinus Torvalds pud = pud_alloc(mm, pgd, address); 36051da177e4SLinus Torvalds if (!pud) 3606c74df32cSHugh Dickins return VM_FAULT_OOM; 360782b0f8c3SJan Kara vmf.pmd = pmd_alloc(mm, pud, address); 360882b0f8c3SJan Kara if (!vmf.pmd) 3609c74df32cSHugh Dickins return VM_FAULT_OOM; 361082b0f8c3SJan Kara if (pmd_none(*vmf.pmd) && transparent_hugepage_enabled(vma)) { 361182b0f8c3SJan Kara int ret = create_huge_pmd(&vmf); 3612c0292554SKirill A. Shutemov if (!(ret & VM_FAULT_FALLBACK)) 3613c0292554SKirill A. Shutemov return ret; 361471e3aac0SAndrea Arcangeli } else { 361582b0f8c3SJan Kara pmd_t orig_pmd = *vmf.pmd; 36161f1d06c3SDavid Rientjes int ret; 36171f1d06c3SDavid Rientjes 361871e3aac0SAndrea Arcangeli barrier(); 36195c7fb56eSDan Williams if (pmd_trans_huge(orig_pmd) || pmd_devmap(orig_pmd)) { 362038e08854SLorenzo Stoakes if (pmd_protnone(orig_pmd) && vma_is_accessible(vma)) 362182b0f8c3SJan Kara return do_huge_pmd_numa_page(&vmf, orig_pmd); 3622d10e63f2SMel Gorman 362382b0f8c3SJan Kara if ((vmf.flags & FAULT_FLAG_WRITE) && 3624bae473a4SKirill A. Shutemov !pmd_write(orig_pmd)) { 362582b0f8c3SJan Kara ret = wp_huge_pmd(&vmf, orig_pmd); 36269845cbbdSKirill A. Shutemov if (!(ret & VM_FAULT_FALLBACK)) 36271f1d06c3SDavid Rientjes return ret; 3628a1dd450bSWill Deacon } else { 362982b0f8c3SJan Kara huge_pmd_set_accessed(&vmf, orig_pmd); 363071e3aac0SAndrea Arcangeli return 0; 363171e3aac0SAndrea Arcangeli } 363271e3aac0SAndrea Arcangeli } 36339845cbbdSKirill A. Shutemov } 363471e3aac0SAndrea Arcangeli 363582b0f8c3SJan Kara return handle_pte_fault(&vmf); 36361da177e4SLinus Torvalds } 36371da177e4SLinus Torvalds 36389a95f3cfSPaul Cassella /* 36399a95f3cfSPaul Cassella * By the time we get here, we already hold the mm semaphore 36409a95f3cfSPaul Cassella * 36419a95f3cfSPaul Cassella * The mmap_sem may have been released depending on flags and our 36429a95f3cfSPaul Cassella * return value. See filemap_fault() and __lock_page_or_retry(). 36439a95f3cfSPaul Cassella */ 3644dcddffd4SKirill A. Shutemov int handle_mm_fault(struct vm_area_struct *vma, unsigned long address, 3645dcddffd4SKirill A. Shutemov unsigned int flags) 3646519e5247SJohannes Weiner { 3647519e5247SJohannes Weiner int ret; 3648519e5247SJohannes Weiner 3649519e5247SJohannes Weiner __set_current_state(TASK_RUNNING); 3650519e5247SJohannes Weiner 3651519e5247SJohannes Weiner count_vm_event(PGFAULT); 3652dcddffd4SKirill A. Shutemov mem_cgroup_count_vm_event(vma->vm_mm, PGFAULT); 3653519e5247SJohannes Weiner 3654519e5247SJohannes Weiner /* do counter updates before entering really critical section. */ 3655519e5247SJohannes Weiner check_sync_rss_stat(current); 3656519e5247SJohannes Weiner 3657519e5247SJohannes Weiner /* 3658519e5247SJohannes Weiner * Enable the memcg OOM handling for faults triggered in user 3659519e5247SJohannes Weiner * space. Kernel faults are handled more gracefully. 3660519e5247SJohannes Weiner */ 3661519e5247SJohannes Weiner if (flags & FAULT_FLAG_USER) 366249426420SJohannes Weiner mem_cgroup_oom_enable(); 3663519e5247SJohannes Weiner 3664bae473a4SKirill A. Shutemov if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE, 3665bae473a4SKirill A. Shutemov flags & FAULT_FLAG_INSTRUCTION, 3666bae473a4SKirill A. Shutemov flags & FAULT_FLAG_REMOTE)) 3667bae473a4SKirill A. Shutemov return VM_FAULT_SIGSEGV; 3668bae473a4SKirill A. Shutemov 3669bae473a4SKirill A. Shutemov if (unlikely(is_vm_hugetlb_page(vma))) 3670bae473a4SKirill A. Shutemov ret = hugetlb_fault(vma->vm_mm, vma, address, flags); 3671bae473a4SKirill A. Shutemov else 3672dcddffd4SKirill A. Shutemov ret = __handle_mm_fault(vma, address, flags); 3673519e5247SJohannes Weiner 367449426420SJohannes Weiner if (flags & FAULT_FLAG_USER) { 367549426420SJohannes Weiner mem_cgroup_oom_disable(); 367649426420SJohannes Weiner /* 367749426420SJohannes Weiner * The task may have entered a memcg OOM situation but 367849426420SJohannes Weiner * if the allocation error was handled gracefully (no 367949426420SJohannes Weiner * VM_FAULT_OOM), there is no need to kill anything. 368049426420SJohannes Weiner * Just clean up the OOM state peacefully. 368149426420SJohannes Weiner */ 368249426420SJohannes Weiner if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM)) 368349426420SJohannes Weiner mem_cgroup_oom_synchronize(false); 368449426420SJohannes Weiner } 36853812c8c8SJohannes Weiner 36863f70dc38SMichal Hocko /* 36873f70dc38SMichal Hocko * This mm has been already reaped by the oom reaper and so the 36883f70dc38SMichal Hocko * refault cannot be trusted in general. Anonymous refaults would 36893f70dc38SMichal Hocko * lose data and give a zero page instead e.g. This is especially 36903f70dc38SMichal Hocko * problem for use_mm() because regular tasks will just die and 36913f70dc38SMichal Hocko * the corrupted data will not be visible anywhere while kthread 36923f70dc38SMichal Hocko * will outlive the oom victim and potentially propagate the date 36933f70dc38SMichal Hocko * further. 36943f70dc38SMichal Hocko */ 36953f70dc38SMichal Hocko if (unlikely((current->flags & PF_KTHREAD) && !(ret & VM_FAULT_ERROR) 36963f70dc38SMichal Hocko && test_bit(MMF_UNSTABLE, &vma->vm_mm->flags))) 36973f70dc38SMichal Hocko ret = VM_FAULT_SIGBUS; 36983f70dc38SMichal Hocko 3699519e5247SJohannes Weiner return ret; 3700519e5247SJohannes Weiner } 3701e1d6d01aSJesse Barnes EXPORT_SYMBOL_GPL(handle_mm_fault); 3702519e5247SJohannes Weiner 37031da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED 37041da177e4SLinus Torvalds /* 37051da177e4SLinus Torvalds * Allocate page upper directory. 3706872fec16SHugh Dickins * We've already handled the fast-path in-line. 37071da177e4SLinus Torvalds */ 37081bb3630eSHugh Dickins int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address) 37091da177e4SLinus Torvalds { 3710c74df32cSHugh Dickins pud_t *new = pud_alloc_one(mm, address); 3711c74df32cSHugh Dickins if (!new) 37121bb3630eSHugh Dickins return -ENOMEM; 37131da177e4SLinus Torvalds 3714362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 3715362a61adSNick Piggin 3716872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 37171bb3630eSHugh Dickins if (pgd_present(*pgd)) /* Another has populated it */ 37185e541973SBenjamin Herrenschmidt pud_free(mm, new); 37191bb3630eSHugh Dickins else 37201da177e4SLinus Torvalds pgd_populate(mm, pgd, new); 3721872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 37221bb3630eSHugh Dickins return 0; 37231da177e4SLinus Torvalds } 37241da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */ 37251da177e4SLinus Torvalds 37261da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED 37271da177e4SLinus Torvalds /* 37281da177e4SLinus Torvalds * Allocate page middle directory. 3729872fec16SHugh Dickins * We've already handled the fast-path in-line. 37301da177e4SLinus Torvalds */ 37311bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) 37321da177e4SLinus Torvalds { 3733c74df32cSHugh Dickins pmd_t *new = pmd_alloc_one(mm, address); 3734c74df32cSHugh Dickins if (!new) 37351bb3630eSHugh Dickins return -ENOMEM; 37361da177e4SLinus Torvalds 3737362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 3738362a61adSNick Piggin 3739872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 37401da177e4SLinus Torvalds #ifndef __ARCH_HAS_4LEVEL_HACK 3741dc6c9a35SKirill A. Shutemov if (!pud_present(*pud)) { 3742dc6c9a35SKirill A. Shutemov mm_inc_nr_pmds(mm); 37431da177e4SLinus Torvalds pud_populate(mm, pud, new); 3744dc6c9a35SKirill A. Shutemov } else /* Another has populated it */ 37455e541973SBenjamin Herrenschmidt pmd_free(mm, new); 3746dc6c9a35SKirill A. Shutemov #else 3747dc6c9a35SKirill A. Shutemov if (!pgd_present(*pud)) { 3748dc6c9a35SKirill A. Shutemov mm_inc_nr_pmds(mm); 37491da177e4SLinus Torvalds pgd_populate(mm, pud, new); 3750dc6c9a35SKirill A. Shutemov } else /* Another has populated it */ 3751dc6c9a35SKirill A. Shutemov pmd_free(mm, new); 37521da177e4SLinus Torvalds #endif /* __ARCH_HAS_4LEVEL_HACK */ 3753872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 37541bb3630eSHugh Dickins return 0; 37551da177e4SLinus Torvalds } 37561da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */ 37571da177e4SLinus Torvalds 37581b36ba81SNamhyung Kim static int __follow_pte(struct mm_struct *mm, unsigned long address, 3759f8ad0f49SJohannes Weiner pte_t **ptepp, spinlock_t **ptlp) 3760f8ad0f49SJohannes Weiner { 3761f8ad0f49SJohannes Weiner pgd_t *pgd; 3762f8ad0f49SJohannes Weiner pud_t *pud; 3763f8ad0f49SJohannes Weiner pmd_t *pmd; 3764f8ad0f49SJohannes Weiner pte_t *ptep; 3765f8ad0f49SJohannes Weiner 3766f8ad0f49SJohannes Weiner pgd = pgd_offset(mm, address); 3767f8ad0f49SJohannes Weiner if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) 3768f8ad0f49SJohannes Weiner goto out; 3769f8ad0f49SJohannes Weiner 3770f8ad0f49SJohannes Weiner pud = pud_offset(pgd, address); 3771f8ad0f49SJohannes Weiner if (pud_none(*pud) || unlikely(pud_bad(*pud))) 3772f8ad0f49SJohannes Weiner goto out; 3773f8ad0f49SJohannes Weiner 3774f8ad0f49SJohannes Weiner pmd = pmd_offset(pud, address); 3775f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 3776f8ad0f49SJohannes Weiner if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd))) 3777f8ad0f49SJohannes Weiner goto out; 3778f8ad0f49SJohannes Weiner 3779f8ad0f49SJohannes Weiner /* We cannot handle huge page PFN maps. Luckily they don't exist. */ 3780f8ad0f49SJohannes Weiner if (pmd_huge(*pmd)) 3781f8ad0f49SJohannes Weiner goto out; 3782f8ad0f49SJohannes Weiner 3783f8ad0f49SJohannes Weiner ptep = pte_offset_map_lock(mm, pmd, address, ptlp); 3784f8ad0f49SJohannes Weiner if (!ptep) 3785f8ad0f49SJohannes Weiner goto out; 3786f8ad0f49SJohannes Weiner if (!pte_present(*ptep)) 3787f8ad0f49SJohannes Weiner goto unlock; 3788f8ad0f49SJohannes Weiner *ptepp = ptep; 3789f8ad0f49SJohannes Weiner return 0; 3790f8ad0f49SJohannes Weiner unlock: 3791f8ad0f49SJohannes Weiner pte_unmap_unlock(ptep, *ptlp); 3792f8ad0f49SJohannes Weiner out: 3793f8ad0f49SJohannes Weiner return -EINVAL; 3794f8ad0f49SJohannes Weiner } 3795f8ad0f49SJohannes Weiner 37961b36ba81SNamhyung Kim static inline int follow_pte(struct mm_struct *mm, unsigned long address, 37971b36ba81SNamhyung Kim pte_t **ptepp, spinlock_t **ptlp) 37981b36ba81SNamhyung Kim { 37991b36ba81SNamhyung Kim int res; 38001b36ba81SNamhyung Kim 38011b36ba81SNamhyung Kim /* (void) is needed to make gcc happy */ 38021b36ba81SNamhyung Kim (void) __cond_lock(*ptlp, 38031b36ba81SNamhyung Kim !(res = __follow_pte(mm, address, ptepp, ptlp))); 38041b36ba81SNamhyung Kim return res; 38051b36ba81SNamhyung Kim } 38061b36ba81SNamhyung Kim 38073b6748e2SJohannes Weiner /** 38083b6748e2SJohannes Weiner * follow_pfn - look up PFN at a user virtual address 38093b6748e2SJohannes Weiner * @vma: memory mapping 38103b6748e2SJohannes Weiner * @address: user virtual address 38113b6748e2SJohannes Weiner * @pfn: location to store found PFN 38123b6748e2SJohannes Weiner * 38133b6748e2SJohannes Weiner * Only IO mappings and raw PFN mappings are allowed. 38143b6748e2SJohannes Weiner * 38153b6748e2SJohannes Weiner * Returns zero and the pfn at @pfn on success, -ve otherwise. 38163b6748e2SJohannes Weiner */ 38173b6748e2SJohannes Weiner int follow_pfn(struct vm_area_struct *vma, unsigned long address, 38183b6748e2SJohannes Weiner unsigned long *pfn) 38193b6748e2SJohannes Weiner { 38203b6748e2SJohannes Weiner int ret = -EINVAL; 38213b6748e2SJohannes Weiner spinlock_t *ptl; 38223b6748e2SJohannes Weiner pte_t *ptep; 38233b6748e2SJohannes Weiner 38243b6748e2SJohannes Weiner if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 38253b6748e2SJohannes Weiner return ret; 38263b6748e2SJohannes Weiner 38273b6748e2SJohannes Weiner ret = follow_pte(vma->vm_mm, address, &ptep, &ptl); 38283b6748e2SJohannes Weiner if (ret) 38293b6748e2SJohannes Weiner return ret; 38303b6748e2SJohannes Weiner *pfn = pte_pfn(*ptep); 38313b6748e2SJohannes Weiner pte_unmap_unlock(ptep, ptl); 38323b6748e2SJohannes Weiner return 0; 38333b6748e2SJohannes Weiner } 38343b6748e2SJohannes Weiner EXPORT_SYMBOL(follow_pfn); 38353b6748e2SJohannes Weiner 383628b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT 3837d87fe660Svenkatesh.pallipadi@intel.com int follow_phys(struct vm_area_struct *vma, 383828b2ee20SRik van Riel unsigned long address, unsigned int flags, 3839d87fe660Svenkatesh.pallipadi@intel.com unsigned long *prot, resource_size_t *phys) 384028b2ee20SRik van Riel { 384103668a4dSJohannes Weiner int ret = -EINVAL; 384228b2ee20SRik van Riel pte_t *ptep, pte; 384328b2ee20SRik van Riel spinlock_t *ptl; 384428b2ee20SRik van Riel 3845d87fe660Svenkatesh.pallipadi@intel.com if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 3846d87fe660Svenkatesh.pallipadi@intel.com goto out; 384728b2ee20SRik van Riel 384803668a4dSJohannes Weiner if (follow_pte(vma->vm_mm, address, &ptep, &ptl)) 3849d87fe660Svenkatesh.pallipadi@intel.com goto out; 385028b2ee20SRik van Riel pte = *ptep; 385103668a4dSJohannes Weiner 385228b2ee20SRik van Riel if ((flags & FOLL_WRITE) && !pte_write(pte)) 385328b2ee20SRik van Riel goto unlock; 385428b2ee20SRik van Riel 385528b2ee20SRik van Riel *prot = pgprot_val(pte_pgprot(pte)); 385603668a4dSJohannes Weiner *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT; 385728b2ee20SRik van Riel 385803668a4dSJohannes Weiner ret = 0; 385928b2ee20SRik van Riel unlock: 386028b2ee20SRik van Riel pte_unmap_unlock(ptep, ptl); 386128b2ee20SRik van Riel out: 3862d87fe660Svenkatesh.pallipadi@intel.com return ret; 386328b2ee20SRik van Riel } 386428b2ee20SRik van Riel 386528b2ee20SRik van Riel int generic_access_phys(struct vm_area_struct *vma, unsigned long addr, 386628b2ee20SRik van Riel void *buf, int len, int write) 386728b2ee20SRik van Riel { 386828b2ee20SRik van Riel resource_size_t phys_addr; 386928b2ee20SRik van Riel unsigned long prot = 0; 38702bc7273bSKOSAKI Motohiro void __iomem *maddr; 387128b2ee20SRik van Riel int offset = addr & (PAGE_SIZE-1); 387228b2ee20SRik van Riel 3873d87fe660Svenkatesh.pallipadi@intel.com if (follow_phys(vma, addr, write, &prot, &phys_addr)) 387428b2ee20SRik van Riel return -EINVAL; 387528b2ee20SRik van Riel 38769cb12d7bSGrazvydas Ignotas maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot); 387728b2ee20SRik van Riel if (write) 387828b2ee20SRik van Riel memcpy_toio(maddr + offset, buf, len); 387928b2ee20SRik van Riel else 388028b2ee20SRik van Riel memcpy_fromio(buf, maddr + offset, len); 388128b2ee20SRik van Riel iounmap(maddr); 388228b2ee20SRik van Riel 388328b2ee20SRik van Riel return len; 388428b2ee20SRik van Riel } 38855a73633eSUwe Kleine-König EXPORT_SYMBOL_GPL(generic_access_phys); 388628b2ee20SRik van Riel #endif 388728b2ee20SRik van Riel 38880ec76a11SDavid Howells /* 3889206cb636SStephen Wilson * Access another process' address space as given in mm. If non-NULL, use the 3890206cb636SStephen Wilson * given task for page fault accounting. 38910ec76a11SDavid Howells */ 3892206cb636SStephen Wilson static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm, 3893442486ecSLorenzo Stoakes unsigned long addr, void *buf, int len, unsigned int gup_flags) 38940ec76a11SDavid Howells { 38950ec76a11SDavid Howells struct vm_area_struct *vma; 38960ec76a11SDavid Howells void *old_buf = buf; 3897442486ecSLorenzo Stoakes int write = gup_flags & FOLL_WRITE; 38980ec76a11SDavid Howells 38990ec76a11SDavid Howells down_read(&mm->mmap_sem); 3900183ff22bSSimon Arlott /* ignore errors, just check how much was successfully transferred */ 39010ec76a11SDavid Howells while (len) { 39020ec76a11SDavid Howells int bytes, ret, offset; 39030ec76a11SDavid Howells void *maddr; 390428b2ee20SRik van Riel struct page *page = NULL; 39050ec76a11SDavid Howells 39061e987790SDave Hansen ret = get_user_pages_remote(tsk, mm, addr, 1, 39075b56d49fSLorenzo Stoakes gup_flags, &page, &vma, NULL); 390828b2ee20SRik van Riel if (ret <= 0) { 3909dbffcd03SRik van Riel #ifndef CONFIG_HAVE_IOREMAP_PROT 3910dbffcd03SRik van Riel break; 3911dbffcd03SRik van Riel #else 391228b2ee20SRik van Riel /* 391328b2ee20SRik van Riel * Check if this is a VM_IO | VM_PFNMAP VMA, which 391428b2ee20SRik van Riel * we can access using slightly different code. 391528b2ee20SRik van Riel */ 391628b2ee20SRik van Riel vma = find_vma(mm, addr); 3917fe936dfcSMichael Ellerman if (!vma || vma->vm_start > addr) 39180ec76a11SDavid Howells break; 391928b2ee20SRik van Riel if (vma->vm_ops && vma->vm_ops->access) 392028b2ee20SRik van Riel ret = vma->vm_ops->access(vma, addr, buf, 392128b2ee20SRik van Riel len, write); 392228b2ee20SRik van Riel if (ret <= 0) 392328b2ee20SRik van Riel break; 392428b2ee20SRik van Riel bytes = ret; 3925dbffcd03SRik van Riel #endif 392628b2ee20SRik van Riel } else { 39270ec76a11SDavid Howells bytes = len; 39280ec76a11SDavid Howells offset = addr & (PAGE_SIZE-1); 39290ec76a11SDavid Howells if (bytes > PAGE_SIZE-offset) 39300ec76a11SDavid Howells bytes = PAGE_SIZE-offset; 39310ec76a11SDavid Howells 39320ec76a11SDavid Howells maddr = kmap(page); 39330ec76a11SDavid Howells if (write) { 39340ec76a11SDavid Howells copy_to_user_page(vma, page, addr, 39350ec76a11SDavid Howells maddr + offset, buf, bytes); 39360ec76a11SDavid Howells set_page_dirty_lock(page); 39370ec76a11SDavid Howells } else { 39380ec76a11SDavid Howells copy_from_user_page(vma, page, addr, 39390ec76a11SDavid Howells buf, maddr + offset, bytes); 39400ec76a11SDavid Howells } 39410ec76a11SDavid Howells kunmap(page); 394209cbfeafSKirill A. Shutemov put_page(page); 394328b2ee20SRik van Riel } 39440ec76a11SDavid Howells len -= bytes; 39450ec76a11SDavid Howells buf += bytes; 39460ec76a11SDavid Howells addr += bytes; 39470ec76a11SDavid Howells } 39480ec76a11SDavid Howells up_read(&mm->mmap_sem); 39490ec76a11SDavid Howells 39500ec76a11SDavid Howells return buf - old_buf; 39510ec76a11SDavid Howells } 395203252919SAndi Kleen 39535ddd36b9SStephen Wilson /** 3954ae91dbfcSRandy Dunlap * access_remote_vm - access another process' address space 39555ddd36b9SStephen Wilson * @mm: the mm_struct of the target address space 39565ddd36b9SStephen Wilson * @addr: start address to access 39575ddd36b9SStephen Wilson * @buf: source or destination buffer 39585ddd36b9SStephen Wilson * @len: number of bytes to transfer 39596347e8d5SLorenzo Stoakes * @gup_flags: flags modifying lookup behaviour 39605ddd36b9SStephen Wilson * 39615ddd36b9SStephen Wilson * The caller must hold a reference on @mm. 39625ddd36b9SStephen Wilson */ 39635ddd36b9SStephen Wilson int access_remote_vm(struct mm_struct *mm, unsigned long addr, 39646347e8d5SLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 39655ddd36b9SStephen Wilson { 39666347e8d5SLorenzo Stoakes return __access_remote_vm(NULL, mm, addr, buf, len, gup_flags); 39675ddd36b9SStephen Wilson } 39685ddd36b9SStephen Wilson 396903252919SAndi Kleen /* 3970206cb636SStephen Wilson * Access another process' address space. 3971206cb636SStephen Wilson * Source/target buffer must be kernel space, 3972206cb636SStephen Wilson * Do not walk the page table directly, use get_user_pages 3973206cb636SStephen Wilson */ 3974206cb636SStephen Wilson int access_process_vm(struct task_struct *tsk, unsigned long addr, 3975f307ab6dSLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 3976206cb636SStephen Wilson { 3977206cb636SStephen Wilson struct mm_struct *mm; 3978206cb636SStephen Wilson int ret; 3979206cb636SStephen Wilson 3980206cb636SStephen Wilson mm = get_task_mm(tsk); 3981206cb636SStephen Wilson if (!mm) 3982206cb636SStephen Wilson return 0; 3983206cb636SStephen Wilson 3984f307ab6dSLorenzo Stoakes ret = __access_remote_vm(tsk, mm, addr, buf, len, gup_flags); 3985442486ecSLorenzo Stoakes 3986206cb636SStephen Wilson mmput(mm); 3987206cb636SStephen Wilson 3988206cb636SStephen Wilson return ret; 3989206cb636SStephen Wilson } 3990fcd35857SCatalin Marinas EXPORT_SYMBOL_GPL(access_process_vm); 3991206cb636SStephen Wilson 399203252919SAndi Kleen /* 399303252919SAndi Kleen * Print the name of a VMA. 399403252919SAndi Kleen */ 399503252919SAndi Kleen void print_vma_addr(char *prefix, unsigned long ip) 399603252919SAndi Kleen { 399703252919SAndi Kleen struct mm_struct *mm = current->mm; 399803252919SAndi Kleen struct vm_area_struct *vma; 399903252919SAndi Kleen 4000e8bff74aSIngo Molnar /* 4001e8bff74aSIngo Molnar * Do not print if we are in atomic 4002e8bff74aSIngo Molnar * contexts (in exception stacks, etc.): 4003e8bff74aSIngo Molnar */ 4004e8bff74aSIngo Molnar if (preempt_count()) 4005e8bff74aSIngo Molnar return; 4006e8bff74aSIngo Molnar 400703252919SAndi Kleen down_read(&mm->mmap_sem); 400803252919SAndi Kleen vma = find_vma(mm, ip); 400903252919SAndi Kleen if (vma && vma->vm_file) { 401003252919SAndi Kleen struct file *f = vma->vm_file; 401103252919SAndi Kleen char *buf = (char *)__get_free_page(GFP_KERNEL); 401203252919SAndi Kleen if (buf) { 40132fbc57c5SAndy Shevchenko char *p; 401403252919SAndi Kleen 40159bf39ab2SMiklos Szeredi p = file_path(f, buf, PAGE_SIZE); 401603252919SAndi Kleen if (IS_ERR(p)) 401703252919SAndi Kleen p = "?"; 40182fbc57c5SAndy Shevchenko printk("%s%s[%lx+%lx]", prefix, kbasename(p), 401903252919SAndi Kleen vma->vm_start, 402003252919SAndi Kleen vma->vm_end - vma->vm_start); 402103252919SAndi Kleen free_page((unsigned long)buf); 402203252919SAndi Kleen } 402303252919SAndi Kleen } 402451a07e50SJeff Liu up_read(&mm->mmap_sem); 402503252919SAndi Kleen } 40263ee1afa3SNick Piggin 4027662bbcb2SMichael S. Tsirkin #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP) 40289ec23531SDavid Hildenbrand void __might_fault(const char *file, int line) 40293ee1afa3SNick Piggin { 403095156f00SPeter Zijlstra /* 403195156f00SPeter Zijlstra * Some code (nfs/sunrpc) uses socket ops on kernel memory while 403295156f00SPeter Zijlstra * holding the mmap_sem, this is safe because kernel memory doesn't 403395156f00SPeter Zijlstra * get paged out, therefore we'll never actually fault, and the 403495156f00SPeter Zijlstra * below annotations will generate false positives. 403595156f00SPeter Zijlstra */ 403695156f00SPeter Zijlstra if (segment_eq(get_fs(), KERNEL_DS)) 403795156f00SPeter Zijlstra return; 40389ec23531SDavid Hildenbrand if (pagefault_disabled()) 4039662bbcb2SMichael S. Tsirkin return; 40409ec23531SDavid Hildenbrand __might_sleep(file, line, 0); 40419ec23531SDavid Hildenbrand #if defined(CONFIG_DEBUG_ATOMIC_SLEEP) 4042662bbcb2SMichael S. Tsirkin if (current->mm) 40433ee1afa3SNick Piggin might_lock_read(¤t->mm->mmap_sem); 40449ec23531SDavid Hildenbrand #endif 40453ee1afa3SNick Piggin } 40469ec23531SDavid Hildenbrand EXPORT_SYMBOL(__might_fault); 40473ee1afa3SNick Piggin #endif 404847ad8475SAndrea Arcangeli 404947ad8475SAndrea Arcangeli #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS) 405047ad8475SAndrea Arcangeli static void clear_gigantic_page(struct page *page, 405147ad8475SAndrea Arcangeli unsigned long addr, 405247ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 405347ad8475SAndrea Arcangeli { 405447ad8475SAndrea Arcangeli int i; 405547ad8475SAndrea Arcangeli struct page *p = page; 405647ad8475SAndrea Arcangeli 405747ad8475SAndrea Arcangeli might_sleep(); 405847ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; 405947ad8475SAndrea Arcangeli i++, p = mem_map_next(p, page, i)) { 406047ad8475SAndrea Arcangeli cond_resched(); 406147ad8475SAndrea Arcangeli clear_user_highpage(p, addr + i * PAGE_SIZE); 406247ad8475SAndrea Arcangeli } 406347ad8475SAndrea Arcangeli } 406447ad8475SAndrea Arcangeli void clear_huge_page(struct page *page, 406547ad8475SAndrea Arcangeli unsigned long addr, unsigned int pages_per_huge_page) 406647ad8475SAndrea Arcangeli { 406747ad8475SAndrea Arcangeli int i; 406847ad8475SAndrea Arcangeli 406947ad8475SAndrea Arcangeli if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { 407047ad8475SAndrea Arcangeli clear_gigantic_page(page, addr, pages_per_huge_page); 407147ad8475SAndrea Arcangeli return; 407247ad8475SAndrea Arcangeli } 407347ad8475SAndrea Arcangeli 407447ad8475SAndrea Arcangeli might_sleep(); 407547ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; i++) { 407647ad8475SAndrea Arcangeli cond_resched(); 407747ad8475SAndrea Arcangeli clear_user_highpage(page + i, addr + i * PAGE_SIZE); 407847ad8475SAndrea Arcangeli } 407947ad8475SAndrea Arcangeli } 408047ad8475SAndrea Arcangeli 408147ad8475SAndrea Arcangeli static void copy_user_gigantic_page(struct page *dst, struct page *src, 408247ad8475SAndrea Arcangeli unsigned long addr, 408347ad8475SAndrea Arcangeli struct vm_area_struct *vma, 408447ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 408547ad8475SAndrea Arcangeli { 408647ad8475SAndrea Arcangeli int i; 408747ad8475SAndrea Arcangeli struct page *dst_base = dst; 408847ad8475SAndrea Arcangeli struct page *src_base = src; 408947ad8475SAndrea Arcangeli 409047ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; ) { 409147ad8475SAndrea Arcangeli cond_resched(); 409247ad8475SAndrea Arcangeli copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma); 409347ad8475SAndrea Arcangeli 409447ad8475SAndrea Arcangeli i++; 409547ad8475SAndrea Arcangeli dst = mem_map_next(dst, dst_base, i); 409647ad8475SAndrea Arcangeli src = mem_map_next(src, src_base, i); 409747ad8475SAndrea Arcangeli } 409847ad8475SAndrea Arcangeli } 409947ad8475SAndrea Arcangeli 410047ad8475SAndrea Arcangeli void copy_user_huge_page(struct page *dst, struct page *src, 410147ad8475SAndrea Arcangeli unsigned long addr, struct vm_area_struct *vma, 410247ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 410347ad8475SAndrea Arcangeli { 410447ad8475SAndrea Arcangeli int i; 410547ad8475SAndrea Arcangeli 410647ad8475SAndrea Arcangeli if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { 410747ad8475SAndrea Arcangeli copy_user_gigantic_page(dst, src, addr, vma, 410847ad8475SAndrea Arcangeli pages_per_huge_page); 410947ad8475SAndrea Arcangeli return; 411047ad8475SAndrea Arcangeli } 411147ad8475SAndrea Arcangeli 411247ad8475SAndrea Arcangeli might_sleep(); 411347ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; i++) { 411447ad8475SAndrea Arcangeli cond_resched(); 411547ad8475SAndrea Arcangeli copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma); 411647ad8475SAndrea Arcangeli } 411747ad8475SAndrea Arcangeli } 411847ad8475SAndrea Arcangeli #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */ 411949076ec2SKirill A. Shutemov 412040b64acdSOlof Johansson #if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS 4121b35f1819SKirill A. Shutemov 4122b35f1819SKirill A. Shutemov static struct kmem_cache *page_ptl_cachep; 4123b35f1819SKirill A. Shutemov 4124b35f1819SKirill A. Shutemov void __init ptlock_cache_init(void) 4125b35f1819SKirill A. Shutemov { 4126b35f1819SKirill A. Shutemov page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0, 4127b35f1819SKirill A. Shutemov SLAB_PANIC, NULL); 4128b35f1819SKirill A. Shutemov } 4129b35f1819SKirill A. Shutemov 4130539edb58SPeter Zijlstra bool ptlock_alloc(struct page *page) 413149076ec2SKirill A. Shutemov { 413249076ec2SKirill A. Shutemov spinlock_t *ptl; 413349076ec2SKirill A. Shutemov 4134b35f1819SKirill A. Shutemov ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL); 413549076ec2SKirill A. Shutemov if (!ptl) 413649076ec2SKirill A. Shutemov return false; 4137539edb58SPeter Zijlstra page->ptl = ptl; 413849076ec2SKirill A. Shutemov return true; 413949076ec2SKirill A. Shutemov } 414049076ec2SKirill A. Shutemov 4141539edb58SPeter Zijlstra void ptlock_free(struct page *page) 414249076ec2SKirill A. Shutemov { 4143b35f1819SKirill A. Shutemov kmem_cache_free(page_ptl_cachep, page->ptl); 414449076ec2SKirill A. Shutemov } 414549076ec2SKirill A. Shutemov #endif 4146