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 75b3d1411bSJoel Fernandes (Google) #include <trace/events/kmem.h> 76b3d1411bSJoel 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 157e4dcad20SJoel Fernandes (Google) void mm_trace_rss_stat(struct mm_struct *mm, int member, long count) 158b3d1411bSJoel Fernandes (Google) { 159e4dcad20SJoel Fernandes (Google) trace_rss_stat(mm, member, count); 160b3d1411bSJoel 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; 6753cde287bSYu Zhao if (is_huge_zero_pmd(pmd)) 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 * 1667*574c5b3dSThomas Hellstrom * See vmf_insert_mixed_prot() for a discussion of the implication of using 1668*574c5b3dSThomas Hellstrom * a value of @pgprot different from that of @vma->vm_page_prot. 1669*574c5b3dSThomas Hellstrom * 1670ae2b01f3SMatthew Wilcox * Context: Process context. May allocate using %GFP_KERNEL. 1671f5e6d1d5SMatthew Wilcox * Return: vm_fault_t value. 1672f5e6d1d5SMatthew Wilcox */ 1673f5e6d1d5SMatthew Wilcox vm_fault_t vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr, 1674f5e6d1d5SMatthew Wilcox unsigned long pfn, pgprot_t pgprot) 1675f5e6d1d5SMatthew Wilcox { 16766d958546SMatthew Wilcox /* 16776d958546SMatthew Wilcox * Technically, architectures with pte_special can avoid all these 16786d958546SMatthew Wilcox * restrictions (same for remap_pfn_range). However we would like 16796d958546SMatthew Wilcox * consistency in testing and feature parity among all, so we should 16806d958546SMatthew Wilcox * try to keep these invariants in place for everybody. 16816d958546SMatthew Wilcox */ 16826d958546SMatthew Wilcox BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))); 16836d958546SMatthew Wilcox BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) == 16846d958546SMatthew Wilcox (VM_PFNMAP|VM_MIXEDMAP)); 16856d958546SMatthew Wilcox BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); 16866d958546SMatthew Wilcox BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn)); 16876d958546SMatthew Wilcox 16886d958546SMatthew Wilcox if (addr < vma->vm_start || addr >= vma->vm_end) 16896d958546SMatthew Wilcox return VM_FAULT_SIGBUS; 16906d958546SMatthew Wilcox 16916d958546SMatthew Wilcox if (!pfn_modify_allowed(pfn, pgprot)) 16926d958546SMatthew Wilcox return VM_FAULT_SIGBUS; 16936d958546SMatthew Wilcox 16946d958546SMatthew Wilcox track_pfn_insert(vma, &pgprot, __pfn_to_pfn_t(pfn, PFN_DEV)); 16956d958546SMatthew Wilcox 16969b5a8e00SMatthew Wilcox return insert_pfn(vma, addr, __pfn_to_pfn_t(pfn, PFN_DEV), pgprot, 16976d958546SMatthew Wilcox false); 1698f5e6d1d5SMatthew Wilcox } 1699f5e6d1d5SMatthew Wilcox EXPORT_SYMBOL(vmf_insert_pfn_prot); 1700e0dc0d8fSNick Piggin 1701ae2b01f3SMatthew Wilcox /** 1702ae2b01f3SMatthew Wilcox * vmf_insert_pfn - insert single pfn into user vma 1703ae2b01f3SMatthew Wilcox * @vma: user vma to map to 1704ae2b01f3SMatthew Wilcox * @addr: target user address of this page 1705ae2b01f3SMatthew Wilcox * @pfn: source kernel pfn 1706ae2b01f3SMatthew Wilcox * 1707ae2b01f3SMatthew Wilcox * Similar to vm_insert_page, this allows drivers to insert individual pages 1708ae2b01f3SMatthew Wilcox * they've allocated into a user vma. Same comments apply. 1709ae2b01f3SMatthew Wilcox * 1710ae2b01f3SMatthew Wilcox * This function should only be called from a vm_ops->fault handler, and 1711ae2b01f3SMatthew Wilcox * in that case the handler should return the result of this function. 1712ae2b01f3SMatthew Wilcox * 1713ae2b01f3SMatthew Wilcox * vma cannot be a COW mapping. 1714ae2b01f3SMatthew Wilcox * 1715ae2b01f3SMatthew Wilcox * As this is called only for pages that do not currently exist, we 1716ae2b01f3SMatthew Wilcox * do not need to flush old virtual caches or the TLB. 1717ae2b01f3SMatthew Wilcox * 1718ae2b01f3SMatthew Wilcox * Context: Process context. May allocate using %GFP_KERNEL. 1719ae2b01f3SMatthew Wilcox * Return: vm_fault_t value. 1720ae2b01f3SMatthew Wilcox */ 1721ae2b01f3SMatthew Wilcox vm_fault_t vmf_insert_pfn(struct vm_area_struct *vma, unsigned long addr, 1722ae2b01f3SMatthew Wilcox unsigned long pfn) 1723ae2b01f3SMatthew Wilcox { 1724ae2b01f3SMatthew Wilcox return vmf_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot); 1725ae2b01f3SMatthew Wilcox } 1726ae2b01f3SMatthew Wilcox EXPORT_SYMBOL(vmf_insert_pfn); 1727ae2b01f3SMatthew Wilcox 1728785a3fabSDan Williams static bool vm_mixed_ok(struct vm_area_struct *vma, pfn_t pfn) 1729785a3fabSDan Williams { 1730785a3fabSDan Williams /* these checks mirror the abort conditions in vm_normal_page */ 1731785a3fabSDan Williams if (vma->vm_flags & VM_MIXEDMAP) 1732785a3fabSDan Williams return true; 1733785a3fabSDan Williams if (pfn_t_devmap(pfn)) 1734785a3fabSDan Williams return true; 1735785a3fabSDan Williams if (pfn_t_special(pfn)) 1736785a3fabSDan Williams return true; 1737785a3fabSDan Williams if (is_zero_pfn(pfn_t_to_pfn(pfn))) 1738785a3fabSDan Williams return true; 1739785a3fabSDan Williams return false; 1740785a3fabSDan Williams } 1741785a3fabSDan Williams 174279f3aa5bSMatthew Wilcox static vm_fault_t __vm_insert_mixed(struct vm_area_struct *vma, 1743*574c5b3dSThomas Hellstrom unsigned long addr, pfn_t pfn, pgprot_t pgprot, 1744*574c5b3dSThomas Hellstrom bool mkwrite) 1745423bad60SNick Piggin { 174679f3aa5bSMatthew Wilcox int err; 174787744ab3SDan Williams 1748785a3fabSDan Williams BUG_ON(!vm_mixed_ok(vma, pfn)); 1749423bad60SNick Piggin 1750423bad60SNick Piggin if (addr < vma->vm_start || addr >= vma->vm_end) 175179f3aa5bSMatthew Wilcox return VM_FAULT_SIGBUS; 1752308a047cSBorislav Petkov 1753308a047cSBorislav Petkov track_pfn_insert(vma, &pgprot, pfn); 1754423bad60SNick Piggin 175542e4089cSAndi Kleen if (!pfn_modify_allowed(pfn_t_to_pfn(pfn), pgprot)) 175679f3aa5bSMatthew Wilcox return VM_FAULT_SIGBUS; 175742e4089cSAndi Kleen 1758423bad60SNick Piggin /* 1759423bad60SNick Piggin * If we don't have pte special, then we have to use the pfn_valid() 1760423bad60SNick Piggin * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must* 1761423bad60SNick Piggin * refcount the page if pfn_valid is true (hence insert_page rather 176262eede62SHugh Dickins * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP 176362eede62SHugh Dickins * without pte special, it would there be refcounted as a normal page. 1764423bad60SNick Piggin */ 176500b3a331SLaurent Dufour if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) && 176600b3a331SLaurent Dufour !pfn_t_devmap(pfn) && pfn_t_valid(pfn)) { 1767423bad60SNick Piggin struct page *page; 1768423bad60SNick Piggin 176903fc2da6SDan Williams /* 177003fc2da6SDan Williams * At this point we are committed to insert_page() 177103fc2da6SDan Williams * regardless of whether the caller specified flags that 177203fc2da6SDan Williams * result in pfn_t_has_page() == false. 177303fc2da6SDan Williams */ 177403fc2da6SDan Williams page = pfn_to_page(pfn_t_to_pfn(pfn)); 177579f3aa5bSMatthew Wilcox err = insert_page(vma, addr, page, pgprot); 177679f3aa5bSMatthew Wilcox } else { 17779b5a8e00SMatthew Wilcox return insert_pfn(vma, addr, pfn, pgprot, mkwrite); 1778423bad60SNick Piggin } 1779b2770da6SRoss Zwisler 17805d747637SMatthew Wilcox if (err == -ENOMEM) 17815d747637SMatthew Wilcox return VM_FAULT_OOM; 17825d747637SMatthew Wilcox if (err < 0 && err != -EBUSY) 17835d747637SMatthew Wilcox return VM_FAULT_SIGBUS; 17845d747637SMatthew Wilcox 17855d747637SMatthew Wilcox return VM_FAULT_NOPAGE; 1786423bad60SNick Piggin } 178779f3aa5bSMatthew Wilcox 1788*574c5b3dSThomas Hellstrom /** 1789*574c5b3dSThomas Hellstrom * vmf_insert_mixed_prot - insert single pfn into user vma with specified pgprot 1790*574c5b3dSThomas Hellstrom * @vma: user vma to map to 1791*574c5b3dSThomas Hellstrom * @addr: target user address of this page 1792*574c5b3dSThomas Hellstrom * @pfn: source kernel pfn 1793*574c5b3dSThomas Hellstrom * @pgprot: pgprot flags for the inserted page 1794*574c5b3dSThomas Hellstrom * 1795*574c5b3dSThomas Hellstrom * This is exactly like vmf_insert_mixed(), except that it allows drivers to 1796*574c5b3dSThomas Hellstrom * to override pgprot on a per-page basis. 1797*574c5b3dSThomas Hellstrom * 1798*574c5b3dSThomas Hellstrom * Typically this function should be used by drivers to set caching- and 1799*574c5b3dSThomas Hellstrom * encryption bits different than those of @vma->vm_page_prot, because 1800*574c5b3dSThomas Hellstrom * the caching- or encryption mode may not be known at mmap() time. 1801*574c5b3dSThomas Hellstrom * This is ok as long as @vma->vm_page_prot is not used by the core vm 1802*574c5b3dSThomas Hellstrom * to set caching and encryption bits for those vmas (except for COW pages). 1803*574c5b3dSThomas Hellstrom * This is ensured by core vm only modifying these page table entries using 1804*574c5b3dSThomas Hellstrom * functions that don't touch caching- or encryption bits, using pte_modify() 1805*574c5b3dSThomas Hellstrom * if needed. (See for example mprotect()). 1806*574c5b3dSThomas Hellstrom * Also when new page-table entries are created, this is only done using the 1807*574c5b3dSThomas Hellstrom * fault() callback, and never using the value of vma->vm_page_prot, 1808*574c5b3dSThomas Hellstrom * except for page-table entries that point to anonymous pages as the result 1809*574c5b3dSThomas Hellstrom * of COW. 1810*574c5b3dSThomas Hellstrom * 1811*574c5b3dSThomas Hellstrom * Context: Process context. May allocate using %GFP_KERNEL. 1812*574c5b3dSThomas Hellstrom * Return: vm_fault_t value. 1813*574c5b3dSThomas Hellstrom */ 1814*574c5b3dSThomas Hellstrom vm_fault_t vmf_insert_mixed_prot(struct vm_area_struct *vma, unsigned long addr, 1815*574c5b3dSThomas Hellstrom pfn_t pfn, pgprot_t pgprot) 1816*574c5b3dSThomas Hellstrom { 1817*574c5b3dSThomas Hellstrom return __vm_insert_mixed(vma, addr, pfn, pgprot, false); 1818*574c5b3dSThomas Hellstrom } 1819*574c5b3dSThomas Hellstrom 182079f3aa5bSMatthew Wilcox vm_fault_t vmf_insert_mixed(struct vm_area_struct *vma, unsigned long addr, 182179f3aa5bSMatthew Wilcox pfn_t pfn) 182279f3aa5bSMatthew Wilcox { 1823*574c5b3dSThomas Hellstrom return __vm_insert_mixed(vma, addr, pfn, vma->vm_page_prot, false); 182479f3aa5bSMatthew Wilcox } 18255d747637SMatthew Wilcox EXPORT_SYMBOL(vmf_insert_mixed); 1826423bad60SNick Piggin 1827ab77dab4SSouptick Joarder /* 1828ab77dab4SSouptick Joarder * If the insertion of PTE failed because someone else already added a 1829ab77dab4SSouptick Joarder * different entry in the mean time, we treat that as success as we assume 1830ab77dab4SSouptick Joarder * the same entry was actually inserted. 1831ab77dab4SSouptick Joarder */ 1832ab77dab4SSouptick Joarder vm_fault_t vmf_insert_mixed_mkwrite(struct vm_area_struct *vma, 1833ab77dab4SSouptick Joarder unsigned long addr, pfn_t pfn) 1834b2770da6SRoss Zwisler { 1835*574c5b3dSThomas Hellstrom return __vm_insert_mixed(vma, addr, pfn, vma->vm_page_prot, true); 1836b2770da6SRoss Zwisler } 1837ab77dab4SSouptick Joarder EXPORT_SYMBOL(vmf_insert_mixed_mkwrite); 1838b2770da6SRoss Zwisler 1839a145dd41SLinus Torvalds /* 18401da177e4SLinus Torvalds * maps a range of physical memory into the requested pages. the old 18411da177e4SLinus Torvalds * mappings are removed. any references to nonexistent pages results 18421da177e4SLinus Torvalds * in null mappings (currently treated as "copy-on-access") 18431da177e4SLinus Torvalds */ 18441da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd, 18451da177e4SLinus Torvalds unsigned long addr, unsigned long end, 18461da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 18471da177e4SLinus Torvalds { 18481da177e4SLinus Torvalds pte_t *pte; 1849c74df32cSHugh Dickins spinlock_t *ptl; 185042e4089cSAndi Kleen int err = 0; 18511da177e4SLinus Torvalds 1852c74df32cSHugh Dickins pte = pte_alloc_map_lock(mm, pmd, addr, &ptl); 18531da177e4SLinus Torvalds if (!pte) 18541da177e4SLinus Torvalds return -ENOMEM; 18556606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 18561da177e4SLinus Torvalds do { 18571da177e4SLinus Torvalds BUG_ON(!pte_none(*pte)); 185842e4089cSAndi Kleen if (!pfn_modify_allowed(pfn, prot)) { 185942e4089cSAndi Kleen err = -EACCES; 186042e4089cSAndi Kleen break; 186142e4089cSAndi Kleen } 18627e675137SNick Piggin set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot))); 18631da177e4SLinus Torvalds pfn++; 18641da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 18656606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 1866c74df32cSHugh Dickins pte_unmap_unlock(pte - 1, ptl); 186742e4089cSAndi Kleen return err; 18681da177e4SLinus Torvalds } 18691da177e4SLinus Torvalds 18701da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud, 18711da177e4SLinus Torvalds unsigned long addr, unsigned long end, 18721da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 18731da177e4SLinus Torvalds { 18741da177e4SLinus Torvalds pmd_t *pmd; 18751da177e4SLinus Torvalds unsigned long next; 187642e4089cSAndi Kleen int err; 18771da177e4SLinus Torvalds 18781da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 18791da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, addr); 18801da177e4SLinus Torvalds if (!pmd) 18811da177e4SLinus Torvalds return -ENOMEM; 1882f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 18831da177e4SLinus Torvalds do { 18841da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 188542e4089cSAndi Kleen err = remap_pte_range(mm, pmd, addr, next, 188642e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 188742e4089cSAndi Kleen if (err) 188842e4089cSAndi Kleen return err; 18891da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 18901da177e4SLinus Torvalds return 0; 18911da177e4SLinus Torvalds } 18921da177e4SLinus Torvalds 1893c2febafcSKirill A. Shutemov static inline int remap_pud_range(struct mm_struct *mm, p4d_t *p4d, 18941da177e4SLinus Torvalds unsigned long addr, unsigned long end, 18951da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 18961da177e4SLinus Torvalds { 18971da177e4SLinus Torvalds pud_t *pud; 18981da177e4SLinus Torvalds unsigned long next; 189942e4089cSAndi Kleen int err; 19001da177e4SLinus Torvalds 19011da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 1902c2febafcSKirill A. Shutemov pud = pud_alloc(mm, p4d, addr); 19031da177e4SLinus Torvalds if (!pud) 19041da177e4SLinus Torvalds return -ENOMEM; 19051da177e4SLinus Torvalds do { 19061da177e4SLinus Torvalds next = pud_addr_end(addr, end); 190742e4089cSAndi Kleen err = remap_pmd_range(mm, pud, addr, next, 190842e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 190942e4089cSAndi Kleen if (err) 191042e4089cSAndi Kleen return err; 19111da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 19121da177e4SLinus Torvalds return 0; 19131da177e4SLinus Torvalds } 19141da177e4SLinus Torvalds 1915c2febafcSKirill A. Shutemov static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd, 1916c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 1917c2febafcSKirill A. Shutemov unsigned long pfn, pgprot_t prot) 1918c2febafcSKirill A. Shutemov { 1919c2febafcSKirill A. Shutemov p4d_t *p4d; 1920c2febafcSKirill A. Shutemov unsigned long next; 192142e4089cSAndi Kleen int err; 1922c2febafcSKirill A. Shutemov 1923c2febafcSKirill A. Shutemov pfn -= addr >> PAGE_SHIFT; 1924c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, addr); 1925c2febafcSKirill A. Shutemov if (!p4d) 1926c2febafcSKirill A. Shutemov return -ENOMEM; 1927c2febafcSKirill A. Shutemov do { 1928c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 192942e4089cSAndi Kleen err = remap_pud_range(mm, p4d, addr, next, 193042e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 193142e4089cSAndi Kleen if (err) 193242e4089cSAndi Kleen return err; 1933c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 1934c2febafcSKirill A. Shutemov return 0; 1935c2febafcSKirill A. Shutemov } 1936c2febafcSKirill A. Shutemov 1937bfa5bf6dSRolf Eike Beer /** 1938bfa5bf6dSRolf Eike Beer * remap_pfn_range - remap kernel memory to userspace 1939bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 1940bfa5bf6dSRolf Eike Beer * @addr: target user address to start at 1941bfa5bf6dSRolf Eike Beer * @pfn: physical address of kernel memory 1942bfa5bf6dSRolf Eike Beer * @size: size of map area 1943bfa5bf6dSRolf Eike Beer * @prot: page protection flags for this mapping 1944bfa5bf6dSRolf Eike Beer * 1945bfa5bf6dSRolf Eike Beer * Note: this is only safe if the mm semaphore is held when called. 1946a862f68aSMike Rapoport * 1947a862f68aSMike Rapoport * Return: %0 on success, negative error code otherwise. 1948bfa5bf6dSRolf Eike Beer */ 19491da177e4SLinus Torvalds int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 19501da177e4SLinus Torvalds unsigned long pfn, unsigned long size, pgprot_t prot) 19511da177e4SLinus Torvalds { 19521da177e4SLinus Torvalds pgd_t *pgd; 19531da177e4SLinus Torvalds unsigned long next; 19542d15cab8SHugh Dickins unsigned long end = addr + PAGE_ALIGN(size); 19551da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 1956d5957d2fSYongji Xie unsigned long remap_pfn = pfn; 19571da177e4SLinus Torvalds int err; 19581da177e4SLinus Torvalds 19591da177e4SLinus Torvalds /* 19601da177e4SLinus Torvalds * Physically remapped pages are special. Tell the 19611da177e4SLinus Torvalds * rest of the world about it: 19621da177e4SLinus Torvalds * VM_IO tells people not to look at these pages 19631da177e4SLinus Torvalds * (accesses can have side effects). 19646aab341eSLinus Torvalds * VM_PFNMAP tells the core MM that the base pages are just 19656aab341eSLinus Torvalds * raw PFN mappings, and do not have a "struct page" associated 19666aab341eSLinus Torvalds * with them. 1967314e51b9SKonstantin Khlebnikov * VM_DONTEXPAND 1968314e51b9SKonstantin Khlebnikov * Disable vma merging and expanding with mremap(). 1969314e51b9SKonstantin Khlebnikov * VM_DONTDUMP 1970314e51b9SKonstantin Khlebnikov * Omit vma from core dump, even when VM_IO turned off. 1971fb155c16SLinus Torvalds * 1972fb155c16SLinus Torvalds * There's a horrible special case to handle copy-on-write 1973fb155c16SLinus Torvalds * behaviour that some programs depend on. We mark the "original" 1974fb155c16SLinus Torvalds * un-COW'ed pages by matching them up with "vma->vm_pgoff". 1975b3b9c293SKonstantin Khlebnikov * See vm_normal_page() for details. 19761da177e4SLinus Torvalds */ 1977b3b9c293SKonstantin Khlebnikov if (is_cow_mapping(vma->vm_flags)) { 1978b3b9c293SKonstantin Khlebnikov if (addr != vma->vm_start || end != vma->vm_end) 1979b3b9c293SKonstantin Khlebnikov return -EINVAL; 19806aab341eSLinus Torvalds vma->vm_pgoff = pfn; 1981b3b9c293SKonstantin Khlebnikov } 1982b3b9c293SKonstantin Khlebnikov 1983d5957d2fSYongji Xie err = track_pfn_remap(vma, &prot, remap_pfn, addr, PAGE_ALIGN(size)); 1984b3b9c293SKonstantin Khlebnikov if (err) 19853c8bb73aSvenkatesh.pallipadi@intel.com return -EINVAL; 1986fb155c16SLinus Torvalds 1987314e51b9SKonstantin Khlebnikov vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; 19881da177e4SLinus Torvalds 19891da177e4SLinus Torvalds BUG_ON(addr >= end); 19901da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 19911da177e4SLinus Torvalds pgd = pgd_offset(mm, addr); 19921da177e4SLinus Torvalds flush_cache_range(vma, addr, end); 19931da177e4SLinus Torvalds do { 19941da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 1995c2febafcSKirill A. Shutemov err = remap_p4d_range(mm, pgd, addr, next, 19961da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot); 19971da177e4SLinus Torvalds if (err) 19981da177e4SLinus Torvalds break; 19991da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 20002ab64037Svenkatesh.pallipadi@intel.com 20012ab64037Svenkatesh.pallipadi@intel.com if (err) 2002d5957d2fSYongji Xie untrack_pfn(vma, remap_pfn, PAGE_ALIGN(size)); 20032ab64037Svenkatesh.pallipadi@intel.com 20041da177e4SLinus Torvalds return err; 20051da177e4SLinus Torvalds } 20061da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range); 20071da177e4SLinus Torvalds 2008b4cbb197SLinus Torvalds /** 2009b4cbb197SLinus Torvalds * vm_iomap_memory - remap memory to userspace 2010b4cbb197SLinus Torvalds * @vma: user vma to map to 2011b4cbb197SLinus Torvalds * @start: start of area 2012b4cbb197SLinus Torvalds * @len: size of area 2013b4cbb197SLinus Torvalds * 2014b4cbb197SLinus Torvalds * This is a simplified io_remap_pfn_range() for common driver use. The 2015b4cbb197SLinus Torvalds * driver just needs to give us the physical memory range to be mapped, 2016b4cbb197SLinus Torvalds * we'll figure out the rest from the vma information. 2017b4cbb197SLinus Torvalds * 2018b4cbb197SLinus Torvalds * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get 2019b4cbb197SLinus Torvalds * whatever write-combining details or similar. 2020a862f68aSMike Rapoport * 2021a862f68aSMike Rapoport * Return: %0 on success, negative error code otherwise. 2022b4cbb197SLinus Torvalds */ 2023b4cbb197SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len) 2024b4cbb197SLinus Torvalds { 2025b4cbb197SLinus Torvalds unsigned long vm_len, pfn, pages; 2026b4cbb197SLinus Torvalds 2027b4cbb197SLinus Torvalds /* Check that the physical memory area passed in looks valid */ 2028b4cbb197SLinus Torvalds if (start + len < start) 2029b4cbb197SLinus Torvalds return -EINVAL; 2030b4cbb197SLinus Torvalds /* 2031b4cbb197SLinus Torvalds * You *really* shouldn't map things that aren't page-aligned, 2032b4cbb197SLinus Torvalds * but we've historically allowed it because IO memory might 2033b4cbb197SLinus Torvalds * just have smaller alignment. 2034b4cbb197SLinus Torvalds */ 2035b4cbb197SLinus Torvalds len += start & ~PAGE_MASK; 2036b4cbb197SLinus Torvalds pfn = start >> PAGE_SHIFT; 2037b4cbb197SLinus Torvalds pages = (len + ~PAGE_MASK) >> PAGE_SHIFT; 2038b4cbb197SLinus Torvalds if (pfn + pages < pfn) 2039b4cbb197SLinus Torvalds return -EINVAL; 2040b4cbb197SLinus Torvalds 2041b4cbb197SLinus Torvalds /* We start the mapping 'vm_pgoff' pages into the area */ 2042b4cbb197SLinus Torvalds if (vma->vm_pgoff > pages) 2043b4cbb197SLinus Torvalds return -EINVAL; 2044b4cbb197SLinus Torvalds pfn += vma->vm_pgoff; 2045b4cbb197SLinus Torvalds pages -= vma->vm_pgoff; 2046b4cbb197SLinus Torvalds 2047b4cbb197SLinus Torvalds /* Can we fit all of the mapping? */ 2048b4cbb197SLinus Torvalds vm_len = vma->vm_end - vma->vm_start; 2049b4cbb197SLinus Torvalds if (vm_len >> PAGE_SHIFT > pages) 2050b4cbb197SLinus Torvalds return -EINVAL; 2051b4cbb197SLinus Torvalds 2052b4cbb197SLinus Torvalds /* Ok, let it rip */ 2053b4cbb197SLinus Torvalds return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot); 2054b4cbb197SLinus Torvalds } 2055b4cbb197SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory); 2056b4cbb197SLinus Torvalds 2057aee16b3cSJeremy Fitzhardinge static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd, 2058aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2059aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 2060aee16b3cSJeremy Fitzhardinge { 2061aee16b3cSJeremy Fitzhardinge pte_t *pte; 2062aee16b3cSJeremy Fitzhardinge int err; 206394909914SBorislav Petkov spinlock_t *uninitialized_var(ptl); 2064aee16b3cSJeremy Fitzhardinge 2065aee16b3cSJeremy Fitzhardinge pte = (mm == &init_mm) ? 2066aee16b3cSJeremy Fitzhardinge pte_alloc_kernel(pmd, addr) : 2067aee16b3cSJeremy Fitzhardinge pte_alloc_map_lock(mm, pmd, addr, &ptl); 2068aee16b3cSJeremy Fitzhardinge if (!pte) 2069aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2070aee16b3cSJeremy Fitzhardinge 2071aee16b3cSJeremy Fitzhardinge BUG_ON(pmd_huge(*pmd)); 2072aee16b3cSJeremy Fitzhardinge 207338e0edb1SJeremy Fitzhardinge arch_enter_lazy_mmu_mode(); 207438e0edb1SJeremy Fitzhardinge 2075aee16b3cSJeremy Fitzhardinge do { 20768b1e0f81SAnshuman Khandual err = fn(pte++, addr, data); 2077aee16b3cSJeremy Fitzhardinge if (err) 2078aee16b3cSJeremy Fitzhardinge break; 2079c36987e2SDaisuke Nishimura } while (addr += PAGE_SIZE, addr != end); 2080aee16b3cSJeremy Fitzhardinge 208138e0edb1SJeremy Fitzhardinge arch_leave_lazy_mmu_mode(); 208238e0edb1SJeremy Fitzhardinge 2083aee16b3cSJeremy Fitzhardinge if (mm != &init_mm) 2084aee16b3cSJeremy Fitzhardinge pte_unmap_unlock(pte-1, ptl); 2085aee16b3cSJeremy Fitzhardinge return err; 2086aee16b3cSJeremy Fitzhardinge } 2087aee16b3cSJeremy Fitzhardinge 2088aee16b3cSJeremy Fitzhardinge static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud, 2089aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2090aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 2091aee16b3cSJeremy Fitzhardinge { 2092aee16b3cSJeremy Fitzhardinge pmd_t *pmd; 2093aee16b3cSJeremy Fitzhardinge unsigned long next; 2094aee16b3cSJeremy Fitzhardinge int err; 2095aee16b3cSJeremy Fitzhardinge 2096ceb86879SAndi Kleen BUG_ON(pud_huge(*pud)); 2097ceb86879SAndi Kleen 2098aee16b3cSJeremy Fitzhardinge pmd = pmd_alloc(mm, pud, addr); 2099aee16b3cSJeremy Fitzhardinge if (!pmd) 2100aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2101aee16b3cSJeremy Fitzhardinge do { 2102aee16b3cSJeremy Fitzhardinge next = pmd_addr_end(addr, end); 2103aee16b3cSJeremy Fitzhardinge err = apply_to_pte_range(mm, pmd, addr, next, fn, data); 2104aee16b3cSJeremy Fitzhardinge if (err) 2105aee16b3cSJeremy Fitzhardinge break; 2106aee16b3cSJeremy Fitzhardinge } while (pmd++, addr = next, addr != end); 2107aee16b3cSJeremy Fitzhardinge return err; 2108aee16b3cSJeremy Fitzhardinge } 2109aee16b3cSJeremy Fitzhardinge 2110c2febafcSKirill A. Shutemov static int apply_to_pud_range(struct mm_struct *mm, p4d_t *p4d, 2111aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2112aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 2113aee16b3cSJeremy Fitzhardinge { 2114aee16b3cSJeremy Fitzhardinge pud_t *pud; 2115aee16b3cSJeremy Fitzhardinge unsigned long next; 2116aee16b3cSJeremy Fitzhardinge int err; 2117aee16b3cSJeremy Fitzhardinge 2118c2febafcSKirill A. Shutemov pud = pud_alloc(mm, p4d, addr); 2119aee16b3cSJeremy Fitzhardinge if (!pud) 2120aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2121aee16b3cSJeremy Fitzhardinge do { 2122aee16b3cSJeremy Fitzhardinge next = pud_addr_end(addr, end); 2123aee16b3cSJeremy Fitzhardinge err = apply_to_pmd_range(mm, pud, addr, next, fn, data); 2124aee16b3cSJeremy Fitzhardinge if (err) 2125aee16b3cSJeremy Fitzhardinge break; 2126aee16b3cSJeremy Fitzhardinge } while (pud++, addr = next, addr != end); 2127aee16b3cSJeremy Fitzhardinge return err; 2128aee16b3cSJeremy Fitzhardinge } 2129aee16b3cSJeremy Fitzhardinge 2130c2febafcSKirill A. Shutemov static int apply_to_p4d_range(struct mm_struct *mm, pgd_t *pgd, 2131c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 2132c2febafcSKirill A. Shutemov pte_fn_t fn, void *data) 2133c2febafcSKirill A. Shutemov { 2134c2febafcSKirill A. Shutemov p4d_t *p4d; 2135c2febafcSKirill A. Shutemov unsigned long next; 2136c2febafcSKirill A. Shutemov int err; 2137c2febafcSKirill A. Shutemov 2138c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, addr); 2139c2febafcSKirill A. Shutemov if (!p4d) 2140c2febafcSKirill A. Shutemov return -ENOMEM; 2141c2febafcSKirill A. Shutemov do { 2142c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 2143c2febafcSKirill A. Shutemov err = apply_to_pud_range(mm, p4d, addr, next, fn, data); 2144c2febafcSKirill A. Shutemov if (err) 2145c2febafcSKirill A. Shutemov break; 2146c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 2147c2febafcSKirill A. Shutemov return err; 2148c2febafcSKirill A. Shutemov } 2149c2febafcSKirill A. Shutemov 2150aee16b3cSJeremy Fitzhardinge /* 2151aee16b3cSJeremy Fitzhardinge * Scan a region of virtual memory, filling in page tables as necessary 2152aee16b3cSJeremy Fitzhardinge * and calling a provided function on each leaf page table. 2153aee16b3cSJeremy Fitzhardinge */ 2154aee16b3cSJeremy Fitzhardinge int apply_to_page_range(struct mm_struct *mm, unsigned long addr, 2155aee16b3cSJeremy Fitzhardinge unsigned long size, pte_fn_t fn, void *data) 2156aee16b3cSJeremy Fitzhardinge { 2157aee16b3cSJeremy Fitzhardinge pgd_t *pgd; 2158aee16b3cSJeremy Fitzhardinge unsigned long next; 215957250a5bSJeremy Fitzhardinge unsigned long end = addr + size; 2160aee16b3cSJeremy Fitzhardinge int err; 2161aee16b3cSJeremy Fitzhardinge 21629cb65bc3SMika Penttilä if (WARN_ON(addr >= end)) 21639cb65bc3SMika Penttilä return -EINVAL; 21649cb65bc3SMika Penttilä 2165aee16b3cSJeremy Fitzhardinge pgd = pgd_offset(mm, addr); 2166aee16b3cSJeremy Fitzhardinge do { 2167aee16b3cSJeremy Fitzhardinge next = pgd_addr_end(addr, end); 2168c2febafcSKirill A. Shutemov err = apply_to_p4d_range(mm, pgd, addr, next, fn, data); 2169aee16b3cSJeremy Fitzhardinge if (err) 2170aee16b3cSJeremy Fitzhardinge break; 2171aee16b3cSJeremy Fitzhardinge } while (pgd++, addr = next, addr != end); 217257250a5bSJeremy Fitzhardinge 2173aee16b3cSJeremy Fitzhardinge return err; 2174aee16b3cSJeremy Fitzhardinge } 2175aee16b3cSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(apply_to_page_range); 2176aee16b3cSJeremy Fitzhardinge 21771da177e4SLinus Torvalds /* 21789b4bdd2fSKirill A. Shutemov * handle_pte_fault chooses page fault handler according to an entry which was 21799b4bdd2fSKirill A. Shutemov * read non-atomically. Before making any commitment, on those architectures 21809b4bdd2fSKirill A. Shutemov * or configurations (e.g. i386 with PAE) which might give a mix of unmatched 21819b4bdd2fSKirill A. Shutemov * parts, do_swap_page must check under lock before unmapping the pte and 21829b4bdd2fSKirill A. Shutemov * proceeding (but do_wp_page is only called after already making such a check; 2183a335b2e1SRyota Ozaki * and do_anonymous_page can safely check later on). 21848f4e2101SHugh Dickins */ 21854c21e2f2SHugh Dickins static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd, 21868f4e2101SHugh Dickins pte_t *page_table, pte_t orig_pte) 21878f4e2101SHugh Dickins { 21888f4e2101SHugh Dickins int same = 1; 21898f4e2101SHugh Dickins #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT) 21908f4e2101SHugh Dickins if (sizeof(pte_t) > sizeof(unsigned long)) { 21914c21e2f2SHugh Dickins spinlock_t *ptl = pte_lockptr(mm, pmd); 21924c21e2f2SHugh Dickins spin_lock(ptl); 21938f4e2101SHugh Dickins same = pte_same(*page_table, orig_pte); 21944c21e2f2SHugh Dickins spin_unlock(ptl); 21958f4e2101SHugh Dickins } 21968f4e2101SHugh Dickins #endif 21978f4e2101SHugh Dickins pte_unmap(page_table); 21988f4e2101SHugh Dickins return same; 21998f4e2101SHugh Dickins } 22008f4e2101SHugh Dickins 220183d116c5SJia He static inline bool cow_user_page(struct page *dst, struct page *src, 220283d116c5SJia He struct vm_fault *vmf) 22036aab341eSLinus Torvalds { 220483d116c5SJia He bool ret; 220583d116c5SJia He void *kaddr; 220683d116c5SJia He void __user *uaddr; 220783d116c5SJia He bool force_mkyoung; 220883d116c5SJia He struct vm_area_struct *vma = vmf->vma; 220983d116c5SJia He struct mm_struct *mm = vma->vm_mm; 221083d116c5SJia He unsigned long addr = vmf->address; 221183d116c5SJia He 22120abdd7a8SDan Williams debug_dma_assert_idle(src); 22130abdd7a8SDan Williams 221483d116c5SJia He if (likely(src)) { 221583d116c5SJia He copy_user_highpage(dst, src, addr, vma); 221683d116c5SJia He return true; 221783d116c5SJia He } 221883d116c5SJia He 22196aab341eSLinus Torvalds /* 22206aab341eSLinus Torvalds * If the source page was a PFN mapping, we don't have 22216aab341eSLinus Torvalds * a "struct page" for it. We do a best-effort copy by 22226aab341eSLinus Torvalds * just copying from the original user address. If that 22236aab341eSLinus Torvalds * fails, we just zero-fill it. Live with it. 22246aab341eSLinus Torvalds */ 222583d116c5SJia He kaddr = kmap_atomic(dst); 222683d116c5SJia He uaddr = (void __user *)(addr & PAGE_MASK); 222783d116c5SJia He 222883d116c5SJia He /* 222983d116c5SJia He * On architectures with software "accessed" bits, we would 223083d116c5SJia He * take a double page fault, so mark it accessed here. 223183d116c5SJia He */ 223283d116c5SJia He force_mkyoung = arch_faults_on_old_pte() && !pte_young(vmf->orig_pte); 223383d116c5SJia He if (force_mkyoung) { 223483d116c5SJia He pte_t entry; 223583d116c5SJia He 223683d116c5SJia He vmf->pte = pte_offset_map_lock(mm, vmf->pmd, addr, &vmf->ptl); 223783d116c5SJia He if (!likely(pte_same(*vmf->pte, vmf->orig_pte))) { 223883d116c5SJia He /* 223983d116c5SJia He * Other thread has already handled the fault 224083d116c5SJia He * and we don't need to do anything. If it's 224183d116c5SJia He * not the case, the fault will be triggered 224283d116c5SJia He * again on the same address. 224383d116c5SJia He */ 224483d116c5SJia He ret = false; 224583d116c5SJia He goto pte_unlock; 224683d116c5SJia He } 224783d116c5SJia He 224883d116c5SJia He entry = pte_mkyoung(vmf->orig_pte); 224983d116c5SJia He if (ptep_set_access_flags(vma, addr, vmf->pte, entry, 0)) 225083d116c5SJia He update_mmu_cache(vma, addr, vmf->pte); 225183d116c5SJia He } 22525d2a2dbbSLinus Torvalds 22535d2a2dbbSLinus Torvalds /* 22545d2a2dbbSLinus Torvalds * This really shouldn't fail, because the page is there 22555d2a2dbbSLinus Torvalds * in the page tables. But it might just be unreadable, 22565d2a2dbbSLinus Torvalds * in which case we just give up and fill the result with 22575d2a2dbbSLinus Torvalds * zeroes. 22585d2a2dbbSLinus Torvalds */ 225983d116c5SJia He if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) { 226083d116c5SJia He /* 226183d116c5SJia He * Give a warn in case there can be some obscure 226283d116c5SJia He * use-case 226383d116c5SJia He */ 226483d116c5SJia He WARN_ON_ONCE(1); 22653ecb01dfSJan Beulich clear_page(kaddr); 226683d116c5SJia He } 226783d116c5SJia He 226883d116c5SJia He ret = true; 226983d116c5SJia He 227083d116c5SJia He pte_unlock: 227183d116c5SJia He if (force_mkyoung) 227283d116c5SJia He pte_unmap_unlock(vmf->pte, vmf->ptl); 22739b04c5feSCong Wang kunmap_atomic(kaddr); 2274c4ec7b0dSDmitriy Monakhov flush_dcache_page(dst); 227583d116c5SJia He 227683d116c5SJia He return ret; 22776aab341eSLinus Torvalds } 22786aab341eSLinus Torvalds 2279c20cd45eSMichal Hocko static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma) 2280c20cd45eSMichal Hocko { 2281c20cd45eSMichal Hocko struct file *vm_file = vma->vm_file; 2282c20cd45eSMichal Hocko 2283c20cd45eSMichal Hocko if (vm_file) 2284c20cd45eSMichal Hocko return mapping_gfp_mask(vm_file->f_mapping) | __GFP_FS | __GFP_IO; 2285c20cd45eSMichal Hocko 2286c20cd45eSMichal Hocko /* 2287c20cd45eSMichal Hocko * Special mappings (e.g. VDSO) do not have any file so fake 2288c20cd45eSMichal Hocko * a default GFP_KERNEL for them. 2289c20cd45eSMichal Hocko */ 2290c20cd45eSMichal Hocko return GFP_KERNEL; 2291c20cd45eSMichal Hocko } 2292c20cd45eSMichal Hocko 22931da177e4SLinus Torvalds /* 2294fb09a464SKirill A. Shutemov * Notify the address space that the page is about to become writable so that 2295fb09a464SKirill A. Shutemov * it can prohibit this or wait for the page to get into an appropriate state. 2296fb09a464SKirill A. Shutemov * 2297fb09a464SKirill A. Shutemov * We do this without the lock held, so that it can sleep if it needs to. 2298fb09a464SKirill A. Shutemov */ 22992b740303SSouptick Joarder static vm_fault_t do_page_mkwrite(struct vm_fault *vmf) 2300fb09a464SKirill A. Shutemov { 23012b740303SSouptick Joarder vm_fault_t ret; 230238b8cb7fSJan Kara struct page *page = vmf->page; 230338b8cb7fSJan Kara unsigned int old_flags = vmf->flags; 2304fb09a464SKirill A. Shutemov 230538b8cb7fSJan Kara vmf->flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE; 2306fb09a464SKirill A. Shutemov 2307dc617f29SDarrick J. Wong if (vmf->vma->vm_file && 2308dc617f29SDarrick J. Wong IS_SWAPFILE(vmf->vma->vm_file->f_mapping->host)) 2309dc617f29SDarrick J. Wong return VM_FAULT_SIGBUS; 2310dc617f29SDarrick J. Wong 231111bac800SDave Jiang ret = vmf->vma->vm_ops->page_mkwrite(vmf); 231238b8cb7fSJan Kara /* Restore original flags so that caller is not surprised */ 231338b8cb7fSJan Kara vmf->flags = old_flags; 2314fb09a464SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) 2315fb09a464SKirill A. Shutemov return ret; 2316fb09a464SKirill A. Shutemov if (unlikely(!(ret & VM_FAULT_LOCKED))) { 2317fb09a464SKirill A. Shutemov lock_page(page); 2318fb09a464SKirill A. Shutemov if (!page->mapping) { 2319fb09a464SKirill A. Shutemov unlock_page(page); 2320fb09a464SKirill A. Shutemov return 0; /* retry */ 2321fb09a464SKirill A. Shutemov } 2322fb09a464SKirill A. Shutemov ret |= VM_FAULT_LOCKED; 2323fb09a464SKirill A. Shutemov } else 2324fb09a464SKirill A. Shutemov VM_BUG_ON_PAGE(!PageLocked(page), page); 2325fb09a464SKirill A. Shutemov return ret; 2326fb09a464SKirill A. Shutemov } 2327fb09a464SKirill A. Shutemov 2328fb09a464SKirill A. Shutemov /* 232997ba0c2bSJan Kara * Handle dirtying of a page in shared file mapping on a write fault. 23304e047f89SShachar Raindel * 233197ba0c2bSJan Kara * The function expects the page to be locked and unlocks it. 23324e047f89SShachar Raindel */ 233389b15332SJohannes Weiner static vm_fault_t fault_dirty_shared_page(struct vm_fault *vmf) 23344e047f89SShachar Raindel { 233589b15332SJohannes Weiner struct vm_area_struct *vma = vmf->vma; 23364e047f89SShachar Raindel struct address_space *mapping; 233789b15332SJohannes Weiner struct page *page = vmf->page; 233897ba0c2bSJan Kara bool dirtied; 233997ba0c2bSJan Kara bool page_mkwrite = vma->vm_ops && vma->vm_ops->page_mkwrite; 23404e047f89SShachar Raindel 23414e047f89SShachar Raindel dirtied = set_page_dirty(page); 23424e047f89SShachar Raindel VM_BUG_ON_PAGE(PageAnon(page), page); 234397ba0c2bSJan Kara /* 234497ba0c2bSJan Kara * Take a local copy of the address_space - page.mapping may be zeroed 234597ba0c2bSJan Kara * by truncate after unlock_page(). The address_space itself remains 234697ba0c2bSJan Kara * pinned by vma->vm_file's reference. We rely on unlock_page()'s 234797ba0c2bSJan Kara * release semantics to prevent the compiler from undoing this copying. 234897ba0c2bSJan Kara */ 234997ba0c2bSJan Kara mapping = page_rmapping(page); 23504e047f89SShachar Raindel unlock_page(page); 23514e047f89SShachar Raindel 23524e047f89SShachar Raindel if (!page_mkwrite) 23534e047f89SShachar Raindel file_update_time(vma->vm_file); 235489b15332SJohannes Weiner 235589b15332SJohannes Weiner /* 235689b15332SJohannes Weiner * Throttle page dirtying rate down to writeback speed. 235789b15332SJohannes Weiner * 235889b15332SJohannes Weiner * mapping may be NULL here because some device drivers do not 235989b15332SJohannes Weiner * set page.mapping but still dirty their pages 236089b15332SJohannes Weiner * 236189b15332SJohannes Weiner * Drop the mmap_sem before waiting on IO, if we can. The file 236289b15332SJohannes Weiner * is pinning the mapping, as per above. 236389b15332SJohannes Weiner */ 236489b15332SJohannes Weiner if ((dirtied || page_mkwrite) && mapping) { 236589b15332SJohannes Weiner struct file *fpin; 236689b15332SJohannes Weiner 236789b15332SJohannes Weiner fpin = maybe_unlock_mmap_for_io(vmf, NULL); 236889b15332SJohannes Weiner balance_dirty_pages_ratelimited(mapping); 236989b15332SJohannes Weiner if (fpin) { 237089b15332SJohannes Weiner fput(fpin); 237189b15332SJohannes Weiner return VM_FAULT_RETRY; 237289b15332SJohannes Weiner } 237389b15332SJohannes Weiner } 237489b15332SJohannes Weiner 237589b15332SJohannes Weiner return 0; 23764e047f89SShachar Raindel } 23774e047f89SShachar Raindel 237897ba0c2bSJan Kara /* 23794e047f89SShachar Raindel * Handle write page faults for pages that can be reused in the current vma 23804e047f89SShachar Raindel * 23814e047f89SShachar Raindel * This can happen either due to the mapping being with the VM_SHARED flag, 23824e047f89SShachar Raindel * or due to us being the last reference standing to the page. In either 23834e047f89SShachar Raindel * case, all we need to do here is to mark the page as writable and update 23844e047f89SShachar Raindel * any related book-keeping. 23854e047f89SShachar Raindel */ 2386997dd98dSJan Kara static inline void wp_page_reuse(struct vm_fault *vmf) 238782b0f8c3SJan Kara __releases(vmf->ptl) 23884e047f89SShachar Raindel { 238982b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 2390a41b70d6SJan Kara struct page *page = vmf->page; 23914e047f89SShachar Raindel pte_t entry; 23924e047f89SShachar Raindel /* 23934e047f89SShachar Raindel * Clear the pages cpupid information as the existing 23944e047f89SShachar Raindel * information potentially belongs to a now completely 23954e047f89SShachar Raindel * unrelated process. 23964e047f89SShachar Raindel */ 23974e047f89SShachar Raindel if (page) 23984e047f89SShachar Raindel page_cpupid_xchg_last(page, (1 << LAST_CPUPID_SHIFT) - 1); 23994e047f89SShachar Raindel 24002994302bSJan Kara flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); 24012994302bSJan Kara entry = pte_mkyoung(vmf->orig_pte); 24024e047f89SShachar Raindel entry = maybe_mkwrite(pte_mkdirty(entry), vma); 240382b0f8c3SJan Kara if (ptep_set_access_flags(vma, vmf->address, vmf->pte, entry, 1)) 240482b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 240582b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 24064e047f89SShachar Raindel } 24074e047f89SShachar Raindel 24084e047f89SShachar Raindel /* 24092f38ab2cSShachar Raindel * Handle the case of a page which we actually need to copy to a new page. 24102f38ab2cSShachar Raindel * 24112f38ab2cSShachar Raindel * Called with mmap_sem locked and the old page referenced, but 24122f38ab2cSShachar Raindel * without the ptl held. 24132f38ab2cSShachar Raindel * 24142f38ab2cSShachar Raindel * High level logic flow: 24152f38ab2cSShachar Raindel * 24162f38ab2cSShachar Raindel * - Allocate a page, copy the content of the old page to the new one. 24172f38ab2cSShachar Raindel * - Handle book keeping and accounting - cgroups, mmu-notifiers, etc. 24182f38ab2cSShachar Raindel * - Take the PTL. If the pte changed, bail out and release the allocated page 24192f38ab2cSShachar Raindel * - If the pte is still the way we remember it, update the page table and all 24202f38ab2cSShachar Raindel * relevant references. This includes dropping the reference the page-table 24212f38ab2cSShachar Raindel * held to the old page, as well as updating the rmap. 24222f38ab2cSShachar Raindel * - In any case, unlock the PTL and drop the reference we took to the old page. 24232f38ab2cSShachar Raindel */ 24242b740303SSouptick Joarder static vm_fault_t wp_page_copy(struct vm_fault *vmf) 24252f38ab2cSShachar Raindel { 242682b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 2427bae473a4SKirill A. Shutemov struct mm_struct *mm = vma->vm_mm; 2428a41b70d6SJan Kara struct page *old_page = vmf->page; 24292f38ab2cSShachar Raindel struct page *new_page = NULL; 24302f38ab2cSShachar Raindel pte_t entry; 24312f38ab2cSShachar Raindel int page_copied = 0; 24322f38ab2cSShachar Raindel struct mem_cgroup *memcg; 2433ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 24342f38ab2cSShachar Raindel 24352f38ab2cSShachar Raindel if (unlikely(anon_vma_prepare(vma))) 24362f38ab2cSShachar Raindel goto oom; 24372f38ab2cSShachar Raindel 24382994302bSJan Kara if (is_zero_pfn(pte_pfn(vmf->orig_pte))) { 243982b0f8c3SJan Kara new_page = alloc_zeroed_user_highpage_movable(vma, 244082b0f8c3SJan Kara vmf->address); 24412f38ab2cSShachar Raindel if (!new_page) 24422f38ab2cSShachar Raindel goto oom; 24432f38ab2cSShachar Raindel } else { 2444bae473a4SKirill A. Shutemov new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, 244582b0f8c3SJan Kara vmf->address); 24462f38ab2cSShachar Raindel if (!new_page) 24472f38ab2cSShachar Raindel goto oom; 244883d116c5SJia He 244983d116c5SJia He if (!cow_user_page(new_page, old_page, vmf)) { 245083d116c5SJia He /* 245183d116c5SJia He * COW failed, if the fault was solved by other, 245283d116c5SJia He * it's fine. If not, userspace would re-fault on 245383d116c5SJia He * the same address and we will handle the fault 245483d116c5SJia He * from the second attempt. 245583d116c5SJia He */ 245683d116c5SJia He put_page(new_page); 245783d116c5SJia He if (old_page) 245883d116c5SJia He put_page(old_page); 245983d116c5SJia He return 0; 246083d116c5SJia He } 24612f38ab2cSShachar Raindel } 24622f38ab2cSShachar Raindel 24632cf85583STejun Heo if (mem_cgroup_try_charge_delay(new_page, mm, GFP_KERNEL, &memcg, false)) 24642f38ab2cSShachar Raindel goto oom_free_new; 24652f38ab2cSShachar Raindel 2466eb3c24f3SMel Gorman __SetPageUptodate(new_page); 2467eb3c24f3SMel Gorman 24687269f999SJérôme Glisse mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm, 24696f4f13e8SJérôme Glisse vmf->address & PAGE_MASK, 2470ac46d4f3SJérôme Glisse (vmf->address & PAGE_MASK) + PAGE_SIZE); 2471ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 24722f38ab2cSShachar Raindel 24732f38ab2cSShachar Raindel /* 24742f38ab2cSShachar Raindel * Re-check the pte - we dropped the lock 24752f38ab2cSShachar Raindel */ 247682b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(mm, vmf->pmd, vmf->address, &vmf->ptl); 24772994302bSJan Kara if (likely(pte_same(*vmf->pte, vmf->orig_pte))) { 24782f38ab2cSShachar Raindel if (old_page) { 24792f38ab2cSShachar Raindel if (!PageAnon(old_page)) { 2480eca56ff9SJerome Marchand dec_mm_counter_fast(mm, 2481eca56ff9SJerome Marchand mm_counter_file(old_page)); 24822f38ab2cSShachar Raindel inc_mm_counter_fast(mm, MM_ANONPAGES); 24832f38ab2cSShachar Raindel } 24842f38ab2cSShachar Raindel } else { 24852f38ab2cSShachar Raindel inc_mm_counter_fast(mm, MM_ANONPAGES); 24862f38ab2cSShachar Raindel } 24872994302bSJan Kara flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); 24882f38ab2cSShachar Raindel entry = mk_pte(new_page, vma->vm_page_prot); 24892f38ab2cSShachar Raindel entry = maybe_mkwrite(pte_mkdirty(entry), vma); 24902f38ab2cSShachar Raindel /* 24912f38ab2cSShachar Raindel * Clear the pte entry and flush it first, before updating the 24922f38ab2cSShachar Raindel * pte with the new entry. This will avoid a race condition 24932f38ab2cSShachar Raindel * seen in the presence of one thread doing SMC and another 24942f38ab2cSShachar Raindel * thread doing COW. 24952f38ab2cSShachar Raindel */ 249682b0f8c3SJan Kara ptep_clear_flush_notify(vma, vmf->address, vmf->pte); 249782b0f8c3SJan Kara page_add_new_anon_rmap(new_page, vma, vmf->address, false); 2498f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(new_page, memcg, false, false); 24992f38ab2cSShachar Raindel lru_cache_add_active_or_unevictable(new_page, vma); 25002f38ab2cSShachar Raindel /* 25012f38ab2cSShachar Raindel * We call the notify macro here because, when using secondary 25022f38ab2cSShachar Raindel * mmu page tables (such as kvm shadow page tables), we want the 25032f38ab2cSShachar Raindel * new page to be mapped directly into the secondary page table. 25042f38ab2cSShachar Raindel */ 250582b0f8c3SJan Kara set_pte_at_notify(mm, vmf->address, vmf->pte, entry); 250682b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 25072f38ab2cSShachar Raindel if (old_page) { 25082f38ab2cSShachar Raindel /* 25092f38ab2cSShachar Raindel * Only after switching the pte to the new page may 25102f38ab2cSShachar Raindel * we remove the mapcount here. Otherwise another 25112f38ab2cSShachar Raindel * process may come and find the rmap count decremented 25122f38ab2cSShachar Raindel * before the pte is switched to the new page, and 25132f38ab2cSShachar Raindel * "reuse" the old page writing into it while our pte 25142f38ab2cSShachar Raindel * here still points into it and can be read by other 25152f38ab2cSShachar Raindel * threads. 25162f38ab2cSShachar Raindel * 25172f38ab2cSShachar Raindel * The critical issue is to order this 25182f38ab2cSShachar Raindel * page_remove_rmap with the ptp_clear_flush above. 25192f38ab2cSShachar Raindel * Those stores are ordered by (if nothing else,) 25202f38ab2cSShachar Raindel * the barrier present in the atomic_add_negative 25212f38ab2cSShachar Raindel * in page_remove_rmap. 25222f38ab2cSShachar Raindel * 25232f38ab2cSShachar Raindel * Then the TLB flush in ptep_clear_flush ensures that 25242f38ab2cSShachar Raindel * no process can access the old page before the 25252f38ab2cSShachar Raindel * decremented mapcount is visible. And the old page 25262f38ab2cSShachar Raindel * cannot be reused until after the decremented 25272f38ab2cSShachar Raindel * mapcount is visible. So transitively, TLBs to 25282f38ab2cSShachar Raindel * old page will be flushed before it can be reused. 25292f38ab2cSShachar Raindel */ 2530d281ee61SKirill A. Shutemov page_remove_rmap(old_page, false); 25312f38ab2cSShachar Raindel } 25322f38ab2cSShachar Raindel 25332f38ab2cSShachar Raindel /* Free the old page.. */ 25342f38ab2cSShachar Raindel new_page = old_page; 25352f38ab2cSShachar Raindel page_copied = 1; 25362f38ab2cSShachar Raindel } else { 2537f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(new_page, memcg, false); 25382f38ab2cSShachar Raindel } 25392f38ab2cSShachar Raindel 25402f38ab2cSShachar Raindel if (new_page) 254109cbfeafSKirill A. Shutemov put_page(new_page); 25422f38ab2cSShachar Raindel 254382b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 25444645b9feSJérôme Glisse /* 25454645b9feSJérôme Glisse * No need to double call mmu_notifier->invalidate_range() callback as 25464645b9feSJérôme Glisse * the above ptep_clear_flush_notify() did already call it. 25474645b9feSJérôme Glisse */ 2548ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_only_end(&range); 25492f38ab2cSShachar Raindel if (old_page) { 25502f38ab2cSShachar Raindel /* 25512f38ab2cSShachar Raindel * Don't let another task, with possibly unlocked vma, 25522f38ab2cSShachar Raindel * keep the mlocked page. 25532f38ab2cSShachar Raindel */ 25542f38ab2cSShachar Raindel if (page_copied && (vma->vm_flags & VM_LOCKED)) { 25552f38ab2cSShachar Raindel lock_page(old_page); /* LRU manipulation */ 2556e90309c9SKirill A. Shutemov if (PageMlocked(old_page)) 25572f38ab2cSShachar Raindel munlock_vma_page(old_page); 25582f38ab2cSShachar Raindel unlock_page(old_page); 25592f38ab2cSShachar Raindel } 256009cbfeafSKirill A. Shutemov put_page(old_page); 25612f38ab2cSShachar Raindel } 25622f38ab2cSShachar Raindel return page_copied ? VM_FAULT_WRITE : 0; 25632f38ab2cSShachar Raindel oom_free_new: 256409cbfeafSKirill A. Shutemov put_page(new_page); 25652f38ab2cSShachar Raindel oom: 25662f38ab2cSShachar Raindel if (old_page) 256709cbfeafSKirill A. Shutemov put_page(old_page); 25682f38ab2cSShachar Raindel return VM_FAULT_OOM; 25692f38ab2cSShachar Raindel } 25702f38ab2cSShachar Raindel 257166a6197cSJan Kara /** 257266a6197cSJan Kara * finish_mkwrite_fault - finish page fault for a shared mapping, making PTE 257366a6197cSJan Kara * writeable once the page is prepared 257466a6197cSJan Kara * 257566a6197cSJan Kara * @vmf: structure describing the fault 257666a6197cSJan Kara * 257766a6197cSJan Kara * This function handles all that is needed to finish a write page fault in a 257866a6197cSJan Kara * shared mapping due to PTE being read-only once the mapped page is prepared. 2579a862f68aSMike Rapoport * It handles locking of PTE and modifying it. 258066a6197cSJan Kara * 258166a6197cSJan Kara * The function expects the page to be locked or other protection against 258266a6197cSJan Kara * concurrent faults / writeback (such as DAX radix tree locks). 2583a862f68aSMike Rapoport * 2584a862f68aSMike Rapoport * Return: %VM_FAULT_WRITE on success, %0 when PTE got changed before 2585a862f68aSMike Rapoport * we acquired PTE lock. 258666a6197cSJan Kara */ 25872b740303SSouptick Joarder vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf) 258866a6197cSJan Kara { 258966a6197cSJan Kara WARN_ON_ONCE(!(vmf->vma->vm_flags & VM_SHARED)); 259066a6197cSJan Kara vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, vmf->address, 259166a6197cSJan Kara &vmf->ptl); 259266a6197cSJan Kara /* 259366a6197cSJan Kara * We might have raced with another page fault while we released the 259466a6197cSJan Kara * pte_offset_map_lock. 259566a6197cSJan Kara */ 259666a6197cSJan Kara if (!pte_same(*vmf->pte, vmf->orig_pte)) { 259766a6197cSJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2598a19e2553SJan Kara return VM_FAULT_NOPAGE; 259966a6197cSJan Kara } 260066a6197cSJan Kara wp_page_reuse(vmf); 2601a19e2553SJan Kara return 0; 260266a6197cSJan Kara } 260366a6197cSJan Kara 2604dd906184SBoaz Harrosh /* 2605dd906184SBoaz Harrosh * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED 2606dd906184SBoaz Harrosh * mapping 2607dd906184SBoaz Harrosh */ 26082b740303SSouptick Joarder static vm_fault_t wp_pfn_shared(struct vm_fault *vmf) 2609dd906184SBoaz Harrosh { 261082b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 2611bae473a4SKirill A. Shutemov 2612dd906184SBoaz Harrosh if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) { 26132b740303SSouptick Joarder vm_fault_t ret; 2614dd906184SBoaz Harrosh 261582b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2616fe82221fSJan Kara vmf->flags |= FAULT_FLAG_MKWRITE; 261711bac800SDave Jiang ret = vma->vm_ops->pfn_mkwrite(vmf); 26182f89dc12SJan Kara if (ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)) 2619dd906184SBoaz Harrosh return ret; 262066a6197cSJan Kara return finish_mkwrite_fault(vmf); 2621dd906184SBoaz Harrosh } 2622997dd98dSJan Kara wp_page_reuse(vmf); 2623997dd98dSJan Kara return VM_FAULT_WRITE; 2624dd906184SBoaz Harrosh } 2625dd906184SBoaz Harrosh 26262b740303SSouptick Joarder static vm_fault_t wp_page_shared(struct vm_fault *vmf) 262782b0f8c3SJan Kara __releases(vmf->ptl) 262893e478d4SShachar Raindel { 262982b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 263089b15332SJohannes Weiner vm_fault_t ret = VM_FAULT_WRITE; 263193e478d4SShachar Raindel 2632a41b70d6SJan Kara get_page(vmf->page); 263393e478d4SShachar Raindel 263493e478d4SShachar Raindel if (vma->vm_ops && vma->vm_ops->page_mkwrite) { 26352b740303SSouptick Joarder vm_fault_t tmp; 263693e478d4SShachar Raindel 263782b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 263838b8cb7fSJan Kara tmp = do_page_mkwrite(vmf); 263993e478d4SShachar Raindel if (unlikely(!tmp || (tmp & 264093e478d4SShachar Raindel (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { 2641a41b70d6SJan Kara put_page(vmf->page); 264293e478d4SShachar Raindel return tmp; 264393e478d4SShachar Raindel } 264466a6197cSJan Kara tmp = finish_mkwrite_fault(vmf); 2645a19e2553SJan Kara if (unlikely(tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) { 2646a41b70d6SJan Kara unlock_page(vmf->page); 2647a41b70d6SJan Kara put_page(vmf->page); 264866a6197cSJan Kara return tmp; 264993e478d4SShachar Raindel } 265066a6197cSJan Kara } else { 2651997dd98dSJan Kara wp_page_reuse(vmf); 2652997dd98dSJan Kara lock_page(vmf->page); 265393e478d4SShachar Raindel } 265489b15332SJohannes Weiner ret |= fault_dirty_shared_page(vmf); 2655997dd98dSJan Kara put_page(vmf->page); 265693e478d4SShachar Raindel 265789b15332SJohannes Weiner return ret; 265893e478d4SShachar Raindel } 265993e478d4SShachar Raindel 26602f38ab2cSShachar Raindel /* 26611da177e4SLinus Torvalds * This routine handles present pages, when users try to write 26621da177e4SLinus Torvalds * to a shared page. It is done by copying the page to a new address 26631da177e4SLinus Torvalds * and decrementing the shared-page counter for the old page. 26641da177e4SLinus Torvalds * 26651da177e4SLinus Torvalds * Note that this routine assumes that the protection checks have been 26661da177e4SLinus Torvalds * done by the caller (the low-level page fault routine in most cases). 26671da177e4SLinus Torvalds * Thus we can safely just mark it writable once we've done any necessary 26681da177e4SLinus Torvalds * COW. 26691da177e4SLinus Torvalds * 26701da177e4SLinus Torvalds * We also mark the page dirty at this point even though the page will 26711da177e4SLinus Torvalds * change only once the write actually happens. This avoids a few races, 26721da177e4SLinus Torvalds * and potentially makes it more efficient. 26731da177e4SLinus Torvalds * 26748f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 26758f4e2101SHugh Dickins * but allow concurrent faults), with pte both mapped and locked. 26768f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 26771da177e4SLinus Torvalds */ 26782b740303SSouptick Joarder static vm_fault_t do_wp_page(struct vm_fault *vmf) 267982b0f8c3SJan Kara __releases(vmf->ptl) 26801da177e4SLinus Torvalds { 268182b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 26821da177e4SLinus Torvalds 2683a41b70d6SJan Kara vmf->page = vm_normal_page(vma, vmf->address, vmf->orig_pte); 2684a41b70d6SJan Kara if (!vmf->page) { 2685251b97f5SPeter Zijlstra /* 268664e45507SPeter Feiner * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a 268764e45507SPeter Feiner * VM_PFNMAP VMA. 2688251b97f5SPeter Zijlstra * 2689251b97f5SPeter Zijlstra * We should not cow pages in a shared writeable mapping. 2690dd906184SBoaz Harrosh * Just mark the pages writable and/or call ops->pfn_mkwrite. 2691251b97f5SPeter Zijlstra */ 2692251b97f5SPeter Zijlstra if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 2693251b97f5SPeter Zijlstra (VM_WRITE|VM_SHARED)) 26942994302bSJan Kara return wp_pfn_shared(vmf); 26952f38ab2cSShachar Raindel 269682b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2697a41b70d6SJan Kara return wp_page_copy(vmf); 2698251b97f5SPeter Zijlstra } 26991da177e4SLinus Torvalds 2700d08b3851SPeter Zijlstra /* 2701ee6a6457SPeter Zijlstra * Take out anonymous pages first, anonymous shared vmas are 2702ee6a6457SPeter Zijlstra * not dirty accountable. 2703d08b3851SPeter Zijlstra */ 270452d1e606SKirill Tkhai if (PageAnon(vmf->page)) { 2705ba3c4ce6SHuang Ying int total_map_swapcount; 270652d1e606SKirill Tkhai if (PageKsm(vmf->page) && (PageSwapCache(vmf->page) || 270752d1e606SKirill Tkhai page_count(vmf->page) != 1)) 270852d1e606SKirill Tkhai goto copy; 2709a41b70d6SJan Kara if (!trylock_page(vmf->page)) { 2710a41b70d6SJan Kara get_page(vmf->page); 271182b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2712a41b70d6SJan Kara lock_page(vmf->page); 271382b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 271482b0f8c3SJan Kara vmf->address, &vmf->ptl); 27152994302bSJan Kara if (!pte_same(*vmf->pte, vmf->orig_pte)) { 2716a41b70d6SJan Kara unlock_page(vmf->page); 271782b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2718a41b70d6SJan Kara put_page(vmf->page); 271928766805SShachar Raindel return 0; 2720ab967d86SHugh Dickins } 2721a41b70d6SJan Kara put_page(vmf->page); 2722ab967d86SHugh Dickins } 272352d1e606SKirill Tkhai if (PageKsm(vmf->page)) { 272452d1e606SKirill Tkhai bool reused = reuse_ksm_page(vmf->page, vmf->vma, 272552d1e606SKirill Tkhai vmf->address); 272652d1e606SKirill Tkhai unlock_page(vmf->page); 272752d1e606SKirill Tkhai if (!reused) 272852d1e606SKirill Tkhai goto copy; 272952d1e606SKirill Tkhai wp_page_reuse(vmf); 273052d1e606SKirill Tkhai return VM_FAULT_WRITE; 273152d1e606SKirill Tkhai } 2732ba3c4ce6SHuang Ying if (reuse_swap_page(vmf->page, &total_map_swapcount)) { 2733ba3c4ce6SHuang Ying if (total_map_swapcount == 1) { 2734c44b6743SRik van Riel /* 27356d0a07edSAndrea Arcangeli * The page is all ours. Move it to 27366d0a07edSAndrea Arcangeli * our anon_vma so the rmap code will 27376d0a07edSAndrea Arcangeli * not search our parent or siblings. 27386d0a07edSAndrea Arcangeli * Protected against the rmap code by 27396d0a07edSAndrea Arcangeli * the page lock. 2740c44b6743SRik van Riel */ 2741a41b70d6SJan Kara page_move_anon_rmap(vmf->page, vma); 27426d0a07edSAndrea Arcangeli } 2743a41b70d6SJan Kara unlock_page(vmf->page); 2744997dd98dSJan Kara wp_page_reuse(vmf); 2745997dd98dSJan Kara return VM_FAULT_WRITE; 2746b009c024SMichel Lespinasse } 2747a41b70d6SJan Kara unlock_page(vmf->page); 2748ee6a6457SPeter Zijlstra } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 2749d08b3851SPeter Zijlstra (VM_WRITE|VM_SHARED))) { 2750a41b70d6SJan Kara return wp_page_shared(vmf); 27511da177e4SLinus Torvalds } 275252d1e606SKirill Tkhai copy: 27531da177e4SLinus Torvalds /* 27541da177e4SLinus Torvalds * Ok, we need to copy. Oh, well.. 27551da177e4SLinus Torvalds */ 2756a41b70d6SJan Kara get_page(vmf->page); 275728766805SShachar Raindel 275882b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2759a41b70d6SJan Kara return wp_page_copy(vmf); 27601da177e4SLinus Torvalds } 27611da177e4SLinus Torvalds 276297a89413SPeter Zijlstra static void unmap_mapping_range_vma(struct vm_area_struct *vma, 27631da177e4SLinus Torvalds unsigned long start_addr, unsigned long end_addr, 27641da177e4SLinus Torvalds struct zap_details *details) 27651da177e4SLinus Torvalds { 2766f5cc4eefSAl Viro zap_page_range_single(vma, start_addr, end_addr - start_addr, details); 27671da177e4SLinus Torvalds } 27681da177e4SLinus Torvalds 2769f808c13fSDavidlohr Bueso static inline void unmap_mapping_range_tree(struct rb_root_cached *root, 27701da177e4SLinus Torvalds struct zap_details *details) 27711da177e4SLinus Torvalds { 27721da177e4SLinus Torvalds struct vm_area_struct *vma; 27731da177e4SLinus Torvalds pgoff_t vba, vea, zba, zea; 27741da177e4SLinus Torvalds 27756b2dbba8SMichel Lespinasse vma_interval_tree_foreach(vma, root, 27761da177e4SLinus Torvalds details->first_index, details->last_index) { 27771da177e4SLinus Torvalds 27781da177e4SLinus Torvalds vba = vma->vm_pgoff; 2779d6e93217SLibin vea = vba + vma_pages(vma) - 1; 27801da177e4SLinus Torvalds zba = details->first_index; 27811da177e4SLinus Torvalds if (zba < vba) 27821da177e4SLinus Torvalds zba = vba; 27831da177e4SLinus Torvalds zea = details->last_index; 27841da177e4SLinus Torvalds if (zea > vea) 27851da177e4SLinus Torvalds zea = vea; 27861da177e4SLinus Torvalds 278797a89413SPeter Zijlstra unmap_mapping_range_vma(vma, 27881da177e4SLinus Torvalds ((zba - vba) << PAGE_SHIFT) + vma->vm_start, 27891da177e4SLinus Torvalds ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start, 279097a89413SPeter Zijlstra details); 27911da177e4SLinus Torvalds } 27921da177e4SLinus Torvalds } 27931da177e4SLinus Torvalds 27941da177e4SLinus Torvalds /** 2795977fbdcdSMatthew Wilcox * unmap_mapping_pages() - Unmap pages from processes. 2796977fbdcdSMatthew Wilcox * @mapping: The address space containing pages to be unmapped. 2797977fbdcdSMatthew Wilcox * @start: Index of first page to be unmapped. 2798977fbdcdSMatthew Wilcox * @nr: Number of pages to be unmapped. 0 to unmap to end of file. 2799977fbdcdSMatthew Wilcox * @even_cows: Whether to unmap even private COWed pages. 2800977fbdcdSMatthew Wilcox * 2801977fbdcdSMatthew Wilcox * Unmap the pages in this address space from any userspace process which 2802977fbdcdSMatthew Wilcox * has them mmaped. Generally, you want to remove COWed pages as well when 2803977fbdcdSMatthew Wilcox * a file is being truncated, but not when invalidating pages from the page 2804977fbdcdSMatthew Wilcox * cache. 2805977fbdcdSMatthew Wilcox */ 2806977fbdcdSMatthew Wilcox void unmap_mapping_pages(struct address_space *mapping, pgoff_t start, 2807977fbdcdSMatthew Wilcox pgoff_t nr, bool even_cows) 2808977fbdcdSMatthew Wilcox { 2809977fbdcdSMatthew Wilcox struct zap_details details = { }; 2810977fbdcdSMatthew Wilcox 2811977fbdcdSMatthew Wilcox details.check_mapping = even_cows ? NULL : mapping; 2812977fbdcdSMatthew Wilcox details.first_index = start; 2813977fbdcdSMatthew Wilcox details.last_index = start + nr - 1; 2814977fbdcdSMatthew Wilcox if (details.last_index < details.first_index) 2815977fbdcdSMatthew Wilcox details.last_index = ULONG_MAX; 2816977fbdcdSMatthew Wilcox 2817977fbdcdSMatthew Wilcox i_mmap_lock_write(mapping); 2818977fbdcdSMatthew Wilcox if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root))) 2819977fbdcdSMatthew Wilcox unmap_mapping_range_tree(&mapping->i_mmap, &details); 2820977fbdcdSMatthew Wilcox i_mmap_unlock_write(mapping); 2821977fbdcdSMatthew Wilcox } 2822977fbdcdSMatthew Wilcox 2823977fbdcdSMatthew Wilcox /** 28248a5f14a2SKirill A. Shutemov * unmap_mapping_range - unmap the portion of all mmaps in the specified 2825977fbdcdSMatthew Wilcox * address_space corresponding to the specified byte range in the underlying 28268a5f14a2SKirill A. Shutemov * file. 28278a5f14a2SKirill A. Shutemov * 28283d41088fSMartin Waitz * @mapping: the address space containing mmaps to be unmapped. 28291da177e4SLinus Torvalds * @holebegin: byte in first page to unmap, relative to the start of 28301da177e4SLinus Torvalds * the underlying file. This will be rounded down to a PAGE_SIZE 283125d9e2d1Snpiggin@suse.de * boundary. Note that this is different from truncate_pagecache(), which 28321da177e4SLinus Torvalds * must keep the partial page. In contrast, we must get rid of 28331da177e4SLinus Torvalds * partial pages. 28341da177e4SLinus Torvalds * @holelen: size of prospective hole in bytes. This will be rounded 28351da177e4SLinus Torvalds * up to a PAGE_SIZE boundary. A holelen of zero truncates to the 28361da177e4SLinus Torvalds * end of the file. 28371da177e4SLinus Torvalds * @even_cows: 1 when truncating a file, unmap even private COWed pages; 28381da177e4SLinus Torvalds * but 0 when invalidating pagecache, don't throw away private data. 28391da177e4SLinus Torvalds */ 28401da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping, 28411da177e4SLinus Torvalds loff_t const holebegin, loff_t const holelen, int even_cows) 28421da177e4SLinus Torvalds { 28431da177e4SLinus Torvalds pgoff_t hba = holebegin >> PAGE_SHIFT; 28441da177e4SLinus Torvalds pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 28451da177e4SLinus Torvalds 28461da177e4SLinus Torvalds /* Check for overflow. */ 28471da177e4SLinus Torvalds if (sizeof(holelen) > sizeof(hlen)) { 28481da177e4SLinus Torvalds long long holeend = 28491da177e4SLinus Torvalds (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 28501da177e4SLinus Torvalds if (holeend & ~(long long)ULONG_MAX) 28511da177e4SLinus Torvalds hlen = ULONG_MAX - hba + 1; 28521da177e4SLinus Torvalds } 28531da177e4SLinus Torvalds 2854977fbdcdSMatthew Wilcox unmap_mapping_pages(mapping, hba, hlen, even_cows); 28551da177e4SLinus Torvalds } 28561da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range); 28571da177e4SLinus Torvalds 28581da177e4SLinus Torvalds /* 28598f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 28608f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 28619a95f3cfSPaul Cassella * We return with pte unmapped and unlocked. 28629a95f3cfSPaul Cassella * 28639a95f3cfSPaul Cassella * We return with the mmap_sem locked or unlocked in the same cases 28649a95f3cfSPaul Cassella * as does filemap_fault(). 28651da177e4SLinus Torvalds */ 28662b740303SSouptick Joarder vm_fault_t do_swap_page(struct vm_fault *vmf) 28671da177e4SLinus Torvalds { 286882b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 2869eaf649ebSMinchan Kim struct page *page = NULL, *swapcache; 287000501b53SJohannes Weiner struct mem_cgroup *memcg; 287165500d23SHugh Dickins swp_entry_t entry; 28721da177e4SLinus Torvalds pte_t pte; 2873d065bd81SMichel Lespinasse int locked; 2874ad8c2ee8SRik van Riel int exclusive = 0; 28752b740303SSouptick Joarder vm_fault_t ret = 0; 28761da177e4SLinus Torvalds 2877eaf649ebSMinchan Kim if (!pte_unmap_same(vma->vm_mm, vmf->pmd, vmf->pte, vmf->orig_pte)) 28788f4e2101SHugh Dickins goto out; 287965500d23SHugh Dickins 28802994302bSJan Kara entry = pte_to_swp_entry(vmf->orig_pte); 2881d1737fdbSAndi Kleen if (unlikely(non_swap_entry(entry))) { 28820697212aSChristoph Lameter if (is_migration_entry(entry)) { 288382b0f8c3SJan Kara migration_entry_wait(vma->vm_mm, vmf->pmd, 288482b0f8c3SJan Kara vmf->address); 28855042db43SJérôme Glisse } else if (is_device_private_entry(entry)) { 2886897e6365SChristoph Hellwig vmf->page = device_private_entry_to_page(entry); 2887897e6365SChristoph Hellwig ret = vmf->page->pgmap->ops->migrate_to_ram(vmf); 2888d1737fdbSAndi Kleen } else if (is_hwpoison_entry(entry)) { 2889d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 2890d1737fdbSAndi Kleen } else { 28912994302bSJan Kara print_bad_pte(vma, vmf->address, vmf->orig_pte, NULL); 2892d99be1a8SHugh Dickins ret = VM_FAULT_SIGBUS; 2893d1737fdbSAndi Kleen } 28940697212aSChristoph Lameter goto out; 28950697212aSChristoph Lameter } 28960bcac06fSMinchan Kim 28970bcac06fSMinchan Kim 28980ff92245SShailabh Nagar delayacct_set_flag(DELAYACCT_PF_SWAPIN); 2899eaf649ebSMinchan Kim page = lookup_swap_cache(entry, vma, vmf->address); 2900f8020772SMinchan Kim swapcache = page; 2901f8020772SMinchan Kim 29021da177e4SLinus Torvalds if (!page) { 29030bcac06fSMinchan Kim struct swap_info_struct *si = swp_swap_info(entry); 29040bcac06fSMinchan Kim 2905aa8d22a1SMinchan Kim if (si->flags & SWP_SYNCHRONOUS_IO && 2906eb085574SHuang Ying __swap_count(entry) == 1) { 29070bcac06fSMinchan Kim /* skip swapcache */ 2908e9e9b7ecSMinchan Kim page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, 2909e9e9b7ecSMinchan Kim vmf->address); 29100bcac06fSMinchan Kim if (page) { 29110bcac06fSMinchan Kim __SetPageLocked(page); 29120bcac06fSMinchan Kim __SetPageSwapBacked(page); 29130bcac06fSMinchan Kim set_page_private(page, entry.val); 29140bcac06fSMinchan Kim lru_cache_add_anon(page); 29150bcac06fSMinchan Kim swap_readpage(page, true); 29160bcac06fSMinchan Kim } 2917aa8d22a1SMinchan Kim } else { 2918e9e9b7ecSMinchan Kim page = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, 2919e9e9b7ecSMinchan Kim vmf); 2920aa8d22a1SMinchan Kim swapcache = page; 29210bcac06fSMinchan Kim } 29220bcac06fSMinchan Kim 2923ec560175SHuang Ying if (!page) { 29241da177e4SLinus Torvalds /* 29258f4e2101SHugh Dickins * Back out if somebody else faulted in this pte 29268f4e2101SHugh Dickins * while we released the pte lock. 29271da177e4SLinus Torvalds */ 292882b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 292982b0f8c3SJan Kara vmf->address, &vmf->ptl); 29302994302bSJan Kara if (likely(pte_same(*vmf->pte, vmf->orig_pte))) 29311da177e4SLinus Torvalds ret = VM_FAULT_OOM; 29320ff92245SShailabh Nagar delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 293365500d23SHugh Dickins goto unlock; 29341da177e4SLinus Torvalds } 29351da177e4SLinus Torvalds 29361da177e4SLinus Torvalds /* Had to read the page from swap area: Major fault */ 29371da177e4SLinus Torvalds ret = VM_FAULT_MAJOR; 2938f8891e5eSChristoph Lameter count_vm_event(PGMAJFAULT); 29392262185cSRoman Gushchin count_memcg_event_mm(vma->vm_mm, PGMAJFAULT); 2940d1737fdbSAndi Kleen } else if (PageHWPoison(page)) { 294171f72525SWu Fengguang /* 294271f72525SWu Fengguang * hwpoisoned dirty swapcache pages are kept for killing 294371f72525SWu Fengguang * owner processes (which may be unknown at hwpoison time) 294471f72525SWu Fengguang */ 2945d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 2946d1737fdbSAndi Kleen delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 29474779cb31SAndi Kleen goto out_release; 29481da177e4SLinus Torvalds } 29491da177e4SLinus Torvalds 295082b0f8c3SJan Kara locked = lock_page_or_retry(page, vma->vm_mm, vmf->flags); 2951e709ffd6SRik van Riel 295220a1022dSBalbir Singh delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 2953d065bd81SMichel Lespinasse if (!locked) { 2954d065bd81SMichel Lespinasse ret |= VM_FAULT_RETRY; 2955d065bd81SMichel Lespinasse goto out_release; 2956d065bd81SMichel Lespinasse } 29571da177e4SLinus Torvalds 29584969c119SAndrea Arcangeli /* 295931c4a3d3SHugh Dickins * Make sure try_to_free_swap or reuse_swap_page or swapoff did not 296031c4a3d3SHugh Dickins * release the swapcache from under us. The page pin, and pte_same 296131c4a3d3SHugh Dickins * test below, are not enough to exclude that. Even if it is still 296231c4a3d3SHugh Dickins * swapcache, we need to check that the page's swap has not changed. 29634969c119SAndrea Arcangeli */ 29640bcac06fSMinchan Kim if (unlikely((!PageSwapCache(page) || 29650bcac06fSMinchan Kim page_private(page) != entry.val)) && swapcache) 29664969c119SAndrea Arcangeli goto out_page; 29674969c119SAndrea Arcangeli 296882b0f8c3SJan Kara page = ksm_might_need_to_copy(page, vma, vmf->address); 29694969c119SAndrea Arcangeli if (unlikely(!page)) { 29705ad64688SHugh Dickins ret = VM_FAULT_OOM; 29714969c119SAndrea Arcangeli page = swapcache; 29724969c119SAndrea Arcangeli goto out_page; 29734969c119SAndrea Arcangeli } 29745ad64688SHugh Dickins 29752cf85583STejun Heo if (mem_cgroup_try_charge_delay(page, vma->vm_mm, GFP_KERNEL, 2976bae473a4SKirill A. Shutemov &memcg, false)) { 2977073e587eSKAMEZAWA Hiroyuki ret = VM_FAULT_OOM; 2978bc43f75cSJohannes Weiner goto out_page; 2979073e587eSKAMEZAWA Hiroyuki } 2980073e587eSKAMEZAWA Hiroyuki 29811da177e4SLinus Torvalds /* 29828f4e2101SHugh Dickins * Back out if somebody else already faulted in this pte. 29831da177e4SLinus Torvalds */ 298482b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 298582b0f8c3SJan Kara &vmf->ptl); 29862994302bSJan Kara if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) 2987b8107480SKirill Korotaev goto out_nomap; 2988b8107480SKirill Korotaev 2989b8107480SKirill Korotaev if (unlikely(!PageUptodate(page))) { 2990b8107480SKirill Korotaev ret = VM_FAULT_SIGBUS; 2991b8107480SKirill Korotaev goto out_nomap; 29921da177e4SLinus Torvalds } 29931da177e4SLinus Torvalds 29948c7c6e34SKAMEZAWA Hiroyuki /* 29958c7c6e34SKAMEZAWA Hiroyuki * The page isn't present yet, go ahead with the fault. 29968c7c6e34SKAMEZAWA Hiroyuki * 29978c7c6e34SKAMEZAWA Hiroyuki * Be careful about the sequence of operations here. 29988c7c6e34SKAMEZAWA Hiroyuki * To get its accounting right, reuse_swap_page() must be called 29998c7c6e34SKAMEZAWA Hiroyuki * while the page is counted on swap but not yet in mapcount i.e. 30008c7c6e34SKAMEZAWA Hiroyuki * before page_add_anon_rmap() and swap_free(); try_to_free_swap() 30018c7c6e34SKAMEZAWA Hiroyuki * must be called after the swap_free(), or it will never succeed. 30028c7c6e34SKAMEZAWA Hiroyuki */ 30031da177e4SLinus Torvalds 3004bae473a4SKirill A. Shutemov inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); 3005bae473a4SKirill A. Shutemov dec_mm_counter_fast(vma->vm_mm, MM_SWAPENTS); 30061da177e4SLinus Torvalds pte = mk_pte(page, vma->vm_page_prot); 300782b0f8c3SJan Kara if ((vmf->flags & FAULT_FLAG_WRITE) && reuse_swap_page(page, NULL)) { 30081da177e4SLinus Torvalds pte = maybe_mkwrite(pte_mkdirty(pte), vma); 300982b0f8c3SJan Kara vmf->flags &= ~FAULT_FLAG_WRITE; 30109a5b489bSAndrea Arcangeli ret |= VM_FAULT_WRITE; 3011d281ee61SKirill A. Shutemov exclusive = RMAP_EXCLUSIVE; 30121da177e4SLinus Torvalds } 30131da177e4SLinus Torvalds flush_icache_page(vma, page); 30142994302bSJan Kara if (pte_swp_soft_dirty(vmf->orig_pte)) 3015179ef71cSCyrill Gorcunov pte = pte_mksoft_dirty(pte); 301682b0f8c3SJan Kara set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte); 3017ca827d55SKhalid Aziz arch_do_swap_page(vma->vm_mm, vma, vmf->address, pte, vmf->orig_pte); 30182994302bSJan Kara vmf->orig_pte = pte; 30190bcac06fSMinchan Kim 30200bcac06fSMinchan Kim /* ksm created a completely new copy */ 30210bcac06fSMinchan Kim if (unlikely(page != swapcache && swapcache)) { 302282b0f8c3SJan Kara page_add_new_anon_rmap(page, vma, vmf->address, false); 3023f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, false); 302400501b53SJohannes Weiner lru_cache_add_active_or_unevictable(page, vma); 30250bcac06fSMinchan Kim } else { 30260bcac06fSMinchan Kim do_page_add_anon_rmap(page, vma, vmf->address, exclusive); 30270bcac06fSMinchan Kim mem_cgroup_commit_charge(page, memcg, true, false); 30280bcac06fSMinchan Kim activate_page(page); 302900501b53SJohannes Weiner } 30301da177e4SLinus Torvalds 3031c475a8abSHugh Dickins swap_free(entry); 30325ccc5abaSVladimir Davydov if (mem_cgroup_swap_full(page) || 30335ccc5abaSVladimir Davydov (vma->vm_flags & VM_LOCKED) || PageMlocked(page)) 3034a2c43eedSHugh Dickins try_to_free_swap(page); 3035c475a8abSHugh Dickins unlock_page(page); 30360bcac06fSMinchan Kim if (page != swapcache && swapcache) { 30374969c119SAndrea Arcangeli /* 30384969c119SAndrea Arcangeli * Hold the lock to avoid the swap entry to be reused 30394969c119SAndrea Arcangeli * until we take the PT lock for the pte_same() check 30404969c119SAndrea Arcangeli * (to avoid false positives from pte_same). For 30414969c119SAndrea Arcangeli * further safety release the lock after the swap_free 30424969c119SAndrea Arcangeli * so that the swap count won't change under a 30434969c119SAndrea Arcangeli * parallel locked swapcache. 30444969c119SAndrea Arcangeli */ 30454969c119SAndrea Arcangeli unlock_page(swapcache); 304609cbfeafSKirill A. Shutemov put_page(swapcache); 30474969c119SAndrea Arcangeli } 3048c475a8abSHugh Dickins 304982b0f8c3SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) { 30502994302bSJan Kara ret |= do_wp_page(vmf); 305161469f1dSHugh Dickins if (ret & VM_FAULT_ERROR) 305261469f1dSHugh Dickins ret &= VM_FAULT_ERROR; 30531da177e4SLinus Torvalds goto out; 30541da177e4SLinus Torvalds } 30551da177e4SLinus Torvalds 30561da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 305782b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 305865500d23SHugh Dickins unlock: 305982b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 30601da177e4SLinus Torvalds out: 30611da177e4SLinus Torvalds return ret; 3062b8107480SKirill Korotaev out_nomap: 3063f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 306482b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3065bc43f75cSJohannes Weiner out_page: 3066b8107480SKirill Korotaev unlock_page(page); 30674779cb31SAndi Kleen out_release: 306809cbfeafSKirill A. Shutemov put_page(page); 30690bcac06fSMinchan Kim if (page != swapcache && swapcache) { 30704969c119SAndrea Arcangeli unlock_page(swapcache); 307109cbfeafSKirill A. Shutemov put_page(swapcache); 30724969c119SAndrea Arcangeli } 307365500d23SHugh Dickins return ret; 30741da177e4SLinus Torvalds } 30751da177e4SLinus Torvalds 30761da177e4SLinus Torvalds /* 30778f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 30788f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 30798f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 30801da177e4SLinus Torvalds */ 30812b740303SSouptick Joarder static vm_fault_t do_anonymous_page(struct vm_fault *vmf) 30821da177e4SLinus Torvalds { 308382b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 308400501b53SJohannes Weiner struct mem_cgroup *memcg; 30858f4e2101SHugh Dickins struct page *page; 30862b740303SSouptick Joarder vm_fault_t ret = 0; 30871da177e4SLinus Torvalds pte_t entry; 30881da177e4SLinus Torvalds 30896b7339f4SKirill A. Shutemov /* File mapping without ->vm_ops ? */ 30906b7339f4SKirill A. Shutemov if (vma->vm_flags & VM_SHARED) 30916b7339f4SKirill A. Shutemov return VM_FAULT_SIGBUS; 30926b7339f4SKirill A. Shutemov 30937267ec00SKirill A. Shutemov /* 30947267ec00SKirill A. Shutemov * Use pte_alloc() instead of pte_alloc_map(). We can't run 30957267ec00SKirill A. Shutemov * pte_offset_map() on pmds where a huge pmd might be created 30967267ec00SKirill A. Shutemov * from a different thread. 30977267ec00SKirill A. Shutemov * 30987267ec00SKirill A. Shutemov * pte_alloc_map() is safe to use under down_write(mmap_sem) or when 30997267ec00SKirill A. Shutemov * parallel threads are excluded by other means. 31007267ec00SKirill A. Shutemov * 31017267ec00SKirill A. Shutemov * Here we only have down_read(mmap_sem). 31027267ec00SKirill A. Shutemov */ 31034cf58924SJoel Fernandes (Google) if (pte_alloc(vma->vm_mm, vmf->pmd)) 31047267ec00SKirill A. Shutemov return VM_FAULT_OOM; 31057267ec00SKirill A. Shutemov 31067267ec00SKirill A. Shutemov /* See the comment in pte_alloc_one_map() */ 310782b0f8c3SJan Kara if (unlikely(pmd_trans_unstable(vmf->pmd))) 31087267ec00SKirill A. Shutemov return 0; 31097267ec00SKirill A. Shutemov 311011ac5524SLinus Torvalds /* Use the zero-page for reads */ 311182b0f8c3SJan Kara if (!(vmf->flags & FAULT_FLAG_WRITE) && 3112bae473a4SKirill A. Shutemov !mm_forbids_zeropage(vma->vm_mm)) { 311382b0f8c3SJan Kara entry = pte_mkspecial(pfn_pte(my_zero_pfn(vmf->address), 311462eede62SHugh Dickins vma->vm_page_prot)); 311582b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 311682b0f8c3SJan Kara vmf->address, &vmf->ptl); 311782b0f8c3SJan Kara if (!pte_none(*vmf->pte)) 3118a13ea5b7SHugh Dickins goto unlock; 31196b31d595SMichal Hocko ret = check_stable_address_space(vma->vm_mm); 31206b31d595SMichal Hocko if (ret) 31216b31d595SMichal Hocko goto unlock; 31226b251fc9SAndrea Arcangeli /* Deliver the page fault to userland, check inside PT lock */ 31236b251fc9SAndrea Arcangeli if (userfaultfd_missing(vma)) { 312482b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 312582b0f8c3SJan Kara return handle_userfault(vmf, VM_UFFD_MISSING); 31266b251fc9SAndrea Arcangeli } 3127a13ea5b7SHugh Dickins goto setpte; 3128a13ea5b7SHugh Dickins } 3129a13ea5b7SHugh Dickins 31301da177e4SLinus Torvalds /* Allocate our own private page. */ 31311da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 313265500d23SHugh Dickins goto oom; 313382b0f8c3SJan Kara page = alloc_zeroed_user_highpage_movable(vma, vmf->address); 31341da177e4SLinus Torvalds if (!page) 313565500d23SHugh Dickins goto oom; 3136eb3c24f3SMel Gorman 31372cf85583STejun Heo if (mem_cgroup_try_charge_delay(page, vma->vm_mm, GFP_KERNEL, &memcg, 31382cf85583STejun Heo false)) 3139eb3c24f3SMel Gorman goto oom_free_page; 3140eb3c24f3SMel Gorman 314152f37629SMinchan Kim /* 314252f37629SMinchan Kim * The memory barrier inside __SetPageUptodate makes sure that 3143f4f5329dSWei Yang * preceding stores to the page contents become visible before 314452f37629SMinchan Kim * the set_pte_at() write. 314552f37629SMinchan Kim */ 31460ed361deSNick Piggin __SetPageUptodate(page); 31471da177e4SLinus Torvalds 314865500d23SHugh Dickins entry = mk_pte(page, vma->vm_page_prot); 31491ac0cb5dSHugh Dickins if (vma->vm_flags & VM_WRITE) 31501ac0cb5dSHugh Dickins entry = pte_mkwrite(pte_mkdirty(entry)); 31518f4e2101SHugh Dickins 315282b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 315382b0f8c3SJan Kara &vmf->ptl); 315482b0f8c3SJan Kara if (!pte_none(*vmf->pte)) 31558f4e2101SHugh Dickins goto release; 31569ba69294SHugh Dickins 31576b31d595SMichal Hocko ret = check_stable_address_space(vma->vm_mm); 31586b31d595SMichal Hocko if (ret) 31596b31d595SMichal Hocko goto release; 31606b31d595SMichal Hocko 31616b251fc9SAndrea Arcangeli /* Deliver the page fault to userland, check inside PT lock */ 31626b251fc9SAndrea Arcangeli if (userfaultfd_missing(vma)) { 316382b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3164f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 316509cbfeafSKirill A. Shutemov put_page(page); 316682b0f8c3SJan Kara return handle_userfault(vmf, VM_UFFD_MISSING); 31676b251fc9SAndrea Arcangeli } 31686b251fc9SAndrea Arcangeli 3169bae473a4SKirill A. Shutemov inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); 317082b0f8c3SJan Kara page_add_new_anon_rmap(page, vma, vmf->address, false); 3171f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, false); 317200501b53SJohannes Weiner lru_cache_add_active_or_unevictable(page, vma); 3173a13ea5b7SHugh Dickins setpte: 317482b0f8c3SJan Kara set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry); 31751da177e4SLinus Torvalds 31761da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 317782b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 317865500d23SHugh Dickins unlock: 317982b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 31806b31d595SMichal Hocko return ret; 31818f4e2101SHugh Dickins release: 3182f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 318309cbfeafSKirill A. Shutemov put_page(page); 31848f4e2101SHugh Dickins goto unlock; 31858a9f3ccdSBalbir Singh oom_free_page: 318609cbfeafSKirill A. Shutemov put_page(page); 318765500d23SHugh Dickins oom: 31881da177e4SLinus Torvalds return VM_FAULT_OOM; 31891da177e4SLinus Torvalds } 31901da177e4SLinus Torvalds 31919a95f3cfSPaul Cassella /* 31929a95f3cfSPaul Cassella * The mmap_sem must have been held on entry, and may have been 31939a95f3cfSPaul Cassella * released depending on flags and vma->vm_ops->fault() return value. 31949a95f3cfSPaul Cassella * See filemap_fault() and __lock_page_retry(). 31959a95f3cfSPaul Cassella */ 31962b740303SSouptick Joarder static vm_fault_t __do_fault(struct vm_fault *vmf) 31977eae74afSKirill A. Shutemov { 319882b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 31992b740303SSouptick Joarder vm_fault_t ret; 32007eae74afSKirill A. Shutemov 320163f3655fSMichal Hocko /* 320263f3655fSMichal Hocko * Preallocate pte before we take page_lock because this might lead to 320363f3655fSMichal Hocko * deadlocks for memcg reclaim which waits for pages under writeback: 320463f3655fSMichal Hocko * lock_page(A) 320563f3655fSMichal Hocko * SetPageWriteback(A) 320663f3655fSMichal Hocko * unlock_page(A) 320763f3655fSMichal Hocko * lock_page(B) 320863f3655fSMichal Hocko * lock_page(B) 320963f3655fSMichal Hocko * pte_alloc_pne 321063f3655fSMichal Hocko * shrink_page_list 321163f3655fSMichal Hocko * wait_on_page_writeback(A) 321263f3655fSMichal Hocko * SetPageWriteback(B) 321363f3655fSMichal Hocko * unlock_page(B) 321463f3655fSMichal Hocko * # flush A, B to clear the writeback 321563f3655fSMichal Hocko */ 321663f3655fSMichal Hocko if (pmd_none(*vmf->pmd) && !vmf->prealloc_pte) { 321763f3655fSMichal Hocko vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm); 321863f3655fSMichal Hocko if (!vmf->prealloc_pte) 321963f3655fSMichal Hocko return VM_FAULT_OOM; 322063f3655fSMichal Hocko smp_wmb(); /* See comment in __pte_alloc() */ 322163f3655fSMichal Hocko } 322263f3655fSMichal Hocko 322311bac800SDave Jiang ret = vma->vm_ops->fault(vmf); 32243917048dSJan Kara if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY | 3225b1aa812bSJan Kara VM_FAULT_DONE_COW))) 32267eae74afSKirill A. Shutemov return ret; 32277eae74afSKirill A. Shutemov 3228667240e0SJan Kara if (unlikely(PageHWPoison(vmf->page))) { 32297eae74afSKirill A. Shutemov if (ret & VM_FAULT_LOCKED) 3230667240e0SJan Kara unlock_page(vmf->page); 3231667240e0SJan Kara put_page(vmf->page); 3232936ca80dSJan Kara vmf->page = NULL; 32337eae74afSKirill A. Shutemov return VM_FAULT_HWPOISON; 32347eae74afSKirill A. Shutemov } 32357eae74afSKirill A. Shutemov 32367eae74afSKirill A. Shutemov if (unlikely(!(ret & VM_FAULT_LOCKED))) 3237667240e0SJan Kara lock_page(vmf->page); 32387eae74afSKirill A. Shutemov else 3239667240e0SJan Kara VM_BUG_ON_PAGE(!PageLocked(vmf->page), vmf->page); 32407eae74afSKirill A. Shutemov 32417eae74afSKirill A. Shutemov return ret; 32427eae74afSKirill A. Shutemov } 32437eae74afSKirill A. Shutemov 3244d0f0931dSRoss Zwisler /* 3245d0f0931dSRoss Zwisler * The ordering of these checks is important for pmds with _PAGE_DEVMAP set. 3246d0f0931dSRoss Zwisler * If we check pmd_trans_unstable() first we will trip the bad_pmd() check 3247d0f0931dSRoss Zwisler * inside of pmd_none_or_trans_huge_or_clear_bad(). This will end up correctly 3248d0f0931dSRoss Zwisler * returning 1 but not before it spams dmesg with the pmd_clear_bad() output. 3249d0f0931dSRoss Zwisler */ 3250d0f0931dSRoss Zwisler static int pmd_devmap_trans_unstable(pmd_t *pmd) 3251d0f0931dSRoss Zwisler { 3252d0f0931dSRoss Zwisler return pmd_devmap(*pmd) || pmd_trans_unstable(pmd); 3253d0f0931dSRoss Zwisler } 3254d0f0931dSRoss Zwisler 32552b740303SSouptick Joarder static vm_fault_t pte_alloc_one_map(struct vm_fault *vmf) 32567267ec00SKirill A. Shutemov { 325782b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 32587267ec00SKirill A. Shutemov 325982b0f8c3SJan Kara if (!pmd_none(*vmf->pmd)) 32607267ec00SKirill A. Shutemov goto map_pte; 326182b0f8c3SJan Kara if (vmf->prealloc_pte) { 326282b0f8c3SJan Kara vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); 326382b0f8c3SJan Kara if (unlikely(!pmd_none(*vmf->pmd))) { 326482b0f8c3SJan Kara spin_unlock(vmf->ptl); 32657267ec00SKirill A. Shutemov goto map_pte; 32667267ec00SKirill A. Shutemov } 32677267ec00SKirill A. Shutemov 3268c4812909SKirill A. Shutemov mm_inc_nr_ptes(vma->vm_mm); 326982b0f8c3SJan Kara pmd_populate(vma->vm_mm, vmf->pmd, vmf->prealloc_pte); 327082b0f8c3SJan Kara spin_unlock(vmf->ptl); 32717f2b6ce8STobin C Harding vmf->prealloc_pte = NULL; 32724cf58924SJoel Fernandes (Google) } else if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd))) { 32737267ec00SKirill A. Shutemov return VM_FAULT_OOM; 32747267ec00SKirill A. Shutemov } 32757267ec00SKirill A. Shutemov map_pte: 32767267ec00SKirill A. Shutemov /* 32777267ec00SKirill A. Shutemov * If a huge pmd materialized under us just retry later. Use 3278d0f0931dSRoss Zwisler * pmd_trans_unstable() via pmd_devmap_trans_unstable() instead of 3279d0f0931dSRoss Zwisler * pmd_trans_huge() to ensure the pmd didn't become pmd_trans_huge 3280d0f0931dSRoss Zwisler * under us and then back to pmd_none, as a result of MADV_DONTNEED 3281d0f0931dSRoss Zwisler * running immediately after a huge pmd fault in a different thread of 3282d0f0931dSRoss Zwisler * this mm, in turn leading to a misleading pmd_trans_huge() retval. 3283d0f0931dSRoss Zwisler * All we have to ensure is that it is a regular pmd that we can walk 3284d0f0931dSRoss Zwisler * with pte_offset_map() and we can do that through an atomic read in 3285d0f0931dSRoss Zwisler * C, which is what pmd_trans_unstable() provides. 32867267ec00SKirill A. Shutemov */ 3287d0f0931dSRoss Zwisler if (pmd_devmap_trans_unstable(vmf->pmd)) 32887267ec00SKirill A. Shutemov return VM_FAULT_NOPAGE; 32897267ec00SKirill A. Shutemov 3290d0f0931dSRoss Zwisler /* 3291d0f0931dSRoss Zwisler * At this point we know that our vmf->pmd points to a page of ptes 3292d0f0931dSRoss Zwisler * and it cannot become pmd_none(), pmd_devmap() or pmd_trans_huge() 3293d0f0931dSRoss Zwisler * for the duration of the fault. If a racing MADV_DONTNEED runs and 3294d0f0931dSRoss Zwisler * we zap the ptes pointed to by our vmf->pmd, the vmf->ptl will still 3295d0f0931dSRoss Zwisler * be valid and we will re-check to make sure the vmf->pte isn't 3296d0f0931dSRoss Zwisler * pte_none() under vmf->ptl protection when we return to 3297d0f0931dSRoss Zwisler * alloc_set_pte(). 3298d0f0931dSRoss Zwisler */ 329982b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 330082b0f8c3SJan Kara &vmf->ptl); 33017267ec00SKirill A. Shutemov return 0; 33027267ec00SKirill A. Shutemov } 33037267ec00SKirill A. Shutemov 3304e496cf3dSKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 330582b0f8c3SJan Kara static void deposit_prealloc_pte(struct vm_fault *vmf) 3306953c66c2SAneesh Kumar K.V { 330782b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3308953c66c2SAneesh Kumar K.V 330982b0f8c3SJan Kara pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, vmf->prealloc_pte); 3310953c66c2SAneesh Kumar K.V /* 3311953c66c2SAneesh Kumar K.V * We are going to consume the prealloc table, 3312953c66c2SAneesh Kumar K.V * count that as nr_ptes. 3313953c66c2SAneesh Kumar K.V */ 3314c4812909SKirill A. Shutemov mm_inc_nr_ptes(vma->vm_mm); 33157f2b6ce8STobin C Harding vmf->prealloc_pte = NULL; 3316953c66c2SAneesh Kumar K.V } 3317953c66c2SAneesh Kumar K.V 33182b740303SSouptick Joarder static vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page) 331910102459SKirill A. Shutemov { 332082b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 332182b0f8c3SJan Kara bool write = vmf->flags & FAULT_FLAG_WRITE; 332282b0f8c3SJan Kara unsigned long haddr = vmf->address & HPAGE_PMD_MASK; 332310102459SKirill A. Shutemov pmd_t entry; 33242b740303SSouptick Joarder int i; 33252b740303SSouptick Joarder vm_fault_t ret; 332610102459SKirill A. Shutemov 332710102459SKirill A. Shutemov if (!transhuge_vma_suitable(vma, haddr)) 332810102459SKirill A. Shutemov return VM_FAULT_FALLBACK; 332910102459SKirill A. Shutemov 333010102459SKirill A. Shutemov ret = VM_FAULT_FALLBACK; 333110102459SKirill A. Shutemov page = compound_head(page); 333210102459SKirill A. Shutemov 3333953c66c2SAneesh Kumar K.V /* 3334953c66c2SAneesh Kumar K.V * Archs like ppc64 need additonal space to store information 3335953c66c2SAneesh Kumar K.V * related to pte entry. Use the preallocated table for that. 3336953c66c2SAneesh Kumar K.V */ 333782b0f8c3SJan Kara if (arch_needs_pgtable_deposit() && !vmf->prealloc_pte) { 33384cf58924SJoel Fernandes (Google) vmf->prealloc_pte = pte_alloc_one(vma->vm_mm); 333982b0f8c3SJan Kara if (!vmf->prealloc_pte) 3340953c66c2SAneesh Kumar K.V return VM_FAULT_OOM; 3341953c66c2SAneesh Kumar K.V smp_wmb(); /* See comment in __pte_alloc() */ 3342953c66c2SAneesh Kumar K.V } 3343953c66c2SAneesh Kumar K.V 334482b0f8c3SJan Kara vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); 334582b0f8c3SJan Kara if (unlikely(!pmd_none(*vmf->pmd))) 334610102459SKirill A. Shutemov goto out; 334710102459SKirill A. Shutemov 334810102459SKirill A. Shutemov for (i = 0; i < HPAGE_PMD_NR; i++) 334910102459SKirill A. Shutemov flush_icache_page(vma, page + i); 335010102459SKirill A. Shutemov 335110102459SKirill A. Shutemov entry = mk_huge_pmd(page, vma->vm_page_prot); 335210102459SKirill A. Shutemov if (write) 3353f55e1014SLinus Torvalds entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); 335410102459SKirill A. Shutemov 3355fadae295SYang Shi add_mm_counter(vma->vm_mm, mm_counter_file(page), HPAGE_PMD_NR); 335610102459SKirill A. Shutemov page_add_file_rmap(page, true); 3357953c66c2SAneesh Kumar K.V /* 3358953c66c2SAneesh Kumar K.V * deposit and withdraw with pmd lock held 3359953c66c2SAneesh Kumar K.V */ 3360953c66c2SAneesh Kumar K.V if (arch_needs_pgtable_deposit()) 336182b0f8c3SJan Kara deposit_prealloc_pte(vmf); 336210102459SKirill A. Shutemov 336382b0f8c3SJan Kara set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry); 336410102459SKirill A. Shutemov 336582b0f8c3SJan Kara update_mmu_cache_pmd(vma, haddr, vmf->pmd); 336610102459SKirill A. Shutemov 336710102459SKirill A. Shutemov /* fault is handled */ 336810102459SKirill A. Shutemov ret = 0; 336995ecedcdSKirill A. Shutemov count_vm_event(THP_FILE_MAPPED); 337010102459SKirill A. Shutemov out: 337182b0f8c3SJan Kara spin_unlock(vmf->ptl); 337210102459SKirill A. Shutemov return ret; 337310102459SKirill A. Shutemov } 337410102459SKirill A. Shutemov #else 33752b740303SSouptick Joarder static vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page) 337610102459SKirill A. Shutemov { 337710102459SKirill A. Shutemov BUILD_BUG(); 337810102459SKirill A. Shutemov return 0; 337910102459SKirill A. Shutemov } 338010102459SKirill A. Shutemov #endif 338110102459SKirill A. Shutemov 33828c6e50b0SKirill A. Shutemov /** 33837267ec00SKirill A. Shutemov * alloc_set_pte - setup new PTE entry for given page and add reverse page 33847267ec00SKirill A. Shutemov * mapping. If needed, the fucntion allocates page table or use pre-allocated. 33858c6e50b0SKirill A. Shutemov * 338682b0f8c3SJan Kara * @vmf: fault environment 33877267ec00SKirill A. Shutemov * @memcg: memcg to charge page (only for private mappings) 33888c6e50b0SKirill A. Shutemov * @page: page to map 33898c6e50b0SKirill A. Shutemov * 339082b0f8c3SJan Kara * Caller must take care of unlocking vmf->ptl, if vmf->pte is non-NULL on 339182b0f8c3SJan Kara * return. 33928c6e50b0SKirill A. Shutemov * 33938c6e50b0SKirill A. Shutemov * Target users are page handler itself and implementations of 33948c6e50b0SKirill A. Shutemov * vm_ops->map_pages. 3395a862f68aSMike Rapoport * 3396a862f68aSMike Rapoport * Return: %0 on success, %VM_FAULT_ code in case of error. 33978c6e50b0SKirill A. Shutemov */ 33982b740303SSouptick Joarder vm_fault_t alloc_set_pte(struct vm_fault *vmf, struct mem_cgroup *memcg, 33997267ec00SKirill A. Shutemov struct page *page) 34003bb97794SKirill A. Shutemov { 340182b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 340282b0f8c3SJan Kara bool write = vmf->flags & FAULT_FLAG_WRITE; 34033bb97794SKirill A. Shutemov pte_t entry; 34042b740303SSouptick Joarder vm_fault_t ret; 340510102459SKirill A. Shutemov 340682b0f8c3SJan Kara if (pmd_none(*vmf->pmd) && PageTransCompound(page) && 3407e496cf3dSKirill A. Shutemov IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) { 340810102459SKirill A. Shutemov /* THP on COW? */ 340910102459SKirill A. Shutemov VM_BUG_ON_PAGE(memcg, page); 341010102459SKirill A. Shutemov 341182b0f8c3SJan Kara ret = do_set_pmd(vmf, page); 341210102459SKirill A. Shutemov if (ret != VM_FAULT_FALLBACK) 3413b0b9b3dfSHugh Dickins return ret; 341410102459SKirill A. Shutemov } 34153bb97794SKirill A. Shutemov 341682b0f8c3SJan Kara if (!vmf->pte) { 341782b0f8c3SJan Kara ret = pte_alloc_one_map(vmf); 34187267ec00SKirill A. Shutemov if (ret) 3419b0b9b3dfSHugh Dickins return ret; 34207267ec00SKirill A. Shutemov } 34217267ec00SKirill A. Shutemov 34227267ec00SKirill A. Shutemov /* Re-check under ptl */ 3423b0b9b3dfSHugh Dickins if (unlikely(!pte_none(*vmf->pte))) 3424b0b9b3dfSHugh Dickins return VM_FAULT_NOPAGE; 34257267ec00SKirill A. Shutemov 34263bb97794SKirill A. Shutemov flush_icache_page(vma, page); 34273bb97794SKirill A. Shutemov entry = mk_pte(page, vma->vm_page_prot); 34283bb97794SKirill A. Shutemov if (write) 34293bb97794SKirill A. Shutemov entry = maybe_mkwrite(pte_mkdirty(entry), vma); 3430bae473a4SKirill A. Shutemov /* copy-on-write page */ 3431bae473a4SKirill A. Shutemov if (write && !(vma->vm_flags & VM_SHARED)) { 34323bb97794SKirill A. Shutemov inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); 343382b0f8c3SJan Kara page_add_new_anon_rmap(page, vma, vmf->address, false); 34347267ec00SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, false); 34357267ec00SKirill A. Shutemov lru_cache_add_active_or_unevictable(page, vma); 34363bb97794SKirill A. Shutemov } else { 3437eca56ff9SJerome Marchand inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page)); 3438dd78feddSKirill A. Shutemov page_add_file_rmap(page, false); 34393bb97794SKirill A. Shutemov } 344082b0f8c3SJan Kara set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry); 34413bb97794SKirill A. Shutemov 34423bb97794SKirill A. Shutemov /* no need to invalidate: a not-present page won't be cached */ 344382b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 34447267ec00SKirill A. Shutemov 3445b0b9b3dfSHugh Dickins return 0; 34463bb97794SKirill A. Shutemov } 34473bb97794SKirill A. Shutemov 34489118c0cbSJan Kara 34499118c0cbSJan Kara /** 34509118c0cbSJan Kara * finish_fault - finish page fault once we have prepared the page to fault 34519118c0cbSJan Kara * 34529118c0cbSJan Kara * @vmf: structure describing the fault 34539118c0cbSJan Kara * 34549118c0cbSJan Kara * This function handles all that is needed to finish a page fault once the 34559118c0cbSJan Kara * page to fault in is prepared. It handles locking of PTEs, inserts PTE for 34569118c0cbSJan Kara * given page, adds reverse page mapping, handles memcg charges and LRU 3457a862f68aSMike Rapoport * addition. 34589118c0cbSJan Kara * 34599118c0cbSJan Kara * The function expects the page to be locked and on success it consumes a 34609118c0cbSJan Kara * reference of a page being mapped (for the PTE which maps it). 3461a862f68aSMike Rapoport * 3462a862f68aSMike Rapoport * Return: %0 on success, %VM_FAULT_ code in case of error. 34639118c0cbSJan Kara */ 34642b740303SSouptick Joarder vm_fault_t finish_fault(struct vm_fault *vmf) 34659118c0cbSJan Kara { 34669118c0cbSJan Kara struct page *page; 34672b740303SSouptick Joarder vm_fault_t ret = 0; 34689118c0cbSJan Kara 34699118c0cbSJan Kara /* Did we COW the page? */ 34709118c0cbSJan Kara if ((vmf->flags & FAULT_FLAG_WRITE) && 34719118c0cbSJan Kara !(vmf->vma->vm_flags & VM_SHARED)) 34729118c0cbSJan Kara page = vmf->cow_page; 34739118c0cbSJan Kara else 34749118c0cbSJan Kara page = vmf->page; 34756b31d595SMichal Hocko 34766b31d595SMichal Hocko /* 34776b31d595SMichal Hocko * check even for read faults because we might have lost our CoWed 34786b31d595SMichal Hocko * page 34796b31d595SMichal Hocko */ 34806b31d595SMichal Hocko if (!(vmf->vma->vm_flags & VM_SHARED)) 34816b31d595SMichal Hocko ret = check_stable_address_space(vmf->vma->vm_mm); 34826b31d595SMichal Hocko if (!ret) 34839118c0cbSJan Kara ret = alloc_set_pte(vmf, vmf->memcg, page); 34849118c0cbSJan Kara if (vmf->pte) 34859118c0cbSJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 34869118c0cbSJan Kara return ret; 34879118c0cbSJan Kara } 34889118c0cbSJan Kara 34893a91053aSKirill A. Shutemov static unsigned long fault_around_bytes __read_mostly = 34903a91053aSKirill A. Shutemov rounddown_pow_of_two(65536); 3491a9b0f861SKirill A. Shutemov 34921592eef0SKirill A. Shutemov #ifdef CONFIG_DEBUG_FS 3493a9b0f861SKirill A. Shutemov static int fault_around_bytes_get(void *data, u64 *val) 34941592eef0SKirill A. Shutemov { 3495a9b0f861SKirill A. Shutemov *val = fault_around_bytes; 34961592eef0SKirill A. Shutemov return 0; 34971592eef0SKirill A. Shutemov } 34981592eef0SKirill A. Shutemov 3499b4903d6eSAndrey Ryabinin /* 3500da391d64SWilliam Kucharski * fault_around_bytes must be rounded down to the nearest page order as it's 3501da391d64SWilliam Kucharski * what do_fault_around() expects to see. 3502b4903d6eSAndrey Ryabinin */ 3503a9b0f861SKirill A. Shutemov static int fault_around_bytes_set(void *data, u64 val) 35041592eef0SKirill A. Shutemov { 3505a9b0f861SKirill A. Shutemov if (val / PAGE_SIZE > PTRS_PER_PTE) 35061592eef0SKirill A. Shutemov return -EINVAL; 3507b4903d6eSAndrey Ryabinin if (val > PAGE_SIZE) 3508b4903d6eSAndrey Ryabinin fault_around_bytes = rounddown_pow_of_two(val); 3509b4903d6eSAndrey Ryabinin else 3510b4903d6eSAndrey Ryabinin fault_around_bytes = PAGE_SIZE; /* rounddown_pow_of_two(0) is undefined */ 35111592eef0SKirill A. Shutemov return 0; 35121592eef0SKirill A. Shutemov } 35130a1345f8SYevgen Pronenko DEFINE_DEBUGFS_ATTRIBUTE(fault_around_bytes_fops, 3514a9b0f861SKirill A. Shutemov fault_around_bytes_get, fault_around_bytes_set, "%llu\n"); 35151592eef0SKirill A. Shutemov 35161592eef0SKirill A. Shutemov static int __init fault_around_debugfs(void) 35171592eef0SKirill A. Shutemov { 3518d9f7979cSGreg Kroah-Hartman debugfs_create_file_unsafe("fault_around_bytes", 0644, NULL, NULL, 3519a9b0f861SKirill A. Shutemov &fault_around_bytes_fops); 35201592eef0SKirill A. Shutemov return 0; 35211592eef0SKirill A. Shutemov } 35221592eef0SKirill A. Shutemov late_initcall(fault_around_debugfs); 35231592eef0SKirill A. Shutemov #endif 35248c6e50b0SKirill A. Shutemov 35251fdb412bSKirill A. Shutemov /* 35261fdb412bSKirill A. Shutemov * do_fault_around() tries to map few pages around the fault address. The hope 35271fdb412bSKirill A. Shutemov * is that the pages will be needed soon and this will lower the number of 35281fdb412bSKirill A. Shutemov * faults to handle. 35291fdb412bSKirill A. Shutemov * 35301fdb412bSKirill A. Shutemov * It uses vm_ops->map_pages() to map the pages, which skips the page if it's 35311fdb412bSKirill A. Shutemov * not ready to be mapped: not up-to-date, locked, etc. 35321fdb412bSKirill A. Shutemov * 35331fdb412bSKirill A. Shutemov * This function is called with the page table lock taken. In the split ptlock 35341fdb412bSKirill A. Shutemov * case the page table lock only protects only those entries which belong to 35351fdb412bSKirill A. Shutemov * the page table corresponding to the fault address. 35361fdb412bSKirill A. Shutemov * 35371fdb412bSKirill A. Shutemov * This function doesn't cross the VMA boundaries, in order to call map_pages() 35381fdb412bSKirill A. Shutemov * only once. 35391fdb412bSKirill A. Shutemov * 3540da391d64SWilliam Kucharski * fault_around_bytes defines how many bytes we'll try to map. 3541da391d64SWilliam Kucharski * do_fault_around() expects it to be set to a power of two less than or equal 3542da391d64SWilliam Kucharski * to PTRS_PER_PTE. 35431fdb412bSKirill A. Shutemov * 3544da391d64SWilliam Kucharski * The virtual address of the area that we map is naturally aligned to 3545da391d64SWilliam Kucharski * fault_around_bytes rounded down to the machine page size 3546da391d64SWilliam Kucharski * (and therefore to page order). This way it's easier to guarantee 3547da391d64SWilliam Kucharski * that we don't cross page table boundaries. 35481fdb412bSKirill A. Shutemov */ 35492b740303SSouptick Joarder static vm_fault_t do_fault_around(struct vm_fault *vmf) 35508c6e50b0SKirill A. Shutemov { 355182b0f8c3SJan Kara unsigned long address = vmf->address, nr_pages, mask; 35520721ec8bSJan Kara pgoff_t start_pgoff = vmf->pgoff; 3553bae473a4SKirill A. Shutemov pgoff_t end_pgoff; 35542b740303SSouptick Joarder int off; 35552b740303SSouptick Joarder vm_fault_t ret = 0; 35568c6e50b0SKirill A. Shutemov 35574db0c3c2SJason Low nr_pages = READ_ONCE(fault_around_bytes) >> PAGE_SHIFT; 3558aecd6f44SKirill A. Shutemov mask = ~(nr_pages * PAGE_SIZE - 1) & PAGE_MASK; 3559aecd6f44SKirill A. Shutemov 356082b0f8c3SJan Kara vmf->address = max(address & mask, vmf->vma->vm_start); 356182b0f8c3SJan Kara off = ((address - vmf->address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1); 3562bae473a4SKirill A. Shutemov start_pgoff -= off; 35638c6e50b0SKirill A. Shutemov 35648c6e50b0SKirill A. Shutemov /* 3565da391d64SWilliam Kucharski * end_pgoff is either the end of the page table, the end of 3566da391d64SWilliam Kucharski * the vma or nr_pages from start_pgoff, depending what is nearest. 35678c6e50b0SKirill A. Shutemov */ 3568bae473a4SKirill A. Shutemov end_pgoff = start_pgoff - 356982b0f8c3SJan Kara ((vmf->address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) + 35708c6e50b0SKirill A. Shutemov PTRS_PER_PTE - 1; 357182b0f8c3SJan Kara end_pgoff = min3(end_pgoff, vma_pages(vmf->vma) + vmf->vma->vm_pgoff - 1, 3572bae473a4SKirill A. Shutemov start_pgoff + nr_pages - 1); 35738c6e50b0SKirill A. Shutemov 357482b0f8c3SJan Kara if (pmd_none(*vmf->pmd)) { 35754cf58924SJoel Fernandes (Google) vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm); 357682b0f8c3SJan Kara if (!vmf->prealloc_pte) 3577c5f88bd2SVegard Nossum goto out; 35787267ec00SKirill A. Shutemov smp_wmb(); /* See comment in __pte_alloc() */ 35798c6e50b0SKirill A. Shutemov } 35808c6e50b0SKirill A. Shutemov 358182b0f8c3SJan Kara vmf->vma->vm_ops->map_pages(vmf, start_pgoff, end_pgoff); 35827267ec00SKirill A. Shutemov 35837267ec00SKirill A. Shutemov /* Huge page is mapped? Page fault is solved */ 358482b0f8c3SJan Kara if (pmd_trans_huge(*vmf->pmd)) { 35857267ec00SKirill A. Shutemov ret = VM_FAULT_NOPAGE; 35867267ec00SKirill A. Shutemov goto out; 35878c6e50b0SKirill A. Shutemov } 35888c6e50b0SKirill A. Shutemov 35897267ec00SKirill A. Shutemov /* ->map_pages() haven't done anything useful. Cold page cache? */ 359082b0f8c3SJan Kara if (!vmf->pte) 35917267ec00SKirill A. Shutemov goto out; 35927267ec00SKirill A. Shutemov 35937267ec00SKirill A. Shutemov /* check if the page fault is solved */ 359482b0f8c3SJan Kara vmf->pte -= (vmf->address >> PAGE_SHIFT) - (address >> PAGE_SHIFT); 359582b0f8c3SJan Kara if (!pte_none(*vmf->pte)) 35967267ec00SKirill A. Shutemov ret = VM_FAULT_NOPAGE; 359782b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 35987267ec00SKirill A. Shutemov out: 359982b0f8c3SJan Kara vmf->address = address; 360082b0f8c3SJan Kara vmf->pte = NULL; 36017267ec00SKirill A. Shutemov return ret; 36027267ec00SKirill A. Shutemov } 36037267ec00SKirill A. Shutemov 36042b740303SSouptick Joarder static vm_fault_t do_read_fault(struct vm_fault *vmf) 3605e655fb29SKirill A. Shutemov { 360682b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 36072b740303SSouptick Joarder vm_fault_t ret = 0; 36088c6e50b0SKirill A. Shutemov 36098c6e50b0SKirill A. Shutemov /* 36108c6e50b0SKirill A. Shutemov * Let's call ->map_pages() first and use ->fault() as fallback 36118c6e50b0SKirill A. Shutemov * if page by the offset is not ready to be mapped (cold cache or 36128c6e50b0SKirill A. Shutemov * something). 36138c6e50b0SKirill A. Shutemov */ 36149b4bdd2fSKirill A. Shutemov if (vma->vm_ops->map_pages && fault_around_bytes >> PAGE_SHIFT > 1) { 36150721ec8bSJan Kara ret = do_fault_around(vmf); 36167267ec00SKirill A. Shutemov if (ret) 36177267ec00SKirill A. Shutemov return ret; 36188c6e50b0SKirill A. Shutemov } 3619e655fb29SKirill A. Shutemov 3620936ca80dSJan Kara ret = __do_fault(vmf); 3621e655fb29SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3622e655fb29SKirill A. Shutemov return ret; 3623e655fb29SKirill A. Shutemov 36249118c0cbSJan Kara ret |= finish_fault(vmf); 3625936ca80dSJan Kara unlock_page(vmf->page); 36267267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3627936ca80dSJan Kara put_page(vmf->page); 3628e655fb29SKirill A. Shutemov return ret; 3629e655fb29SKirill A. Shutemov } 3630e655fb29SKirill A. Shutemov 36312b740303SSouptick Joarder static vm_fault_t do_cow_fault(struct vm_fault *vmf) 3632ec47c3b9SKirill A. Shutemov { 363382b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 36342b740303SSouptick Joarder vm_fault_t ret; 3635ec47c3b9SKirill A. Shutemov 3636ec47c3b9SKirill A. Shutemov if (unlikely(anon_vma_prepare(vma))) 3637ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 3638ec47c3b9SKirill A. Shutemov 3639936ca80dSJan Kara vmf->cow_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vmf->address); 3640936ca80dSJan Kara if (!vmf->cow_page) 3641ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 3642ec47c3b9SKirill A. Shutemov 36432cf85583STejun Heo if (mem_cgroup_try_charge_delay(vmf->cow_page, vma->vm_mm, GFP_KERNEL, 36443917048dSJan Kara &vmf->memcg, false)) { 3645936ca80dSJan Kara put_page(vmf->cow_page); 3646ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 3647ec47c3b9SKirill A. Shutemov } 3648ec47c3b9SKirill A. Shutemov 3649936ca80dSJan Kara ret = __do_fault(vmf); 3650ec47c3b9SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3651ec47c3b9SKirill A. Shutemov goto uncharge_out; 36523917048dSJan Kara if (ret & VM_FAULT_DONE_COW) 36533917048dSJan Kara return ret; 3654ec47c3b9SKirill A. Shutemov 3655936ca80dSJan Kara copy_user_highpage(vmf->cow_page, vmf->page, vmf->address, vma); 3656936ca80dSJan Kara __SetPageUptodate(vmf->cow_page); 3657ec47c3b9SKirill A. Shutemov 36589118c0cbSJan Kara ret |= finish_fault(vmf); 3659936ca80dSJan Kara unlock_page(vmf->page); 3660936ca80dSJan Kara put_page(vmf->page); 36617267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 36627267ec00SKirill A. Shutemov goto uncharge_out; 3663ec47c3b9SKirill A. Shutemov return ret; 3664ec47c3b9SKirill A. Shutemov uncharge_out: 36653917048dSJan Kara mem_cgroup_cancel_charge(vmf->cow_page, vmf->memcg, false); 3666936ca80dSJan Kara put_page(vmf->cow_page); 3667ec47c3b9SKirill A. Shutemov return ret; 3668ec47c3b9SKirill A. Shutemov } 3669ec47c3b9SKirill A. Shutemov 36702b740303SSouptick Joarder static vm_fault_t do_shared_fault(struct vm_fault *vmf) 36711da177e4SLinus Torvalds { 367282b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 36732b740303SSouptick Joarder vm_fault_t ret, tmp; 36741d65f86dSKAMEZAWA Hiroyuki 3675936ca80dSJan Kara ret = __do_fault(vmf); 36767eae74afSKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3677f0c6d4d2SKirill A. Shutemov return ret; 36781da177e4SLinus Torvalds 36791da177e4SLinus Torvalds /* 3680f0c6d4d2SKirill A. Shutemov * Check if the backing address space wants to know that the page is 3681f0c6d4d2SKirill A. Shutemov * about to become writable 36821da177e4SLinus Torvalds */ 3683fb09a464SKirill A. Shutemov if (vma->vm_ops->page_mkwrite) { 3684936ca80dSJan Kara unlock_page(vmf->page); 368538b8cb7fSJan Kara tmp = do_page_mkwrite(vmf); 3686fb09a464SKirill A. Shutemov if (unlikely(!tmp || 3687fb09a464SKirill A. Shutemov (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { 3688936ca80dSJan Kara put_page(vmf->page); 3689f0c6d4d2SKirill A. Shutemov return tmp; 369069676147SMark Fasheh } 3691d0217ac0SNick Piggin } 3692fb09a464SKirill A. Shutemov 36939118c0cbSJan Kara ret |= finish_fault(vmf); 36947267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | 36957267ec00SKirill A. Shutemov VM_FAULT_RETRY))) { 3696936ca80dSJan Kara unlock_page(vmf->page); 3697936ca80dSJan Kara put_page(vmf->page); 3698f0c6d4d2SKirill A. Shutemov return ret; 36999637a5efSDavid Howells } 3700d00806b1SNick Piggin 370189b15332SJohannes Weiner ret |= fault_dirty_shared_page(vmf); 3702b827e496SNick Piggin return ret; 370354cb8821SNick Piggin } 3704d00806b1SNick Piggin 37059a95f3cfSPaul Cassella /* 37069a95f3cfSPaul Cassella * We enter with non-exclusive mmap_sem (to exclude vma changes, 37079a95f3cfSPaul Cassella * but allow concurrent faults). 37089a95f3cfSPaul Cassella * The mmap_sem may have been released depending on flags and our 37099a95f3cfSPaul Cassella * return value. See filemap_fault() and __lock_page_or_retry(). 3710fc8efd2dSJan Stancek * If mmap_sem is released, vma may become invalid (for example 3711fc8efd2dSJan Stancek * by other thread calling munmap()). 37129a95f3cfSPaul Cassella */ 37132b740303SSouptick Joarder static vm_fault_t do_fault(struct vm_fault *vmf) 371454cb8821SNick Piggin { 371582b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3716fc8efd2dSJan Stancek struct mm_struct *vm_mm = vma->vm_mm; 37172b740303SSouptick Joarder vm_fault_t ret; 371854cb8821SNick Piggin 3719ff09d7ecSAneesh Kumar K.V /* 3720ff09d7ecSAneesh Kumar K.V * The VMA was not fully populated on mmap() or missing VM_DONTEXPAND 3721ff09d7ecSAneesh Kumar K.V */ 3722ff09d7ecSAneesh Kumar K.V if (!vma->vm_ops->fault) { 3723ff09d7ecSAneesh Kumar K.V /* 3724ff09d7ecSAneesh Kumar K.V * If we find a migration pmd entry or a none pmd entry, which 3725ff09d7ecSAneesh Kumar K.V * should never happen, return SIGBUS 3726ff09d7ecSAneesh Kumar K.V */ 3727ff09d7ecSAneesh Kumar K.V if (unlikely(!pmd_present(*vmf->pmd))) 3728b0b9b3dfSHugh Dickins ret = VM_FAULT_SIGBUS; 3729ff09d7ecSAneesh Kumar K.V else { 3730ff09d7ecSAneesh Kumar K.V vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, 3731ff09d7ecSAneesh Kumar K.V vmf->pmd, 3732ff09d7ecSAneesh Kumar K.V vmf->address, 3733ff09d7ecSAneesh Kumar K.V &vmf->ptl); 3734ff09d7ecSAneesh Kumar K.V /* 3735ff09d7ecSAneesh Kumar K.V * Make sure this is not a temporary clearing of pte 3736ff09d7ecSAneesh Kumar K.V * by holding ptl and checking again. A R/M/W update 3737ff09d7ecSAneesh Kumar K.V * of pte involves: take ptl, clearing the pte so that 3738ff09d7ecSAneesh Kumar K.V * we don't have concurrent modification by hardware 3739ff09d7ecSAneesh Kumar K.V * followed by an update. 3740ff09d7ecSAneesh Kumar K.V */ 3741ff09d7ecSAneesh Kumar K.V if (unlikely(pte_none(*vmf->pte))) 3742ff09d7ecSAneesh Kumar K.V ret = VM_FAULT_SIGBUS; 3743ff09d7ecSAneesh Kumar K.V else 3744ff09d7ecSAneesh Kumar K.V ret = VM_FAULT_NOPAGE; 3745ff09d7ecSAneesh Kumar K.V 3746ff09d7ecSAneesh Kumar K.V pte_unmap_unlock(vmf->pte, vmf->ptl); 3747ff09d7ecSAneesh Kumar K.V } 3748ff09d7ecSAneesh Kumar K.V } else if (!(vmf->flags & FAULT_FLAG_WRITE)) 3749b0b9b3dfSHugh Dickins ret = do_read_fault(vmf); 3750b0b9b3dfSHugh Dickins else if (!(vma->vm_flags & VM_SHARED)) 3751b0b9b3dfSHugh Dickins ret = do_cow_fault(vmf); 3752b0b9b3dfSHugh Dickins else 3753b0b9b3dfSHugh Dickins ret = do_shared_fault(vmf); 3754b0b9b3dfSHugh Dickins 3755b0b9b3dfSHugh Dickins /* preallocated pagetable is unused: free it */ 3756b0b9b3dfSHugh Dickins if (vmf->prealloc_pte) { 3757fc8efd2dSJan Stancek pte_free(vm_mm, vmf->prealloc_pte); 37587f2b6ce8STobin C Harding vmf->prealloc_pte = NULL; 3759b0b9b3dfSHugh Dickins } 3760b0b9b3dfSHugh Dickins return ret; 376154cb8821SNick Piggin } 376254cb8821SNick Piggin 3763b19a9939SRashika Kheria static int numa_migrate_prep(struct page *page, struct vm_area_struct *vma, 376404bb2f94SRik van Riel unsigned long addr, int page_nid, 376504bb2f94SRik van Riel int *flags) 37669532fec1SMel Gorman { 37679532fec1SMel Gorman get_page(page); 37689532fec1SMel Gorman 37699532fec1SMel Gorman count_vm_numa_event(NUMA_HINT_FAULTS); 377004bb2f94SRik van Riel if (page_nid == numa_node_id()) { 37719532fec1SMel Gorman count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL); 377204bb2f94SRik van Riel *flags |= TNF_FAULT_LOCAL; 377304bb2f94SRik van Riel } 37749532fec1SMel Gorman 37759532fec1SMel Gorman return mpol_misplaced(page, vma, addr); 37769532fec1SMel Gorman } 37779532fec1SMel Gorman 37782b740303SSouptick Joarder static vm_fault_t do_numa_page(struct vm_fault *vmf) 3779d10e63f2SMel Gorman { 378082b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 37814daae3b4SMel Gorman struct page *page = NULL; 378298fa15f3SAnshuman Khandual int page_nid = NUMA_NO_NODE; 378390572890SPeter Zijlstra int last_cpupid; 3784cbee9f88SPeter Zijlstra int target_nid; 3785b8593bfdSMel Gorman bool migrated = false; 378604a86453SAneesh Kumar K.V pte_t pte, old_pte; 3787288bc549SAneesh Kumar K.V bool was_writable = pte_savedwrite(vmf->orig_pte); 37886688cc05SPeter Zijlstra int flags = 0; 3789d10e63f2SMel Gorman 3790d10e63f2SMel Gorman /* 3791d10e63f2SMel Gorman * The "pte" at this point cannot be used safely without 3792d10e63f2SMel Gorman * validation through pte_unmap_same(). It's of NUMA type but 3793d10e63f2SMel Gorman * the pfn may be screwed if the read is non atomic. 3794d10e63f2SMel Gorman */ 379582b0f8c3SJan Kara vmf->ptl = pte_lockptr(vma->vm_mm, vmf->pmd); 379682b0f8c3SJan Kara spin_lock(vmf->ptl); 3797cee216a6SAneesh Kumar K.V if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) { 379882b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 37994daae3b4SMel Gorman goto out; 38004daae3b4SMel Gorman } 38014daae3b4SMel Gorman 3802cee216a6SAneesh Kumar K.V /* 3803cee216a6SAneesh Kumar K.V * Make it present again, Depending on how arch implementes non 3804cee216a6SAneesh Kumar K.V * accessible ptes, some can allow access by kernel mode. 3805cee216a6SAneesh Kumar K.V */ 380604a86453SAneesh Kumar K.V old_pte = ptep_modify_prot_start(vma, vmf->address, vmf->pte); 380704a86453SAneesh Kumar K.V pte = pte_modify(old_pte, vma->vm_page_prot); 38084d942466SMel Gorman pte = pte_mkyoung(pte); 3809b191f9b1SMel Gorman if (was_writable) 3810b191f9b1SMel Gorman pte = pte_mkwrite(pte); 381104a86453SAneesh Kumar K.V ptep_modify_prot_commit(vma, vmf->address, vmf->pte, old_pte, pte); 381282b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 3813d10e63f2SMel Gorman 381482b0f8c3SJan Kara page = vm_normal_page(vma, vmf->address, pte); 3815d10e63f2SMel Gorman if (!page) { 381682b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3817d10e63f2SMel Gorman return 0; 3818d10e63f2SMel Gorman } 3819d10e63f2SMel Gorman 3820e81c4802SKirill A. Shutemov /* TODO: handle PTE-mapped THP */ 3821e81c4802SKirill A. Shutemov if (PageCompound(page)) { 382282b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3823e81c4802SKirill A. Shutemov return 0; 3824e81c4802SKirill A. Shutemov } 3825e81c4802SKirill A. Shutemov 38266688cc05SPeter Zijlstra /* 3827bea66fbdSMel Gorman * Avoid grouping on RO pages in general. RO pages shouldn't hurt as 3828bea66fbdSMel Gorman * much anyway since they can be in shared cache state. This misses 3829bea66fbdSMel Gorman * the case where a mapping is writable but the process never writes 3830bea66fbdSMel Gorman * to it but pte_write gets cleared during protection updates and 3831bea66fbdSMel Gorman * pte_dirty has unpredictable behaviour between PTE scan updates, 3832bea66fbdSMel Gorman * background writeback, dirty balancing and application behaviour. 38336688cc05SPeter Zijlstra */ 3834d59dc7bcSRik van Riel if (!pte_write(pte)) 38356688cc05SPeter Zijlstra flags |= TNF_NO_GROUP; 38366688cc05SPeter Zijlstra 3837dabe1d99SRik van Riel /* 3838dabe1d99SRik van Riel * Flag if the page is shared between multiple address spaces. This 3839dabe1d99SRik van Riel * is later used when determining whether to group tasks together 3840dabe1d99SRik van Riel */ 3841dabe1d99SRik van Riel if (page_mapcount(page) > 1 && (vma->vm_flags & VM_SHARED)) 3842dabe1d99SRik van Riel flags |= TNF_SHARED; 3843dabe1d99SRik van Riel 384490572890SPeter Zijlstra last_cpupid = page_cpupid_last(page); 38458191acbdSMel Gorman page_nid = page_to_nid(page); 384682b0f8c3SJan Kara target_nid = numa_migrate_prep(page, vma, vmf->address, page_nid, 3847bae473a4SKirill A. Shutemov &flags); 384882b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 384998fa15f3SAnshuman Khandual if (target_nid == NUMA_NO_NODE) { 38504daae3b4SMel Gorman put_page(page); 38514daae3b4SMel Gorman goto out; 38524daae3b4SMel Gorman } 38534daae3b4SMel Gorman 38544daae3b4SMel Gorman /* Migrate to the requested node */ 38551bc115d8SMel Gorman migrated = migrate_misplaced_page(page, vma, target_nid); 38566688cc05SPeter Zijlstra if (migrated) { 38578191acbdSMel Gorman page_nid = target_nid; 38586688cc05SPeter Zijlstra flags |= TNF_MIGRATED; 3859074c2381SMel Gorman } else 3860074c2381SMel Gorman flags |= TNF_MIGRATE_FAIL; 38614daae3b4SMel Gorman 38624daae3b4SMel Gorman out: 386398fa15f3SAnshuman Khandual if (page_nid != NUMA_NO_NODE) 38646688cc05SPeter Zijlstra task_numa_fault(last_cpupid, page_nid, 1, flags); 3865d10e63f2SMel Gorman return 0; 3866d10e63f2SMel Gorman } 3867d10e63f2SMel Gorman 38682b740303SSouptick Joarder static inline vm_fault_t create_huge_pmd(struct vm_fault *vmf) 3869b96375f7SMatthew Wilcox { 3870f4200391SDave Jiang if (vma_is_anonymous(vmf->vma)) 387182b0f8c3SJan Kara return do_huge_pmd_anonymous_page(vmf); 3872a2d58167SDave Jiang if (vmf->vma->vm_ops->huge_fault) 3873c791ace1SDave Jiang return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD); 3874b96375f7SMatthew Wilcox return VM_FAULT_FALLBACK; 3875b96375f7SMatthew Wilcox } 3876b96375f7SMatthew Wilcox 3877183f24aaSGeert Uytterhoeven /* `inline' is required to avoid gcc 4.1.2 build error */ 38782b740303SSouptick Joarder static inline vm_fault_t wp_huge_pmd(struct vm_fault *vmf, pmd_t orig_pmd) 3879b96375f7SMatthew Wilcox { 388082b0f8c3SJan Kara if (vma_is_anonymous(vmf->vma)) 388182b0f8c3SJan Kara return do_huge_pmd_wp_page(vmf, orig_pmd); 3882a2d58167SDave Jiang if (vmf->vma->vm_ops->huge_fault) 3883c791ace1SDave Jiang return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD); 3884af9e4d5fSKirill A. Shutemov 3885af9e4d5fSKirill A. Shutemov /* COW handled on pte level: split pmd */ 388682b0f8c3SJan Kara VM_BUG_ON_VMA(vmf->vma->vm_flags & VM_SHARED, vmf->vma); 388782b0f8c3SJan Kara __split_huge_pmd(vmf->vma, vmf->pmd, vmf->address, false, NULL); 3888af9e4d5fSKirill A. Shutemov 3889b96375f7SMatthew Wilcox return VM_FAULT_FALLBACK; 3890b96375f7SMatthew Wilcox } 3891b96375f7SMatthew Wilcox 389238e08854SLorenzo Stoakes static inline bool vma_is_accessible(struct vm_area_struct *vma) 389338e08854SLorenzo Stoakes { 389438e08854SLorenzo Stoakes return vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE); 389538e08854SLorenzo Stoakes } 389638e08854SLorenzo Stoakes 38972b740303SSouptick Joarder static vm_fault_t create_huge_pud(struct vm_fault *vmf) 3898a00cc7d9SMatthew Wilcox { 3899a00cc7d9SMatthew Wilcox #ifdef CONFIG_TRANSPARENT_HUGEPAGE 3900a00cc7d9SMatthew Wilcox /* No support for anonymous transparent PUD pages yet */ 3901a00cc7d9SMatthew Wilcox if (vma_is_anonymous(vmf->vma)) 3902a00cc7d9SMatthew Wilcox return VM_FAULT_FALLBACK; 3903a00cc7d9SMatthew Wilcox if (vmf->vma->vm_ops->huge_fault) 3904c791ace1SDave Jiang return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD); 3905a00cc7d9SMatthew Wilcox #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 3906a00cc7d9SMatthew Wilcox return VM_FAULT_FALLBACK; 3907a00cc7d9SMatthew Wilcox } 3908a00cc7d9SMatthew Wilcox 39092b740303SSouptick Joarder static vm_fault_t wp_huge_pud(struct vm_fault *vmf, pud_t orig_pud) 3910a00cc7d9SMatthew Wilcox { 3911a00cc7d9SMatthew Wilcox #ifdef CONFIG_TRANSPARENT_HUGEPAGE 3912a00cc7d9SMatthew Wilcox /* No support for anonymous transparent PUD pages yet */ 3913a00cc7d9SMatthew Wilcox if (vma_is_anonymous(vmf->vma)) 3914a00cc7d9SMatthew Wilcox return VM_FAULT_FALLBACK; 3915a00cc7d9SMatthew Wilcox if (vmf->vma->vm_ops->huge_fault) 3916c791ace1SDave Jiang return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD); 3917a00cc7d9SMatthew Wilcox #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 3918a00cc7d9SMatthew Wilcox return VM_FAULT_FALLBACK; 3919a00cc7d9SMatthew Wilcox } 3920a00cc7d9SMatthew Wilcox 39211da177e4SLinus Torvalds /* 39221da177e4SLinus Torvalds * These routines also need to handle stuff like marking pages dirty 39231da177e4SLinus Torvalds * and/or accessed for architectures that don't do it in hardware (most 39241da177e4SLinus Torvalds * RISC architectures). The early dirtying is also good on the i386. 39251da177e4SLinus Torvalds * 39261da177e4SLinus Torvalds * There is also a hook called "update_mmu_cache()" that architectures 39271da177e4SLinus Torvalds * with external mmu caches can use to update those (ie the Sparc or 39281da177e4SLinus Torvalds * PowerPC hashed page tables that act as extended TLBs). 39291da177e4SLinus Torvalds * 39307267ec00SKirill A. Shutemov * We enter with non-exclusive mmap_sem (to exclude vma changes, but allow 39317267ec00SKirill A. Shutemov * concurrent faults). 39329a95f3cfSPaul Cassella * 39337267ec00SKirill A. Shutemov * The mmap_sem may have been released depending on flags and our return value. 39347267ec00SKirill A. Shutemov * See filemap_fault() and __lock_page_or_retry(). 39351da177e4SLinus Torvalds */ 39362b740303SSouptick Joarder static vm_fault_t handle_pte_fault(struct vm_fault *vmf) 39371da177e4SLinus Torvalds { 39381da177e4SLinus Torvalds pte_t entry; 39391da177e4SLinus Torvalds 394082b0f8c3SJan Kara if (unlikely(pmd_none(*vmf->pmd))) { 39417267ec00SKirill A. Shutemov /* 39427267ec00SKirill A. Shutemov * Leave __pte_alloc() until later: because vm_ops->fault may 39437267ec00SKirill A. Shutemov * want to allocate huge page, and if we expose page table 39447267ec00SKirill A. Shutemov * for an instant, it will be difficult to retract from 39457267ec00SKirill A. Shutemov * concurrent faults and from rmap lookups. 39467267ec00SKirill A. Shutemov */ 394782b0f8c3SJan Kara vmf->pte = NULL; 39487267ec00SKirill A. Shutemov } else { 39497267ec00SKirill A. Shutemov /* See comment in pte_alloc_one_map() */ 3950d0f0931dSRoss Zwisler if (pmd_devmap_trans_unstable(vmf->pmd)) 39517267ec00SKirill A. Shutemov return 0; 39527267ec00SKirill A. Shutemov /* 39537267ec00SKirill A. Shutemov * A regular pmd is established and it can't morph into a huge 39547267ec00SKirill A. Shutemov * pmd from under us anymore at this point because we hold the 39557267ec00SKirill A. Shutemov * mmap_sem read mode and khugepaged takes it in write mode. 39567267ec00SKirill A. Shutemov * So now it's safe to run pte_offset_map(). 39577267ec00SKirill A. Shutemov */ 395882b0f8c3SJan Kara vmf->pte = pte_offset_map(vmf->pmd, vmf->address); 39592994302bSJan Kara vmf->orig_pte = *vmf->pte; 39607267ec00SKirill A. Shutemov 3961e37c6982SChristian Borntraeger /* 3962e37c6982SChristian Borntraeger * some architectures can have larger ptes than wordsize, 39637267ec00SKirill A. Shutemov * e.g.ppc44x-defconfig has CONFIG_PTE_64BIT=y and 3964b03a0fe0SPaul E. McKenney * CONFIG_32BIT=y, so READ_ONCE cannot guarantee atomic 3965b03a0fe0SPaul E. McKenney * accesses. The code below just needs a consistent view 3966b03a0fe0SPaul E. McKenney * for the ifs and we later double check anyway with the 39677267ec00SKirill A. Shutemov * ptl lock held. So here a barrier will do. 3968e37c6982SChristian Borntraeger */ 3969e37c6982SChristian Borntraeger barrier(); 39702994302bSJan Kara if (pte_none(vmf->orig_pte)) { 397182b0f8c3SJan Kara pte_unmap(vmf->pte); 397282b0f8c3SJan Kara vmf->pte = NULL; 39737267ec00SKirill A. Shutemov } 39747267ec00SKirill A. Shutemov } 39757267ec00SKirill A. Shutemov 397682b0f8c3SJan Kara if (!vmf->pte) { 397782b0f8c3SJan Kara if (vma_is_anonymous(vmf->vma)) 397882b0f8c3SJan Kara return do_anonymous_page(vmf); 3979b5330628SOleg Nesterov else 398082b0f8c3SJan Kara return do_fault(vmf); 398165500d23SHugh Dickins } 39827267ec00SKirill A. Shutemov 39832994302bSJan Kara if (!pte_present(vmf->orig_pte)) 39842994302bSJan Kara return do_swap_page(vmf); 39851da177e4SLinus Torvalds 39862994302bSJan Kara if (pte_protnone(vmf->orig_pte) && vma_is_accessible(vmf->vma)) 39872994302bSJan Kara return do_numa_page(vmf); 3988d10e63f2SMel Gorman 398982b0f8c3SJan Kara vmf->ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd); 399082b0f8c3SJan Kara spin_lock(vmf->ptl); 39912994302bSJan Kara entry = vmf->orig_pte; 399282b0f8c3SJan Kara if (unlikely(!pte_same(*vmf->pte, entry))) 39938f4e2101SHugh Dickins goto unlock; 399482b0f8c3SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) { 3995f6f37321SLinus Torvalds if (!pte_write(entry)) 39962994302bSJan Kara return do_wp_page(vmf); 39971da177e4SLinus Torvalds entry = pte_mkdirty(entry); 39981da177e4SLinus Torvalds } 39991da177e4SLinus Torvalds entry = pte_mkyoung(entry); 400082b0f8c3SJan Kara if (ptep_set_access_flags(vmf->vma, vmf->address, vmf->pte, entry, 400182b0f8c3SJan Kara vmf->flags & FAULT_FLAG_WRITE)) { 400282b0f8c3SJan Kara update_mmu_cache(vmf->vma, vmf->address, vmf->pte); 40031a44e149SAndrea Arcangeli } else { 40041a44e149SAndrea Arcangeli /* 40051a44e149SAndrea Arcangeli * This is needed only for protection faults but the arch code 40061a44e149SAndrea Arcangeli * is not yet telling us if this is a protection fault or not. 40071a44e149SAndrea Arcangeli * This still avoids useless tlb flushes for .text page faults 40081a44e149SAndrea Arcangeli * with threads. 40091a44e149SAndrea Arcangeli */ 401082b0f8c3SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) 401182b0f8c3SJan Kara flush_tlb_fix_spurious_fault(vmf->vma, vmf->address); 40121a44e149SAndrea Arcangeli } 40138f4e2101SHugh Dickins unlock: 401482b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 401583c54070SNick Piggin return 0; 40161da177e4SLinus Torvalds } 40171da177e4SLinus Torvalds 40181da177e4SLinus Torvalds /* 40191da177e4SLinus Torvalds * By the time we get here, we already hold the mm semaphore 40209a95f3cfSPaul Cassella * 40219a95f3cfSPaul Cassella * The mmap_sem may have been released depending on flags and our 40229a95f3cfSPaul Cassella * return value. See filemap_fault() and __lock_page_or_retry(). 40231da177e4SLinus Torvalds */ 40242b740303SSouptick Joarder static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma, 40252b740303SSouptick Joarder unsigned long address, unsigned int flags) 40261da177e4SLinus Torvalds { 402782b0f8c3SJan Kara struct vm_fault vmf = { 4028bae473a4SKirill A. Shutemov .vma = vma, 40291a29d85eSJan Kara .address = address & PAGE_MASK, 4030bae473a4SKirill A. Shutemov .flags = flags, 40310721ec8bSJan Kara .pgoff = linear_page_index(vma, address), 4032667240e0SJan Kara .gfp_mask = __get_fault_gfp_mask(vma), 4033bae473a4SKirill A. Shutemov }; 4034fde26bedSAnshuman Khandual unsigned int dirty = flags & FAULT_FLAG_WRITE; 4035dcddffd4SKirill A. Shutemov struct mm_struct *mm = vma->vm_mm; 40361da177e4SLinus Torvalds pgd_t *pgd; 4037c2febafcSKirill A. Shutemov p4d_t *p4d; 40382b740303SSouptick Joarder vm_fault_t ret; 40391da177e4SLinus Torvalds 40401da177e4SLinus Torvalds pgd = pgd_offset(mm, address); 4041c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, address); 4042c2febafcSKirill A. Shutemov if (!p4d) 4043c2febafcSKirill A. Shutemov return VM_FAULT_OOM; 4044a00cc7d9SMatthew Wilcox 4045c2febafcSKirill A. Shutemov vmf.pud = pud_alloc(mm, p4d, address); 4046a00cc7d9SMatthew Wilcox if (!vmf.pud) 4047c74df32cSHugh Dickins return VM_FAULT_OOM; 4048625110b5SThomas Hellstrom retry_pud: 40497635d9cbSMichal Hocko if (pud_none(*vmf.pud) && __transparent_hugepage_enabled(vma)) { 4050a00cc7d9SMatthew Wilcox ret = create_huge_pud(&vmf); 4051a00cc7d9SMatthew Wilcox if (!(ret & VM_FAULT_FALLBACK)) 4052a00cc7d9SMatthew Wilcox return ret; 4053a00cc7d9SMatthew Wilcox } else { 4054a00cc7d9SMatthew Wilcox pud_t orig_pud = *vmf.pud; 4055a00cc7d9SMatthew Wilcox 4056a00cc7d9SMatthew Wilcox barrier(); 4057a00cc7d9SMatthew Wilcox if (pud_trans_huge(orig_pud) || pud_devmap(orig_pud)) { 4058a00cc7d9SMatthew Wilcox 4059a00cc7d9SMatthew Wilcox /* NUMA case for anonymous PUDs would go here */ 4060a00cc7d9SMatthew Wilcox 4061f6f37321SLinus Torvalds if (dirty && !pud_write(orig_pud)) { 4062a00cc7d9SMatthew Wilcox ret = wp_huge_pud(&vmf, orig_pud); 4063a00cc7d9SMatthew Wilcox if (!(ret & VM_FAULT_FALLBACK)) 4064a00cc7d9SMatthew Wilcox return ret; 4065a00cc7d9SMatthew Wilcox } else { 4066a00cc7d9SMatthew Wilcox huge_pud_set_accessed(&vmf, orig_pud); 4067a00cc7d9SMatthew Wilcox return 0; 4068a00cc7d9SMatthew Wilcox } 4069a00cc7d9SMatthew Wilcox } 4070a00cc7d9SMatthew Wilcox } 4071a00cc7d9SMatthew Wilcox 4072a00cc7d9SMatthew Wilcox vmf.pmd = pmd_alloc(mm, vmf.pud, address); 407382b0f8c3SJan Kara if (!vmf.pmd) 4074c74df32cSHugh Dickins return VM_FAULT_OOM; 4075625110b5SThomas Hellstrom 4076625110b5SThomas Hellstrom /* Huge pud page fault raced with pmd_alloc? */ 4077625110b5SThomas Hellstrom if (pud_trans_unstable(vmf.pud)) 4078625110b5SThomas Hellstrom goto retry_pud; 4079625110b5SThomas Hellstrom 40807635d9cbSMichal Hocko if (pmd_none(*vmf.pmd) && __transparent_hugepage_enabled(vma)) { 4081a2d58167SDave Jiang ret = create_huge_pmd(&vmf); 4082c0292554SKirill A. Shutemov if (!(ret & VM_FAULT_FALLBACK)) 4083c0292554SKirill A. Shutemov return ret; 408471e3aac0SAndrea Arcangeli } else { 408582b0f8c3SJan Kara pmd_t orig_pmd = *vmf.pmd; 40861f1d06c3SDavid Rientjes 408771e3aac0SAndrea Arcangeli barrier(); 408884c3fc4eSZi Yan if (unlikely(is_swap_pmd(orig_pmd))) { 408984c3fc4eSZi Yan VM_BUG_ON(thp_migration_supported() && 409084c3fc4eSZi Yan !is_pmd_migration_entry(orig_pmd)); 409184c3fc4eSZi Yan if (is_pmd_migration_entry(orig_pmd)) 409284c3fc4eSZi Yan pmd_migration_entry_wait(mm, vmf.pmd); 409384c3fc4eSZi Yan return 0; 409484c3fc4eSZi Yan } 40955c7fb56eSDan Williams if (pmd_trans_huge(orig_pmd) || pmd_devmap(orig_pmd)) { 409638e08854SLorenzo Stoakes if (pmd_protnone(orig_pmd) && vma_is_accessible(vma)) 409782b0f8c3SJan Kara return do_huge_pmd_numa_page(&vmf, orig_pmd); 4098d10e63f2SMel Gorman 4099f6f37321SLinus Torvalds if (dirty && !pmd_write(orig_pmd)) { 410082b0f8c3SJan Kara ret = wp_huge_pmd(&vmf, orig_pmd); 41019845cbbdSKirill A. Shutemov if (!(ret & VM_FAULT_FALLBACK)) 41021f1d06c3SDavid Rientjes return ret; 4103a1dd450bSWill Deacon } else { 410482b0f8c3SJan Kara huge_pmd_set_accessed(&vmf, orig_pmd); 410571e3aac0SAndrea Arcangeli return 0; 410671e3aac0SAndrea Arcangeli } 410771e3aac0SAndrea Arcangeli } 41089845cbbdSKirill A. Shutemov } 410971e3aac0SAndrea Arcangeli 411082b0f8c3SJan Kara return handle_pte_fault(&vmf); 41111da177e4SLinus Torvalds } 41121da177e4SLinus Torvalds 41139a95f3cfSPaul Cassella /* 41149a95f3cfSPaul Cassella * By the time we get here, we already hold the mm semaphore 41159a95f3cfSPaul Cassella * 41169a95f3cfSPaul Cassella * The mmap_sem may have been released depending on flags and our 41179a95f3cfSPaul Cassella * return value. See filemap_fault() and __lock_page_or_retry(). 41189a95f3cfSPaul Cassella */ 41192b740303SSouptick Joarder vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address, 4120dcddffd4SKirill A. Shutemov unsigned int flags) 4121519e5247SJohannes Weiner { 41222b740303SSouptick Joarder vm_fault_t ret; 4123519e5247SJohannes Weiner 4124519e5247SJohannes Weiner __set_current_state(TASK_RUNNING); 4125519e5247SJohannes Weiner 4126519e5247SJohannes Weiner count_vm_event(PGFAULT); 41272262185cSRoman Gushchin count_memcg_event_mm(vma->vm_mm, PGFAULT); 4128519e5247SJohannes Weiner 4129519e5247SJohannes Weiner /* do counter updates before entering really critical section. */ 4130519e5247SJohannes Weiner check_sync_rss_stat(current); 4131519e5247SJohannes Weiner 4132de0c799bSLaurent Dufour if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE, 4133de0c799bSLaurent Dufour flags & FAULT_FLAG_INSTRUCTION, 4134de0c799bSLaurent Dufour flags & FAULT_FLAG_REMOTE)) 4135de0c799bSLaurent Dufour return VM_FAULT_SIGSEGV; 4136de0c799bSLaurent Dufour 4137519e5247SJohannes Weiner /* 4138519e5247SJohannes Weiner * Enable the memcg OOM handling for faults triggered in user 4139519e5247SJohannes Weiner * space. Kernel faults are handled more gracefully. 4140519e5247SJohannes Weiner */ 4141519e5247SJohannes Weiner if (flags & FAULT_FLAG_USER) 414229ef680aSMichal Hocko mem_cgroup_enter_user_fault(); 4143519e5247SJohannes Weiner 4144bae473a4SKirill A. Shutemov if (unlikely(is_vm_hugetlb_page(vma))) 4145bae473a4SKirill A. Shutemov ret = hugetlb_fault(vma->vm_mm, vma, address, flags); 4146bae473a4SKirill A. Shutemov else 4147dcddffd4SKirill A. Shutemov ret = __handle_mm_fault(vma, address, flags); 4148519e5247SJohannes Weiner 414949426420SJohannes Weiner if (flags & FAULT_FLAG_USER) { 415029ef680aSMichal Hocko mem_cgroup_exit_user_fault(); 415149426420SJohannes Weiner /* 415249426420SJohannes Weiner * The task may have entered a memcg OOM situation but 415349426420SJohannes Weiner * if the allocation error was handled gracefully (no 415449426420SJohannes Weiner * VM_FAULT_OOM), there is no need to kill anything. 415549426420SJohannes Weiner * Just clean up the OOM state peacefully. 415649426420SJohannes Weiner */ 415749426420SJohannes Weiner if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM)) 415849426420SJohannes Weiner mem_cgroup_oom_synchronize(false); 415949426420SJohannes Weiner } 41603812c8c8SJohannes Weiner 4161519e5247SJohannes Weiner return ret; 4162519e5247SJohannes Weiner } 4163e1d6d01aSJesse Barnes EXPORT_SYMBOL_GPL(handle_mm_fault); 4164519e5247SJohannes Weiner 416590eceff1SKirill A. Shutemov #ifndef __PAGETABLE_P4D_FOLDED 416690eceff1SKirill A. Shutemov /* 416790eceff1SKirill A. Shutemov * Allocate p4d page table. 416890eceff1SKirill A. Shutemov * We've already handled the fast-path in-line. 416990eceff1SKirill A. Shutemov */ 417090eceff1SKirill A. Shutemov int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address) 417190eceff1SKirill A. Shutemov { 417290eceff1SKirill A. Shutemov p4d_t *new = p4d_alloc_one(mm, address); 417390eceff1SKirill A. Shutemov if (!new) 417490eceff1SKirill A. Shutemov return -ENOMEM; 417590eceff1SKirill A. Shutemov 417690eceff1SKirill A. Shutemov smp_wmb(); /* See comment in __pte_alloc */ 417790eceff1SKirill A. Shutemov 417890eceff1SKirill A. Shutemov spin_lock(&mm->page_table_lock); 417990eceff1SKirill A. Shutemov if (pgd_present(*pgd)) /* Another has populated it */ 418090eceff1SKirill A. Shutemov p4d_free(mm, new); 418190eceff1SKirill A. Shutemov else 418290eceff1SKirill A. Shutemov pgd_populate(mm, pgd, new); 418390eceff1SKirill A. Shutemov spin_unlock(&mm->page_table_lock); 418490eceff1SKirill A. Shutemov return 0; 418590eceff1SKirill A. Shutemov } 418690eceff1SKirill A. Shutemov #endif /* __PAGETABLE_P4D_FOLDED */ 418790eceff1SKirill A. Shutemov 41881da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED 41891da177e4SLinus Torvalds /* 41901da177e4SLinus Torvalds * Allocate page upper directory. 4191872fec16SHugh Dickins * We've already handled the fast-path in-line. 41921da177e4SLinus Torvalds */ 4193c2febafcSKirill A. Shutemov int __pud_alloc(struct mm_struct *mm, p4d_t *p4d, unsigned long address) 41941da177e4SLinus Torvalds { 4195c74df32cSHugh Dickins pud_t *new = pud_alloc_one(mm, address); 4196c74df32cSHugh Dickins if (!new) 41971bb3630eSHugh Dickins return -ENOMEM; 41981da177e4SLinus Torvalds 4199362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 4200362a61adSNick Piggin 4201872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 4202c2febafcSKirill A. Shutemov #ifndef __ARCH_HAS_5LEVEL_HACK 4203b4e98d9aSKirill A. Shutemov if (!p4d_present(*p4d)) { 4204b4e98d9aSKirill A. Shutemov mm_inc_nr_puds(mm); 4205c2febafcSKirill A. Shutemov p4d_populate(mm, p4d, new); 4206b4e98d9aSKirill A. Shutemov } else /* Another has populated it */ 4207c2febafcSKirill A. Shutemov pud_free(mm, new); 4208b4e98d9aSKirill A. Shutemov #else 4209b4e98d9aSKirill A. Shutemov if (!pgd_present(*p4d)) { 4210b4e98d9aSKirill A. Shutemov mm_inc_nr_puds(mm); 4211c2febafcSKirill A. Shutemov pgd_populate(mm, p4d, new); 4212b4e98d9aSKirill A. Shutemov } else /* Another has populated it */ 4213b4e98d9aSKirill A. Shutemov pud_free(mm, new); 4214c2febafcSKirill A. Shutemov #endif /* __ARCH_HAS_5LEVEL_HACK */ 4215872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 42161bb3630eSHugh Dickins return 0; 42171da177e4SLinus Torvalds } 42181da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */ 42191da177e4SLinus Torvalds 42201da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED 42211da177e4SLinus Torvalds /* 42221da177e4SLinus Torvalds * Allocate page middle directory. 4223872fec16SHugh Dickins * We've already handled the fast-path in-line. 42241da177e4SLinus Torvalds */ 42251bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) 42261da177e4SLinus Torvalds { 4227a00cc7d9SMatthew Wilcox spinlock_t *ptl; 4228c74df32cSHugh Dickins pmd_t *new = pmd_alloc_one(mm, address); 4229c74df32cSHugh Dickins if (!new) 42301bb3630eSHugh Dickins return -ENOMEM; 42311da177e4SLinus Torvalds 4232362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 4233362a61adSNick Piggin 4234a00cc7d9SMatthew Wilcox ptl = pud_lock(mm, pud); 4235dc6c9a35SKirill A. Shutemov if (!pud_present(*pud)) { 4236dc6c9a35SKirill A. Shutemov mm_inc_nr_pmds(mm); 42371da177e4SLinus Torvalds pud_populate(mm, pud, new); 4238dc6c9a35SKirill A. Shutemov } else /* Another has populated it */ 42395e541973SBenjamin Herrenschmidt pmd_free(mm, new); 4240a00cc7d9SMatthew Wilcox spin_unlock(ptl); 42411bb3630eSHugh Dickins return 0; 42421da177e4SLinus Torvalds } 42431da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */ 42441da177e4SLinus Torvalds 424509796395SRoss Zwisler static int __follow_pte_pmd(struct mm_struct *mm, unsigned long address, 4246ac46d4f3SJérôme Glisse struct mmu_notifier_range *range, 424709796395SRoss Zwisler pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp) 4248f8ad0f49SJohannes Weiner { 4249f8ad0f49SJohannes Weiner pgd_t *pgd; 4250c2febafcSKirill A. Shutemov p4d_t *p4d; 4251f8ad0f49SJohannes Weiner pud_t *pud; 4252f8ad0f49SJohannes Weiner pmd_t *pmd; 4253f8ad0f49SJohannes Weiner pte_t *ptep; 4254f8ad0f49SJohannes Weiner 4255f8ad0f49SJohannes Weiner pgd = pgd_offset(mm, address); 4256f8ad0f49SJohannes Weiner if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) 4257f8ad0f49SJohannes Weiner goto out; 4258f8ad0f49SJohannes Weiner 4259c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, address); 4260c2febafcSKirill A. Shutemov if (p4d_none(*p4d) || unlikely(p4d_bad(*p4d))) 4261c2febafcSKirill A. Shutemov goto out; 4262c2febafcSKirill A. Shutemov 4263c2febafcSKirill A. Shutemov pud = pud_offset(p4d, address); 4264f8ad0f49SJohannes Weiner if (pud_none(*pud) || unlikely(pud_bad(*pud))) 4265f8ad0f49SJohannes Weiner goto out; 4266f8ad0f49SJohannes Weiner 4267f8ad0f49SJohannes Weiner pmd = pmd_offset(pud, address); 4268f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 426909796395SRoss Zwisler 427009796395SRoss Zwisler if (pmd_huge(*pmd)) { 427109796395SRoss Zwisler if (!pmdpp) 4272f8ad0f49SJohannes Weiner goto out; 4273f8ad0f49SJohannes Weiner 4274ac46d4f3SJérôme Glisse if (range) { 42757269f999SJérôme Glisse mmu_notifier_range_init(range, MMU_NOTIFY_CLEAR, 0, 42766f4f13e8SJérôme Glisse NULL, mm, address & PMD_MASK, 4277ac46d4f3SJérôme Glisse (address & PMD_MASK) + PMD_SIZE); 4278ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(range); 4279a4d1a885SJérôme Glisse } 428009796395SRoss Zwisler *ptlp = pmd_lock(mm, pmd); 428109796395SRoss Zwisler if (pmd_huge(*pmd)) { 428209796395SRoss Zwisler *pmdpp = pmd; 428309796395SRoss Zwisler return 0; 428409796395SRoss Zwisler } 428509796395SRoss Zwisler spin_unlock(*ptlp); 4286ac46d4f3SJérôme Glisse if (range) 4287ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(range); 428809796395SRoss Zwisler } 428909796395SRoss Zwisler 429009796395SRoss Zwisler if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd))) 4291f8ad0f49SJohannes Weiner goto out; 4292f8ad0f49SJohannes Weiner 4293ac46d4f3SJérôme Glisse if (range) { 42947269f999SJérôme Glisse mmu_notifier_range_init(range, MMU_NOTIFY_CLEAR, 0, NULL, mm, 42956f4f13e8SJérôme Glisse address & PAGE_MASK, 42961ed7293aSMatthew Wilcox (address & PAGE_MASK) + PAGE_SIZE); 4297ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(range); 4298a4d1a885SJérôme Glisse } 4299f8ad0f49SJohannes Weiner ptep = pte_offset_map_lock(mm, pmd, address, ptlp); 4300f8ad0f49SJohannes Weiner if (!pte_present(*ptep)) 4301f8ad0f49SJohannes Weiner goto unlock; 4302f8ad0f49SJohannes Weiner *ptepp = ptep; 4303f8ad0f49SJohannes Weiner return 0; 4304f8ad0f49SJohannes Weiner unlock: 4305f8ad0f49SJohannes Weiner pte_unmap_unlock(ptep, *ptlp); 4306ac46d4f3SJérôme Glisse if (range) 4307ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(range); 4308f8ad0f49SJohannes Weiner out: 4309f8ad0f49SJohannes Weiner return -EINVAL; 4310f8ad0f49SJohannes Weiner } 4311f8ad0f49SJohannes Weiner 4312f729c8c9SRoss Zwisler static inline int follow_pte(struct mm_struct *mm, unsigned long address, 4313f729c8c9SRoss Zwisler pte_t **ptepp, spinlock_t **ptlp) 43141b36ba81SNamhyung Kim { 43151b36ba81SNamhyung Kim int res; 43161b36ba81SNamhyung Kim 43171b36ba81SNamhyung Kim /* (void) is needed to make gcc happy */ 43181b36ba81SNamhyung Kim (void) __cond_lock(*ptlp, 4319ac46d4f3SJérôme Glisse !(res = __follow_pte_pmd(mm, address, NULL, 4320a4d1a885SJérôme Glisse ptepp, NULL, ptlp))); 43211b36ba81SNamhyung Kim return res; 43221b36ba81SNamhyung Kim } 43231b36ba81SNamhyung Kim 432409796395SRoss Zwisler int follow_pte_pmd(struct mm_struct *mm, unsigned long address, 4325ac46d4f3SJérôme Glisse struct mmu_notifier_range *range, 432609796395SRoss Zwisler pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp) 432709796395SRoss Zwisler { 432809796395SRoss Zwisler int res; 432909796395SRoss Zwisler 433009796395SRoss Zwisler /* (void) is needed to make gcc happy */ 433109796395SRoss Zwisler (void) __cond_lock(*ptlp, 4332ac46d4f3SJérôme Glisse !(res = __follow_pte_pmd(mm, address, range, 4333a4d1a885SJérôme Glisse ptepp, pmdpp, ptlp))); 433409796395SRoss Zwisler return res; 433509796395SRoss Zwisler } 433609796395SRoss Zwisler EXPORT_SYMBOL(follow_pte_pmd); 433709796395SRoss Zwisler 43383b6748e2SJohannes Weiner /** 43393b6748e2SJohannes Weiner * follow_pfn - look up PFN at a user virtual address 43403b6748e2SJohannes Weiner * @vma: memory mapping 43413b6748e2SJohannes Weiner * @address: user virtual address 43423b6748e2SJohannes Weiner * @pfn: location to store found PFN 43433b6748e2SJohannes Weiner * 43443b6748e2SJohannes Weiner * Only IO mappings and raw PFN mappings are allowed. 43453b6748e2SJohannes Weiner * 4346a862f68aSMike Rapoport * Return: zero and the pfn at @pfn on success, -ve otherwise. 43473b6748e2SJohannes Weiner */ 43483b6748e2SJohannes Weiner int follow_pfn(struct vm_area_struct *vma, unsigned long address, 43493b6748e2SJohannes Weiner unsigned long *pfn) 43503b6748e2SJohannes Weiner { 43513b6748e2SJohannes Weiner int ret = -EINVAL; 43523b6748e2SJohannes Weiner spinlock_t *ptl; 43533b6748e2SJohannes Weiner pte_t *ptep; 43543b6748e2SJohannes Weiner 43553b6748e2SJohannes Weiner if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 43563b6748e2SJohannes Weiner return ret; 43573b6748e2SJohannes Weiner 43583b6748e2SJohannes Weiner ret = follow_pte(vma->vm_mm, address, &ptep, &ptl); 43593b6748e2SJohannes Weiner if (ret) 43603b6748e2SJohannes Weiner return ret; 43613b6748e2SJohannes Weiner *pfn = pte_pfn(*ptep); 43623b6748e2SJohannes Weiner pte_unmap_unlock(ptep, ptl); 43633b6748e2SJohannes Weiner return 0; 43643b6748e2SJohannes Weiner } 43653b6748e2SJohannes Weiner EXPORT_SYMBOL(follow_pfn); 43663b6748e2SJohannes Weiner 436728b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT 4368d87fe660Svenkatesh.pallipadi@intel.com int follow_phys(struct vm_area_struct *vma, 436928b2ee20SRik van Riel unsigned long address, unsigned int flags, 4370d87fe660Svenkatesh.pallipadi@intel.com unsigned long *prot, resource_size_t *phys) 437128b2ee20SRik van Riel { 437203668a4dSJohannes Weiner int ret = -EINVAL; 437328b2ee20SRik van Riel pte_t *ptep, pte; 437428b2ee20SRik van Riel spinlock_t *ptl; 437528b2ee20SRik van Riel 4376d87fe660Svenkatesh.pallipadi@intel.com if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 4377d87fe660Svenkatesh.pallipadi@intel.com goto out; 437828b2ee20SRik van Riel 437903668a4dSJohannes Weiner if (follow_pte(vma->vm_mm, address, &ptep, &ptl)) 4380d87fe660Svenkatesh.pallipadi@intel.com goto out; 438128b2ee20SRik van Riel pte = *ptep; 438203668a4dSJohannes Weiner 4383f6f37321SLinus Torvalds if ((flags & FOLL_WRITE) && !pte_write(pte)) 438428b2ee20SRik van Riel goto unlock; 438528b2ee20SRik van Riel 438628b2ee20SRik van Riel *prot = pgprot_val(pte_pgprot(pte)); 438703668a4dSJohannes Weiner *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT; 438828b2ee20SRik van Riel 438903668a4dSJohannes Weiner ret = 0; 439028b2ee20SRik van Riel unlock: 439128b2ee20SRik van Riel pte_unmap_unlock(ptep, ptl); 439228b2ee20SRik van Riel out: 4393d87fe660Svenkatesh.pallipadi@intel.com return ret; 439428b2ee20SRik van Riel } 439528b2ee20SRik van Riel 439628b2ee20SRik van Riel int generic_access_phys(struct vm_area_struct *vma, unsigned long addr, 439728b2ee20SRik van Riel void *buf, int len, int write) 439828b2ee20SRik van Riel { 439928b2ee20SRik van Riel resource_size_t phys_addr; 440028b2ee20SRik van Riel unsigned long prot = 0; 44012bc7273bSKOSAKI Motohiro void __iomem *maddr; 440228b2ee20SRik van Riel int offset = addr & (PAGE_SIZE-1); 440328b2ee20SRik van Riel 4404d87fe660Svenkatesh.pallipadi@intel.com if (follow_phys(vma, addr, write, &prot, &phys_addr)) 440528b2ee20SRik van Riel return -EINVAL; 440628b2ee20SRik van Riel 44079cb12d7bSGrazvydas Ignotas maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot); 440824eee1e4Sjie@chenjie6@huwei.com if (!maddr) 440924eee1e4Sjie@chenjie6@huwei.com return -ENOMEM; 441024eee1e4Sjie@chenjie6@huwei.com 441128b2ee20SRik van Riel if (write) 441228b2ee20SRik van Riel memcpy_toio(maddr + offset, buf, len); 441328b2ee20SRik van Riel else 441428b2ee20SRik van Riel memcpy_fromio(buf, maddr + offset, len); 441528b2ee20SRik van Riel iounmap(maddr); 441628b2ee20SRik van Riel 441728b2ee20SRik van Riel return len; 441828b2ee20SRik van Riel } 44195a73633eSUwe Kleine-König EXPORT_SYMBOL_GPL(generic_access_phys); 442028b2ee20SRik van Riel #endif 442128b2ee20SRik van Riel 44220ec76a11SDavid Howells /* 4423206cb636SStephen Wilson * Access another process' address space as given in mm. If non-NULL, use the 4424206cb636SStephen Wilson * given task for page fault accounting. 44250ec76a11SDavid Howells */ 442684d77d3fSEric W. Biederman int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm, 4427442486ecSLorenzo Stoakes unsigned long addr, void *buf, int len, unsigned int gup_flags) 44280ec76a11SDavid Howells { 44290ec76a11SDavid Howells struct vm_area_struct *vma; 44300ec76a11SDavid Howells void *old_buf = buf; 4431442486ecSLorenzo Stoakes int write = gup_flags & FOLL_WRITE; 44320ec76a11SDavid Howells 44331e426fe2SKonstantin Khlebnikov if (down_read_killable(&mm->mmap_sem)) 44341e426fe2SKonstantin Khlebnikov return 0; 44351e426fe2SKonstantin Khlebnikov 4436183ff22bSSimon Arlott /* ignore errors, just check how much was successfully transferred */ 44370ec76a11SDavid Howells while (len) { 44380ec76a11SDavid Howells int bytes, ret, offset; 44390ec76a11SDavid Howells void *maddr; 444028b2ee20SRik van Riel struct page *page = NULL; 44410ec76a11SDavid Howells 44421e987790SDave Hansen ret = get_user_pages_remote(tsk, mm, addr, 1, 44435b56d49fSLorenzo Stoakes gup_flags, &page, &vma, NULL); 444428b2ee20SRik van Riel if (ret <= 0) { 4445dbffcd03SRik van Riel #ifndef CONFIG_HAVE_IOREMAP_PROT 4446dbffcd03SRik van Riel break; 4447dbffcd03SRik van Riel #else 444828b2ee20SRik van Riel /* 444928b2ee20SRik van Riel * Check if this is a VM_IO | VM_PFNMAP VMA, which 445028b2ee20SRik van Riel * we can access using slightly different code. 445128b2ee20SRik van Riel */ 445228b2ee20SRik van Riel vma = find_vma(mm, addr); 4453fe936dfcSMichael Ellerman if (!vma || vma->vm_start > addr) 44540ec76a11SDavid Howells break; 445528b2ee20SRik van Riel if (vma->vm_ops && vma->vm_ops->access) 445628b2ee20SRik van Riel ret = vma->vm_ops->access(vma, addr, buf, 445728b2ee20SRik van Riel len, write); 445828b2ee20SRik van Riel if (ret <= 0) 445928b2ee20SRik van Riel break; 446028b2ee20SRik van Riel bytes = ret; 4461dbffcd03SRik van Riel #endif 446228b2ee20SRik van Riel } else { 44630ec76a11SDavid Howells bytes = len; 44640ec76a11SDavid Howells offset = addr & (PAGE_SIZE-1); 44650ec76a11SDavid Howells if (bytes > PAGE_SIZE-offset) 44660ec76a11SDavid Howells bytes = PAGE_SIZE-offset; 44670ec76a11SDavid Howells 44680ec76a11SDavid Howells maddr = kmap(page); 44690ec76a11SDavid Howells if (write) { 44700ec76a11SDavid Howells copy_to_user_page(vma, page, addr, 44710ec76a11SDavid Howells maddr + offset, buf, bytes); 44720ec76a11SDavid Howells set_page_dirty_lock(page); 44730ec76a11SDavid Howells } else { 44740ec76a11SDavid Howells copy_from_user_page(vma, page, addr, 44750ec76a11SDavid Howells buf, maddr + offset, bytes); 44760ec76a11SDavid Howells } 44770ec76a11SDavid Howells kunmap(page); 447809cbfeafSKirill A. Shutemov put_page(page); 447928b2ee20SRik van Riel } 44800ec76a11SDavid Howells len -= bytes; 44810ec76a11SDavid Howells buf += bytes; 44820ec76a11SDavid Howells addr += bytes; 44830ec76a11SDavid Howells } 44840ec76a11SDavid Howells up_read(&mm->mmap_sem); 44850ec76a11SDavid Howells 44860ec76a11SDavid Howells return buf - old_buf; 44870ec76a11SDavid Howells } 448803252919SAndi Kleen 44895ddd36b9SStephen Wilson /** 4490ae91dbfcSRandy Dunlap * access_remote_vm - access another process' address space 44915ddd36b9SStephen Wilson * @mm: the mm_struct of the target address space 44925ddd36b9SStephen Wilson * @addr: start address to access 44935ddd36b9SStephen Wilson * @buf: source or destination buffer 44945ddd36b9SStephen Wilson * @len: number of bytes to transfer 44956347e8d5SLorenzo Stoakes * @gup_flags: flags modifying lookup behaviour 44965ddd36b9SStephen Wilson * 44975ddd36b9SStephen Wilson * The caller must hold a reference on @mm. 4498a862f68aSMike Rapoport * 4499a862f68aSMike Rapoport * Return: number of bytes copied from source to destination. 45005ddd36b9SStephen Wilson */ 45015ddd36b9SStephen Wilson int access_remote_vm(struct mm_struct *mm, unsigned long addr, 45026347e8d5SLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 45035ddd36b9SStephen Wilson { 45046347e8d5SLorenzo Stoakes return __access_remote_vm(NULL, mm, addr, buf, len, gup_flags); 45055ddd36b9SStephen Wilson } 45065ddd36b9SStephen Wilson 450703252919SAndi Kleen /* 4508206cb636SStephen Wilson * Access another process' address space. 4509206cb636SStephen Wilson * Source/target buffer must be kernel space, 4510206cb636SStephen Wilson * Do not walk the page table directly, use get_user_pages 4511206cb636SStephen Wilson */ 4512206cb636SStephen Wilson int access_process_vm(struct task_struct *tsk, unsigned long addr, 4513f307ab6dSLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 4514206cb636SStephen Wilson { 4515206cb636SStephen Wilson struct mm_struct *mm; 4516206cb636SStephen Wilson int ret; 4517206cb636SStephen Wilson 4518206cb636SStephen Wilson mm = get_task_mm(tsk); 4519206cb636SStephen Wilson if (!mm) 4520206cb636SStephen Wilson return 0; 4521206cb636SStephen Wilson 4522f307ab6dSLorenzo Stoakes ret = __access_remote_vm(tsk, mm, addr, buf, len, gup_flags); 4523442486ecSLorenzo Stoakes 4524206cb636SStephen Wilson mmput(mm); 4525206cb636SStephen Wilson 4526206cb636SStephen Wilson return ret; 4527206cb636SStephen Wilson } 4528fcd35857SCatalin Marinas EXPORT_SYMBOL_GPL(access_process_vm); 4529206cb636SStephen Wilson 453003252919SAndi Kleen /* 453103252919SAndi Kleen * Print the name of a VMA. 453203252919SAndi Kleen */ 453303252919SAndi Kleen void print_vma_addr(char *prefix, unsigned long ip) 453403252919SAndi Kleen { 453503252919SAndi Kleen struct mm_struct *mm = current->mm; 453603252919SAndi Kleen struct vm_area_struct *vma; 453703252919SAndi Kleen 4538e8bff74aSIngo Molnar /* 45390a7f682dSMichal Hocko * we might be running from an atomic context so we cannot sleep 4540e8bff74aSIngo Molnar */ 45410a7f682dSMichal Hocko if (!down_read_trylock(&mm->mmap_sem)) 4542e8bff74aSIngo Molnar return; 4543e8bff74aSIngo Molnar 454403252919SAndi Kleen vma = find_vma(mm, ip); 454503252919SAndi Kleen if (vma && vma->vm_file) { 454603252919SAndi Kleen struct file *f = vma->vm_file; 45470a7f682dSMichal Hocko char *buf = (char *)__get_free_page(GFP_NOWAIT); 454803252919SAndi Kleen if (buf) { 45492fbc57c5SAndy Shevchenko char *p; 455003252919SAndi Kleen 45519bf39ab2SMiklos Szeredi p = file_path(f, buf, PAGE_SIZE); 455203252919SAndi Kleen if (IS_ERR(p)) 455303252919SAndi Kleen p = "?"; 45542fbc57c5SAndy Shevchenko printk("%s%s[%lx+%lx]", prefix, kbasename(p), 455503252919SAndi Kleen vma->vm_start, 455603252919SAndi Kleen vma->vm_end - vma->vm_start); 455703252919SAndi Kleen free_page((unsigned long)buf); 455803252919SAndi Kleen } 455903252919SAndi Kleen } 456051a07e50SJeff Liu up_read(&mm->mmap_sem); 456103252919SAndi Kleen } 45623ee1afa3SNick Piggin 4563662bbcb2SMichael S. Tsirkin #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP) 45649ec23531SDavid Hildenbrand void __might_fault(const char *file, int line) 45653ee1afa3SNick Piggin { 456695156f00SPeter Zijlstra /* 456795156f00SPeter Zijlstra * Some code (nfs/sunrpc) uses socket ops on kernel memory while 456895156f00SPeter Zijlstra * holding the mmap_sem, this is safe because kernel memory doesn't 456995156f00SPeter Zijlstra * get paged out, therefore we'll never actually fault, and the 457095156f00SPeter Zijlstra * below annotations will generate false positives. 457195156f00SPeter Zijlstra */ 4572db68ce10SAl Viro if (uaccess_kernel()) 457395156f00SPeter Zijlstra return; 45749ec23531SDavid Hildenbrand if (pagefault_disabled()) 4575662bbcb2SMichael S. Tsirkin return; 45769ec23531SDavid Hildenbrand __might_sleep(file, line, 0); 45779ec23531SDavid Hildenbrand #if defined(CONFIG_DEBUG_ATOMIC_SLEEP) 4578662bbcb2SMichael S. Tsirkin if (current->mm) 45793ee1afa3SNick Piggin might_lock_read(¤t->mm->mmap_sem); 45809ec23531SDavid Hildenbrand #endif 45813ee1afa3SNick Piggin } 45829ec23531SDavid Hildenbrand EXPORT_SYMBOL(__might_fault); 45833ee1afa3SNick Piggin #endif 458447ad8475SAndrea Arcangeli 458547ad8475SAndrea Arcangeli #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS) 4586c6ddfb6cSHuang Ying /* 4587c6ddfb6cSHuang Ying * Process all subpages of the specified huge page with the specified 4588c6ddfb6cSHuang Ying * operation. The target subpage will be processed last to keep its 4589c6ddfb6cSHuang Ying * cache lines hot. 4590c6ddfb6cSHuang Ying */ 4591c6ddfb6cSHuang Ying static inline void process_huge_page( 4592c6ddfb6cSHuang Ying unsigned long addr_hint, unsigned int pages_per_huge_page, 4593c6ddfb6cSHuang Ying void (*process_subpage)(unsigned long addr, int idx, void *arg), 4594c6ddfb6cSHuang Ying void *arg) 4595c6ddfb6cSHuang Ying { 4596c6ddfb6cSHuang Ying int i, n, base, l; 4597c6ddfb6cSHuang Ying unsigned long addr = addr_hint & 4598c6ddfb6cSHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 4599c6ddfb6cSHuang Ying 4600c6ddfb6cSHuang Ying /* Process target subpage last to keep its cache lines hot */ 4601c6ddfb6cSHuang Ying might_sleep(); 4602c6ddfb6cSHuang Ying n = (addr_hint - addr) / PAGE_SIZE; 4603c6ddfb6cSHuang Ying if (2 * n <= pages_per_huge_page) { 4604c6ddfb6cSHuang Ying /* If target subpage in first half of huge page */ 4605c6ddfb6cSHuang Ying base = 0; 4606c6ddfb6cSHuang Ying l = n; 4607c6ddfb6cSHuang Ying /* Process subpages at the end of huge page */ 4608c6ddfb6cSHuang Ying for (i = pages_per_huge_page - 1; i >= 2 * n; i--) { 4609c6ddfb6cSHuang Ying cond_resched(); 4610c6ddfb6cSHuang Ying process_subpage(addr + i * PAGE_SIZE, i, arg); 4611c6ddfb6cSHuang Ying } 4612c6ddfb6cSHuang Ying } else { 4613c6ddfb6cSHuang Ying /* If target subpage in second half of huge page */ 4614c6ddfb6cSHuang Ying base = pages_per_huge_page - 2 * (pages_per_huge_page - n); 4615c6ddfb6cSHuang Ying l = pages_per_huge_page - n; 4616c6ddfb6cSHuang Ying /* Process subpages at the begin of huge page */ 4617c6ddfb6cSHuang Ying for (i = 0; i < base; i++) { 4618c6ddfb6cSHuang Ying cond_resched(); 4619c6ddfb6cSHuang Ying process_subpage(addr + i * PAGE_SIZE, i, arg); 4620c6ddfb6cSHuang Ying } 4621c6ddfb6cSHuang Ying } 4622c6ddfb6cSHuang Ying /* 4623c6ddfb6cSHuang Ying * Process remaining subpages in left-right-left-right pattern 4624c6ddfb6cSHuang Ying * towards the target subpage 4625c6ddfb6cSHuang Ying */ 4626c6ddfb6cSHuang Ying for (i = 0; i < l; i++) { 4627c6ddfb6cSHuang Ying int left_idx = base + i; 4628c6ddfb6cSHuang Ying int right_idx = base + 2 * l - 1 - i; 4629c6ddfb6cSHuang Ying 4630c6ddfb6cSHuang Ying cond_resched(); 4631c6ddfb6cSHuang Ying process_subpage(addr + left_idx * PAGE_SIZE, left_idx, arg); 4632c6ddfb6cSHuang Ying cond_resched(); 4633c6ddfb6cSHuang Ying process_subpage(addr + right_idx * PAGE_SIZE, right_idx, arg); 4634c6ddfb6cSHuang Ying } 4635c6ddfb6cSHuang Ying } 4636c6ddfb6cSHuang Ying 463747ad8475SAndrea Arcangeli static void clear_gigantic_page(struct page *page, 463847ad8475SAndrea Arcangeli unsigned long addr, 463947ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 464047ad8475SAndrea Arcangeli { 464147ad8475SAndrea Arcangeli int i; 464247ad8475SAndrea Arcangeli struct page *p = page; 464347ad8475SAndrea Arcangeli 464447ad8475SAndrea Arcangeli might_sleep(); 464547ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; 464647ad8475SAndrea Arcangeli i++, p = mem_map_next(p, page, i)) { 464747ad8475SAndrea Arcangeli cond_resched(); 464847ad8475SAndrea Arcangeli clear_user_highpage(p, addr + i * PAGE_SIZE); 464947ad8475SAndrea Arcangeli } 465047ad8475SAndrea Arcangeli } 4651c6ddfb6cSHuang Ying 4652c6ddfb6cSHuang Ying static void clear_subpage(unsigned long addr, int idx, void *arg) 4653c6ddfb6cSHuang Ying { 4654c6ddfb6cSHuang Ying struct page *page = arg; 4655c6ddfb6cSHuang Ying 4656c6ddfb6cSHuang Ying clear_user_highpage(page + idx, addr); 4657c6ddfb6cSHuang Ying } 4658c6ddfb6cSHuang Ying 465947ad8475SAndrea Arcangeli void clear_huge_page(struct page *page, 4660c79b57e4SHuang Ying unsigned long addr_hint, unsigned int pages_per_huge_page) 466147ad8475SAndrea Arcangeli { 4662c79b57e4SHuang Ying unsigned long addr = addr_hint & 4663c79b57e4SHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 466447ad8475SAndrea Arcangeli 466547ad8475SAndrea Arcangeli if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { 466647ad8475SAndrea Arcangeli clear_gigantic_page(page, addr, pages_per_huge_page); 466747ad8475SAndrea Arcangeli return; 466847ad8475SAndrea Arcangeli } 466947ad8475SAndrea Arcangeli 4670c6ddfb6cSHuang Ying process_huge_page(addr_hint, pages_per_huge_page, clear_subpage, page); 467147ad8475SAndrea Arcangeli } 467247ad8475SAndrea Arcangeli 467347ad8475SAndrea Arcangeli static void copy_user_gigantic_page(struct page *dst, struct page *src, 467447ad8475SAndrea Arcangeli unsigned long addr, 467547ad8475SAndrea Arcangeli struct vm_area_struct *vma, 467647ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 467747ad8475SAndrea Arcangeli { 467847ad8475SAndrea Arcangeli int i; 467947ad8475SAndrea Arcangeli struct page *dst_base = dst; 468047ad8475SAndrea Arcangeli struct page *src_base = src; 468147ad8475SAndrea Arcangeli 468247ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; ) { 468347ad8475SAndrea Arcangeli cond_resched(); 468447ad8475SAndrea Arcangeli copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma); 468547ad8475SAndrea Arcangeli 468647ad8475SAndrea Arcangeli i++; 468747ad8475SAndrea Arcangeli dst = mem_map_next(dst, dst_base, i); 468847ad8475SAndrea Arcangeli src = mem_map_next(src, src_base, i); 468947ad8475SAndrea Arcangeli } 469047ad8475SAndrea Arcangeli } 469147ad8475SAndrea Arcangeli 4692c9f4cd71SHuang Ying struct copy_subpage_arg { 4693c9f4cd71SHuang Ying struct page *dst; 4694c9f4cd71SHuang Ying struct page *src; 4695c9f4cd71SHuang Ying struct vm_area_struct *vma; 4696c9f4cd71SHuang Ying }; 4697c9f4cd71SHuang Ying 4698c9f4cd71SHuang Ying static void copy_subpage(unsigned long addr, int idx, void *arg) 4699c9f4cd71SHuang Ying { 4700c9f4cd71SHuang Ying struct copy_subpage_arg *copy_arg = arg; 4701c9f4cd71SHuang Ying 4702c9f4cd71SHuang Ying copy_user_highpage(copy_arg->dst + idx, copy_arg->src + idx, 4703c9f4cd71SHuang Ying addr, copy_arg->vma); 4704c9f4cd71SHuang Ying } 4705c9f4cd71SHuang Ying 470647ad8475SAndrea Arcangeli void copy_user_huge_page(struct page *dst, struct page *src, 4707c9f4cd71SHuang Ying unsigned long addr_hint, struct vm_area_struct *vma, 470847ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 470947ad8475SAndrea Arcangeli { 4710c9f4cd71SHuang Ying unsigned long addr = addr_hint & 4711c9f4cd71SHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 4712c9f4cd71SHuang Ying struct copy_subpage_arg arg = { 4713c9f4cd71SHuang Ying .dst = dst, 4714c9f4cd71SHuang Ying .src = src, 4715c9f4cd71SHuang Ying .vma = vma, 4716c9f4cd71SHuang Ying }; 471747ad8475SAndrea Arcangeli 471847ad8475SAndrea Arcangeli if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { 471947ad8475SAndrea Arcangeli copy_user_gigantic_page(dst, src, addr, vma, 472047ad8475SAndrea Arcangeli pages_per_huge_page); 472147ad8475SAndrea Arcangeli return; 472247ad8475SAndrea Arcangeli } 472347ad8475SAndrea Arcangeli 4724c9f4cd71SHuang Ying process_huge_page(addr_hint, pages_per_huge_page, copy_subpage, &arg); 472547ad8475SAndrea Arcangeli } 4726fa4d75c1SMike Kravetz 4727fa4d75c1SMike Kravetz long copy_huge_page_from_user(struct page *dst_page, 4728fa4d75c1SMike Kravetz const void __user *usr_src, 4729810a56b9SMike Kravetz unsigned int pages_per_huge_page, 4730810a56b9SMike Kravetz bool allow_pagefault) 4731fa4d75c1SMike Kravetz { 4732fa4d75c1SMike Kravetz void *src = (void *)usr_src; 4733fa4d75c1SMike Kravetz void *page_kaddr; 4734fa4d75c1SMike Kravetz unsigned long i, rc = 0; 4735fa4d75c1SMike Kravetz unsigned long ret_val = pages_per_huge_page * PAGE_SIZE; 4736fa4d75c1SMike Kravetz 4737fa4d75c1SMike Kravetz for (i = 0; i < pages_per_huge_page; i++) { 4738810a56b9SMike Kravetz if (allow_pagefault) 4739810a56b9SMike Kravetz page_kaddr = kmap(dst_page + i); 4740810a56b9SMike Kravetz else 4741fa4d75c1SMike Kravetz page_kaddr = kmap_atomic(dst_page + i); 4742fa4d75c1SMike Kravetz rc = copy_from_user(page_kaddr, 4743fa4d75c1SMike Kravetz (const void __user *)(src + i * PAGE_SIZE), 4744fa4d75c1SMike Kravetz PAGE_SIZE); 4745810a56b9SMike Kravetz if (allow_pagefault) 4746810a56b9SMike Kravetz kunmap(dst_page + i); 4747810a56b9SMike Kravetz else 4748fa4d75c1SMike Kravetz kunmap_atomic(page_kaddr); 4749fa4d75c1SMike Kravetz 4750fa4d75c1SMike Kravetz ret_val -= (PAGE_SIZE - rc); 4751fa4d75c1SMike Kravetz if (rc) 4752fa4d75c1SMike Kravetz break; 4753fa4d75c1SMike Kravetz 4754fa4d75c1SMike Kravetz cond_resched(); 4755fa4d75c1SMike Kravetz } 4756fa4d75c1SMike Kravetz return ret_val; 4757fa4d75c1SMike Kravetz } 475847ad8475SAndrea Arcangeli #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */ 475949076ec2SKirill A. Shutemov 476040b64acdSOlof Johansson #if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS 4761b35f1819SKirill A. Shutemov 4762b35f1819SKirill A. Shutemov static struct kmem_cache *page_ptl_cachep; 4763b35f1819SKirill A. Shutemov 4764b35f1819SKirill A. Shutemov void __init ptlock_cache_init(void) 4765b35f1819SKirill A. Shutemov { 4766b35f1819SKirill A. Shutemov page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0, 4767b35f1819SKirill A. Shutemov SLAB_PANIC, NULL); 4768b35f1819SKirill A. Shutemov } 4769b35f1819SKirill A. Shutemov 4770539edb58SPeter Zijlstra bool ptlock_alloc(struct page *page) 477149076ec2SKirill A. Shutemov { 477249076ec2SKirill A. Shutemov spinlock_t *ptl; 477349076ec2SKirill A. Shutemov 4774b35f1819SKirill A. Shutemov ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL); 477549076ec2SKirill A. Shutemov if (!ptl) 477649076ec2SKirill A. Shutemov return false; 4777539edb58SPeter Zijlstra page->ptl = ptl; 477849076ec2SKirill A. Shutemov return true; 477949076ec2SKirill A. Shutemov } 478049076ec2SKirill A. Shutemov 4781539edb58SPeter Zijlstra void ptlock_free(struct page *page) 478249076ec2SKirill A. Shutemov { 4783b35f1819SKirill A. Shutemov kmem_cache_free(page_ptl_cachep, page->ptl); 478449076ec2SKirill A. Shutemov } 478549076ec2SKirill A. Shutemov #endif 4786