1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/mm/memory.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 81da177e4SLinus Torvalds /* 91da177e4SLinus Torvalds * demand-loading started 01.12.91 - seems it is high on the list of 101da177e4SLinus Torvalds * things wanted, and it should be easy to implement. - Linus 111da177e4SLinus Torvalds */ 121da177e4SLinus Torvalds 131da177e4SLinus Torvalds /* 141da177e4SLinus Torvalds * Ok, demand-loading was easy, shared pages a little bit tricker. Shared 151da177e4SLinus Torvalds * pages started 02.12.91, seems to work. - Linus. 161da177e4SLinus Torvalds * 171da177e4SLinus Torvalds * Tested sharing by executing about 30 /bin/sh: under the old kernel it 181da177e4SLinus Torvalds * would have taken more than the 6M I have free, but it worked well as 191da177e4SLinus Torvalds * far as I could see. 201da177e4SLinus Torvalds * 211da177e4SLinus Torvalds * Also corrected some "invalidate()"s - I wasn't doing enough of them. 221da177e4SLinus Torvalds */ 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds /* 251da177e4SLinus Torvalds * Real VM (paging to/from disk) started 18.12.91. Much more work and 261da177e4SLinus Torvalds * thought has to go into this. Oh, well.. 271da177e4SLinus Torvalds * 19.12.91 - works, somewhat. Sometimes I get faults, don't know why. 281da177e4SLinus Torvalds * Found it. Everything seems to work now. 291da177e4SLinus Torvalds * 20.12.91 - Ok, making the swap-device changeable like the root. 301da177e4SLinus Torvalds */ 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds /* 331da177e4SLinus Torvalds * 05.04.94 - Multi-page memory management added for v1.1. 341da177e4SLinus Torvalds * Idea by Alex Bligh (alex@cconcepts.co.uk) 351da177e4SLinus Torvalds * 361da177e4SLinus Torvalds * 16.07.99 - Support of BIGMEM added by Gerhard Wichert, Siemens AG 371da177e4SLinus Torvalds * (Gerhard.Wichert@pdb.siemens.de) 381da177e4SLinus Torvalds * 391da177e4SLinus Torvalds * Aug/Sep 2004 Changed to four level page tables (Andi Kleen) 401da177e4SLinus Torvalds */ 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds #include <linux/kernel_stat.h> 431da177e4SLinus Torvalds #include <linux/mm.h> 446e84f315SIngo Molnar #include <linux/sched/mm.h> 45f7ccbae4SIngo Molnar #include <linux/sched/coredump.h> 466a3827d7SIngo Molnar #include <linux/sched/numa_balancing.h> 4729930025SIngo Molnar #include <linux/sched/task.h> 481da177e4SLinus Torvalds #include <linux/hugetlb.h> 491da177e4SLinus Torvalds #include <linux/mman.h> 501da177e4SLinus Torvalds #include <linux/swap.h> 511da177e4SLinus Torvalds #include <linux/highmem.h> 521da177e4SLinus Torvalds #include <linux/pagemap.h> 535042db43SJérôme Glisse #include <linux/memremap.h> 549a840895SHugh Dickins #include <linux/ksm.h> 551da177e4SLinus Torvalds #include <linux/rmap.h> 56b95f1b31SPaul Gortmaker #include <linux/export.h> 570ff92245SShailabh Nagar #include <linux/delayacct.h> 581da177e4SLinus Torvalds #include <linux/init.h> 5901c8f1c4SDan Williams #include <linux/pfn_t.h> 60edc79b2aSPeter Zijlstra #include <linux/writeback.h> 618a9f3ccdSBalbir Singh #include <linux/memcontrol.h> 62cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h> 633dc14741SHugh Dickins #include <linux/swapops.h> 643dc14741SHugh Dickins #include <linux/elf.h> 655a0e3ad6STejun Heo #include <linux/gfp.h> 664daae3b4SMel Gorman #include <linux/migrate.h> 672fbc57c5SAndy Shevchenko #include <linux/string.h> 680abdd7a8SDan Williams #include <linux/dma-debug.h> 691592eef0SKirill A. Shutemov #include <linux/debugfs.h> 706b251fc9SAndrea Arcangeli #include <linux/userfaultfd_k.h> 71bc2466e4SJan Kara #include <linux/dax.h> 726b31d595SMichal Hocko #include <linux/oom.h> 7398fa15f3SAnshuman Khandual #include <linux/numa.h> 741da177e4SLinus Torvalds 75*b3d1411bSJoel Fernandes (Google) #include <trace/events/kmem.h> 76*b3d1411bSJoel Fernandes (Google) 776952b61dSAlexey Dobriyan #include <asm/io.h> 7833a709b2SDave Hansen #include <asm/mmu_context.h> 791da177e4SLinus Torvalds #include <asm/pgalloc.h> 807c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 811da177e4SLinus Torvalds #include <asm/tlb.h> 821da177e4SLinus Torvalds #include <asm/tlbflush.h> 831da177e4SLinus Torvalds #include <asm/pgtable.h> 841da177e4SLinus Torvalds 8542b77728SJan Beulich #include "internal.h" 8642b77728SJan Beulich 87af27d940SArnd Bergmann #if defined(LAST_CPUPID_NOT_IN_PAGE_FLAGS) && !defined(CONFIG_COMPILE_TEST) 8890572890SPeter Zijlstra #warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid. 8975980e97SPeter Zijlstra #endif 9075980e97SPeter Zijlstra 91d41dee36SAndy Whitcroft #ifndef CONFIG_NEED_MULTIPLE_NODES 921da177e4SLinus Torvalds /* use the per-pgdat data instead for discontigmem - mbligh */ 931da177e4SLinus Torvalds unsigned long max_mapnr; 941da177e4SLinus Torvalds EXPORT_SYMBOL(max_mapnr); 95166f61b9STobin C Harding 96166f61b9STobin C Harding struct page *mem_map; 971da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map); 981da177e4SLinus Torvalds #endif 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds /* 1011da177e4SLinus Torvalds * A number of key systems in x86 including ioremap() rely on the assumption 1021da177e4SLinus Torvalds * that high_memory defines the upper bound on direct map memory, then end 1031da177e4SLinus Torvalds * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and 1041da177e4SLinus Torvalds * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL 1051da177e4SLinus Torvalds * and ZONE_HIGHMEM. 1061da177e4SLinus Torvalds */ 1071da177e4SLinus Torvalds void *high_memory; 1081da177e4SLinus Torvalds EXPORT_SYMBOL(high_memory); 1091da177e4SLinus Torvalds 11032a93233SIngo Molnar /* 11132a93233SIngo Molnar * Randomize the address space (stacks, mmaps, brk, etc.). 11232a93233SIngo Molnar * 11332a93233SIngo Molnar * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization, 11432a93233SIngo Molnar * as ancient (libc5 based) binaries can segfault. ) 11532a93233SIngo Molnar */ 11632a93233SIngo Molnar int randomize_va_space __read_mostly = 11732a93233SIngo Molnar #ifdef CONFIG_COMPAT_BRK 11832a93233SIngo Molnar 1; 11932a93233SIngo Molnar #else 12032a93233SIngo Molnar 2; 12132a93233SIngo Molnar #endif 122a62eaf15SAndi Kleen 12383d116c5SJia He #ifndef arch_faults_on_old_pte 12483d116c5SJia He static inline bool arch_faults_on_old_pte(void) 12583d116c5SJia He { 12683d116c5SJia He /* 12783d116c5SJia He * Those arches which don't have hw access flag feature need to 12883d116c5SJia He * implement their own helper. By default, "true" means pagefault 12983d116c5SJia He * will be hit on old pte. 13083d116c5SJia He */ 13183d116c5SJia He return true; 13283d116c5SJia He } 13383d116c5SJia He #endif 13483d116c5SJia He 135a62eaf15SAndi Kleen static int __init disable_randmaps(char *s) 136a62eaf15SAndi Kleen { 137a62eaf15SAndi Kleen randomize_va_space = 0; 1389b41046cSOGAWA Hirofumi return 1; 139a62eaf15SAndi Kleen } 140a62eaf15SAndi Kleen __setup("norandmaps", disable_randmaps); 141a62eaf15SAndi Kleen 14262eede62SHugh Dickins unsigned long zero_pfn __read_mostly; 1430b70068eSArd Biesheuvel EXPORT_SYMBOL(zero_pfn); 1440b70068eSArd Biesheuvel 145166f61b9STobin C Harding unsigned long highest_memmap_pfn __read_mostly; 146166f61b9STobin C Harding 147a13ea5b7SHugh Dickins /* 148a13ea5b7SHugh Dickins * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init() 149a13ea5b7SHugh Dickins */ 150a13ea5b7SHugh Dickins static int __init init_zero_pfn(void) 151a13ea5b7SHugh Dickins { 152a13ea5b7SHugh Dickins zero_pfn = page_to_pfn(ZERO_PAGE(0)); 153a13ea5b7SHugh Dickins return 0; 154a13ea5b7SHugh Dickins } 155a13ea5b7SHugh Dickins core_initcall(init_zero_pfn); 156a62eaf15SAndi Kleen 157*b3d1411bSJoel Fernandes (Google) void mm_trace_rss_stat(int member, long count) 158*b3d1411bSJoel Fernandes (Google) { 159*b3d1411bSJoel Fernandes (Google) trace_rss_stat(member, count); 160*b3d1411bSJoel Fernandes (Google) } 161d559db08SKAMEZAWA Hiroyuki 16234e55232SKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING) 16334e55232SKAMEZAWA Hiroyuki 164ea48cf78SDavid Rientjes void sync_mm_rss(struct mm_struct *mm) 16534e55232SKAMEZAWA Hiroyuki { 16634e55232SKAMEZAWA Hiroyuki int i; 16734e55232SKAMEZAWA Hiroyuki 16834e55232SKAMEZAWA Hiroyuki for (i = 0; i < NR_MM_COUNTERS; i++) { 16905af2e10SDavid Rientjes if (current->rss_stat.count[i]) { 17005af2e10SDavid Rientjes add_mm_counter(mm, i, current->rss_stat.count[i]); 17105af2e10SDavid Rientjes current->rss_stat.count[i] = 0; 17234e55232SKAMEZAWA Hiroyuki } 17334e55232SKAMEZAWA Hiroyuki } 17405af2e10SDavid Rientjes current->rss_stat.events = 0; 17534e55232SKAMEZAWA Hiroyuki } 17634e55232SKAMEZAWA Hiroyuki 17734e55232SKAMEZAWA Hiroyuki static void add_mm_counter_fast(struct mm_struct *mm, int member, int val) 17834e55232SKAMEZAWA Hiroyuki { 17934e55232SKAMEZAWA Hiroyuki struct task_struct *task = current; 18034e55232SKAMEZAWA Hiroyuki 18134e55232SKAMEZAWA Hiroyuki if (likely(task->mm == mm)) 18234e55232SKAMEZAWA Hiroyuki task->rss_stat.count[member] += val; 18334e55232SKAMEZAWA Hiroyuki else 18434e55232SKAMEZAWA Hiroyuki add_mm_counter(mm, member, val); 18534e55232SKAMEZAWA Hiroyuki } 18634e55232SKAMEZAWA Hiroyuki #define inc_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, 1) 18734e55232SKAMEZAWA Hiroyuki #define dec_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, -1) 18834e55232SKAMEZAWA Hiroyuki 18934e55232SKAMEZAWA Hiroyuki /* sync counter once per 64 page faults */ 19034e55232SKAMEZAWA Hiroyuki #define TASK_RSS_EVENTS_THRESH (64) 19134e55232SKAMEZAWA Hiroyuki static void check_sync_rss_stat(struct task_struct *task) 19234e55232SKAMEZAWA Hiroyuki { 19334e55232SKAMEZAWA Hiroyuki if (unlikely(task != current)) 19434e55232SKAMEZAWA Hiroyuki return; 19534e55232SKAMEZAWA Hiroyuki if (unlikely(task->rss_stat.events++ > TASK_RSS_EVENTS_THRESH)) 196ea48cf78SDavid Rientjes sync_mm_rss(task->mm); 19734e55232SKAMEZAWA Hiroyuki } 1989547d01bSPeter Zijlstra #else /* SPLIT_RSS_COUNTING */ 19934e55232SKAMEZAWA Hiroyuki 20034e55232SKAMEZAWA Hiroyuki #define inc_mm_counter_fast(mm, member) inc_mm_counter(mm, member) 20134e55232SKAMEZAWA Hiroyuki #define dec_mm_counter_fast(mm, member) dec_mm_counter(mm, member) 20234e55232SKAMEZAWA Hiroyuki 20334e55232SKAMEZAWA Hiroyuki static void check_sync_rss_stat(struct task_struct *task) 20434e55232SKAMEZAWA Hiroyuki { 20534e55232SKAMEZAWA Hiroyuki } 20634e55232SKAMEZAWA Hiroyuki 2079547d01bSPeter Zijlstra #endif /* SPLIT_RSS_COUNTING */ 2089547d01bSPeter Zijlstra 2091da177e4SLinus Torvalds /* 2101da177e4SLinus Torvalds * Note: this doesn't free the actual pages themselves. That 2111da177e4SLinus Torvalds * has been handled earlier when unmapping all the memory regions. 2121da177e4SLinus Torvalds */ 2139e1b32caSBenjamin Herrenschmidt static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd, 2149e1b32caSBenjamin Herrenschmidt unsigned long addr) 2151da177e4SLinus Torvalds { 2162f569afdSMartin Schwidefsky pgtable_t token = pmd_pgtable(*pmd); 2171da177e4SLinus Torvalds pmd_clear(pmd); 2189e1b32caSBenjamin Herrenschmidt pte_free_tlb(tlb, token, addr); 219c4812909SKirill A. Shutemov mm_dec_nr_ptes(tlb->mm); 2201da177e4SLinus Torvalds } 2211da177e4SLinus Torvalds 222e0da382cSHugh Dickins static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud, 223e0da382cSHugh Dickins unsigned long addr, unsigned long end, 224e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 2251da177e4SLinus Torvalds { 2261da177e4SLinus Torvalds pmd_t *pmd; 2271da177e4SLinus Torvalds unsigned long next; 228e0da382cSHugh Dickins unsigned long start; 2291da177e4SLinus Torvalds 230e0da382cSHugh Dickins start = addr; 2311da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 2321da177e4SLinus Torvalds do { 2331da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 2341da177e4SLinus Torvalds if (pmd_none_or_clear_bad(pmd)) 2351da177e4SLinus Torvalds continue; 2369e1b32caSBenjamin Herrenschmidt free_pte_range(tlb, pmd, addr); 2371da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 2381da177e4SLinus Torvalds 239e0da382cSHugh Dickins start &= PUD_MASK; 240e0da382cSHugh Dickins if (start < floor) 241e0da382cSHugh Dickins return; 242e0da382cSHugh Dickins if (ceiling) { 243e0da382cSHugh Dickins ceiling &= PUD_MASK; 244e0da382cSHugh Dickins if (!ceiling) 245e0da382cSHugh Dickins return; 2461da177e4SLinus Torvalds } 247e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 248e0da382cSHugh Dickins return; 249e0da382cSHugh Dickins 250e0da382cSHugh Dickins pmd = pmd_offset(pud, start); 251e0da382cSHugh Dickins pud_clear(pud); 2529e1b32caSBenjamin Herrenschmidt pmd_free_tlb(tlb, pmd, start); 253dc6c9a35SKirill A. Shutemov mm_dec_nr_pmds(tlb->mm); 2541da177e4SLinus Torvalds } 2551da177e4SLinus Torvalds 256c2febafcSKirill A. Shutemov static inline void free_pud_range(struct mmu_gather *tlb, p4d_t *p4d, 257e0da382cSHugh Dickins unsigned long addr, unsigned long end, 258e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 2591da177e4SLinus Torvalds { 2601da177e4SLinus Torvalds pud_t *pud; 2611da177e4SLinus Torvalds unsigned long next; 262e0da382cSHugh Dickins unsigned long start; 2631da177e4SLinus Torvalds 264e0da382cSHugh Dickins start = addr; 265c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 2661da177e4SLinus Torvalds do { 2671da177e4SLinus Torvalds next = pud_addr_end(addr, end); 2681da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 2691da177e4SLinus Torvalds continue; 270e0da382cSHugh Dickins free_pmd_range(tlb, pud, addr, next, floor, ceiling); 2711da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 2721da177e4SLinus Torvalds 273c2febafcSKirill A. Shutemov start &= P4D_MASK; 274c2febafcSKirill A. Shutemov if (start < floor) 275c2febafcSKirill A. Shutemov return; 276c2febafcSKirill A. Shutemov if (ceiling) { 277c2febafcSKirill A. Shutemov ceiling &= P4D_MASK; 278c2febafcSKirill A. Shutemov if (!ceiling) 279c2febafcSKirill A. Shutemov return; 280c2febafcSKirill A. Shutemov } 281c2febafcSKirill A. Shutemov if (end - 1 > ceiling - 1) 282c2febafcSKirill A. Shutemov return; 283c2febafcSKirill A. Shutemov 284c2febafcSKirill A. Shutemov pud = pud_offset(p4d, start); 285c2febafcSKirill A. Shutemov p4d_clear(p4d); 286c2febafcSKirill A. Shutemov pud_free_tlb(tlb, pud, start); 287b4e98d9aSKirill A. Shutemov mm_dec_nr_puds(tlb->mm); 288c2febafcSKirill A. Shutemov } 289c2febafcSKirill A. Shutemov 290c2febafcSKirill A. Shutemov static inline void free_p4d_range(struct mmu_gather *tlb, pgd_t *pgd, 291c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 292c2febafcSKirill A. Shutemov unsigned long floor, unsigned long ceiling) 293c2febafcSKirill A. Shutemov { 294c2febafcSKirill A. Shutemov p4d_t *p4d; 295c2febafcSKirill A. Shutemov unsigned long next; 296c2febafcSKirill A. Shutemov unsigned long start; 297c2febafcSKirill A. Shutemov 298c2febafcSKirill A. Shutemov start = addr; 299c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 300c2febafcSKirill A. Shutemov do { 301c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 302c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(p4d)) 303c2febafcSKirill A. Shutemov continue; 304c2febafcSKirill A. Shutemov free_pud_range(tlb, p4d, addr, next, floor, ceiling); 305c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 306c2febafcSKirill A. Shutemov 307e0da382cSHugh Dickins start &= PGDIR_MASK; 308e0da382cSHugh Dickins if (start < floor) 309e0da382cSHugh Dickins return; 310e0da382cSHugh Dickins if (ceiling) { 311e0da382cSHugh Dickins ceiling &= PGDIR_MASK; 312e0da382cSHugh Dickins if (!ceiling) 313e0da382cSHugh Dickins return; 3141da177e4SLinus Torvalds } 315e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 316e0da382cSHugh Dickins return; 317e0da382cSHugh Dickins 318c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, start); 319e0da382cSHugh Dickins pgd_clear(pgd); 320c2febafcSKirill A. Shutemov p4d_free_tlb(tlb, p4d, start); 3211da177e4SLinus Torvalds } 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds /* 324e0da382cSHugh Dickins * This function frees user-level page tables of a process. 3251da177e4SLinus Torvalds */ 32642b77728SJan Beulich void free_pgd_range(struct mmu_gather *tlb, 327e0da382cSHugh Dickins unsigned long addr, unsigned long end, 328e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 3291da177e4SLinus Torvalds { 3301da177e4SLinus Torvalds pgd_t *pgd; 3311da177e4SLinus Torvalds unsigned long next; 3321da177e4SLinus Torvalds 333e0da382cSHugh Dickins /* 334e0da382cSHugh Dickins * The next few lines have given us lots of grief... 335e0da382cSHugh Dickins * 336e0da382cSHugh Dickins * Why are we testing PMD* at this top level? Because often 337e0da382cSHugh Dickins * there will be no work to do at all, and we'd prefer not to 338e0da382cSHugh Dickins * go all the way down to the bottom just to discover that. 339e0da382cSHugh Dickins * 340e0da382cSHugh Dickins * Why all these "- 1"s? Because 0 represents both the bottom 341e0da382cSHugh Dickins * of the address space and the top of it (using -1 for the 342e0da382cSHugh Dickins * top wouldn't help much: the masks would do the wrong thing). 343e0da382cSHugh Dickins * The rule is that addr 0 and floor 0 refer to the bottom of 344e0da382cSHugh Dickins * the address space, but end 0 and ceiling 0 refer to the top 345e0da382cSHugh Dickins * Comparisons need to use "end - 1" and "ceiling - 1" (though 346e0da382cSHugh Dickins * that end 0 case should be mythical). 347e0da382cSHugh Dickins * 348e0da382cSHugh Dickins * Wherever addr is brought up or ceiling brought down, we must 349e0da382cSHugh Dickins * be careful to reject "the opposite 0" before it confuses the 350e0da382cSHugh Dickins * subsequent tests. But what about where end is brought down 351e0da382cSHugh Dickins * by PMD_SIZE below? no, end can't go down to 0 there. 352e0da382cSHugh Dickins * 353e0da382cSHugh Dickins * Whereas we round start (addr) and ceiling down, by different 354e0da382cSHugh Dickins * masks at different levels, in order to test whether a table 355e0da382cSHugh Dickins * now has no other vmas using it, so can be freed, we don't 356e0da382cSHugh Dickins * bother to round floor or end up - the tests don't need that. 357e0da382cSHugh Dickins */ 358e0da382cSHugh Dickins 359e0da382cSHugh Dickins addr &= PMD_MASK; 360e0da382cSHugh Dickins if (addr < floor) { 361e0da382cSHugh Dickins addr += PMD_SIZE; 362e0da382cSHugh Dickins if (!addr) 363e0da382cSHugh Dickins return; 364e0da382cSHugh Dickins } 365e0da382cSHugh Dickins if (ceiling) { 366e0da382cSHugh Dickins ceiling &= PMD_MASK; 367e0da382cSHugh Dickins if (!ceiling) 368e0da382cSHugh Dickins return; 369e0da382cSHugh Dickins } 370e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 371e0da382cSHugh Dickins end -= PMD_SIZE; 372e0da382cSHugh Dickins if (addr > end - 1) 373e0da382cSHugh Dickins return; 37407e32661SAneesh Kumar K.V /* 37507e32661SAneesh Kumar K.V * We add page table cache pages with PAGE_SIZE, 37607e32661SAneesh Kumar K.V * (see pte_free_tlb()), flush the tlb if we need 37707e32661SAneesh Kumar K.V */ 378ed6a7935SPeter Zijlstra tlb_change_page_size(tlb, PAGE_SIZE); 37942b77728SJan Beulich pgd = pgd_offset(tlb->mm, addr); 3801da177e4SLinus Torvalds do { 3811da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 3821da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 3831da177e4SLinus Torvalds continue; 384c2febafcSKirill A. Shutemov free_p4d_range(tlb, pgd, addr, next, floor, ceiling); 3851da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 386e0da382cSHugh Dickins } 387e0da382cSHugh Dickins 38842b77728SJan Beulich void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma, 389e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 390e0da382cSHugh Dickins { 391e0da382cSHugh Dickins while (vma) { 392e0da382cSHugh Dickins struct vm_area_struct *next = vma->vm_next; 393e0da382cSHugh Dickins unsigned long addr = vma->vm_start; 394e0da382cSHugh Dickins 3958f4f8c16SHugh Dickins /* 39625d9e2d1Snpiggin@suse.de * Hide vma from rmap and truncate_pagecache before freeing 39725d9e2d1Snpiggin@suse.de * pgtables 3988f4f8c16SHugh Dickins */ 3995beb4930SRik van Riel unlink_anon_vmas(vma); 4008f4f8c16SHugh Dickins unlink_file_vma(vma); 4018f4f8c16SHugh Dickins 4029da61aefSDavid Gibson if (is_vm_hugetlb_page(vma)) { 4033bf5ee95SHugh Dickins hugetlb_free_pgd_range(tlb, addr, vma->vm_end, 4043bf5ee95SHugh Dickins floor, next ? next->vm_start : ceiling); 4053bf5ee95SHugh Dickins } else { 4063bf5ee95SHugh Dickins /* 4073bf5ee95SHugh Dickins * Optimization: gather nearby vmas into one call down 4083bf5ee95SHugh Dickins */ 4093bf5ee95SHugh Dickins while (next && next->vm_start <= vma->vm_end + PMD_SIZE 4104866920bSDavid Gibson && !is_vm_hugetlb_page(next)) { 411e0da382cSHugh Dickins vma = next; 412e0da382cSHugh Dickins next = vma->vm_next; 4135beb4930SRik van Riel unlink_anon_vmas(vma); 4148f4f8c16SHugh Dickins unlink_file_vma(vma); 415e0da382cSHugh Dickins } 4163bf5ee95SHugh Dickins free_pgd_range(tlb, addr, vma->vm_end, 417e0da382cSHugh Dickins floor, next ? next->vm_start : ceiling); 4183bf5ee95SHugh Dickins } 419e0da382cSHugh Dickins vma = next; 420e0da382cSHugh Dickins } 4211da177e4SLinus Torvalds } 4221da177e4SLinus Torvalds 4234cf58924SJoel Fernandes (Google) int __pte_alloc(struct mm_struct *mm, pmd_t *pmd) 4241da177e4SLinus Torvalds { 425c4088ebdSKirill A. Shutemov spinlock_t *ptl; 4264cf58924SJoel Fernandes (Google) pgtable_t new = pte_alloc_one(mm); 4271da177e4SLinus Torvalds if (!new) 4281bb3630eSHugh Dickins return -ENOMEM; 4291bb3630eSHugh Dickins 430362a61adSNick Piggin /* 431362a61adSNick Piggin * Ensure all pte setup (eg. pte page lock and page clearing) are 432362a61adSNick Piggin * visible before the pte is made visible to other CPUs by being 433362a61adSNick Piggin * put into page tables. 434362a61adSNick Piggin * 435362a61adSNick Piggin * The other side of the story is the pointer chasing in the page 436362a61adSNick Piggin * table walking code (when walking the page table without locking; 437362a61adSNick Piggin * ie. most of the time). Fortunately, these data accesses consist 438362a61adSNick Piggin * of a chain of data-dependent loads, meaning most CPUs (alpha 439362a61adSNick Piggin * being the notable exception) will already guarantee loads are 440362a61adSNick Piggin * seen in-order. See the alpha page table accessors for the 441362a61adSNick Piggin * smp_read_barrier_depends() barriers in page table walking code. 442362a61adSNick Piggin */ 443362a61adSNick Piggin smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */ 444362a61adSNick Piggin 445c4088ebdSKirill A. Shutemov ptl = pmd_lock(mm, pmd); 4468ac1f832SAndrea Arcangeli if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 447c4812909SKirill A. Shutemov mm_inc_nr_ptes(mm); 4481da177e4SLinus Torvalds pmd_populate(mm, pmd, new); 4492f569afdSMartin Schwidefsky new = NULL; 4504b471e88SKirill A. Shutemov } 451c4088ebdSKirill A. Shutemov spin_unlock(ptl); 4522f569afdSMartin Schwidefsky if (new) 4532f569afdSMartin Schwidefsky pte_free(mm, new); 4541bb3630eSHugh Dickins return 0; 4551da177e4SLinus Torvalds } 4561da177e4SLinus Torvalds 4574cf58924SJoel Fernandes (Google) int __pte_alloc_kernel(pmd_t *pmd) 4581da177e4SLinus Torvalds { 4594cf58924SJoel Fernandes (Google) pte_t *new = pte_alloc_one_kernel(&init_mm); 4601da177e4SLinus Torvalds if (!new) 4611bb3630eSHugh Dickins return -ENOMEM; 4621da177e4SLinus Torvalds 463362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 464362a61adSNick Piggin 465872fec16SHugh Dickins spin_lock(&init_mm.page_table_lock); 4668ac1f832SAndrea Arcangeli if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 467872fec16SHugh Dickins pmd_populate_kernel(&init_mm, pmd, new); 4682f569afdSMartin Schwidefsky new = NULL; 4694b471e88SKirill A. Shutemov } 470872fec16SHugh Dickins spin_unlock(&init_mm.page_table_lock); 4712f569afdSMartin Schwidefsky if (new) 4722f569afdSMartin Schwidefsky pte_free_kernel(&init_mm, new); 4731bb3630eSHugh Dickins return 0; 4741da177e4SLinus Torvalds } 4751da177e4SLinus Torvalds 476d559db08SKAMEZAWA Hiroyuki static inline void init_rss_vec(int *rss) 477ae859762SHugh Dickins { 478d559db08SKAMEZAWA Hiroyuki memset(rss, 0, sizeof(int) * NR_MM_COUNTERS); 479d559db08SKAMEZAWA Hiroyuki } 480d559db08SKAMEZAWA Hiroyuki 481d559db08SKAMEZAWA Hiroyuki static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss) 482d559db08SKAMEZAWA Hiroyuki { 483d559db08SKAMEZAWA Hiroyuki int i; 484d559db08SKAMEZAWA Hiroyuki 48534e55232SKAMEZAWA Hiroyuki if (current->mm == mm) 48605af2e10SDavid Rientjes sync_mm_rss(mm); 487d559db08SKAMEZAWA Hiroyuki for (i = 0; i < NR_MM_COUNTERS; i++) 488d559db08SKAMEZAWA Hiroyuki if (rss[i]) 489d559db08SKAMEZAWA Hiroyuki add_mm_counter(mm, i, rss[i]); 490ae859762SHugh Dickins } 491ae859762SHugh Dickins 4921da177e4SLinus Torvalds /* 4936aab341eSLinus Torvalds * This function is called to print an error when a bad pte 4946aab341eSLinus Torvalds * is found. For example, we might have a PFN-mapped pte in 4956aab341eSLinus Torvalds * a region that doesn't allow it. 496b5810039SNick Piggin * 497b5810039SNick Piggin * The calling function must still handle the error. 498b5810039SNick Piggin */ 4993dc14741SHugh Dickins static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr, 5003dc14741SHugh Dickins pte_t pte, struct page *page) 501b5810039SNick Piggin { 5023dc14741SHugh Dickins pgd_t *pgd = pgd_offset(vma->vm_mm, addr); 503c2febafcSKirill A. Shutemov p4d_t *p4d = p4d_offset(pgd, addr); 504c2febafcSKirill A. Shutemov pud_t *pud = pud_offset(p4d, addr); 5053dc14741SHugh Dickins pmd_t *pmd = pmd_offset(pud, addr); 5063dc14741SHugh Dickins struct address_space *mapping; 5073dc14741SHugh Dickins pgoff_t index; 508d936cf9bSHugh Dickins static unsigned long resume; 509d936cf9bSHugh Dickins static unsigned long nr_shown; 510d936cf9bSHugh Dickins static unsigned long nr_unshown; 511d936cf9bSHugh Dickins 512d936cf9bSHugh Dickins /* 513d936cf9bSHugh Dickins * Allow a burst of 60 reports, then keep quiet for that minute; 514d936cf9bSHugh Dickins * or allow a steady drip of one report per second. 515d936cf9bSHugh Dickins */ 516d936cf9bSHugh Dickins if (nr_shown == 60) { 517d936cf9bSHugh Dickins if (time_before(jiffies, resume)) { 518d936cf9bSHugh Dickins nr_unshown++; 519d936cf9bSHugh Dickins return; 520d936cf9bSHugh Dickins } 521d936cf9bSHugh Dickins if (nr_unshown) { 5221170532bSJoe Perches pr_alert("BUG: Bad page map: %lu messages suppressed\n", 523d936cf9bSHugh Dickins nr_unshown); 524d936cf9bSHugh Dickins nr_unshown = 0; 525d936cf9bSHugh Dickins } 526d936cf9bSHugh Dickins nr_shown = 0; 527d936cf9bSHugh Dickins } 528d936cf9bSHugh Dickins if (nr_shown++ == 0) 529d936cf9bSHugh Dickins resume = jiffies + 60 * HZ; 5303dc14741SHugh Dickins 5313dc14741SHugh Dickins mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL; 5323dc14741SHugh Dickins index = linear_page_index(vma, addr); 5333dc14741SHugh Dickins 5341170532bSJoe Perches pr_alert("BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n", 5353dc14741SHugh Dickins current->comm, 5363dc14741SHugh Dickins (long long)pte_val(pte), (long long)pmd_val(*pmd)); 537718a3821SWu Fengguang if (page) 538f0b791a3SDave Hansen dump_page(page, "bad pte"); 5396aa9b8b2SKefeng Wang pr_alert("addr:%px vm_flags:%08lx anon_vma:%px mapping:%px index:%lx\n", 5403dc14741SHugh Dickins (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index); 541d75f773cSSakari Ailus pr_alert("file:%pD fault:%ps mmap:%ps readpage:%ps\n", 5422682582aSKonstantin Khlebnikov vma->vm_file, 5432682582aSKonstantin Khlebnikov vma->vm_ops ? vma->vm_ops->fault : NULL, 5442682582aSKonstantin Khlebnikov vma->vm_file ? vma->vm_file->f_op->mmap : NULL, 5452682582aSKonstantin Khlebnikov mapping ? mapping->a_ops->readpage : NULL); 546b5810039SNick Piggin dump_stack(); 547373d4d09SRusty Russell add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); 548b5810039SNick Piggin } 549b5810039SNick Piggin 550b5810039SNick Piggin /* 5517e675137SNick Piggin * vm_normal_page -- This function gets the "struct page" associated with a pte. 5526aab341eSLinus Torvalds * 5537e675137SNick Piggin * "Special" mappings do not wish to be associated with a "struct page" (either 5547e675137SNick Piggin * it doesn't exist, or it exists but they don't want to touch it). In this 5557e675137SNick Piggin * case, NULL is returned here. "Normal" mappings do have a struct page. 556b379d790SJared Hulbert * 5577e675137SNick Piggin * There are 2 broad cases. Firstly, an architecture may define a pte_special() 5587e675137SNick Piggin * pte bit, in which case this function is trivial. Secondly, an architecture 5597e675137SNick Piggin * may not have a spare pte bit, which requires a more complicated scheme, 5607e675137SNick Piggin * described below. 5617e675137SNick Piggin * 5627e675137SNick Piggin * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a 5637e675137SNick Piggin * special mapping (even if there are underlying and valid "struct pages"). 5647e675137SNick Piggin * COWed pages of a VM_PFNMAP are always normal. 5656aab341eSLinus Torvalds * 566b379d790SJared Hulbert * The way we recognize COWed pages within VM_PFNMAP mappings is through the 567b379d790SJared Hulbert * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit 5687e675137SNick Piggin * set, and the vm_pgoff will point to the first PFN mapped: thus every special 5697e675137SNick Piggin * mapping will always honor the rule 5706aab341eSLinus Torvalds * 5716aab341eSLinus Torvalds * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT) 5726aab341eSLinus Torvalds * 5737e675137SNick Piggin * And for normal mappings this is false. 574b379d790SJared Hulbert * 5757e675137SNick Piggin * This restricts such mappings to be a linear translation from virtual address 5767e675137SNick Piggin * to pfn. To get around this restriction, we allow arbitrary mappings so long 5777e675137SNick Piggin * as the vma is not a COW mapping; in that case, we know that all ptes are 5787e675137SNick Piggin * special (because none can have been COWed). 579b379d790SJared Hulbert * 580b379d790SJared Hulbert * 5817e675137SNick Piggin * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP. 5827e675137SNick Piggin * 583b379d790SJared Hulbert * VM_MIXEDMAP mappings can likewise contain memory with or without "struct 584b379d790SJared Hulbert * page" backing, however the difference is that _all_ pages with a struct 585b379d790SJared Hulbert * page (that is, those where pfn_valid is true) are refcounted and considered 586b379d790SJared Hulbert * normal pages by the VM. The disadvantage is that pages are refcounted 587b379d790SJared Hulbert * (which can be slower and simply not an option for some PFNMAP users). The 588b379d790SJared Hulbert * advantage is that we don't have to follow the strict linearity rule of 589b379d790SJared Hulbert * PFNMAP mappings in order to support COWable mappings. 590b379d790SJared Hulbert * 591ee498ed7SHugh Dickins */ 59225b2995aSChristoph Hellwig struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, 59325b2995aSChristoph Hellwig pte_t pte) 594ee498ed7SHugh Dickins { 59522b31eecSHugh Dickins unsigned long pfn = pte_pfn(pte); 5967e675137SNick Piggin 59700b3a331SLaurent Dufour if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL)) { 598b38af472SHugh Dickins if (likely(!pte_special(pte))) 59922b31eecSHugh Dickins goto check_pfn; 600667a0a06SDavid Vrabel if (vma->vm_ops && vma->vm_ops->find_special_page) 601667a0a06SDavid Vrabel return vma->vm_ops->find_special_page(vma, addr); 602a13ea5b7SHugh Dickins if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)) 603a13ea5b7SHugh Dickins return NULL; 604df6ad698SJérôme Glisse if (is_zero_pfn(pfn)) 605df6ad698SJérôme Glisse return NULL; 606e1fb4a08SDave Jiang if (pte_devmap(pte)) 607e1fb4a08SDave Jiang return NULL; 608e1fb4a08SDave Jiang 60922b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 6107e675137SNick Piggin return NULL; 6117e675137SNick Piggin } 6127e675137SNick Piggin 61300b3a331SLaurent Dufour /* !CONFIG_ARCH_HAS_PTE_SPECIAL case follows: */ 6147e675137SNick Piggin 615b379d790SJared Hulbert if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { 616b379d790SJared Hulbert if (vma->vm_flags & VM_MIXEDMAP) { 617b379d790SJared Hulbert if (!pfn_valid(pfn)) 618b379d790SJared Hulbert return NULL; 619b379d790SJared Hulbert goto out; 620b379d790SJared Hulbert } else { 6217e675137SNick Piggin unsigned long off; 6227e675137SNick Piggin off = (addr - vma->vm_start) >> PAGE_SHIFT; 6236aab341eSLinus Torvalds if (pfn == vma->vm_pgoff + off) 6246aab341eSLinus Torvalds return NULL; 62567121172SLinus Torvalds if (!is_cow_mapping(vma->vm_flags)) 626fb155c16SLinus Torvalds return NULL; 6276aab341eSLinus Torvalds } 628b379d790SJared Hulbert } 6296aab341eSLinus Torvalds 630b38af472SHugh Dickins if (is_zero_pfn(pfn)) 631b38af472SHugh Dickins return NULL; 63200b3a331SLaurent Dufour 63322b31eecSHugh Dickins check_pfn: 63422b31eecSHugh Dickins if (unlikely(pfn > highest_memmap_pfn)) { 63522b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 63622b31eecSHugh Dickins return NULL; 63722b31eecSHugh Dickins } 6386aab341eSLinus Torvalds 6396aab341eSLinus Torvalds /* 6407e675137SNick Piggin * NOTE! We still have PageReserved() pages in the page tables. 6417e675137SNick Piggin * eg. VDSO mappings can cause them to exist. 6426aab341eSLinus Torvalds */ 643b379d790SJared Hulbert out: 6446aab341eSLinus Torvalds return pfn_to_page(pfn); 645ee498ed7SHugh Dickins } 646ee498ed7SHugh Dickins 64728093f9fSGerald Schaefer #ifdef CONFIG_TRANSPARENT_HUGEPAGE 64828093f9fSGerald Schaefer struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr, 64928093f9fSGerald Schaefer pmd_t pmd) 65028093f9fSGerald Schaefer { 65128093f9fSGerald Schaefer unsigned long pfn = pmd_pfn(pmd); 65228093f9fSGerald Schaefer 65328093f9fSGerald Schaefer /* 65428093f9fSGerald Schaefer * There is no pmd_special() but there may be special pmds, e.g. 65528093f9fSGerald Schaefer * in a direct-access (dax) mapping, so let's just replicate the 65600b3a331SLaurent Dufour * !CONFIG_ARCH_HAS_PTE_SPECIAL case from vm_normal_page() here. 65728093f9fSGerald Schaefer */ 65828093f9fSGerald Schaefer if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { 65928093f9fSGerald Schaefer if (vma->vm_flags & VM_MIXEDMAP) { 66028093f9fSGerald Schaefer if (!pfn_valid(pfn)) 66128093f9fSGerald Schaefer return NULL; 66228093f9fSGerald Schaefer goto out; 66328093f9fSGerald Schaefer } else { 66428093f9fSGerald Schaefer unsigned long off; 66528093f9fSGerald Schaefer off = (addr - vma->vm_start) >> PAGE_SHIFT; 66628093f9fSGerald Schaefer if (pfn == vma->vm_pgoff + off) 66728093f9fSGerald Schaefer return NULL; 66828093f9fSGerald Schaefer if (!is_cow_mapping(vma->vm_flags)) 66928093f9fSGerald Schaefer return NULL; 67028093f9fSGerald Schaefer } 67128093f9fSGerald Schaefer } 67228093f9fSGerald Schaefer 673e1fb4a08SDave Jiang if (pmd_devmap(pmd)) 674e1fb4a08SDave Jiang return NULL; 67528093f9fSGerald Schaefer if (is_zero_pfn(pfn)) 67628093f9fSGerald Schaefer return NULL; 67728093f9fSGerald Schaefer if (unlikely(pfn > highest_memmap_pfn)) 67828093f9fSGerald Schaefer return NULL; 67928093f9fSGerald Schaefer 68028093f9fSGerald Schaefer /* 68128093f9fSGerald Schaefer * NOTE! We still have PageReserved() pages in the page tables. 68228093f9fSGerald Schaefer * eg. VDSO mappings can cause them to exist. 68328093f9fSGerald Schaefer */ 68428093f9fSGerald Schaefer out: 68528093f9fSGerald Schaefer return pfn_to_page(pfn); 68628093f9fSGerald Schaefer } 68728093f9fSGerald Schaefer #endif 68828093f9fSGerald Schaefer 689ee498ed7SHugh Dickins /* 6901da177e4SLinus Torvalds * copy one vm_area from one task to the other. Assumes the page tables 6911da177e4SLinus Torvalds * already present in the new task to be cleared in the whole range 6921da177e4SLinus Torvalds * covered by this vma. 6931da177e4SLinus Torvalds */ 6941da177e4SLinus Torvalds 695570a335bSHugh Dickins static inline unsigned long 6961da177e4SLinus Torvalds copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, 697b5810039SNick Piggin pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma, 6988c103762SHugh Dickins unsigned long addr, int *rss) 6991da177e4SLinus Torvalds { 700b5810039SNick Piggin unsigned long vm_flags = vma->vm_flags; 7011da177e4SLinus Torvalds pte_t pte = *src_pte; 7021da177e4SLinus Torvalds struct page *page; 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds /* pte contains position in swap or file, so copy. */ 7051da177e4SLinus Torvalds if (unlikely(!pte_present(pte))) { 7060697212aSChristoph Lameter swp_entry_t entry = pte_to_swp_entry(pte); 7070697212aSChristoph Lameter 7082022b4d1SHugh Dickins if (likely(!non_swap_entry(entry))) { 709570a335bSHugh Dickins if (swap_duplicate(entry) < 0) 710570a335bSHugh Dickins return entry.val; 711570a335bSHugh Dickins 7121da177e4SLinus Torvalds /* make sure dst_mm is on swapoff's mmlist. */ 7131da177e4SLinus Torvalds if (unlikely(list_empty(&dst_mm->mmlist))) { 7141da177e4SLinus Torvalds spin_lock(&mmlist_lock); 715f412ac08SHugh Dickins if (list_empty(&dst_mm->mmlist)) 716f412ac08SHugh Dickins list_add(&dst_mm->mmlist, 717f412ac08SHugh Dickins &src_mm->mmlist); 7181da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 7191da177e4SLinus Torvalds } 720b084d435SKAMEZAWA Hiroyuki rss[MM_SWAPENTS]++; 7212022b4d1SHugh Dickins } else if (is_migration_entry(entry)) { 7229f9f1acdSKonstantin Khlebnikov page = migration_entry_to_page(entry); 7239f9f1acdSKonstantin Khlebnikov 724eca56ff9SJerome Marchand rss[mm_counter(page)]++; 7259f9f1acdSKonstantin Khlebnikov 7269f9f1acdSKonstantin Khlebnikov if (is_write_migration_entry(entry) && 7270697212aSChristoph Lameter is_cow_mapping(vm_flags)) { 7280697212aSChristoph Lameter /* 7299f9f1acdSKonstantin Khlebnikov * COW mappings require pages in both 7309f9f1acdSKonstantin Khlebnikov * parent and child to be set to read. 7310697212aSChristoph Lameter */ 7320697212aSChristoph Lameter make_migration_entry_read(&entry); 7330697212aSChristoph Lameter pte = swp_entry_to_pte(entry); 734c3d16e16SCyrill Gorcunov if (pte_swp_soft_dirty(*src_pte)) 735c3d16e16SCyrill Gorcunov pte = pte_swp_mksoft_dirty(pte); 7360697212aSChristoph Lameter set_pte_at(src_mm, addr, src_pte, pte); 7370697212aSChristoph Lameter } 7385042db43SJérôme Glisse } else if (is_device_private_entry(entry)) { 7395042db43SJérôme Glisse page = device_private_entry_to_page(entry); 7405042db43SJérôme Glisse 7415042db43SJérôme Glisse /* 7425042db43SJérôme Glisse * Update rss count even for unaddressable pages, as 7435042db43SJérôme Glisse * they should treated just like normal pages in this 7445042db43SJérôme Glisse * respect. 7455042db43SJérôme Glisse * 7465042db43SJérôme Glisse * We will likely want to have some new rss counters 7475042db43SJérôme Glisse * for unaddressable pages, at some point. But for now 7485042db43SJérôme Glisse * keep things as they are. 7495042db43SJérôme Glisse */ 7505042db43SJérôme Glisse get_page(page); 7515042db43SJérôme Glisse rss[mm_counter(page)]++; 7525042db43SJérôme Glisse page_dup_rmap(page, false); 7535042db43SJérôme Glisse 7545042db43SJérôme Glisse /* 7555042db43SJérôme Glisse * We do not preserve soft-dirty information, because so 7565042db43SJérôme Glisse * far, checkpoint/restore is the only feature that 7575042db43SJérôme Glisse * requires that. And checkpoint/restore does not work 7585042db43SJérôme Glisse * when a device driver is involved (you cannot easily 7595042db43SJérôme Glisse * save and restore device driver state). 7605042db43SJérôme Glisse */ 7615042db43SJérôme Glisse if (is_write_device_private_entry(entry) && 7625042db43SJérôme Glisse is_cow_mapping(vm_flags)) { 7635042db43SJérôme Glisse make_device_private_entry_read(&entry); 7645042db43SJérôme Glisse pte = swp_entry_to_pte(entry); 7655042db43SJérôme Glisse set_pte_at(src_mm, addr, src_pte, pte); 7665042db43SJérôme Glisse } 7671da177e4SLinus Torvalds } 768ae859762SHugh Dickins goto out_set_pte; 7691da177e4SLinus Torvalds } 7701da177e4SLinus Torvalds 7711da177e4SLinus Torvalds /* 7721da177e4SLinus Torvalds * If it's a COW mapping, write protect it both 7731da177e4SLinus Torvalds * in the parent and the child 7741da177e4SLinus Torvalds */ 7751b2de5d0SLinus Torvalds if (is_cow_mapping(vm_flags) && pte_write(pte)) { 7761da177e4SLinus Torvalds ptep_set_wrprotect(src_mm, addr, src_pte); 7773dc90795SZachary Amsden pte = pte_wrprotect(pte); 7781da177e4SLinus Torvalds } 7791da177e4SLinus Torvalds 7801da177e4SLinus Torvalds /* 7811da177e4SLinus Torvalds * If it's a shared mapping, mark it clean in 7821da177e4SLinus Torvalds * the child 7831da177e4SLinus Torvalds */ 7841da177e4SLinus Torvalds if (vm_flags & VM_SHARED) 7851da177e4SLinus Torvalds pte = pte_mkclean(pte); 7861da177e4SLinus Torvalds pte = pte_mkold(pte); 7876aab341eSLinus Torvalds 7886aab341eSLinus Torvalds page = vm_normal_page(vma, addr, pte); 7896aab341eSLinus Torvalds if (page) { 7901da177e4SLinus Torvalds get_page(page); 79153f9263bSKirill A. Shutemov page_dup_rmap(page, false); 792eca56ff9SJerome Marchand rss[mm_counter(page)]++; 793df6ad698SJérôme Glisse } else if (pte_devmap(pte)) { 794df6ad698SJérôme Glisse page = pte_page(pte); 7956aab341eSLinus Torvalds } 796ae859762SHugh Dickins 797ae859762SHugh Dickins out_set_pte: 798ae859762SHugh Dickins set_pte_at(dst_mm, addr, dst_pte, pte); 799570a335bSHugh Dickins return 0; 8001da177e4SLinus Torvalds } 8011da177e4SLinus Torvalds 80221bda264SJerome Marchand static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 8031da177e4SLinus Torvalds pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma, 8041da177e4SLinus Torvalds unsigned long addr, unsigned long end) 8051da177e4SLinus Torvalds { 806c36987e2SDaisuke Nishimura pte_t *orig_src_pte, *orig_dst_pte; 8071da177e4SLinus Torvalds pte_t *src_pte, *dst_pte; 808c74df32cSHugh Dickins spinlock_t *src_ptl, *dst_ptl; 809e040f218SHugh Dickins int progress = 0; 810d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 811570a335bSHugh Dickins swp_entry_t entry = (swp_entry_t){0}; 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds again: 814d559db08SKAMEZAWA Hiroyuki init_rss_vec(rss); 815d559db08SKAMEZAWA Hiroyuki 816c74df32cSHugh Dickins dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl); 8171da177e4SLinus Torvalds if (!dst_pte) 8181da177e4SLinus Torvalds return -ENOMEM; 819ece0e2b6SPeter Zijlstra src_pte = pte_offset_map(src_pmd, addr); 8204c21e2f2SHugh Dickins src_ptl = pte_lockptr(src_mm, src_pmd); 821f20dc5f7SIngo Molnar spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 822c36987e2SDaisuke Nishimura orig_src_pte = src_pte; 823c36987e2SDaisuke Nishimura orig_dst_pte = dst_pte; 8246606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 8251da177e4SLinus Torvalds 8261da177e4SLinus Torvalds do { 8271da177e4SLinus Torvalds /* 8281da177e4SLinus Torvalds * We are holding two locks at this point - either of them 8291da177e4SLinus Torvalds * could generate latencies in another task on another CPU. 8301da177e4SLinus Torvalds */ 831e040f218SHugh Dickins if (progress >= 32) { 832e040f218SHugh Dickins progress = 0; 833e040f218SHugh Dickins if (need_resched() || 83495c354feSNick Piggin spin_needbreak(src_ptl) || spin_needbreak(dst_ptl)) 8351da177e4SLinus Torvalds break; 836e040f218SHugh Dickins } 8371da177e4SLinus Torvalds if (pte_none(*src_pte)) { 8381da177e4SLinus Torvalds progress++; 8391da177e4SLinus Torvalds continue; 8401da177e4SLinus Torvalds } 841570a335bSHugh Dickins entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte, 842570a335bSHugh Dickins vma, addr, rss); 843570a335bSHugh Dickins if (entry.val) 844570a335bSHugh Dickins break; 8451da177e4SLinus Torvalds progress += 8; 8461da177e4SLinus Torvalds } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end); 8471da177e4SLinus Torvalds 8486606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 849c74df32cSHugh Dickins spin_unlock(src_ptl); 850ece0e2b6SPeter Zijlstra pte_unmap(orig_src_pte); 851d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(dst_mm, rss); 852c36987e2SDaisuke Nishimura pte_unmap_unlock(orig_dst_pte, dst_ptl); 853c74df32cSHugh Dickins cond_resched(); 854570a335bSHugh Dickins 855570a335bSHugh Dickins if (entry.val) { 856570a335bSHugh Dickins if (add_swap_count_continuation(entry, GFP_KERNEL) < 0) 857570a335bSHugh Dickins return -ENOMEM; 858570a335bSHugh Dickins progress = 0; 859570a335bSHugh Dickins } 8601da177e4SLinus Torvalds if (addr != end) 8611da177e4SLinus Torvalds goto again; 8621da177e4SLinus Torvalds return 0; 8631da177e4SLinus Torvalds } 8641da177e4SLinus Torvalds 8651da177e4SLinus Torvalds static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 8661da177e4SLinus Torvalds pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma, 8671da177e4SLinus Torvalds unsigned long addr, unsigned long end) 8681da177e4SLinus Torvalds { 8691da177e4SLinus Torvalds pmd_t *src_pmd, *dst_pmd; 8701da177e4SLinus Torvalds unsigned long next; 8711da177e4SLinus Torvalds 8721da177e4SLinus Torvalds dst_pmd = pmd_alloc(dst_mm, dst_pud, addr); 8731da177e4SLinus Torvalds if (!dst_pmd) 8741da177e4SLinus Torvalds return -ENOMEM; 8751da177e4SLinus Torvalds src_pmd = pmd_offset(src_pud, addr); 8761da177e4SLinus Torvalds do { 8771da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 87884c3fc4eSZi Yan if (is_swap_pmd(*src_pmd) || pmd_trans_huge(*src_pmd) 87984c3fc4eSZi Yan || pmd_devmap(*src_pmd)) { 88071e3aac0SAndrea Arcangeli int err; 881a00cc7d9SMatthew Wilcox VM_BUG_ON_VMA(next-addr != HPAGE_PMD_SIZE, vma); 88271e3aac0SAndrea Arcangeli err = copy_huge_pmd(dst_mm, src_mm, 88371e3aac0SAndrea Arcangeli dst_pmd, src_pmd, addr, vma); 88471e3aac0SAndrea Arcangeli if (err == -ENOMEM) 88571e3aac0SAndrea Arcangeli return -ENOMEM; 88671e3aac0SAndrea Arcangeli if (!err) 88771e3aac0SAndrea Arcangeli continue; 88871e3aac0SAndrea Arcangeli /* fall through */ 88971e3aac0SAndrea Arcangeli } 8901da177e4SLinus Torvalds if (pmd_none_or_clear_bad(src_pmd)) 8911da177e4SLinus Torvalds continue; 8921da177e4SLinus Torvalds if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd, 8931da177e4SLinus Torvalds vma, addr, next)) 8941da177e4SLinus Torvalds return -ENOMEM; 8951da177e4SLinus Torvalds } while (dst_pmd++, src_pmd++, addr = next, addr != end); 8961da177e4SLinus Torvalds return 0; 8971da177e4SLinus Torvalds } 8981da177e4SLinus Torvalds 8991da177e4SLinus Torvalds static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 900c2febafcSKirill A. Shutemov p4d_t *dst_p4d, p4d_t *src_p4d, struct vm_area_struct *vma, 9011da177e4SLinus Torvalds unsigned long addr, unsigned long end) 9021da177e4SLinus Torvalds { 9031da177e4SLinus Torvalds pud_t *src_pud, *dst_pud; 9041da177e4SLinus Torvalds unsigned long next; 9051da177e4SLinus Torvalds 906c2febafcSKirill A. Shutemov dst_pud = pud_alloc(dst_mm, dst_p4d, addr); 9071da177e4SLinus Torvalds if (!dst_pud) 9081da177e4SLinus Torvalds return -ENOMEM; 909c2febafcSKirill A. Shutemov src_pud = pud_offset(src_p4d, addr); 9101da177e4SLinus Torvalds do { 9111da177e4SLinus Torvalds next = pud_addr_end(addr, end); 912a00cc7d9SMatthew Wilcox if (pud_trans_huge(*src_pud) || pud_devmap(*src_pud)) { 913a00cc7d9SMatthew Wilcox int err; 914a00cc7d9SMatthew Wilcox 915a00cc7d9SMatthew Wilcox VM_BUG_ON_VMA(next-addr != HPAGE_PUD_SIZE, vma); 916a00cc7d9SMatthew Wilcox err = copy_huge_pud(dst_mm, src_mm, 917a00cc7d9SMatthew Wilcox dst_pud, src_pud, addr, vma); 918a00cc7d9SMatthew Wilcox if (err == -ENOMEM) 919a00cc7d9SMatthew Wilcox return -ENOMEM; 920a00cc7d9SMatthew Wilcox if (!err) 921a00cc7d9SMatthew Wilcox continue; 922a00cc7d9SMatthew Wilcox /* fall through */ 923a00cc7d9SMatthew Wilcox } 9241da177e4SLinus Torvalds if (pud_none_or_clear_bad(src_pud)) 9251da177e4SLinus Torvalds continue; 9261da177e4SLinus Torvalds if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud, 9271da177e4SLinus Torvalds vma, addr, next)) 9281da177e4SLinus Torvalds return -ENOMEM; 9291da177e4SLinus Torvalds } while (dst_pud++, src_pud++, addr = next, addr != end); 9301da177e4SLinus Torvalds return 0; 9311da177e4SLinus Torvalds } 9321da177e4SLinus Torvalds 933c2febafcSKirill A. Shutemov static inline int copy_p4d_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 934c2febafcSKirill A. Shutemov pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma, 935c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end) 936c2febafcSKirill A. Shutemov { 937c2febafcSKirill A. Shutemov p4d_t *src_p4d, *dst_p4d; 938c2febafcSKirill A. Shutemov unsigned long next; 939c2febafcSKirill A. Shutemov 940c2febafcSKirill A. Shutemov dst_p4d = p4d_alloc(dst_mm, dst_pgd, addr); 941c2febafcSKirill A. Shutemov if (!dst_p4d) 942c2febafcSKirill A. Shutemov return -ENOMEM; 943c2febafcSKirill A. Shutemov src_p4d = p4d_offset(src_pgd, addr); 944c2febafcSKirill A. Shutemov do { 945c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 946c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(src_p4d)) 947c2febafcSKirill A. Shutemov continue; 948c2febafcSKirill A. Shutemov if (copy_pud_range(dst_mm, src_mm, dst_p4d, src_p4d, 949c2febafcSKirill A. Shutemov vma, addr, next)) 950c2febafcSKirill A. Shutemov return -ENOMEM; 951c2febafcSKirill A. Shutemov } while (dst_p4d++, src_p4d++, addr = next, addr != end); 952c2febafcSKirill A. Shutemov return 0; 953c2febafcSKirill A. Shutemov } 954c2febafcSKirill A. Shutemov 9551da177e4SLinus Torvalds int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 9561da177e4SLinus Torvalds struct vm_area_struct *vma) 9571da177e4SLinus Torvalds { 9581da177e4SLinus Torvalds pgd_t *src_pgd, *dst_pgd; 9591da177e4SLinus Torvalds unsigned long next; 9601da177e4SLinus Torvalds unsigned long addr = vma->vm_start; 9611da177e4SLinus Torvalds unsigned long end = vma->vm_end; 962ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 9632ec74c3eSSagi Grimberg bool is_cow; 964cddb8a5cSAndrea Arcangeli int ret; 9651da177e4SLinus Torvalds 966d992895bSNick Piggin /* 967d992895bSNick Piggin * Don't copy ptes where a page fault will fill them correctly. 968d992895bSNick Piggin * Fork becomes much lighter when there are big shared or private 969d992895bSNick Piggin * readonly mappings. The tradeoff is that copy_page_range is more 970d992895bSNick Piggin * efficient than faulting. 971d992895bSNick Piggin */ 9720661a336SKirill A. Shutemov if (!(vma->vm_flags & (VM_HUGETLB | VM_PFNMAP | VM_MIXEDMAP)) && 9730661a336SKirill A. Shutemov !vma->anon_vma) 974d992895bSNick Piggin return 0; 975d992895bSNick Piggin 9761da177e4SLinus Torvalds if (is_vm_hugetlb_page(vma)) 9771da177e4SLinus Torvalds return copy_hugetlb_page_range(dst_mm, src_mm, vma); 9781da177e4SLinus Torvalds 979b3b9c293SKonstantin Khlebnikov if (unlikely(vma->vm_flags & VM_PFNMAP)) { 9802ab64037Svenkatesh.pallipadi@intel.com /* 9812ab64037Svenkatesh.pallipadi@intel.com * We do not free on error cases below as remove_vma 9822ab64037Svenkatesh.pallipadi@intel.com * gets called on error from higher level routine 9832ab64037Svenkatesh.pallipadi@intel.com */ 9845180da41SSuresh Siddha ret = track_pfn_copy(vma); 9852ab64037Svenkatesh.pallipadi@intel.com if (ret) 9862ab64037Svenkatesh.pallipadi@intel.com return ret; 9872ab64037Svenkatesh.pallipadi@intel.com } 9882ab64037Svenkatesh.pallipadi@intel.com 989cddb8a5cSAndrea Arcangeli /* 990cddb8a5cSAndrea Arcangeli * We need to invalidate the secondary MMU mappings only when 991cddb8a5cSAndrea Arcangeli * there could be a permission downgrade on the ptes of the 992cddb8a5cSAndrea Arcangeli * parent mm. And a permission downgrade will only happen if 993cddb8a5cSAndrea Arcangeli * is_cow_mapping() returns true. 994cddb8a5cSAndrea Arcangeli */ 9952ec74c3eSSagi Grimberg is_cow = is_cow_mapping(vma->vm_flags); 996ac46d4f3SJérôme Glisse 997ac46d4f3SJérôme Glisse if (is_cow) { 9987269f999SJérôme Glisse mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_PAGE, 9997269f999SJérôme Glisse 0, vma, src_mm, addr, end); 1000ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 1001ac46d4f3SJérôme Glisse } 1002cddb8a5cSAndrea Arcangeli 1003cddb8a5cSAndrea Arcangeli ret = 0; 10041da177e4SLinus Torvalds dst_pgd = pgd_offset(dst_mm, addr); 10051da177e4SLinus Torvalds src_pgd = pgd_offset(src_mm, addr); 10061da177e4SLinus Torvalds do { 10071da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 10081da177e4SLinus Torvalds if (pgd_none_or_clear_bad(src_pgd)) 10091da177e4SLinus Torvalds continue; 1010c2febafcSKirill A. Shutemov if (unlikely(copy_p4d_range(dst_mm, src_mm, dst_pgd, src_pgd, 1011cddb8a5cSAndrea Arcangeli vma, addr, next))) { 1012cddb8a5cSAndrea Arcangeli ret = -ENOMEM; 1013cddb8a5cSAndrea Arcangeli break; 1014cddb8a5cSAndrea Arcangeli } 10151da177e4SLinus Torvalds } while (dst_pgd++, src_pgd++, addr = next, addr != end); 1016cddb8a5cSAndrea Arcangeli 10172ec74c3eSSagi Grimberg if (is_cow) 1018ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 1019cddb8a5cSAndrea Arcangeli return ret; 10201da177e4SLinus Torvalds } 10211da177e4SLinus Torvalds 102251c6f666SRobin Holt static unsigned long zap_pte_range(struct mmu_gather *tlb, 1023b5810039SNick Piggin struct vm_area_struct *vma, pmd_t *pmd, 10241da177e4SLinus Torvalds unsigned long addr, unsigned long end, 102597a89413SPeter Zijlstra struct zap_details *details) 10261da177e4SLinus Torvalds { 1027b5810039SNick Piggin struct mm_struct *mm = tlb->mm; 1028d16dfc55SPeter Zijlstra int force_flush = 0; 1029d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 103097a89413SPeter Zijlstra spinlock_t *ptl; 10315f1a1907SSteven Rostedt pte_t *start_pte; 103297a89413SPeter Zijlstra pte_t *pte; 10338a5f14a2SKirill A. Shutemov swp_entry_t entry; 1034d559db08SKAMEZAWA Hiroyuki 1035ed6a7935SPeter Zijlstra tlb_change_page_size(tlb, PAGE_SIZE); 1036d16dfc55SPeter Zijlstra again: 1037e303297eSPeter Zijlstra init_rss_vec(rss); 10385f1a1907SSteven Rostedt start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl); 10395f1a1907SSteven Rostedt pte = start_pte; 10403ea27719SMel Gorman flush_tlb_batched_pending(mm); 10416606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 10421da177e4SLinus Torvalds do { 10431da177e4SLinus Torvalds pte_t ptent = *pte; 1044166f61b9STobin C Harding if (pte_none(ptent)) 10451da177e4SLinus Torvalds continue; 104651c6f666SRobin Holt 10477b167b68SMinchan Kim if (need_resched()) 10487b167b68SMinchan Kim break; 10497b167b68SMinchan Kim 10506f5e6b9eSHugh Dickins if (pte_present(ptent)) { 10516f5e6b9eSHugh Dickins struct page *page; 10526f5e6b9eSHugh Dickins 105325b2995aSChristoph Hellwig page = vm_normal_page(vma, addr, ptent); 10541da177e4SLinus Torvalds if (unlikely(details) && page) { 10551da177e4SLinus Torvalds /* 10561da177e4SLinus Torvalds * unmap_shared_mapping_pages() wants to 10571da177e4SLinus Torvalds * invalidate cache without truncating: 10581da177e4SLinus Torvalds * unmap shared but keep private pages. 10591da177e4SLinus Torvalds */ 10601da177e4SLinus Torvalds if (details->check_mapping && 1061800d8c63SKirill A. Shutemov details->check_mapping != page_rmapping(page)) 10621da177e4SLinus Torvalds continue; 10631da177e4SLinus Torvalds } 1064b5810039SNick Piggin ptent = ptep_get_and_clear_full(mm, addr, pte, 1065a600388dSZachary Amsden tlb->fullmm); 10661da177e4SLinus Torvalds tlb_remove_tlb_entry(tlb, pte, addr); 10671da177e4SLinus Torvalds if (unlikely(!page)) 10681da177e4SLinus Torvalds continue; 1069eca56ff9SJerome Marchand 1070eca56ff9SJerome Marchand if (!PageAnon(page)) { 10711cf35d47SLinus Torvalds if (pte_dirty(ptent)) { 10721cf35d47SLinus Torvalds force_flush = 1; 10736237bcd9SHugh Dickins set_page_dirty(page); 10741cf35d47SLinus Torvalds } 10754917e5d0SJohannes Weiner if (pte_young(ptent) && 107664363aadSJoe Perches likely(!(vma->vm_flags & VM_SEQ_READ))) 1077bf3f3bc5SNick Piggin mark_page_accessed(page); 10786237bcd9SHugh Dickins } 1079eca56ff9SJerome Marchand rss[mm_counter(page)]--; 1080d281ee61SKirill A. Shutemov page_remove_rmap(page, false); 10813dc14741SHugh Dickins if (unlikely(page_mapcount(page) < 0)) 10823dc14741SHugh Dickins print_bad_pte(vma, addr, ptent, page); 1083e9d55e15SAneesh Kumar K.V if (unlikely(__tlb_remove_page(tlb, page))) { 10841cf35d47SLinus Torvalds force_flush = 1; 1085ce9ec37bSWill Deacon addr += PAGE_SIZE; 1086d16dfc55SPeter Zijlstra break; 10871cf35d47SLinus Torvalds } 10881da177e4SLinus Torvalds continue; 10891da177e4SLinus Torvalds } 10905042db43SJérôme Glisse 10915042db43SJérôme Glisse entry = pte_to_swp_entry(ptent); 10925042db43SJérôme Glisse if (non_swap_entry(entry) && is_device_private_entry(entry)) { 10935042db43SJérôme Glisse struct page *page = device_private_entry_to_page(entry); 10945042db43SJérôme Glisse 10955042db43SJérôme Glisse if (unlikely(details && details->check_mapping)) { 10965042db43SJérôme Glisse /* 10975042db43SJérôme Glisse * unmap_shared_mapping_pages() wants to 10985042db43SJérôme Glisse * invalidate cache without truncating: 10995042db43SJérôme Glisse * unmap shared but keep private pages. 11005042db43SJérôme Glisse */ 11015042db43SJérôme Glisse if (details->check_mapping != 11025042db43SJérôme Glisse page_rmapping(page)) 11035042db43SJérôme Glisse continue; 11045042db43SJérôme Glisse } 11055042db43SJérôme Glisse 11065042db43SJérôme Glisse pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); 11075042db43SJérôme Glisse rss[mm_counter(page)]--; 11085042db43SJérôme Glisse page_remove_rmap(page, false); 11095042db43SJérôme Glisse put_page(page); 11105042db43SJérôme Glisse continue; 11115042db43SJérôme Glisse } 11125042db43SJérôme Glisse 11133e8715fdSKirill A. Shutemov /* If details->check_mapping, we leave swap entries. */ 11143e8715fdSKirill A. Shutemov if (unlikely(details)) 11151da177e4SLinus Torvalds continue; 1116b084d435SKAMEZAWA Hiroyuki 1117b084d435SKAMEZAWA Hiroyuki if (!non_swap_entry(entry)) 1118b084d435SKAMEZAWA Hiroyuki rss[MM_SWAPENTS]--; 11199f9f1acdSKonstantin Khlebnikov else if (is_migration_entry(entry)) { 11209f9f1acdSKonstantin Khlebnikov struct page *page; 11219f9f1acdSKonstantin Khlebnikov 11229f9f1acdSKonstantin Khlebnikov page = migration_entry_to_page(entry); 1123eca56ff9SJerome Marchand rss[mm_counter(page)]--; 11249f9f1acdSKonstantin Khlebnikov } 1125b084d435SKAMEZAWA Hiroyuki if (unlikely(!free_swap_and_cache(entry))) 11262509ef26SHugh Dickins print_bad_pte(vma, addr, ptent, NULL); 11279888a1caSZachary Amsden pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); 112897a89413SPeter Zijlstra } while (pte++, addr += PAGE_SIZE, addr != end); 1129ae859762SHugh Dickins 1130d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(mm, rss); 11316606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 113251c6f666SRobin Holt 11331cf35d47SLinus Torvalds /* Do the actual TLB flush before dropping ptl */ 1134fb7332a9SWill Deacon if (force_flush) 11351cf35d47SLinus Torvalds tlb_flush_mmu_tlbonly(tlb); 11361cf35d47SLinus Torvalds pte_unmap_unlock(start_pte, ptl); 11371cf35d47SLinus Torvalds 11381cf35d47SLinus Torvalds /* 11391cf35d47SLinus Torvalds * If we forced a TLB flush (either due to running out of 11401cf35d47SLinus Torvalds * batch buffers or because we needed to flush dirty TLB 11411cf35d47SLinus Torvalds * entries before releasing the ptl), free the batched 11421cf35d47SLinus Torvalds * memory too. Restart if we didn't do everything. 11431cf35d47SLinus Torvalds */ 11441cf35d47SLinus Torvalds if (force_flush) { 11451cf35d47SLinus Torvalds force_flush = 0; 1146fa0aafb8SPeter Zijlstra tlb_flush_mmu(tlb); 11477b167b68SMinchan Kim } 11487b167b68SMinchan Kim 11497b167b68SMinchan Kim if (addr != end) { 11507b167b68SMinchan Kim cond_resched(); 1151d16dfc55SPeter Zijlstra goto again; 1152d16dfc55SPeter Zijlstra } 1153d16dfc55SPeter Zijlstra 115451c6f666SRobin Holt return addr; 11551da177e4SLinus Torvalds } 11561da177e4SLinus Torvalds 115751c6f666SRobin Holt static inline unsigned long zap_pmd_range(struct mmu_gather *tlb, 1158b5810039SNick Piggin struct vm_area_struct *vma, pud_t *pud, 11591da177e4SLinus Torvalds unsigned long addr, unsigned long end, 116097a89413SPeter Zijlstra struct zap_details *details) 11611da177e4SLinus Torvalds { 11621da177e4SLinus Torvalds pmd_t *pmd; 11631da177e4SLinus Torvalds unsigned long next; 11641da177e4SLinus Torvalds 11651da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 11661da177e4SLinus Torvalds do { 11671da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 116884c3fc4eSZi Yan if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) { 116953406ed1SHugh Dickins if (next - addr != HPAGE_PMD_SIZE) 1170fd60775aSDavid Rientjes __split_huge_pmd(vma, pmd, addr, false, NULL); 117153406ed1SHugh Dickins else if (zap_huge_pmd(tlb, vma, pmd, addr)) 11721a5a9906SAndrea Arcangeli goto next; 117371e3aac0SAndrea Arcangeli /* fall through */ 117471e3aac0SAndrea Arcangeli } 11751a5a9906SAndrea Arcangeli /* 11761a5a9906SAndrea Arcangeli * Here there can be other concurrent MADV_DONTNEED or 11771a5a9906SAndrea Arcangeli * trans huge page faults running, and if the pmd is 11781a5a9906SAndrea Arcangeli * none or trans huge it can change under us. This is 11791a5a9906SAndrea Arcangeli * because MADV_DONTNEED holds the mmap_sem in read 11801a5a9906SAndrea Arcangeli * mode. 11811a5a9906SAndrea Arcangeli */ 11821a5a9906SAndrea Arcangeli if (pmd_none_or_trans_huge_or_clear_bad(pmd)) 11831a5a9906SAndrea Arcangeli goto next; 118497a89413SPeter Zijlstra next = zap_pte_range(tlb, vma, pmd, addr, next, details); 11851a5a9906SAndrea Arcangeli next: 118697a89413SPeter Zijlstra cond_resched(); 118797a89413SPeter Zijlstra } while (pmd++, addr = next, addr != end); 118851c6f666SRobin Holt 118951c6f666SRobin Holt return addr; 11901da177e4SLinus Torvalds } 11911da177e4SLinus Torvalds 119251c6f666SRobin Holt static inline unsigned long zap_pud_range(struct mmu_gather *tlb, 1193c2febafcSKirill A. Shutemov struct vm_area_struct *vma, p4d_t *p4d, 11941da177e4SLinus Torvalds unsigned long addr, unsigned long end, 119597a89413SPeter Zijlstra struct zap_details *details) 11961da177e4SLinus Torvalds { 11971da177e4SLinus Torvalds pud_t *pud; 11981da177e4SLinus Torvalds unsigned long next; 11991da177e4SLinus Torvalds 1200c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 12011da177e4SLinus Torvalds do { 12021da177e4SLinus Torvalds next = pud_addr_end(addr, end); 1203a00cc7d9SMatthew Wilcox if (pud_trans_huge(*pud) || pud_devmap(*pud)) { 1204a00cc7d9SMatthew Wilcox if (next - addr != HPAGE_PUD_SIZE) { 1205a00cc7d9SMatthew Wilcox VM_BUG_ON_VMA(!rwsem_is_locked(&tlb->mm->mmap_sem), vma); 1206a00cc7d9SMatthew Wilcox split_huge_pud(vma, pud, addr); 1207a00cc7d9SMatthew Wilcox } else if (zap_huge_pud(tlb, vma, pud, addr)) 1208a00cc7d9SMatthew Wilcox goto next; 1209a00cc7d9SMatthew Wilcox /* fall through */ 1210a00cc7d9SMatthew Wilcox } 121197a89413SPeter Zijlstra if (pud_none_or_clear_bad(pud)) 12121da177e4SLinus Torvalds continue; 121397a89413SPeter Zijlstra next = zap_pmd_range(tlb, vma, pud, addr, next, details); 1214a00cc7d9SMatthew Wilcox next: 1215a00cc7d9SMatthew Wilcox cond_resched(); 121697a89413SPeter Zijlstra } while (pud++, addr = next, addr != end); 121751c6f666SRobin Holt 121851c6f666SRobin Holt return addr; 12191da177e4SLinus Torvalds } 12201da177e4SLinus Torvalds 1221c2febafcSKirill A. Shutemov static inline unsigned long zap_p4d_range(struct mmu_gather *tlb, 1222c2febafcSKirill A. Shutemov struct vm_area_struct *vma, pgd_t *pgd, 1223c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 1224c2febafcSKirill A. Shutemov struct zap_details *details) 1225c2febafcSKirill A. Shutemov { 1226c2febafcSKirill A. Shutemov p4d_t *p4d; 1227c2febafcSKirill A. Shutemov unsigned long next; 1228c2febafcSKirill A. Shutemov 1229c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 1230c2febafcSKirill A. Shutemov do { 1231c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 1232c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(p4d)) 1233c2febafcSKirill A. Shutemov continue; 1234c2febafcSKirill A. Shutemov next = zap_pud_range(tlb, vma, p4d, addr, next, details); 1235c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 1236c2febafcSKirill A. Shutemov 1237c2febafcSKirill A. Shutemov return addr; 1238c2febafcSKirill A. Shutemov } 1239c2febafcSKirill A. Shutemov 1240aac45363SMichal Hocko void unmap_page_range(struct mmu_gather *tlb, 124151c6f666SRobin Holt struct vm_area_struct *vma, 12421da177e4SLinus Torvalds unsigned long addr, unsigned long end, 124397a89413SPeter Zijlstra struct zap_details *details) 12441da177e4SLinus Torvalds { 12451da177e4SLinus Torvalds pgd_t *pgd; 12461da177e4SLinus Torvalds unsigned long next; 12471da177e4SLinus Torvalds 12481da177e4SLinus Torvalds BUG_ON(addr >= end); 12491da177e4SLinus Torvalds tlb_start_vma(tlb, vma); 12501da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 12511da177e4SLinus Torvalds do { 12521da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 125397a89413SPeter Zijlstra if (pgd_none_or_clear_bad(pgd)) 12541da177e4SLinus Torvalds continue; 1255c2febafcSKirill A. Shutemov next = zap_p4d_range(tlb, vma, pgd, addr, next, details); 125697a89413SPeter Zijlstra } while (pgd++, addr = next, addr != end); 12571da177e4SLinus Torvalds tlb_end_vma(tlb, vma); 12581da177e4SLinus Torvalds } 12591da177e4SLinus Torvalds 1260f5cc4eefSAl Viro 1261f5cc4eefSAl Viro static void unmap_single_vma(struct mmu_gather *tlb, 12621da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long start_addr, 12634f74d2c8SLinus Torvalds unsigned long end_addr, 12641da177e4SLinus Torvalds struct zap_details *details) 12651da177e4SLinus Torvalds { 1266f5cc4eefSAl Viro unsigned long start = max(vma->vm_start, start_addr); 12671da177e4SLinus Torvalds unsigned long end; 12681da177e4SLinus Torvalds 12691da177e4SLinus Torvalds if (start >= vma->vm_end) 1270f5cc4eefSAl Viro return; 12711da177e4SLinus Torvalds end = min(vma->vm_end, end_addr); 12721da177e4SLinus Torvalds if (end <= vma->vm_start) 1273f5cc4eefSAl Viro return; 12741da177e4SLinus Torvalds 1275cbc91f71SSrikar Dronamraju if (vma->vm_file) 1276cbc91f71SSrikar Dronamraju uprobe_munmap(vma, start, end); 1277cbc91f71SSrikar Dronamraju 1278b3b9c293SKonstantin Khlebnikov if (unlikely(vma->vm_flags & VM_PFNMAP)) 12795180da41SSuresh Siddha untrack_pfn(vma, 0, 0); 12802ab64037Svenkatesh.pallipadi@intel.com 12818b2a1238SAl Viro if (start != end) { 128251c6f666SRobin Holt if (unlikely(is_vm_hugetlb_page(vma))) { 1283a137e1ccSAndi Kleen /* 1284a137e1ccSAndi Kleen * It is undesirable to test vma->vm_file as it 1285a137e1ccSAndi Kleen * should be non-null for valid hugetlb area. 1286a137e1ccSAndi Kleen * However, vm_file will be NULL in the error 12877aa6b4adSDavidlohr Bueso * cleanup path of mmap_region. When 1288a137e1ccSAndi Kleen * hugetlbfs ->mmap method fails, 12897aa6b4adSDavidlohr Bueso * mmap_region() nullifies vma->vm_file 1290a137e1ccSAndi Kleen * before calling this function to clean up. 1291a137e1ccSAndi Kleen * Since no pte has actually been setup, it is 1292a137e1ccSAndi Kleen * safe to do nothing in this case. 1293a137e1ccSAndi Kleen */ 129424669e58SAneesh Kumar K.V if (vma->vm_file) { 129583cde9e8SDavidlohr Bueso i_mmap_lock_write(vma->vm_file->f_mapping); 1296d833352aSMel Gorman __unmap_hugepage_range_final(tlb, vma, start, end, NULL); 129783cde9e8SDavidlohr Bueso i_mmap_unlock_write(vma->vm_file->f_mapping); 129824669e58SAneesh Kumar K.V } 129951c6f666SRobin Holt } else 1300038c7aa1SAl Viro unmap_page_range(tlb, vma, start, end, details); 130197a89413SPeter Zijlstra } 130251c6f666SRobin Holt } 13031da177e4SLinus Torvalds 1304f5cc4eefSAl Viro /** 1305f5cc4eefSAl Viro * unmap_vmas - unmap a range of memory covered by a list of vma's 1306f5cc4eefSAl Viro * @tlb: address of the caller's struct mmu_gather 1307f5cc4eefSAl Viro * @vma: the starting vma 1308f5cc4eefSAl Viro * @start_addr: virtual address at which to start unmapping 1309f5cc4eefSAl Viro * @end_addr: virtual address at which to end unmapping 1310f5cc4eefSAl Viro * 1311f5cc4eefSAl Viro * Unmap all pages in the vma list. 1312f5cc4eefSAl Viro * 1313f5cc4eefSAl Viro * Only addresses between `start' and `end' will be unmapped. 1314f5cc4eefSAl Viro * 1315f5cc4eefSAl Viro * The VMA list must be sorted in ascending virtual address order. 1316f5cc4eefSAl Viro * 1317f5cc4eefSAl Viro * unmap_vmas() assumes that the caller will flush the whole unmapped address 1318f5cc4eefSAl Viro * range after unmap_vmas() returns. So the only responsibility here is to 1319f5cc4eefSAl Viro * ensure that any thus-far unmapped pages are flushed before unmap_vmas() 1320f5cc4eefSAl Viro * drops the lock and schedules. 1321f5cc4eefSAl Viro */ 1322f5cc4eefSAl Viro void unmap_vmas(struct mmu_gather *tlb, 1323f5cc4eefSAl Viro struct vm_area_struct *vma, unsigned long start_addr, 13244f74d2c8SLinus Torvalds unsigned long end_addr) 1325f5cc4eefSAl Viro { 1326ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 1327f5cc4eefSAl Viro 13286f4f13e8SJérôme Glisse mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma, vma->vm_mm, 13296f4f13e8SJérôme Glisse start_addr, end_addr); 1330ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 1331f5cc4eefSAl Viro for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) 13324f74d2c8SLinus Torvalds unmap_single_vma(tlb, vma, start_addr, end_addr, NULL); 1333ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 13341da177e4SLinus Torvalds } 13351da177e4SLinus Torvalds 13361da177e4SLinus Torvalds /** 13371da177e4SLinus Torvalds * zap_page_range - remove user pages in a given range 13381da177e4SLinus Torvalds * @vma: vm_area_struct holding the applicable pages 1339eb4546bbSRandy Dunlap * @start: starting address of pages to zap 13401da177e4SLinus Torvalds * @size: number of bytes to zap 1341f5cc4eefSAl Viro * 1342f5cc4eefSAl Viro * Caller must protect the VMA list 13431da177e4SLinus Torvalds */ 13447e027b14SLinus Torvalds void zap_page_range(struct vm_area_struct *vma, unsigned long start, 1345ecf1385dSKirill A. Shutemov unsigned long size) 13461da177e4SLinus Torvalds { 1347ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 1348d16dfc55SPeter Zijlstra struct mmu_gather tlb; 13491da177e4SLinus Torvalds 13501da177e4SLinus Torvalds lru_add_drain(); 13517269f999SJérôme Glisse mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm, 13526f4f13e8SJérôme Glisse start, start + size); 1353ac46d4f3SJérôme Glisse tlb_gather_mmu(&tlb, vma->vm_mm, start, range.end); 1354ac46d4f3SJérôme Glisse update_hiwater_rss(vma->vm_mm); 1355ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 1356ac46d4f3SJérôme Glisse for ( ; vma && vma->vm_start < range.end; vma = vma->vm_next) 1357ac46d4f3SJérôme Glisse unmap_single_vma(&tlb, vma, start, range.end, NULL); 1358ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 1359ac46d4f3SJérôme Glisse tlb_finish_mmu(&tlb, start, range.end); 13601da177e4SLinus Torvalds } 13611da177e4SLinus Torvalds 1362c627f9ccSJack Steiner /** 1363f5cc4eefSAl Viro * zap_page_range_single - remove user pages in a given range 1364f5cc4eefSAl Viro * @vma: vm_area_struct holding the applicable pages 1365f5cc4eefSAl Viro * @address: starting address of pages to zap 1366f5cc4eefSAl Viro * @size: number of bytes to zap 13678a5f14a2SKirill A. Shutemov * @details: details of shared cache invalidation 1368f5cc4eefSAl Viro * 1369f5cc4eefSAl Viro * The range must fit into one VMA. 1370f5cc4eefSAl Viro */ 1371f5cc4eefSAl Viro static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address, 1372f5cc4eefSAl Viro unsigned long size, struct zap_details *details) 1373f5cc4eefSAl Viro { 1374ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 1375f5cc4eefSAl Viro struct mmu_gather tlb; 1376f5cc4eefSAl Viro 1377f5cc4eefSAl Viro lru_add_drain(); 13787269f999SJérôme Glisse mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm, 13796f4f13e8SJérôme Glisse address, address + size); 1380ac46d4f3SJérôme Glisse tlb_gather_mmu(&tlb, vma->vm_mm, address, range.end); 1381ac46d4f3SJérôme Glisse update_hiwater_rss(vma->vm_mm); 1382ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 1383ac46d4f3SJérôme Glisse unmap_single_vma(&tlb, vma, address, range.end, details); 1384ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 1385ac46d4f3SJérôme Glisse tlb_finish_mmu(&tlb, address, range.end); 13861da177e4SLinus Torvalds } 13871da177e4SLinus Torvalds 1388c627f9ccSJack Steiner /** 1389c627f9ccSJack Steiner * zap_vma_ptes - remove ptes mapping the vma 1390c627f9ccSJack Steiner * @vma: vm_area_struct holding ptes to be zapped 1391c627f9ccSJack Steiner * @address: starting address of pages to zap 1392c627f9ccSJack Steiner * @size: number of bytes to zap 1393c627f9ccSJack Steiner * 1394c627f9ccSJack Steiner * This function only unmaps ptes assigned to VM_PFNMAP vmas. 1395c627f9ccSJack Steiner * 1396c627f9ccSJack Steiner * The entire address range must be fully contained within the vma. 1397c627f9ccSJack Steiner * 1398c627f9ccSJack Steiner */ 139927d036e3SLeon Romanovsky void zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, 1400c627f9ccSJack Steiner unsigned long size) 1401c627f9ccSJack Steiner { 1402c627f9ccSJack Steiner if (address < vma->vm_start || address + size > vma->vm_end || 1403c627f9ccSJack Steiner !(vma->vm_flags & VM_PFNMAP)) 140427d036e3SLeon Romanovsky return; 140527d036e3SLeon Romanovsky 1406f5cc4eefSAl Viro zap_page_range_single(vma, address, size, NULL); 1407c627f9ccSJack Steiner } 1408c627f9ccSJack Steiner EXPORT_SYMBOL_GPL(zap_vma_ptes); 1409c627f9ccSJack Steiner 141025ca1d6cSNamhyung Kim pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr, 1411920c7a5dSHarvey Harrison spinlock_t **ptl) 1412c9cfcddfSLinus Torvalds { 1413c2febafcSKirill A. Shutemov pgd_t *pgd; 1414c2febafcSKirill A. Shutemov p4d_t *p4d; 1415c2febafcSKirill A. Shutemov pud_t *pud; 1416c2febafcSKirill A. Shutemov pmd_t *pmd; 1417c2febafcSKirill A. Shutemov 1418c2febafcSKirill A. Shutemov pgd = pgd_offset(mm, addr); 1419c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, addr); 1420c2febafcSKirill A. Shutemov if (!p4d) 1421c2febafcSKirill A. Shutemov return NULL; 1422c2febafcSKirill A. Shutemov pud = pud_alloc(mm, p4d, addr); 1423c2febafcSKirill A. Shutemov if (!pud) 1424c2febafcSKirill A. Shutemov return NULL; 1425c2febafcSKirill A. Shutemov pmd = pmd_alloc(mm, pud, addr); 1426c2febafcSKirill A. Shutemov if (!pmd) 1427c2febafcSKirill A. Shutemov return NULL; 1428c2febafcSKirill A. Shutemov 1429f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 1430c9cfcddfSLinus Torvalds return pte_alloc_map_lock(mm, pmd, addr, ptl); 1431c9cfcddfSLinus Torvalds } 1432c9cfcddfSLinus Torvalds 14331da177e4SLinus Torvalds /* 1434238f58d8SLinus Torvalds * This is the old fallback for page remapping. 1435238f58d8SLinus Torvalds * 1436238f58d8SLinus Torvalds * For historical reasons, it only allows reserved pages. Only 1437238f58d8SLinus Torvalds * old drivers should use this, and they needed to mark their 1438238f58d8SLinus Torvalds * pages reserved for the old functions anyway. 1439238f58d8SLinus Torvalds */ 1440423bad60SNick Piggin static int insert_page(struct vm_area_struct *vma, unsigned long addr, 1441423bad60SNick Piggin struct page *page, pgprot_t prot) 1442238f58d8SLinus Torvalds { 1443423bad60SNick Piggin struct mm_struct *mm = vma->vm_mm; 1444238f58d8SLinus Torvalds int retval; 1445238f58d8SLinus Torvalds pte_t *pte; 1446238f58d8SLinus Torvalds spinlock_t *ptl; 1447238f58d8SLinus Torvalds 1448238f58d8SLinus Torvalds retval = -EINVAL; 14490ee930e6SMatthew Wilcox if (PageAnon(page) || PageSlab(page) || page_has_type(page)) 14505b4e655eSKAMEZAWA Hiroyuki goto out; 1451238f58d8SLinus Torvalds retval = -ENOMEM; 1452238f58d8SLinus Torvalds flush_dcache_page(page); 1453c9cfcddfSLinus Torvalds pte = get_locked_pte(mm, addr, &ptl); 1454238f58d8SLinus Torvalds if (!pte) 14555b4e655eSKAMEZAWA Hiroyuki goto out; 1456238f58d8SLinus Torvalds retval = -EBUSY; 1457238f58d8SLinus Torvalds if (!pte_none(*pte)) 1458238f58d8SLinus Torvalds goto out_unlock; 1459238f58d8SLinus Torvalds 1460238f58d8SLinus Torvalds /* Ok, finally just insert the thing.. */ 1461238f58d8SLinus Torvalds get_page(page); 1462eca56ff9SJerome Marchand inc_mm_counter_fast(mm, mm_counter_file(page)); 1463dd78feddSKirill A. Shutemov page_add_file_rmap(page, false); 1464238f58d8SLinus Torvalds set_pte_at(mm, addr, pte, mk_pte(page, prot)); 1465238f58d8SLinus Torvalds 1466238f58d8SLinus Torvalds retval = 0; 1467238f58d8SLinus Torvalds out_unlock: 1468238f58d8SLinus Torvalds pte_unmap_unlock(pte, ptl); 1469238f58d8SLinus Torvalds out: 1470238f58d8SLinus Torvalds return retval; 1471238f58d8SLinus Torvalds } 1472238f58d8SLinus Torvalds 1473bfa5bf6dSRolf Eike Beer /** 1474bfa5bf6dSRolf Eike Beer * vm_insert_page - insert single page into user vma 1475bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 1476bfa5bf6dSRolf Eike Beer * @addr: target user address of this page 1477bfa5bf6dSRolf Eike Beer * @page: source kernel page 1478bfa5bf6dSRolf Eike Beer * 1479a145dd41SLinus Torvalds * This allows drivers to insert individual pages they've allocated 1480a145dd41SLinus Torvalds * into a user vma. 1481a145dd41SLinus Torvalds * 1482a145dd41SLinus Torvalds * The page has to be a nice clean _individual_ kernel allocation. 1483a145dd41SLinus Torvalds * If you allocate a compound page, you need to have marked it as 1484a145dd41SLinus Torvalds * such (__GFP_COMP), or manually just split the page up yourself 14858dfcc9baSNick Piggin * (see split_page()). 1486a145dd41SLinus Torvalds * 1487a145dd41SLinus Torvalds * NOTE! Traditionally this was done with "remap_pfn_range()" which 1488a145dd41SLinus Torvalds * took an arbitrary page protection parameter. This doesn't allow 1489a145dd41SLinus Torvalds * that. Your vma protection will have to be set up correctly, which 1490a145dd41SLinus Torvalds * means that if you want a shared writable mapping, you'd better 1491a145dd41SLinus Torvalds * ask for a shared writable mapping! 1492a145dd41SLinus Torvalds * 1493a145dd41SLinus Torvalds * The page does not need to be reserved. 14944b6e1e37SKonstantin Khlebnikov * 14954b6e1e37SKonstantin Khlebnikov * Usually this function is called from f_op->mmap() handler 14964b6e1e37SKonstantin Khlebnikov * under mm->mmap_sem write-lock, so it can change vma->vm_flags. 14974b6e1e37SKonstantin Khlebnikov * Caller must set VM_MIXEDMAP on vma if it wants to call this 14984b6e1e37SKonstantin Khlebnikov * function from other places, for example from page-fault handler. 1499a862f68aSMike Rapoport * 1500a862f68aSMike Rapoport * Return: %0 on success, negative error code otherwise. 1501a145dd41SLinus Torvalds */ 1502423bad60SNick Piggin int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, 1503423bad60SNick Piggin struct page *page) 1504a145dd41SLinus Torvalds { 1505a145dd41SLinus Torvalds if (addr < vma->vm_start || addr >= vma->vm_end) 1506a145dd41SLinus Torvalds return -EFAULT; 1507a145dd41SLinus Torvalds if (!page_count(page)) 1508a145dd41SLinus Torvalds return -EINVAL; 15094b6e1e37SKonstantin Khlebnikov if (!(vma->vm_flags & VM_MIXEDMAP)) { 15104b6e1e37SKonstantin Khlebnikov BUG_ON(down_read_trylock(&vma->vm_mm->mmap_sem)); 15114b6e1e37SKonstantin Khlebnikov BUG_ON(vma->vm_flags & VM_PFNMAP); 15124b6e1e37SKonstantin Khlebnikov vma->vm_flags |= VM_MIXEDMAP; 15134b6e1e37SKonstantin Khlebnikov } 1514423bad60SNick Piggin return insert_page(vma, addr, page, vma->vm_page_prot); 1515a145dd41SLinus Torvalds } 1516e3c3374fSLinus Torvalds EXPORT_SYMBOL(vm_insert_page); 1517a145dd41SLinus Torvalds 1518a667d745SSouptick Joarder /* 1519a667d745SSouptick Joarder * __vm_map_pages - maps range of kernel pages into user vma 1520a667d745SSouptick Joarder * @vma: user vma to map to 1521a667d745SSouptick Joarder * @pages: pointer to array of source kernel pages 1522a667d745SSouptick Joarder * @num: number of pages in page array 1523a667d745SSouptick Joarder * @offset: user's requested vm_pgoff 1524a667d745SSouptick Joarder * 1525a667d745SSouptick Joarder * This allows drivers to map range of kernel pages into a user vma. 1526a667d745SSouptick Joarder * 1527a667d745SSouptick Joarder * Return: 0 on success and error code otherwise. 1528a667d745SSouptick Joarder */ 1529a667d745SSouptick Joarder static int __vm_map_pages(struct vm_area_struct *vma, struct page **pages, 1530a667d745SSouptick Joarder unsigned long num, unsigned long offset) 1531a667d745SSouptick Joarder { 1532a667d745SSouptick Joarder unsigned long count = vma_pages(vma); 1533a667d745SSouptick Joarder unsigned long uaddr = vma->vm_start; 1534a667d745SSouptick Joarder int ret, i; 1535a667d745SSouptick Joarder 1536a667d745SSouptick Joarder /* Fail if the user requested offset is beyond the end of the object */ 153796756fcbSMiguel Ojeda if (offset >= num) 1538a667d745SSouptick Joarder return -ENXIO; 1539a667d745SSouptick Joarder 1540a667d745SSouptick Joarder /* Fail if the user requested size exceeds available object size */ 1541a667d745SSouptick Joarder if (count > num - offset) 1542a667d745SSouptick Joarder return -ENXIO; 1543a667d745SSouptick Joarder 1544a667d745SSouptick Joarder for (i = 0; i < count; i++) { 1545a667d745SSouptick Joarder ret = vm_insert_page(vma, uaddr, pages[offset + i]); 1546a667d745SSouptick Joarder if (ret < 0) 1547a667d745SSouptick Joarder return ret; 1548a667d745SSouptick Joarder uaddr += PAGE_SIZE; 1549a667d745SSouptick Joarder } 1550a667d745SSouptick Joarder 1551a667d745SSouptick Joarder return 0; 1552a667d745SSouptick Joarder } 1553a667d745SSouptick Joarder 1554a667d745SSouptick Joarder /** 1555a667d745SSouptick Joarder * vm_map_pages - maps range of kernel pages starts with non zero offset 1556a667d745SSouptick Joarder * @vma: user vma to map to 1557a667d745SSouptick Joarder * @pages: pointer to array of source kernel pages 1558a667d745SSouptick Joarder * @num: number of pages in page array 1559a667d745SSouptick Joarder * 1560a667d745SSouptick Joarder * Maps an object consisting of @num pages, catering for the user's 1561a667d745SSouptick Joarder * requested vm_pgoff 1562a667d745SSouptick Joarder * 1563a667d745SSouptick Joarder * If we fail to insert any page into the vma, the function will return 1564a667d745SSouptick Joarder * immediately leaving any previously inserted pages present. Callers 1565a667d745SSouptick Joarder * from the mmap handler may immediately return the error as their caller 1566a667d745SSouptick Joarder * will destroy the vma, removing any successfully inserted pages. Other 1567a667d745SSouptick Joarder * callers should make their own arrangements for calling unmap_region(). 1568a667d745SSouptick Joarder * 1569a667d745SSouptick Joarder * Context: Process context. Called by mmap handlers. 1570a667d745SSouptick Joarder * Return: 0 on success and error code otherwise. 1571a667d745SSouptick Joarder */ 1572a667d745SSouptick Joarder int vm_map_pages(struct vm_area_struct *vma, struct page **pages, 1573a667d745SSouptick Joarder unsigned long num) 1574a667d745SSouptick Joarder { 1575a667d745SSouptick Joarder return __vm_map_pages(vma, pages, num, vma->vm_pgoff); 1576a667d745SSouptick Joarder } 1577a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages); 1578a667d745SSouptick Joarder 1579a667d745SSouptick Joarder /** 1580a667d745SSouptick Joarder * vm_map_pages_zero - map range of kernel pages starts with zero offset 1581a667d745SSouptick Joarder * @vma: user vma to map to 1582a667d745SSouptick Joarder * @pages: pointer to array of source kernel pages 1583a667d745SSouptick Joarder * @num: number of pages in page array 1584a667d745SSouptick Joarder * 1585a667d745SSouptick Joarder * Similar to vm_map_pages(), except that it explicitly sets the offset 1586a667d745SSouptick Joarder * to 0. This function is intended for the drivers that did not consider 1587a667d745SSouptick Joarder * vm_pgoff. 1588a667d745SSouptick Joarder * 1589a667d745SSouptick Joarder * Context: Process context. Called by mmap handlers. 1590a667d745SSouptick Joarder * Return: 0 on success and error code otherwise. 1591a667d745SSouptick Joarder */ 1592a667d745SSouptick Joarder int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages, 1593a667d745SSouptick Joarder unsigned long num) 1594a667d745SSouptick Joarder { 1595a667d745SSouptick Joarder return __vm_map_pages(vma, pages, num, 0); 1596a667d745SSouptick Joarder } 1597a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages_zero); 1598a667d745SSouptick Joarder 15999b5a8e00SMatthew Wilcox static vm_fault_t insert_pfn(struct vm_area_struct *vma, unsigned long addr, 1600b2770da6SRoss Zwisler pfn_t pfn, pgprot_t prot, bool mkwrite) 1601423bad60SNick Piggin { 1602423bad60SNick Piggin struct mm_struct *mm = vma->vm_mm; 1603423bad60SNick Piggin pte_t *pte, entry; 1604423bad60SNick Piggin spinlock_t *ptl; 1605423bad60SNick Piggin 1606423bad60SNick Piggin pte = get_locked_pte(mm, addr, &ptl); 1607423bad60SNick Piggin if (!pte) 16089b5a8e00SMatthew Wilcox return VM_FAULT_OOM; 1609b2770da6SRoss Zwisler if (!pte_none(*pte)) { 1610b2770da6SRoss Zwisler if (mkwrite) { 1611b2770da6SRoss Zwisler /* 1612b2770da6SRoss Zwisler * For read faults on private mappings the PFN passed 1613b2770da6SRoss Zwisler * in may not match the PFN we have mapped if the 1614b2770da6SRoss Zwisler * mapped PFN is a writeable COW page. In the mkwrite 1615b2770da6SRoss Zwisler * case we are creating a writable PTE for a shared 1616f2c57d91SJan Kara * mapping and we expect the PFNs to match. If they 1617f2c57d91SJan Kara * don't match, we are likely racing with block 1618f2c57d91SJan Kara * allocation and mapping invalidation so just skip the 1619f2c57d91SJan Kara * update. 1620b2770da6SRoss Zwisler */ 1621f2c57d91SJan Kara if (pte_pfn(*pte) != pfn_t_to_pfn(pfn)) { 1622f2c57d91SJan Kara WARN_ON_ONCE(!is_zero_pfn(pte_pfn(*pte))); 1623423bad60SNick Piggin goto out_unlock; 1624f2c57d91SJan Kara } 1625cae85cb8SJan Kara entry = pte_mkyoung(*pte); 1626cae85cb8SJan Kara entry = maybe_mkwrite(pte_mkdirty(entry), vma); 1627cae85cb8SJan Kara if (ptep_set_access_flags(vma, addr, pte, entry, 1)) 1628cae85cb8SJan Kara update_mmu_cache(vma, addr, pte); 1629cae85cb8SJan Kara } 1630b2770da6SRoss Zwisler goto out_unlock; 1631b2770da6SRoss Zwisler } 1632423bad60SNick Piggin 1633423bad60SNick Piggin /* Ok, finally just insert the thing.. */ 163401c8f1c4SDan Williams if (pfn_t_devmap(pfn)) 163501c8f1c4SDan Williams entry = pte_mkdevmap(pfn_t_pte(pfn, prot)); 163601c8f1c4SDan Williams else 163701c8f1c4SDan Williams entry = pte_mkspecial(pfn_t_pte(pfn, prot)); 1638b2770da6SRoss Zwisler 1639b2770da6SRoss Zwisler if (mkwrite) { 1640b2770da6SRoss Zwisler entry = pte_mkyoung(entry); 1641b2770da6SRoss Zwisler entry = maybe_mkwrite(pte_mkdirty(entry), vma); 1642b2770da6SRoss Zwisler } 1643b2770da6SRoss Zwisler 1644423bad60SNick Piggin set_pte_at(mm, addr, pte, entry); 16454b3073e1SRussell King update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */ 1646423bad60SNick Piggin 1647423bad60SNick Piggin out_unlock: 1648423bad60SNick Piggin pte_unmap_unlock(pte, ptl); 16499b5a8e00SMatthew Wilcox return VM_FAULT_NOPAGE; 1650423bad60SNick Piggin } 1651423bad60SNick Piggin 1652f5e6d1d5SMatthew Wilcox /** 1653f5e6d1d5SMatthew Wilcox * vmf_insert_pfn_prot - insert single pfn into user vma with specified pgprot 1654f5e6d1d5SMatthew Wilcox * @vma: user vma to map to 1655f5e6d1d5SMatthew Wilcox * @addr: target user address of this page 1656f5e6d1d5SMatthew Wilcox * @pfn: source kernel pfn 1657f5e6d1d5SMatthew Wilcox * @pgprot: pgprot flags for the inserted page 1658f5e6d1d5SMatthew Wilcox * 1659f5e6d1d5SMatthew Wilcox * This is exactly like vmf_insert_pfn(), except that it allows drivers to 1660f5e6d1d5SMatthew Wilcox * to override pgprot on a per-page basis. 1661f5e6d1d5SMatthew Wilcox * 1662f5e6d1d5SMatthew Wilcox * This only makes sense for IO mappings, and it makes no sense for 1663f5e6d1d5SMatthew Wilcox * COW mappings. In general, using multiple vmas is preferable; 1664ae2b01f3SMatthew Wilcox * vmf_insert_pfn_prot should only be used if using multiple VMAs is 1665f5e6d1d5SMatthew Wilcox * impractical. 1666f5e6d1d5SMatthew Wilcox * 1667ae2b01f3SMatthew Wilcox * Context: Process context. May allocate using %GFP_KERNEL. 1668f5e6d1d5SMatthew Wilcox * Return: vm_fault_t value. 1669f5e6d1d5SMatthew Wilcox */ 1670f5e6d1d5SMatthew Wilcox vm_fault_t vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr, 1671f5e6d1d5SMatthew Wilcox unsigned long pfn, pgprot_t pgprot) 1672f5e6d1d5SMatthew Wilcox { 16736d958546SMatthew Wilcox /* 16746d958546SMatthew Wilcox * Technically, architectures with pte_special can avoid all these 16756d958546SMatthew Wilcox * restrictions (same for remap_pfn_range). However we would like 16766d958546SMatthew Wilcox * consistency in testing and feature parity among all, so we should 16776d958546SMatthew Wilcox * try to keep these invariants in place for everybody. 16786d958546SMatthew Wilcox */ 16796d958546SMatthew Wilcox BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))); 16806d958546SMatthew Wilcox BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) == 16816d958546SMatthew Wilcox (VM_PFNMAP|VM_MIXEDMAP)); 16826d958546SMatthew Wilcox BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); 16836d958546SMatthew Wilcox BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn)); 16846d958546SMatthew Wilcox 16856d958546SMatthew Wilcox if (addr < vma->vm_start || addr >= vma->vm_end) 16866d958546SMatthew Wilcox return VM_FAULT_SIGBUS; 16876d958546SMatthew Wilcox 16886d958546SMatthew Wilcox if (!pfn_modify_allowed(pfn, pgprot)) 16896d958546SMatthew Wilcox return VM_FAULT_SIGBUS; 16906d958546SMatthew Wilcox 16916d958546SMatthew Wilcox track_pfn_insert(vma, &pgprot, __pfn_to_pfn_t(pfn, PFN_DEV)); 16926d958546SMatthew Wilcox 16939b5a8e00SMatthew Wilcox return insert_pfn(vma, addr, __pfn_to_pfn_t(pfn, PFN_DEV), pgprot, 16946d958546SMatthew Wilcox false); 1695f5e6d1d5SMatthew Wilcox } 1696f5e6d1d5SMatthew Wilcox EXPORT_SYMBOL(vmf_insert_pfn_prot); 1697e0dc0d8fSNick Piggin 1698ae2b01f3SMatthew Wilcox /** 1699ae2b01f3SMatthew Wilcox * vmf_insert_pfn - insert single pfn into user vma 1700ae2b01f3SMatthew Wilcox * @vma: user vma to map to 1701ae2b01f3SMatthew Wilcox * @addr: target user address of this page 1702ae2b01f3SMatthew Wilcox * @pfn: source kernel pfn 1703ae2b01f3SMatthew Wilcox * 1704ae2b01f3SMatthew Wilcox * Similar to vm_insert_page, this allows drivers to insert individual pages 1705ae2b01f3SMatthew Wilcox * they've allocated into a user vma. Same comments apply. 1706ae2b01f3SMatthew Wilcox * 1707ae2b01f3SMatthew Wilcox * This function should only be called from a vm_ops->fault handler, and 1708ae2b01f3SMatthew Wilcox * in that case the handler should return the result of this function. 1709ae2b01f3SMatthew Wilcox * 1710ae2b01f3SMatthew Wilcox * vma cannot be a COW mapping. 1711ae2b01f3SMatthew Wilcox * 1712ae2b01f3SMatthew Wilcox * As this is called only for pages that do not currently exist, we 1713ae2b01f3SMatthew Wilcox * do not need to flush old virtual caches or the TLB. 1714ae2b01f3SMatthew Wilcox * 1715ae2b01f3SMatthew Wilcox * Context: Process context. May allocate using %GFP_KERNEL. 1716ae2b01f3SMatthew Wilcox * Return: vm_fault_t value. 1717ae2b01f3SMatthew Wilcox */ 1718ae2b01f3SMatthew Wilcox vm_fault_t vmf_insert_pfn(struct vm_area_struct *vma, unsigned long addr, 1719ae2b01f3SMatthew Wilcox unsigned long pfn) 1720ae2b01f3SMatthew Wilcox { 1721ae2b01f3SMatthew Wilcox return vmf_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot); 1722ae2b01f3SMatthew Wilcox } 1723ae2b01f3SMatthew Wilcox EXPORT_SYMBOL(vmf_insert_pfn); 1724ae2b01f3SMatthew Wilcox 1725785a3fabSDan Williams static bool vm_mixed_ok(struct vm_area_struct *vma, pfn_t pfn) 1726785a3fabSDan Williams { 1727785a3fabSDan Williams /* these checks mirror the abort conditions in vm_normal_page */ 1728785a3fabSDan Williams if (vma->vm_flags & VM_MIXEDMAP) 1729785a3fabSDan Williams return true; 1730785a3fabSDan Williams if (pfn_t_devmap(pfn)) 1731785a3fabSDan Williams return true; 1732785a3fabSDan Williams if (pfn_t_special(pfn)) 1733785a3fabSDan Williams return true; 1734785a3fabSDan Williams if (is_zero_pfn(pfn_t_to_pfn(pfn))) 1735785a3fabSDan Williams return true; 1736785a3fabSDan Williams return false; 1737785a3fabSDan Williams } 1738785a3fabSDan Williams 173979f3aa5bSMatthew Wilcox static vm_fault_t __vm_insert_mixed(struct vm_area_struct *vma, 174079f3aa5bSMatthew Wilcox unsigned long addr, pfn_t pfn, bool mkwrite) 1741423bad60SNick Piggin { 174287744ab3SDan Williams pgprot_t pgprot = vma->vm_page_prot; 174379f3aa5bSMatthew Wilcox int err; 174487744ab3SDan Williams 1745785a3fabSDan Williams BUG_ON(!vm_mixed_ok(vma, pfn)); 1746423bad60SNick Piggin 1747423bad60SNick Piggin if (addr < vma->vm_start || addr >= vma->vm_end) 174879f3aa5bSMatthew Wilcox return VM_FAULT_SIGBUS; 1749308a047cSBorislav Petkov 1750308a047cSBorislav Petkov track_pfn_insert(vma, &pgprot, pfn); 1751423bad60SNick Piggin 175242e4089cSAndi Kleen if (!pfn_modify_allowed(pfn_t_to_pfn(pfn), pgprot)) 175379f3aa5bSMatthew Wilcox return VM_FAULT_SIGBUS; 175442e4089cSAndi Kleen 1755423bad60SNick Piggin /* 1756423bad60SNick Piggin * If we don't have pte special, then we have to use the pfn_valid() 1757423bad60SNick Piggin * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must* 1758423bad60SNick Piggin * refcount the page if pfn_valid is true (hence insert_page rather 175962eede62SHugh Dickins * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP 176062eede62SHugh Dickins * without pte special, it would there be refcounted as a normal page. 1761423bad60SNick Piggin */ 176200b3a331SLaurent Dufour if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) && 176300b3a331SLaurent Dufour !pfn_t_devmap(pfn) && pfn_t_valid(pfn)) { 1764423bad60SNick Piggin struct page *page; 1765423bad60SNick Piggin 176603fc2da6SDan Williams /* 176703fc2da6SDan Williams * At this point we are committed to insert_page() 176803fc2da6SDan Williams * regardless of whether the caller specified flags that 176903fc2da6SDan Williams * result in pfn_t_has_page() == false. 177003fc2da6SDan Williams */ 177103fc2da6SDan Williams page = pfn_to_page(pfn_t_to_pfn(pfn)); 177279f3aa5bSMatthew Wilcox err = insert_page(vma, addr, page, pgprot); 177379f3aa5bSMatthew Wilcox } else { 17749b5a8e00SMatthew Wilcox return insert_pfn(vma, addr, pfn, pgprot, mkwrite); 1775423bad60SNick Piggin } 1776b2770da6SRoss Zwisler 17775d747637SMatthew Wilcox if (err == -ENOMEM) 17785d747637SMatthew Wilcox return VM_FAULT_OOM; 17795d747637SMatthew Wilcox if (err < 0 && err != -EBUSY) 17805d747637SMatthew Wilcox return VM_FAULT_SIGBUS; 17815d747637SMatthew Wilcox 17825d747637SMatthew Wilcox return VM_FAULT_NOPAGE; 1783423bad60SNick Piggin } 178479f3aa5bSMatthew Wilcox 178579f3aa5bSMatthew Wilcox vm_fault_t vmf_insert_mixed(struct vm_area_struct *vma, unsigned long addr, 178679f3aa5bSMatthew Wilcox pfn_t pfn) 178779f3aa5bSMatthew Wilcox { 178879f3aa5bSMatthew Wilcox return __vm_insert_mixed(vma, addr, pfn, false); 178979f3aa5bSMatthew Wilcox } 17905d747637SMatthew Wilcox EXPORT_SYMBOL(vmf_insert_mixed); 1791423bad60SNick Piggin 1792ab77dab4SSouptick Joarder /* 1793ab77dab4SSouptick Joarder * If the insertion of PTE failed because someone else already added a 1794ab77dab4SSouptick Joarder * different entry in the mean time, we treat that as success as we assume 1795ab77dab4SSouptick Joarder * the same entry was actually inserted. 1796ab77dab4SSouptick Joarder */ 1797ab77dab4SSouptick Joarder vm_fault_t vmf_insert_mixed_mkwrite(struct vm_area_struct *vma, 1798ab77dab4SSouptick Joarder unsigned long addr, pfn_t pfn) 1799b2770da6SRoss Zwisler { 180079f3aa5bSMatthew Wilcox return __vm_insert_mixed(vma, addr, pfn, true); 1801b2770da6SRoss Zwisler } 1802ab77dab4SSouptick Joarder EXPORT_SYMBOL(vmf_insert_mixed_mkwrite); 1803b2770da6SRoss Zwisler 1804a145dd41SLinus Torvalds /* 18051da177e4SLinus Torvalds * maps a range of physical memory into the requested pages. the old 18061da177e4SLinus Torvalds * mappings are removed. any references to nonexistent pages results 18071da177e4SLinus Torvalds * in null mappings (currently treated as "copy-on-access") 18081da177e4SLinus Torvalds */ 18091da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd, 18101da177e4SLinus Torvalds unsigned long addr, unsigned long end, 18111da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 18121da177e4SLinus Torvalds { 18131da177e4SLinus Torvalds pte_t *pte; 1814c74df32cSHugh Dickins spinlock_t *ptl; 181542e4089cSAndi Kleen int err = 0; 18161da177e4SLinus Torvalds 1817c74df32cSHugh Dickins pte = pte_alloc_map_lock(mm, pmd, addr, &ptl); 18181da177e4SLinus Torvalds if (!pte) 18191da177e4SLinus Torvalds return -ENOMEM; 18206606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 18211da177e4SLinus Torvalds do { 18221da177e4SLinus Torvalds BUG_ON(!pte_none(*pte)); 182342e4089cSAndi Kleen if (!pfn_modify_allowed(pfn, prot)) { 182442e4089cSAndi Kleen err = -EACCES; 182542e4089cSAndi Kleen break; 182642e4089cSAndi Kleen } 18277e675137SNick Piggin set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot))); 18281da177e4SLinus Torvalds pfn++; 18291da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 18306606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 1831c74df32cSHugh Dickins pte_unmap_unlock(pte - 1, ptl); 183242e4089cSAndi Kleen return err; 18331da177e4SLinus Torvalds } 18341da177e4SLinus Torvalds 18351da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud, 18361da177e4SLinus Torvalds unsigned long addr, unsigned long end, 18371da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 18381da177e4SLinus Torvalds { 18391da177e4SLinus Torvalds pmd_t *pmd; 18401da177e4SLinus Torvalds unsigned long next; 184142e4089cSAndi Kleen int err; 18421da177e4SLinus Torvalds 18431da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 18441da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, addr); 18451da177e4SLinus Torvalds if (!pmd) 18461da177e4SLinus Torvalds return -ENOMEM; 1847f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 18481da177e4SLinus Torvalds do { 18491da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 185042e4089cSAndi Kleen err = remap_pte_range(mm, pmd, addr, next, 185142e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 185242e4089cSAndi Kleen if (err) 185342e4089cSAndi Kleen return err; 18541da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 18551da177e4SLinus Torvalds return 0; 18561da177e4SLinus Torvalds } 18571da177e4SLinus Torvalds 1858c2febafcSKirill A. Shutemov static inline int remap_pud_range(struct mm_struct *mm, p4d_t *p4d, 18591da177e4SLinus Torvalds unsigned long addr, unsigned long end, 18601da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 18611da177e4SLinus Torvalds { 18621da177e4SLinus Torvalds pud_t *pud; 18631da177e4SLinus Torvalds unsigned long next; 186442e4089cSAndi Kleen int err; 18651da177e4SLinus Torvalds 18661da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 1867c2febafcSKirill A. Shutemov pud = pud_alloc(mm, p4d, addr); 18681da177e4SLinus Torvalds if (!pud) 18691da177e4SLinus Torvalds return -ENOMEM; 18701da177e4SLinus Torvalds do { 18711da177e4SLinus Torvalds next = pud_addr_end(addr, end); 187242e4089cSAndi Kleen err = remap_pmd_range(mm, pud, addr, next, 187342e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 187442e4089cSAndi Kleen if (err) 187542e4089cSAndi Kleen return err; 18761da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 18771da177e4SLinus Torvalds return 0; 18781da177e4SLinus Torvalds } 18791da177e4SLinus Torvalds 1880c2febafcSKirill A. Shutemov static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd, 1881c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 1882c2febafcSKirill A. Shutemov unsigned long pfn, pgprot_t prot) 1883c2febafcSKirill A. Shutemov { 1884c2febafcSKirill A. Shutemov p4d_t *p4d; 1885c2febafcSKirill A. Shutemov unsigned long next; 188642e4089cSAndi Kleen int err; 1887c2febafcSKirill A. Shutemov 1888c2febafcSKirill A. Shutemov pfn -= addr >> PAGE_SHIFT; 1889c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, addr); 1890c2febafcSKirill A. Shutemov if (!p4d) 1891c2febafcSKirill A. Shutemov return -ENOMEM; 1892c2febafcSKirill A. Shutemov do { 1893c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 189442e4089cSAndi Kleen err = remap_pud_range(mm, p4d, addr, next, 189542e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 189642e4089cSAndi Kleen if (err) 189742e4089cSAndi Kleen return err; 1898c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 1899c2febafcSKirill A. Shutemov return 0; 1900c2febafcSKirill A. Shutemov } 1901c2febafcSKirill A. Shutemov 1902bfa5bf6dSRolf Eike Beer /** 1903bfa5bf6dSRolf Eike Beer * remap_pfn_range - remap kernel memory to userspace 1904bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 1905bfa5bf6dSRolf Eike Beer * @addr: target user address to start at 1906bfa5bf6dSRolf Eike Beer * @pfn: physical address of kernel memory 1907bfa5bf6dSRolf Eike Beer * @size: size of map area 1908bfa5bf6dSRolf Eike Beer * @prot: page protection flags for this mapping 1909bfa5bf6dSRolf Eike Beer * 1910bfa5bf6dSRolf Eike Beer * Note: this is only safe if the mm semaphore is held when called. 1911a862f68aSMike Rapoport * 1912a862f68aSMike Rapoport * Return: %0 on success, negative error code otherwise. 1913bfa5bf6dSRolf Eike Beer */ 19141da177e4SLinus Torvalds int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 19151da177e4SLinus Torvalds unsigned long pfn, unsigned long size, pgprot_t prot) 19161da177e4SLinus Torvalds { 19171da177e4SLinus Torvalds pgd_t *pgd; 19181da177e4SLinus Torvalds unsigned long next; 19192d15cab8SHugh Dickins unsigned long end = addr + PAGE_ALIGN(size); 19201da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 1921d5957d2fSYongji Xie unsigned long remap_pfn = pfn; 19221da177e4SLinus Torvalds int err; 19231da177e4SLinus Torvalds 19241da177e4SLinus Torvalds /* 19251da177e4SLinus Torvalds * Physically remapped pages are special. Tell the 19261da177e4SLinus Torvalds * rest of the world about it: 19271da177e4SLinus Torvalds * VM_IO tells people not to look at these pages 19281da177e4SLinus Torvalds * (accesses can have side effects). 19296aab341eSLinus Torvalds * VM_PFNMAP tells the core MM that the base pages are just 19306aab341eSLinus Torvalds * raw PFN mappings, and do not have a "struct page" associated 19316aab341eSLinus Torvalds * with them. 1932314e51b9SKonstantin Khlebnikov * VM_DONTEXPAND 1933314e51b9SKonstantin Khlebnikov * Disable vma merging and expanding with mremap(). 1934314e51b9SKonstantin Khlebnikov * VM_DONTDUMP 1935314e51b9SKonstantin Khlebnikov * Omit vma from core dump, even when VM_IO turned off. 1936fb155c16SLinus Torvalds * 1937fb155c16SLinus Torvalds * There's a horrible special case to handle copy-on-write 1938fb155c16SLinus Torvalds * behaviour that some programs depend on. We mark the "original" 1939fb155c16SLinus Torvalds * un-COW'ed pages by matching them up with "vma->vm_pgoff". 1940b3b9c293SKonstantin Khlebnikov * See vm_normal_page() for details. 19411da177e4SLinus Torvalds */ 1942b3b9c293SKonstantin Khlebnikov if (is_cow_mapping(vma->vm_flags)) { 1943b3b9c293SKonstantin Khlebnikov if (addr != vma->vm_start || end != vma->vm_end) 1944b3b9c293SKonstantin Khlebnikov return -EINVAL; 19456aab341eSLinus Torvalds vma->vm_pgoff = pfn; 1946b3b9c293SKonstantin Khlebnikov } 1947b3b9c293SKonstantin Khlebnikov 1948d5957d2fSYongji Xie err = track_pfn_remap(vma, &prot, remap_pfn, addr, PAGE_ALIGN(size)); 1949b3b9c293SKonstantin Khlebnikov if (err) 19503c8bb73aSvenkatesh.pallipadi@intel.com return -EINVAL; 1951fb155c16SLinus Torvalds 1952314e51b9SKonstantin Khlebnikov vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; 19531da177e4SLinus Torvalds 19541da177e4SLinus Torvalds BUG_ON(addr >= end); 19551da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 19561da177e4SLinus Torvalds pgd = pgd_offset(mm, addr); 19571da177e4SLinus Torvalds flush_cache_range(vma, addr, end); 19581da177e4SLinus Torvalds do { 19591da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 1960c2febafcSKirill A. Shutemov err = remap_p4d_range(mm, pgd, addr, next, 19611da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot); 19621da177e4SLinus Torvalds if (err) 19631da177e4SLinus Torvalds break; 19641da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 19652ab64037Svenkatesh.pallipadi@intel.com 19662ab64037Svenkatesh.pallipadi@intel.com if (err) 1967d5957d2fSYongji Xie untrack_pfn(vma, remap_pfn, PAGE_ALIGN(size)); 19682ab64037Svenkatesh.pallipadi@intel.com 19691da177e4SLinus Torvalds return err; 19701da177e4SLinus Torvalds } 19711da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range); 19721da177e4SLinus Torvalds 1973b4cbb197SLinus Torvalds /** 1974b4cbb197SLinus Torvalds * vm_iomap_memory - remap memory to userspace 1975b4cbb197SLinus Torvalds * @vma: user vma to map to 1976b4cbb197SLinus Torvalds * @start: start of area 1977b4cbb197SLinus Torvalds * @len: size of area 1978b4cbb197SLinus Torvalds * 1979b4cbb197SLinus Torvalds * This is a simplified io_remap_pfn_range() for common driver use. The 1980b4cbb197SLinus Torvalds * driver just needs to give us the physical memory range to be mapped, 1981b4cbb197SLinus Torvalds * we'll figure out the rest from the vma information. 1982b4cbb197SLinus Torvalds * 1983b4cbb197SLinus Torvalds * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get 1984b4cbb197SLinus Torvalds * whatever write-combining details or similar. 1985a862f68aSMike Rapoport * 1986a862f68aSMike Rapoport * Return: %0 on success, negative error code otherwise. 1987b4cbb197SLinus Torvalds */ 1988b4cbb197SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len) 1989b4cbb197SLinus Torvalds { 1990b4cbb197SLinus Torvalds unsigned long vm_len, pfn, pages; 1991b4cbb197SLinus Torvalds 1992b4cbb197SLinus Torvalds /* Check that the physical memory area passed in looks valid */ 1993b4cbb197SLinus Torvalds if (start + len < start) 1994b4cbb197SLinus Torvalds return -EINVAL; 1995b4cbb197SLinus Torvalds /* 1996b4cbb197SLinus Torvalds * You *really* shouldn't map things that aren't page-aligned, 1997b4cbb197SLinus Torvalds * but we've historically allowed it because IO memory might 1998b4cbb197SLinus Torvalds * just have smaller alignment. 1999b4cbb197SLinus Torvalds */ 2000b4cbb197SLinus Torvalds len += start & ~PAGE_MASK; 2001b4cbb197SLinus Torvalds pfn = start >> PAGE_SHIFT; 2002b4cbb197SLinus Torvalds pages = (len + ~PAGE_MASK) >> PAGE_SHIFT; 2003b4cbb197SLinus Torvalds if (pfn + pages < pfn) 2004b4cbb197SLinus Torvalds return -EINVAL; 2005b4cbb197SLinus Torvalds 2006b4cbb197SLinus Torvalds /* We start the mapping 'vm_pgoff' pages into the area */ 2007b4cbb197SLinus Torvalds if (vma->vm_pgoff > pages) 2008b4cbb197SLinus Torvalds return -EINVAL; 2009b4cbb197SLinus Torvalds pfn += vma->vm_pgoff; 2010b4cbb197SLinus Torvalds pages -= vma->vm_pgoff; 2011b4cbb197SLinus Torvalds 2012b4cbb197SLinus Torvalds /* Can we fit all of the mapping? */ 2013b4cbb197SLinus Torvalds vm_len = vma->vm_end - vma->vm_start; 2014b4cbb197SLinus Torvalds if (vm_len >> PAGE_SHIFT > pages) 2015b4cbb197SLinus Torvalds return -EINVAL; 2016b4cbb197SLinus Torvalds 2017b4cbb197SLinus Torvalds /* Ok, let it rip */ 2018b4cbb197SLinus Torvalds return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot); 2019b4cbb197SLinus Torvalds } 2020b4cbb197SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory); 2021b4cbb197SLinus Torvalds 2022aee16b3cSJeremy Fitzhardinge static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd, 2023aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2024aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 2025aee16b3cSJeremy Fitzhardinge { 2026aee16b3cSJeremy Fitzhardinge pte_t *pte; 2027aee16b3cSJeremy Fitzhardinge int err; 202894909914SBorislav Petkov spinlock_t *uninitialized_var(ptl); 2029aee16b3cSJeremy Fitzhardinge 2030aee16b3cSJeremy Fitzhardinge pte = (mm == &init_mm) ? 2031aee16b3cSJeremy Fitzhardinge pte_alloc_kernel(pmd, addr) : 2032aee16b3cSJeremy Fitzhardinge pte_alloc_map_lock(mm, pmd, addr, &ptl); 2033aee16b3cSJeremy Fitzhardinge if (!pte) 2034aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2035aee16b3cSJeremy Fitzhardinge 2036aee16b3cSJeremy Fitzhardinge BUG_ON(pmd_huge(*pmd)); 2037aee16b3cSJeremy Fitzhardinge 203838e0edb1SJeremy Fitzhardinge arch_enter_lazy_mmu_mode(); 203938e0edb1SJeremy Fitzhardinge 2040aee16b3cSJeremy Fitzhardinge do { 20418b1e0f81SAnshuman Khandual err = fn(pte++, addr, data); 2042aee16b3cSJeremy Fitzhardinge if (err) 2043aee16b3cSJeremy Fitzhardinge break; 2044c36987e2SDaisuke Nishimura } while (addr += PAGE_SIZE, addr != end); 2045aee16b3cSJeremy Fitzhardinge 204638e0edb1SJeremy Fitzhardinge arch_leave_lazy_mmu_mode(); 204738e0edb1SJeremy Fitzhardinge 2048aee16b3cSJeremy Fitzhardinge if (mm != &init_mm) 2049aee16b3cSJeremy Fitzhardinge pte_unmap_unlock(pte-1, ptl); 2050aee16b3cSJeremy Fitzhardinge return err; 2051aee16b3cSJeremy Fitzhardinge } 2052aee16b3cSJeremy Fitzhardinge 2053aee16b3cSJeremy Fitzhardinge static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud, 2054aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2055aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 2056aee16b3cSJeremy Fitzhardinge { 2057aee16b3cSJeremy Fitzhardinge pmd_t *pmd; 2058aee16b3cSJeremy Fitzhardinge unsigned long next; 2059aee16b3cSJeremy Fitzhardinge int err; 2060aee16b3cSJeremy Fitzhardinge 2061ceb86879SAndi Kleen BUG_ON(pud_huge(*pud)); 2062ceb86879SAndi Kleen 2063aee16b3cSJeremy Fitzhardinge pmd = pmd_alloc(mm, pud, addr); 2064aee16b3cSJeremy Fitzhardinge if (!pmd) 2065aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2066aee16b3cSJeremy Fitzhardinge do { 2067aee16b3cSJeremy Fitzhardinge next = pmd_addr_end(addr, end); 2068aee16b3cSJeremy Fitzhardinge err = apply_to_pte_range(mm, pmd, addr, next, fn, data); 2069aee16b3cSJeremy Fitzhardinge if (err) 2070aee16b3cSJeremy Fitzhardinge break; 2071aee16b3cSJeremy Fitzhardinge } while (pmd++, addr = next, addr != end); 2072aee16b3cSJeremy Fitzhardinge return err; 2073aee16b3cSJeremy Fitzhardinge } 2074aee16b3cSJeremy Fitzhardinge 2075c2febafcSKirill A. Shutemov static int apply_to_pud_range(struct mm_struct *mm, p4d_t *p4d, 2076aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2077aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 2078aee16b3cSJeremy Fitzhardinge { 2079aee16b3cSJeremy Fitzhardinge pud_t *pud; 2080aee16b3cSJeremy Fitzhardinge unsigned long next; 2081aee16b3cSJeremy Fitzhardinge int err; 2082aee16b3cSJeremy Fitzhardinge 2083c2febafcSKirill A. Shutemov pud = pud_alloc(mm, p4d, addr); 2084aee16b3cSJeremy Fitzhardinge if (!pud) 2085aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2086aee16b3cSJeremy Fitzhardinge do { 2087aee16b3cSJeremy Fitzhardinge next = pud_addr_end(addr, end); 2088aee16b3cSJeremy Fitzhardinge err = apply_to_pmd_range(mm, pud, addr, next, fn, data); 2089aee16b3cSJeremy Fitzhardinge if (err) 2090aee16b3cSJeremy Fitzhardinge break; 2091aee16b3cSJeremy Fitzhardinge } while (pud++, addr = next, addr != end); 2092aee16b3cSJeremy Fitzhardinge return err; 2093aee16b3cSJeremy Fitzhardinge } 2094aee16b3cSJeremy Fitzhardinge 2095c2febafcSKirill A. Shutemov static int apply_to_p4d_range(struct mm_struct *mm, pgd_t *pgd, 2096c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 2097c2febafcSKirill A. Shutemov pte_fn_t fn, void *data) 2098c2febafcSKirill A. Shutemov { 2099c2febafcSKirill A. Shutemov p4d_t *p4d; 2100c2febafcSKirill A. Shutemov unsigned long next; 2101c2febafcSKirill A. Shutemov int err; 2102c2febafcSKirill A. Shutemov 2103c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, addr); 2104c2febafcSKirill A. Shutemov if (!p4d) 2105c2febafcSKirill A. Shutemov return -ENOMEM; 2106c2febafcSKirill A. Shutemov do { 2107c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 2108c2febafcSKirill A. Shutemov err = apply_to_pud_range(mm, p4d, addr, next, fn, data); 2109c2febafcSKirill A. Shutemov if (err) 2110c2febafcSKirill A. Shutemov break; 2111c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 2112c2febafcSKirill A. Shutemov return err; 2113c2febafcSKirill A. Shutemov } 2114c2febafcSKirill A. Shutemov 2115aee16b3cSJeremy Fitzhardinge /* 2116aee16b3cSJeremy Fitzhardinge * Scan a region of virtual memory, filling in page tables as necessary 2117aee16b3cSJeremy Fitzhardinge * and calling a provided function on each leaf page table. 2118aee16b3cSJeremy Fitzhardinge */ 2119aee16b3cSJeremy Fitzhardinge int apply_to_page_range(struct mm_struct *mm, unsigned long addr, 2120aee16b3cSJeremy Fitzhardinge unsigned long size, pte_fn_t fn, void *data) 2121aee16b3cSJeremy Fitzhardinge { 2122aee16b3cSJeremy Fitzhardinge pgd_t *pgd; 2123aee16b3cSJeremy Fitzhardinge unsigned long next; 212457250a5bSJeremy Fitzhardinge unsigned long end = addr + size; 2125aee16b3cSJeremy Fitzhardinge int err; 2126aee16b3cSJeremy Fitzhardinge 21279cb65bc3SMika Penttilä if (WARN_ON(addr >= end)) 21289cb65bc3SMika Penttilä return -EINVAL; 21299cb65bc3SMika Penttilä 2130aee16b3cSJeremy Fitzhardinge pgd = pgd_offset(mm, addr); 2131aee16b3cSJeremy Fitzhardinge do { 2132aee16b3cSJeremy Fitzhardinge next = pgd_addr_end(addr, end); 2133c2febafcSKirill A. Shutemov err = apply_to_p4d_range(mm, pgd, addr, next, fn, data); 2134aee16b3cSJeremy Fitzhardinge if (err) 2135aee16b3cSJeremy Fitzhardinge break; 2136aee16b3cSJeremy Fitzhardinge } while (pgd++, addr = next, addr != end); 213757250a5bSJeremy Fitzhardinge 2138aee16b3cSJeremy Fitzhardinge return err; 2139aee16b3cSJeremy Fitzhardinge } 2140aee16b3cSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(apply_to_page_range); 2141aee16b3cSJeremy Fitzhardinge 21421da177e4SLinus Torvalds /* 21439b4bdd2fSKirill A. Shutemov * handle_pte_fault chooses page fault handler according to an entry which was 21449b4bdd2fSKirill A. Shutemov * read non-atomically. Before making any commitment, on those architectures 21459b4bdd2fSKirill A. Shutemov * or configurations (e.g. i386 with PAE) which might give a mix of unmatched 21469b4bdd2fSKirill A. Shutemov * parts, do_swap_page must check under lock before unmapping the pte and 21479b4bdd2fSKirill A. Shutemov * proceeding (but do_wp_page is only called after already making such a check; 2148a335b2e1SRyota Ozaki * and do_anonymous_page can safely check later on). 21498f4e2101SHugh Dickins */ 21504c21e2f2SHugh Dickins static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd, 21518f4e2101SHugh Dickins pte_t *page_table, pte_t orig_pte) 21528f4e2101SHugh Dickins { 21538f4e2101SHugh Dickins int same = 1; 21548f4e2101SHugh Dickins #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT) 21558f4e2101SHugh Dickins if (sizeof(pte_t) > sizeof(unsigned long)) { 21564c21e2f2SHugh Dickins spinlock_t *ptl = pte_lockptr(mm, pmd); 21574c21e2f2SHugh Dickins spin_lock(ptl); 21588f4e2101SHugh Dickins same = pte_same(*page_table, orig_pte); 21594c21e2f2SHugh Dickins spin_unlock(ptl); 21608f4e2101SHugh Dickins } 21618f4e2101SHugh Dickins #endif 21628f4e2101SHugh Dickins pte_unmap(page_table); 21638f4e2101SHugh Dickins return same; 21648f4e2101SHugh Dickins } 21658f4e2101SHugh Dickins 216683d116c5SJia He static inline bool cow_user_page(struct page *dst, struct page *src, 216783d116c5SJia He struct vm_fault *vmf) 21686aab341eSLinus Torvalds { 216983d116c5SJia He bool ret; 217083d116c5SJia He void *kaddr; 217183d116c5SJia He void __user *uaddr; 217283d116c5SJia He bool force_mkyoung; 217383d116c5SJia He struct vm_area_struct *vma = vmf->vma; 217483d116c5SJia He struct mm_struct *mm = vma->vm_mm; 217583d116c5SJia He unsigned long addr = vmf->address; 217683d116c5SJia He 21770abdd7a8SDan Williams debug_dma_assert_idle(src); 21780abdd7a8SDan Williams 217983d116c5SJia He if (likely(src)) { 218083d116c5SJia He copy_user_highpage(dst, src, addr, vma); 218183d116c5SJia He return true; 218283d116c5SJia He } 218383d116c5SJia He 21846aab341eSLinus Torvalds /* 21856aab341eSLinus Torvalds * If the source page was a PFN mapping, we don't have 21866aab341eSLinus Torvalds * a "struct page" for it. We do a best-effort copy by 21876aab341eSLinus Torvalds * just copying from the original user address. If that 21886aab341eSLinus Torvalds * fails, we just zero-fill it. Live with it. 21896aab341eSLinus Torvalds */ 219083d116c5SJia He kaddr = kmap_atomic(dst); 219183d116c5SJia He uaddr = (void __user *)(addr & PAGE_MASK); 219283d116c5SJia He 219383d116c5SJia He /* 219483d116c5SJia He * On architectures with software "accessed" bits, we would 219583d116c5SJia He * take a double page fault, so mark it accessed here. 219683d116c5SJia He */ 219783d116c5SJia He force_mkyoung = arch_faults_on_old_pte() && !pte_young(vmf->orig_pte); 219883d116c5SJia He if (force_mkyoung) { 219983d116c5SJia He pte_t entry; 220083d116c5SJia He 220183d116c5SJia He vmf->pte = pte_offset_map_lock(mm, vmf->pmd, addr, &vmf->ptl); 220283d116c5SJia He if (!likely(pte_same(*vmf->pte, vmf->orig_pte))) { 220383d116c5SJia He /* 220483d116c5SJia He * Other thread has already handled the fault 220583d116c5SJia He * and we don't need to do anything. If it's 220683d116c5SJia He * not the case, the fault will be triggered 220783d116c5SJia He * again on the same address. 220883d116c5SJia He */ 220983d116c5SJia He ret = false; 221083d116c5SJia He goto pte_unlock; 221183d116c5SJia He } 221283d116c5SJia He 221383d116c5SJia He entry = pte_mkyoung(vmf->orig_pte); 221483d116c5SJia He if (ptep_set_access_flags(vma, addr, vmf->pte, entry, 0)) 221583d116c5SJia He update_mmu_cache(vma, addr, vmf->pte); 221683d116c5SJia He } 22175d2a2dbbSLinus Torvalds 22185d2a2dbbSLinus Torvalds /* 22195d2a2dbbSLinus Torvalds * This really shouldn't fail, because the page is there 22205d2a2dbbSLinus Torvalds * in the page tables. But it might just be unreadable, 22215d2a2dbbSLinus Torvalds * in which case we just give up and fill the result with 22225d2a2dbbSLinus Torvalds * zeroes. 22235d2a2dbbSLinus Torvalds */ 222483d116c5SJia He if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) { 222583d116c5SJia He /* 222683d116c5SJia He * Give a warn in case there can be some obscure 222783d116c5SJia He * use-case 222883d116c5SJia He */ 222983d116c5SJia He WARN_ON_ONCE(1); 22303ecb01dfSJan Beulich clear_page(kaddr); 223183d116c5SJia He } 223283d116c5SJia He 223383d116c5SJia He ret = true; 223483d116c5SJia He 223583d116c5SJia He pte_unlock: 223683d116c5SJia He if (force_mkyoung) 223783d116c5SJia He pte_unmap_unlock(vmf->pte, vmf->ptl); 22389b04c5feSCong Wang kunmap_atomic(kaddr); 2239c4ec7b0dSDmitriy Monakhov flush_dcache_page(dst); 224083d116c5SJia He 224183d116c5SJia He return ret; 22426aab341eSLinus Torvalds } 22436aab341eSLinus Torvalds 2244c20cd45eSMichal Hocko static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma) 2245c20cd45eSMichal Hocko { 2246c20cd45eSMichal Hocko struct file *vm_file = vma->vm_file; 2247c20cd45eSMichal Hocko 2248c20cd45eSMichal Hocko if (vm_file) 2249c20cd45eSMichal Hocko return mapping_gfp_mask(vm_file->f_mapping) | __GFP_FS | __GFP_IO; 2250c20cd45eSMichal Hocko 2251c20cd45eSMichal Hocko /* 2252c20cd45eSMichal Hocko * Special mappings (e.g. VDSO) do not have any file so fake 2253c20cd45eSMichal Hocko * a default GFP_KERNEL for them. 2254c20cd45eSMichal Hocko */ 2255c20cd45eSMichal Hocko return GFP_KERNEL; 2256c20cd45eSMichal Hocko } 2257c20cd45eSMichal Hocko 22581da177e4SLinus Torvalds /* 2259fb09a464SKirill A. Shutemov * Notify the address space that the page is about to become writable so that 2260fb09a464SKirill A. Shutemov * it can prohibit this or wait for the page to get into an appropriate state. 2261fb09a464SKirill A. Shutemov * 2262fb09a464SKirill A. Shutemov * We do this without the lock held, so that it can sleep if it needs to. 2263fb09a464SKirill A. Shutemov */ 22642b740303SSouptick Joarder static vm_fault_t do_page_mkwrite(struct vm_fault *vmf) 2265fb09a464SKirill A. Shutemov { 22662b740303SSouptick Joarder vm_fault_t ret; 226738b8cb7fSJan Kara struct page *page = vmf->page; 226838b8cb7fSJan Kara unsigned int old_flags = vmf->flags; 2269fb09a464SKirill A. Shutemov 227038b8cb7fSJan Kara vmf->flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE; 2271fb09a464SKirill A. Shutemov 2272dc617f29SDarrick J. Wong if (vmf->vma->vm_file && 2273dc617f29SDarrick J. Wong IS_SWAPFILE(vmf->vma->vm_file->f_mapping->host)) 2274dc617f29SDarrick J. Wong return VM_FAULT_SIGBUS; 2275dc617f29SDarrick J. Wong 227611bac800SDave Jiang ret = vmf->vma->vm_ops->page_mkwrite(vmf); 227738b8cb7fSJan Kara /* Restore original flags so that caller is not surprised */ 227838b8cb7fSJan Kara vmf->flags = old_flags; 2279fb09a464SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) 2280fb09a464SKirill A. Shutemov return ret; 2281fb09a464SKirill A. Shutemov if (unlikely(!(ret & VM_FAULT_LOCKED))) { 2282fb09a464SKirill A. Shutemov lock_page(page); 2283fb09a464SKirill A. Shutemov if (!page->mapping) { 2284fb09a464SKirill A. Shutemov unlock_page(page); 2285fb09a464SKirill A. Shutemov return 0; /* retry */ 2286fb09a464SKirill A. Shutemov } 2287fb09a464SKirill A. Shutemov ret |= VM_FAULT_LOCKED; 2288fb09a464SKirill A. Shutemov } else 2289fb09a464SKirill A. Shutemov VM_BUG_ON_PAGE(!PageLocked(page), page); 2290fb09a464SKirill A. Shutemov return ret; 2291fb09a464SKirill A. Shutemov } 2292fb09a464SKirill A. Shutemov 2293fb09a464SKirill A. Shutemov /* 229497ba0c2bSJan Kara * Handle dirtying of a page in shared file mapping on a write fault. 22954e047f89SShachar Raindel * 229697ba0c2bSJan Kara * The function expects the page to be locked and unlocks it. 22974e047f89SShachar Raindel */ 229889b15332SJohannes Weiner static vm_fault_t fault_dirty_shared_page(struct vm_fault *vmf) 22994e047f89SShachar Raindel { 230089b15332SJohannes Weiner struct vm_area_struct *vma = vmf->vma; 23014e047f89SShachar Raindel struct address_space *mapping; 230289b15332SJohannes Weiner struct page *page = vmf->page; 230397ba0c2bSJan Kara bool dirtied; 230497ba0c2bSJan Kara bool page_mkwrite = vma->vm_ops && vma->vm_ops->page_mkwrite; 23054e047f89SShachar Raindel 23064e047f89SShachar Raindel dirtied = set_page_dirty(page); 23074e047f89SShachar Raindel VM_BUG_ON_PAGE(PageAnon(page), page); 230897ba0c2bSJan Kara /* 230997ba0c2bSJan Kara * Take a local copy of the address_space - page.mapping may be zeroed 231097ba0c2bSJan Kara * by truncate after unlock_page(). The address_space itself remains 231197ba0c2bSJan Kara * pinned by vma->vm_file's reference. We rely on unlock_page()'s 231297ba0c2bSJan Kara * release semantics to prevent the compiler from undoing this copying. 231397ba0c2bSJan Kara */ 231497ba0c2bSJan Kara mapping = page_rmapping(page); 23154e047f89SShachar Raindel unlock_page(page); 23164e047f89SShachar Raindel 23174e047f89SShachar Raindel if (!page_mkwrite) 23184e047f89SShachar Raindel file_update_time(vma->vm_file); 231989b15332SJohannes Weiner 232089b15332SJohannes Weiner /* 232189b15332SJohannes Weiner * Throttle page dirtying rate down to writeback speed. 232289b15332SJohannes Weiner * 232389b15332SJohannes Weiner * mapping may be NULL here because some device drivers do not 232489b15332SJohannes Weiner * set page.mapping but still dirty their pages 232589b15332SJohannes Weiner * 232689b15332SJohannes Weiner * Drop the mmap_sem before waiting on IO, if we can. The file 232789b15332SJohannes Weiner * is pinning the mapping, as per above. 232889b15332SJohannes Weiner */ 232989b15332SJohannes Weiner if ((dirtied || page_mkwrite) && mapping) { 233089b15332SJohannes Weiner struct file *fpin; 233189b15332SJohannes Weiner 233289b15332SJohannes Weiner fpin = maybe_unlock_mmap_for_io(vmf, NULL); 233389b15332SJohannes Weiner balance_dirty_pages_ratelimited(mapping); 233489b15332SJohannes Weiner if (fpin) { 233589b15332SJohannes Weiner fput(fpin); 233689b15332SJohannes Weiner return VM_FAULT_RETRY; 233789b15332SJohannes Weiner } 233889b15332SJohannes Weiner } 233989b15332SJohannes Weiner 234089b15332SJohannes Weiner return 0; 23414e047f89SShachar Raindel } 23424e047f89SShachar Raindel 234397ba0c2bSJan Kara /* 23444e047f89SShachar Raindel * Handle write page faults for pages that can be reused in the current vma 23454e047f89SShachar Raindel * 23464e047f89SShachar Raindel * This can happen either due to the mapping being with the VM_SHARED flag, 23474e047f89SShachar Raindel * or due to us being the last reference standing to the page. In either 23484e047f89SShachar Raindel * case, all we need to do here is to mark the page as writable and update 23494e047f89SShachar Raindel * any related book-keeping. 23504e047f89SShachar Raindel */ 2351997dd98dSJan Kara static inline void wp_page_reuse(struct vm_fault *vmf) 235282b0f8c3SJan Kara __releases(vmf->ptl) 23534e047f89SShachar Raindel { 235482b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 2355a41b70d6SJan Kara struct page *page = vmf->page; 23564e047f89SShachar Raindel pte_t entry; 23574e047f89SShachar Raindel /* 23584e047f89SShachar Raindel * Clear the pages cpupid information as the existing 23594e047f89SShachar Raindel * information potentially belongs to a now completely 23604e047f89SShachar Raindel * unrelated process. 23614e047f89SShachar Raindel */ 23624e047f89SShachar Raindel if (page) 23634e047f89SShachar Raindel page_cpupid_xchg_last(page, (1 << LAST_CPUPID_SHIFT) - 1); 23644e047f89SShachar Raindel 23652994302bSJan Kara flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); 23662994302bSJan Kara entry = pte_mkyoung(vmf->orig_pte); 23674e047f89SShachar Raindel entry = maybe_mkwrite(pte_mkdirty(entry), vma); 236882b0f8c3SJan Kara if (ptep_set_access_flags(vma, vmf->address, vmf->pte, entry, 1)) 236982b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 237082b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 23714e047f89SShachar Raindel } 23724e047f89SShachar Raindel 23734e047f89SShachar Raindel /* 23742f38ab2cSShachar Raindel * Handle the case of a page which we actually need to copy to a new page. 23752f38ab2cSShachar Raindel * 23762f38ab2cSShachar Raindel * Called with mmap_sem locked and the old page referenced, but 23772f38ab2cSShachar Raindel * without the ptl held. 23782f38ab2cSShachar Raindel * 23792f38ab2cSShachar Raindel * High level logic flow: 23802f38ab2cSShachar Raindel * 23812f38ab2cSShachar Raindel * - Allocate a page, copy the content of the old page to the new one. 23822f38ab2cSShachar Raindel * - Handle book keeping and accounting - cgroups, mmu-notifiers, etc. 23832f38ab2cSShachar Raindel * - Take the PTL. If the pte changed, bail out and release the allocated page 23842f38ab2cSShachar Raindel * - If the pte is still the way we remember it, update the page table and all 23852f38ab2cSShachar Raindel * relevant references. This includes dropping the reference the page-table 23862f38ab2cSShachar Raindel * held to the old page, as well as updating the rmap. 23872f38ab2cSShachar Raindel * - In any case, unlock the PTL and drop the reference we took to the old page. 23882f38ab2cSShachar Raindel */ 23892b740303SSouptick Joarder static vm_fault_t wp_page_copy(struct vm_fault *vmf) 23902f38ab2cSShachar Raindel { 239182b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 2392bae473a4SKirill A. Shutemov struct mm_struct *mm = vma->vm_mm; 2393a41b70d6SJan Kara struct page *old_page = vmf->page; 23942f38ab2cSShachar Raindel struct page *new_page = NULL; 23952f38ab2cSShachar Raindel pte_t entry; 23962f38ab2cSShachar Raindel int page_copied = 0; 23972f38ab2cSShachar Raindel struct mem_cgroup *memcg; 2398ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 23992f38ab2cSShachar Raindel 24002f38ab2cSShachar Raindel if (unlikely(anon_vma_prepare(vma))) 24012f38ab2cSShachar Raindel goto oom; 24022f38ab2cSShachar Raindel 24032994302bSJan Kara if (is_zero_pfn(pte_pfn(vmf->orig_pte))) { 240482b0f8c3SJan Kara new_page = alloc_zeroed_user_highpage_movable(vma, 240582b0f8c3SJan Kara vmf->address); 24062f38ab2cSShachar Raindel if (!new_page) 24072f38ab2cSShachar Raindel goto oom; 24082f38ab2cSShachar Raindel } else { 2409bae473a4SKirill A. Shutemov new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, 241082b0f8c3SJan Kara vmf->address); 24112f38ab2cSShachar Raindel if (!new_page) 24122f38ab2cSShachar Raindel goto oom; 241383d116c5SJia He 241483d116c5SJia He if (!cow_user_page(new_page, old_page, vmf)) { 241583d116c5SJia He /* 241683d116c5SJia He * COW failed, if the fault was solved by other, 241783d116c5SJia He * it's fine. If not, userspace would re-fault on 241883d116c5SJia He * the same address and we will handle the fault 241983d116c5SJia He * from the second attempt. 242083d116c5SJia He */ 242183d116c5SJia He put_page(new_page); 242283d116c5SJia He if (old_page) 242383d116c5SJia He put_page(old_page); 242483d116c5SJia He return 0; 242583d116c5SJia He } 24262f38ab2cSShachar Raindel } 24272f38ab2cSShachar Raindel 24282cf85583STejun Heo if (mem_cgroup_try_charge_delay(new_page, mm, GFP_KERNEL, &memcg, false)) 24292f38ab2cSShachar Raindel goto oom_free_new; 24302f38ab2cSShachar Raindel 2431eb3c24f3SMel Gorman __SetPageUptodate(new_page); 2432eb3c24f3SMel Gorman 24337269f999SJérôme Glisse mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm, 24346f4f13e8SJérôme Glisse vmf->address & PAGE_MASK, 2435ac46d4f3SJérôme Glisse (vmf->address & PAGE_MASK) + PAGE_SIZE); 2436ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 24372f38ab2cSShachar Raindel 24382f38ab2cSShachar Raindel /* 24392f38ab2cSShachar Raindel * Re-check the pte - we dropped the lock 24402f38ab2cSShachar Raindel */ 244182b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(mm, vmf->pmd, vmf->address, &vmf->ptl); 24422994302bSJan Kara if (likely(pte_same(*vmf->pte, vmf->orig_pte))) { 24432f38ab2cSShachar Raindel if (old_page) { 24442f38ab2cSShachar Raindel if (!PageAnon(old_page)) { 2445eca56ff9SJerome Marchand dec_mm_counter_fast(mm, 2446eca56ff9SJerome Marchand mm_counter_file(old_page)); 24472f38ab2cSShachar Raindel inc_mm_counter_fast(mm, MM_ANONPAGES); 24482f38ab2cSShachar Raindel } 24492f38ab2cSShachar Raindel } else { 24502f38ab2cSShachar Raindel inc_mm_counter_fast(mm, MM_ANONPAGES); 24512f38ab2cSShachar Raindel } 24522994302bSJan Kara flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); 24532f38ab2cSShachar Raindel entry = mk_pte(new_page, vma->vm_page_prot); 24542f38ab2cSShachar Raindel entry = maybe_mkwrite(pte_mkdirty(entry), vma); 24552f38ab2cSShachar Raindel /* 24562f38ab2cSShachar Raindel * Clear the pte entry and flush it first, before updating the 24572f38ab2cSShachar Raindel * pte with the new entry. This will avoid a race condition 24582f38ab2cSShachar Raindel * seen in the presence of one thread doing SMC and another 24592f38ab2cSShachar Raindel * thread doing COW. 24602f38ab2cSShachar Raindel */ 246182b0f8c3SJan Kara ptep_clear_flush_notify(vma, vmf->address, vmf->pte); 246282b0f8c3SJan Kara page_add_new_anon_rmap(new_page, vma, vmf->address, false); 2463f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(new_page, memcg, false, false); 24642f38ab2cSShachar Raindel lru_cache_add_active_or_unevictable(new_page, vma); 24652f38ab2cSShachar Raindel /* 24662f38ab2cSShachar Raindel * We call the notify macro here because, when using secondary 24672f38ab2cSShachar Raindel * mmu page tables (such as kvm shadow page tables), we want the 24682f38ab2cSShachar Raindel * new page to be mapped directly into the secondary page table. 24692f38ab2cSShachar Raindel */ 247082b0f8c3SJan Kara set_pte_at_notify(mm, vmf->address, vmf->pte, entry); 247182b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 24722f38ab2cSShachar Raindel if (old_page) { 24732f38ab2cSShachar Raindel /* 24742f38ab2cSShachar Raindel * Only after switching the pte to the new page may 24752f38ab2cSShachar Raindel * we remove the mapcount here. Otherwise another 24762f38ab2cSShachar Raindel * process may come and find the rmap count decremented 24772f38ab2cSShachar Raindel * before the pte is switched to the new page, and 24782f38ab2cSShachar Raindel * "reuse" the old page writing into it while our pte 24792f38ab2cSShachar Raindel * here still points into it and can be read by other 24802f38ab2cSShachar Raindel * threads. 24812f38ab2cSShachar Raindel * 24822f38ab2cSShachar Raindel * The critical issue is to order this 24832f38ab2cSShachar Raindel * page_remove_rmap with the ptp_clear_flush above. 24842f38ab2cSShachar Raindel * Those stores are ordered by (if nothing else,) 24852f38ab2cSShachar Raindel * the barrier present in the atomic_add_negative 24862f38ab2cSShachar Raindel * in page_remove_rmap. 24872f38ab2cSShachar Raindel * 24882f38ab2cSShachar Raindel * Then the TLB flush in ptep_clear_flush ensures that 24892f38ab2cSShachar Raindel * no process can access the old page before the 24902f38ab2cSShachar Raindel * decremented mapcount is visible. And the old page 24912f38ab2cSShachar Raindel * cannot be reused until after the decremented 24922f38ab2cSShachar Raindel * mapcount is visible. So transitively, TLBs to 24932f38ab2cSShachar Raindel * old page will be flushed before it can be reused. 24942f38ab2cSShachar Raindel */ 2495d281ee61SKirill A. Shutemov page_remove_rmap(old_page, false); 24962f38ab2cSShachar Raindel } 24972f38ab2cSShachar Raindel 24982f38ab2cSShachar Raindel /* Free the old page.. */ 24992f38ab2cSShachar Raindel new_page = old_page; 25002f38ab2cSShachar Raindel page_copied = 1; 25012f38ab2cSShachar Raindel } else { 2502f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(new_page, memcg, false); 25032f38ab2cSShachar Raindel } 25042f38ab2cSShachar Raindel 25052f38ab2cSShachar Raindel if (new_page) 250609cbfeafSKirill A. Shutemov put_page(new_page); 25072f38ab2cSShachar Raindel 250882b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 25094645b9feSJérôme Glisse /* 25104645b9feSJérôme Glisse * No need to double call mmu_notifier->invalidate_range() callback as 25114645b9feSJérôme Glisse * the above ptep_clear_flush_notify() did already call it. 25124645b9feSJérôme Glisse */ 2513ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_only_end(&range); 25142f38ab2cSShachar Raindel if (old_page) { 25152f38ab2cSShachar Raindel /* 25162f38ab2cSShachar Raindel * Don't let another task, with possibly unlocked vma, 25172f38ab2cSShachar Raindel * keep the mlocked page. 25182f38ab2cSShachar Raindel */ 25192f38ab2cSShachar Raindel if (page_copied && (vma->vm_flags & VM_LOCKED)) { 25202f38ab2cSShachar Raindel lock_page(old_page); /* LRU manipulation */ 2521e90309c9SKirill A. Shutemov if (PageMlocked(old_page)) 25222f38ab2cSShachar Raindel munlock_vma_page(old_page); 25232f38ab2cSShachar Raindel unlock_page(old_page); 25242f38ab2cSShachar Raindel } 252509cbfeafSKirill A. Shutemov put_page(old_page); 25262f38ab2cSShachar Raindel } 25272f38ab2cSShachar Raindel return page_copied ? VM_FAULT_WRITE : 0; 25282f38ab2cSShachar Raindel oom_free_new: 252909cbfeafSKirill A. Shutemov put_page(new_page); 25302f38ab2cSShachar Raindel oom: 25312f38ab2cSShachar Raindel if (old_page) 253209cbfeafSKirill A. Shutemov put_page(old_page); 25332f38ab2cSShachar Raindel return VM_FAULT_OOM; 25342f38ab2cSShachar Raindel } 25352f38ab2cSShachar Raindel 253666a6197cSJan Kara /** 253766a6197cSJan Kara * finish_mkwrite_fault - finish page fault for a shared mapping, making PTE 253866a6197cSJan Kara * writeable once the page is prepared 253966a6197cSJan Kara * 254066a6197cSJan Kara * @vmf: structure describing the fault 254166a6197cSJan Kara * 254266a6197cSJan Kara * This function handles all that is needed to finish a write page fault in a 254366a6197cSJan Kara * shared mapping due to PTE being read-only once the mapped page is prepared. 2544a862f68aSMike Rapoport * It handles locking of PTE and modifying it. 254566a6197cSJan Kara * 254666a6197cSJan Kara * The function expects the page to be locked or other protection against 254766a6197cSJan Kara * concurrent faults / writeback (such as DAX radix tree locks). 2548a862f68aSMike Rapoport * 2549a862f68aSMike Rapoport * Return: %VM_FAULT_WRITE on success, %0 when PTE got changed before 2550a862f68aSMike Rapoport * we acquired PTE lock. 255166a6197cSJan Kara */ 25522b740303SSouptick Joarder vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf) 255366a6197cSJan Kara { 255466a6197cSJan Kara WARN_ON_ONCE(!(vmf->vma->vm_flags & VM_SHARED)); 255566a6197cSJan Kara vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, vmf->address, 255666a6197cSJan Kara &vmf->ptl); 255766a6197cSJan Kara /* 255866a6197cSJan Kara * We might have raced with another page fault while we released the 255966a6197cSJan Kara * pte_offset_map_lock. 256066a6197cSJan Kara */ 256166a6197cSJan Kara if (!pte_same(*vmf->pte, vmf->orig_pte)) { 256266a6197cSJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2563a19e2553SJan Kara return VM_FAULT_NOPAGE; 256466a6197cSJan Kara } 256566a6197cSJan Kara wp_page_reuse(vmf); 2566a19e2553SJan Kara return 0; 256766a6197cSJan Kara } 256866a6197cSJan Kara 2569dd906184SBoaz Harrosh /* 2570dd906184SBoaz Harrosh * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED 2571dd906184SBoaz Harrosh * mapping 2572dd906184SBoaz Harrosh */ 25732b740303SSouptick Joarder static vm_fault_t wp_pfn_shared(struct vm_fault *vmf) 2574dd906184SBoaz Harrosh { 257582b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 2576bae473a4SKirill A. Shutemov 2577dd906184SBoaz Harrosh if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) { 25782b740303SSouptick Joarder vm_fault_t ret; 2579dd906184SBoaz Harrosh 258082b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2581fe82221fSJan Kara vmf->flags |= FAULT_FLAG_MKWRITE; 258211bac800SDave Jiang ret = vma->vm_ops->pfn_mkwrite(vmf); 25832f89dc12SJan Kara if (ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)) 2584dd906184SBoaz Harrosh return ret; 258566a6197cSJan Kara return finish_mkwrite_fault(vmf); 2586dd906184SBoaz Harrosh } 2587997dd98dSJan Kara wp_page_reuse(vmf); 2588997dd98dSJan Kara return VM_FAULT_WRITE; 2589dd906184SBoaz Harrosh } 2590dd906184SBoaz Harrosh 25912b740303SSouptick Joarder static vm_fault_t wp_page_shared(struct vm_fault *vmf) 259282b0f8c3SJan Kara __releases(vmf->ptl) 259393e478d4SShachar Raindel { 259482b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 259589b15332SJohannes Weiner vm_fault_t ret = VM_FAULT_WRITE; 259693e478d4SShachar Raindel 2597a41b70d6SJan Kara get_page(vmf->page); 259893e478d4SShachar Raindel 259993e478d4SShachar Raindel if (vma->vm_ops && vma->vm_ops->page_mkwrite) { 26002b740303SSouptick Joarder vm_fault_t tmp; 260193e478d4SShachar Raindel 260282b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 260338b8cb7fSJan Kara tmp = do_page_mkwrite(vmf); 260493e478d4SShachar Raindel if (unlikely(!tmp || (tmp & 260593e478d4SShachar Raindel (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { 2606a41b70d6SJan Kara put_page(vmf->page); 260793e478d4SShachar Raindel return tmp; 260893e478d4SShachar Raindel } 260966a6197cSJan Kara tmp = finish_mkwrite_fault(vmf); 2610a19e2553SJan Kara if (unlikely(tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) { 2611a41b70d6SJan Kara unlock_page(vmf->page); 2612a41b70d6SJan Kara put_page(vmf->page); 261366a6197cSJan Kara return tmp; 261493e478d4SShachar Raindel } 261566a6197cSJan Kara } else { 2616997dd98dSJan Kara wp_page_reuse(vmf); 2617997dd98dSJan Kara lock_page(vmf->page); 261893e478d4SShachar Raindel } 261989b15332SJohannes Weiner ret |= fault_dirty_shared_page(vmf); 2620997dd98dSJan Kara put_page(vmf->page); 262193e478d4SShachar Raindel 262289b15332SJohannes Weiner return ret; 262393e478d4SShachar Raindel } 262493e478d4SShachar Raindel 26252f38ab2cSShachar Raindel /* 26261da177e4SLinus Torvalds * This routine handles present pages, when users try to write 26271da177e4SLinus Torvalds * to a shared page. It is done by copying the page to a new address 26281da177e4SLinus Torvalds * and decrementing the shared-page counter for the old page. 26291da177e4SLinus Torvalds * 26301da177e4SLinus Torvalds * Note that this routine assumes that the protection checks have been 26311da177e4SLinus Torvalds * done by the caller (the low-level page fault routine in most cases). 26321da177e4SLinus Torvalds * Thus we can safely just mark it writable once we've done any necessary 26331da177e4SLinus Torvalds * COW. 26341da177e4SLinus Torvalds * 26351da177e4SLinus Torvalds * We also mark the page dirty at this point even though the page will 26361da177e4SLinus Torvalds * change only once the write actually happens. This avoids a few races, 26371da177e4SLinus Torvalds * and potentially makes it more efficient. 26381da177e4SLinus Torvalds * 26398f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 26408f4e2101SHugh Dickins * but allow concurrent faults), with pte both mapped and locked. 26418f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 26421da177e4SLinus Torvalds */ 26432b740303SSouptick Joarder static vm_fault_t do_wp_page(struct vm_fault *vmf) 264482b0f8c3SJan Kara __releases(vmf->ptl) 26451da177e4SLinus Torvalds { 264682b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 26471da177e4SLinus Torvalds 2648a41b70d6SJan Kara vmf->page = vm_normal_page(vma, vmf->address, vmf->orig_pte); 2649a41b70d6SJan Kara if (!vmf->page) { 2650251b97f5SPeter Zijlstra /* 265164e45507SPeter Feiner * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a 265264e45507SPeter Feiner * VM_PFNMAP VMA. 2653251b97f5SPeter Zijlstra * 2654251b97f5SPeter Zijlstra * We should not cow pages in a shared writeable mapping. 2655dd906184SBoaz Harrosh * Just mark the pages writable and/or call ops->pfn_mkwrite. 2656251b97f5SPeter Zijlstra */ 2657251b97f5SPeter Zijlstra if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 2658251b97f5SPeter Zijlstra (VM_WRITE|VM_SHARED)) 26592994302bSJan Kara return wp_pfn_shared(vmf); 26602f38ab2cSShachar Raindel 266182b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2662a41b70d6SJan Kara return wp_page_copy(vmf); 2663251b97f5SPeter Zijlstra } 26641da177e4SLinus Torvalds 2665d08b3851SPeter Zijlstra /* 2666ee6a6457SPeter Zijlstra * Take out anonymous pages first, anonymous shared vmas are 2667ee6a6457SPeter Zijlstra * not dirty accountable. 2668d08b3851SPeter Zijlstra */ 266952d1e606SKirill Tkhai if (PageAnon(vmf->page)) { 2670ba3c4ce6SHuang Ying int total_map_swapcount; 267152d1e606SKirill Tkhai if (PageKsm(vmf->page) && (PageSwapCache(vmf->page) || 267252d1e606SKirill Tkhai page_count(vmf->page) != 1)) 267352d1e606SKirill Tkhai goto copy; 2674a41b70d6SJan Kara if (!trylock_page(vmf->page)) { 2675a41b70d6SJan Kara get_page(vmf->page); 267682b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2677a41b70d6SJan Kara lock_page(vmf->page); 267882b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 267982b0f8c3SJan Kara vmf->address, &vmf->ptl); 26802994302bSJan Kara if (!pte_same(*vmf->pte, vmf->orig_pte)) { 2681a41b70d6SJan Kara unlock_page(vmf->page); 268282b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2683a41b70d6SJan Kara put_page(vmf->page); 268428766805SShachar Raindel return 0; 2685ab967d86SHugh Dickins } 2686a41b70d6SJan Kara put_page(vmf->page); 2687ab967d86SHugh Dickins } 268852d1e606SKirill Tkhai if (PageKsm(vmf->page)) { 268952d1e606SKirill Tkhai bool reused = reuse_ksm_page(vmf->page, vmf->vma, 269052d1e606SKirill Tkhai vmf->address); 269152d1e606SKirill Tkhai unlock_page(vmf->page); 269252d1e606SKirill Tkhai if (!reused) 269352d1e606SKirill Tkhai goto copy; 269452d1e606SKirill Tkhai wp_page_reuse(vmf); 269552d1e606SKirill Tkhai return VM_FAULT_WRITE; 269652d1e606SKirill Tkhai } 2697ba3c4ce6SHuang Ying if (reuse_swap_page(vmf->page, &total_map_swapcount)) { 2698ba3c4ce6SHuang Ying if (total_map_swapcount == 1) { 2699c44b6743SRik van Riel /* 27006d0a07edSAndrea Arcangeli * The page is all ours. Move it to 27016d0a07edSAndrea Arcangeli * our anon_vma so the rmap code will 27026d0a07edSAndrea Arcangeli * not search our parent or siblings. 27036d0a07edSAndrea Arcangeli * Protected against the rmap code by 27046d0a07edSAndrea Arcangeli * the page lock. 2705c44b6743SRik van Riel */ 2706a41b70d6SJan Kara page_move_anon_rmap(vmf->page, vma); 27076d0a07edSAndrea Arcangeli } 2708a41b70d6SJan Kara unlock_page(vmf->page); 2709997dd98dSJan Kara wp_page_reuse(vmf); 2710997dd98dSJan Kara return VM_FAULT_WRITE; 2711b009c024SMichel Lespinasse } 2712a41b70d6SJan Kara unlock_page(vmf->page); 2713ee6a6457SPeter Zijlstra } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 2714d08b3851SPeter Zijlstra (VM_WRITE|VM_SHARED))) { 2715a41b70d6SJan Kara return wp_page_shared(vmf); 27161da177e4SLinus Torvalds } 271752d1e606SKirill Tkhai copy: 27181da177e4SLinus Torvalds /* 27191da177e4SLinus Torvalds * Ok, we need to copy. Oh, well.. 27201da177e4SLinus Torvalds */ 2721a41b70d6SJan Kara get_page(vmf->page); 272228766805SShachar Raindel 272382b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2724a41b70d6SJan Kara return wp_page_copy(vmf); 27251da177e4SLinus Torvalds } 27261da177e4SLinus Torvalds 272797a89413SPeter Zijlstra static void unmap_mapping_range_vma(struct vm_area_struct *vma, 27281da177e4SLinus Torvalds unsigned long start_addr, unsigned long end_addr, 27291da177e4SLinus Torvalds struct zap_details *details) 27301da177e4SLinus Torvalds { 2731f5cc4eefSAl Viro zap_page_range_single(vma, start_addr, end_addr - start_addr, details); 27321da177e4SLinus Torvalds } 27331da177e4SLinus Torvalds 2734f808c13fSDavidlohr Bueso static inline void unmap_mapping_range_tree(struct rb_root_cached *root, 27351da177e4SLinus Torvalds struct zap_details *details) 27361da177e4SLinus Torvalds { 27371da177e4SLinus Torvalds struct vm_area_struct *vma; 27381da177e4SLinus Torvalds pgoff_t vba, vea, zba, zea; 27391da177e4SLinus Torvalds 27406b2dbba8SMichel Lespinasse vma_interval_tree_foreach(vma, root, 27411da177e4SLinus Torvalds details->first_index, details->last_index) { 27421da177e4SLinus Torvalds 27431da177e4SLinus Torvalds vba = vma->vm_pgoff; 2744d6e93217SLibin vea = vba + vma_pages(vma) - 1; 27451da177e4SLinus Torvalds zba = details->first_index; 27461da177e4SLinus Torvalds if (zba < vba) 27471da177e4SLinus Torvalds zba = vba; 27481da177e4SLinus Torvalds zea = details->last_index; 27491da177e4SLinus Torvalds if (zea > vea) 27501da177e4SLinus Torvalds zea = vea; 27511da177e4SLinus Torvalds 275297a89413SPeter Zijlstra unmap_mapping_range_vma(vma, 27531da177e4SLinus Torvalds ((zba - vba) << PAGE_SHIFT) + vma->vm_start, 27541da177e4SLinus Torvalds ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start, 275597a89413SPeter Zijlstra details); 27561da177e4SLinus Torvalds } 27571da177e4SLinus Torvalds } 27581da177e4SLinus Torvalds 27591da177e4SLinus Torvalds /** 2760977fbdcdSMatthew Wilcox * unmap_mapping_pages() - Unmap pages from processes. 2761977fbdcdSMatthew Wilcox * @mapping: The address space containing pages to be unmapped. 2762977fbdcdSMatthew Wilcox * @start: Index of first page to be unmapped. 2763977fbdcdSMatthew Wilcox * @nr: Number of pages to be unmapped. 0 to unmap to end of file. 2764977fbdcdSMatthew Wilcox * @even_cows: Whether to unmap even private COWed pages. 2765977fbdcdSMatthew Wilcox * 2766977fbdcdSMatthew Wilcox * Unmap the pages in this address space from any userspace process which 2767977fbdcdSMatthew Wilcox * has them mmaped. Generally, you want to remove COWed pages as well when 2768977fbdcdSMatthew Wilcox * a file is being truncated, but not when invalidating pages from the page 2769977fbdcdSMatthew Wilcox * cache. 2770977fbdcdSMatthew Wilcox */ 2771977fbdcdSMatthew Wilcox void unmap_mapping_pages(struct address_space *mapping, pgoff_t start, 2772977fbdcdSMatthew Wilcox pgoff_t nr, bool even_cows) 2773977fbdcdSMatthew Wilcox { 2774977fbdcdSMatthew Wilcox struct zap_details details = { }; 2775977fbdcdSMatthew Wilcox 2776977fbdcdSMatthew Wilcox details.check_mapping = even_cows ? NULL : mapping; 2777977fbdcdSMatthew Wilcox details.first_index = start; 2778977fbdcdSMatthew Wilcox details.last_index = start + nr - 1; 2779977fbdcdSMatthew Wilcox if (details.last_index < details.first_index) 2780977fbdcdSMatthew Wilcox details.last_index = ULONG_MAX; 2781977fbdcdSMatthew Wilcox 2782977fbdcdSMatthew Wilcox i_mmap_lock_write(mapping); 2783977fbdcdSMatthew Wilcox if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root))) 2784977fbdcdSMatthew Wilcox unmap_mapping_range_tree(&mapping->i_mmap, &details); 2785977fbdcdSMatthew Wilcox i_mmap_unlock_write(mapping); 2786977fbdcdSMatthew Wilcox } 2787977fbdcdSMatthew Wilcox 2788977fbdcdSMatthew Wilcox /** 27898a5f14a2SKirill A. Shutemov * unmap_mapping_range - unmap the portion of all mmaps in the specified 2790977fbdcdSMatthew Wilcox * address_space corresponding to the specified byte range in the underlying 27918a5f14a2SKirill A. Shutemov * file. 27928a5f14a2SKirill A. Shutemov * 27933d41088fSMartin Waitz * @mapping: the address space containing mmaps to be unmapped. 27941da177e4SLinus Torvalds * @holebegin: byte in first page to unmap, relative to the start of 27951da177e4SLinus Torvalds * the underlying file. This will be rounded down to a PAGE_SIZE 279625d9e2d1Snpiggin@suse.de * boundary. Note that this is different from truncate_pagecache(), which 27971da177e4SLinus Torvalds * must keep the partial page. In contrast, we must get rid of 27981da177e4SLinus Torvalds * partial pages. 27991da177e4SLinus Torvalds * @holelen: size of prospective hole in bytes. This will be rounded 28001da177e4SLinus Torvalds * up to a PAGE_SIZE boundary. A holelen of zero truncates to the 28011da177e4SLinus Torvalds * end of the file. 28021da177e4SLinus Torvalds * @even_cows: 1 when truncating a file, unmap even private COWed pages; 28031da177e4SLinus Torvalds * but 0 when invalidating pagecache, don't throw away private data. 28041da177e4SLinus Torvalds */ 28051da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping, 28061da177e4SLinus Torvalds loff_t const holebegin, loff_t const holelen, int even_cows) 28071da177e4SLinus Torvalds { 28081da177e4SLinus Torvalds pgoff_t hba = holebegin >> PAGE_SHIFT; 28091da177e4SLinus Torvalds pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 28101da177e4SLinus Torvalds 28111da177e4SLinus Torvalds /* Check for overflow. */ 28121da177e4SLinus Torvalds if (sizeof(holelen) > sizeof(hlen)) { 28131da177e4SLinus Torvalds long long holeend = 28141da177e4SLinus Torvalds (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 28151da177e4SLinus Torvalds if (holeend & ~(long long)ULONG_MAX) 28161da177e4SLinus Torvalds hlen = ULONG_MAX - hba + 1; 28171da177e4SLinus Torvalds } 28181da177e4SLinus Torvalds 2819977fbdcdSMatthew Wilcox unmap_mapping_pages(mapping, hba, hlen, even_cows); 28201da177e4SLinus Torvalds } 28211da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range); 28221da177e4SLinus Torvalds 28231da177e4SLinus Torvalds /* 28248f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 28258f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 28269a95f3cfSPaul Cassella * We return with pte unmapped and unlocked. 28279a95f3cfSPaul Cassella * 28289a95f3cfSPaul Cassella * We return with the mmap_sem locked or unlocked in the same cases 28299a95f3cfSPaul Cassella * as does filemap_fault(). 28301da177e4SLinus Torvalds */ 28312b740303SSouptick Joarder vm_fault_t do_swap_page(struct vm_fault *vmf) 28321da177e4SLinus Torvalds { 283382b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 2834eaf649ebSMinchan Kim struct page *page = NULL, *swapcache; 283500501b53SJohannes Weiner struct mem_cgroup *memcg; 283665500d23SHugh Dickins swp_entry_t entry; 28371da177e4SLinus Torvalds pte_t pte; 2838d065bd81SMichel Lespinasse int locked; 2839ad8c2ee8SRik van Riel int exclusive = 0; 28402b740303SSouptick Joarder vm_fault_t ret = 0; 28411da177e4SLinus Torvalds 2842eaf649ebSMinchan Kim if (!pte_unmap_same(vma->vm_mm, vmf->pmd, vmf->pte, vmf->orig_pte)) 28438f4e2101SHugh Dickins goto out; 284465500d23SHugh Dickins 28452994302bSJan Kara entry = pte_to_swp_entry(vmf->orig_pte); 2846d1737fdbSAndi Kleen if (unlikely(non_swap_entry(entry))) { 28470697212aSChristoph Lameter if (is_migration_entry(entry)) { 284882b0f8c3SJan Kara migration_entry_wait(vma->vm_mm, vmf->pmd, 284982b0f8c3SJan Kara vmf->address); 28505042db43SJérôme Glisse } else if (is_device_private_entry(entry)) { 2851897e6365SChristoph Hellwig vmf->page = device_private_entry_to_page(entry); 2852897e6365SChristoph Hellwig ret = vmf->page->pgmap->ops->migrate_to_ram(vmf); 2853d1737fdbSAndi Kleen } else if (is_hwpoison_entry(entry)) { 2854d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 2855d1737fdbSAndi Kleen } else { 28562994302bSJan Kara print_bad_pte(vma, vmf->address, vmf->orig_pte, NULL); 2857d99be1a8SHugh Dickins ret = VM_FAULT_SIGBUS; 2858d1737fdbSAndi Kleen } 28590697212aSChristoph Lameter goto out; 28600697212aSChristoph Lameter } 28610bcac06fSMinchan Kim 28620bcac06fSMinchan Kim 28630ff92245SShailabh Nagar delayacct_set_flag(DELAYACCT_PF_SWAPIN); 2864eaf649ebSMinchan Kim page = lookup_swap_cache(entry, vma, vmf->address); 2865f8020772SMinchan Kim swapcache = page; 2866f8020772SMinchan Kim 28671da177e4SLinus Torvalds if (!page) { 28680bcac06fSMinchan Kim struct swap_info_struct *si = swp_swap_info(entry); 28690bcac06fSMinchan Kim 2870aa8d22a1SMinchan Kim if (si->flags & SWP_SYNCHRONOUS_IO && 2871eb085574SHuang Ying __swap_count(entry) == 1) { 28720bcac06fSMinchan Kim /* skip swapcache */ 2873e9e9b7ecSMinchan Kim page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, 2874e9e9b7ecSMinchan Kim vmf->address); 28750bcac06fSMinchan Kim if (page) { 28760bcac06fSMinchan Kim __SetPageLocked(page); 28770bcac06fSMinchan Kim __SetPageSwapBacked(page); 28780bcac06fSMinchan Kim set_page_private(page, entry.val); 28790bcac06fSMinchan Kim lru_cache_add_anon(page); 28800bcac06fSMinchan Kim swap_readpage(page, true); 28810bcac06fSMinchan Kim } 2882aa8d22a1SMinchan Kim } else { 2883e9e9b7ecSMinchan Kim page = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, 2884e9e9b7ecSMinchan Kim vmf); 2885aa8d22a1SMinchan Kim swapcache = page; 28860bcac06fSMinchan Kim } 28870bcac06fSMinchan Kim 2888ec560175SHuang Ying if (!page) { 28891da177e4SLinus Torvalds /* 28908f4e2101SHugh Dickins * Back out if somebody else faulted in this pte 28918f4e2101SHugh Dickins * while we released the pte lock. 28921da177e4SLinus Torvalds */ 289382b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 289482b0f8c3SJan Kara vmf->address, &vmf->ptl); 28952994302bSJan Kara if (likely(pte_same(*vmf->pte, vmf->orig_pte))) 28961da177e4SLinus Torvalds ret = VM_FAULT_OOM; 28970ff92245SShailabh Nagar delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 289865500d23SHugh Dickins goto unlock; 28991da177e4SLinus Torvalds } 29001da177e4SLinus Torvalds 29011da177e4SLinus Torvalds /* Had to read the page from swap area: Major fault */ 29021da177e4SLinus Torvalds ret = VM_FAULT_MAJOR; 2903f8891e5eSChristoph Lameter count_vm_event(PGMAJFAULT); 29042262185cSRoman Gushchin count_memcg_event_mm(vma->vm_mm, PGMAJFAULT); 2905d1737fdbSAndi Kleen } else if (PageHWPoison(page)) { 290671f72525SWu Fengguang /* 290771f72525SWu Fengguang * hwpoisoned dirty swapcache pages are kept for killing 290871f72525SWu Fengguang * owner processes (which may be unknown at hwpoison time) 290971f72525SWu Fengguang */ 2910d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 2911d1737fdbSAndi Kleen delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 29124779cb31SAndi Kleen goto out_release; 29131da177e4SLinus Torvalds } 29141da177e4SLinus Torvalds 291582b0f8c3SJan Kara locked = lock_page_or_retry(page, vma->vm_mm, vmf->flags); 2916e709ffd6SRik van Riel 291720a1022dSBalbir Singh delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 2918d065bd81SMichel Lespinasse if (!locked) { 2919d065bd81SMichel Lespinasse ret |= VM_FAULT_RETRY; 2920d065bd81SMichel Lespinasse goto out_release; 2921d065bd81SMichel Lespinasse } 29221da177e4SLinus Torvalds 29234969c119SAndrea Arcangeli /* 292431c4a3d3SHugh Dickins * Make sure try_to_free_swap or reuse_swap_page or swapoff did not 292531c4a3d3SHugh Dickins * release the swapcache from under us. The page pin, and pte_same 292631c4a3d3SHugh Dickins * test below, are not enough to exclude that. Even if it is still 292731c4a3d3SHugh Dickins * swapcache, we need to check that the page's swap has not changed. 29284969c119SAndrea Arcangeli */ 29290bcac06fSMinchan Kim if (unlikely((!PageSwapCache(page) || 29300bcac06fSMinchan Kim page_private(page) != entry.val)) && swapcache) 29314969c119SAndrea Arcangeli goto out_page; 29324969c119SAndrea Arcangeli 293382b0f8c3SJan Kara page = ksm_might_need_to_copy(page, vma, vmf->address); 29344969c119SAndrea Arcangeli if (unlikely(!page)) { 29355ad64688SHugh Dickins ret = VM_FAULT_OOM; 29364969c119SAndrea Arcangeli page = swapcache; 29374969c119SAndrea Arcangeli goto out_page; 29384969c119SAndrea Arcangeli } 29395ad64688SHugh Dickins 29402cf85583STejun Heo if (mem_cgroup_try_charge_delay(page, vma->vm_mm, GFP_KERNEL, 2941bae473a4SKirill A. Shutemov &memcg, false)) { 2942073e587eSKAMEZAWA Hiroyuki ret = VM_FAULT_OOM; 2943bc43f75cSJohannes Weiner goto out_page; 2944073e587eSKAMEZAWA Hiroyuki } 2945073e587eSKAMEZAWA Hiroyuki 29461da177e4SLinus Torvalds /* 29478f4e2101SHugh Dickins * Back out if somebody else already faulted in this pte. 29481da177e4SLinus Torvalds */ 294982b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 295082b0f8c3SJan Kara &vmf->ptl); 29512994302bSJan Kara if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) 2952b8107480SKirill Korotaev goto out_nomap; 2953b8107480SKirill Korotaev 2954b8107480SKirill Korotaev if (unlikely(!PageUptodate(page))) { 2955b8107480SKirill Korotaev ret = VM_FAULT_SIGBUS; 2956b8107480SKirill Korotaev goto out_nomap; 29571da177e4SLinus Torvalds } 29581da177e4SLinus Torvalds 29598c7c6e34SKAMEZAWA Hiroyuki /* 29608c7c6e34SKAMEZAWA Hiroyuki * The page isn't present yet, go ahead with the fault. 29618c7c6e34SKAMEZAWA Hiroyuki * 29628c7c6e34SKAMEZAWA Hiroyuki * Be careful about the sequence of operations here. 29638c7c6e34SKAMEZAWA Hiroyuki * To get its accounting right, reuse_swap_page() must be called 29648c7c6e34SKAMEZAWA Hiroyuki * while the page is counted on swap but not yet in mapcount i.e. 29658c7c6e34SKAMEZAWA Hiroyuki * before page_add_anon_rmap() and swap_free(); try_to_free_swap() 29668c7c6e34SKAMEZAWA Hiroyuki * must be called after the swap_free(), or it will never succeed. 29678c7c6e34SKAMEZAWA Hiroyuki */ 29681da177e4SLinus Torvalds 2969bae473a4SKirill A. Shutemov inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); 2970bae473a4SKirill A. Shutemov dec_mm_counter_fast(vma->vm_mm, MM_SWAPENTS); 29711da177e4SLinus Torvalds pte = mk_pte(page, vma->vm_page_prot); 297282b0f8c3SJan Kara if ((vmf->flags & FAULT_FLAG_WRITE) && reuse_swap_page(page, NULL)) { 29731da177e4SLinus Torvalds pte = maybe_mkwrite(pte_mkdirty(pte), vma); 297482b0f8c3SJan Kara vmf->flags &= ~FAULT_FLAG_WRITE; 29759a5b489bSAndrea Arcangeli ret |= VM_FAULT_WRITE; 2976d281ee61SKirill A. Shutemov exclusive = RMAP_EXCLUSIVE; 29771da177e4SLinus Torvalds } 29781da177e4SLinus Torvalds flush_icache_page(vma, page); 29792994302bSJan Kara if (pte_swp_soft_dirty(vmf->orig_pte)) 2980179ef71cSCyrill Gorcunov pte = pte_mksoft_dirty(pte); 298182b0f8c3SJan Kara set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte); 2982ca827d55SKhalid Aziz arch_do_swap_page(vma->vm_mm, vma, vmf->address, pte, vmf->orig_pte); 29832994302bSJan Kara vmf->orig_pte = pte; 29840bcac06fSMinchan Kim 29850bcac06fSMinchan Kim /* ksm created a completely new copy */ 29860bcac06fSMinchan Kim if (unlikely(page != swapcache && swapcache)) { 298782b0f8c3SJan Kara page_add_new_anon_rmap(page, vma, vmf->address, false); 2988f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, false); 298900501b53SJohannes Weiner lru_cache_add_active_or_unevictable(page, vma); 29900bcac06fSMinchan Kim } else { 29910bcac06fSMinchan Kim do_page_add_anon_rmap(page, vma, vmf->address, exclusive); 29920bcac06fSMinchan Kim mem_cgroup_commit_charge(page, memcg, true, false); 29930bcac06fSMinchan Kim activate_page(page); 299400501b53SJohannes Weiner } 29951da177e4SLinus Torvalds 2996c475a8abSHugh Dickins swap_free(entry); 29975ccc5abaSVladimir Davydov if (mem_cgroup_swap_full(page) || 29985ccc5abaSVladimir Davydov (vma->vm_flags & VM_LOCKED) || PageMlocked(page)) 2999a2c43eedSHugh Dickins try_to_free_swap(page); 3000c475a8abSHugh Dickins unlock_page(page); 30010bcac06fSMinchan Kim if (page != swapcache && swapcache) { 30024969c119SAndrea Arcangeli /* 30034969c119SAndrea Arcangeli * Hold the lock to avoid the swap entry to be reused 30044969c119SAndrea Arcangeli * until we take the PT lock for the pte_same() check 30054969c119SAndrea Arcangeli * (to avoid false positives from pte_same). For 30064969c119SAndrea Arcangeli * further safety release the lock after the swap_free 30074969c119SAndrea Arcangeli * so that the swap count won't change under a 30084969c119SAndrea Arcangeli * parallel locked swapcache. 30094969c119SAndrea Arcangeli */ 30104969c119SAndrea Arcangeli unlock_page(swapcache); 301109cbfeafSKirill A. Shutemov put_page(swapcache); 30124969c119SAndrea Arcangeli } 3013c475a8abSHugh Dickins 301482b0f8c3SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) { 30152994302bSJan Kara ret |= do_wp_page(vmf); 301661469f1dSHugh Dickins if (ret & VM_FAULT_ERROR) 301761469f1dSHugh Dickins ret &= VM_FAULT_ERROR; 30181da177e4SLinus Torvalds goto out; 30191da177e4SLinus Torvalds } 30201da177e4SLinus Torvalds 30211da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 302282b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 302365500d23SHugh Dickins unlock: 302482b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 30251da177e4SLinus Torvalds out: 30261da177e4SLinus Torvalds return ret; 3027b8107480SKirill Korotaev out_nomap: 3028f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 302982b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3030bc43f75cSJohannes Weiner out_page: 3031b8107480SKirill Korotaev unlock_page(page); 30324779cb31SAndi Kleen out_release: 303309cbfeafSKirill A. Shutemov put_page(page); 30340bcac06fSMinchan Kim if (page != swapcache && swapcache) { 30354969c119SAndrea Arcangeli unlock_page(swapcache); 303609cbfeafSKirill A. Shutemov put_page(swapcache); 30374969c119SAndrea Arcangeli } 303865500d23SHugh Dickins return ret; 30391da177e4SLinus Torvalds } 30401da177e4SLinus Torvalds 30411da177e4SLinus Torvalds /* 30428f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 30438f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 30448f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 30451da177e4SLinus Torvalds */ 30462b740303SSouptick Joarder static vm_fault_t do_anonymous_page(struct vm_fault *vmf) 30471da177e4SLinus Torvalds { 304882b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 304900501b53SJohannes Weiner struct mem_cgroup *memcg; 30508f4e2101SHugh Dickins struct page *page; 30512b740303SSouptick Joarder vm_fault_t ret = 0; 30521da177e4SLinus Torvalds pte_t entry; 30531da177e4SLinus Torvalds 30546b7339f4SKirill A. Shutemov /* File mapping without ->vm_ops ? */ 30556b7339f4SKirill A. Shutemov if (vma->vm_flags & VM_SHARED) 30566b7339f4SKirill A. Shutemov return VM_FAULT_SIGBUS; 30576b7339f4SKirill A. Shutemov 30587267ec00SKirill A. Shutemov /* 30597267ec00SKirill A. Shutemov * Use pte_alloc() instead of pte_alloc_map(). We can't run 30607267ec00SKirill A. Shutemov * pte_offset_map() on pmds where a huge pmd might be created 30617267ec00SKirill A. Shutemov * from a different thread. 30627267ec00SKirill A. Shutemov * 30637267ec00SKirill A. Shutemov * pte_alloc_map() is safe to use under down_write(mmap_sem) or when 30647267ec00SKirill A. Shutemov * parallel threads are excluded by other means. 30657267ec00SKirill A. Shutemov * 30667267ec00SKirill A. Shutemov * Here we only have down_read(mmap_sem). 30677267ec00SKirill A. Shutemov */ 30684cf58924SJoel Fernandes (Google) if (pte_alloc(vma->vm_mm, vmf->pmd)) 30697267ec00SKirill A. Shutemov return VM_FAULT_OOM; 30707267ec00SKirill A. Shutemov 30717267ec00SKirill A. Shutemov /* See the comment in pte_alloc_one_map() */ 307282b0f8c3SJan Kara if (unlikely(pmd_trans_unstable(vmf->pmd))) 30737267ec00SKirill A. Shutemov return 0; 30747267ec00SKirill A. Shutemov 307511ac5524SLinus Torvalds /* Use the zero-page for reads */ 307682b0f8c3SJan Kara if (!(vmf->flags & FAULT_FLAG_WRITE) && 3077bae473a4SKirill A. Shutemov !mm_forbids_zeropage(vma->vm_mm)) { 307882b0f8c3SJan Kara entry = pte_mkspecial(pfn_pte(my_zero_pfn(vmf->address), 307962eede62SHugh Dickins vma->vm_page_prot)); 308082b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 308182b0f8c3SJan Kara vmf->address, &vmf->ptl); 308282b0f8c3SJan Kara if (!pte_none(*vmf->pte)) 3083a13ea5b7SHugh Dickins goto unlock; 30846b31d595SMichal Hocko ret = check_stable_address_space(vma->vm_mm); 30856b31d595SMichal Hocko if (ret) 30866b31d595SMichal Hocko goto unlock; 30876b251fc9SAndrea Arcangeli /* Deliver the page fault to userland, check inside PT lock */ 30886b251fc9SAndrea Arcangeli if (userfaultfd_missing(vma)) { 308982b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 309082b0f8c3SJan Kara return handle_userfault(vmf, VM_UFFD_MISSING); 30916b251fc9SAndrea Arcangeli } 3092a13ea5b7SHugh Dickins goto setpte; 3093a13ea5b7SHugh Dickins } 3094a13ea5b7SHugh Dickins 30951da177e4SLinus Torvalds /* Allocate our own private page. */ 30961da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 309765500d23SHugh Dickins goto oom; 309882b0f8c3SJan Kara page = alloc_zeroed_user_highpage_movable(vma, vmf->address); 30991da177e4SLinus Torvalds if (!page) 310065500d23SHugh Dickins goto oom; 3101eb3c24f3SMel Gorman 31022cf85583STejun Heo if (mem_cgroup_try_charge_delay(page, vma->vm_mm, GFP_KERNEL, &memcg, 31032cf85583STejun Heo false)) 3104eb3c24f3SMel Gorman goto oom_free_page; 3105eb3c24f3SMel Gorman 310652f37629SMinchan Kim /* 310752f37629SMinchan Kim * The memory barrier inside __SetPageUptodate makes sure that 310852f37629SMinchan Kim * preceeding stores to the page contents become visible before 310952f37629SMinchan Kim * the set_pte_at() write. 311052f37629SMinchan Kim */ 31110ed361deSNick Piggin __SetPageUptodate(page); 31121da177e4SLinus Torvalds 311365500d23SHugh Dickins entry = mk_pte(page, vma->vm_page_prot); 31141ac0cb5dSHugh Dickins if (vma->vm_flags & VM_WRITE) 31151ac0cb5dSHugh Dickins entry = pte_mkwrite(pte_mkdirty(entry)); 31168f4e2101SHugh Dickins 311782b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 311882b0f8c3SJan Kara &vmf->ptl); 311982b0f8c3SJan Kara if (!pte_none(*vmf->pte)) 31208f4e2101SHugh Dickins goto release; 31219ba69294SHugh Dickins 31226b31d595SMichal Hocko ret = check_stable_address_space(vma->vm_mm); 31236b31d595SMichal Hocko if (ret) 31246b31d595SMichal Hocko goto release; 31256b31d595SMichal Hocko 31266b251fc9SAndrea Arcangeli /* Deliver the page fault to userland, check inside PT lock */ 31276b251fc9SAndrea Arcangeli if (userfaultfd_missing(vma)) { 312882b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3129f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 313009cbfeafSKirill A. Shutemov put_page(page); 313182b0f8c3SJan Kara return handle_userfault(vmf, VM_UFFD_MISSING); 31326b251fc9SAndrea Arcangeli } 31336b251fc9SAndrea Arcangeli 3134bae473a4SKirill A. Shutemov inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); 313582b0f8c3SJan Kara page_add_new_anon_rmap(page, vma, vmf->address, false); 3136f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, false); 313700501b53SJohannes Weiner lru_cache_add_active_or_unevictable(page, vma); 3138a13ea5b7SHugh Dickins setpte: 313982b0f8c3SJan Kara set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry); 31401da177e4SLinus Torvalds 31411da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 314282b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 314365500d23SHugh Dickins unlock: 314482b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 31456b31d595SMichal Hocko return ret; 31468f4e2101SHugh Dickins release: 3147f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 314809cbfeafSKirill A. Shutemov put_page(page); 31498f4e2101SHugh Dickins goto unlock; 31508a9f3ccdSBalbir Singh oom_free_page: 315109cbfeafSKirill A. Shutemov put_page(page); 315265500d23SHugh Dickins oom: 31531da177e4SLinus Torvalds return VM_FAULT_OOM; 31541da177e4SLinus Torvalds } 31551da177e4SLinus Torvalds 31569a95f3cfSPaul Cassella /* 31579a95f3cfSPaul Cassella * The mmap_sem must have been held on entry, and may have been 31589a95f3cfSPaul Cassella * released depending on flags and vma->vm_ops->fault() return value. 31599a95f3cfSPaul Cassella * See filemap_fault() and __lock_page_retry(). 31609a95f3cfSPaul Cassella */ 31612b740303SSouptick Joarder static vm_fault_t __do_fault(struct vm_fault *vmf) 31627eae74afSKirill A. Shutemov { 316382b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 31642b740303SSouptick Joarder vm_fault_t ret; 31657eae74afSKirill A. Shutemov 316663f3655fSMichal Hocko /* 316763f3655fSMichal Hocko * Preallocate pte before we take page_lock because this might lead to 316863f3655fSMichal Hocko * deadlocks for memcg reclaim which waits for pages under writeback: 316963f3655fSMichal Hocko * lock_page(A) 317063f3655fSMichal Hocko * SetPageWriteback(A) 317163f3655fSMichal Hocko * unlock_page(A) 317263f3655fSMichal Hocko * lock_page(B) 317363f3655fSMichal Hocko * lock_page(B) 317463f3655fSMichal Hocko * pte_alloc_pne 317563f3655fSMichal Hocko * shrink_page_list 317663f3655fSMichal Hocko * wait_on_page_writeback(A) 317763f3655fSMichal Hocko * SetPageWriteback(B) 317863f3655fSMichal Hocko * unlock_page(B) 317963f3655fSMichal Hocko * # flush A, B to clear the writeback 318063f3655fSMichal Hocko */ 318163f3655fSMichal Hocko if (pmd_none(*vmf->pmd) && !vmf->prealloc_pte) { 318263f3655fSMichal Hocko vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm); 318363f3655fSMichal Hocko if (!vmf->prealloc_pte) 318463f3655fSMichal Hocko return VM_FAULT_OOM; 318563f3655fSMichal Hocko smp_wmb(); /* See comment in __pte_alloc() */ 318663f3655fSMichal Hocko } 318763f3655fSMichal Hocko 318811bac800SDave Jiang ret = vma->vm_ops->fault(vmf); 31893917048dSJan Kara if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY | 3190b1aa812bSJan Kara VM_FAULT_DONE_COW))) 31917eae74afSKirill A. Shutemov return ret; 31927eae74afSKirill A. Shutemov 3193667240e0SJan Kara if (unlikely(PageHWPoison(vmf->page))) { 31947eae74afSKirill A. Shutemov if (ret & VM_FAULT_LOCKED) 3195667240e0SJan Kara unlock_page(vmf->page); 3196667240e0SJan Kara put_page(vmf->page); 3197936ca80dSJan Kara vmf->page = NULL; 31987eae74afSKirill A. Shutemov return VM_FAULT_HWPOISON; 31997eae74afSKirill A. Shutemov } 32007eae74afSKirill A. Shutemov 32017eae74afSKirill A. Shutemov if (unlikely(!(ret & VM_FAULT_LOCKED))) 3202667240e0SJan Kara lock_page(vmf->page); 32037eae74afSKirill A. Shutemov else 3204667240e0SJan Kara VM_BUG_ON_PAGE(!PageLocked(vmf->page), vmf->page); 32057eae74afSKirill A. Shutemov 32067eae74afSKirill A. Shutemov return ret; 32077eae74afSKirill A. Shutemov } 32087eae74afSKirill A. Shutemov 3209d0f0931dSRoss Zwisler /* 3210d0f0931dSRoss Zwisler * The ordering of these checks is important for pmds with _PAGE_DEVMAP set. 3211d0f0931dSRoss Zwisler * If we check pmd_trans_unstable() first we will trip the bad_pmd() check 3212d0f0931dSRoss Zwisler * inside of pmd_none_or_trans_huge_or_clear_bad(). This will end up correctly 3213d0f0931dSRoss Zwisler * returning 1 but not before it spams dmesg with the pmd_clear_bad() output. 3214d0f0931dSRoss Zwisler */ 3215d0f0931dSRoss Zwisler static int pmd_devmap_trans_unstable(pmd_t *pmd) 3216d0f0931dSRoss Zwisler { 3217d0f0931dSRoss Zwisler return pmd_devmap(*pmd) || pmd_trans_unstable(pmd); 3218d0f0931dSRoss Zwisler } 3219d0f0931dSRoss Zwisler 32202b740303SSouptick Joarder static vm_fault_t pte_alloc_one_map(struct vm_fault *vmf) 32217267ec00SKirill A. Shutemov { 322282b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 32237267ec00SKirill A. Shutemov 322482b0f8c3SJan Kara if (!pmd_none(*vmf->pmd)) 32257267ec00SKirill A. Shutemov goto map_pte; 322682b0f8c3SJan Kara if (vmf->prealloc_pte) { 322782b0f8c3SJan Kara vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); 322882b0f8c3SJan Kara if (unlikely(!pmd_none(*vmf->pmd))) { 322982b0f8c3SJan Kara spin_unlock(vmf->ptl); 32307267ec00SKirill A. Shutemov goto map_pte; 32317267ec00SKirill A. Shutemov } 32327267ec00SKirill A. Shutemov 3233c4812909SKirill A. Shutemov mm_inc_nr_ptes(vma->vm_mm); 323482b0f8c3SJan Kara pmd_populate(vma->vm_mm, vmf->pmd, vmf->prealloc_pte); 323582b0f8c3SJan Kara spin_unlock(vmf->ptl); 32367f2b6ce8STobin C Harding vmf->prealloc_pte = NULL; 32374cf58924SJoel Fernandes (Google) } else if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd))) { 32387267ec00SKirill A. Shutemov return VM_FAULT_OOM; 32397267ec00SKirill A. Shutemov } 32407267ec00SKirill A. Shutemov map_pte: 32417267ec00SKirill A. Shutemov /* 32427267ec00SKirill A. Shutemov * If a huge pmd materialized under us just retry later. Use 3243d0f0931dSRoss Zwisler * pmd_trans_unstable() via pmd_devmap_trans_unstable() instead of 3244d0f0931dSRoss Zwisler * pmd_trans_huge() to ensure the pmd didn't become pmd_trans_huge 3245d0f0931dSRoss Zwisler * under us and then back to pmd_none, as a result of MADV_DONTNEED 3246d0f0931dSRoss Zwisler * running immediately after a huge pmd fault in a different thread of 3247d0f0931dSRoss Zwisler * this mm, in turn leading to a misleading pmd_trans_huge() retval. 3248d0f0931dSRoss Zwisler * All we have to ensure is that it is a regular pmd that we can walk 3249d0f0931dSRoss Zwisler * with pte_offset_map() and we can do that through an atomic read in 3250d0f0931dSRoss Zwisler * C, which is what pmd_trans_unstable() provides. 32517267ec00SKirill A. Shutemov */ 3252d0f0931dSRoss Zwisler if (pmd_devmap_trans_unstable(vmf->pmd)) 32537267ec00SKirill A. Shutemov return VM_FAULT_NOPAGE; 32547267ec00SKirill A. Shutemov 3255d0f0931dSRoss Zwisler /* 3256d0f0931dSRoss Zwisler * At this point we know that our vmf->pmd points to a page of ptes 3257d0f0931dSRoss Zwisler * and it cannot become pmd_none(), pmd_devmap() or pmd_trans_huge() 3258d0f0931dSRoss Zwisler * for the duration of the fault. If a racing MADV_DONTNEED runs and 3259d0f0931dSRoss Zwisler * we zap the ptes pointed to by our vmf->pmd, the vmf->ptl will still 3260d0f0931dSRoss Zwisler * be valid and we will re-check to make sure the vmf->pte isn't 3261d0f0931dSRoss Zwisler * pte_none() under vmf->ptl protection when we return to 3262d0f0931dSRoss Zwisler * alloc_set_pte(). 3263d0f0931dSRoss Zwisler */ 326482b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 326582b0f8c3SJan Kara &vmf->ptl); 32667267ec00SKirill A. Shutemov return 0; 32677267ec00SKirill A. Shutemov } 32687267ec00SKirill A. Shutemov 3269e496cf3dSKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 327082b0f8c3SJan Kara static void deposit_prealloc_pte(struct vm_fault *vmf) 3271953c66c2SAneesh Kumar K.V { 327282b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3273953c66c2SAneesh Kumar K.V 327482b0f8c3SJan Kara pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, vmf->prealloc_pte); 3275953c66c2SAneesh Kumar K.V /* 3276953c66c2SAneesh Kumar K.V * We are going to consume the prealloc table, 3277953c66c2SAneesh Kumar K.V * count that as nr_ptes. 3278953c66c2SAneesh Kumar K.V */ 3279c4812909SKirill A. Shutemov mm_inc_nr_ptes(vma->vm_mm); 32807f2b6ce8STobin C Harding vmf->prealloc_pte = NULL; 3281953c66c2SAneesh Kumar K.V } 3282953c66c2SAneesh Kumar K.V 32832b740303SSouptick Joarder static vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page) 328410102459SKirill A. Shutemov { 328582b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 328682b0f8c3SJan Kara bool write = vmf->flags & FAULT_FLAG_WRITE; 328782b0f8c3SJan Kara unsigned long haddr = vmf->address & HPAGE_PMD_MASK; 328810102459SKirill A. Shutemov pmd_t entry; 32892b740303SSouptick Joarder int i; 32902b740303SSouptick Joarder vm_fault_t ret; 329110102459SKirill A. Shutemov 329210102459SKirill A. Shutemov if (!transhuge_vma_suitable(vma, haddr)) 329310102459SKirill A. Shutemov return VM_FAULT_FALLBACK; 329410102459SKirill A. Shutemov 329510102459SKirill A. Shutemov ret = VM_FAULT_FALLBACK; 329610102459SKirill A. Shutemov page = compound_head(page); 329710102459SKirill A. Shutemov 3298953c66c2SAneesh Kumar K.V /* 3299953c66c2SAneesh Kumar K.V * Archs like ppc64 need additonal space to store information 3300953c66c2SAneesh Kumar K.V * related to pte entry. Use the preallocated table for that. 3301953c66c2SAneesh Kumar K.V */ 330282b0f8c3SJan Kara if (arch_needs_pgtable_deposit() && !vmf->prealloc_pte) { 33034cf58924SJoel Fernandes (Google) vmf->prealloc_pte = pte_alloc_one(vma->vm_mm); 330482b0f8c3SJan Kara if (!vmf->prealloc_pte) 3305953c66c2SAneesh Kumar K.V return VM_FAULT_OOM; 3306953c66c2SAneesh Kumar K.V smp_wmb(); /* See comment in __pte_alloc() */ 3307953c66c2SAneesh Kumar K.V } 3308953c66c2SAneesh Kumar K.V 330982b0f8c3SJan Kara vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); 331082b0f8c3SJan Kara if (unlikely(!pmd_none(*vmf->pmd))) 331110102459SKirill A. Shutemov goto out; 331210102459SKirill A. Shutemov 331310102459SKirill A. Shutemov for (i = 0; i < HPAGE_PMD_NR; i++) 331410102459SKirill A. Shutemov flush_icache_page(vma, page + i); 331510102459SKirill A. Shutemov 331610102459SKirill A. Shutemov entry = mk_huge_pmd(page, vma->vm_page_prot); 331710102459SKirill A. Shutemov if (write) 3318f55e1014SLinus Torvalds entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); 331910102459SKirill A. Shutemov 3320fadae295SYang Shi add_mm_counter(vma->vm_mm, mm_counter_file(page), HPAGE_PMD_NR); 332110102459SKirill A. Shutemov page_add_file_rmap(page, true); 3322953c66c2SAneesh Kumar K.V /* 3323953c66c2SAneesh Kumar K.V * deposit and withdraw with pmd lock held 3324953c66c2SAneesh Kumar K.V */ 3325953c66c2SAneesh Kumar K.V if (arch_needs_pgtable_deposit()) 332682b0f8c3SJan Kara deposit_prealloc_pte(vmf); 332710102459SKirill A. Shutemov 332882b0f8c3SJan Kara set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry); 332910102459SKirill A. Shutemov 333082b0f8c3SJan Kara update_mmu_cache_pmd(vma, haddr, vmf->pmd); 333110102459SKirill A. Shutemov 333210102459SKirill A. Shutemov /* fault is handled */ 333310102459SKirill A. Shutemov ret = 0; 333495ecedcdSKirill A. Shutemov count_vm_event(THP_FILE_MAPPED); 333510102459SKirill A. Shutemov out: 333682b0f8c3SJan Kara spin_unlock(vmf->ptl); 333710102459SKirill A. Shutemov return ret; 333810102459SKirill A. Shutemov } 333910102459SKirill A. Shutemov #else 33402b740303SSouptick Joarder static vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page) 334110102459SKirill A. Shutemov { 334210102459SKirill A. Shutemov BUILD_BUG(); 334310102459SKirill A. Shutemov return 0; 334410102459SKirill A. Shutemov } 334510102459SKirill A. Shutemov #endif 334610102459SKirill A. Shutemov 33478c6e50b0SKirill A. Shutemov /** 33487267ec00SKirill A. Shutemov * alloc_set_pte - setup new PTE entry for given page and add reverse page 33497267ec00SKirill A. Shutemov * mapping. If needed, the fucntion allocates page table or use pre-allocated. 33508c6e50b0SKirill A. Shutemov * 335182b0f8c3SJan Kara * @vmf: fault environment 33527267ec00SKirill A. Shutemov * @memcg: memcg to charge page (only for private mappings) 33538c6e50b0SKirill A. Shutemov * @page: page to map 33548c6e50b0SKirill A. Shutemov * 335582b0f8c3SJan Kara * Caller must take care of unlocking vmf->ptl, if vmf->pte is non-NULL on 335682b0f8c3SJan Kara * return. 33578c6e50b0SKirill A. Shutemov * 33588c6e50b0SKirill A. Shutemov * Target users are page handler itself and implementations of 33598c6e50b0SKirill A. Shutemov * vm_ops->map_pages. 3360a862f68aSMike Rapoport * 3361a862f68aSMike Rapoport * Return: %0 on success, %VM_FAULT_ code in case of error. 33628c6e50b0SKirill A. Shutemov */ 33632b740303SSouptick Joarder vm_fault_t alloc_set_pte(struct vm_fault *vmf, struct mem_cgroup *memcg, 33647267ec00SKirill A. Shutemov struct page *page) 33653bb97794SKirill A. Shutemov { 336682b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 336782b0f8c3SJan Kara bool write = vmf->flags & FAULT_FLAG_WRITE; 33683bb97794SKirill A. Shutemov pte_t entry; 33692b740303SSouptick Joarder vm_fault_t ret; 337010102459SKirill A. Shutemov 337182b0f8c3SJan Kara if (pmd_none(*vmf->pmd) && PageTransCompound(page) && 3372e496cf3dSKirill A. Shutemov IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) { 337310102459SKirill A. Shutemov /* THP on COW? */ 337410102459SKirill A. Shutemov VM_BUG_ON_PAGE(memcg, page); 337510102459SKirill A. Shutemov 337682b0f8c3SJan Kara ret = do_set_pmd(vmf, page); 337710102459SKirill A. Shutemov if (ret != VM_FAULT_FALLBACK) 3378b0b9b3dfSHugh Dickins return ret; 337910102459SKirill A. Shutemov } 33803bb97794SKirill A. Shutemov 338182b0f8c3SJan Kara if (!vmf->pte) { 338282b0f8c3SJan Kara ret = pte_alloc_one_map(vmf); 33837267ec00SKirill A. Shutemov if (ret) 3384b0b9b3dfSHugh Dickins return ret; 33857267ec00SKirill A. Shutemov } 33867267ec00SKirill A. Shutemov 33877267ec00SKirill A. Shutemov /* Re-check under ptl */ 3388b0b9b3dfSHugh Dickins if (unlikely(!pte_none(*vmf->pte))) 3389b0b9b3dfSHugh Dickins return VM_FAULT_NOPAGE; 33907267ec00SKirill A. Shutemov 33913bb97794SKirill A. Shutemov flush_icache_page(vma, page); 33923bb97794SKirill A. Shutemov entry = mk_pte(page, vma->vm_page_prot); 33933bb97794SKirill A. Shutemov if (write) 33943bb97794SKirill A. Shutemov entry = maybe_mkwrite(pte_mkdirty(entry), vma); 3395bae473a4SKirill A. Shutemov /* copy-on-write page */ 3396bae473a4SKirill A. Shutemov if (write && !(vma->vm_flags & VM_SHARED)) { 33973bb97794SKirill A. Shutemov inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); 339882b0f8c3SJan Kara page_add_new_anon_rmap(page, vma, vmf->address, false); 33997267ec00SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, false); 34007267ec00SKirill A. Shutemov lru_cache_add_active_or_unevictable(page, vma); 34013bb97794SKirill A. Shutemov } else { 3402eca56ff9SJerome Marchand inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page)); 3403dd78feddSKirill A. Shutemov page_add_file_rmap(page, false); 34043bb97794SKirill A. Shutemov } 340582b0f8c3SJan Kara set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry); 34063bb97794SKirill A. Shutemov 34073bb97794SKirill A. Shutemov /* no need to invalidate: a not-present page won't be cached */ 340882b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 34097267ec00SKirill A. Shutemov 3410b0b9b3dfSHugh Dickins return 0; 34113bb97794SKirill A. Shutemov } 34123bb97794SKirill A. Shutemov 34139118c0cbSJan Kara 34149118c0cbSJan Kara /** 34159118c0cbSJan Kara * finish_fault - finish page fault once we have prepared the page to fault 34169118c0cbSJan Kara * 34179118c0cbSJan Kara * @vmf: structure describing the fault 34189118c0cbSJan Kara * 34199118c0cbSJan Kara * This function handles all that is needed to finish a page fault once the 34209118c0cbSJan Kara * page to fault in is prepared. It handles locking of PTEs, inserts PTE for 34219118c0cbSJan Kara * given page, adds reverse page mapping, handles memcg charges and LRU 3422a862f68aSMike Rapoport * addition. 34239118c0cbSJan Kara * 34249118c0cbSJan Kara * The function expects the page to be locked and on success it consumes a 34259118c0cbSJan Kara * reference of a page being mapped (for the PTE which maps it). 3426a862f68aSMike Rapoport * 3427a862f68aSMike Rapoport * Return: %0 on success, %VM_FAULT_ code in case of error. 34289118c0cbSJan Kara */ 34292b740303SSouptick Joarder vm_fault_t finish_fault(struct vm_fault *vmf) 34309118c0cbSJan Kara { 34319118c0cbSJan Kara struct page *page; 34322b740303SSouptick Joarder vm_fault_t ret = 0; 34339118c0cbSJan Kara 34349118c0cbSJan Kara /* Did we COW the page? */ 34359118c0cbSJan Kara if ((vmf->flags & FAULT_FLAG_WRITE) && 34369118c0cbSJan Kara !(vmf->vma->vm_flags & VM_SHARED)) 34379118c0cbSJan Kara page = vmf->cow_page; 34389118c0cbSJan Kara else 34399118c0cbSJan Kara page = vmf->page; 34406b31d595SMichal Hocko 34416b31d595SMichal Hocko /* 34426b31d595SMichal Hocko * check even for read faults because we might have lost our CoWed 34436b31d595SMichal Hocko * page 34446b31d595SMichal Hocko */ 34456b31d595SMichal Hocko if (!(vmf->vma->vm_flags & VM_SHARED)) 34466b31d595SMichal Hocko ret = check_stable_address_space(vmf->vma->vm_mm); 34476b31d595SMichal Hocko if (!ret) 34489118c0cbSJan Kara ret = alloc_set_pte(vmf, vmf->memcg, page); 34499118c0cbSJan Kara if (vmf->pte) 34509118c0cbSJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 34519118c0cbSJan Kara return ret; 34529118c0cbSJan Kara } 34539118c0cbSJan Kara 34543a91053aSKirill A. Shutemov static unsigned long fault_around_bytes __read_mostly = 34553a91053aSKirill A. Shutemov rounddown_pow_of_two(65536); 3456a9b0f861SKirill A. Shutemov 34571592eef0SKirill A. Shutemov #ifdef CONFIG_DEBUG_FS 3458a9b0f861SKirill A. Shutemov static int fault_around_bytes_get(void *data, u64 *val) 34591592eef0SKirill A. Shutemov { 3460a9b0f861SKirill A. Shutemov *val = fault_around_bytes; 34611592eef0SKirill A. Shutemov return 0; 34621592eef0SKirill A. Shutemov } 34631592eef0SKirill A. Shutemov 3464b4903d6eSAndrey Ryabinin /* 3465da391d64SWilliam Kucharski * fault_around_bytes must be rounded down to the nearest page order as it's 3466da391d64SWilliam Kucharski * what do_fault_around() expects to see. 3467b4903d6eSAndrey Ryabinin */ 3468a9b0f861SKirill A. Shutemov static int fault_around_bytes_set(void *data, u64 val) 34691592eef0SKirill A. Shutemov { 3470a9b0f861SKirill A. Shutemov if (val / PAGE_SIZE > PTRS_PER_PTE) 34711592eef0SKirill A. Shutemov return -EINVAL; 3472b4903d6eSAndrey Ryabinin if (val > PAGE_SIZE) 3473b4903d6eSAndrey Ryabinin fault_around_bytes = rounddown_pow_of_two(val); 3474b4903d6eSAndrey Ryabinin else 3475b4903d6eSAndrey Ryabinin fault_around_bytes = PAGE_SIZE; /* rounddown_pow_of_two(0) is undefined */ 34761592eef0SKirill A. Shutemov return 0; 34771592eef0SKirill A. Shutemov } 34780a1345f8SYevgen Pronenko DEFINE_DEBUGFS_ATTRIBUTE(fault_around_bytes_fops, 3479a9b0f861SKirill A. Shutemov fault_around_bytes_get, fault_around_bytes_set, "%llu\n"); 34801592eef0SKirill A. Shutemov 34811592eef0SKirill A. Shutemov static int __init fault_around_debugfs(void) 34821592eef0SKirill A. Shutemov { 3483d9f7979cSGreg Kroah-Hartman debugfs_create_file_unsafe("fault_around_bytes", 0644, NULL, NULL, 3484a9b0f861SKirill A. Shutemov &fault_around_bytes_fops); 34851592eef0SKirill A. Shutemov return 0; 34861592eef0SKirill A. Shutemov } 34871592eef0SKirill A. Shutemov late_initcall(fault_around_debugfs); 34881592eef0SKirill A. Shutemov #endif 34898c6e50b0SKirill A. Shutemov 34901fdb412bSKirill A. Shutemov /* 34911fdb412bSKirill A. Shutemov * do_fault_around() tries to map few pages around the fault address. The hope 34921fdb412bSKirill A. Shutemov * is that the pages will be needed soon and this will lower the number of 34931fdb412bSKirill A. Shutemov * faults to handle. 34941fdb412bSKirill A. Shutemov * 34951fdb412bSKirill A. Shutemov * It uses vm_ops->map_pages() to map the pages, which skips the page if it's 34961fdb412bSKirill A. Shutemov * not ready to be mapped: not up-to-date, locked, etc. 34971fdb412bSKirill A. Shutemov * 34981fdb412bSKirill A. Shutemov * This function is called with the page table lock taken. In the split ptlock 34991fdb412bSKirill A. Shutemov * case the page table lock only protects only those entries which belong to 35001fdb412bSKirill A. Shutemov * the page table corresponding to the fault address. 35011fdb412bSKirill A. Shutemov * 35021fdb412bSKirill A. Shutemov * This function doesn't cross the VMA boundaries, in order to call map_pages() 35031fdb412bSKirill A. Shutemov * only once. 35041fdb412bSKirill A. Shutemov * 3505da391d64SWilliam Kucharski * fault_around_bytes defines how many bytes we'll try to map. 3506da391d64SWilliam Kucharski * do_fault_around() expects it to be set to a power of two less than or equal 3507da391d64SWilliam Kucharski * to PTRS_PER_PTE. 35081fdb412bSKirill A. Shutemov * 3509da391d64SWilliam Kucharski * The virtual address of the area that we map is naturally aligned to 3510da391d64SWilliam Kucharski * fault_around_bytes rounded down to the machine page size 3511da391d64SWilliam Kucharski * (and therefore to page order). This way it's easier to guarantee 3512da391d64SWilliam Kucharski * that we don't cross page table boundaries. 35131fdb412bSKirill A. Shutemov */ 35142b740303SSouptick Joarder static vm_fault_t do_fault_around(struct vm_fault *vmf) 35158c6e50b0SKirill A. Shutemov { 351682b0f8c3SJan Kara unsigned long address = vmf->address, nr_pages, mask; 35170721ec8bSJan Kara pgoff_t start_pgoff = vmf->pgoff; 3518bae473a4SKirill A. Shutemov pgoff_t end_pgoff; 35192b740303SSouptick Joarder int off; 35202b740303SSouptick Joarder vm_fault_t ret = 0; 35218c6e50b0SKirill A. Shutemov 35224db0c3c2SJason Low nr_pages = READ_ONCE(fault_around_bytes) >> PAGE_SHIFT; 3523aecd6f44SKirill A. Shutemov mask = ~(nr_pages * PAGE_SIZE - 1) & PAGE_MASK; 3524aecd6f44SKirill A. Shutemov 352582b0f8c3SJan Kara vmf->address = max(address & mask, vmf->vma->vm_start); 352682b0f8c3SJan Kara off = ((address - vmf->address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1); 3527bae473a4SKirill A. Shutemov start_pgoff -= off; 35288c6e50b0SKirill A. Shutemov 35298c6e50b0SKirill A. Shutemov /* 3530da391d64SWilliam Kucharski * end_pgoff is either the end of the page table, the end of 3531da391d64SWilliam Kucharski * the vma or nr_pages from start_pgoff, depending what is nearest. 35328c6e50b0SKirill A. Shutemov */ 3533bae473a4SKirill A. Shutemov end_pgoff = start_pgoff - 353482b0f8c3SJan Kara ((vmf->address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) + 35358c6e50b0SKirill A. Shutemov PTRS_PER_PTE - 1; 353682b0f8c3SJan Kara end_pgoff = min3(end_pgoff, vma_pages(vmf->vma) + vmf->vma->vm_pgoff - 1, 3537bae473a4SKirill A. Shutemov start_pgoff + nr_pages - 1); 35388c6e50b0SKirill A. Shutemov 353982b0f8c3SJan Kara if (pmd_none(*vmf->pmd)) { 35404cf58924SJoel Fernandes (Google) vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm); 354182b0f8c3SJan Kara if (!vmf->prealloc_pte) 3542c5f88bd2SVegard Nossum goto out; 35437267ec00SKirill A. Shutemov smp_wmb(); /* See comment in __pte_alloc() */ 35448c6e50b0SKirill A. Shutemov } 35458c6e50b0SKirill A. Shutemov 354682b0f8c3SJan Kara vmf->vma->vm_ops->map_pages(vmf, start_pgoff, end_pgoff); 35477267ec00SKirill A. Shutemov 35487267ec00SKirill A. Shutemov /* Huge page is mapped? Page fault is solved */ 354982b0f8c3SJan Kara if (pmd_trans_huge(*vmf->pmd)) { 35507267ec00SKirill A. Shutemov ret = VM_FAULT_NOPAGE; 35517267ec00SKirill A. Shutemov goto out; 35528c6e50b0SKirill A. Shutemov } 35538c6e50b0SKirill A. Shutemov 35547267ec00SKirill A. Shutemov /* ->map_pages() haven't done anything useful. Cold page cache? */ 355582b0f8c3SJan Kara if (!vmf->pte) 35567267ec00SKirill A. Shutemov goto out; 35577267ec00SKirill A. Shutemov 35587267ec00SKirill A. Shutemov /* check if the page fault is solved */ 355982b0f8c3SJan Kara vmf->pte -= (vmf->address >> PAGE_SHIFT) - (address >> PAGE_SHIFT); 356082b0f8c3SJan Kara if (!pte_none(*vmf->pte)) 35617267ec00SKirill A. Shutemov ret = VM_FAULT_NOPAGE; 356282b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 35637267ec00SKirill A. Shutemov out: 356482b0f8c3SJan Kara vmf->address = address; 356582b0f8c3SJan Kara vmf->pte = NULL; 35667267ec00SKirill A. Shutemov return ret; 35677267ec00SKirill A. Shutemov } 35687267ec00SKirill A. Shutemov 35692b740303SSouptick Joarder static vm_fault_t do_read_fault(struct vm_fault *vmf) 3570e655fb29SKirill A. Shutemov { 357182b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 35722b740303SSouptick Joarder vm_fault_t ret = 0; 35738c6e50b0SKirill A. Shutemov 35748c6e50b0SKirill A. Shutemov /* 35758c6e50b0SKirill A. Shutemov * Let's call ->map_pages() first and use ->fault() as fallback 35768c6e50b0SKirill A. Shutemov * if page by the offset is not ready to be mapped (cold cache or 35778c6e50b0SKirill A. Shutemov * something). 35788c6e50b0SKirill A. Shutemov */ 35799b4bdd2fSKirill A. Shutemov if (vma->vm_ops->map_pages && fault_around_bytes >> PAGE_SHIFT > 1) { 35800721ec8bSJan Kara ret = do_fault_around(vmf); 35817267ec00SKirill A. Shutemov if (ret) 35827267ec00SKirill A. Shutemov return ret; 35838c6e50b0SKirill A. Shutemov } 3584e655fb29SKirill A. Shutemov 3585936ca80dSJan Kara ret = __do_fault(vmf); 3586e655fb29SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3587e655fb29SKirill A. Shutemov return ret; 3588e655fb29SKirill A. Shutemov 35899118c0cbSJan Kara ret |= finish_fault(vmf); 3590936ca80dSJan Kara unlock_page(vmf->page); 35917267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3592936ca80dSJan Kara put_page(vmf->page); 3593e655fb29SKirill A. Shutemov return ret; 3594e655fb29SKirill A. Shutemov } 3595e655fb29SKirill A. Shutemov 35962b740303SSouptick Joarder static vm_fault_t do_cow_fault(struct vm_fault *vmf) 3597ec47c3b9SKirill A. Shutemov { 359882b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 35992b740303SSouptick Joarder vm_fault_t ret; 3600ec47c3b9SKirill A. Shutemov 3601ec47c3b9SKirill A. Shutemov if (unlikely(anon_vma_prepare(vma))) 3602ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 3603ec47c3b9SKirill A. Shutemov 3604936ca80dSJan Kara vmf->cow_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vmf->address); 3605936ca80dSJan Kara if (!vmf->cow_page) 3606ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 3607ec47c3b9SKirill A. Shutemov 36082cf85583STejun Heo if (mem_cgroup_try_charge_delay(vmf->cow_page, vma->vm_mm, GFP_KERNEL, 36093917048dSJan Kara &vmf->memcg, false)) { 3610936ca80dSJan Kara put_page(vmf->cow_page); 3611ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 3612ec47c3b9SKirill A. Shutemov } 3613ec47c3b9SKirill A. Shutemov 3614936ca80dSJan Kara ret = __do_fault(vmf); 3615ec47c3b9SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3616ec47c3b9SKirill A. Shutemov goto uncharge_out; 36173917048dSJan Kara if (ret & VM_FAULT_DONE_COW) 36183917048dSJan Kara return ret; 3619ec47c3b9SKirill A. Shutemov 3620936ca80dSJan Kara copy_user_highpage(vmf->cow_page, vmf->page, vmf->address, vma); 3621936ca80dSJan Kara __SetPageUptodate(vmf->cow_page); 3622ec47c3b9SKirill A. Shutemov 36239118c0cbSJan Kara ret |= finish_fault(vmf); 3624936ca80dSJan Kara unlock_page(vmf->page); 3625936ca80dSJan Kara put_page(vmf->page); 36267267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 36277267ec00SKirill A. Shutemov goto uncharge_out; 3628ec47c3b9SKirill A. Shutemov return ret; 3629ec47c3b9SKirill A. Shutemov uncharge_out: 36303917048dSJan Kara mem_cgroup_cancel_charge(vmf->cow_page, vmf->memcg, false); 3631936ca80dSJan Kara put_page(vmf->cow_page); 3632ec47c3b9SKirill A. Shutemov return ret; 3633ec47c3b9SKirill A. Shutemov } 3634ec47c3b9SKirill A. Shutemov 36352b740303SSouptick Joarder static vm_fault_t do_shared_fault(struct vm_fault *vmf) 36361da177e4SLinus Torvalds { 363782b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 36382b740303SSouptick Joarder vm_fault_t ret, tmp; 36391d65f86dSKAMEZAWA Hiroyuki 3640936ca80dSJan Kara ret = __do_fault(vmf); 36417eae74afSKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3642f0c6d4d2SKirill A. Shutemov return ret; 36431da177e4SLinus Torvalds 36441da177e4SLinus Torvalds /* 3645f0c6d4d2SKirill A. Shutemov * Check if the backing address space wants to know that the page is 3646f0c6d4d2SKirill A. Shutemov * about to become writable 36471da177e4SLinus Torvalds */ 3648fb09a464SKirill A. Shutemov if (vma->vm_ops->page_mkwrite) { 3649936ca80dSJan Kara unlock_page(vmf->page); 365038b8cb7fSJan Kara tmp = do_page_mkwrite(vmf); 3651fb09a464SKirill A. Shutemov if (unlikely(!tmp || 3652fb09a464SKirill A. Shutemov (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { 3653936ca80dSJan Kara put_page(vmf->page); 3654f0c6d4d2SKirill A. Shutemov return tmp; 365569676147SMark Fasheh } 3656d0217ac0SNick Piggin } 3657fb09a464SKirill A. Shutemov 36589118c0cbSJan Kara ret |= finish_fault(vmf); 36597267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | 36607267ec00SKirill A. Shutemov VM_FAULT_RETRY))) { 3661936ca80dSJan Kara unlock_page(vmf->page); 3662936ca80dSJan Kara put_page(vmf->page); 3663f0c6d4d2SKirill A. Shutemov return ret; 36649637a5efSDavid Howells } 3665d00806b1SNick Piggin 366689b15332SJohannes Weiner ret |= fault_dirty_shared_page(vmf); 3667b827e496SNick Piggin return ret; 366854cb8821SNick Piggin } 3669d00806b1SNick Piggin 36709a95f3cfSPaul Cassella /* 36719a95f3cfSPaul Cassella * We enter with non-exclusive mmap_sem (to exclude vma changes, 36729a95f3cfSPaul Cassella * but allow concurrent faults). 36739a95f3cfSPaul Cassella * The mmap_sem may have been released depending on flags and our 36749a95f3cfSPaul Cassella * return value. See filemap_fault() and __lock_page_or_retry(). 3675fc8efd2dSJan Stancek * If mmap_sem is released, vma may become invalid (for example 3676fc8efd2dSJan Stancek * by other thread calling munmap()). 36779a95f3cfSPaul Cassella */ 36782b740303SSouptick Joarder static vm_fault_t do_fault(struct vm_fault *vmf) 367954cb8821SNick Piggin { 368082b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3681fc8efd2dSJan Stancek struct mm_struct *vm_mm = vma->vm_mm; 36822b740303SSouptick Joarder vm_fault_t ret; 368354cb8821SNick Piggin 3684ff09d7ecSAneesh Kumar K.V /* 3685ff09d7ecSAneesh Kumar K.V * The VMA was not fully populated on mmap() or missing VM_DONTEXPAND 3686ff09d7ecSAneesh Kumar K.V */ 3687ff09d7ecSAneesh Kumar K.V if (!vma->vm_ops->fault) { 3688ff09d7ecSAneesh Kumar K.V /* 3689ff09d7ecSAneesh Kumar K.V * If we find a migration pmd entry or a none pmd entry, which 3690ff09d7ecSAneesh Kumar K.V * should never happen, return SIGBUS 3691ff09d7ecSAneesh Kumar K.V */ 3692ff09d7ecSAneesh Kumar K.V if (unlikely(!pmd_present(*vmf->pmd))) 3693b0b9b3dfSHugh Dickins ret = VM_FAULT_SIGBUS; 3694ff09d7ecSAneesh Kumar K.V else { 3695ff09d7ecSAneesh Kumar K.V vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, 3696ff09d7ecSAneesh Kumar K.V vmf->pmd, 3697ff09d7ecSAneesh Kumar K.V vmf->address, 3698ff09d7ecSAneesh Kumar K.V &vmf->ptl); 3699ff09d7ecSAneesh Kumar K.V /* 3700ff09d7ecSAneesh Kumar K.V * Make sure this is not a temporary clearing of pte 3701ff09d7ecSAneesh Kumar K.V * by holding ptl and checking again. A R/M/W update 3702ff09d7ecSAneesh Kumar K.V * of pte involves: take ptl, clearing the pte so that 3703ff09d7ecSAneesh Kumar K.V * we don't have concurrent modification by hardware 3704ff09d7ecSAneesh Kumar K.V * followed by an update. 3705ff09d7ecSAneesh Kumar K.V */ 3706ff09d7ecSAneesh Kumar K.V if (unlikely(pte_none(*vmf->pte))) 3707ff09d7ecSAneesh Kumar K.V ret = VM_FAULT_SIGBUS; 3708ff09d7ecSAneesh Kumar K.V else 3709ff09d7ecSAneesh Kumar K.V ret = VM_FAULT_NOPAGE; 3710ff09d7ecSAneesh Kumar K.V 3711ff09d7ecSAneesh Kumar K.V pte_unmap_unlock(vmf->pte, vmf->ptl); 3712ff09d7ecSAneesh Kumar K.V } 3713ff09d7ecSAneesh Kumar K.V } else if (!(vmf->flags & FAULT_FLAG_WRITE)) 3714b0b9b3dfSHugh Dickins ret = do_read_fault(vmf); 3715b0b9b3dfSHugh Dickins else if (!(vma->vm_flags & VM_SHARED)) 3716b0b9b3dfSHugh Dickins ret = do_cow_fault(vmf); 3717b0b9b3dfSHugh Dickins else 3718b0b9b3dfSHugh Dickins ret = do_shared_fault(vmf); 3719b0b9b3dfSHugh Dickins 3720b0b9b3dfSHugh Dickins /* preallocated pagetable is unused: free it */ 3721b0b9b3dfSHugh Dickins if (vmf->prealloc_pte) { 3722fc8efd2dSJan Stancek pte_free(vm_mm, vmf->prealloc_pte); 37237f2b6ce8STobin C Harding vmf->prealloc_pte = NULL; 3724b0b9b3dfSHugh Dickins } 3725b0b9b3dfSHugh Dickins return ret; 372654cb8821SNick Piggin } 372754cb8821SNick Piggin 3728b19a9939SRashika Kheria static int numa_migrate_prep(struct page *page, struct vm_area_struct *vma, 372904bb2f94SRik van Riel unsigned long addr, int page_nid, 373004bb2f94SRik van Riel int *flags) 37319532fec1SMel Gorman { 37329532fec1SMel Gorman get_page(page); 37339532fec1SMel Gorman 37349532fec1SMel Gorman count_vm_numa_event(NUMA_HINT_FAULTS); 373504bb2f94SRik van Riel if (page_nid == numa_node_id()) { 37369532fec1SMel Gorman count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL); 373704bb2f94SRik van Riel *flags |= TNF_FAULT_LOCAL; 373804bb2f94SRik van Riel } 37399532fec1SMel Gorman 37409532fec1SMel Gorman return mpol_misplaced(page, vma, addr); 37419532fec1SMel Gorman } 37429532fec1SMel Gorman 37432b740303SSouptick Joarder static vm_fault_t do_numa_page(struct vm_fault *vmf) 3744d10e63f2SMel Gorman { 374582b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 37464daae3b4SMel Gorman struct page *page = NULL; 374798fa15f3SAnshuman Khandual int page_nid = NUMA_NO_NODE; 374890572890SPeter Zijlstra int last_cpupid; 3749cbee9f88SPeter Zijlstra int target_nid; 3750b8593bfdSMel Gorman bool migrated = false; 375104a86453SAneesh Kumar K.V pte_t pte, old_pte; 3752288bc549SAneesh Kumar K.V bool was_writable = pte_savedwrite(vmf->orig_pte); 37536688cc05SPeter Zijlstra int flags = 0; 3754d10e63f2SMel Gorman 3755d10e63f2SMel Gorman /* 3756d10e63f2SMel Gorman * The "pte" at this point cannot be used safely without 3757d10e63f2SMel Gorman * validation through pte_unmap_same(). It's of NUMA type but 3758d10e63f2SMel Gorman * the pfn may be screwed if the read is non atomic. 3759d10e63f2SMel Gorman */ 376082b0f8c3SJan Kara vmf->ptl = pte_lockptr(vma->vm_mm, vmf->pmd); 376182b0f8c3SJan Kara spin_lock(vmf->ptl); 3762cee216a6SAneesh Kumar K.V if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) { 376382b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 37644daae3b4SMel Gorman goto out; 37654daae3b4SMel Gorman } 37664daae3b4SMel Gorman 3767cee216a6SAneesh Kumar K.V /* 3768cee216a6SAneesh Kumar K.V * Make it present again, Depending on how arch implementes non 3769cee216a6SAneesh Kumar K.V * accessible ptes, some can allow access by kernel mode. 3770cee216a6SAneesh Kumar K.V */ 377104a86453SAneesh Kumar K.V old_pte = ptep_modify_prot_start(vma, vmf->address, vmf->pte); 377204a86453SAneesh Kumar K.V pte = pte_modify(old_pte, vma->vm_page_prot); 37734d942466SMel Gorman pte = pte_mkyoung(pte); 3774b191f9b1SMel Gorman if (was_writable) 3775b191f9b1SMel Gorman pte = pte_mkwrite(pte); 377604a86453SAneesh Kumar K.V ptep_modify_prot_commit(vma, vmf->address, vmf->pte, old_pte, pte); 377782b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 3778d10e63f2SMel Gorman 377982b0f8c3SJan Kara page = vm_normal_page(vma, vmf->address, pte); 3780d10e63f2SMel Gorman if (!page) { 378182b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3782d10e63f2SMel Gorman return 0; 3783d10e63f2SMel Gorman } 3784d10e63f2SMel Gorman 3785e81c4802SKirill A. Shutemov /* TODO: handle PTE-mapped THP */ 3786e81c4802SKirill A. Shutemov if (PageCompound(page)) { 378782b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3788e81c4802SKirill A. Shutemov return 0; 3789e81c4802SKirill A. Shutemov } 3790e81c4802SKirill A. Shutemov 37916688cc05SPeter Zijlstra /* 3792bea66fbdSMel Gorman * Avoid grouping on RO pages in general. RO pages shouldn't hurt as 3793bea66fbdSMel Gorman * much anyway since they can be in shared cache state. This misses 3794bea66fbdSMel Gorman * the case where a mapping is writable but the process never writes 3795bea66fbdSMel Gorman * to it but pte_write gets cleared during protection updates and 3796bea66fbdSMel Gorman * pte_dirty has unpredictable behaviour between PTE scan updates, 3797bea66fbdSMel Gorman * background writeback, dirty balancing and application behaviour. 37986688cc05SPeter Zijlstra */ 3799d59dc7bcSRik van Riel if (!pte_write(pte)) 38006688cc05SPeter Zijlstra flags |= TNF_NO_GROUP; 38016688cc05SPeter Zijlstra 3802dabe1d99SRik van Riel /* 3803dabe1d99SRik van Riel * Flag if the page is shared between multiple address spaces. This 3804dabe1d99SRik van Riel * is later used when determining whether to group tasks together 3805dabe1d99SRik van Riel */ 3806dabe1d99SRik van Riel if (page_mapcount(page) > 1 && (vma->vm_flags & VM_SHARED)) 3807dabe1d99SRik van Riel flags |= TNF_SHARED; 3808dabe1d99SRik van Riel 380990572890SPeter Zijlstra last_cpupid = page_cpupid_last(page); 38108191acbdSMel Gorman page_nid = page_to_nid(page); 381182b0f8c3SJan Kara target_nid = numa_migrate_prep(page, vma, vmf->address, page_nid, 3812bae473a4SKirill A. Shutemov &flags); 381382b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 381498fa15f3SAnshuman Khandual if (target_nid == NUMA_NO_NODE) { 38154daae3b4SMel Gorman put_page(page); 38164daae3b4SMel Gorman goto out; 38174daae3b4SMel Gorman } 38184daae3b4SMel Gorman 38194daae3b4SMel Gorman /* Migrate to the requested node */ 38201bc115d8SMel Gorman migrated = migrate_misplaced_page(page, vma, target_nid); 38216688cc05SPeter Zijlstra if (migrated) { 38228191acbdSMel Gorman page_nid = target_nid; 38236688cc05SPeter Zijlstra flags |= TNF_MIGRATED; 3824074c2381SMel Gorman } else 3825074c2381SMel Gorman flags |= TNF_MIGRATE_FAIL; 38264daae3b4SMel Gorman 38274daae3b4SMel Gorman out: 382898fa15f3SAnshuman Khandual if (page_nid != NUMA_NO_NODE) 38296688cc05SPeter Zijlstra task_numa_fault(last_cpupid, page_nid, 1, flags); 3830d10e63f2SMel Gorman return 0; 3831d10e63f2SMel Gorman } 3832d10e63f2SMel Gorman 38332b740303SSouptick Joarder static inline vm_fault_t create_huge_pmd(struct vm_fault *vmf) 3834b96375f7SMatthew Wilcox { 3835f4200391SDave Jiang if (vma_is_anonymous(vmf->vma)) 383682b0f8c3SJan Kara return do_huge_pmd_anonymous_page(vmf); 3837a2d58167SDave Jiang if (vmf->vma->vm_ops->huge_fault) 3838c791ace1SDave Jiang return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD); 3839b96375f7SMatthew Wilcox return VM_FAULT_FALLBACK; 3840b96375f7SMatthew Wilcox } 3841b96375f7SMatthew Wilcox 3842183f24aaSGeert Uytterhoeven /* `inline' is required to avoid gcc 4.1.2 build error */ 38432b740303SSouptick Joarder static inline vm_fault_t wp_huge_pmd(struct vm_fault *vmf, pmd_t orig_pmd) 3844b96375f7SMatthew Wilcox { 384582b0f8c3SJan Kara if (vma_is_anonymous(vmf->vma)) 384682b0f8c3SJan Kara return do_huge_pmd_wp_page(vmf, orig_pmd); 3847a2d58167SDave Jiang if (vmf->vma->vm_ops->huge_fault) 3848c791ace1SDave Jiang return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD); 3849af9e4d5fSKirill A. Shutemov 3850af9e4d5fSKirill A. Shutemov /* COW handled on pte level: split pmd */ 385182b0f8c3SJan Kara VM_BUG_ON_VMA(vmf->vma->vm_flags & VM_SHARED, vmf->vma); 385282b0f8c3SJan Kara __split_huge_pmd(vmf->vma, vmf->pmd, vmf->address, false, NULL); 3853af9e4d5fSKirill A. Shutemov 3854b96375f7SMatthew Wilcox return VM_FAULT_FALLBACK; 3855b96375f7SMatthew Wilcox } 3856b96375f7SMatthew Wilcox 385738e08854SLorenzo Stoakes static inline bool vma_is_accessible(struct vm_area_struct *vma) 385838e08854SLorenzo Stoakes { 385938e08854SLorenzo Stoakes return vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE); 386038e08854SLorenzo Stoakes } 386138e08854SLorenzo Stoakes 38622b740303SSouptick Joarder static vm_fault_t create_huge_pud(struct vm_fault *vmf) 3863a00cc7d9SMatthew Wilcox { 3864a00cc7d9SMatthew Wilcox #ifdef CONFIG_TRANSPARENT_HUGEPAGE 3865a00cc7d9SMatthew Wilcox /* No support for anonymous transparent PUD pages yet */ 3866a00cc7d9SMatthew Wilcox if (vma_is_anonymous(vmf->vma)) 3867a00cc7d9SMatthew Wilcox return VM_FAULT_FALLBACK; 3868a00cc7d9SMatthew Wilcox if (vmf->vma->vm_ops->huge_fault) 3869c791ace1SDave Jiang return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD); 3870a00cc7d9SMatthew Wilcox #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 3871a00cc7d9SMatthew Wilcox return VM_FAULT_FALLBACK; 3872a00cc7d9SMatthew Wilcox } 3873a00cc7d9SMatthew Wilcox 38742b740303SSouptick Joarder static vm_fault_t wp_huge_pud(struct vm_fault *vmf, pud_t orig_pud) 3875a00cc7d9SMatthew Wilcox { 3876a00cc7d9SMatthew Wilcox #ifdef CONFIG_TRANSPARENT_HUGEPAGE 3877a00cc7d9SMatthew Wilcox /* No support for anonymous transparent PUD pages yet */ 3878a00cc7d9SMatthew Wilcox if (vma_is_anonymous(vmf->vma)) 3879a00cc7d9SMatthew Wilcox return VM_FAULT_FALLBACK; 3880a00cc7d9SMatthew Wilcox if (vmf->vma->vm_ops->huge_fault) 3881c791ace1SDave Jiang return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD); 3882a00cc7d9SMatthew Wilcox #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 3883a00cc7d9SMatthew Wilcox return VM_FAULT_FALLBACK; 3884a00cc7d9SMatthew Wilcox } 3885a00cc7d9SMatthew Wilcox 38861da177e4SLinus Torvalds /* 38871da177e4SLinus Torvalds * These routines also need to handle stuff like marking pages dirty 38881da177e4SLinus Torvalds * and/or accessed for architectures that don't do it in hardware (most 38891da177e4SLinus Torvalds * RISC architectures). The early dirtying is also good on the i386. 38901da177e4SLinus Torvalds * 38911da177e4SLinus Torvalds * There is also a hook called "update_mmu_cache()" that architectures 38921da177e4SLinus Torvalds * with external mmu caches can use to update those (ie the Sparc or 38931da177e4SLinus Torvalds * PowerPC hashed page tables that act as extended TLBs). 38941da177e4SLinus Torvalds * 38957267ec00SKirill A. Shutemov * We enter with non-exclusive mmap_sem (to exclude vma changes, but allow 38967267ec00SKirill A. Shutemov * concurrent faults). 38979a95f3cfSPaul Cassella * 38987267ec00SKirill A. Shutemov * The mmap_sem may have been released depending on flags and our return value. 38997267ec00SKirill A. Shutemov * See filemap_fault() and __lock_page_or_retry(). 39001da177e4SLinus Torvalds */ 39012b740303SSouptick Joarder static vm_fault_t handle_pte_fault(struct vm_fault *vmf) 39021da177e4SLinus Torvalds { 39031da177e4SLinus Torvalds pte_t entry; 39041da177e4SLinus Torvalds 390582b0f8c3SJan Kara if (unlikely(pmd_none(*vmf->pmd))) { 39067267ec00SKirill A. Shutemov /* 39077267ec00SKirill A. Shutemov * Leave __pte_alloc() until later: because vm_ops->fault may 39087267ec00SKirill A. Shutemov * want to allocate huge page, and if we expose page table 39097267ec00SKirill A. Shutemov * for an instant, it will be difficult to retract from 39107267ec00SKirill A. Shutemov * concurrent faults and from rmap lookups. 39117267ec00SKirill A. Shutemov */ 391282b0f8c3SJan Kara vmf->pte = NULL; 39137267ec00SKirill A. Shutemov } else { 39147267ec00SKirill A. Shutemov /* See comment in pte_alloc_one_map() */ 3915d0f0931dSRoss Zwisler if (pmd_devmap_trans_unstable(vmf->pmd)) 39167267ec00SKirill A. Shutemov return 0; 39177267ec00SKirill A. Shutemov /* 39187267ec00SKirill A. Shutemov * A regular pmd is established and it can't morph into a huge 39197267ec00SKirill A. Shutemov * pmd from under us anymore at this point because we hold the 39207267ec00SKirill A. Shutemov * mmap_sem read mode and khugepaged takes it in write mode. 39217267ec00SKirill A. Shutemov * So now it's safe to run pte_offset_map(). 39227267ec00SKirill A. Shutemov */ 392382b0f8c3SJan Kara vmf->pte = pte_offset_map(vmf->pmd, vmf->address); 39242994302bSJan Kara vmf->orig_pte = *vmf->pte; 39257267ec00SKirill A. Shutemov 3926e37c6982SChristian Borntraeger /* 3927e37c6982SChristian Borntraeger * some architectures can have larger ptes than wordsize, 39287267ec00SKirill A. Shutemov * e.g.ppc44x-defconfig has CONFIG_PTE_64BIT=y and 3929b03a0fe0SPaul E. McKenney * CONFIG_32BIT=y, so READ_ONCE cannot guarantee atomic 3930b03a0fe0SPaul E. McKenney * accesses. The code below just needs a consistent view 3931b03a0fe0SPaul E. McKenney * for the ifs and we later double check anyway with the 39327267ec00SKirill A. Shutemov * ptl lock held. So here a barrier will do. 3933e37c6982SChristian Borntraeger */ 3934e37c6982SChristian Borntraeger barrier(); 39352994302bSJan Kara if (pte_none(vmf->orig_pte)) { 393682b0f8c3SJan Kara pte_unmap(vmf->pte); 393782b0f8c3SJan Kara vmf->pte = NULL; 39387267ec00SKirill A. Shutemov } 39397267ec00SKirill A. Shutemov } 39407267ec00SKirill A. Shutemov 394182b0f8c3SJan Kara if (!vmf->pte) { 394282b0f8c3SJan Kara if (vma_is_anonymous(vmf->vma)) 394382b0f8c3SJan Kara return do_anonymous_page(vmf); 3944b5330628SOleg Nesterov else 394582b0f8c3SJan Kara return do_fault(vmf); 394665500d23SHugh Dickins } 39477267ec00SKirill A. Shutemov 39482994302bSJan Kara if (!pte_present(vmf->orig_pte)) 39492994302bSJan Kara return do_swap_page(vmf); 39501da177e4SLinus Torvalds 39512994302bSJan Kara if (pte_protnone(vmf->orig_pte) && vma_is_accessible(vmf->vma)) 39522994302bSJan Kara return do_numa_page(vmf); 3953d10e63f2SMel Gorman 395482b0f8c3SJan Kara vmf->ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd); 395582b0f8c3SJan Kara spin_lock(vmf->ptl); 39562994302bSJan Kara entry = vmf->orig_pte; 395782b0f8c3SJan Kara if (unlikely(!pte_same(*vmf->pte, entry))) 39588f4e2101SHugh Dickins goto unlock; 395982b0f8c3SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) { 3960f6f37321SLinus Torvalds if (!pte_write(entry)) 39612994302bSJan Kara return do_wp_page(vmf); 39621da177e4SLinus Torvalds entry = pte_mkdirty(entry); 39631da177e4SLinus Torvalds } 39641da177e4SLinus Torvalds entry = pte_mkyoung(entry); 396582b0f8c3SJan Kara if (ptep_set_access_flags(vmf->vma, vmf->address, vmf->pte, entry, 396682b0f8c3SJan Kara vmf->flags & FAULT_FLAG_WRITE)) { 396782b0f8c3SJan Kara update_mmu_cache(vmf->vma, vmf->address, vmf->pte); 39681a44e149SAndrea Arcangeli } else { 39691a44e149SAndrea Arcangeli /* 39701a44e149SAndrea Arcangeli * This is needed only for protection faults but the arch code 39711a44e149SAndrea Arcangeli * is not yet telling us if this is a protection fault or not. 39721a44e149SAndrea Arcangeli * This still avoids useless tlb flushes for .text page faults 39731a44e149SAndrea Arcangeli * with threads. 39741a44e149SAndrea Arcangeli */ 397582b0f8c3SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) 397682b0f8c3SJan Kara flush_tlb_fix_spurious_fault(vmf->vma, vmf->address); 39771a44e149SAndrea Arcangeli } 39788f4e2101SHugh Dickins unlock: 397982b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 398083c54070SNick Piggin return 0; 39811da177e4SLinus Torvalds } 39821da177e4SLinus Torvalds 39831da177e4SLinus Torvalds /* 39841da177e4SLinus Torvalds * By the time we get here, we already hold the mm semaphore 39859a95f3cfSPaul Cassella * 39869a95f3cfSPaul Cassella * The mmap_sem may have been released depending on flags and our 39879a95f3cfSPaul Cassella * return value. See filemap_fault() and __lock_page_or_retry(). 39881da177e4SLinus Torvalds */ 39892b740303SSouptick Joarder static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma, 39902b740303SSouptick Joarder unsigned long address, unsigned int flags) 39911da177e4SLinus Torvalds { 399282b0f8c3SJan Kara struct vm_fault vmf = { 3993bae473a4SKirill A. Shutemov .vma = vma, 39941a29d85eSJan Kara .address = address & PAGE_MASK, 3995bae473a4SKirill A. Shutemov .flags = flags, 39960721ec8bSJan Kara .pgoff = linear_page_index(vma, address), 3997667240e0SJan Kara .gfp_mask = __get_fault_gfp_mask(vma), 3998bae473a4SKirill A. Shutemov }; 3999fde26bedSAnshuman Khandual unsigned int dirty = flags & FAULT_FLAG_WRITE; 4000dcddffd4SKirill A. Shutemov struct mm_struct *mm = vma->vm_mm; 40011da177e4SLinus Torvalds pgd_t *pgd; 4002c2febafcSKirill A. Shutemov p4d_t *p4d; 40032b740303SSouptick Joarder vm_fault_t ret; 40041da177e4SLinus Torvalds 40051da177e4SLinus Torvalds pgd = pgd_offset(mm, address); 4006c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, address); 4007c2febafcSKirill A. Shutemov if (!p4d) 4008c2febafcSKirill A. Shutemov return VM_FAULT_OOM; 4009a00cc7d9SMatthew Wilcox 4010c2febafcSKirill A. Shutemov vmf.pud = pud_alloc(mm, p4d, address); 4011a00cc7d9SMatthew Wilcox if (!vmf.pud) 4012c74df32cSHugh Dickins return VM_FAULT_OOM; 40137635d9cbSMichal Hocko if (pud_none(*vmf.pud) && __transparent_hugepage_enabled(vma)) { 4014a00cc7d9SMatthew Wilcox ret = create_huge_pud(&vmf); 4015a00cc7d9SMatthew Wilcox if (!(ret & VM_FAULT_FALLBACK)) 4016a00cc7d9SMatthew Wilcox return ret; 4017a00cc7d9SMatthew Wilcox } else { 4018a00cc7d9SMatthew Wilcox pud_t orig_pud = *vmf.pud; 4019a00cc7d9SMatthew Wilcox 4020a00cc7d9SMatthew Wilcox barrier(); 4021a00cc7d9SMatthew Wilcox if (pud_trans_huge(orig_pud) || pud_devmap(orig_pud)) { 4022a00cc7d9SMatthew Wilcox 4023a00cc7d9SMatthew Wilcox /* NUMA case for anonymous PUDs would go here */ 4024a00cc7d9SMatthew Wilcox 4025f6f37321SLinus Torvalds if (dirty && !pud_write(orig_pud)) { 4026a00cc7d9SMatthew Wilcox ret = wp_huge_pud(&vmf, orig_pud); 4027a00cc7d9SMatthew Wilcox if (!(ret & VM_FAULT_FALLBACK)) 4028a00cc7d9SMatthew Wilcox return ret; 4029a00cc7d9SMatthew Wilcox } else { 4030a00cc7d9SMatthew Wilcox huge_pud_set_accessed(&vmf, orig_pud); 4031a00cc7d9SMatthew Wilcox return 0; 4032a00cc7d9SMatthew Wilcox } 4033a00cc7d9SMatthew Wilcox } 4034a00cc7d9SMatthew Wilcox } 4035a00cc7d9SMatthew Wilcox 4036a00cc7d9SMatthew Wilcox vmf.pmd = pmd_alloc(mm, vmf.pud, address); 403782b0f8c3SJan Kara if (!vmf.pmd) 4038c74df32cSHugh Dickins return VM_FAULT_OOM; 40397635d9cbSMichal Hocko if (pmd_none(*vmf.pmd) && __transparent_hugepage_enabled(vma)) { 4040a2d58167SDave Jiang ret = create_huge_pmd(&vmf); 4041c0292554SKirill A. Shutemov if (!(ret & VM_FAULT_FALLBACK)) 4042c0292554SKirill A. Shutemov return ret; 404371e3aac0SAndrea Arcangeli } else { 404482b0f8c3SJan Kara pmd_t orig_pmd = *vmf.pmd; 40451f1d06c3SDavid Rientjes 404671e3aac0SAndrea Arcangeli barrier(); 404784c3fc4eSZi Yan if (unlikely(is_swap_pmd(orig_pmd))) { 404884c3fc4eSZi Yan VM_BUG_ON(thp_migration_supported() && 404984c3fc4eSZi Yan !is_pmd_migration_entry(orig_pmd)); 405084c3fc4eSZi Yan if (is_pmd_migration_entry(orig_pmd)) 405184c3fc4eSZi Yan pmd_migration_entry_wait(mm, vmf.pmd); 405284c3fc4eSZi Yan return 0; 405384c3fc4eSZi Yan } 40545c7fb56eSDan Williams if (pmd_trans_huge(orig_pmd) || pmd_devmap(orig_pmd)) { 405538e08854SLorenzo Stoakes if (pmd_protnone(orig_pmd) && vma_is_accessible(vma)) 405682b0f8c3SJan Kara return do_huge_pmd_numa_page(&vmf, orig_pmd); 4057d10e63f2SMel Gorman 4058f6f37321SLinus Torvalds if (dirty && !pmd_write(orig_pmd)) { 405982b0f8c3SJan Kara ret = wp_huge_pmd(&vmf, orig_pmd); 40609845cbbdSKirill A. Shutemov if (!(ret & VM_FAULT_FALLBACK)) 40611f1d06c3SDavid Rientjes return ret; 4062a1dd450bSWill Deacon } else { 406382b0f8c3SJan Kara huge_pmd_set_accessed(&vmf, orig_pmd); 406471e3aac0SAndrea Arcangeli return 0; 406571e3aac0SAndrea Arcangeli } 406671e3aac0SAndrea Arcangeli } 40679845cbbdSKirill A. Shutemov } 406871e3aac0SAndrea Arcangeli 406982b0f8c3SJan Kara return handle_pte_fault(&vmf); 40701da177e4SLinus Torvalds } 40711da177e4SLinus Torvalds 40729a95f3cfSPaul Cassella /* 40739a95f3cfSPaul Cassella * By the time we get here, we already hold the mm semaphore 40749a95f3cfSPaul Cassella * 40759a95f3cfSPaul Cassella * The mmap_sem may have been released depending on flags and our 40769a95f3cfSPaul Cassella * return value. See filemap_fault() and __lock_page_or_retry(). 40779a95f3cfSPaul Cassella */ 40782b740303SSouptick Joarder vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address, 4079dcddffd4SKirill A. Shutemov unsigned int flags) 4080519e5247SJohannes Weiner { 40812b740303SSouptick Joarder vm_fault_t ret; 4082519e5247SJohannes Weiner 4083519e5247SJohannes Weiner __set_current_state(TASK_RUNNING); 4084519e5247SJohannes Weiner 4085519e5247SJohannes Weiner count_vm_event(PGFAULT); 40862262185cSRoman Gushchin count_memcg_event_mm(vma->vm_mm, PGFAULT); 4087519e5247SJohannes Weiner 4088519e5247SJohannes Weiner /* do counter updates before entering really critical section. */ 4089519e5247SJohannes Weiner check_sync_rss_stat(current); 4090519e5247SJohannes Weiner 4091de0c799bSLaurent Dufour if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE, 4092de0c799bSLaurent Dufour flags & FAULT_FLAG_INSTRUCTION, 4093de0c799bSLaurent Dufour flags & FAULT_FLAG_REMOTE)) 4094de0c799bSLaurent Dufour return VM_FAULT_SIGSEGV; 4095de0c799bSLaurent Dufour 4096519e5247SJohannes Weiner /* 4097519e5247SJohannes Weiner * Enable the memcg OOM handling for faults triggered in user 4098519e5247SJohannes Weiner * space. Kernel faults are handled more gracefully. 4099519e5247SJohannes Weiner */ 4100519e5247SJohannes Weiner if (flags & FAULT_FLAG_USER) 410129ef680aSMichal Hocko mem_cgroup_enter_user_fault(); 4102519e5247SJohannes Weiner 4103bae473a4SKirill A. Shutemov if (unlikely(is_vm_hugetlb_page(vma))) 4104bae473a4SKirill A. Shutemov ret = hugetlb_fault(vma->vm_mm, vma, address, flags); 4105bae473a4SKirill A. Shutemov else 4106dcddffd4SKirill A. Shutemov ret = __handle_mm_fault(vma, address, flags); 4107519e5247SJohannes Weiner 410849426420SJohannes Weiner if (flags & FAULT_FLAG_USER) { 410929ef680aSMichal Hocko mem_cgroup_exit_user_fault(); 411049426420SJohannes Weiner /* 411149426420SJohannes Weiner * The task may have entered a memcg OOM situation but 411249426420SJohannes Weiner * if the allocation error was handled gracefully (no 411349426420SJohannes Weiner * VM_FAULT_OOM), there is no need to kill anything. 411449426420SJohannes Weiner * Just clean up the OOM state peacefully. 411549426420SJohannes Weiner */ 411649426420SJohannes Weiner if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM)) 411749426420SJohannes Weiner mem_cgroup_oom_synchronize(false); 411849426420SJohannes Weiner } 41193812c8c8SJohannes Weiner 4120519e5247SJohannes Weiner return ret; 4121519e5247SJohannes Weiner } 4122e1d6d01aSJesse Barnes EXPORT_SYMBOL_GPL(handle_mm_fault); 4123519e5247SJohannes Weiner 412490eceff1SKirill A. Shutemov #ifndef __PAGETABLE_P4D_FOLDED 412590eceff1SKirill A. Shutemov /* 412690eceff1SKirill A. Shutemov * Allocate p4d page table. 412790eceff1SKirill A. Shutemov * We've already handled the fast-path in-line. 412890eceff1SKirill A. Shutemov */ 412990eceff1SKirill A. Shutemov int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address) 413090eceff1SKirill A. Shutemov { 413190eceff1SKirill A. Shutemov p4d_t *new = p4d_alloc_one(mm, address); 413290eceff1SKirill A. Shutemov if (!new) 413390eceff1SKirill A. Shutemov return -ENOMEM; 413490eceff1SKirill A. Shutemov 413590eceff1SKirill A. Shutemov smp_wmb(); /* See comment in __pte_alloc */ 413690eceff1SKirill A. Shutemov 413790eceff1SKirill A. Shutemov spin_lock(&mm->page_table_lock); 413890eceff1SKirill A. Shutemov if (pgd_present(*pgd)) /* Another has populated it */ 413990eceff1SKirill A. Shutemov p4d_free(mm, new); 414090eceff1SKirill A. Shutemov else 414190eceff1SKirill A. Shutemov pgd_populate(mm, pgd, new); 414290eceff1SKirill A. Shutemov spin_unlock(&mm->page_table_lock); 414390eceff1SKirill A. Shutemov return 0; 414490eceff1SKirill A. Shutemov } 414590eceff1SKirill A. Shutemov #endif /* __PAGETABLE_P4D_FOLDED */ 414690eceff1SKirill A. Shutemov 41471da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED 41481da177e4SLinus Torvalds /* 41491da177e4SLinus Torvalds * Allocate page upper directory. 4150872fec16SHugh Dickins * We've already handled the fast-path in-line. 41511da177e4SLinus Torvalds */ 4152c2febafcSKirill A. Shutemov int __pud_alloc(struct mm_struct *mm, p4d_t *p4d, unsigned long address) 41531da177e4SLinus Torvalds { 4154c74df32cSHugh Dickins pud_t *new = pud_alloc_one(mm, address); 4155c74df32cSHugh Dickins if (!new) 41561bb3630eSHugh Dickins return -ENOMEM; 41571da177e4SLinus Torvalds 4158362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 4159362a61adSNick Piggin 4160872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 4161c2febafcSKirill A. Shutemov #ifndef __ARCH_HAS_5LEVEL_HACK 4162b4e98d9aSKirill A. Shutemov if (!p4d_present(*p4d)) { 4163b4e98d9aSKirill A. Shutemov mm_inc_nr_puds(mm); 4164c2febafcSKirill A. Shutemov p4d_populate(mm, p4d, new); 4165b4e98d9aSKirill A. Shutemov } else /* Another has populated it */ 4166c2febafcSKirill A. Shutemov pud_free(mm, new); 4167b4e98d9aSKirill A. Shutemov #else 4168b4e98d9aSKirill A. Shutemov if (!pgd_present(*p4d)) { 4169b4e98d9aSKirill A. Shutemov mm_inc_nr_puds(mm); 4170c2febafcSKirill A. Shutemov pgd_populate(mm, p4d, new); 4171b4e98d9aSKirill A. Shutemov } else /* Another has populated it */ 4172b4e98d9aSKirill A. Shutemov pud_free(mm, new); 4173c2febafcSKirill A. Shutemov #endif /* __ARCH_HAS_5LEVEL_HACK */ 4174872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 41751bb3630eSHugh Dickins return 0; 41761da177e4SLinus Torvalds } 41771da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */ 41781da177e4SLinus Torvalds 41791da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED 41801da177e4SLinus Torvalds /* 41811da177e4SLinus Torvalds * Allocate page middle directory. 4182872fec16SHugh Dickins * We've already handled the fast-path in-line. 41831da177e4SLinus Torvalds */ 41841bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) 41851da177e4SLinus Torvalds { 4186a00cc7d9SMatthew Wilcox spinlock_t *ptl; 4187c74df32cSHugh Dickins pmd_t *new = pmd_alloc_one(mm, address); 4188c74df32cSHugh Dickins if (!new) 41891bb3630eSHugh Dickins return -ENOMEM; 41901da177e4SLinus Torvalds 4191362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 4192362a61adSNick Piggin 4193a00cc7d9SMatthew Wilcox ptl = pud_lock(mm, pud); 41941da177e4SLinus Torvalds #ifndef __ARCH_HAS_4LEVEL_HACK 4195dc6c9a35SKirill A. Shutemov if (!pud_present(*pud)) { 4196dc6c9a35SKirill A. Shutemov mm_inc_nr_pmds(mm); 41971da177e4SLinus Torvalds pud_populate(mm, pud, new); 4198dc6c9a35SKirill A. Shutemov } else /* Another has populated it */ 41995e541973SBenjamin Herrenschmidt pmd_free(mm, new); 4200dc6c9a35SKirill A. Shutemov #else 4201dc6c9a35SKirill A. Shutemov if (!pgd_present(*pud)) { 4202dc6c9a35SKirill A. Shutemov mm_inc_nr_pmds(mm); 42031da177e4SLinus Torvalds pgd_populate(mm, pud, new); 4204dc6c9a35SKirill A. Shutemov } else /* Another has populated it */ 4205dc6c9a35SKirill A. Shutemov pmd_free(mm, new); 42061da177e4SLinus Torvalds #endif /* __ARCH_HAS_4LEVEL_HACK */ 4207a00cc7d9SMatthew Wilcox spin_unlock(ptl); 42081bb3630eSHugh Dickins return 0; 42091da177e4SLinus Torvalds } 42101da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */ 42111da177e4SLinus Torvalds 421209796395SRoss Zwisler static int __follow_pte_pmd(struct mm_struct *mm, unsigned long address, 4213ac46d4f3SJérôme Glisse struct mmu_notifier_range *range, 421409796395SRoss Zwisler pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp) 4215f8ad0f49SJohannes Weiner { 4216f8ad0f49SJohannes Weiner pgd_t *pgd; 4217c2febafcSKirill A. Shutemov p4d_t *p4d; 4218f8ad0f49SJohannes Weiner pud_t *pud; 4219f8ad0f49SJohannes Weiner pmd_t *pmd; 4220f8ad0f49SJohannes Weiner pte_t *ptep; 4221f8ad0f49SJohannes Weiner 4222f8ad0f49SJohannes Weiner pgd = pgd_offset(mm, address); 4223f8ad0f49SJohannes Weiner if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) 4224f8ad0f49SJohannes Weiner goto out; 4225f8ad0f49SJohannes Weiner 4226c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, address); 4227c2febafcSKirill A. Shutemov if (p4d_none(*p4d) || unlikely(p4d_bad(*p4d))) 4228c2febafcSKirill A. Shutemov goto out; 4229c2febafcSKirill A. Shutemov 4230c2febafcSKirill A. Shutemov pud = pud_offset(p4d, address); 4231f8ad0f49SJohannes Weiner if (pud_none(*pud) || unlikely(pud_bad(*pud))) 4232f8ad0f49SJohannes Weiner goto out; 4233f8ad0f49SJohannes Weiner 4234f8ad0f49SJohannes Weiner pmd = pmd_offset(pud, address); 4235f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 423609796395SRoss Zwisler 423709796395SRoss Zwisler if (pmd_huge(*pmd)) { 423809796395SRoss Zwisler if (!pmdpp) 4239f8ad0f49SJohannes Weiner goto out; 4240f8ad0f49SJohannes Weiner 4241ac46d4f3SJérôme Glisse if (range) { 42427269f999SJérôme Glisse mmu_notifier_range_init(range, MMU_NOTIFY_CLEAR, 0, 42436f4f13e8SJérôme Glisse NULL, mm, address & PMD_MASK, 4244ac46d4f3SJérôme Glisse (address & PMD_MASK) + PMD_SIZE); 4245ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(range); 4246a4d1a885SJérôme Glisse } 424709796395SRoss Zwisler *ptlp = pmd_lock(mm, pmd); 424809796395SRoss Zwisler if (pmd_huge(*pmd)) { 424909796395SRoss Zwisler *pmdpp = pmd; 425009796395SRoss Zwisler return 0; 425109796395SRoss Zwisler } 425209796395SRoss Zwisler spin_unlock(*ptlp); 4253ac46d4f3SJérôme Glisse if (range) 4254ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(range); 425509796395SRoss Zwisler } 425609796395SRoss Zwisler 425709796395SRoss Zwisler if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd))) 4258f8ad0f49SJohannes Weiner goto out; 4259f8ad0f49SJohannes Weiner 4260ac46d4f3SJérôme Glisse if (range) { 42617269f999SJérôme Glisse mmu_notifier_range_init(range, MMU_NOTIFY_CLEAR, 0, NULL, mm, 42626f4f13e8SJérôme Glisse address & PAGE_MASK, 42631ed7293aSMatthew Wilcox (address & PAGE_MASK) + PAGE_SIZE); 4264ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(range); 4265a4d1a885SJérôme Glisse } 4266f8ad0f49SJohannes Weiner ptep = pte_offset_map_lock(mm, pmd, address, ptlp); 4267f8ad0f49SJohannes Weiner if (!pte_present(*ptep)) 4268f8ad0f49SJohannes Weiner goto unlock; 4269f8ad0f49SJohannes Weiner *ptepp = ptep; 4270f8ad0f49SJohannes Weiner return 0; 4271f8ad0f49SJohannes Weiner unlock: 4272f8ad0f49SJohannes Weiner pte_unmap_unlock(ptep, *ptlp); 4273ac46d4f3SJérôme Glisse if (range) 4274ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(range); 4275f8ad0f49SJohannes Weiner out: 4276f8ad0f49SJohannes Weiner return -EINVAL; 4277f8ad0f49SJohannes Weiner } 4278f8ad0f49SJohannes Weiner 4279f729c8c9SRoss Zwisler static inline int follow_pte(struct mm_struct *mm, unsigned long address, 4280f729c8c9SRoss Zwisler pte_t **ptepp, spinlock_t **ptlp) 42811b36ba81SNamhyung Kim { 42821b36ba81SNamhyung Kim int res; 42831b36ba81SNamhyung Kim 42841b36ba81SNamhyung Kim /* (void) is needed to make gcc happy */ 42851b36ba81SNamhyung Kim (void) __cond_lock(*ptlp, 4286ac46d4f3SJérôme Glisse !(res = __follow_pte_pmd(mm, address, NULL, 4287a4d1a885SJérôme Glisse ptepp, NULL, ptlp))); 42881b36ba81SNamhyung Kim return res; 42891b36ba81SNamhyung Kim } 42901b36ba81SNamhyung Kim 429109796395SRoss Zwisler int follow_pte_pmd(struct mm_struct *mm, unsigned long address, 4292ac46d4f3SJérôme Glisse struct mmu_notifier_range *range, 429309796395SRoss Zwisler pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp) 429409796395SRoss Zwisler { 429509796395SRoss Zwisler int res; 429609796395SRoss Zwisler 429709796395SRoss Zwisler /* (void) is needed to make gcc happy */ 429809796395SRoss Zwisler (void) __cond_lock(*ptlp, 4299ac46d4f3SJérôme Glisse !(res = __follow_pte_pmd(mm, address, range, 4300a4d1a885SJérôme Glisse ptepp, pmdpp, ptlp))); 430109796395SRoss Zwisler return res; 430209796395SRoss Zwisler } 430309796395SRoss Zwisler EXPORT_SYMBOL(follow_pte_pmd); 430409796395SRoss Zwisler 43053b6748e2SJohannes Weiner /** 43063b6748e2SJohannes Weiner * follow_pfn - look up PFN at a user virtual address 43073b6748e2SJohannes Weiner * @vma: memory mapping 43083b6748e2SJohannes Weiner * @address: user virtual address 43093b6748e2SJohannes Weiner * @pfn: location to store found PFN 43103b6748e2SJohannes Weiner * 43113b6748e2SJohannes Weiner * Only IO mappings and raw PFN mappings are allowed. 43123b6748e2SJohannes Weiner * 4313a862f68aSMike Rapoport * Return: zero and the pfn at @pfn on success, -ve otherwise. 43143b6748e2SJohannes Weiner */ 43153b6748e2SJohannes Weiner int follow_pfn(struct vm_area_struct *vma, unsigned long address, 43163b6748e2SJohannes Weiner unsigned long *pfn) 43173b6748e2SJohannes Weiner { 43183b6748e2SJohannes Weiner int ret = -EINVAL; 43193b6748e2SJohannes Weiner spinlock_t *ptl; 43203b6748e2SJohannes Weiner pte_t *ptep; 43213b6748e2SJohannes Weiner 43223b6748e2SJohannes Weiner if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 43233b6748e2SJohannes Weiner return ret; 43243b6748e2SJohannes Weiner 43253b6748e2SJohannes Weiner ret = follow_pte(vma->vm_mm, address, &ptep, &ptl); 43263b6748e2SJohannes Weiner if (ret) 43273b6748e2SJohannes Weiner return ret; 43283b6748e2SJohannes Weiner *pfn = pte_pfn(*ptep); 43293b6748e2SJohannes Weiner pte_unmap_unlock(ptep, ptl); 43303b6748e2SJohannes Weiner return 0; 43313b6748e2SJohannes Weiner } 43323b6748e2SJohannes Weiner EXPORT_SYMBOL(follow_pfn); 43333b6748e2SJohannes Weiner 433428b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT 4335d87fe660Svenkatesh.pallipadi@intel.com int follow_phys(struct vm_area_struct *vma, 433628b2ee20SRik van Riel unsigned long address, unsigned int flags, 4337d87fe660Svenkatesh.pallipadi@intel.com unsigned long *prot, resource_size_t *phys) 433828b2ee20SRik van Riel { 433903668a4dSJohannes Weiner int ret = -EINVAL; 434028b2ee20SRik van Riel pte_t *ptep, pte; 434128b2ee20SRik van Riel spinlock_t *ptl; 434228b2ee20SRik van Riel 4343d87fe660Svenkatesh.pallipadi@intel.com if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 4344d87fe660Svenkatesh.pallipadi@intel.com goto out; 434528b2ee20SRik van Riel 434603668a4dSJohannes Weiner if (follow_pte(vma->vm_mm, address, &ptep, &ptl)) 4347d87fe660Svenkatesh.pallipadi@intel.com goto out; 434828b2ee20SRik van Riel pte = *ptep; 434903668a4dSJohannes Weiner 4350f6f37321SLinus Torvalds if ((flags & FOLL_WRITE) && !pte_write(pte)) 435128b2ee20SRik van Riel goto unlock; 435228b2ee20SRik van Riel 435328b2ee20SRik van Riel *prot = pgprot_val(pte_pgprot(pte)); 435403668a4dSJohannes Weiner *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT; 435528b2ee20SRik van Riel 435603668a4dSJohannes Weiner ret = 0; 435728b2ee20SRik van Riel unlock: 435828b2ee20SRik van Riel pte_unmap_unlock(ptep, ptl); 435928b2ee20SRik van Riel out: 4360d87fe660Svenkatesh.pallipadi@intel.com return ret; 436128b2ee20SRik van Riel } 436228b2ee20SRik van Riel 436328b2ee20SRik van Riel int generic_access_phys(struct vm_area_struct *vma, unsigned long addr, 436428b2ee20SRik van Riel void *buf, int len, int write) 436528b2ee20SRik van Riel { 436628b2ee20SRik van Riel resource_size_t phys_addr; 436728b2ee20SRik van Riel unsigned long prot = 0; 43682bc7273bSKOSAKI Motohiro void __iomem *maddr; 436928b2ee20SRik van Riel int offset = addr & (PAGE_SIZE-1); 437028b2ee20SRik van Riel 4371d87fe660Svenkatesh.pallipadi@intel.com if (follow_phys(vma, addr, write, &prot, &phys_addr)) 437228b2ee20SRik van Riel return -EINVAL; 437328b2ee20SRik van Riel 43749cb12d7bSGrazvydas Ignotas maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot); 437524eee1e4Sjie@chenjie6@huwei.com if (!maddr) 437624eee1e4Sjie@chenjie6@huwei.com return -ENOMEM; 437724eee1e4Sjie@chenjie6@huwei.com 437828b2ee20SRik van Riel if (write) 437928b2ee20SRik van Riel memcpy_toio(maddr + offset, buf, len); 438028b2ee20SRik van Riel else 438128b2ee20SRik van Riel memcpy_fromio(buf, maddr + offset, len); 438228b2ee20SRik van Riel iounmap(maddr); 438328b2ee20SRik van Riel 438428b2ee20SRik van Riel return len; 438528b2ee20SRik van Riel } 43865a73633eSUwe Kleine-König EXPORT_SYMBOL_GPL(generic_access_phys); 438728b2ee20SRik van Riel #endif 438828b2ee20SRik van Riel 43890ec76a11SDavid Howells /* 4390206cb636SStephen Wilson * Access another process' address space as given in mm. If non-NULL, use the 4391206cb636SStephen Wilson * given task for page fault accounting. 43920ec76a11SDavid Howells */ 439384d77d3fSEric W. Biederman int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm, 4394442486ecSLorenzo Stoakes unsigned long addr, void *buf, int len, unsigned int gup_flags) 43950ec76a11SDavid Howells { 43960ec76a11SDavid Howells struct vm_area_struct *vma; 43970ec76a11SDavid Howells void *old_buf = buf; 4398442486ecSLorenzo Stoakes int write = gup_flags & FOLL_WRITE; 43990ec76a11SDavid Howells 44001e426fe2SKonstantin Khlebnikov if (down_read_killable(&mm->mmap_sem)) 44011e426fe2SKonstantin Khlebnikov return 0; 44021e426fe2SKonstantin Khlebnikov 4403183ff22bSSimon Arlott /* ignore errors, just check how much was successfully transferred */ 44040ec76a11SDavid Howells while (len) { 44050ec76a11SDavid Howells int bytes, ret, offset; 44060ec76a11SDavid Howells void *maddr; 440728b2ee20SRik van Riel struct page *page = NULL; 44080ec76a11SDavid Howells 44091e987790SDave Hansen ret = get_user_pages_remote(tsk, mm, addr, 1, 44105b56d49fSLorenzo Stoakes gup_flags, &page, &vma, NULL); 441128b2ee20SRik van Riel if (ret <= 0) { 4412dbffcd03SRik van Riel #ifndef CONFIG_HAVE_IOREMAP_PROT 4413dbffcd03SRik van Riel break; 4414dbffcd03SRik van Riel #else 441528b2ee20SRik van Riel /* 441628b2ee20SRik van Riel * Check if this is a VM_IO | VM_PFNMAP VMA, which 441728b2ee20SRik van Riel * we can access using slightly different code. 441828b2ee20SRik van Riel */ 441928b2ee20SRik van Riel vma = find_vma(mm, addr); 4420fe936dfcSMichael Ellerman if (!vma || vma->vm_start > addr) 44210ec76a11SDavid Howells break; 442228b2ee20SRik van Riel if (vma->vm_ops && vma->vm_ops->access) 442328b2ee20SRik van Riel ret = vma->vm_ops->access(vma, addr, buf, 442428b2ee20SRik van Riel len, write); 442528b2ee20SRik van Riel if (ret <= 0) 442628b2ee20SRik van Riel break; 442728b2ee20SRik van Riel bytes = ret; 4428dbffcd03SRik van Riel #endif 442928b2ee20SRik van Riel } else { 44300ec76a11SDavid Howells bytes = len; 44310ec76a11SDavid Howells offset = addr & (PAGE_SIZE-1); 44320ec76a11SDavid Howells if (bytes > PAGE_SIZE-offset) 44330ec76a11SDavid Howells bytes = PAGE_SIZE-offset; 44340ec76a11SDavid Howells 44350ec76a11SDavid Howells maddr = kmap(page); 44360ec76a11SDavid Howells if (write) { 44370ec76a11SDavid Howells copy_to_user_page(vma, page, addr, 44380ec76a11SDavid Howells maddr + offset, buf, bytes); 44390ec76a11SDavid Howells set_page_dirty_lock(page); 44400ec76a11SDavid Howells } else { 44410ec76a11SDavid Howells copy_from_user_page(vma, page, addr, 44420ec76a11SDavid Howells buf, maddr + offset, bytes); 44430ec76a11SDavid Howells } 44440ec76a11SDavid Howells kunmap(page); 444509cbfeafSKirill A. Shutemov put_page(page); 444628b2ee20SRik van Riel } 44470ec76a11SDavid Howells len -= bytes; 44480ec76a11SDavid Howells buf += bytes; 44490ec76a11SDavid Howells addr += bytes; 44500ec76a11SDavid Howells } 44510ec76a11SDavid Howells up_read(&mm->mmap_sem); 44520ec76a11SDavid Howells 44530ec76a11SDavid Howells return buf - old_buf; 44540ec76a11SDavid Howells } 445503252919SAndi Kleen 44565ddd36b9SStephen Wilson /** 4457ae91dbfcSRandy Dunlap * access_remote_vm - access another process' address space 44585ddd36b9SStephen Wilson * @mm: the mm_struct of the target address space 44595ddd36b9SStephen Wilson * @addr: start address to access 44605ddd36b9SStephen Wilson * @buf: source or destination buffer 44615ddd36b9SStephen Wilson * @len: number of bytes to transfer 44626347e8d5SLorenzo Stoakes * @gup_flags: flags modifying lookup behaviour 44635ddd36b9SStephen Wilson * 44645ddd36b9SStephen Wilson * The caller must hold a reference on @mm. 4465a862f68aSMike Rapoport * 4466a862f68aSMike Rapoport * Return: number of bytes copied from source to destination. 44675ddd36b9SStephen Wilson */ 44685ddd36b9SStephen Wilson int access_remote_vm(struct mm_struct *mm, unsigned long addr, 44696347e8d5SLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 44705ddd36b9SStephen Wilson { 44716347e8d5SLorenzo Stoakes return __access_remote_vm(NULL, mm, addr, buf, len, gup_flags); 44725ddd36b9SStephen Wilson } 44735ddd36b9SStephen Wilson 447403252919SAndi Kleen /* 4475206cb636SStephen Wilson * Access another process' address space. 4476206cb636SStephen Wilson * Source/target buffer must be kernel space, 4477206cb636SStephen Wilson * Do not walk the page table directly, use get_user_pages 4478206cb636SStephen Wilson */ 4479206cb636SStephen Wilson int access_process_vm(struct task_struct *tsk, unsigned long addr, 4480f307ab6dSLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 4481206cb636SStephen Wilson { 4482206cb636SStephen Wilson struct mm_struct *mm; 4483206cb636SStephen Wilson int ret; 4484206cb636SStephen Wilson 4485206cb636SStephen Wilson mm = get_task_mm(tsk); 4486206cb636SStephen Wilson if (!mm) 4487206cb636SStephen Wilson return 0; 4488206cb636SStephen Wilson 4489f307ab6dSLorenzo Stoakes ret = __access_remote_vm(tsk, mm, addr, buf, len, gup_flags); 4490442486ecSLorenzo Stoakes 4491206cb636SStephen Wilson mmput(mm); 4492206cb636SStephen Wilson 4493206cb636SStephen Wilson return ret; 4494206cb636SStephen Wilson } 4495fcd35857SCatalin Marinas EXPORT_SYMBOL_GPL(access_process_vm); 4496206cb636SStephen Wilson 449703252919SAndi Kleen /* 449803252919SAndi Kleen * Print the name of a VMA. 449903252919SAndi Kleen */ 450003252919SAndi Kleen void print_vma_addr(char *prefix, unsigned long ip) 450103252919SAndi Kleen { 450203252919SAndi Kleen struct mm_struct *mm = current->mm; 450303252919SAndi Kleen struct vm_area_struct *vma; 450403252919SAndi Kleen 4505e8bff74aSIngo Molnar /* 45060a7f682dSMichal Hocko * we might be running from an atomic context so we cannot sleep 4507e8bff74aSIngo Molnar */ 45080a7f682dSMichal Hocko if (!down_read_trylock(&mm->mmap_sem)) 4509e8bff74aSIngo Molnar return; 4510e8bff74aSIngo Molnar 451103252919SAndi Kleen vma = find_vma(mm, ip); 451203252919SAndi Kleen if (vma && vma->vm_file) { 451303252919SAndi Kleen struct file *f = vma->vm_file; 45140a7f682dSMichal Hocko char *buf = (char *)__get_free_page(GFP_NOWAIT); 451503252919SAndi Kleen if (buf) { 45162fbc57c5SAndy Shevchenko char *p; 451703252919SAndi Kleen 45189bf39ab2SMiklos Szeredi p = file_path(f, buf, PAGE_SIZE); 451903252919SAndi Kleen if (IS_ERR(p)) 452003252919SAndi Kleen p = "?"; 45212fbc57c5SAndy Shevchenko printk("%s%s[%lx+%lx]", prefix, kbasename(p), 452203252919SAndi Kleen vma->vm_start, 452303252919SAndi Kleen vma->vm_end - vma->vm_start); 452403252919SAndi Kleen free_page((unsigned long)buf); 452503252919SAndi Kleen } 452603252919SAndi Kleen } 452751a07e50SJeff Liu up_read(&mm->mmap_sem); 452803252919SAndi Kleen } 45293ee1afa3SNick Piggin 4530662bbcb2SMichael S. Tsirkin #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP) 45319ec23531SDavid Hildenbrand void __might_fault(const char *file, int line) 45323ee1afa3SNick Piggin { 453395156f00SPeter Zijlstra /* 453495156f00SPeter Zijlstra * Some code (nfs/sunrpc) uses socket ops on kernel memory while 453595156f00SPeter Zijlstra * holding the mmap_sem, this is safe because kernel memory doesn't 453695156f00SPeter Zijlstra * get paged out, therefore we'll never actually fault, and the 453795156f00SPeter Zijlstra * below annotations will generate false positives. 453895156f00SPeter Zijlstra */ 4539db68ce10SAl Viro if (uaccess_kernel()) 454095156f00SPeter Zijlstra return; 45419ec23531SDavid Hildenbrand if (pagefault_disabled()) 4542662bbcb2SMichael S. Tsirkin return; 45439ec23531SDavid Hildenbrand __might_sleep(file, line, 0); 45449ec23531SDavid Hildenbrand #if defined(CONFIG_DEBUG_ATOMIC_SLEEP) 4545662bbcb2SMichael S. Tsirkin if (current->mm) 45463ee1afa3SNick Piggin might_lock_read(¤t->mm->mmap_sem); 45479ec23531SDavid Hildenbrand #endif 45483ee1afa3SNick Piggin } 45499ec23531SDavid Hildenbrand EXPORT_SYMBOL(__might_fault); 45503ee1afa3SNick Piggin #endif 455147ad8475SAndrea Arcangeli 455247ad8475SAndrea Arcangeli #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS) 4553c6ddfb6cSHuang Ying /* 4554c6ddfb6cSHuang Ying * Process all subpages of the specified huge page with the specified 4555c6ddfb6cSHuang Ying * operation. The target subpage will be processed last to keep its 4556c6ddfb6cSHuang Ying * cache lines hot. 4557c6ddfb6cSHuang Ying */ 4558c6ddfb6cSHuang Ying static inline void process_huge_page( 4559c6ddfb6cSHuang Ying unsigned long addr_hint, unsigned int pages_per_huge_page, 4560c6ddfb6cSHuang Ying void (*process_subpage)(unsigned long addr, int idx, void *arg), 4561c6ddfb6cSHuang Ying void *arg) 4562c6ddfb6cSHuang Ying { 4563c6ddfb6cSHuang Ying int i, n, base, l; 4564c6ddfb6cSHuang Ying unsigned long addr = addr_hint & 4565c6ddfb6cSHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 4566c6ddfb6cSHuang Ying 4567c6ddfb6cSHuang Ying /* Process target subpage last to keep its cache lines hot */ 4568c6ddfb6cSHuang Ying might_sleep(); 4569c6ddfb6cSHuang Ying n = (addr_hint - addr) / PAGE_SIZE; 4570c6ddfb6cSHuang Ying if (2 * n <= pages_per_huge_page) { 4571c6ddfb6cSHuang Ying /* If target subpage in first half of huge page */ 4572c6ddfb6cSHuang Ying base = 0; 4573c6ddfb6cSHuang Ying l = n; 4574c6ddfb6cSHuang Ying /* Process subpages at the end of huge page */ 4575c6ddfb6cSHuang Ying for (i = pages_per_huge_page - 1; i >= 2 * n; i--) { 4576c6ddfb6cSHuang Ying cond_resched(); 4577c6ddfb6cSHuang Ying process_subpage(addr + i * PAGE_SIZE, i, arg); 4578c6ddfb6cSHuang Ying } 4579c6ddfb6cSHuang Ying } else { 4580c6ddfb6cSHuang Ying /* If target subpage in second half of huge page */ 4581c6ddfb6cSHuang Ying base = pages_per_huge_page - 2 * (pages_per_huge_page - n); 4582c6ddfb6cSHuang Ying l = pages_per_huge_page - n; 4583c6ddfb6cSHuang Ying /* Process subpages at the begin of huge page */ 4584c6ddfb6cSHuang Ying for (i = 0; i < base; i++) { 4585c6ddfb6cSHuang Ying cond_resched(); 4586c6ddfb6cSHuang Ying process_subpage(addr + i * PAGE_SIZE, i, arg); 4587c6ddfb6cSHuang Ying } 4588c6ddfb6cSHuang Ying } 4589c6ddfb6cSHuang Ying /* 4590c6ddfb6cSHuang Ying * Process remaining subpages in left-right-left-right pattern 4591c6ddfb6cSHuang Ying * towards the target subpage 4592c6ddfb6cSHuang Ying */ 4593c6ddfb6cSHuang Ying for (i = 0; i < l; i++) { 4594c6ddfb6cSHuang Ying int left_idx = base + i; 4595c6ddfb6cSHuang Ying int right_idx = base + 2 * l - 1 - i; 4596c6ddfb6cSHuang Ying 4597c6ddfb6cSHuang Ying cond_resched(); 4598c6ddfb6cSHuang Ying process_subpage(addr + left_idx * PAGE_SIZE, left_idx, arg); 4599c6ddfb6cSHuang Ying cond_resched(); 4600c6ddfb6cSHuang Ying process_subpage(addr + right_idx * PAGE_SIZE, right_idx, arg); 4601c6ddfb6cSHuang Ying } 4602c6ddfb6cSHuang Ying } 4603c6ddfb6cSHuang Ying 460447ad8475SAndrea Arcangeli static void clear_gigantic_page(struct page *page, 460547ad8475SAndrea Arcangeli unsigned long addr, 460647ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 460747ad8475SAndrea Arcangeli { 460847ad8475SAndrea Arcangeli int i; 460947ad8475SAndrea Arcangeli struct page *p = page; 461047ad8475SAndrea Arcangeli 461147ad8475SAndrea Arcangeli might_sleep(); 461247ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; 461347ad8475SAndrea Arcangeli i++, p = mem_map_next(p, page, i)) { 461447ad8475SAndrea Arcangeli cond_resched(); 461547ad8475SAndrea Arcangeli clear_user_highpage(p, addr + i * PAGE_SIZE); 461647ad8475SAndrea Arcangeli } 461747ad8475SAndrea Arcangeli } 4618c6ddfb6cSHuang Ying 4619c6ddfb6cSHuang Ying static void clear_subpage(unsigned long addr, int idx, void *arg) 4620c6ddfb6cSHuang Ying { 4621c6ddfb6cSHuang Ying struct page *page = arg; 4622c6ddfb6cSHuang Ying 4623c6ddfb6cSHuang Ying clear_user_highpage(page + idx, addr); 4624c6ddfb6cSHuang Ying } 4625c6ddfb6cSHuang Ying 462647ad8475SAndrea Arcangeli void clear_huge_page(struct page *page, 4627c79b57e4SHuang Ying unsigned long addr_hint, unsigned int pages_per_huge_page) 462847ad8475SAndrea Arcangeli { 4629c79b57e4SHuang Ying unsigned long addr = addr_hint & 4630c79b57e4SHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 463147ad8475SAndrea Arcangeli 463247ad8475SAndrea Arcangeli if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { 463347ad8475SAndrea Arcangeli clear_gigantic_page(page, addr, pages_per_huge_page); 463447ad8475SAndrea Arcangeli return; 463547ad8475SAndrea Arcangeli } 463647ad8475SAndrea Arcangeli 4637c6ddfb6cSHuang Ying process_huge_page(addr_hint, pages_per_huge_page, clear_subpage, page); 463847ad8475SAndrea Arcangeli } 463947ad8475SAndrea Arcangeli 464047ad8475SAndrea Arcangeli static void copy_user_gigantic_page(struct page *dst, struct page *src, 464147ad8475SAndrea Arcangeli unsigned long addr, 464247ad8475SAndrea Arcangeli struct vm_area_struct *vma, 464347ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 464447ad8475SAndrea Arcangeli { 464547ad8475SAndrea Arcangeli int i; 464647ad8475SAndrea Arcangeli struct page *dst_base = dst; 464747ad8475SAndrea Arcangeli struct page *src_base = src; 464847ad8475SAndrea Arcangeli 464947ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; ) { 465047ad8475SAndrea Arcangeli cond_resched(); 465147ad8475SAndrea Arcangeli copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma); 465247ad8475SAndrea Arcangeli 465347ad8475SAndrea Arcangeli i++; 465447ad8475SAndrea Arcangeli dst = mem_map_next(dst, dst_base, i); 465547ad8475SAndrea Arcangeli src = mem_map_next(src, src_base, i); 465647ad8475SAndrea Arcangeli } 465747ad8475SAndrea Arcangeli } 465847ad8475SAndrea Arcangeli 4659c9f4cd71SHuang Ying struct copy_subpage_arg { 4660c9f4cd71SHuang Ying struct page *dst; 4661c9f4cd71SHuang Ying struct page *src; 4662c9f4cd71SHuang Ying struct vm_area_struct *vma; 4663c9f4cd71SHuang Ying }; 4664c9f4cd71SHuang Ying 4665c9f4cd71SHuang Ying static void copy_subpage(unsigned long addr, int idx, void *arg) 4666c9f4cd71SHuang Ying { 4667c9f4cd71SHuang Ying struct copy_subpage_arg *copy_arg = arg; 4668c9f4cd71SHuang Ying 4669c9f4cd71SHuang Ying copy_user_highpage(copy_arg->dst + idx, copy_arg->src + idx, 4670c9f4cd71SHuang Ying addr, copy_arg->vma); 4671c9f4cd71SHuang Ying } 4672c9f4cd71SHuang Ying 467347ad8475SAndrea Arcangeli void copy_user_huge_page(struct page *dst, struct page *src, 4674c9f4cd71SHuang Ying unsigned long addr_hint, struct vm_area_struct *vma, 467547ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 467647ad8475SAndrea Arcangeli { 4677c9f4cd71SHuang Ying unsigned long addr = addr_hint & 4678c9f4cd71SHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 4679c9f4cd71SHuang Ying struct copy_subpage_arg arg = { 4680c9f4cd71SHuang Ying .dst = dst, 4681c9f4cd71SHuang Ying .src = src, 4682c9f4cd71SHuang Ying .vma = vma, 4683c9f4cd71SHuang Ying }; 468447ad8475SAndrea Arcangeli 468547ad8475SAndrea Arcangeli if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { 468647ad8475SAndrea Arcangeli copy_user_gigantic_page(dst, src, addr, vma, 468747ad8475SAndrea Arcangeli pages_per_huge_page); 468847ad8475SAndrea Arcangeli return; 468947ad8475SAndrea Arcangeli } 469047ad8475SAndrea Arcangeli 4691c9f4cd71SHuang Ying process_huge_page(addr_hint, pages_per_huge_page, copy_subpage, &arg); 469247ad8475SAndrea Arcangeli } 4693fa4d75c1SMike Kravetz 4694fa4d75c1SMike Kravetz long copy_huge_page_from_user(struct page *dst_page, 4695fa4d75c1SMike Kravetz const void __user *usr_src, 4696810a56b9SMike Kravetz unsigned int pages_per_huge_page, 4697810a56b9SMike Kravetz bool allow_pagefault) 4698fa4d75c1SMike Kravetz { 4699fa4d75c1SMike Kravetz void *src = (void *)usr_src; 4700fa4d75c1SMike Kravetz void *page_kaddr; 4701fa4d75c1SMike Kravetz unsigned long i, rc = 0; 4702fa4d75c1SMike Kravetz unsigned long ret_val = pages_per_huge_page * PAGE_SIZE; 4703fa4d75c1SMike Kravetz 4704fa4d75c1SMike Kravetz for (i = 0; i < pages_per_huge_page; i++) { 4705810a56b9SMike Kravetz if (allow_pagefault) 4706810a56b9SMike Kravetz page_kaddr = kmap(dst_page + i); 4707810a56b9SMike Kravetz else 4708fa4d75c1SMike Kravetz page_kaddr = kmap_atomic(dst_page + i); 4709fa4d75c1SMike Kravetz rc = copy_from_user(page_kaddr, 4710fa4d75c1SMike Kravetz (const void __user *)(src + i * PAGE_SIZE), 4711fa4d75c1SMike Kravetz PAGE_SIZE); 4712810a56b9SMike Kravetz if (allow_pagefault) 4713810a56b9SMike Kravetz kunmap(dst_page + i); 4714810a56b9SMike Kravetz else 4715fa4d75c1SMike Kravetz kunmap_atomic(page_kaddr); 4716fa4d75c1SMike Kravetz 4717fa4d75c1SMike Kravetz ret_val -= (PAGE_SIZE - rc); 4718fa4d75c1SMike Kravetz if (rc) 4719fa4d75c1SMike Kravetz break; 4720fa4d75c1SMike Kravetz 4721fa4d75c1SMike Kravetz cond_resched(); 4722fa4d75c1SMike Kravetz } 4723fa4d75c1SMike Kravetz return ret_val; 4724fa4d75c1SMike Kravetz } 472547ad8475SAndrea Arcangeli #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */ 472649076ec2SKirill A. Shutemov 472740b64acdSOlof Johansson #if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS 4728b35f1819SKirill A. Shutemov 4729b35f1819SKirill A. Shutemov static struct kmem_cache *page_ptl_cachep; 4730b35f1819SKirill A. Shutemov 4731b35f1819SKirill A. Shutemov void __init ptlock_cache_init(void) 4732b35f1819SKirill A. Shutemov { 4733b35f1819SKirill A. Shutemov page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0, 4734b35f1819SKirill A. Shutemov SLAB_PANIC, NULL); 4735b35f1819SKirill A. Shutemov } 4736b35f1819SKirill A. Shutemov 4737539edb58SPeter Zijlstra bool ptlock_alloc(struct page *page) 473849076ec2SKirill A. Shutemov { 473949076ec2SKirill A. Shutemov spinlock_t *ptl; 474049076ec2SKirill A. Shutemov 4741b35f1819SKirill A. Shutemov ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL); 474249076ec2SKirill A. Shutemov if (!ptl) 474349076ec2SKirill A. Shutemov return false; 4744539edb58SPeter Zijlstra page->ptl = ptl; 474549076ec2SKirill A. Shutemov return true; 474649076ec2SKirill A. Shutemov } 474749076ec2SKirill A. Shutemov 4748539edb58SPeter Zijlstra void ptlock_free(struct page *page) 474949076ec2SKirill A. Shutemov { 4750b35f1819SKirill A. Shutemov kmem_cache_free(page_ptl_cachep, page->ptl); 475149076ec2SKirill A. Shutemov } 475249076ec2SKirill A. Shutemov #endif 4753