1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/mm/memory.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 81da177e4SLinus Torvalds /* 91da177e4SLinus Torvalds * demand-loading started 01.12.91 - seems it is high on the list of 101da177e4SLinus Torvalds * things wanted, and it should be easy to implement. - Linus 111da177e4SLinus Torvalds */ 121da177e4SLinus Torvalds 131da177e4SLinus Torvalds /* 141da177e4SLinus Torvalds * Ok, demand-loading was easy, shared pages a little bit tricker. Shared 151da177e4SLinus Torvalds * pages started 02.12.91, seems to work. - Linus. 161da177e4SLinus Torvalds * 171da177e4SLinus Torvalds * Tested sharing by executing about 30 /bin/sh: under the old kernel it 181da177e4SLinus Torvalds * would have taken more than the 6M I have free, but it worked well as 191da177e4SLinus Torvalds * far as I could see. 201da177e4SLinus Torvalds * 211da177e4SLinus Torvalds * Also corrected some "invalidate()"s - I wasn't doing enough of them. 221da177e4SLinus Torvalds */ 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds /* 251da177e4SLinus Torvalds * Real VM (paging to/from disk) started 18.12.91. Much more work and 261da177e4SLinus Torvalds * thought has to go into this. Oh, well.. 271da177e4SLinus Torvalds * 19.12.91 - works, somewhat. Sometimes I get faults, don't know why. 281da177e4SLinus Torvalds * Found it. Everything seems to work now. 291da177e4SLinus Torvalds * 20.12.91 - Ok, making the swap-device changeable like the root. 301da177e4SLinus Torvalds */ 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds /* 331da177e4SLinus Torvalds * 05.04.94 - Multi-page memory management added for v1.1. 341da177e4SLinus Torvalds * Idea by Alex Bligh (alex@cconcepts.co.uk) 351da177e4SLinus Torvalds * 361da177e4SLinus Torvalds * 16.07.99 - Support of BIGMEM added by Gerhard Wichert, Siemens AG 371da177e4SLinus Torvalds * (Gerhard.Wichert@pdb.siemens.de) 381da177e4SLinus Torvalds * 391da177e4SLinus Torvalds * Aug/Sep 2004 Changed to four level page tables (Andi Kleen) 401da177e4SLinus Torvalds */ 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds #include <linux/kernel_stat.h> 431da177e4SLinus Torvalds #include <linux/mm.h> 446e84f315SIngo Molnar #include <linux/sched/mm.h> 45f7ccbae4SIngo Molnar #include <linux/sched/coredump.h> 466a3827d7SIngo Molnar #include <linux/sched/numa_balancing.h> 4729930025SIngo Molnar #include <linux/sched/task.h> 481da177e4SLinus Torvalds #include <linux/hugetlb.h> 491da177e4SLinus Torvalds #include <linux/mman.h> 501da177e4SLinus Torvalds #include <linux/swap.h> 511da177e4SLinus Torvalds #include <linux/highmem.h> 521da177e4SLinus Torvalds #include <linux/pagemap.h> 535042db43SJérôme Glisse #include <linux/memremap.h> 549a840895SHugh Dickins #include <linux/ksm.h> 551da177e4SLinus Torvalds #include <linux/rmap.h> 56b95f1b31SPaul Gortmaker #include <linux/export.h> 570ff92245SShailabh Nagar #include <linux/delayacct.h> 581da177e4SLinus Torvalds #include <linux/init.h> 5901c8f1c4SDan Williams #include <linux/pfn_t.h> 60edc79b2aSPeter Zijlstra #include <linux/writeback.h> 618a9f3ccdSBalbir Singh #include <linux/memcontrol.h> 62cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h> 633dc14741SHugh Dickins #include <linux/swapops.h> 643dc14741SHugh Dickins #include <linux/elf.h> 655a0e3ad6STejun Heo #include <linux/gfp.h> 664daae3b4SMel Gorman #include <linux/migrate.h> 672fbc57c5SAndy Shevchenko #include <linux/string.h> 680abdd7a8SDan Williams #include <linux/dma-debug.h> 691592eef0SKirill A. Shutemov #include <linux/debugfs.h> 706b251fc9SAndrea Arcangeli #include <linux/userfaultfd_k.h> 71bc2466e4SJan Kara #include <linux/dax.h> 726b31d595SMichal Hocko #include <linux/oom.h> 7398fa15f3SAnshuman Khandual #include <linux/numa.h> 741da177e4SLinus Torvalds 756952b61dSAlexey Dobriyan #include <asm/io.h> 7633a709b2SDave Hansen #include <asm/mmu_context.h> 771da177e4SLinus Torvalds #include <asm/pgalloc.h> 787c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 791da177e4SLinus Torvalds #include <asm/tlb.h> 801da177e4SLinus Torvalds #include <asm/tlbflush.h> 811da177e4SLinus Torvalds #include <asm/pgtable.h> 821da177e4SLinus Torvalds 8342b77728SJan Beulich #include "internal.h" 8442b77728SJan Beulich 85af27d940SArnd Bergmann #if defined(LAST_CPUPID_NOT_IN_PAGE_FLAGS) && !defined(CONFIG_COMPILE_TEST) 8690572890SPeter Zijlstra #warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid. 8775980e97SPeter Zijlstra #endif 8875980e97SPeter Zijlstra 89d41dee36SAndy Whitcroft #ifndef CONFIG_NEED_MULTIPLE_NODES 901da177e4SLinus Torvalds /* use the per-pgdat data instead for discontigmem - mbligh */ 911da177e4SLinus Torvalds unsigned long max_mapnr; 921da177e4SLinus Torvalds EXPORT_SYMBOL(max_mapnr); 93166f61b9STobin C Harding 94166f61b9STobin C Harding struct page *mem_map; 951da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map); 961da177e4SLinus Torvalds #endif 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds /* 991da177e4SLinus Torvalds * A number of key systems in x86 including ioremap() rely on the assumption 1001da177e4SLinus Torvalds * that high_memory defines the upper bound on direct map memory, then end 1011da177e4SLinus Torvalds * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and 1021da177e4SLinus Torvalds * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL 1031da177e4SLinus Torvalds * and ZONE_HIGHMEM. 1041da177e4SLinus Torvalds */ 1051da177e4SLinus Torvalds void *high_memory; 1061da177e4SLinus Torvalds EXPORT_SYMBOL(high_memory); 1071da177e4SLinus Torvalds 10832a93233SIngo Molnar /* 10932a93233SIngo Molnar * Randomize the address space (stacks, mmaps, brk, etc.). 11032a93233SIngo Molnar * 11132a93233SIngo Molnar * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization, 11232a93233SIngo Molnar * as ancient (libc5 based) binaries can segfault. ) 11332a93233SIngo Molnar */ 11432a93233SIngo Molnar int randomize_va_space __read_mostly = 11532a93233SIngo Molnar #ifdef CONFIG_COMPAT_BRK 11632a93233SIngo Molnar 1; 11732a93233SIngo Molnar #else 11832a93233SIngo Molnar 2; 11932a93233SIngo Molnar #endif 120a62eaf15SAndi Kleen 121a62eaf15SAndi Kleen static int __init disable_randmaps(char *s) 122a62eaf15SAndi Kleen { 123a62eaf15SAndi Kleen randomize_va_space = 0; 1249b41046cSOGAWA Hirofumi return 1; 125a62eaf15SAndi Kleen } 126a62eaf15SAndi Kleen __setup("norandmaps", disable_randmaps); 127a62eaf15SAndi Kleen 12862eede62SHugh Dickins unsigned long zero_pfn __read_mostly; 1290b70068eSArd Biesheuvel EXPORT_SYMBOL(zero_pfn); 1300b70068eSArd Biesheuvel 131166f61b9STobin C Harding unsigned long highest_memmap_pfn __read_mostly; 132166f61b9STobin C Harding 133a13ea5b7SHugh Dickins /* 134a13ea5b7SHugh Dickins * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init() 135a13ea5b7SHugh Dickins */ 136a13ea5b7SHugh Dickins static int __init init_zero_pfn(void) 137a13ea5b7SHugh Dickins { 138a13ea5b7SHugh Dickins zero_pfn = page_to_pfn(ZERO_PAGE(0)); 139a13ea5b7SHugh Dickins return 0; 140a13ea5b7SHugh Dickins } 141a13ea5b7SHugh Dickins core_initcall(init_zero_pfn); 142a62eaf15SAndi Kleen 143d559db08SKAMEZAWA Hiroyuki 14434e55232SKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING) 14534e55232SKAMEZAWA Hiroyuki 146ea48cf78SDavid Rientjes void sync_mm_rss(struct mm_struct *mm) 14734e55232SKAMEZAWA Hiroyuki { 14834e55232SKAMEZAWA Hiroyuki int i; 14934e55232SKAMEZAWA Hiroyuki 15034e55232SKAMEZAWA Hiroyuki for (i = 0; i < NR_MM_COUNTERS; i++) { 15105af2e10SDavid Rientjes if (current->rss_stat.count[i]) { 15205af2e10SDavid Rientjes add_mm_counter(mm, i, current->rss_stat.count[i]); 15305af2e10SDavid Rientjes current->rss_stat.count[i] = 0; 15434e55232SKAMEZAWA Hiroyuki } 15534e55232SKAMEZAWA Hiroyuki } 15605af2e10SDavid Rientjes current->rss_stat.events = 0; 15734e55232SKAMEZAWA Hiroyuki } 15834e55232SKAMEZAWA Hiroyuki 15934e55232SKAMEZAWA Hiroyuki static void add_mm_counter_fast(struct mm_struct *mm, int member, int val) 16034e55232SKAMEZAWA Hiroyuki { 16134e55232SKAMEZAWA Hiroyuki struct task_struct *task = current; 16234e55232SKAMEZAWA Hiroyuki 16334e55232SKAMEZAWA Hiroyuki if (likely(task->mm == mm)) 16434e55232SKAMEZAWA Hiroyuki task->rss_stat.count[member] += val; 16534e55232SKAMEZAWA Hiroyuki else 16634e55232SKAMEZAWA Hiroyuki add_mm_counter(mm, member, val); 16734e55232SKAMEZAWA Hiroyuki } 16834e55232SKAMEZAWA Hiroyuki #define inc_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, 1) 16934e55232SKAMEZAWA Hiroyuki #define dec_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, -1) 17034e55232SKAMEZAWA Hiroyuki 17134e55232SKAMEZAWA Hiroyuki /* sync counter once per 64 page faults */ 17234e55232SKAMEZAWA Hiroyuki #define TASK_RSS_EVENTS_THRESH (64) 17334e55232SKAMEZAWA Hiroyuki static void check_sync_rss_stat(struct task_struct *task) 17434e55232SKAMEZAWA Hiroyuki { 17534e55232SKAMEZAWA Hiroyuki if (unlikely(task != current)) 17634e55232SKAMEZAWA Hiroyuki return; 17734e55232SKAMEZAWA Hiroyuki if (unlikely(task->rss_stat.events++ > TASK_RSS_EVENTS_THRESH)) 178ea48cf78SDavid Rientjes sync_mm_rss(task->mm); 17934e55232SKAMEZAWA Hiroyuki } 1809547d01bSPeter Zijlstra #else /* SPLIT_RSS_COUNTING */ 18134e55232SKAMEZAWA Hiroyuki 18234e55232SKAMEZAWA Hiroyuki #define inc_mm_counter_fast(mm, member) inc_mm_counter(mm, member) 18334e55232SKAMEZAWA Hiroyuki #define dec_mm_counter_fast(mm, member) dec_mm_counter(mm, member) 18434e55232SKAMEZAWA Hiroyuki 18534e55232SKAMEZAWA Hiroyuki static void check_sync_rss_stat(struct task_struct *task) 18634e55232SKAMEZAWA Hiroyuki { 18734e55232SKAMEZAWA Hiroyuki } 18834e55232SKAMEZAWA Hiroyuki 1899547d01bSPeter Zijlstra #endif /* SPLIT_RSS_COUNTING */ 1909547d01bSPeter Zijlstra 1911da177e4SLinus Torvalds /* 1921da177e4SLinus Torvalds * Note: this doesn't free the actual pages themselves. That 1931da177e4SLinus Torvalds * has been handled earlier when unmapping all the memory regions. 1941da177e4SLinus Torvalds */ 1959e1b32caSBenjamin Herrenschmidt static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd, 1969e1b32caSBenjamin Herrenschmidt unsigned long addr) 1971da177e4SLinus Torvalds { 1982f569afdSMartin Schwidefsky pgtable_t token = pmd_pgtable(*pmd); 1991da177e4SLinus Torvalds pmd_clear(pmd); 2009e1b32caSBenjamin Herrenschmidt pte_free_tlb(tlb, token, addr); 201c4812909SKirill A. Shutemov mm_dec_nr_ptes(tlb->mm); 2021da177e4SLinus Torvalds } 2031da177e4SLinus Torvalds 204e0da382cSHugh Dickins static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud, 205e0da382cSHugh Dickins unsigned long addr, unsigned long end, 206e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 2071da177e4SLinus Torvalds { 2081da177e4SLinus Torvalds pmd_t *pmd; 2091da177e4SLinus Torvalds unsigned long next; 210e0da382cSHugh Dickins unsigned long start; 2111da177e4SLinus Torvalds 212e0da382cSHugh Dickins start = addr; 2131da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 2141da177e4SLinus Torvalds do { 2151da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 2161da177e4SLinus Torvalds if (pmd_none_or_clear_bad(pmd)) 2171da177e4SLinus Torvalds continue; 2189e1b32caSBenjamin Herrenschmidt free_pte_range(tlb, pmd, addr); 2191da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 2201da177e4SLinus Torvalds 221e0da382cSHugh Dickins start &= PUD_MASK; 222e0da382cSHugh Dickins if (start < floor) 223e0da382cSHugh Dickins return; 224e0da382cSHugh Dickins if (ceiling) { 225e0da382cSHugh Dickins ceiling &= PUD_MASK; 226e0da382cSHugh Dickins if (!ceiling) 227e0da382cSHugh Dickins return; 2281da177e4SLinus Torvalds } 229e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 230e0da382cSHugh Dickins return; 231e0da382cSHugh Dickins 232e0da382cSHugh Dickins pmd = pmd_offset(pud, start); 233e0da382cSHugh Dickins pud_clear(pud); 2349e1b32caSBenjamin Herrenschmidt pmd_free_tlb(tlb, pmd, start); 235dc6c9a35SKirill A. Shutemov mm_dec_nr_pmds(tlb->mm); 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds 238c2febafcSKirill A. Shutemov static inline void free_pud_range(struct mmu_gather *tlb, p4d_t *p4d, 239e0da382cSHugh Dickins unsigned long addr, unsigned long end, 240e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 2411da177e4SLinus Torvalds { 2421da177e4SLinus Torvalds pud_t *pud; 2431da177e4SLinus Torvalds unsigned long next; 244e0da382cSHugh Dickins unsigned long start; 2451da177e4SLinus Torvalds 246e0da382cSHugh Dickins start = addr; 247c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 2481da177e4SLinus Torvalds do { 2491da177e4SLinus Torvalds next = pud_addr_end(addr, end); 2501da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 2511da177e4SLinus Torvalds continue; 252e0da382cSHugh Dickins free_pmd_range(tlb, pud, addr, next, floor, ceiling); 2531da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 2541da177e4SLinus Torvalds 255c2febafcSKirill A. Shutemov start &= P4D_MASK; 256c2febafcSKirill A. Shutemov if (start < floor) 257c2febafcSKirill A. Shutemov return; 258c2febafcSKirill A. Shutemov if (ceiling) { 259c2febafcSKirill A. Shutemov ceiling &= P4D_MASK; 260c2febafcSKirill A. Shutemov if (!ceiling) 261c2febafcSKirill A. Shutemov return; 262c2febafcSKirill A. Shutemov } 263c2febafcSKirill A. Shutemov if (end - 1 > ceiling - 1) 264c2febafcSKirill A. Shutemov return; 265c2febafcSKirill A. Shutemov 266c2febafcSKirill A. Shutemov pud = pud_offset(p4d, start); 267c2febafcSKirill A. Shutemov p4d_clear(p4d); 268c2febafcSKirill A. Shutemov pud_free_tlb(tlb, pud, start); 269b4e98d9aSKirill A. Shutemov mm_dec_nr_puds(tlb->mm); 270c2febafcSKirill A. Shutemov } 271c2febafcSKirill A. Shutemov 272c2febafcSKirill A. Shutemov static inline void free_p4d_range(struct mmu_gather *tlb, pgd_t *pgd, 273c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 274c2febafcSKirill A. Shutemov unsigned long floor, unsigned long ceiling) 275c2febafcSKirill A. Shutemov { 276c2febafcSKirill A. Shutemov p4d_t *p4d; 277c2febafcSKirill A. Shutemov unsigned long next; 278c2febafcSKirill A. Shutemov unsigned long start; 279c2febafcSKirill A. Shutemov 280c2febafcSKirill A. Shutemov start = addr; 281c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 282c2febafcSKirill A. Shutemov do { 283c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 284c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(p4d)) 285c2febafcSKirill A. Shutemov continue; 286c2febafcSKirill A. Shutemov free_pud_range(tlb, p4d, addr, next, floor, ceiling); 287c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 288c2febafcSKirill A. Shutemov 289e0da382cSHugh Dickins start &= PGDIR_MASK; 290e0da382cSHugh Dickins if (start < floor) 291e0da382cSHugh Dickins return; 292e0da382cSHugh Dickins if (ceiling) { 293e0da382cSHugh Dickins ceiling &= PGDIR_MASK; 294e0da382cSHugh Dickins if (!ceiling) 295e0da382cSHugh Dickins return; 2961da177e4SLinus Torvalds } 297e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 298e0da382cSHugh Dickins return; 299e0da382cSHugh Dickins 300c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, start); 301e0da382cSHugh Dickins pgd_clear(pgd); 302c2febafcSKirill A. Shutemov p4d_free_tlb(tlb, p4d, start); 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds /* 306e0da382cSHugh Dickins * This function frees user-level page tables of a process. 3071da177e4SLinus Torvalds */ 30842b77728SJan Beulich void free_pgd_range(struct mmu_gather *tlb, 309e0da382cSHugh Dickins unsigned long addr, unsigned long end, 310e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 3111da177e4SLinus Torvalds { 3121da177e4SLinus Torvalds pgd_t *pgd; 3131da177e4SLinus Torvalds unsigned long next; 3141da177e4SLinus Torvalds 315e0da382cSHugh Dickins /* 316e0da382cSHugh Dickins * The next few lines have given us lots of grief... 317e0da382cSHugh Dickins * 318e0da382cSHugh Dickins * Why are we testing PMD* at this top level? Because often 319e0da382cSHugh Dickins * there will be no work to do at all, and we'd prefer not to 320e0da382cSHugh Dickins * go all the way down to the bottom just to discover that. 321e0da382cSHugh Dickins * 322e0da382cSHugh Dickins * Why all these "- 1"s? Because 0 represents both the bottom 323e0da382cSHugh Dickins * of the address space and the top of it (using -1 for the 324e0da382cSHugh Dickins * top wouldn't help much: the masks would do the wrong thing). 325e0da382cSHugh Dickins * The rule is that addr 0 and floor 0 refer to the bottom of 326e0da382cSHugh Dickins * the address space, but end 0 and ceiling 0 refer to the top 327e0da382cSHugh Dickins * Comparisons need to use "end - 1" and "ceiling - 1" (though 328e0da382cSHugh Dickins * that end 0 case should be mythical). 329e0da382cSHugh Dickins * 330e0da382cSHugh Dickins * Wherever addr is brought up or ceiling brought down, we must 331e0da382cSHugh Dickins * be careful to reject "the opposite 0" before it confuses the 332e0da382cSHugh Dickins * subsequent tests. But what about where end is brought down 333e0da382cSHugh Dickins * by PMD_SIZE below? no, end can't go down to 0 there. 334e0da382cSHugh Dickins * 335e0da382cSHugh Dickins * Whereas we round start (addr) and ceiling down, by different 336e0da382cSHugh Dickins * masks at different levels, in order to test whether a table 337e0da382cSHugh Dickins * now has no other vmas using it, so can be freed, we don't 338e0da382cSHugh Dickins * bother to round floor or end up - the tests don't need that. 339e0da382cSHugh Dickins */ 340e0da382cSHugh Dickins 341e0da382cSHugh Dickins addr &= PMD_MASK; 342e0da382cSHugh Dickins if (addr < floor) { 343e0da382cSHugh Dickins addr += PMD_SIZE; 344e0da382cSHugh Dickins if (!addr) 345e0da382cSHugh Dickins return; 346e0da382cSHugh Dickins } 347e0da382cSHugh Dickins if (ceiling) { 348e0da382cSHugh Dickins ceiling &= PMD_MASK; 349e0da382cSHugh Dickins if (!ceiling) 350e0da382cSHugh Dickins return; 351e0da382cSHugh Dickins } 352e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 353e0da382cSHugh Dickins end -= PMD_SIZE; 354e0da382cSHugh Dickins if (addr > end - 1) 355e0da382cSHugh Dickins return; 35607e32661SAneesh Kumar K.V /* 35707e32661SAneesh Kumar K.V * We add page table cache pages with PAGE_SIZE, 35807e32661SAneesh Kumar K.V * (see pte_free_tlb()), flush the tlb if we need 35907e32661SAneesh Kumar K.V */ 360ed6a7935SPeter Zijlstra tlb_change_page_size(tlb, PAGE_SIZE); 36142b77728SJan Beulich pgd = pgd_offset(tlb->mm, addr); 3621da177e4SLinus Torvalds do { 3631da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 3641da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 3651da177e4SLinus Torvalds continue; 366c2febafcSKirill A. Shutemov free_p4d_range(tlb, pgd, addr, next, floor, ceiling); 3671da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 368e0da382cSHugh Dickins } 369e0da382cSHugh Dickins 37042b77728SJan Beulich void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma, 371e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 372e0da382cSHugh Dickins { 373e0da382cSHugh Dickins while (vma) { 374e0da382cSHugh Dickins struct vm_area_struct *next = vma->vm_next; 375e0da382cSHugh Dickins unsigned long addr = vma->vm_start; 376e0da382cSHugh Dickins 3778f4f8c16SHugh Dickins /* 37825d9e2d1Snpiggin@suse.de * Hide vma from rmap and truncate_pagecache before freeing 37925d9e2d1Snpiggin@suse.de * pgtables 3808f4f8c16SHugh Dickins */ 3815beb4930SRik van Riel unlink_anon_vmas(vma); 3828f4f8c16SHugh Dickins unlink_file_vma(vma); 3838f4f8c16SHugh Dickins 3849da61aefSDavid Gibson if (is_vm_hugetlb_page(vma)) { 3853bf5ee95SHugh Dickins hugetlb_free_pgd_range(tlb, addr, vma->vm_end, 3863bf5ee95SHugh Dickins floor, next ? next->vm_start : ceiling); 3873bf5ee95SHugh Dickins } else { 3883bf5ee95SHugh Dickins /* 3893bf5ee95SHugh Dickins * Optimization: gather nearby vmas into one call down 3903bf5ee95SHugh Dickins */ 3913bf5ee95SHugh Dickins while (next && next->vm_start <= vma->vm_end + PMD_SIZE 3924866920bSDavid Gibson && !is_vm_hugetlb_page(next)) { 393e0da382cSHugh Dickins vma = next; 394e0da382cSHugh Dickins next = vma->vm_next; 3955beb4930SRik van Riel unlink_anon_vmas(vma); 3968f4f8c16SHugh Dickins unlink_file_vma(vma); 397e0da382cSHugh Dickins } 3983bf5ee95SHugh Dickins free_pgd_range(tlb, addr, vma->vm_end, 399e0da382cSHugh Dickins floor, next ? next->vm_start : ceiling); 4003bf5ee95SHugh Dickins } 401e0da382cSHugh Dickins vma = next; 402e0da382cSHugh Dickins } 4031da177e4SLinus Torvalds } 4041da177e4SLinus Torvalds 4054cf58924SJoel Fernandes (Google) int __pte_alloc(struct mm_struct *mm, pmd_t *pmd) 4061da177e4SLinus Torvalds { 407c4088ebdSKirill A. Shutemov spinlock_t *ptl; 4084cf58924SJoel Fernandes (Google) pgtable_t new = pte_alloc_one(mm); 4091da177e4SLinus Torvalds if (!new) 4101bb3630eSHugh Dickins return -ENOMEM; 4111bb3630eSHugh Dickins 412362a61adSNick Piggin /* 413362a61adSNick Piggin * Ensure all pte setup (eg. pte page lock and page clearing) are 414362a61adSNick Piggin * visible before the pte is made visible to other CPUs by being 415362a61adSNick Piggin * put into page tables. 416362a61adSNick Piggin * 417362a61adSNick Piggin * The other side of the story is the pointer chasing in the page 418362a61adSNick Piggin * table walking code (when walking the page table without locking; 419362a61adSNick Piggin * ie. most of the time). Fortunately, these data accesses consist 420362a61adSNick Piggin * of a chain of data-dependent loads, meaning most CPUs (alpha 421362a61adSNick Piggin * being the notable exception) will already guarantee loads are 422362a61adSNick Piggin * seen in-order. See the alpha page table accessors for the 423362a61adSNick Piggin * smp_read_barrier_depends() barriers in page table walking code. 424362a61adSNick Piggin */ 425362a61adSNick Piggin smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */ 426362a61adSNick Piggin 427c4088ebdSKirill A. Shutemov ptl = pmd_lock(mm, pmd); 4288ac1f832SAndrea Arcangeli if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 429c4812909SKirill A. Shutemov mm_inc_nr_ptes(mm); 4301da177e4SLinus Torvalds pmd_populate(mm, pmd, new); 4312f569afdSMartin Schwidefsky new = NULL; 4324b471e88SKirill A. Shutemov } 433c4088ebdSKirill A. Shutemov spin_unlock(ptl); 4342f569afdSMartin Schwidefsky if (new) 4352f569afdSMartin Schwidefsky pte_free(mm, new); 4361bb3630eSHugh Dickins return 0; 4371da177e4SLinus Torvalds } 4381da177e4SLinus Torvalds 4394cf58924SJoel Fernandes (Google) int __pte_alloc_kernel(pmd_t *pmd) 4401da177e4SLinus Torvalds { 4414cf58924SJoel Fernandes (Google) pte_t *new = pte_alloc_one_kernel(&init_mm); 4421da177e4SLinus Torvalds if (!new) 4431bb3630eSHugh Dickins return -ENOMEM; 4441da177e4SLinus Torvalds 445362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 446362a61adSNick Piggin 447872fec16SHugh Dickins spin_lock(&init_mm.page_table_lock); 4488ac1f832SAndrea Arcangeli if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 449872fec16SHugh Dickins pmd_populate_kernel(&init_mm, pmd, new); 4502f569afdSMartin Schwidefsky new = NULL; 4514b471e88SKirill A. Shutemov } 452872fec16SHugh Dickins spin_unlock(&init_mm.page_table_lock); 4532f569afdSMartin Schwidefsky if (new) 4542f569afdSMartin Schwidefsky pte_free_kernel(&init_mm, new); 4551bb3630eSHugh Dickins return 0; 4561da177e4SLinus Torvalds } 4571da177e4SLinus Torvalds 458d559db08SKAMEZAWA Hiroyuki static inline void init_rss_vec(int *rss) 459ae859762SHugh Dickins { 460d559db08SKAMEZAWA Hiroyuki memset(rss, 0, sizeof(int) * NR_MM_COUNTERS); 461d559db08SKAMEZAWA Hiroyuki } 462d559db08SKAMEZAWA Hiroyuki 463d559db08SKAMEZAWA Hiroyuki static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss) 464d559db08SKAMEZAWA Hiroyuki { 465d559db08SKAMEZAWA Hiroyuki int i; 466d559db08SKAMEZAWA Hiroyuki 46734e55232SKAMEZAWA Hiroyuki if (current->mm == mm) 46805af2e10SDavid Rientjes sync_mm_rss(mm); 469d559db08SKAMEZAWA Hiroyuki for (i = 0; i < NR_MM_COUNTERS; i++) 470d559db08SKAMEZAWA Hiroyuki if (rss[i]) 471d559db08SKAMEZAWA Hiroyuki add_mm_counter(mm, i, rss[i]); 472ae859762SHugh Dickins } 473ae859762SHugh Dickins 4741da177e4SLinus Torvalds /* 4756aab341eSLinus Torvalds * This function is called to print an error when a bad pte 4766aab341eSLinus Torvalds * is found. For example, we might have a PFN-mapped pte in 4776aab341eSLinus Torvalds * a region that doesn't allow it. 478b5810039SNick Piggin * 479b5810039SNick Piggin * The calling function must still handle the error. 480b5810039SNick Piggin */ 4813dc14741SHugh Dickins static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr, 4823dc14741SHugh Dickins pte_t pte, struct page *page) 483b5810039SNick Piggin { 4843dc14741SHugh Dickins pgd_t *pgd = pgd_offset(vma->vm_mm, addr); 485c2febafcSKirill A. Shutemov p4d_t *p4d = p4d_offset(pgd, addr); 486c2febafcSKirill A. Shutemov pud_t *pud = pud_offset(p4d, addr); 4873dc14741SHugh Dickins pmd_t *pmd = pmd_offset(pud, addr); 4883dc14741SHugh Dickins struct address_space *mapping; 4893dc14741SHugh Dickins pgoff_t index; 490d936cf9bSHugh Dickins static unsigned long resume; 491d936cf9bSHugh Dickins static unsigned long nr_shown; 492d936cf9bSHugh Dickins static unsigned long nr_unshown; 493d936cf9bSHugh Dickins 494d936cf9bSHugh Dickins /* 495d936cf9bSHugh Dickins * Allow a burst of 60 reports, then keep quiet for that minute; 496d936cf9bSHugh Dickins * or allow a steady drip of one report per second. 497d936cf9bSHugh Dickins */ 498d936cf9bSHugh Dickins if (nr_shown == 60) { 499d936cf9bSHugh Dickins if (time_before(jiffies, resume)) { 500d936cf9bSHugh Dickins nr_unshown++; 501d936cf9bSHugh Dickins return; 502d936cf9bSHugh Dickins } 503d936cf9bSHugh Dickins if (nr_unshown) { 5041170532bSJoe Perches pr_alert("BUG: Bad page map: %lu messages suppressed\n", 505d936cf9bSHugh Dickins nr_unshown); 506d936cf9bSHugh Dickins nr_unshown = 0; 507d936cf9bSHugh Dickins } 508d936cf9bSHugh Dickins nr_shown = 0; 509d936cf9bSHugh Dickins } 510d936cf9bSHugh Dickins if (nr_shown++ == 0) 511d936cf9bSHugh Dickins resume = jiffies + 60 * HZ; 5123dc14741SHugh Dickins 5133dc14741SHugh Dickins mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL; 5143dc14741SHugh Dickins index = linear_page_index(vma, addr); 5153dc14741SHugh Dickins 5161170532bSJoe Perches pr_alert("BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n", 5173dc14741SHugh Dickins current->comm, 5183dc14741SHugh Dickins (long long)pte_val(pte), (long long)pmd_val(*pmd)); 519718a3821SWu Fengguang if (page) 520f0b791a3SDave Hansen dump_page(page, "bad pte"); 5211170532bSJoe Perches pr_alert("addr:%p vm_flags:%08lx anon_vma:%p mapping:%p index:%lx\n", 5223dc14741SHugh Dickins (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index); 523d75f773cSSakari Ailus pr_alert("file:%pD fault:%ps mmap:%ps readpage:%ps\n", 5242682582aSKonstantin Khlebnikov vma->vm_file, 5252682582aSKonstantin Khlebnikov vma->vm_ops ? vma->vm_ops->fault : NULL, 5262682582aSKonstantin Khlebnikov vma->vm_file ? vma->vm_file->f_op->mmap : NULL, 5272682582aSKonstantin Khlebnikov mapping ? mapping->a_ops->readpage : NULL); 528b5810039SNick Piggin dump_stack(); 529373d4d09SRusty Russell add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); 530b5810039SNick Piggin } 531b5810039SNick Piggin 532b5810039SNick Piggin /* 5337e675137SNick Piggin * vm_normal_page -- This function gets the "struct page" associated with a pte. 5346aab341eSLinus Torvalds * 5357e675137SNick Piggin * "Special" mappings do not wish to be associated with a "struct page" (either 5367e675137SNick Piggin * it doesn't exist, or it exists but they don't want to touch it). In this 5377e675137SNick Piggin * case, NULL is returned here. "Normal" mappings do have a struct page. 538b379d790SJared Hulbert * 5397e675137SNick Piggin * There are 2 broad cases. Firstly, an architecture may define a pte_special() 5407e675137SNick Piggin * pte bit, in which case this function is trivial. Secondly, an architecture 5417e675137SNick Piggin * may not have a spare pte bit, which requires a more complicated scheme, 5427e675137SNick Piggin * described below. 5437e675137SNick Piggin * 5447e675137SNick Piggin * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a 5457e675137SNick Piggin * special mapping (even if there are underlying and valid "struct pages"). 5467e675137SNick Piggin * COWed pages of a VM_PFNMAP are always normal. 5476aab341eSLinus Torvalds * 548b379d790SJared Hulbert * The way we recognize COWed pages within VM_PFNMAP mappings is through the 549b379d790SJared Hulbert * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit 5507e675137SNick Piggin * set, and the vm_pgoff will point to the first PFN mapped: thus every special 5517e675137SNick Piggin * mapping will always honor the rule 5526aab341eSLinus Torvalds * 5536aab341eSLinus Torvalds * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT) 5546aab341eSLinus Torvalds * 5557e675137SNick Piggin * And for normal mappings this is false. 556b379d790SJared Hulbert * 5577e675137SNick Piggin * This restricts such mappings to be a linear translation from virtual address 5587e675137SNick Piggin * to pfn. To get around this restriction, we allow arbitrary mappings so long 5597e675137SNick Piggin * as the vma is not a COW mapping; in that case, we know that all ptes are 5607e675137SNick Piggin * special (because none can have been COWed). 561b379d790SJared Hulbert * 562b379d790SJared Hulbert * 5637e675137SNick Piggin * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP. 5647e675137SNick Piggin * 565b379d790SJared Hulbert * VM_MIXEDMAP mappings can likewise contain memory with or without "struct 566b379d790SJared Hulbert * page" backing, however the difference is that _all_ pages with a struct 567b379d790SJared Hulbert * page (that is, those where pfn_valid is true) are refcounted and considered 568b379d790SJared Hulbert * normal pages by the VM. The disadvantage is that pages are refcounted 569b379d790SJared Hulbert * (which can be slower and simply not an option for some PFNMAP users). The 570b379d790SJared Hulbert * advantage is that we don't have to follow the strict linearity rule of 571b379d790SJared Hulbert * PFNMAP mappings in order to support COWable mappings. 572b379d790SJared Hulbert * 573ee498ed7SHugh Dickins */ 57425b2995aSChristoph Hellwig struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, 57525b2995aSChristoph Hellwig pte_t pte) 576ee498ed7SHugh Dickins { 57722b31eecSHugh Dickins unsigned long pfn = pte_pfn(pte); 5787e675137SNick Piggin 57900b3a331SLaurent Dufour if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL)) { 580b38af472SHugh Dickins if (likely(!pte_special(pte))) 58122b31eecSHugh Dickins goto check_pfn; 582667a0a06SDavid Vrabel if (vma->vm_ops && vma->vm_ops->find_special_page) 583667a0a06SDavid Vrabel return vma->vm_ops->find_special_page(vma, addr); 584a13ea5b7SHugh Dickins if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)) 585a13ea5b7SHugh Dickins return NULL; 586df6ad698SJérôme Glisse if (is_zero_pfn(pfn)) 587df6ad698SJérôme Glisse return NULL; 588e1fb4a08SDave Jiang if (pte_devmap(pte)) 589e1fb4a08SDave Jiang return NULL; 590e1fb4a08SDave Jiang 59122b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 5927e675137SNick Piggin return NULL; 5937e675137SNick Piggin } 5947e675137SNick Piggin 59500b3a331SLaurent Dufour /* !CONFIG_ARCH_HAS_PTE_SPECIAL case follows: */ 5967e675137SNick Piggin 597b379d790SJared Hulbert if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { 598b379d790SJared Hulbert if (vma->vm_flags & VM_MIXEDMAP) { 599b379d790SJared Hulbert if (!pfn_valid(pfn)) 600b379d790SJared Hulbert return NULL; 601b379d790SJared Hulbert goto out; 602b379d790SJared Hulbert } else { 6037e675137SNick Piggin unsigned long off; 6047e675137SNick Piggin off = (addr - vma->vm_start) >> PAGE_SHIFT; 6056aab341eSLinus Torvalds if (pfn == vma->vm_pgoff + off) 6066aab341eSLinus Torvalds return NULL; 60767121172SLinus Torvalds if (!is_cow_mapping(vma->vm_flags)) 608fb155c16SLinus Torvalds return NULL; 6096aab341eSLinus Torvalds } 610b379d790SJared Hulbert } 6116aab341eSLinus Torvalds 612b38af472SHugh Dickins if (is_zero_pfn(pfn)) 613b38af472SHugh Dickins return NULL; 61400b3a331SLaurent Dufour 61522b31eecSHugh Dickins check_pfn: 61622b31eecSHugh Dickins if (unlikely(pfn > highest_memmap_pfn)) { 61722b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 61822b31eecSHugh Dickins return NULL; 61922b31eecSHugh Dickins } 6206aab341eSLinus Torvalds 6216aab341eSLinus Torvalds /* 6227e675137SNick Piggin * NOTE! We still have PageReserved() pages in the page tables. 6237e675137SNick Piggin * eg. VDSO mappings can cause them to exist. 6246aab341eSLinus Torvalds */ 625b379d790SJared Hulbert out: 6266aab341eSLinus Torvalds return pfn_to_page(pfn); 627ee498ed7SHugh Dickins } 628ee498ed7SHugh Dickins 62928093f9fSGerald Schaefer #ifdef CONFIG_TRANSPARENT_HUGEPAGE 63028093f9fSGerald Schaefer struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr, 63128093f9fSGerald Schaefer pmd_t pmd) 63228093f9fSGerald Schaefer { 63328093f9fSGerald Schaefer unsigned long pfn = pmd_pfn(pmd); 63428093f9fSGerald Schaefer 63528093f9fSGerald Schaefer /* 63628093f9fSGerald Schaefer * There is no pmd_special() but there may be special pmds, e.g. 63728093f9fSGerald Schaefer * in a direct-access (dax) mapping, so let's just replicate the 63800b3a331SLaurent Dufour * !CONFIG_ARCH_HAS_PTE_SPECIAL case from vm_normal_page() here. 63928093f9fSGerald Schaefer */ 64028093f9fSGerald Schaefer if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { 64128093f9fSGerald Schaefer if (vma->vm_flags & VM_MIXEDMAP) { 64228093f9fSGerald Schaefer if (!pfn_valid(pfn)) 64328093f9fSGerald Schaefer return NULL; 64428093f9fSGerald Schaefer goto out; 64528093f9fSGerald Schaefer } else { 64628093f9fSGerald Schaefer unsigned long off; 64728093f9fSGerald Schaefer off = (addr - vma->vm_start) >> PAGE_SHIFT; 64828093f9fSGerald Schaefer if (pfn == vma->vm_pgoff + off) 64928093f9fSGerald Schaefer return NULL; 65028093f9fSGerald Schaefer if (!is_cow_mapping(vma->vm_flags)) 65128093f9fSGerald Schaefer return NULL; 65228093f9fSGerald Schaefer } 65328093f9fSGerald Schaefer } 65428093f9fSGerald Schaefer 655e1fb4a08SDave Jiang if (pmd_devmap(pmd)) 656e1fb4a08SDave Jiang return NULL; 65728093f9fSGerald Schaefer if (is_zero_pfn(pfn)) 65828093f9fSGerald Schaefer return NULL; 65928093f9fSGerald Schaefer if (unlikely(pfn > highest_memmap_pfn)) 66028093f9fSGerald Schaefer return NULL; 66128093f9fSGerald Schaefer 66228093f9fSGerald Schaefer /* 66328093f9fSGerald Schaefer * NOTE! We still have PageReserved() pages in the page tables. 66428093f9fSGerald Schaefer * eg. VDSO mappings can cause them to exist. 66528093f9fSGerald Schaefer */ 66628093f9fSGerald Schaefer out: 66728093f9fSGerald Schaefer return pfn_to_page(pfn); 66828093f9fSGerald Schaefer } 66928093f9fSGerald Schaefer #endif 67028093f9fSGerald Schaefer 671ee498ed7SHugh Dickins /* 6721da177e4SLinus Torvalds * copy one vm_area from one task to the other. Assumes the page tables 6731da177e4SLinus Torvalds * already present in the new task to be cleared in the whole range 6741da177e4SLinus Torvalds * covered by this vma. 6751da177e4SLinus Torvalds */ 6761da177e4SLinus Torvalds 677570a335bSHugh Dickins static inline unsigned long 6781da177e4SLinus Torvalds copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, 679b5810039SNick Piggin pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma, 6808c103762SHugh Dickins unsigned long addr, int *rss) 6811da177e4SLinus Torvalds { 682b5810039SNick Piggin unsigned long vm_flags = vma->vm_flags; 6831da177e4SLinus Torvalds pte_t pte = *src_pte; 6841da177e4SLinus Torvalds struct page *page; 6851da177e4SLinus Torvalds 6861da177e4SLinus Torvalds /* pte contains position in swap or file, so copy. */ 6871da177e4SLinus Torvalds if (unlikely(!pte_present(pte))) { 6880697212aSChristoph Lameter swp_entry_t entry = pte_to_swp_entry(pte); 6890697212aSChristoph Lameter 6902022b4d1SHugh Dickins if (likely(!non_swap_entry(entry))) { 691570a335bSHugh Dickins if (swap_duplicate(entry) < 0) 692570a335bSHugh Dickins return entry.val; 693570a335bSHugh Dickins 6941da177e4SLinus Torvalds /* make sure dst_mm is on swapoff's mmlist. */ 6951da177e4SLinus Torvalds if (unlikely(list_empty(&dst_mm->mmlist))) { 6961da177e4SLinus Torvalds spin_lock(&mmlist_lock); 697f412ac08SHugh Dickins if (list_empty(&dst_mm->mmlist)) 698f412ac08SHugh Dickins list_add(&dst_mm->mmlist, 699f412ac08SHugh Dickins &src_mm->mmlist); 7001da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 7011da177e4SLinus Torvalds } 702b084d435SKAMEZAWA Hiroyuki rss[MM_SWAPENTS]++; 7032022b4d1SHugh Dickins } else if (is_migration_entry(entry)) { 7049f9f1acdSKonstantin Khlebnikov page = migration_entry_to_page(entry); 7059f9f1acdSKonstantin Khlebnikov 706eca56ff9SJerome Marchand rss[mm_counter(page)]++; 7079f9f1acdSKonstantin Khlebnikov 7089f9f1acdSKonstantin Khlebnikov if (is_write_migration_entry(entry) && 7090697212aSChristoph Lameter is_cow_mapping(vm_flags)) { 7100697212aSChristoph Lameter /* 7119f9f1acdSKonstantin Khlebnikov * COW mappings require pages in both 7129f9f1acdSKonstantin Khlebnikov * parent and child to be set to read. 7130697212aSChristoph Lameter */ 7140697212aSChristoph Lameter make_migration_entry_read(&entry); 7150697212aSChristoph Lameter pte = swp_entry_to_pte(entry); 716c3d16e16SCyrill Gorcunov if (pte_swp_soft_dirty(*src_pte)) 717c3d16e16SCyrill Gorcunov pte = pte_swp_mksoft_dirty(pte); 7180697212aSChristoph Lameter set_pte_at(src_mm, addr, src_pte, pte); 7190697212aSChristoph Lameter } 7205042db43SJérôme Glisse } else if (is_device_private_entry(entry)) { 7215042db43SJérôme Glisse page = device_private_entry_to_page(entry); 7225042db43SJérôme Glisse 7235042db43SJérôme Glisse /* 7245042db43SJérôme Glisse * Update rss count even for unaddressable pages, as 7255042db43SJérôme Glisse * they should treated just like normal pages in this 7265042db43SJérôme Glisse * respect. 7275042db43SJérôme Glisse * 7285042db43SJérôme Glisse * We will likely want to have some new rss counters 7295042db43SJérôme Glisse * for unaddressable pages, at some point. But for now 7305042db43SJérôme Glisse * keep things as they are. 7315042db43SJérôme Glisse */ 7325042db43SJérôme Glisse get_page(page); 7335042db43SJérôme Glisse rss[mm_counter(page)]++; 7345042db43SJérôme Glisse page_dup_rmap(page, false); 7355042db43SJérôme Glisse 7365042db43SJérôme Glisse /* 7375042db43SJérôme Glisse * We do not preserve soft-dirty information, because so 7385042db43SJérôme Glisse * far, checkpoint/restore is the only feature that 7395042db43SJérôme Glisse * requires that. And checkpoint/restore does not work 7405042db43SJérôme Glisse * when a device driver is involved (you cannot easily 7415042db43SJérôme Glisse * save and restore device driver state). 7425042db43SJérôme Glisse */ 7435042db43SJérôme Glisse if (is_write_device_private_entry(entry) && 7445042db43SJérôme Glisse is_cow_mapping(vm_flags)) { 7455042db43SJérôme Glisse make_device_private_entry_read(&entry); 7465042db43SJérôme Glisse pte = swp_entry_to_pte(entry); 7475042db43SJérôme Glisse set_pte_at(src_mm, addr, src_pte, pte); 7485042db43SJérôme Glisse } 7491da177e4SLinus Torvalds } 750ae859762SHugh Dickins goto out_set_pte; 7511da177e4SLinus Torvalds } 7521da177e4SLinus Torvalds 7531da177e4SLinus Torvalds /* 7541da177e4SLinus Torvalds * If it's a COW mapping, write protect it both 7551da177e4SLinus Torvalds * in the parent and the child 7561da177e4SLinus Torvalds */ 7571b2de5d0SLinus Torvalds if (is_cow_mapping(vm_flags) && pte_write(pte)) { 7581da177e4SLinus Torvalds ptep_set_wrprotect(src_mm, addr, src_pte); 7593dc90795SZachary Amsden pte = pte_wrprotect(pte); 7601da177e4SLinus Torvalds } 7611da177e4SLinus Torvalds 7621da177e4SLinus Torvalds /* 7631da177e4SLinus Torvalds * If it's a shared mapping, mark it clean in 7641da177e4SLinus Torvalds * the child 7651da177e4SLinus Torvalds */ 7661da177e4SLinus Torvalds if (vm_flags & VM_SHARED) 7671da177e4SLinus Torvalds pte = pte_mkclean(pte); 7681da177e4SLinus Torvalds pte = pte_mkold(pte); 7696aab341eSLinus Torvalds 7706aab341eSLinus Torvalds page = vm_normal_page(vma, addr, pte); 7716aab341eSLinus Torvalds if (page) { 7721da177e4SLinus Torvalds get_page(page); 77353f9263bSKirill A. Shutemov page_dup_rmap(page, false); 774eca56ff9SJerome Marchand rss[mm_counter(page)]++; 775df6ad698SJérôme Glisse } else if (pte_devmap(pte)) { 776df6ad698SJérôme Glisse page = pte_page(pte); 7776aab341eSLinus Torvalds } 778ae859762SHugh Dickins 779ae859762SHugh Dickins out_set_pte: 780ae859762SHugh Dickins set_pte_at(dst_mm, addr, dst_pte, pte); 781570a335bSHugh Dickins return 0; 7821da177e4SLinus Torvalds } 7831da177e4SLinus Torvalds 78421bda264SJerome Marchand static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 7851da177e4SLinus Torvalds pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma, 7861da177e4SLinus Torvalds unsigned long addr, unsigned long end) 7871da177e4SLinus Torvalds { 788c36987e2SDaisuke Nishimura pte_t *orig_src_pte, *orig_dst_pte; 7891da177e4SLinus Torvalds pte_t *src_pte, *dst_pte; 790c74df32cSHugh Dickins spinlock_t *src_ptl, *dst_ptl; 791e040f218SHugh Dickins int progress = 0; 792d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 793570a335bSHugh Dickins swp_entry_t entry = (swp_entry_t){0}; 7941da177e4SLinus Torvalds 7951da177e4SLinus Torvalds again: 796d559db08SKAMEZAWA Hiroyuki init_rss_vec(rss); 797d559db08SKAMEZAWA Hiroyuki 798c74df32cSHugh Dickins dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl); 7991da177e4SLinus Torvalds if (!dst_pte) 8001da177e4SLinus Torvalds return -ENOMEM; 801ece0e2b6SPeter Zijlstra src_pte = pte_offset_map(src_pmd, addr); 8024c21e2f2SHugh Dickins src_ptl = pte_lockptr(src_mm, src_pmd); 803f20dc5f7SIngo Molnar spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 804c36987e2SDaisuke Nishimura orig_src_pte = src_pte; 805c36987e2SDaisuke Nishimura orig_dst_pte = dst_pte; 8066606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 8071da177e4SLinus Torvalds 8081da177e4SLinus Torvalds do { 8091da177e4SLinus Torvalds /* 8101da177e4SLinus Torvalds * We are holding two locks at this point - either of them 8111da177e4SLinus Torvalds * could generate latencies in another task on another CPU. 8121da177e4SLinus Torvalds */ 813e040f218SHugh Dickins if (progress >= 32) { 814e040f218SHugh Dickins progress = 0; 815e040f218SHugh Dickins if (need_resched() || 81695c354feSNick Piggin spin_needbreak(src_ptl) || spin_needbreak(dst_ptl)) 8171da177e4SLinus Torvalds break; 818e040f218SHugh Dickins } 8191da177e4SLinus Torvalds if (pte_none(*src_pte)) { 8201da177e4SLinus Torvalds progress++; 8211da177e4SLinus Torvalds continue; 8221da177e4SLinus Torvalds } 823570a335bSHugh Dickins entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte, 824570a335bSHugh Dickins vma, addr, rss); 825570a335bSHugh Dickins if (entry.val) 826570a335bSHugh Dickins break; 8271da177e4SLinus Torvalds progress += 8; 8281da177e4SLinus Torvalds } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end); 8291da177e4SLinus Torvalds 8306606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 831c74df32cSHugh Dickins spin_unlock(src_ptl); 832ece0e2b6SPeter Zijlstra pte_unmap(orig_src_pte); 833d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(dst_mm, rss); 834c36987e2SDaisuke Nishimura pte_unmap_unlock(orig_dst_pte, dst_ptl); 835c74df32cSHugh Dickins cond_resched(); 836570a335bSHugh Dickins 837570a335bSHugh Dickins if (entry.val) { 838570a335bSHugh Dickins if (add_swap_count_continuation(entry, GFP_KERNEL) < 0) 839570a335bSHugh Dickins return -ENOMEM; 840570a335bSHugh Dickins progress = 0; 841570a335bSHugh Dickins } 8421da177e4SLinus Torvalds if (addr != end) 8431da177e4SLinus Torvalds goto again; 8441da177e4SLinus Torvalds return 0; 8451da177e4SLinus Torvalds } 8461da177e4SLinus Torvalds 8471da177e4SLinus Torvalds static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 8481da177e4SLinus Torvalds pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma, 8491da177e4SLinus Torvalds unsigned long addr, unsigned long end) 8501da177e4SLinus Torvalds { 8511da177e4SLinus Torvalds pmd_t *src_pmd, *dst_pmd; 8521da177e4SLinus Torvalds unsigned long next; 8531da177e4SLinus Torvalds 8541da177e4SLinus Torvalds dst_pmd = pmd_alloc(dst_mm, dst_pud, addr); 8551da177e4SLinus Torvalds if (!dst_pmd) 8561da177e4SLinus Torvalds return -ENOMEM; 8571da177e4SLinus Torvalds src_pmd = pmd_offset(src_pud, addr); 8581da177e4SLinus Torvalds do { 8591da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 86084c3fc4eSZi Yan if (is_swap_pmd(*src_pmd) || pmd_trans_huge(*src_pmd) 86184c3fc4eSZi Yan || pmd_devmap(*src_pmd)) { 86271e3aac0SAndrea Arcangeli int err; 863a00cc7d9SMatthew Wilcox VM_BUG_ON_VMA(next-addr != HPAGE_PMD_SIZE, vma); 86471e3aac0SAndrea Arcangeli err = copy_huge_pmd(dst_mm, src_mm, 86571e3aac0SAndrea Arcangeli dst_pmd, src_pmd, addr, vma); 86671e3aac0SAndrea Arcangeli if (err == -ENOMEM) 86771e3aac0SAndrea Arcangeli return -ENOMEM; 86871e3aac0SAndrea Arcangeli if (!err) 86971e3aac0SAndrea Arcangeli continue; 87071e3aac0SAndrea Arcangeli /* fall through */ 87171e3aac0SAndrea Arcangeli } 8721da177e4SLinus Torvalds if (pmd_none_or_clear_bad(src_pmd)) 8731da177e4SLinus Torvalds continue; 8741da177e4SLinus Torvalds if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd, 8751da177e4SLinus Torvalds vma, addr, next)) 8761da177e4SLinus Torvalds return -ENOMEM; 8771da177e4SLinus Torvalds } while (dst_pmd++, src_pmd++, addr = next, addr != end); 8781da177e4SLinus Torvalds return 0; 8791da177e4SLinus Torvalds } 8801da177e4SLinus Torvalds 8811da177e4SLinus Torvalds static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 882c2febafcSKirill A. Shutemov p4d_t *dst_p4d, p4d_t *src_p4d, struct vm_area_struct *vma, 8831da177e4SLinus Torvalds unsigned long addr, unsigned long end) 8841da177e4SLinus Torvalds { 8851da177e4SLinus Torvalds pud_t *src_pud, *dst_pud; 8861da177e4SLinus Torvalds unsigned long next; 8871da177e4SLinus Torvalds 888c2febafcSKirill A. Shutemov dst_pud = pud_alloc(dst_mm, dst_p4d, addr); 8891da177e4SLinus Torvalds if (!dst_pud) 8901da177e4SLinus Torvalds return -ENOMEM; 891c2febafcSKirill A. Shutemov src_pud = pud_offset(src_p4d, addr); 8921da177e4SLinus Torvalds do { 8931da177e4SLinus Torvalds next = pud_addr_end(addr, end); 894a00cc7d9SMatthew Wilcox if (pud_trans_huge(*src_pud) || pud_devmap(*src_pud)) { 895a00cc7d9SMatthew Wilcox int err; 896a00cc7d9SMatthew Wilcox 897a00cc7d9SMatthew Wilcox VM_BUG_ON_VMA(next-addr != HPAGE_PUD_SIZE, vma); 898a00cc7d9SMatthew Wilcox err = copy_huge_pud(dst_mm, src_mm, 899a00cc7d9SMatthew Wilcox dst_pud, src_pud, addr, vma); 900a00cc7d9SMatthew Wilcox if (err == -ENOMEM) 901a00cc7d9SMatthew Wilcox return -ENOMEM; 902a00cc7d9SMatthew Wilcox if (!err) 903a00cc7d9SMatthew Wilcox continue; 904a00cc7d9SMatthew Wilcox /* fall through */ 905a00cc7d9SMatthew Wilcox } 9061da177e4SLinus Torvalds if (pud_none_or_clear_bad(src_pud)) 9071da177e4SLinus Torvalds continue; 9081da177e4SLinus Torvalds if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud, 9091da177e4SLinus Torvalds vma, addr, next)) 9101da177e4SLinus Torvalds return -ENOMEM; 9111da177e4SLinus Torvalds } while (dst_pud++, src_pud++, addr = next, addr != end); 9121da177e4SLinus Torvalds return 0; 9131da177e4SLinus Torvalds } 9141da177e4SLinus Torvalds 915c2febafcSKirill A. Shutemov static inline int copy_p4d_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 916c2febafcSKirill A. Shutemov pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma, 917c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end) 918c2febafcSKirill A. Shutemov { 919c2febafcSKirill A. Shutemov p4d_t *src_p4d, *dst_p4d; 920c2febafcSKirill A. Shutemov unsigned long next; 921c2febafcSKirill A. Shutemov 922c2febafcSKirill A. Shutemov dst_p4d = p4d_alloc(dst_mm, dst_pgd, addr); 923c2febafcSKirill A. Shutemov if (!dst_p4d) 924c2febafcSKirill A. Shutemov return -ENOMEM; 925c2febafcSKirill A. Shutemov src_p4d = p4d_offset(src_pgd, addr); 926c2febafcSKirill A. Shutemov do { 927c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 928c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(src_p4d)) 929c2febafcSKirill A. Shutemov continue; 930c2febafcSKirill A. Shutemov if (copy_pud_range(dst_mm, src_mm, dst_p4d, src_p4d, 931c2febafcSKirill A. Shutemov vma, addr, next)) 932c2febafcSKirill A. Shutemov return -ENOMEM; 933c2febafcSKirill A. Shutemov } while (dst_p4d++, src_p4d++, addr = next, addr != end); 934c2febafcSKirill A. Shutemov return 0; 935c2febafcSKirill A. Shutemov } 936c2febafcSKirill A. Shutemov 9371da177e4SLinus Torvalds int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, 9381da177e4SLinus Torvalds struct vm_area_struct *vma) 9391da177e4SLinus Torvalds { 9401da177e4SLinus Torvalds pgd_t *src_pgd, *dst_pgd; 9411da177e4SLinus Torvalds unsigned long next; 9421da177e4SLinus Torvalds unsigned long addr = vma->vm_start; 9431da177e4SLinus Torvalds unsigned long end = vma->vm_end; 944ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 9452ec74c3eSSagi Grimberg bool is_cow; 946cddb8a5cSAndrea Arcangeli int ret; 9471da177e4SLinus Torvalds 948d992895bSNick Piggin /* 949d992895bSNick Piggin * Don't copy ptes where a page fault will fill them correctly. 950d992895bSNick Piggin * Fork becomes much lighter when there are big shared or private 951d992895bSNick Piggin * readonly mappings. The tradeoff is that copy_page_range is more 952d992895bSNick Piggin * efficient than faulting. 953d992895bSNick Piggin */ 9540661a336SKirill A. Shutemov if (!(vma->vm_flags & (VM_HUGETLB | VM_PFNMAP | VM_MIXEDMAP)) && 9550661a336SKirill A. Shutemov !vma->anon_vma) 956d992895bSNick Piggin return 0; 957d992895bSNick Piggin 9581da177e4SLinus Torvalds if (is_vm_hugetlb_page(vma)) 9591da177e4SLinus Torvalds return copy_hugetlb_page_range(dst_mm, src_mm, vma); 9601da177e4SLinus Torvalds 961b3b9c293SKonstantin Khlebnikov if (unlikely(vma->vm_flags & VM_PFNMAP)) { 9622ab64037Svenkatesh.pallipadi@intel.com /* 9632ab64037Svenkatesh.pallipadi@intel.com * We do not free on error cases below as remove_vma 9642ab64037Svenkatesh.pallipadi@intel.com * gets called on error from higher level routine 9652ab64037Svenkatesh.pallipadi@intel.com */ 9665180da41SSuresh Siddha ret = track_pfn_copy(vma); 9672ab64037Svenkatesh.pallipadi@intel.com if (ret) 9682ab64037Svenkatesh.pallipadi@intel.com return ret; 9692ab64037Svenkatesh.pallipadi@intel.com } 9702ab64037Svenkatesh.pallipadi@intel.com 971cddb8a5cSAndrea Arcangeli /* 972cddb8a5cSAndrea Arcangeli * We need to invalidate the secondary MMU mappings only when 973cddb8a5cSAndrea Arcangeli * there could be a permission downgrade on the ptes of the 974cddb8a5cSAndrea Arcangeli * parent mm. And a permission downgrade will only happen if 975cddb8a5cSAndrea Arcangeli * is_cow_mapping() returns true. 976cddb8a5cSAndrea Arcangeli */ 9772ec74c3eSSagi Grimberg is_cow = is_cow_mapping(vma->vm_flags); 978ac46d4f3SJérôme Glisse 979ac46d4f3SJérôme Glisse if (is_cow) { 9807269f999SJérôme Glisse mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_PAGE, 9817269f999SJérôme Glisse 0, vma, src_mm, addr, end); 982ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 983ac46d4f3SJérôme Glisse } 984cddb8a5cSAndrea Arcangeli 985cddb8a5cSAndrea Arcangeli ret = 0; 9861da177e4SLinus Torvalds dst_pgd = pgd_offset(dst_mm, addr); 9871da177e4SLinus Torvalds src_pgd = pgd_offset(src_mm, addr); 9881da177e4SLinus Torvalds do { 9891da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 9901da177e4SLinus Torvalds if (pgd_none_or_clear_bad(src_pgd)) 9911da177e4SLinus Torvalds continue; 992c2febafcSKirill A. Shutemov if (unlikely(copy_p4d_range(dst_mm, src_mm, dst_pgd, src_pgd, 993cddb8a5cSAndrea Arcangeli vma, addr, next))) { 994cddb8a5cSAndrea Arcangeli ret = -ENOMEM; 995cddb8a5cSAndrea Arcangeli break; 996cddb8a5cSAndrea Arcangeli } 9971da177e4SLinus Torvalds } while (dst_pgd++, src_pgd++, addr = next, addr != end); 998cddb8a5cSAndrea Arcangeli 9992ec74c3eSSagi Grimberg if (is_cow) 1000ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 1001cddb8a5cSAndrea Arcangeli return ret; 10021da177e4SLinus Torvalds } 10031da177e4SLinus Torvalds 100451c6f666SRobin Holt static unsigned long zap_pte_range(struct mmu_gather *tlb, 1005b5810039SNick Piggin struct vm_area_struct *vma, pmd_t *pmd, 10061da177e4SLinus Torvalds unsigned long addr, unsigned long end, 100797a89413SPeter Zijlstra struct zap_details *details) 10081da177e4SLinus Torvalds { 1009b5810039SNick Piggin struct mm_struct *mm = tlb->mm; 1010d16dfc55SPeter Zijlstra int force_flush = 0; 1011d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 101297a89413SPeter Zijlstra spinlock_t *ptl; 10135f1a1907SSteven Rostedt pte_t *start_pte; 101497a89413SPeter Zijlstra pte_t *pte; 10158a5f14a2SKirill A. Shutemov swp_entry_t entry; 1016d559db08SKAMEZAWA Hiroyuki 1017ed6a7935SPeter Zijlstra tlb_change_page_size(tlb, PAGE_SIZE); 1018d16dfc55SPeter Zijlstra again: 1019e303297eSPeter Zijlstra init_rss_vec(rss); 10205f1a1907SSteven Rostedt start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl); 10215f1a1907SSteven Rostedt pte = start_pte; 10223ea27719SMel Gorman flush_tlb_batched_pending(mm); 10236606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 10241da177e4SLinus Torvalds do { 10251da177e4SLinus Torvalds pte_t ptent = *pte; 1026166f61b9STobin C Harding if (pte_none(ptent)) 10271da177e4SLinus Torvalds continue; 102851c6f666SRobin Holt 10296f5e6b9eSHugh Dickins if (pte_present(ptent)) { 10306f5e6b9eSHugh Dickins struct page *page; 10316f5e6b9eSHugh Dickins 103225b2995aSChristoph Hellwig page = vm_normal_page(vma, addr, ptent); 10331da177e4SLinus Torvalds if (unlikely(details) && page) { 10341da177e4SLinus Torvalds /* 10351da177e4SLinus Torvalds * unmap_shared_mapping_pages() wants to 10361da177e4SLinus Torvalds * invalidate cache without truncating: 10371da177e4SLinus Torvalds * unmap shared but keep private pages. 10381da177e4SLinus Torvalds */ 10391da177e4SLinus Torvalds if (details->check_mapping && 1040800d8c63SKirill A. Shutemov details->check_mapping != page_rmapping(page)) 10411da177e4SLinus Torvalds continue; 10421da177e4SLinus Torvalds } 1043b5810039SNick Piggin ptent = ptep_get_and_clear_full(mm, addr, pte, 1044a600388dSZachary Amsden tlb->fullmm); 10451da177e4SLinus Torvalds tlb_remove_tlb_entry(tlb, pte, addr); 10461da177e4SLinus Torvalds if (unlikely(!page)) 10471da177e4SLinus Torvalds continue; 1048eca56ff9SJerome Marchand 1049eca56ff9SJerome Marchand if (!PageAnon(page)) { 10501cf35d47SLinus Torvalds if (pte_dirty(ptent)) { 10511cf35d47SLinus Torvalds force_flush = 1; 10526237bcd9SHugh Dickins set_page_dirty(page); 10531cf35d47SLinus Torvalds } 10544917e5d0SJohannes Weiner if (pte_young(ptent) && 105564363aadSJoe Perches likely(!(vma->vm_flags & VM_SEQ_READ))) 1056bf3f3bc5SNick Piggin mark_page_accessed(page); 10576237bcd9SHugh Dickins } 1058eca56ff9SJerome Marchand rss[mm_counter(page)]--; 1059d281ee61SKirill A. Shutemov page_remove_rmap(page, false); 10603dc14741SHugh Dickins if (unlikely(page_mapcount(page) < 0)) 10613dc14741SHugh Dickins print_bad_pte(vma, addr, ptent, page); 1062e9d55e15SAneesh Kumar K.V if (unlikely(__tlb_remove_page(tlb, page))) { 10631cf35d47SLinus Torvalds force_flush = 1; 1064ce9ec37bSWill Deacon addr += PAGE_SIZE; 1065d16dfc55SPeter Zijlstra break; 10661cf35d47SLinus Torvalds } 10671da177e4SLinus Torvalds continue; 10681da177e4SLinus Torvalds } 10695042db43SJérôme Glisse 10705042db43SJérôme Glisse entry = pte_to_swp_entry(ptent); 10715042db43SJérôme Glisse if (non_swap_entry(entry) && is_device_private_entry(entry)) { 10725042db43SJérôme Glisse struct page *page = device_private_entry_to_page(entry); 10735042db43SJérôme Glisse 10745042db43SJérôme Glisse if (unlikely(details && details->check_mapping)) { 10755042db43SJérôme Glisse /* 10765042db43SJérôme Glisse * unmap_shared_mapping_pages() wants to 10775042db43SJérôme Glisse * invalidate cache without truncating: 10785042db43SJérôme Glisse * unmap shared but keep private pages. 10795042db43SJérôme Glisse */ 10805042db43SJérôme Glisse if (details->check_mapping != 10815042db43SJérôme Glisse page_rmapping(page)) 10825042db43SJérôme Glisse continue; 10835042db43SJérôme Glisse } 10845042db43SJérôme Glisse 10855042db43SJérôme Glisse pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); 10865042db43SJérôme Glisse rss[mm_counter(page)]--; 10875042db43SJérôme Glisse page_remove_rmap(page, false); 10885042db43SJérôme Glisse put_page(page); 10895042db43SJérôme Glisse continue; 10905042db43SJérôme Glisse } 10915042db43SJérôme Glisse 10923e8715fdSKirill A. Shutemov /* If details->check_mapping, we leave swap entries. */ 10933e8715fdSKirill A. Shutemov if (unlikely(details)) 10941da177e4SLinus Torvalds continue; 1095b084d435SKAMEZAWA Hiroyuki 10968a5f14a2SKirill A. Shutemov entry = pte_to_swp_entry(ptent); 1097b084d435SKAMEZAWA Hiroyuki if (!non_swap_entry(entry)) 1098b084d435SKAMEZAWA Hiroyuki rss[MM_SWAPENTS]--; 10999f9f1acdSKonstantin Khlebnikov else if (is_migration_entry(entry)) { 11009f9f1acdSKonstantin Khlebnikov struct page *page; 11019f9f1acdSKonstantin Khlebnikov 11029f9f1acdSKonstantin Khlebnikov page = migration_entry_to_page(entry); 1103eca56ff9SJerome Marchand rss[mm_counter(page)]--; 11049f9f1acdSKonstantin Khlebnikov } 1105b084d435SKAMEZAWA Hiroyuki if (unlikely(!free_swap_and_cache(entry))) 11062509ef26SHugh Dickins print_bad_pte(vma, addr, ptent, NULL); 11079888a1caSZachary Amsden pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); 110897a89413SPeter Zijlstra } while (pte++, addr += PAGE_SIZE, addr != end); 1109ae859762SHugh Dickins 1110d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(mm, rss); 11116606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 111251c6f666SRobin Holt 11131cf35d47SLinus Torvalds /* Do the actual TLB flush before dropping ptl */ 1114fb7332a9SWill Deacon if (force_flush) 11151cf35d47SLinus Torvalds tlb_flush_mmu_tlbonly(tlb); 11161cf35d47SLinus Torvalds pte_unmap_unlock(start_pte, ptl); 11171cf35d47SLinus Torvalds 11181cf35d47SLinus Torvalds /* 11191cf35d47SLinus Torvalds * If we forced a TLB flush (either due to running out of 11201cf35d47SLinus Torvalds * batch buffers or because we needed to flush dirty TLB 11211cf35d47SLinus Torvalds * entries before releasing the ptl), free the batched 11221cf35d47SLinus Torvalds * memory too. Restart if we didn't do everything. 11231cf35d47SLinus Torvalds */ 11241cf35d47SLinus Torvalds if (force_flush) { 11251cf35d47SLinus Torvalds force_flush = 0; 1126fa0aafb8SPeter Zijlstra tlb_flush_mmu(tlb); 11272b047252SLinus Torvalds if (addr != end) 1128d16dfc55SPeter Zijlstra goto again; 1129d16dfc55SPeter Zijlstra } 1130d16dfc55SPeter Zijlstra 113151c6f666SRobin Holt return addr; 11321da177e4SLinus Torvalds } 11331da177e4SLinus Torvalds 113451c6f666SRobin Holt static inline unsigned long zap_pmd_range(struct mmu_gather *tlb, 1135b5810039SNick Piggin struct vm_area_struct *vma, pud_t *pud, 11361da177e4SLinus Torvalds unsigned long addr, unsigned long end, 113797a89413SPeter Zijlstra struct zap_details *details) 11381da177e4SLinus Torvalds { 11391da177e4SLinus Torvalds pmd_t *pmd; 11401da177e4SLinus Torvalds unsigned long next; 11411da177e4SLinus Torvalds 11421da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 11431da177e4SLinus Torvalds do { 11441da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 114584c3fc4eSZi Yan if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) { 114653406ed1SHugh Dickins if (next - addr != HPAGE_PMD_SIZE) 1147fd60775aSDavid Rientjes __split_huge_pmd(vma, pmd, addr, false, NULL); 114853406ed1SHugh Dickins else if (zap_huge_pmd(tlb, vma, pmd, addr)) 11491a5a9906SAndrea Arcangeli goto next; 115071e3aac0SAndrea Arcangeli /* fall through */ 115171e3aac0SAndrea Arcangeli } 11521a5a9906SAndrea Arcangeli /* 11531a5a9906SAndrea Arcangeli * Here there can be other concurrent MADV_DONTNEED or 11541a5a9906SAndrea Arcangeli * trans huge page faults running, and if the pmd is 11551a5a9906SAndrea Arcangeli * none or trans huge it can change under us. This is 11561a5a9906SAndrea Arcangeli * because MADV_DONTNEED holds the mmap_sem in read 11571a5a9906SAndrea Arcangeli * mode. 11581a5a9906SAndrea Arcangeli */ 11591a5a9906SAndrea Arcangeli if (pmd_none_or_trans_huge_or_clear_bad(pmd)) 11601a5a9906SAndrea Arcangeli goto next; 116197a89413SPeter Zijlstra next = zap_pte_range(tlb, vma, pmd, addr, next, details); 11621a5a9906SAndrea Arcangeli next: 116397a89413SPeter Zijlstra cond_resched(); 116497a89413SPeter Zijlstra } while (pmd++, addr = next, addr != end); 116551c6f666SRobin Holt 116651c6f666SRobin Holt return addr; 11671da177e4SLinus Torvalds } 11681da177e4SLinus Torvalds 116951c6f666SRobin Holt static inline unsigned long zap_pud_range(struct mmu_gather *tlb, 1170c2febafcSKirill A. Shutemov struct vm_area_struct *vma, p4d_t *p4d, 11711da177e4SLinus Torvalds unsigned long addr, unsigned long end, 117297a89413SPeter Zijlstra struct zap_details *details) 11731da177e4SLinus Torvalds { 11741da177e4SLinus Torvalds pud_t *pud; 11751da177e4SLinus Torvalds unsigned long next; 11761da177e4SLinus Torvalds 1177c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 11781da177e4SLinus Torvalds do { 11791da177e4SLinus Torvalds next = pud_addr_end(addr, end); 1180a00cc7d9SMatthew Wilcox if (pud_trans_huge(*pud) || pud_devmap(*pud)) { 1181a00cc7d9SMatthew Wilcox if (next - addr != HPAGE_PUD_SIZE) { 1182a00cc7d9SMatthew Wilcox VM_BUG_ON_VMA(!rwsem_is_locked(&tlb->mm->mmap_sem), vma); 1183a00cc7d9SMatthew Wilcox split_huge_pud(vma, pud, addr); 1184a00cc7d9SMatthew Wilcox } else if (zap_huge_pud(tlb, vma, pud, addr)) 1185a00cc7d9SMatthew Wilcox goto next; 1186a00cc7d9SMatthew Wilcox /* fall through */ 1187a00cc7d9SMatthew Wilcox } 118897a89413SPeter Zijlstra if (pud_none_or_clear_bad(pud)) 11891da177e4SLinus Torvalds continue; 119097a89413SPeter Zijlstra next = zap_pmd_range(tlb, vma, pud, addr, next, details); 1191a00cc7d9SMatthew Wilcox next: 1192a00cc7d9SMatthew Wilcox cond_resched(); 119397a89413SPeter Zijlstra } while (pud++, addr = next, addr != end); 119451c6f666SRobin Holt 119551c6f666SRobin Holt return addr; 11961da177e4SLinus Torvalds } 11971da177e4SLinus Torvalds 1198c2febafcSKirill A. Shutemov static inline unsigned long zap_p4d_range(struct mmu_gather *tlb, 1199c2febafcSKirill A. Shutemov struct vm_area_struct *vma, pgd_t *pgd, 1200c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 1201c2febafcSKirill A. Shutemov struct zap_details *details) 1202c2febafcSKirill A. Shutemov { 1203c2febafcSKirill A. Shutemov p4d_t *p4d; 1204c2febafcSKirill A. Shutemov unsigned long next; 1205c2febafcSKirill A. Shutemov 1206c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 1207c2febafcSKirill A. Shutemov do { 1208c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 1209c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(p4d)) 1210c2febafcSKirill A. Shutemov continue; 1211c2febafcSKirill A. Shutemov next = zap_pud_range(tlb, vma, p4d, addr, next, details); 1212c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 1213c2febafcSKirill A. Shutemov 1214c2febafcSKirill A. Shutemov return addr; 1215c2febafcSKirill A. Shutemov } 1216c2febafcSKirill A. Shutemov 1217aac45363SMichal Hocko void unmap_page_range(struct mmu_gather *tlb, 121851c6f666SRobin Holt struct vm_area_struct *vma, 12191da177e4SLinus Torvalds unsigned long addr, unsigned long end, 122097a89413SPeter Zijlstra struct zap_details *details) 12211da177e4SLinus Torvalds { 12221da177e4SLinus Torvalds pgd_t *pgd; 12231da177e4SLinus Torvalds unsigned long next; 12241da177e4SLinus Torvalds 12251da177e4SLinus Torvalds BUG_ON(addr >= end); 12261da177e4SLinus Torvalds tlb_start_vma(tlb, vma); 12271da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 12281da177e4SLinus Torvalds do { 12291da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 123097a89413SPeter Zijlstra if (pgd_none_or_clear_bad(pgd)) 12311da177e4SLinus Torvalds continue; 1232c2febafcSKirill A. Shutemov next = zap_p4d_range(tlb, vma, pgd, addr, next, details); 123397a89413SPeter Zijlstra } while (pgd++, addr = next, addr != end); 12341da177e4SLinus Torvalds tlb_end_vma(tlb, vma); 12351da177e4SLinus Torvalds } 12361da177e4SLinus Torvalds 1237f5cc4eefSAl Viro 1238f5cc4eefSAl Viro static void unmap_single_vma(struct mmu_gather *tlb, 12391da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long start_addr, 12404f74d2c8SLinus Torvalds unsigned long end_addr, 12411da177e4SLinus Torvalds struct zap_details *details) 12421da177e4SLinus Torvalds { 1243f5cc4eefSAl Viro unsigned long start = max(vma->vm_start, start_addr); 12441da177e4SLinus Torvalds unsigned long end; 12451da177e4SLinus Torvalds 12461da177e4SLinus Torvalds if (start >= vma->vm_end) 1247f5cc4eefSAl Viro return; 12481da177e4SLinus Torvalds end = min(vma->vm_end, end_addr); 12491da177e4SLinus Torvalds if (end <= vma->vm_start) 1250f5cc4eefSAl Viro return; 12511da177e4SLinus Torvalds 1252cbc91f71SSrikar Dronamraju if (vma->vm_file) 1253cbc91f71SSrikar Dronamraju uprobe_munmap(vma, start, end); 1254cbc91f71SSrikar Dronamraju 1255b3b9c293SKonstantin Khlebnikov if (unlikely(vma->vm_flags & VM_PFNMAP)) 12565180da41SSuresh Siddha untrack_pfn(vma, 0, 0); 12572ab64037Svenkatesh.pallipadi@intel.com 12588b2a1238SAl Viro if (start != end) { 125951c6f666SRobin Holt if (unlikely(is_vm_hugetlb_page(vma))) { 1260a137e1ccSAndi Kleen /* 1261a137e1ccSAndi Kleen * It is undesirable to test vma->vm_file as it 1262a137e1ccSAndi Kleen * should be non-null for valid hugetlb area. 1263a137e1ccSAndi Kleen * However, vm_file will be NULL in the error 12647aa6b4adSDavidlohr Bueso * cleanup path of mmap_region. When 1265a137e1ccSAndi Kleen * hugetlbfs ->mmap method fails, 12667aa6b4adSDavidlohr Bueso * mmap_region() nullifies vma->vm_file 1267a137e1ccSAndi Kleen * before calling this function to clean up. 1268a137e1ccSAndi Kleen * Since no pte has actually been setup, it is 1269a137e1ccSAndi Kleen * safe to do nothing in this case. 1270a137e1ccSAndi Kleen */ 127124669e58SAneesh Kumar K.V if (vma->vm_file) { 127283cde9e8SDavidlohr Bueso i_mmap_lock_write(vma->vm_file->f_mapping); 1273d833352aSMel Gorman __unmap_hugepage_range_final(tlb, vma, start, end, NULL); 127483cde9e8SDavidlohr Bueso i_mmap_unlock_write(vma->vm_file->f_mapping); 127524669e58SAneesh Kumar K.V } 127651c6f666SRobin Holt } else 1277038c7aa1SAl Viro unmap_page_range(tlb, vma, start, end, details); 127897a89413SPeter Zijlstra } 127951c6f666SRobin Holt } 12801da177e4SLinus Torvalds 1281f5cc4eefSAl Viro /** 1282f5cc4eefSAl Viro * unmap_vmas - unmap a range of memory covered by a list of vma's 1283f5cc4eefSAl Viro * @tlb: address of the caller's struct mmu_gather 1284f5cc4eefSAl Viro * @vma: the starting vma 1285f5cc4eefSAl Viro * @start_addr: virtual address at which to start unmapping 1286f5cc4eefSAl Viro * @end_addr: virtual address at which to end unmapping 1287f5cc4eefSAl Viro * 1288f5cc4eefSAl Viro * Unmap all pages in the vma list. 1289f5cc4eefSAl Viro * 1290f5cc4eefSAl Viro * Only addresses between `start' and `end' will be unmapped. 1291f5cc4eefSAl Viro * 1292f5cc4eefSAl Viro * The VMA list must be sorted in ascending virtual address order. 1293f5cc4eefSAl Viro * 1294f5cc4eefSAl Viro * unmap_vmas() assumes that the caller will flush the whole unmapped address 1295f5cc4eefSAl Viro * range after unmap_vmas() returns. So the only responsibility here is to 1296f5cc4eefSAl Viro * ensure that any thus-far unmapped pages are flushed before unmap_vmas() 1297f5cc4eefSAl Viro * drops the lock and schedules. 1298f5cc4eefSAl Viro */ 1299f5cc4eefSAl Viro void unmap_vmas(struct mmu_gather *tlb, 1300f5cc4eefSAl Viro struct vm_area_struct *vma, unsigned long start_addr, 13014f74d2c8SLinus Torvalds unsigned long end_addr) 1302f5cc4eefSAl Viro { 1303ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 1304f5cc4eefSAl Viro 13056f4f13e8SJérôme Glisse mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma, vma->vm_mm, 13066f4f13e8SJérôme Glisse start_addr, end_addr); 1307ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 1308f5cc4eefSAl Viro for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) 13094f74d2c8SLinus Torvalds unmap_single_vma(tlb, vma, start_addr, end_addr, NULL); 1310ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 13111da177e4SLinus Torvalds } 13121da177e4SLinus Torvalds 13131da177e4SLinus Torvalds /** 13141da177e4SLinus Torvalds * zap_page_range - remove user pages in a given range 13151da177e4SLinus Torvalds * @vma: vm_area_struct holding the applicable pages 1316eb4546bbSRandy Dunlap * @start: starting address of pages to zap 13171da177e4SLinus Torvalds * @size: number of bytes to zap 1318f5cc4eefSAl Viro * 1319f5cc4eefSAl Viro * Caller must protect the VMA list 13201da177e4SLinus Torvalds */ 13217e027b14SLinus Torvalds void zap_page_range(struct vm_area_struct *vma, unsigned long start, 1322ecf1385dSKirill A. Shutemov unsigned long size) 13231da177e4SLinus Torvalds { 1324ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 1325d16dfc55SPeter Zijlstra struct mmu_gather tlb; 13261da177e4SLinus Torvalds 13271da177e4SLinus Torvalds lru_add_drain(); 13287269f999SJérôme Glisse mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm, 13296f4f13e8SJérôme Glisse start, start + size); 1330ac46d4f3SJérôme Glisse tlb_gather_mmu(&tlb, vma->vm_mm, start, range.end); 1331ac46d4f3SJérôme Glisse update_hiwater_rss(vma->vm_mm); 1332ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 1333ac46d4f3SJérôme Glisse for ( ; vma && vma->vm_start < range.end; vma = vma->vm_next) 1334ac46d4f3SJérôme Glisse unmap_single_vma(&tlb, vma, start, range.end, NULL); 1335ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 1336ac46d4f3SJérôme Glisse tlb_finish_mmu(&tlb, start, range.end); 13371da177e4SLinus Torvalds } 13381da177e4SLinus Torvalds 1339c627f9ccSJack Steiner /** 1340f5cc4eefSAl Viro * zap_page_range_single - remove user pages in a given range 1341f5cc4eefSAl Viro * @vma: vm_area_struct holding the applicable pages 1342f5cc4eefSAl Viro * @address: starting address of pages to zap 1343f5cc4eefSAl Viro * @size: number of bytes to zap 13448a5f14a2SKirill A. Shutemov * @details: details of shared cache invalidation 1345f5cc4eefSAl Viro * 1346f5cc4eefSAl Viro * The range must fit into one VMA. 1347f5cc4eefSAl Viro */ 1348f5cc4eefSAl Viro static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address, 1349f5cc4eefSAl Viro unsigned long size, struct zap_details *details) 1350f5cc4eefSAl Viro { 1351ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 1352f5cc4eefSAl Viro struct mmu_gather tlb; 1353f5cc4eefSAl Viro 1354f5cc4eefSAl Viro lru_add_drain(); 13557269f999SJérôme Glisse mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm, 13566f4f13e8SJérôme Glisse address, address + size); 1357ac46d4f3SJérôme Glisse tlb_gather_mmu(&tlb, vma->vm_mm, address, range.end); 1358ac46d4f3SJérôme Glisse update_hiwater_rss(vma->vm_mm); 1359ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 1360ac46d4f3SJérôme Glisse unmap_single_vma(&tlb, vma, address, range.end, details); 1361ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 1362ac46d4f3SJérôme Glisse tlb_finish_mmu(&tlb, address, range.end); 13631da177e4SLinus Torvalds } 13641da177e4SLinus Torvalds 1365c627f9ccSJack Steiner /** 1366c627f9ccSJack Steiner * zap_vma_ptes - remove ptes mapping the vma 1367c627f9ccSJack Steiner * @vma: vm_area_struct holding ptes to be zapped 1368c627f9ccSJack Steiner * @address: starting address of pages to zap 1369c627f9ccSJack Steiner * @size: number of bytes to zap 1370c627f9ccSJack Steiner * 1371c627f9ccSJack Steiner * This function only unmaps ptes assigned to VM_PFNMAP vmas. 1372c627f9ccSJack Steiner * 1373c627f9ccSJack Steiner * The entire address range must be fully contained within the vma. 1374c627f9ccSJack Steiner * 1375c627f9ccSJack Steiner */ 137627d036e3SLeon Romanovsky void zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, 1377c627f9ccSJack Steiner unsigned long size) 1378c627f9ccSJack Steiner { 1379c627f9ccSJack Steiner if (address < vma->vm_start || address + size > vma->vm_end || 1380c627f9ccSJack Steiner !(vma->vm_flags & VM_PFNMAP)) 138127d036e3SLeon Romanovsky return; 138227d036e3SLeon Romanovsky 1383f5cc4eefSAl Viro zap_page_range_single(vma, address, size, NULL); 1384c627f9ccSJack Steiner } 1385c627f9ccSJack Steiner EXPORT_SYMBOL_GPL(zap_vma_ptes); 1386c627f9ccSJack Steiner 138725ca1d6cSNamhyung Kim pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr, 1388920c7a5dSHarvey Harrison spinlock_t **ptl) 1389c9cfcddfSLinus Torvalds { 1390c2febafcSKirill A. Shutemov pgd_t *pgd; 1391c2febafcSKirill A. Shutemov p4d_t *p4d; 1392c2febafcSKirill A. Shutemov pud_t *pud; 1393c2febafcSKirill A. Shutemov pmd_t *pmd; 1394c2febafcSKirill A. Shutemov 1395c2febafcSKirill A. Shutemov pgd = pgd_offset(mm, addr); 1396c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, addr); 1397c2febafcSKirill A. Shutemov if (!p4d) 1398c2febafcSKirill A. Shutemov return NULL; 1399c2febafcSKirill A. Shutemov pud = pud_alloc(mm, p4d, addr); 1400c2febafcSKirill A. Shutemov if (!pud) 1401c2febafcSKirill A. Shutemov return NULL; 1402c2febafcSKirill A. Shutemov pmd = pmd_alloc(mm, pud, addr); 1403c2febafcSKirill A. Shutemov if (!pmd) 1404c2febafcSKirill A. Shutemov return NULL; 1405c2febafcSKirill A. Shutemov 1406f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 1407c9cfcddfSLinus Torvalds return pte_alloc_map_lock(mm, pmd, addr, ptl); 1408c9cfcddfSLinus Torvalds } 1409c9cfcddfSLinus Torvalds 14101da177e4SLinus Torvalds /* 1411238f58d8SLinus Torvalds * This is the old fallback for page remapping. 1412238f58d8SLinus Torvalds * 1413238f58d8SLinus Torvalds * For historical reasons, it only allows reserved pages. Only 1414238f58d8SLinus Torvalds * old drivers should use this, and they needed to mark their 1415238f58d8SLinus Torvalds * pages reserved for the old functions anyway. 1416238f58d8SLinus Torvalds */ 1417423bad60SNick Piggin static int insert_page(struct vm_area_struct *vma, unsigned long addr, 1418423bad60SNick Piggin struct page *page, pgprot_t prot) 1419238f58d8SLinus Torvalds { 1420423bad60SNick Piggin struct mm_struct *mm = vma->vm_mm; 1421238f58d8SLinus Torvalds int retval; 1422238f58d8SLinus Torvalds pte_t *pte; 1423238f58d8SLinus Torvalds spinlock_t *ptl; 1424238f58d8SLinus Torvalds 1425238f58d8SLinus Torvalds retval = -EINVAL; 14260ee930e6SMatthew Wilcox if (PageAnon(page) || PageSlab(page) || page_has_type(page)) 14275b4e655eSKAMEZAWA Hiroyuki goto out; 1428238f58d8SLinus Torvalds retval = -ENOMEM; 1429238f58d8SLinus Torvalds flush_dcache_page(page); 1430c9cfcddfSLinus Torvalds pte = get_locked_pte(mm, addr, &ptl); 1431238f58d8SLinus Torvalds if (!pte) 14325b4e655eSKAMEZAWA Hiroyuki goto out; 1433238f58d8SLinus Torvalds retval = -EBUSY; 1434238f58d8SLinus Torvalds if (!pte_none(*pte)) 1435238f58d8SLinus Torvalds goto out_unlock; 1436238f58d8SLinus Torvalds 1437238f58d8SLinus Torvalds /* Ok, finally just insert the thing.. */ 1438238f58d8SLinus Torvalds get_page(page); 1439eca56ff9SJerome Marchand inc_mm_counter_fast(mm, mm_counter_file(page)); 1440dd78feddSKirill A. Shutemov page_add_file_rmap(page, false); 1441238f58d8SLinus Torvalds set_pte_at(mm, addr, pte, mk_pte(page, prot)); 1442238f58d8SLinus Torvalds 1443238f58d8SLinus Torvalds retval = 0; 1444238f58d8SLinus Torvalds out_unlock: 1445238f58d8SLinus Torvalds pte_unmap_unlock(pte, ptl); 1446238f58d8SLinus Torvalds out: 1447238f58d8SLinus Torvalds return retval; 1448238f58d8SLinus Torvalds } 1449238f58d8SLinus Torvalds 1450bfa5bf6dSRolf Eike Beer /** 1451bfa5bf6dSRolf Eike Beer * vm_insert_page - insert single page into user vma 1452bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 1453bfa5bf6dSRolf Eike Beer * @addr: target user address of this page 1454bfa5bf6dSRolf Eike Beer * @page: source kernel page 1455bfa5bf6dSRolf Eike Beer * 1456a145dd41SLinus Torvalds * This allows drivers to insert individual pages they've allocated 1457a145dd41SLinus Torvalds * into a user vma. 1458a145dd41SLinus Torvalds * 1459a145dd41SLinus Torvalds * The page has to be a nice clean _individual_ kernel allocation. 1460a145dd41SLinus Torvalds * If you allocate a compound page, you need to have marked it as 1461a145dd41SLinus Torvalds * such (__GFP_COMP), or manually just split the page up yourself 14628dfcc9baSNick Piggin * (see split_page()). 1463a145dd41SLinus Torvalds * 1464a145dd41SLinus Torvalds * NOTE! Traditionally this was done with "remap_pfn_range()" which 1465a145dd41SLinus Torvalds * took an arbitrary page protection parameter. This doesn't allow 1466a145dd41SLinus Torvalds * that. Your vma protection will have to be set up correctly, which 1467a145dd41SLinus Torvalds * means that if you want a shared writable mapping, you'd better 1468a145dd41SLinus Torvalds * ask for a shared writable mapping! 1469a145dd41SLinus Torvalds * 1470a145dd41SLinus Torvalds * The page does not need to be reserved. 14714b6e1e37SKonstantin Khlebnikov * 14724b6e1e37SKonstantin Khlebnikov * Usually this function is called from f_op->mmap() handler 14734b6e1e37SKonstantin Khlebnikov * under mm->mmap_sem write-lock, so it can change vma->vm_flags. 14744b6e1e37SKonstantin Khlebnikov * Caller must set VM_MIXEDMAP on vma if it wants to call this 14754b6e1e37SKonstantin Khlebnikov * function from other places, for example from page-fault handler. 1476a862f68aSMike Rapoport * 1477a862f68aSMike Rapoport * Return: %0 on success, negative error code otherwise. 1478a145dd41SLinus Torvalds */ 1479423bad60SNick Piggin int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, 1480423bad60SNick Piggin struct page *page) 1481a145dd41SLinus Torvalds { 1482a145dd41SLinus Torvalds if (addr < vma->vm_start || addr >= vma->vm_end) 1483a145dd41SLinus Torvalds return -EFAULT; 1484a145dd41SLinus Torvalds if (!page_count(page)) 1485a145dd41SLinus Torvalds return -EINVAL; 14864b6e1e37SKonstantin Khlebnikov if (!(vma->vm_flags & VM_MIXEDMAP)) { 14874b6e1e37SKonstantin Khlebnikov BUG_ON(down_read_trylock(&vma->vm_mm->mmap_sem)); 14884b6e1e37SKonstantin Khlebnikov BUG_ON(vma->vm_flags & VM_PFNMAP); 14894b6e1e37SKonstantin Khlebnikov vma->vm_flags |= VM_MIXEDMAP; 14904b6e1e37SKonstantin Khlebnikov } 1491423bad60SNick Piggin return insert_page(vma, addr, page, vma->vm_page_prot); 1492a145dd41SLinus Torvalds } 1493e3c3374fSLinus Torvalds EXPORT_SYMBOL(vm_insert_page); 1494a145dd41SLinus Torvalds 1495a667d745SSouptick Joarder /* 1496a667d745SSouptick Joarder * __vm_map_pages - maps range of kernel pages into user vma 1497a667d745SSouptick Joarder * @vma: user vma to map to 1498a667d745SSouptick Joarder * @pages: pointer to array of source kernel pages 1499a667d745SSouptick Joarder * @num: number of pages in page array 1500a667d745SSouptick Joarder * @offset: user's requested vm_pgoff 1501a667d745SSouptick Joarder * 1502a667d745SSouptick Joarder * This allows drivers to map range of kernel pages into a user vma. 1503a667d745SSouptick Joarder * 1504a667d745SSouptick Joarder * Return: 0 on success and error code otherwise. 1505a667d745SSouptick Joarder */ 1506a667d745SSouptick Joarder static int __vm_map_pages(struct vm_area_struct *vma, struct page **pages, 1507a667d745SSouptick Joarder unsigned long num, unsigned long offset) 1508a667d745SSouptick Joarder { 1509a667d745SSouptick Joarder unsigned long count = vma_pages(vma); 1510a667d745SSouptick Joarder unsigned long uaddr = vma->vm_start; 1511a667d745SSouptick Joarder int ret, i; 1512a667d745SSouptick Joarder 1513a667d745SSouptick Joarder /* Fail if the user requested offset is beyond the end of the object */ 151496756fcbSMiguel Ojeda if (offset >= num) 1515a667d745SSouptick Joarder return -ENXIO; 1516a667d745SSouptick Joarder 1517a667d745SSouptick Joarder /* Fail if the user requested size exceeds available object size */ 1518a667d745SSouptick Joarder if (count > num - offset) 1519a667d745SSouptick Joarder return -ENXIO; 1520a667d745SSouptick Joarder 1521a667d745SSouptick Joarder for (i = 0; i < count; i++) { 1522a667d745SSouptick Joarder ret = vm_insert_page(vma, uaddr, pages[offset + i]); 1523a667d745SSouptick Joarder if (ret < 0) 1524a667d745SSouptick Joarder return ret; 1525a667d745SSouptick Joarder uaddr += PAGE_SIZE; 1526a667d745SSouptick Joarder } 1527a667d745SSouptick Joarder 1528a667d745SSouptick Joarder return 0; 1529a667d745SSouptick Joarder } 1530a667d745SSouptick Joarder 1531a667d745SSouptick Joarder /** 1532a667d745SSouptick Joarder * vm_map_pages - maps range of kernel pages starts with non zero offset 1533a667d745SSouptick Joarder * @vma: user vma to map to 1534a667d745SSouptick Joarder * @pages: pointer to array of source kernel pages 1535a667d745SSouptick Joarder * @num: number of pages in page array 1536a667d745SSouptick Joarder * 1537a667d745SSouptick Joarder * Maps an object consisting of @num pages, catering for the user's 1538a667d745SSouptick Joarder * requested vm_pgoff 1539a667d745SSouptick Joarder * 1540a667d745SSouptick Joarder * If we fail to insert any page into the vma, the function will return 1541a667d745SSouptick Joarder * immediately leaving any previously inserted pages present. Callers 1542a667d745SSouptick Joarder * from the mmap handler may immediately return the error as their caller 1543a667d745SSouptick Joarder * will destroy the vma, removing any successfully inserted pages. Other 1544a667d745SSouptick Joarder * callers should make their own arrangements for calling unmap_region(). 1545a667d745SSouptick Joarder * 1546a667d745SSouptick Joarder * Context: Process context. Called by mmap handlers. 1547a667d745SSouptick Joarder * Return: 0 on success and error code otherwise. 1548a667d745SSouptick Joarder */ 1549a667d745SSouptick Joarder int vm_map_pages(struct vm_area_struct *vma, struct page **pages, 1550a667d745SSouptick Joarder unsigned long num) 1551a667d745SSouptick Joarder { 1552a667d745SSouptick Joarder return __vm_map_pages(vma, pages, num, vma->vm_pgoff); 1553a667d745SSouptick Joarder } 1554a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages); 1555a667d745SSouptick Joarder 1556a667d745SSouptick Joarder /** 1557a667d745SSouptick Joarder * vm_map_pages_zero - map range of kernel pages starts with zero offset 1558a667d745SSouptick Joarder * @vma: user vma to map to 1559a667d745SSouptick Joarder * @pages: pointer to array of source kernel pages 1560a667d745SSouptick Joarder * @num: number of pages in page array 1561a667d745SSouptick Joarder * 1562a667d745SSouptick Joarder * Similar to vm_map_pages(), except that it explicitly sets the offset 1563a667d745SSouptick Joarder * to 0. This function is intended for the drivers that did not consider 1564a667d745SSouptick Joarder * vm_pgoff. 1565a667d745SSouptick Joarder * 1566a667d745SSouptick Joarder * Context: Process context. Called by mmap handlers. 1567a667d745SSouptick Joarder * Return: 0 on success and error code otherwise. 1568a667d745SSouptick Joarder */ 1569a667d745SSouptick Joarder int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages, 1570a667d745SSouptick Joarder unsigned long num) 1571a667d745SSouptick Joarder { 1572a667d745SSouptick Joarder return __vm_map_pages(vma, pages, num, 0); 1573a667d745SSouptick Joarder } 1574a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages_zero); 1575a667d745SSouptick Joarder 15769b5a8e00SMatthew Wilcox static vm_fault_t insert_pfn(struct vm_area_struct *vma, unsigned long addr, 1577b2770da6SRoss Zwisler pfn_t pfn, pgprot_t prot, bool mkwrite) 1578423bad60SNick Piggin { 1579423bad60SNick Piggin struct mm_struct *mm = vma->vm_mm; 1580423bad60SNick Piggin pte_t *pte, entry; 1581423bad60SNick Piggin spinlock_t *ptl; 1582423bad60SNick Piggin 1583423bad60SNick Piggin pte = get_locked_pte(mm, addr, &ptl); 1584423bad60SNick Piggin if (!pte) 15859b5a8e00SMatthew Wilcox return VM_FAULT_OOM; 1586b2770da6SRoss Zwisler if (!pte_none(*pte)) { 1587b2770da6SRoss Zwisler if (mkwrite) { 1588b2770da6SRoss Zwisler /* 1589b2770da6SRoss Zwisler * For read faults on private mappings the PFN passed 1590b2770da6SRoss Zwisler * in may not match the PFN we have mapped if the 1591b2770da6SRoss Zwisler * mapped PFN is a writeable COW page. In the mkwrite 1592b2770da6SRoss Zwisler * case we are creating a writable PTE for a shared 1593f2c57d91SJan Kara * mapping and we expect the PFNs to match. If they 1594f2c57d91SJan Kara * don't match, we are likely racing with block 1595f2c57d91SJan Kara * allocation and mapping invalidation so just skip the 1596f2c57d91SJan Kara * update. 1597b2770da6SRoss Zwisler */ 1598f2c57d91SJan Kara if (pte_pfn(*pte) != pfn_t_to_pfn(pfn)) { 1599f2c57d91SJan Kara WARN_ON_ONCE(!is_zero_pfn(pte_pfn(*pte))); 1600423bad60SNick Piggin goto out_unlock; 1601f2c57d91SJan Kara } 1602cae85cb8SJan Kara entry = pte_mkyoung(*pte); 1603cae85cb8SJan Kara entry = maybe_mkwrite(pte_mkdirty(entry), vma); 1604cae85cb8SJan Kara if (ptep_set_access_flags(vma, addr, pte, entry, 1)) 1605cae85cb8SJan Kara update_mmu_cache(vma, addr, pte); 1606cae85cb8SJan Kara } 1607b2770da6SRoss Zwisler goto out_unlock; 1608b2770da6SRoss Zwisler } 1609423bad60SNick Piggin 1610423bad60SNick Piggin /* Ok, finally just insert the thing.. */ 161101c8f1c4SDan Williams if (pfn_t_devmap(pfn)) 161201c8f1c4SDan Williams entry = pte_mkdevmap(pfn_t_pte(pfn, prot)); 161301c8f1c4SDan Williams else 161401c8f1c4SDan Williams entry = pte_mkspecial(pfn_t_pte(pfn, prot)); 1615b2770da6SRoss Zwisler 1616b2770da6SRoss Zwisler if (mkwrite) { 1617b2770da6SRoss Zwisler entry = pte_mkyoung(entry); 1618b2770da6SRoss Zwisler entry = maybe_mkwrite(pte_mkdirty(entry), vma); 1619b2770da6SRoss Zwisler } 1620b2770da6SRoss Zwisler 1621423bad60SNick Piggin set_pte_at(mm, addr, pte, entry); 16224b3073e1SRussell King update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */ 1623423bad60SNick Piggin 1624423bad60SNick Piggin out_unlock: 1625423bad60SNick Piggin pte_unmap_unlock(pte, ptl); 16269b5a8e00SMatthew Wilcox return VM_FAULT_NOPAGE; 1627423bad60SNick Piggin } 1628423bad60SNick Piggin 1629f5e6d1d5SMatthew Wilcox /** 1630f5e6d1d5SMatthew Wilcox * vmf_insert_pfn_prot - insert single pfn into user vma with specified pgprot 1631f5e6d1d5SMatthew Wilcox * @vma: user vma to map to 1632f5e6d1d5SMatthew Wilcox * @addr: target user address of this page 1633f5e6d1d5SMatthew Wilcox * @pfn: source kernel pfn 1634f5e6d1d5SMatthew Wilcox * @pgprot: pgprot flags for the inserted page 1635f5e6d1d5SMatthew Wilcox * 1636f5e6d1d5SMatthew Wilcox * This is exactly like vmf_insert_pfn(), except that it allows drivers to 1637f5e6d1d5SMatthew Wilcox * to override pgprot on a per-page basis. 1638f5e6d1d5SMatthew Wilcox * 1639f5e6d1d5SMatthew Wilcox * This only makes sense for IO mappings, and it makes no sense for 1640f5e6d1d5SMatthew Wilcox * COW mappings. In general, using multiple vmas is preferable; 1641ae2b01f3SMatthew Wilcox * vmf_insert_pfn_prot should only be used if using multiple VMAs is 1642f5e6d1d5SMatthew Wilcox * impractical. 1643f5e6d1d5SMatthew Wilcox * 1644ae2b01f3SMatthew Wilcox * Context: Process context. May allocate using %GFP_KERNEL. 1645f5e6d1d5SMatthew Wilcox * Return: vm_fault_t value. 1646f5e6d1d5SMatthew Wilcox */ 1647f5e6d1d5SMatthew Wilcox vm_fault_t vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr, 1648f5e6d1d5SMatthew Wilcox unsigned long pfn, pgprot_t pgprot) 1649f5e6d1d5SMatthew Wilcox { 16506d958546SMatthew Wilcox /* 16516d958546SMatthew Wilcox * Technically, architectures with pte_special can avoid all these 16526d958546SMatthew Wilcox * restrictions (same for remap_pfn_range). However we would like 16536d958546SMatthew Wilcox * consistency in testing and feature parity among all, so we should 16546d958546SMatthew Wilcox * try to keep these invariants in place for everybody. 16556d958546SMatthew Wilcox */ 16566d958546SMatthew Wilcox BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))); 16576d958546SMatthew Wilcox BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) == 16586d958546SMatthew Wilcox (VM_PFNMAP|VM_MIXEDMAP)); 16596d958546SMatthew Wilcox BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); 16606d958546SMatthew Wilcox BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn)); 16616d958546SMatthew Wilcox 16626d958546SMatthew Wilcox if (addr < vma->vm_start || addr >= vma->vm_end) 16636d958546SMatthew Wilcox return VM_FAULT_SIGBUS; 16646d958546SMatthew Wilcox 16656d958546SMatthew Wilcox if (!pfn_modify_allowed(pfn, pgprot)) 16666d958546SMatthew Wilcox return VM_FAULT_SIGBUS; 16676d958546SMatthew Wilcox 16686d958546SMatthew Wilcox track_pfn_insert(vma, &pgprot, __pfn_to_pfn_t(pfn, PFN_DEV)); 16696d958546SMatthew Wilcox 16709b5a8e00SMatthew Wilcox return insert_pfn(vma, addr, __pfn_to_pfn_t(pfn, PFN_DEV), pgprot, 16716d958546SMatthew Wilcox false); 1672f5e6d1d5SMatthew Wilcox } 1673f5e6d1d5SMatthew Wilcox EXPORT_SYMBOL(vmf_insert_pfn_prot); 1674e0dc0d8fSNick Piggin 1675ae2b01f3SMatthew Wilcox /** 1676ae2b01f3SMatthew Wilcox * vmf_insert_pfn - insert single pfn into user vma 1677ae2b01f3SMatthew Wilcox * @vma: user vma to map to 1678ae2b01f3SMatthew Wilcox * @addr: target user address of this page 1679ae2b01f3SMatthew Wilcox * @pfn: source kernel pfn 1680ae2b01f3SMatthew Wilcox * 1681ae2b01f3SMatthew Wilcox * Similar to vm_insert_page, this allows drivers to insert individual pages 1682ae2b01f3SMatthew Wilcox * they've allocated into a user vma. Same comments apply. 1683ae2b01f3SMatthew Wilcox * 1684ae2b01f3SMatthew Wilcox * This function should only be called from a vm_ops->fault handler, and 1685ae2b01f3SMatthew Wilcox * in that case the handler should return the result of this function. 1686ae2b01f3SMatthew Wilcox * 1687ae2b01f3SMatthew Wilcox * vma cannot be a COW mapping. 1688ae2b01f3SMatthew Wilcox * 1689ae2b01f3SMatthew Wilcox * As this is called only for pages that do not currently exist, we 1690ae2b01f3SMatthew Wilcox * do not need to flush old virtual caches or the TLB. 1691ae2b01f3SMatthew Wilcox * 1692ae2b01f3SMatthew Wilcox * Context: Process context. May allocate using %GFP_KERNEL. 1693ae2b01f3SMatthew Wilcox * Return: vm_fault_t value. 1694ae2b01f3SMatthew Wilcox */ 1695ae2b01f3SMatthew Wilcox vm_fault_t vmf_insert_pfn(struct vm_area_struct *vma, unsigned long addr, 1696ae2b01f3SMatthew Wilcox unsigned long pfn) 1697ae2b01f3SMatthew Wilcox { 1698ae2b01f3SMatthew Wilcox return vmf_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot); 1699ae2b01f3SMatthew Wilcox } 1700ae2b01f3SMatthew Wilcox EXPORT_SYMBOL(vmf_insert_pfn); 1701ae2b01f3SMatthew Wilcox 1702785a3fabSDan Williams static bool vm_mixed_ok(struct vm_area_struct *vma, pfn_t pfn) 1703785a3fabSDan Williams { 1704785a3fabSDan Williams /* these checks mirror the abort conditions in vm_normal_page */ 1705785a3fabSDan Williams if (vma->vm_flags & VM_MIXEDMAP) 1706785a3fabSDan Williams return true; 1707785a3fabSDan Williams if (pfn_t_devmap(pfn)) 1708785a3fabSDan Williams return true; 1709785a3fabSDan Williams if (pfn_t_special(pfn)) 1710785a3fabSDan Williams return true; 1711785a3fabSDan Williams if (is_zero_pfn(pfn_t_to_pfn(pfn))) 1712785a3fabSDan Williams return true; 1713785a3fabSDan Williams return false; 1714785a3fabSDan Williams } 1715785a3fabSDan Williams 171679f3aa5bSMatthew Wilcox static vm_fault_t __vm_insert_mixed(struct vm_area_struct *vma, 171779f3aa5bSMatthew Wilcox unsigned long addr, pfn_t pfn, bool mkwrite) 1718423bad60SNick Piggin { 171987744ab3SDan Williams pgprot_t pgprot = vma->vm_page_prot; 172079f3aa5bSMatthew Wilcox int err; 172187744ab3SDan Williams 1722785a3fabSDan Williams BUG_ON(!vm_mixed_ok(vma, pfn)); 1723423bad60SNick Piggin 1724423bad60SNick Piggin if (addr < vma->vm_start || addr >= vma->vm_end) 172579f3aa5bSMatthew Wilcox return VM_FAULT_SIGBUS; 1726308a047cSBorislav Petkov 1727308a047cSBorislav Petkov track_pfn_insert(vma, &pgprot, pfn); 1728423bad60SNick Piggin 172942e4089cSAndi Kleen if (!pfn_modify_allowed(pfn_t_to_pfn(pfn), pgprot)) 173079f3aa5bSMatthew Wilcox return VM_FAULT_SIGBUS; 173142e4089cSAndi Kleen 1732423bad60SNick Piggin /* 1733423bad60SNick Piggin * If we don't have pte special, then we have to use the pfn_valid() 1734423bad60SNick Piggin * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must* 1735423bad60SNick Piggin * refcount the page if pfn_valid is true (hence insert_page rather 173662eede62SHugh Dickins * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP 173762eede62SHugh Dickins * without pte special, it would there be refcounted as a normal page. 1738423bad60SNick Piggin */ 173900b3a331SLaurent Dufour if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) && 174000b3a331SLaurent Dufour !pfn_t_devmap(pfn) && pfn_t_valid(pfn)) { 1741423bad60SNick Piggin struct page *page; 1742423bad60SNick Piggin 174303fc2da6SDan Williams /* 174403fc2da6SDan Williams * At this point we are committed to insert_page() 174503fc2da6SDan Williams * regardless of whether the caller specified flags that 174603fc2da6SDan Williams * result in pfn_t_has_page() == false. 174703fc2da6SDan Williams */ 174803fc2da6SDan Williams page = pfn_to_page(pfn_t_to_pfn(pfn)); 174979f3aa5bSMatthew Wilcox err = insert_page(vma, addr, page, pgprot); 175079f3aa5bSMatthew Wilcox } else { 17519b5a8e00SMatthew Wilcox return insert_pfn(vma, addr, pfn, pgprot, mkwrite); 1752423bad60SNick Piggin } 1753b2770da6SRoss Zwisler 17545d747637SMatthew Wilcox if (err == -ENOMEM) 17555d747637SMatthew Wilcox return VM_FAULT_OOM; 17565d747637SMatthew Wilcox if (err < 0 && err != -EBUSY) 17575d747637SMatthew Wilcox return VM_FAULT_SIGBUS; 17585d747637SMatthew Wilcox 17595d747637SMatthew Wilcox return VM_FAULT_NOPAGE; 1760423bad60SNick Piggin } 176179f3aa5bSMatthew Wilcox 176279f3aa5bSMatthew Wilcox vm_fault_t vmf_insert_mixed(struct vm_area_struct *vma, unsigned long addr, 176379f3aa5bSMatthew Wilcox pfn_t pfn) 176479f3aa5bSMatthew Wilcox { 176579f3aa5bSMatthew Wilcox return __vm_insert_mixed(vma, addr, pfn, false); 176679f3aa5bSMatthew Wilcox } 17675d747637SMatthew Wilcox EXPORT_SYMBOL(vmf_insert_mixed); 1768423bad60SNick Piggin 1769ab77dab4SSouptick Joarder /* 1770ab77dab4SSouptick Joarder * If the insertion of PTE failed because someone else already added a 1771ab77dab4SSouptick Joarder * different entry in the mean time, we treat that as success as we assume 1772ab77dab4SSouptick Joarder * the same entry was actually inserted. 1773ab77dab4SSouptick Joarder */ 1774ab77dab4SSouptick Joarder vm_fault_t vmf_insert_mixed_mkwrite(struct vm_area_struct *vma, 1775ab77dab4SSouptick Joarder unsigned long addr, pfn_t pfn) 1776b2770da6SRoss Zwisler { 177779f3aa5bSMatthew Wilcox return __vm_insert_mixed(vma, addr, pfn, true); 1778b2770da6SRoss Zwisler } 1779ab77dab4SSouptick Joarder EXPORT_SYMBOL(vmf_insert_mixed_mkwrite); 1780b2770da6SRoss Zwisler 1781a145dd41SLinus Torvalds /* 17821da177e4SLinus Torvalds * maps a range of physical memory into the requested pages. the old 17831da177e4SLinus Torvalds * mappings are removed. any references to nonexistent pages results 17841da177e4SLinus Torvalds * in null mappings (currently treated as "copy-on-access") 17851da177e4SLinus Torvalds */ 17861da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd, 17871da177e4SLinus Torvalds unsigned long addr, unsigned long end, 17881da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 17891da177e4SLinus Torvalds { 17901da177e4SLinus Torvalds pte_t *pte; 1791c74df32cSHugh Dickins spinlock_t *ptl; 179242e4089cSAndi Kleen int err = 0; 17931da177e4SLinus Torvalds 1794c74df32cSHugh Dickins pte = pte_alloc_map_lock(mm, pmd, addr, &ptl); 17951da177e4SLinus Torvalds if (!pte) 17961da177e4SLinus Torvalds return -ENOMEM; 17976606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 17981da177e4SLinus Torvalds do { 17991da177e4SLinus Torvalds BUG_ON(!pte_none(*pte)); 180042e4089cSAndi Kleen if (!pfn_modify_allowed(pfn, prot)) { 180142e4089cSAndi Kleen err = -EACCES; 180242e4089cSAndi Kleen break; 180342e4089cSAndi Kleen } 18047e675137SNick Piggin set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot))); 18051da177e4SLinus Torvalds pfn++; 18061da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 18076606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 1808c74df32cSHugh Dickins pte_unmap_unlock(pte - 1, ptl); 180942e4089cSAndi Kleen return err; 18101da177e4SLinus Torvalds } 18111da177e4SLinus Torvalds 18121da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud, 18131da177e4SLinus Torvalds unsigned long addr, unsigned long end, 18141da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 18151da177e4SLinus Torvalds { 18161da177e4SLinus Torvalds pmd_t *pmd; 18171da177e4SLinus Torvalds unsigned long next; 181842e4089cSAndi Kleen int err; 18191da177e4SLinus Torvalds 18201da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 18211da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, addr); 18221da177e4SLinus Torvalds if (!pmd) 18231da177e4SLinus Torvalds return -ENOMEM; 1824f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 18251da177e4SLinus Torvalds do { 18261da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 182742e4089cSAndi Kleen err = remap_pte_range(mm, pmd, addr, next, 182842e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 182942e4089cSAndi Kleen if (err) 183042e4089cSAndi Kleen return err; 18311da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 18321da177e4SLinus Torvalds return 0; 18331da177e4SLinus Torvalds } 18341da177e4SLinus Torvalds 1835c2febafcSKirill A. Shutemov static inline int remap_pud_range(struct mm_struct *mm, p4d_t *p4d, 18361da177e4SLinus Torvalds unsigned long addr, unsigned long end, 18371da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 18381da177e4SLinus Torvalds { 18391da177e4SLinus Torvalds pud_t *pud; 18401da177e4SLinus Torvalds unsigned long next; 184142e4089cSAndi Kleen int err; 18421da177e4SLinus Torvalds 18431da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 1844c2febafcSKirill A. Shutemov pud = pud_alloc(mm, p4d, addr); 18451da177e4SLinus Torvalds if (!pud) 18461da177e4SLinus Torvalds return -ENOMEM; 18471da177e4SLinus Torvalds do { 18481da177e4SLinus Torvalds next = pud_addr_end(addr, end); 184942e4089cSAndi Kleen err = remap_pmd_range(mm, pud, addr, next, 185042e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 185142e4089cSAndi Kleen if (err) 185242e4089cSAndi Kleen return err; 18531da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 18541da177e4SLinus Torvalds return 0; 18551da177e4SLinus Torvalds } 18561da177e4SLinus Torvalds 1857c2febafcSKirill A. Shutemov static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd, 1858c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 1859c2febafcSKirill A. Shutemov unsigned long pfn, pgprot_t prot) 1860c2febafcSKirill A. Shutemov { 1861c2febafcSKirill A. Shutemov p4d_t *p4d; 1862c2febafcSKirill A. Shutemov unsigned long next; 186342e4089cSAndi Kleen int err; 1864c2febafcSKirill A. Shutemov 1865c2febafcSKirill A. Shutemov pfn -= addr >> PAGE_SHIFT; 1866c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, addr); 1867c2febafcSKirill A. Shutemov if (!p4d) 1868c2febafcSKirill A. Shutemov return -ENOMEM; 1869c2febafcSKirill A. Shutemov do { 1870c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 187142e4089cSAndi Kleen err = remap_pud_range(mm, p4d, addr, next, 187242e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 187342e4089cSAndi Kleen if (err) 187442e4089cSAndi Kleen return err; 1875c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 1876c2febafcSKirill A. Shutemov return 0; 1877c2febafcSKirill A. Shutemov } 1878c2febafcSKirill A. Shutemov 1879bfa5bf6dSRolf Eike Beer /** 1880bfa5bf6dSRolf Eike Beer * remap_pfn_range - remap kernel memory to userspace 1881bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 1882bfa5bf6dSRolf Eike Beer * @addr: target user address to start at 1883bfa5bf6dSRolf Eike Beer * @pfn: physical address of kernel memory 1884bfa5bf6dSRolf Eike Beer * @size: size of map area 1885bfa5bf6dSRolf Eike Beer * @prot: page protection flags for this mapping 1886bfa5bf6dSRolf Eike Beer * 1887bfa5bf6dSRolf Eike Beer * Note: this is only safe if the mm semaphore is held when called. 1888a862f68aSMike Rapoport * 1889a862f68aSMike Rapoport * Return: %0 on success, negative error code otherwise. 1890bfa5bf6dSRolf Eike Beer */ 18911da177e4SLinus Torvalds int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 18921da177e4SLinus Torvalds unsigned long pfn, unsigned long size, pgprot_t prot) 18931da177e4SLinus Torvalds { 18941da177e4SLinus Torvalds pgd_t *pgd; 18951da177e4SLinus Torvalds unsigned long next; 18962d15cab8SHugh Dickins unsigned long end = addr + PAGE_ALIGN(size); 18971da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 1898d5957d2fSYongji Xie unsigned long remap_pfn = pfn; 18991da177e4SLinus Torvalds int err; 19001da177e4SLinus Torvalds 19011da177e4SLinus Torvalds /* 19021da177e4SLinus Torvalds * Physically remapped pages are special. Tell the 19031da177e4SLinus Torvalds * rest of the world about it: 19041da177e4SLinus Torvalds * VM_IO tells people not to look at these pages 19051da177e4SLinus Torvalds * (accesses can have side effects). 19066aab341eSLinus Torvalds * VM_PFNMAP tells the core MM that the base pages are just 19076aab341eSLinus Torvalds * raw PFN mappings, and do not have a "struct page" associated 19086aab341eSLinus Torvalds * with them. 1909314e51b9SKonstantin Khlebnikov * VM_DONTEXPAND 1910314e51b9SKonstantin Khlebnikov * Disable vma merging and expanding with mremap(). 1911314e51b9SKonstantin Khlebnikov * VM_DONTDUMP 1912314e51b9SKonstantin Khlebnikov * Omit vma from core dump, even when VM_IO turned off. 1913fb155c16SLinus Torvalds * 1914fb155c16SLinus Torvalds * There's a horrible special case to handle copy-on-write 1915fb155c16SLinus Torvalds * behaviour that some programs depend on. We mark the "original" 1916fb155c16SLinus Torvalds * un-COW'ed pages by matching them up with "vma->vm_pgoff". 1917b3b9c293SKonstantin Khlebnikov * See vm_normal_page() for details. 19181da177e4SLinus Torvalds */ 1919b3b9c293SKonstantin Khlebnikov if (is_cow_mapping(vma->vm_flags)) { 1920b3b9c293SKonstantin Khlebnikov if (addr != vma->vm_start || end != vma->vm_end) 1921b3b9c293SKonstantin Khlebnikov return -EINVAL; 19226aab341eSLinus Torvalds vma->vm_pgoff = pfn; 1923b3b9c293SKonstantin Khlebnikov } 1924b3b9c293SKonstantin Khlebnikov 1925d5957d2fSYongji Xie err = track_pfn_remap(vma, &prot, remap_pfn, addr, PAGE_ALIGN(size)); 1926b3b9c293SKonstantin Khlebnikov if (err) 19273c8bb73aSvenkatesh.pallipadi@intel.com return -EINVAL; 1928fb155c16SLinus Torvalds 1929314e51b9SKonstantin Khlebnikov vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; 19301da177e4SLinus Torvalds 19311da177e4SLinus Torvalds BUG_ON(addr >= end); 19321da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 19331da177e4SLinus Torvalds pgd = pgd_offset(mm, addr); 19341da177e4SLinus Torvalds flush_cache_range(vma, addr, end); 19351da177e4SLinus Torvalds do { 19361da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 1937c2febafcSKirill A. Shutemov err = remap_p4d_range(mm, pgd, addr, next, 19381da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot); 19391da177e4SLinus Torvalds if (err) 19401da177e4SLinus Torvalds break; 19411da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 19422ab64037Svenkatesh.pallipadi@intel.com 19432ab64037Svenkatesh.pallipadi@intel.com if (err) 1944d5957d2fSYongji Xie untrack_pfn(vma, remap_pfn, PAGE_ALIGN(size)); 19452ab64037Svenkatesh.pallipadi@intel.com 19461da177e4SLinus Torvalds return err; 19471da177e4SLinus Torvalds } 19481da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range); 19491da177e4SLinus Torvalds 1950b4cbb197SLinus Torvalds /** 1951b4cbb197SLinus Torvalds * vm_iomap_memory - remap memory to userspace 1952b4cbb197SLinus Torvalds * @vma: user vma to map to 1953b4cbb197SLinus Torvalds * @start: start of area 1954b4cbb197SLinus Torvalds * @len: size of area 1955b4cbb197SLinus Torvalds * 1956b4cbb197SLinus Torvalds * This is a simplified io_remap_pfn_range() for common driver use. The 1957b4cbb197SLinus Torvalds * driver just needs to give us the physical memory range to be mapped, 1958b4cbb197SLinus Torvalds * we'll figure out the rest from the vma information. 1959b4cbb197SLinus Torvalds * 1960b4cbb197SLinus Torvalds * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get 1961b4cbb197SLinus Torvalds * whatever write-combining details or similar. 1962a862f68aSMike Rapoport * 1963a862f68aSMike Rapoport * Return: %0 on success, negative error code otherwise. 1964b4cbb197SLinus Torvalds */ 1965b4cbb197SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len) 1966b4cbb197SLinus Torvalds { 1967b4cbb197SLinus Torvalds unsigned long vm_len, pfn, pages; 1968b4cbb197SLinus Torvalds 1969b4cbb197SLinus Torvalds /* Check that the physical memory area passed in looks valid */ 1970b4cbb197SLinus Torvalds if (start + len < start) 1971b4cbb197SLinus Torvalds return -EINVAL; 1972b4cbb197SLinus Torvalds /* 1973b4cbb197SLinus Torvalds * You *really* shouldn't map things that aren't page-aligned, 1974b4cbb197SLinus Torvalds * but we've historically allowed it because IO memory might 1975b4cbb197SLinus Torvalds * just have smaller alignment. 1976b4cbb197SLinus Torvalds */ 1977b4cbb197SLinus Torvalds len += start & ~PAGE_MASK; 1978b4cbb197SLinus Torvalds pfn = start >> PAGE_SHIFT; 1979b4cbb197SLinus Torvalds pages = (len + ~PAGE_MASK) >> PAGE_SHIFT; 1980b4cbb197SLinus Torvalds if (pfn + pages < pfn) 1981b4cbb197SLinus Torvalds return -EINVAL; 1982b4cbb197SLinus Torvalds 1983b4cbb197SLinus Torvalds /* We start the mapping 'vm_pgoff' pages into the area */ 1984b4cbb197SLinus Torvalds if (vma->vm_pgoff > pages) 1985b4cbb197SLinus Torvalds return -EINVAL; 1986b4cbb197SLinus Torvalds pfn += vma->vm_pgoff; 1987b4cbb197SLinus Torvalds pages -= vma->vm_pgoff; 1988b4cbb197SLinus Torvalds 1989b4cbb197SLinus Torvalds /* Can we fit all of the mapping? */ 1990b4cbb197SLinus Torvalds vm_len = vma->vm_end - vma->vm_start; 1991b4cbb197SLinus Torvalds if (vm_len >> PAGE_SHIFT > pages) 1992b4cbb197SLinus Torvalds return -EINVAL; 1993b4cbb197SLinus Torvalds 1994b4cbb197SLinus Torvalds /* Ok, let it rip */ 1995b4cbb197SLinus Torvalds return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot); 1996b4cbb197SLinus Torvalds } 1997b4cbb197SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory); 1998b4cbb197SLinus Torvalds 1999aee16b3cSJeremy Fitzhardinge static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd, 2000aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2001aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 2002aee16b3cSJeremy Fitzhardinge { 2003aee16b3cSJeremy Fitzhardinge pte_t *pte; 2004aee16b3cSJeremy Fitzhardinge int err; 200594909914SBorislav Petkov spinlock_t *uninitialized_var(ptl); 2006aee16b3cSJeremy Fitzhardinge 2007aee16b3cSJeremy Fitzhardinge pte = (mm == &init_mm) ? 2008aee16b3cSJeremy Fitzhardinge pte_alloc_kernel(pmd, addr) : 2009aee16b3cSJeremy Fitzhardinge pte_alloc_map_lock(mm, pmd, addr, &ptl); 2010aee16b3cSJeremy Fitzhardinge if (!pte) 2011aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2012aee16b3cSJeremy Fitzhardinge 2013aee16b3cSJeremy Fitzhardinge BUG_ON(pmd_huge(*pmd)); 2014aee16b3cSJeremy Fitzhardinge 201538e0edb1SJeremy Fitzhardinge arch_enter_lazy_mmu_mode(); 201638e0edb1SJeremy Fitzhardinge 2017aee16b3cSJeremy Fitzhardinge do { 20188b1e0f81SAnshuman Khandual err = fn(pte++, addr, data); 2019aee16b3cSJeremy Fitzhardinge if (err) 2020aee16b3cSJeremy Fitzhardinge break; 2021c36987e2SDaisuke Nishimura } while (addr += PAGE_SIZE, addr != end); 2022aee16b3cSJeremy Fitzhardinge 202338e0edb1SJeremy Fitzhardinge arch_leave_lazy_mmu_mode(); 202438e0edb1SJeremy Fitzhardinge 2025aee16b3cSJeremy Fitzhardinge if (mm != &init_mm) 2026aee16b3cSJeremy Fitzhardinge pte_unmap_unlock(pte-1, ptl); 2027aee16b3cSJeremy Fitzhardinge return err; 2028aee16b3cSJeremy Fitzhardinge } 2029aee16b3cSJeremy Fitzhardinge 2030aee16b3cSJeremy Fitzhardinge static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud, 2031aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2032aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 2033aee16b3cSJeremy Fitzhardinge { 2034aee16b3cSJeremy Fitzhardinge pmd_t *pmd; 2035aee16b3cSJeremy Fitzhardinge unsigned long next; 2036aee16b3cSJeremy Fitzhardinge int err; 2037aee16b3cSJeremy Fitzhardinge 2038ceb86879SAndi Kleen BUG_ON(pud_huge(*pud)); 2039ceb86879SAndi Kleen 2040aee16b3cSJeremy Fitzhardinge pmd = pmd_alloc(mm, pud, addr); 2041aee16b3cSJeremy Fitzhardinge if (!pmd) 2042aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2043aee16b3cSJeremy Fitzhardinge do { 2044aee16b3cSJeremy Fitzhardinge next = pmd_addr_end(addr, end); 2045aee16b3cSJeremy Fitzhardinge err = apply_to_pte_range(mm, pmd, addr, next, fn, data); 2046aee16b3cSJeremy Fitzhardinge if (err) 2047aee16b3cSJeremy Fitzhardinge break; 2048aee16b3cSJeremy Fitzhardinge } while (pmd++, addr = next, addr != end); 2049aee16b3cSJeremy Fitzhardinge return err; 2050aee16b3cSJeremy Fitzhardinge } 2051aee16b3cSJeremy Fitzhardinge 2052c2febafcSKirill A. Shutemov static int apply_to_pud_range(struct mm_struct *mm, p4d_t *p4d, 2053aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2054aee16b3cSJeremy Fitzhardinge pte_fn_t fn, void *data) 2055aee16b3cSJeremy Fitzhardinge { 2056aee16b3cSJeremy Fitzhardinge pud_t *pud; 2057aee16b3cSJeremy Fitzhardinge unsigned long next; 2058aee16b3cSJeremy Fitzhardinge int err; 2059aee16b3cSJeremy Fitzhardinge 2060c2febafcSKirill A. Shutemov pud = pud_alloc(mm, p4d, addr); 2061aee16b3cSJeremy Fitzhardinge if (!pud) 2062aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2063aee16b3cSJeremy Fitzhardinge do { 2064aee16b3cSJeremy Fitzhardinge next = pud_addr_end(addr, end); 2065aee16b3cSJeremy Fitzhardinge err = apply_to_pmd_range(mm, pud, addr, next, fn, data); 2066aee16b3cSJeremy Fitzhardinge if (err) 2067aee16b3cSJeremy Fitzhardinge break; 2068aee16b3cSJeremy Fitzhardinge } while (pud++, addr = next, addr != end); 2069aee16b3cSJeremy Fitzhardinge return err; 2070aee16b3cSJeremy Fitzhardinge } 2071aee16b3cSJeremy Fitzhardinge 2072c2febafcSKirill A. Shutemov static int apply_to_p4d_range(struct mm_struct *mm, pgd_t *pgd, 2073c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 2074c2febafcSKirill A. Shutemov pte_fn_t fn, void *data) 2075c2febafcSKirill A. Shutemov { 2076c2febafcSKirill A. Shutemov p4d_t *p4d; 2077c2febafcSKirill A. Shutemov unsigned long next; 2078c2febafcSKirill A. Shutemov int err; 2079c2febafcSKirill A. Shutemov 2080c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, addr); 2081c2febafcSKirill A. Shutemov if (!p4d) 2082c2febafcSKirill A. Shutemov return -ENOMEM; 2083c2febafcSKirill A. Shutemov do { 2084c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 2085c2febafcSKirill A. Shutemov err = apply_to_pud_range(mm, p4d, addr, next, fn, data); 2086c2febafcSKirill A. Shutemov if (err) 2087c2febafcSKirill A. Shutemov break; 2088c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 2089c2febafcSKirill A. Shutemov return err; 2090c2febafcSKirill A. Shutemov } 2091c2febafcSKirill A. Shutemov 2092aee16b3cSJeremy Fitzhardinge /* 2093aee16b3cSJeremy Fitzhardinge * Scan a region of virtual memory, filling in page tables as necessary 2094aee16b3cSJeremy Fitzhardinge * and calling a provided function on each leaf page table. 2095aee16b3cSJeremy Fitzhardinge */ 2096aee16b3cSJeremy Fitzhardinge int apply_to_page_range(struct mm_struct *mm, unsigned long addr, 2097aee16b3cSJeremy Fitzhardinge unsigned long size, pte_fn_t fn, void *data) 2098aee16b3cSJeremy Fitzhardinge { 2099aee16b3cSJeremy Fitzhardinge pgd_t *pgd; 2100aee16b3cSJeremy Fitzhardinge unsigned long next; 210157250a5bSJeremy Fitzhardinge unsigned long end = addr + size; 2102aee16b3cSJeremy Fitzhardinge int err; 2103aee16b3cSJeremy Fitzhardinge 21049cb65bc3SMika Penttilä if (WARN_ON(addr >= end)) 21059cb65bc3SMika Penttilä return -EINVAL; 21069cb65bc3SMika Penttilä 2107aee16b3cSJeremy Fitzhardinge pgd = pgd_offset(mm, addr); 2108aee16b3cSJeremy Fitzhardinge do { 2109aee16b3cSJeremy Fitzhardinge next = pgd_addr_end(addr, end); 2110c2febafcSKirill A. Shutemov err = apply_to_p4d_range(mm, pgd, addr, next, fn, data); 2111aee16b3cSJeremy Fitzhardinge if (err) 2112aee16b3cSJeremy Fitzhardinge break; 2113aee16b3cSJeremy Fitzhardinge } while (pgd++, addr = next, addr != end); 211457250a5bSJeremy Fitzhardinge 2115aee16b3cSJeremy Fitzhardinge return err; 2116aee16b3cSJeremy Fitzhardinge } 2117aee16b3cSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(apply_to_page_range); 2118aee16b3cSJeremy Fitzhardinge 21191da177e4SLinus Torvalds /* 21209b4bdd2fSKirill A. Shutemov * handle_pte_fault chooses page fault handler according to an entry which was 21219b4bdd2fSKirill A. Shutemov * read non-atomically. Before making any commitment, on those architectures 21229b4bdd2fSKirill A. Shutemov * or configurations (e.g. i386 with PAE) which might give a mix of unmatched 21239b4bdd2fSKirill A. Shutemov * parts, do_swap_page must check under lock before unmapping the pte and 21249b4bdd2fSKirill A. Shutemov * proceeding (but do_wp_page is only called after already making such a check; 2125a335b2e1SRyota Ozaki * and do_anonymous_page can safely check later on). 21268f4e2101SHugh Dickins */ 21274c21e2f2SHugh Dickins static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd, 21288f4e2101SHugh Dickins pte_t *page_table, pte_t orig_pte) 21298f4e2101SHugh Dickins { 21308f4e2101SHugh Dickins int same = 1; 21318f4e2101SHugh Dickins #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT) 21328f4e2101SHugh Dickins if (sizeof(pte_t) > sizeof(unsigned long)) { 21334c21e2f2SHugh Dickins spinlock_t *ptl = pte_lockptr(mm, pmd); 21344c21e2f2SHugh Dickins spin_lock(ptl); 21358f4e2101SHugh Dickins same = pte_same(*page_table, orig_pte); 21364c21e2f2SHugh Dickins spin_unlock(ptl); 21378f4e2101SHugh Dickins } 21388f4e2101SHugh Dickins #endif 21398f4e2101SHugh Dickins pte_unmap(page_table); 21408f4e2101SHugh Dickins return same; 21418f4e2101SHugh Dickins } 21428f4e2101SHugh Dickins 21439de455b2SAtsushi Nemoto static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma) 21446aab341eSLinus Torvalds { 21450abdd7a8SDan Williams debug_dma_assert_idle(src); 21460abdd7a8SDan Williams 21476aab341eSLinus Torvalds /* 21486aab341eSLinus Torvalds * If the source page was a PFN mapping, we don't have 21496aab341eSLinus Torvalds * a "struct page" for it. We do a best-effort copy by 21506aab341eSLinus Torvalds * just copying from the original user address. If that 21516aab341eSLinus Torvalds * fails, we just zero-fill it. Live with it. 21526aab341eSLinus Torvalds */ 21536aab341eSLinus Torvalds if (unlikely(!src)) { 21549b04c5feSCong Wang void *kaddr = kmap_atomic(dst); 21555d2a2dbbSLinus Torvalds void __user *uaddr = (void __user *)(va & PAGE_MASK); 21565d2a2dbbSLinus Torvalds 21575d2a2dbbSLinus Torvalds /* 21585d2a2dbbSLinus Torvalds * This really shouldn't fail, because the page is there 21595d2a2dbbSLinus Torvalds * in the page tables. But it might just be unreadable, 21605d2a2dbbSLinus Torvalds * in which case we just give up and fill the result with 21615d2a2dbbSLinus Torvalds * zeroes. 21625d2a2dbbSLinus Torvalds */ 21635d2a2dbbSLinus Torvalds if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) 21643ecb01dfSJan Beulich clear_page(kaddr); 21659b04c5feSCong Wang kunmap_atomic(kaddr); 2166c4ec7b0dSDmitriy Monakhov flush_dcache_page(dst); 21670ed361deSNick Piggin } else 21689de455b2SAtsushi Nemoto copy_user_highpage(dst, src, va, vma); 21696aab341eSLinus Torvalds } 21706aab341eSLinus Torvalds 2171c20cd45eSMichal Hocko static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma) 2172c20cd45eSMichal Hocko { 2173c20cd45eSMichal Hocko struct file *vm_file = vma->vm_file; 2174c20cd45eSMichal Hocko 2175c20cd45eSMichal Hocko if (vm_file) 2176c20cd45eSMichal Hocko return mapping_gfp_mask(vm_file->f_mapping) | __GFP_FS | __GFP_IO; 2177c20cd45eSMichal Hocko 2178c20cd45eSMichal Hocko /* 2179c20cd45eSMichal Hocko * Special mappings (e.g. VDSO) do not have any file so fake 2180c20cd45eSMichal Hocko * a default GFP_KERNEL for them. 2181c20cd45eSMichal Hocko */ 2182c20cd45eSMichal Hocko return GFP_KERNEL; 2183c20cd45eSMichal Hocko } 2184c20cd45eSMichal Hocko 21851da177e4SLinus Torvalds /* 2186fb09a464SKirill A. Shutemov * Notify the address space that the page is about to become writable so that 2187fb09a464SKirill A. Shutemov * it can prohibit this or wait for the page to get into an appropriate state. 2188fb09a464SKirill A. Shutemov * 2189fb09a464SKirill A. Shutemov * We do this without the lock held, so that it can sleep if it needs to. 2190fb09a464SKirill A. Shutemov */ 21912b740303SSouptick Joarder static vm_fault_t do_page_mkwrite(struct vm_fault *vmf) 2192fb09a464SKirill A. Shutemov { 21932b740303SSouptick Joarder vm_fault_t ret; 219438b8cb7fSJan Kara struct page *page = vmf->page; 219538b8cb7fSJan Kara unsigned int old_flags = vmf->flags; 2196fb09a464SKirill A. Shutemov 219738b8cb7fSJan Kara vmf->flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE; 2198fb09a464SKirill A. Shutemov 2199*dc617f29SDarrick J. Wong if (vmf->vma->vm_file && 2200*dc617f29SDarrick J. Wong IS_SWAPFILE(vmf->vma->vm_file->f_mapping->host)) 2201*dc617f29SDarrick J. Wong return VM_FAULT_SIGBUS; 2202*dc617f29SDarrick J. Wong 220311bac800SDave Jiang ret = vmf->vma->vm_ops->page_mkwrite(vmf); 220438b8cb7fSJan Kara /* Restore original flags so that caller is not surprised */ 220538b8cb7fSJan Kara vmf->flags = old_flags; 2206fb09a464SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) 2207fb09a464SKirill A. Shutemov return ret; 2208fb09a464SKirill A. Shutemov if (unlikely(!(ret & VM_FAULT_LOCKED))) { 2209fb09a464SKirill A. Shutemov lock_page(page); 2210fb09a464SKirill A. Shutemov if (!page->mapping) { 2211fb09a464SKirill A. Shutemov unlock_page(page); 2212fb09a464SKirill A. Shutemov return 0; /* retry */ 2213fb09a464SKirill A. Shutemov } 2214fb09a464SKirill A. Shutemov ret |= VM_FAULT_LOCKED; 2215fb09a464SKirill A. Shutemov } else 2216fb09a464SKirill A. Shutemov VM_BUG_ON_PAGE(!PageLocked(page), page); 2217fb09a464SKirill A. Shutemov return ret; 2218fb09a464SKirill A. Shutemov } 2219fb09a464SKirill A. Shutemov 2220fb09a464SKirill A. Shutemov /* 222197ba0c2bSJan Kara * Handle dirtying of a page in shared file mapping on a write fault. 22224e047f89SShachar Raindel * 222397ba0c2bSJan Kara * The function expects the page to be locked and unlocks it. 22244e047f89SShachar Raindel */ 222597ba0c2bSJan Kara static void fault_dirty_shared_page(struct vm_area_struct *vma, 222697ba0c2bSJan Kara struct page *page) 22274e047f89SShachar Raindel { 22284e047f89SShachar Raindel struct address_space *mapping; 222997ba0c2bSJan Kara bool dirtied; 223097ba0c2bSJan Kara bool page_mkwrite = vma->vm_ops && vma->vm_ops->page_mkwrite; 22314e047f89SShachar Raindel 22324e047f89SShachar Raindel dirtied = set_page_dirty(page); 22334e047f89SShachar Raindel VM_BUG_ON_PAGE(PageAnon(page), page); 223497ba0c2bSJan Kara /* 223597ba0c2bSJan Kara * Take a local copy of the address_space - page.mapping may be zeroed 223697ba0c2bSJan Kara * by truncate after unlock_page(). The address_space itself remains 223797ba0c2bSJan Kara * pinned by vma->vm_file's reference. We rely on unlock_page()'s 223897ba0c2bSJan Kara * release semantics to prevent the compiler from undoing this copying. 223997ba0c2bSJan Kara */ 224097ba0c2bSJan Kara mapping = page_rmapping(page); 22414e047f89SShachar Raindel unlock_page(page); 22424e047f89SShachar Raindel 22434e047f89SShachar Raindel if ((dirtied || page_mkwrite) && mapping) { 22444e047f89SShachar Raindel /* 22454e047f89SShachar Raindel * Some device drivers do not set page.mapping 22464e047f89SShachar Raindel * but still dirty their pages 22474e047f89SShachar Raindel */ 22484e047f89SShachar Raindel balance_dirty_pages_ratelimited(mapping); 22494e047f89SShachar Raindel } 22504e047f89SShachar Raindel 22514e047f89SShachar Raindel if (!page_mkwrite) 22524e047f89SShachar Raindel file_update_time(vma->vm_file); 22534e047f89SShachar Raindel } 22544e047f89SShachar Raindel 225597ba0c2bSJan Kara /* 22564e047f89SShachar Raindel * Handle write page faults for pages that can be reused in the current vma 22574e047f89SShachar Raindel * 22584e047f89SShachar Raindel * This can happen either due to the mapping being with the VM_SHARED flag, 22594e047f89SShachar Raindel * or due to us being the last reference standing to the page. In either 22604e047f89SShachar Raindel * case, all we need to do here is to mark the page as writable and update 22614e047f89SShachar Raindel * any related book-keeping. 22624e047f89SShachar Raindel */ 2263997dd98dSJan Kara static inline void wp_page_reuse(struct vm_fault *vmf) 226482b0f8c3SJan Kara __releases(vmf->ptl) 22654e047f89SShachar Raindel { 226682b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 2267a41b70d6SJan Kara struct page *page = vmf->page; 22684e047f89SShachar Raindel pte_t entry; 22694e047f89SShachar Raindel /* 22704e047f89SShachar Raindel * Clear the pages cpupid information as the existing 22714e047f89SShachar Raindel * information potentially belongs to a now completely 22724e047f89SShachar Raindel * unrelated process. 22734e047f89SShachar Raindel */ 22744e047f89SShachar Raindel if (page) 22754e047f89SShachar Raindel page_cpupid_xchg_last(page, (1 << LAST_CPUPID_SHIFT) - 1); 22764e047f89SShachar Raindel 22772994302bSJan Kara flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); 22782994302bSJan Kara entry = pte_mkyoung(vmf->orig_pte); 22794e047f89SShachar Raindel entry = maybe_mkwrite(pte_mkdirty(entry), vma); 228082b0f8c3SJan Kara if (ptep_set_access_flags(vma, vmf->address, vmf->pte, entry, 1)) 228182b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 228282b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 22834e047f89SShachar Raindel } 22844e047f89SShachar Raindel 22854e047f89SShachar Raindel /* 22862f38ab2cSShachar Raindel * Handle the case of a page which we actually need to copy to a new page. 22872f38ab2cSShachar Raindel * 22882f38ab2cSShachar Raindel * Called with mmap_sem locked and the old page referenced, but 22892f38ab2cSShachar Raindel * without the ptl held. 22902f38ab2cSShachar Raindel * 22912f38ab2cSShachar Raindel * High level logic flow: 22922f38ab2cSShachar Raindel * 22932f38ab2cSShachar Raindel * - Allocate a page, copy the content of the old page to the new one. 22942f38ab2cSShachar Raindel * - Handle book keeping and accounting - cgroups, mmu-notifiers, etc. 22952f38ab2cSShachar Raindel * - Take the PTL. If the pte changed, bail out and release the allocated page 22962f38ab2cSShachar Raindel * - If the pte is still the way we remember it, update the page table and all 22972f38ab2cSShachar Raindel * relevant references. This includes dropping the reference the page-table 22982f38ab2cSShachar Raindel * held to the old page, as well as updating the rmap. 22992f38ab2cSShachar Raindel * - In any case, unlock the PTL and drop the reference we took to the old page. 23002f38ab2cSShachar Raindel */ 23012b740303SSouptick Joarder static vm_fault_t wp_page_copy(struct vm_fault *vmf) 23022f38ab2cSShachar Raindel { 230382b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 2304bae473a4SKirill A. Shutemov struct mm_struct *mm = vma->vm_mm; 2305a41b70d6SJan Kara struct page *old_page = vmf->page; 23062f38ab2cSShachar Raindel struct page *new_page = NULL; 23072f38ab2cSShachar Raindel pte_t entry; 23082f38ab2cSShachar Raindel int page_copied = 0; 23092f38ab2cSShachar Raindel struct mem_cgroup *memcg; 2310ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 23112f38ab2cSShachar Raindel 23122f38ab2cSShachar Raindel if (unlikely(anon_vma_prepare(vma))) 23132f38ab2cSShachar Raindel goto oom; 23142f38ab2cSShachar Raindel 23152994302bSJan Kara if (is_zero_pfn(pte_pfn(vmf->orig_pte))) { 231682b0f8c3SJan Kara new_page = alloc_zeroed_user_highpage_movable(vma, 231782b0f8c3SJan Kara vmf->address); 23182f38ab2cSShachar Raindel if (!new_page) 23192f38ab2cSShachar Raindel goto oom; 23202f38ab2cSShachar Raindel } else { 2321bae473a4SKirill A. Shutemov new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, 232282b0f8c3SJan Kara vmf->address); 23232f38ab2cSShachar Raindel if (!new_page) 23242f38ab2cSShachar Raindel goto oom; 232582b0f8c3SJan Kara cow_user_page(new_page, old_page, vmf->address, vma); 23262f38ab2cSShachar Raindel } 23272f38ab2cSShachar Raindel 23282cf85583STejun Heo if (mem_cgroup_try_charge_delay(new_page, mm, GFP_KERNEL, &memcg, false)) 23292f38ab2cSShachar Raindel goto oom_free_new; 23302f38ab2cSShachar Raindel 2331eb3c24f3SMel Gorman __SetPageUptodate(new_page); 2332eb3c24f3SMel Gorman 23337269f999SJérôme Glisse mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm, 23346f4f13e8SJérôme Glisse vmf->address & PAGE_MASK, 2335ac46d4f3SJérôme Glisse (vmf->address & PAGE_MASK) + PAGE_SIZE); 2336ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 23372f38ab2cSShachar Raindel 23382f38ab2cSShachar Raindel /* 23392f38ab2cSShachar Raindel * Re-check the pte - we dropped the lock 23402f38ab2cSShachar Raindel */ 234182b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(mm, vmf->pmd, vmf->address, &vmf->ptl); 23422994302bSJan Kara if (likely(pte_same(*vmf->pte, vmf->orig_pte))) { 23432f38ab2cSShachar Raindel if (old_page) { 23442f38ab2cSShachar Raindel if (!PageAnon(old_page)) { 2345eca56ff9SJerome Marchand dec_mm_counter_fast(mm, 2346eca56ff9SJerome Marchand mm_counter_file(old_page)); 23472f38ab2cSShachar Raindel inc_mm_counter_fast(mm, MM_ANONPAGES); 23482f38ab2cSShachar Raindel } 23492f38ab2cSShachar Raindel } else { 23502f38ab2cSShachar Raindel inc_mm_counter_fast(mm, MM_ANONPAGES); 23512f38ab2cSShachar Raindel } 23522994302bSJan Kara flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); 23532f38ab2cSShachar Raindel entry = mk_pte(new_page, vma->vm_page_prot); 23542f38ab2cSShachar Raindel entry = maybe_mkwrite(pte_mkdirty(entry), vma); 23552f38ab2cSShachar Raindel /* 23562f38ab2cSShachar Raindel * Clear the pte entry and flush it first, before updating the 23572f38ab2cSShachar Raindel * pte with the new entry. This will avoid a race condition 23582f38ab2cSShachar Raindel * seen in the presence of one thread doing SMC and another 23592f38ab2cSShachar Raindel * thread doing COW. 23602f38ab2cSShachar Raindel */ 236182b0f8c3SJan Kara ptep_clear_flush_notify(vma, vmf->address, vmf->pte); 236282b0f8c3SJan Kara page_add_new_anon_rmap(new_page, vma, vmf->address, false); 2363f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(new_page, memcg, false, false); 23642f38ab2cSShachar Raindel lru_cache_add_active_or_unevictable(new_page, vma); 23652f38ab2cSShachar Raindel /* 23662f38ab2cSShachar Raindel * We call the notify macro here because, when using secondary 23672f38ab2cSShachar Raindel * mmu page tables (such as kvm shadow page tables), we want the 23682f38ab2cSShachar Raindel * new page to be mapped directly into the secondary page table. 23692f38ab2cSShachar Raindel */ 237082b0f8c3SJan Kara set_pte_at_notify(mm, vmf->address, vmf->pte, entry); 237182b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 23722f38ab2cSShachar Raindel if (old_page) { 23732f38ab2cSShachar Raindel /* 23742f38ab2cSShachar Raindel * Only after switching the pte to the new page may 23752f38ab2cSShachar Raindel * we remove the mapcount here. Otherwise another 23762f38ab2cSShachar Raindel * process may come and find the rmap count decremented 23772f38ab2cSShachar Raindel * before the pte is switched to the new page, and 23782f38ab2cSShachar Raindel * "reuse" the old page writing into it while our pte 23792f38ab2cSShachar Raindel * here still points into it and can be read by other 23802f38ab2cSShachar Raindel * threads. 23812f38ab2cSShachar Raindel * 23822f38ab2cSShachar Raindel * The critical issue is to order this 23832f38ab2cSShachar Raindel * page_remove_rmap with the ptp_clear_flush above. 23842f38ab2cSShachar Raindel * Those stores are ordered by (if nothing else,) 23852f38ab2cSShachar Raindel * the barrier present in the atomic_add_negative 23862f38ab2cSShachar Raindel * in page_remove_rmap. 23872f38ab2cSShachar Raindel * 23882f38ab2cSShachar Raindel * Then the TLB flush in ptep_clear_flush ensures that 23892f38ab2cSShachar Raindel * no process can access the old page before the 23902f38ab2cSShachar Raindel * decremented mapcount is visible. And the old page 23912f38ab2cSShachar Raindel * cannot be reused until after the decremented 23922f38ab2cSShachar Raindel * mapcount is visible. So transitively, TLBs to 23932f38ab2cSShachar Raindel * old page will be flushed before it can be reused. 23942f38ab2cSShachar Raindel */ 2395d281ee61SKirill A. Shutemov page_remove_rmap(old_page, false); 23962f38ab2cSShachar Raindel } 23972f38ab2cSShachar Raindel 23982f38ab2cSShachar Raindel /* Free the old page.. */ 23992f38ab2cSShachar Raindel new_page = old_page; 24002f38ab2cSShachar Raindel page_copied = 1; 24012f38ab2cSShachar Raindel } else { 2402f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(new_page, memcg, false); 24032f38ab2cSShachar Raindel } 24042f38ab2cSShachar Raindel 24052f38ab2cSShachar Raindel if (new_page) 240609cbfeafSKirill A. Shutemov put_page(new_page); 24072f38ab2cSShachar Raindel 240882b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 24094645b9feSJérôme Glisse /* 24104645b9feSJérôme Glisse * No need to double call mmu_notifier->invalidate_range() callback as 24114645b9feSJérôme Glisse * the above ptep_clear_flush_notify() did already call it. 24124645b9feSJérôme Glisse */ 2413ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_only_end(&range); 24142f38ab2cSShachar Raindel if (old_page) { 24152f38ab2cSShachar Raindel /* 24162f38ab2cSShachar Raindel * Don't let another task, with possibly unlocked vma, 24172f38ab2cSShachar Raindel * keep the mlocked page. 24182f38ab2cSShachar Raindel */ 24192f38ab2cSShachar Raindel if (page_copied && (vma->vm_flags & VM_LOCKED)) { 24202f38ab2cSShachar Raindel lock_page(old_page); /* LRU manipulation */ 2421e90309c9SKirill A. Shutemov if (PageMlocked(old_page)) 24222f38ab2cSShachar Raindel munlock_vma_page(old_page); 24232f38ab2cSShachar Raindel unlock_page(old_page); 24242f38ab2cSShachar Raindel } 242509cbfeafSKirill A. Shutemov put_page(old_page); 24262f38ab2cSShachar Raindel } 24272f38ab2cSShachar Raindel return page_copied ? VM_FAULT_WRITE : 0; 24282f38ab2cSShachar Raindel oom_free_new: 242909cbfeafSKirill A. Shutemov put_page(new_page); 24302f38ab2cSShachar Raindel oom: 24312f38ab2cSShachar Raindel if (old_page) 243209cbfeafSKirill A. Shutemov put_page(old_page); 24332f38ab2cSShachar Raindel return VM_FAULT_OOM; 24342f38ab2cSShachar Raindel } 24352f38ab2cSShachar Raindel 243666a6197cSJan Kara /** 243766a6197cSJan Kara * finish_mkwrite_fault - finish page fault for a shared mapping, making PTE 243866a6197cSJan Kara * writeable once the page is prepared 243966a6197cSJan Kara * 244066a6197cSJan Kara * @vmf: structure describing the fault 244166a6197cSJan Kara * 244266a6197cSJan Kara * This function handles all that is needed to finish a write page fault in a 244366a6197cSJan Kara * shared mapping due to PTE being read-only once the mapped page is prepared. 2444a862f68aSMike Rapoport * It handles locking of PTE and modifying it. 244566a6197cSJan Kara * 244666a6197cSJan Kara * The function expects the page to be locked or other protection against 244766a6197cSJan Kara * concurrent faults / writeback (such as DAX radix tree locks). 2448a862f68aSMike Rapoport * 2449a862f68aSMike Rapoport * Return: %VM_FAULT_WRITE on success, %0 when PTE got changed before 2450a862f68aSMike Rapoport * we acquired PTE lock. 245166a6197cSJan Kara */ 24522b740303SSouptick Joarder vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf) 245366a6197cSJan Kara { 245466a6197cSJan Kara WARN_ON_ONCE(!(vmf->vma->vm_flags & VM_SHARED)); 245566a6197cSJan Kara vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, vmf->address, 245666a6197cSJan Kara &vmf->ptl); 245766a6197cSJan Kara /* 245866a6197cSJan Kara * We might have raced with another page fault while we released the 245966a6197cSJan Kara * pte_offset_map_lock. 246066a6197cSJan Kara */ 246166a6197cSJan Kara if (!pte_same(*vmf->pte, vmf->orig_pte)) { 246266a6197cSJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2463a19e2553SJan Kara return VM_FAULT_NOPAGE; 246466a6197cSJan Kara } 246566a6197cSJan Kara wp_page_reuse(vmf); 2466a19e2553SJan Kara return 0; 246766a6197cSJan Kara } 246866a6197cSJan Kara 2469dd906184SBoaz Harrosh /* 2470dd906184SBoaz Harrosh * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED 2471dd906184SBoaz Harrosh * mapping 2472dd906184SBoaz Harrosh */ 24732b740303SSouptick Joarder static vm_fault_t wp_pfn_shared(struct vm_fault *vmf) 2474dd906184SBoaz Harrosh { 247582b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 2476bae473a4SKirill A. Shutemov 2477dd906184SBoaz Harrosh if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) { 24782b740303SSouptick Joarder vm_fault_t ret; 2479dd906184SBoaz Harrosh 248082b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2481fe82221fSJan Kara vmf->flags |= FAULT_FLAG_MKWRITE; 248211bac800SDave Jiang ret = vma->vm_ops->pfn_mkwrite(vmf); 24832f89dc12SJan Kara if (ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)) 2484dd906184SBoaz Harrosh return ret; 248566a6197cSJan Kara return finish_mkwrite_fault(vmf); 2486dd906184SBoaz Harrosh } 2487997dd98dSJan Kara wp_page_reuse(vmf); 2488997dd98dSJan Kara return VM_FAULT_WRITE; 2489dd906184SBoaz Harrosh } 2490dd906184SBoaz Harrosh 24912b740303SSouptick Joarder static vm_fault_t wp_page_shared(struct vm_fault *vmf) 249282b0f8c3SJan Kara __releases(vmf->ptl) 249393e478d4SShachar Raindel { 249482b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 249593e478d4SShachar Raindel 2496a41b70d6SJan Kara get_page(vmf->page); 249793e478d4SShachar Raindel 249893e478d4SShachar Raindel if (vma->vm_ops && vma->vm_ops->page_mkwrite) { 24992b740303SSouptick Joarder vm_fault_t tmp; 250093e478d4SShachar Raindel 250182b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 250238b8cb7fSJan Kara tmp = do_page_mkwrite(vmf); 250393e478d4SShachar Raindel if (unlikely(!tmp || (tmp & 250493e478d4SShachar Raindel (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { 2505a41b70d6SJan Kara put_page(vmf->page); 250693e478d4SShachar Raindel return tmp; 250793e478d4SShachar Raindel } 250866a6197cSJan Kara tmp = finish_mkwrite_fault(vmf); 2509a19e2553SJan Kara if (unlikely(tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) { 2510a41b70d6SJan Kara unlock_page(vmf->page); 2511a41b70d6SJan Kara put_page(vmf->page); 251266a6197cSJan Kara return tmp; 251393e478d4SShachar Raindel } 251466a6197cSJan Kara } else { 2515997dd98dSJan Kara wp_page_reuse(vmf); 2516997dd98dSJan Kara lock_page(vmf->page); 251793e478d4SShachar Raindel } 2518997dd98dSJan Kara fault_dirty_shared_page(vma, vmf->page); 2519997dd98dSJan Kara put_page(vmf->page); 252093e478d4SShachar Raindel 2521997dd98dSJan Kara return VM_FAULT_WRITE; 252293e478d4SShachar Raindel } 252393e478d4SShachar Raindel 25242f38ab2cSShachar Raindel /* 25251da177e4SLinus Torvalds * This routine handles present pages, when users try to write 25261da177e4SLinus Torvalds * to a shared page. It is done by copying the page to a new address 25271da177e4SLinus Torvalds * and decrementing the shared-page counter for the old page. 25281da177e4SLinus Torvalds * 25291da177e4SLinus Torvalds * Note that this routine assumes that the protection checks have been 25301da177e4SLinus Torvalds * done by the caller (the low-level page fault routine in most cases). 25311da177e4SLinus Torvalds * Thus we can safely just mark it writable once we've done any necessary 25321da177e4SLinus Torvalds * COW. 25331da177e4SLinus Torvalds * 25341da177e4SLinus Torvalds * We also mark the page dirty at this point even though the page will 25351da177e4SLinus Torvalds * change only once the write actually happens. This avoids a few races, 25361da177e4SLinus Torvalds * and potentially makes it more efficient. 25371da177e4SLinus Torvalds * 25388f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 25398f4e2101SHugh Dickins * but allow concurrent faults), with pte both mapped and locked. 25408f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 25411da177e4SLinus Torvalds */ 25422b740303SSouptick Joarder static vm_fault_t do_wp_page(struct vm_fault *vmf) 254382b0f8c3SJan Kara __releases(vmf->ptl) 25441da177e4SLinus Torvalds { 254582b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 25461da177e4SLinus Torvalds 2547a41b70d6SJan Kara vmf->page = vm_normal_page(vma, vmf->address, vmf->orig_pte); 2548a41b70d6SJan Kara if (!vmf->page) { 2549251b97f5SPeter Zijlstra /* 255064e45507SPeter Feiner * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a 255164e45507SPeter Feiner * VM_PFNMAP VMA. 2552251b97f5SPeter Zijlstra * 2553251b97f5SPeter Zijlstra * We should not cow pages in a shared writeable mapping. 2554dd906184SBoaz Harrosh * Just mark the pages writable and/or call ops->pfn_mkwrite. 2555251b97f5SPeter Zijlstra */ 2556251b97f5SPeter Zijlstra if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 2557251b97f5SPeter Zijlstra (VM_WRITE|VM_SHARED)) 25582994302bSJan Kara return wp_pfn_shared(vmf); 25592f38ab2cSShachar Raindel 256082b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2561a41b70d6SJan Kara return wp_page_copy(vmf); 2562251b97f5SPeter Zijlstra } 25631da177e4SLinus Torvalds 2564d08b3851SPeter Zijlstra /* 2565ee6a6457SPeter Zijlstra * Take out anonymous pages first, anonymous shared vmas are 2566ee6a6457SPeter Zijlstra * not dirty accountable. 2567d08b3851SPeter Zijlstra */ 256852d1e606SKirill Tkhai if (PageAnon(vmf->page)) { 2569ba3c4ce6SHuang Ying int total_map_swapcount; 257052d1e606SKirill Tkhai if (PageKsm(vmf->page) && (PageSwapCache(vmf->page) || 257152d1e606SKirill Tkhai page_count(vmf->page) != 1)) 257252d1e606SKirill Tkhai goto copy; 2573a41b70d6SJan Kara if (!trylock_page(vmf->page)) { 2574a41b70d6SJan Kara get_page(vmf->page); 257582b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2576a41b70d6SJan Kara lock_page(vmf->page); 257782b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 257882b0f8c3SJan Kara vmf->address, &vmf->ptl); 25792994302bSJan Kara if (!pte_same(*vmf->pte, vmf->orig_pte)) { 2580a41b70d6SJan Kara unlock_page(vmf->page); 258182b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2582a41b70d6SJan Kara put_page(vmf->page); 258328766805SShachar Raindel return 0; 2584ab967d86SHugh Dickins } 2585a41b70d6SJan Kara put_page(vmf->page); 2586ab967d86SHugh Dickins } 258752d1e606SKirill Tkhai if (PageKsm(vmf->page)) { 258852d1e606SKirill Tkhai bool reused = reuse_ksm_page(vmf->page, vmf->vma, 258952d1e606SKirill Tkhai vmf->address); 259052d1e606SKirill Tkhai unlock_page(vmf->page); 259152d1e606SKirill Tkhai if (!reused) 259252d1e606SKirill Tkhai goto copy; 259352d1e606SKirill Tkhai wp_page_reuse(vmf); 259452d1e606SKirill Tkhai return VM_FAULT_WRITE; 259552d1e606SKirill Tkhai } 2596ba3c4ce6SHuang Ying if (reuse_swap_page(vmf->page, &total_map_swapcount)) { 2597ba3c4ce6SHuang Ying if (total_map_swapcount == 1) { 2598c44b6743SRik van Riel /* 25996d0a07edSAndrea Arcangeli * The page is all ours. Move it to 26006d0a07edSAndrea Arcangeli * our anon_vma so the rmap code will 26016d0a07edSAndrea Arcangeli * not search our parent or siblings. 26026d0a07edSAndrea Arcangeli * Protected against the rmap code by 26036d0a07edSAndrea Arcangeli * the page lock. 2604c44b6743SRik van Riel */ 2605a41b70d6SJan Kara page_move_anon_rmap(vmf->page, vma); 26066d0a07edSAndrea Arcangeli } 2607a41b70d6SJan Kara unlock_page(vmf->page); 2608997dd98dSJan Kara wp_page_reuse(vmf); 2609997dd98dSJan Kara return VM_FAULT_WRITE; 2610b009c024SMichel Lespinasse } 2611a41b70d6SJan Kara unlock_page(vmf->page); 2612ee6a6457SPeter Zijlstra } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) == 2613d08b3851SPeter Zijlstra (VM_WRITE|VM_SHARED))) { 2614a41b70d6SJan Kara return wp_page_shared(vmf); 26151da177e4SLinus Torvalds } 261652d1e606SKirill Tkhai copy: 26171da177e4SLinus Torvalds /* 26181da177e4SLinus Torvalds * Ok, we need to copy. Oh, well.. 26191da177e4SLinus Torvalds */ 2620a41b70d6SJan Kara get_page(vmf->page); 262128766805SShachar Raindel 262282b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2623a41b70d6SJan Kara return wp_page_copy(vmf); 26241da177e4SLinus Torvalds } 26251da177e4SLinus Torvalds 262697a89413SPeter Zijlstra static void unmap_mapping_range_vma(struct vm_area_struct *vma, 26271da177e4SLinus Torvalds unsigned long start_addr, unsigned long end_addr, 26281da177e4SLinus Torvalds struct zap_details *details) 26291da177e4SLinus Torvalds { 2630f5cc4eefSAl Viro zap_page_range_single(vma, start_addr, end_addr - start_addr, details); 26311da177e4SLinus Torvalds } 26321da177e4SLinus Torvalds 2633f808c13fSDavidlohr Bueso static inline void unmap_mapping_range_tree(struct rb_root_cached *root, 26341da177e4SLinus Torvalds struct zap_details *details) 26351da177e4SLinus Torvalds { 26361da177e4SLinus Torvalds struct vm_area_struct *vma; 26371da177e4SLinus Torvalds pgoff_t vba, vea, zba, zea; 26381da177e4SLinus Torvalds 26396b2dbba8SMichel Lespinasse vma_interval_tree_foreach(vma, root, 26401da177e4SLinus Torvalds details->first_index, details->last_index) { 26411da177e4SLinus Torvalds 26421da177e4SLinus Torvalds vba = vma->vm_pgoff; 2643d6e93217SLibin vea = vba + vma_pages(vma) - 1; 26441da177e4SLinus Torvalds zba = details->first_index; 26451da177e4SLinus Torvalds if (zba < vba) 26461da177e4SLinus Torvalds zba = vba; 26471da177e4SLinus Torvalds zea = details->last_index; 26481da177e4SLinus Torvalds if (zea > vea) 26491da177e4SLinus Torvalds zea = vea; 26501da177e4SLinus Torvalds 265197a89413SPeter Zijlstra unmap_mapping_range_vma(vma, 26521da177e4SLinus Torvalds ((zba - vba) << PAGE_SHIFT) + vma->vm_start, 26531da177e4SLinus Torvalds ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start, 265497a89413SPeter Zijlstra details); 26551da177e4SLinus Torvalds } 26561da177e4SLinus Torvalds } 26571da177e4SLinus Torvalds 26581da177e4SLinus Torvalds /** 2659977fbdcdSMatthew Wilcox * unmap_mapping_pages() - Unmap pages from processes. 2660977fbdcdSMatthew Wilcox * @mapping: The address space containing pages to be unmapped. 2661977fbdcdSMatthew Wilcox * @start: Index of first page to be unmapped. 2662977fbdcdSMatthew Wilcox * @nr: Number of pages to be unmapped. 0 to unmap to end of file. 2663977fbdcdSMatthew Wilcox * @even_cows: Whether to unmap even private COWed pages. 2664977fbdcdSMatthew Wilcox * 2665977fbdcdSMatthew Wilcox * Unmap the pages in this address space from any userspace process which 2666977fbdcdSMatthew Wilcox * has them mmaped. Generally, you want to remove COWed pages as well when 2667977fbdcdSMatthew Wilcox * a file is being truncated, but not when invalidating pages from the page 2668977fbdcdSMatthew Wilcox * cache. 2669977fbdcdSMatthew Wilcox */ 2670977fbdcdSMatthew Wilcox void unmap_mapping_pages(struct address_space *mapping, pgoff_t start, 2671977fbdcdSMatthew Wilcox pgoff_t nr, bool even_cows) 2672977fbdcdSMatthew Wilcox { 2673977fbdcdSMatthew Wilcox struct zap_details details = { }; 2674977fbdcdSMatthew Wilcox 2675977fbdcdSMatthew Wilcox details.check_mapping = even_cows ? NULL : mapping; 2676977fbdcdSMatthew Wilcox details.first_index = start; 2677977fbdcdSMatthew Wilcox details.last_index = start + nr - 1; 2678977fbdcdSMatthew Wilcox if (details.last_index < details.first_index) 2679977fbdcdSMatthew Wilcox details.last_index = ULONG_MAX; 2680977fbdcdSMatthew Wilcox 2681977fbdcdSMatthew Wilcox i_mmap_lock_write(mapping); 2682977fbdcdSMatthew Wilcox if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root))) 2683977fbdcdSMatthew Wilcox unmap_mapping_range_tree(&mapping->i_mmap, &details); 2684977fbdcdSMatthew Wilcox i_mmap_unlock_write(mapping); 2685977fbdcdSMatthew Wilcox } 2686977fbdcdSMatthew Wilcox 2687977fbdcdSMatthew Wilcox /** 26888a5f14a2SKirill A. Shutemov * unmap_mapping_range - unmap the portion of all mmaps in the specified 2689977fbdcdSMatthew Wilcox * address_space corresponding to the specified byte range in the underlying 26908a5f14a2SKirill A. Shutemov * file. 26918a5f14a2SKirill A. Shutemov * 26923d41088fSMartin Waitz * @mapping: the address space containing mmaps to be unmapped. 26931da177e4SLinus Torvalds * @holebegin: byte in first page to unmap, relative to the start of 26941da177e4SLinus Torvalds * the underlying file. This will be rounded down to a PAGE_SIZE 269525d9e2d1Snpiggin@suse.de * boundary. Note that this is different from truncate_pagecache(), which 26961da177e4SLinus Torvalds * must keep the partial page. In contrast, we must get rid of 26971da177e4SLinus Torvalds * partial pages. 26981da177e4SLinus Torvalds * @holelen: size of prospective hole in bytes. This will be rounded 26991da177e4SLinus Torvalds * up to a PAGE_SIZE boundary. A holelen of zero truncates to the 27001da177e4SLinus Torvalds * end of the file. 27011da177e4SLinus Torvalds * @even_cows: 1 when truncating a file, unmap even private COWed pages; 27021da177e4SLinus Torvalds * but 0 when invalidating pagecache, don't throw away private data. 27031da177e4SLinus Torvalds */ 27041da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping, 27051da177e4SLinus Torvalds loff_t const holebegin, loff_t const holelen, int even_cows) 27061da177e4SLinus Torvalds { 27071da177e4SLinus Torvalds pgoff_t hba = holebegin >> PAGE_SHIFT; 27081da177e4SLinus Torvalds pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 27091da177e4SLinus Torvalds 27101da177e4SLinus Torvalds /* Check for overflow. */ 27111da177e4SLinus Torvalds if (sizeof(holelen) > sizeof(hlen)) { 27121da177e4SLinus Torvalds long long holeend = 27131da177e4SLinus Torvalds (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 27141da177e4SLinus Torvalds if (holeend & ~(long long)ULONG_MAX) 27151da177e4SLinus Torvalds hlen = ULONG_MAX - hba + 1; 27161da177e4SLinus Torvalds } 27171da177e4SLinus Torvalds 2718977fbdcdSMatthew Wilcox unmap_mapping_pages(mapping, hba, hlen, even_cows); 27191da177e4SLinus Torvalds } 27201da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range); 27211da177e4SLinus Torvalds 27221da177e4SLinus Torvalds /* 27238f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 27248f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 27259a95f3cfSPaul Cassella * We return with pte unmapped and unlocked. 27269a95f3cfSPaul Cassella * 27279a95f3cfSPaul Cassella * We return with the mmap_sem locked or unlocked in the same cases 27289a95f3cfSPaul Cassella * as does filemap_fault(). 27291da177e4SLinus Torvalds */ 27302b740303SSouptick Joarder vm_fault_t do_swap_page(struct vm_fault *vmf) 27311da177e4SLinus Torvalds { 273282b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 2733eaf649ebSMinchan Kim struct page *page = NULL, *swapcache; 273400501b53SJohannes Weiner struct mem_cgroup *memcg; 273565500d23SHugh Dickins swp_entry_t entry; 27361da177e4SLinus Torvalds pte_t pte; 2737d065bd81SMichel Lespinasse int locked; 2738ad8c2ee8SRik van Riel int exclusive = 0; 27392b740303SSouptick Joarder vm_fault_t ret = 0; 27401da177e4SLinus Torvalds 2741eaf649ebSMinchan Kim if (!pte_unmap_same(vma->vm_mm, vmf->pmd, vmf->pte, vmf->orig_pte)) 27428f4e2101SHugh Dickins goto out; 274365500d23SHugh Dickins 27442994302bSJan Kara entry = pte_to_swp_entry(vmf->orig_pte); 2745d1737fdbSAndi Kleen if (unlikely(non_swap_entry(entry))) { 27460697212aSChristoph Lameter if (is_migration_entry(entry)) { 274782b0f8c3SJan Kara migration_entry_wait(vma->vm_mm, vmf->pmd, 274882b0f8c3SJan Kara vmf->address); 27495042db43SJérôme Glisse } else if (is_device_private_entry(entry)) { 2750897e6365SChristoph Hellwig vmf->page = device_private_entry_to_page(entry); 2751897e6365SChristoph Hellwig ret = vmf->page->pgmap->ops->migrate_to_ram(vmf); 2752d1737fdbSAndi Kleen } else if (is_hwpoison_entry(entry)) { 2753d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 2754d1737fdbSAndi Kleen } else { 27552994302bSJan Kara print_bad_pte(vma, vmf->address, vmf->orig_pte, NULL); 2756d99be1a8SHugh Dickins ret = VM_FAULT_SIGBUS; 2757d1737fdbSAndi Kleen } 27580697212aSChristoph Lameter goto out; 27590697212aSChristoph Lameter } 27600bcac06fSMinchan Kim 27610bcac06fSMinchan Kim 27620ff92245SShailabh Nagar delayacct_set_flag(DELAYACCT_PF_SWAPIN); 2763eaf649ebSMinchan Kim page = lookup_swap_cache(entry, vma, vmf->address); 2764f8020772SMinchan Kim swapcache = page; 2765f8020772SMinchan Kim 27661da177e4SLinus Torvalds if (!page) { 27670bcac06fSMinchan Kim struct swap_info_struct *si = swp_swap_info(entry); 27680bcac06fSMinchan Kim 2769aa8d22a1SMinchan Kim if (si->flags & SWP_SYNCHRONOUS_IO && 2770eb085574SHuang Ying __swap_count(entry) == 1) { 27710bcac06fSMinchan Kim /* skip swapcache */ 2772e9e9b7ecSMinchan Kim page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, 2773e9e9b7ecSMinchan Kim vmf->address); 27740bcac06fSMinchan Kim if (page) { 27750bcac06fSMinchan Kim __SetPageLocked(page); 27760bcac06fSMinchan Kim __SetPageSwapBacked(page); 27770bcac06fSMinchan Kim set_page_private(page, entry.val); 27780bcac06fSMinchan Kim lru_cache_add_anon(page); 27790bcac06fSMinchan Kim swap_readpage(page, true); 27800bcac06fSMinchan Kim } 2781aa8d22a1SMinchan Kim } else { 2782e9e9b7ecSMinchan Kim page = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, 2783e9e9b7ecSMinchan Kim vmf); 2784aa8d22a1SMinchan Kim swapcache = page; 27850bcac06fSMinchan Kim } 27860bcac06fSMinchan Kim 2787ec560175SHuang Ying if (!page) { 27881da177e4SLinus Torvalds /* 27898f4e2101SHugh Dickins * Back out if somebody else faulted in this pte 27908f4e2101SHugh Dickins * while we released the pte lock. 27911da177e4SLinus Torvalds */ 279282b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 279382b0f8c3SJan Kara vmf->address, &vmf->ptl); 27942994302bSJan Kara if (likely(pte_same(*vmf->pte, vmf->orig_pte))) 27951da177e4SLinus Torvalds ret = VM_FAULT_OOM; 27960ff92245SShailabh Nagar delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 279765500d23SHugh Dickins goto unlock; 27981da177e4SLinus Torvalds } 27991da177e4SLinus Torvalds 28001da177e4SLinus Torvalds /* Had to read the page from swap area: Major fault */ 28011da177e4SLinus Torvalds ret = VM_FAULT_MAJOR; 2802f8891e5eSChristoph Lameter count_vm_event(PGMAJFAULT); 28032262185cSRoman Gushchin count_memcg_event_mm(vma->vm_mm, PGMAJFAULT); 2804d1737fdbSAndi Kleen } else if (PageHWPoison(page)) { 280571f72525SWu Fengguang /* 280671f72525SWu Fengguang * hwpoisoned dirty swapcache pages are kept for killing 280771f72525SWu Fengguang * owner processes (which may be unknown at hwpoison time) 280871f72525SWu Fengguang */ 2809d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 2810d1737fdbSAndi Kleen delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 28114779cb31SAndi Kleen goto out_release; 28121da177e4SLinus Torvalds } 28131da177e4SLinus Torvalds 281482b0f8c3SJan Kara locked = lock_page_or_retry(page, vma->vm_mm, vmf->flags); 2815e709ffd6SRik van Riel 281620a1022dSBalbir Singh delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 2817d065bd81SMichel Lespinasse if (!locked) { 2818d065bd81SMichel Lespinasse ret |= VM_FAULT_RETRY; 2819d065bd81SMichel Lespinasse goto out_release; 2820d065bd81SMichel Lespinasse } 28211da177e4SLinus Torvalds 28224969c119SAndrea Arcangeli /* 282331c4a3d3SHugh Dickins * Make sure try_to_free_swap or reuse_swap_page or swapoff did not 282431c4a3d3SHugh Dickins * release the swapcache from under us. The page pin, and pte_same 282531c4a3d3SHugh Dickins * test below, are not enough to exclude that. Even if it is still 282631c4a3d3SHugh Dickins * swapcache, we need to check that the page's swap has not changed. 28274969c119SAndrea Arcangeli */ 28280bcac06fSMinchan Kim if (unlikely((!PageSwapCache(page) || 28290bcac06fSMinchan Kim page_private(page) != entry.val)) && swapcache) 28304969c119SAndrea Arcangeli goto out_page; 28314969c119SAndrea Arcangeli 283282b0f8c3SJan Kara page = ksm_might_need_to_copy(page, vma, vmf->address); 28334969c119SAndrea Arcangeli if (unlikely(!page)) { 28345ad64688SHugh Dickins ret = VM_FAULT_OOM; 28354969c119SAndrea Arcangeli page = swapcache; 28364969c119SAndrea Arcangeli goto out_page; 28374969c119SAndrea Arcangeli } 28385ad64688SHugh Dickins 28392cf85583STejun Heo if (mem_cgroup_try_charge_delay(page, vma->vm_mm, GFP_KERNEL, 2840bae473a4SKirill A. Shutemov &memcg, false)) { 2841073e587eSKAMEZAWA Hiroyuki ret = VM_FAULT_OOM; 2842bc43f75cSJohannes Weiner goto out_page; 2843073e587eSKAMEZAWA Hiroyuki } 2844073e587eSKAMEZAWA Hiroyuki 28451da177e4SLinus Torvalds /* 28468f4e2101SHugh Dickins * Back out if somebody else already faulted in this pte. 28471da177e4SLinus Torvalds */ 284882b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 284982b0f8c3SJan Kara &vmf->ptl); 28502994302bSJan Kara if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) 2851b8107480SKirill Korotaev goto out_nomap; 2852b8107480SKirill Korotaev 2853b8107480SKirill Korotaev if (unlikely(!PageUptodate(page))) { 2854b8107480SKirill Korotaev ret = VM_FAULT_SIGBUS; 2855b8107480SKirill Korotaev goto out_nomap; 28561da177e4SLinus Torvalds } 28571da177e4SLinus Torvalds 28588c7c6e34SKAMEZAWA Hiroyuki /* 28598c7c6e34SKAMEZAWA Hiroyuki * The page isn't present yet, go ahead with the fault. 28608c7c6e34SKAMEZAWA Hiroyuki * 28618c7c6e34SKAMEZAWA Hiroyuki * Be careful about the sequence of operations here. 28628c7c6e34SKAMEZAWA Hiroyuki * To get its accounting right, reuse_swap_page() must be called 28638c7c6e34SKAMEZAWA Hiroyuki * while the page is counted on swap but not yet in mapcount i.e. 28648c7c6e34SKAMEZAWA Hiroyuki * before page_add_anon_rmap() and swap_free(); try_to_free_swap() 28658c7c6e34SKAMEZAWA Hiroyuki * must be called after the swap_free(), or it will never succeed. 28668c7c6e34SKAMEZAWA Hiroyuki */ 28671da177e4SLinus Torvalds 2868bae473a4SKirill A. Shutemov inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); 2869bae473a4SKirill A. Shutemov dec_mm_counter_fast(vma->vm_mm, MM_SWAPENTS); 28701da177e4SLinus Torvalds pte = mk_pte(page, vma->vm_page_prot); 287182b0f8c3SJan Kara if ((vmf->flags & FAULT_FLAG_WRITE) && reuse_swap_page(page, NULL)) { 28721da177e4SLinus Torvalds pte = maybe_mkwrite(pte_mkdirty(pte), vma); 287382b0f8c3SJan Kara vmf->flags &= ~FAULT_FLAG_WRITE; 28749a5b489bSAndrea Arcangeli ret |= VM_FAULT_WRITE; 2875d281ee61SKirill A. Shutemov exclusive = RMAP_EXCLUSIVE; 28761da177e4SLinus Torvalds } 28771da177e4SLinus Torvalds flush_icache_page(vma, page); 28782994302bSJan Kara if (pte_swp_soft_dirty(vmf->orig_pte)) 2879179ef71cSCyrill Gorcunov pte = pte_mksoft_dirty(pte); 288082b0f8c3SJan Kara set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte); 2881ca827d55SKhalid Aziz arch_do_swap_page(vma->vm_mm, vma, vmf->address, pte, vmf->orig_pte); 28822994302bSJan Kara vmf->orig_pte = pte; 28830bcac06fSMinchan Kim 28840bcac06fSMinchan Kim /* ksm created a completely new copy */ 28850bcac06fSMinchan Kim if (unlikely(page != swapcache && swapcache)) { 288682b0f8c3SJan Kara page_add_new_anon_rmap(page, vma, vmf->address, false); 2887f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, false); 288800501b53SJohannes Weiner lru_cache_add_active_or_unevictable(page, vma); 28890bcac06fSMinchan Kim } else { 28900bcac06fSMinchan Kim do_page_add_anon_rmap(page, vma, vmf->address, exclusive); 28910bcac06fSMinchan Kim mem_cgroup_commit_charge(page, memcg, true, false); 28920bcac06fSMinchan Kim activate_page(page); 289300501b53SJohannes Weiner } 28941da177e4SLinus Torvalds 2895c475a8abSHugh Dickins swap_free(entry); 28965ccc5abaSVladimir Davydov if (mem_cgroup_swap_full(page) || 28975ccc5abaSVladimir Davydov (vma->vm_flags & VM_LOCKED) || PageMlocked(page)) 2898a2c43eedSHugh Dickins try_to_free_swap(page); 2899c475a8abSHugh Dickins unlock_page(page); 29000bcac06fSMinchan Kim if (page != swapcache && swapcache) { 29014969c119SAndrea Arcangeli /* 29024969c119SAndrea Arcangeli * Hold the lock to avoid the swap entry to be reused 29034969c119SAndrea Arcangeli * until we take the PT lock for the pte_same() check 29044969c119SAndrea Arcangeli * (to avoid false positives from pte_same). For 29054969c119SAndrea Arcangeli * further safety release the lock after the swap_free 29064969c119SAndrea Arcangeli * so that the swap count won't change under a 29074969c119SAndrea Arcangeli * parallel locked swapcache. 29084969c119SAndrea Arcangeli */ 29094969c119SAndrea Arcangeli unlock_page(swapcache); 291009cbfeafSKirill A. Shutemov put_page(swapcache); 29114969c119SAndrea Arcangeli } 2912c475a8abSHugh Dickins 291382b0f8c3SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) { 29142994302bSJan Kara ret |= do_wp_page(vmf); 291561469f1dSHugh Dickins if (ret & VM_FAULT_ERROR) 291661469f1dSHugh Dickins ret &= VM_FAULT_ERROR; 29171da177e4SLinus Torvalds goto out; 29181da177e4SLinus Torvalds } 29191da177e4SLinus Torvalds 29201da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 292182b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 292265500d23SHugh Dickins unlock: 292382b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 29241da177e4SLinus Torvalds out: 29251da177e4SLinus Torvalds return ret; 2926b8107480SKirill Korotaev out_nomap: 2927f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 292882b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 2929bc43f75cSJohannes Weiner out_page: 2930b8107480SKirill Korotaev unlock_page(page); 29314779cb31SAndi Kleen out_release: 293209cbfeafSKirill A. Shutemov put_page(page); 29330bcac06fSMinchan Kim if (page != swapcache && swapcache) { 29344969c119SAndrea Arcangeli unlock_page(swapcache); 293509cbfeafSKirill A. Shutemov put_page(swapcache); 29364969c119SAndrea Arcangeli } 293765500d23SHugh Dickins return ret; 29381da177e4SLinus Torvalds } 29391da177e4SLinus Torvalds 29401da177e4SLinus Torvalds /* 29418f4e2101SHugh Dickins * We enter with non-exclusive mmap_sem (to exclude vma changes, 29428f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 29438f4e2101SHugh Dickins * We return with mmap_sem still held, but pte unmapped and unlocked. 29441da177e4SLinus Torvalds */ 29452b740303SSouptick Joarder static vm_fault_t do_anonymous_page(struct vm_fault *vmf) 29461da177e4SLinus Torvalds { 294782b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 294800501b53SJohannes Weiner struct mem_cgroup *memcg; 29498f4e2101SHugh Dickins struct page *page; 29502b740303SSouptick Joarder vm_fault_t ret = 0; 29511da177e4SLinus Torvalds pte_t entry; 29521da177e4SLinus Torvalds 29536b7339f4SKirill A. Shutemov /* File mapping without ->vm_ops ? */ 29546b7339f4SKirill A. Shutemov if (vma->vm_flags & VM_SHARED) 29556b7339f4SKirill A. Shutemov return VM_FAULT_SIGBUS; 29566b7339f4SKirill A. Shutemov 29577267ec00SKirill A. Shutemov /* 29587267ec00SKirill A. Shutemov * Use pte_alloc() instead of pte_alloc_map(). We can't run 29597267ec00SKirill A. Shutemov * pte_offset_map() on pmds where a huge pmd might be created 29607267ec00SKirill A. Shutemov * from a different thread. 29617267ec00SKirill A. Shutemov * 29627267ec00SKirill A. Shutemov * pte_alloc_map() is safe to use under down_write(mmap_sem) or when 29637267ec00SKirill A. Shutemov * parallel threads are excluded by other means. 29647267ec00SKirill A. Shutemov * 29657267ec00SKirill A. Shutemov * Here we only have down_read(mmap_sem). 29667267ec00SKirill A. Shutemov */ 29674cf58924SJoel Fernandes (Google) if (pte_alloc(vma->vm_mm, vmf->pmd)) 29687267ec00SKirill A. Shutemov return VM_FAULT_OOM; 29697267ec00SKirill A. Shutemov 29707267ec00SKirill A. Shutemov /* See the comment in pte_alloc_one_map() */ 297182b0f8c3SJan Kara if (unlikely(pmd_trans_unstable(vmf->pmd))) 29727267ec00SKirill A. Shutemov return 0; 29737267ec00SKirill A. Shutemov 297411ac5524SLinus Torvalds /* Use the zero-page for reads */ 297582b0f8c3SJan Kara if (!(vmf->flags & FAULT_FLAG_WRITE) && 2976bae473a4SKirill A. Shutemov !mm_forbids_zeropage(vma->vm_mm)) { 297782b0f8c3SJan Kara entry = pte_mkspecial(pfn_pte(my_zero_pfn(vmf->address), 297862eede62SHugh Dickins vma->vm_page_prot)); 297982b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 298082b0f8c3SJan Kara vmf->address, &vmf->ptl); 298182b0f8c3SJan Kara if (!pte_none(*vmf->pte)) 2982a13ea5b7SHugh Dickins goto unlock; 29836b31d595SMichal Hocko ret = check_stable_address_space(vma->vm_mm); 29846b31d595SMichal Hocko if (ret) 29856b31d595SMichal Hocko goto unlock; 29866b251fc9SAndrea Arcangeli /* Deliver the page fault to userland, check inside PT lock */ 29876b251fc9SAndrea Arcangeli if (userfaultfd_missing(vma)) { 298882b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 298982b0f8c3SJan Kara return handle_userfault(vmf, VM_UFFD_MISSING); 29906b251fc9SAndrea Arcangeli } 2991a13ea5b7SHugh Dickins goto setpte; 2992a13ea5b7SHugh Dickins } 2993a13ea5b7SHugh Dickins 29941da177e4SLinus Torvalds /* Allocate our own private page. */ 29951da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 299665500d23SHugh Dickins goto oom; 299782b0f8c3SJan Kara page = alloc_zeroed_user_highpage_movable(vma, vmf->address); 29981da177e4SLinus Torvalds if (!page) 299965500d23SHugh Dickins goto oom; 3000eb3c24f3SMel Gorman 30012cf85583STejun Heo if (mem_cgroup_try_charge_delay(page, vma->vm_mm, GFP_KERNEL, &memcg, 30022cf85583STejun Heo false)) 3003eb3c24f3SMel Gorman goto oom_free_page; 3004eb3c24f3SMel Gorman 300552f37629SMinchan Kim /* 300652f37629SMinchan Kim * The memory barrier inside __SetPageUptodate makes sure that 300752f37629SMinchan Kim * preceeding stores to the page contents become visible before 300852f37629SMinchan Kim * the set_pte_at() write. 300952f37629SMinchan Kim */ 30100ed361deSNick Piggin __SetPageUptodate(page); 30111da177e4SLinus Torvalds 301265500d23SHugh Dickins entry = mk_pte(page, vma->vm_page_prot); 30131ac0cb5dSHugh Dickins if (vma->vm_flags & VM_WRITE) 30141ac0cb5dSHugh Dickins entry = pte_mkwrite(pte_mkdirty(entry)); 30158f4e2101SHugh Dickins 301682b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 301782b0f8c3SJan Kara &vmf->ptl); 301882b0f8c3SJan Kara if (!pte_none(*vmf->pte)) 30198f4e2101SHugh Dickins goto release; 30209ba69294SHugh Dickins 30216b31d595SMichal Hocko ret = check_stable_address_space(vma->vm_mm); 30226b31d595SMichal Hocko if (ret) 30236b31d595SMichal Hocko goto release; 30246b31d595SMichal Hocko 30256b251fc9SAndrea Arcangeli /* Deliver the page fault to userland, check inside PT lock */ 30266b251fc9SAndrea Arcangeli if (userfaultfd_missing(vma)) { 302782b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3028f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 302909cbfeafSKirill A. Shutemov put_page(page); 303082b0f8c3SJan Kara return handle_userfault(vmf, VM_UFFD_MISSING); 30316b251fc9SAndrea Arcangeli } 30326b251fc9SAndrea Arcangeli 3033bae473a4SKirill A. Shutemov inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); 303482b0f8c3SJan Kara page_add_new_anon_rmap(page, vma, vmf->address, false); 3035f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, false); 303600501b53SJohannes Weiner lru_cache_add_active_or_unevictable(page, vma); 3037a13ea5b7SHugh Dickins setpte: 303882b0f8c3SJan Kara set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry); 30391da177e4SLinus Torvalds 30401da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 304182b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 304265500d23SHugh Dickins unlock: 304382b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 30446b31d595SMichal Hocko return ret; 30458f4e2101SHugh Dickins release: 3046f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 304709cbfeafSKirill A. Shutemov put_page(page); 30488f4e2101SHugh Dickins goto unlock; 30498a9f3ccdSBalbir Singh oom_free_page: 305009cbfeafSKirill A. Shutemov put_page(page); 305165500d23SHugh Dickins oom: 30521da177e4SLinus Torvalds return VM_FAULT_OOM; 30531da177e4SLinus Torvalds } 30541da177e4SLinus Torvalds 30559a95f3cfSPaul Cassella /* 30569a95f3cfSPaul Cassella * The mmap_sem must have been held on entry, and may have been 30579a95f3cfSPaul Cassella * released depending on flags and vma->vm_ops->fault() return value. 30589a95f3cfSPaul Cassella * See filemap_fault() and __lock_page_retry(). 30599a95f3cfSPaul Cassella */ 30602b740303SSouptick Joarder static vm_fault_t __do_fault(struct vm_fault *vmf) 30617eae74afSKirill A. Shutemov { 306282b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 30632b740303SSouptick Joarder vm_fault_t ret; 30647eae74afSKirill A. Shutemov 306563f3655fSMichal Hocko /* 306663f3655fSMichal Hocko * Preallocate pte before we take page_lock because this might lead to 306763f3655fSMichal Hocko * deadlocks for memcg reclaim which waits for pages under writeback: 306863f3655fSMichal Hocko * lock_page(A) 306963f3655fSMichal Hocko * SetPageWriteback(A) 307063f3655fSMichal Hocko * unlock_page(A) 307163f3655fSMichal Hocko * lock_page(B) 307263f3655fSMichal Hocko * lock_page(B) 307363f3655fSMichal Hocko * pte_alloc_pne 307463f3655fSMichal Hocko * shrink_page_list 307563f3655fSMichal Hocko * wait_on_page_writeback(A) 307663f3655fSMichal Hocko * SetPageWriteback(B) 307763f3655fSMichal Hocko * unlock_page(B) 307863f3655fSMichal Hocko * # flush A, B to clear the writeback 307963f3655fSMichal Hocko */ 308063f3655fSMichal Hocko if (pmd_none(*vmf->pmd) && !vmf->prealloc_pte) { 308163f3655fSMichal Hocko vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm); 308263f3655fSMichal Hocko if (!vmf->prealloc_pte) 308363f3655fSMichal Hocko return VM_FAULT_OOM; 308463f3655fSMichal Hocko smp_wmb(); /* See comment in __pte_alloc() */ 308563f3655fSMichal Hocko } 308663f3655fSMichal Hocko 308711bac800SDave Jiang ret = vma->vm_ops->fault(vmf); 30883917048dSJan Kara if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY | 3089b1aa812bSJan Kara VM_FAULT_DONE_COW))) 30907eae74afSKirill A. Shutemov return ret; 30917eae74afSKirill A. Shutemov 3092667240e0SJan Kara if (unlikely(PageHWPoison(vmf->page))) { 30937eae74afSKirill A. Shutemov if (ret & VM_FAULT_LOCKED) 3094667240e0SJan Kara unlock_page(vmf->page); 3095667240e0SJan Kara put_page(vmf->page); 3096936ca80dSJan Kara vmf->page = NULL; 30977eae74afSKirill A. Shutemov return VM_FAULT_HWPOISON; 30987eae74afSKirill A. Shutemov } 30997eae74afSKirill A. Shutemov 31007eae74afSKirill A. Shutemov if (unlikely(!(ret & VM_FAULT_LOCKED))) 3101667240e0SJan Kara lock_page(vmf->page); 31027eae74afSKirill A. Shutemov else 3103667240e0SJan Kara VM_BUG_ON_PAGE(!PageLocked(vmf->page), vmf->page); 31047eae74afSKirill A. Shutemov 31057eae74afSKirill A. Shutemov return ret; 31067eae74afSKirill A. Shutemov } 31077eae74afSKirill A. Shutemov 3108d0f0931dSRoss Zwisler /* 3109d0f0931dSRoss Zwisler * The ordering of these checks is important for pmds with _PAGE_DEVMAP set. 3110d0f0931dSRoss Zwisler * If we check pmd_trans_unstable() first we will trip the bad_pmd() check 3111d0f0931dSRoss Zwisler * inside of pmd_none_or_trans_huge_or_clear_bad(). This will end up correctly 3112d0f0931dSRoss Zwisler * returning 1 but not before it spams dmesg with the pmd_clear_bad() output. 3113d0f0931dSRoss Zwisler */ 3114d0f0931dSRoss Zwisler static int pmd_devmap_trans_unstable(pmd_t *pmd) 3115d0f0931dSRoss Zwisler { 3116d0f0931dSRoss Zwisler return pmd_devmap(*pmd) || pmd_trans_unstable(pmd); 3117d0f0931dSRoss Zwisler } 3118d0f0931dSRoss Zwisler 31192b740303SSouptick Joarder static vm_fault_t pte_alloc_one_map(struct vm_fault *vmf) 31207267ec00SKirill A. Shutemov { 312182b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 31227267ec00SKirill A. Shutemov 312382b0f8c3SJan Kara if (!pmd_none(*vmf->pmd)) 31247267ec00SKirill A. Shutemov goto map_pte; 312582b0f8c3SJan Kara if (vmf->prealloc_pte) { 312682b0f8c3SJan Kara vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); 312782b0f8c3SJan Kara if (unlikely(!pmd_none(*vmf->pmd))) { 312882b0f8c3SJan Kara spin_unlock(vmf->ptl); 31297267ec00SKirill A. Shutemov goto map_pte; 31307267ec00SKirill A. Shutemov } 31317267ec00SKirill A. Shutemov 3132c4812909SKirill A. Shutemov mm_inc_nr_ptes(vma->vm_mm); 313382b0f8c3SJan Kara pmd_populate(vma->vm_mm, vmf->pmd, vmf->prealloc_pte); 313482b0f8c3SJan Kara spin_unlock(vmf->ptl); 31357f2b6ce8STobin C Harding vmf->prealloc_pte = NULL; 31364cf58924SJoel Fernandes (Google) } else if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd))) { 31377267ec00SKirill A. Shutemov return VM_FAULT_OOM; 31387267ec00SKirill A. Shutemov } 31397267ec00SKirill A. Shutemov map_pte: 31407267ec00SKirill A. Shutemov /* 31417267ec00SKirill A. Shutemov * If a huge pmd materialized under us just retry later. Use 3142d0f0931dSRoss Zwisler * pmd_trans_unstable() via pmd_devmap_trans_unstable() instead of 3143d0f0931dSRoss Zwisler * pmd_trans_huge() to ensure the pmd didn't become pmd_trans_huge 3144d0f0931dSRoss Zwisler * under us and then back to pmd_none, as a result of MADV_DONTNEED 3145d0f0931dSRoss Zwisler * running immediately after a huge pmd fault in a different thread of 3146d0f0931dSRoss Zwisler * this mm, in turn leading to a misleading pmd_trans_huge() retval. 3147d0f0931dSRoss Zwisler * All we have to ensure is that it is a regular pmd that we can walk 3148d0f0931dSRoss Zwisler * with pte_offset_map() and we can do that through an atomic read in 3149d0f0931dSRoss Zwisler * C, which is what pmd_trans_unstable() provides. 31507267ec00SKirill A. Shutemov */ 3151d0f0931dSRoss Zwisler if (pmd_devmap_trans_unstable(vmf->pmd)) 31527267ec00SKirill A. Shutemov return VM_FAULT_NOPAGE; 31537267ec00SKirill A. Shutemov 3154d0f0931dSRoss Zwisler /* 3155d0f0931dSRoss Zwisler * At this point we know that our vmf->pmd points to a page of ptes 3156d0f0931dSRoss Zwisler * and it cannot become pmd_none(), pmd_devmap() or pmd_trans_huge() 3157d0f0931dSRoss Zwisler * for the duration of the fault. If a racing MADV_DONTNEED runs and 3158d0f0931dSRoss Zwisler * we zap the ptes pointed to by our vmf->pmd, the vmf->ptl will still 3159d0f0931dSRoss Zwisler * be valid and we will re-check to make sure the vmf->pte isn't 3160d0f0931dSRoss Zwisler * pte_none() under vmf->ptl protection when we return to 3161d0f0931dSRoss Zwisler * alloc_set_pte(). 3162d0f0931dSRoss Zwisler */ 316382b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 316482b0f8c3SJan Kara &vmf->ptl); 31657267ec00SKirill A. Shutemov return 0; 31667267ec00SKirill A. Shutemov } 31677267ec00SKirill A. Shutemov 3168e496cf3dSKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 316982b0f8c3SJan Kara static void deposit_prealloc_pte(struct vm_fault *vmf) 3170953c66c2SAneesh Kumar K.V { 317182b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3172953c66c2SAneesh Kumar K.V 317382b0f8c3SJan Kara pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, vmf->prealloc_pte); 3174953c66c2SAneesh Kumar K.V /* 3175953c66c2SAneesh Kumar K.V * We are going to consume the prealloc table, 3176953c66c2SAneesh Kumar K.V * count that as nr_ptes. 3177953c66c2SAneesh Kumar K.V */ 3178c4812909SKirill A. Shutemov mm_inc_nr_ptes(vma->vm_mm); 31797f2b6ce8STobin C Harding vmf->prealloc_pte = NULL; 3180953c66c2SAneesh Kumar K.V } 3181953c66c2SAneesh Kumar K.V 31822b740303SSouptick Joarder static vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page) 318310102459SKirill A. Shutemov { 318482b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 318582b0f8c3SJan Kara bool write = vmf->flags & FAULT_FLAG_WRITE; 318682b0f8c3SJan Kara unsigned long haddr = vmf->address & HPAGE_PMD_MASK; 318710102459SKirill A. Shutemov pmd_t entry; 31882b740303SSouptick Joarder int i; 31892b740303SSouptick Joarder vm_fault_t ret; 319010102459SKirill A. Shutemov 319110102459SKirill A. Shutemov if (!transhuge_vma_suitable(vma, haddr)) 319210102459SKirill A. Shutemov return VM_FAULT_FALLBACK; 319310102459SKirill A. Shutemov 319410102459SKirill A. Shutemov ret = VM_FAULT_FALLBACK; 319510102459SKirill A. Shutemov page = compound_head(page); 319610102459SKirill A. Shutemov 3197953c66c2SAneesh Kumar K.V /* 3198953c66c2SAneesh Kumar K.V * Archs like ppc64 need additonal space to store information 3199953c66c2SAneesh Kumar K.V * related to pte entry. Use the preallocated table for that. 3200953c66c2SAneesh Kumar K.V */ 320182b0f8c3SJan Kara if (arch_needs_pgtable_deposit() && !vmf->prealloc_pte) { 32024cf58924SJoel Fernandes (Google) vmf->prealloc_pte = pte_alloc_one(vma->vm_mm); 320382b0f8c3SJan Kara if (!vmf->prealloc_pte) 3204953c66c2SAneesh Kumar K.V return VM_FAULT_OOM; 3205953c66c2SAneesh Kumar K.V smp_wmb(); /* See comment in __pte_alloc() */ 3206953c66c2SAneesh Kumar K.V } 3207953c66c2SAneesh Kumar K.V 320882b0f8c3SJan Kara vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); 320982b0f8c3SJan Kara if (unlikely(!pmd_none(*vmf->pmd))) 321010102459SKirill A. Shutemov goto out; 321110102459SKirill A. Shutemov 321210102459SKirill A. Shutemov for (i = 0; i < HPAGE_PMD_NR; i++) 321310102459SKirill A. Shutemov flush_icache_page(vma, page + i); 321410102459SKirill A. Shutemov 321510102459SKirill A. Shutemov entry = mk_huge_pmd(page, vma->vm_page_prot); 321610102459SKirill A. Shutemov if (write) 3217f55e1014SLinus Torvalds entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); 321810102459SKirill A. Shutemov 3219fadae295SYang Shi add_mm_counter(vma->vm_mm, mm_counter_file(page), HPAGE_PMD_NR); 322010102459SKirill A. Shutemov page_add_file_rmap(page, true); 3221953c66c2SAneesh Kumar K.V /* 3222953c66c2SAneesh Kumar K.V * deposit and withdraw with pmd lock held 3223953c66c2SAneesh Kumar K.V */ 3224953c66c2SAneesh Kumar K.V if (arch_needs_pgtable_deposit()) 322582b0f8c3SJan Kara deposit_prealloc_pte(vmf); 322610102459SKirill A. Shutemov 322782b0f8c3SJan Kara set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry); 322810102459SKirill A. Shutemov 322982b0f8c3SJan Kara update_mmu_cache_pmd(vma, haddr, vmf->pmd); 323010102459SKirill A. Shutemov 323110102459SKirill A. Shutemov /* fault is handled */ 323210102459SKirill A. Shutemov ret = 0; 323395ecedcdSKirill A. Shutemov count_vm_event(THP_FILE_MAPPED); 323410102459SKirill A. Shutemov out: 323582b0f8c3SJan Kara spin_unlock(vmf->ptl); 323610102459SKirill A. Shutemov return ret; 323710102459SKirill A. Shutemov } 323810102459SKirill A. Shutemov #else 32392b740303SSouptick Joarder static vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page) 324010102459SKirill A. Shutemov { 324110102459SKirill A. Shutemov BUILD_BUG(); 324210102459SKirill A. Shutemov return 0; 324310102459SKirill A. Shutemov } 324410102459SKirill A. Shutemov #endif 324510102459SKirill A. Shutemov 32468c6e50b0SKirill A. Shutemov /** 32477267ec00SKirill A. Shutemov * alloc_set_pte - setup new PTE entry for given page and add reverse page 32487267ec00SKirill A. Shutemov * mapping. If needed, the fucntion allocates page table or use pre-allocated. 32498c6e50b0SKirill A. Shutemov * 325082b0f8c3SJan Kara * @vmf: fault environment 32517267ec00SKirill A. Shutemov * @memcg: memcg to charge page (only for private mappings) 32528c6e50b0SKirill A. Shutemov * @page: page to map 32538c6e50b0SKirill A. Shutemov * 325482b0f8c3SJan Kara * Caller must take care of unlocking vmf->ptl, if vmf->pte is non-NULL on 325582b0f8c3SJan Kara * return. 32568c6e50b0SKirill A. Shutemov * 32578c6e50b0SKirill A. Shutemov * Target users are page handler itself and implementations of 32588c6e50b0SKirill A. Shutemov * vm_ops->map_pages. 3259a862f68aSMike Rapoport * 3260a862f68aSMike Rapoport * Return: %0 on success, %VM_FAULT_ code in case of error. 32618c6e50b0SKirill A. Shutemov */ 32622b740303SSouptick Joarder vm_fault_t alloc_set_pte(struct vm_fault *vmf, struct mem_cgroup *memcg, 32637267ec00SKirill A. Shutemov struct page *page) 32643bb97794SKirill A. Shutemov { 326582b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 326682b0f8c3SJan Kara bool write = vmf->flags & FAULT_FLAG_WRITE; 32673bb97794SKirill A. Shutemov pte_t entry; 32682b740303SSouptick Joarder vm_fault_t ret; 326910102459SKirill A. Shutemov 327082b0f8c3SJan Kara if (pmd_none(*vmf->pmd) && PageTransCompound(page) && 3271e496cf3dSKirill A. Shutemov IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) { 327210102459SKirill A. Shutemov /* THP on COW? */ 327310102459SKirill A. Shutemov VM_BUG_ON_PAGE(memcg, page); 327410102459SKirill A. Shutemov 327582b0f8c3SJan Kara ret = do_set_pmd(vmf, page); 327610102459SKirill A. Shutemov if (ret != VM_FAULT_FALLBACK) 3277b0b9b3dfSHugh Dickins return ret; 327810102459SKirill A. Shutemov } 32793bb97794SKirill A. Shutemov 328082b0f8c3SJan Kara if (!vmf->pte) { 328182b0f8c3SJan Kara ret = pte_alloc_one_map(vmf); 32827267ec00SKirill A. Shutemov if (ret) 3283b0b9b3dfSHugh Dickins return ret; 32847267ec00SKirill A. Shutemov } 32857267ec00SKirill A. Shutemov 32867267ec00SKirill A. Shutemov /* Re-check under ptl */ 3287b0b9b3dfSHugh Dickins if (unlikely(!pte_none(*vmf->pte))) 3288b0b9b3dfSHugh Dickins return VM_FAULT_NOPAGE; 32897267ec00SKirill A. Shutemov 32903bb97794SKirill A. Shutemov flush_icache_page(vma, page); 32913bb97794SKirill A. Shutemov entry = mk_pte(page, vma->vm_page_prot); 32923bb97794SKirill A. Shutemov if (write) 32933bb97794SKirill A. Shutemov entry = maybe_mkwrite(pte_mkdirty(entry), vma); 3294bae473a4SKirill A. Shutemov /* copy-on-write page */ 3295bae473a4SKirill A. Shutemov if (write && !(vma->vm_flags & VM_SHARED)) { 32963bb97794SKirill A. Shutemov inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); 329782b0f8c3SJan Kara page_add_new_anon_rmap(page, vma, vmf->address, false); 32987267ec00SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, false); 32997267ec00SKirill A. Shutemov lru_cache_add_active_or_unevictable(page, vma); 33003bb97794SKirill A. Shutemov } else { 3301eca56ff9SJerome Marchand inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page)); 3302dd78feddSKirill A. Shutemov page_add_file_rmap(page, false); 33033bb97794SKirill A. Shutemov } 330482b0f8c3SJan Kara set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry); 33053bb97794SKirill A. Shutemov 33063bb97794SKirill A. Shutemov /* no need to invalidate: a not-present page won't be cached */ 330782b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 33087267ec00SKirill A. Shutemov 3309b0b9b3dfSHugh Dickins return 0; 33103bb97794SKirill A. Shutemov } 33113bb97794SKirill A. Shutemov 33129118c0cbSJan Kara 33139118c0cbSJan Kara /** 33149118c0cbSJan Kara * finish_fault - finish page fault once we have prepared the page to fault 33159118c0cbSJan Kara * 33169118c0cbSJan Kara * @vmf: structure describing the fault 33179118c0cbSJan Kara * 33189118c0cbSJan Kara * This function handles all that is needed to finish a page fault once the 33199118c0cbSJan Kara * page to fault in is prepared. It handles locking of PTEs, inserts PTE for 33209118c0cbSJan Kara * given page, adds reverse page mapping, handles memcg charges and LRU 3321a862f68aSMike Rapoport * addition. 33229118c0cbSJan Kara * 33239118c0cbSJan Kara * The function expects the page to be locked and on success it consumes a 33249118c0cbSJan Kara * reference of a page being mapped (for the PTE which maps it). 3325a862f68aSMike Rapoport * 3326a862f68aSMike Rapoport * Return: %0 on success, %VM_FAULT_ code in case of error. 33279118c0cbSJan Kara */ 33282b740303SSouptick Joarder vm_fault_t finish_fault(struct vm_fault *vmf) 33299118c0cbSJan Kara { 33309118c0cbSJan Kara struct page *page; 33312b740303SSouptick Joarder vm_fault_t ret = 0; 33329118c0cbSJan Kara 33339118c0cbSJan Kara /* Did we COW the page? */ 33349118c0cbSJan Kara if ((vmf->flags & FAULT_FLAG_WRITE) && 33359118c0cbSJan Kara !(vmf->vma->vm_flags & VM_SHARED)) 33369118c0cbSJan Kara page = vmf->cow_page; 33379118c0cbSJan Kara else 33389118c0cbSJan Kara page = vmf->page; 33396b31d595SMichal Hocko 33406b31d595SMichal Hocko /* 33416b31d595SMichal Hocko * check even for read faults because we might have lost our CoWed 33426b31d595SMichal Hocko * page 33436b31d595SMichal Hocko */ 33446b31d595SMichal Hocko if (!(vmf->vma->vm_flags & VM_SHARED)) 33456b31d595SMichal Hocko ret = check_stable_address_space(vmf->vma->vm_mm); 33466b31d595SMichal Hocko if (!ret) 33479118c0cbSJan Kara ret = alloc_set_pte(vmf, vmf->memcg, page); 33489118c0cbSJan Kara if (vmf->pte) 33499118c0cbSJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 33509118c0cbSJan Kara return ret; 33519118c0cbSJan Kara } 33529118c0cbSJan Kara 33533a91053aSKirill A. Shutemov static unsigned long fault_around_bytes __read_mostly = 33543a91053aSKirill A. Shutemov rounddown_pow_of_two(65536); 3355a9b0f861SKirill A. Shutemov 33561592eef0SKirill A. Shutemov #ifdef CONFIG_DEBUG_FS 3357a9b0f861SKirill A. Shutemov static int fault_around_bytes_get(void *data, u64 *val) 33581592eef0SKirill A. Shutemov { 3359a9b0f861SKirill A. Shutemov *val = fault_around_bytes; 33601592eef0SKirill A. Shutemov return 0; 33611592eef0SKirill A. Shutemov } 33621592eef0SKirill A. Shutemov 3363b4903d6eSAndrey Ryabinin /* 3364da391d64SWilliam Kucharski * fault_around_bytes must be rounded down to the nearest page order as it's 3365da391d64SWilliam Kucharski * what do_fault_around() expects to see. 3366b4903d6eSAndrey Ryabinin */ 3367a9b0f861SKirill A. Shutemov static int fault_around_bytes_set(void *data, u64 val) 33681592eef0SKirill A. Shutemov { 3369a9b0f861SKirill A. Shutemov if (val / PAGE_SIZE > PTRS_PER_PTE) 33701592eef0SKirill A. Shutemov return -EINVAL; 3371b4903d6eSAndrey Ryabinin if (val > PAGE_SIZE) 3372b4903d6eSAndrey Ryabinin fault_around_bytes = rounddown_pow_of_two(val); 3373b4903d6eSAndrey Ryabinin else 3374b4903d6eSAndrey Ryabinin fault_around_bytes = PAGE_SIZE; /* rounddown_pow_of_two(0) is undefined */ 33751592eef0SKirill A. Shutemov return 0; 33761592eef0SKirill A. Shutemov } 33770a1345f8SYevgen Pronenko DEFINE_DEBUGFS_ATTRIBUTE(fault_around_bytes_fops, 3378a9b0f861SKirill A. Shutemov fault_around_bytes_get, fault_around_bytes_set, "%llu\n"); 33791592eef0SKirill A. Shutemov 33801592eef0SKirill A. Shutemov static int __init fault_around_debugfs(void) 33811592eef0SKirill A. Shutemov { 3382d9f7979cSGreg Kroah-Hartman debugfs_create_file_unsafe("fault_around_bytes", 0644, NULL, NULL, 3383a9b0f861SKirill A. Shutemov &fault_around_bytes_fops); 33841592eef0SKirill A. Shutemov return 0; 33851592eef0SKirill A. Shutemov } 33861592eef0SKirill A. Shutemov late_initcall(fault_around_debugfs); 33871592eef0SKirill A. Shutemov #endif 33888c6e50b0SKirill A. Shutemov 33891fdb412bSKirill A. Shutemov /* 33901fdb412bSKirill A. Shutemov * do_fault_around() tries to map few pages around the fault address. The hope 33911fdb412bSKirill A. Shutemov * is that the pages will be needed soon and this will lower the number of 33921fdb412bSKirill A. Shutemov * faults to handle. 33931fdb412bSKirill A. Shutemov * 33941fdb412bSKirill A. Shutemov * It uses vm_ops->map_pages() to map the pages, which skips the page if it's 33951fdb412bSKirill A. Shutemov * not ready to be mapped: not up-to-date, locked, etc. 33961fdb412bSKirill A. Shutemov * 33971fdb412bSKirill A. Shutemov * This function is called with the page table lock taken. In the split ptlock 33981fdb412bSKirill A. Shutemov * case the page table lock only protects only those entries which belong to 33991fdb412bSKirill A. Shutemov * the page table corresponding to the fault address. 34001fdb412bSKirill A. Shutemov * 34011fdb412bSKirill A. Shutemov * This function doesn't cross the VMA boundaries, in order to call map_pages() 34021fdb412bSKirill A. Shutemov * only once. 34031fdb412bSKirill A. Shutemov * 3404da391d64SWilliam Kucharski * fault_around_bytes defines how many bytes we'll try to map. 3405da391d64SWilliam Kucharski * do_fault_around() expects it to be set to a power of two less than or equal 3406da391d64SWilliam Kucharski * to PTRS_PER_PTE. 34071fdb412bSKirill A. Shutemov * 3408da391d64SWilliam Kucharski * The virtual address of the area that we map is naturally aligned to 3409da391d64SWilliam Kucharski * fault_around_bytes rounded down to the machine page size 3410da391d64SWilliam Kucharski * (and therefore to page order). This way it's easier to guarantee 3411da391d64SWilliam Kucharski * that we don't cross page table boundaries. 34121fdb412bSKirill A. Shutemov */ 34132b740303SSouptick Joarder static vm_fault_t do_fault_around(struct vm_fault *vmf) 34148c6e50b0SKirill A. Shutemov { 341582b0f8c3SJan Kara unsigned long address = vmf->address, nr_pages, mask; 34160721ec8bSJan Kara pgoff_t start_pgoff = vmf->pgoff; 3417bae473a4SKirill A. Shutemov pgoff_t end_pgoff; 34182b740303SSouptick Joarder int off; 34192b740303SSouptick Joarder vm_fault_t ret = 0; 34208c6e50b0SKirill A. Shutemov 34214db0c3c2SJason Low nr_pages = READ_ONCE(fault_around_bytes) >> PAGE_SHIFT; 3422aecd6f44SKirill A. Shutemov mask = ~(nr_pages * PAGE_SIZE - 1) & PAGE_MASK; 3423aecd6f44SKirill A. Shutemov 342482b0f8c3SJan Kara vmf->address = max(address & mask, vmf->vma->vm_start); 342582b0f8c3SJan Kara off = ((address - vmf->address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1); 3426bae473a4SKirill A. Shutemov start_pgoff -= off; 34278c6e50b0SKirill A. Shutemov 34288c6e50b0SKirill A. Shutemov /* 3429da391d64SWilliam Kucharski * end_pgoff is either the end of the page table, the end of 3430da391d64SWilliam Kucharski * the vma or nr_pages from start_pgoff, depending what is nearest. 34318c6e50b0SKirill A. Shutemov */ 3432bae473a4SKirill A. Shutemov end_pgoff = start_pgoff - 343382b0f8c3SJan Kara ((vmf->address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) + 34348c6e50b0SKirill A. Shutemov PTRS_PER_PTE - 1; 343582b0f8c3SJan Kara end_pgoff = min3(end_pgoff, vma_pages(vmf->vma) + vmf->vma->vm_pgoff - 1, 3436bae473a4SKirill A. Shutemov start_pgoff + nr_pages - 1); 34378c6e50b0SKirill A. Shutemov 343882b0f8c3SJan Kara if (pmd_none(*vmf->pmd)) { 34394cf58924SJoel Fernandes (Google) vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm); 344082b0f8c3SJan Kara if (!vmf->prealloc_pte) 3441c5f88bd2SVegard Nossum goto out; 34427267ec00SKirill A. Shutemov smp_wmb(); /* See comment in __pte_alloc() */ 34438c6e50b0SKirill A. Shutemov } 34448c6e50b0SKirill A. Shutemov 344582b0f8c3SJan Kara vmf->vma->vm_ops->map_pages(vmf, start_pgoff, end_pgoff); 34467267ec00SKirill A. Shutemov 34477267ec00SKirill A. Shutemov /* Huge page is mapped? Page fault is solved */ 344882b0f8c3SJan Kara if (pmd_trans_huge(*vmf->pmd)) { 34497267ec00SKirill A. Shutemov ret = VM_FAULT_NOPAGE; 34507267ec00SKirill A. Shutemov goto out; 34518c6e50b0SKirill A. Shutemov } 34528c6e50b0SKirill A. Shutemov 34537267ec00SKirill A. Shutemov /* ->map_pages() haven't done anything useful. Cold page cache? */ 345482b0f8c3SJan Kara if (!vmf->pte) 34557267ec00SKirill A. Shutemov goto out; 34567267ec00SKirill A. Shutemov 34577267ec00SKirill A. Shutemov /* check if the page fault is solved */ 345882b0f8c3SJan Kara vmf->pte -= (vmf->address >> PAGE_SHIFT) - (address >> PAGE_SHIFT); 345982b0f8c3SJan Kara if (!pte_none(*vmf->pte)) 34607267ec00SKirill A. Shutemov ret = VM_FAULT_NOPAGE; 346182b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 34627267ec00SKirill A. Shutemov out: 346382b0f8c3SJan Kara vmf->address = address; 346482b0f8c3SJan Kara vmf->pte = NULL; 34657267ec00SKirill A. Shutemov return ret; 34667267ec00SKirill A. Shutemov } 34677267ec00SKirill A. Shutemov 34682b740303SSouptick Joarder static vm_fault_t do_read_fault(struct vm_fault *vmf) 3469e655fb29SKirill A. Shutemov { 347082b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 34712b740303SSouptick Joarder vm_fault_t ret = 0; 34728c6e50b0SKirill A. Shutemov 34738c6e50b0SKirill A. Shutemov /* 34748c6e50b0SKirill A. Shutemov * Let's call ->map_pages() first and use ->fault() as fallback 34758c6e50b0SKirill A. Shutemov * if page by the offset is not ready to be mapped (cold cache or 34768c6e50b0SKirill A. Shutemov * something). 34778c6e50b0SKirill A. Shutemov */ 34789b4bdd2fSKirill A. Shutemov if (vma->vm_ops->map_pages && fault_around_bytes >> PAGE_SHIFT > 1) { 34790721ec8bSJan Kara ret = do_fault_around(vmf); 34807267ec00SKirill A. Shutemov if (ret) 34817267ec00SKirill A. Shutemov return ret; 34828c6e50b0SKirill A. Shutemov } 3483e655fb29SKirill A. Shutemov 3484936ca80dSJan Kara ret = __do_fault(vmf); 3485e655fb29SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3486e655fb29SKirill A. Shutemov return ret; 3487e655fb29SKirill A. Shutemov 34889118c0cbSJan Kara ret |= finish_fault(vmf); 3489936ca80dSJan Kara unlock_page(vmf->page); 34907267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3491936ca80dSJan Kara put_page(vmf->page); 3492e655fb29SKirill A. Shutemov return ret; 3493e655fb29SKirill A. Shutemov } 3494e655fb29SKirill A. Shutemov 34952b740303SSouptick Joarder static vm_fault_t do_cow_fault(struct vm_fault *vmf) 3496ec47c3b9SKirill A. Shutemov { 349782b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 34982b740303SSouptick Joarder vm_fault_t ret; 3499ec47c3b9SKirill A. Shutemov 3500ec47c3b9SKirill A. Shutemov if (unlikely(anon_vma_prepare(vma))) 3501ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 3502ec47c3b9SKirill A. Shutemov 3503936ca80dSJan Kara vmf->cow_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vmf->address); 3504936ca80dSJan Kara if (!vmf->cow_page) 3505ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 3506ec47c3b9SKirill A. Shutemov 35072cf85583STejun Heo if (mem_cgroup_try_charge_delay(vmf->cow_page, vma->vm_mm, GFP_KERNEL, 35083917048dSJan Kara &vmf->memcg, false)) { 3509936ca80dSJan Kara put_page(vmf->cow_page); 3510ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 3511ec47c3b9SKirill A. Shutemov } 3512ec47c3b9SKirill A. Shutemov 3513936ca80dSJan Kara ret = __do_fault(vmf); 3514ec47c3b9SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3515ec47c3b9SKirill A. Shutemov goto uncharge_out; 35163917048dSJan Kara if (ret & VM_FAULT_DONE_COW) 35173917048dSJan Kara return ret; 3518ec47c3b9SKirill A. Shutemov 3519936ca80dSJan Kara copy_user_highpage(vmf->cow_page, vmf->page, vmf->address, vma); 3520936ca80dSJan Kara __SetPageUptodate(vmf->cow_page); 3521ec47c3b9SKirill A. Shutemov 35229118c0cbSJan Kara ret |= finish_fault(vmf); 3523936ca80dSJan Kara unlock_page(vmf->page); 3524936ca80dSJan Kara put_page(vmf->page); 35257267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 35267267ec00SKirill A. Shutemov goto uncharge_out; 3527ec47c3b9SKirill A. Shutemov return ret; 3528ec47c3b9SKirill A. Shutemov uncharge_out: 35293917048dSJan Kara mem_cgroup_cancel_charge(vmf->cow_page, vmf->memcg, false); 3530936ca80dSJan Kara put_page(vmf->cow_page); 3531ec47c3b9SKirill A. Shutemov return ret; 3532ec47c3b9SKirill A. Shutemov } 3533ec47c3b9SKirill A. Shutemov 35342b740303SSouptick Joarder static vm_fault_t do_shared_fault(struct vm_fault *vmf) 35351da177e4SLinus Torvalds { 353682b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 35372b740303SSouptick Joarder vm_fault_t ret, tmp; 35381d65f86dSKAMEZAWA Hiroyuki 3539936ca80dSJan Kara ret = __do_fault(vmf); 35407eae74afSKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 3541f0c6d4d2SKirill A. Shutemov return ret; 35421da177e4SLinus Torvalds 35431da177e4SLinus Torvalds /* 3544f0c6d4d2SKirill A. Shutemov * Check if the backing address space wants to know that the page is 3545f0c6d4d2SKirill A. Shutemov * about to become writable 35461da177e4SLinus Torvalds */ 3547fb09a464SKirill A. Shutemov if (vma->vm_ops->page_mkwrite) { 3548936ca80dSJan Kara unlock_page(vmf->page); 354938b8cb7fSJan Kara tmp = do_page_mkwrite(vmf); 3550fb09a464SKirill A. Shutemov if (unlikely(!tmp || 3551fb09a464SKirill A. Shutemov (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { 3552936ca80dSJan Kara put_page(vmf->page); 3553f0c6d4d2SKirill A. Shutemov return tmp; 355469676147SMark Fasheh } 3555d0217ac0SNick Piggin } 3556fb09a464SKirill A. Shutemov 35579118c0cbSJan Kara ret |= finish_fault(vmf); 35587267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | 35597267ec00SKirill A. Shutemov VM_FAULT_RETRY))) { 3560936ca80dSJan Kara unlock_page(vmf->page); 3561936ca80dSJan Kara put_page(vmf->page); 3562f0c6d4d2SKirill A. Shutemov return ret; 35639637a5efSDavid Howells } 3564d00806b1SNick Piggin 356597ba0c2bSJan Kara fault_dirty_shared_page(vma, vmf->page); 3566b827e496SNick Piggin return ret; 356754cb8821SNick Piggin } 3568d00806b1SNick Piggin 35699a95f3cfSPaul Cassella /* 35709a95f3cfSPaul Cassella * We enter with non-exclusive mmap_sem (to exclude vma changes, 35719a95f3cfSPaul Cassella * but allow concurrent faults). 35729a95f3cfSPaul Cassella * The mmap_sem may have been released depending on flags and our 35739a95f3cfSPaul Cassella * return value. See filemap_fault() and __lock_page_or_retry(). 3574fc8efd2dSJan Stancek * If mmap_sem is released, vma may become invalid (for example 3575fc8efd2dSJan Stancek * by other thread calling munmap()). 35769a95f3cfSPaul Cassella */ 35772b740303SSouptick Joarder static vm_fault_t do_fault(struct vm_fault *vmf) 357854cb8821SNick Piggin { 357982b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3580fc8efd2dSJan Stancek struct mm_struct *vm_mm = vma->vm_mm; 35812b740303SSouptick Joarder vm_fault_t ret; 358254cb8821SNick Piggin 3583ff09d7ecSAneesh Kumar K.V /* 3584ff09d7ecSAneesh Kumar K.V * The VMA was not fully populated on mmap() or missing VM_DONTEXPAND 3585ff09d7ecSAneesh Kumar K.V */ 3586ff09d7ecSAneesh Kumar K.V if (!vma->vm_ops->fault) { 3587ff09d7ecSAneesh Kumar K.V /* 3588ff09d7ecSAneesh Kumar K.V * If we find a migration pmd entry or a none pmd entry, which 3589ff09d7ecSAneesh Kumar K.V * should never happen, return SIGBUS 3590ff09d7ecSAneesh Kumar K.V */ 3591ff09d7ecSAneesh Kumar K.V if (unlikely(!pmd_present(*vmf->pmd))) 3592b0b9b3dfSHugh Dickins ret = VM_FAULT_SIGBUS; 3593ff09d7ecSAneesh Kumar K.V else { 3594ff09d7ecSAneesh Kumar K.V vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, 3595ff09d7ecSAneesh Kumar K.V vmf->pmd, 3596ff09d7ecSAneesh Kumar K.V vmf->address, 3597ff09d7ecSAneesh Kumar K.V &vmf->ptl); 3598ff09d7ecSAneesh Kumar K.V /* 3599ff09d7ecSAneesh Kumar K.V * Make sure this is not a temporary clearing of pte 3600ff09d7ecSAneesh Kumar K.V * by holding ptl and checking again. A R/M/W update 3601ff09d7ecSAneesh Kumar K.V * of pte involves: take ptl, clearing the pte so that 3602ff09d7ecSAneesh Kumar K.V * we don't have concurrent modification by hardware 3603ff09d7ecSAneesh Kumar K.V * followed by an update. 3604ff09d7ecSAneesh Kumar K.V */ 3605ff09d7ecSAneesh Kumar K.V if (unlikely(pte_none(*vmf->pte))) 3606ff09d7ecSAneesh Kumar K.V ret = VM_FAULT_SIGBUS; 3607ff09d7ecSAneesh Kumar K.V else 3608ff09d7ecSAneesh Kumar K.V ret = VM_FAULT_NOPAGE; 3609ff09d7ecSAneesh Kumar K.V 3610ff09d7ecSAneesh Kumar K.V pte_unmap_unlock(vmf->pte, vmf->ptl); 3611ff09d7ecSAneesh Kumar K.V } 3612ff09d7ecSAneesh Kumar K.V } else if (!(vmf->flags & FAULT_FLAG_WRITE)) 3613b0b9b3dfSHugh Dickins ret = do_read_fault(vmf); 3614b0b9b3dfSHugh Dickins else if (!(vma->vm_flags & VM_SHARED)) 3615b0b9b3dfSHugh Dickins ret = do_cow_fault(vmf); 3616b0b9b3dfSHugh Dickins else 3617b0b9b3dfSHugh Dickins ret = do_shared_fault(vmf); 3618b0b9b3dfSHugh Dickins 3619b0b9b3dfSHugh Dickins /* preallocated pagetable is unused: free it */ 3620b0b9b3dfSHugh Dickins if (vmf->prealloc_pte) { 3621fc8efd2dSJan Stancek pte_free(vm_mm, vmf->prealloc_pte); 36227f2b6ce8STobin C Harding vmf->prealloc_pte = NULL; 3623b0b9b3dfSHugh Dickins } 3624b0b9b3dfSHugh Dickins return ret; 362554cb8821SNick Piggin } 362654cb8821SNick Piggin 3627b19a9939SRashika Kheria static int numa_migrate_prep(struct page *page, struct vm_area_struct *vma, 362804bb2f94SRik van Riel unsigned long addr, int page_nid, 362904bb2f94SRik van Riel int *flags) 36309532fec1SMel Gorman { 36319532fec1SMel Gorman get_page(page); 36329532fec1SMel Gorman 36339532fec1SMel Gorman count_vm_numa_event(NUMA_HINT_FAULTS); 363404bb2f94SRik van Riel if (page_nid == numa_node_id()) { 36359532fec1SMel Gorman count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL); 363604bb2f94SRik van Riel *flags |= TNF_FAULT_LOCAL; 363704bb2f94SRik van Riel } 36389532fec1SMel Gorman 36399532fec1SMel Gorman return mpol_misplaced(page, vma, addr); 36409532fec1SMel Gorman } 36419532fec1SMel Gorman 36422b740303SSouptick Joarder static vm_fault_t do_numa_page(struct vm_fault *vmf) 3643d10e63f2SMel Gorman { 364482b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 36454daae3b4SMel Gorman struct page *page = NULL; 364698fa15f3SAnshuman Khandual int page_nid = NUMA_NO_NODE; 364790572890SPeter Zijlstra int last_cpupid; 3648cbee9f88SPeter Zijlstra int target_nid; 3649b8593bfdSMel Gorman bool migrated = false; 365004a86453SAneesh Kumar K.V pte_t pte, old_pte; 3651288bc549SAneesh Kumar K.V bool was_writable = pte_savedwrite(vmf->orig_pte); 36526688cc05SPeter Zijlstra int flags = 0; 3653d10e63f2SMel Gorman 3654d10e63f2SMel Gorman /* 3655d10e63f2SMel Gorman * The "pte" at this point cannot be used safely without 3656d10e63f2SMel Gorman * validation through pte_unmap_same(). It's of NUMA type but 3657d10e63f2SMel Gorman * the pfn may be screwed if the read is non atomic. 3658d10e63f2SMel Gorman */ 365982b0f8c3SJan Kara vmf->ptl = pte_lockptr(vma->vm_mm, vmf->pmd); 366082b0f8c3SJan Kara spin_lock(vmf->ptl); 3661cee216a6SAneesh Kumar K.V if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) { 366282b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 36634daae3b4SMel Gorman goto out; 36644daae3b4SMel Gorman } 36654daae3b4SMel Gorman 3666cee216a6SAneesh Kumar K.V /* 3667cee216a6SAneesh Kumar K.V * Make it present again, Depending on how arch implementes non 3668cee216a6SAneesh Kumar K.V * accessible ptes, some can allow access by kernel mode. 3669cee216a6SAneesh Kumar K.V */ 367004a86453SAneesh Kumar K.V old_pte = ptep_modify_prot_start(vma, vmf->address, vmf->pte); 367104a86453SAneesh Kumar K.V pte = pte_modify(old_pte, vma->vm_page_prot); 36724d942466SMel Gorman pte = pte_mkyoung(pte); 3673b191f9b1SMel Gorman if (was_writable) 3674b191f9b1SMel Gorman pte = pte_mkwrite(pte); 367504a86453SAneesh Kumar K.V ptep_modify_prot_commit(vma, vmf->address, vmf->pte, old_pte, pte); 367682b0f8c3SJan Kara update_mmu_cache(vma, vmf->address, vmf->pte); 3677d10e63f2SMel Gorman 367882b0f8c3SJan Kara page = vm_normal_page(vma, vmf->address, pte); 3679d10e63f2SMel Gorman if (!page) { 368082b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3681d10e63f2SMel Gorman return 0; 3682d10e63f2SMel Gorman } 3683d10e63f2SMel Gorman 3684e81c4802SKirill A. Shutemov /* TODO: handle PTE-mapped THP */ 3685e81c4802SKirill A. Shutemov if (PageCompound(page)) { 368682b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3687e81c4802SKirill A. Shutemov return 0; 3688e81c4802SKirill A. Shutemov } 3689e81c4802SKirill A. Shutemov 36906688cc05SPeter Zijlstra /* 3691bea66fbdSMel Gorman * Avoid grouping on RO pages in general. RO pages shouldn't hurt as 3692bea66fbdSMel Gorman * much anyway since they can be in shared cache state. This misses 3693bea66fbdSMel Gorman * the case where a mapping is writable but the process never writes 3694bea66fbdSMel Gorman * to it but pte_write gets cleared during protection updates and 3695bea66fbdSMel Gorman * pte_dirty has unpredictable behaviour between PTE scan updates, 3696bea66fbdSMel Gorman * background writeback, dirty balancing and application behaviour. 36976688cc05SPeter Zijlstra */ 3698d59dc7bcSRik van Riel if (!pte_write(pte)) 36996688cc05SPeter Zijlstra flags |= TNF_NO_GROUP; 37006688cc05SPeter Zijlstra 3701dabe1d99SRik van Riel /* 3702dabe1d99SRik van Riel * Flag if the page is shared between multiple address spaces. This 3703dabe1d99SRik van Riel * is later used when determining whether to group tasks together 3704dabe1d99SRik van Riel */ 3705dabe1d99SRik van Riel if (page_mapcount(page) > 1 && (vma->vm_flags & VM_SHARED)) 3706dabe1d99SRik van Riel flags |= TNF_SHARED; 3707dabe1d99SRik van Riel 370890572890SPeter Zijlstra last_cpupid = page_cpupid_last(page); 37098191acbdSMel Gorman page_nid = page_to_nid(page); 371082b0f8c3SJan Kara target_nid = numa_migrate_prep(page, vma, vmf->address, page_nid, 3711bae473a4SKirill A. Shutemov &flags); 371282b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 371398fa15f3SAnshuman Khandual if (target_nid == NUMA_NO_NODE) { 37144daae3b4SMel Gorman put_page(page); 37154daae3b4SMel Gorman goto out; 37164daae3b4SMel Gorman } 37174daae3b4SMel Gorman 37184daae3b4SMel Gorman /* Migrate to the requested node */ 37191bc115d8SMel Gorman migrated = migrate_misplaced_page(page, vma, target_nid); 37206688cc05SPeter Zijlstra if (migrated) { 37218191acbdSMel Gorman page_nid = target_nid; 37226688cc05SPeter Zijlstra flags |= TNF_MIGRATED; 3723074c2381SMel Gorman } else 3724074c2381SMel Gorman flags |= TNF_MIGRATE_FAIL; 37254daae3b4SMel Gorman 37264daae3b4SMel Gorman out: 372798fa15f3SAnshuman Khandual if (page_nid != NUMA_NO_NODE) 37286688cc05SPeter Zijlstra task_numa_fault(last_cpupid, page_nid, 1, flags); 3729d10e63f2SMel Gorman return 0; 3730d10e63f2SMel Gorman } 3731d10e63f2SMel Gorman 37322b740303SSouptick Joarder static inline vm_fault_t create_huge_pmd(struct vm_fault *vmf) 3733b96375f7SMatthew Wilcox { 3734f4200391SDave Jiang if (vma_is_anonymous(vmf->vma)) 373582b0f8c3SJan Kara return do_huge_pmd_anonymous_page(vmf); 3736a2d58167SDave Jiang if (vmf->vma->vm_ops->huge_fault) 3737c791ace1SDave Jiang return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD); 3738b96375f7SMatthew Wilcox return VM_FAULT_FALLBACK; 3739b96375f7SMatthew Wilcox } 3740b96375f7SMatthew Wilcox 3741183f24aaSGeert Uytterhoeven /* `inline' is required to avoid gcc 4.1.2 build error */ 37422b740303SSouptick Joarder static inline vm_fault_t wp_huge_pmd(struct vm_fault *vmf, pmd_t orig_pmd) 3743b96375f7SMatthew Wilcox { 374482b0f8c3SJan Kara if (vma_is_anonymous(vmf->vma)) 374582b0f8c3SJan Kara return do_huge_pmd_wp_page(vmf, orig_pmd); 3746a2d58167SDave Jiang if (vmf->vma->vm_ops->huge_fault) 3747c791ace1SDave Jiang return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD); 3748af9e4d5fSKirill A. Shutemov 3749af9e4d5fSKirill A. Shutemov /* COW handled on pte level: split pmd */ 375082b0f8c3SJan Kara VM_BUG_ON_VMA(vmf->vma->vm_flags & VM_SHARED, vmf->vma); 375182b0f8c3SJan Kara __split_huge_pmd(vmf->vma, vmf->pmd, vmf->address, false, NULL); 3752af9e4d5fSKirill A. Shutemov 3753b96375f7SMatthew Wilcox return VM_FAULT_FALLBACK; 3754b96375f7SMatthew Wilcox } 3755b96375f7SMatthew Wilcox 375638e08854SLorenzo Stoakes static inline bool vma_is_accessible(struct vm_area_struct *vma) 375738e08854SLorenzo Stoakes { 375838e08854SLorenzo Stoakes return vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE); 375938e08854SLorenzo Stoakes } 376038e08854SLorenzo Stoakes 37612b740303SSouptick Joarder static vm_fault_t create_huge_pud(struct vm_fault *vmf) 3762a00cc7d9SMatthew Wilcox { 3763a00cc7d9SMatthew Wilcox #ifdef CONFIG_TRANSPARENT_HUGEPAGE 3764a00cc7d9SMatthew Wilcox /* No support for anonymous transparent PUD pages yet */ 3765a00cc7d9SMatthew Wilcox if (vma_is_anonymous(vmf->vma)) 3766a00cc7d9SMatthew Wilcox return VM_FAULT_FALLBACK; 3767a00cc7d9SMatthew Wilcox if (vmf->vma->vm_ops->huge_fault) 3768c791ace1SDave Jiang return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD); 3769a00cc7d9SMatthew Wilcox #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 3770a00cc7d9SMatthew Wilcox return VM_FAULT_FALLBACK; 3771a00cc7d9SMatthew Wilcox } 3772a00cc7d9SMatthew Wilcox 37732b740303SSouptick Joarder static vm_fault_t wp_huge_pud(struct vm_fault *vmf, pud_t orig_pud) 3774a00cc7d9SMatthew Wilcox { 3775a00cc7d9SMatthew Wilcox #ifdef CONFIG_TRANSPARENT_HUGEPAGE 3776a00cc7d9SMatthew Wilcox /* No support for anonymous transparent PUD pages yet */ 3777a00cc7d9SMatthew Wilcox if (vma_is_anonymous(vmf->vma)) 3778a00cc7d9SMatthew Wilcox return VM_FAULT_FALLBACK; 3779a00cc7d9SMatthew Wilcox if (vmf->vma->vm_ops->huge_fault) 3780c791ace1SDave Jiang return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD); 3781a00cc7d9SMatthew Wilcox #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 3782a00cc7d9SMatthew Wilcox return VM_FAULT_FALLBACK; 3783a00cc7d9SMatthew Wilcox } 3784a00cc7d9SMatthew Wilcox 37851da177e4SLinus Torvalds /* 37861da177e4SLinus Torvalds * These routines also need to handle stuff like marking pages dirty 37871da177e4SLinus Torvalds * and/or accessed for architectures that don't do it in hardware (most 37881da177e4SLinus Torvalds * RISC architectures). The early dirtying is also good on the i386. 37891da177e4SLinus Torvalds * 37901da177e4SLinus Torvalds * There is also a hook called "update_mmu_cache()" that architectures 37911da177e4SLinus Torvalds * with external mmu caches can use to update those (ie the Sparc or 37921da177e4SLinus Torvalds * PowerPC hashed page tables that act as extended TLBs). 37931da177e4SLinus Torvalds * 37947267ec00SKirill A. Shutemov * We enter with non-exclusive mmap_sem (to exclude vma changes, but allow 37957267ec00SKirill A. Shutemov * concurrent faults). 37969a95f3cfSPaul Cassella * 37977267ec00SKirill A. Shutemov * The mmap_sem may have been released depending on flags and our return value. 37987267ec00SKirill A. Shutemov * See filemap_fault() and __lock_page_or_retry(). 37991da177e4SLinus Torvalds */ 38002b740303SSouptick Joarder static vm_fault_t handle_pte_fault(struct vm_fault *vmf) 38011da177e4SLinus Torvalds { 38021da177e4SLinus Torvalds pte_t entry; 38031da177e4SLinus Torvalds 380482b0f8c3SJan Kara if (unlikely(pmd_none(*vmf->pmd))) { 38057267ec00SKirill A. Shutemov /* 38067267ec00SKirill A. Shutemov * Leave __pte_alloc() until later: because vm_ops->fault may 38077267ec00SKirill A. Shutemov * want to allocate huge page, and if we expose page table 38087267ec00SKirill A. Shutemov * for an instant, it will be difficult to retract from 38097267ec00SKirill A. Shutemov * concurrent faults and from rmap lookups. 38107267ec00SKirill A. Shutemov */ 381182b0f8c3SJan Kara vmf->pte = NULL; 38127267ec00SKirill A. Shutemov } else { 38137267ec00SKirill A. Shutemov /* See comment in pte_alloc_one_map() */ 3814d0f0931dSRoss Zwisler if (pmd_devmap_trans_unstable(vmf->pmd)) 38157267ec00SKirill A. Shutemov return 0; 38167267ec00SKirill A. Shutemov /* 38177267ec00SKirill A. Shutemov * A regular pmd is established and it can't morph into a huge 38187267ec00SKirill A. Shutemov * pmd from under us anymore at this point because we hold the 38197267ec00SKirill A. Shutemov * mmap_sem read mode and khugepaged takes it in write mode. 38207267ec00SKirill A. Shutemov * So now it's safe to run pte_offset_map(). 38217267ec00SKirill A. Shutemov */ 382282b0f8c3SJan Kara vmf->pte = pte_offset_map(vmf->pmd, vmf->address); 38232994302bSJan Kara vmf->orig_pte = *vmf->pte; 38247267ec00SKirill A. Shutemov 3825e37c6982SChristian Borntraeger /* 3826e37c6982SChristian Borntraeger * some architectures can have larger ptes than wordsize, 38277267ec00SKirill A. Shutemov * e.g.ppc44x-defconfig has CONFIG_PTE_64BIT=y and 3828b03a0fe0SPaul E. McKenney * CONFIG_32BIT=y, so READ_ONCE cannot guarantee atomic 3829b03a0fe0SPaul E. McKenney * accesses. The code below just needs a consistent view 3830b03a0fe0SPaul E. McKenney * for the ifs and we later double check anyway with the 38317267ec00SKirill A. Shutemov * ptl lock held. So here a barrier will do. 3832e37c6982SChristian Borntraeger */ 3833e37c6982SChristian Borntraeger barrier(); 38342994302bSJan Kara if (pte_none(vmf->orig_pte)) { 383582b0f8c3SJan Kara pte_unmap(vmf->pte); 383682b0f8c3SJan Kara vmf->pte = NULL; 38377267ec00SKirill A. Shutemov } 38387267ec00SKirill A. Shutemov } 38397267ec00SKirill A. Shutemov 384082b0f8c3SJan Kara if (!vmf->pte) { 384182b0f8c3SJan Kara if (vma_is_anonymous(vmf->vma)) 384282b0f8c3SJan Kara return do_anonymous_page(vmf); 3843b5330628SOleg Nesterov else 384482b0f8c3SJan Kara return do_fault(vmf); 384565500d23SHugh Dickins } 38467267ec00SKirill A. Shutemov 38472994302bSJan Kara if (!pte_present(vmf->orig_pte)) 38482994302bSJan Kara return do_swap_page(vmf); 38491da177e4SLinus Torvalds 38502994302bSJan Kara if (pte_protnone(vmf->orig_pte) && vma_is_accessible(vmf->vma)) 38512994302bSJan Kara return do_numa_page(vmf); 3852d10e63f2SMel Gorman 385382b0f8c3SJan Kara vmf->ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd); 385482b0f8c3SJan Kara spin_lock(vmf->ptl); 38552994302bSJan Kara entry = vmf->orig_pte; 385682b0f8c3SJan Kara if (unlikely(!pte_same(*vmf->pte, entry))) 38578f4e2101SHugh Dickins goto unlock; 385882b0f8c3SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) { 3859f6f37321SLinus Torvalds if (!pte_write(entry)) 38602994302bSJan Kara return do_wp_page(vmf); 38611da177e4SLinus Torvalds entry = pte_mkdirty(entry); 38621da177e4SLinus Torvalds } 38631da177e4SLinus Torvalds entry = pte_mkyoung(entry); 386482b0f8c3SJan Kara if (ptep_set_access_flags(vmf->vma, vmf->address, vmf->pte, entry, 386582b0f8c3SJan Kara vmf->flags & FAULT_FLAG_WRITE)) { 386682b0f8c3SJan Kara update_mmu_cache(vmf->vma, vmf->address, vmf->pte); 38671a44e149SAndrea Arcangeli } else { 38681a44e149SAndrea Arcangeli /* 38691a44e149SAndrea Arcangeli * This is needed only for protection faults but the arch code 38701a44e149SAndrea Arcangeli * is not yet telling us if this is a protection fault or not. 38711a44e149SAndrea Arcangeli * This still avoids useless tlb flushes for .text page faults 38721a44e149SAndrea Arcangeli * with threads. 38731a44e149SAndrea Arcangeli */ 387482b0f8c3SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) 387582b0f8c3SJan Kara flush_tlb_fix_spurious_fault(vmf->vma, vmf->address); 38761a44e149SAndrea Arcangeli } 38778f4e2101SHugh Dickins unlock: 387882b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 387983c54070SNick Piggin return 0; 38801da177e4SLinus Torvalds } 38811da177e4SLinus Torvalds 38821da177e4SLinus Torvalds /* 38831da177e4SLinus Torvalds * By the time we get here, we already hold the mm semaphore 38849a95f3cfSPaul Cassella * 38859a95f3cfSPaul Cassella * The mmap_sem may have been released depending on flags and our 38869a95f3cfSPaul Cassella * return value. See filemap_fault() and __lock_page_or_retry(). 38871da177e4SLinus Torvalds */ 38882b740303SSouptick Joarder static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma, 38892b740303SSouptick Joarder unsigned long address, unsigned int flags) 38901da177e4SLinus Torvalds { 389182b0f8c3SJan Kara struct vm_fault vmf = { 3892bae473a4SKirill A. Shutemov .vma = vma, 38931a29d85eSJan Kara .address = address & PAGE_MASK, 3894bae473a4SKirill A. Shutemov .flags = flags, 38950721ec8bSJan Kara .pgoff = linear_page_index(vma, address), 3896667240e0SJan Kara .gfp_mask = __get_fault_gfp_mask(vma), 3897bae473a4SKirill A. Shutemov }; 3898fde26bedSAnshuman Khandual unsigned int dirty = flags & FAULT_FLAG_WRITE; 3899dcddffd4SKirill A. Shutemov struct mm_struct *mm = vma->vm_mm; 39001da177e4SLinus Torvalds pgd_t *pgd; 3901c2febafcSKirill A. Shutemov p4d_t *p4d; 39022b740303SSouptick Joarder vm_fault_t ret; 39031da177e4SLinus Torvalds 39041da177e4SLinus Torvalds pgd = pgd_offset(mm, address); 3905c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, address); 3906c2febafcSKirill A. Shutemov if (!p4d) 3907c2febafcSKirill A. Shutemov return VM_FAULT_OOM; 3908a00cc7d9SMatthew Wilcox 3909c2febafcSKirill A. Shutemov vmf.pud = pud_alloc(mm, p4d, address); 3910a00cc7d9SMatthew Wilcox if (!vmf.pud) 3911c74df32cSHugh Dickins return VM_FAULT_OOM; 39127635d9cbSMichal Hocko if (pud_none(*vmf.pud) && __transparent_hugepage_enabled(vma)) { 3913a00cc7d9SMatthew Wilcox ret = create_huge_pud(&vmf); 3914a00cc7d9SMatthew Wilcox if (!(ret & VM_FAULT_FALLBACK)) 3915a00cc7d9SMatthew Wilcox return ret; 3916a00cc7d9SMatthew Wilcox } else { 3917a00cc7d9SMatthew Wilcox pud_t orig_pud = *vmf.pud; 3918a00cc7d9SMatthew Wilcox 3919a00cc7d9SMatthew Wilcox barrier(); 3920a00cc7d9SMatthew Wilcox if (pud_trans_huge(orig_pud) || pud_devmap(orig_pud)) { 3921a00cc7d9SMatthew Wilcox 3922a00cc7d9SMatthew Wilcox /* NUMA case for anonymous PUDs would go here */ 3923a00cc7d9SMatthew Wilcox 3924f6f37321SLinus Torvalds if (dirty && !pud_write(orig_pud)) { 3925a00cc7d9SMatthew Wilcox ret = wp_huge_pud(&vmf, orig_pud); 3926a00cc7d9SMatthew Wilcox if (!(ret & VM_FAULT_FALLBACK)) 3927a00cc7d9SMatthew Wilcox return ret; 3928a00cc7d9SMatthew Wilcox } else { 3929a00cc7d9SMatthew Wilcox huge_pud_set_accessed(&vmf, orig_pud); 3930a00cc7d9SMatthew Wilcox return 0; 3931a00cc7d9SMatthew Wilcox } 3932a00cc7d9SMatthew Wilcox } 3933a00cc7d9SMatthew Wilcox } 3934a00cc7d9SMatthew Wilcox 3935a00cc7d9SMatthew Wilcox vmf.pmd = pmd_alloc(mm, vmf.pud, address); 393682b0f8c3SJan Kara if (!vmf.pmd) 3937c74df32cSHugh Dickins return VM_FAULT_OOM; 39387635d9cbSMichal Hocko if (pmd_none(*vmf.pmd) && __transparent_hugepage_enabled(vma)) { 3939a2d58167SDave Jiang ret = create_huge_pmd(&vmf); 3940c0292554SKirill A. Shutemov if (!(ret & VM_FAULT_FALLBACK)) 3941c0292554SKirill A. Shutemov return ret; 394271e3aac0SAndrea Arcangeli } else { 394382b0f8c3SJan Kara pmd_t orig_pmd = *vmf.pmd; 39441f1d06c3SDavid Rientjes 394571e3aac0SAndrea Arcangeli barrier(); 394684c3fc4eSZi Yan if (unlikely(is_swap_pmd(orig_pmd))) { 394784c3fc4eSZi Yan VM_BUG_ON(thp_migration_supported() && 394884c3fc4eSZi Yan !is_pmd_migration_entry(orig_pmd)); 394984c3fc4eSZi Yan if (is_pmd_migration_entry(orig_pmd)) 395084c3fc4eSZi Yan pmd_migration_entry_wait(mm, vmf.pmd); 395184c3fc4eSZi Yan return 0; 395284c3fc4eSZi Yan } 39535c7fb56eSDan Williams if (pmd_trans_huge(orig_pmd) || pmd_devmap(orig_pmd)) { 395438e08854SLorenzo Stoakes if (pmd_protnone(orig_pmd) && vma_is_accessible(vma)) 395582b0f8c3SJan Kara return do_huge_pmd_numa_page(&vmf, orig_pmd); 3956d10e63f2SMel Gorman 3957f6f37321SLinus Torvalds if (dirty && !pmd_write(orig_pmd)) { 395882b0f8c3SJan Kara ret = wp_huge_pmd(&vmf, orig_pmd); 39599845cbbdSKirill A. Shutemov if (!(ret & VM_FAULT_FALLBACK)) 39601f1d06c3SDavid Rientjes return ret; 3961a1dd450bSWill Deacon } else { 396282b0f8c3SJan Kara huge_pmd_set_accessed(&vmf, orig_pmd); 396371e3aac0SAndrea Arcangeli return 0; 396471e3aac0SAndrea Arcangeli } 396571e3aac0SAndrea Arcangeli } 39669845cbbdSKirill A. Shutemov } 396771e3aac0SAndrea Arcangeli 396882b0f8c3SJan Kara return handle_pte_fault(&vmf); 39691da177e4SLinus Torvalds } 39701da177e4SLinus Torvalds 39719a95f3cfSPaul Cassella /* 39729a95f3cfSPaul Cassella * By the time we get here, we already hold the mm semaphore 39739a95f3cfSPaul Cassella * 39749a95f3cfSPaul Cassella * The mmap_sem may have been released depending on flags and our 39759a95f3cfSPaul Cassella * return value. See filemap_fault() and __lock_page_or_retry(). 39769a95f3cfSPaul Cassella */ 39772b740303SSouptick Joarder vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address, 3978dcddffd4SKirill A. Shutemov unsigned int flags) 3979519e5247SJohannes Weiner { 39802b740303SSouptick Joarder vm_fault_t ret; 3981519e5247SJohannes Weiner 3982519e5247SJohannes Weiner __set_current_state(TASK_RUNNING); 3983519e5247SJohannes Weiner 3984519e5247SJohannes Weiner count_vm_event(PGFAULT); 39852262185cSRoman Gushchin count_memcg_event_mm(vma->vm_mm, PGFAULT); 3986519e5247SJohannes Weiner 3987519e5247SJohannes Weiner /* do counter updates before entering really critical section. */ 3988519e5247SJohannes Weiner check_sync_rss_stat(current); 3989519e5247SJohannes Weiner 3990de0c799bSLaurent Dufour if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE, 3991de0c799bSLaurent Dufour flags & FAULT_FLAG_INSTRUCTION, 3992de0c799bSLaurent Dufour flags & FAULT_FLAG_REMOTE)) 3993de0c799bSLaurent Dufour return VM_FAULT_SIGSEGV; 3994de0c799bSLaurent Dufour 3995519e5247SJohannes Weiner /* 3996519e5247SJohannes Weiner * Enable the memcg OOM handling for faults triggered in user 3997519e5247SJohannes Weiner * space. Kernel faults are handled more gracefully. 3998519e5247SJohannes Weiner */ 3999519e5247SJohannes Weiner if (flags & FAULT_FLAG_USER) 400029ef680aSMichal Hocko mem_cgroup_enter_user_fault(); 4001519e5247SJohannes Weiner 4002bae473a4SKirill A. Shutemov if (unlikely(is_vm_hugetlb_page(vma))) 4003bae473a4SKirill A. Shutemov ret = hugetlb_fault(vma->vm_mm, vma, address, flags); 4004bae473a4SKirill A. Shutemov else 4005dcddffd4SKirill A. Shutemov ret = __handle_mm_fault(vma, address, flags); 4006519e5247SJohannes Weiner 400749426420SJohannes Weiner if (flags & FAULT_FLAG_USER) { 400829ef680aSMichal Hocko mem_cgroup_exit_user_fault(); 400949426420SJohannes Weiner /* 401049426420SJohannes Weiner * The task may have entered a memcg OOM situation but 401149426420SJohannes Weiner * if the allocation error was handled gracefully (no 401249426420SJohannes Weiner * VM_FAULT_OOM), there is no need to kill anything. 401349426420SJohannes Weiner * Just clean up the OOM state peacefully. 401449426420SJohannes Weiner */ 401549426420SJohannes Weiner if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM)) 401649426420SJohannes Weiner mem_cgroup_oom_synchronize(false); 401749426420SJohannes Weiner } 40183812c8c8SJohannes Weiner 4019519e5247SJohannes Weiner return ret; 4020519e5247SJohannes Weiner } 4021e1d6d01aSJesse Barnes EXPORT_SYMBOL_GPL(handle_mm_fault); 4022519e5247SJohannes Weiner 402390eceff1SKirill A. Shutemov #ifndef __PAGETABLE_P4D_FOLDED 402490eceff1SKirill A. Shutemov /* 402590eceff1SKirill A. Shutemov * Allocate p4d page table. 402690eceff1SKirill A. Shutemov * We've already handled the fast-path in-line. 402790eceff1SKirill A. Shutemov */ 402890eceff1SKirill A. Shutemov int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address) 402990eceff1SKirill A. Shutemov { 403090eceff1SKirill A. Shutemov p4d_t *new = p4d_alloc_one(mm, address); 403190eceff1SKirill A. Shutemov if (!new) 403290eceff1SKirill A. Shutemov return -ENOMEM; 403390eceff1SKirill A. Shutemov 403490eceff1SKirill A. Shutemov smp_wmb(); /* See comment in __pte_alloc */ 403590eceff1SKirill A. Shutemov 403690eceff1SKirill A. Shutemov spin_lock(&mm->page_table_lock); 403790eceff1SKirill A. Shutemov if (pgd_present(*pgd)) /* Another has populated it */ 403890eceff1SKirill A. Shutemov p4d_free(mm, new); 403990eceff1SKirill A. Shutemov else 404090eceff1SKirill A. Shutemov pgd_populate(mm, pgd, new); 404190eceff1SKirill A. Shutemov spin_unlock(&mm->page_table_lock); 404290eceff1SKirill A. Shutemov return 0; 404390eceff1SKirill A. Shutemov } 404490eceff1SKirill A. Shutemov #endif /* __PAGETABLE_P4D_FOLDED */ 404590eceff1SKirill A. Shutemov 40461da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED 40471da177e4SLinus Torvalds /* 40481da177e4SLinus Torvalds * Allocate page upper directory. 4049872fec16SHugh Dickins * We've already handled the fast-path in-line. 40501da177e4SLinus Torvalds */ 4051c2febafcSKirill A. Shutemov int __pud_alloc(struct mm_struct *mm, p4d_t *p4d, unsigned long address) 40521da177e4SLinus Torvalds { 4053c74df32cSHugh Dickins pud_t *new = pud_alloc_one(mm, address); 4054c74df32cSHugh Dickins if (!new) 40551bb3630eSHugh Dickins return -ENOMEM; 40561da177e4SLinus Torvalds 4057362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 4058362a61adSNick Piggin 4059872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 4060c2febafcSKirill A. Shutemov #ifndef __ARCH_HAS_5LEVEL_HACK 4061b4e98d9aSKirill A. Shutemov if (!p4d_present(*p4d)) { 4062b4e98d9aSKirill A. Shutemov mm_inc_nr_puds(mm); 4063c2febafcSKirill A. Shutemov p4d_populate(mm, p4d, new); 4064b4e98d9aSKirill A. Shutemov } else /* Another has populated it */ 4065c2febafcSKirill A. Shutemov pud_free(mm, new); 4066b4e98d9aSKirill A. Shutemov #else 4067b4e98d9aSKirill A. Shutemov if (!pgd_present(*p4d)) { 4068b4e98d9aSKirill A. Shutemov mm_inc_nr_puds(mm); 4069c2febafcSKirill A. Shutemov pgd_populate(mm, p4d, new); 4070b4e98d9aSKirill A. Shutemov } else /* Another has populated it */ 4071b4e98d9aSKirill A. Shutemov pud_free(mm, new); 4072c2febafcSKirill A. Shutemov #endif /* __ARCH_HAS_5LEVEL_HACK */ 4073872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 40741bb3630eSHugh Dickins return 0; 40751da177e4SLinus Torvalds } 40761da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */ 40771da177e4SLinus Torvalds 40781da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED 40791da177e4SLinus Torvalds /* 40801da177e4SLinus Torvalds * Allocate page middle directory. 4081872fec16SHugh Dickins * We've already handled the fast-path in-line. 40821da177e4SLinus Torvalds */ 40831bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) 40841da177e4SLinus Torvalds { 4085a00cc7d9SMatthew Wilcox spinlock_t *ptl; 4086c74df32cSHugh Dickins pmd_t *new = pmd_alloc_one(mm, address); 4087c74df32cSHugh Dickins if (!new) 40881bb3630eSHugh Dickins return -ENOMEM; 40891da177e4SLinus Torvalds 4090362a61adSNick Piggin smp_wmb(); /* See comment in __pte_alloc */ 4091362a61adSNick Piggin 4092a00cc7d9SMatthew Wilcox ptl = pud_lock(mm, pud); 40931da177e4SLinus Torvalds #ifndef __ARCH_HAS_4LEVEL_HACK 4094dc6c9a35SKirill A. Shutemov if (!pud_present(*pud)) { 4095dc6c9a35SKirill A. Shutemov mm_inc_nr_pmds(mm); 40961da177e4SLinus Torvalds pud_populate(mm, pud, new); 4097dc6c9a35SKirill A. Shutemov } else /* Another has populated it */ 40985e541973SBenjamin Herrenschmidt pmd_free(mm, new); 4099dc6c9a35SKirill A. Shutemov #else 4100dc6c9a35SKirill A. Shutemov if (!pgd_present(*pud)) { 4101dc6c9a35SKirill A. Shutemov mm_inc_nr_pmds(mm); 41021da177e4SLinus Torvalds pgd_populate(mm, pud, new); 4103dc6c9a35SKirill A. Shutemov } else /* Another has populated it */ 4104dc6c9a35SKirill A. Shutemov pmd_free(mm, new); 41051da177e4SLinus Torvalds #endif /* __ARCH_HAS_4LEVEL_HACK */ 4106a00cc7d9SMatthew Wilcox spin_unlock(ptl); 41071bb3630eSHugh Dickins return 0; 41081da177e4SLinus Torvalds } 41091da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */ 41101da177e4SLinus Torvalds 411109796395SRoss Zwisler static int __follow_pte_pmd(struct mm_struct *mm, unsigned long address, 4112ac46d4f3SJérôme Glisse struct mmu_notifier_range *range, 411309796395SRoss Zwisler pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp) 4114f8ad0f49SJohannes Weiner { 4115f8ad0f49SJohannes Weiner pgd_t *pgd; 4116c2febafcSKirill A. Shutemov p4d_t *p4d; 4117f8ad0f49SJohannes Weiner pud_t *pud; 4118f8ad0f49SJohannes Weiner pmd_t *pmd; 4119f8ad0f49SJohannes Weiner pte_t *ptep; 4120f8ad0f49SJohannes Weiner 4121f8ad0f49SJohannes Weiner pgd = pgd_offset(mm, address); 4122f8ad0f49SJohannes Weiner if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) 4123f8ad0f49SJohannes Weiner goto out; 4124f8ad0f49SJohannes Weiner 4125c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, address); 4126c2febafcSKirill A. Shutemov if (p4d_none(*p4d) || unlikely(p4d_bad(*p4d))) 4127c2febafcSKirill A. Shutemov goto out; 4128c2febafcSKirill A. Shutemov 4129c2febafcSKirill A. Shutemov pud = pud_offset(p4d, address); 4130f8ad0f49SJohannes Weiner if (pud_none(*pud) || unlikely(pud_bad(*pud))) 4131f8ad0f49SJohannes Weiner goto out; 4132f8ad0f49SJohannes Weiner 4133f8ad0f49SJohannes Weiner pmd = pmd_offset(pud, address); 4134f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 413509796395SRoss Zwisler 413609796395SRoss Zwisler if (pmd_huge(*pmd)) { 413709796395SRoss Zwisler if (!pmdpp) 4138f8ad0f49SJohannes Weiner goto out; 4139f8ad0f49SJohannes Weiner 4140ac46d4f3SJérôme Glisse if (range) { 41417269f999SJérôme Glisse mmu_notifier_range_init(range, MMU_NOTIFY_CLEAR, 0, 41426f4f13e8SJérôme Glisse NULL, mm, address & PMD_MASK, 4143ac46d4f3SJérôme Glisse (address & PMD_MASK) + PMD_SIZE); 4144ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(range); 4145a4d1a885SJérôme Glisse } 414609796395SRoss Zwisler *ptlp = pmd_lock(mm, pmd); 414709796395SRoss Zwisler if (pmd_huge(*pmd)) { 414809796395SRoss Zwisler *pmdpp = pmd; 414909796395SRoss Zwisler return 0; 415009796395SRoss Zwisler } 415109796395SRoss Zwisler spin_unlock(*ptlp); 4152ac46d4f3SJérôme Glisse if (range) 4153ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(range); 415409796395SRoss Zwisler } 415509796395SRoss Zwisler 415609796395SRoss Zwisler if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd))) 4157f8ad0f49SJohannes Weiner goto out; 4158f8ad0f49SJohannes Weiner 4159ac46d4f3SJérôme Glisse if (range) { 41607269f999SJérôme Glisse mmu_notifier_range_init(range, MMU_NOTIFY_CLEAR, 0, NULL, mm, 41616f4f13e8SJérôme Glisse address & PAGE_MASK, 41621ed7293aSMatthew Wilcox (address & PAGE_MASK) + PAGE_SIZE); 4163ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(range); 4164a4d1a885SJérôme Glisse } 4165f8ad0f49SJohannes Weiner ptep = pte_offset_map_lock(mm, pmd, address, ptlp); 4166f8ad0f49SJohannes Weiner if (!pte_present(*ptep)) 4167f8ad0f49SJohannes Weiner goto unlock; 4168f8ad0f49SJohannes Weiner *ptepp = ptep; 4169f8ad0f49SJohannes Weiner return 0; 4170f8ad0f49SJohannes Weiner unlock: 4171f8ad0f49SJohannes Weiner pte_unmap_unlock(ptep, *ptlp); 4172ac46d4f3SJérôme Glisse if (range) 4173ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(range); 4174f8ad0f49SJohannes Weiner out: 4175f8ad0f49SJohannes Weiner return -EINVAL; 4176f8ad0f49SJohannes Weiner } 4177f8ad0f49SJohannes Weiner 4178f729c8c9SRoss Zwisler static inline int follow_pte(struct mm_struct *mm, unsigned long address, 4179f729c8c9SRoss Zwisler pte_t **ptepp, spinlock_t **ptlp) 41801b36ba81SNamhyung Kim { 41811b36ba81SNamhyung Kim int res; 41821b36ba81SNamhyung Kim 41831b36ba81SNamhyung Kim /* (void) is needed to make gcc happy */ 41841b36ba81SNamhyung Kim (void) __cond_lock(*ptlp, 4185ac46d4f3SJérôme Glisse !(res = __follow_pte_pmd(mm, address, NULL, 4186a4d1a885SJérôme Glisse ptepp, NULL, ptlp))); 41871b36ba81SNamhyung Kim return res; 41881b36ba81SNamhyung Kim } 41891b36ba81SNamhyung Kim 419009796395SRoss Zwisler int follow_pte_pmd(struct mm_struct *mm, unsigned long address, 4191ac46d4f3SJérôme Glisse struct mmu_notifier_range *range, 419209796395SRoss Zwisler pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp) 419309796395SRoss Zwisler { 419409796395SRoss Zwisler int res; 419509796395SRoss Zwisler 419609796395SRoss Zwisler /* (void) is needed to make gcc happy */ 419709796395SRoss Zwisler (void) __cond_lock(*ptlp, 4198ac46d4f3SJérôme Glisse !(res = __follow_pte_pmd(mm, address, range, 4199a4d1a885SJérôme Glisse ptepp, pmdpp, ptlp))); 420009796395SRoss Zwisler return res; 420109796395SRoss Zwisler } 420209796395SRoss Zwisler EXPORT_SYMBOL(follow_pte_pmd); 420309796395SRoss Zwisler 42043b6748e2SJohannes Weiner /** 42053b6748e2SJohannes Weiner * follow_pfn - look up PFN at a user virtual address 42063b6748e2SJohannes Weiner * @vma: memory mapping 42073b6748e2SJohannes Weiner * @address: user virtual address 42083b6748e2SJohannes Weiner * @pfn: location to store found PFN 42093b6748e2SJohannes Weiner * 42103b6748e2SJohannes Weiner * Only IO mappings and raw PFN mappings are allowed. 42113b6748e2SJohannes Weiner * 4212a862f68aSMike Rapoport * Return: zero and the pfn at @pfn on success, -ve otherwise. 42133b6748e2SJohannes Weiner */ 42143b6748e2SJohannes Weiner int follow_pfn(struct vm_area_struct *vma, unsigned long address, 42153b6748e2SJohannes Weiner unsigned long *pfn) 42163b6748e2SJohannes Weiner { 42173b6748e2SJohannes Weiner int ret = -EINVAL; 42183b6748e2SJohannes Weiner spinlock_t *ptl; 42193b6748e2SJohannes Weiner pte_t *ptep; 42203b6748e2SJohannes Weiner 42213b6748e2SJohannes Weiner if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 42223b6748e2SJohannes Weiner return ret; 42233b6748e2SJohannes Weiner 42243b6748e2SJohannes Weiner ret = follow_pte(vma->vm_mm, address, &ptep, &ptl); 42253b6748e2SJohannes Weiner if (ret) 42263b6748e2SJohannes Weiner return ret; 42273b6748e2SJohannes Weiner *pfn = pte_pfn(*ptep); 42283b6748e2SJohannes Weiner pte_unmap_unlock(ptep, ptl); 42293b6748e2SJohannes Weiner return 0; 42303b6748e2SJohannes Weiner } 42313b6748e2SJohannes Weiner EXPORT_SYMBOL(follow_pfn); 42323b6748e2SJohannes Weiner 423328b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT 4234d87fe660Svenkatesh.pallipadi@intel.com int follow_phys(struct vm_area_struct *vma, 423528b2ee20SRik van Riel unsigned long address, unsigned int flags, 4236d87fe660Svenkatesh.pallipadi@intel.com unsigned long *prot, resource_size_t *phys) 423728b2ee20SRik van Riel { 423803668a4dSJohannes Weiner int ret = -EINVAL; 423928b2ee20SRik van Riel pte_t *ptep, pte; 424028b2ee20SRik van Riel spinlock_t *ptl; 424128b2ee20SRik van Riel 4242d87fe660Svenkatesh.pallipadi@intel.com if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 4243d87fe660Svenkatesh.pallipadi@intel.com goto out; 424428b2ee20SRik van Riel 424503668a4dSJohannes Weiner if (follow_pte(vma->vm_mm, address, &ptep, &ptl)) 4246d87fe660Svenkatesh.pallipadi@intel.com goto out; 424728b2ee20SRik van Riel pte = *ptep; 424803668a4dSJohannes Weiner 4249f6f37321SLinus Torvalds if ((flags & FOLL_WRITE) && !pte_write(pte)) 425028b2ee20SRik van Riel goto unlock; 425128b2ee20SRik van Riel 425228b2ee20SRik van Riel *prot = pgprot_val(pte_pgprot(pte)); 425303668a4dSJohannes Weiner *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT; 425428b2ee20SRik van Riel 425503668a4dSJohannes Weiner ret = 0; 425628b2ee20SRik van Riel unlock: 425728b2ee20SRik van Riel pte_unmap_unlock(ptep, ptl); 425828b2ee20SRik van Riel out: 4259d87fe660Svenkatesh.pallipadi@intel.com return ret; 426028b2ee20SRik van Riel } 426128b2ee20SRik van Riel 426228b2ee20SRik van Riel int generic_access_phys(struct vm_area_struct *vma, unsigned long addr, 426328b2ee20SRik van Riel void *buf, int len, int write) 426428b2ee20SRik van Riel { 426528b2ee20SRik van Riel resource_size_t phys_addr; 426628b2ee20SRik van Riel unsigned long prot = 0; 42672bc7273bSKOSAKI Motohiro void __iomem *maddr; 426828b2ee20SRik van Riel int offset = addr & (PAGE_SIZE-1); 426928b2ee20SRik van Riel 4270d87fe660Svenkatesh.pallipadi@intel.com if (follow_phys(vma, addr, write, &prot, &phys_addr)) 427128b2ee20SRik van Riel return -EINVAL; 427228b2ee20SRik van Riel 42739cb12d7bSGrazvydas Ignotas maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot); 427424eee1e4Sjie@chenjie6@huwei.com if (!maddr) 427524eee1e4Sjie@chenjie6@huwei.com return -ENOMEM; 427624eee1e4Sjie@chenjie6@huwei.com 427728b2ee20SRik van Riel if (write) 427828b2ee20SRik van Riel memcpy_toio(maddr + offset, buf, len); 427928b2ee20SRik van Riel else 428028b2ee20SRik van Riel memcpy_fromio(buf, maddr + offset, len); 428128b2ee20SRik van Riel iounmap(maddr); 428228b2ee20SRik van Riel 428328b2ee20SRik van Riel return len; 428428b2ee20SRik van Riel } 42855a73633eSUwe Kleine-König EXPORT_SYMBOL_GPL(generic_access_phys); 428628b2ee20SRik van Riel #endif 428728b2ee20SRik van Riel 42880ec76a11SDavid Howells /* 4289206cb636SStephen Wilson * Access another process' address space as given in mm. If non-NULL, use the 4290206cb636SStephen Wilson * given task for page fault accounting. 42910ec76a11SDavid Howells */ 429284d77d3fSEric W. Biederman int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm, 4293442486ecSLorenzo Stoakes unsigned long addr, void *buf, int len, unsigned int gup_flags) 42940ec76a11SDavid Howells { 42950ec76a11SDavid Howells struct vm_area_struct *vma; 42960ec76a11SDavid Howells void *old_buf = buf; 4297442486ecSLorenzo Stoakes int write = gup_flags & FOLL_WRITE; 42980ec76a11SDavid Howells 42991e426fe2SKonstantin Khlebnikov if (down_read_killable(&mm->mmap_sem)) 43001e426fe2SKonstantin Khlebnikov return 0; 43011e426fe2SKonstantin Khlebnikov 4302183ff22bSSimon Arlott /* ignore errors, just check how much was successfully transferred */ 43030ec76a11SDavid Howells while (len) { 43040ec76a11SDavid Howells int bytes, ret, offset; 43050ec76a11SDavid Howells void *maddr; 430628b2ee20SRik van Riel struct page *page = NULL; 43070ec76a11SDavid Howells 43081e987790SDave Hansen ret = get_user_pages_remote(tsk, mm, addr, 1, 43095b56d49fSLorenzo Stoakes gup_flags, &page, &vma, NULL); 431028b2ee20SRik van Riel if (ret <= 0) { 4311dbffcd03SRik van Riel #ifndef CONFIG_HAVE_IOREMAP_PROT 4312dbffcd03SRik van Riel break; 4313dbffcd03SRik van Riel #else 431428b2ee20SRik van Riel /* 431528b2ee20SRik van Riel * Check if this is a VM_IO | VM_PFNMAP VMA, which 431628b2ee20SRik van Riel * we can access using slightly different code. 431728b2ee20SRik van Riel */ 431828b2ee20SRik van Riel vma = find_vma(mm, addr); 4319fe936dfcSMichael Ellerman if (!vma || vma->vm_start > addr) 43200ec76a11SDavid Howells break; 432128b2ee20SRik van Riel if (vma->vm_ops && vma->vm_ops->access) 432228b2ee20SRik van Riel ret = vma->vm_ops->access(vma, addr, buf, 432328b2ee20SRik van Riel len, write); 432428b2ee20SRik van Riel if (ret <= 0) 432528b2ee20SRik van Riel break; 432628b2ee20SRik van Riel bytes = ret; 4327dbffcd03SRik van Riel #endif 432828b2ee20SRik van Riel } else { 43290ec76a11SDavid Howells bytes = len; 43300ec76a11SDavid Howells offset = addr & (PAGE_SIZE-1); 43310ec76a11SDavid Howells if (bytes > PAGE_SIZE-offset) 43320ec76a11SDavid Howells bytes = PAGE_SIZE-offset; 43330ec76a11SDavid Howells 43340ec76a11SDavid Howells maddr = kmap(page); 43350ec76a11SDavid Howells if (write) { 43360ec76a11SDavid Howells copy_to_user_page(vma, page, addr, 43370ec76a11SDavid Howells maddr + offset, buf, bytes); 43380ec76a11SDavid Howells set_page_dirty_lock(page); 43390ec76a11SDavid Howells } else { 43400ec76a11SDavid Howells copy_from_user_page(vma, page, addr, 43410ec76a11SDavid Howells buf, maddr + offset, bytes); 43420ec76a11SDavid Howells } 43430ec76a11SDavid Howells kunmap(page); 434409cbfeafSKirill A. Shutemov put_page(page); 434528b2ee20SRik van Riel } 43460ec76a11SDavid Howells len -= bytes; 43470ec76a11SDavid Howells buf += bytes; 43480ec76a11SDavid Howells addr += bytes; 43490ec76a11SDavid Howells } 43500ec76a11SDavid Howells up_read(&mm->mmap_sem); 43510ec76a11SDavid Howells 43520ec76a11SDavid Howells return buf - old_buf; 43530ec76a11SDavid Howells } 435403252919SAndi Kleen 43555ddd36b9SStephen Wilson /** 4356ae91dbfcSRandy Dunlap * access_remote_vm - access another process' address space 43575ddd36b9SStephen Wilson * @mm: the mm_struct of the target address space 43585ddd36b9SStephen Wilson * @addr: start address to access 43595ddd36b9SStephen Wilson * @buf: source or destination buffer 43605ddd36b9SStephen Wilson * @len: number of bytes to transfer 43616347e8d5SLorenzo Stoakes * @gup_flags: flags modifying lookup behaviour 43625ddd36b9SStephen Wilson * 43635ddd36b9SStephen Wilson * The caller must hold a reference on @mm. 4364a862f68aSMike Rapoport * 4365a862f68aSMike Rapoport * Return: number of bytes copied from source to destination. 43665ddd36b9SStephen Wilson */ 43675ddd36b9SStephen Wilson int access_remote_vm(struct mm_struct *mm, unsigned long addr, 43686347e8d5SLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 43695ddd36b9SStephen Wilson { 43706347e8d5SLorenzo Stoakes return __access_remote_vm(NULL, mm, addr, buf, len, gup_flags); 43715ddd36b9SStephen Wilson } 43725ddd36b9SStephen Wilson 437303252919SAndi Kleen /* 4374206cb636SStephen Wilson * Access another process' address space. 4375206cb636SStephen Wilson * Source/target buffer must be kernel space, 4376206cb636SStephen Wilson * Do not walk the page table directly, use get_user_pages 4377206cb636SStephen Wilson */ 4378206cb636SStephen Wilson int access_process_vm(struct task_struct *tsk, unsigned long addr, 4379f307ab6dSLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 4380206cb636SStephen Wilson { 4381206cb636SStephen Wilson struct mm_struct *mm; 4382206cb636SStephen Wilson int ret; 4383206cb636SStephen Wilson 4384206cb636SStephen Wilson mm = get_task_mm(tsk); 4385206cb636SStephen Wilson if (!mm) 4386206cb636SStephen Wilson return 0; 4387206cb636SStephen Wilson 4388f307ab6dSLorenzo Stoakes ret = __access_remote_vm(tsk, mm, addr, buf, len, gup_flags); 4389442486ecSLorenzo Stoakes 4390206cb636SStephen Wilson mmput(mm); 4391206cb636SStephen Wilson 4392206cb636SStephen Wilson return ret; 4393206cb636SStephen Wilson } 4394fcd35857SCatalin Marinas EXPORT_SYMBOL_GPL(access_process_vm); 4395206cb636SStephen Wilson 439603252919SAndi Kleen /* 439703252919SAndi Kleen * Print the name of a VMA. 439803252919SAndi Kleen */ 439903252919SAndi Kleen void print_vma_addr(char *prefix, unsigned long ip) 440003252919SAndi Kleen { 440103252919SAndi Kleen struct mm_struct *mm = current->mm; 440203252919SAndi Kleen struct vm_area_struct *vma; 440303252919SAndi Kleen 4404e8bff74aSIngo Molnar /* 44050a7f682dSMichal Hocko * we might be running from an atomic context so we cannot sleep 4406e8bff74aSIngo Molnar */ 44070a7f682dSMichal Hocko if (!down_read_trylock(&mm->mmap_sem)) 4408e8bff74aSIngo Molnar return; 4409e8bff74aSIngo Molnar 441003252919SAndi Kleen vma = find_vma(mm, ip); 441103252919SAndi Kleen if (vma && vma->vm_file) { 441203252919SAndi Kleen struct file *f = vma->vm_file; 44130a7f682dSMichal Hocko char *buf = (char *)__get_free_page(GFP_NOWAIT); 441403252919SAndi Kleen if (buf) { 44152fbc57c5SAndy Shevchenko char *p; 441603252919SAndi Kleen 44179bf39ab2SMiklos Szeredi p = file_path(f, buf, PAGE_SIZE); 441803252919SAndi Kleen if (IS_ERR(p)) 441903252919SAndi Kleen p = "?"; 44202fbc57c5SAndy Shevchenko printk("%s%s[%lx+%lx]", prefix, kbasename(p), 442103252919SAndi Kleen vma->vm_start, 442203252919SAndi Kleen vma->vm_end - vma->vm_start); 442303252919SAndi Kleen free_page((unsigned long)buf); 442403252919SAndi Kleen } 442503252919SAndi Kleen } 442651a07e50SJeff Liu up_read(&mm->mmap_sem); 442703252919SAndi Kleen } 44283ee1afa3SNick Piggin 4429662bbcb2SMichael S. Tsirkin #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP) 44309ec23531SDavid Hildenbrand void __might_fault(const char *file, int line) 44313ee1afa3SNick Piggin { 443295156f00SPeter Zijlstra /* 443395156f00SPeter Zijlstra * Some code (nfs/sunrpc) uses socket ops on kernel memory while 443495156f00SPeter Zijlstra * holding the mmap_sem, this is safe because kernel memory doesn't 443595156f00SPeter Zijlstra * get paged out, therefore we'll never actually fault, and the 443695156f00SPeter Zijlstra * below annotations will generate false positives. 443795156f00SPeter Zijlstra */ 4438db68ce10SAl Viro if (uaccess_kernel()) 443995156f00SPeter Zijlstra return; 44409ec23531SDavid Hildenbrand if (pagefault_disabled()) 4441662bbcb2SMichael S. Tsirkin return; 44429ec23531SDavid Hildenbrand __might_sleep(file, line, 0); 44439ec23531SDavid Hildenbrand #if defined(CONFIG_DEBUG_ATOMIC_SLEEP) 4444662bbcb2SMichael S. Tsirkin if (current->mm) 44453ee1afa3SNick Piggin might_lock_read(¤t->mm->mmap_sem); 44469ec23531SDavid Hildenbrand #endif 44473ee1afa3SNick Piggin } 44489ec23531SDavid Hildenbrand EXPORT_SYMBOL(__might_fault); 44493ee1afa3SNick Piggin #endif 445047ad8475SAndrea Arcangeli 445147ad8475SAndrea Arcangeli #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS) 4452c6ddfb6cSHuang Ying /* 4453c6ddfb6cSHuang Ying * Process all subpages of the specified huge page with the specified 4454c6ddfb6cSHuang Ying * operation. The target subpage will be processed last to keep its 4455c6ddfb6cSHuang Ying * cache lines hot. 4456c6ddfb6cSHuang Ying */ 4457c6ddfb6cSHuang Ying static inline void process_huge_page( 4458c6ddfb6cSHuang Ying unsigned long addr_hint, unsigned int pages_per_huge_page, 4459c6ddfb6cSHuang Ying void (*process_subpage)(unsigned long addr, int idx, void *arg), 4460c6ddfb6cSHuang Ying void *arg) 4461c6ddfb6cSHuang Ying { 4462c6ddfb6cSHuang Ying int i, n, base, l; 4463c6ddfb6cSHuang Ying unsigned long addr = addr_hint & 4464c6ddfb6cSHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 4465c6ddfb6cSHuang Ying 4466c6ddfb6cSHuang Ying /* Process target subpage last to keep its cache lines hot */ 4467c6ddfb6cSHuang Ying might_sleep(); 4468c6ddfb6cSHuang Ying n = (addr_hint - addr) / PAGE_SIZE; 4469c6ddfb6cSHuang Ying if (2 * n <= pages_per_huge_page) { 4470c6ddfb6cSHuang Ying /* If target subpage in first half of huge page */ 4471c6ddfb6cSHuang Ying base = 0; 4472c6ddfb6cSHuang Ying l = n; 4473c6ddfb6cSHuang Ying /* Process subpages at the end of huge page */ 4474c6ddfb6cSHuang Ying for (i = pages_per_huge_page - 1; i >= 2 * n; i--) { 4475c6ddfb6cSHuang Ying cond_resched(); 4476c6ddfb6cSHuang Ying process_subpage(addr + i * PAGE_SIZE, i, arg); 4477c6ddfb6cSHuang Ying } 4478c6ddfb6cSHuang Ying } else { 4479c6ddfb6cSHuang Ying /* If target subpage in second half of huge page */ 4480c6ddfb6cSHuang Ying base = pages_per_huge_page - 2 * (pages_per_huge_page - n); 4481c6ddfb6cSHuang Ying l = pages_per_huge_page - n; 4482c6ddfb6cSHuang Ying /* Process subpages at the begin of huge page */ 4483c6ddfb6cSHuang Ying for (i = 0; i < base; i++) { 4484c6ddfb6cSHuang Ying cond_resched(); 4485c6ddfb6cSHuang Ying process_subpage(addr + i * PAGE_SIZE, i, arg); 4486c6ddfb6cSHuang Ying } 4487c6ddfb6cSHuang Ying } 4488c6ddfb6cSHuang Ying /* 4489c6ddfb6cSHuang Ying * Process remaining subpages in left-right-left-right pattern 4490c6ddfb6cSHuang Ying * towards the target subpage 4491c6ddfb6cSHuang Ying */ 4492c6ddfb6cSHuang Ying for (i = 0; i < l; i++) { 4493c6ddfb6cSHuang Ying int left_idx = base + i; 4494c6ddfb6cSHuang Ying int right_idx = base + 2 * l - 1 - i; 4495c6ddfb6cSHuang Ying 4496c6ddfb6cSHuang Ying cond_resched(); 4497c6ddfb6cSHuang Ying process_subpage(addr + left_idx * PAGE_SIZE, left_idx, arg); 4498c6ddfb6cSHuang Ying cond_resched(); 4499c6ddfb6cSHuang Ying process_subpage(addr + right_idx * PAGE_SIZE, right_idx, arg); 4500c6ddfb6cSHuang Ying } 4501c6ddfb6cSHuang Ying } 4502c6ddfb6cSHuang Ying 450347ad8475SAndrea Arcangeli static void clear_gigantic_page(struct page *page, 450447ad8475SAndrea Arcangeli unsigned long addr, 450547ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 450647ad8475SAndrea Arcangeli { 450747ad8475SAndrea Arcangeli int i; 450847ad8475SAndrea Arcangeli struct page *p = page; 450947ad8475SAndrea Arcangeli 451047ad8475SAndrea Arcangeli might_sleep(); 451147ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; 451247ad8475SAndrea Arcangeli i++, p = mem_map_next(p, page, i)) { 451347ad8475SAndrea Arcangeli cond_resched(); 451447ad8475SAndrea Arcangeli clear_user_highpage(p, addr + i * PAGE_SIZE); 451547ad8475SAndrea Arcangeli } 451647ad8475SAndrea Arcangeli } 4517c6ddfb6cSHuang Ying 4518c6ddfb6cSHuang Ying static void clear_subpage(unsigned long addr, int idx, void *arg) 4519c6ddfb6cSHuang Ying { 4520c6ddfb6cSHuang Ying struct page *page = arg; 4521c6ddfb6cSHuang Ying 4522c6ddfb6cSHuang Ying clear_user_highpage(page + idx, addr); 4523c6ddfb6cSHuang Ying } 4524c6ddfb6cSHuang Ying 452547ad8475SAndrea Arcangeli void clear_huge_page(struct page *page, 4526c79b57e4SHuang Ying unsigned long addr_hint, unsigned int pages_per_huge_page) 452747ad8475SAndrea Arcangeli { 4528c79b57e4SHuang Ying unsigned long addr = addr_hint & 4529c79b57e4SHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 453047ad8475SAndrea Arcangeli 453147ad8475SAndrea Arcangeli if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { 453247ad8475SAndrea Arcangeli clear_gigantic_page(page, addr, pages_per_huge_page); 453347ad8475SAndrea Arcangeli return; 453447ad8475SAndrea Arcangeli } 453547ad8475SAndrea Arcangeli 4536c6ddfb6cSHuang Ying process_huge_page(addr_hint, pages_per_huge_page, clear_subpage, page); 453747ad8475SAndrea Arcangeli } 453847ad8475SAndrea Arcangeli 453947ad8475SAndrea Arcangeli static void copy_user_gigantic_page(struct page *dst, struct page *src, 454047ad8475SAndrea Arcangeli unsigned long addr, 454147ad8475SAndrea Arcangeli struct vm_area_struct *vma, 454247ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 454347ad8475SAndrea Arcangeli { 454447ad8475SAndrea Arcangeli int i; 454547ad8475SAndrea Arcangeli struct page *dst_base = dst; 454647ad8475SAndrea Arcangeli struct page *src_base = src; 454747ad8475SAndrea Arcangeli 454847ad8475SAndrea Arcangeli for (i = 0; i < pages_per_huge_page; ) { 454947ad8475SAndrea Arcangeli cond_resched(); 455047ad8475SAndrea Arcangeli copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma); 455147ad8475SAndrea Arcangeli 455247ad8475SAndrea Arcangeli i++; 455347ad8475SAndrea Arcangeli dst = mem_map_next(dst, dst_base, i); 455447ad8475SAndrea Arcangeli src = mem_map_next(src, src_base, i); 455547ad8475SAndrea Arcangeli } 455647ad8475SAndrea Arcangeli } 455747ad8475SAndrea Arcangeli 4558c9f4cd71SHuang Ying struct copy_subpage_arg { 4559c9f4cd71SHuang Ying struct page *dst; 4560c9f4cd71SHuang Ying struct page *src; 4561c9f4cd71SHuang Ying struct vm_area_struct *vma; 4562c9f4cd71SHuang Ying }; 4563c9f4cd71SHuang Ying 4564c9f4cd71SHuang Ying static void copy_subpage(unsigned long addr, int idx, void *arg) 4565c9f4cd71SHuang Ying { 4566c9f4cd71SHuang Ying struct copy_subpage_arg *copy_arg = arg; 4567c9f4cd71SHuang Ying 4568c9f4cd71SHuang Ying copy_user_highpage(copy_arg->dst + idx, copy_arg->src + idx, 4569c9f4cd71SHuang Ying addr, copy_arg->vma); 4570c9f4cd71SHuang Ying } 4571c9f4cd71SHuang Ying 457247ad8475SAndrea Arcangeli void copy_user_huge_page(struct page *dst, struct page *src, 4573c9f4cd71SHuang Ying unsigned long addr_hint, struct vm_area_struct *vma, 457447ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 457547ad8475SAndrea Arcangeli { 4576c9f4cd71SHuang Ying unsigned long addr = addr_hint & 4577c9f4cd71SHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 4578c9f4cd71SHuang Ying struct copy_subpage_arg arg = { 4579c9f4cd71SHuang Ying .dst = dst, 4580c9f4cd71SHuang Ying .src = src, 4581c9f4cd71SHuang Ying .vma = vma, 4582c9f4cd71SHuang Ying }; 458347ad8475SAndrea Arcangeli 458447ad8475SAndrea Arcangeli if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { 458547ad8475SAndrea Arcangeli copy_user_gigantic_page(dst, src, addr, vma, 458647ad8475SAndrea Arcangeli pages_per_huge_page); 458747ad8475SAndrea Arcangeli return; 458847ad8475SAndrea Arcangeli } 458947ad8475SAndrea Arcangeli 4590c9f4cd71SHuang Ying process_huge_page(addr_hint, pages_per_huge_page, copy_subpage, &arg); 459147ad8475SAndrea Arcangeli } 4592fa4d75c1SMike Kravetz 4593fa4d75c1SMike Kravetz long copy_huge_page_from_user(struct page *dst_page, 4594fa4d75c1SMike Kravetz const void __user *usr_src, 4595810a56b9SMike Kravetz unsigned int pages_per_huge_page, 4596810a56b9SMike Kravetz bool allow_pagefault) 4597fa4d75c1SMike Kravetz { 4598fa4d75c1SMike Kravetz void *src = (void *)usr_src; 4599fa4d75c1SMike Kravetz void *page_kaddr; 4600fa4d75c1SMike Kravetz unsigned long i, rc = 0; 4601fa4d75c1SMike Kravetz unsigned long ret_val = pages_per_huge_page * PAGE_SIZE; 4602fa4d75c1SMike Kravetz 4603fa4d75c1SMike Kravetz for (i = 0; i < pages_per_huge_page; i++) { 4604810a56b9SMike Kravetz if (allow_pagefault) 4605810a56b9SMike Kravetz page_kaddr = kmap(dst_page + i); 4606810a56b9SMike Kravetz else 4607fa4d75c1SMike Kravetz page_kaddr = kmap_atomic(dst_page + i); 4608fa4d75c1SMike Kravetz rc = copy_from_user(page_kaddr, 4609fa4d75c1SMike Kravetz (const void __user *)(src + i * PAGE_SIZE), 4610fa4d75c1SMike Kravetz PAGE_SIZE); 4611810a56b9SMike Kravetz if (allow_pagefault) 4612810a56b9SMike Kravetz kunmap(dst_page + i); 4613810a56b9SMike Kravetz else 4614fa4d75c1SMike Kravetz kunmap_atomic(page_kaddr); 4615fa4d75c1SMike Kravetz 4616fa4d75c1SMike Kravetz ret_val -= (PAGE_SIZE - rc); 4617fa4d75c1SMike Kravetz if (rc) 4618fa4d75c1SMike Kravetz break; 4619fa4d75c1SMike Kravetz 4620fa4d75c1SMike Kravetz cond_resched(); 4621fa4d75c1SMike Kravetz } 4622fa4d75c1SMike Kravetz return ret_val; 4623fa4d75c1SMike Kravetz } 462447ad8475SAndrea Arcangeli #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */ 462549076ec2SKirill A. Shutemov 462640b64acdSOlof Johansson #if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS 4627b35f1819SKirill A. Shutemov 4628b35f1819SKirill A. Shutemov static struct kmem_cache *page_ptl_cachep; 4629b35f1819SKirill A. Shutemov 4630b35f1819SKirill A. Shutemov void __init ptlock_cache_init(void) 4631b35f1819SKirill A. Shutemov { 4632b35f1819SKirill A. Shutemov page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0, 4633b35f1819SKirill A. Shutemov SLAB_PANIC, NULL); 4634b35f1819SKirill A. Shutemov } 4635b35f1819SKirill A. Shutemov 4636539edb58SPeter Zijlstra bool ptlock_alloc(struct page *page) 463749076ec2SKirill A. Shutemov { 463849076ec2SKirill A. Shutemov spinlock_t *ptl; 463949076ec2SKirill A. Shutemov 4640b35f1819SKirill A. Shutemov ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL); 464149076ec2SKirill A. Shutemov if (!ptl) 464249076ec2SKirill A. Shutemov return false; 4643539edb58SPeter Zijlstra page->ptl = ptl; 464449076ec2SKirill A. Shutemov return true; 464549076ec2SKirill A. Shutemov } 464649076ec2SKirill A. Shutemov 4647539edb58SPeter Zijlstra void ptlock_free(struct page *page) 464849076ec2SKirill A. Shutemov { 4649b35f1819SKirill A. Shutemov kmem_cache_free(page_ptl_cachep, page->ptl); 465049076ec2SKirill A. Shutemov } 465149076ec2SKirill A. Shutemov #endif 4652