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> 436e84f315SIngo Molnar #include <linux/sched/mm.h> 44f7ccbae4SIngo Molnar #include <linux/sched/coredump.h> 456a3827d7SIngo Molnar #include <linux/sched/numa_balancing.h> 4629930025SIngo Molnar #include <linux/sched/task.h> 471da177e4SLinus Torvalds #include <linux/hugetlb.h> 481da177e4SLinus Torvalds #include <linux/mman.h> 491da177e4SLinus Torvalds #include <linux/swap.h> 501da177e4SLinus Torvalds #include <linux/highmem.h> 511da177e4SLinus Torvalds #include <linux/pagemap.h> 525042db43SJérôme Glisse #include <linux/memremap.h> 539a840895SHugh Dickins #include <linux/ksm.h> 541da177e4SLinus Torvalds #include <linux/rmap.h> 55b95f1b31SPaul Gortmaker #include <linux/export.h> 560ff92245SShailabh Nagar #include <linux/delayacct.h> 571da177e4SLinus Torvalds #include <linux/init.h> 5801c8f1c4SDan Williams #include <linux/pfn_t.h> 59edc79b2aSPeter Zijlstra #include <linux/writeback.h> 608a9f3ccdSBalbir Singh #include <linux/memcontrol.h> 61cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h> 623dc14741SHugh Dickins #include <linux/swapops.h> 633dc14741SHugh Dickins #include <linux/elf.h> 645a0e3ad6STejun Heo #include <linux/gfp.h> 654daae3b4SMel Gorman #include <linux/migrate.h> 662fbc57c5SAndy Shevchenko #include <linux/string.h> 670abdd7a8SDan Williams #include <linux/dma-debug.h> 681592eef0SKirill A. Shutemov #include <linux/debugfs.h> 696b251fc9SAndrea Arcangeli #include <linux/userfaultfd_k.h> 70bc2466e4SJan Kara #include <linux/dax.h> 716b31d595SMichal Hocko #include <linux/oom.h> 721da177e4SLinus Torvalds 736952b61dSAlexey Dobriyan #include <asm/io.h> 7433a709b2SDave Hansen #include <asm/mmu_context.h> 751da177e4SLinus Torvalds #include <asm/pgalloc.h> 767c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 771da177e4SLinus Torvalds #include <asm/tlb.h> 781da177e4SLinus Torvalds #include <asm/tlbflush.h> 791da177e4SLinus Torvalds #include <asm/pgtable.h> 801da177e4SLinus Torvalds 8142b77728SJan Beulich #include "internal.h" 8242b77728SJan Beulich 83af27d940SArnd Bergmann #if defined(LAST_CPUPID_NOT_IN_PAGE_FLAGS) && !defined(CONFIG_COMPILE_TEST) 8490572890SPeter Zijlstra #warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid. 8575980e97SPeter Zijlstra #endif 8675980e97SPeter Zijlstra 87d41dee36SAndy Whitcroft #ifndef CONFIG_NEED_MULTIPLE_NODES 881da177e4SLinus Torvalds /* use the per-pgdat data instead for discontigmem - mbligh */ 891da177e4SLinus Torvalds unsigned long max_mapnr; 901da177e4SLinus Torvalds EXPORT_SYMBOL(max_mapnr); 91166f61b9STobin C Harding 92166f61b9STobin C Harding struct page *mem_map; 931da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map); 941da177e4SLinus Torvalds #endif 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds /* 971da177e4SLinus Torvalds * A number of key systems in x86 including ioremap() rely on the assumption 981da177e4SLinus Torvalds * that high_memory defines the upper bound on direct map memory, then end 991da177e4SLinus Torvalds * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and 1001da177e4SLinus Torvalds * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL 1011da177e4SLinus Torvalds * and ZONE_HIGHMEM. 1021da177e4SLinus Torvalds */ 1031da177e4SLinus Torvalds void *high_memory; 1041da177e4SLinus Torvalds EXPORT_SYMBOL(high_memory); 1051da177e4SLinus Torvalds 10632a93233SIngo Molnar /* 10732a93233SIngo Molnar * Randomize the address space (stacks, mmaps, brk, etc.). 10832a93233SIngo Molnar * 10932a93233SIngo Molnar * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization, 11032a93233SIngo Molnar * as ancient (libc5 based) binaries can segfault. ) 11132a93233SIngo Molnar */ 11232a93233SIngo Molnar int randomize_va_space __read_mostly = 11332a93233SIngo Molnar #ifdef CONFIG_COMPAT_BRK 11432a93233SIngo Molnar 1; 11532a93233SIngo Molnar #else 11632a93233SIngo Molnar 2; 11732a93233SIngo Molnar #endif 118a62eaf15SAndi Kleen 119a62eaf15SAndi Kleen static int __init disable_randmaps(char *s) 120a62eaf15SAndi Kleen { 121a62eaf15SAndi Kleen randomize_va_space = 0; 1229b41046cSOGAWA Hirofumi return 1; 123a62eaf15SAndi Kleen } 124a62eaf15SAndi Kleen __setup("norandmaps", disable_randmaps); 125a62eaf15SAndi Kleen 12662eede62SHugh Dickins unsigned long zero_pfn __read_mostly; 1270b70068eSArd Biesheuvel EXPORT_SYMBOL(zero_pfn); 1280b70068eSArd Biesheuvel 129166f61b9STobin C Harding unsigned long highest_memmap_pfn __read_mostly; 130166f61b9STobin C Harding 131a13ea5b7SHugh Dickins /* 132a13ea5b7SHugh Dickins * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init() 133a13ea5b7SHugh Dickins */ 134a13ea5b7SHugh Dickins static int __init init_zero_pfn(void) 135a13ea5b7SHugh Dickins { 136a13ea5b7SHugh Dickins zero_pfn = page_to_pfn(ZERO_PAGE(0)); 137a13ea5b7SHugh Dickins return 0; 138a13ea5b7SHugh Dickins } 139a13ea5b7SHugh Dickins core_initcall(init_zero_pfn); 140a62eaf15SAndi Kleen 141d559db08SKAMEZAWA Hiroyuki 14234e55232SKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING) 14334e55232SKAMEZAWA Hiroyuki 144ea48cf78SDavid Rientjes void sync_mm_rss(struct mm_struct *mm) 14534e55232SKAMEZAWA Hiroyuki { 14634e55232SKAMEZAWA Hiroyuki int i; 14734e55232SKAMEZAWA Hiroyuki 14834e55232SKAMEZAWA Hiroyuki for (i = 0; i < NR_MM_COUNTERS; i++) { 14905af2e10SDavid Rientjes if (current->rss_stat.count[i]) { 15005af2e10SDavid Rientjes add_mm_counter(mm, i, current->rss_stat.count[i]); 15105af2e10SDavid Rientjes current->rss_stat.count[i] = 0; 15234e55232SKAMEZAWA Hiroyuki } 15334e55232SKAMEZAWA Hiroyuki } 15405af2e10SDavid Rientjes current->rss_stat.events = 0; 15534e55232SKAMEZAWA Hiroyuki } 15634e55232SKAMEZAWA Hiroyuki 15734e55232SKAMEZAWA Hiroyuki static void add_mm_counter_fast(struct mm_struct *mm, int member, int val) 15834e55232SKAMEZAWA Hiroyuki { 15934e55232SKAMEZAWA Hiroyuki struct task_struct *task = current; 16034e55232SKAMEZAWA Hiroyuki 16134e55232SKAMEZAWA Hiroyuki if (likely(task->mm == mm)) 16234e55232SKAMEZAWA Hiroyuki task->rss_stat.count[member] += val; 16334e55232SKAMEZAWA Hiroyuki else 16434e55232SKAMEZAWA Hiroyuki add_mm_counter(mm, member, val); 16534e55232SKAMEZAWA Hiroyuki } 16634e55232SKAMEZAWA Hiroyuki #define inc_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, 1) 16734e55232SKAMEZAWA Hiroyuki #define dec_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, -1) 16834e55232SKAMEZAWA Hiroyuki 16934e55232SKAMEZAWA Hiroyuki /* sync counter once per 64 page faults */ 17034e55232SKAMEZAWA Hiroyuki #define TASK_RSS_EVENTS_THRESH (64) 17134e55232SKAMEZAWA Hiroyuki static void check_sync_rss_stat(struct task_struct *task) 17234e55232SKAMEZAWA Hiroyuki { 17334e55232SKAMEZAWA Hiroyuki if (unlikely(task != current)) 17434e55232SKAMEZAWA Hiroyuki return; 17534e55232SKAMEZAWA Hiroyuki if (unlikely(task->rss_stat.events++ > TASK_RSS_EVENTS_THRESH)) 176ea48cf78SDavid Rientjes sync_mm_rss(task->mm); 17734e55232SKAMEZAWA Hiroyuki } 1789547d01bSPeter Zijlstra #else /* SPLIT_RSS_COUNTING */ 17934e55232SKAMEZAWA Hiroyuki 18034e55232SKAMEZAWA Hiroyuki #define inc_mm_counter_fast(mm, member) inc_mm_counter(mm, member) 18134e55232SKAMEZAWA Hiroyuki #define dec_mm_counter_fast(mm, member) dec_mm_counter(mm, member) 18234e55232SKAMEZAWA Hiroyuki 18334e55232SKAMEZAWA Hiroyuki static void check_sync_rss_stat(struct task_struct *task) 18434e55232SKAMEZAWA Hiroyuki { 18534e55232SKAMEZAWA Hiroyuki } 18634e55232SKAMEZAWA Hiroyuki 1879547d01bSPeter Zijlstra #endif /* SPLIT_RSS_COUNTING */ 1889547d01bSPeter Zijlstra 1891da177e4SLinus Torvalds /* 1901da177e4SLinus Torvalds * Note: this doesn't free the actual pages themselves. That 1911da177e4SLinus Torvalds * has been handled earlier when unmapping all the memory regions. 1921da177e4SLinus Torvalds */ 1939e1b32caSBenjamin Herrenschmidt static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd, 1949e1b32caSBenjamin Herrenschmidt unsigned long addr) 1951da177e4SLinus Torvalds { 1962f569afdSMartin Schwidefsky pgtable_t token = pmd_pgtable(*pmd); 1971da177e4SLinus Torvalds pmd_clear(pmd); 1989e1b32caSBenjamin Herrenschmidt pte_free_tlb(tlb, token, addr); 199c4812909SKirill A. Shutemov mm_dec_nr_ptes(tlb->mm); 2001da177e4SLinus Torvalds } 2011da177e4SLinus Torvalds 202e0da382cSHugh Dickins static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud, 203e0da382cSHugh Dickins unsigned long addr, unsigned long end, 204e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 2051da177e4SLinus Torvalds { 2061da177e4SLinus Torvalds pmd_t *pmd; 2071da177e4SLinus Torvalds unsigned long next; 208e0da382cSHugh Dickins unsigned long start; 2091da177e4SLinus Torvalds 210e0da382cSHugh Dickins start = addr; 2111da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 2121da177e4SLinus Torvalds do { 2131da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 2141da177e4SLinus Torvalds if (pmd_none_or_clear_bad(pmd)) 2151da177e4SLinus Torvalds continue; 2169e1b32caSBenjamin Herrenschmidt free_pte_range(tlb, pmd, addr); 2171da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 2181da177e4SLinus Torvalds 219e0da382cSHugh Dickins start &= PUD_MASK; 220e0da382cSHugh Dickins if (start < floor) 221e0da382cSHugh Dickins return; 222e0da382cSHugh Dickins if (ceiling) { 223e0da382cSHugh Dickins ceiling &= PUD_MASK; 224e0da382cSHugh Dickins if (!ceiling) 225e0da382cSHugh Dickins return; 2261da177e4SLinus Torvalds } 227e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 228e0da382cSHugh Dickins return; 229e0da382cSHugh Dickins 230e0da382cSHugh Dickins pmd = pmd_offset(pud, start); 231e0da382cSHugh Dickins pud_clear(pud); 2329e1b32caSBenjamin Herrenschmidt pmd_free_tlb(tlb, pmd, start); 233dc6c9a35SKirill A. Shutemov mm_dec_nr_pmds(tlb->mm); 2341da177e4SLinus Torvalds } 2351da177e4SLinus Torvalds 236c2febafcSKirill A. Shutemov static inline void free_pud_range(struct mmu_gather *tlb, p4d_t *p4d, 237e0da382cSHugh Dickins unsigned long addr, unsigned long end, 238e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 2391da177e4SLinus Torvalds { 2401da177e4SLinus Torvalds pud_t *pud; 2411da177e4SLinus Torvalds unsigned long next; 242e0da382cSHugh Dickins unsigned long start; 2431da177e4SLinus Torvalds 244e0da382cSHugh Dickins start = addr; 245c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 2461da177e4SLinus Torvalds do { 2471da177e4SLinus Torvalds next = pud_addr_end(addr, end); 2481da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 2491da177e4SLinus Torvalds continue; 250e0da382cSHugh Dickins free_pmd_range(tlb, pud, addr, next, floor, ceiling); 2511da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 2521da177e4SLinus Torvalds 253c2febafcSKirill A. Shutemov start &= P4D_MASK; 254c2febafcSKirill A. Shutemov if (start < floor) 255c2febafcSKirill A. Shutemov return; 256c2febafcSKirill A. Shutemov if (ceiling) { 257c2febafcSKirill A. Shutemov ceiling &= P4D_MASK; 258c2febafcSKirill A. Shutemov if (!ceiling) 259c2febafcSKirill A. Shutemov return; 260c2febafcSKirill A. Shutemov } 261c2febafcSKirill A. Shutemov if (end - 1 > ceiling - 1) 262c2febafcSKirill A. Shutemov return; 263c2febafcSKirill A. Shutemov 264c2febafcSKirill A. Shutemov pud = pud_offset(p4d, start); 265c2febafcSKirill A. Shutemov p4d_clear(p4d); 266c2febafcSKirill A. Shutemov pud_free_tlb(tlb, pud, start); 267b4e98d9aSKirill A. Shutemov mm_dec_nr_puds(tlb->mm); 268c2febafcSKirill A. Shutemov } 269c2febafcSKirill A. Shutemov 270c2febafcSKirill A. Shutemov static inline void free_p4d_range(struct mmu_gather *tlb, pgd_t *pgd, 271c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 272c2febafcSKirill A. Shutemov unsigned long floor, unsigned long ceiling) 273c2febafcSKirill A. Shutemov { 274c2febafcSKirill A. Shutemov p4d_t *p4d; 275c2febafcSKirill A. Shutemov unsigned long next; 276c2febafcSKirill A. Shutemov unsigned long start; 277c2febafcSKirill A. Shutemov 278c2febafcSKirill A. Shutemov start = addr; 279c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 280c2febafcSKirill A. Shutemov do { 281c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 282c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(p4d)) 283c2febafcSKirill A. Shutemov continue; 284c2febafcSKirill A. Shutemov free_pud_range(tlb, p4d, addr, next, floor, ceiling); 285c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 286c2febafcSKirill A. Shutemov 287e0da382cSHugh Dickins start &= PGDIR_MASK; 288e0da382cSHugh Dickins if (start < floor) 289e0da382cSHugh Dickins return; 290e0da382cSHugh Dickins if (ceiling) { 291e0da382cSHugh Dickins ceiling &= PGDIR_MASK; 292e0da382cSHugh Dickins if (!ceiling) 293e0da382cSHugh Dickins return; 2941da177e4SLinus Torvalds } 295e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 296e0da382cSHugh Dickins return; 297e0da382cSHugh Dickins 298c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, start); 299e0da382cSHugh Dickins pgd_clear(pgd); 300c2febafcSKirill A. Shutemov p4d_free_tlb(tlb, p4d, start); 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds /* 304e0da382cSHugh Dickins * This function frees user-level page tables of a process. 3051da177e4SLinus Torvalds */ 30642b77728SJan Beulich void free_pgd_range(struct mmu_gather *tlb, 307e0da382cSHugh Dickins unsigned long addr, unsigned long end, 308e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 3091da177e4SLinus Torvalds { 3101da177e4SLinus Torvalds pgd_t *pgd; 3111da177e4SLinus Torvalds unsigned long next; 3121da177e4SLinus Torvalds 313e0da382cSHugh Dickins /* 314e0da382cSHugh Dickins * The next few lines have given us lots of grief... 315e0da382cSHugh Dickins * 316e0da382cSHugh Dickins * Why are we testing PMD* at this top level? Because often 317e0da382cSHugh Dickins * there will be no work to do at all, and we'd prefer not to 318e0da382cSHugh Dickins * go all the way down to the bottom just to discover that. 319e0da382cSHugh Dickins * 320e0da382cSHugh Dickins * Why all these "- 1"s? Because 0 represents both the bottom 321e0da382cSHugh Dickins * of the address space and the top of it (using -1 for the 322e0da382cSHugh Dickins * top wouldn't help much: the masks would do the wrong thing). 323e0da382cSHugh Dickins * The rule is that addr 0 and floor 0 refer to the bottom of 324e0da382cSHugh Dickins * the address space, but end 0 and ceiling 0 refer to the top 325e0da382cSHugh Dickins * Comparisons need to use "end - 1" and "ceiling - 1" (though 326e0da382cSHugh Dickins * that end 0 case should be mythical). 327e0da382cSHugh Dickins * 328e0da382cSHugh Dickins * Wherever addr is brought up or ceiling brought down, we must 329e0da382cSHugh Dickins * be careful to reject "the opposite 0" before it confuses the 330e0da382cSHugh Dickins * subsequent tests. But what about where end is brought down 331e0da382cSHugh Dickins * by PMD_SIZE below? no, end can't go down to 0 there. 332e0da382cSHugh Dickins * 333e0da382cSHugh Dickins * Whereas we round start (addr) and ceiling down, by different 334e0da382cSHugh Dickins * masks at different levels, in order to test whether a table 335e0da382cSHugh Dickins * now has no other vmas using it, so can be freed, we don't 336e0da382cSHugh Dickins * bother to round floor or end up - the tests don't need that. 337e0da382cSHugh Dickins */ 338e0da382cSHugh Dickins 339e0da382cSHugh Dickins addr &= PMD_MASK; 340e0da382cSHugh Dickins if (addr < floor) { 341e0da382cSHugh Dickins addr += PMD_SIZE; 342e0da382cSHugh Dickins if (!addr) 343e0da382cSHugh Dickins return; 344e0da382cSHugh Dickins } 345e0da382cSHugh Dickins if (ceiling) { 346e0da382cSHugh Dickins ceiling &= PMD_MASK; 347e0da382cSHugh Dickins if (!ceiling) 348e0da382cSHugh Dickins return; 349e0da382cSHugh Dickins } 350e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 351e0da382cSHugh Dickins end -= PMD_SIZE; 352e0da382cSHugh Dickins if (addr > end - 1) 353e0da382cSHugh Dickins return; 35407e32661SAneesh Kumar K.V /* 35507e32661SAneesh Kumar K.V * We add page table cache pages with PAGE_SIZE, 35607e32661SAneesh Kumar K.V * (see pte_free_tlb()), flush the tlb if we need 35707e32661SAneesh Kumar K.V */ 35807e32661SAneesh Kumar K.V tlb_remove_check_page_size_change(tlb, PAGE_SIZE); 35942b77728SJan Beulich pgd = pgd_offset(tlb->mm, addr); 3601da177e4SLinus Torvalds do { 3611da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 3621da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 3631da177e4SLinus Torvalds continue; 364c2febafcSKirill A. Shutemov free_p4d_range(tlb, pgd, addr, next, floor, ceiling); 3651da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 366e0da382cSHugh Dickins } 367e0da382cSHugh Dickins 36842b77728SJan Beulich void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma, 369e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 370e0da382cSHugh Dickins { 371e0da382cSHugh Dickins while (vma) { 372e0da382cSHugh Dickins struct vm_area_struct *next = vma->vm_next; 373e0da382cSHugh Dickins unsigned long addr = vma->vm_start; 374e0da382cSHugh Dickins 3758f4f8c16SHugh Dickins /* 37625d9e2d1Snpiggin@suse.de * Hide vma from rmap and truncate_pagecache before freeing 37725d9e2d1Snpiggin@suse.de * pgtables 3788f4f8c16SHugh Dickins */ 3795beb4930SRik van Riel unlink_anon_vmas(vma); 3808f4f8c16SHugh Dickins unlink_file_vma(vma); 3818f4f8c16SHugh Dickins 3829da61aefSDavid Gibson if (is_vm_hugetlb_page(vma)) { 3833bf5ee95SHugh Dickins hugetlb_free_pgd_range(tlb, addr, vma->vm_end, 3843bf5ee95SHugh Dickins floor, next ? next->vm_start : ceiling); 3853bf5ee95SHugh Dickins } else { 3863bf5ee95SHugh Dickins /* 3873bf5ee95SHugh Dickins * Optimization: gather nearby vmas into one call down 3883bf5ee95SHugh Dickins */ 3893bf5ee95SHugh Dickins while (next && next->vm_start <= vma->vm_end + PMD_SIZE 3904866920bSDavid Gibson && !is_vm_hugetlb_page(next)) { 391e0da382cSHugh Dickins vma = next; 392e0da382cSHugh Dickins next = vma->vm_next; 3935beb4930SRik van Riel unlink_anon_vmas(vma); 3948f4f8c16SHugh Dickins unlink_file_vma(vma); 395e0da382cSHugh Dickins } 3963bf5ee95SHugh Dickins free_pgd_range(tlb, addr, vma->vm_end, 397e0da382cSHugh Dickins floor, next ? next->vm_start : ceiling); 3983bf5ee95SHugh Dickins } 399e0da382cSHugh Dickins vma = next; 400e0da382cSHugh Dickins } 4011da177e4SLinus Torvalds } 4021da177e4SLinus Torvalds 4033ed3a4f0SKirill A. Shutemov int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address) 4041da177e4SLinus Torvalds { 405c4088ebdSKirill A. Shutemov spinlock_t *ptl; 4062f569afdSMartin Schwidefsky pgtable_t new = pte_alloc_one(mm, address); 4071da177e4SLinus Torvalds if (!new) 4081bb3630eSHugh Dickins return -ENOMEM; 4091bb3630eSHugh Dickins 410362a61adSNick Piggin /* 411362a61adSNick Piggin * Ensure all pte setup (eg. pte page lock and page clearing) are 412362a61adSNick Piggin * visible before the pte is made visible to other CPUs by being 413362a61adSNick Piggin * put into page tables. 414362a61adSNick Piggin * 415362a61adSNick Piggin * The other side of the story is the pointer chasing in the page 416362a61adSNick Piggin * table walking code (when walking the page table without locking; 417362a61adSNick Piggin * ie. most of the time). Fortunately, these data accesses consist 418362a61adSNick Piggin * of a chain of data-dependent loads, meaning most CPUs (alpha 419362a61adSNick Piggin * being the notable exception) will already guarantee loads are 420362a61adSNick Piggin * seen in-order. See the alpha page table accessors for the 421362a61adSNick Piggin * smp_read_barrier_depends() barriers in page table walking code. 422362a61adSNick Piggin */ 423362a61adSNick Piggin smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */ 424362a61adSNick Piggin 425c4088ebdSKirill A. Shutemov ptl = pmd_lock(mm, pmd); 4268ac1f832SAndrea Arcangeli if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 427c4812909SKirill A. Shutemov mm_inc_nr_ptes(mm); 4281da177e4SLinus Torvalds pmd_populate(mm, pmd, new); 4292f569afdSMartin Schwidefsky new = NULL; 4304b471e88SKirill A. Shutemov } 431c4088ebdSKirill A. Shutemov spin_unlock(ptl); 4322f569afdSMartin Schwidefsky if (new) 4332f569afdSMartin Schwidefsky pte_free(mm, new); 4341bb3630eSHugh Dickins return 0; 4351da177e4SLinus Torvalds } 4361da177e4SLinus Torvalds 4371bb3630eSHugh Dickins int __pte_alloc_kernel(pmd_t *pmd, unsigned long address) 4381da177e4SLinus Torvalds { 4391bb3630eSHugh Dickins pte_t *new = pte_alloc_one_kernel(&init_mm, address); 4401da177e4SLinus Torvalds if (!new) 4411bb3630eSHugh Dickins return -ENOMEM; 4421da177e4SLinus Torvalds 443362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 444362a61adSNick Piggin 445872fec16SHugh Dickins spin_lock(&init_mm.page_table_lock); 4468ac1f832SAndrea Arcangeli if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 447872fec16SHugh Dickins pmd_populate_kernel(&init_mm, pmd, new); 4482f569afdSMartin Schwidefsky new = NULL; 4494b471e88SKirill A. Shutemov } 450872fec16SHugh Dickins spin_unlock(&init_mm.page_table_lock); 4512f569afdSMartin Schwidefsky if (new) 4522f569afdSMartin Schwidefsky pte_free_kernel(&init_mm, new); 4531bb3630eSHugh Dickins return 0; 4541da177e4SLinus Torvalds } 4551da177e4SLinus Torvalds 456d559db08SKAMEZAWA Hiroyuki static inline void init_rss_vec(int *rss) 457ae859762SHugh Dickins { 458d559db08SKAMEZAWA Hiroyuki memset(rss, 0, sizeof(int) * NR_MM_COUNTERS); 459d559db08SKAMEZAWA Hiroyuki } 460d559db08SKAMEZAWA Hiroyuki 461d559db08SKAMEZAWA Hiroyuki static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss) 462d559db08SKAMEZAWA Hiroyuki { 463d559db08SKAMEZAWA Hiroyuki int i; 464d559db08SKAMEZAWA Hiroyuki 46534e55232SKAMEZAWA Hiroyuki if (current->mm == mm) 46605af2e10SDavid Rientjes sync_mm_rss(mm); 467d559db08SKAMEZAWA Hiroyuki for (i = 0; i < NR_MM_COUNTERS; i++) 468d559db08SKAMEZAWA Hiroyuki if (rss[i]) 469d559db08SKAMEZAWA Hiroyuki add_mm_counter(mm, i, rss[i]); 470ae859762SHugh Dickins } 471ae859762SHugh Dickins 4721da177e4SLinus Torvalds /* 4736aab341eSLinus Torvalds * This function is called to print an error when a bad pte 4746aab341eSLinus Torvalds * is found. For example, we might have a PFN-mapped pte in 4756aab341eSLinus Torvalds * a region that doesn't allow it. 476b5810039SNick Piggin * 477b5810039SNick Piggin * The calling function must still handle the error. 478b5810039SNick Piggin */ 4793dc14741SHugh Dickins static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr, 4803dc14741SHugh Dickins pte_t pte, struct page *page) 481b5810039SNick Piggin { 4823dc14741SHugh Dickins pgd_t *pgd = pgd_offset(vma->vm_mm, addr); 483c2febafcSKirill A. Shutemov p4d_t *p4d = p4d_offset(pgd, addr); 484c2febafcSKirill A. Shutemov pud_t *pud = pud_offset(p4d, addr); 4853dc14741SHugh Dickins pmd_t *pmd = pmd_offset(pud, addr); 4863dc14741SHugh Dickins struct address_space *mapping; 4873dc14741SHugh Dickins pgoff_t index; 488d936cf9bSHugh Dickins static unsigned long resume; 489d936cf9bSHugh Dickins static unsigned long nr_shown; 490d936cf9bSHugh Dickins static unsigned long nr_unshown; 491d936cf9bSHugh Dickins 492d936cf9bSHugh Dickins /* 493d936cf9bSHugh Dickins * Allow a burst of 60 reports, then keep quiet for that minute; 494d936cf9bSHugh Dickins * or allow a steady drip of one report per second. 495d936cf9bSHugh Dickins */ 496d936cf9bSHugh Dickins if (nr_shown == 60) { 497d936cf9bSHugh Dickins if (time_before(jiffies, resume)) { 498d936cf9bSHugh Dickins nr_unshown++; 499d936cf9bSHugh Dickins return; 500d936cf9bSHugh Dickins } 501d936cf9bSHugh Dickins if (nr_unshown) { 5021170532bSJoe Perches pr_alert("BUG: Bad page map: %lu messages suppressed\n", 503d936cf9bSHugh Dickins nr_unshown); 504d936cf9bSHugh Dickins nr_unshown = 0; 505d936cf9bSHugh Dickins } 506d936cf9bSHugh Dickins nr_shown = 0; 507d936cf9bSHugh Dickins } 508d936cf9bSHugh Dickins if (nr_shown++ == 0) 509d936cf9bSHugh Dickins resume = jiffies + 60 * HZ; 5103dc14741SHugh Dickins 5113dc14741SHugh Dickins mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL; 5123dc14741SHugh Dickins index = linear_page_index(vma, addr); 5133dc14741SHugh Dickins 5141170532bSJoe Perches pr_alert("BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n", 5153dc14741SHugh Dickins current->comm, 5163dc14741SHugh Dickins (long long)pte_val(pte), (long long)pmd_val(*pmd)); 517718a3821SWu Fengguang if (page) 518f0b791a3SDave Hansen dump_page(page, "bad pte"); 5191170532bSJoe Perches pr_alert("addr:%p vm_flags:%08lx anon_vma:%p mapping:%p index:%lx\n", 5203dc14741SHugh Dickins (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index); 5212682582aSKonstantin Khlebnikov pr_alert("file:%pD fault:%pf mmap:%pf readpage:%pf\n", 5222682582aSKonstantin Khlebnikov vma->vm_file, 5232682582aSKonstantin Khlebnikov vma->vm_ops ? vma->vm_ops->fault : NULL, 5242682582aSKonstantin Khlebnikov vma->vm_file ? vma->vm_file->f_op->mmap : NULL, 5252682582aSKonstantin Khlebnikov mapping ? mapping->a_ops->readpage : NULL); 526b5810039SNick Piggin dump_stack(); 527373d4d09SRusty Russell add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); 528b5810039SNick Piggin } 529b5810039SNick Piggin 530b5810039SNick Piggin /* 5317e675137SNick Piggin * vm_normal_page -- This function gets the "struct page" associated with a pte. 5326aab341eSLinus Torvalds * 5337e675137SNick Piggin * "Special" mappings do not wish to be associated with a "struct page" (either 5347e675137SNick Piggin * it doesn't exist, or it exists but they don't want to touch it). In this 5357e675137SNick Piggin * case, NULL is returned here. "Normal" mappings do have a struct page. 536b379d790SJared Hulbert * 5377e675137SNick Piggin * There are 2 broad cases. Firstly, an architecture may define a pte_special() 5387e675137SNick Piggin * pte bit, in which case this function is trivial. Secondly, an architecture 5397e675137SNick Piggin * may not have a spare pte bit, which requires a more complicated scheme, 5407e675137SNick Piggin * described below. 5417e675137SNick Piggin * 5427e675137SNick Piggin * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a 5437e675137SNick Piggin * special mapping (even if there are underlying and valid "struct pages"). 5447e675137SNick Piggin * COWed pages of a VM_PFNMAP are always normal. 5456aab341eSLinus Torvalds * 546b379d790SJared Hulbert * The way we recognize COWed pages within VM_PFNMAP mappings is through the 547b379d790SJared Hulbert * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit 5487e675137SNick Piggin * set, and the vm_pgoff will point to the first PFN mapped: thus every special 5497e675137SNick Piggin * mapping will always honor the rule 5506aab341eSLinus Torvalds * 5516aab341eSLinus Torvalds * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT) 5526aab341eSLinus Torvalds * 5537e675137SNick Piggin * And for normal mappings this is false. 554b379d790SJared Hulbert * 5557e675137SNick Piggin * This restricts such mappings to be a linear translation from virtual address 5567e675137SNick Piggin * to pfn. To get around this restriction, we allow arbitrary mappings so long 5577e675137SNick Piggin * as the vma is not a COW mapping; in that case, we know that all ptes are 5587e675137SNick Piggin * special (because none can have been COWed). 559b379d790SJared Hulbert * 560b379d790SJared Hulbert * 5617e675137SNick Piggin * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP. 5627e675137SNick Piggin * 563b379d790SJared Hulbert * VM_MIXEDMAP mappings can likewise contain memory with or without "struct 564b379d790SJared Hulbert * page" backing, however the difference is that _all_ pages with a struct 565b379d790SJared Hulbert * page (that is, those where pfn_valid is true) are refcounted and considered 566b379d790SJared Hulbert * normal pages by the VM. The disadvantage is that pages are refcounted 567b379d790SJared Hulbert * (which can be slower and simply not an option for some PFNMAP users). The 568b379d790SJared Hulbert * advantage is that we don't have to follow the strict linearity rule of 569b379d790SJared Hulbert * PFNMAP mappings in order to support COWable mappings. 570b379d790SJared Hulbert * 571ee498ed7SHugh Dickins */ 572df6ad698SJérôme Glisse struct page *_vm_normal_page(struct vm_area_struct *vma, unsigned long addr, 573df6ad698SJérôme Glisse pte_t pte, bool with_public_device) 574ee498ed7SHugh Dickins { 57522b31eecSHugh Dickins unsigned long pfn = pte_pfn(pte); 5767e675137SNick Piggin 57700b3a331SLaurent Dufour if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL)) { 578b38af472SHugh Dickins if (likely(!pte_special(pte))) 57922b31eecSHugh Dickins goto check_pfn; 580667a0a06SDavid Vrabel if (vma->vm_ops && vma->vm_ops->find_special_page) 581667a0a06SDavid Vrabel return vma->vm_ops->find_special_page(vma, addr); 582a13ea5b7SHugh Dickins if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)) 583a13ea5b7SHugh Dickins return NULL; 584df6ad698SJérôme Glisse if (is_zero_pfn(pfn)) 585df6ad698SJérôme Glisse return NULL; 586df6ad698SJérôme Glisse 587df6ad698SJérôme Glisse /* 588df6ad698SJérôme Glisse * Device public pages are special pages (they are ZONE_DEVICE 589df6ad698SJérôme Glisse * pages but different from persistent memory). They behave 590df6ad698SJérôme Glisse * allmost like normal pages. The difference is that they are 591df6ad698SJérôme Glisse * not on the lru and thus should never be involve with any- 592df6ad698SJérôme Glisse * thing that involve lru manipulation (mlock, numa balancing, 593df6ad698SJérôme Glisse * ...). 594df6ad698SJérôme Glisse * 595df6ad698SJérôme Glisse * This is why we still want to return NULL for such page from 596df6ad698SJérôme Glisse * vm_normal_page() so that we do not have to special case all 597df6ad698SJérôme Glisse * call site of vm_normal_page(). 598df6ad698SJérôme Glisse */ 5997d790d2dSReza Arbab if (likely(pfn <= highest_memmap_pfn)) { 600df6ad698SJérôme Glisse struct page *page = pfn_to_page(pfn); 601df6ad698SJérôme Glisse 602df6ad698SJérôme Glisse if (is_device_public_page(page)) { 603df6ad698SJérôme Glisse if (with_public_device) 604df6ad698SJérôme Glisse return page; 605df6ad698SJérôme Glisse return NULL; 606df6ad698SJérôme Glisse } 607df6ad698SJérôme Glisse } 608e1fb4a08SDave Jiang 609e1fb4a08SDave Jiang if (pte_devmap(pte)) 610e1fb4a08SDave Jiang return NULL; 611e1fb4a08SDave Jiang 61222b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 6137e675137SNick Piggin return NULL; 6147e675137SNick Piggin } 6157e675137SNick Piggin 61600b3a331SLaurent Dufour /* !CONFIG_ARCH_HAS_PTE_SPECIAL case follows: */ 6177e675137SNick Piggin 618b379d790SJared Hulbert if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { 619b379d790SJared Hulbert if (vma->vm_flags & VM_MIXEDMAP) { 620b379d790SJared Hulbert if (!pfn_valid(pfn)) 621b379d790SJared Hulbert return NULL; 622b379d790SJared Hulbert goto out; 623b379d790SJared Hulbert } else { 6247e675137SNick Piggin unsigned long off; 6257e675137SNick Piggin off = (addr - vma->vm_start) >> PAGE_SHIFT; 6266aab341eSLinus Torvalds if (pfn == vma->vm_pgoff + off) 6276aab341eSLinus Torvalds return NULL; 62867121172SLinus Torvalds if (!is_cow_mapping(vma->vm_flags)) 629fb155c16SLinus Torvalds return NULL; 6306aab341eSLinus Torvalds } 631b379d790SJared Hulbert } 6326aab341eSLinus Torvalds 633b38af472SHugh Dickins if (is_zero_pfn(pfn)) 634b38af472SHugh Dickins return NULL; 63500b3a331SLaurent Dufour 63622b31eecSHugh Dickins check_pfn: 63722b31eecSHugh Dickins if (unlikely(pfn > highest_memmap_pfn)) { 63822b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 63922b31eecSHugh Dickins return NULL; 64022b31eecSHugh Dickins } 6416aab341eSLinus Torvalds 6426aab341eSLinus Torvalds /* 6437e675137SNick Piggin * NOTE! We still have PageReserved() pages in the page tables. 6447e675137SNick Piggin * eg. VDSO mappings can cause them to exist. 6456aab341eSLinus Torvalds */ 646b379d790SJared Hulbert out: 6476aab341eSLinus Torvalds return pfn_to_page(pfn); 648ee498ed7SHugh Dickins } 649ee498ed7SHugh Dickins 65028093f9fSGerald Schaefer #ifdef CONFIG_TRANSPARENT_HUGEPAGE 65128093f9fSGerald Schaefer struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr, 65228093f9fSGerald Schaefer pmd_t pmd) 65328093f9fSGerald Schaefer { 65428093f9fSGerald Schaefer unsigned long pfn = pmd_pfn(pmd); 65528093f9fSGerald Schaefer 65628093f9fSGerald Schaefer /* 65728093f9fSGerald Schaefer * There is no pmd_special() but there may be special pmds, e.g. 65828093f9fSGerald Schaefer * in a direct-access (dax) mapping, so let's just replicate the 65900b3a331SLaurent Dufour * !CONFIG_ARCH_HAS_PTE_SPECIAL case from vm_normal_page() here. 66028093f9fSGerald Schaefer */ 66128093f9fSGerald Schaefer if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { 66228093f9fSGerald Schaefer if (vma->vm_flags & VM_MIXEDMAP) { 66328093f9fSGerald Schaefer if (!pfn_valid(pfn)) 66428093f9fSGerald Schaefer return NULL; 66528093f9fSGerald Schaefer goto out; 66628093f9fSGerald Schaefer } else { 66728093f9fSGerald Schaefer unsigned long off; 66828093f9fSGerald Schaefer off = (addr - vma->vm_start) >> PAGE_SHIFT; 66928093f9fSGerald Schaefer if (pfn == vma->vm_pgoff + off) 67028093f9fSGerald Schaefer return NULL; 67128093f9fSGerald Schaefer if (!is_cow_mapping(vma->vm_flags)) 67228093f9fSGerald Schaefer return NULL; 67328093f9fSGerald Schaefer } 67428093f9fSGerald Schaefer } 67528093f9fSGerald Schaefer 676e1fb4a08SDave Jiang if (pmd_devmap(pmd)) 677e1fb4a08SDave Jiang return NULL; 67828093f9fSGerald Schaefer if (is_zero_pfn(pfn)) 67928093f9fSGerald Schaefer return NULL; 68028093f9fSGerald Schaefer if (unlikely(pfn > highest_memmap_pfn)) 68128093f9fSGerald Schaefer return NULL; 68228093f9fSGerald Schaefer 68328093f9fSGerald Schaefer /* 68428093f9fSGerald Schaefer * NOTE! We still have PageReserved() pages in the page tables. 68528093f9fSGerald Schaefer * eg. VDSO mappings can cause them to exist. 68628093f9fSGerald Schaefer */ 68728093f9fSGerald Schaefer out: 68828093f9fSGerald Schaefer return pfn_to_page(pfn); 68928093f9fSGerald Schaefer } 69028093f9fSGerald Schaefer #endif 69128093f9fSGerald Schaefer 692ee498ed7SHugh Dickins /* 6931da177e4SLinus Torvalds * copy one vm_area from one task to the other. Assumes the page tables 6941da177e4SLinus Torvalds * already present in the new task to be cleared in the whole range 6951da177e4SLinus Torvalds * covered by this vma. 6961da177e4SLinus Torvalds */ 6971da177e4SLinus Torvalds 698570a335bSHugh Dickins static inline unsigned long 6991da177e4SLinus Torvalds copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, 700b5810039SNick Piggin pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma, 7018c103762SHugh Dickins unsigned long addr, int *rss) 7021da177e4SLinus Torvalds { 703b5810039SNick Piggin unsigned long vm_flags = vma->vm_flags; 7041da177e4SLinus Torvalds pte_t pte = *src_pte; 7051da177e4SLinus Torvalds struct page *page; 7061da177e4SLinus Torvalds 7071da177e4SLinus Torvalds /* pte contains position in swap or file, so copy. */ 7081da177e4SLinus Torvalds if (unlikely(!pte_present(pte))) { 7090697212aSChristoph Lameter swp_entry_t entry = pte_to_swp_entry(pte); 7100697212aSChristoph Lameter 7112022b4d1SHugh Dickins if (likely(!non_swap_entry(entry))) { 712570a335bSHugh Dickins if (swap_duplicate(entry) < 0) 713570a335bSHugh Dickins return entry.val; 714570a335bSHugh Dickins 7151da177e4SLinus Torvalds /* make sure dst_mm is on swapoff's mmlist. */ 7161da177e4SLinus Torvalds if (unlikely(list_empty(&dst_mm->mmlist))) { 7171da177e4SLinus Torvalds spin_lock(&mmlist_lock); 718f412ac08SHugh Dickins if (list_empty(&dst_mm->mmlist)) 719f412ac08SHugh Dickins list_add(&dst_mm->mmlist, 720f412ac08SHugh Dickins &src_mm->mmlist); 7211da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 7221da177e4SLinus Torvalds } 723b084d435SKAMEZAWA Hiroyuki rss[MM_SWAPENTS]++; 7242022b4d1SHugh Dickins } else if (is_migration_entry(entry)) { 7259f9f1acdSKonstantin Khlebnikov page = migration_entry_to_page(entry); 7269f9f1acdSKonstantin Khlebnikov 727eca56ff9SJerome Marchand rss[mm_counter(page)]++; 7289f9f1acdSKonstantin Khlebnikov 7299f9f1acdSKonstantin Khlebnikov if (is_write_migration_entry(entry) && 7300697212aSChristoph Lameter is_cow_mapping(vm_flags)) { 7310697212aSChristoph Lameter /* 7329f9f1acdSKonstantin Khlebnikov * COW mappings require pages in both 7339f9f1acdSKonstantin Khlebnikov * parent and child to be set to read. 7340697212aSChristoph Lameter */ 7350697212aSChristoph Lameter make_migration_entry_read(&entry); 7360697212aSChristoph Lameter pte = swp_entry_to_pte(entry); 737c3d16e16SCyrill Gorcunov if (pte_swp_soft_dirty(*src_pte)) 738c3d16e16SCyrill Gorcunov pte = pte_swp_mksoft_dirty(pte); 7390697212aSChristoph Lameter set_pte_at(src_mm, addr, src_pte, pte); 7400697212aSChristoph Lameter } 7415042db43SJérôme Glisse } else if (is_device_private_entry(entry)) { 7425042db43SJérôme Glisse page = device_private_entry_to_page(entry); 7435042db43SJérôme Glisse 7445042db43SJérôme Glisse /* 7455042db43SJérôme Glisse * Update rss count even for unaddressable pages, as 7465042db43SJérôme Glisse * they should treated just like normal pages in this 7475042db43SJérôme Glisse * respect. 7485042db43SJérôme Glisse * 7495042db43SJérôme Glisse * We will likely want to have some new rss counters 7505042db43SJérôme Glisse * for unaddressable pages, at some point. But for now 7515042db43SJérôme Glisse * keep things as they are. 7525042db43SJérôme Glisse */ 7535042db43SJérôme Glisse get_page(page); 7545042db43SJérôme Glisse rss[mm_counter(page)]++; 7555042db43SJérôme Glisse page_dup_rmap(page, false); 7565042db43SJérôme Glisse 7575042db43SJérôme Glisse /* 7585042db43SJérôme Glisse * We do not preserve soft-dirty information, because so 7595042db43SJérôme Glisse * far, checkpoint/restore is the only feature that 7605042db43SJérôme Glisse * requires that. And checkpoint/restore does not work 7615042db43SJérôme Glisse * when a device driver is involved (you cannot easily 7625042db43SJérôme Glisse * save and restore device driver state). 7635042db43SJérôme Glisse */ 7645042db43SJérôme Glisse if (is_write_device_private_entry(entry) && 7655042db43SJérôme Glisse is_cow_mapping(vm_flags)) { 7665042db43SJérôme Glisse make_device_private_entry_read(&entry); 7675042db43SJérôme Glisse pte = swp_entry_to_pte(entry); 7685042db43SJérôme Glisse set_pte_at(src_mm, addr, src_pte, pte); 7695042db43SJérôme Glisse } 7701da177e4SLinus Torvalds } 771ae859762SHugh Dickins goto out_set_pte; 7721da177e4SLinus Torvalds } 7731da177e4SLinus Torvalds 7741da177e4SLinus Torvalds /* 7751da177e4SLinus Torvalds * If it's a COW mapping, write protect it both 7761da177e4SLinus Torvalds * in the parent and the child 7771da177e4SLinus Torvalds */ 7781b2de5d0SLinus Torvalds if (is_cow_mapping(vm_flags) && pte_write(pte)) { 7791da177e4SLinus Torvalds ptep_set_wrprotect(src_mm, addr, src_pte); 7803dc90795SZachary Amsden pte = pte_wrprotect(pte); 7811da177e4SLinus Torvalds } 7821da177e4SLinus Torvalds 7831da177e4SLinus Torvalds /* 7841da177e4SLinus Torvalds * If it's a shared mapping, mark it clean in 7851da177e4SLinus Torvalds * the child 7861da177e4SLinus Torvalds */ 7871da177e4SLinus Torvalds if (vm_flags & VM_SHARED) 7881da177e4SLinus Torvalds pte = pte_mkclean(pte); 7891da177e4SLinus Torvalds pte = pte_mkold(pte); 7906aab341eSLinus Torvalds 7916aab341eSLinus Torvalds page = vm_normal_page(vma, addr, pte); 7926aab341eSLinus Torvalds if (page) { 7931da177e4SLinus Torvalds get_page(page); 79453f9263bSKirill A. Shutemov page_dup_rmap(page, false); 795eca56ff9SJerome Marchand rss[mm_counter(page)]++; 796df6ad698SJérôme Glisse } else if (pte_devmap(pte)) { 797df6ad698SJérôme Glisse page = pte_page(pte); 798df6ad698SJérôme Glisse 799df6ad698SJérôme Glisse /* 800df6ad698SJérôme Glisse * Cache coherent device memory behave like regular page and 801df6ad698SJérôme Glisse * not like persistent memory page. For more informations see 802df6ad698SJérôme Glisse * MEMORY_DEVICE_CACHE_COHERENT in memory_hotplug.h 803df6ad698SJérôme Glisse */ 804df6ad698SJérôme Glisse if (is_device_public_page(page)) { 805df6ad698SJérôme Glisse get_page(page); 806df6ad698SJérôme Glisse page_dup_rmap(page, false); 807df6ad698SJérôme Glisse rss[mm_counter(page)]++; 808df6ad698SJérôme Glisse } 8096aab341eSLinus Torvalds } 810ae859762SHugh Dickins 811ae859762SHugh Dickins out_set_pte: 812ae859762SHugh Dickins set_pte_at(dst_mm, addr, dst_pte, pte); 813570a335bSHugh Dickins return 0; 8141da177e4SLinus Torvalds } 8151da177e4SLinus Torvalds 81621bda264SJerome Marchand static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 8171da177e4SLinus Torvalds pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma, 8181da177e4SLinus Torvalds unsigned long addr, unsigned long end) 8191da177e4SLinus Torvalds { 820c36987e2SDaisuke Nishimura pte_t *orig_src_pte, *orig_dst_pte; 8211da177e4SLinus Torvalds pte_t *src_pte, *dst_pte; 822c74df32cSHugh Dickins spinlock_t *src_ptl, *dst_ptl; 823e040f218SHugh Dickins int progress = 0; 824d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 825570a335bSHugh Dickins swp_entry_t entry = (swp_entry_t){0}; 8261da177e4SLinus Torvalds 8271da177e4SLinus Torvalds again: 828d559db08SKAMEZAWA Hiroyuki init_rss_vec(rss); 829d559db08SKAMEZAWA Hiroyuki 830c74df32cSHugh Dickins dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl); 8311da177e4SLinus Torvalds if (!dst_pte) 8321da177e4SLinus Torvalds return -ENOMEM; 833ece0e2b6SPeter Zijlstra src_pte = pte_offset_map(src_pmd, addr); 8344c21e2f2SHugh Dickins src_ptl = pte_lockptr(src_mm, src_pmd); 835f20dc5f7SIngo Molnar spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 836c36987e2SDaisuke Nishimura orig_src_pte = src_pte; 837c36987e2SDaisuke Nishimura orig_dst_pte = dst_pte; 8386606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 8391da177e4SLinus Torvalds 8401da177e4SLinus Torvalds do { 8411da177e4SLinus Torvalds /* 8421da177e4SLinus Torvalds * We are holding two locks at this point - either of them 8431da177e4SLinus Torvalds * could generate latencies in another task on another CPU. 8441da177e4SLinus Torvalds */ 845e040f218SHugh Dickins if (progress >= 32) { 846e040f218SHugh Dickins progress = 0; 847e040f218SHugh Dickins if (need_resched() || 84895c354feSNick Piggin spin_needbreak(src_ptl) || spin_needbreak(dst_ptl)) 8491da177e4SLinus Torvalds break; 850e040f218SHugh Dickins } 8511da177e4SLinus Torvalds if (pte_none(*src_pte)) { 8521da177e4SLinus Torvalds progress++; 8531da177e4SLinus Torvalds continue; 8541da177e4SLinus Torvalds } 855570a335bSHugh Dickins entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte, 856570a335bSHugh Dickins vma, addr, rss); 857570a335bSHugh Dickins if (entry.val) 858570a335bSHugh Dickins break; 8591da177e4SLinus Torvalds progress += 8; 8601da177e4SLinus Torvalds } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end); 8611da177e4SLinus Torvalds 8626606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 863c74df32cSHugh Dickins spin_unlock(src_ptl); 864ece0e2b6SPeter Zijlstra pte_unmap(orig_src_pte); 865d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(dst_mm, rss); 866c36987e2SDaisuke Nishimura pte_unmap_unlock(orig_dst_pte, dst_ptl); 867c74df32cSHugh Dickins cond_resched(); 868570a335bSHugh Dickins 869570a335bSHugh Dickins if (entry.val) { 870570a335bSHugh Dickins if (add_swap_count_continuation(entry, GFP_KERNEL) < 0) 871570a335bSHugh Dickins return -ENOMEM; 872570a335bSHugh Dickins progress = 0; 873570a335bSHugh Dickins } 8741da177e4SLinus Torvalds if (addr != end) 8751da177e4SLinus Torvalds goto again; 8761da177e4SLinus Torvalds return 0; 8771da177e4SLinus Torvalds } 8781da177e4SLinus Torvalds 8791da177e4SLinus Torvalds static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 8801da177e4SLinus Torvalds pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma, 8811da177e4SLinus Torvalds unsigned long addr, unsigned long end) 8821da177e4SLinus Torvalds { 8831da177e4SLinus Torvalds pmd_t *src_pmd, *dst_pmd; 8841da177e4SLinus Torvalds unsigned long next; 8851da177e4SLinus Torvalds 8861da177e4SLinus Torvalds dst_pmd = pmd_alloc(dst_mm, dst_pud, addr); 8871da177e4SLinus Torvalds if (!dst_pmd) 8881da177e4SLinus Torvalds return -ENOMEM; 8891da177e4SLinus Torvalds src_pmd = pmd_offset(src_pud, addr); 8901da177e4SLinus Torvalds do { 8911da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 89284c3fc4eSZi Yan if (is_swap_pmd(*src_pmd) || pmd_trans_huge(*src_pmd) 89384c3fc4eSZi Yan || pmd_devmap(*src_pmd)) { 89471e3aac0SAndrea Arcangeli int err; 895a00cc7d9SMatthew Wilcox VM_BUG_ON_VMA(next-addr != HPAGE_PMD_SIZE, vma); 89671e3aac0SAndrea Arcangeli err = copy_huge_pmd(dst_mm, src_mm, 89771e3aac0SAndrea Arcangeli dst_pmd, src_pmd, addr, vma); 89871e3aac0SAndrea Arcangeli if (err == -ENOMEM) 89971e3aac0SAndrea Arcangeli return -ENOMEM; 90071e3aac0SAndrea Arcangeli if (!err) 90171e3aac0SAndrea Arcangeli continue; 90271e3aac0SAndrea Arcangeli /* fall through */ 90371e3aac0SAndrea Arcangeli } 9041da177e4SLinus Torvalds if (pmd_none_or_clear_bad(src_pmd)) 9051da177e4SLinus Torvalds continue; 9061da177e4SLinus Torvalds if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd, 9071da177e4SLinus Torvalds vma, addr, next)) 9081da177e4SLinus Torvalds return -ENOMEM; 9091da177e4SLinus Torvalds } while (dst_pmd++, src_pmd++, addr = next, addr != end); 9101da177e4SLinus Torvalds return 0; 9111da177e4SLinus Torvalds } 9121da177e4SLinus Torvalds 9131da177e4SLinus Torvalds static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 914c2febafcSKirill A. Shutemov p4d_t *dst_p4d, p4d_t *src_p4d, struct vm_area_struct *vma, 9151da177e4SLinus Torvalds unsigned long addr, unsigned long end) 9161da177e4SLinus Torvalds { 9171da177e4SLinus Torvalds pud_t *src_pud, *dst_pud; 9181da177e4SLinus Torvalds unsigned long next; 9191da177e4SLinus Torvalds 920c2febafcSKirill A. Shutemov dst_pud = pud_alloc(dst_mm, dst_p4d, addr); 9211da177e4SLinus Torvalds if (!dst_pud) 9221da177e4SLinus Torvalds return -ENOMEM; 923c2febafcSKirill A. Shutemov src_pud = pud_offset(src_p4d, addr); 9241da177e4SLinus Torvalds do { 9251da177e4SLinus Torvalds next = pud_addr_end(addr, end); 926a00cc7d9SMatthew Wilcox if (pud_trans_huge(*src_pud) || pud_devmap(*src_pud)) { 927a00cc7d9SMatthew Wilcox int err; 928a00cc7d9SMatthew Wilcox 929a00cc7d9SMatthew Wilcox VM_BUG_ON_VMA(next-addr != HPAGE_PUD_SIZE, vma); 930a00cc7d9SMatthew Wilcox err = copy_huge_pud(dst_mm, src_mm, 931a00cc7d9SMatthew Wilcox dst_pud, src_pud, addr, vma); 932a00cc7d9SMatthew Wilcox if (err == -ENOMEM) 933a00cc7d9SMatthew Wilcox return -ENOMEM; 934a00cc7d9SMatthew Wilcox if (!err) 935a00cc7d9SMatthew Wilcox continue; 936a00cc7d9SMatthew Wilcox /* fall through */ 937a00cc7d9SMatthew Wilcox } 9381da177e4SLinus Torvalds if (pud_none_or_clear_bad(src_pud)) 9391da177e4SLinus Torvalds continue; 9401da177e4SLinus Torvalds if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud, 9411da177e4SLinus Torvalds vma, addr, next)) 9421da177e4SLinus Torvalds return -ENOMEM; 9431da177e4SLinus Torvalds } while (dst_pud++, src_pud++, addr = next, addr != end); 9441da177e4SLinus Torvalds return 0; 9451da177e4SLinus Torvalds } 9461da177e4SLinus Torvalds 947c2febafcSKirill A. Shutemov static inline int copy_p4d_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 948c2febafcSKirill A. Shutemov pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma, 949c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end) 950c2febafcSKirill A. Shutemov { 951c2febafcSKirill A. Shutemov p4d_t *src_p4d, *dst_p4d; 952c2febafcSKirill A. Shutemov unsigned long next; 953c2febafcSKirill A. Shutemov 954c2febafcSKirill A. Shutemov dst_p4d = p4d_alloc(dst_mm, dst_pgd, addr); 955c2febafcSKirill A. Shutemov if (!dst_p4d) 956c2febafcSKirill A. Shutemov return -ENOMEM; 957c2febafcSKirill A. Shutemov src_p4d = p4d_offset(src_pgd, addr); 958c2febafcSKirill A. Shutemov do { 959c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 960c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(src_p4d)) 961c2febafcSKirill A. Shutemov continue; 962c2febafcSKirill A. Shutemov if (copy_pud_range(dst_mm, src_mm, dst_p4d, src_p4d, 963c2febafcSKirill A. Shutemov vma, addr, next)) 964c2febafcSKirill A. Shutemov return -ENOMEM; 965c2febafcSKirill A. Shutemov } while (dst_p4d++, src_p4d++, addr = next, addr != end); 966c2febafcSKirill A. Shutemov return 0; 967c2febafcSKirill A. Shutemov } 968c2febafcSKirill A. Shutemov 9691da177e4SLinus Torvalds int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 9701da177e4SLinus Torvalds struct vm_area_struct *vma) 9711da177e4SLinus Torvalds { 9721da177e4SLinus Torvalds pgd_t *src_pgd, *dst_pgd; 9731da177e4SLinus Torvalds unsigned long next; 9741da177e4SLinus Torvalds unsigned long addr = vma->vm_start; 9751da177e4SLinus Torvalds unsigned long end = vma->vm_end; 9762ec74c3eSSagi Grimberg unsigned long mmun_start; /* For mmu_notifiers */ 9772ec74c3eSSagi Grimberg unsigned long mmun_end; /* For mmu_notifiers */ 9782ec74c3eSSagi Grimberg bool is_cow; 979cddb8a5cSAndrea Arcangeli int ret; 9801da177e4SLinus Torvalds 981d992895bSNick Piggin /* 982d992895bSNick Piggin * Don't copy ptes where a page fault will fill them correctly. 983d992895bSNick Piggin * Fork becomes much lighter when there are big shared or private 984d992895bSNick Piggin * readonly mappings. The tradeoff is that copy_page_range is more 985d992895bSNick Piggin * efficient than faulting. 986d992895bSNick Piggin */ 9870661a336SKirill A. Shutemov if (!(vma->vm_flags & (VM_HUGETLB | VM_PFNMAP | VM_MIXEDMAP)) && 9880661a336SKirill A. Shutemov !vma->anon_vma) 989d992895bSNick Piggin return 0; 990d992895bSNick Piggin 9911da177e4SLinus Torvalds if (is_vm_hugetlb_page(vma)) 9921da177e4SLinus Torvalds return copy_hugetlb_page_range(dst_mm, src_mm, vma); 9931da177e4SLinus Torvalds 994b3b9c293SKonstantin Khlebnikov if (unlikely(vma->vm_flags & VM_PFNMAP)) { 9952ab64037Svenkatesh.pallipadi@intel.com /* 9962ab64037Svenkatesh.pallipadi@intel.com * We do not free on error cases below as remove_vma 9972ab64037Svenkatesh.pallipadi@intel.com * gets called on error from higher level routine 9982ab64037Svenkatesh.pallipadi@intel.com */ 9995180da41SSuresh Siddha ret = track_pfn_copy(vma); 10002ab64037Svenkatesh.pallipadi@intel.com if (ret) 10012ab64037Svenkatesh.pallipadi@intel.com return ret; 10022ab64037Svenkatesh.pallipadi@intel.com } 10032ab64037Svenkatesh.pallipadi@intel.com 1004cddb8a5cSAndrea Arcangeli /* 1005cddb8a5cSAndrea Arcangeli * We need to invalidate the secondary MMU mappings only when 1006cddb8a5cSAndrea Arcangeli * there could be a permission downgrade on the ptes of the 1007cddb8a5cSAndrea Arcangeli * parent mm. And a permission downgrade will only happen if 1008cddb8a5cSAndrea Arcangeli * is_cow_mapping() returns true. 1009cddb8a5cSAndrea Arcangeli */ 10102ec74c3eSSagi Grimberg is_cow = is_cow_mapping(vma->vm_flags); 10112ec74c3eSSagi Grimberg mmun_start = addr; 10122ec74c3eSSagi Grimberg mmun_end = end; 10132ec74c3eSSagi Grimberg if (is_cow) 10142ec74c3eSSagi Grimberg mmu_notifier_invalidate_range_start(src_mm, mmun_start, 10152ec74c3eSSagi Grimberg mmun_end); 1016cddb8a5cSAndrea Arcangeli 1017cddb8a5cSAndrea Arcangeli ret = 0; 10181da177e4SLinus Torvalds dst_pgd = pgd_offset(dst_mm, addr); 10191da177e4SLinus Torvalds src_pgd = pgd_offset(src_mm, addr); 10201da177e4SLinus Torvalds do { 10211da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 10221da177e4SLinus Torvalds if (pgd_none_or_clear_bad(src_pgd)) 10231da177e4SLinus Torvalds continue; 1024c2febafcSKirill A. Shutemov if (unlikely(copy_p4d_range(dst_mm, src_mm, dst_pgd, src_pgd, 1025cddb8a5cSAndrea Arcangeli vma, addr, next))) { 1026cddb8a5cSAndrea Arcangeli ret = -ENOMEM; 1027cddb8a5cSAndrea Arcangeli break; 1028cddb8a5cSAndrea Arcangeli } 10291da177e4SLinus Torvalds } while (dst_pgd++, src_pgd++, addr = next, addr != end); 1030cddb8a5cSAndrea Arcangeli 10312ec74c3eSSagi Grimberg if (is_cow) 10322ec74c3eSSagi Grimberg mmu_notifier_invalidate_range_end(src_mm, mmun_start, mmun_end); 1033cddb8a5cSAndrea Arcangeli return ret; 10341da177e4SLinus Torvalds } 10351da177e4SLinus Torvalds 103651c6f666SRobin Holt static unsigned long zap_pte_range(struct mmu_gather *tlb, 1037b5810039SNick Piggin struct vm_area_struct *vma, pmd_t *pmd, 10381da177e4SLinus Torvalds unsigned long addr, unsigned long end, 103997a89413SPeter Zijlstra struct zap_details *details) 10401da177e4SLinus Torvalds { 1041b5810039SNick Piggin struct mm_struct *mm = tlb->mm; 1042d16dfc55SPeter Zijlstra int force_flush = 0; 1043d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 104497a89413SPeter Zijlstra spinlock_t *ptl; 10455f1a1907SSteven Rostedt pte_t *start_pte; 104697a89413SPeter Zijlstra pte_t *pte; 10478a5f14a2SKirill A. Shutemov swp_entry_t entry; 1048d559db08SKAMEZAWA Hiroyuki 104907e32661SAneesh Kumar K.V tlb_remove_check_page_size_change(tlb, PAGE_SIZE); 1050d16dfc55SPeter Zijlstra again: 1051e303297eSPeter Zijlstra init_rss_vec(rss); 10525f1a1907SSteven Rostedt start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl); 10535f1a1907SSteven Rostedt pte = start_pte; 10543ea27719SMel Gorman flush_tlb_batched_pending(mm); 10556606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 10561da177e4SLinus Torvalds do { 10571da177e4SLinus Torvalds pte_t ptent = *pte; 1058166f61b9STobin C Harding if (pte_none(ptent)) 10591da177e4SLinus Torvalds continue; 106051c6f666SRobin Holt 10616f5e6b9eSHugh Dickins if (pte_present(ptent)) { 10626f5e6b9eSHugh Dickins struct page *page; 10636f5e6b9eSHugh Dickins 1064df6ad698SJérôme Glisse page = _vm_normal_page(vma, addr, ptent, true); 10651da177e4SLinus Torvalds if (unlikely(details) && page) { 10661da177e4SLinus Torvalds /* 10671da177e4SLinus Torvalds * unmap_shared_mapping_pages() wants to 10681da177e4SLinus Torvalds * invalidate cache without truncating: 10691da177e4SLinus Torvalds * unmap shared but keep private pages. 10701da177e4SLinus Torvalds */ 10711da177e4SLinus Torvalds if (details->check_mapping && 1072800d8c63SKirill A. Shutemov details->check_mapping != page_rmapping(page)) 10731da177e4SLinus Torvalds continue; 10741da177e4SLinus Torvalds } 1075b5810039SNick Piggin ptent = ptep_get_and_clear_full(mm, addr, pte, 1076a600388dSZachary Amsden tlb->fullmm); 10771da177e4SLinus Torvalds tlb_remove_tlb_entry(tlb, pte, addr); 10781da177e4SLinus Torvalds if (unlikely(!page)) 10791da177e4SLinus Torvalds continue; 1080eca56ff9SJerome Marchand 1081eca56ff9SJerome Marchand if (!PageAnon(page)) { 10821cf35d47SLinus Torvalds if (pte_dirty(ptent)) { 10831cf35d47SLinus Torvalds force_flush = 1; 10846237bcd9SHugh Dickins set_page_dirty(page); 10851cf35d47SLinus Torvalds } 10864917e5d0SJohannes Weiner if (pte_young(ptent) && 108764363aadSJoe Perches likely(!(vma->vm_flags & VM_SEQ_READ))) 1088bf3f3bc5SNick Piggin mark_page_accessed(page); 10896237bcd9SHugh Dickins } 1090eca56ff9SJerome Marchand rss[mm_counter(page)]--; 1091d281ee61SKirill A. Shutemov page_remove_rmap(page, false); 10923dc14741SHugh Dickins if (unlikely(page_mapcount(page) < 0)) 10933dc14741SHugh Dickins print_bad_pte(vma, addr, ptent, page); 1094e9d55e15SAneesh Kumar K.V if (unlikely(__tlb_remove_page(tlb, page))) { 10951cf35d47SLinus Torvalds force_flush = 1; 1096ce9ec37bSWill Deacon addr += PAGE_SIZE; 1097d16dfc55SPeter Zijlstra break; 10981cf35d47SLinus Torvalds } 10991da177e4SLinus Torvalds continue; 11001da177e4SLinus Torvalds } 11015042db43SJérôme Glisse 11025042db43SJérôme Glisse entry = pte_to_swp_entry(ptent); 11035042db43SJérôme Glisse if (non_swap_entry(entry) && is_device_private_entry(entry)) { 11045042db43SJérôme Glisse struct page *page = device_private_entry_to_page(entry); 11055042db43SJérôme Glisse 11065042db43SJérôme Glisse if (unlikely(details && details->check_mapping)) { 11075042db43SJérôme Glisse /* 11085042db43SJérôme Glisse * unmap_shared_mapping_pages() wants to 11095042db43SJérôme Glisse * invalidate cache without truncating: 11105042db43SJérôme Glisse * unmap shared but keep private pages. 11115042db43SJérôme Glisse */ 11125042db43SJérôme Glisse if (details->check_mapping != 11135042db43SJérôme Glisse page_rmapping(page)) 11145042db43SJérôme Glisse continue; 11155042db43SJérôme Glisse } 11165042db43SJérôme Glisse 11175042db43SJérôme Glisse pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); 11185042db43SJérôme Glisse rss[mm_counter(page)]--; 11195042db43SJérôme Glisse page_remove_rmap(page, false); 11205042db43SJérôme Glisse put_page(page); 11215042db43SJérôme Glisse continue; 11225042db43SJérôme Glisse } 11235042db43SJérôme Glisse 11243e8715fdSKirill A. Shutemov /* If details->check_mapping, we leave swap entries. */ 11253e8715fdSKirill A. Shutemov if (unlikely(details)) 11261da177e4SLinus Torvalds continue; 1127b084d435SKAMEZAWA Hiroyuki 11288a5f14a2SKirill A. Shutemov entry = pte_to_swp_entry(ptent); 1129b084d435SKAMEZAWA Hiroyuki if (!non_swap_entry(entry)) 1130b084d435SKAMEZAWA Hiroyuki rss[MM_SWAPENTS]--; 11319f9f1acdSKonstantin Khlebnikov else if (is_migration_entry(entry)) { 11329f9f1acdSKonstantin Khlebnikov struct page *page; 11339f9f1acdSKonstantin Khlebnikov 11349f9f1acdSKonstantin Khlebnikov page = migration_entry_to_page(entry); 1135eca56ff9SJerome Marchand rss[mm_counter(page)]--; 11369f9f1acdSKonstantin Khlebnikov } 1137b084d435SKAMEZAWA Hiroyuki if (unlikely(!free_swap_and_cache(entry))) 11382509ef26SHugh Dickins print_bad_pte(vma, addr, ptent, NULL); 11399888a1caSZachary Amsden pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); 114097a89413SPeter Zijlstra } while (pte++, addr += PAGE_SIZE, addr != end); 1141ae859762SHugh Dickins 1142d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(mm, rss); 11436606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 114451c6f666SRobin Holt 11451cf35d47SLinus Torvalds /* Do the actual TLB flush before dropping ptl */ 1146fb7332a9SWill Deacon if (force_flush) 11471cf35d47SLinus Torvalds tlb_flush_mmu_tlbonly(tlb); 11481cf35d47SLinus Torvalds pte_unmap_unlock(start_pte, ptl); 11491cf35d47SLinus Torvalds 11501cf35d47SLinus Torvalds /* 11511cf35d47SLinus Torvalds * If we forced a TLB flush (either due to running out of 11521cf35d47SLinus Torvalds * batch buffers or because we needed to flush dirty TLB 11531cf35d47SLinus Torvalds * entries before releasing the ptl), free the batched 11541cf35d47SLinus Torvalds * memory too. Restart if we didn't do everything. 11551cf35d47SLinus Torvalds */ 11561cf35d47SLinus Torvalds if (force_flush) { 11571cf35d47SLinus Torvalds force_flush = 0; 11581cf35d47SLinus Torvalds tlb_flush_mmu_free(tlb); 11592b047252SLinus Torvalds if (addr != end) 1160d16dfc55SPeter Zijlstra goto again; 1161d16dfc55SPeter Zijlstra } 1162d16dfc55SPeter Zijlstra 116351c6f666SRobin Holt return addr; 11641da177e4SLinus Torvalds } 11651da177e4SLinus Torvalds 116651c6f666SRobin Holt static inline unsigned long zap_pmd_range(struct mmu_gather *tlb, 1167b5810039SNick Piggin struct vm_area_struct *vma, pud_t *pud, 11681da177e4SLinus Torvalds unsigned long addr, unsigned long end, 116997a89413SPeter Zijlstra struct zap_details *details) 11701da177e4SLinus Torvalds { 11711da177e4SLinus Torvalds pmd_t *pmd; 11721da177e4SLinus Torvalds unsigned long next; 11731da177e4SLinus Torvalds 11741da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 11751da177e4SLinus Torvalds do { 11761da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 117784c3fc4eSZi Yan if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) { 117853406ed1SHugh Dickins if (next - addr != HPAGE_PMD_SIZE) 1179fd60775aSDavid Rientjes __split_huge_pmd(vma, pmd, addr, false, NULL); 118053406ed1SHugh Dickins else if (zap_huge_pmd(tlb, vma, pmd, addr)) 11811a5a9906SAndrea Arcangeli goto next; 118271e3aac0SAndrea Arcangeli /* fall through */ 118371e3aac0SAndrea Arcangeli } 11841a5a9906SAndrea Arcangeli /* 11851a5a9906SAndrea Arcangeli * Here there can be other concurrent MADV_DONTNEED or 11861a5a9906SAndrea Arcangeli * trans huge page faults running, and if the pmd is 11871a5a9906SAndrea Arcangeli * none or trans huge it can change under us. This is 11881a5a9906SAndrea Arcangeli * because MADV_DONTNEED holds the mmap_sem in read 11891a5a9906SAndrea Arcangeli * mode. 11901a5a9906SAndrea Arcangeli */ 11911a5a9906SAndrea Arcangeli if (pmd_none_or_trans_huge_or_clear_bad(pmd)) 11921a5a9906SAndrea Arcangeli goto next; 119397a89413SPeter Zijlstra next = zap_pte_range(tlb, vma, pmd, addr, next, details); 11941a5a9906SAndrea Arcangeli next: 119597a89413SPeter Zijlstra cond_resched(); 119697a89413SPeter Zijlstra } while (pmd++, addr = next, addr != end); 119751c6f666SRobin Holt 119851c6f666SRobin Holt return addr; 11991da177e4SLinus Torvalds } 12001da177e4SLinus Torvalds 120151c6f666SRobin Holt static inline unsigned long zap_pud_range(struct mmu_gather *tlb, 1202c2febafcSKirill A. Shutemov struct vm_area_struct *vma, p4d_t *p4d, 12031da177e4SLinus Torvalds unsigned long addr, unsigned long end, 120497a89413SPeter Zijlstra struct zap_details *details) 12051da177e4SLinus Torvalds { 12061da177e4SLinus Torvalds pud_t *pud; 12071da177e4SLinus Torvalds unsigned long next; 12081da177e4SLinus Torvalds 1209c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 12101da177e4SLinus Torvalds do { 12111da177e4SLinus Torvalds next = pud_addr_end(addr, end); 1212a00cc7d9SMatthew Wilcox if (pud_trans_huge(*pud) || pud_devmap(*pud)) { 1213a00cc7d9SMatthew Wilcox if (next - addr != HPAGE_PUD_SIZE) { 1214a00cc7d9SMatthew Wilcox VM_BUG_ON_VMA(!rwsem_is_locked(&tlb->mm->mmap_sem), vma); 1215a00cc7d9SMatthew Wilcox split_huge_pud(vma, pud, addr); 1216a00cc7d9SMatthew Wilcox } else if (zap_huge_pud(tlb, vma, pud, addr)) 1217a00cc7d9SMatthew Wilcox goto next; 1218a00cc7d9SMatthew Wilcox /* fall through */ 1219a00cc7d9SMatthew Wilcox } 122097a89413SPeter Zijlstra if (pud_none_or_clear_bad(pud)) 12211da177e4SLinus Torvalds continue; 122297a89413SPeter Zijlstra next = zap_pmd_range(tlb, vma, pud, addr, next, details); 1223a00cc7d9SMatthew Wilcox next: 1224a00cc7d9SMatthew Wilcox cond_resched(); 122597a89413SPeter Zijlstra } while (pud++, addr = next, addr != end); 122651c6f666SRobin Holt 122751c6f666SRobin Holt return addr; 12281da177e4SLinus Torvalds } 12291da177e4SLinus Torvalds 1230c2febafcSKirill A. Shutemov static inline unsigned long zap_p4d_range(struct mmu_gather *tlb, 1231c2febafcSKirill A. Shutemov struct vm_area_struct *vma, pgd_t *pgd, 1232c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 1233c2febafcSKirill A. Shutemov struct zap_details *details) 1234c2febafcSKirill A. Shutemov { 1235c2febafcSKirill A. Shutemov p4d_t *p4d; 1236c2febafcSKirill A. Shutemov unsigned long next; 1237c2febafcSKirill A. Shutemov 1238c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 1239c2febafcSKirill A. Shutemov do { 1240c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 1241c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(p4d)) 1242c2febafcSKirill A. Shutemov continue; 1243c2febafcSKirill A. Shutemov next = zap_pud_range(tlb, vma, p4d, addr, next, details); 1244c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 1245c2febafcSKirill A. Shutemov 1246c2febafcSKirill A. Shutemov return addr; 1247c2febafcSKirill A. Shutemov } 1248c2febafcSKirill A. Shutemov 1249aac45363SMichal Hocko void unmap_page_range(struct mmu_gather *tlb, 125051c6f666SRobin Holt struct vm_area_struct *vma, 12511da177e4SLinus Torvalds unsigned long addr, unsigned long end, 125297a89413SPeter Zijlstra struct zap_details *details) 12531da177e4SLinus Torvalds { 12541da177e4SLinus Torvalds pgd_t *pgd; 12551da177e4SLinus Torvalds unsigned long next; 12561da177e4SLinus Torvalds 12571da177e4SLinus Torvalds BUG_ON(addr >= end); 12581da177e4SLinus Torvalds tlb_start_vma(tlb, vma); 12591da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 12601da177e4SLinus Torvalds do { 12611da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 126297a89413SPeter Zijlstra if (pgd_none_or_clear_bad(pgd)) 12631da177e4SLinus Torvalds continue; 1264c2febafcSKirill A. Shutemov next = zap_p4d_range(tlb, vma, pgd, addr, next, details); 126597a89413SPeter Zijlstra } while (pgd++, addr = next, addr != end); 12661da177e4SLinus Torvalds tlb_end_vma(tlb, vma); 12671da177e4SLinus Torvalds } 12681da177e4SLinus Torvalds 1269f5cc4eefSAl Viro 1270f5cc4eefSAl Viro static void unmap_single_vma(struct mmu_gather *tlb, 12711da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long start_addr, 12724f74d2c8SLinus Torvalds unsigned long end_addr, 12731da177e4SLinus Torvalds struct zap_details *details) 12741da177e4SLinus Torvalds { 1275f5cc4eefSAl Viro unsigned long start = max(vma->vm_start, start_addr); 12761da177e4SLinus Torvalds unsigned long end; 12771da177e4SLinus Torvalds 12781da177e4SLinus Torvalds if (start >= vma->vm_end) 1279f5cc4eefSAl Viro return; 12801da177e4SLinus Torvalds end = min(vma->vm_end, end_addr); 12811da177e4SLinus Torvalds if (end <= vma->vm_start) 1282f5cc4eefSAl Viro return; 12831da177e4SLinus Torvalds 1284cbc91f71SSrikar Dronamraju if (vma->vm_file) 1285cbc91f71SSrikar Dronamraju uprobe_munmap(vma, start, end); 1286cbc91f71SSrikar Dronamraju 1287b3b9c293SKonstantin Khlebnikov if (unlikely(vma->vm_flags & VM_PFNMAP)) 12885180da41SSuresh Siddha untrack_pfn(vma, 0, 0); 12892ab64037Svenkatesh.pallipadi@intel.com 12908b2a1238SAl Viro if (start != end) { 129151c6f666SRobin Holt if (unlikely(is_vm_hugetlb_page(vma))) { 1292a137e1ccSAndi Kleen /* 1293a137e1ccSAndi Kleen * It is undesirable to test vma->vm_file as it 1294a137e1ccSAndi Kleen * should be non-null for valid hugetlb area. 1295a137e1ccSAndi Kleen * However, vm_file will be NULL in the error 12967aa6b4adSDavidlohr Bueso * cleanup path of mmap_region. When 1297a137e1ccSAndi Kleen * hugetlbfs ->mmap method fails, 12987aa6b4adSDavidlohr Bueso * mmap_region() nullifies vma->vm_file 1299a137e1ccSAndi Kleen * before calling this function to clean up. 1300a137e1ccSAndi Kleen * Since no pte has actually been setup, it is 1301a137e1ccSAndi Kleen * safe to do nothing in this case. 1302a137e1ccSAndi Kleen */ 130324669e58SAneesh Kumar K.V if (vma->vm_file) { 130483cde9e8SDavidlohr Bueso i_mmap_lock_write(vma->vm_file->f_mapping); 1305d833352aSMel Gorman __unmap_hugepage_range_final(tlb, vma, start, end, NULL); 130683cde9e8SDavidlohr Bueso i_mmap_unlock_write(vma->vm_file->f_mapping); 130724669e58SAneesh Kumar K.V } 130851c6f666SRobin Holt } else 1309038c7aa1SAl Viro unmap_page_range(tlb, vma, start, end, details); 131097a89413SPeter Zijlstra } 131151c6f666SRobin Holt } 13121da177e4SLinus Torvalds 1313f5cc4eefSAl Viro /** 1314f5cc4eefSAl Viro * unmap_vmas - unmap a range of memory covered by a list of vma's 1315f5cc4eefSAl Viro * @tlb: address of the caller's struct mmu_gather 1316f5cc4eefSAl Viro * @vma: the starting vma 1317f5cc4eefSAl Viro * @start_addr: virtual address at which to start unmapping 1318f5cc4eefSAl Viro * @end_addr: virtual address at which to end unmapping 1319f5cc4eefSAl Viro * 1320f5cc4eefSAl Viro * Unmap all pages in the vma list. 1321f5cc4eefSAl Viro * 1322f5cc4eefSAl Viro * Only addresses between `start' and `end' will be unmapped. 1323f5cc4eefSAl Viro * 1324f5cc4eefSAl Viro * The VMA list must be sorted in ascending virtual address order. 1325f5cc4eefSAl Viro * 1326f5cc4eefSAl Viro * unmap_vmas() assumes that the caller will flush the whole unmapped address 1327f5cc4eefSAl Viro * range after unmap_vmas() returns. So the only responsibility here is to 1328f5cc4eefSAl Viro * ensure that any thus-far unmapped pages are flushed before unmap_vmas() 1329f5cc4eefSAl Viro * drops the lock and schedules. 1330f5cc4eefSAl Viro */ 1331f5cc4eefSAl Viro void unmap_vmas(struct mmu_gather *tlb, 1332f5cc4eefSAl Viro struct vm_area_struct *vma, unsigned long start_addr, 13334f74d2c8SLinus Torvalds unsigned long end_addr) 1334f5cc4eefSAl Viro { 1335f5cc4eefSAl Viro struct mm_struct *mm = vma->vm_mm; 1336f5cc4eefSAl Viro 1337f5cc4eefSAl Viro mmu_notifier_invalidate_range_start(mm, start_addr, end_addr); 1338f5cc4eefSAl Viro for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) 13394f74d2c8SLinus Torvalds unmap_single_vma(tlb, vma, start_addr, end_addr, NULL); 1340cddb8a5cSAndrea Arcangeli mmu_notifier_invalidate_range_end(mm, start_addr, end_addr); 13411da177e4SLinus Torvalds } 13421da177e4SLinus Torvalds 13431da177e4SLinus Torvalds /** 13441da177e4SLinus Torvalds * zap_page_range - remove user pages in a given range 13451da177e4SLinus Torvalds * @vma: vm_area_struct holding the applicable pages 1346eb4546bbSRandy Dunlap * @start: starting address of pages to zap 13471da177e4SLinus Torvalds * @size: number of bytes to zap 1348f5cc4eefSAl Viro * 1349f5cc4eefSAl Viro * Caller must protect the VMA list 13501da177e4SLinus Torvalds */ 13517e027b14SLinus Torvalds void zap_page_range(struct vm_area_struct *vma, unsigned long start, 1352ecf1385dSKirill A. Shutemov unsigned long size) 13531da177e4SLinus Torvalds { 13541da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 1355d16dfc55SPeter Zijlstra struct mmu_gather tlb; 13567e027b14SLinus Torvalds unsigned long end = start + size; 13571da177e4SLinus Torvalds 13581da177e4SLinus Torvalds lru_add_drain(); 13592b047252SLinus Torvalds tlb_gather_mmu(&tlb, mm, start, end); 1360365e9c87SHugh Dickins update_hiwater_rss(mm); 13617e027b14SLinus Torvalds mmu_notifier_invalidate_range_start(mm, start, end); 136250c150f2SRik van Riel for ( ; vma && vma->vm_start < end; vma = vma->vm_next) 1363ecf1385dSKirill A. Shutemov unmap_single_vma(&tlb, vma, start, end, NULL); 13647e027b14SLinus Torvalds mmu_notifier_invalidate_range_end(mm, start, end); 13657e027b14SLinus Torvalds tlb_finish_mmu(&tlb, start, end); 13661da177e4SLinus Torvalds } 13671da177e4SLinus Torvalds 1368c627f9ccSJack Steiner /** 1369f5cc4eefSAl Viro * zap_page_range_single - remove user pages in a given range 1370f5cc4eefSAl Viro * @vma: vm_area_struct holding the applicable pages 1371f5cc4eefSAl Viro * @address: starting address of pages to zap 1372f5cc4eefSAl Viro * @size: number of bytes to zap 13738a5f14a2SKirill A. Shutemov * @details: details of shared cache invalidation 1374f5cc4eefSAl Viro * 1375f5cc4eefSAl Viro * The range must fit into one VMA. 1376f5cc4eefSAl Viro */ 1377f5cc4eefSAl Viro static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address, 1378f5cc4eefSAl Viro unsigned long size, struct zap_details *details) 1379f5cc4eefSAl Viro { 1380f5cc4eefSAl Viro struct mm_struct *mm = vma->vm_mm; 1381f5cc4eefSAl Viro struct mmu_gather tlb; 1382f5cc4eefSAl Viro unsigned long end = address + size; 1383f5cc4eefSAl Viro 1384f5cc4eefSAl Viro lru_add_drain(); 13852b047252SLinus Torvalds tlb_gather_mmu(&tlb, mm, address, end); 1386f5cc4eefSAl Viro update_hiwater_rss(mm); 1387f5cc4eefSAl Viro mmu_notifier_invalidate_range_start(mm, address, end); 13884f74d2c8SLinus Torvalds unmap_single_vma(&tlb, vma, address, end, details); 1389f5cc4eefSAl Viro mmu_notifier_invalidate_range_end(mm, address, end); 1390f5cc4eefSAl Viro tlb_finish_mmu(&tlb, address, end); 13911da177e4SLinus Torvalds } 13921da177e4SLinus Torvalds 1393c627f9ccSJack Steiner /** 1394c627f9ccSJack Steiner * zap_vma_ptes - remove ptes mapping the vma 1395c627f9ccSJack Steiner * @vma: vm_area_struct holding ptes to be zapped 1396c627f9ccSJack Steiner * @address: starting address of pages to zap 1397c627f9ccSJack Steiner * @size: number of bytes to zap 1398c627f9ccSJack Steiner * 1399c627f9ccSJack Steiner * This function only unmaps ptes assigned to VM_PFNMAP vmas. 1400c627f9ccSJack Steiner * 1401c627f9ccSJack Steiner * The entire address range must be fully contained within the vma. 1402c627f9ccSJack Steiner * 1403c627f9ccSJack Steiner */ 140427d036e3SLeon Romanovsky void zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, 1405c627f9ccSJack Steiner unsigned long size) 1406c627f9ccSJack Steiner { 1407c627f9ccSJack Steiner if (address < vma->vm_start || address + size > vma->vm_end || 1408c627f9ccSJack Steiner !(vma->vm_flags & VM_PFNMAP)) 140927d036e3SLeon Romanovsky return; 141027d036e3SLeon Romanovsky 1411f5cc4eefSAl Viro zap_page_range_single(vma, address, size, NULL); 1412c627f9ccSJack Steiner } 1413c627f9ccSJack Steiner EXPORT_SYMBOL_GPL(zap_vma_ptes); 1414c627f9ccSJack Steiner 141525ca1d6cSNamhyung Kim pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr, 1416920c7a5dSHarvey Harrison spinlock_t **ptl) 1417c9cfcddfSLinus Torvalds { 1418c2febafcSKirill A. Shutemov pgd_t *pgd; 1419c2febafcSKirill A. Shutemov p4d_t *p4d; 1420c2febafcSKirill A. Shutemov pud_t *pud; 1421c2febafcSKirill A. Shutemov pmd_t *pmd; 1422c2febafcSKirill A. Shutemov 1423c2febafcSKirill A. Shutemov pgd = pgd_offset(mm, addr); 1424c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, addr); 1425c2febafcSKirill A. Shutemov if (!p4d) 1426c2febafcSKirill A. Shutemov return NULL; 1427c2febafcSKirill A. Shutemov pud = pud_alloc(mm, p4d, addr); 1428c2febafcSKirill A. Shutemov if (!pud) 1429c2febafcSKirill A. Shutemov return NULL; 1430c2febafcSKirill A. Shutemov pmd = pmd_alloc(mm, pud, addr); 1431c2febafcSKirill A. Shutemov if (!pmd) 1432c2febafcSKirill A. Shutemov return NULL; 1433c2febafcSKirill A. Shutemov 1434f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 1435c9cfcddfSLinus Torvalds return pte_alloc_map_lock(mm, pmd, addr, ptl); 1436c9cfcddfSLinus Torvalds } 1437c9cfcddfSLinus Torvalds 14381da177e4SLinus Torvalds /* 1439238f58d8SLinus Torvalds * This is the old fallback for page remapping. 1440238f58d8SLinus Torvalds * 1441238f58d8SLinus Torvalds * For historical reasons, it only allows reserved pages. Only 1442238f58d8SLinus Torvalds * old drivers should use this, and they needed to mark their 1443238f58d8SLinus Torvalds * pages reserved for the old functions anyway. 1444238f58d8SLinus Torvalds */ 1445423bad60SNick Piggin static int insert_page(struct vm_area_struct *vma, unsigned long addr, 1446423bad60SNick Piggin struct page *page, pgprot_t prot) 1447238f58d8SLinus Torvalds { 1448423bad60SNick Piggin struct mm_struct *mm = vma->vm_mm; 1449238f58d8SLinus Torvalds int retval; 1450238f58d8SLinus Torvalds pte_t *pte; 1451238f58d8SLinus Torvalds spinlock_t *ptl; 1452238f58d8SLinus Torvalds 1453238f58d8SLinus Torvalds retval = -EINVAL; 1454a145dd41SLinus Torvalds if (PageAnon(page)) 14555b4e655eSKAMEZAWA Hiroyuki goto out; 1456238f58d8SLinus Torvalds retval = -ENOMEM; 1457238f58d8SLinus Torvalds flush_dcache_page(page); 1458c9cfcddfSLinus Torvalds pte = get_locked_pte(mm, addr, &ptl); 1459238f58d8SLinus Torvalds if (!pte) 14605b4e655eSKAMEZAWA Hiroyuki goto out; 1461238f58d8SLinus Torvalds retval = -EBUSY; 1462238f58d8SLinus Torvalds if (!pte_none(*pte)) 1463238f58d8SLinus Torvalds goto out_unlock; 1464238f58d8SLinus Torvalds 1465238f58d8SLinus Torvalds /* Ok, finally just insert the thing.. */ 1466238f58d8SLinus Torvalds get_page(page); 1467eca56ff9SJerome Marchand inc_mm_counter_fast(mm, mm_counter_file(page)); 1468dd78feddSKirill A. Shutemov page_add_file_rmap(page, false); 1469238f58d8SLinus Torvalds set_pte_at(mm, addr, pte, mk_pte(page, prot)); 1470238f58d8SLinus Torvalds 1471238f58d8SLinus Torvalds retval = 0; 14728a9f3ccdSBalbir Singh pte_unmap_unlock(pte, ptl); 14738a9f3ccdSBalbir Singh return retval; 1474238f58d8SLinus Torvalds out_unlock: 1475238f58d8SLinus Torvalds pte_unmap_unlock(pte, ptl); 1476238f58d8SLinus Torvalds out: 1477238f58d8SLinus Torvalds return retval; 1478238f58d8SLinus Torvalds } 1479238f58d8SLinus Torvalds 1480bfa5bf6dSRolf Eike Beer /** 1481bfa5bf6dSRolf Eike Beer * vm_insert_page - insert single page into user vma 1482bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 1483bfa5bf6dSRolf Eike Beer * @addr: target user address of this page 1484bfa5bf6dSRolf Eike Beer * @page: source kernel page 1485bfa5bf6dSRolf Eike Beer * 1486a145dd41SLinus Torvalds * This allows drivers to insert individual pages they've allocated 1487a145dd41SLinus Torvalds * into a user vma. 1488a145dd41SLinus Torvalds * 1489a145dd41SLinus Torvalds * The page has to be a nice clean _individual_ kernel allocation. 1490a145dd41SLinus Torvalds * If you allocate a compound page, you need to have marked it as 1491a145dd41SLinus Torvalds * such (__GFP_COMP), or manually just split the page up yourself 14928dfcc9baSNick Piggin * (see split_page()). 1493a145dd41SLinus Torvalds * 1494a145dd41SLinus Torvalds * NOTE! Traditionally this was done with "remap_pfn_range()" which 1495a145dd41SLinus Torvalds * took an arbitrary page protection parameter. This doesn't allow 1496a145dd41SLinus Torvalds * that. Your vma protection will have to be set up correctly, which 1497a145dd41SLinus Torvalds * means that if you want a shared writable mapping, you'd better 1498a145dd41SLinus Torvalds * ask for a shared writable mapping! 1499a145dd41SLinus Torvalds * 1500a145dd41SLinus Torvalds * The page does not need to be reserved. 15014b6e1e37SKonstantin Khlebnikov * 15024b6e1e37SKonstantin Khlebnikov * Usually this function is called from f_op->mmap() handler 15034b6e1e37SKonstantin Khlebnikov * under mm->mmap_sem write-lock, so it can change vma->vm_flags. 15044b6e1e37SKonstantin Khlebnikov * Caller must set VM_MIXEDMAP on vma if it wants to call this 15054b6e1e37SKonstantin Khlebnikov * function from other places, for example from page-fault handler. 1506a145dd41SLinus Torvalds */ 1507423bad60SNick Piggin int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, 1508423bad60SNick Piggin struct page *page) 1509a145dd41SLinus Torvalds { 1510a145dd41SLinus Torvalds if (addr < vma->vm_start || addr >= vma->vm_end) 1511a145dd41SLinus Torvalds return -EFAULT; 1512a145dd41SLinus Torvalds if (!page_count(page)) 1513a145dd41SLinus Torvalds return -EINVAL; 15144b6e1e37SKonstantin Khlebnikov if (!(vma->vm_flags & VM_MIXEDMAP)) { 15154b6e1e37SKonstantin Khlebnikov BUG_ON(down_read_trylock(&vma->vm_mm->mmap_sem)); 15164b6e1e37SKonstantin Khlebnikov BUG_ON(vma->vm_flags & VM_PFNMAP); 15174b6e1e37SKonstantin Khlebnikov vma->vm_flags |= VM_MIXEDMAP; 15184b6e1e37SKonstantin Khlebnikov } 1519423bad60SNick Piggin return insert_page(vma, addr, page, vma->vm_page_prot); 1520a145dd41SLinus Torvalds } 1521e3c3374fSLinus Torvalds EXPORT_SYMBOL(vm_insert_page); 1522a145dd41SLinus Torvalds 1523*9b5a8e00SMatthew Wilcox static vm_fault_t insert_pfn(struct vm_area_struct *vma, unsigned long addr, 1524b2770da6SRoss Zwisler pfn_t pfn, pgprot_t prot, bool mkwrite) 1525423bad60SNick Piggin { 1526423bad60SNick Piggin struct mm_struct *mm = vma->vm_mm; 1527423bad60SNick Piggin pte_t *pte, entry; 1528423bad60SNick Piggin spinlock_t *ptl; 1529423bad60SNick Piggin 1530423bad60SNick Piggin pte = get_locked_pte(mm, addr, &ptl); 1531423bad60SNick Piggin if (!pte) 1532*9b5a8e00SMatthew Wilcox return VM_FAULT_OOM; 1533b2770da6SRoss Zwisler if (!pte_none(*pte)) { 1534b2770da6SRoss Zwisler if (mkwrite) { 1535b2770da6SRoss Zwisler /* 1536b2770da6SRoss Zwisler * For read faults on private mappings the PFN passed 1537b2770da6SRoss Zwisler * in may not match the PFN we have mapped if the 1538b2770da6SRoss Zwisler * mapped PFN is a writeable COW page. In the mkwrite 1539b2770da6SRoss Zwisler * case we are creating a writable PTE for a shared 1540b2770da6SRoss Zwisler * mapping and we expect the PFNs to match. 1541b2770da6SRoss Zwisler */ 1542b2770da6SRoss Zwisler if (WARN_ON_ONCE(pte_pfn(*pte) != pfn_t_to_pfn(pfn))) 1543423bad60SNick Piggin goto out_unlock; 1544b2770da6SRoss Zwisler entry = *pte; 1545b2770da6SRoss Zwisler goto out_mkwrite; 1546b2770da6SRoss Zwisler } else 1547b2770da6SRoss Zwisler goto out_unlock; 1548b2770da6SRoss Zwisler } 1549423bad60SNick Piggin 1550423bad60SNick Piggin /* Ok, finally just insert the thing.. */ 155101c8f1c4SDan Williams if (pfn_t_devmap(pfn)) 155201c8f1c4SDan Williams entry = pte_mkdevmap(pfn_t_pte(pfn, prot)); 155301c8f1c4SDan Williams else 155401c8f1c4SDan Williams entry = pte_mkspecial(pfn_t_pte(pfn, prot)); 1555b2770da6SRoss Zwisler 1556b2770da6SRoss Zwisler out_mkwrite: 1557b2770da6SRoss Zwisler if (mkwrite) { 1558b2770da6SRoss Zwisler entry = pte_mkyoung(entry); 1559b2770da6SRoss Zwisler entry = maybe_mkwrite(pte_mkdirty(entry), vma); 1560b2770da6SRoss Zwisler } 1561b2770da6SRoss Zwisler 1562423bad60SNick Piggin set_pte_at(mm, addr, pte, entry); 15634b3073e1SRussell King update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */ 1564423bad60SNick Piggin 1565423bad60SNick Piggin out_unlock: 1566423bad60SNick Piggin pte_unmap_unlock(pte, ptl); 1567*9b5a8e00SMatthew Wilcox return VM_FAULT_NOPAGE; 1568423bad60SNick Piggin } 1569423bad60SNick Piggin 1570f5e6d1d5SMatthew Wilcox /** 1571f5e6d1d5SMatthew Wilcox * vmf_insert_pfn_prot - insert single pfn into user vma with specified pgprot 1572f5e6d1d5SMatthew Wilcox * @vma: user vma to map to 1573f5e6d1d5SMatthew Wilcox * @addr: target user address of this page 1574f5e6d1d5SMatthew Wilcox * @pfn: source kernel pfn 1575f5e6d1d5SMatthew Wilcox * @pgprot: pgprot flags for the inserted page 1576f5e6d1d5SMatthew Wilcox * 1577f5e6d1d5SMatthew Wilcox * This is exactly like vmf_insert_pfn(), except that it allows drivers to 1578f5e6d1d5SMatthew Wilcox * to override pgprot on a per-page basis. 1579f5e6d1d5SMatthew Wilcox * 1580f5e6d1d5SMatthew Wilcox * This only makes sense for IO mappings, and it makes no sense for 1581f5e6d1d5SMatthew Wilcox * COW mappings. In general, using multiple vmas is preferable; 1582ae2b01f3SMatthew Wilcox * vmf_insert_pfn_prot should only be used if using multiple VMAs is 1583f5e6d1d5SMatthew Wilcox * impractical. 1584f5e6d1d5SMatthew Wilcox * 1585ae2b01f3SMatthew Wilcox * Context: Process context. May allocate using %GFP_KERNEL. 1586f5e6d1d5SMatthew Wilcox * Return: vm_fault_t value. 1587f5e6d1d5SMatthew Wilcox */ 1588f5e6d1d5SMatthew Wilcox vm_fault_t vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr, 1589f5e6d1d5SMatthew Wilcox unsigned long pfn, pgprot_t pgprot) 1590f5e6d1d5SMatthew Wilcox { 15916d958546SMatthew Wilcox /* 15926d958546SMatthew Wilcox * Technically, architectures with pte_special can avoid all these 15936d958546SMatthew Wilcox * restrictions (same for remap_pfn_range). However we would like 15946d958546SMatthew Wilcox * consistency in testing and feature parity among all, so we should 15956d958546SMatthew Wilcox * try to keep these invariants in place for everybody. 15966d958546SMatthew Wilcox */ 15976d958546SMatthew Wilcox BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))); 15986d958546SMatthew Wilcox BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) == 15996d958546SMatthew Wilcox (VM_PFNMAP|VM_MIXEDMAP)); 16006d958546SMatthew Wilcox BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); 16016d958546SMatthew Wilcox BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn)); 16026d958546SMatthew Wilcox 16036d958546SMatthew Wilcox if (addr < vma->vm_start || addr >= vma->vm_end) 16046d958546SMatthew Wilcox return VM_FAULT_SIGBUS; 16056d958546SMatthew Wilcox 16066d958546SMatthew Wilcox if (!pfn_modify_allowed(pfn, pgprot)) 16076d958546SMatthew Wilcox return VM_FAULT_SIGBUS; 16086d958546SMatthew Wilcox 16096d958546SMatthew Wilcox track_pfn_insert(vma, &pgprot, __pfn_to_pfn_t(pfn, PFN_DEV)); 16106d958546SMatthew Wilcox 1611*9b5a8e00SMatthew Wilcox return insert_pfn(vma, addr, __pfn_to_pfn_t(pfn, PFN_DEV), pgprot, 16126d958546SMatthew Wilcox false); 1613f5e6d1d5SMatthew Wilcox } 1614f5e6d1d5SMatthew Wilcox EXPORT_SYMBOL(vmf_insert_pfn_prot); 1615e0dc0d8fSNick Piggin 1616ae2b01f3SMatthew Wilcox /** 1617ae2b01f3SMatthew Wilcox * vmf_insert_pfn - insert single pfn into user vma 1618ae2b01f3SMatthew Wilcox * @vma: user vma to map to 1619ae2b01f3SMatthew Wilcox * @addr: target user address of this page 1620ae2b01f3SMatthew Wilcox * @pfn: source kernel pfn 1621ae2b01f3SMatthew Wilcox * 1622ae2b01f3SMatthew Wilcox * Similar to vm_insert_page, this allows drivers to insert individual pages 1623ae2b01f3SMatthew Wilcox * they've allocated into a user vma. Same comments apply. 1624ae2b01f3SMatthew Wilcox * 1625ae2b01f3SMatthew Wilcox * This function should only be called from a vm_ops->fault handler, and 1626ae2b01f3SMatthew Wilcox * in that case the handler should return the result of this function. 1627ae2b01f3SMatthew Wilcox * 1628ae2b01f3SMatthew Wilcox * vma cannot be a COW mapping. 1629ae2b01f3SMatthew Wilcox * 1630ae2b01f3SMatthew Wilcox * As this is called only for pages that do not currently exist, we 1631ae2b01f3SMatthew Wilcox * do not need to flush old virtual caches or the TLB. 1632ae2b01f3SMatthew Wilcox * 1633ae2b01f3SMatthew Wilcox * Context: Process context. May allocate using %GFP_KERNEL. 1634ae2b01f3SMatthew Wilcox * Return: vm_fault_t value. 1635ae2b01f3SMatthew Wilcox */ 1636ae2b01f3SMatthew Wilcox vm_fault_t vmf_insert_pfn(struct vm_area_struct *vma, unsigned long addr, 1637ae2b01f3SMatthew Wilcox unsigned long pfn) 1638ae2b01f3SMatthew Wilcox { 1639ae2b01f3SMatthew Wilcox return vmf_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot); 1640ae2b01f3SMatthew Wilcox } 1641ae2b01f3SMatthew Wilcox EXPORT_SYMBOL(vmf_insert_pfn); 1642ae2b01f3SMatthew Wilcox 1643785a3fabSDan Williams static bool vm_mixed_ok(struct vm_area_struct *vma, pfn_t pfn) 1644785a3fabSDan Williams { 1645785a3fabSDan Williams /* these checks mirror the abort conditions in vm_normal_page */ 1646785a3fabSDan Williams if (vma->vm_flags & VM_MIXEDMAP) 1647785a3fabSDan Williams return true; 1648785a3fabSDan Williams if (pfn_t_devmap(pfn)) 1649785a3fabSDan Williams return true; 1650785a3fabSDan Williams if (pfn_t_special(pfn)) 1651785a3fabSDan Williams return true; 1652785a3fabSDan Williams if (is_zero_pfn(pfn_t_to_pfn(pfn))) 1653785a3fabSDan Williams return true; 1654785a3fabSDan Williams return false; 1655785a3fabSDan Williams } 1656785a3fabSDan Williams 165779f3aa5bSMatthew Wilcox static vm_fault_t __vm_insert_mixed(struct vm_area_struct *vma, 165879f3aa5bSMatthew Wilcox unsigned long addr, pfn_t pfn, bool mkwrite) 1659423bad60SNick Piggin { 166087744ab3SDan Williams pgprot_t pgprot = vma->vm_page_prot; 166179f3aa5bSMatthew Wilcox int err; 166287744ab3SDan Williams 1663785a3fabSDan Williams BUG_ON(!vm_mixed_ok(vma, pfn)); 1664423bad60SNick Piggin 1665423bad60SNick Piggin if (addr < vma->vm_start || addr >= vma->vm_end) 166679f3aa5bSMatthew Wilcox return VM_FAULT_SIGBUS; 1667308a047cSBorislav Petkov 1668308a047cSBorislav Petkov track_pfn_insert(vma, &pgprot, pfn); 1669423bad60SNick Piggin 167042e4089cSAndi Kleen if (!pfn_modify_allowed(pfn_t_to_pfn(pfn), pgprot)) 167179f3aa5bSMatthew Wilcox return VM_FAULT_SIGBUS; 167242e4089cSAndi Kleen 1673423bad60SNick Piggin /* 1674423bad60SNick Piggin * If we don't have pte special, then we have to use the pfn_valid() 1675423bad60SNick Piggin * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must* 1676423bad60SNick Piggin * refcount the page if pfn_valid is true (hence insert_page rather 167762eede62SHugh Dickins * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP 167862eede62SHugh Dickins * without pte special, it would there be refcounted as a normal page. 1679423bad60SNick Piggin */ 168000b3a331SLaurent Dufour if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) && 168100b3a331SLaurent Dufour !pfn_t_devmap(pfn) && pfn_t_valid(pfn)) { 1682423bad60SNick Piggin struct page *page; 1683423bad60SNick Piggin 168403fc2da6SDan Williams /* 168503fc2da6SDan Williams * At this point we are committed to insert_page() 168603fc2da6SDan Williams * regardless of whether the caller specified flags that 168703fc2da6SDan Williams * result in pfn_t_has_page() == false. 168803fc2da6SDan Williams */ 168903fc2da6SDan Williams page = pfn_to_page(pfn_t_to_pfn(pfn)); 169079f3aa5bSMatthew Wilcox err = insert_page(vma, addr, page, pgprot); 169179f3aa5bSMatthew Wilcox } else { 1692*9b5a8e00SMatthew Wilcox return insert_pfn(vma, addr, pfn, pgprot, mkwrite); 1693423bad60SNick Piggin } 1694b2770da6SRoss Zwisler 16955d747637SMatthew Wilcox if (err == -ENOMEM) 16965d747637SMatthew Wilcox return VM_FAULT_OOM; 16975d747637SMatthew Wilcox if (err < 0 && err != -EBUSY) 16985d747637SMatthew Wilcox return VM_FAULT_SIGBUS; 16995d747637SMatthew Wilcox 17005d747637SMatthew Wilcox return VM_FAULT_NOPAGE; 1701423bad60SNick Piggin } 170279f3aa5bSMatthew Wilcox 170379f3aa5bSMatthew Wilcox vm_fault_t vmf_insert_mixed(struct vm_area_struct *vma, unsigned long addr, 170479f3aa5bSMatthew Wilcox pfn_t pfn) 170579f3aa5bSMatthew Wilcox { 170679f3aa5bSMatthew Wilcox return __vm_insert_mixed(vma, addr, pfn, false); 170779f3aa5bSMatthew Wilcox } 17085d747637SMatthew Wilcox EXPORT_SYMBOL(vmf_insert_mixed); 1709423bad60SNick Piggin 1710ab77dab4SSouptick Joarder /* 1711ab77dab4SSouptick Joarder * If the insertion of PTE failed because someone else already added a 1712ab77dab4SSouptick Joarder * different entry in the mean time, we treat that as success as we assume 1713ab77dab4SSouptick Joarder * the same entry was actually inserted. 1714ab77dab4SSouptick Joarder */ 1715ab77dab4SSouptick Joarder vm_fault_t vmf_insert_mixed_mkwrite(struct vm_area_struct *vma, 1716ab77dab4SSouptick Joarder unsigned long addr, pfn_t pfn) 1717b2770da6SRoss Zwisler { 171879f3aa5bSMatthew Wilcox return __vm_insert_mixed(vma, addr, pfn, true); 1719b2770da6SRoss Zwisler } 1720ab77dab4SSouptick Joarder EXPORT_SYMBOL(vmf_insert_mixed_mkwrite); 1721b2770da6SRoss Zwisler 1722a145dd41SLinus Torvalds /* 17231da177e4SLinus Torvalds * maps a range of physical memory into the requested pages. the old 17241da177e4SLinus Torvalds * mappings are removed. any references to nonexistent pages results 17251da177e4SLinus Torvalds * in null mappings (currently treated as "copy-on-access") 17261da177e4SLinus Torvalds */ 17271da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd, 17281da177e4SLinus Torvalds unsigned long addr, unsigned long end, 17291da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 17301da177e4SLinus Torvalds { 17311da177e4SLinus Torvalds pte_t *pte; 1732c74df32cSHugh Dickins spinlock_t *ptl; 173342e4089cSAndi Kleen int err = 0; 17341da177e4SLinus Torvalds 1735c74df32cSHugh Dickins pte = pte_alloc_map_lock(mm, pmd, addr, &ptl); 17361da177e4SLinus Torvalds if (!pte) 17371da177e4SLinus Torvalds return -ENOMEM; 17386606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 17391da177e4SLinus Torvalds do { 17401da177e4SLinus Torvalds BUG_ON(!pte_none(*pte)); 174142e4089cSAndi Kleen if (!pfn_modify_allowed(pfn, prot)) { 174242e4089cSAndi Kleen err = -EACCES; 174342e4089cSAndi Kleen break; 174442e4089cSAndi Kleen } 17457e675137SNick Piggin set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot))); 17461da177e4SLinus Torvalds pfn++; 17471da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 17486606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 1749c74df32cSHugh Dickins pte_unmap_unlock(pte - 1, ptl); 175042e4089cSAndi Kleen return err; 17511da177e4SLinus Torvalds } 17521da177e4SLinus Torvalds 17531da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud, 17541da177e4SLinus Torvalds unsigned long addr, unsigned long end, 17551da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 17561da177e4SLinus Torvalds { 17571da177e4SLinus Torvalds pmd_t *pmd; 17581da177e4SLinus Torvalds unsigned long next; 175942e4089cSAndi Kleen int err; 17601da177e4SLinus Torvalds 17611da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 17621da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, addr); 17631da177e4SLinus Torvalds if (!pmd) 17641da177e4SLinus Torvalds return -ENOMEM; 1765f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 17661da177e4SLinus Torvalds do { 17671da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 176842e4089cSAndi Kleen err = remap_pte_range(mm, pmd, addr, next, 176942e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 177042e4089cSAndi Kleen if (err) 177142e4089cSAndi Kleen return err; 17721da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 17731da177e4SLinus Torvalds return 0; 17741da177e4SLinus Torvalds } 17751da177e4SLinus Torvalds 1776c2febafcSKirill A. Shutemov static inline int remap_pud_range(struct mm_struct *mm, p4d_t *p4d, 17771da177e4SLinus Torvalds unsigned long addr, unsigned long end, 17781da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 17791da177e4SLinus Torvalds { 17801da177e4SLinus Torvalds pud_t *pud; 17811da177e4SLinus Torvalds unsigned long next; 178242e4089cSAndi Kleen int err; 17831da177e4SLinus Torvalds 17841da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 1785c2febafcSKirill A. Shutemov pud = pud_alloc(mm, p4d, addr); 17861da177e4SLinus Torvalds if (!pud) 17871da177e4SLinus Torvalds return -ENOMEM; 17881da177e4SLinus Torvalds do { 17891da177e4SLinus Torvalds next = pud_addr_end(addr, end); 179042e4089cSAndi Kleen err = remap_pmd_range(mm, pud, addr, next, 179142e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 179242e4089cSAndi Kleen if (err) 179342e4089cSAndi Kleen return err; 17941da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 17951da177e4SLinus Torvalds return 0; 17961da177e4SLinus Torvalds } 17971da177e4SLinus Torvalds 1798c2febafcSKirill A. Shutemov static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd, 1799c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 1800c2febafcSKirill A. Shutemov unsigned long pfn, pgprot_t prot) 1801c2febafcSKirill A. Shutemov { 1802c2febafcSKirill A. Shutemov p4d_t *p4d; 1803c2febafcSKirill A. Shutemov unsigned long next; 180442e4089cSAndi Kleen int err; 1805c2febafcSKirill A. Shutemov 1806c2febafcSKirill A. Shutemov pfn -= addr >> PAGE_SHIFT; 1807c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, addr); 1808c2febafcSKirill A. Shutemov if (!p4d) 1809c2febafcSKirill A. Shutemov return -ENOMEM; 1810c2febafcSKirill A. Shutemov do { 1811c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 181242e4089cSAndi Kleen err = remap_pud_range(mm, p4d, addr, next, 181342e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 181442e4089cSAndi Kleen if (err) 181542e4089cSAndi Kleen return err; 1816c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 1817c2febafcSKirill A. Shutemov return 0; 1818c2febafcSKirill A. Shutemov } 1819c2febafcSKirill A. Shutemov 1820bfa5bf6dSRolf Eike Beer /** 1821bfa5bf6dSRolf Eike Beer * remap_pfn_range - remap kernel memory to userspace 1822bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 1823bfa5bf6dSRolf Eike Beer * @addr: target user address to start at 1824bfa5bf6dSRolf Eike Beer * @pfn: physical address of kernel memory 1825bfa5bf6dSRolf Eike Beer * @size: size of map area 1826bfa5bf6dSRolf Eike Beer * @prot: page protection flags for this mapping 1827bfa5bf6dSRolf Eike Beer * 1828bfa5bf6dSRolf Eike Beer * Note: this is only safe if the mm semaphore is held when called. 1829bfa5bf6dSRolf Eike Beer */ 18301da177e4SLinus Torvalds int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 18311da177e4SLinus Torvalds unsigned long pfn, unsigned long size, pgprot_t prot) 18321da177e4SLinus Torvalds { 18331da177e4SLinus Torvalds pgd_t *pgd; 18341da177e4SLinus Torvalds unsigned long next; 18352d15cab8SHugh Dickins unsigned long end = addr + PAGE_ALIGN(size); 18361da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 1837d5957d2fSYongji Xie unsigned long remap_pfn = pfn; 18381da177e4SLinus Torvalds int err; 18391da177e4SLinus Torvalds 18401da177e4SLinus Torvalds /* 18411da177e4SLinus Torvalds * Physically remapped pages are special. Tell the 18421da177e4SLinus Torvalds * rest of the world about it: 18431da177e4SLinus Torvalds * VM_IO tells people not to look at these pages 18441da177e4SLinus Torvalds * (accesses can have side effects). 18456aab341eSLinus Torvalds * VM_PFNMAP tells the core MM that the base pages are just 18466aab341eSLinus Torvalds * raw PFN mappings, and do not have a "struct page" associated 18476aab341eSLinus Torvalds * with them. 1848314e51b9SKonstantin Khlebnikov * VM_DONTEXPAND 1849314e51b9SKonstantin Khlebnikov * Disable vma merging and expanding with mremap(). 1850314e51b9SKonstantin Khlebnikov * VM_DONTDUMP 1851314e51b9SKonstantin Khlebnikov * Omit vma from core dump, even when VM_IO turned off. 1852fb155c16SLinus Torvalds * 1853fb155c16SLinus Torvalds * There's a horrible special case to handle copy-on-write 1854fb155c16SLinus Torvalds * behaviour that some programs depend on. We mark the "original" 1855fb155c16SLinus Torvalds * un-COW'ed pages by matching them up with "vma->vm_pgoff". 1856b3b9c293SKonstantin Khlebnikov * See vm_normal_page() for details. 18571da177e4SLinus Torvalds */ 1858b3b9c293SKonstantin Khlebnikov if (is_cow_mapping(vma->vm_flags)) { 1859b3b9c293SKonstantin Khlebnikov if (addr != vma->vm_start || end != vma->vm_end) 1860b3b9c293SKonstantin Khlebnikov return -EINVAL; 18616aab341eSLinus Torvalds vma->vm_pgoff = pfn; 1862b3b9c293SKonstantin Khlebnikov } 1863b3b9c293SKonstantin Khlebnikov 1864d5957d2fSYongji Xie err = track_pfn_remap(vma, &prot, remap_pfn, addr, PAGE_ALIGN(size)); 1865b3b9c293SKonstantin Khlebnikov if (err) 18663c8bb73aSvenkatesh.pallipadi@intel.com return -EINVAL; 1867fb155c16SLinus Torvalds 1868314e51b9SKonstantin Khlebnikov vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; 18691da177e4SLinus Torvalds 18701da177e4SLinus Torvalds BUG_ON(addr >= end); 18711da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 18721da177e4SLinus Torvalds pgd = pgd_offset(mm, addr); 18731da177e4SLinus Torvalds flush_cache_range(vma, addr, end); 18741da177e4SLinus Torvalds do { 18751da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 1876c2febafcSKirill A. Shutemov err = remap_p4d_range(mm, pgd, addr, next, 18771da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot); 18781da177e4SLinus Torvalds if (err) 18791da177e4SLinus Torvalds break; 18801da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 18812ab64037Svenkatesh.pallipadi@intel.com 18822ab64037Svenkatesh.pallipadi@intel.com if (err) 1883d5957d2fSYongji Xie untrack_pfn(vma, remap_pfn, PAGE_ALIGN(size)); 18842ab64037Svenkatesh.pallipadi@intel.com 18851da177e4SLinus Torvalds return err; 18861da177e4SLinus Torvalds } 18871da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range); 18881da177e4SLinus Torvalds 1889b4cbb197SLinus Torvalds /** 1890b4cbb197SLinus Torvalds * vm_iomap_memory - remap memory to userspace 1891b4cbb197SLinus Torvalds * @vma: user vma to map to 1892b4cbb197SLinus Torvalds * @start: start of area 1893b4cbb197SLinus Torvalds * @len: size of area 1894b4cbb197SLinus Torvalds * 1895b4cbb197SLinus Torvalds * This is a simplified io_remap_pfn_range() for common driver use. The 1896b4cbb197SLinus Torvalds * driver just needs to give us the physical memory range to be mapped, 1897b4cbb197SLinus Torvalds * we'll figure out the rest from the vma information. 1898b4cbb197SLinus Torvalds * 1899b4cbb197SLinus Torvalds * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get 1900b4cbb197SLinus Torvalds * whatever write-combining details or similar. 1901b4cbb197SLinus Torvalds */ 1902b4cbb197SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len) 1903b4cbb197SLinus Torvalds { 1904b4cbb197SLinus Torvalds unsigned long vm_len, pfn, pages; 1905b4cbb197SLinus Torvalds 1906b4cbb197SLinus Torvalds /* Check that the physical memory area passed in looks valid */ 1907b4cbb197SLinus Torvalds if (start + len < start) 1908b4cbb197SLinus Torvalds return -EINVAL; 1909b4cbb197SLinus Torvalds /* 1910b4cbb197SLinus Torvalds * You *really* shouldn't map things that aren't page-aligned, 1911b4cbb197SLinus Torvalds * but we've historically allowed it because IO memory might 1912b4cbb197SLinus Torvalds * just have smaller alignment. 1913b4cbb197SLinus Torvalds */ 1914b4cbb197SLinus Torvalds len += start & ~PAGE_MASK; 1915b4cbb197SLinus Torvalds pfn = start >> PAGE_SHIFT; 1916b4cbb197SLinus Torvalds pages = (len + ~PAGE_MASK) >> PAGE_SHIFT; 1917b4cbb197SLinus Torvalds if (pfn + pages < pfn) 1918b4cbb197SLinus Torvalds return -EINVAL; 1919b4cbb197SLinus Torvalds 1920b4cbb197SLinus Torvalds /* We start the mapping 'vm_pgoff' pages into the area */ 1921b4cbb197SLinus Torvalds if (vma->vm_pgoff > pages) 1922b4cbb197SLinus Torvalds return -EINVAL; 1923b4cbb197SLinus Torvalds pfn += vma->vm_pgoff; 1924b4cbb197SLinus Torvalds pages -= vma->vm_pgoff; 1925b4cbb197SLinus Torvalds 1926b4cbb197SLinus Torvalds /* Can we fit all of the mapping? */ 1927b4cbb197SLinus Torvalds vm_len = vma->vm_end - vma->vm_start; 1928b4cbb197SLinus Torvalds if (vm_len >> PAGE_SHIFT > pages) 1929b4cbb197SLinus Torvalds return -EINVAL; 1930b4cbb197SLinus Torvalds 1931b4cbb197SLinus Torvalds /* Ok, let it rip */ 1932b4cbb197SLinus Torvalds return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot); 1933b4cbb197SLinus Torvalds } 1934b4cbb197SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory); 1935b4cbb197SLinus Torvalds 1936aee16b3cSJeremy Fitzhardinge static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd, 1937aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 1938aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 1939aee16b3cSJeremy Fitzhardinge { 1940aee16b3cSJeremy Fitzhardinge pte_t *pte; 1941aee16b3cSJeremy Fitzhardinge int err; 19422f569afdSMartin Schwidefsky pgtable_t token; 194394909914SBorislav Petkov spinlock_t *uninitialized_var(ptl); 1944aee16b3cSJeremy Fitzhardinge 1945aee16b3cSJeremy Fitzhardinge pte = (mm == &init_mm) ? 1946aee16b3cSJeremy Fitzhardinge pte_alloc_kernel(pmd, addr) : 1947aee16b3cSJeremy Fitzhardinge pte_alloc_map_lock(mm, pmd, addr, &ptl); 1948aee16b3cSJeremy Fitzhardinge if (!pte) 1949aee16b3cSJeremy Fitzhardinge return -ENOMEM; 1950aee16b3cSJeremy Fitzhardinge 1951aee16b3cSJeremy Fitzhardinge BUG_ON(pmd_huge(*pmd)); 1952aee16b3cSJeremy Fitzhardinge 195338e0edb1SJeremy Fitzhardinge arch_enter_lazy_mmu_mode(); 195438e0edb1SJeremy Fitzhardinge 19552f569afdSMartin Schwidefsky token = pmd_pgtable(*pmd); 1956aee16b3cSJeremy Fitzhardinge 1957aee16b3cSJeremy Fitzhardinge do { 1958c36987e2SDaisuke Nishimura err = fn(pte++, token, addr, data); 1959aee16b3cSJeremy Fitzhardinge if (err) 1960aee16b3cSJeremy Fitzhardinge break; 1961c36987e2SDaisuke Nishimura } while (addr += PAGE_SIZE, addr != end); 1962aee16b3cSJeremy Fitzhardinge 196338e0edb1SJeremy Fitzhardinge arch_leave_lazy_mmu_mode(); 196438e0edb1SJeremy Fitzhardinge 1965aee16b3cSJeremy Fitzhardinge if (mm != &init_mm) 1966aee16b3cSJeremy Fitzhardinge pte_unmap_unlock(pte-1, ptl); 1967aee16b3cSJeremy Fitzhardinge return err; 1968aee16b3cSJeremy Fitzhardinge } 1969aee16b3cSJeremy Fitzhardinge 1970aee16b3cSJeremy Fitzhardinge static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud, 1971aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 1972aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 1973aee16b3cSJeremy Fitzhardinge { 1974aee16b3cSJeremy Fitzhardinge pmd_t *pmd; 1975aee16b3cSJeremy Fitzhardinge unsigned long next; 1976aee16b3cSJeremy Fitzhardinge int err; 1977aee16b3cSJeremy Fitzhardinge 1978ceb86879SAndi Kleen BUG_ON(pud_huge(*pud)); 1979ceb86879SAndi Kleen 1980aee16b3cSJeremy Fitzhardinge pmd = pmd_alloc(mm, pud, addr); 1981aee16b3cSJeremy Fitzhardinge if (!pmd) 1982aee16b3cSJeremy Fitzhardinge return -ENOMEM; 1983aee16b3cSJeremy Fitzhardinge do { 1984aee16b3cSJeremy Fitzhardinge next = pmd_addr_end(addr, end); 1985aee16b3cSJeremy Fitzhardinge err = apply_to_pte_range(mm, pmd, addr, next, fn, data); 1986aee16b3cSJeremy Fitzhardinge if (err) 1987aee16b3cSJeremy Fitzhardinge break; 1988aee16b3cSJeremy Fitzhardinge } while (pmd++, addr = next, addr != end); 1989aee16b3cSJeremy Fitzhardinge return err; 1990aee16b3cSJeremy Fitzhardinge } 1991aee16b3cSJeremy Fitzhardinge 1992c2febafcSKirill A. Shutemov static int apply_to_pud_range(struct mm_struct *mm, p4d_t *p4d, 1993aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 1994aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 1995aee16b3cSJeremy Fitzhardinge { 1996aee16b3cSJeremy Fitzhardinge pud_t *pud; 1997aee16b3cSJeremy Fitzhardinge unsigned long next; 1998aee16b3cSJeremy Fitzhardinge int err; 1999aee16b3cSJeremy Fitzhardinge 2000c2febafcSKirill A. Shutemov pud = pud_alloc(mm, p4d, addr); 2001aee16b3cSJeremy Fitzhardinge if (!pud) 2002aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2003aee16b3cSJeremy Fitzhardinge do { 2004aee16b3cSJeremy Fitzhardinge next = pud_addr_end(addr, end); 2005aee16b3cSJeremy Fitzhardinge err = apply_to_pmd_range(mm, pud, addr, next, fn, data); 2006aee16b3cSJeremy Fitzhardinge if (err) 2007aee16b3cSJeremy Fitzhardinge break; 2008aee16b3cSJeremy Fitzhardinge } while (pud++, addr = next, addr != end); 2009aee16b3cSJeremy Fitzhardinge return err; 2010aee16b3cSJeremy Fitzhardinge } 2011aee16b3cSJeremy Fitzhardinge 2012c2febafcSKirill A. Shutemov static int apply_to_p4d_range(struct mm_struct *mm, pgd_t *pgd, 2013c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 2014c2febafcSKirill A. Shutemov pte_fn_t fn, void *data) 2015c2febafcSKirill A. Shutemov { 2016c2febafcSKirill A. Shutemov p4d_t *p4d; 2017c2febafcSKirill A. Shutemov unsigned long next; 2018c2febafcSKirill A. Shutemov int err; 2019c2febafcSKirill A. Shutemov 2020c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, addr); 2021c2febafcSKirill A. Shutemov if (!p4d) 2022c2febafcSKirill A. Shutemov return -ENOMEM; 2023c2febafcSKirill A. Shutemov do { 2024c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 2025c2febafcSKirill A. Shutemov err = apply_to_pud_range(mm, p4d, addr, next, fn, data); 2026c2febafcSKirill A. Shutemov if (err) 2027c2febafcSKirill A. Shutemov break; 2028c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 2029c2febafcSKirill A. Shutemov return err; 2030c2febafcSKirill A. Shutemov } 2031c2febafcSKirill A. Shutemov 2032aee16b3cSJeremy Fitzhardinge /* 2033aee16b3cSJeremy Fitzhardinge * Scan a region of virtual memory, filling in page tables as necessary 2034aee16b3cSJeremy Fitzhardinge * and calling a provided function on each leaf page table. 2035aee16b3cSJeremy Fitzhardinge */ 2036aee16b3cSJeremy Fitzhardinge int apply_to_page_range(struct mm_struct *mm, unsigned long addr, 2037aee16b3cSJeremy Fitzhardinge unsigned long size, pte_fn_t fn, void *data) 2038aee16b3cSJeremy Fitzhardinge { 2039aee16b3cSJeremy Fitzhardinge pgd_t *pgd; 2040aee16b3cSJeremy Fitzhardinge unsigned long next; 204157250a5bSJeremy Fitzhardinge unsigned long end = addr + size; 2042aee16b3cSJeremy Fitzhardinge int err; 2043aee16b3cSJeremy Fitzhardinge 20449cb65bc3SMika Penttilä if (WARN_ON(addr >= end)) 20459cb65bc3SMika Penttilä return -EINVAL; 20469cb65bc3SMika Penttilä 2047aee16b3cSJeremy Fitzhardinge pgd = pgd_offset(mm, addr); 2048aee16b3cSJeremy Fitzhardinge do { 2049aee16b3cSJeremy Fitzhardinge next = pgd_addr_end(addr, end); 2050c2febafcSKirill A. Shutemov err = apply_to_p4d_range(mm, pgd, addr, next, fn, data); 2051aee16b3cSJeremy Fitzhardinge if (err) 2052aee16b3cSJeremy Fitzhardinge break; 2053aee16b3cSJeremy Fitzhardinge } while (pgd++, addr = next, addr != end); 205457250a5bSJeremy Fitzhardinge 2055aee16b3cSJeremy Fitzhardinge return err; 2056aee16b3cSJeremy Fitzhardinge } 2057aee16b3cSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(apply_to_page_range); 2058aee16b3cSJeremy Fitzhardinge 20591da177e4SLinus Torvalds /* 20609b4bdd2fSKirill A. Shutemov * handle_pte_fault chooses page fault handler according to an entry which was 20619b4bdd2fSKirill A. Shutemov * read non-atomically. Before making any commitment, on those architectures 20629b4bdd2fSKirill A. Shutemov * or configurations (e.g. i386 with PAE) which might give a mix of unmatched 20639b4bdd2fSKirill A. Shutemov * parts, do_swap_page must check under lock before unmapping the pte and 20649b4bdd2fSKirill A. Shutemov * proceeding (but do_wp_page is only called after already making such a check; 2065a335b2e1SRyota Ozaki * and do_anonymous_page can safely check later on). 20668f4e2101SHugh Dickins */ 20674c21e2f2SHugh Dickins static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd, 20688f4e2101SHugh Dickins pte_t *page_table, pte_t orig_pte) 20698f4e2101SHugh Dickins { 20708f4e2101SHugh Dickins int same = 1; 20718f4e2101SHugh Dickins #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT) 20728f4e2101SHugh Dickins if (sizeof(pte_t) > sizeof(unsigned long)) { 20734c21e2f2SHugh Dickins spinlock_t *ptl = pte_lockptr(mm, pmd); 20744c21e2f2SHugh Dickins spin_lock(ptl); 20758f4e2101SHugh Dickins same = pte_same(*page_table, orig_pte); 20764c21e2f2SHugh Dickins spin_unlock(ptl); 20778f4e2101SHugh Dickins } 20788f4e2101SHugh Dickins #endif 20798f4e2101SHugh Dickins pte_unmap(page_table); 20808f4e2101SHugh Dickins return same; 20818f4e2101SHugh Dickins } 20828f4e2101SHugh Dickins 20839de455b2SAtsushi Nemoto static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma) 20846aab341eSLinus Torvalds { 20850abdd7a8SDan Williams debug_dma_assert_idle(src); 20860abdd7a8SDan Williams 20876aab341eSLinus Torvalds /* 20886aab341eSLinus Torvalds * If the source page was a PFN mapping, we don't have 20896aab341eSLinus Torvalds * a "struct page" for it. We do a best-effort copy by 20906aab341eSLinus Torvalds * just copying from the original user address. If that 20916aab341eSLinus Torvalds * fails, we just zero-fill it. Live with it. 20926aab341eSLinus Torvalds */ 20936aab341eSLinus Torvalds if (unlikely(!src)) { 20949b04c5feSCong Wang void *kaddr = kmap_atomic(dst); 20955d2a2dbbSLinus Torvalds void __user *uaddr = (void __user *)(va & PAGE_MASK); 20965d2a2dbbSLinus Torvalds 20975d2a2dbbSLinus Torvalds /* 20985d2a2dbbSLinus Torvalds * This really shouldn't fail, because the page is there 20995d2a2dbbSLinus Torvalds * in the page tables. But it might just be unreadable, 21005d2a2dbbSLinus Torvalds * in which case we just give up and fill the result with 21015d2a2dbbSLinus Torvalds * zeroes. 21025d2a2dbbSLinus Torvalds */ 21035d2a2dbbSLinus Torvalds if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) 21043ecb01dfSJan Beulich clear_page(kaddr); 21059b04c5feSCong Wang kunmap_atomic(kaddr); 2106c4ec7b0dSDmitriy Monakhov flush_dcache_page(dst); 21070ed361deSNick Piggin } else 21089de455b2SAtsushi Nemoto copy_user_highpage(dst, src, va, vma); 21096aab341eSLinus Torvalds } 21106aab341eSLinus Torvalds 2111c20cd45eSMichal Hocko static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma) 2112c20cd45eSMichal Hocko { 2113c20cd45eSMichal Hocko struct file *vm_file = vma->vm_file; 2114c20cd45eSMichal Hocko 2115c20cd45eSMichal Hocko if (vm_file) 2116c20cd45eSMichal Hocko return mapping_gfp_mask(vm_file->f_mapping) | __GFP_FS | __GFP_IO; 2117c20cd45eSMichal Hocko 2118c20cd45eSMichal Hocko /* 2119c20cd45eSMichal Hocko * Special mappings (e.g. VDSO) do not have any file so fake 2120c20cd45eSMichal Hocko * a default GFP_KERNEL for them. 2121c20cd45eSMichal Hocko */ 2122c20cd45eSMichal Hocko return GFP_KERNEL; 2123c20cd45eSMichal Hocko } 2124c20cd45eSMichal Hocko 21251da177e4SLinus Torvalds /* 2126fb09a464SKirill A. Shutemov * Notify the address space that the page is about to become writable so that 2127fb09a464SKirill A. Shutemov * it can prohibit this or wait for the page to get into an appropriate state. 2128fb09a464SKirill A. Shutemov * 2129fb09a464SKirill A. Shutemov * We do this without the lock held, so that it can sleep if it needs to. 2130fb09a464SKirill A. Shutemov */ 21312b740303SSouptick Joarder static vm_fault_t do_page_mkwrite(struct vm_fault *vmf) 2132fb09a464SKirill A. Shutemov { 21332b740303SSouptick Joarder vm_fault_t ret; 213438b8cb7fSJan Kara struct page *page = vmf->page; 213538b8cb7fSJan Kara unsigned int old_flags = vmf->flags; 2136fb09a464SKirill A. Shutemov 213738b8cb7fSJan Kara vmf->flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE; 2138fb09a464SKirill A. Shutemov 213911bac800SDave Jiang ret = vmf->vma->vm_ops->page_mkwrite(vmf); 214038b8cb7fSJan Kara /* Restore original flags so that caller is not surprised */ 214138b8cb7fSJan Kara vmf->flags = old_flags; 2142fb09a464SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) 2143fb09a464SKirill A. Shutemov return ret; 2144fb09a464SKirill A. Shutemov if (unlikely(!(ret & VM_FAULT_LOCKED))) { 2145fb09a464SKirill A. Shutemov lock_page(page); 2146fb09a464SKirill A. Shutemov if (!page->mapping) { 2147fb09a464SKirill A. Shutemov unlock_page(page); 2148fb09a464SKirill A. Shutemov return 0; /* retry */ 2149fb09a464SKirill A. Shutemov } 2150fb09a464SKirill A. Shutemov ret |= VM_FAULT_LOCKED; 2151fb09a464SKirill A. Shutemov } else 2152fb09a464SKirill A. Shutemov VM_BUG_ON_PAGE(!PageLocked(page), page); 2153fb09a464SKirill A. Shutemov return ret; 2154fb09a464SKirill A. Shutemov } 2155fb09a464SKirill A. Shutemov 2156fb09a464SKirill A. Shutemov /* 215797ba0c2bSJan Kara * Handle dirtying of a page in shared file mapping on a write fault. 21584e047f89SShachar Raindel * 215997ba0c2bSJan Kara * The function expects the page to be locked and unlocks it. 21604e047f89SShachar Raindel */ 216197ba0c2bSJan Kara static void fault_dirty_shared_page(struct vm_area_struct *vma, 216297ba0c2bSJan Kara struct page *page) 21634e047f89SShachar Raindel { 21644e047f89SShachar Raindel struct address_space *mapping; 216597ba0c2bSJan Kara bool dirtied; 216697ba0c2bSJan Kara bool page_mkwrite = vma->vm_ops && vma->vm_ops->page_mkwrite; 21674e047f89SShachar Raindel 21684e047f89SShachar Raindel dirtied = set_page_dirty(page); 21694e047f89SShachar Raindel VM_BUG_ON_PAGE(PageAnon(page), page); 217097ba0c2bSJan Kara /* 217197ba0c2bSJan Kara * Take a local copy of the address_space - page.mapping may be zeroed 217297ba0c2bSJan Kara * by truncate after unlock_page(). The address_space itself remains 217397ba0c2bSJan Kara * pinned by vma->vm_file's reference. We rely on unlock_page()'s 217497ba0c2bSJan Kara * release semantics to prevent the compiler from undoing this copying. 217597ba0c2bSJan Kara */ 217697ba0c2bSJan Kara mapping = page_rmapping(page); 21774e047f89SShachar Raindel unlock_page(page); 21784e047f89SShachar Raindel 21794e047f89SShachar Raindel if ((dirtied || page_mkwrite) && mapping) { 21804e047f89SShachar Raindel /* 21814e047f89SShachar Raindel * Some device drivers do not set page.mapping 21824e047f89SShachar Raindel * but still dirty their pages 21834e047f89SShachar Raindel */ 21844e047f89SShachar Raindel balance_dirty_pages_ratelimited(mapping); 21854e047f89SShachar Raindel } 21864e047f89SShachar Raindel 21874e047f89SShachar Raindel if (!page_mkwrite) 21884e047f89SShachar Raindel file_update_time(vma->vm_file); 21894e047f89SShachar Raindel } 21904e047f89SShachar Raindel 219197ba0c2bSJan Kara /* 21924e047f89SShachar Raindel * Handle write page faults for pages that can be reused in the current vma 21934e047f89SShachar Raindel * 21944e047f89SShachar Raindel * This can happen either due to the mapping being with the VM_SHARED flag, 21954e047f89SShachar Raindel * or due to us being the last reference standing to the page. In either 21964e047f89SShachar Raindel * case, all we need to do here is to mark the page as writable and update 21974e047f89SShachar Raindel * any related book-keeping. 21984e047f89SShachar Raindel */ 2199997dd98dSJan Kara static inline void wp_page_reuse(struct vm_fault *vmf) 220082b0f8c3SJan Kara __releases(vmf->ptl) 22014e047f89SShachar Raindel { 220282b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 2203a41b70d6SJan Kara struct page *page = vmf->page; 22044e047f89SShachar Raindel pte_t entry; 22054e047f89SShachar Raindel /* 22064e047f89SShachar Raindel * Clear the pages cpupid information as the existing 22074e047f89SShachar Raindel * information potentially belongs to a now completely 22084e047f89SShachar Raindel * unrelated process. 22094e047f89SShachar Raindel */ 22104e047f89SShachar Raindel if (page) 22114e047f89SShachar Raindel page_cpupid_xchg_last(page, (1 << LAST_CPUPID_SHIFT) - 1); 22124e047f89SShachar Raindel 22132994302bSJan Kara flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); 22142994302bSJan Kara entry = pte_mkyoung(vmf->orig_pte); 22154e047f89SShachar Raindel entry = maybe_mkwrite(pte_mkdirty(entry), vma); 221682b0f8c3SJan Kara if (ptep_set_access_flags(vma, vmf->address, vmf->pte, entry, 1)) 221782b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 221882b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 22194e047f89SShachar Raindel } 22204e047f89SShachar Raindel 22214e047f89SShachar Raindel /* 22222f38ab2cSShachar Raindel * Handle the case of a page which we actually need to copy to a new page. 22232f38ab2cSShachar Raindel * 22242f38ab2cSShachar Raindel * Called with mmap_sem locked and the old page referenced, but 22252f38ab2cSShachar Raindel * without the ptl held. 22262f38ab2cSShachar Raindel * 22272f38ab2cSShachar Raindel * High level logic flow: 22282f38ab2cSShachar Raindel * 22292f38ab2cSShachar Raindel * - Allocate a page, copy the content of the old page to the new one. 22302f38ab2cSShachar Raindel * - Handle book keeping and accounting - cgroups, mmu-notifiers, etc. 22312f38ab2cSShachar Raindel * - Take the PTL. If the pte changed, bail out and release the allocated page 22322f38ab2cSShachar Raindel * - If the pte is still the way we remember it, update the page table and all 22332f38ab2cSShachar Raindel * relevant references. This includes dropping the reference the page-table 22342f38ab2cSShachar Raindel * held to the old page, as well as updating the rmap. 22352f38ab2cSShachar Raindel * - In any case, unlock the PTL and drop the reference we took to the old page. 22362f38ab2cSShachar Raindel */ 22372b740303SSouptick Joarder static vm_fault_t wp_page_copy(struct vm_fault *vmf) 22382f38ab2cSShachar Raindel { 223982b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 2240bae473a4SKirill A. Shutemov struct mm_struct *mm = vma->vm_mm; 2241a41b70d6SJan Kara struct page *old_page = vmf->page; 22422f38ab2cSShachar Raindel struct page *new_page = NULL; 22432f38ab2cSShachar Raindel pte_t entry; 22442f38ab2cSShachar Raindel int page_copied = 0; 224582b0f8c3SJan Kara const unsigned long mmun_start = vmf->address & PAGE_MASK; 2246bae473a4SKirill A. Shutemov const unsigned long mmun_end = mmun_start + PAGE_SIZE; 22472f38ab2cSShachar Raindel struct mem_cgroup *memcg; 22482f38ab2cSShachar Raindel 22492f38ab2cSShachar Raindel if (unlikely(anon_vma_prepare(vma))) 22502f38ab2cSShachar Raindel goto oom; 22512f38ab2cSShachar Raindel 22522994302bSJan Kara if (is_zero_pfn(pte_pfn(vmf->orig_pte))) { 225382b0f8c3SJan Kara new_page = alloc_zeroed_user_highpage_movable(vma, 225482b0f8c3SJan Kara vmf->address); 22552f38ab2cSShachar Raindel if (!new_page) 22562f38ab2cSShachar Raindel goto oom; 22572f38ab2cSShachar Raindel } else { 2258bae473a4SKirill A. Shutemov new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, 225982b0f8c3SJan Kara vmf->address); 22602f38ab2cSShachar Raindel if (!new_page) 22612f38ab2cSShachar Raindel goto oom; 226282b0f8c3SJan Kara cow_user_page(new_page, old_page, vmf->address, vma); 22632f38ab2cSShachar Raindel } 22642f38ab2cSShachar Raindel 22652cf85583STejun Heo if (mem_cgroup_try_charge_delay(new_page, mm, GFP_KERNEL, &memcg, false)) 22662f38ab2cSShachar Raindel goto oom_free_new; 22672f38ab2cSShachar Raindel 2268eb3c24f3SMel Gorman __SetPageUptodate(new_page); 2269eb3c24f3SMel Gorman 22702f38ab2cSShachar Raindel mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); 22712f38ab2cSShachar Raindel 22722f38ab2cSShachar Raindel /* 22732f38ab2cSShachar Raindel * Re-check the pte - we dropped the lock 22742f38ab2cSShachar Raindel */ 227582b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(mm, vmf->pmd, vmf->address, &vmf->ptl); 22762994302bSJan Kara if (likely(pte_same(*vmf->pte, vmf->orig_pte))) { 22772f38ab2cSShachar Raindel if (old_page) { 22782f38ab2cSShachar Raindel if (!PageAnon(old_page)) { 2279eca56ff9SJerome Marchand dec_mm_counter_fast(mm, 2280eca56ff9SJerome Marchand mm_counter_file(old_page)); 22812f38ab2cSShachar Raindel inc_mm_counter_fast(mm, MM_ANONPAGES); 22822f38ab2cSShachar Raindel } 22832f38ab2cSShachar Raindel } else { 22842f38ab2cSShachar Raindel inc_mm_counter_fast(mm, MM_ANONPAGES); 22852f38ab2cSShachar Raindel } 22862994302bSJan Kara flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); 22872f38ab2cSShachar Raindel entry = mk_pte(new_page, vma->vm_page_prot); 22882f38ab2cSShachar Raindel entry = maybe_mkwrite(pte_mkdirty(entry), vma); 22892f38ab2cSShachar Raindel /* 22902f38ab2cSShachar Raindel * Clear the pte entry and flush it first, before updating the 22912f38ab2cSShachar Raindel * pte with the new entry. This will avoid a race condition 22922f38ab2cSShachar Raindel * seen in the presence of one thread doing SMC and another 22932f38ab2cSShachar Raindel * thread doing COW. 22942f38ab2cSShachar Raindel */ 229582b0f8c3SJan Kara ptep_clear_flush_notify(vma, vmf->address, vmf->pte); 229682b0f8c3SJan Kara page_add_new_anon_rmap(new_page, vma, vmf->address, false); 2297f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(new_page, memcg, false, false); 22982f38ab2cSShachar Raindel lru_cache_add_active_or_unevictable(new_page, vma); 22992f38ab2cSShachar Raindel /* 23002f38ab2cSShachar Raindel * We call the notify macro here because, when using secondary 23012f38ab2cSShachar Raindel * mmu page tables (such as kvm shadow page tables), we want the 23022f38ab2cSShachar Raindel * new page to be mapped directly into the secondary page table. 23032f38ab2cSShachar Raindel */ 230482b0f8c3SJan Kara set_pte_at_notify(mm, vmf->address, vmf->pte, entry); 230582b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 23062f38ab2cSShachar Raindel if (old_page) { 23072f38ab2cSShachar Raindel /* 23082f38ab2cSShachar Raindel * Only after switching the pte to the new page may 23092f38ab2cSShachar Raindel * we remove the mapcount here. Otherwise another 23102f38ab2cSShachar Raindel * process may come and find the rmap count decremented 23112f38ab2cSShachar Raindel * before the pte is switched to the new page, and 23122f38ab2cSShachar Raindel * "reuse" the old page writing into it while our pte 23132f38ab2cSShachar Raindel * here still points into it and can be read by other 23142f38ab2cSShachar Raindel * threads. 23152f38ab2cSShachar Raindel * 23162f38ab2cSShachar Raindel * The critical issue is to order this 23172f38ab2cSShachar Raindel * page_remove_rmap with the ptp_clear_flush above. 23182f38ab2cSShachar Raindel * Those stores are ordered by (if nothing else,) 23192f38ab2cSShachar Raindel * the barrier present in the atomic_add_negative 23202f38ab2cSShachar Raindel * in page_remove_rmap. 23212f38ab2cSShachar Raindel * 23222f38ab2cSShachar Raindel * Then the TLB flush in ptep_clear_flush ensures that 23232f38ab2cSShachar Raindel * no process can access the old page before the 23242f38ab2cSShachar Raindel * decremented mapcount is visible. And the old page 23252f38ab2cSShachar Raindel * cannot be reused until after the decremented 23262f38ab2cSShachar Raindel * mapcount is visible. So transitively, TLBs to 23272f38ab2cSShachar Raindel * old page will be flushed before it can be reused. 23282f38ab2cSShachar Raindel */ 2329d281ee61SKirill A. Shutemov page_remove_rmap(old_page, false); 23302f38ab2cSShachar Raindel } 23312f38ab2cSShachar Raindel 23322f38ab2cSShachar Raindel /* Free the old page.. */ 23332f38ab2cSShachar Raindel new_page = old_page; 23342f38ab2cSShachar Raindel page_copied = 1; 23352f38ab2cSShachar Raindel } else { 2336f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(new_page, memcg, false); 23372f38ab2cSShachar Raindel } 23382f38ab2cSShachar Raindel 23392f38ab2cSShachar Raindel if (new_page) 234009cbfeafSKirill A. Shutemov put_page(new_page); 23412f38ab2cSShachar Raindel 234282b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 23434645b9feSJérôme Glisse /* 23444645b9feSJérôme Glisse * No need to double call mmu_notifier->invalidate_range() callback as 23454645b9feSJérôme Glisse * the above ptep_clear_flush_notify() did already call it. 23464645b9feSJérôme Glisse */ 23474645b9feSJérôme Glisse mmu_notifier_invalidate_range_only_end(mm, mmun_start, mmun_end); 23482f38ab2cSShachar Raindel if (old_page) { 23492f38ab2cSShachar Raindel /* 23502f38ab2cSShachar Raindel * Don't let another task, with possibly unlocked vma, 23512f38ab2cSShachar Raindel * keep the mlocked page. 23522f38ab2cSShachar Raindel */ 23532f38ab2cSShachar Raindel if (page_copied && (vma->vm_flags & VM_LOCKED)) { 23542f38ab2cSShachar Raindel lock_page(old_page); /* LRU manipulation */ 2355e90309c9SKirill A. Shutemov if (PageMlocked(old_page)) 23562f38ab2cSShachar Raindel munlock_vma_page(old_page); 23572f38ab2cSShachar Raindel unlock_page(old_page); 23582f38ab2cSShachar Raindel } 235909cbfeafSKirill A. Shutemov put_page(old_page); 23602f38ab2cSShachar Raindel } 23612f38ab2cSShachar Raindel return page_copied ? VM_FAULT_WRITE : 0; 23622f38ab2cSShachar Raindel oom_free_new: 236309cbfeafSKirill A. Shutemov put_page(new_page); 23642f38ab2cSShachar Raindel oom: 23652f38ab2cSShachar Raindel if (old_page) 236609cbfeafSKirill A. Shutemov put_page(old_page); 23672f38ab2cSShachar Raindel return VM_FAULT_OOM; 23682f38ab2cSShachar Raindel } 23692f38ab2cSShachar Raindel 237066a6197cSJan Kara /** 237166a6197cSJan Kara * finish_mkwrite_fault - finish page fault for a shared mapping, making PTE 237266a6197cSJan Kara * writeable once the page is prepared 237366a6197cSJan Kara * 237466a6197cSJan Kara * @vmf: structure describing the fault 237566a6197cSJan Kara * 237666a6197cSJan Kara * This function handles all that is needed to finish a write page fault in a 237766a6197cSJan Kara * shared mapping due to PTE being read-only once the mapped page is prepared. 237866a6197cSJan Kara * It handles locking of PTE and modifying it. The function returns 237966a6197cSJan Kara * VM_FAULT_WRITE on success, 0 when PTE got changed before we acquired PTE 238066a6197cSJan Kara * lock. 238166a6197cSJan Kara * 238266a6197cSJan Kara * The function expects the page to be locked or other protection against 238366a6197cSJan Kara * concurrent faults / writeback (such as DAX radix tree locks). 238466a6197cSJan Kara */ 23852b740303SSouptick Joarder vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf) 238666a6197cSJan Kara { 238766a6197cSJan Kara WARN_ON_ONCE(!(vmf->vma->vm_flags & VM_SHARED)); 238866a6197cSJan Kara vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, vmf->address, 238966a6197cSJan Kara &vmf->ptl); 239066a6197cSJan Kara /* 239166a6197cSJan Kara * We might have raced with another page fault while we released the 239266a6197cSJan Kara * pte_offset_map_lock. 239366a6197cSJan Kara */ 239466a6197cSJan Kara if (!pte_same(*vmf->pte, vmf->orig_pte)) { 239566a6197cSJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2396a19e2553SJan Kara return VM_FAULT_NOPAGE; 239766a6197cSJan Kara } 239866a6197cSJan Kara wp_page_reuse(vmf); 2399a19e2553SJan Kara return 0; 240066a6197cSJan Kara } 240166a6197cSJan Kara 2402dd906184SBoaz Harrosh /* 2403dd906184SBoaz Harrosh * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED 2404dd906184SBoaz Harrosh * mapping 2405dd906184SBoaz Harrosh */ 24062b740303SSouptick Joarder static vm_fault_t wp_pfn_shared(struct vm_fault *vmf) 2407dd906184SBoaz Harrosh { 240882b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 2409bae473a4SKirill A. Shutemov 2410dd906184SBoaz Harrosh if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) { 24112b740303SSouptick Joarder vm_fault_t ret; 2412dd906184SBoaz Harrosh 241382b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2414fe82221fSJan Kara vmf->flags |= FAULT_FLAG_MKWRITE; 241511bac800SDave Jiang ret = vma->vm_ops->pfn_mkwrite(vmf); 24162f89dc12SJan Kara if (ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)) 2417dd906184SBoaz Harrosh return ret; 241866a6197cSJan Kara return finish_mkwrite_fault(vmf); 2419dd906184SBoaz Harrosh } 2420997dd98dSJan Kara wp_page_reuse(vmf); 2421997dd98dSJan Kara return VM_FAULT_WRITE; 2422dd906184SBoaz Harrosh } 2423dd906184SBoaz Harrosh 24242b740303SSouptick Joarder static vm_fault_t wp_page_shared(struct vm_fault *vmf) 242582b0f8c3SJan Kara __releases(vmf->ptl) 242693e478d4SShachar Raindel { 242782b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 242893e478d4SShachar Raindel 2429a41b70d6SJan Kara get_page(vmf->page); 243093e478d4SShachar Raindel 243193e478d4SShachar Raindel if (vma->vm_ops && vma->vm_ops->page_mkwrite) { 24322b740303SSouptick Joarder vm_fault_t tmp; 243393e478d4SShachar Raindel 243482b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 243538b8cb7fSJan Kara tmp = do_page_mkwrite(vmf); 243693e478d4SShachar Raindel if (unlikely(!tmp || (tmp & 243793e478d4SShachar Raindel (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { 2438a41b70d6SJan Kara put_page(vmf->page); 243993e478d4SShachar Raindel return tmp; 244093e478d4SShachar Raindel } 244166a6197cSJan Kara tmp = finish_mkwrite_fault(vmf); 2442a19e2553SJan Kara if (unlikely(tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) { 2443a41b70d6SJan Kara unlock_page(vmf->page); 2444a41b70d6SJan Kara put_page(vmf->page); 244566a6197cSJan Kara return tmp; 244693e478d4SShachar Raindel } 244766a6197cSJan Kara } else { 2448997dd98dSJan Kara wp_page_reuse(vmf); 2449997dd98dSJan Kara lock_page(vmf->page); 245093e478d4SShachar Raindel } 2451997dd98dSJan Kara fault_dirty_shared_page(vma, vmf->page); 2452997dd98dSJan Kara put_page(vmf->page); 245393e478d4SShachar Raindel 2454997dd98dSJan Kara return VM_FAULT_WRITE; 245593e478d4SShachar Raindel } 245693e478d4SShachar Raindel 24572f38ab2cSShachar Raindel /* 24581da177e4SLinus Torvalds * This routine handles present pages, when users try to write 24591da177e4SLinus Torvalds * to a shared page. It is done by copying the page to a new address 24601da177e4SLinus Torvalds * and decrementing the shared-page counter for the old page. 24611da177e4SLinus Torvalds * 24621da177e4SLinus Torvalds * Note that this routine assumes that the protection checks have been 24631da177e4SLinus Torvalds * done by the caller (the low-level page fault routine in most cases). 24641da177e4SLinus Torvalds * Thus we can safely just mark it writable once we've done any necessary 24651da177e4SLinus Torvalds * COW. 24661da177e4SLinus Torvalds * 24671da177e4SLinus Torvalds * We also mark the page dirty at this point even though the page will 24681da177e4SLinus Torvalds * change only once the write actually happens. This avoids a few races, 24691da177e4SLinus Torvalds * and potentially makes it more efficient. 24701da177e4SLinus Torvalds * 24718f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 24728f4e2101SHugh Dickins * but allow concurrent faults), with pte both mapped and locked. 24738f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 24741da177e4SLinus Torvalds */ 24752b740303SSouptick Joarder static vm_fault_t do_wp_page(struct vm_fault *vmf) 247682b0f8c3SJan Kara __releases(vmf->ptl) 24771da177e4SLinus Torvalds { 247882b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 24791da177e4SLinus Torvalds 2480a41b70d6SJan Kara vmf->page = vm_normal_page(vma, vmf->address, vmf->orig_pte); 2481a41b70d6SJan Kara if (!vmf->page) { 2482251b97f5SPeter Zijlstra /* 248364e45507SPeter Feiner * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a 248464e45507SPeter Feiner * VM_PFNMAP VMA. 2485251b97f5SPeter Zijlstra * 2486251b97f5SPeter Zijlstra * We should not cow pages in a shared writeable mapping. 2487dd906184SBoaz Harrosh * Just mark the pages writable and/or call ops->pfn_mkwrite. 2488251b97f5SPeter Zijlstra */ 2489251b97f5SPeter Zijlstra if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 2490251b97f5SPeter Zijlstra (VM_WRITE|VM_SHARED)) 24912994302bSJan Kara return wp_pfn_shared(vmf); 24922f38ab2cSShachar Raindel 249382b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2494a41b70d6SJan Kara return wp_page_copy(vmf); 2495251b97f5SPeter Zijlstra } 24961da177e4SLinus Torvalds 2497d08b3851SPeter Zijlstra /* 2498ee6a6457SPeter Zijlstra * Take out anonymous pages first, anonymous shared vmas are 2499ee6a6457SPeter Zijlstra * not dirty accountable. 2500d08b3851SPeter Zijlstra */ 2501a41b70d6SJan Kara if (PageAnon(vmf->page) && !PageKsm(vmf->page)) { 2502ba3c4ce6SHuang Ying int total_map_swapcount; 2503a41b70d6SJan Kara if (!trylock_page(vmf->page)) { 2504a41b70d6SJan Kara get_page(vmf->page); 250582b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2506a41b70d6SJan Kara lock_page(vmf->page); 250782b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 250882b0f8c3SJan Kara vmf->address, &vmf->ptl); 25092994302bSJan Kara if (!pte_same(*vmf->pte, vmf->orig_pte)) { 2510a41b70d6SJan Kara unlock_page(vmf->page); 251182b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2512a41b70d6SJan Kara put_page(vmf->page); 251328766805SShachar Raindel return 0; 2514ab967d86SHugh Dickins } 2515a41b70d6SJan Kara put_page(vmf->page); 2516ab967d86SHugh Dickins } 2517ba3c4ce6SHuang Ying if (reuse_swap_page(vmf->page, &total_map_swapcount)) { 2518ba3c4ce6SHuang Ying if (total_map_swapcount == 1) { 2519c44b6743SRik van Riel /* 25206d0a07edSAndrea Arcangeli * The page is all ours. Move it to 25216d0a07edSAndrea Arcangeli * our anon_vma so the rmap code will 25226d0a07edSAndrea Arcangeli * not search our parent or siblings. 25236d0a07edSAndrea Arcangeli * Protected against the rmap code by 25246d0a07edSAndrea Arcangeli * the page lock. 2525c44b6743SRik van Riel */ 2526a41b70d6SJan Kara page_move_anon_rmap(vmf->page, vma); 25276d0a07edSAndrea Arcangeli } 2528a41b70d6SJan Kara unlock_page(vmf->page); 2529997dd98dSJan Kara wp_page_reuse(vmf); 2530997dd98dSJan Kara return VM_FAULT_WRITE; 2531b009c024SMichel Lespinasse } 2532a41b70d6SJan Kara unlock_page(vmf->page); 2533ee6a6457SPeter Zijlstra } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 2534d08b3851SPeter Zijlstra (VM_WRITE|VM_SHARED))) { 2535a41b70d6SJan Kara return wp_page_shared(vmf); 25361da177e4SLinus Torvalds } 25371da177e4SLinus Torvalds 25381da177e4SLinus Torvalds /* 25391da177e4SLinus Torvalds * Ok, we need to copy. Oh, well.. 25401da177e4SLinus Torvalds */ 2541a41b70d6SJan Kara get_page(vmf->page); 254228766805SShachar Raindel 254382b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2544a41b70d6SJan Kara return wp_page_copy(vmf); 25451da177e4SLinus Torvalds } 25461da177e4SLinus Torvalds 254797a89413SPeter Zijlstra static void unmap_mapping_range_vma(struct vm_area_struct *vma, 25481da177e4SLinus Torvalds unsigned long start_addr, unsigned long end_addr, 25491da177e4SLinus Torvalds struct zap_details *details) 25501da177e4SLinus Torvalds { 2551f5cc4eefSAl Viro zap_page_range_single(vma, start_addr, end_addr - start_addr, details); 25521da177e4SLinus Torvalds } 25531da177e4SLinus Torvalds 2554f808c13fSDavidlohr Bueso static inline void unmap_mapping_range_tree(struct rb_root_cached *root, 25551da177e4SLinus Torvalds struct zap_details *details) 25561da177e4SLinus Torvalds { 25571da177e4SLinus Torvalds struct vm_area_struct *vma; 25581da177e4SLinus Torvalds pgoff_t vba, vea, zba, zea; 25591da177e4SLinus Torvalds 25606b2dbba8SMichel Lespinasse vma_interval_tree_foreach(vma, root, 25611da177e4SLinus Torvalds details->first_index, details->last_index) { 25621da177e4SLinus Torvalds 25631da177e4SLinus Torvalds vba = vma->vm_pgoff; 2564d6e93217SLibin vea = vba + vma_pages(vma) - 1; 25651da177e4SLinus Torvalds zba = details->first_index; 25661da177e4SLinus Torvalds if (zba < vba) 25671da177e4SLinus Torvalds zba = vba; 25681da177e4SLinus Torvalds zea = details->last_index; 25691da177e4SLinus Torvalds if (zea > vea) 25701da177e4SLinus Torvalds zea = vea; 25711da177e4SLinus Torvalds 257297a89413SPeter Zijlstra unmap_mapping_range_vma(vma, 25731da177e4SLinus Torvalds ((zba - vba) << PAGE_SHIFT) + vma->vm_start, 25741da177e4SLinus Torvalds ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start, 257597a89413SPeter Zijlstra details); 25761da177e4SLinus Torvalds } 25771da177e4SLinus Torvalds } 25781da177e4SLinus Torvalds 25791da177e4SLinus Torvalds /** 2580977fbdcdSMatthew Wilcox * unmap_mapping_pages() - Unmap pages from processes. 2581977fbdcdSMatthew Wilcox * @mapping: The address space containing pages to be unmapped. 2582977fbdcdSMatthew Wilcox * @start: Index of first page to be unmapped. 2583977fbdcdSMatthew Wilcox * @nr: Number of pages to be unmapped. 0 to unmap to end of file. 2584977fbdcdSMatthew Wilcox * @even_cows: Whether to unmap even private COWed pages. 2585977fbdcdSMatthew Wilcox * 2586977fbdcdSMatthew Wilcox * Unmap the pages in this address space from any userspace process which 2587977fbdcdSMatthew Wilcox * has them mmaped. Generally, you want to remove COWed pages as well when 2588977fbdcdSMatthew Wilcox * a file is being truncated, but not when invalidating pages from the page 2589977fbdcdSMatthew Wilcox * cache. 2590977fbdcdSMatthew Wilcox */ 2591977fbdcdSMatthew Wilcox void unmap_mapping_pages(struct address_space *mapping, pgoff_t start, 2592977fbdcdSMatthew Wilcox pgoff_t nr, bool even_cows) 2593977fbdcdSMatthew Wilcox { 2594977fbdcdSMatthew Wilcox struct zap_details details = { }; 2595977fbdcdSMatthew Wilcox 2596977fbdcdSMatthew Wilcox details.check_mapping = even_cows ? NULL : mapping; 2597977fbdcdSMatthew Wilcox details.first_index = start; 2598977fbdcdSMatthew Wilcox details.last_index = start + nr - 1; 2599977fbdcdSMatthew Wilcox if (details.last_index < details.first_index) 2600977fbdcdSMatthew Wilcox details.last_index = ULONG_MAX; 2601977fbdcdSMatthew Wilcox 2602977fbdcdSMatthew Wilcox i_mmap_lock_write(mapping); 2603977fbdcdSMatthew Wilcox if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root))) 2604977fbdcdSMatthew Wilcox unmap_mapping_range_tree(&mapping->i_mmap, &details); 2605977fbdcdSMatthew Wilcox i_mmap_unlock_write(mapping); 2606977fbdcdSMatthew Wilcox } 2607977fbdcdSMatthew Wilcox 2608977fbdcdSMatthew Wilcox /** 26098a5f14a2SKirill A. Shutemov * unmap_mapping_range - unmap the portion of all mmaps in the specified 2610977fbdcdSMatthew Wilcox * address_space corresponding to the specified byte range in the underlying 26118a5f14a2SKirill A. Shutemov * file. 26128a5f14a2SKirill A. Shutemov * 26133d41088fSMartin Waitz * @mapping: the address space containing mmaps to be unmapped. 26141da177e4SLinus Torvalds * @holebegin: byte in first page to unmap, relative to the start of 26151da177e4SLinus Torvalds * the underlying file. This will be rounded down to a PAGE_SIZE 261625d9e2d1Snpiggin@suse.de * boundary. Note that this is different from truncate_pagecache(), which 26171da177e4SLinus Torvalds * must keep the partial page. In contrast, we must get rid of 26181da177e4SLinus Torvalds * partial pages. 26191da177e4SLinus Torvalds * @holelen: size of prospective hole in bytes. This will be rounded 26201da177e4SLinus Torvalds * up to a PAGE_SIZE boundary. A holelen of zero truncates to the 26211da177e4SLinus Torvalds * end of the file. 26221da177e4SLinus Torvalds * @even_cows: 1 when truncating a file, unmap even private COWed pages; 26231da177e4SLinus Torvalds * but 0 when invalidating pagecache, don't throw away private data. 26241da177e4SLinus Torvalds */ 26251da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping, 26261da177e4SLinus Torvalds loff_t const holebegin, loff_t const holelen, int even_cows) 26271da177e4SLinus Torvalds { 26281da177e4SLinus Torvalds pgoff_t hba = holebegin >> PAGE_SHIFT; 26291da177e4SLinus Torvalds pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 26301da177e4SLinus Torvalds 26311da177e4SLinus Torvalds /* Check for overflow. */ 26321da177e4SLinus Torvalds if (sizeof(holelen) > sizeof(hlen)) { 26331da177e4SLinus Torvalds long long holeend = 26341da177e4SLinus Torvalds (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 26351da177e4SLinus Torvalds if (holeend & ~(long long)ULONG_MAX) 26361da177e4SLinus Torvalds hlen = ULONG_MAX - hba + 1; 26371da177e4SLinus Torvalds } 26381da177e4SLinus Torvalds 2639977fbdcdSMatthew Wilcox unmap_mapping_pages(mapping, hba, hlen, even_cows); 26401da177e4SLinus Torvalds } 26411da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range); 26421da177e4SLinus Torvalds 26431da177e4SLinus Torvalds /* 26448f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 26458f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 26469a95f3cfSPaul Cassella * We return with pte unmapped and unlocked. 26479a95f3cfSPaul Cassella * 26489a95f3cfSPaul Cassella * We return with the mmap_sem locked or unlocked in the same cases 26499a95f3cfSPaul Cassella * as does filemap_fault(). 26501da177e4SLinus Torvalds */ 26512b740303SSouptick Joarder vm_fault_t do_swap_page(struct vm_fault *vmf) 26521da177e4SLinus Torvalds { 265382b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 2654eaf649ebSMinchan Kim struct page *page = NULL, *swapcache; 265500501b53SJohannes Weiner struct mem_cgroup *memcg; 265665500d23SHugh Dickins swp_entry_t entry; 26571da177e4SLinus Torvalds pte_t pte; 2658d065bd81SMichel Lespinasse int locked; 2659ad8c2ee8SRik van Riel int exclusive = 0; 26602b740303SSouptick Joarder vm_fault_t ret = 0; 26611da177e4SLinus Torvalds 2662eaf649ebSMinchan Kim if (!pte_unmap_same(vma->vm_mm, vmf->pmd, vmf->pte, vmf->orig_pte)) 26638f4e2101SHugh Dickins goto out; 266465500d23SHugh Dickins 26652994302bSJan Kara entry = pte_to_swp_entry(vmf->orig_pte); 2666d1737fdbSAndi Kleen if (unlikely(non_swap_entry(entry))) { 26670697212aSChristoph Lameter if (is_migration_entry(entry)) { 266882b0f8c3SJan Kara migration_entry_wait(vma->vm_mm, vmf->pmd, 266982b0f8c3SJan Kara vmf->address); 26705042db43SJérôme Glisse } else if (is_device_private_entry(entry)) { 26715042db43SJérôme Glisse /* 26725042db43SJérôme Glisse * For un-addressable device memory we call the pgmap 26735042db43SJérôme Glisse * fault handler callback. The callback must migrate 26745042db43SJérôme Glisse * the page back to some CPU accessible page. 26755042db43SJérôme Glisse */ 26765042db43SJérôme Glisse ret = device_private_entry_fault(vma, vmf->address, entry, 26775042db43SJérôme Glisse vmf->flags, vmf->pmd); 2678d1737fdbSAndi Kleen } else if (is_hwpoison_entry(entry)) { 2679d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 2680d1737fdbSAndi Kleen } else { 26812994302bSJan Kara print_bad_pte(vma, vmf->address, vmf->orig_pte, NULL); 2682d99be1a8SHugh Dickins ret = VM_FAULT_SIGBUS; 2683d1737fdbSAndi Kleen } 26840697212aSChristoph Lameter goto out; 26850697212aSChristoph Lameter } 26860bcac06fSMinchan Kim 26870bcac06fSMinchan Kim 26880ff92245SShailabh Nagar delayacct_set_flag(DELAYACCT_PF_SWAPIN); 2689eaf649ebSMinchan Kim page = lookup_swap_cache(entry, vma, vmf->address); 2690f8020772SMinchan Kim swapcache = page; 2691f8020772SMinchan Kim 26921da177e4SLinus Torvalds if (!page) { 26930bcac06fSMinchan Kim struct swap_info_struct *si = swp_swap_info(entry); 26940bcac06fSMinchan Kim 2695aa8d22a1SMinchan Kim if (si->flags & SWP_SYNCHRONOUS_IO && 2696aa8d22a1SMinchan Kim __swap_count(si, entry) == 1) { 26970bcac06fSMinchan Kim /* skip swapcache */ 2698e9e9b7ecSMinchan Kim page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, 2699e9e9b7ecSMinchan Kim vmf->address); 27000bcac06fSMinchan Kim if (page) { 27010bcac06fSMinchan Kim __SetPageLocked(page); 27020bcac06fSMinchan Kim __SetPageSwapBacked(page); 27030bcac06fSMinchan Kim set_page_private(page, entry.val); 27040bcac06fSMinchan Kim lru_cache_add_anon(page); 27050bcac06fSMinchan Kim swap_readpage(page, true); 27060bcac06fSMinchan Kim } 2707aa8d22a1SMinchan Kim } else { 2708e9e9b7ecSMinchan Kim page = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, 2709e9e9b7ecSMinchan Kim vmf); 2710aa8d22a1SMinchan Kim swapcache = page; 27110bcac06fSMinchan Kim } 27120bcac06fSMinchan Kim 2713ec560175SHuang Ying if (!page) { 27141da177e4SLinus Torvalds /* 27158f4e2101SHugh Dickins * Back out if somebody else faulted in this pte 27168f4e2101SHugh Dickins * while we released the pte lock. 27171da177e4SLinus Torvalds */ 271882b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 271982b0f8c3SJan Kara vmf->address, &vmf->ptl); 27202994302bSJan Kara if (likely(pte_same(*vmf->pte, vmf->orig_pte))) 27211da177e4SLinus Torvalds ret = VM_FAULT_OOM; 27220ff92245SShailabh Nagar delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 272365500d23SHugh Dickins goto unlock; 27241da177e4SLinus Torvalds } 27251da177e4SLinus Torvalds 27261da177e4SLinus Torvalds /* Had to read the page from swap area: Major fault */ 27271da177e4SLinus Torvalds ret = VM_FAULT_MAJOR; 2728f8891e5eSChristoph Lameter count_vm_event(PGMAJFAULT); 27292262185cSRoman Gushchin count_memcg_event_mm(vma->vm_mm, PGMAJFAULT); 2730d1737fdbSAndi Kleen } else if (PageHWPoison(page)) { 273171f72525SWu Fengguang /* 273271f72525SWu Fengguang * hwpoisoned dirty swapcache pages are kept for killing 273371f72525SWu Fengguang * owner processes (which may be unknown at hwpoison time) 273471f72525SWu Fengguang */ 2735d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 2736d1737fdbSAndi Kleen delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 27374779cb31SAndi Kleen goto out_release; 27381da177e4SLinus Torvalds } 27391da177e4SLinus Torvalds 274082b0f8c3SJan Kara locked = lock_page_or_retry(page, vma->vm_mm, vmf->flags); 2741e709ffd6SRik van Riel 274220a1022dSBalbir Singh delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 2743d065bd81SMichel Lespinasse if (!locked) { 2744d065bd81SMichel Lespinasse ret |= VM_FAULT_RETRY; 2745d065bd81SMichel Lespinasse goto out_release; 2746d065bd81SMichel Lespinasse } 27471da177e4SLinus Torvalds 27484969c119SAndrea Arcangeli /* 274931c4a3d3SHugh Dickins * Make sure try_to_free_swap or reuse_swap_page or swapoff did not 275031c4a3d3SHugh Dickins * release the swapcache from under us. The page pin, and pte_same 275131c4a3d3SHugh Dickins * test below, are not enough to exclude that. Even if it is still 275231c4a3d3SHugh Dickins * swapcache, we need to check that the page's swap has not changed. 27534969c119SAndrea Arcangeli */ 27540bcac06fSMinchan Kim if (unlikely((!PageSwapCache(page) || 27550bcac06fSMinchan Kim page_private(page) != entry.val)) && swapcache) 27564969c119SAndrea Arcangeli goto out_page; 27574969c119SAndrea Arcangeli 275882b0f8c3SJan Kara page = ksm_might_need_to_copy(page, vma, vmf->address); 27594969c119SAndrea Arcangeli if (unlikely(!page)) { 27605ad64688SHugh Dickins ret = VM_FAULT_OOM; 27614969c119SAndrea Arcangeli page = swapcache; 27624969c119SAndrea Arcangeli goto out_page; 27634969c119SAndrea Arcangeli } 27645ad64688SHugh Dickins 27652cf85583STejun Heo if (mem_cgroup_try_charge_delay(page, vma->vm_mm, GFP_KERNEL, 2766bae473a4SKirill A. Shutemov &memcg, false)) { 2767073e587eSKAMEZAWA Hiroyuki ret = VM_FAULT_OOM; 2768bc43f75cSJohannes Weiner goto out_page; 2769073e587eSKAMEZAWA Hiroyuki } 2770073e587eSKAMEZAWA Hiroyuki 27711da177e4SLinus Torvalds /* 27728f4e2101SHugh Dickins * Back out if somebody else already faulted in this pte. 27731da177e4SLinus Torvalds */ 277482b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 277582b0f8c3SJan Kara &vmf->ptl); 27762994302bSJan Kara if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) 2777b8107480SKirill Korotaev goto out_nomap; 2778b8107480SKirill Korotaev 2779b8107480SKirill Korotaev if (unlikely(!PageUptodate(page))) { 2780b8107480SKirill Korotaev ret = VM_FAULT_SIGBUS; 2781b8107480SKirill Korotaev goto out_nomap; 27821da177e4SLinus Torvalds } 27831da177e4SLinus Torvalds 27848c7c6e34SKAMEZAWA Hiroyuki /* 27858c7c6e34SKAMEZAWA Hiroyuki * The page isn't present yet, go ahead with the fault. 27868c7c6e34SKAMEZAWA Hiroyuki * 27878c7c6e34SKAMEZAWA Hiroyuki * Be careful about the sequence of operations here. 27888c7c6e34SKAMEZAWA Hiroyuki * To get its accounting right, reuse_swap_page() must be called 27898c7c6e34SKAMEZAWA Hiroyuki * while the page is counted on swap but not yet in mapcount i.e. 27908c7c6e34SKAMEZAWA Hiroyuki * before page_add_anon_rmap() and swap_free(); try_to_free_swap() 27918c7c6e34SKAMEZAWA Hiroyuki * must be called after the swap_free(), or it will never succeed. 27928c7c6e34SKAMEZAWA Hiroyuki */ 27931da177e4SLinus Torvalds 2794bae473a4SKirill A. Shutemov inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); 2795bae473a4SKirill A. Shutemov dec_mm_counter_fast(vma->vm_mm, MM_SWAPENTS); 27961da177e4SLinus Torvalds pte = mk_pte(page, vma->vm_page_prot); 279782b0f8c3SJan Kara if ((vmf->flags & FAULT_FLAG_WRITE) && reuse_swap_page(page, NULL)) { 27981da177e4SLinus Torvalds pte = maybe_mkwrite(pte_mkdirty(pte), vma); 279982b0f8c3SJan Kara vmf->flags &= ~FAULT_FLAG_WRITE; 28009a5b489bSAndrea Arcangeli ret |= VM_FAULT_WRITE; 2801d281ee61SKirill A. Shutemov exclusive = RMAP_EXCLUSIVE; 28021da177e4SLinus Torvalds } 28031da177e4SLinus Torvalds flush_icache_page(vma, page); 28042994302bSJan Kara if (pte_swp_soft_dirty(vmf->orig_pte)) 2805179ef71cSCyrill Gorcunov pte = pte_mksoft_dirty(pte); 280682b0f8c3SJan Kara set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte); 2807ca827d55SKhalid Aziz arch_do_swap_page(vma->vm_mm, vma, vmf->address, pte, vmf->orig_pte); 28082994302bSJan Kara vmf->orig_pte = pte; 28090bcac06fSMinchan Kim 28100bcac06fSMinchan Kim /* ksm created a completely new copy */ 28110bcac06fSMinchan Kim if (unlikely(page != swapcache && swapcache)) { 281282b0f8c3SJan Kara page_add_new_anon_rmap(page, vma, vmf->address, false); 2813f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, false); 281400501b53SJohannes Weiner lru_cache_add_active_or_unevictable(page, vma); 28150bcac06fSMinchan Kim } else { 28160bcac06fSMinchan Kim do_page_add_anon_rmap(page, vma, vmf->address, exclusive); 28170bcac06fSMinchan Kim mem_cgroup_commit_charge(page, memcg, true, false); 28180bcac06fSMinchan Kim activate_page(page); 281900501b53SJohannes Weiner } 28201da177e4SLinus Torvalds 2821c475a8abSHugh Dickins swap_free(entry); 28225ccc5abaSVladimir Davydov if (mem_cgroup_swap_full(page) || 28235ccc5abaSVladimir Davydov (vma->vm_flags & VM_LOCKED) || PageMlocked(page)) 2824a2c43eedSHugh Dickins try_to_free_swap(page); 2825c475a8abSHugh Dickins unlock_page(page); 28260bcac06fSMinchan Kim if (page != swapcache && swapcache) { 28274969c119SAndrea Arcangeli /* 28284969c119SAndrea Arcangeli * Hold the lock to avoid the swap entry to be reused 28294969c119SAndrea Arcangeli * until we take the PT lock for the pte_same() check 28304969c119SAndrea Arcangeli * (to avoid false positives from pte_same). For 28314969c119SAndrea Arcangeli * further safety release the lock after the swap_free 28324969c119SAndrea Arcangeli * so that the swap count won't change under a 28334969c119SAndrea Arcangeli * parallel locked swapcache. 28344969c119SAndrea Arcangeli */ 28354969c119SAndrea Arcangeli unlock_page(swapcache); 283609cbfeafSKirill A. Shutemov put_page(swapcache); 28374969c119SAndrea Arcangeli } 2838c475a8abSHugh Dickins 283982b0f8c3SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) { 28402994302bSJan Kara ret |= do_wp_page(vmf); 284161469f1dSHugh Dickins if (ret & VM_FAULT_ERROR) 284261469f1dSHugh Dickins ret &= VM_FAULT_ERROR; 28431da177e4SLinus Torvalds goto out; 28441da177e4SLinus Torvalds } 28451da177e4SLinus Torvalds 28461da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 284782b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 284865500d23SHugh Dickins unlock: 284982b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 28501da177e4SLinus Torvalds out: 28511da177e4SLinus Torvalds return ret; 2852b8107480SKirill Korotaev out_nomap: 2853f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 285482b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2855bc43f75cSJohannes Weiner out_page: 2856b8107480SKirill Korotaev unlock_page(page); 28574779cb31SAndi Kleen out_release: 285809cbfeafSKirill A. Shutemov put_page(page); 28590bcac06fSMinchan Kim if (page != swapcache && swapcache) { 28604969c119SAndrea Arcangeli unlock_page(swapcache); 286109cbfeafSKirill A. Shutemov put_page(swapcache); 28624969c119SAndrea Arcangeli } 286365500d23SHugh Dickins return ret; 28641da177e4SLinus Torvalds } 28651da177e4SLinus Torvalds 28661da177e4SLinus Torvalds /* 28678f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 28688f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 28698f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 28701da177e4SLinus Torvalds */ 28712b740303SSouptick Joarder static vm_fault_t do_anonymous_page(struct vm_fault *vmf) 28721da177e4SLinus Torvalds { 287382b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 287400501b53SJohannes Weiner struct mem_cgroup *memcg; 28758f4e2101SHugh Dickins struct page *page; 28762b740303SSouptick Joarder vm_fault_t ret = 0; 28771da177e4SLinus Torvalds pte_t entry; 28781da177e4SLinus Torvalds 28796b7339f4SKirill A. Shutemov /* File mapping without ->vm_ops ? */ 28806b7339f4SKirill A. Shutemov if (vma->vm_flags & VM_SHARED) 28816b7339f4SKirill A. Shutemov return VM_FAULT_SIGBUS; 28826b7339f4SKirill A. Shutemov 28837267ec00SKirill A. Shutemov /* 28847267ec00SKirill A. Shutemov * Use pte_alloc() instead of pte_alloc_map(). We can't run 28857267ec00SKirill A. Shutemov * pte_offset_map() on pmds where a huge pmd might be created 28867267ec00SKirill A. Shutemov * from a different thread. 28877267ec00SKirill A. Shutemov * 28887267ec00SKirill A. Shutemov * pte_alloc_map() is safe to use under down_write(mmap_sem) or when 28897267ec00SKirill A. Shutemov * parallel threads are excluded by other means. 28907267ec00SKirill A. Shutemov * 28917267ec00SKirill A. Shutemov * Here we only have down_read(mmap_sem). 28927267ec00SKirill A. Shutemov */ 289382b0f8c3SJan Kara if (pte_alloc(vma->vm_mm, vmf->pmd, vmf->address)) 28947267ec00SKirill A. Shutemov return VM_FAULT_OOM; 28957267ec00SKirill A. Shutemov 28967267ec00SKirill A. Shutemov /* See the comment in pte_alloc_one_map() */ 289782b0f8c3SJan Kara if (unlikely(pmd_trans_unstable(vmf->pmd))) 28987267ec00SKirill A. Shutemov return 0; 28997267ec00SKirill A. Shutemov 290011ac5524SLinus Torvalds /* Use the zero-page for reads */ 290182b0f8c3SJan Kara if (!(vmf->flags & FAULT_FLAG_WRITE) && 2902bae473a4SKirill A. Shutemov !mm_forbids_zeropage(vma->vm_mm)) { 290382b0f8c3SJan Kara entry = pte_mkspecial(pfn_pte(my_zero_pfn(vmf->address), 290462eede62SHugh Dickins vma->vm_page_prot)); 290582b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 290682b0f8c3SJan Kara vmf->address, &vmf->ptl); 290782b0f8c3SJan Kara if (!pte_none(*vmf->pte)) 2908a13ea5b7SHugh Dickins goto unlock; 29096b31d595SMichal Hocko ret = check_stable_address_space(vma->vm_mm); 29106b31d595SMichal Hocko if (ret) 29116b31d595SMichal Hocko goto unlock; 29126b251fc9SAndrea Arcangeli /* Deliver the page fault to userland, check inside PT lock */ 29136b251fc9SAndrea Arcangeli if (userfaultfd_missing(vma)) { 291482b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 291582b0f8c3SJan Kara return handle_userfault(vmf, VM_UFFD_MISSING); 29166b251fc9SAndrea Arcangeli } 2917a13ea5b7SHugh Dickins goto setpte; 2918a13ea5b7SHugh Dickins } 2919a13ea5b7SHugh Dickins 29201da177e4SLinus Torvalds /* Allocate our own private page. */ 29211da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 292265500d23SHugh Dickins goto oom; 292382b0f8c3SJan Kara page = alloc_zeroed_user_highpage_movable(vma, vmf->address); 29241da177e4SLinus Torvalds if (!page) 292565500d23SHugh Dickins goto oom; 2926eb3c24f3SMel Gorman 29272cf85583STejun Heo if (mem_cgroup_try_charge_delay(page, vma->vm_mm, GFP_KERNEL, &memcg, 29282cf85583STejun Heo false)) 2929eb3c24f3SMel Gorman goto oom_free_page; 2930eb3c24f3SMel Gorman 293152f37629SMinchan Kim /* 293252f37629SMinchan Kim * The memory barrier inside __SetPageUptodate makes sure that 293352f37629SMinchan Kim * preceeding stores to the page contents become visible before 293452f37629SMinchan Kim * the set_pte_at() write. 293552f37629SMinchan Kim */ 29360ed361deSNick Piggin __SetPageUptodate(page); 29371da177e4SLinus Torvalds 293865500d23SHugh Dickins entry = mk_pte(page, vma->vm_page_prot); 29391ac0cb5dSHugh Dickins if (vma->vm_flags & VM_WRITE) 29401ac0cb5dSHugh Dickins entry = pte_mkwrite(pte_mkdirty(entry)); 29418f4e2101SHugh Dickins 294282b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 294382b0f8c3SJan Kara &vmf->ptl); 294482b0f8c3SJan Kara if (!pte_none(*vmf->pte)) 29458f4e2101SHugh Dickins goto release; 29469ba69294SHugh Dickins 29476b31d595SMichal Hocko ret = check_stable_address_space(vma->vm_mm); 29486b31d595SMichal Hocko if (ret) 29496b31d595SMichal Hocko goto release; 29506b31d595SMichal Hocko 29516b251fc9SAndrea Arcangeli /* Deliver the page fault to userland, check inside PT lock */ 29526b251fc9SAndrea Arcangeli if (userfaultfd_missing(vma)) { 295382b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2954f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 295509cbfeafSKirill A. Shutemov put_page(page); 295682b0f8c3SJan Kara return handle_userfault(vmf, VM_UFFD_MISSING); 29576b251fc9SAndrea Arcangeli } 29586b251fc9SAndrea Arcangeli 2959bae473a4SKirill A. Shutemov inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); 296082b0f8c3SJan Kara page_add_new_anon_rmap(page, vma, vmf->address, false); 2961f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, false); 296200501b53SJohannes Weiner lru_cache_add_active_or_unevictable(page, vma); 2963a13ea5b7SHugh Dickins setpte: 296482b0f8c3SJan Kara set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry); 29651da177e4SLinus Torvalds 29661da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 296782b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 296865500d23SHugh Dickins unlock: 296982b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 29706b31d595SMichal Hocko return ret; 29718f4e2101SHugh Dickins release: 2972f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 297309cbfeafSKirill A. Shutemov put_page(page); 29748f4e2101SHugh Dickins goto unlock; 29758a9f3ccdSBalbir Singh oom_free_page: 297609cbfeafSKirill A. Shutemov put_page(page); 297765500d23SHugh Dickins oom: 29781da177e4SLinus Torvalds return VM_FAULT_OOM; 29791da177e4SLinus Torvalds } 29801da177e4SLinus Torvalds 29819a95f3cfSPaul Cassella /* 29829a95f3cfSPaul Cassella * The mmap_sem must have been held on entry, and may have been 29839a95f3cfSPaul Cassella * released depending on flags and vma->vm_ops->fault() return value. 29849a95f3cfSPaul Cassella * See filemap_fault() and __lock_page_retry(). 29859a95f3cfSPaul Cassella */ 29862b740303SSouptick Joarder static vm_fault_t __do_fault(struct vm_fault *vmf) 29877eae74afSKirill A. Shutemov { 298882b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 29892b740303SSouptick Joarder vm_fault_t ret; 29907eae74afSKirill A. Shutemov 299111bac800SDave Jiang ret = vma->vm_ops->fault(vmf); 29923917048dSJan Kara if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY | 2993b1aa812bSJan Kara VM_FAULT_DONE_COW))) 29947eae74afSKirill A. Shutemov return ret; 29957eae74afSKirill A. Shutemov 2996667240e0SJan Kara if (unlikely(PageHWPoison(vmf->page))) { 29977eae74afSKirill A. Shutemov if (ret & VM_FAULT_LOCKED) 2998667240e0SJan Kara unlock_page(vmf->page); 2999667240e0SJan Kara put_page(vmf->page); 3000936ca80dSJan Kara vmf->page = NULL; 30017eae74afSKirill A. Shutemov return VM_FAULT_HWPOISON; 30027eae74afSKirill A. Shutemov } 30037eae74afSKirill A. Shutemov 30047eae74afSKirill A. Shutemov if (unlikely(!(ret & VM_FAULT_LOCKED))) 3005667240e0SJan Kara lock_page(vmf->page); 30067eae74afSKirill A. Shutemov else 3007667240e0SJan Kara VM_BUG_ON_PAGE(!PageLocked(vmf->page), vmf->page); 30087eae74afSKirill A. Shutemov 30097eae74afSKirill A. Shutemov return ret; 30107eae74afSKirill A. Shutemov } 30117eae74afSKirill A. Shutemov 3012d0f0931dSRoss Zwisler /* 3013d0f0931dSRoss Zwisler * The ordering of these checks is important for pmds with _PAGE_DEVMAP set. 3014d0f0931dSRoss Zwisler * If we check pmd_trans_unstable() first we will trip the bad_pmd() check 3015d0f0931dSRoss Zwisler * inside of pmd_none_or_trans_huge_or_clear_bad(). This will end up correctly 3016d0f0931dSRoss Zwisler * returning 1 but not before it spams dmesg with the pmd_clear_bad() output. 3017d0f0931dSRoss Zwisler */ 3018d0f0931dSRoss Zwisler static int pmd_devmap_trans_unstable(pmd_t *pmd) 3019d0f0931dSRoss Zwisler { 3020d0f0931dSRoss Zwisler return pmd_devmap(*pmd) || pmd_trans_unstable(pmd); 3021d0f0931dSRoss Zwisler } 3022d0f0931dSRoss Zwisler 30232b740303SSouptick Joarder static vm_fault_t pte_alloc_one_map(struct vm_fault *vmf) 30247267ec00SKirill A. Shutemov { 302582b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 30267267ec00SKirill A. Shutemov 302782b0f8c3SJan Kara if (!pmd_none(*vmf->pmd)) 30287267ec00SKirill A. Shutemov goto map_pte; 302982b0f8c3SJan Kara if (vmf->prealloc_pte) { 303082b0f8c3SJan Kara vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); 303182b0f8c3SJan Kara if (unlikely(!pmd_none(*vmf->pmd))) { 303282b0f8c3SJan Kara spin_unlock(vmf->ptl); 30337267ec00SKirill A. Shutemov goto map_pte; 30347267ec00SKirill A. Shutemov } 30357267ec00SKirill A. Shutemov 3036c4812909SKirill A. Shutemov mm_inc_nr_ptes(vma->vm_mm); 303782b0f8c3SJan Kara pmd_populate(vma->vm_mm, vmf->pmd, vmf->prealloc_pte); 303882b0f8c3SJan Kara spin_unlock(vmf->ptl); 30397f2b6ce8STobin C Harding vmf->prealloc_pte = NULL; 304082b0f8c3SJan Kara } else if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd, vmf->address))) { 30417267ec00SKirill A. Shutemov return VM_FAULT_OOM; 30427267ec00SKirill A. Shutemov } 30437267ec00SKirill A. Shutemov map_pte: 30447267ec00SKirill A. Shutemov /* 30457267ec00SKirill A. Shutemov * If a huge pmd materialized under us just retry later. Use 3046d0f0931dSRoss Zwisler * pmd_trans_unstable() via pmd_devmap_trans_unstable() instead of 3047d0f0931dSRoss Zwisler * pmd_trans_huge() to ensure the pmd didn't become pmd_trans_huge 3048d0f0931dSRoss Zwisler * under us and then back to pmd_none, as a result of MADV_DONTNEED 3049d0f0931dSRoss Zwisler * running immediately after a huge pmd fault in a different thread of 3050d0f0931dSRoss Zwisler * this mm, in turn leading to a misleading pmd_trans_huge() retval. 3051d0f0931dSRoss Zwisler * All we have to ensure is that it is a regular pmd that we can walk 3052d0f0931dSRoss Zwisler * with pte_offset_map() and we can do that through an atomic read in 3053d0f0931dSRoss Zwisler * C, which is what pmd_trans_unstable() provides. 30547267ec00SKirill A. Shutemov */ 3055d0f0931dSRoss Zwisler if (pmd_devmap_trans_unstable(vmf->pmd)) 30567267ec00SKirill A. Shutemov return VM_FAULT_NOPAGE; 30577267ec00SKirill A. Shutemov 3058d0f0931dSRoss Zwisler /* 3059d0f0931dSRoss Zwisler * At this point we know that our vmf->pmd points to a page of ptes 3060d0f0931dSRoss Zwisler * and it cannot become pmd_none(), pmd_devmap() or pmd_trans_huge() 3061d0f0931dSRoss Zwisler * for the duration of the fault. If a racing MADV_DONTNEED runs and 3062d0f0931dSRoss Zwisler * we zap the ptes pointed to by our vmf->pmd, the vmf->ptl will still 3063d0f0931dSRoss Zwisler * be valid and we will re-check to make sure the vmf->pte isn't 3064d0f0931dSRoss Zwisler * pte_none() under vmf->ptl protection when we return to 3065d0f0931dSRoss Zwisler * alloc_set_pte(). 3066d0f0931dSRoss Zwisler */ 306782b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 306882b0f8c3SJan Kara &vmf->ptl); 30697267ec00SKirill A. Shutemov return 0; 30707267ec00SKirill A. Shutemov } 30717267ec00SKirill A. Shutemov 3072e496cf3dSKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 307310102459SKirill A. Shutemov 307410102459SKirill A. Shutemov #define HPAGE_CACHE_INDEX_MASK (HPAGE_PMD_NR - 1) 307510102459SKirill A. Shutemov static inline bool transhuge_vma_suitable(struct vm_area_struct *vma, 307610102459SKirill A. Shutemov unsigned long haddr) 307710102459SKirill A. Shutemov { 307810102459SKirill A. Shutemov if (((vma->vm_start >> PAGE_SHIFT) & HPAGE_CACHE_INDEX_MASK) != 307910102459SKirill A. Shutemov (vma->vm_pgoff & HPAGE_CACHE_INDEX_MASK)) 308010102459SKirill A. Shutemov return false; 308110102459SKirill A. Shutemov if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end) 308210102459SKirill A. Shutemov return false; 308310102459SKirill A. Shutemov return true; 308410102459SKirill A. Shutemov } 308510102459SKirill A. Shutemov 308682b0f8c3SJan Kara static void deposit_prealloc_pte(struct vm_fault *vmf) 3087953c66c2SAneesh Kumar K.V { 308882b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3089953c66c2SAneesh Kumar K.V 309082b0f8c3SJan Kara pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, vmf->prealloc_pte); 3091953c66c2SAneesh Kumar K.V /* 3092953c66c2SAneesh Kumar K.V * We are going to consume the prealloc table, 3093953c66c2SAneesh Kumar K.V * count that as nr_ptes. 3094953c66c2SAneesh Kumar K.V */ 3095c4812909SKirill A. Shutemov mm_inc_nr_ptes(vma->vm_mm); 30967f2b6ce8STobin C Harding vmf->prealloc_pte = NULL; 3097953c66c2SAneesh Kumar K.V } 3098953c66c2SAneesh Kumar K.V 30992b740303SSouptick Joarder static vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page) 310010102459SKirill A. Shutemov { 310182b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 310282b0f8c3SJan Kara bool write = vmf->flags & FAULT_FLAG_WRITE; 310382b0f8c3SJan Kara unsigned long haddr = vmf->address & HPAGE_PMD_MASK; 310410102459SKirill A. Shutemov pmd_t entry; 31052b740303SSouptick Joarder int i; 31062b740303SSouptick Joarder vm_fault_t ret; 310710102459SKirill A. Shutemov 310810102459SKirill A. Shutemov if (!transhuge_vma_suitable(vma, haddr)) 310910102459SKirill A. Shutemov return VM_FAULT_FALLBACK; 311010102459SKirill A. Shutemov 311110102459SKirill A. Shutemov ret = VM_FAULT_FALLBACK; 311210102459SKirill A. Shutemov page = compound_head(page); 311310102459SKirill A. Shutemov 3114953c66c2SAneesh Kumar K.V /* 3115953c66c2SAneesh Kumar K.V * Archs like ppc64 need additonal space to store information 3116953c66c2SAneesh Kumar K.V * related to pte entry. Use the preallocated table for that. 3117953c66c2SAneesh Kumar K.V */ 311882b0f8c3SJan Kara if (arch_needs_pgtable_deposit() && !vmf->prealloc_pte) { 311982b0f8c3SJan Kara vmf->prealloc_pte = pte_alloc_one(vma->vm_mm, vmf->address); 312082b0f8c3SJan Kara if (!vmf->prealloc_pte) 3121953c66c2SAneesh Kumar K.V return VM_FAULT_OOM; 3122953c66c2SAneesh Kumar K.V smp_wmb(); /* See comment in __pte_alloc() */ 3123953c66c2SAneesh Kumar K.V } 3124953c66c2SAneesh Kumar K.V 312582b0f8c3SJan Kara vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); 312682b0f8c3SJan Kara if (unlikely(!pmd_none(*vmf->pmd))) 312710102459SKirill A. Shutemov goto out; 312810102459SKirill A. Shutemov 312910102459SKirill A. Shutemov for (i = 0; i < HPAGE_PMD_NR; i++) 313010102459SKirill A. Shutemov flush_icache_page(vma, page + i); 313110102459SKirill A. Shutemov 313210102459SKirill A. Shutemov entry = mk_huge_pmd(page, vma->vm_page_prot); 313310102459SKirill A. Shutemov if (write) 3134f55e1014SLinus Torvalds entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); 313510102459SKirill A. Shutemov 3136fadae295SYang Shi add_mm_counter(vma->vm_mm, mm_counter_file(page), HPAGE_PMD_NR); 313710102459SKirill A. Shutemov page_add_file_rmap(page, true); 3138953c66c2SAneesh Kumar K.V /* 3139953c66c2SAneesh Kumar K.V * deposit and withdraw with pmd lock held 3140953c66c2SAneesh Kumar K.V */ 3141953c66c2SAneesh Kumar K.V if (arch_needs_pgtable_deposit()) 314282b0f8c3SJan Kara deposit_prealloc_pte(vmf); 314310102459SKirill A. Shutemov 314482b0f8c3SJan Kara set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry); 314510102459SKirill A. Shutemov 314682b0f8c3SJan Kara update_mmu_cache_pmd(vma, haddr, vmf->pmd); 314710102459SKirill A. Shutemov 314810102459SKirill A. Shutemov /* fault is handled */ 314910102459SKirill A. Shutemov ret = 0; 315095ecedcdSKirill A. Shutemov count_vm_event(THP_FILE_MAPPED); 315110102459SKirill A. Shutemov out: 315282b0f8c3SJan Kara spin_unlock(vmf->ptl); 315310102459SKirill A. Shutemov return ret; 315410102459SKirill A. Shutemov } 315510102459SKirill A. Shutemov #else 31562b740303SSouptick Joarder static vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page) 315710102459SKirill A. Shutemov { 315810102459SKirill A. Shutemov BUILD_BUG(); 315910102459SKirill A. Shutemov return 0; 316010102459SKirill A. Shutemov } 316110102459SKirill A. Shutemov #endif 316210102459SKirill A. Shutemov 31638c6e50b0SKirill A. Shutemov /** 31647267ec00SKirill A. Shutemov * alloc_set_pte - setup new PTE entry for given page and add reverse page 31657267ec00SKirill A. Shutemov * mapping. If needed, the fucntion allocates page table or use pre-allocated. 31668c6e50b0SKirill A. Shutemov * 316782b0f8c3SJan Kara * @vmf: fault environment 31687267ec00SKirill A. Shutemov * @memcg: memcg to charge page (only for private mappings) 31698c6e50b0SKirill A. Shutemov * @page: page to map 31708c6e50b0SKirill A. Shutemov * 317182b0f8c3SJan Kara * Caller must take care of unlocking vmf->ptl, if vmf->pte is non-NULL on 317282b0f8c3SJan Kara * return. 31738c6e50b0SKirill A. Shutemov * 31748c6e50b0SKirill A. Shutemov * Target users are page handler itself and implementations of 31758c6e50b0SKirill A. Shutemov * vm_ops->map_pages. 31768c6e50b0SKirill A. Shutemov */ 31772b740303SSouptick Joarder vm_fault_t alloc_set_pte(struct vm_fault *vmf, struct mem_cgroup *memcg, 31787267ec00SKirill A. Shutemov struct page *page) 31793bb97794SKirill A. Shutemov { 318082b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 318182b0f8c3SJan Kara bool write = vmf->flags & FAULT_FLAG_WRITE; 31823bb97794SKirill A. Shutemov pte_t entry; 31832b740303SSouptick Joarder vm_fault_t ret; 318410102459SKirill A. Shutemov 318582b0f8c3SJan Kara if (pmd_none(*vmf->pmd) && PageTransCompound(page) && 3186e496cf3dSKirill A. Shutemov IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) { 318710102459SKirill A. Shutemov /* THP on COW? */ 318810102459SKirill A. Shutemov VM_BUG_ON_PAGE(memcg, page); 318910102459SKirill A. Shutemov 319082b0f8c3SJan Kara ret = do_set_pmd(vmf, page); 319110102459SKirill A. Shutemov if (ret != VM_FAULT_FALLBACK) 3192b0b9b3dfSHugh Dickins return ret; 319310102459SKirill A. Shutemov } 31943bb97794SKirill A. Shutemov 319582b0f8c3SJan Kara if (!vmf->pte) { 319682b0f8c3SJan Kara ret = pte_alloc_one_map(vmf); 31977267ec00SKirill A. Shutemov if (ret) 3198b0b9b3dfSHugh Dickins return ret; 31997267ec00SKirill A. Shutemov } 32007267ec00SKirill A. Shutemov 32017267ec00SKirill A. Shutemov /* Re-check under ptl */ 3202b0b9b3dfSHugh Dickins if (unlikely(!pte_none(*vmf->pte))) 3203b0b9b3dfSHugh Dickins return VM_FAULT_NOPAGE; 32047267ec00SKirill A. Shutemov 32053bb97794SKirill A. Shutemov flush_icache_page(vma, page); 32063bb97794SKirill A. Shutemov entry = mk_pte(page, vma->vm_page_prot); 32073bb97794SKirill A. Shutemov if (write) 32083bb97794SKirill A. Shutemov entry = maybe_mkwrite(pte_mkdirty(entry), vma); 3209bae473a4SKirill A. Shutemov /* copy-on-write page */ 3210bae473a4SKirill A. Shutemov if (write && !(vma->vm_flags & VM_SHARED)) { 32113bb97794SKirill A. Shutemov inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); 321282b0f8c3SJan Kara page_add_new_anon_rmap(page, vma, vmf->address, false); 32137267ec00SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, false); 32147267ec00SKirill A. Shutemov lru_cache_add_active_or_unevictable(page, vma); 32153bb97794SKirill A. Shutemov } else { 3216eca56ff9SJerome Marchand inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page)); 3217dd78feddSKirill A. Shutemov page_add_file_rmap(page, false); 32183bb97794SKirill A. Shutemov } 321982b0f8c3SJan Kara set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry); 32203bb97794SKirill A. Shutemov 32213bb97794SKirill A. Shutemov /* no need to invalidate: a not-present page won't be cached */ 322282b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 32237267ec00SKirill A. Shutemov 3224b0b9b3dfSHugh Dickins return 0; 32253bb97794SKirill A. Shutemov } 32263bb97794SKirill A. Shutemov 32279118c0cbSJan Kara 32289118c0cbSJan Kara /** 32299118c0cbSJan Kara * finish_fault - finish page fault once we have prepared the page to fault 32309118c0cbSJan Kara * 32319118c0cbSJan Kara * @vmf: structure describing the fault 32329118c0cbSJan Kara * 32339118c0cbSJan Kara * This function handles all that is needed to finish a page fault once the 32349118c0cbSJan Kara * page to fault in is prepared. It handles locking of PTEs, inserts PTE for 32359118c0cbSJan Kara * given page, adds reverse page mapping, handles memcg charges and LRU 32369118c0cbSJan Kara * addition. The function returns 0 on success, VM_FAULT_ code in case of 32379118c0cbSJan Kara * error. 32389118c0cbSJan Kara * 32399118c0cbSJan Kara * The function expects the page to be locked and on success it consumes a 32409118c0cbSJan Kara * reference of a page being mapped (for the PTE which maps it). 32419118c0cbSJan Kara */ 32422b740303SSouptick Joarder vm_fault_t finish_fault(struct vm_fault *vmf) 32439118c0cbSJan Kara { 32449118c0cbSJan Kara struct page *page; 32452b740303SSouptick Joarder vm_fault_t ret = 0; 32469118c0cbSJan Kara 32479118c0cbSJan Kara /* Did we COW the page? */ 32489118c0cbSJan Kara if ((vmf->flags & FAULT_FLAG_WRITE) && 32499118c0cbSJan Kara !(vmf->vma->vm_flags & VM_SHARED)) 32509118c0cbSJan Kara page = vmf->cow_page; 32519118c0cbSJan Kara else 32529118c0cbSJan Kara page = vmf->page; 32536b31d595SMichal Hocko 32546b31d595SMichal Hocko /* 32556b31d595SMichal Hocko * check even for read faults because we might have lost our CoWed 32566b31d595SMichal Hocko * page 32576b31d595SMichal Hocko */ 32586b31d595SMichal Hocko if (!(vmf->vma->vm_flags & VM_SHARED)) 32596b31d595SMichal Hocko ret = check_stable_address_space(vmf->vma->vm_mm); 32606b31d595SMichal Hocko if (!ret) 32619118c0cbSJan Kara ret = alloc_set_pte(vmf, vmf->memcg, page); 32629118c0cbSJan Kara if (vmf->pte) 32639118c0cbSJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 32649118c0cbSJan Kara return ret; 32659118c0cbSJan Kara } 32669118c0cbSJan Kara 32673a91053aSKirill A. Shutemov static unsigned long fault_around_bytes __read_mostly = 32683a91053aSKirill A. Shutemov rounddown_pow_of_two(65536); 3269a9b0f861SKirill A. Shutemov 32701592eef0SKirill A. Shutemov #ifdef CONFIG_DEBUG_FS 3271a9b0f861SKirill A. Shutemov static int fault_around_bytes_get(void *data, u64 *val) 32721592eef0SKirill A. Shutemov { 3273a9b0f861SKirill A. Shutemov *val = fault_around_bytes; 32741592eef0SKirill A. Shutemov return 0; 32751592eef0SKirill A. Shutemov } 32761592eef0SKirill A. Shutemov 3277b4903d6eSAndrey Ryabinin /* 3278da391d64SWilliam Kucharski * fault_around_bytes must be rounded down to the nearest page order as it's 3279da391d64SWilliam Kucharski * what do_fault_around() expects to see. 3280b4903d6eSAndrey Ryabinin */ 3281a9b0f861SKirill A. Shutemov static int fault_around_bytes_set(void *data, u64 val) 32821592eef0SKirill A. Shutemov { 3283a9b0f861SKirill A. Shutemov if (val / PAGE_SIZE > PTRS_PER_PTE) 32841592eef0SKirill A. Shutemov return -EINVAL; 3285b4903d6eSAndrey Ryabinin if (val > PAGE_SIZE) 3286b4903d6eSAndrey Ryabinin fault_around_bytes = rounddown_pow_of_two(val); 3287b4903d6eSAndrey Ryabinin else 3288b4903d6eSAndrey Ryabinin fault_around_bytes = PAGE_SIZE; /* rounddown_pow_of_two(0) is undefined */ 32891592eef0SKirill A. Shutemov return 0; 32901592eef0SKirill A. Shutemov } 32910a1345f8SYevgen Pronenko DEFINE_DEBUGFS_ATTRIBUTE(fault_around_bytes_fops, 3292a9b0f861SKirill A. Shutemov fault_around_bytes_get, fault_around_bytes_set, "%llu\n"); 32931592eef0SKirill A. Shutemov 32941592eef0SKirill A. Shutemov static int __init fault_around_debugfs(void) 32951592eef0SKirill A. Shutemov { 32961592eef0SKirill A. Shutemov void *ret; 32971592eef0SKirill A. Shutemov 32980a1345f8SYevgen Pronenko ret = debugfs_create_file_unsafe("fault_around_bytes", 0644, NULL, NULL, 3299a9b0f861SKirill A. Shutemov &fault_around_bytes_fops); 33001592eef0SKirill A. Shutemov if (!ret) 3301a9b0f861SKirill A. Shutemov pr_warn("Failed to create fault_around_bytes in debugfs"); 33021592eef0SKirill A. Shutemov return 0; 33031592eef0SKirill A. Shutemov } 33041592eef0SKirill A. Shutemov late_initcall(fault_around_debugfs); 33051592eef0SKirill A. Shutemov #endif 33068c6e50b0SKirill A. Shutemov 33071fdb412bSKirill A. Shutemov /* 33081fdb412bSKirill A. Shutemov * do_fault_around() tries to map few pages around the fault address. The hope 33091fdb412bSKirill A. Shutemov * is that the pages will be needed soon and this will lower the number of 33101fdb412bSKirill A. Shutemov * faults to handle. 33111fdb412bSKirill A. Shutemov * 33121fdb412bSKirill A. Shutemov * It uses vm_ops->map_pages() to map the pages, which skips the page if it's 33131fdb412bSKirill A. Shutemov * not ready to be mapped: not up-to-date, locked, etc. 33141fdb412bSKirill A. Shutemov * 33151fdb412bSKirill A. Shutemov * This function is called with the page table lock taken. In the split ptlock 33161fdb412bSKirill A. Shutemov * case the page table lock only protects only those entries which belong to 33171fdb412bSKirill A. Shutemov * the page table corresponding to the fault address. 33181fdb412bSKirill A. Shutemov * 33191fdb412bSKirill A. Shutemov * This function doesn't cross the VMA boundaries, in order to call map_pages() 33201fdb412bSKirill A. Shutemov * only once. 33211fdb412bSKirill A. Shutemov * 3322da391d64SWilliam Kucharski * fault_around_bytes defines how many bytes we'll try to map. 3323da391d64SWilliam Kucharski * do_fault_around() expects it to be set to a power of two less than or equal 3324da391d64SWilliam Kucharski * to PTRS_PER_PTE. 33251fdb412bSKirill A. Shutemov * 3326da391d64SWilliam Kucharski * The virtual address of the area that we map is naturally aligned to 3327da391d64SWilliam Kucharski * fault_around_bytes rounded down to the machine page size 3328da391d64SWilliam Kucharski * (and therefore to page order). This way it's easier to guarantee 3329da391d64SWilliam Kucharski * that we don't cross page table boundaries. 33301fdb412bSKirill A. Shutemov */ 33312b740303SSouptick Joarder static vm_fault_t do_fault_around(struct vm_fault *vmf) 33328c6e50b0SKirill A. Shutemov { 333382b0f8c3SJan Kara unsigned long address = vmf->address, nr_pages, mask; 33340721ec8bSJan Kara pgoff_t start_pgoff = vmf->pgoff; 3335bae473a4SKirill A. Shutemov pgoff_t end_pgoff; 33362b740303SSouptick Joarder int off; 33372b740303SSouptick Joarder vm_fault_t ret = 0; 33388c6e50b0SKirill A. Shutemov 33394db0c3c2SJason Low nr_pages = READ_ONCE(fault_around_bytes) >> PAGE_SHIFT; 3340aecd6f44SKirill A. Shutemov mask = ~(nr_pages * PAGE_SIZE - 1) & PAGE_MASK; 3341aecd6f44SKirill A. Shutemov 334282b0f8c3SJan Kara vmf->address = max(address & mask, vmf->vma->vm_start); 334382b0f8c3SJan Kara off = ((address - vmf->address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1); 3344bae473a4SKirill A. Shutemov start_pgoff -= off; 33458c6e50b0SKirill A. Shutemov 33468c6e50b0SKirill A. Shutemov /* 3347da391d64SWilliam Kucharski * end_pgoff is either the end of the page table, the end of 3348da391d64SWilliam Kucharski * the vma or nr_pages from start_pgoff, depending what is nearest. 33498c6e50b0SKirill A. Shutemov */ 3350bae473a4SKirill A. Shutemov end_pgoff = start_pgoff - 335182b0f8c3SJan Kara ((vmf->address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) + 33528c6e50b0SKirill A. Shutemov PTRS_PER_PTE - 1; 335382b0f8c3SJan Kara end_pgoff = min3(end_pgoff, vma_pages(vmf->vma) + vmf->vma->vm_pgoff - 1, 3354bae473a4SKirill A. Shutemov start_pgoff + nr_pages - 1); 33558c6e50b0SKirill A. Shutemov 335682b0f8c3SJan Kara if (pmd_none(*vmf->pmd)) { 335782b0f8c3SJan Kara vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm, 335882b0f8c3SJan Kara vmf->address); 335982b0f8c3SJan Kara if (!vmf->prealloc_pte) 3360c5f88bd2SVegard Nossum goto out; 33617267ec00SKirill A. Shutemov smp_wmb(); /* See comment in __pte_alloc() */ 33628c6e50b0SKirill A. Shutemov } 33638c6e50b0SKirill A. Shutemov 336482b0f8c3SJan Kara vmf->vma->vm_ops->map_pages(vmf, start_pgoff, end_pgoff); 33657267ec00SKirill A. Shutemov 33667267ec00SKirill A. Shutemov /* Huge page is mapped? Page fault is solved */ 336782b0f8c3SJan Kara if (pmd_trans_huge(*vmf->pmd)) { 33687267ec00SKirill A. Shutemov ret = VM_FAULT_NOPAGE; 33697267ec00SKirill A. Shutemov goto out; 33708c6e50b0SKirill A. Shutemov } 33718c6e50b0SKirill A. Shutemov 33727267ec00SKirill A. Shutemov /* ->map_pages() haven't done anything useful. Cold page cache? */ 337382b0f8c3SJan Kara if (!vmf->pte) 33747267ec00SKirill A. Shutemov goto out; 33757267ec00SKirill A. Shutemov 33767267ec00SKirill A. Shutemov /* check if the page fault is solved */ 337782b0f8c3SJan Kara vmf->pte -= (vmf->address >> PAGE_SHIFT) - (address >> PAGE_SHIFT); 337882b0f8c3SJan Kara if (!pte_none(*vmf->pte)) 33797267ec00SKirill A. Shutemov ret = VM_FAULT_NOPAGE; 338082b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 33817267ec00SKirill A. Shutemov out: 338282b0f8c3SJan Kara vmf->address = address; 338382b0f8c3SJan Kara vmf->pte = NULL; 33847267ec00SKirill A. Shutemov return ret; 33857267ec00SKirill A. Shutemov } 33867267ec00SKirill A. Shutemov 33872b740303SSouptick Joarder static vm_fault_t do_read_fault(struct vm_fault *vmf) 3388e655fb29SKirill A. Shutemov { 338982b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 33902b740303SSouptick Joarder vm_fault_t ret = 0; 33918c6e50b0SKirill A. Shutemov 33928c6e50b0SKirill A. Shutemov /* 33938c6e50b0SKirill A. Shutemov * Let's call ->map_pages() first and use ->fault() as fallback 33948c6e50b0SKirill A. Shutemov * if page by the offset is not ready to be mapped (cold cache or 33958c6e50b0SKirill A. Shutemov * something). 33968c6e50b0SKirill A. Shutemov */ 33979b4bdd2fSKirill A. Shutemov if (vma->vm_ops->map_pages && fault_around_bytes >> PAGE_SHIFT > 1) { 33980721ec8bSJan Kara ret = do_fault_around(vmf); 33997267ec00SKirill A. Shutemov if (ret) 34007267ec00SKirill A. Shutemov return ret; 34018c6e50b0SKirill A. Shutemov } 3402e655fb29SKirill A. Shutemov 3403936ca80dSJan Kara ret = __do_fault(vmf); 3404e655fb29SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3405e655fb29SKirill A. Shutemov return ret; 3406e655fb29SKirill A. Shutemov 34079118c0cbSJan Kara ret |= finish_fault(vmf); 3408936ca80dSJan Kara unlock_page(vmf->page); 34097267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3410936ca80dSJan Kara put_page(vmf->page); 3411e655fb29SKirill A. Shutemov return ret; 3412e655fb29SKirill A. Shutemov } 3413e655fb29SKirill A. Shutemov 34142b740303SSouptick Joarder static vm_fault_t do_cow_fault(struct vm_fault *vmf) 3415ec47c3b9SKirill A. Shutemov { 341682b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 34172b740303SSouptick Joarder vm_fault_t ret; 3418ec47c3b9SKirill A. Shutemov 3419ec47c3b9SKirill A. Shutemov if (unlikely(anon_vma_prepare(vma))) 3420ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 3421ec47c3b9SKirill A. Shutemov 3422936ca80dSJan Kara vmf->cow_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vmf->address); 3423936ca80dSJan Kara if (!vmf->cow_page) 3424ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 3425ec47c3b9SKirill A. Shutemov 34262cf85583STejun Heo if (mem_cgroup_try_charge_delay(vmf->cow_page, vma->vm_mm, GFP_KERNEL, 34273917048dSJan Kara &vmf->memcg, false)) { 3428936ca80dSJan Kara put_page(vmf->cow_page); 3429ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 3430ec47c3b9SKirill A. Shutemov } 3431ec47c3b9SKirill A. Shutemov 3432936ca80dSJan Kara ret = __do_fault(vmf); 3433ec47c3b9SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3434ec47c3b9SKirill A. Shutemov goto uncharge_out; 34353917048dSJan Kara if (ret & VM_FAULT_DONE_COW) 34363917048dSJan Kara return ret; 3437ec47c3b9SKirill A. Shutemov 3438936ca80dSJan Kara copy_user_highpage(vmf->cow_page, vmf->page, vmf->address, vma); 3439936ca80dSJan Kara __SetPageUptodate(vmf->cow_page); 3440ec47c3b9SKirill A. Shutemov 34419118c0cbSJan Kara ret |= finish_fault(vmf); 3442936ca80dSJan Kara unlock_page(vmf->page); 3443936ca80dSJan Kara put_page(vmf->page); 34447267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 34457267ec00SKirill A. Shutemov goto uncharge_out; 3446ec47c3b9SKirill A. Shutemov return ret; 3447ec47c3b9SKirill A. Shutemov uncharge_out: 34483917048dSJan Kara mem_cgroup_cancel_charge(vmf->cow_page, vmf->memcg, false); 3449936ca80dSJan Kara put_page(vmf->cow_page); 3450ec47c3b9SKirill A. Shutemov return ret; 3451ec47c3b9SKirill A. Shutemov } 3452ec47c3b9SKirill A. Shutemov 34532b740303SSouptick Joarder static vm_fault_t do_shared_fault(struct vm_fault *vmf) 34541da177e4SLinus Torvalds { 345582b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 34562b740303SSouptick Joarder vm_fault_t ret, tmp; 34571d65f86dSKAMEZAWA Hiroyuki 3458936ca80dSJan Kara ret = __do_fault(vmf); 34597eae74afSKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3460f0c6d4d2SKirill A. Shutemov return ret; 34611da177e4SLinus Torvalds 34621da177e4SLinus Torvalds /* 3463f0c6d4d2SKirill A. Shutemov * Check if the backing address space wants to know that the page is 3464f0c6d4d2SKirill A. Shutemov * about to become writable 34651da177e4SLinus Torvalds */ 3466fb09a464SKirill A. Shutemov if (vma->vm_ops->page_mkwrite) { 3467936ca80dSJan Kara unlock_page(vmf->page); 346838b8cb7fSJan Kara tmp = do_page_mkwrite(vmf); 3469fb09a464SKirill A. Shutemov if (unlikely(!tmp || 3470fb09a464SKirill A. Shutemov (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { 3471936ca80dSJan Kara put_page(vmf->page); 3472f0c6d4d2SKirill A. Shutemov return tmp; 347369676147SMark Fasheh } 3474d0217ac0SNick Piggin } 3475fb09a464SKirill A. Shutemov 34769118c0cbSJan Kara ret |= finish_fault(vmf); 34777267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | 34787267ec00SKirill A. Shutemov VM_FAULT_RETRY))) { 3479936ca80dSJan Kara unlock_page(vmf->page); 3480936ca80dSJan Kara put_page(vmf->page); 3481f0c6d4d2SKirill A. Shutemov return ret; 34829637a5efSDavid Howells } 3483d00806b1SNick Piggin 348497ba0c2bSJan Kara fault_dirty_shared_page(vma, vmf->page); 3485b827e496SNick Piggin return ret; 348654cb8821SNick Piggin } 3487d00806b1SNick Piggin 34889a95f3cfSPaul Cassella /* 34899a95f3cfSPaul Cassella * We enter with non-exclusive mmap_sem (to exclude vma changes, 34909a95f3cfSPaul Cassella * but allow concurrent faults). 34919a95f3cfSPaul Cassella * The mmap_sem may have been released depending on flags and our 34929a95f3cfSPaul Cassella * return value. See filemap_fault() and __lock_page_or_retry(). 34939a95f3cfSPaul Cassella */ 34942b740303SSouptick Joarder static vm_fault_t do_fault(struct vm_fault *vmf) 349554cb8821SNick Piggin { 349682b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 34972b740303SSouptick Joarder vm_fault_t ret; 349854cb8821SNick Piggin 34996b7339f4SKirill A. Shutemov /* The VMA was not fully populated on mmap() or missing VM_DONTEXPAND */ 35006b7339f4SKirill A. Shutemov if (!vma->vm_ops->fault) 3501b0b9b3dfSHugh Dickins ret = VM_FAULT_SIGBUS; 3502b0b9b3dfSHugh Dickins else if (!(vmf->flags & FAULT_FLAG_WRITE)) 3503b0b9b3dfSHugh Dickins ret = do_read_fault(vmf); 3504b0b9b3dfSHugh Dickins else if (!(vma->vm_flags & VM_SHARED)) 3505b0b9b3dfSHugh Dickins ret = do_cow_fault(vmf); 3506b0b9b3dfSHugh Dickins else 3507b0b9b3dfSHugh Dickins ret = do_shared_fault(vmf); 3508b0b9b3dfSHugh Dickins 3509b0b9b3dfSHugh Dickins /* preallocated pagetable is unused: free it */ 3510b0b9b3dfSHugh Dickins if (vmf->prealloc_pte) { 3511b0b9b3dfSHugh Dickins pte_free(vma->vm_mm, vmf->prealloc_pte); 35127f2b6ce8STobin C Harding vmf->prealloc_pte = NULL; 3513b0b9b3dfSHugh Dickins } 3514b0b9b3dfSHugh Dickins return ret; 351554cb8821SNick Piggin } 351654cb8821SNick Piggin 3517b19a9939SRashika Kheria static int numa_migrate_prep(struct page *page, struct vm_area_struct *vma, 351804bb2f94SRik van Riel unsigned long addr, int page_nid, 351904bb2f94SRik van Riel int *flags) 35209532fec1SMel Gorman { 35219532fec1SMel Gorman get_page(page); 35229532fec1SMel Gorman 35239532fec1SMel Gorman count_vm_numa_event(NUMA_HINT_FAULTS); 352404bb2f94SRik van Riel if (page_nid == numa_node_id()) { 35259532fec1SMel Gorman count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL); 352604bb2f94SRik van Riel *flags |= TNF_FAULT_LOCAL; 352704bb2f94SRik van Riel } 35289532fec1SMel Gorman 35299532fec1SMel Gorman return mpol_misplaced(page, vma, addr); 35309532fec1SMel Gorman } 35319532fec1SMel Gorman 35322b740303SSouptick Joarder static vm_fault_t do_numa_page(struct vm_fault *vmf) 3533d10e63f2SMel Gorman { 353482b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 35354daae3b4SMel Gorman struct page *page = NULL; 35368191acbdSMel Gorman int page_nid = -1; 353790572890SPeter Zijlstra int last_cpupid; 3538cbee9f88SPeter Zijlstra int target_nid; 3539b8593bfdSMel Gorman bool migrated = false; 3540cee216a6SAneesh Kumar K.V pte_t pte; 3541288bc549SAneesh Kumar K.V bool was_writable = pte_savedwrite(vmf->orig_pte); 35426688cc05SPeter Zijlstra int flags = 0; 3543d10e63f2SMel Gorman 3544d10e63f2SMel Gorman /* 3545d10e63f2SMel Gorman * The "pte" at this point cannot be used safely without 3546d10e63f2SMel Gorman * validation through pte_unmap_same(). It's of NUMA type but 3547d10e63f2SMel Gorman * the pfn may be screwed if the read is non atomic. 3548d10e63f2SMel Gorman */ 354982b0f8c3SJan Kara vmf->ptl = pte_lockptr(vma->vm_mm, vmf->pmd); 355082b0f8c3SJan Kara spin_lock(vmf->ptl); 3551cee216a6SAneesh Kumar K.V if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) { 355282b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 35534daae3b4SMel Gorman goto out; 35544daae3b4SMel Gorman } 35554daae3b4SMel Gorman 3556cee216a6SAneesh Kumar K.V /* 3557cee216a6SAneesh Kumar K.V * Make it present again, Depending on how arch implementes non 3558cee216a6SAneesh Kumar K.V * accessible ptes, some can allow access by kernel mode. 3559cee216a6SAneesh Kumar K.V */ 3560cee216a6SAneesh Kumar K.V pte = ptep_modify_prot_start(vma->vm_mm, vmf->address, vmf->pte); 35614d942466SMel Gorman pte = pte_modify(pte, vma->vm_page_prot); 35624d942466SMel Gorman pte = pte_mkyoung(pte); 3563b191f9b1SMel Gorman if (was_writable) 3564b191f9b1SMel Gorman pte = pte_mkwrite(pte); 3565cee216a6SAneesh Kumar K.V ptep_modify_prot_commit(vma->vm_mm, vmf->address, vmf->pte, pte); 356682b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 3567d10e63f2SMel Gorman 356882b0f8c3SJan Kara page = vm_normal_page(vma, vmf->address, pte); 3569d10e63f2SMel Gorman if (!page) { 357082b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3571d10e63f2SMel Gorman return 0; 3572d10e63f2SMel Gorman } 3573d10e63f2SMel Gorman 3574e81c4802SKirill A. Shutemov /* TODO: handle PTE-mapped THP */ 3575e81c4802SKirill A. Shutemov if (PageCompound(page)) { 357682b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3577e81c4802SKirill A. Shutemov return 0; 3578e81c4802SKirill A. Shutemov } 3579e81c4802SKirill A. Shutemov 35806688cc05SPeter Zijlstra /* 3581bea66fbdSMel Gorman * Avoid grouping on RO pages in general. RO pages shouldn't hurt as 3582bea66fbdSMel Gorman * much anyway since they can be in shared cache state. This misses 3583bea66fbdSMel Gorman * the case where a mapping is writable but the process never writes 3584bea66fbdSMel Gorman * to it but pte_write gets cleared during protection updates and 3585bea66fbdSMel Gorman * pte_dirty has unpredictable behaviour between PTE scan updates, 3586bea66fbdSMel Gorman * background writeback, dirty balancing and application behaviour. 35876688cc05SPeter Zijlstra */ 3588d59dc7bcSRik van Riel if (!pte_write(pte)) 35896688cc05SPeter Zijlstra flags |= TNF_NO_GROUP; 35906688cc05SPeter Zijlstra 3591dabe1d99SRik van Riel /* 3592dabe1d99SRik van Riel * Flag if the page is shared between multiple address spaces. This 3593dabe1d99SRik van Riel * is later used when determining whether to group tasks together 3594dabe1d99SRik van Riel */ 3595dabe1d99SRik van Riel if (page_mapcount(page) > 1 && (vma->vm_flags & VM_SHARED)) 3596dabe1d99SRik van Riel flags |= TNF_SHARED; 3597dabe1d99SRik van Riel 359890572890SPeter Zijlstra last_cpupid = page_cpupid_last(page); 35998191acbdSMel Gorman page_nid = page_to_nid(page); 360082b0f8c3SJan Kara target_nid = numa_migrate_prep(page, vma, vmf->address, page_nid, 3601bae473a4SKirill A. Shutemov &flags); 360282b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 36034daae3b4SMel Gorman if (target_nid == -1) { 36044daae3b4SMel Gorman put_page(page); 36054daae3b4SMel Gorman goto out; 36064daae3b4SMel Gorman } 36074daae3b4SMel Gorman 36084daae3b4SMel Gorman /* Migrate to the requested node */ 36091bc115d8SMel Gorman migrated = migrate_misplaced_page(page, vma, target_nid); 36106688cc05SPeter Zijlstra if (migrated) { 36118191acbdSMel Gorman page_nid = target_nid; 36126688cc05SPeter Zijlstra flags |= TNF_MIGRATED; 3613074c2381SMel Gorman } else 3614074c2381SMel Gorman flags |= TNF_MIGRATE_FAIL; 36154daae3b4SMel Gorman 36164daae3b4SMel Gorman out: 36178191acbdSMel Gorman if (page_nid != -1) 36186688cc05SPeter Zijlstra task_numa_fault(last_cpupid, page_nid, 1, flags); 3619d10e63f2SMel Gorman return 0; 3620d10e63f2SMel Gorman } 3621d10e63f2SMel Gorman 36222b740303SSouptick Joarder static inline vm_fault_t create_huge_pmd(struct vm_fault *vmf) 3623b96375f7SMatthew Wilcox { 3624f4200391SDave Jiang if (vma_is_anonymous(vmf->vma)) 362582b0f8c3SJan Kara return do_huge_pmd_anonymous_page(vmf); 3626a2d58167SDave Jiang if (vmf->vma->vm_ops->huge_fault) 3627c791ace1SDave Jiang return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD); 3628b96375f7SMatthew Wilcox return VM_FAULT_FALLBACK; 3629b96375f7SMatthew Wilcox } 3630b96375f7SMatthew Wilcox 3631183f24aaSGeert Uytterhoeven /* `inline' is required to avoid gcc 4.1.2 build error */ 36322b740303SSouptick Joarder static inline vm_fault_t wp_huge_pmd(struct vm_fault *vmf, pmd_t orig_pmd) 3633b96375f7SMatthew Wilcox { 363482b0f8c3SJan Kara if (vma_is_anonymous(vmf->vma)) 363582b0f8c3SJan Kara return do_huge_pmd_wp_page(vmf, orig_pmd); 3636a2d58167SDave Jiang if (vmf->vma->vm_ops->huge_fault) 3637c791ace1SDave Jiang return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD); 3638af9e4d5fSKirill A. Shutemov 3639af9e4d5fSKirill A. Shutemov /* COW handled on pte level: split pmd */ 364082b0f8c3SJan Kara VM_BUG_ON_VMA(vmf->vma->vm_flags & VM_SHARED, vmf->vma); 364182b0f8c3SJan Kara __split_huge_pmd(vmf->vma, vmf->pmd, vmf->address, false, NULL); 3642af9e4d5fSKirill A. Shutemov 3643b96375f7SMatthew Wilcox return VM_FAULT_FALLBACK; 3644b96375f7SMatthew Wilcox } 3645b96375f7SMatthew Wilcox 364638e08854SLorenzo Stoakes static inline bool vma_is_accessible(struct vm_area_struct *vma) 364738e08854SLorenzo Stoakes { 364838e08854SLorenzo Stoakes return vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE); 364938e08854SLorenzo Stoakes } 365038e08854SLorenzo Stoakes 36512b740303SSouptick Joarder static vm_fault_t create_huge_pud(struct vm_fault *vmf) 3652a00cc7d9SMatthew Wilcox { 3653a00cc7d9SMatthew Wilcox #ifdef CONFIG_TRANSPARENT_HUGEPAGE 3654a00cc7d9SMatthew Wilcox /* No support for anonymous transparent PUD pages yet */ 3655a00cc7d9SMatthew Wilcox if (vma_is_anonymous(vmf->vma)) 3656a00cc7d9SMatthew Wilcox return VM_FAULT_FALLBACK; 3657a00cc7d9SMatthew Wilcox if (vmf->vma->vm_ops->huge_fault) 3658c791ace1SDave Jiang return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD); 3659a00cc7d9SMatthew Wilcox #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 3660a00cc7d9SMatthew Wilcox return VM_FAULT_FALLBACK; 3661a00cc7d9SMatthew Wilcox } 3662a00cc7d9SMatthew Wilcox 36632b740303SSouptick Joarder static vm_fault_t wp_huge_pud(struct vm_fault *vmf, pud_t orig_pud) 3664a00cc7d9SMatthew Wilcox { 3665a00cc7d9SMatthew Wilcox #ifdef CONFIG_TRANSPARENT_HUGEPAGE 3666a00cc7d9SMatthew Wilcox /* No support for anonymous transparent PUD pages yet */ 3667a00cc7d9SMatthew Wilcox if (vma_is_anonymous(vmf->vma)) 3668a00cc7d9SMatthew Wilcox return VM_FAULT_FALLBACK; 3669a00cc7d9SMatthew Wilcox if (vmf->vma->vm_ops->huge_fault) 3670c791ace1SDave Jiang return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD); 3671a00cc7d9SMatthew Wilcox #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 3672a00cc7d9SMatthew Wilcox return VM_FAULT_FALLBACK; 3673a00cc7d9SMatthew Wilcox } 3674a00cc7d9SMatthew Wilcox 36751da177e4SLinus Torvalds /* 36761da177e4SLinus Torvalds * These routines also need to handle stuff like marking pages dirty 36771da177e4SLinus Torvalds * and/or accessed for architectures that don't do it in hardware (most 36781da177e4SLinus Torvalds * RISC architectures). The early dirtying is also good on the i386. 36791da177e4SLinus Torvalds * 36801da177e4SLinus Torvalds * There is also a hook called "update_mmu_cache()" that architectures 36811da177e4SLinus Torvalds * with external mmu caches can use to update those (ie the Sparc or 36821da177e4SLinus Torvalds * PowerPC hashed page tables that act as extended TLBs). 36831da177e4SLinus Torvalds * 36847267ec00SKirill A. Shutemov * We enter with non-exclusive mmap_sem (to exclude vma changes, but allow 36857267ec00SKirill A. Shutemov * concurrent faults). 36869a95f3cfSPaul Cassella * 36877267ec00SKirill A. Shutemov * The mmap_sem may have been released depending on flags and our return value. 36887267ec00SKirill A. Shutemov * See filemap_fault() and __lock_page_or_retry(). 36891da177e4SLinus Torvalds */ 36902b740303SSouptick Joarder static vm_fault_t handle_pte_fault(struct vm_fault *vmf) 36911da177e4SLinus Torvalds { 36921da177e4SLinus Torvalds pte_t entry; 36931da177e4SLinus Torvalds 369482b0f8c3SJan Kara if (unlikely(pmd_none(*vmf->pmd))) { 36957267ec00SKirill A. Shutemov /* 36967267ec00SKirill A. Shutemov * Leave __pte_alloc() until later: because vm_ops->fault may 36977267ec00SKirill A. Shutemov * want to allocate huge page, and if we expose page table 36987267ec00SKirill A. Shutemov * for an instant, it will be difficult to retract from 36997267ec00SKirill A. Shutemov * concurrent faults and from rmap lookups. 37007267ec00SKirill A. Shutemov */ 370182b0f8c3SJan Kara vmf->pte = NULL; 37027267ec00SKirill A. Shutemov } else { 37037267ec00SKirill A. Shutemov /* See comment in pte_alloc_one_map() */ 3704d0f0931dSRoss Zwisler if (pmd_devmap_trans_unstable(vmf->pmd)) 37057267ec00SKirill A. Shutemov return 0; 37067267ec00SKirill A. Shutemov /* 37077267ec00SKirill A. Shutemov * A regular pmd is established and it can't morph into a huge 37087267ec00SKirill A. Shutemov * pmd from under us anymore at this point because we hold the 37097267ec00SKirill A. Shutemov * mmap_sem read mode and khugepaged takes it in write mode. 37107267ec00SKirill A. Shutemov * So now it's safe to run pte_offset_map(). 37117267ec00SKirill A. Shutemov */ 371282b0f8c3SJan Kara vmf->pte = pte_offset_map(vmf->pmd, vmf->address); 37132994302bSJan Kara vmf->orig_pte = *vmf->pte; 37147267ec00SKirill A. Shutemov 3715e37c6982SChristian Borntraeger /* 3716e37c6982SChristian Borntraeger * some architectures can have larger ptes than wordsize, 37177267ec00SKirill A. Shutemov * e.g.ppc44x-defconfig has CONFIG_PTE_64BIT=y and 3718b03a0fe0SPaul E. McKenney * CONFIG_32BIT=y, so READ_ONCE cannot guarantee atomic 3719b03a0fe0SPaul E. McKenney * accesses. The code below just needs a consistent view 3720b03a0fe0SPaul E. McKenney * for the ifs and we later double check anyway with the 37217267ec00SKirill A. Shutemov * ptl lock held. So here a barrier will do. 3722e37c6982SChristian Borntraeger */ 3723e37c6982SChristian Borntraeger barrier(); 37242994302bSJan Kara if (pte_none(vmf->orig_pte)) { 372582b0f8c3SJan Kara pte_unmap(vmf->pte); 372682b0f8c3SJan Kara vmf->pte = NULL; 37277267ec00SKirill A. Shutemov } 37287267ec00SKirill A. Shutemov } 37297267ec00SKirill A. Shutemov 373082b0f8c3SJan Kara if (!vmf->pte) { 373182b0f8c3SJan Kara if (vma_is_anonymous(vmf->vma)) 373282b0f8c3SJan Kara return do_anonymous_page(vmf); 3733b5330628SOleg Nesterov else 373482b0f8c3SJan Kara return do_fault(vmf); 373565500d23SHugh Dickins } 37367267ec00SKirill A. Shutemov 37372994302bSJan Kara if (!pte_present(vmf->orig_pte)) 37382994302bSJan Kara return do_swap_page(vmf); 37391da177e4SLinus Torvalds 37402994302bSJan Kara if (pte_protnone(vmf->orig_pte) && vma_is_accessible(vmf->vma)) 37412994302bSJan Kara return do_numa_page(vmf); 3742d10e63f2SMel Gorman 374382b0f8c3SJan Kara vmf->ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd); 374482b0f8c3SJan Kara spin_lock(vmf->ptl); 37452994302bSJan Kara entry = vmf->orig_pte; 374682b0f8c3SJan Kara if (unlikely(!pte_same(*vmf->pte, entry))) 37478f4e2101SHugh Dickins goto unlock; 374882b0f8c3SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) { 3749f6f37321SLinus Torvalds if (!pte_write(entry)) 37502994302bSJan Kara return do_wp_page(vmf); 37511da177e4SLinus Torvalds entry = pte_mkdirty(entry); 37521da177e4SLinus Torvalds } 37531da177e4SLinus Torvalds entry = pte_mkyoung(entry); 375482b0f8c3SJan Kara if (ptep_set_access_flags(vmf->vma, vmf->address, vmf->pte, entry, 375582b0f8c3SJan Kara vmf->flags & FAULT_FLAG_WRITE)) { 375682b0f8c3SJan Kara update_mmu_cache(vmf->vma, vmf->address, vmf->pte); 37571a44e149SAndrea Arcangeli } else { 37581a44e149SAndrea Arcangeli /* 37591a44e149SAndrea Arcangeli * This is needed only for protection faults but the arch code 37601a44e149SAndrea Arcangeli * is not yet telling us if this is a protection fault or not. 37611a44e149SAndrea Arcangeli * This still avoids useless tlb flushes for .text page faults 37621a44e149SAndrea Arcangeli * with threads. 37631a44e149SAndrea Arcangeli */ 376482b0f8c3SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) 376582b0f8c3SJan Kara flush_tlb_fix_spurious_fault(vmf->vma, vmf->address); 37661a44e149SAndrea Arcangeli } 37678f4e2101SHugh Dickins unlock: 376882b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 376983c54070SNick Piggin return 0; 37701da177e4SLinus Torvalds } 37711da177e4SLinus Torvalds 37721da177e4SLinus Torvalds /* 37731da177e4SLinus Torvalds * By the time we get here, we already hold the mm semaphore 37749a95f3cfSPaul Cassella * 37759a95f3cfSPaul Cassella * The mmap_sem may have been released depending on flags and our 37769a95f3cfSPaul Cassella * return value. See filemap_fault() and __lock_page_or_retry(). 37771da177e4SLinus Torvalds */ 37782b740303SSouptick Joarder static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma, 37792b740303SSouptick Joarder unsigned long address, unsigned int flags) 37801da177e4SLinus Torvalds { 378182b0f8c3SJan Kara struct vm_fault vmf = { 3782bae473a4SKirill A. Shutemov .vma = vma, 37831a29d85eSJan Kara .address = address & PAGE_MASK, 3784bae473a4SKirill A. Shutemov .flags = flags, 37850721ec8bSJan Kara .pgoff = linear_page_index(vma, address), 3786667240e0SJan Kara .gfp_mask = __get_fault_gfp_mask(vma), 3787bae473a4SKirill A. Shutemov }; 3788fde26bedSAnshuman Khandual unsigned int dirty = flags & FAULT_FLAG_WRITE; 3789dcddffd4SKirill A. Shutemov struct mm_struct *mm = vma->vm_mm; 37901da177e4SLinus Torvalds pgd_t *pgd; 3791c2febafcSKirill A. Shutemov p4d_t *p4d; 37922b740303SSouptick Joarder vm_fault_t ret; 37931da177e4SLinus Torvalds 37941da177e4SLinus Torvalds pgd = pgd_offset(mm, address); 3795c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, address); 3796c2febafcSKirill A. Shutemov if (!p4d) 3797c2febafcSKirill A. Shutemov return VM_FAULT_OOM; 3798a00cc7d9SMatthew Wilcox 3799c2febafcSKirill A. Shutemov vmf.pud = pud_alloc(mm, p4d, address); 3800a00cc7d9SMatthew Wilcox if (!vmf.pud) 3801c74df32cSHugh Dickins return VM_FAULT_OOM; 3802a00cc7d9SMatthew Wilcox if (pud_none(*vmf.pud) && transparent_hugepage_enabled(vma)) { 3803a00cc7d9SMatthew Wilcox ret = create_huge_pud(&vmf); 3804a00cc7d9SMatthew Wilcox if (!(ret & VM_FAULT_FALLBACK)) 3805a00cc7d9SMatthew Wilcox return ret; 3806a00cc7d9SMatthew Wilcox } else { 3807a00cc7d9SMatthew Wilcox pud_t orig_pud = *vmf.pud; 3808a00cc7d9SMatthew Wilcox 3809a00cc7d9SMatthew Wilcox barrier(); 3810a00cc7d9SMatthew Wilcox if (pud_trans_huge(orig_pud) || pud_devmap(orig_pud)) { 3811a00cc7d9SMatthew Wilcox 3812a00cc7d9SMatthew Wilcox /* NUMA case for anonymous PUDs would go here */ 3813a00cc7d9SMatthew Wilcox 3814f6f37321SLinus Torvalds if (dirty && !pud_write(orig_pud)) { 3815a00cc7d9SMatthew Wilcox ret = wp_huge_pud(&vmf, orig_pud); 3816a00cc7d9SMatthew Wilcox if (!(ret & VM_FAULT_FALLBACK)) 3817a00cc7d9SMatthew Wilcox return ret; 3818a00cc7d9SMatthew Wilcox } else { 3819a00cc7d9SMatthew Wilcox huge_pud_set_accessed(&vmf, orig_pud); 3820a00cc7d9SMatthew Wilcox return 0; 3821a00cc7d9SMatthew Wilcox } 3822a00cc7d9SMatthew Wilcox } 3823a00cc7d9SMatthew Wilcox } 3824a00cc7d9SMatthew Wilcox 3825a00cc7d9SMatthew Wilcox vmf.pmd = pmd_alloc(mm, vmf.pud, address); 382682b0f8c3SJan Kara if (!vmf.pmd) 3827c74df32cSHugh Dickins return VM_FAULT_OOM; 382882b0f8c3SJan Kara if (pmd_none(*vmf.pmd) && transparent_hugepage_enabled(vma)) { 3829a2d58167SDave Jiang ret = create_huge_pmd(&vmf); 3830c0292554SKirill A. Shutemov if (!(ret & VM_FAULT_FALLBACK)) 3831c0292554SKirill A. Shutemov return ret; 383271e3aac0SAndrea Arcangeli } else { 383382b0f8c3SJan Kara pmd_t orig_pmd = *vmf.pmd; 38341f1d06c3SDavid Rientjes 383571e3aac0SAndrea Arcangeli barrier(); 383684c3fc4eSZi Yan if (unlikely(is_swap_pmd(orig_pmd))) { 383784c3fc4eSZi Yan VM_BUG_ON(thp_migration_supported() && 383884c3fc4eSZi Yan !is_pmd_migration_entry(orig_pmd)); 383984c3fc4eSZi Yan if (is_pmd_migration_entry(orig_pmd)) 384084c3fc4eSZi Yan pmd_migration_entry_wait(mm, vmf.pmd); 384184c3fc4eSZi Yan return 0; 384284c3fc4eSZi Yan } 38435c7fb56eSDan Williams if (pmd_trans_huge(orig_pmd) || pmd_devmap(orig_pmd)) { 384438e08854SLorenzo Stoakes if (pmd_protnone(orig_pmd) && vma_is_accessible(vma)) 384582b0f8c3SJan Kara return do_huge_pmd_numa_page(&vmf, orig_pmd); 3846d10e63f2SMel Gorman 3847f6f37321SLinus Torvalds if (dirty && !pmd_write(orig_pmd)) { 384882b0f8c3SJan Kara ret = wp_huge_pmd(&vmf, orig_pmd); 38499845cbbdSKirill A. Shutemov if (!(ret & VM_FAULT_FALLBACK)) 38501f1d06c3SDavid Rientjes return ret; 3851a1dd450bSWill Deacon } else { 385282b0f8c3SJan Kara huge_pmd_set_accessed(&vmf, orig_pmd); 385371e3aac0SAndrea Arcangeli return 0; 385471e3aac0SAndrea Arcangeli } 385571e3aac0SAndrea Arcangeli } 38569845cbbdSKirill A. Shutemov } 385771e3aac0SAndrea Arcangeli 385882b0f8c3SJan Kara return handle_pte_fault(&vmf); 38591da177e4SLinus Torvalds } 38601da177e4SLinus Torvalds 38619a95f3cfSPaul Cassella /* 38629a95f3cfSPaul Cassella * By the time we get here, we already hold the mm semaphore 38639a95f3cfSPaul Cassella * 38649a95f3cfSPaul Cassella * The mmap_sem may have been released depending on flags and our 38659a95f3cfSPaul Cassella * return value. See filemap_fault() and __lock_page_or_retry(). 38669a95f3cfSPaul Cassella */ 38672b740303SSouptick Joarder vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address, 3868dcddffd4SKirill A. Shutemov unsigned int flags) 3869519e5247SJohannes Weiner { 38702b740303SSouptick Joarder vm_fault_t ret; 3871519e5247SJohannes Weiner 3872519e5247SJohannes Weiner __set_current_state(TASK_RUNNING); 3873519e5247SJohannes Weiner 3874519e5247SJohannes Weiner count_vm_event(PGFAULT); 38752262185cSRoman Gushchin count_memcg_event_mm(vma->vm_mm, PGFAULT); 3876519e5247SJohannes Weiner 3877519e5247SJohannes Weiner /* do counter updates before entering really critical section. */ 3878519e5247SJohannes Weiner check_sync_rss_stat(current); 3879519e5247SJohannes Weiner 3880de0c799bSLaurent Dufour if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE, 3881de0c799bSLaurent Dufour flags & FAULT_FLAG_INSTRUCTION, 3882de0c799bSLaurent Dufour flags & FAULT_FLAG_REMOTE)) 3883de0c799bSLaurent Dufour return VM_FAULT_SIGSEGV; 3884de0c799bSLaurent Dufour 3885519e5247SJohannes Weiner /* 3886519e5247SJohannes Weiner * Enable the memcg OOM handling for faults triggered in user 3887519e5247SJohannes Weiner * space. Kernel faults are handled more gracefully. 3888519e5247SJohannes Weiner */ 3889519e5247SJohannes Weiner if (flags & FAULT_FLAG_USER) 389029ef680aSMichal Hocko mem_cgroup_enter_user_fault(); 3891519e5247SJohannes Weiner 3892bae473a4SKirill A. Shutemov if (unlikely(is_vm_hugetlb_page(vma))) 3893bae473a4SKirill A. Shutemov ret = hugetlb_fault(vma->vm_mm, vma, address, flags); 3894bae473a4SKirill A. Shutemov else 3895dcddffd4SKirill A. Shutemov ret = __handle_mm_fault(vma, address, flags); 3896519e5247SJohannes Weiner 389749426420SJohannes Weiner if (flags & FAULT_FLAG_USER) { 389829ef680aSMichal Hocko mem_cgroup_exit_user_fault(); 389949426420SJohannes Weiner /* 390049426420SJohannes Weiner * The task may have entered a memcg OOM situation but 390149426420SJohannes Weiner * if the allocation error was handled gracefully (no 390249426420SJohannes Weiner * VM_FAULT_OOM), there is no need to kill anything. 390349426420SJohannes Weiner * Just clean up the OOM state peacefully. 390449426420SJohannes Weiner */ 390549426420SJohannes Weiner if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM)) 390649426420SJohannes Weiner mem_cgroup_oom_synchronize(false); 390749426420SJohannes Weiner } 39083812c8c8SJohannes Weiner 3909519e5247SJohannes Weiner return ret; 3910519e5247SJohannes Weiner } 3911e1d6d01aSJesse Barnes EXPORT_SYMBOL_GPL(handle_mm_fault); 3912519e5247SJohannes Weiner 391390eceff1SKirill A. Shutemov #ifndef __PAGETABLE_P4D_FOLDED 391490eceff1SKirill A. Shutemov /* 391590eceff1SKirill A. Shutemov * Allocate p4d page table. 391690eceff1SKirill A. Shutemov * We've already handled the fast-path in-line. 391790eceff1SKirill A. Shutemov */ 391890eceff1SKirill A. Shutemov int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address) 391990eceff1SKirill A. Shutemov { 392090eceff1SKirill A. Shutemov p4d_t *new = p4d_alloc_one(mm, address); 392190eceff1SKirill A. Shutemov if (!new) 392290eceff1SKirill A. Shutemov return -ENOMEM; 392390eceff1SKirill A. Shutemov 392490eceff1SKirill A. Shutemov smp_wmb(); /* See comment in __pte_alloc */ 392590eceff1SKirill A. Shutemov 392690eceff1SKirill A. Shutemov spin_lock(&mm->page_table_lock); 392790eceff1SKirill A. Shutemov if (pgd_present(*pgd)) /* Another has populated it */ 392890eceff1SKirill A. Shutemov p4d_free(mm, new); 392990eceff1SKirill A. Shutemov else 393090eceff1SKirill A. Shutemov pgd_populate(mm, pgd, new); 393190eceff1SKirill A. Shutemov spin_unlock(&mm->page_table_lock); 393290eceff1SKirill A. Shutemov return 0; 393390eceff1SKirill A. Shutemov } 393490eceff1SKirill A. Shutemov #endif /* __PAGETABLE_P4D_FOLDED */ 393590eceff1SKirill A. Shutemov 39361da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED 39371da177e4SLinus Torvalds /* 39381da177e4SLinus Torvalds * Allocate page upper directory. 3939872fec16SHugh Dickins * We've already handled the fast-path in-line. 39401da177e4SLinus Torvalds */ 3941c2febafcSKirill A. Shutemov int __pud_alloc(struct mm_struct *mm, p4d_t *p4d, unsigned long address) 39421da177e4SLinus Torvalds { 3943c74df32cSHugh Dickins pud_t *new = pud_alloc_one(mm, address); 3944c74df32cSHugh Dickins if (!new) 39451bb3630eSHugh Dickins return -ENOMEM; 39461da177e4SLinus Torvalds 3947362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 3948362a61adSNick Piggin 3949872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 3950c2febafcSKirill A. Shutemov #ifndef __ARCH_HAS_5LEVEL_HACK 3951b4e98d9aSKirill A. Shutemov if (!p4d_present(*p4d)) { 3952b4e98d9aSKirill A. Shutemov mm_inc_nr_puds(mm); 3953c2febafcSKirill A. Shutemov p4d_populate(mm, p4d, new); 3954b4e98d9aSKirill A. Shutemov } else /* Another has populated it */ 3955c2febafcSKirill A. Shutemov pud_free(mm, new); 3956b4e98d9aSKirill A. Shutemov #else 3957b4e98d9aSKirill A. Shutemov if (!pgd_present(*p4d)) { 3958b4e98d9aSKirill A. Shutemov mm_inc_nr_puds(mm); 3959c2febafcSKirill A. Shutemov pgd_populate(mm, p4d, new); 3960b4e98d9aSKirill A. Shutemov } else /* Another has populated it */ 3961b4e98d9aSKirill A. Shutemov pud_free(mm, new); 3962c2febafcSKirill A. Shutemov #endif /* __ARCH_HAS_5LEVEL_HACK */ 3963872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 39641bb3630eSHugh Dickins return 0; 39651da177e4SLinus Torvalds } 39661da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */ 39671da177e4SLinus Torvalds 39681da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED 39691da177e4SLinus Torvalds /* 39701da177e4SLinus Torvalds * Allocate page middle directory. 3971872fec16SHugh Dickins * We've already handled the fast-path in-line. 39721da177e4SLinus Torvalds */ 39731bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) 39741da177e4SLinus Torvalds { 3975a00cc7d9SMatthew Wilcox spinlock_t *ptl; 3976c74df32cSHugh Dickins pmd_t *new = pmd_alloc_one(mm, address); 3977c74df32cSHugh Dickins if (!new) 39781bb3630eSHugh Dickins return -ENOMEM; 39791da177e4SLinus Torvalds 3980362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 3981362a61adSNick Piggin 3982a00cc7d9SMatthew Wilcox ptl = pud_lock(mm, pud); 39831da177e4SLinus Torvalds #ifndef __ARCH_HAS_4LEVEL_HACK 3984dc6c9a35SKirill A. Shutemov if (!pud_present(*pud)) { 3985dc6c9a35SKirill A. Shutemov mm_inc_nr_pmds(mm); 39861da177e4SLinus Torvalds pud_populate(mm, pud, new); 3987dc6c9a35SKirill A. Shutemov } else /* Another has populated it */ 39885e541973SBenjamin Herrenschmidt pmd_free(mm, new); 3989dc6c9a35SKirill A. Shutemov #else 3990dc6c9a35SKirill A. Shutemov if (!pgd_present(*pud)) { 3991dc6c9a35SKirill A. Shutemov mm_inc_nr_pmds(mm); 39921da177e4SLinus Torvalds pgd_populate(mm, pud, new); 3993dc6c9a35SKirill A. Shutemov } else /* Another has populated it */ 3994dc6c9a35SKirill A. Shutemov pmd_free(mm, new); 39951da177e4SLinus Torvalds #endif /* __ARCH_HAS_4LEVEL_HACK */ 3996a00cc7d9SMatthew Wilcox spin_unlock(ptl); 39971bb3630eSHugh Dickins return 0; 39981da177e4SLinus Torvalds } 39991da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */ 40001da177e4SLinus Torvalds 400109796395SRoss Zwisler static int __follow_pte_pmd(struct mm_struct *mm, unsigned long address, 4002a4d1a885SJérôme Glisse unsigned long *start, unsigned long *end, 400309796395SRoss Zwisler pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp) 4004f8ad0f49SJohannes Weiner { 4005f8ad0f49SJohannes Weiner pgd_t *pgd; 4006c2febafcSKirill A. Shutemov p4d_t *p4d; 4007f8ad0f49SJohannes Weiner pud_t *pud; 4008f8ad0f49SJohannes Weiner pmd_t *pmd; 4009f8ad0f49SJohannes Weiner pte_t *ptep; 4010f8ad0f49SJohannes Weiner 4011f8ad0f49SJohannes Weiner pgd = pgd_offset(mm, address); 4012f8ad0f49SJohannes Weiner if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) 4013f8ad0f49SJohannes Weiner goto out; 4014f8ad0f49SJohannes Weiner 4015c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, address); 4016c2febafcSKirill A. Shutemov if (p4d_none(*p4d) || unlikely(p4d_bad(*p4d))) 4017c2febafcSKirill A. Shutemov goto out; 4018c2febafcSKirill A. Shutemov 4019c2febafcSKirill A. Shutemov pud = pud_offset(p4d, address); 4020f8ad0f49SJohannes Weiner if (pud_none(*pud) || unlikely(pud_bad(*pud))) 4021f8ad0f49SJohannes Weiner goto out; 4022f8ad0f49SJohannes Weiner 4023f8ad0f49SJohannes Weiner pmd = pmd_offset(pud, address); 4024f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 402509796395SRoss Zwisler 402609796395SRoss Zwisler if (pmd_huge(*pmd)) { 402709796395SRoss Zwisler if (!pmdpp) 4028f8ad0f49SJohannes Weiner goto out; 4029f8ad0f49SJohannes Weiner 4030a4d1a885SJérôme Glisse if (start && end) { 4031a4d1a885SJérôme Glisse *start = address & PMD_MASK; 4032a4d1a885SJérôme Glisse *end = *start + PMD_SIZE; 4033a4d1a885SJérôme Glisse mmu_notifier_invalidate_range_start(mm, *start, *end); 4034a4d1a885SJérôme Glisse } 403509796395SRoss Zwisler *ptlp = pmd_lock(mm, pmd); 403609796395SRoss Zwisler if (pmd_huge(*pmd)) { 403709796395SRoss Zwisler *pmdpp = pmd; 403809796395SRoss Zwisler return 0; 403909796395SRoss Zwisler } 404009796395SRoss Zwisler spin_unlock(*ptlp); 4041a4d1a885SJérôme Glisse if (start && end) 4042a4d1a885SJérôme Glisse mmu_notifier_invalidate_range_end(mm, *start, *end); 404309796395SRoss Zwisler } 404409796395SRoss Zwisler 404509796395SRoss Zwisler if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd))) 4046f8ad0f49SJohannes Weiner goto out; 4047f8ad0f49SJohannes Weiner 4048a4d1a885SJérôme Glisse if (start && end) { 4049a4d1a885SJérôme Glisse *start = address & PAGE_MASK; 4050a4d1a885SJérôme Glisse *end = *start + PAGE_SIZE; 4051a4d1a885SJérôme Glisse mmu_notifier_invalidate_range_start(mm, *start, *end); 4052a4d1a885SJérôme Glisse } 4053f8ad0f49SJohannes Weiner ptep = pte_offset_map_lock(mm, pmd, address, ptlp); 4054f8ad0f49SJohannes Weiner if (!pte_present(*ptep)) 4055f8ad0f49SJohannes Weiner goto unlock; 4056f8ad0f49SJohannes Weiner *ptepp = ptep; 4057f8ad0f49SJohannes Weiner return 0; 4058f8ad0f49SJohannes Weiner unlock: 4059f8ad0f49SJohannes Weiner pte_unmap_unlock(ptep, *ptlp); 4060a4d1a885SJérôme Glisse if (start && end) 4061a4d1a885SJérôme Glisse mmu_notifier_invalidate_range_end(mm, *start, *end); 4062f8ad0f49SJohannes Weiner out: 4063f8ad0f49SJohannes Weiner return -EINVAL; 4064f8ad0f49SJohannes Weiner } 4065f8ad0f49SJohannes Weiner 4066f729c8c9SRoss Zwisler static inline int follow_pte(struct mm_struct *mm, unsigned long address, 4067f729c8c9SRoss Zwisler pte_t **ptepp, spinlock_t **ptlp) 40681b36ba81SNamhyung Kim { 40691b36ba81SNamhyung Kim int res; 40701b36ba81SNamhyung Kim 40711b36ba81SNamhyung Kim /* (void) is needed to make gcc happy */ 40721b36ba81SNamhyung Kim (void) __cond_lock(*ptlp, 4073a4d1a885SJérôme Glisse !(res = __follow_pte_pmd(mm, address, NULL, NULL, 4074a4d1a885SJérôme Glisse ptepp, NULL, ptlp))); 40751b36ba81SNamhyung Kim return res; 40761b36ba81SNamhyung Kim } 40771b36ba81SNamhyung Kim 407809796395SRoss Zwisler int follow_pte_pmd(struct mm_struct *mm, unsigned long address, 4079a4d1a885SJérôme Glisse unsigned long *start, unsigned long *end, 408009796395SRoss Zwisler pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp) 408109796395SRoss Zwisler { 408209796395SRoss Zwisler int res; 408309796395SRoss Zwisler 408409796395SRoss Zwisler /* (void) is needed to make gcc happy */ 408509796395SRoss Zwisler (void) __cond_lock(*ptlp, 4086a4d1a885SJérôme Glisse !(res = __follow_pte_pmd(mm, address, start, end, 4087a4d1a885SJérôme Glisse ptepp, pmdpp, ptlp))); 408809796395SRoss Zwisler return res; 408909796395SRoss Zwisler } 409009796395SRoss Zwisler EXPORT_SYMBOL(follow_pte_pmd); 409109796395SRoss Zwisler 40923b6748e2SJohannes Weiner /** 40933b6748e2SJohannes Weiner * follow_pfn - look up PFN at a user virtual address 40943b6748e2SJohannes Weiner * @vma: memory mapping 40953b6748e2SJohannes Weiner * @address: user virtual address 40963b6748e2SJohannes Weiner * @pfn: location to store found PFN 40973b6748e2SJohannes Weiner * 40983b6748e2SJohannes Weiner * Only IO mappings and raw PFN mappings are allowed. 40993b6748e2SJohannes Weiner * 41003b6748e2SJohannes Weiner * Returns zero and the pfn at @pfn on success, -ve otherwise. 41013b6748e2SJohannes Weiner */ 41023b6748e2SJohannes Weiner int follow_pfn(struct vm_area_struct *vma, unsigned long address, 41033b6748e2SJohannes Weiner unsigned long *pfn) 41043b6748e2SJohannes Weiner { 41053b6748e2SJohannes Weiner int ret = -EINVAL; 41063b6748e2SJohannes Weiner spinlock_t *ptl; 41073b6748e2SJohannes Weiner pte_t *ptep; 41083b6748e2SJohannes Weiner 41093b6748e2SJohannes Weiner if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 41103b6748e2SJohannes Weiner return ret; 41113b6748e2SJohannes Weiner 41123b6748e2SJohannes Weiner ret = follow_pte(vma->vm_mm, address, &ptep, &ptl); 41133b6748e2SJohannes Weiner if (ret) 41143b6748e2SJohannes Weiner return ret; 41153b6748e2SJohannes Weiner *pfn = pte_pfn(*ptep); 41163b6748e2SJohannes Weiner pte_unmap_unlock(ptep, ptl); 41173b6748e2SJohannes Weiner return 0; 41183b6748e2SJohannes Weiner } 41193b6748e2SJohannes Weiner EXPORT_SYMBOL(follow_pfn); 41203b6748e2SJohannes Weiner 412128b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT 4122d87fe660Svenkatesh.pallipadi@intel.com int follow_phys(struct vm_area_struct *vma, 412328b2ee20SRik van Riel unsigned long address, unsigned int flags, 4124d87fe660Svenkatesh.pallipadi@intel.com unsigned long *prot, resource_size_t *phys) 412528b2ee20SRik van Riel { 412603668a4dSJohannes Weiner int ret = -EINVAL; 412728b2ee20SRik van Riel pte_t *ptep, pte; 412828b2ee20SRik van Riel spinlock_t *ptl; 412928b2ee20SRik van Riel 4130d87fe660Svenkatesh.pallipadi@intel.com if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 4131d87fe660Svenkatesh.pallipadi@intel.com goto out; 413228b2ee20SRik van Riel 413303668a4dSJohannes Weiner if (follow_pte(vma->vm_mm, address, &ptep, &ptl)) 4134d87fe660Svenkatesh.pallipadi@intel.com goto out; 413528b2ee20SRik van Riel pte = *ptep; 413603668a4dSJohannes Weiner 4137f6f37321SLinus Torvalds if ((flags & FOLL_WRITE) && !pte_write(pte)) 413828b2ee20SRik van Riel goto unlock; 413928b2ee20SRik van Riel 414028b2ee20SRik van Riel *prot = pgprot_val(pte_pgprot(pte)); 414103668a4dSJohannes Weiner *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT; 414228b2ee20SRik van Riel 414303668a4dSJohannes Weiner ret = 0; 414428b2ee20SRik van Riel unlock: 414528b2ee20SRik van Riel pte_unmap_unlock(ptep, ptl); 414628b2ee20SRik van Riel out: 4147d87fe660Svenkatesh.pallipadi@intel.com return ret; 414828b2ee20SRik van Riel } 414928b2ee20SRik van Riel 415028b2ee20SRik van Riel int generic_access_phys(struct vm_area_struct *vma, unsigned long addr, 415128b2ee20SRik van Riel void *buf, int len, int write) 415228b2ee20SRik van Riel { 415328b2ee20SRik van Riel resource_size_t phys_addr; 415428b2ee20SRik van Riel unsigned long prot = 0; 41552bc7273bSKOSAKI Motohiro void __iomem *maddr; 415628b2ee20SRik van Riel int offset = addr & (PAGE_SIZE-1); 415728b2ee20SRik van Riel 4158d87fe660Svenkatesh.pallipadi@intel.com if (follow_phys(vma, addr, write, &prot, &phys_addr)) 415928b2ee20SRik van Riel return -EINVAL; 416028b2ee20SRik van Riel 41619cb12d7bSGrazvydas Ignotas maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot); 416224eee1e4Sjie@chenjie6@huwei.com if (!maddr) 416324eee1e4Sjie@chenjie6@huwei.com return -ENOMEM; 416424eee1e4Sjie@chenjie6@huwei.com 416528b2ee20SRik van Riel if (write) 416628b2ee20SRik van Riel memcpy_toio(maddr + offset, buf, len); 416728b2ee20SRik van Riel else 416828b2ee20SRik van Riel memcpy_fromio(buf, maddr + offset, len); 416928b2ee20SRik van Riel iounmap(maddr); 417028b2ee20SRik van Riel 417128b2ee20SRik van Riel return len; 417228b2ee20SRik van Riel } 41735a73633eSUwe Kleine-König EXPORT_SYMBOL_GPL(generic_access_phys); 417428b2ee20SRik van Riel #endif 417528b2ee20SRik van Riel 41760ec76a11SDavid Howells /* 4177206cb636SStephen Wilson * Access another process' address space as given in mm. If non-NULL, use the 4178206cb636SStephen Wilson * given task for page fault accounting. 41790ec76a11SDavid Howells */ 418084d77d3fSEric W. Biederman int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm, 4181442486ecSLorenzo Stoakes unsigned long addr, void *buf, int len, unsigned int gup_flags) 41820ec76a11SDavid Howells { 41830ec76a11SDavid Howells struct vm_area_struct *vma; 41840ec76a11SDavid Howells void *old_buf = buf; 4185442486ecSLorenzo Stoakes int write = gup_flags & FOLL_WRITE; 41860ec76a11SDavid Howells 41870ec76a11SDavid Howells down_read(&mm->mmap_sem); 4188183ff22bSSimon Arlott /* ignore errors, just check how much was successfully transferred */ 41890ec76a11SDavid Howells while (len) { 41900ec76a11SDavid Howells int bytes, ret, offset; 41910ec76a11SDavid Howells void *maddr; 419228b2ee20SRik van Riel struct page *page = NULL; 41930ec76a11SDavid Howells 41941e987790SDave Hansen ret = get_user_pages_remote(tsk, mm, addr, 1, 41955b56d49fSLorenzo Stoakes gup_flags, &page, &vma, NULL); 419628b2ee20SRik van Riel if (ret <= 0) { 4197dbffcd03SRik van Riel #ifndef CONFIG_HAVE_IOREMAP_PROT 4198dbffcd03SRik van Riel break; 4199dbffcd03SRik van Riel #else 420028b2ee20SRik van Riel /* 420128b2ee20SRik van Riel * Check if this is a VM_IO | VM_PFNMAP VMA, which 420228b2ee20SRik van Riel * we can access using slightly different code. 420328b2ee20SRik van Riel */ 420428b2ee20SRik van Riel vma = find_vma(mm, addr); 4205fe936dfcSMichael Ellerman if (!vma || vma->vm_start > addr) 42060ec76a11SDavid Howells break; 420728b2ee20SRik van Riel if (vma->vm_ops && vma->vm_ops->access) 420828b2ee20SRik van Riel ret = vma->vm_ops->access(vma, addr, buf, 420928b2ee20SRik van Riel len, write); 421028b2ee20SRik van Riel if (ret <= 0) 421128b2ee20SRik van Riel break; 421228b2ee20SRik van Riel bytes = ret; 4213dbffcd03SRik van Riel #endif 421428b2ee20SRik van Riel } else { 42150ec76a11SDavid Howells bytes = len; 42160ec76a11SDavid Howells offset = addr & (PAGE_SIZE-1); 42170ec76a11SDavid Howells if (bytes > PAGE_SIZE-offset) 42180ec76a11SDavid Howells bytes = PAGE_SIZE-offset; 42190ec76a11SDavid Howells 42200ec76a11SDavid Howells maddr = kmap(page); 42210ec76a11SDavid Howells if (write) { 42220ec76a11SDavid Howells copy_to_user_page(vma, page, addr, 42230ec76a11SDavid Howells maddr + offset, buf, bytes); 42240ec76a11SDavid Howells set_page_dirty_lock(page); 42250ec76a11SDavid Howells } else { 42260ec76a11SDavid Howells copy_from_user_page(vma, page, addr, 42270ec76a11SDavid Howells buf, maddr + offset, bytes); 42280ec76a11SDavid Howells } 42290ec76a11SDavid Howells kunmap(page); 423009cbfeafSKirill A. Shutemov put_page(page); 423128b2ee20SRik van Riel } 42320ec76a11SDavid Howells len -= bytes; 42330ec76a11SDavid Howells buf += bytes; 42340ec76a11SDavid Howells addr += bytes; 42350ec76a11SDavid Howells } 42360ec76a11SDavid Howells up_read(&mm->mmap_sem); 42370ec76a11SDavid Howells 42380ec76a11SDavid Howells return buf - old_buf; 42390ec76a11SDavid Howells } 424003252919SAndi Kleen 42415ddd36b9SStephen Wilson /** 4242ae91dbfcSRandy Dunlap * access_remote_vm - access another process' address space 42435ddd36b9SStephen Wilson * @mm: the mm_struct of the target address space 42445ddd36b9SStephen Wilson * @addr: start address to access 42455ddd36b9SStephen Wilson * @buf: source or destination buffer 42465ddd36b9SStephen Wilson * @len: number of bytes to transfer 42476347e8d5SLorenzo Stoakes * @gup_flags: flags modifying lookup behaviour 42485ddd36b9SStephen Wilson * 42495ddd36b9SStephen Wilson * The caller must hold a reference on @mm. 42505ddd36b9SStephen Wilson */ 42515ddd36b9SStephen Wilson int access_remote_vm(struct mm_struct *mm, unsigned long addr, 42526347e8d5SLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 42535ddd36b9SStephen Wilson { 42546347e8d5SLorenzo Stoakes return __access_remote_vm(NULL, mm, addr, buf, len, gup_flags); 42555ddd36b9SStephen Wilson } 42565ddd36b9SStephen Wilson 425703252919SAndi Kleen /* 4258206cb636SStephen Wilson * Access another process' address space. 4259206cb636SStephen Wilson * Source/target buffer must be kernel space, 4260206cb636SStephen Wilson * Do not walk the page table directly, use get_user_pages 4261206cb636SStephen Wilson */ 4262206cb636SStephen Wilson int access_process_vm(struct task_struct *tsk, unsigned long addr, 4263f307ab6dSLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 4264206cb636SStephen Wilson { 4265206cb636SStephen Wilson struct mm_struct *mm; 4266206cb636SStephen Wilson int ret; 4267206cb636SStephen Wilson 4268206cb636SStephen Wilson mm = get_task_mm(tsk); 4269206cb636SStephen Wilson if (!mm) 4270206cb636SStephen Wilson return 0; 4271206cb636SStephen Wilson 4272f307ab6dSLorenzo Stoakes ret = __access_remote_vm(tsk, mm, addr, buf, len, gup_flags); 4273442486ecSLorenzo Stoakes 4274206cb636SStephen Wilson mmput(mm); 4275206cb636SStephen Wilson 4276206cb636SStephen Wilson return ret; 4277206cb636SStephen Wilson } 4278fcd35857SCatalin Marinas EXPORT_SYMBOL_GPL(access_process_vm); 4279206cb636SStephen Wilson 428003252919SAndi Kleen /* 428103252919SAndi Kleen * Print the name of a VMA. 428203252919SAndi Kleen */ 428303252919SAndi Kleen void print_vma_addr(char *prefix, unsigned long ip) 428403252919SAndi Kleen { 428503252919SAndi Kleen struct mm_struct *mm = current->mm; 428603252919SAndi Kleen struct vm_area_struct *vma; 428703252919SAndi Kleen 4288e8bff74aSIngo Molnar /* 42890a7f682dSMichal Hocko * we might be running from an atomic context so we cannot sleep 4290e8bff74aSIngo Molnar */ 42910a7f682dSMichal Hocko if (!down_read_trylock(&mm->mmap_sem)) 4292e8bff74aSIngo Molnar return; 4293e8bff74aSIngo Molnar 429403252919SAndi Kleen vma = find_vma(mm, ip); 429503252919SAndi Kleen if (vma && vma->vm_file) { 429603252919SAndi Kleen struct file *f = vma->vm_file; 42970a7f682dSMichal Hocko char *buf = (char *)__get_free_page(GFP_NOWAIT); 429803252919SAndi Kleen if (buf) { 42992fbc57c5SAndy Shevchenko char *p; 430003252919SAndi Kleen 43019bf39ab2SMiklos Szeredi p = file_path(f, buf, PAGE_SIZE); 430203252919SAndi Kleen if (IS_ERR(p)) 430303252919SAndi Kleen p = "?"; 43042fbc57c5SAndy Shevchenko printk("%s%s[%lx+%lx]", prefix, kbasename(p), 430503252919SAndi Kleen vma->vm_start, 430603252919SAndi Kleen vma->vm_end - vma->vm_start); 430703252919SAndi Kleen free_page((unsigned long)buf); 430803252919SAndi Kleen } 430903252919SAndi Kleen } 431051a07e50SJeff Liu up_read(&mm->mmap_sem); 431103252919SAndi Kleen } 43123ee1afa3SNick Piggin 4313662bbcb2SMichael S. Tsirkin #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP) 43149ec23531SDavid Hildenbrand void __might_fault(const char *file, int line) 43153ee1afa3SNick Piggin { 431695156f00SPeter Zijlstra /* 431795156f00SPeter Zijlstra * Some code (nfs/sunrpc) uses socket ops on kernel memory while 431895156f00SPeter Zijlstra * holding the mmap_sem, this is safe because kernel memory doesn't 431995156f00SPeter Zijlstra * get paged out, therefore we'll never actually fault, and the 432095156f00SPeter Zijlstra * below annotations will generate false positives. 432195156f00SPeter Zijlstra */ 4322db68ce10SAl Viro if (uaccess_kernel()) 432395156f00SPeter Zijlstra return; 43249ec23531SDavid Hildenbrand if (pagefault_disabled()) 4325662bbcb2SMichael S. Tsirkin return; 43269ec23531SDavid Hildenbrand __might_sleep(file, line, 0); 43279ec23531SDavid Hildenbrand #if defined(CONFIG_DEBUG_ATOMIC_SLEEP) 4328662bbcb2SMichael S. Tsirkin if (current->mm) 43293ee1afa3SNick Piggin might_lock_read(¤t->mm->mmap_sem); 43309ec23531SDavid Hildenbrand #endif 43313ee1afa3SNick Piggin } 43329ec23531SDavid Hildenbrand EXPORT_SYMBOL(__might_fault); 43333ee1afa3SNick Piggin #endif 433447ad8475SAndrea Arcangeli 433547ad8475SAndrea Arcangeli #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS) 4336c6ddfb6cSHuang Ying /* 4337c6ddfb6cSHuang Ying * Process all subpages of the specified huge page with the specified 4338c6ddfb6cSHuang Ying * operation. The target subpage will be processed last to keep its 4339c6ddfb6cSHuang Ying * cache lines hot. 4340c6ddfb6cSHuang Ying */ 4341c6ddfb6cSHuang Ying static inline void process_huge_page( 4342c6ddfb6cSHuang Ying unsigned long addr_hint, unsigned int pages_per_huge_page, 4343c6ddfb6cSHuang Ying void (*process_subpage)(unsigned long addr, int idx, void *arg), 4344c6ddfb6cSHuang Ying void *arg) 4345c6ddfb6cSHuang Ying { 4346c6ddfb6cSHuang Ying int i, n, base, l; 4347c6ddfb6cSHuang Ying unsigned long addr = addr_hint & 4348c6ddfb6cSHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 4349c6ddfb6cSHuang Ying 4350c6ddfb6cSHuang Ying /* Process target subpage last to keep its cache lines hot */ 4351c6ddfb6cSHuang Ying might_sleep(); 4352c6ddfb6cSHuang Ying n = (addr_hint - addr) / PAGE_SIZE; 4353c6ddfb6cSHuang Ying if (2 * n <= pages_per_huge_page) { 4354c6ddfb6cSHuang Ying /* If target subpage in first half of huge page */ 4355c6ddfb6cSHuang Ying base = 0; 4356c6ddfb6cSHuang Ying l = n; 4357c6ddfb6cSHuang Ying /* Process subpages at the end of huge page */ 4358c6ddfb6cSHuang Ying for (i = pages_per_huge_page - 1; i >= 2 * n; i--) { 4359c6ddfb6cSHuang Ying cond_resched(); 4360c6ddfb6cSHuang Ying process_subpage(addr + i * PAGE_SIZE, i, arg); 4361c6ddfb6cSHuang Ying } 4362c6ddfb6cSHuang Ying } else { 4363c6ddfb6cSHuang Ying /* If target subpage in second half of huge page */ 4364c6ddfb6cSHuang Ying base = pages_per_huge_page - 2 * (pages_per_huge_page - n); 4365c6ddfb6cSHuang Ying l = pages_per_huge_page - n; 4366c6ddfb6cSHuang Ying /* Process subpages at the begin of huge page */ 4367c6ddfb6cSHuang Ying for (i = 0; i < base; i++) { 4368c6ddfb6cSHuang Ying cond_resched(); 4369c6ddfb6cSHuang Ying process_subpage(addr + i * PAGE_SIZE, i, arg); 4370c6ddfb6cSHuang Ying } 4371c6ddfb6cSHuang Ying } 4372c6ddfb6cSHuang Ying /* 4373c6ddfb6cSHuang Ying * Process remaining subpages in left-right-left-right pattern 4374c6ddfb6cSHuang Ying * towards the target subpage 4375c6ddfb6cSHuang Ying */ 4376c6ddfb6cSHuang Ying for (i = 0; i < l; i++) { 4377c6ddfb6cSHuang Ying int left_idx = base + i; 4378c6ddfb6cSHuang Ying int right_idx = base + 2 * l - 1 - i; 4379c6ddfb6cSHuang Ying 4380c6ddfb6cSHuang Ying cond_resched(); 4381c6ddfb6cSHuang Ying process_subpage(addr + left_idx * PAGE_SIZE, left_idx, arg); 4382c6ddfb6cSHuang Ying cond_resched(); 4383c6ddfb6cSHuang Ying process_subpage(addr + right_idx * PAGE_SIZE, right_idx, arg); 4384c6ddfb6cSHuang Ying } 4385c6ddfb6cSHuang Ying } 4386c6ddfb6cSHuang Ying 438747ad8475SAndrea Arcangeli static void clear_gigantic_page(struct page *page, 438847ad8475SAndrea Arcangeli unsigned long addr, 438947ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 439047ad8475SAndrea Arcangeli { 439147ad8475SAndrea Arcangeli int i; 439247ad8475SAndrea Arcangeli struct page *p = page; 439347ad8475SAndrea Arcangeli 439447ad8475SAndrea Arcangeli might_sleep(); 439547ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; 439647ad8475SAndrea Arcangeli i++, p = mem_map_next(p, page, i)) { 439747ad8475SAndrea Arcangeli cond_resched(); 439847ad8475SAndrea Arcangeli clear_user_highpage(p, addr + i * PAGE_SIZE); 439947ad8475SAndrea Arcangeli } 440047ad8475SAndrea Arcangeli } 4401c6ddfb6cSHuang Ying 4402c6ddfb6cSHuang Ying static void clear_subpage(unsigned long addr, int idx, void *arg) 4403c6ddfb6cSHuang Ying { 4404c6ddfb6cSHuang Ying struct page *page = arg; 4405c6ddfb6cSHuang Ying 4406c6ddfb6cSHuang Ying clear_user_highpage(page + idx, addr); 4407c6ddfb6cSHuang Ying } 4408c6ddfb6cSHuang Ying 440947ad8475SAndrea Arcangeli void clear_huge_page(struct page *page, 4410c79b57e4SHuang Ying unsigned long addr_hint, unsigned int pages_per_huge_page) 441147ad8475SAndrea Arcangeli { 4412c79b57e4SHuang Ying unsigned long addr = addr_hint & 4413c79b57e4SHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 441447ad8475SAndrea Arcangeli 441547ad8475SAndrea Arcangeli if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { 441647ad8475SAndrea Arcangeli clear_gigantic_page(page, addr, pages_per_huge_page); 441747ad8475SAndrea Arcangeli return; 441847ad8475SAndrea Arcangeli } 441947ad8475SAndrea Arcangeli 4420c6ddfb6cSHuang Ying process_huge_page(addr_hint, pages_per_huge_page, clear_subpage, page); 442147ad8475SAndrea Arcangeli } 442247ad8475SAndrea Arcangeli 442347ad8475SAndrea Arcangeli static void copy_user_gigantic_page(struct page *dst, struct page *src, 442447ad8475SAndrea Arcangeli unsigned long addr, 442547ad8475SAndrea Arcangeli struct vm_area_struct *vma, 442647ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 442747ad8475SAndrea Arcangeli { 442847ad8475SAndrea Arcangeli int i; 442947ad8475SAndrea Arcangeli struct page *dst_base = dst; 443047ad8475SAndrea Arcangeli struct page *src_base = src; 443147ad8475SAndrea Arcangeli 443247ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; ) { 443347ad8475SAndrea Arcangeli cond_resched(); 443447ad8475SAndrea Arcangeli copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma); 443547ad8475SAndrea Arcangeli 443647ad8475SAndrea Arcangeli i++; 443747ad8475SAndrea Arcangeli dst = mem_map_next(dst, dst_base, i); 443847ad8475SAndrea Arcangeli src = mem_map_next(src, src_base, i); 443947ad8475SAndrea Arcangeli } 444047ad8475SAndrea Arcangeli } 444147ad8475SAndrea Arcangeli 4442c9f4cd71SHuang Ying struct copy_subpage_arg { 4443c9f4cd71SHuang Ying struct page *dst; 4444c9f4cd71SHuang Ying struct page *src; 4445c9f4cd71SHuang Ying struct vm_area_struct *vma; 4446c9f4cd71SHuang Ying }; 4447c9f4cd71SHuang Ying 4448c9f4cd71SHuang Ying static void copy_subpage(unsigned long addr, int idx, void *arg) 4449c9f4cd71SHuang Ying { 4450c9f4cd71SHuang Ying struct copy_subpage_arg *copy_arg = arg; 4451c9f4cd71SHuang Ying 4452c9f4cd71SHuang Ying copy_user_highpage(copy_arg->dst + idx, copy_arg->src + idx, 4453c9f4cd71SHuang Ying addr, copy_arg->vma); 4454c9f4cd71SHuang Ying } 4455c9f4cd71SHuang Ying 445647ad8475SAndrea Arcangeli void copy_user_huge_page(struct page *dst, struct page *src, 4457c9f4cd71SHuang Ying unsigned long addr_hint, struct vm_area_struct *vma, 445847ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 445947ad8475SAndrea Arcangeli { 4460c9f4cd71SHuang Ying unsigned long addr = addr_hint & 4461c9f4cd71SHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 4462c9f4cd71SHuang Ying struct copy_subpage_arg arg = { 4463c9f4cd71SHuang Ying .dst = dst, 4464c9f4cd71SHuang Ying .src = src, 4465c9f4cd71SHuang Ying .vma = vma, 4466c9f4cd71SHuang Ying }; 446747ad8475SAndrea Arcangeli 446847ad8475SAndrea Arcangeli if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { 446947ad8475SAndrea Arcangeli copy_user_gigantic_page(dst, src, addr, vma, 447047ad8475SAndrea Arcangeli pages_per_huge_page); 447147ad8475SAndrea Arcangeli return; 447247ad8475SAndrea Arcangeli } 447347ad8475SAndrea Arcangeli 4474c9f4cd71SHuang Ying process_huge_page(addr_hint, pages_per_huge_page, copy_subpage, &arg); 447547ad8475SAndrea Arcangeli } 4476fa4d75c1SMike Kravetz 4477fa4d75c1SMike Kravetz long copy_huge_page_from_user(struct page *dst_page, 4478fa4d75c1SMike Kravetz const void __user *usr_src, 4479810a56b9SMike Kravetz unsigned int pages_per_huge_page, 4480810a56b9SMike Kravetz bool allow_pagefault) 4481fa4d75c1SMike Kravetz { 4482fa4d75c1SMike Kravetz void *src = (void *)usr_src; 4483fa4d75c1SMike Kravetz void *page_kaddr; 4484fa4d75c1SMike Kravetz unsigned long i, rc = 0; 4485fa4d75c1SMike Kravetz unsigned long ret_val = pages_per_huge_page * PAGE_SIZE; 4486fa4d75c1SMike Kravetz 4487fa4d75c1SMike Kravetz for (i = 0; i < pages_per_huge_page; i++) { 4488810a56b9SMike Kravetz if (allow_pagefault) 4489810a56b9SMike Kravetz page_kaddr = kmap(dst_page + i); 4490810a56b9SMike Kravetz else 4491fa4d75c1SMike Kravetz page_kaddr = kmap_atomic(dst_page + i); 4492fa4d75c1SMike Kravetz rc = copy_from_user(page_kaddr, 4493fa4d75c1SMike Kravetz (const void __user *)(src + i * PAGE_SIZE), 4494fa4d75c1SMike Kravetz PAGE_SIZE); 4495810a56b9SMike Kravetz if (allow_pagefault) 4496810a56b9SMike Kravetz kunmap(dst_page + i); 4497810a56b9SMike Kravetz else 4498fa4d75c1SMike Kravetz kunmap_atomic(page_kaddr); 4499fa4d75c1SMike Kravetz 4500fa4d75c1SMike Kravetz ret_val -= (PAGE_SIZE - rc); 4501fa4d75c1SMike Kravetz if (rc) 4502fa4d75c1SMike Kravetz break; 4503fa4d75c1SMike Kravetz 4504fa4d75c1SMike Kravetz cond_resched(); 4505fa4d75c1SMike Kravetz } 4506fa4d75c1SMike Kravetz return ret_val; 4507fa4d75c1SMike Kravetz } 450847ad8475SAndrea Arcangeli #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */ 450949076ec2SKirill A. Shutemov 451040b64acdSOlof Johansson #if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS 4511b35f1819SKirill A. Shutemov 4512b35f1819SKirill A. Shutemov static struct kmem_cache *page_ptl_cachep; 4513b35f1819SKirill A. Shutemov 4514b35f1819SKirill A. Shutemov void __init ptlock_cache_init(void) 4515b35f1819SKirill A. Shutemov { 4516b35f1819SKirill A. Shutemov page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0, 4517b35f1819SKirill A. Shutemov SLAB_PANIC, NULL); 4518b35f1819SKirill A. Shutemov } 4519b35f1819SKirill A. Shutemov 4520539edb58SPeter Zijlstra bool ptlock_alloc(struct page *page) 452149076ec2SKirill A. Shutemov { 452249076ec2SKirill A. Shutemov spinlock_t *ptl; 452349076ec2SKirill A. Shutemov 4524b35f1819SKirill A. Shutemov ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL); 452549076ec2SKirill A. Shutemov if (!ptl) 452649076ec2SKirill A. Shutemov return false; 4527539edb58SPeter Zijlstra page->ptl = ptl; 452849076ec2SKirill A. Shutemov return true; 452949076ec2SKirill A. Shutemov } 453049076ec2SKirill A. Shutemov 4531539edb58SPeter Zijlstra void ptlock_free(struct page *page) 453249076ec2SKirill A. Shutemov { 4533b35f1819SKirill A. Shutemov kmem_cache_free(page_ptl_cachep, page->ptl); 453449076ec2SKirill A. Shutemov } 453549076ec2SKirill A. Shutemov #endif 4536