1d61ea1cbSPeter 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); 377d2406291SPeng Zhang if (unlikely(xa_is_zero(next))) 378d2406291SPeng Zhang next = NULL; 379e0da382cSHugh Dickins 3808f4f8c16SHugh Dickins /* 38125d9e2d1Snpiggin@suse.de * Hide vma from rmap and truncate_pagecache before freeing 38225d9e2d1Snpiggin@suse.de * pgtables 3838f4f8c16SHugh Dickins */ 38498e51a22SSuren Baghdasaryan if (mm_wr_locked) 38598e51a22SSuren Baghdasaryan vma_start_write(vma); 3865beb4930SRik van Riel unlink_anon_vmas(vma); 3878f4f8c16SHugh Dickins unlink_file_vma(vma); 3888f4f8c16SHugh Dickins 3899da61aefSDavid Gibson if (is_vm_hugetlb_page(vma)) { 3903bf5ee95SHugh Dickins hugetlb_free_pgd_range(tlb, addr, vma->vm_end, 3913bf5ee95SHugh Dickins floor, next ? next->vm_start : ceiling); 3923bf5ee95SHugh Dickins } else { 3933bf5ee95SHugh Dickins /* 3943bf5ee95SHugh Dickins * Optimization: gather nearby vmas into one call down 3953bf5ee95SHugh Dickins */ 3963bf5ee95SHugh Dickins while (next && next->vm_start <= vma->vm_end + PMD_SIZE 3974866920bSDavid Gibson && !is_vm_hugetlb_page(next)) { 398e0da382cSHugh Dickins vma = next; 399fd892593SLiam R. Howlett next = mas_find(mas, ceiling - 1); 400d2406291SPeng Zhang if (unlikely(xa_is_zero(next))) 401d2406291SPeng Zhang next = NULL; 40298e51a22SSuren Baghdasaryan if (mm_wr_locked) 40398e51a22SSuren Baghdasaryan vma_start_write(vma); 4045beb4930SRik van Riel unlink_anon_vmas(vma); 4058f4f8c16SHugh Dickins unlink_file_vma(vma); 406e0da382cSHugh Dickins } 4073bf5ee95SHugh Dickins free_pgd_range(tlb, addr, vma->vm_end, 408e0da382cSHugh Dickins floor, next ? next->vm_start : ceiling); 4093bf5ee95SHugh Dickins } 410e0da382cSHugh Dickins vma = next; 411763ecb03SLiam R. Howlett } while (vma); 4121da177e4SLinus Torvalds } 4131da177e4SLinus Torvalds 41403c4f204SQi Zheng void pmd_install(struct mm_struct *mm, pmd_t *pmd, pgtable_t *pte) 4151da177e4SLinus Torvalds { 41603c4f204SQi Zheng spinlock_t *ptl = pmd_lock(mm, pmd); 4171bb3630eSHugh Dickins 41803c4f204SQi Zheng if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 41903c4f204SQi Zheng mm_inc_nr_ptes(mm); 420362a61adSNick Piggin /* 421362a61adSNick Piggin * Ensure all pte setup (eg. pte page lock and page clearing) are 422362a61adSNick Piggin * visible before the pte is made visible to other CPUs by being 423362a61adSNick Piggin * put into page tables. 424362a61adSNick Piggin * 425362a61adSNick Piggin * The other side of the story is the pointer chasing in the page 426362a61adSNick Piggin * table walking code (when walking the page table without locking; 427362a61adSNick Piggin * ie. most of the time). Fortunately, these data accesses consist 428362a61adSNick Piggin * of a chain of data-dependent loads, meaning most CPUs (alpha 429362a61adSNick Piggin * being the notable exception) will already guarantee loads are 430362a61adSNick Piggin * seen in-order. See the alpha page table accessors for the 431bb7cdd38SWill Deacon * smp_rmb() barriers in page table walking code. 432362a61adSNick Piggin */ 433362a61adSNick Piggin smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */ 434ed33b5a6SQi Zheng pmd_populate(mm, pmd, *pte); 435ed33b5a6SQi Zheng *pte = NULL; 4364b471e88SKirill A. Shutemov } 437c4088ebdSKirill A. Shutemov spin_unlock(ptl); 438ed33b5a6SQi Zheng } 439ed33b5a6SQi Zheng 440ed33b5a6SQi Zheng int __pte_alloc(struct mm_struct *mm, pmd_t *pmd) 441ed33b5a6SQi Zheng { 442ed33b5a6SQi Zheng pgtable_t new = pte_alloc_one(mm); 443ed33b5a6SQi Zheng if (!new) 444ed33b5a6SQi Zheng return -ENOMEM; 4458ac1f832SAndrea Arcangeli 44603c4f204SQi Zheng pmd_install(mm, pmd, &new); 4472f569afdSMartin Schwidefsky if (new) 4482f569afdSMartin Schwidefsky pte_free(mm, new); 4491bb3630eSHugh Dickins return 0; 4501da177e4SLinus Torvalds } 4511da177e4SLinus Torvalds 4524cf58924SJoel Fernandes (Google) int __pte_alloc_kernel(pmd_t *pmd) 4531da177e4SLinus Torvalds { 4544cf58924SJoel Fernandes (Google) pte_t *new = pte_alloc_one_kernel(&init_mm); 4551da177e4SLinus Torvalds if (!new) 4561bb3630eSHugh Dickins return -ENOMEM; 4571da177e4SLinus Torvalds 458872fec16SHugh Dickins spin_lock(&init_mm.page_table_lock); 4598ac1f832SAndrea Arcangeli if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 460ed33b5a6SQi Zheng smp_wmb(); /* See comment in pmd_install() */ 461872fec16SHugh Dickins pmd_populate_kernel(&init_mm, pmd, new); 4622f569afdSMartin Schwidefsky new = NULL; 4634b471e88SKirill A. Shutemov } 464872fec16SHugh Dickins spin_unlock(&init_mm.page_table_lock); 4652f569afdSMartin Schwidefsky if (new) 4662f569afdSMartin Schwidefsky pte_free_kernel(&init_mm, new); 4671bb3630eSHugh Dickins return 0; 4681da177e4SLinus Torvalds } 4691da177e4SLinus Torvalds 470d559db08SKAMEZAWA Hiroyuki static inline void init_rss_vec(int *rss) 471ae859762SHugh Dickins { 472d559db08SKAMEZAWA Hiroyuki memset(rss, 0, sizeof(int) * NR_MM_COUNTERS); 473d559db08SKAMEZAWA Hiroyuki } 474d559db08SKAMEZAWA Hiroyuki 475d559db08SKAMEZAWA Hiroyuki static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss) 476d559db08SKAMEZAWA Hiroyuki { 477d559db08SKAMEZAWA Hiroyuki int i; 478d559db08SKAMEZAWA Hiroyuki 479d559db08SKAMEZAWA Hiroyuki for (i = 0; i < NR_MM_COUNTERS; i++) 480d559db08SKAMEZAWA Hiroyuki if (rss[i]) 481d559db08SKAMEZAWA Hiroyuki add_mm_counter(mm, i, rss[i]); 482ae859762SHugh Dickins } 483ae859762SHugh Dickins 4841da177e4SLinus Torvalds /* 4856aab341eSLinus Torvalds * This function is called to print an error when a bad pte 4866aab341eSLinus Torvalds * is found. For example, we might have a PFN-mapped pte in 4876aab341eSLinus Torvalds * a region that doesn't allow it. 488b5810039SNick Piggin * 489b5810039SNick Piggin * The calling function must still handle the error. 490b5810039SNick Piggin */ 4913dc14741SHugh Dickins static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr, 4923dc14741SHugh Dickins pte_t pte, struct page *page) 493b5810039SNick Piggin { 4943dc14741SHugh Dickins pgd_t *pgd = pgd_offset(vma->vm_mm, addr); 495c2febafcSKirill A. Shutemov p4d_t *p4d = p4d_offset(pgd, addr); 496c2febafcSKirill A. Shutemov pud_t *pud = pud_offset(p4d, addr); 4973dc14741SHugh Dickins pmd_t *pmd = pmd_offset(pud, addr); 4983dc14741SHugh Dickins struct address_space *mapping; 4993dc14741SHugh Dickins pgoff_t index; 500d936cf9bSHugh Dickins static unsigned long resume; 501d936cf9bSHugh Dickins static unsigned long nr_shown; 502d936cf9bSHugh Dickins static unsigned long nr_unshown; 503d936cf9bSHugh Dickins 504d936cf9bSHugh Dickins /* 505d936cf9bSHugh Dickins * Allow a burst of 60 reports, then keep quiet for that minute; 506d936cf9bSHugh Dickins * or allow a steady drip of one report per second. 507d936cf9bSHugh Dickins */ 508d936cf9bSHugh Dickins if (nr_shown == 60) { 509d936cf9bSHugh Dickins if (time_before(jiffies, resume)) { 510d936cf9bSHugh Dickins nr_unshown++; 511d936cf9bSHugh Dickins return; 512d936cf9bSHugh Dickins } 513d936cf9bSHugh Dickins if (nr_unshown) { 5141170532bSJoe Perches pr_alert("BUG: Bad page map: %lu messages suppressed\n", 515d936cf9bSHugh Dickins nr_unshown); 516d936cf9bSHugh Dickins nr_unshown = 0; 517d936cf9bSHugh Dickins } 518d936cf9bSHugh Dickins nr_shown = 0; 519d936cf9bSHugh Dickins } 520d936cf9bSHugh Dickins if (nr_shown++ == 0) 521d936cf9bSHugh Dickins resume = jiffies + 60 * HZ; 5223dc14741SHugh Dickins 5233dc14741SHugh Dickins mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL; 5243dc14741SHugh Dickins index = linear_page_index(vma, addr); 5253dc14741SHugh Dickins 5261170532bSJoe Perches pr_alert("BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n", 5273dc14741SHugh Dickins current->comm, 5283dc14741SHugh Dickins (long long)pte_val(pte), (long long)pmd_val(*pmd)); 529718a3821SWu Fengguang if (page) 530f0b791a3SDave Hansen dump_page(page, "bad pte"); 5316aa9b8b2SKefeng Wang pr_alert("addr:%px vm_flags:%08lx anon_vma:%px mapping:%px index:%lx\n", 5323dc14741SHugh Dickins (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index); 5337e0a1265SMatthew Wilcox (Oracle) pr_alert("file:%pD fault:%ps mmap:%ps read_folio:%ps\n", 5342682582aSKonstantin Khlebnikov vma->vm_file, 5352682582aSKonstantin Khlebnikov vma->vm_ops ? vma->vm_ops->fault : NULL, 5362682582aSKonstantin Khlebnikov vma->vm_file ? vma->vm_file->f_op->mmap : NULL, 5377e0a1265SMatthew Wilcox (Oracle) mapping ? mapping->a_ops->read_folio : NULL); 538b5810039SNick Piggin dump_stack(); 539373d4d09SRusty Russell add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); 540b5810039SNick Piggin } 541b5810039SNick Piggin 542b5810039SNick Piggin /* 5437e675137SNick Piggin * vm_normal_page -- This function gets the "struct page" associated with a pte. 5446aab341eSLinus Torvalds * 5457e675137SNick Piggin * "Special" mappings do not wish to be associated with a "struct page" (either 5467e675137SNick Piggin * it doesn't exist, or it exists but they don't want to touch it). In this 5477e675137SNick Piggin * case, NULL is returned here. "Normal" mappings do have a struct page. 548b379d790SJared Hulbert * 5497e675137SNick Piggin * There are 2 broad cases. Firstly, an architecture may define a pte_special() 5507e675137SNick Piggin * pte bit, in which case this function is trivial. Secondly, an architecture 5517e675137SNick Piggin * may not have a spare pte bit, which requires a more complicated scheme, 5527e675137SNick Piggin * described below. 5537e675137SNick Piggin * 5547e675137SNick Piggin * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a 5557e675137SNick Piggin * special mapping (even if there are underlying and valid "struct pages"). 5567e675137SNick Piggin * COWed pages of a VM_PFNMAP are always normal. 5576aab341eSLinus Torvalds * 558b379d790SJared Hulbert * The way we recognize COWed pages within VM_PFNMAP mappings is through the 559b379d790SJared Hulbert * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit 5607e675137SNick Piggin * set, and the vm_pgoff will point to the first PFN mapped: thus every special 5617e675137SNick Piggin * mapping will always honor the rule 5626aab341eSLinus Torvalds * 5636aab341eSLinus Torvalds * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT) 5646aab341eSLinus Torvalds * 5657e675137SNick Piggin * And for normal mappings this is false. 566b379d790SJared Hulbert * 5677e675137SNick Piggin * This restricts such mappings to be a linear translation from virtual address 5687e675137SNick Piggin * to pfn. To get around this restriction, we allow arbitrary mappings so long 5697e675137SNick Piggin * as the vma is not a COW mapping; in that case, we know that all ptes are 5707e675137SNick Piggin * special (because none can have been COWed). 571b379d790SJared Hulbert * 572b379d790SJared Hulbert * 5737e675137SNick Piggin * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP. 5747e675137SNick Piggin * 575b379d790SJared Hulbert * VM_MIXEDMAP mappings can likewise contain memory with or without "struct 576b379d790SJared Hulbert * page" backing, however the difference is that _all_ pages with a struct 577b379d790SJared Hulbert * page (that is, those where pfn_valid is true) are refcounted and considered 578b379d790SJared Hulbert * normal pages by the VM. The disadvantage is that pages are refcounted 579b379d790SJared Hulbert * (which can be slower and simply not an option for some PFNMAP users). The 580b379d790SJared Hulbert * advantage is that we don't have to follow the strict linearity rule of 581b379d790SJared Hulbert * PFNMAP mappings in order to support COWable mappings. 582b379d790SJared Hulbert * 583ee498ed7SHugh Dickins */ 58425b2995aSChristoph Hellwig struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, 58525b2995aSChristoph Hellwig pte_t pte) 586ee498ed7SHugh Dickins { 58722b31eecSHugh Dickins unsigned long pfn = pte_pfn(pte); 5887e675137SNick Piggin 58900b3a331SLaurent Dufour if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL)) { 590b38af472SHugh Dickins if (likely(!pte_special(pte))) 59122b31eecSHugh Dickins goto check_pfn; 592667a0a06SDavid Vrabel if (vma->vm_ops && vma->vm_ops->find_special_page) 593667a0a06SDavid Vrabel return vma->vm_ops->find_special_page(vma, addr); 594a13ea5b7SHugh Dickins if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)) 595a13ea5b7SHugh Dickins return NULL; 596df6ad698SJérôme Glisse if (is_zero_pfn(pfn)) 597df6ad698SJérôme Glisse return NULL; 598e1fb4a08SDave Jiang if (pte_devmap(pte)) 5993218f871SAlex Sierra /* 6003218f871SAlex Sierra * NOTE: New users of ZONE_DEVICE will not set pte_devmap() 6013218f871SAlex Sierra * and will have refcounts incremented on their struct pages 6023218f871SAlex Sierra * when they are inserted into PTEs, thus they are safe to 6033218f871SAlex Sierra * return here. Legacy ZONE_DEVICE pages that set pte_devmap() 6043218f871SAlex Sierra * do not have refcounts. Example of legacy ZONE_DEVICE is 6053218f871SAlex Sierra * MEMORY_DEVICE_FS_DAX type in pmem or virtio_fs drivers. 6063218f871SAlex Sierra */ 607e1fb4a08SDave Jiang return NULL; 608e1fb4a08SDave Jiang 60922b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 6107e675137SNick Piggin return NULL; 6117e675137SNick Piggin } 6127e675137SNick Piggin 61300b3a331SLaurent Dufour /* !CONFIG_ARCH_HAS_PTE_SPECIAL case follows: */ 6147e675137SNick Piggin 615b379d790SJared Hulbert if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { 616b379d790SJared Hulbert if (vma->vm_flags & VM_MIXEDMAP) { 617b379d790SJared Hulbert if (!pfn_valid(pfn)) 618b379d790SJared Hulbert return NULL; 619b379d790SJared Hulbert goto out; 620b379d790SJared Hulbert } else { 6217e675137SNick Piggin unsigned long off; 6227e675137SNick Piggin off = (addr - vma->vm_start) >> PAGE_SHIFT; 6236aab341eSLinus Torvalds if (pfn == vma->vm_pgoff + off) 6246aab341eSLinus Torvalds return NULL; 62567121172SLinus Torvalds if (!is_cow_mapping(vma->vm_flags)) 626fb155c16SLinus Torvalds return NULL; 6276aab341eSLinus Torvalds } 628b379d790SJared Hulbert } 6296aab341eSLinus Torvalds 630b38af472SHugh Dickins if (is_zero_pfn(pfn)) 631b38af472SHugh Dickins return NULL; 63200b3a331SLaurent Dufour 63322b31eecSHugh Dickins check_pfn: 63422b31eecSHugh Dickins if (unlikely(pfn > highest_memmap_pfn)) { 63522b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 63622b31eecSHugh Dickins return NULL; 63722b31eecSHugh Dickins } 6386aab341eSLinus Torvalds 6396aab341eSLinus Torvalds /* 6407e675137SNick Piggin * NOTE! We still have PageReserved() pages in the page tables. 6417e675137SNick Piggin * eg. VDSO mappings can cause them to exist. 6426aab341eSLinus Torvalds */ 643b379d790SJared Hulbert out: 6446aab341eSLinus Torvalds return pfn_to_page(pfn); 645ee498ed7SHugh Dickins } 646ee498ed7SHugh Dickins 647318e9342SVishal Moola (Oracle) struct folio *vm_normal_folio(struct vm_area_struct *vma, unsigned long addr, 648318e9342SVishal Moola (Oracle) pte_t pte) 649318e9342SVishal Moola (Oracle) { 650318e9342SVishal Moola (Oracle) struct page *page = vm_normal_page(vma, addr, pte); 651318e9342SVishal Moola (Oracle) 652318e9342SVishal Moola (Oracle) if (page) 653318e9342SVishal Moola (Oracle) return page_folio(page); 654318e9342SVishal Moola (Oracle) return NULL; 655318e9342SVishal Moola (Oracle) } 656318e9342SVishal Moola (Oracle) 65728093f9fSGerald Schaefer #ifdef CONFIG_TRANSPARENT_HUGEPAGE 65828093f9fSGerald Schaefer struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr, 65928093f9fSGerald Schaefer pmd_t pmd) 66028093f9fSGerald Schaefer { 66128093f9fSGerald Schaefer unsigned long pfn = pmd_pfn(pmd); 66228093f9fSGerald Schaefer 66328093f9fSGerald Schaefer /* 66428093f9fSGerald Schaefer * There is no pmd_special() but there may be special pmds, e.g. 66528093f9fSGerald Schaefer * in a direct-access (dax) mapping, so let's just replicate the 66600b3a331SLaurent Dufour * !CONFIG_ARCH_HAS_PTE_SPECIAL case from vm_normal_page() here. 66728093f9fSGerald Schaefer */ 66828093f9fSGerald Schaefer if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { 66928093f9fSGerald Schaefer if (vma->vm_flags & VM_MIXEDMAP) { 67028093f9fSGerald Schaefer if (!pfn_valid(pfn)) 67128093f9fSGerald Schaefer return NULL; 67228093f9fSGerald Schaefer goto out; 67328093f9fSGerald Schaefer } else { 67428093f9fSGerald Schaefer unsigned long off; 67528093f9fSGerald Schaefer off = (addr - vma->vm_start) >> PAGE_SHIFT; 67628093f9fSGerald Schaefer if (pfn == vma->vm_pgoff + off) 67728093f9fSGerald Schaefer return NULL; 67828093f9fSGerald Schaefer if (!is_cow_mapping(vma->vm_flags)) 67928093f9fSGerald Schaefer return NULL; 68028093f9fSGerald Schaefer } 68128093f9fSGerald Schaefer } 68228093f9fSGerald Schaefer 683e1fb4a08SDave Jiang if (pmd_devmap(pmd)) 684e1fb4a08SDave Jiang return NULL; 6853cde287bSYu Zhao if (is_huge_zero_pmd(pmd)) 68628093f9fSGerald Schaefer return NULL; 68728093f9fSGerald Schaefer if (unlikely(pfn > highest_memmap_pfn)) 68828093f9fSGerald Schaefer return NULL; 68928093f9fSGerald Schaefer 69028093f9fSGerald Schaefer /* 69128093f9fSGerald Schaefer * NOTE! We still have PageReserved() pages in the page tables. 69228093f9fSGerald Schaefer * eg. VDSO mappings can cause them to exist. 69328093f9fSGerald Schaefer */ 69428093f9fSGerald Schaefer out: 69528093f9fSGerald Schaefer return pfn_to_page(pfn); 69628093f9fSGerald Schaefer } 69765610453SKefeng Wang 69865610453SKefeng Wang struct folio *vm_normal_folio_pmd(struct vm_area_struct *vma, 69965610453SKefeng Wang unsigned long addr, pmd_t pmd) 70065610453SKefeng Wang { 70165610453SKefeng Wang struct page *page = vm_normal_page_pmd(vma, addr, pmd); 70265610453SKefeng Wang 70365610453SKefeng Wang if (page) 70465610453SKefeng Wang return page_folio(page); 70565610453SKefeng Wang return NULL; 70665610453SKefeng Wang } 70728093f9fSGerald Schaefer #endif 70828093f9fSGerald Schaefer 709b756a3b5SAlistair Popple static void restore_exclusive_pte(struct vm_area_struct *vma, 710b756a3b5SAlistair Popple struct page *page, unsigned long address, 711b756a3b5SAlistair Popple pte_t *ptep) 712b756a3b5SAlistair Popple { 713c33c7948SRyan Roberts pte_t orig_pte; 714b756a3b5SAlistair Popple pte_t pte; 715b756a3b5SAlistair Popple swp_entry_t entry; 716b756a3b5SAlistair Popple 717c33c7948SRyan Roberts orig_pte = ptep_get(ptep); 718b756a3b5SAlistair Popple pte = pte_mkold(mk_pte(page, READ_ONCE(vma->vm_page_prot))); 719c33c7948SRyan Roberts if (pte_swp_soft_dirty(orig_pte)) 720b756a3b5SAlistair Popple pte = pte_mksoft_dirty(pte); 721b756a3b5SAlistair Popple 722c33c7948SRyan Roberts entry = pte_to_swp_entry(orig_pte); 723c33c7948SRyan Roberts if (pte_swp_uffd_wp(orig_pte)) 724b756a3b5SAlistair Popple pte = pte_mkuffd_wp(pte); 725b756a3b5SAlistair Popple else if (is_writable_device_exclusive_entry(entry)) 726b756a3b5SAlistair Popple pte = maybe_mkwrite(pte_mkdirty(pte), vma); 727b756a3b5SAlistair Popple 7286c287605SDavid Hildenbrand VM_BUG_ON(pte_write(pte) && !(PageAnon(page) && PageAnonExclusive(page))); 7296c287605SDavid Hildenbrand 730b756a3b5SAlistair Popple /* 731b756a3b5SAlistair Popple * No need to take a page reference as one was already 732b756a3b5SAlistair Popple * created when the swap entry was made. 733b756a3b5SAlistair Popple */ 734b756a3b5SAlistair Popple if (PageAnon(page)) 735f1e2db12SDavid Hildenbrand page_add_anon_rmap(page, vma, address, RMAP_NONE); 736b756a3b5SAlistair Popple else 737b756a3b5SAlistair Popple /* 738b756a3b5SAlistair Popple * Currently device exclusive access only supports anonymous 739b756a3b5SAlistair Popple * memory so the entry shouldn't point to a filebacked page. 740b756a3b5SAlistair Popple */ 7414d8ff640SMiaohe Lin WARN_ON_ONCE(1); 742b756a3b5SAlistair Popple 7431eba86c0SPasha Tatashin set_pte_at(vma->vm_mm, address, ptep, pte); 7441eba86c0SPasha Tatashin 745b756a3b5SAlistair Popple /* 746b756a3b5SAlistair Popple * No need to invalidate - it was non-present before. However 747b756a3b5SAlistair Popple * secondary CPUs may have mappings that need invalidating. 748b756a3b5SAlistair Popple */ 749b756a3b5SAlistair Popple update_mmu_cache(vma, address, ptep); 750b756a3b5SAlistair Popple } 751b756a3b5SAlistair Popple 752b756a3b5SAlistair Popple /* 753b756a3b5SAlistair Popple * Tries to restore an exclusive pte if the page lock can be acquired without 754b756a3b5SAlistair Popple * sleeping. 755b756a3b5SAlistair Popple */ 756b756a3b5SAlistair Popple static int 757b756a3b5SAlistair Popple try_restore_exclusive_pte(pte_t *src_pte, struct vm_area_struct *vma, 758b756a3b5SAlistair Popple unsigned long addr) 759b756a3b5SAlistair Popple { 760c33c7948SRyan Roberts swp_entry_t entry = pte_to_swp_entry(ptep_get(src_pte)); 761b756a3b5SAlistair Popple struct page *page = pfn_swap_entry_to_page(entry); 762b756a3b5SAlistair Popple 763b756a3b5SAlistair Popple if (trylock_page(page)) { 764b756a3b5SAlistair Popple restore_exclusive_pte(vma, page, addr, src_pte); 765b756a3b5SAlistair Popple unlock_page(page); 766b756a3b5SAlistair Popple return 0; 767b756a3b5SAlistair Popple } 768b756a3b5SAlistair Popple 769b756a3b5SAlistair Popple return -EBUSY; 770b756a3b5SAlistair Popple } 771b756a3b5SAlistair Popple 772ee498ed7SHugh Dickins /* 7731da177e4SLinus Torvalds * copy one vm_area from one task to the other. Assumes the page tables 7741da177e4SLinus Torvalds * already present in the new task to be cleared in the whole range 7751da177e4SLinus Torvalds * covered by this vma. 7761da177e4SLinus Torvalds */ 7771da177e4SLinus Torvalds 778df3a57d1SLinus Torvalds static unsigned long 779df3a57d1SLinus Torvalds copy_nonpresent_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, 7808f34f1eaSPeter Xu pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *dst_vma, 7818f34f1eaSPeter Xu struct vm_area_struct *src_vma, unsigned long addr, int *rss) 7821da177e4SLinus Torvalds { 7838f34f1eaSPeter Xu unsigned long vm_flags = dst_vma->vm_flags; 784c33c7948SRyan Roberts pte_t orig_pte = ptep_get(src_pte); 785c33c7948SRyan Roberts pte_t pte = orig_pte; 7861da177e4SLinus Torvalds struct page *page; 787c33c7948SRyan Roberts swp_entry_t entry = pte_to_swp_entry(orig_pte); 7880697212aSChristoph Lameter 7892022b4d1SHugh Dickins if (likely(!non_swap_entry(entry))) { 790570a335bSHugh Dickins if (swap_duplicate(entry) < 0) 7919a5cc85cSAlistair Popple return -EIO; 792570a335bSHugh Dickins 7931da177e4SLinus Torvalds /* make sure dst_mm is on swapoff's mmlist. */ 7941da177e4SLinus Torvalds if (unlikely(list_empty(&dst_mm->mmlist))) { 7951da177e4SLinus Torvalds spin_lock(&mmlist_lock); 796f412ac08SHugh Dickins if (list_empty(&dst_mm->mmlist)) 797f412ac08SHugh Dickins list_add(&dst_mm->mmlist, 798f412ac08SHugh Dickins &src_mm->mmlist); 7991da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 8001da177e4SLinus Torvalds } 8011493a191SDavid Hildenbrand /* Mark the swap entry as shared. */ 802c33c7948SRyan Roberts if (pte_swp_exclusive(orig_pte)) { 803c33c7948SRyan Roberts pte = pte_swp_clear_exclusive(orig_pte); 8041493a191SDavid Hildenbrand set_pte_at(src_mm, addr, src_pte, pte); 8051493a191SDavid Hildenbrand } 806b084d435SKAMEZAWA Hiroyuki rss[MM_SWAPENTS]++; 8072022b4d1SHugh Dickins } else if (is_migration_entry(entry)) { 808af5cdaf8SAlistair Popple page = pfn_swap_entry_to_page(entry); 8099f9f1acdSKonstantin Khlebnikov 810eca56ff9SJerome Marchand rss[mm_counter(page)]++; 8119f9f1acdSKonstantin Khlebnikov 8126c287605SDavid Hildenbrand if (!is_readable_migration_entry(entry) && 8130697212aSChristoph Lameter is_cow_mapping(vm_flags)) { 8140697212aSChristoph Lameter /* 8156c287605SDavid Hildenbrand * COW mappings require pages in both parent and child 8166c287605SDavid Hildenbrand * to be set to read. A previously exclusive entry is 8176c287605SDavid Hildenbrand * now shared. 8180697212aSChristoph Lameter */ 8194dd845b5SAlistair Popple entry = make_readable_migration_entry( 8204dd845b5SAlistair Popple swp_offset(entry)); 8210697212aSChristoph Lameter pte = swp_entry_to_pte(entry); 822c33c7948SRyan Roberts if (pte_swp_soft_dirty(orig_pte)) 823c3d16e16SCyrill Gorcunov pte = pte_swp_mksoft_dirty(pte); 824c33c7948SRyan Roberts if (pte_swp_uffd_wp(orig_pte)) 825f45ec5ffSPeter Xu pte = pte_swp_mkuffd_wp(pte); 8260697212aSChristoph Lameter set_pte_at(src_mm, addr, src_pte, pte); 8270697212aSChristoph Lameter } 8285042db43SJérôme Glisse } else if (is_device_private_entry(entry)) { 829af5cdaf8SAlistair Popple page = pfn_swap_entry_to_page(entry); 8305042db43SJérôme Glisse 8315042db43SJérôme Glisse /* 8325042db43SJérôme Glisse * Update rss count even for unaddressable pages, as 8335042db43SJérôme Glisse * they should treated just like normal pages in this 8345042db43SJérôme Glisse * respect. 8355042db43SJérôme Glisse * 8365042db43SJérôme Glisse * We will likely want to have some new rss counters 8375042db43SJérôme Glisse * for unaddressable pages, at some point. But for now 8385042db43SJérôme Glisse * keep things as they are. 8395042db43SJérôme Glisse */ 8405042db43SJérôme Glisse get_page(page); 8415042db43SJérôme Glisse rss[mm_counter(page)]++; 842fb3d824dSDavid Hildenbrand /* Cannot fail as these pages cannot get pinned. */ 843fb3d824dSDavid Hildenbrand BUG_ON(page_try_dup_anon_rmap(page, false, src_vma)); 8445042db43SJérôme Glisse 8455042db43SJérôme Glisse /* 8465042db43SJérôme Glisse * We do not preserve soft-dirty information, because so 8475042db43SJérôme Glisse * far, checkpoint/restore is the only feature that 8485042db43SJérôme Glisse * requires that. And checkpoint/restore does not work 8495042db43SJérôme Glisse * when a device driver is involved (you cannot easily 8505042db43SJérôme Glisse * save and restore device driver state). 8515042db43SJérôme Glisse */ 8524dd845b5SAlistair Popple if (is_writable_device_private_entry(entry) && 8535042db43SJérôme Glisse is_cow_mapping(vm_flags)) { 8544dd845b5SAlistair Popple entry = make_readable_device_private_entry( 8554dd845b5SAlistair Popple swp_offset(entry)); 8565042db43SJérôme Glisse pte = swp_entry_to_pte(entry); 857c33c7948SRyan Roberts if (pte_swp_uffd_wp(orig_pte)) 858f45ec5ffSPeter Xu pte = pte_swp_mkuffd_wp(pte); 8595042db43SJérôme Glisse set_pte_at(src_mm, addr, src_pte, pte); 8605042db43SJérôme Glisse } 861b756a3b5SAlistair Popple } else if (is_device_exclusive_entry(entry)) { 862b756a3b5SAlistair Popple /* 863b756a3b5SAlistair Popple * Make device exclusive entries present by restoring the 864b756a3b5SAlistair Popple * original entry then copying as for a present pte. Device 865b756a3b5SAlistair Popple * exclusive entries currently only support private writable 866b756a3b5SAlistair Popple * (ie. COW) mappings. 867b756a3b5SAlistair Popple */ 868b756a3b5SAlistair Popple VM_BUG_ON(!is_cow_mapping(src_vma->vm_flags)); 869b756a3b5SAlistair Popple if (try_restore_exclusive_pte(src_pte, src_vma, addr)) 870b756a3b5SAlistair Popple return -EBUSY; 871b756a3b5SAlistair Popple return -ENOENT; 872c56d1b62SPeter Xu } else if (is_pte_marker_entry(entry)) { 873af19487fSAxel Rasmussen pte_marker marker = copy_pte_marker(entry, dst_vma); 874af19487fSAxel Rasmussen 875af19487fSAxel Rasmussen if (marker) 876af19487fSAxel Rasmussen set_pte_at(dst_mm, addr, dst_pte, 877af19487fSAxel Rasmussen make_pte_marker(marker)); 878c56d1b62SPeter Xu return 0; 8791da177e4SLinus Torvalds } 8808f34f1eaSPeter Xu if (!userfaultfd_wp(dst_vma)) 8818f34f1eaSPeter Xu pte = pte_swp_clear_uffd_wp(pte); 882df3a57d1SLinus Torvalds set_pte_at(dst_mm, addr, dst_pte, pte); 883df3a57d1SLinus Torvalds return 0; 8841da177e4SLinus Torvalds } 8851da177e4SLinus Torvalds 88670e806e4SPeter Xu /* 887b51ad4f8SDavid Hildenbrand * Copy a present and normal page. 88870e806e4SPeter Xu * 889b51ad4f8SDavid Hildenbrand * NOTE! The usual case is that this isn't required; 890b51ad4f8SDavid Hildenbrand * instead, the caller can just increase the page refcount 891b51ad4f8SDavid Hildenbrand * and re-use the pte the traditional way. 89270e806e4SPeter Xu * 89370e806e4SPeter Xu * And if we need a pre-allocated page but don't yet have 89470e806e4SPeter Xu * one, return a negative error to let the preallocation 89570e806e4SPeter Xu * code know so that it can do so outside the page table 89670e806e4SPeter Xu * lock. 89770e806e4SPeter Xu */ 89870e806e4SPeter Xu static inline int 899c78f4636SPeter Xu copy_present_page(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, 900c78f4636SPeter Xu pte_t *dst_pte, pte_t *src_pte, unsigned long addr, int *rss, 901edf50470SMatthew Wilcox (Oracle) struct folio **prealloc, struct page *page) 90270e806e4SPeter Xu { 903edf50470SMatthew Wilcox (Oracle) struct folio *new_folio; 904b51ad4f8SDavid Hildenbrand pte_t pte; 90570e806e4SPeter Xu 906edf50470SMatthew Wilcox (Oracle) new_folio = *prealloc; 907edf50470SMatthew Wilcox (Oracle) if (!new_folio) 90870e806e4SPeter Xu return -EAGAIN; 90970e806e4SPeter Xu 91070e806e4SPeter Xu /* 91170e806e4SPeter Xu * We have a prealloc page, all good! Take it 91270e806e4SPeter Xu * over and copy the page & arm it. 91370e806e4SPeter Xu */ 91470e806e4SPeter Xu *prealloc = NULL; 915edf50470SMatthew Wilcox (Oracle) copy_user_highpage(&new_folio->page, page, addr, src_vma); 916edf50470SMatthew Wilcox (Oracle) __folio_mark_uptodate(new_folio); 917edf50470SMatthew Wilcox (Oracle) folio_add_new_anon_rmap(new_folio, dst_vma, addr); 918edf50470SMatthew Wilcox (Oracle) folio_add_lru_vma(new_folio, dst_vma); 919edf50470SMatthew Wilcox (Oracle) rss[MM_ANONPAGES]++; 92070e806e4SPeter Xu 92170e806e4SPeter Xu /* All done, just insert the new page copy in the child */ 922edf50470SMatthew Wilcox (Oracle) pte = mk_pte(&new_folio->page, dst_vma->vm_page_prot); 923c78f4636SPeter Xu pte = maybe_mkwrite(pte_mkdirty(pte), dst_vma); 924c33c7948SRyan Roberts if (userfaultfd_pte_wp(dst_vma, ptep_get(src_pte))) 9258f34f1eaSPeter Xu /* Uffd-wp needs to be delivered to dest pte as well */ 926f1eb1bacSPeter Xu pte = pte_mkuffd_wp(pte); 927c78f4636SPeter Xu set_pte_at(dst_vma->vm_mm, addr, dst_pte, pte); 92870e806e4SPeter Xu return 0; 92970e806e4SPeter Xu } 93070e806e4SPeter Xu 93170e806e4SPeter Xu /* 93270e806e4SPeter Xu * Copy one pte. Returns 0 if succeeded, or -EAGAIN if one preallocated page 93370e806e4SPeter Xu * is required to copy this pte. 93470e806e4SPeter Xu */ 93570e806e4SPeter Xu static inline int 936c78f4636SPeter Xu copy_present_pte(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, 937c78f4636SPeter Xu pte_t *dst_pte, pte_t *src_pte, unsigned long addr, int *rss, 938edf50470SMatthew Wilcox (Oracle) struct folio **prealloc) 939df3a57d1SLinus Torvalds { 940c78f4636SPeter Xu struct mm_struct *src_mm = src_vma->vm_mm; 941c78f4636SPeter Xu unsigned long vm_flags = src_vma->vm_flags; 942c33c7948SRyan Roberts pte_t pte = ptep_get(src_pte); 943df3a57d1SLinus Torvalds struct page *page; 94414ddee41SMatthew Wilcox (Oracle) struct folio *folio; 945df3a57d1SLinus Torvalds 946c78f4636SPeter Xu page = vm_normal_page(src_vma, addr, pte); 94714ddee41SMatthew Wilcox (Oracle) if (page) 94814ddee41SMatthew Wilcox (Oracle) folio = page_folio(page); 94914ddee41SMatthew Wilcox (Oracle) if (page && folio_test_anon(folio)) { 950b51ad4f8SDavid Hildenbrand /* 951b51ad4f8SDavid Hildenbrand * If this page may have been pinned by the parent process, 952b51ad4f8SDavid Hildenbrand * copy the page immediately for the child so that we'll always 953b51ad4f8SDavid Hildenbrand * guarantee the pinned page won't be randomly replaced in the 954b51ad4f8SDavid Hildenbrand * future. 955b51ad4f8SDavid Hildenbrand */ 95614ddee41SMatthew Wilcox (Oracle) folio_get(folio); 957fb3d824dSDavid Hildenbrand if (unlikely(page_try_dup_anon_rmap(page, false, src_vma))) { 958fb3d824dSDavid Hildenbrand /* Page may be pinned, we have to copy. */ 95914ddee41SMatthew Wilcox (Oracle) folio_put(folio); 960b51ad4f8SDavid Hildenbrand return copy_present_page(dst_vma, src_vma, dst_pte, src_pte, 961b51ad4f8SDavid Hildenbrand addr, rss, prealloc, page); 962fb3d824dSDavid Hildenbrand } 963edf50470SMatthew Wilcox (Oracle) rss[MM_ANONPAGES]++; 964b51ad4f8SDavid Hildenbrand } else if (page) { 96514ddee41SMatthew Wilcox (Oracle) folio_get(folio); 966fb3d824dSDavid Hildenbrand page_dup_file_rmap(page, false); 967edf50470SMatthew Wilcox (Oracle) rss[mm_counter_file(page)]++; 96870e806e4SPeter Xu } 96970e806e4SPeter Xu 9701da177e4SLinus Torvalds /* 9711da177e4SLinus Torvalds * If it's a COW mapping, write protect it both 9721da177e4SLinus Torvalds * in the parent and the child 9731da177e4SLinus Torvalds */ 9741b2de5d0SLinus Torvalds if (is_cow_mapping(vm_flags) && pte_write(pte)) { 9751da177e4SLinus Torvalds ptep_set_wrprotect(src_mm, addr, src_pte); 9763dc90795SZachary Amsden pte = pte_wrprotect(pte); 9771da177e4SLinus Torvalds } 97814ddee41SMatthew Wilcox (Oracle) VM_BUG_ON(page && folio_test_anon(folio) && PageAnonExclusive(page)); 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds /* 9811da177e4SLinus Torvalds * If it's a shared mapping, mark it clean in 9821da177e4SLinus Torvalds * the child 9831da177e4SLinus Torvalds */ 9841da177e4SLinus Torvalds if (vm_flags & VM_SHARED) 9851da177e4SLinus Torvalds pte = pte_mkclean(pte); 9861da177e4SLinus Torvalds pte = pte_mkold(pte); 9876aab341eSLinus Torvalds 9888f34f1eaSPeter Xu if (!userfaultfd_wp(dst_vma)) 989b569a176SPeter Xu pte = pte_clear_uffd_wp(pte); 990b569a176SPeter Xu 991c78f4636SPeter Xu set_pte_at(dst_vma->vm_mm, addr, dst_pte, pte); 99270e806e4SPeter Xu return 0; 9936aab341eSLinus Torvalds } 994ae859762SHugh Dickins 995294de6d8SKefeng Wang static inline struct folio *folio_prealloc(struct mm_struct *src_mm, 996294de6d8SKefeng Wang struct vm_area_struct *vma, unsigned long addr, bool need_zero) 99770e806e4SPeter Xu { 998edf50470SMatthew Wilcox (Oracle) struct folio *new_folio; 99970e806e4SPeter Xu 1000294de6d8SKefeng Wang if (need_zero) 1001294de6d8SKefeng Wang new_folio = vma_alloc_zeroed_movable_folio(vma, addr); 1002294de6d8SKefeng Wang else 1003294de6d8SKefeng Wang new_folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, vma, 1004294de6d8SKefeng Wang addr, false); 1005294de6d8SKefeng Wang 1006edf50470SMatthew Wilcox (Oracle) if (!new_folio) 100770e806e4SPeter Xu return NULL; 100870e806e4SPeter Xu 1009edf50470SMatthew Wilcox (Oracle) if (mem_cgroup_charge(new_folio, src_mm, GFP_KERNEL)) { 1010edf50470SMatthew Wilcox (Oracle) folio_put(new_folio); 101170e806e4SPeter Xu return NULL; 101270e806e4SPeter Xu } 1013e601ded4SKefeng Wang folio_throttle_swaprate(new_folio, GFP_KERNEL); 101470e806e4SPeter Xu 1015edf50470SMatthew Wilcox (Oracle) return new_folio; 10161da177e4SLinus Torvalds } 10171da177e4SLinus Torvalds 1018c78f4636SPeter Xu static int 1019c78f4636SPeter Xu copy_pte_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, 1020c78f4636SPeter Xu pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr, 1021c78f4636SPeter Xu unsigned long end) 10221da177e4SLinus Torvalds { 1023c78f4636SPeter Xu struct mm_struct *dst_mm = dst_vma->vm_mm; 1024c78f4636SPeter Xu struct mm_struct *src_mm = src_vma->vm_mm; 1025c36987e2SDaisuke Nishimura pte_t *orig_src_pte, *orig_dst_pte; 10261da177e4SLinus Torvalds pte_t *src_pte, *dst_pte; 1027c33c7948SRyan Roberts pte_t ptent; 1028c74df32cSHugh Dickins spinlock_t *src_ptl, *dst_ptl; 102970e806e4SPeter Xu int progress, ret = 0; 1030d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 1031570a335bSHugh Dickins swp_entry_t entry = (swp_entry_t){0}; 1032edf50470SMatthew Wilcox (Oracle) struct folio *prealloc = NULL; 10331da177e4SLinus Torvalds 10341da177e4SLinus Torvalds again: 103570e806e4SPeter Xu progress = 0; 1036d559db08SKAMEZAWA Hiroyuki init_rss_vec(rss); 1037d559db08SKAMEZAWA Hiroyuki 10383db82b93SHugh Dickins /* 10393db82b93SHugh Dickins * copy_pmd_range()'s prior pmd_none_or_clear_bad(src_pmd), and the 10403db82b93SHugh Dickins * error handling here, assume that exclusive mmap_lock on dst and src 10413db82b93SHugh Dickins * protects anon from unexpected THP transitions; with shmem and file 10423db82b93SHugh Dickins * protected by mmap_lock-less collapse skipping areas with anon_vma 10433db82b93SHugh Dickins * (whereas vma_needs_copy() skips areas without anon_vma). A rework 10443db82b93SHugh Dickins * can remove such assumptions later, but this is good enough for now. 10453db82b93SHugh Dickins */ 1046c74df32cSHugh Dickins dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl); 104770e806e4SPeter Xu if (!dst_pte) { 104870e806e4SPeter Xu ret = -ENOMEM; 104970e806e4SPeter Xu goto out; 105070e806e4SPeter Xu } 10513db82b93SHugh Dickins src_pte = pte_offset_map_nolock(src_mm, src_pmd, addr, &src_ptl); 10523db82b93SHugh Dickins if (!src_pte) { 10533db82b93SHugh Dickins pte_unmap_unlock(dst_pte, dst_ptl); 10543db82b93SHugh Dickins /* ret == 0 */ 10553db82b93SHugh Dickins goto out; 10563db82b93SHugh Dickins } 1057f20dc5f7SIngo Molnar spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 1058c36987e2SDaisuke Nishimura orig_src_pte = src_pte; 1059c36987e2SDaisuke Nishimura orig_dst_pte = dst_pte; 10606606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 10611da177e4SLinus Torvalds 10621da177e4SLinus Torvalds do { 10631da177e4SLinus Torvalds /* 10641da177e4SLinus Torvalds * We are holding two locks at this point - either of them 10651da177e4SLinus Torvalds * could generate latencies in another task on another CPU. 10661da177e4SLinus Torvalds */ 1067e040f218SHugh Dickins if (progress >= 32) { 1068e040f218SHugh Dickins progress = 0; 1069e040f218SHugh Dickins if (need_resched() || 107095c354feSNick Piggin spin_needbreak(src_ptl) || spin_needbreak(dst_ptl)) 10711da177e4SLinus Torvalds break; 1072e040f218SHugh Dickins } 1073c33c7948SRyan Roberts ptent = ptep_get(src_pte); 1074c33c7948SRyan Roberts if (pte_none(ptent)) { 10751da177e4SLinus Torvalds progress++; 10761da177e4SLinus Torvalds continue; 10771da177e4SLinus Torvalds } 1078c33c7948SRyan Roberts if (unlikely(!pte_present(ptent))) { 10799a5cc85cSAlistair Popple ret = copy_nonpresent_pte(dst_mm, src_mm, 108079a1971cSLinus Torvalds dst_pte, src_pte, 10818f34f1eaSPeter Xu dst_vma, src_vma, 10828f34f1eaSPeter Xu addr, rss); 10839a5cc85cSAlistair Popple if (ret == -EIO) { 1084c33c7948SRyan Roberts entry = pte_to_swp_entry(ptep_get(src_pte)); 1085570a335bSHugh Dickins break; 1086b756a3b5SAlistair Popple } else if (ret == -EBUSY) { 1087b756a3b5SAlistair Popple break; 1088b756a3b5SAlistair Popple } else if (!ret) { 10891da177e4SLinus Torvalds progress += 8; 109079a1971cSLinus Torvalds continue; 109179a1971cSLinus Torvalds } 1092b756a3b5SAlistair Popple 1093b756a3b5SAlistair Popple /* 1094b756a3b5SAlistair Popple * Device exclusive entry restored, continue by copying 1095b756a3b5SAlistair Popple * the now present pte. 1096b756a3b5SAlistair Popple */ 1097b756a3b5SAlistair Popple WARN_ON_ONCE(ret != -ENOENT); 1098b756a3b5SAlistair Popple } 109970e806e4SPeter Xu /* copy_present_pte() will clear `*prealloc' if consumed */ 1100c78f4636SPeter Xu ret = copy_present_pte(dst_vma, src_vma, dst_pte, src_pte, 1101c78f4636SPeter Xu addr, rss, &prealloc); 110270e806e4SPeter Xu /* 110370e806e4SPeter Xu * If we need a pre-allocated page for this pte, drop the 110470e806e4SPeter Xu * locks, allocate, and try again. 110570e806e4SPeter Xu */ 110670e806e4SPeter Xu if (unlikely(ret == -EAGAIN)) 110770e806e4SPeter Xu break; 110870e806e4SPeter Xu if (unlikely(prealloc)) { 110970e806e4SPeter Xu /* 111070e806e4SPeter Xu * pre-alloc page cannot be reused by next time so as 111170e806e4SPeter Xu * to strictly follow mempolicy (e.g., alloc_page_vma() 111270e806e4SPeter Xu * will allocate page according to address). This 111370e806e4SPeter Xu * could only happen if one pinned pte changed. 111470e806e4SPeter Xu */ 1115edf50470SMatthew Wilcox (Oracle) folio_put(prealloc); 111670e806e4SPeter Xu prealloc = NULL; 111770e806e4SPeter Xu } 111879a1971cSLinus Torvalds progress += 8; 11191da177e4SLinus Torvalds } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end); 11201da177e4SLinus Torvalds 11216606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 11223db82b93SHugh Dickins pte_unmap_unlock(orig_src_pte, src_ptl); 1123d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(dst_mm, rss); 1124c36987e2SDaisuke Nishimura pte_unmap_unlock(orig_dst_pte, dst_ptl); 1125c74df32cSHugh Dickins cond_resched(); 1126570a335bSHugh Dickins 11279a5cc85cSAlistair Popple if (ret == -EIO) { 11289a5cc85cSAlistair Popple VM_WARN_ON_ONCE(!entry.val); 112970e806e4SPeter Xu if (add_swap_count_continuation(entry, GFP_KERNEL) < 0) { 113070e806e4SPeter Xu ret = -ENOMEM; 113170e806e4SPeter Xu goto out; 113270e806e4SPeter Xu } 113370e806e4SPeter Xu entry.val = 0; 1134b756a3b5SAlistair Popple } else if (ret == -EBUSY) { 1135b756a3b5SAlistair Popple goto out; 11369a5cc85cSAlistair Popple } else if (ret == -EAGAIN) { 1137294de6d8SKefeng Wang prealloc = folio_prealloc(src_mm, src_vma, addr, false); 113870e806e4SPeter Xu if (!prealloc) 1139570a335bSHugh Dickins return -ENOMEM; 11409a5cc85cSAlistair Popple } else if (ret) { 11419a5cc85cSAlistair Popple VM_WARN_ON_ONCE(1); 11429a5cc85cSAlistair Popple } 11439a5cc85cSAlistair Popple 114470e806e4SPeter Xu /* We've captured and resolved the error. Reset, try again. */ 114570e806e4SPeter Xu ret = 0; 11469a5cc85cSAlistair Popple 11471da177e4SLinus Torvalds if (addr != end) 11481da177e4SLinus Torvalds goto again; 114970e806e4SPeter Xu out: 115070e806e4SPeter Xu if (unlikely(prealloc)) 1151edf50470SMatthew Wilcox (Oracle) folio_put(prealloc); 115270e806e4SPeter Xu return ret; 11531da177e4SLinus Torvalds } 11541da177e4SLinus Torvalds 1155c78f4636SPeter Xu static inline int 1156c78f4636SPeter Xu copy_pmd_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, 1157c78f4636SPeter Xu pud_t *dst_pud, pud_t *src_pud, unsigned long addr, 1158c78f4636SPeter Xu unsigned long end) 11591da177e4SLinus Torvalds { 1160c78f4636SPeter Xu struct mm_struct *dst_mm = dst_vma->vm_mm; 1161c78f4636SPeter Xu struct mm_struct *src_mm = src_vma->vm_mm; 11621da177e4SLinus Torvalds pmd_t *src_pmd, *dst_pmd; 11631da177e4SLinus Torvalds unsigned long next; 11641da177e4SLinus Torvalds 11651da177e4SLinus Torvalds dst_pmd = pmd_alloc(dst_mm, dst_pud, addr); 11661da177e4SLinus Torvalds if (!dst_pmd) 11671da177e4SLinus Torvalds return -ENOMEM; 11681da177e4SLinus Torvalds src_pmd = pmd_offset(src_pud, addr); 11691da177e4SLinus Torvalds do { 11701da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 117184c3fc4eSZi Yan if (is_swap_pmd(*src_pmd) || pmd_trans_huge(*src_pmd) 117284c3fc4eSZi Yan || pmd_devmap(*src_pmd)) { 117371e3aac0SAndrea Arcangeli int err; 1174c78f4636SPeter Xu VM_BUG_ON_VMA(next-addr != HPAGE_PMD_SIZE, src_vma); 11758f34f1eaSPeter Xu err = copy_huge_pmd(dst_mm, src_mm, dst_pmd, src_pmd, 11768f34f1eaSPeter Xu addr, dst_vma, src_vma); 117771e3aac0SAndrea Arcangeli if (err == -ENOMEM) 117871e3aac0SAndrea Arcangeli return -ENOMEM; 117971e3aac0SAndrea Arcangeli if (!err) 118071e3aac0SAndrea Arcangeli continue; 118171e3aac0SAndrea Arcangeli /* fall through */ 118271e3aac0SAndrea Arcangeli } 11831da177e4SLinus Torvalds if (pmd_none_or_clear_bad(src_pmd)) 11841da177e4SLinus Torvalds continue; 1185c78f4636SPeter Xu if (copy_pte_range(dst_vma, src_vma, dst_pmd, src_pmd, 1186c78f4636SPeter Xu addr, next)) 11871da177e4SLinus Torvalds return -ENOMEM; 11881da177e4SLinus Torvalds } while (dst_pmd++, src_pmd++, addr = next, addr != end); 11891da177e4SLinus Torvalds return 0; 11901da177e4SLinus Torvalds } 11911da177e4SLinus Torvalds 1192c78f4636SPeter Xu static inline int 1193c78f4636SPeter Xu copy_pud_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, 1194c78f4636SPeter Xu p4d_t *dst_p4d, p4d_t *src_p4d, unsigned long addr, 1195c78f4636SPeter Xu unsigned long end) 11961da177e4SLinus Torvalds { 1197c78f4636SPeter Xu struct mm_struct *dst_mm = dst_vma->vm_mm; 1198c78f4636SPeter Xu struct mm_struct *src_mm = src_vma->vm_mm; 11991da177e4SLinus Torvalds pud_t *src_pud, *dst_pud; 12001da177e4SLinus Torvalds unsigned long next; 12011da177e4SLinus Torvalds 1202c2febafcSKirill A. Shutemov dst_pud = pud_alloc(dst_mm, dst_p4d, addr); 12031da177e4SLinus Torvalds if (!dst_pud) 12041da177e4SLinus Torvalds return -ENOMEM; 1205c2febafcSKirill A. Shutemov src_pud = pud_offset(src_p4d, addr); 12061da177e4SLinus Torvalds do { 12071da177e4SLinus Torvalds next = pud_addr_end(addr, end); 1208a00cc7d9SMatthew Wilcox if (pud_trans_huge(*src_pud) || pud_devmap(*src_pud)) { 1209a00cc7d9SMatthew Wilcox int err; 1210a00cc7d9SMatthew Wilcox 1211c78f4636SPeter Xu VM_BUG_ON_VMA(next-addr != HPAGE_PUD_SIZE, src_vma); 1212a00cc7d9SMatthew Wilcox err = copy_huge_pud(dst_mm, src_mm, 1213c78f4636SPeter Xu dst_pud, src_pud, addr, src_vma); 1214a00cc7d9SMatthew Wilcox if (err == -ENOMEM) 1215a00cc7d9SMatthew Wilcox return -ENOMEM; 1216a00cc7d9SMatthew Wilcox if (!err) 1217a00cc7d9SMatthew Wilcox continue; 1218a00cc7d9SMatthew Wilcox /* fall through */ 1219a00cc7d9SMatthew Wilcox } 12201da177e4SLinus Torvalds if (pud_none_or_clear_bad(src_pud)) 12211da177e4SLinus Torvalds continue; 1222c78f4636SPeter Xu if (copy_pmd_range(dst_vma, src_vma, dst_pud, src_pud, 1223c78f4636SPeter Xu addr, next)) 12241da177e4SLinus Torvalds return -ENOMEM; 12251da177e4SLinus Torvalds } while (dst_pud++, src_pud++, addr = next, addr != end); 12261da177e4SLinus Torvalds return 0; 12271da177e4SLinus Torvalds } 12281da177e4SLinus Torvalds 1229c78f4636SPeter Xu static inline int 1230c78f4636SPeter Xu copy_p4d_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, 1231c78f4636SPeter Xu pgd_t *dst_pgd, pgd_t *src_pgd, unsigned long addr, 1232c78f4636SPeter Xu unsigned long end) 1233c2febafcSKirill A. Shutemov { 1234c78f4636SPeter Xu struct mm_struct *dst_mm = dst_vma->vm_mm; 1235c2febafcSKirill A. Shutemov p4d_t *src_p4d, *dst_p4d; 1236c2febafcSKirill A. Shutemov unsigned long next; 1237c2febafcSKirill A. Shutemov 1238c2febafcSKirill A. Shutemov dst_p4d = p4d_alloc(dst_mm, dst_pgd, addr); 1239c2febafcSKirill A. Shutemov if (!dst_p4d) 1240c2febafcSKirill A. Shutemov return -ENOMEM; 1241c2febafcSKirill A. Shutemov src_p4d = p4d_offset(src_pgd, addr); 1242c2febafcSKirill A. Shutemov do { 1243c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 1244c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(src_p4d)) 1245c2febafcSKirill A. Shutemov continue; 1246c78f4636SPeter Xu if (copy_pud_range(dst_vma, src_vma, dst_p4d, src_p4d, 1247c78f4636SPeter Xu addr, next)) 1248c2febafcSKirill A. Shutemov return -ENOMEM; 1249c2febafcSKirill A. Shutemov } while (dst_p4d++, src_p4d++, addr = next, addr != end); 1250c2febafcSKirill A. Shutemov return 0; 1251c2febafcSKirill A. Shutemov } 1252c2febafcSKirill A. Shutemov 1253c56d1b62SPeter Xu /* 1254c56d1b62SPeter Xu * Return true if the vma needs to copy the pgtable during this fork(). Return 1255c56d1b62SPeter Xu * false when we can speed up fork() by allowing lazy page faults later until 1256c56d1b62SPeter Xu * when the child accesses the memory range. 1257c56d1b62SPeter Xu */ 1258bc70fbf2SPeter Xu static bool 1259c56d1b62SPeter Xu vma_needs_copy(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) 1260c56d1b62SPeter Xu { 1261c56d1b62SPeter Xu /* 1262c56d1b62SPeter Xu * Always copy pgtables when dst_vma has uffd-wp enabled even if it's 1263c56d1b62SPeter Xu * file-backed (e.g. shmem). Because when uffd-wp is enabled, pgtable 1264c56d1b62SPeter Xu * contains uffd-wp protection information, that's something we can't 1265c56d1b62SPeter Xu * retrieve from page cache, and skip copying will lose those info. 1266c56d1b62SPeter Xu */ 1267c56d1b62SPeter Xu if (userfaultfd_wp(dst_vma)) 1268c56d1b62SPeter Xu return true; 1269c56d1b62SPeter Xu 1270bcd51a3cSMike Kravetz if (src_vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)) 1271c56d1b62SPeter Xu return true; 1272c56d1b62SPeter Xu 1273c56d1b62SPeter Xu if (src_vma->anon_vma) 1274c56d1b62SPeter Xu return true; 1275c56d1b62SPeter Xu 1276c56d1b62SPeter Xu /* 1277c56d1b62SPeter Xu * Don't copy ptes where a page fault will fill them correctly. Fork 1278c56d1b62SPeter Xu * becomes much lighter when there are big shared or private readonly 1279c56d1b62SPeter Xu * mappings. The tradeoff is that copy_page_range is more efficient 1280c56d1b62SPeter Xu * than faulting. 1281c56d1b62SPeter Xu */ 1282c56d1b62SPeter Xu return false; 1283c56d1b62SPeter Xu } 1284c56d1b62SPeter Xu 1285c78f4636SPeter Xu int 1286c78f4636SPeter Xu copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) 12871da177e4SLinus Torvalds { 12881da177e4SLinus Torvalds pgd_t *src_pgd, *dst_pgd; 12891da177e4SLinus Torvalds unsigned long next; 1290c78f4636SPeter Xu unsigned long addr = src_vma->vm_start; 1291c78f4636SPeter Xu unsigned long end = src_vma->vm_end; 1292c78f4636SPeter Xu struct mm_struct *dst_mm = dst_vma->vm_mm; 1293c78f4636SPeter Xu struct mm_struct *src_mm = src_vma->vm_mm; 1294ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 12952ec74c3eSSagi Grimberg bool is_cow; 1296cddb8a5cSAndrea Arcangeli int ret; 12971da177e4SLinus Torvalds 1298c56d1b62SPeter Xu if (!vma_needs_copy(dst_vma, src_vma)) 1299d992895bSNick Piggin return 0; 1300d992895bSNick Piggin 1301c78f4636SPeter Xu if (is_vm_hugetlb_page(src_vma)) 1302bc70fbf2SPeter Xu return copy_hugetlb_page_range(dst_mm, src_mm, dst_vma, src_vma); 13031da177e4SLinus Torvalds 1304c78f4636SPeter Xu if (unlikely(src_vma->vm_flags & VM_PFNMAP)) { 13052ab64037Svenkatesh.pallipadi@intel.com /* 13062ab64037Svenkatesh.pallipadi@intel.com * We do not free on error cases below as remove_vma 13072ab64037Svenkatesh.pallipadi@intel.com * gets called on error from higher level routine 13082ab64037Svenkatesh.pallipadi@intel.com */ 1309c78f4636SPeter Xu ret = track_pfn_copy(src_vma); 13102ab64037Svenkatesh.pallipadi@intel.com if (ret) 13112ab64037Svenkatesh.pallipadi@intel.com return ret; 13122ab64037Svenkatesh.pallipadi@intel.com } 13132ab64037Svenkatesh.pallipadi@intel.com 1314cddb8a5cSAndrea Arcangeli /* 1315cddb8a5cSAndrea Arcangeli * We need to invalidate the secondary MMU mappings only when 1316cddb8a5cSAndrea Arcangeli * there could be a permission downgrade on the ptes of the 1317cddb8a5cSAndrea Arcangeli * parent mm. And a permission downgrade will only happen if 1318cddb8a5cSAndrea Arcangeli * is_cow_mapping() returns true. 1319cddb8a5cSAndrea Arcangeli */ 1320c78f4636SPeter Xu is_cow = is_cow_mapping(src_vma->vm_flags); 1321ac46d4f3SJérôme Glisse 1322ac46d4f3SJérôme Glisse if (is_cow) { 13237269f999SJérôme Glisse mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_PAGE, 13247d4a8be0SAlistair Popple 0, src_mm, addr, end); 1325ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 132657efa1feSJason Gunthorpe /* 132757efa1feSJason Gunthorpe * Disabling preemption is not needed for the write side, as 132857efa1feSJason Gunthorpe * the read side doesn't spin, but goes to the mmap_lock. 132957efa1feSJason Gunthorpe * 133057efa1feSJason Gunthorpe * Use the raw variant of the seqcount_t write API to avoid 133157efa1feSJason Gunthorpe * lockdep complaining about preemptibility. 133257efa1feSJason Gunthorpe */ 1333e727bfd5SSuren Baghdasaryan vma_assert_write_locked(src_vma); 133457efa1feSJason Gunthorpe raw_write_seqcount_begin(&src_mm->write_protect_seq); 1335ac46d4f3SJérôme Glisse } 1336cddb8a5cSAndrea Arcangeli 1337cddb8a5cSAndrea Arcangeli ret = 0; 13381da177e4SLinus Torvalds dst_pgd = pgd_offset(dst_mm, addr); 13391da177e4SLinus Torvalds src_pgd = pgd_offset(src_mm, addr); 13401da177e4SLinus Torvalds do { 13411da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 13421da177e4SLinus Torvalds if (pgd_none_or_clear_bad(src_pgd)) 13431da177e4SLinus Torvalds continue; 1344c78f4636SPeter Xu if (unlikely(copy_p4d_range(dst_vma, src_vma, dst_pgd, src_pgd, 1345c78f4636SPeter Xu addr, next))) { 1346d155df53SMa Wupeng untrack_pfn_clear(dst_vma); 1347cddb8a5cSAndrea Arcangeli ret = -ENOMEM; 1348cddb8a5cSAndrea Arcangeli break; 1349cddb8a5cSAndrea Arcangeli } 13501da177e4SLinus Torvalds } while (dst_pgd++, src_pgd++, addr = next, addr != end); 1351cddb8a5cSAndrea Arcangeli 135257efa1feSJason Gunthorpe if (is_cow) { 135357efa1feSJason Gunthorpe raw_write_seqcount_end(&src_mm->write_protect_seq); 1354ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 135557efa1feSJason Gunthorpe } 1356cddb8a5cSAndrea Arcangeli return ret; 13571da177e4SLinus Torvalds } 13581da177e4SLinus Torvalds 13595abfd71dSPeter Xu /* Whether we should zap all COWed (private) pages too */ 13605abfd71dSPeter Xu static inline bool should_zap_cows(struct zap_details *details) 13615abfd71dSPeter Xu { 13625abfd71dSPeter Xu /* By default, zap all pages */ 13635abfd71dSPeter Xu if (!details) 13645abfd71dSPeter Xu return true; 13655abfd71dSPeter Xu 13665abfd71dSPeter Xu /* Or, we zap COWed pages only if the caller wants to */ 13672e148f1eSPeter Xu return details->even_cows; 13685abfd71dSPeter Xu } 13695abfd71dSPeter Xu 13702e148f1eSPeter Xu /* Decides whether we should zap this page with the page pointer specified */ 1371254ab940SPeter Xu static inline bool should_zap_page(struct zap_details *details, struct page *page) 13723506659eSMatthew Wilcox (Oracle) { 13735abfd71dSPeter Xu /* If we can make a decision without *page.. */ 13745abfd71dSPeter Xu if (should_zap_cows(details)) 1375254ab940SPeter Xu return true; 13763506659eSMatthew Wilcox (Oracle) 13775abfd71dSPeter Xu /* E.g. the caller passes NULL for the case of a zero page */ 13785abfd71dSPeter Xu if (!page) 1379254ab940SPeter Xu return true; 13805abfd71dSPeter Xu 13812e148f1eSPeter Xu /* Otherwise we should only zap non-anon pages */ 13822e148f1eSPeter Xu return !PageAnon(page); 13833506659eSMatthew Wilcox (Oracle) } 13843506659eSMatthew Wilcox (Oracle) 1385999dad82SPeter Xu static inline bool zap_drop_file_uffd_wp(struct zap_details *details) 1386999dad82SPeter Xu { 1387999dad82SPeter Xu if (!details) 1388999dad82SPeter Xu return false; 1389999dad82SPeter Xu 1390999dad82SPeter Xu return details->zap_flags & ZAP_FLAG_DROP_MARKER; 1391999dad82SPeter Xu } 1392999dad82SPeter Xu 1393999dad82SPeter Xu /* 1394999dad82SPeter Xu * This function makes sure that we'll replace the none pte with an uffd-wp 1395999dad82SPeter Xu * swap special pte marker when necessary. Must be with the pgtable lock held. 1396999dad82SPeter Xu */ 1397999dad82SPeter Xu static inline void 1398999dad82SPeter Xu zap_install_uffd_wp_if_needed(struct vm_area_struct *vma, 1399999dad82SPeter Xu unsigned long addr, pte_t *pte, 1400999dad82SPeter Xu struct zap_details *details, pte_t pteval) 1401999dad82SPeter Xu { 14022bad466cSPeter Xu /* Zap on anonymous always means dropping everything */ 14032bad466cSPeter Xu if (vma_is_anonymous(vma)) 14042bad466cSPeter Xu return; 14052bad466cSPeter Xu 1406999dad82SPeter Xu if (zap_drop_file_uffd_wp(details)) 1407999dad82SPeter Xu return; 1408999dad82SPeter Xu 1409999dad82SPeter Xu pte_install_uffd_wp_if_needed(vma, addr, pte, pteval); 1410999dad82SPeter Xu } 1411999dad82SPeter Xu 141251c6f666SRobin Holt static unsigned long zap_pte_range(struct mmu_gather *tlb, 1413b5810039SNick Piggin struct vm_area_struct *vma, pmd_t *pmd, 14141da177e4SLinus Torvalds unsigned long addr, unsigned long end, 141597a89413SPeter Zijlstra struct zap_details *details) 14161da177e4SLinus Torvalds { 1417b5810039SNick Piggin struct mm_struct *mm = tlb->mm; 1418d16dfc55SPeter Zijlstra int force_flush = 0; 1419d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 142097a89413SPeter Zijlstra spinlock_t *ptl; 14215f1a1907SSteven Rostedt pte_t *start_pte; 142297a89413SPeter Zijlstra pte_t *pte; 14238a5f14a2SKirill A. Shutemov swp_entry_t entry; 1424d559db08SKAMEZAWA Hiroyuki 1425ed6a7935SPeter Zijlstra tlb_change_page_size(tlb, PAGE_SIZE); 1426e303297eSPeter Zijlstra init_rss_vec(rss); 14273db82b93SHugh Dickins start_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl); 14283db82b93SHugh Dickins if (!pte) 14293db82b93SHugh Dickins return addr; 14303db82b93SHugh Dickins 14313ea27719SMel Gorman flush_tlb_batched_pending(mm); 14326606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 14331da177e4SLinus Torvalds do { 1434c33c7948SRyan Roberts pte_t ptent = ptep_get(pte); 14358018db85SPeter Xu struct page *page; 14368018db85SPeter Xu 1437166f61b9STobin C Harding if (pte_none(ptent)) 14381da177e4SLinus Torvalds continue; 143951c6f666SRobin Holt 14407b167b68SMinchan Kim if (need_resched()) 14417b167b68SMinchan Kim break; 14427b167b68SMinchan Kim 14436f5e6b9eSHugh Dickins if (pte_present(ptent)) { 14445df397deSLinus Torvalds unsigned int delay_rmap; 14455df397deSLinus Torvalds 144625b2995aSChristoph Hellwig page = vm_normal_page(vma, addr, ptent); 1447254ab940SPeter Xu if (unlikely(!should_zap_page(details, page))) 14481da177e4SLinus Torvalds continue; 1449b5810039SNick Piggin ptent = ptep_get_and_clear_full(mm, addr, pte, 1450a600388dSZachary Amsden tlb->fullmm); 1451e5136e87SRick Edgecombe arch_check_zapped_pte(vma, ptent); 14521da177e4SLinus Torvalds tlb_remove_tlb_entry(tlb, pte, addr); 1453999dad82SPeter Xu zap_install_uffd_wp_if_needed(vma, addr, pte, details, 1454999dad82SPeter Xu ptent); 1455e2942062Sxu xin if (unlikely(!page)) { 14566080d19fSxu xin ksm_might_unmap_zero_page(mm, ptent); 14571da177e4SLinus Torvalds continue; 1458e2942062Sxu xin } 1459eca56ff9SJerome Marchand 14605df397deSLinus Torvalds delay_rmap = 0; 1461eca56ff9SJerome Marchand if (!PageAnon(page)) { 14621cf35d47SLinus Torvalds if (pte_dirty(ptent)) { 14636237bcd9SHugh Dickins set_page_dirty(page); 14645df397deSLinus Torvalds if (tlb_delay_rmap(tlb)) { 14655df397deSLinus Torvalds delay_rmap = 1; 14665df397deSLinus Torvalds force_flush = 1; 14675df397deSLinus Torvalds } 14681cf35d47SLinus Torvalds } 14698788f678SYu Zhao if (pte_young(ptent) && likely(vma_has_recency(vma))) 1470bf3f3bc5SNick Piggin mark_page_accessed(page); 14716237bcd9SHugh Dickins } 1472eca56ff9SJerome Marchand rss[mm_counter(page)]--; 14735df397deSLinus Torvalds if (!delay_rmap) { 1474cea86fe2SHugh Dickins page_remove_rmap(page, vma, false); 14753dc14741SHugh Dickins if (unlikely(page_mapcount(page) < 0)) 14763dc14741SHugh Dickins print_bad_pte(vma, addr, ptent, page); 14775df397deSLinus Torvalds } 14785df397deSLinus Torvalds if (unlikely(__tlb_remove_page(tlb, page, delay_rmap))) { 14791cf35d47SLinus Torvalds force_flush = 1; 1480ce9ec37bSWill Deacon addr += PAGE_SIZE; 1481d16dfc55SPeter Zijlstra break; 14821cf35d47SLinus Torvalds } 14831da177e4SLinus Torvalds continue; 14841da177e4SLinus Torvalds } 14855042db43SJérôme Glisse 14865042db43SJérôme Glisse entry = pte_to_swp_entry(ptent); 1487b756a3b5SAlistair Popple if (is_device_private_entry(entry) || 1488b756a3b5SAlistair Popple is_device_exclusive_entry(entry)) { 14898018db85SPeter Xu page = pfn_swap_entry_to_page(entry); 1490254ab940SPeter Xu if (unlikely(!should_zap_page(details, page))) 14915042db43SJérôme Glisse continue; 1492999dad82SPeter Xu /* 1493999dad82SPeter Xu * Both device private/exclusive mappings should only 1494999dad82SPeter Xu * work with anonymous page so far, so we don't need to 1495999dad82SPeter Xu * consider uffd-wp bit when zap. For more information, 1496999dad82SPeter Xu * see zap_install_uffd_wp_if_needed(). 1497999dad82SPeter Xu */ 1498999dad82SPeter Xu WARN_ON_ONCE(!vma_is_anonymous(vma)); 14995042db43SJérôme Glisse rss[mm_counter(page)]--; 1500b756a3b5SAlistair Popple if (is_device_private_entry(entry)) 1501cea86fe2SHugh Dickins page_remove_rmap(page, vma, false); 15025042db43SJérôme Glisse put_page(page); 15038018db85SPeter Xu } else if (!non_swap_entry(entry)) { 15045abfd71dSPeter Xu /* Genuine swap entry, hence a private anon page */ 15055abfd71dSPeter Xu if (!should_zap_cows(details)) 15061da177e4SLinus Torvalds continue; 1507b084d435SKAMEZAWA Hiroyuki rss[MM_SWAPENTS]--; 15088018db85SPeter Xu if (unlikely(!free_swap_and_cache(entry))) 15098018db85SPeter Xu print_bad_pte(vma, addr, ptent, NULL); 15105abfd71dSPeter Xu } else if (is_migration_entry(entry)) { 1511af5cdaf8SAlistair Popple page = pfn_swap_entry_to_page(entry); 1512254ab940SPeter Xu if (!should_zap_page(details, page)) 15135abfd71dSPeter Xu continue; 1514eca56ff9SJerome Marchand rss[mm_counter(page)]--; 1515999dad82SPeter Xu } else if (pte_marker_entry_uffd_wp(entry)) { 15162bad466cSPeter Xu /* 15172bad466cSPeter Xu * For anon: always drop the marker; for file: only 15182bad466cSPeter Xu * drop the marker if explicitly requested. 15192bad466cSPeter Xu */ 15202bad466cSPeter Xu if (!vma_is_anonymous(vma) && 15212bad466cSPeter Xu !zap_drop_file_uffd_wp(details)) 1522999dad82SPeter Xu continue; 15239f186f9eSMiaohe Lin } else if (is_hwpoison_entry(entry) || 1524af19487fSAxel Rasmussen is_poisoned_swp_entry(entry)) { 15255abfd71dSPeter Xu if (!should_zap_cows(details)) 15265abfd71dSPeter Xu continue; 15275abfd71dSPeter Xu } else { 15285abfd71dSPeter Xu /* We should have covered all the swap entry types */ 1529727d16f1SAndrew Morton pr_alert("unrecognized swap entry 0x%lx\n", entry.val); 15305abfd71dSPeter Xu WARN_ON_ONCE(1); 15319f9f1acdSKonstantin Khlebnikov } 15329888a1caSZachary Amsden pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); 1533999dad82SPeter Xu zap_install_uffd_wp_if_needed(vma, addr, pte, details, ptent); 153497a89413SPeter Zijlstra } while (pte++, addr += PAGE_SIZE, addr != end); 1535ae859762SHugh Dickins 1536d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(mm, rss); 15376606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 153851c6f666SRobin Holt 15391cf35d47SLinus Torvalds /* Do the actual TLB flush before dropping ptl */ 15405df397deSLinus Torvalds if (force_flush) { 15411cf35d47SLinus Torvalds tlb_flush_mmu_tlbonly(tlb); 15425df397deSLinus Torvalds tlb_flush_rmaps(tlb, vma); 15435df397deSLinus Torvalds } 15441cf35d47SLinus Torvalds pte_unmap_unlock(start_pte, ptl); 15451cf35d47SLinus Torvalds 15461cf35d47SLinus Torvalds /* 15471cf35d47SLinus Torvalds * If we forced a TLB flush (either due to running out of 15481cf35d47SLinus Torvalds * batch buffers or because we needed to flush dirty TLB 15491cf35d47SLinus Torvalds * entries before releasing the ptl), free the batched 15503db82b93SHugh Dickins * memory too. Come back again if we didn't do everything. 15511cf35d47SLinus Torvalds */ 15523db82b93SHugh Dickins if (force_flush) 1553fa0aafb8SPeter Zijlstra tlb_flush_mmu(tlb); 1554d16dfc55SPeter Zijlstra 155551c6f666SRobin Holt return addr; 15561da177e4SLinus Torvalds } 15571da177e4SLinus Torvalds 155851c6f666SRobin Holt static inline unsigned long zap_pmd_range(struct mmu_gather *tlb, 1559b5810039SNick Piggin struct vm_area_struct *vma, pud_t *pud, 15601da177e4SLinus Torvalds unsigned long addr, unsigned long end, 156197a89413SPeter Zijlstra struct zap_details *details) 15621da177e4SLinus Torvalds { 15631da177e4SLinus Torvalds pmd_t *pmd; 15641da177e4SLinus Torvalds unsigned long next; 15651da177e4SLinus Torvalds 15661da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 15671da177e4SLinus Torvalds do { 15681da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 156984c3fc4eSZi Yan if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) { 157053406ed1SHugh Dickins if (next - addr != HPAGE_PMD_SIZE) 1571fd60775aSDavid Rientjes __split_huge_pmd(vma, pmd, addr, false, NULL); 15723db82b93SHugh Dickins else if (zap_huge_pmd(tlb, vma, pmd, addr)) { 15733db82b93SHugh Dickins addr = next; 15743db82b93SHugh Dickins continue; 15753db82b93SHugh Dickins } 157671e3aac0SAndrea Arcangeli /* fall through */ 15773506659eSMatthew Wilcox (Oracle) } else if (details && details->single_folio && 15783506659eSMatthew Wilcox (Oracle) folio_test_pmd_mappable(details->single_folio) && 157922061a1fSHugh Dickins next - addr == HPAGE_PMD_SIZE && pmd_none(*pmd)) { 158022061a1fSHugh Dickins spinlock_t *ptl = pmd_lock(tlb->mm, pmd); 158122061a1fSHugh Dickins /* 158222061a1fSHugh Dickins * Take and drop THP pmd lock so that we cannot return 158322061a1fSHugh Dickins * prematurely, while zap_huge_pmd() has cleared *pmd, 158422061a1fSHugh Dickins * but not yet decremented compound_mapcount(). 158522061a1fSHugh Dickins */ 158622061a1fSHugh Dickins spin_unlock(ptl); 158771e3aac0SAndrea Arcangeli } 15883db82b93SHugh Dickins if (pmd_none(*pmd)) { 15893db82b93SHugh Dickins addr = next; 15903db82b93SHugh Dickins continue; 15913db82b93SHugh Dickins } 15923db82b93SHugh Dickins addr = zap_pte_range(tlb, vma, pmd, addr, next, details); 15933db82b93SHugh Dickins if (addr != next) 15943db82b93SHugh Dickins pmd--; 15953db82b93SHugh Dickins } while (pmd++, cond_resched(), addr != end); 159651c6f666SRobin Holt 159751c6f666SRobin Holt return addr; 15981da177e4SLinus Torvalds } 15991da177e4SLinus Torvalds 160051c6f666SRobin Holt static inline unsigned long zap_pud_range(struct mmu_gather *tlb, 1601c2febafcSKirill A. Shutemov struct vm_area_struct *vma, p4d_t *p4d, 16021da177e4SLinus Torvalds unsigned long addr, unsigned long end, 160397a89413SPeter Zijlstra struct zap_details *details) 16041da177e4SLinus Torvalds { 16051da177e4SLinus Torvalds pud_t *pud; 16061da177e4SLinus Torvalds unsigned long next; 16071da177e4SLinus Torvalds 1608c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 16091da177e4SLinus Torvalds do { 16101da177e4SLinus Torvalds next = pud_addr_end(addr, end); 1611a00cc7d9SMatthew Wilcox if (pud_trans_huge(*pud) || pud_devmap(*pud)) { 1612a00cc7d9SMatthew Wilcox if (next - addr != HPAGE_PUD_SIZE) { 161342fc5414SMichel Lespinasse mmap_assert_locked(tlb->mm); 1614a00cc7d9SMatthew Wilcox split_huge_pud(vma, pud, addr); 1615a00cc7d9SMatthew Wilcox } else if (zap_huge_pud(tlb, vma, pud, addr)) 1616a00cc7d9SMatthew Wilcox goto next; 1617a00cc7d9SMatthew Wilcox /* fall through */ 1618a00cc7d9SMatthew Wilcox } 161997a89413SPeter Zijlstra if (pud_none_or_clear_bad(pud)) 16201da177e4SLinus Torvalds continue; 162197a89413SPeter Zijlstra next = zap_pmd_range(tlb, vma, pud, addr, next, details); 1622a00cc7d9SMatthew Wilcox next: 1623a00cc7d9SMatthew Wilcox cond_resched(); 162497a89413SPeter Zijlstra } while (pud++, addr = next, addr != end); 162551c6f666SRobin Holt 162651c6f666SRobin Holt return addr; 16271da177e4SLinus Torvalds } 16281da177e4SLinus Torvalds 1629c2febafcSKirill A. Shutemov static inline unsigned long zap_p4d_range(struct mmu_gather *tlb, 1630c2febafcSKirill A. Shutemov struct vm_area_struct *vma, pgd_t *pgd, 1631c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 1632c2febafcSKirill A. Shutemov struct zap_details *details) 1633c2febafcSKirill A. Shutemov { 1634c2febafcSKirill A. Shutemov p4d_t *p4d; 1635c2febafcSKirill A. Shutemov unsigned long next; 1636c2febafcSKirill A. Shutemov 1637c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 1638c2febafcSKirill A. Shutemov do { 1639c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 1640c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(p4d)) 1641c2febafcSKirill A. Shutemov continue; 1642c2febafcSKirill A. Shutemov next = zap_pud_range(tlb, vma, p4d, addr, next, details); 1643c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 1644c2febafcSKirill A. Shutemov 1645c2febafcSKirill A. Shutemov return addr; 1646c2febafcSKirill A. Shutemov } 1647c2febafcSKirill A. Shutemov 1648aac45363SMichal Hocko void unmap_page_range(struct mmu_gather *tlb, 164951c6f666SRobin Holt struct vm_area_struct *vma, 16501da177e4SLinus Torvalds unsigned long addr, unsigned long end, 165197a89413SPeter Zijlstra struct zap_details *details) 16521da177e4SLinus Torvalds { 16531da177e4SLinus Torvalds pgd_t *pgd; 16541da177e4SLinus Torvalds unsigned long next; 16551da177e4SLinus Torvalds 16561da177e4SLinus Torvalds BUG_ON(addr >= end); 16571da177e4SLinus Torvalds tlb_start_vma(tlb, vma); 16581da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 16591da177e4SLinus Torvalds do { 16601da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 166197a89413SPeter Zijlstra if (pgd_none_or_clear_bad(pgd)) 16621da177e4SLinus Torvalds continue; 1663c2febafcSKirill A. Shutemov next = zap_p4d_range(tlb, vma, pgd, addr, next, details); 166497a89413SPeter Zijlstra } while (pgd++, addr = next, addr != end); 16651da177e4SLinus Torvalds tlb_end_vma(tlb, vma); 16661da177e4SLinus Torvalds } 16671da177e4SLinus Torvalds 1668f5cc4eefSAl Viro 1669f5cc4eefSAl Viro static void unmap_single_vma(struct mmu_gather *tlb, 16701da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long start_addr, 16714f74d2c8SLinus Torvalds unsigned long end_addr, 167268f48381SSuren Baghdasaryan struct zap_details *details, bool mm_wr_locked) 16731da177e4SLinus Torvalds { 1674f5cc4eefSAl Viro unsigned long start = max(vma->vm_start, start_addr); 16751da177e4SLinus Torvalds unsigned long end; 16761da177e4SLinus Torvalds 16771da177e4SLinus Torvalds if (start >= vma->vm_end) 1678f5cc4eefSAl Viro return; 16791da177e4SLinus Torvalds end = min(vma->vm_end, end_addr); 16801da177e4SLinus Torvalds if (end <= vma->vm_start) 1681f5cc4eefSAl Viro return; 16821da177e4SLinus Torvalds 1683cbc91f71SSrikar Dronamraju if (vma->vm_file) 1684cbc91f71SSrikar Dronamraju uprobe_munmap(vma, start, end); 1685cbc91f71SSrikar Dronamraju 1686b3b9c293SKonstantin Khlebnikov if (unlikely(vma->vm_flags & VM_PFNMAP)) 168768f48381SSuren Baghdasaryan untrack_pfn(vma, 0, 0, mm_wr_locked); 16882ab64037Svenkatesh.pallipadi@intel.com 16898b2a1238SAl Viro if (start != end) { 169051c6f666SRobin Holt if (unlikely(is_vm_hugetlb_page(vma))) { 1691a137e1ccSAndi Kleen /* 1692a137e1ccSAndi Kleen * It is undesirable to test vma->vm_file as it 1693a137e1ccSAndi Kleen * should be non-null for valid hugetlb area. 1694a137e1ccSAndi Kleen * However, vm_file will be NULL in the error 16957aa6b4adSDavidlohr Bueso * cleanup path of mmap_region. When 1696a137e1ccSAndi Kleen * hugetlbfs ->mmap method fails, 16977aa6b4adSDavidlohr Bueso * mmap_region() nullifies vma->vm_file 1698a137e1ccSAndi Kleen * before calling this function to clean up. 1699a137e1ccSAndi Kleen * Since no pte has actually been setup, it is 1700a137e1ccSAndi Kleen * safe to do nothing in this case. 1701a137e1ccSAndi Kleen */ 170224669e58SAneesh Kumar K.V if (vma->vm_file) { 170305e90bd0SPeter Xu zap_flags_t zap_flags = details ? 170405e90bd0SPeter Xu details->zap_flags : 0; 17052820b0f0SRik van Riel __unmap_hugepage_range(tlb, vma, start, end, 170605e90bd0SPeter Xu NULL, zap_flags); 170724669e58SAneesh Kumar K.V } 170851c6f666SRobin Holt } else 1709038c7aa1SAl Viro unmap_page_range(tlb, vma, start, end, details); 171097a89413SPeter Zijlstra } 171151c6f666SRobin Holt } 17121da177e4SLinus Torvalds 1713f5cc4eefSAl Viro /** 1714f5cc4eefSAl Viro * unmap_vmas - unmap a range of memory covered by a list of vma's 1715f5cc4eefSAl Viro * @tlb: address of the caller's struct mmu_gather 17166e412203SYang Li * @mas: the maple state 1717f5cc4eefSAl Viro * @vma: the starting vma 1718f5cc4eefSAl Viro * @start_addr: virtual address at which to start unmapping 1719f5cc4eefSAl Viro * @end_addr: virtual address at which to end unmapping 17206e412203SYang Li * @tree_end: The maximum index to check 1721809ef83cSYang Li * @mm_wr_locked: lock flag 1722f5cc4eefSAl Viro * 1723f5cc4eefSAl Viro * Unmap all pages in the vma list. 1724f5cc4eefSAl Viro * 1725f5cc4eefSAl Viro * Only addresses between `start' and `end' will be unmapped. 1726f5cc4eefSAl Viro * 1727f5cc4eefSAl Viro * The VMA list must be sorted in ascending virtual address order. 1728f5cc4eefSAl Viro * 1729f5cc4eefSAl Viro * unmap_vmas() assumes that the caller will flush the whole unmapped address 1730f5cc4eefSAl Viro * range after unmap_vmas() returns. So the only responsibility here is to 1731f5cc4eefSAl Viro * ensure that any thus-far unmapped pages are flushed before unmap_vmas() 1732f5cc4eefSAl Viro * drops the lock and schedules. 1733f5cc4eefSAl Viro */ 1734fd892593SLiam R. Howlett void unmap_vmas(struct mmu_gather *tlb, struct ma_state *mas, 1735f5cc4eefSAl Viro struct vm_area_struct *vma, unsigned long start_addr, 1736fd892593SLiam R. Howlett unsigned long end_addr, unsigned long tree_end, 1737fd892593SLiam R. Howlett bool mm_wr_locked) 1738f5cc4eefSAl Viro { 1739ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 1740999dad82SPeter Xu struct zap_details details = { 174104ada095SMike Kravetz .zap_flags = ZAP_FLAG_DROP_MARKER | ZAP_FLAG_UNMAP, 1742999dad82SPeter Xu /* Careful - we need to zap private pages too! */ 1743999dad82SPeter Xu .even_cows = true, 1744999dad82SPeter Xu }; 1745f5cc4eefSAl Viro 17467d4a8be0SAlistair Popple mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma->vm_mm, 17476f4f13e8SJérôme Glisse start_addr, end_addr); 1748ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 1749763ecb03SLiam R. Howlett do { 17502820b0f0SRik van Riel unsigned long start = start_addr; 17512820b0f0SRik van Riel unsigned long end = end_addr; 17522820b0f0SRik van Riel hugetlb_zap_begin(vma, &start, &end); 17532820b0f0SRik van Riel unmap_single_vma(tlb, vma, start, end, &details, 175468f48381SSuren Baghdasaryan mm_wr_locked); 17552820b0f0SRik van Riel hugetlb_zap_end(vma, &details); 1756d2406291SPeng Zhang vma = mas_find(mas, tree_end - 1); 1757d2406291SPeng Zhang } while (vma && likely(!xa_is_zero(vma))); 1758ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 17591da177e4SLinus Torvalds } 17601da177e4SLinus Torvalds 17611da177e4SLinus Torvalds /** 1762f5cc4eefSAl Viro * zap_page_range_single - remove user pages in a given range 1763f5cc4eefSAl Viro * @vma: vm_area_struct holding the applicable pages 1764f5cc4eefSAl Viro * @address: starting address of pages to zap 1765f5cc4eefSAl Viro * @size: number of bytes to zap 17668a5f14a2SKirill A. Shutemov * @details: details of shared cache invalidation 1767f5cc4eefSAl Viro * 1768f5cc4eefSAl Viro * The range must fit into one VMA. 1769f5cc4eefSAl Viro */ 177021b85b09SMike Kravetz void zap_page_range_single(struct vm_area_struct *vma, unsigned long address, 1771f5cc4eefSAl Viro unsigned long size, struct zap_details *details) 1772f5cc4eefSAl Viro { 177321b85b09SMike Kravetz const unsigned long end = address + size; 1774ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 1775f5cc4eefSAl Viro struct mmu_gather tlb; 1776f5cc4eefSAl Viro 1777f5cc4eefSAl Viro lru_add_drain(); 17787d4a8be0SAlistair Popple mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma->vm_mm, 177921b85b09SMike Kravetz address, end); 17802820b0f0SRik van Riel hugetlb_zap_begin(vma, &range.start, &range.end); 1781a72afd87SWill Deacon tlb_gather_mmu(&tlb, vma->vm_mm); 1782ac46d4f3SJérôme Glisse update_hiwater_rss(vma->vm_mm); 1783ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 178421b85b09SMike Kravetz /* 178521b85b09SMike Kravetz * unmap 'address-end' not 'range.start-range.end' as range 178621b85b09SMike Kravetz * could have been expanded for hugetlb pmd sharing. 178721b85b09SMike Kravetz */ 178868f48381SSuren Baghdasaryan unmap_single_vma(&tlb, vma, address, end, details, false); 1789ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 1790ae8eba8bSWill Deacon tlb_finish_mmu(&tlb); 17912820b0f0SRik van Riel hugetlb_zap_end(vma, details); 17921da177e4SLinus Torvalds } 17931da177e4SLinus Torvalds 1794c627f9ccSJack Steiner /** 1795c627f9ccSJack Steiner * zap_vma_ptes - remove ptes mapping the vma 1796c627f9ccSJack Steiner * @vma: vm_area_struct holding ptes to be zapped 1797c627f9ccSJack Steiner * @address: starting address of pages to zap 1798c627f9ccSJack Steiner * @size: number of bytes to zap 1799c627f9ccSJack Steiner * 1800c627f9ccSJack Steiner * This function only unmaps ptes assigned to VM_PFNMAP vmas. 1801c627f9ccSJack Steiner * 1802c627f9ccSJack Steiner * The entire address range must be fully contained within the vma. 1803c627f9ccSJack Steiner * 1804c627f9ccSJack Steiner */ 180527d036e3SLeon Romanovsky void zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, 1806c627f9ccSJack Steiner unsigned long size) 1807c627f9ccSJack Steiner { 180888a35912SMiaohe Lin if (!range_in_vma(vma, address, address + size) || 1809c627f9ccSJack Steiner !(vma->vm_flags & VM_PFNMAP)) 181027d036e3SLeon Romanovsky return; 181127d036e3SLeon Romanovsky 1812f5cc4eefSAl Viro zap_page_range_single(vma, address, size, NULL); 1813c627f9ccSJack Steiner } 1814c627f9ccSJack Steiner EXPORT_SYMBOL_GPL(zap_vma_ptes); 1815c627f9ccSJack Steiner 18168cd3984dSArjun Roy static pmd_t *walk_to_pmd(struct mm_struct *mm, unsigned long addr) 1817c9cfcddfSLinus Torvalds { 1818c2febafcSKirill A. Shutemov pgd_t *pgd; 1819c2febafcSKirill A. Shutemov p4d_t *p4d; 1820c2febafcSKirill A. Shutemov pud_t *pud; 1821c2febafcSKirill A. Shutemov pmd_t *pmd; 1822c2febafcSKirill A. Shutemov 1823c2febafcSKirill A. Shutemov pgd = pgd_offset(mm, addr); 1824c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, addr); 1825c2febafcSKirill A. Shutemov if (!p4d) 1826c2febafcSKirill A. Shutemov return NULL; 1827c2febafcSKirill A. Shutemov pud = pud_alloc(mm, p4d, addr); 1828c2febafcSKirill A. Shutemov if (!pud) 1829c2febafcSKirill A. Shutemov return NULL; 1830c2febafcSKirill A. Shutemov pmd = pmd_alloc(mm, pud, addr); 1831c2febafcSKirill A. Shutemov if (!pmd) 1832c2febafcSKirill A. Shutemov return NULL; 1833c2febafcSKirill A. Shutemov 1834f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 18358cd3984dSArjun Roy return pmd; 18368cd3984dSArjun Roy } 18378cd3984dSArjun Roy 18388cd3984dSArjun Roy pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr, 18398cd3984dSArjun Roy spinlock_t **ptl) 18408cd3984dSArjun Roy { 18418cd3984dSArjun Roy pmd_t *pmd = walk_to_pmd(mm, addr); 18428cd3984dSArjun Roy 18438cd3984dSArjun Roy if (!pmd) 18448cd3984dSArjun Roy return NULL; 1845c9cfcddfSLinus Torvalds return pte_alloc_map_lock(mm, pmd, addr, ptl); 1846c9cfcddfSLinus Torvalds } 1847c9cfcddfSLinus Torvalds 18488efd6f5bSArjun Roy static int validate_page_before_insert(struct page *page) 18498efd6f5bSArjun Roy { 1850f8b6187dSKefeng Wang struct folio *folio = page_folio(page); 1851f8b6187dSKefeng Wang 1852f8b6187dSKefeng Wang if (folio_test_anon(folio) || folio_test_slab(folio) || 1853f8b6187dSKefeng Wang page_has_type(page)) 18548efd6f5bSArjun Roy return -EINVAL; 1855f8b6187dSKefeng Wang flush_dcache_folio(folio); 18568efd6f5bSArjun Roy return 0; 18578efd6f5bSArjun Roy } 18588efd6f5bSArjun Roy 1859cea86fe2SHugh Dickins static int insert_page_into_pte_locked(struct vm_area_struct *vma, pte_t *pte, 18608efd6f5bSArjun Roy unsigned long addr, struct page *page, pgprot_t prot) 18618efd6f5bSArjun Roy { 1862*ef37b2eaSDavid Hildenbrand struct folio *folio = page_folio(page); 1863*ef37b2eaSDavid Hildenbrand 1864c33c7948SRyan Roberts if (!pte_none(ptep_get(pte))) 18658efd6f5bSArjun Roy return -EBUSY; 18668efd6f5bSArjun Roy /* Ok, finally just insert the thing.. */ 1867*ef37b2eaSDavid Hildenbrand folio_get(folio); 1868f1a79412SShakeel Butt inc_mm_counter(vma->vm_mm, mm_counter_file(page)); 1869*ef37b2eaSDavid Hildenbrand folio_add_file_rmap_pte(folio, page, vma); 1870cea86fe2SHugh Dickins set_pte_at(vma->vm_mm, addr, pte, mk_pte(page, prot)); 18718efd6f5bSArjun Roy return 0; 18728efd6f5bSArjun Roy } 18738efd6f5bSArjun Roy 18741da177e4SLinus Torvalds /* 1875238f58d8SLinus Torvalds * This is the old fallback for page remapping. 1876238f58d8SLinus Torvalds * 1877238f58d8SLinus Torvalds * For historical reasons, it only allows reserved pages. Only 1878238f58d8SLinus Torvalds * old drivers should use this, and they needed to mark their 1879238f58d8SLinus Torvalds * pages reserved for the old functions anyway. 1880238f58d8SLinus Torvalds */ 1881423bad60SNick Piggin static int insert_page(struct vm_area_struct *vma, unsigned long addr, 1882423bad60SNick Piggin struct page *page, pgprot_t prot) 1883238f58d8SLinus Torvalds { 1884238f58d8SLinus Torvalds int retval; 1885238f58d8SLinus Torvalds pte_t *pte; 1886238f58d8SLinus Torvalds spinlock_t *ptl; 1887238f58d8SLinus Torvalds 18888efd6f5bSArjun Roy retval = validate_page_before_insert(page); 18898efd6f5bSArjun Roy if (retval) 18905b4e655eSKAMEZAWA Hiroyuki goto out; 1891238f58d8SLinus Torvalds retval = -ENOMEM; 1892cea86fe2SHugh Dickins pte = get_locked_pte(vma->vm_mm, addr, &ptl); 1893238f58d8SLinus Torvalds if (!pte) 18945b4e655eSKAMEZAWA Hiroyuki goto out; 1895cea86fe2SHugh Dickins retval = insert_page_into_pte_locked(vma, pte, addr, page, prot); 1896238f58d8SLinus Torvalds pte_unmap_unlock(pte, ptl); 1897238f58d8SLinus Torvalds out: 1898238f58d8SLinus Torvalds return retval; 1899238f58d8SLinus Torvalds } 1900238f58d8SLinus Torvalds 1901cea86fe2SHugh Dickins static int insert_page_in_batch_locked(struct vm_area_struct *vma, pte_t *pte, 19028cd3984dSArjun Roy unsigned long addr, struct page *page, pgprot_t prot) 19038cd3984dSArjun Roy { 19048cd3984dSArjun Roy int err; 19058cd3984dSArjun Roy 19068cd3984dSArjun Roy if (!page_count(page)) 19078cd3984dSArjun Roy return -EINVAL; 19088cd3984dSArjun Roy err = validate_page_before_insert(page); 19097f70c2a6SArjun Roy if (err) 19107f70c2a6SArjun Roy return err; 1911cea86fe2SHugh Dickins return insert_page_into_pte_locked(vma, pte, addr, page, prot); 19128cd3984dSArjun Roy } 19138cd3984dSArjun Roy 19148cd3984dSArjun Roy /* insert_pages() amortizes the cost of spinlock operations 1915bb7dbaafSMatthew Wilcox (Oracle) * when inserting pages in a loop. 19168cd3984dSArjun Roy */ 19178cd3984dSArjun Roy static int insert_pages(struct vm_area_struct *vma, unsigned long addr, 19188cd3984dSArjun Roy struct page **pages, unsigned long *num, pgprot_t prot) 19198cd3984dSArjun Roy { 19208cd3984dSArjun Roy pmd_t *pmd = NULL; 19217f70c2a6SArjun Roy pte_t *start_pte, *pte; 19227f70c2a6SArjun Roy spinlock_t *pte_lock; 19238cd3984dSArjun Roy struct mm_struct *const mm = vma->vm_mm; 19248cd3984dSArjun Roy unsigned long curr_page_idx = 0; 19258cd3984dSArjun Roy unsigned long remaining_pages_total = *num; 19268cd3984dSArjun Roy unsigned long pages_to_write_in_pmd; 19278cd3984dSArjun Roy int ret; 19288cd3984dSArjun Roy more: 19298cd3984dSArjun Roy ret = -EFAULT; 19308cd3984dSArjun Roy pmd = walk_to_pmd(mm, addr); 19318cd3984dSArjun Roy if (!pmd) 19328cd3984dSArjun Roy goto out; 19338cd3984dSArjun Roy 19348cd3984dSArjun Roy pages_to_write_in_pmd = min_t(unsigned long, 19358cd3984dSArjun Roy remaining_pages_total, PTRS_PER_PTE - pte_index(addr)); 19368cd3984dSArjun Roy 19378cd3984dSArjun Roy /* Allocate the PTE if necessary; takes PMD lock once only. */ 19388cd3984dSArjun Roy ret = -ENOMEM; 19398cd3984dSArjun Roy if (pte_alloc(mm, pmd)) 19408cd3984dSArjun Roy goto out; 19418cd3984dSArjun Roy 19428cd3984dSArjun Roy while (pages_to_write_in_pmd) { 19438cd3984dSArjun Roy int pte_idx = 0; 19448cd3984dSArjun Roy const int batch_size = min_t(int, pages_to_write_in_pmd, 8); 19458cd3984dSArjun Roy 19467f70c2a6SArjun Roy start_pte = pte_offset_map_lock(mm, pmd, addr, &pte_lock); 19473db82b93SHugh Dickins if (!start_pte) { 19483db82b93SHugh Dickins ret = -EFAULT; 19493db82b93SHugh Dickins goto out; 19503db82b93SHugh Dickins } 19517f70c2a6SArjun Roy for (pte = start_pte; pte_idx < batch_size; ++pte, ++pte_idx) { 1952cea86fe2SHugh Dickins int err = insert_page_in_batch_locked(vma, pte, 19538cd3984dSArjun Roy addr, pages[curr_page_idx], prot); 19548cd3984dSArjun Roy if (unlikely(err)) { 19557f70c2a6SArjun Roy pte_unmap_unlock(start_pte, pte_lock); 19568cd3984dSArjun Roy ret = err; 19578cd3984dSArjun Roy remaining_pages_total -= pte_idx; 19588cd3984dSArjun Roy goto out; 19598cd3984dSArjun Roy } 19608cd3984dSArjun Roy addr += PAGE_SIZE; 19618cd3984dSArjun Roy ++curr_page_idx; 19628cd3984dSArjun Roy } 19637f70c2a6SArjun Roy pte_unmap_unlock(start_pte, pte_lock); 19648cd3984dSArjun Roy pages_to_write_in_pmd -= batch_size; 19658cd3984dSArjun Roy remaining_pages_total -= batch_size; 19668cd3984dSArjun Roy } 19678cd3984dSArjun Roy if (remaining_pages_total) 19688cd3984dSArjun Roy goto more; 19698cd3984dSArjun Roy ret = 0; 19708cd3984dSArjun Roy out: 19718cd3984dSArjun Roy *num = remaining_pages_total; 19728cd3984dSArjun Roy return ret; 19738cd3984dSArjun Roy } 19748cd3984dSArjun Roy 19758cd3984dSArjun Roy /** 19768cd3984dSArjun Roy * vm_insert_pages - insert multiple pages into user vma, batching the pmd lock. 19778cd3984dSArjun Roy * @vma: user vma to map to 19788cd3984dSArjun Roy * @addr: target start user address of these pages 19798cd3984dSArjun Roy * @pages: source kernel pages 19808cd3984dSArjun Roy * @num: in: number of pages to map. out: number of pages that were *not* 19818cd3984dSArjun Roy * mapped. (0 means all pages were successfully mapped). 19828cd3984dSArjun Roy * 19838cd3984dSArjun Roy * Preferred over vm_insert_page() when inserting multiple pages. 19848cd3984dSArjun Roy * 19858cd3984dSArjun Roy * In case of error, we may have mapped a subset of the provided 19868cd3984dSArjun Roy * pages. It is the caller's responsibility to account for this case. 19878cd3984dSArjun Roy * 19888cd3984dSArjun Roy * The same restrictions apply as in vm_insert_page(). 19898cd3984dSArjun Roy */ 19908cd3984dSArjun Roy int vm_insert_pages(struct vm_area_struct *vma, unsigned long addr, 19918cd3984dSArjun Roy struct page **pages, unsigned long *num) 19928cd3984dSArjun Roy { 19938cd3984dSArjun Roy const unsigned long end_addr = addr + (*num * PAGE_SIZE) - 1; 19948cd3984dSArjun Roy 19958cd3984dSArjun Roy if (addr < vma->vm_start || end_addr >= vma->vm_end) 19968cd3984dSArjun Roy return -EFAULT; 19978cd3984dSArjun Roy if (!(vma->vm_flags & VM_MIXEDMAP)) { 1998d8ed45c5SMichel Lespinasse BUG_ON(mmap_read_trylock(vma->vm_mm)); 19998cd3984dSArjun Roy BUG_ON(vma->vm_flags & VM_PFNMAP); 20001c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_MIXEDMAP); 20018cd3984dSArjun Roy } 20028cd3984dSArjun Roy /* Defer page refcount checking till we're about to map that page. */ 20038cd3984dSArjun Roy return insert_pages(vma, addr, pages, num, vma->vm_page_prot); 20048cd3984dSArjun Roy } 20058cd3984dSArjun Roy EXPORT_SYMBOL(vm_insert_pages); 20068cd3984dSArjun Roy 2007bfa5bf6dSRolf Eike Beer /** 2008bfa5bf6dSRolf Eike Beer * vm_insert_page - insert single page into user vma 2009bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 2010bfa5bf6dSRolf Eike Beer * @addr: target user address of this page 2011bfa5bf6dSRolf Eike Beer * @page: source kernel page 2012bfa5bf6dSRolf Eike Beer * 2013a145dd41SLinus Torvalds * This allows drivers to insert individual pages they've allocated 2014a145dd41SLinus Torvalds * into a user vma. 2015a145dd41SLinus Torvalds * 2016a145dd41SLinus Torvalds * The page has to be a nice clean _individual_ kernel allocation. 2017a145dd41SLinus Torvalds * If you allocate a compound page, you need to have marked it as 2018a145dd41SLinus Torvalds * such (__GFP_COMP), or manually just split the page up yourself 20198dfcc9baSNick Piggin * (see split_page()). 2020a145dd41SLinus Torvalds * 2021a145dd41SLinus Torvalds * NOTE! Traditionally this was done with "remap_pfn_range()" which 2022a145dd41SLinus Torvalds * took an arbitrary page protection parameter. This doesn't allow 2023a145dd41SLinus Torvalds * that. Your vma protection will have to be set up correctly, which 2024a145dd41SLinus Torvalds * means that if you want a shared writable mapping, you'd better 2025a145dd41SLinus Torvalds * ask for a shared writable mapping! 2026a145dd41SLinus Torvalds * 2027a145dd41SLinus Torvalds * The page does not need to be reserved. 20284b6e1e37SKonstantin Khlebnikov * 20294b6e1e37SKonstantin Khlebnikov * Usually this function is called from f_op->mmap() handler 2030c1e8d7c6SMichel Lespinasse * under mm->mmap_lock write-lock, so it can change vma->vm_flags. 20314b6e1e37SKonstantin Khlebnikov * Caller must set VM_MIXEDMAP on vma if it wants to call this 20324b6e1e37SKonstantin Khlebnikov * function from other places, for example from page-fault handler. 2033a862f68aSMike Rapoport * 2034a862f68aSMike Rapoport * Return: %0 on success, negative error code otherwise. 2035a145dd41SLinus Torvalds */ 2036423bad60SNick Piggin int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, 2037423bad60SNick Piggin struct page *page) 2038a145dd41SLinus Torvalds { 2039a145dd41SLinus Torvalds if (addr < vma->vm_start || addr >= vma->vm_end) 2040a145dd41SLinus Torvalds return -EFAULT; 2041a145dd41SLinus Torvalds if (!page_count(page)) 2042a145dd41SLinus Torvalds return -EINVAL; 20434b6e1e37SKonstantin Khlebnikov if (!(vma->vm_flags & VM_MIXEDMAP)) { 2044d8ed45c5SMichel Lespinasse BUG_ON(mmap_read_trylock(vma->vm_mm)); 20454b6e1e37SKonstantin Khlebnikov BUG_ON(vma->vm_flags & VM_PFNMAP); 20461c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_MIXEDMAP); 20474b6e1e37SKonstantin Khlebnikov } 2048423bad60SNick Piggin return insert_page(vma, addr, page, vma->vm_page_prot); 2049a145dd41SLinus Torvalds } 2050e3c3374fSLinus Torvalds EXPORT_SYMBOL(vm_insert_page); 2051a145dd41SLinus Torvalds 2052a667d745SSouptick Joarder /* 2053a667d745SSouptick Joarder * __vm_map_pages - maps range of kernel pages into user vma 2054a667d745SSouptick Joarder * @vma: user vma to map to 2055a667d745SSouptick Joarder * @pages: pointer to array of source kernel pages 2056a667d745SSouptick Joarder * @num: number of pages in page array 2057a667d745SSouptick Joarder * @offset: user's requested vm_pgoff 2058a667d745SSouptick Joarder * 2059a667d745SSouptick Joarder * This allows drivers to map range of kernel pages into a user vma. 2060a667d745SSouptick Joarder * 2061a667d745SSouptick Joarder * Return: 0 on success and error code otherwise. 2062a667d745SSouptick Joarder */ 2063a667d745SSouptick Joarder static int __vm_map_pages(struct vm_area_struct *vma, struct page **pages, 2064a667d745SSouptick Joarder unsigned long num, unsigned long offset) 2065a667d745SSouptick Joarder { 2066a667d745SSouptick Joarder unsigned long count = vma_pages(vma); 2067a667d745SSouptick Joarder unsigned long uaddr = vma->vm_start; 2068a667d745SSouptick Joarder int ret, i; 2069a667d745SSouptick Joarder 2070a667d745SSouptick Joarder /* Fail if the user requested offset is beyond the end of the object */ 207196756fcbSMiguel Ojeda if (offset >= num) 2072a667d745SSouptick Joarder return -ENXIO; 2073a667d745SSouptick Joarder 2074a667d745SSouptick Joarder /* Fail if the user requested size exceeds available object size */ 2075a667d745SSouptick Joarder if (count > num - offset) 2076a667d745SSouptick Joarder return -ENXIO; 2077a667d745SSouptick Joarder 2078a667d745SSouptick Joarder for (i = 0; i < count; i++) { 2079a667d745SSouptick Joarder ret = vm_insert_page(vma, uaddr, pages[offset + i]); 2080a667d745SSouptick Joarder if (ret < 0) 2081a667d745SSouptick Joarder return ret; 2082a667d745SSouptick Joarder uaddr += PAGE_SIZE; 2083a667d745SSouptick Joarder } 2084a667d745SSouptick Joarder 2085a667d745SSouptick Joarder return 0; 2086a667d745SSouptick Joarder } 2087a667d745SSouptick Joarder 2088a667d745SSouptick Joarder /** 2089a667d745SSouptick Joarder * vm_map_pages - maps range of kernel pages starts with non zero offset 2090a667d745SSouptick Joarder * @vma: user vma to map to 2091a667d745SSouptick Joarder * @pages: pointer to array of source kernel pages 2092a667d745SSouptick Joarder * @num: number of pages in page array 2093a667d745SSouptick Joarder * 2094a667d745SSouptick Joarder * Maps an object consisting of @num pages, catering for the user's 2095a667d745SSouptick Joarder * requested vm_pgoff 2096a667d745SSouptick Joarder * 2097a667d745SSouptick Joarder * If we fail to insert any page into the vma, the function will return 2098a667d745SSouptick Joarder * immediately leaving any previously inserted pages present. Callers 2099a667d745SSouptick Joarder * from the mmap handler may immediately return the error as their caller 2100a667d745SSouptick Joarder * will destroy the vma, removing any successfully inserted pages. Other 2101a667d745SSouptick Joarder * callers should make their own arrangements for calling unmap_region(). 2102a667d745SSouptick Joarder * 2103a667d745SSouptick Joarder * Context: Process context. Called by mmap handlers. 2104a667d745SSouptick Joarder * Return: 0 on success and error code otherwise. 2105a667d745SSouptick Joarder */ 2106a667d745SSouptick Joarder int vm_map_pages(struct vm_area_struct *vma, struct page **pages, 2107a667d745SSouptick Joarder unsigned long num) 2108a667d745SSouptick Joarder { 2109a667d745SSouptick Joarder return __vm_map_pages(vma, pages, num, vma->vm_pgoff); 2110a667d745SSouptick Joarder } 2111a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages); 2112a667d745SSouptick Joarder 2113a667d745SSouptick Joarder /** 2114a667d745SSouptick Joarder * vm_map_pages_zero - map range of kernel pages starts with zero offset 2115a667d745SSouptick Joarder * @vma: user vma to map to 2116a667d745SSouptick Joarder * @pages: pointer to array of source kernel pages 2117a667d745SSouptick Joarder * @num: number of pages in page array 2118a667d745SSouptick Joarder * 2119a667d745SSouptick Joarder * Similar to vm_map_pages(), except that it explicitly sets the offset 2120a667d745SSouptick Joarder * to 0. This function is intended for the drivers that did not consider 2121a667d745SSouptick Joarder * vm_pgoff. 2122a667d745SSouptick Joarder * 2123a667d745SSouptick Joarder * Context: Process context. Called by mmap handlers. 2124a667d745SSouptick Joarder * Return: 0 on success and error code otherwise. 2125a667d745SSouptick Joarder */ 2126a667d745SSouptick Joarder int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages, 2127a667d745SSouptick Joarder unsigned long num) 2128a667d745SSouptick Joarder { 2129a667d745SSouptick Joarder return __vm_map_pages(vma, pages, num, 0); 2130a667d745SSouptick Joarder } 2131a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages_zero); 2132a667d745SSouptick Joarder 21339b5a8e00SMatthew Wilcox static vm_fault_t insert_pfn(struct vm_area_struct *vma, unsigned long addr, 2134b2770da6SRoss Zwisler pfn_t pfn, pgprot_t prot, bool mkwrite) 2135423bad60SNick Piggin { 2136423bad60SNick Piggin struct mm_struct *mm = vma->vm_mm; 2137423bad60SNick Piggin pte_t *pte, entry; 2138423bad60SNick Piggin spinlock_t *ptl; 2139423bad60SNick Piggin 2140423bad60SNick Piggin pte = get_locked_pte(mm, addr, &ptl); 2141423bad60SNick Piggin if (!pte) 21429b5a8e00SMatthew Wilcox return VM_FAULT_OOM; 2143c33c7948SRyan Roberts entry = ptep_get(pte); 2144c33c7948SRyan Roberts if (!pte_none(entry)) { 2145b2770da6SRoss Zwisler if (mkwrite) { 2146b2770da6SRoss Zwisler /* 2147b2770da6SRoss Zwisler * For read faults on private mappings the PFN passed 2148b2770da6SRoss Zwisler * in may not match the PFN we have mapped if the 2149b2770da6SRoss Zwisler * mapped PFN is a writeable COW page. In the mkwrite 2150b2770da6SRoss Zwisler * case we are creating a writable PTE for a shared 2151f2c57d91SJan Kara * mapping and we expect the PFNs to match. If they 2152f2c57d91SJan Kara * don't match, we are likely racing with block 2153f2c57d91SJan Kara * allocation and mapping invalidation so just skip the 2154f2c57d91SJan Kara * update. 2155b2770da6SRoss Zwisler */ 2156c33c7948SRyan Roberts if (pte_pfn(entry) != pfn_t_to_pfn(pfn)) { 2157c33c7948SRyan Roberts WARN_ON_ONCE(!is_zero_pfn(pte_pfn(entry))); 2158423bad60SNick Piggin goto out_unlock; 2159f2c57d91SJan Kara } 2160c33c7948SRyan Roberts entry = pte_mkyoung(entry); 2161cae85cb8SJan Kara entry = maybe_mkwrite(pte_mkdirty(entry), vma); 2162cae85cb8SJan Kara if (ptep_set_access_flags(vma, addr, pte, entry, 1)) 2163cae85cb8SJan Kara update_mmu_cache(vma, addr, pte); 2164cae85cb8SJan Kara } 2165b2770da6SRoss Zwisler goto out_unlock; 2166b2770da6SRoss Zwisler } 2167423bad60SNick Piggin 2168423bad60SNick Piggin /* Ok, finally just insert the thing.. */ 216901c8f1c4SDan Williams if (pfn_t_devmap(pfn)) 217001c8f1c4SDan Williams entry = pte_mkdevmap(pfn_t_pte(pfn, prot)); 217101c8f1c4SDan Williams else 217201c8f1c4SDan Williams entry = pte_mkspecial(pfn_t_pte(pfn, prot)); 2173b2770da6SRoss Zwisler 2174b2770da6SRoss Zwisler if (mkwrite) { 2175b2770da6SRoss Zwisler entry = pte_mkyoung(entry); 2176b2770da6SRoss Zwisler entry = maybe_mkwrite(pte_mkdirty(entry), vma); 2177b2770da6SRoss Zwisler } 2178b2770da6SRoss Zwisler 2179423bad60SNick Piggin set_pte_at(mm, addr, pte, entry); 21804b3073e1SRussell King update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */ 2181423bad60SNick Piggin 2182423bad60SNick Piggin out_unlock: 2183423bad60SNick Piggin pte_unmap_unlock(pte, ptl); 21849b5a8e00SMatthew Wilcox return VM_FAULT_NOPAGE; 2185423bad60SNick Piggin } 2186423bad60SNick Piggin 2187f5e6d1d5SMatthew Wilcox /** 2188f5e6d1d5SMatthew Wilcox * vmf_insert_pfn_prot - insert single pfn into user vma with specified pgprot 2189f5e6d1d5SMatthew Wilcox * @vma: user vma to map to 2190f5e6d1d5SMatthew Wilcox * @addr: target user address of this page 2191f5e6d1d5SMatthew Wilcox * @pfn: source kernel pfn 2192f5e6d1d5SMatthew Wilcox * @pgprot: pgprot flags for the inserted page 2193f5e6d1d5SMatthew Wilcox * 2194a1a0aea5SRandy Dunlap * This is exactly like vmf_insert_pfn(), except that it allows drivers 2195f5e6d1d5SMatthew Wilcox * to override pgprot on a per-page basis. 2196f5e6d1d5SMatthew Wilcox * 2197f5e6d1d5SMatthew Wilcox * This only makes sense for IO mappings, and it makes no sense for 2198f5e6d1d5SMatthew Wilcox * COW mappings. In general, using multiple vmas is preferable; 2199ae2b01f3SMatthew Wilcox * vmf_insert_pfn_prot should only be used if using multiple VMAs is 2200f5e6d1d5SMatthew Wilcox * impractical. 2201f5e6d1d5SMatthew Wilcox * 220228d8b812SLorenzo Stoakes * pgprot typically only differs from @vma->vm_page_prot when drivers set 220328d8b812SLorenzo Stoakes * caching- and encryption bits different than those of @vma->vm_page_prot, 220428d8b812SLorenzo Stoakes * because the caching- or encryption mode may not be known at mmap() time. 220528d8b812SLorenzo Stoakes * 220628d8b812SLorenzo Stoakes * This is ok as long as @vma->vm_page_prot is not used by the core vm 220728d8b812SLorenzo Stoakes * to set caching and encryption bits for those vmas (except for COW pages). 220828d8b812SLorenzo Stoakes * This is ensured by core vm only modifying these page table entries using 220928d8b812SLorenzo Stoakes * functions that don't touch caching- or encryption bits, using pte_modify() 221028d8b812SLorenzo Stoakes * if needed. (See for example mprotect()). 221128d8b812SLorenzo Stoakes * 221228d8b812SLorenzo Stoakes * Also when new page-table entries are created, this is only done using the 221328d8b812SLorenzo Stoakes * fault() callback, and never using the value of vma->vm_page_prot, 221428d8b812SLorenzo Stoakes * except for page-table entries that point to anonymous pages as the result 221528d8b812SLorenzo Stoakes * of COW. 2216574c5b3dSThomas Hellstrom * 2217ae2b01f3SMatthew Wilcox * Context: Process context. May allocate using %GFP_KERNEL. 2218f5e6d1d5SMatthew Wilcox * Return: vm_fault_t value. 2219f5e6d1d5SMatthew Wilcox */ 2220f5e6d1d5SMatthew Wilcox vm_fault_t vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr, 2221f5e6d1d5SMatthew Wilcox unsigned long pfn, pgprot_t pgprot) 2222f5e6d1d5SMatthew Wilcox { 22236d958546SMatthew Wilcox /* 22246d958546SMatthew Wilcox * Technically, architectures with pte_special can avoid all these 22256d958546SMatthew Wilcox * restrictions (same for remap_pfn_range). However we would like 22266d958546SMatthew Wilcox * consistency in testing and feature parity among all, so we should 22276d958546SMatthew Wilcox * try to keep these invariants in place for everybody. 22286d958546SMatthew Wilcox */ 22296d958546SMatthew Wilcox BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))); 22306d958546SMatthew Wilcox BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) == 22316d958546SMatthew Wilcox (VM_PFNMAP|VM_MIXEDMAP)); 22326d958546SMatthew Wilcox BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); 22336d958546SMatthew Wilcox BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn)); 22346d958546SMatthew Wilcox 22356d958546SMatthew Wilcox if (addr < vma->vm_start || addr >= vma->vm_end) 22366d958546SMatthew Wilcox return VM_FAULT_SIGBUS; 22376d958546SMatthew Wilcox 22386d958546SMatthew Wilcox if (!pfn_modify_allowed(pfn, pgprot)) 22396d958546SMatthew Wilcox return VM_FAULT_SIGBUS; 22406d958546SMatthew Wilcox 22416d958546SMatthew Wilcox track_pfn_insert(vma, &pgprot, __pfn_to_pfn_t(pfn, PFN_DEV)); 22426d958546SMatthew Wilcox 22439b5a8e00SMatthew Wilcox return insert_pfn(vma, addr, __pfn_to_pfn_t(pfn, PFN_DEV), pgprot, 22446d958546SMatthew Wilcox false); 2245f5e6d1d5SMatthew Wilcox } 2246f5e6d1d5SMatthew Wilcox EXPORT_SYMBOL(vmf_insert_pfn_prot); 2247e0dc0d8fSNick Piggin 2248ae2b01f3SMatthew Wilcox /** 2249ae2b01f3SMatthew Wilcox * vmf_insert_pfn - insert single pfn into user vma 2250ae2b01f3SMatthew Wilcox * @vma: user vma to map to 2251ae2b01f3SMatthew Wilcox * @addr: target user address of this page 2252ae2b01f3SMatthew Wilcox * @pfn: source kernel pfn 2253ae2b01f3SMatthew Wilcox * 2254ae2b01f3SMatthew Wilcox * Similar to vm_insert_page, this allows drivers to insert individual pages 2255ae2b01f3SMatthew Wilcox * they've allocated into a user vma. Same comments apply. 2256ae2b01f3SMatthew Wilcox * 2257ae2b01f3SMatthew Wilcox * This function should only be called from a vm_ops->fault handler, and 2258ae2b01f3SMatthew Wilcox * in that case the handler should return the result of this function. 2259ae2b01f3SMatthew Wilcox * 2260ae2b01f3SMatthew Wilcox * vma cannot be a COW mapping. 2261ae2b01f3SMatthew Wilcox * 2262ae2b01f3SMatthew Wilcox * As this is called only for pages that do not currently exist, we 2263ae2b01f3SMatthew Wilcox * do not need to flush old virtual caches or the TLB. 2264ae2b01f3SMatthew Wilcox * 2265ae2b01f3SMatthew Wilcox * Context: Process context. May allocate using %GFP_KERNEL. 2266ae2b01f3SMatthew Wilcox * Return: vm_fault_t value. 2267ae2b01f3SMatthew Wilcox */ 2268ae2b01f3SMatthew Wilcox vm_fault_t vmf_insert_pfn(struct vm_area_struct *vma, unsigned long addr, 2269ae2b01f3SMatthew Wilcox unsigned long pfn) 2270ae2b01f3SMatthew Wilcox { 2271ae2b01f3SMatthew Wilcox return vmf_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot); 2272ae2b01f3SMatthew Wilcox } 2273ae2b01f3SMatthew Wilcox EXPORT_SYMBOL(vmf_insert_pfn); 2274ae2b01f3SMatthew Wilcox 2275785a3fabSDan Williams static bool vm_mixed_ok(struct vm_area_struct *vma, pfn_t pfn) 2276785a3fabSDan Williams { 2277785a3fabSDan Williams /* these checks mirror the abort conditions in vm_normal_page */ 2278785a3fabSDan Williams if (vma->vm_flags & VM_MIXEDMAP) 2279785a3fabSDan Williams return true; 2280785a3fabSDan Williams if (pfn_t_devmap(pfn)) 2281785a3fabSDan Williams return true; 2282785a3fabSDan Williams if (pfn_t_special(pfn)) 2283785a3fabSDan Williams return true; 2284785a3fabSDan Williams if (is_zero_pfn(pfn_t_to_pfn(pfn))) 2285785a3fabSDan Williams return true; 2286785a3fabSDan Williams return false; 2287785a3fabSDan Williams } 2288785a3fabSDan Williams 228979f3aa5bSMatthew Wilcox static vm_fault_t __vm_insert_mixed(struct vm_area_struct *vma, 229028d8b812SLorenzo Stoakes unsigned long addr, pfn_t pfn, bool mkwrite) 2291423bad60SNick Piggin { 229228d8b812SLorenzo Stoakes pgprot_t pgprot = vma->vm_page_prot; 229379f3aa5bSMatthew Wilcox int err; 229487744ab3SDan Williams 2295785a3fabSDan Williams BUG_ON(!vm_mixed_ok(vma, pfn)); 2296423bad60SNick Piggin 2297423bad60SNick Piggin if (addr < vma->vm_start || addr >= vma->vm_end) 229879f3aa5bSMatthew Wilcox return VM_FAULT_SIGBUS; 2299308a047cSBorislav Petkov 2300308a047cSBorislav Petkov track_pfn_insert(vma, &pgprot, pfn); 2301423bad60SNick Piggin 230242e4089cSAndi Kleen if (!pfn_modify_allowed(pfn_t_to_pfn(pfn), pgprot)) 230379f3aa5bSMatthew Wilcox return VM_FAULT_SIGBUS; 230442e4089cSAndi Kleen 2305423bad60SNick Piggin /* 2306423bad60SNick Piggin * If we don't have pte special, then we have to use the pfn_valid() 2307423bad60SNick Piggin * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must* 2308423bad60SNick Piggin * refcount the page if pfn_valid is true (hence insert_page rather 230962eede62SHugh Dickins * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP 231062eede62SHugh Dickins * without pte special, it would there be refcounted as a normal page. 2311423bad60SNick Piggin */ 231200b3a331SLaurent Dufour if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) && 231300b3a331SLaurent Dufour !pfn_t_devmap(pfn) && pfn_t_valid(pfn)) { 2314423bad60SNick Piggin struct page *page; 2315423bad60SNick Piggin 231603fc2da6SDan Williams /* 231703fc2da6SDan Williams * At this point we are committed to insert_page() 231803fc2da6SDan Williams * regardless of whether the caller specified flags that 231903fc2da6SDan Williams * result in pfn_t_has_page() == false. 232003fc2da6SDan Williams */ 232103fc2da6SDan Williams page = pfn_to_page(pfn_t_to_pfn(pfn)); 232279f3aa5bSMatthew Wilcox err = insert_page(vma, addr, page, pgprot); 232379f3aa5bSMatthew Wilcox } else { 23249b5a8e00SMatthew Wilcox return insert_pfn(vma, addr, pfn, pgprot, mkwrite); 2325423bad60SNick Piggin } 2326b2770da6SRoss Zwisler 23275d747637SMatthew Wilcox if (err == -ENOMEM) 23285d747637SMatthew Wilcox return VM_FAULT_OOM; 23295d747637SMatthew Wilcox if (err < 0 && err != -EBUSY) 23305d747637SMatthew Wilcox return VM_FAULT_SIGBUS; 23315d747637SMatthew Wilcox 23325d747637SMatthew Wilcox return VM_FAULT_NOPAGE; 2333423bad60SNick Piggin } 233479f3aa5bSMatthew Wilcox 233579f3aa5bSMatthew Wilcox vm_fault_t vmf_insert_mixed(struct vm_area_struct *vma, unsigned long addr, 233679f3aa5bSMatthew Wilcox pfn_t pfn) 233779f3aa5bSMatthew Wilcox { 233828d8b812SLorenzo Stoakes return __vm_insert_mixed(vma, addr, pfn, false); 233979f3aa5bSMatthew Wilcox } 23405d747637SMatthew Wilcox EXPORT_SYMBOL(vmf_insert_mixed); 2341423bad60SNick Piggin 2342ab77dab4SSouptick Joarder /* 2343ab77dab4SSouptick Joarder * If the insertion of PTE failed because someone else already added a 2344ab77dab4SSouptick Joarder * different entry in the mean time, we treat that as success as we assume 2345ab77dab4SSouptick Joarder * the same entry was actually inserted. 2346ab77dab4SSouptick Joarder */ 2347ab77dab4SSouptick Joarder vm_fault_t vmf_insert_mixed_mkwrite(struct vm_area_struct *vma, 2348ab77dab4SSouptick Joarder unsigned long addr, pfn_t pfn) 2349b2770da6SRoss Zwisler { 235028d8b812SLorenzo Stoakes return __vm_insert_mixed(vma, addr, pfn, true); 2351b2770da6SRoss Zwisler } 2352ab77dab4SSouptick Joarder EXPORT_SYMBOL(vmf_insert_mixed_mkwrite); 2353b2770da6SRoss Zwisler 2354a145dd41SLinus Torvalds /* 23551da177e4SLinus Torvalds * maps a range of physical memory into the requested pages. the old 23561da177e4SLinus Torvalds * mappings are removed. any references to nonexistent pages results 23571da177e4SLinus Torvalds * in null mappings (currently treated as "copy-on-access") 23581da177e4SLinus Torvalds */ 23591da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd, 23601da177e4SLinus Torvalds unsigned long addr, unsigned long end, 23611da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 23621da177e4SLinus Torvalds { 236390a3e375SMiaohe Lin pte_t *pte, *mapped_pte; 2364c74df32cSHugh Dickins spinlock_t *ptl; 236542e4089cSAndi Kleen int err = 0; 23661da177e4SLinus Torvalds 236790a3e375SMiaohe Lin mapped_pte = pte = pte_alloc_map_lock(mm, pmd, addr, &ptl); 23681da177e4SLinus Torvalds if (!pte) 23691da177e4SLinus Torvalds return -ENOMEM; 23706606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 23711da177e4SLinus Torvalds do { 2372c33c7948SRyan Roberts BUG_ON(!pte_none(ptep_get(pte))); 237342e4089cSAndi Kleen if (!pfn_modify_allowed(pfn, prot)) { 237442e4089cSAndi Kleen err = -EACCES; 237542e4089cSAndi Kleen break; 237642e4089cSAndi Kleen } 23777e675137SNick Piggin set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot))); 23781da177e4SLinus Torvalds pfn++; 23791da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 23806606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 238190a3e375SMiaohe Lin pte_unmap_unlock(mapped_pte, ptl); 238242e4089cSAndi Kleen return err; 23831da177e4SLinus Torvalds } 23841da177e4SLinus Torvalds 23851da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud, 23861da177e4SLinus Torvalds unsigned long addr, unsigned long end, 23871da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 23881da177e4SLinus Torvalds { 23891da177e4SLinus Torvalds pmd_t *pmd; 23901da177e4SLinus Torvalds unsigned long next; 239142e4089cSAndi Kleen int err; 23921da177e4SLinus Torvalds 23931da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 23941da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, addr); 23951da177e4SLinus Torvalds if (!pmd) 23961da177e4SLinus Torvalds return -ENOMEM; 2397f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 23981da177e4SLinus Torvalds do { 23991da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 240042e4089cSAndi Kleen err = remap_pte_range(mm, pmd, addr, next, 240142e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 240242e4089cSAndi Kleen if (err) 240342e4089cSAndi Kleen return err; 24041da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 24051da177e4SLinus Torvalds return 0; 24061da177e4SLinus Torvalds } 24071da177e4SLinus Torvalds 2408c2febafcSKirill A. Shutemov static inline int remap_pud_range(struct mm_struct *mm, p4d_t *p4d, 24091da177e4SLinus Torvalds unsigned long addr, unsigned long end, 24101da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 24111da177e4SLinus Torvalds { 24121da177e4SLinus Torvalds pud_t *pud; 24131da177e4SLinus Torvalds unsigned long next; 241442e4089cSAndi Kleen int err; 24151da177e4SLinus Torvalds 24161da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 2417c2febafcSKirill A. Shutemov pud = pud_alloc(mm, p4d, addr); 24181da177e4SLinus Torvalds if (!pud) 24191da177e4SLinus Torvalds return -ENOMEM; 24201da177e4SLinus Torvalds do { 24211da177e4SLinus Torvalds next = pud_addr_end(addr, end); 242242e4089cSAndi Kleen err = remap_pmd_range(mm, pud, addr, next, 242342e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 242442e4089cSAndi Kleen if (err) 242542e4089cSAndi Kleen return err; 24261da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 24271da177e4SLinus Torvalds return 0; 24281da177e4SLinus Torvalds } 24291da177e4SLinus Torvalds 2430c2febafcSKirill A. Shutemov static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd, 2431c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 2432c2febafcSKirill A. Shutemov unsigned long pfn, pgprot_t prot) 2433c2febafcSKirill A. Shutemov { 2434c2febafcSKirill A. Shutemov p4d_t *p4d; 2435c2febafcSKirill A. Shutemov unsigned long next; 243642e4089cSAndi Kleen int err; 2437c2febafcSKirill A. Shutemov 2438c2febafcSKirill A. Shutemov pfn -= addr >> PAGE_SHIFT; 2439c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, addr); 2440c2febafcSKirill A. Shutemov if (!p4d) 2441c2febafcSKirill A. Shutemov return -ENOMEM; 2442c2febafcSKirill A. Shutemov do { 2443c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 244442e4089cSAndi Kleen err = remap_pud_range(mm, p4d, addr, next, 244542e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 244642e4089cSAndi Kleen if (err) 244742e4089cSAndi Kleen return err; 2448c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 2449c2febafcSKirill A. Shutemov return 0; 2450c2febafcSKirill A. Shutemov } 2451c2febafcSKirill A. Shutemov 245274ffa5a3SChristoph Hellwig /* 245374ffa5a3SChristoph Hellwig * Variant of remap_pfn_range that does not call track_pfn_remap. The caller 245474ffa5a3SChristoph Hellwig * must have pre-validated the caching bits of the pgprot_t. 2455bfa5bf6dSRolf Eike Beer */ 245674ffa5a3SChristoph Hellwig int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr, 24571da177e4SLinus Torvalds unsigned long pfn, unsigned long size, pgprot_t prot) 24581da177e4SLinus Torvalds { 24591da177e4SLinus Torvalds pgd_t *pgd; 24601da177e4SLinus Torvalds unsigned long next; 24612d15cab8SHugh Dickins unsigned long end = addr + PAGE_ALIGN(size); 24621da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 24631da177e4SLinus Torvalds int err; 24641da177e4SLinus Torvalds 24650c4123e3SAlex Zhang if (WARN_ON_ONCE(!PAGE_ALIGNED(addr))) 24660c4123e3SAlex Zhang return -EINVAL; 24670c4123e3SAlex Zhang 24681da177e4SLinus Torvalds /* 24691da177e4SLinus Torvalds * Physically remapped pages are special. Tell the 24701da177e4SLinus Torvalds * rest of the world about it: 24711da177e4SLinus Torvalds * VM_IO tells people not to look at these pages 24721da177e4SLinus Torvalds * (accesses can have side effects). 24736aab341eSLinus Torvalds * VM_PFNMAP tells the core MM that the base pages are just 24746aab341eSLinus Torvalds * raw PFN mappings, and do not have a "struct page" associated 24756aab341eSLinus Torvalds * with them. 2476314e51b9SKonstantin Khlebnikov * VM_DONTEXPAND 2477314e51b9SKonstantin Khlebnikov * Disable vma merging and expanding with mremap(). 2478314e51b9SKonstantin Khlebnikov * VM_DONTDUMP 2479314e51b9SKonstantin Khlebnikov * Omit vma from core dump, even when VM_IO turned off. 2480fb155c16SLinus Torvalds * 2481fb155c16SLinus Torvalds * There's a horrible special case to handle copy-on-write 2482fb155c16SLinus Torvalds * behaviour that some programs depend on. We mark the "original" 2483fb155c16SLinus Torvalds * un-COW'ed pages by matching them up with "vma->vm_pgoff". 2484b3b9c293SKonstantin Khlebnikov * See vm_normal_page() for details. 24851da177e4SLinus Torvalds */ 2486b3b9c293SKonstantin Khlebnikov if (is_cow_mapping(vma->vm_flags)) { 2487b3b9c293SKonstantin Khlebnikov if (addr != vma->vm_start || end != vma->vm_end) 2488b3b9c293SKonstantin Khlebnikov return -EINVAL; 24896aab341eSLinus Torvalds vma->vm_pgoff = pfn; 2490b3b9c293SKonstantin Khlebnikov } 2491b3b9c293SKonstantin Khlebnikov 24921c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP); 24931da177e4SLinus Torvalds 24941da177e4SLinus Torvalds BUG_ON(addr >= end); 24951da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 24961da177e4SLinus Torvalds pgd = pgd_offset(mm, addr); 24971da177e4SLinus Torvalds flush_cache_range(vma, addr, end); 24981da177e4SLinus Torvalds do { 24991da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 2500c2febafcSKirill A. Shutemov err = remap_p4d_range(mm, pgd, addr, next, 25011da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot); 25021da177e4SLinus Torvalds if (err) 250374ffa5a3SChristoph Hellwig return err; 25041da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 25052ab64037Svenkatesh.pallipadi@intel.com 250674ffa5a3SChristoph Hellwig return 0; 250774ffa5a3SChristoph Hellwig } 25082ab64037Svenkatesh.pallipadi@intel.com 250974ffa5a3SChristoph Hellwig /** 251074ffa5a3SChristoph Hellwig * remap_pfn_range - remap kernel memory to userspace 251174ffa5a3SChristoph Hellwig * @vma: user vma to map to 251274ffa5a3SChristoph Hellwig * @addr: target page aligned user address to start at 251374ffa5a3SChristoph Hellwig * @pfn: page frame number of kernel physical memory address 251474ffa5a3SChristoph Hellwig * @size: size of mapping area 251574ffa5a3SChristoph Hellwig * @prot: page protection flags for this mapping 251674ffa5a3SChristoph Hellwig * 251774ffa5a3SChristoph Hellwig * Note: this is only safe if the mm semaphore is held when called. 251874ffa5a3SChristoph Hellwig * 251974ffa5a3SChristoph Hellwig * Return: %0 on success, negative error code otherwise. 252074ffa5a3SChristoph Hellwig */ 252174ffa5a3SChristoph Hellwig int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 252274ffa5a3SChristoph Hellwig unsigned long pfn, unsigned long size, pgprot_t prot) 252374ffa5a3SChristoph Hellwig { 252474ffa5a3SChristoph Hellwig int err; 252574ffa5a3SChristoph Hellwig 252674ffa5a3SChristoph Hellwig err = track_pfn_remap(vma, &prot, pfn, addr, PAGE_ALIGN(size)); 252774ffa5a3SChristoph Hellwig if (err) 252874ffa5a3SChristoph Hellwig return -EINVAL; 252974ffa5a3SChristoph Hellwig 253074ffa5a3SChristoph Hellwig err = remap_pfn_range_notrack(vma, addr, pfn, size, prot); 253174ffa5a3SChristoph Hellwig if (err) 253268f48381SSuren Baghdasaryan untrack_pfn(vma, pfn, PAGE_ALIGN(size), true); 25331da177e4SLinus Torvalds return err; 25341da177e4SLinus Torvalds } 25351da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range); 25361da177e4SLinus Torvalds 2537b4cbb197SLinus Torvalds /** 2538b4cbb197SLinus Torvalds * vm_iomap_memory - remap memory to userspace 2539b4cbb197SLinus Torvalds * @vma: user vma to map to 2540abd69b9eSWang Wenhu * @start: start of the physical memory to be mapped 2541b4cbb197SLinus Torvalds * @len: size of area 2542b4cbb197SLinus Torvalds * 2543b4cbb197SLinus Torvalds * This is a simplified io_remap_pfn_range() for common driver use. The 2544b4cbb197SLinus Torvalds * driver just needs to give us the physical memory range to be mapped, 2545b4cbb197SLinus Torvalds * we'll figure out the rest from the vma information. 2546b4cbb197SLinus Torvalds * 2547b4cbb197SLinus Torvalds * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get 2548b4cbb197SLinus Torvalds * whatever write-combining details or similar. 2549a862f68aSMike Rapoport * 2550a862f68aSMike Rapoport * Return: %0 on success, negative error code otherwise. 2551b4cbb197SLinus Torvalds */ 2552b4cbb197SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len) 2553b4cbb197SLinus Torvalds { 2554b4cbb197SLinus Torvalds unsigned long vm_len, pfn, pages; 2555b4cbb197SLinus Torvalds 2556b4cbb197SLinus Torvalds /* Check that the physical memory area passed in looks valid */ 2557b4cbb197SLinus Torvalds if (start + len < start) 2558b4cbb197SLinus Torvalds return -EINVAL; 2559b4cbb197SLinus Torvalds /* 2560b4cbb197SLinus Torvalds * You *really* shouldn't map things that aren't page-aligned, 2561b4cbb197SLinus Torvalds * but we've historically allowed it because IO memory might 2562b4cbb197SLinus Torvalds * just have smaller alignment. 2563b4cbb197SLinus Torvalds */ 2564b4cbb197SLinus Torvalds len += start & ~PAGE_MASK; 2565b4cbb197SLinus Torvalds pfn = start >> PAGE_SHIFT; 2566b4cbb197SLinus Torvalds pages = (len + ~PAGE_MASK) >> PAGE_SHIFT; 2567b4cbb197SLinus Torvalds if (pfn + pages < pfn) 2568b4cbb197SLinus Torvalds return -EINVAL; 2569b4cbb197SLinus Torvalds 2570b4cbb197SLinus Torvalds /* We start the mapping 'vm_pgoff' pages into the area */ 2571b4cbb197SLinus Torvalds if (vma->vm_pgoff > pages) 2572b4cbb197SLinus Torvalds return -EINVAL; 2573b4cbb197SLinus Torvalds pfn += vma->vm_pgoff; 2574b4cbb197SLinus Torvalds pages -= vma->vm_pgoff; 2575b4cbb197SLinus Torvalds 2576b4cbb197SLinus Torvalds /* Can we fit all of the mapping? */ 2577b4cbb197SLinus Torvalds vm_len = vma->vm_end - vma->vm_start; 2578b4cbb197SLinus Torvalds if (vm_len >> PAGE_SHIFT > pages) 2579b4cbb197SLinus Torvalds return -EINVAL; 2580b4cbb197SLinus Torvalds 2581b4cbb197SLinus Torvalds /* Ok, let it rip */ 2582b4cbb197SLinus Torvalds return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot); 2583b4cbb197SLinus Torvalds } 2584b4cbb197SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory); 2585b4cbb197SLinus Torvalds 2586aee16b3cSJeremy Fitzhardinge static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd, 2587aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2588e80d3909SJoerg Roedel pte_fn_t fn, void *data, bool create, 2589e80d3909SJoerg Roedel pgtbl_mod_mask *mask) 2590aee16b3cSJeremy Fitzhardinge { 25918abb50c7SMiaohe Lin pte_t *pte, *mapped_pte; 2592be1db475SDaniel Axtens int err = 0; 25933f649ab7SKees Cook spinlock_t *ptl; 2594aee16b3cSJeremy Fitzhardinge 2595be1db475SDaniel Axtens if (create) { 25968abb50c7SMiaohe Lin mapped_pte = pte = (mm == &init_mm) ? 2597e80d3909SJoerg Roedel pte_alloc_kernel_track(pmd, addr, mask) : 2598aee16b3cSJeremy Fitzhardinge pte_alloc_map_lock(mm, pmd, addr, &ptl); 2599aee16b3cSJeremy Fitzhardinge if (!pte) 2600aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2601be1db475SDaniel Axtens } else { 26028abb50c7SMiaohe Lin mapped_pte = pte = (mm == &init_mm) ? 2603be1db475SDaniel Axtens pte_offset_kernel(pmd, addr) : 2604be1db475SDaniel Axtens pte_offset_map_lock(mm, pmd, addr, &ptl); 26053db82b93SHugh Dickins if (!pte) 26063db82b93SHugh Dickins return -EINVAL; 2607be1db475SDaniel Axtens } 2608aee16b3cSJeremy Fitzhardinge 260938e0edb1SJeremy Fitzhardinge arch_enter_lazy_mmu_mode(); 261038e0edb1SJeremy Fitzhardinge 2611eeb4a05fSChristoph Hellwig if (fn) { 2612aee16b3cSJeremy Fitzhardinge do { 2613c33c7948SRyan Roberts if (create || !pte_none(ptep_get(pte))) { 26148b1e0f81SAnshuman Khandual err = fn(pte++, addr, data); 2615aee16b3cSJeremy Fitzhardinge if (err) 2616aee16b3cSJeremy Fitzhardinge break; 2617be1db475SDaniel Axtens } 2618c36987e2SDaisuke Nishimura } while (addr += PAGE_SIZE, addr != end); 2619eeb4a05fSChristoph Hellwig } 2620e80d3909SJoerg Roedel *mask |= PGTBL_PTE_MODIFIED; 2621aee16b3cSJeremy Fitzhardinge 262238e0edb1SJeremy Fitzhardinge arch_leave_lazy_mmu_mode(); 262338e0edb1SJeremy Fitzhardinge 2624aee16b3cSJeremy Fitzhardinge if (mm != &init_mm) 26258abb50c7SMiaohe Lin pte_unmap_unlock(mapped_pte, ptl); 2626aee16b3cSJeremy Fitzhardinge return err; 2627aee16b3cSJeremy Fitzhardinge } 2628aee16b3cSJeremy Fitzhardinge 2629aee16b3cSJeremy Fitzhardinge static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud, 2630aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2631e80d3909SJoerg Roedel pte_fn_t fn, void *data, bool create, 2632e80d3909SJoerg Roedel pgtbl_mod_mask *mask) 2633aee16b3cSJeremy Fitzhardinge { 2634aee16b3cSJeremy Fitzhardinge pmd_t *pmd; 2635aee16b3cSJeremy Fitzhardinge unsigned long next; 2636be1db475SDaniel Axtens int err = 0; 2637aee16b3cSJeremy Fitzhardinge 2638ceb86879SAndi Kleen BUG_ON(pud_huge(*pud)); 2639ceb86879SAndi Kleen 2640be1db475SDaniel Axtens if (create) { 2641e80d3909SJoerg Roedel pmd = pmd_alloc_track(mm, pud, addr, mask); 2642aee16b3cSJeremy Fitzhardinge if (!pmd) 2643aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2644be1db475SDaniel Axtens } else { 2645be1db475SDaniel Axtens pmd = pmd_offset(pud, addr); 2646be1db475SDaniel Axtens } 2647aee16b3cSJeremy Fitzhardinge do { 2648aee16b3cSJeremy Fitzhardinge next = pmd_addr_end(addr, end); 26490c95cba4SNicholas Piggin if (pmd_none(*pmd) && !create) 26500c95cba4SNicholas Piggin continue; 26510c95cba4SNicholas Piggin if (WARN_ON_ONCE(pmd_leaf(*pmd))) 26520c95cba4SNicholas Piggin return -EINVAL; 26530c95cba4SNicholas Piggin if (!pmd_none(*pmd) && WARN_ON_ONCE(pmd_bad(*pmd))) { 26540c95cba4SNicholas Piggin if (!create) 26550c95cba4SNicholas Piggin continue; 26560c95cba4SNicholas Piggin pmd_clear_bad(pmd); 26570c95cba4SNicholas Piggin } 26580c95cba4SNicholas Piggin err = apply_to_pte_range(mm, pmd, addr, next, 26590c95cba4SNicholas Piggin fn, data, create, mask); 2660aee16b3cSJeremy Fitzhardinge if (err) 2661aee16b3cSJeremy Fitzhardinge break; 2662aee16b3cSJeremy Fitzhardinge } while (pmd++, addr = next, addr != end); 26630c95cba4SNicholas Piggin 2664aee16b3cSJeremy Fitzhardinge return err; 2665aee16b3cSJeremy Fitzhardinge } 2666aee16b3cSJeremy Fitzhardinge 2667c2febafcSKirill A. Shutemov static int apply_to_pud_range(struct mm_struct *mm, p4d_t *p4d, 2668aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2669e80d3909SJoerg Roedel pte_fn_t fn, void *data, bool create, 2670e80d3909SJoerg Roedel pgtbl_mod_mask *mask) 2671aee16b3cSJeremy Fitzhardinge { 2672aee16b3cSJeremy Fitzhardinge pud_t *pud; 2673aee16b3cSJeremy Fitzhardinge unsigned long next; 2674be1db475SDaniel Axtens int err = 0; 2675aee16b3cSJeremy Fitzhardinge 2676be1db475SDaniel Axtens if (create) { 2677e80d3909SJoerg Roedel pud = pud_alloc_track(mm, p4d, addr, mask); 2678aee16b3cSJeremy Fitzhardinge if (!pud) 2679aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2680be1db475SDaniel Axtens } else { 2681be1db475SDaniel Axtens pud = pud_offset(p4d, addr); 2682be1db475SDaniel Axtens } 2683aee16b3cSJeremy Fitzhardinge do { 2684aee16b3cSJeremy Fitzhardinge next = pud_addr_end(addr, end); 26850c95cba4SNicholas Piggin if (pud_none(*pud) && !create) 26860c95cba4SNicholas Piggin continue; 26870c95cba4SNicholas Piggin if (WARN_ON_ONCE(pud_leaf(*pud))) 26880c95cba4SNicholas Piggin return -EINVAL; 26890c95cba4SNicholas Piggin if (!pud_none(*pud) && WARN_ON_ONCE(pud_bad(*pud))) { 26900c95cba4SNicholas Piggin if (!create) 26910c95cba4SNicholas Piggin continue; 26920c95cba4SNicholas Piggin pud_clear_bad(pud); 26930c95cba4SNicholas Piggin } 26940c95cba4SNicholas Piggin err = apply_to_pmd_range(mm, pud, addr, next, 26950c95cba4SNicholas Piggin fn, data, create, mask); 2696aee16b3cSJeremy Fitzhardinge if (err) 2697aee16b3cSJeremy Fitzhardinge break; 2698aee16b3cSJeremy Fitzhardinge } while (pud++, addr = next, addr != end); 26990c95cba4SNicholas Piggin 2700aee16b3cSJeremy Fitzhardinge return err; 2701aee16b3cSJeremy Fitzhardinge } 2702aee16b3cSJeremy Fitzhardinge 2703c2febafcSKirill A. Shutemov static int apply_to_p4d_range(struct mm_struct *mm, pgd_t *pgd, 2704c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 2705e80d3909SJoerg Roedel pte_fn_t fn, void *data, bool create, 2706e80d3909SJoerg Roedel pgtbl_mod_mask *mask) 2707c2febafcSKirill A. Shutemov { 2708c2febafcSKirill A. Shutemov p4d_t *p4d; 2709c2febafcSKirill A. Shutemov unsigned long next; 2710be1db475SDaniel Axtens int err = 0; 2711c2febafcSKirill A. Shutemov 2712be1db475SDaniel Axtens if (create) { 2713e80d3909SJoerg Roedel p4d = p4d_alloc_track(mm, pgd, addr, mask); 2714c2febafcSKirill A. Shutemov if (!p4d) 2715c2febafcSKirill A. Shutemov return -ENOMEM; 2716be1db475SDaniel Axtens } else { 2717be1db475SDaniel Axtens p4d = p4d_offset(pgd, addr); 2718be1db475SDaniel Axtens } 2719c2febafcSKirill A. Shutemov do { 2720c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 27210c95cba4SNicholas Piggin if (p4d_none(*p4d) && !create) 27220c95cba4SNicholas Piggin continue; 27230c95cba4SNicholas Piggin if (WARN_ON_ONCE(p4d_leaf(*p4d))) 27240c95cba4SNicholas Piggin return -EINVAL; 27250c95cba4SNicholas Piggin if (!p4d_none(*p4d) && WARN_ON_ONCE(p4d_bad(*p4d))) { 27260c95cba4SNicholas Piggin if (!create) 27270c95cba4SNicholas Piggin continue; 27280c95cba4SNicholas Piggin p4d_clear_bad(p4d); 27290c95cba4SNicholas Piggin } 27300c95cba4SNicholas Piggin err = apply_to_pud_range(mm, p4d, addr, next, 27310c95cba4SNicholas Piggin fn, data, create, mask); 2732c2febafcSKirill A. Shutemov if (err) 2733c2febafcSKirill A. Shutemov break; 2734c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 27350c95cba4SNicholas Piggin 2736c2febafcSKirill A. Shutemov return err; 2737c2febafcSKirill A. Shutemov } 2738c2febafcSKirill A. Shutemov 2739be1db475SDaniel Axtens static int __apply_to_page_range(struct mm_struct *mm, unsigned long addr, 2740be1db475SDaniel Axtens unsigned long size, pte_fn_t fn, 2741be1db475SDaniel Axtens void *data, bool create) 2742be1db475SDaniel Axtens { 2743be1db475SDaniel Axtens pgd_t *pgd; 2744e80d3909SJoerg Roedel unsigned long start = addr, next; 2745be1db475SDaniel Axtens unsigned long end = addr + size; 2746e80d3909SJoerg Roedel pgtbl_mod_mask mask = 0; 2747be1db475SDaniel Axtens int err = 0; 2748be1db475SDaniel Axtens 2749be1db475SDaniel Axtens if (WARN_ON(addr >= end)) 2750be1db475SDaniel Axtens return -EINVAL; 2751be1db475SDaniel Axtens 2752be1db475SDaniel Axtens pgd = pgd_offset(mm, addr); 2753be1db475SDaniel Axtens do { 2754be1db475SDaniel Axtens next = pgd_addr_end(addr, end); 27550c95cba4SNicholas Piggin if (pgd_none(*pgd) && !create) 2756be1db475SDaniel Axtens continue; 27570c95cba4SNicholas Piggin if (WARN_ON_ONCE(pgd_leaf(*pgd))) 27580c95cba4SNicholas Piggin return -EINVAL; 27590c95cba4SNicholas Piggin if (!pgd_none(*pgd) && WARN_ON_ONCE(pgd_bad(*pgd))) { 27600c95cba4SNicholas Piggin if (!create) 27610c95cba4SNicholas Piggin continue; 27620c95cba4SNicholas Piggin pgd_clear_bad(pgd); 27630c95cba4SNicholas Piggin } 27640c95cba4SNicholas Piggin err = apply_to_p4d_range(mm, pgd, addr, next, 27650c95cba4SNicholas Piggin fn, data, create, &mask); 2766be1db475SDaniel Axtens if (err) 2767be1db475SDaniel Axtens break; 2768be1db475SDaniel Axtens } while (pgd++, addr = next, addr != end); 2769be1db475SDaniel Axtens 2770e80d3909SJoerg Roedel if (mask & ARCH_PAGE_TABLE_SYNC_MASK) 2771e80d3909SJoerg Roedel arch_sync_kernel_mappings(start, start + size); 2772e80d3909SJoerg Roedel 2773be1db475SDaniel Axtens return err; 2774be1db475SDaniel Axtens } 2775be1db475SDaniel Axtens 2776aee16b3cSJeremy Fitzhardinge /* 2777aee16b3cSJeremy Fitzhardinge * Scan a region of virtual memory, filling in page tables as necessary 2778aee16b3cSJeremy Fitzhardinge * and calling a provided function on each leaf page table. 2779aee16b3cSJeremy Fitzhardinge */ 2780aee16b3cSJeremy Fitzhardinge int apply_to_page_range(struct mm_struct *mm, unsigned long addr, 2781aee16b3cSJeremy Fitzhardinge unsigned long size, pte_fn_t fn, void *data) 2782aee16b3cSJeremy Fitzhardinge { 2783be1db475SDaniel Axtens return __apply_to_page_range(mm, addr, size, fn, data, true); 2784aee16b3cSJeremy Fitzhardinge } 2785aee16b3cSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(apply_to_page_range); 2786aee16b3cSJeremy Fitzhardinge 27871da177e4SLinus Torvalds /* 2788be1db475SDaniel Axtens * Scan a region of virtual memory, calling a provided function on 2789be1db475SDaniel Axtens * each leaf page table where it exists. 2790be1db475SDaniel Axtens * 2791be1db475SDaniel Axtens * Unlike apply_to_page_range, this does _not_ fill in page tables 2792be1db475SDaniel Axtens * where they are absent. 2793be1db475SDaniel Axtens */ 2794be1db475SDaniel Axtens int apply_to_existing_page_range(struct mm_struct *mm, unsigned long addr, 2795be1db475SDaniel Axtens unsigned long size, pte_fn_t fn, void *data) 2796be1db475SDaniel Axtens { 2797be1db475SDaniel Axtens return __apply_to_page_range(mm, addr, size, fn, data, false); 2798be1db475SDaniel Axtens } 2799be1db475SDaniel Axtens EXPORT_SYMBOL_GPL(apply_to_existing_page_range); 2800be1db475SDaniel Axtens 2801be1db475SDaniel Axtens /* 28029b4bdd2fSKirill A. Shutemov * handle_pte_fault chooses page fault handler according to an entry which was 28039b4bdd2fSKirill A. Shutemov * read non-atomically. Before making any commitment, on those architectures 28049b4bdd2fSKirill A. Shutemov * or configurations (e.g. i386 with PAE) which might give a mix of unmatched 28059b4bdd2fSKirill A. Shutemov * parts, do_swap_page must check under lock before unmapping the pte and 28069b4bdd2fSKirill A. Shutemov * proceeding (but do_wp_page is only called after already making such a check; 2807a335b2e1SRyota Ozaki * and do_anonymous_page can safely check later on). 28088f4e2101SHugh Dickins */ 28092ca99358SPeter Xu static inline int pte_unmap_same(struct vm_fault *vmf) 28108f4e2101SHugh Dickins { 28118f4e2101SHugh Dickins int same = 1; 2812923717cbSThomas Gleixner #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPTION) 28138f4e2101SHugh Dickins if (sizeof(pte_t) > sizeof(unsigned long)) { 2814c7ad0880SHugh Dickins spin_lock(vmf->ptl); 2815c33c7948SRyan Roberts same = pte_same(ptep_get(vmf->pte), vmf->orig_pte); 2816c7ad0880SHugh Dickins spin_unlock(vmf->ptl); 28178f4e2101SHugh Dickins } 28188f4e2101SHugh Dickins #endif 28192ca99358SPeter Xu pte_unmap(vmf->pte); 28202ca99358SPeter Xu vmf->pte = NULL; 28218f4e2101SHugh Dickins return same; 28228f4e2101SHugh Dickins } 28238f4e2101SHugh Dickins 2824a873dfe1STony Luck /* 2825a873dfe1STony Luck * Return: 2826a873dfe1STony Luck * 0: copied succeeded 2827a873dfe1STony Luck * -EHWPOISON: copy failed due to hwpoison in source page 2828a873dfe1STony Luck * -EAGAIN: copied failed (some other reason) 2829a873dfe1STony Luck */ 2830a873dfe1STony Luck static inline int __wp_page_copy_user(struct page *dst, struct page *src, 283183d116c5SJia He struct vm_fault *vmf) 28326aab341eSLinus Torvalds { 2833a873dfe1STony Luck int ret; 283483d116c5SJia He void *kaddr; 283583d116c5SJia He void __user *uaddr; 283683d116c5SJia He struct vm_area_struct *vma = vmf->vma; 283783d116c5SJia He struct mm_struct *mm = vma->vm_mm; 283883d116c5SJia He unsigned long addr = vmf->address; 283983d116c5SJia He 284083d116c5SJia He if (likely(src)) { 2841d302c239STony Luck if (copy_mc_user_highpage(dst, src, addr, vma)) { 2842d302c239STony Luck memory_failure_queue(page_to_pfn(src), 0); 2843a873dfe1STony Luck return -EHWPOISON; 2844d302c239STony Luck } 2845a873dfe1STony Luck return 0; 284683d116c5SJia He } 284783d116c5SJia He 28486aab341eSLinus Torvalds /* 28496aab341eSLinus Torvalds * If the source page was a PFN mapping, we don't have 28506aab341eSLinus Torvalds * a "struct page" for it. We do a best-effort copy by 28516aab341eSLinus Torvalds * just copying from the original user address. If that 28526aab341eSLinus Torvalds * fails, we just zero-fill it. Live with it. 28536aab341eSLinus Torvalds */ 285424d2613aSFabio M. De Francesco kaddr = kmap_local_page(dst); 285524d2613aSFabio M. De Francesco pagefault_disable(); 285683d116c5SJia He uaddr = (void __user *)(addr & PAGE_MASK); 285783d116c5SJia He 285883d116c5SJia He /* 285983d116c5SJia He * On architectures with software "accessed" bits, we would 286083d116c5SJia He * take a double page fault, so mark it accessed here. 286183d116c5SJia He */ 28623db82b93SHugh Dickins vmf->pte = NULL; 2863e1fd09e3SYu Zhao if (!arch_has_hw_pte_young() && !pte_young(vmf->orig_pte)) { 286483d116c5SJia He pte_t entry; 286583d116c5SJia He 286683d116c5SJia He vmf->pte = pte_offset_map_lock(mm, vmf->pmd, addr, &vmf->ptl); 2867c33c7948SRyan Roberts if (unlikely(!vmf->pte || !pte_same(ptep_get(vmf->pte), vmf->orig_pte))) { 286883d116c5SJia He /* 286983d116c5SJia He * Other thread has already handled the fault 28707df67697SBibo Mao * and update local tlb only 287183d116c5SJia He */ 2872a92cbb82SHugh Dickins if (vmf->pte) 28737df67697SBibo Mao update_mmu_tlb(vma, addr, vmf->pte); 2874a873dfe1STony Luck ret = -EAGAIN; 287583d116c5SJia He goto pte_unlock; 287683d116c5SJia He } 287783d116c5SJia He 287883d116c5SJia He entry = pte_mkyoung(vmf->orig_pte); 287983d116c5SJia He if (ptep_set_access_flags(vma, addr, vmf->pte, entry, 0)) 28805003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vma, addr, vmf->pte, 1); 288183d116c5SJia He } 28825d2a2dbbSLinus Torvalds 28835d2a2dbbSLinus Torvalds /* 28845d2a2dbbSLinus Torvalds * This really shouldn't fail, because the page is there 28855d2a2dbbSLinus Torvalds * in the page tables. But it might just be unreadable, 28865d2a2dbbSLinus Torvalds * in which case we just give up and fill the result with 28875d2a2dbbSLinus Torvalds * zeroes. 28885d2a2dbbSLinus Torvalds */ 288983d116c5SJia He if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) { 28903db82b93SHugh Dickins if (vmf->pte) 2891c3e5ea6eSKirill A. Shutemov goto warn; 2892c3e5ea6eSKirill A. Shutemov 2893c3e5ea6eSKirill A. Shutemov /* Re-validate under PTL if the page is still mapped */ 2894c3e5ea6eSKirill A. Shutemov vmf->pte = pte_offset_map_lock(mm, vmf->pmd, addr, &vmf->ptl); 2895c33c7948SRyan Roberts if (unlikely(!vmf->pte || !pte_same(ptep_get(vmf->pte), vmf->orig_pte))) { 28967df67697SBibo Mao /* The PTE changed under us, update local tlb */ 2897a92cbb82SHugh Dickins if (vmf->pte) 28987df67697SBibo Mao update_mmu_tlb(vma, addr, vmf->pte); 2899a873dfe1STony Luck ret = -EAGAIN; 2900c3e5ea6eSKirill A. Shutemov goto pte_unlock; 2901c3e5ea6eSKirill A. Shutemov } 2902c3e5ea6eSKirill A. Shutemov 2903c3e5ea6eSKirill A. Shutemov /* 2904985ba004SEthon Paul * The same page can be mapped back since last copy attempt. 2905c3e5ea6eSKirill A. Shutemov * Try to copy again under PTL. 2906c3e5ea6eSKirill A. Shutemov */ 2907c3e5ea6eSKirill A. Shutemov if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) { 290883d116c5SJia He /* 290983d116c5SJia He * Give a warn in case there can be some obscure 291083d116c5SJia He * use-case 291183d116c5SJia He */ 2912c3e5ea6eSKirill A. Shutemov warn: 291383d116c5SJia He WARN_ON_ONCE(1); 29143ecb01dfSJan Beulich clear_page(kaddr); 291583d116c5SJia He } 2916c3e5ea6eSKirill A. Shutemov } 291783d116c5SJia He 2918a873dfe1STony Luck ret = 0; 291983d116c5SJia He 292083d116c5SJia He pte_unlock: 29213db82b93SHugh Dickins if (vmf->pte) 292283d116c5SJia He pte_unmap_unlock(vmf->pte, vmf->ptl); 292324d2613aSFabio M. De Francesco pagefault_enable(); 292424d2613aSFabio M. De Francesco kunmap_local(kaddr); 2925c4ec7b0dSDmitriy Monakhov flush_dcache_page(dst); 292683d116c5SJia He 292783d116c5SJia He return ret; 29286aab341eSLinus Torvalds } 29296aab341eSLinus Torvalds 2930c20cd45eSMichal Hocko static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma) 2931c20cd45eSMichal Hocko { 2932c20cd45eSMichal Hocko struct file *vm_file = vma->vm_file; 2933c20cd45eSMichal Hocko 2934c20cd45eSMichal Hocko if (vm_file) 2935c20cd45eSMichal Hocko return mapping_gfp_mask(vm_file->f_mapping) | __GFP_FS | __GFP_IO; 2936c20cd45eSMichal Hocko 2937c20cd45eSMichal Hocko /* 2938c20cd45eSMichal Hocko * Special mappings (e.g. VDSO) do not have any file so fake 2939c20cd45eSMichal Hocko * a default GFP_KERNEL for them. 2940c20cd45eSMichal Hocko */ 2941c20cd45eSMichal Hocko return GFP_KERNEL; 2942c20cd45eSMichal Hocko } 2943c20cd45eSMichal Hocko 29441da177e4SLinus Torvalds /* 2945fb09a464SKirill A. Shutemov * Notify the address space that the page is about to become writable so that 2946fb09a464SKirill A. Shutemov * it can prohibit this or wait for the page to get into an appropriate state. 2947fb09a464SKirill A. Shutemov * 2948fb09a464SKirill A. Shutemov * We do this without the lock held, so that it can sleep if it needs to. 2949fb09a464SKirill A. Shutemov */ 295086aa6998SSidhartha Kumar static vm_fault_t do_page_mkwrite(struct vm_fault *vmf, struct folio *folio) 2951fb09a464SKirill A. Shutemov { 29522b740303SSouptick Joarder vm_fault_t ret; 295338b8cb7fSJan Kara unsigned int old_flags = vmf->flags; 2954fb09a464SKirill A. Shutemov 295538b8cb7fSJan Kara vmf->flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE; 2956fb09a464SKirill A. Shutemov 2957dc617f29SDarrick J. Wong if (vmf->vma->vm_file && 2958dc617f29SDarrick J. Wong IS_SWAPFILE(vmf->vma->vm_file->f_mapping->host)) 2959dc617f29SDarrick J. Wong return VM_FAULT_SIGBUS; 2960dc617f29SDarrick J. Wong 296111bac800SDave Jiang ret = vmf->vma->vm_ops->page_mkwrite(vmf); 296238b8cb7fSJan Kara /* Restore original flags so that caller is not surprised */ 296338b8cb7fSJan Kara vmf->flags = old_flags; 2964fb09a464SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) 2965fb09a464SKirill A. Shutemov return ret; 2966fb09a464SKirill A. Shutemov if (unlikely(!(ret & VM_FAULT_LOCKED))) { 29673d243659SSidhartha Kumar folio_lock(folio); 29683d243659SSidhartha Kumar if (!folio->mapping) { 29693d243659SSidhartha Kumar folio_unlock(folio); 2970fb09a464SKirill A. Shutemov return 0; /* retry */ 2971fb09a464SKirill A. Shutemov } 2972fb09a464SKirill A. Shutemov ret |= VM_FAULT_LOCKED; 2973fb09a464SKirill A. Shutemov } else 29743d243659SSidhartha Kumar VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); 2975fb09a464SKirill A. Shutemov return ret; 2976fb09a464SKirill A. Shutemov } 2977fb09a464SKirill A. Shutemov 2978fb09a464SKirill A. Shutemov /* 297997ba0c2bSJan Kara * Handle dirtying of a page in shared file mapping on a write fault. 29804e047f89SShachar Raindel * 298197ba0c2bSJan Kara * The function expects the page to be locked and unlocks it. 29824e047f89SShachar Raindel */ 298389b15332SJohannes Weiner static vm_fault_t fault_dirty_shared_page(struct vm_fault *vmf) 29844e047f89SShachar Raindel { 298589b15332SJohannes Weiner struct vm_area_struct *vma = vmf->vma; 29864e047f89SShachar Raindel struct address_space *mapping; 298715b4919aSZhangPeng struct folio *folio = page_folio(vmf->page); 298897ba0c2bSJan Kara bool dirtied; 298997ba0c2bSJan Kara bool page_mkwrite = vma->vm_ops && vma->vm_ops->page_mkwrite; 29904e047f89SShachar Raindel 299115b4919aSZhangPeng dirtied = folio_mark_dirty(folio); 299215b4919aSZhangPeng VM_BUG_ON_FOLIO(folio_test_anon(folio), folio); 299397ba0c2bSJan Kara /* 299415b4919aSZhangPeng * Take a local copy of the address_space - folio.mapping may be zeroed 299515b4919aSZhangPeng * by truncate after folio_unlock(). The address_space itself remains 299615b4919aSZhangPeng * pinned by vma->vm_file's reference. We rely on folio_unlock()'s 299797ba0c2bSJan Kara * release semantics to prevent the compiler from undoing this copying. 299897ba0c2bSJan Kara */ 299915b4919aSZhangPeng mapping = folio_raw_mapping(folio); 300015b4919aSZhangPeng folio_unlock(folio); 30014e047f89SShachar Raindel 30024e047f89SShachar Raindel if (!page_mkwrite) 30034e047f89SShachar Raindel file_update_time(vma->vm_file); 300489b15332SJohannes Weiner 300589b15332SJohannes Weiner /* 300689b15332SJohannes Weiner * Throttle page dirtying rate down to writeback speed. 300789b15332SJohannes Weiner * 300889b15332SJohannes Weiner * mapping may be NULL here because some device drivers do not 300989b15332SJohannes Weiner * set page.mapping but still dirty their pages 301089b15332SJohannes Weiner * 3011c1e8d7c6SMichel Lespinasse * Drop the mmap_lock before waiting on IO, if we can. The file 301289b15332SJohannes Weiner * is pinning the mapping, as per above. 301389b15332SJohannes Weiner */ 301489b15332SJohannes Weiner if ((dirtied || page_mkwrite) && mapping) { 301589b15332SJohannes Weiner struct file *fpin; 301689b15332SJohannes Weiner 301789b15332SJohannes Weiner fpin = maybe_unlock_mmap_for_io(vmf, NULL); 301889b15332SJohannes Weiner balance_dirty_pages_ratelimited(mapping); 301989b15332SJohannes Weiner if (fpin) { 302089b15332SJohannes Weiner fput(fpin); 3021d9272525SPeter Xu return VM_FAULT_COMPLETED; 302289b15332SJohannes Weiner } 302389b15332SJohannes Weiner } 302489b15332SJohannes Weiner 302589b15332SJohannes Weiner return 0; 30264e047f89SShachar Raindel } 30274e047f89SShachar Raindel 302897ba0c2bSJan Kara /* 30294e047f89SShachar Raindel * Handle write page faults for pages that can be reused in the current vma 30304e047f89SShachar Raindel * 30314e047f89SShachar Raindel * This can happen either due to the mapping being with the VM_SHARED flag, 30324e047f89SShachar Raindel * or due to us being the last reference standing to the page. In either 30334e047f89SShachar Raindel * case, all we need to do here is to mark the page as writable and update 30344e047f89SShachar Raindel * any related book-keeping. 30354e047f89SShachar Raindel */ 3036a86bc96bSKefeng Wang static inline void wp_page_reuse(struct vm_fault *vmf, struct folio *folio) 303782b0f8c3SJan Kara __releases(vmf->ptl) 30384e047f89SShachar Raindel { 303982b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 30404e047f89SShachar Raindel pte_t entry; 30416c287605SDavid Hildenbrand 3042c89357e2SDavid Hildenbrand VM_BUG_ON(!(vmf->flags & FAULT_FLAG_WRITE)); 30436c287605SDavid Hildenbrand 3044c2c3b514SKefeng Wang if (folio) { 3045c2c3b514SKefeng Wang VM_BUG_ON(folio_test_anon(folio) && 3046c2c3b514SKefeng Wang !PageAnonExclusive(vmf->page)); 30474e047f89SShachar Raindel /* 3048c2c3b514SKefeng Wang * Clear the folio's cpupid information as the existing 30494e047f89SShachar Raindel * information potentially belongs to a now completely 30504e047f89SShachar Raindel * unrelated process. 30514e047f89SShachar Raindel */ 3052c2c3b514SKefeng Wang folio_xchg_last_cpupid(folio, (1 << LAST_CPUPID_SHIFT) - 1); 3053c2c3b514SKefeng Wang } 30544e047f89SShachar Raindel 30552994302bSJan Kara flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); 30562994302bSJan Kara entry = pte_mkyoung(vmf->orig_pte); 30574e047f89SShachar Raindel entry = maybe_mkwrite(pte_mkdirty(entry), vma); 305882b0f8c3SJan Kara if (ptep_set_access_flags(vma, vmf->address, vmf->pte, entry, 1)) 30595003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vma, vmf->address, vmf->pte, 1); 306082b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3061798a6b87SPeter Xu count_vm_event(PGREUSE); 30624e047f89SShachar Raindel } 30634e047f89SShachar Raindel 30644ed43798SMatthew Wilcox (Oracle) /* 30654ed43798SMatthew Wilcox (Oracle) * We could add a bitflag somewhere, but for now, we know that all 30664ed43798SMatthew Wilcox (Oracle) * vm_ops that have a ->map_pages have been audited and don't need 30674ed43798SMatthew Wilcox (Oracle) * the mmap_lock to be held. 30684ed43798SMatthew Wilcox (Oracle) */ 30694ed43798SMatthew Wilcox (Oracle) static inline vm_fault_t vmf_can_call_fault(const struct vm_fault *vmf) 30704ed43798SMatthew Wilcox (Oracle) { 30714ed43798SMatthew Wilcox (Oracle) struct vm_area_struct *vma = vmf->vma; 30724ed43798SMatthew Wilcox (Oracle) 30734ed43798SMatthew Wilcox (Oracle) if (vma->vm_ops->map_pages || !(vmf->flags & FAULT_FLAG_VMA_LOCK)) 30744ed43798SMatthew Wilcox (Oracle) return 0; 30754ed43798SMatthew Wilcox (Oracle) vma_end_read(vma); 30764ed43798SMatthew Wilcox (Oracle) return VM_FAULT_RETRY; 30774ed43798SMatthew Wilcox (Oracle) } 30784ed43798SMatthew Wilcox (Oracle) 3079164b06f2SMatthew Wilcox (Oracle) static vm_fault_t vmf_anon_prepare(struct vm_fault *vmf) 3080164b06f2SMatthew Wilcox (Oracle) { 3081164b06f2SMatthew Wilcox (Oracle) struct vm_area_struct *vma = vmf->vma; 3082164b06f2SMatthew Wilcox (Oracle) 3083164b06f2SMatthew Wilcox (Oracle) if (likely(vma->anon_vma)) 3084164b06f2SMatthew Wilcox (Oracle) return 0; 3085164b06f2SMatthew Wilcox (Oracle) if (vmf->flags & FAULT_FLAG_VMA_LOCK) { 3086164b06f2SMatthew Wilcox (Oracle) vma_end_read(vma); 3087164b06f2SMatthew Wilcox (Oracle) return VM_FAULT_RETRY; 3088164b06f2SMatthew Wilcox (Oracle) } 3089164b06f2SMatthew Wilcox (Oracle) if (__anon_vma_prepare(vma)) 3090164b06f2SMatthew Wilcox (Oracle) return VM_FAULT_OOM; 3091164b06f2SMatthew Wilcox (Oracle) return 0; 3092164b06f2SMatthew Wilcox (Oracle) } 3093164b06f2SMatthew Wilcox (Oracle) 30944e047f89SShachar Raindel /* 3095c89357e2SDavid Hildenbrand * Handle the case of a page which we actually need to copy to a new page, 3096c89357e2SDavid Hildenbrand * either due to COW or unsharing. 30972f38ab2cSShachar Raindel * 3098c1e8d7c6SMichel Lespinasse * Called with mmap_lock locked and the old page referenced, but 30992f38ab2cSShachar Raindel * without the ptl held. 31002f38ab2cSShachar Raindel * 31012f38ab2cSShachar Raindel * High level logic flow: 31022f38ab2cSShachar Raindel * 31032f38ab2cSShachar Raindel * - Allocate a page, copy the content of the old page to the new one. 31042f38ab2cSShachar Raindel * - Handle book keeping and accounting - cgroups, mmu-notifiers, etc. 31052f38ab2cSShachar Raindel * - Take the PTL. If the pte changed, bail out and release the allocated page 31062f38ab2cSShachar Raindel * - If the pte is still the way we remember it, update the page table and all 31072f38ab2cSShachar Raindel * relevant references. This includes dropping the reference the page-table 31082f38ab2cSShachar Raindel * held to the old page, as well as updating the rmap. 31092f38ab2cSShachar Raindel * - In any case, unlock the PTL and drop the reference we took to the old page. 31102f38ab2cSShachar Raindel */ 31112b740303SSouptick Joarder static vm_fault_t wp_page_copy(struct vm_fault *vmf) 31122f38ab2cSShachar Raindel { 3113c89357e2SDavid Hildenbrand const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE; 311482b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3115bae473a4SKirill A. Shutemov struct mm_struct *mm = vma->vm_mm; 311628d41a48SMatthew Wilcox (Oracle) struct folio *old_folio = NULL; 311728d41a48SMatthew Wilcox (Oracle) struct folio *new_folio = NULL; 31182f38ab2cSShachar Raindel pte_t entry; 31192f38ab2cSShachar Raindel int page_copied = 0; 3120ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 3121164b06f2SMatthew Wilcox (Oracle) vm_fault_t ret; 3122cf503cc6SKefeng Wang bool pfn_is_zero; 31232f38ab2cSShachar Raindel 3124662ce1dcSYang Yang delayacct_wpcopy_start(); 3125662ce1dcSYang Yang 312628d41a48SMatthew Wilcox (Oracle) if (vmf->page) 312728d41a48SMatthew Wilcox (Oracle) old_folio = page_folio(vmf->page); 3128164b06f2SMatthew Wilcox (Oracle) ret = vmf_anon_prepare(vmf); 3129164b06f2SMatthew Wilcox (Oracle) if (unlikely(ret)) 3130164b06f2SMatthew Wilcox (Oracle) goto out; 31312f38ab2cSShachar Raindel 3132cf503cc6SKefeng Wang pfn_is_zero = is_zero_pfn(pte_pfn(vmf->orig_pte)); 3133cf503cc6SKefeng Wang new_folio = folio_prealloc(mm, vma, vmf->address, pfn_is_zero); 31346bc56a4dSMatthew Wilcox (Oracle) if (!new_folio) 31352f38ab2cSShachar Raindel goto oom; 3136cf503cc6SKefeng Wang 3137cf503cc6SKefeng Wang if (!pfn_is_zero) { 3138164b06f2SMatthew Wilcox (Oracle) int err; 313983d116c5SJia He 3140164b06f2SMatthew Wilcox (Oracle) err = __wp_page_copy_user(&new_folio->page, vmf->page, vmf); 3141164b06f2SMatthew Wilcox (Oracle) if (err) { 314283d116c5SJia He /* 314383d116c5SJia He * COW failed, if the fault was solved by other, 314483d116c5SJia He * it's fine. If not, userspace would re-fault on 314583d116c5SJia He * the same address and we will handle the fault 314683d116c5SJia He * from the second attempt. 3147a873dfe1STony Luck * The -EHWPOISON case will not be retried. 314883d116c5SJia He */ 314928d41a48SMatthew Wilcox (Oracle) folio_put(new_folio); 315028d41a48SMatthew Wilcox (Oracle) if (old_folio) 315128d41a48SMatthew Wilcox (Oracle) folio_put(old_folio); 3152662ce1dcSYang Yang 3153662ce1dcSYang Yang delayacct_wpcopy_end(); 3154164b06f2SMatthew Wilcox (Oracle) return err == -EHWPOISON ? VM_FAULT_HWPOISON : 0; 315583d116c5SJia He } 315628d41a48SMatthew Wilcox (Oracle) kmsan_copy_page_meta(&new_folio->page, vmf->page); 31572f38ab2cSShachar Raindel } 31582f38ab2cSShachar Raindel 315928d41a48SMatthew Wilcox (Oracle) __folio_mark_uptodate(new_folio); 3160eb3c24f3SMel Gorman 31617d4a8be0SAlistair Popple mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, 31626f4f13e8SJérôme Glisse vmf->address & PAGE_MASK, 3163ac46d4f3SJérôme Glisse (vmf->address & PAGE_MASK) + PAGE_SIZE); 3164ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 31652f38ab2cSShachar Raindel 31662f38ab2cSShachar Raindel /* 31672f38ab2cSShachar Raindel * Re-check the pte - we dropped the lock 31682f38ab2cSShachar Raindel */ 316982b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(mm, vmf->pmd, vmf->address, &vmf->ptl); 3170c33c7948SRyan Roberts if (likely(vmf->pte && pte_same(ptep_get(vmf->pte), vmf->orig_pte))) { 317128d41a48SMatthew Wilcox (Oracle) if (old_folio) { 317228d41a48SMatthew Wilcox (Oracle) if (!folio_test_anon(old_folio)) { 317328d41a48SMatthew Wilcox (Oracle) dec_mm_counter(mm, mm_counter_file(&old_folio->page)); 3174f1a79412SShakeel Butt inc_mm_counter(mm, MM_ANONPAGES); 31752f38ab2cSShachar Raindel } 31762f38ab2cSShachar Raindel } else { 31776080d19fSxu xin ksm_might_unmap_zero_page(mm, vmf->orig_pte); 3178f1a79412SShakeel Butt inc_mm_counter(mm, MM_ANONPAGES); 31792f38ab2cSShachar Raindel } 31802994302bSJan Kara flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); 318128d41a48SMatthew Wilcox (Oracle) entry = mk_pte(&new_folio->page, vma->vm_page_prot); 318250c25ee9SThomas Bogendoerfer entry = pte_sw_mkyoung(entry); 3183c89357e2SDavid Hildenbrand if (unlikely(unshare)) { 3184c89357e2SDavid Hildenbrand if (pte_soft_dirty(vmf->orig_pte)) 3185c89357e2SDavid Hildenbrand entry = pte_mksoft_dirty(entry); 3186c89357e2SDavid Hildenbrand if (pte_uffd_wp(vmf->orig_pte)) 3187c89357e2SDavid Hildenbrand entry = pte_mkuffd_wp(entry); 3188c89357e2SDavid Hildenbrand } else { 31892f38ab2cSShachar Raindel entry = maybe_mkwrite(pte_mkdirty(entry), vma); 3190c89357e2SDavid Hildenbrand } 3191111fe718SNicholas Piggin 31922f38ab2cSShachar Raindel /* 31932f38ab2cSShachar Raindel * Clear the pte entry and flush it first, before updating the 3194111fe718SNicholas Piggin * pte with the new entry, to keep TLBs on different CPUs in 3195111fe718SNicholas Piggin * sync. This code used to set the new PTE then flush TLBs, but 3196111fe718SNicholas Piggin * that left a window where the new PTE could be loaded into 3197111fe718SNicholas Piggin * some TLBs while the old PTE remains in others. 31982f38ab2cSShachar Raindel */ 3199ec8832d0SAlistair Popple ptep_clear_flush(vma, vmf->address, vmf->pte); 320028d41a48SMatthew Wilcox (Oracle) folio_add_new_anon_rmap(new_folio, vma, vmf->address); 320128d41a48SMatthew Wilcox (Oracle) folio_add_lru_vma(new_folio, vma); 32022f38ab2cSShachar Raindel /* 32032f38ab2cSShachar Raindel * We call the notify macro here because, when using secondary 32042f38ab2cSShachar Raindel * mmu page tables (such as kvm shadow page tables), we want the 32052f38ab2cSShachar Raindel * new page to be mapped directly into the secondary page table. 32062f38ab2cSShachar Raindel */ 3207c89357e2SDavid Hildenbrand BUG_ON(unshare && pte_write(entry)); 320882b0f8c3SJan Kara set_pte_at_notify(mm, vmf->address, vmf->pte, entry); 32095003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vma, vmf->address, vmf->pte, 1); 321028d41a48SMatthew Wilcox (Oracle) if (old_folio) { 32112f38ab2cSShachar Raindel /* 32122f38ab2cSShachar Raindel * Only after switching the pte to the new page may 32132f38ab2cSShachar Raindel * we remove the mapcount here. Otherwise another 32142f38ab2cSShachar Raindel * process may come and find the rmap count decremented 32152f38ab2cSShachar Raindel * before the pte is switched to the new page, and 32162f38ab2cSShachar Raindel * "reuse" the old page writing into it while our pte 32172f38ab2cSShachar Raindel * here still points into it and can be read by other 32182f38ab2cSShachar Raindel * threads. 32192f38ab2cSShachar Raindel * 32202f38ab2cSShachar Raindel * The critical issue is to order this 32212f38ab2cSShachar Raindel * page_remove_rmap with the ptp_clear_flush above. 32222f38ab2cSShachar Raindel * Those stores are ordered by (if nothing else,) 32232f38ab2cSShachar Raindel * the barrier present in the atomic_add_negative 32242f38ab2cSShachar Raindel * in page_remove_rmap. 32252f38ab2cSShachar Raindel * 32262f38ab2cSShachar Raindel * Then the TLB flush in ptep_clear_flush ensures that 32272f38ab2cSShachar Raindel * no process can access the old page before the 32282f38ab2cSShachar Raindel * decremented mapcount is visible. And the old page 32292f38ab2cSShachar Raindel * cannot be reused until after the decremented 32302f38ab2cSShachar Raindel * mapcount is visible. So transitively, TLBs to 32312f38ab2cSShachar Raindel * old page will be flushed before it can be reused. 32322f38ab2cSShachar Raindel */ 323328d41a48SMatthew Wilcox (Oracle) page_remove_rmap(vmf->page, vma, false); 32342f38ab2cSShachar Raindel } 32352f38ab2cSShachar Raindel 32362f38ab2cSShachar Raindel /* Free the old page.. */ 323728d41a48SMatthew Wilcox (Oracle) new_folio = old_folio; 32382f38ab2cSShachar Raindel page_copied = 1; 32393db82b93SHugh Dickins pte_unmap_unlock(vmf->pte, vmf->ptl); 32403db82b93SHugh Dickins } else if (vmf->pte) { 32417df67697SBibo Mao update_mmu_tlb(vma, vmf->address, vmf->pte); 32423db82b93SHugh Dickins pte_unmap_unlock(vmf->pte, vmf->ptl); 32432f38ab2cSShachar Raindel } 32442f38ab2cSShachar Raindel 3245ec8832d0SAlistair Popple mmu_notifier_invalidate_range_end(&range); 32463db82b93SHugh Dickins 32473db82b93SHugh Dickins if (new_folio) 32483db82b93SHugh Dickins folio_put(new_folio); 324928d41a48SMatthew Wilcox (Oracle) if (old_folio) { 3250f4c4a3f4SHuang Ying if (page_copied) 325128d41a48SMatthew Wilcox (Oracle) free_swap_cache(&old_folio->page); 325228d41a48SMatthew Wilcox (Oracle) folio_put(old_folio); 32532f38ab2cSShachar Raindel } 3254662ce1dcSYang Yang 3255662ce1dcSYang Yang delayacct_wpcopy_end(); 3256cb8d8633SDavid Hildenbrand return 0; 32572f38ab2cSShachar Raindel oom: 3258164b06f2SMatthew Wilcox (Oracle) ret = VM_FAULT_OOM; 3259164b06f2SMatthew Wilcox (Oracle) out: 326028d41a48SMatthew Wilcox (Oracle) if (old_folio) 326128d41a48SMatthew Wilcox (Oracle) folio_put(old_folio); 3262662ce1dcSYang Yang 3263662ce1dcSYang Yang delayacct_wpcopy_end(); 3264164b06f2SMatthew Wilcox (Oracle) return ret; 32652f38ab2cSShachar Raindel } 32662f38ab2cSShachar Raindel 326766a6197cSJan Kara /** 326866a6197cSJan Kara * finish_mkwrite_fault - finish page fault for a shared mapping, making PTE 326966a6197cSJan Kara * writeable once the page is prepared 327066a6197cSJan Kara * 327166a6197cSJan Kara * @vmf: structure describing the fault 3272a86bc96bSKefeng Wang * @folio: the folio of vmf->page 327366a6197cSJan Kara * 327466a6197cSJan Kara * This function handles all that is needed to finish a write page fault in a 327566a6197cSJan Kara * shared mapping due to PTE being read-only once the mapped page is prepared. 3276a862f68aSMike Rapoport * It handles locking of PTE and modifying it. 327766a6197cSJan Kara * 327866a6197cSJan Kara * The function expects the page to be locked or other protection against 327966a6197cSJan Kara * concurrent faults / writeback (such as DAX radix tree locks). 3280a862f68aSMike Rapoport * 32812797e79fSLiu Xiang * Return: %0 on success, %VM_FAULT_NOPAGE when PTE got changed before 3282a862f68aSMike Rapoport * we acquired PTE lock. 328366a6197cSJan Kara */ 3284a86bc96bSKefeng Wang static vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf, struct folio *folio) 328566a6197cSJan Kara { 328666a6197cSJan Kara WARN_ON_ONCE(!(vmf->vma->vm_flags & VM_SHARED)); 328766a6197cSJan Kara vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, vmf->address, 328866a6197cSJan Kara &vmf->ptl); 32893db82b93SHugh Dickins if (!vmf->pte) 32903db82b93SHugh Dickins return VM_FAULT_NOPAGE; 329166a6197cSJan Kara /* 329266a6197cSJan Kara * We might have raced with another page fault while we released the 329366a6197cSJan Kara * pte_offset_map_lock. 329466a6197cSJan Kara */ 3295c33c7948SRyan Roberts if (!pte_same(ptep_get(vmf->pte), vmf->orig_pte)) { 32967df67697SBibo Mao update_mmu_tlb(vmf->vma, vmf->address, vmf->pte); 329766a6197cSJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3298a19e2553SJan Kara return VM_FAULT_NOPAGE; 329966a6197cSJan Kara } 3300a86bc96bSKefeng Wang wp_page_reuse(vmf, folio); 3301a19e2553SJan Kara return 0; 330266a6197cSJan Kara } 330366a6197cSJan Kara 3304dd906184SBoaz Harrosh /* 3305dd906184SBoaz Harrosh * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED 3306dd906184SBoaz Harrosh * mapping 3307dd906184SBoaz Harrosh */ 33082b740303SSouptick Joarder static vm_fault_t wp_pfn_shared(struct vm_fault *vmf) 3309dd906184SBoaz Harrosh { 331082b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3311bae473a4SKirill A. Shutemov 3312dd906184SBoaz Harrosh if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) { 33132b740303SSouptick Joarder vm_fault_t ret; 3314dd906184SBoaz Harrosh 331582b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 33164a68fef1SMatthew Wilcox (Oracle) ret = vmf_can_call_fault(vmf); 33174a68fef1SMatthew Wilcox (Oracle) if (ret) 33184a68fef1SMatthew Wilcox (Oracle) return ret; 3319063e60d8SMatthew Wilcox (Oracle) 3320fe82221fSJan Kara vmf->flags |= FAULT_FLAG_MKWRITE; 332111bac800SDave Jiang ret = vma->vm_ops->pfn_mkwrite(vmf); 33222f89dc12SJan Kara if (ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)) 3323dd906184SBoaz Harrosh return ret; 3324a86bc96bSKefeng Wang return finish_mkwrite_fault(vmf, NULL); 3325dd906184SBoaz Harrosh } 3326a86bc96bSKefeng Wang wp_page_reuse(vmf, NULL); 3327cb8d8633SDavid Hildenbrand return 0; 3328dd906184SBoaz Harrosh } 3329dd906184SBoaz Harrosh 33305a97858bSSidhartha Kumar static vm_fault_t wp_page_shared(struct vm_fault *vmf, struct folio *folio) 333182b0f8c3SJan Kara __releases(vmf->ptl) 333293e478d4SShachar Raindel { 333382b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3334cb8d8633SDavid Hildenbrand vm_fault_t ret = 0; 333593e478d4SShachar Raindel 33365a97858bSSidhartha Kumar folio_get(folio); 333793e478d4SShachar Raindel 333893e478d4SShachar Raindel if (vma->vm_ops && vma->vm_ops->page_mkwrite) { 33392b740303SSouptick Joarder vm_fault_t tmp; 334093e478d4SShachar Raindel 334182b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 33424a68fef1SMatthew Wilcox (Oracle) tmp = vmf_can_call_fault(vmf); 33434a68fef1SMatthew Wilcox (Oracle) if (tmp) { 3344063e60d8SMatthew Wilcox (Oracle) folio_put(folio); 33454a68fef1SMatthew Wilcox (Oracle) return tmp; 3346063e60d8SMatthew Wilcox (Oracle) } 3347063e60d8SMatthew Wilcox (Oracle) 334886aa6998SSidhartha Kumar tmp = do_page_mkwrite(vmf, folio); 334993e478d4SShachar Raindel if (unlikely(!tmp || (tmp & 335093e478d4SShachar Raindel (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { 33515a97858bSSidhartha Kumar folio_put(folio); 335293e478d4SShachar Raindel return tmp; 335393e478d4SShachar Raindel } 3354a86bc96bSKefeng Wang tmp = finish_mkwrite_fault(vmf, folio); 3355a19e2553SJan Kara if (unlikely(tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) { 33565a97858bSSidhartha Kumar folio_unlock(folio); 33575a97858bSSidhartha Kumar folio_put(folio); 335866a6197cSJan Kara return tmp; 335993e478d4SShachar Raindel } 336066a6197cSJan Kara } else { 3361a86bc96bSKefeng Wang wp_page_reuse(vmf, folio); 33625a97858bSSidhartha Kumar folio_lock(folio); 336393e478d4SShachar Raindel } 336489b15332SJohannes Weiner ret |= fault_dirty_shared_page(vmf); 33655a97858bSSidhartha Kumar folio_put(folio); 336693e478d4SShachar Raindel 336789b15332SJohannes Weiner return ret; 336893e478d4SShachar Raindel } 336993e478d4SShachar Raindel 3370dec078ccSDavid Hildenbrand static bool wp_can_reuse_anon_folio(struct folio *folio, 3371dec078ccSDavid Hildenbrand struct vm_area_struct *vma) 3372dec078ccSDavid Hildenbrand { 3373dec078ccSDavid Hildenbrand /* 3374dec078ccSDavid Hildenbrand * We have to verify under folio lock: these early checks are 3375dec078ccSDavid Hildenbrand * just an optimization to avoid locking the folio and freeing 3376dec078ccSDavid Hildenbrand * the swapcache if there is little hope that we can reuse. 3377dec078ccSDavid Hildenbrand * 3378dec078ccSDavid Hildenbrand * KSM doesn't necessarily raise the folio refcount. 3379dec078ccSDavid Hildenbrand */ 3380dec078ccSDavid Hildenbrand if (folio_test_ksm(folio) || folio_ref_count(folio) > 3) 3381dec078ccSDavid Hildenbrand return false; 3382dec078ccSDavid Hildenbrand if (!folio_test_lru(folio)) 3383dec078ccSDavid Hildenbrand /* 3384dec078ccSDavid Hildenbrand * We cannot easily detect+handle references from 3385dec078ccSDavid Hildenbrand * remote LRU caches or references to LRU folios. 3386dec078ccSDavid Hildenbrand */ 3387dec078ccSDavid Hildenbrand lru_add_drain(); 3388dec078ccSDavid Hildenbrand if (folio_ref_count(folio) > 1 + folio_test_swapcache(folio)) 3389dec078ccSDavid Hildenbrand return false; 3390dec078ccSDavid Hildenbrand if (!folio_trylock(folio)) 3391dec078ccSDavid Hildenbrand return false; 3392dec078ccSDavid Hildenbrand if (folio_test_swapcache(folio)) 3393dec078ccSDavid Hildenbrand folio_free_swap(folio); 3394dec078ccSDavid Hildenbrand if (folio_test_ksm(folio) || folio_ref_count(folio) != 1) { 3395dec078ccSDavid Hildenbrand folio_unlock(folio); 3396dec078ccSDavid Hildenbrand return false; 3397dec078ccSDavid Hildenbrand } 3398dec078ccSDavid Hildenbrand /* 3399dec078ccSDavid Hildenbrand * Ok, we've got the only folio reference from our mapping 3400dec078ccSDavid Hildenbrand * and the folio is locked, it's dark out, and we're wearing 3401dec078ccSDavid Hildenbrand * sunglasses. Hit it. 3402dec078ccSDavid Hildenbrand */ 3403dec078ccSDavid Hildenbrand folio_move_anon_rmap(folio, vma); 3404dec078ccSDavid Hildenbrand folio_unlock(folio); 3405dec078ccSDavid Hildenbrand return true; 3406dec078ccSDavid Hildenbrand } 3407dec078ccSDavid Hildenbrand 34082f38ab2cSShachar Raindel /* 3409c89357e2SDavid Hildenbrand * This routine handles present pages, when 3410c89357e2SDavid Hildenbrand * * users try to write to a shared page (FAULT_FLAG_WRITE) 3411c89357e2SDavid Hildenbrand * * GUP wants to take a R/O pin on a possibly shared anonymous page 3412c89357e2SDavid Hildenbrand * (FAULT_FLAG_UNSHARE) 3413c89357e2SDavid Hildenbrand * 3414c89357e2SDavid Hildenbrand * It is done by copying the page to a new address and decrementing the 3415c89357e2SDavid Hildenbrand * shared-page counter for the old page. 34161da177e4SLinus Torvalds * 34171da177e4SLinus Torvalds * Note that this routine assumes that the protection checks have been 34181da177e4SLinus Torvalds * done by the caller (the low-level page fault routine in most cases). 3419c89357e2SDavid Hildenbrand * Thus, with FAULT_FLAG_WRITE, we can safely just mark it writable once we've 3420c89357e2SDavid Hildenbrand * done any necessary COW. 34211da177e4SLinus Torvalds * 3422c89357e2SDavid Hildenbrand * In case of FAULT_FLAG_WRITE, we also mark the page dirty at this point even 3423c89357e2SDavid Hildenbrand * though the page will change only once the write actually happens. This 3424c89357e2SDavid Hildenbrand * avoids a few races, and potentially makes it more efficient. 34251da177e4SLinus Torvalds * 3426c1e8d7c6SMichel Lespinasse * We enter with non-exclusive mmap_lock (to exclude vma changes, 34278f4e2101SHugh Dickins * but allow concurrent faults), with pte both mapped and locked. 3428c1e8d7c6SMichel Lespinasse * We return with mmap_lock still held, but pte unmapped and unlocked. 34291da177e4SLinus Torvalds */ 34302b740303SSouptick Joarder static vm_fault_t do_wp_page(struct vm_fault *vmf) 343182b0f8c3SJan Kara __releases(vmf->ptl) 34321da177e4SLinus Torvalds { 3433c89357e2SDavid Hildenbrand const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE; 343482b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3435b9086fdeSDavid Hildenbrand struct folio *folio = NULL; 3436d61ea1cbSPeter Xu pte_t pte; 34371da177e4SLinus Torvalds 3438c89357e2SDavid Hildenbrand if (likely(!unshare)) { 3439c33c7948SRyan Roberts if (userfaultfd_pte_wp(vma, ptep_get(vmf->pte))) { 3440d61ea1cbSPeter Xu if (!userfaultfd_wp_async(vma)) { 3441529b930bSAndrea Arcangeli pte_unmap_unlock(vmf->pte, vmf->ptl); 3442529b930bSAndrea Arcangeli return handle_userfault(vmf, VM_UFFD_WP); 3443529b930bSAndrea Arcangeli } 3444529b930bSAndrea Arcangeli 34456ce64428SNadav Amit /* 3446d61ea1cbSPeter Xu * Nothing needed (cache flush, TLB invalidations, 3447d61ea1cbSPeter Xu * etc.) because we're only removing the uffd-wp bit, 3448d61ea1cbSPeter Xu * which is completely invisible to the user. 3449d61ea1cbSPeter Xu */ 3450d61ea1cbSPeter Xu pte = pte_clear_uffd_wp(ptep_get(vmf->pte)); 3451d61ea1cbSPeter Xu 3452d61ea1cbSPeter Xu set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte); 3453d61ea1cbSPeter Xu /* 3454d61ea1cbSPeter Xu * Update this to be prepared for following up CoW 3455d61ea1cbSPeter Xu * handling 3456d61ea1cbSPeter Xu */ 3457d61ea1cbSPeter Xu vmf->orig_pte = pte; 3458d61ea1cbSPeter Xu } 3459d61ea1cbSPeter Xu 3460d61ea1cbSPeter Xu /* 34616ce64428SNadav Amit * Userfaultfd write-protect can defer flushes. Ensure the TLB 34626ce64428SNadav Amit * is flushed in this case before copying. 34636ce64428SNadav Amit */ 34646ce64428SNadav Amit if (unlikely(userfaultfd_wp(vmf->vma) && 34656ce64428SNadav Amit mm_tlb_flush_pending(vmf->vma->vm_mm))) 34666ce64428SNadav Amit flush_tlb_page(vmf->vma, vmf->address); 3467c89357e2SDavid Hildenbrand } 34686ce64428SNadav Amit 3469a41b70d6SJan Kara vmf->page = vm_normal_page(vma, vmf->address, vmf->orig_pte); 3470c89357e2SDavid Hildenbrand 34715a97858bSSidhartha Kumar if (vmf->page) 34725a97858bSSidhartha Kumar folio = page_folio(vmf->page); 34735a97858bSSidhartha Kumar 3474251b97f5SPeter Zijlstra /* 3475b9086fdeSDavid Hildenbrand * Shared mapping: we are guaranteed to have VM_WRITE and 3476b9086fdeSDavid Hildenbrand * FAULT_FLAG_WRITE set at this point. 3477b9086fdeSDavid Hildenbrand */ 3478b9086fdeSDavid Hildenbrand if (vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) { 3479b9086fdeSDavid Hildenbrand /* 348064e45507SPeter Feiner * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a 348164e45507SPeter Feiner * VM_PFNMAP VMA. 3482251b97f5SPeter Zijlstra * 3483251b97f5SPeter Zijlstra * We should not cow pages in a shared writeable mapping. 3484dd906184SBoaz Harrosh * Just mark the pages writable and/or call ops->pfn_mkwrite. 3485251b97f5SPeter Zijlstra */ 3486b9086fdeSDavid Hildenbrand if (!vmf->page) 34872994302bSJan Kara return wp_pfn_shared(vmf); 34885a97858bSSidhartha Kumar return wp_page_shared(vmf, folio); 3489251b97f5SPeter Zijlstra } 34901da177e4SLinus Torvalds 3491b9086fdeSDavid Hildenbrand /* 3492b9086fdeSDavid Hildenbrand * Private mapping: create an exclusive anonymous page copy if reuse 3493b9086fdeSDavid Hildenbrand * is impossible. We might miss VM_WRITE for FOLL_FORCE handling. 349453a05ad9SDavid Hildenbrand * 3495dec078ccSDavid Hildenbrand * If we encounter a page that is marked exclusive, we must reuse 3496dec078ccSDavid Hildenbrand * the page without further checks. 349753a05ad9SDavid Hildenbrand */ 3498dec078ccSDavid Hildenbrand if (folio && folio_test_anon(folio) && 3499dec078ccSDavid Hildenbrand (PageAnonExclusive(vmf->page) || wp_can_reuse_anon_folio(folio, vma))) { 3500dec078ccSDavid Hildenbrand if (!PageAnonExclusive(vmf->page)) 35015ca43289SDavid Hildenbrand SetPageAnonExclusive(vmf->page); 3502c89357e2SDavid Hildenbrand if (unlikely(unshare)) { 3503c89357e2SDavid Hildenbrand pte_unmap_unlock(vmf->pte, vmf->ptl); 3504c89357e2SDavid Hildenbrand return 0; 3505c89357e2SDavid Hildenbrand } 3506a86bc96bSKefeng Wang wp_page_reuse(vmf, folio); 3507cb8d8633SDavid Hildenbrand return 0; 35081da177e4SLinus Torvalds } 35091da177e4SLinus Torvalds /* 35101da177e4SLinus Torvalds * Ok, we need to copy. Oh, well.. 35111da177e4SLinus Torvalds */ 3512b9086fdeSDavid Hildenbrand if (folio) 3513b9086fdeSDavid Hildenbrand folio_get(folio); 351428766805SShachar Raindel 351582b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 351694bfe85bSYang Yang #ifdef CONFIG_KSM 3517b9086fdeSDavid Hildenbrand if (folio && folio_test_ksm(folio)) 351894bfe85bSYang Yang count_vm_event(COW_KSM); 351994bfe85bSYang Yang #endif 3520a41b70d6SJan Kara return wp_page_copy(vmf); 35211da177e4SLinus Torvalds } 35221da177e4SLinus Torvalds 352397a89413SPeter Zijlstra static void unmap_mapping_range_vma(struct vm_area_struct *vma, 35241da177e4SLinus Torvalds unsigned long start_addr, unsigned long end_addr, 35251da177e4SLinus Torvalds struct zap_details *details) 35261da177e4SLinus Torvalds { 3527f5cc4eefSAl Viro zap_page_range_single(vma, start_addr, end_addr - start_addr, details); 35281da177e4SLinus Torvalds } 35291da177e4SLinus Torvalds 3530f808c13fSDavidlohr Bueso static inline void unmap_mapping_range_tree(struct rb_root_cached *root, 3531232a6a1cSPeter Xu pgoff_t first_index, 3532232a6a1cSPeter Xu pgoff_t last_index, 35331da177e4SLinus Torvalds struct zap_details *details) 35341da177e4SLinus Torvalds { 35351da177e4SLinus Torvalds struct vm_area_struct *vma; 35361da177e4SLinus Torvalds pgoff_t vba, vea, zba, zea; 35371da177e4SLinus Torvalds 3538232a6a1cSPeter Xu vma_interval_tree_foreach(vma, root, first_index, last_index) { 35391da177e4SLinus Torvalds vba = vma->vm_pgoff; 3540d6e93217SLibin vea = vba + vma_pages(vma) - 1; 3541f9871da9SMiaohe Lin zba = max(first_index, vba); 3542f9871da9SMiaohe Lin zea = min(last_index, vea); 35431da177e4SLinus Torvalds 354497a89413SPeter Zijlstra unmap_mapping_range_vma(vma, 35451da177e4SLinus Torvalds ((zba - vba) << PAGE_SHIFT) + vma->vm_start, 35461da177e4SLinus Torvalds ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start, 354797a89413SPeter Zijlstra details); 35481da177e4SLinus Torvalds } 35491da177e4SLinus Torvalds } 35501da177e4SLinus Torvalds 35511da177e4SLinus Torvalds /** 35523506659eSMatthew Wilcox (Oracle) * unmap_mapping_folio() - Unmap single folio from processes. 35533506659eSMatthew Wilcox (Oracle) * @folio: The locked folio to be unmapped. 355422061a1fSHugh Dickins * 35553506659eSMatthew Wilcox (Oracle) * Unmap this folio from any userspace process which still has it mmaped. 355622061a1fSHugh Dickins * Typically, for efficiency, the range of nearby pages has already been 355722061a1fSHugh Dickins * unmapped by unmap_mapping_pages() or unmap_mapping_range(). But once 35583506659eSMatthew Wilcox (Oracle) * truncation or invalidation holds the lock on a folio, it may find that 35593506659eSMatthew Wilcox (Oracle) * the page has been remapped again: and then uses unmap_mapping_folio() 356022061a1fSHugh Dickins * to unmap it finally. 356122061a1fSHugh Dickins */ 35623506659eSMatthew Wilcox (Oracle) void unmap_mapping_folio(struct folio *folio) 356322061a1fSHugh Dickins { 35643506659eSMatthew Wilcox (Oracle) struct address_space *mapping = folio->mapping; 356522061a1fSHugh Dickins struct zap_details details = { }; 3566232a6a1cSPeter Xu pgoff_t first_index; 3567232a6a1cSPeter Xu pgoff_t last_index; 356822061a1fSHugh Dickins 35693506659eSMatthew Wilcox (Oracle) VM_BUG_ON(!folio_test_locked(folio)); 357022061a1fSHugh Dickins 35713506659eSMatthew Wilcox (Oracle) first_index = folio->index; 357287b11f86SSidhartha Kumar last_index = folio_next_index(folio) - 1; 3573232a6a1cSPeter Xu 35742e148f1eSPeter Xu details.even_cows = false; 35753506659eSMatthew Wilcox (Oracle) details.single_folio = folio; 3576999dad82SPeter Xu details.zap_flags = ZAP_FLAG_DROP_MARKER; 357722061a1fSHugh Dickins 35782c865995SHugh Dickins i_mmap_lock_read(mapping); 357922061a1fSHugh Dickins if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root))) 3580232a6a1cSPeter Xu unmap_mapping_range_tree(&mapping->i_mmap, first_index, 3581232a6a1cSPeter Xu last_index, &details); 35822c865995SHugh Dickins i_mmap_unlock_read(mapping); 358322061a1fSHugh Dickins } 358422061a1fSHugh Dickins 358522061a1fSHugh Dickins /** 3586977fbdcdSMatthew Wilcox * unmap_mapping_pages() - Unmap pages from processes. 3587977fbdcdSMatthew Wilcox * @mapping: The address space containing pages to be unmapped. 3588977fbdcdSMatthew Wilcox * @start: Index of first page to be unmapped. 3589977fbdcdSMatthew Wilcox * @nr: Number of pages to be unmapped. 0 to unmap to end of file. 3590977fbdcdSMatthew Wilcox * @even_cows: Whether to unmap even private COWed pages. 3591977fbdcdSMatthew Wilcox * 3592977fbdcdSMatthew Wilcox * Unmap the pages in this address space from any userspace process which 3593977fbdcdSMatthew Wilcox * has them mmaped. Generally, you want to remove COWed pages as well when 3594977fbdcdSMatthew Wilcox * a file is being truncated, but not when invalidating pages from the page 3595977fbdcdSMatthew Wilcox * cache. 3596977fbdcdSMatthew Wilcox */ 3597977fbdcdSMatthew Wilcox void unmap_mapping_pages(struct address_space *mapping, pgoff_t start, 3598977fbdcdSMatthew Wilcox pgoff_t nr, bool even_cows) 3599977fbdcdSMatthew Wilcox { 3600977fbdcdSMatthew Wilcox struct zap_details details = { }; 3601232a6a1cSPeter Xu pgoff_t first_index = start; 3602232a6a1cSPeter Xu pgoff_t last_index = start + nr - 1; 3603977fbdcdSMatthew Wilcox 36042e148f1eSPeter Xu details.even_cows = even_cows; 3605232a6a1cSPeter Xu if (last_index < first_index) 3606232a6a1cSPeter Xu last_index = ULONG_MAX; 3607977fbdcdSMatthew Wilcox 36082c865995SHugh Dickins i_mmap_lock_read(mapping); 3609977fbdcdSMatthew Wilcox if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root))) 3610232a6a1cSPeter Xu unmap_mapping_range_tree(&mapping->i_mmap, first_index, 3611232a6a1cSPeter Xu last_index, &details); 36122c865995SHugh Dickins i_mmap_unlock_read(mapping); 3613977fbdcdSMatthew Wilcox } 36146e0e99d5SDavid Howells EXPORT_SYMBOL_GPL(unmap_mapping_pages); 3615977fbdcdSMatthew Wilcox 3616977fbdcdSMatthew Wilcox /** 36178a5f14a2SKirill A. Shutemov * unmap_mapping_range - unmap the portion of all mmaps in the specified 3618977fbdcdSMatthew Wilcox * address_space corresponding to the specified byte range in the underlying 36198a5f14a2SKirill A. Shutemov * file. 36208a5f14a2SKirill A. Shutemov * 36213d41088fSMartin Waitz * @mapping: the address space containing mmaps to be unmapped. 36221da177e4SLinus Torvalds * @holebegin: byte in first page to unmap, relative to the start of 36231da177e4SLinus Torvalds * the underlying file. This will be rounded down to a PAGE_SIZE 362425d9e2d1Snpiggin@suse.de * boundary. Note that this is different from truncate_pagecache(), which 36251da177e4SLinus Torvalds * must keep the partial page. In contrast, we must get rid of 36261da177e4SLinus Torvalds * partial pages. 36271da177e4SLinus Torvalds * @holelen: size of prospective hole in bytes. This will be rounded 36281da177e4SLinus Torvalds * up to a PAGE_SIZE boundary. A holelen of zero truncates to the 36291da177e4SLinus Torvalds * end of the file. 36301da177e4SLinus Torvalds * @even_cows: 1 when truncating a file, unmap even private COWed pages; 36311da177e4SLinus Torvalds * but 0 when invalidating pagecache, don't throw away private data. 36321da177e4SLinus Torvalds */ 36331da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping, 36341da177e4SLinus Torvalds loff_t const holebegin, loff_t const holelen, int even_cows) 36351da177e4SLinus Torvalds { 36361da177e4SLinus Torvalds pgoff_t hba = holebegin >> PAGE_SHIFT; 36371da177e4SLinus Torvalds pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 36381da177e4SLinus Torvalds 36391da177e4SLinus Torvalds /* Check for overflow. */ 36401da177e4SLinus Torvalds if (sizeof(holelen) > sizeof(hlen)) { 36411da177e4SLinus Torvalds long long holeend = 36421da177e4SLinus Torvalds (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 36431da177e4SLinus Torvalds if (holeend & ~(long long)ULONG_MAX) 36441da177e4SLinus Torvalds hlen = ULONG_MAX - hba + 1; 36451da177e4SLinus Torvalds } 36461da177e4SLinus Torvalds 3647977fbdcdSMatthew Wilcox unmap_mapping_pages(mapping, hba, hlen, even_cows); 36481da177e4SLinus Torvalds } 36491da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range); 36501da177e4SLinus Torvalds 36511da177e4SLinus Torvalds /* 3652b756a3b5SAlistair Popple * Restore a potential device exclusive pte to a working pte entry 3653b756a3b5SAlistair Popple */ 3654b756a3b5SAlistair Popple static vm_fault_t remove_device_exclusive_entry(struct vm_fault *vmf) 3655b756a3b5SAlistair Popple { 365619672a9eSMatthew Wilcox (Oracle) struct folio *folio = page_folio(vmf->page); 3657b756a3b5SAlistair Popple struct vm_area_struct *vma = vmf->vma; 3658b756a3b5SAlistair Popple struct mmu_notifier_range range; 3659fdc724d6SSuren Baghdasaryan vm_fault_t ret; 3660b756a3b5SAlistair Popple 36617c7b9629SAlistair Popple /* 36627c7b9629SAlistair Popple * We need a reference to lock the folio because we don't hold 36637c7b9629SAlistair Popple * the PTL so a racing thread can remove the device-exclusive 36647c7b9629SAlistair Popple * entry and unmap it. If the folio is free the entry must 36657c7b9629SAlistair Popple * have been removed already. If it happens to have already 36667c7b9629SAlistair Popple * been re-allocated after being freed all we do is lock and 36677c7b9629SAlistair Popple * unlock it. 36687c7b9629SAlistair Popple */ 36697c7b9629SAlistair Popple if (!folio_try_get(folio)) 36707c7b9629SAlistair Popple return 0; 36717c7b9629SAlistair Popple 3672fdc724d6SSuren Baghdasaryan ret = folio_lock_or_retry(folio, vmf); 3673fdc724d6SSuren Baghdasaryan if (ret) { 36747c7b9629SAlistair Popple folio_put(folio); 3675fdc724d6SSuren Baghdasaryan return ret; 36767c7b9629SAlistair Popple } 36777d4a8be0SAlistair Popple mmu_notifier_range_init_owner(&range, MMU_NOTIFY_EXCLUSIVE, 0, 3678b756a3b5SAlistair Popple vma->vm_mm, vmf->address & PAGE_MASK, 3679b756a3b5SAlistair Popple (vmf->address & PAGE_MASK) + PAGE_SIZE, NULL); 3680b756a3b5SAlistair Popple mmu_notifier_invalidate_range_start(&range); 3681b756a3b5SAlistair Popple 3682b756a3b5SAlistair Popple vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 3683b756a3b5SAlistair Popple &vmf->ptl); 3684c33c7948SRyan Roberts if (likely(vmf->pte && pte_same(ptep_get(vmf->pte), vmf->orig_pte))) 368519672a9eSMatthew Wilcox (Oracle) restore_exclusive_pte(vma, vmf->page, vmf->address, vmf->pte); 3686b756a3b5SAlistair Popple 36873db82b93SHugh Dickins if (vmf->pte) 3688b756a3b5SAlistair Popple pte_unmap_unlock(vmf->pte, vmf->ptl); 368919672a9eSMatthew Wilcox (Oracle) folio_unlock(folio); 36907c7b9629SAlistair Popple folio_put(folio); 3691b756a3b5SAlistair Popple 3692b756a3b5SAlistair Popple mmu_notifier_invalidate_range_end(&range); 3693b756a3b5SAlistair Popple return 0; 3694b756a3b5SAlistair Popple } 3695b756a3b5SAlistair Popple 3696a160e537SMatthew Wilcox (Oracle) static inline bool should_try_to_free_swap(struct folio *folio, 3697c145e0b4SDavid Hildenbrand struct vm_area_struct *vma, 3698c145e0b4SDavid Hildenbrand unsigned int fault_flags) 3699c145e0b4SDavid Hildenbrand { 3700a160e537SMatthew Wilcox (Oracle) if (!folio_test_swapcache(folio)) 3701c145e0b4SDavid Hildenbrand return false; 37029202d527SMatthew Wilcox (Oracle) if (mem_cgroup_swap_full(folio) || (vma->vm_flags & VM_LOCKED) || 3703a160e537SMatthew Wilcox (Oracle) folio_test_mlocked(folio)) 3704c145e0b4SDavid Hildenbrand return true; 3705c145e0b4SDavid Hildenbrand /* 3706c145e0b4SDavid Hildenbrand * If we want to map a page that's in the swapcache writable, we 3707c145e0b4SDavid Hildenbrand * have to detect via the refcount if we're really the exclusive 3708c145e0b4SDavid Hildenbrand * user. Try freeing the swapcache to get rid of the swapcache 3709c145e0b4SDavid Hildenbrand * reference only in case it's likely that we'll be the exlusive user. 3710c145e0b4SDavid Hildenbrand */ 3711a160e537SMatthew Wilcox (Oracle) return (fault_flags & FAULT_FLAG_WRITE) && !folio_test_ksm(folio) && 3712a160e537SMatthew Wilcox (Oracle) folio_ref_count(folio) == 2; 3713c145e0b4SDavid Hildenbrand } 3714c145e0b4SDavid Hildenbrand 37159c28a205SPeter Xu static vm_fault_t pte_marker_clear(struct vm_fault *vmf) 37169c28a205SPeter Xu { 37179c28a205SPeter Xu vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, 37189c28a205SPeter Xu vmf->address, &vmf->ptl); 37193db82b93SHugh Dickins if (!vmf->pte) 37203db82b93SHugh Dickins return 0; 37219c28a205SPeter Xu /* 37229c28a205SPeter Xu * Be careful so that we will only recover a special uffd-wp pte into a 37239c28a205SPeter Xu * none pte. Otherwise it means the pte could have changed, so retry. 37247e3ce3f8SPeter Xu * 37257e3ce3f8SPeter Xu * This should also cover the case where e.g. the pte changed 3726af19487fSAxel Rasmussen * quickly from a PTE_MARKER_UFFD_WP into PTE_MARKER_POISONED. 37277e3ce3f8SPeter Xu * So is_pte_marker() check is not enough to safely drop the pte. 37289c28a205SPeter Xu */ 3729c33c7948SRyan Roberts if (pte_same(vmf->orig_pte, ptep_get(vmf->pte))) 37309c28a205SPeter Xu pte_clear(vmf->vma->vm_mm, vmf->address, vmf->pte); 37319c28a205SPeter Xu pte_unmap_unlock(vmf->pte, vmf->ptl); 37329c28a205SPeter Xu return 0; 37339c28a205SPeter Xu } 37349c28a205SPeter Xu 37352bad466cSPeter Xu static vm_fault_t do_pte_missing(struct vm_fault *vmf) 37362bad466cSPeter Xu { 37372bad466cSPeter Xu if (vma_is_anonymous(vmf->vma)) 37382bad466cSPeter Xu return do_anonymous_page(vmf); 37392bad466cSPeter Xu else 37402bad466cSPeter Xu return do_fault(vmf); 37412bad466cSPeter Xu } 37422bad466cSPeter Xu 37439c28a205SPeter Xu /* 37449c28a205SPeter Xu * This is actually a page-missing access, but with uffd-wp special pte 37459c28a205SPeter Xu * installed. It means this pte was wr-protected before being unmapped. 37469c28a205SPeter Xu */ 37479c28a205SPeter Xu static vm_fault_t pte_marker_handle_uffd_wp(struct vm_fault *vmf) 37489c28a205SPeter Xu { 37499c28a205SPeter Xu /* 37509c28a205SPeter Xu * Just in case there're leftover special ptes even after the region 37517a079ba2SPeter Xu * got unregistered - we can simply clear them. 37529c28a205SPeter Xu */ 37532bad466cSPeter Xu if (unlikely(!userfaultfd_wp(vmf->vma))) 37549c28a205SPeter Xu return pte_marker_clear(vmf); 37559c28a205SPeter Xu 37562bad466cSPeter Xu return do_pte_missing(vmf); 37579c28a205SPeter Xu } 37589c28a205SPeter Xu 37595c041f5dSPeter Xu static vm_fault_t handle_pte_marker(struct vm_fault *vmf) 37605c041f5dSPeter Xu { 37615c041f5dSPeter Xu swp_entry_t entry = pte_to_swp_entry(vmf->orig_pte); 37625c041f5dSPeter Xu unsigned long marker = pte_marker_get(entry); 37635c041f5dSPeter Xu 37645c041f5dSPeter Xu /* 3765ca92ea3dSPeter Xu * PTE markers should never be empty. If anything weird happened, 3766ca92ea3dSPeter Xu * the best thing to do is to kill the process along with its mm. 37675c041f5dSPeter Xu */ 3768ca92ea3dSPeter Xu if (WARN_ON_ONCE(!marker)) 37695c041f5dSPeter Xu return VM_FAULT_SIGBUS; 37705c041f5dSPeter Xu 377115520a3fSPeter Xu /* Higher priority than uffd-wp when data corrupted */ 3772af19487fSAxel Rasmussen if (marker & PTE_MARKER_POISONED) 3773af19487fSAxel Rasmussen return VM_FAULT_HWPOISON; 377415520a3fSPeter Xu 37759c28a205SPeter Xu if (pte_marker_entry_uffd_wp(entry)) 37769c28a205SPeter Xu return pte_marker_handle_uffd_wp(vmf); 37779c28a205SPeter Xu 37789c28a205SPeter Xu /* This is an unknown pte marker */ 37799c28a205SPeter Xu return VM_FAULT_SIGBUS; 37805c041f5dSPeter Xu } 37815c041f5dSPeter Xu 3782b756a3b5SAlistair Popple /* 3783c1e8d7c6SMichel Lespinasse * We enter with non-exclusive mmap_lock (to exclude vma changes, 37848f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 37859a95f3cfSPaul Cassella * We return with pte unmapped and unlocked. 37869a95f3cfSPaul Cassella * 3787c1e8d7c6SMichel Lespinasse * We return with the mmap_lock locked or unlocked in the same cases 37889a95f3cfSPaul Cassella * as does filemap_fault(). 37891da177e4SLinus Torvalds */ 37902b740303SSouptick Joarder vm_fault_t do_swap_page(struct vm_fault *vmf) 37911da177e4SLinus Torvalds { 379282b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3793d4f9565aSMatthew Wilcox (Oracle) struct folio *swapcache, *folio = NULL; 3794d4f9565aSMatthew Wilcox (Oracle) struct page *page; 37952799e775SMiaohe Lin struct swap_info_struct *si = NULL; 379614f9135dSDavid Hildenbrand rmap_t rmap_flags = RMAP_NONE; 37971493a191SDavid Hildenbrand bool exclusive = false; 379865500d23SHugh Dickins swp_entry_t entry; 37991da177e4SLinus Torvalds pte_t pte; 38002b740303SSouptick Joarder vm_fault_t ret = 0; 3801aae466b0SJoonsoo Kim void *shadow = NULL; 38021da177e4SLinus Torvalds 38032ca99358SPeter Xu if (!pte_unmap_same(vmf)) 38048f4e2101SHugh Dickins goto out; 380565500d23SHugh Dickins 38062994302bSJan Kara entry = pte_to_swp_entry(vmf->orig_pte); 3807d1737fdbSAndi Kleen if (unlikely(non_swap_entry(entry))) { 38080697212aSChristoph Lameter if (is_migration_entry(entry)) { 380982b0f8c3SJan Kara migration_entry_wait(vma->vm_mm, vmf->pmd, 381082b0f8c3SJan Kara vmf->address); 3811b756a3b5SAlistair Popple } else if (is_device_exclusive_entry(entry)) { 3812b756a3b5SAlistair Popple vmf->page = pfn_swap_entry_to_page(entry); 3813b756a3b5SAlistair Popple ret = remove_device_exclusive_entry(vmf); 38145042db43SJérôme Glisse } else if (is_device_private_entry(entry)) { 38151235ccd0SSuren Baghdasaryan if (vmf->flags & FAULT_FLAG_VMA_LOCK) { 38161235ccd0SSuren Baghdasaryan /* 38171235ccd0SSuren Baghdasaryan * migrate_to_ram is not yet ready to operate 38181235ccd0SSuren Baghdasaryan * under VMA lock. 38191235ccd0SSuren Baghdasaryan */ 38201235ccd0SSuren Baghdasaryan vma_end_read(vma); 38211235ccd0SSuren Baghdasaryan ret = VM_FAULT_RETRY; 38221235ccd0SSuren Baghdasaryan goto out; 38231235ccd0SSuren Baghdasaryan } 38241235ccd0SSuren Baghdasaryan 3825af5cdaf8SAlistair Popple vmf->page = pfn_swap_entry_to_page(entry); 382616ce101dSAlistair Popple vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 382716ce101dSAlistair Popple vmf->address, &vmf->ptl); 38283db82b93SHugh Dickins if (unlikely(!vmf->pte || 3829c33c7948SRyan Roberts !pte_same(ptep_get(vmf->pte), 3830c33c7948SRyan Roberts vmf->orig_pte))) 38313b65f437SRyan Roberts goto unlock; 383216ce101dSAlistair Popple 383316ce101dSAlistair Popple /* 383416ce101dSAlistair Popple * Get a page reference while we know the page can't be 383516ce101dSAlistair Popple * freed. 383616ce101dSAlistair Popple */ 383716ce101dSAlistair Popple get_page(vmf->page); 383816ce101dSAlistair Popple pte_unmap_unlock(vmf->pte, vmf->ptl); 38394a955bedSAlistair Popple ret = vmf->page->pgmap->ops->migrate_to_ram(vmf); 384016ce101dSAlistair Popple put_page(vmf->page); 3841d1737fdbSAndi Kleen } else if (is_hwpoison_entry(entry)) { 3842d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 38435c041f5dSPeter Xu } else if (is_pte_marker_entry(entry)) { 38445c041f5dSPeter Xu ret = handle_pte_marker(vmf); 3845d1737fdbSAndi Kleen } else { 38462994302bSJan Kara print_bad_pte(vma, vmf->address, vmf->orig_pte, NULL); 3847d99be1a8SHugh Dickins ret = VM_FAULT_SIGBUS; 3848d1737fdbSAndi Kleen } 38490697212aSChristoph Lameter goto out; 38500697212aSChristoph Lameter } 38510bcac06fSMinchan Kim 38522799e775SMiaohe Lin /* Prevent swapoff from happening to us. */ 38532799e775SMiaohe Lin si = get_swap_device(entry); 38542799e775SMiaohe Lin if (unlikely(!si)) 38552799e775SMiaohe Lin goto out; 38560bcac06fSMinchan Kim 38575a423081SMatthew Wilcox (Oracle) folio = swap_cache_get_folio(entry, vma, vmf->address); 38585a423081SMatthew Wilcox (Oracle) if (folio) 38595a423081SMatthew Wilcox (Oracle) page = folio_file_page(folio, swp_offset(entry)); 3860d4f9565aSMatthew Wilcox (Oracle) swapcache = folio; 3861f8020772SMinchan Kim 3862d4f9565aSMatthew Wilcox (Oracle) if (!folio) { 3863a449bf58SQian Cai if (data_race(si->flags & SWP_SYNCHRONOUS_IO) && 3864eb085574SHuang Ying __swap_count(entry) == 1) { 38650bcac06fSMinchan Kim /* skip swapcache */ 386663ad4addSMatthew Wilcox (Oracle) folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, 386763ad4addSMatthew Wilcox (Oracle) vma, vmf->address, false); 386863ad4addSMatthew Wilcox (Oracle) page = &folio->page; 386963ad4addSMatthew Wilcox (Oracle) if (folio) { 387063ad4addSMatthew Wilcox (Oracle) __folio_set_locked(folio); 387163ad4addSMatthew Wilcox (Oracle) __folio_set_swapbacked(folio); 38724c6355b2SJohannes Weiner 387365995918SMatthew Wilcox (Oracle) if (mem_cgroup_swapin_charge_folio(folio, 387463ad4addSMatthew Wilcox (Oracle) vma->vm_mm, GFP_KERNEL, 387563ad4addSMatthew Wilcox (Oracle) entry)) { 3876545b1b07SMichal Hocko ret = VM_FAULT_OOM; 38774c6355b2SJohannes Weiner goto out_page; 3878545b1b07SMichal Hocko } 38790add0c77SShakeel Butt mem_cgroup_swapin_uncharge_swap(entry); 38804c6355b2SJohannes Weiner 3881aae466b0SJoonsoo Kim shadow = get_shadow_from_swap_cache(entry); 3882aae466b0SJoonsoo Kim if (shadow) 388363ad4addSMatthew Wilcox (Oracle) workingset_refault(folio, shadow); 38840076f029SJoonsoo Kim 388563ad4addSMatthew Wilcox (Oracle) folio_add_lru(folio); 38860add0c77SShakeel Butt 3887c9bdf768SMatthew Wilcox (Oracle) /* To provide entry to swap_read_folio() */ 38883d2c9087SDavid Hildenbrand folio->swap = entry; 3889c9bdf768SMatthew Wilcox (Oracle) swap_read_folio(folio, true, NULL); 389063ad4addSMatthew Wilcox (Oracle) folio->private = NULL; 38910bcac06fSMinchan Kim } 3892aa8d22a1SMinchan Kim } else { 3893e9e9b7ecSMinchan Kim page = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, 3894e9e9b7ecSMinchan Kim vmf); 389563ad4addSMatthew Wilcox (Oracle) if (page) 389663ad4addSMatthew Wilcox (Oracle) folio = page_folio(page); 3897d4f9565aSMatthew Wilcox (Oracle) swapcache = folio; 38980bcac06fSMinchan Kim } 38990bcac06fSMinchan Kim 3900d4f9565aSMatthew Wilcox (Oracle) if (!folio) { 39011da177e4SLinus Torvalds /* 39028f4e2101SHugh Dickins * Back out if somebody else faulted in this pte 39038f4e2101SHugh Dickins * while we released the pte lock. 39041da177e4SLinus Torvalds */ 390582b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 390682b0f8c3SJan Kara vmf->address, &vmf->ptl); 3907c33c7948SRyan Roberts if (likely(vmf->pte && 3908c33c7948SRyan Roberts pte_same(ptep_get(vmf->pte), vmf->orig_pte))) 39091da177e4SLinus Torvalds ret = VM_FAULT_OOM; 391065500d23SHugh Dickins goto unlock; 39111da177e4SLinus Torvalds } 39121da177e4SLinus Torvalds 39131da177e4SLinus Torvalds /* Had to read the page from swap area: Major fault */ 39141da177e4SLinus Torvalds ret = VM_FAULT_MAJOR; 3915f8891e5eSChristoph Lameter count_vm_event(PGMAJFAULT); 39162262185cSRoman Gushchin count_memcg_event_mm(vma->vm_mm, PGMAJFAULT); 3917d1737fdbSAndi Kleen } else if (PageHWPoison(page)) { 391871f72525SWu Fengguang /* 391971f72525SWu Fengguang * hwpoisoned dirty swapcache pages are kept for killing 392071f72525SWu Fengguang * owner processes (which may be unknown at hwpoison time) 392171f72525SWu Fengguang */ 3922d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 39234779cb31SAndi Kleen goto out_release; 39241da177e4SLinus Torvalds } 39251da177e4SLinus Torvalds 3926fdc724d6SSuren Baghdasaryan ret |= folio_lock_or_retry(folio, vmf); 3927fdc724d6SSuren Baghdasaryan if (ret & VM_FAULT_RETRY) 3928d065bd81SMichel Lespinasse goto out_release; 39291da177e4SLinus Torvalds 393084d60fddSDavid Hildenbrand if (swapcache) { 39314969c119SAndrea Arcangeli /* 39323b344157SMatthew Wilcox (Oracle) * Make sure folio_free_swap() or swapoff did not release the 393384d60fddSDavid Hildenbrand * swapcache from under us. The page pin, and pte_same test 393484d60fddSDavid Hildenbrand * below, are not enough to exclude that. Even if it is still 393584d60fddSDavid Hildenbrand * swapcache, we need to check that the page's swap has not 393684d60fddSDavid Hildenbrand * changed. 39374969c119SAndrea Arcangeli */ 393863ad4addSMatthew Wilcox (Oracle) if (unlikely(!folio_test_swapcache(folio) || 3939cfeed8ffSDavid Hildenbrand page_swap_entry(page).val != entry.val)) 39404969c119SAndrea Arcangeli goto out_page; 39414969c119SAndrea Arcangeli 394284d60fddSDavid Hildenbrand /* 394384d60fddSDavid Hildenbrand * KSM sometimes has to copy on read faults, for example, if 394484d60fddSDavid Hildenbrand * page->index of !PageKSM() pages would be nonlinear inside the 394584d60fddSDavid Hildenbrand * anon VMA -- PageKSM() is lost on actual swapout. 394684d60fddSDavid Hildenbrand */ 394796db66d9SMatthew Wilcox (Oracle) folio = ksm_might_need_to_copy(folio, vma, vmf->address); 394896db66d9SMatthew Wilcox (Oracle) if (unlikely(!folio)) { 39495ad64688SHugh Dickins ret = VM_FAULT_OOM; 395096db66d9SMatthew Wilcox (Oracle) folio = swapcache; 39514969c119SAndrea Arcangeli goto out_page; 395296db66d9SMatthew Wilcox (Oracle) } else if (unlikely(folio == ERR_PTR(-EHWPOISON))) { 39536b970599SKefeng Wang ret = VM_FAULT_HWPOISON; 395496db66d9SMatthew Wilcox (Oracle) folio = swapcache; 39556b970599SKefeng Wang goto out_page; 39564969c119SAndrea Arcangeli } 395796db66d9SMatthew Wilcox (Oracle) if (folio != swapcache) 395896db66d9SMatthew Wilcox (Oracle) page = folio_page(folio, 0); 3959c145e0b4SDavid Hildenbrand 3960c145e0b4SDavid Hildenbrand /* 3961c145e0b4SDavid Hildenbrand * If we want to map a page that's in the swapcache writable, we 3962c145e0b4SDavid Hildenbrand * have to detect via the refcount if we're really the exclusive 3963c145e0b4SDavid Hildenbrand * owner. Try removing the extra reference from the local LRU 39641fec6890SMatthew Wilcox (Oracle) * caches if required. 3965c145e0b4SDavid Hildenbrand */ 3966d4f9565aSMatthew Wilcox (Oracle) if ((vmf->flags & FAULT_FLAG_WRITE) && folio == swapcache && 396763ad4addSMatthew Wilcox (Oracle) !folio_test_ksm(folio) && !folio_test_lru(folio)) 3968c145e0b4SDavid Hildenbrand lru_add_drain(); 396984d60fddSDavid Hildenbrand } 39705ad64688SHugh Dickins 39714231f842SKefeng Wang folio_throttle_swaprate(folio, GFP_KERNEL); 3972073e587eSKAMEZAWA Hiroyuki 39731da177e4SLinus Torvalds /* 39748f4e2101SHugh Dickins * Back out if somebody else already faulted in this pte. 39751da177e4SLinus Torvalds */ 397682b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 397782b0f8c3SJan Kara &vmf->ptl); 3978c33c7948SRyan Roberts if (unlikely(!vmf->pte || !pte_same(ptep_get(vmf->pte), vmf->orig_pte))) 3979b8107480SKirill Korotaev goto out_nomap; 3980b8107480SKirill Korotaev 398163ad4addSMatthew Wilcox (Oracle) if (unlikely(!folio_test_uptodate(folio))) { 3982b8107480SKirill Korotaev ret = VM_FAULT_SIGBUS; 3983b8107480SKirill Korotaev goto out_nomap; 39841da177e4SLinus Torvalds } 39851da177e4SLinus Torvalds 39868c7c6e34SKAMEZAWA Hiroyuki /* 398778fbe906SDavid Hildenbrand * PG_anon_exclusive reuses PG_mappedtodisk for anon pages. A swap pte 398878fbe906SDavid Hildenbrand * must never point at an anonymous page in the swapcache that is 398978fbe906SDavid Hildenbrand * PG_anon_exclusive. Sanity check that this holds and especially, that 399078fbe906SDavid Hildenbrand * no filesystem set PG_mappedtodisk on a page in the swapcache. Sanity 399178fbe906SDavid Hildenbrand * check after taking the PT lock and making sure that nobody 399278fbe906SDavid Hildenbrand * concurrently faulted in this page and set PG_anon_exclusive. 399378fbe906SDavid Hildenbrand */ 399463ad4addSMatthew Wilcox (Oracle) BUG_ON(!folio_test_anon(folio) && folio_test_mappedtodisk(folio)); 399563ad4addSMatthew Wilcox (Oracle) BUG_ON(folio_test_anon(folio) && PageAnonExclusive(page)); 399678fbe906SDavid Hildenbrand 399778fbe906SDavid Hildenbrand /* 39981493a191SDavid Hildenbrand * Check under PT lock (to protect against concurrent fork() sharing 39991493a191SDavid Hildenbrand * the swap entry concurrently) for certainly exclusive pages. 40001493a191SDavid Hildenbrand */ 400163ad4addSMatthew Wilcox (Oracle) if (!folio_test_ksm(folio)) { 40021493a191SDavid Hildenbrand exclusive = pte_swp_exclusive(vmf->orig_pte); 4003d4f9565aSMatthew Wilcox (Oracle) if (folio != swapcache) { 40041493a191SDavid Hildenbrand /* 40051493a191SDavid Hildenbrand * We have a fresh page that is not exposed to the 40061493a191SDavid Hildenbrand * swapcache -> certainly exclusive. 40071493a191SDavid Hildenbrand */ 40081493a191SDavid Hildenbrand exclusive = true; 400963ad4addSMatthew Wilcox (Oracle) } else if (exclusive && folio_test_writeback(folio) && 4010eacde327SMiaohe Lin data_race(si->flags & SWP_STABLE_WRITES)) { 40111493a191SDavid Hildenbrand /* 40121493a191SDavid Hildenbrand * This is tricky: not all swap backends support 40131493a191SDavid Hildenbrand * concurrent page modifications while under writeback. 40141493a191SDavid Hildenbrand * 40151493a191SDavid Hildenbrand * So if we stumble over such a page in the swapcache 40161493a191SDavid Hildenbrand * we must not set the page exclusive, otherwise we can 40171493a191SDavid Hildenbrand * map it writable without further checks and modify it 40181493a191SDavid Hildenbrand * while still under writeback. 40191493a191SDavid Hildenbrand * 40201493a191SDavid Hildenbrand * For these problematic swap backends, simply drop the 40211493a191SDavid Hildenbrand * exclusive marker: this is perfectly fine as we start 40221493a191SDavid Hildenbrand * writeback only if we fully unmapped the page and 40231493a191SDavid Hildenbrand * there are no unexpected references on the page after 40241493a191SDavid Hildenbrand * unmapping succeeded. After fully unmapped, no 40251493a191SDavid Hildenbrand * further GUP references (FOLL_GET and FOLL_PIN) can 40261493a191SDavid Hildenbrand * appear, so dropping the exclusive marker and mapping 40271493a191SDavid Hildenbrand * it only R/O is fine. 40281493a191SDavid Hildenbrand */ 40291493a191SDavid Hildenbrand exclusive = false; 40301493a191SDavid Hildenbrand } 40311493a191SDavid Hildenbrand } 40321493a191SDavid Hildenbrand 40331493a191SDavid Hildenbrand /* 40346dca4ac6SPeter Collingbourne * Some architectures may have to restore extra metadata to the page 40356dca4ac6SPeter Collingbourne * when reading from swap. This metadata may be indexed by swap entry 40366dca4ac6SPeter Collingbourne * so this must be called before swap_free(). 40376dca4ac6SPeter Collingbourne */ 40386dca4ac6SPeter Collingbourne arch_swap_restore(entry, folio); 40396dca4ac6SPeter Collingbourne 40406dca4ac6SPeter Collingbourne /* 4041c145e0b4SDavid Hildenbrand * Remove the swap entry and conditionally try to free up the swapcache. 4042c145e0b4SDavid Hildenbrand * We're already holding a reference on the page but haven't mapped it 4043c145e0b4SDavid Hildenbrand * yet. 40448c7c6e34SKAMEZAWA Hiroyuki */ 4045c145e0b4SDavid Hildenbrand swap_free(entry); 4046a160e537SMatthew Wilcox (Oracle) if (should_try_to_free_swap(folio, vma, vmf->flags)) 4047a160e537SMatthew Wilcox (Oracle) folio_free_swap(folio); 40481da177e4SLinus Torvalds 4049f1a79412SShakeel Butt inc_mm_counter(vma->vm_mm, MM_ANONPAGES); 4050f1a79412SShakeel Butt dec_mm_counter(vma->vm_mm, MM_SWAPENTS); 40511da177e4SLinus Torvalds pte = mk_pte(page, vma->vm_page_prot); 4052c145e0b4SDavid Hildenbrand 4053c145e0b4SDavid Hildenbrand /* 40541493a191SDavid Hildenbrand * Same logic as in do_wp_page(); however, optimize for pages that are 40551493a191SDavid Hildenbrand * certainly not shared either because we just allocated them without 40561493a191SDavid Hildenbrand * exposing them to the swapcache or because the swap entry indicates 40571493a191SDavid Hildenbrand * exclusivity. 4058c145e0b4SDavid Hildenbrand */ 405963ad4addSMatthew Wilcox (Oracle) if (!folio_test_ksm(folio) && 406063ad4addSMatthew Wilcox (Oracle) (exclusive || folio_ref_count(folio) == 1)) { 40616c287605SDavid Hildenbrand if (vmf->flags & FAULT_FLAG_WRITE) { 40621da177e4SLinus Torvalds pte = maybe_mkwrite(pte_mkdirty(pte), vma); 406382b0f8c3SJan Kara vmf->flags &= ~FAULT_FLAG_WRITE; 40646c287605SDavid Hildenbrand } 406514f9135dSDavid Hildenbrand rmap_flags |= RMAP_EXCLUSIVE; 40661da177e4SLinus Torvalds } 40671da177e4SLinus Torvalds flush_icache_page(vma, page); 40682994302bSJan Kara if (pte_swp_soft_dirty(vmf->orig_pte)) 4069179ef71cSCyrill Gorcunov pte = pte_mksoft_dirty(pte); 4070f1eb1bacSPeter Xu if (pte_swp_uffd_wp(vmf->orig_pte)) 4071f45ec5ffSPeter Xu pte = pte_mkuffd_wp(pte); 40722994302bSJan Kara vmf->orig_pte = pte; 40730bcac06fSMinchan Kim 40740bcac06fSMinchan Kim /* ksm created a completely new copy */ 4075d4f9565aSMatthew Wilcox (Oracle) if (unlikely(folio != swapcache && swapcache)) { 40762853b66bSMatthew Wilcox (Oracle) folio_add_new_anon_rmap(folio, vma, vmf->address); 407763ad4addSMatthew Wilcox (Oracle) folio_add_lru_vma(folio, vma); 40780bcac06fSMinchan Kim } else { 4079f1e2db12SDavid Hildenbrand page_add_anon_rmap(page, vma, vmf->address, rmap_flags); 408000501b53SJohannes Weiner } 40811da177e4SLinus Torvalds 408263ad4addSMatthew Wilcox (Oracle) VM_BUG_ON(!folio_test_anon(folio) || 408363ad4addSMatthew Wilcox (Oracle) (pte_write(pte) && !PageAnonExclusive(page))); 40841eba86c0SPasha Tatashin set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte); 40851eba86c0SPasha Tatashin arch_do_swap_page(vma->vm_mm, vma, vmf->address, pte, vmf->orig_pte); 40861eba86c0SPasha Tatashin 408763ad4addSMatthew Wilcox (Oracle) folio_unlock(folio); 4088d4f9565aSMatthew Wilcox (Oracle) if (folio != swapcache && swapcache) { 40894969c119SAndrea Arcangeli /* 40904969c119SAndrea Arcangeli * Hold the lock to avoid the swap entry to be reused 40914969c119SAndrea Arcangeli * until we take the PT lock for the pte_same() check 40924969c119SAndrea Arcangeli * (to avoid false positives from pte_same). For 40934969c119SAndrea Arcangeli * further safety release the lock after the swap_free 40944969c119SAndrea Arcangeli * so that the swap count won't change under a 40954969c119SAndrea Arcangeli * parallel locked swapcache. 40964969c119SAndrea Arcangeli */ 4097d4f9565aSMatthew Wilcox (Oracle) folio_unlock(swapcache); 4098d4f9565aSMatthew Wilcox (Oracle) folio_put(swapcache); 40994969c119SAndrea Arcangeli } 4100c475a8abSHugh Dickins 410182b0f8c3SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) { 41022994302bSJan Kara ret |= do_wp_page(vmf); 410361469f1dSHugh Dickins if (ret & VM_FAULT_ERROR) 410461469f1dSHugh Dickins ret &= VM_FAULT_ERROR; 41051da177e4SLinus Torvalds goto out; 41061da177e4SLinus Torvalds } 41071da177e4SLinus Torvalds 41081da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 41095003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vma, vmf->address, vmf->pte, 1); 411065500d23SHugh Dickins unlock: 41113db82b93SHugh Dickins if (vmf->pte) 411282b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 41131da177e4SLinus Torvalds out: 41142799e775SMiaohe Lin if (si) 41152799e775SMiaohe Lin put_swap_device(si); 41161da177e4SLinus Torvalds return ret; 4117b8107480SKirill Korotaev out_nomap: 41183db82b93SHugh Dickins if (vmf->pte) 411982b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 4120bc43f75cSJohannes Weiner out_page: 412163ad4addSMatthew Wilcox (Oracle) folio_unlock(folio); 41224779cb31SAndi Kleen out_release: 412363ad4addSMatthew Wilcox (Oracle) folio_put(folio); 4124d4f9565aSMatthew Wilcox (Oracle) if (folio != swapcache && swapcache) { 4125d4f9565aSMatthew Wilcox (Oracle) folio_unlock(swapcache); 4126d4f9565aSMatthew Wilcox (Oracle) folio_put(swapcache); 41274969c119SAndrea Arcangeli } 41282799e775SMiaohe Lin if (si) 41292799e775SMiaohe Lin put_swap_device(si); 413065500d23SHugh Dickins return ret; 41311da177e4SLinus Torvalds } 41321da177e4SLinus Torvalds 413319eaf449SRyan Roberts static bool pte_range_none(pte_t *pte, int nr_pages) 413419eaf449SRyan Roberts { 413519eaf449SRyan Roberts int i; 413619eaf449SRyan Roberts 413719eaf449SRyan Roberts for (i = 0; i < nr_pages; i++) { 413819eaf449SRyan Roberts if (!pte_none(ptep_get_lockless(pte + i))) 413919eaf449SRyan Roberts return false; 414019eaf449SRyan Roberts } 414119eaf449SRyan Roberts 414219eaf449SRyan Roberts return true; 414319eaf449SRyan Roberts } 414419eaf449SRyan Roberts 414519eaf449SRyan Roberts static struct folio *alloc_anon_folio(struct vm_fault *vmf) 414619eaf449SRyan Roberts { 414719eaf449SRyan Roberts #ifdef CONFIG_TRANSPARENT_HUGEPAGE 414819eaf449SRyan Roberts struct vm_area_struct *vma = vmf->vma; 414919eaf449SRyan Roberts unsigned long orders; 415019eaf449SRyan Roberts struct folio *folio; 415119eaf449SRyan Roberts unsigned long addr; 415219eaf449SRyan Roberts pte_t *pte; 415319eaf449SRyan Roberts gfp_t gfp; 415419eaf449SRyan Roberts int order; 415519eaf449SRyan Roberts 415619eaf449SRyan Roberts /* 415719eaf449SRyan Roberts * If uffd is active for the vma we need per-page fault fidelity to 415819eaf449SRyan Roberts * maintain the uffd semantics. 415919eaf449SRyan Roberts */ 416019eaf449SRyan Roberts if (unlikely(userfaultfd_armed(vma))) 416119eaf449SRyan Roberts goto fallback; 416219eaf449SRyan Roberts 416319eaf449SRyan Roberts /* 416419eaf449SRyan Roberts * Get a list of all the (large) orders below PMD_ORDER that are enabled 416519eaf449SRyan Roberts * for this vma. Then filter out the orders that can't be allocated over 416619eaf449SRyan Roberts * the faulting address and still be fully contained in the vma. 416719eaf449SRyan Roberts */ 416819eaf449SRyan Roberts orders = thp_vma_allowable_orders(vma, vma->vm_flags, false, true, true, 416919eaf449SRyan Roberts BIT(PMD_ORDER) - 1); 417019eaf449SRyan Roberts orders = thp_vma_suitable_orders(vma, vmf->address, orders); 417119eaf449SRyan Roberts 417219eaf449SRyan Roberts if (!orders) 417319eaf449SRyan Roberts goto fallback; 417419eaf449SRyan Roberts 417519eaf449SRyan Roberts pte = pte_offset_map(vmf->pmd, vmf->address & PMD_MASK); 417619eaf449SRyan Roberts if (!pte) 417719eaf449SRyan Roberts return ERR_PTR(-EAGAIN); 417819eaf449SRyan Roberts 417919eaf449SRyan Roberts /* 418019eaf449SRyan Roberts * Find the highest order where the aligned range is completely 418119eaf449SRyan Roberts * pte_none(). Note that all remaining orders will be completely 418219eaf449SRyan Roberts * pte_none(). 418319eaf449SRyan Roberts */ 418419eaf449SRyan Roberts order = highest_order(orders); 418519eaf449SRyan Roberts while (orders) { 418619eaf449SRyan Roberts addr = ALIGN_DOWN(vmf->address, PAGE_SIZE << order); 418719eaf449SRyan Roberts if (pte_range_none(pte + pte_index(addr), 1 << order)) 418819eaf449SRyan Roberts break; 418919eaf449SRyan Roberts order = next_order(&orders, order); 419019eaf449SRyan Roberts } 419119eaf449SRyan Roberts 419219eaf449SRyan Roberts pte_unmap(pte); 419319eaf449SRyan Roberts 419419eaf449SRyan Roberts /* Try allocating the highest of the remaining orders. */ 419519eaf449SRyan Roberts gfp = vma_thp_gfp_mask(vma); 419619eaf449SRyan Roberts while (orders) { 419719eaf449SRyan Roberts addr = ALIGN_DOWN(vmf->address, PAGE_SIZE << order); 419819eaf449SRyan Roberts folio = vma_alloc_folio(gfp, order, vma, addr, true); 419919eaf449SRyan Roberts if (folio) { 420019eaf449SRyan Roberts clear_huge_page(&folio->page, vmf->address, 1 << order); 420119eaf449SRyan Roberts return folio; 420219eaf449SRyan Roberts } 420319eaf449SRyan Roberts order = next_order(&orders, order); 420419eaf449SRyan Roberts } 420519eaf449SRyan Roberts 420619eaf449SRyan Roberts fallback: 420719eaf449SRyan Roberts #endif 420819eaf449SRyan Roberts return vma_alloc_zeroed_movable_folio(vmf->vma, vmf->address); 420919eaf449SRyan Roberts } 421019eaf449SRyan Roberts 42111da177e4SLinus Torvalds /* 4212c1e8d7c6SMichel Lespinasse * We enter with non-exclusive mmap_lock (to exclude vma changes, 42138f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 4214c1e8d7c6SMichel Lespinasse * We return with mmap_lock still held, but pte unmapped and unlocked. 42151da177e4SLinus Torvalds */ 42162b740303SSouptick Joarder static vm_fault_t do_anonymous_page(struct vm_fault *vmf) 42171da177e4SLinus Torvalds { 42182bad466cSPeter Xu bool uffd_wp = vmf_orig_pte_uffd_wp(vmf); 421982b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 422019eaf449SRyan Roberts unsigned long addr = vmf->address; 42216bc56a4dSMatthew Wilcox (Oracle) struct folio *folio; 42222b740303SSouptick Joarder vm_fault_t ret = 0; 422319eaf449SRyan Roberts int nr_pages = 1; 42241da177e4SLinus Torvalds pte_t entry; 422519eaf449SRyan Roberts int i; 42261da177e4SLinus Torvalds 42276b7339f4SKirill A. Shutemov /* File mapping without ->vm_ops ? */ 42286b7339f4SKirill A. Shutemov if (vma->vm_flags & VM_SHARED) 42296b7339f4SKirill A. Shutemov return VM_FAULT_SIGBUS; 42306b7339f4SKirill A. Shutemov 42317267ec00SKirill A. Shutemov /* 42323db82b93SHugh Dickins * Use pte_alloc() instead of pte_alloc_map(), so that OOM can 42333db82b93SHugh Dickins * be distinguished from a transient failure of pte_offset_map(). 42347267ec00SKirill A. Shutemov */ 42354cf58924SJoel Fernandes (Google) if (pte_alloc(vma->vm_mm, vmf->pmd)) 42367267ec00SKirill A. Shutemov return VM_FAULT_OOM; 42377267ec00SKirill A. Shutemov 423811ac5524SLinus Torvalds /* Use the zero-page for reads */ 423982b0f8c3SJan Kara if (!(vmf->flags & FAULT_FLAG_WRITE) && 4240bae473a4SKirill A. Shutemov !mm_forbids_zeropage(vma->vm_mm)) { 424182b0f8c3SJan Kara entry = pte_mkspecial(pfn_pte(my_zero_pfn(vmf->address), 424262eede62SHugh Dickins vma->vm_page_prot)); 424382b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 424482b0f8c3SJan Kara vmf->address, &vmf->ptl); 42453db82b93SHugh Dickins if (!vmf->pte) 42463db82b93SHugh Dickins goto unlock; 42472bad466cSPeter Xu if (vmf_pte_changed(vmf)) { 42487df67697SBibo Mao update_mmu_tlb(vma, vmf->address, vmf->pte); 4249a13ea5b7SHugh Dickins goto unlock; 42507df67697SBibo Mao } 42516b31d595SMichal Hocko ret = check_stable_address_space(vma->vm_mm); 42526b31d595SMichal Hocko if (ret) 42536b31d595SMichal Hocko goto unlock; 42546b251fc9SAndrea Arcangeli /* Deliver the page fault to userland, check inside PT lock */ 42556b251fc9SAndrea Arcangeli if (userfaultfd_missing(vma)) { 425682b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 425782b0f8c3SJan Kara return handle_userfault(vmf, VM_UFFD_MISSING); 42586b251fc9SAndrea Arcangeli } 4259a13ea5b7SHugh Dickins goto setpte; 4260a13ea5b7SHugh Dickins } 4261a13ea5b7SHugh Dickins 42621da177e4SLinus Torvalds /* Allocate our own private page. */ 42631da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 426465500d23SHugh Dickins goto oom; 426519eaf449SRyan Roberts /* Returns NULL on OOM or ERR_PTR(-EAGAIN) if we must retry the fault */ 426619eaf449SRyan Roberts folio = alloc_anon_folio(vmf); 426719eaf449SRyan Roberts if (IS_ERR(folio)) 426819eaf449SRyan Roberts return 0; 42696bc56a4dSMatthew Wilcox (Oracle) if (!folio) 427065500d23SHugh Dickins goto oom; 4271eb3c24f3SMel Gorman 427219eaf449SRyan Roberts nr_pages = folio_nr_pages(folio); 427319eaf449SRyan Roberts addr = ALIGN_DOWN(vmf->address, nr_pages * PAGE_SIZE); 427419eaf449SRyan Roberts 42756bc56a4dSMatthew Wilcox (Oracle) if (mem_cgroup_charge(folio, vma->vm_mm, GFP_KERNEL)) 4276eb3c24f3SMel Gorman goto oom_free_page; 4277e2bf3e2cSKefeng Wang folio_throttle_swaprate(folio, GFP_KERNEL); 4278eb3c24f3SMel Gorman 427952f37629SMinchan Kim /* 4280cb3184deSMatthew Wilcox (Oracle) * The memory barrier inside __folio_mark_uptodate makes sure that 4281f4f5329dSWei Yang * preceding stores to the page contents become visible before 428252f37629SMinchan Kim * the set_pte_at() write. 428352f37629SMinchan Kim */ 4284cb3184deSMatthew Wilcox (Oracle) __folio_mark_uptodate(folio); 42851da177e4SLinus Torvalds 4286cb3184deSMatthew Wilcox (Oracle) entry = mk_pte(&folio->page, vma->vm_page_prot); 428750c25ee9SThomas Bogendoerfer entry = pte_sw_mkyoung(entry); 42881ac0cb5dSHugh Dickins if (vma->vm_flags & VM_WRITE) 4289161e393cSRick Edgecombe entry = pte_mkwrite(pte_mkdirty(entry), vma); 42908f4e2101SHugh Dickins 429119eaf449SRyan Roberts vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, addr, &vmf->ptl); 42923db82b93SHugh Dickins if (!vmf->pte) 42933db82b93SHugh Dickins goto release; 429419eaf449SRyan Roberts if (nr_pages == 1 && vmf_pte_changed(vmf)) { 429519eaf449SRyan Roberts update_mmu_tlb(vma, addr, vmf->pte); 429619eaf449SRyan Roberts goto release; 429719eaf449SRyan Roberts } else if (nr_pages > 1 && !pte_range_none(vmf->pte, nr_pages)) { 429819eaf449SRyan Roberts for (i = 0; i < nr_pages; i++) 429919eaf449SRyan Roberts update_mmu_tlb(vma, addr + PAGE_SIZE * i, vmf->pte + i); 43008f4e2101SHugh Dickins goto release; 43017df67697SBibo Mao } 43029ba69294SHugh Dickins 43036b31d595SMichal Hocko ret = check_stable_address_space(vma->vm_mm); 43046b31d595SMichal Hocko if (ret) 43056b31d595SMichal Hocko goto release; 43066b31d595SMichal Hocko 43076b251fc9SAndrea Arcangeli /* Deliver the page fault to userland, check inside PT lock */ 43086b251fc9SAndrea Arcangeli if (userfaultfd_missing(vma)) { 430982b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 4310cb3184deSMatthew Wilcox (Oracle) folio_put(folio); 431182b0f8c3SJan Kara return handle_userfault(vmf, VM_UFFD_MISSING); 43126b251fc9SAndrea Arcangeli } 43136b251fc9SAndrea Arcangeli 431419eaf449SRyan Roberts folio_ref_add(folio, nr_pages - 1); 431519eaf449SRyan Roberts add_mm_counter(vma->vm_mm, MM_ANONPAGES, nr_pages); 431619eaf449SRyan Roberts folio_add_new_anon_rmap(folio, vma, addr); 4317cb3184deSMatthew Wilcox (Oracle) folio_add_lru_vma(folio, vma); 4318a13ea5b7SHugh Dickins setpte: 43192bad466cSPeter Xu if (uffd_wp) 43202bad466cSPeter Xu entry = pte_mkuffd_wp(entry); 432119eaf449SRyan Roberts set_ptes(vma->vm_mm, addr, vmf->pte, entry, nr_pages); 43221da177e4SLinus Torvalds 43231da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 432419eaf449SRyan Roberts update_mmu_cache_range(vmf, vma, addr, vmf->pte, nr_pages); 432565500d23SHugh Dickins unlock: 43263db82b93SHugh Dickins if (vmf->pte) 432782b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 43286b31d595SMichal Hocko return ret; 43298f4e2101SHugh Dickins release: 4330cb3184deSMatthew Wilcox (Oracle) folio_put(folio); 43318f4e2101SHugh Dickins goto unlock; 43328a9f3ccdSBalbir Singh oom_free_page: 4333cb3184deSMatthew Wilcox (Oracle) folio_put(folio); 433465500d23SHugh Dickins oom: 43351da177e4SLinus Torvalds return VM_FAULT_OOM; 43361da177e4SLinus Torvalds } 43371da177e4SLinus Torvalds 43389a95f3cfSPaul Cassella /* 4339c1e8d7c6SMichel Lespinasse * The mmap_lock must have been held on entry, and may have been 43409a95f3cfSPaul Cassella * released depending on flags and vma->vm_ops->fault() return value. 43419a95f3cfSPaul Cassella * See filemap_fault() and __lock_page_retry(). 43429a95f3cfSPaul Cassella */ 43432b740303SSouptick Joarder static vm_fault_t __do_fault(struct vm_fault *vmf) 43447eae74afSKirill A. Shutemov { 434582b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 434601d1e0e6SMatthew Wilcox (Oracle) struct folio *folio; 43472b740303SSouptick Joarder vm_fault_t ret; 43487eae74afSKirill A. Shutemov 434963f3655fSMichal Hocko /* 435063f3655fSMichal Hocko * Preallocate pte before we take page_lock because this might lead to 435163f3655fSMichal Hocko * deadlocks for memcg reclaim which waits for pages under writeback: 435263f3655fSMichal Hocko * lock_page(A) 435363f3655fSMichal Hocko * SetPageWriteback(A) 435463f3655fSMichal Hocko * unlock_page(A) 435563f3655fSMichal Hocko * lock_page(B) 435663f3655fSMichal Hocko * lock_page(B) 4357d383807aSYanfei Xu * pte_alloc_one 435863f3655fSMichal Hocko * shrink_page_list 435963f3655fSMichal Hocko * wait_on_page_writeback(A) 436063f3655fSMichal Hocko * SetPageWriteback(B) 436163f3655fSMichal Hocko * unlock_page(B) 436263f3655fSMichal Hocko * # flush A, B to clear the writeback 436363f3655fSMichal Hocko */ 436463f3655fSMichal Hocko if (pmd_none(*vmf->pmd) && !vmf->prealloc_pte) { 4365a7069ee3SYanfei Xu vmf->prealloc_pte = pte_alloc_one(vma->vm_mm); 436663f3655fSMichal Hocko if (!vmf->prealloc_pte) 436763f3655fSMichal Hocko return VM_FAULT_OOM; 436863f3655fSMichal Hocko } 436963f3655fSMichal Hocko 437011bac800SDave Jiang ret = vma->vm_ops->fault(vmf); 43713917048dSJan Kara if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY | 4372b1aa812bSJan Kara VM_FAULT_DONE_COW))) 43737eae74afSKirill A. Shutemov return ret; 43747eae74afSKirill A. Shutemov 437501d1e0e6SMatthew Wilcox (Oracle) folio = page_folio(vmf->page); 4376667240e0SJan Kara if (unlikely(PageHWPoison(vmf->page))) { 4377e53ac737SRik van Riel vm_fault_t poisonret = VM_FAULT_HWPOISON; 4378e53ac737SRik van Riel if (ret & VM_FAULT_LOCKED) { 437901d1e0e6SMatthew Wilcox (Oracle) if (page_mapped(vmf->page)) 438001d1e0e6SMatthew Wilcox (Oracle) unmap_mapping_folio(folio); 438101d1e0e6SMatthew Wilcox (Oracle) /* Retry if a clean folio was removed from the cache. */ 438201d1e0e6SMatthew Wilcox (Oracle) if (mapping_evict_folio(folio->mapping, folio)) 43833149c79fSRik van Riel poisonret = VM_FAULT_NOPAGE; 438401d1e0e6SMatthew Wilcox (Oracle) folio_unlock(folio); 4385e53ac737SRik van Riel } 438601d1e0e6SMatthew Wilcox (Oracle) folio_put(folio); 4387936ca80dSJan Kara vmf->page = NULL; 4388e53ac737SRik van Riel return poisonret; 43897eae74afSKirill A. Shutemov } 43907eae74afSKirill A. Shutemov 43917eae74afSKirill A. Shutemov if (unlikely(!(ret & VM_FAULT_LOCKED))) 439201d1e0e6SMatthew Wilcox (Oracle) folio_lock(folio); 43937eae74afSKirill A. Shutemov else 439401d1e0e6SMatthew Wilcox (Oracle) VM_BUG_ON_PAGE(!folio_test_locked(folio), vmf->page); 43957eae74afSKirill A. Shutemov 43967eae74afSKirill A. Shutemov return ret; 43977eae74afSKirill A. Shutemov } 43987eae74afSKirill A. Shutemov 4399396bcc52SMatthew Wilcox (Oracle) #ifdef CONFIG_TRANSPARENT_HUGEPAGE 440082b0f8c3SJan Kara static void deposit_prealloc_pte(struct vm_fault *vmf) 4401953c66c2SAneesh Kumar K.V { 440282b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 4403953c66c2SAneesh Kumar K.V 440482b0f8c3SJan Kara pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, vmf->prealloc_pte); 4405953c66c2SAneesh Kumar K.V /* 4406953c66c2SAneesh Kumar K.V * We are going to consume the prealloc table, 4407953c66c2SAneesh Kumar K.V * count that as nr_ptes. 4408953c66c2SAneesh Kumar K.V */ 4409c4812909SKirill A. Shutemov mm_inc_nr_ptes(vma->vm_mm); 44107f2b6ce8STobin C Harding vmf->prealloc_pte = NULL; 4411953c66c2SAneesh Kumar K.V } 4412953c66c2SAneesh Kumar K.V 4413f9ce0be7SKirill A. Shutemov vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page) 441410102459SKirill A. Shutemov { 4415*ef37b2eaSDavid Hildenbrand struct folio *folio = page_folio(page); 441682b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 441782b0f8c3SJan Kara bool write = vmf->flags & FAULT_FLAG_WRITE; 441882b0f8c3SJan Kara unsigned long haddr = vmf->address & HPAGE_PMD_MASK; 441910102459SKirill A. Shutemov pmd_t entry; 4420d01ac3c3SMatthew Wilcox (Oracle) vm_fault_t ret = VM_FAULT_FALLBACK; 442110102459SKirill A. Shutemov 44223485b883SRyan Roberts if (!thp_vma_suitable_order(vma, haddr, PMD_ORDER)) 4423d01ac3c3SMatthew Wilcox (Oracle) return ret; 442410102459SKirill A. Shutemov 4425*ef37b2eaSDavid Hildenbrand if (page != &folio->page || folio_order(folio) != HPAGE_PMD_ORDER) 4426d01ac3c3SMatthew Wilcox (Oracle) return ret; 442710102459SKirill A. Shutemov 4428953c66c2SAneesh Kumar K.V /* 4429eac96c3eSYang Shi * Just backoff if any subpage of a THP is corrupted otherwise 4430eac96c3eSYang Shi * the corrupted page may mapped by PMD silently to escape the 4431eac96c3eSYang Shi * check. This kind of THP just can be PTE mapped. Access to 4432eac96c3eSYang Shi * the corrupted subpage should trigger SIGBUS as expected. 4433eac96c3eSYang Shi */ 4434*ef37b2eaSDavid Hildenbrand if (unlikely(folio_test_has_hwpoisoned(folio))) 4435eac96c3eSYang Shi return ret; 4436eac96c3eSYang Shi 4437eac96c3eSYang Shi /* 4438f0953a1bSIngo Molnar * Archs like ppc64 need additional space to store information 4439953c66c2SAneesh Kumar K.V * related to pte entry. Use the preallocated table for that. 4440953c66c2SAneesh Kumar K.V */ 444182b0f8c3SJan Kara if (arch_needs_pgtable_deposit() && !vmf->prealloc_pte) { 44424cf58924SJoel Fernandes (Google) vmf->prealloc_pte = pte_alloc_one(vma->vm_mm); 444382b0f8c3SJan Kara if (!vmf->prealloc_pte) 4444953c66c2SAneesh Kumar K.V return VM_FAULT_OOM; 4445953c66c2SAneesh Kumar K.V } 4446953c66c2SAneesh Kumar K.V 444782b0f8c3SJan Kara vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); 444882b0f8c3SJan Kara if (unlikely(!pmd_none(*vmf->pmd))) 444910102459SKirill A. Shutemov goto out; 445010102459SKirill A. Shutemov 44519f1f5b60SMatthew Wilcox (Oracle) flush_icache_pages(vma, page, HPAGE_PMD_NR); 445210102459SKirill A. Shutemov 445310102459SKirill A. Shutemov entry = mk_huge_pmd(page, vma->vm_page_prot); 445410102459SKirill A. Shutemov if (write) 4455f55e1014SLinus Torvalds entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); 445610102459SKirill A. Shutemov 4457fadae295SYang Shi add_mm_counter(vma->vm_mm, mm_counter_file(page), HPAGE_PMD_NR); 4458*ef37b2eaSDavid Hildenbrand folio_add_file_rmap_pmd(folio, page, vma); 4459cea86fe2SHugh Dickins 4460953c66c2SAneesh Kumar K.V /* 4461953c66c2SAneesh Kumar K.V * deposit and withdraw with pmd lock held 4462953c66c2SAneesh Kumar K.V */ 4463953c66c2SAneesh Kumar K.V if (arch_needs_pgtable_deposit()) 446482b0f8c3SJan Kara deposit_prealloc_pte(vmf); 446510102459SKirill A. Shutemov 446682b0f8c3SJan Kara set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry); 446710102459SKirill A. Shutemov 446882b0f8c3SJan Kara update_mmu_cache_pmd(vma, haddr, vmf->pmd); 446910102459SKirill A. Shutemov 447010102459SKirill A. Shutemov /* fault is handled */ 447110102459SKirill A. Shutemov ret = 0; 447295ecedcdSKirill A. Shutemov count_vm_event(THP_FILE_MAPPED); 447310102459SKirill A. Shutemov out: 447482b0f8c3SJan Kara spin_unlock(vmf->ptl); 447510102459SKirill A. Shutemov return ret; 447610102459SKirill A. Shutemov } 447710102459SKirill A. Shutemov #else 4478f9ce0be7SKirill A. Shutemov vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page) 447910102459SKirill A. Shutemov { 4480f9ce0be7SKirill A. Shutemov return VM_FAULT_FALLBACK; 448110102459SKirill A. Shutemov } 448210102459SKirill A. Shutemov #endif 448310102459SKirill A. Shutemov 44843bd786f7SYin Fengwei /** 44853bd786f7SYin Fengwei * set_pte_range - Set a range of PTEs to point to pages in a folio. 44863bd786f7SYin Fengwei * @vmf: Fault decription. 44873bd786f7SYin Fengwei * @folio: The folio that contains @page. 44883bd786f7SYin Fengwei * @page: The first page to create a PTE for. 44893bd786f7SYin Fengwei * @nr: The number of PTEs to create. 44903bd786f7SYin Fengwei * @addr: The first address to create a PTE for. 44913bd786f7SYin Fengwei */ 44923bd786f7SYin Fengwei void set_pte_range(struct vm_fault *vmf, struct folio *folio, 44933bd786f7SYin Fengwei struct page *page, unsigned int nr, unsigned long addr) 44943bb97794SKirill A. Shutemov { 449582b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 44962bad466cSPeter Xu bool uffd_wp = vmf_orig_pte_uffd_wp(vmf); 449782b0f8c3SJan Kara bool write = vmf->flags & FAULT_FLAG_WRITE; 44983bd786f7SYin Fengwei bool prefault = in_range(vmf->address, addr, nr * PAGE_SIZE); 44993bb97794SKirill A. Shutemov pte_t entry; 45007267ec00SKirill A. Shutemov 45013bd786f7SYin Fengwei flush_icache_pages(vma, page, nr); 45023bb97794SKirill A. Shutemov entry = mk_pte(page, vma->vm_page_prot); 450346bdb427SWill Deacon 450446bdb427SWill Deacon if (prefault && arch_wants_old_prefaulted_pte()) 450546bdb427SWill Deacon entry = pte_mkold(entry); 450650c25ee9SThomas Bogendoerfer else 450750c25ee9SThomas Bogendoerfer entry = pte_sw_mkyoung(entry); 450846bdb427SWill Deacon 45093bb97794SKirill A. Shutemov if (write) 45103bb97794SKirill A. Shutemov entry = maybe_mkwrite(pte_mkdirty(entry), vma); 45119c28a205SPeter Xu if (unlikely(uffd_wp)) 4512f1eb1bacSPeter Xu entry = pte_mkuffd_wp(entry); 4513bae473a4SKirill A. Shutemov /* copy-on-write page */ 4514bae473a4SKirill A. Shutemov if (write && !(vma->vm_flags & VM_SHARED)) { 45153bd786f7SYin Fengwei add_mm_counter(vma->vm_mm, MM_ANONPAGES, nr); 45163bd786f7SYin Fengwei VM_BUG_ON_FOLIO(nr != 1, folio); 45173bd786f7SYin Fengwei folio_add_new_anon_rmap(folio, vma, addr); 45183bd786f7SYin Fengwei folio_add_lru_vma(folio, vma); 45193bb97794SKirill A. Shutemov } else { 45203bd786f7SYin Fengwei add_mm_counter(vma->vm_mm, mm_counter_file(page), nr); 452168f03208SDavid Hildenbrand folio_add_file_rmap_ptes(folio, page, nr, vma); 45223bb97794SKirill A. Shutemov } 45233bd786f7SYin Fengwei set_ptes(vma->vm_mm, addr, vmf->pte, entry, nr); 45243bd786f7SYin Fengwei 45253bd786f7SYin Fengwei /* no need to invalidate: a not-present page won't be cached */ 45263bd786f7SYin Fengwei update_mmu_cache_range(vmf, vma, addr, vmf->pte, nr); 45273bb97794SKirill A. Shutemov } 45283bb97794SKirill A. Shutemov 4529f46f2adeSPeter Xu static bool vmf_pte_changed(struct vm_fault *vmf) 4530f46f2adeSPeter Xu { 4531f46f2adeSPeter Xu if (vmf->flags & FAULT_FLAG_ORIG_PTE_VALID) 4532c33c7948SRyan Roberts return !pte_same(ptep_get(vmf->pte), vmf->orig_pte); 4533f46f2adeSPeter Xu 4534c33c7948SRyan Roberts return !pte_none(ptep_get(vmf->pte)); 4535f46f2adeSPeter Xu } 4536f46f2adeSPeter Xu 45379118c0cbSJan Kara /** 45389118c0cbSJan Kara * finish_fault - finish page fault once we have prepared the page to fault 45399118c0cbSJan Kara * 45409118c0cbSJan Kara * @vmf: structure describing the fault 45419118c0cbSJan Kara * 45429118c0cbSJan Kara * This function handles all that is needed to finish a page fault once the 45439118c0cbSJan Kara * page to fault in is prepared. It handles locking of PTEs, inserts PTE for 45449118c0cbSJan Kara * given page, adds reverse page mapping, handles memcg charges and LRU 4545a862f68aSMike Rapoport * addition. 45469118c0cbSJan Kara * 45479118c0cbSJan Kara * The function expects the page to be locked and on success it consumes a 45489118c0cbSJan Kara * reference of a page being mapped (for the PTE which maps it). 4549a862f68aSMike Rapoport * 4550a862f68aSMike Rapoport * Return: %0 on success, %VM_FAULT_ code in case of error. 45519118c0cbSJan Kara */ 45522b740303SSouptick Joarder vm_fault_t finish_fault(struct vm_fault *vmf) 45539118c0cbSJan Kara { 4554f9ce0be7SKirill A. Shutemov struct vm_area_struct *vma = vmf->vma; 45559118c0cbSJan Kara struct page *page; 4556f9ce0be7SKirill A. Shutemov vm_fault_t ret; 45579118c0cbSJan Kara 45589118c0cbSJan Kara /* Did we COW the page? */ 4559f9ce0be7SKirill A. Shutemov if ((vmf->flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) 45609118c0cbSJan Kara page = vmf->cow_page; 45619118c0cbSJan Kara else 45629118c0cbSJan Kara page = vmf->page; 45636b31d595SMichal Hocko 45646b31d595SMichal Hocko /* 45656b31d595SMichal Hocko * check even for read faults because we might have lost our CoWed 45666b31d595SMichal Hocko * page 45676b31d595SMichal Hocko */ 4568f9ce0be7SKirill A. Shutemov if (!(vma->vm_flags & VM_SHARED)) { 4569f9ce0be7SKirill A. Shutemov ret = check_stable_address_space(vma->vm_mm); 4570f9ce0be7SKirill A. Shutemov if (ret) 4571f9ce0be7SKirill A. Shutemov return ret; 4572f9ce0be7SKirill A. Shutemov } 4573f9ce0be7SKirill A. Shutemov 4574f9ce0be7SKirill A. Shutemov if (pmd_none(*vmf->pmd)) { 4575f9ce0be7SKirill A. Shutemov if (PageTransCompound(page)) { 4576f9ce0be7SKirill A. Shutemov ret = do_set_pmd(vmf, page); 4577f9ce0be7SKirill A. Shutemov if (ret != VM_FAULT_FALLBACK) 4578f9ce0be7SKirill A. Shutemov return ret; 4579f9ce0be7SKirill A. Shutemov } 4580f9ce0be7SKirill A. Shutemov 458103c4f204SQi Zheng if (vmf->prealloc_pte) 458203c4f204SQi Zheng pmd_install(vma->vm_mm, vmf->pmd, &vmf->prealloc_pte); 458303c4f204SQi Zheng else if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd))) 4584f9ce0be7SKirill A. Shutemov return VM_FAULT_OOM; 4585f9ce0be7SKirill A. Shutemov } 4586f9ce0be7SKirill A. Shutemov 4587f9ce0be7SKirill A. Shutemov vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 4588f9ce0be7SKirill A. Shutemov vmf->address, &vmf->ptl); 45893db82b93SHugh Dickins if (!vmf->pte) 45903db82b93SHugh Dickins return VM_FAULT_NOPAGE; 4591f9ce0be7SKirill A. Shutemov 459270427f6eSSergei Antonov /* Re-check under ptl */ 459370427f6eSSergei Antonov if (likely(!vmf_pte_changed(vmf))) { 45943bd786f7SYin Fengwei struct folio *folio = page_folio(page); 459570427f6eSSergei Antonov 45963bd786f7SYin Fengwei set_pte_range(vmf, folio, page, 1, vmf->address); 459770427f6eSSergei Antonov ret = 0; 459870427f6eSSergei Antonov } else { 4599f9ce0be7SKirill A. Shutemov update_mmu_tlb(vma, vmf->address, vmf->pte); 460070427f6eSSergei Antonov ret = VM_FAULT_NOPAGE; 460170427f6eSSergei Antonov } 460270427f6eSSergei Antonov 46039118c0cbSJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 46049118c0cbSJan Kara return ret; 46059118c0cbSJan Kara } 46069118c0cbSJan Kara 460753d36a56SLorenzo Stoakes static unsigned long fault_around_pages __read_mostly = 460853d36a56SLorenzo Stoakes 65536 >> PAGE_SHIFT; 4609a9b0f861SKirill A. Shutemov 46101592eef0SKirill A. Shutemov #ifdef CONFIG_DEBUG_FS 4611a9b0f861SKirill A. Shutemov static int fault_around_bytes_get(void *data, u64 *val) 46121592eef0SKirill A. Shutemov { 461353d36a56SLorenzo Stoakes *val = fault_around_pages << PAGE_SHIFT; 46141592eef0SKirill A. Shutemov return 0; 46151592eef0SKirill A. Shutemov } 46161592eef0SKirill A. Shutemov 4617b4903d6eSAndrey Ryabinin /* 4618da391d64SWilliam Kucharski * fault_around_bytes must be rounded down to the nearest page order as it's 4619da391d64SWilliam Kucharski * what do_fault_around() expects to see. 4620b4903d6eSAndrey Ryabinin */ 4621a9b0f861SKirill A. Shutemov static int fault_around_bytes_set(void *data, u64 val) 46221592eef0SKirill A. Shutemov { 4623a9b0f861SKirill A. Shutemov if (val / PAGE_SIZE > PTRS_PER_PTE) 46241592eef0SKirill A. Shutemov return -EINVAL; 462553d36a56SLorenzo Stoakes 462653d36a56SLorenzo Stoakes /* 462753d36a56SLorenzo Stoakes * The minimum value is 1 page, however this results in no fault-around 462853d36a56SLorenzo Stoakes * at all. See should_fault_around(). 462953d36a56SLorenzo Stoakes */ 463053d36a56SLorenzo Stoakes fault_around_pages = max(rounddown_pow_of_two(val) >> PAGE_SHIFT, 1UL); 463153d36a56SLorenzo Stoakes 46321592eef0SKirill A. Shutemov return 0; 46331592eef0SKirill A. Shutemov } 46340a1345f8SYevgen Pronenko DEFINE_DEBUGFS_ATTRIBUTE(fault_around_bytes_fops, 4635a9b0f861SKirill A. Shutemov fault_around_bytes_get, fault_around_bytes_set, "%llu\n"); 46361592eef0SKirill A. Shutemov 46371592eef0SKirill A. Shutemov static int __init fault_around_debugfs(void) 46381592eef0SKirill A. Shutemov { 4639d9f7979cSGreg Kroah-Hartman debugfs_create_file_unsafe("fault_around_bytes", 0644, NULL, NULL, 4640a9b0f861SKirill A. Shutemov &fault_around_bytes_fops); 46411592eef0SKirill A. Shutemov return 0; 46421592eef0SKirill A. Shutemov } 46431592eef0SKirill A. Shutemov late_initcall(fault_around_debugfs); 46441592eef0SKirill A. Shutemov #endif 46458c6e50b0SKirill A. Shutemov 46461fdb412bSKirill A. Shutemov /* 46471fdb412bSKirill A. Shutemov * do_fault_around() tries to map few pages around the fault address. The hope 46481fdb412bSKirill A. Shutemov * is that the pages will be needed soon and this will lower the number of 46491fdb412bSKirill A. Shutemov * faults to handle. 46501fdb412bSKirill A. Shutemov * 46511fdb412bSKirill A. Shutemov * It uses vm_ops->map_pages() to map the pages, which skips the page if it's 46521fdb412bSKirill A. Shutemov * not ready to be mapped: not up-to-date, locked, etc. 46531fdb412bSKirill A. Shutemov * 46549042599eSLorenzo Stoakes * This function doesn't cross VMA or page table boundaries, in order to call 46559042599eSLorenzo Stoakes * map_pages() and acquire a PTE lock only once. 46561fdb412bSKirill A. Shutemov * 465753d36a56SLorenzo Stoakes * fault_around_pages defines how many pages we'll try to map. 4658da391d64SWilliam Kucharski * do_fault_around() expects it to be set to a power of two less than or equal 4659da391d64SWilliam Kucharski * to PTRS_PER_PTE. 46601fdb412bSKirill A. Shutemov * 4661da391d64SWilliam Kucharski * The virtual address of the area that we map is naturally aligned to 466253d36a56SLorenzo Stoakes * fault_around_pages * PAGE_SIZE rounded down to the machine page size 4663da391d64SWilliam Kucharski * (and therefore to page order). This way it's easier to guarantee 4664da391d64SWilliam Kucharski * that we don't cross page table boundaries. 46651fdb412bSKirill A. Shutemov */ 46662b740303SSouptick Joarder static vm_fault_t do_fault_around(struct vm_fault *vmf) 46678c6e50b0SKirill A. Shutemov { 466853d36a56SLorenzo Stoakes pgoff_t nr_pages = READ_ONCE(fault_around_pages); 46699042599eSLorenzo Stoakes pgoff_t pte_off = pte_index(vmf->address); 46709042599eSLorenzo Stoakes /* The page offset of vmf->address within the VMA. */ 46719042599eSLorenzo Stoakes pgoff_t vma_off = vmf->pgoff - vmf->vma->vm_pgoff; 46729042599eSLorenzo Stoakes pgoff_t from_pte, to_pte; 467358ef47efSMatthew Wilcox (Oracle) vm_fault_t ret; 46748c6e50b0SKirill A. Shutemov 46759042599eSLorenzo Stoakes /* The PTE offset of the start address, clamped to the VMA. */ 46769042599eSLorenzo Stoakes from_pte = max(ALIGN_DOWN(pte_off, nr_pages), 46779042599eSLorenzo Stoakes pte_off - min(pte_off, vma_off)); 4678aecd6f44SKirill A. Shutemov 46799042599eSLorenzo Stoakes /* The PTE offset of the end address, clamped to the VMA and PTE. */ 46809042599eSLorenzo Stoakes to_pte = min3(from_pte + nr_pages, (pgoff_t)PTRS_PER_PTE, 46819042599eSLorenzo Stoakes pte_off + vma_pages(vmf->vma) - vma_off) - 1; 46828c6e50b0SKirill A. Shutemov 468382b0f8c3SJan Kara if (pmd_none(*vmf->pmd)) { 46844cf58924SJoel Fernandes (Google) vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm); 468582b0f8c3SJan Kara if (!vmf->prealloc_pte) 4686f9ce0be7SKirill A. Shutemov return VM_FAULT_OOM; 46878c6e50b0SKirill A. Shutemov } 46888c6e50b0SKirill A. Shutemov 468958ef47efSMatthew Wilcox (Oracle) rcu_read_lock(); 469058ef47efSMatthew Wilcox (Oracle) ret = vmf->vma->vm_ops->map_pages(vmf, 46919042599eSLorenzo Stoakes vmf->pgoff + from_pte - pte_off, 46929042599eSLorenzo Stoakes vmf->pgoff + to_pte - pte_off); 469358ef47efSMatthew Wilcox (Oracle) rcu_read_unlock(); 469458ef47efSMatthew Wilcox (Oracle) 469558ef47efSMatthew Wilcox (Oracle) return ret; 46967267ec00SKirill A. Shutemov } 46977267ec00SKirill A. Shutemov 46989c28a205SPeter Xu /* Return true if we should do read fault-around, false otherwise */ 46999c28a205SPeter Xu static inline bool should_fault_around(struct vm_fault *vmf) 47009c28a205SPeter Xu { 47019c28a205SPeter Xu /* No ->map_pages? No way to fault around... */ 47029c28a205SPeter Xu if (!vmf->vma->vm_ops->map_pages) 47039c28a205SPeter Xu return false; 47049c28a205SPeter Xu 47059c28a205SPeter Xu if (uffd_disable_fault_around(vmf->vma)) 47069c28a205SPeter Xu return false; 47079c28a205SPeter Xu 470853d36a56SLorenzo Stoakes /* A single page implies no faulting 'around' at all. */ 470953d36a56SLorenzo Stoakes return fault_around_pages > 1; 47109c28a205SPeter Xu } 47119c28a205SPeter Xu 47122b740303SSouptick Joarder static vm_fault_t do_read_fault(struct vm_fault *vmf) 4713e655fb29SKirill A. Shutemov { 47142b740303SSouptick Joarder vm_fault_t ret = 0; 471522d1e68fSSidhartha Kumar struct folio *folio; 47168c6e50b0SKirill A. Shutemov 47178c6e50b0SKirill A. Shutemov /* 47188c6e50b0SKirill A. Shutemov * Let's call ->map_pages() first and use ->fault() as fallback 47198c6e50b0SKirill A. Shutemov * if page by the offset is not ready to be mapped (cold cache or 47208c6e50b0SKirill A. Shutemov * something). 47218c6e50b0SKirill A. Shutemov */ 47229c28a205SPeter Xu if (should_fault_around(vmf)) { 47230721ec8bSJan Kara ret = do_fault_around(vmf); 47247267ec00SKirill A. Shutemov if (ret) 47257267ec00SKirill A. Shutemov return ret; 47268c6e50b0SKirill A. Shutemov } 4727e655fb29SKirill A. Shutemov 472812214ebaSMatthew Wilcox (Oracle) ret = vmf_can_call_fault(vmf); 472912214ebaSMatthew Wilcox (Oracle) if (ret) 473012214ebaSMatthew Wilcox (Oracle) return ret; 4731f5617ffeSMatthew Wilcox (Oracle) 4732936ca80dSJan Kara ret = __do_fault(vmf); 4733e655fb29SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 4734e655fb29SKirill A. Shutemov return ret; 4735e655fb29SKirill A. Shutemov 47369118c0cbSJan Kara ret |= finish_fault(vmf); 473722d1e68fSSidhartha Kumar folio = page_folio(vmf->page); 473822d1e68fSSidhartha Kumar folio_unlock(folio); 47397267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 474022d1e68fSSidhartha Kumar folio_put(folio); 4741e655fb29SKirill A. Shutemov return ret; 4742e655fb29SKirill A. Shutemov } 4743e655fb29SKirill A. Shutemov 47442b740303SSouptick Joarder static vm_fault_t do_cow_fault(struct vm_fault *vmf) 4745ec47c3b9SKirill A. Shutemov { 474682b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 4747e4621e70SKefeng Wang struct folio *folio; 47482b740303SSouptick Joarder vm_fault_t ret; 4749ec47c3b9SKirill A. Shutemov 47504de8c93aSMatthew Wilcox (Oracle) ret = vmf_can_call_fault(vmf); 47514de8c93aSMatthew Wilcox (Oracle) if (!ret) 47524de8c93aSMatthew Wilcox (Oracle) ret = vmf_anon_prepare(vmf); 47534de8c93aSMatthew Wilcox (Oracle) if (ret) 47544de8c93aSMatthew Wilcox (Oracle) return ret; 4755ec47c3b9SKirill A. Shutemov 4756e4621e70SKefeng Wang folio = folio_prealloc(vma->vm_mm, vma, vmf->address, false); 4757e4621e70SKefeng Wang if (!folio) 4758ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 4759ec47c3b9SKirill A. Shutemov 4760e4621e70SKefeng Wang vmf->cow_page = &folio->page; 4761ec47c3b9SKirill A. Shutemov 4762936ca80dSJan Kara ret = __do_fault(vmf); 4763ec47c3b9SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 4764ec47c3b9SKirill A. Shutemov goto uncharge_out; 47653917048dSJan Kara if (ret & VM_FAULT_DONE_COW) 47663917048dSJan Kara return ret; 4767ec47c3b9SKirill A. Shutemov 4768936ca80dSJan Kara copy_user_highpage(vmf->cow_page, vmf->page, vmf->address, vma); 4769e4621e70SKefeng Wang __folio_mark_uptodate(folio); 4770ec47c3b9SKirill A. Shutemov 47719118c0cbSJan Kara ret |= finish_fault(vmf); 4772936ca80dSJan Kara unlock_page(vmf->page); 4773936ca80dSJan Kara put_page(vmf->page); 47747267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 47757267ec00SKirill A. Shutemov goto uncharge_out; 4776ec47c3b9SKirill A. Shutemov return ret; 4777ec47c3b9SKirill A. Shutemov uncharge_out: 4778e4621e70SKefeng Wang folio_put(folio); 4779ec47c3b9SKirill A. Shutemov return ret; 4780ec47c3b9SKirill A. Shutemov } 4781ec47c3b9SKirill A. Shutemov 47822b740303SSouptick Joarder static vm_fault_t do_shared_fault(struct vm_fault *vmf) 47831da177e4SLinus Torvalds { 478482b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 47852b740303SSouptick Joarder vm_fault_t ret, tmp; 47866f609b7eSSidhartha Kumar struct folio *folio; 47871d65f86dSKAMEZAWA Hiroyuki 47884ed43798SMatthew Wilcox (Oracle) ret = vmf_can_call_fault(vmf); 47894ed43798SMatthew Wilcox (Oracle) if (ret) 47904ed43798SMatthew Wilcox (Oracle) return ret; 479154cb8821SNick Piggin 4792936ca80dSJan Kara ret = __do_fault(vmf); 47937eae74afSKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 4794f0c6d4d2SKirill A. Shutemov return ret; 47951da177e4SLinus Torvalds 47966f609b7eSSidhartha Kumar folio = page_folio(vmf->page); 47976f609b7eSSidhartha Kumar 47981da177e4SLinus Torvalds /* 4799f0c6d4d2SKirill A. Shutemov * Check if the backing address space wants to know that the page is 4800f0c6d4d2SKirill A. Shutemov * about to become writable 48011da177e4SLinus Torvalds */ 4802fb09a464SKirill A. Shutemov if (vma->vm_ops->page_mkwrite) { 48036f609b7eSSidhartha Kumar folio_unlock(folio); 480486aa6998SSidhartha Kumar tmp = do_page_mkwrite(vmf, folio); 4805fb09a464SKirill A. Shutemov if (unlikely(!tmp || 4806fb09a464SKirill A. Shutemov (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { 48076f609b7eSSidhartha Kumar folio_put(folio); 4808f0c6d4d2SKirill A. Shutemov return tmp; 480969676147SMark Fasheh } 4810d0217ac0SNick Piggin } 4811fb09a464SKirill A. Shutemov 48129118c0cbSJan Kara ret |= finish_fault(vmf); 48137267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | 48147267ec00SKirill A. Shutemov VM_FAULT_RETRY))) { 48156f609b7eSSidhartha Kumar folio_unlock(folio); 48166f609b7eSSidhartha Kumar folio_put(folio); 4817f0c6d4d2SKirill A. Shutemov return ret; 48189637a5efSDavid Howells } 4819d00806b1SNick Piggin 482089b15332SJohannes Weiner ret |= fault_dirty_shared_page(vmf); 4821b827e496SNick Piggin return ret; 482254cb8821SNick Piggin } 4823d00806b1SNick Piggin 48249a95f3cfSPaul Cassella /* 4825c1e8d7c6SMichel Lespinasse * We enter with non-exclusive mmap_lock (to exclude vma changes, 48269a95f3cfSPaul Cassella * but allow concurrent faults). 4827c1e8d7c6SMichel Lespinasse * The mmap_lock may have been released depending on flags and our 48289138e47eSMatthew Wilcox (Oracle) * return value. See filemap_fault() and __folio_lock_or_retry(). 4829c1e8d7c6SMichel Lespinasse * If mmap_lock is released, vma may become invalid (for example 4830fc8efd2dSJan Stancek * by other thread calling munmap()). 48319a95f3cfSPaul Cassella */ 48322b740303SSouptick Joarder static vm_fault_t do_fault(struct vm_fault *vmf) 483354cb8821SNick Piggin { 483482b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 4835fc8efd2dSJan Stancek struct mm_struct *vm_mm = vma->vm_mm; 48362b740303SSouptick Joarder vm_fault_t ret; 483754cb8821SNick Piggin 4838ff09d7ecSAneesh Kumar K.V /* 4839ff09d7ecSAneesh Kumar K.V * The VMA was not fully populated on mmap() or missing VM_DONTEXPAND 4840ff09d7ecSAneesh Kumar K.V */ 4841ff09d7ecSAneesh Kumar K.V if (!vma->vm_ops->fault) { 48423db82b93SHugh Dickins vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, 48433db82b93SHugh Dickins vmf->address, &vmf->ptl); 48443db82b93SHugh Dickins if (unlikely(!vmf->pte)) 4845b0b9b3dfSHugh Dickins ret = VM_FAULT_SIGBUS; 4846ff09d7ecSAneesh Kumar K.V else { 4847ff09d7ecSAneesh Kumar K.V /* 4848ff09d7ecSAneesh Kumar K.V * Make sure this is not a temporary clearing of pte 4849ff09d7ecSAneesh Kumar K.V * by holding ptl and checking again. A R/M/W update 4850ff09d7ecSAneesh Kumar K.V * of pte involves: take ptl, clearing the pte so that 4851ff09d7ecSAneesh Kumar K.V * we don't have concurrent modification by hardware 4852ff09d7ecSAneesh Kumar K.V * followed by an update. 4853ff09d7ecSAneesh Kumar K.V */ 4854c33c7948SRyan Roberts if (unlikely(pte_none(ptep_get(vmf->pte)))) 4855ff09d7ecSAneesh Kumar K.V ret = VM_FAULT_SIGBUS; 4856ff09d7ecSAneesh Kumar K.V else 4857ff09d7ecSAneesh Kumar K.V ret = VM_FAULT_NOPAGE; 4858ff09d7ecSAneesh Kumar K.V 4859ff09d7ecSAneesh Kumar K.V pte_unmap_unlock(vmf->pte, vmf->ptl); 4860ff09d7ecSAneesh Kumar K.V } 4861ff09d7ecSAneesh Kumar K.V } else if (!(vmf->flags & FAULT_FLAG_WRITE)) 4862b0b9b3dfSHugh Dickins ret = do_read_fault(vmf); 4863b0b9b3dfSHugh Dickins else if (!(vma->vm_flags & VM_SHARED)) 4864b0b9b3dfSHugh Dickins ret = do_cow_fault(vmf); 4865b0b9b3dfSHugh Dickins else 4866b0b9b3dfSHugh Dickins ret = do_shared_fault(vmf); 4867b0b9b3dfSHugh Dickins 4868b0b9b3dfSHugh Dickins /* preallocated pagetable is unused: free it */ 4869b0b9b3dfSHugh Dickins if (vmf->prealloc_pte) { 4870fc8efd2dSJan Stancek pte_free(vm_mm, vmf->prealloc_pte); 48717f2b6ce8STobin C Harding vmf->prealloc_pte = NULL; 4872b0b9b3dfSHugh Dickins } 4873b0b9b3dfSHugh Dickins return ret; 487454cb8821SNick Piggin } 487554cb8821SNick Piggin 4876cda6d936SKefeng Wang int numa_migrate_prep(struct folio *folio, struct vm_area_struct *vma, 4877f4c0d836SYang Shi unsigned long addr, int page_nid, int *flags) 48789532fec1SMel Gorman { 4879cda6d936SKefeng Wang folio_get(folio); 48809532fec1SMel Gorman 4881fc137c0dSRaghavendra K T /* Record the current PID acceesing VMA */ 4882fc137c0dSRaghavendra K T vma_set_access_pid_bit(vma); 4883fc137c0dSRaghavendra K T 48849532fec1SMel Gorman count_vm_numa_event(NUMA_HINT_FAULTS); 488504bb2f94SRik van Riel if (page_nid == numa_node_id()) { 48869532fec1SMel Gorman count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL); 488704bb2f94SRik van Riel *flags |= TNF_FAULT_LOCAL; 488804bb2f94SRik van Riel } 48899532fec1SMel Gorman 489075c70128SKefeng Wang return mpol_misplaced(folio, vma, addr); 48919532fec1SMel Gorman } 48929532fec1SMel Gorman 48932b740303SSouptick Joarder static vm_fault_t do_numa_page(struct vm_fault *vmf) 4894d10e63f2SMel Gorman { 489582b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 48966695cf68SKefeng Wang struct folio *folio = NULL; 48976695cf68SKefeng Wang int nid = NUMA_NO_NODE; 48986a56ccbcSDavid Hildenbrand bool writable = false; 489990572890SPeter Zijlstra int last_cpupid; 4900cbee9f88SPeter Zijlstra int target_nid; 490104a86453SAneesh Kumar K.V pte_t pte, old_pte; 49026688cc05SPeter Zijlstra int flags = 0; 4903d10e63f2SMel Gorman 4904d10e63f2SMel Gorman /* 4905d10e63f2SMel Gorman * The "pte" at this point cannot be used safely without 4906d10e63f2SMel Gorman * validation through pte_unmap_same(). It's of NUMA type but 4907d10e63f2SMel Gorman * the pfn may be screwed if the read is non atomic. 4908d10e63f2SMel Gorman */ 490982b0f8c3SJan Kara spin_lock(vmf->ptl); 4910c33c7948SRyan Roberts if (unlikely(!pte_same(ptep_get(vmf->pte), vmf->orig_pte))) { 491182b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 49124daae3b4SMel Gorman goto out; 49134daae3b4SMel Gorman } 49144daae3b4SMel Gorman 4915b99a342dSHuang Ying /* Get the normal PTE */ 4916b99a342dSHuang Ying old_pte = ptep_get(vmf->pte); 491704a86453SAneesh Kumar K.V pte = pte_modify(old_pte, vma->vm_page_prot); 4918d10e63f2SMel Gorman 49196a56ccbcSDavid Hildenbrand /* 49206a56ccbcSDavid Hildenbrand * Detect now whether the PTE could be writable; this information 49216a56ccbcSDavid Hildenbrand * is only valid while holding the PT lock. 49226a56ccbcSDavid Hildenbrand */ 49236a56ccbcSDavid Hildenbrand writable = pte_write(pte); 49246a56ccbcSDavid Hildenbrand if (!writable && vma_wants_manual_pte_write_upgrade(vma) && 49256a56ccbcSDavid Hildenbrand can_change_pte_writable(vma, vmf->address, pte)) 49266a56ccbcSDavid Hildenbrand writable = true; 49276a56ccbcSDavid Hildenbrand 49286695cf68SKefeng Wang folio = vm_normal_folio(vma, vmf->address, pte); 49296695cf68SKefeng Wang if (!folio || folio_is_zone_device(folio)) 4930b99a342dSHuang Ying goto out_map; 4931d10e63f2SMel Gorman 4932e81c4802SKirill A. Shutemov /* TODO: handle PTE-mapped THP */ 49336695cf68SKefeng Wang if (folio_test_large(folio)) 4934b99a342dSHuang Ying goto out_map; 4935e81c4802SKirill A. Shutemov 49366688cc05SPeter Zijlstra /* 4937bea66fbdSMel Gorman * Avoid grouping on RO pages in general. RO pages shouldn't hurt as 4938bea66fbdSMel Gorman * much anyway since they can be in shared cache state. This misses 4939bea66fbdSMel Gorman * the case where a mapping is writable but the process never writes 4940bea66fbdSMel Gorman * to it but pte_write gets cleared during protection updates and 4941bea66fbdSMel Gorman * pte_dirty has unpredictable behaviour between PTE scan updates, 4942bea66fbdSMel Gorman * background writeback, dirty balancing and application behaviour. 49436688cc05SPeter Zijlstra */ 49446a56ccbcSDavid Hildenbrand if (!writable) 49456688cc05SPeter Zijlstra flags |= TNF_NO_GROUP; 49466688cc05SPeter Zijlstra 4947dabe1d99SRik van Riel /* 49486695cf68SKefeng Wang * Flag if the folio is shared between multiple address spaces. This 4949dabe1d99SRik van Riel * is later used when determining whether to group tasks together 4950dabe1d99SRik van Riel */ 49516695cf68SKefeng Wang if (folio_estimated_sharers(folio) > 1 && (vma->vm_flags & VM_SHARED)) 4952dabe1d99SRik van Riel flags |= TNF_SHARED; 4953dabe1d99SRik van Riel 49546695cf68SKefeng Wang nid = folio_nid(folio); 495533024536SHuang Ying /* 495633024536SHuang Ying * For memory tiering mode, cpupid of slow memory page is used 495733024536SHuang Ying * to record page access time. So use default value. 495833024536SHuang Ying */ 495933024536SHuang Ying if ((sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING) && 49606695cf68SKefeng Wang !node_is_toptier(nid)) 496133024536SHuang Ying last_cpupid = (-1 & LAST_CPUPID_MASK); 496233024536SHuang Ying else 496367b33e3fSKefeng Wang last_cpupid = folio_last_cpupid(folio); 4964cda6d936SKefeng Wang target_nid = numa_migrate_prep(folio, vma, vmf->address, nid, &flags); 496598fa15f3SAnshuman Khandual if (target_nid == NUMA_NO_NODE) { 49666695cf68SKefeng Wang folio_put(folio); 4967b99a342dSHuang Ying goto out_map; 49684daae3b4SMel Gorman } 4969b99a342dSHuang Ying pte_unmap_unlock(vmf->pte, vmf->ptl); 49706a56ccbcSDavid Hildenbrand writable = false; 49714daae3b4SMel Gorman 49724daae3b4SMel Gorman /* Migrate to the requested node */ 49736695cf68SKefeng Wang if (migrate_misplaced_folio(folio, vma, target_nid)) { 49746695cf68SKefeng Wang nid = target_nid; 49756688cc05SPeter Zijlstra flags |= TNF_MIGRATED; 4976b99a342dSHuang Ying } else { 4977074c2381SMel Gorman flags |= TNF_MIGRATE_FAIL; 4978c7ad0880SHugh Dickins vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 4979c7ad0880SHugh Dickins vmf->address, &vmf->ptl); 4980c7ad0880SHugh Dickins if (unlikely(!vmf->pte)) 4981c7ad0880SHugh Dickins goto out; 4982c33c7948SRyan Roberts if (unlikely(!pte_same(ptep_get(vmf->pte), vmf->orig_pte))) { 4983b99a342dSHuang Ying pte_unmap_unlock(vmf->pte, vmf->ptl); 4984b99a342dSHuang Ying goto out; 4985b99a342dSHuang Ying } 4986b99a342dSHuang Ying goto out_map; 4987b99a342dSHuang Ying } 49884daae3b4SMel Gorman 49894daae3b4SMel Gorman out: 49906695cf68SKefeng Wang if (nid != NUMA_NO_NODE) 49916695cf68SKefeng Wang task_numa_fault(last_cpupid, nid, 1, flags); 4992d10e63f2SMel Gorman return 0; 4993b99a342dSHuang Ying out_map: 4994b99a342dSHuang Ying /* 4995b99a342dSHuang Ying * Make it present again, depending on how arch implements 4996b99a342dSHuang Ying * non-accessible ptes, some can allow access by kernel mode. 4997b99a342dSHuang Ying */ 4998b99a342dSHuang Ying old_pte = ptep_modify_prot_start(vma, vmf->address, vmf->pte); 4999b99a342dSHuang Ying pte = pte_modify(old_pte, vma->vm_page_prot); 5000b99a342dSHuang Ying pte = pte_mkyoung(pte); 50016a56ccbcSDavid Hildenbrand if (writable) 5002161e393cSRick Edgecombe pte = pte_mkwrite(pte, vma); 5003b99a342dSHuang Ying ptep_modify_prot_commit(vma, vmf->address, vmf->pte, old_pte, pte); 50045003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vma, vmf->address, vmf->pte, 1); 5005b99a342dSHuang Ying pte_unmap_unlock(vmf->pte, vmf->ptl); 5006b99a342dSHuang Ying goto out; 5007d10e63f2SMel Gorman } 5008d10e63f2SMel Gorman 50092b740303SSouptick Joarder static inline vm_fault_t create_huge_pmd(struct vm_fault *vmf) 5010b96375f7SMatthew Wilcox { 50118f5fd0e1SMatthew Wilcox (Oracle) struct vm_area_struct *vma = vmf->vma; 50128f5fd0e1SMatthew Wilcox (Oracle) if (vma_is_anonymous(vma)) 501382b0f8c3SJan Kara return do_huge_pmd_anonymous_page(vmf); 501440d49a3cSMatthew Wilcox (Oracle) if (vma->vm_ops->huge_fault) 50151d024e7aSMatthew Wilcox (Oracle) return vma->vm_ops->huge_fault(vmf, PMD_ORDER); 5016b96375f7SMatthew Wilcox return VM_FAULT_FALLBACK; 5017b96375f7SMatthew Wilcox } 5018b96375f7SMatthew Wilcox 5019183f24aaSGeert Uytterhoeven /* `inline' is required to avoid gcc 4.1.2 build error */ 50205db4f15cSYang Shi static inline vm_fault_t wp_huge_pmd(struct vm_fault *vmf) 5021b96375f7SMatthew Wilcox { 50228f5fd0e1SMatthew Wilcox (Oracle) struct vm_area_struct *vma = vmf->vma; 5023c89357e2SDavid Hildenbrand const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE; 5024aea06577SDavid Hildenbrand vm_fault_t ret; 5025c89357e2SDavid Hildenbrand 50268f5fd0e1SMatthew Wilcox (Oracle) if (vma_is_anonymous(vma)) { 5027c89357e2SDavid Hildenbrand if (likely(!unshare) && 5028d61ea1cbSPeter Xu userfaultfd_huge_pmd_wp(vma, vmf->orig_pmd)) { 5029d61ea1cbSPeter Xu if (userfaultfd_wp_async(vmf->vma)) 5030d61ea1cbSPeter Xu goto split; 5031529b930bSAndrea Arcangeli return handle_userfault(vmf, VM_UFFD_WP); 5032d61ea1cbSPeter Xu } 50335db4f15cSYang Shi return do_huge_pmd_wp_page(vmf); 5034529b930bSAndrea Arcangeli } 5035af9e4d5fSKirill A. Shutemov 50368f5fd0e1SMatthew Wilcox (Oracle) if (vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) { 50378f5fd0e1SMatthew Wilcox (Oracle) if (vma->vm_ops->huge_fault) { 50381d024e7aSMatthew Wilcox (Oracle) ret = vma->vm_ops->huge_fault(vmf, PMD_ORDER); 5039327e9fd4SThomas Hellstrom (VMware) if (!(ret & VM_FAULT_FALLBACK)) 5040327e9fd4SThomas Hellstrom (VMware) return ret; 5041327e9fd4SThomas Hellstrom (VMware) } 5042aea06577SDavid Hildenbrand } 5043327e9fd4SThomas Hellstrom (VMware) 5044d61ea1cbSPeter Xu split: 5045327e9fd4SThomas Hellstrom (VMware) /* COW or write-notify handled on pte level: split pmd. */ 50468f5fd0e1SMatthew Wilcox (Oracle) __split_huge_pmd(vma, vmf->pmd, vmf->address, false, NULL); 5047af9e4d5fSKirill A. Shutemov 5048b96375f7SMatthew Wilcox return VM_FAULT_FALLBACK; 5049b96375f7SMatthew Wilcox } 5050b96375f7SMatthew Wilcox 50512b740303SSouptick Joarder static vm_fault_t create_huge_pud(struct vm_fault *vmf) 5052a00cc7d9SMatthew Wilcox { 5053327e9fd4SThomas Hellstrom (VMware) #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && \ 5054327e9fd4SThomas Hellstrom (VMware) defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) 5055c4fd825eSMatthew Wilcox (Oracle) struct vm_area_struct *vma = vmf->vma; 5056a00cc7d9SMatthew Wilcox /* No support for anonymous transparent PUD pages yet */ 5057c4fd825eSMatthew Wilcox (Oracle) if (vma_is_anonymous(vma)) 505814c99d65SGowans, James return VM_FAULT_FALLBACK; 505940d49a3cSMatthew Wilcox (Oracle) if (vma->vm_ops->huge_fault) 50601d024e7aSMatthew Wilcox (Oracle) return vma->vm_ops->huge_fault(vmf, PUD_ORDER); 506114c99d65SGowans, James #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 506214c99d65SGowans, James return VM_FAULT_FALLBACK; 506314c99d65SGowans, James } 506414c99d65SGowans, James 506514c99d65SGowans, James static vm_fault_t wp_huge_pud(struct vm_fault *vmf, pud_t orig_pud) 506614c99d65SGowans, James { 506714c99d65SGowans, James #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && \ 506814c99d65SGowans, James defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) 5069c4fd825eSMatthew Wilcox (Oracle) struct vm_area_struct *vma = vmf->vma; 5070aea06577SDavid Hildenbrand vm_fault_t ret; 5071aea06577SDavid Hildenbrand 507214c99d65SGowans, James /* No support for anonymous transparent PUD pages yet */ 5073c4fd825eSMatthew Wilcox (Oracle) if (vma_is_anonymous(vma)) 5074327e9fd4SThomas Hellstrom (VMware) goto split; 5075c4fd825eSMatthew Wilcox (Oracle) if (vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) { 5076c4fd825eSMatthew Wilcox (Oracle) if (vma->vm_ops->huge_fault) { 50771d024e7aSMatthew Wilcox (Oracle) ret = vma->vm_ops->huge_fault(vmf, PUD_ORDER); 5078327e9fd4SThomas Hellstrom (VMware) if (!(ret & VM_FAULT_FALLBACK)) 5079327e9fd4SThomas Hellstrom (VMware) return ret; 5080327e9fd4SThomas Hellstrom (VMware) } 5081aea06577SDavid Hildenbrand } 5082327e9fd4SThomas Hellstrom (VMware) split: 5083327e9fd4SThomas Hellstrom (VMware) /* COW or write-notify not handled on PUD level: split pud.*/ 5084c4fd825eSMatthew Wilcox (Oracle) __split_huge_pud(vma, vmf->pud, vmf->address); 508514c99d65SGowans, James #endif /* CONFIG_TRANSPARENT_HUGEPAGE && CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 5086a00cc7d9SMatthew Wilcox return VM_FAULT_FALLBACK; 5087a00cc7d9SMatthew Wilcox } 5088a00cc7d9SMatthew Wilcox 50891da177e4SLinus Torvalds /* 50901da177e4SLinus Torvalds * These routines also need to handle stuff like marking pages dirty 50911da177e4SLinus Torvalds * and/or accessed for architectures that don't do it in hardware (most 50921da177e4SLinus Torvalds * RISC architectures). The early dirtying is also good on the i386. 50931da177e4SLinus Torvalds * 50941da177e4SLinus Torvalds * There is also a hook called "update_mmu_cache()" that architectures 50951da177e4SLinus Torvalds * with external mmu caches can use to update those (ie the Sparc or 50961da177e4SLinus Torvalds * PowerPC hashed page tables that act as extended TLBs). 50971da177e4SLinus Torvalds * 5098c1e8d7c6SMichel Lespinasse * We enter with non-exclusive mmap_lock (to exclude vma changes, but allow 50997267ec00SKirill A. Shutemov * concurrent faults). 51009a95f3cfSPaul Cassella * 5101c1e8d7c6SMichel Lespinasse * The mmap_lock may have been released depending on flags and our return value. 51029138e47eSMatthew Wilcox (Oracle) * See filemap_fault() and __folio_lock_or_retry(). 51031da177e4SLinus Torvalds */ 51042b740303SSouptick Joarder static vm_fault_t handle_pte_fault(struct vm_fault *vmf) 51051da177e4SLinus Torvalds { 51061da177e4SLinus Torvalds pte_t entry; 51071da177e4SLinus Torvalds 510882b0f8c3SJan Kara if (unlikely(pmd_none(*vmf->pmd))) { 51097267ec00SKirill A. Shutemov /* 51107267ec00SKirill A. Shutemov * Leave __pte_alloc() until later: because vm_ops->fault may 51117267ec00SKirill A. Shutemov * want to allocate huge page, and if we expose page table 51127267ec00SKirill A. Shutemov * for an instant, it will be difficult to retract from 51137267ec00SKirill A. Shutemov * concurrent faults and from rmap lookups. 51147267ec00SKirill A. Shutemov */ 511582b0f8c3SJan Kara vmf->pte = NULL; 5116f46f2adeSPeter Xu vmf->flags &= ~FAULT_FLAG_ORIG_PTE_VALID; 51177267ec00SKirill A. Shutemov } else { 5118f9ce0be7SKirill A. Shutemov /* 51197267ec00SKirill A. Shutemov * A regular pmd is established and it can't morph into a huge 5120c7ad0880SHugh Dickins * pmd by anon khugepaged, since that takes mmap_lock in write 5121c7ad0880SHugh Dickins * mode; but shmem or file collapse to THP could still morph 5122c7ad0880SHugh Dickins * it into a huge pmd: just retry later if so. 51237267ec00SKirill A. Shutemov */ 5124c7ad0880SHugh Dickins vmf->pte = pte_offset_map_nolock(vmf->vma->vm_mm, vmf->pmd, 5125c7ad0880SHugh Dickins vmf->address, &vmf->ptl); 5126c7ad0880SHugh Dickins if (unlikely(!vmf->pte)) 5127c7ad0880SHugh Dickins return 0; 512826e1a0c3SHugh Dickins vmf->orig_pte = ptep_get_lockless(vmf->pte); 5129f46f2adeSPeter Xu vmf->flags |= FAULT_FLAG_ORIG_PTE_VALID; 51307267ec00SKirill A. Shutemov 51312994302bSJan Kara if (pte_none(vmf->orig_pte)) { 513282b0f8c3SJan Kara pte_unmap(vmf->pte); 513382b0f8c3SJan Kara vmf->pte = NULL; 51347267ec00SKirill A. Shutemov } 51357267ec00SKirill A. Shutemov } 51367267ec00SKirill A. Shutemov 51372bad466cSPeter Xu if (!vmf->pte) 51382bad466cSPeter Xu return do_pte_missing(vmf); 51397267ec00SKirill A. Shutemov 51402994302bSJan Kara if (!pte_present(vmf->orig_pte)) 51412994302bSJan Kara return do_swap_page(vmf); 51421da177e4SLinus Torvalds 51432994302bSJan Kara if (pte_protnone(vmf->orig_pte) && vma_is_accessible(vmf->vma)) 51442994302bSJan Kara return do_numa_page(vmf); 5145d10e63f2SMel Gorman 514682b0f8c3SJan Kara spin_lock(vmf->ptl); 51472994302bSJan Kara entry = vmf->orig_pte; 5148c33c7948SRyan Roberts if (unlikely(!pte_same(ptep_get(vmf->pte), entry))) { 51497df67697SBibo Mao update_mmu_tlb(vmf->vma, vmf->address, vmf->pte); 51508f4e2101SHugh Dickins goto unlock; 51517df67697SBibo Mao } 5152c89357e2SDavid Hildenbrand if (vmf->flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) { 5153f6f37321SLinus Torvalds if (!pte_write(entry)) 51542994302bSJan Kara return do_wp_page(vmf); 5155c89357e2SDavid Hildenbrand else if (likely(vmf->flags & FAULT_FLAG_WRITE)) 51561da177e4SLinus Torvalds entry = pte_mkdirty(entry); 51571da177e4SLinus Torvalds } 51581da177e4SLinus Torvalds entry = pte_mkyoung(entry); 515982b0f8c3SJan Kara if (ptep_set_access_flags(vmf->vma, vmf->address, vmf->pte, entry, 516082b0f8c3SJan Kara vmf->flags & FAULT_FLAG_WRITE)) { 51615003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vmf->vma, vmf->address, 51625003a2bdSMatthew Wilcox (Oracle) vmf->pte, 1); 51631a44e149SAndrea Arcangeli } else { 5164b7333b58SYang Shi /* Skip spurious TLB flush for retried page fault */ 5165b7333b58SYang Shi if (vmf->flags & FAULT_FLAG_TRIED) 5166b7333b58SYang Shi goto unlock; 51671a44e149SAndrea Arcangeli /* 51681a44e149SAndrea Arcangeli * This is needed only for protection faults but the arch code 51691a44e149SAndrea Arcangeli * is not yet telling us if this is a protection fault or not. 51701a44e149SAndrea Arcangeli * This still avoids useless tlb flushes for .text page faults 51711a44e149SAndrea Arcangeli * with threads. 51721a44e149SAndrea Arcangeli */ 517382b0f8c3SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) 517499c29133SGerald Schaefer flush_tlb_fix_spurious_fault(vmf->vma, vmf->address, 517599c29133SGerald Schaefer vmf->pte); 51761a44e149SAndrea Arcangeli } 51778f4e2101SHugh Dickins unlock: 517882b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 517983c54070SNick Piggin return 0; 51801da177e4SLinus Torvalds } 51811da177e4SLinus Torvalds 51821da177e4SLinus Torvalds /* 51834ec31152SMatthew Wilcox (Oracle) * On entry, we hold either the VMA lock or the mmap_lock 51844ec31152SMatthew Wilcox (Oracle) * (FAULT_FLAG_VMA_LOCK tells you which). If VM_FAULT_RETRY is set in 51854ec31152SMatthew Wilcox (Oracle) * the result, the mmap_lock is not held on exit. See filemap_fault() 51864ec31152SMatthew Wilcox (Oracle) * and __folio_lock_or_retry(). 51871da177e4SLinus Torvalds */ 51882b740303SSouptick Joarder static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma, 51892b740303SSouptick Joarder unsigned long address, unsigned int flags) 51901da177e4SLinus Torvalds { 519182b0f8c3SJan Kara struct vm_fault vmf = { 5192bae473a4SKirill A. Shutemov .vma = vma, 51931a29d85eSJan Kara .address = address & PAGE_MASK, 5194824ddc60SNadav Amit .real_address = address, 5195bae473a4SKirill A. Shutemov .flags = flags, 51960721ec8bSJan Kara .pgoff = linear_page_index(vma, address), 5197667240e0SJan Kara .gfp_mask = __get_fault_gfp_mask(vma), 5198bae473a4SKirill A. Shutemov }; 5199dcddffd4SKirill A. Shutemov struct mm_struct *mm = vma->vm_mm; 52007da4e2cbSYang Shi unsigned long vm_flags = vma->vm_flags; 52011da177e4SLinus Torvalds pgd_t *pgd; 5202c2febafcSKirill A. Shutemov p4d_t *p4d; 52032b740303SSouptick Joarder vm_fault_t ret; 52041da177e4SLinus Torvalds 52051da177e4SLinus Torvalds pgd = pgd_offset(mm, address); 5206c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, address); 5207c2febafcSKirill A. Shutemov if (!p4d) 5208c2febafcSKirill A. Shutemov return VM_FAULT_OOM; 5209a00cc7d9SMatthew Wilcox 5210c2febafcSKirill A. Shutemov vmf.pud = pud_alloc(mm, p4d, address); 5211a00cc7d9SMatthew Wilcox if (!vmf.pud) 5212c74df32cSHugh Dickins return VM_FAULT_OOM; 5213625110b5SThomas Hellstrom retry_pud: 52147da4e2cbSYang Shi if (pud_none(*vmf.pud) && 52153485b883SRyan Roberts thp_vma_allowable_order(vma, vm_flags, false, true, true, PUD_ORDER)) { 5216a00cc7d9SMatthew Wilcox ret = create_huge_pud(&vmf); 5217a00cc7d9SMatthew Wilcox if (!(ret & VM_FAULT_FALLBACK)) 5218a00cc7d9SMatthew Wilcox return ret; 5219a00cc7d9SMatthew Wilcox } else { 5220a00cc7d9SMatthew Wilcox pud_t orig_pud = *vmf.pud; 5221a00cc7d9SMatthew Wilcox 5222a00cc7d9SMatthew Wilcox barrier(); 5223a00cc7d9SMatthew Wilcox if (pud_trans_huge(orig_pud) || pud_devmap(orig_pud)) { 5224a00cc7d9SMatthew Wilcox 5225c89357e2SDavid Hildenbrand /* 5226c89357e2SDavid Hildenbrand * TODO once we support anonymous PUDs: NUMA case and 5227c89357e2SDavid Hildenbrand * FAULT_FLAG_UNSHARE handling. 5228c89357e2SDavid Hildenbrand */ 5229c89357e2SDavid Hildenbrand if ((flags & FAULT_FLAG_WRITE) && !pud_write(orig_pud)) { 5230a00cc7d9SMatthew Wilcox ret = wp_huge_pud(&vmf, orig_pud); 5231a00cc7d9SMatthew Wilcox if (!(ret & VM_FAULT_FALLBACK)) 5232a00cc7d9SMatthew Wilcox return ret; 5233a00cc7d9SMatthew Wilcox } else { 5234a00cc7d9SMatthew Wilcox huge_pud_set_accessed(&vmf, orig_pud); 5235a00cc7d9SMatthew Wilcox return 0; 5236a00cc7d9SMatthew Wilcox } 5237a00cc7d9SMatthew Wilcox } 5238a00cc7d9SMatthew Wilcox } 5239a00cc7d9SMatthew Wilcox 5240a00cc7d9SMatthew Wilcox vmf.pmd = pmd_alloc(mm, vmf.pud, address); 524182b0f8c3SJan Kara if (!vmf.pmd) 5242c74df32cSHugh Dickins return VM_FAULT_OOM; 5243625110b5SThomas Hellstrom 5244625110b5SThomas Hellstrom /* Huge pud page fault raced with pmd_alloc? */ 5245625110b5SThomas Hellstrom if (pud_trans_unstable(vmf.pud)) 5246625110b5SThomas Hellstrom goto retry_pud; 5247625110b5SThomas Hellstrom 52487da4e2cbSYang Shi if (pmd_none(*vmf.pmd) && 52493485b883SRyan Roberts thp_vma_allowable_order(vma, vm_flags, false, true, true, PMD_ORDER)) { 5250a2d58167SDave Jiang ret = create_huge_pmd(&vmf); 5251c0292554SKirill A. Shutemov if (!(ret & VM_FAULT_FALLBACK)) 5252c0292554SKirill A. Shutemov return ret; 525371e3aac0SAndrea Arcangeli } else { 525426e1a0c3SHugh Dickins vmf.orig_pmd = pmdp_get_lockless(vmf.pmd); 52551f1d06c3SDavid Rientjes 52565db4f15cSYang Shi if (unlikely(is_swap_pmd(vmf.orig_pmd))) { 525784c3fc4eSZi Yan VM_BUG_ON(thp_migration_supported() && 52585db4f15cSYang Shi !is_pmd_migration_entry(vmf.orig_pmd)); 52595db4f15cSYang Shi if (is_pmd_migration_entry(vmf.orig_pmd)) 526084c3fc4eSZi Yan pmd_migration_entry_wait(mm, vmf.pmd); 526184c3fc4eSZi Yan return 0; 526284c3fc4eSZi Yan } 52635db4f15cSYang Shi if (pmd_trans_huge(vmf.orig_pmd) || pmd_devmap(vmf.orig_pmd)) { 52645db4f15cSYang Shi if (pmd_protnone(vmf.orig_pmd) && vma_is_accessible(vma)) 52655db4f15cSYang Shi return do_huge_pmd_numa_page(&vmf); 5266d10e63f2SMel Gorman 5267c89357e2SDavid Hildenbrand if ((flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) && 5268c89357e2SDavid Hildenbrand !pmd_write(vmf.orig_pmd)) { 52695db4f15cSYang Shi ret = wp_huge_pmd(&vmf); 52709845cbbdSKirill A. Shutemov if (!(ret & VM_FAULT_FALLBACK)) 52711f1d06c3SDavid Rientjes return ret; 5272a1dd450bSWill Deacon } else { 52735db4f15cSYang Shi huge_pmd_set_accessed(&vmf); 527471e3aac0SAndrea Arcangeli return 0; 527571e3aac0SAndrea Arcangeli } 527671e3aac0SAndrea Arcangeli } 52779845cbbdSKirill A. Shutemov } 527871e3aac0SAndrea Arcangeli 527982b0f8c3SJan Kara return handle_pte_fault(&vmf); 52801da177e4SLinus Torvalds } 52811da177e4SLinus Torvalds 5282bce617edSPeter Xu /** 5283f0953a1bSIngo Molnar * mm_account_fault - Do page fault accounting 5284809ef83cSYang Li * @mm: mm from which memcg should be extracted. It can be NULL. 5285bce617edSPeter Xu * @regs: the pt_regs struct pointer. When set to NULL, will skip accounting 5286bce617edSPeter Xu * of perf event counters, but we'll still do the per-task accounting to 5287bce617edSPeter Xu * the task who triggered this page fault. 5288bce617edSPeter Xu * @address: the faulted address. 5289bce617edSPeter Xu * @flags: the fault flags. 5290bce617edSPeter Xu * @ret: the fault retcode. 5291bce617edSPeter Xu * 5292f0953a1bSIngo Molnar * This will take care of most of the page fault accounting. Meanwhile, it 5293bce617edSPeter Xu * will also include the PERF_COUNT_SW_PAGE_FAULTS_[MAJ|MIN] perf counter 5294f0953a1bSIngo Molnar * updates. However, note that the handling of PERF_COUNT_SW_PAGE_FAULTS should 5295bce617edSPeter Xu * still be in per-arch page fault handlers at the entry of page fault. 5296bce617edSPeter Xu */ 529753156443SSuren Baghdasaryan static inline void mm_account_fault(struct mm_struct *mm, struct pt_regs *regs, 5298bce617edSPeter Xu unsigned long address, unsigned int flags, 5299bce617edSPeter Xu vm_fault_t ret) 5300bce617edSPeter Xu { 5301bce617edSPeter Xu bool major; 5302bce617edSPeter Xu 530353156443SSuren Baghdasaryan /* Incomplete faults will be accounted upon completion. */ 530453156443SSuren Baghdasaryan if (ret & VM_FAULT_RETRY) 530553156443SSuren Baghdasaryan return; 530653156443SSuren Baghdasaryan 5307bce617edSPeter Xu /* 530853156443SSuren Baghdasaryan * To preserve the behavior of older kernels, PGFAULT counters record 530953156443SSuren Baghdasaryan * both successful and failed faults, as opposed to perf counters, 531053156443SSuren Baghdasaryan * which ignore failed cases. 5311bce617edSPeter Xu */ 531253156443SSuren Baghdasaryan count_vm_event(PGFAULT); 531353156443SSuren Baghdasaryan count_memcg_event_mm(mm, PGFAULT); 531453156443SSuren Baghdasaryan 531553156443SSuren Baghdasaryan /* 531653156443SSuren Baghdasaryan * Do not account for unsuccessful faults (e.g. when the address wasn't 531753156443SSuren Baghdasaryan * valid). That includes arch_vma_access_permitted() failing before 531853156443SSuren Baghdasaryan * reaching here. So this is not a "this many hardware page faults" 531953156443SSuren Baghdasaryan * counter. We should use the hw profiling for that. 532053156443SSuren Baghdasaryan */ 532153156443SSuren Baghdasaryan if (ret & VM_FAULT_ERROR) 5322bce617edSPeter Xu return; 5323bce617edSPeter Xu 5324bce617edSPeter Xu /* 5325bce617edSPeter Xu * We define the fault as a major fault when the final successful fault 5326bce617edSPeter Xu * is VM_FAULT_MAJOR, or if it retried (which implies that we couldn't 5327bce617edSPeter Xu * handle it immediately previously). 5328bce617edSPeter Xu */ 5329bce617edSPeter Xu major = (ret & VM_FAULT_MAJOR) || (flags & FAULT_FLAG_TRIED); 5330bce617edSPeter Xu 5331a2beb5f1SPeter Xu if (major) 5332a2beb5f1SPeter Xu current->maj_flt++; 5333a2beb5f1SPeter Xu else 5334a2beb5f1SPeter Xu current->min_flt++; 5335a2beb5f1SPeter Xu 5336bce617edSPeter Xu /* 5337a2beb5f1SPeter Xu * If the fault is done for GUP, regs will be NULL. We only do the 5338a2beb5f1SPeter Xu * accounting for the per thread fault counters who triggered the 5339a2beb5f1SPeter Xu * fault, and we skip the perf event updates. 5340bce617edSPeter Xu */ 5341bce617edSPeter Xu if (!regs) 5342bce617edSPeter Xu return; 5343bce617edSPeter Xu 5344a2beb5f1SPeter Xu if (major) 5345bce617edSPeter Xu perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address); 5346a2beb5f1SPeter Xu else 5347bce617edSPeter Xu perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address); 5348bce617edSPeter Xu } 5349bce617edSPeter Xu 5350ec1c86b2SYu Zhao #ifdef CONFIG_LRU_GEN 5351ec1c86b2SYu Zhao static void lru_gen_enter_fault(struct vm_area_struct *vma) 5352ec1c86b2SYu Zhao { 53538788f678SYu Zhao /* the LRU algorithm only applies to accesses with recency */ 53548788f678SYu Zhao current->in_lru_fault = vma_has_recency(vma); 5355ec1c86b2SYu Zhao } 5356ec1c86b2SYu Zhao 5357ec1c86b2SYu Zhao static void lru_gen_exit_fault(void) 5358ec1c86b2SYu Zhao { 5359ec1c86b2SYu Zhao current->in_lru_fault = false; 5360ec1c86b2SYu Zhao } 5361ec1c86b2SYu Zhao #else 5362ec1c86b2SYu Zhao static void lru_gen_enter_fault(struct vm_area_struct *vma) 5363ec1c86b2SYu Zhao { 5364ec1c86b2SYu Zhao } 5365ec1c86b2SYu Zhao 5366ec1c86b2SYu Zhao static void lru_gen_exit_fault(void) 5367ec1c86b2SYu Zhao { 5368ec1c86b2SYu Zhao } 5369ec1c86b2SYu Zhao #endif /* CONFIG_LRU_GEN */ 5370ec1c86b2SYu Zhao 5371cdc5021cSDavid Hildenbrand static vm_fault_t sanitize_fault_flags(struct vm_area_struct *vma, 5372cdc5021cSDavid Hildenbrand unsigned int *flags) 5373cdc5021cSDavid Hildenbrand { 5374cdc5021cSDavid Hildenbrand if (unlikely(*flags & FAULT_FLAG_UNSHARE)) { 5375cdc5021cSDavid Hildenbrand if (WARN_ON_ONCE(*flags & FAULT_FLAG_WRITE)) 5376cdc5021cSDavid Hildenbrand return VM_FAULT_SIGSEGV; 5377cdc5021cSDavid Hildenbrand /* 5378cdc5021cSDavid Hildenbrand * FAULT_FLAG_UNSHARE only applies to COW mappings. Let's 5379cdc5021cSDavid Hildenbrand * just treat it like an ordinary read-fault otherwise. 5380cdc5021cSDavid Hildenbrand */ 5381cdc5021cSDavid Hildenbrand if (!is_cow_mapping(vma->vm_flags)) 5382cdc5021cSDavid Hildenbrand *flags &= ~FAULT_FLAG_UNSHARE; 538379881fedSDavid Hildenbrand } else if (*flags & FAULT_FLAG_WRITE) { 538479881fedSDavid Hildenbrand /* Write faults on read-only mappings are impossible ... */ 538579881fedSDavid Hildenbrand if (WARN_ON_ONCE(!(vma->vm_flags & VM_MAYWRITE))) 538679881fedSDavid Hildenbrand return VM_FAULT_SIGSEGV; 538779881fedSDavid Hildenbrand /* ... and FOLL_FORCE only applies to COW mappings. */ 538879881fedSDavid Hildenbrand if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE) && 538979881fedSDavid Hildenbrand !is_cow_mapping(vma->vm_flags))) 539079881fedSDavid Hildenbrand return VM_FAULT_SIGSEGV; 5391cdc5021cSDavid Hildenbrand } 53924089eef0SSuren Baghdasaryan #ifdef CONFIG_PER_VMA_LOCK 53934089eef0SSuren Baghdasaryan /* 53944089eef0SSuren Baghdasaryan * Per-VMA locks can't be used with FAULT_FLAG_RETRY_NOWAIT because of 53954089eef0SSuren Baghdasaryan * the assumption that lock is dropped on VM_FAULT_RETRY. 53964089eef0SSuren Baghdasaryan */ 53974089eef0SSuren Baghdasaryan if (WARN_ON_ONCE((*flags & 53984089eef0SSuren Baghdasaryan (FAULT_FLAG_VMA_LOCK | FAULT_FLAG_RETRY_NOWAIT)) == 53994089eef0SSuren Baghdasaryan (FAULT_FLAG_VMA_LOCK | FAULT_FLAG_RETRY_NOWAIT))) 54004089eef0SSuren Baghdasaryan return VM_FAULT_SIGSEGV; 54014089eef0SSuren Baghdasaryan #endif 54024089eef0SSuren Baghdasaryan 5403cdc5021cSDavid Hildenbrand return 0; 5404cdc5021cSDavid Hildenbrand } 5405cdc5021cSDavid Hildenbrand 54069a95f3cfSPaul Cassella /* 54079a95f3cfSPaul Cassella * By the time we get here, we already hold the mm semaphore 54089a95f3cfSPaul Cassella * 5409c1e8d7c6SMichel Lespinasse * The mmap_lock may have been released depending on flags and our 54109138e47eSMatthew Wilcox (Oracle) * return value. See filemap_fault() and __folio_lock_or_retry(). 54119a95f3cfSPaul Cassella */ 54122b740303SSouptick Joarder vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address, 5413bce617edSPeter Xu unsigned int flags, struct pt_regs *regs) 5414519e5247SJohannes Weiner { 541553156443SSuren Baghdasaryan /* If the fault handler drops the mmap_lock, vma may be freed */ 541653156443SSuren Baghdasaryan struct mm_struct *mm = vma->vm_mm; 54172b740303SSouptick Joarder vm_fault_t ret; 5418519e5247SJohannes Weiner 5419519e5247SJohannes Weiner __set_current_state(TASK_RUNNING); 5420519e5247SJohannes Weiner 5421cdc5021cSDavid Hildenbrand ret = sanitize_fault_flags(vma, &flags); 5422cdc5021cSDavid Hildenbrand if (ret) 542353156443SSuren Baghdasaryan goto out; 5424cdc5021cSDavid Hildenbrand 5425de0c799bSLaurent Dufour if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE, 5426de0c799bSLaurent Dufour flags & FAULT_FLAG_INSTRUCTION, 542753156443SSuren Baghdasaryan flags & FAULT_FLAG_REMOTE)) { 542853156443SSuren Baghdasaryan ret = VM_FAULT_SIGSEGV; 542953156443SSuren Baghdasaryan goto out; 543053156443SSuren Baghdasaryan } 5431de0c799bSLaurent Dufour 5432519e5247SJohannes Weiner /* 5433519e5247SJohannes Weiner * Enable the memcg OOM handling for faults triggered in user 5434519e5247SJohannes Weiner * space. Kernel faults are handled more gracefully. 5435519e5247SJohannes Weiner */ 5436519e5247SJohannes Weiner if (flags & FAULT_FLAG_USER) 543729ef680aSMichal Hocko mem_cgroup_enter_user_fault(); 5438519e5247SJohannes Weiner 5439ec1c86b2SYu Zhao lru_gen_enter_fault(vma); 5440ec1c86b2SYu Zhao 5441bae473a4SKirill A. Shutemov if (unlikely(is_vm_hugetlb_page(vma))) 5442bae473a4SKirill A. Shutemov ret = hugetlb_fault(vma->vm_mm, vma, address, flags); 5443bae473a4SKirill A. Shutemov else 5444dcddffd4SKirill A. Shutemov ret = __handle_mm_fault(vma, address, flags); 5445519e5247SJohannes Weiner 5446ec1c86b2SYu Zhao lru_gen_exit_fault(); 5447ec1c86b2SYu Zhao 544849426420SJohannes Weiner if (flags & FAULT_FLAG_USER) { 544929ef680aSMichal Hocko mem_cgroup_exit_user_fault(); 545049426420SJohannes Weiner /* 545149426420SJohannes Weiner * The task may have entered a memcg OOM situation but 545249426420SJohannes Weiner * if the allocation error was handled gracefully (no 545349426420SJohannes Weiner * VM_FAULT_OOM), there is no need to kill anything. 545449426420SJohannes Weiner * Just clean up the OOM state peacefully. 545549426420SJohannes Weiner */ 545649426420SJohannes Weiner if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM)) 545749426420SJohannes Weiner mem_cgroup_oom_synchronize(false); 545849426420SJohannes Weiner } 545953156443SSuren Baghdasaryan out: 546053156443SSuren Baghdasaryan mm_account_fault(mm, regs, address, flags, ret); 5461bce617edSPeter Xu 5462519e5247SJohannes Weiner return ret; 5463519e5247SJohannes Weiner } 5464e1d6d01aSJesse Barnes EXPORT_SYMBOL_GPL(handle_mm_fault); 5465519e5247SJohannes Weiner 5466c2508ec5SLinus Torvalds #ifdef CONFIG_LOCK_MM_AND_FIND_VMA 5467c2508ec5SLinus Torvalds #include <linux/extable.h> 5468c2508ec5SLinus Torvalds 5469c2508ec5SLinus Torvalds static inline bool get_mmap_lock_carefully(struct mm_struct *mm, struct pt_regs *regs) 5470c2508ec5SLinus Torvalds { 54714542057eSLinus Torvalds if (likely(mmap_read_trylock(mm))) 5472c2508ec5SLinus Torvalds return true; 5473c2508ec5SLinus Torvalds 5474c2508ec5SLinus Torvalds if (regs && !user_mode(regs)) { 5475c2508ec5SLinus Torvalds unsigned long ip = instruction_pointer(regs); 5476c2508ec5SLinus Torvalds if (!search_exception_tables(ip)) 5477c2508ec5SLinus Torvalds return false; 5478c2508ec5SLinus Torvalds } 5479c2508ec5SLinus Torvalds 5480eda00472SLinus Torvalds return !mmap_read_lock_killable(mm); 5481c2508ec5SLinus Torvalds } 5482c2508ec5SLinus Torvalds 5483c2508ec5SLinus Torvalds static inline bool mmap_upgrade_trylock(struct mm_struct *mm) 5484c2508ec5SLinus Torvalds { 5485c2508ec5SLinus Torvalds /* 5486c2508ec5SLinus Torvalds * We don't have this operation yet. 5487c2508ec5SLinus Torvalds * 5488c2508ec5SLinus Torvalds * It should be easy enough to do: it's basically a 5489c2508ec5SLinus Torvalds * atomic_long_try_cmpxchg_acquire() 5490c2508ec5SLinus Torvalds * from RWSEM_READER_BIAS -> RWSEM_WRITER_LOCKED, but 5491c2508ec5SLinus Torvalds * it also needs the proper lockdep magic etc. 5492c2508ec5SLinus Torvalds */ 5493c2508ec5SLinus Torvalds return false; 5494c2508ec5SLinus Torvalds } 5495c2508ec5SLinus Torvalds 5496c2508ec5SLinus Torvalds static inline bool upgrade_mmap_lock_carefully(struct mm_struct *mm, struct pt_regs *regs) 5497c2508ec5SLinus Torvalds { 5498c2508ec5SLinus Torvalds mmap_read_unlock(mm); 5499c2508ec5SLinus Torvalds if (regs && !user_mode(regs)) { 5500c2508ec5SLinus Torvalds unsigned long ip = instruction_pointer(regs); 5501c2508ec5SLinus Torvalds if (!search_exception_tables(ip)) 5502c2508ec5SLinus Torvalds return false; 5503c2508ec5SLinus Torvalds } 5504eda00472SLinus Torvalds return !mmap_write_lock_killable(mm); 5505c2508ec5SLinus Torvalds } 5506c2508ec5SLinus Torvalds 5507c2508ec5SLinus Torvalds /* 5508c2508ec5SLinus Torvalds * Helper for page fault handling. 5509c2508ec5SLinus Torvalds * 5510c2508ec5SLinus Torvalds * This is kind of equivalend to "mmap_read_lock()" followed 5511c2508ec5SLinus Torvalds * by "find_extend_vma()", except it's a lot more careful about 5512c2508ec5SLinus Torvalds * the locking (and will drop the lock on failure). 5513c2508ec5SLinus Torvalds * 5514c2508ec5SLinus Torvalds * For example, if we have a kernel bug that causes a page 5515c2508ec5SLinus Torvalds * fault, we don't want to just use mmap_read_lock() to get 5516c2508ec5SLinus Torvalds * the mm lock, because that would deadlock if the bug were 5517c2508ec5SLinus Torvalds * to happen while we're holding the mm lock for writing. 5518c2508ec5SLinus Torvalds * 5519c2508ec5SLinus Torvalds * So this checks the exception tables on kernel faults in 5520c2508ec5SLinus Torvalds * order to only do this all for instructions that are actually 5521c2508ec5SLinus Torvalds * expected to fault. 5522c2508ec5SLinus Torvalds * 5523c2508ec5SLinus Torvalds * We can also actually take the mm lock for writing if we 5524c2508ec5SLinus Torvalds * need to extend the vma, which helps the VM layer a lot. 5525c2508ec5SLinus Torvalds */ 5526c2508ec5SLinus Torvalds struct vm_area_struct *lock_mm_and_find_vma(struct mm_struct *mm, 5527c2508ec5SLinus Torvalds unsigned long addr, struct pt_regs *regs) 5528c2508ec5SLinus Torvalds { 5529c2508ec5SLinus Torvalds struct vm_area_struct *vma; 5530c2508ec5SLinus Torvalds 5531c2508ec5SLinus Torvalds if (!get_mmap_lock_carefully(mm, regs)) 5532c2508ec5SLinus Torvalds return NULL; 5533c2508ec5SLinus Torvalds 5534c2508ec5SLinus Torvalds vma = find_vma(mm, addr); 5535c2508ec5SLinus Torvalds if (likely(vma && (vma->vm_start <= addr))) 5536c2508ec5SLinus Torvalds return vma; 5537c2508ec5SLinus Torvalds 5538c2508ec5SLinus Torvalds /* 5539c2508ec5SLinus Torvalds * Well, dang. We might still be successful, but only 5540c2508ec5SLinus Torvalds * if we can extend a vma to do so. 5541c2508ec5SLinus Torvalds */ 5542c2508ec5SLinus Torvalds if (!vma || !(vma->vm_flags & VM_GROWSDOWN)) { 5543c2508ec5SLinus Torvalds mmap_read_unlock(mm); 5544c2508ec5SLinus Torvalds return NULL; 5545c2508ec5SLinus Torvalds } 5546c2508ec5SLinus Torvalds 5547c2508ec5SLinus Torvalds /* 5548c2508ec5SLinus Torvalds * We can try to upgrade the mmap lock atomically, 5549c2508ec5SLinus Torvalds * in which case we can continue to use the vma 5550c2508ec5SLinus Torvalds * we already looked up. 5551c2508ec5SLinus Torvalds * 5552c2508ec5SLinus Torvalds * Otherwise we'll have to drop the mmap lock and 5553c2508ec5SLinus Torvalds * re-take it, and also look up the vma again, 5554c2508ec5SLinus Torvalds * re-checking it. 5555c2508ec5SLinus Torvalds */ 5556c2508ec5SLinus Torvalds if (!mmap_upgrade_trylock(mm)) { 5557c2508ec5SLinus Torvalds if (!upgrade_mmap_lock_carefully(mm, regs)) 5558c2508ec5SLinus Torvalds return NULL; 5559c2508ec5SLinus Torvalds 5560c2508ec5SLinus Torvalds vma = find_vma(mm, addr); 5561c2508ec5SLinus Torvalds if (!vma) 5562c2508ec5SLinus Torvalds goto fail; 5563c2508ec5SLinus Torvalds if (vma->vm_start <= addr) 5564c2508ec5SLinus Torvalds goto success; 5565c2508ec5SLinus Torvalds if (!(vma->vm_flags & VM_GROWSDOWN)) 5566c2508ec5SLinus Torvalds goto fail; 5567c2508ec5SLinus Torvalds } 5568c2508ec5SLinus Torvalds 55698d7071afSLinus Torvalds if (expand_stack_locked(vma, addr)) 5570c2508ec5SLinus Torvalds goto fail; 5571c2508ec5SLinus Torvalds 5572c2508ec5SLinus Torvalds success: 5573c2508ec5SLinus Torvalds mmap_write_downgrade(mm); 5574c2508ec5SLinus Torvalds return vma; 5575c2508ec5SLinus Torvalds 5576c2508ec5SLinus Torvalds fail: 5577c2508ec5SLinus Torvalds mmap_write_unlock(mm); 5578c2508ec5SLinus Torvalds return NULL; 5579c2508ec5SLinus Torvalds } 5580c2508ec5SLinus Torvalds #endif 5581c2508ec5SLinus Torvalds 558250ee3253SSuren Baghdasaryan #ifdef CONFIG_PER_VMA_LOCK 558350ee3253SSuren Baghdasaryan /* 558450ee3253SSuren Baghdasaryan * Lookup and lock a VMA under RCU protection. Returned VMA is guaranteed to be 558550ee3253SSuren Baghdasaryan * stable and not isolated. If the VMA is not found or is being modified the 558650ee3253SSuren Baghdasaryan * function returns NULL. 558750ee3253SSuren Baghdasaryan */ 558850ee3253SSuren Baghdasaryan struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm, 558950ee3253SSuren Baghdasaryan unsigned long address) 559050ee3253SSuren Baghdasaryan { 559150ee3253SSuren Baghdasaryan MA_STATE(mas, &mm->mm_mt, address, address); 559250ee3253SSuren Baghdasaryan struct vm_area_struct *vma; 559350ee3253SSuren Baghdasaryan 559450ee3253SSuren Baghdasaryan rcu_read_lock(); 559550ee3253SSuren Baghdasaryan retry: 559650ee3253SSuren Baghdasaryan vma = mas_walk(&mas); 559750ee3253SSuren Baghdasaryan if (!vma) 559850ee3253SSuren Baghdasaryan goto inval; 559950ee3253SSuren Baghdasaryan 560050ee3253SSuren Baghdasaryan if (!vma_start_read(vma)) 560150ee3253SSuren Baghdasaryan goto inval; 560250ee3253SSuren Baghdasaryan 5603444eeb17SSuren Baghdasaryan /* 5604657b5146SJann Horn * find_mergeable_anon_vma uses adjacent vmas which are not locked. 5605657b5146SJann Horn * This check must happen after vma_start_read(); otherwise, a 5606657b5146SJann Horn * concurrent mremap() with MREMAP_DONTUNMAP could dissociate the VMA 5607657b5146SJann Horn * from its anon_vma. 5608657b5146SJann Horn */ 560929a22b9eSSuren Baghdasaryan if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma)) 5610657b5146SJann Horn goto inval_end_read; 5611444eeb17SSuren Baghdasaryan 561250ee3253SSuren Baghdasaryan /* Check since vm_start/vm_end might change before we lock the VMA */ 5613657b5146SJann Horn if (unlikely(address < vma->vm_start || address >= vma->vm_end)) 5614657b5146SJann Horn goto inval_end_read; 561550ee3253SSuren Baghdasaryan 561650ee3253SSuren Baghdasaryan /* Check if the VMA got isolated after we found it */ 561750ee3253SSuren Baghdasaryan if (vma->detached) { 561850ee3253SSuren Baghdasaryan vma_end_read(vma); 561952f23865SSuren Baghdasaryan count_vm_vma_lock_event(VMA_LOCK_MISS); 562050ee3253SSuren Baghdasaryan /* The area was replaced with another one */ 562150ee3253SSuren Baghdasaryan goto retry; 562250ee3253SSuren Baghdasaryan } 562350ee3253SSuren Baghdasaryan 562450ee3253SSuren Baghdasaryan rcu_read_unlock(); 562550ee3253SSuren Baghdasaryan return vma; 5626657b5146SJann Horn 5627657b5146SJann Horn inval_end_read: 5628657b5146SJann Horn vma_end_read(vma); 562950ee3253SSuren Baghdasaryan inval: 563050ee3253SSuren Baghdasaryan rcu_read_unlock(); 563152f23865SSuren Baghdasaryan count_vm_vma_lock_event(VMA_LOCK_ABORT); 563250ee3253SSuren Baghdasaryan return NULL; 563350ee3253SSuren Baghdasaryan } 563450ee3253SSuren Baghdasaryan #endif /* CONFIG_PER_VMA_LOCK */ 563550ee3253SSuren Baghdasaryan 563690eceff1SKirill A. Shutemov #ifndef __PAGETABLE_P4D_FOLDED 563790eceff1SKirill A. Shutemov /* 563890eceff1SKirill A. Shutemov * Allocate p4d page table. 563990eceff1SKirill A. Shutemov * We've already handled the fast-path in-line. 564090eceff1SKirill A. Shutemov */ 564190eceff1SKirill A. Shutemov int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address) 564290eceff1SKirill A. Shutemov { 564390eceff1SKirill A. Shutemov p4d_t *new = p4d_alloc_one(mm, address); 564490eceff1SKirill A. Shutemov if (!new) 564590eceff1SKirill A. Shutemov return -ENOMEM; 564690eceff1SKirill A. Shutemov 564790eceff1SKirill A. Shutemov spin_lock(&mm->page_table_lock); 5648ed33b5a6SQi Zheng if (pgd_present(*pgd)) { /* Another has populated it */ 564990eceff1SKirill A. Shutemov p4d_free(mm, new); 5650ed33b5a6SQi Zheng } else { 5651ed33b5a6SQi Zheng smp_wmb(); /* See comment in pmd_install() */ 565290eceff1SKirill A. Shutemov pgd_populate(mm, pgd, new); 5653ed33b5a6SQi Zheng } 565490eceff1SKirill A. Shutemov spin_unlock(&mm->page_table_lock); 565590eceff1SKirill A. Shutemov return 0; 565690eceff1SKirill A. Shutemov } 565790eceff1SKirill A. Shutemov #endif /* __PAGETABLE_P4D_FOLDED */ 565890eceff1SKirill A. Shutemov 56591da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED 56601da177e4SLinus Torvalds /* 56611da177e4SLinus Torvalds * Allocate page upper directory. 5662872fec16SHugh Dickins * We've already handled the fast-path in-line. 56631da177e4SLinus Torvalds */ 5664c2febafcSKirill A. Shutemov int __pud_alloc(struct mm_struct *mm, p4d_t *p4d, unsigned long address) 56651da177e4SLinus Torvalds { 5666c74df32cSHugh Dickins pud_t *new = pud_alloc_one(mm, address); 5667c74df32cSHugh Dickins if (!new) 56681bb3630eSHugh Dickins return -ENOMEM; 56691da177e4SLinus Torvalds 5670872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 5671b4e98d9aSKirill A. Shutemov if (!p4d_present(*p4d)) { 5672b4e98d9aSKirill A. Shutemov mm_inc_nr_puds(mm); 5673ed33b5a6SQi Zheng smp_wmb(); /* See comment in pmd_install() */ 5674c2febafcSKirill A. Shutemov p4d_populate(mm, p4d, new); 5675b4e98d9aSKirill A. Shutemov } else /* Another has populated it */ 5676c2febafcSKirill A. Shutemov pud_free(mm, new); 5677872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 56781bb3630eSHugh Dickins return 0; 56791da177e4SLinus Torvalds } 56801da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */ 56811da177e4SLinus Torvalds 56821da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED 56831da177e4SLinus Torvalds /* 56841da177e4SLinus Torvalds * Allocate page middle directory. 5685872fec16SHugh Dickins * We've already handled the fast-path in-line. 56861da177e4SLinus Torvalds */ 56871bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) 56881da177e4SLinus Torvalds { 5689a00cc7d9SMatthew Wilcox spinlock_t *ptl; 5690c74df32cSHugh Dickins pmd_t *new = pmd_alloc_one(mm, address); 5691c74df32cSHugh Dickins if (!new) 56921bb3630eSHugh Dickins return -ENOMEM; 56931da177e4SLinus Torvalds 5694a00cc7d9SMatthew Wilcox ptl = pud_lock(mm, pud); 5695dc6c9a35SKirill A. Shutemov if (!pud_present(*pud)) { 5696dc6c9a35SKirill A. Shutemov mm_inc_nr_pmds(mm); 5697ed33b5a6SQi Zheng smp_wmb(); /* See comment in pmd_install() */ 56981da177e4SLinus Torvalds pud_populate(mm, pud, new); 5699ed33b5a6SQi Zheng } else { /* Another has populated it */ 57005e541973SBenjamin Herrenschmidt pmd_free(mm, new); 5701ed33b5a6SQi Zheng } 5702a00cc7d9SMatthew Wilcox spin_unlock(ptl); 57031bb3630eSHugh Dickins return 0; 57041da177e4SLinus Torvalds } 57051da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */ 57061da177e4SLinus Torvalds 57073b6748e2SJohannes Weiner /** 57089fd6dad1SPaolo Bonzini * follow_pte - look up PTE at a user virtual address 57099fd6dad1SPaolo Bonzini * @mm: the mm_struct of the target address space 57109fd6dad1SPaolo Bonzini * @address: user virtual address 57119fd6dad1SPaolo Bonzini * @ptepp: location to store found PTE 57129fd6dad1SPaolo Bonzini * @ptlp: location to store the lock for the PTE 57139fd6dad1SPaolo Bonzini * 57149fd6dad1SPaolo Bonzini * On a successful return, the pointer to the PTE is stored in @ptepp; 57159fd6dad1SPaolo Bonzini * the corresponding lock is taken and its location is stored in @ptlp. 57169fd6dad1SPaolo Bonzini * The contents of the PTE are only stable until @ptlp is released; 57179fd6dad1SPaolo Bonzini * any further use, if any, must be protected against invalidation 57189fd6dad1SPaolo Bonzini * with MMU notifiers. 57199fd6dad1SPaolo Bonzini * 57209fd6dad1SPaolo Bonzini * Only IO mappings and raw PFN mappings are allowed. The mmap semaphore 57219fd6dad1SPaolo Bonzini * should be taken for read. 57229fd6dad1SPaolo Bonzini * 57239fd6dad1SPaolo Bonzini * KVM uses this function. While it is arguably less bad than ``follow_pfn``, 57249fd6dad1SPaolo Bonzini * it is not a good general-purpose API. 57259fd6dad1SPaolo Bonzini * 57269fd6dad1SPaolo Bonzini * Return: zero on success, -ve otherwise. 57279fd6dad1SPaolo Bonzini */ 57289fd6dad1SPaolo Bonzini int follow_pte(struct mm_struct *mm, unsigned long address, 57299fd6dad1SPaolo Bonzini pte_t **ptepp, spinlock_t **ptlp) 57309fd6dad1SPaolo Bonzini { 57310e5e64c0SMuchun Song pgd_t *pgd; 57320e5e64c0SMuchun Song p4d_t *p4d; 57330e5e64c0SMuchun Song pud_t *pud; 57340e5e64c0SMuchun Song pmd_t *pmd; 57350e5e64c0SMuchun Song pte_t *ptep; 57360e5e64c0SMuchun Song 57370e5e64c0SMuchun Song pgd = pgd_offset(mm, address); 57380e5e64c0SMuchun Song if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) 57390e5e64c0SMuchun Song goto out; 57400e5e64c0SMuchun Song 57410e5e64c0SMuchun Song p4d = p4d_offset(pgd, address); 57420e5e64c0SMuchun Song if (p4d_none(*p4d) || unlikely(p4d_bad(*p4d))) 57430e5e64c0SMuchun Song goto out; 57440e5e64c0SMuchun Song 57450e5e64c0SMuchun Song pud = pud_offset(p4d, address); 57460e5e64c0SMuchun Song if (pud_none(*pud) || unlikely(pud_bad(*pud))) 57470e5e64c0SMuchun Song goto out; 57480e5e64c0SMuchun Song 57490e5e64c0SMuchun Song pmd = pmd_offset(pud, address); 57500e5e64c0SMuchun Song VM_BUG_ON(pmd_trans_huge(*pmd)); 57510e5e64c0SMuchun Song 57520e5e64c0SMuchun Song ptep = pte_offset_map_lock(mm, pmd, address, ptlp); 57533db82b93SHugh Dickins if (!ptep) 57543db82b93SHugh Dickins goto out; 5755c33c7948SRyan Roberts if (!pte_present(ptep_get(ptep))) 57560e5e64c0SMuchun Song goto unlock; 57570e5e64c0SMuchun Song *ptepp = ptep; 57580e5e64c0SMuchun Song return 0; 57590e5e64c0SMuchun Song unlock: 57600e5e64c0SMuchun Song pte_unmap_unlock(ptep, *ptlp); 57610e5e64c0SMuchun Song out: 57620e5e64c0SMuchun Song return -EINVAL; 57639fd6dad1SPaolo Bonzini } 57649fd6dad1SPaolo Bonzini EXPORT_SYMBOL_GPL(follow_pte); 57659fd6dad1SPaolo Bonzini 57669fd6dad1SPaolo Bonzini /** 57673b6748e2SJohannes Weiner * follow_pfn - look up PFN at a user virtual address 57683b6748e2SJohannes Weiner * @vma: memory mapping 57693b6748e2SJohannes Weiner * @address: user virtual address 57703b6748e2SJohannes Weiner * @pfn: location to store found PFN 57713b6748e2SJohannes Weiner * 57723b6748e2SJohannes Weiner * Only IO mappings and raw PFN mappings are allowed. 57733b6748e2SJohannes Weiner * 57749fd6dad1SPaolo Bonzini * This function does not allow the caller to read the permissions 57759fd6dad1SPaolo Bonzini * of the PTE. Do not use it. 57769fd6dad1SPaolo Bonzini * 5777a862f68aSMike Rapoport * Return: zero and the pfn at @pfn on success, -ve otherwise. 57783b6748e2SJohannes Weiner */ 57793b6748e2SJohannes Weiner int follow_pfn(struct vm_area_struct *vma, unsigned long address, 57803b6748e2SJohannes Weiner unsigned long *pfn) 57813b6748e2SJohannes Weiner { 57823b6748e2SJohannes Weiner int ret = -EINVAL; 57833b6748e2SJohannes Weiner spinlock_t *ptl; 57843b6748e2SJohannes Weiner pte_t *ptep; 57853b6748e2SJohannes Weiner 57863b6748e2SJohannes Weiner if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 57873b6748e2SJohannes Weiner return ret; 57883b6748e2SJohannes Weiner 57899fd6dad1SPaolo Bonzini ret = follow_pte(vma->vm_mm, address, &ptep, &ptl); 57903b6748e2SJohannes Weiner if (ret) 57913b6748e2SJohannes Weiner return ret; 5792c33c7948SRyan Roberts *pfn = pte_pfn(ptep_get(ptep)); 57933b6748e2SJohannes Weiner pte_unmap_unlock(ptep, ptl); 57943b6748e2SJohannes Weiner return 0; 57953b6748e2SJohannes Weiner } 57963b6748e2SJohannes Weiner EXPORT_SYMBOL(follow_pfn); 57973b6748e2SJohannes Weiner 579828b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT 5799d87fe660Svenkatesh.pallipadi@intel.com int follow_phys(struct vm_area_struct *vma, 580028b2ee20SRik van Riel unsigned long address, unsigned int flags, 5801d87fe660Svenkatesh.pallipadi@intel.com unsigned long *prot, resource_size_t *phys) 580228b2ee20SRik van Riel { 580303668a4dSJohannes Weiner int ret = -EINVAL; 580428b2ee20SRik van Riel pte_t *ptep, pte; 580528b2ee20SRik van Riel spinlock_t *ptl; 580628b2ee20SRik van Riel 5807d87fe660Svenkatesh.pallipadi@intel.com if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 5808d87fe660Svenkatesh.pallipadi@intel.com goto out; 580928b2ee20SRik van Riel 58109fd6dad1SPaolo Bonzini if (follow_pte(vma->vm_mm, address, &ptep, &ptl)) 5811d87fe660Svenkatesh.pallipadi@intel.com goto out; 5812c33c7948SRyan Roberts pte = ptep_get(ptep); 581303668a4dSJohannes Weiner 5814f6f37321SLinus Torvalds if ((flags & FOLL_WRITE) && !pte_write(pte)) 581528b2ee20SRik van Riel goto unlock; 581628b2ee20SRik van Riel 581728b2ee20SRik van Riel *prot = pgprot_val(pte_pgprot(pte)); 581803668a4dSJohannes Weiner *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT; 581928b2ee20SRik van Riel 582003668a4dSJohannes Weiner ret = 0; 582128b2ee20SRik van Riel unlock: 582228b2ee20SRik van Riel pte_unmap_unlock(ptep, ptl); 582328b2ee20SRik van Riel out: 5824d87fe660Svenkatesh.pallipadi@intel.com return ret; 582528b2ee20SRik van Riel } 582628b2ee20SRik van Riel 582796667f8aSDaniel Vetter /** 582896667f8aSDaniel Vetter * generic_access_phys - generic implementation for iomem mmap access 582996667f8aSDaniel Vetter * @vma: the vma to access 5830f0953a1bSIngo Molnar * @addr: userspace address, not relative offset within @vma 583196667f8aSDaniel Vetter * @buf: buffer to read/write 583296667f8aSDaniel Vetter * @len: length of transfer 583396667f8aSDaniel Vetter * @write: set to FOLL_WRITE when writing, otherwise reading 583496667f8aSDaniel Vetter * 583596667f8aSDaniel Vetter * This is a generic implementation for &vm_operations_struct.access for an 583696667f8aSDaniel Vetter * iomem mapping. This callback is used by access_process_vm() when the @vma is 583796667f8aSDaniel Vetter * not page based. 583896667f8aSDaniel Vetter */ 583928b2ee20SRik van Riel int generic_access_phys(struct vm_area_struct *vma, unsigned long addr, 584028b2ee20SRik van Riel void *buf, int len, int write) 584128b2ee20SRik van Riel { 584228b2ee20SRik van Riel resource_size_t phys_addr; 584328b2ee20SRik van Riel unsigned long prot = 0; 58442bc7273bSKOSAKI Motohiro void __iomem *maddr; 584596667f8aSDaniel Vetter pte_t *ptep, pte; 584696667f8aSDaniel Vetter spinlock_t *ptl; 584796667f8aSDaniel Vetter int offset = offset_in_page(addr); 584896667f8aSDaniel Vetter int ret = -EINVAL; 584928b2ee20SRik van Riel 585096667f8aSDaniel Vetter if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 585196667f8aSDaniel Vetter return -EINVAL; 585296667f8aSDaniel Vetter 585396667f8aSDaniel Vetter retry: 5854e913a8cdSLinus Torvalds if (follow_pte(vma->vm_mm, addr, &ptep, &ptl)) 585596667f8aSDaniel Vetter return -EINVAL; 5856c33c7948SRyan Roberts pte = ptep_get(ptep); 585796667f8aSDaniel Vetter pte_unmap_unlock(ptep, ptl); 585896667f8aSDaniel Vetter 585996667f8aSDaniel Vetter prot = pgprot_val(pte_pgprot(pte)); 586096667f8aSDaniel Vetter phys_addr = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT; 586196667f8aSDaniel Vetter 586296667f8aSDaniel Vetter if ((write & FOLL_WRITE) && !pte_write(pte)) 586328b2ee20SRik van Riel return -EINVAL; 586428b2ee20SRik van Riel 58659cb12d7bSGrazvydas Ignotas maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot); 586624eee1e4Sjie@chenjie6@huwei.com if (!maddr) 586724eee1e4Sjie@chenjie6@huwei.com return -ENOMEM; 586824eee1e4Sjie@chenjie6@huwei.com 5869e913a8cdSLinus Torvalds if (follow_pte(vma->vm_mm, addr, &ptep, &ptl)) 587096667f8aSDaniel Vetter goto out_unmap; 587196667f8aSDaniel Vetter 5872c33c7948SRyan Roberts if (!pte_same(pte, ptep_get(ptep))) { 587396667f8aSDaniel Vetter pte_unmap_unlock(ptep, ptl); 587496667f8aSDaniel Vetter iounmap(maddr); 587596667f8aSDaniel Vetter 587696667f8aSDaniel Vetter goto retry; 587796667f8aSDaniel Vetter } 587896667f8aSDaniel Vetter 587928b2ee20SRik van Riel if (write) 588028b2ee20SRik van Riel memcpy_toio(maddr + offset, buf, len); 588128b2ee20SRik van Riel else 588228b2ee20SRik van Riel memcpy_fromio(buf, maddr + offset, len); 588396667f8aSDaniel Vetter ret = len; 588496667f8aSDaniel Vetter pte_unmap_unlock(ptep, ptl); 588596667f8aSDaniel Vetter out_unmap: 588628b2ee20SRik van Riel iounmap(maddr); 588728b2ee20SRik van Riel 588896667f8aSDaniel Vetter return ret; 588928b2ee20SRik van Riel } 58905a73633eSUwe Kleine-König EXPORT_SYMBOL_GPL(generic_access_phys); 589128b2ee20SRik van Riel #endif 589228b2ee20SRik van Riel 58930ec76a11SDavid Howells /* 5894d3f5ffcaSJohn Hubbard * Access another process' address space as given in mm. 58950ec76a11SDavid Howells */ 5896c43cfa42SLorenzo Stoakes static int __access_remote_vm(struct mm_struct *mm, unsigned long addr, 5897c43cfa42SLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 58980ec76a11SDavid Howells { 58990ec76a11SDavid Howells void *old_buf = buf; 5900442486ecSLorenzo Stoakes int write = gup_flags & FOLL_WRITE; 59010ec76a11SDavid Howells 5902d8ed45c5SMichel Lespinasse if (mmap_read_lock_killable(mm)) 59031e426fe2SKonstantin Khlebnikov return 0; 59041e426fe2SKonstantin Khlebnikov 590522883973SKirill A. Shutemov /* Untag the address before looking up the VMA */ 590622883973SKirill A. Shutemov addr = untagged_addr_remote(mm, addr); 590722883973SKirill A. Shutemov 5908eee9c708SLinus Torvalds /* Avoid triggering the temporary warning in __get_user_pages */ 5909eee9c708SLinus Torvalds if (!vma_lookup(mm, addr) && !expand_stack(mm, addr)) 5910eee9c708SLinus Torvalds return 0; 5911eee9c708SLinus Torvalds 5912183ff22bSSimon Arlott /* ignore errors, just check how much was successfully transferred */ 59130ec76a11SDavid Howells while (len) { 5914ca5e8632SLorenzo Stoakes int bytes, offset; 59150ec76a11SDavid Howells void *maddr; 5916ca5e8632SLorenzo Stoakes struct vm_area_struct *vma = NULL; 5917ca5e8632SLorenzo Stoakes struct page *page = get_user_page_vma_remote(mm, addr, 5918ca5e8632SLorenzo Stoakes gup_flags, &vma); 59190ec76a11SDavid Howells 59206a1960b8SLorenzo Stoakes if (IS_ERR(page)) { 59218d7071afSLinus Torvalds /* We might need to expand the stack to access it */ 59228d7071afSLinus Torvalds vma = vma_lookup(mm, addr); 59238d7071afSLinus Torvalds if (!vma) { 59248d7071afSLinus Torvalds vma = expand_stack(mm, addr); 59259471f1f2SLinus Torvalds 59269471f1f2SLinus Torvalds /* mmap_lock was dropped on failure */ 59278d7071afSLinus Torvalds if (!vma) 59289471f1f2SLinus Torvalds return buf - old_buf; 59299471f1f2SLinus Torvalds 59309471f1f2SLinus Torvalds /* Try again if stack expansion worked */ 59319471f1f2SLinus Torvalds continue; 59328d7071afSLinus Torvalds } 59338d7071afSLinus Torvalds 593428b2ee20SRik van Riel /* 593528b2ee20SRik van Riel * Check if this is a VM_IO | VM_PFNMAP VMA, which 593628b2ee20SRik van Riel * we can access using slightly different code. 593728b2ee20SRik van Riel */ 59389471f1f2SLinus Torvalds bytes = 0; 59399471f1f2SLinus Torvalds #ifdef CONFIG_HAVE_IOREMAP_PROT 594028b2ee20SRik van Riel if (vma->vm_ops && vma->vm_ops->access) 59419471f1f2SLinus Torvalds bytes = vma->vm_ops->access(vma, addr, buf, 594228b2ee20SRik van Riel len, write); 5943dbffcd03SRik van Riel #endif 59449471f1f2SLinus Torvalds if (bytes <= 0) 59459471f1f2SLinus Torvalds break; 594628b2ee20SRik van Riel } else { 59470ec76a11SDavid Howells bytes = len; 59480ec76a11SDavid Howells offset = addr & (PAGE_SIZE-1); 59490ec76a11SDavid Howells if (bytes > PAGE_SIZE-offset) 59500ec76a11SDavid Howells bytes = PAGE_SIZE-offset; 59510ec76a11SDavid Howells 5952f7ef5fe7SFabio M. De Francesco maddr = kmap_local_page(page); 59530ec76a11SDavid Howells if (write) { 59540ec76a11SDavid Howells copy_to_user_page(vma, page, addr, 59550ec76a11SDavid Howells maddr + offset, buf, bytes); 59560ec76a11SDavid Howells set_page_dirty_lock(page); 59570ec76a11SDavid Howells } else { 59580ec76a11SDavid Howells copy_from_user_page(vma, page, addr, 59590ec76a11SDavid Howells buf, maddr + offset, bytes); 59600ec76a11SDavid Howells } 5961f7ef5fe7SFabio M. De Francesco unmap_and_put_page(page, maddr); 596228b2ee20SRik van Riel } 59630ec76a11SDavid Howells len -= bytes; 59640ec76a11SDavid Howells buf += bytes; 59650ec76a11SDavid Howells addr += bytes; 59660ec76a11SDavid Howells } 5967d8ed45c5SMichel Lespinasse mmap_read_unlock(mm); 59680ec76a11SDavid Howells 59690ec76a11SDavid Howells return buf - old_buf; 59700ec76a11SDavid Howells } 597103252919SAndi Kleen 59725ddd36b9SStephen Wilson /** 5973ae91dbfcSRandy Dunlap * access_remote_vm - access another process' address space 59745ddd36b9SStephen Wilson * @mm: the mm_struct of the target address space 59755ddd36b9SStephen Wilson * @addr: start address to access 59765ddd36b9SStephen Wilson * @buf: source or destination buffer 59775ddd36b9SStephen Wilson * @len: number of bytes to transfer 59786347e8d5SLorenzo Stoakes * @gup_flags: flags modifying lookup behaviour 59795ddd36b9SStephen Wilson * 59805ddd36b9SStephen Wilson * The caller must hold a reference on @mm. 5981a862f68aSMike Rapoport * 5982a862f68aSMike Rapoport * Return: number of bytes copied from source to destination. 59835ddd36b9SStephen Wilson */ 59845ddd36b9SStephen Wilson int access_remote_vm(struct mm_struct *mm, unsigned long addr, 59856347e8d5SLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 59865ddd36b9SStephen Wilson { 5987d3f5ffcaSJohn Hubbard return __access_remote_vm(mm, addr, buf, len, gup_flags); 59885ddd36b9SStephen Wilson } 59895ddd36b9SStephen Wilson 599003252919SAndi Kleen /* 5991206cb636SStephen Wilson * Access another process' address space. 5992206cb636SStephen Wilson * Source/target buffer must be kernel space, 5993206cb636SStephen Wilson * Do not walk the page table directly, use get_user_pages 5994206cb636SStephen Wilson */ 5995206cb636SStephen Wilson int access_process_vm(struct task_struct *tsk, unsigned long addr, 5996f307ab6dSLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 5997206cb636SStephen Wilson { 5998206cb636SStephen Wilson struct mm_struct *mm; 5999206cb636SStephen Wilson int ret; 6000206cb636SStephen Wilson 6001206cb636SStephen Wilson mm = get_task_mm(tsk); 6002206cb636SStephen Wilson if (!mm) 6003206cb636SStephen Wilson return 0; 6004206cb636SStephen Wilson 6005d3f5ffcaSJohn Hubbard ret = __access_remote_vm(mm, addr, buf, len, gup_flags); 6006442486ecSLorenzo Stoakes 6007206cb636SStephen Wilson mmput(mm); 6008206cb636SStephen Wilson 6009206cb636SStephen Wilson return ret; 6010206cb636SStephen Wilson } 6011fcd35857SCatalin Marinas EXPORT_SYMBOL_GPL(access_process_vm); 6012206cb636SStephen Wilson 601303252919SAndi Kleen /* 601403252919SAndi Kleen * Print the name of a VMA. 601503252919SAndi Kleen */ 601603252919SAndi Kleen void print_vma_addr(char *prefix, unsigned long ip) 601703252919SAndi Kleen { 601803252919SAndi Kleen struct mm_struct *mm = current->mm; 601903252919SAndi Kleen struct vm_area_struct *vma; 602003252919SAndi Kleen 6021e8bff74aSIngo Molnar /* 60220a7f682dSMichal Hocko * we might be running from an atomic context so we cannot sleep 6023e8bff74aSIngo Molnar */ 6024d8ed45c5SMichel Lespinasse if (!mmap_read_trylock(mm)) 6025e8bff74aSIngo Molnar return; 6026e8bff74aSIngo Molnar 602703252919SAndi Kleen vma = find_vma(mm, ip); 602803252919SAndi Kleen if (vma && vma->vm_file) { 602903252919SAndi Kleen struct file *f = vma->vm_file; 60300a7f682dSMichal Hocko char *buf = (char *)__get_free_page(GFP_NOWAIT); 603103252919SAndi Kleen if (buf) { 60322fbc57c5SAndy Shevchenko char *p; 603303252919SAndi Kleen 60349bf39ab2SMiklos Szeredi p = file_path(f, buf, PAGE_SIZE); 603503252919SAndi Kleen if (IS_ERR(p)) 603603252919SAndi Kleen p = "?"; 60372fbc57c5SAndy Shevchenko printk("%s%s[%lx+%lx]", prefix, kbasename(p), 603803252919SAndi Kleen vma->vm_start, 603903252919SAndi Kleen vma->vm_end - vma->vm_start); 604003252919SAndi Kleen free_page((unsigned long)buf); 604103252919SAndi Kleen } 604203252919SAndi Kleen } 6043d8ed45c5SMichel Lespinasse mmap_read_unlock(mm); 604403252919SAndi Kleen } 60453ee1afa3SNick Piggin 6046662bbcb2SMichael S. Tsirkin #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP) 60479ec23531SDavid Hildenbrand void __might_fault(const char *file, int line) 60483ee1afa3SNick Piggin { 60499ec23531SDavid Hildenbrand if (pagefault_disabled()) 6050662bbcb2SMichael S. Tsirkin return; 605142a38756SThomas Gleixner __might_sleep(file, line); 60529ec23531SDavid Hildenbrand #if defined(CONFIG_DEBUG_ATOMIC_SLEEP) 6053662bbcb2SMichael S. Tsirkin if (current->mm) 6054da1c55f1SMichel Lespinasse might_lock_read(¤t->mm->mmap_lock); 60559ec23531SDavid Hildenbrand #endif 60563ee1afa3SNick Piggin } 60579ec23531SDavid Hildenbrand EXPORT_SYMBOL(__might_fault); 60583ee1afa3SNick Piggin #endif 605947ad8475SAndrea Arcangeli 606047ad8475SAndrea Arcangeli #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS) 6061c6ddfb6cSHuang Ying /* 6062c6ddfb6cSHuang Ying * Process all subpages of the specified huge page with the specified 6063c6ddfb6cSHuang Ying * operation. The target subpage will be processed last to keep its 6064c6ddfb6cSHuang Ying * cache lines hot. 6065c6ddfb6cSHuang Ying */ 60661cb9dc4bSLiu Shixin static inline int process_huge_page( 6067c6ddfb6cSHuang Ying unsigned long addr_hint, unsigned int pages_per_huge_page, 60681cb9dc4bSLiu Shixin int (*process_subpage)(unsigned long addr, int idx, void *arg), 6069c6ddfb6cSHuang Ying void *arg) 6070c6ddfb6cSHuang Ying { 60711cb9dc4bSLiu Shixin int i, n, base, l, ret; 6072c6ddfb6cSHuang Ying unsigned long addr = addr_hint & 6073c6ddfb6cSHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 6074c6ddfb6cSHuang Ying 6075c6ddfb6cSHuang Ying /* Process target subpage last to keep its cache lines hot */ 6076c6ddfb6cSHuang Ying might_sleep(); 6077c6ddfb6cSHuang Ying n = (addr_hint - addr) / PAGE_SIZE; 6078c6ddfb6cSHuang Ying if (2 * n <= pages_per_huge_page) { 6079c6ddfb6cSHuang Ying /* If target subpage in first half of huge page */ 6080c6ddfb6cSHuang Ying base = 0; 6081c6ddfb6cSHuang Ying l = n; 6082c6ddfb6cSHuang Ying /* Process subpages at the end of huge page */ 6083c6ddfb6cSHuang Ying for (i = pages_per_huge_page - 1; i >= 2 * n; i--) { 6084c6ddfb6cSHuang Ying cond_resched(); 60851cb9dc4bSLiu Shixin ret = process_subpage(addr + i * PAGE_SIZE, i, arg); 60861cb9dc4bSLiu Shixin if (ret) 60871cb9dc4bSLiu Shixin return ret; 6088c6ddfb6cSHuang Ying } 6089c6ddfb6cSHuang Ying } else { 6090c6ddfb6cSHuang Ying /* If target subpage in second half of huge page */ 6091c6ddfb6cSHuang Ying base = pages_per_huge_page - 2 * (pages_per_huge_page - n); 6092c6ddfb6cSHuang Ying l = pages_per_huge_page - n; 6093c6ddfb6cSHuang Ying /* Process subpages at the begin of huge page */ 6094c6ddfb6cSHuang Ying for (i = 0; i < base; i++) { 6095c6ddfb6cSHuang Ying cond_resched(); 60961cb9dc4bSLiu Shixin ret = process_subpage(addr + i * PAGE_SIZE, i, arg); 60971cb9dc4bSLiu Shixin if (ret) 60981cb9dc4bSLiu Shixin return ret; 6099c6ddfb6cSHuang Ying } 6100c6ddfb6cSHuang Ying } 6101c6ddfb6cSHuang Ying /* 6102c6ddfb6cSHuang Ying * Process remaining subpages in left-right-left-right pattern 6103c6ddfb6cSHuang Ying * towards the target subpage 6104c6ddfb6cSHuang Ying */ 6105c6ddfb6cSHuang Ying for (i = 0; i < l; i++) { 6106c6ddfb6cSHuang Ying int left_idx = base + i; 6107c6ddfb6cSHuang Ying int right_idx = base + 2 * l - 1 - i; 6108c6ddfb6cSHuang Ying 6109c6ddfb6cSHuang Ying cond_resched(); 61101cb9dc4bSLiu Shixin ret = process_subpage(addr + left_idx * PAGE_SIZE, left_idx, arg); 61111cb9dc4bSLiu Shixin if (ret) 61121cb9dc4bSLiu Shixin return ret; 6113c6ddfb6cSHuang Ying cond_resched(); 61141cb9dc4bSLiu Shixin ret = process_subpage(addr + right_idx * PAGE_SIZE, right_idx, arg); 61151cb9dc4bSLiu Shixin if (ret) 61161cb9dc4bSLiu Shixin return ret; 6117c6ddfb6cSHuang Ying } 61181cb9dc4bSLiu Shixin return 0; 6119c6ddfb6cSHuang Ying } 6120c6ddfb6cSHuang Ying 612147ad8475SAndrea Arcangeli static void clear_gigantic_page(struct page *page, 612247ad8475SAndrea Arcangeli unsigned long addr, 612347ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 612447ad8475SAndrea Arcangeli { 612547ad8475SAndrea Arcangeli int i; 612614455eabSCheng Li struct page *p; 612747ad8475SAndrea Arcangeli 612847ad8475SAndrea Arcangeli might_sleep(); 612914455eabSCheng Li for (i = 0; i < pages_per_huge_page; i++) { 613014455eabSCheng Li p = nth_page(page, i); 613147ad8475SAndrea Arcangeli cond_resched(); 613247ad8475SAndrea Arcangeli clear_user_highpage(p, addr + i * PAGE_SIZE); 613347ad8475SAndrea Arcangeli } 613447ad8475SAndrea Arcangeli } 6135c6ddfb6cSHuang Ying 61361cb9dc4bSLiu Shixin static int clear_subpage(unsigned long addr, int idx, void *arg) 6137c6ddfb6cSHuang Ying { 6138c6ddfb6cSHuang Ying struct page *page = arg; 6139c6ddfb6cSHuang Ying 6140c6ddfb6cSHuang Ying clear_user_highpage(page + idx, addr); 61411cb9dc4bSLiu Shixin return 0; 6142c6ddfb6cSHuang Ying } 6143c6ddfb6cSHuang Ying 614447ad8475SAndrea Arcangeli void clear_huge_page(struct page *page, 6145c79b57e4SHuang Ying unsigned long addr_hint, unsigned int pages_per_huge_page) 614647ad8475SAndrea Arcangeli { 6147c79b57e4SHuang Ying unsigned long addr = addr_hint & 6148c79b57e4SHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 614947ad8475SAndrea Arcangeli 615047ad8475SAndrea Arcangeli if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { 615147ad8475SAndrea Arcangeli clear_gigantic_page(page, addr, pages_per_huge_page); 615247ad8475SAndrea Arcangeli return; 615347ad8475SAndrea Arcangeli } 615447ad8475SAndrea Arcangeli 6155c6ddfb6cSHuang Ying process_huge_page(addr_hint, pages_per_huge_page, clear_subpage, page); 615647ad8475SAndrea Arcangeli } 615747ad8475SAndrea Arcangeli 61581cb9dc4bSLiu Shixin static int copy_user_gigantic_page(struct folio *dst, struct folio *src, 615947ad8475SAndrea Arcangeli unsigned long addr, 616047ad8475SAndrea Arcangeli struct vm_area_struct *vma, 616147ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 616247ad8475SAndrea Arcangeli { 616347ad8475SAndrea Arcangeli int i; 6164c0e8150eSZhangPeng struct page *dst_page; 6165c0e8150eSZhangPeng struct page *src_page; 616647ad8475SAndrea Arcangeli 616714455eabSCheng Li for (i = 0; i < pages_per_huge_page; i++) { 6168c0e8150eSZhangPeng dst_page = folio_page(dst, i); 6169c0e8150eSZhangPeng src_page = folio_page(src, i); 617014455eabSCheng Li 617147ad8475SAndrea Arcangeli cond_resched(); 61721cb9dc4bSLiu Shixin if (copy_mc_user_highpage(dst_page, src_page, 61731cb9dc4bSLiu Shixin addr + i*PAGE_SIZE, vma)) { 61741cb9dc4bSLiu Shixin memory_failure_queue(page_to_pfn(src_page), 0); 61751cb9dc4bSLiu Shixin return -EHWPOISON; 617647ad8475SAndrea Arcangeli } 617747ad8475SAndrea Arcangeli } 61781cb9dc4bSLiu Shixin return 0; 61791cb9dc4bSLiu Shixin } 618047ad8475SAndrea Arcangeli 6181c9f4cd71SHuang Ying struct copy_subpage_arg { 6182c9f4cd71SHuang Ying struct page *dst; 6183c9f4cd71SHuang Ying struct page *src; 6184c9f4cd71SHuang Ying struct vm_area_struct *vma; 6185c9f4cd71SHuang Ying }; 6186c9f4cd71SHuang Ying 61871cb9dc4bSLiu Shixin static int copy_subpage(unsigned long addr, int idx, void *arg) 6188c9f4cd71SHuang Ying { 6189c9f4cd71SHuang Ying struct copy_subpage_arg *copy_arg = arg; 6190c9f4cd71SHuang Ying 61911cb9dc4bSLiu Shixin if (copy_mc_user_highpage(copy_arg->dst + idx, copy_arg->src + idx, 61921cb9dc4bSLiu Shixin addr, copy_arg->vma)) { 61931cb9dc4bSLiu Shixin memory_failure_queue(page_to_pfn(copy_arg->src + idx), 0); 61941cb9dc4bSLiu Shixin return -EHWPOISON; 61951cb9dc4bSLiu Shixin } 61961cb9dc4bSLiu Shixin return 0; 6197c9f4cd71SHuang Ying } 6198c9f4cd71SHuang Ying 61991cb9dc4bSLiu Shixin int copy_user_large_folio(struct folio *dst, struct folio *src, 6200c0e8150eSZhangPeng unsigned long addr_hint, struct vm_area_struct *vma) 620147ad8475SAndrea Arcangeli { 6202c0e8150eSZhangPeng unsigned int pages_per_huge_page = folio_nr_pages(dst); 6203c9f4cd71SHuang Ying unsigned long addr = addr_hint & 6204c9f4cd71SHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 6205c9f4cd71SHuang Ying struct copy_subpage_arg arg = { 6206c0e8150eSZhangPeng .dst = &dst->page, 6207c0e8150eSZhangPeng .src = &src->page, 6208c9f4cd71SHuang Ying .vma = vma, 6209c9f4cd71SHuang Ying }; 621047ad8475SAndrea Arcangeli 62111cb9dc4bSLiu Shixin if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) 62121cb9dc4bSLiu Shixin return copy_user_gigantic_page(dst, src, addr, vma, 621347ad8475SAndrea Arcangeli pages_per_huge_page); 621447ad8475SAndrea Arcangeli 62151cb9dc4bSLiu Shixin return process_huge_page(addr_hint, pages_per_huge_page, copy_subpage, &arg); 621647ad8475SAndrea Arcangeli } 6217fa4d75c1SMike Kravetz 6218e87340caSZhangPeng long copy_folio_from_user(struct folio *dst_folio, 6219fa4d75c1SMike Kravetz const void __user *usr_src, 6220810a56b9SMike Kravetz bool allow_pagefault) 6221fa4d75c1SMike Kravetz { 6222e87340caSZhangPeng void *kaddr; 6223fa4d75c1SMike Kravetz unsigned long i, rc = 0; 6224e87340caSZhangPeng unsigned int nr_pages = folio_nr_pages(dst_folio); 6225e87340caSZhangPeng unsigned long ret_val = nr_pages * PAGE_SIZE; 622614455eabSCheng Li struct page *subpage; 6227fa4d75c1SMike Kravetz 6228e87340caSZhangPeng for (i = 0; i < nr_pages; i++) { 6229e87340caSZhangPeng subpage = folio_page(dst_folio, i); 6230e87340caSZhangPeng kaddr = kmap_local_page(subpage); 62310d508c1fSZhangPeng if (!allow_pagefault) 62320d508c1fSZhangPeng pagefault_disable(); 6233e87340caSZhangPeng rc = copy_from_user(kaddr, usr_src + i * PAGE_SIZE, PAGE_SIZE); 62340d508c1fSZhangPeng if (!allow_pagefault) 62350d508c1fSZhangPeng pagefault_enable(); 6236e87340caSZhangPeng kunmap_local(kaddr); 6237fa4d75c1SMike Kravetz 6238fa4d75c1SMike Kravetz ret_val -= (PAGE_SIZE - rc); 6239fa4d75c1SMike Kravetz if (rc) 6240fa4d75c1SMike Kravetz break; 6241fa4d75c1SMike Kravetz 6242e763243cSMuchun Song flush_dcache_page(subpage); 6243e763243cSMuchun Song 6244fa4d75c1SMike Kravetz cond_resched(); 6245fa4d75c1SMike Kravetz } 6246fa4d75c1SMike Kravetz return ret_val; 6247fa4d75c1SMike Kravetz } 624847ad8475SAndrea Arcangeli #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */ 624949076ec2SKirill A. Shutemov 625040b64acdSOlof Johansson #if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS 6251b35f1819SKirill A. Shutemov 6252b35f1819SKirill A. Shutemov static struct kmem_cache *page_ptl_cachep; 6253b35f1819SKirill A. Shutemov 6254b35f1819SKirill A. Shutemov void __init ptlock_cache_init(void) 6255b35f1819SKirill A. Shutemov { 6256b35f1819SKirill A. Shutemov page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0, 6257b35f1819SKirill A. Shutemov SLAB_PANIC, NULL); 6258b35f1819SKirill A. Shutemov } 6259b35f1819SKirill A. Shutemov 6260f5ecca06SVishal Moola (Oracle) bool ptlock_alloc(struct ptdesc *ptdesc) 626149076ec2SKirill A. Shutemov { 626249076ec2SKirill A. Shutemov spinlock_t *ptl; 626349076ec2SKirill A. Shutemov 6264b35f1819SKirill A. Shutemov ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL); 626549076ec2SKirill A. Shutemov if (!ptl) 626649076ec2SKirill A. Shutemov return false; 6267f5ecca06SVishal Moola (Oracle) ptdesc->ptl = ptl; 626849076ec2SKirill A. Shutemov return true; 626949076ec2SKirill A. Shutemov } 627049076ec2SKirill A. Shutemov 62716ed1b8a0SVishal Moola (Oracle) void ptlock_free(struct ptdesc *ptdesc) 627249076ec2SKirill A. Shutemov { 62736ed1b8a0SVishal Moola (Oracle) kmem_cache_free(page_ptl_cachep, ptdesc->ptl); 627449076ec2SKirill A. Shutemov } 627549076ec2SKirill A. Shutemov #endif 6276