11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/mm/memory.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds 51da177e4SLinus Torvalds */ 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds /* 81da177e4SLinus Torvalds * demand-loading started 01.12.91 - seems it is high on the list of 91da177e4SLinus Torvalds * things wanted, and it should be easy to implement. - Linus 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds /* 131da177e4SLinus Torvalds * Ok, demand-loading was easy, shared pages a little bit tricker. Shared 141da177e4SLinus Torvalds * pages started 02.12.91, seems to work. - Linus. 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * Tested sharing by executing about 30 /bin/sh: under the old kernel it 171da177e4SLinus Torvalds * would have taken more than the 6M I have free, but it worked well as 181da177e4SLinus Torvalds * far as I could see. 191da177e4SLinus Torvalds * 201da177e4SLinus Torvalds * Also corrected some "invalidate()"s - I wasn't doing enough of them. 211da177e4SLinus Torvalds */ 221da177e4SLinus Torvalds 231da177e4SLinus Torvalds /* 241da177e4SLinus Torvalds * Real VM (paging to/from disk) started 18.12.91. Much more work and 251da177e4SLinus Torvalds * thought has to go into this. Oh, well.. 261da177e4SLinus Torvalds * 19.12.91 - works, somewhat. Sometimes I get faults, don't know why. 271da177e4SLinus Torvalds * Found it. Everything seems to work now. 281da177e4SLinus Torvalds * 20.12.91 - Ok, making the swap-device changeable like the root. 291da177e4SLinus Torvalds */ 301da177e4SLinus Torvalds 311da177e4SLinus Torvalds /* 321da177e4SLinus Torvalds * 05.04.94 - Multi-page memory management added for v1.1. 331da177e4SLinus Torvalds * Idea by Alex Bligh (alex@cconcepts.co.uk) 341da177e4SLinus Torvalds * 351da177e4SLinus Torvalds * 16.07.99 - Support of BIGMEM added by Gerhard Wichert, Siemens AG 361da177e4SLinus Torvalds * (Gerhard.Wichert@pdb.siemens.de) 371da177e4SLinus Torvalds * 381da177e4SLinus Torvalds * Aug/Sep 2004 Changed to four level page tables (Andi Kleen) 391da177e4SLinus Torvalds */ 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds #include <linux/kernel_stat.h> 421da177e4SLinus Torvalds #include <linux/mm.h> 436e84f315SIngo Molnar #include <linux/sched/mm.h> 44f7ccbae4SIngo Molnar #include <linux/sched/coredump.h> 456a3827d7SIngo Molnar #include <linux/sched/numa_balancing.h> 4629930025SIngo Molnar #include <linux/sched/task.h> 471da177e4SLinus Torvalds #include <linux/hugetlb.h> 481da177e4SLinus Torvalds #include <linux/mman.h> 491da177e4SLinus Torvalds #include <linux/swap.h> 501da177e4SLinus Torvalds #include <linux/highmem.h> 511da177e4SLinus Torvalds #include <linux/pagemap.h> 525042db43SJérôme Glisse #include <linux/memremap.h> 539a840895SHugh Dickins #include <linux/ksm.h> 541da177e4SLinus Torvalds #include <linux/rmap.h> 55b95f1b31SPaul Gortmaker #include <linux/export.h> 560ff92245SShailabh Nagar #include <linux/delayacct.h> 571da177e4SLinus Torvalds #include <linux/init.h> 5801c8f1c4SDan Williams #include <linux/pfn_t.h> 59edc79b2aSPeter Zijlstra #include <linux/writeback.h> 608a9f3ccdSBalbir Singh #include <linux/memcontrol.h> 61cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h> 623dc14741SHugh Dickins #include <linux/swapops.h> 633dc14741SHugh Dickins #include <linux/elf.h> 645a0e3ad6STejun Heo #include <linux/gfp.h> 654daae3b4SMel Gorman #include <linux/migrate.h> 662fbc57c5SAndy Shevchenko #include <linux/string.h> 670abdd7a8SDan Williams #include <linux/dma-debug.h> 681592eef0SKirill A. Shutemov #include <linux/debugfs.h> 696b251fc9SAndrea Arcangeli #include <linux/userfaultfd_k.h> 70bc2466e4SJan Kara #include <linux/dax.h> 716b31d595SMichal Hocko #include <linux/oom.h> 721da177e4SLinus Torvalds 736952b61dSAlexey Dobriyan #include <asm/io.h> 7433a709b2SDave Hansen #include <asm/mmu_context.h> 751da177e4SLinus Torvalds #include <asm/pgalloc.h> 767c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 771da177e4SLinus Torvalds #include <asm/tlb.h> 781da177e4SLinus Torvalds #include <asm/tlbflush.h> 791da177e4SLinus Torvalds #include <asm/pgtable.h> 801da177e4SLinus Torvalds 8142b77728SJan Beulich #include "internal.h" 8242b77728SJan Beulich 83af27d940SArnd Bergmann #if defined(LAST_CPUPID_NOT_IN_PAGE_FLAGS) && !defined(CONFIG_COMPILE_TEST) 8490572890SPeter Zijlstra #warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid. 8575980e97SPeter Zijlstra #endif 8675980e97SPeter Zijlstra 87d41dee36SAndy Whitcroft #ifndef CONFIG_NEED_MULTIPLE_NODES 881da177e4SLinus Torvalds /* use the per-pgdat data instead for discontigmem - mbligh */ 891da177e4SLinus Torvalds unsigned long max_mapnr; 901da177e4SLinus Torvalds EXPORT_SYMBOL(max_mapnr); 91166f61b9STobin C Harding 92166f61b9STobin C Harding struct page *mem_map; 931da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map); 941da177e4SLinus Torvalds #endif 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds /* 971da177e4SLinus Torvalds * A number of key systems in x86 including ioremap() rely on the assumption 981da177e4SLinus Torvalds * that high_memory defines the upper bound on direct map memory, then end 991da177e4SLinus Torvalds * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and 1001da177e4SLinus Torvalds * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL 1011da177e4SLinus Torvalds * and ZONE_HIGHMEM. 1021da177e4SLinus Torvalds */ 1031da177e4SLinus Torvalds void *high_memory; 1041da177e4SLinus Torvalds EXPORT_SYMBOL(high_memory); 1051da177e4SLinus Torvalds 10632a93233SIngo Molnar /* 10732a93233SIngo Molnar * Randomize the address space (stacks, mmaps, brk, etc.). 10832a93233SIngo Molnar * 10932a93233SIngo Molnar * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization, 11032a93233SIngo Molnar * as ancient (libc5 based) binaries can segfault. ) 11132a93233SIngo Molnar */ 11232a93233SIngo Molnar int randomize_va_space __read_mostly = 11332a93233SIngo Molnar #ifdef CONFIG_COMPAT_BRK 11432a93233SIngo Molnar 1; 11532a93233SIngo Molnar #else 11632a93233SIngo Molnar 2; 11732a93233SIngo Molnar #endif 118a62eaf15SAndi Kleen 119a62eaf15SAndi Kleen static int __init disable_randmaps(char *s) 120a62eaf15SAndi Kleen { 121a62eaf15SAndi Kleen randomize_va_space = 0; 1229b41046cSOGAWA Hirofumi return 1; 123a62eaf15SAndi Kleen } 124a62eaf15SAndi Kleen __setup("norandmaps", disable_randmaps); 125a62eaf15SAndi Kleen 12662eede62SHugh Dickins unsigned long zero_pfn __read_mostly; 1270b70068eSArd Biesheuvel EXPORT_SYMBOL(zero_pfn); 1280b70068eSArd Biesheuvel 129166f61b9STobin C Harding unsigned long highest_memmap_pfn __read_mostly; 130166f61b9STobin C Harding 131a13ea5b7SHugh Dickins /* 132a13ea5b7SHugh Dickins * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init() 133a13ea5b7SHugh Dickins */ 134a13ea5b7SHugh Dickins static int __init init_zero_pfn(void) 135a13ea5b7SHugh Dickins { 136a13ea5b7SHugh Dickins zero_pfn = page_to_pfn(ZERO_PAGE(0)); 137a13ea5b7SHugh Dickins return 0; 138a13ea5b7SHugh Dickins } 139a13ea5b7SHugh Dickins core_initcall(init_zero_pfn); 140a62eaf15SAndi Kleen 141d559db08SKAMEZAWA Hiroyuki 14234e55232SKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING) 14334e55232SKAMEZAWA Hiroyuki 144ea48cf78SDavid Rientjes void sync_mm_rss(struct mm_struct *mm) 14534e55232SKAMEZAWA Hiroyuki { 14634e55232SKAMEZAWA Hiroyuki int i; 14734e55232SKAMEZAWA Hiroyuki 14834e55232SKAMEZAWA Hiroyuki for (i = 0; i < NR_MM_COUNTERS; i++) { 14905af2e10SDavid Rientjes if (current->rss_stat.count[i]) { 15005af2e10SDavid Rientjes add_mm_counter(mm, i, current->rss_stat.count[i]); 15105af2e10SDavid Rientjes current->rss_stat.count[i] = 0; 15234e55232SKAMEZAWA Hiroyuki } 15334e55232SKAMEZAWA Hiroyuki } 15405af2e10SDavid Rientjes current->rss_stat.events = 0; 15534e55232SKAMEZAWA Hiroyuki } 15634e55232SKAMEZAWA Hiroyuki 15734e55232SKAMEZAWA Hiroyuki static void add_mm_counter_fast(struct mm_struct *mm, int member, int val) 15834e55232SKAMEZAWA Hiroyuki { 15934e55232SKAMEZAWA Hiroyuki struct task_struct *task = current; 16034e55232SKAMEZAWA Hiroyuki 16134e55232SKAMEZAWA Hiroyuki if (likely(task->mm == mm)) 16234e55232SKAMEZAWA Hiroyuki task->rss_stat.count[member] += val; 16334e55232SKAMEZAWA Hiroyuki else 16434e55232SKAMEZAWA Hiroyuki add_mm_counter(mm, member, val); 16534e55232SKAMEZAWA Hiroyuki } 16634e55232SKAMEZAWA Hiroyuki #define inc_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, 1) 16734e55232SKAMEZAWA Hiroyuki #define dec_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, -1) 16834e55232SKAMEZAWA Hiroyuki 16934e55232SKAMEZAWA Hiroyuki /* sync counter once per 64 page faults */ 17034e55232SKAMEZAWA Hiroyuki #define TASK_RSS_EVENTS_THRESH (64) 17134e55232SKAMEZAWA Hiroyuki static void check_sync_rss_stat(struct task_struct *task) 17234e55232SKAMEZAWA Hiroyuki { 17334e55232SKAMEZAWA Hiroyuki if (unlikely(task != current)) 17434e55232SKAMEZAWA Hiroyuki return; 17534e55232SKAMEZAWA Hiroyuki if (unlikely(task->rss_stat.events++ > TASK_RSS_EVENTS_THRESH)) 176ea48cf78SDavid Rientjes sync_mm_rss(task->mm); 17734e55232SKAMEZAWA Hiroyuki } 1789547d01bSPeter Zijlstra #else /* SPLIT_RSS_COUNTING */ 17934e55232SKAMEZAWA Hiroyuki 18034e55232SKAMEZAWA Hiroyuki #define inc_mm_counter_fast(mm, member) inc_mm_counter(mm, member) 18134e55232SKAMEZAWA Hiroyuki #define dec_mm_counter_fast(mm, member) dec_mm_counter(mm, member) 18234e55232SKAMEZAWA Hiroyuki 18334e55232SKAMEZAWA Hiroyuki static void check_sync_rss_stat(struct task_struct *task) 18434e55232SKAMEZAWA Hiroyuki { 18534e55232SKAMEZAWA Hiroyuki } 18634e55232SKAMEZAWA Hiroyuki 1879547d01bSPeter Zijlstra #endif /* SPLIT_RSS_COUNTING */ 1889547d01bSPeter Zijlstra 1891da177e4SLinus Torvalds /* 1901da177e4SLinus Torvalds * Note: this doesn't free the actual pages themselves. That 1911da177e4SLinus Torvalds * has been handled earlier when unmapping all the memory regions. 1921da177e4SLinus Torvalds */ 1939e1b32caSBenjamin Herrenschmidt static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd, 1949e1b32caSBenjamin Herrenschmidt unsigned long addr) 1951da177e4SLinus Torvalds { 1962f569afdSMartin Schwidefsky pgtable_t token = pmd_pgtable(*pmd); 1971da177e4SLinus Torvalds pmd_clear(pmd); 1989e1b32caSBenjamin Herrenschmidt pte_free_tlb(tlb, token, addr); 199c4812909SKirill A. Shutemov mm_dec_nr_ptes(tlb->mm); 2001da177e4SLinus Torvalds } 2011da177e4SLinus Torvalds 202e0da382cSHugh Dickins static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud, 203e0da382cSHugh Dickins unsigned long addr, unsigned long end, 204e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 2051da177e4SLinus Torvalds { 2061da177e4SLinus Torvalds pmd_t *pmd; 2071da177e4SLinus Torvalds unsigned long next; 208e0da382cSHugh Dickins unsigned long start; 2091da177e4SLinus Torvalds 210e0da382cSHugh Dickins start = addr; 2111da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 2121da177e4SLinus Torvalds do { 2131da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 2141da177e4SLinus Torvalds if (pmd_none_or_clear_bad(pmd)) 2151da177e4SLinus Torvalds continue; 2169e1b32caSBenjamin Herrenschmidt free_pte_range(tlb, pmd, addr); 2171da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 2181da177e4SLinus Torvalds 219e0da382cSHugh Dickins start &= PUD_MASK; 220e0da382cSHugh Dickins if (start < floor) 221e0da382cSHugh Dickins return; 222e0da382cSHugh Dickins if (ceiling) { 223e0da382cSHugh Dickins ceiling &= PUD_MASK; 224e0da382cSHugh Dickins if (!ceiling) 225e0da382cSHugh Dickins return; 2261da177e4SLinus Torvalds } 227e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 228e0da382cSHugh Dickins return; 229e0da382cSHugh Dickins 230e0da382cSHugh Dickins pmd = pmd_offset(pud, start); 231e0da382cSHugh Dickins pud_clear(pud); 2329e1b32caSBenjamin Herrenschmidt pmd_free_tlb(tlb, pmd, start); 233dc6c9a35SKirill A. Shutemov mm_dec_nr_pmds(tlb->mm); 2341da177e4SLinus Torvalds } 2351da177e4SLinus Torvalds 236c2febafcSKirill A. Shutemov static inline void free_pud_range(struct mmu_gather *tlb, p4d_t *p4d, 237e0da382cSHugh Dickins unsigned long addr, unsigned long end, 238e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 2391da177e4SLinus Torvalds { 2401da177e4SLinus Torvalds pud_t *pud; 2411da177e4SLinus Torvalds unsigned long next; 242e0da382cSHugh Dickins unsigned long start; 2431da177e4SLinus Torvalds 244e0da382cSHugh Dickins start = addr; 245c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 2461da177e4SLinus Torvalds do { 2471da177e4SLinus Torvalds next = pud_addr_end(addr, end); 2481da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 2491da177e4SLinus Torvalds continue; 250e0da382cSHugh Dickins free_pmd_range(tlb, pud, addr, next, floor, ceiling); 2511da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 2521da177e4SLinus Torvalds 253c2febafcSKirill A. Shutemov start &= P4D_MASK; 254c2febafcSKirill A. Shutemov if (start < floor) 255c2febafcSKirill A. Shutemov return; 256c2febafcSKirill A. Shutemov if (ceiling) { 257c2febafcSKirill A. Shutemov ceiling &= P4D_MASK; 258c2febafcSKirill A. Shutemov if (!ceiling) 259c2febafcSKirill A. Shutemov return; 260c2febafcSKirill A. Shutemov } 261c2febafcSKirill A. Shutemov if (end - 1 > ceiling - 1) 262c2febafcSKirill A. Shutemov return; 263c2febafcSKirill A. Shutemov 264c2febafcSKirill A. Shutemov pud = pud_offset(p4d, start); 265c2febafcSKirill A. Shutemov p4d_clear(p4d); 266c2febafcSKirill A. Shutemov pud_free_tlb(tlb, pud, start); 267b4e98d9aSKirill A. Shutemov mm_dec_nr_puds(tlb->mm); 268c2febafcSKirill A. Shutemov } 269c2febafcSKirill A. Shutemov 270c2febafcSKirill A. Shutemov static inline void free_p4d_range(struct mmu_gather *tlb, pgd_t *pgd, 271c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 272c2febafcSKirill A. Shutemov unsigned long floor, unsigned long ceiling) 273c2febafcSKirill A. Shutemov { 274c2febafcSKirill A. Shutemov p4d_t *p4d; 275c2febafcSKirill A. Shutemov unsigned long next; 276c2febafcSKirill A. Shutemov unsigned long start; 277c2febafcSKirill A. Shutemov 278c2febafcSKirill A. Shutemov start = addr; 279c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 280c2febafcSKirill A. Shutemov do { 281c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 282c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(p4d)) 283c2febafcSKirill A. Shutemov continue; 284c2febafcSKirill A. Shutemov free_pud_range(tlb, p4d, addr, next, floor, ceiling); 285c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 286c2febafcSKirill A. Shutemov 287e0da382cSHugh Dickins start &= PGDIR_MASK; 288e0da382cSHugh Dickins if (start < floor) 289e0da382cSHugh Dickins return; 290e0da382cSHugh Dickins if (ceiling) { 291e0da382cSHugh Dickins ceiling &= PGDIR_MASK; 292e0da382cSHugh Dickins if (!ceiling) 293e0da382cSHugh Dickins return; 2941da177e4SLinus Torvalds } 295e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 296e0da382cSHugh Dickins return; 297e0da382cSHugh Dickins 298c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, start); 299e0da382cSHugh Dickins pgd_clear(pgd); 300c2febafcSKirill A. Shutemov p4d_free_tlb(tlb, p4d, start); 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds /* 304e0da382cSHugh Dickins * This function frees user-level page tables of a process. 3051da177e4SLinus Torvalds */ 30642b77728SJan Beulich void free_pgd_range(struct mmu_gather *tlb, 307e0da382cSHugh Dickins unsigned long addr, unsigned long end, 308e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 3091da177e4SLinus Torvalds { 3101da177e4SLinus Torvalds pgd_t *pgd; 3111da177e4SLinus Torvalds unsigned long next; 3121da177e4SLinus Torvalds 313e0da382cSHugh Dickins /* 314e0da382cSHugh Dickins * The next few lines have given us lots of grief... 315e0da382cSHugh Dickins * 316e0da382cSHugh Dickins * Why are we testing PMD* at this top level? Because often 317e0da382cSHugh Dickins * there will be no work to do at all, and we'd prefer not to 318e0da382cSHugh Dickins * go all the way down to the bottom just to discover that. 319e0da382cSHugh Dickins * 320e0da382cSHugh Dickins * Why all these "- 1"s? Because 0 represents both the bottom 321e0da382cSHugh Dickins * of the address space and the top of it (using -1 for the 322e0da382cSHugh Dickins * top wouldn't help much: the masks would do the wrong thing). 323e0da382cSHugh Dickins * The rule is that addr 0 and floor 0 refer to the bottom of 324e0da382cSHugh Dickins * the address space, but end 0 and ceiling 0 refer to the top 325e0da382cSHugh Dickins * Comparisons need to use "end - 1" and "ceiling - 1" (though 326e0da382cSHugh Dickins * that end 0 case should be mythical). 327e0da382cSHugh Dickins * 328e0da382cSHugh Dickins * Wherever addr is brought up or ceiling brought down, we must 329e0da382cSHugh Dickins * be careful to reject "the opposite 0" before it confuses the 330e0da382cSHugh Dickins * subsequent tests. But what about where end is brought down 331e0da382cSHugh Dickins * by PMD_SIZE below? no, end can't go down to 0 there. 332e0da382cSHugh Dickins * 333e0da382cSHugh Dickins * Whereas we round start (addr) and ceiling down, by different 334e0da382cSHugh Dickins * masks at different levels, in order to test whether a table 335e0da382cSHugh Dickins * now has no other vmas using it, so can be freed, we don't 336e0da382cSHugh Dickins * bother to round floor or end up - the tests don't need that. 337e0da382cSHugh Dickins */ 338e0da382cSHugh Dickins 339e0da382cSHugh Dickins addr &= PMD_MASK; 340e0da382cSHugh Dickins if (addr < floor) { 341e0da382cSHugh Dickins addr += PMD_SIZE; 342e0da382cSHugh Dickins if (!addr) 343e0da382cSHugh Dickins return; 344e0da382cSHugh Dickins } 345e0da382cSHugh Dickins if (ceiling) { 346e0da382cSHugh Dickins ceiling &= PMD_MASK; 347e0da382cSHugh Dickins if (!ceiling) 348e0da382cSHugh Dickins return; 349e0da382cSHugh Dickins } 350e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 351e0da382cSHugh Dickins end -= PMD_SIZE; 352e0da382cSHugh Dickins if (addr > end - 1) 353e0da382cSHugh Dickins return; 35407e32661SAneesh Kumar K.V /* 35507e32661SAneesh Kumar K.V * We add page table cache pages with PAGE_SIZE, 35607e32661SAneesh Kumar K.V * (see pte_free_tlb()), flush the tlb if we need 35707e32661SAneesh Kumar K.V */ 35807e32661SAneesh Kumar K.V tlb_remove_check_page_size_change(tlb, PAGE_SIZE); 35942b77728SJan Beulich pgd = pgd_offset(tlb->mm, addr); 3601da177e4SLinus Torvalds do { 3611da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 3621da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 3631da177e4SLinus Torvalds continue; 364c2febafcSKirill A. Shutemov free_p4d_range(tlb, pgd, addr, next, floor, ceiling); 3651da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 366e0da382cSHugh Dickins } 367e0da382cSHugh Dickins 36842b77728SJan Beulich void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma, 369e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 370e0da382cSHugh Dickins { 371e0da382cSHugh Dickins while (vma) { 372e0da382cSHugh Dickins struct vm_area_struct *next = vma->vm_next; 373e0da382cSHugh Dickins unsigned long addr = vma->vm_start; 374e0da382cSHugh Dickins 3758f4f8c16SHugh Dickins /* 37625d9e2d1Snpiggin@suse.de * Hide vma from rmap and truncate_pagecache before freeing 37725d9e2d1Snpiggin@suse.de * pgtables 3788f4f8c16SHugh Dickins */ 3795beb4930SRik van Riel unlink_anon_vmas(vma); 3808f4f8c16SHugh Dickins unlink_file_vma(vma); 3818f4f8c16SHugh Dickins 3829da61aefSDavid Gibson if (is_vm_hugetlb_page(vma)) { 3833bf5ee95SHugh Dickins hugetlb_free_pgd_range(tlb, addr, vma->vm_end, 3843bf5ee95SHugh Dickins floor, next ? next->vm_start : ceiling); 3853bf5ee95SHugh Dickins } else { 3863bf5ee95SHugh Dickins /* 3873bf5ee95SHugh Dickins * Optimization: gather nearby vmas into one call down 3883bf5ee95SHugh Dickins */ 3893bf5ee95SHugh Dickins while (next && next->vm_start <= vma->vm_end + PMD_SIZE 3904866920bSDavid Gibson && !is_vm_hugetlb_page(next)) { 391e0da382cSHugh Dickins vma = next; 392e0da382cSHugh Dickins next = vma->vm_next; 3935beb4930SRik van Riel unlink_anon_vmas(vma); 3948f4f8c16SHugh Dickins unlink_file_vma(vma); 395e0da382cSHugh Dickins } 3963bf5ee95SHugh Dickins free_pgd_range(tlb, addr, vma->vm_end, 397e0da382cSHugh Dickins floor, next ? next->vm_start : ceiling); 3983bf5ee95SHugh Dickins } 399e0da382cSHugh Dickins vma = next; 400e0da382cSHugh Dickins } 4011da177e4SLinus Torvalds } 4021da177e4SLinus Torvalds 4033ed3a4f0SKirill A. Shutemov int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address) 4041da177e4SLinus Torvalds { 405c4088ebdSKirill A. Shutemov spinlock_t *ptl; 4062f569afdSMartin Schwidefsky pgtable_t new = pte_alloc_one(mm, address); 4071da177e4SLinus Torvalds if (!new) 4081bb3630eSHugh Dickins return -ENOMEM; 4091bb3630eSHugh Dickins 410362a61adSNick Piggin /* 411362a61adSNick Piggin * Ensure all pte setup (eg. pte page lock and page clearing) are 412362a61adSNick Piggin * visible before the pte is made visible to other CPUs by being 413362a61adSNick Piggin * put into page tables. 414362a61adSNick Piggin * 415362a61adSNick Piggin * The other side of the story is the pointer chasing in the page 416362a61adSNick Piggin * table walking code (when walking the page table without locking; 417362a61adSNick Piggin * ie. most of the time). Fortunately, these data accesses consist 418362a61adSNick Piggin * of a chain of data-dependent loads, meaning most CPUs (alpha 419362a61adSNick Piggin * being the notable exception) will already guarantee loads are 420362a61adSNick Piggin * seen in-order. See the alpha page table accessors for the 421362a61adSNick Piggin * smp_read_barrier_depends() barriers in page table walking code. 422362a61adSNick Piggin */ 423362a61adSNick Piggin smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */ 424362a61adSNick Piggin 425c4088ebdSKirill A. Shutemov ptl = pmd_lock(mm, pmd); 4268ac1f832SAndrea Arcangeli if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 427c4812909SKirill A. Shutemov mm_inc_nr_ptes(mm); 4281da177e4SLinus Torvalds pmd_populate(mm, pmd, new); 4292f569afdSMartin Schwidefsky new = NULL; 4304b471e88SKirill A. Shutemov } 431c4088ebdSKirill A. Shutemov spin_unlock(ptl); 4322f569afdSMartin Schwidefsky if (new) 4332f569afdSMartin Schwidefsky pte_free(mm, new); 4341bb3630eSHugh Dickins return 0; 4351da177e4SLinus Torvalds } 4361da177e4SLinus Torvalds 4371bb3630eSHugh Dickins int __pte_alloc_kernel(pmd_t *pmd, unsigned long address) 4381da177e4SLinus Torvalds { 4391bb3630eSHugh Dickins pte_t *new = pte_alloc_one_kernel(&init_mm, address); 4401da177e4SLinus Torvalds if (!new) 4411bb3630eSHugh Dickins return -ENOMEM; 4421da177e4SLinus Torvalds 443362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 444362a61adSNick Piggin 445872fec16SHugh Dickins spin_lock(&init_mm.page_table_lock); 4468ac1f832SAndrea Arcangeli if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 447872fec16SHugh Dickins pmd_populate_kernel(&init_mm, pmd, new); 4482f569afdSMartin Schwidefsky new = NULL; 4494b471e88SKirill A. Shutemov } 450872fec16SHugh Dickins spin_unlock(&init_mm.page_table_lock); 4512f569afdSMartin Schwidefsky if (new) 4522f569afdSMartin Schwidefsky pte_free_kernel(&init_mm, new); 4531bb3630eSHugh Dickins return 0; 4541da177e4SLinus Torvalds } 4551da177e4SLinus Torvalds 456d559db08SKAMEZAWA Hiroyuki static inline void init_rss_vec(int *rss) 457ae859762SHugh Dickins { 458d559db08SKAMEZAWA Hiroyuki memset(rss, 0, sizeof(int) * NR_MM_COUNTERS); 459d559db08SKAMEZAWA Hiroyuki } 460d559db08SKAMEZAWA Hiroyuki 461d559db08SKAMEZAWA Hiroyuki static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss) 462d559db08SKAMEZAWA Hiroyuki { 463d559db08SKAMEZAWA Hiroyuki int i; 464d559db08SKAMEZAWA Hiroyuki 46534e55232SKAMEZAWA Hiroyuki if (current->mm == mm) 46605af2e10SDavid Rientjes sync_mm_rss(mm); 467d559db08SKAMEZAWA Hiroyuki for (i = 0; i < NR_MM_COUNTERS; i++) 468d559db08SKAMEZAWA Hiroyuki if (rss[i]) 469d559db08SKAMEZAWA Hiroyuki add_mm_counter(mm, i, rss[i]); 470ae859762SHugh Dickins } 471ae859762SHugh Dickins 4721da177e4SLinus Torvalds /* 4736aab341eSLinus Torvalds * This function is called to print an error when a bad pte 4746aab341eSLinus Torvalds * is found. For example, we might have a PFN-mapped pte in 4756aab341eSLinus Torvalds * a region that doesn't allow it. 476b5810039SNick Piggin * 477b5810039SNick Piggin * The calling function must still handle the error. 478b5810039SNick Piggin */ 4793dc14741SHugh Dickins static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr, 4803dc14741SHugh Dickins pte_t pte, struct page *page) 481b5810039SNick Piggin { 4823dc14741SHugh Dickins pgd_t *pgd = pgd_offset(vma->vm_mm, addr); 483c2febafcSKirill A. Shutemov p4d_t *p4d = p4d_offset(pgd, addr); 484c2febafcSKirill A. Shutemov pud_t *pud = pud_offset(p4d, addr); 4853dc14741SHugh Dickins pmd_t *pmd = pmd_offset(pud, addr); 4863dc14741SHugh Dickins struct address_space *mapping; 4873dc14741SHugh Dickins pgoff_t index; 488d936cf9bSHugh Dickins static unsigned long resume; 489d936cf9bSHugh Dickins static unsigned long nr_shown; 490d936cf9bSHugh Dickins static unsigned long nr_unshown; 491d936cf9bSHugh Dickins 492d936cf9bSHugh Dickins /* 493d936cf9bSHugh Dickins * Allow a burst of 60 reports, then keep quiet for that minute; 494d936cf9bSHugh Dickins * or allow a steady drip of one report per second. 495d936cf9bSHugh Dickins */ 496d936cf9bSHugh Dickins if (nr_shown == 60) { 497d936cf9bSHugh Dickins if (time_before(jiffies, resume)) { 498d936cf9bSHugh Dickins nr_unshown++; 499d936cf9bSHugh Dickins return; 500d936cf9bSHugh Dickins } 501d936cf9bSHugh Dickins if (nr_unshown) { 5021170532bSJoe Perches pr_alert("BUG: Bad page map: %lu messages suppressed\n", 503d936cf9bSHugh Dickins nr_unshown); 504d936cf9bSHugh Dickins nr_unshown = 0; 505d936cf9bSHugh Dickins } 506d936cf9bSHugh Dickins nr_shown = 0; 507d936cf9bSHugh Dickins } 508d936cf9bSHugh Dickins if (nr_shown++ == 0) 509d936cf9bSHugh Dickins resume = jiffies + 60 * HZ; 5103dc14741SHugh Dickins 5113dc14741SHugh Dickins mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL; 5123dc14741SHugh Dickins index = linear_page_index(vma, addr); 5133dc14741SHugh Dickins 5141170532bSJoe Perches pr_alert("BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n", 5153dc14741SHugh Dickins current->comm, 5163dc14741SHugh Dickins (long long)pte_val(pte), (long long)pmd_val(*pmd)); 517718a3821SWu Fengguang if (page) 518f0b791a3SDave Hansen dump_page(page, "bad pte"); 5191170532bSJoe Perches pr_alert("addr:%p vm_flags:%08lx anon_vma:%p mapping:%p index:%lx\n", 5203dc14741SHugh Dickins (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index); 5212682582aSKonstantin Khlebnikov pr_alert("file:%pD fault:%pf mmap:%pf readpage:%pf\n", 5222682582aSKonstantin Khlebnikov vma->vm_file, 5232682582aSKonstantin Khlebnikov vma->vm_ops ? vma->vm_ops->fault : NULL, 5242682582aSKonstantin Khlebnikov vma->vm_file ? vma->vm_file->f_op->mmap : NULL, 5252682582aSKonstantin Khlebnikov mapping ? mapping->a_ops->readpage : NULL); 526b5810039SNick Piggin dump_stack(); 527373d4d09SRusty Russell add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); 528b5810039SNick Piggin } 529b5810039SNick Piggin 530b5810039SNick Piggin /* 5317e675137SNick Piggin * vm_normal_page -- This function gets the "struct page" associated with a pte. 5326aab341eSLinus Torvalds * 5337e675137SNick Piggin * "Special" mappings do not wish to be associated with a "struct page" (either 5347e675137SNick Piggin * it doesn't exist, or it exists but they don't want to touch it). In this 5357e675137SNick Piggin * case, NULL is returned here. "Normal" mappings do have a struct page. 536b379d790SJared Hulbert * 5377e675137SNick Piggin * There are 2 broad cases. Firstly, an architecture may define a pte_special() 5387e675137SNick Piggin * pte bit, in which case this function is trivial. Secondly, an architecture 5397e675137SNick Piggin * may not have a spare pte bit, which requires a more complicated scheme, 5407e675137SNick Piggin * described below. 5417e675137SNick Piggin * 5427e675137SNick Piggin * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a 5437e675137SNick Piggin * special mapping (even if there are underlying and valid "struct pages"). 5447e675137SNick Piggin * COWed pages of a VM_PFNMAP are always normal. 5456aab341eSLinus Torvalds * 546b379d790SJared Hulbert * The way we recognize COWed pages within VM_PFNMAP mappings is through the 547b379d790SJared Hulbert * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit 5487e675137SNick Piggin * set, and the vm_pgoff will point to the first PFN mapped: thus every special 5497e675137SNick Piggin * mapping will always honor the rule 5506aab341eSLinus Torvalds * 5516aab341eSLinus Torvalds * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT) 5526aab341eSLinus Torvalds * 5537e675137SNick Piggin * And for normal mappings this is false. 554b379d790SJared Hulbert * 5557e675137SNick Piggin * This restricts such mappings to be a linear translation from virtual address 5567e675137SNick Piggin * to pfn. To get around this restriction, we allow arbitrary mappings so long 5577e675137SNick Piggin * as the vma is not a COW mapping; in that case, we know that all ptes are 5587e675137SNick Piggin * special (because none can have been COWed). 559b379d790SJared Hulbert * 560b379d790SJared Hulbert * 5617e675137SNick Piggin * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP. 5627e675137SNick Piggin * 563b379d790SJared Hulbert * VM_MIXEDMAP mappings can likewise contain memory with or without "struct 564b379d790SJared Hulbert * page" backing, however the difference is that _all_ pages with a struct 565b379d790SJared Hulbert * page (that is, those where pfn_valid is true) are refcounted and considered 566b379d790SJared Hulbert * normal pages by the VM. The disadvantage is that pages are refcounted 567b379d790SJared Hulbert * (which can be slower and simply not an option for some PFNMAP users). The 568b379d790SJared Hulbert * advantage is that we don't have to follow the strict linearity rule of 569b379d790SJared Hulbert * PFNMAP mappings in order to support COWable mappings. 570b379d790SJared Hulbert * 571ee498ed7SHugh Dickins */ 572df6ad698SJérôme Glisse struct page *_vm_normal_page(struct vm_area_struct *vma, unsigned long addr, 573df6ad698SJérôme Glisse pte_t pte, bool with_public_device) 574ee498ed7SHugh Dickins { 57522b31eecSHugh Dickins unsigned long pfn = pte_pfn(pte); 5767e675137SNick Piggin 57700b3a331SLaurent Dufour if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL)) { 578b38af472SHugh Dickins if (likely(!pte_special(pte))) 57922b31eecSHugh Dickins goto check_pfn; 580667a0a06SDavid Vrabel if (vma->vm_ops && vma->vm_ops->find_special_page) 581667a0a06SDavid Vrabel return vma->vm_ops->find_special_page(vma, addr); 582a13ea5b7SHugh Dickins if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)) 583a13ea5b7SHugh Dickins return NULL; 584df6ad698SJérôme Glisse if (is_zero_pfn(pfn)) 585df6ad698SJérôme Glisse return NULL; 586df6ad698SJérôme Glisse 587df6ad698SJérôme Glisse /* 588df6ad698SJérôme Glisse * Device public pages are special pages (they are ZONE_DEVICE 589df6ad698SJérôme Glisse * pages but different from persistent memory). They behave 590df6ad698SJérôme Glisse * allmost like normal pages. The difference is that they are 591df6ad698SJérôme Glisse * not on the lru and thus should never be involve with any- 592df6ad698SJérôme Glisse * thing that involve lru manipulation (mlock, numa balancing, 593df6ad698SJérôme Glisse * ...). 594df6ad698SJérôme Glisse * 595df6ad698SJérôme Glisse * This is why we still want to return NULL for such page from 596df6ad698SJérôme Glisse * vm_normal_page() so that we do not have to special case all 597df6ad698SJérôme Glisse * call site of vm_normal_page(). 598df6ad698SJérôme Glisse */ 5997d790d2dSReza Arbab if (likely(pfn <= highest_memmap_pfn)) { 600df6ad698SJérôme Glisse struct page *page = pfn_to_page(pfn); 601df6ad698SJérôme Glisse 602df6ad698SJérôme Glisse if (is_device_public_page(page)) { 603df6ad698SJérôme Glisse if (with_public_device) 604df6ad698SJérôme Glisse return page; 605df6ad698SJérôme Glisse return NULL; 606df6ad698SJérôme Glisse } 607df6ad698SJérôme Glisse } 608e1fb4a08SDave Jiang 609e1fb4a08SDave Jiang if (pte_devmap(pte)) 610e1fb4a08SDave Jiang return NULL; 611e1fb4a08SDave Jiang 61222b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 6137e675137SNick Piggin return NULL; 6147e675137SNick Piggin } 6157e675137SNick Piggin 61600b3a331SLaurent Dufour /* !CONFIG_ARCH_HAS_PTE_SPECIAL case follows: */ 6177e675137SNick Piggin 618b379d790SJared Hulbert if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { 619b379d790SJared Hulbert if (vma->vm_flags & VM_MIXEDMAP) { 620b379d790SJared Hulbert if (!pfn_valid(pfn)) 621b379d790SJared Hulbert return NULL; 622b379d790SJared Hulbert goto out; 623b379d790SJared Hulbert } else { 6247e675137SNick Piggin unsigned long off; 6257e675137SNick Piggin off = (addr - vma->vm_start) >> PAGE_SHIFT; 6266aab341eSLinus Torvalds if (pfn == vma->vm_pgoff + off) 6276aab341eSLinus Torvalds return NULL; 62867121172SLinus Torvalds if (!is_cow_mapping(vma->vm_flags)) 629fb155c16SLinus Torvalds return NULL; 6306aab341eSLinus Torvalds } 631b379d790SJared Hulbert } 6326aab341eSLinus Torvalds 633b38af472SHugh Dickins if (is_zero_pfn(pfn)) 634b38af472SHugh Dickins return NULL; 63500b3a331SLaurent Dufour 63622b31eecSHugh Dickins check_pfn: 63722b31eecSHugh Dickins if (unlikely(pfn > highest_memmap_pfn)) { 63822b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 63922b31eecSHugh Dickins return NULL; 64022b31eecSHugh Dickins } 6416aab341eSLinus Torvalds 6426aab341eSLinus Torvalds /* 6437e675137SNick Piggin * NOTE! We still have PageReserved() pages in the page tables. 6447e675137SNick Piggin * eg. VDSO mappings can cause them to exist. 6456aab341eSLinus Torvalds */ 646b379d790SJared Hulbert out: 6476aab341eSLinus Torvalds return pfn_to_page(pfn); 648ee498ed7SHugh Dickins } 649ee498ed7SHugh Dickins 65028093f9fSGerald Schaefer #ifdef CONFIG_TRANSPARENT_HUGEPAGE 65128093f9fSGerald Schaefer struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr, 65228093f9fSGerald Schaefer pmd_t pmd) 65328093f9fSGerald Schaefer { 65428093f9fSGerald Schaefer unsigned long pfn = pmd_pfn(pmd); 65528093f9fSGerald Schaefer 65628093f9fSGerald Schaefer /* 65728093f9fSGerald Schaefer * There is no pmd_special() but there may be special pmds, e.g. 65828093f9fSGerald Schaefer * in a direct-access (dax) mapping, so let's just replicate the 65900b3a331SLaurent Dufour * !CONFIG_ARCH_HAS_PTE_SPECIAL case from vm_normal_page() here. 66028093f9fSGerald Schaefer */ 66128093f9fSGerald Schaefer if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { 66228093f9fSGerald Schaefer if (vma->vm_flags & VM_MIXEDMAP) { 66328093f9fSGerald Schaefer if (!pfn_valid(pfn)) 66428093f9fSGerald Schaefer return NULL; 66528093f9fSGerald Schaefer goto out; 66628093f9fSGerald Schaefer } else { 66728093f9fSGerald Schaefer unsigned long off; 66828093f9fSGerald Schaefer off = (addr - vma->vm_start) >> PAGE_SHIFT; 66928093f9fSGerald Schaefer if (pfn == vma->vm_pgoff + off) 67028093f9fSGerald Schaefer return NULL; 67128093f9fSGerald Schaefer if (!is_cow_mapping(vma->vm_flags)) 67228093f9fSGerald Schaefer return NULL; 67328093f9fSGerald Schaefer } 67428093f9fSGerald Schaefer } 67528093f9fSGerald Schaefer 676e1fb4a08SDave Jiang if (pmd_devmap(pmd)) 677e1fb4a08SDave Jiang return NULL; 67828093f9fSGerald Schaefer if (is_zero_pfn(pfn)) 67928093f9fSGerald Schaefer return NULL; 68028093f9fSGerald Schaefer if (unlikely(pfn > highest_memmap_pfn)) 68128093f9fSGerald Schaefer return NULL; 68228093f9fSGerald Schaefer 68328093f9fSGerald Schaefer /* 68428093f9fSGerald Schaefer * NOTE! We still have PageReserved() pages in the page tables. 68528093f9fSGerald Schaefer * eg. VDSO mappings can cause them to exist. 68628093f9fSGerald Schaefer */ 68728093f9fSGerald Schaefer out: 68828093f9fSGerald Schaefer return pfn_to_page(pfn); 68928093f9fSGerald Schaefer } 69028093f9fSGerald Schaefer #endif 69128093f9fSGerald Schaefer 692ee498ed7SHugh Dickins /* 6931da177e4SLinus Torvalds * copy one vm_area from one task to the other. Assumes the page tables 6941da177e4SLinus Torvalds * already present in the new task to be cleared in the whole range 6951da177e4SLinus Torvalds * covered by this vma. 6961da177e4SLinus Torvalds */ 6971da177e4SLinus Torvalds 698570a335bSHugh Dickins static inline unsigned long 6991da177e4SLinus Torvalds copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, 700b5810039SNick Piggin pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma, 7018c103762SHugh Dickins unsigned long addr, int *rss) 7021da177e4SLinus Torvalds { 703b5810039SNick Piggin unsigned long vm_flags = vma->vm_flags; 7041da177e4SLinus Torvalds pte_t pte = *src_pte; 7051da177e4SLinus Torvalds struct page *page; 7061da177e4SLinus Torvalds 7071da177e4SLinus Torvalds /* pte contains position in swap or file, so copy. */ 7081da177e4SLinus Torvalds if (unlikely(!pte_present(pte))) { 7090697212aSChristoph Lameter swp_entry_t entry = pte_to_swp_entry(pte); 7100697212aSChristoph Lameter 7112022b4d1SHugh Dickins if (likely(!non_swap_entry(entry))) { 712570a335bSHugh Dickins if (swap_duplicate(entry) < 0) 713570a335bSHugh Dickins return entry.val; 714570a335bSHugh Dickins 7151da177e4SLinus Torvalds /* make sure dst_mm is on swapoff's mmlist. */ 7161da177e4SLinus Torvalds if (unlikely(list_empty(&dst_mm->mmlist))) { 7171da177e4SLinus Torvalds spin_lock(&mmlist_lock); 718f412ac08SHugh Dickins if (list_empty(&dst_mm->mmlist)) 719f412ac08SHugh Dickins list_add(&dst_mm->mmlist, 720f412ac08SHugh Dickins &src_mm->mmlist); 7211da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 7221da177e4SLinus Torvalds } 723b084d435SKAMEZAWA Hiroyuki rss[MM_SWAPENTS]++; 7242022b4d1SHugh Dickins } else if (is_migration_entry(entry)) { 7259f9f1acdSKonstantin Khlebnikov page = migration_entry_to_page(entry); 7269f9f1acdSKonstantin Khlebnikov 727eca56ff9SJerome Marchand rss[mm_counter(page)]++; 7289f9f1acdSKonstantin Khlebnikov 7299f9f1acdSKonstantin Khlebnikov if (is_write_migration_entry(entry) && 7300697212aSChristoph Lameter is_cow_mapping(vm_flags)) { 7310697212aSChristoph Lameter /* 7329f9f1acdSKonstantin Khlebnikov * COW mappings require pages in both 7339f9f1acdSKonstantin Khlebnikov * parent and child to be set to read. 7340697212aSChristoph Lameter */ 7350697212aSChristoph Lameter make_migration_entry_read(&entry); 7360697212aSChristoph Lameter pte = swp_entry_to_pte(entry); 737c3d16e16SCyrill Gorcunov if (pte_swp_soft_dirty(*src_pte)) 738c3d16e16SCyrill Gorcunov pte = pte_swp_mksoft_dirty(pte); 7390697212aSChristoph Lameter set_pte_at(src_mm, addr, src_pte, pte); 7400697212aSChristoph Lameter } 7415042db43SJérôme Glisse } else if (is_device_private_entry(entry)) { 7425042db43SJérôme Glisse page = device_private_entry_to_page(entry); 7435042db43SJérôme Glisse 7445042db43SJérôme Glisse /* 7455042db43SJérôme Glisse * Update rss count even for unaddressable pages, as 7465042db43SJérôme Glisse * they should treated just like normal pages in this 7475042db43SJérôme Glisse * respect. 7485042db43SJérôme Glisse * 7495042db43SJérôme Glisse * We will likely want to have some new rss counters 7505042db43SJérôme Glisse * for unaddressable pages, at some point. But for now 7515042db43SJérôme Glisse * keep things as they are. 7525042db43SJérôme Glisse */ 7535042db43SJérôme Glisse get_page(page); 7545042db43SJérôme Glisse rss[mm_counter(page)]++; 7555042db43SJérôme Glisse page_dup_rmap(page, false); 7565042db43SJérôme Glisse 7575042db43SJérôme Glisse /* 7585042db43SJérôme Glisse * We do not preserve soft-dirty information, because so 7595042db43SJérôme Glisse * far, checkpoint/restore is the only feature that 7605042db43SJérôme Glisse * requires that. And checkpoint/restore does not work 7615042db43SJérôme Glisse * when a device driver is involved (you cannot easily 7625042db43SJérôme Glisse * save and restore device driver state). 7635042db43SJérôme Glisse */ 7645042db43SJérôme Glisse if (is_write_device_private_entry(entry) && 7655042db43SJérôme Glisse is_cow_mapping(vm_flags)) { 7665042db43SJérôme Glisse make_device_private_entry_read(&entry); 7675042db43SJérôme Glisse pte = swp_entry_to_pte(entry); 7685042db43SJérôme Glisse set_pte_at(src_mm, addr, src_pte, pte); 7695042db43SJérôme Glisse } 7701da177e4SLinus Torvalds } 771ae859762SHugh Dickins goto out_set_pte; 7721da177e4SLinus Torvalds } 7731da177e4SLinus Torvalds 7741da177e4SLinus Torvalds /* 7751da177e4SLinus Torvalds * If it's a COW mapping, write protect it both 7761da177e4SLinus Torvalds * in the parent and the child 7771da177e4SLinus Torvalds */ 7781b2de5d0SLinus Torvalds if (is_cow_mapping(vm_flags) && pte_write(pte)) { 7791da177e4SLinus Torvalds ptep_set_wrprotect(src_mm, addr, src_pte); 7803dc90795SZachary Amsden pte = pte_wrprotect(pte); 7811da177e4SLinus Torvalds } 7821da177e4SLinus Torvalds 7831da177e4SLinus Torvalds /* 7841da177e4SLinus Torvalds * If it's a shared mapping, mark it clean in 7851da177e4SLinus Torvalds * the child 7861da177e4SLinus Torvalds */ 7871da177e4SLinus Torvalds if (vm_flags & VM_SHARED) 7881da177e4SLinus Torvalds pte = pte_mkclean(pte); 7891da177e4SLinus Torvalds pte = pte_mkold(pte); 7906aab341eSLinus Torvalds 7916aab341eSLinus Torvalds page = vm_normal_page(vma, addr, pte); 7926aab341eSLinus Torvalds if (page) { 7931da177e4SLinus Torvalds get_page(page); 79453f9263bSKirill A. Shutemov page_dup_rmap(page, false); 795eca56ff9SJerome Marchand rss[mm_counter(page)]++; 796df6ad698SJérôme Glisse } else if (pte_devmap(pte)) { 797df6ad698SJérôme Glisse page = pte_page(pte); 798df6ad698SJérôme Glisse 799df6ad698SJérôme Glisse /* 800df6ad698SJérôme Glisse * Cache coherent device memory behave like regular page and 801df6ad698SJérôme Glisse * not like persistent memory page. For more informations see 802df6ad698SJérôme Glisse * MEMORY_DEVICE_CACHE_COHERENT in memory_hotplug.h 803df6ad698SJérôme Glisse */ 804df6ad698SJérôme Glisse if (is_device_public_page(page)) { 805df6ad698SJérôme Glisse get_page(page); 806df6ad698SJérôme Glisse page_dup_rmap(page, false); 807df6ad698SJérôme Glisse rss[mm_counter(page)]++; 808df6ad698SJérôme Glisse } 8096aab341eSLinus Torvalds } 810ae859762SHugh Dickins 811ae859762SHugh Dickins out_set_pte: 812ae859762SHugh Dickins set_pte_at(dst_mm, addr, dst_pte, pte); 813570a335bSHugh Dickins return 0; 8141da177e4SLinus Torvalds } 8151da177e4SLinus Torvalds 81621bda264SJerome Marchand static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 8171da177e4SLinus Torvalds pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma, 8181da177e4SLinus Torvalds unsigned long addr, unsigned long end) 8191da177e4SLinus Torvalds { 820c36987e2SDaisuke Nishimura pte_t *orig_src_pte, *orig_dst_pte; 8211da177e4SLinus Torvalds pte_t *src_pte, *dst_pte; 822c74df32cSHugh Dickins spinlock_t *src_ptl, *dst_ptl; 823e040f218SHugh Dickins int progress = 0; 824d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 825570a335bSHugh Dickins swp_entry_t entry = (swp_entry_t){0}; 8261da177e4SLinus Torvalds 8271da177e4SLinus Torvalds again: 828d559db08SKAMEZAWA Hiroyuki init_rss_vec(rss); 829d559db08SKAMEZAWA Hiroyuki 830c74df32cSHugh Dickins dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl); 8311da177e4SLinus Torvalds if (!dst_pte) 8321da177e4SLinus Torvalds return -ENOMEM; 833ece0e2b6SPeter Zijlstra src_pte = pte_offset_map(src_pmd, addr); 8344c21e2f2SHugh Dickins src_ptl = pte_lockptr(src_mm, src_pmd); 835f20dc5f7SIngo Molnar spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 836c36987e2SDaisuke Nishimura orig_src_pte = src_pte; 837c36987e2SDaisuke Nishimura orig_dst_pte = dst_pte; 8386606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 8391da177e4SLinus Torvalds 8401da177e4SLinus Torvalds do { 8411da177e4SLinus Torvalds /* 8421da177e4SLinus Torvalds * We are holding two locks at this point - either of them 8431da177e4SLinus Torvalds * could generate latencies in another task on another CPU. 8441da177e4SLinus Torvalds */ 845e040f218SHugh Dickins if (progress >= 32) { 846e040f218SHugh Dickins progress = 0; 847e040f218SHugh Dickins if (need_resched() || 84895c354feSNick Piggin spin_needbreak(src_ptl) || spin_needbreak(dst_ptl)) 8491da177e4SLinus Torvalds break; 850e040f218SHugh Dickins } 8511da177e4SLinus Torvalds if (pte_none(*src_pte)) { 8521da177e4SLinus Torvalds progress++; 8531da177e4SLinus Torvalds continue; 8541da177e4SLinus Torvalds } 855570a335bSHugh Dickins entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte, 856570a335bSHugh Dickins vma, addr, rss); 857570a335bSHugh Dickins if (entry.val) 858570a335bSHugh Dickins break; 8591da177e4SLinus Torvalds progress += 8; 8601da177e4SLinus Torvalds } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end); 8611da177e4SLinus Torvalds 8626606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 863c74df32cSHugh Dickins spin_unlock(src_ptl); 864ece0e2b6SPeter Zijlstra pte_unmap(orig_src_pte); 865d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(dst_mm, rss); 866c36987e2SDaisuke Nishimura pte_unmap_unlock(orig_dst_pte, dst_ptl); 867c74df32cSHugh Dickins cond_resched(); 868570a335bSHugh Dickins 869570a335bSHugh Dickins if (entry.val) { 870570a335bSHugh Dickins if (add_swap_count_continuation(entry, GFP_KERNEL) < 0) 871570a335bSHugh Dickins return -ENOMEM; 872570a335bSHugh Dickins progress = 0; 873570a335bSHugh Dickins } 8741da177e4SLinus Torvalds if (addr != end) 8751da177e4SLinus Torvalds goto again; 8761da177e4SLinus Torvalds return 0; 8771da177e4SLinus Torvalds } 8781da177e4SLinus Torvalds 8791da177e4SLinus Torvalds static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 8801da177e4SLinus Torvalds pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma, 8811da177e4SLinus Torvalds unsigned long addr, unsigned long end) 8821da177e4SLinus Torvalds { 8831da177e4SLinus Torvalds pmd_t *src_pmd, *dst_pmd; 8841da177e4SLinus Torvalds unsigned long next; 8851da177e4SLinus Torvalds 8861da177e4SLinus Torvalds dst_pmd = pmd_alloc(dst_mm, dst_pud, addr); 8871da177e4SLinus Torvalds if (!dst_pmd) 8881da177e4SLinus Torvalds return -ENOMEM; 8891da177e4SLinus Torvalds src_pmd = pmd_offset(src_pud, addr); 8901da177e4SLinus Torvalds do { 8911da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 89284c3fc4eSZi Yan if (is_swap_pmd(*src_pmd) || pmd_trans_huge(*src_pmd) 89384c3fc4eSZi Yan || pmd_devmap(*src_pmd)) { 89471e3aac0SAndrea Arcangeli int err; 895a00cc7d9SMatthew Wilcox VM_BUG_ON_VMA(next-addr != HPAGE_PMD_SIZE, vma); 89671e3aac0SAndrea Arcangeli err = copy_huge_pmd(dst_mm, src_mm, 89771e3aac0SAndrea Arcangeli dst_pmd, src_pmd, addr, vma); 89871e3aac0SAndrea Arcangeli if (err == -ENOMEM) 89971e3aac0SAndrea Arcangeli return -ENOMEM; 90071e3aac0SAndrea Arcangeli if (!err) 90171e3aac0SAndrea Arcangeli continue; 90271e3aac0SAndrea Arcangeli /* fall through */ 90371e3aac0SAndrea Arcangeli } 9041da177e4SLinus Torvalds if (pmd_none_or_clear_bad(src_pmd)) 9051da177e4SLinus Torvalds continue; 9061da177e4SLinus Torvalds if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd, 9071da177e4SLinus Torvalds vma, addr, next)) 9081da177e4SLinus Torvalds return -ENOMEM; 9091da177e4SLinus Torvalds } while (dst_pmd++, src_pmd++, addr = next, addr != end); 9101da177e4SLinus Torvalds return 0; 9111da177e4SLinus Torvalds } 9121da177e4SLinus Torvalds 9131da177e4SLinus Torvalds static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 914c2febafcSKirill A. Shutemov p4d_t *dst_p4d, p4d_t *src_p4d, struct vm_area_struct *vma, 9151da177e4SLinus Torvalds unsigned long addr, unsigned long end) 9161da177e4SLinus Torvalds { 9171da177e4SLinus Torvalds pud_t *src_pud, *dst_pud; 9181da177e4SLinus Torvalds unsigned long next; 9191da177e4SLinus Torvalds 920c2febafcSKirill A. Shutemov dst_pud = pud_alloc(dst_mm, dst_p4d, addr); 9211da177e4SLinus Torvalds if (!dst_pud) 9221da177e4SLinus Torvalds return -ENOMEM; 923c2febafcSKirill A. Shutemov src_pud = pud_offset(src_p4d, addr); 9241da177e4SLinus Torvalds do { 9251da177e4SLinus Torvalds next = pud_addr_end(addr, end); 926a00cc7d9SMatthew Wilcox if (pud_trans_huge(*src_pud) || pud_devmap(*src_pud)) { 927a00cc7d9SMatthew Wilcox int err; 928a00cc7d9SMatthew Wilcox 929a00cc7d9SMatthew Wilcox VM_BUG_ON_VMA(next-addr != HPAGE_PUD_SIZE, vma); 930a00cc7d9SMatthew Wilcox err = copy_huge_pud(dst_mm, src_mm, 931a00cc7d9SMatthew Wilcox dst_pud, src_pud, addr, vma); 932a00cc7d9SMatthew Wilcox if (err == -ENOMEM) 933a00cc7d9SMatthew Wilcox return -ENOMEM; 934a00cc7d9SMatthew Wilcox if (!err) 935a00cc7d9SMatthew Wilcox continue; 936a00cc7d9SMatthew Wilcox /* fall through */ 937a00cc7d9SMatthew Wilcox } 9381da177e4SLinus Torvalds if (pud_none_or_clear_bad(src_pud)) 9391da177e4SLinus Torvalds continue; 9401da177e4SLinus Torvalds if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud, 9411da177e4SLinus Torvalds vma, addr, next)) 9421da177e4SLinus Torvalds return -ENOMEM; 9431da177e4SLinus Torvalds } while (dst_pud++, src_pud++, addr = next, addr != end); 9441da177e4SLinus Torvalds return 0; 9451da177e4SLinus Torvalds } 9461da177e4SLinus Torvalds 947c2febafcSKirill A. Shutemov static inline int copy_p4d_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 948c2febafcSKirill A. Shutemov pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma, 949c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end) 950c2febafcSKirill A. Shutemov { 951c2febafcSKirill A. Shutemov p4d_t *src_p4d, *dst_p4d; 952c2febafcSKirill A. Shutemov unsigned long next; 953c2febafcSKirill A. Shutemov 954c2febafcSKirill A. Shutemov dst_p4d = p4d_alloc(dst_mm, dst_pgd, addr); 955c2febafcSKirill A. Shutemov if (!dst_p4d) 956c2febafcSKirill A. Shutemov return -ENOMEM; 957c2febafcSKirill A. Shutemov src_p4d = p4d_offset(src_pgd, addr); 958c2febafcSKirill A. Shutemov do { 959c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 960c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(src_p4d)) 961c2febafcSKirill A. Shutemov continue; 962c2febafcSKirill A. Shutemov if (copy_pud_range(dst_mm, src_mm, dst_p4d, src_p4d, 963c2febafcSKirill A. Shutemov vma, addr, next)) 964c2febafcSKirill A. Shutemov return -ENOMEM; 965c2febafcSKirill A. Shutemov } while (dst_p4d++, src_p4d++, addr = next, addr != end); 966c2febafcSKirill A. Shutemov return 0; 967c2febafcSKirill A. Shutemov } 968c2febafcSKirill A. Shutemov 9691da177e4SLinus Torvalds int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 9701da177e4SLinus Torvalds struct vm_area_struct *vma) 9711da177e4SLinus Torvalds { 9721da177e4SLinus Torvalds pgd_t *src_pgd, *dst_pgd; 9731da177e4SLinus Torvalds unsigned long next; 9741da177e4SLinus Torvalds unsigned long addr = vma->vm_start; 9751da177e4SLinus Torvalds unsigned long end = vma->vm_end; 9762ec74c3eSSagi Grimberg unsigned long mmun_start; /* For mmu_notifiers */ 9772ec74c3eSSagi Grimberg unsigned long mmun_end; /* For mmu_notifiers */ 9782ec74c3eSSagi Grimberg bool is_cow; 979cddb8a5cSAndrea Arcangeli int ret; 9801da177e4SLinus Torvalds 981d992895bSNick Piggin /* 982d992895bSNick Piggin * Don't copy ptes where a page fault will fill them correctly. 983d992895bSNick Piggin * Fork becomes much lighter when there are big shared or private 984d992895bSNick Piggin * readonly mappings. The tradeoff is that copy_page_range is more 985d992895bSNick Piggin * efficient than faulting. 986d992895bSNick Piggin */ 9870661a336SKirill A. Shutemov if (!(vma->vm_flags & (VM_HUGETLB | VM_PFNMAP | VM_MIXEDMAP)) && 9880661a336SKirill A. Shutemov !vma->anon_vma) 989d992895bSNick Piggin return 0; 990d992895bSNick Piggin 9911da177e4SLinus Torvalds if (is_vm_hugetlb_page(vma)) 9921da177e4SLinus Torvalds return copy_hugetlb_page_range(dst_mm, src_mm, vma); 9931da177e4SLinus Torvalds 994b3b9c293SKonstantin Khlebnikov if (unlikely(vma->vm_flags & VM_PFNMAP)) { 9952ab64037Svenkatesh.pallipadi@intel.com /* 9962ab64037Svenkatesh.pallipadi@intel.com * We do not free on error cases below as remove_vma 9972ab64037Svenkatesh.pallipadi@intel.com * gets called on error from higher level routine 9982ab64037Svenkatesh.pallipadi@intel.com */ 9995180da41SSuresh Siddha ret = track_pfn_copy(vma); 10002ab64037Svenkatesh.pallipadi@intel.com if (ret) 10012ab64037Svenkatesh.pallipadi@intel.com return ret; 10022ab64037Svenkatesh.pallipadi@intel.com } 10032ab64037Svenkatesh.pallipadi@intel.com 1004cddb8a5cSAndrea Arcangeli /* 1005cddb8a5cSAndrea Arcangeli * We need to invalidate the secondary MMU mappings only when 1006cddb8a5cSAndrea Arcangeli * there could be a permission downgrade on the ptes of the 1007cddb8a5cSAndrea Arcangeli * parent mm. And a permission downgrade will only happen if 1008cddb8a5cSAndrea Arcangeli * is_cow_mapping() returns true. 1009cddb8a5cSAndrea Arcangeli */ 10102ec74c3eSSagi Grimberg is_cow = is_cow_mapping(vma->vm_flags); 10112ec74c3eSSagi Grimberg mmun_start = addr; 10122ec74c3eSSagi Grimberg mmun_end = end; 10132ec74c3eSSagi Grimberg if (is_cow) 10142ec74c3eSSagi Grimberg mmu_notifier_invalidate_range_start(src_mm, mmun_start, 10152ec74c3eSSagi Grimberg mmun_end); 1016cddb8a5cSAndrea Arcangeli 1017cddb8a5cSAndrea Arcangeli ret = 0; 10181da177e4SLinus Torvalds dst_pgd = pgd_offset(dst_mm, addr); 10191da177e4SLinus Torvalds src_pgd = pgd_offset(src_mm, addr); 10201da177e4SLinus Torvalds do { 10211da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 10221da177e4SLinus Torvalds if (pgd_none_or_clear_bad(src_pgd)) 10231da177e4SLinus Torvalds continue; 1024c2febafcSKirill A. Shutemov if (unlikely(copy_p4d_range(dst_mm, src_mm, dst_pgd, src_pgd, 1025cddb8a5cSAndrea Arcangeli vma, addr, next))) { 1026cddb8a5cSAndrea Arcangeli ret = -ENOMEM; 1027cddb8a5cSAndrea Arcangeli break; 1028cddb8a5cSAndrea Arcangeli } 10291da177e4SLinus Torvalds } while (dst_pgd++, src_pgd++, addr = next, addr != end); 1030cddb8a5cSAndrea Arcangeli 10312ec74c3eSSagi Grimberg if (is_cow) 10322ec74c3eSSagi Grimberg mmu_notifier_invalidate_range_end(src_mm, mmun_start, mmun_end); 1033cddb8a5cSAndrea Arcangeli return ret; 10341da177e4SLinus Torvalds } 10351da177e4SLinus Torvalds 103651c6f666SRobin Holt static unsigned long zap_pte_range(struct mmu_gather *tlb, 1037b5810039SNick Piggin struct vm_area_struct *vma, pmd_t *pmd, 10381da177e4SLinus Torvalds unsigned long addr, unsigned long end, 103997a89413SPeter Zijlstra struct zap_details *details) 10401da177e4SLinus Torvalds { 1041b5810039SNick Piggin struct mm_struct *mm = tlb->mm; 1042d16dfc55SPeter Zijlstra int force_flush = 0; 1043d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 104497a89413SPeter Zijlstra spinlock_t *ptl; 10455f1a1907SSteven Rostedt pte_t *start_pte; 104697a89413SPeter Zijlstra pte_t *pte; 10478a5f14a2SKirill A. Shutemov swp_entry_t entry; 1048d559db08SKAMEZAWA Hiroyuki 104907e32661SAneesh Kumar K.V tlb_remove_check_page_size_change(tlb, PAGE_SIZE); 1050d16dfc55SPeter Zijlstra again: 1051e303297eSPeter Zijlstra init_rss_vec(rss); 10525f1a1907SSteven Rostedt start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl); 10535f1a1907SSteven Rostedt pte = start_pte; 10543ea27719SMel Gorman flush_tlb_batched_pending(mm); 10556606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 10561da177e4SLinus Torvalds do { 10571da177e4SLinus Torvalds pte_t ptent = *pte; 1058166f61b9STobin C Harding if (pte_none(ptent)) 10591da177e4SLinus Torvalds continue; 106051c6f666SRobin Holt 10616f5e6b9eSHugh Dickins if (pte_present(ptent)) { 10626f5e6b9eSHugh Dickins struct page *page; 10636f5e6b9eSHugh Dickins 1064df6ad698SJérôme Glisse page = _vm_normal_page(vma, addr, ptent, true); 10651da177e4SLinus Torvalds if (unlikely(details) && page) { 10661da177e4SLinus Torvalds /* 10671da177e4SLinus Torvalds * unmap_shared_mapping_pages() wants to 10681da177e4SLinus Torvalds * invalidate cache without truncating: 10691da177e4SLinus Torvalds * unmap shared but keep private pages. 10701da177e4SLinus Torvalds */ 10711da177e4SLinus Torvalds if (details->check_mapping && 1072800d8c63SKirill A. Shutemov details->check_mapping != page_rmapping(page)) 10731da177e4SLinus Torvalds continue; 10741da177e4SLinus Torvalds } 1075b5810039SNick Piggin ptent = ptep_get_and_clear_full(mm, addr, pte, 1076a600388dSZachary Amsden tlb->fullmm); 10771da177e4SLinus Torvalds tlb_remove_tlb_entry(tlb, pte, addr); 10781da177e4SLinus Torvalds if (unlikely(!page)) 10791da177e4SLinus Torvalds continue; 1080eca56ff9SJerome Marchand 1081eca56ff9SJerome Marchand if (!PageAnon(page)) { 10821cf35d47SLinus Torvalds if (pte_dirty(ptent)) { 10831cf35d47SLinus Torvalds force_flush = 1; 10846237bcd9SHugh Dickins set_page_dirty(page); 10851cf35d47SLinus Torvalds } 10864917e5d0SJohannes Weiner if (pte_young(ptent) && 108764363aadSJoe Perches likely(!(vma->vm_flags & VM_SEQ_READ))) 1088bf3f3bc5SNick Piggin mark_page_accessed(page); 10896237bcd9SHugh Dickins } 1090eca56ff9SJerome Marchand rss[mm_counter(page)]--; 1091d281ee61SKirill A. Shutemov page_remove_rmap(page, false); 10923dc14741SHugh Dickins if (unlikely(page_mapcount(page) < 0)) 10933dc14741SHugh Dickins print_bad_pte(vma, addr, ptent, page); 1094e9d55e15SAneesh Kumar K.V if (unlikely(__tlb_remove_page(tlb, page))) { 10951cf35d47SLinus Torvalds force_flush = 1; 1096ce9ec37bSWill Deacon addr += PAGE_SIZE; 1097d16dfc55SPeter Zijlstra break; 10981cf35d47SLinus Torvalds } 10991da177e4SLinus Torvalds continue; 11001da177e4SLinus Torvalds } 11015042db43SJérôme Glisse 11025042db43SJérôme Glisse entry = pte_to_swp_entry(ptent); 11035042db43SJérôme Glisse if (non_swap_entry(entry) && is_device_private_entry(entry)) { 11045042db43SJérôme Glisse struct page *page = device_private_entry_to_page(entry); 11055042db43SJérôme Glisse 11065042db43SJérôme Glisse if (unlikely(details && details->check_mapping)) { 11075042db43SJérôme Glisse /* 11085042db43SJérôme Glisse * unmap_shared_mapping_pages() wants to 11095042db43SJérôme Glisse * invalidate cache without truncating: 11105042db43SJérôme Glisse * unmap shared but keep private pages. 11115042db43SJérôme Glisse */ 11125042db43SJérôme Glisse if (details->check_mapping != 11135042db43SJérôme Glisse page_rmapping(page)) 11145042db43SJérôme Glisse continue; 11155042db43SJérôme Glisse } 11165042db43SJérôme Glisse 11175042db43SJérôme Glisse pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); 11185042db43SJérôme Glisse rss[mm_counter(page)]--; 11195042db43SJérôme Glisse page_remove_rmap(page, false); 11205042db43SJérôme Glisse put_page(page); 11215042db43SJérôme Glisse continue; 11225042db43SJérôme Glisse } 11235042db43SJérôme Glisse 11243e8715fdSKirill A. Shutemov /* If details->check_mapping, we leave swap entries. */ 11253e8715fdSKirill A. Shutemov if (unlikely(details)) 11261da177e4SLinus Torvalds continue; 1127b084d435SKAMEZAWA Hiroyuki 11288a5f14a2SKirill A. Shutemov entry = pte_to_swp_entry(ptent); 1129b084d435SKAMEZAWA Hiroyuki if (!non_swap_entry(entry)) 1130b084d435SKAMEZAWA Hiroyuki rss[MM_SWAPENTS]--; 11319f9f1acdSKonstantin Khlebnikov else if (is_migration_entry(entry)) { 11329f9f1acdSKonstantin Khlebnikov struct page *page; 11339f9f1acdSKonstantin Khlebnikov 11349f9f1acdSKonstantin Khlebnikov page = migration_entry_to_page(entry); 1135eca56ff9SJerome Marchand rss[mm_counter(page)]--; 11369f9f1acdSKonstantin Khlebnikov } 1137b084d435SKAMEZAWA Hiroyuki if (unlikely(!free_swap_and_cache(entry))) 11382509ef26SHugh Dickins print_bad_pte(vma, addr, ptent, NULL); 11399888a1caSZachary Amsden pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); 114097a89413SPeter Zijlstra } while (pte++, addr += PAGE_SIZE, addr != end); 1141ae859762SHugh Dickins 1142d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(mm, rss); 11436606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 114451c6f666SRobin Holt 11451cf35d47SLinus Torvalds /* Do the actual TLB flush before dropping ptl */ 1146fb7332a9SWill Deacon if (force_flush) 11471cf35d47SLinus Torvalds tlb_flush_mmu_tlbonly(tlb); 11481cf35d47SLinus Torvalds pte_unmap_unlock(start_pte, ptl); 11491cf35d47SLinus Torvalds 11501cf35d47SLinus Torvalds /* 11511cf35d47SLinus Torvalds * If we forced a TLB flush (either due to running out of 11521cf35d47SLinus Torvalds * batch buffers or because we needed to flush dirty TLB 11531cf35d47SLinus Torvalds * entries before releasing the ptl), free the batched 11541cf35d47SLinus Torvalds * memory too. Restart if we didn't do everything. 11551cf35d47SLinus Torvalds */ 11561cf35d47SLinus Torvalds if (force_flush) { 11571cf35d47SLinus Torvalds force_flush = 0; 11581cf35d47SLinus Torvalds tlb_flush_mmu_free(tlb); 11592b047252SLinus Torvalds if (addr != end) 1160d16dfc55SPeter Zijlstra goto again; 1161d16dfc55SPeter Zijlstra } 1162d16dfc55SPeter Zijlstra 116351c6f666SRobin Holt return addr; 11641da177e4SLinus Torvalds } 11651da177e4SLinus Torvalds 116651c6f666SRobin Holt static inline unsigned long zap_pmd_range(struct mmu_gather *tlb, 1167b5810039SNick Piggin struct vm_area_struct *vma, pud_t *pud, 11681da177e4SLinus Torvalds unsigned long addr, unsigned long end, 116997a89413SPeter Zijlstra struct zap_details *details) 11701da177e4SLinus Torvalds { 11711da177e4SLinus Torvalds pmd_t *pmd; 11721da177e4SLinus Torvalds unsigned long next; 11731da177e4SLinus Torvalds 11741da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 11751da177e4SLinus Torvalds do { 11761da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 117784c3fc4eSZi Yan if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) { 117853406ed1SHugh Dickins if (next - addr != HPAGE_PMD_SIZE) 1179fd60775aSDavid Rientjes __split_huge_pmd(vma, pmd, addr, false, NULL); 118053406ed1SHugh Dickins else if (zap_huge_pmd(tlb, vma, pmd, addr)) 11811a5a9906SAndrea Arcangeli goto next; 118271e3aac0SAndrea Arcangeli /* fall through */ 118371e3aac0SAndrea Arcangeli } 11841a5a9906SAndrea Arcangeli /* 11851a5a9906SAndrea Arcangeli * Here there can be other concurrent MADV_DONTNEED or 11861a5a9906SAndrea Arcangeli * trans huge page faults running, and if the pmd is 11871a5a9906SAndrea Arcangeli * none or trans huge it can change under us. This is 11881a5a9906SAndrea Arcangeli * because MADV_DONTNEED holds the mmap_sem in read 11891a5a9906SAndrea Arcangeli * mode. 11901a5a9906SAndrea Arcangeli */ 11911a5a9906SAndrea Arcangeli if (pmd_none_or_trans_huge_or_clear_bad(pmd)) 11921a5a9906SAndrea Arcangeli goto next; 119397a89413SPeter Zijlstra next = zap_pte_range(tlb, vma, pmd, addr, next, details); 11941a5a9906SAndrea Arcangeli next: 119597a89413SPeter Zijlstra cond_resched(); 119697a89413SPeter Zijlstra } while (pmd++, addr = next, addr != end); 119751c6f666SRobin Holt 119851c6f666SRobin Holt return addr; 11991da177e4SLinus Torvalds } 12001da177e4SLinus Torvalds 120151c6f666SRobin Holt static inline unsigned long zap_pud_range(struct mmu_gather *tlb, 1202c2febafcSKirill A. Shutemov struct vm_area_struct *vma, p4d_t *p4d, 12031da177e4SLinus Torvalds unsigned long addr, unsigned long end, 120497a89413SPeter Zijlstra struct zap_details *details) 12051da177e4SLinus Torvalds { 12061da177e4SLinus Torvalds pud_t *pud; 12071da177e4SLinus Torvalds unsigned long next; 12081da177e4SLinus Torvalds 1209c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 12101da177e4SLinus Torvalds do { 12111da177e4SLinus Torvalds next = pud_addr_end(addr, end); 1212a00cc7d9SMatthew Wilcox if (pud_trans_huge(*pud) || pud_devmap(*pud)) { 1213a00cc7d9SMatthew Wilcox if (next - addr != HPAGE_PUD_SIZE) { 1214a00cc7d9SMatthew Wilcox VM_BUG_ON_VMA(!rwsem_is_locked(&tlb->mm->mmap_sem), vma); 1215a00cc7d9SMatthew Wilcox split_huge_pud(vma, pud, addr); 1216a00cc7d9SMatthew Wilcox } else if (zap_huge_pud(tlb, vma, pud, addr)) 1217a00cc7d9SMatthew Wilcox goto next; 1218a00cc7d9SMatthew Wilcox /* fall through */ 1219a00cc7d9SMatthew Wilcox } 122097a89413SPeter Zijlstra if (pud_none_or_clear_bad(pud)) 12211da177e4SLinus Torvalds continue; 122297a89413SPeter Zijlstra next = zap_pmd_range(tlb, vma, pud, addr, next, details); 1223a00cc7d9SMatthew Wilcox next: 1224a00cc7d9SMatthew Wilcox cond_resched(); 122597a89413SPeter Zijlstra } while (pud++, addr = next, addr != end); 122651c6f666SRobin Holt 122751c6f666SRobin Holt return addr; 12281da177e4SLinus Torvalds } 12291da177e4SLinus Torvalds 1230c2febafcSKirill A. Shutemov static inline unsigned long zap_p4d_range(struct mmu_gather *tlb, 1231c2febafcSKirill A. Shutemov struct vm_area_struct *vma, pgd_t *pgd, 1232c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 1233c2febafcSKirill A. Shutemov struct zap_details *details) 1234c2febafcSKirill A. Shutemov { 1235c2febafcSKirill A. Shutemov p4d_t *p4d; 1236c2febafcSKirill A. Shutemov unsigned long next; 1237c2febafcSKirill A. Shutemov 1238c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 1239c2febafcSKirill A. Shutemov do { 1240c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 1241c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(p4d)) 1242c2febafcSKirill A. Shutemov continue; 1243c2febafcSKirill A. Shutemov next = zap_pud_range(tlb, vma, p4d, addr, next, details); 1244c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 1245c2febafcSKirill A. Shutemov 1246c2febafcSKirill A. Shutemov return addr; 1247c2febafcSKirill A. Shutemov } 1248c2febafcSKirill A. Shutemov 1249aac45363SMichal Hocko void unmap_page_range(struct mmu_gather *tlb, 125051c6f666SRobin Holt struct vm_area_struct *vma, 12511da177e4SLinus Torvalds unsigned long addr, unsigned long end, 125297a89413SPeter Zijlstra struct zap_details *details) 12531da177e4SLinus Torvalds { 12541da177e4SLinus Torvalds pgd_t *pgd; 12551da177e4SLinus Torvalds unsigned long next; 12561da177e4SLinus Torvalds 12571da177e4SLinus Torvalds BUG_ON(addr >= end); 12581da177e4SLinus Torvalds tlb_start_vma(tlb, vma); 12591da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 12601da177e4SLinus Torvalds do { 12611da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 126297a89413SPeter Zijlstra if (pgd_none_or_clear_bad(pgd)) 12631da177e4SLinus Torvalds continue; 1264c2febafcSKirill A. Shutemov next = zap_p4d_range(tlb, vma, pgd, addr, next, details); 126597a89413SPeter Zijlstra } while (pgd++, addr = next, addr != end); 12661da177e4SLinus Torvalds tlb_end_vma(tlb, vma); 12671da177e4SLinus Torvalds } 12681da177e4SLinus Torvalds 1269f5cc4eefSAl Viro 1270f5cc4eefSAl Viro static void unmap_single_vma(struct mmu_gather *tlb, 12711da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long start_addr, 12724f74d2c8SLinus Torvalds unsigned long end_addr, 12731da177e4SLinus Torvalds struct zap_details *details) 12741da177e4SLinus Torvalds { 1275f5cc4eefSAl Viro unsigned long start = max(vma->vm_start, start_addr); 12761da177e4SLinus Torvalds unsigned long end; 12771da177e4SLinus Torvalds 12781da177e4SLinus Torvalds if (start >= vma->vm_end) 1279f5cc4eefSAl Viro return; 12801da177e4SLinus Torvalds end = min(vma->vm_end, end_addr); 12811da177e4SLinus Torvalds if (end <= vma->vm_start) 1282f5cc4eefSAl Viro return; 12831da177e4SLinus Torvalds 1284cbc91f71SSrikar Dronamraju if (vma->vm_file) 1285cbc91f71SSrikar Dronamraju uprobe_munmap(vma, start, end); 1286cbc91f71SSrikar Dronamraju 1287b3b9c293SKonstantin Khlebnikov if (unlikely(vma->vm_flags & VM_PFNMAP)) 12885180da41SSuresh Siddha untrack_pfn(vma, 0, 0); 12892ab64037Svenkatesh.pallipadi@intel.com 12908b2a1238SAl Viro if (start != end) { 129151c6f666SRobin Holt if (unlikely(is_vm_hugetlb_page(vma))) { 1292a137e1ccSAndi Kleen /* 1293a137e1ccSAndi Kleen * It is undesirable to test vma->vm_file as it 1294a137e1ccSAndi Kleen * should be non-null for valid hugetlb area. 1295a137e1ccSAndi Kleen * However, vm_file will be NULL in the error 12967aa6b4adSDavidlohr Bueso * cleanup path of mmap_region. When 1297a137e1ccSAndi Kleen * hugetlbfs ->mmap method fails, 12987aa6b4adSDavidlohr Bueso * mmap_region() nullifies vma->vm_file 1299a137e1ccSAndi Kleen * before calling this function to clean up. 1300a137e1ccSAndi Kleen * Since no pte has actually been setup, it is 1301a137e1ccSAndi Kleen * safe to do nothing in this case. 1302a137e1ccSAndi Kleen */ 130324669e58SAneesh Kumar K.V if (vma->vm_file) { 130483cde9e8SDavidlohr Bueso i_mmap_lock_write(vma->vm_file->f_mapping); 1305d833352aSMel Gorman __unmap_hugepage_range_final(tlb, vma, start, end, NULL); 130683cde9e8SDavidlohr Bueso i_mmap_unlock_write(vma->vm_file->f_mapping); 130724669e58SAneesh Kumar K.V } 130851c6f666SRobin Holt } else 1309038c7aa1SAl Viro unmap_page_range(tlb, vma, start, end, details); 131097a89413SPeter Zijlstra } 131151c6f666SRobin Holt } 13121da177e4SLinus Torvalds 1313f5cc4eefSAl Viro /** 1314f5cc4eefSAl Viro * unmap_vmas - unmap a range of memory covered by a list of vma's 1315f5cc4eefSAl Viro * @tlb: address of the caller's struct mmu_gather 1316f5cc4eefSAl Viro * @vma: the starting vma 1317f5cc4eefSAl Viro * @start_addr: virtual address at which to start unmapping 1318f5cc4eefSAl Viro * @end_addr: virtual address at which to end unmapping 1319f5cc4eefSAl Viro * 1320f5cc4eefSAl Viro * Unmap all pages in the vma list. 1321f5cc4eefSAl Viro * 1322f5cc4eefSAl Viro * Only addresses between `start' and `end' will be unmapped. 1323f5cc4eefSAl Viro * 1324f5cc4eefSAl Viro * The VMA list must be sorted in ascending virtual address order. 1325f5cc4eefSAl Viro * 1326f5cc4eefSAl Viro * unmap_vmas() assumes that the caller will flush the whole unmapped address 1327f5cc4eefSAl Viro * range after unmap_vmas() returns. So the only responsibility here is to 1328f5cc4eefSAl Viro * ensure that any thus-far unmapped pages are flushed before unmap_vmas() 1329f5cc4eefSAl Viro * drops the lock and schedules. 1330f5cc4eefSAl Viro */ 1331f5cc4eefSAl Viro void unmap_vmas(struct mmu_gather *tlb, 1332f5cc4eefSAl Viro struct vm_area_struct *vma, unsigned long start_addr, 13334f74d2c8SLinus Torvalds unsigned long end_addr) 1334f5cc4eefSAl Viro { 1335f5cc4eefSAl Viro struct mm_struct *mm = vma->vm_mm; 1336f5cc4eefSAl Viro 1337f5cc4eefSAl Viro mmu_notifier_invalidate_range_start(mm, start_addr, end_addr); 1338f5cc4eefSAl Viro for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) 13394f74d2c8SLinus Torvalds unmap_single_vma(tlb, vma, start_addr, end_addr, NULL); 1340cddb8a5cSAndrea Arcangeli mmu_notifier_invalidate_range_end(mm, start_addr, end_addr); 13411da177e4SLinus Torvalds } 13421da177e4SLinus Torvalds 13431da177e4SLinus Torvalds /** 13441da177e4SLinus Torvalds * zap_page_range - remove user pages in a given range 13451da177e4SLinus Torvalds * @vma: vm_area_struct holding the applicable pages 1346eb4546bbSRandy Dunlap * @start: starting address of pages to zap 13471da177e4SLinus Torvalds * @size: number of bytes to zap 1348f5cc4eefSAl Viro * 1349f5cc4eefSAl Viro * Caller must protect the VMA list 13501da177e4SLinus Torvalds */ 13517e027b14SLinus Torvalds void zap_page_range(struct vm_area_struct *vma, unsigned long start, 1352ecf1385dSKirill A. Shutemov unsigned long size) 13531da177e4SLinus Torvalds { 13541da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 1355d16dfc55SPeter Zijlstra struct mmu_gather tlb; 13567e027b14SLinus Torvalds unsigned long end = start + size; 13571da177e4SLinus Torvalds 13581da177e4SLinus Torvalds lru_add_drain(); 13592b047252SLinus Torvalds tlb_gather_mmu(&tlb, mm, start, end); 1360365e9c87SHugh Dickins update_hiwater_rss(mm); 13617e027b14SLinus Torvalds mmu_notifier_invalidate_range_start(mm, start, end); 136250c150f2SRik van Riel for ( ; vma && vma->vm_start < end; vma = vma->vm_next) 1363ecf1385dSKirill A. Shutemov unmap_single_vma(&tlb, vma, start, end, NULL); 13647e027b14SLinus Torvalds mmu_notifier_invalidate_range_end(mm, start, end); 13657e027b14SLinus Torvalds tlb_finish_mmu(&tlb, start, end); 13661da177e4SLinus Torvalds } 13671da177e4SLinus Torvalds 1368c627f9ccSJack Steiner /** 1369f5cc4eefSAl Viro * zap_page_range_single - remove user pages in a given range 1370f5cc4eefSAl Viro * @vma: vm_area_struct holding the applicable pages 1371f5cc4eefSAl Viro * @address: starting address of pages to zap 1372f5cc4eefSAl Viro * @size: number of bytes to zap 13738a5f14a2SKirill A. Shutemov * @details: details of shared cache invalidation 1374f5cc4eefSAl Viro * 1375f5cc4eefSAl Viro * The range must fit into one VMA. 1376f5cc4eefSAl Viro */ 1377f5cc4eefSAl Viro static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address, 1378f5cc4eefSAl Viro unsigned long size, struct zap_details *details) 1379f5cc4eefSAl Viro { 1380f5cc4eefSAl Viro struct mm_struct *mm = vma->vm_mm; 1381f5cc4eefSAl Viro struct mmu_gather tlb; 1382f5cc4eefSAl Viro unsigned long end = address + size; 1383f5cc4eefSAl Viro 1384f5cc4eefSAl Viro lru_add_drain(); 13852b047252SLinus Torvalds tlb_gather_mmu(&tlb, mm, address, end); 1386f5cc4eefSAl Viro update_hiwater_rss(mm); 1387f5cc4eefSAl Viro mmu_notifier_invalidate_range_start(mm, address, end); 13884f74d2c8SLinus Torvalds unmap_single_vma(&tlb, vma, address, end, details); 1389f5cc4eefSAl Viro mmu_notifier_invalidate_range_end(mm, address, end); 1390f5cc4eefSAl Viro tlb_finish_mmu(&tlb, address, end); 13911da177e4SLinus Torvalds } 13921da177e4SLinus Torvalds 1393c627f9ccSJack Steiner /** 1394c627f9ccSJack Steiner * zap_vma_ptes - remove ptes mapping the vma 1395c627f9ccSJack Steiner * @vma: vm_area_struct holding ptes to be zapped 1396c627f9ccSJack Steiner * @address: starting address of pages to zap 1397c627f9ccSJack Steiner * @size: number of bytes to zap 1398c627f9ccSJack Steiner * 1399c627f9ccSJack Steiner * This function only unmaps ptes assigned to VM_PFNMAP vmas. 1400c627f9ccSJack Steiner * 1401c627f9ccSJack Steiner * The entire address range must be fully contained within the vma. 1402c627f9ccSJack Steiner * 1403c627f9ccSJack Steiner */ 140427d036e3SLeon Romanovsky void zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, 1405c627f9ccSJack Steiner unsigned long size) 1406c627f9ccSJack Steiner { 1407c627f9ccSJack Steiner if (address < vma->vm_start || address + size > vma->vm_end || 1408c627f9ccSJack Steiner !(vma->vm_flags & VM_PFNMAP)) 140927d036e3SLeon Romanovsky return; 141027d036e3SLeon Romanovsky 1411f5cc4eefSAl Viro zap_page_range_single(vma, address, size, NULL); 1412c627f9ccSJack Steiner } 1413c627f9ccSJack Steiner EXPORT_SYMBOL_GPL(zap_vma_ptes); 1414c627f9ccSJack Steiner 141525ca1d6cSNamhyung Kim pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr, 1416920c7a5dSHarvey Harrison spinlock_t **ptl) 1417c9cfcddfSLinus Torvalds { 1418c2febafcSKirill A. Shutemov pgd_t *pgd; 1419c2febafcSKirill A. Shutemov p4d_t *p4d; 1420c2febafcSKirill A. Shutemov pud_t *pud; 1421c2febafcSKirill A. Shutemov pmd_t *pmd; 1422c2febafcSKirill A. Shutemov 1423c2febafcSKirill A. Shutemov pgd = pgd_offset(mm, addr); 1424c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, addr); 1425c2febafcSKirill A. Shutemov if (!p4d) 1426c2febafcSKirill A. Shutemov return NULL; 1427c2febafcSKirill A. Shutemov pud = pud_alloc(mm, p4d, addr); 1428c2febafcSKirill A. Shutemov if (!pud) 1429c2febafcSKirill A. Shutemov return NULL; 1430c2febafcSKirill A. Shutemov pmd = pmd_alloc(mm, pud, addr); 1431c2febafcSKirill A. Shutemov if (!pmd) 1432c2febafcSKirill A. Shutemov return NULL; 1433c2febafcSKirill A. Shutemov 1434f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 1435c9cfcddfSLinus Torvalds return pte_alloc_map_lock(mm, pmd, addr, ptl); 1436c9cfcddfSLinus Torvalds } 1437c9cfcddfSLinus Torvalds 14381da177e4SLinus Torvalds /* 1439238f58d8SLinus Torvalds * This is the old fallback for page remapping. 1440238f58d8SLinus Torvalds * 1441238f58d8SLinus Torvalds * For historical reasons, it only allows reserved pages. Only 1442238f58d8SLinus Torvalds * old drivers should use this, and they needed to mark their 1443238f58d8SLinus Torvalds * pages reserved for the old functions anyway. 1444238f58d8SLinus Torvalds */ 1445423bad60SNick Piggin static int insert_page(struct vm_area_struct *vma, unsigned long addr, 1446423bad60SNick Piggin struct page *page, pgprot_t prot) 1447238f58d8SLinus Torvalds { 1448423bad60SNick Piggin struct mm_struct *mm = vma->vm_mm; 1449238f58d8SLinus Torvalds int retval; 1450238f58d8SLinus Torvalds pte_t *pte; 1451238f58d8SLinus Torvalds spinlock_t *ptl; 1452238f58d8SLinus Torvalds 1453238f58d8SLinus Torvalds retval = -EINVAL; 1454a145dd41SLinus Torvalds if (PageAnon(page)) 14555b4e655eSKAMEZAWA Hiroyuki goto out; 1456238f58d8SLinus Torvalds retval = -ENOMEM; 1457238f58d8SLinus Torvalds flush_dcache_page(page); 1458c9cfcddfSLinus Torvalds pte = get_locked_pte(mm, addr, &ptl); 1459238f58d8SLinus Torvalds if (!pte) 14605b4e655eSKAMEZAWA Hiroyuki goto out; 1461238f58d8SLinus Torvalds retval = -EBUSY; 1462238f58d8SLinus Torvalds if (!pte_none(*pte)) 1463238f58d8SLinus Torvalds goto out_unlock; 1464238f58d8SLinus Torvalds 1465238f58d8SLinus Torvalds /* Ok, finally just insert the thing.. */ 1466238f58d8SLinus Torvalds get_page(page); 1467eca56ff9SJerome Marchand inc_mm_counter_fast(mm, mm_counter_file(page)); 1468dd78feddSKirill A. Shutemov page_add_file_rmap(page, false); 1469238f58d8SLinus Torvalds set_pte_at(mm, addr, pte, mk_pte(page, prot)); 1470238f58d8SLinus Torvalds 1471238f58d8SLinus Torvalds retval = 0; 14728a9f3ccdSBalbir Singh pte_unmap_unlock(pte, ptl); 14738a9f3ccdSBalbir Singh return retval; 1474238f58d8SLinus Torvalds out_unlock: 1475238f58d8SLinus Torvalds pte_unmap_unlock(pte, ptl); 1476238f58d8SLinus Torvalds out: 1477238f58d8SLinus Torvalds return retval; 1478238f58d8SLinus Torvalds } 1479238f58d8SLinus Torvalds 1480bfa5bf6dSRolf Eike Beer /** 1481bfa5bf6dSRolf Eike Beer * vm_insert_page - insert single page into user vma 1482bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 1483bfa5bf6dSRolf Eike Beer * @addr: target user address of this page 1484bfa5bf6dSRolf Eike Beer * @page: source kernel page 1485bfa5bf6dSRolf Eike Beer * 1486a145dd41SLinus Torvalds * This allows drivers to insert individual pages they've allocated 1487a145dd41SLinus Torvalds * into a user vma. 1488a145dd41SLinus Torvalds * 1489a145dd41SLinus Torvalds * The page has to be a nice clean _individual_ kernel allocation. 1490a145dd41SLinus Torvalds * If you allocate a compound page, you need to have marked it as 1491a145dd41SLinus Torvalds * such (__GFP_COMP), or manually just split the page up yourself 14928dfcc9baSNick Piggin * (see split_page()). 1493a145dd41SLinus Torvalds * 1494a145dd41SLinus Torvalds * NOTE! Traditionally this was done with "remap_pfn_range()" which 1495a145dd41SLinus Torvalds * took an arbitrary page protection parameter. This doesn't allow 1496a145dd41SLinus Torvalds * that. Your vma protection will have to be set up correctly, which 1497a145dd41SLinus Torvalds * means that if you want a shared writable mapping, you'd better 1498a145dd41SLinus Torvalds * ask for a shared writable mapping! 1499a145dd41SLinus Torvalds * 1500a145dd41SLinus Torvalds * The page does not need to be reserved. 15014b6e1e37SKonstantin Khlebnikov * 15024b6e1e37SKonstantin Khlebnikov * Usually this function is called from f_op->mmap() handler 15034b6e1e37SKonstantin Khlebnikov * under mm->mmap_sem write-lock, so it can change vma->vm_flags. 15044b6e1e37SKonstantin Khlebnikov * Caller must set VM_MIXEDMAP on vma if it wants to call this 15054b6e1e37SKonstantin Khlebnikov * function from other places, for example from page-fault handler. 1506a145dd41SLinus Torvalds */ 1507423bad60SNick Piggin int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, 1508423bad60SNick Piggin struct page *page) 1509a145dd41SLinus Torvalds { 1510a145dd41SLinus Torvalds if (addr < vma->vm_start || addr >= vma->vm_end) 1511a145dd41SLinus Torvalds return -EFAULT; 1512a145dd41SLinus Torvalds if (!page_count(page)) 1513a145dd41SLinus Torvalds return -EINVAL; 15144b6e1e37SKonstantin Khlebnikov if (!(vma->vm_flags & VM_MIXEDMAP)) { 15154b6e1e37SKonstantin Khlebnikov BUG_ON(down_read_trylock(&vma->vm_mm->mmap_sem)); 15164b6e1e37SKonstantin Khlebnikov BUG_ON(vma->vm_flags & VM_PFNMAP); 15174b6e1e37SKonstantin Khlebnikov vma->vm_flags |= VM_MIXEDMAP; 15184b6e1e37SKonstantin Khlebnikov } 1519423bad60SNick Piggin return insert_page(vma, addr, page, vma->vm_page_prot); 1520a145dd41SLinus Torvalds } 1521e3c3374fSLinus Torvalds EXPORT_SYMBOL(vm_insert_page); 1522a145dd41SLinus Torvalds 1523423bad60SNick Piggin static int insert_pfn(struct vm_area_struct *vma, unsigned long addr, 1524b2770da6SRoss Zwisler pfn_t pfn, pgprot_t prot, bool mkwrite) 1525423bad60SNick Piggin { 1526423bad60SNick Piggin struct mm_struct *mm = vma->vm_mm; 1527423bad60SNick Piggin int retval; 1528423bad60SNick Piggin pte_t *pte, entry; 1529423bad60SNick Piggin spinlock_t *ptl; 1530423bad60SNick Piggin 1531423bad60SNick Piggin retval = -ENOMEM; 1532423bad60SNick Piggin pte = get_locked_pte(mm, addr, &ptl); 1533423bad60SNick Piggin if (!pte) 1534423bad60SNick Piggin goto out; 1535423bad60SNick Piggin retval = -EBUSY; 1536b2770da6SRoss Zwisler if (!pte_none(*pte)) { 1537b2770da6SRoss Zwisler if (mkwrite) { 1538b2770da6SRoss Zwisler /* 1539b2770da6SRoss Zwisler * For read faults on private mappings the PFN passed 1540b2770da6SRoss Zwisler * in may not match the PFN we have mapped if the 1541b2770da6SRoss Zwisler * mapped PFN is a writeable COW page. In the mkwrite 1542b2770da6SRoss Zwisler * case we are creating a writable PTE for a shared 1543b2770da6SRoss Zwisler * mapping and we expect the PFNs to match. 1544b2770da6SRoss Zwisler */ 1545b2770da6SRoss Zwisler if (WARN_ON_ONCE(pte_pfn(*pte) != pfn_t_to_pfn(pfn))) 1546423bad60SNick Piggin goto out_unlock; 1547b2770da6SRoss Zwisler entry = *pte; 1548b2770da6SRoss Zwisler goto out_mkwrite; 1549b2770da6SRoss Zwisler } else 1550b2770da6SRoss Zwisler goto out_unlock; 1551b2770da6SRoss Zwisler } 1552423bad60SNick Piggin 1553423bad60SNick Piggin /* Ok, finally just insert the thing.. */ 155401c8f1c4SDan Williams if (pfn_t_devmap(pfn)) 155501c8f1c4SDan Williams entry = pte_mkdevmap(pfn_t_pte(pfn, prot)); 155601c8f1c4SDan Williams else 155701c8f1c4SDan Williams entry = pte_mkspecial(pfn_t_pte(pfn, prot)); 1558b2770da6SRoss Zwisler 1559b2770da6SRoss Zwisler out_mkwrite: 1560b2770da6SRoss Zwisler if (mkwrite) { 1561b2770da6SRoss Zwisler entry = pte_mkyoung(entry); 1562b2770da6SRoss Zwisler entry = maybe_mkwrite(pte_mkdirty(entry), vma); 1563b2770da6SRoss Zwisler } 1564b2770da6SRoss Zwisler 1565423bad60SNick Piggin set_pte_at(mm, addr, pte, entry); 15664b3073e1SRussell King update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */ 1567423bad60SNick Piggin 1568423bad60SNick Piggin retval = 0; 1569423bad60SNick Piggin out_unlock: 1570423bad60SNick Piggin pte_unmap_unlock(pte, ptl); 1571423bad60SNick Piggin out: 1572423bad60SNick Piggin return retval; 1573423bad60SNick Piggin } 1574423bad60SNick Piggin 1575e0dc0d8fSNick Piggin /** 1576e0dc0d8fSNick Piggin * vm_insert_pfn - insert single pfn into user vma 1577e0dc0d8fSNick Piggin * @vma: user vma to map to 1578e0dc0d8fSNick Piggin * @addr: target user address of this page 1579e0dc0d8fSNick Piggin * @pfn: source kernel pfn 1580e0dc0d8fSNick Piggin * 1581c462f179SRobert P. J. Day * Similar to vm_insert_page, this allows drivers to insert individual pages 1582e0dc0d8fSNick Piggin * they've allocated into a user vma. Same comments apply. 1583e0dc0d8fSNick Piggin * 1584e0dc0d8fSNick Piggin * This function should only be called from a vm_ops->fault handler, and 1585e0dc0d8fSNick Piggin * in that case the handler should return NULL. 15860d71d10aSNick Piggin * 15870d71d10aSNick Piggin * vma cannot be a COW mapping. 15880d71d10aSNick Piggin * 15890d71d10aSNick Piggin * As this is called only for pages that do not currently exist, we 15900d71d10aSNick Piggin * do not need to flush old virtual caches or the TLB. 1591e0dc0d8fSNick Piggin */ 1592e0dc0d8fSNick Piggin int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr, 1593e0dc0d8fSNick Piggin unsigned long pfn) 1594e0dc0d8fSNick Piggin { 15951745cbc5SAndy Lutomirski return vm_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot); 15961745cbc5SAndy Lutomirski } 15971745cbc5SAndy Lutomirski EXPORT_SYMBOL(vm_insert_pfn); 15981745cbc5SAndy Lutomirski 15991745cbc5SAndy Lutomirski /** 16001745cbc5SAndy Lutomirski * vm_insert_pfn_prot - insert single pfn into user vma with specified pgprot 16011745cbc5SAndy Lutomirski * @vma: user vma to map to 16021745cbc5SAndy Lutomirski * @addr: target user address of this page 16031745cbc5SAndy Lutomirski * @pfn: source kernel pfn 16041745cbc5SAndy Lutomirski * @pgprot: pgprot flags for the inserted page 16051745cbc5SAndy Lutomirski * 16061745cbc5SAndy Lutomirski * This is exactly like vm_insert_pfn, except that it allows drivers to 16071745cbc5SAndy Lutomirski * to override pgprot on a per-page basis. 16081745cbc5SAndy Lutomirski * 16091745cbc5SAndy Lutomirski * This only makes sense for IO mappings, and it makes no sense for 16101745cbc5SAndy Lutomirski * cow mappings. In general, using multiple vmas is preferable; 16111745cbc5SAndy Lutomirski * vm_insert_pfn_prot should only be used if using multiple VMAs is 16121745cbc5SAndy Lutomirski * impractical. 16131745cbc5SAndy Lutomirski */ 16141745cbc5SAndy Lutomirski int vm_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr, 16151745cbc5SAndy Lutomirski unsigned long pfn, pgprot_t pgprot) 16161745cbc5SAndy Lutomirski { 16172ab64037Svenkatesh.pallipadi@intel.com int ret; 16187e675137SNick Piggin /* 16197e675137SNick Piggin * Technically, architectures with pte_special can avoid all these 16207e675137SNick Piggin * restrictions (same for remap_pfn_range). However we would like 16217e675137SNick Piggin * consistency in testing and feature parity among all, so we should 16227e675137SNick Piggin * try to keep these invariants in place for everybody. 16237e675137SNick Piggin */ 1624b379d790SJared Hulbert BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))); 1625b379d790SJared Hulbert BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) == 1626b379d790SJared Hulbert (VM_PFNMAP|VM_MIXEDMAP)); 1627b379d790SJared Hulbert BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); 1628b379d790SJared Hulbert BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn)); 1629e0dc0d8fSNick Piggin 1630423bad60SNick Piggin if (addr < vma->vm_start || addr >= vma->vm_end) 1631423bad60SNick Piggin return -EFAULT; 1632308a047cSBorislav Petkov 163342e4089cSAndi Kleen if (!pfn_modify_allowed(pfn, pgprot)) 163442e4089cSAndi Kleen return -EACCES; 163542e4089cSAndi Kleen 1636308a047cSBorislav Petkov track_pfn_insert(vma, &pgprot, __pfn_to_pfn_t(pfn, PFN_DEV)); 16372ab64037Svenkatesh.pallipadi@intel.com 1638b2770da6SRoss Zwisler ret = insert_pfn(vma, addr, __pfn_to_pfn_t(pfn, PFN_DEV), pgprot, 1639b2770da6SRoss Zwisler false); 16402ab64037Svenkatesh.pallipadi@intel.com 16412ab64037Svenkatesh.pallipadi@intel.com return ret; 1642e0dc0d8fSNick Piggin } 16431745cbc5SAndy Lutomirski EXPORT_SYMBOL(vm_insert_pfn_prot); 1644e0dc0d8fSNick Piggin 1645785a3fabSDan Williams static bool vm_mixed_ok(struct vm_area_struct *vma, pfn_t pfn) 1646785a3fabSDan Williams { 1647785a3fabSDan Williams /* these checks mirror the abort conditions in vm_normal_page */ 1648785a3fabSDan Williams if (vma->vm_flags & VM_MIXEDMAP) 1649785a3fabSDan Williams return true; 1650785a3fabSDan Williams if (pfn_t_devmap(pfn)) 1651785a3fabSDan Williams return true; 1652785a3fabSDan Williams if (pfn_t_special(pfn)) 1653785a3fabSDan Williams return true; 1654785a3fabSDan Williams if (is_zero_pfn(pfn_t_to_pfn(pfn))) 1655785a3fabSDan Williams return true; 1656785a3fabSDan Williams return false; 1657785a3fabSDan Williams } 1658785a3fabSDan Williams 1659b2770da6SRoss Zwisler static int __vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr, 1660b2770da6SRoss Zwisler pfn_t pfn, bool mkwrite) 1661423bad60SNick Piggin { 166287744ab3SDan Williams pgprot_t pgprot = vma->vm_page_prot; 166387744ab3SDan Williams 1664785a3fabSDan Williams BUG_ON(!vm_mixed_ok(vma, pfn)); 1665423bad60SNick Piggin 1666423bad60SNick Piggin if (addr < vma->vm_start || addr >= vma->vm_end) 1667423bad60SNick Piggin return -EFAULT; 1668308a047cSBorislav Petkov 1669308a047cSBorislav Petkov track_pfn_insert(vma, &pgprot, pfn); 1670423bad60SNick Piggin 167142e4089cSAndi Kleen if (!pfn_modify_allowed(pfn_t_to_pfn(pfn), pgprot)) 167242e4089cSAndi Kleen return -EACCES; 167342e4089cSAndi Kleen 1674423bad60SNick Piggin /* 1675423bad60SNick Piggin * If we don't have pte special, then we have to use the pfn_valid() 1676423bad60SNick Piggin * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must* 1677423bad60SNick Piggin * refcount the page if pfn_valid is true (hence insert_page rather 167862eede62SHugh Dickins * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP 167962eede62SHugh Dickins * without pte special, it would there be refcounted as a normal page. 1680423bad60SNick Piggin */ 168100b3a331SLaurent Dufour if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) && 168200b3a331SLaurent Dufour !pfn_t_devmap(pfn) && pfn_t_valid(pfn)) { 1683423bad60SNick Piggin struct page *page; 1684423bad60SNick Piggin 168503fc2da6SDan Williams /* 168603fc2da6SDan Williams * At this point we are committed to insert_page() 168703fc2da6SDan Williams * regardless of whether the caller specified flags that 168803fc2da6SDan Williams * result in pfn_t_has_page() == false. 168903fc2da6SDan Williams */ 169003fc2da6SDan Williams page = pfn_to_page(pfn_t_to_pfn(pfn)); 169187744ab3SDan Williams return insert_page(vma, addr, page, pgprot); 1692423bad60SNick Piggin } 1693b2770da6SRoss Zwisler return insert_pfn(vma, addr, pfn, pgprot, mkwrite); 1694b2770da6SRoss Zwisler } 1695b2770da6SRoss Zwisler 1696*5d747637SMatthew Wilcox vm_fault_t vmf_insert_mixed(struct vm_area_struct *vma, unsigned long addr, 1697b2770da6SRoss Zwisler pfn_t pfn) 1698b2770da6SRoss Zwisler { 1699*5d747637SMatthew Wilcox int err = __vm_insert_mixed(vma, addr, pfn, false); 1700b2770da6SRoss Zwisler 1701*5d747637SMatthew Wilcox if (err == -ENOMEM) 1702*5d747637SMatthew Wilcox return VM_FAULT_OOM; 1703*5d747637SMatthew Wilcox if (err < 0 && err != -EBUSY) 1704*5d747637SMatthew Wilcox return VM_FAULT_SIGBUS; 1705*5d747637SMatthew Wilcox 1706*5d747637SMatthew Wilcox return VM_FAULT_NOPAGE; 1707423bad60SNick Piggin } 1708*5d747637SMatthew Wilcox EXPORT_SYMBOL(vmf_insert_mixed); 1709423bad60SNick Piggin 1710ab77dab4SSouptick Joarder /* 1711ab77dab4SSouptick Joarder * If the insertion of PTE failed because someone else already added a 1712ab77dab4SSouptick Joarder * different entry in the mean time, we treat that as success as we assume 1713ab77dab4SSouptick Joarder * the same entry was actually inserted. 1714ab77dab4SSouptick Joarder */ 1715ab77dab4SSouptick Joarder 1716ab77dab4SSouptick Joarder vm_fault_t vmf_insert_mixed_mkwrite(struct vm_area_struct *vma, 1717ab77dab4SSouptick Joarder unsigned long addr, pfn_t pfn) 1718b2770da6SRoss Zwisler { 1719ab77dab4SSouptick Joarder int err; 1720ab77dab4SSouptick Joarder 1721ab77dab4SSouptick Joarder err = __vm_insert_mixed(vma, addr, pfn, true); 1722ab77dab4SSouptick Joarder if (err == -ENOMEM) 1723ab77dab4SSouptick Joarder return VM_FAULT_OOM; 1724ab77dab4SSouptick Joarder if (err < 0 && err != -EBUSY) 1725ab77dab4SSouptick Joarder return VM_FAULT_SIGBUS; 1726ab77dab4SSouptick Joarder return VM_FAULT_NOPAGE; 1727b2770da6SRoss Zwisler } 1728ab77dab4SSouptick Joarder EXPORT_SYMBOL(vmf_insert_mixed_mkwrite); 1729b2770da6SRoss Zwisler 1730a145dd41SLinus Torvalds /* 17311da177e4SLinus Torvalds * maps a range of physical memory into the requested pages. the old 17321da177e4SLinus Torvalds * mappings are removed. any references to nonexistent pages results 17331da177e4SLinus Torvalds * in null mappings (currently treated as "copy-on-access") 17341da177e4SLinus Torvalds */ 17351da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd, 17361da177e4SLinus Torvalds unsigned long addr, unsigned long end, 17371da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 17381da177e4SLinus Torvalds { 17391da177e4SLinus Torvalds pte_t *pte; 1740c74df32cSHugh Dickins spinlock_t *ptl; 174142e4089cSAndi Kleen int err = 0; 17421da177e4SLinus Torvalds 1743c74df32cSHugh Dickins pte = pte_alloc_map_lock(mm, pmd, addr, &ptl); 17441da177e4SLinus Torvalds if (!pte) 17451da177e4SLinus Torvalds return -ENOMEM; 17466606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 17471da177e4SLinus Torvalds do { 17481da177e4SLinus Torvalds BUG_ON(!pte_none(*pte)); 174942e4089cSAndi Kleen if (!pfn_modify_allowed(pfn, prot)) { 175042e4089cSAndi Kleen err = -EACCES; 175142e4089cSAndi Kleen break; 175242e4089cSAndi Kleen } 17537e675137SNick Piggin set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot))); 17541da177e4SLinus Torvalds pfn++; 17551da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 17566606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 1757c74df32cSHugh Dickins pte_unmap_unlock(pte - 1, ptl); 175842e4089cSAndi Kleen return err; 17591da177e4SLinus Torvalds } 17601da177e4SLinus Torvalds 17611da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud, 17621da177e4SLinus Torvalds unsigned long addr, unsigned long end, 17631da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 17641da177e4SLinus Torvalds { 17651da177e4SLinus Torvalds pmd_t *pmd; 17661da177e4SLinus Torvalds unsigned long next; 176742e4089cSAndi Kleen int err; 17681da177e4SLinus Torvalds 17691da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 17701da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, addr); 17711da177e4SLinus Torvalds if (!pmd) 17721da177e4SLinus Torvalds return -ENOMEM; 1773f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 17741da177e4SLinus Torvalds do { 17751da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 177642e4089cSAndi Kleen err = remap_pte_range(mm, pmd, addr, next, 177742e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 177842e4089cSAndi Kleen if (err) 177942e4089cSAndi Kleen return err; 17801da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 17811da177e4SLinus Torvalds return 0; 17821da177e4SLinus Torvalds } 17831da177e4SLinus Torvalds 1784c2febafcSKirill A. Shutemov static inline int remap_pud_range(struct mm_struct *mm, p4d_t *p4d, 17851da177e4SLinus Torvalds unsigned long addr, unsigned long end, 17861da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 17871da177e4SLinus Torvalds { 17881da177e4SLinus Torvalds pud_t *pud; 17891da177e4SLinus Torvalds unsigned long next; 179042e4089cSAndi Kleen int err; 17911da177e4SLinus Torvalds 17921da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 1793c2febafcSKirill A. Shutemov pud = pud_alloc(mm, p4d, addr); 17941da177e4SLinus Torvalds if (!pud) 17951da177e4SLinus Torvalds return -ENOMEM; 17961da177e4SLinus Torvalds do { 17971da177e4SLinus Torvalds next = pud_addr_end(addr, end); 179842e4089cSAndi Kleen err = remap_pmd_range(mm, pud, addr, next, 179942e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 180042e4089cSAndi Kleen if (err) 180142e4089cSAndi Kleen return err; 18021da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 18031da177e4SLinus Torvalds return 0; 18041da177e4SLinus Torvalds } 18051da177e4SLinus Torvalds 1806c2febafcSKirill A. Shutemov static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd, 1807c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 1808c2febafcSKirill A. Shutemov unsigned long pfn, pgprot_t prot) 1809c2febafcSKirill A. Shutemov { 1810c2febafcSKirill A. Shutemov p4d_t *p4d; 1811c2febafcSKirill A. Shutemov unsigned long next; 181242e4089cSAndi Kleen int err; 1813c2febafcSKirill A. Shutemov 1814c2febafcSKirill A. Shutemov pfn -= addr >> PAGE_SHIFT; 1815c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, addr); 1816c2febafcSKirill A. Shutemov if (!p4d) 1817c2febafcSKirill A. Shutemov return -ENOMEM; 1818c2febafcSKirill A. Shutemov do { 1819c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 182042e4089cSAndi Kleen err = remap_pud_range(mm, p4d, addr, next, 182142e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 182242e4089cSAndi Kleen if (err) 182342e4089cSAndi Kleen return err; 1824c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 1825c2febafcSKirill A. Shutemov return 0; 1826c2febafcSKirill A. Shutemov } 1827c2febafcSKirill A. Shutemov 1828bfa5bf6dSRolf Eike Beer /** 1829bfa5bf6dSRolf Eike Beer * remap_pfn_range - remap kernel memory to userspace 1830bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 1831bfa5bf6dSRolf Eike Beer * @addr: target user address to start at 1832bfa5bf6dSRolf Eike Beer * @pfn: physical address of kernel memory 1833bfa5bf6dSRolf Eike Beer * @size: size of map area 1834bfa5bf6dSRolf Eike Beer * @prot: page protection flags for this mapping 1835bfa5bf6dSRolf Eike Beer * 1836bfa5bf6dSRolf Eike Beer * Note: this is only safe if the mm semaphore is held when called. 1837bfa5bf6dSRolf Eike Beer */ 18381da177e4SLinus Torvalds int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 18391da177e4SLinus Torvalds unsigned long pfn, unsigned long size, pgprot_t prot) 18401da177e4SLinus Torvalds { 18411da177e4SLinus Torvalds pgd_t *pgd; 18421da177e4SLinus Torvalds unsigned long next; 18432d15cab8SHugh Dickins unsigned long end = addr + PAGE_ALIGN(size); 18441da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 1845d5957d2fSYongji Xie unsigned long remap_pfn = pfn; 18461da177e4SLinus Torvalds int err; 18471da177e4SLinus Torvalds 18481da177e4SLinus Torvalds /* 18491da177e4SLinus Torvalds * Physically remapped pages are special. Tell the 18501da177e4SLinus Torvalds * rest of the world about it: 18511da177e4SLinus Torvalds * VM_IO tells people not to look at these pages 18521da177e4SLinus Torvalds * (accesses can have side effects). 18536aab341eSLinus Torvalds * VM_PFNMAP tells the core MM that the base pages are just 18546aab341eSLinus Torvalds * raw PFN mappings, and do not have a "struct page" associated 18556aab341eSLinus Torvalds * with them. 1856314e51b9SKonstantin Khlebnikov * VM_DONTEXPAND 1857314e51b9SKonstantin Khlebnikov * Disable vma merging and expanding with mremap(). 1858314e51b9SKonstantin Khlebnikov * VM_DONTDUMP 1859314e51b9SKonstantin Khlebnikov * Omit vma from core dump, even when VM_IO turned off. 1860fb155c16SLinus Torvalds * 1861fb155c16SLinus Torvalds * There's a horrible special case to handle copy-on-write 1862fb155c16SLinus Torvalds * behaviour that some programs depend on. We mark the "original" 1863fb155c16SLinus Torvalds * un-COW'ed pages by matching them up with "vma->vm_pgoff". 1864b3b9c293SKonstantin Khlebnikov * See vm_normal_page() for details. 18651da177e4SLinus Torvalds */ 1866b3b9c293SKonstantin Khlebnikov if (is_cow_mapping(vma->vm_flags)) { 1867b3b9c293SKonstantin Khlebnikov if (addr != vma->vm_start || end != vma->vm_end) 1868b3b9c293SKonstantin Khlebnikov return -EINVAL; 18696aab341eSLinus Torvalds vma->vm_pgoff = pfn; 1870b3b9c293SKonstantin Khlebnikov } 1871b3b9c293SKonstantin Khlebnikov 1872d5957d2fSYongji Xie err = track_pfn_remap(vma, &prot, remap_pfn, addr, PAGE_ALIGN(size)); 1873b3b9c293SKonstantin Khlebnikov if (err) 18743c8bb73aSvenkatesh.pallipadi@intel.com return -EINVAL; 1875fb155c16SLinus Torvalds 1876314e51b9SKonstantin Khlebnikov vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; 18771da177e4SLinus Torvalds 18781da177e4SLinus Torvalds BUG_ON(addr >= end); 18791da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 18801da177e4SLinus Torvalds pgd = pgd_offset(mm, addr); 18811da177e4SLinus Torvalds flush_cache_range(vma, addr, end); 18821da177e4SLinus Torvalds do { 18831da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 1884c2febafcSKirill A. Shutemov err = remap_p4d_range(mm, pgd, addr, next, 18851da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot); 18861da177e4SLinus Torvalds if (err) 18871da177e4SLinus Torvalds break; 18881da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 18892ab64037Svenkatesh.pallipadi@intel.com 18902ab64037Svenkatesh.pallipadi@intel.com if (err) 1891d5957d2fSYongji Xie untrack_pfn(vma, remap_pfn, PAGE_ALIGN(size)); 18922ab64037Svenkatesh.pallipadi@intel.com 18931da177e4SLinus Torvalds return err; 18941da177e4SLinus Torvalds } 18951da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range); 18961da177e4SLinus Torvalds 1897b4cbb197SLinus Torvalds /** 1898b4cbb197SLinus Torvalds * vm_iomap_memory - remap memory to userspace 1899b4cbb197SLinus Torvalds * @vma: user vma to map to 1900b4cbb197SLinus Torvalds * @start: start of area 1901b4cbb197SLinus Torvalds * @len: size of area 1902b4cbb197SLinus Torvalds * 1903b4cbb197SLinus Torvalds * This is a simplified io_remap_pfn_range() for common driver use. The 1904b4cbb197SLinus Torvalds * driver just needs to give us the physical memory range to be mapped, 1905b4cbb197SLinus Torvalds * we'll figure out the rest from the vma information. 1906b4cbb197SLinus Torvalds * 1907b4cbb197SLinus Torvalds * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get 1908b4cbb197SLinus Torvalds * whatever write-combining details or similar. 1909b4cbb197SLinus Torvalds */ 1910b4cbb197SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len) 1911b4cbb197SLinus Torvalds { 1912b4cbb197SLinus Torvalds unsigned long vm_len, pfn, pages; 1913b4cbb197SLinus Torvalds 1914b4cbb197SLinus Torvalds /* Check that the physical memory area passed in looks valid */ 1915b4cbb197SLinus Torvalds if (start + len < start) 1916b4cbb197SLinus Torvalds return -EINVAL; 1917b4cbb197SLinus Torvalds /* 1918b4cbb197SLinus Torvalds * You *really* shouldn't map things that aren't page-aligned, 1919b4cbb197SLinus Torvalds * but we've historically allowed it because IO memory might 1920b4cbb197SLinus Torvalds * just have smaller alignment. 1921b4cbb197SLinus Torvalds */ 1922b4cbb197SLinus Torvalds len += start & ~PAGE_MASK; 1923b4cbb197SLinus Torvalds pfn = start >> PAGE_SHIFT; 1924b4cbb197SLinus Torvalds pages = (len + ~PAGE_MASK) >> PAGE_SHIFT; 1925b4cbb197SLinus Torvalds if (pfn + pages < pfn) 1926b4cbb197SLinus Torvalds return -EINVAL; 1927b4cbb197SLinus Torvalds 1928b4cbb197SLinus Torvalds /* We start the mapping 'vm_pgoff' pages into the area */ 1929b4cbb197SLinus Torvalds if (vma->vm_pgoff > pages) 1930b4cbb197SLinus Torvalds return -EINVAL; 1931b4cbb197SLinus Torvalds pfn += vma->vm_pgoff; 1932b4cbb197SLinus Torvalds pages -= vma->vm_pgoff; 1933b4cbb197SLinus Torvalds 1934b4cbb197SLinus Torvalds /* Can we fit all of the mapping? */ 1935b4cbb197SLinus Torvalds vm_len = vma->vm_end - vma->vm_start; 1936b4cbb197SLinus Torvalds if (vm_len >> PAGE_SHIFT > pages) 1937b4cbb197SLinus Torvalds return -EINVAL; 1938b4cbb197SLinus Torvalds 1939b4cbb197SLinus Torvalds /* Ok, let it rip */ 1940b4cbb197SLinus Torvalds return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot); 1941b4cbb197SLinus Torvalds } 1942b4cbb197SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory); 1943b4cbb197SLinus Torvalds 1944aee16b3cSJeremy Fitzhardinge static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd, 1945aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 1946aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 1947aee16b3cSJeremy Fitzhardinge { 1948aee16b3cSJeremy Fitzhardinge pte_t *pte; 1949aee16b3cSJeremy Fitzhardinge int err; 19502f569afdSMartin Schwidefsky pgtable_t token; 195194909914SBorislav Petkov spinlock_t *uninitialized_var(ptl); 1952aee16b3cSJeremy Fitzhardinge 1953aee16b3cSJeremy Fitzhardinge pte = (mm == &init_mm) ? 1954aee16b3cSJeremy Fitzhardinge pte_alloc_kernel(pmd, addr) : 1955aee16b3cSJeremy Fitzhardinge pte_alloc_map_lock(mm, pmd, addr, &ptl); 1956aee16b3cSJeremy Fitzhardinge if (!pte) 1957aee16b3cSJeremy Fitzhardinge return -ENOMEM; 1958aee16b3cSJeremy Fitzhardinge 1959aee16b3cSJeremy Fitzhardinge BUG_ON(pmd_huge(*pmd)); 1960aee16b3cSJeremy Fitzhardinge 196138e0edb1SJeremy Fitzhardinge arch_enter_lazy_mmu_mode(); 196238e0edb1SJeremy Fitzhardinge 19632f569afdSMartin Schwidefsky token = pmd_pgtable(*pmd); 1964aee16b3cSJeremy Fitzhardinge 1965aee16b3cSJeremy Fitzhardinge do { 1966c36987e2SDaisuke Nishimura err = fn(pte++, token, addr, data); 1967aee16b3cSJeremy Fitzhardinge if (err) 1968aee16b3cSJeremy Fitzhardinge break; 1969c36987e2SDaisuke Nishimura } while (addr += PAGE_SIZE, addr != end); 1970aee16b3cSJeremy Fitzhardinge 197138e0edb1SJeremy Fitzhardinge arch_leave_lazy_mmu_mode(); 197238e0edb1SJeremy Fitzhardinge 1973aee16b3cSJeremy Fitzhardinge if (mm != &init_mm) 1974aee16b3cSJeremy Fitzhardinge pte_unmap_unlock(pte-1, ptl); 1975aee16b3cSJeremy Fitzhardinge return err; 1976aee16b3cSJeremy Fitzhardinge } 1977aee16b3cSJeremy Fitzhardinge 1978aee16b3cSJeremy Fitzhardinge static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud, 1979aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 1980aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 1981aee16b3cSJeremy Fitzhardinge { 1982aee16b3cSJeremy Fitzhardinge pmd_t *pmd; 1983aee16b3cSJeremy Fitzhardinge unsigned long next; 1984aee16b3cSJeremy Fitzhardinge int err; 1985aee16b3cSJeremy Fitzhardinge 1986ceb86879SAndi Kleen BUG_ON(pud_huge(*pud)); 1987ceb86879SAndi Kleen 1988aee16b3cSJeremy Fitzhardinge pmd = pmd_alloc(mm, pud, addr); 1989aee16b3cSJeremy Fitzhardinge if (!pmd) 1990aee16b3cSJeremy Fitzhardinge return -ENOMEM; 1991aee16b3cSJeremy Fitzhardinge do { 1992aee16b3cSJeremy Fitzhardinge next = pmd_addr_end(addr, end); 1993aee16b3cSJeremy Fitzhardinge err = apply_to_pte_range(mm, pmd, addr, next, fn, data); 1994aee16b3cSJeremy Fitzhardinge if (err) 1995aee16b3cSJeremy Fitzhardinge break; 1996aee16b3cSJeremy Fitzhardinge } while (pmd++, addr = next, addr != end); 1997aee16b3cSJeremy Fitzhardinge return err; 1998aee16b3cSJeremy Fitzhardinge } 1999aee16b3cSJeremy Fitzhardinge 2000c2febafcSKirill A. Shutemov static int apply_to_pud_range(struct mm_struct *mm, p4d_t *p4d, 2001aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2002aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 2003aee16b3cSJeremy Fitzhardinge { 2004aee16b3cSJeremy Fitzhardinge pud_t *pud; 2005aee16b3cSJeremy Fitzhardinge unsigned long next; 2006aee16b3cSJeremy Fitzhardinge int err; 2007aee16b3cSJeremy Fitzhardinge 2008c2febafcSKirill A. Shutemov pud = pud_alloc(mm, p4d, addr); 2009aee16b3cSJeremy Fitzhardinge if (!pud) 2010aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2011aee16b3cSJeremy Fitzhardinge do { 2012aee16b3cSJeremy Fitzhardinge next = pud_addr_end(addr, end); 2013aee16b3cSJeremy Fitzhardinge err = apply_to_pmd_range(mm, pud, addr, next, fn, data); 2014aee16b3cSJeremy Fitzhardinge if (err) 2015aee16b3cSJeremy Fitzhardinge break; 2016aee16b3cSJeremy Fitzhardinge } while (pud++, addr = next, addr != end); 2017aee16b3cSJeremy Fitzhardinge return err; 2018aee16b3cSJeremy Fitzhardinge } 2019aee16b3cSJeremy Fitzhardinge 2020c2febafcSKirill A. Shutemov static int apply_to_p4d_range(struct mm_struct *mm, pgd_t *pgd, 2021c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 2022c2febafcSKirill A. Shutemov pte_fn_t fn, void *data) 2023c2febafcSKirill A. Shutemov { 2024c2febafcSKirill A. Shutemov p4d_t *p4d; 2025c2febafcSKirill A. Shutemov unsigned long next; 2026c2febafcSKirill A. Shutemov int err; 2027c2febafcSKirill A. Shutemov 2028c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, addr); 2029c2febafcSKirill A. Shutemov if (!p4d) 2030c2febafcSKirill A. Shutemov return -ENOMEM; 2031c2febafcSKirill A. Shutemov do { 2032c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 2033c2febafcSKirill A. Shutemov err = apply_to_pud_range(mm, p4d, addr, next, fn, data); 2034c2febafcSKirill A. Shutemov if (err) 2035c2febafcSKirill A. Shutemov break; 2036c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 2037c2febafcSKirill A. Shutemov return err; 2038c2febafcSKirill A. Shutemov } 2039c2febafcSKirill A. Shutemov 2040aee16b3cSJeremy Fitzhardinge /* 2041aee16b3cSJeremy Fitzhardinge * Scan a region of virtual memory, filling in page tables as necessary 2042aee16b3cSJeremy Fitzhardinge * and calling a provided function on each leaf page table. 2043aee16b3cSJeremy Fitzhardinge */ 2044aee16b3cSJeremy Fitzhardinge int apply_to_page_range(struct mm_struct *mm, unsigned long addr, 2045aee16b3cSJeremy Fitzhardinge unsigned long size, pte_fn_t fn, void *data) 2046aee16b3cSJeremy Fitzhardinge { 2047aee16b3cSJeremy Fitzhardinge pgd_t *pgd; 2048aee16b3cSJeremy Fitzhardinge unsigned long next; 204957250a5bSJeremy Fitzhardinge unsigned long end = addr + size; 2050aee16b3cSJeremy Fitzhardinge int err; 2051aee16b3cSJeremy Fitzhardinge 20529cb65bc3SMika Penttilä if (WARN_ON(addr >= end)) 20539cb65bc3SMika Penttilä return -EINVAL; 20549cb65bc3SMika Penttilä 2055aee16b3cSJeremy Fitzhardinge pgd = pgd_offset(mm, addr); 2056aee16b3cSJeremy Fitzhardinge do { 2057aee16b3cSJeremy Fitzhardinge next = pgd_addr_end(addr, end); 2058c2febafcSKirill A. Shutemov err = apply_to_p4d_range(mm, pgd, addr, next, fn, data); 2059aee16b3cSJeremy Fitzhardinge if (err) 2060aee16b3cSJeremy Fitzhardinge break; 2061aee16b3cSJeremy Fitzhardinge } while (pgd++, addr = next, addr != end); 206257250a5bSJeremy Fitzhardinge 2063aee16b3cSJeremy Fitzhardinge return err; 2064aee16b3cSJeremy Fitzhardinge } 2065aee16b3cSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(apply_to_page_range); 2066aee16b3cSJeremy Fitzhardinge 20671da177e4SLinus Torvalds /* 20689b4bdd2fSKirill A. Shutemov * handle_pte_fault chooses page fault handler according to an entry which was 20699b4bdd2fSKirill A. Shutemov * read non-atomically. Before making any commitment, on those architectures 20709b4bdd2fSKirill A. Shutemov * or configurations (e.g. i386 with PAE) which might give a mix of unmatched 20719b4bdd2fSKirill A. Shutemov * parts, do_swap_page must check under lock before unmapping the pte and 20729b4bdd2fSKirill A. Shutemov * proceeding (but do_wp_page is only called after already making such a check; 2073a335b2e1SRyota Ozaki * and do_anonymous_page can safely check later on). 20748f4e2101SHugh Dickins */ 20754c21e2f2SHugh Dickins static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd, 20768f4e2101SHugh Dickins pte_t *page_table, pte_t orig_pte) 20778f4e2101SHugh Dickins { 20788f4e2101SHugh Dickins int same = 1; 20798f4e2101SHugh Dickins #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT) 20808f4e2101SHugh Dickins if (sizeof(pte_t) > sizeof(unsigned long)) { 20814c21e2f2SHugh Dickins spinlock_t *ptl = pte_lockptr(mm, pmd); 20824c21e2f2SHugh Dickins spin_lock(ptl); 20838f4e2101SHugh Dickins same = pte_same(*page_table, orig_pte); 20844c21e2f2SHugh Dickins spin_unlock(ptl); 20858f4e2101SHugh Dickins } 20868f4e2101SHugh Dickins #endif 20878f4e2101SHugh Dickins pte_unmap(page_table); 20888f4e2101SHugh Dickins return same; 20898f4e2101SHugh Dickins } 20908f4e2101SHugh Dickins 20919de455b2SAtsushi Nemoto static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma) 20926aab341eSLinus Torvalds { 20930abdd7a8SDan Williams debug_dma_assert_idle(src); 20940abdd7a8SDan Williams 20956aab341eSLinus Torvalds /* 20966aab341eSLinus Torvalds * If the source page was a PFN mapping, we don't have 20976aab341eSLinus Torvalds * a "struct page" for it. We do a best-effort copy by 20986aab341eSLinus Torvalds * just copying from the original user address. If that 20996aab341eSLinus Torvalds * fails, we just zero-fill it. Live with it. 21006aab341eSLinus Torvalds */ 21016aab341eSLinus Torvalds if (unlikely(!src)) { 21029b04c5feSCong Wang void *kaddr = kmap_atomic(dst); 21035d2a2dbbSLinus Torvalds void __user *uaddr = (void __user *)(va & PAGE_MASK); 21045d2a2dbbSLinus Torvalds 21055d2a2dbbSLinus Torvalds /* 21065d2a2dbbSLinus Torvalds * This really shouldn't fail, because the page is there 21075d2a2dbbSLinus Torvalds * in the page tables. But it might just be unreadable, 21085d2a2dbbSLinus Torvalds * in which case we just give up and fill the result with 21095d2a2dbbSLinus Torvalds * zeroes. 21105d2a2dbbSLinus Torvalds */ 21115d2a2dbbSLinus Torvalds if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) 21123ecb01dfSJan Beulich clear_page(kaddr); 21139b04c5feSCong Wang kunmap_atomic(kaddr); 2114c4ec7b0dSDmitriy Monakhov flush_dcache_page(dst); 21150ed361deSNick Piggin } else 21169de455b2SAtsushi Nemoto copy_user_highpage(dst, src, va, vma); 21176aab341eSLinus Torvalds } 21186aab341eSLinus Torvalds 2119c20cd45eSMichal Hocko static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma) 2120c20cd45eSMichal Hocko { 2121c20cd45eSMichal Hocko struct file *vm_file = vma->vm_file; 2122c20cd45eSMichal Hocko 2123c20cd45eSMichal Hocko if (vm_file) 2124c20cd45eSMichal Hocko return mapping_gfp_mask(vm_file->f_mapping) | __GFP_FS | __GFP_IO; 2125c20cd45eSMichal Hocko 2126c20cd45eSMichal Hocko /* 2127c20cd45eSMichal Hocko * Special mappings (e.g. VDSO) do not have any file so fake 2128c20cd45eSMichal Hocko * a default GFP_KERNEL for them. 2129c20cd45eSMichal Hocko */ 2130c20cd45eSMichal Hocko return GFP_KERNEL; 2131c20cd45eSMichal Hocko } 2132c20cd45eSMichal Hocko 21331da177e4SLinus Torvalds /* 2134fb09a464SKirill A. Shutemov * Notify the address space that the page is about to become writable so that 2135fb09a464SKirill A. Shutemov * it can prohibit this or wait for the page to get into an appropriate state. 2136fb09a464SKirill A. Shutemov * 2137fb09a464SKirill A. Shutemov * We do this without the lock held, so that it can sleep if it needs to. 2138fb09a464SKirill A. Shutemov */ 21392b740303SSouptick Joarder static vm_fault_t do_page_mkwrite(struct vm_fault *vmf) 2140fb09a464SKirill A. Shutemov { 21412b740303SSouptick Joarder vm_fault_t ret; 214238b8cb7fSJan Kara struct page *page = vmf->page; 214338b8cb7fSJan Kara unsigned int old_flags = vmf->flags; 2144fb09a464SKirill A. Shutemov 214538b8cb7fSJan Kara vmf->flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE; 2146fb09a464SKirill A. Shutemov 214711bac800SDave Jiang ret = vmf->vma->vm_ops->page_mkwrite(vmf); 214838b8cb7fSJan Kara /* Restore original flags so that caller is not surprised */ 214938b8cb7fSJan Kara vmf->flags = old_flags; 2150fb09a464SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) 2151fb09a464SKirill A. Shutemov return ret; 2152fb09a464SKirill A. Shutemov if (unlikely(!(ret & VM_FAULT_LOCKED))) { 2153fb09a464SKirill A. Shutemov lock_page(page); 2154fb09a464SKirill A. Shutemov if (!page->mapping) { 2155fb09a464SKirill A. Shutemov unlock_page(page); 2156fb09a464SKirill A. Shutemov return 0; /* retry */ 2157fb09a464SKirill A. Shutemov } 2158fb09a464SKirill A. Shutemov ret |= VM_FAULT_LOCKED; 2159fb09a464SKirill A. Shutemov } else 2160fb09a464SKirill A. Shutemov VM_BUG_ON_PAGE(!PageLocked(page), page); 2161fb09a464SKirill A. Shutemov return ret; 2162fb09a464SKirill A. Shutemov } 2163fb09a464SKirill A. Shutemov 2164fb09a464SKirill A. Shutemov /* 216597ba0c2bSJan Kara * Handle dirtying of a page in shared file mapping on a write fault. 21664e047f89SShachar Raindel * 216797ba0c2bSJan Kara * The function expects the page to be locked and unlocks it. 21684e047f89SShachar Raindel */ 216997ba0c2bSJan Kara static void fault_dirty_shared_page(struct vm_area_struct *vma, 217097ba0c2bSJan Kara struct page *page) 21714e047f89SShachar Raindel { 21724e047f89SShachar Raindel struct address_space *mapping; 217397ba0c2bSJan Kara bool dirtied; 217497ba0c2bSJan Kara bool page_mkwrite = vma->vm_ops && vma->vm_ops->page_mkwrite; 21754e047f89SShachar Raindel 21764e047f89SShachar Raindel dirtied = set_page_dirty(page); 21774e047f89SShachar Raindel VM_BUG_ON_PAGE(PageAnon(page), page); 217897ba0c2bSJan Kara /* 217997ba0c2bSJan Kara * Take a local copy of the address_space - page.mapping may be zeroed 218097ba0c2bSJan Kara * by truncate after unlock_page(). The address_space itself remains 218197ba0c2bSJan Kara * pinned by vma->vm_file's reference. We rely on unlock_page()'s 218297ba0c2bSJan Kara * release semantics to prevent the compiler from undoing this copying. 218397ba0c2bSJan Kara */ 218497ba0c2bSJan Kara mapping = page_rmapping(page); 21854e047f89SShachar Raindel unlock_page(page); 21864e047f89SShachar Raindel 21874e047f89SShachar Raindel if ((dirtied || page_mkwrite) && mapping) { 21884e047f89SShachar Raindel /* 21894e047f89SShachar Raindel * Some device drivers do not set page.mapping 21904e047f89SShachar Raindel * but still dirty their pages 21914e047f89SShachar Raindel */ 21924e047f89SShachar Raindel balance_dirty_pages_ratelimited(mapping); 21934e047f89SShachar Raindel } 21944e047f89SShachar Raindel 21954e047f89SShachar Raindel if (!page_mkwrite) 21964e047f89SShachar Raindel file_update_time(vma->vm_file); 21974e047f89SShachar Raindel } 21984e047f89SShachar Raindel 219997ba0c2bSJan Kara /* 22004e047f89SShachar Raindel * Handle write page faults for pages that can be reused in the current vma 22014e047f89SShachar Raindel * 22024e047f89SShachar Raindel * This can happen either due to the mapping being with the VM_SHARED flag, 22034e047f89SShachar Raindel * or due to us being the last reference standing to the page. In either 22044e047f89SShachar Raindel * case, all we need to do here is to mark the page as writable and update 22054e047f89SShachar Raindel * any related book-keeping. 22064e047f89SShachar Raindel */ 2207997dd98dSJan Kara static inline void wp_page_reuse(struct vm_fault *vmf) 220882b0f8c3SJan Kara __releases(vmf->ptl) 22094e047f89SShachar Raindel { 221082b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 2211a41b70d6SJan Kara struct page *page = vmf->page; 22124e047f89SShachar Raindel pte_t entry; 22134e047f89SShachar Raindel /* 22144e047f89SShachar Raindel * Clear the pages cpupid information as the existing 22154e047f89SShachar Raindel * information potentially belongs to a now completely 22164e047f89SShachar Raindel * unrelated process. 22174e047f89SShachar Raindel */ 22184e047f89SShachar Raindel if (page) 22194e047f89SShachar Raindel page_cpupid_xchg_last(page, (1 << LAST_CPUPID_SHIFT) - 1); 22204e047f89SShachar Raindel 22212994302bSJan Kara flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); 22222994302bSJan Kara entry = pte_mkyoung(vmf->orig_pte); 22234e047f89SShachar Raindel entry = maybe_mkwrite(pte_mkdirty(entry), vma); 222482b0f8c3SJan Kara if (ptep_set_access_flags(vma, vmf->address, vmf->pte, entry, 1)) 222582b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 222682b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 22274e047f89SShachar Raindel } 22284e047f89SShachar Raindel 22294e047f89SShachar Raindel /* 22302f38ab2cSShachar Raindel * Handle the case of a page which we actually need to copy to a new page. 22312f38ab2cSShachar Raindel * 22322f38ab2cSShachar Raindel * Called with mmap_sem locked and the old page referenced, but 22332f38ab2cSShachar Raindel * without the ptl held. 22342f38ab2cSShachar Raindel * 22352f38ab2cSShachar Raindel * High level logic flow: 22362f38ab2cSShachar Raindel * 22372f38ab2cSShachar Raindel * - Allocate a page, copy the content of the old page to the new one. 22382f38ab2cSShachar Raindel * - Handle book keeping and accounting - cgroups, mmu-notifiers, etc. 22392f38ab2cSShachar Raindel * - Take the PTL. If the pte changed, bail out and release the allocated page 22402f38ab2cSShachar Raindel * - If the pte is still the way we remember it, update the page table and all 22412f38ab2cSShachar Raindel * relevant references. This includes dropping the reference the page-table 22422f38ab2cSShachar Raindel * held to the old page, as well as updating the rmap. 22432f38ab2cSShachar Raindel * - In any case, unlock the PTL and drop the reference we took to the old page. 22442f38ab2cSShachar Raindel */ 22452b740303SSouptick Joarder static vm_fault_t wp_page_copy(struct vm_fault *vmf) 22462f38ab2cSShachar Raindel { 224782b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 2248bae473a4SKirill A. Shutemov struct mm_struct *mm = vma->vm_mm; 2249a41b70d6SJan Kara struct page *old_page = vmf->page; 22502f38ab2cSShachar Raindel struct page *new_page = NULL; 22512f38ab2cSShachar Raindel pte_t entry; 22522f38ab2cSShachar Raindel int page_copied = 0; 225382b0f8c3SJan Kara const unsigned long mmun_start = vmf->address & PAGE_MASK; 2254bae473a4SKirill A. Shutemov const unsigned long mmun_end = mmun_start + PAGE_SIZE; 22552f38ab2cSShachar Raindel struct mem_cgroup *memcg; 22562f38ab2cSShachar Raindel 22572f38ab2cSShachar Raindel if (unlikely(anon_vma_prepare(vma))) 22582f38ab2cSShachar Raindel goto oom; 22592f38ab2cSShachar Raindel 22602994302bSJan Kara if (is_zero_pfn(pte_pfn(vmf->orig_pte))) { 226182b0f8c3SJan Kara new_page = alloc_zeroed_user_highpage_movable(vma, 226282b0f8c3SJan Kara vmf->address); 22632f38ab2cSShachar Raindel if (!new_page) 22642f38ab2cSShachar Raindel goto oom; 22652f38ab2cSShachar Raindel } else { 2266bae473a4SKirill A. Shutemov new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, 226782b0f8c3SJan Kara vmf->address); 22682f38ab2cSShachar Raindel if (!new_page) 22692f38ab2cSShachar Raindel goto oom; 227082b0f8c3SJan Kara cow_user_page(new_page, old_page, vmf->address, vma); 22712f38ab2cSShachar Raindel } 22722f38ab2cSShachar Raindel 22732cf85583STejun Heo if (mem_cgroup_try_charge_delay(new_page, mm, GFP_KERNEL, &memcg, false)) 22742f38ab2cSShachar Raindel goto oom_free_new; 22752f38ab2cSShachar Raindel 2276eb3c24f3SMel Gorman __SetPageUptodate(new_page); 2277eb3c24f3SMel Gorman 22782f38ab2cSShachar Raindel mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); 22792f38ab2cSShachar Raindel 22802f38ab2cSShachar Raindel /* 22812f38ab2cSShachar Raindel * Re-check the pte - we dropped the lock 22822f38ab2cSShachar Raindel */ 228382b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(mm, vmf->pmd, vmf->address, &vmf->ptl); 22842994302bSJan Kara if (likely(pte_same(*vmf->pte, vmf->orig_pte))) { 22852f38ab2cSShachar Raindel if (old_page) { 22862f38ab2cSShachar Raindel if (!PageAnon(old_page)) { 2287eca56ff9SJerome Marchand dec_mm_counter_fast(mm, 2288eca56ff9SJerome Marchand mm_counter_file(old_page)); 22892f38ab2cSShachar Raindel inc_mm_counter_fast(mm, MM_ANONPAGES); 22902f38ab2cSShachar Raindel } 22912f38ab2cSShachar Raindel } else { 22922f38ab2cSShachar Raindel inc_mm_counter_fast(mm, MM_ANONPAGES); 22932f38ab2cSShachar Raindel } 22942994302bSJan Kara flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); 22952f38ab2cSShachar Raindel entry = mk_pte(new_page, vma->vm_page_prot); 22962f38ab2cSShachar Raindel entry = maybe_mkwrite(pte_mkdirty(entry), vma); 22972f38ab2cSShachar Raindel /* 22982f38ab2cSShachar Raindel * Clear the pte entry and flush it first, before updating the 22992f38ab2cSShachar Raindel * pte with the new entry. This will avoid a race condition 23002f38ab2cSShachar Raindel * seen in the presence of one thread doing SMC and another 23012f38ab2cSShachar Raindel * thread doing COW. 23022f38ab2cSShachar Raindel */ 230382b0f8c3SJan Kara ptep_clear_flush_notify(vma, vmf->address, vmf->pte); 230482b0f8c3SJan Kara page_add_new_anon_rmap(new_page, vma, vmf->address, false); 2305f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(new_page, memcg, false, false); 23062f38ab2cSShachar Raindel lru_cache_add_active_or_unevictable(new_page, vma); 23072f38ab2cSShachar Raindel /* 23082f38ab2cSShachar Raindel * We call the notify macro here because, when using secondary 23092f38ab2cSShachar Raindel * mmu page tables (such as kvm shadow page tables), we want the 23102f38ab2cSShachar Raindel * new page to be mapped directly into the secondary page table. 23112f38ab2cSShachar Raindel */ 231282b0f8c3SJan Kara set_pte_at_notify(mm, vmf->address, vmf->pte, entry); 231382b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 23142f38ab2cSShachar Raindel if (old_page) { 23152f38ab2cSShachar Raindel /* 23162f38ab2cSShachar Raindel * Only after switching the pte to the new page may 23172f38ab2cSShachar Raindel * we remove the mapcount here. Otherwise another 23182f38ab2cSShachar Raindel * process may come and find the rmap count decremented 23192f38ab2cSShachar Raindel * before the pte is switched to the new page, and 23202f38ab2cSShachar Raindel * "reuse" the old page writing into it while our pte 23212f38ab2cSShachar Raindel * here still points into it and can be read by other 23222f38ab2cSShachar Raindel * threads. 23232f38ab2cSShachar Raindel * 23242f38ab2cSShachar Raindel * The critical issue is to order this 23252f38ab2cSShachar Raindel * page_remove_rmap with the ptp_clear_flush above. 23262f38ab2cSShachar Raindel * Those stores are ordered by (if nothing else,) 23272f38ab2cSShachar Raindel * the barrier present in the atomic_add_negative 23282f38ab2cSShachar Raindel * in page_remove_rmap. 23292f38ab2cSShachar Raindel * 23302f38ab2cSShachar Raindel * Then the TLB flush in ptep_clear_flush ensures that 23312f38ab2cSShachar Raindel * no process can access the old page before the 23322f38ab2cSShachar Raindel * decremented mapcount is visible. And the old page 23332f38ab2cSShachar Raindel * cannot be reused until after the decremented 23342f38ab2cSShachar Raindel * mapcount is visible. So transitively, TLBs to 23352f38ab2cSShachar Raindel * old page will be flushed before it can be reused. 23362f38ab2cSShachar Raindel */ 2337d281ee61SKirill A. Shutemov page_remove_rmap(old_page, false); 23382f38ab2cSShachar Raindel } 23392f38ab2cSShachar Raindel 23402f38ab2cSShachar Raindel /* Free the old page.. */ 23412f38ab2cSShachar Raindel new_page = old_page; 23422f38ab2cSShachar Raindel page_copied = 1; 23432f38ab2cSShachar Raindel } else { 2344f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(new_page, memcg, false); 23452f38ab2cSShachar Raindel } 23462f38ab2cSShachar Raindel 23472f38ab2cSShachar Raindel if (new_page) 234809cbfeafSKirill A. Shutemov put_page(new_page); 23492f38ab2cSShachar Raindel 235082b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 23514645b9feSJérôme Glisse /* 23524645b9feSJérôme Glisse * No need to double call mmu_notifier->invalidate_range() callback as 23534645b9feSJérôme Glisse * the above ptep_clear_flush_notify() did already call it. 23544645b9feSJérôme Glisse */ 23554645b9feSJérôme Glisse mmu_notifier_invalidate_range_only_end(mm, mmun_start, mmun_end); 23562f38ab2cSShachar Raindel if (old_page) { 23572f38ab2cSShachar Raindel /* 23582f38ab2cSShachar Raindel * Don't let another task, with possibly unlocked vma, 23592f38ab2cSShachar Raindel * keep the mlocked page. 23602f38ab2cSShachar Raindel */ 23612f38ab2cSShachar Raindel if (page_copied && (vma->vm_flags & VM_LOCKED)) { 23622f38ab2cSShachar Raindel lock_page(old_page); /* LRU manipulation */ 2363e90309c9SKirill A. Shutemov if (PageMlocked(old_page)) 23642f38ab2cSShachar Raindel munlock_vma_page(old_page); 23652f38ab2cSShachar Raindel unlock_page(old_page); 23662f38ab2cSShachar Raindel } 236709cbfeafSKirill A. Shutemov put_page(old_page); 23682f38ab2cSShachar Raindel } 23692f38ab2cSShachar Raindel return page_copied ? VM_FAULT_WRITE : 0; 23702f38ab2cSShachar Raindel oom_free_new: 237109cbfeafSKirill A. Shutemov put_page(new_page); 23722f38ab2cSShachar Raindel oom: 23732f38ab2cSShachar Raindel if (old_page) 237409cbfeafSKirill A. Shutemov put_page(old_page); 23752f38ab2cSShachar Raindel return VM_FAULT_OOM; 23762f38ab2cSShachar Raindel } 23772f38ab2cSShachar Raindel 237866a6197cSJan Kara /** 237966a6197cSJan Kara * finish_mkwrite_fault - finish page fault for a shared mapping, making PTE 238066a6197cSJan Kara * writeable once the page is prepared 238166a6197cSJan Kara * 238266a6197cSJan Kara * @vmf: structure describing the fault 238366a6197cSJan Kara * 238466a6197cSJan Kara * This function handles all that is needed to finish a write page fault in a 238566a6197cSJan Kara * shared mapping due to PTE being read-only once the mapped page is prepared. 238666a6197cSJan Kara * It handles locking of PTE and modifying it. The function returns 238766a6197cSJan Kara * VM_FAULT_WRITE on success, 0 when PTE got changed before we acquired PTE 238866a6197cSJan Kara * lock. 238966a6197cSJan Kara * 239066a6197cSJan Kara * The function expects the page to be locked or other protection against 239166a6197cSJan Kara * concurrent faults / writeback (such as DAX radix tree locks). 239266a6197cSJan Kara */ 23932b740303SSouptick Joarder vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf) 239466a6197cSJan Kara { 239566a6197cSJan Kara WARN_ON_ONCE(!(vmf->vma->vm_flags & VM_SHARED)); 239666a6197cSJan Kara vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, vmf->address, 239766a6197cSJan Kara &vmf->ptl); 239866a6197cSJan Kara /* 239966a6197cSJan Kara * We might have raced with another page fault while we released the 240066a6197cSJan Kara * pte_offset_map_lock. 240166a6197cSJan Kara */ 240266a6197cSJan Kara if (!pte_same(*vmf->pte, vmf->orig_pte)) { 240366a6197cSJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2404a19e2553SJan Kara return VM_FAULT_NOPAGE; 240566a6197cSJan Kara } 240666a6197cSJan Kara wp_page_reuse(vmf); 2407a19e2553SJan Kara return 0; 240866a6197cSJan Kara } 240966a6197cSJan Kara 2410dd906184SBoaz Harrosh /* 2411dd906184SBoaz Harrosh * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED 2412dd906184SBoaz Harrosh * mapping 2413dd906184SBoaz Harrosh */ 24142b740303SSouptick Joarder static vm_fault_t wp_pfn_shared(struct vm_fault *vmf) 2415dd906184SBoaz Harrosh { 241682b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 2417bae473a4SKirill A. Shutemov 2418dd906184SBoaz Harrosh if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) { 24192b740303SSouptick Joarder vm_fault_t ret; 2420dd906184SBoaz Harrosh 242182b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2422fe82221fSJan Kara vmf->flags |= FAULT_FLAG_MKWRITE; 242311bac800SDave Jiang ret = vma->vm_ops->pfn_mkwrite(vmf); 24242f89dc12SJan Kara if (ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)) 2425dd906184SBoaz Harrosh return ret; 242666a6197cSJan Kara return finish_mkwrite_fault(vmf); 2427dd906184SBoaz Harrosh } 2428997dd98dSJan Kara wp_page_reuse(vmf); 2429997dd98dSJan Kara return VM_FAULT_WRITE; 2430dd906184SBoaz Harrosh } 2431dd906184SBoaz Harrosh 24322b740303SSouptick Joarder static vm_fault_t wp_page_shared(struct vm_fault *vmf) 243382b0f8c3SJan Kara __releases(vmf->ptl) 243493e478d4SShachar Raindel { 243582b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 243693e478d4SShachar Raindel 2437a41b70d6SJan Kara get_page(vmf->page); 243893e478d4SShachar Raindel 243993e478d4SShachar Raindel if (vma->vm_ops && vma->vm_ops->page_mkwrite) { 24402b740303SSouptick Joarder vm_fault_t tmp; 244193e478d4SShachar Raindel 244282b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 244338b8cb7fSJan Kara tmp = do_page_mkwrite(vmf); 244493e478d4SShachar Raindel if (unlikely(!tmp || (tmp & 244593e478d4SShachar Raindel (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { 2446a41b70d6SJan Kara put_page(vmf->page); 244793e478d4SShachar Raindel return tmp; 244893e478d4SShachar Raindel } 244966a6197cSJan Kara tmp = finish_mkwrite_fault(vmf); 2450a19e2553SJan Kara if (unlikely(tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) { 2451a41b70d6SJan Kara unlock_page(vmf->page); 2452a41b70d6SJan Kara put_page(vmf->page); 245366a6197cSJan Kara return tmp; 245493e478d4SShachar Raindel } 245566a6197cSJan Kara } else { 2456997dd98dSJan Kara wp_page_reuse(vmf); 2457997dd98dSJan Kara lock_page(vmf->page); 245893e478d4SShachar Raindel } 2459997dd98dSJan Kara fault_dirty_shared_page(vma, vmf->page); 2460997dd98dSJan Kara put_page(vmf->page); 246193e478d4SShachar Raindel 2462997dd98dSJan Kara return VM_FAULT_WRITE; 246393e478d4SShachar Raindel } 246493e478d4SShachar Raindel 24652f38ab2cSShachar Raindel /* 24661da177e4SLinus Torvalds * This routine handles present pages, when users try to write 24671da177e4SLinus Torvalds * to a shared page. It is done by copying the page to a new address 24681da177e4SLinus Torvalds * and decrementing the shared-page counter for the old page. 24691da177e4SLinus Torvalds * 24701da177e4SLinus Torvalds * Note that this routine assumes that the protection checks have been 24711da177e4SLinus Torvalds * done by the caller (the low-level page fault routine in most cases). 24721da177e4SLinus Torvalds * Thus we can safely just mark it writable once we've done any necessary 24731da177e4SLinus Torvalds * COW. 24741da177e4SLinus Torvalds * 24751da177e4SLinus Torvalds * We also mark the page dirty at this point even though the page will 24761da177e4SLinus Torvalds * change only once the write actually happens. This avoids a few races, 24771da177e4SLinus Torvalds * and potentially makes it more efficient. 24781da177e4SLinus Torvalds * 24798f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 24808f4e2101SHugh Dickins * but allow concurrent faults), with pte both mapped and locked. 24818f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 24821da177e4SLinus Torvalds */ 24832b740303SSouptick Joarder static vm_fault_t do_wp_page(struct vm_fault *vmf) 248482b0f8c3SJan Kara __releases(vmf->ptl) 24851da177e4SLinus Torvalds { 248682b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 24871da177e4SLinus Torvalds 2488a41b70d6SJan Kara vmf->page = vm_normal_page(vma, vmf->address, vmf->orig_pte); 2489a41b70d6SJan Kara if (!vmf->page) { 2490251b97f5SPeter Zijlstra /* 249164e45507SPeter Feiner * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a 249264e45507SPeter Feiner * VM_PFNMAP VMA. 2493251b97f5SPeter Zijlstra * 2494251b97f5SPeter Zijlstra * We should not cow pages in a shared writeable mapping. 2495dd906184SBoaz Harrosh * Just mark the pages writable and/or call ops->pfn_mkwrite. 2496251b97f5SPeter Zijlstra */ 2497251b97f5SPeter Zijlstra if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 2498251b97f5SPeter Zijlstra (VM_WRITE|VM_SHARED)) 24992994302bSJan Kara return wp_pfn_shared(vmf); 25002f38ab2cSShachar Raindel 250182b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2502a41b70d6SJan Kara return wp_page_copy(vmf); 2503251b97f5SPeter Zijlstra } 25041da177e4SLinus Torvalds 2505d08b3851SPeter Zijlstra /* 2506ee6a6457SPeter Zijlstra * Take out anonymous pages first, anonymous shared vmas are 2507ee6a6457SPeter Zijlstra * not dirty accountable. 2508d08b3851SPeter Zijlstra */ 2509a41b70d6SJan Kara if (PageAnon(vmf->page) && !PageKsm(vmf->page)) { 2510ba3c4ce6SHuang Ying int total_map_swapcount; 2511a41b70d6SJan Kara if (!trylock_page(vmf->page)) { 2512a41b70d6SJan Kara get_page(vmf->page); 251382b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2514a41b70d6SJan Kara lock_page(vmf->page); 251582b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 251682b0f8c3SJan Kara vmf->address, &vmf->ptl); 25172994302bSJan Kara if (!pte_same(*vmf->pte, vmf->orig_pte)) { 2518a41b70d6SJan Kara unlock_page(vmf->page); 251982b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2520a41b70d6SJan Kara put_page(vmf->page); 252128766805SShachar Raindel return 0; 2522ab967d86SHugh Dickins } 2523a41b70d6SJan Kara put_page(vmf->page); 2524ab967d86SHugh Dickins } 2525ba3c4ce6SHuang Ying if (reuse_swap_page(vmf->page, &total_map_swapcount)) { 2526ba3c4ce6SHuang Ying if (total_map_swapcount == 1) { 2527c44b6743SRik van Riel /* 25286d0a07edSAndrea Arcangeli * The page is all ours. Move it to 25296d0a07edSAndrea Arcangeli * our anon_vma so the rmap code will 25306d0a07edSAndrea Arcangeli * not search our parent or siblings. 25316d0a07edSAndrea Arcangeli * Protected against the rmap code by 25326d0a07edSAndrea Arcangeli * the page lock. 2533c44b6743SRik van Riel */ 2534a41b70d6SJan Kara page_move_anon_rmap(vmf->page, vma); 25356d0a07edSAndrea Arcangeli } 2536a41b70d6SJan Kara unlock_page(vmf->page); 2537997dd98dSJan Kara wp_page_reuse(vmf); 2538997dd98dSJan Kara return VM_FAULT_WRITE; 2539b009c024SMichel Lespinasse } 2540a41b70d6SJan Kara unlock_page(vmf->page); 2541ee6a6457SPeter Zijlstra } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 2542d08b3851SPeter Zijlstra (VM_WRITE|VM_SHARED))) { 2543a41b70d6SJan Kara return wp_page_shared(vmf); 25441da177e4SLinus Torvalds } 25451da177e4SLinus Torvalds 25461da177e4SLinus Torvalds /* 25471da177e4SLinus Torvalds * Ok, we need to copy. Oh, well.. 25481da177e4SLinus Torvalds */ 2549a41b70d6SJan Kara get_page(vmf->page); 255028766805SShachar Raindel 255182b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2552a41b70d6SJan Kara return wp_page_copy(vmf); 25531da177e4SLinus Torvalds } 25541da177e4SLinus Torvalds 255597a89413SPeter Zijlstra static void unmap_mapping_range_vma(struct vm_area_struct *vma, 25561da177e4SLinus Torvalds unsigned long start_addr, unsigned long end_addr, 25571da177e4SLinus Torvalds struct zap_details *details) 25581da177e4SLinus Torvalds { 2559f5cc4eefSAl Viro zap_page_range_single(vma, start_addr, end_addr - start_addr, details); 25601da177e4SLinus Torvalds } 25611da177e4SLinus Torvalds 2562f808c13fSDavidlohr Bueso static inline void unmap_mapping_range_tree(struct rb_root_cached *root, 25631da177e4SLinus Torvalds struct zap_details *details) 25641da177e4SLinus Torvalds { 25651da177e4SLinus Torvalds struct vm_area_struct *vma; 25661da177e4SLinus Torvalds pgoff_t vba, vea, zba, zea; 25671da177e4SLinus Torvalds 25686b2dbba8SMichel Lespinasse vma_interval_tree_foreach(vma, root, 25691da177e4SLinus Torvalds details->first_index, details->last_index) { 25701da177e4SLinus Torvalds 25711da177e4SLinus Torvalds vba = vma->vm_pgoff; 2572d6e93217SLibin vea = vba + vma_pages(vma) - 1; 25731da177e4SLinus Torvalds zba = details->first_index; 25741da177e4SLinus Torvalds if (zba < vba) 25751da177e4SLinus Torvalds zba = vba; 25761da177e4SLinus Torvalds zea = details->last_index; 25771da177e4SLinus Torvalds if (zea > vea) 25781da177e4SLinus Torvalds zea = vea; 25791da177e4SLinus Torvalds 258097a89413SPeter Zijlstra unmap_mapping_range_vma(vma, 25811da177e4SLinus Torvalds ((zba - vba) << PAGE_SHIFT) + vma->vm_start, 25821da177e4SLinus Torvalds ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start, 258397a89413SPeter Zijlstra details); 25841da177e4SLinus Torvalds } 25851da177e4SLinus Torvalds } 25861da177e4SLinus Torvalds 25871da177e4SLinus Torvalds /** 2588977fbdcdSMatthew Wilcox * unmap_mapping_pages() - Unmap pages from processes. 2589977fbdcdSMatthew Wilcox * @mapping: The address space containing pages to be unmapped. 2590977fbdcdSMatthew Wilcox * @start: Index of first page to be unmapped. 2591977fbdcdSMatthew Wilcox * @nr: Number of pages to be unmapped. 0 to unmap to end of file. 2592977fbdcdSMatthew Wilcox * @even_cows: Whether to unmap even private COWed pages. 2593977fbdcdSMatthew Wilcox * 2594977fbdcdSMatthew Wilcox * Unmap the pages in this address space from any userspace process which 2595977fbdcdSMatthew Wilcox * has them mmaped. Generally, you want to remove COWed pages as well when 2596977fbdcdSMatthew Wilcox * a file is being truncated, but not when invalidating pages from the page 2597977fbdcdSMatthew Wilcox * cache. 2598977fbdcdSMatthew Wilcox */ 2599977fbdcdSMatthew Wilcox void unmap_mapping_pages(struct address_space *mapping, pgoff_t start, 2600977fbdcdSMatthew Wilcox pgoff_t nr, bool even_cows) 2601977fbdcdSMatthew Wilcox { 2602977fbdcdSMatthew Wilcox struct zap_details details = { }; 2603977fbdcdSMatthew Wilcox 2604977fbdcdSMatthew Wilcox details.check_mapping = even_cows ? NULL : mapping; 2605977fbdcdSMatthew Wilcox details.first_index = start; 2606977fbdcdSMatthew Wilcox details.last_index = start + nr - 1; 2607977fbdcdSMatthew Wilcox if (details.last_index < details.first_index) 2608977fbdcdSMatthew Wilcox details.last_index = ULONG_MAX; 2609977fbdcdSMatthew Wilcox 2610977fbdcdSMatthew Wilcox i_mmap_lock_write(mapping); 2611977fbdcdSMatthew Wilcox if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root))) 2612977fbdcdSMatthew Wilcox unmap_mapping_range_tree(&mapping->i_mmap, &details); 2613977fbdcdSMatthew Wilcox i_mmap_unlock_write(mapping); 2614977fbdcdSMatthew Wilcox } 2615977fbdcdSMatthew Wilcox 2616977fbdcdSMatthew Wilcox /** 26178a5f14a2SKirill A. Shutemov * unmap_mapping_range - unmap the portion of all mmaps in the specified 2618977fbdcdSMatthew Wilcox * address_space corresponding to the specified byte range in the underlying 26198a5f14a2SKirill A. Shutemov * file. 26208a5f14a2SKirill A. Shutemov * 26213d41088fSMartin Waitz * @mapping: the address space containing mmaps to be unmapped. 26221da177e4SLinus Torvalds * @holebegin: byte in first page to unmap, relative to the start of 26231da177e4SLinus Torvalds * the underlying file. This will be rounded down to a PAGE_SIZE 262425d9e2d1Snpiggin@suse.de * boundary. Note that this is different from truncate_pagecache(), which 26251da177e4SLinus Torvalds * must keep the partial page. In contrast, we must get rid of 26261da177e4SLinus Torvalds * partial pages. 26271da177e4SLinus Torvalds * @holelen: size of prospective hole in bytes. This will be rounded 26281da177e4SLinus Torvalds * up to a PAGE_SIZE boundary. A holelen of zero truncates to the 26291da177e4SLinus Torvalds * end of the file. 26301da177e4SLinus Torvalds * @even_cows: 1 when truncating a file, unmap even private COWed pages; 26311da177e4SLinus Torvalds * but 0 when invalidating pagecache, don't throw away private data. 26321da177e4SLinus Torvalds */ 26331da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping, 26341da177e4SLinus Torvalds loff_t const holebegin, loff_t const holelen, int even_cows) 26351da177e4SLinus Torvalds { 26361da177e4SLinus Torvalds pgoff_t hba = holebegin >> PAGE_SHIFT; 26371da177e4SLinus Torvalds pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 26381da177e4SLinus Torvalds 26391da177e4SLinus Torvalds /* Check for overflow. */ 26401da177e4SLinus Torvalds if (sizeof(holelen) > sizeof(hlen)) { 26411da177e4SLinus Torvalds long long holeend = 26421da177e4SLinus Torvalds (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 26431da177e4SLinus Torvalds if (holeend & ~(long long)ULONG_MAX) 26441da177e4SLinus Torvalds hlen = ULONG_MAX - hba + 1; 26451da177e4SLinus Torvalds } 26461da177e4SLinus Torvalds 2647977fbdcdSMatthew Wilcox unmap_mapping_pages(mapping, hba, hlen, even_cows); 26481da177e4SLinus Torvalds } 26491da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range); 26501da177e4SLinus Torvalds 26511da177e4SLinus Torvalds /* 26528f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 26538f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 26549a95f3cfSPaul Cassella * We return with pte unmapped and unlocked. 26559a95f3cfSPaul Cassella * 26569a95f3cfSPaul Cassella * We return with the mmap_sem locked or unlocked in the same cases 26579a95f3cfSPaul Cassella * as does filemap_fault(). 26581da177e4SLinus Torvalds */ 26592b740303SSouptick Joarder vm_fault_t do_swap_page(struct vm_fault *vmf) 26601da177e4SLinus Torvalds { 266182b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 2662eaf649ebSMinchan Kim struct page *page = NULL, *swapcache; 266300501b53SJohannes Weiner struct mem_cgroup *memcg; 266465500d23SHugh Dickins swp_entry_t entry; 26651da177e4SLinus Torvalds pte_t pte; 2666d065bd81SMichel Lespinasse int locked; 2667ad8c2ee8SRik van Riel int exclusive = 0; 26682b740303SSouptick Joarder vm_fault_t ret = 0; 26691da177e4SLinus Torvalds 2670eaf649ebSMinchan Kim if (!pte_unmap_same(vma->vm_mm, vmf->pmd, vmf->pte, vmf->orig_pte)) 26718f4e2101SHugh Dickins goto out; 267265500d23SHugh Dickins 26732994302bSJan Kara entry = pte_to_swp_entry(vmf->orig_pte); 2674d1737fdbSAndi Kleen if (unlikely(non_swap_entry(entry))) { 26750697212aSChristoph Lameter if (is_migration_entry(entry)) { 267682b0f8c3SJan Kara migration_entry_wait(vma->vm_mm, vmf->pmd, 267782b0f8c3SJan Kara vmf->address); 26785042db43SJérôme Glisse } else if (is_device_private_entry(entry)) { 26795042db43SJérôme Glisse /* 26805042db43SJérôme Glisse * For un-addressable device memory we call the pgmap 26815042db43SJérôme Glisse * fault handler callback. The callback must migrate 26825042db43SJérôme Glisse * the page back to some CPU accessible page. 26835042db43SJérôme Glisse */ 26845042db43SJérôme Glisse ret = device_private_entry_fault(vma, vmf->address, entry, 26855042db43SJérôme Glisse vmf->flags, vmf->pmd); 2686d1737fdbSAndi Kleen } else if (is_hwpoison_entry(entry)) { 2687d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 2688d1737fdbSAndi Kleen } else { 26892994302bSJan Kara print_bad_pte(vma, vmf->address, vmf->orig_pte, NULL); 2690d99be1a8SHugh Dickins ret = VM_FAULT_SIGBUS; 2691d1737fdbSAndi Kleen } 26920697212aSChristoph Lameter goto out; 26930697212aSChristoph Lameter } 26940bcac06fSMinchan Kim 26950bcac06fSMinchan Kim 26960ff92245SShailabh Nagar delayacct_set_flag(DELAYACCT_PF_SWAPIN); 2697eaf649ebSMinchan Kim page = lookup_swap_cache(entry, vma, vmf->address); 2698f8020772SMinchan Kim swapcache = page; 2699f8020772SMinchan Kim 27001da177e4SLinus Torvalds if (!page) { 27010bcac06fSMinchan Kim struct swap_info_struct *si = swp_swap_info(entry); 27020bcac06fSMinchan Kim 2703aa8d22a1SMinchan Kim if (si->flags & SWP_SYNCHRONOUS_IO && 2704aa8d22a1SMinchan Kim __swap_count(si, entry) == 1) { 27050bcac06fSMinchan Kim /* skip swapcache */ 2706e9e9b7ecSMinchan Kim page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, 2707e9e9b7ecSMinchan Kim vmf->address); 27080bcac06fSMinchan Kim if (page) { 27090bcac06fSMinchan Kim __SetPageLocked(page); 27100bcac06fSMinchan Kim __SetPageSwapBacked(page); 27110bcac06fSMinchan Kim set_page_private(page, entry.val); 27120bcac06fSMinchan Kim lru_cache_add_anon(page); 27130bcac06fSMinchan Kim swap_readpage(page, true); 27140bcac06fSMinchan Kim } 2715aa8d22a1SMinchan Kim } else { 2716e9e9b7ecSMinchan Kim page = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, 2717e9e9b7ecSMinchan Kim vmf); 2718aa8d22a1SMinchan Kim swapcache = page; 27190bcac06fSMinchan Kim } 27200bcac06fSMinchan Kim 2721ec560175SHuang Ying if (!page) { 27221da177e4SLinus Torvalds /* 27238f4e2101SHugh Dickins * Back out if somebody else faulted in this pte 27248f4e2101SHugh Dickins * while we released the pte lock. 27251da177e4SLinus Torvalds */ 272682b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 272782b0f8c3SJan Kara vmf->address, &vmf->ptl); 27282994302bSJan Kara if (likely(pte_same(*vmf->pte, vmf->orig_pte))) 27291da177e4SLinus Torvalds ret = VM_FAULT_OOM; 27300ff92245SShailabh Nagar delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 273165500d23SHugh Dickins goto unlock; 27321da177e4SLinus Torvalds } 27331da177e4SLinus Torvalds 27341da177e4SLinus Torvalds /* Had to read the page from swap area: Major fault */ 27351da177e4SLinus Torvalds ret = VM_FAULT_MAJOR; 2736f8891e5eSChristoph Lameter count_vm_event(PGMAJFAULT); 27372262185cSRoman Gushchin count_memcg_event_mm(vma->vm_mm, PGMAJFAULT); 2738d1737fdbSAndi Kleen } else if (PageHWPoison(page)) { 273971f72525SWu Fengguang /* 274071f72525SWu Fengguang * hwpoisoned dirty swapcache pages are kept for killing 274171f72525SWu Fengguang * owner processes (which may be unknown at hwpoison time) 274271f72525SWu Fengguang */ 2743d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 2744d1737fdbSAndi Kleen delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 27454779cb31SAndi Kleen goto out_release; 27461da177e4SLinus Torvalds } 27471da177e4SLinus Torvalds 274882b0f8c3SJan Kara locked = lock_page_or_retry(page, vma->vm_mm, vmf->flags); 2749e709ffd6SRik van Riel 275020a1022dSBalbir Singh delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 2751d065bd81SMichel Lespinasse if (!locked) { 2752d065bd81SMichel Lespinasse ret |= VM_FAULT_RETRY; 2753d065bd81SMichel Lespinasse goto out_release; 2754d065bd81SMichel Lespinasse } 27551da177e4SLinus Torvalds 27564969c119SAndrea Arcangeli /* 275731c4a3d3SHugh Dickins * Make sure try_to_free_swap or reuse_swap_page or swapoff did not 275831c4a3d3SHugh Dickins * release the swapcache from under us. The page pin, and pte_same 275931c4a3d3SHugh Dickins * test below, are not enough to exclude that. Even if it is still 276031c4a3d3SHugh Dickins * swapcache, we need to check that the page's swap has not changed. 27614969c119SAndrea Arcangeli */ 27620bcac06fSMinchan Kim if (unlikely((!PageSwapCache(page) || 27630bcac06fSMinchan Kim page_private(page) != entry.val)) && swapcache) 27644969c119SAndrea Arcangeli goto out_page; 27654969c119SAndrea Arcangeli 276682b0f8c3SJan Kara page = ksm_might_need_to_copy(page, vma, vmf->address); 27674969c119SAndrea Arcangeli if (unlikely(!page)) { 27685ad64688SHugh Dickins ret = VM_FAULT_OOM; 27694969c119SAndrea Arcangeli page = swapcache; 27704969c119SAndrea Arcangeli goto out_page; 27714969c119SAndrea Arcangeli } 27725ad64688SHugh Dickins 27732cf85583STejun Heo if (mem_cgroup_try_charge_delay(page, vma->vm_mm, GFP_KERNEL, 2774bae473a4SKirill A. Shutemov &memcg, false)) { 2775073e587eSKAMEZAWA Hiroyuki ret = VM_FAULT_OOM; 2776bc43f75cSJohannes Weiner goto out_page; 2777073e587eSKAMEZAWA Hiroyuki } 2778073e587eSKAMEZAWA Hiroyuki 27791da177e4SLinus Torvalds /* 27808f4e2101SHugh Dickins * Back out if somebody else already faulted in this pte. 27811da177e4SLinus Torvalds */ 278282b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 278382b0f8c3SJan Kara &vmf->ptl); 27842994302bSJan Kara if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) 2785b8107480SKirill Korotaev goto out_nomap; 2786b8107480SKirill Korotaev 2787b8107480SKirill Korotaev if (unlikely(!PageUptodate(page))) { 2788b8107480SKirill Korotaev ret = VM_FAULT_SIGBUS; 2789b8107480SKirill Korotaev goto out_nomap; 27901da177e4SLinus Torvalds } 27911da177e4SLinus Torvalds 27928c7c6e34SKAMEZAWA Hiroyuki /* 27938c7c6e34SKAMEZAWA Hiroyuki * The page isn't present yet, go ahead with the fault. 27948c7c6e34SKAMEZAWA Hiroyuki * 27958c7c6e34SKAMEZAWA Hiroyuki * Be careful about the sequence of operations here. 27968c7c6e34SKAMEZAWA Hiroyuki * To get its accounting right, reuse_swap_page() must be called 27978c7c6e34SKAMEZAWA Hiroyuki * while the page is counted on swap but not yet in mapcount i.e. 27988c7c6e34SKAMEZAWA Hiroyuki * before page_add_anon_rmap() and swap_free(); try_to_free_swap() 27998c7c6e34SKAMEZAWA Hiroyuki * must be called after the swap_free(), or it will never succeed. 28008c7c6e34SKAMEZAWA Hiroyuki */ 28011da177e4SLinus Torvalds 2802bae473a4SKirill A. Shutemov inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); 2803bae473a4SKirill A. Shutemov dec_mm_counter_fast(vma->vm_mm, MM_SWAPENTS); 28041da177e4SLinus Torvalds pte = mk_pte(page, vma->vm_page_prot); 280582b0f8c3SJan Kara if ((vmf->flags & FAULT_FLAG_WRITE) && reuse_swap_page(page, NULL)) { 28061da177e4SLinus Torvalds pte = maybe_mkwrite(pte_mkdirty(pte), vma); 280782b0f8c3SJan Kara vmf->flags &= ~FAULT_FLAG_WRITE; 28089a5b489bSAndrea Arcangeli ret |= VM_FAULT_WRITE; 2809d281ee61SKirill A. Shutemov exclusive = RMAP_EXCLUSIVE; 28101da177e4SLinus Torvalds } 28111da177e4SLinus Torvalds flush_icache_page(vma, page); 28122994302bSJan Kara if (pte_swp_soft_dirty(vmf->orig_pte)) 2813179ef71cSCyrill Gorcunov pte = pte_mksoft_dirty(pte); 281482b0f8c3SJan Kara set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte); 2815ca827d55SKhalid Aziz arch_do_swap_page(vma->vm_mm, vma, vmf->address, pte, vmf->orig_pte); 28162994302bSJan Kara vmf->orig_pte = pte; 28170bcac06fSMinchan Kim 28180bcac06fSMinchan Kim /* ksm created a completely new copy */ 28190bcac06fSMinchan Kim if (unlikely(page != swapcache && swapcache)) { 282082b0f8c3SJan Kara page_add_new_anon_rmap(page, vma, vmf->address, false); 2821f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, false); 282200501b53SJohannes Weiner lru_cache_add_active_or_unevictable(page, vma); 28230bcac06fSMinchan Kim } else { 28240bcac06fSMinchan Kim do_page_add_anon_rmap(page, vma, vmf->address, exclusive); 28250bcac06fSMinchan Kim mem_cgroup_commit_charge(page, memcg, true, false); 28260bcac06fSMinchan Kim activate_page(page); 282700501b53SJohannes Weiner } 28281da177e4SLinus Torvalds 2829c475a8abSHugh Dickins swap_free(entry); 28305ccc5abaSVladimir Davydov if (mem_cgroup_swap_full(page) || 28315ccc5abaSVladimir Davydov (vma->vm_flags & VM_LOCKED) || PageMlocked(page)) 2832a2c43eedSHugh Dickins try_to_free_swap(page); 2833c475a8abSHugh Dickins unlock_page(page); 28340bcac06fSMinchan Kim if (page != swapcache && swapcache) { 28354969c119SAndrea Arcangeli /* 28364969c119SAndrea Arcangeli * Hold the lock to avoid the swap entry to be reused 28374969c119SAndrea Arcangeli * until we take the PT lock for the pte_same() check 28384969c119SAndrea Arcangeli * (to avoid false positives from pte_same). For 28394969c119SAndrea Arcangeli * further safety release the lock after the swap_free 28404969c119SAndrea Arcangeli * so that the swap count won't change under a 28414969c119SAndrea Arcangeli * parallel locked swapcache. 28424969c119SAndrea Arcangeli */ 28434969c119SAndrea Arcangeli unlock_page(swapcache); 284409cbfeafSKirill A. Shutemov put_page(swapcache); 28454969c119SAndrea Arcangeli } 2846c475a8abSHugh Dickins 284782b0f8c3SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) { 28482994302bSJan Kara ret |= do_wp_page(vmf); 284961469f1dSHugh Dickins if (ret & VM_FAULT_ERROR) 285061469f1dSHugh Dickins ret &= VM_FAULT_ERROR; 28511da177e4SLinus Torvalds goto out; 28521da177e4SLinus Torvalds } 28531da177e4SLinus Torvalds 28541da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 285582b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 285665500d23SHugh Dickins unlock: 285782b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 28581da177e4SLinus Torvalds out: 28591da177e4SLinus Torvalds return ret; 2860b8107480SKirill Korotaev out_nomap: 2861f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 286282b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2863bc43f75cSJohannes Weiner out_page: 2864b8107480SKirill Korotaev unlock_page(page); 28654779cb31SAndi Kleen out_release: 286609cbfeafSKirill A. Shutemov put_page(page); 28670bcac06fSMinchan Kim if (page != swapcache && swapcache) { 28684969c119SAndrea Arcangeli unlock_page(swapcache); 286909cbfeafSKirill A. Shutemov put_page(swapcache); 28704969c119SAndrea Arcangeli } 287165500d23SHugh Dickins return ret; 28721da177e4SLinus Torvalds } 28731da177e4SLinus Torvalds 28741da177e4SLinus Torvalds /* 28758f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 28768f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 28778f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 28781da177e4SLinus Torvalds */ 28792b740303SSouptick Joarder static vm_fault_t do_anonymous_page(struct vm_fault *vmf) 28801da177e4SLinus Torvalds { 288182b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 288200501b53SJohannes Weiner struct mem_cgroup *memcg; 28838f4e2101SHugh Dickins struct page *page; 28842b740303SSouptick Joarder vm_fault_t ret = 0; 28851da177e4SLinus Torvalds pte_t entry; 28861da177e4SLinus Torvalds 28876b7339f4SKirill A. Shutemov /* File mapping without ->vm_ops ? */ 28886b7339f4SKirill A. Shutemov if (vma->vm_flags & VM_SHARED) 28896b7339f4SKirill A. Shutemov return VM_FAULT_SIGBUS; 28906b7339f4SKirill A. Shutemov 28917267ec00SKirill A. Shutemov /* 28927267ec00SKirill A. Shutemov * Use pte_alloc() instead of pte_alloc_map(). We can't run 28937267ec00SKirill A. Shutemov * pte_offset_map() on pmds where a huge pmd might be created 28947267ec00SKirill A. Shutemov * from a different thread. 28957267ec00SKirill A. Shutemov * 28967267ec00SKirill A. Shutemov * pte_alloc_map() is safe to use under down_write(mmap_sem) or when 28977267ec00SKirill A. Shutemov * parallel threads are excluded by other means. 28987267ec00SKirill A. Shutemov * 28997267ec00SKirill A. Shutemov * Here we only have down_read(mmap_sem). 29007267ec00SKirill A. Shutemov */ 290182b0f8c3SJan Kara if (pte_alloc(vma->vm_mm, vmf->pmd, vmf->address)) 29027267ec00SKirill A. Shutemov return VM_FAULT_OOM; 29037267ec00SKirill A. Shutemov 29047267ec00SKirill A. Shutemov /* See the comment in pte_alloc_one_map() */ 290582b0f8c3SJan Kara if (unlikely(pmd_trans_unstable(vmf->pmd))) 29067267ec00SKirill A. Shutemov return 0; 29077267ec00SKirill A. Shutemov 290811ac5524SLinus Torvalds /* Use the zero-page for reads */ 290982b0f8c3SJan Kara if (!(vmf->flags & FAULT_FLAG_WRITE) && 2910bae473a4SKirill A. Shutemov !mm_forbids_zeropage(vma->vm_mm)) { 291182b0f8c3SJan Kara entry = pte_mkspecial(pfn_pte(my_zero_pfn(vmf->address), 291262eede62SHugh Dickins vma->vm_page_prot)); 291382b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 291482b0f8c3SJan Kara vmf->address, &vmf->ptl); 291582b0f8c3SJan Kara if (!pte_none(*vmf->pte)) 2916a13ea5b7SHugh Dickins goto unlock; 29176b31d595SMichal Hocko ret = check_stable_address_space(vma->vm_mm); 29186b31d595SMichal Hocko if (ret) 29196b31d595SMichal Hocko goto unlock; 29206b251fc9SAndrea Arcangeli /* Deliver the page fault to userland, check inside PT lock */ 29216b251fc9SAndrea Arcangeli if (userfaultfd_missing(vma)) { 292282b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 292382b0f8c3SJan Kara return handle_userfault(vmf, VM_UFFD_MISSING); 29246b251fc9SAndrea Arcangeli } 2925a13ea5b7SHugh Dickins goto setpte; 2926a13ea5b7SHugh Dickins } 2927a13ea5b7SHugh Dickins 29281da177e4SLinus Torvalds /* Allocate our own private page. */ 29291da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 293065500d23SHugh Dickins goto oom; 293182b0f8c3SJan Kara page = alloc_zeroed_user_highpage_movable(vma, vmf->address); 29321da177e4SLinus Torvalds if (!page) 293365500d23SHugh Dickins goto oom; 2934eb3c24f3SMel Gorman 29352cf85583STejun Heo if (mem_cgroup_try_charge_delay(page, vma->vm_mm, GFP_KERNEL, &memcg, 29362cf85583STejun Heo false)) 2937eb3c24f3SMel Gorman goto oom_free_page; 2938eb3c24f3SMel Gorman 293952f37629SMinchan Kim /* 294052f37629SMinchan Kim * The memory barrier inside __SetPageUptodate makes sure that 294152f37629SMinchan Kim * preceeding stores to the page contents become visible before 294252f37629SMinchan Kim * the set_pte_at() write. 294352f37629SMinchan Kim */ 29440ed361deSNick Piggin __SetPageUptodate(page); 29451da177e4SLinus Torvalds 294665500d23SHugh Dickins entry = mk_pte(page, vma->vm_page_prot); 29471ac0cb5dSHugh Dickins if (vma->vm_flags & VM_WRITE) 29481ac0cb5dSHugh Dickins entry = pte_mkwrite(pte_mkdirty(entry)); 29498f4e2101SHugh Dickins 295082b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 295182b0f8c3SJan Kara &vmf->ptl); 295282b0f8c3SJan Kara if (!pte_none(*vmf->pte)) 29538f4e2101SHugh Dickins goto release; 29549ba69294SHugh Dickins 29556b31d595SMichal Hocko ret = check_stable_address_space(vma->vm_mm); 29566b31d595SMichal Hocko if (ret) 29576b31d595SMichal Hocko goto release; 29586b31d595SMichal Hocko 29596b251fc9SAndrea Arcangeli /* Deliver the page fault to userland, check inside PT lock */ 29606b251fc9SAndrea Arcangeli if (userfaultfd_missing(vma)) { 296182b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2962f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 296309cbfeafSKirill A. Shutemov put_page(page); 296482b0f8c3SJan Kara return handle_userfault(vmf, VM_UFFD_MISSING); 29656b251fc9SAndrea Arcangeli } 29666b251fc9SAndrea Arcangeli 2967bae473a4SKirill A. Shutemov inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); 296882b0f8c3SJan Kara page_add_new_anon_rmap(page, vma, vmf->address, false); 2969f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, false); 297000501b53SJohannes Weiner lru_cache_add_active_or_unevictable(page, vma); 2971a13ea5b7SHugh Dickins setpte: 297282b0f8c3SJan Kara set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry); 29731da177e4SLinus Torvalds 29741da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 297582b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 297665500d23SHugh Dickins unlock: 297782b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 29786b31d595SMichal Hocko return ret; 29798f4e2101SHugh Dickins release: 2980f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 298109cbfeafSKirill A. Shutemov put_page(page); 29828f4e2101SHugh Dickins goto unlock; 29838a9f3ccdSBalbir Singh oom_free_page: 298409cbfeafSKirill A. Shutemov put_page(page); 298565500d23SHugh Dickins oom: 29861da177e4SLinus Torvalds return VM_FAULT_OOM; 29871da177e4SLinus Torvalds } 29881da177e4SLinus Torvalds 29899a95f3cfSPaul Cassella /* 29909a95f3cfSPaul Cassella * The mmap_sem must have been held on entry, and may have been 29919a95f3cfSPaul Cassella * released depending on flags and vma->vm_ops->fault() return value. 29929a95f3cfSPaul Cassella * See filemap_fault() and __lock_page_retry(). 29939a95f3cfSPaul Cassella */ 29942b740303SSouptick Joarder static vm_fault_t __do_fault(struct vm_fault *vmf) 29957eae74afSKirill A. Shutemov { 299682b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 29972b740303SSouptick Joarder vm_fault_t ret; 29987eae74afSKirill A. Shutemov 299911bac800SDave Jiang ret = vma->vm_ops->fault(vmf); 30003917048dSJan Kara if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY | 3001b1aa812bSJan Kara VM_FAULT_DONE_COW))) 30027eae74afSKirill A. Shutemov return ret; 30037eae74afSKirill A. Shutemov 3004667240e0SJan Kara if (unlikely(PageHWPoison(vmf->page))) { 30057eae74afSKirill A. Shutemov if (ret & VM_FAULT_LOCKED) 3006667240e0SJan Kara unlock_page(vmf->page); 3007667240e0SJan Kara put_page(vmf->page); 3008936ca80dSJan Kara vmf->page = NULL; 30097eae74afSKirill A. Shutemov return VM_FAULT_HWPOISON; 30107eae74afSKirill A. Shutemov } 30117eae74afSKirill A. Shutemov 30127eae74afSKirill A. Shutemov if (unlikely(!(ret & VM_FAULT_LOCKED))) 3013667240e0SJan Kara lock_page(vmf->page); 30147eae74afSKirill A. Shutemov else 3015667240e0SJan Kara VM_BUG_ON_PAGE(!PageLocked(vmf->page), vmf->page); 30167eae74afSKirill A. Shutemov 30177eae74afSKirill A. Shutemov return ret; 30187eae74afSKirill A. Shutemov } 30197eae74afSKirill A. Shutemov 3020d0f0931dSRoss Zwisler /* 3021d0f0931dSRoss Zwisler * The ordering of these checks is important for pmds with _PAGE_DEVMAP set. 3022d0f0931dSRoss Zwisler * If we check pmd_trans_unstable() first we will trip the bad_pmd() check 3023d0f0931dSRoss Zwisler * inside of pmd_none_or_trans_huge_or_clear_bad(). This will end up correctly 3024d0f0931dSRoss Zwisler * returning 1 but not before it spams dmesg with the pmd_clear_bad() output. 3025d0f0931dSRoss Zwisler */ 3026d0f0931dSRoss Zwisler static int pmd_devmap_trans_unstable(pmd_t *pmd) 3027d0f0931dSRoss Zwisler { 3028d0f0931dSRoss Zwisler return pmd_devmap(*pmd) || pmd_trans_unstable(pmd); 3029d0f0931dSRoss Zwisler } 3030d0f0931dSRoss Zwisler 30312b740303SSouptick Joarder static vm_fault_t pte_alloc_one_map(struct vm_fault *vmf) 30327267ec00SKirill A. Shutemov { 303382b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 30347267ec00SKirill A. Shutemov 303582b0f8c3SJan Kara if (!pmd_none(*vmf->pmd)) 30367267ec00SKirill A. Shutemov goto map_pte; 303782b0f8c3SJan Kara if (vmf->prealloc_pte) { 303882b0f8c3SJan Kara vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); 303982b0f8c3SJan Kara if (unlikely(!pmd_none(*vmf->pmd))) { 304082b0f8c3SJan Kara spin_unlock(vmf->ptl); 30417267ec00SKirill A. Shutemov goto map_pte; 30427267ec00SKirill A. Shutemov } 30437267ec00SKirill A. Shutemov 3044c4812909SKirill A. Shutemov mm_inc_nr_ptes(vma->vm_mm); 304582b0f8c3SJan Kara pmd_populate(vma->vm_mm, vmf->pmd, vmf->prealloc_pte); 304682b0f8c3SJan Kara spin_unlock(vmf->ptl); 30477f2b6ce8STobin C Harding vmf->prealloc_pte = NULL; 304882b0f8c3SJan Kara } else if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd, vmf->address))) { 30497267ec00SKirill A. Shutemov return VM_FAULT_OOM; 30507267ec00SKirill A. Shutemov } 30517267ec00SKirill A. Shutemov map_pte: 30527267ec00SKirill A. Shutemov /* 30537267ec00SKirill A. Shutemov * If a huge pmd materialized under us just retry later. Use 3054d0f0931dSRoss Zwisler * pmd_trans_unstable() via pmd_devmap_trans_unstable() instead of 3055d0f0931dSRoss Zwisler * pmd_trans_huge() to ensure the pmd didn't become pmd_trans_huge 3056d0f0931dSRoss Zwisler * under us and then back to pmd_none, as a result of MADV_DONTNEED 3057d0f0931dSRoss Zwisler * running immediately after a huge pmd fault in a different thread of 3058d0f0931dSRoss Zwisler * this mm, in turn leading to a misleading pmd_trans_huge() retval. 3059d0f0931dSRoss Zwisler * All we have to ensure is that it is a regular pmd that we can walk 3060d0f0931dSRoss Zwisler * with pte_offset_map() and we can do that through an atomic read in 3061d0f0931dSRoss Zwisler * C, which is what pmd_trans_unstable() provides. 30627267ec00SKirill A. Shutemov */ 3063d0f0931dSRoss Zwisler if (pmd_devmap_trans_unstable(vmf->pmd)) 30647267ec00SKirill A. Shutemov return VM_FAULT_NOPAGE; 30657267ec00SKirill A. Shutemov 3066d0f0931dSRoss Zwisler /* 3067d0f0931dSRoss Zwisler * At this point we know that our vmf->pmd points to a page of ptes 3068d0f0931dSRoss Zwisler * and it cannot become pmd_none(), pmd_devmap() or pmd_trans_huge() 3069d0f0931dSRoss Zwisler * for the duration of the fault. If a racing MADV_DONTNEED runs and 3070d0f0931dSRoss Zwisler * we zap the ptes pointed to by our vmf->pmd, the vmf->ptl will still 3071d0f0931dSRoss Zwisler * be valid and we will re-check to make sure the vmf->pte isn't 3072d0f0931dSRoss Zwisler * pte_none() under vmf->ptl protection when we return to 3073d0f0931dSRoss Zwisler * alloc_set_pte(). 3074d0f0931dSRoss Zwisler */ 307582b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 307682b0f8c3SJan Kara &vmf->ptl); 30777267ec00SKirill A. Shutemov return 0; 30787267ec00SKirill A. Shutemov } 30797267ec00SKirill A. Shutemov 3080e496cf3dSKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 308110102459SKirill A. Shutemov 308210102459SKirill A. Shutemov #define HPAGE_CACHE_INDEX_MASK (HPAGE_PMD_NR - 1) 308310102459SKirill A. Shutemov static inline bool transhuge_vma_suitable(struct vm_area_struct *vma, 308410102459SKirill A. Shutemov unsigned long haddr) 308510102459SKirill A. Shutemov { 308610102459SKirill A. Shutemov if (((vma->vm_start >> PAGE_SHIFT) & HPAGE_CACHE_INDEX_MASK) != 308710102459SKirill A. Shutemov (vma->vm_pgoff & HPAGE_CACHE_INDEX_MASK)) 308810102459SKirill A. Shutemov return false; 308910102459SKirill A. Shutemov if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end) 309010102459SKirill A. Shutemov return false; 309110102459SKirill A. Shutemov return true; 309210102459SKirill A. Shutemov } 309310102459SKirill A. Shutemov 309482b0f8c3SJan Kara static void deposit_prealloc_pte(struct vm_fault *vmf) 3095953c66c2SAneesh Kumar K.V { 309682b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3097953c66c2SAneesh Kumar K.V 309882b0f8c3SJan Kara pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, vmf->prealloc_pte); 3099953c66c2SAneesh Kumar K.V /* 3100953c66c2SAneesh Kumar K.V * We are going to consume the prealloc table, 3101953c66c2SAneesh Kumar K.V * count that as nr_ptes. 3102953c66c2SAneesh Kumar K.V */ 3103c4812909SKirill A. Shutemov mm_inc_nr_ptes(vma->vm_mm); 31047f2b6ce8STobin C Harding vmf->prealloc_pte = NULL; 3105953c66c2SAneesh Kumar K.V } 3106953c66c2SAneesh Kumar K.V 31072b740303SSouptick Joarder static vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page) 310810102459SKirill A. Shutemov { 310982b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 311082b0f8c3SJan Kara bool write = vmf->flags & FAULT_FLAG_WRITE; 311182b0f8c3SJan Kara unsigned long haddr = vmf->address & HPAGE_PMD_MASK; 311210102459SKirill A. Shutemov pmd_t entry; 31132b740303SSouptick Joarder int i; 31142b740303SSouptick Joarder vm_fault_t ret; 311510102459SKirill A. Shutemov 311610102459SKirill A. Shutemov if (!transhuge_vma_suitable(vma, haddr)) 311710102459SKirill A. Shutemov return VM_FAULT_FALLBACK; 311810102459SKirill A. Shutemov 311910102459SKirill A. Shutemov ret = VM_FAULT_FALLBACK; 312010102459SKirill A. Shutemov page = compound_head(page); 312110102459SKirill A. Shutemov 3122953c66c2SAneesh Kumar K.V /* 3123953c66c2SAneesh Kumar K.V * Archs like ppc64 need additonal space to store information 3124953c66c2SAneesh Kumar K.V * related to pte entry. Use the preallocated table for that. 3125953c66c2SAneesh Kumar K.V */ 312682b0f8c3SJan Kara if (arch_needs_pgtable_deposit() && !vmf->prealloc_pte) { 312782b0f8c3SJan Kara vmf->prealloc_pte = pte_alloc_one(vma->vm_mm, vmf->address); 312882b0f8c3SJan Kara if (!vmf->prealloc_pte) 3129953c66c2SAneesh Kumar K.V return VM_FAULT_OOM; 3130953c66c2SAneesh Kumar K.V smp_wmb(); /* See comment in __pte_alloc() */ 3131953c66c2SAneesh Kumar K.V } 3132953c66c2SAneesh Kumar K.V 313382b0f8c3SJan Kara vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); 313482b0f8c3SJan Kara if (unlikely(!pmd_none(*vmf->pmd))) 313510102459SKirill A. Shutemov goto out; 313610102459SKirill A. Shutemov 313710102459SKirill A. Shutemov for (i = 0; i < HPAGE_PMD_NR; i++) 313810102459SKirill A. Shutemov flush_icache_page(vma, page + i); 313910102459SKirill A. Shutemov 314010102459SKirill A. Shutemov entry = mk_huge_pmd(page, vma->vm_page_prot); 314110102459SKirill A. Shutemov if (write) 3142f55e1014SLinus Torvalds entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); 314310102459SKirill A. Shutemov 3144fadae295SYang Shi add_mm_counter(vma->vm_mm, mm_counter_file(page), HPAGE_PMD_NR); 314510102459SKirill A. Shutemov page_add_file_rmap(page, true); 3146953c66c2SAneesh Kumar K.V /* 3147953c66c2SAneesh Kumar K.V * deposit and withdraw with pmd lock held 3148953c66c2SAneesh Kumar K.V */ 3149953c66c2SAneesh Kumar K.V if (arch_needs_pgtable_deposit()) 315082b0f8c3SJan Kara deposit_prealloc_pte(vmf); 315110102459SKirill A. Shutemov 315282b0f8c3SJan Kara set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry); 315310102459SKirill A. Shutemov 315482b0f8c3SJan Kara update_mmu_cache_pmd(vma, haddr, vmf->pmd); 315510102459SKirill A. Shutemov 315610102459SKirill A. Shutemov /* fault is handled */ 315710102459SKirill A. Shutemov ret = 0; 315895ecedcdSKirill A. Shutemov count_vm_event(THP_FILE_MAPPED); 315910102459SKirill A. Shutemov out: 316082b0f8c3SJan Kara spin_unlock(vmf->ptl); 316110102459SKirill A. Shutemov return ret; 316210102459SKirill A. Shutemov } 316310102459SKirill A. Shutemov #else 31642b740303SSouptick Joarder static vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page) 316510102459SKirill A. Shutemov { 316610102459SKirill A. Shutemov BUILD_BUG(); 316710102459SKirill A. Shutemov return 0; 316810102459SKirill A. Shutemov } 316910102459SKirill A. Shutemov #endif 317010102459SKirill A. Shutemov 31718c6e50b0SKirill A. Shutemov /** 31727267ec00SKirill A. Shutemov * alloc_set_pte - setup new PTE entry for given page and add reverse page 31737267ec00SKirill A. Shutemov * mapping. If needed, the fucntion allocates page table or use pre-allocated. 31748c6e50b0SKirill A. Shutemov * 317582b0f8c3SJan Kara * @vmf: fault environment 31767267ec00SKirill A. Shutemov * @memcg: memcg to charge page (only for private mappings) 31778c6e50b0SKirill A. Shutemov * @page: page to map 31788c6e50b0SKirill A. Shutemov * 317982b0f8c3SJan Kara * Caller must take care of unlocking vmf->ptl, if vmf->pte is non-NULL on 318082b0f8c3SJan Kara * return. 31818c6e50b0SKirill A. Shutemov * 31828c6e50b0SKirill A. Shutemov * Target users are page handler itself and implementations of 31838c6e50b0SKirill A. Shutemov * vm_ops->map_pages. 31848c6e50b0SKirill A. Shutemov */ 31852b740303SSouptick Joarder vm_fault_t alloc_set_pte(struct vm_fault *vmf, struct mem_cgroup *memcg, 31867267ec00SKirill A. Shutemov struct page *page) 31873bb97794SKirill A. Shutemov { 318882b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 318982b0f8c3SJan Kara bool write = vmf->flags & FAULT_FLAG_WRITE; 31903bb97794SKirill A. Shutemov pte_t entry; 31912b740303SSouptick Joarder vm_fault_t ret; 319210102459SKirill A. Shutemov 319382b0f8c3SJan Kara if (pmd_none(*vmf->pmd) && PageTransCompound(page) && 3194e496cf3dSKirill A. Shutemov IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) { 319510102459SKirill A. Shutemov /* THP on COW? */ 319610102459SKirill A. Shutemov VM_BUG_ON_PAGE(memcg, page); 319710102459SKirill A. Shutemov 319882b0f8c3SJan Kara ret = do_set_pmd(vmf, page); 319910102459SKirill A. Shutemov if (ret != VM_FAULT_FALLBACK) 3200b0b9b3dfSHugh Dickins return ret; 320110102459SKirill A. Shutemov } 32023bb97794SKirill A. Shutemov 320382b0f8c3SJan Kara if (!vmf->pte) { 320482b0f8c3SJan Kara ret = pte_alloc_one_map(vmf); 32057267ec00SKirill A. Shutemov if (ret) 3206b0b9b3dfSHugh Dickins return ret; 32077267ec00SKirill A. Shutemov } 32087267ec00SKirill A. Shutemov 32097267ec00SKirill A. Shutemov /* Re-check under ptl */ 3210b0b9b3dfSHugh Dickins if (unlikely(!pte_none(*vmf->pte))) 3211b0b9b3dfSHugh Dickins return VM_FAULT_NOPAGE; 32127267ec00SKirill A. Shutemov 32133bb97794SKirill A. Shutemov flush_icache_page(vma, page); 32143bb97794SKirill A. Shutemov entry = mk_pte(page, vma->vm_page_prot); 32153bb97794SKirill A. Shutemov if (write) 32163bb97794SKirill A. Shutemov entry = maybe_mkwrite(pte_mkdirty(entry), vma); 3217bae473a4SKirill A. Shutemov /* copy-on-write page */ 3218bae473a4SKirill A. Shutemov if (write && !(vma->vm_flags & VM_SHARED)) { 32193bb97794SKirill A. Shutemov inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); 322082b0f8c3SJan Kara page_add_new_anon_rmap(page, vma, vmf->address, false); 32217267ec00SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, false); 32227267ec00SKirill A. Shutemov lru_cache_add_active_or_unevictable(page, vma); 32233bb97794SKirill A. Shutemov } else { 3224eca56ff9SJerome Marchand inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page)); 3225dd78feddSKirill A. Shutemov page_add_file_rmap(page, false); 32263bb97794SKirill A. Shutemov } 322782b0f8c3SJan Kara set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry); 32283bb97794SKirill A. Shutemov 32293bb97794SKirill A. Shutemov /* no need to invalidate: a not-present page won't be cached */ 323082b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 32317267ec00SKirill A. Shutemov 3232b0b9b3dfSHugh Dickins return 0; 32333bb97794SKirill A. Shutemov } 32343bb97794SKirill A. Shutemov 32359118c0cbSJan Kara 32369118c0cbSJan Kara /** 32379118c0cbSJan Kara * finish_fault - finish page fault once we have prepared the page to fault 32389118c0cbSJan Kara * 32399118c0cbSJan Kara * @vmf: structure describing the fault 32409118c0cbSJan Kara * 32419118c0cbSJan Kara * This function handles all that is needed to finish a page fault once the 32429118c0cbSJan Kara * page to fault in is prepared. It handles locking of PTEs, inserts PTE for 32439118c0cbSJan Kara * given page, adds reverse page mapping, handles memcg charges and LRU 32449118c0cbSJan Kara * addition. The function returns 0 on success, VM_FAULT_ code in case of 32459118c0cbSJan Kara * error. 32469118c0cbSJan Kara * 32479118c0cbSJan Kara * The function expects the page to be locked and on success it consumes a 32489118c0cbSJan Kara * reference of a page being mapped (for the PTE which maps it). 32499118c0cbSJan Kara */ 32502b740303SSouptick Joarder vm_fault_t finish_fault(struct vm_fault *vmf) 32519118c0cbSJan Kara { 32529118c0cbSJan Kara struct page *page; 32532b740303SSouptick Joarder vm_fault_t ret = 0; 32549118c0cbSJan Kara 32559118c0cbSJan Kara /* Did we COW the page? */ 32569118c0cbSJan Kara if ((vmf->flags & FAULT_FLAG_WRITE) && 32579118c0cbSJan Kara !(vmf->vma->vm_flags & VM_SHARED)) 32589118c0cbSJan Kara page = vmf->cow_page; 32599118c0cbSJan Kara else 32609118c0cbSJan Kara page = vmf->page; 32616b31d595SMichal Hocko 32626b31d595SMichal Hocko /* 32636b31d595SMichal Hocko * check even for read faults because we might have lost our CoWed 32646b31d595SMichal Hocko * page 32656b31d595SMichal Hocko */ 32666b31d595SMichal Hocko if (!(vmf->vma->vm_flags & VM_SHARED)) 32676b31d595SMichal Hocko ret = check_stable_address_space(vmf->vma->vm_mm); 32686b31d595SMichal Hocko if (!ret) 32699118c0cbSJan Kara ret = alloc_set_pte(vmf, vmf->memcg, page); 32709118c0cbSJan Kara if (vmf->pte) 32719118c0cbSJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 32729118c0cbSJan Kara return ret; 32739118c0cbSJan Kara } 32749118c0cbSJan Kara 32753a91053aSKirill A. Shutemov static unsigned long fault_around_bytes __read_mostly = 32763a91053aSKirill A. Shutemov rounddown_pow_of_two(65536); 3277a9b0f861SKirill A. Shutemov 32781592eef0SKirill A. Shutemov #ifdef CONFIG_DEBUG_FS 3279a9b0f861SKirill A. Shutemov static int fault_around_bytes_get(void *data, u64 *val) 32801592eef0SKirill A. Shutemov { 3281a9b0f861SKirill A. Shutemov *val = fault_around_bytes; 32821592eef0SKirill A. Shutemov return 0; 32831592eef0SKirill A. Shutemov } 32841592eef0SKirill A. Shutemov 3285b4903d6eSAndrey Ryabinin /* 3286da391d64SWilliam Kucharski * fault_around_bytes must be rounded down to the nearest page order as it's 3287da391d64SWilliam Kucharski * what do_fault_around() expects to see. 3288b4903d6eSAndrey Ryabinin */ 3289a9b0f861SKirill A. Shutemov static int fault_around_bytes_set(void *data, u64 val) 32901592eef0SKirill A. Shutemov { 3291a9b0f861SKirill A. Shutemov if (val / PAGE_SIZE > PTRS_PER_PTE) 32921592eef0SKirill A. Shutemov return -EINVAL; 3293b4903d6eSAndrey Ryabinin if (val > PAGE_SIZE) 3294b4903d6eSAndrey Ryabinin fault_around_bytes = rounddown_pow_of_two(val); 3295b4903d6eSAndrey Ryabinin else 3296b4903d6eSAndrey Ryabinin fault_around_bytes = PAGE_SIZE; /* rounddown_pow_of_two(0) is undefined */ 32971592eef0SKirill A. Shutemov return 0; 32981592eef0SKirill A. Shutemov } 32990a1345f8SYevgen Pronenko DEFINE_DEBUGFS_ATTRIBUTE(fault_around_bytes_fops, 3300a9b0f861SKirill A. Shutemov fault_around_bytes_get, fault_around_bytes_set, "%llu\n"); 33011592eef0SKirill A. Shutemov 33021592eef0SKirill A. Shutemov static int __init fault_around_debugfs(void) 33031592eef0SKirill A. Shutemov { 33041592eef0SKirill A. Shutemov void *ret; 33051592eef0SKirill A. Shutemov 33060a1345f8SYevgen Pronenko ret = debugfs_create_file_unsafe("fault_around_bytes", 0644, NULL, NULL, 3307a9b0f861SKirill A. Shutemov &fault_around_bytes_fops); 33081592eef0SKirill A. Shutemov if (!ret) 3309a9b0f861SKirill A. Shutemov pr_warn("Failed to create fault_around_bytes in debugfs"); 33101592eef0SKirill A. Shutemov return 0; 33111592eef0SKirill A. Shutemov } 33121592eef0SKirill A. Shutemov late_initcall(fault_around_debugfs); 33131592eef0SKirill A. Shutemov #endif 33148c6e50b0SKirill A. Shutemov 33151fdb412bSKirill A. Shutemov /* 33161fdb412bSKirill A. Shutemov * do_fault_around() tries to map few pages around the fault address. The hope 33171fdb412bSKirill A. Shutemov * is that the pages will be needed soon and this will lower the number of 33181fdb412bSKirill A. Shutemov * faults to handle. 33191fdb412bSKirill A. Shutemov * 33201fdb412bSKirill A. Shutemov * It uses vm_ops->map_pages() to map the pages, which skips the page if it's 33211fdb412bSKirill A. Shutemov * not ready to be mapped: not up-to-date, locked, etc. 33221fdb412bSKirill A. Shutemov * 33231fdb412bSKirill A. Shutemov * This function is called with the page table lock taken. In the split ptlock 33241fdb412bSKirill A. Shutemov * case the page table lock only protects only those entries which belong to 33251fdb412bSKirill A. Shutemov * the page table corresponding to the fault address. 33261fdb412bSKirill A. Shutemov * 33271fdb412bSKirill A. Shutemov * This function doesn't cross the VMA boundaries, in order to call map_pages() 33281fdb412bSKirill A. Shutemov * only once. 33291fdb412bSKirill A. Shutemov * 3330da391d64SWilliam Kucharski * fault_around_bytes defines how many bytes we'll try to map. 3331da391d64SWilliam Kucharski * do_fault_around() expects it to be set to a power of two less than or equal 3332da391d64SWilliam Kucharski * to PTRS_PER_PTE. 33331fdb412bSKirill A. Shutemov * 3334da391d64SWilliam Kucharski * The virtual address of the area that we map is naturally aligned to 3335da391d64SWilliam Kucharski * fault_around_bytes rounded down to the machine page size 3336da391d64SWilliam Kucharski * (and therefore to page order). This way it's easier to guarantee 3337da391d64SWilliam Kucharski * that we don't cross page table boundaries. 33381fdb412bSKirill A. Shutemov */ 33392b740303SSouptick Joarder static vm_fault_t do_fault_around(struct vm_fault *vmf) 33408c6e50b0SKirill A. Shutemov { 334182b0f8c3SJan Kara unsigned long address = vmf->address, nr_pages, mask; 33420721ec8bSJan Kara pgoff_t start_pgoff = vmf->pgoff; 3343bae473a4SKirill A. Shutemov pgoff_t end_pgoff; 33442b740303SSouptick Joarder int off; 33452b740303SSouptick Joarder vm_fault_t ret = 0; 33468c6e50b0SKirill A. Shutemov 33474db0c3c2SJason Low nr_pages = READ_ONCE(fault_around_bytes) >> PAGE_SHIFT; 3348aecd6f44SKirill A. Shutemov mask = ~(nr_pages * PAGE_SIZE - 1) & PAGE_MASK; 3349aecd6f44SKirill A. Shutemov 335082b0f8c3SJan Kara vmf->address = max(address & mask, vmf->vma->vm_start); 335182b0f8c3SJan Kara off = ((address - vmf->address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1); 3352bae473a4SKirill A. Shutemov start_pgoff -= off; 33538c6e50b0SKirill A. Shutemov 33548c6e50b0SKirill A. Shutemov /* 3355da391d64SWilliam Kucharski * end_pgoff is either the end of the page table, the end of 3356da391d64SWilliam Kucharski * the vma or nr_pages from start_pgoff, depending what is nearest. 33578c6e50b0SKirill A. Shutemov */ 3358bae473a4SKirill A. Shutemov end_pgoff = start_pgoff - 335982b0f8c3SJan Kara ((vmf->address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) + 33608c6e50b0SKirill A. Shutemov PTRS_PER_PTE - 1; 336182b0f8c3SJan Kara end_pgoff = min3(end_pgoff, vma_pages(vmf->vma) + vmf->vma->vm_pgoff - 1, 3362bae473a4SKirill A. Shutemov start_pgoff + nr_pages - 1); 33638c6e50b0SKirill A. Shutemov 336482b0f8c3SJan Kara if (pmd_none(*vmf->pmd)) { 336582b0f8c3SJan Kara vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm, 336682b0f8c3SJan Kara vmf->address); 336782b0f8c3SJan Kara if (!vmf->prealloc_pte) 3368c5f88bd2SVegard Nossum goto out; 33697267ec00SKirill A. Shutemov smp_wmb(); /* See comment in __pte_alloc() */ 33708c6e50b0SKirill A. Shutemov } 33718c6e50b0SKirill A. Shutemov 337282b0f8c3SJan Kara vmf->vma->vm_ops->map_pages(vmf, start_pgoff, end_pgoff); 33737267ec00SKirill A. Shutemov 33747267ec00SKirill A. Shutemov /* Huge page is mapped? Page fault is solved */ 337582b0f8c3SJan Kara if (pmd_trans_huge(*vmf->pmd)) { 33767267ec00SKirill A. Shutemov ret = VM_FAULT_NOPAGE; 33777267ec00SKirill A. Shutemov goto out; 33788c6e50b0SKirill A. Shutemov } 33798c6e50b0SKirill A. Shutemov 33807267ec00SKirill A. Shutemov /* ->map_pages() haven't done anything useful. Cold page cache? */ 338182b0f8c3SJan Kara if (!vmf->pte) 33827267ec00SKirill A. Shutemov goto out; 33837267ec00SKirill A. Shutemov 33847267ec00SKirill A. Shutemov /* check if the page fault is solved */ 338582b0f8c3SJan Kara vmf->pte -= (vmf->address >> PAGE_SHIFT) - (address >> PAGE_SHIFT); 338682b0f8c3SJan Kara if (!pte_none(*vmf->pte)) 33877267ec00SKirill A. Shutemov ret = VM_FAULT_NOPAGE; 338882b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 33897267ec00SKirill A. Shutemov out: 339082b0f8c3SJan Kara vmf->address = address; 339182b0f8c3SJan Kara vmf->pte = NULL; 33927267ec00SKirill A. Shutemov return ret; 33937267ec00SKirill A. Shutemov } 33947267ec00SKirill A. Shutemov 33952b740303SSouptick Joarder static vm_fault_t do_read_fault(struct vm_fault *vmf) 3396e655fb29SKirill A. Shutemov { 339782b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 33982b740303SSouptick Joarder vm_fault_t ret = 0; 33998c6e50b0SKirill A. Shutemov 34008c6e50b0SKirill A. Shutemov /* 34018c6e50b0SKirill A. Shutemov * Let's call ->map_pages() first and use ->fault() as fallback 34028c6e50b0SKirill A. Shutemov * if page by the offset is not ready to be mapped (cold cache or 34038c6e50b0SKirill A. Shutemov * something). 34048c6e50b0SKirill A. Shutemov */ 34059b4bdd2fSKirill A. Shutemov if (vma->vm_ops->map_pages && fault_around_bytes >> PAGE_SHIFT > 1) { 34060721ec8bSJan Kara ret = do_fault_around(vmf); 34077267ec00SKirill A. Shutemov if (ret) 34087267ec00SKirill A. Shutemov return ret; 34098c6e50b0SKirill A. Shutemov } 3410e655fb29SKirill A. Shutemov 3411936ca80dSJan Kara ret = __do_fault(vmf); 3412e655fb29SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3413e655fb29SKirill A. Shutemov return ret; 3414e655fb29SKirill A. Shutemov 34159118c0cbSJan Kara ret |= finish_fault(vmf); 3416936ca80dSJan Kara unlock_page(vmf->page); 34177267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3418936ca80dSJan Kara put_page(vmf->page); 3419e655fb29SKirill A. Shutemov return ret; 3420e655fb29SKirill A. Shutemov } 3421e655fb29SKirill A. Shutemov 34222b740303SSouptick Joarder static vm_fault_t do_cow_fault(struct vm_fault *vmf) 3423ec47c3b9SKirill A. Shutemov { 342482b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 34252b740303SSouptick Joarder vm_fault_t ret; 3426ec47c3b9SKirill A. Shutemov 3427ec47c3b9SKirill A. Shutemov if (unlikely(anon_vma_prepare(vma))) 3428ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 3429ec47c3b9SKirill A. Shutemov 3430936ca80dSJan Kara vmf->cow_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vmf->address); 3431936ca80dSJan Kara if (!vmf->cow_page) 3432ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 3433ec47c3b9SKirill A. Shutemov 34342cf85583STejun Heo if (mem_cgroup_try_charge_delay(vmf->cow_page, vma->vm_mm, GFP_KERNEL, 34353917048dSJan Kara &vmf->memcg, false)) { 3436936ca80dSJan Kara put_page(vmf->cow_page); 3437ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 3438ec47c3b9SKirill A. Shutemov } 3439ec47c3b9SKirill A. Shutemov 3440936ca80dSJan Kara ret = __do_fault(vmf); 3441ec47c3b9SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3442ec47c3b9SKirill A. Shutemov goto uncharge_out; 34433917048dSJan Kara if (ret & VM_FAULT_DONE_COW) 34443917048dSJan Kara return ret; 3445ec47c3b9SKirill A. Shutemov 3446936ca80dSJan Kara copy_user_highpage(vmf->cow_page, vmf->page, vmf->address, vma); 3447936ca80dSJan Kara __SetPageUptodate(vmf->cow_page); 3448ec47c3b9SKirill A. Shutemov 34499118c0cbSJan Kara ret |= finish_fault(vmf); 3450936ca80dSJan Kara unlock_page(vmf->page); 3451936ca80dSJan Kara put_page(vmf->page); 34527267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 34537267ec00SKirill A. Shutemov goto uncharge_out; 3454ec47c3b9SKirill A. Shutemov return ret; 3455ec47c3b9SKirill A. Shutemov uncharge_out: 34563917048dSJan Kara mem_cgroup_cancel_charge(vmf->cow_page, vmf->memcg, false); 3457936ca80dSJan Kara put_page(vmf->cow_page); 3458ec47c3b9SKirill A. Shutemov return ret; 3459ec47c3b9SKirill A. Shutemov } 3460ec47c3b9SKirill A. Shutemov 34612b740303SSouptick Joarder static vm_fault_t do_shared_fault(struct vm_fault *vmf) 34621da177e4SLinus Torvalds { 346382b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 34642b740303SSouptick Joarder vm_fault_t ret, tmp; 34651d65f86dSKAMEZAWA Hiroyuki 3466936ca80dSJan Kara ret = __do_fault(vmf); 34677eae74afSKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3468f0c6d4d2SKirill A. Shutemov return ret; 34691da177e4SLinus Torvalds 34701da177e4SLinus Torvalds /* 3471f0c6d4d2SKirill A. Shutemov * Check if the backing address space wants to know that the page is 3472f0c6d4d2SKirill A. Shutemov * about to become writable 34731da177e4SLinus Torvalds */ 3474fb09a464SKirill A. Shutemov if (vma->vm_ops->page_mkwrite) { 3475936ca80dSJan Kara unlock_page(vmf->page); 347638b8cb7fSJan Kara tmp = do_page_mkwrite(vmf); 3477fb09a464SKirill A. Shutemov if (unlikely(!tmp || 3478fb09a464SKirill A. Shutemov (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { 3479936ca80dSJan Kara put_page(vmf->page); 3480f0c6d4d2SKirill A. Shutemov return tmp; 348169676147SMark Fasheh } 3482d0217ac0SNick Piggin } 3483fb09a464SKirill A. Shutemov 34849118c0cbSJan Kara ret |= finish_fault(vmf); 34857267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | 34867267ec00SKirill A. Shutemov VM_FAULT_RETRY))) { 3487936ca80dSJan Kara unlock_page(vmf->page); 3488936ca80dSJan Kara put_page(vmf->page); 3489f0c6d4d2SKirill A. Shutemov return ret; 34909637a5efSDavid Howells } 3491d00806b1SNick Piggin 349297ba0c2bSJan Kara fault_dirty_shared_page(vma, vmf->page); 3493b827e496SNick Piggin return ret; 349454cb8821SNick Piggin } 3495d00806b1SNick Piggin 34969a95f3cfSPaul Cassella /* 34979a95f3cfSPaul Cassella * We enter with non-exclusive mmap_sem (to exclude vma changes, 34989a95f3cfSPaul Cassella * but allow concurrent faults). 34999a95f3cfSPaul Cassella * The mmap_sem may have been released depending on flags and our 35009a95f3cfSPaul Cassella * return value. See filemap_fault() and __lock_page_or_retry(). 35019a95f3cfSPaul Cassella */ 35022b740303SSouptick Joarder static vm_fault_t do_fault(struct vm_fault *vmf) 350354cb8821SNick Piggin { 350482b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 35052b740303SSouptick Joarder vm_fault_t ret; 350654cb8821SNick Piggin 35076b7339f4SKirill A. Shutemov /* The VMA was not fully populated on mmap() or missing VM_DONTEXPAND */ 35086b7339f4SKirill A. Shutemov if (!vma->vm_ops->fault) 3509b0b9b3dfSHugh Dickins ret = VM_FAULT_SIGBUS; 3510b0b9b3dfSHugh Dickins else if (!(vmf->flags & FAULT_FLAG_WRITE)) 3511b0b9b3dfSHugh Dickins ret = do_read_fault(vmf); 3512b0b9b3dfSHugh Dickins else if (!(vma->vm_flags & VM_SHARED)) 3513b0b9b3dfSHugh Dickins ret = do_cow_fault(vmf); 3514b0b9b3dfSHugh Dickins else 3515b0b9b3dfSHugh Dickins ret = do_shared_fault(vmf); 3516b0b9b3dfSHugh Dickins 3517b0b9b3dfSHugh Dickins /* preallocated pagetable is unused: free it */ 3518b0b9b3dfSHugh Dickins if (vmf->prealloc_pte) { 3519b0b9b3dfSHugh Dickins pte_free(vma->vm_mm, vmf->prealloc_pte); 35207f2b6ce8STobin C Harding vmf->prealloc_pte = NULL; 3521b0b9b3dfSHugh Dickins } 3522b0b9b3dfSHugh Dickins return ret; 352354cb8821SNick Piggin } 352454cb8821SNick Piggin 3525b19a9939SRashika Kheria static int numa_migrate_prep(struct page *page, struct vm_area_struct *vma, 352604bb2f94SRik van Riel unsigned long addr, int page_nid, 352704bb2f94SRik van Riel int *flags) 35289532fec1SMel Gorman { 35299532fec1SMel Gorman get_page(page); 35309532fec1SMel Gorman 35319532fec1SMel Gorman count_vm_numa_event(NUMA_HINT_FAULTS); 353204bb2f94SRik van Riel if (page_nid == numa_node_id()) { 35339532fec1SMel Gorman count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL); 353404bb2f94SRik van Riel *flags |= TNF_FAULT_LOCAL; 353504bb2f94SRik van Riel } 35369532fec1SMel Gorman 35379532fec1SMel Gorman return mpol_misplaced(page, vma, addr); 35389532fec1SMel Gorman } 35399532fec1SMel Gorman 35402b740303SSouptick Joarder static vm_fault_t do_numa_page(struct vm_fault *vmf) 3541d10e63f2SMel Gorman { 354282b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 35434daae3b4SMel Gorman struct page *page = NULL; 35448191acbdSMel Gorman int page_nid = -1; 354590572890SPeter Zijlstra int last_cpupid; 3546cbee9f88SPeter Zijlstra int target_nid; 3547b8593bfdSMel Gorman bool migrated = false; 3548cee216a6SAneesh Kumar K.V pte_t pte; 3549288bc549SAneesh Kumar K.V bool was_writable = pte_savedwrite(vmf->orig_pte); 35506688cc05SPeter Zijlstra int flags = 0; 3551d10e63f2SMel Gorman 3552d10e63f2SMel Gorman /* 3553d10e63f2SMel Gorman * The "pte" at this point cannot be used safely without 3554d10e63f2SMel Gorman * validation through pte_unmap_same(). It's of NUMA type but 3555d10e63f2SMel Gorman * the pfn may be screwed if the read is non atomic. 3556d10e63f2SMel Gorman */ 355782b0f8c3SJan Kara vmf->ptl = pte_lockptr(vma->vm_mm, vmf->pmd); 355882b0f8c3SJan Kara spin_lock(vmf->ptl); 3559cee216a6SAneesh Kumar K.V if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) { 356082b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 35614daae3b4SMel Gorman goto out; 35624daae3b4SMel Gorman } 35634daae3b4SMel Gorman 3564cee216a6SAneesh Kumar K.V /* 3565cee216a6SAneesh Kumar K.V * Make it present again, Depending on how arch implementes non 3566cee216a6SAneesh Kumar K.V * accessible ptes, some can allow access by kernel mode. 3567cee216a6SAneesh Kumar K.V */ 3568cee216a6SAneesh Kumar K.V pte = ptep_modify_prot_start(vma->vm_mm, vmf->address, vmf->pte); 35694d942466SMel Gorman pte = pte_modify(pte, vma->vm_page_prot); 35704d942466SMel Gorman pte = pte_mkyoung(pte); 3571b191f9b1SMel Gorman if (was_writable) 3572b191f9b1SMel Gorman pte = pte_mkwrite(pte); 3573cee216a6SAneesh Kumar K.V ptep_modify_prot_commit(vma->vm_mm, vmf->address, vmf->pte, pte); 357482b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 3575d10e63f2SMel Gorman 357682b0f8c3SJan Kara page = vm_normal_page(vma, vmf->address, pte); 3577d10e63f2SMel Gorman if (!page) { 357882b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3579d10e63f2SMel Gorman return 0; 3580d10e63f2SMel Gorman } 3581d10e63f2SMel Gorman 3582e81c4802SKirill A. Shutemov /* TODO: handle PTE-mapped THP */ 3583e81c4802SKirill A. Shutemov if (PageCompound(page)) { 358482b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3585e81c4802SKirill A. Shutemov return 0; 3586e81c4802SKirill A. Shutemov } 3587e81c4802SKirill A. Shutemov 35886688cc05SPeter Zijlstra /* 3589bea66fbdSMel Gorman * Avoid grouping on RO pages in general. RO pages shouldn't hurt as 3590bea66fbdSMel Gorman * much anyway since they can be in shared cache state. This misses 3591bea66fbdSMel Gorman * the case where a mapping is writable but the process never writes 3592bea66fbdSMel Gorman * to it but pte_write gets cleared during protection updates and 3593bea66fbdSMel Gorman * pte_dirty has unpredictable behaviour between PTE scan updates, 3594bea66fbdSMel Gorman * background writeback, dirty balancing and application behaviour. 35956688cc05SPeter Zijlstra */ 3596d59dc7bcSRik van Riel if (!pte_write(pte)) 35976688cc05SPeter Zijlstra flags |= TNF_NO_GROUP; 35986688cc05SPeter Zijlstra 3599dabe1d99SRik van Riel /* 3600dabe1d99SRik van Riel * Flag if the page is shared between multiple address spaces. This 3601dabe1d99SRik van Riel * is later used when determining whether to group tasks together 3602dabe1d99SRik van Riel */ 3603dabe1d99SRik van Riel if (page_mapcount(page) > 1 && (vma->vm_flags & VM_SHARED)) 3604dabe1d99SRik van Riel flags |= TNF_SHARED; 3605dabe1d99SRik van Riel 360690572890SPeter Zijlstra last_cpupid = page_cpupid_last(page); 36078191acbdSMel Gorman page_nid = page_to_nid(page); 360882b0f8c3SJan Kara target_nid = numa_migrate_prep(page, vma, vmf->address, page_nid, 3609bae473a4SKirill A. Shutemov &flags); 361082b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 36114daae3b4SMel Gorman if (target_nid == -1) { 36124daae3b4SMel Gorman put_page(page); 36134daae3b4SMel Gorman goto out; 36144daae3b4SMel Gorman } 36154daae3b4SMel Gorman 36164daae3b4SMel Gorman /* Migrate to the requested node */ 36171bc115d8SMel Gorman migrated = migrate_misplaced_page(page, vma, target_nid); 36186688cc05SPeter Zijlstra if (migrated) { 36198191acbdSMel Gorman page_nid = target_nid; 36206688cc05SPeter Zijlstra flags |= TNF_MIGRATED; 3621074c2381SMel Gorman } else 3622074c2381SMel Gorman flags |= TNF_MIGRATE_FAIL; 36234daae3b4SMel Gorman 36244daae3b4SMel Gorman out: 36258191acbdSMel Gorman if (page_nid != -1) 36266688cc05SPeter Zijlstra task_numa_fault(last_cpupid, page_nid, 1, flags); 3627d10e63f2SMel Gorman return 0; 3628d10e63f2SMel Gorman } 3629d10e63f2SMel Gorman 36302b740303SSouptick Joarder static inline vm_fault_t create_huge_pmd(struct vm_fault *vmf) 3631b96375f7SMatthew Wilcox { 3632f4200391SDave Jiang if (vma_is_anonymous(vmf->vma)) 363382b0f8c3SJan Kara return do_huge_pmd_anonymous_page(vmf); 3634a2d58167SDave Jiang if (vmf->vma->vm_ops->huge_fault) 3635c791ace1SDave Jiang return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD); 3636b96375f7SMatthew Wilcox return VM_FAULT_FALLBACK; 3637b96375f7SMatthew Wilcox } 3638b96375f7SMatthew Wilcox 3639183f24aaSGeert Uytterhoeven /* `inline' is required to avoid gcc 4.1.2 build error */ 36402b740303SSouptick Joarder static inline vm_fault_t wp_huge_pmd(struct vm_fault *vmf, pmd_t orig_pmd) 3641b96375f7SMatthew Wilcox { 364282b0f8c3SJan Kara if (vma_is_anonymous(vmf->vma)) 364382b0f8c3SJan Kara return do_huge_pmd_wp_page(vmf, orig_pmd); 3644a2d58167SDave Jiang if (vmf->vma->vm_ops->huge_fault) 3645c791ace1SDave Jiang return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD); 3646af9e4d5fSKirill A. Shutemov 3647af9e4d5fSKirill A. Shutemov /* COW handled on pte level: split pmd */ 364882b0f8c3SJan Kara VM_BUG_ON_VMA(vmf->vma->vm_flags & VM_SHARED, vmf->vma); 364982b0f8c3SJan Kara __split_huge_pmd(vmf->vma, vmf->pmd, vmf->address, false, NULL); 3650af9e4d5fSKirill A. Shutemov 3651b96375f7SMatthew Wilcox return VM_FAULT_FALLBACK; 3652b96375f7SMatthew Wilcox } 3653b96375f7SMatthew Wilcox 365438e08854SLorenzo Stoakes static inline bool vma_is_accessible(struct vm_area_struct *vma) 365538e08854SLorenzo Stoakes { 365638e08854SLorenzo Stoakes return vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE); 365738e08854SLorenzo Stoakes } 365838e08854SLorenzo Stoakes 36592b740303SSouptick Joarder static vm_fault_t create_huge_pud(struct vm_fault *vmf) 3660a00cc7d9SMatthew Wilcox { 3661a00cc7d9SMatthew Wilcox #ifdef CONFIG_TRANSPARENT_HUGEPAGE 3662a00cc7d9SMatthew Wilcox /* No support for anonymous transparent PUD pages yet */ 3663a00cc7d9SMatthew Wilcox if (vma_is_anonymous(vmf->vma)) 3664a00cc7d9SMatthew Wilcox return VM_FAULT_FALLBACK; 3665a00cc7d9SMatthew Wilcox if (vmf->vma->vm_ops->huge_fault) 3666c791ace1SDave Jiang return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD); 3667a00cc7d9SMatthew Wilcox #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 3668a00cc7d9SMatthew Wilcox return VM_FAULT_FALLBACK; 3669a00cc7d9SMatthew Wilcox } 3670a00cc7d9SMatthew Wilcox 36712b740303SSouptick Joarder static vm_fault_t wp_huge_pud(struct vm_fault *vmf, pud_t orig_pud) 3672a00cc7d9SMatthew Wilcox { 3673a00cc7d9SMatthew Wilcox #ifdef CONFIG_TRANSPARENT_HUGEPAGE 3674a00cc7d9SMatthew Wilcox /* No support for anonymous transparent PUD pages yet */ 3675a00cc7d9SMatthew Wilcox if (vma_is_anonymous(vmf->vma)) 3676a00cc7d9SMatthew Wilcox return VM_FAULT_FALLBACK; 3677a00cc7d9SMatthew Wilcox if (vmf->vma->vm_ops->huge_fault) 3678c791ace1SDave Jiang return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD); 3679a00cc7d9SMatthew Wilcox #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 3680a00cc7d9SMatthew Wilcox return VM_FAULT_FALLBACK; 3681a00cc7d9SMatthew Wilcox } 3682a00cc7d9SMatthew Wilcox 36831da177e4SLinus Torvalds /* 36841da177e4SLinus Torvalds * These routines also need to handle stuff like marking pages dirty 36851da177e4SLinus Torvalds * and/or accessed for architectures that don't do it in hardware (most 36861da177e4SLinus Torvalds * RISC architectures). The early dirtying is also good on the i386. 36871da177e4SLinus Torvalds * 36881da177e4SLinus Torvalds * There is also a hook called "update_mmu_cache()" that architectures 36891da177e4SLinus Torvalds * with external mmu caches can use to update those (ie the Sparc or 36901da177e4SLinus Torvalds * PowerPC hashed page tables that act as extended TLBs). 36911da177e4SLinus Torvalds * 36927267ec00SKirill A. Shutemov * We enter with non-exclusive mmap_sem (to exclude vma changes, but allow 36937267ec00SKirill A. Shutemov * concurrent faults). 36949a95f3cfSPaul Cassella * 36957267ec00SKirill A. Shutemov * The mmap_sem may have been released depending on flags and our return value. 36967267ec00SKirill A. Shutemov * See filemap_fault() and __lock_page_or_retry(). 36971da177e4SLinus Torvalds */ 36982b740303SSouptick Joarder static vm_fault_t handle_pte_fault(struct vm_fault *vmf) 36991da177e4SLinus Torvalds { 37001da177e4SLinus Torvalds pte_t entry; 37011da177e4SLinus Torvalds 370282b0f8c3SJan Kara if (unlikely(pmd_none(*vmf->pmd))) { 37037267ec00SKirill A. Shutemov /* 37047267ec00SKirill A. Shutemov * Leave __pte_alloc() until later: because vm_ops->fault may 37057267ec00SKirill A. Shutemov * want to allocate huge page, and if we expose page table 37067267ec00SKirill A. Shutemov * for an instant, it will be difficult to retract from 37077267ec00SKirill A. Shutemov * concurrent faults and from rmap lookups. 37087267ec00SKirill A. Shutemov */ 370982b0f8c3SJan Kara vmf->pte = NULL; 37107267ec00SKirill A. Shutemov } else { 37117267ec00SKirill A. Shutemov /* See comment in pte_alloc_one_map() */ 3712d0f0931dSRoss Zwisler if (pmd_devmap_trans_unstable(vmf->pmd)) 37137267ec00SKirill A. Shutemov return 0; 37147267ec00SKirill A. Shutemov /* 37157267ec00SKirill A. Shutemov * A regular pmd is established and it can't morph into a huge 37167267ec00SKirill A. Shutemov * pmd from under us anymore at this point because we hold the 37177267ec00SKirill A. Shutemov * mmap_sem read mode and khugepaged takes it in write mode. 37187267ec00SKirill A. Shutemov * So now it's safe to run pte_offset_map(). 37197267ec00SKirill A. Shutemov */ 372082b0f8c3SJan Kara vmf->pte = pte_offset_map(vmf->pmd, vmf->address); 37212994302bSJan Kara vmf->orig_pte = *vmf->pte; 37227267ec00SKirill A. Shutemov 3723e37c6982SChristian Borntraeger /* 3724e37c6982SChristian Borntraeger * some architectures can have larger ptes than wordsize, 37257267ec00SKirill A. Shutemov * e.g.ppc44x-defconfig has CONFIG_PTE_64BIT=y and 3726b03a0fe0SPaul E. McKenney * CONFIG_32BIT=y, so READ_ONCE cannot guarantee atomic 3727b03a0fe0SPaul E. McKenney * accesses. The code below just needs a consistent view 3728b03a0fe0SPaul E. McKenney * for the ifs and we later double check anyway with the 37297267ec00SKirill A. Shutemov * ptl lock held. So here a barrier will do. 3730e37c6982SChristian Borntraeger */ 3731e37c6982SChristian Borntraeger barrier(); 37322994302bSJan Kara if (pte_none(vmf->orig_pte)) { 373382b0f8c3SJan Kara pte_unmap(vmf->pte); 373482b0f8c3SJan Kara vmf->pte = NULL; 37357267ec00SKirill A. Shutemov } 37367267ec00SKirill A. Shutemov } 37377267ec00SKirill A. Shutemov 373882b0f8c3SJan Kara if (!vmf->pte) { 373982b0f8c3SJan Kara if (vma_is_anonymous(vmf->vma)) 374082b0f8c3SJan Kara return do_anonymous_page(vmf); 3741b5330628SOleg Nesterov else 374282b0f8c3SJan Kara return do_fault(vmf); 374365500d23SHugh Dickins } 37447267ec00SKirill A. Shutemov 37452994302bSJan Kara if (!pte_present(vmf->orig_pte)) 37462994302bSJan Kara return do_swap_page(vmf); 37471da177e4SLinus Torvalds 37482994302bSJan Kara if (pte_protnone(vmf->orig_pte) && vma_is_accessible(vmf->vma)) 37492994302bSJan Kara return do_numa_page(vmf); 3750d10e63f2SMel Gorman 375182b0f8c3SJan Kara vmf->ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd); 375282b0f8c3SJan Kara spin_lock(vmf->ptl); 37532994302bSJan Kara entry = vmf->orig_pte; 375482b0f8c3SJan Kara if (unlikely(!pte_same(*vmf->pte, entry))) 37558f4e2101SHugh Dickins goto unlock; 375682b0f8c3SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) { 3757f6f37321SLinus Torvalds if (!pte_write(entry)) 37582994302bSJan Kara return do_wp_page(vmf); 37591da177e4SLinus Torvalds entry = pte_mkdirty(entry); 37601da177e4SLinus Torvalds } 37611da177e4SLinus Torvalds entry = pte_mkyoung(entry); 376282b0f8c3SJan Kara if (ptep_set_access_flags(vmf->vma, vmf->address, vmf->pte, entry, 376382b0f8c3SJan Kara vmf->flags & FAULT_FLAG_WRITE)) { 376482b0f8c3SJan Kara update_mmu_cache(vmf->vma, vmf->address, vmf->pte); 37651a44e149SAndrea Arcangeli } else { 37661a44e149SAndrea Arcangeli /* 37671a44e149SAndrea Arcangeli * This is needed only for protection faults but the arch code 37681a44e149SAndrea Arcangeli * is not yet telling us if this is a protection fault or not. 37691a44e149SAndrea Arcangeli * This still avoids useless tlb flushes for .text page faults 37701a44e149SAndrea Arcangeli * with threads. 37711a44e149SAndrea Arcangeli */ 377282b0f8c3SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) 377382b0f8c3SJan Kara flush_tlb_fix_spurious_fault(vmf->vma, vmf->address); 37741a44e149SAndrea Arcangeli } 37758f4e2101SHugh Dickins unlock: 377682b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 377783c54070SNick Piggin return 0; 37781da177e4SLinus Torvalds } 37791da177e4SLinus Torvalds 37801da177e4SLinus Torvalds /* 37811da177e4SLinus Torvalds * By the time we get here, we already hold the mm semaphore 37829a95f3cfSPaul Cassella * 37839a95f3cfSPaul Cassella * The mmap_sem may have been released depending on flags and our 37849a95f3cfSPaul Cassella * return value. See filemap_fault() and __lock_page_or_retry(). 37851da177e4SLinus Torvalds */ 37862b740303SSouptick Joarder static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma, 37872b740303SSouptick Joarder unsigned long address, unsigned int flags) 37881da177e4SLinus Torvalds { 378982b0f8c3SJan Kara struct vm_fault vmf = { 3790bae473a4SKirill A. Shutemov .vma = vma, 37911a29d85eSJan Kara .address = address & PAGE_MASK, 3792bae473a4SKirill A. Shutemov .flags = flags, 37930721ec8bSJan Kara .pgoff = linear_page_index(vma, address), 3794667240e0SJan Kara .gfp_mask = __get_fault_gfp_mask(vma), 3795bae473a4SKirill A. Shutemov }; 3796fde26bedSAnshuman Khandual unsigned int dirty = flags & FAULT_FLAG_WRITE; 3797dcddffd4SKirill A. Shutemov struct mm_struct *mm = vma->vm_mm; 37981da177e4SLinus Torvalds pgd_t *pgd; 3799c2febafcSKirill A. Shutemov p4d_t *p4d; 38002b740303SSouptick Joarder vm_fault_t ret; 38011da177e4SLinus Torvalds 38021da177e4SLinus Torvalds pgd = pgd_offset(mm, address); 3803c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, address); 3804c2febafcSKirill A. Shutemov if (!p4d) 3805c2febafcSKirill A. Shutemov return VM_FAULT_OOM; 3806a00cc7d9SMatthew Wilcox 3807c2febafcSKirill A. Shutemov vmf.pud = pud_alloc(mm, p4d, address); 3808a00cc7d9SMatthew Wilcox if (!vmf.pud) 3809c74df32cSHugh Dickins return VM_FAULT_OOM; 3810a00cc7d9SMatthew Wilcox if (pud_none(*vmf.pud) && transparent_hugepage_enabled(vma)) { 3811a00cc7d9SMatthew Wilcox ret = create_huge_pud(&vmf); 3812a00cc7d9SMatthew Wilcox if (!(ret & VM_FAULT_FALLBACK)) 3813a00cc7d9SMatthew Wilcox return ret; 3814a00cc7d9SMatthew Wilcox } else { 3815a00cc7d9SMatthew Wilcox pud_t orig_pud = *vmf.pud; 3816a00cc7d9SMatthew Wilcox 3817a00cc7d9SMatthew Wilcox barrier(); 3818a00cc7d9SMatthew Wilcox if (pud_trans_huge(orig_pud) || pud_devmap(orig_pud)) { 3819a00cc7d9SMatthew Wilcox 3820a00cc7d9SMatthew Wilcox /* NUMA case for anonymous PUDs would go here */ 3821a00cc7d9SMatthew Wilcox 3822f6f37321SLinus Torvalds if (dirty && !pud_write(orig_pud)) { 3823a00cc7d9SMatthew Wilcox ret = wp_huge_pud(&vmf, orig_pud); 3824a00cc7d9SMatthew Wilcox if (!(ret & VM_FAULT_FALLBACK)) 3825a00cc7d9SMatthew Wilcox return ret; 3826a00cc7d9SMatthew Wilcox } else { 3827a00cc7d9SMatthew Wilcox huge_pud_set_accessed(&vmf, orig_pud); 3828a00cc7d9SMatthew Wilcox return 0; 3829a00cc7d9SMatthew Wilcox } 3830a00cc7d9SMatthew Wilcox } 3831a00cc7d9SMatthew Wilcox } 3832a00cc7d9SMatthew Wilcox 3833a00cc7d9SMatthew Wilcox vmf.pmd = pmd_alloc(mm, vmf.pud, address); 383482b0f8c3SJan Kara if (!vmf.pmd) 3835c74df32cSHugh Dickins return VM_FAULT_OOM; 383682b0f8c3SJan Kara if (pmd_none(*vmf.pmd) && transparent_hugepage_enabled(vma)) { 3837a2d58167SDave Jiang ret = create_huge_pmd(&vmf); 3838c0292554SKirill A. Shutemov if (!(ret & VM_FAULT_FALLBACK)) 3839c0292554SKirill A. Shutemov return ret; 384071e3aac0SAndrea Arcangeli } else { 384182b0f8c3SJan Kara pmd_t orig_pmd = *vmf.pmd; 38421f1d06c3SDavid Rientjes 384371e3aac0SAndrea Arcangeli barrier(); 384484c3fc4eSZi Yan if (unlikely(is_swap_pmd(orig_pmd))) { 384584c3fc4eSZi Yan VM_BUG_ON(thp_migration_supported() && 384684c3fc4eSZi Yan !is_pmd_migration_entry(orig_pmd)); 384784c3fc4eSZi Yan if (is_pmd_migration_entry(orig_pmd)) 384884c3fc4eSZi Yan pmd_migration_entry_wait(mm, vmf.pmd); 384984c3fc4eSZi Yan return 0; 385084c3fc4eSZi Yan } 38515c7fb56eSDan Williams if (pmd_trans_huge(orig_pmd) || pmd_devmap(orig_pmd)) { 385238e08854SLorenzo Stoakes if (pmd_protnone(orig_pmd) && vma_is_accessible(vma)) 385382b0f8c3SJan Kara return do_huge_pmd_numa_page(&vmf, orig_pmd); 3854d10e63f2SMel Gorman 3855f6f37321SLinus Torvalds if (dirty && !pmd_write(orig_pmd)) { 385682b0f8c3SJan Kara ret = wp_huge_pmd(&vmf, orig_pmd); 38579845cbbdSKirill A. Shutemov if (!(ret & VM_FAULT_FALLBACK)) 38581f1d06c3SDavid Rientjes return ret; 3859a1dd450bSWill Deacon } else { 386082b0f8c3SJan Kara huge_pmd_set_accessed(&vmf, orig_pmd); 386171e3aac0SAndrea Arcangeli return 0; 386271e3aac0SAndrea Arcangeli } 386371e3aac0SAndrea Arcangeli } 38649845cbbdSKirill A. Shutemov } 386571e3aac0SAndrea Arcangeli 386682b0f8c3SJan Kara return handle_pte_fault(&vmf); 38671da177e4SLinus Torvalds } 38681da177e4SLinus Torvalds 38699a95f3cfSPaul Cassella /* 38709a95f3cfSPaul Cassella * By the time we get here, we already hold the mm semaphore 38719a95f3cfSPaul Cassella * 38729a95f3cfSPaul Cassella * The mmap_sem may have been released depending on flags and our 38739a95f3cfSPaul Cassella * return value. See filemap_fault() and __lock_page_or_retry(). 38749a95f3cfSPaul Cassella */ 38752b740303SSouptick Joarder vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address, 3876dcddffd4SKirill A. Shutemov unsigned int flags) 3877519e5247SJohannes Weiner { 38782b740303SSouptick Joarder vm_fault_t ret; 3879519e5247SJohannes Weiner 3880519e5247SJohannes Weiner __set_current_state(TASK_RUNNING); 3881519e5247SJohannes Weiner 3882519e5247SJohannes Weiner count_vm_event(PGFAULT); 38832262185cSRoman Gushchin count_memcg_event_mm(vma->vm_mm, PGFAULT); 3884519e5247SJohannes Weiner 3885519e5247SJohannes Weiner /* do counter updates before entering really critical section. */ 3886519e5247SJohannes Weiner check_sync_rss_stat(current); 3887519e5247SJohannes Weiner 3888de0c799bSLaurent Dufour if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE, 3889de0c799bSLaurent Dufour flags & FAULT_FLAG_INSTRUCTION, 3890de0c799bSLaurent Dufour flags & FAULT_FLAG_REMOTE)) 3891de0c799bSLaurent Dufour return VM_FAULT_SIGSEGV; 3892de0c799bSLaurent Dufour 3893519e5247SJohannes Weiner /* 3894519e5247SJohannes Weiner * Enable the memcg OOM handling for faults triggered in user 3895519e5247SJohannes Weiner * space. Kernel faults are handled more gracefully. 3896519e5247SJohannes Weiner */ 3897519e5247SJohannes Weiner if (flags & FAULT_FLAG_USER) 389829ef680aSMichal Hocko mem_cgroup_enter_user_fault(); 3899519e5247SJohannes Weiner 3900bae473a4SKirill A. Shutemov if (unlikely(is_vm_hugetlb_page(vma))) 3901bae473a4SKirill A. Shutemov ret = hugetlb_fault(vma->vm_mm, vma, address, flags); 3902bae473a4SKirill A. Shutemov else 3903dcddffd4SKirill A. Shutemov ret = __handle_mm_fault(vma, address, flags); 3904519e5247SJohannes Weiner 390549426420SJohannes Weiner if (flags & FAULT_FLAG_USER) { 390629ef680aSMichal Hocko mem_cgroup_exit_user_fault(); 390749426420SJohannes Weiner /* 390849426420SJohannes Weiner * The task may have entered a memcg OOM situation but 390949426420SJohannes Weiner * if the allocation error was handled gracefully (no 391049426420SJohannes Weiner * VM_FAULT_OOM), there is no need to kill anything. 391149426420SJohannes Weiner * Just clean up the OOM state peacefully. 391249426420SJohannes Weiner */ 391349426420SJohannes Weiner if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM)) 391449426420SJohannes Weiner mem_cgroup_oom_synchronize(false); 391549426420SJohannes Weiner } 39163812c8c8SJohannes Weiner 3917519e5247SJohannes Weiner return ret; 3918519e5247SJohannes Weiner } 3919e1d6d01aSJesse Barnes EXPORT_SYMBOL_GPL(handle_mm_fault); 3920519e5247SJohannes Weiner 392190eceff1SKirill A. Shutemov #ifndef __PAGETABLE_P4D_FOLDED 392290eceff1SKirill A. Shutemov /* 392390eceff1SKirill A. Shutemov * Allocate p4d page table. 392490eceff1SKirill A. Shutemov * We've already handled the fast-path in-line. 392590eceff1SKirill A. Shutemov */ 392690eceff1SKirill A. Shutemov int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address) 392790eceff1SKirill A. Shutemov { 392890eceff1SKirill A. Shutemov p4d_t *new = p4d_alloc_one(mm, address); 392990eceff1SKirill A. Shutemov if (!new) 393090eceff1SKirill A. Shutemov return -ENOMEM; 393190eceff1SKirill A. Shutemov 393290eceff1SKirill A. Shutemov smp_wmb(); /* See comment in __pte_alloc */ 393390eceff1SKirill A. Shutemov 393490eceff1SKirill A. Shutemov spin_lock(&mm->page_table_lock); 393590eceff1SKirill A. Shutemov if (pgd_present(*pgd)) /* Another has populated it */ 393690eceff1SKirill A. Shutemov p4d_free(mm, new); 393790eceff1SKirill A. Shutemov else 393890eceff1SKirill A. Shutemov pgd_populate(mm, pgd, new); 393990eceff1SKirill A. Shutemov spin_unlock(&mm->page_table_lock); 394090eceff1SKirill A. Shutemov return 0; 394190eceff1SKirill A. Shutemov } 394290eceff1SKirill A. Shutemov #endif /* __PAGETABLE_P4D_FOLDED */ 394390eceff1SKirill A. Shutemov 39441da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED 39451da177e4SLinus Torvalds /* 39461da177e4SLinus Torvalds * Allocate page upper directory. 3947872fec16SHugh Dickins * We've already handled the fast-path in-line. 39481da177e4SLinus Torvalds */ 3949c2febafcSKirill A. Shutemov int __pud_alloc(struct mm_struct *mm, p4d_t *p4d, unsigned long address) 39501da177e4SLinus Torvalds { 3951c74df32cSHugh Dickins pud_t *new = pud_alloc_one(mm, address); 3952c74df32cSHugh Dickins if (!new) 39531bb3630eSHugh Dickins return -ENOMEM; 39541da177e4SLinus Torvalds 3955362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 3956362a61adSNick Piggin 3957872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 3958c2febafcSKirill A. Shutemov #ifndef __ARCH_HAS_5LEVEL_HACK 3959b4e98d9aSKirill A. Shutemov if (!p4d_present(*p4d)) { 3960b4e98d9aSKirill A. Shutemov mm_inc_nr_puds(mm); 3961c2febafcSKirill A. Shutemov p4d_populate(mm, p4d, new); 3962b4e98d9aSKirill A. Shutemov } else /* Another has populated it */ 3963c2febafcSKirill A. Shutemov pud_free(mm, new); 3964b4e98d9aSKirill A. Shutemov #else 3965b4e98d9aSKirill A. Shutemov if (!pgd_present(*p4d)) { 3966b4e98d9aSKirill A. Shutemov mm_inc_nr_puds(mm); 3967c2febafcSKirill A. Shutemov pgd_populate(mm, p4d, new); 3968b4e98d9aSKirill A. Shutemov } else /* Another has populated it */ 3969b4e98d9aSKirill A. Shutemov pud_free(mm, new); 3970c2febafcSKirill A. Shutemov #endif /* __ARCH_HAS_5LEVEL_HACK */ 3971872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 39721bb3630eSHugh Dickins return 0; 39731da177e4SLinus Torvalds } 39741da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */ 39751da177e4SLinus Torvalds 39761da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED 39771da177e4SLinus Torvalds /* 39781da177e4SLinus Torvalds * Allocate page middle directory. 3979872fec16SHugh Dickins * We've already handled the fast-path in-line. 39801da177e4SLinus Torvalds */ 39811bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) 39821da177e4SLinus Torvalds { 3983a00cc7d9SMatthew Wilcox spinlock_t *ptl; 3984c74df32cSHugh Dickins pmd_t *new = pmd_alloc_one(mm, address); 3985c74df32cSHugh Dickins if (!new) 39861bb3630eSHugh Dickins return -ENOMEM; 39871da177e4SLinus Torvalds 3988362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 3989362a61adSNick Piggin 3990a00cc7d9SMatthew Wilcox ptl = pud_lock(mm, pud); 39911da177e4SLinus Torvalds #ifndef __ARCH_HAS_4LEVEL_HACK 3992dc6c9a35SKirill A. Shutemov if (!pud_present(*pud)) { 3993dc6c9a35SKirill A. Shutemov mm_inc_nr_pmds(mm); 39941da177e4SLinus Torvalds pud_populate(mm, pud, new); 3995dc6c9a35SKirill A. Shutemov } else /* Another has populated it */ 39965e541973SBenjamin Herrenschmidt pmd_free(mm, new); 3997dc6c9a35SKirill A. Shutemov #else 3998dc6c9a35SKirill A. Shutemov if (!pgd_present(*pud)) { 3999dc6c9a35SKirill A. Shutemov mm_inc_nr_pmds(mm); 40001da177e4SLinus Torvalds pgd_populate(mm, pud, new); 4001dc6c9a35SKirill A. Shutemov } else /* Another has populated it */ 4002dc6c9a35SKirill A. Shutemov pmd_free(mm, new); 40031da177e4SLinus Torvalds #endif /* __ARCH_HAS_4LEVEL_HACK */ 4004a00cc7d9SMatthew Wilcox spin_unlock(ptl); 40051bb3630eSHugh Dickins return 0; 40061da177e4SLinus Torvalds } 40071da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */ 40081da177e4SLinus Torvalds 400909796395SRoss Zwisler static int __follow_pte_pmd(struct mm_struct *mm, unsigned long address, 4010a4d1a885SJérôme Glisse unsigned long *start, unsigned long *end, 401109796395SRoss Zwisler pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp) 4012f8ad0f49SJohannes Weiner { 4013f8ad0f49SJohannes Weiner pgd_t *pgd; 4014c2febafcSKirill A. Shutemov p4d_t *p4d; 4015f8ad0f49SJohannes Weiner pud_t *pud; 4016f8ad0f49SJohannes Weiner pmd_t *pmd; 4017f8ad0f49SJohannes Weiner pte_t *ptep; 4018f8ad0f49SJohannes Weiner 4019f8ad0f49SJohannes Weiner pgd = pgd_offset(mm, address); 4020f8ad0f49SJohannes Weiner if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) 4021f8ad0f49SJohannes Weiner goto out; 4022f8ad0f49SJohannes Weiner 4023c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, address); 4024c2febafcSKirill A. Shutemov if (p4d_none(*p4d) || unlikely(p4d_bad(*p4d))) 4025c2febafcSKirill A. Shutemov goto out; 4026c2febafcSKirill A. Shutemov 4027c2febafcSKirill A. Shutemov pud = pud_offset(p4d, address); 4028f8ad0f49SJohannes Weiner if (pud_none(*pud) || unlikely(pud_bad(*pud))) 4029f8ad0f49SJohannes Weiner goto out; 4030f8ad0f49SJohannes Weiner 4031f8ad0f49SJohannes Weiner pmd = pmd_offset(pud, address); 4032f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 403309796395SRoss Zwisler 403409796395SRoss Zwisler if (pmd_huge(*pmd)) { 403509796395SRoss Zwisler if (!pmdpp) 4036f8ad0f49SJohannes Weiner goto out; 4037f8ad0f49SJohannes Weiner 4038a4d1a885SJérôme Glisse if (start && end) { 4039a4d1a885SJérôme Glisse *start = address & PMD_MASK; 4040a4d1a885SJérôme Glisse *end = *start + PMD_SIZE; 4041a4d1a885SJérôme Glisse mmu_notifier_invalidate_range_start(mm, *start, *end); 4042a4d1a885SJérôme Glisse } 404309796395SRoss Zwisler *ptlp = pmd_lock(mm, pmd); 404409796395SRoss Zwisler if (pmd_huge(*pmd)) { 404509796395SRoss Zwisler *pmdpp = pmd; 404609796395SRoss Zwisler return 0; 404709796395SRoss Zwisler } 404809796395SRoss Zwisler spin_unlock(*ptlp); 4049a4d1a885SJérôme Glisse if (start && end) 4050a4d1a885SJérôme Glisse mmu_notifier_invalidate_range_end(mm, *start, *end); 405109796395SRoss Zwisler } 405209796395SRoss Zwisler 405309796395SRoss Zwisler if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd))) 4054f8ad0f49SJohannes Weiner goto out; 4055f8ad0f49SJohannes Weiner 4056a4d1a885SJérôme Glisse if (start && end) { 4057a4d1a885SJérôme Glisse *start = address & PAGE_MASK; 4058a4d1a885SJérôme Glisse *end = *start + PAGE_SIZE; 4059a4d1a885SJérôme Glisse mmu_notifier_invalidate_range_start(mm, *start, *end); 4060a4d1a885SJérôme Glisse } 4061f8ad0f49SJohannes Weiner ptep = pte_offset_map_lock(mm, pmd, address, ptlp); 4062f8ad0f49SJohannes Weiner if (!pte_present(*ptep)) 4063f8ad0f49SJohannes Weiner goto unlock; 4064f8ad0f49SJohannes Weiner *ptepp = ptep; 4065f8ad0f49SJohannes Weiner return 0; 4066f8ad0f49SJohannes Weiner unlock: 4067f8ad0f49SJohannes Weiner pte_unmap_unlock(ptep, *ptlp); 4068a4d1a885SJérôme Glisse if (start && end) 4069a4d1a885SJérôme Glisse mmu_notifier_invalidate_range_end(mm, *start, *end); 4070f8ad0f49SJohannes Weiner out: 4071f8ad0f49SJohannes Weiner return -EINVAL; 4072f8ad0f49SJohannes Weiner } 4073f8ad0f49SJohannes Weiner 4074f729c8c9SRoss Zwisler static inline int follow_pte(struct mm_struct *mm, unsigned long address, 4075f729c8c9SRoss Zwisler pte_t **ptepp, spinlock_t **ptlp) 40761b36ba81SNamhyung Kim { 40771b36ba81SNamhyung Kim int res; 40781b36ba81SNamhyung Kim 40791b36ba81SNamhyung Kim /* (void) is needed to make gcc happy */ 40801b36ba81SNamhyung Kim (void) __cond_lock(*ptlp, 4081a4d1a885SJérôme Glisse !(res = __follow_pte_pmd(mm, address, NULL, NULL, 4082a4d1a885SJérôme Glisse ptepp, NULL, ptlp))); 40831b36ba81SNamhyung Kim return res; 40841b36ba81SNamhyung Kim } 40851b36ba81SNamhyung Kim 408609796395SRoss Zwisler int follow_pte_pmd(struct mm_struct *mm, unsigned long address, 4087a4d1a885SJérôme Glisse unsigned long *start, unsigned long *end, 408809796395SRoss Zwisler pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp) 408909796395SRoss Zwisler { 409009796395SRoss Zwisler int res; 409109796395SRoss Zwisler 409209796395SRoss Zwisler /* (void) is needed to make gcc happy */ 409309796395SRoss Zwisler (void) __cond_lock(*ptlp, 4094a4d1a885SJérôme Glisse !(res = __follow_pte_pmd(mm, address, start, end, 4095a4d1a885SJérôme Glisse ptepp, pmdpp, ptlp))); 409609796395SRoss Zwisler return res; 409709796395SRoss Zwisler } 409809796395SRoss Zwisler EXPORT_SYMBOL(follow_pte_pmd); 409909796395SRoss Zwisler 41003b6748e2SJohannes Weiner /** 41013b6748e2SJohannes Weiner * follow_pfn - look up PFN at a user virtual address 41023b6748e2SJohannes Weiner * @vma: memory mapping 41033b6748e2SJohannes Weiner * @address: user virtual address 41043b6748e2SJohannes Weiner * @pfn: location to store found PFN 41053b6748e2SJohannes Weiner * 41063b6748e2SJohannes Weiner * Only IO mappings and raw PFN mappings are allowed. 41073b6748e2SJohannes Weiner * 41083b6748e2SJohannes Weiner * Returns zero and the pfn at @pfn on success, -ve otherwise. 41093b6748e2SJohannes Weiner */ 41103b6748e2SJohannes Weiner int follow_pfn(struct vm_area_struct *vma, unsigned long address, 41113b6748e2SJohannes Weiner unsigned long *pfn) 41123b6748e2SJohannes Weiner { 41133b6748e2SJohannes Weiner int ret = -EINVAL; 41143b6748e2SJohannes Weiner spinlock_t *ptl; 41153b6748e2SJohannes Weiner pte_t *ptep; 41163b6748e2SJohannes Weiner 41173b6748e2SJohannes Weiner if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 41183b6748e2SJohannes Weiner return ret; 41193b6748e2SJohannes Weiner 41203b6748e2SJohannes Weiner ret = follow_pte(vma->vm_mm, address, &ptep, &ptl); 41213b6748e2SJohannes Weiner if (ret) 41223b6748e2SJohannes Weiner return ret; 41233b6748e2SJohannes Weiner *pfn = pte_pfn(*ptep); 41243b6748e2SJohannes Weiner pte_unmap_unlock(ptep, ptl); 41253b6748e2SJohannes Weiner return 0; 41263b6748e2SJohannes Weiner } 41273b6748e2SJohannes Weiner EXPORT_SYMBOL(follow_pfn); 41283b6748e2SJohannes Weiner 412928b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT 4130d87fe660Svenkatesh.pallipadi@intel.com int follow_phys(struct vm_area_struct *vma, 413128b2ee20SRik van Riel unsigned long address, unsigned int flags, 4132d87fe660Svenkatesh.pallipadi@intel.com unsigned long *prot, resource_size_t *phys) 413328b2ee20SRik van Riel { 413403668a4dSJohannes Weiner int ret = -EINVAL; 413528b2ee20SRik van Riel pte_t *ptep, pte; 413628b2ee20SRik van Riel spinlock_t *ptl; 413728b2ee20SRik van Riel 4138d87fe660Svenkatesh.pallipadi@intel.com if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 4139d87fe660Svenkatesh.pallipadi@intel.com goto out; 414028b2ee20SRik van Riel 414103668a4dSJohannes Weiner if (follow_pte(vma->vm_mm, address, &ptep, &ptl)) 4142d87fe660Svenkatesh.pallipadi@intel.com goto out; 414328b2ee20SRik van Riel pte = *ptep; 414403668a4dSJohannes Weiner 4145f6f37321SLinus Torvalds if ((flags & FOLL_WRITE) && !pte_write(pte)) 414628b2ee20SRik van Riel goto unlock; 414728b2ee20SRik van Riel 414828b2ee20SRik van Riel *prot = pgprot_val(pte_pgprot(pte)); 414903668a4dSJohannes Weiner *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT; 415028b2ee20SRik van Riel 415103668a4dSJohannes Weiner ret = 0; 415228b2ee20SRik van Riel unlock: 415328b2ee20SRik van Riel pte_unmap_unlock(ptep, ptl); 415428b2ee20SRik van Riel out: 4155d87fe660Svenkatesh.pallipadi@intel.com return ret; 415628b2ee20SRik van Riel } 415728b2ee20SRik van Riel 415828b2ee20SRik van Riel int generic_access_phys(struct vm_area_struct *vma, unsigned long addr, 415928b2ee20SRik van Riel void *buf, int len, int write) 416028b2ee20SRik van Riel { 416128b2ee20SRik van Riel resource_size_t phys_addr; 416228b2ee20SRik van Riel unsigned long prot = 0; 41632bc7273bSKOSAKI Motohiro void __iomem *maddr; 416428b2ee20SRik van Riel int offset = addr & (PAGE_SIZE-1); 416528b2ee20SRik van Riel 4166d87fe660Svenkatesh.pallipadi@intel.com if (follow_phys(vma, addr, write, &prot, &phys_addr)) 416728b2ee20SRik van Riel return -EINVAL; 416828b2ee20SRik van Riel 41699cb12d7bSGrazvydas Ignotas maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot); 417024eee1e4Sjie@chenjie6@huwei.com if (!maddr) 417124eee1e4Sjie@chenjie6@huwei.com return -ENOMEM; 417224eee1e4Sjie@chenjie6@huwei.com 417328b2ee20SRik van Riel if (write) 417428b2ee20SRik van Riel memcpy_toio(maddr + offset, buf, len); 417528b2ee20SRik van Riel else 417628b2ee20SRik van Riel memcpy_fromio(buf, maddr + offset, len); 417728b2ee20SRik van Riel iounmap(maddr); 417828b2ee20SRik van Riel 417928b2ee20SRik van Riel return len; 418028b2ee20SRik van Riel } 41815a73633eSUwe Kleine-König EXPORT_SYMBOL_GPL(generic_access_phys); 418228b2ee20SRik van Riel #endif 418328b2ee20SRik van Riel 41840ec76a11SDavid Howells /* 4185206cb636SStephen Wilson * Access another process' address space as given in mm. If non-NULL, use the 4186206cb636SStephen Wilson * given task for page fault accounting. 41870ec76a11SDavid Howells */ 418884d77d3fSEric W. Biederman int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm, 4189442486ecSLorenzo Stoakes unsigned long addr, void *buf, int len, unsigned int gup_flags) 41900ec76a11SDavid Howells { 41910ec76a11SDavid Howells struct vm_area_struct *vma; 41920ec76a11SDavid Howells void *old_buf = buf; 4193442486ecSLorenzo Stoakes int write = gup_flags & FOLL_WRITE; 41940ec76a11SDavid Howells 41950ec76a11SDavid Howells down_read(&mm->mmap_sem); 4196183ff22bSSimon Arlott /* ignore errors, just check how much was successfully transferred */ 41970ec76a11SDavid Howells while (len) { 41980ec76a11SDavid Howells int bytes, ret, offset; 41990ec76a11SDavid Howells void *maddr; 420028b2ee20SRik van Riel struct page *page = NULL; 42010ec76a11SDavid Howells 42021e987790SDave Hansen ret = get_user_pages_remote(tsk, mm, addr, 1, 42035b56d49fSLorenzo Stoakes gup_flags, &page, &vma, NULL); 420428b2ee20SRik van Riel if (ret <= 0) { 4205dbffcd03SRik van Riel #ifndef CONFIG_HAVE_IOREMAP_PROT 4206dbffcd03SRik van Riel break; 4207dbffcd03SRik van Riel #else 420828b2ee20SRik van Riel /* 420928b2ee20SRik van Riel * Check if this is a VM_IO | VM_PFNMAP VMA, which 421028b2ee20SRik van Riel * we can access using slightly different code. 421128b2ee20SRik van Riel */ 421228b2ee20SRik van Riel vma = find_vma(mm, addr); 4213fe936dfcSMichael Ellerman if (!vma || vma->vm_start > addr) 42140ec76a11SDavid Howells break; 421528b2ee20SRik van Riel if (vma->vm_ops && vma->vm_ops->access) 421628b2ee20SRik van Riel ret = vma->vm_ops->access(vma, addr, buf, 421728b2ee20SRik van Riel len, write); 421828b2ee20SRik van Riel if (ret <= 0) 421928b2ee20SRik van Riel break; 422028b2ee20SRik van Riel bytes = ret; 4221dbffcd03SRik van Riel #endif 422228b2ee20SRik van Riel } else { 42230ec76a11SDavid Howells bytes = len; 42240ec76a11SDavid Howells offset = addr & (PAGE_SIZE-1); 42250ec76a11SDavid Howells if (bytes > PAGE_SIZE-offset) 42260ec76a11SDavid Howells bytes = PAGE_SIZE-offset; 42270ec76a11SDavid Howells 42280ec76a11SDavid Howells maddr = kmap(page); 42290ec76a11SDavid Howells if (write) { 42300ec76a11SDavid Howells copy_to_user_page(vma, page, addr, 42310ec76a11SDavid Howells maddr + offset, buf, bytes); 42320ec76a11SDavid Howells set_page_dirty_lock(page); 42330ec76a11SDavid Howells } else { 42340ec76a11SDavid Howells copy_from_user_page(vma, page, addr, 42350ec76a11SDavid Howells buf, maddr + offset, bytes); 42360ec76a11SDavid Howells } 42370ec76a11SDavid Howells kunmap(page); 423809cbfeafSKirill A. Shutemov put_page(page); 423928b2ee20SRik van Riel } 42400ec76a11SDavid Howells len -= bytes; 42410ec76a11SDavid Howells buf += bytes; 42420ec76a11SDavid Howells addr += bytes; 42430ec76a11SDavid Howells } 42440ec76a11SDavid Howells up_read(&mm->mmap_sem); 42450ec76a11SDavid Howells 42460ec76a11SDavid Howells return buf - old_buf; 42470ec76a11SDavid Howells } 424803252919SAndi Kleen 42495ddd36b9SStephen Wilson /** 4250ae91dbfcSRandy Dunlap * access_remote_vm - access another process' address space 42515ddd36b9SStephen Wilson * @mm: the mm_struct of the target address space 42525ddd36b9SStephen Wilson * @addr: start address to access 42535ddd36b9SStephen Wilson * @buf: source or destination buffer 42545ddd36b9SStephen Wilson * @len: number of bytes to transfer 42556347e8d5SLorenzo Stoakes * @gup_flags: flags modifying lookup behaviour 42565ddd36b9SStephen Wilson * 42575ddd36b9SStephen Wilson * The caller must hold a reference on @mm. 42585ddd36b9SStephen Wilson */ 42595ddd36b9SStephen Wilson int access_remote_vm(struct mm_struct *mm, unsigned long addr, 42606347e8d5SLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 42615ddd36b9SStephen Wilson { 42626347e8d5SLorenzo Stoakes return __access_remote_vm(NULL, mm, addr, buf, len, gup_flags); 42635ddd36b9SStephen Wilson } 42645ddd36b9SStephen Wilson 426503252919SAndi Kleen /* 4266206cb636SStephen Wilson * Access another process' address space. 4267206cb636SStephen Wilson * Source/target buffer must be kernel space, 4268206cb636SStephen Wilson * Do not walk the page table directly, use get_user_pages 4269206cb636SStephen Wilson */ 4270206cb636SStephen Wilson int access_process_vm(struct task_struct *tsk, unsigned long addr, 4271f307ab6dSLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 4272206cb636SStephen Wilson { 4273206cb636SStephen Wilson struct mm_struct *mm; 4274206cb636SStephen Wilson int ret; 4275206cb636SStephen Wilson 4276206cb636SStephen Wilson mm = get_task_mm(tsk); 4277206cb636SStephen Wilson if (!mm) 4278206cb636SStephen Wilson return 0; 4279206cb636SStephen Wilson 4280f307ab6dSLorenzo Stoakes ret = __access_remote_vm(tsk, mm, addr, buf, len, gup_flags); 4281442486ecSLorenzo Stoakes 4282206cb636SStephen Wilson mmput(mm); 4283206cb636SStephen Wilson 4284206cb636SStephen Wilson return ret; 4285206cb636SStephen Wilson } 4286fcd35857SCatalin Marinas EXPORT_SYMBOL_GPL(access_process_vm); 4287206cb636SStephen Wilson 428803252919SAndi Kleen /* 428903252919SAndi Kleen * Print the name of a VMA. 429003252919SAndi Kleen */ 429103252919SAndi Kleen void print_vma_addr(char *prefix, unsigned long ip) 429203252919SAndi Kleen { 429303252919SAndi Kleen struct mm_struct *mm = current->mm; 429403252919SAndi Kleen struct vm_area_struct *vma; 429503252919SAndi Kleen 4296e8bff74aSIngo Molnar /* 42970a7f682dSMichal Hocko * we might be running from an atomic context so we cannot sleep 4298e8bff74aSIngo Molnar */ 42990a7f682dSMichal Hocko if (!down_read_trylock(&mm->mmap_sem)) 4300e8bff74aSIngo Molnar return; 4301e8bff74aSIngo Molnar 430203252919SAndi Kleen vma = find_vma(mm, ip); 430303252919SAndi Kleen if (vma && vma->vm_file) { 430403252919SAndi Kleen struct file *f = vma->vm_file; 43050a7f682dSMichal Hocko char *buf = (char *)__get_free_page(GFP_NOWAIT); 430603252919SAndi Kleen if (buf) { 43072fbc57c5SAndy Shevchenko char *p; 430803252919SAndi Kleen 43099bf39ab2SMiklos Szeredi p = file_path(f, buf, PAGE_SIZE); 431003252919SAndi Kleen if (IS_ERR(p)) 431103252919SAndi Kleen p = "?"; 43122fbc57c5SAndy Shevchenko printk("%s%s[%lx+%lx]", prefix, kbasename(p), 431303252919SAndi Kleen vma->vm_start, 431403252919SAndi Kleen vma->vm_end - vma->vm_start); 431503252919SAndi Kleen free_page((unsigned long)buf); 431603252919SAndi Kleen } 431703252919SAndi Kleen } 431851a07e50SJeff Liu up_read(&mm->mmap_sem); 431903252919SAndi Kleen } 43203ee1afa3SNick Piggin 4321662bbcb2SMichael S. Tsirkin #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP) 43229ec23531SDavid Hildenbrand void __might_fault(const char *file, int line) 43233ee1afa3SNick Piggin { 432495156f00SPeter Zijlstra /* 432595156f00SPeter Zijlstra * Some code (nfs/sunrpc) uses socket ops on kernel memory while 432695156f00SPeter Zijlstra * holding the mmap_sem, this is safe because kernel memory doesn't 432795156f00SPeter Zijlstra * get paged out, therefore we'll never actually fault, and the 432895156f00SPeter Zijlstra * below annotations will generate false positives. 432995156f00SPeter Zijlstra */ 4330db68ce10SAl Viro if (uaccess_kernel()) 433195156f00SPeter Zijlstra return; 43329ec23531SDavid Hildenbrand if (pagefault_disabled()) 4333662bbcb2SMichael S. Tsirkin return; 43349ec23531SDavid Hildenbrand __might_sleep(file, line, 0); 43359ec23531SDavid Hildenbrand #if defined(CONFIG_DEBUG_ATOMIC_SLEEP) 4336662bbcb2SMichael S. Tsirkin if (current->mm) 43373ee1afa3SNick Piggin might_lock_read(¤t->mm->mmap_sem); 43389ec23531SDavid Hildenbrand #endif 43393ee1afa3SNick Piggin } 43409ec23531SDavid Hildenbrand EXPORT_SYMBOL(__might_fault); 43413ee1afa3SNick Piggin #endif 434247ad8475SAndrea Arcangeli 434347ad8475SAndrea Arcangeli #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS) 4344c6ddfb6cSHuang Ying /* 4345c6ddfb6cSHuang Ying * Process all subpages of the specified huge page with the specified 4346c6ddfb6cSHuang Ying * operation. The target subpage will be processed last to keep its 4347c6ddfb6cSHuang Ying * cache lines hot. 4348c6ddfb6cSHuang Ying */ 4349c6ddfb6cSHuang Ying static inline void process_huge_page( 4350c6ddfb6cSHuang Ying unsigned long addr_hint, unsigned int pages_per_huge_page, 4351c6ddfb6cSHuang Ying void (*process_subpage)(unsigned long addr, int idx, void *arg), 4352c6ddfb6cSHuang Ying void *arg) 4353c6ddfb6cSHuang Ying { 4354c6ddfb6cSHuang Ying int i, n, base, l; 4355c6ddfb6cSHuang Ying unsigned long addr = addr_hint & 4356c6ddfb6cSHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 4357c6ddfb6cSHuang Ying 4358c6ddfb6cSHuang Ying /* Process target subpage last to keep its cache lines hot */ 4359c6ddfb6cSHuang Ying might_sleep(); 4360c6ddfb6cSHuang Ying n = (addr_hint - addr) / PAGE_SIZE; 4361c6ddfb6cSHuang Ying if (2 * n <= pages_per_huge_page) { 4362c6ddfb6cSHuang Ying /* If target subpage in first half of huge page */ 4363c6ddfb6cSHuang Ying base = 0; 4364c6ddfb6cSHuang Ying l = n; 4365c6ddfb6cSHuang Ying /* Process subpages at the end of huge page */ 4366c6ddfb6cSHuang Ying for (i = pages_per_huge_page - 1; i >= 2 * n; i--) { 4367c6ddfb6cSHuang Ying cond_resched(); 4368c6ddfb6cSHuang Ying process_subpage(addr + i * PAGE_SIZE, i, arg); 4369c6ddfb6cSHuang Ying } 4370c6ddfb6cSHuang Ying } else { 4371c6ddfb6cSHuang Ying /* If target subpage in second half of huge page */ 4372c6ddfb6cSHuang Ying base = pages_per_huge_page - 2 * (pages_per_huge_page - n); 4373c6ddfb6cSHuang Ying l = pages_per_huge_page - n; 4374c6ddfb6cSHuang Ying /* Process subpages at the begin of huge page */ 4375c6ddfb6cSHuang Ying for (i = 0; i < base; i++) { 4376c6ddfb6cSHuang Ying cond_resched(); 4377c6ddfb6cSHuang Ying process_subpage(addr + i * PAGE_SIZE, i, arg); 4378c6ddfb6cSHuang Ying } 4379c6ddfb6cSHuang Ying } 4380c6ddfb6cSHuang Ying /* 4381c6ddfb6cSHuang Ying * Process remaining subpages in left-right-left-right pattern 4382c6ddfb6cSHuang Ying * towards the target subpage 4383c6ddfb6cSHuang Ying */ 4384c6ddfb6cSHuang Ying for (i = 0; i < l; i++) { 4385c6ddfb6cSHuang Ying int left_idx = base + i; 4386c6ddfb6cSHuang Ying int right_idx = base + 2 * l - 1 - i; 4387c6ddfb6cSHuang Ying 4388c6ddfb6cSHuang Ying cond_resched(); 4389c6ddfb6cSHuang Ying process_subpage(addr + left_idx * PAGE_SIZE, left_idx, arg); 4390c6ddfb6cSHuang Ying cond_resched(); 4391c6ddfb6cSHuang Ying process_subpage(addr + right_idx * PAGE_SIZE, right_idx, arg); 4392c6ddfb6cSHuang Ying } 4393c6ddfb6cSHuang Ying } 4394c6ddfb6cSHuang Ying 439547ad8475SAndrea Arcangeli static void clear_gigantic_page(struct page *page, 439647ad8475SAndrea Arcangeli unsigned long addr, 439747ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 439847ad8475SAndrea Arcangeli { 439947ad8475SAndrea Arcangeli int i; 440047ad8475SAndrea Arcangeli struct page *p = page; 440147ad8475SAndrea Arcangeli 440247ad8475SAndrea Arcangeli might_sleep(); 440347ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; 440447ad8475SAndrea Arcangeli i++, p = mem_map_next(p, page, i)) { 440547ad8475SAndrea Arcangeli cond_resched(); 440647ad8475SAndrea Arcangeli clear_user_highpage(p, addr + i * PAGE_SIZE); 440747ad8475SAndrea Arcangeli } 440847ad8475SAndrea Arcangeli } 4409c6ddfb6cSHuang Ying 4410c6ddfb6cSHuang Ying static void clear_subpage(unsigned long addr, int idx, void *arg) 4411c6ddfb6cSHuang Ying { 4412c6ddfb6cSHuang Ying struct page *page = arg; 4413c6ddfb6cSHuang Ying 4414c6ddfb6cSHuang Ying clear_user_highpage(page + idx, addr); 4415c6ddfb6cSHuang Ying } 4416c6ddfb6cSHuang Ying 441747ad8475SAndrea Arcangeli void clear_huge_page(struct page *page, 4418c79b57e4SHuang Ying unsigned long addr_hint, unsigned int pages_per_huge_page) 441947ad8475SAndrea Arcangeli { 4420c79b57e4SHuang Ying unsigned long addr = addr_hint & 4421c79b57e4SHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 442247ad8475SAndrea Arcangeli 442347ad8475SAndrea Arcangeli if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { 442447ad8475SAndrea Arcangeli clear_gigantic_page(page, addr, pages_per_huge_page); 442547ad8475SAndrea Arcangeli return; 442647ad8475SAndrea Arcangeli } 442747ad8475SAndrea Arcangeli 4428c6ddfb6cSHuang Ying process_huge_page(addr_hint, pages_per_huge_page, clear_subpage, page); 442947ad8475SAndrea Arcangeli } 443047ad8475SAndrea Arcangeli 443147ad8475SAndrea Arcangeli static void copy_user_gigantic_page(struct page *dst, struct page *src, 443247ad8475SAndrea Arcangeli unsigned long addr, 443347ad8475SAndrea Arcangeli struct vm_area_struct *vma, 443447ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 443547ad8475SAndrea Arcangeli { 443647ad8475SAndrea Arcangeli int i; 443747ad8475SAndrea Arcangeli struct page *dst_base = dst; 443847ad8475SAndrea Arcangeli struct page *src_base = src; 443947ad8475SAndrea Arcangeli 444047ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; ) { 444147ad8475SAndrea Arcangeli cond_resched(); 444247ad8475SAndrea Arcangeli copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma); 444347ad8475SAndrea Arcangeli 444447ad8475SAndrea Arcangeli i++; 444547ad8475SAndrea Arcangeli dst = mem_map_next(dst, dst_base, i); 444647ad8475SAndrea Arcangeli src = mem_map_next(src, src_base, i); 444747ad8475SAndrea Arcangeli } 444847ad8475SAndrea Arcangeli } 444947ad8475SAndrea Arcangeli 4450c9f4cd71SHuang Ying struct copy_subpage_arg { 4451c9f4cd71SHuang Ying struct page *dst; 4452c9f4cd71SHuang Ying struct page *src; 4453c9f4cd71SHuang Ying struct vm_area_struct *vma; 4454c9f4cd71SHuang Ying }; 4455c9f4cd71SHuang Ying 4456c9f4cd71SHuang Ying static void copy_subpage(unsigned long addr, int idx, void *arg) 4457c9f4cd71SHuang Ying { 4458c9f4cd71SHuang Ying struct copy_subpage_arg *copy_arg = arg; 4459c9f4cd71SHuang Ying 4460c9f4cd71SHuang Ying copy_user_highpage(copy_arg->dst + idx, copy_arg->src + idx, 4461c9f4cd71SHuang Ying addr, copy_arg->vma); 4462c9f4cd71SHuang Ying } 4463c9f4cd71SHuang Ying 446447ad8475SAndrea Arcangeli void copy_user_huge_page(struct page *dst, struct page *src, 4465c9f4cd71SHuang Ying unsigned long addr_hint, struct vm_area_struct *vma, 446647ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 446747ad8475SAndrea Arcangeli { 4468c9f4cd71SHuang Ying unsigned long addr = addr_hint & 4469c9f4cd71SHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 4470c9f4cd71SHuang Ying struct copy_subpage_arg arg = { 4471c9f4cd71SHuang Ying .dst = dst, 4472c9f4cd71SHuang Ying .src = src, 4473c9f4cd71SHuang Ying .vma = vma, 4474c9f4cd71SHuang Ying }; 447547ad8475SAndrea Arcangeli 447647ad8475SAndrea Arcangeli if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { 447747ad8475SAndrea Arcangeli copy_user_gigantic_page(dst, src, addr, vma, 447847ad8475SAndrea Arcangeli pages_per_huge_page); 447947ad8475SAndrea Arcangeli return; 448047ad8475SAndrea Arcangeli } 448147ad8475SAndrea Arcangeli 4482c9f4cd71SHuang Ying process_huge_page(addr_hint, pages_per_huge_page, copy_subpage, &arg); 448347ad8475SAndrea Arcangeli } 4484fa4d75c1SMike Kravetz 4485fa4d75c1SMike Kravetz long copy_huge_page_from_user(struct page *dst_page, 4486fa4d75c1SMike Kravetz const void __user *usr_src, 4487810a56b9SMike Kravetz unsigned int pages_per_huge_page, 4488810a56b9SMike Kravetz bool allow_pagefault) 4489fa4d75c1SMike Kravetz { 4490fa4d75c1SMike Kravetz void *src = (void *)usr_src; 4491fa4d75c1SMike Kravetz void *page_kaddr; 4492fa4d75c1SMike Kravetz unsigned long i, rc = 0; 4493fa4d75c1SMike Kravetz unsigned long ret_val = pages_per_huge_page * PAGE_SIZE; 4494fa4d75c1SMike Kravetz 4495fa4d75c1SMike Kravetz for (i = 0; i < pages_per_huge_page; i++) { 4496810a56b9SMike Kravetz if (allow_pagefault) 4497810a56b9SMike Kravetz page_kaddr = kmap(dst_page + i); 4498810a56b9SMike Kravetz else 4499fa4d75c1SMike Kravetz page_kaddr = kmap_atomic(dst_page + i); 4500fa4d75c1SMike Kravetz rc = copy_from_user(page_kaddr, 4501fa4d75c1SMike Kravetz (const void __user *)(src + i * PAGE_SIZE), 4502fa4d75c1SMike Kravetz PAGE_SIZE); 4503810a56b9SMike Kravetz if (allow_pagefault) 4504810a56b9SMike Kravetz kunmap(dst_page + i); 4505810a56b9SMike Kravetz else 4506fa4d75c1SMike Kravetz kunmap_atomic(page_kaddr); 4507fa4d75c1SMike Kravetz 4508fa4d75c1SMike Kravetz ret_val -= (PAGE_SIZE - rc); 4509fa4d75c1SMike Kravetz if (rc) 4510fa4d75c1SMike Kravetz break; 4511fa4d75c1SMike Kravetz 4512fa4d75c1SMike Kravetz cond_resched(); 4513fa4d75c1SMike Kravetz } 4514fa4d75c1SMike Kravetz return ret_val; 4515fa4d75c1SMike Kravetz } 451647ad8475SAndrea Arcangeli #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */ 451749076ec2SKirill A. Shutemov 451840b64acdSOlof Johansson #if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS 4519b35f1819SKirill A. Shutemov 4520b35f1819SKirill A. Shutemov static struct kmem_cache *page_ptl_cachep; 4521b35f1819SKirill A. Shutemov 4522b35f1819SKirill A. Shutemov void __init ptlock_cache_init(void) 4523b35f1819SKirill A. Shutemov { 4524b35f1819SKirill A. Shutemov page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0, 4525b35f1819SKirill A. Shutemov SLAB_PANIC, NULL); 4526b35f1819SKirill A. Shutemov } 4527b35f1819SKirill A. Shutemov 4528539edb58SPeter Zijlstra bool ptlock_alloc(struct page *page) 452949076ec2SKirill A. Shutemov { 453049076ec2SKirill A. Shutemov spinlock_t *ptl; 453149076ec2SKirill A. Shutemov 4532b35f1819SKirill A. Shutemov ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL); 453349076ec2SKirill A. Shutemov if (!ptl) 453449076ec2SKirill A. Shutemov return false; 4535539edb58SPeter Zijlstra page->ptl = ptl; 453649076ec2SKirill A. Shutemov return true; 453749076ec2SKirill A. Shutemov } 453849076ec2SKirill A. Shutemov 4539539edb58SPeter Zijlstra void ptlock_free(struct page *page) 454049076ec2SKirill A. Shutemov { 4541b35f1819SKirill A. Shutemov kmem_cache_free(page_ptl_cachep, page->ptl); 454249076ec2SKirill A. Shutemov } 454349076ec2SKirill A. Shutemov #endif 4544