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 756952b61dSAlexey Dobriyan #include <asm/io.h> 7633a709b2SDave Hansen #include <asm/mmu_context.h> 771da177e4SLinus Torvalds #include <asm/pgalloc.h> 787c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 791da177e4SLinus Torvalds #include <asm/tlb.h> 801da177e4SLinus Torvalds #include <asm/tlbflush.h> 811da177e4SLinus Torvalds #include <asm/pgtable.h> 821da177e4SLinus Torvalds 8342b77728SJan Beulich #include "internal.h" 8442b77728SJan Beulich 85af27d940SArnd Bergmann #if defined(LAST_CPUPID_NOT_IN_PAGE_FLAGS) && !defined(CONFIG_COMPILE_TEST) 8690572890SPeter Zijlstra #warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid. 8775980e97SPeter Zijlstra #endif 8875980e97SPeter Zijlstra 89d41dee36SAndy Whitcroft #ifndef CONFIG_NEED_MULTIPLE_NODES 901da177e4SLinus Torvalds /* use the per-pgdat data instead for discontigmem - mbligh */ 911da177e4SLinus Torvalds unsigned long max_mapnr; 921da177e4SLinus Torvalds EXPORT_SYMBOL(max_mapnr); 93166f61b9STobin C Harding 94166f61b9STobin C Harding struct page *mem_map; 951da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map); 961da177e4SLinus Torvalds #endif 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds /* 991da177e4SLinus Torvalds * A number of key systems in x86 including ioremap() rely on the assumption 1001da177e4SLinus Torvalds * that high_memory defines the upper bound on direct map memory, then end 1011da177e4SLinus Torvalds * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and 1021da177e4SLinus Torvalds * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL 1031da177e4SLinus Torvalds * and ZONE_HIGHMEM. 1041da177e4SLinus Torvalds */ 1051da177e4SLinus Torvalds void *high_memory; 1061da177e4SLinus Torvalds EXPORT_SYMBOL(high_memory); 1071da177e4SLinus Torvalds 10832a93233SIngo Molnar /* 10932a93233SIngo Molnar * Randomize the address space (stacks, mmaps, brk, etc.). 11032a93233SIngo Molnar * 11132a93233SIngo Molnar * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization, 11232a93233SIngo Molnar * as ancient (libc5 based) binaries can segfault. ) 11332a93233SIngo Molnar */ 11432a93233SIngo Molnar int randomize_va_space __read_mostly = 11532a93233SIngo Molnar #ifdef CONFIG_COMPAT_BRK 11632a93233SIngo Molnar 1; 11732a93233SIngo Molnar #else 11832a93233SIngo Molnar 2; 11932a93233SIngo Molnar #endif 120a62eaf15SAndi Kleen 121*83d116c5SJia He #ifndef arch_faults_on_old_pte 122*83d116c5SJia He static inline bool arch_faults_on_old_pte(void) 123*83d116c5SJia He { 124*83d116c5SJia He /* 125*83d116c5SJia He * Those arches which don't have hw access flag feature need to 126*83d116c5SJia He * implement their own helper. By default, "true" means pagefault 127*83d116c5SJia He * will be hit on old pte. 128*83d116c5SJia He */ 129*83d116c5SJia He return true; 130*83d116c5SJia He } 131*83d116c5SJia He #endif 132*83d116c5SJia He 133a62eaf15SAndi Kleen static int __init disable_randmaps(char *s) 134a62eaf15SAndi Kleen { 135a62eaf15SAndi Kleen randomize_va_space = 0; 1369b41046cSOGAWA Hirofumi return 1; 137a62eaf15SAndi Kleen } 138a62eaf15SAndi Kleen __setup("norandmaps", disable_randmaps); 139a62eaf15SAndi Kleen 14062eede62SHugh Dickins unsigned long zero_pfn __read_mostly; 1410b70068eSArd Biesheuvel EXPORT_SYMBOL(zero_pfn); 1420b70068eSArd Biesheuvel 143166f61b9STobin C Harding unsigned long highest_memmap_pfn __read_mostly; 144166f61b9STobin C Harding 145a13ea5b7SHugh Dickins /* 146a13ea5b7SHugh Dickins * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init() 147a13ea5b7SHugh Dickins */ 148a13ea5b7SHugh Dickins static int __init init_zero_pfn(void) 149a13ea5b7SHugh Dickins { 150a13ea5b7SHugh Dickins zero_pfn = page_to_pfn(ZERO_PAGE(0)); 151a13ea5b7SHugh Dickins return 0; 152a13ea5b7SHugh Dickins } 153a13ea5b7SHugh Dickins core_initcall(init_zero_pfn); 154a62eaf15SAndi Kleen 155d559db08SKAMEZAWA Hiroyuki 15634e55232SKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING) 15734e55232SKAMEZAWA Hiroyuki 158ea48cf78SDavid Rientjes void sync_mm_rss(struct mm_struct *mm) 15934e55232SKAMEZAWA Hiroyuki { 16034e55232SKAMEZAWA Hiroyuki int i; 16134e55232SKAMEZAWA Hiroyuki 16234e55232SKAMEZAWA Hiroyuki for (i = 0; i < NR_MM_COUNTERS; i++) { 16305af2e10SDavid Rientjes if (current->rss_stat.count[i]) { 16405af2e10SDavid Rientjes add_mm_counter(mm, i, current->rss_stat.count[i]); 16505af2e10SDavid Rientjes current->rss_stat.count[i] = 0; 16634e55232SKAMEZAWA Hiroyuki } 16734e55232SKAMEZAWA Hiroyuki } 16805af2e10SDavid Rientjes current->rss_stat.events = 0; 16934e55232SKAMEZAWA Hiroyuki } 17034e55232SKAMEZAWA Hiroyuki 17134e55232SKAMEZAWA Hiroyuki static void add_mm_counter_fast(struct mm_struct *mm, int member, int val) 17234e55232SKAMEZAWA Hiroyuki { 17334e55232SKAMEZAWA Hiroyuki struct task_struct *task = current; 17434e55232SKAMEZAWA Hiroyuki 17534e55232SKAMEZAWA Hiroyuki if (likely(task->mm == mm)) 17634e55232SKAMEZAWA Hiroyuki task->rss_stat.count[member] += val; 17734e55232SKAMEZAWA Hiroyuki else 17834e55232SKAMEZAWA Hiroyuki add_mm_counter(mm, member, val); 17934e55232SKAMEZAWA Hiroyuki } 18034e55232SKAMEZAWA Hiroyuki #define inc_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, 1) 18134e55232SKAMEZAWA Hiroyuki #define dec_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, -1) 18234e55232SKAMEZAWA Hiroyuki 18334e55232SKAMEZAWA Hiroyuki /* sync counter once per 64 page faults */ 18434e55232SKAMEZAWA Hiroyuki #define TASK_RSS_EVENTS_THRESH (64) 18534e55232SKAMEZAWA Hiroyuki static void check_sync_rss_stat(struct task_struct *task) 18634e55232SKAMEZAWA Hiroyuki { 18734e55232SKAMEZAWA Hiroyuki if (unlikely(task != current)) 18834e55232SKAMEZAWA Hiroyuki return; 18934e55232SKAMEZAWA Hiroyuki if (unlikely(task->rss_stat.events++ > TASK_RSS_EVENTS_THRESH)) 190ea48cf78SDavid Rientjes sync_mm_rss(task->mm); 19134e55232SKAMEZAWA Hiroyuki } 1929547d01bSPeter Zijlstra #else /* SPLIT_RSS_COUNTING */ 19334e55232SKAMEZAWA Hiroyuki 19434e55232SKAMEZAWA Hiroyuki #define inc_mm_counter_fast(mm, member) inc_mm_counter(mm, member) 19534e55232SKAMEZAWA Hiroyuki #define dec_mm_counter_fast(mm, member) dec_mm_counter(mm, member) 19634e55232SKAMEZAWA Hiroyuki 19734e55232SKAMEZAWA Hiroyuki static void check_sync_rss_stat(struct task_struct *task) 19834e55232SKAMEZAWA Hiroyuki { 19934e55232SKAMEZAWA Hiroyuki } 20034e55232SKAMEZAWA Hiroyuki 2019547d01bSPeter Zijlstra #endif /* SPLIT_RSS_COUNTING */ 2029547d01bSPeter Zijlstra 2031da177e4SLinus Torvalds /* 2041da177e4SLinus Torvalds * Note: this doesn't free the actual pages themselves. That 2051da177e4SLinus Torvalds * has been handled earlier when unmapping all the memory regions. 2061da177e4SLinus Torvalds */ 2079e1b32caSBenjamin Herrenschmidt static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd, 2089e1b32caSBenjamin Herrenschmidt unsigned long addr) 2091da177e4SLinus Torvalds { 2102f569afdSMartin Schwidefsky pgtable_t token = pmd_pgtable(*pmd); 2111da177e4SLinus Torvalds pmd_clear(pmd); 2129e1b32caSBenjamin Herrenschmidt pte_free_tlb(tlb, token, addr); 213c4812909SKirill A. Shutemov mm_dec_nr_ptes(tlb->mm); 2141da177e4SLinus Torvalds } 2151da177e4SLinus Torvalds 216e0da382cSHugh Dickins static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud, 217e0da382cSHugh Dickins unsigned long addr, unsigned long end, 218e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 2191da177e4SLinus Torvalds { 2201da177e4SLinus Torvalds pmd_t *pmd; 2211da177e4SLinus Torvalds unsigned long next; 222e0da382cSHugh Dickins unsigned long start; 2231da177e4SLinus Torvalds 224e0da382cSHugh Dickins start = addr; 2251da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 2261da177e4SLinus Torvalds do { 2271da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 2281da177e4SLinus Torvalds if (pmd_none_or_clear_bad(pmd)) 2291da177e4SLinus Torvalds continue; 2309e1b32caSBenjamin Herrenschmidt free_pte_range(tlb, pmd, addr); 2311da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 2321da177e4SLinus Torvalds 233e0da382cSHugh Dickins start &= PUD_MASK; 234e0da382cSHugh Dickins if (start < floor) 235e0da382cSHugh Dickins return; 236e0da382cSHugh Dickins if (ceiling) { 237e0da382cSHugh Dickins ceiling &= PUD_MASK; 238e0da382cSHugh Dickins if (!ceiling) 239e0da382cSHugh Dickins return; 2401da177e4SLinus Torvalds } 241e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 242e0da382cSHugh Dickins return; 243e0da382cSHugh Dickins 244e0da382cSHugh Dickins pmd = pmd_offset(pud, start); 245e0da382cSHugh Dickins pud_clear(pud); 2469e1b32caSBenjamin Herrenschmidt pmd_free_tlb(tlb, pmd, start); 247dc6c9a35SKirill A. Shutemov mm_dec_nr_pmds(tlb->mm); 2481da177e4SLinus Torvalds } 2491da177e4SLinus Torvalds 250c2febafcSKirill A. Shutemov static inline void free_pud_range(struct mmu_gather *tlb, p4d_t *p4d, 251e0da382cSHugh Dickins unsigned long addr, unsigned long end, 252e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 2531da177e4SLinus Torvalds { 2541da177e4SLinus Torvalds pud_t *pud; 2551da177e4SLinus Torvalds unsigned long next; 256e0da382cSHugh Dickins unsigned long start; 2571da177e4SLinus Torvalds 258e0da382cSHugh Dickins start = addr; 259c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 2601da177e4SLinus Torvalds do { 2611da177e4SLinus Torvalds next = pud_addr_end(addr, end); 2621da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 2631da177e4SLinus Torvalds continue; 264e0da382cSHugh Dickins free_pmd_range(tlb, pud, addr, next, floor, ceiling); 2651da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 2661da177e4SLinus Torvalds 267c2febafcSKirill A. Shutemov start &= P4D_MASK; 268c2febafcSKirill A. Shutemov if (start < floor) 269c2febafcSKirill A. Shutemov return; 270c2febafcSKirill A. Shutemov if (ceiling) { 271c2febafcSKirill A. Shutemov ceiling &= P4D_MASK; 272c2febafcSKirill A. Shutemov if (!ceiling) 273c2febafcSKirill A. Shutemov return; 274c2febafcSKirill A. Shutemov } 275c2febafcSKirill A. Shutemov if (end - 1 > ceiling - 1) 276c2febafcSKirill A. Shutemov return; 277c2febafcSKirill A. Shutemov 278c2febafcSKirill A. Shutemov pud = pud_offset(p4d, start); 279c2febafcSKirill A. Shutemov p4d_clear(p4d); 280c2febafcSKirill A. Shutemov pud_free_tlb(tlb, pud, start); 281b4e98d9aSKirill A. Shutemov mm_dec_nr_puds(tlb->mm); 282c2febafcSKirill A. Shutemov } 283c2febafcSKirill A. Shutemov 284c2febafcSKirill A. Shutemov static inline void free_p4d_range(struct mmu_gather *tlb, pgd_t *pgd, 285c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 286c2febafcSKirill A. Shutemov unsigned long floor, unsigned long ceiling) 287c2febafcSKirill A. Shutemov { 288c2febafcSKirill A. Shutemov p4d_t *p4d; 289c2febafcSKirill A. Shutemov unsigned long next; 290c2febafcSKirill A. Shutemov unsigned long start; 291c2febafcSKirill A. Shutemov 292c2febafcSKirill A. Shutemov start = addr; 293c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 294c2febafcSKirill A. Shutemov do { 295c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 296c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(p4d)) 297c2febafcSKirill A. Shutemov continue; 298c2febafcSKirill A. Shutemov free_pud_range(tlb, p4d, addr, next, floor, ceiling); 299c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 300c2febafcSKirill A. Shutemov 301e0da382cSHugh Dickins start &= PGDIR_MASK; 302e0da382cSHugh Dickins if (start < floor) 303e0da382cSHugh Dickins return; 304e0da382cSHugh Dickins if (ceiling) { 305e0da382cSHugh Dickins ceiling &= PGDIR_MASK; 306e0da382cSHugh Dickins if (!ceiling) 307e0da382cSHugh Dickins return; 3081da177e4SLinus Torvalds } 309e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 310e0da382cSHugh Dickins return; 311e0da382cSHugh Dickins 312c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, start); 313e0da382cSHugh Dickins pgd_clear(pgd); 314c2febafcSKirill A. Shutemov p4d_free_tlb(tlb, p4d, start); 3151da177e4SLinus Torvalds } 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds /* 318e0da382cSHugh Dickins * This function frees user-level page tables of a process. 3191da177e4SLinus Torvalds */ 32042b77728SJan Beulich void free_pgd_range(struct mmu_gather *tlb, 321e0da382cSHugh Dickins unsigned long addr, unsigned long end, 322e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 3231da177e4SLinus Torvalds { 3241da177e4SLinus Torvalds pgd_t *pgd; 3251da177e4SLinus Torvalds unsigned long next; 3261da177e4SLinus Torvalds 327e0da382cSHugh Dickins /* 328e0da382cSHugh Dickins * The next few lines have given us lots of grief... 329e0da382cSHugh Dickins * 330e0da382cSHugh Dickins * Why are we testing PMD* at this top level? Because often 331e0da382cSHugh Dickins * there will be no work to do at all, and we'd prefer not to 332e0da382cSHugh Dickins * go all the way down to the bottom just to discover that. 333e0da382cSHugh Dickins * 334e0da382cSHugh Dickins * Why all these "- 1"s? Because 0 represents both the bottom 335e0da382cSHugh Dickins * of the address space and the top of it (using -1 for the 336e0da382cSHugh Dickins * top wouldn't help much: the masks would do the wrong thing). 337e0da382cSHugh Dickins * The rule is that addr 0 and floor 0 refer to the bottom of 338e0da382cSHugh Dickins * the address space, but end 0 and ceiling 0 refer to the top 339e0da382cSHugh Dickins * Comparisons need to use "end - 1" and "ceiling - 1" (though 340e0da382cSHugh Dickins * that end 0 case should be mythical). 341e0da382cSHugh Dickins * 342e0da382cSHugh Dickins * Wherever addr is brought up or ceiling brought down, we must 343e0da382cSHugh Dickins * be careful to reject "the opposite 0" before it confuses the 344e0da382cSHugh Dickins * subsequent tests. But what about where end is brought down 345e0da382cSHugh Dickins * by PMD_SIZE below? no, end can't go down to 0 there. 346e0da382cSHugh Dickins * 347e0da382cSHugh Dickins * Whereas we round start (addr) and ceiling down, by different 348e0da382cSHugh Dickins * masks at different levels, in order to test whether a table 349e0da382cSHugh Dickins * now has no other vmas using it, so can be freed, we don't 350e0da382cSHugh Dickins * bother to round floor or end up - the tests don't need that. 351e0da382cSHugh Dickins */ 352e0da382cSHugh Dickins 353e0da382cSHugh Dickins addr &= PMD_MASK; 354e0da382cSHugh Dickins if (addr < floor) { 355e0da382cSHugh Dickins addr += PMD_SIZE; 356e0da382cSHugh Dickins if (!addr) 357e0da382cSHugh Dickins return; 358e0da382cSHugh Dickins } 359e0da382cSHugh Dickins if (ceiling) { 360e0da382cSHugh Dickins ceiling &= PMD_MASK; 361e0da382cSHugh Dickins if (!ceiling) 362e0da382cSHugh Dickins return; 363e0da382cSHugh Dickins } 364e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 365e0da382cSHugh Dickins end -= PMD_SIZE; 366e0da382cSHugh Dickins if (addr > end - 1) 367e0da382cSHugh Dickins return; 36807e32661SAneesh Kumar K.V /* 36907e32661SAneesh Kumar K.V * We add page table cache pages with PAGE_SIZE, 37007e32661SAneesh Kumar K.V * (see pte_free_tlb()), flush the tlb if we need 37107e32661SAneesh Kumar K.V */ 372ed6a7935SPeter Zijlstra tlb_change_page_size(tlb, PAGE_SIZE); 37342b77728SJan Beulich pgd = pgd_offset(tlb->mm, addr); 3741da177e4SLinus Torvalds do { 3751da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 3761da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 3771da177e4SLinus Torvalds continue; 378c2febafcSKirill A. Shutemov free_p4d_range(tlb, pgd, addr, next, floor, ceiling); 3791da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 380e0da382cSHugh Dickins } 381e0da382cSHugh Dickins 38242b77728SJan Beulich void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma, 383e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 384e0da382cSHugh Dickins { 385e0da382cSHugh Dickins while (vma) { 386e0da382cSHugh Dickins struct vm_area_struct *next = vma->vm_next; 387e0da382cSHugh Dickins unsigned long addr = vma->vm_start; 388e0da382cSHugh Dickins 3898f4f8c16SHugh Dickins /* 39025d9e2d1Snpiggin@suse.de * Hide vma from rmap and truncate_pagecache before freeing 39125d9e2d1Snpiggin@suse.de * pgtables 3928f4f8c16SHugh Dickins */ 3935beb4930SRik van Riel unlink_anon_vmas(vma); 3948f4f8c16SHugh Dickins unlink_file_vma(vma); 3958f4f8c16SHugh Dickins 3969da61aefSDavid Gibson if (is_vm_hugetlb_page(vma)) { 3973bf5ee95SHugh Dickins hugetlb_free_pgd_range(tlb, addr, vma->vm_end, 3983bf5ee95SHugh Dickins floor, next ? next->vm_start : ceiling); 3993bf5ee95SHugh Dickins } else { 4003bf5ee95SHugh Dickins /* 4013bf5ee95SHugh Dickins * Optimization: gather nearby vmas into one call down 4023bf5ee95SHugh Dickins */ 4033bf5ee95SHugh Dickins while (next && next->vm_start <= vma->vm_end + PMD_SIZE 4044866920bSDavid Gibson && !is_vm_hugetlb_page(next)) { 405e0da382cSHugh Dickins vma = next; 406e0da382cSHugh Dickins next = vma->vm_next; 4075beb4930SRik van Riel unlink_anon_vmas(vma); 4088f4f8c16SHugh Dickins unlink_file_vma(vma); 409e0da382cSHugh Dickins } 4103bf5ee95SHugh Dickins free_pgd_range(tlb, addr, vma->vm_end, 411e0da382cSHugh Dickins floor, next ? next->vm_start : ceiling); 4123bf5ee95SHugh Dickins } 413e0da382cSHugh Dickins vma = next; 414e0da382cSHugh Dickins } 4151da177e4SLinus Torvalds } 4161da177e4SLinus Torvalds 4174cf58924SJoel Fernandes (Google) int __pte_alloc(struct mm_struct *mm, pmd_t *pmd) 4181da177e4SLinus Torvalds { 419c4088ebdSKirill A. Shutemov spinlock_t *ptl; 4204cf58924SJoel Fernandes (Google) pgtable_t new = pte_alloc_one(mm); 4211da177e4SLinus Torvalds if (!new) 4221bb3630eSHugh Dickins return -ENOMEM; 4231bb3630eSHugh Dickins 424362a61adSNick Piggin /* 425362a61adSNick Piggin * Ensure all pte setup (eg. pte page lock and page clearing) are 426362a61adSNick Piggin * visible before the pte is made visible to other CPUs by being 427362a61adSNick Piggin * put into page tables. 428362a61adSNick Piggin * 429362a61adSNick Piggin * The other side of the story is the pointer chasing in the page 430362a61adSNick Piggin * table walking code (when walking the page table without locking; 431362a61adSNick Piggin * ie. most of the time). Fortunately, these data accesses consist 432362a61adSNick Piggin * of a chain of data-dependent loads, meaning most CPUs (alpha 433362a61adSNick Piggin * being the notable exception) will already guarantee loads are 434362a61adSNick Piggin * seen in-order. See the alpha page table accessors for the 435362a61adSNick Piggin * smp_read_barrier_depends() barriers in page table walking code. 436362a61adSNick Piggin */ 437362a61adSNick Piggin smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */ 438362a61adSNick Piggin 439c4088ebdSKirill A. Shutemov ptl = pmd_lock(mm, pmd); 4408ac1f832SAndrea Arcangeli if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 441c4812909SKirill A. Shutemov mm_inc_nr_ptes(mm); 4421da177e4SLinus Torvalds pmd_populate(mm, pmd, new); 4432f569afdSMartin Schwidefsky new = NULL; 4444b471e88SKirill A. Shutemov } 445c4088ebdSKirill A. Shutemov spin_unlock(ptl); 4462f569afdSMartin Schwidefsky if (new) 4472f569afdSMartin Schwidefsky pte_free(mm, new); 4481bb3630eSHugh Dickins return 0; 4491da177e4SLinus Torvalds } 4501da177e4SLinus Torvalds 4514cf58924SJoel Fernandes (Google) int __pte_alloc_kernel(pmd_t *pmd) 4521da177e4SLinus Torvalds { 4534cf58924SJoel Fernandes (Google) pte_t *new = pte_alloc_one_kernel(&init_mm); 4541da177e4SLinus Torvalds if (!new) 4551bb3630eSHugh Dickins return -ENOMEM; 4561da177e4SLinus Torvalds 457362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 458362a61adSNick Piggin 459872fec16SHugh Dickins spin_lock(&init_mm.page_table_lock); 4608ac1f832SAndrea Arcangeli if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 461872fec16SHugh Dickins pmd_populate_kernel(&init_mm, pmd, new); 4622f569afdSMartin Schwidefsky new = NULL; 4634b471e88SKirill A. Shutemov } 464872fec16SHugh Dickins spin_unlock(&init_mm.page_table_lock); 4652f569afdSMartin Schwidefsky if (new) 4662f569afdSMartin Schwidefsky pte_free_kernel(&init_mm, new); 4671bb3630eSHugh Dickins return 0; 4681da177e4SLinus Torvalds } 4691da177e4SLinus Torvalds 470d559db08SKAMEZAWA Hiroyuki static inline void init_rss_vec(int *rss) 471ae859762SHugh Dickins { 472d559db08SKAMEZAWA Hiroyuki memset(rss, 0, sizeof(int) * NR_MM_COUNTERS); 473d559db08SKAMEZAWA Hiroyuki } 474d559db08SKAMEZAWA Hiroyuki 475d559db08SKAMEZAWA Hiroyuki static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss) 476d559db08SKAMEZAWA Hiroyuki { 477d559db08SKAMEZAWA Hiroyuki int i; 478d559db08SKAMEZAWA Hiroyuki 47934e55232SKAMEZAWA Hiroyuki if (current->mm == mm) 48005af2e10SDavid Rientjes sync_mm_rss(mm); 481d559db08SKAMEZAWA Hiroyuki for (i = 0; i < NR_MM_COUNTERS; i++) 482d559db08SKAMEZAWA Hiroyuki if (rss[i]) 483d559db08SKAMEZAWA Hiroyuki add_mm_counter(mm, i, rss[i]); 484ae859762SHugh Dickins } 485ae859762SHugh Dickins 4861da177e4SLinus Torvalds /* 4876aab341eSLinus Torvalds * This function is called to print an error when a bad pte 4886aab341eSLinus Torvalds * is found. For example, we might have a PFN-mapped pte in 4896aab341eSLinus Torvalds * a region that doesn't allow it. 490b5810039SNick Piggin * 491b5810039SNick Piggin * The calling function must still handle the error. 492b5810039SNick Piggin */ 4933dc14741SHugh Dickins static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr, 4943dc14741SHugh Dickins pte_t pte, struct page *page) 495b5810039SNick Piggin { 4963dc14741SHugh Dickins pgd_t *pgd = pgd_offset(vma->vm_mm, addr); 497c2febafcSKirill A. Shutemov p4d_t *p4d = p4d_offset(pgd, addr); 498c2febafcSKirill A. Shutemov pud_t *pud = pud_offset(p4d, addr); 4993dc14741SHugh Dickins pmd_t *pmd = pmd_offset(pud, addr); 5003dc14741SHugh Dickins struct address_space *mapping; 5013dc14741SHugh Dickins pgoff_t index; 502d936cf9bSHugh Dickins static unsigned long resume; 503d936cf9bSHugh Dickins static unsigned long nr_shown; 504d936cf9bSHugh Dickins static unsigned long nr_unshown; 505d936cf9bSHugh Dickins 506d936cf9bSHugh Dickins /* 507d936cf9bSHugh Dickins * Allow a burst of 60 reports, then keep quiet for that minute; 508d936cf9bSHugh Dickins * or allow a steady drip of one report per second. 509d936cf9bSHugh Dickins */ 510d936cf9bSHugh Dickins if (nr_shown == 60) { 511d936cf9bSHugh Dickins if (time_before(jiffies, resume)) { 512d936cf9bSHugh Dickins nr_unshown++; 513d936cf9bSHugh Dickins return; 514d936cf9bSHugh Dickins } 515d936cf9bSHugh Dickins if (nr_unshown) { 5161170532bSJoe Perches pr_alert("BUG: Bad page map: %lu messages suppressed\n", 517d936cf9bSHugh Dickins nr_unshown); 518d936cf9bSHugh Dickins nr_unshown = 0; 519d936cf9bSHugh Dickins } 520d936cf9bSHugh Dickins nr_shown = 0; 521d936cf9bSHugh Dickins } 522d936cf9bSHugh Dickins if (nr_shown++ == 0) 523d936cf9bSHugh Dickins resume = jiffies + 60 * HZ; 5243dc14741SHugh Dickins 5253dc14741SHugh Dickins mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL; 5263dc14741SHugh Dickins index = linear_page_index(vma, addr); 5273dc14741SHugh Dickins 5281170532bSJoe Perches pr_alert("BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n", 5293dc14741SHugh Dickins current->comm, 5303dc14741SHugh Dickins (long long)pte_val(pte), (long long)pmd_val(*pmd)); 531718a3821SWu Fengguang if (page) 532f0b791a3SDave Hansen dump_page(page, "bad pte"); 5336aa9b8b2SKefeng Wang pr_alert("addr:%px vm_flags:%08lx anon_vma:%px mapping:%px index:%lx\n", 5343dc14741SHugh Dickins (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index); 535d75f773cSSakari Ailus pr_alert("file:%pD fault:%ps mmap:%ps readpage:%ps\n", 5362682582aSKonstantin Khlebnikov vma->vm_file, 5372682582aSKonstantin Khlebnikov vma->vm_ops ? vma->vm_ops->fault : NULL, 5382682582aSKonstantin Khlebnikov vma->vm_file ? vma->vm_file->f_op->mmap : NULL, 5392682582aSKonstantin Khlebnikov mapping ? mapping->a_ops->readpage : NULL); 540b5810039SNick Piggin dump_stack(); 541373d4d09SRusty Russell add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); 542b5810039SNick Piggin } 543b5810039SNick Piggin 544b5810039SNick Piggin /* 5457e675137SNick Piggin * vm_normal_page -- This function gets the "struct page" associated with a pte. 5466aab341eSLinus Torvalds * 5477e675137SNick Piggin * "Special" mappings do not wish to be associated with a "struct page" (either 5487e675137SNick Piggin * it doesn't exist, or it exists but they don't want to touch it). In this 5497e675137SNick Piggin * case, NULL is returned here. "Normal" mappings do have a struct page. 550b379d790SJared Hulbert * 5517e675137SNick Piggin * There are 2 broad cases. Firstly, an architecture may define a pte_special() 5527e675137SNick Piggin * pte bit, in which case this function is trivial. Secondly, an architecture 5537e675137SNick Piggin * may not have a spare pte bit, which requires a more complicated scheme, 5547e675137SNick Piggin * described below. 5557e675137SNick Piggin * 5567e675137SNick Piggin * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a 5577e675137SNick Piggin * special mapping (even if there are underlying and valid "struct pages"). 5587e675137SNick Piggin * COWed pages of a VM_PFNMAP are always normal. 5596aab341eSLinus Torvalds * 560b379d790SJared Hulbert * The way we recognize COWed pages within VM_PFNMAP mappings is through the 561b379d790SJared Hulbert * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit 5627e675137SNick Piggin * set, and the vm_pgoff will point to the first PFN mapped: thus every special 5637e675137SNick Piggin * mapping will always honor the rule 5646aab341eSLinus Torvalds * 5656aab341eSLinus Torvalds * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT) 5666aab341eSLinus Torvalds * 5677e675137SNick Piggin * And for normal mappings this is false. 568b379d790SJared Hulbert * 5697e675137SNick Piggin * This restricts such mappings to be a linear translation from virtual address 5707e675137SNick Piggin * to pfn. To get around this restriction, we allow arbitrary mappings so long 5717e675137SNick Piggin * as the vma is not a COW mapping; in that case, we know that all ptes are 5727e675137SNick Piggin * special (because none can have been COWed). 573b379d790SJared Hulbert * 574b379d790SJared Hulbert * 5757e675137SNick Piggin * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP. 5767e675137SNick Piggin * 577b379d790SJared Hulbert * VM_MIXEDMAP mappings can likewise contain memory with or without "struct 578b379d790SJared Hulbert * page" backing, however the difference is that _all_ pages with a struct 579b379d790SJared Hulbert * page (that is, those where pfn_valid is true) are refcounted and considered 580b379d790SJared Hulbert * normal pages by the VM. The disadvantage is that pages are refcounted 581b379d790SJared Hulbert * (which can be slower and simply not an option for some PFNMAP users). The 582b379d790SJared Hulbert * advantage is that we don't have to follow the strict linearity rule of 583b379d790SJared Hulbert * PFNMAP mappings in order to support COWable mappings. 584b379d790SJared Hulbert * 585ee498ed7SHugh Dickins */ 58625b2995aSChristoph Hellwig struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, 58725b2995aSChristoph Hellwig pte_t pte) 588ee498ed7SHugh Dickins { 58922b31eecSHugh Dickins unsigned long pfn = pte_pfn(pte); 5907e675137SNick Piggin 59100b3a331SLaurent Dufour if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL)) { 592b38af472SHugh Dickins if (likely(!pte_special(pte))) 59322b31eecSHugh Dickins goto check_pfn; 594667a0a06SDavid Vrabel if (vma->vm_ops && vma->vm_ops->find_special_page) 595667a0a06SDavid Vrabel return vma->vm_ops->find_special_page(vma, addr); 596a13ea5b7SHugh Dickins if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)) 597a13ea5b7SHugh Dickins return NULL; 598df6ad698SJérôme Glisse if (is_zero_pfn(pfn)) 599df6ad698SJérôme Glisse return NULL; 600e1fb4a08SDave Jiang if (pte_devmap(pte)) 601e1fb4a08SDave Jiang return NULL; 602e1fb4a08SDave Jiang 60322b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 6047e675137SNick Piggin return NULL; 6057e675137SNick Piggin } 6067e675137SNick Piggin 60700b3a331SLaurent Dufour /* !CONFIG_ARCH_HAS_PTE_SPECIAL case follows: */ 6087e675137SNick Piggin 609b379d790SJared Hulbert if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { 610b379d790SJared Hulbert if (vma->vm_flags & VM_MIXEDMAP) { 611b379d790SJared Hulbert if (!pfn_valid(pfn)) 612b379d790SJared Hulbert return NULL; 613b379d790SJared Hulbert goto out; 614b379d790SJared Hulbert } else { 6157e675137SNick Piggin unsigned long off; 6167e675137SNick Piggin off = (addr - vma->vm_start) >> PAGE_SHIFT; 6176aab341eSLinus Torvalds if (pfn == vma->vm_pgoff + off) 6186aab341eSLinus Torvalds return NULL; 61967121172SLinus Torvalds if (!is_cow_mapping(vma->vm_flags)) 620fb155c16SLinus Torvalds return NULL; 6216aab341eSLinus Torvalds } 622b379d790SJared Hulbert } 6236aab341eSLinus Torvalds 624b38af472SHugh Dickins if (is_zero_pfn(pfn)) 625b38af472SHugh Dickins return NULL; 62600b3a331SLaurent Dufour 62722b31eecSHugh Dickins check_pfn: 62822b31eecSHugh Dickins if (unlikely(pfn > highest_memmap_pfn)) { 62922b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 63022b31eecSHugh Dickins return NULL; 63122b31eecSHugh Dickins } 6326aab341eSLinus Torvalds 6336aab341eSLinus Torvalds /* 6347e675137SNick Piggin * NOTE! We still have PageReserved() pages in the page tables. 6357e675137SNick Piggin * eg. VDSO mappings can cause them to exist. 6366aab341eSLinus Torvalds */ 637b379d790SJared Hulbert out: 6386aab341eSLinus Torvalds return pfn_to_page(pfn); 639ee498ed7SHugh Dickins } 640ee498ed7SHugh Dickins 64128093f9fSGerald Schaefer #ifdef CONFIG_TRANSPARENT_HUGEPAGE 64228093f9fSGerald Schaefer struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr, 64328093f9fSGerald Schaefer pmd_t pmd) 64428093f9fSGerald Schaefer { 64528093f9fSGerald Schaefer unsigned long pfn = pmd_pfn(pmd); 64628093f9fSGerald Schaefer 64728093f9fSGerald Schaefer /* 64828093f9fSGerald Schaefer * There is no pmd_special() but there may be special pmds, e.g. 64928093f9fSGerald Schaefer * in a direct-access (dax) mapping, so let's just replicate the 65000b3a331SLaurent Dufour * !CONFIG_ARCH_HAS_PTE_SPECIAL case from vm_normal_page() here. 65128093f9fSGerald Schaefer */ 65228093f9fSGerald Schaefer if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { 65328093f9fSGerald Schaefer if (vma->vm_flags & VM_MIXEDMAP) { 65428093f9fSGerald Schaefer if (!pfn_valid(pfn)) 65528093f9fSGerald Schaefer return NULL; 65628093f9fSGerald Schaefer goto out; 65728093f9fSGerald Schaefer } else { 65828093f9fSGerald Schaefer unsigned long off; 65928093f9fSGerald Schaefer off = (addr - vma->vm_start) >> PAGE_SHIFT; 66028093f9fSGerald Schaefer if (pfn == vma->vm_pgoff + off) 66128093f9fSGerald Schaefer return NULL; 66228093f9fSGerald Schaefer if (!is_cow_mapping(vma->vm_flags)) 66328093f9fSGerald Schaefer return NULL; 66428093f9fSGerald Schaefer } 66528093f9fSGerald Schaefer } 66628093f9fSGerald Schaefer 667e1fb4a08SDave Jiang if (pmd_devmap(pmd)) 668e1fb4a08SDave Jiang return NULL; 66928093f9fSGerald Schaefer if (is_zero_pfn(pfn)) 67028093f9fSGerald Schaefer return NULL; 67128093f9fSGerald Schaefer if (unlikely(pfn > highest_memmap_pfn)) 67228093f9fSGerald Schaefer return NULL; 67328093f9fSGerald Schaefer 67428093f9fSGerald Schaefer /* 67528093f9fSGerald Schaefer * NOTE! We still have PageReserved() pages in the page tables. 67628093f9fSGerald Schaefer * eg. VDSO mappings can cause them to exist. 67728093f9fSGerald Schaefer */ 67828093f9fSGerald Schaefer out: 67928093f9fSGerald Schaefer return pfn_to_page(pfn); 68028093f9fSGerald Schaefer } 68128093f9fSGerald Schaefer #endif 68228093f9fSGerald Schaefer 683ee498ed7SHugh Dickins /* 6841da177e4SLinus Torvalds * copy one vm_area from one task to the other. Assumes the page tables 6851da177e4SLinus Torvalds * already present in the new task to be cleared in the whole range 6861da177e4SLinus Torvalds * covered by this vma. 6871da177e4SLinus Torvalds */ 6881da177e4SLinus Torvalds 689570a335bSHugh Dickins static inline unsigned long 6901da177e4SLinus Torvalds copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, 691b5810039SNick Piggin pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma, 6928c103762SHugh Dickins unsigned long addr, int *rss) 6931da177e4SLinus Torvalds { 694b5810039SNick Piggin unsigned long vm_flags = vma->vm_flags; 6951da177e4SLinus Torvalds pte_t pte = *src_pte; 6961da177e4SLinus Torvalds struct page *page; 6971da177e4SLinus Torvalds 6981da177e4SLinus Torvalds /* pte contains position in swap or file, so copy. */ 6991da177e4SLinus Torvalds if (unlikely(!pte_present(pte))) { 7000697212aSChristoph Lameter swp_entry_t entry = pte_to_swp_entry(pte); 7010697212aSChristoph Lameter 7022022b4d1SHugh Dickins if (likely(!non_swap_entry(entry))) { 703570a335bSHugh Dickins if (swap_duplicate(entry) < 0) 704570a335bSHugh Dickins return entry.val; 705570a335bSHugh Dickins 7061da177e4SLinus Torvalds /* make sure dst_mm is on swapoff's mmlist. */ 7071da177e4SLinus Torvalds if (unlikely(list_empty(&dst_mm->mmlist))) { 7081da177e4SLinus Torvalds spin_lock(&mmlist_lock); 709f412ac08SHugh Dickins if (list_empty(&dst_mm->mmlist)) 710f412ac08SHugh Dickins list_add(&dst_mm->mmlist, 711f412ac08SHugh Dickins &src_mm->mmlist); 7121da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 7131da177e4SLinus Torvalds } 714b084d435SKAMEZAWA Hiroyuki rss[MM_SWAPENTS]++; 7152022b4d1SHugh Dickins } else if (is_migration_entry(entry)) { 7169f9f1acdSKonstantin Khlebnikov page = migration_entry_to_page(entry); 7179f9f1acdSKonstantin Khlebnikov 718eca56ff9SJerome Marchand rss[mm_counter(page)]++; 7199f9f1acdSKonstantin Khlebnikov 7209f9f1acdSKonstantin Khlebnikov if (is_write_migration_entry(entry) && 7210697212aSChristoph Lameter is_cow_mapping(vm_flags)) { 7220697212aSChristoph Lameter /* 7239f9f1acdSKonstantin Khlebnikov * COW mappings require pages in both 7249f9f1acdSKonstantin Khlebnikov * parent and child to be set to read. 7250697212aSChristoph Lameter */ 7260697212aSChristoph Lameter make_migration_entry_read(&entry); 7270697212aSChristoph Lameter pte = swp_entry_to_pte(entry); 728c3d16e16SCyrill Gorcunov if (pte_swp_soft_dirty(*src_pte)) 729c3d16e16SCyrill Gorcunov pte = pte_swp_mksoft_dirty(pte); 7300697212aSChristoph Lameter set_pte_at(src_mm, addr, src_pte, pte); 7310697212aSChristoph Lameter } 7325042db43SJérôme Glisse } else if (is_device_private_entry(entry)) { 7335042db43SJérôme Glisse page = device_private_entry_to_page(entry); 7345042db43SJérôme Glisse 7355042db43SJérôme Glisse /* 7365042db43SJérôme Glisse * Update rss count even for unaddressable pages, as 7375042db43SJérôme Glisse * they should treated just like normal pages in this 7385042db43SJérôme Glisse * respect. 7395042db43SJérôme Glisse * 7405042db43SJérôme Glisse * We will likely want to have some new rss counters 7415042db43SJérôme Glisse * for unaddressable pages, at some point. But for now 7425042db43SJérôme Glisse * keep things as they are. 7435042db43SJérôme Glisse */ 7445042db43SJérôme Glisse get_page(page); 7455042db43SJérôme Glisse rss[mm_counter(page)]++; 7465042db43SJérôme Glisse page_dup_rmap(page, false); 7475042db43SJérôme Glisse 7485042db43SJérôme Glisse /* 7495042db43SJérôme Glisse * We do not preserve soft-dirty information, because so 7505042db43SJérôme Glisse * far, checkpoint/restore is the only feature that 7515042db43SJérôme Glisse * requires that. And checkpoint/restore does not work 7525042db43SJérôme Glisse * when a device driver is involved (you cannot easily 7535042db43SJérôme Glisse * save and restore device driver state). 7545042db43SJérôme Glisse */ 7555042db43SJérôme Glisse if (is_write_device_private_entry(entry) && 7565042db43SJérôme Glisse is_cow_mapping(vm_flags)) { 7575042db43SJérôme Glisse make_device_private_entry_read(&entry); 7585042db43SJérôme Glisse pte = swp_entry_to_pte(entry); 7595042db43SJérôme Glisse set_pte_at(src_mm, addr, src_pte, pte); 7605042db43SJérôme Glisse } 7611da177e4SLinus Torvalds } 762ae859762SHugh Dickins goto out_set_pte; 7631da177e4SLinus Torvalds } 7641da177e4SLinus Torvalds 7651da177e4SLinus Torvalds /* 7661da177e4SLinus Torvalds * If it's a COW mapping, write protect it both 7671da177e4SLinus Torvalds * in the parent and the child 7681da177e4SLinus Torvalds */ 7691b2de5d0SLinus Torvalds if (is_cow_mapping(vm_flags) && pte_write(pte)) { 7701da177e4SLinus Torvalds ptep_set_wrprotect(src_mm, addr, src_pte); 7713dc90795SZachary Amsden pte = pte_wrprotect(pte); 7721da177e4SLinus Torvalds } 7731da177e4SLinus Torvalds 7741da177e4SLinus Torvalds /* 7751da177e4SLinus Torvalds * If it's a shared mapping, mark it clean in 7761da177e4SLinus Torvalds * the child 7771da177e4SLinus Torvalds */ 7781da177e4SLinus Torvalds if (vm_flags & VM_SHARED) 7791da177e4SLinus Torvalds pte = pte_mkclean(pte); 7801da177e4SLinus Torvalds pte = pte_mkold(pte); 7816aab341eSLinus Torvalds 7826aab341eSLinus Torvalds page = vm_normal_page(vma, addr, pte); 7836aab341eSLinus Torvalds if (page) { 7841da177e4SLinus Torvalds get_page(page); 78553f9263bSKirill A. Shutemov page_dup_rmap(page, false); 786eca56ff9SJerome Marchand rss[mm_counter(page)]++; 787df6ad698SJérôme Glisse } else if (pte_devmap(pte)) { 788df6ad698SJérôme Glisse page = pte_page(pte); 7896aab341eSLinus Torvalds } 790ae859762SHugh Dickins 791ae859762SHugh Dickins out_set_pte: 792ae859762SHugh Dickins set_pte_at(dst_mm, addr, dst_pte, pte); 793570a335bSHugh Dickins return 0; 7941da177e4SLinus Torvalds } 7951da177e4SLinus Torvalds 79621bda264SJerome Marchand static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 7971da177e4SLinus Torvalds pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma, 7981da177e4SLinus Torvalds unsigned long addr, unsigned long end) 7991da177e4SLinus Torvalds { 800c36987e2SDaisuke Nishimura pte_t *orig_src_pte, *orig_dst_pte; 8011da177e4SLinus Torvalds pte_t *src_pte, *dst_pte; 802c74df32cSHugh Dickins spinlock_t *src_ptl, *dst_ptl; 803e040f218SHugh Dickins int progress = 0; 804d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 805570a335bSHugh Dickins swp_entry_t entry = (swp_entry_t){0}; 8061da177e4SLinus Torvalds 8071da177e4SLinus Torvalds again: 808d559db08SKAMEZAWA Hiroyuki init_rss_vec(rss); 809d559db08SKAMEZAWA Hiroyuki 810c74df32cSHugh Dickins dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl); 8111da177e4SLinus Torvalds if (!dst_pte) 8121da177e4SLinus Torvalds return -ENOMEM; 813ece0e2b6SPeter Zijlstra src_pte = pte_offset_map(src_pmd, addr); 8144c21e2f2SHugh Dickins src_ptl = pte_lockptr(src_mm, src_pmd); 815f20dc5f7SIngo Molnar spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 816c36987e2SDaisuke Nishimura orig_src_pte = src_pte; 817c36987e2SDaisuke Nishimura orig_dst_pte = dst_pte; 8186606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 8191da177e4SLinus Torvalds 8201da177e4SLinus Torvalds do { 8211da177e4SLinus Torvalds /* 8221da177e4SLinus Torvalds * We are holding two locks at this point - either of them 8231da177e4SLinus Torvalds * could generate latencies in another task on another CPU. 8241da177e4SLinus Torvalds */ 825e040f218SHugh Dickins if (progress >= 32) { 826e040f218SHugh Dickins progress = 0; 827e040f218SHugh Dickins if (need_resched() || 82895c354feSNick Piggin spin_needbreak(src_ptl) || spin_needbreak(dst_ptl)) 8291da177e4SLinus Torvalds break; 830e040f218SHugh Dickins } 8311da177e4SLinus Torvalds if (pte_none(*src_pte)) { 8321da177e4SLinus Torvalds progress++; 8331da177e4SLinus Torvalds continue; 8341da177e4SLinus Torvalds } 835570a335bSHugh Dickins entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte, 836570a335bSHugh Dickins vma, addr, rss); 837570a335bSHugh Dickins if (entry.val) 838570a335bSHugh Dickins break; 8391da177e4SLinus Torvalds progress += 8; 8401da177e4SLinus Torvalds } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end); 8411da177e4SLinus Torvalds 8426606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 843c74df32cSHugh Dickins spin_unlock(src_ptl); 844ece0e2b6SPeter Zijlstra pte_unmap(orig_src_pte); 845d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(dst_mm, rss); 846c36987e2SDaisuke Nishimura pte_unmap_unlock(orig_dst_pte, dst_ptl); 847c74df32cSHugh Dickins cond_resched(); 848570a335bSHugh Dickins 849570a335bSHugh Dickins if (entry.val) { 850570a335bSHugh Dickins if (add_swap_count_continuation(entry, GFP_KERNEL) < 0) 851570a335bSHugh Dickins return -ENOMEM; 852570a335bSHugh Dickins progress = 0; 853570a335bSHugh Dickins } 8541da177e4SLinus Torvalds if (addr != end) 8551da177e4SLinus Torvalds goto again; 8561da177e4SLinus Torvalds return 0; 8571da177e4SLinus Torvalds } 8581da177e4SLinus Torvalds 8591da177e4SLinus Torvalds static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 8601da177e4SLinus Torvalds pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma, 8611da177e4SLinus Torvalds unsigned long addr, unsigned long end) 8621da177e4SLinus Torvalds { 8631da177e4SLinus Torvalds pmd_t *src_pmd, *dst_pmd; 8641da177e4SLinus Torvalds unsigned long next; 8651da177e4SLinus Torvalds 8661da177e4SLinus Torvalds dst_pmd = pmd_alloc(dst_mm, dst_pud, addr); 8671da177e4SLinus Torvalds if (!dst_pmd) 8681da177e4SLinus Torvalds return -ENOMEM; 8691da177e4SLinus Torvalds src_pmd = pmd_offset(src_pud, addr); 8701da177e4SLinus Torvalds do { 8711da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 87284c3fc4eSZi Yan if (is_swap_pmd(*src_pmd) || pmd_trans_huge(*src_pmd) 87384c3fc4eSZi Yan || pmd_devmap(*src_pmd)) { 87471e3aac0SAndrea Arcangeli int err; 875a00cc7d9SMatthew Wilcox VM_BUG_ON_VMA(next-addr != HPAGE_PMD_SIZE, vma); 87671e3aac0SAndrea Arcangeli err = copy_huge_pmd(dst_mm, src_mm, 87771e3aac0SAndrea Arcangeli dst_pmd, src_pmd, addr, vma); 87871e3aac0SAndrea Arcangeli if (err == -ENOMEM) 87971e3aac0SAndrea Arcangeli return -ENOMEM; 88071e3aac0SAndrea Arcangeli if (!err) 88171e3aac0SAndrea Arcangeli continue; 88271e3aac0SAndrea Arcangeli /* fall through */ 88371e3aac0SAndrea Arcangeli } 8841da177e4SLinus Torvalds if (pmd_none_or_clear_bad(src_pmd)) 8851da177e4SLinus Torvalds continue; 8861da177e4SLinus Torvalds if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd, 8871da177e4SLinus Torvalds vma, addr, next)) 8881da177e4SLinus Torvalds return -ENOMEM; 8891da177e4SLinus Torvalds } while (dst_pmd++, src_pmd++, addr = next, addr != end); 8901da177e4SLinus Torvalds return 0; 8911da177e4SLinus Torvalds } 8921da177e4SLinus Torvalds 8931da177e4SLinus Torvalds static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 894c2febafcSKirill A. Shutemov p4d_t *dst_p4d, p4d_t *src_p4d, struct vm_area_struct *vma, 8951da177e4SLinus Torvalds unsigned long addr, unsigned long end) 8961da177e4SLinus Torvalds { 8971da177e4SLinus Torvalds pud_t *src_pud, *dst_pud; 8981da177e4SLinus Torvalds unsigned long next; 8991da177e4SLinus Torvalds 900c2febafcSKirill A. Shutemov dst_pud = pud_alloc(dst_mm, dst_p4d, addr); 9011da177e4SLinus Torvalds if (!dst_pud) 9021da177e4SLinus Torvalds return -ENOMEM; 903c2febafcSKirill A. Shutemov src_pud = pud_offset(src_p4d, addr); 9041da177e4SLinus Torvalds do { 9051da177e4SLinus Torvalds next = pud_addr_end(addr, end); 906a00cc7d9SMatthew Wilcox if (pud_trans_huge(*src_pud) || pud_devmap(*src_pud)) { 907a00cc7d9SMatthew Wilcox int err; 908a00cc7d9SMatthew Wilcox 909a00cc7d9SMatthew Wilcox VM_BUG_ON_VMA(next-addr != HPAGE_PUD_SIZE, vma); 910a00cc7d9SMatthew Wilcox err = copy_huge_pud(dst_mm, src_mm, 911a00cc7d9SMatthew Wilcox dst_pud, src_pud, addr, vma); 912a00cc7d9SMatthew Wilcox if (err == -ENOMEM) 913a00cc7d9SMatthew Wilcox return -ENOMEM; 914a00cc7d9SMatthew Wilcox if (!err) 915a00cc7d9SMatthew Wilcox continue; 916a00cc7d9SMatthew Wilcox /* fall through */ 917a00cc7d9SMatthew Wilcox } 9181da177e4SLinus Torvalds if (pud_none_or_clear_bad(src_pud)) 9191da177e4SLinus Torvalds continue; 9201da177e4SLinus Torvalds if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud, 9211da177e4SLinus Torvalds vma, addr, next)) 9221da177e4SLinus Torvalds return -ENOMEM; 9231da177e4SLinus Torvalds } while (dst_pud++, src_pud++, addr = next, addr != end); 9241da177e4SLinus Torvalds return 0; 9251da177e4SLinus Torvalds } 9261da177e4SLinus Torvalds 927c2febafcSKirill A. Shutemov static inline int copy_p4d_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 928c2febafcSKirill A. Shutemov pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma, 929c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end) 930c2febafcSKirill A. Shutemov { 931c2febafcSKirill A. Shutemov p4d_t *src_p4d, *dst_p4d; 932c2febafcSKirill A. Shutemov unsigned long next; 933c2febafcSKirill A. Shutemov 934c2febafcSKirill A. Shutemov dst_p4d = p4d_alloc(dst_mm, dst_pgd, addr); 935c2febafcSKirill A. Shutemov if (!dst_p4d) 936c2febafcSKirill A. Shutemov return -ENOMEM; 937c2febafcSKirill A. Shutemov src_p4d = p4d_offset(src_pgd, addr); 938c2febafcSKirill A. Shutemov do { 939c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 940c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(src_p4d)) 941c2febafcSKirill A. Shutemov continue; 942c2febafcSKirill A. Shutemov if (copy_pud_range(dst_mm, src_mm, dst_p4d, src_p4d, 943c2febafcSKirill A. Shutemov vma, addr, next)) 944c2febafcSKirill A. Shutemov return -ENOMEM; 945c2febafcSKirill A. Shutemov } while (dst_p4d++, src_p4d++, addr = next, addr != end); 946c2febafcSKirill A. Shutemov return 0; 947c2febafcSKirill A. Shutemov } 948c2febafcSKirill A. Shutemov 9491da177e4SLinus Torvalds int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 9501da177e4SLinus Torvalds struct vm_area_struct *vma) 9511da177e4SLinus Torvalds { 9521da177e4SLinus Torvalds pgd_t *src_pgd, *dst_pgd; 9531da177e4SLinus Torvalds unsigned long next; 9541da177e4SLinus Torvalds unsigned long addr = vma->vm_start; 9551da177e4SLinus Torvalds unsigned long end = vma->vm_end; 956ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 9572ec74c3eSSagi Grimberg bool is_cow; 958cddb8a5cSAndrea Arcangeli int ret; 9591da177e4SLinus Torvalds 960d992895bSNick Piggin /* 961d992895bSNick Piggin * Don't copy ptes where a page fault will fill them correctly. 962d992895bSNick Piggin * Fork becomes much lighter when there are big shared or private 963d992895bSNick Piggin * readonly mappings. The tradeoff is that copy_page_range is more 964d992895bSNick Piggin * efficient than faulting. 965d992895bSNick Piggin */ 9660661a336SKirill A. Shutemov if (!(vma->vm_flags & (VM_HUGETLB | VM_PFNMAP | VM_MIXEDMAP)) && 9670661a336SKirill A. Shutemov !vma->anon_vma) 968d992895bSNick Piggin return 0; 969d992895bSNick Piggin 9701da177e4SLinus Torvalds if (is_vm_hugetlb_page(vma)) 9711da177e4SLinus Torvalds return copy_hugetlb_page_range(dst_mm, src_mm, vma); 9721da177e4SLinus Torvalds 973b3b9c293SKonstantin Khlebnikov if (unlikely(vma->vm_flags & VM_PFNMAP)) { 9742ab64037Svenkatesh.pallipadi@intel.com /* 9752ab64037Svenkatesh.pallipadi@intel.com * We do not free on error cases below as remove_vma 9762ab64037Svenkatesh.pallipadi@intel.com * gets called on error from higher level routine 9772ab64037Svenkatesh.pallipadi@intel.com */ 9785180da41SSuresh Siddha ret = track_pfn_copy(vma); 9792ab64037Svenkatesh.pallipadi@intel.com if (ret) 9802ab64037Svenkatesh.pallipadi@intel.com return ret; 9812ab64037Svenkatesh.pallipadi@intel.com } 9822ab64037Svenkatesh.pallipadi@intel.com 983cddb8a5cSAndrea Arcangeli /* 984cddb8a5cSAndrea Arcangeli * We need to invalidate the secondary MMU mappings only when 985cddb8a5cSAndrea Arcangeli * there could be a permission downgrade on the ptes of the 986cddb8a5cSAndrea Arcangeli * parent mm. And a permission downgrade will only happen if 987cddb8a5cSAndrea Arcangeli * is_cow_mapping() returns true. 988cddb8a5cSAndrea Arcangeli */ 9892ec74c3eSSagi Grimberg is_cow = is_cow_mapping(vma->vm_flags); 990ac46d4f3SJérôme Glisse 991ac46d4f3SJérôme Glisse if (is_cow) { 9927269f999SJérôme Glisse mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_PAGE, 9937269f999SJérôme Glisse 0, vma, src_mm, addr, end); 994ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 995ac46d4f3SJérôme Glisse } 996cddb8a5cSAndrea Arcangeli 997cddb8a5cSAndrea Arcangeli ret = 0; 9981da177e4SLinus Torvalds dst_pgd = pgd_offset(dst_mm, addr); 9991da177e4SLinus Torvalds src_pgd = pgd_offset(src_mm, addr); 10001da177e4SLinus Torvalds do { 10011da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 10021da177e4SLinus Torvalds if (pgd_none_or_clear_bad(src_pgd)) 10031da177e4SLinus Torvalds continue; 1004c2febafcSKirill A. Shutemov if (unlikely(copy_p4d_range(dst_mm, src_mm, dst_pgd, src_pgd, 1005cddb8a5cSAndrea Arcangeli vma, addr, next))) { 1006cddb8a5cSAndrea Arcangeli ret = -ENOMEM; 1007cddb8a5cSAndrea Arcangeli break; 1008cddb8a5cSAndrea Arcangeli } 10091da177e4SLinus Torvalds } while (dst_pgd++, src_pgd++, addr = next, addr != end); 1010cddb8a5cSAndrea Arcangeli 10112ec74c3eSSagi Grimberg if (is_cow) 1012ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 1013cddb8a5cSAndrea Arcangeli return ret; 10141da177e4SLinus Torvalds } 10151da177e4SLinus Torvalds 101651c6f666SRobin Holt static unsigned long zap_pte_range(struct mmu_gather *tlb, 1017b5810039SNick Piggin struct vm_area_struct *vma, pmd_t *pmd, 10181da177e4SLinus Torvalds unsigned long addr, unsigned long end, 101997a89413SPeter Zijlstra struct zap_details *details) 10201da177e4SLinus Torvalds { 1021b5810039SNick Piggin struct mm_struct *mm = tlb->mm; 1022d16dfc55SPeter Zijlstra int force_flush = 0; 1023d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 102497a89413SPeter Zijlstra spinlock_t *ptl; 10255f1a1907SSteven Rostedt pte_t *start_pte; 102697a89413SPeter Zijlstra pte_t *pte; 10278a5f14a2SKirill A. Shutemov swp_entry_t entry; 1028d559db08SKAMEZAWA Hiroyuki 1029ed6a7935SPeter Zijlstra tlb_change_page_size(tlb, PAGE_SIZE); 1030d16dfc55SPeter Zijlstra again: 1031e303297eSPeter Zijlstra init_rss_vec(rss); 10325f1a1907SSteven Rostedt start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl); 10335f1a1907SSteven Rostedt pte = start_pte; 10343ea27719SMel Gorman flush_tlb_batched_pending(mm); 10356606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 10361da177e4SLinus Torvalds do { 10371da177e4SLinus Torvalds pte_t ptent = *pte; 1038166f61b9STobin C Harding if (pte_none(ptent)) 10391da177e4SLinus Torvalds continue; 104051c6f666SRobin Holt 10417b167b68SMinchan Kim if (need_resched()) 10427b167b68SMinchan Kim break; 10437b167b68SMinchan Kim 10446f5e6b9eSHugh Dickins if (pte_present(ptent)) { 10456f5e6b9eSHugh Dickins struct page *page; 10466f5e6b9eSHugh Dickins 104725b2995aSChristoph Hellwig page = vm_normal_page(vma, addr, ptent); 10481da177e4SLinus Torvalds if (unlikely(details) && page) { 10491da177e4SLinus Torvalds /* 10501da177e4SLinus Torvalds * unmap_shared_mapping_pages() wants to 10511da177e4SLinus Torvalds * invalidate cache without truncating: 10521da177e4SLinus Torvalds * unmap shared but keep private pages. 10531da177e4SLinus Torvalds */ 10541da177e4SLinus Torvalds if (details->check_mapping && 1055800d8c63SKirill A. Shutemov details->check_mapping != page_rmapping(page)) 10561da177e4SLinus Torvalds continue; 10571da177e4SLinus Torvalds } 1058b5810039SNick Piggin ptent = ptep_get_and_clear_full(mm, addr, pte, 1059a600388dSZachary Amsden tlb->fullmm); 10601da177e4SLinus Torvalds tlb_remove_tlb_entry(tlb, pte, addr); 10611da177e4SLinus Torvalds if (unlikely(!page)) 10621da177e4SLinus Torvalds continue; 1063eca56ff9SJerome Marchand 1064eca56ff9SJerome Marchand if (!PageAnon(page)) { 10651cf35d47SLinus Torvalds if (pte_dirty(ptent)) { 10661cf35d47SLinus Torvalds force_flush = 1; 10676237bcd9SHugh Dickins set_page_dirty(page); 10681cf35d47SLinus Torvalds } 10694917e5d0SJohannes Weiner if (pte_young(ptent) && 107064363aadSJoe Perches likely(!(vma->vm_flags & VM_SEQ_READ))) 1071bf3f3bc5SNick Piggin mark_page_accessed(page); 10726237bcd9SHugh Dickins } 1073eca56ff9SJerome Marchand rss[mm_counter(page)]--; 1074d281ee61SKirill A. Shutemov page_remove_rmap(page, false); 10753dc14741SHugh Dickins if (unlikely(page_mapcount(page) < 0)) 10763dc14741SHugh Dickins print_bad_pte(vma, addr, ptent, page); 1077e9d55e15SAneesh Kumar K.V if (unlikely(__tlb_remove_page(tlb, page))) { 10781cf35d47SLinus Torvalds force_flush = 1; 1079ce9ec37bSWill Deacon addr += PAGE_SIZE; 1080d16dfc55SPeter Zijlstra break; 10811cf35d47SLinus Torvalds } 10821da177e4SLinus Torvalds continue; 10831da177e4SLinus Torvalds } 10845042db43SJérôme Glisse 10855042db43SJérôme Glisse entry = pte_to_swp_entry(ptent); 10865042db43SJérôme Glisse if (non_swap_entry(entry) && is_device_private_entry(entry)) { 10875042db43SJérôme Glisse struct page *page = device_private_entry_to_page(entry); 10885042db43SJérôme Glisse 10895042db43SJérôme Glisse if (unlikely(details && details->check_mapping)) { 10905042db43SJérôme Glisse /* 10915042db43SJérôme Glisse * unmap_shared_mapping_pages() wants to 10925042db43SJérôme Glisse * invalidate cache without truncating: 10935042db43SJérôme Glisse * unmap shared but keep private pages. 10945042db43SJérôme Glisse */ 10955042db43SJérôme Glisse if (details->check_mapping != 10965042db43SJérôme Glisse page_rmapping(page)) 10975042db43SJérôme Glisse continue; 10985042db43SJérôme Glisse } 10995042db43SJérôme Glisse 11005042db43SJérôme Glisse pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); 11015042db43SJérôme Glisse rss[mm_counter(page)]--; 11025042db43SJérôme Glisse page_remove_rmap(page, false); 11035042db43SJérôme Glisse put_page(page); 11045042db43SJérôme Glisse continue; 11055042db43SJérôme Glisse } 11065042db43SJérôme Glisse 11073e8715fdSKirill A. Shutemov /* If details->check_mapping, we leave swap entries. */ 11083e8715fdSKirill A. Shutemov if (unlikely(details)) 11091da177e4SLinus Torvalds continue; 1110b084d435SKAMEZAWA Hiroyuki 1111b084d435SKAMEZAWA Hiroyuki if (!non_swap_entry(entry)) 1112b084d435SKAMEZAWA Hiroyuki rss[MM_SWAPENTS]--; 11139f9f1acdSKonstantin Khlebnikov else if (is_migration_entry(entry)) { 11149f9f1acdSKonstantin Khlebnikov struct page *page; 11159f9f1acdSKonstantin Khlebnikov 11169f9f1acdSKonstantin Khlebnikov page = migration_entry_to_page(entry); 1117eca56ff9SJerome Marchand rss[mm_counter(page)]--; 11189f9f1acdSKonstantin Khlebnikov } 1119b084d435SKAMEZAWA Hiroyuki if (unlikely(!free_swap_and_cache(entry))) 11202509ef26SHugh Dickins print_bad_pte(vma, addr, ptent, NULL); 11219888a1caSZachary Amsden pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); 112297a89413SPeter Zijlstra } while (pte++, addr += PAGE_SIZE, addr != end); 1123ae859762SHugh Dickins 1124d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(mm, rss); 11256606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 112651c6f666SRobin Holt 11271cf35d47SLinus Torvalds /* Do the actual TLB flush before dropping ptl */ 1128fb7332a9SWill Deacon if (force_flush) 11291cf35d47SLinus Torvalds tlb_flush_mmu_tlbonly(tlb); 11301cf35d47SLinus Torvalds pte_unmap_unlock(start_pte, ptl); 11311cf35d47SLinus Torvalds 11321cf35d47SLinus Torvalds /* 11331cf35d47SLinus Torvalds * If we forced a TLB flush (either due to running out of 11341cf35d47SLinus Torvalds * batch buffers or because we needed to flush dirty TLB 11351cf35d47SLinus Torvalds * entries before releasing the ptl), free the batched 11361cf35d47SLinus Torvalds * memory too. Restart if we didn't do everything. 11371cf35d47SLinus Torvalds */ 11381cf35d47SLinus Torvalds if (force_flush) { 11391cf35d47SLinus Torvalds force_flush = 0; 1140fa0aafb8SPeter Zijlstra tlb_flush_mmu(tlb); 11417b167b68SMinchan Kim } 11427b167b68SMinchan Kim 11437b167b68SMinchan Kim if (addr != end) { 11447b167b68SMinchan Kim cond_resched(); 1145d16dfc55SPeter Zijlstra goto again; 1146d16dfc55SPeter Zijlstra } 1147d16dfc55SPeter Zijlstra 114851c6f666SRobin Holt return addr; 11491da177e4SLinus Torvalds } 11501da177e4SLinus Torvalds 115151c6f666SRobin Holt static inline unsigned long zap_pmd_range(struct mmu_gather *tlb, 1152b5810039SNick Piggin struct vm_area_struct *vma, pud_t *pud, 11531da177e4SLinus Torvalds unsigned long addr, unsigned long end, 115497a89413SPeter Zijlstra struct zap_details *details) 11551da177e4SLinus Torvalds { 11561da177e4SLinus Torvalds pmd_t *pmd; 11571da177e4SLinus Torvalds unsigned long next; 11581da177e4SLinus Torvalds 11591da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 11601da177e4SLinus Torvalds do { 11611da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 116284c3fc4eSZi Yan if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) { 116353406ed1SHugh Dickins if (next - addr != HPAGE_PMD_SIZE) 1164fd60775aSDavid Rientjes __split_huge_pmd(vma, pmd, addr, false, NULL); 116553406ed1SHugh Dickins else if (zap_huge_pmd(tlb, vma, pmd, addr)) 11661a5a9906SAndrea Arcangeli goto next; 116771e3aac0SAndrea Arcangeli /* fall through */ 116871e3aac0SAndrea Arcangeli } 11691a5a9906SAndrea Arcangeli /* 11701a5a9906SAndrea Arcangeli * Here there can be other concurrent MADV_DONTNEED or 11711a5a9906SAndrea Arcangeli * trans huge page faults running, and if the pmd is 11721a5a9906SAndrea Arcangeli * none or trans huge it can change under us. This is 11731a5a9906SAndrea Arcangeli * because MADV_DONTNEED holds the mmap_sem in read 11741a5a9906SAndrea Arcangeli * mode. 11751a5a9906SAndrea Arcangeli */ 11761a5a9906SAndrea Arcangeli if (pmd_none_or_trans_huge_or_clear_bad(pmd)) 11771a5a9906SAndrea Arcangeli goto next; 117897a89413SPeter Zijlstra next = zap_pte_range(tlb, vma, pmd, addr, next, details); 11791a5a9906SAndrea Arcangeli next: 118097a89413SPeter Zijlstra cond_resched(); 118197a89413SPeter Zijlstra } while (pmd++, addr = next, addr != end); 118251c6f666SRobin Holt 118351c6f666SRobin Holt return addr; 11841da177e4SLinus Torvalds } 11851da177e4SLinus Torvalds 118651c6f666SRobin Holt static inline unsigned long zap_pud_range(struct mmu_gather *tlb, 1187c2febafcSKirill A. Shutemov struct vm_area_struct *vma, p4d_t *p4d, 11881da177e4SLinus Torvalds unsigned long addr, unsigned long end, 118997a89413SPeter Zijlstra struct zap_details *details) 11901da177e4SLinus Torvalds { 11911da177e4SLinus Torvalds pud_t *pud; 11921da177e4SLinus Torvalds unsigned long next; 11931da177e4SLinus Torvalds 1194c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 11951da177e4SLinus Torvalds do { 11961da177e4SLinus Torvalds next = pud_addr_end(addr, end); 1197a00cc7d9SMatthew Wilcox if (pud_trans_huge(*pud) || pud_devmap(*pud)) { 1198a00cc7d9SMatthew Wilcox if (next - addr != HPAGE_PUD_SIZE) { 1199a00cc7d9SMatthew Wilcox VM_BUG_ON_VMA(!rwsem_is_locked(&tlb->mm->mmap_sem), vma); 1200a00cc7d9SMatthew Wilcox split_huge_pud(vma, pud, addr); 1201a00cc7d9SMatthew Wilcox } else if (zap_huge_pud(tlb, vma, pud, addr)) 1202a00cc7d9SMatthew Wilcox goto next; 1203a00cc7d9SMatthew Wilcox /* fall through */ 1204a00cc7d9SMatthew Wilcox } 120597a89413SPeter Zijlstra if (pud_none_or_clear_bad(pud)) 12061da177e4SLinus Torvalds continue; 120797a89413SPeter Zijlstra next = zap_pmd_range(tlb, vma, pud, addr, next, details); 1208a00cc7d9SMatthew Wilcox next: 1209a00cc7d9SMatthew Wilcox cond_resched(); 121097a89413SPeter Zijlstra } while (pud++, addr = next, addr != end); 121151c6f666SRobin Holt 121251c6f666SRobin Holt return addr; 12131da177e4SLinus Torvalds } 12141da177e4SLinus Torvalds 1215c2febafcSKirill A. Shutemov static inline unsigned long zap_p4d_range(struct mmu_gather *tlb, 1216c2febafcSKirill A. Shutemov struct vm_area_struct *vma, pgd_t *pgd, 1217c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 1218c2febafcSKirill A. Shutemov struct zap_details *details) 1219c2febafcSKirill A. Shutemov { 1220c2febafcSKirill A. Shutemov p4d_t *p4d; 1221c2febafcSKirill A. Shutemov unsigned long next; 1222c2febafcSKirill A. Shutemov 1223c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 1224c2febafcSKirill A. Shutemov do { 1225c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 1226c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(p4d)) 1227c2febafcSKirill A. Shutemov continue; 1228c2febafcSKirill A. Shutemov next = zap_pud_range(tlb, vma, p4d, addr, next, details); 1229c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 1230c2febafcSKirill A. Shutemov 1231c2febafcSKirill A. Shutemov return addr; 1232c2febafcSKirill A. Shutemov } 1233c2febafcSKirill A. Shutemov 1234aac45363SMichal Hocko void unmap_page_range(struct mmu_gather *tlb, 123551c6f666SRobin Holt struct vm_area_struct *vma, 12361da177e4SLinus Torvalds unsigned long addr, unsigned long end, 123797a89413SPeter Zijlstra struct zap_details *details) 12381da177e4SLinus Torvalds { 12391da177e4SLinus Torvalds pgd_t *pgd; 12401da177e4SLinus Torvalds unsigned long next; 12411da177e4SLinus Torvalds 12421da177e4SLinus Torvalds BUG_ON(addr >= end); 12431da177e4SLinus Torvalds tlb_start_vma(tlb, vma); 12441da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 12451da177e4SLinus Torvalds do { 12461da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 124797a89413SPeter Zijlstra if (pgd_none_or_clear_bad(pgd)) 12481da177e4SLinus Torvalds continue; 1249c2febafcSKirill A. Shutemov next = zap_p4d_range(tlb, vma, pgd, addr, next, details); 125097a89413SPeter Zijlstra } while (pgd++, addr = next, addr != end); 12511da177e4SLinus Torvalds tlb_end_vma(tlb, vma); 12521da177e4SLinus Torvalds } 12531da177e4SLinus Torvalds 1254f5cc4eefSAl Viro 1255f5cc4eefSAl Viro static void unmap_single_vma(struct mmu_gather *tlb, 12561da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long start_addr, 12574f74d2c8SLinus Torvalds unsigned long end_addr, 12581da177e4SLinus Torvalds struct zap_details *details) 12591da177e4SLinus Torvalds { 1260f5cc4eefSAl Viro unsigned long start = max(vma->vm_start, start_addr); 12611da177e4SLinus Torvalds unsigned long end; 12621da177e4SLinus Torvalds 12631da177e4SLinus Torvalds if (start >= vma->vm_end) 1264f5cc4eefSAl Viro return; 12651da177e4SLinus Torvalds end = min(vma->vm_end, end_addr); 12661da177e4SLinus Torvalds if (end <= vma->vm_start) 1267f5cc4eefSAl Viro return; 12681da177e4SLinus Torvalds 1269cbc91f71SSrikar Dronamraju if (vma->vm_file) 1270cbc91f71SSrikar Dronamraju uprobe_munmap(vma, start, end); 1271cbc91f71SSrikar Dronamraju 1272b3b9c293SKonstantin Khlebnikov if (unlikely(vma->vm_flags & VM_PFNMAP)) 12735180da41SSuresh Siddha untrack_pfn(vma, 0, 0); 12742ab64037Svenkatesh.pallipadi@intel.com 12758b2a1238SAl Viro if (start != end) { 127651c6f666SRobin Holt if (unlikely(is_vm_hugetlb_page(vma))) { 1277a137e1ccSAndi Kleen /* 1278a137e1ccSAndi Kleen * It is undesirable to test vma->vm_file as it 1279a137e1ccSAndi Kleen * should be non-null for valid hugetlb area. 1280a137e1ccSAndi Kleen * However, vm_file will be NULL in the error 12817aa6b4adSDavidlohr Bueso * cleanup path of mmap_region. When 1282a137e1ccSAndi Kleen * hugetlbfs ->mmap method fails, 12837aa6b4adSDavidlohr Bueso * mmap_region() nullifies vma->vm_file 1284a137e1ccSAndi Kleen * before calling this function to clean up. 1285a137e1ccSAndi Kleen * Since no pte has actually been setup, it is 1286a137e1ccSAndi Kleen * safe to do nothing in this case. 1287a137e1ccSAndi Kleen */ 128824669e58SAneesh Kumar K.V if (vma->vm_file) { 128983cde9e8SDavidlohr Bueso i_mmap_lock_write(vma->vm_file->f_mapping); 1290d833352aSMel Gorman __unmap_hugepage_range_final(tlb, vma, start, end, NULL); 129183cde9e8SDavidlohr Bueso i_mmap_unlock_write(vma->vm_file->f_mapping); 129224669e58SAneesh Kumar K.V } 129351c6f666SRobin Holt } else 1294038c7aa1SAl Viro unmap_page_range(tlb, vma, start, end, details); 129597a89413SPeter Zijlstra } 129651c6f666SRobin Holt } 12971da177e4SLinus Torvalds 1298f5cc4eefSAl Viro /** 1299f5cc4eefSAl Viro * unmap_vmas - unmap a range of memory covered by a list of vma's 1300f5cc4eefSAl Viro * @tlb: address of the caller's struct mmu_gather 1301f5cc4eefSAl Viro * @vma: the starting vma 1302f5cc4eefSAl Viro * @start_addr: virtual address at which to start unmapping 1303f5cc4eefSAl Viro * @end_addr: virtual address at which to end unmapping 1304f5cc4eefSAl Viro * 1305f5cc4eefSAl Viro * Unmap all pages in the vma list. 1306f5cc4eefSAl Viro * 1307f5cc4eefSAl Viro * Only addresses between `start' and `end' will be unmapped. 1308f5cc4eefSAl Viro * 1309f5cc4eefSAl Viro * The VMA list must be sorted in ascending virtual address order. 1310f5cc4eefSAl Viro * 1311f5cc4eefSAl Viro * unmap_vmas() assumes that the caller will flush the whole unmapped address 1312f5cc4eefSAl Viro * range after unmap_vmas() returns. So the only responsibility here is to 1313f5cc4eefSAl Viro * ensure that any thus-far unmapped pages are flushed before unmap_vmas() 1314f5cc4eefSAl Viro * drops the lock and schedules. 1315f5cc4eefSAl Viro */ 1316f5cc4eefSAl Viro void unmap_vmas(struct mmu_gather *tlb, 1317f5cc4eefSAl Viro struct vm_area_struct *vma, unsigned long start_addr, 13184f74d2c8SLinus Torvalds unsigned long end_addr) 1319f5cc4eefSAl Viro { 1320ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 1321f5cc4eefSAl Viro 13226f4f13e8SJérôme Glisse mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma, vma->vm_mm, 13236f4f13e8SJérôme Glisse start_addr, end_addr); 1324ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 1325f5cc4eefSAl Viro for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) 13264f74d2c8SLinus Torvalds unmap_single_vma(tlb, vma, start_addr, end_addr, NULL); 1327ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 13281da177e4SLinus Torvalds } 13291da177e4SLinus Torvalds 13301da177e4SLinus Torvalds /** 13311da177e4SLinus Torvalds * zap_page_range - remove user pages in a given range 13321da177e4SLinus Torvalds * @vma: vm_area_struct holding the applicable pages 1333eb4546bbSRandy Dunlap * @start: starting address of pages to zap 13341da177e4SLinus Torvalds * @size: number of bytes to zap 1335f5cc4eefSAl Viro * 1336f5cc4eefSAl Viro * Caller must protect the VMA list 13371da177e4SLinus Torvalds */ 13387e027b14SLinus Torvalds void zap_page_range(struct vm_area_struct *vma, unsigned long start, 1339ecf1385dSKirill A. Shutemov unsigned long size) 13401da177e4SLinus Torvalds { 1341ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 1342d16dfc55SPeter Zijlstra struct mmu_gather tlb; 13431da177e4SLinus Torvalds 13441da177e4SLinus Torvalds lru_add_drain(); 13457269f999SJérôme Glisse mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm, 13466f4f13e8SJérôme Glisse start, start + size); 1347ac46d4f3SJérôme Glisse tlb_gather_mmu(&tlb, vma->vm_mm, start, range.end); 1348ac46d4f3SJérôme Glisse update_hiwater_rss(vma->vm_mm); 1349ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 1350ac46d4f3SJérôme Glisse for ( ; vma && vma->vm_start < range.end; vma = vma->vm_next) 1351ac46d4f3SJérôme Glisse unmap_single_vma(&tlb, vma, start, range.end, NULL); 1352ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 1353ac46d4f3SJérôme Glisse tlb_finish_mmu(&tlb, start, range.end); 13541da177e4SLinus Torvalds } 13551da177e4SLinus Torvalds 1356c627f9ccSJack Steiner /** 1357f5cc4eefSAl Viro * zap_page_range_single - remove user pages in a given range 1358f5cc4eefSAl Viro * @vma: vm_area_struct holding the applicable pages 1359f5cc4eefSAl Viro * @address: starting address of pages to zap 1360f5cc4eefSAl Viro * @size: number of bytes to zap 13618a5f14a2SKirill A. Shutemov * @details: details of shared cache invalidation 1362f5cc4eefSAl Viro * 1363f5cc4eefSAl Viro * The range must fit into one VMA. 1364f5cc4eefSAl Viro */ 1365f5cc4eefSAl Viro static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address, 1366f5cc4eefSAl Viro unsigned long size, struct zap_details *details) 1367f5cc4eefSAl Viro { 1368ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 1369f5cc4eefSAl Viro struct mmu_gather tlb; 1370f5cc4eefSAl Viro 1371f5cc4eefSAl Viro lru_add_drain(); 13727269f999SJérôme Glisse mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm, 13736f4f13e8SJérôme Glisse address, address + size); 1374ac46d4f3SJérôme Glisse tlb_gather_mmu(&tlb, vma->vm_mm, address, range.end); 1375ac46d4f3SJérôme Glisse update_hiwater_rss(vma->vm_mm); 1376ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 1377ac46d4f3SJérôme Glisse unmap_single_vma(&tlb, vma, address, range.end, details); 1378ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 1379ac46d4f3SJérôme Glisse tlb_finish_mmu(&tlb, address, range.end); 13801da177e4SLinus Torvalds } 13811da177e4SLinus Torvalds 1382c627f9ccSJack Steiner /** 1383c627f9ccSJack Steiner * zap_vma_ptes - remove ptes mapping the vma 1384c627f9ccSJack Steiner * @vma: vm_area_struct holding ptes to be zapped 1385c627f9ccSJack Steiner * @address: starting address of pages to zap 1386c627f9ccSJack Steiner * @size: number of bytes to zap 1387c627f9ccSJack Steiner * 1388c627f9ccSJack Steiner * This function only unmaps ptes assigned to VM_PFNMAP vmas. 1389c627f9ccSJack Steiner * 1390c627f9ccSJack Steiner * The entire address range must be fully contained within the vma. 1391c627f9ccSJack Steiner * 1392c627f9ccSJack Steiner */ 139327d036e3SLeon Romanovsky void zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, 1394c627f9ccSJack Steiner unsigned long size) 1395c627f9ccSJack Steiner { 1396c627f9ccSJack Steiner if (address < vma->vm_start || address + size > vma->vm_end || 1397c627f9ccSJack Steiner !(vma->vm_flags & VM_PFNMAP)) 139827d036e3SLeon Romanovsky return; 139927d036e3SLeon Romanovsky 1400f5cc4eefSAl Viro zap_page_range_single(vma, address, size, NULL); 1401c627f9ccSJack Steiner } 1402c627f9ccSJack Steiner EXPORT_SYMBOL_GPL(zap_vma_ptes); 1403c627f9ccSJack Steiner 140425ca1d6cSNamhyung Kim pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr, 1405920c7a5dSHarvey Harrison spinlock_t **ptl) 1406c9cfcddfSLinus Torvalds { 1407c2febafcSKirill A. Shutemov pgd_t *pgd; 1408c2febafcSKirill A. Shutemov p4d_t *p4d; 1409c2febafcSKirill A. Shutemov pud_t *pud; 1410c2febafcSKirill A. Shutemov pmd_t *pmd; 1411c2febafcSKirill A. Shutemov 1412c2febafcSKirill A. Shutemov pgd = pgd_offset(mm, addr); 1413c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, addr); 1414c2febafcSKirill A. Shutemov if (!p4d) 1415c2febafcSKirill A. Shutemov return NULL; 1416c2febafcSKirill A. Shutemov pud = pud_alloc(mm, p4d, addr); 1417c2febafcSKirill A. Shutemov if (!pud) 1418c2febafcSKirill A. Shutemov return NULL; 1419c2febafcSKirill A. Shutemov pmd = pmd_alloc(mm, pud, addr); 1420c2febafcSKirill A. Shutemov if (!pmd) 1421c2febafcSKirill A. Shutemov return NULL; 1422c2febafcSKirill A. Shutemov 1423f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 1424c9cfcddfSLinus Torvalds return pte_alloc_map_lock(mm, pmd, addr, ptl); 1425c9cfcddfSLinus Torvalds } 1426c9cfcddfSLinus Torvalds 14271da177e4SLinus Torvalds /* 1428238f58d8SLinus Torvalds * This is the old fallback for page remapping. 1429238f58d8SLinus Torvalds * 1430238f58d8SLinus Torvalds * For historical reasons, it only allows reserved pages. Only 1431238f58d8SLinus Torvalds * old drivers should use this, and they needed to mark their 1432238f58d8SLinus Torvalds * pages reserved for the old functions anyway. 1433238f58d8SLinus Torvalds */ 1434423bad60SNick Piggin static int insert_page(struct vm_area_struct *vma, unsigned long addr, 1435423bad60SNick Piggin struct page *page, pgprot_t prot) 1436238f58d8SLinus Torvalds { 1437423bad60SNick Piggin struct mm_struct *mm = vma->vm_mm; 1438238f58d8SLinus Torvalds int retval; 1439238f58d8SLinus Torvalds pte_t *pte; 1440238f58d8SLinus Torvalds spinlock_t *ptl; 1441238f58d8SLinus Torvalds 1442238f58d8SLinus Torvalds retval = -EINVAL; 14430ee930e6SMatthew Wilcox if (PageAnon(page) || PageSlab(page) || page_has_type(page)) 14445b4e655eSKAMEZAWA Hiroyuki goto out; 1445238f58d8SLinus Torvalds retval = -ENOMEM; 1446238f58d8SLinus Torvalds flush_dcache_page(page); 1447c9cfcddfSLinus Torvalds pte = get_locked_pte(mm, addr, &ptl); 1448238f58d8SLinus Torvalds if (!pte) 14495b4e655eSKAMEZAWA Hiroyuki goto out; 1450238f58d8SLinus Torvalds retval = -EBUSY; 1451238f58d8SLinus Torvalds if (!pte_none(*pte)) 1452238f58d8SLinus Torvalds goto out_unlock; 1453238f58d8SLinus Torvalds 1454238f58d8SLinus Torvalds /* Ok, finally just insert the thing.. */ 1455238f58d8SLinus Torvalds get_page(page); 1456eca56ff9SJerome Marchand inc_mm_counter_fast(mm, mm_counter_file(page)); 1457dd78feddSKirill A. Shutemov page_add_file_rmap(page, false); 1458238f58d8SLinus Torvalds set_pte_at(mm, addr, pte, mk_pte(page, prot)); 1459238f58d8SLinus Torvalds 1460238f58d8SLinus Torvalds retval = 0; 1461238f58d8SLinus Torvalds out_unlock: 1462238f58d8SLinus Torvalds pte_unmap_unlock(pte, ptl); 1463238f58d8SLinus Torvalds out: 1464238f58d8SLinus Torvalds return retval; 1465238f58d8SLinus Torvalds } 1466238f58d8SLinus Torvalds 1467bfa5bf6dSRolf Eike Beer /** 1468bfa5bf6dSRolf Eike Beer * vm_insert_page - insert single page into user vma 1469bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 1470bfa5bf6dSRolf Eike Beer * @addr: target user address of this page 1471bfa5bf6dSRolf Eike Beer * @page: source kernel page 1472bfa5bf6dSRolf Eike Beer * 1473a145dd41SLinus Torvalds * This allows drivers to insert individual pages they've allocated 1474a145dd41SLinus Torvalds * into a user vma. 1475a145dd41SLinus Torvalds * 1476a145dd41SLinus Torvalds * The page has to be a nice clean _individual_ kernel allocation. 1477a145dd41SLinus Torvalds * If you allocate a compound page, you need to have marked it as 1478a145dd41SLinus Torvalds * such (__GFP_COMP), or manually just split the page up yourself 14798dfcc9baSNick Piggin * (see split_page()). 1480a145dd41SLinus Torvalds * 1481a145dd41SLinus Torvalds * NOTE! Traditionally this was done with "remap_pfn_range()" which 1482a145dd41SLinus Torvalds * took an arbitrary page protection parameter. This doesn't allow 1483a145dd41SLinus Torvalds * that. Your vma protection will have to be set up correctly, which 1484a145dd41SLinus Torvalds * means that if you want a shared writable mapping, you'd better 1485a145dd41SLinus Torvalds * ask for a shared writable mapping! 1486a145dd41SLinus Torvalds * 1487a145dd41SLinus Torvalds * The page does not need to be reserved. 14884b6e1e37SKonstantin Khlebnikov * 14894b6e1e37SKonstantin Khlebnikov * Usually this function is called from f_op->mmap() handler 14904b6e1e37SKonstantin Khlebnikov * under mm->mmap_sem write-lock, so it can change vma->vm_flags. 14914b6e1e37SKonstantin Khlebnikov * Caller must set VM_MIXEDMAP on vma if it wants to call this 14924b6e1e37SKonstantin Khlebnikov * function from other places, for example from page-fault handler. 1493a862f68aSMike Rapoport * 1494a862f68aSMike Rapoport * Return: %0 on success, negative error code otherwise. 1495a145dd41SLinus Torvalds */ 1496423bad60SNick Piggin int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, 1497423bad60SNick Piggin struct page *page) 1498a145dd41SLinus Torvalds { 1499a145dd41SLinus Torvalds if (addr < vma->vm_start || addr >= vma->vm_end) 1500a145dd41SLinus Torvalds return -EFAULT; 1501a145dd41SLinus Torvalds if (!page_count(page)) 1502a145dd41SLinus Torvalds return -EINVAL; 15034b6e1e37SKonstantin Khlebnikov if (!(vma->vm_flags & VM_MIXEDMAP)) { 15044b6e1e37SKonstantin Khlebnikov BUG_ON(down_read_trylock(&vma->vm_mm->mmap_sem)); 15054b6e1e37SKonstantin Khlebnikov BUG_ON(vma->vm_flags & VM_PFNMAP); 15064b6e1e37SKonstantin Khlebnikov vma->vm_flags |= VM_MIXEDMAP; 15074b6e1e37SKonstantin Khlebnikov } 1508423bad60SNick Piggin return insert_page(vma, addr, page, vma->vm_page_prot); 1509a145dd41SLinus Torvalds } 1510e3c3374fSLinus Torvalds EXPORT_SYMBOL(vm_insert_page); 1511a145dd41SLinus Torvalds 1512a667d745SSouptick Joarder /* 1513a667d745SSouptick Joarder * __vm_map_pages - maps range of kernel pages into user vma 1514a667d745SSouptick Joarder * @vma: user vma to map to 1515a667d745SSouptick Joarder * @pages: pointer to array of source kernel pages 1516a667d745SSouptick Joarder * @num: number of pages in page array 1517a667d745SSouptick Joarder * @offset: user's requested vm_pgoff 1518a667d745SSouptick Joarder * 1519a667d745SSouptick Joarder * This allows drivers to map range of kernel pages into a user vma. 1520a667d745SSouptick Joarder * 1521a667d745SSouptick Joarder * Return: 0 on success and error code otherwise. 1522a667d745SSouptick Joarder */ 1523a667d745SSouptick Joarder static int __vm_map_pages(struct vm_area_struct *vma, struct page **pages, 1524a667d745SSouptick Joarder unsigned long num, unsigned long offset) 1525a667d745SSouptick Joarder { 1526a667d745SSouptick Joarder unsigned long count = vma_pages(vma); 1527a667d745SSouptick Joarder unsigned long uaddr = vma->vm_start; 1528a667d745SSouptick Joarder int ret, i; 1529a667d745SSouptick Joarder 1530a667d745SSouptick Joarder /* Fail if the user requested offset is beyond the end of the object */ 153196756fcbSMiguel Ojeda if (offset >= num) 1532a667d745SSouptick Joarder return -ENXIO; 1533a667d745SSouptick Joarder 1534a667d745SSouptick Joarder /* Fail if the user requested size exceeds available object size */ 1535a667d745SSouptick Joarder if (count > num - offset) 1536a667d745SSouptick Joarder return -ENXIO; 1537a667d745SSouptick Joarder 1538a667d745SSouptick Joarder for (i = 0; i < count; i++) { 1539a667d745SSouptick Joarder ret = vm_insert_page(vma, uaddr, pages[offset + i]); 1540a667d745SSouptick Joarder if (ret < 0) 1541a667d745SSouptick Joarder return ret; 1542a667d745SSouptick Joarder uaddr += PAGE_SIZE; 1543a667d745SSouptick Joarder } 1544a667d745SSouptick Joarder 1545a667d745SSouptick Joarder return 0; 1546a667d745SSouptick Joarder } 1547a667d745SSouptick Joarder 1548a667d745SSouptick Joarder /** 1549a667d745SSouptick Joarder * vm_map_pages - maps range of kernel pages starts with non zero offset 1550a667d745SSouptick Joarder * @vma: user vma to map to 1551a667d745SSouptick Joarder * @pages: pointer to array of source kernel pages 1552a667d745SSouptick Joarder * @num: number of pages in page array 1553a667d745SSouptick Joarder * 1554a667d745SSouptick Joarder * Maps an object consisting of @num pages, catering for the user's 1555a667d745SSouptick Joarder * requested vm_pgoff 1556a667d745SSouptick Joarder * 1557a667d745SSouptick Joarder * If we fail to insert any page into the vma, the function will return 1558a667d745SSouptick Joarder * immediately leaving any previously inserted pages present. Callers 1559a667d745SSouptick Joarder * from the mmap handler may immediately return the error as their caller 1560a667d745SSouptick Joarder * will destroy the vma, removing any successfully inserted pages. Other 1561a667d745SSouptick Joarder * callers should make their own arrangements for calling unmap_region(). 1562a667d745SSouptick Joarder * 1563a667d745SSouptick Joarder * Context: Process context. Called by mmap handlers. 1564a667d745SSouptick Joarder * Return: 0 on success and error code otherwise. 1565a667d745SSouptick Joarder */ 1566a667d745SSouptick Joarder int vm_map_pages(struct vm_area_struct *vma, struct page **pages, 1567a667d745SSouptick Joarder unsigned long num) 1568a667d745SSouptick Joarder { 1569a667d745SSouptick Joarder return __vm_map_pages(vma, pages, num, vma->vm_pgoff); 1570a667d745SSouptick Joarder } 1571a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages); 1572a667d745SSouptick Joarder 1573a667d745SSouptick Joarder /** 1574a667d745SSouptick Joarder * vm_map_pages_zero - map range of kernel pages starts with zero offset 1575a667d745SSouptick Joarder * @vma: user vma to map to 1576a667d745SSouptick Joarder * @pages: pointer to array of source kernel pages 1577a667d745SSouptick Joarder * @num: number of pages in page array 1578a667d745SSouptick Joarder * 1579a667d745SSouptick Joarder * Similar to vm_map_pages(), except that it explicitly sets the offset 1580a667d745SSouptick Joarder * to 0. This function is intended for the drivers that did not consider 1581a667d745SSouptick Joarder * vm_pgoff. 1582a667d745SSouptick Joarder * 1583a667d745SSouptick Joarder * Context: Process context. Called by mmap handlers. 1584a667d745SSouptick Joarder * Return: 0 on success and error code otherwise. 1585a667d745SSouptick Joarder */ 1586a667d745SSouptick Joarder int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages, 1587a667d745SSouptick Joarder unsigned long num) 1588a667d745SSouptick Joarder { 1589a667d745SSouptick Joarder return __vm_map_pages(vma, pages, num, 0); 1590a667d745SSouptick Joarder } 1591a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages_zero); 1592a667d745SSouptick Joarder 15939b5a8e00SMatthew Wilcox static vm_fault_t insert_pfn(struct vm_area_struct *vma, unsigned long addr, 1594b2770da6SRoss Zwisler pfn_t pfn, pgprot_t prot, bool mkwrite) 1595423bad60SNick Piggin { 1596423bad60SNick Piggin struct mm_struct *mm = vma->vm_mm; 1597423bad60SNick Piggin pte_t *pte, entry; 1598423bad60SNick Piggin spinlock_t *ptl; 1599423bad60SNick Piggin 1600423bad60SNick Piggin pte = get_locked_pte(mm, addr, &ptl); 1601423bad60SNick Piggin if (!pte) 16029b5a8e00SMatthew Wilcox return VM_FAULT_OOM; 1603b2770da6SRoss Zwisler if (!pte_none(*pte)) { 1604b2770da6SRoss Zwisler if (mkwrite) { 1605b2770da6SRoss Zwisler /* 1606b2770da6SRoss Zwisler * For read faults on private mappings the PFN passed 1607b2770da6SRoss Zwisler * in may not match the PFN we have mapped if the 1608b2770da6SRoss Zwisler * mapped PFN is a writeable COW page. In the mkwrite 1609b2770da6SRoss Zwisler * case we are creating a writable PTE for a shared 1610f2c57d91SJan Kara * mapping and we expect the PFNs to match. If they 1611f2c57d91SJan Kara * don't match, we are likely racing with block 1612f2c57d91SJan Kara * allocation and mapping invalidation so just skip the 1613f2c57d91SJan Kara * update. 1614b2770da6SRoss Zwisler */ 1615f2c57d91SJan Kara if (pte_pfn(*pte) != pfn_t_to_pfn(pfn)) { 1616f2c57d91SJan Kara WARN_ON_ONCE(!is_zero_pfn(pte_pfn(*pte))); 1617423bad60SNick Piggin goto out_unlock; 1618f2c57d91SJan Kara } 1619cae85cb8SJan Kara entry = pte_mkyoung(*pte); 1620cae85cb8SJan Kara entry = maybe_mkwrite(pte_mkdirty(entry), vma); 1621cae85cb8SJan Kara if (ptep_set_access_flags(vma, addr, pte, entry, 1)) 1622cae85cb8SJan Kara update_mmu_cache(vma, addr, pte); 1623cae85cb8SJan Kara } 1624b2770da6SRoss Zwisler goto out_unlock; 1625b2770da6SRoss Zwisler } 1626423bad60SNick Piggin 1627423bad60SNick Piggin /* Ok, finally just insert the thing.. */ 162801c8f1c4SDan Williams if (pfn_t_devmap(pfn)) 162901c8f1c4SDan Williams entry = pte_mkdevmap(pfn_t_pte(pfn, prot)); 163001c8f1c4SDan Williams else 163101c8f1c4SDan Williams entry = pte_mkspecial(pfn_t_pte(pfn, prot)); 1632b2770da6SRoss Zwisler 1633b2770da6SRoss Zwisler if (mkwrite) { 1634b2770da6SRoss Zwisler entry = pte_mkyoung(entry); 1635b2770da6SRoss Zwisler entry = maybe_mkwrite(pte_mkdirty(entry), vma); 1636b2770da6SRoss Zwisler } 1637b2770da6SRoss Zwisler 1638423bad60SNick Piggin set_pte_at(mm, addr, pte, entry); 16394b3073e1SRussell King update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */ 1640423bad60SNick Piggin 1641423bad60SNick Piggin out_unlock: 1642423bad60SNick Piggin pte_unmap_unlock(pte, ptl); 16439b5a8e00SMatthew Wilcox return VM_FAULT_NOPAGE; 1644423bad60SNick Piggin } 1645423bad60SNick Piggin 1646f5e6d1d5SMatthew Wilcox /** 1647f5e6d1d5SMatthew Wilcox * vmf_insert_pfn_prot - insert single pfn into user vma with specified pgprot 1648f5e6d1d5SMatthew Wilcox * @vma: user vma to map to 1649f5e6d1d5SMatthew Wilcox * @addr: target user address of this page 1650f5e6d1d5SMatthew Wilcox * @pfn: source kernel pfn 1651f5e6d1d5SMatthew Wilcox * @pgprot: pgprot flags for the inserted page 1652f5e6d1d5SMatthew Wilcox * 1653f5e6d1d5SMatthew Wilcox * This is exactly like vmf_insert_pfn(), except that it allows drivers to 1654f5e6d1d5SMatthew Wilcox * to override pgprot on a per-page basis. 1655f5e6d1d5SMatthew Wilcox * 1656f5e6d1d5SMatthew Wilcox * This only makes sense for IO mappings, and it makes no sense for 1657f5e6d1d5SMatthew Wilcox * COW mappings. In general, using multiple vmas is preferable; 1658ae2b01f3SMatthew Wilcox * vmf_insert_pfn_prot should only be used if using multiple VMAs is 1659f5e6d1d5SMatthew Wilcox * impractical. 1660f5e6d1d5SMatthew Wilcox * 1661ae2b01f3SMatthew Wilcox * Context: Process context. May allocate using %GFP_KERNEL. 1662f5e6d1d5SMatthew Wilcox * Return: vm_fault_t value. 1663f5e6d1d5SMatthew Wilcox */ 1664f5e6d1d5SMatthew Wilcox vm_fault_t vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr, 1665f5e6d1d5SMatthew Wilcox unsigned long pfn, pgprot_t pgprot) 1666f5e6d1d5SMatthew Wilcox { 16676d958546SMatthew Wilcox /* 16686d958546SMatthew Wilcox * Technically, architectures with pte_special can avoid all these 16696d958546SMatthew Wilcox * restrictions (same for remap_pfn_range). However we would like 16706d958546SMatthew Wilcox * consistency in testing and feature parity among all, so we should 16716d958546SMatthew Wilcox * try to keep these invariants in place for everybody. 16726d958546SMatthew Wilcox */ 16736d958546SMatthew Wilcox BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))); 16746d958546SMatthew Wilcox BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) == 16756d958546SMatthew Wilcox (VM_PFNMAP|VM_MIXEDMAP)); 16766d958546SMatthew Wilcox BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); 16776d958546SMatthew Wilcox BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn)); 16786d958546SMatthew Wilcox 16796d958546SMatthew Wilcox if (addr < vma->vm_start || addr >= vma->vm_end) 16806d958546SMatthew Wilcox return VM_FAULT_SIGBUS; 16816d958546SMatthew Wilcox 16826d958546SMatthew Wilcox if (!pfn_modify_allowed(pfn, pgprot)) 16836d958546SMatthew Wilcox return VM_FAULT_SIGBUS; 16846d958546SMatthew Wilcox 16856d958546SMatthew Wilcox track_pfn_insert(vma, &pgprot, __pfn_to_pfn_t(pfn, PFN_DEV)); 16866d958546SMatthew Wilcox 16879b5a8e00SMatthew Wilcox return insert_pfn(vma, addr, __pfn_to_pfn_t(pfn, PFN_DEV), pgprot, 16886d958546SMatthew Wilcox false); 1689f5e6d1d5SMatthew Wilcox } 1690f5e6d1d5SMatthew Wilcox EXPORT_SYMBOL(vmf_insert_pfn_prot); 1691e0dc0d8fSNick Piggin 1692ae2b01f3SMatthew Wilcox /** 1693ae2b01f3SMatthew Wilcox * vmf_insert_pfn - insert single pfn into user vma 1694ae2b01f3SMatthew Wilcox * @vma: user vma to map to 1695ae2b01f3SMatthew Wilcox * @addr: target user address of this page 1696ae2b01f3SMatthew Wilcox * @pfn: source kernel pfn 1697ae2b01f3SMatthew Wilcox * 1698ae2b01f3SMatthew Wilcox * Similar to vm_insert_page, this allows drivers to insert individual pages 1699ae2b01f3SMatthew Wilcox * they've allocated into a user vma. Same comments apply. 1700ae2b01f3SMatthew Wilcox * 1701ae2b01f3SMatthew Wilcox * This function should only be called from a vm_ops->fault handler, and 1702ae2b01f3SMatthew Wilcox * in that case the handler should return the result of this function. 1703ae2b01f3SMatthew Wilcox * 1704ae2b01f3SMatthew Wilcox * vma cannot be a COW mapping. 1705ae2b01f3SMatthew Wilcox * 1706ae2b01f3SMatthew Wilcox * As this is called only for pages that do not currently exist, we 1707ae2b01f3SMatthew Wilcox * do not need to flush old virtual caches or the TLB. 1708ae2b01f3SMatthew Wilcox * 1709ae2b01f3SMatthew Wilcox * Context: Process context. May allocate using %GFP_KERNEL. 1710ae2b01f3SMatthew Wilcox * Return: vm_fault_t value. 1711ae2b01f3SMatthew Wilcox */ 1712ae2b01f3SMatthew Wilcox vm_fault_t vmf_insert_pfn(struct vm_area_struct *vma, unsigned long addr, 1713ae2b01f3SMatthew Wilcox unsigned long pfn) 1714ae2b01f3SMatthew Wilcox { 1715ae2b01f3SMatthew Wilcox return vmf_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot); 1716ae2b01f3SMatthew Wilcox } 1717ae2b01f3SMatthew Wilcox EXPORT_SYMBOL(vmf_insert_pfn); 1718ae2b01f3SMatthew Wilcox 1719785a3fabSDan Williams static bool vm_mixed_ok(struct vm_area_struct *vma, pfn_t pfn) 1720785a3fabSDan Williams { 1721785a3fabSDan Williams /* these checks mirror the abort conditions in vm_normal_page */ 1722785a3fabSDan Williams if (vma->vm_flags & VM_MIXEDMAP) 1723785a3fabSDan Williams return true; 1724785a3fabSDan Williams if (pfn_t_devmap(pfn)) 1725785a3fabSDan Williams return true; 1726785a3fabSDan Williams if (pfn_t_special(pfn)) 1727785a3fabSDan Williams return true; 1728785a3fabSDan Williams if (is_zero_pfn(pfn_t_to_pfn(pfn))) 1729785a3fabSDan Williams return true; 1730785a3fabSDan Williams return false; 1731785a3fabSDan Williams } 1732785a3fabSDan Williams 173379f3aa5bSMatthew Wilcox static vm_fault_t __vm_insert_mixed(struct vm_area_struct *vma, 173479f3aa5bSMatthew Wilcox unsigned long addr, pfn_t pfn, bool mkwrite) 1735423bad60SNick Piggin { 173687744ab3SDan Williams pgprot_t pgprot = vma->vm_page_prot; 173779f3aa5bSMatthew Wilcox int err; 173887744ab3SDan Williams 1739785a3fabSDan Williams BUG_ON(!vm_mixed_ok(vma, pfn)); 1740423bad60SNick Piggin 1741423bad60SNick Piggin if (addr < vma->vm_start || addr >= vma->vm_end) 174279f3aa5bSMatthew Wilcox return VM_FAULT_SIGBUS; 1743308a047cSBorislav Petkov 1744308a047cSBorislav Petkov track_pfn_insert(vma, &pgprot, pfn); 1745423bad60SNick Piggin 174642e4089cSAndi Kleen if (!pfn_modify_allowed(pfn_t_to_pfn(pfn), pgprot)) 174779f3aa5bSMatthew Wilcox return VM_FAULT_SIGBUS; 174842e4089cSAndi Kleen 1749423bad60SNick Piggin /* 1750423bad60SNick Piggin * If we don't have pte special, then we have to use the pfn_valid() 1751423bad60SNick Piggin * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must* 1752423bad60SNick Piggin * refcount the page if pfn_valid is true (hence insert_page rather 175362eede62SHugh Dickins * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP 175462eede62SHugh Dickins * without pte special, it would there be refcounted as a normal page. 1755423bad60SNick Piggin */ 175600b3a331SLaurent Dufour if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) && 175700b3a331SLaurent Dufour !pfn_t_devmap(pfn) && pfn_t_valid(pfn)) { 1758423bad60SNick Piggin struct page *page; 1759423bad60SNick Piggin 176003fc2da6SDan Williams /* 176103fc2da6SDan Williams * At this point we are committed to insert_page() 176203fc2da6SDan Williams * regardless of whether the caller specified flags that 176303fc2da6SDan Williams * result in pfn_t_has_page() == false. 176403fc2da6SDan Williams */ 176503fc2da6SDan Williams page = pfn_to_page(pfn_t_to_pfn(pfn)); 176679f3aa5bSMatthew Wilcox err = insert_page(vma, addr, page, pgprot); 176779f3aa5bSMatthew Wilcox } else { 17689b5a8e00SMatthew Wilcox return insert_pfn(vma, addr, pfn, pgprot, mkwrite); 1769423bad60SNick Piggin } 1770b2770da6SRoss Zwisler 17715d747637SMatthew Wilcox if (err == -ENOMEM) 17725d747637SMatthew Wilcox return VM_FAULT_OOM; 17735d747637SMatthew Wilcox if (err < 0 && err != -EBUSY) 17745d747637SMatthew Wilcox return VM_FAULT_SIGBUS; 17755d747637SMatthew Wilcox 17765d747637SMatthew Wilcox return VM_FAULT_NOPAGE; 1777423bad60SNick Piggin } 177879f3aa5bSMatthew Wilcox 177979f3aa5bSMatthew Wilcox vm_fault_t vmf_insert_mixed(struct vm_area_struct *vma, unsigned long addr, 178079f3aa5bSMatthew Wilcox pfn_t pfn) 178179f3aa5bSMatthew Wilcox { 178279f3aa5bSMatthew Wilcox return __vm_insert_mixed(vma, addr, pfn, false); 178379f3aa5bSMatthew Wilcox } 17845d747637SMatthew Wilcox EXPORT_SYMBOL(vmf_insert_mixed); 1785423bad60SNick Piggin 1786ab77dab4SSouptick Joarder /* 1787ab77dab4SSouptick Joarder * If the insertion of PTE failed because someone else already added a 1788ab77dab4SSouptick Joarder * different entry in the mean time, we treat that as success as we assume 1789ab77dab4SSouptick Joarder * the same entry was actually inserted. 1790ab77dab4SSouptick Joarder */ 1791ab77dab4SSouptick Joarder vm_fault_t vmf_insert_mixed_mkwrite(struct vm_area_struct *vma, 1792ab77dab4SSouptick Joarder unsigned long addr, pfn_t pfn) 1793b2770da6SRoss Zwisler { 179479f3aa5bSMatthew Wilcox return __vm_insert_mixed(vma, addr, pfn, true); 1795b2770da6SRoss Zwisler } 1796ab77dab4SSouptick Joarder EXPORT_SYMBOL(vmf_insert_mixed_mkwrite); 1797b2770da6SRoss Zwisler 1798a145dd41SLinus Torvalds /* 17991da177e4SLinus Torvalds * maps a range of physical memory into the requested pages. the old 18001da177e4SLinus Torvalds * mappings are removed. any references to nonexistent pages results 18011da177e4SLinus Torvalds * in null mappings (currently treated as "copy-on-access") 18021da177e4SLinus Torvalds */ 18031da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd, 18041da177e4SLinus Torvalds unsigned long addr, unsigned long end, 18051da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 18061da177e4SLinus Torvalds { 18071da177e4SLinus Torvalds pte_t *pte; 1808c74df32cSHugh Dickins spinlock_t *ptl; 180942e4089cSAndi Kleen int err = 0; 18101da177e4SLinus Torvalds 1811c74df32cSHugh Dickins pte = pte_alloc_map_lock(mm, pmd, addr, &ptl); 18121da177e4SLinus Torvalds if (!pte) 18131da177e4SLinus Torvalds return -ENOMEM; 18146606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 18151da177e4SLinus Torvalds do { 18161da177e4SLinus Torvalds BUG_ON(!pte_none(*pte)); 181742e4089cSAndi Kleen if (!pfn_modify_allowed(pfn, prot)) { 181842e4089cSAndi Kleen err = -EACCES; 181942e4089cSAndi Kleen break; 182042e4089cSAndi Kleen } 18217e675137SNick Piggin set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot))); 18221da177e4SLinus Torvalds pfn++; 18231da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 18246606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 1825c74df32cSHugh Dickins pte_unmap_unlock(pte - 1, ptl); 182642e4089cSAndi Kleen return err; 18271da177e4SLinus Torvalds } 18281da177e4SLinus Torvalds 18291da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud, 18301da177e4SLinus Torvalds unsigned long addr, unsigned long end, 18311da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 18321da177e4SLinus Torvalds { 18331da177e4SLinus Torvalds pmd_t *pmd; 18341da177e4SLinus Torvalds unsigned long next; 183542e4089cSAndi Kleen int err; 18361da177e4SLinus Torvalds 18371da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 18381da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, addr); 18391da177e4SLinus Torvalds if (!pmd) 18401da177e4SLinus Torvalds return -ENOMEM; 1841f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 18421da177e4SLinus Torvalds do { 18431da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 184442e4089cSAndi Kleen err = remap_pte_range(mm, pmd, addr, next, 184542e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 184642e4089cSAndi Kleen if (err) 184742e4089cSAndi Kleen return err; 18481da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 18491da177e4SLinus Torvalds return 0; 18501da177e4SLinus Torvalds } 18511da177e4SLinus Torvalds 1852c2febafcSKirill A. Shutemov static inline int remap_pud_range(struct mm_struct *mm, p4d_t *p4d, 18531da177e4SLinus Torvalds unsigned long addr, unsigned long end, 18541da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 18551da177e4SLinus Torvalds { 18561da177e4SLinus Torvalds pud_t *pud; 18571da177e4SLinus Torvalds unsigned long next; 185842e4089cSAndi Kleen int err; 18591da177e4SLinus Torvalds 18601da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 1861c2febafcSKirill A. Shutemov pud = pud_alloc(mm, p4d, addr); 18621da177e4SLinus Torvalds if (!pud) 18631da177e4SLinus Torvalds return -ENOMEM; 18641da177e4SLinus Torvalds do { 18651da177e4SLinus Torvalds next = pud_addr_end(addr, end); 186642e4089cSAndi Kleen err = remap_pmd_range(mm, pud, addr, next, 186742e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 186842e4089cSAndi Kleen if (err) 186942e4089cSAndi Kleen return err; 18701da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 18711da177e4SLinus Torvalds return 0; 18721da177e4SLinus Torvalds } 18731da177e4SLinus Torvalds 1874c2febafcSKirill A. Shutemov static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd, 1875c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 1876c2febafcSKirill A. Shutemov unsigned long pfn, pgprot_t prot) 1877c2febafcSKirill A. Shutemov { 1878c2febafcSKirill A. Shutemov p4d_t *p4d; 1879c2febafcSKirill A. Shutemov unsigned long next; 188042e4089cSAndi Kleen int err; 1881c2febafcSKirill A. Shutemov 1882c2febafcSKirill A. Shutemov pfn -= addr >> PAGE_SHIFT; 1883c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, addr); 1884c2febafcSKirill A. Shutemov if (!p4d) 1885c2febafcSKirill A. Shutemov return -ENOMEM; 1886c2febafcSKirill A. Shutemov do { 1887c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 188842e4089cSAndi Kleen err = remap_pud_range(mm, p4d, addr, next, 188942e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 189042e4089cSAndi Kleen if (err) 189142e4089cSAndi Kleen return err; 1892c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 1893c2febafcSKirill A. Shutemov return 0; 1894c2febafcSKirill A. Shutemov } 1895c2febafcSKirill A. Shutemov 1896bfa5bf6dSRolf Eike Beer /** 1897bfa5bf6dSRolf Eike Beer * remap_pfn_range - remap kernel memory to userspace 1898bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 1899bfa5bf6dSRolf Eike Beer * @addr: target user address to start at 1900bfa5bf6dSRolf Eike Beer * @pfn: physical address of kernel memory 1901bfa5bf6dSRolf Eike Beer * @size: size of map area 1902bfa5bf6dSRolf Eike Beer * @prot: page protection flags for this mapping 1903bfa5bf6dSRolf Eike Beer * 1904bfa5bf6dSRolf Eike Beer * Note: this is only safe if the mm semaphore is held when called. 1905a862f68aSMike Rapoport * 1906a862f68aSMike Rapoport * Return: %0 on success, negative error code otherwise. 1907bfa5bf6dSRolf Eike Beer */ 19081da177e4SLinus Torvalds int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 19091da177e4SLinus Torvalds unsigned long pfn, unsigned long size, pgprot_t prot) 19101da177e4SLinus Torvalds { 19111da177e4SLinus Torvalds pgd_t *pgd; 19121da177e4SLinus Torvalds unsigned long next; 19132d15cab8SHugh Dickins unsigned long end = addr + PAGE_ALIGN(size); 19141da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 1915d5957d2fSYongji Xie unsigned long remap_pfn = pfn; 19161da177e4SLinus Torvalds int err; 19171da177e4SLinus Torvalds 19181da177e4SLinus Torvalds /* 19191da177e4SLinus Torvalds * Physically remapped pages are special. Tell the 19201da177e4SLinus Torvalds * rest of the world about it: 19211da177e4SLinus Torvalds * VM_IO tells people not to look at these pages 19221da177e4SLinus Torvalds * (accesses can have side effects). 19236aab341eSLinus Torvalds * VM_PFNMAP tells the core MM that the base pages are just 19246aab341eSLinus Torvalds * raw PFN mappings, and do not have a "struct page" associated 19256aab341eSLinus Torvalds * with them. 1926314e51b9SKonstantin Khlebnikov * VM_DONTEXPAND 1927314e51b9SKonstantin Khlebnikov * Disable vma merging and expanding with mremap(). 1928314e51b9SKonstantin Khlebnikov * VM_DONTDUMP 1929314e51b9SKonstantin Khlebnikov * Omit vma from core dump, even when VM_IO turned off. 1930fb155c16SLinus Torvalds * 1931fb155c16SLinus Torvalds * There's a horrible special case to handle copy-on-write 1932fb155c16SLinus Torvalds * behaviour that some programs depend on. We mark the "original" 1933fb155c16SLinus Torvalds * un-COW'ed pages by matching them up with "vma->vm_pgoff". 1934b3b9c293SKonstantin Khlebnikov * See vm_normal_page() for details. 19351da177e4SLinus Torvalds */ 1936b3b9c293SKonstantin Khlebnikov if (is_cow_mapping(vma->vm_flags)) { 1937b3b9c293SKonstantin Khlebnikov if (addr != vma->vm_start || end != vma->vm_end) 1938b3b9c293SKonstantin Khlebnikov return -EINVAL; 19396aab341eSLinus Torvalds vma->vm_pgoff = pfn; 1940b3b9c293SKonstantin Khlebnikov } 1941b3b9c293SKonstantin Khlebnikov 1942d5957d2fSYongji Xie err = track_pfn_remap(vma, &prot, remap_pfn, addr, PAGE_ALIGN(size)); 1943b3b9c293SKonstantin Khlebnikov if (err) 19443c8bb73aSvenkatesh.pallipadi@intel.com return -EINVAL; 1945fb155c16SLinus Torvalds 1946314e51b9SKonstantin Khlebnikov vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; 19471da177e4SLinus Torvalds 19481da177e4SLinus Torvalds BUG_ON(addr >= end); 19491da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 19501da177e4SLinus Torvalds pgd = pgd_offset(mm, addr); 19511da177e4SLinus Torvalds flush_cache_range(vma, addr, end); 19521da177e4SLinus Torvalds do { 19531da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 1954c2febafcSKirill A. Shutemov err = remap_p4d_range(mm, pgd, addr, next, 19551da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot); 19561da177e4SLinus Torvalds if (err) 19571da177e4SLinus Torvalds break; 19581da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 19592ab64037Svenkatesh.pallipadi@intel.com 19602ab64037Svenkatesh.pallipadi@intel.com if (err) 1961d5957d2fSYongji Xie untrack_pfn(vma, remap_pfn, PAGE_ALIGN(size)); 19622ab64037Svenkatesh.pallipadi@intel.com 19631da177e4SLinus Torvalds return err; 19641da177e4SLinus Torvalds } 19651da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range); 19661da177e4SLinus Torvalds 1967b4cbb197SLinus Torvalds /** 1968b4cbb197SLinus Torvalds * vm_iomap_memory - remap memory to userspace 1969b4cbb197SLinus Torvalds * @vma: user vma to map to 1970b4cbb197SLinus Torvalds * @start: start of area 1971b4cbb197SLinus Torvalds * @len: size of area 1972b4cbb197SLinus Torvalds * 1973b4cbb197SLinus Torvalds * This is a simplified io_remap_pfn_range() for common driver use. The 1974b4cbb197SLinus Torvalds * driver just needs to give us the physical memory range to be mapped, 1975b4cbb197SLinus Torvalds * we'll figure out the rest from the vma information. 1976b4cbb197SLinus Torvalds * 1977b4cbb197SLinus Torvalds * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get 1978b4cbb197SLinus Torvalds * whatever write-combining details or similar. 1979a862f68aSMike Rapoport * 1980a862f68aSMike Rapoport * Return: %0 on success, negative error code otherwise. 1981b4cbb197SLinus Torvalds */ 1982b4cbb197SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len) 1983b4cbb197SLinus Torvalds { 1984b4cbb197SLinus Torvalds unsigned long vm_len, pfn, pages; 1985b4cbb197SLinus Torvalds 1986b4cbb197SLinus Torvalds /* Check that the physical memory area passed in looks valid */ 1987b4cbb197SLinus Torvalds if (start + len < start) 1988b4cbb197SLinus Torvalds return -EINVAL; 1989b4cbb197SLinus Torvalds /* 1990b4cbb197SLinus Torvalds * You *really* shouldn't map things that aren't page-aligned, 1991b4cbb197SLinus Torvalds * but we've historically allowed it because IO memory might 1992b4cbb197SLinus Torvalds * just have smaller alignment. 1993b4cbb197SLinus Torvalds */ 1994b4cbb197SLinus Torvalds len += start & ~PAGE_MASK; 1995b4cbb197SLinus Torvalds pfn = start >> PAGE_SHIFT; 1996b4cbb197SLinus Torvalds pages = (len + ~PAGE_MASK) >> PAGE_SHIFT; 1997b4cbb197SLinus Torvalds if (pfn + pages < pfn) 1998b4cbb197SLinus Torvalds return -EINVAL; 1999b4cbb197SLinus Torvalds 2000b4cbb197SLinus Torvalds /* We start the mapping 'vm_pgoff' pages into the area */ 2001b4cbb197SLinus Torvalds if (vma->vm_pgoff > pages) 2002b4cbb197SLinus Torvalds return -EINVAL; 2003b4cbb197SLinus Torvalds pfn += vma->vm_pgoff; 2004b4cbb197SLinus Torvalds pages -= vma->vm_pgoff; 2005b4cbb197SLinus Torvalds 2006b4cbb197SLinus Torvalds /* Can we fit all of the mapping? */ 2007b4cbb197SLinus Torvalds vm_len = vma->vm_end - vma->vm_start; 2008b4cbb197SLinus Torvalds if (vm_len >> PAGE_SHIFT > pages) 2009b4cbb197SLinus Torvalds return -EINVAL; 2010b4cbb197SLinus Torvalds 2011b4cbb197SLinus Torvalds /* Ok, let it rip */ 2012b4cbb197SLinus Torvalds return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot); 2013b4cbb197SLinus Torvalds } 2014b4cbb197SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory); 2015b4cbb197SLinus Torvalds 2016aee16b3cSJeremy Fitzhardinge static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd, 2017aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2018aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 2019aee16b3cSJeremy Fitzhardinge { 2020aee16b3cSJeremy Fitzhardinge pte_t *pte; 2021aee16b3cSJeremy Fitzhardinge int err; 202294909914SBorislav Petkov spinlock_t *uninitialized_var(ptl); 2023aee16b3cSJeremy Fitzhardinge 2024aee16b3cSJeremy Fitzhardinge pte = (mm == &init_mm) ? 2025aee16b3cSJeremy Fitzhardinge pte_alloc_kernel(pmd, addr) : 2026aee16b3cSJeremy Fitzhardinge pte_alloc_map_lock(mm, pmd, addr, &ptl); 2027aee16b3cSJeremy Fitzhardinge if (!pte) 2028aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2029aee16b3cSJeremy Fitzhardinge 2030aee16b3cSJeremy Fitzhardinge BUG_ON(pmd_huge(*pmd)); 2031aee16b3cSJeremy Fitzhardinge 203238e0edb1SJeremy Fitzhardinge arch_enter_lazy_mmu_mode(); 203338e0edb1SJeremy Fitzhardinge 2034aee16b3cSJeremy Fitzhardinge do { 20358b1e0f81SAnshuman Khandual err = fn(pte++, addr, data); 2036aee16b3cSJeremy Fitzhardinge if (err) 2037aee16b3cSJeremy Fitzhardinge break; 2038c36987e2SDaisuke Nishimura } while (addr += PAGE_SIZE, addr != end); 2039aee16b3cSJeremy Fitzhardinge 204038e0edb1SJeremy Fitzhardinge arch_leave_lazy_mmu_mode(); 204138e0edb1SJeremy Fitzhardinge 2042aee16b3cSJeremy Fitzhardinge if (mm != &init_mm) 2043aee16b3cSJeremy Fitzhardinge pte_unmap_unlock(pte-1, ptl); 2044aee16b3cSJeremy Fitzhardinge return err; 2045aee16b3cSJeremy Fitzhardinge } 2046aee16b3cSJeremy Fitzhardinge 2047aee16b3cSJeremy Fitzhardinge static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud, 2048aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2049aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 2050aee16b3cSJeremy Fitzhardinge { 2051aee16b3cSJeremy Fitzhardinge pmd_t *pmd; 2052aee16b3cSJeremy Fitzhardinge unsigned long next; 2053aee16b3cSJeremy Fitzhardinge int err; 2054aee16b3cSJeremy Fitzhardinge 2055ceb86879SAndi Kleen BUG_ON(pud_huge(*pud)); 2056ceb86879SAndi Kleen 2057aee16b3cSJeremy Fitzhardinge pmd = pmd_alloc(mm, pud, addr); 2058aee16b3cSJeremy Fitzhardinge if (!pmd) 2059aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2060aee16b3cSJeremy Fitzhardinge do { 2061aee16b3cSJeremy Fitzhardinge next = pmd_addr_end(addr, end); 2062aee16b3cSJeremy Fitzhardinge err = apply_to_pte_range(mm, pmd, addr, next, fn, data); 2063aee16b3cSJeremy Fitzhardinge if (err) 2064aee16b3cSJeremy Fitzhardinge break; 2065aee16b3cSJeremy Fitzhardinge } while (pmd++, addr = next, addr != end); 2066aee16b3cSJeremy Fitzhardinge return err; 2067aee16b3cSJeremy Fitzhardinge } 2068aee16b3cSJeremy Fitzhardinge 2069c2febafcSKirill A. Shutemov static int apply_to_pud_range(struct mm_struct *mm, p4d_t *p4d, 2070aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2071aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 2072aee16b3cSJeremy Fitzhardinge { 2073aee16b3cSJeremy Fitzhardinge pud_t *pud; 2074aee16b3cSJeremy Fitzhardinge unsigned long next; 2075aee16b3cSJeremy Fitzhardinge int err; 2076aee16b3cSJeremy Fitzhardinge 2077c2febafcSKirill A. Shutemov pud = pud_alloc(mm, p4d, addr); 2078aee16b3cSJeremy Fitzhardinge if (!pud) 2079aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2080aee16b3cSJeremy Fitzhardinge do { 2081aee16b3cSJeremy Fitzhardinge next = pud_addr_end(addr, end); 2082aee16b3cSJeremy Fitzhardinge err = apply_to_pmd_range(mm, pud, addr, next, fn, data); 2083aee16b3cSJeremy Fitzhardinge if (err) 2084aee16b3cSJeremy Fitzhardinge break; 2085aee16b3cSJeremy Fitzhardinge } while (pud++, addr = next, addr != end); 2086aee16b3cSJeremy Fitzhardinge return err; 2087aee16b3cSJeremy Fitzhardinge } 2088aee16b3cSJeremy Fitzhardinge 2089c2febafcSKirill A. Shutemov static int apply_to_p4d_range(struct mm_struct *mm, pgd_t *pgd, 2090c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 2091c2febafcSKirill A. Shutemov pte_fn_t fn, void *data) 2092c2febafcSKirill A. Shutemov { 2093c2febafcSKirill A. Shutemov p4d_t *p4d; 2094c2febafcSKirill A. Shutemov unsigned long next; 2095c2febafcSKirill A. Shutemov int err; 2096c2febafcSKirill A. Shutemov 2097c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, addr); 2098c2febafcSKirill A. Shutemov if (!p4d) 2099c2febafcSKirill A. Shutemov return -ENOMEM; 2100c2febafcSKirill A. Shutemov do { 2101c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 2102c2febafcSKirill A. Shutemov err = apply_to_pud_range(mm, p4d, addr, next, fn, data); 2103c2febafcSKirill A. Shutemov if (err) 2104c2febafcSKirill A. Shutemov break; 2105c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 2106c2febafcSKirill A. Shutemov return err; 2107c2febafcSKirill A. Shutemov } 2108c2febafcSKirill A. Shutemov 2109aee16b3cSJeremy Fitzhardinge /* 2110aee16b3cSJeremy Fitzhardinge * Scan a region of virtual memory, filling in page tables as necessary 2111aee16b3cSJeremy Fitzhardinge * and calling a provided function on each leaf page table. 2112aee16b3cSJeremy Fitzhardinge */ 2113aee16b3cSJeremy Fitzhardinge int apply_to_page_range(struct mm_struct *mm, unsigned long addr, 2114aee16b3cSJeremy Fitzhardinge unsigned long size, pte_fn_t fn, void *data) 2115aee16b3cSJeremy Fitzhardinge { 2116aee16b3cSJeremy Fitzhardinge pgd_t *pgd; 2117aee16b3cSJeremy Fitzhardinge unsigned long next; 211857250a5bSJeremy Fitzhardinge unsigned long end = addr + size; 2119aee16b3cSJeremy Fitzhardinge int err; 2120aee16b3cSJeremy Fitzhardinge 21219cb65bc3SMika Penttilä if (WARN_ON(addr >= end)) 21229cb65bc3SMika Penttilä return -EINVAL; 21239cb65bc3SMika Penttilä 2124aee16b3cSJeremy Fitzhardinge pgd = pgd_offset(mm, addr); 2125aee16b3cSJeremy Fitzhardinge do { 2126aee16b3cSJeremy Fitzhardinge next = pgd_addr_end(addr, end); 2127c2febafcSKirill A. Shutemov err = apply_to_p4d_range(mm, pgd, addr, next, fn, data); 2128aee16b3cSJeremy Fitzhardinge if (err) 2129aee16b3cSJeremy Fitzhardinge break; 2130aee16b3cSJeremy Fitzhardinge } while (pgd++, addr = next, addr != end); 213157250a5bSJeremy Fitzhardinge 2132aee16b3cSJeremy Fitzhardinge return err; 2133aee16b3cSJeremy Fitzhardinge } 2134aee16b3cSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(apply_to_page_range); 2135aee16b3cSJeremy Fitzhardinge 21361da177e4SLinus Torvalds /* 21379b4bdd2fSKirill A. Shutemov * handle_pte_fault chooses page fault handler according to an entry which was 21389b4bdd2fSKirill A. Shutemov * read non-atomically. Before making any commitment, on those architectures 21399b4bdd2fSKirill A. Shutemov * or configurations (e.g. i386 with PAE) which might give a mix of unmatched 21409b4bdd2fSKirill A. Shutemov * parts, do_swap_page must check under lock before unmapping the pte and 21419b4bdd2fSKirill A. Shutemov * proceeding (but do_wp_page is only called after already making such a check; 2142a335b2e1SRyota Ozaki * and do_anonymous_page can safely check later on). 21438f4e2101SHugh Dickins */ 21444c21e2f2SHugh Dickins static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd, 21458f4e2101SHugh Dickins pte_t *page_table, pte_t orig_pte) 21468f4e2101SHugh Dickins { 21478f4e2101SHugh Dickins int same = 1; 21488f4e2101SHugh Dickins #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT) 21498f4e2101SHugh Dickins if (sizeof(pte_t) > sizeof(unsigned long)) { 21504c21e2f2SHugh Dickins spinlock_t *ptl = pte_lockptr(mm, pmd); 21514c21e2f2SHugh Dickins spin_lock(ptl); 21528f4e2101SHugh Dickins same = pte_same(*page_table, orig_pte); 21534c21e2f2SHugh Dickins spin_unlock(ptl); 21548f4e2101SHugh Dickins } 21558f4e2101SHugh Dickins #endif 21568f4e2101SHugh Dickins pte_unmap(page_table); 21578f4e2101SHugh Dickins return same; 21588f4e2101SHugh Dickins } 21598f4e2101SHugh Dickins 2160*83d116c5SJia He static inline bool cow_user_page(struct page *dst, struct page *src, 2161*83d116c5SJia He struct vm_fault *vmf) 21626aab341eSLinus Torvalds { 2163*83d116c5SJia He bool ret; 2164*83d116c5SJia He void *kaddr; 2165*83d116c5SJia He void __user *uaddr; 2166*83d116c5SJia He bool force_mkyoung; 2167*83d116c5SJia He struct vm_area_struct *vma = vmf->vma; 2168*83d116c5SJia He struct mm_struct *mm = vma->vm_mm; 2169*83d116c5SJia He unsigned long addr = vmf->address; 2170*83d116c5SJia He 21710abdd7a8SDan Williams debug_dma_assert_idle(src); 21720abdd7a8SDan Williams 2173*83d116c5SJia He if (likely(src)) { 2174*83d116c5SJia He copy_user_highpage(dst, src, addr, vma); 2175*83d116c5SJia He return true; 2176*83d116c5SJia He } 2177*83d116c5SJia He 21786aab341eSLinus Torvalds /* 21796aab341eSLinus Torvalds * If the source page was a PFN mapping, we don't have 21806aab341eSLinus Torvalds * a "struct page" for it. We do a best-effort copy by 21816aab341eSLinus Torvalds * just copying from the original user address. If that 21826aab341eSLinus Torvalds * fails, we just zero-fill it. Live with it. 21836aab341eSLinus Torvalds */ 2184*83d116c5SJia He kaddr = kmap_atomic(dst); 2185*83d116c5SJia He uaddr = (void __user *)(addr & PAGE_MASK); 2186*83d116c5SJia He 2187*83d116c5SJia He /* 2188*83d116c5SJia He * On architectures with software "accessed" bits, we would 2189*83d116c5SJia He * take a double page fault, so mark it accessed here. 2190*83d116c5SJia He */ 2191*83d116c5SJia He force_mkyoung = arch_faults_on_old_pte() && !pte_young(vmf->orig_pte); 2192*83d116c5SJia He if (force_mkyoung) { 2193*83d116c5SJia He pte_t entry; 2194*83d116c5SJia He 2195*83d116c5SJia He vmf->pte = pte_offset_map_lock(mm, vmf->pmd, addr, &vmf->ptl); 2196*83d116c5SJia He if (!likely(pte_same(*vmf->pte, vmf->orig_pte))) { 2197*83d116c5SJia He /* 2198*83d116c5SJia He * Other thread has already handled the fault 2199*83d116c5SJia He * and we don't need to do anything. If it's 2200*83d116c5SJia He * not the case, the fault will be triggered 2201*83d116c5SJia He * again on the same address. 2202*83d116c5SJia He */ 2203*83d116c5SJia He ret = false; 2204*83d116c5SJia He goto pte_unlock; 2205*83d116c5SJia He } 2206*83d116c5SJia He 2207*83d116c5SJia He entry = pte_mkyoung(vmf->orig_pte); 2208*83d116c5SJia He if (ptep_set_access_flags(vma, addr, vmf->pte, entry, 0)) 2209*83d116c5SJia He update_mmu_cache(vma, addr, vmf->pte); 2210*83d116c5SJia He } 22115d2a2dbbSLinus Torvalds 22125d2a2dbbSLinus Torvalds /* 22135d2a2dbbSLinus Torvalds * This really shouldn't fail, because the page is there 22145d2a2dbbSLinus Torvalds * in the page tables. But it might just be unreadable, 22155d2a2dbbSLinus Torvalds * in which case we just give up and fill the result with 22165d2a2dbbSLinus Torvalds * zeroes. 22175d2a2dbbSLinus Torvalds */ 2218*83d116c5SJia He if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) { 2219*83d116c5SJia He /* 2220*83d116c5SJia He * Give a warn in case there can be some obscure 2221*83d116c5SJia He * use-case 2222*83d116c5SJia He */ 2223*83d116c5SJia He WARN_ON_ONCE(1); 22243ecb01dfSJan Beulich clear_page(kaddr); 2225*83d116c5SJia He } 2226*83d116c5SJia He 2227*83d116c5SJia He ret = true; 2228*83d116c5SJia He 2229*83d116c5SJia He pte_unlock: 2230*83d116c5SJia He if (force_mkyoung) 2231*83d116c5SJia He pte_unmap_unlock(vmf->pte, vmf->ptl); 22329b04c5feSCong Wang kunmap_atomic(kaddr); 2233c4ec7b0dSDmitriy Monakhov flush_dcache_page(dst); 2234*83d116c5SJia He 2235*83d116c5SJia He return ret; 22366aab341eSLinus Torvalds } 22376aab341eSLinus Torvalds 2238c20cd45eSMichal Hocko static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma) 2239c20cd45eSMichal Hocko { 2240c20cd45eSMichal Hocko struct file *vm_file = vma->vm_file; 2241c20cd45eSMichal Hocko 2242c20cd45eSMichal Hocko if (vm_file) 2243c20cd45eSMichal Hocko return mapping_gfp_mask(vm_file->f_mapping) | __GFP_FS | __GFP_IO; 2244c20cd45eSMichal Hocko 2245c20cd45eSMichal Hocko /* 2246c20cd45eSMichal Hocko * Special mappings (e.g. VDSO) do not have any file so fake 2247c20cd45eSMichal Hocko * a default GFP_KERNEL for them. 2248c20cd45eSMichal Hocko */ 2249c20cd45eSMichal Hocko return GFP_KERNEL; 2250c20cd45eSMichal Hocko } 2251c20cd45eSMichal Hocko 22521da177e4SLinus Torvalds /* 2253fb09a464SKirill A. Shutemov * Notify the address space that the page is about to become writable so that 2254fb09a464SKirill A. Shutemov * it can prohibit this or wait for the page to get into an appropriate state. 2255fb09a464SKirill A. Shutemov * 2256fb09a464SKirill A. Shutemov * We do this without the lock held, so that it can sleep if it needs to. 2257fb09a464SKirill A. Shutemov */ 22582b740303SSouptick Joarder static vm_fault_t do_page_mkwrite(struct vm_fault *vmf) 2259fb09a464SKirill A. Shutemov { 22602b740303SSouptick Joarder vm_fault_t ret; 226138b8cb7fSJan Kara struct page *page = vmf->page; 226238b8cb7fSJan Kara unsigned int old_flags = vmf->flags; 2263fb09a464SKirill A. Shutemov 226438b8cb7fSJan Kara vmf->flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE; 2265fb09a464SKirill A. Shutemov 2266dc617f29SDarrick J. Wong if (vmf->vma->vm_file && 2267dc617f29SDarrick J. Wong IS_SWAPFILE(vmf->vma->vm_file->f_mapping->host)) 2268dc617f29SDarrick J. Wong return VM_FAULT_SIGBUS; 2269dc617f29SDarrick J. Wong 227011bac800SDave Jiang ret = vmf->vma->vm_ops->page_mkwrite(vmf); 227138b8cb7fSJan Kara /* Restore original flags so that caller is not surprised */ 227238b8cb7fSJan Kara vmf->flags = old_flags; 2273fb09a464SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) 2274fb09a464SKirill A. Shutemov return ret; 2275fb09a464SKirill A. Shutemov if (unlikely(!(ret & VM_FAULT_LOCKED))) { 2276fb09a464SKirill A. Shutemov lock_page(page); 2277fb09a464SKirill A. Shutemov if (!page->mapping) { 2278fb09a464SKirill A. Shutemov unlock_page(page); 2279fb09a464SKirill A. Shutemov return 0; /* retry */ 2280fb09a464SKirill A. Shutemov } 2281fb09a464SKirill A. Shutemov ret |= VM_FAULT_LOCKED; 2282fb09a464SKirill A. Shutemov } else 2283fb09a464SKirill A. Shutemov VM_BUG_ON_PAGE(!PageLocked(page), page); 2284fb09a464SKirill A. Shutemov return ret; 2285fb09a464SKirill A. Shutemov } 2286fb09a464SKirill A. Shutemov 2287fb09a464SKirill A. Shutemov /* 228897ba0c2bSJan Kara * Handle dirtying of a page in shared file mapping on a write fault. 22894e047f89SShachar Raindel * 229097ba0c2bSJan Kara * The function expects the page to be locked and unlocks it. 22914e047f89SShachar Raindel */ 229297ba0c2bSJan Kara static void fault_dirty_shared_page(struct vm_area_struct *vma, 229397ba0c2bSJan Kara struct page *page) 22944e047f89SShachar Raindel { 22954e047f89SShachar Raindel struct address_space *mapping; 229697ba0c2bSJan Kara bool dirtied; 229797ba0c2bSJan Kara bool page_mkwrite = vma->vm_ops && vma->vm_ops->page_mkwrite; 22984e047f89SShachar Raindel 22994e047f89SShachar Raindel dirtied = set_page_dirty(page); 23004e047f89SShachar Raindel VM_BUG_ON_PAGE(PageAnon(page), page); 230197ba0c2bSJan Kara /* 230297ba0c2bSJan Kara * Take a local copy of the address_space - page.mapping may be zeroed 230397ba0c2bSJan Kara * by truncate after unlock_page(). The address_space itself remains 230497ba0c2bSJan Kara * pinned by vma->vm_file's reference. We rely on unlock_page()'s 230597ba0c2bSJan Kara * release semantics to prevent the compiler from undoing this copying. 230697ba0c2bSJan Kara */ 230797ba0c2bSJan Kara mapping = page_rmapping(page); 23084e047f89SShachar Raindel unlock_page(page); 23094e047f89SShachar Raindel 23104e047f89SShachar Raindel if ((dirtied || page_mkwrite) && mapping) { 23114e047f89SShachar Raindel /* 23124e047f89SShachar Raindel * Some device drivers do not set page.mapping 23134e047f89SShachar Raindel * but still dirty their pages 23144e047f89SShachar Raindel */ 23154e047f89SShachar Raindel balance_dirty_pages_ratelimited(mapping); 23164e047f89SShachar Raindel } 23174e047f89SShachar Raindel 23184e047f89SShachar Raindel if (!page_mkwrite) 23194e047f89SShachar Raindel file_update_time(vma->vm_file); 23204e047f89SShachar Raindel } 23214e047f89SShachar Raindel 232297ba0c2bSJan Kara /* 23234e047f89SShachar Raindel * Handle write page faults for pages that can be reused in the current vma 23244e047f89SShachar Raindel * 23254e047f89SShachar Raindel * This can happen either due to the mapping being with the VM_SHARED flag, 23264e047f89SShachar Raindel * or due to us being the last reference standing to the page. In either 23274e047f89SShachar Raindel * case, all we need to do here is to mark the page as writable and update 23284e047f89SShachar Raindel * any related book-keeping. 23294e047f89SShachar Raindel */ 2330997dd98dSJan Kara static inline void wp_page_reuse(struct vm_fault *vmf) 233182b0f8c3SJan Kara __releases(vmf->ptl) 23324e047f89SShachar Raindel { 233382b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 2334a41b70d6SJan Kara struct page *page = vmf->page; 23354e047f89SShachar Raindel pte_t entry; 23364e047f89SShachar Raindel /* 23374e047f89SShachar Raindel * Clear the pages cpupid information as the existing 23384e047f89SShachar Raindel * information potentially belongs to a now completely 23394e047f89SShachar Raindel * unrelated process. 23404e047f89SShachar Raindel */ 23414e047f89SShachar Raindel if (page) 23424e047f89SShachar Raindel page_cpupid_xchg_last(page, (1 << LAST_CPUPID_SHIFT) - 1); 23434e047f89SShachar Raindel 23442994302bSJan Kara flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); 23452994302bSJan Kara entry = pte_mkyoung(vmf->orig_pte); 23464e047f89SShachar Raindel entry = maybe_mkwrite(pte_mkdirty(entry), vma); 234782b0f8c3SJan Kara if (ptep_set_access_flags(vma, vmf->address, vmf->pte, entry, 1)) 234882b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 234982b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 23504e047f89SShachar Raindel } 23514e047f89SShachar Raindel 23524e047f89SShachar Raindel /* 23532f38ab2cSShachar Raindel * Handle the case of a page which we actually need to copy to a new page. 23542f38ab2cSShachar Raindel * 23552f38ab2cSShachar Raindel * Called with mmap_sem locked and the old page referenced, but 23562f38ab2cSShachar Raindel * without the ptl held. 23572f38ab2cSShachar Raindel * 23582f38ab2cSShachar Raindel * High level logic flow: 23592f38ab2cSShachar Raindel * 23602f38ab2cSShachar Raindel * - Allocate a page, copy the content of the old page to the new one. 23612f38ab2cSShachar Raindel * - Handle book keeping and accounting - cgroups, mmu-notifiers, etc. 23622f38ab2cSShachar Raindel * - Take the PTL. If the pte changed, bail out and release the allocated page 23632f38ab2cSShachar Raindel * - If the pte is still the way we remember it, update the page table and all 23642f38ab2cSShachar Raindel * relevant references. This includes dropping the reference the page-table 23652f38ab2cSShachar Raindel * held to the old page, as well as updating the rmap. 23662f38ab2cSShachar Raindel * - In any case, unlock the PTL and drop the reference we took to the old page. 23672f38ab2cSShachar Raindel */ 23682b740303SSouptick Joarder static vm_fault_t wp_page_copy(struct vm_fault *vmf) 23692f38ab2cSShachar Raindel { 237082b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 2371bae473a4SKirill A. Shutemov struct mm_struct *mm = vma->vm_mm; 2372a41b70d6SJan Kara struct page *old_page = vmf->page; 23732f38ab2cSShachar Raindel struct page *new_page = NULL; 23742f38ab2cSShachar Raindel pte_t entry; 23752f38ab2cSShachar Raindel int page_copied = 0; 23762f38ab2cSShachar Raindel struct mem_cgroup *memcg; 2377ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 23782f38ab2cSShachar Raindel 23792f38ab2cSShachar Raindel if (unlikely(anon_vma_prepare(vma))) 23802f38ab2cSShachar Raindel goto oom; 23812f38ab2cSShachar Raindel 23822994302bSJan Kara if (is_zero_pfn(pte_pfn(vmf->orig_pte))) { 238382b0f8c3SJan Kara new_page = alloc_zeroed_user_highpage_movable(vma, 238482b0f8c3SJan Kara vmf->address); 23852f38ab2cSShachar Raindel if (!new_page) 23862f38ab2cSShachar Raindel goto oom; 23872f38ab2cSShachar Raindel } else { 2388bae473a4SKirill A. Shutemov new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, 238982b0f8c3SJan Kara vmf->address); 23902f38ab2cSShachar Raindel if (!new_page) 23912f38ab2cSShachar Raindel goto oom; 2392*83d116c5SJia He 2393*83d116c5SJia He if (!cow_user_page(new_page, old_page, vmf)) { 2394*83d116c5SJia He /* 2395*83d116c5SJia He * COW failed, if the fault was solved by other, 2396*83d116c5SJia He * it's fine. If not, userspace would re-fault on 2397*83d116c5SJia He * the same address and we will handle the fault 2398*83d116c5SJia He * from the second attempt. 2399*83d116c5SJia He */ 2400*83d116c5SJia He put_page(new_page); 2401*83d116c5SJia He if (old_page) 2402*83d116c5SJia He put_page(old_page); 2403*83d116c5SJia He return 0; 2404*83d116c5SJia He } 24052f38ab2cSShachar Raindel } 24062f38ab2cSShachar Raindel 24072cf85583STejun Heo if (mem_cgroup_try_charge_delay(new_page, mm, GFP_KERNEL, &memcg, false)) 24082f38ab2cSShachar Raindel goto oom_free_new; 24092f38ab2cSShachar Raindel 2410eb3c24f3SMel Gorman __SetPageUptodate(new_page); 2411eb3c24f3SMel Gorman 24127269f999SJérôme Glisse mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm, 24136f4f13e8SJérôme Glisse vmf->address & PAGE_MASK, 2414ac46d4f3SJérôme Glisse (vmf->address & PAGE_MASK) + PAGE_SIZE); 2415ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 24162f38ab2cSShachar Raindel 24172f38ab2cSShachar Raindel /* 24182f38ab2cSShachar Raindel * Re-check the pte - we dropped the lock 24192f38ab2cSShachar Raindel */ 242082b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(mm, vmf->pmd, vmf->address, &vmf->ptl); 24212994302bSJan Kara if (likely(pte_same(*vmf->pte, vmf->orig_pte))) { 24222f38ab2cSShachar Raindel if (old_page) { 24232f38ab2cSShachar Raindel if (!PageAnon(old_page)) { 2424eca56ff9SJerome Marchand dec_mm_counter_fast(mm, 2425eca56ff9SJerome Marchand mm_counter_file(old_page)); 24262f38ab2cSShachar Raindel inc_mm_counter_fast(mm, MM_ANONPAGES); 24272f38ab2cSShachar Raindel } 24282f38ab2cSShachar Raindel } else { 24292f38ab2cSShachar Raindel inc_mm_counter_fast(mm, MM_ANONPAGES); 24302f38ab2cSShachar Raindel } 24312994302bSJan Kara flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); 24322f38ab2cSShachar Raindel entry = mk_pte(new_page, vma->vm_page_prot); 24332f38ab2cSShachar Raindel entry = maybe_mkwrite(pte_mkdirty(entry), vma); 24342f38ab2cSShachar Raindel /* 24352f38ab2cSShachar Raindel * Clear the pte entry and flush it first, before updating the 24362f38ab2cSShachar Raindel * pte with the new entry. This will avoid a race condition 24372f38ab2cSShachar Raindel * seen in the presence of one thread doing SMC and another 24382f38ab2cSShachar Raindel * thread doing COW. 24392f38ab2cSShachar Raindel */ 244082b0f8c3SJan Kara ptep_clear_flush_notify(vma, vmf->address, vmf->pte); 244182b0f8c3SJan Kara page_add_new_anon_rmap(new_page, vma, vmf->address, false); 2442f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(new_page, memcg, false, false); 24432f38ab2cSShachar Raindel lru_cache_add_active_or_unevictable(new_page, vma); 24442f38ab2cSShachar Raindel /* 24452f38ab2cSShachar Raindel * We call the notify macro here because, when using secondary 24462f38ab2cSShachar Raindel * mmu page tables (such as kvm shadow page tables), we want the 24472f38ab2cSShachar Raindel * new page to be mapped directly into the secondary page table. 24482f38ab2cSShachar Raindel */ 244982b0f8c3SJan Kara set_pte_at_notify(mm, vmf->address, vmf->pte, entry); 245082b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 24512f38ab2cSShachar Raindel if (old_page) { 24522f38ab2cSShachar Raindel /* 24532f38ab2cSShachar Raindel * Only after switching the pte to the new page may 24542f38ab2cSShachar Raindel * we remove the mapcount here. Otherwise another 24552f38ab2cSShachar Raindel * process may come and find the rmap count decremented 24562f38ab2cSShachar Raindel * before the pte is switched to the new page, and 24572f38ab2cSShachar Raindel * "reuse" the old page writing into it while our pte 24582f38ab2cSShachar Raindel * here still points into it and can be read by other 24592f38ab2cSShachar Raindel * threads. 24602f38ab2cSShachar Raindel * 24612f38ab2cSShachar Raindel * The critical issue is to order this 24622f38ab2cSShachar Raindel * page_remove_rmap with the ptp_clear_flush above. 24632f38ab2cSShachar Raindel * Those stores are ordered by (if nothing else,) 24642f38ab2cSShachar Raindel * the barrier present in the atomic_add_negative 24652f38ab2cSShachar Raindel * in page_remove_rmap. 24662f38ab2cSShachar Raindel * 24672f38ab2cSShachar Raindel * Then the TLB flush in ptep_clear_flush ensures that 24682f38ab2cSShachar Raindel * no process can access the old page before the 24692f38ab2cSShachar Raindel * decremented mapcount is visible. And the old page 24702f38ab2cSShachar Raindel * cannot be reused until after the decremented 24712f38ab2cSShachar Raindel * mapcount is visible. So transitively, TLBs to 24722f38ab2cSShachar Raindel * old page will be flushed before it can be reused. 24732f38ab2cSShachar Raindel */ 2474d281ee61SKirill A. Shutemov page_remove_rmap(old_page, false); 24752f38ab2cSShachar Raindel } 24762f38ab2cSShachar Raindel 24772f38ab2cSShachar Raindel /* Free the old page.. */ 24782f38ab2cSShachar Raindel new_page = old_page; 24792f38ab2cSShachar Raindel page_copied = 1; 24802f38ab2cSShachar Raindel } else { 2481f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(new_page, memcg, false); 24822f38ab2cSShachar Raindel } 24832f38ab2cSShachar Raindel 24842f38ab2cSShachar Raindel if (new_page) 248509cbfeafSKirill A. Shutemov put_page(new_page); 24862f38ab2cSShachar Raindel 248782b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 24884645b9feSJérôme Glisse /* 24894645b9feSJérôme Glisse * No need to double call mmu_notifier->invalidate_range() callback as 24904645b9feSJérôme Glisse * the above ptep_clear_flush_notify() did already call it. 24914645b9feSJérôme Glisse */ 2492ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_only_end(&range); 24932f38ab2cSShachar Raindel if (old_page) { 24942f38ab2cSShachar Raindel /* 24952f38ab2cSShachar Raindel * Don't let another task, with possibly unlocked vma, 24962f38ab2cSShachar Raindel * keep the mlocked page. 24972f38ab2cSShachar Raindel */ 24982f38ab2cSShachar Raindel if (page_copied && (vma->vm_flags & VM_LOCKED)) { 24992f38ab2cSShachar Raindel lock_page(old_page); /* LRU manipulation */ 2500e90309c9SKirill A. Shutemov if (PageMlocked(old_page)) 25012f38ab2cSShachar Raindel munlock_vma_page(old_page); 25022f38ab2cSShachar Raindel unlock_page(old_page); 25032f38ab2cSShachar Raindel } 250409cbfeafSKirill A. Shutemov put_page(old_page); 25052f38ab2cSShachar Raindel } 25062f38ab2cSShachar Raindel return page_copied ? VM_FAULT_WRITE : 0; 25072f38ab2cSShachar Raindel oom_free_new: 250809cbfeafSKirill A. Shutemov put_page(new_page); 25092f38ab2cSShachar Raindel oom: 25102f38ab2cSShachar Raindel if (old_page) 251109cbfeafSKirill A. Shutemov put_page(old_page); 25122f38ab2cSShachar Raindel return VM_FAULT_OOM; 25132f38ab2cSShachar Raindel } 25142f38ab2cSShachar Raindel 251566a6197cSJan Kara /** 251666a6197cSJan Kara * finish_mkwrite_fault - finish page fault for a shared mapping, making PTE 251766a6197cSJan Kara * writeable once the page is prepared 251866a6197cSJan Kara * 251966a6197cSJan Kara * @vmf: structure describing the fault 252066a6197cSJan Kara * 252166a6197cSJan Kara * This function handles all that is needed to finish a write page fault in a 252266a6197cSJan Kara * shared mapping due to PTE being read-only once the mapped page is prepared. 2523a862f68aSMike Rapoport * It handles locking of PTE and modifying it. 252466a6197cSJan Kara * 252566a6197cSJan Kara * The function expects the page to be locked or other protection against 252666a6197cSJan Kara * concurrent faults / writeback (such as DAX radix tree locks). 2527a862f68aSMike Rapoport * 2528a862f68aSMike Rapoport * Return: %VM_FAULT_WRITE on success, %0 when PTE got changed before 2529a862f68aSMike Rapoport * we acquired PTE lock. 253066a6197cSJan Kara */ 25312b740303SSouptick Joarder vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf) 253266a6197cSJan Kara { 253366a6197cSJan Kara WARN_ON_ONCE(!(vmf->vma->vm_flags & VM_SHARED)); 253466a6197cSJan Kara vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, vmf->address, 253566a6197cSJan Kara &vmf->ptl); 253666a6197cSJan Kara /* 253766a6197cSJan Kara * We might have raced with another page fault while we released the 253866a6197cSJan Kara * pte_offset_map_lock. 253966a6197cSJan Kara */ 254066a6197cSJan Kara if (!pte_same(*vmf->pte, vmf->orig_pte)) { 254166a6197cSJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2542a19e2553SJan Kara return VM_FAULT_NOPAGE; 254366a6197cSJan Kara } 254466a6197cSJan Kara wp_page_reuse(vmf); 2545a19e2553SJan Kara return 0; 254666a6197cSJan Kara } 254766a6197cSJan Kara 2548dd906184SBoaz Harrosh /* 2549dd906184SBoaz Harrosh * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED 2550dd906184SBoaz Harrosh * mapping 2551dd906184SBoaz Harrosh */ 25522b740303SSouptick Joarder static vm_fault_t wp_pfn_shared(struct vm_fault *vmf) 2553dd906184SBoaz Harrosh { 255482b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 2555bae473a4SKirill A. Shutemov 2556dd906184SBoaz Harrosh if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) { 25572b740303SSouptick Joarder vm_fault_t ret; 2558dd906184SBoaz Harrosh 255982b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2560fe82221fSJan Kara vmf->flags |= FAULT_FLAG_MKWRITE; 256111bac800SDave Jiang ret = vma->vm_ops->pfn_mkwrite(vmf); 25622f89dc12SJan Kara if (ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)) 2563dd906184SBoaz Harrosh return ret; 256466a6197cSJan Kara return finish_mkwrite_fault(vmf); 2565dd906184SBoaz Harrosh } 2566997dd98dSJan Kara wp_page_reuse(vmf); 2567997dd98dSJan Kara return VM_FAULT_WRITE; 2568dd906184SBoaz Harrosh } 2569dd906184SBoaz Harrosh 25702b740303SSouptick Joarder static vm_fault_t wp_page_shared(struct vm_fault *vmf) 257182b0f8c3SJan Kara __releases(vmf->ptl) 257293e478d4SShachar Raindel { 257382b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 257493e478d4SShachar Raindel 2575a41b70d6SJan Kara get_page(vmf->page); 257693e478d4SShachar Raindel 257793e478d4SShachar Raindel if (vma->vm_ops && vma->vm_ops->page_mkwrite) { 25782b740303SSouptick Joarder vm_fault_t tmp; 257993e478d4SShachar Raindel 258082b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 258138b8cb7fSJan Kara tmp = do_page_mkwrite(vmf); 258293e478d4SShachar Raindel if (unlikely(!tmp || (tmp & 258393e478d4SShachar Raindel (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { 2584a41b70d6SJan Kara put_page(vmf->page); 258593e478d4SShachar Raindel return tmp; 258693e478d4SShachar Raindel } 258766a6197cSJan Kara tmp = finish_mkwrite_fault(vmf); 2588a19e2553SJan Kara if (unlikely(tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) { 2589a41b70d6SJan Kara unlock_page(vmf->page); 2590a41b70d6SJan Kara put_page(vmf->page); 259166a6197cSJan Kara return tmp; 259293e478d4SShachar Raindel } 259366a6197cSJan Kara } else { 2594997dd98dSJan Kara wp_page_reuse(vmf); 2595997dd98dSJan Kara lock_page(vmf->page); 259693e478d4SShachar Raindel } 2597997dd98dSJan Kara fault_dirty_shared_page(vma, vmf->page); 2598997dd98dSJan Kara put_page(vmf->page); 259993e478d4SShachar Raindel 2600997dd98dSJan Kara return VM_FAULT_WRITE; 260193e478d4SShachar Raindel } 260293e478d4SShachar Raindel 26032f38ab2cSShachar Raindel /* 26041da177e4SLinus Torvalds * This routine handles present pages, when users try to write 26051da177e4SLinus Torvalds * to a shared page. It is done by copying the page to a new address 26061da177e4SLinus Torvalds * and decrementing the shared-page counter for the old page. 26071da177e4SLinus Torvalds * 26081da177e4SLinus Torvalds * Note that this routine assumes that the protection checks have been 26091da177e4SLinus Torvalds * done by the caller (the low-level page fault routine in most cases). 26101da177e4SLinus Torvalds * Thus we can safely just mark it writable once we've done any necessary 26111da177e4SLinus Torvalds * COW. 26121da177e4SLinus Torvalds * 26131da177e4SLinus Torvalds * We also mark the page dirty at this point even though the page will 26141da177e4SLinus Torvalds * change only once the write actually happens. This avoids a few races, 26151da177e4SLinus Torvalds * and potentially makes it more efficient. 26161da177e4SLinus Torvalds * 26178f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 26188f4e2101SHugh Dickins * but allow concurrent faults), with pte both mapped and locked. 26198f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 26201da177e4SLinus Torvalds */ 26212b740303SSouptick Joarder static vm_fault_t do_wp_page(struct vm_fault *vmf) 262282b0f8c3SJan Kara __releases(vmf->ptl) 26231da177e4SLinus Torvalds { 262482b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 26251da177e4SLinus Torvalds 2626a41b70d6SJan Kara vmf->page = vm_normal_page(vma, vmf->address, vmf->orig_pte); 2627a41b70d6SJan Kara if (!vmf->page) { 2628251b97f5SPeter Zijlstra /* 262964e45507SPeter Feiner * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a 263064e45507SPeter Feiner * VM_PFNMAP VMA. 2631251b97f5SPeter Zijlstra * 2632251b97f5SPeter Zijlstra * We should not cow pages in a shared writeable mapping. 2633dd906184SBoaz Harrosh * Just mark the pages writable and/or call ops->pfn_mkwrite. 2634251b97f5SPeter Zijlstra */ 2635251b97f5SPeter Zijlstra if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 2636251b97f5SPeter Zijlstra (VM_WRITE|VM_SHARED)) 26372994302bSJan Kara return wp_pfn_shared(vmf); 26382f38ab2cSShachar Raindel 263982b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2640a41b70d6SJan Kara return wp_page_copy(vmf); 2641251b97f5SPeter Zijlstra } 26421da177e4SLinus Torvalds 2643d08b3851SPeter Zijlstra /* 2644ee6a6457SPeter Zijlstra * Take out anonymous pages first, anonymous shared vmas are 2645ee6a6457SPeter Zijlstra * not dirty accountable. 2646d08b3851SPeter Zijlstra */ 264752d1e606SKirill Tkhai if (PageAnon(vmf->page)) { 2648ba3c4ce6SHuang Ying int total_map_swapcount; 264952d1e606SKirill Tkhai if (PageKsm(vmf->page) && (PageSwapCache(vmf->page) || 265052d1e606SKirill Tkhai page_count(vmf->page) != 1)) 265152d1e606SKirill Tkhai goto copy; 2652a41b70d6SJan Kara if (!trylock_page(vmf->page)) { 2653a41b70d6SJan Kara get_page(vmf->page); 265482b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2655a41b70d6SJan Kara lock_page(vmf->page); 265682b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 265782b0f8c3SJan Kara vmf->address, &vmf->ptl); 26582994302bSJan Kara if (!pte_same(*vmf->pte, vmf->orig_pte)) { 2659a41b70d6SJan Kara unlock_page(vmf->page); 266082b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2661a41b70d6SJan Kara put_page(vmf->page); 266228766805SShachar Raindel return 0; 2663ab967d86SHugh Dickins } 2664a41b70d6SJan Kara put_page(vmf->page); 2665ab967d86SHugh Dickins } 266652d1e606SKirill Tkhai if (PageKsm(vmf->page)) { 266752d1e606SKirill Tkhai bool reused = reuse_ksm_page(vmf->page, vmf->vma, 266852d1e606SKirill Tkhai vmf->address); 266952d1e606SKirill Tkhai unlock_page(vmf->page); 267052d1e606SKirill Tkhai if (!reused) 267152d1e606SKirill Tkhai goto copy; 267252d1e606SKirill Tkhai wp_page_reuse(vmf); 267352d1e606SKirill Tkhai return VM_FAULT_WRITE; 267452d1e606SKirill Tkhai } 2675ba3c4ce6SHuang Ying if (reuse_swap_page(vmf->page, &total_map_swapcount)) { 2676ba3c4ce6SHuang Ying if (total_map_swapcount == 1) { 2677c44b6743SRik van Riel /* 26786d0a07edSAndrea Arcangeli * The page is all ours. Move it to 26796d0a07edSAndrea Arcangeli * our anon_vma so the rmap code will 26806d0a07edSAndrea Arcangeli * not search our parent or siblings. 26816d0a07edSAndrea Arcangeli * Protected against the rmap code by 26826d0a07edSAndrea Arcangeli * the page lock. 2683c44b6743SRik van Riel */ 2684a41b70d6SJan Kara page_move_anon_rmap(vmf->page, vma); 26856d0a07edSAndrea Arcangeli } 2686a41b70d6SJan Kara unlock_page(vmf->page); 2687997dd98dSJan Kara wp_page_reuse(vmf); 2688997dd98dSJan Kara return VM_FAULT_WRITE; 2689b009c024SMichel Lespinasse } 2690a41b70d6SJan Kara unlock_page(vmf->page); 2691ee6a6457SPeter Zijlstra } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 2692d08b3851SPeter Zijlstra (VM_WRITE|VM_SHARED))) { 2693a41b70d6SJan Kara return wp_page_shared(vmf); 26941da177e4SLinus Torvalds } 269552d1e606SKirill Tkhai copy: 26961da177e4SLinus Torvalds /* 26971da177e4SLinus Torvalds * Ok, we need to copy. Oh, well.. 26981da177e4SLinus Torvalds */ 2699a41b70d6SJan Kara get_page(vmf->page); 270028766805SShachar Raindel 270182b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2702a41b70d6SJan Kara return wp_page_copy(vmf); 27031da177e4SLinus Torvalds } 27041da177e4SLinus Torvalds 270597a89413SPeter Zijlstra static void unmap_mapping_range_vma(struct vm_area_struct *vma, 27061da177e4SLinus Torvalds unsigned long start_addr, unsigned long end_addr, 27071da177e4SLinus Torvalds struct zap_details *details) 27081da177e4SLinus Torvalds { 2709f5cc4eefSAl Viro zap_page_range_single(vma, start_addr, end_addr - start_addr, details); 27101da177e4SLinus Torvalds } 27111da177e4SLinus Torvalds 2712f808c13fSDavidlohr Bueso static inline void unmap_mapping_range_tree(struct rb_root_cached *root, 27131da177e4SLinus Torvalds struct zap_details *details) 27141da177e4SLinus Torvalds { 27151da177e4SLinus Torvalds struct vm_area_struct *vma; 27161da177e4SLinus Torvalds pgoff_t vba, vea, zba, zea; 27171da177e4SLinus Torvalds 27186b2dbba8SMichel Lespinasse vma_interval_tree_foreach(vma, root, 27191da177e4SLinus Torvalds details->first_index, details->last_index) { 27201da177e4SLinus Torvalds 27211da177e4SLinus Torvalds vba = vma->vm_pgoff; 2722d6e93217SLibin vea = vba + vma_pages(vma) - 1; 27231da177e4SLinus Torvalds zba = details->first_index; 27241da177e4SLinus Torvalds if (zba < vba) 27251da177e4SLinus Torvalds zba = vba; 27261da177e4SLinus Torvalds zea = details->last_index; 27271da177e4SLinus Torvalds if (zea > vea) 27281da177e4SLinus Torvalds zea = vea; 27291da177e4SLinus Torvalds 273097a89413SPeter Zijlstra unmap_mapping_range_vma(vma, 27311da177e4SLinus Torvalds ((zba - vba) << PAGE_SHIFT) + vma->vm_start, 27321da177e4SLinus Torvalds ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start, 273397a89413SPeter Zijlstra details); 27341da177e4SLinus Torvalds } 27351da177e4SLinus Torvalds } 27361da177e4SLinus Torvalds 27371da177e4SLinus Torvalds /** 2738977fbdcdSMatthew Wilcox * unmap_mapping_pages() - Unmap pages from processes. 2739977fbdcdSMatthew Wilcox * @mapping: The address space containing pages to be unmapped. 2740977fbdcdSMatthew Wilcox * @start: Index of first page to be unmapped. 2741977fbdcdSMatthew Wilcox * @nr: Number of pages to be unmapped. 0 to unmap to end of file. 2742977fbdcdSMatthew Wilcox * @even_cows: Whether to unmap even private COWed pages. 2743977fbdcdSMatthew Wilcox * 2744977fbdcdSMatthew Wilcox * Unmap the pages in this address space from any userspace process which 2745977fbdcdSMatthew Wilcox * has them mmaped. Generally, you want to remove COWed pages as well when 2746977fbdcdSMatthew Wilcox * a file is being truncated, but not when invalidating pages from the page 2747977fbdcdSMatthew Wilcox * cache. 2748977fbdcdSMatthew Wilcox */ 2749977fbdcdSMatthew Wilcox void unmap_mapping_pages(struct address_space *mapping, pgoff_t start, 2750977fbdcdSMatthew Wilcox pgoff_t nr, bool even_cows) 2751977fbdcdSMatthew Wilcox { 2752977fbdcdSMatthew Wilcox struct zap_details details = { }; 2753977fbdcdSMatthew Wilcox 2754977fbdcdSMatthew Wilcox details.check_mapping = even_cows ? NULL : mapping; 2755977fbdcdSMatthew Wilcox details.first_index = start; 2756977fbdcdSMatthew Wilcox details.last_index = start + nr - 1; 2757977fbdcdSMatthew Wilcox if (details.last_index < details.first_index) 2758977fbdcdSMatthew Wilcox details.last_index = ULONG_MAX; 2759977fbdcdSMatthew Wilcox 2760977fbdcdSMatthew Wilcox i_mmap_lock_write(mapping); 2761977fbdcdSMatthew Wilcox if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root))) 2762977fbdcdSMatthew Wilcox unmap_mapping_range_tree(&mapping->i_mmap, &details); 2763977fbdcdSMatthew Wilcox i_mmap_unlock_write(mapping); 2764977fbdcdSMatthew Wilcox } 2765977fbdcdSMatthew Wilcox 2766977fbdcdSMatthew Wilcox /** 27678a5f14a2SKirill A. Shutemov * unmap_mapping_range - unmap the portion of all mmaps in the specified 2768977fbdcdSMatthew Wilcox * address_space corresponding to the specified byte range in the underlying 27698a5f14a2SKirill A. Shutemov * file. 27708a5f14a2SKirill A. Shutemov * 27713d41088fSMartin Waitz * @mapping: the address space containing mmaps to be unmapped. 27721da177e4SLinus Torvalds * @holebegin: byte in first page to unmap, relative to the start of 27731da177e4SLinus Torvalds * the underlying file. This will be rounded down to a PAGE_SIZE 277425d9e2d1Snpiggin@suse.de * boundary. Note that this is different from truncate_pagecache(), which 27751da177e4SLinus Torvalds * must keep the partial page. In contrast, we must get rid of 27761da177e4SLinus Torvalds * partial pages. 27771da177e4SLinus Torvalds * @holelen: size of prospective hole in bytes. This will be rounded 27781da177e4SLinus Torvalds * up to a PAGE_SIZE boundary. A holelen of zero truncates to the 27791da177e4SLinus Torvalds * end of the file. 27801da177e4SLinus Torvalds * @even_cows: 1 when truncating a file, unmap even private COWed pages; 27811da177e4SLinus Torvalds * but 0 when invalidating pagecache, don't throw away private data. 27821da177e4SLinus Torvalds */ 27831da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping, 27841da177e4SLinus Torvalds loff_t const holebegin, loff_t const holelen, int even_cows) 27851da177e4SLinus Torvalds { 27861da177e4SLinus Torvalds pgoff_t hba = holebegin >> PAGE_SHIFT; 27871da177e4SLinus Torvalds pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 27881da177e4SLinus Torvalds 27891da177e4SLinus Torvalds /* Check for overflow. */ 27901da177e4SLinus Torvalds if (sizeof(holelen) > sizeof(hlen)) { 27911da177e4SLinus Torvalds long long holeend = 27921da177e4SLinus Torvalds (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 27931da177e4SLinus Torvalds if (holeend & ~(long long)ULONG_MAX) 27941da177e4SLinus Torvalds hlen = ULONG_MAX - hba + 1; 27951da177e4SLinus Torvalds } 27961da177e4SLinus Torvalds 2797977fbdcdSMatthew Wilcox unmap_mapping_pages(mapping, hba, hlen, even_cows); 27981da177e4SLinus Torvalds } 27991da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range); 28001da177e4SLinus Torvalds 28011da177e4SLinus Torvalds /* 28028f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 28038f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 28049a95f3cfSPaul Cassella * We return with pte unmapped and unlocked. 28059a95f3cfSPaul Cassella * 28069a95f3cfSPaul Cassella * We return with the mmap_sem locked or unlocked in the same cases 28079a95f3cfSPaul Cassella * as does filemap_fault(). 28081da177e4SLinus Torvalds */ 28092b740303SSouptick Joarder vm_fault_t do_swap_page(struct vm_fault *vmf) 28101da177e4SLinus Torvalds { 281182b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 2812eaf649ebSMinchan Kim struct page *page = NULL, *swapcache; 281300501b53SJohannes Weiner struct mem_cgroup *memcg; 281465500d23SHugh Dickins swp_entry_t entry; 28151da177e4SLinus Torvalds pte_t pte; 2816d065bd81SMichel Lespinasse int locked; 2817ad8c2ee8SRik van Riel int exclusive = 0; 28182b740303SSouptick Joarder vm_fault_t ret = 0; 28191da177e4SLinus Torvalds 2820eaf649ebSMinchan Kim if (!pte_unmap_same(vma->vm_mm, vmf->pmd, vmf->pte, vmf->orig_pte)) 28218f4e2101SHugh Dickins goto out; 282265500d23SHugh Dickins 28232994302bSJan Kara entry = pte_to_swp_entry(vmf->orig_pte); 2824d1737fdbSAndi Kleen if (unlikely(non_swap_entry(entry))) { 28250697212aSChristoph Lameter if (is_migration_entry(entry)) { 282682b0f8c3SJan Kara migration_entry_wait(vma->vm_mm, vmf->pmd, 282782b0f8c3SJan Kara vmf->address); 28285042db43SJérôme Glisse } else if (is_device_private_entry(entry)) { 2829897e6365SChristoph Hellwig vmf->page = device_private_entry_to_page(entry); 2830897e6365SChristoph Hellwig ret = vmf->page->pgmap->ops->migrate_to_ram(vmf); 2831d1737fdbSAndi Kleen } else if (is_hwpoison_entry(entry)) { 2832d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 2833d1737fdbSAndi Kleen } else { 28342994302bSJan Kara print_bad_pte(vma, vmf->address, vmf->orig_pte, NULL); 2835d99be1a8SHugh Dickins ret = VM_FAULT_SIGBUS; 2836d1737fdbSAndi Kleen } 28370697212aSChristoph Lameter goto out; 28380697212aSChristoph Lameter } 28390bcac06fSMinchan Kim 28400bcac06fSMinchan Kim 28410ff92245SShailabh Nagar delayacct_set_flag(DELAYACCT_PF_SWAPIN); 2842eaf649ebSMinchan Kim page = lookup_swap_cache(entry, vma, vmf->address); 2843f8020772SMinchan Kim swapcache = page; 2844f8020772SMinchan Kim 28451da177e4SLinus Torvalds if (!page) { 28460bcac06fSMinchan Kim struct swap_info_struct *si = swp_swap_info(entry); 28470bcac06fSMinchan Kim 2848aa8d22a1SMinchan Kim if (si->flags & SWP_SYNCHRONOUS_IO && 2849eb085574SHuang Ying __swap_count(entry) == 1) { 28500bcac06fSMinchan Kim /* skip swapcache */ 2851e9e9b7ecSMinchan Kim page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, 2852e9e9b7ecSMinchan Kim vmf->address); 28530bcac06fSMinchan Kim if (page) { 28540bcac06fSMinchan Kim __SetPageLocked(page); 28550bcac06fSMinchan Kim __SetPageSwapBacked(page); 28560bcac06fSMinchan Kim set_page_private(page, entry.val); 28570bcac06fSMinchan Kim lru_cache_add_anon(page); 28580bcac06fSMinchan Kim swap_readpage(page, true); 28590bcac06fSMinchan Kim } 2860aa8d22a1SMinchan Kim } else { 2861e9e9b7ecSMinchan Kim page = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, 2862e9e9b7ecSMinchan Kim vmf); 2863aa8d22a1SMinchan Kim swapcache = page; 28640bcac06fSMinchan Kim } 28650bcac06fSMinchan Kim 2866ec560175SHuang Ying if (!page) { 28671da177e4SLinus Torvalds /* 28688f4e2101SHugh Dickins * Back out if somebody else faulted in this pte 28698f4e2101SHugh Dickins * while we released the pte lock. 28701da177e4SLinus Torvalds */ 287182b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 287282b0f8c3SJan Kara vmf->address, &vmf->ptl); 28732994302bSJan Kara if (likely(pte_same(*vmf->pte, vmf->orig_pte))) 28741da177e4SLinus Torvalds ret = VM_FAULT_OOM; 28750ff92245SShailabh Nagar delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 287665500d23SHugh Dickins goto unlock; 28771da177e4SLinus Torvalds } 28781da177e4SLinus Torvalds 28791da177e4SLinus Torvalds /* Had to read the page from swap area: Major fault */ 28801da177e4SLinus Torvalds ret = VM_FAULT_MAJOR; 2881f8891e5eSChristoph Lameter count_vm_event(PGMAJFAULT); 28822262185cSRoman Gushchin count_memcg_event_mm(vma->vm_mm, PGMAJFAULT); 2883d1737fdbSAndi Kleen } else if (PageHWPoison(page)) { 288471f72525SWu Fengguang /* 288571f72525SWu Fengguang * hwpoisoned dirty swapcache pages are kept for killing 288671f72525SWu Fengguang * owner processes (which may be unknown at hwpoison time) 288771f72525SWu Fengguang */ 2888d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 2889d1737fdbSAndi Kleen delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 28904779cb31SAndi Kleen goto out_release; 28911da177e4SLinus Torvalds } 28921da177e4SLinus Torvalds 289382b0f8c3SJan Kara locked = lock_page_or_retry(page, vma->vm_mm, vmf->flags); 2894e709ffd6SRik van Riel 289520a1022dSBalbir Singh delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 2896d065bd81SMichel Lespinasse if (!locked) { 2897d065bd81SMichel Lespinasse ret |= VM_FAULT_RETRY; 2898d065bd81SMichel Lespinasse goto out_release; 2899d065bd81SMichel Lespinasse } 29001da177e4SLinus Torvalds 29014969c119SAndrea Arcangeli /* 290231c4a3d3SHugh Dickins * Make sure try_to_free_swap or reuse_swap_page or swapoff did not 290331c4a3d3SHugh Dickins * release the swapcache from under us. The page pin, and pte_same 290431c4a3d3SHugh Dickins * test below, are not enough to exclude that. Even if it is still 290531c4a3d3SHugh Dickins * swapcache, we need to check that the page's swap has not changed. 29064969c119SAndrea Arcangeli */ 29070bcac06fSMinchan Kim if (unlikely((!PageSwapCache(page) || 29080bcac06fSMinchan Kim page_private(page) != entry.val)) && swapcache) 29094969c119SAndrea Arcangeli goto out_page; 29104969c119SAndrea Arcangeli 291182b0f8c3SJan Kara page = ksm_might_need_to_copy(page, vma, vmf->address); 29124969c119SAndrea Arcangeli if (unlikely(!page)) { 29135ad64688SHugh Dickins ret = VM_FAULT_OOM; 29144969c119SAndrea Arcangeli page = swapcache; 29154969c119SAndrea Arcangeli goto out_page; 29164969c119SAndrea Arcangeli } 29175ad64688SHugh Dickins 29182cf85583STejun Heo if (mem_cgroup_try_charge_delay(page, vma->vm_mm, GFP_KERNEL, 2919bae473a4SKirill A. Shutemov &memcg, false)) { 2920073e587eSKAMEZAWA Hiroyuki ret = VM_FAULT_OOM; 2921bc43f75cSJohannes Weiner goto out_page; 2922073e587eSKAMEZAWA Hiroyuki } 2923073e587eSKAMEZAWA Hiroyuki 29241da177e4SLinus Torvalds /* 29258f4e2101SHugh Dickins * Back out if somebody else already faulted in this pte. 29261da177e4SLinus Torvalds */ 292782b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 292882b0f8c3SJan Kara &vmf->ptl); 29292994302bSJan Kara if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) 2930b8107480SKirill Korotaev goto out_nomap; 2931b8107480SKirill Korotaev 2932b8107480SKirill Korotaev if (unlikely(!PageUptodate(page))) { 2933b8107480SKirill Korotaev ret = VM_FAULT_SIGBUS; 2934b8107480SKirill Korotaev goto out_nomap; 29351da177e4SLinus Torvalds } 29361da177e4SLinus Torvalds 29378c7c6e34SKAMEZAWA Hiroyuki /* 29388c7c6e34SKAMEZAWA Hiroyuki * The page isn't present yet, go ahead with the fault. 29398c7c6e34SKAMEZAWA Hiroyuki * 29408c7c6e34SKAMEZAWA Hiroyuki * Be careful about the sequence of operations here. 29418c7c6e34SKAMEZAWA Hiroyuki * To get its accounting right, reuse_swap_page() must be called 29428c7c6e34SKAMEZAWA Hiroyuki * while the page is counted on swap but not yet in mapcount i.e. 29438c7c6e34SKAMEZAWA Hiroyuki * before page_add_anon_rmap() and swap_free(); try_to_free_swap() 29448c7c6e34SKAMEZAWA Hiroyuki * must be called after the swap_free(), or it will never succeed. 29458c7c6e34SKAMEZAWA Hiroyuki */ 29461da177e4SLinus Torvalds 2947bae473a4SKirill A. Shutemov inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); 2948bae473a4SKirill A. Shutemov dec_mm_counter_fast(vma->vm_mm, MM_SWAPENTS); 29491da177e4SLinus Torvalds pte = mk_pte(page, vma->vm_page_prot); 295082b0f8c3SJan Kara if ((vmf->flags & FAULT_FLAG_WRITE) && reuse_swap_page(page, NULL)) { 29511da177e4SLinus Torvalds pte = maybe_mkwrite(pte_mkdirty(pte), vma); 295282b0f8c3SJan Kara vmf->flags &= ~FAULT_FLAG_WRITE; 29539a5b489bSAndrea Arcangeli ret |= VM_FAULT_WRITE; 2954d281ee61SKirill A. Shutemov exclusive = RMAP_EXCLUSIVE; 29551da177e4SLinus Torvalds } 29561da177e4SLinus Torvalds flush_icache_page(vma, page); 29572994302bSJan Kara if (pte_swp_soft_dirty(vmf->orig_pte)) 2958179ef71cSCyrill Gorcunov pte = pte_mksoft_dirty(pte); 295982b0f8c3SJan Kara set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte); 2960ca827d55SKhalid Aziz arch_do_swap_page(vma->vm_mm, vma, vmf->address, pte, vmf->orig_pte); 29612994302bSJan Kara vmf->orig_pte = pte; 29620bcac06fSMinchan Kim 29630bcac06fSMinchan Kim /* ksm created a completely new copy */ 29640bcac06fSMinchan Kim if (unlikely(page != swapcache && swapcache)) { 296582b0f8c3SJan Kara page_add_new_anon_rmap(page, vma, vmf->address, false); 2966f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, false); 296700501b53SJohannes Weiner lru_cache_add_active_or_unevictable(page, vma); 29680bcac06fSMinchan Kim } else { 29690bcac06fSMinchan Kim do_page_add_anon_rmap(page, vma, vmf->address, exclusive); 29700bcac06fSMinchan Kim mem_cgroup_commit_charge(page, memcg, true, false); 29710bcac06fSMinchan Kim activate_page(page); 297200501b53SJohannes Weiner } 29731da177e4SLinus Torvalds 2974c475a8abSHugh Dickins swap_free(entry); 29755ccc5abaSVladimir Davydov if (mem_cgroup_swap_full(page) || 29765ccc5abaSVladimir Davydov (vma->vm_flags & VM_LOCKED) || PageMlocked(page)) 2977a2c43eedSHugh Dickins try_to_free_swap(page); 2978c475a8abSHugh Dickins unlock_page(page); 29790bcac06fSMinchan Kim if (page != swapcache && swapcache) { 29804969c119SAndrea Arcangeli /* 29814969c119SAndrea Arcangeli * Hold the lock to avoid the swap entry to be reused 29824969c119SAndrea Arcangeli * until we take the PT lock for the pte_same() check 29834969c119SAndrea Arcangeli * (to avoid false positives from pte_same). For 29844969c119SAndrea Arcangeli * further safety release the lock after the swap_free 29854969c119SAndrea Arcangeli * so that the swap count won't change under a 29864969c119SAndrea Arcangeli * parallel locked swapcache. 29874969c119SAndrea Arcangeli */ 29884969c119SAndrea Arcangeli unlock_page(swapcache); 298909cbfeafSKirill A. Shutemov put_page(swapcache); 29904969c119SAndrea Arcangeli } 2991c475a8abSHugh Dickins 299282b0f8c3SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) { 29932994302bSJan Kara ret |= do_wp_page(vmf); 299461469f1dSHugh Dickins if (ret & VM_FAULT_ERROR) 299561469f1dSHugh Dickins ret &= VM_FAULT_ERROR; 29961da177e4SLinus Torvalds goto out; 29971da177e4SLinus Torvalds } 29981da177e4SLinus Torvalds 29991da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 300082b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 300165500d23SHugh Dickins unlock: 300282b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 30031da177e4SLinus Torvalds out: 30041da177e4SLinus Torvalds return ret; 3005b8107480SKirill Korotaev out_nomap: 3006f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 300782b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3008bc43f75cSJohannes Weiner out_page: 3009b8107480SKirill Korotaev unlock_page(page); 30104779cb31SAndi Kleen out_release: 301109cbfeafSKirill A. Shutemov put_page(page); 30120bcac06fSMinchan Kim if (page != swapcache && swapcache) { 30134969c119SAndrea Arcangeli unlock_page(swapcache); 301409cbfeafSKirill A. Shutemov put_page(swapcache); 30154969c119SAndrea Arcangeli } 301665500d23SHugh Dickins return ret; 30171da177e4SLinus Torvalds } 30181da177e4SLinus Torvalds 30191da177e4SLinus Torvalds /* 30208f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 30218f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 30228f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 30231da177e4SLinus Torvalds */ 30242b740303SSouptick Joarder static vm_fault_t do_anonymous_page(struct vm_fault *vmf) 30251da177e4SLinus Torvalds { 302682b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 302700501b53SJohannes Weiner struct mem_cgroup *memcg; 30288f4e2101SHugh Dickins struct page *page; 30292b740303SSouptick Joarder vm_fault_t ret = 0; 30301da177e4SLinus Torvalds pte_t entry; 30311da177e4SLinus Torvalds 30326b7339f4SKirill A. Shutemov /* File mapping without ->vm_ops ? */ 30336b7339f4SKirill A. Shutemov if (vma->vm_flags & VM_SHARED) 30346b7339f4SKirill A. Shutemov return VM_FAULT_SIGBUS; 30356b7339f4SKirill A. Shutemov 30367267ec00SKirill A. Shutemov /* 30377267ec00SKirill A. Shutemov * Use pte_alloc() instead of pte_alloc_map(). We can't run 30387267ec00SKirill A. Shutemov * pte_offset_map() on pmds where a huge pmd might be created 30397267ec00SKirill A. Shutemov * from a different thread. 30407267ec00SKirill A. Shutemov * 30417267ec00SKirill A. Shutemov * pte_alloc_map() is safe to use under down_write(mmap_sem) or when 30427267ec00SKirill A. Shutemov * parallel threads are excluded by other means. 30437267ec00SKirill A. Shutemov * 30447267ec00SKirill A. Shutemov * Here we only have down_read(mmap_sem). 30457267ec00SKirill A. Shutemov */ 30464cf58924SJoel Fernandes (Google) if (pte_alloc(vma->vm_mm, vmf->pmd)) 30477267ec00SKirill A. Shutemov return VM_FAULT_OOM; 30487267ec00SKirill A. Shutemov 30497267ec00SKirill A. Shutemov /* See the comment in pte_alloc_one_map() */ 305082b0f8c3SJan Kara if (unlikely(pmd_trans_unstable(vmf->pmd))) 30517267ec00SKirill A. Shutemov return 0; 30527267ec00SKirill A. Shutemov 305311ac5524SLinus Torvalds /* Use the zero-page for reads */ 305482b0f8c3SJan Kara if (!(vmf->flags & FAULT_FLAG_WRITE) && 3055bae473a4SKirill A. Shutemov !mm_forbids_zeropage(vma->vm_mm)) { 305682b0f8c3SJan Kara entry = pte_mkspecial(pfn_pte(my_zero_pfn(vmf->address), 305762eede62SHugh Dickins vma->vm_page_prot)); 305882b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 305982b0f8c3SJan Kara vmf->address, &vmf->ptl); 306082b0f8c3SJan Kara if (!pte_none(*vmf->pte)) 3061a13ea5b7SHugh Dickins goto unlock; 30626b31d595SMichal Hocko ret = check_stable_address_space(vma->vm_mm); 30636b31d595SMichal Hocko if (ret) 30646b31d595SMichal Hocko goto unlock; 30656b251fc9SAndrea Arcangeli /* Deliver the page fault to userland, check inside PT lock */ 30666b251fc9SAndrea Arcangeli if (userfaultfd_missing(vma)) { 306782b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 306882b0f8c3SJan Kara return handle_userfault(vmf, VM_UFFD_MISSING); 30696b251fc9SAndrea Arcangeli } 3070a13ea5b7SHugh Dickins goto setpte; 3071a13ea5b7SHugh Dickins } 3072a13ea5b7SHugh Dickins 30731da177e4SLinus Torvalds /* Allocate our own private page. */ 30741da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 307565500d23SHugh Dickins goto oom; 307682b0f8c3SJan Kara page = alloc_zeroed_user_highpage_movable(vma, vmf->address); 30771da177e4SLinus Torvalds if (!page) 307865500d23SHugh Dickins goto oom; 3079eb3c24f3SMel Gorman 30802cf85583STejun Heo if (mem_cgroup_try_charge_delay(page, vma->vm_mm, GFP_KERNEL, &memcg, 30812cf85583STejun Heo false)) 3082eb3c24f3SMel Gorman goto oom_free_page; 3083eb3c24f3SMel Gorman 308452f37629SMinchan Kim /* 308552f37629SMinchan Kim * The memory barrier inside __SetPageUptodate makes sure that 308652f37629SMinchan Kim * preceeding stores to the page contents become visible before 308752f37629SMinchan Kim * the set_pte_at() write. 308852f37629SMinchan Kim */ 30890ed361deSNick Piggin __SetPageUptodate(page); 30901da177e4SLinus Torvalds 309165500d23SHugh Dickins entry = mk_pte(page, vma->vm_page_prot); 30921ac0cb5dSHugh Dickins if (vma->vm_flags & VM_WRITE) 30931ac0cb5dSHugh Dickins entry = pte_mkwrite(pte_mkdirty(entry)); 30948f4e2101SHugh Dickins 309582b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 309682b0f8c3SJan Kara &vmf->ptl); 309782b0f8c3SJan Kara if (!pte_none(*vmf->pte)) 30988f4e2101SHugh Dickins goto release; 30999ba69294SHugh Dickins 31006b31d595SMichal Hocko ret = check_stable_address_space(vma->vm_mm); 31016b31d595SMichal Hocko if (ret) 31026b31d595SMichal Hocko goto release; 31036b31d595SMichal Hocko 31046b251fc9SAndrea Arcangeli /* Deliver the page fault to userland, check inside PT lock */ 31056b251fc9SAndrea Arcangeli if (userfaultfd_missing(vma)) { 310682b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3107f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 310809cbfeafSKirill A. Shutemov put_page(page); 310982b0f8c3SJan Kara return handle_userfault(vmf, VM_UFFD_MISSING); 31106b251fc9SAndrea Arcangeli } 31116b251fc9SAndrea Arcangeli 3112bae473a4SKirill A. Shutemov inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); 311382b0f8c3SJan Kara page_add_new_anon_rmap(page, vma, vmf->address, false); 3114f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, false); 311500501b53SJohannes Weiner lru_cache_add_active_or_unevictable(page, vma); 3116a13ea5b7SHugh Dickins setpte: 311782b0f8c3SJan Kara set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry); 31181da177e4SLinus Torvalds 31191da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 312082b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 312165500d23SHugh Dickins unlock: 312282b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 31236b31d595SMichal Hocko return ret; 31248f4e2101SHugh Dickins release: 3125f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 312609cbfeafSKirill A. Shutemov put_page(page); 31278f4e2101SHugh Dickins goto unlock; 31288a9f3ccdSBalbir Singh oom_free_page: 312909cbfeafSKirill A. Shutemov put_page(page); 313065500d23SHugh Dickins oom: 31311da177e4SLinus Torvalds return VM_FAULT_OOM; 31321da177e4SLinus Torvalds } 31331da177e4SLinus Torvalds 31349a95f3cfSPaul Cassella /* 31359a95f3cfSPaul Cassella * The mmap_sem must have been held on entry, and may have been 31369a95f3cfSPaul Cassella * released depending on flags and vma->vm_ops->fault() return value. 31379a95f3cfSPaul Cassella * See filemap_fault() and __lock_page_retry(). 31389a95f3cfSPaul Cassella */ 31392b740303SSouptick Joarder static vm_fault_t __do_fault(struct vm_fault *vmf) 31407eae74afSKirill A. Shutemov { 314182b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 31422b740303SSouptick Joarder vm_fault_t ret; 31437eae74afSKirill A. Shutemov 314463f3655fSMichal Hocko /* 314563f3655fSMichal Hocko * Preallocate pte before we take page_lock because this might lead to 314663f3655fSMichal Hocko * deadlocks for memcg reclaim which waits for pages under writeback: 314763f3655fSMichal Hocko * lock_page(A) 314863f3655fSMichal Hocko * SetPageWriteback(A) 314963f3655fSMichal Hocko * unlock_page(A) 315063f3655fSMichal Hocko * lock_page(B) 315163f3655fSMichal Hocko * lock_page(B) 315263f3655fSMichal Hocko * pte_alloc_pne 315363f3655fSMichal Hocko * shrink_page_list 315463f3655fSMichal Hocko * wait_on_page_writeback(A) 315563f3655fSMichal Hocko * SetPageWriteback(B) 315663f3655fSMichal Hocko * unlock_page(B) 315763f3655fSMichal Hocko * # flush A, B to clear the writeback 315863f3655fSMichal Hocko */ 315963f3655fSMichal Hocko if (pmd_none(*vmf->pmd) && !vmf->prealloc_pte) { 316063f3655fSMichal Hocko vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm); 316163f3655fSMichal Hocko if (!vmf->prealloc_pte) 316263f3655fSMichal Hocko return VM_FAULT_OOM; 316363f3655fSMichal Hocko smp_wmb(); /* See comment in __pte_alloc() */ 316463f3655fSMichal Hocko } 316563f3655fSMichal Hocko 316611bac800SDave Jiang ret = vma->vm_ops->fault(vmf); 31673917048dSJan Kara if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY | 3168b1aa812bSJan Kara VM_FAULT_DONE_COW))) 31697eae74afSKirill A. Shutemov return ret; 31707eae74afSKirill A. Shutemov 3171667240e0SJan Kara if (unlikely(PageHWPoison(vmf->page))) { 31727eae74afSKirill A. Shutemov if (ret & VM_FAULT_LOCKED) 3173667240e0SJan Kara unlock_page(vmf->page); 3174667240e0SJan Kara put_page(vmf->page); 3175936ca80dSJan Kara vmf->page = NULL; 31767eae74afSKirill A. Shutemov return VM_FAULT_HWPOISON; 31777eae74afSKirill A. Shutemov } 31787eae74afSKirill A. Shutemov 31797eae74afSKirill A. Shutemov if (unlikely(!(ret & VM_FAULT_LOCKED))) 3180667240e0SJan Kara lock_page(vmf->page); 31817eae74afSKirill A. Shutemov else 3182667240e0SJan Kara VM_BUG_ON_PAGE(!PageLocked(vmf->page), vmf->page); 31837eae74afSKirill A. Shutemov 31847eae74afSKirill A. Shutemov return ret; 31857eae74afSKirill A. Shutemov } 31867eae74afSKirill A. Shutemov 3187d0f0931dSRoss Zwisler /* 3188d0f0931dSRoss Zwisler * The ordering of these checks is important for pmds with _PAGE_DEVMAP set. 3189d0f0931dSRoss Zwisler * If we check pmd_trans_unstable() first we will trip the bad_pmd() check 3190d0f0931dSRoss Zwisler * inside of pmd_none_or_trans_huge_or_clear_bad(). This will end up correctly 3191d0f0931dSRoss Zwisler * returning 1 but not before it spams dmesg with the pmd_clear_bad() output. 3192d0f0931dSRoss Zwisler */ 3193d0f0931dSRoss Zwisler static int pmd_devmap_trans_unstable(pmd_t *pmd) 3194d0f0931dSRoss Zwisler { 3195d0f0931dSRoss Zwisler return pmd_devmap(*pmd) || pmd_trans_unstable(pmd); 3196d0f0931dSRoss Zwisler } 3197d0f0931dSRoss Zwisler 31982b740303SSouptick Joarder static vm_fault_t pte_alloc_one_map(struct vm_fault *vmf) 31997267ec00SKirill A. Shutemov { 320082b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 32017267ec00SKirill A. Shutemov 320282b0f8c3SJan Kara if (!pmd_none(*vmf->pmd)) 32037267ec00SKirill A. Shutemov goto map_pte; 320482b0f8c3SJan Kara if (vmf->prealloc_pte) { 320582b0f8c3SJan Kara vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); 320682b0f8c3SJan Kara if (unlikely(!pmd_none(*vmf->pmd))) { 320782b0f8c3SJan Kara spin_unlock(vmf->ptl); 32087267ec00SKirill A. Shutemov goto map_pte; 32097267ec00SKirill A. Shutemov } 32107267ec00SKirill A. Shutemov 3211c4812909SKirill A. Shutemov mm_inc_nr_ptes(vma->vm_mm); 321282b0f8c3SJan Kara pmd_populate(vma->vm_mm, vmf->pmd, vmf->prealloc_pte); 321382b0f8c3SJan Kara spin_unlock(vmf->ptl); 32147f2b6ce8STobin C Harding vmf->prealloc_pte = NULL; 32154cf58924SJoel Fernandes (Google) } else if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd))) { 32167267ec00SKirill A. Shutemov return VM_FAULT_OOM; 32177267ec00SKirill A. Shutemov } 32187267ec00SKirill A. Shutemov map_pte: 32197267ec00SKirill A. Shutemov /* 32207267ec00SKirill A. Shutemov * If a huge pmd materialized under us just retry later. Use 3221d0f0931dSRoss Zwisler * pmd_trans_unstable() via pmd_devmap_trans_unstable() instead of 3222d0f0931dSRoss Zwisler * pmd_trans_huge() to ensure the pmd didn't become pmd_trans_huge 3223d0f0931dSRoss Zwisler * under us and then back to pmd_none, as a result of MADV_DONTNEED 3224d0f0931dSRoss Zwisler * running immediately after a huge pmd fault in a different thread of 3225d0f0931dSRoss Zwisler * this mm, in turn leading to a misleading pmd_trans_huge() retval. 3226d0f0931dSRoss Zwisler * All we have to ensure is that it is a regular pmd that we can walk 3227d0f0931dSRoss Zwisler * with pte_offset_map() and we can do that through an atomic read in 3228d0f0931dSRoss Zwisler * C, which is what pmd_trans_unstable() provides. 32297267ec00SKirill A. Shutemov */ 3230d0f0931dSRoss Zwisler if (pmd_devmap_trans_unstable(vmf->pmd)) 32317267ec00SKirill A. Shutemov return VM_FAULT_NOPAGE; 32327267ec00SKirill A. Shutemov 3233d0f0931dSRoss Zwisler /* 3234d0f0931dSRoss Zwisler * At this point we know that our vmf->pmd points to a page of ptes 3235d0f0931dSRoss Zwisler * and it cannot become pmd_none(), pmd_devmap() or pmd_trans_huge() 3236d0f0931dSRoss Zwisler * for the duration of the fault. If a racing MADV_DONTNEED runs and 3237d0f0931dSRoss Zwisler * we zap the ptes pointed to by our vmf->pmd, the vmf->ptl will still 3238d0f0931dSRoss Zwisler * be valid and we will re-check to make sure the vmf->pte isn't 3239d0f0931dSRoss Zwisler * pte_none() under vmf->ptl protection when we return to 3240d0f0931dSRoss Zwisler * alloc_set_pte(). 3241d0f0931dSRoss Zwisler */ 324282b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 324382b0f8c3SJan Kara &vmf->ptl); 32447267ec00SKirill A. Shutemov return 0; 32457267ec00SKirill A. Shutemov } 32467267ec00SKirill A. Shutemov 3247e496cf3dSKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 324882b0f8c3SJan Kara static void deposit_prealloc_pte(struct vm_fault *vmf) 3249953c66c2SAneesh Kumar K.V { 325082b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3251953c66c2SAneesh Kumar K.V 325282b0f8c3SJan Kara pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, vmf->prealloc_pte); 3253953c66c2SAneesh Kumar K.V /* 3254953c66c2SAneesh Kumar K.V * We are going to consume the prealloc table, 3255953c66c2SAneesh Kumar K.V * count that as nr_ptes. 3256953c66c2SAneesh Kumar K.V */ 3257c4812909SKirill A. Shutemov mm_inc_nr_ptes(vma->vm_mm); 32587f2b6ce8STobin C Harding vmf->prealloc_pte = NULL; 3259953c66c2SAneesh Kumar K.V } 3260953c66c2SAneesh Kumar K.V 32612b740303SSouptick Joarder static vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page) 326210102459SKirill A. Shutemov { 326382b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 326482b0f8c3SJan Kara bool write = vmf->flags & FAULT_FLAG_WRITE; 326582b0f8c3SJan Kara unsigned long haddr = vmf->address & HPAGE_PMD_MASK; 326610102459SKirill A. Shutemov pmd_t entry; 32672b740303SSouptick Joarder int i; 32682b740303SSouptick Joarder vm_fault_t ret; 326910102459SKirill A. Shutemov 327010102459SKirill A. Shutemov if (!transhuge_vma_suitable(vma, haddr)) 327110102459SKirill A. Shutemov return VM_FAULT_FALLBACK; 327210102459SKirill A. Shutemov 327310102459SKirill A. Shutemov ret = VM_FAULT_FALLBACK; 327410102459SKirill A. Shutemov page = compound_head(page); 327510102459SKirill A. Shutemov 3276953c66c2SAneesh Kumar K.V /* 3277953c66c2SAneesh Kumar K.V * Archs like ppc64 need additonal space to store information 3278953c66c2SAneesh Kumar K.V * related to pte entry. Use the preallocated table for that. 3279953c66c2SAneesh Kumar K.V */ 328082b0f8c3SJan Kara if (arch_needs_pgtable_deposit() && !vmf->prealloc_pte) { 32814cf58924SJoel Fernandes (Google) vmf->prealloc_pte = pte_alloc_one(vma->vm_mm); 328282b0f8c3SJan Kara if (!vmf->prealloc_pte) 3283953c66c2SAneesh Kumar K.V return VM_FAULT_OOM; 3284953c66c2SAneesh Kumar K.V smp_wmb(); /* See comment in __pte_alloc() */ 3285953c66c2SAneesh Kumar K.V } 3286953c66c2SAneesh Kumar K.V 328782b0f8c3SJan Kara vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); 328882b0f8c3SJan Kara if (unlikely(!pmd_none(*vmf->pmd))) 328910102459SKirill A. Shutemov goto out; 329010102459SKirill A. Shutemov 329110102459SKirill A. Shutemov for (i = 0; i < HPAGE_PMD_NR; i++) 329210102459SKirill A. Shutemov flush_icache_page(vma, page + i); 329310102459SKirill A. Shutemov 329410102459SKirill A. Shutemov entry = mk_huge_pmd(page, vma->vm_page_prot); 329510102459SKirill A. Shutemov if (write) 3296f55e1014SLinus Torvalds entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); 329710102459SKirill A. Shutemov 3298fadae295SYang Shi add_mm_counter(vma->vm_mm, mm_counter_file(page), HPAGE_PMD_NR); 329910102459SKirill A. Shutemov page_add_file_rmap(page, true); 3300953c66c2SAneesh Kumar K.V /* 3301953c66c2SAneesh Kumar K.V * deposit and withdraw with pmd lock held 3302953c66c2SAneesh Kumar K.V */ 3303953c66c2SAneesh Kumar K.V if (arch_needs_pgtable_deposit()) 330482b0f8c3SJan Kara deposit_prealloc_pte(vmf); 330510102459SKirill A. Shutemov 330682b0f8c3SJan Kara set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry); 330710102459SKirill A. Shutemov 330882b0f8c3SJan Kara update_mmu_cache_pmd(vma, haddr, vmf->pmd); 330910102459SKirill A. Shutemov 331010102459SKirill A. Shutemov /* fault is handled */ 331110102459SKirill A. Shutemov ret = 0; 331295ecedcdSKirill A. Shutemov count_vm_event(THP_FILE_MAPPED); 331310102459SKirill A. Shutemov out: 331482b0f8c3SJan Kara spin_unlock(vmf->ptl); 331510102459SKirill A. Shutemov return ret; 331610102459SKirill A. Shutemov } 331710102459SKirill A. Shutemov #else 33182b740303SSouptick Joarder static vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page) 331910102459SKirill A. Shutemov { 332010102459SKirill A. Shutemov BUILD_BUG(); 332110102459SKirill A. Shutemov return 0; 332210102459SKirill A. Shutemov } 332310102459SKirill A. Shutemov #endif 332410102459SKirill A. Shutemov 33258c6e50b0SKirill A. Shutemov /** 33267267ec00SKirill A. Shutemov * alloc_set_pte - setup new PTE entry for given page and add reverse page 33277267ec00SKirill A. Shutemov * mapping. If needed, the fucntion allocates page table or use pre-allocated. 33288c6e50b0SKirill A. Shutemov * 332982b0f8c3SJan Kara * @vmf: fault environment 33307267ec00SKirill A. Shutemov * @memcg: memcg to charge page (only for private mappings) 33318c6e50b0SKirill A. Shutemov * @page: page to map 33328c6e50b0SKirill A. Shutemov * 333382b0f8c3SJan Kara * Caller must take care of unlocking vmf->ptl, if vmf->pte is non-NULL on 333482b0f8c3SJan Kara * return. 33358c6e50b0SKirill A. Shutemov * 33368c6e50b0SKirill A. Shutemov * Target users are page handler itself and implementations of 33378c6e50b0SKirill A. Shutemov * vm_ops->map_pages. 3338a862f68aSMike Rapoport * 3339a862f68aSMike Rapoport * Return: %0 on success, %VM_FAULT_ code in case of error. 33408c6e50b0SKirill A. Shutemov */ 33412b740303SSouptick Joarder vm_fault_t alloc_set_pte(struct vm_fault *vmf, struct mem_cgroup *memcg, 33427267ec00SKirill A. Shutemov struct page *page) 33433bb97794SKirill A. Shutemov { 334482b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 334582b0f8c3SJan Kara bool write = vmf->flags & FAULT_FLAG_WRITE; 33463bb97794SKirill A. Shutemov pte_t entry; 33472b740303SSouptick Joarder vm_fault_t ret; 334810102459SKirill A. Shutemov 334982b0f8c3SJan Kara if (pmd_none(*vmf->pmd) && PageTransCompound(page) && 3350e496cf3dSKirill A. Shutemov IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) { 335110102459SKirill A. Shutemov /* THP on COW? */ 335210102459SKirill A. Shutemov VM_BUG_ON_PAGE(memcg, page); 335310102459SKirill A. Shutemov 335482b0f8c3SJan Kara ret = do_set_pmd(vmf, page); 335510102459SKirill A. Shutemov if (ret != VM_FAULT_FALLBACK) 3356b0b9b3dfSHugh Dickins return ret; 335710102459SKirill A. Shutemov } 33583bb97794SKirill A. Shutemov 335982b0f8c3SJan Kara if (!vmf->pte) { 336082b0f8c3SJan Kara ret = pte_alloc_one_map(vmf); 33617267ec00SKirill A. Shutemov if (ret) 3362b0b9b3dfSHugh Dickins return ret; 33637267ec00SKirill A. Shutemov } 33647267ec00SKirill A. Shutemov 33657267ec00SKirill A. Shutemov /* Re-check under ptl */ 3366b0b9b3dfSHugh Dickins if (unlikely(!pte_none(*vmf->pte))) 3367b0b9b3dfSHugh Dickins return VM_FAULT_NOPAGE; 33687267ec00SKirill A. Shutemov 33693bb97794SKirill A. Shutemov flush_icache_page(vma, page); 33703bb97794SKirill A. Shutemov entry = mk_pte(page, vma->vm_page_prot); 33713bb97794SKirill A. Shutemov if (write) 33723bb97794SKirill A. Shutemov entry = maybe_mkwrite(pte_mkdirty(entry), vma); 3373bae473a4SKirill A. Shutemov /* copy-on-write page */ 3374bae473a4SKirill A. Shutemov if (write && !(vma->vm_flags & VM_SHARED)) { 33753bb97794SKirill A. Shutemov inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); 337682b0f8c3SJan Kara page_add_new_anon_rmap(page, vma, vmf->address, false); 33777267ec00SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, false); 33787267ec00SKirill A. Shutemov lru_cache_add_active_or_unevictable(page, vma); 33793bb97794SKirill A. Shutemov } else { 3380eca56ff9SJerome Marchand inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page)); 3381dd78feddSKirill A. Shutemov page_add_file_rmap(page, false); 33823bb97794SKirill A. Shutemov } 338382b0f8c3SJan Kara set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry); 33843bb97794SKirill A. Shutemov 33853bb97794SKirill A. Shutemov /* no need to invalidate: a not-present page won't be cached */ 338682b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 33877267ec00SKirill A. Shutemov 3388b0b9b3dfSHugh Dickins return 0; 33893bb97794SKirill A. Shutemov } 33903bb97794SKirill A. Shutemov 33919118c0cbSJan Kara 33929118c0cbSJan Kara /** 33939118c0cbSJan Kara * finish_fault - finish page fault once we have prepared the page to fault 33949118c0cbSJan Kara * 33959118c0cbSJan Kara * @vmf: structure describing the fault 33969118c0cbSJan Kara * 33979118c0cbSJan Kara * This function handles all that is needed to finish a page fault once the 33989118c0cbSJan Kara * page to fault in is prepared. It handles locking of PTEs, inserts PTE for 33999118c0cbSJan Kara * given page, adds reverse page mapping, handles memcg charges and LRU 3400a862f68aSMike Rapoport * addition. 34019118c0cbSJan Kara * 34029118c0cbSJan Kara * The function expects the page to be locked and on success it consumes a 34039118c0cbSJan Kara * reference of a page being mapped (for the PTE which maps it). 3404a862f68aSMike Rapoport * 3405a862f68aSMike Rapoport * Return: %0 on success, %VM_FAULT_ code in case of error. 34069118c0cbSJan Kara */ 34072b740303SSouptick Joarder vm_fault_t finish_fault(struct vm_fault *vmf) 34089118c0cbSJan Kara { 34099118c0cbSJan Kara struct page *page; 34102b740303SSouptick Joarder vm_fault_t ret = 0; 34119118c0cbSJan Kara 34129118c0cbSJan Kara /* Did we COW the page? */ 34139118c0cbSJan Kara if ((vmf->flags & FAULT_FLAG_WRITE) && 34149118c0cbSJan Kara !(vmf->vma->vm_flags & VM_SHARED)) 34159118c0cbSJan Kara page = vmf->cow_page; 34169118c0cbSJan Kara else 34179118c0cbSJan Kara page = vmf->page; 34186b31d595SMichal Hocko 34196b31d595SMichal Hocko /* 34206b31d595SMichal Hocko * check even for read faults because we might have lost our CoWed 34216b31d595SMichal Hocko * page 34226b31d595SMichal Hocko */ 34236b31d595SMichal Hocko if (!(vmf->vma->vm_flags & VM_SHARED)) 34246b31d595SMichal Hocko ret = check_stable_address_space(vmf->vma->vm_mm); 34256b31d595SMichal Hocko if (!ret) 34269118c0cbSJan Kara ret = alloc_set_pte(vmf, vmf->memcg, page); 34279118c0cbSJan Kara if (vmf->pte) 34289118c0cbSJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 34299118c0cbSJan Kara return ret; 34309118c0cbSJan Kara } 34319118c0cbSJan Kara 34323a91053aSKirill A. Shutemov static unsigned long fault_around_bytes __read_mostly = 34333a91053aSKirill A. Shutemov rounddown_pow_of_two(65536); 3434a9b0f861SKirill A. Shutemov 34351592eef0SKirill A. Shutemov #ifdef CONFIG_DEBUG_FS 3436a9b0f861SKirill A. Shutemov static int fault_around_bytes_get(void *data, u64 *val) 34371592eef0SKirill A. Shutemov { 3438a9b0f861SKirill A. Shutemov *val = fault_around_bytes; 34391592eef0SKirill A. Shutemov return 0; 34401592eef0SKirill A. Shutemov } 34411592eef0SKirill A. Shutemov 3442b4903d6eSAndrey Ryabinin /* 3443da391d64SWilliam Kucharski * fault_around_bytes must be rounded down to the nearest page order as it's 3444da391d64SWilliam Kucharski * what do_fault_around() expects to see. 3445b4903d6eSAndrey Ryabinin */ 3446a9b0f861SKirill A. Shutemov static int fault_around_bytes_set(void *data, u64 val) 34471592eef0SKirill A. Shutemov { 3448a9b0f861SKirill A. Shutemov if (val / PAGE_SIZE > PTRS_PER_PTE) 34491592eef0SKirill A. Shutemov return -EINVAL; 3450b4903d6eSAndrey Ryabinin if (val > PAGE_SIZE) 3451b4903d6eSAndrey Ryabinin fault_around_bytes = rounddown_pow_of_two(val); 3452b4903d6eSAndrey Ryabinin else 3453b4903d6eSAndrey Ryabinin fault_around_bytes = PAGE_SIZE; /* rounddown_pow_of_two(0) is undefined */ 34541592eef0SKirill A. Shutemov return 0; 34551592eef0SKirill A. Shutemov } 34560a1345f8SYevgen Pronenko DEFINE_DEBUGFS_ATTRIBUTE(fault_around_bytes_fops, 3457a9b0f861SKirill A. Shutemov fault_around_bytes_get, fault_around_bytes_set, "%llu\n"); 34581592eef0SKirill A. Shutemov 34591592eef0SKirill A. Shutemov static int __init fault_around_debugfs(void) 34601592eef0SKirill A. Shutemov { 3461d9f7979cSGreg Kroah-Hartman debugfs_create_file_unsafe("fault_around_bytes", 0644, NULL, NULL, 3462a9b0f861SKirill A. Shutemov &fault_around_bytes_fops); 34631592eef0SKirill A. Shutemov return 0; 34641592eef0SKirill A. Shutemov } 34651592eef0SKirill A. Shutemov late_initcall(fault_around_debugfs); 34661592eef0SKirill A. Shutemov #endif 34678c6e50b0SKirill A. Shutemov 34681fdb412bSKirill A. Shutemov /* 34691fdb412bSKirill A. Shutemov * do_fault_around() tries to map few pages around the fault address. The hope 34701fdb412bSKirill A. Shutemov * is that the pages will be needed soon and this will lower the number of 34711fdb412bSKirill A. Shutemov * faults to handle. 34721fdb412bSKirill A. Shutemov * 34731fdb412bSKirill A. Shutemov * It uses vm_ops->map_pages() to map the pages, which skips the page if it's 34741fdb412bSKirill A. Shutemov * not ready to be mapped: not up-to-date, locked, etc. 34751fdb412bSKirill A. Shutemov * 34761fdb412bSKirill A. Shutemov * This function is called with the page table lock taken. In the split ptlock 34771fdb412bSKirill A. Shutemov * case the page table lock only protects only those entries which belong to 34781fdb412bSKirill A. Shutemov * the page table corresponding to the fault address. 34791fdb412bSKirill A. Shutemov * 34801fdb412bSKirill A. Shutemov * This function doesn't cross the VMA boundaries, in order to call map_pages() 34811fdb412bSKirill A. Shutemov * only once. 34821fdb412bSKirill A. Shutemov * 3483da391d64SWilliam Kucharski * fault_around_bytes defines how many bytes we'll try to map. 3484da391d64SWilliam Kucharski * do_fault_around() expects it to be set to a power of two less than or equal 3485da391d64SWilliam Kucharski * to PTRS_PER_PTE. 34861fdb412bSKirill A. Shutemov * 3487da391d64SWilliam Kucharski * The virtual address of the area that we map is naturally aligned to 3488da391d64SWilliam Kucharski * fault_around_bytes rounded down to the machine page size 3489da391d64SWilliam Kucharski * (and therefore to page order). This way it's easier to guarantee 3490da391d64SWilliam Kucharski * that we don't cross page table boundaries. 34911fdb412bSKirill A. Shutemov */ 34922b740303SSouptick Joarder static vm_fault_t do_fault_around(struct vm_fault *vmf) 34938c6e50b0SKirill A. Shutemov { 349482b0f8c3SJan Kara unsigned long address = vmf->address, nr_pages, mask; 34950721ec8bSJan Kara pgoff_t start_pgoff = vmf->pgoff; 3496bae473a4SKirill A. Shutemov pgoff_t end_pgoff; 34972b740303SSouptick Joarder int off; 34982b740303SSouptick Joarder vm_fault_t ret = 0; 34998c6e50b0SKirill A. Shutemov 35004db0c3c2SJason Low nr_pages = READ_ONCE(fault_around_bytes) >> PAGE_SHIFT; 3501aecd6f44SKirill A. Shutemov mask = ~(nr_pages * PAGE_SIZE - 1) & PAGE_MASK; 3502aecd6f44SKirill A. Shutemov 350382b0f8c3SJan Kara vmf->address = max(address & mask, vmf->vma->vm_start); 350482b0f8c3SJan Kara off = ((address - vmf->address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1); 3505bae473a4SKirill A. Shutemov start_pgoff -= off; 35068c6e50b0SKirill A. Shutemov 35078c6e50b0SKirill A. Shutemov /* 3508da391d64SWilliam Kucharski * end_pgoff is either the end of the page table, the end of 3509da391d64SWilliam Kucharski * the vma or nr_pages from start_pgoff, depending what is nearest. 35108c6e50b0SKirill A. Shutemov */ 3511bae473a4SKirill A. Shutemov end_pgoff = start_pgoff - 351282b0f8c3SJan Kara ((vmf->address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) + 35138c6e50b0SKirill A. Shutemov PTRS_PER_PTE - 1; 351482b0f8c3SJan Kara end_pgoff = min3(end_pgoff, vma_pages(vmf->vma) + vmf->vma->vm_pgoff - 1, 3515bae473a4SKirill A. Shutemov start_pgoff + nr_pages - 1); 35168c6e50b0SKirill A. Shutemov 351782b0f8c3SJan Kara if (pmd_none(*vmf->pmd)) { 35184cf58924SJoel Fernandes (Google) vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm); 351982b0f8c3SJan Kara if (!vmf->prealloc_pte) 3520c5f88bd2SVegard Nossum goto out; 35217267ec00SKirill A. Shutemov smp_wmb(); /* See comment in __pte_alloc() */ 35228c6e50b0SKirill A. Shutemov } 35238c6e50b0SKirill A. Shutemov 352482b0f8c3SJan Kara vmf->vma->vm_ops->map_pages(vmf, start_pgoff, end_pgoff); 35257267ec00SKirill A. Shutemov 35267267ec00SKirill A. Shutemov /* Huge page is mapped? Page fault is solved */ 352782b0f8c3SJan Kara if (pmd_trans_huge(*vmf->pmd)) { 35287267ec00SKirill A. Shutemov ret = VM_FAULT_NOPAGE; 35297267ec00SKirill A. Shutemov goto out; 35308c6e50b0SKirill A. Shutemov } 35318c6e50b0SKirill A. Shutemov 35327267ec00SKirill A. Shutemov /* ->map_pages() haven't done anything useful. Cold page cache? */ 353382b0f8c3SJan Kara if (!vmf->pte) 35347267ec00SKirill A. Shutemov goto out; 35357267ec00SKirill A. Shutemov 35367267ec00SKirill A. Shutemov /* check if the page fault is solved */ 353782b0f8c3SJan Kara vmf->pte -= (vmf->address >> PAGE_SHIFT) - (address >> PAGE_SHIFT); 353882b0f8c3SJan Kara if (!pte_none(*vmf->pte)) 35397267ec00SKirill A. Shutemov ret = VM_FAULT_NOPAGE; 354082b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 35417267ec00SKirill A. Shutemov out: 354282b0f8c3SJan Kara vmf->address = address; 354382b0f8c3SJan Kara vmf->pte = NULL; 35447267ec00SKirill A. Shutemov return ret; 35457267ec00SKirill A. Shutemov } 35467267ec00SKirill A. Shutemov 35472b740303SSouptick Joarder static vm_fault_t do_read_fault(struct vm_fault *vmf) 3548e655fb29SKirill A. Shutemov { 354982b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 35502b740303SSouptick Joarder vm_fault_t ret = 0; 35518c6e50b0SKirill A. Shutemov 35528c6e50b0SKirill A. Shutemov /* 35538c6e50b0SKirill A. Shutemov * Let's call ->map_pages() first and use ->fault() as fallback 35548c6e50b0SKirill A. Shutemov * if page by the offset is not ready to be mapped (cold cache or 35558c6e50b0SKirill A. Shutemov * something). 35568c6e50b0SKirill A. Shutemov */ 35579b4bdd2fSKirill A. Shutemov if (vma->vm_ops->map_pages && fault_around_bytes >> PAGE_SHIFT > 1) { 35580721ec8bSJan Kara ret = do_fault_around(vmf); 35597267ec00SKirill A. Shutemov if (ret) 35607267ec00SKirill A. Shutemov return ret; 35618c6e50b0SKirill A. Shutemov } 3562e655fb29SKirill A. Shutemov 3563936ca80dSJan Kara ret = __do_fault(vmf); 3564e655fb29SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3565e655fb29SKirill A. Shutemov return ret; 3566e655fb29SKirill A. Shutemov 35679118c0cbSJan Kara ret |= finish_fault(vmf); 3568936ca80dSJan Kara unlock_page(vmf->page); 35697267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3570936ca80dSJan Kara put_page(vmf->page); 3571e655fb29SKirill A. Shutemov return ret; 3572e655fb29SKirill A. Shutemov } 3573e655fb29SKirill A. Shutemov 35742b740303SSouptick Joarder static vm_fault_t do_cow_fault(struct vm_fault *vmf) 3575ec47c3b9SKirill A. Shutemov { 357682b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 35772b740303SSouptick Joarder vm_fault_t ret; 3578ec47c3b9SKirill A. Shutemov 3579ec47c3b9SKirill A. Shutemov if (unlikely(anon_vma_prepare(vma))) 3580ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 3581ec47c3b9SKirill A. Shutemov 3582936ca80dSJan Kara vmf->cow_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vmf->address); 3583936ca80dSJan Kara if (!vmf->cow_page) 3584ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 3585ec47c3b9SKirill A. Shutemov 35862cf85583STejun Heo if (mem_cgroup_try_charge_delay(vmf->cow_page, vma->vm_mm, GFP_KERNEL, 35873917048dSJan Kara &vmf->memcg, false)) { 3588936ca80dSJan Kara put_page(vmf->cow_page); 3589ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 3590ec47c3b9SKirill A. Shutemov } 3591ec47c3b9SKirill A. Shutemov 3592936ca80dSJan Kara ret = __do_fault(vmf); 3593ec47c3b9SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3594ec47c3b9SKirill A. Shutemov goto uncharge_out; 35953917048dSJan Kara if (ret & VM_FAULT_DONE_COW) 35963917048dSJan Kara return ret; 3597ec47c3b9SKirill A. Shutemov 3598936ca80dSJan Kara copy_user_highpage(vmf->cow_page, vmf->page, vmf->address, vma); 3599936ca80dSJan Kara __SetPageUptodate(vmf->cow_page); 3600ec47c3b9SKirill A. Shutemov 36019118c0cbSJan Kara ret |= finish_fault(vmf); 3602936ca80dSJan Kara unlock_page(vmf->page); 3603936ca80dSJan Kara put_page(vmf->page); 36047267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 36057267ec00SKirill A. Shutemov goto uncharge_out; 3606ec47c3b9SKirill A. Shutemov return ret; 3607ec47c3b9SKirill A. Shutemov uncharge_out: 36083917048dSJan Kara mem_cgroup_cancel_charge(vmf->cow_page, vmf->memcg, false); 3609936ca80dSJan Kara put_page(vmf->cow_page); 3610ec47c3b9SKirill A. Shutemov return ret; 3611ec47c3b9SKirill A. Shutemov } 3612ec47c3b9SKirill A. Shutemov 36132b740303SSouptick Joarder static vm_fault_t do_shared_fault(struct vm_fault *vmf) 36141da177e4SLinus Torvalds { 361582b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 36162b740303SSouptick Joarder vm_fault_t ret, tmp; 36171d65f86dSKAMEZAWA Hiroyuki 3618936ca80dSJan Kara ret = __do_fault(vmf); 36197eae74afSKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3620f0c6d4d2SKirill A. Shutemov return ret; 36211da177e4SLinus Torvalds 36221da177e4SLinus Torvalds /* 3623f0c6d4d2SKirill A. Shutemov * Check if the backing address space wants to know that the page is 3624f0c6d4d2SKirill A. Shutemov * about to become writable 36251da177e4SLinus Torvalds */ 3626fb09a464SKirill A. Shutemov if (vma->vm_ops->page_mkwrite) { 3627936ca80dSJan Kara unlock_page(vmf->page); 362838b8cb7fSJan Kara tmp = do_page_mkwrite(vmf); 3629fb09a464SKirill A. Shutemov if (unlikely(!tmp || 3630fb09a464SKirill A. Shutemov (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { 3631936ca80dSJan Kara put_page(vmf->page); 3632f0c6d4d2SKirill A. Shutemov return tmp; 363369676147SMark Fasheh } 3634d0217ac0SNick Piggin } 3635fb09a464SKirill A. Shutemov 36369118c0cbSJan Kara ret |= finish_fault(vmf); 36377267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | 36387267ec00SKirill A. Shutemov VM_FAULT_RETRY))) { 3639936ca80dSJan Kara unlock_page(vmf->page); 3640936ca80dSJan Kara put_page(vmf->page); 3641f0c6d4d2SKirill A. Shutemov return ret; 36429637a5efSDavid Howells } 3643d00806b1SNick Piggin 364497ba0c2bSJan Kara fault_dirty_shared_page(vma, vmf->page); 3645b827e496SNick Piggin return ret; 364654cb8821SNick Piggin } 3647d00806b1SNick Piggin 36489a95f3cfSPaul Cassella /* 36499a95f3cfSPaul Cassella * We enter with non-exclusive mmap_sem (to exclude vma changes, 36509a95f3cfSPaul Cassella * but allow concurrent faults). 36519a95f3cfSPaul Cassella * The mmap_sem may have been released depending on flags and our 36529a95f3cfSPaul Cassella * return value. See filemap_fault() and __lock_page_or_retry(). 3653fc8efd2dSJan Stancek * If mmap_sem is released, vma may become invalid (for example 3654fc8efd2dSJan Stancek * by other thread calling munmap()). 36559a95f3cfSPaul Cassella */ 36562b740303SSouptick Joarder static vm_fault_t do_fault(struct vm_fault *vmf) 365754cb8821SNick Piggin { 365882b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3659fc8efd2dSJan Stancek struct mm_struct *vm_mm = vma->vm_mm; 36602b740303SSouptick Joarder vm_fault_t ret; 366154cb8821SNick Piggin 3662ff09d7ecSAneesh Kumar K.V /* 3663ff09d7ecSAneesh Kumar K.V * The VMA was not fully populated on mmap() or missing VM_DONTEXPAND 3664ff09d7ecSAneesh Kumar K.V */ 3665ff09d7ecSAneesh Kumar K.V if (!vma->vm_ops->fault) { 3666ff09d7ecSAneesh Kumar K.V /* 3667ff09d7ecSAneesh Kumar K.V * If we find a migration pmd entry or a none pmd entry, which 3668ff09d7ecSAneesh Kumar K.V * should never happen, return SIGBUS 3669ff09d7ecSAneesh Kumar K.V */ 3670ff09d7ecSAneesh Kumar K.V if (unlikely(!pmd_present(*vmf->pmd))) 3671b0b9b3dfSHugh Dickins ret = VM_FAULT_SIGBUS; 3672ff09d7ecSAneesh Kumar K.V else { 3673ff09d7ecSAneesh Kumar K.V vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, 3674ff09d7ecSAneesh Kumar K.V vmf->pmd, 3675ff09d7ecSAneesh Kumar K.V vmf->address, 3676ff09d7ecSAneesh Kumar K.V &vmf->ptl); 3677ff09d7ecSAneesh Kumar K.V /* 3678ff09d7ecSAneesh Kumar K.V * Make sure this is not a temporary clearing of pte 3679ff09d7ecSAneesh Kumar K.V * by holding ptl and checking again. A R/M/W update 3680ff09d7ecSAneesh Kumar K.V * of pte involves: take ptl, clearing the pte so that 3681ff09d7ecSAneesh Kumar K.V * we don't have concurrent modification by hardware 3682ff09d7ecSAneesh Kumar K.V * followed by an update. 3683ff09d7ecSAneesh Kumar K.V */ 3684ff09d7ecSAneesh Kumar K.V if (unlikely(pte_none(*vmf->pte))) 3685ff09d7ecSAneesh Kumar K.V ret = VM_FAULT_SIGBUS; 3686ff09d7ecSAneesh Kumar K.V else 3687ff09d7ecSAneesh Kumar K.V ret = VM_FAULT_NOPAGE; 3688ff09d7ecSAneesh Kumar K.V 3689ff09d7ecSAneesh Kumar K.V pte_unmap_unlock(vmf->pte, vmf->ptl); 3690ff09d7ecSAneesh Kumar K.V } 3691ff09d7ecSAneesh Kumar K.V } else if (!(vmf->flags & FAULT_FLAG_WRITE)) 3692b0b9b3dfSHugh Dickins ret = do_read_fault(vmf); 3693b0b9b3dfSHugh Dickins else if (!(vma->vm_flags & VM_SHARED)) 3694b0b9b3dfSHugh Dickins ret = do_cow_fault(vmf); 3695b0b9b3dfSHugh Dickins else 3696b0b9b3dfSHugh Dickins ret = do_shared_fault(vmf); 3697b0b9b3dfSHugh Dickins 3698b0b9b3dfSHugh Dickins /* preallocated pagetable is unused: free it */ 3699b0b9b3dfSHugh Dickins if (vmf->prealloc_pte) { 3700fc8efd2dSJan Stancek pte_free(vm_mm, vmf->prealloc_pte); 37017f2b6ce8STobin C Harding vmf->prealloc_pte = NULL; 3702b0b9b3dfSHugh Dickins } 3703b0b9b3dfSHugh Dickins return ret; 370454cb8821SNick Piggin } 370554cb8821SNick Piggin 3706b19a9939SRashika Kheria static int numa_migrate_prep(struct page *page, struct vm_area_struct *vma, 370704bb2f94SRik van Riel unsigned long addr, int page_nid, 370804bb2f94SRik van Riel int *flags) 37099532fec1SMel Gorman { 37109532fec1SMel Gorman get_page(page); 37119532fec1SMel Gorman 37129532fec1SMel Gorman count_vm_numa_event(NUMA_HINT_FAULTS); 371304bb2f94SRik van Riel if (page_nid == numa_node_id()) { 37149532fec1SMel Gorman count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL); 371504bb2f94SRik van Riel *flags |= TNF_FAULT_LOCAL; 371604bb2f94SRik van Riel } 37179532fec1SMel Gorman 37189532fec1SMel Gorman return mpol_misplaced(page, vma, addr); 37199532fec1SMel Gorman } 37209532fec1SMel Gorman 37212b740303SSouptick Joarder static vm_fault_t do_numa_page(struct vm_fault *vmf) 3722d10e63f2SMel Gorman { 372382b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 37244daae3b4SMel Gorman struct page *page = NULL; 372598fa15f3SAnshuman Khandual int page_nid = NUMA_NO_NODE; 372690572890SPeter Zijlstra int last_cpupid; 3727cbee9f88SPeter Zijlstra int target_nid; 3728b8593bfdSMel Gorman bool migrated = false; 372904a86453SAneesh Kumar K.V pte_t pte, old_pte; 3730288bc549SAneesh Kumar K.V bool was_writable = pte_savedwrite(vmf->orig_pte); 37316688cc05SPeter Zijlstra int flags = 0; 3732d10e63f2SMel Gorman 3733d10e63f2SMel Gorman /* 3734d10e63f2SMel Gorman * The "pte" at this point cannot be used safely without 3735d10e63f2SMel Gorman * validation through pte_unmap_same(). It's of NUMA type but 3736d10e63f2SMel Gorman * the pfn may be screwed if the read is non atomic. 3737d10e63f2SMel Gorman */ 373882b0f8c3SJan Kara vmf->ptl = pte_lockptr(vma->vm_mm, vmf->pmd); 373982b0f8c3SJan Kara spin_lock(vmf->ptl); 3740cee216a6SAneesh Kumar K.V if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) { 374182b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 37424daae3b4SMel Gorman goto out; 37434daae3b4SMel Gorman } 37444daae3b4SMel Gorman 3745cee216a6SAneesh Kumar K.V /* 3746cee216a6SAneesh Kumar K.V * Make it present again, Depending on how arch implementes non 3747cee216a6SAneesh Kumar K.V * accessible ptes, some can allow access by kernel mode. 3748cee216a6SAneesh Kumar K.V */ 374904a86453SAneesh Kumar K.V old_pte = ptep_modify_prot_start(vma, vmf->address, vmf->pte); 375004a86453SAneesh Kumar K.V pte = pte_modify(old_pte, vma->vm_page_prot); 37514d942466SMel Gorman pte = pte_mkyoung(pte); 3752b191f9b1SMel Gorman if (was_writable) 3753b191f9b1SMel Gorman pte = pte_mkwrite(pte); 375404a86453SAneesh Kumar K.V ptep_modify_prot_commit(vma, vmf->address, vmf->pte, old_pte, pte); 375582b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 3756d10e63f2SMel Gorman 375782b0f8c3SJan Kara page = vm_normal_page(vma, vmf->address, pte); 3758d10e63f2SMel Gorman if (!page) { 375982b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3760d10e63f2SMel Gorman return 0; 3761d10e63f2SMel Gorman } 3762d10e63f2SMel Gorman 3763e81c4802SKirill A. Shutemov /* TODO: handle PTE-mapped THP */ 3764e81c4802SKirill A. Shutemov if (PageCompound(page)) { 376582b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3766e81c4802SKirill A. Shutemov return 0; 3767e81c4802SKirill A. Shutemov } 3768e81c4802SKirill A. Shutemov 37696688cc05SPeter Zijlstra /* 3770bea66fbdSMel Gorman * Avoid grouping on RO pages in general. RO pages shouldn't hurt as 3771bea66fbdSMel Gorman * much anyway since they can be in shared cache state. This misses 3772bea66fbdSMel Gorman * the case where a mapping is writable but the process never writes 3773bea66fbdSMel Gorman * to it but pte_write gets cleared during protection updates and 3774bea66fbdSMel Gorman * pte_dirty has unpredictable behaviour between PTE scan updates, 3775bea66fbdSMel Gorman * background writeback, dirty balancing and application behaviour. 37766688cc05SPeter Zijlstra */ 3777d59dc7bcSRik van Riel if (!pte_write(pte)) 37786688cc05SPeter Zijlstra flags |= TNF_NO_GROUP; 37796688cc05SPeter Zijlstra 3780dabe1d99SRik van Riel /* 3781dabe1d99SRik van Riel * Flag if the page is shared between multiple address spaces. This 3782dabe1d99SRik van Riel * is later used when determining whether to group tasks together 3783dabe1d99SRik van Riel */ 3784dabe1d99SRik van Riel if (page_mapcount(page) > 1 && (vma->vm_flags & VM_SHARED)) 3785dabe1d99SRik van Riel flags |= TNF_SHARED; 3786dabe1d99SRik van Riel 378790572890SPeter Zijlstra last_cpupid = page_cpupid_last(page); 37888191acbdSMel Gorman page_nid = page_to_nid(page); 378982b0f8c3SJan Kara target_nid = numa_migrate_prep(page, vma, vmf->address, page_nid, 3790bae473a4SKirill A. Shutemov &flags); 379182b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 379298fa15f3SAnshuman Khandual if (target_nid == NUMA_NO_NODE) { 37934daae3b4SMel Gorman put_page(page); 37944daae3b4SMel Gorman goto out; 37954daae3b4SMel Gorman } 37964daae3b4SMel Gorman 37974daae3b4SMel Gorman /* Migrate to the requested node */ 37981bc115d8SMel Gorman migrated = migrate_misplaced_page(page, vma, target_nid); 37996688cc05SPeter Zijlstra if (migrated) { 38008191acbdSMel Gorman page_nid = target_nid; 38016688cc05SPeter Zijlstra flags |= TNF_MIGRATED; 3802074c2381SMel Gorman } else 3803074c2381SMel Gorman flags |= TNF_MIGRATE_FAIL; 38044daae3b4SMel Gorman 38054daae3b4SMel Gorman out: 380698fa15f3SAnshuman Khandual if (page_nid != NUMA_NO_NODE) 38076688cc05SPeter Zijlstra task_numa_fault(last_cpupid, page_nid, 1, flags); 3808d10e63f2SMel Gorman return 0; 3809d10e63f2SMel Gorman } 3810d10e63f2SMel Gorman 38112b740303SSouptick Joarder static inline vm_fault_t create_huge_pmd(struct vm_fault *vmf) 3812b96375f7SMatthew Wilcox { 3813f4200391SDave Jiang if (vma_is_anonymous(vmf->vma)) 381482b0f8c3SJan Kara return do_huge_pmd_anonymous_page(vmf); 3815a2d58167SDave Jiang if (vmf->vma->vm_ops->huge_fault) 3816c791ace1SDave Jiang return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD); 3817b96375f7SMatthew Wilcox return VM_FAULT_FALLBACK; 3818b96375f7SMatthew Wilcox } 3819b96375f7SMatthew Wilcox 3820183f24aaSGeert Uytterhoeven /* `inline' is required to avoid gcc 4.1.2 build error */ 38212b740303SSouptick Joarder static inline vm_fault_t wp_huge_pmd(struct vm_fault *vmf, pmd_t orig_pmd) 3822b96375f7SMatthew Wilcox { 382382b0f8c3SJan Kara if (vma_is_anonymous(vmf->vma)) 382482b0f8c3SJan Kara return do_huge_pmd_wp_page(vmf, orig_pmd); 3825a2d58167SDave Jiang if (vmf->vma->vm_ops->huge_fault) 3826c791ace1SDave Jiang return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD); 3827af9e4d5fSKirill A. Shutemov 3828af9e4d5fSKirill A. Shutemov /* COW handled on pte level: split pmd */ 382982b0f8c3SJan Kara VM_BUG_ON_VMA(vmf->vma->vm_flags & VM_SHARED, vmf->vma); 383082b0f8c3SJan Kara __split_huge_pmd(vmf->vma, vmf->pmd, vmf->address, false, NULL); 3831af9e4d5fSKirill A. Shutemov 3832b96375f7SMatthew Wilcox return VM_FAULT_FALLBACK; 3833b96375f7SMatthew Wilcox } 3834b96375f7SMatthew Wilcox 383538e08854SLorenzo Stoakes static inline bool vma_is_accessible(struct vm_area_struct *vma) 383638e08854SLorenzo Stoakes { 383738e08854SLorenzo Stoakes return vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE); 383838e08854SLorenzo Stoakes } 383938e08854SLorenzo Stoakes 38402b740303SSouptick Joarder static vm_fault_t create_huge_pud(struct vm_fault *vmf) 3841a00cc7d9SMatthew Wilcox { 3842a00cc7d9SMatthew Wilcox #ifdef CONFIG_TRANSPARENT_HUGEPAGE 3843a00cc7d9SMatthew Wilcox /* No support for anonymous transparent PUD pages yet */ 3844a00cc7d9SMatthew Wilcox if (vma_is_anonymous(vmf->vma)) 3845a00cc7d9SMatthew Wilcox return VM_FAULT_FALLBACK; 3846a00cc7d9SMatthew Wilcox if (vmf->vma->vm_ops->huge_fault) 3847c791ace1SDave Jiang return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD); 3848a00cc7d9SMatthew Wilcox #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 3849a00cc7d9SMatthew Wilcox return VM_FAULT_FALLBACK; 3850a00cc7d9SMatthew Wilcox } 3851a00cc7d9SMatthew Wilcox 38522b740303SSouptick Joarder static vm_fault_t wp_huge_pud(struct vm_fault *vmf, pud_t orig_pud) 3853a00cc7d9SMatthew Wilcox { 3854a00cc7d9SMatthew Wilcox #ifdef CONFIG_TRANSPARENT_HUGEPAGE 3855a00cc7d9SMatthew Wilcox /* No support for anonymous transparent PUD pages yet */ 3856a00cc7d9SMatthew Wilcox if (vma_is_anonymous(vmf->vma)) 3857a00cc7d9SMatthew Wilcox return VM_FAULT_FALLBACK; 3858a00cc7d9SMatthew Wilcox if (vmf->vma->vm_ops->huge_fault) 3859c791ace1SDave Jiang return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD); 3860a00cc7d9SMatthew Wilcox #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 3861a00cc7d9SMatthew Wilcox return VM_FAULT_FALLBACK; 3862a00cc7d9SMatthew Wilcox } 3863a00cc7d9SMatthew Wilcox 38641da177e4SLinus Torvalds /* 38651da177e4SLinus Torvalds * These routines also need to handle stuff like marking pages dirty 38661da177e4SLinus Torvalds * and/or accessed for architectures that don't do it in hardware (most 38671da177e4SLinus Torvalds * RISC architectures). The early dirtying is also good on the i386. 38681da177e4SLinus Torvalds * 38691da177e4SLinus Torvalds * There is also a hook called "update_mmu_cache()" that architectures 38701da177e4SLinus Torvalds * with external mmu caches can use to update those (ie the Sparc or 38711da177e4SLinus Torvalds * PowerPC hashed page tables that act as extended TLBs). 38721da177e4SLinus Torvalds * 38737267ec00SKirill A. Shutemov * We enter with non-exclusive mmap_sem (to exclude vma changes, but allow 38747267ec00SKirill A. Shutemov * concurrent faults). 38759a95f3cfSPaul Cassella * 38767267ec00SKirill A. Shutemov * The mmap_sem may have been released depending on flags and our return value. 38777267ec00SKirill A. Shutemov * See filemap_fault() and __lock_page_or_retry(). 38781da177e4SLinus Torvalds */ 38792b740303SSouptick Joarder static vm_fault_t handle_pte_fault(struct vm_fault *vmf) 38801da177e4SLinus Torvalds { 38811da177e4SLinus Torvalds pte_t entry; 38821da177e4SLinus Torvalds 388382b0f8c3SJan Kara if (unlikely(pmd_none(*vmf->pmd))) { 38847267ec00SKirill A. Shutemov /* 38857267ec00SKirill A. Shutemov * Leave __pte_alloc() until later: because vm_ops->fault may 38867267ec00SKirill A. Shutemov * want to allocate huge page, and if we expose page table 38877267ec00SKirill A. Shutemov * for an instant, it will be difficult to retract from 38887267ec00SKirill A. Shutemov * concurrent faults and from rmap lookups. 38897267ec00SKirill A. Shutemov */ 389082b0f8c3SJan Kara vmf->pte = NULL; 38917267ec00SKirill A. Shutemov } else { 38927267ec00SKirill A. Shutemov /* See comment in pte_alloc_one_map() */ 3893d0f0931dSRoss Zwisler if (pmd_devmap_trans_unstable(vmf->pmd)) 38947267ec00SKirill A. Shutemov return 0; 38957267ec00SKirill A. Shutemov /* 38967267ec00SKirill A. Shutemov * A regular pmd is established and it can't morph into a huge 38977267ec00SKirill A. Shutemov * pmd from under us anymore at this point because we hold the 38987267ec00SKirill A. Shutemov * mmap_sem read mode and khugepaged takes it in write mode. 38997267ec00SKirill A. Shutemov * So now it's safe to run pte_offset_map(). 39007267ec00SKirill A. Shutemov */ 390182b0f8c3SJan Kara vmf->pte = pte_offset_map(vmf->pmd, vmf->address); 39022994302bSJan Kara vmf->orig_pte = *vmf->pte; 39037267ec00SKirill A. Shutemov 3904e37c6982SChristian Borntraeger /* 3905e37c6982SChristian Borntraeger * some architectures can have larger ptes than wordsize, 39067267ec00SKirill A. Shutemov * e.g.ppc44x-defconfig has CONFIG_PTE_64BIT=y and 3907b03a0fe0SPaul E. McKenney * CONFIG_32BIT=y, so READ_ONCE cannot guarantee atomic 3908b03a0fe0SPaul E. McKenney * accesses. The code below just needs a consistent view 3909b03a0fe0SPaul E. McKenney * for the ifs and we later double check anyway with the 39107267ec00SKirill A. Shutemov * ptl lock held. So here a barrier will do. 3911e37c6982SChristian Borntraeger */ 3912e37c6982SChristian Borntraeger barrier(); 39132994302bSJan Kara if (pte_none(vmf->orig_pte)) { 391482b0f8c3SJan Kara pte_unmap(vmf->pte); 391582b0f8c3SJan Kara vmf->pte = NULL; 39167267ec00SKirill A. Shutemov } 39177267ec00SKirill A. Shutemov } 39187267ec00SKirill A. Shutemov 391982b0f8c3SJan Kara if (!vmf->pte) { 392082b0f8c3SJan Kara if (vma_is_anonymous(vmf->vma)) 392182b0f8c3SJan Kara return do_anonymous_page(vmf); 3922b5330628SOleg Nesterov else 392382b0f8c3SJan Kara return do_fault(vmf); 392465500d23SHugh Dickins } 39257267ec00SKirill A. Shutemov 39262994302bSJan Kara if (!pte_present(vmf->orig_pte)) 39272994302bSJan Kara return do_swap_page(vmf); 39281da177e4SLinus Torvalds 39292994302bSJan Kara if (pte_protnone(vmf->orig_pte) && vma_is_accessible(vmf->vma)) 39302994302bSJan Kara return do_numa_page(vmf); 3931d10e63f2SMel Gorman 393282b0f8c3SJan Kara vmf->ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd); 393382b0f8c3SJan Kara spin_lock(vmf->ptl); 39342994302bSJan Kara entry = vmf->orig_pte; 393582b0f8c3SJan Kara if (unlikely(!pte_same(*vmf->pte, entry))) 39368f4e2101SHugh Dickins goto unlock; 393782b0f8c3SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) { 3938f6f37321SLinus Torvalds if (!pte_write(entry)) 39392994302bSJan Kara return do_wp_page(vmf); 39401da177e4SLinus Torvalds entry = pte_mkdirty(entry); 39411da177e4SLinus Torvalds } 39421da177e4SLinus Torvalds entry = pte_mkyoung(entry); 394382b0f8c3SJan Kara if (ptep_set_access_flags(vmf->vma, vmf->address, vmf->pte, entry, 394482b0f8c3SJan Kara vmf->flags & FAULT_FLAG_WRITE)) { 394582b0f8c3SJan Kara update_mmu_cache(vmf->vma, vmf->address, vmf->pte); 39461a44e149SAndrea Arcangeli } else { 39471a44e149SAndrea Arcangeli /* 39481a44e149SAndrea Arcangeli * This is needed only for protection faults but the arch code 39491a44e149SAndrea Arcangeli * is not yet telling us if this is a protection fault or not. 39501a44e149SAndrea Arcangeli * This still avoids useless tlb flushes for .text page faults 39511a44e149SAndrea Arcangeli * with threads. 39521a44e149SAndrea Arcangeli */ 395382b0f8c3SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) 395482b0f8c3SJan Kara flush_tlb_fix_spurious_fault(vmf->vma, vmf->address); 39551a44e149SAndrea Arcangeli } 39568f4e2101SHugh Dickins unlock: 395782b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 395883c54070SNick Piggin return 0; 39591da177e4SLinus Torvalds } 39601da177e4SLinus Torvalds 39611da177e4SLinus Torvalds /* 39621da177e4SLinus Torvalds * By the time we get here, we already hold the mm semaphore 39639a95f3cfSPaul Cassella * 39649a95f3cfSPaul Cassella * The mmap_sem may have been released depending on flags and our 39659a95f3cfSPaul Cassella * return value. See filemap_fault() and __lock_page_or_retry(). 39661da177e4SLinus Torvalds */ 39672b740303SSouptick Joarder static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma, 39682b740303SSouptick Joarder unsigned long address, unsigned int flags) 39691da177e4SLinus Torvalds { 397082b0f8c3SJan Kara struct vm_fault vmf = { 3971bae473a4SKirill A. Shutemov .vma = vma, 39721a29d85eSJan Kara .address = address & PAGE_MASK, 3973bae473a4SKirill A. Shutemov .flags = flags, 39740721ec8bSJan Kara .pgoff = linear_page_index(vma, address), 3975667240e0SJan Kara .gfp_mask = __get_fault_gfp_mask(vma), 3976bae473a4SKirill A. Shutemov }; 3977fde26bedSAnshuman Khandual unsigned int dirty = flags & FAULT_FLAG_WRITE; 3978dcddffd4SKirill A. Shutemov struct mm_struct *mm = vma->vm_mm; 39791da177e4SLinus Torvalds pgd_t *pgd; 3980c2febafcSKirill A. Shutemov p4d_t *p4d; 39812b740303SSouptick Joarder vm_fault_t ret; 39821da177e4SLinus Torvalds 39831da177e4SLinus Torvalds pgd = pgd_offset(mm, address); 3984c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, address); 3985c2febafcSKirill A. Shutemov if (!p4d) 3986c2febafcSKirill A. Shutemov return VM_FAULT_OOM; 3987a00cc7d9SMatthew Wilcox 3988c2febafcSKirill A. Shutemov vmf.pud = pud_alloc(mm, p4d, address); 3989a00cc7d9SMatthew Wilcox if (!vmf.pud) 3990c74df32cSHugh Dickins return VM_FAULT_OOM; 39917635d9cbSMichal Hocko if (pud_none(*vmf.pud) && __transparent_hugepage_enabled(vma)) { 3992a00cc7d9SMatthew Wilcox ret = create_huge_pud(&vmf); 3993a00cc7d9SMatthew Wilcox if (!(ret & VM_FAULT_FALLBACK)) 3994a00cc7d9SMatthew Wilcox return ret; 3995a00cc7d9SMatthew Wilcox } else { 3996a00cc7d9SMatthew Wilcox pud_t orig_pud = *vmf.pud; 3997a00cc7d9SMatthew Wilcox 3998a00cc7d9SMatthew Wilcox barrier(); 3999a00cc7d9SMatthew Wilcox if (pud_trans_huge(orig_pud) || pud_devmap(orig_pud)) { 4000a00cc7d9SMatthew Wilcox 4001a00cc7d9SMatthew Wilcox /* NUMA case for anonymous PUDs would go here */ 4002a00cc7d9SMatthew Wilcox 4003f6f37321SLinus Torvalds if (dirty && !pud_write(orig_pud)) { 4004a00cc7d9SMatthew Wilcox ret = wp_huge_pud(&vmf, orig_pud); 4005a00cc7d9SMatthew Wilcox if (!(ret & VM_FAULT_FALLBACK)) 4006a00cc7d9SMatthew Wilcox return ret; 4007a00cc7d9SMatthew Wilcox } else { 4008a00cc7d9SMatthew Wilcox huge_pud_set_accessed(&vmf, orig_pud); 4009a00cc7d9SMatthew Wilcox return 0; 4010a00cc7d9SMatthew Wilcox } 4011a00cc7d9SMatthew Wilcox } 4012a00cc7d9SMatthew Wilcox } 4013a00cc7d9SMatthew Wilcox 4014a00cc7d9SMatthew Wilcox vmf.pmd = pmd_alloc(mm, vmf.pud, address); 401582b0f8c3SJan Kara if (!vmf.pmd) 4016c74df32cSHugh Dickins return VM_FAULT_OOM; 40177635d9cbSMichal Hocko if (pmd_none(*vmf.pmd) && __transparent_hugepage_enabled(vma)) { 4018a2d58167SDave Jiang ret = create_huge_pmd(&vmf); 4019c0292554SKirill A. Shutemov if (!(ret & VM_FAULT_FALLBACK)) 4020c0292554SKirill A. Shutemov return ret; 402171e3aac0SAndrea Arcangeli } else { 402282b0f8c3SJan Kara pmd_t orig_pmd = *vmf.pmd; 40231f1d06c3SDavid Rientjes 402471e3aac0SAndrea Arcangeli barrier(); 402584c3fc4eSZi Yan if (unlikely(is_swap_pmd(orig_pmd))) { 402684c3fc4eSZi Yan VM_BUG_ON(thp_migration_supported() && 402784c3fc4eSZi Yan !is_pmd_migration_entry(orig_pmd)); 402884c3fc4eSZi Yan if (is_pmd_migration_entry(orig_pmd)) 402984c3fc4eSZi Yan pmd_migration_entry_wait(mm, vmf.pmd); 403084c3fc4eSZi Yan return 0; 403184c3fc4eSZi Yan } 40325c7fb56eSDan Williams if (pmd_trans_huge(orig_pmd) || pmd_devmap(orig_pmd)) { 403338e08854SLorenzo Stoakes if (pmd_protnone(orig_pmd) && vma_is_accessible(vma)) 403482b0f8c3SJan Kara return do_huge_pmd_numa_page(&vmf, orig_pmd); 4035d10e63f2SMel Gorman 4036f6f37321SLinus Torvalds if (dirty && !pmd_write(orig_pmd)) { 403782b0f8c3SJan Kara ret = wp_huge_pmd(&vmf, orig_pmd); 40389845cbbdSKirill A. Shutemov if (!(ret & VM_FAULT_FALLBACK)) 40391f1d06c3SDavid Rientjes return ret; 4040a1dd450bSWill Deacon } else { 404182b0f8c3SJan Kara huge_pmd_set_accessed(&vmf, orig_pmd); 404271e3aac0SAndrea Arcangeli return 0; 404371e3aac0SAndrea Arcangeli } 404471e3aac0SAndrea Arcangeli } 40459845cbbdSKirill A. Shutemov } 404671e3aac0SAndrea Arcangeli 404782b0f8c3SJan Kara return handle_pte_fault(&vmf); 40481da177e4SLinus Torvalds } 40491da177e4SLinus Torvalds 40509a95f3cfSPaul Cassella /* 40519a95f3cfSPaul Cassella * By the time we get here, we already hold the mm semaphore 40529a95f3cfSPaul Cassella * 40539a95f3cfSPaul Cassella * The mmap_sem may have been released depending on flags and our 40549a95f3cfSPaul Cassella * return value. See filemap_fault() and __lock_page_or_retry(). 40559a95f3cfSPaul Cassella */ 40562b740303SSouptick Joarder vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address, 4057dcddffd4SKirill A. Shutemov unsigned int flags) 4058519e5247SJohannes Weiner { 40592b740303SSouptick Joarder vm_fault_t ret; 4060519e5247SJohannes Weiner 4061519e5247SJohannes Weiner __set_current_state(TASK_RUNNING); 4062519e5247SJohannes Weiner 4063519e5247SJohannes Weiner count_vm_event(PGFAULT); 40642262185cSRoman Gushchin count_memcg_event_mm(vma->vm_mm, PGFAULT); 4065519e5247SJohannes Weiner 4066519e5247SJohannes Weiner /* do counter updates before entering really critical section. */ 4067519e5247SJohannes Weiner check_sync_rss_stat(current); 4068519e5247SJohannes Weiner 4069de0c799bSLaurent Dufour if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE, 4070de0c799bSLaurent Dufour flags & FAULT_FLAG_INSTRUCTION, 4071de0c799bSLaurent Dufour flags & FAULT_FLAG_REMOTE)) 4072de0c799bSLaurent Dufour return VM_FAULT_SIGSEGV; 4073de0c799bSLaurent Dufour 4074519e5247SJohannes Weiner /* 4075519e5247SJohannes Weiner * Enable the memcg OOM handling for faults triggered in user 4076519e5247SJohannes Weiner * space. Kernel faults are handled more gracefully. 4077519e5247SJohannes Weiner */ 4078519e5247SJohannes Weiner if (flags & FAULT_FLAG_USER) 407929ef680aSMichal Hocko mem_cgroup_enter_user_fault(); 4080519e5247SJohannes Weiner 4081bae473a4SKirill A. Shutemov if (unlikely(is_vm_hugetlb_page(vma))) 4082bae473a4SKirill A. Shutemov ret = hugetlb_fault(vma->vm_mm, vma, address, flags); 4083bae473a4SKirill A. Shutemov else 4084dcddffd4SKirill A. Shutemov ret = __handle_mm_fault(vma, address, flags); 4085519e5247SJohannes Weiner 408649426420SJohannes Weiner if (flags & FAULT_FLAG_USER) { 408729ef680aSMichal Hocko mem_cgroup_exit_user_fault(); 408849426420SJohannes Weiner /* 408949426420SJohannes Weiner * The task may have entered a memcg OOM situation but 409049426420SJohannes Weiner * if the allocation error was handled gracefully (no 409149426420SJohannes Weiner * VM_FAULT_OOM), there is no need to kill anything. 409249426420SJohannes Weiner * Just clean up the OOM state peacefully. 409349426420SJohannes Weiner */ 409449426420SJohannes Weiner if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM)) 409549426420SJohannes Weiner mem_cgroup_oom_synchronize(false); 409649426420SJohannes Weiner } 40973812c8c8SJohannes Weiner 4098519e5247SJohannes Weiner return ret; 4099519e5247SJohannes Weiner } 4100e1d6d01aSJesse Barnes EXPORT_SYMBOL_GPL(handle_mm_fault); 4101519e5247SJohannes Weiner 410290eceff1SKirill A. Shutemov #ifndef __PAGETABLE_P4D_FOLDED 410390eceff1SKirill A. Shutemov /* 410490eceff1SKirill A. Shutemov * Allocate p4d page table. 410590eceff1SKirill A. Shutemov * We've already handled the fast-path in-line. 410690eceff1SKirill A. Shutemov */ 410790eceff1SKirill A. Shutemov int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address) 410890eceff1SKirill A. Shutemov { 410990eceff1SKirill A. Shutemov p4d_t *new = p4d_alloc_one(mm, address); 411090eceff1SKirill A. Shutemov if (!new) 411190eceff1SKirill A. Shutemov return -ENOMEM; 411290eceff1SKirill A. Shutemov 411390eceff1SKirill A. Shutemov smp_wmb(); /* See comment in __pte_alloc */ 411490eceff1SKirill A. Shutemov 411590eceff1SKirill A. Shutemov spin_lock(&mm->page_table_lock); 411690eceff1SKirill A. Shutemov if (pgd_present(*pgd)) /* Another has populated it */ 411790eceff1SKirill A. Shutemov p4d_free(mm, new); 411890eceff1SKirill A. Shutemov else 411990eceff1SKirill A. Shutemov pgd_populate(mm, pgd, new); 412090eceff1SKirill A. Shutemov spin_unlock(&mm->page_table_lock); 412190eceff1SKirill A. Shutemov return 0; 412290eceff1SKirill A. Shutemov } 412390eceff1SKirill A. Shutemov #endif /* __PAGETABLE_P4D_FOLDED */ 412490eceff1SKirill A. Shutemov 41251da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED 41261da177e4SLinus Torvalds /* 41271da177e4SLinus Torvalds * Allocate page upper directory. 4128872fec16SHugh Dickins * We've already handled the fast-path in-line. 41291da177e4SLinus Torvalds */ 4130c2febafcSKirill A. Shutemov int __pud_alloc(struct mm_struct *mm, p4d_t *p4d, unsigned long address) 41311da177e4SLinus Torvalds { 4132c74df32cSHugh Dickins pud_t *new = pud_alloc_one(mm, address); 4133c74df32cSHugh Dickins if (!new) 41341bb3630eSHugh Dickins return -ENOMEM; 41351da177e4SLinus Torvalds 4136362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 4137362a61adSNick Piggin 4138872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 4139c2febafcSKirill A. Shutemov #ifndef __ARCH_HAS_5LEVEL_HACK 4140b4e98d9aSKirill A. Shutemov if (!p4d_present(*p4d)) { 4141b4e98d9aSKirill A. Shutemov mm_inc_nr_puds(mm); 4142c2febafcSKirill A. Shutemov p4d_populate(mm, p4d, new); 4143b4e98d9aSKirill A. Shutemov } else /* Another has populated it */ 4144c2febafcSKirill A. Shutemov pud_free(mm, new); 4145b4e98d9aSKirill A. Shutemov #else 4146b4e98d9aSKirill A. Shutemov if (!pgd_present(*p4d)) { 4147b4e98d9aSKirill A. Shutemov mm_inc_nr_puds(mm); 4148c2febafcSKirill A. Shutemov pgd_populate(mm, p4d, new); 4149b4e98d9aSKirill A. Shutemov } else /* Another has populated it */ 4150b4e98d9aSKirill A. Shutemov pud_free(mm, new); 4151c2febafcSKirill A. Shutemov #endif /* __ARCH_HAS_5LEVEL_HACK */ 4152872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 41531bb3630eSHugh Dickins return 0; 41541da177e4SLinus Torvalds } 41551da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */ 41561da177e4SLinus Torvalds 41571da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED 41581da177e4SLinus Torvalds /* 41591da177e4SLinus Torvalds * Allocate page middle directory. 4160872fec16SHugh Dickins * We've already handled the fast-path in-line. 41611da177e4SLinus Torvalds */ 41621bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) 41631da177e4SLinus Torvalds { 4164a00cc7d9SMatthew Wilcox spinlock_t *ptl; 4165c74df32cSHugh Dickins pmd_t *new = pmd_alloc_one(mm, address); 4166c74df32cSHugh Dickins if (!new) 41671bb3630eSHugh Dickins return -ENOMEM; 41681da177e4SLinus Torvalds 4169362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 4170362a61adSNick Piggin 4171a00cc7d9SMatthew Wilcox ptl = pud_lock(mm, pud); 41721da177e4SLinus Torvalds #ifndef __ARCH_HAS_4LEVEL_HACK 4173dc6c9a35SKirill A. Shutemov if (!pud_present(*pud)) { 4174dc6c9a35SKirill A. Shutemov mm_inc_nr_pmds(mm); 41751da177e4SLinus Torvalds pud_populate(mm, pud, new); 4176dc6c9a35SKirill A. Shutemov } else /* Another has populated it */ 41775e541973SBenjamin Herrenschmidt pmd_free(mm, new); 4178dc6c9a35SKirill A. Shutemov #else 4179dc6c9a35SKirill A. Shutemov if (!pgd_present(*pud)) { 4180dc6c9a35SKirill A. Shutemov mm_inc_nr_pmds(mm); 41811da177e4SLinus Torvalds pgd_populate(mm, pud, new); 4182dc6c9a35SKirill A. Shutemov } else /* Another has populated it */ 4183dc6c9a35SKirill A. Shutemov pmd_free(mm, new); 41841da177e4SLinus Torvalds #endif /* __ARCH_HAS_4LEVEL_HACK */ 4185a00cc7d9SMatthew Wilcox spin_unlock(ptl); 41861bb3630eSHugh Dickins return 0; 41871da177e4SLinus Torvalds } 41881da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */ 41891da177e4SLinus Torvalds 419009796395SRoss Zwisler static int __follow_pte_pmd(struct mm_struct *mm, unsigned long address, 4191ac46d4f3SJérôme Glisse struct mmu_notifier_range *range, 419209796395SRoss Zwisler pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp) 4193f8ad0f49SJohannes Weiner { 4194f8ad0f49SJohannes Weiner pgd_t *pgd; 4195c2febafcSKirill A. Shutemov p4d_t *p4d; 4196f8ad0f49SJohannes Weiner pud_t *pud; 4197f8ad0f49SJohannes Weiner pmd_t *pmd; 4198f8ad0f49SJohannes Weiner pte_t *ptep; 4199f8ad0f49SJohannes Weiner 4200f8ad0f49SJohannes Weiner pgd = pgd_offset(mm, address); 4201f8ad0f49SJohannes Weiner if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) 4202f8ad0f49SJohannes Weiner goto out; 4203f8ad0f49SJohannes Weiner 4204c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, address); 4205c2febafcSKirill A. Shutemov if (p4d_none(*p4d) || unlikely(p4d_bad(*p4d))) 4206c2febafcSKirill A. Shutemov goto out; 4207c2febafcSKirill A. Shutemov 4208c2febafcSKirill A. Shutemov pud = pud_offset(p4d, address); 4209f8ad0f49SJohannes Weiner if (pud_none(*pud) || unlikely(pud_bad(*pud))) 4210f8ad0f49SJohannes Weiner goto out; 4211f8ad0f49SJohannes Weiner 4212f8ad0f49SJohannes Weiner pmd = pmd_offset(pud, address); 4213f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 421409796395SRoss Zwisler 421509796395SRoss Zwisler if (pmd_huge(*pmd)) { 421609796395SRoss Zwisler if (!pmdpp) 4217f8ad0f49SJohannes Weiner goto out; 4218f8ad0f49SJohannes Weiner 4219ac46d4f3SJérôme Glisse if (range) { 42207269f999SJérôme Glisse mmu_notifier_range_init(range, MMU_NOTIFY_CLEAR, 0, 42216f4f13e8SJérôme Glisse NULL, mm, address & PMD_MASK, 4222ac46d4f3SJérôme Glisse (address & PMD_MASK) + PMD_SIZE); 4223ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(range); 4224a4d1a885SJérôme Glisse } 422509796395SRoss Zwisler *ptlp = pmd_lock(mm, pmd); 422609796395SRoss Zwisler if (pmd_huge(*pmd)) { 422709796395SRoss Zwisler *pmdpp = pmd; 422809796395SRoss Zwisler return 0; 422909796395SRoss Zwisler } 423009796395SRoss Zwisler spin_unlock(*ptlp); 4231ac46d4f3SJérôme Glisse if (range) 4232ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(range); 423309796395SRoss Zwisler } 423409796395SRoss Zwisler 423509796395SRoss Zwisler if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd))) 4236f8ad0f49SJohannes Weiner goto out; 4237f8ad0f49SJohannes Weiner 4238ac46d4f3SJérôme Glisse if (range) { 42397269f999SJérôme Glisse mmu_notifier_range_init(range, MMU_NOTIFY_CLEAR, 0, NULL, mm, 42406f4f13e8SJérôme Glisse address & PAGE_MASK, 42411ed7293aSMatthew Wilcox (address & PAGE_MASK) + PAGE_SIZE); 4242ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(range); 4243a4d1a885SJérôme Glisse } 4244f8ad0f49SJohannes Weiner ptep = pte_offset_map_lock(mm, pmd, address, ptlp); 4245f8ad0f49SJohannes Weiner if (!pte_present(*ptep)) 4246f8ad0f49SJohannes Weiner goto unlock; 4247f8ad0f49SJohannes Weiner *ptepp = ptep; 4248f8ad0f49SJohannes Weiner return 0; 4249f8ad0f49SJohannes Weiner unlock: 4250f8ad0f49SJohannes Weiner pte_unmap_unlock(ptep, *ptlp); 4251ac46d4f3SJérôme Glisse if (range) 4252ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(range); 4253f8ad0f49SJohannes Weiner out: 4254f8ad0f49SJohannes Weiner return -EINVAL; 4255f8ad0f49SJohannes Weiner } 4256f8ad0f49SJohannes Weiner 4257f729c8c9SRoss Zwisler static inline int follow_pte(struct mm_struct *mm, unsigned long address, 4258f729c8c9SRoss Zwisler pte_t **ptepp, spinlock_t **ptlp) 42591b36ba81SNamhyung Kim { 42601b36ba81SNamhyung Kim int res; 42611b36ba81SNamhyung Kim 42621b36ba81SNamhyung Kim /* (void) is needed to make gcc happy */ 42631b36ba81SNamhyung Kim (void) __cond_lock(*ptlp, 4264ac46d4f3SJérôme Glisse !(res = __follow_pte_pmd(mm, address, NULL, 4265a4d1a885SJérôme Glisse ptepp, NULL, ptlp))); 42661b36ba81SNamhyung Kim return res; 42671b36ba81SNamhyung Kim } 42681b36ba81SNamhyung Kim 426909796395SRoss Zwisler int follow_pte_pmd(struct mm_struct *mm, unsigned long address, 4270ac46d4f3SJérôme Glisse struct mmu_notifier_range *range, 427109796395SRoss Zwisler pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp) 427209796395SRoss Zwisler { 427309796395SRoss Zwisler int res; 427409796395SRoss Zwisler 427509796395SRoss Zwisler /* (void) is needed to make gcc happy */ 427609796395SRoss Zwisler (void) __cond_lock(*ptlp, 4277ac46d4f3SJérôme Glisse !(res = __follow_pte_pmd(mm, address, range, 4278a4d1a885SJérôme Glisse ptepp, pmdpp, ptlp))); 427909796395SRoss Zwisler return res; 428009796395SRoss Zwisler } 428109796395SRoss Zwisler EXPORT_SYMBOL(follow_pte_pmd); 428209796395SRoss Zwisler 42833b6748e2SJohannes Weiner /** 42843b6748e2SJohannes Weiner * follow_pfn - look up PFN at a user virtual address 42853b6748e2SJohannes Weiner * @vma: memory mapping 42863b6748e2SJohannes Weiner * @address: user virtual address 42873b6748e2SJohannes Weiner * @pfn: location to store found PFN 42883b6748e2SJohannes Weiner * 42893b6748e2SJohannes Weiner * Only IO mappings and raw PFN mappings are allowed. 42903b6748e2SJohannes Weiner * 4291a862f68aSMike Rapoport * Return: zero and the pfn at @pfn on success, -ve otherwise. 42923b6748e2SJohannes Weiner */ 42933b6748e2SJohannes Weiner int follow_pfn(struct vm_area_struct *vma, unsigned long address, 42943b6748e2SJohannes Weiner unsigned long *pfn) 42953b6748e2SJohannes Weiner { 42963b6748e2SJohannes Weiner int ret = -EINVAL; 42973b6748e2SJohannes Weiner spinlock_t *ptl; 42983b6748e2SJohannes Weiner pte_t *ptep; 42993b6748e2SJohannes Weiner 43003b6748e2SJohannes Weiner if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 43013b6748e2SJohannes Weiner return ret; 43023b6748e2SJohannes Weiner 43033b6748e2SJohannes Weiner ret = follow_pte(vma->vm_mm, address, &ptep, &ptl); 43043b6748e2SJohannes Weiner if (ret) 43053b6748e2SJohannes Weiner return ret; 43063b6748e2SJohannes Weiner *pfn = pte_pfn(*ptep); 43073b6748e2SJohannes Weiner pte_unmap_unlock(ptep, ptl); 43083b6748e2SJohannes Weiner return 0; 43093b6748e2SJohannes Weiner } 43103b6748e2SJohannes Weiner EXPORT_SYMBOL(follow_pfn); 43113b6748e2SJohannes Weiner 431228b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT 4313d87fe660Svenkatesh.pallipadi@intel.com int follow_phys(struct vm_area_struct *vma, 431428b2ee20SRik van Riel unsigned long address, unsigned int flags, 4315d87fe660Svenkatesh.pallipadi@intel.com unsigned long *prot, resource_size_t *phys) 431628b2ee20SRik van Riel { 431703668a4dSJohannes Weiner int ret = -EINVAL; 431828b2ee20SRik van Riel pte_t *ptep, pte; 431928b2ee20SRik van Riel spinlock_t *ptl; 432028b2ee20SRik van Riel 4321d87fe660Svenkatesh.pallipadi@intel.com if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 4322d87fe660Svenkatesh.pallipadi@intel.com goto out; 432328b2ee20SRik van Riel 432403668a4dSJohannes Weiner if (follow_pte(vma->vm_mm, address, &ptep, &ptl)) 4325d87fe660Svenkatesh.pallipadi@intel.com goto out; 432628b2ee20SRik van Riel pte = *ptep; 432703668a4dSJohannes Weiner 4328f6f37321SLinus Torvalds if ((flags & FOLL_WRITE) && !pte_write(pte)) 432928b2ee20SRik van Riel goto unlock; 433028b2ee20SRik van Riel 433128b2ee20SRik van Riel *prot = pgprot_val(pte_pgprot(pte)); 433203668a4dSJohannes Weiner *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT; 433328b2ee20SRik van Riel 433403668a4dSJohannes Weiner ret = 0; 433528b2ee20SRik van Riel unlock: 433628b2ee20SRik van Riel pte_unmap_unlock(ptep, ptl); 433728b2ee20SRik van Riel out: 4338d87fe660Svenkatesh.pallipadi@intel.com return ret; 433928b2ee20SRik van Riel } 434028b2ee20SRik van Riel 434128b2ee20SRik van Riel int generic_access_phys(struct vm_area_struct *vma, unsigned long addr, 434228b2ee20SRik van Riel void *buf, int len, int write) 434328b2ee20SRik van Riel { 434428b2ee20SRik van Riel resource_size_t phys_addr; 434528b2ee20SRik van Riel unsigned long prot = 0; 43462bc7273bSKOSAKI Motohiro void __iomem *maddr; 434728b2ee20SRik van Riel int offset = addr & (PAGE_SIZE-1); 434828b2ee20SRik van Riel 4349d87fe660Svenkatesh.pallipadi@intel.com if (follow_phys(vma, addr, write, &prot, &phys_addr)) 435028b2ee20SRik van Riel return -EINVAL; 435128b2ee20SRik van Riel 43529cb12d7bSGrazvydas Ignotas maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot); 435324eee1e4Sjie@chenjie6@huwei.com if (!maddr) 435424eee1e4Sjie@chenjie6@huwei.com return -ENOMEM; 435524eee1e4Sjie@chenjie6@huwei.com 435628b2ee20SRik van Riel if (write) 435728b2ee20SRik van Riel memcpy_toio(maddr + offset, buf, len); 435828b2ee20SRik van Riel else 435928b2ee20SRik van Riel memcpy_fromio(buf, maddr + offset, len); 436028b2ee20SRik van Riel iounmap(maddr); 436128b2ee20SRik van Riel 436228b2ee20SRik van Riel return len; 436328b2ee20SRik van Riel } 43645a73633eSUwe Kleine-König EXPORT_SYMBOL_GPL(generic_access_phys); 436528b2ee20SRik van Riel #endif 436628b2ee20SRik van Riel 43670ec76a11SDavid Howells /* 4368206cb636SStephen Wilson * Access another process' address space as given in mm. If non-NULL, use the 4369206cb636SStephen Wilson * given task for page fault accounting. 43700ec76a11SDavid Howells */ 437184d77d3fSEric W. Biederman int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm, 4372442486ecSLorenzo Stoakes unsigned long addr, void *buf, int len, unsigned int gup_flags) 43730ec76a11SDavid Howells { 43740ec76a11SDavid Howells struct vm_area_struct *vma; 43750ec76a11SDavid Howells void *old_buf = buf; 4376442486ecSLorenzo Stoakes int write = gup_flags & FOLL_WRITE; 43770ec76a11SDavid Howells 43781e426fe2SKonstantin Khlebnikov if (down_read_killable(&mm->mmap_sem)) 43791e426fe2SKonstantin Khlebnikov return 0; 43801e426fe2SKonstantin Khlebnikov 4381183ff22bSSimon Arlott /* ignore errors, just check how much was successfully transferred */ 43820ec76a11SDavid Howells while (len) { 43830ec76a11SDavid Howells int bytes, ret, offset; 43840ec76a11SDavid Howells void *maddr; 438528b2ee20SRik van Riel struct page *page = NULL; 43860ec76a11SDavid Howells 43871e987790SDave Hansen ret = get_user_pages_remote(tsk, mm, addr, 1, 43885b56d49fSLorenzo Stoakes gup_flags, &page, &vma, NULL); 438928b2ee20SRik van Riel if (ret <= 0) { 4390dbffcd03SRik van Riel #ifndef CONFIG_HAVE_IOREMAP_PROT 4391dbffcd03SRik van Riel break; 4392dbffcd03SRik van Riel #else 439328b2ee20SRik van Riel /* 439428b2ee20SRik van Riel * Check if this is a VM_IO | VM_PFNMAP VMA, which 439528b2ee20SRik van Riel * we can access using slightly different code. 439628b2ee20SRik van Riel */ 439728b2ee20SRik van Riel vma = find_vma(mm, addr); 4398fe936dfcSMichael Ellerman if (!vma || vma->vm_start > addr) 43990ec76a11SDavid Howells break; 440028b2ee20SRik van Riel if (vma->vm_ops && vma->vm_ops->access) 440128b2ee20SRik van Riel ret = vma->vm_ops->access(vma, addr, buf, 440228b2ee20SRik van Riel len, write); 440328b2ee20SRik van Riel if (ret <= 0) 440428b2ee20SRik van Riel break; 440528b2ee20SRik van Riel bytes = ret; 4406dbffcd03SRik van Riel #endif 440728b2ee20SRik van Riel } else { 44080ec76a11SDavid Howells bytes = len; 44090ec76a11SDavid Howells offset = addr & (PAGE_SIZE-1); 44100ec76a11SDavid Howells if (bytes > PAGE_SIZE-offset) 44110ec76a11SDavid Howells bytes = PAGE_SIZE-offset; 44120ec76a11SDavid Howells 44130ec76a11SDavid Howells maddr = kmap(page); 44140ec76a11SDavid Howells if (write) { 44150ec76a11SDavid Howells copy_to_user_page(vma, page, addr, 44160ec76a11SDavid Howells maddr + offset, buf, bytes); 44170ec76a11SDavid Howells set_page_dirty_lock(page); 44180ec76a11SDavid Howells } else { 44190ec76a11SDavid Howells copy_from_user_page(vma, page, addr, 44200ec76a11SDavid Howells buf, maddr + offset, bytes); 44210ec76a11SDavid Howells } 44220ec76a11SDavid Howells kunmap(page); 442309cbfeafSKirill A. Shutemov put_page(page); 442428b2ee20SRik van Riel } 44250ec76a11SDavid Howells len -= bytes; 44260ec76a11SDavid Howells buf += bytes; 44270ec76a11SDavid Howells addr += bytes; 44280ec76a11SDavid Howells } 44290ec76a11SDavid Howells up_read(&mm->mmap_sem); 44300ec76a11SDavid Howells 44310ec76a11SDavid Howells return buf - old_buf; 44320ec76a11SDavid Howells } 443303252919SAndi Kleen 44345ddd36b9SStephen Wilson /** 4435ae91dbfcSRandy Dunlap * access_remote_vm - access another process' address space 44365ddd36b9SStephen Wilson * @mm: the mm_struct of the target address space 44375ddd36b9SStephen Wilson * @addr: start address to access 44385ddd36b9SStephen Wilson * @buf: source or destination buffer 44395ddd36b9SStephen Wilson * @len: number of bytes to transfer 44406347e8d5SLorenzo Stoakes * @gup_flags: flags modifying lookup behaviour 44415ddd36b9SStephen Wilson * 44425ddd36b9SStephen Wilson * The caller must hold a reference on @mm. 4443a862f68aSMike Rapoport * 4444a862f68aSMike Rapoport * Return: number of bytes copied from source to destination. 44455ddd36b9SStephen Wilson */ 44465ddd36b9SStephen Wilson int access_remote_vm(struct mm_struct *mm, unsigned long addr, 44476347e8d5SLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 44485ddd36b9SStephen Wilson { 44496347e8d5SLorenzo Stoakes return __access_remote_vm(NULL, mm, addr, buf, len, gup_flags); 44505ddd36b9SStephen Wilson } 44515ddd36b9SStephen Wilson 445203252919SAndi Kleen /* 4453206cb636SStephen Wilson * Access another process' address space. 4454206cb636SStephen Wilson * Source/target buffer must be kernel space, 4455206cb636SStephen Wilson * Do not walk the page table directly, use get_user_pages 4456206cb636SStephen Wilson */ 4457206cb636SStephen Wilson int access_process_vm(struct task_struct *tsk, unsigned long addr, 4458f307ab6dSLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 4459206cb636SStephen Wilson { 4460206cb636SStephen Wilson struct mm_struct *mm; 4461206cb636SStephen Wilson int ret; 4462206cb636SStephen Wilson 4463206cb636SStephen Wilson mm = get_task_mm(tsk); 4464206cb636SStephen Wilson if (!mm) 4465206cb636SStephen Wilson return 0; 4466206cb636SStephen Wilson 4467f307ab6dSLorenzo Stoakes ret = __access_remote_vm(tsk, mm, addr, buf, len, gup_flags); 4468442486ecSLorenzo Stoakes 4469206cb636SStephen Wilson mmput(mm); 4470206cb636SStephen Wilson 4471206cb636SStephen Wilson return ret; 4472206cb636SStephen Wilson } 4473fcd35857SCatalin Marinas EXPORT_SYMBOL_GPL(access_process_vm); 4474206cb636SStephen Wilson 447503252919SAndi Kleen /* 447603252919SAndi Kleen * Print the name of a VMA. 447703252919SAndi Kleen */ 447803252919SAndi Kleen void print_vma_addr(char *prefix, unsigned long ip) 447903252919SAndi Kleen { 448003252919SAndi Kleen struct mm_struct *mm = current->mm; 448103252919SAndi Kleen struct vm_area_struct *vma; 448203252919SAndi Kleen 4483e8bff74aSIngo Molnar /* 44840a7f682dSMichal Hocko * we might be running from an atomic context so we cannot sleep 4485e8bff74aSIngo Molnar */ 44860a7f682dSMichal Hocko if (!down_read_trylock(&mm->mmap_sem)) 4487e8bff74aSIngo Molnar return; 4488e8bff74aSIngo Molnar 448903252919SAndi Kleen vma = find_vma(mm, ip); 449003252919SAndi Kleen if (vma && vma->vm_file) { 449103252919SAndi Kleen struct file *f = vma->vm_file; 44920a7f682dSMichal Hocko char *buf = (char *)__get_free_page(GFP_NOWAIT); 449303252919SAndi Kleen if (buf) { 44942fbc57c5SAndy Shevchenko char *p; 449503252919SAndi Kleen 44969bf39ab2SMiklos Szeredi p = file_path(f, buf, PAGE_SIZE); 449703252919SAndi Kleen if (IS_ERR(p)) 449803252919SAndi Kleen p = "?"; 44992fbc57c5SAndy Shevchenko printk("%s%s[%lx+%lx]", prefix, kbasename(p), 450003252919SAndi Kleen vma->vm_start, 450103252919SAndi Kleen vma->vm_end - vma->vm_start); 450203252919SAndi Kleen free_page((unsigned long)buf); 450303252919SAndi Kleen } 450403252919SAndi Kleen } 450551a07e50SJeff Liu up_read(&mm->mmap_sem); 450603252919SAndi Kleen } 45073ee1afa3SNick Piggin 4508662bbcb2SMichael S. Tsirkin #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP) 45099ec23531SDavid Hildenbrand void __might_fault(const char *file, int line) 45103ee1afa3SNick Piggin { 451195156f00SPeter Zijlstra /* 451295156f00SPeter Zijlstra * Some code (nfs/sunrpc) uses socket ops on kernel memory while 451395156f00SPeter Zijlstra * holding the mmap_sem, this is safe because kernel memory doesn't 451495156f00SPeter Zijlstra * get paged out, therefore we'll never actually fault, and the 451595156f00SPeter Zijlstra * below annotations will generate false positives. 451695156f00SPeter Zijlstra */ 4517db68ce10SAl Viro if (uaccess_kernel()) 451895156f00SPeter Zijlstra return; 45199ec23531SDavid Hildenbrand if (pagefault_disabled()) 4520662bbcb2SMichael S. Tsirkin return; 45219ec23531SDavid Hildenbrand __might_sleep(file, line, 0); 45229ec23531SDavid Hildenbrand #if defined(CONFIG_DEBUG_ATOMIC_SLEEP) 4523662bbcb2SMichael S. Tsirkin if (current->mm) 45243ee1afa3SNick Piggin might_lock_read(¤t->mm->mmap_sem); 45259ec23531SDavid Hildenbrand #endif 45263ee1afa3SNick Piggin } 45279ec23531SDavid Hildenbrand EXPORT_SYMBOL(__might_fault); 45283ee1afa3SNick Piggin #endif 452947ad8475SAndrea Arcangeli 453047ad8475SAndrea Arcangeli #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS) 4531c6ddfb6cSHuang Ying /* 4532c6ddfb6cSHuang Ying * Process all subpages of the specified huge page with the specified 4533c6ddfb6cSHuang Ying * operation. The target subpage will be processed last to keep its 4534c6ddfb6cSHuang Ying * cache lines hot. 4535c6ddfb6cSHuang Ying */ 4536c6ddfb6cSHuang Ying static inline void process_huge_page( 4537c6ddfb6cSHuang Ying unsigned long addr_hint, unsigned int pages_per_huge_page, 4538c6ddfb6cSHuang Ying void (*process_subpage)(unsigned long addr, int idx, void *arg), 4539c6ddfb6cSHuang Ying void *arg) 4540c6ddfb6cSHuang Ying { 4541c6ddfb6cSHuang Ying int i, n, base, l; 4542c6ddfb6cSHuang Ying unsigned long addr = addr_hint & 4543c6ddfb6cSHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 4544c6ddfb6cSHuang Ying 4545c6ddfb6cSHuang Ying /* Process target subpage last to keep its cache lines hot */ 4546c6ddfb6cSHuang Ying might_sleep(); 4547c6ddfb6cSHuang Ying n = (addr_hint - addr) / PAGE_SIZE; 4548c6ddfb6cSHuang Ying if (2 * n <= pages_per_huge_page) { 4549c6ddfb6cSHuang Ying /* If target subpage in first half of huge page */ 4550c6ddfb6cSHuang Ying base = 0; 4551c6ddfb6cSHuang Ying l = n; 4552c6ddfb6cSHuang Ying /* Process subpages at the end of huge page */ 4553c6ddfb6cSHuang Ying for (i = pages_per_huge_page - 1; i >= 2 * n; i--) { 4554c6ddfb6cSHuang Ying cond_resched(); 4555c6ddfb6cSHuang Ying process_subpage(addr + i * PAGE_SIZE, i, arg); 4556c6ddfb6cSHuang Ying } 4557c6ddfb6cSHuang Ying } else { 4558c6ddfb6cSHuang Ying /* If target subpage in second half of huge page */ 4559c6ddfb6cSHuang Ying base = pages_per_huge_page - 2 * (pages_per_huge_page - n); 4560c6ddfb6cSHuang Ying l = pages_per_huge_page - n; 4561c6ddfb6cSHuang Ying /* Process subpages at the begin of huge page */ 4562c6ddfb6cSHuang Ying for (i = 0; i < base; i++) { 4563c6ddfb6cSHuang Ying cond_resched(); 4564c6ddfb6cSHuang Ying process_subpage(addr + i * PAGE_SIZE, i, arg); 4565c6ddfb6cSHuang Ying } 4566c6ddfb6cSHuang Ying } 4567c6ddfb6cSHuang Ying /* 4568c6ddfb6cSHuang Ying * Process remaining subpages in left-right-left-right pattern 4569c6ddfb6cSHuang Ying * towards the target subpage 4570c6ddfb6cSHuang Ying */ 4571c6ddfb6cSHuang Ying for (i = 0; i < l; i++) { 4572c6ddfb6cSHuang Ying int left_idx = base + i; 4573c6ddfb6cSHuang Ying int right_idx = base + 2 * l - 1 - i; 4574c6ddfb6cSHuang Ying 4575c6ddfb6cSHuang Ying cond_resched(); 4576c6ddfb6cSHuang Ying process_subpage(addr + left_idx * PAGE_SIZE, left_idx, arg); 4577c6ddfb6cSHuang Ying cond_resched(); 4578c6ddfb6cSHuang Ying process_subpage(addr + right_idx * PAGE_SIZE, right_idx, arg); 4579c6ddfb6cSHuang Ying } 4580c6ddfb6cSHuang Ying } 4581c6ddfb6cSHuang Ying 458247ad8475SAndrea Arcangeli static void clear_gigantic_page(struct page *page, 458347ad8475SAndrea Arcangeli unsigned long addr, 458447ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 458547ad8475SAndrea Arcangeli { 458647ad8475SAndrea Arcangeli int i; 458747ad8475SAndrea Arcangeli struct page *p = page; 458847ad8475SAndrea Arcangeli 458947ad8475SAndrea Arcangeli might_sleep(); 459047ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; 459147ad8475SAndrea Arcangeli i++, p = mem_map_next(p, page, i)) { 459247ad8475SAndrea Arcangeli cond_resched(); 459347ad8475SAndrea Arcangeli clear_user_highpage(p, addr + i * PAGE_SIZE); 459447ad8475SAndrea Arcangeli } 459547ad8475SAndrea Arcangeli } 4596c6ddfb6cSHuang Ying 4597c6ddfb6cSHuang Ying static void clear_subpage(unsigned long addr, int idx, void *arg) 4598c6ddfb6cSHuang Ying { 4599c6ddfb6cSHuang Ying struct page *page = arg; 4600c6ddfb6cSHuang Ying 4601c6ddfb6cSHuang Ying clear_user_highpage(page + idx, addr); 4602c6ddfb6cSHuang Ying } 4603c6ddfb6cSHuang Ying 460447ad8475SAndrea Arcangeli void clear_huge_page(struct page *page, 4605c79b57e4SHuang Ying unsigned long addr_hint, unsigned int pages_per_huge_page) 460647ad8475SAndrea Arcangeli { 4607c79b57e4SHuang Ying unsigned long addr = addr_hint & 4608c79b57e4SHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 460947ad8475SAndrea Arcangeli 461047ad8475SAndrea Arcangeli if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { 461147ad8475SAndrea Arcangeli clear_gigantic_page(page, addr, pages_per_huge_page); 461247ad8475SAndrea Arcangeli return; 461347ad8475SAndrea Arcangeli } 461447ad8475SAndrea Arcangeli 4615c6ddfb6cSHuang Ying process_huge_page(addr_hint, pages_per_huge_page, clear_subpage, page); 461647ad8475SAndrea Arcangeli } 461747ad8475SAndrea Arcangeli 461847ad8475SAndrea Arcangeli static void copy_user_gigantic_page(struct page *dst, struct page *src, 461947ad8475SAndrea Arcangeli unsigned long addr, 462047ad8475SAndrea Arcangeli struct vm_area_struct *vma, 462147ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 462247ad8475SAndrea Arcangeli { 462347ad8475SAndrea Arcangeli int i; 462447ad8475SAndrea Arcangeli struct page *dst_base = dst; 462547ad8475SAndrea Arcangeli struct page *src_base = src; 462647ad8475SAndrea Arcangeli 462747ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; ) { 462847ad8475SAndrea Arcangeli cond_resched(); 462947ad8475SAndrea Arcangeli copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma); 463047ad8475SAndrea Arcangeli 463147ad8475SAndrea Arcangeli i++; 463247ad8475SAndrea Arcangeli dst = mem_map_next(dst, dst_base, i); 463347ad8475SAndrea Arcangeli src = mem_map_next(src, src_base, i); 463447ad8475SAndrea Arcangeli } 463547ad8475SAndrea Arcangeli } 463647ad8475SAndrea Arcangeli 4637c9f4cd71SHuang Ying struct copy_subpage_arg { 4638c9f4cd71SHuang Ying struct page *dst; 4639c9f4cd71SHuang Ying struct page *src; 4640c9f4cd71SHuang Ying struct vm_area_struct *vma; 4641c9f4cd71SHuang Ying }; 4642c9f4cd71SHuang Ying 4643c9f4cd71SHuang Ying static void copy_subpage(unsigned long addr, int idx, void *arg) 4644c9f4cd71SHuang Ying { 4645c9f4cd71SHuang Ying struct copy_subpage_arg *copy_arg = arg; 4646c9f4cd71SHuang Ying 4647c9f4cd71SHuang Ying copy_user_highpage(copy_arg->dst + idx, copy_arg->src + idx, 4648c9f4cd71SHuang Ying addr, copy_arg->vma); 4649c9f4cd71SHuang Ying } 4650c9f4cd71SHuang Ying 465147ad8475SAndrea Arcangeli void copy_user_huge_page(struct page *dst, struct page *src, 4652c9f4cd71SHuang Ying unsigned long addr_hint, struct vm_area_struct *vma, 465347ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 465447ad8475SAndrea Arcangeli { 4655c9f4cd71SHuang Ying unsigned long addr = addr_hint & 4656c9f4cd71SHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 4657c9f4cd71SHuang Ying struct copy_subpage_arg arg = { 4658c9f4cd71SHuang Ying .dst = dst, 4659c9f4cd71SHuang Ying .src = src, 4660c9f4cd71SHuang Ying .vma = vma, 4661c9f4cd71SHuang Ying }; 466247ad8475SAndrea Arcangeli 466347ad8475SAndrea Arcangeli if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { 466447ad8475SAndrea Arcangeli copy_user_gigantic_page(dst, src, addr, vma, 466547ad8475SAndrea Arcangeli pages_per_huge_page); 466647ad8475SAndrea Arcangeli return; 466747ad8475SAndrea Arcangeli } 466847ad8475SAndrea Arcangeli 4669c9f4cd71SHuang Ying process_huge_page(addr_hint, pages_per_huge_page, copy_subpage, &arg); 467047ad8475SAndrea Arcangeli } 4671fa4d75c1SMike Kravetz 4672fa4d75c1SMike Kravetz long copy_huge_page_from_user(struct page *dst_page, 4673fa4d75c1SMike Kravetz const void __user *usr_src, 4674810a56b9SMike Kravetz unsigned int pages_per_huge_page, 4675810a56b9SMike Kravetz bool allow_pagefault) 4676fa4d75c1SMike Kravetz { 4677fa4d75c1SMike Kravetz void *src = (void *)usr_src; 4678fa4d75c1SMike Kravetz void *page_kaddr; 4679fa4d75c1SMike Kravetz unsigned long i, rc = 0; 4680fa4d75c1SMike Kravetz unsigned long ret_val = pages_per_huge_page * PAGE_SIZE; 4681fa4d75c1SMike Kravetz 4682fa4d75c1SMike Kravetz for (i = 0; i < pages_per_huge_page; i++) { 4683810a56b9SMike Kravetz if (allow_pagefault) 4684810a56b9SMike Kravetz page_kaddr = kmap(dst_page + i); 4685810a56b9SMike Kravetz else 4686fa4d75c1SMike Kravetz page_kaddr = kmap_atomic(dst_page + i); 4687fa4d75c1SMike Kravetz rc = copy_from_user(page_kaddr, 4688fa4d75c1SMike Kravetz (const void __user *)(src + i * PAGE_SIZE), 4689fa4d75c1SMike Kravetz PAGE_SIZE); 4690810a56b9SMike Kravetz if (allow_pagefault) 4691810a56b9SMike Kravetz kunmap(dst_page + i); 4692810a56b9SMike Kravetz else 4693fa4d75c1SMike Kravetz kunmap_atomic(page_kaddr); 4694fa4d75c1SMike Kravetz 4695fa4d75c1SMike Kravetz ret_val -= (PAGE_SIZE - rc); 4696fa4d75c1SMike Kravetz if (rc) 4697fa4d75c1SMike Kravetz break; 4698fa4d75c1SMike Kravetz 4699fa4d75c1SMike Kravetz cond_resched(); 4700fa4d75c1SMike Kravetz } 4701fa4d75c1SMike Kravetz return ret_val; 4702fa4d75c1SMike Kravetz } 470347ad8475SAndrea Arcangeli #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */ 470449076ec2SKirill A. Shutemov 470540b64acdSOlof Johansson #if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS 4706b35f1819SKirill A. Shutemov 4707b35f1819SKirill A. Shutemov static struct kmem_cache *page_ptl_cachep; 4708b35f1819SKirill A. Shutemov 4709b35f1819SKirill A. Shutemov void __init ptlock_cache_init(void) 4710b35f1819SKirill A. Shutemov { 4711b35f1819SKirill A. Shutemov page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0, 4712b35f1819SKirill A. Shutemov SLAB_PANIC, NULL); 4713b35f1819SKirill A. Shutemov } 4714b35f1819SKirill A. Shutemov 4715539edb58SPeter Zijlstra bool ptlock_alloc(struct page *page) 471649076ec2SKirill A. Shutemov { 471749076ec2SKirill A. Shutemov spinlock_t *ptl; 471849076ec2SKirill A. Shutemov 4719b35f1819SKirill A. Shutemov ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL); 472049076ec2SKirill A. Shutemov if (!ptl) 472149076ec2SKirill A. Shutemov return false; 4722539edb58SPeter Zijlstra page->ptl = ptl; 472349076ec2SKirill A. Shutemov return true; 472449076ec2SKirill A. Shutemov } 472549076ec2SKirill A. Shutemov 4726539edb58SPeter Zijlstra void ptlock_free(struct page *page) 472749076ec2SKirill A. Shutemov { 4728b35f1819SKirill A. Shutemov kmem_cache_free(page_ptl_cachep, page->ptl); 472949076ec2SKirill A. Shutemov } 473049076ec2SKirill A. Shutemov #endif 4731