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> 661da177e4SLinus Torvalds 676952b61dSAlexey Dobriyan #include <asm/io.h> 681da177e4SLinus Torvalds #include <asm/pgalloc.h> 691da177e4SLinus Torvalds #include <asm/uaccess.h> 701da177e4SLinus Torvalds #include <asm/tlb.h> 711da177e4SLinus Torvalds #include <asm/tlbflush.h> 721da177e4SLinus Torvalds #include <asm/pgtable.h> 731da177e4SLinus Torvalds 7442b77728SJan Beulich #include "internal.h" 7542b77728SJan Beulich 7690572890SPeter Zijlstra #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS 7790572890SPeter Zijlstra #warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid. 7875980e97SPeter Zijlstra #endif 7975980e97SPeter Zijlstra 80d41dee36SAndy Whitcroft #ifndef CONFIG_NEED_MULTIPLE_NODES 811da177e4SLinus Torvalds /* use the per-pgdat data instead for discontigmem - mbligh */ 821da177e4SLinus Torvalds unsigned long max_mapnr; 831da177e4SLinus Torvalds struct page *mem_map; 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds EXPORT_SYMBOL(max_mapnr); 861da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map); 871da177e4SLinus Torvalds #endif 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds /* 901da177e4SLinus Torvalds * A number of key systems in x86 including ioremap() rely on the assumption 911da177e4SLinus Torvalds * that high_memory defines the upper bound on direct map memory, then end 921da177e4SLinus Torvalds * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and 931da177e4SLinus Torvalds * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL 941da177e4SLinus Torvalds * and ZONE_HIGHMEM. 951da177e4SLinus Torvalds */ 961da177e4SLinus Torvalds void * high_memory; 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds EXPORT_SYMBOL(high_memory); 991da177e4SLinus Torvalds 10032a93233SIngo Molnar /* 10132a93233SIngo Molnar * Randomize the address space (stacks, mmaps, brk, etc.). 10232a93233SIngo Molnar * 10332a93233SIngo Molnar * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization, 10432a93233SIngo Molnar * as ancient (libc5 based) binaries can segfault. ) 10532a93233SIngo Molnar */ 10632a93233SIngo Molnar int randomize_va_space __read_mostly = 10732a93233SIngo Molnar #ifdef CONFIG_COMPAT_BRK 10832a93233SIngo Molnar 1; 10932a93233SIngo Molnar #else 11032a93233SIngo Molnar 2; 11132a93233SIngo Molnar #endif 112a62eaf15SAndi Kleen 113a62eaf15SAndi Kleen static int __init disable_randmaps(char *s) 114a62eaf15SAndi Kleen { 115a62eaf15SAndi Kleen randomize_va_space = 0; 1169b41046cSOGAWA Hirofumi return 1; 117a62eaf15SAndi Kleen } 118a62eaf15SAndi Kleen __setup("norandmaps", disable_randmaps); 119a62eaf15SAndi Kleen 12062eede62SHugh Dickins unsigned long zero_pfn __read_mostly; 12103f6462aSHugh Dickins unsigned long highest_memmap_pfn __read_mostly; 122a13ea5b7SHugh Dickins 1230b70068eSArd Biesheuvel EXPORT_SYMBOL(zero_pfn); 1240b70068eSArd Biesheuvel 125a13ea5b7SHugh Dickins /* 126a13ea5b7SHugh Dickins * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init() 127a13ea5b7SHugh Dickins */ 128a13ea5b7SHugh Dickins static int __init init_zero_pfn(void) 129a13ea5b7SHugh Dickins { 130a13ea5b7SHugh Dickins zero_pfn = page_to_pfn(ZERO_PAGE(0)); 131a13ea5b7SHugh Dickins return 0; 132a13ea5b7SHugh Dickins } 133a13ea5b7SHugh Dickins core_initcall(init_zero_pfn); 134a62eaf15SAndi Kleen 135d559db08SKAMEZAWA Hiroyuki 13634e55232SKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING) 13734e55232SKAMEZAWA Hiroyuki 138ea48cf78SDavid Rientjes void sync_mm_rss(struct mm_struct *mm) 13934e55232SKAMEZAWA Hiroyuki { 14034e55232SKAMEZAWA Hiroyuki int i; 14134e55232SKAMEZAWA Hiroyuki 14234e55232SKAMEZAWA Hiroyuki for (i = 0; i < NR_MM_COUNTERS; i++) { 14305af2e10SDavid Rientjes if (current->rss_stat.count[i]) { 14405af2e10SDavid Rientjes add_mm_counter(mm, i, current->rss_stat.count[i]); 14505af2e10SDavid Rientjes current->rss_stat.count[i] = 0; 14634e55232SKAMEZAWA Hiroyuki } 14734e55232SKAMEZAWA Hiroyuki } 14805af2e10SDavid Rientjes current->rss_stat.events = 0; 14934e55232SKAMEZAWA Hiroyuki } 15034e55232SKAMEZAWA Hiroyuki 15134e55232SKAMEZAWA Hiroyuki static void add_mm_counter_fast(struct mm_struct *mm, int member, int val) 15234e55232SKAMEZAWA Hiroyuki { 15334e55232SKAMEZAWA Hiroyuki struct task_struct *task = current; 15434e55232SKAMEZAWA Hiroyuki 15534e55232SKAMEZAWA Hiroyuki if (likely(task->mm == mm)) 15634e55232SKAMEZAWA Hiroyuki task->rss_stat.count[member] += val; 15734e55232SKAMEZAWA Hiroyuki else 15834e55232SKAMEZAWA Hiroyuki add_mm_counter(mm, member, val); 15934e55232SKAMEZAWA Hiroyuki } 16034e55232SKAMEZAWA Hiroyuki #define inc_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, 1) 16134e55232SKAMEZAWA Hiroyuki #define dec_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, -1) 16234e55232SKAMEZAWA Hiroyuki 16334e55232SKAMEZAWA Hiroyuki /* sync counter once per 64 page faults */ 16434e55232SKAMEZAWA Hiroyuki #define TASK_RSS_EVENTS_THRESH (64) 16534e55232SKAMEZAWA Hiroyuki static void check_sync_rss_stat(struct task_struct *task) 16634e55232SKAMEZAWA Hiroyuki { 16734e55232SKAMEZAWA Hiroyuki if (unlikely(task != current)) 16834e55232SKAMEZAWA Hiroyuki return; 16934e55232SKAMEZAWA Hiroyuki if (unlikely(task->rss_stat.events++ > TASK_RSS_EVENTS_THRESH)) 170ea48cf78SDavid Rientjes sync_mm_rss(task->mm); 17134e55232SKAMEZAWA Hiroyuki } 1729547d01bSPeter Zijlstra #else /* SPLIT_RSS_COUNTING */ 17334e55232SKAMEZAWA Hiroyuki 17434e55232SKAMEZAWA Hiroyuki #define inc_mm_counter_fast(mm, member) inc_mm_counter(mm, member) 17534e55232SKAMEZAWA Hiroyuki #define dec_mm_counter_fast(mm, member) dec_mm_counter(mm, member) 17634e55232SKAMEZAWA Hiroyuki 17734e55232SKAMEZAWA Hiroyuki static void check_sync_rss_stat(struct task_struct *task) 17834e55232SKAMEZAWA Hiroyuki { 17934e55232SKAMEZAWA Hiroyuki } 18034e55232SKAMEZAWA Hiroyuki 1819547d01bSPeter Zijlstra #endif /* SPLIT_RSS_COUNTING */ 1829547d01bSPeter Zijlstra 1839547d01bSPeter Zijlstra #ifdef HAVE_GENERIC_MMU_GATHER 1849547d01bSPeter Zijlstra 185ca1d6c7dSNicholas Krause static bool tlb_next_batch(struct mmu_gather *tlb) 1869547d01bSPeter Zijlstra { 1879547d01bSPeter Zijlstra struct mmu_gather_batch *batch; 1889547d01bSPeter Zijlstra 1899547d01bSPeter Zijlstra batch = tlb->active; 1909547d01bSPeter Zijlstra if (batch->next) { 1919547d01bSPeter Zijlstra tlb->active = batch->next; 192ca1d6c7dSNicholas Krause return true; 1939547d01bSPeter Zijlstra } 1949547d01bSPeter Zijlstra 19553a59fc6SMichal Hocko if (tlb->batch_count == MAX_GATHER_BATCH_COUNT) 196ca1d6c7dSNicholas Krause return false; 19753a59fc6SMichal Hocko 1989547d01bSPeter Zijlstra batch = (void *)__get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0); 1999547d01bSPeter Zijlstra if (!batch) 200ca1d6c7dSNicholas Krause return false; 2019547d01bSPeter Zijlstra 20253a59fc6SMichal Hocko tlb->batch_count++; 2039547d01bSPeter Zijlstra batch->next = NULL; 2049547d01bSPeter Zijlstra batch->nr = 0; 2059547d01bSPeter Zijlstra batch->max = MAX_GATHER_BATCH; 2069547d01bSPeter Zijlstra 2079547d01bSPeter Zijlstra tlb->active->next = batch; 2089547d01bSPeter Zijlstra tlb->active = batch; 2099547d01bSPeter Zijlstra 210ca1d6c7dSNicholas Krause return true; 2119547d01bSPeter Zijlstra } 2129547d01bSPeter Zijlstra 2139547d01bSPeter Zijlstra /* tlb_gather_mmu 2149547d01bSPeter Zijlstra * Called to initialize an (on-stack) mmu_gather structure for page-table 2159547d01bSPeter Zijlstra * tear-down from @mm. The @fullmm argument is used when @mm is without 2169547d01bSPeter Zijlstra * users and we're going to destroy the full address space (exit/execve). 2179547d01bSPeter Zijlstra */ 2182b047252SLinus Torvalds void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long start, unsigned long end) 2199547d01bSPeter Zijlstra { 2209547d01bSPeter Zijlstra tlb->mm = mm; 2219547d01bSPeter Zijlstra 2222b047252SLinus Torvalds /* Is it from 0 to ~0? */ 2232b047252SLinus Torvalds tlb->fullmm = !(start | (end+1)); 2241de14c3cSDave Hansen tlb->need_flush_all = 0; 2259547d01bSPeter Zijlstra tlb->local.next = NULL; 2269547d01bSPeter Zijlstra tlb->local.nr = 0; 2279547d01bSPeter Zijlstra tlb->local.max = ARRAY_SIZE(tlb->__pages); 2289547d01bSPeter Zijlstra tlb->active = &tlb->local; 22953a59fc6SMichal Hocko tlb->batch_count = 0; 2309547d01bSPeter Zijlstra 2319547d01bSPeter Zijlstra #ifdef CONFIG_HAVE_RCU_TABLE_FREE 2329547d01bSPeter Zijlstra tlb->batch = NULL; 23334e55232SKAMEZAWA Hiroyuki #endif 234fb7332a9SWill Deacon 235fb7332a9SWill Deacon __tlb_reset_range(tlb); 2369547d01bSPeter Zijlstra } 2379547d01bSPeter Zijlstra 2381cf35d47SLinus Torvalds static void tlb_flush_mmu_tlbonly(struct mmu_gather *tlb) 2399547d01bSPeter Zijlstra { 240721c21c1SWill Deacon if (!tlb->end) 241721c21c1SWill Deacon return; 242721c21c1SWill Deacon 2439547d01bSPeter Zijlstra tlb_flush(tlb); 24434ee645eSJoerg Roedel mmu_notifier_invalidate_range(tlb->mm, tlb->start, tlb->end); 2459547d01bSPeter Zijlstra #ifdef CONFIG_HAVE_RCU_TABLE_FREE 2469547d01bSPeter Zijlstra tlb_table_flush(tlb); 2479547d01bSPeter Zijlstra #endif 248fb7332a9SWill Deacon __tlb_reset_range(tlb); 2491cf35d47SLinus Torvalds } 2501cf35d47SLinus Torvalds 2511cf35d47SLinus Torvalds static void tlb_flush_mmu_free(struct mmu_gather *tlb) 2521cf35d47SLinus Torvalds { 2531cf35d47SLinus Torvalds struct mmu_gather_batch *batch; 2549547d01bSPeter Zijlstra 255721c21c1SWill Deacon for (batch = &tlb->local; batch && batch->nr; batch = batch->next) { 2569547d01bSPeter Zijlstra free_pages_and_swap_cache(batch->pages, batch->nr); 2579547d01bSPeter Zijlstra batch->nr = 0; 2589547d01bSPeter Zijlstra } 2599547d01bSPeter Zijlstra tlb->active = &tlb->local; 2609547d01bSPeter Zijlstra } 2619547d01bSPeter Zijlstra 2621cf35d47SLinus Torvalds void tlb_flush_mmu(struct mmu_gather *tlb) 2631cf35d47SLinus Torvalds { 2641cf35d47SLinus Torvalds tlb_flush_mmu_tlbonly(tlb); 2651cf35d47SLinus Torvalds tlb_flush_mmu_free(tlb); 2661cf35d47SLinus Torvalds } 2671cf35d47SLinus Torvalds 2689547d01bSPeter Zijlstra /* tlb_finish_mmu 2699547d01bSPeter Zijlstra * Called at the end of the shootdown operation to free up any resources 2709547d01bSPeter Zijlstra * that were required. 2719547d01bSPeter Zijlstra */ 2729547d01bSPeter Zijlstra void tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end) 2739547d01bSPeter Zijlstra { 2749547d01bSPeter Zijlstra struct mmu_gather_batch *batch, *next; 2759547d01bSPeter Zijlstra 2769547d01bSPeter Zijlstra tlb_flush_mmu(tlb); 2779547d01bSPeter Zijlstra 2789547d01bSPeter Zijlstra /* keep the page table cache within bounds */ 2799547d01bSPeter Zijlstra check_pgt_cache(); 2809547d01bSPeter Zijlstra 2819547d01bSPeter Zijlstra for (batch = tlb->local.next; batch; batch = next) { 2829547d01bSPeter Zijlstra next = batch->next; 2839547d01bSPeter Zijlstra free_pages((unsigned long)batch, 0); 2849547d01bSPeter Zijlstra } 2859547d01bSPeter Zijlstra tlb->local.next = NULL; 2869547d01bSPeter Zijlstra } 2879547d01bSPeter Zijlstra 2889547d01bSPeter Zijlstra /* __tlb_remove_page 2899547d01bSPeter Zijlstra * Must perform the equivalent to __free_pte(pte_get_and_clear(ptep)), while 2909547d01bSPeter Zijlstra * handling the additional races in SMP caused by other CPUs caching valid 2919547d01bSPeter Zijlstra * mappings in their TLBs. Returns the number of free page slots left. 2929547d01bSPeter Zijlstra * When out of page slots we must call tlb_flush_mmu(). 2939547d01bSPeter Zijlstra */ 2949547d01bSPeter Zijlstra int __tlb_remove_page(struct mmu_gather *tlb, struct page *page) 2959547d01bSPeter Zijlstra { 2969547d01bSPeter Zijlstra struct mmu_gather_batch *batch; 2979547d01bSPeter Zijlstra 298fb7332a9SWill Deacon VM_BUG_ON(!tlb->end); 2999547d01bSPeter Zijlstra 3009547d01bSPeter Zijlstra batch = tlb->active; 3019547d01bSPeter Zijlstra batch->pages[batch->nr++] = page; 3029547d01bSPeter Zijlstra if (batch->nr == batch->max) { 3039547d01bSPeter Zijlstra if (!tlb_next_batch(tlb)) 3049547d01bSPeter Zijlstra return 0; 3050b43c3aaSShaohua Li batch = tlb->active; 3069547d01bSPeter Zijlstra } 307309381feSSasha Levin VM_BUG_ON_PAGE(batch->nr > batch->max, page); 3089547d01bSPeter Zijlstra 3099547d01bSPeter Zijlstra return batch->max - batch->nr; 3109547d01bSPeter Zijlstra } 3119547d01bSPeter Zijlstra 3129547d01bSPeter Zijlstra #endif /* HAVE_GENERIC_MMU_GATHER */ 31334e55232SKAMEZAWA Hiroyuki 31426723911SPeter Zijlstra #ifdef CONFIG_HAVE_RCU_TABLE_FREE 31526723911SPeter Zijlstra 31626723911SPeter Zijlstra /* 31726723911SPeter Zijlstra * See the comment near struct mmu_table_batch. 31826723911SPeter Zijlstra */ 31926723911SPeter Zijlstra 32026723911SPeter Zijlstra static void tlb_remove_table_smp_sync(void *arg) 32126723911SPeter Zijlstra { 32226723911SPeter Zijlstra /* Simply deliver the interrupt */ 32326723911SPeter Zijlstra } 32426723911SPeter Zijlstra 32526723911SPeter Zijlstra static void tlb_remove_table_one(void *table) 32626723911SPeter Zijlstra { 32726723911SPeter Zijlstra /* 32826723911SPeter Zijlstra * This isn't an RCU grace period and hence the page-tables cannot be 32926723911SPeter Zijlstra * assumed to be actually RCU-freed. 33026723911SPeter Zijlstra * 33126723911SPeter Zijlstra * It is however sufficient for software page-table walkers that rely on 33226723911SPeter Zijlstra * IRQ disabling. See the comment near struct mmu_table_batch. 33326723911SPeter Zijlstra */ 33426723911SPeter Zijlstra smp_call_function(tlb_remove_table_smp_sync, NULL, 1); 33526723911SPeter Zijlstra __tlb_remove_table(table); 33626723911SPeter Zijlstra } 33726723911SPeter Zijlstra 33826723911SPeter Zijlstra static void tlb_remove_table_rcu(struct rcu_head *head) 33926723911SPeter Zijlstra { 34026723911SPeter Zijlstra struct mmu_table_batch *batch; 34126723911SPeter Zijlstra int i; 34226723911SPeter Zijlstra 34326723911SPeter Zijlstra batch = container_of(head, struct mmu_table_batch, rcu); 34426723911SPeter Zijlstra 34526723911SPeter Zijlstra for (i = 0; i < batch->nr; i++) 34626723911SPeter Zijlstra __tlb_remove_table(batch->tables[i]); 34726723911SPeter Zijlstra 34826723911SPeter Zijlstra free_page((unsigned long)batch); 34926723911SPeter Zijlstra } 35026723911SPeter Zijlstra 35126723911SPeter Zijlstra void tlb_table_flush(struct mmu_gather *tlb) 35226723911SPeter Zijlstra { 35326723911SPeter Zijlstra struct mmu_table_batch **batch = &tlb->batch; 35426723911SPeter Zijlstra 35526723911SPeter Zijlstra if (*batch) { 35626723911SPeter Zijlstra call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu); 35726723911SPeter Zijlstra *batch = NULL; 35826723911SPeter Zijlstra } 35926723911SPeter Zijlstra } 36026723911SPeter Zijlstra 36126723911SPeter Zijlstra void tlb_remove_table(struct mmu_gather *tlb, void *table) 36226723911SPeter Zijlstra { 36326723911SPeter Zijlstra struct mmu_table_batch **batch = &tlb->batch; 36426723911SPeter Zijlstra 36526723911SPeter Zijlstra /* 36626723911SPeter Zijlstra * When there's less then two users of this mm there cannot be a 36726723911SPeter Zijlstra * concurrent page-table walk. 36826723911SPeter Zijlstra */ 36926723911SPeter Zijlstra if (atomic_read(&tlb->mm->mm_users) < 2) { 37026723911SPeter Zijlstra __tlb_remove_table(table); 37126723911SPeter Zijlstra return; 37226723911SPeter Zijlstra } 37326723911SPeter Zijlstra 37426723911SPeter Zijlstra if (*batch == NULL) { 37526723911SPeter Zijlstra *batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN); 37626723911SPeter Zijlstra if (*batch == NULL) { 37726723911SPeter Zijlstra tlb_remove_table_one(table); 37826723911SPeter Zijlstra return; 37926723911SPeter Zijlstra } 38026723911SPeter Zijlstra (*batch)->nr = 0; 38126723911SPeter Zijlstra } 38226723911SPeter Zijlstra (*batch)->tables[(*batch)->nr++] = table; 38326723911SPeter Zijlstra if ((*batch)->nr == MAX_TABLE_BATCH) 38426723911SPeter Zijlstra tlb_table_flush(tlb); 38526723911SPeter Zijlstra } 38626723911SPeter Zijlstra 3879547d01bSPeter Zijlstra #endif /* CONFIG_HAVE_RCU_TABLE_FREE */ 38826723911SPeter Zijlstra 3891da177e4SLinus Torvalds /* 3901da177e4SLinus Torvalds * Note: this doesn't free the actual pages themselves. That 3911da177e4SLinus Torvalds * has been handled earlier when unmapping all the memory regions. 3921da177e4SLinus Torvalds */ 3939e1b32caSBenjamin Herrenschmidt static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd, 3949e1b32caSBenjamin Herrenschmidt unsigned long addr) 3951da177e4SLinus Torvalds { 3962f569afdSMartin Schwidefsky pgtable_t token = pmd_pgtable(*pmd); 3971da177e4SLinus Torvalds pmd_clear(pmd); 3989e1b32caSBenjamin Herrenschmidt pte_free_tlb(tlb, token, addr); 399e1f56c89SKirill A. Shutemov atomic_long_dec(&tlb->mm->nr_ptes); 4001da177e4SLinus Torvalds } 4011da177e4SLinus Torvalds 402e0da382cSHugh Dickins static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud, 403e0da382cSHugh Dickins unsigned long addr, unsigned long end, 404e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 4051da177e4SLinus Torvalds { 4061da177e4SLinus Torvalds pmd_t *pmd; 4071da177e4SLinus Torvalds unsigned long next; 408e0da382cSHugh Dickins unsigned long start; 4091da177e4SLinus Torvalds 410e0da382cSHugh Dickins start = addr; 4111da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 4121da177e4SLinus Torvalds do { 4131da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 4141da177e4SLinus Torvalds if (pmd_none_or_clear_bad(pmd)) 4151da177e4SLinus Torvalds continue; 4169e1b32caSBenjamin Herrenschmidt free_pte_range(tlb, pmd, addr); 4171da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 4181da177e4SLinus Torvalds 419e0da382cSHugh Dickins start &= PUD_MASK; 420e0da382cSHugh Dickins if (start < floor) 421e0da382cSHugh Dickins return; 422e0da382cSHugh Dickins if (ceiling) { 423e0da382cSHugh Dickins ceiling &= PUD_MASK; 424e0da382cSHugh Dickins if (!ceiling) 425e0da382cSHugh Dickins return; 4261da177e4SLinus Torvalds } 427e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 428e0da382cSHugh Dickins return; 429e0da382cSHugh Dickins 430e0da382cSHugh Dickins pmd = pmd_offset(pud, start); 431e0da382cSHugh Dickins pud_clear(pud); 4329e1b32caSBenjamin Herrenschmidt pmd_free_tlb(tlb, pmd, start); 433dc6c9a35SKirill A. Shutemov mm_dec_nr_pmds(tlb->mm); 4341da177e4SLinus Torvalds } 4351da177e4SLinus Torvalds 436e0da382cSHugh Dickins static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd, 437e0da382cSHugh Dickins unsigned long addr, unsigned long end, 438e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 4391da177e4SLinus Torvalds { 4401da177e4SLinus Torvalds pud_t *pud; 4411da177e4SLinus Torvalds unsigned long next; 442e0da382cSHugh Dickins unsigned long start; 4431da177e4SLinus Torvalds 444e0da382cSHugh Dickins start = addr; 4451da177e4SLinus Torvalds pud = pud_offset(pgd, addr); 4461da177e4SLinus Torvalds do { 4471da177e4SLinus Torvalds next = pud_addr_end(addr, end); 4481da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 4491da177e4SLinus Torvalds continue; 450e0da382cSHugh Dickins free_pmd_range(tlb, pud, addr, next, floor, ceiling); 4511da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 4521da177e4SLinus Torvalds 453e0da382cSHugh Dickins start &= PGDIR_MASK; 454e0da382cSHugh Dickins if (start < floor) 455e0da382cSHugh Dickins return; 456e0da382cSHugh Dickins if (ceiling) { 457e0da382cSHugh Dickins ceiling &= PGDIR_MASK; 458e0da382cSHugh Dickins if (!ceiling) 459e0da382cSHugh Dickins return; 4601da177e4SLinus Torvalds } 461e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 462e0da382cSHugh Dickins return; 463e0da382cSHugh Dickins 464e0da382cSHugh Dickins pud = pud_offset(pgd, start); 465e0da382cSHugh Dickins pgd_clear(pgd); 4669e1b32caSBenjamin Herrenschmidt pud_free_tlb(tlb, pud, start); 4671da177e4SLinus Torvalds } 4681da177e4SLinus Torvalds 4691da177e4SLinus Torvalds /* 470e0da382cSHugh Dickins * This function frees user-level page tables of a process. 4711da177e4SLinus Torvalds */ 47242b77728SJan Beulich void free_pgd_range(struct mmu_gather *tlb, 473e0da382cSHugh Dickins unsigned long addr, unsigned long end, 474e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 4751da177e4SLinus Torvalds { 4761da177e4SLinus Torvalds pgd_t *pgd; 4771da177e4SLinus Torvalds unsigned long next; 4781da177e4SLinus Torvalds 479e0da382cSHugh Dickins /* 480e0da382cSHugh Dickins * The next few lines have given us lots of grief... 481e0da382cSHugh Dickins * 482e0da382cSHugh Dickins * Why are we testing PMD* at this top level? Because often 483e0da382cSHugh Dickins * there will be no work to do at all, and we'd prefer not to 484e0da382cSHugh Dickins * go all the way down to the bottom just to discover that. 485e0da382cSHugh Dickins * 486e0da382cSHugh Dickins * Why all these "- 1"s? Because 0 represents both the bottom 487e0da382cSHugh Dickins * of the address space and the top of it (using -1 for the 488e0da382cSHugh Dickins * top wouldn't help much: the masks would do the wrong thing). 489e0da382cSHugh Dickins * The rule is that addr 0 and floor 0 refer to the bottom of 490e0da382cSHugh Dickins * the address space, but end 0 and ceiling 0 refer to the top 491e0da382cSHugh Dickins * Comparisons need to use "end - 1" and "ceiling - 1" (though 492e0da382cSHugh Dickins * that end 0 case should be mythical). 493e0da382cSHugh Dickins * 494e0da382cSHugh Dickins * Wherever addr is brought up or ceiling brought down, we must 495e0da382cSHugh Dickins * be careful to reject "the opposite 0" before it confuses the 496e0da382cSHugh Dickins * subsequent tests. But what about where end is brought down 497e0da382cSHugh Dickins * by PMD_SIZE below? no, end can't go down to 0 there. 498e0da382cSHugh Dickins * 499e0da382cSHugh Dickins * Whereas we round start (addr) and ceiling down, by different 500e0da382cSHugh Dickins * masks at different levels, in order to test whether a table 501e0da382cSHugh Dickins * now has no other vmas using it, so can be freed, we don't 502e0da382cSHugh Dickins * bother to round floor or end up - the tests don't need that. 503e0da382cSHugh Dickins */ 504e0da382cSHugh Dickins 505e0da382cSHugh Dickins addr &= PMD_MASK; 506e0da382cSHugh Dickins if (addr < floor) { 507e0da382cSHugh Dickins addr += PMD_SIZE; 508e0da382cSHugh Dickins if (!addr) 509e0da382cSHugh Dickins return; 510e0da382cSHugh Dickins } 511e0da382cSHugh Dickins if (ceiling) { 512e0da382cSHugh Dickins ceiling &= PMD_MASK; 513e0da382cSHugh Dickins if (!ceiling) 514e0da382cSHugh Dickins return; 515e0da382cSHugh Dickins } 516e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 517e0da382cSHugh Dickins end -= PMD_SIZE; 518e0da382cSHugh Dickins if (addr > end - 1) 519e0da382cSHugh Dickins return; 520e0da382cSHugh Dickins 52142b77728SJan Beulich pgd = pgd_offset(tlb->mm, addr); 5221da177e4SLinus Torvalds do { 5231da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 5241da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 5251da177e4SLinus Torvalds continue; 52642b77728SJan Beulich free_pud_range(tlb, pgd, addr, next, floor, ceiling); 5271da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 528e0da382cSHugh Dickins } 529e0da382cSHugh Dickins 53042b77728SJan Beulich void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma, 531e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 532e0da382cSHugh Dickins { 533e0da382cSHugh Dickins while (vma) { 534e0da382cSHugh Dickins struct vm_area_struct *next = vma->vm_next; 535e0da382cSHugh Dickins unsigned long addr = vma->vm_start; 536e0da382cSHugh Dickins 5378f4f8c16SHugh Dickins /* 53825d9e2d1Snpiggin@suse.de * Hide vma from rmap and truncate_pagecache before freeing 53925d9e2d1Snpiggin@suse.de * pgtables 5408f4f8c16SHugh Dickins */ 5415beb4930SRik van Riel unlink_anon_vmas(vma); 5428f4f8c16SHugh Dickins unlink_file_vma(vma); 5438f4f8c16SHugh Dickins 5449da61aefSDavid Gibson if (is_vm_hugetlb_page(vma)) { 5453bf5ee95SHugh Dickins hugetlb_free_pgd_range(tlb, addr, vma->vm_end, 5463bf5ee95SHugh Dickins floor, next? next->vm_start: ceiling); 5473bf5ee95SHugh Dickins } else { 5483bf5ee95SHugh Dickins /* 5493bf5ee95SHugh Dickins * Optimization: gather nearby vmas into one call down 5503bf5ee95SHugh Dickins */ 5513bf5ee95SHugh Dickins while (next && next->vm_start <= vma->vm_end + PMD_SIZE 5524866920bSDavid Gibson && !is_vm_hugetlb_page(next)) { 553e0da382cSHugh Dickins vma = next; 554e0da382cSHugh Dickins next = vma->vm_next; 5555beb4930SRik van Riel unlink_anon_vmas(vma); 5568f4f8c16SHugh Dickins unlink_file_vma(vma); 557e0da382cSHugh Dickins } 5583bf5ee95SHugh Dickins free_pgd_range(tlb, addr, vma->vm_end, 559e0da382cSHugh Dickins floor, next? next->vm_start: ceiling); 5603bf5ee95SHugh Dickins } 561e0da382cSHugh Dickins vma = next; 562e0da382cSHugh Dickins } 5631da177e4SLinus Torvalds } 5641da177e4SLinus Torvalds 5653ed3a4f0SKirill A. Shutemov int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address) 5661da177e4SLinus Torvalds { 567c4088ebdSKirill A. Shutemov spinlock_t *ptl; 5682f569afdSMartin Schwidefsky pgtable_t new = pte_alloc_one(mm, address); 5691da177e4SLinus Torvalds if (!new) 5701bb3630eSHugh Dickins return -ENOMEM; 5711bb3630eSHugh Dickins 572362a61adSNick Piggin /* 573362a61adSNick Piggin * Ensure all pte setup (eg. pte page lock and page clearing) are 574362a61adSNick Piggin * visible before the pte is made visible to other CPUs by being 575362a61adSNick Piggin * put into page tables. 576362a61adSNick Piggin * 577362a61adSNick Piggin * The other side of the story is the pointer chasing in the page 578362a61adSNick Piggin * table walking code (when walking the page table without locking; 579362a61adSNick Piggin * ie. most of the time). Fortunately, these data accesses consist 580362a61adSNick Piggin * of a chain of data-dependent loads, meaning most CPUs (alpha 581362a61adSNick Piggin * being the notable exception) will already guarantee loads are 582362a61adSNick Piggin * seen in-order. See the alpha page table accessors for the 583362a61adSNick Piggin * smp_read_barrier_depends() barriers in page table walking code. 584362a61adSNick Piggin */ 585362a61adSNick Piggin smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */ 586362a61adSNick Piggin 587c4088ebdSKirill A. Shutemov ptl = pmd_lock(mm, pmd); 5888ac1f832SAndrea Arcangeli if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 589e1f56c89SKirill A. Shutemov atomic_long_inc(&mm->nr_ptes); 5901da177e4SLinus Torvalds pmd_populate(mm, pmd, new); 5912f569afdSMartin Schwidefsky new = NULL; 5924b471e88SKirill A. Shutemov } 593c4088ebdSKirill A. Shutemov spin_unlock(ptl); 5942f569afdSMartin Schwidefsky if (new) 5952f569afdSMartin Schwidefsky pte_free(mm, new); 5961bb3630eSHugh Dickins return 0; 5971da177e4SLinus Torvalds } 5981da177e4SLinus Torvalds 5991bb3630eSHugh Dickins int __pte_alloc_kernel(pmd_t *pmd, unsigned long address) 6001da177e4SLinus Torvalds { 6011bb3630eSHugh Dickins pte_t *new = pte_alloc_one_kernel(&init_mm, address); 6021da177e4SLinus Torvalds if (!new) 6031bb3630eSHugh Dickins return -ENOMEM; 6041da177e4SLinus Torvalds 605362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 606362a61adSNick Piggin 607872fec16SHugh Dickins spin_lock(&init_mm.page_table_lock); 6088ac1f832SAndrea Arcangeli if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 609872fec16SHugh Dickins pmd_populate_kernel(&init_mm, pmd, new); 6102f569afdSMartin Schwidefsky new = NULL; 6114b471e88SKirill A. Shutemov } 612872fec16SHugh Dickins spin_unlock(&init_mm.page_table_lock); 6132f569afdSMartin Schwidefsky if (new) 6142f569afdSMartin Schwidefsky pte_free_kernel(&init_mm, new); 6151bb3630eSHugh Dickins return 0; 6161da177e4SLinus Torvalds } 6171da177e4SLinus Torvalds 618d559db08SKAMEZAWA Hiroyuki static inline void init_rss_vec(int *rss) 619ae859762SHugh Dickins { 620d559db08SKAMEZAWA Hiroyuki memset(rss, 0, sizeof(int) * NR_MM_COUNTERS); 621d559db08SKAMEZAWA Hiroyuki } 622d559db08SKAMEZAWA Hiroyuki 623d559db08SKAMEZAWA Hiroyuki static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss) 624d559db08SKAMEZAWA Hiroyuki { 625d559db08SKAMEZAWA Hiroyuki int i; 626d559db08SKAMEZAWA Hiroyuki 62734e55232SKAMEZAWA Hiroyuki if (current->mm == mm) 62805af2e10SDavid Rientjes sync_mm_rss(mm); 629d559db08SKAMEZAWA Hiroyuki for (i = 0; i < NR_MM_COUNTERS; i++) 630d559db08SKAMEZAWA Hiroyuki if (rss[i]) 631d559db08SKAMEZAWA Hiroyuki add_mm_counter(mm, i, rss[i]); 632ae859762SHugh Dickins } 633ae859762SHugh Dickins 6341da177e4SLinus Torvalds /* 6356aab341eSLinus Torvalds * This function is called to print an error when a bad pte 6366aab341eSLinus Torvalds * is found. For example, we might have a PFN-mapped pte in 6376aab341eSLinus Torvalds * a region that doesn't allow it. 638b5810039SNick Piggin * 639b5810039SNick Piggin * The calling function must still handle the error. 640b5810039SNick Piggin */ 6413dc14741SHugh Dickins static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr, 6423dc14741SHugh Dickins pte_t pte, struct page *page) 643b5810039SNick Piggin { 6443dc14741SHugh Dickins pgd_t *pgd = pgd_offset(vma->vm_mm, addr); 6453dc14741SHugh Dickins pud_t *pud = pud_offset(pgd, addr); 6463dc14741SHugh Dickins pmd_t *pmd = pmd_offset(pud, addr); 6473dc14741SHugh Dickins struct address_space *mapping; 6483dc14741SHugh Dickins pgoff_t index; 649d936cf9bSHugh Dickins static unsigned long resume; 650d936cf9bSHugh Dickins static unsigned long nr_shown; 651d936cf9bSHugh Dickins static unsigned long nr_unshown; 652d936cf9bSHugh Dickins 653d936cf9bSHugh Dickins /* 654d936cf9bSHugh Dickins * Allow a burst of 60 reports, then keep quiet for that minute; 655d936cf9bSHugh Dickins * or allow a steady drip of one report per second. 656d936cf9bSHugh Dickins */ 657d936cf9bSHugh Dickins if (nr_shown == 60) { 658d936cf9bSHugh Dickins if (time_before(jiffies, resume)) { 659d936cf9bSHugh Dickins nr_unshown++; 660d936cf9bSHugh Dickins return; 661d936cf9bSHugh Dickins } 662d936cf9bSHugh Dickins if (nr_unshown) { 663*1170532bSJoe Perches pr_alert("BUG: Bad page map: %lu messages suppressed\n", 664d936cf9bSHugh Dickins nr_unshown); 665d936cf9bSHugh Dickins nr_unshown = 0; 666d936cf9bSHugh Dickins } 667d936cf9bSHugh Dickins nr_shown = 0; 668d936cf9bSHugh Dickins } 669d936cf9bSHugh Dickins if (nr_shown++ == 0) 670d936cf9bSHugh Dickins resume = jiffies + 60 * HZ; 6713dc14741SHugh Dickins 6723dc14741SHugh Dickins mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL; 6733dc14741SHugh Dickins index = linear_page_index(vma, addr); 6743dc14741SHugh Dickins 675*1170532bSJoe Perches pr_alert("BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n", 6763dc14741SHugh Dickins current->comm, 6773dc14741SHugh Dickins (long long)pte_val(pte), (long long)pmd_val(*pmd)); 678718a3821SWu Fengguang if (page) 679f0b791a3SDave Hansen dump_page(page, "bad pte"); 680*1170532bSJoe Perches pr_alert("addr:%p vm_flags:%08lx anon_vma:%p mapping:%p index:%lx\n", 6813dc14741SHugh Dickins (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index); 6823dc14741SHugh Dickins /* 6833dc14741SHugh Dickins * Choose text because data symbols depend on CONFIG_KALLSYMS_ALL=y 6843dc14741SHugh Dickins */ 6852682582aSKonstantin Khlebnikov pr_alert("file:%pD fault:%pf mmap:%pf readpage:%pf\n", 6862682582aSKonstantin Khlebnikov vma->vm_file, 6872682582aSKonstantin Khlebnikov vma->vm_ops ? vma->vm_ops->fault : NULL, 6882682582aSKonstantin Khlebnikov vma->vm_file ? vma->vm_file->f_op->mmap : NULL, 6892682582aSKonstantin Khlebnikov mapping ? mapping->a_ops->readpage : NULL); 690b5810039SNick Piggin dump_stack(); 691373d4d09SRusty Russell add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); 692b5810039SNick Piggin } 693b5810039SNick Piggin 694b5810039SNick Piggin /* 6957e675137SNick Piggin * vm_normal_page -- This function gets the "struct page" associated with a pte. 6966aab341eSLinus Torvalds * 6977e675137SNick Piggin * "Special" mappings do not wish to be associated with a "struct page" (either 6987e675137SNick Piggin * it doesn't exist, or it exists but they don't want to touch it). In this 6997e675137SNick Piggin * case, NULL is returned here. "Normal" mappings do have a struct page. 700b379d790SJared Hulbert * 7017e675137SNick Piggin * There are 2 broad cases. Firstly, an architecture may define a pte_special() 7027e675137SNick Piggin * pte bit, in which case this function is trivial. Secondly, an architecture 7037e675137SNick Piggin * may not have a spare pte bit, which requires a more complicated scheme, 7047e675137SNick Piggin * described below. 7057e675137SNick Piggin * 7067e675137SNick Piggin * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a 7077e675137SNick Piggin * special mapping (even if there are underlying and valid "struct pages"). 7087e675137SNick Piggin * COWed pages of a VM_PFNMAP are always normal. 7096aab341eSLinus Torvalds * 710b379d790SJared Hulbert * The way we recognize COWed pages within VM_PFNMAP mappings is through the 711b379d790SJared Hulbert * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit 7127e675137SNick Piggin * set, and the vm_pgoff will point to the first PFN mapped: thus every special 7137e675137SNick Piggin * mapping will always honor the rule 7146aab341eSLinus Torvalds * 7156aab341eSLinus Torvalds * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT) 7166aab341eSLinus Torvalds * 7177e675137SNick Piggin * And for normal mappings this is false. 718b379d790SJared Hulbert * 7197e675137SNick Piggin * This restricts such mappings to be a linear translation from virtual address 7207e675137SNick Piggin * to pfn. To get around this restriction, we allow arbitrary mappings so long 7217e675137SNick Piggin * as the vma is not a COW mapping; in that case, we know that all ptes are 7227e675137SNick Piggin * special (because none can have been COWed). 723b379d790SJared Hulbert * 724b379d790SJared Hulbert * 7257e675137SNick Piggin * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP. 7267e675137SNick Piggin * 727b379d790SJared Hulbert * VM_MIXEDMAP mappings can likewise contain memory with or without "struct 728b379d790SJared Hulbert * page" backing, however the difference is that _all_ pages with a struct 729b379d790SJared Hulbert * page (that is, those where pfn_valid is true) are refcounted and considered 730b379d790SJared Hulbert * normal pages by the VM. The disadvantage is that pages are refcounted 731b379d790SJared Hulbert * (which can be slower and simply not an option for some PFNMAP users). The 732b379d790SJared Hulbert * advantage is that we don't have to follow the strict linearity rule of 733b379d790SJared Hulbert * PFNMAP mappings in order to support COWable mappings. 734b379d790SJared Hulbert * 735ee498ed7SHugh Dickins */ 7367e675137SNick Piggin #ifdef __HAVE_ARCH_PTE_SPECIAL 7377e675137SNick Piggin # define HAVE_PTE_SPECIAL 1 7387e675137SNick Piggin #else 7397e675137SNick Piggin # define HAVE_PTE_SPECIAL 0 7407e675137SNick Piggin #endif 7417e675137SNick Piggin struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, 7427e675137SNick Piggin pte_t pte) 743ee498ed7SHugh Dickins { 74422b31eecSHugh Dickins unsigned long pfn = pte_pfn(pte); 7457e675137SNick Piggin 7467e675137SNick Piggin if (HAVE_PTE_SPECIAL) { 747b38af472SHugh Dickins if (likely(!pte_special(pte))) 74822b31eecSHugh Dickins goto check_pfn; 749667a0a06SDavid Vrabel if (vma->vm_ops && vma->vm_ops->find_special_page) 750667a0a06SDavid Vrabel return vma->vm_ops->find_special_page(vma, addr); 751a13ea5b7SHugh Dickins if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)) 752a13ea5b7SHugh Dickins return NULL; 75362eede62SHugh Dickins if (!is_zero_pfn(pfn)) 75422b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 7557e675137SNick Piggin return NULL; 7567e675137SNick Piggin } 7577e675137SNick Piggin 7587e675137SNick Piggin /* !HAVE_PTE_SPECIAL case follows: */ 7597e675137SNick Piggin 760b379d790SJared Hulbert if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { 761b379d790SJared Hulbert if (vma->vm_flags & VM_MIXEDMAP) { 762b379d790SJared Hulbert if (!pfn_valid(pfn)) 763b379d790SJared Hulbert return NULL; 764b379d790SJared Hulbert goto out; 765b379d790SJared Hulbert } else { 7667e675137SNick Piggin unsigned long off; 7677e675137SNick Piggin off = (addr - vma->vm_start) >> PAGE_SHIFT; 7686aab341eSLinus Torvalds if (pfn == vma->vm_pgoff + off) 7696aab341eSLinus Torvalds return NULL; 77067121172SLinus Torvalds if (!is_cow_mapping(vma->vm_flags)) 771fb155c16SLinus Torvalds return NULL; 7726aab341eSLinus Torvalds } 773b379d790SJared Hulbert } 7746aab341eSLinus Torvalds 775b38af472SHugh Dickins if (is_zero_pfn(pfn)) 776b38af472SHugh Dickins return NULL; 77722b31eecSHugh Dickins check_pfn: 77822b31eecSHugh Dickins if (unlikely(pfn > highest_memmap_pfn)) { 77922b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 78022b31eecSHugh Dickins return NULL; 78122b31eecSHugh Dickins } 7826aab341eSLinus Torvalds 7836aab341eSLinus Torvalds /* 7847e675137SNick Piggin * NOTE! We still have PageReserved() pages in the page tables. 7857e675137SNick Piggin * eg. VDSO mappings can cause them to exist. 7866aab341eSLinus Torvalds */ 787b379d790SJared Hulbert out: 7886aab341eSLinus Torvalds return pfn_to_page(pfn); 789ee498ed7SHugh Dickins } 790ee498ed7SHugh Dickins 791ee498ed7SHugh Dickins /* 7921da177e4SLinus Torvalds * copy one vm_area from one task to the other. Assumes the page tables 7931da177e4SLinus Torvalds * already present in the new task to be cleared in the whole range 7941da177e4SLinus Torvalds * covered by this vma. 7951da177e4SLinus Torvalds */ 7961da177e4SLinus Torvalds 797570a335bSHugh Dickins static inline unsigned long 7981da177e4SLinus Torvalds copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, 799b5810039SNick Piggin pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma, 8008c103762SHugh Dickins unsigned long addr, int *rss) 8011da177e4SLinus Torvalds { 802b5810039SNick Piggin unsigned long vm_flags = vma->vm_flags; 8031da177e4SLinus Torvalds pte_t pte = *src_pte; 8041da177e4SLinus Torvalds struct page *page; 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds /* pte contains position in swap or file, so copy. */ 8071da177e4SLinus Torvalds if (unlikely(!pte_present(pte))) { 8080697212aSChristoph Lameter swp_entry_t entry = pte_to_swp_entry(pte); 8090697212aSChristoph Lameter 8102022b4d1SHugh Dickins if (likely(!non_swap_entry(entry))) { 811570a335bSHugh Dickins if (swap_duplicate(entry) < 0) 812570a335bSHugh Dickins return entry.val; 813570a335bSHugh Dickins 8141da177e4SLinus Torvalds /* make sure dst_mm is on swapoff's mmlist. */ 8151da177e4SLinus Torvalds if (unlikely(list_empty(&dst_mm->mmlist))) { 8161da177e4SLinus Torvalds spin_lock(&mmlist_lock); 817f412ac08SHugh Dickins if (list_empty(&dst_mm->mmlist)) 818f412ac08SHugh Dickins list_add(&dst_mm->mmlist, 819f412ac08SHugh Dickins &src_mm->mmlist); 8201da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 8211da177e4SLinus Torvalds } 822b084d435SKAMEZAWA Hiroyuki rss[MM_SWAPENTS]++; 8232022b4d1SHugh Dickins } else if (is_migration_entry(entry)) { 8249f9f1acdSKonstantin Khlebnikov page = migration_entry_to_page(entry); 8259f9f1acdSKonstantin Khlebnikov 826eca56ff9SJerome Marchand rss[mm_counter(page)]++; 8279f9f1acdSKonstantin Khlebnikov 8289f9f1acdSKonstantin Khlebnikov if (is_write_migration_entry(entry) && 8290697212aSChristoph Lameter is_cow_mapping(vm_flags)) { 8300697212aSChristoph Lameter /* 8319f9f1acdSKonstantin Khlebnikov * COW mappings require pages in both 8329f9f1acdSKonstantin Khlebnikov * parent and child to be set to read. 8330697212aSChristoph Lameter */ 8340697212aSChristoph Lameter make_migration_entry_read(&entry); 8350697212aSChristoph Lameter pte = swp_entry_to_pte(entry); 836c3d16e16SCyrill Gorcunov if (pte_swp_soft_dirty(*src_pte)) 837c3d16e16SCyrill Gorcunov pte = pte_swp_mksoft_dirty(pte); 8380697212aSChristoph Lameter set_pte_at(src_mm, addr, src_pte, pte); 8390697212aSChristoph Lameter } 8401da177e4SLinus Torvalds } 841ae859762SHugh Dickins goto out_set_pte; 8421da177e4SLinus Torvalds } 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds /* 8451da177e4SLinus Torvalds * If it's a COW mapping, write protect it both 8461da177e4SLinus Torvalds * in the parent and the child 8471da177e4SLinus Torvalds */ 84867121172SLinus Torvalds if (is_cow_mapping(vm_flags)) { 8491da177e4SLinus Torvalds ptep_set_wrprotect(src_mm, addr, src_pte); 8503dc90795SZachary Amsden pte = pte_wrprotect(pte); 8511da177e4SLinus Torvalds } 8521da177e4SLinus Torvalds 8531da177e4SLinus Torvalds /* 8541da177e4SLinus Torvalds * If it's a shared mapping, mark it clean in 8551da177e4SLinus Torvalds * the child 8561da177e4SLinus Torvalds */ 8571da177e4SLinus Torvalds if (vm_flags & VM_SHARED) 8581da177e4SLinus Torvalds pte = pte_mkclean(pte); 8591da177e4SLinus Torvalds pte = pte_mkold(pte); 8606aab341eSLinus Torvalds 8616aab341eSLinus Torvalds page = vm_normal_page(vma, addr, pte); 8626aab341eSLinus Torvalds if (page) { 8631da177e4SLinus Torvalds get_page(page); 86453f9263bSKirill A. Shutemov page_dup_rmap(page, false); 865eca56ff9SJerome Marchand rss[mm_counter(page)]++; 8666aab341eSLinus Torvalds } 867ae859762SHugh Dickins 868ae859762SHugh Dickins out_set_pte: 869ae859762SHugh Dickins set_pte_at(dst_mm, addr, dst_pte, pte); 870570a335bSHugh Dickins return 0; 8711da177e4SLinus Torvalds } 8721da177e4SLinus Torvalds 87321bda264SJerome Marchand static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 8741da177e4SLinus Torvalds pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma, 8751da177e4SLinus Torvalds unsigned long addr, unsigned long end) 8761da177e4SLinus Torvalds { 877c36987e2SDaisuke Nishimura pte_t *orig_src_pte, *orig_dst_pte; 8781da177e4SLinus Torvalds pte_t *src_pte, *dst_pte; 879c74df32cSHugh Dickins spinlock_t *src_ptl, *dst_ptl; 880e040f218SHugh Dickins int progress = 0; 881d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 882570a335bSHugh Dickins swp_entry_t entry = (swp_entry_t){0}; 8831da177e4SLinus Torvalds 8841da177e4SLinus Torvalds again: 885d559db08SKAMEZAWA Hiroyuki init_rss_vec(rss); 886d559db08SKAMEZAWA Hiroyuki 887c74df32cSHugh Dickins dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl); 8881da177e4SLinus Torvalds if (!dst_pte) 8891da177e4SLinus Torvalds return -ENOMEM; 890ece0e2b6SPeter Zijlstra src_pte = pte_offset_map(src_pmd, addr); 8914c21e2f2SHugh Dickins src_ptl = pte_lockptr(src_mm, src_pmd); 892f20dc5f7SIngo Molnar spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 893c36987e2SDaisuke Nishimura orig_src_pte = src_pte; 894c36987e2SDaisuke Nishimura orig_dst_pte = dst_pte; 8956606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 8961da177e4SLinus Torvalds 8971da177e4SLinus Torvalds do { 8981da177e4SLinus Torvalds /* 8991da177e4SLinus Torvalds * We are holding two locks at this point - either of them 9001da177e4SLinus Torvalds * could generate latencies in another task on another CPU. 9011da177e4SLinus Torvalds */ 902e040f218SHugh Dickins if (progress >= 32) { 903e040f218SHugh Dickins progress = 0; 904e040f218SHugh Dickins if (need_resched() || 90595c354feSNick Piggin spin_needbreak(src_ptl) || spin_needbreak(dst_ptl)) 9061da177e4SLinus Torvalds break; 907e040f218SHugh Dickins } 9081da177e4SLinus Torvalds if (pte_none(*src_pte)) { 9091da177e4SLinus Torvalds progress++; 9101da177e4SLinus Torvalds continue; 9111da177e4SLinus Torvalds } 912570a335bSHugh Dickins entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte, 913570a335bSHugh Dickins vma, addr, rss); 914570a335bSHugh Dickins if (entry.val) 915570a335bSHugh Dickins break; 9161da177e4SLinus Torvalds progress += 8; 9171da177e4SLinus Torvalds } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end); 9181da177e4SLinus Torvalds 9196606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 920c74df32cSHugh Dickins spin_unlock(src_ptl); 921ece0e2b6SPeter Zijlstra pte_unmap(orig_src_pte); 922d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(dst_mm, rss); 923c36987e2SDaisuke Nishimura pte_unmap_unlock(orig_dst_pte, dst_ptl); 924c74df32cSHugh Dickins cond_resched(); 925570a335bSHugh Dickins 926570a335bSHugh Dickins if (entry.val) { 927570a335bSHugh Dickins if (add_swap_count_continuation(entry, GFP_KERNEL) < 0) 928570a335bSHugh Dickins return -ENOMEM; 929570a335bSHugh Dickins progress = 0; 930570a335bSHugh Dickins } 9311da177e4SLinus Torvalds if (addr != end) 9321da177e4SLinus Torvalds goto again; 9331da177e4SLinus Torvalds return 0; 9341da177e4SLinus Torvalds } 9351da177e4SLinus Torvalds 9361da177e4SLinus Torvalds static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 9371da177e4SLinus Torvalds pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma, 9381da177e4SLinus Torvalds unsigned long addr, unsigned long end) 9391da177e4SLinus Torvalds { 9401da177e4SLinus Torvalds pmd_t *src_pmd, *dst_pmd; 9411da177e4SLinus Torvalds unsigned long next; 9421da177e4SLinus Torvalds 9431da177e4SLinus Torvalds dst_pmd = pmd_alloc(dst_mm, dst_pud, addr); 9441da177e4SLinus Torvalds if (!dst_pmd) 9451da177e4SLinus Torvalds return -ENOMEM; 9461da177e4SLinus Torvalds src_pmd = pmd_offset(src_pud, addr); 9471da177e4SLinus Torvalds do { 9481da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 9495c7fb56eSDan Williams if (pmd_trans_huge(*src_pmd) || pmd_devmap(*src_pmd)) { 95071e3aac0SAndrea Arcangeli int err; 95114d1a55cSAndrea Arcangeli VM_BUG_ON(next-addr != HPAGE_PMD_SIZE); 95271e3aac0SAndrea Arcangeli err = copy_huge_pmd(dst_mm, src_mm, 95371e3aac0SAndrea Arcangeli dst_pmd, src_pmd, addr, vma); 95471e3aac0SAndrea Arcangeli if (err == -ENOMEM) 95571e3aac0SAndrea Arcangeli return -ENOMEM; 95671e3aac0SAndrea Arcangeli if (!err) 95771e3aac0SAndrea Arcangeli continue; 95871e3aac0SAndrea Arcangeli /* fall through */ 95971e3aac0SAndrea Arcangeli } 9601da177e4SLinus Torvalds if (pmd_none_or_clear_bad(src_pmd)) 9611da177e4SLinus Torvalds continue; 9621da177e4SLinus Torvalds if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd, 9631da177e4SLinus Torvalds vma, addr, next)) 9641da177e4SLinus Torvalds return -ENOMEM; 9651da177e4SLinus Torvalds } while (dst_pmd++, src_pmd++, addr = next, addr != end); 9661da177e4SLinus Torvalds return 0; 9671da177e4SLinus Torvalds } 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 9701da177e4SLinus Torvalds pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma, 9711da177e4SLinus Torvalds unsigned long addr, unsigned long end) 9721da177e4SLinus Torvalds { 9731da177e4SLinus Torvalds pud_t *src_pud, *dst_pud; 9741da177e4SLinus Torvalds unsigned long next; 9751da177e4SLinus Torvalds 9761da177e4SLinus Torvalds dst_pud = pud_alloc(dst_mm, dst_pgd, addr); 9771da177e4SLinus Torvalds if (!dst_pud) 9781da177e4SLinus Torvalds return -ENOMEM; 9791da177e4SLinus Torvalds src_pud = pud_offset(src_pgd, addr); 9801da177e4SLinus Torvalds do { 9811da177e4SLinus Torvalds next = pud_addr_end(addr, end); 9821da177e4SLinus Torvalds if (pud_none_or_clear_bad(src_pud)) 9831da177e4SLinus Torvalds continue; 9841da177e4SLinus Torvalds if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud, 9851da177e4SLinus Torvalds vma, addr, next)) 9861da177e4SLinus Torvalds return -ENOMEM; 9871da177e4SLinus Torvalds } while (dst_pud++, src_pud++, addr = next, addr != end); 9881da177e4SLinus Torvalds return 0; 9891da177e4SLinus Torvalds } 9901da177e4SLinus Torvalds 9911da177e4SLinus Torvalds int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 9921da177e4SLinus Torvalds struct vm_area_struct *vma) 9931da177e4SLinus Torvalds { 9941da177e4SLinus Torvalds pgd_t *src_pgd, *dst_pgd; 9951da177e4SLinus Torvalds unsigned long next; 9961da177e4SLinus Torvalds unsigned long addr = vma->vm_start; 9971da177e4SLinus Torvalds unsigned long end = vma->vm_end; 9982ec74c3eSSagi Grimberg unsigned long mmun_start; /* For mmu_notifiers */ 9992ec74c3eSSagi Grimberg unsigned long mmun_end; /* For mmu_notifiers */ 10002ec74c3eSSagi Grimberg bool is_cow; 1001cddb8a5cSAndrea Arcangeli int ret; 10021da177e4SLinus Torvalds 1003d992895bSNick Piggin /* 1004d992895bSNick Piggin * Don't copy ptes where a page fault will fill them correctly. 1005d992895bSNick Piggin * Fork becomes much lighter when there are big shared or private 1006d992895bSNick Piggin * readonly mappings. The tradeoff is that copy_page_range is more 1007d992895bSNick Piggin * efficient than faulting. 1008d992895bSNick Piggin */ 10090661a336SKirill A. Shutemov if (!(vma->vm_flags & (VM_HUGETLB | VM_PFNMAP | VM_MIXEDMAP)) && 10100661a336SKirill A. Shutemov !vma->anon_vma) 1011d992895bSNick Piggin return 0; 1012d992895bSNick Piggin 10131da177e4SLinus Torvalds if (is_vm_hugetlb_page(vma)) 10141da177e4SLinus Torvalds return copy_hugetlb_page_range(dst_mm, src_mm, vma); 10151da177e4SLinus Torvalds 1016b3b9c293SKonstantin Khlebnikov if (unlikely(vma->vm_flags & VM_PFNMAP)) { 10172ab64037Svenkatesh.pallipadi@intel.com /* 10182ab64037Svenkatesh.pallipadi@intel.com * We do not free on error cases below as remove_vma 10192ab64037Svenkatesh.pallipadi@intel.com * gets called on error from higher level routine 10202ab64037Svenkatesh.pallipadi@intel.com */ 10215180da41SSuresh Siddha ret = track_pfn_copy(vma); 10222ab64037Svenkatesh.pallipadi@intel.com if (ret) 10232ab64037Svenkatesh.pallipadi@intel.com return ret; 10242ab64037Svenkatesh.pallipadi@intel.com } 10252ab64037Svenkatesh.pallipadi@intel.com 1026cddb8a5cSAndrea Arcangeli /* 1027cddb8a5cSAndrea Arcangeli * We need to invalidate the secondary MMU mappings only when 1028cddb8a5cSAndrea Arcangeli * there could be a permission downgrade on the ptes of the 1029cddb8a5cSAndrea Arcangeli * parent mm. And a permission downgrade will only happen if 1030cddb8a5cSAndrea Arcangeli * is_cow_mapping() returns true. 1031cddb8a5cSAndrea Arcangeli */ 10322ec74c3eSSagi Grimberg is_cow = is_cow_mapping(vma->vm_flags); 10332ec74c3eSSagi Grimberg mmun_start = addr; 10342ec74c3eSSagi Grimberg mmun_end = end; 10352ec74c3eSSagi Grimberg if (is_cow) 10362ec74c3eSSagi Grimberg mmu_notifier_invalidate_range_start(src_mm, mmun_start, 10372ec74c3eSSagi Grimberg mmun_end); 1038cddb8a5cSAndrea Arcangeli 1039cddb8a5cSAndrea Arcangeli ret = 0; 10401da177e4SLinus Torvalds dst_pgd = pgd_offset(dst_mm, addr); 10411da177e4SLinus Torvalds src_pgd = pgd_offset(src_mm, addr); 10421da177e4SLinus Torvalds do { 10431da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 10441da177e4SLinus Torvalds if (pgd_none_or_clear_bad(src_pgd)) 10451da177e4SLinus Torvalds continue; 1046cddb8a5cSAndrea Arcangeli if (unlikely(copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd, 1047cddb8a5cSAndrea Arcangeli vma, addr, next))) { 1048cddb8a5cSAndrea Arcangeli ret = -ENOMEM; 1049cddb8a5cSAndrea Arcangeli break; 1050cddb8a5cSAndrea Arcangeli } 10511da177e4SLinus Torvalds } while (dst_pgd++, src_pgd++, addr = next, addr != end); 1052cddb8a5cSAndrea Arcangeli 10532ec74c3eSSagi Grimberg if (is_cow) 10542ec74c3eSSagi Grimberg mmu_notifier_invalidate_range_end(src_mm, mmun_start, mmun_end); 1055cddb8a5cSAndrea Arcangeli return ret; 10561da177e4SLinus Torvalds } 10571da177e4SLinus Torvalds 105851c6f666SRobin Holt static unsigned long zap_pte_range(struct mmu_gather *tlb, 1059b5810039SNick Piggin struct vm_area_struct *vma, pmd_t *pmd, 10601da177e4SLinus Torvalds unsigned long addr, unsigned long end, 106197a89413SPeter Zijlstra struct zap_details *details) 10621da177e4SLinus Torvalds { 1063b5810039SNick Piggin struct mm_struct *mm = tlb->mm; 1064d16dfc55SPeter Zijlstra int force_flush = 0; 1065d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 106697a89413SPeter Zijlstra spinlock_t *ptl; 10675f1a1907SSteven Rostedt pte_t *start_pte; 106897a89413SPeter Zijlstra pte_t *pte; 10698a5f14a2SKirill A. Shutemov swp_entry_t entry; 1070d559db08SKAMEZAWA Hiroyuki 1071d16dfc55SPeter Zijlstra again: 1072e303297eSPeter Zijlstra init_rss_vec(rss); 10735f1a1907SSteven Rostedt start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl); 10745f1a1907SSteven Rostedt pte = start_pte; 10756606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 10761da177e4SLinus Torvalds do { 10771da177e4SLinus Torvalds pte_t ptent = *pte; 107851c6f666SRobin Holt if (pte_none(ptent)) { 10791da177e4SLinus Torvalds continue; 108051c6f666SRobin Holt } 108151c6f666SRobin Holt 10826f5e6b9eSHugh Dickins if (pte_present(ptent)) { 10836f5e6b9eSHugh Dickins struct page *page; 10846f5e6b9eSHugh Dickins 10856aab341eSLinus Torvalds page = vm_normal_page(vma, addr, ptent); 10861da177e4SLinus Torvalds if (unlikely(details) && page) { 10871da177e4SLinus Torvalds /* 10881da177e4SLinus Torvalds * unmap_shared_mapping_pages() wants to 10891da177e4SLinus Torvalds * invalidate cache without truncating: 10901da177e4SLinus Torvalds * unmap shared but keep private pages. 10911da177e4SLinus Torvalds */ 10921da177e4SLinus Torvalds if (details->check_mapping && 10931da177e4SLinus Torvalds details->check_mapping != page->mapping) 10941da177e4SLinus Torvalds continue; 10951da177e4SLinus Torvalds } 1096b5810039SNick Piggin ptent = ptep_get_and_clear_full(mm, addr, pte, 1097a600388dSZachary Amsden tlb->fullmm); 10981da177e4SLinus Torvalds tlb_remove_tlb_entry(tlb, pte, addr); 10991da177e4SLinus Torvalds if (unlikely(!page)) 11001da177e4SLinus Torvalds continue; 1101eca56ff9SJerome Marchand 1102eca56ff9SJerome Marchand if (!PageAnon(page)) { 11031cf35d47SLinus Torvalds if (pte_dirty(ptent)) { 11041cf35d47SLinus Torvalds force_flush = 1; 11056237bcd9SHugh Dickins set_page_dirty(page); 11061cf35d47SLinus Torvalds } 11074917e5d0SJohannes Weiner if (pte_young(ptent) && 110864363aadSJoe Perches likely(!(vma->vm_flags & VM_SEQ_READ))) 1109bf3f3bc5SNick Piggin mark_page_accessed(page); 11106237bcd9SHugh Dickins } 1111eca56ff9SJerome Marchand rss[mm_counter(page)]--; 1112d281ee61SKirill A. Shutemov page_remove_rmap(page, false); 11133dc14741SHugh Dickins if (unlikely(page_mapcount(page) < 0)) 11143dc14741SHugh Dickins print_bad_pte(vma, addr, ptent, page); 11151cf35d47SLinus Torvalds if (unlikely(!__tlb_remove_page(tlb, page))) { 11161cf35d47SLinus Torvalds force_flush = 1; 1117ce9ec37bSWill Deacon addr += PAGE_SIZE; 1118d16dfc55SPeter Zijlstra break; 11191cf35d47SLinus Torvalds } 11201da177e4SLinus Torvalds continue; 11211da177e4SLinus Torvalds } 11228a5f14a2SKirill A. Shutemov /* If details->check_mapping, we leave swap entries. */ 11231da177e4SLinus Torvalds if (unlikely(details)) 11241da177e4SLinus Torvalds continue; 1125b084d435SKAMEZAWA Hiroyuki 11268a5f14a2SKirill A. Shutemov entry = pte_to_swp_entry(ptent); 1127b084d435SKAMEZAWA Hiroyuki if (!non_swap_entry(entry)) 1128b084d435SKAMEZAWA Hiroyuki rss[MM_SWAPENTS]--; 11299f9f1acdSKonstantin Khlebnikov else if (is_migration_entry(entry)) { 11309f9f1acdSKonstantin Khlebnikov struct page *page; 11319f9f1acdSKonstantin Khlebnikov 11329f9f1acdSKonstantin Khlebnikov page = migration_entry_to_page(entry); 1133eca56ff9SJerome Marchand rss[mm_counter(page)]--; 11349f9f1acdSKonstantin Khlebnikov } 1135b084d435SKAMEZAWA Hiroyuki if (unlikely(!free_swap_and_cache(entry))) 11362509ef26SHugh Dickins print_bad_pte(vma, addr, ptent, NULL); 11379888a1caSZachary Amsden pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); 113897a89413SPeter Zijlstra } while (pte++, addr += PAGE_SIZE, addr != end); 1139ae859762SHugh Dickins 1140d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(mm, rss); 11416606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 114251c6f666SRobin Holt 11431cf35d47SLinus Torvalds /* Do the actual TLB flush before dropping ptl */ 1144fb7332a9SWill Deacon if (force_flush) 11451cf35d47SLinus Torvalds tlb_flush_mmu_tlbonly(tlb); 11461cf35d47SLinus Torvalds pte_unmap_unlock(start_pte, ptl); 11471cf35d47SLinus Torvalds 11481cf35d47SLinus Torvalds /* 11491cf35d47SLinus Torvalds * If we forced a TLB flush (either due to running out of 11501cf35d47SLinus Torvalds * batch buffers or because we needed to flush dirty TLB 11511cf35d47SLinus Torvalds * entries before releasing the ptl), free the batched 11521cf35d47SLinus Torvalds * memory too. Restart if we didn't do everything. 11531cf35d47SLinus Torvalds */ 11541cf35d47SLinus Torvalds if (force_flush) { 11551cf35d47SLinus Torvalds force_flush = 0; 11561cf35d47SLinus Torvalds tlb_flush_mmu_free(tlb); 11572b047252SLinus Torvalds 11582b047252SLinus Torvalds if (addr != end) 1159d16dfc55SPeter Zijlstra goto again; 1160d16dfc55SPeter Zijlstra } 1161d16dfc55SPeter Zijlstra 116251c6f666SRobin Holt return addr; 11631da177e4SLinus Torvalds } 11641da177e4SLinus Torvalds 116551c6f666SRobin Holt static inline unsigned long zap_pmd_range(struct mmu_gather *tlb, 1166b5810039SNick Piggin struct vm_area_struct *vma, pud_t *pud, 11671da177e4SLinus Torvalds unsigned long addr, unsigned long end, 116897a89413SPeter Zijlstra struct zap_details *details) 11691da177e4SLinus Torvalds { 11701da177e4SLinus Torvalds pmd_t *pmd; 11711da177e4SLinus Torvalds unsigned long next; 11721da177e4SLinus Torvalds 11731da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 11741da177e4SLinus Torvalds do { 11751da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 11765c7fb56eSDan Williams if (pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) { 117714d1a55cSAndrea Arcangeli if (next - addr != HPAGE_PMD_SIZE) { 1178e0897d75SDavid Rientjes #ifdef CONFIG_DEBUG_VM 1179e0897d75SDavid Rientjes if (!rwsem_is_locked(&tlb->mm->mmap_sem)) { 1180e0897d75SDavid Rientjes pr_err("%s: mmap_sem is unlocked! addr=0x%lx end=0x%lx vma->vm_start=0x%lx vma->vm_end=0x%lx\n", 1181e0897d75SDavid Rientjes __func__, addr, end, 1182e0897d75SDavid Rientjes vma->vm_start, 1183e0897d75SDavid Rientjes vma->vm_end); 1184e0897d75SDavid Rientjes BUG(); 1185e0897d75SDavid Rientjes } 1186e0897d75SDavid Rientjes #endif 118778ddc534SKirill A. Shutemov split_huge_pmd(vma, pmd, addr); 1188f21760b1SShaohua Li } else if (zap_huge_pmd(tlb, vma, pmd, addr)) 11891a5a9906SAndrea Arcangeli goto next; 119071e3aac0SAndrea Arcangeli /* fall through */ 119171e3aac0SAndrea Arcangeli } 11921a5a9906SAndrea Arcangeli /* 11931a5a9906SAndrea Arcangeli * Here there can be other concurrent MADV_DONTNEED or 11941a5a9906SAndrea Arcangeli * trans huge page faults running, and if the pmd is 11951a5a9906SAndrea Arcangeli * none or trans huge it can change under us. This is 11961a5a9906SAndrea Arcangeli * because MADV_DONTNEED holds the mmap_sem in read 11971a5a9906SAndrea Arcangeli * mode. 11981a5a9906SAndrea Arcangeli */ 11991a5a9906SAndrea Arcangeli if (pmd_none_or_trans_huge_or_clear_bad(pmd)) 12001a5a9906SAndrea Arcangeli goto next; 120197a89413SPeter Zijlstra next = zap_pte_range(tlb, vma, pmd, addr, next, details); 12021a5a9906SAndrea Arcangeli next: 120397a89413SPeter Zijlstra cond_resched(); 120497a89413SPeter Zijlstra } while (pmd++, addr = next, addr != end); 120551c6f666SRobin Holt 120651c6f666SRobin Holt return addr; 12071da177e4SLinus Torvalds } 12081da177e4SLinus Torvalds 120951c6f666SRobin Holt static inline unsigned long zap_pud_range(struct mmu_gather *tlb, 1210b5810039SNick Piggin struct vm_area_struct *vma, pgd_t *pgd, 12111da177e4SLinus Torvalds unsigned long addr, unsigned long end, 121297a89413SPeter Zijlstra struct zap_details *details) 12131da177e4SLinus Torvalds { 12141da177e4SLinus Torvalds pud_t *pud; 12151da177e4SLinus Torvalds unsigned long next; 12161da177e4SLinus Torvalds 12171da177e4SLinus Torvalds pud = pud_offset(pgd, addr); 12181da177e4SLinus Torvalds do { 12191da177e4SLinus Torvalds next = pud_addr_end(addr, end); 122097a89413SPeter Zijlstra if (pud_none_or_clear_bad(pud)) 12211da177e4SLinus Torvalds continue; 122297a89413SPeter Zijlstra next = zap_pmd_range(tlb, vma, pud, addr, next, details); 122397a89413SPeter Zijlstra } while (pud++, addr = next, addr != end); 122451c6f666SRobin Holt 122551c6f666SRobin Holt return addr; 12261da177e4SLinus Torvalds } 12271da177e4SLinus Torvalds 1228038c7aa1SAl Viro static void unmap_page_range(struct mmu_gather *tlb, 122951c6f666SRobin Holt struct vm_area_struct *vma, 12301da177e4SLinus Torvalds unsigned long addr, unsigned long end, 123197a89413SPeter Zijlstra struct zap_details *details) 12321da177e4SLinus Torvalds { 12331da177e4SLinus Torvalds pgd_t *pgd; 12341da177e4SLinus Torvalds unsigned long next; 12351da177e4SLinus Torvalds 12368a5f14a2SKirill A. Shutemov if (details && !details->check_mapping) 12371da177e4SLinus Torvalds details = NULL; 12381da177e4SLinus Torvalds 12391da177e4SLinus Torvalds BUG_ON(addr >= end); 12401da177e4SLinus Torvalds tlb_start_vma(tlb, vma); 12411da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 12421da177e4SLinus Torvalds do { 12431da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 124497a89413SPeter Zijlstra if (pgd_none_or_clear_bad(pgd)) 12451da177e4SLinus Torvalds continue; 124697a89413SPeter Zijlstra next = zap_pud_range(tlb, vma, pgd, addr, next, details); 124797a89413SPeter Zijlstra } while (pgd++, addr = next, addr != end); 12481da177e4SLinus Torvalds tlb_end_vma(tlb, vma); 12491da177e4SLinus Torvalds } 12501da177e4SLinus Torvalds 1251f5cc4eefSAl Viro 1252f5cc4eefSAl Viro static void unmap_single_vma(struct mmu_gather *tlb, 12531da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long start_addr, 12544f74d2c8SLinus Torvalds unsigned long end_addr, 12551da177e4SLinus Torvalds struct zap_details *details) 12561da177e4SLinus Torvalds { 1257f5cc4eefSAl Viro unsigned long start = max(vma->vm_start, start_addr); 12581da177e4SLinus Torvalds unsigned long end; 12591da177e4SLinus Torvalds 12601da177e4SLinus Torvalds if (start >= vma->vm_end) 1261f5cc4eefSAl Viro return; 12621da177e4SLinus Torvalds end = min(vma->vm_end, end_addr); 12631da177e4SLinus Torvalds if (end <= vma->vm_start) 1264f5cc4eefSAl Viro return; 12651da177e4SLinus Torvalds 1266cbc91f71SSrikar Dronamraju if (vma->vm_file) 1267cbc91f71SSrikar Dronamraju uprobe_munmap(vma, start, end); 1268cbc91f71SSrikar Dronamraju 1269b3b9c293SKonstantin Khlebnikov if (unlikely(vma->vm_flags & VM_PFNMAP)) 12705180da41SSuresh Siddha untrack_pfn(vma, 0, 0); 12712ab64037Svenkatesh.pallipadi@intel.com 12728b2a1238SAl Viro if (start != end) { 127351c6f666SRobin Holt if (unlikely(is_vm_hugetlb_page(vma))) { 1274a137e1ccSAndi Kleen /* 1275a137e1ccSAndi Kleen * It is undesirable to test vma->vm_file as it 1276a137e1ccSAndi Kleen * should be non-null for valid hugetlb area. 1277a137e1ccSAndi Kleen * However, vm_file will be NULL in the error 12787aa6b4adSDavidlohr Bueso * cleanup path of mmap_region. When 1279a137e1ccSAndi Kleen * hugetlbfs ->mmap method fails, 12807aa6b4adSDavidlohr Bueso * mmap_region() nullifies vma->vm_file 1281a137e1ccSAndi Kleen * before calling this function to clean up. 1282a137e1ccSAndi Kleen * Since no pte has actually been setup, it is 1283a137e1ccSAndi Kleen * safe to do nothing in this case. 1284a137e1ccSAndi Kleen */ 128524669e58SAneesh Kumar K.V if (vma->vm_file) { 128683cde9e8SDavidlohr Bueso i_mmap_lock_write(vma->vm_file->f_mapping); 1287d833352aSMel Gorman __unmap_hugepage_range_final(tlb, vma, start, end, NULL); 128883cde9e8SDavidlohr Bueso i_mmap_unlock_write(vma->vm_file->f_mapping); 128924669e58SAneesh Kumar K.V } 129051c6f666SRobin Holt } else 1291038c7aa1SAl Viro unmap_page_range(tlb, vma, start, end, details); 129297a89413SPeter Zijlstra } 129351c6f666SRobin Holt } 12941da177e4SLinus Torvalds 1295f5cc4eefSAl Viro /** 1296f5cc4eefSAl Viro * unmap_vmas - unmap a range of memory covered by a list of vma's 1297f5cc4eefSAl Viro * @tlb: address of the caller's struct mmu_gather 1298f5cc4eefSAl Viro * @vma: the starting vma 1299f5cc4eefSAl Viro * @start_addr: virtual address at which to start unmapping 1300f5cc4eefSAl Viro * @end_addr: virtual address at which to end unmapping 1301f5cc4eefSAl Viro * 1302f5cc4eefSAl Viro * Unmap all pages in the vma list. 1303f5cc4eefSAl Viro * 1304f5cc4eefSAl Viro * Only addresses between `start' and `end' will be unmapped. 1305f5cc4eefSAl Viro * 1306f5cc4eefSAl Viro * The VMA list must be sorted in ascending virtual address order. 1307f5cc4eefSAl Viro * 1308f5cc4eefSAl Viro * unmap_vmas() assumes that the caller will flush the whole unmapped address 1309f5cc4eefSAl Viro * range after unmap_vmas() returns. So the only responsibility here is to 1310f5cc4eefSAl Viro * ensure that any thus-far unmapped pages are flushed before unmap_vmas() 1311f5cc4eefSAl Viro * drops the lock and schedules. 1312f5cc4eefSAl Viro */ 1313f5cc4eefSAl Viro void unmap_vmas(struct mmu_gather *tlb, 1314f5cc4eefSAl Viro struct vm_area_struct *vma, unsigned long start_addr, 13154f74d2c8SLinus Torvalds unsigned long end_addr) 1316f5cc4eefSAl Viro { 1317f5cc4eefSAl Viro struct mm_struct *mm = vma->vm_mm; 1318f5cc4eefSAl Viro 1319f5cc4eefSAl Viro mmu_notifier_invalidate_range_start(mm, start_addr, end_addr); 1320f5cc4eefSAl Viro for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) 13214f74d2c8SLinus Torvalds unmap_single_vma(tlb, vma, start_addr, end_addr, NULL); 1322cddb8a5cSAndrea Arcangeli mmu_notifier_invalidate_range_end(mm, start_addr, end_addr); 13231da177e4SLinus Torvalds } 13241da177e4SLinus Torvalds 13251da177e4SLinus Torvalds /** 13261da177e4SLinus Torvalds * zap_page_range - remove user pages in a given range 13271da177e4SLinus Torvalds * @vma: vm_area_struct holding the applicable pages 1328eb4546bbSRandy Dunlap * @start: starting address of pages to zap 13291da177e4SLinus Torvalds * @size: number of bytes to zap 13308a5f14a2SKirill A. Shutemov * @details: details of shared cache invalidation 1331f5cc4eefSAl Viro * 1332f5cc4eefSAl Viro * Caller must protect the VMA list 13331da177e4SLinus Torvalds */ 13347e027b14SLinus Torvalds void zap_page_range(struct vm_area_struct *vma, unsigned long start, 13351da177e4SLinus Torvalds unsigned long size, struct zap_details *details) 13361da177e4SLinus Torvalds { 13371da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 1338d16dfc55SPeter Zijlstra struct mmu_gather tlb; 13397e027b14SLinus Torvalds unsigned long end = start + size; 13401da177e4SLinus Torvalds 13411da177e4SLinus Torvalds lru_add_drain(); 13422b047252SLinus Torvalds tlb_gather_mmu(&tlb, mm, start, end); 1343365e9c87SHugh Dickins update_hiwater_rss(mm); 13447e027b14SLinus Torvalds mmu_notifier_invalidate_range_start(mm, start, end); 13457e027b14SLinus Torvalds for ( ; vma && vma->vm_start < end; vma = vma->vm_next) 13464f74d2c8SLinus Torvalds unmap_single_vma(&tlb, vma, start, end, details); 13477e027b14SLinus Torvalds mmu_notifier_invalidate_range_end(mm, start, end); 13487e027b14SLinus Torvalds tlb_finish_mmu(&tlb, start, end); 13491da177e4SLinus Torvalds } 13501da177e4SLinus Torvalds 1351c627f9ccSJack Steiner /** 1352f5cc4eefSAl Viro * zap_page_range_single - remove user pages in a given range 1353f5cc4eefSAl Viro * @vma: vm_area_struct holding the applicable pages 1354f5cc4eefSAl Viro * @address: starting address of pages to zap 1355f5cc4eefSAl Viro * @size: number of bytes to zap 13568a5f14a2SKirill A. Shutemov * @details: details of shared cache invalidation 1357f5cc4eefSAl Viro * 1358f5cc4eefSAl Viro * The range must fit into one VMA. 1359f5cc4eefSAl Viro */ 1360f5cc4eefSAl Viro static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address, 1361f5cc4eefSAl Viro unsigned long size, struct zap_details *details) 1362f5cc4eefSAl Viro { 1363f5cc4eefSAl Viro struct mm_struct *mm = vma->vm_mm; 1364f5cc4eefSAl Viro struct mmu_gather tlb; 1365f5cc4eefSAl Viro unsigned long end = address + size; 1366f5cc4eefSAl Viro 1367f5cc4eefSAl Viro lru_add_drain(); 13682b047252SLinus Torvalds tlb_gather_mmu(&tlb, mm, address, end); 1369f5cc4eefSAl Viro update_hiwater_rss(mm); 1370f5cc4eefSAl Viro mmu_notifier_invalidate_range_start(mm, address, end); 13714f74d2c8SLinus Torvalds unmap_single_vma(&tlb, vma, address, end, details); 1372f5cc4eefSAl Viro mmu_notifier_invalidate_range_end(mm, address, end); 1373f5cc4eefSAl Viro tlb_finish_mmu(&tlb, address, end); 13741da177e4SLinus Torvalds } 13751da177e4SLinus Torvalds 1376c627f9ccSJack Steiner /** 1377c627f9ccSJack Steiner * zap_vma_ptes - remove ptes mapping the vma 1378c627f9ccSJack Steiner * @vma: vm_area_struct holding ptes to be zapped 1379c627f9ccSJack Steiner * @address: starting address of pages to zap 1380c627f9ccSJack Steiner * @size: number of bytes to zap 1381c627f9ccSJack Steiner * 1382c627f9ccSJack Steiner * This function only unmaps ptes assigned to VM_PFNMAP vmas. 1383c627f9ccSJack Steiner * 1384c627f9ccSJack Steiner * The entire address range must be fully contained within the vma. 1385c627f9ccSJack Steiner * 1386c627f9ccSJack Steiner * Returns 0 if successful. 1387c627f9ccSJack Steiner */ 1388c627f9ccSJack Steiner int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, 1389c627f9ccSJack Steiner unsigned long size) 1390c627f9ccSJack Steiner { 1391c627f9ccSJack Steiner if (address < vma->vm_start || address + size > vma->vm_end || 1392c627f9ccSJack Steiner !(vma->vm_flags & VM_PFNMAP)) 1393c627f9ccSJack Steiner return -1; 1394f5cc4eefSAl Viro zap_page_range_single(vma, address, size, NULL); 1395c627f9ccSJack Steiner return 0; 1396c627f9ccSJack Steiner } 1397c627f9ccSJack Steiner EXPORT_SYMBOL_GPL(zap_vma_ptes); 1398c627f9ccSJack Steiner 139925ca1d6cSNamhyung Kim pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr, 1400920c7a5dSHarvey Harrison spinlock_t **ptl) 1401c9cfcddfSLinus Torvalds { 1402c9cfcddfSLinus Torvalds pgd_t * pgd = pgd_offset(mm, addr); 1403c9cfcddfSLinus Torvalds pud_t * pud = pud_alloc(mm, pgd, addr); 1404c9cfcddfSLinus Torvalds if (pud) { 140549c91fb0STrond Myklebust pmd_t * pmd = pmd_alloc(mm, pud, addr); 1406f66055abSAndrea Arcangeli if (pmd) { 1407f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 1408c9cfcddfSLinus Torvalds return pte_alloc_map_lock(mm, pmd, addr, ptl); 1409c9cfcddfSLinus Torvalds } 1410f66055abSAndrea Arcangeli } 1411c9cfcddfSLinus Torvalds return NULL; 1412c9cfcddfSLinus Torvalds } 1413c9cfcddfSLinus Torvalds 14141da177e4SLinus Torvalds /* 1415238f58d8SLinus Torvalds * This is the old fallback for page remapping. 1416238f58d8SLinus Torvalds * 1417238f58d8SLinus Torvalds * For historical reasons, it only allows reserved pages. Only 1418238f58d8SLinus Torvalds * old drivers should use this, and they needed to mark their 1419238f58d8SLinus Torvalds * pages reserved for the old functions anyway. 1420238f58d8SLinus Torvalds */ 1421423bad60SNick Piggin static int insert_page(struct vm_area_struct *vma, unsigned long addr, 1422423bad60SNick Piggin struct page *page, pgprot_t prot) 1423238f58d8SLinus Torvalds { 1424423bad60SNick Piggin struct mm_struct *mm = vma->vm_mm; 1425238f58d8SLinus Torvalds int retval; 1426238f58d8SLinus Torvalds pte_t *pte; 1427238f58d8SLinus Torvalds spinlock_t *ptl; 1428238f58d8SLinus Torvalds 1429238f58d8SLinus Torvalds retval = -EINVAL; 1430a145dd41SLinus Torvalds if (PageAnon(page)) 14315b4e655eSKAMEZAWA Hiroyuki goto out; 1432238f58d8SLinus Torvalds retval = -ENOMEM; 1433238f58d8SLinus Torvalds flush_dcache_page(page); 1434c9cfcddfSLinus Torvalds pte = get_locked_pte(mm, addr, &ptl); 1435238f58d8SLinus Torvalds if (!pte) 14365b4e655eSKAMEZAWA Hiroyuki goto out; 1437238f58d8SLinus Torvalds retval = -EBUSY; 1438238f58d8SLinus Torvalds if (!pte_none(*pte)) 1439238f58d8SLinus Torvalds goto out_unlock; 1440238f58d8SLinus Torvalds 1441238f58d8SLinus Torvalds /* Ok, finally just insert the thing.. */ 1442238f58d8SLinus Torvalds get_page(page); 1443eca56ff9SJerome Marchand inc_mm_counter_fast(mm, mm_counter_file(page)); 1444238f58d8SLinus Torvalds page_add_file_rmap(page); 1445238f58d8SLinus Torvalds set_pte_at(mm, addr, pte, mk_pte(page, prot)); 1446238f58d8SLinus Torvalds 1447238f58d8SLinus Torvalds retval = 0; 14488a9f3ccdSBalbir Singh pte_unmap_unlock(pte, ptl); 14498a9f3ccdSBalbir Singh return retval; 1450238f58d8SLinus Torvalds out_unlock: 1451238f58d8SLinus Torvalds pte_unmap_unlock(pte, ptl); 1452238f58d8SLinus Torvalds out: 1453238f58d8SLinus Torvalds return retval; 1454238f58d8SLinus Torvalds } 1455238f58d8SLinus Torvalds 1456bfa5bf6dSRolf Eike Beer /** 1457bfa5bf6dSRolf Eike Beer * vm_insert_page - insert single page into user vma 1458bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 1459bfa5bf6dSRolf Eike Beer * @addr: target user address of this page 1460bfa5bf6dSRolf Eike Beer * @page: source kernel page 1461bfa5bf6dSRolf Eike Beer * 1462a145dd41SLinus Torvalds * This allows drivers to insert individual pages they've allocated 1463a145dd41SLinus Torvalds * into a user vma. 1464a145dd41SLinus Torvalds * 1465a145dd41SLinus Torvalds * The page has to be a nice clean _individual_ kernel allocation. 1466a145dd41SLinus Torvalds * If you allocate a compound page, you need to have marked it as 1467a145dd41SLinus Torvalds * such (__GFP_COMP), or manually just split the page up yourself 14688dfcc9baSNick Piggin * (see split_page()). 1469a145dd41SLinus Torvalds * 1470a145dd41SLinus Torvalds * NOTE! Traditionally this was done with "remap_pfn_range()" which 1471a145dd41SLinus Torvalds * took an arbitrary page protection parameter. This doesn't allow 1472a145dd41SLinus Torvalds * that. Your vma protection will have to be set up correctly, which 1473a145dd41SLinus Torvalds * means that if you want a shared writable mapping, you'd better 1474a145dd41SLinus Torvalds * ask for a shared writable mapping! 1475a145dd41SLinus Torvalds * 1476a145dd41SLinus Torvalds * The page does not need to be reserved. 14774b6e1e37SKonstantin Khlebnikov * 14784b6e1e37SKonstantin Khlebnikov * Usually this function is called from f_op->mmap() handler 14794b6e1e37SKonstantin Khlebnikov * under mm->mmap_sem write-lock, so it can change vma->vm_flags. 14804b6e1e37SKonstantin Khlebnikov * Caller must set VM_MIXEDMAP on vma if it wants to call this 14814b6e1e37SKonstantin Khlebnikov * function from other places, for example from page-fault handler. 1482a145dd41SLinus Torvalds */ 1483423bad60SNick Piggin int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, 1484423bad60SNick Piggin struct page *page) 1485a145dd41SLinus Torvalds { 1486a145dd41SLinus Torvalds if (addr < vma->vm_start || addr >= vma->vm_end) 1487a145dd41SLinus Torvalds return -EFAULT; 1488a145dd41SLinus Torvalds if (!page_count(page)) 1489a145dd41SLinus Torvalds return -EINVAL; 14904b6e1e37SKonstantin Khlebnikov if (!(vma->vm_flags & VM_MIXEDMAP)) { 14914b6e1e37SKonstantin Khlebnikov BUG_ON(down_read_trylock(&vma->vm_mm->mmap_sem)); 14924b6e1e37SKonstantin Khlebnikov BUG_ON(vma->vm_flags & VM_PFNMAP); 14934b6e1e37SKonstantin Khlebnikov vma->vm_flags |= VM_MIXEDMAP; 14944b6e1e37SKonstantin Khlebnikov } 1495423bad60SNick Piggin return insert_page(vma, addr, page, vma->vm_page_prot); 1496a145dd41SLinus Torvalds } 1497e3c3374fSLinus Torvalds EXPORT_SYMBOL(vm_insert_page); 1498a145dd41SLinus Torvalds 1499423bad60SNick Piggin static int insert_pfn(struct vm_area_struct *vma, unsigned long addr, 150001c8f1c4SDan Williams pfn_t pfn, pgprot_t prot) 1501423bad60SNick Piggin { 1502423bad60SNick Piggin struct mm_struct *mm = vma->vm_mm; 1503423bad60SNick Piggin int retval; 1504423bad60SNick Piggin pte_t *pte, entry; 1505423bad60SNick Piggin spinlock_t *ptl; 1506423bad60SNick Piggin 1507423bad60SNick Piggin retval = -ENOMEM; 1508423bad60SNick Piggin pte = get_locked_pte(mm, addr, &ptl); 1509423bad60SNick Piggin if (!pte) 1510423bad60SNick Piggin goto out; 1511423bad60SNick Piggin retval = -EBUSY; 1512423bad60SNick Piggin if (!pte_none(*pte)) 1513423bad60SNick Piggin goto out_unlock; 1514423bad60SNick Piggin 1515423bad60SNick Piggin /* Ok, finally just insert the thing.. */ 151601c8f1c4SDan Williams if (pfn_t_devmap(pfn)) 151701c8f1c4SDan Williams entry = pte_mkdevmap(pfn_t_pte(pfn, prot)); 151801c8f1c4SDan Williams else 151901c8f1c4SDan Williams entry = pte_mkspecial(pfn_t_pte(pfn, prot)); 1520423bad60SNick Piggin set_pte_at(mm, addr, pte, entry); 15214b3073e1SRussell King update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */ 1522423bad60SNick Piggin 1523423bad60SNick Piggin retval = 0; 1524423bad60SNick Piggin out_unlock: 1525423bad60SNick Piggin pte_unmap_unlock(pte, ptl); 1526423bad60SNick Piggin out: 1527423bad60SNick Piggin return retval; 1528423bad60SNick Piggin } 1529423bad60SNick Piggin 1530e0dc0d8fSNick Piggin /** 1531e0dc0d8fSNick Piggin * vm_insert_pfn - insert single pfn into user vma 1532e0dc0d8fSNick Piggin * @vma: user vma to map to 1533e0dc0d8fSNick Piggin * @addr: target user address of this page 1534e0dc0d8fSNick Piggin * @pfn: source kernel pfn 1535e0dc0d8fSNick Piggin * 1536c462f179SRobert P. J. Day * Similar to vm_insert_page, this allows drivers to insert individual pages 1537e0dc0d8fSNick Piggin * they've allocated into a user vma. Same comments apply. 1538e0dc0d8fSNick Piggin * 1539e0dc0d8fSNick Piggin * This function should only be called from a vm_ops->fault handler, and 1540e0dc0d8fSNick Piggin * in that case the handler should return NULL. 15410d71d10aSNick Piggin * 15420d71d10aSNick Piggin * vma cannot be a COW mapping. 15430d71d10aSNick Piggin * 15440d71d10aSNick Piggin * As this is called only for pages that do not currently exist, we 15450d71d10aSNick Piggin * do not need to flush old virtual caches or the TLB. 1546e0dc0d8fSNick Piggin */ 1547e0dc0d8fSNick Piggin int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr, 1548e0dc0d8fSNick Piggin unsigned long pfn) 1549e0dc0d8fSNick Piggin { 15501745cbc5SAndy Lutomirski return vm_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot); 15511745cbc5SAndy Lutomirski } 15521745cbc5SAndy Lutomirski EXPORT_SYMBOL(vm_insert_pfn); 15531745cbc5SAndy Lutomirski 15541745cbc5SAndy Lutomirski /** 15551745cbc5SAndy Lutomirski * vm_insert_pfn_prot - insert single pfn into user vma with specified pgprot 15561745cbc5SAndy Lutomirski * @vma: user vma to map to 15571745cbc5SAndy Lutomirski * @addr: target user address of this page 15581745cbc5SAndy Lutomirski * @pfn: source kernel pfn 15591745cbc5SAndy Lutomirski * @pgprot: pgprot flags for the inserted page 15601745cbc5SAndy Lutomirski * 15611745cbc5SAndy Lutomirski * This is exactly like vm_insert_pfn, except that it allows drivers to 15621745cbc5SAndy Lutomirski * to override pgprot on a per-page basis. 15631745cbc5SAndy Lutomirski * 15641745cbc5SAndy Lutomirski * This only makes sense for IO mappings, and it makes no sense for 15651745cbc5SAndy Lutomirski * cow mappings. In general, using multiple vmas is preferable; 15661745cbc5SAndy Lutomirski * vm_insert_pfn_prot should only be used if using multiple VMAs is 15671745cbc5SAndy Lutomirski * impractical. 15681745cbc5SAndy Lutomirski */ 15691745cbc5SAndy Lutomirski int vm_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr, 15701745cbc5SAndy Lutomirski unsigned long pfn, pgprot_t pgprot) 15711745cbc5SAndy Lutomirski { 15722ab64037Svenkatesh.pallipadi@intel.com int ret; 15737e675137SNick Piggin /* 15747e675137SNick Piggin * Technically, architectures with pte_special can avoid all these 15757e675137SNick Piggin * restrictions (same for remap_pfn_range). However we would like 15767e675137SNick Piggin * consistency in testing and feature parity among all, so we should 15777e675137SNick Piggin * try to keep these invariants in place for everybody. 15787e675137SNick Piggin */ 1579b379d790SJared Hulbert BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))); 1580b379d790SJared Hulbert BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) == 1581b379d790SJared Hulbert (VM_PFNMAP|VM_MIXEDMAP)); 1582b379d790SJared Hulbert BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); 1583b379d790SJared Hulbert BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn)); 1584e0dc0d8fSNick Piggin 1585423bad60SNick Piggin if (addr < vma->vm_start || addr >= vma->vm_end) 1586423bad60SNick Piggin return -EFAULT; 1587f25748e3SDan Williams if (track_pfn_insert(vma, &pgprot, __pfn_to_pfn_t(pfn, PFN_DEV))) 15882ab64037Svenkatesh.pallipadi@intel.com return -EINVAL; 15892ab64037Svenkatesh.pallipadi@intel.com 159001c8f1c4SDan Williams ret = insert_pfn(vma, addr, __pfn_to_pfn_t(pfn, PFN_DEV), pgprot); 15912ab64037Svenkatesh.pallipadi@intel.com 15922ab64037Svenkatesh.pallipadi@intel.com return ret; 1593e0dc0d8fSNick Piggin } 15941745cbc5SAndy Lutomirski EXPORT_SYMBOL(vm_insert_pfn_prot); 1595e0dc0d8fSNick Piggin 1596423bad60SNick Piggin int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr, 159701c8f1c4SDan Williams pfn_t pfn) 1598423bad60SNick Piggin { 1599423bad60SNick Piggin BUG_ON(!(vma->vm_flags & VM_MIXEDMAP)); 1600423bad60SNick Piggin 1601423bad60SNick Piggin if (addr < vma->vm_start || addr >= vma->vm_end) 1602423bad60SNick Piggin return -EFAULT; 1603423bad60SNick Piggin 1604423bad60SNick Piggin /* 1605423bad60SNick Piggin * If we don't have pte special, then we have to use the pfn_valid() 1606423bad60SNick Piggin * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must* 1607423bad60SNick Piggin * refcount the page if pfn_valid is true (hence insert_page rather 160862eede62SHugh Dickins * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP 160962eede62SHugh Dickins * without pte special, it would there be refcounted as a normal page. 1610423bad60SNick Piggin */ 161103fc2da6SDan Williams if (!HAVE_PTE_SPECIAL && !pfn_t_devmap(pfn) && pfn_t_valid(pfn)) { 1612423bad60SNick Piggin struct page *page; 1613423bad60SNick Piggin 161403fc2da6SDan Williams /* 161503fc2da6SDan Williams * At this point we are committed to insert_page() 161603fc2da6SDan Williams * regardless of whether the caller specified flags that 161703fc2da6SDan Williams * result in pfn_t_has_page() == false. 161803fc2da6SDan Williams */ 161903fc2da6SDan Williams page = pfn_to_page(pfn_t_to_pfn(pfn)); 1620423bad60SNick Piggin return insert_page(vma, addr, page, vma->vm_page_prot); 1621423bad60SNick Piggin } 1622423bad60SNick Piggin return insert_pfn(vma, addr, pfn, vma->vm_page_prot); 1623423bad60SNick Piggin } 1624423bad60SNick Piggin EXPORT_SYMBOL(vm_insert_mixed); 1625423bad60SNick Piggin 1626a145dd41SLinus Torvalds /* 16271da177e4SLinus Torvalds * maps a range of physical memory into the requested pages. the old 16281da177e4SLinus Torvalds * mappings are removed. any references to nonexistent pages results 16291da177e4SLinus Torvalds * in null mappings (currently treated as "copy-on-access") 16301da177e4SLinus Torvalds */ 16311da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd, 16321da177e4SLinus Torvalds unsigned long addr, unsigned long end, 16331da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 16341da177e4SLinus Torvalds { 16351da177e4SLinus Torvalds pte_t *pte; 1636c74df32cSHugh Dickins spinlock_t *ptl; 16371da177e4SLinus Torvalds 1638c74df32cSHugh Dickins pte = pte_alloc_map_lock(mm, pmd, addr, &ptl); 16391da177e4SLinus Torvalds if (!pte) 16401da177e4SLinus Torvalds return -ENOMEM; 16416606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 16421da177e4SLinus Torvalds do { 16431da177e4SLinus Torvalds BUG_ON(!pte_none(*pte)); 16447e675137SNick Piggin set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot))); 16451da177e4SLinus Torvalds pfn++; 16461da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 16476606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 1648c74df32cSHugh Dickins pte_unmap_unlock(pte - 1, ptl); 16491da177e4SLinus Torvalds return 0; 16501da177e4SLinus Torvalds } 16511da177e4SLinus Torvalds 16521da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud, 16531da177e4SLinus Torvalds unsigned long addr, unsigned long end, 16541da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 16551da177e4SLinus Torvalds { 16561da177e4SLinus Torvalds pmd_t *pmd; 16571da177e4SLinus Torvalds unsigned long next; 16581da177e4SLinus Torvalds 16591da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 16601da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, addr); 16611da177e4SLinus Torvalds if (!pmd) 16621da177e4SLinus Torvalds return -ENOMEM; 1663f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 16641da177e4SLinus Torvalds do { 16651da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 16661da177e4SLinus Torvalds if (remap_pte_range(mm, pmd, addr, next, 16671da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot)) 16681da177e4SLinus Torvalds return -ENOMEM; 16691da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 16701da177e4SLinus Torvalds return 0; 16711da177e4SLinus Torvalds } 16721da177e4SLinus Torvalds 16731da177e4SLinus Torvalds static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd, 16741da177e4SLinus Torvalds unsigned long addr, unsigned long end, 16751da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 16761da177e4SLinus Torvalds { 16771da177e4SLinus Torvalds pud_t *pud; 16781da177e4SLinus Torvalds unsigned long next; 16791da177e4SLinus Torvalds 16801da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 16811da177e4SLinus Torvalds pud = pud_alloc(mm, pgd, addr); 16821da177e4SLinus Torvalds if (!pud) 16831da177e4SLinus Torvalds return -ENOMEM; 16841da177e4SLinus Torvalds do { 16851da177e4SLinus Torvalds next = pud_addr_end(addr, end); 16861da177e4SLinus Torvalds if (remap_pmd_range(mm, pud, addr, next, 16871da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot)) 16881da177e4SLinus Torvalds return -ENOMEM; 16891da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 16901da177e4SLinus Torvalds return 0; 16911da177e4SLinus Torvalds } 16921da177e4SLinus Torvalds 1693bfa5bf6dSRolf Eike Beer /** 1694bfa5bf6dSRolf Eike Beer * remap_pfn_range - remap kernel memory to userspace 1695bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 1696bfa5bf6dSRolf Eike Beer * @addr: target user address to start at 1697bfa5bf6dSRolf Eike Beer * @pfn: physical address of kernel memory 1698bfa5bf6dSRolf Eike Beer * @size: size of map area 1699bfa5bf6dSRolf Eike Beer * @prot: page protection flags for this mapping 1700bfa5bf6dSRolf Eike Beer * 1701bfa5bf6dSRolf Eike Beer * Note: this is only safe if the mm semaphore is held when called. 1702bfa5bf6dSRolf Eike Beer */ 17031da177e4SLinus Torvalds int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 17041da177e4SLinus Torvalds unsigned long pfn, unsigned long size, pgprot_t prot) 17051da177e4SLinus Torvalds { 17061da177e4SLinus Torvalds pgd_t *pgd; 17071da177e4SLinus Torvalds unsigned long next; 17082d15cab8SHugh Dickins unsigned long end = addr + PAGE_ALIGN(size); 17091da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 17101da177e4SLinus Torvalds int err; 17111da177e4SLinus Torvalds 17121da177e4SLinus Torvalds /* 17131da177e4SLinus Torvalds * Physically remapped pages are special. Tell the 17141da177e4SLinus Torvalds * rest of the world about it: 17151da177e4SLinus Torvalds * VM_IO tells people not to look at these pages 17161da177e4SLinus Torvalds * (accesses can have side effects). 17176aab341eSLinus Torvalds * VM_PFNMAP tells the core MM that the base pages are just 17186aab341eSLinus Torvalds * raw PFN mappings, and do not have a "struct page" associated 17196aab341eSLinus Torvalds * with them. 1720314e51b9SKonstantin Khlebnikov * VM_DONTEXPAND 1721314e51b9SKonstantin Khlebnikov * Disable vma merging and expanding with mremap(). 1722314e51b9SKonstantin Khlebnikov * VM_DONTDUMP 1723314e51b9SKonstantin Khlebnikov * Omit vma from core dump, even when VM_IO turned off. 1724fb155c16SLinus Torvalds * 1725fb155c16SLinus Torvalds * There's a horrible special case to handle copy-on-write 1726fb155c16SLinus Torvalds * behaviour that some programs depend on. We mark the "original" 1727fb155c16SLinus Torvalds * un-COW'ed pages by matching them up with "vma->vm_pgoff". 1728b3b9c293SKonstantin Khlebnikov * See vm_normal_page() for details. 17291da177e4SLinus Torvalds */ 1730b3b9c293SKonstantin Khlebnikov if (is_cow_mapping(vma->vm_flags)) { 1731b3b9c293SKonstantin Khlebnikov if (addr != vma->vm_start || end != vma->vm_end) 1732b3b9c293SKonstantin Khlebnikov return -EINVAL; 17336aab341eSLinus Torvalds vma->vm_pgoff = pfn; 1734b3b9c293SKonstantin Khlebnikov } 1735b3b9c293SKonstantin Khlebnikov 1736b3b9c293SKonstantin Khlebnikov err = track_pfn_remap(vma, &prot, pfn, addr, PAGE_ALIGN(size)); 1737b3b9c293SKonstantin Khlebnikov if (err) 17383c8bb73aSvenkatesh.pallipadi@intel.com return -EINVAL; 1739fb155c16SLinus Torvalds 1740314e51b9SKonstantin Khlebnikov vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; 17411da177e4SLinus Torvalds 17421da177e4SLinus Torvalds BUG_ON(addr >= end); 17431da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 17441da177e4SLinus Torvalds pgd = pgd_offset(mm, addr); 17451da177e4SLinus Torvalds flush_cache_range(vma, addr, end); 17461da177e4SLinus Torvalds do { 17471da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 17481da177e4SLinus Torvalds err = remap_pud_range(mm, pgd, addr, next, 17491da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot); 17501da177e4SLinus Torvalds if (err) 17511da177e4SLinus Torvalds break; 17521da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 17532ab64037Svenkatesh.pallipadi@intel.com 17542ab64037Svenkatesh.pallipadi@intel.com if (err) 17555180da41SSuresh Siddha untrack_pfn(vma, pfn, PAGE_ALIGN(size)); 17562ab64037Svenkatesh.pallipadi@intel.com 17571da177e4SLinus Torvalds return err; 17581da177e4SLinus Torvalds } 17591da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range); 17601da177e4SLinus Torvalds 1761b4cbb197SLinus Torvalds /** 1762b4cbb197SLinus Torvalds * vm_iomap_memory - remap memory to userspace 1763b4cbb197SLinus Torvalds * @vma: user vma to map to 1764b4cbb197SLinus Torvalds * @start: start of area 1765b4cbb197SLinus Torvalds * @len: size of area 1766b4cbb197SLinus Torvalds * 1767b4cbb197SLinus Torvalds * This is a simplified io_remap_pfn_range() for common driver use. The 1768b4cbb197SLinus Torvalds * driver just needs to give us the physical memory range to be mapped, 1769b4cbb197SLinus Torvalds * we'll figure out the rest from the vma information. 1770b4cbb197SLinus Torvalds * 1771b4cbb197SLinus Torvalds * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get 1772b4cbb197SLinus Torvalds * whatever write-combining details or similar. 1773b4cbb197SLinus Torvalds */ 1774b4cbb197SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len) 1775b4cbb197SLinus Torvalds { 1776b4cbb197SLinus Torvalds unsigned long vm_len, pfn, pages; 1777b4cbb197SLinus Torvalds 1778b4cbb197SLinus Torvalds /* Check that the physical memory area passed in looks valid */ 1779b4cbb197SLinus Torvalds if (start + len < start) 1780b4cbb197SLinus Torvalds return -EINVAL; 1781b4cbb197SLinus Torvalds /* 1782b4cbb197SLinus Torvalds * You *really* shouldn't map things that aren't page-aligned, 1783b4cbb197SLinus Torvalds * but we've historically allowed it because IO memory might 1784b4cbb197SLinus Torvalds * just have smaller alignment. 1785b4cbb197SLinus Torvalds */ 1786b4cbb197SLinus Torvalds len += start & ~PAGE_MASK; 1787b4cbb197SLinus Torvalds pfn = start >> PAGE_SHIFT; 1788b4cbb197SLinus Torvalds pages = (len + ~PAGE_MASK) >> PAGE_SHIFT; 1789b4cbb197SLinus Torvalds if (pfn + pages < pfn) 1790b4cbb197SLinus Torvalds return -EINVAL; 1791b4cbb197SLinus Torvalds 1792b4cbb197SLinus Torvalds /* We start the mapping 'vm_pgoff' pages into the area */ 1793b4cbb197SLinus Torvalds if (vma->vm_pgoff > pages) 1794b4cbb197SLinus Torvalds return -EINVAL; 1795b4cbb197SLinus Torvalds pfn += vma->vm_pgoff; 1796b4cbb197SLinus Torvalds pages -= vma->vm_pgoff; 1797b4cbb197SLinus Torvalds 1798b4cbb197SLinus Torvalds /* Can we fit all of the mapping? */ 1799b4cbb197SLinus Torvalds vm_len = vma->vm_end - vma->vm_start; 1800b4cbb197SLinus Torvalds if (vm_len >> PAGE_SHIFT > pages) 1801b4cbb197SLinus Torvalds return -EINVAL; 1802b4cbb197SLinus Torvalds 1803b4cbb197SLinus Torvalds /* Ok, let it rip */ 1804b4cbb197SLinus Torvalds return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot); 1805b4cbb197SLinus Torvalds } 1806b4cbb197SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory); 1807b4cbb197SLinus Torvalds 1808aee16b3cSJeremy Fitzhardinge static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd, 1809aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 1810aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 1811aee16b3cSJeremy Fitzhardinge { 1812aee16b3cSJeremy Fitzhardinge pte_t *pte; 1813aee16b3cSJeremy Fitzhardinge int err; 18142f569afdSMartin Schwidefsky pgtable_t token; 181594909914SBorislav Petkov spinlock_t *uninitialized_var(ptl); 1816aee16b3cSJeremy Fitzhardinge 1817aee16b3cSJeremy Fitzhardinge pte = (mm == &init_mm) ? 1818aee16b3cSJeremy Fitzhardinge pte_alloc_kernel(pmd, addr) : 1819aee16b3cSJeremy Fitzhardinge pte_alloc_map_lock(mm, pmd, addr, &ptl); 1820aee16b3cSJeremy Fitzhardinge if (!pte) 1821aee16b3cSJeremy Fitzhardinge return -ENOMEM; 1822aee16b3cSJeremy Fitzhardinge 1823aee16b3cSJeremy Fitzhardinge BUG_ON(pmd_huge(*pmd)); 1824aee16b3cSJeremy Fitzhardinge 182538e0edb1SJeremy Fitzhardinge arch_enter_lazy_mmu_mode(); 182638e0edb1SJeremy Fitzhardinge 18272f569afdSMartin Schwidefsky token = pmd_pgtable(*pmd); 1828aee16b3cSJeremy Fitzhardinge 1829aee16b3cSJeremy Fitzhardinge do { 1830c36987e2SDaisuke Nishimura err = fn(pte++, token, addr, data); 1831aee16b3cSJeremy Fitzhardinge if (err) 1832aee16b3cSJeremy Fitzhardinge break; 1833c36987e2SDaisuke Nishimura } while (addr += PAGE_SIZE, addr != end); 1834aee16b3cSJeremy Fitzhardinge 183538e0edb1SJeremy Fitzhardinge arch_leave_lazy_mmu_mode(); 183638e0edb1SJeremy Fitzhardinge 1837aee16b3cSJeremy Fitzhardinge if (mm != &init_mm) 1838aee16b3cSJeremy Fitzhardinge pte_unmap_unlock(pte-1, ptl); 1839aee16b3cSJeremy Fitzhardinge return err; 1840aee16b3cSJeremy Fitzhardinge } 1841aee16b3cSJeremy Fitzhardinge 1842aee16b3cSJeremy Fitzhardinge static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud, 1843aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 1844aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 1845aee16b3cSJeremy Fitzhardinge { 1846aee16b3cSJeremy Fitzhardinge pmd_t *pmd; 1847aee16b3cSJeremy Fitzhardinge unsigned long next; 1848aee16b3cSJeremy Fitzhardinge int err; 1849aee16b3cSJeremy Fitzhardinge 1850ceb86879SAndi Kleen BUG_ON(pud_huge(*pud)); 1851ceb86879SAndi Kleen 1852aee16b3cSJeremy Fitzhardinge pmd = pmd_alloc(mm, pud, addr); 1853aee16b3cSJeremy Fitzhardinge if (!pmd) 1854aee16b3cSJeremy Fitzhardinge return -ENOMEM; 1855aee16b3cSJeremy Fitzhardinge do { 1856aee16b3cSJeremy Fitzhardinge next = pmd_addr_end(addr, end); 1857aee16b3cSJeremy Fitzhardinge err = apply_to_pte_range(mm, pmd, addr, next, fn, data); 1858aee16b3cSJeremy Fitzhardinge if (err) 1859aee16b3cSJeremy Fitzhardinge break; 1860aee16b3cSJeremy Fitzhardinge } while (pmd++, addr = next, addr != end); 1861aee16b3cSJeremy Fitzhardinge return err; 1862aee16b3cSJeremy Fitzhardinge } 1863aee16b3cSJeremy Fitzhardinge 1864aee16b3cSJeremy Fitzhardinge static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd, 1865aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 1866aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 1867aee16b3cSJeremy Fitzhardinge { 1868aee16b3cSJeremy Fitzhardinge pud_t *pud; 1869aee16b3cSJeremy Fitzhardinge unsigned long next; 1870aee16b3cSJeremy Fitzhardinge int err; 1871aee16b3cSJeremy Fitzhardinge 1872aee16b3cSJeremy Fitzhardinge pud = pud_alloc(mm, pgd, addr); 1873aee16b3cSJeremy Fitzhardinge if (!pud) 1874aee16b3cSJeremy Fitzhardinge return -ENOMEM; 1875aee16b3cSJeremy Fitzhardinge do { 1876aee16b3cSJeremy Fitzhardinge next = pud_addr_end(addr, end); 1877aee16b3cSJeremy Fitzhardinge err = apply_to_pmd_range(mm, pud, addr, next, fn, data); 1878aee16b3cSJeremy Fitzhardinge if (err) 1879aee16b3cSJeremy Fitzhardinge break; 1880aee16b3cSJeremy Fitzhardinge } while (pud++, addr = next, addr != end); 1881aee16b3cSJeremy Fitzhardinge return err; 1882aee16b3cSJeremy Fitzhardinge } 1883aee16b3cSJeremy Fitzhardinge 1884aee16b3cSJeremy Fitzhardinge /* 1885aee16b3cSJeremy Fitzhardinge * Scan a region of virtual memory, filling in page tables as necessary 1886aee16b3cSJeremy Fitzhardinge * and calling a provided function on each leaf page table. 1887aee16b3cSJeremy Fitzhardinge */ 1888aee16b3cSJeremy Fitzhardinge int apply_to_page_range(struct mm_struct *mm, unsigned long addr, 1889aee16b3cSJeremy Fitzhardinge unsigned long size, pte_fn_t fn, void *data) 1890aee16b3cSJeremy Fitzhardinge { 1891aee16b3cSJeremy Fitzhardinge pgd_t *pgd; 1892aee16b3cSJeremy Fitzhardinge unsigned long next; 189357250a5bSJeremy Fitzhardinge unsigned long end = addr + size; 1894aee16b3cSJeremy Fitzhardinge int err; 1895aee16b3cSJeremy Fitzhardinge 18969cb65bc3SMika Penttilä if (WARN_ON(addr >= end)) 18979cb65bc3SMika Penttilä return -EINVAL; 18989cb65bc3SMika Penttilä 1899aee16b3cSJeremy Fitzhardinge pgd = pgd_offset(mm, addr); 1900aee16b3cSJeremy Fitzhardinge do { 1901aee16b3cSJeremy Fitzhardinge next = pgd_addr_end(addr, end); 1902aee16b3cSJeremy Fitzhardinge err = apply_to_pud_range(mm, pgd, addr, next, fn, data); 1903aee16b3cSJeremy Fitzhardinge if (err) 1904aee16b3cSJeremy Fitzhardinge break; 1905aee16b3cSJeremy Fitzhardinge } while (pgd++, addr = next, addr != end); 190657250a5bSJeremy Fitzhardinge 1907aee16b3cSJeremy Fitzhardinge return err; 1908aee16b3cSJeremy Fitzhardinge } 1909aee16b3cSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(apply_to_page_range); 1910aee16b3cSJeremy Fitzhardinge 19111da177e4SLinus Torvalds /* 19129b4bdd2fSKirill A. Shutemov * handle_pte_fault chooses page fault handler according to an entry which was 19139b4bdd2fSKirill A. Shutemov * read non-atomically. Before making any commitment, on those architectures 19149b4bdd2fSKirill A. Shutemov * or configurations (e.g. i386 with PAE) which might give a mix of unmatched 19159b4bdd2fSKirill A. Shutemov * parts, do_swap_page must check under lock before unmapping the pte and 19169b4bdd2fSKirill A. Shutemov * proceeding (but do_wp_page is only called after already making such a check; 1917a335b2e1SRyota Ozaki * and do_anonymous_page can safely check later on). 19188f4e2101SHugh Dickins */ 19194c21e2f2SHugh Dickins static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd, 19208f4e2101SHugh Dickins pte_t *page_table, pte_t orig_pte) 19218f4e2101SHugh Dickins { 19228f4e2101SHugh Dickins int same = 1; 19238f4e2101SHugh Dickins #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT) 19248f4e2101SHugh Dickins if (sizeof(pte_t) > sizeof(unsigned long)) { 19254c21e2f2SHugh Dickins spinlock_t *ptl = pte_lockptr(mm, pmd); 19264c21e2f2SHugh Dickins spin_lock(ptl); 19278f4e2101SHugh Dickins same = pte_same(*page_table, orig_pte); 19284c21e2f2SHugh Dickins spin_unlock(ptl); 19298f4e2101SHugh Dickins } 19308f4e2101SHugh Dickins #endif 19318f4e2101SHugh Dickins pte_unmap(page_table); 19328f4e2101SHugh Dickins return same; 19338f4e2101SHugh Dickins } 19348f4e2101SHugh Dickins 19359de455b2SAtsushi Nemoto static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma) 19366aab341eSLinus Torvalds { 19370abdd7a8SDan Williams debug_dma_assert_idle(src); 19380abdd7a8SDan Williams 19396aab341eSLinus Torvalds /* 19406aab341eSLinus Torvalds * If the source page was a PFN mapping, we don't have 19416aab341eSLinus Torvalds * a "struct page" for it. We do a best-effort copy by 19426aab341eSLinus Torvalds * just copying from the original user address. If that 19436aab341eSLinus Torvalds * fails, we just zero-fill it. Live with it. 19446aab341eSLinus Torvalds */ 19456aab341eSLinus Torvalds if (unlikely(!src)) { 19469b04c5feSCong Wang void *kaddr = kmap_atomic(dst); 19475d2a2dbbSLinus Torvalds void __user *uaddr = (void __user *)(va & PAGE_MASK); 19485d2a2dbbSLinus Torvalds 19495d2a2dbbSLinus Torvalds /* 19505d2a2dbbSLinus Torvalds * This really shouldn't fail, because the page is there 19515d2a2dbbSLinus Torvalds * in the page tables. But it might just be unreadable, 19525d2a2dbbSLinus Torvalds * in which case we just give up and fill the result with 19535d2a2dbbSLinus Torvalds * zeroes. 19545d2a2dbbSLinus Torvalds */ 19555d2a2dbbSLinus Torvalds if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) 19563ecb01dfSJan Beulich clear_page(kaddr); 19579b04c5feSCong Wang kunmap_atomic(kaddr); 1958c4ec7b0dSDmitriy Monakhov flush_dcache_page(dst); 19590ed361deSNick Piggin } else 19609de455b2SAtsushi Nemoto copy_user_highpage(dst, src, va, vma); 19616aab341eSLinus Torvalds } 19626aab341eSLinus Torvalds 1963c20cd45eSMichal Hocko static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma) 1964c20cd45eSMichal Hocko { 1965c20cd45eSMichal Hocko struct file *vm_file = vma->vm_file; 1966c20cd45eSMichal Hocko 1967c20cd45eSMichal Hocko if (vm_file) 1968c20cd45eSMichal Hocko return mapping_gfp_mask(vm_file->f_mapping) | __GFP_FS | __GFP_IO; 1969c20cd45eSMichal Hocko 1970c20cd45eSMichal Hocko /* 1971c20cd45eSMichal Hocko * Special mappings (e.g. VDSO) do not have any file so fake 1972c20cd45eSMichal Hocko * a default GFP_KERNEL for them. 1973c20cd45eSMichal Hocko */ 1974c20cd45eSMichal Hocko return GFP_KERNEL; 1975c20cd45eSMichal Hocko } 1976c20cd45eSMichal Hocko 19771da177e4SLinus Torvalds /* 1978fb09a464SKirill A. Shutemov * Notify the address space that the page is about to become writable so that 1979fb09a464SKirill A. Shutemov * it can prohibit this or wait for the page to get into an appropriate state. 1980fb09a464SKirill A. Shutemov * 1981fb09a464SKirill A. Shutemov * We do this without the lock held, so that it can sleep if it needs to. 1982fb09a464SKirill A. Shutemov */ 1983fb09a464SKirill A. Shutemov static int do_page_mkwrite(struct vm_area_struct *vma, struct page *page, 1984fb09a464SKirill A. Shutemov unsigned long address) 1985fb09a464SKirill A. Shutemov { 1986fb09a464SKirill A. Shutemov struct vm_fault vmf; 1987fb09a464SKirill A. Shutemov int ret; 1988fb09a464SKirill A. Shutemov 1989fb09a464SKirill A. Shutemov vmf.virtual_address = (void __user *)(address & PAGE_MASK); 1990fb09a464SKirill A. Shutemov vmf.pgoff = page->index; 1991fb09a464SKirill A. Shutemov vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE; 1992c20cd45eSMichal Hocko vmf.gfp_mask = __get_fault_gfp_mask(vma); 1993fb09a464SKirill A. Shutemov vmf.page = page; 19942e4cdab0SMatthew Wilcox vmf.cow_page = NULL; 1995fb09a464SKirill A. Shutemov 1996fb09a464SKirill A. Shutemov ret = vma->vm_ops->page_mkwrite(vma, &vmf); 1997fb09a464SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) 1998fb09a464SKirill A. Shutemov return ret; 1999fb09a464SKirill A. Shutemov if (unlikely(!(ret & VM_FAULT_LOCKED))) { 2000fb09a464SKirill A. Shutemov lock_page(page); 2001fb09a464SKirill A. Shutemov if (!page->mapping) { 2002fb09a464SKirill A. Shutemov unlock_page(page); 2003fb09a464SKirill A. Shutemov return 0; /* retry */ 2004fb09a464SKirill A. Shutemov } 2005fb09a464SKirill A. Shutemov ret |= VM_FAULT_LOCKED; 2006fb09a464SKirill A. Shutemov } else 2007fb09a464SKirill A. Shutemov VM_BUG_ON_PAGE(!PageLocked(page), page); 2008fb09a464SKirill A. Shutemov return ret; 2009fb09a464SKirill A. Shutemov } 2010fb09a464SKirill A. Shutemov 2011fb09a464SKirill A. Shutemov /* 20124e047f89SShachar Raindel * Handle write page faults for pages that can be reused in the current vma 20134e047f89SShachar Raindel * 20144e047f89SShachar Raindel * This can happen either due to the mapping being with the VM_SHARED flag, 20154e047f89SShachar Raindel * or due to us being the last reference standing to the page. In either 20164e047f89SShachar Raindel * case, all we need to do here is to mark the page as writable and update 20174e047f89SShachar Raindel * any related book-keeping. 20184e047f89SShachar Raindel */ 20194e047f89SShachar Raindel static inline int wp_page_reuse(struct mm_struct *mm, 20204e047f89SShachar Raindel struct vm_area_struct *vma, unsigned long address, 20214e047f89SShachar Raindel pte_t *page_table, spinlock_t *ptl, pte_t orig_pte, 20224e047f89SShachar Raindel struct page *page, int page_mkwrite, 20234e047f89SShachar Raindel int dirty_shared) 20244e047f89SShachar Raindel __releases(ptl) 20254e047f89SShachar Raindel { 20264e047f89SShachar Raindel pte_t entry; 20274e047f89SShachar Raindel /* 20284e047f89SShachar Raindel * Clear the pages cpupid information as the existing 20294e047f89SShachar Raindel * information potentially belongs to a now completely 20304e047f89SShachar Raindel * unrelated process. 20314e047f89SShachar Raindel */ 20324e047f89SShachar Raindel if (page) 20334e047f89SShachar Raindel page_cpupid_xchg_last(page, (1 << LAST_CPUPID_SHIFT) - 1); 20344e047f89SShachar Raindel 20354e047f89SShachar Raindel flush_cache_page(vma, address, pte_pfn(orig_pte)); 20364e047f89SShachar Raindel entry = pte_mkyoung(orig_pte); 20374e047f89SShachar Raindel entry = maybe_mkwrite(pte_mkdirty(entry), vma); 20384e047f89SShachar Raindel if (ptep_set_access_flags(vma, address, page_table, entry, 1)) 20394e047f89SShachar Raindel update_mmu_cache(vma, address, page_table); 20404e047f89SShachar Raindel pte_unmap_unlock(page_table, ptl); 20414e047f89SShachar Raindel 20424e047f89SShachar Raindel if (dirty_shared) { 20434e047f89SShachar Raindel struct address_space *mapping; 20444e047f89SShachar Raindel int dirtied; 20454e047f89SShachar Raindel 20464e047f89SShachar Raindel if (!page_mkwrite) 20474e047f89SShachar Raindel lock_page(page); 20484e047f89SShachar Raindel 20494e047f89SShachar Raindel dirtied = set_page_dirty(page); 20504e047f89SShachar Raindel VM_BUG_ON_PAGE(PageAnon(page), page); 20514e047f89SShachar Raindel mapping = page->mapping; 20524e047f89SShachar Raindel unlock_page(page); 20534e047f89SShachar Raindel page_cache_release(page); 20544e047f89SShachar Raindel 20554e047f89SShachar Raindel if ((dirtied || page_mkwrite) && mapping) { 20564e047f89SShachar Raindel /* 20574e047f89SShachar Raindel * Some device drivers do not set page.mapping 20584e047f89SShachar Raindel * but still dirty their pages 20594e047f89SShachar Raindel */ 20604e047f89SShachar Raindel balance_dirty_pages_ratelimited(mapping); 20614e047f89SShachar Raindel } 20624e047f89SShachar Raindel 20634e047f89SShachar Raindel if (!page_mkwrite) 20644e047f89SShachar Raindel file_update_time(vma->vm_file); 20654e047f89SShachar Raindel } 20664e047f89SShachar Raindel 20674e047f89SShachar Raindel return VM_FAULT_WRITE; 20684e047f89SShachar Raindel } 20694e047f89SShachar Raindel 20704e047f89SShachar Raindel /* 20712f38ab2cSShachar Raindel * Handle the case of a page which we actually need to copy to a new page. 20722f38ab2cSShachar Raindel * 20732f38ab2cSShachar Raindel * Called with mmap_sem locked and the old page referenced, but 20742f38ab2cSShachar Raindel * without the ptl held. 20752f38ab2cSShachar Raindel * 20762f38ab2cSShachar Raindel * High level logic flow: 20772f38ab2cSShachar Raindel * 20782f38ab2cSShachar Raindel * - Allocate a page, copy the content of the old page to the new one. 20792f38ab2cSShachar Raindel * - Handle book keeping and accounting - cgroups, mmu-notifiers, etc. 20802f38ab2cSShachar Raindel * - Take the PTL. If the pte changed, bail out and release the allocated page 20812f38ab2cSShachar Raindel * - If the pte is still the way we remember it, update the page table and all 20822f38ab2cSShachar Raindel * relevant references. This includes dropping the reference the page-table 20832f38ab2cSShachar Raindel * held to the old page, as well as updating the rmap. 20842f38ab2cSShachar Raindel * - In any case, unlock the PTL and drop the reference we took to the old page. 20852f38ab2cSShachar Raindel */ 20862f38ab2cSShachar Raindel static int wp_page_copy(struct mm_struct *mm, struct vm_area_struct *vma, 20872f38ab2cSShachar Raindel unsigned long address, pte_t *page_table, pmd_t *pmd, 20882f38ab2cSShachar Raindel pte_t orig_pte, struct page *old_page) 20892f38ab2cSShachar Raindel { 20902f38ab2cSShachar Raindel struct page *new_page = NULL; 20912f38ab2cSShachar Raindel spinlock_t *ptl = NULL; 20922f38ab2cSShachar Raindel pte_t entry; 20932f38ab2cSShachar Raindel int page_copied = 0; 20942f38ab2cSShachar Raindel const unsigned long mmun_start = address & PAGE_MASK; /* For mmu_notifiers */ 20952f38ab2cSShachar Raindel const unsigned long mmun_end = mmun_start + PAGE_SIZE; /* For mmu_notifiers */ 20962f38ab2cSShachar Raindel struct mem_cgroup *memcg; 20972f38ab2cSShachar Raindel 20982f38ab2cSShachar Raindel if (unlikely(anon_vma_prepare(vma))) 20992f38ab2cSShachar Raindel goto oom; 21002f38ab2cSShachar Raindel 21012f38ab2cSShachar Raindel if (is_zero_pfn(pte_pfn(orig_pte))) { 21022f38ab2cSShachar Raindel new_page = alloc_zeroed_user_highpage_movable(vma, address); 21032f38ab2cSShachar Raindel if (!new_page) 21042f38ab2cSShachar Raindel goto oom; 21052f38ab2cSShachar Raindel } else { 21062f38ab2cSShachar Raindel new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address); 21072f38ab2cSShachar Raindel if (!new_page) 21082f38ab2cSShachar Raindel goto oom; 21092f38ab2cSShachar Raindel cow_user_page(new_page, old_page, address, vma); 21102f38ab2cSShachar Raindel } 21112f38ab2cSShachar Raindel 2112f627c2f5SKirill A. Shutemov if (mem_cgroup_try_charge(new_page, mm, GFP_KERNEL, &memcg, false)) 21132f38ab2cSShachar Raindel goto oom_free_new; 21142f38ab2cSShachar Raindel 2115eb3c24f3SMel Gorman __SetPageUptodate(new_page); 2116eb3c24f3SMel Gorman 21172f38ab2cSShachar Raindel mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); 21182f38ab2cSShachar Raindel 21192f38ab2cSShachar Raindel /* 21202f38ab2cSShachar Raindel * Re-check the pte - we dropped the lock 21212f38ab2cSShachar Raindel */ 21222f38ab2cSShachar Raindel page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 21232f38ab2cSShachar Raindel if (likely(pte_same(*page_table, orig_pte))) { 21242f38ab2cSShachar Raindel if (old_page) { 21252f38ab2cSShachar Raindel if (!PageAnon(old_page)) { 2126eca56ff9SJerome Marchand dec_mm_counter_fast(mm, 2127eca56ff9SJerome Marchand mm_counter_file(old_page)); 21282f38ab2cSShachar Raindel inc_mm_counter_fast(mm, MM_ANONPAGES); 21292f38ab2cSShachar Raindel } 21302f38ab2cSShachar Raindel } else { 21312f38ab2cSShachar Raindel inc_mm_counter_fast(mm, MM_ANONPAGES); 21322f38ab2cSShachar Raindel } 21332f38ab2cSShachar Raindel flush_cache_page(vma, address, pte_pfn(orig_pte)); 21342f38ab2cSShachar Raindel entry = mk_pte(new_page, vma->vm_page_prot); 21352f38ab2cSShachar Raindel entry = maybe_mkwrite(pte_mkdirty(entry), vma); 21362f38ab2cSShachar Raindel /* 21372f38ab2cSShachar Raindel * Clear the pte entry and flush it first, before updating the 21382f38ab2cSShachar Raindel * pte with the new entry. This will avoid a race condition 21392f38ab2cSShachar Raindel * seen in the presence of one thread doing SMC and another 21402f38ab2cSShachar Raindel * thread doing COW. 21412f38ab2cSShachar Raindel */ 21422f38ab2cSShachar Raindel ptep_clear_flush_notify(vma, address, page_table); 2143d281ee61SKirill A. Shutemov page_add_new_anon_rmap(new_page, vma, address, false); 2144f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(new_page, memcg, false, false); 21452f38ab2cSShachar Raindel lru_cache_add_active_or_unevictable(new_page, vma); 21462f38ab2cSShachar Raindel /* 21472f38ab2cSShachar Raindel * We call the notify macro here because, when using secondary 21482f38ab2cSShachar Raindel * mmu page tables (such as kvm shadow page tables), we want the 21492f38ab2cSShachar Raindel * new page to be mapped directly into the secondary page table. 21502f38ab2cSShachar Raindel */ 21512f38ab2cSShachar Raindel set_pte_at_notify(mm, address, page_table, entry); 21522f38ab2cSShachar Raindel update_mmu_cache(vma, address, page_table); 21532f38ab2cSShachar Raindel if (old_page) { 21542f38ab2cSShachar Raindel /* 21552f38ab2cSShachar Raindel * Only after switching the pte to the new page may 21562f38ab2cSShachar Raindel * we remove the mapcount here. Otherwise another 21572f38ab2cSShachar Raindel * process may come and find the rmap count decremented 21582f38ab2cSShachar Raindel * before the pte is switched to the new page, and 21592f38ab2cSShachar Raindel * "reuse" the old page writing into it while our pte 21602f38ab2cSShachar Raindel * here still points into it and can be read by other 21612f38ab2cSShachar Raindel * threads. 21622f38ab2cSShachar Raindel * 21632f38ab2cSShachar Raindel * The critical issue is to order this 21642f38ab2cSShachar Raindel * page_remove_rmap with the ptp_clear_flush above. 21652f38ab2cSShachar Raindel * Those stores are ordered by (if nothing else,) 21662f38ab2cSShachar Raindel * the barrier present in the atomic_add_negative 21672f38ab2cSShachar Raindel * in page_remove_rmap. 21682f38ab2cSShachar Raindel * 21692f38ab2cSShachar Raindel * Then the TLB flush in ptep_clear_flush ensures that 21702f38ab2cSShachar Raindel * no process can access the old page before the 21712f38ab2cSShachar Raindel * decremented mapcount is visible. And the old page 21722f38ab2cSShachar Raindel * cannot be reused until after the decremented 21732f38ab2cSShachar Raindel * mapcount is visible. So transitively, TLBs to 21742f38ab2cSShachar Raindel * old page will be flushed before it can be reused. 21752f38ab2cSShachar Raindel */ 2176d281ee61SKirill A. Shutemov page_remove_rmap(old_page, false); 21772f38ab2cSShachar Raindel } 21782f38ab2cSShachar Raindel 21792f38ab2cSShachar Raindel /* Free the old page.. */ 21802f38ab2cSShachar Raindel new_page = old_page; 21812f38ab2cSShachar Raindel page_copied = 1; 21822f38ab2cSShachar Raindel } else { 2183f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(new_page, memcg, false); 21842f38ab2cSShachar Raindel } 21852f38ab2cSShachar Raindel 21862f38ab2cSShachar Raindel if (new_page) 21872f38ab2cSShachar Raindel page_cache_release(new_page); 21882f38ab2cSShachar Raindel 21892f38ab2cSShachar Raindel pte_unmap_unlock(page_table, ptl); 21902f38ab2cSShachar Raindel mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); 21912f38ab2cSShachar Raindel if (old_page) { 21922f38ab2cSShachar Raindel /* 21932f38ab2cSShachar Raindel * Don't let another task, with possibly unlocked vma, 21942f38ab2cSShachar Raindel * keep the mlocked page. 21952f38ab2cSShachar Raindel */ 21962f38ab2cSShachar Raindel if (page_copied && (vma->vm_flags & VM_LOCKED)) { 21972f38ab2cSShachar Raindel lock_page(old_page); /* LRU manipulation */ 2198e90309c9SKirill A. Shutemov if (PageMlocked(old_page)) 21992f38ab2cSShachar Raindel munlock_vma_page(old_page); 22002f38ab2cSShachar Raindel unlock_page(old_page); 22012f38ab2cSShachar Raindel } 22022f38ab2cSShachar Raindel page_cache_release(old_page); 22032f38ab2cSShachar Raindel } 22042f38ab2cSShachar Raindel return page_copied ? VM_FAULT_WRITE : 0; 22052f38ab2cSShachar Raindel oom_free_new: 22062f38ab2cSShachar Raindel page_cache_release(new_page); 22072f38ab2cSShachar Raindel oom: 22082f38ab2cSShachar Raindel if (old_page) 22092f38ab2cSShachar Raindel page_cache_release(old_page); 22102f38ab2cSShachar Raindel return VM_FAULT_OOM; 22112f38ab2cSShachar Raindel } 22122f38ab2cSShachar Raindel 2213dd906184SBoaz Harrosh /* 2214dd906184SBoaz Harrosh * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED 2215dd906184SBoaz Harrosh * mapping 2216dd906184SBoaz Harrosh */ 2217dd906184SBoaz Harrosh static int wp_pfn_shared(struct mm_struct *mm, 2218dd906184SBoaz Harrosh struct vm_area_struct *vma, unsigned long address, 2219dd906184SBoaz Harrosh pte_t *page_table, spinlock_t *ptl, pte_t orig_pte, 2220dd906184SBoaz Harrosh pmd_t *pmd) 2221dd906184SBoaz Harrosh { 2222dd906184SBoaz Harrosh if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) { 2223dd906184SBoaz Harrosh struct vm_fault vmf = { 2224dd906184SBoaz Harrosh .page = NULL, 2225dd906184SBoaz Harrosh .pgoff = linear_page_index(vma, address), 2226dd906184SBoaz Harrosh .virtual_address = (void __user *)(address & PAGE_MASK), 2227dd906184SBoaz Harrosh .flags = FAULT_FLAG_WRITE | FAULT_FLAG_MKWRITE, 2228dd906184SBoaz Harrosh }; 2229dd906184SBoaz Harrosh int ret; 2230dd906184SBoaz Harrosh 2231dd906184SBoaz Harrosh pte_unmap_unlock(page_table, ptl); 2232dd906184SBoaz Harrosh ret = vma->vm_ops->pfn_mkwrite(vma, &vmf); 2233dd906184SBoaz Harrosh if (ret & VM_FAULT_ERROR) 2234dd906184SBoaz Harrosh return ret; 2235dd906184SBoaz Harrosh page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 2236dd906184SBoaz Harrosh /* 2237dd906184SBoaz Harrosh * We might have raced with another page fault while we 2238dd906184SBoaz Harrosh * released the pte_offset_map_lock. 2239dd906184SBoaz Harrosh */ 2240dd906184SBoaz Harrosh if (!pte_same(*page_table, orig_pte)) { 2241dd906184SBoaz Harrosh pte_unmap_unlock(page_table, ptl); 2242dd906184SBoaz Harrosh return 0; 2243dd906184SBoaz Harrosh } 2244dd906184SBoaz Harrosh } 2245dd906184SBoaz Harrosh return wp_page_reuse(mm, vma, address, page_table, ptl, orig_pte, 2246dd906184SBoaz Harrosh NULL, 0, 0); 2247dd906184SBoaz Harrosh } 2248dd906184SBoaz Harrosh 224993e478d4SShachar Raindel static int wp_page_shared(struct mm_struct *mm, struct vm_area_struct *vma, 225093e478d4SShachar Raindel unsigned long address, pte_t *page_table, 225193e478d4SShachar Raindel pmd_t *pmd, spinlock_t *ptl, pte_t orig_pte, 225293e478d4SShachar Raindel struct page *old_page) 225393e478d4SShachar Raindel __releases(ptl) 225493e478d4SShachar Raindel { 225593e478d4SShachar Raindel int page_mkwrite = 0; 225693e478d4SShachar Raindel 225793e478d4SShachar Raindel page_cache_get(old_page); 225893e478d4SShachar Raindel 225993e478d4SShachar Raindel if (vma->vm_ops && vma->vm_ops->page_mkwrite) { 226093e478d4SShachar Raindel int tmp; 226193e478d4SShachar Raindel 226293e478d4SShachar Raindel pte_unmap_unlock(page_table, ptl); 226393e478d4SShachar Raindel tmp = do_page_mkwrite(vma, old_page, address); 226493e478d4SShachar Raindel if (unlikely(!tmp || (tmp & 226593e478d4SShachar Raindel (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { 226693e478d4SShachar Raindel page_cache_release(old_page); 226793e478d4SShachar Raindel return tmp; 226893e478d4SShachar Raindel } 226993e478d4SShachar Raindel /* 227093e478d4SShachar Raindel * Since we dropped the lock we need to revalidate 227193e478d4SShachar Raindel * the PTE as someone else may have changed it. If 227293e478d4SShachar Raindel * they did, we just return, as we can count on the 227393e478d4SShachar Raindel * MMU to tell us if they didn't also make it writable. 227493e478d4SShachar Raindel */ 227593e478d4SShachar Raindel page_table = pte_offset_map_lock(mm, pmd, address, 227693e478d4SShachar Raindel &ptl); 227793e478d4SShachar Raindel if (!pte_same(*page_table, orig_pte)) { 227893e478d4SShachar Raindel unlock_page(old_page); 227993e478d4SShachar Raindel pte_unmap_unlock(page_table, ptl); 228093e478d4SShachar Raindel page_cache_release(old_page); 228193e478d4SShachar Raindel return 0; 228293e478d4SShachar Raindel } 228393e478d4SShachar Raindel page_mkwrite = 1; 228493e478d4SShachar Raindel } 228593e478d4SShachar Raindel 228693e478d4SShachar Raindel return wp_page_reuse(mm, vma, address, page_table, ptl, 228793e478d4SShachar Raindel orig_pte, old_page, page_mkwrite, 1); 228893e478d4SShachar Raindel } 228993e478d4SShachar Raindel 22902f38ab2cSShachar Raindel /* 22911da177e4SLinus Torvalds * This routine handles present pages, when users try to write 22921da177e4SLinus Torvalds * to a shared page. It is done by copying the page to a new address 22931da177e4SLinus Torvalds * and decrementing the shared-page counter for the old page. 22941da177e4SLinus Torvalds * 22951da177e4SLinus Torvalds * Note that this routine assumes that the protection checks have been 22961da177e4SLinus Torvalds * done by the caller (the low-level page fault routine in most cases). 22971da177e4SLinus Torvalds * Thus we can safely just mark it writable once we've done any necessary 22981da177e4SLinus Torvalds * COW. 22991da177e4SLinus Torvalds * 23001da177e4SLinus Torvalds * We also mark the page dirty at this point even though the page will 23011da177e4SLinus Torvalds * change only once the write actually happens. This avoids a few races, 23021da177e4SLinus Torvalds * and potentially makes it more efficient. 23031da177e4SLinus Torvalds * 23048f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 23058f4e2101SHugh Dickins * but allow concurrent faults), with pte both mapped and locked. 23068f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 23071da177e4SLinus Torvalds */ 23081da177e4SLinus Torvalds static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma, 230965500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 23108f4e2101SHugh Dickins spinlock_t *ptl, pte_t orig_pte) 2311e6219ec8SNamhyung Kim __releases(ptl) 23121da177e4SLinus Torvalds { 23132f38ab2cSShachar Raindel struct page *old_page; 23141da177e4SLinus Torvalds 23156aab341eSLinus Torvalds old_page = vm_normal_page(vma, address, orig_pte); 2316251b97f5SPeter Zijlstra if (!old_page) { 2317251b97f5SPeter Zijlstra /* 231864e45507SPeter Feiner * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a 231964e45507SPeter Feiner * VM_PFNMAP VMA. 2320251b97f5SPeter Zijlstra * 2321251b97f5SPeter Zijlstra * We should not cow pages in a shared writeable mapping. 2322dd906184SBoaz Harrosh * Just mark the pages writable and/or call ops->pfn_mkwrite. 2323251b97f5SPeter Zijlstra */ 2324251b97f5SPeter Zijlstra if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 2325251b97f5SPeter Zijlstra (VM_WRITE|VM_SHARED)) 2326dd906184SBoaz Harrosh return wp_pfn_shared(mm, vma, address, page_table, ptl, 2327dd906184SBoaz Harrosh orig_pte, pmd); 23282f38ab2cSShachar Raindel 23292f38ab2cSShachar Raindel pte_unmap_unlock(page_table, ptl); 23302f38ab2cSShachar Raindel return wp_page_copy(mm, vma, address, page_table, pmd, 23312f38ab2cSShachar Raindel orig_pte, old_page); 2332251b97f5SPeter Zijlstra } 23331da177e4SLinus Torvalds 2334d08b3851SPeter Zijlstra /* 2335ee6a6457SPeter Zijlstra * Take out anonymous pages first, anonymous shared vmas are 2336ee6a6457SPeter Zijlstra * not dirty accountable. 2337d08b3851SPeter Zijlstra */ 23389a840895SHugh Dickins if (PageAnon(old_page) && !PageKsm(old_page)) { 2339ab967d86SHugh Dickins if (!trylock_page(old_page)) { 2340ab967d86SHugh Dickins page_cache_get(old_page); 2341ab967d86SHugh Dickins pte_unmap_unlock(page_table, ptl); 2342ab967d86SHugh Dickins lock_page(old_page); 2343ab967d86SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, 2344ab967d86SHugh Dickins &ptl); 2345ab967d86SHugh Dickins if (!pte_same(*page_table, orig_pte)) { 2346ab967d86SHugh Dickins unlock_page(old_page); 234728766805SShachar Raindel pte_unmap_unlock(page_table, ptl); 234828766805SShachar Raindel page_cache_release(old_page); 234928766805SShachar Raindel return 0; 2350ab967d86SHugh Dickins } 2351ab967d86SHugh Dickins page_cache_release(old_page); 2352ab967d86SHugh Dickins } 2353b009c024SMichel Lespinasse if (reuse_swap_page(old_page)) { 2354c44b6743SRik van Riel /* 2355c44b6743SRik van Riel * The page is all ours. Move it to our anon_vma so 2356c44b6743SRik van Riel * the rmap code will not search our parent or siblings. 2357c44b6743SRik van Riel * Protected against the rmap code by the page lock. 2358c44b6743SRik van Riel */ 2359c44b6743SRik van Riel page_move_anon_rmap(old_page, vma, address); 2360ee6a6457SPeter Zijlstra unlock_page(old_page); 23614e047f89SShachar Raindel return wp_page_reuse(mm, vma, address, page_table, ptl, 23624e047f89SShachar Raindel orig_pte, old_page, 0, 0); 2363b009c024SMichel Lespinasse } 2364b009c024SMichel Lespinasse unlock_page(old_page); 2365ee6a6457SPeter Zijlstra } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 2366d08b3851SPeter Zijlstra (VM_WRITE|VM_SHARED))) { 236793e478d4SShachar Raindel return wp_page_shared(mm, vma, address, page_table, pmd, 236893e478d4SShachar Raindel ptl, orig_pte, old_page); 23691da177e4SLinus Torvalds } 23701da177e4SLinus Torvalds 23711da177e4SLinus Torvalds /* 23721da177e4SLinus Torvalds * Ok, we need to copy. Oh, well.. 23731da177e4SLinus Torvalds */ 23741da177e4SLinus Torvalds page_cache_get(old_page); 237528766805SShachar Raindel 23768f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 23772f38ab2cSShachar Raindel return wp_page_copy(mm, vma, address, page_table, pmd, 23782f38ab2cSShachar Raindel orig_pte, old_page); 23791da177e4SLinus Torvalds } 23801da177e4SLinus Torvalds 238197a89413SPeter Zijlstra static void unmap_mapping_range_vma(struct vm_area_struct *vma, 23821da177e4SLinus Torvalds unsigned long start_addr, unsigned long end_addr, 23831da177e4SLinus Torvalds struct zap_details *details) 23841da177e4SLinus Torvalds { 2385f5cc4eefSAl Viro zap_page_range_single(vma, start_addr, end_addr - start_addr, details); 23861da177e4SLinus Torvalds } 23871da177e4SLinus Torvalds 23886b2dbba8SMichel Lespinasse static inline void unmap_mapping_range_tree(struct rb_root *root, 23891da177e4SLinus Torvalds struct zap_details *details) 23901da177e4SLinus Torvalds { 23911da177e4SLinus Torvalds struct vm_area_struct *vma; 23921da177e4SLinus Torvalds pgoff_t vba, vea, zba, zea; 23931da177e4SLinus Torvalds 23946b2dbba8SMichel Lespinasse vma_interval_tree_foreach(vma, root, 23951da177e4SLinus Torvalds details->first_index, details->last_index) { 23961da177e4SLinus Torvalds 23971da177e4SLinus Torvalds vba = vma->vm_pgoff; 2398d6e93217SLibin vea = vba + vma_pages(vma) - 1; 23991da177e4SLinus Torvalds /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */ 24001da177e4SLinus Torvalds zba = details->first_index; 24011da177e4SLinus Torvalds if (zba < vba) 24021da177e4SLinus Torvalds zba = vba; 24031da177e4SLinus Torvalds zea = details->last_index; 24041da177e4SLinus Torvalds if (zea > vea) 24051da177e4SLinus Torvalds zea = vea; 24061da177e4SLinus Torvalds 240797a89413SPeter Zijlstra unmap_mapping_range_vma(vma, 24081da177e4SLinus Torvalds ((zba - vba) << PAGE_SHIFT) + vma->vm_start, 24091da177e4SLinus Torvalds ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start, 241097a89413SPeter Zijlstra details); 24111da177e4SLinus Torvalds } 24121da177e4SLinus Torvalds } 24131da177e4SLinus Torvalds 24141da177e4SLinus Torvalds /** 24158a5f14a2SKirill A. Shutemov * unmap_mapping_range - unmap the portion of all mmaps in the specified 24168a5f14a2SKirill A. Shutemov * address_space corresponding to the specified page range in the underlying 24178a5f14a2SKirill A. Shutemov * file. 24188a5f14a2SKirill A. Shutemov * 24193d41088fSMartin Waitz * @mapping: the address space containing mmaps to be unmapped. 24201da177e4SLinus Torvalds * @holebegin: byte in first page to unmap, relative to the start of 24211da177e4SLinus Torvalds * the underlying file. This will be rounded down to a PAGE_SIZE 242225d9e2d1Snpiggin@suse.de * boundary. Note that this is different from truncate_pagecache(), which 24231da177e4SLinus Torvalds * must keep the partial page. In contrast, we must get rid of 24241da177e4SLinus Torvalds * partial pages. 24251da177e4SLinus Torvalds * @holelen: size of prospective hole in bytes. This will be rounded 24261da177e4SLinus Torvalds * up to a PAGE_SIZE boundary. A holelen of zero truncates to the 24271da177e4SLinus Torvalds * end of the file. 24281da177e4SLinus Torvalds * @even_cows: 1 when truncating a file, unmap even private COWed pages; 24291da177e4SLinus Torvalds * but 0 when invalidating pagecache, don't throw away private data. 24301da177e4SLinus Torvalds */ 24311da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping, 24321da177e4SLinus Torvalds loff_t const holebegin, loff_t const holelen, int even_cows) 24331da177e4SLinus Torvalds { 24341da177e4SLinus Torvalds struct zap_details details; 24351da177e4SLinus Torvalds pgoff_t hba = holebegin >> PAGE_SHIFT; 24361da177e4SLinus Torvalds pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 24371da177e4SLinus Torvalds 24381da177e4SLinus Torvalds /* Check for overflow. */ 24391da177e4SLinus Torvalds if (sizeof(holelen) > sizeof(hlen)) { 24401da177e4SLinus Torvalds long long holeend = 24411da177e4SLinus Torvalds (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 24421da177e4SLinus Torvalds if (holeend & ~(long long)ULONG_MAX) 24431da177e4SLinus Torvalds hlen = ULONG_MAX - hba + 1; 24441da177e4SLinus Torvalds } 24451da177e4SLinus Torvalds 24461da177e4SLinus Torvalds details.check_mapping = even_cows? NULL: mapping; 24471da177e4SLinus Torvalds details.first_index = hba; 24481da177e4SLinus Torvalds details.last_index = hba + hlen - 1; 24491da177e4SLinus Torvalds if (details.last_index < details.first_index) 24501da177e4SLinus Torvalds details.last_index = ULONG_MAX; 24511da177e4SLinus Torvalds 24520f90cc66SRoss Zwisler 24530f90cc66SRoss Zwisler /* DAX uses i_mmap_lock to serialise file truncate vs page fault */ 245448ec833bSKirill A. Shutemov i_mmap_lock_write(mapping); 24556b2dbba8SMichel Lespinasse if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap))) 24561da177e4SLinus Torvalds unmap_mapping_range_tree(&mapping->i_mmap, &details); 245748ec833bSKirill A. Shutemov i_mmap_unlock_write(mapping); 24581da177e4SLinus Torvalds } 24591da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range); 24601da177e4SLinus Torvalds 24611da177e4SLinus Torvalds /* 24628f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 24638f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 24649a95f3cfSPaul Cassella * We return with pte unmapped and unlocked. 24659a95f3cfSPaul Cassella * 24669a95f3cfSPaul Cassella * We return with the mmap_sem locked or unlocked in the same cases 24679a95f3cfSPaul Cassella * as does filemap_fault(). 24681da177e4SLinus Torvalds */ 246965500d23SHugh Dickins static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma, 247065500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 247130c9f3a9SLinus Torvalds unsigned int flags, pte_t orig_pte) 24721da177e4SLinus Torvalds { 24738f4e2101SHugh Dickins spinlock_t *ptl; 247456f31801SHugh Dickins struct page *page, *swapcache; 247500501b53SJohannes Weiner struct mem_cgroup *memcg; 247665500d23SHugh Dickins swp_entry_t entry; 24771da177e4SLinus Torvalds pte_t pte; 2478d065bd81SMichel Lespinasse int locked; 2479ad8c2ee8SRik van Riel int exclusive = 0; 248083c54070SNick Piggin int ret = 0; 24811da177e4SLinus Torvalds 24824c21e2f2SHugh Dickins if (!pte_unmap_same(mm, pmd, page_table, orig_pte)) 24838f4e2101SHugh Dickins goto out; 248465500d23SHugh Dickins 248565500d23SHugh Dickins entry = pte_to_swp_entry(orig_pte); 2486d1737fdbSAndi Kleen if (unlikely(non_swap_entry(entry))) { 24870697212aSChristoph Lameter if (is_migration_entry(entry)) { 24880697212aSChristoph Lameter migration_entry_wait(mm, pmd, address); 2489d1737fdbSAndi Kleen } else if (is_hwpoison_entry(entry)) { 2490d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 2491d1737fdbSAndi Kleen } else { 2492d1737fdbSAndi Kleen print_bad_pte(vma, address, orig_pte, NULL); 2493d99be1a8SHugh Dickins ret = VM_FAULT_SIGBUS; 2494d1737fdbSAndi Kleen } 24950697212aSChristoph Lameter goto out; 24960697212aSChristoph Lameter } 24970ff92245SShailabh Nagar delayacct_set_flag(DELAYACCT_PF_SWAPIN); 24981da177e4SLinus Torvalds page = lookup_swap_cache(entry); 24991da177e4SLinus Torvalds if (!page) { 250002098feaSHugh Dickins page = swapin_readahead(entry, 250102098feaSHugh Dickins GFP_HIGHUSER_MOVABLE, vma, address); 25021da177e4SLinus Torvalds if (!page) { 25031da177e4SLinus Torvalds /* 25048f4e2101SHugh Dickins * Back out if somebody else faulted in this pte 25058f4e2101SHugh Dickins * while we released the pte lock. 25061da177e4SLinus Torvalds */ 25078f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 25081da177e4SLinus Torvalds if (likely(pte_same(*page_table, orig_pte))) 25091da177e4SLinus Torvalds ret = VM_FAULT_OOM; 25100ff92245SShailabh Nagar delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 251165500d23SHugh Dickins goto unlock; 25121da177e4SLinus Torvalds } 25131da177e4SLinus Torvalds 25141da177e4SLinus Torvalds /* Had to read the page from swap area: Major fault */ 25151da177e4SLinus Torvalds ret = VM_FAULT_MAJOR; 2516f8891e5eSChristoph Lameter count_vm_event(PGMAJFAULT); 2517456f998eSYing Han mem_cgroup_count_vm_event(mm, PGMAJFAULT); 2518d1737fdbSAndi Kleen } else if (PageHWPoison(page)) { 251971f72525SWu Fengguang /* 252071f72525SWu Fengguang * hwpoisoned dirty swapcache pages are kept for killing 252171f72525SWu Fengguang * owner processes (which may be unknown at hwpoison time) 252271f72525SWu Fengguang */ 2523d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 2524d1737fdbSAndi Kleen delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 252556f31801SHugh Dickins swapcache = page; 25264779cb31SAndi Kleen goto out_release; 25271da177e4SLinus Torvalds } 25281da177e4SLinus Torvalds 252956f31801SHugh Dickins swapcache = page; 2530d065bd81SMichel Lespinasse locked = lock_page_or_retry(page, mm, flags); 2531e709ffd6SRik van Riel 253220a1022dSBalbir Singh delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 2533d065bd81SMichel Lespinasse if (!locked) { 2534d065bd81SMichel Lespinasse ret |= VM_FAULT_RETRY; 2535d065bd81SMichel Lespinasse goto out_release; 2536d065bd81SMichel Lespinasse } 25371da177e4SLinus Torvalds 25384969c119SAndrea Arcangeli /* 253931c4a3d3SHugh Dickins * Make sure try_to_free_swap or reuse_swap_page or swapoff did not 254031c4a3d3SHugh Dickins * release the swapcache from under us. The page pin, and pte_same 254131c4a3d3SHugh Dickins * test below, are not enough to exclude that. Even if it is still 254231c4a3d3SHugh Dickins * swapcache, we need to check that the page's swap has not changed. 25434969c119SAndrea Arcangeli */ 254431c4a3d3SHugh Dickins if (unlikely(!PageSwapCache(page) || page_private(page) != entry.val)) 25454969c119SAndrea Arcangeli goto out_page; 25464969c119SAndrea Arcangeli 2547cbf86cfeSHugh Dickins page = ksm_might_need_to_copy(page, vma, address); 25484969c119SAndrea Arcangeli if (unlikely(!page)) { 25495ad64688SHugh Dickins ret = VM_FAULT_OOM; 25504969c119SAndrea Arcangeli page = swapcache; 25514969c119SAndrea Arcangeli goto out_page; 25524969c119SAndrea Arcangeli } 25535ad64688SHugh Dickins 2554f627c2f5SKirill A. Shutemov if (mem_cgroup_try_charge(page, mm, GFP_KERNEL, &memcg, false)) { 2555073e587eSKAMEZAWA Hiroyuki ret = VM_FAULT_OOM; 2556bc43f75cSJohannes Weiner goto out_page; 2557073e587eSKAMEZAWA Hiroyuki } 2558073e587eSKAMEZAWA Hiroyuki 25591da177e4SLinus Torvalds /* 25608f4e2101SHugh Dickins * Back out if somebody else already faulted in this pte. 25611da177e4SLinus Torvalds */ 25628f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 25639e9bef07SHugh Dickins if (unlikely(!pte_same(*page_table, orig_pte))) 2564b8107480SKirill Korotaev goto out_nomap; 2565b8107480SKirill Korotaev 2566b8107480SKirill Korotaev if (unlikely(!PageUptodate(page))) { 2567b8107480SKirill Korotaev ret = VM_FAULT_SIGBUS; 2568b8107480SKirill Korotaev goto out_nomap; 25691da177e4SLinus Torvalds } 25701da177e4SLinus Torvalds 25718c7c6e34SKAMEZAWA Hiroyuki /* 25728c7c6e34SKAMEZAWA Hiroyuki * The page isn't present yet, go ahead with the fault. 25738c7c6e34SKAMEZAWA Hiroyuki * 25748c7c6e34SKAMEZAWA Hiroyuki * Be careful about the sequence of operations here. 25758c7c6e34SKAMEZAWA Hiroyuki * To get its accounting right, reuse_swap_page() must be called 25768c7c6e34SKAMEZAWA Hiroyuki * while the page is counted on swap but not yet in mapcount i.e. 25778c7c6e34SKAMEZAWA Hiroyuki * before page_add_anon_rmap() and swap_free(); try_to_free_swap() 25788c7c6e34SKAMEZAWA Hiroyuki * must be called after the swap_free(), or it will never succeed. 25798c7c6e34SKAMEZAWA Hiroyuki */ 25801da177e4SLinus Torvalds 258134e55232SKAMEZAWA Hiroyuki inc_mm_counter_fast(mm, MM_ANONPAGES); 2582b084d435SKAMEZAWA Hiroyuki dec_mm_counter_fast(mm, MM_SWAPENTS); 25831da177e4SLinus Torvalds pte = mk_pte(page, vma->vm_page_prot); 258430c9f3a9SLinus Torvalds if ((flags & FAULT_FLAG_WRITE) && reuse_swap_page(page)) { 25851da177e4SLinus Torvalds pte = maybe_mkwrite(pte_mkdirty(pte), vma); 258630c9f3a9SLinus Torvalds flags &= ~FAULT_FLAG_WRITE; 25879a5b489bSAndrea Arcangeli ret |= VM_FAULT_WRITE; 2588d281ee61SKirill A. Shutemov exclusive = RMAP_EXCLUSIVE; 25891da177e4SLinus Torvalds } 25901da177e4SLinus Torvalds flush_icache_page(vma, page); 2591179ef71cSCyrill Gorcunov if (pte_swp_soft_dirty(orig_pte)) 2592179ef71cSCyrill Gorcunov pte = pte_mksoft_dirty(pte); 25931da177e4SLinus Torvalds set_pte_at(mm, address, page_table, pte); 259400501b53SJohannes Weiner if (page == swapcache) { 2595ad8c2ee8SRik van Riel do_page_add_anon_rmap(page, vma, address, exclusive); 2596f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, true, false); 259700501b53SJohannes Weiner } else { /* ksm created a completely new copy */ 2598d281ee61SKirill A. Shutemov page_add_new_anon_rmap(page, vma, address, false); 2599f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, false); 260000501b53SJohannes Weiner lru_cache_add_active_or_unevictable(page, vma); 260100501b53SJohannes Weiner } 26021da177e4SLinus Torvalds 2603c475a8abSHugh Dickins swap_free(entry); 26045ccc5abaSVladimir Davydov if (mem_cgroup_swap_full(page) || 26055ccc5abaSVladimir Davydov (vma->vm_flags & VM_LOCKED) || PageMlocked(page)) 2606a2c43eedSHugh Dickins try_to_free_swap(page); 2607c475a8abSHugh Dickins unlock_page(page); 260856f31801SHugh Dickins if (page != swapcache) { 26094969c119SAndrea Arcangeli /* 26104969c119SAndrea Arcangeli * Hold the lock to avoid the swap entry to be reused 26114969c119SAndrea Arcangeli * until we take the PT lock for the pte_same() check 26124969c119SAndrea Arcangeli * (to avoid false positives from pte_same). For 26134969c119SAndrea Arcangeli * further safety release the lock after the swap_free 26144969c119SAndrea Arcangeli * so that the swap count won't change under a 26154969c119SAndrea Arcangeli * parallel locked swapcache. 26164969c119SAndrea Arcangeli */ 26174969c119SAndrea Arcangeli unlock_page(swapcache); 26184969c119SAndrea Arcangeli page_cache_release(swapcache); 26194969c119SAndrea Arcangeli } 2620c475a8abSHugh Dickins 262130c9f3a9SLinus Torvalds if (flags & FAULT_FLAG_WRITE) { 262261469f1dSHugh Dickins ret |= do_wp_page(mm, vma, address, page_table, pmd, ptl, pte); 262361469f1dSHugh Dickins if (ret & VM_FAULT_ERROR) 262461469f1dSHugh Dickins ret &= VM_FAULT_ERROR; 26251da177e4SLinus Torvalds goto out; 26261da177e4SLinus Torvalds } 26271da177e4SLinus Torvalds 26281da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 26294b3073e1SRussell King update_mmu_cache(vma, address, page_table); 263065500d23SHugh Dickins unlock: 26318f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 26321da177e4SLinus Torvalds out: 26331da177e4SLinus Torvalds return ret; 2634b8107480SKirill Korotaev out_nomap: 2635f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 26368f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 2637bc43f75cSJohannes Weiner out_page: 2638b8107480SKirill Korotaev unlock_page(page); 26394779cb31SAndi Kleen out_release: 2640b8107480SKirill Korotaev page_cache_release(page); 264156f31801SHugh Dickins if (page != swapcache) { 26424969c119SAndrea Arcangeli unlock_page(swapcache); 26434969c119SAndrea Arcangeli page_cache_release(swapcache); 26444969c119SAndrea Arcangeli } 264565500d23SHugh Dickins return ret; 26461da177e4SLinus Torvalds } 26471da177e4SLinus Torvalds 26481da177e4SLinus Torvalds /* 26498ca3eb08SLuck, Tony * This is like a special single-page "expand_{down|up}wards()", 26508ca3eb08SLuck, Tony * except we must first make sure that 'address{-|+}PAGE_SIZE' 2651320b2b8dSLinus Torvalds * doesn't hit another vma. 2652320b2b8dSLinus Torvalds */ 2653320b2b8dSLinus Torvalds static inline int check_stack_guard_page(struct vm_area_struct *vma, unsigned long address) 2654320b2b8dSLinus Torvalds { 2655320b2b8dSLinus Torvalds address &= PAGE_MASK; 2656320b2b8dSLinus Torvalds if ((vma->vm_flags & VM_GROWSDOWN) && address == vma->vm_start) { 26570e8e50e2SLinus Torvalds struct vm_area_struct *prev = vma->vm_prev; 2658320b2b8dSLinus Torvalds 26590e8e50e2SLinus Torvalds /* 26600e8e50e2SLinus Torvalds * Is there a mapping abutting this one below? 26610e8e50e2SLinus Torvalds * 26620e8e50e2SLinus Torvalds * That's only ok if it's the same stack mapping 26630e8e50e2SLinus Torvalds * that has gotten split.. 26640e8e50e2SLinus Torvalds */ 26650e8e50e2SLinus Torvalds if (prev && prev->vm_end == address) 26660e8e50e2SLinus Torvalds return prev->vm_flags & VM_GROWSDOWN ? 0 : -ENOMEM; 26670e8e50e2SLinus Torvalds 2668fee7e49dSLinus Torvalds return expand_downwards(vma, address - PAGE_SIZE); 2669320b2b8dSLinus Torvalds } 26708ca3eb08SLuck, Tony if ((vma->vm_flags & VM_GROWSUP) && address + PAGE_SIZE == vma->vm_end) { 26718ca3eb08SLuck, Tony struct vm_area_struct *next = vma->vm_next; 26728ca3eb08SLuck, Tony 26738ca3eb08SLuck, Tony /* As VM_GROWSDOWN but s/below/above/ */ 26748ca3eb08SLuck, Tony if (next && next->vm_start == address + PAGE_SIZE) 26758ca3eb08SLuck, Tony return next->vm_flags & VM_GROWSUP ? 0 : -ENOMEM; 26768ca3eb08SLuck, Tony 2677fee7e49dSLinus Torvalds return expand_upwards(vma, address + PAGE_SIZE); 26788ca3eb08SLuck, Tony } 2679320b2b8dSLinus Torvalds return 0; 2680320b2b8dSLinus Torvalds } 2681320b2b8dSLinus Torvalds 2682320b2b8dSLinus Torvalds /* 26838f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 26848f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 26858f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 26861da177e4SLinus Torvalds */ 268765500d23SHugh Dickins static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma, 268865500d23SHugh Dickins unsigned long address, pte_t *page_table, pmd_t *pmd, 268930c9f3a9SLinus Torvalds unsigned int flags) 26901da177e4SLinus Torvalds { 269100501b53SJohannes Weiner struct mem_cgroup *memcg; 26928f4e2101SHugh Dickins struct page *page; 26938f4e2101SHugh Dickins spinlock_t *ptl; 26941da177e4SLinus Torvalds pte_t entry; 26951da177e4SLinus Torvalds 26965528f913SLinus Torvalds pte_unmap(page_table); 2697320b2b8dSLinus Torvalds 26986b7339f4SKirill A. Shutemov /* File mapping without ->vm_ops ? */ 26996b7339f4SKirill A. Shutemov if (vma->vm_flags & VM_SHARED) 27006b7339f4SKirill A. Shutemov return VM_FAULT_SIGBUS; 27016b7339f4SKirill A. Shutemov 270211ac5524SLinus Torvalds /* Check if we need to add a guard page to the stack */ 270311ac5524SLinus Torvalds if (check_stack_guard_page(vma, address) < 0) 27049c145c56SLinus Torvalds return VM_FAULT_SIGSEGV; 270511ac5524SLinus Torvalds 270611ac5524SLinus Torvalds /* Use the zero-page for reads */ 2707593befa6SDominik Dingel if (!(flags & FAULT_FLAG_WRITE) && !mm_forbids_zeropage(mm)) { 270862eede62SHugh Dickins entry = pte_mkspecial(pfn_pte(my_zero_pfn(address), 270962eede62SHugh Dickins vma->vm_page_prot)); 271011ac5524SLinus Torvalds page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 2711a13ea5b7SHugh Dickins if (!pte_none(*page_table)) 2712a13ea5b7SHugh Dickins goto unlock; 27136b251fc9SAndrea Arcangeli /* Deliver the page fault to userland, check inside PT lock */ 27146b251fc9SAndrea Arcangeli if (userfaultfd_missing(vma)) { 27156b251fc9SAndrea Arcangeli pte_unmap_unlock(page_table, ptl); 27166b251fc9SAndrea Arcangeli return handle_userfault(vma, address, flags, 27176b251fc9SAndrea Arcangeli VM_UFFD_MISSING); 27186b251fc9SAndrea Arcangeli } 2719a13ea5b7SHugh Dickins goto setpte; 2720a13ea5b7SHugh Dickins } 2721a13ea5b7SHugh Dickins 27221da177e4SLinus Torvalds /* Allocate our own private page. */ 27231da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 272465500d23SHugh Dickins goto oom; 2725769848c0SMel Gorman page = alloc_zeroed_user_highpage_movable(vma, address); 27261da177e4SLinus Torvalds if (!page) 272765500d23SHugh Dickins goto oom; 2728eb3c24f3SMel Gorman 2729f627c2f5SKirill A. Shutemov if (mem_cgroup_try_charge(page, mm, GFP_KERNEL, &memcg, false)) 2730eb3c24f3SMel Gorman goto oom_free_page; 2731eb3c24f3SMel Gorman 273252f37629SMinchan Kim /* 273352f37629SMinchan Kim * The memory barrier inside __SetPageUptodate makes sure that 273452f37629SMinchan Kim * preceeding stores to the page contents become visible before 273552f37629SMinchan Kim * the set_pte_at() write. 273652f37629SMinchan Kim */ 27370ed361deSNick Piggin __SetPageUptodate(page); 27381da177e4SLinus Torvalds 273965500d23SHugh Dickins entry = mk_pte(page, vma->vm_page_prot); 27401ac0cb5dSHugh Dickins if (vma->vm_flags & VM_WRITE) 27411ac0cb5dSHugh Dickins entry = pte_mkwrite(pte_mkdirty(entry)); 27428f4e2101SHugh Dickins 27438f4e2101SHugh Dickins page_table = pte_offset_map_lock(mm, pmd, address, &ptl); 27441c2fb7a4SAndrea Arcangeli if (!pte_none(*page_table)) 27458f4e2101SHugh Dickins goto release; 27469ba69294SHugh Dickins 27476b251fc9SAndrea Arcangeli /* Deliver the page fault to userland, check inside PT lock */ 27486b251fc9SAndrea Arcangeli if (userfaultfd_missing(vma)) { 27496b251fc9SAndrea Arcangeli pte_unmap_unlock(page_table, ptl); 2750f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 27516b251fc9SAndrea Arcangeli page_cache_release(page); 27526b251fc9SAndrea Arcangeli return handle_userfault(vma, address, flags, 27536b251fc9SAndrea Arcangeli VM_UFFD_MISSING); 27546b251fc9SAndrea Arcangeli } 27556b251fc9SAndrea Arcangeli 275634e55232SKAMEZAWA Hiroyuki inc_mm_counter_fast(mm, MM_ANONPAGES); 2757d281ee61SKirill A. Shutemov page_add_new_anon_rmap(page, vma, address, false); 2758f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, false); 275900501b53SJohannes Weiner lru_cache_add_active_or_unevictable(page, vma); 2760a13ea5b7SHugh Dickins setpte: 276165500d23SHugh Dickins set_pte_at(mm, address, page_table, entry); 27621da177e4SLinus Torvalds 27631da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 27644b3073e1SRussell King update_mmu_cache(vma, address, page_table); 276565500d23SHugh Dickins unlock: 27668f4e2101SHugh Dickins pte_unmap_unlock(page_table, ptl); 276783c54070SNick Piggin return 0; 27688f4e2101SHugh Dickins release: 2769f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 27708f4e2101SHugh Dickins page_cache_release(page); 27718f4e2101SHugh Dickins goto unlock; 27728a9f3ccdSBalbir Singh oom_free_page: 27736dbf6d3bSHugh Dickins page_cache_release(page); 277465500d23SHugh Dickins oom: 27751da177e4SLinus Torvalds return VM_FAULT_OOM; 27761da177e4SLinus Torvalds } 27771da177e4SLinus Torvalds 27789a95f3cfSPaul Cassella /* 27799a95f3cfSPaul Cassella * The mmap_sem must have been held on entry, and may have been 27809a95f3cfSPaul Cassella * released depending on flags and vma->vm_ops->fault() return value. 27819a95f3cfSPaul Cassella * See filemap_fault() and __lock_page_retry(). 27829a95f3cfSPaul Cassella */ 27837eae74afSKirill A. Shutemov static int __do_fault(struct vm_area_struct *vma, unsigned long address, 27842e4cdab0SMatthew Wilcox pgoff_t pgoff, unsigned int flags, 27852e4cdab0SMatthew Wilcox struct page *cow_page, struct page **page) 27867eae74afSKirill A. Shutemov { 27877eae74afSKirill A. Shutemov struct vm_fault vmf; 27887eae74afSKirill A. Shutemov int ret; 27897eae74afSKirill A. Shutemov 27907eae74afSKirill A. Shutemov vmf.virtual_address = (void __user *)(address & PAGE_MASK); 27917eae74afSKirill A. Shutemov vmf.pgoff = pgoff; 27927eae74afSKirill A. Shutemov vmf.flags = flags; 27937eae74afSKirill A. Shutemov vmf.page = NULL; 2794c20cd45eSMichal Hocko vmf.gfp_mask = __get_fault_gfp_mask(vma); 27952e4cdab0SMatthew Wilcox vmf.cow_page = cow_page; 27967eae74afSKirill A. Shutemov 27977eae74afSKirill A. Shutemov ret = vma->vm_ops->fault(vma, &vmf); 27987eae74afSKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 27997eae74afSKirill A. Shutemov return ret; 28002e4cdab0SMatthew Wilcox if (!vmf.page) 28012e4cdab0SMatthew Wilcox goto out; 28027eae74afSKirill A. Shutemov 28037eae74afSKirill A. Shutemov if (unlikely(PageHWPoison(vmf.page))) { 28047eae74afSKirill A. Shutemov if (ret & VM_FAULT_LOCKED) 28057eae74afSKirill A. Shutemov unlock_page(vmf.page); 28067eae74afSKirill A. Shutemov page_cache_release(vmf.page); 28077eae74afSKirill A. Shutemov return VM_FAULT_HWPOISON; 28087eae74afSKirill A. Shutemov } 28097eae74afSKirill A. Shutemov 28107eae74afSKirill A. Shutemov if (unlikely(!(ret & VM_FAULT_LOCKED))) 28117eae74afSKirill A. Shutemov lock_page(vmf.page); 28127eae74afSKirill A. Shutemov else 28137eae74afSKirill A. Shutemov VM_BUG_ON_PAGE(!PageLocked(vmf.page), vmf.page); 28147eae74afSKirill A. Shutemov 28152e4cdab0SMatthew Wilcox out: 28167eae74afSKirill A. Shutemov *page = vmf.page; 28177eae74afSKirill A. Shutemov return ret; 28187eae74afSKirill A. Shutemov } 28197eae74afSKirill A. Shutemov 28208c6e50b0SKirill A. Shutemov /** 28218c6e50b0SKirill A. Shutemov * do_set_pte - setup new PTE entry for given page and add reverse page mapping. 28228c6e50b0SKirill A. Shutemov * 28238c6e50b0SKirill A. Shutemov * @vma: virtual memory area 28248c6e50b0SKirill A. Shutemov * @address: user virtual address 28258c6e50b0SKirill A. Shutemov * @page: page to map 28268c6e50b0SKirill A. Shutemov * @pte: pointer to target page table entry 28278c6e50b0SKirill A. Shutemov * @write: true, if new entry is writable 28288c6e50b0SKirill A. Shutemov * @anon: true, if it's anonymous page 28298c6e50b0SKirill A. Shutemov * 28308c6e50b0SKirill A. Shutemov * Caller must hold page table lock relevant for @pte. 28318c6e50b0SKirill A. Shutemov * 28328c6e50b0SKirill A. Shutemov * Target users are page handler itself and implementations of 28338c6e50b0SKirill A. Shutemov * vm_ops->map_pages. 28348c6e50b0SKirill A. Shutemov */ 28358c6e50b0SKirill A. Shutemov void do_set_pte(struct vm_area_struct *vma, unsigned long address, 28363bb97794SKirill A. Shutemov struct page *page, pte_t *pte, bool write, bool anon) 28373bb97794SKirill A. Shutemov { 28383bb97794SKirill A. Shutemov pte_t entry; 28393bb97794SKirill A. Shutemov 28403bb97794SKirill A. Shutemov flush_icache_page(vma, page); 28413bb97794SKirill A. Shutemov entry = mk_pte(page, vma->vm_page_prot); 28423bb97794SKirill A. Shutemov if (write) 28433bb97794SKirill A. Shutemov entry = maybe_mkwrite(pte_mkdirty(entry), vma); 28443bb97794SKirill A. Shutemov if (anon) { 28453bb97794SKirill A. Shutemov inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); 2846d281ee61SKirill A. Shutemov page_add_new_anon_rmap(page, vma, address, false); 28473bb97794SKirill A. Shutemov } else { 2848eca56ff9SJerome Marchand inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page)); 28493bb97794SKirill A. Shutemov page_add_file_rmap(page); 28503bb97794SKirill A. Shutemov } 28513bb97794SKirill A. Shutemov set_pte_at(vma->vm_mm, address, pte, entry); 28523bb97794SKirill A. Shutemov 28533bb97794SKirill A. Shutemov /* no need to invalidate: a not-present page won't be cached */ 28543bb97794SKirill A. Shutemov update_mmu_cache(vma, address, pte); 28553bb97794SKirill A. Shutemov } 28563bb97794SKirill A. Shutemov 28573a91053aSKirill A. Shutemov static unsigned long fault_around_bytes __read_mostly = 28583a91053aSKirill A. Shutemov rounddown_pow_of_two(65536); 2859a9b0f861SKirill A. Shutemov 28601592eef0SKirill A. Shutemov #ifdef CONFIG_DEBUG_FS 2861a9b0f861SKirill A. Shutemov static int fault_around_bytes_get(void *data, u64 *val) 28621592eef0SKirill A. Shutemov { 2863a9b0f861SKirill A. Shutemov *val = fault_around_bytes; 28641592eef0SKirill A. Shutemov return 0; 28651592eef0SKirill A. Shutemov } 28661592eef0SKirill A. Shutemov 2867b4903d6eSAndrey Ryabinin /* 2868b4903d6eSAndrey Ryabinin * fault_around_pages() and fault_around_mask() expects fault_around_bytes 2869b4903d6eSAndrey Ryabinin * rounded down to nearest page order. It's what do_fault_around() expects to 2870b4903d6eSAndrey Ryabinin * see. 2871b4903d6eSAndrey Ryabinin */ 2872a9b0f861SKirill A. Shutemov static int fault_around_bytes_set(void *data, u64 val) 28731592eef0SKirill A. Shutemov { 2874a9b0f861SKirill A. Shutemov if (val / PAGE_SIZE > PTRS_PER_PTE) 28751592eef0SKirill A. Shutemov return -EINVAL; 2876b4903d6eSAndrey Ryabinin if (val > PAGE_SIZE) 2877b4903d6eSAndrey Ryabinin fault_around_bytes = rounddown_pow_of_two(val); 2878b4903d6eSAndrey Ryabinin else 2879b4903d6eSAndrey Ryabinin fault_around_bytes = PAGE_SIZE; /* rounddown_pow_of_two(0) is undefined */ 28801592eef0SKirill A. Shutemov return 0; 28811592eef0SKirill A. Shutemov } 2882a9b0f861SKirill A. Shutemov DEFINE_SIMPLE_ATTRIBUTE(fault_around_bytes_fops, 2883a9b0f861SKirill A. Shutemov fault_around_bytes_get, fault_around_bytes_set, "%llu\n"); 28841592eef0SKirill A. Shutemov 28851592eef0SKirill A. Shutemov static int __init fault_around_debugfs(void) 28861592eef0SKirill A. Shutemov { 28871592eef0SKirill A. Shutemov void *ret; 28881592eef0SKirill A. Shutemov 2889a9b0f861SKirill A. Shutemov ret = debugfs_create_file("fault_around_bytes", 0644, NULL, NULL, 2890a9b0f861SKirill A. Shutemov &fault_around_bytes_fops); 28911592eef0SKirill A. Shutemov if (!ret) 2892a9b0f861SKirill A. Shutemov pr_warn("Failed to create fault_around_bytes in debugfs"); 28931592eef0SKirill A. Shutemov return 0; 28941592eef0SKirill A. Shutemov } 28951592eef0SKirill A. Shutemov late_initcall(fault_around_debugfs); 28961592eef0SKirill A. Shutemov #endif 28978c6e50b0SKirill A. Shutemov 28981fdb412bSKirill A. Shutemov /* 28991fdb412bSKirill A. Shutemov * do_fault_around() tries to map few pages around the fault address. The hope 29001fdb412bSKirill A. Shutemov * is that the pages will be needed soon and this will lower the number of 29011fdb412bSKirill A. Shutemov * faults to handle. 29021fdb412bSKirill A. Shutemov * 29031fdb412bSKirill A. Shutemov * It uses vm_ops->map_pages() to map the pages, which skips the page if it's 29041fdb412bSKirill A. Shutemov * not ready to be mapped: not up-to-date, locked, etc. 29051fdb412bSKirill A. Shutemov * 29061fdb412bSKirill A. Shutemov * This function is called with the page table lock taken. In the split ptlock 29071fdb412bSKirill A. Shutemov * case the page table lock only protects only those entries which belong to 29081fdb412bSKirill A. Shutemov * the page table corresponding to the fault address. 29091fdb412bSKirill A. Shutemov * 29101fdb412bSKirill A. Shutemov * This function doesn't cross the VMA boundaries, in order to call map_pages() 29111fdb412bSKirill A. Shutemov * only once. 29121fdb412bSKirill A. Shutemov * 29131fdb412bSKirill A. Shutemov * fault_around_pages() defines how many pages we'll try to map. 29141fdb412bSKirill A. Shutemov * do_fault_around() expects it to return a power of two less than or equal to 29151fdb412bSKirill A. Shutemov * PTRS_PER_PTE. 29161fdb412bSKirill A. Shutemov * 29171fdb412bSKirill A. Shutemov * The virtual address of the area that we map is naturally aligned to the 29181fdb412bSKirill A. Shutemov * fault_around_pages() value (and therefore to page order). This way it's 29191fdb412bSKirill A. Shutemov * easier to guarantee that we don't cross page table boundaries. 29201fdb412bSKirill A. Shutemov */ 29218c6e50b0SKirill A. Shutemov static void do_fault_around(struct vm_area_struct *vma, unsigned long address, 29228c6e50b0SKirill A. Shutemov pte_t *pte, pgoff_t pgoff, unsigned int flags) 29238c6e50b0SKirill A. Shutemov { 2924aecd6f44SKirill A. Shutemov unsigned long start_addr, nr_pages, mask; 29258c6e50b0SKirill A. Shutemov pgoff_t max_pgoff; 29268c6e50b0SKirill A. Shutemov struct vm_fault vmf; 29278c6e50b0SKirill A. Shutemov int off; 29288c6e50b0SKirill A. Shutemov 29294db0c3c2SJason Low nr_pages = READ_ONCE(fault_around_bytes) >> PAGE_SHIFT; 2930aecd6f44SKirill A. Shutemov mask = ~(nr_pages * PAGE_SIZE - 1) & PAGE_MASK; 2931aecd6f44SKirill A. Shutemov 2932aecd6f44SKirill A. Shutemov start_addr = max(address & mask, vma->vm_start); 29338c6e50b0SKirill A. Shutemov off = ((address - start_addr) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1); 29348c6e50b0SKirill A. Shutemov pte -= off; 29358c6e50b0SKirill A. Shutemov pgoff -= off; 29368c6e50b0SKirill A. Shutemov 29378c6e50b0SKirill A. Shutemov /* 29388c6e50b0SKirill A. Shutemov * max_pgoff is either end of page table or end of vma 2939850e9c69SKirill A. Shutemov * or fault_around_pages() from pgoff, depending what is nearest. 29408c6e50b0SKirill A. Shutemov */ 29418c6e50b0SKirill A. Shutemov max_pgoff = pgoff - ((start_addr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) + 29428c6e50b0SKirill A. Shutemov PTRS_PER_PTE - 1; 29438c6e50b0SKirill A. Shutemov max_pgoff = min3(max_pgoff, vma_pages(vma) + vma->vm_pgoff - 1, 2944aecd6f44SKirill A. Shutemov pgoff + nr_pages - 1); 29458c6e50b0SKirill A. Shutemov 29468c6e50b0SKirill A. Shutemov /* Check if it makes any sense to call ->map_pages */ 29478c6e50b0SKirill A. Shutemov while (!pte_none(*pte)) { 29488c6e50b0SKirill A. Shutemov if (++pgoff > max_pgoff) 29498c6e50b0SKirill A. Shutemov return; 29508c6e50b0SKirill A. Shutemov start_addr += PAGE_SIZE; 29518c6e50b0SKirill A. Shutemov if (start_addr >= vma->vm_end) 29528c6e50b0SKirill A. Shutemov return; 29538c6e50b0SKirill A. Shutemov pte++; 29548c6e50b0SKirill A. Shutemov } 29558c6e50b0SKirill A. Shutemov 29568c6e50b0SKirill A. Shutemov vmf.virtual_address = (void __user *) start_addr; 29578c6e50b0SKirill A. Shutemov vmf.pte = pte; 29588c6e50b0SKirill A. Shutemov vmf.pgoff = pgoff; 29598c6e50b0SKirill A. Shutemov vmf.max_pgoff = max_pgoff; 29608c6e50b0SKirill A. Shutemov vmf.flags = flags; 2961c20cd45eSMichal Hocko vmf.gfp_mask = __get_fault_gfp_mask(vma); 29628c6e50b0SKirill A. Shutemov vma->vm_ops->map_pages(vma, &vmf); 29638c6e50b0SKirill A. Shutemov } 29648c6e50b0SKirill A. Shutemov 2965e655fb29SKirill A. Shutemov static int do_read_fault(struct mm_struct *mm, struct vm_area_struct *vma, 2966e655fb29SKirill A. Shutemov unsigned long address, pmd_t *pmd, 2967e655fb29SKirill A. Shutemov pgoff_t pgoff, unsigned int flags, pte_t orig_pte) 2968e655fb29SKirill A. Shutemov { 2969e655fb29SKirill A. Shutemov struct page *fault_page; 2970e655fb29SKirill A. Shutemov spinlock_t *ptl; 29713bb97794SKirill A. Shutemov pte_t *pte; 29728c6e50b0SKirill A. Shutemov int ret = 0; 29738c6e50b0SKirill A. Shutemov 29748c6e50b0SKirill A. Shutemov /* 29758c6e50b0SKirill A. Shutemov * Let's call ->map_pages() first and use ->fault() as fallback 29768c6e50b0SKirill A. Shutemov * if page by the offset is not ready to be mapped (cold cache or 29778c6e50b0SKirill A. Shutemov * something). 29788c6e50b0SKirill A. Shutemov */ 29799b4bdd2fSKirill A. Shutemov if (vma->vm_ops->map_pages && fault_around_bytes >> PAGE_SHIFT > 1) { 29808c6e50b0SKirill A. Shutemov pte = pte_offset_map_lock(mm, pmd, address, &ptl); 29818c6e50b0SKirill A. Shutemov do_fault_around(vma, address, pte, pgoff, flags); 29828c6e50b0SKirill A. Shutemov if (!pte_same(*pte, orig_pte)) 29838c6e50b0SKirill A. Shutemov goto unlock_out; 29848c6e50b0SKirill A. Shutemov pte_unmap_unlock(pte, ptl); 29858c6e50b0SKirill A. Shutemov } 2986e655fb29SKirill A. Shutemov 29872e4cdab0SMatthew Wilcox ret = __do_fault(vma, address, pgoff, flags, NULL, &fault_page); 2988e655fb29SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 2989e655fb29SKirill A. Shutemov return ret; 2990e655fb29SKirill A. Shutemov 2991e655fb29SKirill A. Shutemov pte = pte_offset_map_lock(mm, pmd, address, &ptl); 2992e655fb29SKirill A. Shutemov if (unlikely(!pte_same(*pte, orig_pte))) { 2993e655fb29SKirill A. Shutemov pte_unmap_unlock(pte, ptl); 2994e655fb29SKirill A. Shutemov unlock_page(fault_page); 2995e655fb29SKirill A. Shutemov page_cache_release(fault_page); 2996e655fb29SKirill A. Shutemov return ret; 2997e655fb29SKirill A. Shutemov } 29983bb97794SKirill A. Shutemov do_set_pte(vma, address, fault_page, pte, false, false); 2999e655fb29SKirill A. Shutemov unlock_page(fault_page); 30008c6e50b0SKirill A. Shutemov unlock_out: 30018c6e50b0SKirill A. Shutemov pte_unmap_unlock(pte, ptl); 3002e655fb29SKirill A. Shutemov return ret; 3003e655fb29SKirill A. Shutemov } 3004e655fb29SKirill A. Shutemov 3005ec47c3b9SKirill A. Shutemov static int do_cow_fault(struct mm_struct *mm, struct vm_area_struct *vma, 3006ec47c3b9SKirill A. Shutemov unsigned long address, pmd_t *pmd, 3007ec47c3b9SKirill A. Shutemov pgoff_t pgoff, unsigned int flags, pte_t orig_pte) 3008ec47c3b9SKirill A. Shutemov { 3009ec47c3b9SKirill A. Shutemov struct page *fault_page, *new_page; 301000501b53SJohannes Weiner struct mem_cgroup *memcg; 3011ec47c3b9SKirill A. Shutemov spinlock_t *ptl; 30123bb97794SKirill A. Shutemov pte_t *pte; 3013ec47c3b9SKirill A. Shutemov int ret; 3014ec47c3b9SKirill A. Shutemov 3015ec47c3b9SKirill A. Shutemov if (unlikely(anon_vma_prepare(vma))) 3016ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 3017ec47c3b9SKirill A. Shutemov 3018ec47c3b9SKirill A. Shutemov new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address); 3019ec47c3b9SKirill A. Shutemov if (!new_page) 3020ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 3021ec47c3b9SKirill A. Shutemov 3022f627c2f5SKirill A. Shutemov if (mem_cgroup_try_charge(new_page, mm, GFP_KERNEL, &memcg, false)) { 3023ec47c3b9SKirill A. Shutemov page_cache_release(new_page); 3024ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 3025ec47c3b9SKirill A. Shutemov } 3026ec47c3b9SKirill A. Shutemov 30272e4cdab0SMatthew Wilcox ret = __do_fault(vma, address, pgoff, flags, new_page, &fault_page); 3028ec47c3b9SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3029ec47c3b9SKirill A. Shutemov goto uncharge_out; 3030ec47c3b9SKirill A. Shutemov 30312e4cdab0SMatthew Wilcox if (fault_page) 3032ec47c3b9SKirill A. Shutemov copy_user_highpage(new_page, fault_page, address, vma); 3033ec47c3b9SKirill A. Shutemov __SetPageUptodate(new_page); 3034ec47c3b9SKirill A. Shutemov 3035ec47c3b9SKirill A. Shutemov pte = pte_offset_map_lock(mm, pmd, address, &ptl); 3036ec47c3b9SKirill A. Shutemov if (unlikely(!pte_same(*pte, orig_pte))) { 3037ec47c3b9SKirill A. Shutemov pte_unmap_unlock(pte, ptl); 30382e4cdab0SMatthew Wilcox if (fault_page) { 3039ec47c3b9SKirill A. Shutemov unlock_page(fault_page); 3040ec47c3b9SKirill A. Shutemov page_cache_release(fault_page); 30412e4cdab0SMatthew Wilcox } else { 30422e4cdab0SMatthew Wilcox /* 30432e4cdab0SMatthew Wilcox * The fault handler has no page to lock, so it holds 30440df9d41aSYigal Korman * i_mmap_lock for read to protect against truncate. 30452e4cdab0SMatthew Wilcox */ 30460df9d41aSYigal Korman i_mmap_unlock_read(vma->vm_file->f_mapping); 30472e4cdab0SMatthew Wilcox } 3048ec47c3b9SKirill A. Shutemov goto uncharge_out; 3049ec47c3b9SKirill A. Shutemov } 30503bb97794SKirill A. Shutemov do_set_pte(vma, address, new_page, pte, true, true); 3051f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(new_page, memcg, false, false); 305200501b53SJohannes Weiner lru_cache_add_active_or_unevictable(new_page, vma); 3053ec47c3b9SKirill A. Shutemov pte_unmap_unlock(pte, ptl); 30542e4cdab0SMatthew Wilcox if (fault_page) { 3055ec47c3b9SKirill A. Shutemov unlock_page(fault_page); 3056ec47c3b9SKirill A. Shutemov page_cache_release(fault_page); 30572e4cdab0SMatthew Wilcox } else { 30582e4cdab0SMatthew Wilcox /* 30592e4cdab0SMatthew Wilcox * The fault handler has no page to lock, so it holds 30600df9d41aSYigal Korman * i_mmap_lock for read to protect against truncate. 30612e4cdab0SMatthew Wilcox */ 30620df9d41aSYigal Korman i_mmap_unlock_read(vma->vm_file->f_mapping); 30632e4cdab0SMatthew Wilcox } 3064ec47c3b9SKirill A. Shutemov return ret; 3065ec47c3b9SKirill A. Shutemov uncharge_out: 3066f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(new_page, memcg, false); 3067ec47c3b9SKirill A. Shutemov page_cache_release(new_page); 3068ec47c3b9SKirill A. Shutemov return ret; 3069ec47c3b9SKirill A. Shutemov } 3070ec47c3b9SKirill A. Shutemov 3071f0c6d4d2SKirill A. Shutemov static int do_shared_fault(struct mm_struct *mm, struct vm_area_struct *vma, 307216abfa08SHugh Dickins unsigned long address, pmd_t *pmd, 307354cb8821SNick Piggin pgoff_t pgoff, unsigned int flags, pte_t orig_pte) 30741da177e4SLinus Torvalds { 3075f0c6d4d2SKirill A. Shutemov struct page *fault_page; 3076f0c6d4d2SKirill A. Shutemov struct address_space *mapping; 30778f4e2101SHugh Dickins spinlock_t *ptl; 30783bb97794SKirill A. Shutemov pte_t *pte; 3079f0c6d4d2SKirill A. Shutemov int dirtied = 0; 3080f0c6d4d2SKirill A. Shutemov int ret, tmp; 30811d65f86dSKAMEZAWA Hiroyuki 30822e4cdab0SMatthew Wilcox ret = __do_fault(vma, address, pgoff, flags, NULL, &fault_page); 30837eae74afSKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3084f0c6d4d2SKirill A. Shutemov return ret; 30851da177e4SLinus Torvalds 30861da177e4SLinus Torvalds /* 3087f0c6d4d2SKirill A. Shutemov * Check if the backing address space wants to know that the page is 3088f0c6d4d2SKirill A. Shutemov * about to become writable 30891da177e4SLinus Torvalds */ 3090fb09a464SKirill A. Shutemov if (vma->vm_ops->page_mkwrite) { 3091f0c6d4d2SKirill A. Shutemov unlock_page(fault_page); 3092fb09a464SKirill A. Shutemov tmp = do_page_mkwrite(vma, fault_page, address); 3093fb09a464SKirill A. Shutemov if (unlikely(!tmp || 3094fb09a464SKirill A. Shutemov (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { 3095f0c6d4d2SKirill A. Shutemov page_cache_release(fault_page); 3096f0c6d4d2SKirill A. Shutemov return tmp; 309769676147SMark Fasheh } 3098d0217ac0SNick Piggin } 3099fb09a464SKirill A. Shutemov 3100f0c6d4d2SKirill A. Shutemov pte = pte_offset_map_lock(mm, pmd, address, &ptl); 3101f0c6d4d2SKirill A. Shutemov if (unlikely(!pte_same(*pte, orig_pte))) { 3102f0c6d4d2SKirill A. Shutemov pte_unmap_unlock(pte, ptl); 3103f0c6d4d2SKirill A. Shutemov unlock_page(fault_page); 3104f0c6d4d2SKirill A. Shutemov page_cache_release(fault_page); 3105f0c6d4d2SKirill A. Shutemov return ret; 31069637a5efSDavid Howells } 31073bb97794SKirill A. Shutemov do_set_pte(vma, address, fault_page, pte, true, false); 3108f0c6d4d2SKirill A. Shutemov pte_unmap_unlock(pte, ptl); 3109d00806b1SNick Piggin 3110f0c6d4d2SKirill A. Shutemov if (set_page_dirty(fault_page)) 311141c4d25fSJan Kara dirtied = 1; 3112d82fa87dSAndrew Morton /* 3113d82fa87dSAndrew Morton * Take a local copy of the address_space - page.mapping may be zeroed 3114d82fa87dSAndrew Morton * by truncate after unlock_page(). The address_space itself remains 3115d82fa87dSAndrew Morton * pinned by vma->vm_file's reference. We rely on unlock_page()'s 3116d82fa87dSAndrew Morton * release semantics to prevent the compiler from undoing this copying. 3117d82fa87dSAndrew Morton */ 31181c290f64SKirill A. Shutemov mapping = page_rmapping(fault_page); 3119f0c6d4d2SKirill A. Shutemov unlock_page(fault_page); 3120f0c6d4d2SKirill A. Shutemov if ((dirtied || vma->vm_ops->page_mkwrite) && mapping) { 3121b827e496SNick Piggin /* 3122b827e496SNick Piggin * Some device drivers do not set page.mapping but still 3123b827e496SNick Piggin * dirty their pages 3124b827e496SNick Piggin */ 3125b827e496SNick Piggin balance_dirty_pages_ratelimited(mapping); 3126d08b3851SPeter Zijlstra } 3127d00806b1SNick Piggin 312874ec6751SJohannes Weiner if (!vma->vm_ops->page_mkwrite) 3129b827e496SNick Piggin file_update_time(vma->vm_file); 3130b827e496SNick Piggin 3131b827e496SNick Piggin return ret; 313254cb8821SNick Piggin } 3133d00806b1SNick Piggin 31349a95f3cfSPaul Cassella /* 31359a95f3cfSPaul Cassella * We enter with non-exclusive mmap_sem (to exclude vma changes, 31369a95f3cfSPaul Cassella * but allow concurrent faults). 31379a95f3cfSPaul Cassella * The mmap_sem may have been released depending on flags and our 31389a95f3cfSPaul Cassella * return value. See filemap_fault() and __lock_page_or_retry(). 31399a95f3cfSPaul Cassella */ 31409b4bdd2fSKirill A. Shutemov static int do_fault(struct mm_struct *mm, struct vm_area_struct *vma, 314154cb8821SNick Piggin unsigned long address, pte_t *page_table, pmd_t *pmd, 314230c9f3a9SLinus Torvalds unsigned int flags, pte_t orig_pte) 314354cb8821SNick Piggin { 314488193f7cSMatthew Wilcox pgoff_t pgoff = linear_page_index(vma, address); 314554cb8821SNick Piggin 314616abfa08SHugh Dickins pte_unmap(page_table); 31476b7339f4SKirill A. Shutemov /* The VMA was not fully populated on mmap() or missing VM_DONTEXPAND */ 31486b7339f4SKirill A. Shutemov if (!vma->vm_ops->fault) 31496b7339f4SKirill A. Shutemov return VM_FAULT_SIGBUS; 3150e655fb29SKirill A. Shutemov if (!(flags & FAULT_FLAG_WRITE)) 3151e655fb29SKirill A. Shutemov return do_read_fault(mm, vma, address, pmd, pgoff, flags, 3152e655fb29SKirill A. Shutemov orig_pte); 3153ec47c3b9SKirill A. Shutemov if (!(vma->vm_flags & VM_SHARED)) 3154ec47c3b9SKirill A. Shutemov return do_cow_fault(mm, vma, address, pmd, pgoff, flags, 3155ec47c3b9SKirill A. Shutemov orig_pte); 3156f0c6d4d2SKirill A. Shutemov return do_shared_fault(mm, vma, address, pmd, pgoff, flags, orig_pte); 315754cb8821SNick Piggin } 315854cb8821SNick Piggin 3159b19a9939SRashika Kheria static int numa_migrate_prep(struct page *page, struct vm_area_struct *vma, 316004bb2f94SRik van Riel unsigned long addr, int page_nid, 316104bb2f94SRik van Riel int *flags) 31629532fec1SMel Gorman { 31639532fec1SMel Gorman get_page(page); 31649532fec1SMel Gorman 31659532fec1SMel Gorman count_vm_numa_event(NUMA_HINT_FAULTS); 316604bb2f94SRik van Riel if (page_nid == numa_node_id()) { 31679532fec1SMel Gorman count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL); 316804bb2f94SRik van Riel *flags |= TNF_FAULT_LOCAL; 316904bb2f94SRik van Riel } 31709532fec1SMel Gorman 31719532fec1SMel Gorman return mpol_misplaced(page, vma, addr); 31729532fec1SMel Gorman } 31739532fec1SMel Gorman 3174b19a9939SRashika Kheria static int do_numa_page(struct mm_struct *mm, struct vm_area_struct *vma, 3175d10e63f2SMel Gorman unsigned long addr, pte_t pte, pte_t *ptep, pmd_t *pmd) 3176d10e63f2SMel Gorman { 31774daae3b4SMel Gorman struct page *page = NULL; 3178d10e63f2SMel Gorman spinlock_t *ptl; 31798191acbdSMel Gorman int page_nid = -1; 318090572890SPeter Zijlstra int last_cpupid; 3181cbee9f88SPeter Zijlstra int target_nid; 3182b8593bfdSMel Gorman bool migrated = false; 3183b191f9b1SMel Gorman bool was_writable = pte_write(pte); 31846688cc05SPeter Zijlstra int flags = 0; 3185d10e63f2SMel Gorman 3186c0e7cad9SMel Gorman /* A PROT_NONE fault should not end up here */ 3187c0e7cad9SMel Gorman BUG_ON(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))); 3188c0e7cad9SMel Gorman 3189d10e63f2SMel Gorman /* 3190d10e63f2SMel Gorman * The "pte" at this point cannot be used safely without 3191d10e63f2SMel Gorman * validation through pte_unmap_same(). It's of NUMA type but 3192d10e63f2SMel Gorman * the pfn may be screwed if the read is non atomic. 3193d10e63f2SMel Gorman * 31944d942466SMel Gorman * We can safely just do a "set_pte_at()", because the old 31954d942466SMel Gorman * page table entry is not accessible, so there would be no 31964d942466SMel Gorman * concurrent hardware modifications to the PTE. 3197d10e63f2SMel Gorman */ 3198d10e63f2SMel Gorman ptl = pte_lockptr(mm, pmd); 3199d10e63f2SMel Gorman spin_lock(ptl); 32004daae3b4SMel Gorman if (unlikely(!pte_same(*ptep, pte))) { 32014daae3b4SMel Gorman pte_unmap_unlock(ptep, ptl); 32024daae3b4SMel Gorman goto out; 32034daae3b4SMel Gorman } 32044daae3b4SMel Gorman 32054d942466SMel Gorman /* Make it present again */ 32064d942466SMel Gorman pte = pte_modify(pte, vma->vm_page_prot); 32074d942466SMel Gorman pte = pte_mkyoung(pte); 3208b191f9b1SMel Gorman if (was_writable) 3209b191f9b1SMel Gorman pte = pte_mkwrite(pte); 3210d10e63f2SMel Gorman set_pte_at(mm, addr, ptep, pte); 3211d10e63f2SMel Gorman update_mmu_cache(vma, addr, ptep); 3212d10e63f2SMel Gorman 3213d10e63f2SMel Gorman page = vm_normal_page(vma, addr, pte); 3214d10e63f2SMel Gorman if (!page) { 3215d10e63f2SMel Gorman pte_unmap_unlock(ptep, ptl); 3216d10e63f2SMel Gorman return 0; 3217d10e63f2SMel Gorman } 3218d10e63f2SMel Gorman 3219e81c4802SKirill A. Shutemov /* TODO: handle PTE-mapped THP */ 3220e81c4802SKirill A. Shutemov if (PageCompound(page)) { 3221e81c4802SKirill A. Shutemov pte_unmap_unlock(ptep, ptl); 3222e81c4802SKirill A. Shutemov return 0; 3223e81c4802SKirill A. Shutemov } 3224e81c4802SKirill A. Shutemov 32256688cc05SPeter Zijlstra /* 3226bea66fbdSMel Gorman * Avoid grouping on RO pages in general. RO pages shouldn't hurt as 3227bea66fbdSMel Gorman * much anyway since they can be in shared cache state. This misses 3228bea66fbdSMel Gorman * the case where a mapping is writable but the process never writes 3229bea66fbdSMel Gorman * to it but pte_write gets cleared during protection updates and 3230bea66fbdSMel Gorman * pte_dirty has unpredictable behaviour between PTE scan updates, 3231bea66fbdSMel Gorman * background writeback, dirty balancing and application behaviour. 32326688cc05SPeter Zijlstra */ 3233bea66fbdSMel Gorman if (!(vma->vm_flags & VM_WRITE)) 32346688cc05SPeter Zijlstra flags |= TNF_NO_GROUP; 32356688cc05SPeter Zijlstra 3236dabe1d99SRik van Riel /* 3237dabe1d99SRik van Riel * Flag if the page is shared between multiple address spaces. This 3238dabe1d99SRik van Riel * is later used when determining whether to group tasks together 3239dabe1d99SRik van Riel */ 3240dabe1d99SRik van Riel if (page_mapcount(page) > 1 && (vma->vm_flags & VM_SHARED)) 3241dabe1d99SRik van Riel flags |= TNF_SHARED; 3242dabe1d99SRik van Riel 324390572890SPeter Zijlstra last_cpupid = page_cpupid_last(page); 32448191acbdSMel Gorman page_nid = page_to_nid(page); 324504bb2f94SRik van Riel target_nid = numa_migrate_prep(page, vma, addr, page_nid, &flags); 3246d10e63f2SMel Gorman pte_unmap_unlock(ptep, ptl); 32474daae3b4SMel Gorman if (target_nid == -1) { 32484daae3b4SMel Gorman put_page(page); 32494daae3b4SMel Gorman goto out; 32504daae3b4SMel Gorman } 32514daae3b4SMel Gorman 32524daae3b4SMel Gorman /* Migrate to the requested node */ 32531bc115d8SMel Gorman migrated = migrate_misplaced_page(page, vma, target_nid); 32546688cc05SPeter Zijlstra if (migrated) { 32558191acbdSMel Gorman page_nid = target_nid; 32566688cc05SPeter Zijlstra flags |= TNF_MIGRATED; 3257074c2381SMel Gorman } else 3258074c2381SMel Gorman flags |= TNF_MIGRATE_FAIL; 32594daae3b4SMel Gorman 32604daae3b4SMel Gorman out: 32618191acbdSMel Gorman if (page_nid != -1) 32626688cc05SPeter Zijlstra task_numa_fault(last_cpupid, page_nid, 1, flags); 3263d10e63f2SMel Gorman return 0; 3264d10e63f2SMel Gorman } 3265d10e63f2SMel Gorman 3266b96375f7SMatthew Wilcox static int create_huge_pmd(struct mm_struct *mm, struct vm_area_struct *vma, 3267b96375f7SMatthew Wilcox unsigned long address, pmd_t *pmd, unsigned int flags) 3268b96375f7SMatthew Wilcox { 3269fb6dd5faSKirill A. Shutemov if (vma_is_anonymous(vma)) 3270b96375f7SMatthew Wilcox return do_huge_pmd_anonymous_page(mm, vma, address, pmd, flags); 3271b96375f7SMatthew Wilcox if (vma->vm_ops->pmd_fault) 3272b96375f7SMatthew Wilcox return vma->vm_ops->pmd_fault(vma, address, pmd, flags); 3273b96375f7SMatthew Wilcox return VM_FAULT_FALLBACK; 3274b96375f7SMatthew Wilcox } 3275b96375f7SMatthew Wilcox 3276b96375f7SMatthew Wilcox static int wp_huge_pmd(struct mm_struct *mm, struct vm_area_struct *vma, 3277b96375f7SMatthew Wilcox unsigned long address, pmd_t *pmd, pmd_t orig_pmd, 3278b96375f7SMatthew Wilcox unsigned int flags) 3279b96375f7SMatthew Wilcox { 3280fb6dd5faSKirill A. Shutemov if (vma_is_anonymous(vma)) 3281b96375f7SMatthew Wilcox return do_huge_pmd_wp_page(mm, vma, address, pmd, orig_pmd); 3282b96375f7SMatthew Wilcox if (vma->vm_ops->pmd_fault) 3283b96375f7SMatthew Wilcox return vma->vm_ops->pmd_fault(vma, address, pmd, flags); 3284b96375f7SMatthew Wilcox return VM_FAULT_FALLBACK; 3285b96375f7SMatthew Wilcox } 3286b96375f7SMatthew Wilcox 32871da177e4SLinus Torvalds /* 32881da177e4SLinus Torvalds * These routines also need to handle stuff like marking pages dirty 32891da177e4SLinus Torvalds * and/or accessed for architectures that don't do it in hardware (most 32901da177e4SLinus Torvalds * RISC architectures). The early dirtying is also good on the i386. 32911da177e4SLinus Torvalds * 32921da177e4SLinus Torvalds * There is also a hook called "update_mmu_cache()" that architectures 32931da177e4SLinus Torvalds * with external mmu caches can use to update those (ie the Sparc or 32941da177e4SLinus Torvalds * PowerPC hashed page tables that act as extended TLBs). 32951da177e4SLinus Torvalds * 3296c74df32cSHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 3297c74df32cSHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 32989a95f3cfSPaul Cassella * We return with pte unmapped and unlocked. 32999a95f3cfSPaul Cassella * 33009a95f3cfSPaul Cassella * The mmap_sem may have been released depending on flags and our 33019a95f3cfSPaul Cassella * return value. See filemap_fault() and __lock_page_or_retry(). 33021da177e4SLinus Torvalds */ 3303c0292554SKirill A. Shutemov static int handle_pte_fault(struct mm_struct *mm, 33041da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long address, 330530c9f3a9SLinus Torvalds pte_t *pte, pmd_t *pmd, unsigned int flags) 33061da177e4SLinus Torvalds { 33071da177e4SLinus Torvalds pte_t entry; 33088f4e2101SHugh Dickins spinlock_t *ptl; 33091da177e4SLinus Torvalds 3310e37c6982SChristian Borntraeger /* 3311e37c6982SChristian Borntraeger * some architectures can have larger ptes than wordsize, 3312e37c6982SChristian Borntraeger * e.g.ppc44x-defconfig has CONFIG_PTE_64BIT=y and CONFIG_32BIT=y, 3313e37c6982SChristian Borntraeger * so READ_ONCE or ACCESS_ONCE cannot guarantee atomic accesses. 3314e37c6982SChristian Borntraeger * The code below just needs a consistent view for the ifs and 3315e37c6982SChristian Borntraeger * we later double check anyway with the ptl lock held. So here 3316e37c6982SChristian Borntraeger * a barrier will do. 3317e37c6982SChristian Borntraeger */ 3318e37c6982SChristian Borntraeger entry = *pte; 3319e37c6982SChristian Borntraeger barrier(); 33201da177e4SLinus Torvalds if (!pte_present(entry)) { 332165500d23SHugh Dickins if (pte_none(entry)) { 3322b5330628SOleg Nesterov if (vma_is_anonymous(vma)) 3323b5330628SOleg Nesterov return do_anonymous_page(mm, vma, address, 3324b5330628SOleg Nesterov pte, pmd, flags); 3325b5330628SOleg Nesterov else 33266b7339f4SKirill A. Shutemov return do_fault(mm, vma, address, pte, pmd, 33276b7339f4SKirill A. Shutemov flags, entry); 332865500d23SHugh Dickins } 332965500d23SHugh Dickins return do_swap_page(mm, vma, address, 333030c9f3a9SLinus Torvalds pte, pmd, flags, entry); 33311da177e4SLinus Torvalds } 33321da177e4SLinus Torvalds 33338a0516edSMel Gorman if (pte_protnone(entry)) 3334d10e63f2SMel Gorman return do_numa_page(mm, vma, address, entry, pte, pmd); 3335d10e63f2SMel Gorman 33364c21e2f2SHugh Dickins ptl = pte_lockptr(mm, pmd); 33378f4e2101SHugh Dickins spin_lock(ptl); 33388f4e2101SHugh Dickins if (unlikely(!pte_same(*pte, entry))) 33398f4e2101SHugh Dickins goto unlock; 334030c9f3a9SLinus Torvalds if (flags & FAULT_FLAG_WRITE) { 33411da177e4SLinus Torvalds if (!pte_write(entry)) 33428f4e2101SHugh Dickins return do_wp_page(mm, vma, address, 33438f4e2101SHugh Dickins pte, pmd, ptl, entry); 33441da177e4SLinus Torvalds entry = pte_mkdirty(entry); 33451da177e4SLinus Torvalds } 33461da177e4SLinus Torvalds entry = pte_mkyoung(entry); 334730c9f3a9SLinus Torvalds if (ptep_set_access_flags(vma, address, pte, entry, flags & FAULT_FLAG_WRITE)) { 33484b3073e1SRussell King update_mmu_cache(vma, address, pte); 33491a44e149SAndrea Arcangeli } else { 33501a44e149SAndrea Arcangeli /* 33511a44e149SAndrea Arcangeli * This is needed only for protection faults but the arch code 33521a44e149SAndrea Arcangeli * is not yet telling us if this is a protection fault or not. 33531a44e149SAndrea Arcangeli * This still avoids useless tlb flushes for .text page faults 33541a44e149SAndrea Arcangeli * with threads. 33551a44e149SAndrea Arcangeli */ 335630c9f3a9SLinus Torvalds if (flags & FAULT_FLAG_WRITE) 335761c77326SShaohua Li flush_tlb_fix_spurious_fault(vma, address); 33581a44e149SAndrea Arcangeli } 33598f4e2101SHugh Dickins unlock: 33608f4e2101SHugh Dickins pte_unmap_unlock(pte, ptl); 336183c54070SNick Piggin return 0; 33621da177e4SLinus Torvalds } 33631da177e4SLinus Torvalds 33641da177e4SLinus Torvalds /* 33651da177e4SLinus Torvalds * By the time we get here, we already hold the mm semaphore 33669a95f3cfSPaul Cassella * 33679a95f3cfSPaul Cassella * The mmap_sem may have been released depending on flags and our 33689a95f3cfSPaul Cassella * return value. See filemap_fault() and __lock_page_or_retry(). 33691da177e4SLinus Torvalds */ 3370519e5247SJohannes Weiner static int __handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma, 3371d06063ccSLinus Torvalds unsigned long address, unsigned int flags) 33721da177e4SLinus Torvalds { 33731da177e4SLinus Torvalds pgd_t *pgd; 33741da177e4SLinus Torvalds pud_t *pud; 33751da177e4SLinus Torvalds pmd_t *pmd; 33761da177e4SLinus Torvalds pte_t *pte; 33771da177e4SLinus Torvalds 3378ac9b9c66SHugh Dickins if (unlikely(is_vm_hugetlb_page(vma))) 337930c9f3a9SLinus Torvalds return hugetlb_fault(mm, vma, address, flags); 33801da177e4SLinus Torvalds 33811da177e4SLinus Torvalds pgd = pgd_offset(mm, address); 33821da177e4SLinus Torvalds pud = pud_alloc(mm, pgd, address); 33831da177e4SLinus Torvalds if (!pud) 3384c74df32cSHugh Dickins return VM_FAULT_OOM; 33851da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, address); 33861da177e4SLinus Torvalds if (!pmd) 3387c74df32cSHugh Dickins return VM_FAULT_OOM; 338871e3aac0SAndrea Arcangeli if (pmd_none(*pmd) && transparent_hugepage_enabled(vma)) { 3389b96375f7SMatthew Wilcox int ret = create_huge_pmd(mm, vma, address, pmd, flags); 3390c0292554SKirill A. Shutemov if (!(ret & VM_FAULT_FALLBACK)) 3391c0292554SKirill A. Shutemov return ret; 339271e3aac0SAndrea Arcangeli } else { 339371e3aac0SAndrea Arcangeli pmd_t orig_pmd = *pmd; 33941f1d06c3SDavid Rientjes int ret; 33951f1d06c3SDavid Rientjes 339671e3aac0SAndrea Arcangeli barrier(); 33975c7fb56eSDan Williams if (pmd_trans_huge(orig_pmd) || pmd_devmap(orig_pmd)) { 3398a1dd450bSWill Deacon unsigned int dirty = flags & FAULT_FLAG_WRITE; 3399a1dd450bSWill Deacon 34008a0516edSMel Gorman if (pmd_protnone(orig_pmd)) 34014daae3b4SMel Gorman return do_huge_pmd_numa_page(mm, vma, address, 3402d10e63f2SMel Gorman orig_pmd, pmd); 3403d10e63f2SMel Gorman 34043d59eebcSLinus Torvalds if (dirty && !pmd_write(orig_pmd)) { 3405b96375f7SMatthew Wilcox ret = wp_huge_pmd(mm, vma, address, pmd, 3406b96375f7SMatthew Wilcox orig_pmd, flags); 34079845cbbdSKirill A. Shutemov if (!(ret & VM_FAULT_FALLBACK)) 34081f1d06c3SDavid Rientjes return ret; 3409a1dd450bSWill Deacon } else { 3410a1dd450bSWill Deacon huge_pmd_set_accessed(mm, vma, address, pmd, 3411a1dd450bSWill Deacon orig_pmd, dirty); 341271e3aac0SAndrea Arcangeli return 0; 341371e3aac0SAndrea Arcangeli } 341471e3aac0SAndrea Arcangeli } 34159845cbbdSKirill A. Shutemov } 341671e3aac0SAndrea Arcangeli 341771e3aac0SAndrea Arcangeli /* 34183ed3a4f0SKirill A. Shutemov * Use pte_alloc() instead of pte_alloc_map, because we can't 341971e3aac0SAndrea Arcangeli * run pte_offset_map on the pmd, if an huge pmd could 342071e3aac0SAndrea Arcangeli * materialize from under us from a different thread. 342171e3aac0SAndrea Arcangeli */ 34223ed3a4f0SKirill A. Shutemov if (unlikely(pte_alloc(mm, pmd, address))) 3423c74df32cSHugh Dickins return VM_FAULT_OOM; 3424ad33bb04SAndrea Arcangeli /* 3425ad33bb04SAndrea Arcangeli * If a huge pmd materialized under us just retry later. Use 3426ad33bb04SAndrea Arcangeli * pmd_trans_unstable() instead of pmd_trans_huge() to ensure the pmd 3427ad33bb04SAndrea Arcangeli * didn't become pmd_trans_huge under us and then back to pmd_none, as 3428ad33bb04SAndrea Arcangeli * a result of MADV_DONTNEED running immediately after a huge pmd fault 3429ad33bb04SAndrea Arcangeli * in a different thread of this mm, in turn leading to a misleading 3430ad33bb04SAndrea Arcangeli * pmd_trans_huge() retval. All we have to ensure is that it is a 3431ad33bb04SAndrea Arcangeli * regular pmd that we can walk with pte_offset_map() and we can do that 3432ad33bb04SAndrea Arcangeli * through an atomic read in C, which is what pmd_trans_unstable() 3433ad33bb04SAndrea Arcangeli * provides. 3434ad33bb04SAndrea Arcangeli */ 3435ad33bb04SAndrea Arcangeli if (unlikely(pmd_trans_unstable(pmd) || pmd_devmap(*pmd))) 343671e3aac0SAndrea Arcangeli return 0; 343771e3aac0SAndrea Arcangeli /* 343871e3aac0SAndrea Arcangeli * A regular pmd is established and it can't morph into a huge pmd 343971e3aac0SAndrea Arcangeli * from under us anymore at this point because we hold the mmap_sem 344071e3aac0SAndrea Arcangeli * read mode and khugepaged takes it in write mode. So now it's 344171e3aac0SAndrea Arcangeli * safe to run pte_offset_map(). 344271e3aac0SAndrea Arcangeli */ 344371e3aac0SAndrea Arcangeli pte = pte_offset_map(pmd, address); 34441da177e4SLinus Torvalds 344530c9f3a9SLinus Torvalds return handle_pte_fault(mm, vma, address, pte, pmd, flags); 34461da177e4SLinus Torvalds } 34471da177e4SLinus Torvalds 34489a95f3cfSPaul Cassella /* 34499a95f3cfSPaul Cassella * By the time we get here, we already hold the mm semaphore 34509a95f3cfSPaul Cassella * 34519a95f3cfSPaul Cassella * The mmap_sem may have been released depending on flags and our 34529a95f3cfSPaul Cassella * return value. See filemap_fault() and __lock_page_or_retry(). 34539a95f3cfSPaul Cassella */ 3454519e5247SJohannes Weiner int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma, 3455519e5247SJohannes Weiner unsigned long address, unsigned int flags) 3456519e5247SJohannes Weiner { 3457519e5247SJohannes Weiner int ret; 3458519e5247SJohannes Weiner 3459519e5247SJohannes Weiner __set_current_state(TASK_RUNNING); 3460519e5247SJohannes Weiner 3461519e5247SJohannes Weiner count_vm_event(PGFAULT); 3462519e5247SJohannes Weiner mem_cgroup_count_vm_event(mm, PGFAULT); 3463519e5247SJohannes Weiner 3464519e5247SJohannes Weiner /* do counter updates before entering really critical section. */ 3465519e5247SJohannes Weiner check_sync_rss_stat(current); 3466519e5247SJohannes Weiner 3467519e5247SJohannes Weiner /* 3468519e5247SJohannes Weiner * Enable the memcg OOM handling for faults triggered in user 3469519e5247SJohannes Weiner * space. Kernel faults are handled more gracefully. 3470519e5247SJohannes Weiner */ 3471519e5247SJohannes Weiner if (flags & FAULT_FLAG_USER) 347249426420SJohannes Weiner mem_cgroup_oom_enable(); 3473519e5247SJohannes Weiner 3474519e5247SJohannes Weiner ret = __handle_mm_fault(mm, vma, address, flags); 3475519e5247SJohannes Weiner 347649426420SJohannes Weiner if (flags & FAULT_FLAG_USER) { 347749426420SJohannes Weiner mem_cgroup_oom_disable(); 347849426420SJohannes Weiner /* 347949426420SJohannes Weiner * The task may have entered a memcg OOM situation but 348049426420SJohannes Weiner * if the allocation error was handled gracefully (no 348149426420SJohannes Weiner * VM_FAULT_OOM), there is no need to kill anything. 348249426420SJohannes Weiner * Just clean up the OOM state peacefully. 348349426420SJohannes Weiner */ 348449426420SJohannes Weiner if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM)) 348549426420SJohannes Weiner mem_cgroup_oom_synchronize(false); 348649426420SJohannes Weiner } 34873812c8c8SJohannes Weiner 3488519e5247SJohannes Weiner return ret; 3489519e5247SJohannes Weiner } 3490e1d6d01aSJesse Barnes EXPORT_SYMBOL_GPL(handle_mm_fault); 3491519e5247SJohannes Weiner 34921da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED 34931da177e4SLinus Torvalds /* 34941da177e4SLinus Torvalds * Allocate page upper directory. 3495872fec16SHugh Dickins * We've already handled the fast-path in-line. 34961da177e4SLinus Torvalds */ 34971bb3630eSHugh Dickins int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address) 34981da177e4SLinus Torvalds { 3499c74df32cSHugh Dickins pud_t *new = pud_alloc_one(mm, address); 3500c74df32cSHugh Dickins if (!new) 35011bb3630eSHugh Dickins return -ENOMEM; 35021da177e4SLinus Torvalds 3503362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 3504362a61adSNick Piggin 3505872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 35061bb3630eSHugh Dickins if (pgd_present(*pgd)) /* Another has populated it */ 35075e541973SBenjamin Herrenschmidt pud_free(mm, new); 35081bb3630eSHugh Dickins else 35091da177e4SLinus Torvalds pgd_populate(mm, pgd, new); 3510872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 35111bb3630eSHugh Dickins return 0; 35121da177e4SLinus Torvalds } 35131da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */ 35141da177e4SLinus Torvalds 35151da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED 35161da177e4SLinus Torvalds /* 35171da177e4SLinus Torvalds * Allocate page middle directory. 3518872fec16SHugh Dickins * We've already handled the fast-path in-line. 35191da177e4SLinus Torvalds */ 35201bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) 35211da177e4SLinus Torvalds { 3522c74df32cSHugh Dickins pmd_t *new = pmd_alloc_one(mm, address); 3523c74df32cSHugh Dickins if (!new) 35241bb3630eSHugh Dickins return -ENOMEM; 35251da177e4SLinus Torvalds 3526362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 3527362a61adSNick Piggin 3528872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 35291da177e4SLinus Torvalds #ifndef __ARCH_HAS_4LEVEL_HACK 3530dc6c9a35SKirill A. Shutemov if (!pud_present(*pud)) { 3531dc6c9a35SKirill A. Shutemov mm_inc_nr_pmds(mm); 35321da177e4SLinus Torvalds pud_populate(mm, pud, new); 3533dc6c9a35SKirill A. Shutemov } else /* Another has populated it */ 35345e541973SBenjamin Herrenschmidt pmd_free(mm, new); 3535dc6c9a35SKirill A. Shutemov #else 3536dc6c9a35SKirill A. Shutemov if (!pgd_present(*pud)) { 3537dc6c9a35SKirill A. Shutemov mm_inc_nr_pmds(mm); 35381da177e4SLinus Torvalds pgd_populate(mm, pud, new); 3539dc6c9a35SKirill A. Shutemov } else /* Another has populated it */ 3540dc6c9a35SKirill A. Shutemov pmd_free(mm, new); 35411da177e4SLinus Torvalds #endif /* __ARCH_HAS_4LEVEL_HACK */ 3542872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 35431bb3630eSHugh Dickins return 0; 35441da177e4SLinus Torvalds } 35451da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */ 35461da177e4SLinus Torvalds 35471b36ba81SNamhyung Kim static int __follow_pte(struct mm_struct *mm, unsigned long address, 3548f8ad0f49SJohannes Weiner pte_t **ptepp, spinlock_t **ptlp) 3549f8ad0f49SJohannes Weiner { 3550f8ad0f49SJohannes Weiner pgd_t *pgd; 3551f8ad0f49SJohannes Weiner pud_t *pud; 3552f8ad0f49SJohannes Weiner pmd_t *pmd; 3553f8ad0f49SJohannes Weiner pte_t *ptep; 3554f8ad0f49SJohannes Weiner 3555f8ad0f49SJohannes Weiner pgd = pgd_offset(mm, address); 3556f8ad0f49SJohannes Weiner if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) 3557f8ad0f49SJohannes Weiner goto out; 3558f8ad0f49SJohannes Weiner 3559f8ad0f49SJohannes Weiner pud = pud_offset(pgd, address); 3560f8ad0f49SJohannes Weiner if (pud_none(*pud) || unlikely(pud_bad(*pud))) 3561f8ad0f49SJohannes Weiner goto out; 3562f8ad0f49SJohannes Weiner 3563f8ad0f49SJohannes Weiner pmd = pmd_offset(pud, address); 3564f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 3565f8ad0f49SJohannes Weiner if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd))) 3566f8ad0f49SJohannes Weiner goto out; 3567f8ad0f49SJohannes Weiner 3568f8ad0f49SJohannes Weiner /* We cannot handle huge page PFN maps. Luckily they don't exist. */ 3569f8ad0f49SJohannes Weiner if (pmd_huge(*pmd)) 3570f8ad0f49SJohannes Weiner goto out; 3571f8ad0f49SJohannes Weiner 3572f8ad0f49SJohannes Weiner ptep = pte_offset_map_lock(mm, pmd, address, ptlp); 3573f8ad0f49SJohannes Weiner if (!ptep) 3574f8ad0f49SJohannes Weiner goto out; 3575f8ad0f49SJohannes Weiner if (!pte_present(*ptep)) 3576f8ad0f49SJohannes Weiner goto unlock; 3577f8ad0f49SJohannes Weiner *ptepp = ptep; 3578f8ad0f49SJohannes Weiner return 0; 3579f8ad0f49SJohannes Weiner unlock: 3580f8ad0f49SJohannes Weiner pte_unmap_unlock(ptep, *ptlp); 3581f8ad0f49SJohannes Weiner out: 3582f8ad0f49SJohannes Weiner return -EINVAL; 3583f8ad0f49SJohannes Weiner } 3584f8ad0f49SJohannes Weiner 35851b36ba81SNamhyung Kim static inline int follow_pte(struct mm_struct *mm, unsigned long address, 35861b36ba81SNamhyung Kim pte_t **ptepp, spinlock_t **ptlp) 35871b36ba81SNamhyung Kim { 35881b36ba81SNamhyung Kim int res; 35891b36ba81SNamhyung Kim 35901b36ba81SNamhyung Kim /* (void) is needed to make gcc happy */ 35911b36ba81SNamhyung Kim (void) __cond_lock(*ptlp, 35921b36ba81SNamhyung Kim !(res = __follow_pte(mm, address, ptepp, ptlp))); 35931b36ba81SNamhyung Kim return res; 35941b36ba81SNamhyung Kim } 35951b36ba81SNamhyung Kim 35963b6748e2SJohannes Weiner /** 35973b6748e2SJohannes Weiner * follow_pfn - look up PFN at a user virtual address 35983b6748e2SJohannes Weiner * @vma: memory mapping 35993b6748e2SJohannes Weiner * @address: user virtual address 36003b6748e2SJohannes Weiner * @pfn: location to store found PFN 36013b6748e2SJohannes Weiner * 36023b6748e2SJohannes Weiner * Only IO mappings and raw PFN mappings are allowed. 36033b6748e2SJohannes Weiner * 36043b6748e2SJohannes Weiner * Returns zero and the pfn at @pfn on success, -ve otherwise. 36053b6748e2SJohannes Weiner */ 36063b6748e2SJohannes Weiner int follow_pfn(struct vm_area_struct *vma, unsigned long address, 36073b6748e2SJohannes Weiner unsigned long *pfn) 36083b6748e2SJohannes Weiner { 36093b6748e2SJohannes Weiner int ret = -EINVAL; 36103b6748e2SJohannes Weiner spinlock_t *ptl; 36113b6748e2SJohannes Weiner pte_t *ptep; 36123b6748e2SJohannes Weiner 36133b6748e2SJohannes Weiner if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 36143b6748e2SJohannes Weiner return ret; 36153b6748e2SJohannes Weiner 36163b6748e2SJohannes Weiner ret = follow_pte(vma->vm_mm, address, &ptep, &ptl); 36173b6748e2SJohannes Weiner if (ret) 36183b6748e2SJohannes Weiner return ret; 36193b6748e2SJohannes Weiner *pfn = pte_pfn(*ptep); 36203b6748e2SJohannes Weiner pte_unmap_unlock(ptep, ptl); 36213b6748e2SJohannes Weiner return 0; 36223b6748e2SJohannes Weiner } 36233b6748e2SJohannes Weiner EXPORT_SYMBOL(follow_pfn); 36243b6748e2SJohannes Weiner 362528b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT 3626d87fe660Svenkatesh.pallipadi@intel.com int follow_phys(struct vm_area_struct *vma, 362728b2ee20SRik van Riel unsigned long address, unsigned int flags, 3628d87fe660Svenkatesh.pallipadi@intel.com unsigned long *prot, resource_size_t *phys) 362928b2ee20SRik van Riel { 363003668a4dSJohannes Weiner int ret = -EINVAL; 363128b2ee20SRik van Riel pte_t *ptep, pte; 363228b2ee20SRik van Riel spinlock_t *ptl; 363328b2ee20SRik van Riel 3634d87fe660Svenkatesh.pallipadi@intel.com if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 3635d87fe660Svenkatesh.pallipadi@intel.com goto out; 363628b2ee20SRik van Riel 363703668a4dSJohannes Weiner if (follow_pte(vma->vm_mm, address, &ptep, &ptl)) 3638d87fe660Svenkatesh.pallipadi@intel.com goto out; 363928b2ee20SRik van Riel pte = *ptep; 364003668a4dSJohannes Weiner 364128b2ee20SRik van Riel if ((flags & FOLL_WRITE) && !pte_write(pte)) 364228b2ee20SRik van Riel goto unlock; 364328b2ee20SRik van Riel 364428b2ee20SRik van Riel *prot = pgprot_val(pte_pgprot(pte)); 364503668a4dSJohannes Weiner *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT; 364628b2ee20SRik van Riel 364703668a4dSJohannes Weiner ret = 0; 364828b2ee20SRik van Riel unlock: 364928b2ee20SRik van Riel pte_unmap_unlock(ptep, ptl); 365028b2ee20SRik van Riel out: 3651d87fe660Svenkatesh.pallipadi@intel.com return ret; 365228b2ee20SRik van Riel } 365328b2ee20SRik van Riel 365428b2ee20SRik van Riel int generic_access_phys(struct vm_area_struct *vma, unsigned long addr, 365528b2ee20SRik van Riel void *buf, int len, int write) 365628b2ee20SRik van Riel { 365728b2ee20SRik van Riel resource_size_t phys_addr; 365828b2ee20SRik van Riel unsigned long prot = 0; 36592bc7273bSKOSAKI Motohiro void __iomem *maddr; 366028b2ee20SRik van Riel int offset = addr & (PAGE_SIZE-1); 366128b2ee20SRik van Riel 3662d87fe660Svenkatesh.pallipadi@intel.com if (follow_phys(vma, addr, write, &prot, &phys_addr)) 366328b2ee20SRik van Riel return -EINVAL; 366428b2ee20SRik van Riel 36659cb12d7bSGrazvydas Ignotas maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot); 366628b2ee20SRik van Riel if (write) 366728b2ee20SRik van Riel memcpy_toio(maddr + offset, buf, len); 366828b2ee20SRik van Riel else 366928b2ee20SRik van Riel memcpy_fromio(buf, maddr + offset, len); 367028b2ee20SRik van Riel iounmap(maddr); 367128b2ee20SRik van Riel 367228b2ee20SRik van Riel return len; 367328b2ee20SRik van Riel } 36745a73633eSUwe Kleine-König EXPORT_SYMBOL_GPL(generic_access_phys); 367528b2ee20SRik van Riel #endif 367628b2ee20SRik van Riel 36770ec76a11SDavid Howells /* 3678206cb636SStephen Wilson * Access another process' address space as given in mm. If non-NULL, use the 3679206cb636SStephen Wilson * given task for page fault accounting. 36800ec76a11SDavid Howells */ 3681206cb636SStephen Wilson static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm, 3682206cb636SStephen Wilson unsigned long addr, void *buf, int len, int write) 36830ec76a11SDavid Howells { 36840ec76a11SDavid Howells struct vm_area_struct *vma; 36850ec76a11SDavid Howells void *old_buf = buf; 36860ec76a11SDavid Howells 36870ec76a11SDavid Howells down_read(&mm->mmap_sem); 3688183ff22bSSimon Arlott /* ignore errors, just check how much was successfully transferred */ 36890ec76a11SDavid Howells while (len) { 36900ec76a11SDavid Howells int bytes, ret, offset; 36910ec76a11SDavid Howells void *maddr; 369228b2ee20SRik van Riel struct page *page = NULL; 36930ec76a11SDavid Howells 36940ec76a11SDavid Howells ret = get_user_pages(tsk, mm, addr, 1, 36950ec76a11SDavid Howells write, 1, &page, &vma); 369628b2ee20SRik van Riel if (ret <= 0) { 3697dbffcd03SRik van Riel #ifndef CONFIG_HAVE_IOREMAP_PROT 3698dbffcd03SRik van Riel break; 3699dbffcd03SRik van Riel #else 370028b2ee20SRik van Riel /* 370128b2ee20SRik van Riel * Check if this is a VM_IO | VM_PFNMAP VMA, which 370228b2ee20SRik van Riel * we can access using slightly different code. 370328b2ee20SRik van Riel */ 370428b2ee20SRik van Riel vma = find_vma(mm, addr); 3705fe936dfcSMichael Ellerman if (!vma || vma->vm_start > addr) 37060ec76a11SDavid Howells break; 370728b2ee20SRik van Riel if (vma->vm_ops && vma->vm_ops->access) 370828b2ee20SRik van Riel ret = vma->vm_ops->access(vma, addr, buf, 370928b2ee20SRik van Riel len, write); 371028b2ee20SRik van Riel if (ret <= 0) 371128b2ee20SRik van Riel break; 371228b2ee20SRik van Riel bytes = ret; 3713dbffcd03SRik van Riel #endif 371428b2ee20SRik van Riel } else { 37150ec76a11SDavid Howells bytes = len; 37160ec76a11SDavid Howells offset = addr & (PAGE_SIZE-1); 37170ec76a11SDavid Howells if (bytes > PAGE_SIZE-offset) 37180ec76a11SDavid Howells bytes = PAGE_SIZE-offset; 37190ec76a11SDavid Howells 37200ec76a11SDavid Howells maddr = kmap(page); 37210ec76a11SDavid Howells if (write) { 37220ec76a11SDavid Howells copy_to_user_page(vma, page, addr, 37230ec76a11SDavid Howells maddr + offset, buf, bytes); 37240ec76a11SDavid Howells set_page_dirty_lock(page); 37250ec76a11SDavid Howells } else { 37260ec76a11SDavid Howells copy_from_user_page(vma, page, addr, 37270ec76a11SDavid Howells buf, maddr + offset, bytes); 37280ec76a11SDavid Howells } 37290ec76a11SDavid Howells kunmap(page); 37300ec76a11SDavid Howells page_cache_release(page); 373128b2ee20SRik van Riel } 37320ec76a11SDavid Howells len -= bytes; 37330ec76a11SDavid Howells buf += bytes; 37340ec76a11SDavid Howells addr += bytes; 37350ec76a11SDavid Howells } 37360ec76a11SDavid Howells up_read(&mm->mmap_sem); 37370ec76a11SDavid Howells 37380ec76a11SDavid Howells return buf - old_buf; 37390ec76a11SDavid Howells } 374003252919SAndi Kleen 37415ddd36b9SStephen Wilson /** 3742ae91dbfcSRandy Dunlap * access_remote_vm - access another process' address space 37435ddd36b9SStephen Wilson * @mm: the mm_struct of the target address space 37445ddd36b9SStephen Wilson * @addr: start address to access 37455ddd36b9SStephen Wilson * @buf: source or destination buffer 37465ddd36b9SStephen Wilson * @len: number of bytes to transfer 37475ddd36b9SStephen Wilson * @write: whether the access is a write 37485ddd36b9SStephen Wilson * 37495ddd36b9SStephen Wilson * The caller must hold a reference on @mm. 37505ddd36b9SStephen Wilson */ 37515ddd36b9SStephen Wilson int access_remote_vm(struct mm_struct *mm, unsigned long addr, 37525ddd36b9SStephen Wilson void *buf, int len, int write) 37535ddd36b9SStephen Wilson { 37545ddd36b9SStephen Wilson return __access_remote_vm(NULL, mm, addr, buf, len, write); 37555ddd36b9SStephen Wilson } 37565ddd36b9SStephen Wilson 375703252919SAndi Kleen /* 3758206cb636SStephen Wilson * Access another process' address space. 3759206cb636SStephen Wilson * Source/target buffer must be kernel space, 3760206cb636SStephen Wilson * Do not walk the page table directly, use get_user_pages 3761206cb636SStephen Wilson */ 3762206cb636SStephen Wilson int access_process_vm(struct task_struct *tsk, unsigned long addr, 3763206cb636SStephen Wilson void *buf, int len, int write) 3764206cb636SStephen Wilson { 3765206cb636SStephen Wilson struct mm_struct *mm; 3766206cb636SStephen Wilson int ret; 3767206cb636SStephen Wilson 3768206cb636SStephen Wilson mm = get_task_mm(tsk); 3769206cb636SStephen Wilson if (!mm) 3770206cb636SStephen Wilson return 0; 3771206cb636SStephen Wilson 3772206cb636SStephen Wilson ret = __access_remote_vm(tsk, mm, addr, buf, len, write); 3773206cb636SStephen Wilson mmput(mm); 3774206cb636SStephen Wilson 3775206cb636SStephen Wilson return ret; 3776206cb636SStephen Wilson } 3777206cb636SStephen Wilson 377803252919SAndi Kleen /* 377903252919SAndi Kleen * Print the name of a VMA. 378003252919SAndi Kleen */ 378103252919SAndi Kleen void print_vma_addr(char *prefix, unsigned long ip) 378203252919SAndi Kleen { 378303252919SAndi Kleen struct mm_struct *mm = current->mm; 378403252919SAndi Kleen struct vm_area_struct *vma; 378503252919SAndi Kleen 3786e8bff74aSIngo Molnar /* 3787e8bff74aSIngo Molnar * Do not print if we are in atomic 3788e8bff74aSIngo Molnar * contexts (in exception stacks, etc.): 3789e8bff74aSIngo Molnar */ 3790e8bff74aSIngo Molnar if (preempt_count()) 3791e8bff74aSIngo Molnar return; 3792e8bff74aSIngo Molnar 379303252919SAndi Kleen down_read(&mm->mmap_sem); 379403252919SAndi Kleen vma = find_vma(mm, ip); 379503252919SAndi Kleen if (vma && vma->vm_file) { 379603252919SAndi Kleen struct file *f = vma->vm_file; 379703252919SAndi Kleen char *buf = (char *)__get_free_page(GFP_KERNEL); 379803252919SAndi Kleen if (buf) { 37992fbc57c5SAndy Shevchenko char *p; 380003252919SAndi Kleen 38019bf39ab2SMiklos Szeredi p = file_path(f, buf, PAGE_SIZE); 380203252919SAndi Kleen if (IS_ERR(p)) 380303252919SAndi Kleen p = "?"; 38042fbc57c5SAndy Shevchenko printk("%s%s[%lx+%lx]", prefix, kbasename(p), 380503252919SAndi Kleen vma->vm_start, 380603252919SAndi Kleen vma->vm_end - vma->vm_start); 380703252919SAndi Kleen free_page((unsigned long)buf); 380803252919SAndi Kleen } 380903252919SAndi Kleen } 381051a07e50SJeff Liu up_read(&mm->mmap_sem); 381103252919SAndi Kleen } 38123ee1afa3SNick Piggin 3813662bbcb2SMichael S. Tsirkin #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP) 38149ec23531SDavid Hildenbrand void __might_fault(const char *file, int line) 38153ee1afa3SNick Piggin { 381695156f00SPeter Zijlstra /* 381795156f00SPeter Zijlstra * Some code (nfs/sunrpc) uses socket ops on kernel memory while 381895156f00SPeter Zijlstra * holding the mmap_sem, this is safe because kernel memory doesn't 381995156f00SPeter Zijlstra * get paged out, therefore we'll never actually fault, and the 382095156f00SPeter Zijlstra * below annotations will generate false positives. 382195156f00SPeter Zijlstra */ 382295156f00SPeter Zijlstra if (segment_eq(get_fs(), KERNEL_DS)) 382395156f00SPeter Zijlstra return; 38249ec23531SDavid Hildenbrand if (pagefault_disabled()) 3825662bbcb2SMichael S. Tsirkin return; 38269ec23531SDavid Hildenbrand __might_sleep(file, line, 0); 38279ec23531SDavid Hildenbrand #if defined(CONFIG_DEBUG_ATOMIC_SLEEP) 3828662bbcb2SMichael S. Tsirkin if (current->mm) 38293ee1afa3SNick Piggin might_lock_read(¤t->mm->mmap_sem); 38309ec23531SDavid Hildenbrand #endif 38313ee1afa3SNick Piggin } 38329ec23531SDavid Hildenbrand EXPORT_SYMBOL(__might_fault); 38333ee1afa3SNick Piggin #endif 383447ad8475SAndrea Arcangeli 383547ad8475SAndrea Arcangeli #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS) 383647ad8475SAndrea Arcangeli static void clear_gigantic_page(struct page *page, 383747ad8475SAndrea Arcangeli unsigned long addr, 383847ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 383947ad8475SAndrea Arcangeli { 384047ad8475SAndrea Arcangeli int i; 384147ad8475SAndrea Arcangeli struct page *p = page; 384247ad8475SAndrea Arcangeli 384347ad8475SAndrea Arcangeli might_sleep(); 384447ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; 384547ad8475SAndrea Arcangeli i++, p = mem_map_next(p, page, i)) { 384647ad8475SAndrea Arcangeli cond_resched(); 384747ad8475SAndrea Arcangeli clear_user_highpage(p, addr + i * PAGE_SIZE); 384847ad8475SAndrea Arcangeli } 384947ad8475SAndrea Arcangeli } 385047ad8475SAndrea Arcangeli void clear_huge_page(struct page *page, 385147ad8475SAndrea Arcangeli unsigned long addr, unsigned int pages_per_huge_page) 385247ad8475SAndrea Arcangeli { 385347ad8475SAndrea Arcangeli int i; 385447ad8475SAndrea Arcangeli 385547ad8475SAndrea Arcangeli if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { 385647ad8475SAndrea Arcangeli clear_gigantic_page(page, addr, pages_per_huge_page); 385747ad8475SAndrea Arcangeli return; 385847ad8475SAndrea Arcangeli } 385947ad8475SAndrea Arcangeli 386047ad8475SAndrea Arcangeli might_sleep(); 386147ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; i++) { 386247ad8475SAndrea Arcangeli cond_resched(); 386347ad8475SAndrea Arcangeli clear_user_highpage(page + i, addr + i * PAGE_SIZE); 386447ad8475SAndrea Arcangeli } 386547ad8475SAndrea Arcangeli } 386647ad8475SAndrea Arcangeli 386747ad8475SAndrea Arcangeli static void copy_user_gigantic_page(struct page *dst, struct page *src, 386847ad8475SAndrea Arcangeli unsigned long addr, 386947ad8475SAndrea Arcangeli struct vm_area_struct *vma, 387047ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 387147ad8475SAndrea Arcangeli { 387247ad8475SAndrea Arcangeli int i; 387347ad8475SAndrea Arcangeli struct page *dst_base = dst; 387447ad8475SAndrea Arcangeli struct page *src_base = src; 387547ad8475SAndrea Arcangeli 387647ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; ) { 387747ad8475SAndrea Arcangeli cond_resched(); 387847ad8475SAndrea Arcangeli copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma); 387947ad8475SAndrea Arcangeli 388047ad8475SAndrea Arcangeli i++; 388147ad8475SAndrea Arcangeli dst = mem_map_next(dst, dst_base, i); 388247ad8475SAndrea Arcangeli src = mem_map_next(src, src_base, i); 388347ad8475SAndrea Arcangeli } 388447ad8475SAndrea Arcangeli } 388547ad8475SAndrea Arcangeli 388647ad8475SAndrea Arcangeli void copy_user_huge_page(struct page *dst, struct page *src, 388747ad8475SAndrea Arcangeli unsigned long addr, struct vm_area_struct *vma, 388847ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 388947ad8475SAndrea Arcangeli { 389047ad8475SAndrea Arcangeli int i; 389147ad8475SAndrea Arcangeli 389247ad8475SAndrea Arcangeli if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { 389347ad8475SAndrea Arcangeli copy_user_gigantic_page(dst, src, addr, vma, 389447ad8475SAndrea Arcangeli pages_per_huge_page); 389547ad8475SAndrea Arcangeli return; 389647ad8475SAndrea Arcangeli } 389747ad8475SAndrea Arcangeli 389847ad8475SAndrea Arcangeli might_sleep(); 389947ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; i++) { 390047ad8475SAndrea Arcangeli cond_resched(); 390147ad8475SAndrea Arcangeli copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma); 390247ad8475SAndrea Arcangeli } 390347ad8475SAndrea Arcangeli } 390447ad8475SAndrea Arcangeli #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */ 390549076ec2SKirill A. Shutemov 390640b64acdSOlof Johansson #if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS 3907b35f1819SKirill A. Shutemov 3908b35f1819SKirill A. Shutemov static struct kmem_cache *page_ptl_cachep; 3909b35f1819SKirill A. Shutemov 3910b35f1819SKirill A. Shutemov void __init ptlock_cache_init(void) 3911b35f1819SKirill A. Shutemov { 3912b35f1819SKirill A. Shutemov page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0, 3913b35f1819SKirill A. Shutemov SLAB_PANIC, NULL); 3914b35f1819SKirill A. Shutemov } 3915b35f1819SKirill A. Shutemov 3916539edb58SPeter Zijlstra bool ptlock_alloc(struct page *page) 391749076ec2SKirill A. Shutemov { 391849076ec2SKirill A. Shutemov spinlock_t *ptl; 391949076ec2SKirill A. Shutemov 3920b35f1819SKirill A. Shutemov ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL); 392149076ec2SKirill A. Shutemov if (!ptl) 392249076ec2SKirill A. Shutemov return false; 3923539edb58SPeter Zijlstra page->ptl = ptl; 392449076ec2SKirill A. Shutemov return true; 392549076ec2SKirill A. Shutemov } 392649076ec2SKirill A. Shutemov 3927539edb58SPeter Zijlstra void ptlock_free(struct page *page) 392849076ec2SKirill A. Shutemov { 3929b35f1819SKirill A. Shutemov kmem_cache_free(page_ptl_cachep, page->ptl); 393049076ec2SKirill A. Shutemov } 393149076ec2SKirill A. Shutemov #endif 3932