1*d61ea1cbSPeter Xu 2457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 31da177e4SLinus Torvalds /* 41da177e4SLinus Torvalds * linux/mm/memory.c 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds 71da177e4SLinus Torvalds */ 81da177e4SLinus Torvalds 91da177e4SLinus Torvalds /* 101da177e4SLinus Torvalds * demand-loading started 01.12.91 - seems it is high on the list of 111da177e4SLinus Torvalds * things wanted, and it should be easy to implement. - Linus 121da177e4SLinus Torvalds */ 131da177e4SLinus Torvalds 141da177e4SLinus Torvalds /* 151da177e4SLinus Torvalds * Ok, demand-loading was easy, shared pages a little bit tricker. Shared 161da177e4SLinus Torvalds * pages started 02.12.91, seems to work. - Linus. 171da177e4SLinus Torvalds * 181da177e4SLinus Torvalds * Tested sharing by executing about 30 /bin/sh: under the old kernel it 191da177e4SLinus Torvalds * would have taken more than the 6M I have free, but it worked well as 201da177e4SLinus Torvalds * far as I could see. 211da177e4SLinus Torvalds * 221da177e4SLinus Torvalds * Also corrected some "invalidate()"s - I wasn't doing enough of them. 231da177e4SLinus Torvalds */ 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds /* 261da177e4SLinus Torvalds * Real VM (paging to/from disk) started 18.12.91. Much more work and 271da177e4SLinus Torvalds * thought has to go into this. Oh, well.. 281da177e4SLinus Torvalds * 19.12.91 - works, somewhat. Sometimes I get faults, don't know why. 291da177e4SLinus Torvalds * Found it. Everything seems to work now. 301da177e4SLinus Torvalds * 20.12.91 - Ok, making the swap-device changeable like the root. 311da177e4SLinus Torvalds */ 321da177e4SLinus Torvalds 331da177e4SLinus Torvalds /* 341da177e4SLinus Torvalds * 05.04.94 - Multi-page memory management added for v1.1. 351da177e4SLinus Torvalds * Idea by Alex Bligh (alex@cconcepts.co.uk) 361da177e4SLinus Torvalds * 371da177e4SLinus Torvalds * 16.07.99 - Support of BIGMEM added by Gerhard Wichert, Siemens AG 381da177e4SLinus Torvalds * (Gerhard.Wichert@pdb.siemens.de) 391da177e4SLinus Torvalds * 401da177e4SLinus Torvalds * Aug/Sep 2004 Changed to four level page tables (Andi Kleen) 411da177e4SLinus Torvalds */ 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds #include <linux/kernel_stat.h> 441da177e4SLinus Torvalds #include <linux/mm.h> 4536090defSArnd Bergmann #include <linux/mm_inline.h> 466e84f315SIngo Molnar #include <linux/sched/mm.h> 47f7ccbae4SIngo Molnar #include <linux/sched/coredump.h> 486a3827d7SIngo Molnar #include <linux/sched/numa_balancing.h> 4929930025SIngo Molnar #include <linux/sched/task.h> 501da177e4SLinus Torvalds #include <linux/hugetlb.h> 511da177e4SLinus Torvalds #include <linux/mman.h> 521da177e4SLinus Torvalds #include <linux/swap.h> 531da177e4SLinus Torvalds #include <linux/highmem.h> 541da177e4SLinus Torvalds #include <linux/pagemap.h> 555042db43SJérôme Glisse #include <linux/memremap.h> 56b073d7f8SAlexander Potapenko #include <linux/kmsan.h> 579a840895SHugh Dickins #include <linux/ksm.h> 581da177e4SLinus Torvalds #include <linux/rmap.h> 59b95f1b31SPaul Gortmaker #include <linux/export.h> 600ff92245SShailabh Nagar #include <linux/delayacct.h> 611da177e4SLinus Torvalds #include <linux/init.h> 6201c8f1c4SDan Williams #include <linux/pfn_t.h> 63edc79b2aSPeter Zijlstra #include <linux/writeback.h> 648a9f3ccdSBalbir Singh #include <linux/memcontrol.h> 65cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h> 663dc14741SHugh Dickins #include <linux/swapops.h> 673dc14741SHugh Dickins #include <linux/elf.h> 685a0e3ad6STejun Heo #include <linux/gfp.h> 694daae3b4SMel Gorman #include <linux/migrate.h> 702fbc57c5SAndy Shevchenko #include <linux/string.h> 71467b171aSAneesh Kumar K.V #include <linux/memory-tiers.h> 721592eef0SKirill A. Shutemov #include <linux/debugfs.h> 736b251fc9SAndrea Arcangeli #include <linux/userfaultfd_k.h> 74bc2466e4SJan Kara #include <linux/dax.h> 756b31d595SMichal Hocko #include <linux/oom.h> 7698fa15f3SAnshuman Khandual #include <linux/numa.h> 77bce617edSPeter Xu #include <linux/perf_event.h> 78bce617edSPeter Xu #include <linux/ptrace.h> 79e80d3909SJoerg Roedel #include <linux/vmalloc.h> 8033024536SHuang Ying #include <linux/sched/sysctl.h> 811da177e4SLinus Torvalds 82b3d1411bSJoel Fernandes (Google) #include <trace/events/kmem.h> 83b3d1411bSJoel Fernandes (Google) 846952b61dSAlexey Dobriyan #include <asm/io.h> 8533a709b2SDave Hansen #include <asm/mmu_context.h> 861da177e4SLinus Torvalds #include <asm/pgalloc.h> 877c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 881da177e4SLinus Torvalds #include <asm/tlb.h> 891da177e4SLinus Torvalds #include <asm/tlbflush.h> 901da177e4SLinus Torvalds 91e80d3909SJoerg Roedel #include "pgalloc-track.h" 9242b77728SJan Beulich #include "internal.h" 93014bb1deSNeilBrown #include "swap.h" 9442b77728SJan Beulich 95af27d940SArnd Bergmann #if defined(LAST_CPUPID_NOT_IN_PAGE_FLAGS) && !defined(CONFIG_COMPILE_TEST) 9690572890SPeter Zijlstra #warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid. 9775980e97SPeter Zijlstra #endif 9875980e97SPeter Zijlstra 99a9ee6cf5SMike Rapoport #ifndef CONFIG_NUMA 1001da177e4SLinus Torvalds unsigned long max_mapnr; 1011da177e4SLinus Torvalds EXPORT_SYMBOL(max_mapnr); 102166f61b9STobin C Harding 103166f61b9STobin C Harding struct page *mem_map; 1041da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map); 1051da177e4SLinus Torvalds #endif 1061da177e4SLinus Torvalds 1075c041f5dSPeter Xu static vm_fault_t do_fault(struct vm_fault *vmf); 1082bad466cSPeter Xu static vm_fault_t do_anonymous_page(struct vm_fault *vmf); 1092bad466cSPeter Xu static bool vmf_pte_changed(struct vm_fault *vmf); 1102bad466cSPeter Xu 1112bad466cSPeter Xu /* 1122bad466cSPeter Xu * Return true if the original pte was a uffd-wp pte marker (so the pte was 1132bad466cSPeter Xu * wr-protected). 1142bad466cSPeter Xu */ 1152bad466cSPeter Xu static bool vmf_orig_pte_uffd_wp(struct vm_fault *vmf) 1162bad466cSPeter Xu { 1172bad466cSPeter Xu if (!(vmf->flags & FAULT_FLAG_ORIG_PTE_VALID)) 1182bad466cSPeter Xu return false; 1192bad466cSPeter Xu 1202bad466cSPeter Xu return pte_marker_uffd_wp(vmf->orig_pte); 1212bad466cSPeter Xu } 1225c041f5dSPeter Xu 1231da177e4SLinus Torvalds /* 1241da177e4SLinus Torvalds * A number of key systems in x86 including ioremap() rely on the assumption 1251da177e4SLinus Torvalds * that high_memory defines the upper bound on direct map memory, then end 1261da177e4SLinus Torvalds * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and 1271da177e4SLinus Torvalds * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL 1281da177e4SLinus Torvalds * and ZONE_HIGHMEM. 1291da177e4SLinus Torvalds */ 1301da177e4SLinus Torvalds void *high_memory; 1311da177e4SLinus Torvalds EXPORT_SYMBOL(high_memory); 1321da177e4SLinus Torvalds 13332a93233SIngo Molnar /* 13432a93233SIngo Molnar * Randomize the address space (stacks, mmaps, brk, etc.). 13532a93233SIngo Molnar * 13632a93233SIngo Molnar * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization, 13732a93233SIngo Molnar * as ancient (libc5 based) binaries can segfault. ) 13832a93233SIngo Molnar */ 13932a93233SIngo Molnar int randomize_va_space __read_mostly = 14032a93233SIngo Molnar #ifdef CONFIG_COMPAT_BRK 14132a93233SIngo Molnar 1; 14232a93233SIngo Molnar #else 14332a93233SIngo Molnar 2; 14432a93233SIngo Molnar #endif 145a62eaf15SAndi Kleen 14646bdb427SWill Deacon #ifndef arch_wants_old_prefaulted_pte 14746bdb427SWill Deacon static inline bool arch_wants_old_prefaulted_pte(void) 14846bdb427SWill Deacon { 14946bdb427SWill Deacon /* 15046bdb427SWill Deacon * Transitioning a PTE from 'old' to 'young' can be expensive on 15146bdb427SWill Deacon * some architectures, even if it's performed in hardware. By 15246bdb427SWill Deacon * default, "false" means prefaulted entries will be 'young'. 15346bdb427SWill Deacon */ 15446bdb427SWill Deacon return false; 15546bdb427SWill Deacon } 15646bdb427SWill Deacon #endif 15746bdb427SWill Deacon 158a62eaf15SAndi Kleen static int __init disable_randmaps(char *s) 159a62eaf15SAndi Kleen { 160a62eaf15SAndi Kleen randomize_va_space = 0; 1619b41046cSOGAWA Hirofumi return 1; 162a62eaf15SAndi Kleen } 163a62eaf15SAndi Kleen __setup("norandmaps", disable_randmaps); 164a62eaf15SAndi Kleen 16562eede62SHugh Dickins unsigned long zero_pfn __read_mostly; 1660b70068eSArd Biesheuvel EXPORT_SYMBOL(zero_pfn); 1670b70068eSArd Biesheuvel 168166f61b9STobin C Harding unsigned long highest_memmap_pfn __read_mostly; 169166f61b9STobin C Harding 170a13ea5b7SHugh Dickins /* 171a13ea5b7SHugh Dickins * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init() 172a13ea5b7SHugh Dickins */ 173a13ea5b7SHugh Dickins static int __init init_zero_pfn(void) 174a13ea5b7SHugh Dickins { 175a13ea5b7SHugh Dickins zero_pfn = page_to_pfn(ZERO_PAGE(0)); 176a13ea5b7SHugh Dickins return 0; 177a13ea5b7SHugh Dickins } 178e720e7d0SIlya Lipnitskiy early_initcall(init_zero_pfn); 179a62eaf15SAndi Kleen 180f1a79412SShakeel Butt void mm_trace_rss_stat(struct mm_struct *mm, int member) 181b3d1411bSJoel Fernandes (Google) { 182f1a79412SShakeel Butt trace_rss_stat(mm, member); 183b3d1411bSJoel Fernandes (Google) } 184d559db08SKAMEZAWA Hiroyuki 1851da177e4SLinus Torvalds /* 1861da177e4SLinus Torvalds * Note: this doesn't free the actual pages themselves. That 1871da177e4SLinus Torvalds * has been handled earlier when unmapping all the memory regions. 1881da177e4SLinus Torvalds */ 1899e1b32caSBenjamin Herrenschmidt static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd, 1909e1b32caSBenjamin Herrenschmidt unsigned long addr) 1911da177e4SLinus Torvalds { 1922f569afdSMartin Schwidefsky pgtable_t token = pmd_pgtable(*pmd); 1931da177e4SLinus Torvalds pmd_clear(pmd); 1949e1b32caSBenjamin Herrenschmidt pte_free_tlb(tlb, token, addr); 195c4812909SKirill A. Shutemov mm_dec_nr_ptes(tlb->mm); 1961da177e4SLinus Torvalds } 1971da177e4SLinus Torvalds 198e0da382cSHugh Dickins static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud, 199e0da382cSHugh Dickins unsigned long addr, unsigned long end, 200e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 2011da177e4SLinus Torvalds { 2021da177e4SLinus Torvalds pmd_t *pmd; 2031da177e4SLinus Torvalds unsigned long next; 204e0da382cSHugh Dickins unsigned long start; 2051da177e4SLinus Torvalds 206e0da382cSHugh Dickins start = addr; 2071da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 2081da177e4SLinus Torvalds do { 2091da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 2101da177e4SLinus Torvalds if (pmd_none_or_clear_bad(pmd)) 2111da177e4SLinus Torvalds continue; 2129e1b32caSBenjamin Herrenschmidt free_pte_range(tlb, pmd, addr); 2131da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 2141da177e4SLinus Torvalds 215e0da382cSHugh Dickins start &= PUD_MASK; 216e0da382cSHugh Dickins if (start < floor) 217e0da382cSHugh Dickins return; 218e0da382cSHugh Dickins if (ceiling) { 219e0da382cSHugh Dickins ceiling &= PUD_MASK; 220e0da382cSHugh Dickins if (!ceiling) 221e0da382cSHugh Dickins return; 2221da177e4SLinus Torvalds } 223e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 224e0da382cSHugh Dickins return; 225e0da382cSHugh Dickins 226e0da382cSHugh Dickins pmd = pmd_offset(pud, start); 227e0da382cSHugh Dickins pud_clear(pud); 2289e1b32caSBenjamin Herrenschmidt pmd_free_tlb(tlb, pmd, start); 229dc6c9a35SKirill A. Shutemov mm_dec_nr_pmds(tlb->mm); 2301da177e4SLinus Torvalds } 2311da177e4SLinus Torvalds 232c2febafcSKirill A. Shutemov static inline void free_pud_range(struct mmu_gather *tlb, p4d_t *p4d, 233e0da382cSHugh Dickins unsigned long addr, unsigned long end, 234e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 2351da177e4SLinus Torvalds { 2361da177e4SLinus Torvalds pud_t *pud; 2371da177e4SLinus Torvalds unsigned long next; 238e0da382cSHugh Dickins unsigned long start; 2391da177e4SLinus Torvalds 240e0da382cSHugh Dickins start = addr; 241c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 2421da177e4SLinus Torvalds do { 2431da177e4SLinus Torvalds next = pud_addr_end(addr, end); 2441da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 2451da177e4SLinus Torvalds continue; 246e0da382cSHugh Dickins free_pmd_range(tlb, pud, addr, next, floor, ceiling); 2471da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 2481da177e4SLinus Torvalds 249c2febafcSKirill A. Shutemov start &= P4D_MASK; 250c2febafcSKirill A. Shutemov if (start < floor) 251c2febafcSKirill A. Shutemov return; 252c2febafcSKirill A. Shutemov if (ceiling) { 253c2febafcSKirill A. Shutemov ceiling &= P4D_MASK; 254c2febafcSKirill A. Shutemov if (!ceiling) 255c2febafcSKirill A. Shutemov return; 256c2febafcSKirill A. Shutemov } 257c2febafcSKirill A. Shutemov if (end - 1 > ceiling - 1) 258c2febafcSKirill A. Shutemov return; 259c2febafcSKirill A. Shutemov 260c2febafcSKirill A. Shutemov pud = pud_offset(p4d, start); 261c2febafcSKirill A. Shutemov p4d_clear(p4d); 262c2febafcSKirill A. Shutemov pud_free_tlb(tlb, pud, start); 263b4e98d9aSKirill A. Shutemov mm_dec_nr_puds(tlb->mm); 264c2febafcSKirill A. Shutemov } 265c2febafcSKirill A. Shutemov 266c2febafcSKirill A. Shutemov static inline void free_p4d_range(struct mmu_gather *tlb, pgd_t *pgd, 267c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 268c2febafcSKirill A. Shutemov unsigned long floor, unsigned long ceiling) 269c2febafcSKirill A. Shutemov { 270c2febafcSKirill A. Shutemov p4d_t *p4d; 271c2febafcSKirill A. Shutemov unsigned long next; 272c2febafcSKirill A. Shutemov unsigned long start; 273c2febafcSKirill A. Shutemov 274c2febafcSKirill A. Shutemov start = addr; 275c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 276c2febafcSKirill A. Shutemov do { 277c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 278c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(p4d)) 279c2febafcSKirill A. Shutemov continue; 280c2febafcSKirill A. Shutemov free_pud_range(tlb, p4d, addr, next, floor, ceiling); 281c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 282c2febafcSKirill A. Shutemov 283e0da382cSHugh Dickins start &= PGDIR_MASK; 284e0da382cSHugh Dickins if (start < floor) 285e0da382cSHugh Dickins return; 286e0da382cSHugh Dickins if (ceiling) { 287e0da382cSHugh Dickins ceiling &= PGDIR_MASK; 288e0da382cSHugh Dickins if (!ceiling) 289e0da382cSHugh Dickins return; 2901da177e4SLinus Torvalds } 291e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 292e0da382cSHugh Dickins return; 293e0da382cSHugh Dickins 294c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, start); 295e0da382cSHugh Dickins pgd_clear(pgd); 296c2febafcSKirill A. Shutemov p4d_free_tlb(tlb, p4d, start); 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds /* 300e0da382cSHugh Dickins * This function frees user-level page tables of a process. 3011da177e4SLinus Torvalds */ 30242b77728SJan Beulich void free_pgd_range(struct mmu_gather *tlb, 303e0da382cSHugh Dickins unsigned long addr, unsigned long end, 304e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 3051da177e4SLinus Torvalds { 3061da177e4SLinus Torvalds pgd_t *pgd; 3071da177e4SLinus Torvalds unsigned long next; 3081da177e4SLinus Torvalds 309e0da382cSHugh Dickins /* 310e0da382cSHugh Dickins * The next few lines have given us lots of grief... 311e0da382cSHugh Dickins * 312e0da382cSHugh Dickins * Why are we testing PMD* at this top level? Because often 313e0da382cSHugh Dickins * there will be no work to do at all, and we'd prefer not to 314e0da382cSHugh Dickins * go all the way down to the bottom just to discover that. 315e0da382cSHugh Dickins * 316e0da382cSHugh Dickins * Why all these "- 1"s? Because 0 represents both the bottom 317e0da382cSHugh Dickins * of the address space and the top of it (using -1 for the 318e0da382cSHugh Dickins * top wouldn't help much: the masks would do the wrong thing). 319e0da382cSHugh Dickins * The rule is that addr 0 and floor 0 refer to the bottom of 320e0da382cSHugh Dickins * the address space, but end 0 and ceiling 0 refer to the top 321e0da382cSHugh Dickins * Comparisons need to use "end - 1" and "ceiling - 1" (though 322e0da382cSHugh Dickins * that end 0 case should be mythical). 323e0da382cSHugh Dickins * 324e0da382cSHugh Dickins * Wherever addr is brought up or ceiling brought down, we must 325e0da382cSHugh Dickins * be careful to reject "the opposite 0" before it confuses the 326e0da382cSHugh Dickins * subsequent tests. But what about where end is brought down 327e0da382cSHugh Dickins * by PMD_SIZE below? no, end can't go down to 0 there. 328e0da382cSHugh Dickins * 329e0da382cSHugh Dickins * Whereas we round start (addr) and ceiling down, by different 330e0da382cSHugh Dickins * masks at different levels, in order to test whether a table 331e0da382cSHugh Dickins * now has no other vmas using it, so can be freed, we don't 332e0da382cSHugh Dickins * bother to round floor or end up - the tests don't need that. 333e0da382cSHugh Dickins */ 334e0da382cSHugh Dickins 335e0da382cSHugh Dickins addr &= PMD_MASK; 336e0da382cSHugh Dickins if (addr < floor) { 337e0da382cSHugh Dickins addr += PMD_SIZE; 338e0da382cSHugh Dickins if (!addr) 339e0da382cSHugh Dickins return; 340e0da382cSHugh Dickins } 341e0da382cSHugh Dickins if (ceiling) { 342e0da382cSHugh Dickins ceiling &= PMD_MASK; 343e0da382cSHugh Dickins if (!ceiling) 344e0da382cSHugh Dickins return; 345e0da382cSHugh Dickins } 346e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 347e0da382cSHugh Dickins end -= PMD_SIZE; 348e0da382cSHugh Dickins if (addr > end - 1) 349e0da382cSHugh Dickins return; 35007e32661SAneesh Kumar K.V /* 35107e32661SAneesh Kumar K.V * We add page table cache pages with PAGE_SIZE, 35207e32661SAneesh Kumar K.V * (see pte_free_tlb()), flush the tlb if we need 35307e32661SAneesh Kumar K.V */ 354ed6a7935SPeter Zijlstra tlb_change_page_size(tlb, PAGE_SIZE); 35542b77728SJan Beulich pgd = pgd_offset(tlb->mm, addr); 3561da177e4SLinus Torvalds do { 3571da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 3581da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 3591da177e4SLinus Torvalds continue; 360c2febafcSKirill A. Shutemov free_p4d_range(tlb, pgd, addr, next, floor, ceiling); 3611da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 362e0da382cSHugh Dickins } 363e0da382cSHugh Dickins 364fd892593SLiam R. Howlett void free_pgtables(struct mmu_gather *tlb, struct ma_state *mas, 365763ecb03SLiam R. Howlett struct vm_area_struct *vma, unsigned long floor, 36698e51a22SSuren Baghdasaryan unsigned long ceiling, bool mm_wr_locked) 367e0da382cSHugh Dickins { 368763ecb03SLiam R. Howlett do { 369e0da382cSHugh Dickins unsigned long addr = vma->vm_start; 370763ecb03SLiam R. Howlett struct vm_area_struct *next; 371763ecb03SLiam R. Howlett 372763ecb03SLiam R. Howlett /* 373763ecb03SLiam R. Howlett * Note: USER_PGTABLES_CEILING may be passed as ceiling and may 374763ecb03SLiam R. Howlett * be 0. This will underflow and is okay. 375763ecb03SLiam R. Howlett */ 376fd892593SLiam R. Howlett next = mas_find(mas, ceiling - 1); 377e0da382cSHugh Dickins 3788f4f8c16SHugh Dickins /* 37925d9e2d1Snpiggin@suse.de * Hide vma from rmap and truncate_pagecache before freeing 38025d9e2d1Snpiggin@suse.de * pgtables 3818f4f8c16SHugh Dickins */ 38298e51a22SSuren Baghdasaryan if (mm_wr_locked) 38398e51a22SSuren Baghdasaryan vma_start_write(vma); 3845beb4930SRik van Riel unlink_anon_vmas(vma); 3858f4f8c16SHugh Dickins unlink_file_vma(vma); 3868f4f8c16SHugh Dickins 3879da61aefSDavid Gibson if (is_vm_hugetlb_page(vma)) { 3883bf5ee95SHugh Dickins hugetlb_free_pgd_range(tlb, addr, vma->vm_end, 3893bf5ee95SHugh Dickins floor, next ? next->vm_start : ceiling); 3903bf5ee95SHugh Dickins } else { 3913bf5ee95SHugh Dickins /* 3923bf5ee95SHugh Dickins * Optimization: gather nearby vmas into one call down 3933bf5ee95SHugh Dickins */ 3943bf5ee95SHugh Dickins while (next && next->vm_start <= vma->vm_end + PMD_SIZE 3954866920bSDavid Gibson && !is_vm_hugetlb_page(next)) { 396e0da382cSHugh Dickins vma = next; 397fd892593SLiam R. Howlett next = mas_find(mas, ceiling - 1); 39898e51a22SSuren Baghdasaryan if (mm_wr_locked) 39998e51a22SSuren Baghdasaryan vma_start_write(vma); 4005beb4930SRik van Riel unlink_anon_vmas(vma); 4018f4f8c16SHugh Dickins unlink_file_vma(vma); 402e0da382cSHugh Dickins } 4033bf5ee95SHugh Dickins free_pgd_range(tlb, addr, vma->vm_end, 404e0da382cSHugh Dickins floor, next ? next->vm_start : ceiling); 4053bf5ee95SHugh Dickins } 406e0da382cSHugh Dickins vma = next; 407763ecb03SLiam R. Howlett } while (vma); 4081da177e4SLinus Torvalds } 4091da177e4SLinus Torvalds 41003c4f204SQi Zheng void pmd_install(struct mm_struct *mm, pmd_t *pmd, pgtable_t *pte) 4111da177e4SLinus Torvalds { 41203c4f204SQi Zheng spinlock_t *ptl = pmd_lock(mm, pmd); 4131bb3630eSHugh Dickins 41403c4f204SQi Zheng if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 41503c4f204SQi Zheng mm_inc_nr_ptes(mm); 416362a61adSNick Piggin /* 417362a61adSNick Piggin * Ensure all pte setup (eg. pte page lock and page clearing) are 418362a61adSNick Piggin * visible before the pte is made visible to other CPUs by being 419362a61adSNick Piggin * put into page tables. 420362a61adSNick Piggin * 421362a61adSNick Piggin * The other side of the story is the pointer chasing in the page 422362a61adSNick Piggin * table walking code (when walking the page table without locking; 423362a61adSNick Piggin * ie. most of the time). Fortunately, these data accesses consist 424362a61adSNick Piggin * of a chain of data-dependent loads, meaning most CPUs (alpha 425362a61adSNick Piggin * being the notable exception) will already guarantee loads are 426362a61adSNick Piggin * seen in-order. See the alpha page table accessors for the 427bb7cdd38SWill Deacon * smp_rmb() barriers in page table walking code. 428362a61adSNick Piggin */ 429362a61adSNick Piggin smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */ 430ed33b5a6SQi Zheng pmd_populate(mm, pmd, *pte); 431ed33b5a6SQi Zheng *pte = NULL; 4324b471e88SKirill A. Shutemov } 433c4088ebdSKirill A. Shutemov spin_unlock(ptl); 434ed33b5a6SQi Zheng } 435ed33b5a6SQi Zheng 436ed33b5a6SQi Zheng int __pte_alloc(struct mm_struct *mm, pmd_t *pmd) 437ed33b5a6SQi Zheng { 438ed33b5a6SQi Zheng pgtable_t new = pte_alloc_one(mm); 439ed33b5a6SQi Zheng if (!new) 440ed33b5a6SQi Zheng return -ENOMEM; 4418ac1f832SAndrea Arcangeli 44203c4f204SQi Zheng pmd_install(mm, pmd, &new); 4432f569afdSMartin Schwidefsky if (new) 4442f569afdSMartin Schwidefsky pte_free(mm, new); 4451bb3630eSHugh Dickins return 0; 4461da177e4SLinus Torvalds } 4471da177e4SLinus Torvalds 4484cf58924SJoel Fernandes (Google) int __pte_alloc_kernel(pmd_t *pmd) 4491da177e4SLinus Torvalds { 4504cf58924SJoel Fernandes (Google) pte_t *new = pte_alloc_one_kernel(&init_mm); 4511da177e4SLinus Torvalds if (!new) 4521bb3630eSHugh Dickins return -ENOMEM; 4531da177e4SLinus Torvalds 454872fec16SHugh Dickins spin_lock(&init_mm.page_table_lock); 4558ac1f832SAndrea Arcangeli if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 456ed33b5a6SQi Zheng smp_wmb(); /* See comment in pmd_install() */ 457872fec16SHugh Dickins pmd_populate_kernel(&init_mm, pmd, new); 4582f569afdSMartin Schwidefsky new = NULL; 4594b471e88SKirill A. Shutemov } 460872fec16SHugh Dickins spin_unlock(&init_mm.page_table_lock); 4612f569afdSMartin Schwidefsky if (new) 4622f569afdSMartin Schwidefsky pte_free_kernel(&init_mm, new); 4631bb3630eSHugh Dickins return 0; 4641da177e4SLinus Torvalds } 4651da177e4SLinus Torvalds 466d559db08SKAMEZAWA Hiroyuki static inline void init_rss_vec(int *rss) 467ae859762SHugh Dickins { 468d559db08SKAMEZAWA Hiroyuki memset(rss, 0, sizeof(int) * NR_MM_COUNTERS); 469d559db08SKAMEZAWA Hiroyuki } 470d559db08SKAMEZAWA Hiroyuki 471d559db08SKAMEZAWA Hiroyuki static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss) 472d559db08SKAMEZAWA Hiroyuki { 473d559db08SKAMEZAWA Hiroyuki int i; 474d559db08SKAMEZAWA Hiroyuki 475d559db08SKAMEZAWA Hiroyuki for (i = 0; i < NR_MM_COUNTERS; i++) 476d559db08SKAMEZAWA Hiroyuki if (rss[i]) 477d559db08SKAMEZAWA Hiroyuki add_mm_counter(mm, i, rss[i]); 478ae859762SHugh Dickins } 479ae859762SHugh Dickins 4801da177e4SLinus Torvalds /* 4816aab341eSLinus Torvalds * This function is called to print an error when a bad pte 4826aab341eSLinus Torvalds * is found. For example, we might have a PFN-mapped pte in 4836aab341eSLinus Torvalds * a region that doesn't allow it. 484b5810039SNick Piggin * 485b5810039SNick Piggin * The calling function must still handle the error. 486b5810039SNick Piggin */ 4873dc14741SHugh Dickins static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr, 4883dc14741SHugh Dickins pte_t pte, struct page *page) 489b5810039SNick Piggin { 4903dc14741SHugh Dickins pgd_t *pgd = pgd_offset(vma->vm_mm, addr); 491c2febafcSKirill A. Shutemov p4d_t *p4d = p4d_offset(pgd, addr); 492c2febafcSKirill A. Shutemov pud_t *pud = pud_offset(p4d, addr); 4933dc14741SHugh Dickins pmd_t *pmd = pmd_offset(pud, addr); 4943dc14741SHugh Dickins struct address_space *mapping; 4953dc14741SHugh Dickins pgoff_t index; 496d936cf9bSHugh Dickins static unsigned long resume; 497d936cf9bSHugh Dickins static unsigned long nr_shown; 498d936cf9bSHugh Dickins static unsigned long nr_unshown; 499d936cf9bSHugh Dickins 500d936cf9bSHugh Dickins /* 501d936cf9bSHugh Dickins * Allow a burst of 60 reports, then keep quiet for that minute; 502d936cf9bSHugh Dickins * or allow a steady drip of one report per second. 503d936cf9bSHugh Dickins */ 504d936cf9bSHugh Dickins if (nr_shown == 60) { 505d936cf9bSHugh Dickins if (time_before(jiffies, resume)) { 506d936cf9bSHugh Dickins nr_unshown++; 507d936cf9bSHugh Dickins return; 508d936cf9bSHugh Dickins } 509d936cf9bSHugh Dickins if (nr_unshown) { 5101170532bSJoe Perches pr_alert("BUG: Bad page map: %lu messages suppressed\n", 511d936cf9bSHugh Dickins nr_unshown); 512d936cf9bSHugh Dickins nr_unshown = 0; 513d936cf9bSHugh Dickins } 514d936cf9bSHugh Dickins nr_shown = 0; 515d936cf9bSHugh Dickins } 516d936cf9bSHugh Dickins if (nr_shown++ == 0) 517d936cf9bSHugh Dickins resume = jiffies + 60 * HZ; 5183dc14741SHugh Dickins 5193dc14741SHugh Dickins mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL; 5203dc14741SHugh Dickins index = linear_page_index(vma, addr); 5213dc14741SHugh Dickins 5221170532bSJoe Perches pr_alert("BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n", 5233dc14741SHugh Dickins current->comm, 5243dc14741SHugh Dickins (long long)pte_val(pte), (long long)pmd_val(*pmd)); 525718a3821SWu Fengguang if (page) 526f0b791a3SDave Hansen dump_page(page, "bad pte"); 5276aa9b8b2SKefeng Wang pr_alert("addr:%px vm_flags:%08lx anon_vma:%px mapping:%px index:%lx\n", 5283dc14741SHugh Dickins (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index); 5297e0a1265SMatthew Wilcox (Oracle) pr_alert("file:%pD fault:%ps mmap:%ps read_folio:%ps\n", 5302682582aSKonstantin Khlebnikov vma->vm_file, 5312682582aSKonstantin Khlebnikov vma->vm_ops ? vma->vm_ops->fault : NULL, 5322682582aSKonstantin Khlebnikov vma->vm_file ? vma->vm_file->f_op->mmap : NULL, 5337e0a1265SMatthew Wilcox (Oracle) mapping ? mapping->a_ops->read_folio : NULL); 534b5810039SNick Piggin dump_stack(); 535373d4d09SRusty Russell add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); 536b5810039SNick Piggin } 537b5810039SNick Piggin 538b5810039SNick Piggin /* 5397e675137SNick Piggin * vm_normal_page -- This function gets the "struct page" associated with a pte. 5406aab341eSLinus Torvalds * 5417e675137SNick Piggin * "Special" mappings do not wish to be associated with a "struct page" (either 5427e675137SNick Piggin * it doesn't exist, or it exists but they don't want to touch it). In this 5437e675137SNick Piggin * case, NULL is returned here. "Normal" mappings do have a struct page. 544b379d790SJared Hulbert * 5457e675137SNick Piggin * There are 2 broad cases. Firstly, an architecture may define a pte_special() 5467e675137SNick Piggin * pte bit, in which case this function is trivial. Secondly, an architecture 5477e675137SNick Piggin * may not have a spare pte bit, which requires a more complicated scheme, 5487e675137SNick Piggin * described below. 5497e675137SNick Piggin * 5507e675137SNick Piggin * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a 5517e675137SNick Piggin * special mapping (even if there are underlying and valid "struct pages"). 5527e675137SNick Piggin * COWed pages of a VM_PFNMAP are always normal. 5536aab341eSLinus Torvalds * 554b379d790SJared Hulbert * The way we recognize COWed pages within VM_PFNMAP mappings is through the 555b379d790SJared Hulbert * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit 5567e675137SNick Piggin * set, and the vm_pgoff will point to the first PFN mapped: thus every special 5577e675137SNick Piggin * mapping will always honor the rule 5586aab341eSLinus Torvalds * 5596aab341eSLinus Torvalds * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT) 5606aab341eSLinus Torvalds * 5617e675137SNick Piggin * And for normal mappings this is false. 562b379d790SJared Hulbert * 5637e675137SNick Piggin * This restricts such mappings to be a linear translation from virtual address 5647e675137SNick Piggin * to pfn. To get around this restriction, we allow arbitrary mappings so long 5657e675137SNick Piggin * as the vma is not a COW mapping; in that case, we know that all ptes are 5667e675137SNick Piggin * special (because none can have been COWed). 567b379d790SJared Hulbert * 568b379d790SJared Hulbert * 5697e675137SNick Piggin * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP. 5707e675137SNick Piggin * 571b379d790SJared Hulbert * VM_MIXEDMAP mappings can likewise contain memory with or without "struct 572b379d790SJared Hulbert * page" backing, however the difference is that _all_ pages with a struct 573b379d790SJared Hulbert * page (that is, those where pfn_valid is true) are refcounted and considered 574b379d790SJared Hulbert * normal pages by the VM. The disadvantage is that pages are refcounted 575b379d790SJared Hulbert * (which can be slower and simply not an option for some PFNMAP users). The 576b379d790SJared Hulbert * advantage is that we don't have to follow the strict linearity rule of 577b379d790SJared Hulbert * PFNMAP mappings in order to support COWable mappings. 578b379d790SJared Hulbert * 579ee498ed7SHugh Dickins */ 58025b2995aSChristoph Hellwig struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, 58125b2995aSChristoph Hellwig pte_t pte) 582ee498ed7SHugh Dickins { 58322b31eecSHugh Dickins unsigned long pfn = pte_pfn(pte); 5847e675137SNick Piggin 58500b3a331SLaurent Dufour if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL)) { 586b38af472SHugh Dickins if (likely(!pte_special(pte))) 58722b31eecSHugh Dickins goto check_pfn; 588667a0a06SDavid Vrabel if (vma->vm_ops && vma->vm_ops->find_special_page) 589667a0a06SDavid Vrabel return vma->vm_ops->find_special_page(vma, addr); 590a13ea5b7SHugh Dickins if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)) 591a13ea5b7SHugh Dickins return NULL; 592df6ad698SJérôme Glisse if (is_zero_pfn(pfn)) 593df6ad698SJérôme Glisse return NULL; 594e1fb4a08SDave Jiang if (pte_devmap(pte)) 5953218f871SAlex Sierra /* 5963218f871SAlex Sierra * NOTE: New users of ZONE_DEVICE will not set pte_devmap() 5973218f871SAlex Sierra * and will have refcounts incremented on their struct pages 5983218f871SAlex Sierra * when they are inserted into PTEs, thus they are safe to 5993218f871SAlex Sierra * return here. Legacy ZONE_DEVICE pages that set pte_devmap() 6003218f871SAlex Sierra * do not have refcounts. Example of legacy ZONE_DEVICE is 6013218f871SAlex Sierra * MEMORY_DEVICE_FS_DAX type in pmem or virtio_fs drivers. 6023218f871SAlex Sierra */ 603e1fb4a08SDave Jiang return NULL; 604e1fb4a08SDave Jiang 60522b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 6067e675137SNick Piggin return NULL; 6077e675137SNick Piggin } 6087e675137SNick Piggin 60900b3a331SLaurent Dufour /* !CONFIG_ARCH_HAS_PTE_SPECIAL case follows: */ 6107e675137SNick Piggin 611b379d790SJared Hulbert if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { 612b379d790SJared Hulbert if (vma->vm_flags & VM_MIXEDMAP) { 613b379d790SJared Hulbert if (!pfn_valid(pfn)) 614b379d790SJared Hulbert return NULL; 615b379d790SJared Hulbert goto out; 616b379d790SJared Hulbert } else { 6177e675137SNick Piggin unsigned long off; 6187e675137SNick Piggin off = (addr - vma->vm_start) >> PAGE_SHIFT; 6196aab341eSLinus Torvalds if (pfn == vma->vm_pgoff + off) 6206aab341eSLinus Torvalds return NULL; 62167121172SLinus Torvalds if (!is_cow_mapping(vma->vm_flags)) 622fb155c16SLinus Torvalds return NULL; 6236aab341eSLinus Torvalds } 624b379d790SJared Hulbert } 6256aab341eSLinus Torvalds 626b38af472SHugh Dickins if (is_zero_pfn(pfn)) 627b38af472SHugh Dickins return NULL; 62800b3a331SLaurent Dufour 62922b31eecSHugh Dickins check_pfn: 63022b31eecSHugh Dickins if (unlikely(pfn > highest_memmap_pfn)) { 63122b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 63222b31eecSHugh Dickins return NULL; 63322b31eecSHugh Dickins } 6346aab341eSLinus Torvalds 6356aab341eSLinus Torvalds /* 6367e675137SNick Piggin * NOTE! We still have PageReserved() pages in the page tables. 6377e675137SNick Piggin * eg. VDSO mappings can cause them to exist. 6386aab341eSLinus Torvalds */ 639b379d790SJared Hulbert out: 6406aab341eSLinus Torvalds return pfn_to_page(pfn); 641ee498ed7SHugh Dickins } 642ee498ed7SHugh Dickins 643318e9342SVishal Moola (Oracle) struct folio *vm_normal_folio(struct vm_area_struct *vma, unsigned long addr, 644318e9342SVishal Moola (Oracle) pte_t pte) 645318e9342SVishal Moola (Oracle) { 646318e9342SVishal Moola (Oracle) struct page *page = vm_normal_page(vma, addr, pte); 647318e9342SVishal Moola (Oracle) 648318e9342SVishal Moola (Oracle) if (page) 649318e9342SVishal Moola (Oracle) return page_folio(page); 650318e9342SVishal Moola (Oracle) return NULL; 651318e9342SVishal Moola (Oracle) } 652318e9342SVishal Moola (Oracle) 65328093f9fSGerald Schaefer #ifdef CONFIG_TRANSPARENT_HUGEPAGE 65428093f9fSGerald Schaefer struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr, 65528093f9fSGerald Schaefer pmd_t pmd) 65628093f9fSGerald Schaefer { 65728093f9fSGerald Schaefer unsigned long pfn = pmd_pfn(pmd); 65828093f9fSGerald Schaefer 65928093f9fSGerald Schaefer /* 66028093f9fSGerald Schaefer * There is no pmd_special() but there may be special pmds, e.g. 66128093f9fSGerald Schaefer * in a direct-access (dax) mapping, so let's just replicate the 66200b3a331SLaurent Dufour * !CONFIG_ARCH_HAS_PTE_SPECIAL case from vm_normal_page() here. 66328093f9fSGerald Schaefer */ 66428093f9fSGerald Schaefer if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { 66528093f9fSGerald Schaefer if (vma->vm_flags & VM_MIXEDMAP) { 66628093f9fSGerald Schaefer if (!pfn_valid(pfn)) 66728093f9fSGerald Schaefer return NULL; 66828093f9fSGerald Schaefer goto out; 66928093f9fSGerald Schaefer } else { 67028093f9fSGerald Schaefer unsigned long off; 67128093f9fSGerald Schaefer off = (addr - vma->vm_start) >> PAGE_SHIFT; 67228093f9fSGerald Schaefer if (pfn == vma->vm_pgoff + off) 67328093f9fSGerald Schaefer return NULL; 67428093f9fSGerald Schaefer if (!is_cow_mapping(vma->vm_flags)) 67528093f9fSGerald Schaefer return NULL; 67628093f9fSGerald Schaefer } 67728093f9fSGerald Schaefer } 67828093f9fSGerald Schaefer 679e1fb4a08SDave Jiang if (pmd_devmap(pmd)) 680e1fb4a08SDave Jiang return NULL; 6813cde287bSYu Zhao if (is_huge_zero_pmd(pmd)) 68228093f9fSGerald Schaefer return NULL; 68328093f9fSGerald Schaefer if (unlikely(pfn > highest_memmap_pfn)) 68428093f9fSGerald Schaefer return NULL; 68528093f9fSGerald Schaefer 68628093f9fSGerald Schaefer /* 68728093f9fSGerald Schaefer * NOTE! We still have PageReserved() pages in the page tables. 68828093f9fSGerald Schaefer * eg. VDSO mappings can cause them to exist. 68928093f9fSGerald Schaefer */ 69028093f9fSGerald Schaefer out: 69128093f9fSGerald Schaefer return pfn_to_page(pfn); 69228093f9fSGerald Schaefer } 69365610453SKefeng Wang 69465610453SKefeng Wang struct folio *vm_normal_folio_pmd(struct vm_area_struct *vma, 69565610453SKefeng Wang unsigned long addr, pmd_t pmd) 69665610453SKefeng Wang { 69765610453SKefeng Wang struct page *page = vm_normal_page_pmd(vma, addr, pmd); 69865610453SKefeng Wang 69965610453SKefeng Wang if (page) 70065610453SKefeng Wang return page_folio(page); 70165610453SKefeng Wang return NULL; 70265610453SKefeng Wang } 70328093f9fSGerald Schaefer #endif 70428093f9fSGerald Schaefer 705b756a3b5SAlistair Popple static void restore_exclusive_pte(struct vm_area_struct *vma, 706b756a3b5SAlistair Popple struct page *page, unsigned long address, 707b756a3b5SAlistair Popple pte_t *ptep) 708b756a3b5SAlistair Popple { 709c33c7948SRyan Roberts pte_t orig_pte; 710b756a3b5SAlistair Popple pte_t pte; 711b756a3b5SAlistair Popple swp_entry_t entry; 712b756a3b5SAlistair Popple 713c33c7948SRyan Roberts orig_pte = ptep_get(ptep); 714b756a3b5SAlistair Popple pte = pte_mkold(mk_pte(page, READ_ONCE(vma->vm_page_prot))); 715c33c7948SRyan Roberts if (pte_swp_soft_dirty(orig_pte)) 716b756a3b5SAlistair Popple pte = pte_mksoft_dirty(pte); 717b756a3b5SAlistair Popple 718c33c7948SRyan Roberts entry = pte_to_swp_entry(orig_pte); 719c33c7948SRyan Roberts if (pte_swp_uffd_wp(orig_pte)) 720b756a3b5SAlistair Popple pte = pte_mkuffd_wp(pte); 721b756a3b5SAlistair Popple else if (is_writable_device_exclusive_entry(entry)) 722b756a3b5SAlistair Popple pte = maybe_mkwrite(pte_mkdirty(pte), vma); 723b756a3b5SAlistair Popple 7246c287605SDavid Hildenbrand VM_BUG_ON(pte_write(pte) && !(PageAnon(page) && PageAnonExclusive(page))); 7256c287605SDavid Hildenbrand 726b756a3b5SAlistair Popple /* 727b756a3b5SAlistair Popple * No need to take a page reference as one was already 728b756a3b5SAlistair Popple * created when the swap entry was made. 729b756a3b5SAlistair Popple */ 730b756a3b5SAlistair Popple if (PageAnon(page)) 731f1e2db12SDavid Hildenbrand page_add_anon_rmap(page, vma, address, RMAP_NONE); 732b756a3b5SAlistair Popple else 733b756a3b5SAlistair Popple /* 734b756a3b5SAlistair Popple * Currently device exclusive access only supports anonymous 735b756a3b5SAlistair Popple * memory so the entry shouldn't point to a filebacked page. 736b756a3b5SAlistair Popple */ 7374d8ff640SMiaohe Lin WARN_ON_ONCE(1); 738b756a3b5SAlistair Popple 7391eba86c0SPasha Tatashin set_pte_at(vma->vm_mm, address, ptep, pte); 7401eba86c0SPasha Tatashin 741b756a3b5SAlistair Popple /* 742b756a3b5SAlistair Popple * No need to invalidate - it was non-present before. However 743b756a3b5SAlistair Popple * secondary CPUs may have mappings that need invalidating. 744b756a3b5SAlistair Popple */ 745b756a3b5SAlistair Popple update_mmu_cache(vma, address, ptep); 746b756a3b5SAlistair Popple } 747b756a3b5SAlistair Popple 748b756a3b5SAlistair Popple /* 749b756a3b5SAlistair Popple * Tries to restore an exclusive pte if the page lock can be acquired without 750b756a3b5SAlistair Popple * sleeping. 751b756a3b5SAlistair Popple */ 752b756a3b5SAlistair Popple static int 753b756a3b5SAlistair Popple try_restore_exclusive_pte(pte_t *src_pte, struct vm_area_struct *vma, 754b756a3b5SAlistair Popple unsigned long addr) 755b756a3b5SAlistair Popple { 756c33c7948SRyan Roberts swp_entry_t entry = pte_to_swp_entry(ptep_get(src_pte)); 757b756a3b5SAlistair Popple struct page *page = pfn_swap_entry_to_page(entry); 758b756a3b5SAlistair Popple 759b756a3b5SAlistair Popple if (trylock_page(page)) { 760b756a3b5SAlistair Popple restore_exclusive_pte(vma, page, addr, src_pte); 761b756a3b5SAlistair Popple unlock_page(page); 762b756a3b5SAlistair Popple return 0; 763b756a3b5SAlistair Popple } 764b756a3b5SAlistair Popple 765b756a3b5SAlistair Popple return -EBUSY; 766b756a3b5SAlistair Popple } 767b756a3b5SAlistair Popple 768ee498ed7SHugh Dickins /* 7691da177e4SLinus Torvalds * copy one vm_area from one task to the other. Assumes the page tables 7701da177e4SLinus Torvalds * already present in the new task to be cleared in the whole range 7711da177e4SLinus Torvalds * covered by this vma. 7721da177e4SLinus Torvalds */ 7731da177e4SLinus Torvalds 774df3a57d1SLinus Torvalds static unsigned long 775df3a57d1SLinus Torvalds copy_nonpresent_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, 7768f34f1eaSPeter Xu pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *dst_vma, 7778f34f1eaSPeter Xu struct vm_area_struct *src_vma, unsigned long addr, int *rss) 7781da177e4SLinus Torvalds { 7798f34f1eaSPeter Xu unsigned long vm_flags = dst_vma->vm_flags; 780c33c7948SRyan Roberts pte_t orig_pte = ptep_get(src_pte); 781c33c7948SRyan Roberts pte_t pte = orig_pte; 7821da177e4SLinus Torvalds struct page *page; 783c33c7948SRyan Roberts swp_entry_t entry = pte_to_swp_entry(orig_pte); 7840697212aSChristoph Lameter 7852022b4d1SHugh Dickins if (likely(!non_swap_entry(entry))) { 786570a335bSHugh Dickins if (swap_duplicate(entry) < 0) 7879a5cc85cSAlistair Popple return -EIO; 788570a335bSHugh Dickins 7891da177e4SLinus Torvalds /* make sure dst_mm is on swapoff's mmlist. */ 7901da177e4SLinus Torvalds if (unlikely(list_empty(&dst_mm->mmlist))) { 7911da177e4SLinus Torvalds spin_lock(&mmlist_lock); 792f412ac08SHugh Dickins if (list_empty(&dst_mm->mmlist)) 793f412ac08SHugh Dickins list_add(&dst_mm->mmlist, 794f412ac08SHugh Dickins &src_mm->mmlist); 7951da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 7961da177e4SLinus Torvalds } 7971493a191SDavid Hildenbrand /* Mark the swap entry as shared. */ 798c33c7948SRyan Roberts if (pte_swp_exclusive(orig_pte)) { 799c33c7948SRyan Roberts pte = pte_swp_clear_exclusive(orig_pte); 8001493a191SDavid Hildenbrand set_pte_at(src_mm, addr, src_pte, pte); 8011493a191SDavid Hildenbrand } 802b084d435SKAMEZAWA Hiroyuki rss[MM_SWAPENTS]++; 8032022b4d1SHugh Dickins } else if (is_migration_entry(entry)) { 804af5cdaf8SAlistair Popple page = pfn_swap_entry_to_page(entry); 8059f9f1acdSKonstantin Khlebnikov 806eca56ff9SJerome Marchand rss[mm_counter(page)]++; 8079f9f1acdSKonstantin Khlebnikov 8086c287605SDavid Hildenbrand if (!is_readable_migration_entry(entry) && 8090697212aSChristoph Lameter is_cow_mapping(vm_flags)) { 8100697212aSChristoph Lameter /* 8116c287605SDavid Hildenbrand * COW mappings require pages in both parent and child 8126c287605SDavid Hildenbrand * to be set to read. A previously exclusive entry is 8136c287605SDavid Hildenbrand * now shared. 8140697212aSChristoph Lameter */ 8154dd845b5SAlistair Popple entry = make_readable_migration_entry( 8164dd845b5SAlistair Popple swp_offset(entry)); 8170697212aSChristoph Lameter pte = swp_entry_to_pte(entry); 818c33c7948SRyan Roberts if (pte_swp_soft_dirty(orig_pte)) 819c3d16e16SCyrill Gorcunov pte = pte_swp_mksoft_dirty(pte); 820c33c7948SRyan Roberts if (pte_swp_uffd_wp(orig_pte)) 821f45ec5ffSPeter Xu pte = pte_swp_mkuffd_wp(pte); 8220697212aSChristoph Lameter set_pte_at(src_mm, addr, src_pte, pte); 8230697212aSChristoph Lameter } 8245042db43SJérôme Glisse } else if (is_device_private_entry(entry)) { 825af5cdaf8SAlistair Popple page = pfn_swap_entry_to_page(entry); 8265042db43SJérôme Glisse 8275042db43SJérôme Glisse /* 8285042db43SJérôme Glisse * Update rss count even for unaddressable pages, as 8295042db43SJérôme Glisse * they should treated just like normal pages in this 8305042db43SJérôme Glisse * respect. 8315042db43SJérôme Glisse * 8325042db43SJérôme Glisse * We will likely want to have some new rss counters 8335042db43SJérôme Glisse * for unaddressable pages, at some point. But for now 8345042db43SJérôme Glisse * keep things as they are. 8355042db43SJérôme Glisse */ 8365042db43SJérôme Glisse get_page(page); 8375042db43SJérôme Glisse rss[mm_counter(page)]++; 838fb3d824dSDavid Hildenbrand /* Cannot fail as these pages cannot get pinned. */ 839fb3d824dSDavid Hildenbrand BUG_ON(page_try_dup_anon_rmap(page, false, src_vma)); 8405042db43SJérôme Glisse 8415042db43SJérôme Glisse /* 8425042db43SJérôme Glisse * We do not preserve soft-dirty information, because so 8435042db43SJérôme Glisse * far, checkpoint/restore is the only feature that 8445042db43SJérôme Glisse * requires that. And checkpoint/restore does not work 8455042db43SJérôme Glisse * when a device driver is involved (you cannot easily 8465042db43SJérôme Glisse * save and restore device driver state). 8475042db43SJérôme Glisse */ 8484dd845b5SAlistair Popple if (is_writable_device_private_entry(entry) && 8495042db43SJérôme Glisse is_cow_mapping(vm_flags)) { 8504dd845b5SAlistair Popple entry = make_readable_device_private_entry( 8514dd845b5SAlistair Popple swp_offset(entry)); 8525042db43SJérôme Glisse pte = swp_entry_to_pte(entry); 853c33c7948SRyan Roberts if (pte_swp_uffd_wp(orig_pte)) 854f45ec5ffSPeter Xu pte = pte_swp_mkuffd_wp(pte); 8555042db43SJérôme Glisse set_pte_at(src_mm, addr, src_pte, pte); 8565042db43SJérôme Glisse } 857b756a3b5SAlistair Popple } else if (is_device_exclusive_entry(entry)) { 858b756a3b5SAlistair Popple /* 859b756a3b5SAlistair Popple * Make device exclusive entries present by restoring the 860b756a3b5SAlistair Popple * original entry then copying as for a present pte. Device 861b756a3b5SAlistair Popple * exclusive entries currently only support private writable 862b756a3b5SAlistair Popple * (ie. COW) mappings. 863b756a3b5SAlistair Popple */ 864b756a3b5SAlistair Popple VM_BUG_ON(!is_cow_mapping(src_vma->vm_flags)); 865b756a3b5SAlistair Popple if (try_restore_exclusive_pte(src_pte, src_vma, addr)) 866b756a3b5SAlistair Popple return -EBUSY; 867b756a3b5SAlistair Popple return -ENOENT; 868c56d1b62SPeter Xu } else if (is_pte_marker_entry(entry)) { 869af19487fSAxel Rasmussen pte_marker marker = copy_pte_marker(entry, dst_vma); 870af19487fSAxel Rasmussen 871af19487fSAxel Rasmussen if (marker) 872af19487fSAxel Rasmussen set_pte_at(dst_mm, addr, dst_pte, 873af19487fSAxel Rasmussen make_pte_marker(marker)); 874c56d1b62SPeter Xu return 0; 8751da177e4SLinus Torvalds } 8768f34f1eaSPeter Xu if (!userfaultfd_wp(dst_vma)) 8778f34f1eaSPeter Xu pte = pte_swp_clear_uffd_wp(pte); 878df3a57d1SLinus Torvalds set_pte_at(dst_mm, addr, dst_pte, pte); 879df3a57d1SLinus Torvalds return 0; 8801da177e4SLinus Torvalds } 8811da177e4SLinus Torvalds 88270e806e4SPeter Xu /* 883b51ad4f8SDavid Hildenbrand * Copy a present and normal page. 88470e806e4SPeter Xu * 885b51ad4f8SDavid Hildenbrand * NOTE! The usual case is that this isn't required; 886b51ad4f8SDavid Hildenbrand * instead, the caller can just increase the page refcount 887b51ad4f8SDavid Hildenbrand * and re-use the pte the traditional way. 88870e806e4SPeter Xu * 88970e806e4SPeter Xu * And if we need a pre-allocated page but don't yet have 89070e806e4SPeter Xu * one, return a negative error to let the preallocation 89170e806e4SPeter Xu * code know so that it can do so outside the page table 89270e806e4SPeter Xu * lock. 89370e806e4SPeter Xu */ 89470e806e4SPeter Xu static inline int 895c78f4636SPeter Xu copy_present_page(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, 896c78f4636SPeter Xu pte_t *dst_pte, pte_t *src_pte, unsigned long addr, int *rss, 897edf50470SMatthew Wilcox (Oracle) struct folio **prealloc, struct page *page) 89870e806e4SPeter Xu { 899edf50470SMatthew Wilcox (Oracle) struct folio *new_folio; 900b51ad4f8SDavid Hildenbrand pte_t pte; 90170e806e4SPeter Xu 902edf50470SMatthew Wilcox (Oracle) new_folio = *prealloc; 903edf50470SMatthew Wilcox (Oracle) if (!new_folio) 90470e806e4SPeter Xu return -EAGAIN; 90570e806e4SPeter Xu 90670e806e4SPeter Xu /* 90770e806e4SPeter Xu * We have a prealloc page, all good! Take it 90870e806e4SPeter Xu * over and copy the page & arm it. 90970e806e4SPeter Xu */ 91070e806e4SPeter Xu *prealloc = NULL; 911edf50470SMatthew Wilcox (Oracle) copy_user_highpage(&new_folio->page, page, addr, src_vma); 912edf50470SMatthew Wilcox (Oracle) __folio_mark_uptodate(new_folio); 913edf50470SMatthew Wilcox (Oracle) folio_add_new_anon_rmap(new_folio, dst_vma, addr); 914edf50470SMatthew Wilcox (Oracle) folio_add_lru_vma(new_folio, dst_vma); 915edf50470SMatthew Wilcox (Oracle) rss[MM_ANONPAGES]++; 91670e806e4SPeter Xu 91770e806e4SPeter Xu /* All done, just insert the new page copy in the child */ 918edf50470SMatthew Wilcox (Oracle) pte = mk_pte(&new_folio->page, dst_vma->vm_page_prot); 919c78f4636SPeter Xu pte = maybe_mkwrite(pte_mkdirty(pte), dst_vma); 920c33c7948SRyan Roberts if (userfaultfd_pte_wp(dst_vma, ptep_get(src_pte))) 9218f34f1eaSPeter Xu /* Uffd-wp needs to be delivered to dest pte as well */ 922f1eb1bacSPeter Xu pte = pte_mkuffd_wp(pte); 923c78f4636SPeter Xu set_pte_at(dst_vma->vm_mm, addr, dst_pte, pte); 92470e806e4SPeter Xu return 0; 92570e806e4SPeter Xu } 92670e806e4SPeter Xu 92770e806e4SPeter Xu /* 92870e806e4SPeter Xu * Copy one pte. Returns 0 if succeeded, or -EAGAIN if one preallocated page 92970e806e4SPeter Xu * is required to copy this pte. 93070e806e4SPeter Xu */ 93170e806e4SPeter Xu static inline int 932c78f4636SPeter Xu copy_present_pte(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, 933c78f4636SPeter Xu pte_t *dst_pte, pte_t *src_pte, unsigned long addr, int *rss, 934edf50470SMatthew Wilcox (Oracle) struct folio **prealloc) 935df3a57d1SLinus Torvalds { 936c78f4636SPeter Xu struct mm_struct *src_mm = src_vma->vm_mm; 937c78f4636SPeter Xu unsigned long vm_flags = src_vma->vm_flags; 938c33c7948SRyan Roberts pte_t pte = ptep_get(src_pte); 939df3a57d1SLinus Torvalds struct page *page; 94014ddee41SMatthew Wilcox (Oracle) struct folio *folio; 941df3a57d1SLinus Torvalds 942c78f4636SPeter Xu page = vm_normal_page(src_vma, addr, pte); 94314ddee41SMatthew Wilcox (Oracle) if (page) 94414ddee41SMatthew Wilcox (Oracle) folio = page_folio(page); 94514ddee41SMatthew Wilcox (Oracle) if (page && folio_test_anon(folio)) { 946b51ad4f8SDavid Hildenbrand /* 947b51ad4f8SDavid Hildenbrand * If this page may have been pinned by the parent process, 948b51ad4f8SDavid Hildenbrand * copy the page immediately for the child so that we'll always 949b51ad4f8SDavid Hildenbrand * guarantee the pinned page won't be randomly replaced in the 950b51ad4f8SDavid Hildenbrand * future. 951b51ad4f8SDavid Hildenbrand */ 95214ddee41SMatthew Wilcox (Oracle) folio_get(folio); 953fb3d824dSDavid Hildenbrand if (unlikely(page_try_dup_anon_rmap(page, false, src_vma))) { 954fb3d824dSDavid Hildenbrand /* Page may be pinned, we have to copy. */ 95514ddee41SMatthew Wilcox (Oracle) folio_put(folio); 956b51ad4f8SDavid Hildenbrand return copy_present_page(dst_vma, src_vma, dst_pte, src_pte, 957b51ad4f8SDavid Hildenbrand addr, rss, prealloc, page); 958fb3d824dSDavid Hildenbrand } 959edf50470SMatthew Wilcox (Oracle) rss[MM_ANONPAGES]++; 960b51ad4f8SDavid Hildenbrand } else if (page) { 96114ddee41SMatthew Wilcox (Oracle) folio_get(folio); 962fb3d824dSDavid Hildenbrand page_dup_file_rmap(page, false); 963edf50470SMatthew Wilcox (Oracle) rss[mm_counter_file(page)]++; 96470e806e4SPeter Xu } 96570e806e4SPeter Xu 9661da177e4SLinus Torvalds /* 9671da177e4SLinus Torvalds * If it's a COW mapping, write protect it both 9681da177e4SLinus Torvalds * in the parent and the child 9691da177e4SLinus Torvalds */ 9701b2de5d0SLinus Torvalds if (is_cow_mapping(vm_flags) && pte_write(pte)) { 9711da177e4SLinus Torvalds ptep_set_wrprotect(src_mm, addr, src_pte); 9723dc90795SZachary Amsden pte = pte_wrprotect(pte); 9731da177e4SLinus Torvalds } 97414ddee41SMatthew Wilcox (Oracle) VM_BUG_ON(page && folio_test_anon(folio) && PageAnonExclusive(page)); 9751da177e4SLinus Torvalds 9761da177e4SLinus Torvalds /* 9771da177e4SLinus Torvalds * If it's a shared mapping, mark it clean in 9781da177e4SLinus Torvalds * the child 9791da177e4SLinus Torvalds */ 9801da177e4SLinus Torvalds if (vm_flags & VM_SHARED) 9811da177e4SLinus Torvalds pte = pte_mkclean(pte); 9821da177e4SLinus Torvalds pte = pte_mkold(pte); 9836aab341eSLinus Torvalds 9848f34f1eaSPeter Xu if (!userfaultfd_wp(dst_vma)) 985b569a176SPeter Xu pte = pte_clear_uffd_wp(pte); 986b569a176SPeter Xu 987c78f4636SPeter Xu set_pte_at(dst_vma->vm_mm, addr, dst_pte, pte); 98870e806e4SPeter Xu return 0; 9896aab341eSLinus Torvalds } 990ae859762SHugh Dickins 991edf50470SMatthew Wilcox (Oracle) static inline struct folio *page_copy_prealloc(struct mm_struct *src_mm, 992edf50470SMatthew Wilcox (Oracle) struct vm_area_struct *vma, unsigned long addr) 99370e806e4SPeter Xu { 994edf50470SMatthew Wilcox (Oracle) struct folio *new_folio; 99570e806e4SPeter Xu 996edf50470SMatthew Wilcox (Oracle) new_folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, vma, addr, false); 997edf50470SMatthew Wilcox (Oracle) if (!new_folio) 99870e806e4SPeter Xu return NULL; 99970e806e4SPeter Xu 1000edf50470SMatthew Wilcox (Oracle) if (mem_cgroup_charge(new_folio, src_mm, GFP_KERNEL)) { 1001edf50470SMatthew Wilcox (Oracle) folio_put(new_folio); 100270e806e4SPeter Xu return NULL; 100370e806e4SPeter Xu } 1004e601ded4SKefeng Wang folio_throttle_swaprate(new_folio, GFP_KERNEL); 100570e806e4SPeter Xu 1006edf50470SMatthew Wilcox (Oracle) return new_folio; 10071da177e4SLinus Torvalds } 10081da177e4SLinus Torvalds 1009c78f4636SPeter Xu static int 1010c78f4636SPeter Xu copy_pte_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, 1011c78f4636SPeter Xu pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr, 1012c78f4636SPeter Xu unsigned long end) 10131da177e4SLinus Torvalds { 1014c78f4636SPeter Xu struct mm_struct *dst_mm = dst_vma->vm_mm; 1015c78f4636SPeter Xu struct mm_struct *src_mm = src_vma->vm_mm; 1016c36987e2SDaisuke Nishimura pte_t *orig_src_pte, *orig_dst_pte; 10171da177e4SLinus Torvalds pte_t *src_pte, *dst_pte; 1018c33c7948SRyan Roberts pte_t ptent; 1019c74df32cSHugh Dickins spinlock_t *src_ptl, *dst_ptl; 102070e806e4SPeter Xu int progress, ret = 0; 1021d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 1022570a335bSHugh Dickins swp_entry_t entry = (swp_entry_t){0}; 1023edf50470SMatthew Wilcox (Oracle) struct folio *prealloc = NULL; 10241da177e4SLinus Torvalds 10251da177e4SLinus Torvalds again: 102670e806e4SPeter Xu progress = 0; 1027d559db08SKAMEZAWA Hiroyuki init_rss_vec(rss); 1028d559db08SKAMEZAWA Hiroyuki 10293db82b93SHugh Dickins /* 10303db82b93SHugh Dickins * copy_pmd_range()'s prior pmd_none_or_clear_bad(src_pmd), and the 10313db82b93SHugh Dickins * error handling here, assume that exclusive mmap_lock on dst and src 10323db82b93SHugh Dickins * protects anon from unexpected THP transitions; with shmem and file 10333db82b93SHugh Dickins * protected by mmap_lock-less collapse skipping areas with anon_vma 10343db82b93SHugh Dickins * (whereas vma_needs_copy() skips areas without anon_vma). A rework 10353db82b93SHugh Dickins * can remove such assumptions later, but this is good enough for now. 10363db82b93SHugh Dickins */ 1037c74df32cSHugh Dickins dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl); 103870e806e4SPeter Xu if (!dst_pte) { 103970e806e4SPeter Xu ret = -ENOMEM; 104070e806e4SPeter Xu goto out; 104170e806e4SPeter Xu } 10423db82b93SHugh Dickins src_pte = pte_offset_map_nolock(src_mm, src_pmd, addr, &src_ptl); 10433db82b93SHugh Dickins if (!src_pte) { 10443db82b93SHugh Dickins pte_unmap_unlock(dst_pte, dst_ptl); 10453db82b93SHugh Dickins /* ret == 0 */ 10463db82b93SHugh Dickins goto out; 10473db82b93SHugh Dickins } 1048f20dc5f7SIngo Molnar spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 1049c36987e2SDaisuke Nishimura orig_src_pte = src_pte; 1050c36987e2SDaisuke Nishimura orig_dst_pte = dst_pte; 10516606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 10521da177e4SLinus Torvalds 10531da177e4SLinus Torvalds do { 10541da177e4SLinus Torvalds /* 10551da177e4SLinus Torvalds * We are holding two locks at this point - either of them 10561da177e4SLinus Torvalds * could generate latencies in another task on another CPU. 10571da177e4SLinus Torvalds */ 1058e040f218SHugh Dickins if (progress >= 32) { 1059e040f218SHugh Dickins progress = 0; 1060e040f218SHugh Dickins if (need_resched() || 106195c354feSNick Piggin spin_needbreak(src_ptl) || spin_needbreak(dst_ptl)) 10621da177e4SLinus Torvalds break; 1063e040f218SHugh Dickins } 1064c33c7948SRyan Roberts ptent = ptep_get(src_pte); 1065c33c7948SRyan Roberts if (pte_none(ptent)) { 10661da177e4SLinus Torvalds progress++; 10671da177e4SLinus Torvalds continue; 10681da177e4SLinus Torvalds } 1069c33c7948SRyan Roberts if (unlikely(!pte_present(ptent))) { 10709a5cc85cSAlistair Popple ret = copy_nonpresent_pte(dst_mm, src_mm, 107179a1971cSLinus Torvalds dst_pte, src_pte, 10728f34f1eaSPeter Xu dst_vma, src_vma, 10738f34f1eaSPeter Xu addr, rss); 10749a5cc85cSAlistair Popple if (ret == -EIO) { 1075c33c7948SRyan Roberts entry = pte_to_swp_entry(ptep_get(src_pte)); 1076570a335bSHugh Dickins break; 1077b756a3b5SAlistair Popple } else if (ret == -EBUSY) { 1078b756a3b5SAlistair Popple break; 1079b756a3b5SAlistair Popple } else if (!ret) { 10801da177e4SLinus Torvalds progress += 8; 108179a1971cSLinus Torvalds continue; 108279a1971cSLinus Torvalds } 1083b756a3b5SAlistair Popple 1084b756a3b5SAlistair Popple /* 1085b756a3b5SAlistair Popple * Device exclusive entry restored, continue by copying 1086b756a3b5SAlistair Popple * the now present pte. 1087b756a3b5SAlistair Popple */ 1088b756a3b5SAlistair Popple WARN_ON_ONCE(ret != -ENOENT); 1089b756a3b5SAlistair Popple } 109070e806e4SPeter Xu /* copy_present_pte() will clear `*prealloc' if consumed */ 1091c78f4636SPeter Xu ret = copy_present_pte(dst_vma, src_vma, dst_pte, src_pte, 1092c78f4636SPeter Xu addr, rss, &prealloc); 109370e806e4SPeter Xu /* 109470e806e4SPeter Xu * If we need a pre-allocated page for this pte, drop the 109570e806e4SPeter Xu * locks, allocate, and try again. 109670e806e4SPeter Xu */ 109770e806e4SPeter Xu if (unlikely(ret == -EAGAIN)) 109870e806e4SPeter Xu break; 109970e806e4SPeter Xu if (unlikely(prealloc)) { 110070e806e4SPeter Xu /* 110170e806e4SPeter Xu * pre-alloc page cannot be reused by next time so as 110270e806e4SPeter Xu * to strictly follow mempolicy (e.g., alloc_page_vma() 110370e806e4SPeter Xu * will allocate page according to address). This 110470e806e4SPeter Xu * could only happen if one pinned pte changed. 110570e806e4SPeter Xu */ 1106edf50470SMatthew Wilcox (Oracle) folio_put(prealloc); 110770e806e4SPeter Xu prealloc = NULL; 110870e806e4SPeter Xu } 110979a1971cSLinus Torvalds progress += 8; 11101da177e4SLinus Torvalds } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end); 11111da177e4SLinus Torvalds 11126606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 11133db82b93SHugh Dickins pte_unmap_unlock(orig_src_pte, src_ptl); 1114d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(dst_mm, rss); 1115c36987e2SDaisuke Nishimura pte_unmap_unlock(orig_dst_pte, dst_ptl); 1116c74df32cSHugh Dickins cond_resched(); 1117570a335bSHugh Dickins 11189a5cc85cSAlistair Popple if (ret == -EIO) { 11199a5cc85cSAlistair Popple VM_WARN_ON_ONCE(!entry.val); 112070e806e4SPeter Xu if (add_swap_count_continuation(entry, GFP_KERNEL) < 0) { 112170e806e4SPeter Xu ret = -ENOMEM; 112270e806e4SPeter Xu goto out; 112370e806e4SPeter Xu } 112470e806e4SPeter Xu entry.val = 0; 1125b756a3b5SAlistair Popple } else if (ret == -EBUSY) { 1126b756a3b5SAlistair Popple goto out; 11279a5cc85cSAlistair Popple } else if (ret == -EAGAIN) { 1128c78f4636SPeter Xu prealloc = page_copy_prealloc(src_mm, src_vma, addr); 112970e806e4SPeter Xu if (!prealloc) 1130570a335bSHugh Dickins return -ENOMEM; 11319a5cc85cSAlistair Popple } else if (ret) { 11329a5cc85cSAlistair Popple VM_WARN_ON_ONCE(1); 11339a5cc85cSAlistair Popple } 11349a5cc85cSAlistair Popple 113570e806e4SPeter Xu /* We've captured and resolved the error. Reset, try again. */ 113670e806e4SPeter Xu ret = 0; 11379a5cc85cSAlistair Popple 11381da177e4SLinus Torvalds if (addr != end) 11391da177e4SLinus Torvalds goto again; 114070e806e4SPeter Xu out: 114170e806e4SPeter Xu if (unlikely(prealloc)) 1142edf50470SMatthew Wilcox (Oracle) folio_put(prealloc); 114370e806e4SPeter Xu return ret; 11441da177e4SLinus Torvalds } 11451da177e4SLinus Torvalds 1146c78f4636SPeter Xu static inline int 1147c78f4636SPeter Xu copy_pmd_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, 1148c78f4636SPeter Xu pud_t *dst_pud, pud_t *src_pud, unsigned long addr, 1149c78f4636SPeter Xu unsigned long end) 11501da177e4SLinus Torvalds { 1151c78f4636SPeter Xu struct mm_struct *dst_mm = dst_vma->vm_mm; 1152c78f4636SPeter Xu struct mm_struct *src_mm = src_vma->vm_mm; 11531da177e4SLinus Torvalds pmd_t *src_pmd, *dst_pmd; 11541da177e4SLinus Torvalds unsigned long next; 11551da177e4SLinus Torvalds 11561da177e4SLinus Torvalds dst_pmd = pmd_alloc(dst_mm, dst_pud, addr); 11571da177e4SLinus Torvalds if (!dst_pmd) 11581da177e4SLinus Torvalds return -ENOMEM; 11591da177e4SLinus Torvalds src_pmd = pmd_offset(src_pud, addr); 11601da177e4SLinus Torvalds do { 11611da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 116284c3fc4eSZi Yan if (is_swap_pmd(*src_pmd) || pmd_trans_huge(*src_pmd) 116384c3fc4eSZi Yan || pmd_devmap(*src_pmd)) { 116471e3aac0SAndrea Arcangeli int err; 1165c78f4636SPeter Xu VM_BUG_ON_VMA(next-addr != HPAGE_PMD_SIZE, src_vma); 11668f34f1eaSPeter Xu err = copy_huge_pmd(dst_mm, src_mm, dst_pmd, src_pmd, 11678f34f1eaSPeter Xu addr, dst_vma, src_vma); 116871e3aac0SAndrea Arcangeli if (err == -ENOMEM) 116971e3aac0SAndrea Arcangeli return -ENOMEM; 117071e3aac0SAndrea Arcangeli if (!err) 117171e3aac0SAndrea Arcangeli continue; 117271e3aac0SAndrea Arcangeli /* fall through */ 117371e3aac0SAndrea Arcangeli } 11741da177e4SLinus Torvalds if (pmd_none_or_clear_bad(src_pmd)) 11751da177e4SLinus Torvalds continue; 1176c78f4636SPeter Xu if (copy_pte_range(dst_vma, src_vma, dst_pmd, src_pmd, 1177c78f4636SPeter Xu addr, next)) 11781da177e4SLinus Torvalds return -ENOMEM; 11791da177e4SLinus Torvalds } while (dst_pmd++, src_pmd++, addr = next, addr != end); 11801da177e4SLinus Torvalds return 0; 11811da177e4SLinus Torvalds } 11821da177e4SLinus Torvalds 1183c78f4636SPeter Xu static inline int 1184c78f4636SPeter Xu copy_pud_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, 1185c78f4636SPeter Xu p4d_t *dst_p4d, p4d_t *src_p4d, unsigned long addr, 1186c78f4636SPeter Xu unsigned long end) 11871da177e4SLinus Torvalds { 1188c78f4636SPeter Xu struct mm_struct *dst_mm = dst_vma->vm_mm; 1189c78f4636SPeter Xu struct mm_struct *src_mm = src_vma->vm_mm; 11901da177e4SLinus Torvalds pud_t *src_pud, *dst_pud; 11911da177e4SLinus Torvalds unsigned long next; 11921da177e4SLinus Torvalds 1193c2febafcSKirill A. Shutemov dst_pud = pud_alloc(dst_mm, dst_p4d, addr); 11941da177e4SLinus Torvalds if (!dst_pud) 11951da177e4SLinus Torvalds return -ENOMEM; 1196c2febafcSKirill A. Shutemov src_pud = pud_offset(src_p4d, addr); 11971da177e4SLinus Torvalds do { 11981da177e4SLinus Torvalds next = pud_addr_end(addr, end); 1199a00cc7d9SMatthew Wilcox if (pud_trans_huge(*src_pud) || pud_devmap(*src_pud)) { 1200a00cc7d9SMatthew Wilcox int err; 1201a00cc7d9SMatthew Wilcox 1202c78f4636SPeter Xu VM_BUG_ON_VMA(next-addr != HPAGE_PUD_SIZE, src_vma); 1203a00cc7d9SMatthew Wilcox err = copy_huge_pud(dst_mm, src_mm, 1204c78f4636SPeter Xu dst_pud, src_pud, addr, src_vma); 1205a00cc7d9SMatthew Wilcox if (err == -ENOMEM) 1206a00cc7d9SMatthew Wilcox return -ENOMEM; 1207a00cc7d9SMatthew Wilcox if (!err) 1208a00cc7d9SMatthew Wilcox continue; 1209a00cc7d9SMatthew Wilcox /* fall through */ 1210a00cc7d9SMatthew Wilcox } 12111da177e4SLinus Torvalds if (pud_none_or_clear_bad(src_pud)) 12121da177e4SLinus Torvalds continue; 1213c78f4636SPeter Xu if (copy_pmd_range(dst_vma, src_vma, dst_pud, src_pud, 1214c78f4636SPeter Xu addr, next)) 12151da177e4SLinus Torvalds return -ENOMEM; 12161da177e4SLinus Torvalds } while (dst_pud++, src_pud++, addr = next, addr != end); 12171da177e4SLinus Torvalds return 0; 12181da177e4SLinus Torvalds } 12191da177e4SLinus Torvalds 1220c78f4636SPeter Xu static inline int 1221c78f4636SPeter Xu copy_p4d_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, 1222c78f4636SPeter Xu pgd_t *dst_pgd, pgd_t *src_pgd, unsigned long addr, 1223c78f4636SPeter Xu unsigned long end) 1224c2febafcSKirill A. Shutemov { 1225c78f4636SPeter Xu struct mm_struct *dst_mm = dst_vma->vm_mm; 1226c2febafcSKirill A. Shutemov p4d_t *src_p4d, *dst_p4d; 1227c2febafcSKirill A. Shutemov unsigned long next; 1228c2febafcSKirill A. Shutemov 1229c2febafcSKirill A. Shutemov dst_p4d = p4d_alloc(dst_mm, dst_pgd, addr); 1230c2febafcSKirill A. Shutemov if (!dst_p4d) 1231c2febafcSKirill A. Shutemov return -ENOMEM; 1232c2febafcSKirill A. Shutemov src_p4d = p4d_offset(src_pgd, addr); 1233c2febafcSKirill A. Shutemov do { 1234c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 1235c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(src_p4d)) 1236c2febafcSKirill A. Shutemov continue; 1237c78f4636SPeter Xu if (copy_pud_range(dst_vma, src_vma, dst_p4d, src_p4d, 1238c78f4636SPeter Xu addr, next)) 1239c2febafcSKirill A. Shutemov return -ENOMEM; 1240c2febafcSKirill A. Shutemov } while (dst_p4d++, src_p4d++, addr = next, addr != end); 1241c2febafcSKirill A. Shutemov return 0; 1242c2febafcSKirill A. Shutemov } 1243c2febafcSKirill A. Shutemov 1244c56d1b62SPeter Xu /* 1245c56d1b62SPeter Xu * Return true if the vma needs to copy the pgtable during this fork(). Return 1246c56d1b62SPeter Xu * false when we can speed up fork() by allowing lazy page faults later until 1247c56d1b62SPeter Xu * when the child accesses the memory range. 1248c56d1b62SPeter Xu */ 1249bc70fbf2SPeter Xu static bool 1250c56d1b62SPeter Xu vma_needs_copy(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) 1251c56d1b62SPeter Xu { 1252c56d1b62SPeter Xu /* 1253c56d1b62SPeter Xu * Always copy pgtables when dst_vma has uffd-wp enabled even if it's 1254c56d1b62SPeter Xu * file-backed (e.g. shmem). Because when uffd-wp is enabled, pgtable 1255c56d1b62SPeter Xu * contains uffd-wp protection information, that's something we can't 1256c56d1b62SPeter Xu * retrieve from page cache, and skip copying will lose those info. 1257c56d1b62SPeter Xu */ 1258c56d1b62SPeter Xu if (userfaultfd_wp(dst_vma)) 1259c56d1b62SPeter Xu return true; 1260c56d1b62SPeter Xu 1261bcd51a3cSMike Kravetz if (src_vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)) 1262c56d1b62SPeter Xu return true; 1263c56d1b62SPeter Xu 1264c56d1b62SPeter Xu if (src_vma->anon_vma) 1265c56d1b62SPeter Xu return true; 1266c56d1b62SPeter Xu 1267c56d1b62SPeter Xu /* 1268c56d1b62SPeter Xu * Don't copy ptes where a page fault will fill them correctly. Fork 1269c56d1b62SPeter Xu * becomes much lighter when there are big shared or private readonly 1270c56d1b62SPeter Xu * mappings. The tradeoff is that copy_page_range is more efficient 1271c56d1b62SPeter Xu * than faulting. 1272c56d1b62SPeter Xu */ 1273c56d1b62SPeter Xu return false; 1274c56d1b62SPeter Xu } 1275c56d1b62SPeter Xu 1276c78f4636SPeter Xu int 1277c78f4636SPeter Xu copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) 12781da177e4SLinus Torvalds { 12791da177e4SLinus Torvalds pgd_t *src_pgd, *dst_pgd; 12801da177e4SLinus Torvalds unsigned long next; 1281c78f4636SPeter Xu unsigned long addr = src_vma->vm_start; 1282c78f4636SPeter Xu unsigned long end = src_vma->vm_end; 1283c78f4636SPeter Xu struct mm_struct *dst_mm = dst_vma->vm_mm; 1284c78f4636SPeter Xu struct mm_struct *src_mm = src_vma->vm_mm; 1285ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 12862ec74c3eSSagi Grimberg bool is_cow; 1287cddb8a5cSAndrea Arcangeli int ret; 12881da177e4SLinus Torvalds 1289c56d1b62SPeter Xu if (!vma_needs_copy(dst_vma, src_vma)) 1290d992895bSNick Piggin return 0; 1291d992895bSNick Piggin 1292c78f4636SPeter Xu if (is_vm_hugetlb_page(src_vma)) 1293bc70fbf2SPeter Xu return copy_hugetlb_page_range(dst_mm, src_mm, dst_vma, src_vma); 12941da177e4SLinus Torvalds 1295c78f4636SPeter Xu if (unlikely(src_vma->vm_flags & VM_PFNMAP)) { 12962ab64037Svenkatesh.pallipadi@intel.com /* 12972ab64037Svenkatesh.pallipadi@intel.com * We do not free on error cases below as remove_vma 12982ab64037Svenkatesh.pallipadi@intel.com * gets called on error from higher level routine 12992ab64037Svenkatesh.pallipadi@intel.com */ 1300c78f4636SPeter Xu ret = track_pfn_copy(src_vma); 13012ab64037Svenkatesh.pallipadi@intel.com if (ret) 13022ab64037Svenkatesh.pallipadi@intel.com return ret; 13032ab64037Svenkatesh.pallipadi@intel.com } 13042ab64037Svenkatesh.pallipadi@intel.com 1305cddb8a5cSAndrea Arcangeli /* 1306cddb8a5cSAndrea Arcangeli * We need to invalidate the secondary MMU mappings only when 1307cddb8a5cSAndrea Arcangeli * there could be a permission downgrade on the ptes of the 1308cddb8a5cSAndrea Arcangeli * parent mm. And a permission downgrade will only happen if 1309cddb8a5cSAndrea Arcangeli * is_cow_mapping() returns true. 1310cddb8a5cSAndrea Arcangeli */ 1311c78f4636SPeter Xu is_cow = is_cow_mapping(src_vma->vm_flags); 1312ac46d4f3SJérôme Glisse 1313ac46d4f3SJérôme Glisse if (is_cow) { 13147269f999SJérôme Glisse mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_PAGE, 13157d4a8be0SAlistair Popple 0, src_mm, addr, end); 1316ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 131757efa1feSJason Gunthorpe /* 131857efa1feSJason Gunthorpe * Disabling preemption is not needed for the write side, as 131957efa1feSJason Gunthorpe * the read side doesn't spin, but goes to the mmap_lock. 132057efa1feSJason Gunthorpe * 132157efa1feSJason Gunthorpe * Use the raw variant of the seqcount_t write API to avoid 132257efa1feSJason Gunthorpe * lockdep complaining about preemptibility. 132357efa1feSJason Gunthorpe */ 1324e727bfd5SSuren Baghdasaryan vma_assert_write_locked(src_vma); 132557efa1feSJason Gunthorpe raw_write_seqcount_begin(&src_mm->write_protect_seq); 1326ac46d4f3SJérôme Glisse } 1327cddb8a5cSAndrea Arcangeli 1328cddb8a5cSAndrea Arcangeli ret = 0; 13291da177e4SLinus Torvalds dst_pgd = pgd_offset(dst_mm, addr); 13301da177e4SLinus Torvalds src_pgd = pgd_offset(src_mm, addr); 13311da177e4SLinus Torvalds do { 13321da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 13331da177e4SLinus Torvalds if (pgd_none_or_clear_bad(src_pgd)) 13341da177e4SLinus Torvalds continue; 1335c78f4636SPeter Xu if (unlikely(copy_p4d_range(dst_vma, src_vma, dst_pgd, src_pgd, 1336c78f4636SPeter Xu addr, next))) { 1337d155df53SMa Wupeng untrack_pfn_clear(dst_vma); 1338cddb8a5cSAndrea Arcangeli ret = -ENOMEM; 1339cddb8a5cSAndrea Arcangeli break; 1340cddb8a5cSAndrea Arcangeli } 13411da177e4SLinus Torvalds } while (dst_pgd++, src_pgd++, addr = next, addr != end); 1342cddb8a5cSAndrea Arcangeli 134357efa1feSJason Gunthorpe if (is_cow) { 134457efa1feSJason Gunthorpe raw_write_seqcount_end(&src_mm->write_protect_seq); 1345ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 134657efa1feSJason Gunthorpe } 1347cddb8a5cSAndrea Arcangeli return ret; 13481da177e4SLinus Torvalds } 13491da177e4SLinus Torvalds 13505abfd71dSPeter Xu /* Whether we should zap all COWed (private) pages too */ 13515abfd71dSPeter Xu static inline bool should_zap_cows(struct zap_details *details) 13525abfd71dSPeter Xu { 13535abfd71dSPeter Xu /* By default, zap all pages */ 13545abfd71dSPeter Xu if (!details) 13555abfd71dSPeter Xu return true; 13565abfd71dSPeter Xu 13575abfd71dSPeter Xu /* Or, we zap COWed pages only if the caller wants to */ 13582e148f1eSPeter Xu return details->even_cows; 13595abfd71dSPeter Xu } 13605abfd71dSPeter Xu 13612e148f1eSPeter Xu /* Decides whether we should zap this page with the page pointer specified */ 1362254ab940SPeter Xu static inline bool should_zap_page(struct zap_details *details, struct page *page) 13633506659eSMatthew Wilcox (Oracle) { 13645abfd71dSPeter Xu /* If we can make a decision without *page.. */ 13655abfd71dSPeter Xu if (should_zap_cows(details)) 1366254ab940SPeter Xu return true; 13673506659eSMatthew Wilcox (Oracle) 13685abfd71dSPeter Xu /* E.g. the caller passes NULL for the case of a zero page */ 13695abfd71dSPeter Xu if (!page) 1370254ab940SPeter Xu return true; 13715abfd71dSPeter Xu 13722e148f1eSPeter Xu /* Otherwise we should only zap non-anon pages */ 13732e148f1eSPeter Xu return !PageAnon(page); 13743506659eSMatthew Wilcox (Oracle) } 13753506659eSMatthew Wilcox (Oracle) 1376999dad82SPeter Xu static inline bool zap_drop_file_uffd_wp(struct zap_details *details) 1377999dad82SPeter Xu { 1378999dad82SPeter Xu if (!details) 1379999dad82SPeter Xu return false; 1380999dad82SPeter Xu 1381999dad82SPeter Xu return details->zap_flags & ZAP_FLAG_DROP_MARKER; 1382999dad82SPeter Xu } 1383999dad82SPeter Xu 1384999dad82SPeter Xu /* 1385999dad82SPeter Xu * This function makes sure that we'll replace the none pte with an uffd-wp 1386999dad82SPeter Xu * swap special pte marker when necessary. Must be with the pgtable lock held. 1387999dad82SPeter Xu */ 1388999dad82SPeter Xu static inline void 1389999dad82SPeter Xu zap_install_uffd_wp_if_needed(struct vm_area_struct *vma, 1390999dad82SPeter Xu unsigned long addr, pte_t *pte, 1391999dad82SPeter Xu struct zap_details *details, pte_t pteval) 1392999dad82SPeter Xu { 13932bad466cSPeter Xu /* Zap on anonymous always means dropping everything */ 13942bad466cSPeter Xu if (vma_is_anonymous(vma)) 13952bad466cSPeter Xu return; 13962bad466cSPeter Xu 1397999dad82SPeter Xu if (zap_drop_file_uffd_wp(details)) 1398999dad82SPeter Xu return; 1399999dad82SPeter Xu 1400999dad82SPeter Xu pte_install_uffd_wp_if_needed(vma, addr, pte, pteval); 1401999dad82SPeter Xu } 1402999dad82SPeter Xu 140351c6f666SRobin Holt static unsigned long zap_pte_range(struct mmu_gather *tlb, 1404b5810039SNick Piggin struct vm_area_struct *vma, pmd_t *pmd, 14051da177e4SLinus Torvalds unsigned long addr, unsigned long end, 140697a89413SPeter Zijlstra struct zap_details *details) 14071da177e4SLinus Torvalds { 1408b5810039SNick Piggin struct mm_struct *mm = tlb->mm; 1409d16dfc55SPeter Zijlstra int force_flush = 0; 1410d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 141197a89413SPeter Zijlstra spinlock_t *ptl; 14125f1a1907SSteven Rostedt pte_t *start_pte; 141397a89413SPeter Zijlstra pte_t *pte; 14148a5f14a2SKirill A. Shutemov swp_entry_t entry; 1415d559db08SKAMEZAWA Hiroyuki 1416ed6a7935SPeter Zijlstra tlb_change_page_size(tlb, PAGE_SIZE); 1417e303297eSPeter Zijlstra init_rss_vec(rss); 14183db82b93SHugh Dickins start_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl); 14193db82b93SHugh Dickins if (!pte) 14203db82b93SHugh Dickins return addr; 14213db82b93SHugh Dickins 14223ea27719SMel Gorman flush_tlb_batched_pending(mm); 14236606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 14241da177e4SLinus Torvalds do { 1425c33c7948SRyan Roberts pte_t ptent = ptep_get(pte); 14268018db85SPeter Xu struct page *page; 14278018db85SPeter Xu 1428166f61b9STobin C Harding if (pte_none(ptent)) 14291da177e4SLinus Torvalds continue; 143051c6f666SRobin Holt 14317b167b68SMinchan Kim if (need_resched()) 14327b167b68SMinchan Kim break; 14337b167b68SMinchan Kim 14346f5e6b9eSHugh Dickins if (pte_present(ptent)) { 14355df397deSLinus Torvalds unsigned int delay_rmap; 14365df397deSLinus Torvalds 143725b2995aSChristoph Hellwig page = vm_normal_page(vma, addr, ptent); 1438254ab940SPeter Xu if (unlikely(!should_zap_page(details, page))) 14391da177e4SLinus Torvalds continue; 1440b5810039SNick Piggin ptent = ptep_get_and_clear_full(mm, addr, pte, 1441a600388dSZachary Amsden tlb->fullmm); 1442e5136e87SRick Edgecombe arch_check_zapped_pte(vma, ptent); 14431da177e4SLinus Torvalds tlb_remove_tlb_entry(tlb, pte, addr); 1444999dad82SPeter Xu zap_install_uffd_wp_if_needed(vma, addr, pte, details, 1445999dad82SPeter Xu ptent); 1446e2942062Sxu xin if (unlikely(!page)) { 14476080d19fSxu xin ksm_might_unmap_zero_page(mm, ptent); 14481da177e4SLinus Torvalds continue; 1449e2942062Sxu xin } 1450eca56ff9SJerome Marchand 14515df397deSLinus Torvalds delay_rmap = 0; 1452eca56ff9SJerome Marchand if (!PageAnon(page)) { 14531cf35d47SLinus Torvalds if (pte_dirty(ptent)) { 14546237bcd9SHugh Dickins set_page_dirty(page); 14555df397deSLinus Torvalds if (tlb_delay_rmap(tlb)) { 14565df397deSLinus Torvalds delay_rmap = 1; 14575df397deSLinus Torvalds force_flush = 1; 14585df397deSLinus Torvalds } 14591cf35d47SLinus Torvalds } 14608788f678SYu Zhao if (pte_young(ptent) && likely(vma_has_recency(vma))) 1461bf3f3bc5SNick Piggin mark_page_accessed(page); 14626237bcd9SHugh Dickins } 1463eca56ff9SJerome Marchand rss[mm_counter(page)]--; 14645df397deSLinus Torvalds if (!delay_rmap) { 1465cea86fe2SHugh Dickins page_remove_rmap(page, vma, false); 14663dc14741SHugh Dickins if (unlikely(page_mapcount(page) < 0)) 14673dc14741SHugh Dickins print_bad_pte(vma, addr, ptent, page); 14685df397deSLinus Torvalds } 14695df397deSLinus Torvalds if (unlikely(__tlb_remove_page(tlb, page, delay_rmap))) { 14701cf35d47SLinus Torvalds force_flush = 1; 1471ce9ec37bSWill Deacon addr += PAGE_SIZE; 1472d16dfc55SPeter Zijlstra break; 14731cf35d47SLinus Torvalds } 14741da177e4SLinus Torvalds continue; 14751da177e4SLinus Torvalds } 14765042db43SJérôme Glisse 14775042db43SJérôme Glisse entry = pte_to_swp_entry(ptent); 1478b756a3b5SAlistair Popple if (is_device_private_entry(entry) || 1479b756a3b5SAlistair Popple is_device_exclusive_entry(entry)) { 14808018db85SPeter Xu page = pfn_swap_entry_to_page(entry); 1481254ab940SPeter Xu if (unlikely(!should_zap_page(details, page))) 14825042db43SJérôme Glisse continue; 1483999dad82SPeter Xu /* 1484999dad82SPeter Xu * Both device private/exclusive mappings should only 1485999dad82SPeter Xu * work with anonymous page so far, so we don't need to 1486999dad82SPeter Xu * consider uffd-wp bit when zap. For more information, 1487999dad82SPeter Xu * see zap_install_uffd_wp_if_needed(). 1488999dad82SPeter Xu */ 1489999dad82SPeter Xu WARN_ON_ONCE(!vma_is_anonymous(vma)); 14905042db43SJérôme Glisse rss[mm_counter(page)]--; 1491b756a3b5SAlistair Popple if (is_device_private_entry(entry)) 1492cea86fe2SHugh Dickins page_remove_rmap(page, vma, false); 14935042db43SJérôme Glisse put_page(page); 14948018db85SPeter Xu } else if (!non_swap_entry(entry)) { 14955abfd71dSPeter Xu /* Genuine swap entry, hence a private anon page */ 14965abfd71dSPeter Xu if (!should_zap_cows(details)) 14971da177e4SLinus Torvalds continue; 1498b084d435SKAMEZAWA Hiroyuki rss[MM_SWAPENTS]--; 14998018db85SPeter Xu if (unlikely(!free_swap_and_cache(entry))) 15008018db85SPeter Xu print_bad_pte(vma, addr, ptent, NULL); 15015abfd71dSPeter Xu } else if (is_migration_entry(entry)) { 1502af5cdaf8SAlistair Popple page = pfn_swap_entry_to_page(entry); 1503254ab940SPeter Xu if (!should_zap_page(details, page)) 15045abfd71dSPeter Xu continue; 1505eca56ff9SJerome Marchand rss[mm_counter(page)]--; 1506999dad82SPeter Xu } else if (pte_marker_entry_uffd_wp(entry)) { 15072bad466cSPeter Xu /* 15082bad466cSPeter Xu * For anon: always drop the marker; for file: only 15092bad466cSPeter Xu * drop the marker if explicitly requested. 15102bad466cSPeter Xu */ 15112bad466cSPeter Xu if (!vma_is_anonymous(vma) && 15122bad466cSPeter Xu !zap_drop_file_uffd_wp(details)) 1513999dad82SPeter Xu continue; 15149f186f9eSMiaohe Lin } else if (is_hwpoison_entry(entry) || 1515af19487fSAxel Rasmussen is_poisoned_swp_entry(entry)) { 15165abfd71dSPeter Xu if (!should_zap_cows(details)) 15175abfd71dSPeter Xu continue; 15185abfd71dSPeter Xu } else { 15195abfd71dSPeter Xu /* We should have covered all the swap entry types */ 15205abfd71dSPeter Xu WARN_ON_ONCE(1); 15219f9f1acdSKonstantin Khlebnikov } 15229888a1caSZachary Amsden pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); 1523999dad82SPeter Xu zap_install_uffd_wp_if_needed(vma, addr, pte, details, ptent); 152497a89413SPeter Zijlstra } while (pte++, addr += PAGE_SIZE, addr != end); 1525ae859762SHugh Dickins 1526d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(mm, rss); 15276606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 152851c6f666SRobin Holt 15291cf35d47SLinus Torvalds /* Do the actual TLB flush before dropping ptl */ 15305df397deSLinus Torvalds if (force_flush) { 15311cf35d47SLinus Torvalds tlb_flush_mmu_tlbonly(tlb); 15325df397deSLinus Torvalds tlb_flush_rmaps(tlb, vma); 15335df397deSLinus Torvalds } 15341cf35d47SLinus Torvalds pte_unmap_unlock(start_pte, ptl); 15351cf35d47SLinus Torvalds 15361cf35d47SLinus Torvalds /* 15371cf35d47SLinus Torvalds * If we forced a TLB flush (either due to running out of 15381cf35d47SLinus Torvalds * batch buffers or because we needed to flush dirty TLB 15391cf35d47SLinus Torvalds * entries before releasing the ptl), free the batched 15403db82b93SHugh Dickins * memory too. Come back again if we didn't do everything. 15411cf35d47SLinus Torvalds */ 15423db82b93SHugh Dickins if (force_flush) 1543fa0aafb8SPeter Zijlstra tlb_flush_mmu(tlb); 1544d16dfc55SPeter Zijlstra 154551c6f666SRobin Holt return addr; 15461da177e4SLinus Torvalds } 15471da177e4SLinus Torvalds 154851c6f666SRobin Holt static inline unsigned long zap_pmd_range(struct mmu_gather *tlb, 1549b5810039SNick Piggin struct vm_area_struct *vma, pud_t *pud, 15501da177e4SLinus Torvalds unsigned long addr, unsigned long end, 155197a89413SPeter Zijlstra struct zap_details *details) 15521da177e4SLinus Torvalds { 15531da177e4SLinus Torvalds pmd_t *pmd; 15541da177e4SLinus Torvalds unsigned long next; 15551da177e4SLinus Torvalds 15561da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 15571da177e4SLinus Torvalds do { 15581da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 155984c3fc4eSZi Yan if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) { 156053406ed1SHugh Dickins if (next - addr != HPAGE_PMD_SIZE) 1561fd60775aSDavid Rientjes __split_huge_pmd(vma, pmd, addr, false, NULL); 15623db82b93SHugh Dickins else if (zap_huge_pmd(tlb, vma, pmd, addr)) { 15633db82b93SHugh Dickins addr = next; 15643db82b93SHugh Dickins continue; 15653db82b93SHugh Dickins } 156671e3aac0SAndrea Arcangeli /* fall through */ 15673506659eSMatthew Wilcox (Oracle) } else if (details && details->single_folio && 15683506659eSMatthew Wilcox (Oracle) folio_test_pmd_mappable(details->single_folio) && 156922061a1fSHugh Dickins next - addr == HPAGE_PMD_SIZE && pmd_none(*pmd)) { 157022061a1fSHugh Dickins spinlock_t *ptl = pmd_lock(tlb->mm, pmd); 157122061a1fSHugh Dickins /* 157222061a1fSHugh Dickins * Take and drop THP pmd lock so that we cannot return 157322061a1fSHugh Dickins * prematurely, while zap_huge_pmd() has cleared *pmd, 157422061a1fSHugh Dickins * but not yet decremented compound_mapcount(). 157522061a1fSHugh Dickins */ 157622061a1fSHugh Dickins spin_unlock(ptl); 157771e3aac0SAndrea Arcangeli } 15783db82b93SHugh Dickins if (pmd_none(*pmd)) { 15793db82b93SHugh Dickins addr = next; 15803db82b93SHugh Dickins continue; 15813db82b93SHugh Dickins } 15823db82b93SHugh Dickins addr = zap_pte_range(tlb, vma, pmd, addr, next, details); 15833db82b93SHugh Dickins if (addr != next) 15843db82b93SHugh Dickins pmd--; 15853db82b93SHugh Dickins } while (pmd++, cond_resched(), addr != end); 158651c6f666SRobin Holt 158751c6f666SRobin Holt return addr; 15881da177e4SLinus Torvalds } 15891da177e4SLinus Torvalds 159051c6f666SRobin Holt static inline unsigned long zap_pud_range(struct mmu_gather *tlb, 1591c2febafcSKirill A. Shutemov struct vm_area_struct *vma, p4d_t *p4d, 15921da177e4SLinus Torvalds unsigned long addr, unsigned long end, 159397a89413SPeter Zijlstra struct zap_details *details) 15941da177e4SLinus Torvalds { 15951da177e4SLinus Torvalds pud_t *pud; 15961da177e4SLinus Torvalds unsigned long next; 15971da177e4SLinus Torvalds 1598c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 15991da177e4SLinus Torvalds do { 16001da177e4SLinus Torvalds next = pud_addr_end(addr, end); 1601a00cc7d9SMatthew Wilcox if (pud_trans_huge(*pud) || pud_devmap(*pud)) { 1602a00cc7d9SMatthew Wilcox if (next - addr != HPAGE_PUD_SIZE) { 160342fc5414SMichel Lespinasse mmap_assert_locked(tlb->mm); 1604a00cc7d9SMatthew Wilcox split_huge_pud(vma, pud, addr); 1605a00cc7d9SMatthew Wilcox } else if (zap_huge_pud(tlb, vma, pud, addr)) 1606a00cc7d9SMatthew Wilcox goto next; 1607a00cc7d9SMatthew Wilcox /* fall through */ 1608a00cc7d9SMatthew Wilcox } 160997a89413SPeter Zijlstra if (pud_none_or_clear_bad(pud)) 16101da177e4SLinus Torvalds continue; 161197a89413SPeter Zijlstra next = zap_pmd_range(tlb, vma, pud, addr, next, details); 1612a00cc7d9SMatthew Wilcox next: 1613a00cc7d9SMatthew Wilcox cond_resched(); 161497a89413SPeter Zijlstra } while (pud++, addr = next, addr != end); 161551c6f666SRobin Holt 161651c6f666SRobin Holt return addr; 16171da177e4SLinus Torvalds } 16181da177e4SLinus Torvalds 1619c2febafcSKirill A. Shutemov static inline unsigned long zap_p4d_range(struct mmu_gather *tlb, 1620c2febafcSKirill A. Shutemov struct vm_area_struct *vma, pgd_t *pgd, 1621c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 1622c2febafcSKirill A. Shutemov struct zap_details *details) 1623c2febafcSKirill A. Shutemov { 1624c2febafcSKirill A. Shutemov p4d_t *p4d; 1625c2febafcSKirill A. Shutemov unsigned long next; 1626c2febafcSKirill A. Shutemov 1627c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 1628c2febafcSKirill A. Shutemov do { 1629c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 1630c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(p4d)) 1631c2febafcSKirill A. Shutemov continue; 1632c2febafcSKirill A. Shutemov next = zap_pud_range(tlb, vma, p4d, addr, next, details); 1633c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 1634c2febafcSKirill A. Shutemov 1635c2febafcSKirill A. Shutemov return addr; 1636c2febafcSKirill A. Shutemov } 1637c2febafcSKirill A. Shutemov 1638aac45363SMichal Hocko void unmap_page_range(struct mmu_gather *tlb, 163951c6f666SRobin Holt struct vm_area_struct *vma, 16401da177e4SLinus Torvalds unsigned long addr, unsigned long end, 164197a89413SPeter Zijlstra struct zap_details *details) 16421da177e4SLinus Torvalds { 16431da177e4SLinus Torvalds pgd_t *pgd; 16441da177e4SLinus Torvalds unsigned long next; 16451da177e4SLinus Torvalds 16461da177e4SLinus Torvalds BUG_ON(addr >= end); 16471da177e4SLinus Torvalds tlb_start_vma(tlb, vma); 16481da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 16491da177e4SLinus Torvalds do { 16501da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 165197a89413SPeter Zijlstra if (pgd_none_or_clear_bad(pgd)) 16521da177e4SLinus Torvalds continue; 1653c2febafcSKirill A. Shutemov next = zap_p4d_range(tlb, vma, pgd, addr, next, details); 165497a89413SPeter Zijlstra } while (pgd++, addr = next, addr != end); 16551da177e4SLinus Torvalds tlb_end_vma(tlb, vma); 16561da177e4SLinus Torvalds } 16571da177e4SLinus Torvalds 1658f5cc4eefSAl Viro 1659f5cc4eefSAl Viro static void unmap_single_vma(struct mmu_gather *tlb, 16601da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long start_addr, 16614f74d2c8SLinus Torvalds unsigned long end_addr, 166268f48381SSuren Baghdasaryan struct zap_details *details, bool mm_wr_locked) 16631da177e4SLinus Torvalds { 1664f5cc4eefSAl Viro unsigned long start = max(vma->vm_start, start_addr); 16651da177e4SLinus Torvalds unsigned long end; 16661da177e4SLinus Torvalds 16671da177e4SLinus Torvalds if (start >= vma->vm_end) 1668f5cc4eefSAl Viro return; 16691da177e4SLinus Torvalds end = min(vma->vm_end, end_addr); 16701da177e4SLinus Torvalds if (end <= vma->vm_start) 1671f5cc4eefSAl Viro return; 16721da177e4SLinus Torvalds 1673cbc91f71SSrikar Dronamraju if (vma->vm_file) 1674cbc91f71SSrikar Dronamraju uprobe_munmap(vma, start, end); 1675cbc91f71SSrikar Dronamraju 1676b3b9c293SKonstantin Khlebnikov if (unlikely(vma->vm_flags & VM_PFNMAP)) 167768f48381SSuren Baghdasaryan untrack_pfn(vma, 0, 0, mm_wr_locked); 16782ab64037Svenkatesh.pallipadi@intel.com 16798b2a1238SAl Viro if (start != end) { 168051c6f666SRobin Holt if (unlikely(is_vm_hugetlb_page(vma))) { 1681a137e1ccSAndi Kleen /* 1682a137e1ccSAndi Kleen * It is undesirable to test vma->vm_file as it 1683a137e1ccSAndi Kleen * should be non-null for valid hugetlb area. 1684a137e1ccSAndi Kleen * However, vm_file will be NULL in the error 16857aa6b4adSDavidlohr Bueso * cleanup path of mmap_region. When 1686a137e1ccSAndi Kleen * hugetlbfs ->mmap method fails, 16877aa6b4adSDavidlohr Bueso * mmap_region() nullifies vma->vm_file 1688a137e1ccSAndi Kleen * before calling this function to clean up. 1689a137e1ccSAndi Kleen * Since no pte has actually been setup, it is 1690a137e1ccSAndi Kleen * safe to do nothing in this case. 1691a137e1ccSAndi Kleen */ 169224669e58SAneesh Kumar K.V if (vma->vm_file) { 169305e90bd0SPeter Xu zap_flags_t zap_flags = details ? 169405e90bd0SPeter Xu details->zap_flags : 0; 16952820b0f0SRik van Riel __unmap_hugepage_range(tlb, vma, start, end, 169605e90bd0SPeter Xu NULL, zap_flags); 169724669e58SAneesh Kumar K.V } 169851c6f666SRobin Holt } else 1699038c7aa1SAl Viro unmap_page_range(tlb, vma, start, end, details); 170097a89413SPeter Zijlstra } 170151c6f666SRobin Holt } 17021da177e4SLinus Torvalds 1703f5cc4eefSAl Viro /** 1704f5cc4eefSAl Viro * unmap_vmas - unmap a range of memory covered by a list of vma's 1705f5cc4eefSAl Viro * @tlb: address of the caller's struct mmu_gather 17066e412203SYang Li * @mas: the maple state 1707f5cc4eefSAl Viro * @vma: the starting vma 1708f5cc4eefSAl Viro * @start_addr: virtual address at which to start unmapping 1709f5cc4eefSAl Viro * @end_addr: virtual address at which to end unmapping 17106e412203SYang Li * @tree_end: The maximum index to check 1711809ef83cSYang Li * @mm_wr_locked: lock flag 1712f5cc4eefSAl Viro * 1713f5cc4eefSAl Viro * Unmap all pages in the vma list. 1714f5cc4eefSAl Viro * 1715f5cc4eefSAl Viro * Only addresses between `start' and `end' will be unmapped. 1716f5cc4eefSAl Viro * 1717f5cc4eefSAl Viro * The VMA list must be sorted in ascending virtual address order. 1718f5cc4eefSAl Viro * 1719f5cc4eefSAl Viro * unmap_vmas() assumes that the caller will flush the whole unmapped address 1720f5cc4eefSAl Viro * range after unmap_vmas() returns. So the only responsibility here is to 1721f5cc4eefSAl Viro * ensure that any thus-far unmapped pages are flushed before unmap_vmas() 1722f5cc4eefSAl Viro * drops the lock and schedules. 1723f5cc4eefSAl Viro */ 1724fd892593SLiam R. Howlett void unmap_vmas(struct mmu_gather *tlb, struct ma_state *mas, 1725f5cc4eefSAl Viro struct vm_area_struct *vma, unsigned long start_addr, 1726fd892593SLiam R. Howlett unsigned long end_addr, unsigned long tree_end, 1727fd892593SLiam R. Howlett bool mm_wr_locked) 1728f5cc4eefSAl Viro { 1729ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 1730999dad82SPeter Xu struct zap_details details = { 173104ada095SMike Kravetz .zap_flags = ZAP_FLAG_DROP_MARKER | ZAP_FLAG_UNMAP, 1732999dad82SPeter Xu /* Careful - we need to zap private pages too! */ 1733999dad82SPeter Xu .even_cows = true, 1734999dad82SPeter Xu }; 1735f5cc4eefSAl Viro 17367d4a8be0SAlistair Popple mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma->vm_mm, 17376f4f13e8SJérôme Glisse start_addr, end_addr); 1738ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 1739763ecb03SLiam R. Howlett do { 17402820b0f0SRik van Riel unsigned long start = start_addr; 17412820b0f0SRik van Riel unsigned long end = end_addr; 17422820b0f0SRik van Riel hugetlb_zap_begin(vma, &start, &end); 17432820b0f0SRik van Riel unmap_single_vma(tlb, vma, start, end, &details, 174468f48381SSuren Baghdasaryan mm_wr_locked); 17452820b0f0SRik van Riel hugetlb_zap_end(vma, &details); 1746fd892593SLiam R. Howlett } while ((vma = mas_find(mas, tree_end - 1)) != NULL); 1747ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 17481da177e4SLinus Torvalds } 17491da177e4SLinus Torvalds 17501da177e4SLinus Torvalds /** 1751f5cc4eefSAl Viro * zap_page_range_single - remove user pages in a given range 1752f5cc4eefSAl Viro * @vma: vm_area_struct holding the applicable pages 1753f5cc4eefSAl Viro * @address: starting address of pages to zap 1754f5cc4eefSAl Viro * @size: number of bytes to zap 17558a5f14a2SKirill A. Shutemov * @details: details of shared cache invalidation 1756f5cc4eefSAl Viro * 1757f5cc4eefSAl Viro * The range must fit into one VMA. 1758f5cc4eefSAl Viro */ 175921b85b09SMike Kravetz void zap_page_range_single(struct vm_area_struct *vma, unsigned long address, 1760f5cc4eefSAl Viro unsigned long size, struct zap_details *details) 1761f5cc4eefSAl Viro { 176221b85b09SMike Kravetz const unsigned long end = address + size; 1763ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 1764f5cc4eefSAl Viro struct mmu_gather tlb; 1765f5cc4eefSAl Viro 1766f5cc4eefSAl Viro lru_add_drain(); 17677d4a8be0SAlistair Popple mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma->vm_mm, 176821b85b09SMike Kravetz address, end); 17692820b0f0SRik van Riel hugetlb_zap_begin(vma, &range.start, &range.end); 1770a72afd87SWill Deacon tlb_gather_mmu(&tlb, vma->vm_mm); 1771ac46d4f3SJérôme Glisse update_hiwater_rss(vma->vm_mm); 1772ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 177321b85b09SMike Kravetz /* 177421b85b09SMike Kravetz * unmap 'address-end' not 'range.start-range.end' as range 177521b85b09SMike Kravetz * could have been expanded for hugetlb pmd sharing. 177621b85b09SMike Kravetz */ 177768f48381SSuren Baghdasaryan unmap_single_vma(&tlb, vma, address, end, details, false); 1778ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 1779ae8eba8bSWill Deacon tlb_finish_mmu(&tlb); 17802820b0f0SRik van Riel hugetlb_zap_end(vma, details); 17811da177e4SLinus Torvalds } 17821da177e4SLinus Torvalds 1783c627f9ccSJack Steiner /** 1784c627f9ccSJack Steiner * zap_vma_ptes - remove ptes mapping the vma 1785c627f9ccSJack Steiner * @vma: vm_area_struct holding ptes to be zapped 1786c627f9ccSJack Steiner * @address: starting address of pages to zap 1787c627f9ccSJack Steiner * @size: number of bytes to zap 1788c627f9ccSJack Steiner * 1789c627f9ccSJack Steiner * This function only unmaps ptes assigned to VM_PFNMAP vmas. 1790c627f9ccSJack Steiner * 1791c627f9ccSJack Steiner * The entire address range must be fully contained within the vma. 1792c627f9ccSJack Steiner * 1793c627f9ccSJack Steiner */ 179427d036e3SLeon Romanovsky void zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, 1795c627f9ccSJack Steiner unsigned long size) 1796c627f9ccSJack Steiner { 179788a35912SMiaohe Lin if (!range_in_vma(vma, address, address + size) || 1798c627f9ccSJack Steiner !(vma->vm_flags & VM_PFNMAP)) 179927d036e3SLeon Romanovsky return; 180027d036e3SLeon Romanovsky 1801f5cc4eefSAl Viro zap_page_range_single(vma, address, size, NULL); 1802c627f9ccSJack Steiner } 1803c627f9ccSJack Steiner EXPORT_SYMBOL_GPL(zap_vma_ptes); 1804c627f9ccSJack Steiner 18058cd3984dSArjun Roy static pmd_t *walk_to_pmd(struct mm_struct *mm, unsigned long addr) 1806c9cfcddfSLinus Torvalds { 1807c2febafcSKirill A. Shutemov pgd_t *pgd; 1808c2febafcSKirill A. Shutemov p4d_t *p4d; 1809c2febafcSKirill A. Shutemov pud_t *pud; 1810c2febafcSKirill A. Shutemov pmd_t *pmd; 1811c2febafcSKirill A. Shutemov 1812c2febafcSKirill A. Shutemov pgd = pgd_offset(mm, addr); 1813c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, addr); 1814c2febafcSKirill A. Shutemov if (!p4d) 1815c2febafcSKirill A. Shutemov return NULL; 1816c2febafcSKirill A. Shutemov pud = pud_alloc(mm, p4d, addr); 1817c2febafcSKirill A. Shutemov if (!pud) 1818c2febafcSKirill A. Shutemov return NULL; 1819c2febafcSKirill A. Shutemov pmd = pmd_alloc(mm, pud, addr); 1820c2febafcSKirill A. Shutemov if (!pmd) 1821c2febafcSKirill A. Shutemov return NULL; 1822c2febafcSKirill A. Shutemov 1823f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 18248cd3984dSArjun Roy return pmd; 18258cd3984dSArjun Roy } 18268cd3984dSArjun Roy 18278cd3984dSArjun Roy pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr, 18288cd3984dSArjun Roy spinlock_t **ptl) 18298cd3984dSArjun Roy { 18308cd3984dSArjun Roy pmd_t *pmd = walk_to_pmd(mm, addr); 18318cd3984dSArjun Roy 18328cd3984dSArjun Roy if (!pmd) 18338cd3984dSArjun Roy return NULL; 1834c9cfcddfSLinus Torvalds return pte_alloc_map_lock(mm, pmd, addr, ptl); 1835c9cfcddfSLinus Torvalds } 1836c9cfcddfSLinus Torvalds 18378efd6f5bSArjun Roy static int validate_page_before_insert(struct page *page) 18388efd6f5bSArjun Roy { 18398efd6f5bSArjun Roy if (PageAnon(page) || PageSlab(page) || page_has_type(page)) 18408efd6f5bSArjun Roy return -EINVAL; 18418efd6f5bSArjun Roy flush_dcache_page(page); 18428efd6f5bSArjun Roy return 0; 18438efd6f5bSArjun Roy } 18448efd6f5bSArjun Roy 1845cea86fe2SHugh Dickins static int insert_page_into_pte_locked(struct vm_area_struct *vma, pte_t *pte, 18468efd6f5bSArjun Roy unsigned long addr, struct page *page, pgprot_t prot) 18478efd6f5bSArjun Roy { 1848c33c7948SRyan Roberts if (!pte_none(ptep_get(pte))) 18498efd6f5bSArjun Roy return -EBUSY; 18508efd6f5bSArjun Roy /* Ok, finally just insert the thing.. */ 18518efd6f5bSArjun Roy get_page(page); 1852f1a79412SShakeel Butt inc_mm_counter(vma->vm_mm, mm_counter_file(page)); 1853cea86fe2SHugh Dickins page_add_file_rmap(page, vma, false); 1854cea86fe2SHugh Dickins set_pte_at(vma->vm_mm, addr, pte, mk_pte(page, prot)); 18558efd6f5bSArjun Roy return 0; 18568efd6f5bSArjun Roy } 18578efd6f5bSArjun Roy 18581da177e4SLinus Torvalds /* 1859238f58d8SLinus Torvalds * This is the old fallback for page remapping. 1860238f58d8SLinus Torvalds * 1861238f58d8SLinus Torvalds * For historical reasons, it only allows reserved pages. Only 1862238f58d8SLinus Torvalds * old drivers should use this, and they needed to mark their 1863238f58d8SLinus Torvalds * pages reserved for the old functions anyway. 1864238f58d8SLinus Torvalds */ 1865423bad60SNick Piggin static int insert_page(struct vm_area_struct *vma, unsigned long addr, 1866423bad60SNick Piggin struct page *page, pgprot_t prot) 1867238f58d8SLinus Torvalds { 1868238f58d8SLinus Torvalds int retval; 1869238f58d8SLinus Torvalds pte_t *pte; 1870238f58d8SLinus Torvalds spinlock_t *ptl; 1871238f58d8SLinus Torvalds 18728efd6f5bSArjun Roy retval = validate_page_before_insert(page); 18738efd6f5bSArjun Roy if (retval) 18745b4e655eSKAMEZAWA Hiroyuki goto out; 1875238f58d8SLinus Torvalds retval = -ENOMEM; 1876cea86fe2SHugh Dickins pte = get_locked_pte(vma->vm_mm, addr, &ptl); 1877238f58d8SLinus Torvalds if (!pte) 18785b4e655eSKAMEZAWA Hiroyuki goto out; 1879cea86fe2SHugh Dickins retval = insert_page_into_pte_locked(vma, pte, addr, page, prot); 1880238f58d8SLinus Torvalds pte_unmap_unlock(pte, ptl); 1881238f58d8SLinus Torvalds out: 1882238f58d8SLinus Torvalds return retval; 1883238f58d8SLinus Torvalds } 1884238f58d8SLinus Torvalds 1885cea86fe2SHugh Dickins static int insert_page_in_batch_locked(struct vm_area_struct *vma, pte_t *pte, 18868cd3984dSArjun Roy unsigned long addr, struct page *page, pgprot_t prot) 18878cd3984dSArjun Roy { 18888cd3984dSArjun Roy int err; 18898cd3984dSArjun Roy 18908cd3984dSArjun Roy if (!page_count(page)) 18918cd3984dSArjun Roy return -EINVAL; 18928cd3984dSArjun Roy err = validate_page_before_insert(page); 18937f70c2a6SArjun Roy if (err) 18947f70c2a6SArjun Roy return err; 1895cea86fe2SHugh Dickins return insert_page_into_pte_locked(vma, pte, addr, page, prot); 18968cd3984dSArjun Roy } 18978cd3984dSArjun Roy 18988cd3984dSArjun Roy /* insert_pages() amortizes the cost of spinlock operations 1899bb7dbaafSMatthew Wilcox (Oracle) * when inserting pages in a loop. 19008cd3984dSArjun Roy */ 19018cd3984dSArjun Roy static int insert_pages(struct vm_area_struct *vma, unsigned long addr, 19028cd3984dSArjun Roy struct page **pages, unsigned long *num, pgprot_t prot) 19038cd3984dSArjun Roy { 19048cd3984dSArjun Roy pmd_t *pmd = NULL; 19057f70c2a6SArjun Roy pte_t *start_pte, *pte; 19067f70c2a6SArjun Roy spinlock_t *pte_lock; 19078cd3984dSArjun Roy struct mm_struct *const mm = vma->vm_mm; 19088cd3984dSArjun Roy unsigned long curr_page_idx = 0; 19098cd3984dSArjun Roy unsigned long remaining_pages_total = *num; 19108cd3984dSArjun Roy unsigned long pages_to_write_in_pmd; 19118cd3984dSArjun Roy int ret; 19128cd3984dSArjun Roy more: 19138cd3984dSArjun Roy ret = -EFAULT; 19148cd3984dSArjun Roy pmd = walk_to_pmd(mm, addr); 19158cd3984dSArjun Roy if (!pmd) 19168cd3984dSArjun Roy goto out; 19178cd3984dSArjun Roy 19188cd3984dSArjun Roy pages_to_write_in_pmd = min_t(unsigned long, 19198cd3984dSArjun Roy remaining_pages_total, PTRS_PER_PTE - pte_index(addr)); 19208cd3984dSArjun Roy 19218cd3984dSArjun Roy /* Allocate the PTE if necessary; takes PMD lock once only. */ 19228cd3984dSArjun Roy ret = -ENOMEM; 19238cd3984dSArjun Roy if (pte_alloc(mm, pmd)) 19248cd3984dSArjun Roy goto out; 19258cd3984dSArjun Roy 19268cd3984dSArjun Roy while (pages_to_write_in_pmd) { 19278cd3984dSArjun Roy int pte_idx = 0; 19288cd3984dSArjun Roy const int batch_size = min_t(int, pages_to_write_in_pmd, 8); 19298cd3984dSArjun Roy 19307f70c2a6SArjun Roy start_pte = pte_offset_map_lock(mm, pmd, addr, &pte_lock); 19313db82b93SHugh Dickins if (!start_pte) { 19323db82b93SHugh Dickins ret = -EFAULT; 19333db82b93SHugh Dickins goto out; 19343db82b93SHugh Dickins } 19357f70c2a6SArjun Roy for (pte = start_pte; pte_idx < batch_size; ++pte, ++pte_idx) { 1936cea86fe2SHugh Dickins int err = insert_page_in_batch_locked(vma, pte, 19378cd3984dSArjun Roy addr, pages[curr_page_idx], prot); 19388cd3984dSArjun Roy if (unlikely(err)) { 19397f70c2a6SArjun Roy pte_unmap_unlock(start_pte, pte_lock); 19408cd3984dSArjun Roy ret = err; 19418cd3984dSArjun Roy remaining_pages_total -= pte_idx; 19428cd3984dSArjun Roy goto out; 19438cd3984dSArjun Roy } 19448cd3984dSArjun Roy addr += PAGE_SIZE; 19458cd3984dSArjun Roy ++curr_page_idx; 19468cd3984dSArjun Roy } 19477f70c2a6SArjun Roy pte_unmap_unlock(start_pte, pte_lock); 19488cd3984dSArjun Roy pages_to_write_in_pmd -= batch_size; 19498cd3984dSArjun Roy remaining_pages_total -= batch_size; 19508cd3984dSArjun Roy } 19518cd3984dSArjun Roy if (remaining_pages_total) 19528cd3984dSArjun Roy goto more; 19538cd3984dSArjun Roy ret = 0; 19548cd3984dSArjun Roy out: 19558cd3984dSArjun Roy *num = remaining_pages_total; 19568cd3984dSArjun Roy return ret; 19578cd3984dSArjun Roy } 19588cd3984dSArjun Roy 19598cd3984dSArjun Roy /** 19608cd3984dSArjun Roy * vm_insert_pages - insert multiple pages into user vma, batching the pmd lock. 19618cd3984dSArjun Roy * @vma: user vma to map to 19628cd3984dSArjun Roy * @addr: target start user address of these pages 19638cd3984dSArjun Roy * @pages: source kernel pages 19648cd3984dSArjun Roy * @num: in: number of pages to map. out: number of pages that were *not* 19658cd3984dSArjun Roy * mapped. (0 means all pages were successfully mapped). 19668cd3984dSArjun Roy * 19678cd3984dSArjun Roy * Preferred over vm_insert_page() when inserting multiple pages. 19688cd3984dSArjun Roy * 19698cd3984dSArjun Roy * In case of error, we may have mapped a subset of the provided 19708cd3984dSArjun Roy * pages. It is the caller's responsibility to account for this case. 19718cd3984dSArjun Roy * 19728cd3984dSArjun Roy * The same restrictions apply as in vm_insert_page(). 19738cd3984dSArjun Roy */ 19748cd3984dSArjun Roy int vm_insert_pages(struct vm_area_struct *vma, unsigned long addr, 19758cd3984dSArjun Roy struct page **pages, unsigned long *num) 19768cd3984dSArjun Roy { 19778cd3984dSArjun Roy const unsigned long end_addr = addr + (*num * PAGE_SIZE) - 1; 19788cd3984dSArjun Roy 19798cd3984dSArjun Roy if (addr < vma->vm_start || end_addr >= vma->vm_end) 19808cd3984dSArjun Roy return -EFAULT; 19818cd3984dSArjun Roy if (!(vma->vm_flags & VM_MIXEDMAP)) { 1982d8ed45c5SMichel Lespinasse BUG_ON(mmap_read_trylock(vma->vm_mm)); 19838cd3984dSArjun Roy BUG_ON(vma->vm_flags & VM_PFNMAP); 19841c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_MIXEDMAP); 19858cd3984dSArjun Roy } 19868cd3984dSArjun Roy /* Defer page refcount checking till we're about to map that page. */ 19878cd3984dSArjun Roy return insert_pages(vma, addr, pages, num, vma->vm_page_prot); 19888cd3984dSArjun Roy } 19898cd3984dSArjun Roy EXPORT_SYMBOL(vm_insert_pages); 19908cd3984dSArjun Roy 1991bfa5bf6dSRolf Eike Beer /** 1992bfa5bf6dSRolf Eike Beer * vm_insert_page - insert single page into user vma 1993bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 1994bfa5bf6dSRolf Eike Beer * @addr: target user address of this page 1995bfa5bf6dSRolf Eike Beer * @page: source kernel page 1996bfa5bf6dSRolf Eike Beer * 1997a145dd41SLinus Torvalds * This allows drivers to insert individual pages they've allocated 1998a145dd41SLinus Torvalds * into a user vma. 1999a145dd41SLinus Torvalds * 2000a145dd41SLinus Torvalds * The page has to be a nice clean _individual_ kernel allocation. 2001a145dd41SLinus Torvalds * If you allocate a compound page, you need to have marked it as 2002a145dd41SLinus Torvalds * such (__GFP_COMP), or manually just split the page up yourself 20038dfcc9baSNick Piggin * (see split_page()). 2004a145dd41SLinus Torvalds * 2005a145dd41SLinus Torvalds * NOTE! Traditionally this was done with "remap_pfn_range()" which 2006a145dd41SLinus Torvalds * took an arbitrary page protection parameter. This doesn't allow 2007a145dd41SLinus Torvalds * that. Your vma protection will have to be set up correctly, which 2008a145dd41SLinus Torvalds * means that if you want a shared writable mapping, you'd better 2009a145dd41SLinus Torvalds * ask for a shared writable mapping! 2010a145dd41SLinus Torvalds * 2011a145dd41SLinus Torvalds * The page does not need to be reserved. 20124b6e1e37SKonstantin Khlebnikov * 20134b6e1e37SKonstantin Khlebnikov * Usually this function is called from f_op->mmap() handler 2014c1e8d7c6SMichel Lespinasse * under mm->mmap_lock write-lock, so it can change vma->vm_flags. 20154b6e1e37SKonstantin Khlebnikov * Caller must set VM_MIXEDMAP on vma if it wants to call this 20164b6e1e37SKonstantin Khlebnikov * function from other places, for example from page-fault handler. 2017a862f68aSMike Rapoport * 2018a862f68aSMike Rapoport * Return: %0 on success, negative error code otherwise. 2019a145dd41SLinus Torvalds */ 2020423bad60SNick Piggin int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, 2021423bad60SNick Piggin struct page *page) 2022a145dd41SLinus Torvalds { 2023a145dd41SLinus Torvalds if (addr < vma->vm_start || addr >= vma->vm_end) 2024a145dd41SLinus Torvalds return -EFAULT; 2025a145dd41SLinus Torvalds if (!page_count(page)) 2026a145dd41SLinus Torvalds return -EINVAL; 20274b6e1e37SKonstantin Khlebnikov if (!(vma->vm_flags & VM_MIXEDMAP)) { 2028d8ed45c5SMichel Lespinasse BUG_ON(mmap_read_trylock(vma->vm_mm)); 20294b6e1e37SKonstantin Khlebnikov BUG_ON(vma->vm_flags & VM_PFNMAP); 20301c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_MIXEDMAP); 20314b6e1e37SKonstantin Khlebnikov } 2032423bad60SNick Piggin return insert_page(vma, addr, page, vma->vm_page_prot); 2033a145dd41SLinus Torvalds } 2034e3c3374fSLinus Torvalds EXPORT_SYMBOL(vm_insert_page); 2035a145dd41SLinus Torvalds 2036a667d745SSouptick Joarder /* 2037a667d745SSouptick Joarder * __vm_map_pages - maps range of kernel pages into user vma 2038a667d745SSouptick Joarder * @vma: user vma to map to 2039a667d745SSouptick Joarder * @pages: pointer to array of source kernel pages 2040a667d745SSouptick Joarder * @num: number of pages in page array 2041a667d745SSouptick Joarder * @offset: user's requested vm_pgoff 2042a667d745SSouptick Joarder * 2043a667d745SSouptick Joarder * This allows drivers to map range of kernel pages into a user vma. 2044a667d745SSouptick Joarder * 2045a667d745SSouptick Joarder * Return: 0 on success and error code otherwise. 2046a667d745SSouptick Joarder */ 2047a667d745SSouptick Joarder static int __vm_map_pages(struct vm_area_struct *vma, struct page **pages, 2048a667d745SSouptick Joarder unsigned long num, unsigned long offset) 2049a667d745SSouptick Joarder { 2050a667d745SSouptick Joarder unsigned long count = vma_pages(vma); 2051a667d745SSouptick Joarder unsigned long uaddr = vma->vm_start; 2052a667d745SSouptick Joarder int ret, i; 2053a667d745SSouptick Joarder 2054a667d745SSouptick Joarder /* Fail if the user requested offset is beyond the end of the object */ 205596756fcbSMiguel Ojeda if (offset >= num) 2056a667d745SSouptick Joarder return -ENXIO; 2057a667d745SSouptick Joarder 2058a667d745SSouptick Joarder /* Fail if the user requested size exceeds available object size */ 2059a667d745SSouptick Joarder if (count > num - offset) 2060a667d745SSouptick Joarder return -ENXIO; 2061a667d745SSouptick Joarder 2062a667d745SSouptick Joarder for (i = 0; i < count; i++) { 2063a667d745SSouptick Joarder ret = vm_insert_page(vma, uaddr, pages[offset + i]); 2064a667d745SSouptick Joarder if (ret < 0) 2065a667d745SSouptick Joarder return ret; 2066a667d745SSouptick Joarder uaddr += PAGE_SIZE; 2067a667d745SSouptick Joarder } 2068a667d745SSouptick Joarder 2069a667d745SSouptick Joarder return 0; 2070a667d745SSouptick Joarder } 2071a667d745SSouptick Joarder 2072a667d745SSouptick Joarder /** 2073a667d745SSouptick Joarder * vm_map_pages - maps range of kernel pages starts with non zero offset 2074a667d745SSouptick Joarder * @vma: user vma to map to 2075a667d745SSouptick Joarder * @pages: pointer to array of source kernel pages 2076a667d745SSouptick Joarder * @num: number of pages in page array 2077a667d745SSouptick Joarder * 2078a667d745SSouptick Joarder * Maps an object consisting of @num pages, catering for the user's 2079a667d745SSouptick Joarder * requested vm_pgoff 2080a667d745SSouptick Joarder * 2081a667d745SSouptick Joarder * If we fail to insert any page into the vma, the function will return 2082a667d745SSouptick Joarder * immediately leaving any previously inserted pages present. Callers 2083a667d745SSouptick Joarder * from the mmap handler may immediately return the error as their caller 2084a667d745SSouptick Joarder * will destroy the vma, removing any successfully inserted pages. Other 2085a667d745SSouptick Joarder * callers should make their own arrangements for calling unmap_region(). 2086a667d745SSouptick Joarder * 2087a667d745SSouptick Joarder * Context: Process context. Called by mmap handlers. 2088a667d745SSouptick Joarder * Return: 0 on success and error code otherwise. 2089a667d745SSouptick Joarder */ 2090a667d745SSouptick Joarder int vm_map_pages(struct vm_area_struct *vma, struct page **pages, 2091a667d745SSouptick Joarder unsigned long num) 2092a667d745SSouptick Joarder { 2093a667d745SSouptick Joarder return __vm_map_pages(vma, pages, num, vma->vm_pgoff); 2094a667d745SSouptick Joarder } 2095a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages); 2096a667d745SSouptick Joarder 2097a667d745SSouptick Joarder /** 2098a667d745SSouptick Joarder * vm_map_pages_zero - map range of kernel pages starts with zero offset 2099a667d745SSouptick Joarder * @vma: user vma to map to 2100a667d745SSouptick Joarder * @pages: pointer to array of source kernel pages 2101a667d745SSouptick Joarder * @num: number of pages in page array 2102a667d745SSouptick Joarder * 2103a667d745SSouptick Joarder * Similar to vm_map_pages(), except that it explicitly sets the offset 2104a667d745SSouptick Joarder * to 0. This function is intended for the drivers that did not consider 2105a667d745SSouptick Joarder * vm_pgoff. 2106a667d745SSouptick Joarder * 2107a667d745SSouptick Joarder * Context: Process context. Called by mmap handlers. 2108a667d745SSouptick Joarder * Return: 0 on success and error code otherwise. 2109a667d745SSouptick Joarder */ 2110a667d745SSouptick Joarder int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages, 2111a667d745SSouptick Joarder unsigned long num) 2112a667d745SSouptick Joarder { 2113a667d745SSouptick Joarder return __vm_map_pages(vma, pages, num, 0); 2114a667d745SSouptick Joarder } 2115a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages_zero); 2116a667d745SSouptick Joarder 21179b5a8e00SMatthew Wilcox static vm_fault_t insert_pfn(struct vm_area_struct *vma, unsigned long addr, 2118b2770da6SRoss Zwisler pfn_t pfn, pgprot_t prot, bool mkwrite) 2119423bad60SNick Piggin { 2120423bad60SNick Piggin struct mm_struct *mm = vma->vm_mm; 2121423bad60SNick Piggin pte_t *pte, entry; 2122423bad60SNick Piggin spinlock_t *ptl; 2123423bad60SNick Piggin 2124423bad60SNick Piggin pte = get_locked_pte(mm, addr, &ptl); 2125423bad60SNick Piggin if (!pte) 21269b5a8e00SMatthew Wilcox return VM_FAULT_OOM; 2127c33c7948SRyan Roberts entry = ptep_get(pte); 2128c33c7948SRyan Roberts if (!pte_none(entry)) { 2129b2770da6SRoss Zwisler if (mkwrite) { 2130b2770da6SRoss Zwisler /* 2131b2770da6SRoss Zwisler * For read faults on private mappings the PFN passed 2132b2770da6SRoss Zwisler * in may not match the PFN we have mapped if the 2133b2770da6SRoss Zwisler * mapped PFN is a writeable COW page. In the mkwrite 2134b2770da6SRoss Zwisler * case we are creating a writable PTE for a shared 2135f2c57d91SJan Kara * mapping and we expect the PFNs to match. If they 2136f2c57d91SJan Kara * don't match, we are likely racing with block 2137f2c57d91SJan Kara * allocation and mapping invalidation so just skip the 2138f2c57d91SJan Kara * update. 2139b2770da6SRoss Zwisler */ 2140c33c7948SRyan Roberts if (pte_pfn(entry) != pfn_t_to_pfn(pfn)) { 2141c33c7948SRyan Roberts WARN_ON_ONCE(!is_zero_pfn(pte_pfn(entry))); 2142423bad60SNick Piggin goto out_unlock; 2143f2c57d91SJan Kara } 2144c33c7948SRyan Roberts entry = pte_mkyoung(entry); 2145cae85cb8SJan Kara entry = maybe_mkwrite(pte_mkdirty(entry), vma); 2146cae85cb8SJan Kara if (ptep_set_access_flags(vma, addr, pte, entry, 1)) 2147cae85cb8SJan Kara update_mmu_cache(vma, addr, pte); 2148cae85cb8SJan Kara } 2149b2770da6SRoss Zwisler goto out_unlock; 2150b2770da6SRoss Zwisler } 2151423bad60SNick Piggin 2152423bad60SNick Piggin /* Ok, finally just insert the thing.. */ 215301c8f1c4SDan Williams if (pfn_t_devmap(pfn)) 215401c8f1c4SDan Williams entry = pte_mkdevmap(pfn_t_pte(pfn, prot)); 215501c8f1c4SDan Williams else 215601c8f1c4SDan Williams entry = pte_mkspecial(pfn_t_pte(pfn, prot)); 2157b2770da6SRoss Zwisler 2158b2770da6SRoss Zwisler if (mkwrite) { 2159b2770da6SRoss Zwisler entry = pte_mkyoung(entry); 2160b2770da6SRoss Zwisler entry = maybe_mkwrite(pte_mkdirty(entry), vma); 2161b2770da6SRoss Zwisler } 2162b2770da6SRoss Zwisler 2163423bad60SNick Piggin set_pte_at(mm, addr, pte, entry); 21644b3073e1SRussell King update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */ 2165423bad60SNick Piggin 2166423bad60SNick Piggin out_unlock: 2167423bad60SNick Piggin pte_unmap_unlock(pte, ptl); 21689b5a8e00SMatthew Wilcox return VM_FAULT_NOPAGE; 2169423bad60SNick Piggin } 2170423bad60SNick Piggin 2171f5e6d1d5SMatthew Wilcox /** 2172f5e6d1d5SMatthew Wilcox * vmf_insert_pfn_prot - insert single pfn into user vma with specified pgprot 2173f5e6d1d5SMatthew Wilcox * @vma: user vma to map to 2174f5e6d1d5SMatthew Wilcox * @addr: target user address of this page 2175f5e6d1d5SMatthew Wilcox * @pfn: source kernel pfn 2176f5e6d1d5SMatthew Wilcox * @pgprot: pgprot flags for the inserted page 2177f5e6d1d5SMatthew Wilcox * 2178a1a0aea5SRandy Dunlap * This is exactly like vmf_insert_pfn(), except that it allows drivers 2179f5e6d1d5SMatthew Wilcox * to override pgprot on a per-page basis. 2180f5e6d1d5SMatthew Wilcox * 2181f5e6d1d5SMatthew Wilcox * This only makes sense for IO mappings, and it makes no sense for 2182f5e6d1d5SMatthew Wilcox * COW mappings. In general, using multiple vmas is preferable; 2183ae2b01f3SMatthew Wilcox * vmf_insert_pfn_prot should only be used if using multiple VMAs is 2184f5e6d1d5SMatthew Wilcox * impractical. 2185f5e6d1d5SMatthew Wilcox * 218628d8b812SLorenzo Stoakes * pgprot typically only differs from @vma->vm_page_prot when drivers set 218728d8b812SLorenzo Stoakes * caching- and encryption bits different than those of @vma->vm_page_prot, 218828d8b812SLorenzo Stoakes * because the caching- or encryption mode may not be known at mmap() time. 218928d8b812SLorenzo Stoakes * 219028d8b812SLorenzo Stoakes * This is ok as long as @vma->vm_page_prot is not used by the core vm 219128d8b812SLorenzo Stoakes * to set caching and encryption bits for those vmas (except for COW pages). 219228d8b812SLorenzo Stoakes * This is ensured by core vm only modifying these page table entries using 219328d8b812SLorenzo Stoakes * functions that don't touch caching- or encryption bits, using pte_modify() 219428d8b812SLorenzo Stoakes * if needed. (See for example mprotect()). 219528d8b812SLorenzo Stoakes * 219628d8b812SLorenzo Stoakes * Also when new page-table entries are created, this is only done using the 219728d8b812SLorenzo Stoakes * fault() callback, and never using the value of vma->vm_page_prot, 219828d8b812SLorenzo Stoakes * except for page-table entries that point to anonymous pages as the result 219928d8b812SLorenzo Stoakes * of COW. 2200574c5b3dSThomas Hellstrom * 2201ae2b01f3SMatthew Wilcox * Context: Process context. May allocate using %GFP_KERNEL. 2202f5e6d1d5SMatthew Wilcox * Return: vm_fault_t value. 2203f5e6d1d5SMatthew Wilcox */ 2204f5e6d1d5SMatthew Wilcox vm_fault_t vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr, 2205f5e6d1d5SMatthew Wilcox unsigned long pfn, pgprot_t pgprot) 2206f5e6d1d5SMatthew Wilcox { 22076d958546SMatthew Wilcox /* 22086d958546SMatthew Wilcox * Technically, architectures with pte_special can avoid all these 22096d958546SMatthew Wilcox * restrictions (same for remap_pfn_range). However we would like 22106d958546SMatthew Wilcox * consistency in testing and feature parity among all, so we should 22116d958546SMatthew Wilcox * try to keep these invariants in place for everybody. 22126d958546SMatthew Wilcox */ 22136d958546SMatthew Wilcox BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))); 22146d958546SMatthew Wilcox BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) == 22156d958546SMatthew Wilcox (VM_PFNMAP|VM_MIXEDMAP)); 22166d958546SMatthew Wilcox BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); 22176d958546SMatthew Wilcox BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn)); 22186d958546SMatthew Wilcox 22196d958546SMatthew Wilcox if (addr < vma->vm_start || addr >= vma->vm_end) 22206d958546SMatthew Wilcox return VM_FAULT_SIGBUS; 22216d958546SMatthew Wilcox 22226d958546SMatthew Wilcox if (!pfn_modify_allowed(pfn, pgprot)) 22236d958546SMatthew Wilcox return VM_FAULT_SIGBUS; 22246d958546SMatthew Wilcox 22256d958546SMatthew Wilcox track_pfn_insert(vma, &pgprot, __pfn_to_pfn_t(pfn, PFN_DEV)); 22266d958546SMatthew Wilcox 22279b5a8e00SMatthew Wilcox return insert_pfn(vma, addr, __pfn_to_pfn_t(pfn, PFN_DEV), pgprot, 22286d958546SMatthew Wilcox false); 2229f5e6d1d5SMatthew Wilcox } 2230f5e6d1d5SMatthew Wilcox EXPORT_SYMBOL(vmf_insert_pfn_prot); 2231e0dc0d8fSNick Piggin 2232ae2b01f3SMatthew Wilcox /** 2233ae2b01f3SMatthew Wilcox * vmf_insert_pfn - insert single pfn into user vma 2234ae2b01f3SMatthew Wilcox * @vma: user vma to map to 2235ae2b01f3SMatthew Wilcox * @addr: target user address of this page 2236ae2b01f3SMatthew Wilcox * @pfn: source kernel pfn 2237ae2b01f3SMatthew Wilcox * 2238ae2b01f3SMatthew Wilcox * Similar to vm_insert_page, this allows drivers to insert individual pages 2239ae2b01f3SMatthew Wilcox * they've allocated into a user vma. Same comments apply. 2240ae2b01f3SMatthew Wilcox * 2241ae2b01f3SMatthew Wilcox * This function should only be called from a vm_ops->fault handler, and 2242ae2b01f3SMatthew Wilcox * in that case the handler should return the result of this function. 2243ae2b01f3SMatthew Wilcox * 2244ae2b01f3SMatthew Wilcox * vma cannot be a COW mapping. 2245ae2b01f3SMatthew Wilcox * 2246ae2b01f3SMatthew Wilcox * As this is called only for pages that do not currently exist, we 2247ae2b01f3SMatthew Wilcox * do not need to flush old virtual caches or the TLB. 2248ae2b01f3SMatthew Wilcox * 2249ae2b01f3SMatthew Wilcox * Context: Process context. May allocate using %GFP_KERNEL. 2250ae2b01f3SMatthew Wilcox * Return: vm_fault_t value. 2251ae2b01f3SMatthew Wilcox */ 2252ae2b01f3SMatthew Wilcox vm_fault_t vmf_insert_pfn(struct vm_area_struct *vma, unsigned long addr, 2253ae2b01f3SMatthew Wilcox unsigned long pfn) 2254ae2b01f3SMatthew Wilcox { 2255ae2b01f3SMatthew Wilcox return vmf_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot); 2256ae2b01f3SMatthew Wilcox } 2257ae2b01f3SMatthew Wilcox EXPORT_SYMBOL(vmf_insert_pfn); 2258ae2b01f3SMatthew Wilcox 2259785a3fabSDan Williams static bool vm_mixed_ok(struct vm_area_struct *vma, pfn_t pfn) 2260785a3fabSDan Williams { 2261785a3fabSDan Williams /* these checks mirror the abort conditions in vm_normal_page */ 2262785a3fabSDan Williams if (vma->vm_flags & VM_MIXEDMAP) 2263785a3fabSDan Williams return true; 2264785a3fabSDan Williams if (pfn_t_devmap(pfn)) 2265785a3fabSDan Williams return true; 2266785a3fabSDan Williams if (pfn_t_special(pfn)) 2267785a3fabSDan Williams return true; 2268785a3fabSDan Williams if (is_zero_pfn(pfn_t_to_pfn(pfn))) 2269785a3fabSDan Williams return true; 2270785a3fabSDan Williams return false; 2271785a3fabSDan Williams } 2272785a3fabSDan Williams 227379f3aa5bSMatthew Wilcox static vm_fault_t __vm_insert_mixed(struct vm_area_struct *vma, 227428d8b812SLorenzo Stoakes unsigned long addr, pfn_t pfn, bool mkwrite) 2275423bad60SNick Piggin { 227628d8b812SLorenzo Stoakes pgprot_t pgprot = vma->vm_page_prot; 227779f3aa5bSMatthew Wilcox int err; 227887744ab3SDan Williams 2279785a3fabSDan Williams BUG_ON(!vm_mixed_ok(vma, pfn)); 2280423bad60SNick Piggin 2281423bad60SNick Piggin if (addr < vma->vm_start || addr >= vma->vm_end) 228279f3aa5bSMatthew Wilcox return VM_FAULT_SIGBUS; 2283308a047cSBorislav Petkov 2284308a047cSBorislav Petkov track_pfn_insert(vma, &pgprot, pfn); 2285423bad60SNick Piggin 228642e4089cSAndi Kleen if (!pfn_modify_allowed(pfn_t_to_pfn(pfn), pgprot)) 228779f3aa5bSMatthew Wilcox return VM_FAULT_SIGBUS; 228842e4089cSAndi Kleen 2289423bad60SNick Piggin /* 2290423bad60SNick Piggin * If we don't have pte special, then we have to use the pfn_valid() 2291423bad60SNick Piggin * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must* 2292423bad60SNick Piggin * refcount the page if pfn_valid is true (hence insert_page rather 229362eede62SHugh Dickins * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP 229462eede62SHugh Dickins * without pte special, it would there be refcounted as a normal page. 2295423bad60SNick Piggin */ 229600b3a331SLaurent Dufour if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) && 229700b3a331SLaurent Dufour !pfn_t_devmap(pfn) && pfn_t_valid(pfn)) { 2298423bad60SNick Piggin struct page *page; 2299423bad60SNick Piggin 230003fc2da6SDan Williams /* 230103fc2da6SDan Williams * At this point we are committed to insert_page() 230203fc2da6SDan Williams * regardless of whether the caller specified flags that 230303fc2da6SDan Williams * result in pfn_t_has_page() == false. 230403fc2da6SDan Williams */ 230503fc2da6SDan Williams page = pfn_to_page(pfn_t_to_pfn(pfn)); 230679f3aa5bSMatthew Wilcox err = insert_page(vma, addr, page, pgprot); 230779f3aa5bSMatthew Wilcox } else { 23089b5a8e00SMatthew Wilcox return insert_pfn(vma, addr, pfn, pgprot, mkwrite); 2309423bad60SNick Piggin } 2310b2770da6SRoss Zwisler 23115d747637SMatthew Wilcox if (err == -ENOMEM) 23125d747637SMatthew Wilcox return VM_FAULT_OOM; 23135d747637SMatthew Wilcox if (err < 0 && err != -EBUSY) 23145d747637SMatthew Wilcox return VM_FAULT_SIGBUS; 23155d747637SMatthew Wilcox 23165d747637SMatthew Wilcox return VM_FAULT_NOPAGE; 2317423bad60SNick Piggin } 231879f3aa5bSMatthew Wilcox 231979f3aa5bSMatthew Wilcox vm_fault_t vmf_insert_mixed(struct vm_area_struct *vma, unsigned long addr, 232079f3aa5bSMatthew Wilcox pfn_t pfn) 232179f3aa5bSMatthew Wilcox { 232228d8b812SLorenzo Stoakes return __vm_insert_mixed(vma, addr, pfn, false); 232379f3aa5bSMatthew Wilcox } 23245d747637SMatthew Wilcox EXPORT_SYMBOL(vmf_insert_mixed); 2325423bad60SNick Piggin 2326ab77dab4SSouptick Joarder /* 2327ab77dab4SSouptick Joarder * If the insertion of PTE failed because someone else already added a 2328ab77dab4SSouptick Joarder * different entry in the mean time, we treat that as success as we assume 2329ab77dab4SSouptick Joarder * the same entry was actually inserted. 2330ab77dab4SSouptick Joarder */ 2331ab77dab4SSouptick Joarder vm_fault_t vmf_insert_mixed_mkwrite(struct vm_area_struct *vma, 2332ab77dab4SSouptick Joarder unsigned long addr, pfn_t pfn) 2333b2770da6SRoss Zwisler { 233428d8b812SLorenzo Stoakes return __vm_insert_mixed(vma, addr, pfn, true); 2335b2770da6SRoss Zwisler } 2336ab77dab4SSouptick Joarder EXPORT_SYMBOL(vmf_insert_mixed_mkwrite); 2337b2770da6SRoss Zwisler 2338a145dd41SLinus Torvalds /* 23391da177e4SLinus Torvalds * maps a range of physical memory into the requested pages. the old 23401da177e4SLinus Torvalds * mappings are removed. any references to nonexistent pages results 23411da177e4SLinus Torvalds * in null mappings (currently treated as "copy-on-access") 23421da177e4SLinus Torvalds */ 23431da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd, 23441da177e4SLinus Torvalds unsigned long addr, unsigned long end, 23451da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 23461da177e4SLinus Torvalds { 234790a3e375SMiaohe Lin pte_t *pte, *mapped_pte; 2348c74df32cSHugh Dickins spinlock_t *ptl; 234942e4089cSAndi Kleen int err = 0; 23501da177e4SLinus Torvalds 235190a3e375SMiaohe Lin mapped_pte = pte = pte_alloc_map_lock(mm, pmd, addr, &ptl); 23521da177e4SLinus Torvalds if (!pte) 23531da177e4SLinus Torvalds return -ENOMEM; 23546606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 23551da177e4SLinus Torvalds do { 2356c33c7948SRyan Roberts BUG_ON(!pte_none(ptep_get(pte))); 235742e4089cSAndi Kleen if (!pfn_modify_allowed(pfn, prot)) { 235842e4089cSAndi Kleen err = -EACCES; 235942e4089cSAndi Kleen break; 236042e4089cSAndi Kleen } 23617e675137SNick Piggin set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot))); 23621da177e4SLinus Torvalds pfn++; 23631da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 23646606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 236590a3e375SMiaohe Lin pte_unmap_unlock(mapped_pte, ptl); 236642e4089cSAndi Kleen return err; 23671da177e4SLinus Torvalds } 23681da177e4SLinus Torvalds 23691da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud, 23701da177e4SLinus Torvalds unsigned long addr, unsigned long end, 23711da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 23721da177e4SLinus Torvalds { 23731da177e4SLinus Torvalds pmd_t *pmd; 23741da177e4SLinus Torvalds unsigned long next; 237542e4089cSAndi Kleen int err; 23761da177e4SLinus Torvalds 23771da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 23781da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, addr); 23791da177e4SLinus Torvalds if (!pmd) 23801da177e4SLinus Torvalds return -ENOMEM; 2381f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 23821da177e4SLinus Torvalds do { 23831da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 238442e4089cSAndi Kleen err = remap_pte_range(mm, pmd, addr, next, 238542e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 238642e4089cSAndi Kleen if (err) 238742e4089cSAndi Kleen return err; 23881da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 23891da177e4SLinus Torvalds return 0; 23901da177e4SLinus Torvalds } 23911da177e4SLinus Torvalds 2392c2febafcSKirill A. Shutemov static inline int remap_pud_range(struct mm_struct *mm, p4d_t *p4d, 23931da177e4SLinus Torvalds unsigned long addr, unsigned long end, 23941da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 23951da177e4SLinus Torvalds { 23961da177e4SLinus Torvalds pud_t *pud; 23971da177e4SLinus Torvalds unsigned long next; 239842e4089cSAndi Kleen int err; 23991da177e4SLinus Torvalds 24001da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 2401c2febafcSKirill A. Shutemov pud = pud_alloc(mm, p4d, addr); 24021da177e4SLinus Torvalds if (!pud) 24031da177e4SLinus Torvalds return -ENOMEM; 24041da177e4SLinus Torvalds do { 24051da177e4SLinus Torvalds next = pud_addr_end(addr, end); 240642e4089cSAndi Kleen err = remap_pmd_range(mm, pud, addr, next, 240742e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 240842e4089cSAndi Kleen if (err) 240942e4089cSAndi Kleen return err; 24101da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 24111da177e4SLinus Torvalds return 0; 24121da177e4SLinus Torvalds } 24131da177e4SLinus Torvalds 2414c2febafcSKirill A. Shutemov static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd, 2415c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 2416c2febafcSKirill A. Shutemov unsigned long pfn, pgprot_t prot) 2417c2febafcSKirill A. Shutemov { 2418c2febafcSKirill A. Shutemov p4d_t *p4d; 2419c2febafcSKirill A. Shutemov unsigned long next; 242042e4089cSAndi Kleen int err; 2421c2febafcSKirill A. Shutemov 2422c2febafcSKirill A. Shutemov pfn -= addr >> PAGE_SHIFT; 2423c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, addr); 2424c2febafcSKirill A. Shutemov if (!p4d) 2425c2febafcSKirill A. Shutemov return -ENOMEM; 2426c2febafcSKirill A. Shutemov do { 2427c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 242842e4089cSAndi Kleen err = remap_pud_range(mm, p4d, addr, next, 242942e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 243042e4089cSAndi Kleen if (err) 243142e4089cSAndi Kleen return err; 2432c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 2433c2febafcSKirill A. Shutemov return 0; 2434c2febafcSKirill A. Shutemov } 2435c2febafcSKirill A. Shutemov 243674ffa5a3SChristoph Hellwig /* 243774ffa5a3SChristoph Hellwig * Variant of remap_pfn_range that does not call track_pfn_remap. The caller 243874ffa5a3SChristoph Hellwig * must have pre-validated the caching bits of the pgprot_t. 2439bfa5bf6dSRolf Eike Beer */ 244074ffa5a3SChristoph Hellwig int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr, 24411da177e4SLinus Torvalds unsigned long pfn, unsigned long size, pgprot_t prot) 24421da177e4SLinus Torvalds { 24431da177e4SLinus Torvalds pgd_t *pgd; 24441da177e4SLinus Torvalds unsigned long next; 24452d15cab8SHugh Dickins unsigned long end = addr + PAGE_ALIGN(size); 24461da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 24471da177e4SLinus Torvalds int err; 24481da177e4SLinus Torvalds 24490c4123e3SAlex Zhang if (WARN_ON_ONCE(!PAGE_ALIGNED(addr))) 24500c4123e3SAlex Zhang return -EINVAL; 24510c4123e3SAlex Zhang 24521da177e4SLinus Torvalds /* 24531da177e4SLinus Torvalds * Physically remapped pages are special. Tell the 24541da177e4SLinus Torvalds * rest of the world about it: 24551da177e4SLinus Torvalds * VM_IO tells people not to look at these pages 24561da177e4SLinus Torvalds * (accesses can have side effects). 24576aab341eSLinus Torvalds * VM_PFNMAP tells the core MM that the base pages are just 24586aab341eSLinus Torvalds * raw PFN mappings, and do not have a "struct page" associated 24596aab341eSLinus Torvalds * with them. 2460314e51b9SKonstantin Khlebnikov * VM_DONTEXPAND 2461314e51b9SKonstantin Khlebnikov * Disable vma merging and expanding with mremap(). 2462314e51b9SKonstantin Khlebnikov * VM_DONTDUMP 2463314e51b9SKonstantin Khlebnikov * Omit vma from core dump, even when VM_IO turned off. 2464fb155c16SLinus Torvalds * 2465fb155c16SLinus Torvalds * There's a horrible special case to handle copy-on-write 2466fb155c16SLinus Torvalds * behaviour that some programs depend on. We mark the "original" 2467fb155c16SLinus Torvalds * un-COW'ed pages by matching them up with "vma->vm_pgoff". 2468b3b9c293SKonstantin Khlebnikov * See vm_normal_page() for details. 24691da177e4SLinus Torvalds */ 2470b3b9c293SKonstantin Khlebnikov if (is_cow_mapping(vma->vm_flags)) { 2471b3b9c293SKonstantin Khlebnikov if (addr != vma->vm_start || end != vma->vm_end) 2472b3b9c293SKonstantin Khlebnikov return -EINVAL; 24736aab341eSLinus Torvalds vma->vm_pgoff = pfn; 2474b3b9c293SKonstantin Khlebnikov } 2475b3b9c293SKonstantin Khlebnikov 24761c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP); 24771da177e4SLinus Torvalds 24781da177e4SLinus Torvalds BUG_ON(addr >= end); 24791da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 24801da177e4SLinus Torvalds pgd = pgd_offset(mm, addr); 24811da177e4SLinus Torvalds flush_cache_range(vma, addr, end); 24821da177e4SLinus Torvalds do { 24831da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 2484c2febafcSKirill A. Shutemov err = remap_p4d_range(mm, pgd, addr, next, 24851da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot); 24861da177e4SLinus Torvalds if (err) 248774ffa5a3SChristoph Hellwig return err; 24881da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 24892ab64037Svenkatesh.pallipadi@intel.com 249074ffa5a3SChristoph Hellwig return 0; 249174ffa5a3SChristoph Hellwig } 24922ab64037Svenkatesh.pallipadi@intel.com 249374ffa5a3SChristoph Hellwig /** 249474ffa5a3SChristoph Hellwig * remap_pfn_range - remap kernel memory to userspace 249574ffa5a3SChristoph Hellwig * @vma: user vma to map to 249674ffa5a3SChristoph Hellwig * @addr: target page aligned user address to start at 249774ffa5a3SChristoph Hellwig * @pfn: page frame number of kernel physical memory address 249874ffa5a3SChristoph Hellwig * @size: size of mapping area 249974ffa5a3SChristoph Hellwig * @prot: page protection flags for this mapping 250074ffa5a3SChristoph Hellwig * 250174ffa5a3SChristoph Hellwig * Note: this is only safe if the mm semaphore is held when called. 250274ffa5a3SChristoph Hellwig * 250374ffa5a3SChristoph Hellwig * Return: %0 on success, negative error code otherwise. 250474ffa5a3SChristoph Hellwig */ 250574ffa5a3SChristoph Hellwig int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 250674ffa5a3SChristoph Hellwig unsigned long pfn, unsigned long size, pgprot_t prot) 250774ffa5a3SChristoph Hellwig { 250874ffa5a3SChristoph Hellwig int err; 250974ffa5a3SChristoph Hellwig 251074ffa5a3SChristoph Hellwig err = track_pfn_remap(vma, &prot, pfn, addr, PAGE_ALIGN(size)); 251174ffa5a3SChristoph Hellwig if (err) 251274ffa5a3SChristoph Hellwig return -EINVAL; 251374ffa5a3SChristoph Hellwig 251474ffa5a3SChristoph Hellwig err = remap_pfn_range_notrack(vma, addr, pfn, size, prot); 251574ffa5a3SChristoph Hellwig if (err) 251668f48381SSuren Baghdasaryan untrack_pfn(vma, pfn, PAGE_ALIGN(size), true); 25171da177e4SLinus Torvalds return err; 25181da177e4SLinus Torvalds } 25191da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range); 25201da177e4SLinus Torvalds 2521b4cbb197SLinus Torvalds /** 2522b4cbb197SLinus Torvalds * vm_iomap_memory - remap memory to userspace 2523b4cbb197SLinus Torvalds * @vma: user vma to map to 2524abd69b9eSWang Wenhu * @start: start of the physical memory to be mapped 2525b4cbb197SLinus Torvalds * @len: size of area 2526b4cbb197SLinus Torvalds * 2527b4cbb197SLinus Torvalds * This is a simplified io_remap_pfn_range() for common driver use. The 2528b4cbb197SLinus Torvalds * driver just needs to give us the physical memory range to be mapped, 2529b4cbb197SLinus Torvalds * we'll figure out the rest from the vma information. 2530b4cbb197SLinus Torvalds * 2531b4cbb197SLinus Torvalds * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get 2532b4cbb197SLinus Torvalds * whatever write-combining details or similar. 2533a862f68aSMike Rapoport * 2534a862f68aSMike Rapoport * Return: %0 on success, negative error code otherwise. 2535b4cbb197SLinus Torvalds */ 2536b4cbb197SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len) 2537b4cbb197SLinus Torvalds { 2538b4cbb197SLinus Torvalds unsigned long vm_len, pfn, pages; 2539b4cbb197SLinus Torvalds 2540b4cbb197SLinus Torvalds /* Check that the physical memory area passed in looks valid */ 2541b4cbb197SLinus Torvalds if (start + len < start) 2542b4cbb197SLinus Torvalds return -EINVAL; 2543b4cbb197SLinus Torvalds /* 2544b4cbb197SLinus Torvalds * You *really* shouldn't map things that aren't page-aligned, 2545b4cbb197SLinus Torvalds * but we've historically allowed it because IO memory might 2546b4cbb197SLinus Torvalds * just have smaller alignment. 2547b4cbb197SLinus Torvalds */ 2548b4cbb197SLinus Torvalds len += start & ~PAGE_MASK; 2549b4cbb197SLinus Torvalds pfn = start >> PAGE_SHIFT; 2550b4cbb197SLinus Torvalds pages = (len + ~PAGE_MASK) >> PAGE_SHIFT; 2551b4cbb197SLinus Torvalds if (pfn + pages < pfn) 2552b4cbb197SLinus Torvalds return -EINVAL; 2553b4cbb197SLinus Torvalds 2554b4cbb197SLinus Torvalds /* We start the mapping 'vm_pgoff' pages into the area */ 2555b4cbb197SLinus Torvalds if (vma->vm_pgoff > pages) 2556b4cbb197SLinus Torvalds return -EINVAL; 2557b4cbb197SLinus Torvalds pfn += vma->vm_pgoff; 2558b4cbb197SLinus Torvalds pages -= vma->vm_pgoff; 2559b4cbb197SLinus Torvalds 2560b4cbb197SLinus Torvalds /* Can we fit all of the mapping? */ 2561b4cbb197SLinus Torvalds vm_len = vma->vm_end - vma->vm_start; 2562b4cbb197SLinus Torvalds if (vm_len >> PAGE_SHIFT > pages) 2563b4cbb197SLinus Torvalds return -EINVAL; 2564b4cbb197SLinus Torvalds 2565b4cbb197SLinus Torvalds /* Ok, let it rip */ 2566b4cbb197SLinus Torvalds return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot); 2567b4cbb197SLinus Torvalds } 2568b4cbb197SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory); 2569b4cbb197SLinus Torvalds 2570aee16b3cSJeremy Fitzhardinge static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd, 2571aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2572e80d3909SJoerg Roedel pte_fn_t fn, void *data, bool create, 2573e80d3909SJoerg Roedel pgtbl_mod_mask *mask) 2574aee16b3cSJeremy Fitzhardinge { 25758abb50c7SMiaohe Lin pte_t *pte, *mapped_pte; 2576be1db475SDaniel Axtens int err = 0; 25773f649ab7SKees Cook spinlock_t *ptl; 2578aee16b3cSJeremy Fitzhardinge 2579be1db475SDaniel Axtens if (create) { 25808abb50c7SMiaohe Lin mapped_pte = pte = (mm == &init_mm) ? 2581e80d3909SJoerg Roedel pte_alloc_kernel_track(pmd, addr, mask) : 2582aee16b3cSJeremy Fitzhardinge pte_alloc_map_lock(mm, pmd, addr, &ptl); 2583aee16b3cSJeremy Fitzhardinge if (!pte) 2584aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2585be1db475SDaniel Axtens } else { 25868abb50c7SMiaohe Lin mapped_pte = pte = (mm == &init_mm) ? 2587be1db475SDaniel Axtens pte_offset_kernel(pmd, addr) : 2588be1db475SDaniel Axtens pte_offset_map_lock(mm, pmd, addr, &ptl); 25893db82b93SHugh Dickins if (!pte) 25903db82b93SHugh Dickins return -EINVAL; 2591be1db475SDaniel Axtens } 2592aee16b3cSJeremy Fitzhardinge 259338e0edb1SJeremy Fitzhardinge arch_enter_lazy_mmu_mode(); 259438e0edb1SJeremy Fitzhardinge 2595eeb4a05fSChristoph Hellwig if (fn) { 2596aee16b3cSJeremy Fitzhardinge do { 2597c33c7948SRyan Roberts if (create || !pte_none(ptep_get(pte))) { 25988b1e0f81SAnshuman Khandual err = fn(pte++, addr, data); 2599aee16b3cSJeremy Fitzhardinge if (err) 2600aee16b3cSJeremy Fitzhardinge break; 2601be1db475SDaniel Axtens } 2602c36987e2SDaisuke Nishimura } while (addr += PAGE_SIZE, addr != end); 2603eeb4a05fSChristoph Hellwig } 2604e80d3909SJoerg Roedel *mask |= PGTBL_PTE_MODIFIED; 2605aee16b3cSJeremy Fitzhardinge 260638e0edb1SJeremy Fitzhardinge arch_leave_lazy_mmu_mode(); 260738e0edb1SJeremy Fitzhardinge 2608aee16b3cSJeremy Fitzhardinge if (mm != &init_mm) 26098abb50c7SMiaohe Lin pte_unmap_unlock(mapped_pte, ptl); 2610aee16b3cSJeremy Fitzhardinge return err; 2611aee16b3cSJeremy Fitzhardinge } 2612aee16b3cSJeremy Fitzhardinge 2613aee16b3cSJeremy Fitzhardinge static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud, 2614aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2615e80d3909SJoerg Roedel pte_fn_t fn, void *data, bool create, 2616e80d3909SJoerg Roedel pgtbl_mod_mask *mask) 2617aee16b3cSJeremy Fitzhardinge { 2618aee16b3cSJeremy Fitzhardinge pmd_t *pmd; 2619aee16b3cSJeremy Fitzhardinge unsigned long next; 2620be1db475SDaniel Axtens int err = 0; 2621aee16b3cSJeremy Fitzhardinge 2622ceb86879SAndi Kleen BUG_ON(pud_huge(*pud)); 2623ceb86879SAndi Kleen 2624be1db475SDaniel Axtens if (create) { 2625e80d3909SJoerg Roedel pmd = pmd_alloc_track(mm, pud, addr, mask); 2626aee16b3cSJeremy Fitzhardinge if (!pmd) 2627aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2628be1db475SDaniel Axtens } else { 2629be1db475SDaniel Axtens pmd = pmd_offset(pud, addr); 2630be1db475SDaniel Axtens } 2631aee16b3cSJeremy Fitzhardinge do { 2632aee16b3cSJeremy Fitzhardinge next = pmd_addr_end(addr, end); 26330c95cba4SNicholas Piggin if (pmd_none(*pmd) && !create) 26340c95cba4SNicholas Piggin continue; 26350c95cba4SNicholas Piggin if (WARN_ON_ONCE(pmd_leaf(*pmd))) 26360c95cba4SNicholas Piggin return -EINVAL; 26370c95cba4SNicholas Piggin if (!pmd_none(*pmd) && WARN_ON_ONCE(pmd_bad(*pmd))) { 26380c95cba4SNicholas Piggin if (!create) 26390c95cba4SNicholas Piggin continue; 26400c95cba4SNicholas Piggin pmd_clear_bad(pmd); 26410c95cba4SNicholas Piggin } 26420c95cba4SNicholas Piggin err = apply_to_pte_range(mm, pmd, addr, next, 26430c95cba4SNicholas Piggin fn, data, create, mask); 2644aee16b3cSJeremy Fitzhardinge if (err) 2645aee16b3cSJeremy Fitzhardinge break; 2646aee16b3cSJeremy Fitzhardinge } while (pmd++, addr = next, addr != end); 26470c95cba4SNicholas Piggin 2648aee16b3cSJeremy Fitzhardinge return err; 2649aee16b3cSJeremy Fitzhardinge } 2650aee16b3cSJeremy Fitzhardinge 2651c2febafcSKirill A. Shutemov static int apply_to_pud_range(struct mm_struct *mm, p4d_t *p4d, 2652aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2653e80d3909SJoerg Roedel pte_fn_t fn, void *data, bool create, 2654e80d3909SJoerg Roedel pgtbl_mod_mask *mask) 2655aee16b3cSJeremy Fitzhardinge { 2656aee16b3cSJeremy Fitzhardinge pud_t *pud; 2657aee16b3cSJeremy Fitzhardinge unsigned long next; 2658be1db475SDaniel Axtens int err = 0; 2659aee16b3cSJeremy Fitzhardinge 2660be1db475SDaniel Axtens if (create) { 2661e80d3909SJoerg Roedel pud = pud_alloc_track(mm, p4d, addr, mask); 2662aee16b3cSJeremy Fitzhardinge if (!pud) 2663aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2664be1db475SDaniel Axtens } else { 2665be1db475SDaniel Axtens pud = pud_offset(p4d, addr); 2666be1db475SDaniel Axtens } 2667aee16b3cSJeremy Fitzhardinge do { 2668aee16b3cSJeremy Fitzhardinge next = pud_addr_end(addr, end); 26690c95cba4SNicholas Piggin if (pud_none(*pud) && !create) 26700c95cba4SNicholas Piggin continue; 26710c95cba4SNicholas Piggin if (WARN_ON_ONCE(pud_leaf(*pud))) 26720c95cba4SNicholas Piggin return -EINVAL; 26730c95cba4SNicholas Piggin if (!pud_none(*pud) && WARN_ON_ONCE(pud_bad(*pud))) { 26740c95cba4SNicholas Piggin if (!create) 26750c95cba4SNicholas Piggin continue; 26760c95cba4SNicholas Piggin pud_clear_bad(pud); 26770c95cba4SNicholas Piggin } 26780c95cba4SNicholas Piggin err = apply_to_pmd_range(mm, pud, addr, next, 26790c95cba4SNicholas Piggin fn, data, create, mask); 2680aee16b3cSJeremy Fitzhardinge if (err) 2681aee16b3cSJeremy Fitzhardinge break; 2682aee16b3cSJeremy Fitzhardinge } while (pud++, addr = next, addr != end); 26830c95cba4SNicholas Piggin 2684aee16b3cSJeremy Fitzhardinge return err; 2685aee16b3cSJeremy Fitzhardinge } 2686aee16b3cSJeremy Fitzhardinge 2687c2febafcSKirill A. Shutemov static int apply_to_p4d_range(struct mm_struct *mm, pgd_t *pgd, 2688c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 2689e80d3909SJoerg Roedel pte_fn_t fn, void *data, bool create, 2690e80d3909SJoerg Roedel pgtbl_mod_mask *mask) 2691c2febafcSKirill A. Shutemov { 2692c2febafcSKirill A. Shutemov p4d_t *p4d; 2693c2febafcSKirill A. Shutemov unsigned long next; 2694be1db475SDaniel Axtens int err = 0; 2695c2febafcSKirill A. Shutemov 2696be1db475SDaniel Axtens if (create) { 2697e80d3909SJoerg Roedel p4d = p4d_alloc_track(mm, pgd, addr, mask); 2698c2febafcSKirill A. Shutemov if (!p4d) 2699c2febafcSKirill A. Shutemov return -ENOMEM; 2700be1db475SDaniel Axtens } else { 2701be1db475SDaniel Axtens p4d = p4d_offset(pgd, addr); 2702be1db475SDaniel Axtens } 2703c2febafcSKirill A. Shutemov do { 2704c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 27050c95cba4SNicholas Piggin if (p4d_none(*p4d) && !create) 27060c95cba4SNicholas Piggin continue; 27070c95cba4SNicholas Piggin if (WARN_ON_ONCE(p4d_leaf(*p4d))) 27080c95cba4SNicholas Piggin return -EINVAL; 27090c95cba4SNicholas Piggin if (!p4d_none(*p4d) && WARN_ON_ONCE(p4d_bad(*p4d))) { 27100c95cba4SNicholas Piggin if (!create) 27110c95cba4SNicholas Piggin continue; 27120c95cba4SNicholas Piggin p4d_clear_bad(p4d); 27130c95cba4SNicholas Piggin } 27140c95cba4SNicholas Piggin err = apply_to_pud_range(mm, p4d, addr, next, 27150c95cba4SNicholas Piggin fn, data, create, mask); 2716c2febafcSKirill A. Shutemov if (err) 2717c2febafcSKirill A. Shutemov break; 2718c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 27190c95cba4SNicholas Piggin 2720c2febafcSKirill A. Shutemov return err; 2721c2febafcSKirill A. Shutemov } 2722c2febafcSKirill A. Shutemov 2723be1db475SDaniel Axtens static int __apply_to_page_range(struct mm_struct *mm, unsigned long addr, 2724be1db475SDaniel Axtens unsigned long size, pte_fn_t fn, 2725be1db475SDaniel Axtens void *data, bool create) 2726be1db475SDaniel Axtens { 2727be1db475SDaniel Axtens pgd_t *pgd; 2728e80d3909SJoerg Roedel unsigned long start = addr, next; 2729be1db475SDaniel Axtens unsigned long end = addr + size; 2730e80d3909SJoerg Roedel pgtbl_mod_mask mask = 0; 2731be1db475SDaniel Axtens int err = 0; 2732be1db475SDaniel Axtens 2733be1db475SDaniel Axtens if (WARN_ON(addr >= end)) 2734be1db475SDaniel Axtens return -EINVAL; 2735be1db475SDaniel Axtens 2736be1db475SDaniel Axtens pgd = pgd_offset(mm, addr); 2737be1db475SDaniel Axtens do { 2738be1db475SDaniel Axtens next = pgd_addr_end(addr, end); 27390c95cba4SNicholas Piggin if (pgd_none(*pgd) && !create) 2740be1db475SDaniel Axtens continue; 27410c95cba4SNicholas Piggin if (WARN_ON_ONCE(pgd_leaf(*pgd))) 27420c95cba4SNicholas Piggin return -EINVAL; 27430c95cba4SNicholas Piggin if (!pgd_none(*pgd) && WARN_ON_ONCE(pgd_bad(*pgd))) { 27440c95cba4SNicholas Piggin if (!create) 27450c95cba4SNicholas Piggin continue; 27460c95cba4SNicholas Piggin pgd_clear_bad(pgd); 27470c95cba4SNicholas Piggin } 27480c95cba4SNicholas Piggin err = apply_to_p4d_range(mm, pgd, addr, next, 27490c95cba4SNicholas Piggin fn, data, create, &mask); 2750be1db475SDaniel Axtens if (err) 2751be1db475SDaniel Axtens break; 2752be1db475SDaniel Axtens } while (pgd++, addr = next, addr != end); 2753be1db475SDaniel Axtens 2754e80d3909SJoerg Roedel if (mask & ARCH_PAGE_TABLE_SYNC_MASK) 2755e80d3909SJoerg Roedel arch_sync_kernel_mappings(start, start + size); 2756e80d3909SJoerg Roedel 2757be1db475SDaniel Axtens return err; 2758be1db475SDaniel Axtens } 2759be1db475SDaniel Axtens 2760aee16b3cSJeremy Fitzhardinge /* 2761aee16b3cSJeremy Fitzhardinge * Scan a region of virtual memory, filling in page tables as necessary 2762aee16b3cSJeremy Fitzhardinge * and calling a provided function on each leaf page table. 2763aee16b3cSJeremy Fitzhardinge */ 2764aee16b3cSJeremy Fitzhardinge int apply_to_page_range(struct mm_struct *mm, unsigned long addr, 2765aee16b3cSJeremy Fitzhardinge unsigned long size, pte_fn_t fn, void *data) 2766aee16b3cSJeremy Fitzhardinge { 2767be1db475SDaniel Axtens return __apply_to_page_range(mm, addr, size, fn, data, true); 2768aee16b3cSJeremy Fitzhardinge } 2769aee16b3cSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(apply_to_page_range); 2770aee16b3cSJeremy Fitzhardinge 27711da177e4SLinus Torvalds /* 2772be1db475SDaniel Axtens * Scan a region of virtual memory, calling a provided function on 2773be1db475SDaniel Axtens * each leaf page table where it exists. 2774be1db475SDaniel Axtens * 2775be1db475SDaniel Axtens * Unlike apply_to_page_range, this does _not_ fill in page tables 2776be1db475SDaniel Axtens * where they are absent. 2777be1db475SDaniel Axtens */ 2778be1db475SDaniel Axtens int apply_to_existing_page_range(struct mm_struct *mm, unsigned long addr, 2779be1db475SDaniel Axtens unsigned long size, pte_fn_t fn, void *data) 2780be1db475SDaniel Axtens { 2781be1db475SDaniel Axtens return __apply_to_page_range(mm, addr, size, fn, data, false); 2782be1db475SDaniel Axtens } 2783be1db475SDaniel Axtens EXPORT_SYMBOL_GPL(apply_to_existing_page_range); 2784be1db475SDaniel Axtens 2785be1db475SDaniel Axtens /* 27869b4bdd2fSKirill A. Shutemov * handle_pte_fault chooses page fault handler according to an entry which was 27879b4bdd2fSKirill A. Shutemov * read non-atomically. Before making any commitment, on those architectures 27889b4bdd2fSKirill A. Shutemov * or configurations (e.g. i386 with PAE) which might give a mix of unmatched 27899b4bdd2fSKirill A. Shutemov * parts, do_swap_page must check under lock before unmapping the pte and 27909b4bdd2fSKirill A. Shutemov * proceeding (but do_wp_page is only called after already making such a check; 2791a335b2e1SRyota Ozaki * and do_anonymous_page can safely check later on). 27928f4e2101SHugh Dickins */ 27932ca99358SPeter Xu static inline int pte_unmap_same(struct vm_fault *vmf) 27948f4e2101SHugh Dickins { 27958f4e2101SHugh Dickins int same = 1; 2796923717cbSThomas Gleixner #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPTION) 27978f4e2101SHugh Dickins if (sizeof(pte_t) > sizeof(unsigned long)) { 2798c7ad0880SHugh Dickins spin_lock(vmf->ptl); 2799c33c7948SRyan Roberts same = pte_same(ptep_get(vmf->pte), vmf->orig_pte); 2800c7ad0880SHugh Dickins spin_unlock(vmf->ptl); 28018f4e2101SHugh Dickins } 28028f4e2101SHugh Dickins #endif 28032ca99358SPeter Xu pte_unmap(vmf->pte); 28042ca99358SPeter Xu vmf->pte = NULL; 28058f4e2101SHugh Dickins return same; 28068f4e2101SHugh Dickins } 28078f4e2101SHugh Dickins 2808a873dfe1STony Luck /* 2809a873dfe1STony Luck * Return: 2810a873dfe1STony Luck * 0: copied succeeded 2811a873dfe1STony Luck * -EHWPOISON: copy failed due to hwpoison in source page 2812a873dfe1STony Luck * -EAGAIN: copied failed (some other reason) 2813a873dfe1STony Luck */ 2814a873dfe1STony Luck static inline int __wp_page_copy_user(struct page *dst, struct page *src, 281583d116c5SJia He struct vm_fault *vmf) 28166aab341eSLinus Torvalds { 2817a873dfe1STony Luck int ret; 281883d116c5SJia He void *kaddr; 281983d116c5SJia He void __user *uaddr; 282083d116c5SJia He struct vm_area_struct *vma = vmf->vma; 282183d116c5SJia He struct mm_struct *mm = vma->vm_mm; 282283d116c5SJia He unsigned long addr = vmf->address; 282383d116c5SJia He 282483d116c5SJia He if (likely(src)) { 2825d302c239STony Luck if (copy_mc_user_highpage(dst, src, addr, vma)) { 2826d302c239STony Luck memory_failure_queue(page_to_pfn(src), 0); 2827a873dfe1STony Luck return -EHWPOISON; 2828d302c239STony Luck } 2829a873dfe1STony Luck return 0; 283083d116c5SJia He } 283183d116c5SJia He 28326aab341eSLinus Torvalds /* 28336aab341eSLinus Torvalds * If the source page was a PFN mapping, we don't have 28346aab341eSLinus Torvalds * a "struct page" for it. We do a best-effort copy by 28356aab341eSLinus Torvalds * just copying from the original user address. If that 28366aab341eSLinus Torvalds * fails, we just zero-fill it. Live with it. 28376aab341eSLinus Torvalds */ 283883d116c5SJia He kaddr = kmap_atomic(dst); 283983d116c5SJia He uaddr = (void __user *)(addr & PAGE_MASK); 284083d116c5SJia He 284183d116c5SJia He /* 284283d116c5SJia He * On architectures with software "accessed" bits, we would 284383d116c5SJia He * take a double page fault, so mark it accessed here. 284483d116c5SJia He */ 28453db82b93SHugh Dickins vmf->pte = NULL; 2846e1fd09e3SYu Zhao if (!arch_has_hw_pte_young() && !pte_young(vmf->orig_pte)) { 284783d116c5SJia He pte_t entry; 284883d116c5SJia He 284983d116c5SJia He vmf->pte = pte_offset_map_lock(mm, vmf->pmd, addr, &vmf->ptl); 2850c33c7948SRyan Roberts if (unlikely(!vmf->pte || !pte_same(ptep_get(vmf->pte), vmf->orig_pte))) { 285183d116c5SJia He /* 285283d116c5SJia He * Other thread has already handled the fault 28537df67697SBibo Mao * and update local tlb only 285483d116c5SJia He */ 2855a92cbb82SHugh Dickins if (vmf->pte) 28567df67697SBibo Mao update_mmu_tlb(vma, addr, vmf->pte); 2857a873dfe1STony Luck ret = -EAGAIN; 285883d116c5SJia He goto pte_unlock; 285983d116c5SJia He } 286083d116c5SJia He 286183d116c5SJia He entry = pte_mkyoung(vmf->orig_pte); 286283d116c5SJia He if (ptep_set_access_flags(vma, addr, vmf->pte, entry, 0)) 28635003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vma, addr, vmf->pte, 1); 286483d116c5SJia He } 28655d2a2dbbSLinus Torvalds 28665d2a2dbbSLinus Torvalds /* 28675d2a2dbbSLinus Torvalds * This really shouldn't fail, because the page is there 28685d2a2dbbSLinus Torvalds * in the page tables. But it might just be unreadable, 28695d2a2dbbSLinus Torvalds * in which case we just give up and fill the result with 28705d2a2dbbSLinus Torvalds * zeroes. 28715d2a2dbbSLinus Torvalds */ 287283d116c5SJia He if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) { 28733db82b93SHugh Dickins if (vmf->pte) 2874c3e5ea6eSKirill A. Shutemov goto warn; 2875c3e5ea6eSKirill A. Shutemov 2876c3e5ea6eSKirill A. Shutemov /* Re-validate under PTL if the page is still mapped */ 2877c3e5ea6eSKirill A. Shutemov vmf->pte = pte_offset_map_lock(mm, vmf->pmd, addr, &vmf->ptl); 2878c33c7948SRyan Roberts if (unlikely(!vmf->pte || !pte_same(ptep_get(vmf->pte), vmf->orig_pte))) { 28797df67697SBibo Mao /* The PTE changed under us, update local tlb */ 2880a92cbb82SHugh Dickins if (vmf->pte) 28817df67697SBibo Mao update_mmu_tlb(vma, addr, vmf->pte); 2882a873dfe1STony Luck ret = -EAGAIN; 2883c3e5ea6eSKirill A. Shutemov goto pte_unlock; 2884c3e5ea6eSKirill A. Shutemov } 2885c3e5ea6eSKirill A. Shutemov 2886c3e5ea6eSKirill A. Shutemov /* 2887985ba004SEthon Paul * The same page can be mapped back since last copy attempt. 2888c3e5ea6eSKirill A. Shutemov * Try to copy again under PTL. 2889c3e5ea6eSKirill A. Shutemov */ 2890c3e5ea6eSKirill A. Shutemov if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) { 289183d116c5SJia He /* 289283d116c5SJia He * Give a warn in case there can be some obscure 289383d116c5SJia He * use-case 289483d116c5SJia He */ 2895c3e5ea6eSKirill A. Shutemov warn: 289683d116c5SJia He WARN_ON_ONCE(1); 28973ecb01dfSJan Beulich clear_page(kaddr); 289883d116c5SJia He } 2899c3e5ea6eSKirill A. Shutemov } 290083d116c5SJia He 2901a873dfe1STony Luck ret = 0; 290283d116c5SJia He 290383d116c5SJia He pte_unlock: 29043db82b93SHugh Dickins if (vmf->pte) 290583d116c5SJia He pte_unmap_unlock(vmf->pte, vmf->ptl); 29069b04c5feSCong Wang kunmap_atomic(kaddr); 2907c4ec7b0dSDmitriy Monakhov flush_dcache_page(dst); 290883d116c5SJia He 290983d116c5SJia He return ret; 29106aab341eSLinus Torvalds } 29116aab341eSLinus Torvalds 2912c20cd45eSMichal Hocko static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma) 2913c20cd45eSMichal Hocko { 2914c20cd45eSMichal Hocko struct file *vm_file = vma->vm_file; 2915c20cd45eSMichal Hocko 2916c20cd45eSMichal Hocko if (vm_file) 2917c20cd45eSMichal Hocko return mapping_gfp_mask(vm_file->f_mapping) | __GFP_FS | __GFP_IO; 2918c20cd45eSMichal Hocko 2919c20cd45eSMichal Hocko /* 2920c20cd45eSMichal Hocko * Special mappings (e.g. VDSO) do not have any file so fake 2921c20cd45eSMichal Hocko * a default GFP_KERNEL for them. 2922c20cd45eSMichal Hocko */ 2923c20cd45eSMichal Hocko return GFP_KERNEL; 2924c20cd45eSMichal Hocko } 2925c20cd45eSMichal Hocko 29261da177e4SLinus Torvalds /* 2927fb09a464SKirill A. Shutemov * Notify the address space that the page is about to become writable so that 2928fb09a464SKirill A. Shutemov * it can prohibit this or wait for the page to get into an appropriate state. 2929fb09a464SKirill A. Shutemov * 2930fb09a464SKirill A. Shutemov * We do this without the lock held, so that it can sleep if it needs to. 2931fb09a464SKirill A. Shutemov */ 293286aa6998SSidhartha Kumar static vm_fault_t do_page_mkwrite(struct vm_fault *vmf, struct folio *folio) 2933fb09a464SKirill A. Shutemov { 29342b740303SSouptick Joarder vm_fault_t ret; 293538b8cb7fSJan Kara unsigned int old_flags = vmf->flags; 2936fb09a464SKirill A. Shutemov 293738b8cb7fSJan Kara vmf->flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE; 2938fb09a464SKirill A. Shutemov 2939dc617f29SDarrick J. Wong if (vmf->vma->vm_file && 2940dc617f29SDarrick J. Wong IS_SWAPFILE(vmf->vma->vm_file->f_mapping->host)) 2941dc617f29SDarrick J. Wong return VM_FAULT_SIGBUS; 2942dc617f29SDarrick J. Wong 294311bac800SDave Jiang ret = vmf->vma->vm_ops->page_mkwrite(vmf); 294438b8cb7fSJan Kara /* Restore original flags so that caller is not surprised */ 294538b8cb7fSJan Kara vmf->flags = old_flags; 2946fb09a464SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) 2947fb09a464SKirill A. Shutemov return ret; 2948fb09a464SKirill A. Shutemov if (unlikely(!(ret & VM_FAULT_LOCKED))) { 29493d243659SSidhartha Kumar folio_lock(folio); 29503d243659SSidhartha Kumar if (!folio->mapping) { 29513d243659SSidhartha Kumar folio_unlock(folio); 2952fb09a464SKirill A. Shutemov return 0; /* retry */ 2953fb09a464SKirill A. Shutemov } 2954fb09a464SKirill A. Shutemov ret |= VM_FAULT_LOCKED; 2955fb09a464SKirill A. Shutemov } else 29563d243659SSidhartha Kumar VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); 2957fb09a464SKirill A. Shutemov return ret; 2958fb09a464SKirill A. Shutemov } 2959fb09a464SKirill A. Shutemov 2960fb09a464SKirill A. Shutemov /* 296197ba0c2bSJan Kara * Handle dirtying of a page in shared file mapping on a write fault. 29624e047f89SShachar Raindel * 296397ba0c2bSJan Kara * The function expects the page to be locked and unlocks it. 29644e047f89SShachar Raindel */ 296589b15332SJohannes Weiner static vm_fault_t fault_dirty_shared_page(struct vm_fault *vmf) 29664e047f89SShachar Raindel { 296789b15332SJohannes Weiner struct vm_area_struct *vma = vmf->vma; 29684e047f89SShachar Raindel struct address_space *mapping; 296915b4919aSZhangPeng struct folio *folio = page_folio(vmf->page); 297097ba0c2bSJan Kara bool dirtied; 297197ba0c2bSJan Kara bool page_mkwrite = vma->vm_ops && vma->vm_ops->page_mkwrite; 29724e047f89SShachar Raindel 297315b4919aSZhangPeng dirtied = folio_mark_dirty(folio); 297415b4919aSZhangPeng VM_BUG_ON_FOLIO(folio_test_anon(folio), folio); 297597ba0c2bSJan Kara /* 297615b4919aSZhangPeng * Take a local copy of the address_space - folio.mapping may be zeroed 297715b4919aSZhangPeng * by truncate after folio_unlock(). The address_space itself remains 297815b4919aSZhangPeng * pinned by vma->vm_file's reference. We rely on folio_unlock()'s 297997ba0c2bSJan Kara * release semantics to prevent the compiler from undoing this copying. 298097ba0c2bSJan Kara */ 298115b4919aSZhangPeng mapping = folio_raw_mapping(folio); 298215b4919aSZhangPeng folio_unlock(folio); 29834e047f89SShachar Raindel 29844e047f89SShachar Raindel if (!page_mkwrite) 29854e047f89SShachar Raindel file_update_time(vma->vm_file); 298689b15332SJohannes Weiner 298789b15332SJohannes Weiner /* 298889b15332SJohannes Weiner * Throttle page dirtying rate down to writeback speed. 298989b15332SJohannes Weiner * 299089b15332SJohannes Weiner * mapping may be NULL here because some device drivers do not 299189b15332SJohannes Weiner * set page.mapping but still dirty their pages 299289b15332SJohannes Weiner * 2993c1e8d7c6SMichel Lespinasse * Drop the mmap_lock before waiting on IO, if we can. The file 299489b15332SJohannes Weiner * is pinning the mapping, as per above. 299589b15332SJohannes Weiner */ 299689b15332SJohannes Weiner if ((dirtied || page_mkwrite) && mapping) { 299789b15332SJohannes Weiner struct file *fpin; 299889b15332SJohannes Weiner 299989b15332SJohannes Weiner fpin = maybe_unlock_mmap_for_io(vmf, NULL); 300089b15332SJohannes Weiner balance_dirty_pages_ratelimited(mapping); 300189b15332SJohannes Weiner if (fpin) { 300289b15332SJohannes Weiner fput(fpin); 3003d9272525SPeter Xu return VM_FAULT_COMPLETED; 300489b15332SJohannes Weiner } 300589b15332SJohannes Weiner } 300689b15332SJohannes Weiner 300789b15332SJohannes Weiner return 0; 30084e047f89SShachar Raindel } 30094e047f89SShachar Raindel 301097ba0c2bSJan Kara /* 30114e047f89SShachar Raindel * Handle write page faults for pages that can be reused in the current vma 30124e047f89SShachar Raindel * 30134e047f89SShachar Raindel * This can happen either due to the mapping being with the VM_SHARED flag, 30144e047f89SShachar Raindel * or due to us being the last reference standing to the page. In either 30154e047f89SShachar Raindel * case, all we need to do here is to mark the page as writable and update 30164e047f89SShachar Raindel * any related book-keeping. 30174e047f89SShachar Raindel */ 3018997dd98dSJan Kara static inline void wp_page_reuse(struct vm_fault *vmf) 301982b0f8c3SJan Kara __releases(vmf->ptl) 30204e047f89SShachar Raindel { 302182b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3022a41b70d6SJan Kara struct page *page = vmf->page; 30234e047f89SShachar Raindel pte_t entry; 30246c287605SDavid Hildenbrand 3025c89357e2SDavid Hildenbrand VM_BUG_ON(!(vmf->flags & FAULT_FLAG_WRITE)); 3026cdb281e6SQi Zheng VM_BUG_ON(page && PageAnon(page) && !PageAnonExclusive(page)); 30276c287605SDavid Hildenbrand 30284e047f89SShachar Raindel /* 30294e047f89SShachar Raindel * Clear the pages cpupid information as the existing 30304e047f89SShachar Raindel * information potentially belongs to a now completely 30314e047f89SShachar Raindel * unrelated process. 30324e047f89SShachar Raindel */ 30334e047f89SShachar Raindel if (page) 30344e047f89SShachar Raindel page_cpupid_xchg_last(page, (1 << LAST_CPUPID_SHIFT) - 1); 30354e047f89SShachar Raindel 30362994302bSJan Kara flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); 30372994302bSJan Kara entry = pte_mkyoung(vmf->orig_pte); 30384e047f89SShachar Raindel entry = maybe_mkwrite(pte_mkdirty(entry), vma); 303982b0f8c3SJan Kara if (ptep_set_access_flags(vma, vmf->address, vmf->pte, entry, 1)) 30405003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vma, vmf->address, vmf->pte, 1); 304182b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3042798a6b87SPeter Xu count_vm_event(PGREUSE); 30434e047f89SShachar Raindel } 30444e047f89SShachar Raindel 30454e047f89SShachar Raindel /* 3046c89357e2SDavid Hildenbrand * Handle the case of a page which we actually need to copy to a new page, 3047c89357e2SDavid Hildenbrand * either due to COW or unsharing. 30482f38ab2cSShachar Raindel * 3049c1e8d7c6SMichel Lespinasse * Called with mmap_lock locked and the old page referenced, but 30502f38ab2cSShachar Raindel * without the ptl held. 30512f38ab2cSShachar Raindel * 30522f38ab2cSShachar Raindel * High level logic flow: 30532f38ab2cSShachar Raindel * 30542f38ab2cSShachar Raindel * - Allocate a page, copy the content of the old page to the new one. 30552f38ab2cSShachar Raindel * - Handle book keeping and accounting - cgroups, mmu-notifiers, etc. 30562f38ab2cSShachar Raindel * - Take the PTL. If the pte changed, bail out and release the allocated page 30572f38ab2cSShachar Raindel * - If the pte is still the way we remember it, update the page table and all 30582f38ab2cSShachar Raindel * relevant references. This includes dropping the reference the page-table 30592f38ab2cSShachar Raindel * held to the old page, as well as updating the rmap. 30602f38ab2cSShachar Raindel * - In any case, unlock the PTL and drop the reference we took to the old page. 30612f38ab2cSShachar Raindel */ 30622b740303SSouptick Joarder static vm_fault_t wp_page_copy(struct vm_fault *vmf) 30632f38ab2cSShachar Raindel { 3064c89357e2SDavid Hildenbrand const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE; 306582b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3066bae473a4SKirill A. Shutemov struct mm_struct *mm = vma->vm_mm; 306728d41a48SMatthew Wilcox (Oracle) struct folio *old_folio = NULL; 306828d41a48SMatthew Wilcox (Oracle) struct folio *new_folio = NULL; 30692f38ab2cSShachar Raindel pte_t entry; 30702f38ab2cSShachar Raindel int page_copied = 0; 3071ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 3072a873dfe1STony Luck int ret; 30732f38ab2cSShachar Raindel 3074662ce1dcSYang Yang delayacct_wpcopy_start(); 3075662ce1dcSYang Yang 307628d41a48SMatthew Wilcox (Oracle) if (vmf->page) 307728d41a48SMatthew Wilcox (Oracle) old_folio = page_folio(vmf->page); 30782f38ab2cSShachar Raindel if (unlikely(anon_vma_prepare(vma))) 30792f38ab2cSShachar Raindel goto oom; 30802f38ab2cSShachar Raindel 30812994302bSJan Kara if (is_zero_pfn(pte_pfn(vmf->orig_pte))) { 30826bc56a4dSMatthew Wilcox (Oracle) new_folio = vma_alloc_zeroed_movable_folio(vma, vmf->address); 30836bc56a4dSMatthew Wilcox (Oracle) if (!new_folio) 30842f38ab2cSShachar Raindel goto oom; 30852f38ab2cSShachar Raindel } else { 308628d41a48SMatthew Wilcox (Oracle) new_folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, vma, 308728d41a48SMatthew Wilcox (Oracle) vmf->address, false); 308828d41a48SMatthew Wilcox (Oracle) if (!new_folio) 30892f38ab2cSShachar Raindel goto oom; 309083d116c5SJia He 309128d41a48SMatthew Wilcox (Oracle) ret = __wp_page_copy_user(&new_folio->page, vmf->page, vmf); 3092a873dfe1STony Luck if (ret) { 309383d116c5SJia He /* 309483d116c5SJia He * COW failed, if the fault was solved by other, 309583d116c5SJia He * it's fine. If not, userspace would re-fault on 309683d116c5SJia He * the same address and we will handle the fault 309783d116c5SJia He * from the second attempt. 3098a873dfe1STony Luck * The -EHWPOISON case will not be retried. 309983d116c5SJia He */ 310028d41a48SMatthew Wilcox (Oracle) folio_put(new_folio); 310128d41a48SMatthew Wilcox (Oracle) if (old_folio) 310228d41a48SMatthew Wilcox (Oracle) folio_put(old_folio); 3103662ce1dcSYang Yang 3104662ce1dcSYang Yang delayacct_wpcopy_end(); 3105a873dfe1STony Luck return ret == -EHWPOISON ? VM_FAULT_HWPOISON : 0; 310683d116c5SJia He } 310728d41a48SMatthew Wilcox (Oracle) kmsan_copy_page_meta(&new_folio->page, vmf->page); 31082f38ab2cSShachar Raindel } 31092f38ab2cSShachar Raindel 311028d41a48SMatthew Wilcox (Oracle) if (mem_cgroup_charge(new_folio, mm, GFP_KERNEL)) 31112f38ab2cSShachar Raindel goto oom_free_new; 31124d4f75bfSKefeng Wang folio_throttle_swaprate(new_folio, GFP_KERNEL); 31132f38ab2cSShachar Raindel 311428d41a48SMatthew Wilcox (Oracle) __folio_mark_uptodate(new_folio); 3115eb3c24f3SMel Gorman 31167d4a8be0SAlistair Popple mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, 31176f4f13e8SJérôme Glisse vmf->address & PAGE_MASK, 3118ac46d4f3SJérôme Glisse (vmf->address & PAGE_MASK) + PAGE_SIZE); 3119ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 31202f38ab2cSShachar Raindel 31212f38ab2cSShachar Raindel /* 31222f38ab2cSShachar Raindel * Re-check the pte - we dropped the lock 31232f38ab2cSShachar Raindel */ 312482b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(mm, vmf->pmd, vmf->address, &vmf->ptl); 3125c33c7948SRyan Roberts if (likely(vmf->pte && pte_same(ptep_get(vmf->pte), vmf->orig_pte))) { 312628d41a48SMatthew Wilcox (Oracle) if (old_folio) { 312728d41a48SMatthew Wilcox (Oracle) if (!folio_test_anon(old_folio)) { 312828d41a48SMatthew Wilcox (Oracle) dec_mm_counter(mm, mm_counter_file(&old_folio->page)); 3129f1a79412SShakeel Butt inc_mm_counter(mm, MM_ANONPAGES); 31302f38ab2cSShachar Raindel } 31312f38ab2cSShachar Raindel } else { 31326080d19fSxu xin ksm_might_unmap_zero_page(mm, vmf->orig_pte); 3133f1a79412SShakeel Butt inc_mm_counter(mm, MM_ANONPAGES); 31342f38ab2cSShachar Raindel } 31352994302bSJan Kara flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); 313628d41a48SMatthew Wilcox (Oracle) entry = mk_pte(&new_folio->page, vma->vm_page_prot); 313750c25ee9SThomas Bogendoerfer entry = pte_sw_mkyoung(entry); 3138c89357e2SDavid Hildenbrand if (unlikely(unshare)) { 3139c89357e2SDavid Hildenbrand if (pte_soft_dirty(vmf->orig_pte)) 3140c89357e2SDavid Hildenbrand entry = pte_mksoft_dirty(entry); 3141c89357e2SDavid Hildenbrand if (pte_uffd_wp(vmf->orig_pte)) 3142c89357e2SDavid Hildenbrand entry = pte_mkuffd_wp(entry); 3143c89357e2SDavid Hildenbrand } else { 31442f38ab2cSShachar Raindel entry = maybe_mkwrite(pte_mkdirty(entry), vma); 3145c89357e2SDavid Hildenbrand } 3146111fe718SNicholas Piggin 31472f38ab2cSShachar Raindel /* 31482f38ab2cSShachar Raindel * Clear the pte entry and flush it first, before updating the 3149111fe718SNicholas Piggin * pte with the new entry, to keep TLBs on different CPUs in 3150111fe718SNicholas Piggin * sync. This code used to set the new PTE then flush TLBs, but 3151111fe718SNicholas Piggin * that left a window where the new PTE could be loaded into 3152111fe718SNicholas Piggin * some TLBs while the old PTE remains in others. 31532f38ab2cSShachar Raindel */ 3154ec8832d0SAlistair Popple ptep_clear_flush(vma, vmf->address, vmf->pte); 315528d41a48SMatthew Wilcox (Oracle) folio_add_new_anon_rmap(new_folio, vma, vmf->address); 315628d41a48SMatthew Wilcox (Oracle) folio_add_lru_vma(new_folio, vma); 31572f38ab2cSShachar Raindel /* 31582f38ab2cSShachar Raindel * We call the notify macro here because, when using secondary 31592f38ab2cSShachar Raindel * mmu page tables (such as kvm shadow page tables), we want the 31602f38ab2cSShachar Raindel * new page to be mapped directly into the secondary page table. 31612f38ab2cSShachar Raindel */ 3162c89357e2SDavid Hildenbrand BUG_ON(unshare && pte_write(entry)); 316382b0f8c3SJan Kara set_pte_at_notify(mm, vmf->address, vmf->pte, entry); 31645003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vma, vmf->address, vmf->pte, 1); 316528d41a48SMatthew Wilcox (Oracle) if (old_folio) { 31662f38ab2cSShachar Raindel /* 31672f38ab2cSShachar Raindel * Only after switching the pte to the new page may 31682f38ab2cSShachar Raindel * we remove the mapcount here. Otherwise another 31692f38ab2cSShachar Raindel * process may come and find the rmap count decremented 31702f38ab2cSShachar Raindel * before the pte is switched to the new page, and 31712f38ab2cSShachar Raindel * "reuse" the old page writing into it while our pte 31722f38ab2cSShachar Raindel * here still points into it and can be read by other 31732f38ab2cSShachar Raindel * threads. 31742f38ab2cSShachar Raindel * 31752f38ab2cSShachar Raindel * The critical issue is to order this 31762f38ab2cSShachar Raindel * page_remove_rmap with the ptp_clear_flush above. 31772f38ab2cSShachar Raindel * Those stores are ordered by (if nothing else,) 31782f38ab2cSShachar Raindel * the barrier present in the atomic_add_negative 31792f38ab2cSShachar Raindel * in page_remove_rmap. 31802f38ab2cSShachar Raindel * 31812f38ab2cSShachar Raindel * Then the TLB flush in ptep_clear_flush ensures that 31822f38ab2cSShachar Raindel * no process can access the old page before the 31832f38ab2cSShachar Raindel * decremented mapcount is visible. And the old page 31842f38ab2cSShachar Raindel * cannot be reused until after the decremented 31852f38ab2cSShachar Raindel * mapcount is visible. So transitively, TLBs to 31862f38ab2cSShachar Raindel * old page will be flushed before it can be reused. 31872f38ab2cSShachar Raindel */ 318828d41a48SMatthew Wilcox (Oracle) page_remove_rmap(vmf->page, vma, false); 31892f38ab2cSShachar Raindel } 31902f38ab2cSShachar Raindel 31912f38ab2cSShachar Raindel /* Free the old page.. */ 319228d41a48SMatthew Wilcox (Oracle) new_folio = old_folio; 31932f38ab2cSShachar Raindel page_copied = 1; 31943db82b93SHugh Dickins pte_unmap_unlock(vmf->pte, vmf->ptl); 31953db82b93SHugh Dickins } else if (vmf->pte) { 31967df67697SBibo Mao update_mmu_tlb(vma, vmf->address, vmf->pte); 31973db82b93SHugh Dickins pte_unmap_unlock(vmf->pte, vmf->ptl); 31982f38ab2cSShachar Raindel } 31992f38ab2cSShachar Raindel 3200ec8832d0SAlistair Popple mmu_notifier_invalidate_range_end(&range); 32013db82b93SHugh Dickins 32023db82b93SHugh Dickins if (new_folio) 32033db82b93SHugh Dickins folio_put(new_folio); 320428d41a48SMatthew Wilcox (Oracle) if (old_folio) { 3205f4c4a3f4SHuang Ying if (page_copied) 320628d41a48SMatthew Wilcox (Oracle) free_swap_cache(&old_folio->page); 320728d41a48SMatthew Wilcox (Oracle) folio_put(old_folio); 32082f38ab2cSShachar Raindel } 3209662ce1dcSYang Yang 3210662ce1dcSYang Yang delayacct_wpcopy_end(); 3211cb8d8633SDavid Hildenbrand return 0; 32122f38ab2cSShachar Raindel oom_free_new: 321328d41a48SMatthew Wilcox (Oracle) folio_put(new_folio); 32142f38ab2cSShachar Raindel oom: 321528d41a48SMatthew Wilcox (Oracle) if (old_folio) 321628d41a48SMatthew Wilcox (Oracle) folio_put(old_folio); 3217662ce1dcSYang Yang 3218662ce1dcSYang Yang delayacct_wpcopy_end(); 32192f38ab2cSShachar Raindel return VM_FAULT_OOM; 32202f38ab2cSShachar Raindel } 32212f38ab2cSShachar Raindel 322266a6197cSJan Kara /** 322366a6197cSJan Kara * finish_mkwrite_fault - finish page fault for a shared mapping, making PTE 322466a6197cSJan Kara * writeable once the page is prepared 322566a6197cSJan Kara * 322666a6197cSJan Kara * @vmf: structure describing the fault 322766a6197cSJan Kara * 322866a6197cSJan Kara * This function handles all that is needed to finish a write page fault in a 322966a6197cSJan Kara * shared mapping due to PTE being read-only once the mapped page is prepared. 3230a862f68aSMike Rapoport * It handles locking of PTE and modifying it. 323166a6197cSJan Kara * 323266a6197cSJan Kara * The function expects the page to be locked or other protection against 323366a6197cSJan Kara * concurrent faults / writeback (such as DAX radix tree locks). 3234a862f68aSMike Rapoport * 32352797e79fSLiu Xiang * Return: %0 on success, %VM_FAULT_NOPAGE when PTE got changed before 3236a862f68aSMike Rapoport * we acquired PTE lock. 323766a6197cSJan Kara */ 32382b740303SSouptick Joarder vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf) 323966a6197cSJan Kara { 324066a6197cSJan Kara WARN_ON_ONCE(!(vmf->vma->vm_flags & VM_SHARED)); 324166a6197cSJan Kara vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, vmf->address, 324266a6197cSJan Kara &vmf->ptl); 32433db82b93SHugh Dickins if (!vmf->pte) 32443db82b93SHugh Dickins return VM_FAULT_NOPAGE; 324566a6197cSJan Kara /* 324666a6197cSJan Kara * We might have raced with another page fault while we released the 324766a6197cSJan Kara * pte_offset_map_lock. 324866a6197cSJan Kara */ 3249c33c7948SRyan Roberts if (!pte_same(ptep_get(vmf->pte), vmf->orig_pte)) { 32507df67697SBibo Mao update_mmu_tlb(vmf->vma, vmf->address, vmf->pte); 325166a6197cSJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3252a19e2553SJan Kara return VM_FAULT_NOPAGE; 325366a6197cSJan Kara } 325466a6197cSJan Kara wp_page_reuse(vmf); 3255a19e2553SJan Kara return 0; 325666a6197cSJan Kara } 325766a6197cSJan Kara 3258dd906184SBoaz Harrosh /* 3259dd906184SBoaz Harrosh * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED 3260dd906184SBoaz Harrosh * mapping 3261dd906184SBoaz Harrosh */ 32622b740303SSouptick Joarder static vm_fault_t wp_pfn_shared(struct vm_fault *vmf) 3263dd906184SBoaz Harrosh { 326482b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3265bae473a4SKirill A. Shutemov 3266dd906184SBoaz Harrosh if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) { 32672b740303SSouptick Joarder vm_fault_t ret; 3268dd906184SBoaz Harrosh 326982b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3270063e60d8SMatthew Wilcox (Oracle) if (vmf->flags & FAULT_FLAG_VMA_LOCK) { 3271063e60d8SMatthew Wilcox (Oracle) vma_end_read(vmf->vma); 3272063e60d8SMatthew Wilcox (Oracle) return VM_FAULT_RETRY; 3273063e60d8SMatthew Wilcox (Oracle) } 3274063e60d8SMatthew Wilcox (Oracle) 3275fe82221fSJan Kara vmf->flags |= FAULT_FLAG_MKWRITE; 327611bac800SDave Jiang ret = vma->vm_ops->pfn_mkwrite(vmf); 32772f89dc12SJan Kara if (ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)) 3278dd906184SBoaz Harrosh return ret; 327966a6197cSJan Kara return finish_mkwrite_fault(vmf); 3280dd906184SBoaz Harrosh } 3281997dd98dSJan Kara wp_page_reuse(vmf); 3282cb8d8633SDavid Hildenbrand return 0; 3283dd906184SBoaz Harrosh } 3284dd906184SBoaz Harrosh 32855a97858bSSidhartha Kumar static vm_fault_t wp_page_shared(struct vm_fault *vmf, struct folio *folio) 328682b0f8c3SJan Kara __releases(vmf->ptl) 328793e478d4SShachar Raindel { 328882b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3289cb8d8633SDavid Hildenbrand vm_fault_t ret = 0; 329093e478d4SShachar Raindel 32915a97858bSSidhartha Kumar folio_get(folio); 329293e478d4SShachar Raindel 329393e478d4SShachar Raindel if (vma->vm_ops && vma->vm_ops->page_mkwrite) { 32942b740303SSouptick Joarder vm_fault_t tmp; 329593e478d4SShachar Raindel 329682b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3297063e60d8SMatthew Wilcox (Oracle) if (vmf->flags & FAULT_FLAG_VMA_LOCK) { 3298063e60d8SMatthew Wilcox (Oracle) folio_put(folio); 3299063e60d8SMatthew Wilcox (Oracle) vma_end_read(vmf->vma); 3300063e60d8SMatthew Wilcox (Oracle) return VM_FAULT_RETRY; 3301063e60d8SMatthew Wilcox (Oracle) } 3302063e60d8SMatthew Wilcox (Oracle) 330386aa6998SSidhartha Kumar tmp = do_page_mkwrite(vmf, folio); 330493e478d4SShachar Raindel if (unlikely(!tmp || (tmp & 330593e478d4SShachar Raindel (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { 33065a97858bSSidhartha Kumar folio_put(folio); 330793e478d4SShachar Raindel return tmp; 330893e478d4SShachar Raindel } 330966a6197cSJan Kara tmp = finish_mkwrite_fault(vmf); 3310a19e2553SJan Kara if (unlikely(tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) { 33115a97858bSSidhartha Kumar folio_unlock(folio); 33125a97858bSSidhartha Kumar folio_put(folio); 331366a6197cSJan Kara return tmp; 331493e478d4SShachar Raindel } 331566a6197cSJan Kara } else { 3316997dd98dSJan Kara wp_page_reuse(vmf); 33175a97858bSSidhartha Kumar folio_lock(folio); 331893e478d4SShachar Raindel } 331989b15332SJohannes Weiner ret |= fault_dirty_shared_page(vmf); 33205a97858bSSidhartha Kumar folio_put(folio); 332193e478d4SShachar Raindel 332289b15332SJohannes Weiner return ret; 332393e478d4SShachar Raindel } 332493e478d4SShachar Raindel 33252f38ab2cSShachar Raindel /* 3326c89357e2SDavid Hildenbrand * This routine handles present pages, when 3327c89357e2SDavid Hildenbrand * * users try to write to a shared page (FAULT_FLAG_WRITE) 3328c89357e2SDavid Hildenbrand * * GUP wants to take a R/O pin on a possibly shared anonymous page 3329c89357e2SDavid Hildenbrand * (FAULT_FLAG_UNSHARE) 3330c89357e2SDavid Hildenbrand * 3331c89357e2SDavid Hildenbrand * It is done by copying the page to a new address and decrementing the 3332c89357e2SDavid Hildenbrand * shared-page counter for the old page. 33331da177e4SLinus Torvalds * 33341da177e4SLinus Torvalds * Note that this routine assumes that the protection checks have been 33351da177e4SLinus Torvalds * done by the caller (the low-level page fault routine in most cases). 3336c89357e2SDavid Hildenbrand * Thus, with FAULT_FLAG_WRITE, we can safely just mark it writable once we've 3337c89357e2SDavid Hildenbrand * done any necessary COW. 33381da177e4SLinus Torvalds * 3339c89357e2SDavid Hildenbrand * In case of FAULT_FLAG_WRITE, we also mark the page dirty at this point even 3340c89357e2SDavid Hildenbrand * though the page will change only once the write actually happens. This 3341c89357e2SDavid Hildenbrand * avoids a few races, and potentially makes it more efficient. 33421da177e4SLinus Torvalds * 3343c1e8d7c6SMichel Lespinasse * We enter with non-exclusive mmap_lock (to exclude vma changes, 33448f4e2101SHugh Dickins * but allow concurrent faults), with pte both mapped and locked. 3345c1e8d7c6SMichel Lespinasse * We return with mmap_lock still held, but pte unmapped and unlocked. 33461da177e4SLinus Torvalds */ 33472b740303SSouptick Joarder static vm_fault_t do_wp_page(struct vm_fault *vmf) 334882b0f8c3SJan Kara __releases(vmf->ptl) 33491da177e4SLinus Torvalds { 3350c89357e2SDavid Hildenbrand const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE; 335182b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3352b9086fdeSDavid Hildenbrand struct folio *folio = NULL; 3353*d61ea1cbSPeter Xu pte_t pte; 33541da177e4SLinus Torvalds 3355c89357e2SDavid Hildenbrand if (likely(!unshare)) { 3356c33c7948SRyan Roberts if (userfaultfd_pte_wp(vma, ptep_get(vmf->pte))) { 3357*d61ea1cbSPeter Xu if (!userfaultfd_wp_async(vma)) { 3358529b930bSAndrea Arcangeli pte_unmap_unlock(vmf->pte, vmf->ptl); 3359529b930bSAndrea Arcangeli return handle_userfault(vmf, VM_UFFD_WP); 3360529b930bSAndrea Arcangeli } 3361529b930bSAndrea Arcangeli 33626ce64428SNadav Amit /* 3363*d61ea1cbSPeter Xu * Nothing needed (cache flush, TLB invalidations, 3364*d61ea1cbSPeter Xu * etc.) because we're only removing the uffd-wp bit, 3365*d61ea1cbSPeter Xu * which is completely invisible to the user. 3366*d61ea1cbSPeter Xu */ 3367*d61ea1cbSPeter Xu pte = pte_clear_uffd_wp(ptep_get(vmf->pte)); 3368*d61ea1cbSPeter Xu 3369*d61ea1cbSPeter Xu set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte); 3370*d61ea1cbSPeter Xu /* 3371*d61ea1cbSPeter Xu * Update this to be prepared for following up CoW 3372*d61ea1cbSPeter Xu * handling 3373*d61ea1cbSPeter Xu */ 3374*d61ea1cbSPeter Xu vmf->orig_pte = pte; 3375*d61ea1cbSPeter Xu } 3376*d61ea1cbSPeter Xu 3377*d61ea1cbSPeter Xu /* 33786ce64428SNadav Amit * Userfaultfd write-protect can defer flushes. Ensure the TLB 33796ce64428SNadav Amit * is flushed in this case before copying. 33806ce64428SNadav Amit */ 33816ce64428SNadav Amit if (unlikely(userfaultfd_wp(vmf->vma) && 33826ce64428SNadav Amit mm_tlb_flush_pending(vmf->vma->vm_mm))) 33836ce64428SNadav Amit flush_tlb_page(vmf->vma, vmf->address); 3384c89357e2SDavid Hildenbrand } 33856ce64428SNadav Amit 3386a41b70d6SJan Kara vmf->page = vm_normal_page(vma, vmf->address, vmf->orig_pte); 3387c89357e2SDavid Hildenbrand 33885a97858bSSidhartha Kumar if (vmf->page) 33895a97858bSSidhartha Kumar folio = page_folio(vmf->page); 33905a97858bSSidhartha Kumar 3391251b97f5SPeter Zijlstra /* 3392b9086fdeSDavid Hildenbrand * Shared mapping: we are guaranteed to have VM_WRITE and 3393b9086fdeSDavid Hildenbrand * FAULT_FLAG_WRITE set at this point. 3394b9086fdeSDavid Hildenbrand */ 3395b9086fdeSDavid Hildenbrand if (vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) { 3396b9086fdeSDavid Hildenbrand /* 339764e45507SPeter Feiner * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a 339864e45507SPeter Feiner * VM_PFNMAP VMA. 3399251b97f5SPeter Zijlstra * 3400251b97f5SPeter Zijlstra * We should not cow pages in a shared writeable mapping. 3401dd906184SBoaz Harrosh * Just mark the pages writable and/or call ops->pfn_mkwrite. 3402251b97f5SPeter Zijlstra */ 3403b9086fdeSDavid Hildenbrand if (!vmf->page) 34042994302bSJan Kara return wp_pfn_shared(vmf); 34055a97858bSSidhartha Kumar return wp_page_shared(vmf, folio); 3406251b97f5SPeter Zijlstra } 34071da177e4SLinus Torvalds 3408b9086fdeSDavid Hildenbrand /* 3409b9086fdeSDavid Hildenbrand * Private mapping: create an exclusive anonymous page copy if reuse 3410b9086fdeSDavid Hildenbrand * is impossible. We might miss VM_WRITE for FOLL_FORCE handling. 3411b9086fdeSDavid Hildenbrand */ 3412b9086fdeSDavid Hildenbrand if (folio && folio_test_anon(folio)) { 341353a05ad9SDavid Hildenbrand /* 34146c287605SDavid Hildenbrand * If the page is exclusive to this process we must reuse the 34156c287605SDavid Hildenbrand * page without further checks. 34166c287605SDavid Hildenbrand */ 3417e4a2ed94SMatthew Wilcox (Oracle) if (PageAnonExclusive(vmf->page)) 34186c287605SDavid Hildenbrand goto reuse; 34196c287605SDavid Hildenbrand 34206c287605SDavid Hildenbrand /* 3421e4a2ed94SMatthew Wilcox (Oracle) * We have to verify under folio lock: these early checks are 3422e4a2ed94SMatthew Wilcox (Oracle) * just an optimization to avoid locking the folio and freeing 342353a05ad9SDavid Hildenbrand * the swapcache if there is little hope that we can reuse. 342453a05ad9SDavid Hildenbrand * 3425e4a2ed94SMatthew Wilcox (Oracle) * KSM doesn't necessarily raise the folio refcount. 342653a05ad9SDavid Hildenbrand */ 3427e4a2ed94SMatthew Wilcox (Oracle) if (folio_test_ksm(folio) || folio_ref_count(folio) > 3) 3428d4c47097SDavid Hildenbrand goto copy; 3429e4a2ed94SMatthew Wilcox (Oracle) if (!folio_test_lru(folio)) 3430d4c47097SDavid Hildenbrand /* 34311fec6890SMatthew Wilcox (Oracle) * We cannot easily detect+handle references from 34321fec6890SMatthew Wilcox (Oracle) * remote LRU caches or references to LRU folios. 3433d4c47097SDavid Hildenbrand */ 3434d4c47097SDavid Hildenbrand lru_add_drain(); 3435e4a2ed94SMatthew Wilcox (Oracle) if (folio_ref_count(folio) > 1 + folio_test_swapcache(folio)) 343652d1e606SKirill Tkhai goto copy; 3437e4a2ed94SMatthew Wilcox (Oracle) if (!folio_trylock(folio)) 343852d1e606SKirill Tkhai goto copy; 3439e4a2ed94SMatthew Wilcox (Oracle) if (folio_test_swapcache(folio)) 3440e4a2ed94SMatthew Wilcox (Oracle) folio_free_swap(folio); 3441e4a2ed94SMatthew Wilcox (Oracle) if (folio_test_ksm(folio) || folio_ref_count(folio) != 1) { 3442e4a2ed94SMatthew Wilcox (Oracle) folio_unlock(folio); 344309854ba9SLinus Torvalds goto copy; 344452d1e606SKirill Tkhai } 3445c44b6743SRik van Riel /* 3446e4a2ed94SMatthew Wilcox (Oracle) * Ok, we've got the only folio reference from our mapping 3447e4a2ed94SMatthew Wilcox (Oracle) * and the folio is locked, it's dark out, and we're wearing 344853a05ad9SDavid Hildenbrand * sunglasses. Hit it. 3449c44b6743SRik van Riel */ 3450e4a2ed94SMatthew Wilcox (Oracle) page_move_anon_rmap(vmf->page, vma); 3451e4a2ed94SMatthew Wilcox (Oracle) folio_unlock(folio); 34526c287605SDavid Hildenbrand reuse: 3453c89357e2SDavid Hildenbrand if (unlikely(unshare)) { 3454c89357e2SDavid Hildenbrand pte_unmap_unlock(vmf->pte, vmf->ptl); 3455c89357e2SDavid Hildenbrand return 0; 3456c89357e2SDavid Hildenbrand } 3457be068f29SLinus Torvalds wp_page_reuse(vmf); 3458cb8d8633SDavid Hildenbrand return 0; 34591da177e4SLinus Torvalds } 346052d1e606SKirill Tkhai copy: 3461063e60d8SMatthew Wilcox (Oracle) if ((vmf->flags & FAULT_FLAG_VMA_LOCK) && !vma->anon_vma) { 3462063e60d8SMatthew Wilcox (Oracle) pte_unmap_unlock(vmf->pte, vmf->ptl); 3463063e60d8SMatthew Wilcox (Oracle) vma_end_read(vmf->vma); 3464063e60d8SMatthew Wilcox (Oracle) return VM_FAULT_RETRY; 3465063e60d8SMatthew Wilcox (Oracle) } 3466063e60d8SMatthew Wilcox (Oracle) 34671da177e4SLinus Torvalds /* 34681da177e4SLinus Torvalds * Ok, we need to copy. Oh, well.. 34691da177e4SLinus Torvalds */ 3470b9086fdeSDavid Hildenbrand if (folio) 3471b9086fdeSDavid Hildenbrand folio_get(folio); 347228766805SShachar Raindel 347382b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 347494bfe85bSYang Yang #ifdef CONFIG_KSM 3475b9086fdeSDavid Hildenbrand if (folio && folio_test_ksm(folio)) 347694bfe85bSYang Yang count_vm_event(COW_KSM); 347794bfe85bSYang Yang #endif 3478a41b70d6SJan Kara return wp_page_copy(vmf); 34791da177e4SLinus Torvalds } 34801da177e4SLinus Torvalds 348197a89413SPeter Zijlstra static void unmap_mapping_range_vma(struct vm_area_struct *vma, 34821da177e4SLinus Torvalds unsigned long start_addr, unsigned long end_addr, 34831da177e4SLinus Torvalds struct zap_details *details) 34841da177e4SLinus Torvalds { 3485f5cc4eefSAl Viro zap_page_range_single(vma, start_addr, end_addr - start_addr, details); 34861da177e4SLinus Torvalds } 34871da177e4SLinus Torvalds 3488f808c13fSDavidlohr Bueso static inline void unmap_mapping_range_tree(struct rb_root_cached *root, 3489232a6a1cSPeter Xu pgoff_t first_index, 3490232a6a1cSPeter Xu pgoff_t last_index, 34911da177e4SLinus Torvalds struct zap_details *details) 34921da177e4SLinus Torvalds { 34931da177e4SLinus Torvalds struct vm_area_struct *vma; 34941da177e4SLinus Torvalds pgoff_t vba, vea, zba, zea; 34951da177e4SLinus Torvalds 3496232a6a1cSPeter Xu vma_interval_tree_foreach(vma, root, first_index, last_index) { 34971da177e4SLinus Torvalds vba = vma->vm_pgoff; 3498d6e93217SLibin vea = vba + vma_pages(vma) - 1; 3499f9871da9SMiaohe Lin zba = max(first_index, vba); 3500f9871da9SMiaohe Lin zea = min(last_index, vea); 35011da177e4SLinus Torvalds 350297a89413SPeter Zijlstra unmap_mapping_range_vma(vma, 35031da177e4SLinus Torvalds ((zba - vba) << PAGE_SHIFT) + vma->vm_start, 35041da177e4SLinus Torvalds ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start, 350597a89413SPeter Zijlstra details); 35061da177e4SLinus Torvalds } 35071da177e4SLinus Torvalds } 35081da177e4SLinus Torvalds 35091da177e4SLinus Torvalds /** 35103506659eSMatthew Wilcox (Oracle) * unmap_mapping_folio() - Unmap single folio from processes. 35113506659eSMatthew Wilcox (Oracle) * @folio: The locked folio to be unmapped. 351222061a1fSHugh Dickins * 35133506659eSMatthew Wilcox (Oracle) * Unmap this folio from any userspace process which still has it mmaped. 351422061a1fSHugh Dickins * Typically, for efficiency, the range of nearby pages has already been 351522061a1fSHugh Dickins * unmapped by unmap_mapping_pages() or unmap_mapping_range(). But once 35163506659eSMatthew Wilcox (Oracle) * truncation or invalidation holds the lock on a folio, it may find that 35173506659eSMatthew Wilcox (Oracle) * the page has been remapped again: and then uses unmap_mapping_folio() 351822061a1fSHugh Dickins * to unmap it finally. 351922061a1fSHugh Dickins */ 35203506659eSMatthew Wilcox (Oracle) void unmap_mapping_folio(struct folio *folio) 352122061a1fSHugh Dickins { 35223506659eSMatthew Wilcox (Oracle) struct address_space *mapping = folio->mapping; 352322061a1fSHugh Dickins struct zap_details details = { }; 3524232a6a1cSPeter Xu pgoff_t first_index; 3525232a6a1cSPeter Xu pgoff_t last_index; 352622061a1fSHugh Dickins 35273506659eSMatthew Wilcox (Oracle) VM_BUG_ON(!folio_test_locked(folio)); 352822061a1fSHugh Dickins 35293506659eSMatthew Wilcox (Oracle) first_index = folio->index; 353087b11f86SSidhartha Kumar last_index = folio_next_index(folio) - 1; 3531232a6a1cSPeter Xu 35322e148f1eSPeter Xu details.even_cows = false; 35333506659eSMatthew Wilcox (Oracle) details.single_folio = folio; 3534999dad82SPeter Xu details.zap_flags = ZAP_FLAG_DROP_MARKER; 353522061a1fSHugh Dickins 35362c865995SHugh Dickins i_mmap_lock_read(mapping); 353722061a1fSHugh Dickins if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root))) 3538232a6a1cSPeter Xu unmap_mapping_range_tree(&mapping->i_mmap, first_index, 3539232a6a1cSPeter Xu last_index, &details); 35402c865995SHugh Dickins i_mmap_unlock_read(mapping); 354122061a1fSHugh Dickins } 354222061a1fSHugh Dickins 354322061a1fSHugh Dickins /** 3544977fbdcdSMatthew Wilcox * unmap_mapping_pages() - Unmap pages from processes. 3545977fbdcdSMatthew Wilcox * @mapping: The address space containing pages to be unmapped. 3546977fbdcdSMatthew Wilcox * @start: Index of first page to be unmapped. 3547977fbdcdSMatthew Wilcox * @nr: Number of pages to be unmapped. 0 to unmap to end of file. 3548977fbdcdSMatthew Wilcox * @even_cows: Whether to unmap even private COWed pages. 3549977fbdcdSMatthew Wilcox * 3550977fbdcdSMatthew Wilcox * Unmap the pages in this address space from any userspace process which 3551977fbdcdSMatthew Wilcox * has them mmaped. Generally, you want to remove COWed pages as well when 3552977fbdcdSMatthew Wilcox * a file is being truncated, but not when invalidating pages from the page 3553977fbdcdSMatthew Wilcox * cache. 3554977fbdcdSMatthew Wilcox */ 3555977fbdcdSMatthew Wilcox void unmap_mapping_pages(struct address_space *mapping, pgoff_t start, 3556977fbdcdSMatthew Wilcox pgoff_t nr, bool even_cows) 3557977fbdcdSMatthew Wilcox { 3558977fbdcdSMatthew Wilcox struct zap_details details = { }; 3559232a6a1cSPeter Xu pgoff_t first_index = start; 3560232a6a1cSPeter Xu pgoff_t last_index = start + nr - 1; 3561977fbdcdSMatthew Wilcox 35622e148f1eSPeter Xu details.even_cows = even_cows; 3563232a6a1cSPeter Xu if (last_index < first_index) 3564232a6a1cSPeter Xu last_index = ULONG_MAX; 3565977fbdcdSMatthew Wilcox 35662c865995SHugh Dickins i_mmap_lock_read(mapping); 3567977fbdcdSMatthew Wilcox if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root))) 3568232a6a1cSPeter Xu unmap_mapping_range_tree(&mapping->i_mmap, first_index, 3569232a6a1cSPeter Xu last_index, &details); 35702c865995SHugh Dickins i_mmap_unlock_read(mapping); 3571977fbdcdSMatthew Wilcox } 35726e0e99d5SDavid Howells EXPORT_SYMBOL_GPL(unmap_mapping_pages); 3573977fbdcdSMatthew Wilcox 3574977fbdcdSMatthew Wilcox /** 35758a5f14a2SKirill A. Shutemov * unmap_mapping_range - unmap the portion of all mmaps in the specified 3576977fbdcdSMatthew Wilcox * address_space corresponding to the specified byte range in the underlying 35778a5f14a2SKirill A. Shutemov * file. 35788a5f14a2SKirill A. Shutemov * 35793d41088fSMartin Waitz * @mapping: the address space containing mmaps to be unmapped. 35801da177e4SLinus Torvalds * @holebegin: byte in first page to unmap, relative to the start of 35811da177e4SLinus Torvalds * the underlying file. This will be rounded down to a PAGE_SIZE 358225d9e2d1Snpiggin@suse.de * boundary. Note that this is different from truncate_pagecache(), which 35831da177e4SLinus Torvalds * must keep the partial page. In contrast, we must get rid of 35841da177e4SLinus Torvalds * partial pages. 35851da177e4SLinus Torvalds * @holelen: size of prospective hole in bytes. This will be rounded 35861da177e4SLinus Torvalds * up to a PAGE_SIZE boundary. A holelen of zero truncates to the 35871da177e4SLinus Torvalds * end of the file. 35881da177e4SLinus Torvalds * @even_cows: 1 when truncating a file, unmap even private COWed pages; 35891da177e4SLinus Torvalds * but 0 when invalidating pagecache, don't throw away private data. 35901da177e4SLinus Torvalds */ 35911da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping, 35921da177e4SLinus Torvalds loff_t const holebegin, loff_t const holelen, int even_cows) 35931da177e4SLinus Torvalds { 35941da177e4SLinus Torvalds pgoff_t hba = holebegin >> PAGE_SHIFT; 35951da177e4SLinus Torvalds pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 35961da177e4SLinus Torvalds 35971da177e4SLinus Torvalds /* Check for overflow. */ 35981da177e4SLinus Torvalds if (sizeof(holelen) > sizeof(hlen)) { 35991da177e4SLinus Torvalds long long holeend = 36001da177e4SLinus Torvalds (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 36011da177e4SLinus Torvalds if (holeend & ~(long long)ULONG_MAX) 36021da177e4SLinus Torvalds hlen = ULONG_MAX - hba + 1; 36031da177e4SLinus Torvalds } 36041da177e4SLinus Torvalds 3605977fbdcdSMatthew Wilcox unmap_mapping_pages(mapping, hba, hlen, even_cows); 36061da177e4SLinus Torvalds } 36071da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range); 36081da177e4SLinus Torvalds 36091da177e4SLinus Torvalds /* 3610b756a3b5SAlistair Popple * Restore a potential device exclusive pte to a working pte entry 3611b756a3b5SAlistair Popple */ 3612b756a3b5SAlistair Popple static vm_fault_t remove_device_exclusive_entry(struct vm_fault *vmf) 3613b756a3b5SAlistair Popple { 361419672a9eSMatthew Wilcox (Oracle) struct folio *folio = page_folio(vmf->page); 3615b756a3b5SAlistair Popple struct vm_area_struct *vma = vmf->vma; 3616b756a3b5SAlistair Popple struct mmu_notifier_range range; 3617fdc724d6SSuren Baghdasaryan vm_fault_t ret; 3618b756a3b5SAlistair Popple 36197c7b9629SAlistair Popple /* 36207c7b9629SAlistair Popple * We need a reference to lock the folio because we don't hold 36217c7b9629SAlistair Popple * the PTL so a racing thread can remove the device-exclusive 36227c7b9629SAlistair Popple * entry and unmap it. If the folio is free the entry must 36237c7b9629SAlistair Popple * have been removed already. If it happens to have already 36247c7b9629SAlistair Popple * been re-allocated after being freed all we do is lock and 36257c7b9629SAlistair Popple * unlock it. 36267c7b9629SAlistair Popple */ 36277c7b9629SAlistair Popple if (!folio_try_get(folio)) 36287c7b9629SAlistair Popple return 0; 36297c7b9629SAlistair Popple 3630fdc724d6SSuren Baghdasaryan ret = folio_lock_or_retry(folio, vmf); 3631fdc724d6SSuren Baghdasaryan if (ret) { 36327c7b9629SAlistair Popple folio_put(folio); 3633fdc724d6SSuren Baghdasaryan return ret; 36347c7b9629SAlistair Popple } 36357d4a8be0SAlistair Popple mmu_notifier_range_init_owner(&range, MMU_NOTIFY_EXCLUSIVE, 0, 3636b756a3b5SAlistair Popple vma->vm_mm, vmf->address & PAGE_MASK, 3637b756a3b5SAlistair Popple (vmf->address & PAGE_MASK) + PAGE_SIZE, NULL); 3638b756a3b5SAlistair Popple mmu_notifier_invalidate_range_start(&range); 3639b756a3b5SAlistair Popple 3640b756a3b5SAlistair Popple vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 3641b756a3b5SAlistair Popple &vmf->ptl); 3642c33c7948SRyan Roberts if (likely(vmf->pte && pte_same(ptep_get(vmf->pte), vmf->orig_pte))) 364319672a9eSMatthew Wilcox (Oracle) restore_exclusive_pte(vma, vmf->page, vmf->address, vmf->pte); 3644b756a3b5SAlistair Popple 36453db82b93SHugh Dickins if (vmf->pte) 3646b756a3b5SAlistair Popple pte_unmap_unlock(vmf->pte, vmf->ptl); 364719672a9eSMatthew Wilcox (Oracle) folio_unlock(folio); 36487c7b9629SAlistair Popple folio_put(folio); 3649b756a3b5SAlistair Popple 3650b756a3b5SAlistair Popple mmu_notifier_invalidate_range_end(&range); 3651b756a3b5SAlistair Popple return 0; 3652b756a3b5SAlistair Popple } 3653b756a3b5SAlistair Popple 3654a160e537SMatthew Wilcox (Oracle) static inline bool should_try_to_free_swap(struct folio *folio, 3655c145e0b4SDavid Hildenbrand struct vm_area_struct *vma, 3656c145e0b4SDavid Hildenbrand unsigned int fault_flags) 3657c145e0b4SDavid Hildenbrand { 3658a160e537SMatthew Wilcox (Oracle) if (!folio_test_swapcache(folio)) 3659c145e0b4SDavid Hildenbrand return false; 36609202d527SMatthew Wilcox (Oracle) if (mem_cgroup_swap_full(folio) || (vma->vm_flags & VM_LOCKED) || 3661a160e537SMatthew Wilcox (Oracle) folio_test_mlocked(folio)) 3662c145e0b4SDavid Hildenbrand return true; 3663c145e0b4SDavid Hildenbrand /* 3664c145e0b4SDavid Hildenbrand * If we want to map a page that's in the swapcache writable, we 3665c145e0b4SDavid Hildenbrand * have to detect via the refcount if we're really the exclusive 3666c145e0b4SDavid Hildenbrand * user. Try freeing the swapcache to get rid of the swapcache 3667c145e0b4SDavid Hildenbrand * reference only in case it's likely that we'll be the exlusive user. 3668c145e0b4SDavid Hildenbrand */ 3669a160e537SMatthew Wilcox (Oracle) return (fault_flags & FAULT_FLAG_WRITE) && !folio_test_ksm(folio) && 3670a160e537SMatthew Wilcox (Oracle) folio_ref_count(folio) == 2; 3671c145e0b4SDavid Hildenbrand } 3672c145e0b4SDavid Hildenbrand 36739c28a205SPeter Xu static vm_fault_t pte_marker_clear(struct vm_fault *vmf) 36749c28a205SPeter Xu { 36759c28a205SPeter Xu vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, 36769c28a205SPeter Xu vmf->address, &vmf->ptl); 36773db82b93SHugh Dickins if (!vmf->pte) 36783db82b93SHugh Dickins return 0; 36799c28a205SPeter Xu /* 36809c28a205SPeter Xu * Be careful so that we will only recover a special uffd-wp pte into a 36819c28a205SPeter Xu * none pte. Otherwise it means the pte could have changed, so retry. 36827e3ce3f8SPeter Xu * 36837e3ce3f8SPeter Xu * This should also cover the case where e.g. the pte changed 3684af19487fSAxel Rasmussen * quickly from a PTE_MARKER_UFFD_WP into PTE_MARKER_POISONED. 36857e3ce3f8SPeter Xu * So is_pte_marker() check is not enough to safely drop the pte. 36869c28a205SPeter Xu */ 3687c33c7948SRyan Roberts if (pte_same(vmf->orig_pte, ptep_get(vmf->pte))) 36889c28a205SPeter Xu pte_clear(vmf->vma->vm_mm, vmf->address, vmf->pte); 36899c28a205SPeter Xu pte_unmap_unlock(vmf->pte, vmf->ptl); 36909c28a205SPeter Xu return 0; 36919c28a205SPeter Xu } 36929c28a205SPeter Xu 36932bad466cSPeter Xu static vm_fault_t do_pte_missing(struct vm_fault *vmf) 36942bad466cSPeter Xu { 36952bad466cSPeter Xu if (vma_is_anonymous(vmf->vma)) 36962bad466cSPeter Xu return do_anonymous_page(vmf); 36972bad466cSPeter Xu else 36982bad466cSPeter Xu return do_fault(vmf); 36992bad466cSPeter Xu } 37002bad466cSPeter Xu 37019c28a205SPeter Xu /* 37029c28a205SPeter Xu * This is actually a page-missing access, but with uffd-wp special pte 37039c28a205SPeter Xu * installed. It means this pte was wr-protected before being unmapped. 37049c28a205SPeter Xu */ 37059c28a205SPeter Xu static vm_fault_t pte_marker_handle_uffd_wp(struct vm_fault *vmf) 37069c28a205SPeter Xu { 37079c28a205SPeter Xu /* 37089c28a205SPeter Xu * Just in case there're leftover special ptes even after the region 37097a079ba2SPeter Xu * got unregistered - we can simply clear them. 37109c28a205SPeter Xu */ 37112bad466cSPeter Xu if (unlikely(!userfaultfd_wp(vmf->vma))) 37129c28a205SPeter Xu return pte_marker_clear(vmf); 37139c28a205SPeter Xu 37142bad466cSPeter Xu return do_pte_missing(vmf); 37159c28a205SPeter Xu } 37169c28a205SPeter Xu 37175c041f5dSPeter Xu static vm_fault_t handle_pte_marker(struct vm_fault *vmf) 37185c041f5dSPeter Xu { 37195c041f5dSPeter Xu swp_entry_t entry = pte_to_swp_entry(vmf->orig_pte); 37205c041f5dSPeter Xu unsigned long marker = pte_marker_get(entry); 37215c041f5dSPeter Xu 37225c041f5dSPeter Xu /* 3723ca92ea3dSPeter Xu * PTE markers should never be empty. If anything weird happened, 3724ca92ea3dSPeter Xu * the best thing to do is to kill the process along with its mm. 37255c041f5dSPeter Xu */ 3726ca92ea3dSPeter Xu if (WARN_ON_ONCE(!marker)) 37275c041f5dSPeter Xu return VM_FAULT_SIGBUS; 37285c041f5dSPeter Xu 372915520a3fSPeter Xu /* Higher priority than uffd-wp when data corrupted */ 3730af19487fSAxel Rasmussen if (marker & PTE_MARKER_POISONED) 3731af19487fSAxel Rasmussen return VM_FAULT_HWPOISON; 373215520a3fSPeter Xu 37339c28a205SPeter Xu if (pte_marker_entry_uffd_wp(entry)) 37349c28a205SPeter Xu return pte_marker_handle_uffd_wp(vmf); 37359c28a205SPeter Xu 37369c28a205SPeter Xu /* This is an unknown pte marker */ 37379c28a205SPeter Xu return VM_FAULT_SIGBUS; 37385c041f5dSPeter Xu } 37395c041f5dSPeter Xu 3740b756a3b5SAlistair Popple /* 3741c1e8d7c6SMichel Lespinasse * We enter with non-exclusive mmap_lock (to exclude vma changes, 37428f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 37439a95f3cfSPaul Cassella * We return with pte unmapped and unlocked. 37449a95f3cfSPaul Cassella * 3745c1e8d7c6SMichel Lespinasse * We return with the mmap_lock locked or unlocked in the same cases 37469a95f3cfSPaul Cassella * as does filemap_fault(). 37471da177e4SLinus Torvalds */ 37482b740303SSouptick Joarder vm_fault_t do_swap_page(struct vm_fault *vmf) 37491da177e4SLinus Torvalds { 375082b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3751d4f9565aSMatthew Wilcox (Oracle) struct folio *swapcache, *folio = NULL; 3752d4f9565aSMatthew Wilcox (Oracle) struct page *page; 37532799e775SMiaohe Lin struct swap_info_struct *si = NULL; 375414f9135dSDavid Hildenbrand rmap_t rmap_flags = RMAP_NONE; 37551493a191SDavid Hildenbrand bool exclusive = false; 375665500d23SHugh Dickins swp_entry_t entry; 37571da177e4SLinus Torvalds pte_t pte; 37582b740303SSouptick Joarder vm_fault_t ret = 0; 3759aae466b0SJoonsoo Kim void *shadow = NULL; 37601da177e4SLinus Torvalds 37612ca99358SPeter Xu if (!pte_unmap_same(vmf)) 37628f4e2101SHugh Dickins goto out; 376365500d23SHugh Dickins 37642994302bSJan Kara entry = pte_to_swp_entry(vmf->orig_pte); 3765d1737fdbSAndi Kleen if (unlikely(non_swap_entry(entry))) { 37660697212aSChristoph Lameter if (is_migration_entry(entry)) { 376782b0f8c3SJan Kara migration_entry_wait(vma->vm_mm, vmf->pmd, 376882b0f8c3SJan Kara vmf->address); 3769b756a3b5SAlistair Popple } else if (is_device_exclusive_entry(entry)) { 3770b756a3b5SAlistair Popple vmf->page = pfn_swap_entry_to_page(entry); 3771b756a3b5SAlistair Popple ret = remove_device_exclusive_entry(vmf); 37725042db43SJérôme Glisse } else if (is_device_private_entry(entry)) { 37731235ccd0SSuren Baghdasaryan if (vmf->flags & FAULT_FLAG_VMA_LOCK) { 37741235ccd0SSuren Baghdasaryan /* 37751235ccd0SSuren Baghdasaryan * migrate_to_ram is not yet ready to operate 37761235ccd0SSuren Baghdasaryan * under VMA lock. 37771235ccd0SSuren Baghdasaryan */ 37781235ccd0SSuren Baghdasaryan vma_end_read(vma); 37791235ccd0SSuren Baghdasaryan ret = VM_FAULT_RETRY; 37801235ccd0SSuren Baghdasaryan goto out; 37811235ccd0SSuren Baghdasaryan } 37821235ccd0SSuren Baghdasaryan 3783af5cdaf8SAlistair Popple vmf->page = pfn_swap_entry_to_page(entry); 378416ce101dSAlistair Popple vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 378516ce101dSAlistair Popple vmf->address, &vmf->ptl); 37863db82b93SHugh Dickins if (unlikely(!vmf->pte || 3787c33c7948SRyan Roberts !pte_same(ptep_get(vmf->pte), 3788c33c7948SRyan Roberts vmf->orig_pte))) 37893b65f437SRyan Roberts goto unlock; 379016ce101dSAlistair Popple 379116ce101dSAlistair Popple /* 379216ce101dSAlistair Popple * Get a page reference while we know the page can't be 379316ce101dSAlistair Popple * freed. 379416ce101dSAlistair Popple */ 379516ce101dSAlistair Popple get_page(vmf->page); 379616ce101dSAlistair Popple pte_unmap_unlock(vmf->pte, vmf->ptl); 37974a955bedSAlistair Popple ret = vmf->page->pgmap->ops->migrate_to_ram(vmf); 379816ce101dSAlistair Popple put_page(vmf->page); 3799d1737fdbSAndi Kleen } else if (is_hwpoison_entry(entry)) { 3800d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 38015c041f5dSPeter Xu } else if (is_pte_marker_entry(entry)) { 38025c041f5dSPeter Xu ret = handle_pte_marker(vmf); 3803d1737fdbSAndi Kleen } else { 38042994302bSJan Kara print_bad_pte(vma, vmf->address, vmf->orig_pte, NULL); 3805d99be1a8SHugh Dickins ret = VM_FAULT_SIGBUS; 3806d1737fdbSAndi Kleen } 38070697212aSChristoph Lameter goto out; 38080697212aSChristoph Lameter } 38090bcac06fSMinchan Kim 38102799e775SMiaohe Lin /* Prevent swapoff from happening to us. */ 38112799e775SMiaohe Lin si = get_swap_device(entry); 38122799e775SMiaohe Lin if (unlikely(!si)) 38132799e775SMiaohe Lin goto out; 38140bcac06fSMinchan Kim 38155a423081SMatthew Wilcox (Oracle) folio = swap_cache_get_folio(entry, vma, vmf->address); 38165a423081SMatthew Wilcox (Oracle) if (folio) 38175a423081SMatthew Wilcox (Oracle) page = folio_file_page(folio, swp_offset(entry)); 3818d4f9565aSMatthew Wilcox (Oracle) swapcache = folio; 3819f8020772SMinchan Kim 3820d4f9565aSMatthew Wilcox (Oracle) if (!folio) { 3821a449bf58SQian Cai if (data_race(si->flags & SWP_SYNCHRONOUS_IO) && 3822eb085574SHuang Ying __swap_count(entry) == 1) { 38230bcac06fSMinchan Kim /* skip swapcache */ 382463ad4addSMatthew Wilcox (Oracle) folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, 382563ad4addSMatthew Wilcox (Oracle) vma, vmf->address, false); 382663ad4addSMatthew Wilcox (Oracle) page = &folio->page; 382763ad4addSMatthew Wilcox (Oracle) if (folio) { 382863ad4addSMatthew Wilcox (Oracle) __folio_set_locked(folio); 382963ad4addSMatthew Wilcox (Oracle) __folio_set_swapbacked(folio); 38304c6355b2SJohannes Weiner 383165995918SMatthew Wilcox (Oracle) if (mem_cgroup_swapin_charge_folio(folio, 383263ad4addSMatthew Wilcox (Oracle) vma->vm_mm, GFP_KERNEL, 383363ad4addSMatthew Wilcox (Oracle) entry)) { 3834545b1b07SMichal Hocko ret = VM_FAULT_OOM; 38354c6355b2SJohannes Weiner goto out_page; 3836545b1b07SMichal Hocko } 38370add0c77SShakeel Butt mem_cgroup_swapin_uncharge_swap(entry); 38384c6355b2SJohannes Weiner 3839aae466b0SJoonsoo Kim shadow = get_shadow_from_swap_cache(entry); 3840aae466b0SJoonsoo Kim if (shadow) 384163ad4addSMatthew Wilcox (Oracle) workingset_refault(folio, shadow); 38420076f029SJoonsoo Kim 384363ad4addSMatthew Wilcox (Oracle) folio_add_lru(folio); 38440add0c77SShakeel Butt 38450add0c77SShakeel Butt /* To provide entry to swap_readpage() */ 38463d2c9087SDavid Hildenbrand folio->swap = entry; 38475169b844SNeilBrown swap_readpage(page, true, NULL); 384863ad4addSMatthew Wilcox (Oracle) folio->private = NULL; 38490bcac06fSMinchan Kim } 3850aa8d22a1SMinchan Kim } else { 3851e9e9b7ecSMinchan Kim page = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, 3852e9e9b7ecSMinchan Kim vmf); 385363ad4addSMatthew Wilcox (Oracle) if (page) 385463ad4addSMatthew Wilcox (Oracle) folio = page_folio(page); 3855d4f9565aSMatthew Wilcox (Oracle) swapcache = folio; 38560bcac06fSMinchan Kim } 38570bcac06fSMinchan Kim 3858d4f9565aSMatthew Wilcox (Oracle) if (!folio) { 38591da177e4SLinus Torvalds /* 38608f4e2101SHugh Dickins * Back out if somebody else faulted in this pte 38618f4e2101SHugh Dickins * while we released the pte lock. 38621da177e4SLinus Torvalds */ 386382b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 386482b0f8c3SJan Kara vmf->address, &vmf->ptl); 3865c33c7948SRyan Roberts if (likely(vmf->pte && 3866c33c7948SRyan Roberts pte_same(ptep_get(vmf->pte), vmf->orig_pte))) 38671da177e4SLinus Torvalds ret = VM_FAULT_OOM; 386865500d23SHugh Dickins goto unlock; 38691da177e4SLinus Torvalds } 38701da177e4SLinus Torvalds 38711da177e4SLinus Torvalds /* Had to read the page from swap area: Major fault */ 38721da177e4SLinus Torvalds ret = VM_FAULT_MAJOR; 3873f8891e5eSChristoph Lameter count_vm_event(PGMAJFAULT); 38742262185cSRoman Gushchin count_memcg_event_mm(vma->vm_mm, PGMAJFAULT); 3875d1737fdbSAndi Kleen } else if (PageHWPoison(page)) { 387671f72525SWu Fengguang /* 387771f72525SWu Fengguang * hwpoisoned dirty swapcache pages are kept for killing 387871f72525SWu Fengguang * owner processes (which may be unknown at hwpoison time) 387971f72525SWu Fengguang */ 3880d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 38814779cb31SAndi Kleen goto out_release; 38821da177e4SLinus Torvalds } 38831da177e4SLinus Torvalds 3884fdc724d6SSuren Baghdasaryan ret |= folio_lock_or_retry(folio, vmf); 3885fdc724d6SSuren Baghdasaryan if (ret & VM_FAULT_RETRY) 3886d065bd81SMichel Lespinasse goto out_release; 38871da177e4SLinus Torvalds 388884d60fddSDavid Hildenbrand if (swapcache) { 38894969c119SAndrea Arcangeli /* 38903b344157SMatthew Wilcox (Oracle) * Make sure folio_free_swap() or swapoff did not release the 389184d60fddSDavid Hildenbrand * swapcache from under us. The page pin, and pte_same test 389284d60fddSDavid Hildenbrand * below, are not enough to exclude that. Even if it is still 389384d60fddSDavid Hildenbrand * swapcache, we need to check that the page's swap has not 389484d60fddSDavid Hildenbrand * changed. 38954969c119SAndrea Arcangeli */ 389663ad4addSMatthew Wilcox (Oracle) if (unlikely(!folio_test_swapcache(folio) || 3897cfeed8ffSDavid Hildenbrand page_swap_entry(page).val != entry.val)) 38984969c119SAndrea Arcangeli goto out_page; 38994969c119SAndrea Arcangeli 390084d60fddSDavid Hildenbrand /* 390184d60fddSDavid Hildenbrand * KSM sometimes has to copy on read faults, for example, if 390284d60fddSDavid Hildenbrand * page->index of !PageKSM() pages would be nonlinear inside the 390384d60fddSDavid Hildenbrand * anon VMA -- PageKSM() is lost on actual swapout. 390484d60fddSDavid Hildenbrand */ 390582b0f8c3SJan Kara page = ksm_might_need_to_copy(page, vma, vmf->address); 39064969c119SAndrea Arcangeli if (unlikely(!page)) { 39075ad64688SHugh Dickins ret = VM_FAULT_OOM; 39084969c119SAndrea Arcangeli goto out_page; 39096b970599SKefeng Wang } else if (unlikely(PTR_ERR(page) == -EHWPOISON)) { 39106b970599SKefeng Wang ret = VM_FAULT_HWPOISON; 39116b970599SKefeng Wang goto out_page; 39124969c119SAndrea Arcangeli } 391363ad4addSMatthew Wilcox (Oracle) folio = page_folio(page); 3914c145e0b4SDavid Hildenbrand 3915c145e0b4SDavid Hildenbrand /* 3916c145e0b4SDavid Hildenbrand * If we want to map a page that's in the swapcache writable, we 3917c145e0b4SDavid Hildenbrand * have to detect via the refcount if we're really the exclusive 3918c145e0b4SDavid Hildenbrand * owner. Try removing the extra reference from the local LRU 39191fec6890SMatthew Wilcox (Oracle) * caches if required. 3920c145e0b4SDavid Hildenbrand */ 3921d4f9565aSMatthew Wilcox (Oracle) if ((vmf->flags & FAULT_FLAG_WRITE) && folio == swapcache && 392263ad4addSMatthew Wilcox (Oracle) !folio_test_ksm(folio) && !folio_test_lru(folio)) 3923c145e0b4SDavid Hildenbrand lru_add_drain(); 392484d60fddSDavid Hildenbrand } 39255ad64688SHugh Dickins 39264231f842SKefeng Wang folio_throttle_swaprate(folio, GFP_KERNEL); 3927073e587eSKAMEZAWA Hiroyuki 39281da177e4SLinus Torvalds /* 39298f4e2101SHugh Dickins * Back out if somebody else already faulted in this pte. 39301da177e4SLinus Torvalds */ 393182b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 393282b0f8c3SJan Kara &vmf->ptl); 3933c33c7948SRyan Roberts if (unlikely(!vmf->pte || !pte_same(ptep_get(vmf->pte), vmf->orig_pte))) 3934b8107480SKirill Korotaev goto out_nomap; 3935b8107480SKirill Korotaev 393663ad4addSMatthew Wilcox (Oracle) if (unlikely(!folio_test_uptodate(folio))) { 3937b8107480SKirill Korotaev ret = VM_FAULT_SIGBUS; 3938b8107480SKirill Korotaev goto out_nomap; 39391da177e4SLinus Torvalds } 39401da177e4SLinus Torvalds 39418c7c6e34SKAMEZAWA Hiroyuki /* 394278fbe906SDavid Hildenbrand * PG_anon_exclusive reuses PG_mappedtodisk for anon pages. A swap pte 394378fbe906SDavid Hildenbrand * must never point at an anonymous page in the swapcache that is 394478fbe906SDavid Hildenbrand * PG_anon_exclusive. Sanity check that this holds and especially, that 394578fbe906SDavid Hildenbrand * no filesystem set PG_mappedtodisk on a page in the swapcache. Sanity 394678fbe906SDavid Hildenbrand * check after taking the PT lock and making sure that nobody 394778fbe906SDavid Hildenbrand * concurrently faulted in this page and set PG_anon_exclusive. 394878fbe906SDavid Hildenbrand */ 394963ad4addSMatthew Wilcox (Oracle) BUG_ON(!folio_test_anon(folio) && folio_test_mappedtodisk(folio)); 395063ad4addSMatthew Wilcox (Oracle) BUG_ON(folio_test_anon(folio) && PageAnonExclusive(page)); 395178fbe906SDavid Hildenbrand 395278fbe906SDavid Hildenbrand /* 39531493a191SDavid Hildenbrand * Check under PT lock (to protect against concurrent fork() sharing 39541493a191SDavid Hildenbrand * the swap entry concurrently) for certainly exclusive pages. 39551493a191SDavid Hildenbrand */ 395663ad4addSMatthew Wilcox (Oracle) if (!folio_test_ksm(folio)) { 39571493a191SDavid Hildenbrand exclusive = pte_swp_exclusive(vmf->orig_pte); 3958d4f9565aSMatthew Wilcox (Oracle) if (folio != swapcache) { 39591493a191SDavid Hildenbrand /* 39601493a191SDavid Hildenbrand * We have a fresh page that is not exposed to the 39611493a191SDavid Hildenbrand * swapcache -> certainly exclusive. 39621493a191SDavid Hildenbrand */ 39631493a191SDavid Hildenbrand exclusive = true; 396463ad4addSMatthew Wilcox (Oracle) } else if (exclusive && folio_test_writeback(folio) && 3965eacde327SMiaohe Lin data_race(si->flags & SWP_STABLE_WRITES)) { 39661493a191SDavid Hildenbrand /* 39671493a191SDavid Hildenbrand * This is tricky: not all swap backends support 39681493a191SDavid Hildenbrand * concurrent page modifications while under writeback. 39691493a191SDavid Hildenbrand * 39701493a191SDavid Hildenbrand * So if we stumble over such a page in the swapcache 39711493a191SDavid Hildenbrand * we must not set the page exclusive, otherwise we can 39721493a191SDavid Hildenbrand * map it writable without further checks and modify it 39731493a191SDavid Hildenbrand * while still under writeback. 39741493a191SDavid Hildenbrand * 39751493a191SDavid Hildenbrand * For these problematic swap backends, simply drop the 39761493a191SDavid Hildenbrand * exclusive marker: this is perfectly fine as we start 39771493a191SDavid Hildenbrand * writeback only if we fully unmapped the page and 39781493a191SDavid Hildenbrand * there are no unexpected references on the page after 39791493a191SDavid Hildenbrand * unmapping succeeded. After fully unmapped, no 39801493a191SDavid Hildenbrand * further GUP references (FOLL_GET and FOLL_PIN) can 39811493a191SDavid Hildenbrand * appear, so dropping the exclusive marker and mapping 39821493a191SDavid Hildenbrand * it only R/O is fine. 39831493a191SDavid Hildenbrand */ 39841493a191SDavid Hildenbrand exclusive = false; 39851493a191SDavid Hildenbrand } 39861493a191SDavid Hildenbrand } 39871493a191SDavid Hildenbrand 39881493a191SDavid Hildenbrand /* 39896dca4ac6SPeter Collingbourne * Some architectures may have to restore extra metadata to the page 39906dca4ac6SPeter Collingbourne * when reading from swap. This metadata may be indexed by swap entry 39916dca4ac6SPeter Collingbourne * so this must be called before swap_free(). 39926dca4ac6SPeter Collingbourne */ 39936dca4ac6SPeter Collingbourne arch_swap_restore(entry, folio); 39946dca4ac6SPeter Collingbourne 39956dca4ac6SPeter Collingbourne /* 3996c145e0b4SDavid Hildenbrand * Remove the swap entry and conditionally try to free up the swapcache. 3997c145e0b4SDavid Hildenbrand * We're already holding a reference on the page but haven't mapped it 3998c145e0b4SDavid Hildenbrand * yet. 39998c7c6e34SKAMEZAWA Hiroyuki */ 4000c145e0b4SDavid Hildenbrand swap_free(entry); 4001a160e537SMatthew Wilcox (Oracle) if (should_try_to_free_swap(folio, vma, vmf->flags)) 4002a160e537SMatthew Wilcox (Oracle) folio_free_swap(folio); 40031da177e4SLinus Torvalds 4004f1a79412SShakeel Butt inc_mm_counter(vma->vm_mm, MM_ANONPAGES); 4005f1a79412SShakeel Butt dec_mm_counter(vma->vm_mm, MM_SWAPENTS); 40061da177e4SLinus Torvalds pte = mk_pte(page, vma->vm_page_prot); 4007c145e0b4SDavid Hildenbrand 4008c145e0b4SDavid Hildenbrand /* 40091493a191SDavid Hildenbrand * Same logic as in do_wp_page(); however, optimize for pages that are 40101493a191SDavid Hildenbrand * certainly not shared either because we just allocated them without 40111493a191SDavid Hildenbrand * exposing them to the swapcache or because the swap entry indicates 40121493a191SDavid Hildenbrand * exclusivity. 4013c145e0b4SDavid Hildenbrand */ 401463ad4addSMatthew Wilcox (Oracle) if (!folio_test_ksm(folio) && 401563ad4addSMatthew Wilcox (Oracle) (exclusive || folio_ref_count(folio) == 1)) { 40166c287605SDavid Hildenbrand if (vmf->flags & FAULT_FLAG_WRITE) { 40171da177e4SLinus Torvalds pte = maybe_mkwrite(pte_mkdirty(pte), vma); 401882b0f8c3SJan Kara vmf->flags &= ~FAULT_FLAG_WRITE; 40196c287605SDavid Hildenbrand } 402014f9135dSDavid Hildenbrand rmap_flags |= RMAP_EXCLUSIVE; 40211da177e4SLinus Torvalds } 40221da177e4SLinus Torvalds flush_icache_page(vma, page); 40232994302bSJan Kara if (pte_swp_soft_dirty(vmf->orig_pte)) 4024179ef71cSCyrill Gorcunov pte = pte_mksoft_dirty(pte); 4025f1eb1bacSPeter Xu if (pte_swp_uffd_wp(vmf->orig_pte)) 4026f45ec5ffSPeter Xu pte = pte_mkuffd_wp(pte); 40272994302bSJan Kara vmf->orig_pte = pte; 40280bcac06fSMinchan Kim 40290bcac06fSMinchan Kim /* ksm created a completely new copy */ 4030d4f9565aSMatthew Wilcox (Oracle) if (unlikely(folio != swapcache && swapcache)) { 403140f2bbf7SDavid Hildenbrand page_add_new_anon_rmap(page, vma, vmf->address); 403263ad4addSMatthew Wilcox (Oracle) folio_add_lru_vma(folio, vma); 40330bcac06fSMinchan Kim } else { 4034f1e2db12SDavid Hildenbrand page_add_anon_rmap(page, vma, vmf->address, rmap_flags); 403500501b53SJohannes Weiner } 40361da177e4SLinus Torvalds 403763ad4addSMatthew Wilcox (Oracle) VM_BUG_ON(!folio_test_anon(folio) || 403863ad4addSMatthew Wilcox (Oracle) (pte_write(pte) && !PageAnonExclusive(page))); 40391eba86c0SPasha Tatashin set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte); 40401eba86c0SPasha Tatashin arch_do_swap_page(vma->vm_mm, vma, vmf->address, pte, vmf->orig_pte); 40411eba86c0SPasha Tatashin 404263ad4addSMatthew Wilcox (Oracle) folio_unlock(folio); 4043d4f9565aSMatthew Wilcox (Oracle) if (folio != swapcache && swapcache) { 40444969c119SAndrea Arcangeli /* 40454969c119SAndrea Arcangeli * Hold the lock to avoid the swap entry to be reused 40464969c119SAndrea Arcangeli * until we take the PT lock for the pte_same() check 40474969c119SAndrea Arcangeli * (to avoid false positives from pte_same). For 40484969c119SAndrea Arcangeli * further safety release the lock after the swap_free 40494969c119SAndrea Arcangeli * so that the swap count won't change under a 40504969c119SAndrea Arcangeli * parallel locked swapcache. 40514969c119SAndrea Arcangeli */ 4052d4f9565aSMatthew Wilcox (Oracle) folio_unlock(swapcache); 4053d4f9565aSMatthew Wilcox (Oracle) folio_put(swapcache); 40544969c119SAndrea Arcangeli } 4055c475a8abSHugh Dickins 405682b0f8c3SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) { 40572994302bSJan Kara ret |= do_wp_page(vmf); 405861469f1dSHugh Dickins if (ret & VM_FAULT_ERROR) 405961469f1dSHugh Dickins ret &= VM_FAULT_ERROR; 40601da177e4SLinus Torvalds goto out; 40611da177e4SLinus Torvalds } 40621da177e4SLinus Torvalds 40631da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 40645003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vma, vmf->address, vmf->pte, 1); 406565500d23SHugh Dickins unlock: 40663db82b93SHugh Dickins if (vmf->pte) 406782b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 40681da177e4SLinus Torvalds out: 40692799e775SMiaohe Lin if (si) 40702799e775SMiaohe Lin put_swap_device(si); 40711da177e4SLinus Torvalds return ret; 4072b8107480SKirill Korotaev out_nomap: 40733db82b93SHugh Dickins if (vmf->pte) 407482b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 4075bc43f75cSJohannes Weiner out_page: 407663ad4addSMatthew Wilcox (Oracle) folio_unlock(folio); 40774779cb31SAndi Kleen out_release: 407863ad4addSMatthew Wilcox (Oracle) folio_put(folio); 4079d4f9565aSMatthew Wilcox (Oracle) if (folio != swapcache && swapcache) { 4080d4f9565aSMatthew Wilcox (Oracle) folio_unlock(swapcache); 4081d4f9565aSMatthew Wilcox (Oracle) folio_put(swapcache); 40824969c119SAndrea Arcangeli } 40832799e775SMiaohe Lin if (si) 40842799e775SMiaohe Lin put_swap_device(si); 408565500d23SHugh Dickins return ret; 40861da177e4SLinus Torvalds } 40871da177e4SLinus Torvalds 40881da177e4SLinus Torvalds /* 4089c1e8d7c6SMichel Lespinasse * We enter with non-exclusive mmap_lock (to exclude vma changes, 40908f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 4091c1e8d7c6SMichel Lespinasse * We return with mmap_lock still held, but pte unmapped and unlocked. 40921da177e4SLinus Torvalds */ 40932b740303SSouptick Joarder static vm_fault_t do_anonymous_page(struct vm_fault *vmf) 40941da177e4SLinus Torvalds { 40952bad466cSPeter Xu bool uffd_wp = vmf_orig_pte_uffd_wp(vmf); 409682b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 40976bc56a4dSMatthew Wilcox (Oracle) struct folio *folio; 40982b740303SSouptick Joarder vm_fault_t ret = 0; 40991da177e4SLinus Torvalds pte_t entry; 41001da177e4SLinus Torvalds 41016b7339f4SKirill A. Shutemov /* File mapping without ->vm_ops ? */ 41026b7339f4SKirill A. Shutemov if (vma->vm_flags & VM_SHARED) 41036b7339f4SKirill A. Shutemov return VM_FAULT_SIGBUS; 41046b7339f4SKirill A. Shutemov 41057267ec00SKirill A. Shutemov /* 41063db82b93SHugh Dickins * Use pte_alloc() instead of pte_alloc_map(), so that OOM can 41073db82b93SHugh Dickins * be distinguished from a transient failure of pte_offset_map(). 41087267ec00SKirill A. Shutemov */ 41094cf58924SJoel Fernandes (Google) if (pte_alloc(vma->vm_mm, vmf->pmd)) 41107267ec00SKirill A. Shutemov return VM_FAULT_OOM; 41117267ec00SKirill A. Shutemov 411211ac5524SLinus Torvalds /* Use the zero-page for reads */ 411382b0f8c3SJan Kara if (!(vmf->flags & FAULT_FLAG_WRITE) && 4114bae473a4SKirill A. Shutemov !mm_forbids_zeropage(vma->vm_mm)) { 411582b0f8c3SJan Kara entry = pte_mkspecial(pfn_pte(my_zero_pfn(vmf->address), 411662eede62SHugh Dickins vma->vm_page_prot)); 411782b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 411882b0f8c3SJan Kara vmf->address, &vmf->ptl); 41193db82b93SHugh Dickins if (!vmf->pte) 41203db82b93SHugh Dickins goto unlock; 41212bad466cSPeter Xu if (vmf_pte_changed(vmf)) { 41227df67697SBibo Mao update_mmu_tlb(vma, vmf->address, vmf->pte); 4123a13ea5b7SHugh Dickins goto unlock; 41247df67697SBibo Mao } 41256b31d595SMichal Hocko ret = check_stable_address_space(vma->vm_mm); 41266b31d595SMichal Hocko if (ret) 41276b31d595SMichal Hocko goto unlock; 41286b251fc9SAndrea Arcangeli /* Deliver the page fault to userland, check inside PT lock */ 41296b251fc9SAndrea Arcangeli if (userfaultfd_missing(vma)) { 413082b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 413182b0f8c3SJan Kara return handle_userfault(vmf, VM_UFFD_MISSING); 41326b251fc9SAndrea Arcangeli } 4133a13ea5b7SHugh Dickins goto setpte; 4134a13ea5b7SHugh Dickins } 4135a13ea5b7SHugh Dickins 41361da177e4SLinus Torvalds /* Allocate our own private page. */ 41371da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 413865500d23SHugh Dickins goto oom; 41396bc56a4dSMatthew Wilcox (Oracle) folio = vma_alloc_zeroed_movable_folio(vma, vmf->address); 41406bc56a4dSMatthew Wilcox (Oracle) if (!folio) 414165500d23SHugh Dickins goto oom; 4142eb3c24f3SMel Gorman 41436bc56a4dSMatthew Wilcox (Oracle) if (mem_cgroup_charge(folio, vma->vm_mm, GFP_KERNEL)) 4144eb3c24f3SMel Gorman goto oom_free_page; 4145e2bf3e2cSKefeng Wang folio_throttle_swaprate(folio, GFP_KERNEL); 4146eb3c24f3SMel Gorman 414752f37629SMinchan Kim /* 4148cb3184deSMatthew Wilcox (Oracle) * The memory barrier inside __folio_mark_uptodate makes sure that 4149f4f5329dSWei Yang * preceding stores to the page contents become visible before 415052f37629SMinchan Kim * the set_pte_at() write. 415152f37629SMinchan Kim */ 4152cb3184deSMatthew Wilcox (Oracle) __folio_mark_uptodate(folio); 41531da177e4SLinus Torvalds 4154cb3184deSMatthew Wilcox (Oracle) entry = mk_pte(&folio->page, vma->vm_page_prot); 415550c25ee9SThomas Bogendoerfer entry = pte_sw_mkyoung(entry); 41561ac0cb5dSHugh Dickins if (vma->vm_flags & VM_WRITE) 4157161e393cSRick Edgecombe entry = pte_mkwrite(pte_mkdirty(entry), vma); 41588f4e2101SHugh Dickins 415982b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 416082b0f8c3SJan Kara &vmf->ptl); 41613db82b93SHugh Dickins if (!vmf->pte) 41623db82b93SHugh Dickins goto release; 41632bad466cSPeter Xu if (vmf_pte_changed(vmf)) { 4164bce8cb3cSQi Zheng update_mmu_tlb(vma, vmf->address, vmf->pte); 41658f4e2101SHugh Dickins goto release; 41667df67697SBibo Mao } 41679ba69294SHugh Dickins 41686b31d595SMichal Hocko ret = check_stable_address_space(vma->vm_mm); 41696b31d595SMichal Hocko if (ret) 41706b31d595SMichal Hocko goto release; 41716b31d595SMichal Hocko 41726b251fc9SAndrea Arcangeli /* Deliver the page fault to userland, check inside PT lock */ 41736b251fc9SAndrea Arcangeli if (userfaultfd_missing(vma)) { 417482b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 4175cb3184deSMatthew Wilcox (Oracle) folio_put(folio); 417682b0f8c3SJan Kara return handle_userfault(vmf, VM_UFFD_MISSING); 41776b251fc9SAndrea Arcangeli } 41786b251fc9SAndrea Arcangeli 4179f1a79412SShakeel Butt inc_mm_counter(vma->vm_mm, MM_ANONPAGES); 4180cb3184deSMatthew Wilcox (Oracle) folio_add_new_anon_rmap(folio, vma, vmf->address); 4181cb3184deSMatthew Wilcox (Oracle) folio_add_lru_vma(folio, vma); 4182a13ea5b7SHugh Dickins setpte: 41832bad466cSPeter Xu if (uffd_wp) 41842bad466cSPeter Xu entry = pte_mkuffd_wp(entry); 418582b0f8c3SJan Kara set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry); 41861da177e4SLinus Torvalds 41871da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 41885003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vma, vmf->address, vmf->pte, 1); 418965500d23SHugh Dickins unlock: 41903db82b93SHugh Dickins if (vmf->pte) 419182b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 41926b31d595SMichal Hocko return ret; 41938f4e2101SHugh Dickins release: 4194cb3184deSMatthew Wilcox (Oracle) folio_put(folio); 41958f4e2101SHugh Dickins goto unlock; 41968a9f3ccdSBalbir Singh oom_free_page: 4197cb3184deSMatthew Wilcox (Oracle) folio_put(folio); 419865500d23SHugh Dickins oom: 41991da177e4SLinus Torvalds return VM_FAULT_OOM; 42001da177e4SLinus Torvalds } 42011da177e4SLinus Torvalds 42029a95f3cfSPaul Cassella /* 4203c1e8d7c6SMichel Lespinasse * The mmap_lock must have been held on entry, and may have been 42049a95f3cfSPaul Cassella * released depending on flags and vma->vm_ops->fault() return value. 42059a95f3cfSPaul Cassella * See filemap_fault() and __lock_page_retry(). 42069a95f3cfSPaul Cassella */ 42072b740303SSouptick Joarder static vm_fault_t __do_fault(struct vm_fault *vmf) 42087eae74afSKirill A. Shutemov { 420982b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 42102b740303SSouptick Joarder vm_fault_t ret; 42117eae74afSKirill A. Shutemov 421263f3655fSMichal Hocko /* 421363f3655fSMichal Hocko * Preallocate pte before we take page_lock because this might lead to 421463f3655fSMichal Hocko * deadlocks for memcg reclaim which waits for pages under writeback: 421563f3655fSMichal Hocko * lock_page(A) 421663f3655fSMichal Hocko * SetPageWriteback(A) 421763f3655fSMichal Hocko * unlock_page(A) 421863f3655fSMichal Hocko * lock_page(B) 421963f3655fSMichal Hocko * lock_page(B) 4220d383807aSYanfei Xu * pte_alloc_one 422163f3655fSMichal Hocko * shrink_page_list 422263f3655fSMichal Hocko * wait_on_page_writeback(A) 422363f3655fSMichal Hocko * SetPageWriteback(B) 422463f3655fSMichal Hocko * unlock_page(B) 422563f3655fSMichal Hocko * # flush A, B to clear the writeback 422663f3655fSMichal Hocko */ 422763f3655fSMichal Hocko if (pmd_none(*vmf->pmd) && !vmf->prealloc_pte) { 4228a7069ee3SYanfei Xu vmf->prealloc_pte = pte_alloc_one(vma->vm_mm); 422963f3655fSMichal Hocko if (!vmf->prealloc_pte) 423063f3655fSMichal Hocko return VM_FAULT_OOM; 423163f3655fSMichal Hocko } 423263f3655fSMichal Hocko 423311bac800SDave Jiang ret = vma->vm_ops->fault(vmf); 42343917048dSJan Kara if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY | 4235b1aa812bSJan Kara VM_FAULT_DONE_COW))) 42367eae74afSKirill A. Shutemov return ret; 42377eae74afSKirill A. Shutemov 4238667240e0SJan Kara if (unlikely(PageHWPoison(vmf->page))) { 42393149c79fSRik van Riel struct page *page = vmf->page; 4240e53ac737SRik van Riel vm_fault_t poisonret = VM_FAULT_HWPOISON; 4241e53ac737SRik van Riel if (ret & VM_FAULT_LOCKED) { 42423149c79fSRik van Riel if (page_mapped(page)) 42433149c79fSRik van Riel unmap_mapping_pages(page_mapping(page), 42443149c79fSRik van Riel page->index, 1, false); 4245e53ac737SRik van Riel /* Retry if a clean page was removed from the cache. */ 42463149c79fSRik van Riel if (invalidate_inode_page(page)) 42473149c79fSRik van Riel poisonret = VM_FAULT_NOPAGE; 42483149c79fSRik van Riel unlock_page(page); 4249e53ac737SRik van Riel } 42503149c79fSRik van Riel put_page(page); 4251936ca80dSJan Kara vmf->page = NULL; 4252e53ac737SRik van Riel return poisonret; 42537eae74afSKirill A. Shutemov } 42547eae74afSKirill A. Shutemov 42557eae74afSKirill A. Shutemov if (unlikely(!(ret & VM_FAULT_LOCKED))) 4256667240e0SJan Kara lock_page(vmf->page); 42577eae74afSKirill A. Shutemov else 4258667240e0SJan Kara VM_BUG_ON_PAGE(!PageLocked(vmf->page), vmf->page); 42597eae74afSKirill A. Shutemov 42607eae74afSKirill A. Shutemov return ret; 42617eae74afSKirill A. Shutemov } 42627eae74afSKirill A. Shutemov 4263396bcc52SMatthew Wilcox (Oracle) #ifdef CONFIG_TRANSPARENT_HUGEPAGE 426482b0f8c3SJan Kara static void deposit_prealloc_pte(struct vm_fault *vmf) 4265953c66c2SAneesh Kumar K.V { 426682b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 4267953c66c2SAneesh Kumar K.V 426882b0f8c3SJan Kara pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, vmf->prealloc_pte); 4269953c66c2SAneesh Kumar K.V /* 4270953c66c2SAneesh Kumar K.V * We are going to consume the prealloc table, 4271953c66c2SAneesh Kumar K.V * count that as nr_ptes. 4272953c66c2SAneesh Kumar K.V */ 4273c4812909SKirill A. Shutemov mm_inc_nr_ptes(vma->vm_mm); 42747f2b6ce8STobin C Harding vmf->prealloc_pte = NULL; 4275953c66c2SAneesh Kumar K.V } 4276953c66c2SAneesh Kumar K.V 4277f9ce0be7SKirill A. Shutemov vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page) 427810102459SKirill A. Shutemov { 427982b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 428082b0f8c3SJan Kara bool write = vmf->flags & FAULT_FLAG_WRITE; 428182b0f8c3SJan Kara unsigned long haddr = vmf->address & HPAGE_PMD_MASK; 428210102459SKirill A. Shutemov pmd_t entry; 4283d01ac3c3SMatthew Wilcox (Oracle) vm_fault_t ret = VM_FAULT_FALLBACK; 428410102459SKirill A. Shutemov 428510102459SKirill A. Shutemov if (!transhuge_vma_suitable(vma, haddr)) 4286d01ac3c3SMatthew Wilcox (Oracle) return ret; 428710102459SKirill A. Shutemov 428810102459SKirill A. Shutemov page = compound_head(page); 4289d01ac3c3SMatthew Wilcox (Oracle) if (compound_order(page) != HPAGE_PMD_ORDER) 4290d01ac3c3SMatthew Wilcox (Oracle) return ret; 429110102459SKirill A. Shutemov 4292953c66c2SAneesh Kumar K.V /* 4293eac96c3eSYang Shi * Just backoff if any subpage of a THP is corrupted otherwise 4294eac96c3eSYang Shi * the corrupted page may mapped by PMD silently to escape the 4295eac96c3eSYang Shi * check. This kind of THP just can be PTE mapped. Access to 4296eac96c3eSYang Shi * the corrupted subpage should trigger SIGBUS as expected. 4297eac96c3eSYang Shi */ 4298eac96c3eSYang Shi if (unlikely(PageHasHWPoisoned(page))) 4299eac96c3eSYang Shi return ret; 4300eac96c3eSYang Shi 4301eac96c3eSYang Shi /* 4302f0953a1bSIngo Molnar * Archs like ppc64 need additional space to store information 4303953c66c2SAneesh Kumar K.V * related to pte entry. Use the preallocated table for that. 4304953c66c2SAneesh Kumar K.V */ 430582b0f8c3SJan Kara if (arch_needs_pgtable_deposit() && !vmf->prealloc_pte) { 43064cf58924SJoel Fernandes (Google) vmf->prealloc_pte = pte_alloc_one(vma->vm_mm); 430782b0f8c3SJan Kara if (!vmf->prealloc_pte) 4308953c66c2SAneesh Kumar K.V return VM_FAULT_OOM; 4309953c66c2SAneesh Kumar K.V } 4310953c66c2SAneesh Kumar K.V 431182b0f8c3SJan Kara vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); 431282b0f8c3SJan Kara if (unlikely(!pmd_none(*vmf->pmd))) 431310102459SKirill A. Shutemov goto out; 431410102459SKirill A. Shutemov 43159f1f5b60SMatthew Wilcox (Oracle) flush_icache_pages(vma, page, HPAGE_PMD_NR); 431610102459SKirill A. Shutemov 431710102459SKirill A. Shutemov entry = mk_huge_pmd(page, vma->vm_page_prot); 431810102459SKirill A. Shutemov if (write) 4319f55e1014SLinus Torvalds entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); 432010102459SKirill A. Shutemov 4321fadae295SYang Shi add_mm_counter(vma->vm_mm, mm_counter_file(page), HPAGE_PMD_NR); 4322cea86fe2SHugh Dickins page_add_file_rmap(page, vma, true); 4323cea86fe2SHugh Dickins 4324953c66c2SAneesh Kumar K.V /* 4325953c66c2SAneesh Kumar K.V * deposit and withdraw with pmd lock held 4326953c66c2SAneesh Kumar K.V */ 4327953c66c2SAneesh Kumar K.V if (arch_needs_pgtable_deposit()) 432882b0f8c3SJan Kara deposit_prealloc_pte(vmf); 432910102459SKirill A. Shutemov 433082b0f8c3SJan Kara set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry); 433110102459SKirill A. Shutemov 433282b0f8c3SJan Kara update_mmu_cache_pmd(vma, haddr, vmf->pmd); 433310102459SKirill A. Shutemov 433410102459SKirill A. Shutemov /* fault is handled */ 433510102459SKirill A. Shutemov ret = 0; 433695ecedcdSKirill A. Shutemov count_vm_event(THP_FILE_MAPPED); 433710102459SKirill A. Shutemov out: 433882b0f8c3SJan Kara spin_unlock(vmf->ptl); 433910102459SKirill A. Shutemov return ret; 434010102459SKirill A. Shutemov } 434110102459SKirill A. Shutemov #else 4342f9ce0be7SKirill A. Shutemov vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page) 434310102459SKirill A. Shutemov { 4344f9ce0be7SKirill A. Shutemov return VM_FAULT_FALLBACK; 434510102459SKirill A. Shutemov } 434610102459SKirill A. Shutemov #endif 434710102459SKirill A. Shutemov 43483bd786f7SYin Fengwei /** 43493bd786f7SYin Fengwei * set_pte_range - Set a range of PTEs to point to pages in a folio. 43503bd786f7SYin Fengwei * @vmf: Fault decription. 43513bd786f7SYin Fengwei * @folio: The folio that contains @page. 43523bd786f7SYin Fengwei * @page: The first page to create a PTE for. 43533bd786f7SYin Fengwei * @nr: The number of PTEs to create. 43543bd786f7SYin Fengwei * @addr: The first address to create a PTE for. 43553bd786f7SYin Fengwei */ 43563bd786f7SYin Fengwei void set_pte_range(struct vm_fault *vmf, struct folio *folio, 43573bd786f7SYin Fengwei struct page *page, unsigned int nr, unsigned long addr) 43583bb97794SKirill A. Shutemov { 435982b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 43602bad466cSPeter Xu bool uffd_wp = vmf_orig_pte_uffd_wp(vmf); 436182b0f8c3SJan Kara bool write = vmf->flags & FAULT_FLAG_WRITE; 43623bd786f7SYin Fengwei bool prefault = in_range(vmf->address, addr, nr * PAGE_SIZE); 43633bb97794SKirill A. Shutemov pte_t entry; 43647267ec00SKirill A. Shutemov 43653bd786f7SYin Fengwei flush_icache_pages(vma, page, nr); 43663bb97794SKirill A. Shutemov entry = mk_pte(page, vma->vm_page_prot); 436746bdb427SWill Deacon 436846bdb427SWill Deacon if (prefault && arch_wants_old_prefaulted_pte()) 436946bdb427SWill Deacon entry = pte_mkold(entry); 437050c25ee9SThomas Bogendoerfer else 437150c25ee9SThomas Bogendoerfer entry = pte_sw_mkyoung(entry); 437246bdb427SWill Deacon 43733bb97794SKirill A. Shutemov if (write) 43743bb97794SKirill A. Shutemov entry = maybe_mkwrite(pte_mkdirty(entry), vma); 43759c28a205SPeter Xu if (unlikely(uffd_wp)) 4376f1eb1bacSPeter Xu entry = pte_mkuffd_wp(entry); 4377bae473a4SKirill A. Shutemov /* copy-on-write page */ 4378bae473a4SKirill A. Shutemov if (write && !(vma->vm_flags & VM_SHARED)) { 43793bd786f7SYin Fengwei add_mm_counter(vma->vm_mm, MM_ANONPAGES, nr); 43803bd786f7SYin Fengwei VM_BUG_ON_FOLIO(nr != 1, folio); 43813bd786f7SYin Fengwei folio_add_new_anon_rmap(folio, vma, addr); 43823bd786f7SYin Fengwei folio_add_lru_vma(folio, vma); 43833bb97794SKirill A. Shutemov } else { 43843bd786f7SYin Fengwei add_mm_counter(vma->vm_mm, mm_counter_file(page), nr); 43853bd786f7SYin Fengwei folio_add_file_rmap_range(folio, page, nr, vma, false); 43863bb97794SKirill A. Shutemov } 43873bd786f7SYin Fengwei set_ptes(vma->vm_mm, addr, vmf->pte, entry, nr); 43883bd786f7SYin Fengwei 43893bd786f7SYin Fengwei /* no need to invalidate: a not-present page won't be cached */ 43903bd786f7SYin Fengwei update_mmu_cache_range(vmf, vma, addr, vmf->pte, nr); 43913bb97794SKirill A. Shutemov } 43923bb97794SKirill A. Shutemov 4393f46f2adeSPeter Xu static bool vmf_pte_changed(struct vm_fault *vmf) 4394f46f2adeSPeter Xu { 4395f46f2adeSPeter Xu if (vmf->flags & FAULT_FLAG_ORIG_PTE_VALID) 4396c33c7948SRyan Roberts return !pte_same(ptep_get(vmf->pte), vmf->orig_pte); 4397f46f2adeSPeter Xu 4398c33c7948SRyan Roberts return !pte_none(ptep_get(vmf->pte)); 4399f46f2adeSPeter Xu } 4400f46f2adeSPeter Xu 44019118c0cbSJan Kara /** 44029118c0cbSJan Kara * finish_fault - finish page fault once we have prepared the page to fault 44039118c0cbSJan Kara * 44049118c0cbSJan Kara * @vmf: structure describing the fault 44059118c0cbSJan Kara * 44069118c0cbSJan Kara * This function handles all that is needed to finish a page fault once the 44079118c0cbSJan Kara * page to fault in is prepared. It handles locking of PTEs, inserts PTE for 44089118c0cbSJan Kara * given page, adds reverse page mapping, handles memcg charges and LRU 4409a862f68aSMike Rapoport * addition. 44109118c0cbSJan Kara * 44119118c0cbSJan Kara * The function expects the page to be locked and on success it consumes a 44129118c0cbSJan Kara * reference of a page being mapped (for the PTE which maps it). 4413a862f68aSMike Rapoport * 4414a862f68aSMike Rapoport * Return: %0 on success, %VM_FAULT_ code in case of error. 44159118c0cbSJan Kara */ 44162b740303SSouptick Joarder vm_fault_t finish_fault(struct vm_fault *vmf) 44179118c0cbSJan Kara { 4418f9ce0be7SKirill A. Shutemov struct vm_area_struct *vma = vmf->vma; 44199118c0cbSJan Kara struct page *page; 4420f9ce0be7SKirill A. Shutemov vm_fault_t ret; 44219118c0cbSJan Kara 44229118c0cbSJan Kara /* Did we COW the page? */ 4423f9ce0be7SKirill A. Shutemov if ((vmf->flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) 44249118c0cbSJan Kara page = vmf->cow_page; 44259118c0cbSJan Kara else 44269118c0cbSJan Kara page = vmf->page; 44276b31d595SMichal Hocko 44286b31d595SMichal Hocko /* 44296b31d595SMichal Hocko * check even for read faults because we might have lost our CoWed 44306b31d595SMichal Hocko * page 44316b31d595SMichal Hocko */ 4432f9ce0be7SKirill A. Shutemov if (!(vma->vm_flags & VM_SHARED)) { 4433f9ce0be7SKirill A. Shutemov ret = check_stable_address_space(vma->vm_mm); 4434f9ce0be7SKirill A. Shutemov if (ret) 4435f9ce0be7SKirill A. Shutemov return ret; 4436f9ce0be7SKirill A. Shutemov } 4437f9ce0be7SKirill A. Shutemov 4438f9ce0be7SKirill A. Shutemov if (pmd_none(*vmf->pmd)) { 4439f9ce0be7SKirill A. Shutemov if (PageTransCompound(page)) { 4440f9ce0be7SKirill A. Shutemov ret = do_set_pmd(vmf, page); 4441f9ce0be7SKirill A. Shutemov if (ret != VM_FAULT_FALLBACK) 4442f9ce0be7SKirill A. Shutemov return ret; 4443f9ce0be7SKirill A. Shutemov } 4444f9ce0be7SKirill A. Shutemov 444503c4f204SQi Zheng if (vmf->prealloc_pte) 444603c4f204SQi Zheng pmd_install(vma->vm_mm, vmf->pmd, &vmf->prealloc_pte); 444703c4f204SQi Zheng else if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd))) 4448f9ce0be7SKirill A. Shutemov return VM_FAULT_OOM; 4449f9ce0be7SKirill A. Shutemov } 4450f9ce0be7SKirill A. Shutemov 4451f9ce0be7SKirill A. Shutemov vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 4452f9ce0be7SKirill A. Shutemov vmf->address, &vmf->ptl); 44533db82b93SHugh Dickins if (!vmf->pte) 44543db82b93SHugh Dickins return VM_FAULT_NOPAGE; 4455f9ce0be7SKirill A. Shutemov 445670427f6eSSergei Antonov /* Re-check under ptl */ 445770427f6eSSergei Antonov if (likely(!vmf_pte_changed(vmf))) { 44583bd786f7SYin Fengwei struct folio *folio = page_folio(page); 445970427f6eSSergei Antonov 44603bd786f7SYin Fengwei set_pte_range(vmf, folio, page, 1, vmf->address); 446170427f6eSSergei Antonov ret = 0; 446270427f6eSSergei Antonov } else { 4463f9ce0be7SKirill A. Shutemov update_mmu_tlb(vma, vmf->address, vmf->pte); 446470427f6eSSergei Antonov ret = VM_FAULT_NOPAGE; 446570427f6eSSergei Antonov } 446670427f6eSSergei Antonov 44679118c0cbSJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 44689118c0cbSJan Kara return ret; 44699118c0cbSJan Kara } 44709118c0cbSJan Kara 447153d36a56SLorenzo Stoakes static unsigned long fault_around_pages __read_mostly = 447253d36a56SLorenzo Stoakes 65536 >> PAGE_SHIFT; 4473a9b0f861SKirill A. Shutemov 44741592eef0SKirill A. Shutemov #ifdef CONFIG_DEBUG_FS 4475a9b0f861SKirill A. Shutemov static int fault_around_bytes_get(void *data, u64 *val) 44761592eef0SKirill A. Shutemov { 447753d36a56SLorenzo Stoakes *val = fault_around_pages << PAGE_SHIFT; 44781592eef0SKirill A. Shutemov return 0; 44791592eef0SKirill A. Shutemov } 44801592eef0SKirill A. Shutemov 4481b4903d6eSAndrey Ryabinin /* 4482da391d64SWilliam Kucharski * fault_around_bytes must be rounded down to the nearest page order as it's 4483da391d64SWilliam Kucharski * what do_fault_around() expects to see. 4484b4903d6eSAndrey Ryabinin */ 4485a9b0f861SKirill A. Shutemov static int fault_around_bytes_set(void *data, u64 val) 44861592eef0SKirill A. Shutemov { 4487a9b0f861SKirill A. Shutemov if (val / PAGE_SIZE > PTRS_PER_PTE) 44881592eef0SKirill A. Shutemov return -EINVAL; 448953d36a56SLorenzo Stoakes 449053d36a56SLorenzo Stoakes /* 449153d36a56SLorenzo Stoakes * The minimum value is 1 page, however this results in no fault-around 449253d36a56SLorenzo Stoakes * at all. See should_fault_around(). 449353d36a56SLorenzo Stoakes */ 449453d36a56SLorenzo Stoakes fault_around_pages = max(rounddown_pow_of_two(val) >> PAGE_SHIFT, 1UL); 449553d36a56SLorenzo Stoakes 44961592eef0SKirill A. Shutemov return 0; 44971592eef0SKirill A. Shutemov } 44980a1345f8SYevgen Pronenko DEFINE_DEBUGFS_ATTRIBUTE(fault_around_bytes_fops, 4499a9b0f861SKirill A. Shutemov fault_around_bytes_get, fault_around_bytes_set, "%llu\n"); 45001592eef0SKirill A. Shutemov 45011592eef0SKirill A. Shutemov static int __init fault_around_debugfs(void) 45021592eef0SKirill A. Shutemov { 4503d9f7979cSGreg Kroah-Hartman debugfs_create_file_unsafe("fault_around_bytes", 0644, NULL, NULL, 4504a9b0f861SKirill A. Shutemov &fault_around_bytes_fops); 45051592eef0SKirill A. Shutemov return 0; 45061592eef0SKirill A. Shutemov } 45071592eef0SKirill A. Shutemov late_initcall(fault_around_debugfs); 45081592eef0SKirill A. Shutemov #endif 45098c6e50b0SKirill A. Shutemov 45101fdb412bSKirill A. Shutemov /* 45111fdb412bSKirill A. Shutemov * do_fault_around() tries to map few pages around the fault address. The hope 45121fdb412bSKirill A. Shutemov * is that the pages will be needed soon and this will lower the number of 45131fdb412bSKirill A. Shutemov * faults to handle. 45141fdb412bSKirill A. Shutemov * 45151fdb412bSKirill A. Shutemov * It uses vm_ops->map_pages() to map the pages, which skips the page if it's 45161fdb412bSKirill A. Shutemov * not ready to be mapped: not up-to-date, locked, etc. 45171fdb412bSKirill A. Shutemov * 45189042599eSLorenzo Stoakes * This function doesn't cross VMA or page table boundaries, in order to call 45199042599eSLorenzo Stoakes * map_pages() and acquire a PTE lock only once. 45201fdb412bSKirill A. Shutemov * 452153d36a56SLorenzo Stoakes * fault_around_pages defines how many pages we'll try to map. 4522da391d64SWilliam Kucharski * do_fault_around() expects it to be set to a power of two less than or equal 4523da391d64SWilliam Kucharski * to PTRS_PER_PTE. 45241fdb412bSKirill A. Shutemov * 4525da391d64SWilliam Kucharski * The virtual address of the area that we map is naturally aligned to 452653d36a56SLorenzo Stoakes * fault_around_pages * PAGE_SIZE rounded down to the machine page size 4527da391d64SWilliam Kucharski * (and therefore to page order). This way it's easier to guarantee 4528da391d64SWilliam Kucharski * that we don't cross page table boundaries. 45291fdb412bSKirill A. Shutemov */ 45302b740303SSouptick Joarder static vm_fault_t do_fault_around(struct vm_fault *vmf) 45318c6e50b0SKirill A. Shutemov { 453253d36a56SLorenzo Stoakes pgoff_t nr_pages = READ_ONCE(fault_around_pages); 45339042599eSLorenzo Stoakes pgoff_t pte_off = pte_index(vmf->address); 45349042599eSLorenzo Stoakes /* The page offset of vmf->address within the VMA. */ 45359042599eSLorenzo Stoakes pgoff_t vma_off = vmf->pgoff - vmf->vma->vm_pgoff; 45369042599eSLorenzo Stoakes pgoff_t from_pte, to_pte; 453758ef47efSMatthew Wilcox (Oracle) vm_fault_t ret; 45388c6e50b0SKirill A. Shutemov 45399042599eSLorenzo Stoakes /* The PTE offset of the start address, clamped to the VMA. */ 45409042599eSLorenzo Stoakes from_pte = max(ALIGN_DOWN(pte_off, nr_pages), 45419042599eSLorenzo Stoakes pte_off - min(pte_off, vma_off)); 4542aecd6f44SKirill A. Shutemov 45439042599eSLorenzo Stoakes /* The PTE offset of the end address, clamped to the VMA and PTE. */ 45449042599eSLorenzo Stoakes to_pte = min3(from_pte + nr_pages, (pgoff_t)PTRS_PER_PTE, 45459042599eSLorenzo Stoakes pte_off + vma_pages(vmf->vma) - vma_off) - 1; 45468c6e50b0SKirill A. Shutemov 454782b0f8c3SJan Kara if (pmd_none(*vmf->pmd)) { 45484cf58924SJoel Fernandes (Google) vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm); 454982b0f8c3SJan Kara if (!vmf->prealloc_pte) 4550f9ce0be7SKirill A. Shutemov return VM_FAULT_OOM; 45518c6e50b0SKirill A. Shutemov } 45528c6e50b0SKirill A. Shutemov 455358ef47efSMatthew Wilcox (Oracle) rcu_read_lock(); 455458ef47efSMatthew Wilcox (Oracle) ret = vmf->vma->vm_ops->map_pages(vmf, 45559042599eSLorenzo Stoakes vmf->pgoff + from_pte - pte_off, 45569042599eSLorenzo Stoakes vmf->pgoff + to_pte - pte_off); 455758ef47efSMatthew Wilcox (Oracle) rcu_read_unlock(); 455858ef47efSMatthew Wilcox (Oracle) 455958ef47efSMatthew Wilcox (Oracle) return ret; 45607267ec00SKirill A. Shutemov } 45617267ec00SKirill A. Shutemov 45629c28a205SPeter Xu /* Return true if we should do read fault-around, false otherwise */ 45639c28a205SPeter Xu static inline bool should_fault_around(struct vm_fault *vmf) 45649c28a205SPeter Xu { 45659c28a205SPeter Xu /* No ->map_pages? No way to fault around... */ 45669c28a205SPeter Xu if (!vmf->vma->vm_ops->map_pages) 45679c28a205SPeter Xu return false; 45689c28a205SPeter Xu 45699c28a205SPeter Xu if (uffd_disable_fault_around(vmf->vma)) 45709c28a205SPeter Xu return false; 45719c28a205SPeter Xu 457253d36a56SLorenzo Stoakes /* A single page implies no faulting 'around' at all. */ 457353d36a56SLorenzo Stoakes return fault_around_pages > 1; 45749c28a205SPeter Xu } 45759c28a205SPeter Xu 45762b740303SSouptick Joarder static vm_fault_t do_read_fault(struct vm_fault *vmf) 4577e655fb29SKirill A. Shutemov { 45782b740303SSouptick Joarder vm_fault_t ret = 0; 457922d1e68fSSidhartha Kumar struct folio *folio; 45808c6e50b0SKirill A. Shutemov 45818c6e50b0SKirill A. Shutemov /* 45828c6e50b0SKirill A. Shutemov * Let's call ->map_pages() first and use ->fault() as fallback 45838c6e50b0SKirill A. Shutemov * if page by the offset is not ready to be mapped (cold cache or 45848c6e50b0SKirill A. Shutemov * something). 45858c6e50b0SKirill A. Shutemov */ 45869c28a205SPeter Xu if (should_fault_around(vmf)) { 45870721ec8bSJan Kara ret = do_fault_around(vmf); 45887267ec00SKirill A. Shutemov if (ret) 45897267ec00SKirill A. Shutemov return ret; 45908c6e50b0SKirill A. Shutemov } 4591e655fb29SKirill A. Shutemov 4592f5617ffeSMatthew Wilcox (Oracle) if (vmf->flags & FAULT_FLAG_VMA_LOCK) { 4593f5617ffeSMatthew Wilcox (Oracle) vma_end_read(vmf->vma); 4594f5617ffeSMatthew Wilcox (Oracle) return VM_FAULT_RETRY; 4595f5617ffeSMatthew Wilcox (Oracle) } 4596f5617ffeSMatthew Wilcox (Oracle) 4597936ca80dSJan Kara ret = __do_fault(vmf); 4598e655fb29SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 4599e655fb29SKirill A. Shutemov return ret; 4600e655fb29SKirill A. Shutemov 46019118c0cbSJan Kara ret |= finish_fault(vmf); 460222d1e68fSSidhartha Kumar folio = page_folio(vmf->page); 460322d1e68fSSidhartha Kumar folio_unlock(folio); 46047267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 460522d1e68fSSidhartha Kumar folio_put(folio); 4606e655fb29SKirill A. Shutemov return ret; 4607e655fb29SKirill A. Shutemov } 4608e655fb29SKirill A. Shutemov 46092b740303SSouptick Joarder static vm_fault_t do_cow_fault(struct vm_fault *vmf) 4610ec47c3b9SKirill A. Shutemov { 461182b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 46122b740303SSouptick Joarder vm_fault_t ret; 4613ec47c3b9SKirill A. Shutemov 461461a4b8d3SMatthew Wilcox (Oracle) if (vmf->flags & FAULT_FLAG_VMA_LOCK) { 461561a4b8d3SMatthew Wilcox (Oracle) vma_end_read(vma); 461661a4b8d3SMatthew Wilcox (Oracle) return VM_FAULT_RETRY; 461761a4b8d3SMatthew Wilcox (Oracle) } 461861a4b8d3SMatthew Wilcox (Oracle) 4619ec47c3b9SKirill A. Shutemov if (unlikely(anon_vma_prepare(vma))) 4620ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 4621ec47c3b9SKirill A. Shutemov 4622936ca80dSJan Kara vmf->cow_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vmf->address); 4623936ca80dSJan Kara if (!vmf->cow_page) 4624ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 4625ec47c3b9SKirill A. Shutemov 46268f425e4eSMatthew Wilcox (Oracle) if (mem_cgroup_charge(page_folio(vmf->cow_page), vma->vm_mm, 46278f425e4eSMatthew Wilcox (Oracle) GFP_KERNEL)) { 4628936ca80dSJan Kara put_page(vmf->cow_page); 4629ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 4630ec47c3b9SKirill A. Shutemov } 463168fa572bSKefeng Wang folio_throttle_swaprate(page_folio(vmf->cow_page), GFP_KERNEL); 4632ec47c3b9SKirill A. Shutemov 4633936ca80dSJan Kara ret = __do_fault(vmf); 4634ec47c3b9SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 4635ec47c3b9SKirill A. Shutemov goto uncharge_out; 46363917048dSJan Kara if (ret & VM_FAULT_DONE_COW) 46373917048dSJan Kara return ret; 4638ec47c3b9SKirill A. Shutemov 4639936ca80dSJan Kara copy_user_highpage(vmf->cow_page, vmf->page, vmf->address, vma); 4640936ca80dSJan Kara __SetPageUptodate(vmf->cow_page); 4641ec47c3b9SKirill A. Shutemov 46429118c0cbSJan Kara ret |= finish_fault(vmf); 4643936ca80dSJan Kara unlock_page(vmf->page); 4644936ca80dSJan Kara put_page(vmf->page); 46457267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 46467267ec00SKirill A. Shutemov goto uncharge_out; 4647ec47c3b9SKirill A. Shutemov return ret; 4648ec47c3b9SKirill A. Shutemov uncharge_out: 4649936ca80dSJan Kara put_page(vmf->cow_page); 4650ec47c3b9SKirill A. Shutemov return ret; 4651ec47c3b9SKirill A. Shutemov } 4652ec47c3b9SKirill A. Shutemov 46532b740303SSouptick Joarder static vm_fault_t do_shared_fault(struct vm_fault *vmf) 46541da177e4SLinus Torvalds { 465582b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 46562b740303SSouptick Joarder vm_fault_t ret, tmp; 46576f609b7eSSidhartha Kumar struct folio *folio; 46581d65f86dSKAMEZAWA Hiroyuki 465961a4b8d3SMatthew Wilcox (Oracle) if (vmf->flags & FAULT_FLAG_VMA_LOCK) { 466061a4b8d3SMatthew Wilcox (Oracle) vma_end_read(vma); 466161a4b8d3SMatthew Wilcox (Oracle) return VM_FAULT_RETRY; 466261a4b8d3SMatthew Wilcox (Oracle) } 466354cb8821SNick Piggin 4664936ca80dSJan Kara ret = __do_fault(vmf); 46657eae74afSKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 4666f0c6d4d2SKirill A. Shutemov return ret; 46671da177e4SLinus Torvalds 46686f609b7eSSidhartha Kumar folio = page_folio(vmf->page); 46696f609b7eSSidhartha Kumar 46701da177e4SLinus Torvalds /* 4671f0c6d4d2SKirill A. Shutemov * Check if the backing address space wants to know that the page is 4672f0c6d4d2SKirill A. Shutemov * about to become writable 46731da177e4SLinus Torvalds */ 4674fb09a464SKirill A. Shutemov if (vma->vm_ops->page_mkwrite) { 46756f609b7eSSidhartha Kumar folio_unlock(folio); 467686aa6998SSidhartha Kumar tmp = do_page_mkwrite(vmf, folio); 4677fb09a464SKirill A. Shutemov if (unlikely(!tmp || 4678fb09a464SKirill A. Shutemov (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { 46796f609b7eSSidhartha Kumar folio_put(folio); 4680f0c6d4d2SKirill A. Shutemov return tmp; 468169676147SMark Fasheh } 4682d0217ac0SNick Piggin } 4683fb09a464SKirill A. Shutemov 46849118c0cbSJan Kara ret |= finish_fault(vmf); 46857267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | 46867267ec00SKirill A. Shutemov VM_FAULT_RETRY))) { 46876f609b7eSSidhartha Kumar folio_unlock(folio); 46886f609b7eSSidhartha Kumar folio_put(folio); 4689f0c6d4d2SKirill A. Shutemov return ret; 46909637a5efSDavid Howells } 4691d00806b1SNick Piggin 469289b15332SJohannes Weiner ret |= fault_dirty_shared_page(vmf); 4693b827e496SNick Piggin return ret; 469454cb8821SNick Piggin } 4695d00806b1SNick Piggin 46969a95f3cfSPaul Cassella /* 4697c1e8d7c6SMichel Lespinasse * We enter with non-exclusive mmap_lock (to exclude vma changes, 46989a95f3cfSPaul Cassella * but allow concurrent faults). 4699c1e8d7c6SMichel Lespinasse * The mmap_lock may have been released depending on flags and our 47009138e47eSMatthew Wilcox (Oracle) * return value. See filemap_fault() and __folio_lock_or_retry(). 4701c1e8d7c6SMichel Lespinasse * If mmap_lock is released, vma may become invalid (for example 4702fc8efd2dSJan Stancek * by other thread calling munmap()). 47039a95f3cfSPaul Cassella */ 47042b740303SSouptick Joarder static vm_fault_t do_fault(struct vm_fault *vmf) 470554cb8821SNick Piggin { 470682b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 4707fc8efd2dSJan Stancek struct mm_struct *vm_mm = vma->vm_mm; 47082b740303SSouptick Joarder vm_fault_t ret; 470954cb8821SNick Piggin 4710ff09d7ecSAneesh Kumar K.V /* 4711ff09d7ecSAneesh Kumar K.V * The VMA was not fully populated on mmap() or missing VM_DONTEXPAND 4712ff09d7ecSAneesh Kumar K.V */ 4713ff09d7ecSAneesh Kumar K.V if (!vma->vm_ops->fault) { 47143db82b93SHugh Dickins vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, 47153db82b93SHugh Dickins vmf->address, &vmf->ptl); 47163db82b93SHugh Dickins if (unlikely(!vmf->pte)) 4717b0b9b3dfSHugh Dickins ret = VM_FAULT_SIGBUS; 4718ff09d7ecSAneesh Kumar K.V else { 4719ff09d7ecSAneesh Kumar K.V /* 4720ff09d7ecSAneesh Kumar K.V * Make sure this is not a temporary clearing of pte 4721ff09d7ecSAneesh Kumar K.V * by holding ptl and checking again. A R/M/W update 4722ff09d7ecSAneesh Kumar K.V * of pte involves: take ptl, clearing the pte so that 4723ff09d7ecSAneesh Kumar K.V * we don't have concurrent modification by hardware 4724ff09d7ecSAneesh Kumar K.V * followed by an update. 4725ff09d7ecSAneesh Kumar K.V */ 4726c33c7948SRyan Roberts if (unlikely(pte_none(ptep_get(vmf->pte)))) 4727ff09d7ecSAneesh Kumar K.V ret = VM_FAULT_SIGBUS; 4728ff09d7ecSAneesh Kumar K.V else 4729ff09d7ecSAneesh Kumar K.V ret = VM_FAULT_NOPAGE; 4730ff09d7ecSAneesh Kumar K.V 4731ff09d7ecSAneesh Kumar K.V pte_unmap_unlock(vmf->pte, vmf->ptl); 4732ff09d7ecSAneesh Kumar K.V } 4733ff09d7ecSAneesh Kumar K.V } else if (!(vmf->flags & FAULT_FLAG_WRITE)) 4734b0b9b3dfSHugh Dickins ret = do_read_fault(vmf); 4735b0b9b3dfSHugh Dickins else if (!(vma->vm_flags & VM_SHARED)) 4736b0b9b3dfSHugh Dickins ret = do_cow_fault(vmf); 4737b0b9b3dfSHugh Dickins else 4738b0b9b3dfSHugh Dickins ret = do_shared_fault(vmf); 4739b0b9b3dfSHugh Dickins 4740b0b9b3dfSHugh Dickins /* preallocated pagetable is unused: free it */ 4741b0b9b3dfSHugh Dickins if (vmf->prealloc_pte) { 4742fc8efd2dSJan Stancek pte_free(vm_mm, vmf->prealloc_pte); 47437f2b6ce8STobin C Harding vmf->prealloc_pte = NULL; 4744b0b9b3dfSHugh Dickins } 4745b0b9b3dfSHugh Dickins return ret; 474654cb8821SNick Piggin } 474754cb8821SNick Piggin 4748cda6d936SKefeng Wang int numa_migrate_prep(struct folio *folio, struct vm_area_struct *vma, 4749f4c0d836SYang Shi unsigned long addr, int page_nid, int *flags) 47509532fec1SMel Gorman { 4751cda6d936SKefeng Wang folio_get(folio); 47529532fec1SMel Gorman 4753fc137c0dSRaghavendra K T /* Record the current PID acceesing VMA */ 4754fc137c0dSRaghavendra K T vma_set_access_pid_bit(vma); 4755fc137c0dSRaghavendra K T 47569532fec1SMel Gorman count_vm_numa_event(NUMA_HINT_FAULTS); 475704bb2f94SRik van Riel if (page_nid == numa_node_id()) { 47589532fec1SMel Gorman count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL); 475904bb2f94SRik van Riel *flags |= TNF_FAULT_LOCAL; 476004bb2f94SRik van Riel } 47619532fec1SMel Gorman 476275c70128SKefeng Wang return mpol_misplaced(folio, vma, addr); 47639532fec1SMel Gorman } 47649532fec1SMel Gorman 47652b740303SSouptick Joarder static vm_fault_t do_numa_page(struct vm_fault *vmf) 4766d10e63f2SMel Gorman { 476782b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 47686695cf68SKefeng Wang struct folio *folio = NULL; 47696695cf68SKefeng Wang int nid = NUMA_NO_NODE; 47706a56ccbcSDavid Hildenbrand bool writable = false; 477190572890SPeter Zijlstra int last_cpupid; 4772cbee9f88SPeter Zijlstra int target_nid; 477304a86453SAneesh Kumar K.V pte_t pte, old_pte; 47746688cc05SPeter Zijlstra int flags = 0; 4775d10e63f2SMel Gorman 4776d10e63f2SMel Gorman /* 4777d10e63f2SMel Gorman * The "pte" at this point cannot be used safely without 4778d10e63f2SMel Gorman * validation through pte_unmap_same(). It's of NUMA type but 4779d10e63f2SMel Gorman * the pfn may be screwed if the read is non atomic. 4780d10e63f2SMel Gorman */ 478182b0f8c3SJan Kara spin_lock(vmf->ptl); 4782c33c7948SRyan Roberts if (unlikely(!pte_same(ptep_get(vmf->pte), vmf->orig_pte))) { 478382b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 47844daae3b4SMel Gorman goto out; 47854daae3b4SMel Gorman } 47864daae3b4SMel Gorman 4787b99a342dSHuang Ying /* Get the normal PTE */ 4788b99a342dSHuang Ying old_pte = ptep_get(vmf->pte); 478904a86453SAneesh Kumar K.V pte = pte_modify(old_pte, vma->vm_page_prot); 4790d10e63f2SMel Gorman 47916a56ccbcSDavid Hildenbrand /* 47926a56ccbcSDavid Hildenbrand * Detect now whether the PTE could be writable; this information 47936a56ccbcSDavid Hildenbrand * is only valid while holding the PT lock. 47946a56ccbcSDavid Hildenbrand */ 47956a56ccbcSDavid Hildenbrand writable = pte_write(pte); 47966a56ccbcSDavid Hildenbrand if (!writable && vma_wants_manual_pte_write_upgrade(vma) && 47976a56ccbcSDavid Hildenbrand can_change_pte_writable(vma, vmf->address, pte)) 47986a56ccbcSDavid Hildenbrand writable = true; 47996a56ccbcSDavid Hildenbrand 48006695cf68SKefeng Wang folio = vm_normal_folio(vma, vmf->address, pte); 48016695cf68SKefeng Wang if (!folio || folio_is_zone_device(folio)) 4802b99a342dSHuang Ying goto out_map; 4803d10e63f2SMel Gorman 4804e81c4802SKirill A. Shutemov /* TODO: handle PTE-mapped THP */ 48056695cf68SKefeng Wang if (folio_test_large(folio)) 4806b99a342dSHuang Ying goto out_map; 4807e81c4802SKirill A. Shutemov 48086688cc05SPeter Zijlstra /* 4809bea66fbdSMel Gorman * Avoid grouping on RO pages in general. RO pages shouldn't hurt as 4810bea66fbdSMel Gorman * much anyway since they can be in shared cache state. This misses 4811bea66fbdSMel Gorman * the case where a mapping is writable but the process never writes 4812bea66fbdSMel Gorman * to it but pte_write gets cleared during protection updates and 4813bea66fbdSMel Gorman * pte_dirty has unpredictable behaviour between PTE scan updates, 4814bea66fbdSMel Gorman * background writeback, dirty balancing and application behaviour. 48156688cc05SPeter Zijlstra */ 48166a56ccbcSDavid Hildenbrand if (!writable) 48176688cc05SPeter Zijlstra flags |= TNF_NO_GROUP; 48186688cc05SPeter Zijlstra 4819dabe1d99SRik van Riel /* 48206695cf68SKefeng Wang * Flag if the folio is shared between multiple address spaces. This 4821dabe1d99SRik van Riel * is later used when determining whether to group tasks together 4822dabe1d99SRik van Riel */ 48236695cf68SKefeng Wang if (folio_estimated_sharers(folio) > 1 && (vma->vm_flags & VM_SHARED)) 4824dabe1d99SRik van Riel flags |= TNF_SHARED; 4825dabe1d99SRik van Riel 48266695cf68SKefeng Wang nid = folio_nid(folio); 482733024536SHuang Ying /* 482833024536SHuang Ying * For memory tiering mode, cpupid of slow memory page is used 482933024536SHuang Ying * to record page access time. So use default value. 483033024536SHuang Ying */ 483133024536SHuang Ying if ((sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING) && 48326695cf68SKefeng Wang !node_is_toptier(nid)) 483333024536SHuang Ying last_cpupid = (-1 & LAST_CPUPID_MASK); 483433024536SHuang Ying else 48356695cf68SKefeng Wang last_cpupid = page_cpupid_last(&folio->page); 4836cda6d936SKefeng Wang target_nid = numa_migrate_prep(folio, vma, vmf->address, nid, &flags); 483798fa15f3SAnshuman Khandual if (target_nid == NUMA_NO_NODE) { 48386695cf68SKefeng Wang folio_put(folio); 4839b99a342dSHuang Ying goto out_map; 48404daae3b4SMel Gorman } 4841b99a342dSHuang Ying pte_unmap_unlock(vmf->pte, vmf->ptl); 48426a56ccbcSDavid Hildenbrand writable = false; 48434daae3b4SMel Gorman 48444daae3b4SMel Gorman /* Migrate to the requested node */ 48456695cf68SKefeng Wang if (migrate_misplaced_folio(folio, vma, target_nid)) { 48466695cf68SKefeng Wang nid = target_nid; 48476688cc05SPeter Zijlstra flags |= TNF_MIGRATED; 4848b99a342dSHuang Ying } else { 4849074c2381SMel Gorman flags |= TNF_MIGRATE_FAIL; 4850c7ad0880SHugh Dickins vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 4851c7ad0880SHugh Dickins vmf->address, &vmf->ptl); 4852c7ad0880SHugh Dickins if (unlikely(!vmf->pte)) 4853c7ad0880SHugh Dickins goto out; 4854c33c7948SRyan Roberts if (unlikely(!pte_same(ptep_get(vmf->pte), vmf->orig_pte))) { 4855b99a342dSHuang Ying pte_unmap_unlock(vmf->pte, vmf->ptl); 4856b99a342dSHuang Ying goto out; 4857b99a342dSHuang Ying } 4858b99a342dSHuang Ying goto out_map; 4859b99a342dSHuang Ying } 48604daae3b4SMel Gorman 48614daae3b4SMel Gorman out: 48626695cf68SKefeng Wang if (nid != NUMA_NO_NODE) 48636695cf68SKefeng Wang task_numa_fault(last_cpupid, nid, 1, flags); 4864d10e63f2SMel Gorman return 0; 4865b99a342dSHuang Ying out_map: 4866b99a342dSHuang Ying /* 4867b99a342dSHuang Ying * Make it present again, depending on how arch implements 4868b99a342dSHuang Ying * non-accessible ptes, some can allow access by kernel mode. 4869b99a342dSHuang Ying */ 4870b99a342dSHuang Ying old_pte = ptep_modify_prot_start(vma, vmf->address, vmf->pte); 4871b99a342dSHuang Ying pte = pte_modify(old_pte, vma->vm_page_prot); 4872b99a342dSHuang Ying pte = pte_mkyoung(pte); 48736a56ccbcSDavid Hildenbrand if (writable) 4874161e393cSRick Edgecombe pte = pte_mkwrite(pte, vma); 4875b99a342dSHuang Ying ptep_modify_prot_commit(vma, vmf->address, vmf->pte, old_pte, pte); 48765003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vma, vmf->address, vmf->pte, 1); 4877b99a342dSHuang Ying pte_unmap_unlock(vmf->pte, vmf->ptl); 4878b99a342dSHuang Ying goto out; 4879d10e63f2SMel Gorman } 4880d10e63f2SMel Gorman 48812b740303SSouptick Joarder static inline vm_fault_t create_huge_pmd(struct vm_fault *vmf) 4882b96375f7SMatthew Wilcox { 48838f5fd0e1SMatthew Wilcox (Oracle) struct vm_area_struct *vma = vmf->vma; 48848f5fd0e1SMatthew Wilcox (Oracle) if (vma_is_anonymous(vma)) 488582b0f8c3SJan Kara return do_huge_pmd_anonymous_page(vmf); 488640d49a3cSMatthew Wilcox (Oracle) if (vma->vm_ops->huge_fault) 48871d024e7aSMatthew Wilcox (Oracle) return vma->vm_ops->huge_fault(vmf, PMD_ORDER); 4888b96375f7SMatthew Wilcox return VM_FAULT_FALLBACK; 4889b96375f7SMatthew Wilcox } 4890b96375f7SMatthew Wilcox 4891183f24aaSGeert Uytterhoeven /* `inline' is required to avoid gcc 4.1.2 build error */ 48925db4f15cSYang Shi static inline vm_fault_t wp_huge_pmd(struct vm_fault *vmf) 4893b96375f7SMatthew Wilcox { 48948f5fd0e1SMatthew Wilcox (Oracle) struct vm_area_struct *vma = vmf->vma; 4895c89357e2SDavid Hildenbrand const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE; 4896aea06577SDavid Hildenbrand vm_fault_t ret; 4897c89357e2SDavid Hildenbrand 48988f5fd0e1SMatthew Wilcox (Oracle) if (vma_is_anonymous(vma)) { 4899c89357e2SDavid Hildenbrand if (likely(!unshare) && 4900*d61ea1cbSPeter Xu userfaultfd_huge_pmd_wp(vma, vmf->orig_pmd)) { 4901*d61ea1cbSPeter Xu if (userfaultfd_wp_async(vmf->vma)) 4902*d61ea1cbSPeter Xu goto split; 4903529b930bSAndrea Arcangeli return handle_userfault(vmf, VM_UFFD_WP); 4904*d61ea1cbSPeter Xu } 49055db4f15cSYang Shi return do_huge_pmd_wp_page(vmf); 4906529b930bSAndrea Arcangeli } 4907af9e4d5fSKirill A. Shutemov 49088f5fd0e1SMatthew Wilcox (Oracle) if (vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) { 49098f5fd0e1SMatthew Wilcox (Oracle) if (vma->vm_ops->huge_fault) { 49101d024e7aSMatthew Wilcox (Oracle) ret = vma->vm_ops->huge_fault(vmf, PMD_ORDER); 4911327e9fd4SThomas Hellstrom (VMware) if (!(ret & VM_FAULT_FALLBACK)) 4912327e9fd4SThomas Hellstrom (VMware) return ret; 4913327e9fd4SThomas Hellstrom (VMware) } 4914aea06577SDavid Hildenbrand } 4915327e9fd4SThomas Hellstrom (VMware) 4916*d61ea1cbSPeter Xu split: 4917327e9fd4SThomas Hellstrom (VMware) /* COW or write-notify handled on pte level: split pmd. */ 49188f5fd0e1SMatthew Wilcox (Oracle) __split_huge_pmd(vma, vmf->pmd, vmf->address, false, NULL); 4919af9e4d5fSKirill A. Shutemov 4920b96375f7SMatthew Wilcox return VM_FAULT_FALLBACK; 4921b96375f7SMatthew Wilcox } 4922b96375f7SMatthew Wilcox 49232b740303SSouptick Joarder static vm_fault_t create_huge_pud(struct vm_fault *vmf) 4924a00cc7d9SMatthew Wilcox { 4925327e9fd4SThomas Hellstrom (VMware) #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && \ 4926327e9fd4SThomas Hellstrom (VMware) defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) 4927c4fd825eSMatthew Wilcox (Oracle) struct vm_area_struct *vma = vmf->vma; 4928a00cc7d9SMatthew Wilcox /* No support for anonymous transparent PUD pages yet */ 4929c4fd825eSMatthew Wilcox (Oracle) if (vma_is_anonymous(vma)) 493014c99d65SGowans, James return VM_FAULT_FALLBACK; 493140d49a3cSMatthew Wilcox (Oracle) if (vma->vm_ops->huge_fault) 49321d024e7aSMatthew Wilcox (Oracle) return vma->vm_ops->huge_fault(vmf, PUD_ORDER); 493314c99d65SGowans, James #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 493414c99d65SGowans, James return VM_FAULT_FALLBACK; 493514c99d65SGowans, James } 493614c99d65SGowans, James 493714c99d65SGowans, James static vm_fault_t wp_huge_pud(struct vm_fault *vmf, pud_t orig_pud) 493814c99d65SGowans, James { 493914c99d65SGowans, James #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && \ 494014c99d65SGowans, James defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) 4941c4fd825eSMatthew Wilcox (Oracle) struct vm_area_struct *vma = vmf->vma; 4942aea06577SDavid Hildenbrand vm_fault_t ret; 4943aea06577SDavid Hildenbrand 494414c99d65SGowans, James /* No support for anonymous transparent PUD pages yet */ 4945c4fd825eSMatthew Wilcox (Oracle) if (vma_is_anonymous(vma)) 4946327e9fd4SThomas Hellstrom (VMware) goto split; 4947c4fd825eSMatthew Wilcox (Oracle) if (vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) { 4948c4fd825eSMatthew Wilcox (Oracle) if (vma->vm_ops->huge_fault) { 49491d024e7aSMatthew Wilcox (Oracle) ret = vma->vm_ops->huge_fault(vmf, PUD_ORDER); 4950327e9fd4SThomas Hellstrom (VMware) if (!(ret & VM_FAULT_FALLBACK)) 4951327e9fd4SThomas Hellstrom (VMware) return ret; 4952327e9fd4SThomas Hellstrom (VMware) } 4953aea06577SDavid Hildenbrand } 4954327e9fd4SThomas Hellstrom (VMware) split: 4955327e9fd4SThomas Hellstrom (VMware) /* COW or write-notify not handled on PUD level: split pud.*/ 4956c4fd825eSMatthew Wilcox (Oracle) __split_huge_pud(vma, vmf->pud, vmf->address); 495714c99d65SGowans, James #endif /* CONFIG_TRANSPARENT_HUGEPAGE && CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 4958a00cc7d9SMatthew Wilcox return VM_FAULT_FALLBACK; 4959a00cc7d9SMatthew Wilcox } 4960a00cc7d9SMatthew Wilcox 49611da177e4SLinus Torvalds /* 49621da177e4SLinus Torvalds * These routines also need to handle stuff like marking pages dirty 49631da177e4SLinus Torvalds * and/or accessed for architectures that don't do it in hardware (most 49641da177e4SLinus Torvalds * RISC architectures). The early dirtying is also good on the i386. 49651da177e4SLinus Torvalds * 49661da177e4SLinus Torvalds * There is also a hook called "update_mmu_cache()" that architectures 49671da177e4SLinus Torvalds * with external mmu caches can use to update those (ie the Sparc or 49681da177e4SLinus Torvalds * PowerPC hashed page tables that act as extended TLBs). 49691da177e4SLinus Torvalds * 4970c1e8d7c6SMichel Lespinasse * We enter with non-exclusive mmap_lock (to exclude vma changes, but allow 49717267ec00SKirill A. Shutemov * concurrent faults). 49729a95f3cfSPaul Cassella * 4973c1e8d7c6SMichel Lespinasse * The mmap_lock may have been released depending on flags and our return value. 49749138e47eSMatthew Wilcox (Oracle) * See filemap_fault() and __folio_lock_or_retry(). 49751da177e4SLinus Torvalds */ 49762b740303SSouptick Joarder static vm_fault_t handle_pte_fault(struct vm_fault *vmf) 49771da177e4SLinus Torvalds { 49781da177e4SLinus Torvalds pte_t entry; 49791da177e4SLinus Torvalds 498082b0f8c3SJan Kara if (unlikely(pmd_none(*vmf->pmd))) { 49817267ec00SKirill A. Shutemov /* 49827267ec00SKirill A. Shutemov * Leave __pte_alloc() until later: because vm_ops->fault may 49837267ec00SKirill A. Shutemov * want to allocate huge page, and if we expose page table 49847267ec00SKirill A. Shutemov * for an instant, it will be difficult to retract from 49857267ec00SKirill A. Shutemov * concurrent faults and from rmap lookups. 49867267ec00SKirill A. Shutemov */ 498782b0f8c3SJan Kara vmf->pte = NULL; 4988f46f2adeSPeter Xu vmf->flags &= ~FAULT_FLAG_ORIG_PTE_VALID; 49897267ec00SKirill A. Shutemov } else { 4990f9ce0be7SKirill A. Shutemov /* 49917267ec00SKirill A. Shutemov * A regular pmd is established and it can't morph into a huge 4992c7ad0880SHugh Dickins * pmd by anon khugepaged, since that takes mmap_lock in write 4993c7ad0880SHugh Dickins * mode; but shmem or file collapse to THP could still morph 4994c7ad0880SHugh Dickins * it into a huge pmd: just retry later if so. 49957267ec00SKirill A. Shutemov */ 4996c7ad0880SHugh Dickins vmf->pte = pte_offset_map_nolock(vmf->vma->vm_mm, vmf->pmd, 4997c7ad0880SHugh Dickins vmf->address, &vmf->ptl); 4998c7ad0880SHugh Dickins if (unlikely(!vmf->pte)) 4999c7ad0880SHugh Dickins return 0; 500026e1a0c3SHugh Dickins vmf->orig_pte = ptep_get_lockless(vmf->pte); 5001f46f2adeSPeter Xu vmf->flags |= FAULT_FLAG_ORIG_PTE_VALID; 50027267ec00SKirill A. Shutemov 50032994302bSJan Kara if (pte_none(vmf->orig_pte)) { 500482b0f8c3SJan Kara pte_unmap(vmf->pte); 500582b0f8c3SJan Kara vmf->pte = NULL; 50067267ec00SKirill A. Shutemov } 50077267ec00SKirill A. Shutemov } 50087267ec00SKirill A. Shutemov 50092bad466cSPeter Xu if (!vmf->pte) 50102bad466cSPeter Xu return do_pte_missing(vmf); 50117267ec00SKirill A. Shutemov 50122994302bSJan Kara if (!pte_present(vmf->orig_pte)) 50132994302bSJan Kara return do_swap_page(vmf); 50141da177e4SLinus Torvalds 50152994302bSJan Kara if (pte_protnone(vmf->orig_pte) && vma_is_accessible(vmf->vma)) 50162994302bSJan Kara return do_numa_page(vmf); 5017d10e63f2SMel Gorman 501882b0f8c3SJan Kara spin_lock(vmf->ptl); 50192994302bSJan Kara entry = vmf->orig_pte; 5020c33c7948SRyan Roberts if (unlikely(!pte_same(ptep_get(vmf->pte), entry))) { 50217df67697SBibo Mao update_mmu_tlb(vmf->vma, vmf->address, vmf->pte); 50228f4e2101SHugh Dickins goto unlock; 50237df67697SBibo Mao } 5024c89357e2SDavid Hildenbrand if (vmf->flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) { 5025f6f37321SLinus Torvalds if (!pte_write(entry)) 50262994302bSJan Kara return do_wp_page(vmf); 5027c89357e2SDavid Hildenbrand else if (likely(vmf->flags & FAULT_FLAG_WRITE)) 50281da177e4SLinus Torvalds entry = pte_mkdirty(entry); 50291da177e4SLinus Torvalds } 50301da177e4SLinus Torvalds entry = pte_mkyoung(entry); 503182b0f8c3SJan Kara if (ptep_set_access_flags(vmf->vma, vmf->address, vmf->pte, entry, 503282b0f8c3SJan Kara vmf->flags & FAULT_FLAG_WRITE)) { 50335003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vmf->vma, vmf->address, 50345003a2bdSMatthew Wilcox (Oracle) vmf->pte, 1); 50351a44e149SAndrea Arcangeli } else { 5036b7333b58SYang Shi /* Skip spurious TLB flush for retried page fault */ 5037b7333b58SYang Shi if (vmf->flags & FAULT_FLAG_TRIED) 5038b7333b58SYang Shi goto unlock; 50391a44e149SAndrea Arcangeli /* 50401a44e149SAndrea Arcangeli * This is needed only for protection faults but the arch code 50411a44e149SAndrea Arcangeli * is not yet telling us if this is a protection fault or not. 50421a44e149SAndrea Arcangeli * This still avoids useless tlb flushes for .text page faults 50431a44e149SAndrea Arcangeli * with threads. 50441a44e149SAndrea Arcangeli */ 504582b0f8c3SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) 504699c29133SGerald Schaefer flush_tlb_fix_spurious_fault(vmf->vma, vmf->address, 504799c29133SGerald Schaefer vmf->pte); 50481a44e149SAndrea Arcangeli } 50498f4e2101SHugh Dickins unlock: 505082b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 505183c54070SNick Piggin return 0; 50521da177e4SLinus Torvalds } 50531da177e4SLinus Torvalds 50541da177e4SLinus Torvalds /* 50554ec31152SMatthew Wilcox (Oracle) * On entry, we hold either the VMA lock or the mmap_lock 50564ec31152SMatthew Wilcox (Oracle) * (FAULT_FLAG_VMA_LOCK tells you which). If VM_FAULT_RETRY is set in 50574ec31152SMatthew Wilcox (Oracle) * the result, the mmap_lock is not held on exit. See filemap_fault() 50584ec31152SMatthew Wilcox (Oracle) * and __folio_lock_or_retry(). 50591da177e4SLinus Torvalds */ 50602b740303SSouptick Joarder static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma, 50612b740303SSouptick Joarder unsigned long address, unsigned int flags) 50621da177e4SLinus Torvalds { 506382b0f8c3SJan Kara struct vm_fault vmf = { 5064bae473a4SKirill A. Shutemov .vma = vma, 50651a29d85eSJan Kara .address = address & PAGE_MASK, 5066824ddc60SNadav Amit .real_address = address, 5067bae473a4SKirill A. Shutemov .flags = flags, 50680721ec8bSJan Kara .pgoff = linear_page_index(vma, address), 5069667240e0SJan Kara .gfp_mask = __get_fault_gfp_mask(vma), 5070bae473a4SKirill A. Shutemov }; 5071dcddffd4SKirill A. Shutemov struct mm_struct *mm = vma->vm_mm; 50727da4e2cbSYang Shi unsigned long vm_flags = vma->vm_flags; 50731da177e4SLinus Torvalds pgd_t *pgd; 5074c2febafcSKirill A. Shutemov p4d_t *p4d; 50752b740303SSouptick Joarder vm_fault_t ret; 50761da177e4SLinus Torvalds 50771da177e4SLinus Torvalds pgd = pgd_offset(mm, address); 5078c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, address); 5079c2febafcSKirill A. Shutemov if (!p4d) 5080c2febafcSKirill A. Shutemov return VM_FAULT_OOM; 5081a00cc7d9SMatthew Wilcox 5082c2febafcSKirill A. Shutemov vmf.pud = pud_alloc(mm, p4d, address); 5083a00cc7d9SMatthew Wilcox if (!vmf.pud) 5084c74df32cSHugh Dickins return VM_FAULT_OOM; 5085625110b5SThomas Hellstrom retry_pud: 50867da4e2cbSYang Shi if (pud_none(*vmf.pud) && 5087a7f4e6e4SZach O'Keefe hugepage_vma_check(vma, vm_flags, false, true, true)) { 5088a00cc7d9SMatthew Wilcox ret = create_huge_pud(&vmf); 5089a00cc7d9SMatthew Wilcox if (!(ret & VM_FAULT_FALLBACK)) 5090a00cc7d9SMatthew Wilcox return ret; 5091a00cc7d9SMatthew Wilcox } else { 5092a00cc7d9SMatthew Wilcox pud_t orig_pud = *vmf.pud; 5093a00cc7d9SMatthew Wilcox 5094a00cc7d9SMatthew Wilcox barrier(); 5095a00cc7d9SMatthew Wilcox if (pud_trans_huge(orig_pud) || pud_devmap(orig_pud)) { 5096a00cc7d9SMatthew Wilcox 5097c89357e2SDavid Hildenbrand /* 5098c89357e2SDavid Hildenbrand * TODO once we support anonymous PUDs: NUMA case and 5099c89357e2SDavid Hildenbrand * FAULT_FLAG_UNSHARE handling. 5100c89357e2SDavid Hildenbrand */ 5101c89357e2SDavid Hildenbrand if ((flags & FAULT_FLAG_WRITE) && !pud_write(orig_pud)) { 5102a00cc7d9SMatthew Wilcox ret = wp_huge_pud(&vmf, orig_pud); 5103a00cc7d9SMatthew Wilcox if (!(ret & VM_FAULT_FALLBACK)) 5104a00cc7d9SMatthew Wilcox return ret; 5105a00cc7d9SMatthew Wilcox } else { 5106a00cc7d9SMatthew Wilcox huge_pud_set_accessed(&vmf, orig_pud); 5107a00cc7d9SMatthew Wilcox return 0; 5108a00cc7d9SMatthew Wilcox } 5109a00cc7d9SMatthew Wilcox } 5110a00cc7d9SMatthew Wilcox } 5111a00cc7d9SMatthew Wilcox 5112a00cc7d9SMatthew Wilcox vmf.pmd = pmd_alloc(mm, vmf.pud, address); 511382b0f8c3SJan Kara if (!vmf.pmd) 5114c74df32cSHugh Dickins return VM_FAULT_OOM; 5115625110b5SThomas Hellstrom 5116625110b5SThomas Hellstrom /* Huge pud page fault raced with pmd_alloc? */ 5117625110b5SThomas Hellstrom if (pud_trans_unstable(vmf.pud)) 5118625110b5SThomas Hellstrom goto retry_pud; 5119625110b5SThomas Hellstrom 51207da4e2cbSYang Shi if (pmd_none(*vmf.pmd) && 5121a7f4e6e4SZach O'Keefe hugepage_vma_check(vma, vm_flags, false, true, true)) { 5122a2d58167SDave Jiang ret = create_huge_pmd(&vmf); 5123c0292554SKirill A. Shutemov if (!(ret & VM_FAULT_FALLBACK)) 5124c0292554SKirill A. Shutemov return ret; 512571e3aac0SAndrea Arcangeli } else { 512626e1a0c3SHugh Dickins vmf.orig_pmd = pmdp_get_lockless(vmf.pmd); 51271f1d06c3SDavid Rientjes 51285db4f15cSYang Shi if (unlikely(is_swap_pmd(vmf.orig_pmd))) { 512984c3fc4eSZi Yan VM_BUG_ON(thp_migration_supported() && 51305db4f15cSYang Shi !is_pmd_migration_entry(vmf.orig_pmd)); 51315db4f15cSYang Shi if (is_pmd_migration_entry(vmf.orig_pmd)) 513284c3fc4eSZi Yan pmd_migration_entry_wait(mm, vmf.pmd); 513384c3fc4eSZi Yan return 0; 513484c3fc4eSZi Yan } 51355db4f15cSYang Shi if (pmd_trans_huge(vmf.orig_pmd) || pmd_devmap(vmf.orig_pmd)) { 51365db4f15cSYang Shi if (pmd_protnone(vmf.orig_pmd) && vma_is_accessible(vma)) 51375db4f15cSYang Shi return do_huge_pmd_numa_page(&vmf); 5138d10e63f2SMel Gorman 5139c89357e2SDavid Hildenbrand if ((flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) && 5140c89357e2SDavid Hildenbrand !pmd_write(vmf.orig_pmd)) { 51415db4f15cSYang Shi ret = wp_huge_pmd(&vmf); 51429845cbbdSKirill A. Shutemov if (!(ret & VM_FAULT_FALLBACK)) 51431f1d06c3SDavid Rientjes return ret; 5144a1dd450bSWill Deacon } else { 51455db4f15cSYang Shi huge_pmd_set_accessed(&vmf); 514671e3aac0SAndrea Arcangeli return 0; 514771e3aac0SAndrea Arcangeli } 514871e3aac0SAndrea Arcangeli } 51499845cbbdSKirill A. Shutemov } 515071e3aac0SAndrea Arcangeli 515182b0f8c3SJan Kara return handle_pte_fault(&vmf); 51521da177e4SLinus Torvalds } 51531da177e4SLinus Torvalds 5154bce617edSPeter Xu /** 5155f0953a1bSIngo Molnar * mm_account_fault - Do page fault accounting 5156809ef83cSYang Li * @mm: mm from which memcg should be extracted. It can be NULL. 5157bce617edSPeter Xu * @regs: the pt_regs struct pointer. When set to NULL, will skip accounting 5158bce617edSPeter Xu * of perf event counters, but we'll still do the per-task accounting to 5159bce617edSPeter Xu * the task who triggered this page fault. 5160bce617edSPeter Xu * @address: the faulted address. 5161bce617edSPeter Xu * @flags: the fault flags. 5162bce617edSPeter Xu * @ret: the fault retcode. 5163bce617edSPeter Xu * 5164f0953a1bSIngo Molnar * This will take care of most of the page fault accounting. Meanwhile, it 5165bce617edSPeter Xu * will also include the PERF_COUNT_SW_PAGE_FAULTS_[MAJ|MIN] perf counter 5166f0953a1bSIngo Molnar * updates. However, note that the handling of PERF_COUNT_SW_PAGE_FAULTS should 5167bce617edSPeter Xu * still be in per-arch page fault handlers at the entry of page fault. 5168bce617edSPeter Xu */ 516953156443SSuren Baghdasaryan static inline void mm_account_fault(struct mm_struct *mm, struct pt_regs *regs, 5170bce617edSPeter Xu unsigned long address, unsigned int flags, 5171bce617edSPeter Xu vm_fault_t ret) 5172bce617edSPeter Xu { 5173bce617edSPeter Xu bool major; 5174bce617edSPeter Xu 517553156443SSuren Baghdasaryan /* Incomplete faults will be accounted upon completion. */ 517653156443SSuren Baghdasaryan if (ret & VM_FAULT_RETRY) 517753156443SSuren Baghdasaryan return; 517853156443SSuren Baghdasaryan 5179bce617edSPeter Xu /* 518053156443SSuren Baghdasaryan * To preserve the behavior of older kernels, PGFAULT counters record 518153156443SSuren Baghdasaryan * both successful and failed faults, as opposed to perf counters, 518253156443SSuren Baghdasaryan * which ignore failed cases. 5183bce617edSPeter Xu */ 518453156443SSuren Baghdasaryan count_vm_event(PGFAULT); 518553156443SSuren Baghdasaryan count_memcg_event_mm(mm, PGFAULT); 518653156443SSuren Baghdasaryan 518753156443SSuren Baghdasaryan /* 518853156443SSuren Baghdasaryan * Do not account for unsuccessful faults (e.g. when the address wasn't 518953156443SSuren Baghdasaryan * valid). That includes arch_vma_access_permitted() failing before 519053156443SSuren Baghdasaryan * reaching here. So this is not a "this many hardware page faults" 519153156443SSuren Baghdasaryan * counter. We should use the hw profiling for that. 519253156443SSuren Baghdasaryan */ 519353156443SSuren Baghdasaryan if (ret & VM_FAULT_ERROR) 5194bce617edSPeter Xu return; 5195bce617edSPeter Xu 5196bce617edSPeter Xu /* 5197bce617edSPeter Xu * We define the fault as a major fault when the final successful fault 5198bce617edSPeter Xu * is VM_FAULT_MAJOR, or if it retried (which implies that we couldn't 5199bce617edSPeter Xu * handle it immediately previously). 5200bce617edSPeter Xu */ 5201bce617edSPeter Xu major = (ret & VM_FAULT_MAJOR) || (flags & FAULT_FLAG_TRIED); 5202bce617edSPeter Xu 5203a2beb5f1SPeter Xu if (major) 5204a2beb5f1SPeter Xu current->maj_flt++; 5205a2beb5f1SPeter Xu else 5206a2beb5f1SPeter Xu current->min_flt++; 5207a2beb5f1SPeter Xu 5208bce617edSPeter Xu /* 5209a2beb5f1SPeter Xu * If the fault is done for GUP, regs will be NULL. We only do the 5210a2beb5f1SPeter Xu * accounting for the per thread fault counters who triggered the 5211a2beb5f1SPeter Xu * fault, and we skip the perf event updates. 5212bce617edSPeter Xu */ 5213bce617edSPeter Xu if (!regs) 5214bce617edSPeter Xu return; 5215bce617edSPeter Xu 5216a2beb5f1SPeter Xu if (major) 5217bce617edSPeter Xu perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address); 5218a2beb5f1SPeter Xu else 5219bce617edSPeter Xu perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address); 5220bce617edSPeter Xu } 5221bce617edSPeter Xu 5222ec1c86b2SYu Zhao #ifdef CONFIG_LRU_GEN 5223ec1c86b2SYu Zhao static void lru_gen_enter_fault(struct vm_area_struct *vma) 5224ec1c86b2SYu Zhao { 52258788f678SYu Zhao /* the LRU algorithm only applies to accesses with recency */ 52268788f678SYu Zhao current->in_lru_fault = vma_has_recency(vma); 5227ec1c86b2SYu Zhao } 5228ec1c86b2SYu Zhao 5229ec1c86b2SYu Zhao static void lru_gen_exit_fault(void) 5230ec1c86b2SYu Zhao { 5231ec1c86b2SYu Zhao current->in_lru_fault = false; 5232ec1c86b2SYu Zhao } 5233ec1c86b2SYu Zhao #else 5234ec1c86b2SYu Zhao static void lru_gen_enter_fault(struct vm_area_struct *vma) 5235ec1c86b2SYu Zhao { 5236ec1c86b2SYu Zhao } 5237ec1c86b2SYu Zhao 5238ec1c86b2SYu Zhao static void lru_gen_exit_fault(void) 5239ec1c86b2SYu Zhao { 5240ec1c86b2SYu Zhao } 5241ec1c86b2SYu Zhao #endif /* CONFIG_LRU_GEN */ 5242ec1c86b2SYu Zhao 5243cdc5021cSDavid Hildenbrand static vm_fault_t sanitize_fault_flags(struct vm_area_struct *vma, 5244cdc5021cSDavid Hildenbrand unsigned int *flags) 5245cdc5021cSDavid Hildenbrand { 5246cdc5021cSDavid Hildenbrand if (unlikely(*flags & FAULT_FLAG_UNSHARE)) { 5247cdc5021cSDavid Hildenbrand if (WARN_ON_ONCE(*flags & FAULT_FLAG_WRITE)) 5248cdc5021cSDavid Hildenbrand return VM_FAULT_SIGSEGV; 5249cdc5021cSDavid Hildenbrand /* 5250cdc5021cSDavid Hildenbrand * FAULT_FLAG_UNSHARE only applies to COW mappings. Let's 5251cdc5021cSDavid Hildenbrand * just treat it like an ordinary read-fault otherwise. 5252cdc5021cSDavid Hildenbrand */ 5253cdc5021cSDavid Hildenbrand if (!is_cow_mapping(vma->vm_flags)) 5254cdc5021cSDavid Hildenbrand *flags &= ~FAULT_FLAG_UNSHARE; 525579881fedSDavid Hildenbrand } else if (*flags & FAULT_FLAG_WRITE) { 525679881fedSDavid Hildenbrand /* Write faults on read-only mappings are impossible ... */ 525779881fedSDavid Hildenbrand if (WARN_ON_ONCE(!(vma->vm_flags & VM_MAYWRITE))) 525879881fedSDavid Hildenbrand return VM_FAULT_SIGSEGV; 525979881fedSDavid Hildenbrand /* ... and FOLL_FORCE only applies to COW mappings. */ 526079881fedSDavid Hildenbrand if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE) && 526179881fedSDavid Hildenbrand !is_cow_mapping(vma->vm_flags))) 526279881fedSDavid Hildenbrand return VM_FAULT_SIGSEGV; 5263cdc5021cSDavid Hildenbrand } 52644089eef0SSuren Baghdasaryan #ifdef CONFIG_PER_VMA_LOCK 52654089eef0SSuren Baghdasaryan /* 52664089eef0SSuren Baghdasaryan * Per-VMA locks can't be used with FAULT_FLAG_RETRY_NOWAIT because of 52674089eef0SSuren Baghdasaryan * the assumption that lock is dropped on VM_FAULT_RETRY. 52684089eef0SSuren Baghdasaryan */ 52694089eef0SSuren Baghdasaryan if (WARN_ON_ONCE((*flags & 52704089eef0SSuren Baghdasaryan (FAULT_FLAG_VMA_LOCK | FAULT_FLAG_RETRY_NOWAIT)) == 52714089eef0SSuren Baghdasaryan (FAULT_FLAG_VMA_LOCK | FAULT_FLAG_RETRY_NOWAIT))) 52724089eef0SSuren Baghdasaryan return VM_FAULT_SIGSEGV; 52734089eef0SSuren Baghdasaryan #endif 52744089eef0SSuren Baghdasaryan 5275cdc5021cSDavid Hildenbrand return 0; 5276cdc5021cSDavid Hildenbrand } 5277cdc5021cSDavid Hildenbrand 52789a95f3cfSPaul Cassella /* 52799a95f3cfSPaul Cassella * By the time we get here, we already hold the mm semaphore 52809a95f3cfSPaul Cassella * 5281c1e8d7c6SMichel Lespinasse * The mmap_lock may have been released depending on flags and our 52829138e47eSMatthew Wilcox (Oracle) * return value. See filemap_fault() and __folio_lock_or_retry(). 52839a95f3cfSPaul Cassella */ 52842b740303SSouptick Joarder vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address, 5285bce617edSPeter Xu unsigned int flags, struct pt_regs *regs) 5286519e5247SJohannes Weiner { 528753156443SSuren Baghdasaryan /* If the fault handler drops the mmap_lock, vma may be freed */ 528853156443SSuren Baghdasaryan struct mm_struct *mm = vma->vm_mm; 52892b740303SSouptick Joarder vm_fault_t ret; 5290519e5247SJohannes Weiner 5291519e5247SJohannes Weiner __set_current_state(TASK_RUNNING); 5292519e5247SJohannes Weiner 5293cdc5021cSDavid Hildenbrand ret = sanitize_fault_flags(vma, &flags); 5294cdc5021cSDavid Hildenbrand if (ret) 529553156443SSuren Baghdasaryan goto out; 5296cdc5021cSDavid Hildenbrand 5297de0c799bSLaurent Dufour if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE, 5298de0c799bSLaurent Dufour flags & FAULT_FLAG_INSTRUCTION, 529953156443SSuren Baghdasaryan flags & FAULT_FLAG_REMOTE)) { 530053156443SSuren Baghdasaryan ret = VM_FAULT_SIGSEGV; 530153156443SSuren Baghdasaryan goto out; 530253156443SSuren Baghdasaryan } 5303de0c799bSLaurent Dufour 5304519e5247SJohannes Weiner /* 5305519e5247SJohannes Weiner * Enable the memcg OOM handling for faults triggered in user 5306519e5247SJohannes Weiner * space. Kernel faults are handled more gracefully. 5307519e5247SJohannes Weiner */ 5308519e5247SJohannes Weiner if (flags & FAULT_FLAG_USER) 530929ef680aSMichal Hocko mem_cgroup_enter_user_fault(); 5310519e5247SJohannes Weiner 5311ec1c86b2SYu Zhao lru_gen_enter_fault(vma); 5312ec1c86b2SYu Zhao 5313bae473a4SKirill A. Shutemov if (unlikely(is_vm_hugetlb_page(vma))) 5314bae473a4SKirill A. Shutemov ret = hugetlb_fault(vma->vm_mm, vma, address, flags); 5315bae473a4SKirill A. Shutemov else 5316dcddffd4SKirill A. Shutemov ret = __handle_mm_fault(vma, address, flags); 5317519e5247SJohannes Weiner 5318ec1c86b2SYu Zhao lru_gen_exit_fault(); 5319ec1c86b2SYu Zhao 532049426420SJohannes Weiner if (flags & FAULT_FLAG_USER) { 532129ef680aSMichal Hocko mem_cgroup_exit_user_fault(); 532249426420SJohannes Weiner /* 532349426420SJohannes Weiner * The task may have entered a memcg OOM situation but 532449426420SJohannes Weiner * if the allocation error was handled gracefully (no 532549426420SJohannes Weiner * VM_FAULT_OOM), there is no need to kill anything. 532649426420SJohannes Weiner * Just clean up the OOM state peacefully. 532749426420SJohannes Weiner */ 532849426420SJohannes Weiner if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM)) 532949426420SJohannes Weiner mem_cgroup_oom_synchronize(false); 533049426420SJohannes Weiner } 533153156443SSuren Baghdasaryan out: 533253156443SSuren Baghdasaryan mm_account_fault(mm, regs, address, flags, ret); 5333bce617edSPeter Xu 5334519e5247SJohannes Weiner return ret; 5335519e5247SJohannes Weiner } 5336e1d6d01aSJesse Barnes EXPORT_SYMBOL_GPL(handle_mm_fault); 5337519e5247SJohannes Weiner 5338c2508ec5SLinus Torvalds #ifdef CONFIG_LOCK_MM_AND_FIND_VMA 5339c2508ec5SLinus Torvalds #include <linux/extable.h> 5340c2508ec5SLinus Torvalds 5341c2508ec5SLinus Torvalds static inline bool get_mmap_lock_carefully(struct mm_struct *mm, struct pt_regs *regs) 5342c2508ec5SLinus Torvalds { 53434542057eSLinus Torvalds if (likely(mmap_read_trylock(mm))) 5344c2508ec5SLinus Torvalds return true; 5345c2508ec5SLinus Torvalds 5346c2508ec5SLinus Torvalds if (regs && !user_mode(regs)) { 5347c2508ec5SLinus Torvalds unsigned long ip = instruction_pointer(regs); 5348c2508ec5SLinus Torvalds if (!search_exception_tables(ip)) 5349c2508ec5SLinus Torvalds return false; 5350c2508ec5SLinus Torvalds } 5351c2508ec5SLinus Torvalds 5352eda00472SLinus Torvalds return !mmap_read_lock_killable(mm); 5353c2508ec5SLinus Torvalds } 5354c2508ec5SLinus Torvalds 5355c2508ec5SLinus Torvalds static inline bool mmap_upgrade_trylock(struct mm_struct *mm) 5356c2508ec5SLinus Torvalds { 5357c2508ec5SLinus Torvalds /* 5358c2508ec5SLinus Torvalds * We don't have this operation yet. 5359c2508ec5SLinus Torvalds * 5360c2508ec5SLinus Torvalds * It should be easy enough to do: it's basically a 5361c2508ec5SLinus Torvalds * atomic_long_try_cmpxchg_acquire() 5362c2508ec5SLinus Torvalds * from RWSEM_READER_BIAS -> RWSEM_WRITER_LOCKED, but 5363c2508ec5SLinus Torvalds * it also needs the proper lockdep magic etc. 5364c2508ec5SLinus Torvalds */ 5365c2508ec5SLinus Torvalds return false; 5366c2508ec5SLinus Torvalds } 5367c2508ec5SLinus Torvalds 5368c2508ec5SLinus Torvalds static inline bool upgrade_mmap_lock_carefully(struct mm_struct *mm, struct pt_regs *regs) 5369c2508ec5SLinus Torvalds { 5370c2508ec5SLinus Torvalds mmap_read_unlock(mm); 5371c2508ec5SLinus Torvalds if (regs && !user_mode(regs)) { 5372c2508ec5SLinus Torvalds unsigned long ip = instruction_pointer(regs); 5373c2508ec5SLinus Torvalds if (!search_exception_tables(ip)) 5374c2508ec5SLinus Torvalds return false; 5375c2508ec5SLinus Torvalds } 5376eda00472SLinus Torvalds return !mmap_write_lock_killable(mm); 5377c2508ec5SLinus Torvalds } 5378c2508ec5SLinus Torvalds 5379c2508ec5SLinus Torvalds /* 5380c2508ec5SLinus Torvalds * Helper for page fault handling. 5381c2508ec5SLinus Torvalds * 5382c2508ec5SLinus Torvalds * This is kind of equivalend to "mmap_read_lock()" followed 5383c2508ec5SLinus Torvalds * by "find_extend_vma()", except it's a lot more careful about 5384c2508ec5SLinus Torvalds * the locking (and will drop the lock on failure). 5385c2508ec5SLinus Torvalds * 5386c2508ec5SLinus Torvalds * For example, if we have a kernel bug that causes a page 5387c2508ec5SLinus Torvalds * fault, we don't want to just use mmap_read_lock() to get 5388c2508ec5SLinus Torvalds * the mm lock, because that would deadlock if the bug were 5389c2508ec5SLinus Torvalds * to happen while we're holding the mm lock for writing. 5390c2508ec5SLinus Torvalds * 5391c2508ec5SLinus Torvalds * So this checks the exception tables on kernel faults in 5392c2508ec5SLinus Torvalds * order to only do this all for instructions that are actually 5393c2508ec5SLinus Torvalds * expected to fault. 5394c2508ec5SLinus Torvalds * 5395c2508ec5SLinus Torvalds * We can also actually take the mm lock for writing if we 5396c2508ec5SLinus Torvalds * need to extend the vma, which helps the VM layer a lot. 5397c2508ec5SLinus Torvalds */ 5398c2508ec5SLinus Torvalds struct vm_area_struct *lock_mm_and_find_vma(struct mm_struct *mm, 5399c2508ec5SLinus Torvalds unsigned long addr, struct pt_regs *regs) 5400c2508ec5SLinus Torvalds { 5401c2508ec5SLinus Torvalds struct vm_area_struct *vma; 5402c2508ec5SLinus Torvalds 5403c2508ec5SLinus Torvalds if (!get_mmap_lock_carefully(mm, regs)) 5404c2508ec5SLinus Torvalds return NULL; 5405c2508ec5SLinus Torvalds 5406c2508ec5SLinus Torvalds vma = find_vma(mm, addr); 5407c2508ec5SLinus Torvalds if (likely(vma && (vma->vm_start <= addr))) 5408c2508ec5SLinus Torvalds return vma; 5409c2508ec5SLinus Torvalds 5410c2508ec5SLinus Torvalds /* 5411c2508ec5SLinus Torvalds * Well, dang. We might still be successful, but only 5412c2508ec5SLinus Torvalds * if we can extend a vma to do so. 5413c2508ec5SLinus Torvalds */ 5414c2508ec5SLinus Torvalds if (!vma || !(vma->vm_flags & VM_GROWSDOWN)) { 5415c2508ec5SLinus Torvalds mmap_read_unlock(mm); 5416c2508ec5SLinus Torvalds return NULL; 5417c2508ec5SLinus Torvalds } 5418c2508ec5SLinus Torvalds 5419c2508ec5SLinus Torvalds /* 5420c2508ec5SLinus Torvalds * We can try to upgrade the mmap lock atomically, 5421c2508ec5SLinus Torvalds * in which case we can continue to use the vma 5422c2508ec5SLinus Torvalds * we already looked up. 5423c2508ec5SLinus Torvalds * 5424c2508ec5SLinus Torvalds * Otherwise we'll have to drop the mmap lock and 5425c2508ec5SLinus Torvalds * re-take it, and also look up the vma again, 5426c2508ec5SLinus Torvalds * re-checking it. 5427c2508ec5SLinus Torvalds */ 5428c2508ec5SLinus Torvalds if (!mmap_upgrade_trylock(mm)) { 5429c2508ec5SLinus Torvalds if (!upgrade_mmap_lock_carefully(mm, regs)) 5430c2508ec5SLinus Torvalds return NULL; 5431c2508ec5SLinus Torvalds 5432c2508ec5SLinus Torvalds vma = find_vma(mm, addr); 5433c2508ec5SLinus Torvalds if (!vma) 5434c2508ec5SLinus Torvalds goto fail; 5435c2508ec5SLinus Torvalds if (vma->vm_start <= addr) 5436c2508ec5SLinus Torvalds goto success; 5437c2508ec5SLinus Torvalds if (!(vma->vm_flags & VM_GROWSDOWN)) 5438c2508ec5SLinus Torvalds goto fail; 5439c2508ec5SLinus Torvalds } 5440c2508ec5SLinus Torvalds 54418d7071afSLinus Torvalds if (expand_stack_locked(vma, addr)) 5442c2508ec5SLinus Torvalds goto fail; 5443c2508ec5SLinus Torvalds 5444c2508ec5SLinus Torvalds success: 5445c2508ec5SLinus Torvalds mmap_write_downgrade(mm); 5446c2508ec5SLinus Torvalds return vma; 5447c2508ec5SLinus Torvalds 5448c2508ec5SLinus Torvalds fail: 5449c2508ec5SLinus Torvalds mmap_write_unlock(mm); 5450c2508ec5SLinus Torvalds return NULL; 5451c2508ec5SLinus Torvalds } 5452c2508ec5SLinus Torvalds #endif 5453c2508ec5SLinus Torvalds 545450ee3253SSuren Baghdasaryan #ifdef CONFIG_PER_VMA_LOCK 545550ee3253SSuren Baghdasaryan /* 545650ee3253SSuren Baghdasaryan * Lookup and lock a VMA under RCU protection. Returned VMA is guaranteed to be 545750ee3253SSuren Baghdasaryan * stable and not isolated. If the VMA is not found or is being modified the 545850ee3253SSuren Baghdasaryan * function returns NULL. 545950ee3253SSuren Baghdasaryan */ 546050ee3253SSuren Baghdasaryan struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm, 546150ee3253SSuren Baghdasaryan unsigned long address) 546250ee3253SSuren Baghdasaryan { 546350ee3253SSuren Baghdasaryan MA_STATE(mas, &mm->mm_mt, address, address); 546450ee3253SSuren Baghdasaryan struct vm_area_struct *vma; 546550ee3253SSuren Baghdasaryan 546650ee3253SSuren Baghdasaryan rcu_read_lock(); 546750ee3253SSuren Baghdasaryan retry: 546850ee3253SSuren Baghdasaryan vma = mas_walk(&mas); 546950ee3253SSuren Baghdasaryan if (!vma) 547050ee3253SSuren Baghdasaryan goto inval; 547150ee3253SSuren Baghdasaryan 547250ee3253SSuren Baghdasaryan if (!vma_start_read(vma)) 547350ee3253SSuren Baghdasaryan goto inval; 547450ee3253SSuren Baghdasaryan 5475444eeb17SSuren Baghdasaryan /* 5476657b5146SJann Horn * find_mergeable_anon_vma uses adjacent vmas which are not locked. 5477657b5146SJann Horn * This check must happen after vma_start_read(); otherwise, a 5478657b5146SJann Horn * concurrent mremap() with MREMAP_DONTUNMAP could dissociate the VMA 5479657b5146SJann Horn * from its anon_vma. 5480657b5146SJann Horn */ 548129a22b9eSSuren Baghdasaryan if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma)) 5482657b5146SJann Horn goto inval_end_read; 5483444eeb17SSuren Baghdasaryan 548450ee3253SSuren Baghdasaryan /* Check since vm_start/vm_end might change before we lock the VMA */ 5485657b5146SJann Horn if (unlikely(address < vma->vm_start || address >= vma->vm_end)) 5486657b5146SJann Horn goto inval_end_read; 548750ee3253SSuren Baghdasaryan 548850ee3253SSuren Baghdasaryan /* Check if the VMA got isolated after we found it */ 548950ee3253SSuren Baghdasaryan if (vma->detached) { 549050ee3253SSuren Baghdasaryan vma_end_read(vma); 549152f23865SSuren Baghdasaryan count_vm_vma_lock_event(VMA_LOCK_MISS); 549250ee3253SSuren Baghdasaryan /* The area was replaced with another one */ 549350ee3253SSuren Baghdasaryan goto retry; 549450ee3253SSuren Baghdasaryan } 549550ee3253SSuren Baghdasaryan 549650ee3253SSuren Baghdasaryan rcu_read_unlock(); 549750ee3253SSuren Baghdasaryan return vma; 5498657b5146SJann Horn 5499657b5146SJann Horn inval_end_read: 5500657b5146SJann Horn vma_end_read(vma); 550150ee3253SSuren Baghdasaryan inval: 550250ee3253SSuren Baghdasaryan rcu_read_unlock(); 550352f23865SSuren Baghdasaryan count_vm_vma_lock_event(VMA_LOCK_ABORT); 550450ee3253SSuren Baghdasaryan return NULL; 550550ee3253SSuren Baghdasaryan } 550650ee3253SSuren Baghdasaryan #endif /* CONFIG_PER_VMA_LOCK */ 550750ee3253SSuren Baghdasaryan 550890eceff1SKirill A. Shutemov #ifndef __PAGETABLE_P4D_FOLDED 550990eceff1SKirill A. Shutemov /* 551090eceff1SKirill A. Shutemov * Allocate p4d page table. 551190eceff1SKirill A. Shutemov * We've already handled the fast-path in-line. 551290eceff1SKirill A. Shutemov */ 551390eceff1SKirill A. Shutemov int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address) 551490eceff1SKirill A. Shutemov { 551590eceff1SKirill A. Shutemov p4d_t *new = p4d_alloc_one(mm, address); 551690eceff1SKirill A. Shutemov if (!new) 551790eceff1SKirill A. Shutemov return -ENOMEM; 551890eceff1SKirill A. Shutemov 551990eceff1SKirill A. Shutemov spin_lock(&mm->page_table_lock); 5520ed33b5a6SQi Zheng if (pgd_present(*pgd)) { /* Another has populated it */ 552190eceff1SKirill A. Shutemov p4d_free(mm, new); 5522ed33b5a6SQi Zheng } else { 5523ed33b5a6SQi Zheng smp_wmb(); /* See comment in pmd_install() */ 552490eceff1SKirill A. Shutemov pgd_populate(mm, pgd, new); 5525ed33b5a6SQi Zheng } 552690eceff1SKirill A. Shutemov spin_unlock(&mm->page_table_lock); 552790eceff1SKirill A. Shutemov return 0; 552890eceff1SKirill A. Shutemov } 552990eceff1SKirill A. Shutemov #endif /* __PAGETABLE_P4D_FOLDED */ 553090eceff1SKirill A. Shutemov 55311da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED 55321da177e4SLinus Torvalds /* 55331da177e4SLinus Torvalds * Allocate page upper directory. 5534872fec16SHugh Dickins * We've already handled the fast-path in-line. 55351da177e4SLinus Torvalds */ 5536c2febafcSKirill A. Shutemov int __pud_alloc(struct mm_struct *mm, p4d_t *p4d, unsigned long address) 55371da177e4SLinus Torvalds { 5538c74df32cSHugh Dickins pud_t *new = pud_alloc_one(mm, address); 5539c74df32cSHugh Dickins if (!new) 55401bb3630eSHugh Dickins return -ENOMEM; 55411da177e4SLinus Torvalds 5542872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 5543b4e98d9aSKirill A. Shutemov if (!p4d_present(*p4d)) { 5544b4e98d9aSKirill A. Shutemov mm_inc_nr_puds(mm); 5545ed33b5a6SQi Zheng smp_wmb(); /* See comment in pmd_install() */ 5546c2febafcSKirill A. Shutemov p4d_populate(mm, p4d, new); 5547b4e98d9aSKirill A. Shutemov } else /* Another has populated it */ 5548c2febafcSKirill A. Shutemov pud_free(mm, new); 5549872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 55501bb3630eSHugh Dickins return 0; 55511da177e4SLinus Torvalds } 55521da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */ 55531da177e4SLinus Torvalds 55541da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED 55551da177e4SLinus Torvalds /* 55561da177e4SLinus Torvalds * Allocate page middle directory. 5557872fec16SHugh Dickins * We've already handled the fast-path in-line. 55581da177e4SLinus Torvalds */ 55591bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) 55601da177e4SLinus Torvalds { 5561a00cc7d9SMatthew Wilcox spinlock_t *ptl; 5562c74df32cSHugh Dickins pmd_t *new = pmd_alloc_one(mm, address); 5563c74df32cSHugh Dickins if (!new) 55641bb3630eSHugh Dickins return -ENOMEM; 55651da177e4SLinus Torvalds 5566a00cc7d9SMatthew Wilcox ptl = pud_lock(mm, pud); 5567dc6c9a35SKirill A. Shutemov if (!pud_present(*pud)) { 5568dc6c9a35SKirill A. Shutemov mm_inc_nr_pmds(mm); 5569ed33b5a6SQi Zheng smp_wmb(); /* See comment in pmd_install() */ 55701da177e4SLinus Torvalds pud_populate(mm, pud, new); 5571ed33b5a6SQi Zheng } else { /* Another has populated it */ 55725e541973SBenjamin Herrenschmidt pmd_free(mm, new); 5573ed33b5a6SQi Zheng } 5574a00cc7d9SMatthew Wilcox spin_unlock(ptl); 55751bb3630eSHugh Dickins return 0; 55761da177e4SLinus Torvalds } 55771da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */ 55781da177e4SLinus Torvalds 55793b6748e2SJohannes Weiner /** 55809fd6dad1SPaolo Bonzini * follow_pte - look up PTE at a user virtual address 55819fd6dad1SPaolo Bonzini * @mm: the mm_struct of the target address space 55829fd6dad1SPaolo Bonzini * @address: user virtual address 55839fd6dad1SPaolo Bonzini * @ptepp: location to store found PTE 55849fd6dad1SPaolo Bonzini * @ptlp: location to store the lock for the PTE 55859fd6dad1SPaolo Bonzini * 55869fd6dad1SPaolo Bonzini * On a successful return, the pointer to the PTE is stored in @ptepp; 55879fd6dad1SPaolo Bonzini * the corresponding lock is taken and its location is stored in @ptlp. 55889fd6dad1SPaolo Bonzini * The contents of the PTE are only stable until @ptlp is released; 55899fd6dad1SPaolo Bonzini * any further use, if any, must be protected against invalidation 55909fd6dad1SPaolo Bonzini * with MMU notifiers. 55919fd6dad1SPaolo Bonzini * 55929fd6dad1SPaolo Bonzini * Only IO mappings and raw PFN mappings are allowed. The mmap semaphore 55939fd6dad1SPaolo Bonzini * should be taken for read. 55949fd6dad1SPaolo Bonzini * 55959fd6dad1SPaolo Bonzini * KVM uses this function. While it is arguably less bad than ``follow_pfn``, 55969fd6dad1SPaolo Bonzini * it is not a good general-purpose API. 55979fd6dad1SPaolo Bonzini * 55989fd6dad1SPaolo Bonzini * Return: zero on success, -ve otherwise. 55999fd6dad1SPaolo Bonzini */ 56009fd6dad1SPaolo Bonzini int follow_pte(struct mm_struct *mm, unsigned long address, 56019fd6dad1SPaolo Bonzini pte_t **ptepp, spinlock_t **ptlp) 56029fd6dad1SPaolo Bonzini { 56030e5e64c0SMuchun Song pgd_t *pgd; 56040e5e64c0SMuchun Song p4d_t *p4d; 56050e5e64c0SMuchun Song pud_t *pud; 56060e5e64c0SMuchun Song pmd_t *pmd; 56070e5e64c0SMuchun Song pte_t *ptep; 56080e5e64c0SMuchun Song 56090e5e64c0SMuchun Song pgd = pgd_offset(mm, address); 56100e5e64c0SMuchun Song if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) 56110e5e64c0SMuchun Song goto out; 56120e5e64c0SMuchun Song 56130e5e64c0SMuchun Song p4d = p4d_offset(pgd, address); 56140e5e64c0SMuchun Song if (p4d_none(*p4d) || unlikely(p4d_bad(*p4d))) 56150e5e64c0SMuchun Song goto out; 56160e5e64c0SMuchun Song 56170e5e64c0SMuchun Song pud = pud_offset(p4d, address); 56180e5e64c0SMuchun Song if (pud_none(*pud) || unlikely(pud_bad(*pud))) 56190e5e64c0SMuchun Song goto out; 56200e5e64c0SMuchun Song 56210e5e64c0SMuchun Song pmd = pmd_offset(pud, address); 56220e5e64c0SMuchun Song VM_BUG_ON(pmd_trans_huge(*pmd)); 56230e5e64c0SMuchun Song 56240e5e64c0SMuchun Song ptep = pte_offset_map_lock(mm, pmd, address, ptlp); 56253db82b93SHugh Dickins if (!ptep) 56263db82b93SHugh Dickins goto out; 5627c33c7948SRyan Roberts if (!pte_present(ptep_get(ptep))) 56280e5e64c0SMuchun Song goto unlock; 56290e5e64c0SMuchun Song *ptepp = ptep; 56300e5e64c0SMuchun Song return 0; 56310e5e64c0SMuchun Song unlock: 56320e5e64c0SMuchun Song pte_unmap_unlock(ptep, *ptlp); 56330e5e64c0SMuchun Song out: 56340e5e64c0SMuchun Song return -EINVAL; 56359fd6dad1SPaolo Bonzini } 56369fd6dad1SPaolo Bonzini EXPORT_SYMBOL_GPL(follow_pte); 56379fd6dad1SPaolo Bonzini 56389fd6dad1SPaolo Bonzini /** 56393b6748e2SJohannes Weiner * follow_pfn - look up PFN at a user virtual address 56403b6748e2SJohannes Weiner * @vma: memory mapping 56413b6748e2SJohannes Weiner * @address: user virtual address 56423b6748e2SJohannes Weiner * @pfn: location to store found PFN 56433b6748e2SJohannes Weiner * 56443b6748e2SJohannes Weiner * Only IO mappings and raw PFN mappings are allowed. 56453b6748e2SJohannes Weiner * 56469fd6dad1SPaolo Bonzini * This function does not allow the caller to read the permissions 56479fd6dad1SPaolo Bonzini * of the PTE. Do not use it. 56489fd6dad1SPaolo Bonzini * 5649a862f68aSMike Rapoport * Return: zero and the pfn at @pfn on success, -ve otherwise. 56503b6748e2SJohannes Weiner */ 56513b6748e2SJohannes Weiner int follow_pfn(struct vm_area_struct *vma, unsigned long address, 56523b6748e2SJohannes Weiner unsigned long *pfn) 56533b6748e2SJohannes Weiner { 56543b6748e2SJohannes Weiner int ret = -EINVAL; 56553b6748e2SJohannes Weiner spinlock_t *ptl; 56563b6748e2SJohannes Weiner pte_t *ptep; 56573b6748e2SJohannes Weiner 56583b6748e2SJohannes Weiner if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 56593b6748e2SJohannes Weiner return ret; 56603b6748e2SJohannes Weiner 56619fd6dad1SPaolo Bonzini ret = follow_pte(vma->vm_mm, address, &ptep, &ptl); 56623b6748e2SJohannes Weiner if (ret) 56633b6748e2SJohannes Weiner return ret; 5664c33c7948SRyan Roberts *pfn = pte_pfn(ptep_get(ptep)); 56653b6748e2SJohannes Weiner pte_unmap_unlock(ptep, ptl); 56663b6748e2SJohannes Weiner return 0; 56673b6748e2SJohannes Weiner } 56683b6748e2SJohannes Weiner EXPORT_SYMBOL(follow_pfn); 56693b6748e2SJohannes Weiner 567028b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT 5671d87fe660Svenkatesh.pallipadi@intel.com int follow_phys(struct vm_area_struct *vma, 567228b2ee20SRik van Riel unsigned long address, unsigned int flags, 5673d87fe660Svenkatesh.pallipadi@intel.com unsigned long *prot, resource_size_t *phys) 567428b2ee20SRik van Riel { 567503668a4dSJohannes Weiner int ret = -EINVAL; 567628b2ee20SRik van Riel pte_t *ptep, pte; 567728b2ee20SRik van Riel spinlock_t *ptl; 567828b2ee20SRik van Riel 5679d87fe660Svenkatesh.pallipadi@intel.com if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 5680d87fe660Svenkatesh.pallipadi@intel.com goto out; 568128b2ee20SRik van Riel 56829fd6dad1SPaolo Bonzini if (follow_pte(vma->vm_mm, address, &ptep, &ptl)) 5683d87fe660Svenkatesh.pallipadi@intel.com goto out; 5684c33c7948SRyan Roberts pte = ptep_get(ptep); 568503668a4dSJohannes Weiner 5686f6f37321SLinus Torvalds if ((flags & FOLL_WRITE) && !pte_write(pte)) 568728b2ee20SRik van Riel goto unlock; 568828b2ee20SRik van Riel 568928b2ee20SRik van Riel *prot = pgprot_val(pte_pgprot(pte)); 569003668a4dSJohannes Weiner *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT; 569128b2ee20SRik van Riel 569203668a4dSJohannes Weiner ret = 0; 569328b2ee20SRik van Riel unlock: 569428b2ee20SRik van Riel pte_unmap_unlock(ptep, ptl); 569528b2ee20SRik van Riel out: 5696d87fe660Svenkatesh.pallipadi@intel.com return ret; 569728b2ee20SRik van Riel } 569828b2ee20SRik van Riel 569996667f8aSDaniel Vetter /** 570096667f8aSDaniel Vetter * generic_access_phys - generic implementation for iomem mmap access 570196667f8aSDaniel Vetter * @vma: the vma to access 5702f0953a1bSIngo Molnar * @addr: userspace address, not relative offset within @vma 570396667f8aSDaniel Vetter * @buf: buffer to read/write 570496667f8aSDaniel Vetter * @len: length of transfer 570596667f8aSDaniel Vetter * @write: set to FOLL_WRITE when writing, otherwise reading 570696667f8aSDaniel Vetter * 570796667f8aSDaniel Vetter * This is a generic implementation for &vm_operations_struct.access for an 570896667f8aSDaniel Vetter * iomem mapping. This callback is used by access_process_vm() when the @vma is 570996667f8aSDaniel Vetter * not page based. 571096667f8aSDaniel Vetter */ 571128b2ee20SRik van Riel int generic_access_phys(struct vm_area_struct *vma, unsigned long addr, 571228b2ee20SRik van Riel void *buf, int len, int write) 571328b2ee20SRik van Riel { 571428b2ee20SRik van Riel resource_size_t phys_addr; 571528b2ee20SRik van Riel unsigned long prot = 0; 57162bc7273bSKOSAKI Motohiro void __iomem *maddr; 571796667f8aSDaniel Vetter pte_t *ptep, pte; 571896667f8aSDaniel Vetter spinlock_t *ptl; 571996667f8aSDaniel Vetter int offset = offset_in_page(addr); 572096667f8aSDaniel Vetter int ret = -EINVAL; 572128b2ee20SRik van Riel 572296667f8aSDaniel Vetter if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 572396667f8aSDaniel Vetter return -EINVAL; 572496667f8aSDaniel Vetter 572596667f8aSDaniel Vetter retry: 5726e913a8cdSLinus Torvalds if (follow_pte(vma->vm_mm, addr, &ptep, &ptl)) 572796667f8aSDaniel Vetter return -EINVAL; 5728c33c7948SRyan Roberts pte = ptep_get(ptep); 572996667f8aSDaniel Vetter pte_unmap_unlock(ptep, ptl); 573096667f8aSDaniel Vetter 573196667f8aSDaniel Vetter prot = pgprot_val(pte_pgprot(pte)); 573296667f8aSDaniel Vetter phys_addr = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT; 573396667f8aSDaniel Vetter 573496667f8aSDaniel Vetter if ((write & FOLL_WRITE) && !pte_write(pte)) 573528b2ee20SRik van Riel return -EINVAL; 573628b2ee20SRik van Riel 57379cb12d7bSGrazvydas Ignotas maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot); 573824eee1e4Sjie@chenjie6@huwei.com if (!maddr) 573924eee1e4Sjie@chenjie6@huwei.com return -ENOMEM; 574024eee1e4Sjie@chenjie6@huwei.com 5741e913a8cdSLinus Torvalds if (follow_pte(vma->vm_mm, addr, &ptep, &ptl)) 574296667f8aSDaniel Vetter goto out_unmap; 574396667f8aSDaniel Vetter 5744c33c7948SRyan Roberts if (!pte_same(pte, ptep_get(ptep))) { 574596667f8aSDaniel Vetter pte_unmap_unlock(ptep, ptl); 574696667f8aSDaniel Vetter iounmap(maddr); 574796667f8aSDaniel Vetter 574896667f8aSDaniel Vetter goto retry; 574996667f8aSDaniel Vetter } 575096667f8aSDaniel Vetter 575128b2ee20SRik van Riel if (write) 575228b2ee20SRik van Riel memcpy_toio(maddr + offset, buf, len); 575328b2ee20SRik van Riel else 575428b2ee20SRik van Riel memcpy_fromio(buf, maddr + offset, len); 575596667f8aSDaniel Vetter ret = len; 575696667f8aSDaniel Vetter pte_unmap_unlock(ptep, ptl); 575796667f8aSDaniel Vetter out_unmap: 575828b2ee20SRik van Riel iounmap(maddr); 575928b2ee20SRik van Riel 576096667f8aSDaniel Vetter return ret; 576128b2ee20SRik van Riel } 57625a73633eSUwe Kleine-König EXPORT_SYMBOL_GPL(generic_access_phys); 576328b2ee20SRik van Riel #endif 576428b2ee20SRik van Riel 57650ec76a11SDavid Howells /* 5766d3f5ffcaSJohn Hubbard * Access another process' address space as given in mm. 57670ec76a11SDavid Howells */ 5768d3f5ffcaSJohn Hubbard int __access_remote_vm(struct mm_struct *mm, unsigned long addr, void *buf, 5769d3f5ffcaSJohn Hubbard int len, unsigned int gup_flags) 57700ec76a11SDavid Howells { 57710ec76a11SDavid Howells void *old_buf = buf; 5772442486ecSLorenzo Stoakes int write = gup_flags & FOLL_WRITE; 57730ec76a11SDavid Howells 5774d8ed45c5SMichel Lespinasse if (mmap_read_lock_killable(mm)) 57751e426fe2SKonstantin Khlebnikov return 0; 57761e426fe2SKonstantin Khlebnikov 577722883973SKirill A. Shutemov /* Untag the address before looking up the VMA */ 577822883973SKirill A. Shutemov addr = untagged_addr_remote(mm, addr); 577922883973SKirill A. Shutemov 5780eee9c708SLinus Torvalds /* Avoid triggering the temporary warning in __get_user_pages */ 5781eee9c708SLinus Torvalds if (!vma_lookup(mm, addr) && !expand_stack(mm, addr)) 5782eee9c708SLinus Torvalds return 0; 5783eee9c708SLinus Torvalds 5784183ff22bSSimon Arlott /* ignore errors, just check how much was successfully transferred */ 57850ec76a11SDavid Howells while (len) { 5786ca5e8632SLorenzo Stoakes int bytes, offset; 57870ec76a11SDavid Howells void *maddr; 5788ca5e8632SLorenzo Stoakes struct vm_area_struct *vma = NULL; 5789ca5e8632SLorenzo Stoakes struct page *page = get_user_page_vma_remote(mm, addr, 5790ca5e8632SLorenzo Stoakes gup_flags, &vma); 57910ec76a11SDavid Howells 5792ca5e8632SLorenzo Stoakes if (IS_ERR_OR_NULL(page)) { 57938d7071afSLinus Torvalds /* We might need to expand the stack to access it */ 57948d7071afSLinus Torvalds vma = vma_lookup(mm, addr); 57958d7071afSLinus Torvalds if (!vma) { 57968d7071afSLinus Torvalds vma = expand_stack(mm, addr); 57979471f1f2SLinus Torvalds 57989471f1f2SLinus Torvalds /* mmap_lock was dropped on failure */ 57998d7071afSLinus Torvalds if (!vma) 58009471f1f2SLinus Torvalds return buf - old_buf; 58019471f1f2SLinus Torvalds 58029471f1f2SLinus Torvalds /* Try again if stack expansion worked */ 58039471f1f2SLinus Torvalds continue; 58048d7071afSLinus Torvalds } 58058d7071afSLinus Torvalds 5806ca5e8632SLorenzo Stoakes 580728b2ee20SRik van Riel /* 580828b2ee20SRik van Riel * Check if this is a VM_IO | VM_PFNMAP VMA, which 580928b2ee20SRik van Riel * we can access using slightly different code. 581028b2ee20SRik van Riel */ 58119471f1f2SLinus Torvalds bytes = 0; 58129471f1f2SLinus Torvalds #ifdef CONFIG_HAVE_IOREMAP_PROT 581328b2ee20SRik van Riel if (vma->vm_ops && vma->vm_ops->access) 58149471f1f2SLinus Torvalds bytes = vma->vm_ops->access(vma, addr, buf, 581528b2ee20SRik van Riel len, write); 5816dbffcd03SRik van Riel #endif 58179471f1f2SLinus Torvalds if (bytes <= 0) 58189471f1f2SLinus Torvalds break; 581928b2ee20SRik van Riel } else { 58200ec76a11SDavid Howells bytes = len; 58210ec76a11SDavid Howells offset = addr & (PAGE_SIZE-1); 58220ec76a11SDavid Howells if (bytes > PAGE_SIZE-offset) 58230ec76a11SDavid Howells bytes = PAGE_SIZE-offset; 58240ec76a11SDavid Howells 58250ec76a11SDavid Howells maddr = kmap(page); 58260ec76a11SDavid Howells if (write) { 58270ec76a11SDavid Howells copy_to_user_page(vma, page, addr, 58280ec76a11SDavid Howells maddr + offset, buf, bytes); 58290ec76a11SDavid Howells set_page_dirty_lock(page); 58300ec76a11SDavid Howells } else { 58310ec76a11SDavid Howells copy_from_user_page(vma, page, addr, 58320ec76a11SDavid Howells buf, maddr + offset, bytes); 58330ec76a11SDavid Howells } 58340ec76a11SDavid Howells kunmap(page); 583509cbfeafSKirill A. Shutemov put_page(page); 583628b2ee20SRik van Riel } 58370ec76a11SDavid Howells len -= bytes; 58380ec76a11SDavid Howells buf += bytes; 58390ec76a11SDavid Howells addr += bytes; 58400ec76a11SDavid Howells } 5841d8ed45c5SMichel Lespinasse mmap_read_unlock(mm); 58420ec76a11SDavid Howells 58430ec76a11SDavid Howells return buf - old_buf; 58440ec76a11SDavid Howells } 584503252919SAndi Kleen 58465ddd36b9SStephen Wilson /** 5847ae91dbfcSRandy Dunlap * access_remote_vm - access another process' address space 58485ddd36b9SStephen Wilson * @mm: the mm_struct of the target address space 58495ddd36b9SStephen Wilson * @addr: start address to access 58505ddd36b9SStephen Wilson * @buf: source or destination buffer 58515ddd36b9SStephen Wilson * @len: number of bytes to transfer 58526347e8d5SLorenzo Stoakes * @gup_flags: flags modifying lookup behaviour 58535ddd36b9SStephen Wilson * 58545ddd36b9SStephen Wilson * The caller must hold a reference on @mm. 5855a862f68aSMike Rapoport * 5856a862f68aSMike Rapoport * Return: number of bytes copied from source to destination. 58575ddd36b9SStephen Wilson */ 58585ddd36b9SStephen Wilson int access_remote_vm(struct mm_struct *mm, unsigned long addr, 58596347e8d5SLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 58605ddd36b9SStephen Wilson { 5861d3f5ffcaSJohn Hubbard return __access_remote_vm(mm, addr, buf, len, gup_flags); 58625ddd36b9SStephen Wilson } 58635ddd36b9SStephen Wilson 586403252919SAndi Kleen /* 5865206cb636SStephen Wilson * Access another process' address space. 5866206cb636SStephen Wilson * Source/target buffer must be kernel space, 5867206cb636SStephen Wilson * Do not walk the page table directly, use get_user_pages 5868206cb636SStephen Wilson */ 5869206cb636SStephen Wilson int access_process_vm(struct task_struct *tsk, unsigned long addr, 5870f307ab6dSLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 5871206cb636SStephen Wilson { 5872206cb636SStephen Wilson struct mm_struct *mm; 5873206cb636SStephen Wilson int ret; 5874206cb636SStephen Wilson 5875206cb636SStephen Wilson mm = get_task_mm(tsk); 5876206cb636SStephen Wilson if (!mm) 5877206cb636SStephen Wilson return 0; 5878206cb636SStephen Wilson 5879d3f5ffcaSJohn Hubbard ret = __access_remote_vm(mm, addr, buf, len, gup_flags); 5880442486ecSLorenzo Stoakes 5881206cb636SStephen Wilson mmput(mm); 5882206cb636SStephen Wilson 5883206cb636SStephen Wilson return ret; 5884206cb636SStephen Wilson } 5885fcd35857SCatalin Marinas EXPORT_SYMBOL_GPL(access_process_vm); 5886206cb636SStephen Wilson 588703252919SAndi Kleen /* 588803252919SAndi Kleen * Print the name of a VMA. 588903252919SAndi Kleen */ 589003252919SAndi Kleen void print_vma_addr(char *prefix, unsigned long ip) 589103252919SAndi Kleen { 589203252919SAndi Kleen struct mm_struct *mm = current->mm; 589303252919SAndi Kleen struct vm_area_struct *vma; 589403252919SAndi Kleen 5895e8bff74aSIngo Molnar /* 58960a7f682dSMichal Hocko * we might be running from an atomic context so we cannot sleep 5897e8bff74aSIngo Molnar */ 5898d8ed45c5SMichel Lespinasse if (!mmap_read_trylock(mm)) 5899e8bff74aSIngo Molnar return; 5900e8bff74aSIngo Molnar 590103252919SAndi Kleen vma = find_vma(mm, ip); 590203252919SAndi Kleen if (vma && vma->vm_file) { 590303252919SAndi Kleen struct file *f = vma->vm_file; 59040a7f682dSMichal Hocko char *buf = (char *)__get_free_page(GFP_NOWAIT); 590503252919SAndi Kleen if (buf) { 59062fbc57c5SAndy Shevchenko char *p; 590703252919SAndi Kleen 59089bf39ab2SMiklos Szeredi p = file_path(f, buf, PAGE_SIZE); 590903252919SAndi Kleen if (IS_ERR(p)) 591003252919SAndi Kleen p = "?"; 59112fbc57c5SAndy Shevchenko printk("%s%s[%lx+%lx]", prefix, kbasename(p), 591203252919SAndi Kleen vma->vm_start, 591303252919SAndi Kleen vma->vm_end - vma->vm_start); 591403252919SAndi Kleen free_page((unsigned long)buf); 591503252919SAndi Kleen } 591603252919SAndi Kleen } 5917d8ed45c5SMichel Lespinasse mmap_read_unlock(mm); 591803252919SAndi Kleen } 59193ee1afa3SNick Piggin 5920662bbcb2SMichael S. Tsirkin #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP) 59219ec23531SDavid Hildenbrand void __might_fault(const char *file, int line) 59223ee1afa3SNick Piggin { 59239ec23531SDavid Hildenbrand if (pagefault_disabled()) 5924662bbcb2SMichael S. Tsirkin return; 592542a38756SThomas Gleixner __might_sleep(file, line); 59269ec23531SDavid Hildenbrand #if defined(CONFIG_DEBUG_ATOMIC_SLEEP) 5927662bbcb2SMichael S. Tsirkin if (current->mm) 5928da1c55f1SMichel Lespinasse might_lock_read(¤t->mm->mmap_lock); 59299ec23531SDavid Hildenbrand #endif 59303ee1afa3SNick Piggin } 59319ec23531SDavid Hildenbrand EXPORT_SYMBOL(__might_fault); 59323ee1afa3SNick Piggin #endif 593347ad8475SAndrea Arcangeli 593447ad8475SAndrea Arcangeli #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS) 5935c6ddfb6cSHuang Ying /* 5936c6ddfb6cSHuang Ying * Process all subpages of the specified huge page with the specified 5937c6ddfb6cSHuang Ying * operation. The target subpage will be processed last to keep its 5938c6ddfb6cSHuang Ying * cache lines hot. 5939c6ddfb6cSHuang Ying */ 59401cb9dc4bSLiu Shixin static inline int process_huge_page( 5941c6ddfb6cSHuang Ying unsigned long addr_hint, unsigned int pages_per_huge_page, 59421cb9dc4bSLiu Shixin int (*process_subpage)(unsigned long addr, int idx, void *arg), 5943c6ddfb6cSHuang Ying void *arg) 5944c6ddfb6cSHuang Ying { 59451cb9dc4bSLiu Shixin int i, n, base, l, ret; 5946c6ddfb6cSHuang Ying unsigned long addr = addr_hint & 5947c6ddfb6cSHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 5948c6ddfb6cSHuang Ying 5949c6ddfb6cSHuang Ying /* Process target subpage last to keep its cache lines hot */ 5950c6ddfb6cSHuang Ying might_sleep(); 5951c6ddfb6cSHuang Ying n = (addr_hint - addr) / PAGE_SIZE; 5952c6ddfb6cSHuang Ying if (2 * n <= pages_per_huge_page) { 5953c6ddfb6cSHuang Ying /* If target subpage in first half of huge page */ 5954c6ddfb6cSHuang Ying base = 0; 5955c6ddfb6cSHuang Ying l = n; 5956c6ddfb6cSHuang Ying /* Process subpages at the end of huge page */ 5957c6ddfb6cSHuang Ying for (i = pages_per_huge_page - 1; i >= 2 * n; i--) { 5958c6ddfb6cSHuang Ying cond_resched(); 59591cb9dc4bSLiu Shixin ret = process_subpage(addr + i * PAGE_SIZE, i, arg); 59601cb9dc4bSLiu Shixin if (ret) 59611cb9dc4bSLiu Shixin return ret; 5962c6ddfb6cSHuang Ying } 5963c6ddfb6cSHuang Ying } else { 5964c6ddfb6cSHuang Ying /* If target subpage in second half of huge page */ 5965c6ddfb6cSHuang Ying base = pages_per_huge_page - 2 * (pages_per_huge_page - n); 5966c6ddfb6cSHuang Ying l = pages_per_huge_page - n; 5967c6ddfb6cSHuang Ying /* Process subpages at the begin of huge page */ 5968c6ddfb6cSHuang Ying for (i = 0; i < base; i++) { 5969c6ddfb6cSHuang Ying cond_resched(); 59701cb9dc4bSLiu Shixin ret = process_subpage(addr + i * PAGE_SIZE, i, arg); 59711cb9dc4bSLiu Shixin if (ret) 59721cb9dc4bSLiu Shixin return ret; 5973c6ddfb6cSHuang Ying } 5974c6ddfb6cSHuang Ying } 5975c6ddfb6cSHuang Ying /* 5976c6ddfb6cSHuang Ying * Process remaining subpages in left-right-left-right pattern 5977c6ddfb6cSHuang Ying * towards the target subpage 5978c6ddfb6cSHuang Ying */ 5979c6ddfb6cSHuang Ying for (i = 0; i < l; i++) { 5980c6ddfb6cSHuang Ying int left_idx = base + i; 5981c6ddfb6cSHuang Ying int right_idx = base + 2 * l - 1 - i; 5982c6ddfb6cSHuang Ying 5983c6ddfb6cSHuang Ying cond_resched(); 59841cb9dc4bSLiu Shixin ret = process_subpage(addr + left_idx * PAGE_SIZE, left_idx, arg); 59851cb9dc4bSLiu Shixin if (ret) 59861cb9dc4bSLiu Shixin return ret; 5987c6ddfb6cSHuang Ying cond_resched(); 59881cb9dc4bSLiu Shixin ret = process_subpage(addr + right_idx * PAGE_SIZE, right_idx, arg); 59891cb9dc4bSLiu Shixin if (ret) 59901cb9dc4bSLiu Shixin return ret; 5991c6ddfb6cSHuang Ying } 59921cb9dc4bSLiu Shixin return 0; 5993c6ddfb6cSHuang Ying } 5994c6ddfb6cSHuang Ying 599547ad8475SAndrea Arcangeli static void clear_gigantic_page(struct page *page, 599647ad8475SAndrea Arcangeli unsigned long addr, 599747ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 599847ad8475SAndrea Arcangeli { 599947ad8475SAndrea Arcangeli int i; 600014455eabSCheng Li struct page *p; 600147ad8475SAndrea Arcangeli 600247ad8475SAndrea Arcangeli might_sleep(); 600314455eabSCheng Li for (i = 0; i < pages_per_huge_page; i++) { 600414455eabSCheng Li p = nth_page(page, i); 600547ad8475SAndrea Arcangeli cond_resched(); 600647ad8475SAndrea Arcangeli clear_user_highpage(p, addr + i * PAGE_SIZE); 600747ad8475SAndrea Arcangeli } 600847ad8475SAndrea Arcangeli } 6009c6ddfb6cSHuang Ying 60101cb9dc4bSLiu Shixin static int clear_subpage(unsigned long addr, int idx, void *arg) 6011c6ddfb6cSHuang Ying { 6012c6ddfb6cSHuang Ying struct page *page = arg; 6013c6ddfb6cSHuang Ying 6014c6ddfb6cSHuang Ying clear_user_highpage(page + idx, addr); 60151cb9dc4bSLiu Shixin return 0; 6016c6ddfb6cSHuang Ying } 6017c6ddfb6cSHuang Ying 601847ad8475SAndrea Arcangeli void clear_huge_page(struct page *page, 6019c79b57e4SHuang Ying unsigned long addr_hint, unsigned int pages_per_huge_page) 602047ad8475SAndrea Arcangeli { 6021c79b57e4SHuang Ying unsigned long addr = addr_hint & 6022c79b57e4SHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 602347ad8475SAndrea Arcangeli 602447ad8475SAndrea Arcangeli if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { 602547ad8475SAndrea Arcangeli clear_gigantic_page(page, addr, pages_per_huge_page); 602647ad8475SAndrea Arcangeli return; 602747ad8475SAndrea Arcangeli } 602847ad8475SAndrea Arcangeli 6029c6ddfb6cSHuang Ying process_huge_page(addr_hint, pages_per_huge_page, clear_subpage, page); 603047ad8475SAndrea Arcangeli } 603147ad8475SAndrea Arcangeli 60321cb9dc4bSLiu Shixin static int copy_user_gigantic_page(struct folio *dst, struct folio *src, 603347ad8475SAndrea Arcangeli unsigned long addr, 603447ad8475SAndrea Arcangeli struct vm_area_struct *vma, 603547ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 603647ad8475SAndrea Arcangeli { 603747ad8475SAndrea Arcangeli int i; 6038c0e8150eSZhangPeng struct page *dst_page; 6039c0e8150eSZhangPeng struct page *src_page; 604047ad8475SAndrea Arcangeli 604114455eabSCheng Li for (i = 0; i < pages_per_huge_page; i++) { 6042c0e8150eSZhangPeng dst_page = folio_page(dst, i); 6043c0e8150eSZhangPeng src_page = folio_page(src, i); 604414455eabSCheng Li 604547ad8475SAndrea Arcangeli cond_resched(); 60461cb9dc4bSLiu Shixin if (copy_mc_user_highpage(dst_page, src_page, 60471cb9dc4bSLiu Shixin addr + i*PAGE_SIZE, vma)) { 60481cb9dc4bSLiu Shixin memory_failure_queue(page_to_pfn(src_page), 0); 60491cb9dc4bSLiu Shixin return -EHWPOISON; 605047ad8475SAndrea Arcangeli } 605147ad8475SAndrea Arcangeli } 60521cb9dc4bSLiu Shixin return 0; 60531cb9dc4bSLiu Shixin } 605447ad8475SAndrea Arcangeli 6055c9f4cd71SHuang Ying struct copy_subpage_arg { 6056c9f4cd71SHuang Ying struct page *dst; 6057c9f4cd71SHuang Ying struct page *src; 6058c9f4cd71SHuang Ying struct vm_area_struct *vma; 6059c9f4cd71SHuang Ying }; 6060c9f4cd71SHuang Ying 60611cb9dc4bSLiu Shixin static int copy_subpage(unsigned long addr, int idx, void *arg) 6062c9f4cd71SHuang Ying { 6063c9f4cd71SHuang Ying struct copy_subpage_arg *copy_arg = arg; 6064c9f4cd71SHuang Ying 60651cb9dc4bSLiu Shixin if (copy_mc_user_highpage(copy_arg->dst + idx, copy_arg->src + idx, 60661cb9dc4bSLiu Shixin addr, copy_arg->vma)) { 60671cb9dc4bSLiu Shixin memory_failure_queue(page_to_pfn(copy_arg->src + idx), 0); 60681cb9dc4bSLiu Shixin return -EHWPOISON; 60691cb9dc4bSLiu Shixin } 60701cb9dc4bSLiu Shixin return 0; 6071c9f4cd71SHuang Ying } 6072c9f4cd71SHuang Ying 60731cb9dc4bSLiu Shixin int copy_user_large_folio(struct folio *dst, struct folio *src, 6074c0e8150eSZhangPeng unsigned long addr_hint, struct vm_area_struct *vma) 607547ad8475SAndrea Arcangeli { 6076c0e8150eSZhangPeng unsigned int pages_per_huge_page = folio_nr_pages(dst); 6077c9f4cd71SHuang Ying unsigned long addr = addr_hint & 6078c9f4cd71SHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 6079c9f4cd71SHuang Ying struct copy_subpage_arg arg = { 6080c0e8150eSZhangPeng .dst = &dst->page, 6081c0e8150eSZhangPeng .src = &src->page, 6082c9f4cd71SHuang Ying .vma = vma, 6083c9f4cd71SHuang Ying }; 608447ad8475SAndrea Arcangeli 60851cb9dc4bSLiu Shixin if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) 60861cb9dc4bSLiu Shixin return copy_user_gigantic_page(dst, src, addr, vma, 608747ad8475SAndrea Arcangeli pages_per_huge_page); 608847ad8475SAndrea Arcangeli 60891cb9dc4bSLiu Shixin return process_huge_page(addr_hint, pages_per_huge_page, copy_subpage, &arg); 609047ad8475SAndrea Arcangeli } 6091fa4d75c1SMike Kravetz 6092e87340caSZhangPeng long copy_folio_from_user(struct folio *dst_folio, 6093fa4d75c1SMike Kravetz const void __user *usr_src, 6094810a56b9SMike Kravetz bool allow_pagefault) 6095fa4d75c1SMike Kravetz { 6096e87340caSZhangPeng void *kaddr; 6097fa4d75c1SMike Kravetz unsigned long i, rc = 0; 6098e87340caSZhangPeng unsigned int nr_pages = folio_nr_pages(dst_folio); 6099e87340caSZhangPeng unsigned long ret_val = nr_pages * PAGE_SIZE; 610014455eabSCheng Li struct page *subpage; 6101fa4d75c1SMike Kravetz 6102e87340caSZhangPeng for (i = 0; i < nr_pages; i++) { 6103e87340caSZhangPeng subpage = folio_page(dst_folio, i); 6104e87340caSZhangPeng kaddr = kmap_local_page(subpage); 61050d508c1fSZhangPeng if (!allow_pagefault) 61060d508c1fSZhangPeng pagefault_disable(); 6107e87340caSZhangPeng rc = copy_from_user(kaddr, usr_src + i * PAGE_SIZE, PAGE_SIZE); 61080d508c1fSZhangPeng if (!allow_pagefault) 61090d508c1fSZhangPeng pagefault_enable(); 6110e87340caSZhangPeng kunmap_local(kaddr); 6111fa4d75c1SMike Kravetz 6112fa4d75c1SMike Kravetz ret_val -= (PAGE_SIZE - rc); 6113fa4d75c1SMike Kravetz if (rc) 6114fa4d75c1SMike Kravetz break; 6115fa4d75c1SMike Kravetz 6116e763243cSMuchun Song flush_dcache_page(subpage); 6117e763243cSMuchun Song 6118fa4d75c1SMike Kravetz cond_resched(); 6119fa4d75c1SMike Kravetz } 6120fa4d75c1SMike Kravetz return ret_val; 6121fa4d75c1SMike Kravetz } 612247ad8475SAndrea Arcangeli #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */ 612349076ec2SKirill A. Shutemov 612440b64acdSOlof Johansson #if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS 6125b35f1819SKirill A. Shutemov 6126b35f1819SKirill A. Shutemov static struct kmem_cache *page_ptl_cachep; 6127b35f1819SKirill A. Shutemov 6128b35f1819SKirill A. Shutemov void __init ptlock_cache_init(void) 6129b35f1819SKirill A. Shutemov { 6130b35f1819SKirill A. Shutemov page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0, 6131b35f1819SKirill A. Shutemov SLAB_PANIC, NULL); 6132b35f1819SKirill A. Shutemov } 6133b35f1819SKirill A. Shutemov 6134f5ecca06SVishal Moola (Oracle) bool ptlock_alloc(struct ptdesc *ptdesc) 613549076ec2SKirill A. Shutemov { 613649076ec2SKirill A. Shutemov spinlock_t *ptl; 613749076ec2SKirill A. Shutemov 6138b35f1819SKirill A. Shutemov ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL); 613949076ec2SKirill A. Shutemov if (!ptl) 614049076ec2SKirill A. Shutemov return false; 6141f5ecca06SVishal Moola (Oracle) ptdesc->ptl = ptl; 614249076ec2SKirill A. Shutemov return true; 614349076ec2SKirill A. Shutemov } 614449076ec2SKirill A. Shutemov 61456ed1b8a0SVishal Moola (Oracle) void ptlock_free(struct ptdesc *ptdesc) 614649076ec2SKirill A. Shutemov { 61476ed1b8a0SVishal Moola (Oracle) kmem_cache_free(page_ptl_cachep, ptdesc->ptl); 614849076ec2SKirill A. Shutemov } 614949076ec2SKirill A. Shutemov #endif 6150