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 126e99fb98dSKefeng Wang * of ZONE_NORMAL. 1271da177e4SLinus Torvalds */ 1281da177e4SLinus Torvalds void *high_memory; 1291da177e4SLinus Torvalds EXPORT_SYMBOL(high_memory); 1301da177e4SLinus Torvalds 13132a93233SIngo Molnar /* 13232a93233SIngo Molnar * Randomize the address space (stacks, mmaps, brk, etc.). 13332a93233SIngo Molnar * 13432a93233SIngo Molnar * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization, 13532a93233SIngo Molnar * as ancient (libc5 based) binaries can segfault. ) 13632a93233SIngo Molnar */ 13732a93233SIngo Molnar int randomize_va_space __read_mostly = 13832a93233SIngo Molnar #ifdef CONFIG_COMPAT_BRK 13932a93233SIngo Molnar 1; 14032a93233SIngo Molnar #else 14132a93233SIngo Molnar 2; 14232a93233SIngo Molnar #endif 143a62eaf15SAndi Kleen 14446bdb427SWill Deacon #ifndef arch_wants_old_prefaulted_pte 14546bdb427SWill Deacon static inline bool arch_wants_old_prefaulted_pte(void) 14646bdb427SWill Deacon { 14746bdb427SWill Deacon /* 14846bdb427SWill Deacon * Transitioning a PTE from 'old' to 'young' can be expensive on 14946bdb427SWill Deacon * some architectures, even if it's performed in hardware. By 15046bdb427SWill Deacon * default, "false" means prefaulted entries will be 'young'. 15146bdb427SWill Deacon */ 15246bdb427SWill Deacon return false; 15346bdb427SWill Deacon } 15446bdb427SWill Deacon #endif 15546bdb427SWill Deacon 156a62eaf15SAndi Kleen static int __init disable_randmaps(char *s) 157a62eaf15SAndi Kleen { 158a62eaf15SAndi Kleen randomize_va_space = 0; 1599b41046cSOGAWA Hirofumi return 1; 160a62eaf15SAndi Kleen } 161a62eaf15SAndi Kleen __setup("norandmaps", disable_randmaps); 162a62eaf15SAndi Kleen 16362eede62SHugh Dickins unsigned long zero_pfn __read_mostly; 1640b70068eSArd Biesheuvel EXPORT_SYMBOL(zero_pfn); 1650b70068eSArd Biesheuvel 166166f61b9STobin C Harding unsigned long highest_memmap_pfn __read_mostly; 167166f61b9STobin C Harding 168a13ea5b7SHugh Dickins /* 169a13ea5b7SHugh Dickins * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init() 170a13ea5b7SHugh Dickins */ 171a13ea5b7SHugh Dickins static int __init init_zero_pfn(void) 172a13ea5b7SHugh Dickins { 173a13ea5b7SHugh Dickins zero_pfn = page_to_pfn(ZERO_PAGE(0)); 174a13ea5b7SHugh Dickins return 0; 175a13ea5b7SHugh Dickins } 176e720e7d0SIlya Lipnitskiy early_initcall(init_zero_pfn); 177a62eaf15SAndi Kleen 178f1a79412SShakeel Butt void mm_trace_rss_stat(struct mm_struct *mm, int member) 179b3d1411bSJoel Fernandes (Google) { 180f1a79412SShakeel Butt trace_rss_stat(mm, member); 181b3d1411bSJoel Fernandes (Google) } 182d559db08SKAMEZAWA Hiroyuki 1831da177e4SLinus Torvalds /* 1841da177e4SLinus Torvalds * Note: this doesn't free the actual pages themselves. That 1851da177e4SLinus Torvalds * has been handled earlier when unmapping all the memory regions. 1861da177e4SLinus Torvalds */ 1879e1b32caSBenjamin Herrenschmidt static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd, 1889e1b32caSBenjamin Herrenschmidt unsigned long addr) 1891da177e4SLinus Torvalds { 1902f569afdSMartin Schwidefsky pgtable_t token = pmd_pgtable(*pmd); 1911da177e4SLinus Torvalds pmd_clear(pmd); 1929e1b32caSBenjamin Herrenschmidt pte_free_tlb(tlb, token, addr); 193c4812909SKirill A. Shutemov mm_dec_nr_ptes(tlb->mm); 1941da177e4SLinus Torvalds } 1951da177e4SLinus Torvalds 196e0da382cSHugh Dickins static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud, 197e0da382cSHugh Dickins unsigned long addr, unsigned long end, 198e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 1991da177e4SLinus Torvalds { 2001da177e4SLinus Torvalds pmd_t *pmd; 2011da177e4SLinus Torvalds unsigned long next; 202e0da382cSHugh Dickins unsigned long start; 2031da177e4SLinus Torvalds 204e0da382cSHugh Dickins start = addr; 2051da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 2061da177e4SLinus Torvalds do { 2071da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 2081da177e4SLinus Torvalds if (pmd_none_or_clear_bad(pmd)) 2091da177e4SLinus Torvalds continue; 2109e1b32caSBenjamin Herrenschmidt free_pte_range(tlb, pmd, addr); 2111da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 2121da177e4SLinus Torvalds 213e0da382cSHugh Dickins start &= PUD_MASK; 214e0da382cSHugh Dickins if (start < floor) 215e0da382cSHugh Dickins return; 216e0da382cSHugh Dickins if (ceiling) { 217e0da382cSHugh Dickins ceiling &= PUD_MASK; 218e0da382cSHugh Dickins if (!ceiling) 219e0da382cSHugh Dickins return; 2201da177e4SLinus Torvalds } 221e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 222e0da382cSHugh Dickins return; 223e0da382cSHugh Dickins 224e0da382cSHugh Dickins pmd = pmd_offset(pud, start); 225e0da382cSHugh Dickins pud_clear(pud); 2269e1b32caSBenjamin Herrenschmidt pmd_free_tlb(tlb, pmd, start); 227dc6c9a35SKirill A. Shutemov mm_dec_nr_pmds(tlb->mm); 2281da177e4SLinus Torvalds } 2291da177e4SLinus Torvalds 230c2febafcSKirill A. Shutemov static inline void free_pud_range(struct mmu_gather *tlb, p4d_t *p4d, 231e0da382cSHugh Dickins unsigned long addr, unsigned long end, 232e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 2331da177e4SLinus Torvalds { 2341da177e4SLinus Torvalds pud_t *pud; 2351da177e4SLinus Torvalds unsigned long next; 236e0da382cSHugh Dickins unsigned long start; 2371da177e4SLinus Torvalds 238e0da382cSHugh Dickins start = addr; 239c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 2401da177e4SLinus Torvalds do { 2411da177e4SLinus Torvalds next = pud_addr_end(addr, end); 2421da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 2431da177e4SLinus Torvalds continue; 244e0da382cSHugh Dickins free_pmd_range(tlb, pud, addr, next, floor, ceiling); 2451da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 2461da177e4SLinus Torvalds 247c2febafcSKirill A. Shutemov start &= P4D_MASK; 248c2febafcSKirill A. Shutemov if (start < floor) 249c2febafcSKirill A. Shutemov return; 250c2febafcSKirill A. Shutemov if (ceiling) { 251c2febafcSKirill A. Shutemov ceiling &= P4D_MASK; 252c2febafcSKirill A. Shutemov if (!ceiling) 253c2febafcSKirill A. Shutemov return; 254c2febafcSKirill A. Shutemov } 255c2febafcSKirill A. Shutemov if (end - 1 > ceiling - 1) 256c2febafcSKirill A. Shutemov return; 257c2febafcSKirill A. Shutemov 258c2febafcSKirill A. Shutemov pud = pud_offset(p4d, start); 259c2febafcSKirill A. Shutemov p4d_clear(p4d); 260c2febafcSKirill A. Shutemov pud_free_tlb(tlb, pud, start); 261b4e98d9aSKirill A. Shutemov mm_dec_nr_puds(tlb->mm); 262c2febafcSKirill A. Shutemov } 263c2febafcSKirill A. Shutemov 264c2febafcSKirill A. Shutemov static inline void free_p4d_range(struct mmu_gather *tlb, pgd_t *pgd, 265c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 266c2febafcSKirill A. Shutemov unsigned long floor, unsigned long ceiling) 267c2febafcSKirill A. Shutemov { 268c2febafcSKirill A. Shutemov p4d_t *p4d; 269c2febafcSKirill A. Shutemov unsigned long next; 270c2febafcSKirill A. Shutemov unsigned long start; 271c2febafcSKirill A. Shutemov 272c2febafcSKirill A. Shutemov start = addr; 273c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 274c2febafcSKirill A. Shutemov do { 275c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 276c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(p4d)) 277c2febafcSKirill A. Shutemov continue; 278c2febafcSKirill A. Shutemov free_pud_range(tlb, p4d, addr, next, floor, ceiling); 279c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 280c2febafcSKirill A. Shutemov 281e0da382cSHugh Dickins start &= PGDIR_MASK; 282e0da382cSHugh Dickins if (start < floor) 283e0da382cSHugh Dickins return; 284e0da382cSHugh Dickins if (ceiling) { 285e0da382cSHugh Dickins ceiling &= PGDIR_MASK; 286e0da382cSHugh Dickins if (!ceiling) 287e0da382cSHugh Dickins return; 2881da177e4SLinus Torvalds } 289e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 290e0da382cSHugh Dickins return; 291e0da382cSHugh Dickins 292c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, start); 293e0da382cSHugh Dickins pgd_clear(pgd); 294c2febafcSKirill A. Shutemov p4d_free_tlb(tlb, p4d, start); 2951da177e4SLinus Torvalds } 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds /* 298e0da382cSHugh Dickins * This function frees user-level page tables of a process. 2991da177e4SLinus Torvalds */ 30042b77728SJan Beulich void free_pgd_range(struct mmu_gather *tlb, 301e0da382cSHugh Dickins unsigned long addr, unsigned long end, 302e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 3031da177e4SLinus Torvalds { 3041da177e4SLinus Torvalds pgd_t *pgd; 3051da177e4SLinus Torvalds unsigned long next; 3061da177e4SLinus Torvalds 307e0da382cSHugh Dickins /* 308e0da382cSHugh Dickins * The next few lines have given us lots of grief... 309e0da382cSHugh Dickins * 310e0da382cSHugh Dickins * Why are we testing PMD* at this top level? Because often 311e0da382cSHugh Dickins * there will be no work to do at all, and we'd prefer not to 312e0da382cSHugh Dickins * go all the way down to the bottom just to discover that. 313e0da382cSHugh Dickins * 314e0da382cSHugh Dickins * Why all these "- 1"s? Because 0 represents both the bottom 315e0da382cSHugh Dickins * of the address space and the top of it (using -1 for the 316e0da382cSHugh Dickins * top wouldn't help much: the masks would do the wrong thing). 317e0da382cSHugh Dickins * The rule is that addr 0 and floor 0 refer to the bottom of 318e0da382cSHugh Dickins * the address space, but end 0 and ceiling 0 refer to the top 319e0da382cSHugh Dickins * Comparisons need to use "end - 1" and "ceiling - 1" (though 320e0da382cSHugh Dickins * that end 0 case should be mythical). 321e0da382cSHugh Dickins * 322e0da382cSHugh Dickins * Wherever addr is brought up or ceiling brought down, we must 323e0da382cSHugh Dickins * be careful to reject "the opposite 0" before it confuses the 324e0da382cSHugh Dickins * subsequent tests. But what about where end is brought down 325e0da382cSHugh Dickins * by PMD_SIZE below? no, end can't go down to 0 there. 326e0da382cSHugh Dickins * 327e0da382cSHugh Dickins * Whereas we round start (addr) and ceiling down, by different 328e0da382cSHugh Dickins * masks at different levels, in order to test whether a table 329e0da382cSHugh Dickins * now has no other vmas using it, so can be freed, we don't 330e0da382cSHugh Dickins * bother to round floor or end up - the tests don't need that. 331e0da382cSHugh Dickins */ 332e0da382cSHugh Dickins 333e0da382cSHugh Dickins addr &= PMD_MASK; 334e0da382cSHugh Dickins if (addr < floor) { 335e0da382cSHugh Dickins addr += PMD_SIZE; 336e0da382cSHugh Dickins if (!addr) 337e0da382cSHugh Dickins return; 338e0da382cSHugh Dickins } 339e0da382cSHugh Dickins if (ceiling) { 340e0da382cSHugh Dickins ceiling &= PMD_MASK; 341e0da382cSHugh Dickins if (!ceiling) 342e0da382cSHugh Dickins return; 343e0da382cSHugh Dickins } 344e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 345e0da382cSHugh Dickins end -= PMD_SIZE; 346e0da382cSHugh Dickins if (addr > end - 1) 347e0da382cSHugh Dickins return; 34807e32661SAneesh Kumar K.V /* 34907e32661SAneesh Kumar K.V * We add page table cache pages with PAGE_SIZE, 35007e32661SAneesh Kumar K.V * (see pte_free_tlb()), flush the tlb if we need 35107e32661SAneesh Kumar K.V */ 352ed6a7935SPeter Zijlstra tlb_change_page_size(tlb, PAGE_SIZE); 35342b77728SJan Beulich pgd = pgd_offset(tlb->mm, addr); 3541da177e4SLinus Torvalds do { 3551da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 3561da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 3571da177e4SLinus Torvalds continue; 358c2febafcSKirill A. Shutemov free_p4d_range(tlb, pgd, addr, next, floor, ceiling); 3591da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 360e0da382cSHugh Dickins } 361e0da382cSHugh Dickins 362fd892593SLiam R. Howlett void free_pgtables(struct mmu_gather *tlb, struct ma_state *mas, 363763ecb03SLiam R. Howlett struct vm_area_struct *vma, unsigned long floor, 36498e51a22SSuren Baghdasaryan unsigned long ceiling, bool mm_wr_locked) 365e0da382cSHugh Dickins { 366763ecb03SLiam R. Howlett do { 367e0da382cSHugh Dickins unsigned long addr = vma->vm_start; 368763ecb03SLiam R. Howlett struct vm_area_struct *next; 369763ecb03SLiam R. Howlett 370763ecb03SLiam R. Howlett /* 371763ecb03SLiam R. Howlett * Note: USER_PGTABLES_CEILING may be passed as ceiling and may 372763ecb03SLiam R. Howlett * be 0. This will underflow and is okay. 373763ecb03SLiam R. Howlett */ 374fd892593SLiam R. Howlett next = mas_find(mas, ceiling - 1); 375d2406291SPeng Zhang if (unlikely(xa_is_zero(next))) 376d2406291SPeng Zhang next = NULL; 377e0da382cSHugh Dickins 3788f4f8c16SHugh Dickins /* 37925d9e2d1Snpiggin@suse.de * Hide vma from rmap and truncate_pagecache before freeing 38025d9e2d1Snpiggin@suse.de * pgtables 3818f4f8c16SHugh Dickins */ 38298e51a22SSuren Baghdasaryan if (mm_wr_locked) 38398e51a22SSuren Baghdasaryan vma_start_write(vma); 3845beb4930SRik van Riel unlink_anon_vmas(vma); 3858f4f8c16SHugh Dickins unlink_file_vma(vma); 3868f4f8c16SHugh Dickins 3879da61aefSDavid Gibson if (is_vm_hugetlb_page(vma)) { 3883bf5ee95SHugh Dickins hugetlb_free_pgd_range(tlb, addr, vma->vm_end, 3893bf5ee95SHugh Dickins floor, next ? next->vm_start : ceiling); 3903bf5ee95SHugh Dickins } else { 3913bf5ee95SHugh Dickins /* 3923bf5ee95SHugh Dickins * Optimization: gather nearby vmas into one call down 3933bf5ee95SHugh Dickins */ 3943bf5ee95SHugh Dickins while (next && next->vm_start <= vma->vm_end + PMD_SIZE 3954866920bSDavid Gibson && !is_vm_hugetlb_page(next)) { 396e0da382cSHugh Dickins vma = next; 397fd892593SLiam R. Howlett next = mas_find(mas, ceiling - 1); 398d2406291SPeng Zhang if (unlikely(xa_is_zero(next))) 399d2406291SPeng Zhang next = NULL; 40098e51a22SSuren Baghdasaryan if (mm_wr_locked) 40198e51a22SSuren Baghdasaryan vma_start_write(vma); 4025beb4930SRik van Riel unlink_anon_vmas(vma); 4038f4f8c16SHugh Dickins unlink_file_vma(vma); 404e0da382cSHugh Dickins } 4053bf5ee95SHugh Dickins free_pgd_range(tlb, addr, vma->vm_end, 406e0da382cSHugh Dickins floor, next ? next->vm_start : ceiling); 4073bf5ee95SHugh Dickins } 408e0da382cSHugh Dickins vma = next; 409763ecb03SLiam R. Howlett } while (vma); 4101da177e4SLinus Torvalds } 4111da177e4SLinus Torvalds 41203c4f204SQi Zheng void pmd_install(struct mm_struct *mm, pmd_t *pmd, pgtable_t *pte) 4131da177e4SLinus Torvalds { 41403c4f204SQi Zheng spinlock_t *ptl = pmd_lock(mm, pmd); 4151bb3630eSHugh Dickins 41603c4f204SQi Zheng if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 41703c4f204SQi Zheng mm_inc_nr_ptes(mm); 418362a61adSNick Piggin /* 419362a61adSNick Piggin * Ensure all pte setup (eg. pte page lock and page clearing) are 420362a61adSNick Piggin * visible before the pte is made visible to other CPUs by being 421362a61adSNick Piggin * put into page tables. 422362a61adSNick Piggin * 423362a61adSNick Piggin * The other side of the story is the pointer chasing in the page 424362a61adSNick Piggin * table walking code (when walking the page table without locking; 425362a61adSNick Piggin * ie. most of the time). Fortunately, these data accesses consist 426362a61adSNick Piggin * of a chain of data-dependent loads, meaning most CPUs (alpha 427362a61adSNick Piggin * being the notable exception) will already guarantee loads are 428362a61adSNick Piggin * seen in-order. See the alpha page table accessors for the 429bb7cdd38SWill Deacon * smp_rmb() barriers in page table walking code. 430362a61adSNick Piggin */ 431362a61adSNick Piggin smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */ 432ed33b5a6SQi Zheng pmd_populate(mm, pmd, *pte); 433ed33b5a6SQi Zheng *pte = NULL; 4344b471e88SKirill A. Shutemov } 435c4088ebdSKirill A. Shutemov spin_unlock(ptl); 436ed33b5a6SQi Zheng } 437ed33b5a6SQi Zheng 438ed33b5a6SQi Zheng int __pte_alloc(struct mm_struct *mm, pmd_t *pmd) 439ed33b5a6SQi Zheng { 440ed33b5a6SQi Zheng pgtable_t new = pte_alloc_one(mm); 441ed33b5a6SQi Zheng if (!new) 442ed33b5a6SQi Zheng return -ENOMEM; 4438ac1f832SAndrea Arcangeli 44403c4f204SQi Zheng pmd_install(mm, pmd, &new); 4452f569afdSMartin Schwidefsky if (new) 4462f569afdSMartin Schwidefsky pte_free(mm, new); 4471bb3630eSHugh Dickins return 0; 4481da177e4SLinus Torvalds } 4491da177e4SLinus Torvalds 4504cf58924SJoel Fernandes (Google) int __pte_alloc_kernel(pmd_t *pmd) 4511da177e4SLinus Torvalds { 4524cf58924SJoel Fernandes (Google) pte_t *new = pte_alloc_one_kernel(&init_mm); 4531da177e4SLinus Torvalds if (!new) 4541bb3630eSHugh Dickins return -ENOMEM; 4551da177e4SLinus Torvalds 456872fec16SHugh Dickins spin_lock(&init_mm.page_table_lock); 4578ac1f832SAndrea Arcangeli if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 458ed33b5a6SQi Zheng smp_wmb(); /* See comment in pmd_install() */ 459872fec16SHugh Dickins pmd_populate_kernel(&init_mm, pmd, new); 4602f569afdSMartin Schwidefsky new = NULL; 4614b471e88SKirill A. Shutemov } 462872fec16SHugh Dickins spin_unlock(&init_mm.page_table_lock); 4632f569afdSMartin Schwidefsky if (new) 4642f569afdSMartin Schwidefsky pte_free_kernel(&init_mm, new); 4651bb3630eSHugh Dickins return 0; 4661da177e4SLinus Torvalds } 4671da177e4SLinus Torvalds 468d559db08SKAMEZAWA Hiroyuki static inline void init_rss_vec(int *rss) 469ae859762SHugh Dickins { 470d559db08SKAMEZAWA Hiroyuki memset(rss, 0, sizeof(int) * NR_MM_COUNTERS); 471d559db08SKAMEZAWA Hiroyuki } 472d559db08SKAMEZAWA Hiroyuki 473d559db08SKAMEZAWA Hiroyuki static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss) 474d559db08SKAMEZAWA Hiroyuki { 475d559db08SKAMEZAWA Hiroyuki int i; 476d559db08SKAMEZAWA Hiroyuki 477d559db08SKAMEZAWA Hiroyuki for (i = 0; i < NR_MM_COUNTERS; i++) 478d559db08SKAMEZAWA Hiroyuki if (rss[i]) 479d559db08SKAMEZAWA Hiroyuki add_mm_counter(mm, i, rss[i]); 480ae859762SHugh Dickins } 481ae859762SHugh Dickins 4821da177e4SLinus Torvalds /* 4836aab341eSLinus Torvalds * This function is called to print an error when a bad pte 4846aab341eSLinus Torvalds * is found. For example, we might have a PFN-mapped pte in 4856aab341eSLinus Torvalds * a region that doesn't allow it. 486b5810039SNick Piggin * 487b5810039SNick Piggin * The calling function must still handle the error. 488b5810039SNick Piggin */ 4893dc14741SHugh Dickins static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr, 4903dc14741SHugh Dickins pte_t pte, struct page *page) 491b5810039SNick Piggin { 4923dc14741SHugh Dickins pgd_t *pgd = pgd_offset(vma->vm_mm, addr); 493c2febafcSKirill A. Shutemov p4d_t *p4d = p4d_offset(pgd, addr); 494c2febafcSKirill A. Shutemov pud_t *pud = pud_offset(p4d, addr); 4953dc14741SHugh Dickins pmd_t *pmd = pmd_offset(pud, addr); 4963dc14741SHugh Dickins struct address_space *mapping; 4973dc14741SHugh Dickins pgoff_t index; 498d936cf9bSHugh Dickins static unsigned long resume; 499d936cf9bSHugh Dickins static unsigned long nr_shown; 500d936cf9bSHugh Dickins static unsigned long nr_unshown; 501d936cf9bSHugh Dickins 502d936cf9bSHugh Dickins /* 503d936cf9bSHugh Dickins * Allow a burst of 60 reports, then keep quiet for that minute; 504d936cf9bSHugh Dickins * or allow a steady drip of one report per second. 505d936cf9bSHugh Dickins */ 506d936cf9bSHugh Dickins if (nr_shown == 60) { 507d936cf9bSHugh Dickins if (time_before(jiffies, resume)) { 508d936cf9bSHugh Dickins nr_unshown++; 509d936cf9bSHugh Dickins return; 510d936cf9bSHugh Dickins } 511d936cf9bSHugh Dickins if (nr_unshown) { 5121170532bSJoe Perches pr_alert("BUG: Bad page map: %lu messages suppressed\n", 513d936cf9bSHugh Dickins nr_unshown); 514d936cf9bSHugh Dickins nr_unshown = 0; 515d936cf9bSHugh Dickins } 516d936cf9bSHugh Dickins nr_shown = 0; 517d936cf9bSHugh Dickins } 518d936cf9bSHugh Dickins if (nr_shown++ == 0) 519d936cf9bSHugh Dickins resume = jiffies + 60 * HZ; 5203dc14741SHugh Dickins 5213dc14741SHugh Dickins mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL; 5223dc14741SHugh Dickins index = linear_page_index(vma, addr); 5233dc14741SHugh Dickins 5241170532bSJoe Perches pr_alert("BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n", 5253dc14741SHugh Dickins current->comm, 5263dc14741SHugh Dickins (long long)pte_val(pte), (long long)pmd_val(*pmd)); 527718a3821SWu Fengguang if (page) 528f0b791a3SDave Hansen dump_page(page, "bad pte"); 5296aa9b8b2SKefeng Wang pr_alert("addr:%px vm_flags:%08lx anon_vma:%px mapping:%px index:%lx\n", 5303dc14741SHugh Dickins (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index); 5317e0a1265SMatthew Wilcox (Oracle) pr_alert("file:%pD fault:%ps mmap:%ps read_folio:%ps\n", 5322682582aSKonstantin Khlebnikov vma->vm_file, 5332682582aSKonstantin Khlebnikov vma->vm_ops ? vma->vm_ops->fault : NULL, 5342682582aSKonstantin Khlebnikov vma->vm_file ? vma->vm_file->f_op->mmap : NULL, 5357e0a1265SMatthew Wilcox (Oracle) mapping ? mapping->a_ops->read_folio : NULL); 536b5810039SNick Piggin dump_stack(); 537373d4d09SRusty Russell add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); 538b5810039SNick Piggin } 539b5810039SNick Piggin 540b5810039SNick Piggin /* 5417e675137SNick Piggin * vm_normal_page -- This function gets the "struct page" associated with a pte. 5426aab341eSLinus Torvalds * 5437e675137SNick Piggin * "Special" mappings do not wish to be associated with a "struct page" (either 5447e675137SNick Piggin * it doesn't exist, or it exists but they don't want to touch it). In this 5457e675137SNick Piggin * case, NULL is returned here. "Normal" mappings do have a struct page. 546b379d790SJared Hulbert * 5477e675137SNick Piggin * There are 2 broad cases. Firstly, an architecture may define a pte_special() 5487e675137SNick Piggin * pte bit, in which case this function is trivial. Secondly, an architecture 5497e675137SNick Piggin * may not have a spare pte bit, which requires a more complicated scheme, 5507e675137SNick Piggin * described below. 5517e675137SNick Piggin * 5527e675137SNick Piggin * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a 5537e675137SNick Piggin * special mapping (even if there are underlying and valid "struct pages"). 5547e675137SNick Piggin * COWed pages of a VM_PFNMAP are always normal. 5556aab341eSLinus Torvalds * 556b379d790SJared Hulbert * The way we recognize COWed pages within VM_PFNMAP mappings is through the 557b379d790SJared Hulbert * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit 5587e675137SNick Piggin * set, and the vm_pgoff will point to the first PFN mapped: thus every special 5597e675137SNick Piggin * mapping will always honor the rule 5606aab341eSLinus Torvalds * 5616aab341eSLinus Torvalds * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT) 5626aab341eSLinus Torvalds * 5637e675137SNick Piggin * And for normal mappings this is false. 564b379d790SJared Hulbert * 5657e675137SNick Piggin * This restricts such mappings to be a linear translation from virtual address 5667e675137SNick Piggin * to pfn. To get around this restriction, we allow arbitrary mappings so long 5677e675137SNick Piggin * as the vma is not a COW mapping; in that case, we know that all ptes are 5687e675137SNick Piggin * special (because none can have been COWed). 569b379d790SJared Hulbert * 570b379d790SJared Hulbert * 5717e675137SNick Piggin * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP. 5727e675137SNick Piggin * 573b379d790SJared Hulbert * VM_MIXEDMAP mappings can likewise contain memory with or without "struct 574b379d790SJared Hulbert * page" backing, however the difference is that _all_ pages with a struct 575b379d790SJared Hulbert * page (that is, those where pfn_valid is true) are refcounted and considered 576b379d790SJared Hulbert * normal pages by the VM. The disadvantage is that pages are refcounted 577b379d790SJared Hulbert * (which can be slower and simply not an option for some PFNMAP users). The 578b379d790SJared Hulbert * advantage is that we don't have to follow the strict linearity rule of 579b379d790SJared Hulbert * PFNMAP mappings in order to support COWable mappings. 580b379d790SJared Hulbert * 581ee498ed7SHugh Dickins */ 58225b2995aSChristoph Hellwig struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, 58325b2995aSChristoph Hellwig pte_t pte) 584ee498ed7SHugh Dickins { 58522b31eecSHugh Dickins unsigned long pfn = pte_pfn(pte); 5867e675137SNick Piggin 58700b3a331SLaurent Dufour if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL)) { 588b38af472SHugh Dickins if (likely(!pte_special(pte))) 58922b31eecSHugh Dickins goto check_pfn; 590667a0a06SDavid Vrabel if (vma->vm_ops && vma->vm_ops->find_special_page) 591667a0a06SDavid Vrabel return vma->vm_ops->find_special_page(vma, addr); 592a13ea5b7SHugh Dickins if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)) 593a13ea5b7SHugh Dickins return NULL; 594df6ad698SJérôme Glisse if (is_zero_pfn(pfn)) 595df6ad698SJérôme Glisse return NULL; 596e1fb4a08SDave Jiang if (pte_devmap(pte)) 5973218f871SAlex Sierra /* 5983218f871SAlex Sierra * NOTE: New users of ZONE_DEVICE will not set pte_devmap() 5993218f871SAlex Sierra * and will have refcounts incremented on their struct pages 6003218f871SAlex Sierra * when they are inserted into PTEs, thus they are safe to 6013218f871SAlex Sierra * return here. Legacy ZONE_DEVICE pages that set pte_devmap() 6023218f871SAlex Sierra * do not have refcounts. Example of legacy ZONE_DEVICE is 6033218f871SAlex Sierra * MEMORY_DEVICE_FS_DAX type in pmem or virtio_fs drivers. 6043218f871SAlex Sierra */ 605e1fb4a08SDave Jiang return NULL; 606e1fb4a08SDave Jiang 60722b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 6087e675137SNick Piggin return NULL; 6097e675137SNick Piggin } 6107e675137SNick Piggin 61100b3a331SLaurent Dufour /* !CONFIG_ARCH_HAS_PTE_SPECIAL case follows: */ 6127e675137SNick Piggin 613b379d790SJared Hulbert if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { 614b379d790SJared Hulbert if (vma->vm_flags & VM_MIXEDMAP) { 615b379d790SJared Hulbert if (!pfn_valid(pfn)) 616b379d790SJared Hulbert return NULL; 617b379d790SJared Hulbert goto out; 618b379d790SJared Hulbert } else { 6197e675137SNick Piggin unsigned long off; 6207e675137SNick Piggin off = (addr - vma->vm_start) >> PAGE_SHIFT; 6216aab341eSLinus Torvalds if (pfn == vma->vm_pgoff + off) 6226aab341eSLinus Torvalds return NULL; 62367121172SLinus Torvalds if (!is_cow_mapping(vma->vm_flags)) 624fb155c16SLinus Torvalds return NULL; 6256aab341eSLinus Torvalds } 626b379d790SJared Hulbert } 6276aab341eSLinus Torvalds 628b38af472SHugh Dickins if (is_zero_pfn(pfn)) 629b38af472SHugh Dickins return NULL; 63000b3a331SLaurent Dufour 63122b31eecSHugh Dickins check_pfn: 63222b31eecSHugh Dickins if (unlikely(pfn > highest_memmap_pfn)) { 63322b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 63422b31eecSHugh Dickins return NULL; 63522b31eecSHugh Dickins } 6366aab341eSLinus Torvalds 6376aab341eSLinus Torvalds /* 6387e675137SNick Piggin * NOTE! We still have PageReserved() pages in the page tables. 6397e675137SNick Piggin * eg. VDSO mappings can cause them to exist. 6406aab341eSLinus Torvalds */ 641b379d790SJared Hulbert out: 6426aab341eSLinus Torvalds return pfn_to_page(pfn); 643ee498ed7SHugh Dickins } 644ee498ed7SHugh Dickins 645318e9342SVishal Moola (Oracle) struct folio *vm_normal_folio(struct vm_area_struct *vma, unsigned long addr, 646318e9342SVishal Moola (Oracle) pte_t pte) 647318e9342SVishal Moola (Oracle) { 648318e9342SVishal Moola (Oracle) struct page *page = vm_normal_page(vma, addr, pte); 649318e9342SVishal Moola (Oracle) 650318e9342SVishal Moola (Oracle) if (page) 651318e9342SVishal Moola (Oracle) return page_folio(page); 652318e9342SVishal Moola (Oracle) return NULL; 653318e9342SVishal Moola (Oracle) } 654318e9342SVishal Moola (Oracle) 65528093f9fSGerald Schaefer #ifdef CONFIG_TRANSPARENT_HUGEPAGE 65628093f9fSGerald Schaefer struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr, 65728093f9fSGerald Schaefer pmd_t pmd) 65828093f9fSGerald Schaefer { 65928093f9fSGerald Schaefer unsigned long pfn = pmd_pfn(pmd); 66028093f9fSGerald Schaefer 66128093f9fSGerald Schaefer /* 66228093f9fSGerald Schaefer * There is no pmd_special() but there may be special pmds, e.g. 66328093f9fSGerald Schaefer * in a direct-access (dax) mapping, so let's just replicate the 66400b3a331SLaurent Dufour * !CONFIG_ARCH_HAS_PTE_SPECIAL case from vm_normal_page() here. 66528093f9fSGerald Schaefer */ 66628093f9fSGerald Schaefer if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { 66728093f9fSGerald Schaefer if (vma->vm_flags & VM_MIXEDMAP) { 66828093f9fSGerald Schaefer if (!pfn_valid(pfn)) 66928093f9fSGerald Schaefer return NULL; 67028093f9fSGerald Schaefer goto out; 67128093f9fSGerald Schaefer } else { 67228093f9fSGerald Schaefer unsigned long off; 67328093f9fSGerald Schaefer off = (addr - vma->vm_start) >> PAGE_SHIFT; 67428093f9fSGerald Schaefer if (pfn == vma->vm_pgoff + off) 67528093f9fSGerald Schaefer return NULL; 67628093f9fSGerald Schaefer if (!is_cow_mapping(vma->vm_flags)) 67728093f9fSGerald Schaefer return NULL; 67828093f9fSGerald Schaefer } 67928093f9fSGerald Schaefer } 68028093f9fSGerald Schaefer 681e1fb4a08SDave Jiang if (pmd_devmap(pmd)) 682e1fb4a08SDave Jiang return NULL; 6833cde287bSYu Zhao if (is_huge_zero_pmd(pmd)) 68428093f9fSGerald Schaefer return NULL; 68528093f9fSGerald Schaefer if (unlikely(pfn > highest_memmap_pfn)) 68628093f9fSGerald Schaefer return NULL; 68728093f9fSGerald Schaefer 68828093f9fSGerald Schaefer /* 68928093f9fSGerald Schaefer * NOTE! We still have PageReserved() pages in the page tables. 69028093f9fSGerald Schaefer * eg. VDSO mappings can cause them to exist. 69128093f9fSGerald Schaefer */ 69228093f9fSGerald Schaefer out: 69328093f9fSGerald Schaefer return pfn_to_page(pfn); 69428093f9fSGerald Schaefer } 69565610453SKefeng Wang 69665610453SKefeng Wang struct folio *vm_normal_folio_pmd(struct vm_area_struct *vma, 69765610453SKefeng Wang unsigned long addr, pmd_t pmd) 69865610453SKefeng Wang { 69965610453SKefeng Wang struct page *page = vm_normal_page_pmd(vma, addr, pmd); 70065610453SKefeng Wang 70165610453SKefeng Wang if (page) 70265610453SKefeng Wang return page_folio(page); 70365610453SKefeng Wang return NULL; 70465610453SKefeng Wang } 70528093f9fSGerald Schaefer #endif 70628093f9fSGerald Schaefer 707b756a3b5SAlistair Popple static void restore_exclusive_pte(struct vm_area_struct *vma, 708b756a3b5SAlistair Popple struct page *page, unsigned long address, 709b756a3b5SAlistair Popple pte_t *ptep) 710b756a3b5SAlistair Popple { 711b832a354SDavid Hildenbrand struct folio *folio = page_folio(page); 712c33c7948SRyan Roberts pte_t orig_pte; 713b756a3b5SAlistair Popple pte_t pte; 714b756a3b5SAlistair Popple swp_entry_t entry; 715b756a3b5SAlistair Popple 716c33c7948SRyan Roberts orig_pte = ptep_get(ptep); 717b756a3b5SAlistair Popple pte = pte_mkold(mk_pte(page, READ_ONCE(vma->vm_page_prot))); 718c33c7948SRyan Roberts if (pte_swp_soft_dirty(orig_pte)) 719b756a3b5SAlistair Popple pte = pte_mksoft_dirty(pte); 720b756a3b5SAlistair Popple 721c33c7948SRyan Roberts entry = pte_to_swp_entry(orig_pte); 722c33c7948SRyan Roberts if (pte_swp_uffd_wp(orig_pte)) 723b756a3b5SAlistair Popple pte = pte_mkuffd_wp(pte); 724b756a3b5SAlistair Popple else if (is_writable_device_exclusive_entry(entry)) 725b756a3b5SAlistair Popple pte = maybe_mkwrite(pte_mkdirty(pte), vma); 726b756a3b5SAlistair Popple 727b832a354SDavid Hildenbrand VM_BUG_ON_FOLIO(pte_write(pte) && (!folio_test_anon(folio) && 728b832a354SDavid Hildenbrand PageAnonExclusive(page)), folio); 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 */ 734b832a354SDavid Hildenbrand if (folio_test_anon(folio)) 735b832a354SDavid Hildenbrand folio_add_anon_rmap_pte(folio, 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; 78608e7795eSDavid Hildenbrand struct folio *folio; 7871da177e4SLinus Torvalds struct page *page; 788c33c7948SRyan Roberts swp_entry_t entry = pte_to_swp_entry(orig_pte); 7890697212aSChristoph Lameter 7902022b4d1SHugh Dickins if (likely(!non_swap_entry(entry))) { 791570a335bSHugh Dickins if (swap_duplicate(entry) < 0) 7929a5cc85cSAlistair Popple return -EIO; 793570a335bSHugh Dickins 7941da177e4SLinus Torvalds /* make sure dst_mm is on swapoff's mmlist. */ 7951da177e4SLinus Torvalds if (unlikely(list_empty(&dst_mm->mmlist))) { 7961da177e4SLinus Torvalds spin_lock(&mmlist_lock); 797f412ac08SHugh Dickins if (list_empty(&dst_mm->mmlist)) 798f412ac08SHugh Dickins list_add(&dst_mm->mmlist, 799f412ac08SHugh Dickins &src_mm->mmlist); 8001da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 8011da177e4SLinus Torvalds } 8021493a191SDavid Hildenbrand /* Mark the swap entry as shared. */ 803c33c7948SRyan Roberts if (pte_swp_exclusive(orig_pte)) { 804c33c7948SRyan Roberts pte = pte_swp_clear_exclusive(orig_pte); 8051493a191SDavid Hildenbrand set_pte_at(src_mm, addr, src_pte, pte); 8061493a191SDavid Hildenbrand } 807b084d435SKAMEZAWA Hiroyuki rss[MM_SWAPENTS]++; 8082022b4d1SHugh Dickins } else if (is_migration_entry(entry)) { 809530c2a0dSKefeng Wang folio = pfn_swap_entry_folio(entry); 8109f9f1acdSKonstantin Khlebnikov 811a23f517bSKefeng Wang rss[mm_counter(folio)]++; 8129f9f1acdSKonstantin Khlebnikov 8136c287605SDavid Hildenbrand if (!is_readable_migration_entry(entry) && 8140697212aSChristoph Lameter is_cow_mapping(vm_flags)) { 8150697212aSChristoph Lameter /* 8166c287605SDavid Hildenbrand * COW mappings require pages in both parent and child 8176c287605SDavid Hildenbrand * to be set to read. A previously exclusive entry is 8186c287605SDavid Hildenbrand * now shared. 8190697212aSChristoph Lameter */ 8204dd845b5SAlistair Popple entry = make_readable_migration_entry( 8214dd845b5SAlistair Popple swp_offset(entry)); 8220697212aSChristoph Lameter pte = swp_entry_to_pte(entry); 823c33c7948SRyan Roberts if (pte_swp_soft_dirty(orig_pte)) 824c3d16e16SCyrill Gorcunov pte = pte_swp_mksoft_dirty(pte); 825c33c7948SRyan Roberts if (pte_swp_uffd_wp(orig_pte)) 826f45ec5ffSPeter Xu pte = pte_swp_mkuffd_wp(pte); 8270697212aSChristoph Lameter set_pte_at(src_mm, addr, src_pte, pte); 8280697212aSChristoph Lameter } 8295042db43SJérôme Glisse } else if (is_device_private_entry(entry)) { 830af5cdaf8SAlistair Popple page = pfn_swap_entry_to_page(entry); 83108e7795eSDavid Hildenbrand folio = page_folio(page); 8325042db43SJérôme Glisse 8335042db43SJérôme Glisse /* 8345042db43SJérôme Glisse * Update rss count even for unaddressable pages, as 8355042db43SJérôme Glisse * they should treated just like normal pages in this 8365042db43SJérôme Glisse * respect. 8375042db43SJérôme Glisse * 8385042db43SJérôme Glisse * We will likely want to have some new rss counters 8395042db43SJérôme Glisse * for unaddressable pages, at some point. But for now 8405042db43SJérôme Glisse * keep things as they are. 8415042db43SJérôme Glisse */ 84208e7795eSDavid Hildenbrand folio_get(folio); 843a23f517bSKefeng Wang rss[mm_counter(folio)]++; 844fb3d824dSDavid Hildenbrand /* Cannot fail as these pages cannot get pinned. */ 84508e7795eSDavid Hildenbrand folio_try_dup_anon_rmap_pte(folio, page, src_vma); 8465042db43SJérôme Glisse 8475042db43SJérôme Glisse /* 8485042db43SJérôme Glisse * We do not preserve soft-dirty information, because so 8495042db43SJérôme Glisse * far, checkpoint/restore is the only feature that 8505042db43SJérôme Glisse * requires that. And checkpoint/restore does not work 8515042db43SJérôme Glisse * when a device driver is involved (you cannot easily 8525042db43SJérôme Glisse * save and restore device driver state). 8535042db43SJérôme Glisse */ 8544dd845b5SAlistair Popple if (is_writable_device_private_entry(entry) && 8555042db43SJérôme Glisse is_cow_mapping(vm_flags)) { 8564dd845b5SAlistair Popple entry = make_readable_device_private_entry( 8574dd845b5SAlistair Popple swp_offset(entry)); 8585042db43SJérôme Glisse pte = swp_entry_to_pte(entry); 859c33c7948SRyan Roberts if (pte_swp_uffd_wp(orig_pte)) 860f45ec5ffSPeter Xu pte = pte_swp_mkuffd_wp(pte); 8615042db43SJérôme Glisse set_pte_at(src_mm, addr, src_pte, pte); 8625042db43SJérôme Glisse } 863b756a3b5SAlistair Popple } else if (is_device_exclusive_entry(entry)) { 864b756a3b5SAlistair Popple /* 865b756a3b5SAlistair Popple * Make device exclusive entries present by restoring the 866b756a3b5SAlistair Popple * original entry then copying as for a present pte. Device 867b756a3b5SAlistair Popple * exclusive entries currently only support private writable 868b756a3b5SAlistair Popple * (ie. COW) mappings. 869b756a3b5SAlistair Popple */ 870b756a3b5SAlistair Popple VM_BUG_ON(!is_cow_mapping(src_vma->vm_flags)); 871b756a3b5SAlistair Popple if (try_restore_exclusive_pte(src_pte, src_vma, addr)) 872b756a3b5SAlistair Popple return -EBUSY; 873b756a3b5SAlistair Popple return -ENOENT; 874c56d1b62SPeter Xu } else if (is_pte_marker_entry(entry)) { 875af19487fSAxel Rasmussen pte_marker marker = copy_pte_marker(entry, dst_vma); 876af19487fSAxel Rasmussen 877af19487fSAxel Rasmussen if (marker) 878af19487fSAxel Rasmussen set_pte_at(dst_mm, addr, dst_pte, 879af19487fSAxel Rasmussen make_pte_marker(marker)); 880c56d1b62SPeter Xu return 0; 8811da177e4SLinus Torvalds } 8828f34f1eaSPeter Xu if (!userfaultfd_wp(dst_vma)) 8838f34f1eaSPeter Xu pte = pte_swp_clear_uffd_wp(pte); 884df3a57d1SLinus Torvalds set_pte_at(dst_mm, addr, dst_pte, pte); 885df3a57d1SLinus Torvalds return 0; 8861da177e4SLinus Torvalds } 8871da177e4SLinus Torvalds 88870e806e4SPeter Xu /* 889b51ad4f8SDavid Hildenbrand * Copy a present and normal page. 89070e806e4SPeter Xu * 891b51ad4f8SDavid Hildenbrand * NOTE! The usual case is that this isn't required; 892b51ad4f8SDavid Hildenbrand * instead, the caller can just increase the page refcount 893b51ad4f8SDavid Hildenbrand * and re-use the pte the traditional way. 89470e806e4SPeter Xu * 89570e806e4SPeter Xu * And if we need a pre-allocated page but don't yet have 89670e806e4SPeter Xu * one, return a negative error to let the preallocation 89770e806e4SPeter Xu * code know so that it can do so outside the page table 89870e806e4SPeter Xu * lock. 89970e806e4SPeter Xu */ 90070e806e4SPeter Xu static inline int 901c78f4636SPeter Xu copy_present_page(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, 902c78f4636SPeter Xu pte_t *dst_pte, pte_t *src_pte, unsigned long addr, int *rss, 903edf50470SMatthew Wilcox (Oracle) struct folio **prealloc, struct page *page) 90470e806e4SPeter Xu { 905edf50470SMatthew Wilcox (Oracle) struct folio *new_folio; 906b51ad4f8SDavid Hildenbrand pte_t pte; 90770e806e4SPeter Xu 908edf50470SMatthew Wilcox (Oracle) new_folio = *prealloc; 909edf50470SMatthew Wilcox (Oracle) if (!new_folio) 91070e806e4SPeter Xu return -EAGAIN; 91170e806e4SPeter Xu 91270e806e4SPeter Xu /* 91370e806e4SPeter Xu * We have a prealloc page, all good! Take it 91470e806e4SPeter Xu * over and copy the page & arm it. 91570e806e4SPeter Xu */ 91670e806e4SPeter Xu *prealloc = NULL; 917edf50470SMatthew Wilcox (Oracle) copy_user_highpage(&new_folio->page, page, addr, src_vma); 918edf50470SMatthew Wilcox (Oracle) __folio_mark_uptodate(new_folio); 919edf50470SMatthew Wilcox (Oracle) folio_add_new_anon_rmap(new_folio, dst_vma, addr); 920edf50470SMatthew Wilcox (Oracle) folio_add_lru_vma(new_folio, dst_vma); 921edf50470SMatthew Wilcox (Oracle) rss[MM_ANONPAGES]++; 92270e806e4SPeter Xu 92370e806e4SPeter Xu /* All done, just insert the new page copy in the child */ 924edf50470SMatthew Wilcox (Oracle) pte = mk_pte(&new_folio->page, dst_vma->vm_page_prot); 925c78f4636SPeter Xu pte = maybe_mkwrite(pte_mkdirty(pte), dst_vma); 926c33c7948SRyan Roberts if (userfaultfd_pte_wp(dst_vma, ptep_get(src_pte))) 9278f34f1eaSPeter Xu /* Uffd-wp needs to be delivered to dest pte as well */ 928f1eb1bacSPeter Xu pte = pte_mkuffd_wp(pte); 929c78f4636SPeter Xu set_pte_at(dst_vma->vm_mm, addr, dst_pte, pte); 93070e806e4SPeter Xu return 0; 93170e806e4SPeter Xu } 93270e806e4SPeter Xu 933f8d93776SDavid Hildenbrand static __always_inline void __copy_present_ptes(struct vm_area_struct *dst_vma, 93423ed1908SDavid Hildenbrand struct vm_area_struct *src_vma, pte_t *dst_pte, pte_t *src_pte, 935f8d93776SDavid Hildenbrand pte_t pte, unsigned long addr, int nr) 93623ed1908SDavid Hildenbrand { 93723ed1908SDavid Hildenbrand struct mm_struct *src_mm = src_vma->vm_mm; 93823ed1908SDavid Hildenbrand 93923ed1908SDavid Hildenbrand /* If it's a COW mapping, write protect it both processes. */ 94023ed1908SDavid Hildenbrand if (is_cow_mapping(src_vma->vm_flags) && pte_write(pte)) { 941f8d93776SDavid Hildenbrand wrprotect_ptes(src_mm, addr, src_pte, nr); 94223ed1908SDavid Hildenbrand pte = pte_wrprotect(pte); 94323ed1908SDavid Hildenbrand } 94423ed1908SDavid Hildenbrand 94523ed1908SDavid Hildenbrand /* If it's a shared mapping, mark it clean in the child. */ 94623ed1908SDavid Hildenbrand if (src_vma->vm_flags & VM_SHARED) 94723ed1908SDavid Hildenbrand pte = pte_mkclean(pte); 94823ed1908SDavid Hildenbrand pte = pte_mkold(pte); 94923ed1908SDavid Hildenbrand 95023ed1908SDavid Hildenbrand if (!userfaultfd_wp(dst_vma)) 95123ed1908SDavid Hildenbrand pte = pte_clear_uffd_wp(pte); 95223ed1908SDavid Hildenbrand 953f8d93776SDavid Hildenbrand set_ptes(dst_vma->vm_mm, addr, dst_pte, pte, nr); 95423ed1908SDavid Hildenbrand } 95523ed1908SDavid Hildenbrand 95625365e10SDavid Hildenbrand /* Flags for folio_pte_batch(). */ 95725365e10SDavid Hildenbrand typedef int __bitwise fpb_t; 95825365e10SDavid Hildenbrand 95925365e10SDavid Hildenbrand /* Compare PTEs after pte_mkclean(), ignoring the dirty bit. */ 96025365e10SDavid Hildenbrand #define FPB_IGNORE_DIRTY ((__force fpb_t)BIT(0)) 96125365e10SDavid Hildenbrand 96225365e10SDavid Hildenbrand /* Compare PTEs after pte_clear_soft_dirty(), ignoring the soft-dirty bit. */ 96325365e10SDavid Hildenbrand #define FPB_IGNORE_SOFT_DIRTY ((__force fpb_t)BIT(1)) 96425365e10SDavid Hildenbrand 96525365e10SDavid Hildenbrand static inline pte_t __pte_batch_clear_ignored(pte_t pte, fpb_t flags) 96625365e10SDavid Hildenbrand { 96725365e10SDavid Hildenbrand if (flags & FPB_IGNORE_DIRTY) 96825365e10SDavid Hildenbrand pte = pte_mkclean(pte); 96925365e10SDavid Hildenbrand if (likely(flags & FPB_IGNORE_SOFT_DIRTY)) 97025365e10SDavid Hildenbrand pte = pte_clear_soft_dirty(pte); 971d7c0e5f7SDavid Hildenbrand return pte_wrprotect(pte_mkold(pte)); 97225365e10SDavid Hildenbrand } 97325365e10SDavid Hildenbrand 97470e806e4SPeter Xu /* 975f8d93776SDavid Hildenbrand * Detect a PTE batch: consecutive (present) PTEs that map consecutive 976f8d93776SDavid Hildenbrand * pages of the same folio. 977f8d93776SDavid Hildenbrand * 97825365e10SDavid Hildenbrand * All PTEs inside a PTE batch have the same PTE bits set, excluding the PFN, 979d7c0e5f7SDavid Hildenbrand * the accessed bit, writable bit, dirty bit (with FPB_IGNORE_DIRTY) and 980d7c0e5f7SDavid Hildenbrand * soft-dirty bit (with FPB_IGNORE_SOFT_DIRTY). 981d7c0e5f7SDavid Hildenbrand * 982d7c0e5f7SDavid Hildenbrand * If "any_writable" is set, it will indicate if any other PTE besides the 983d7c0e5f7SDavid Hildenbrand * first (given) PTE is writable. 984f8d93776SDavid Hildenbrand */ 985f8d93776SDavid Hildenbrand static inline int folio_pte_batch(struct folio *folio, unsigned long addr, 986d7c0e5f7SDavid Hildenbrand pte_t *start_ptep, pte_t pte, int max_nr, fpb_t flags, 987d7c0e5f7SDavid Hildenbrand bool *any_writable) 988f8d93776SDavid Hildenbrand { 989f8d93776SDavid Hildenbrand unsigned long folio_end_pfn = folio_pfn(folio) + folio_nr_pages(folio); 990f8d93776SDavid Hildenbrand const pte_t *end_ptep = start_ptep + max_nr; 991c6ec76a2SRyan Roberts pte_t expected_pte, *ptep; 992d7c0e5f7SDavid Hildenbrand bool writable; 993c6ec76a2SRyan Roberts int nr; 994d7c0e5f7SDavid Hildenbrand 995d7c0e5f7SDavid Hildenbrand if (any_writable) 996d7c0e5f7SDavid Hildenbrand *any_writable = false; 997f8d93776SDavid Hildenbrand 998f8d93776SDavid Hildenbrand VM_WARN_ON_FOLIO(!pte_present(pte), folio); 999f8d93776SDavid Hildenbrand 1000c6ec76a2SRyan Roberts nr = pte_batch_hint(start_ptep, pte); 1001c6ec76a2SRyan Roberts expected_pte = __pte_batch_clear_ignored(pte_advance_pfn(pte, nr), flags); 1002c6ec76a2SRyan Roberts ptep = start_ptep + nr; 1003c6ec76a2SRyan Roberts 1004c6ec76a2SRyan Roberts while (ptep < end_ptep) { 1005d7c0e5f7SDavid Hildenbrand pte = ptep_get(ptep); 1006d7c0e5f7SDavid Hildenbrand if (any_writable) 1007d7c0e5f7SDavid Hildenbrand writable = !!pte_write(pte); 1008d7c0e5f7SDavid Hildenbrand pte = __pte_batch_clear_ignored(pte, flags); 1009f8d93776SDavid Hildenbrand 1010f8d93776SDavid Hildenbrand if (!pte_same(pte, expected_pte)) 1011f8d93776SDavid Hildenbrand break; 1012f8d93776SDavid Hildenbrand 1013f8d93776SDavid Hildenbrand /* 1014f8d93776SDavid Hildenbrand * Stop immediately once we reached the end of the folio. In 1015f8d93776SDavid Hildenbrand * corner cases the next PFN might fall into a different 1016f8d93776SDavid Hildenbrand * folio. 1017f8d93776SDavid Hildenbrand */ 1018c6ec76a2SRyan Roberts if (pte_pfn(pte) >= folio_end_pfn) 1019f8d93776SDavid Hildenbrand break; 1020f8d93776SDavid Hildenbrand 1021d7c0e5f7SDavid Hildenbrand if (any_writable) 1022d7c0e5f7SDavid Hildenbrand *any_writable |= writable; 1023d7c0e5f7SDavid Hildenbrand 1024c6ec76a2SRyan Roberts nr = pte_batch_hint(ptep, pte); 1025c6ec76a2SRyan Roberts expected_pte = pte_advance_pfn(expected_pte, nr); 1026c6ec76a2SRyan Roberts ptep += nr; 1027f8d93776SDavid Hildenbrand } 1028f8d93776SDavid Hildenbrand 1029c6ec76a2SRyan Roberts return min(ptep - start_ptep, max_nr); 1030f8d93776SDavid Hildenbrand } 1031f8d93776SDavid Hildenbrand 1032f8d93776SDavid Hildenbrand /* 1033f8d93776SDavid Hildenbrand * Copy one present PTE, trying to batch-process subsequent PTEs that map 1034f8d93776SDavid Hildenbrand * consecutive pages of the same folio by copying them as well. 1035f8d93776SDavid Hildenbrand * 1036f8d93776SDavid Hildenbrand * Returns -EAGAIN if one preallocated page is required to copy the next PTE. 1037f8d93776SDavid Hildenbrand * Otherwise, returns the number of copied PTEs (at least 1). 103870e806e4SPeter Xu */ 103970e806e4SPeter Xu static inline int 1040f8d93776SDavid Hildenbrand copy_present_ptes(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, 104153723298SDavid Hildenbrand pte_t *dst_pte, pte_t *src_pte, pte_t pte, unsigned long addr, 1042f8d93776SDavid Hildenbrand int max_nr, int *rss, struct folio **prealloc) 1043df3a57d1SLinus Torvalds { 1044df3a57d1SLinus Torvalds struct page *page; 104514ddee41SMatthew Wilcox (Oracle) struct folio *folio; 1046d7c0e5f7SDavid Hildenbrand bool any_writable; 104725365e10SDavid Hildenbrand fpb_t flags = 0; 1048f8d93776SDavid Hildenbrand int err, nr; 1049df3a57d1SLinus Torvalds 1050c78f4636SPeter Xu page = vm_normal_page(src_vma, addr, pte); 105123ed1908SDavid Hildenbrand if (unlikely(!page)) 105223ed1908SDavid Hildenbrand goto copy_pte; 105323ed1908SDavid Hildenbrand 105414ddee41SMatthew Wilcox (Oracle) folio = page_folio(page); 1055f8d93776SDavid Hildenbrand 1056f8d93776SDavid Hildenbrand /* 1057f8d93776SDavid Hildenbrand * If we likely have to copy, just don't bother with batching. Make 1058f8d93776SDavid Hildenbrand * sure that the common "small folio" case is as fast as possible 1059f8d93776SDavid Hildenbrand * by keeping the batching logic separate. 1060f8d93776SDavid Hildenbrand */ 1061f8d93776SDavid Hildenbrand if (unlikely(!*prealloc && folio_test_large(folio) && max_nr != 1)) { 106225365e10SDavid Hildenbrand if (src_vma->vm_flags & VM_SHARED) 106325365e10SDavid Hildenbrand flags |= FPB_IGNORE_DIRTY; 106425365e10SDavid Hildenbrand if (!vma_soft_dirty_enabled(src_vma)) 106525365e10SDavid Hildenbrand flags |= FPB_IGNORE_SOFT_DIRTY; 106625365e10SDavid Hildenbrand 1067d7c0e5f7SDavid Hildenbrand nr = folio_pte_batch(folio, addr, src_pte, pte, max_nr, flags, 1068d7c0e5f7SDavid Hildenbrand &any_writable); 1069f8d93776SDavid Hildenbrand folio_ref_add(folio, nr); 1070f8d93776SDavid Hildenbrand if (folio_test_anon(folio)) { 1071f8d93776SDavid Hildenbrand if (unlikely(folio_try_dup_anon_rmap_ptes(folio, page, 1072f8d93776SDavid Hildenbrand nr, src_vma))) { 1073f8d93776SDavid Hildenbrand folio_ref_sub(folio, nr); 1074f8d93776SDavid Hildenbrand return -EAGAIN; 1075f8d93776SDavid Hildenbrand } 1076f8d93776SDavid Hildenbrand rss[MM_ANONPAGES] += nr; 1077f8d93776SDavid Hildenbrand VM_WARN_ON_FOLIO(PageAnonExclusive(page), folio); 1078f8d93776SDavid Hildenbrand } else { 1079f8d93776SDavid Hildenbrand folio_dup_file_rmap_ptes(folio, page, nr); 1080f8d93776SDavid Hildenbrand rss[mm_counter_file(folio)] += nr; 1081f8d93776SDavid Hildenbrand } 1082d7c0e5f7SDavid Hildenbrand if (any_writable) 1083d7c0e5f7SDavid Hildenbrand pte = pte_mkwrite(pte, src_vma); 1084f8d93776SDavid Hildenbrand __copy_present_ptes(dst_vma, src_vma, dst_pte, src_pte, pte, 1085f8d93776SDavid Hildenbrand addr, nr); 1086f8d93776SDavid Hildenbrand return nr; 1087f8d93776SDavid Hildenbrand } 1088f8d93776SDavid Hildenbrand 108923ed1908SDavid Hildenbrand folio_get(folio); 109023ed1908SDavid Hildenbrand if (folio_test_anon(folio)) { 1091b51ad4f8SDavid Hildenbrand /* 1092b51ad4f8SDavid Hildenbrand * If this page may have been pinned by the parent process, 1093b51ad4f8SDavid Hildenbrand * copy the page immediately for the child so that we'll always 1094b51ad4f8SDavid Hildenbrand * guarantee the pinned page won't be randomly replaced in the 1095b51ad4f8SDavid Hildenbrand * future. 1096b51ad4f8SDavid Hildenbrand */ 109708e7795eSDavid Hildenbrand if (unlikely(folio_try_dup_anon_rmap_pte(folio, page, src_vma))) { 1098fb3d824dSDavid Hildenbrand /* Page may be pinned, we have to copy. */ 109914ddee41SMatthew Wilcox (Oracle) folio_put(folio); 1100f8d93776SDavid Hildenbrand err = copy_present_page(dst_vma, src_vma, dst_pte, src_pte, 1101b51ad4f8SDavid Hildenbrand addr, rss, prealloc, page); 1102f8d93776SDavid Hildenbrand return err ? err : 1; 1103fb3d824dSDavid Hildenbrand } 1104edf50470SMatthew Wilcox (Oracle) rss[MM_ANONPAGES]++; 110523ed1908SDavid Hildenbrand VM_WARN_ON_FOLIO(PageAnonExclusive(page), folio); 110623ed1908SDavid Hildenbrand } else { 1107d8ef5e31SDavid Hildenbrand folio_dup_file_rmap_pte(folio, page); 11086b27cc6cSKefeng Wang rss[mm_counter_file(folio)]++; 110970e806e4SPeter Xu } 111070e806e4SPeter Xu 111123ed1908SDavid Hildenbrand copy_pte: 1112f8d93776SDavid Hildenbrand __copy_present_ptes(dst_vma, src_vma, dst_pte, src_pte, pte, addr, 1); 1113f8d93776SDavid Hildenbrand return 1; 11146aab341eSLinus Torvalds } 1115ae859762SHugh Dickins 1116294de6d8SKefeng Wang static inline struct folio *folio_prealloc(struct mm_struct *src_mm, 1117294de6d8SKefeng Wang struct vm_area_struct *vma, unsigned long addr, bool need_zero) 111870e806e4SPeter Xu { 1119edf50470SMatthew Wilcox (Oracle) struct folio *new_folio; 112070e806e4SPeter Xu 1121294de6d8SKefeng Wang if (need_zero) 1122294de6d8SKefeng Wang new_folio = vma_alloc_zeroed_movable_folio(vma, addr); 1123294de6d8SKefeng Wang else 1124294de6d8SKefeng Wang new_folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, vma, 1125294de6d8SKefeng Wang addr, false); 1126294de6d8SKefeng Wang 1127edf50470SMatthew Wilcox (Oracle) if (!new_folio) 112870e806e4SPeter Xu return NULL; 112970e806e4SPeter Xu 1130edf50470SMatthew Wilcox (Oracle) if (mem_cgroup_charge(new_folio, src_mm, GFP_KERNEL)) { 1131edf50470SMatthew Wilcox (Oracle) folio_put(new_folio); 113270e806e4SPeter Xu return NULL; 113370e806e4SPeter Xu } 1134e601ded4SKefeng Wang folio_throttle_swaprate(new_folio, GFP_KERNEL); 113570e806e4SPeter Xu 1136edf50470SMatthew Wilcox (Oracle) return new_folio; 11371da177e4SLinus Torvalds } 11381da177e4SLinus Torvalds 1139c78f4636SPeter Xu static int 1140c78f4636SPeter Xu copy_pte_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, 1141c78f4636SPeter Xu pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr, 1142c78f4636SPeter Xu unsigned long end) 11431da177e4SLinus Torvalds { 1144c78f4636SPeter Xu struct mm_struct *dst_mm = dst_vma->vm_mm; 1145c78f4636SPeter Xu struct mm_struct *src_mm = src_vma->vm_mm; 1146c36987e2SDaisuke Nishimura pte_t *orig_src_pte, *orig_dst_pte; 11471da177e4SLinus Torvalds pte_t *src_pte, *dst_pte; 1148c33c7948SRyan Roberts pte_t ptent; 1149c74df32cSHugh Dickins spinlock_t *src_ptl, *dst_ptl; 1150f8d93776SDavid Hildenbrand int progress, max_nr, ret = 0; 1151d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 1152570a335bSHugh Dickins swp_entry_t entry = (swp_entry_t){0}; 1153edf50470SMatthew Wilcox (Oracle) struct folio *prealloc = NULL; 1154f8d93776SDavid Hildenbrand int nr; 11551da177e4SLinus Torvalds 11561da177e4SLinus Torvalds again: 115770e806e4SPeter Xu progress = 0; 1158d559db08SKAMEZAWA Hiroyuki init_rss_vec(rss); 1159d559db08SKAMEZAWA Hiroyuki 11603db82b93SHugh Dickins /* 11613db82b93SHugh Dickins * copy_pmd_range()'s prior pmd_none_or_clear_bad(src_pmd), and the 11623db82b93SHugh Dickins * error handling here, assume that exclusive mmap_lock on dst and src 11633db82b93SHugh Dickins * protects anon from unexpected THP transitions; with shmem and file 11643db82b93SHugh Dickins * protected by mmap_lock-less collapse skipping areas with anon_vma 11653db82b93SHugh Dickins * (whereas vma_needs_copy() skips areas without anon_vma). A rework 11663db82b93SHugh Dickins * can remove such assumptions later, but this is good enough for now. 11673db82b93SHugh Dickins */ 1168c74df32cSHugh Dickins dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl); 116970e806e4SPeter Xu if (!dst_pte) { 117070e806e4SPeter Xu ret = -ENOMEM; 117170e806e4SPeter Xu goto out; 117270e806e4SPeter Xu } 11733db82b93SHugh Dickins src_pte = pte_offset_map_nolock(src_mm, src_pmd, addr, &src_ptl); 11743db82b93SHugh Dickins if (!src_pte) { 11753db82b93SHugh Dickins pte_unmap_unlock(dst_pte, dst_ptl); 11763db82b93SHugh Dickins /* ret == 0 */ 11773db82b93SHugh Dickins goto out; 11783db82b93SHugh Dickins } 1179f20dc5f7SIngo Molnar spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 1180c36987e2SDaisuke Nishimura orig_src_pte = src_pte; 1181c36987e2SDaisuke Nishimura orig_dst_pte = dst_pte; 11826606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 11831da177e4SLinus Torvalds 11841da177e4SLinus Torvalds do { 1185f8d93776SDavid Hildenbrand nr = 1; 1186f8d93776SDavid Hildenbrand 11871da177e4SLinus Torvalds /* 11881da177e4SLinus Torvalds * We are holding two locks at this point - either of them 11891da177e4SLinus Torvalds * could generate latencies in another task on another CPU. 11901da177e4SLinus Torvalds */ 1191e040f218SHugh Dickins if (progress >= 32) { 1192e040f218SHugh Dickins progress = 0; 1193e040f218SHugh Dickins if (need_resched() || 119495c354feSNick Piggin spin_needbreak(src_ptl) || spin_needbreak(dst_ptl)) 11951da177e4SLinus Torvalds break; 1196e040f218SHugh Dickins } 1197c33c7948SRyan Roberts ptent = ptep_get(src_pte); 1198c33c7948SRyan Roberts if (pte_none(ptent)) { 11991da177e4SLinus Torvalds progress++; 12001da177e4SLinus Torvalds continue; 12011da177e4SLinus Torvalds } 1202c33c7948SRyan Roberts if (unlikely(!pte_present(ptent))) { 12039a5cc85cSAlistair Popple ret = copy_nonpresent_pte(dst_mm, src_mm, 120479a1971cSLinus Torvalds dst_pte, src_pte, 12058f34f1eaSPeter Xu dst_vma, src_vma, 12068f34f1eaSPeter Xu addr, rss); 12079a5cc85cSAlistair Popple if (ret == -EIO) { 1208c33c7948SRyan Roberts entry = pte_to_swp_entry(ptep_get(src_pte)); 1209570a335bSHugh Dickins break; 1210b756a3b5SAlistair Popple } else if (ret == -EBUSY) { 1211b756a3b5SAlistair Popple break; 1212b756a3b5SAlistair Popple } else if (!ret) { 12131da177e4SLinus Torvalds progress += 8; 121479a1971cSLinus Torvalds continue; 121579a1971cSLinus Torvalds } 121653723298SDavid Hildenbrand ptent = ptep_get(src_pte); 121753723298SDavid Hildenbrand VM_WARN_ON_ONCE(!pte_present(ptent)); 1218b756a3b5SAlistair Popple 1219b756a3b5SAlistair Popple /* 1220b756a3b5SAlistair Popple * Device exclusive entry restored, continue by copying 1221b756a3b5SAlistair Popple * the now present pte. 1222b756a3b5SAlistair Popple */ 1223b756a3b5SAlistair Popple WARN_ON_ONCE(ret != -ENOENT); 1224b756a3b5SAlistair Popple } 1225f8d93776SDavid Hildenbrand /* copy_present_ptes() will clear `*prealloc' if consumed */ 1226f8d93776SDavid Hildenbrand max_nr = (end - addr) / PAGE_SIZE; 1227f8d93776SDavid Hildenbrand ret = copy_present_ptes(dst_vma, src_vma, dst_pte, src_pte, 1228f8d93776SDavid Hildenbrand ptent, addr, max_nr, rss, &prealloc); 122970e806e4SPeter Xu /* 123070e806e4SPeter Xu * If we need a pre-allocated page for this pte, drop the 123170e806e4SPeter Xu * locks, allocate, and try again. 123270e806e4SPeter Xu */ 123370e806e4SPeter Xu if (unlikely(ret == -EAGAIN)) 123470e806e4SPeter Xu break; 123570e806e4SPeter Xu if (unlikely(prealloc)) { 123670e806e4SPeter Xu /* 123770e806e4SPeter Xu * pre-alloc page cannot be reused by next time so as 123870e806e4SPeter Xu * to strictly follow mempolicy (e.g., alloc_page_vma() 123970e806e4SPeter Xu * will allocate page according to address). This 124070e806e4SPeter Xu * could only happen if one pinned pte changed. 124170e806e4SPeter Xu */ 1242edf50470SMatthew Wilcox (Oracle) folio_put(prealloc); 124370e806e4SPeter Xu prealloc = NULL; 124470e806e4SPeter Xu } 1245f8d93776SDavid Hildenbrand nr = ret; 1246f8d93776SDavid Hildenbrand progress += 8 * nr; 1247f8d93776SDavid Hildenbrand } while (dst_pte += nr, src_pte += nr, addr += PAGE_SIZE * nr, 1248f8d93776SDavid Hildenbrand addr != end); 12491da177e4SLinus Torvalds 12506606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 12513db82b93SHugh Dickins pte_unmap_unlock(orig_src_pte, src_ptl); 1252d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(dst_mm, rss); 1253c36987e2SDaisuke Nishimura pte_unmap_unlock(orig_dst_pte, dst_ptl); 1254c74df32cSHugh Dickins cond_resched(); 1255570a335bSHugh Dickins 12569a5cc85cSAlistair Popple if (ret == -EIO) { 12579a5cc85cSAlistair Popple VM_WARN_ON_ONCE(!entry.val); 125870e806e4SPeter Xu if (add_swap_count_continuation(entry, GFP_KERNEL) < 0) { 125970e806e4SPeter Xu ret = -ENOMEM; 126070e806e4SPeter Xu goto out; 126170e806e4SPeter Xu } 126270e806e4SPeter Xu entry.val = 0; 1263b756a3b5SAlistair Popple } else if (ret == -EBUSY) { 1264b756a3b5SAlistair Popple goto out; 12659a5cc85cSAlistair Popple } else if (ret == -EAGAIN) { 1266294de6d8SKefeng Wang prealloc = folio_prealloc(src_mm, src_vma, addr, false); 126770e806e4SPeter Xu if (!prealloc) 1268570a335bSHugh Dickins return -ENOMEM; 1269f8d93776SDavid Hildenbrand } else if (ret < 0) { 12709a5cc85cSAlistair Popple VM_WARN_ON_ONCE(1); 12719a5cc85cSAlistair Popple } 12729a5cc85cSAlistair Popple 127370e806e4SPeter Xu /* We've captured and resolved the error. Reset, try again. */ 127470e806e4SPeter Xu ret = 0; 12759a5cc85cSAlistair Popple 12761da177e4SLinus Torvalds if (addr != end) 12771da177e4SLinus Torvalds goto again; 127870e806e4SPeter Xu out: 127970e806e4SPeter Xu if (unlikely(prealloc)) 1280edf50470SMatthew Wilcox (Oracle) folio_put(prealloc); 128170e806e4SPeter Xu return ret; 12821da177e4SLinus Torvalds } 12831da177e4SLinus Torvalds 1284c78f4636SPeter Xu static inline int 1285c78f4636SPeter Xu copy_pmd_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, 1286c78f4636SPeter Xu pud_t *dst_pud, pud_t *src_pud, unsigned long addr, 1287c78f4636SPeter Xu unsigned long end) 12881da177e4SLinus Torvalds { 1289c78f4636SPeter Xu struct mm_struct *dst_mm = dst_vma->vm_mm; 1290c78f4636SPeter Xu struct mm_struct *src_mm = src_vma->vm_mm; 12911da177e4SLinus Torvalds pmd_t *src_pmd, *dst_pmd; 12921da177e4SLinus Torvalds unsigned long next; 12931da177e4SLinus Torvalds 12941da177e4SLinus Torvalds dst_pmd = pmd_alloc(dst_mm, dst_pud, addr); 12951da177e4SLinus Torvalds if (!dst_pmd) 12961da177e4SLinus Torvalds return -ENOMEM; 12971da177e4SLinus Torvalds src_pmd = pmd_offset(src_pud, addr); 12981da177e4SLinus Torvalds do { 12991da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 130084c3fc4eSZi Yan if (is_swap_pmd(*src_pmd) || pmd_trans_huge(*src_pmd) 130184c3fc4eSZi Yan || pmd_devmap(*src_pmd)) { 130271e3aac0SAndrea Arcangeli int err; 1303c78f4636SPeter Xu VM_BUG_ON_VMA(next-addr != HPAGE_PMD_SIZE, src_vma); 13048f34f1eaSPeter Xu err = copy_huge_pmd(dst_mm, src_mm, dst_pmd, src_pmd, 13058f34f1eaSPeter Xu addr, dst_vma, src_vma); 130671e3aac0SAndrea Arcangeli if (err == -ENOMEM) 130771e3aac0SAndrea Arcangeli return -ENOMEM; 130871e3aac0SAndrea Arcangeli if (!err) 130971e3aac0SAndrea Arcangeli continue; 131071e3aac0SAndrea Arcangeli /* fall through */ 131171e3aac0SAndrea Arcangeli } 13121da177e4SLinus Torvalds if (pmd_none_or_clear_bad(src_pmd)) 13131da177e4SLinus Torvalds continue; 1314c78f4636SPeter Xu if (copy_pte_range(dst_vma, src_vma, dst_pmd, src_pmd, 1315c78f4636SPeter Xu addr, next)) 13161da177e4SLinus Torvalds return -ENOMEM; 13171da177e4SLinus Torvalds } while (dst_pmd++, src_pmd++, addr = next, addr != end); 13181da177e4SLinus Torvalds return 0; 13191da177e4SLinus Torvalds } 13201da177e4SLinus Torvalds 1321c78f4636SPeter Xu static inline int 1322c78f4636SPeter Xu copy_pud_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, 1323c78f4636SPeter Xu p4d_t *dst_p4d, p4d_t *src_p4d, unsigned long addr, 1324c78f4636SPeter Xu unsigned long end) 13251da177e4SLinus Torvalds { 1326c78f4636SPeter Xu struct mm_struct *dst_mm = dst_vma->vm_mm; 1327c78f4636SPeter Xu struct mm_struct *src_mm = src_vma->vm_mm; 13281da177e4SLinus Torvalds pud_t *src_pud, *dst_pud; 13291da177e4SLinus Torvalds unsigned long next; 13301da177e4SLinus Torvalds 1331c2febafcSKirill A. Shutemov dst_pud = pud_alloc(dst_mm, dst_p4d, addr); 13321da177e4SLinus Torvalds if (!dst_pud) 13331da177e4SLinus Torvalds return -ENOMEM; 1334c2febafcSKirill A. Shutemov src_pud = pud_offset(src_p4d, addr); 13351da177e4SLinus Torvalds do { 13361da177e4SLinus Torvalds next = pud_addr_end(addr, end); 1337a00cc7d9SMatthew Wilcox if (pud_trans_huge(*src_pud) || pud_devmap(*src_pud)) { 1338a00cc7d9SMatthew Wilcox int err; 1339a00cc7d9SMatthew Wilcox 1340c78f4636SPeter Xu VM_BUG_ON_VMA(next-addr != HPAGE_PUD_SIZE, src_vma); 1341a00cc7d9SMatthew Wilcox err = copy_huge_pud(dst_mm, src_mm, 1342c78f4636SPeter Xu dst_pud, src_pud, addr, src_vma); 1343a00cc7d9SMatthew Wilcox if (err == -ENOMEM) 1344a00cc7d9SMatthew Wilcox return -ENOMEM; 1345a00cc7d9SMatthew Wilcox if (!err) 1346a00cc7d9SMatthew Wilcox continue; 1347a00cc7d9SMatthew Wilcox /* fall through */ 1348a00cc7d9SMatthew Wilcox } 13491da177e4SLinus Torvalds if (pud_none_or_clear_bad(src_pud)) 13501da177e4SLinus Torvalds continue; 1351c78f4636SPeter Xu if (copy_pmd_range(dst_vma, src_vma, dst_pud, src_pud, 1352c78f4636SPeter Xu addr, next)) 13531da177e4SLinus Torvalds return -ENOMEM; 13541da177e4SLinus Torvalds } while (dst_pud++, src_pud++, addr = next, addr != end); 13551da177e4SLinus Torvalds return 0; 13561da177e4SLinus Torvalds } 13571da177e4SLinus Torvalds 1358c78f4636SPeter Xu static inline int 1359c78f4636SPeter Xu copy_p4d_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, 1360c78f4636SPeter Xu pgd_t *dst_pgd, pgd_t *src_pgd, unsigned long addr, 1361c78f4636SPeter Xu unsigned long end) 1362c2febafcSKirill A. Shutemov { 1363c78f4636SPeter Xu struct mm_struct *dst_mm = dst_vma->vm_mm; 1364c2febafcSKirill A. Shutemov p4d_t *src_p4d, *dst_p4d; 1365c2febafcSKirill A. Shutemov unsigned long next; 1366c2febafcSKirill A. Shutemov 1367c2febafcSKirill A. Shutemov dst_p4d = p4d_alloc(dst_mm, dst_pgd, addr); 1368c2febafcSKirill A. Shutemov if (!dst_p4d) 1369c2febafcSKirill A. Shutemov return -ENOMEM; 1370c2febafcSKirill A. Shutemov src_p4d = p4d_offset(src_pgd, addr); 1371c2febafcSKirill A. Shutemov do { 1372c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 1373c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(src_p4d)) 1374c2febafcSKirill A. Shutemov continue; 1375c78f4636SPeter Xu if (copy_pud_range(dst_vma, src_vma, dst_p4d, src_p4d, 1376c78f4636SPeter Xu addr, next)) 1377c2febafcSKirill A. Shutemov return -ENOMEM; 1378c2febafcSKirill A. Shutemov } while (dst_p4d++, src_p4d++, addr = next, addr != end); 1379c2febafcSKirill A. Shutemov return 0; 1380c2febafcSKirill A. Shutemov } 1381c2febafcSKirill A. Shutemov 1382c56d1b62SPeter Xu /* 1383c56d1b62SPeter Xu * Return true if the vma needs to copy the pgtable during this fork(). Return 1384c56d1b62SPeter Xu * false when we can speed up fork() by allowing lazy page faults later until 1385c56d1b62SPeter Xu * when the child accesses the memory range. 1386c56d1b62SPeter Xu */ 1387bc70fbf2SPeter Xu static bool 1388c56d1b62SPeter Xu vma_needs_copy(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) 1389c56d1b62SPeter Xu { 1390c56d1b62SPeter Xu /* 1391c56d1b62SPeter Xu * Always copy pgtables when dst_vma has uffd-wp enabled even if it's 1392c56d1b62SPeter Xu * file-backed (e.g. shmem). Because when uffd-wp is enabled, pgtable 1393c56d1b62SPeter Xu * contains uffd-wp protection information, that's something we can't 1394c56d1b62SPeter Xu * retrieve from page cache, and skip copying will lose those info. 1395c56d1b62SPeter Xu */ 1396c56d1b62SPeter Xu if (userfaultfd_wp(dst_vma)) 1397c56d1b62SPeter Xu return true; 1398c56d1b62SPeter Xu 1399bcd51a3cSMike Kravetz if (src_vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)) 1400c56d1b62SPeter Xu return true; 1401c56d1b62SPeter Xu 1402c56d1b62SPeter Xu if (src_vma->anon_vma) 1403c56d1b62SPeter Xu return true; 1404c56d1b62SPeter Xu 1405c56d1b62SPeter Xu /* 1406c56d1b62SPeter Xu * Don't copy ptes where a page fault will fill them correctly. Fork 1407c56d1b62SPeter Xu * becomes much lighter when there are big shared or private readonly 1408c56d1b62SPeter Xu * mappings. The tradeoff is that copy_page_range is more efficient 1409c56d1b62SPeter Xu * than faulting. 1410c56d1b62SPeter Xu */ 1411c56d1b62SPeter Xu return false; 1412c56d1b62SPeter Xu } 1413c56d1b62SPeter Xu 1414c78f4636SPeter Xu int 1415c78f4636SPeter Xu copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) 14161da177e4SLinus Torvalds { 14171da177e4SLinus Torvalds pgd_t *src_pgd, *dst_pgd; 14181da177e4SLinus Torvalds unsigned long next; 1419c78f4636SPeter Xu unsigned long addr = src_vma->vm_start; 1420c78f4636SPeter Xu unsigned long end = src_vma->vm_end; 1421c78f4636SPeter Xu struct mm_struct *dst_mm = dst_vma->vm_mm; 1422c78f4636SPeter Xu struct mm_struct *src_mm = src_vma->vm_mm; 1423ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 14242ec74c3eSSagi Grimberg bool is_cow; 1425cddb8a5cSAndrea Arcangeli int ret; 14261da177e4SLinus Torvalds 1427c56d1b62SPeter Xu if (!vma_needs_copy(dst_vma, src_vma)) 1428d992895bSNick Piggin return 0; 1429d992895bSNick Piggin 1430c78f4636SPeter Xu if (is_vm_hugetlb_page(src_vma)) 1431bc70fbf2SPeter Xu return copy_hugetlb_page_range(dst_mm, src_mm, dst_vma, src_vma); 14321da177e4SLinus Torvalds 1433c78f4636SPeter Xu if (unlikely(src_vma->vm_flags & VM_PFNMAP)) { 14342ab64037Svenkatesh.pallipadi@intel.com /* 14352ab64037Svenkatesh.pallipadi@intel.com * We do not free on error cases below as remove_vma 14362ab64037Svenkatesh.pallipadi@intel.com * gets called on error from higher level routine 14372ab64037Svenkatesh.pallipadi@intel.com */ 1438c78f4636SPeter Xu ret = track_pfn_copy(src_vma); 14392ab64037Svenkatesh.pallipadi@intel.com if (ret) 14402ab64037Svenkatesh.pallipadi@intel.com return ret; 14412ab64037Svenkatesh.pallipadi@intel.com } 14422ab64037Svenkatesh.pallipadi@intel.com 1443cddb8a5cSAndrea Arcangeli /* 1444cddb8a5cSAndrea Arcangeli * We need to invalidate the secondary MMU mappings only when 1445cddb8a5cSAndrea Arcangeli * there could be a permission downgrade on the ptes of the 1446cddb8a5cSAndrea Arcangeli * parent mm. And a permission downgrade will only happen if 1447cddb8a5cSAndrea Arcangeli * is_cow_mapping() returns true. 1448cddb8a5cSAndrea Arcangeli */ 1449c78f4636SPeter Xu is_cow = is_cow_mapping(src_vma->vm_flags); 1450ac46d4f3SJérôme Glisse 1451ac46d4f3SJérôme Glisse if (is_cow) { 14527269f999SJérôme Glisse mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_PAGE, 14537d4a8be0SAlistair Popple 0, src_mm, addr, end); 1454ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 145557efa1feSJason Gunthorpe /* 145657efa1feSJason Gunthorpe * Disabling preemption is not needed for the write side, as 145757efa1feSJason Gunthorpe * the read side doesn't spin, but goes to the mmap_lock. 145857efa1feSJason Gunthorpe * 145957efa1feSJason Gunthorpe * Use the raw variant of the seqcount_t write API to avoid 146057efa1feSJason Gunthorpe * lockdep complaining about preemptibility. 146157efa1feSJason Gunthorpe */ 1462e727bfd5SSuren Baghdasaryan vma_assert_write_locked(src_vma); 146357efa1feSJason Gunthorpe raw_write_seqcount_begin(&src_mm->write_protect_seq); 1464ac46d4f3SJérôme Glisse } 1465cddb8a5cSAndrea Arcangeli 1466cddb8a5cSAndrea Arcangeli ret = 0; 14671da177e4SLinus Torvalds dst_pgd = pgd_offset(dst_mm, addr); 14681da177e4SLinus Torvalds src_pgd = pgd_offset(src_mm, addr); 14691da177e4SLinus Torvalds do { 14701da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 14711da177e4SLinus Torvalds if (pgd_none_or_clear_bad(src_pgd)) 14721da177e4SLinus Torvalds continue; 1473c78f4636SPeter Xu if (unlikely(copy_p4d_range(dst_vma, src_vma, dst_pgd, src_pgd, 1474c78f4636SPeter Xu addr, next))) { 1475d155df53SMa Wupeng untrack_pfn_clear(dst_vma); 1476cddb8a5cSAndrea Arcangeli ret = -ENOMEM; 1477cddb8a5cSAndrea Arcangeli break; 1478cddb8a5cSAndrea Arcangeli } 14791da177e4SLinus Torvalds } while (dst_pgd++, src_pgd++, addr = next, addr != end); 1480cddb8a5cSAndrea Arcangeli 148157efa1feSJason Gunthorpe if (is_cow) { 148257efa1feSJason Gunthorpe raw_write_seqcount_end(&src_mm->write_protect_seq); 1483ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 148457efa1feSJason Gunthorpe } 1485cddb8a5cSAndrea Arcangeli return ret; 14861da177e4SLinus Torvalds } 14871da177e4SLinus Torvalds 14885abfd71dSPeter Xu /* Whether we should zap all COWed (private) pages too */ 14895abfd71dSPeter Xu static inline bool should_zap_cows(struct zap_details *details) 14905abfd71dSPeter Xu { 14915abfd71dSPeter Xu /* By default, zap all pages */ 14925abfd71dSPeter Xu if (!details) 14935abfd71dSPeter Xu return true; 14945abfd71dSPeter Xu 14955abfd71dSPeter Xu /* Or, we zap COWed pages only if the caller wants to */ 14962e148f1eSPeter Xu return details->even_cows; 14975abfd71dSPeter Xu } 14985abfd71dSPeter Xu 1499eabafaaaSKefeng Wang /* Decides whether we should zap this folio with the folio pointer specified */ 1500eabafaaaSKefeng Wang static inline bool should_zap_folio(struct zap_details *details, 1501eabafaaaSKefeng Wang struct folio *folio) 15023506659eSMatthew Wilcox (Oracle) { 1503eabafaaaSKefeng Wang /* If we can make a decision without *folio.. */ 15045abfd71dSPeter Xu if (should_zap_cows(details)) 1505254ab940SPeter Xu return true; 15063506659eSMatthew Wilcox (Oracle) 1507eabafaaaSKefeng Wang /* Otherwise we should only zap non-anon folios */ 1508eabafaaaSKefeng Wang return !folio_test_anon(folio); 15093506659eSMatthew Wilcox (Oracle) } 15103506659eSMatthew Wilcox (Oracle) 1511999dad82SPeter Xu static inline bool zap_drop_file_uffd_wp(struct zap_details *details) 1512999dad82SPeter Xu { 1513999dad82SPeter Xu if (!details) 1514999dad82SPeter Xu return false; 1515999dad82SPeter Xu 1516999dad82SPeter Xu return details->zap_flags & ZAP_FLAG_DROP_MARKER; 1517999dad82SPeter Xu } 1518999dad82SPeter Xu 1519999dad82SPeter Xu /* 1520999dad82SPeter Xu * This function makes sure that we'll replace the none pte with an uffd-wp 1521999dad82SPeter Xu * swap special pte marker when necessary. Must be with the pgtable lock held. 1522999dad82SPeter Xu */ 1523999dad82SPeter Xu static inline void 1524999dad82SPeter Xu zap_install_uffd_wp_if_needed(struct vm_area_struct *vma, 152510ebac4fSDavid Hildenbrand unsigned long addr, pte_t *pte, int nr, 1526999dad82SPeter Xu struct zap_details *details, pte_t pteval) 1527999dad82SPeter Xu { 15282bad466cSPeter Xu /* Zap on anonymous always means dropping everything */ 15292bad466cSPeter Xu if (vma_is_anonymous(vma)) 15302bad466cSPeter Xu return; 15312bad466cSPeter Xu 1532999dad82SPeter Xu if (zap_drop_file_uffd_wp(details)) 1533999dad82SPeter Xu return; 1534999dad82SPeter Xu 153510ebac4fSDavid Hildenbrand for (;;) { 153610ebac4fSDavid Hildenbrand /* the PFN in the PTE is irrelevant. */ 1537999dad82SPeter Xu pte_install_uffd_wp_if_needed(vma, addr, pte, pteval); 153810ebac4fSDavid Hildenbrand if (--nr == 0) 153910ebac4fSDavid Hildenbrand break; 154010ebac4fSDavid Hildenbrand pte++; 154110ebac4fSDavid Hildenbrand addr += PAGE_SIZE; 154210ebac4fSDavid Hildenbrand } 1543999dad82SPeter Xu } 1544999dad82SPeter Xu 154510ebac4fSDavid Hildenbrand static __always_inline void zap_present_folio_ptes(struct mmu_gather *tlb, 15462b42a7e5SDavid Hildenbrand struct vm_area_struct *vma, struct folio *folio, 154710ebac4fSDavid Hildenbrand struct page *page, pte_t *pte, pte_t ptent, unsigned int nr, 154810ebac4fSDavid Hildenbrand unsigned long addr, struct zap_details *details, int *rss, 154910ebac4fSDavid Hildenbrand bool *force_flush, bool *force_break) 1550789753e1SDavid Hildenbrand { 1551789753e1SDavid Hildenbrand struct mm_struct *mm = tlb->mm; 1552789753e1SDavid Hildenbrand bool delay_rmap = false; 1553789753e1SDavid Hildenbrand 1554789753e1SDavid Hildenbrand if (!folio_test_anon(folio)) { 155510ebac4fSDavid Hildenbrand ptent = get_and_clear_full_ptes(mm, addr, pte, nr, tlb->fullmm); 1556789753e1SDavid Hildenbrand if (pte_dirty(ptent)) { 1557789753e1SDavid Hildenbrand folio_mark_dirty(folio); 1558789753e1SDavid Hildenbrand if (tlb_delay_rmap(tlb)) { 1559789753e1SDavid Hildenbrand delay_rmap = true; 1560789753e1SDavid Hildenbrand *force_flush = true; 1561789753e1SDavid Hildenbrand } 1562789753e1SDavid Hildenbrand } 1563789753e1SDavid Hildenbrand if (pte_young(ptent) && likely(vma_has_recency(vma))) 1564789753e1SDavid Hildenbrand folio_mark_accessed(folio); 156510ebac4fSDavid Hildenbrand rss[mm_counter(folio)] -= nr; 1566d11838edSDavid Hildenbrand } else { 1567d11838edSDavid Hildenbrand /* We don't need up-to-date accessed/dirty bits. */ 156810ebac4fSDavid Hildenbrand clear_full_ptes(mm, addr, pte, nr, tlb->fullmm); 156910ebac4fSDavid Hildenbrand rss[MM_ANONPAGES] -= nr; 1570d11838edSDavid Hildenbrand } 157110ebac4fSDavid Hildenbrand /* Checking a single PTE in a batch is sufficient. */ 1572d11838edSDavid Hildenbrand arch_check_zapped_pte(vma, ptent); 157310ebac4fSDavid Hildenbrand tlb_remove_tlb_entries(tlb, pte, nr, addr); 1574d11838edSDavid Hildenbrand if (unlikely(userfaultfd_pte_wp(vma, ptent))) 157510ebac4fSDavid Hildenbrand zap_install_uffd_wp_if_needed(vma, addr, pte, nr, details, 157610ebac4fSDavid Hildenbrand ptent); 1577d11838edSDavid Hildenbrand 1578789753e1SDavid Hildenbrand if (!delay_rmap) { 157910ebac4fSDavid Hildenbrand folio_remove_rmap_ptes(folio, page, nr, vma); 158010ebac4fSDavid Hildenbrand 158110ebac4fSDavid Hildenbrand /* Only sanity-check the first page in a batch. */ 1582789753e1SDavid Hildenbrand if (unlikely(page_mapcount(page) < 0)) 1583789753e1SDavid Hildenbrand print_bad_pte(vma, addr, ptent, page); 1584789753e1SDavid Hildenbrand } 158510ebac4fSDavid Hildenbrand if (unlikely(__tlb_remove_folio_pages(tlb, page, nr, delay_rmap))) { 1586789753e1SDavid Hildenbrand *force_flush = true; 1587789753e1SDavid Hildenbrand *force_break = true; 1588789753e1SDavid Hildenbrand } 1589789753e1SDavid Hildenbrand } 1590789753e1SDavid Hildenbrand 159110ebac4fSDavid Hildenbrand /* 159210ebac4fSDavid Hildenbrand * Zap or skip at least one present PTE, trying to batch-process subsequent 159310ebac4fSDavid Hildenbrand * PTEs that map consecutive pages of the same folio. 159410ebac4fSDavid Hildenbrand * 159510ebac4fSDavid Hildenbrand * Returns the number of processed (skipped or zapped) PTEs (at least 1). 159610ebac4fSDavid Hildenbrand */ 159710ebac4fSDavid Hildenbrand static inline int zap_present_ptes(struct mmu_gather *tlb, 15982b42a7e5SDavid Hildenbrand struct vm_area_struct *vma, pte_t *pte, pte_t ptent, 159910ebac4fSDavid Hildenbrand unsigned int max_nr, unsigned long addr, 160010ebac4fSDavid Hildenbrand struct zap_details *details, int *rss, bool *force_flush, 160110ebac4fSDavid Hildenbrand bool *force_break) 16022b42a7e5SDavid Hildenbrand { 160310ebac4fSDavid Hildenbrand const fpb_t fpb_flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY; 16042b42a7e5SDavid Hildenbrand struct mm_struct *mm = tlb->mm; 16052b42a7e5SDavid Hildenbrand struct folio *folio; 16062b42a7e5SDavid Hildenbrand struct page *page; 160710ebac4fSDavid Hildenbrand int nr; 16082b42a7e5SDavid Hildenbrand 16092b42a7e5SDavid Hildenbrand page = vm_normal_page(vma, addr, ptent); 16102b42a7e5SDavid Hildenbrand if (!page) { 16112b42a7e5SDavid Hildenbrand /* We don't need up-to-date accessed/dirty bits. */ 16122b42a7e5SDavid Hildenbrand ptep_get_and_clear_full(mm, addr, pte, tlb->fullmm); 16132b42a7e5SDavid Hildenbrand arch_check_zapped_pte(vma, ptent); 16142b42a7e5SDavid Hildenbrand tlb_remove_tlb_entry(tlb, pte, addr); 16152b42a7e5SDavid Hildenbrand VM_WARN_ON_ONCE(userfaultfd_wp(vma)); 16162b42a7e5SDavid Hildenbrand ksm_might_unmap_zero_page(mm, ptent); 161710ebac4fSDavid Hildenbrand return 1; 16182b42a7e5SDavid Hildenbrand } 16192b42a7e5SDavid Hildenbrand 16202b42a7e5SDavid Hildenbrand folio = page_folio(page); 16212b42a7e5SDavid Hildenbrand if (unlikely(!should_zap_folio(details, folio))) 162210ebac4fSDavid Hildenbrand return 1; 162310ebac4fSDavid Hildenbrand 162410ebac4fSDavid Hildenbrand /* 162510ebac4fSDavid Hildenbrand * Make sure that the common "small folio" case is as fast as possible 162610ebac4fSDavid Hildenbrand * by keeping the batching logic separate. 162710ebac4fSDavid Hildenbrand */ 162810ebac4fSDavid Hildenbrand if (unlikely(folio_test_large(folio) && max_nr != 1)) { 162910ebac4fSDavid Hildenbrand nr = folio_pte_batch(folio, addr, pte, ptent, max_nr, fpb_flags, 163010ebac4fSDavid Hildenbrand NULL); 163110ebac4fSDavid Hildenbrand 163210ebac4fSDavid Hildenbrand zap_present_folio_ptes(tlb, vma, folio, page, pte, ptent, nr, 163310ebac4fSDavid Hildenbrand addr, details, rss, force_flush, 163410ebac4fSDavid Hildenbrand force_break); 163510ebac4fSDavid Hildenbrand return nr; 163610ebac4fSDavid Hildenbrand } 163710ebac4fSDavid Hildenbrand zap_present_folio_ptes(tlb, vma, folio, page, pte, ptent, 1, addr, 163810ebac4fSDavid Hildenbrand details, rss, force_flush, force_break); 163910ebac4fSDavid Hildenbrand return 1; 16402b42a7e5SDavid Hildenbrand } 16412b42a7e5SDavid Hildenbrand 164251c6f666SRobin Holt static unsigned long zap_pte_range(struct mmu_gather *tlb, 1643b5810039SNick Piggin struct vm_area_struct *vma, pmd_t *pmd, 16441da177e4SLinus Torvalds unsigned long addr, unsigned long end, 164597a89413SPeter Zijlstra struct zap_details *details) 16461da177e4SLinus Torvalds { 1647789753e1SDavid Hildenbrand bool force_flush = false, force_break = false; 1648b5810039SNick Piggin struct mm_struct *mm = tlb->mm; 1649d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 165097a89413SPeter Zijlstra spinlock_t *ptl; 16515f1a1907SSteven Rostedt pte_t *start_pte; 165297a89413SPeter Zijlstra pte_t *pte; 16538a5f14a2SKirill A. Shutemov swp_entry_t entry; 165410ebac4fSDavid Hildenbrand int nr; 1655d559db08SKAMEZAWA Hiroyuki 1656ed6a7935SPeter Zijlstra tlb_change_page_size(tlb, PAGE_SIZE); 1657e303297eSPeter Zijlstra init_rss_vec(rss); 16583db82b93SHugh Dickins start_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl); 16593db82b93SHugh Dickins if (!pte) 16603db82b93SHugh Dickins return addr; 16613db82b93SHugh Dickins 16623ea27719SMel Gorman flush_tlb_batched_pending(mm); 16636606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 16641da177e4SLinus Torvalds do { 1665c33c7948SRyan Roberts pte_t ptent = ptep_get(pte); 1666789753e1SDavid Hildenbrand struct folio *folio; 16678018db85SPeter Xu struct page *page; 166810ebac4fSDavid Hildenbrand int max_nr; 16698018db85SPeter Xu 167010ebac4fSDavid Hildenbrand nr = 1; 1671166f61b9STobin C Harding if (pte_none(ptent)) 16721da177e4SLinus Torvalds continue; 167351c6f666SRobin Holt 16747b167b68SMinchan Kim if (need_resched()) 16757b167b68SMinchan Kim break; 16767b167b68SMinchan Kim 16776f5e6b9eSHugh Dickins if (pte_present(ptent)) { 167810ebac4fSDavid Hildenbrand max_nr = (end - addr) / PAGE_SIZE; 167910ebac4fSDavid Hildenbrand nr = zap_present_ptes(tlb, vma, pte, ptent, max_nr, 168010ebac4fSDavid Hildenbrand addr, details, rss, &force_flush, 168110ebac4fSDavid Hildenbrand &force_break); 1682789753e1SDavid Hildenbrand if (unlikely(force_break)) { 168310ebac4fSDavid Hildenbrand addr += nr * PAGE_SIZE; 1684d16dfc55SPeter Zijlstra break; 16851cf35d47SLinus Torvalds } 16861da177e4SLinus Torvalds continue; 16871da177e4SLinus Torvalds } 16885042db43SJérôme Glisse 16895042db43SJérôme Glisse entry = pte_to_swp_entry(ptent); 1690b756a3b5SAlistair Popple if (is_device_private_entry(entry) || 1691b756a3b5SAlistair Popple is_device_exclusive_entry(entry)) { 16928018db85SPeter Xu page = pfn_swap_entry_to_page(entry); 1693c4626503SDavid Hildenbrand folio = page_folio(page); 1694eabafaaaSKefeng Wang if (unlikely(!should_zap_folio(details, folio))) 16955042db43SJérôme Glisse continue; 1696999dad82SPeter Xu /* 1697999dad82SPeter Xu * Both device private/exclusive mappings should only 1698999dad82SPeter Xu * work with anonymous page so far, so we don't need to 1699999dad82SPeter Xu * consider uffd-wp bit when zap. For more information, 1700999dad82SPeter Xu * see zap_install_uffd_wp_if_needed(). 1701999dad82SPeter Xu */ 1702999dad82SPeter Xu WARN_ON_ONCE(!vma_is_anonymous(vma)); 1703a23f517bSKefeng Wang rss[mm_counter(folio)]--; 1704b756a3b5SAlistair Popple if (is_device_private_entry(entry)) 1705c4626503SDavid Hildenbrand folio_remove_rmap_pte(folio, page, vma); 1706c4626503SDavid Hildenbrand folio_put(folio); 17078018db85SPeter Xu } else if (!non_swap_entry(entry)) { 17085abfd71dSPeter Xu /* Genuine swap entry, hence a private anon page */ 17095abfd71dSPeter Xu if (!should_zap_cows(details)) 17101da177e4SLinus Torvalds continue; 1711b084d435SKAMEZAWA Hiroyuki rss[MM_SWAPENTS]--; 17128018db85SPeter Xu if (unlikely(!free_swap_and_cache(entry))) 17138018db85SPeter Xu print_bad_pte(vma, addr, ptent, NULL); 17145abfd71dSPeter Xu } else if (is_migration_entry(entry)) { 1715eabafaaaSKefeng Wang folio = pfn_swap_entry_folio(entry); 1716eabafaaaSKefeng Wang if (!should_zap_folio(details, folio)) 17175abfd71dSPeter Xu continue; 1718a23f517bSKefeng Wang rss[mm_counter(folio)]--; 1719999dad82SPeter Xu } else if (pte_marker_entry_uffd_wp(entry)) { 17202bad466cSPeter Xu /* 17212bad466cSPeter Xu * For anon: always drop the marker; for file: only 17222bad466cSPeter Xu * drop the marker if explicitly requested. 17232bad466cSPeter Xu */ 17242bad466cSPeter Xu if (!vma_is_anonymous(vma) && 17252bad466cSPeter Xu !zap_drop_file_uffd_wp(details)) 1726999dad82SPeter Xu continue; 17279f186f9eSMiaohe Lin } else if (is_hwpoison_entry(entry) || 1728af19487fSAxel Rasmussen is_poisoned_swp_entry(entry)) { 17295abfd71dSPeter Xu if (!should_zap_cows(details)) 17305abfd71dSPeter Xu continue; 17315abfd71dSPeter Xu } else { 17325abfd71dSPeter Xu /* We should have covered all the swap entry types */ 1733727d16f1SAndrew Morton pr_alert("unrecognized swap entry 0x%lx\n", entry.val); 17345abfd71dSPeter Xu WARN_ON_ONCE(1); 17359f9f1acdSKonstantin Khlebnikov } 17369888a1caSZachary Amsden pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); 173710ebac4fSDavid Hildenbrand zap_install_uffd_wp_if_needed(vma, addr, pte, 1, details, ptent); 173810ebac4fSDavid Hildenbrand } while (pte += nr, addr += PAGE_SIZE * nr, addr != end); 1739ae859762SHugh Dickins 1740d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(mm, rss); 17416606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 174251c6f666SRobin Holt 17431cf35d47SLinus Torvalds /* Do the actual TLB flush before dropping ptl */ 17445df397deSLinus Torvalds if (force_flush) { 17451cf35d47SLinus Torvalds tlb_flush_mmu_tlbonly(tlb); 17465df397deSLinus Torvalds tlb_flush_rmaps(tlb, vma); 17475df397deSLinus Torvalds } 17481cf35d47SLinus Torvalds pte_unmap_unlock(start_pte, ptl); 17491cf35d47SLinus Torvalds 17501cf35d47SLinus Torvalds /* 17511cf35d47SLinus Torvalds * If we forced a TLB flush (either due to running out of 17521cf35d47SLinus Torvalds * batch buffers or because we needed to flush dirty TLB 17531cf35d47SLinus Torvalds * entries before releasing the ptl), free the batched 17543db82b93SHugh Dickins * memory too. Come back again if we didn't do everything. 17551cf35d47SLinus Torvalds */ 17563db82b93SHugh Dickins if (force_flush) 1757fa0aafb8SPeter Zijlstra tlb_flush_mmu(tlb); 1758d16dfc55SPeter Zijlstra 175951c6f666SRobin Holt return addr; 17601da177e4SLinus Torvalds } 17611da177e4SLinus Torvalds 176251c6f666SRobin Holt static inline unsigned long zap_pmd_range(struct mmu_gather *tlb, 1763b5810039SNick Piggin struct vm_area_struct *vma, pud_t *pud, 17641da177e4SLinus Torvalds unsigned long addr, unsigned long end, 176597a89413SPeter Zijlstra struct zap_details *details) 17661da177e4SLinus Torvalds { 17671da177e4SLinus Torvalds pmd_t *pmd; 17681da177e4SLinus Torvalds unsigned long next; 17691da177e4SLinus Torvalds 17701da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 17711da177e4SLinus Torvalds do { 17721da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 177384c3fc4eSZi Yan if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) { 177453406ed1SHugh Dickins if (next - addr != HPAGE_PMD_SIZE) 1775fd60775aSDavid Rientjes __split_huge_pmd(vma, pmd, addr, false, NULL); 17763db82b93SHugh Dickins else if (zap_huge_pmd(tlb, vma, pmd, addr)) { 17773db82b93SHugh Dickins addr = next; 17783db82b93SHugh Dickins continue; 17793db82b93SHugh Dickins } 178071e3aac0SAndrea Arcangeli /* fall through */ 17813506659eSMatthew Wilcox (Oracle) } else if (details && details->single_folio && 17823506659eSMatthew Wilcox (Oracle) folio_test_pmd_mappable(details->single_folio) && 178322061a1fSHugh Dickins next - addr == HPAGE_PMD_SIZE && pmd_none(*pmd)) { 178422061a1fSHugh Dickins spinlock_t *ptl = pmd_lock(tlb->mm, pmd); 178522061a1fSHugh Dickins /* 178622061a1fSHugh Dickins * Take and drop THP pmd lock so that we cannot return 178722061a1fSHugh Dickins * prematurely, while zap_huge_pmd() has cleared *pmd, 178822061a1fSHugh Dickins * but not yet decremented compound_mapcount(). 178922061a1fSHugh Dickins */ 179022061a1fSHugh Dickins spin_unlock(ptl); 179171e3aac0SAndrea Arcangeli } 17923db82b93SHugh Dickins if (pmd_none(*pmd)) { 17933db82b93SHugh Dickins addr = next; 17943db82b93SHugh Dickins continue; 17953db82b93SHugh Dickins } 17963db82b93SHugh Dickins addr = zap_pte_range(tlb, vma, pmd, addr, next, details); 17973db82b93SHugh Dickins if (addr != next) 17983db82b93SHugh Dickins pmd--; 17993db82b93SHugh Dickins } while (pmd++, cond_resched(), addr != end); 180051c6f666SRobin Holt 180151c6f666SRobin Holt return addr; 18021da177e4SLinus Torvalds } 18031da177e4SLinus Torvalds 180451c6f666SRobin Holt static inline unsigned long zap_pud_range(struct mmu_gather *tlb, 1805c2febafcSKirill A. Shutemov struct vm_area_struct *vma, p4d_t *p4d, 18061da177e4SLinus Torvalds unsigned long addr, unsigned long end, 180797a89413SPeter Zijlstra struct zap_details *details) 18081da177e4SLinus Torvalds { 18091da177e4SLinus Torvalds pud_t *pud; 18101da177e4SLinus Torvalds unsigned long next; 18111da177e4SLinus Torvalds 1812c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 18131da177e4SLinus Torvalds do { 18141da177e4SLinus Torvalds next = pud_addr_end(addr, end); 1815a00cc7d9SMatthew Wilcox if (pud_trans_huge(*pud) || pud_devmap(*pud)) { 1816a00cc7d9SMatthew Wilcox if (next - addr != HPAGE_PUD_SIZE) { 181742fc5414SMichel Lespinasse mmap_assert_locked(tlb->mm); 1818a00cc7d9SMatthew Wilcox split_huge_pud(vma, pud, addr); 1819a00cc7d9SMatthew Wilcox } else if (zap_huge_pud(tlb, vma, pud, addr)) 1820a00cc7d9SMatthew Wilcox goto next; 1821a00cc7d9SMatthew Wilcox /* fall through */ 1822a00cc7d9SMatthew Wilcox } 182397a89413SPeter Zijlstra if (pud_none_or_clear_bad(pud)) 18241da177e4SLinus Torvalds continue; 182597a89413SPeter Zijlstra next = zap_pmd_range(tlb, vma, pud, addr, next, details); 1826a00cc7d9SMatthew Wilcox next: 1827a00cc7d9SMatthew Wilcox cond_resched(); 182897a89413SPeter Zijlstra } while (pud++, addr = next, addr != end); 182951c6f666SRobin Holt 183051c6f666SRobin Holt return addr; 18311da177e4SLinus Torvalds } 18321da177e4SLinus Torvalds 1833c2febafcSKirill A. Shutemov static inline unsigned long zap_p4d_range(struct mmu_gather *tlb, 1834c2febafcSKirill A. Shutemov struct vm_area_struct *vma, pgd_t *pgd, 1835c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 1836c2febafcSKirill A. Shutemov struct zap_details *details) 1837c2febafcSKirill A. Shutemov { 1838c2febafcSKirill A. Shutemov p4d_t *p4d; 1839c2febafcSKirill A. Shutemov unsigned long next; 1840c2febafcSKirill A. Shutemov 1841c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 1842c2febafcSKirill A. Shutemov do { 1843c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 1844c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(p4d)) 1845c2febafcSKirill A. Shutemov continue; 1846c2febafcSKirill A. Shutemov next = zap_pud_range(tlb, vma, p4d, addr, next, details); 1847c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 1848c2febafcSKirill A. Shutemov 1849c2febafcSKirill A. Shutemov return addr; 1850c2febafcSKirill A. Shutemov } 1851c2febafcSKirill A. Shutemov 1852aac45363SMichal Hocko void unmap_page_range(struct mmu_gather *tlb, 185351c6f666SRobin Holt struct vm_area_struct *vma, 18541da177e4SLinus Torvalds unsigned long addr, unsigned long end, 185597a89413SPeter Zijlstra struct zap_details *details) 18561da177e4SLinus Torvalds { 18571da177e4SLinus Torvalds pgd_t *pgd; 18581da177e4SLinus Torvalds unsigned long next; 18591da177e4SLinus Torvalds 18601da177e4SLinus Torvalds BUG_ON(addr >= end); 18611da177e4SLinus Torvalds tlb_start_vma(tlb, vma); 18621da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 18631da177e4SLinus Torvalds do { 18641da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 186597a89413SPeter Zijlstra if (pgd_none_or_clear_bad(pgd)) 18661da177e4SLinus Torvalds continue; 1867c2febafcSKirill A. Shutemov next = zap_p4d_range(tlb, vma, pgd, addr, next, details); 186897a89413SPeter Zijlstra } while (pgd++, addr = next, addr != end); 18691da177e4SLinus Torvalds tlb_end_vma(tlb, vma); 18701da177e4SLinus Torvalds } 18711da177e4SLinus Torvalds 1872f5cc4eefSAl Viro 1873f5cc4eefSAl Viro static void unmap_single_vma(struct mmu_gather *tlb, 18741da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long start_addr, 18754f74d2c8SLinus Torvalds unsigned long end_addr, 187668f48381SSuren Baghdasaryan struct zap_details *details, bool mm_wr_locked) 18771da177e4SLinus Torvalds { 1878f5cc4eefSAl Viro unsigned long start = max(vma->vm_start, start_addr); 18791da177e4SLinus Torvalds unsigned long end; 18801da177e4SLinus Torvalds 18811da177e4SLinus Torvalds if (start >= vma->vm_end) 1882f5cc4eefSAl Viro return; 18831da177e4SLinus Torvalds end = min(vma->vm_end, end_addr); 18841da177e4SLinus Torvalds if (end <= vma->vm_start) 1885f5cc4eefSAl Viro return; 18861da177e4SLinus Torvalds 1887cbc91f71SSrikar Dronamraju if (vma->vm_file) 1888cbc91f71SSrikar Dronamraju uprobe_munmap(vma, start, end); 1889cbc91f71SSrikar Dronamraju 1890b3b9c293SKonstantin Khlebnikov if (unlikely(vma->vm_flags & VM_PFNMAP)) 189168f48381SSuren Baghdasaryan untrack_pfn(vma, 0, 0, mm_wr_locked); 18922ab64037Svenkatesh.pallipadi@intel.com 18938b2a1238SAl Viro if (start != end) { 189451c6f666SRobin Holt if (unlikely(is_vm_hugetlb_page(vma))) { 1895a137e1ccSAndi Kleen /* 1896a137e1ccSAndi Kleen * It is undesirable to test vma->vm_file as it 1897a137e1ccSAndi Kleen * should be non-null for valid hugetlb area. 1898a137e1ccSAndi Kleen * However, vm_file will be NULL in the error 18997aa6b4adSDavidlohr Bueso * cleanup path of mmap_region. When 1900a137e1ccSAndi Kleen * hugetlbfs ->mmap method fails, 19017aa6b4adSDavidlohr Bueso * mmap_region() nullifies vma->vm_file 1902a137e1ccSAndi Kleen * before calling this function to clean up. 1903a137e1ccSAndi Kleen * Since no pte has actually been setup, it is 1904a137e1ccSAndi Kleen * safe to do nothing in this case. 1905a137e1ccSAndi Kleen */ 190624669e58SAneesh Kumar K.V if (vma->vm_file) { 190705e90bd0SPeter Xu zap_flags_t zap_flags = details ? 190805e90bd0SPeter Xu details->zap_flags : 0; 19092820b0f0SRik van Riel __unmap_hugepage_range(tlb, vma, start, end, 191005e90bd0SPeter Xu NULL, zap_flags); 191124669e58SAneesh Kumar K.V } 191251c6f666SRobin Holt } else 1913038c7aa1SAl Viro unmap_page_range(tlb, vma, start, end, details); 191497a89413SPeter Zijlstra } 191551c6f666SRobin Holt } 19161da177e4SLinus Torvalds 1917f5cc4eefSAl Viro /** 1918f5cc4eefSAl Viro * unmap_vmas - unmap a range of memory covered by a list of vma's 1919f5cc4eefSAl Viro * @tlb: address of the caller's struct mmu_gather 19206e412203SYang Li * @mas: the maple state 1921f5cc4eefSAl Viro * @vma: the starting vma 1922f5cc4eefSAl Viro * @start_addr: virtual address at which to start unmapping 1923f5cc4eefSAl Viro * @end_addr: virtual address at which to end unmapping 19246e412203SYang Li * @tree_end: The maximum index to check 1925809ef83cSYang Li * @mm_wr_locked: lock flag 1926f5cc4eefSAl Viro * 1927f5cc4eefSAl Viro * Unmap all pages in the vma list. 1928f5cc4eefSAl Viro * 1929f5cc4eefSAl Viro * Only addresses between `start' and `end' will be unmapped. 1930f5cc4eefSAl Viro * 1931f5cc4eefSAl Viro * The VMA list must be sorted in ascending virtual address order. 1932f5cc4eefSAl Viro * 1933f5cc4eefSAl Viro * unmap_vmas() assumes that the caller will flush the whole unmapped address 1934f5cc4eefSAl Viro * range after unmap_vmas() returns. So the only responsibility here is to 1935f5cc4eefSAl Viro * ensure that any thus-far unmapped pages are flushed before unmap_vmas() 1936f5cc4eefSAl Viro * drops the lock and schedules. 1937f5cc4eefSAl Viro */ 1938fd892593SLiam R. Howlett void unmap_vmas(struct mmu_gather *tlb, struct ma_state *mas, 1939f5cc4eefSAl Viro struct vm_area_struct *vma, unsigned long start_addr, 1940fd892593SLiam R. Howlett unsigned long end_addr, unsigned long tree_end, 1941fd892593SLiam R. Howlett bool mm_wr_locked) 1942f5cc4eefSAl Viro { 1943ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 1944999dad82SPeter Xu struct zap_details details = { 194504ada095SMike Kravetz .zap_flags = ZAP_FLAG_DROP_MARKER | ZAP_FLAG_UNMAP, 1946999dad82SPeter Xu /* Careful - we need to zap private pages too! */ 1947999dad82SPeter Xu .even_cows = true, 1948999dad82SPeter Xu }; 1949f5cc4eefSAl Viro 19507d4a8be0SAlistair Popple mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma->vm_mm, 19516f4f13e8SJérôme Glisse start_addr, end_addr); 1952ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 1953763ecb03SLiam R. Howlett do { 19542820b0f0SRik van Riel unsigned long start = start_addr; 19552820b0f0SRik van Riel unsigned long end = end_addr; 19562820b0f0SRik van Riel hugetlb_zap_begin(vma, &start, &end); 19572820b0f0SRik van Riel unmap_single_vma(tlb, vma, start, end, &details, 195868f48381SSuren Baghdasaryan mm_wr_locked); 19592820b0f0SRik van Riel hugetlb_zap_end(vma, &details); 1960d2406291SPeng Zhang vma = mas_find(mas, tree_end - 1); 1961d2406291SPeng Zhang } while (vma && likely(!xa_is_zero(vma))); 1962ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 19631da177e4SLinus Torvalds } 19641da177e4SLinus Torvalds 19651da177e4SLinus Torvalds /** 1966f5cc4eefSAl Viro * zap_page_range_single - remove user pages in a given range 1967f5cc4eefSAl Viro * @vma: vm_area_struct holding the applicable pages 1968f5cc4eefSAl Viro * @address: starting address of pages to zap 1969f5cc4eefSAl Viro * @size: number of bytes to zap 19708a5f14a2SKirill A. Shutemov * @details: details of shared cache invalidation 1971f5cc4eefSAl Viro * 1972f5cc4eefSAl Viro * The range must fit into one VMA. 1973f5cc4eefSAl Viro */ 197421b85b09SMike Kravetz void zap_page_range_single(struct vm_area_struct *vma, unsigned long address, 1975f5cc4eefSAl Viro unsigned long size, struct zap_details *details) 1976f5cc4eefSAl Viro { 197721b85b09SMike Kravetz const unsigned long end = address + size; 1978ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 1979f5cc4eefSAl Viro struct mmu_gather tlb; 1980f5cc4eefSAl Viro 1981f5cc4eefSAl Viro lru_add_drain(); 19827d4a8be0SAlistair Popple mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma->vm_mm, 198321b85b09SMike Kravetz address, end); 19842820b0f0SRik van Riel hugetlb_zap_begin(vma, &range.start, &range.end); 1985a72afd87SWill Deacon tlb_gather_mmu(&tlb, vma->vm_mm); 1986ac46d4f3SJérôme Glisse update_hiwater_rss(vma->vm_mm); 1987ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 198821b85b09SMike Kravetz /* 198921b85b09SMike Kravetz * unmap 'address-end' not 'range.start-range.end' as range 199021b85b09SMike Kravetz * could have been expanded for hugetlb pmd sharing. 199121b85b09SMike Kravetz */ 199268f48381SSuren Baghdasaryan unmap_single_vma(&tlb, vma, address, end, details, false); 1993ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 1994ae8eba8bSWill Deacon tlb_finish_mmu(&tlb); 19952820b0f0SRik van Riel hugetlb_zap_end(vma, details); 19961da177e4SLinus Torvalds } 19971da177e4SLinus Torvalds 1998c627f9ccSJack Steiner /** 1999c627f9ccSJack Steiner * zap_vma_ptes - remove ptes mapping the vma 2000c627f9ccSJack Steiner * @vma: vm_area_struct holding ptes to be zapped 2001c627f9ccSJack Steiner * @address: starting address of pages to zap 2002c627f9ccSJack Steiner * @size: number of bytes to zap 2003c627f9ccSJack Steiner * 2004c627f9ccSJack Steiner * This function only unmaps ptes assigned to VM_PFNMAP vmas. 2005c627f9ccSJack Steiner * 2006c627f9ccSJack Steiner * The entire address range must be fully contained within the vma. 2007c627f9ccSJack Steiner * 2008c627f9ccSJack Steiner */ 200927d036e3SLeon Romanovsky void zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, 2010c627f9ccSJack Steiner unsigned long size) 2011c627f9ccSJack Steiner { 201288a35912SMiaohe Lin if (!range_in_vma(vma, address, address + size) || 2013c627f9ccSJack Steiner !(vma->vm_flags & VM_PFNMAP)) 201427d036e3SLeon Romanovsky return; 201527d036e3SLeon Romanovsky 2016f5cc4eefSAl Viro zap_page_range_single(vma, address, size, NULL); 2017c627f9ccSJack Steiner } 2018c627f9ccSJack Steiner EXPORT_SYMBOL_GPL(zap_vma_ptes); 2019c627f9ccSJack Steiner 20208cd3984dSArjun Roy static pmd_t *walk_to_pmd(struct mm_struct *mm, unsigned long addr) 2021c9cfcddfSLinus Torvalds { 2022c2febafcSKirill A. Shutemov pgd_t *pgd; 2023c2febafcSKirill A. Shutemov p4d_t *p4d; 2024c2febafcSKirill A. Shutemov pud_t *pud; 2025c2febafcSKirill A. Shutemov pmd_t *pmd; 2026c2febafcSKirill A. Shutemov 2027c2febafcSKirill A. Shutemov pgd = pgd_offset(mm, addr); 2028c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, addr); 2029c2febafcSKirill A. Shutemov if (!p4d) 2030c2febafcSKirill A. Shutemov return NULL; 2031c2febafcSKirill A. Shutemov pud = pud_alloc(mm, p4d, addr); 2032c2febafcSKirill A. Shutemov if (!pud) 2033c2febafcSKirill A. Shutemov return NULL; 2034c2febafcSKirill A. Shutemov pmd = pmd_alloc(mm, pud, addr); 2035c2febafcSKirill A. Shutemov if (!pmd) 2036c2febafcSKirill A. Shutemov return NULL; 2037c2febafcSKirill A. Shutemov 2038f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 20398cd3984dSArjun Roy return pmd; 20408cd3984dSArjun Roy } 20418cd3984dSArjun Roy 20428cd3984dSArjun Roy pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr, 20438cd3984dSArjun Roy spinlock_t **ptl) 20448cd3984dSArjun Roy { 20458cd3984dSArjun Roy pmd_t *pmd = walk_to_pmd(mm, addr); 20468cd3984dSArjun Roy 20478cd3984dSArjun Roy if (!pmd) 20488cd3984dSArjun Roy return NULL; 2049c9cfcddfSLinus Torvalds return pte_alloc_map_lock(mm, pmd, addr, ptl); 2050c9cfcddfSLinus Torvalds } 2051c9cfcddfSLinus Torvalds 20528efd6f5bSArjun Roy static int validate_page_before_insert(struct page *page) 20538efd6f5bSArjun Roy { 2054f8b6187dSKefeng Wang struct folio *folio = page_folio(page); 2055f8b6187dSKefeng Wang 2056f8b6187dSKefeng Wang if (folio_test_anon(folio) || folio_test_slab(folio) || 2057f8b6187dSKefeng Wang page_has_type(page)) 20588efd6f5bSArjun Roy return -EINVAL; 2059f8b6187dSKefeng Wang flush_dcache_folio(folio); 20608efd6f5bSArjun Roy return 0; 20618efd6f5bSArjun Roy } 20628efd6f5bSArjun Roy 2063cea86fe2SHugh Dickins static int insert_page_into_pte_locked(struct vm_area_struct *vma, pte_t *pte, 20648efd6f5bSArjun Roy unsigned long addr, struct page *page, pgprot_t prot) 20658efd6f5bSArjun Roy { 2066ef37b2eaSDavid Hildenbrand struct folio *folio = page_folio(page); 2067ef37b2eaSDavid Hildenbrand 2068c33c7948SRyan Roberts if (!pte_none(ptep_get(pte))) 20698efd6f5bSArjun Roy return -EBUSY; 20708efd6f5bSArjun Roy /* Ok, finally just insert the thing.. */ 2071ef37b2eaSDavid Hildenbrand folio_get(folio); 20726b27cc6cSKefeng Wang inc_mm_counter(vma->vm_mm, mm_counter_file(folio)); 2073ef37b2eaSDavid Hildenbrand folio_add_file_rmap_pte(folio, page, vma); 2074cea86fe2SHugh Dickins set_pte_at(vma->vm_mm, addr, pte, mk_pte(page, prot)); 20758efd6f5bSArjun Roy return 0; 20768efd6f5bSArjun Roy } 20778efd6f5bSArjun Roy 20781da177e4SLinus Torvalds /* 2079238f58d8SLinus Torvalds * This is the old fallback for page remapping. 2080238f58d8SLinus Torvalds * 2081238f58d8SLinus Torvalds * For historical reasons, it only allows reserved pages. Only 2082238f58d8SLinus Torvalds * old drivers should use this, and they needed to mark their 2083238f58d8SLinus Torvalds * pages reserved for the old functions anyway. 2084238f58d8SLinus Torvalds */ 2085423bad60SNick Piggin static int insert_page(struct vm_area_struct *vma, unsigned long addr, 2086423bad60SNick Piggin struct page *page, pgprot_t prot) 2087238f58d8SLinus Torvalds { 2088238f58d8SLinus Torvalds int retval; 2089238f58d8SLinus Torvalds pte_t *pte; 2090238f58d8SLinus Torvalds spinlock_t *ptl; 2091238f58d8SLinus Torvalds 20928efd6f5bSArjun Roy retval = validate_page_before_insert(page); 20938efd6f5bSArjun Roy if (retval) 20945b4e655eSKAMEZAWA Hiroyuki goto out; 2095238f58d8SLinus Torvalds retval = -ENOMEM; 2096cea86fe2SHugh Dickins pte = get_locked_pte(vma->vm_mm, addr, &ptl); 2097238f58d8SLinus Torvalds if (!pte) 20985b4e655eSKAMEZAWA Hiroyuki goto out; 2099cea86fe2SHugh Dickins retval = insert_page_into_pte_locked(vma, pte, addr, page, prot); 2100238f58d8SLinus Torvalds pte_unmap_unlock(pte, ptl); 2101238f58d8SLinus Torvalds out: 2102238f58d8SLinus Torvalds return retval; 2103238f58d8SLinus Torvalds } 2104238f58d8SLinus Torvalds 2105cea86fe2SHugh Dickins static int insert_page_in_batch_locked(struct vm_area_struct *vma, pte_t *pte, 21068cd3984dSArjun Roy unsigned long addr, struct page *page, pgprot_t prot) 21078cd3984dSArjun Roy { 21088cd3984dSArjun Roy int err; 21098cd3984dSArjun Roy 21108cd3984dSArjun Roy if (!page_count(page)) 21118cd3984dSArjun Roy return -EINVAL; 21128cd3984dSArjun Roy err = validate_page_before_insert(page); 21137f70c2a6SArjun Roy if (err) 21147f70c2a6SArjun Roy return err; 2115cea86fe2SHugh Dickins return insert_page_into_pte_locked(vma, pte, addr, page, prot); 21168cd3984dSArjun Roy } 21178cd3984dSArjun Roy 21188cd3984dSArjun Roy /* insert_pages() amortizes the cost of spinlock operations 2119bb7dbaafSMatthew Wilcox (Oracle) * when inserting pages in a loop. 21208cd3984dSArjun Roy */ 21218cd3984dSArjun Roy static int insert_pages(struct vm_area_struct *vma, unsigned long addr, 21228cd3984dSArjun Roy struct page **pages, unsigned long *num, pgprot_t prot) 21238cd3984dSArjun Roy { 21248cd3984dSArjun Roy pmd_t *pmd = NULL; 21257f70c2a6SArjun Roy pte_t *start_pte, *pte; 21267f70c2a6SArjun Roy spinlock_t *pte_lock; 21278cd3984dSArjun Roy struct mm_struct *const mm = vma->vm_mm; 21288cd3984dSArjun Roy unsigned long curr_page_idx = 0; 21298cd3984dSArjun Roy unsigned long remaining_pages_total = *num; 21308cd3984dSArjun Roy unsigned long pages_to_write_in_pmd; 21318cd3984dSArjun Roy int ret; 21328cd3984dSArjun Roy more: 21338cd3984dSArjun Roy ret = -EFAULT; 21348cd3984dSArjun Roy pmd = walk_to_pmd(mm, addr); 21358cd3984dSArjun Roy if (!pmd) 21368cd3984dSArjun Roy goto out; 21378cd3984dSArjun Roy 21388cd3984dSArjun Roy pages_to_write_in_pmd = min_t(unsigned long, 21398cd3984dSArjun Roy remaining_pages_total, PTRS_PER_PTE - pte_index(addr)); 21408cd3984dSArjun Roy 21418cd3984dSArjun Roy /* Allocate the PTE if necessary; takes PMD lock once only. */ 21428cd3984dSArjun Roy ret = -ENOMEM; 21438cd3984dSArjun Roy if (pte_alloc(mm, pmd)) 21448cd3984dSArjun Roy goto out; 21458cd3984dSArjun Roy 21468cd3984dSArjun Roy while (pages_to_write_in_pmd) { 21478cd3984dSArjun Roy int pte_idx = 0; 21488cd3984dSArjun Roy const int batch_size = min_t(int, pages_to_write_in_pmd, 8); 21498cd3984dSArjun Roy 21507f70c2a6SArjun Roy start_pte = pte_offset_map_lock(mm, pmd, addr, &pte_lock); 21513db82b93SHugh Dickins if (!start_pte) { 21523db82b93SHugh Dickins ret = -EFAULT; 21533db82b93SHugh Dickins goto out; 21543db82b93SHugh Dickins } 21557f70c2a6SArjun Roy for (pte = start_pte; pte_idx < batch_size; ++pte, ++pte_idx) { 2156cea86fe2SHugh Dickins int err = insert_page_in_batch_locked(vma, pte, 21578cd3984dSArjun Roy addr, pages[curr_page_idx], prot); 21588cd3984dSArjun Roy if (unlikely(err)) { 21597f70c2a6SArjun Roy pte_unmap_unlock(start_pte, pte_lock); 21608cd3984dSArjun Roy ret = err; 21618cd3984dSArjun Roy remaining_pages_total -= pte_idx; 21628cd3984dSArjun Roy goto out; 21638cd3984dSArjun Roy } 21648cd3984dSArjun Roy addr += PAGE_SIZE; 21658cd3984dSArjun Roy ++curr_page_idx; 21668cd3984dSArjun Roy } 21677f70c2a6SArjun Roy pte_unmap_unlock(start_pte, pte_lock); 21688cd3984dSArjun Roy pages_to_write_in_pmd -= batch_size; 21698cd3984dSArjun Roy remaining_pages_total -= batch_size; 21708cd3984dSArjun Roy } 21718cd3984dSArjun Roy if (remaining_pages_total) 21728cd3984dSArjun Roy goto more; 21738cd3984dSArjun Roy ret = 0; 21748cd3984dSArjun Roy out: 21758cd3984dSArjun Roy *num = remaining_pages_total; 21768cd3984dSArjun Roy return ret; 21778cd3984dSArjun Roy } 21788cd3984dSArjun Roy 21798cd3984dSArjun Roy /** 21808cd3984dSArjun Roy * vm_insert_pages - insert multiple pages into user vma, batching the pmd lock. 21818cd3984dSArjun Roy * @vma: user vma to map to 21828cd3984dSArjun Roy * @addr: target start user address of these pages 21838cd3984dSArjun Roy * @pages: source kernel pages 21848cd3984dSArjun Roy * @num: in: number of pages to map. out: number of pages that were *not* 21858cd3984dSArjun Roy * mapped. (0 means all pages were successfully mapped). 21868cd3984dSArjun Roy * 21878cd3984dSArjun Roy * Preferred over vm_insert_page() when inserting multiple pages. 21888cd3984dSArjun Roy * 21898cd3984dSArjun Roy * In case of error, we may have mapped a subset of the provided 21908cd3984dSArjun Roy * pages. It is the caller's responsibility to account for this case. 21918cd3984dSArjun Roy * 21928cd3984dSArjun Roy * The same restrictions apply as in vm_insert_page(). 21938cd3984dSArjun Roy */ 21948cd3984dSArjun Roy int vm_insert_pages(struct vm_area_struct *vma, unsigned long addr, 21958cd3984dSArjun Roy struct page **pages, unsigned long *num) 21968cd3984dSArjun Roy { 21978cd3984dSArjun Roy const unsigned long end_addr = addr + (*num * PAGE_SIZE) - 1; 21988cd3984dSArjun Roy 21998cd3984dSArjun Roy if (addr < vma->vm_start || end_addr >= vma->vm_end) 22008cd3984dSArjun Roy return -EFAULT; 22018cd3984dSArjun Roy if (!(vma->vm_flags & VM_MIXEDMAP)) { 2202d8ed45c5SMichel Lespinasse BUG_ON(mmap_read_trylock(vma->vm_mm)); 22038cd3984dSArjun Roy BUG_ON(vma->vm_flags & VM_PFNMAP); 22041c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_MIXEDMAP); 22058cd3984dSArjun Roy } 22068cd3984dSArjun Roy /* Defer page refcount checking till we're about to map that page. */ 22078cd3984dSArjun Roy return insert_pages(vma, addr, pages, num, vma->vm_page_prot); 22088cd3984dSArjun Roy } 22098cd3984dSArjun Roy EXPORT_SYMBOL(vm_insert_pages); 22108cd3984dSArjun Roy 2211bfa5bf6dSRolf Eike Beer /** 2212bfa5bf6dSRolf Eike Beer * vm_insert_page - insert single page into user vma 2213bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 2214bfa5bf6dSRolf Eike Beer * @addr: target user address of this page 2215bfa5bf6dSRolf Eike Beer * @page: source kernel page 2216bfa5bf6dSRolf Eike Beer * 2217a145dd41SLinus Torvalds * This allows drivers to insert individual pages they've allocated 2218a145dd41SLinus Torvalds * into a user vma. 2219a145dd41SLinus Torvalds * 2220a145dd41SLinus Torvalds * The page has to be a nice clean _individual_ kernel allocation. 2221a145dd41SLinus Torvalds * If you allocate a compound page, you need to have marked it as 2222a145dd41SLinus Torvalds * such (__GFP_COMP), or manually just split the page up yourself 22238dfcc9baSNick Piggin * (see split_page()). 2224a145dd41SLinus Torvalds * 2225a145dd41SLinus Torvalds * NOTE! Traditionally this was done with "remap_pfn_range()" which 2226a145dd41SLinus Torvalds * took an arbitrary page protection parameter. This doesn't allow 2227a145dd41SLinus Torvalds * that. Your vma protection will have to be set up correctly, which 2228a145dd41SLinus Torvalds * means that if you want a shared writable mapping, you'd better 2229a145dd41SLinus Torvalds * ask for a shared writable mapping! 2230a145dd41SLinus Torvalds * 2231a145dd41SLinus Torvalds * The page does not need to be reserved. 22324b6e1e37SKonstantin Khlebnikov * 22334b6e1e37SKonstantin Khlebnikov * Usually this function is called from f_op->mmap() handler 2234c1e8d7c6SMichel Lespinasse * under mm->mmap_lock write-lock, so it can change vma->vm_flags. 22354b6e1e37SKonstantin Khlebnikov * Caller must set VM_MIXEDMAP on vma if it wants to call this 22364b6e1e37SKonstantin Khlebnikov * function from other places, for example from page-fault handler. 2237a862f68aSMike Rapoport * 2238a862f68aSMike Rapoport * Return: %0 on success, negative error code otherwise. 2239a145dd41SLinus Torvalds */ 2240423bad60SNick Piggin int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, 2241423bad60SNick Piggin struct page *page) 2242a145dd41SLinus Torvalds { 2243a145dd41SLinus Torvalds if (addr < vma->vm_start || addr >= vma->vm_end) 2244a145dd41SLinus Torvalds return -EFAULT; 2245a145dd41SLinus Torvalds if (!page_count(page)) 2246a145dd41SLinus Torvalds return -EINVAL; 22474b6e1e37SKonstantin Khlebnikov if (!(vma->vm_flags & VM_MIXEDMAP)) { 2248d8ed45c5SMichel Lespinasse BUG_ON(mmap_read_trylock(vma->vm_mm)); 22494b6e1e37SKonstantin Khlebnikov BUG_ON(vma->vm_flags & VM_PFNMAP); 22501c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_MIXEDMAP); 22514b6e1e37SKonstantin Khlebnikov } 2252423bad60SNick Piggin return insert_page(vma, addr, page, vma->vm_page_prot); 2253a145dd41SLinus Torvalds } 2254e3c3374fSLinus Torvalds EXPORT_SYMBOL(vm_insert_page); 2255a145dd41SLinus Torvalds 2256a667d745SSouptick Joarder /* 2257a667d745SSouptick Joarder * __vm_map_pages - maps range of kernel pages into user vma 2258a667d745SSouptick Joarder * @vma: user vma to map to 2259a667d745SSouptick Joarder * @pages: pointer to array of source kernel pages 2260a667d745SSouptick Joarder * @num: number of pages in page array 2261a667d745SSouptick Joarder * @offset: user's requested vm_pgoff 2262a667d745SSouptick Joarder * 2263a667d745SSouptick Joarder * This allows drivers to map range of kernel pages into a user vma. 2264a667d745SSouptick Joarder * 2265a667d745SSouptick Joarder * Return: 0 on success and error code otherwise. 2266a667d745SSouptick Joarder */ 2267a667d745SSouptick Joarder static int __vm_map_pages(struct vm_area_struct *vma, struct page **pages, 2268a667d745SSouptick Joarder unsigned long num, unsigned long offset) 2269a667d745SSouptick Joarder { 2270a667d745SSouptick Joarder unsigned long count = vma_pages(vma); 2271a667d745SSouptick Joarder unsigned long uaddr = vma->vm_start; 2272a667d745SSouptick Joarder int ret, i; 2273a667d745SSouptick Joarder 2274a667d745SSouptick Joarder /* Fail if the user requested offset is beyond the end of the object */ 227596756fcbSMiguel Ojeda if (offset >= num) 2276a667d745SSouptick Joarder return -ENXIO; 2277a667d745SSouptick Joarder 2278a667d745SSouptick Joarder /* Fail if the user requested size exceeds available object size */ 2279a667d745SSouptick Joarder if (count > num - offset) 2280a667d745SSouptick Joarder return -ENXIO; 2281a667d745SSouptick Joarder 2282a667d745SSouptick Joarder for (i = 0; i < count; i++) { 2283a667d745SSouptick Joarder ret = vm_insert_page(vma, uaddr, pages[offset + i]); 2284a667d745SSouptick Joarder if (ret < 0) 2285a667d745SSouptick Joarder return ret; 2286a667d745SSouptick Joarder uaddr += PAGE_SIZE; 2287a667d745SSouptick Joarder } 2288a667d745SSouptick Joarder 2289a667d745SSouptick Joarder return 0; 2290a667d745SSouptick Joarder } 2291a667d745SSouptick Joarder 2292a667d745SSouptick Joarder /** 2293a667d745SSouptick Joarder * vm_map_pages - maps range of kernel pages starts with non zero offset 2294a667d745SSouptick Joarder * @vma: user vma to map to 2295a667d745SSouptick Joarder * @pages: pointer to array of source kernel pages 2296a667d745SSouptick Joarder * @num: number of pages in page array 2297a667d745SSouptick Joarder * 2298a667d745SSouptick Joarder * Maps an object consisting of @num pages, catering for the user's 2299a667d745SSouptick Joarder * requested vm_pgoff 2300a667d745SSouptick Joarder * 2301a667d745SSouptick Joarder * If we fail to insert any page into the vma, the function will return 2302a667d745SSouptick Joarder * immediately leaving any previously inserted pages present. Callers 2303a667d745SSouptick Joarder * from the mmap handler may immediately return the error as their caller 2304a667d745SSouptick Joarder * will destroy the vma, removing any successfully inserted pages. Other 2305a667d745SSouptick Joarder * callers should make their own arrangements for calling unmap_region(). 2306a667d745SSouptick Joarder * 2307a667d745SSouptick Joarder * Context: Process context. Called by mmap handlers. 2308a667d745SSouptick Joarder * Return: 0 on success and error code otherwise. 2309a667d745SSouptick Joarder */ 2310a667d745SSouptick Joarder int vm_map_pages(struct vm_area_struct *vma, struct page **pages, 2311a667d745SSouptick Joarder unsigned long num) 2312a667d745SSouptick Joarder { 2313a667d745SSouptick Joarder return __vm_map_pages(vma, pages, num, vma->vm_pgoff); 2314a667d745SSouptick Joarder } 2315a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages); 2316a667d745SSouptick Joarder 2317a667d745SSouptick Joarder /** 2318a667d745SSouptick Joarder * vm_map_pages_zero - map range of kernel pages starts with zero offset 2319a667d745SSouptick Joarder * @vma: user vma to map to 2320a667d745SSouptick Joarder * @pages: pointer to array of source kernel pages 2321a667d745SSouptick Joarder * @num: number of pages in page array 2322a667d745SSouptick Joarder * 2323a667d745SSouptick Joarder * Similar to vm_map_pages(), except that it explicitly sets the offset 2324a667d745SSouptick Joarder * to 0. This function is intended for the drivers that did not consider 2325a667d745SSouptick Joarder * vm_pgoff. 2326a667d745SSouptick Joarder * 2327a667d745SSouptick Joarder * Context: Process context. Called by mmap handlers. 2328a667d745SSouptick Joarder * Return: 0 on success and error code otherwise. 2329a667d745SSouptick Joarder */ 2330a667d745SSouptick Joarder int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages, 2331a667d745SSouptick Joarder unsigned long num) 2332a667d745SSouptick Joarder { 2333a667d745SSouptick Joarder return __vm_map_pages(vma, pages, num, 0); 2334a667d745SSouptick Joarder } 2335a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages_zero); 2336a667d745SSouptick Joarder 23379b5a8e00SMatthew Wilcox static vm_fault_t insert_pfn(struct vm_area_struct *vma, unsigned long addr, 2338b2770da6SRoss Zwisler pfn_t pfn, pgprot_t prot, bool mkwrite) 2339423bad60SNick Piggin { 2340423bad60SNick Piggin struct mm_struct *mm = vma->vm_mm; 2341423bad60SNick Piggin pte_t *pte, entry; 2342423bad60SNick Piggin spinlock_t *ptl; 2343423bad60SNick Piggin 2344423bad60SNick Piggin pte = get_locked_pte(mm, addr, &ptl); 2345423bad60SNick Piggin if (!pte) 23469b5a8e00SMatthew Wilcox return VM_FAULT_OOM; 2347c33c7948SRyan Roberts entry = ptep_get(pte); 2348c33c7948SRyan Roberts if (!pte_none(entry)) { 2349b2770da6SRoss Zwisler if (mkwrite) { 2350b2770da6SRoss Zwisler /* 2351b2770da6SRoss Zwisler * For read faults on private mappings the PFN passed 2352b2770da6SRoss Zwisler * in may not match the PFN we have mapped if the 2353b2770da6SRoss Zwisler * mapped PFN is a writeable COW page. In the mkwrite 2354b2770da6SRoss Zwisler * case we are creating a writable PTE for a shared 2355f2c57d91SJan Kara * mapping and we expect the PFNs to match. If they 2356f2c57d91SJan Kara * don't match, we are likely racing with block 2357f2c57d91SJan Kara * allocation and mapping invalidation so just skip the 2358f2c57d91SJan Kara * update. 2359b2770da6SRoss Zwisler */ 2360c33c7948SRyan Roberts if (pte_pfn(entry) != pfn_t_to_pfn(pfn)) { 2361c33c7948SRyan Roberts WARN_ON_ONCE(!is_zero_pfn(pte_pfn(entry))); 2362423bad60SNick Piggin goto out_unlock; 2363f2c57d91SJan Kara } 2364c33c7948SRyan Roberts entry = pte_mkyoung(entry); 2365cae85cb8SJan Kara entry = maybe_mkwrite(pte_mkdirty(entry), vma); 2366cae85cb8SJan Kara if (ptep_set_access_flags(vma, addr, pte, entry, 1)) 2367cae85cb8SJan Kara update_mmu_cache(vma, addr, pte); 2368cae85cb8SJan Kara } 2369b2770da6SRoss Zwisler goto out_unlock; 2370b2770da6SRoss Zwisler } 2371423bad60SNick Piggin 2372423bad60SNick Piggin /* Ok, finally just insert the thing.. */ 237301c8f1c4SDan Williams if (pfn_t_devmap(pfn)) 237401c8f1c4SDan Williams entry = pte_mkdevmap(pfn_t_pte(pfn, prot)); 237501c8f1c4SDan Williams else 237601c8f1c4SDan Williams entry = pte_mkspecial(pfn_t_pte(pfn, prot)); 2377b2770da6SRoss Zwisler 2378b2770da6SRoss Zwisler if (mkwrite) { 2379b2770da6SRoss Zwisler entry = pte_mkyoung(entry); 2380b2770da6SRoss Zwisler entry = maybe_mkwrite(pte_mkdirty(entry), vma); 2381b2770da6SRoss Zwisler } 2382b2770da6SRoss Zwisler 2383423bad60SNick Piggin set_pte_at(mm, addr, pte, entry); 23844b3073e1SRussell King update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */ 2385423bad60SNick Piggin 2386423bad60SNick Piggin out_unlock: 2387423bad60SNick Piggin pte_unmap_unlock(pte, ptl); 23889b5a8e00SMatthew Wilcox return VM_FAULT_NOPAGE; 2389423bad60SNick Piggin } 2390423bad60SNick Piggin 2391f5e6d1d5SMatthew Wilcox /** 2392f5e6d1d5SMatthew Wilcox * vmf_insert_pfn_prot - insert single pfn into user vma with specified pgprot 2393f5e6d1d5SMatthew Wilcox * @vma: user vma to map to 2394f5e6d1d5SMatthew Wilcox * @addr: target user address of this page 2395f5e6d1d5SMatthew Wilcox * @pfn: source kernel pfn 2396f5e6d1d5SMatthew Wilcox * @pgprot: pgprot flags for the inserted page 2397f5e6d1d5SMatthew Wilcox * 2398a1a0aea5SRandy Dunlap * This is exactly like vmf_insert_pfn(), except that it allows drivers 2399f5e6d1d5SMatthew Wilcox * to override pgprot on a per-page basis. 2400f5e6d1d5SMatthew Wilcox * 2401f5e6d1d5SMatthew Wilcox * This only makes sense for IO mappings, and it makes no sense for 2402f5e6d1d5SMatthew Wilcox * COW mappings. In general, using multiple vmas is preferable; 2403ae2b01f3SMatthew Wilcox * vmf_insert_pfn_prot should only be used if using multiple VMAs is 2404f5e6d1d5SMatthew Wilcox * impractical. 2405f5e6d1d5SMatthew Wilcox * 240628d8b812SLorenzo Stoakes * pgprot typically only differs from @vma->vm_page_prot when drivers set 240728d8b812SLorenzo Stoakes * caching- and encryption bits different than those of @vma->vm_page_prot, 240828d8b812SLorenzo Stoakes * because the caching- or encryption mode may not be known at mmap() time. 240928d8b812SLorenzo Stoakes * 241028d8b812SLorenzo Stoakes * This is ok as long as @vma->vm_page_prot is not used by the core vm 241128d8b812SLorenzo Stoakes * to set caching and encryption bits for those vmas (except for COW pages). 241228d8b812SLorenzo Stoakes * This is ensured by core vm only modifying these page table entries using 241328d8b812SLorenzo Stoakes * functions that don't touch caching- or encryption bits, using pte_modify() 241428d8b812SLorenzo Stoakes * if needed. (See for example mprotect()). 241528d8b812SLorenzo Stoakes * 241628d8b812SLorenzo Stoakes * Also when new page-table entries are created, this is only done using the 241728d8b812SLorenzo Stoakes * fault() callback, and never using the value of vma->vm_page_prot, 241828d8b812SLorenzo Stoakes * except for page-table entries that point to anonymous pages as the result 241928d8b812SLorenzo Stoakes * of COW. 2420574c5b3dSThomas Hellstrom * 2421ae2b01f3SMatthew Wilcox * Context: Process context. May allocate using %GFP_KERNEL. 2422f5e6d1d5SMatthew Wilcox * Return: vm_fault_t value. 2423f5e6d1d5SMatthew Wilcox */ 2424f5e6d1d5SMatthew Wilcox vm_fault_t vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr, 2425f5e6d1d5SMatthew Wilcox unsigned long pfn, pgprot_t pgprot) 2426f5e6d1d5SMatthew Wilcox { 24276d958546SMatthew Wilcox /* 24286d958546SMatthew Wilcox * Technically, architectures with pte_special can avoid all these 24296d958546SMatthew Wilcox * restrictions (same for remap_pfn_range). However we would like 24306d958546SMatthew Wilcox * consistency in testing and feature parity among all, so we should 24316d958546SMatthew Wilcox * try to keep these invariants in place for everybody. 24326d958546SMatthew Wilcox */ 24336d958546SMatthew Wilcox BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))); 24346d958546SMatthew Wilcox BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) == 24356d958546SMatthew Wilcox (VM_PFNMAP|VM_MIXEDMAP)); 24366d958546SMatthew Wilcox BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); 24376d958546SMatthew Wilcox BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn)); 24386d958546SMatthew Wilcox 24396d958546SMatthew Wilcox if (addr < vma->vm_start || addr >= vma->vm_end) 24406d958546SMatthew Wilcox return VM_FAULT_SIGBUS; 24416d958546SMatthew Wilcox 24426d958546SMatthew Wilcox if (!pfn_modify_allowed(pfn, pgprot)) 24436d958546SMatthew Wilcox return VM_FAULT_SIGBUS; 24446d958546SMatthew Wilcox 24456d958546SMatthew Wilcox track_pfn_insert(vma, &pgprot, __pfn_to_pfn_t(pfn, PFN_DEV)); 24466d958546SMatthew Wilcox 24479b5a8e00SMatthew Wilcox return insert_pfn(vma, addr, __pfn_to_pfn_t(pfn, PFN_DEV), pgprot, 24486d958546SMatthew Wilcox false); 2449f5e6d1d5SMatthew Wilcox } 2450f5e6d1d5SMatthew Wilcox EXPORT_SYMBOL(vmf_insert_pfn_prot); 2451e0dc0d8fSNick Piggin 2452ae2b01f3SMatthew Wilcox /** 2453ae2b01f3SMatthew Wilcox * vmf_insert_pfn - insert single pfn into user vma 2454ae2b01f3SMatthew Wilcox * @vma: user vma to map to 2455ae2b01f3SMatthew Wilcox * @addr: target user address of this page 2456ae2b01f3SMatthew Wilcox * @pfn: source kernel pfn 2457ae2b01f3SMatthew Wilcox * 2458ae2b01f3SMatthew Wilcox * Similar to vm_insert_page, this allows drivers to insert individual pages 2459ae2b01f3SMatthew Wilcox * they've allocated into a user vma. Same comments apply. 2460ae2b01f3SMatthew Wilcox * 2461ae2b01f3SMatthew Wilcox * This function should only be called from a vm_ops->fault handler, and 2462ae2b01f3SMatthew Wilcox * in that case the handler should return the result of this function. 2463ae2b01f3SMatthew Wilcox * 2464ae2b01f3SMatthew Wilcox * vma cannot be a COW mapping. 2465ae2b01f3SMatthew Wilcox * 2466ae2b01f3SMatthew Wilcox * As this is called only for pages that do not currently exist, we 2467ae2b01f3SMatthew Wilcox * do not need to flush old virtual caches or the TLB. 2468ae2b01f3SMatthew Wilcox * 2469ae2b01f3SMatthew Wilcox * Context: Process context. May allocate using %GFP_KERNEL. 2470ae2b01f3SMatthew Wilcox * Return: vm_fault_t value. 2471ae2b01f3SMatthew Wilcox */ 2472ae2b01f3SMatthew Wilcox vm_fault_t vmf_insert_pfn(struct vm_area_struct *vma, unsigned long addr, 2473ae2b01f3SMatthew Wilcox unsigned long pfn) 2474ae2b01f3SMatthew Wilcox { 2475ae2b01f3SMatthew Wilcox return vmf_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot); 2476ae2b01f3SMatthew Wilcox } 2477ae2b01f3SMatthew Wilcox EXPORT_SYMBOL(vmf_insert_pfn); 2478ae2b01f3SMatthew Wilcox 2479785a3fabSDan Williams static bool vm_mixed_ok(struct vm_area_struct *vma, pfn_t pfn) 2480785a3fabSDan Williams { 2481785a3fabSDan Williams /* these checks mirror the abort conditions in vm_normal_page */ 2482785a3fabSDan Williams if (vma->vm_flags & VM_MIXEDMAP) 2483785a3fabSDan Williams return true; 2484785a3fabSDan Williams if (pfn_t_devmap(pfn)) 2485785a3fabSDan Williams return true; 2486785a3fabSDan Williams if (pfn_t_special(pfn)) 2487785a3fabSDan Williams return true; 2488785a3fabSDan Williams if (is_zero_pfn(pfn_t_to_pfn(pfn))) 2489785a3fabSDan Williams return true; 2490785a3fabSDan Williams return false; 2491785a3fabSDan Williams } 2492785a3fabSDan Williams 249379f3aa5bSMatthew Wilcox static vm_fault_t __vm_insert_mixed(struct vm_area_struct *vma, 249428d8b812SLorenzo Stoakes unsigned long addr, pfn_t pfn, bool mkwrite) 2495423bad60SNick Piggin { 249628d8b812SLorenzo Stoakes pgprot_t pgprot = vma->vm_page_prot; 249779f3aa5bSMatthew Wilcox int err; 249887744ab3SDan Williams 2499785a3fabSDan Williams BUG_ON(!vm_mixed_ok(vma, pfn)); 2500423bad60SNick Piggin 2501423bad60SNick Piggin if (addr < vma->vm_start || addr >= vma->vm_end) 250279f3aa5bSMatthew Wilcox return VM_FAULT_SIGBUS; 2503308a047cSBorislav Petkov 2504308a047cSBorislav Petkov track_pfn_insert(vma, &pgprot, pfn); 2505423bad60SNick Piggin 250642e4089cSAndi Kleen if (!pfn_modify_allowed(pfn_t_to_pfn(pfn), pgprot)) 250779f3aa5bSMatthew Wilcox return VM_FAULT_SIGBUS; 250842e4089cSAndi Kleen 2509423bad60SNick Piggin /* 2510423bad60SNick Piggin * If we don't have pte special, then we have to use the pfn_valid() 2511423bad60SNick Piggin * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must* 2512423bad60SNick Piggin * refcount the page if pfn_valid is true (hence insert_page rather 251362eede62SHugh Dickins * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP 251462eede62SHugh Dickins * without pte special, it would there be refcounted as a normal page. 2515423bad60SNick Piggin */ 251600b3a331SLaurent Dufour if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) && 251700b3a331SLaurent Dufour !pfn_t_devmap(pfn) && pfn_t_valid(pfn)) { 2518423bad60SNick Piggin struct page *page; 2519423bad60SNick Piggin 252003fc2da6SDan Williams /* 252103fc2da6SDan Williams * At this point we are committed to insert_page() 252203fc2da6SDan Williams * regardless of whether the caller specified flags that 252303fc2da6SDan Williams * result in pfn_t_has_page() == false. 252403fc2da6SDan Williams */ 252503fc2da6SDan Williams page = pfn_to_page(pfn_t_to_pfn(pfn)); 252679f3aa5bSMatthew Wilcox err = insert_page(vma, addr, page, pgprot); 252779f3aa5bSMatthew Wilcox } else { 25289b5a8e00SMatthew Wilcox return insert_pfn(vma, addr, pfn, pgprot, mkwrite); 2529423bad60SNick Piggin } 2530b2770da6SRoss Zwisler 25315d747637SMatthew Wilcox if (err == -ENOMEM) 25325d747637SMatthew Wilcox return VM_FAULT_OOM; 25335d747637SMatthew Wilcox if (err < 0 && err != -EBUSY) 25345d747637SMatthew Wilcox return VM_FAULT_SIGBUS; 25355d747637SMatthew Wilcox 25365d747637SMatthew Wilcox return VM_FAULT_NOPAGE; 2537423bad60SNick Piggin } 253879f3aa5bSMatthew Wilcox 253979f3aa5bSMatthew Wilcox vm_fault_t vmf_insert_mixed(struct vm_area_struct *vma, unsigned long addr, 254079f3aa5bSMatthew Wilcox pfn_t pfn) 254179f3aa5bSMatthew Wilcox { 254228d8b812SLorenzo Stoakes return __vm_insert_mixed(vma, addr, pfn, false); 254379f3aa5bSMatthew Wilcox } 25445d747637SMatthew Wilcox EXPORT_SYMBOL(vmf_insert_mixed); 2545423bad60SNick Piggin 2546ab77dab4SSouptick Joarder /* 2547ab77dab4SSouptick Joarder * If the insertion of PTE failed because someone else already added a 2548ab77dab4SSouptick Joarder * different entry in the mean time, we treat that as success as we assume 2549ab77dab4SSouptick Joarder * the same entry was actually inserted. 2550ab77dab4SSouptick Joarder */ 2551ab77dab4SSouptick Joarder vm_fault_t vmf_insert_mixed_mkwrite(struct vm_area_struct *vma, 2552ab77dab4SSouptick Joarder unsigned long addr, pfn_t pfn) 2553b2770da6SRoss Zwisler { 255428d8b812SLorenzo Stoakes return __vm_insert_mixed(vma, addr, pfn, true); 2555b2770da6SRoss Zwisler } 2556ab77dab4SSouptick Joarder EXPORT_SYMBOL(vmf_insert_mixed_mkwrite); 2557b2770da6SRoss Zwisler 2558a145dd41SLinus Torvalds /* 25591da177e4SLinus Torvalds * maps a range of physical memory into the requested pages. the old 25601da177e4SLinus Torvalds * mappings are removed. any references to nonexistent pages results 25611da177e4SLinus Torvalds * in null mappings (currently treated as "copy-on-access") 25621da177e4SLinus Torvalds */ 25631da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd, 25641da177e4SLinus Torvalds unsigned long addr, unsigned long end, 25651da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 25661da177e4SLinus Torvalds { 256790a3e375SMiaohe Lin pte_t *pte, *mapped_pte; 2568c74df32cSHugh Dickins spinlock_t *ptl; 256942e4089cSAndi Kleen int err = 0; 25701da177e4SLinus Torvalds 257190a3e375SMiaohe Lin mapped_pte = pte = pte_alloc_map_lock(mm, pmd, addr, &ptl); 25721da177e4SLinus Torvalds if (!pte) 25731da177e4SLinus Torvalds return -ENOMEM; 25746606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 25751da177e4SLinus Torvalds do { 2576c33c7948SRyan Roberts BUG_ON(!pte_none(ptep_get(pte))); 257742e4089cSAndi Kleen if (!pfn_modify_allowed(pfn, prot)) { 257842e4089cSAndi Kleen err = -EACCES; 257942e4089cSAndi Kleen break; 258042e4089cSAndi Kleen } 25817e675137SNick Piggin set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot))); 25821da177e4SLinus Torvalds pfn++; 25831da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 25846606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 258590a3e375SMiaohe Lin pte_unmap_unlock(mapped_pte, ptl); 258642e4089cSAndi Kleen return err; 25871da177e4SLinus Torvalds } 25881da177e4SLinus Torvalds 25891da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud, 25901da177e4SLinus Torvalds unsigned long addr, unsigned long end, 25911da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 25921da177e4SLinus Torvalds { 25931da177e4SLinus Torvalds pmd_t *pmd; 25941da177e4SLinus Torvalds unsigned long next; 259542e4089cSAndi Kleen int err; 25961da177e4SLinus Torvalds 25971da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 25981da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, addr); 25991da177e4SLinus Torvalds if (!pmd) 26001da177e4SLinus Torvalds return -ENOMEM; 2601f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 26021da177e4SLinus Torvalds do { 26031da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 260442e4089cSAndi Kleen err = remap_pte_range(mm, pmd, addr, next, 260542e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 260642e4089cSAndi Kleen if (err) 260742e4089cSAndi Kleen return err; 26081da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 26091da177e4SLinus Torvalds return 0; 26101da177e4SLinus Torvalds } 26111da177e4SLinus Torvalds 2612c2febafcSKirill A. Shutemov static inline int remap_pud_range(struct mm_struct *mm, p4d_t *p4d, 26131da177e4SLinus Torvalds unsigned long addr, unsigned long end, 26141da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 26151da177e4SLinus Torvalds { 26161da177e4SLinus Torvalds pud_t *pud; 26171da177e4SLinus Torvalds unsigned long next; 261842e4089cSAndi Kleen int err; 26191da177e4SLinus Torvalds 26201da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 2621c2febafcSKirill A. Shutemov pud = pud_alloc(mm, p4d, addr); 26221da177e4SLinus Torvalds if (!pud) 26231da177e4SLinus Torvalds return -ENOMEM; 26241da177e4SLinus Torvalds do { 26251da177e4SLinus Torvalds next = pud_addr_end(addr, end); 262642e4089cSAndi Kleen err = remap_pmd_range(mm, pud, addr, next, 262742e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 262842e4089cSAndi Kleen if (err) 262942e4089cSAndi Kleen return err; 26301da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 26311da177e4SLinus Torvalds return 0; 26321da177e4SLinus Torvalds } 26331da177e4SLinus Torvalds 2634c2febafcSKirill A. Shutemov static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd, 2635c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 2636c2febafcSKirill A. Shutemov unsigned long pfn, pgprot_t prot) 2637c2febafcSKirill A. Shutemov { 2638c2febafcSKirill A. Shutemov p4d_t *p4d; 2639c2febafcSKirill A. Shutemov unsigned long next; 264042e4089cSAndi Kleen int err; 2641c2febafcSKirill A. Shutemov 2642c2febafcSKirill A. Shutemov pfn -= addr >> PAGE_SHIFT; 2643c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, addr); 2644c2febafcSKirill A. Shutemov if (!p4d) 2645c2febafcSKirill A. Shutemov return -ENOMEM; 2646c2febafcSKirill A. Shutemov do { 2647c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 264842e4089cSAndi Kleen err = remap_pud_range(mm, p4d, addr, next, 264942e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 265042e4089cSAndi Kleen if (err) 265142e4089cSAndi Kleen return err; 2652c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 2653c2febafcSKirill A. Shutemov return 0; 2654c2febafcSKirill A. Shutemov } 2655c2febafcSKirill A. Shutemov 265674ffa5a3SChristoph Hellwig /* 265774ffa5a3SChristoph Hellwig * Variant of remap_pfn_range that does not call track_pfn_remap. The caller 265874ffa5a3SChristoph Hellwig * must have pre-validated the caching bits of the pgprot_t. 2659bfa5bf6dSRolf Eike Beer */ 266074ffa5a3SChristoph Hellwig int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr, 26611da177e4SLinus Torvalds unsigned long pfn, unsigned long size, pgprot_t prot) 26621da177e4SLinus Torvalds { 26631da177e4SLinus Torvalds pgd_t *pgd; 26641da177e4SLinus Torvalds unsigned long next; 26652d15cab8SHugh Dickins unsigned long end = addr + PAGE_ALIGN(size); 26661da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 26671da177e4SLinus Torvalds int err; 26681da177e4SLinus Torvalds 26690c4123e3SAlex Zhang if (WARN_ON_ONCE(!PAGE_ALIGNED(addr))) 26700c4123e3SAlex Zhang return -EINVAL; 26710c4123e3SAlex Zhang 26721da177e4SLinus Torvalds /* 26731da177e4SLinus Torvalds * Physically remapped pages are special. Tell the 26741da177e4SLinus Torvalds * rest of the world about it: 26751da177e4SLinus Torvalds * VM_IO tells people not to look at these pages 26761da177e4SLinus Torvalds * (accesses can have side effects). 26776aab341eSLinus Torvalds * VM_PFNMAP tells the core MM that the base pages are just 26786aab341eSLinus Torvalds * raw PFN mappings, and do not have a "struct page" associated 26796aab341eSLinus Torvalds * with them. 2680314e51b9SKonstantin Khlebnikov * VM_DONTEXPAND 2681314e51b9SKonstantin Khlebnikov * Disable vma merging and expanding with mremap(). 2682314e51b9SKonstantin Khlebnikov * VM_DONTDUMP 2683314e51b9SKonstantin Khlebnikov * Omit vma from core dump, even when VM_IO turned off. 2684fb155c16SLinus Torvalds * 2685fb155c16SLinus Torvalds * There's a horrible special case to handle copy-on-write 2686fb155c16SLinus Torvalds * behaviour that some programs depend on. We mark the "original" 2687fb155c16SLinus Torvalds * un-COW'ed pages by matching them up with "vma->vm_pgoff". 2688b3b9c293SKonstantin Khlebnikov * See vm_normal_page() for details. 26891da177e4SLinus Torvalds */ 2690b3b9c293SKonstantin Khlebnikov if (is_cow_mapping(vma->vm_flags)) { 2691b3b9c293SKonstantin Khlebnikov if (addr != vma->vm_start || end != vma->vm_end) 2692b3b9c293SKonstantin Khlebnikov return -EINVAL; 26936aab341eSLinus Torvalds vma->vm_pgoff = pfn; 2694b3b9c293SKonstantin Khlebnikov } 2695b3b9c293SKonstantin Khlebnikov 26961c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP); 26971da177e4SLinus Torvalds 26981da177e4SLinus Torvalds BUG_ON(addr >= end); 26991da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 27001da177e4SLinus Torvalds pgd = pgd_offset(mm, addr); 27011da177e4SLinus Torvalds flush_cache_range(vma, addr, end); 27021da177e4SLinus Torvalds do { 27031da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 2704c2febafcSKirill A. Shutemov err = remap_p4d_range(mm, pgd, addr, next, 27051da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot); 27061da177e4SLinus Torvalds if (err) 270774ffa5a3SChristoph Hellwig return err; 27081da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 27092ab64037Svenkatesh.pallipadi@intel.com 271074ffa5a3SChristoph Hellwig return 0; 271174ffa5a3SChristoph Hellwig } 27122ab64037Svenkatesh.pallipadi@intel.com 271374ffa5a3SChristoph Hellwig /** 271474ffa5a3SChristoph Hellwig * remap_pfn_range - remap kernel memory to userspace 271574ffa5a3SChristoph Hellwig * @vma: user vma to map to 271674ffa5a3SChristoph Hellwig * @addr: target page aligned user address to start at 271774ffa5a3SChristoph Hellwig * @pfn: page frame number of kernel physical memory address 271874ffa5a3SChristoph Hellwig * @size: size of mapping area 271974ffa5a3SChristoph Hellwig * @prot: page protection flags for this mapping 272074ffa5a3SChristoph Hellwig * 272174ffa5a3SChristoph Hellwig * Note: this is only safe if the mm semaphore is held when called. 272274ffa5a3SChristoph Hellwig * 272374ffa5a3SChristoph Hellwig * Return: %0 on success, negative error code otherwise. 272474ffa5a3SChristoph Hellwig */ 272574ffa5a3SChristoph Hellwig int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 272674ffa5a3SChristoph Hellwig unsigned long pfn, unsigned long size, pgprot_t prot) 272774ffa5a3SChristoph Hellwig { 272874ffa5a3SChristoph Hellwig int err; 272974ffa5a3SChristoph Hellwig 273074ffa5a3SChristoph Hellwig err = track_pfn_remap(vma, &prot, pfn, addr, PAGE_ALIGN(size)); 273174ffa5a3SChristoph Hellwig if (err) 273274ffa5a3SChristoph Hellwig return -EINVAL; 273374ffa5a3SChristoph Hellwig 273474ffa5a3SChristoph Hellwig err = remap_pfn_range_notrack(vma, addr, pfn, size, prot); 273574ffa5a3SChristoph Hellwig if (err) 273668f48381SSuren Baghdasaryan untrack_pfn(vma, pfn, PAGE_ALIGN(size), true); 27371da177e4SLinus Torvalds return err; 27381da177e4SLinus Torvalds } 27391da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range); 27401da177e4SLinus Torvalds 2741b4cbb197SLinus Torvalds /** 2742b4cbb197SLinus Torvalds * vm_iomap_memory - remap memory to userspace 2743b4cbb197SLinus Torvalds * @vma: user vma to map to 2744abd69b9eSWang Wenhu * @start: start of the physical memory to be mapped 2745b4cbb197SLinus Torvalds * @len: size of area 2746b4cbb197SLinus Torvalds * 2747b4cbb197SLinus Torvalds * This is a simplified io_remap_pfn_range() for common driver use. The 2748b4cbb197SLinus Torvalds * driver just needs to give us the physical memory range to be mapped, 2749b4cbb197SLinus Torvalds * we'll figure out the rest from the vma information. 2750b4cbb197SLinus Torvalds * 2751b4cbb197SLinus Torvalds * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get 2752b4cbb197SLinus Torvalds * whatever write-combining details or similar. 2753a862f68aSMike Rapoport * 2754a862f68aSMike Rapoport * Return: %0 on success, negative error code otherwise. 2755b4cbb197SLinus Torvalds */ 2756b4cbb197SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len) 2757b4cbb197SLinus Torvalds { 2758b4cbb197SLinus Torvalds unsigned long vm_len, pfn, pages; 2759b4cbb197SLinus Torvalds 2760b4cbb197SLinus Torvalds /* Check that the physical memory area passed in looks valid */ 2761b4cbb197SLinus Torvalds if (start + len < start) 2762b4cbb197SLinus Torvalds return -EINVAL; 2763b4cbb197SLinus Torvalds /* 2764b4cbb197SLinus Torvalds * You *really* shouldn't map things that aren't page-aligned, 2765b4cbb197SLinus Torvalds * but we've historically allowed it because IO memory might 2766b4cbb197SLinus Torvalds * just have smaller alignment. 2767b4cbb197SLinus Torvalds */ 2768b4cbb197SLinus Torvalds len += start & ~PAGE_MASK; 2769b4cbb197SLinus Torvalds pfn = start >> PAGE_SHIFT; 2770b4cbb197SLinus Torvalds pages = (len + ~PAGE_MASK) >> PAGE_SHIFT; 2771b4cbb197SLinus Torvalds if (pfn + pages < pfn) 2772b4cbb197SLinus Torvalds return -EINVAL; 2773b4cbb197SLinus Torvalds 2774b4cbb197SLinus Torvalds /* We start the mapping 'vm_pgoff' pages into the area */ 2775b4cbb197SLinus Torvalds if (vma->vm_pgoff > pages) 2776b4cbb197SLinus Torvalds return -EINVAL; 2777b4cbb197SLinus Torvalds pfn += vma->vm_pgoff; 2778b4cbb197SLinus Torvalds pages -= vma->vm_pgoff; 2779b4cbb197SLinus Torvalds 2780b4cbb197SLinus Torvalds /* Can we fit all of the mapping? */ 2781b4cbb197SLinus Torvalds vm_len = vma->vm_end - vma->vm_start; 2782b4cbb197SLinus Torvalds if (vm_len >> PAGE_SHIFT > pages) 2783b4cbb197SLinus Torvalds return -EINVAL; 2784b4cbb197SLinus Torvalds 2785b4cbb197SLinus Torvalds /* Ok, let it rip */ 2786b4cbb197SLinus Torvalds return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot); 2787b4cbb197SLinus Torvalds } 2788b4cbb197SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory); 2789b4cbb197SLinus Torvalds 2790aee16b3cSJeremy Fitzhardinge static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd, 2791aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2792e80d3909SJoerg Roedel pte_fn_t fn, void *data, bool create, 2793e80d3909SJoerg Roedel pgtbl_mod_mask *mask) 2794aee16b3cSJeremy Fitzhardinge { 27958abb50c7SMiaohe Lin pte_t *pte, *mapped_pte; 2796be1db475SDaniel Axtens int err = 0; 27973f649ab7SKees Cook spinlock_t *ptl; 2798aee16b3cSJeremy Fitzhardinge 2799be1db475SDaniel Axtens if (create) { 28008abb50c7SMiaohe Lin mapped_pte = pte = (mm == &init_mm) ? 2801e80d3909SJoerg Roedel pte_alloc_kernel_track(pmd, addr, mask) : 2802aee16b3cSJeremy Fitzhardinge pte_alloc_map_lock(mm, pmd, addr, &ptl); 2803aee16b3cSJeremy Fitzhardinge if (!pte) 2804aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2805be1db475SDaniel Axtens } else { 28068abb50c7SMiaohe Lin mapped_pte = pte = (mm == &init_mm) ? 2807be1db475SDaniel Axtens pte_offset_kernel(pmd, addr) : 2808be1db475SDaniel Axtens pte_offset_map_lock(mm, pmd, addr, &ptl); 28093db82b93SHugh Dickins if (!pte) 28103db82b93SHugh Dickins return -EINVAL; 2811be1db475SDaniel Axtens } 2812aee16b3cSJeremy Fitzhardinge 281338e0edb1SJeremy Fitzhardinge arch_enter_lazy_mmu_mode(); 281438e0edb1SJeremy Fitzhardinge 2815eeb4a05fSChristoph Hellwig if (fn) { 2816aee16b3cSJeremy Fitzhardinge do { 2817c33c7948SRyan Roberts if (create || !pte_none(ptep_get(pte))) { 28188b1e0f81SAnshuman Khandual err = fn(pte++, addr, data); 2819aee16b3cSJeremy Fitzhardinge if (err) 2820aee16b3cSJeremy Fitzhardinge break; 2821be1db475SDaniel Axtens } 2822c36987e2SDaisuke Nishimura } while (addr += PAGE_SIZE, addr != end); 2823eeb4a05fSChristoph Hellwig } 2824e80d3909SJoerg Roedel *mask |= PGTBL_PTE_MODIFIED; 2825aee16b3cSJeremy Fitzhardinge 282638e0edb1SJeremy Fitzhardinge arch_leave_lazy_mmu_mode(); 282738e0edb1SJeremy Fitzhardinge 2828aee16b3cSJeremy Fitzhardinge if (mm != &init_mm) 28298abb50c7SMiaohe Lin pte_unmap_unlock(mapped_pte, ptl); 2830aee16b3cSJeremy Fitzhardinge return err; 2831aee16b3cSJeremy Fitzhardinge } 2832aee16b3cSJeremy Fitzhardinge 2833aee16b3cSJeremy Fitzhardinge static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud, 2834aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2835e80d3909SJoerg Roedel pte_fn_t fn, void *data, bool create, 2836e80d3909SJoerg Roedel pgtbl_mod_mask *mask) 2837aee16b3cSJeremy Fitzhardinge { 2838aee16b3cSJeremy Fitzhardinge pmd_t *pmd; 2839aee16b3cSJeremy Fitzhardinge unsigned long next; 2840be1db475SDaniel Axtens int err = 0; 2841aee16b3cSJeremy Fitzhardinge 2842ceb86879SAndi Kleen BUG_ON(pud_huge(*pud)); 2843ceb86879SAndi Kleen 2844be1db475SDaniel Axtens if (create) { 2845e80d3909SJoerg Roedel pmd = pmd_alloc_track(mm, pud, addr, mask); 2846aee16b3cSJeremy Fitzhardinge if (!pmd) 2847aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2848be1db475SDaniel Axtens } else { 2849be1db475SDaniel Axtens pmd = pmd_offset(pud, addr); 2850be1db475SDaniel Axtens } 2851aee16b3cSJeremy Fitzhardinge do { 2852aee16b3cSJeremy Fitzhardinge next = pmd_addr_end(addr, end); 28530c95cba4SNicholas Piggin if (pmd_none(*pmd) && !create) 28540c95cba4SNicholas Piggin continue; 28550c95cba4SNicholas Piggin if (WARN_ON_ONCE(pmd_leaf(*pmd))) 28560c95cba4SNicholas Piggin return -EINVAL; 28570c95cba4SNicholas Piggin if (!pmd_none(*pmd) && WARN_ON_ONCE(pmd_bad(*pmd))) { 28580c95cba4SNicholas Piggin if (!create) 28590c95cba4SNicholas Piggin continue; 28600c95cba4SNicholas Piggin pmd_clear_bad(pmd); 28610c95cba4SNicholas Piggin } 28620c95cba4SNicholas Piggin err = apply_to_pte_range(mm, pmd, addr, next, 28630c95cba4SNicholas Piggin fn, data, create, mask); 2864aee16b3cSJeremy Fitzhardinge if (err) 2865aee16b3cSJeremy Fitzhardinge break; 2866aee16b3cSJeremy Fitzhardinge } while (pmd++, addr = next, addr != end); 28670c95cba4SNicholas Piggin 2868aee16b3cSJeremy Fitzhardinge return err; 2869aee16b3cSJeremy Fitzhardinge } 2870aee16b3cSJeremy Fitzhardinge 2871c2febafcSKirill A. Shutemov static int apply_to_pud_range(struct mm_struct *mm, p4d_t *p4d, 2872aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2873e80d3909SJoerg Roedel pte_fn_t fn, void *data, bool create, 2874e80d3909SJoerg Roedel pgtbl_mod_mask *mask) 2875aee16b3cSJeremy Fitzhardinge { 2876aee16b3cSJeremy Fitzhardinge pud_t *pud; 2877aee16b3cSJeremy Fitzhardinge unsigned long next; 2878be1db475SDaniel Axtens int err = 0; 2879aee16b3cSJeremy Fitzhardinge 2880be1db475SDaniel Axtens if (create) { 2881e80d3909SJoerg Roedel pud = pud_alloc_track(mm, p4d, addr, mask); 2882aee16b3cSJeremy Fitzhardinge if (!pud) 2883aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2884be1db475SDaniel Axtens } else { 2885be1db475SDaniel Axtens pud = pud_offset(p4d, addr); 2886be1db475SDaniel Axtens } 2887aee16b3cSJeremy Fitzhardinge do { 2888aee16b3cSJeremy Fitzhardinge next = pud_addr_end(addr, end); 28890c95cba4SNicholas Piggin if (pud_none(*pud) && !create) 28900c95cba4SNicholas Piggin continue; 28910c95cba4SNicholas Piggin if (WARN_ON_ONCE(pud_leaf(*pud))) 28920c95cba4SNicholas Piggin return -EINVAL; 28930c95cba4SNicholas Piggin if (!pud_none(*pud) && WARN_ON_ONCE(pud_bad(*pud))) { 28940c95cba4SNicholas Piggin if (!create) 28950c95cba4SNicholas Piggin continue; 28960c95cba4SNicholas Piggin pud_clear_bad(pud); 28970c95cba4SNicholas Piggin } 28980c95cba4SNicholas Piggin err = apply_to_pmd_range(mm, pud, addr, next, 28990c95cba4SNicholas Piggin fn, data, create, mask); 2900aee16b3cSJeremy Fitzhardinge if (err) 2901aee16b3cSJeremy Fitzhardinge break; 2902aee16b3cSJeremy Fitzhardinge } while (pud++, addr = next, addr != end); 29030c95cba4SNicholas Piggin 2904aee16b3cSJeremy Fitzhardinge return err; 2905aee16b3cSJeremy Fitzhardinge } 2906aee16b3cSJeremy Fitzhardinge 2907c2febafcSKirill A. Shutemov static int apply_to_p4d_range(struct mm_struct *mm, pgd_t *pgd, 2908c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 2909e80d3909SJoerg Roedel pte_fn_t fn, void *data, bool create, 2910e80d3909SJoerg Roedel pgtbl_mod_mask *mask) 2911c2febafcSKirill A. Shutemov { 2912c2febafcSKirill A. Shutemov p4d_t *p4d; 2913c2febafcSKirill A. Shutemov unsigned long next; 2914be1db475SDaniel Axtens int err = 0; 2915c2febafcSKirill A. Shutemov 2916be1db475SDaniel Axtens if (create) { 2917e80d3909SJoerg Roedel p4d = p4d_alloc_track(mm, pgd, addr, mask); 2918c2febafcSKirill A. Shutemov if (!p4d) 2919c2febafcSKirill A. Shutemov return -ENOMEM; 2920be1db475SDaniel Axtens } else { 2921be1db475SDaniel Axtens p4d = p4d_offset(pgd, addr); 2922be1db475SDaniel Axtens } 2923c2febafcSKirill A. Shutemov do { 2924c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 29250c95cba4SNicholas Piggin if (p4d_none(*p4d) && !create) 29260c95cba4SNicholas Piggin continue; 29270c95cba4SNicholas Piggin if (WARN_ON_ONCE(p4d_leaf(*p4d))) 29280c95cba4SNicholas Piggin return -EINVAL; 29290c95cba4SNicholas Piggin if (!p4d_none(*p4d) && WARN_ON_ONCE(p4d_bad(*p4d))) { 29300c95cba4SNicholas Piggin if (!create) 29310c95cba4SNicholas Piggin continue; 29320c95cba4SNicholas Piggin p4d_clear_bad(p4d); 29330c95cba4SNicholas Piggin } 29340c95cba4SNicholas Piggin err = apply_to_pud_range(mm, p4d, addr, next, 29350c95cba4SNicholas Piggin fn, data, create, mask); 2936c2febafcSKirill A. Shutemov if (err) 2937c2febafcSKirill A. Shutemov break; 2938c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 29390c95cba4SNicholas Piggin 2940c2febafcSKirill A. Shutemov return err; 2941c2febafcSKirill A. Shutemov } 2942c2febafcSKirill A. Shutemov 2943be1db475SDaniel Axtens static int __apply_to_page_range(struct mm_struct *mm, unsigned long addr, 2944be1db475SDaniel Axtens unsigned long size, pte_fn_t fn, 2945be1db475SDaniel Axtens void *data, bool create) 2946be1db475SDaniel Axtens { 2947be1db475SDaniel Axtens pgd_t *pgd; 2948e80d3909SJoerg Roedel unsigned long start = addr, next; 2949be1db475SDaniel Axtens unsigned long end = addr + size; 2950e80d3909SJoerg Roedel pgtbl_mod_mask mask = 0; 2951be1db475SDaniel Axtens int err = 0; 2952be1db475SDaniel Axtens 2953be1db475SDaniel Axtens if (WARN_ON(addr >= end)) 2954be1db475SDaniel Axtens return -EINVAL; 2955be1db475SDaniel Axtens 2956be1db475SDaniel Axtens pgd = pgd_offset(mm, addr); 2957be1db475SDaniel Axtens do { 2958be1db475SDaniel Axtens next = pgd_addr_end(addr, end); 29590c95cba4SNicholas Piggin if (pgd_none(*pgd) && !create) 2960be1db475SDaniel Axtens continue; 29610c95cba4SNicholas Piggin if (WARN_ON_ONCE(pgd_leaf(*pgd))) 29620c95cba4SNicholas Piggin return -EINVAL; 29630c95cba4SNicholas Piggin if (!pgd_none(*pgd) && WARN_ON_ONCE(pgd_bad(*pgd))) { 29640c95cba4SNicholas Piggin if (!create) 29650c95cba4SNicholas Piggin continue; 29660c95cba4SNicholas Piggin pgd_clear_bad(pgd); 29670c95cba4SNicholas Piggin } 29680c95cba4SNicholas Piggin err = apply_to_p4d_range(mm, pgd, addr, next, 29690c95cba4SNicholas Piggin fn, data, create, &mask); 2970be1db475SDaniel Axtens if (err) 2971be1db475SDaniel Axtens break; 2972be1db475SDaniel Axtens } while (pgd++, addr = next, addr != end); 2973be1db475SDaniel Axtens 2974e80d3909SJoerg Roedel if (mask & ARCH_PAGE_TABLE_SYNC_MASK) 2975e80d3909SJoerg Roedel arch_sync_kernel_mappings(start, start + size); 2976e80d3909SJoerg Roedel 2977be1db475SDaniel Axtens return err; 2978be1db475SDaniel Axtens } 2979be1db475SDaniel Axtens 2980aee16b3cSJeremy Fitzhardinge /* 2981aee16b3cSJeremy Fitzhardinge * Scan a region of virtual memory, filling in page tables as necessary 2982aee16b3cSJeremy Fitzhardinge * and calling a provided function on each leaf page table. 2983aee16b3cSJeremy Fitzhardinge */ 2984aee16b3cSJeremy Fitzhardinge int apply_to_page_range(struct mm_struct *mm, unsigned long addr, 2985aee16b3cSJeremy Fitzhardinge unsigned long size, pte_fn_t fn, void *data) 2986aee16b3cSJeremy Fitzhardinge { 2987be1db475SDaniel Axtens return __apply_to_page_range(mm, addr, size, fn, data, true); 2988aee16b3cSJeremy Fitzhardinge } 2989aee16b3cSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(apply_to_page_range); 2990aee16b3cSJeremy Fitzhardinge 29911da177e4SLinus Torvalds /* 2992be1db475SDaniel Axtens * Scan a region of virtual memory, calling a provided function on 2993be1db475SDaniel Axtens * each leaf page table where it exists. 2994be1db475SDaniel Axtens * 2995be1db475SDaniel Axtens * Unlike apply_to_page_range, this does _not_ fill in page tables 2996be1db475SDaniel Axtens * where they are absent. 2997be1db475SDaniel Axtens */ 2998be1db475SDaniel Axtens int apply_to_existing_page_range(struct mm_struct *mm, unsigned long addr, 2999be1db475SDaniel Axtens unsigned long size, pte_fn_t fn, void *data) 3000be1db475SDaniel Axtens { 3001be1db475SDaniel Axtens return __apply_to_page_range(mm, addr, size, fn, data, false); 3002be1db475SDaniel Axtens } 3003be1db475SDaniel Axtens EXPORT_SYMBOL_GPL(apply_to_existing_page_range); 3004be1db475SDaniel Axtens 3005be1db475SDaniel Axtens /* 30069b4bdd2fSKirill A. Shutemov * handle_pte_fault chooses page fault handler according to an entry which was 30079b4bdd2fSKirill A. Shutemov * read non-atomically. Before making any commitment, on those architectures 30089b4bdd2fSKirill A. Shutemov * or configurations (e.g. i386 with PAE) which might give a mix of unmatched 30099b4bdd2fSKirill A. Shutemov * parts, do_swap_page must check under lock before unmapping the pte and 30109b4bdd2fSKirill A. Shutemov * proceeding (but do_wp_page is only called after already making such a check; 3011a335b2e1SRyota Ozaki * and do_anonymous_page can safely check later on). 30128f4e2101SHugh Dickins */ 30132ca99358SPeter Xu static inline int pte_unmap_same(struct vm_fault *vmf) 30148f4e2101SHugh Dickins { 30158f4e2101SHugh Dickins int same = 1; 3016923717cbSThomas Gleixner #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPTION) 30178f4e2101SHugh Dickins if (sizeof(pte_t) > sizeof(unsigned long)) { 3018c7ad0880SHugh Dickins spin_lock(vmf->ptl); 3019c33c7948SRyan Roberts same = pte_same(ptep_get(vmf->pte), vmf->orig_pte); 3020c7ad0880SHugh Dickins spin_unlock(vmf->ptl); 30218f4e2101SHugh Dickins } 30228f4e2101SHugh Dickins #endif 30232ca99358SPeter Xu pte_unmap(vmf->pte); 30242ca99358SPeter Xu vmf->pte = NULL; 30258f4e2101SHugh Dickins return same; 30268f4e2101SHugh Dickins } 30278f4e2101SHugh Dickins 3028a873dfe1STony Luck /* 3029a873dfe1STony Luck * Return: 3030a873dfe1STony Luck * 0: copied succeeded 3031a873dfe1STony Luck * -EHWPOISON: copy failed due to hwpoison in source page 3032a873dfe1STony Luck * -EAGAIN: copied failed (some other reason) 3033a873dfe1STony Luck */ 3034a873dfe1STony Luck static inline int __wp_page_copy_user(struct page *dst, struct page *src, 303583d116c5SJia He struct vm_fault *vmf) 30366aab341eSLinus Torvalds { 3037a873dfe1STony Luck int ret; 303883d116c5SJia He void *kaddr; 303983d116c5SJia He void __user *uaddr; 304083d116c5SJia He struct vm_area_struct *vma = vmf->vma; 304183d116c5SJia He struct mm_struct *mm = vma->vm_mm; 304283d116c5SJia He unsigned long addr = vmf->address; 304383d116c5SJia He 304483d116c5SJia He if (likely(src)) { 3045d302c239STony Luck if (copy_mc_user_highpage(dst, src, addr, vma)) { 3046d302c239STony Luck memory_failure_queue(page_to_pfn(src), 0); 3047a873dfe1STony Luck return -EHWPOISON; 3048d302c239STony Luck } 3049a873dfe1STony Luck return 0; 305083d116c5SJia He } 305183d116c5SJia He 30526aab341eSLinus Torvalds /* 30536aab341eSLinus Torvalds * If the source page was a PFN mapping, we don't have 30546aab341eSLinus Torvalds * a "struct page" for it. We do a best-effort copy by 30556aab341eSLinus Torvalds * just copying from the original user address. If that 30566aab341eSLinus Torvalds * fails, we just zero-fill it. Live with it. 30576aab341eSLinus Torvalds */ 305824d2613aSFabio M. De Francesco kaddr = kmap_local_page(dst); 305924d2613aSFabio M. De Francesco pagefault_disable(); 306083d116c5SJia He uaddr = (void __user *)(addr & PAGE_MASK); 306183d116c5SJia He 306283d116c5SJia He /* 306383d116c5SJia He * On architectures with software "accessed" bits, we would 306483d116c5SJia He * take a double page fault, so mark it accessed here. 306583d116c5SJia He */ 30663db82b93SHugh Dickins vmf->pte = NULL; 3067e1fd09e3SYu Zhao if (!arch_has_hw_pte_young() && !pte_young(vmf->orig_pte)) { 306883d116c5SJia He pte_t entry; 306983d116c5SJia He 307083d116c5SJia He vmf->pte = pte_offset_map_lock(mm, vmf->pmd, addr, &vmf->ptl); 3071c33c7948SRyan Roberts if (unlikely(!vmf->pte || !pte_same(ptep_get(vmf->pte), vmf->orig_pte))) { 307283d116c5SJia He /* 307383d116c5SJia He * Other thread has already handled the fault 30747df67697SBibo Mao * and update local tlb only 307583d116c5SJia He */ 3076a92cbb82SHugh Dickins if (vmf->pte) 30777df67697SBibo Mao update_mmu_tlb(vma, addr, vmf->pte); 3078a873dfe1STony Luck ret = -EAGAIN; 307983d116c5SJia He goto pte_unlock; 308083d116c5SJia He } 308183d116c5SJia He 308283d116c5SJia He entry = pte_mkyoung(vmf->orig_pte); 308383d116c5SJia He if (ptep_set_access_flags(vma, addr, vmf->pte, entry, 0)) 30845003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vma, addr, vmf->pte, 1); 308583d116c5SJia He } 30865d2a2dbbSLinus Torvalds 30875d2a2dbbSLinus Torvalds /* 30885d2a2dbbSLinus Torvalds * This really shouldn't fail, because the page is there 30895d2a2dbbSLinus Torvalds * in the page tables. But it might just be unreadable, 30905d2a2dbbSLinus Torvalds * in which case we just give up and fill the result with 30915d2a2dbbSLinus Torvalds * zeroes. 30925d2a2dbbSLinus Torvalds */ 309383d116c5SJia He if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) { 30943db82b93SHugh Dickins if (vmf->pte) 3095c3e5ea6eSKirill A. Shutemov goto warn; 3096c3e5ea6eSKirill A. Shutemov 3097c3e5ea6eSKirill A. Shutemov /* Re-validate under PTL if the page is still mapped */ 3098c3e5ea6eSKirill A. Shutemov vmf->pte = pte_offset_map_lock(mm, vmf->pmd, addr, &vmf->ptl); 3099c33c7948SRyan Roberts if (unlikely(!vmf->pte || !pte_same(ptep_get(vmf->pte), vmf->orig_pte))) { 31007df67697SBibo Mao /* The PTE changed under us, update local tlb */ 3101a92cbb82SHugh Dickins if (vmf->pte) 31027df67697SBibo Mao update_mmu_tlb(vma, addr, vmf->pte); 3103a873dfe1STony Luck ret = -EAGAIN; 3104c3e5ea6eSKirill A. Shutemov goto pte_unlock; 3105c3e5ea6eSKirill A. Shutemov } 3106c3e5ea6eSKirill A. Shutemov 3107c3e5ea6eSKirill A. Shutemov /* 3108985ba004SEthon Paul * The same page can be mapped back since last copy attempt. 3109c3e5ea6eSKirill A. Shutemov * Try to copy again under PTL. 3110c3e5ea6eSKirill A. Shutemov */ 3111c3e5ea6eSKirill A. Shutemov if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) { 311283d116c5SJia He /* 311383d116c5SJia He * Give a warn in case there can be some obscure 311483d116c5SJia He * use-case 311583d116c5SJia He */ 3116c3e5ea6eSKirill A. Shutemov warn: 311783d116c5SJia He WARN_ON_ONCE(1); 31183ecb01dfSJan Beulich clear_page(kaddr); 311983d116c5SJia He } 3120c3e5ea6eSKirill A. Shutemov } 312183d116c5SJia He 3122a873dfe1STony Luck ret = 0; 312383d116c5SJia He 312483d116c5SJia He pte_unlock: 31253db82b93SHugh Dickins if (vmf->pte) 312683d116c5SJia He pte_unmap_unlock(vmf->pte, vmf->ptl); 312724d2613aSFabio M. De Francesco pagefault_enable(); 312824d2613aSFabio M. De Francesco kunmap_local(kaddr); 3129c4ec7b0dSDmitriy Monakhov flush_dcache_page(dst); 313083d116c5SJia He 313183d116c5SJia He return ret; 31326aab341eSLinus Torvalds } 31336aab341eSLinus Torvalds 3134c20cd45eSMichal Hocko static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma) 3135c20cd45eSMichal Hocko { 3136c20cd45eSMichal Hocko struct file *vm_file = vma->vm_file; 3137c20cd45eSMichal Hocko 3138c20cd45eSMichal Hocko if (vm_file) 3139c20cd45eSMichal Hocko return mapping_gfp_mask(vm_file->f_mapping) | __GFP_FS | __GFP_IO; 3140c20cd45eSMichal Hocko 3141c20cd45eSMichal Hocko /* 3142c20cd45eSMichal Hocko * Special mappings (e.g. VDSO) do not have any file so fake 3143c20cd45eSMichal Hocko * a default GFP_KERNEL for them. 3144c20cd45eSMichal Hocko */ 3145c20cd45eSMichal Hocko return GFP_KERNEL; 3146c20cd45eSMichal Hocko } 3147c20cd45eSMichal Hocko 31481da177e4SLinus Torvalds /* 3149fb09a464SKirill A. Shutemov * Notify the address space that the page is about to become writable so that 3150fb09a464SKirill A. Shutemov * it can prohibit this or wait for the page to get into an appropriate state. 3151fb09a464SKirill A. Shutemov * 3152fb09a464SKirill A. Shutemov * We do this without the lock held, so that it can sleep if it needs to. 3153fb09a464SKirill A. Shutemov */ 315486aa6998SSidhartha Kumar static vm_fault_t do_page_mkwrite(struct vm_fault *vmf, struct folio *folio) 3155fb09a464SKirill A. Shutemov { 31562b740303SSouptick Joarder vm_fault_t ret; 315738b8cb7fSJan Kara unsigned int old_flags = vmf->flags; 3158fb09a464SKirill A. Shutemov 315938b8cb7fSJan Kara vmf->flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE; 3160fb09a464SKirill A. Shutemov 3161dc617f29SDarrick J. Wong if (vmf->vma->vm_file && 3162dc617f29SDarrick J. Wong IS_SWAPFILE(vmf->vma->vm_file->f_mapping->host)) 3163dc617f29SDarrick J. Wong return VM_FAULT_SIGBUS; 3164dc617f29SDarrick J. Wong 316511bac800SDave Jiang ret = vmf->vma->vm_ops->page_mkwrite(vmf); 316638b8cb7fSJan Kara /* Restore original flags so that caller is not surprised */ 316738b8cb7fSJan Kara vmf->flags = old_flags; 3168fb09a464SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) 3169fb09a464SKirill A. Shutemov return ret; 3170fb09a464SKirill A. Shutemov if (unlikely(!(ret & VM_FAULT_LOCKED))) { 31713d243659SSidhartha Kumar folio_lock(folio); 31723d243659SSidhartha Kumar if (!folio->mapping) { 31733d243659SSidhartha Kumar folio_unlock(folio); 3174fb09a464SKirill A. Shutemov return 0; /* retry */ 3175fb09a464SKirill A. Shutemov } 3176fb09a464SKirill A. Shutemov ret |= VM_FAULT_LOCKED; 3177fb09a464SKirill A. Shutemov } else 31783d243659SSidhartha Kumar VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); 3179fb09a464SKirill A. Shutemov return ret; 3180fb09a464SKirill A. Shutemov } 3181fb09a464SKirill A. Shutemov 3182fb09a464SKirill A. Shutemov /* 318397ba0c2bSJan Kara * Handle dirtying of a page in shared file mapping on a write fault. 31844e047f89SShachar Raindel * 318597ba0c2bSJan Kara * The function expects the page to be locked and unlocks it. 31864e047f89SShachar Raindel */ 318789b15332SJohannes Weiner static vm_fault_t fault_dirty_shared_page(struct vm_fault *vmf) 31884e047f89SShachar Raindel { 318989b15332SJohannes Weiner struct vm_area_struct *vma = vmf->vma; 31904e047f89SShachar Raindel struct address_space *mapping; 319115b4919aSZhangPeng struct folio *folio = page_folio(vmf->page); 319297ba0c2bSJan Kara bool dirtied; 319397ba0c2bSJan Kara bool page_mkwrite = vma->vm_ops && vma->vm_ops->page_mkwrite; 31944e047f89SShachar Raindel 319515b4919aSZhangPeng dirtied = folio_mark_dirty(folio); 319615b4919aSZhangPeng VM_BUG_ON_FOLIO(folio_test_anon(folio), folio); 319797ba0c2bSJan Kara /* 319815b4919aSZhangPeng * Take a local copy of the address_space - folio.mapping may be zeroed 319915b4919aSZhangPeng * by truncate after folio_unlock(). The address_space itself remains 320015b4919aSZhangPeng * pinned by vma->vm_file's reference. We rely on folio_unlock()'s 320197ba0c2bSJan Kara * release semantics to prevent the compiler from undoing this copying. 320297ba0c2bSJan Kara */ 320315b4919aSZhangPeng mapping = folio_raw_mapping(folio); 320415b4919aSZhangPeng folio_unlock(folio); 32054e047f89SShachar Raindel 32064e047f89SShachar Raindel if (!page_mkwrite) 32074e047f89SShachar Raindel file_update_time(vma->vm_file); 320889b15332SJohannes Weiner 320989b15332SJohannes Weiner /* 321089b15332SJohannes Weiner * Throttle page dirtying rate down to writeback speed. 321189b15332SJohannes Weiner * 321289b15332SJohannes Weiner * mapping may be NULL here because some device drivers do not 321389b15332SJohannes Weiner * set page.mapping but still dirty their pages 321489b15332SJohannes Weiner * 3215c1e8d7c6SMichel Lespinasse * Drop the mmap_lock before waiting on IO, if we can. The file 321689b15332SJohannes Weiner * is pinning the mapping, as per above. 321789b15332SJohannes Weiner */ 321889b15332SJohannes Weiner if ((dirtied || page_mkwrite) && mapping) { 321989b15332SJohannes Weiner struct file *fpin; 322089b15332SJohannes Weiner 322189b15332SJohannes Weiner fpin = maybe_unlock_mmap_for_io(vmf, NULL); 322289b15332SJohannes Weiner balance_dirty_pages_ratelimited(mapping); 322389b15332SJohannes Weiner if (fpin) { 322489b15332SJohannes Weiner fput(fpin); 3225d9272525SPeter Xu return VM_FAULT_COMPLETED; 322689b15332SJohannes Weiner } 322789b15332SJohannes Weiner } 322889b15332SJohannes Weiner 322989b15332SJohannes Weiner return 0; 32304e047f89SShachar Raindel } 32314e047f89SShachar Raindel 323297ba0c2bSJan Kara /* 32334e047f89SShachar Raindel * Handle write page faults for pages that can be reused in the current vma 32344e047f89SShachar Raindel * 32354e047f89SShachar Raindel * This can happen either due to the mapping being with the VM_SHARED flag, 32364e047f89SShachar Raindel * or due to us being the last reference standing to the page. In either 32374e047f89SShachar Raindel * case, all we need to do here is to mark the page as writable and update 32384e047f89SShachar Raindel * any related book-keeping. 32394e047f89SShachar Raindel */ 3240a86bc96bSKefeng Wang static inline void wp_page_reuse(struct vm_fault *vmf, struct folio *folio) 324182b0f8c3SJan Kara __releases(vmf->ptl) 32424e047f89SShachar Raindel { 324382b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 32444e047f89SShachar Raindel pte_t entry; 32456c287605SDavid Hildenbrand 3246c89357e2SDavid Hildenbrand VM_BUG_ON(!(vmf->flags & FAULT_FLAG_WRITE)); 32476c287605SDavid Hildenbrand 3248c2c3b514SKefeng Wang if (folio) { 3249c2c3b514SKefeng Wang VM_BUG_ON(folio_test_anon(folio) && 3250c2c3b514SKefeng Wang !PageAnonExclusive(vmf->page)); 32514e047f89SShachar Raindel /* 3252c2c3b514SKefeng Wang * Clear the folio's cpupid information as the existing 32534e047f89SShachar Raindel * information potentially belongs to a now completely 32544e047f89SShachar Raindel * unrelated process. 32554e047f89SShachar Raindel */ 3256c2c3b514SKefeng Wang folio_xchg_last_cpupid(folio, (1 << LAST_CPUPID_SHIFT) - 1); 3257c2c3b514SKefeng Wang } 32584e047f89SShachar Raindel 32592994302bSJan Kara flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); 32602994302bSJan Kara entry = pte_mkyoung(vmf->orig_pte); 32614e047f89SShachar Raindel entry = maybe_mkwrite(pte_mkdirty(entry), vma); 326282b0f8c3SJan Kara if (ptep_set_access_flags(vma, vmf->address, vmf->pte, entry, 1)) 32635003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vma, vmf->address, vmf->pte, 1); 326482b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3265798a6b87SPeter Xu count_vm_event(PGREUSE); 32664e047f89SShachar Raindel } 32674e047f89SShachar Raindel 32684ed43798SMatthew Wilcox (Oracle) /* 32694ed43798SMatthew Wilcox (Oracle) * We could add a bitflag somewhere, but for now, we know that all 32704ed43798SMatthew Wilcox (Oracle) * vm_ops that have a ->map_pages have been audited and don't need 32714ed43798SMatthew Wilcox (Oracle) * the mmap_lock to be held. 32724ed43798SMatthew Wilcox (Oracle) */ 32734ed43798SMatthew Wilcox (Oracle) static inline vm_fault_t vmf_can_call_fault(const struct vm_fault *vmf) 32744ed43798SMatthew Wilcox (Oracle) { 32754ed43798SMatthew Wilcox (Oracle) struct vm_area_struct *vma = vmf->vma; 32764ed43798SMatthew Wilcox (Oracle) 32774ed43798SMatthew Wilcox (Oracle) if (vma->vm_ops->map_pages || !(vmf->flags & FAULT_FLAG_VMA_LOCK)) 32784ed43798SMatthew Wilcox (Oracle) return 0; 32794ed43798SMatthew Wilcox (Oracle) vma_end_read(vma); 32804ed43798SMatthew Wilcox (Oracle) return VM_FAULT_RETRY; 32814ed43798SMatthew Wilcox (Oracle) } 32824ed43798SMatthew Wilcox (Oracle) 3283997f0ecbSVishal Moola (Oracle) vm_fault_t vmf_anon_prepare(struct vm_fault *vmf) 3284164b06f2SMatthew Wilcox (Oracle) { 3285164b06f2SMatthew Wilcox (Oracle) struct vm_area_struct *vma = vmf->vma; 3286164b06f2SMatthew Wilcox (Oracle) 3287164b06f2SMatthew Wilcox (Oracle) if (likely(vma->anon_vma)) 3288164b06f2SMatthew Wilcox (Oracle) return 0; 3289164b06f2SMatthew Wilcox (Oracle) if (vmf->flags & FAULT_FLAG_VMA_LOCK) { 3290164b06f2SMatthew Wilcox (Oracle) vma_end_read(vma); 3291164b06f2SMatthew Wilcox (Oracle) return VM_FAULT_RETRY; 3292164b06f2SMatthew Wilcox (Oracle) } 3293164b06f2SMatthew Wilcox (Oracle) if (__anon_vma_prepare(vma)) 3294164b06f2SMatthew Wilcox (Oracle) return VM_FAULT_OOM; 3295164b06f2SMatthew Wilcox (Oracle) return 0; 3296164b06f2SMatthew Wilcox (Oracle) } 3297164b06f2SMatthew Wilcox (Oracle) 32984e047f89SShachar Raindel /* 3299c89357e2SDavid Hildenbrand * Handle the case of a page which we actually need to copy to a new page, 3300c89357e2SDavid Hildenbrand * either due to COW or unsharing. 33012f38ab2cSShachar Raindel * 3302c1e8d7c6SMichel Lespinasse * Called with mmap_lock locked and the old page referenced, but 33032f38ab2cSShachar Raindel * without the ptl held. 33042f38ab2cSShachar Raindel * 33052f38ab2cSShachar Raindel * High level logic flow: 33062f38ab2cSShachar Raindel * 33072f38ab2cSShachar Raindel * - Allocate a page, copy the content of the old page to the new one. 33082f38ab2cSShachar Raindel * - Handle book keeping and accounting - cgroups, mmu-notifiers, etc. 33092f38ab2cSShachar Raindel * - Take the PTL. If the pte changed, bail out and release the allocated page 33102f38ab2cSShachar Raindel * - If the pte is still the way we remember it, update the page table and all 33112f38ab2cSShachar Raindel * relevant references. This includes dropping the reference the page-table 33122f38ab2cSShachar Raindel * held to the old page, as well as updating the rmap. 33132f38ab2cSShachar Raindel * - In any case, unlock the PTL and drop the reference we took to the old page. 33142f38ab2cSShachar Raindel */ 33152b740303SSouptick Joarder static vm_fault_t wp_page_copy(struct vm_fault *vmf) 33162f38ab2cSShachar Raindel { 3317c89357e2SDavid Hildenbrand const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE; 331882b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3319bae473a4SKirill A. Shutemov struct mm_struct *mm = vma->vm_mm; 332028d41a48SMatthew Wilcox (Oracle) struct folio *old_folio = NULL; 332128d41a48SMatthew Wilcox (Oracle) struct folio *new_folio = NULL; 33222f38ab2cSShachar Raindel pte_t entry; 33232f38ab2cSShachar Raindel int page_copied = 0; 3324ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 3325164b06f2SMatthew Wilcox (Oracle) vm_fault_t ret; 3326cf503cc6SKefeng Wang bool pfn_is_zero; 33272f38ab2cSShachar Raindel 3328662ce1dcSYang Yang delayacct_wpcopy_start(); 3329662ce1dcSYang Yang 333028d41a48SMatthew Wilcox (Oracle) if (vmf->page) 333128d41a48SMatthew Wilcox (Oracle) old_folio = page_folio(vmf->page); 3332164b06f2SMatthew Wilcox (Oracle) ret = vmf_anon_prepare(vmf); 3333164b06f2SMatthew Wilcox (Oracle) if (unlikely(ret)) 3334164b06f2SMatthew Wilcox (Oracle) goto out; 33352f38ab2cSShachar Raindel 3336cf503cc6SKefeng Wang pfn_is_zero = is_zero_pfn(pte_pfn(vmf->orig_pte)); 3337cf503cc6SKefeng Wang new_folio = folio_prealloc(mm, vma, vmf->address, pfn_is_zero); 33386bc56a4dSMatthew Wilcox (Oracle) if (!new_folio) 33392f38ab2cSShachar Raindel goto oom; 3340cf503cc6SKefeng Wang 3341cf503cc6SKefeng Wang if (!pfn_is_zero) { 3342164b06f2SMatthew Wilcox (Oracle) int err; 334383d116c5SJia He 3344164b06f2SMatthew Wilcox (Oracle) err = __wp_page_copy_user(&new_folio->page, vmf->page, vmf); 3345164b06f2SMatthew Wilcox (Oracle) if (err) { 334683d116c5SJia He /* 334783d116c5SJia He * COW failed, if the fault was solved by other, 334883d116c5SJia He * it's fine. If not, userspace would re-fault on 334983d116c5SJia He * the same address and we will handle the fault 335083d116c5SJia He * from the second attempt. 3351a873dfe1STony Luck * The -EHWPOISON case will not be retried. 335283d116c5SJia He */ 335328d41a48SMatthew Wilcox (Oracle) folio_put(new_folio); 335428d41a48SMatthew Wilcox (Oracle) if (old_folio) 335528d41a48SMatthew Wilcox (Oracle) folio_put(old_folio); 3356662ce1dcSYang Yang 3357662ce1dcSYang Yang delayacct_wpcopy_end(); 3358164b06f2SMatthew Wilcox (Oracle) return err == -EHWPOISON ? VM_FAULT_HWPOISON : 0; 335983d116c5SJia He } 336028d41a48SMatthew Wilcox (Oracle) kmsan_copy_page_meta(&new_folio->page, vmf->page); 33612f38ab2cSShachar Raindel } 33622f38ab2cSShachar Raindel 336328d41a48SMatthew Wilcox (Oracle) __folio_mark_uptodate(new_folio); 3364eb3c24f3SMel Gorman 33657d4a8be0SAlistair Popple mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, 33666f4f13e8SJérôme Glisse vmf->address & PAGE_MASK, 3367ac46d4f3SJérôme Glisse (vmf->address & PAGE_MASK) + PAGE_SIZE); 3368ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 33692f38ab2cSShachar Raindel 33702f38ab2cSShachar Raindel /* 33712f38ab2cSShachar Raindel * Re-check the pte - we dropped the lock 33722f38ab2cSShachar Raindel */ 337382b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(mm, vmf->pmd, vmf->address, &vmf->ptl); 3374c33c7948SRyan Roberts if (likely(vmf->pte && pte_same(ptep_get(vmf->pte), vmf->orig_pte))) { 337528d41a48SMatthew Wilcox (Oracle) if (old_folio) { 337628d41a48SMatthew Wilcox (Oracle) if (!folio_test_anon(old_folio)) { 33776b27cc6cSKefeng Wang dec_mm_counter(mm, mm_counter_file(old_folio)); 3378f1a79412SShakeel Butt inc_mm_counter(mm, MM_ANONPAGES); 33792f38ab2cSShachar Raindel } 33802f38ab2cSShachar Raindel } else { 33816080d19fSxu xin ksm_might_unmap_zero_page(mm, vmf->orig_pte); 3382f1a79412SShakeel Butt inc_mm_counter(mm, MM_ANONPAGES); 33832f38ab2cSShachar Raindel } 33842994302bSJan Kara flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); 338528d41a48SMatthew Wilcox (Oracle) entry = mk_pte(&new_folio->page, vma->vm_page_prot); 338650c25ee9SThomas Bogendoerfer entry = pte_sw_mkyoung(entry); 3387c89357e2SDavid Hildenbrand if (unlikely(unshare)) { 3388c89357e2SDavid Hildenbrand if (pte_soft_dirty(vmf->orig_pte)) 3389c89357e2SDavid Hildenbrand entry = pte_mksoft_dirty(entry); 3390c89357e2SDavid Hildenbrand if (pte_uffd_wp(vmf->orig_pte)) 3391c89357e2SDavid Hildenbrand entry = pte_mkuffd_wp(entry); 3392c89357e2SDavid Hildenbrand } else { 33932f38ab2cSShachar Raindel entry = maybe_mkwrite(pte_mkdirty(entry), vma); 3394c89357e2SDavid Hildenbrand } 3395111fe718SNicholas Piggin 33962f38ab2cSShachar Raindel /* 33972f38ab2cSShachar Raindel * Clear the pte entry and flush it first, before updating the 3398111fe718SNicholas Piggin * pte with the new entry, to keep TLBs on different CPUs in 3399111fe718SNicholas Piggin * sync. This code used to set the new PTE then flush TLBs, but 3400111fe718SNicholas Piggin * that left a window where the new PTE could be loaded into 3401111fe718SNicholas Piggin * some TLBs while the old PTE remains in others. 34022f38ab2cSShachar Raindel */ 3403ec8832d0SAlistair Popple ptep_clear_flush(vma, vmf->address, vmf->pte); 340428d41a48SMatthew Wilcox (Oracle) folio_add_new_anon_rmap(new_folio, vma, vmf->address); 340528d41a48SMatthew Wilcox (Oracle) folio_add_lru_vma(new_folio, vma); 34062f38ab2cSShachar Raindel /* 34072f38ab2cSShachar Raindel * We call the notify macro here because, when using secondary 34082f38ab2cSShachar Raindel * mmu page tables (such as kvm shadow page tables), we want the 34092f38ab2cSShachar Raindel * new page to be mapped directly into the secondary page table. 34102f38ab2cSShachar Raindel */ 3411c89357e2SDavid Hildenbrand BUG_ON(unshare && pte_write(entry)); 341282b0f8c3SJan Kara set_pte_at_notify(mm, vmf->address, vmf->pte, entry); 34135003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vma, vmf->address, vmf->pte, 1); 341428d41a48SMatthew Wilcox (Oracle) if (old_folio) { 34152f38ab2cSShachar Raindel /* 34162f38ab2cSShachar Raindel * Only after switching the pte to the new page may 34172f38ab2cSShachar Raindel * we remove the mapcount here. Otherwise another 34182f38ab2cSShachar Raindel * process may come and find the rmap count decremented 34192f38ab2cSShachar Raindel * before the pte is switched to the new page, and 34202f38ab2cSShachar Raindel * "reuse" the old page writing into it while our pte 34212f38ab2cSShachar Raindel * here still points into it and can be read by other 34222f38ab2cSShachar Raindel * threads. 34232f38ab2cSShachar Raindel * 34242f38ab2cSShachar Raindel * The critical issue is to order this 3425c4626503SDavid Hildenbrand * folio_remove_rmap_pte() with the ptp_clear_flush 3426c4626503SDavid Hildenbrand * above. Those stores are ordered by (if nothing else,) 34272f38ab2cSShachar Raindel * the barrier present in the atomic_add_negative 3428c4626503SDavid Hildenbrand * in folio_remove_rmap_pte(); 34292f38ab2cSShachar Raindel * 34302f38ab2cSShachar Raindel * Then the TLB flush in ptep_clear_flush ensures that 34312f38ab2cSShachar Raindel * no process can access the old page before the 34322f38ab2cSShachar Raindel * decremented mapcount is visible. And the old page 34332f38ab2cSShachar Raindel * cannot be reused until after the decremented 34342f38ab2cSShachar Raindel * mapcount is visible. So transitively, TLBs to 34352f38ab2cSShachar Raindel * old page will be flushed before it can be reused. 34362f38ab2cSShachar Raindel */ 3437c4626503SDavid Hildenbrand folio_remove_rmap_pte(old_folio, vmf->page, vma); 34382f38ab2cSShachar Raindel } 34392f38ab2cSShachar Raindel 34402f38ab2cSShachar Raindel /* Free the old page.. */ 344128d41a48SMatthew Wilcox (Oracle) new_folio = old_folio; 34422f38ab2cSShachar Raindel page_copied = 1; 34433db82b93SHugh Dickins pte_unmap_unlock(vmf->pte, vmf->ptl); 34443db82b93SHugh Dickins } else if (vmf->pte) { 34457df67697SBibo Mao update_mmu_tlb(vma, vmf->address, vmf->pte); 34463db82b93SHugh Dickins pte_unmap_unlock(vmf->pte, vmf->ptl); 34472f38ab2cSShachar Raindel } 34482f38ab2cSShachar Raindel 3449ec8832d0SAlistair Popple mmu_notifier_invalidate_range_end(&range); 34503db82b93SHugh Dickins 34513db82b93SHugh Dickins if (new_folio) 34523db82b93SHugh Dickins folio_put(new_folio); 345328d41a48SMatthew Wilcox (Oracle) if (old_folio) { 3454f4c4a3f4SHuang Ying if (page_copied) 3455*63b77499SMatthew Wilcox (Oracle) free_swap_cache(old_folio); 345628d41a48SMatthew Wilcox (Oracle) folio_put(old_folio); 34572f38ab2cSShachar Raindel } 3458662ce1dcSYang Yang 3459662ce1dcSYang Yang delayacct_wpcopy_end(); 3460cb8d8633SDavid Hildenbrand return 0; 34612f38ab2cSShachar Raindel oom: 3462164b06f2SMatthew Wilcox (Oracle) ret = VM_FAULT_OOM; 3463164b06f2SMatthew Wilcox (Oracle) out: 346428d41a48SMatthew Wilcox (Oracle) if (old_folio) 346528d41a48SMatthew Wilcox (Oracle) folio_put(old_folio); 3466662ce1dcSYang Yang 3467662ce1dcSYang Yang delayacct_wpcopy_end(); 3468164b06f2SMatthew Wilcox (Oracle) return ret; 34692f38ab2cSShachar Raindel } 34702f38ab2cSShachar Raindel 347166a6197cSJan Kara /** 347266a6197cSJan Kara * finish_mkwrite_fault - finish page fault for a shared mapping, making PTE 347366a6197cSJan Kara * writeable once the page is prepared 347466a6197cSJan Kara * 347566a6197cSJan Kara * @vmf: structure describing the fault 3476a86bc96bSKefeng Wang * @folio: the folio of vmf->page 347766a6197cSJan Kara * 347866a6197cSJan Kara * This function handles all that is needed to finish a write page fault in a 347966a6197cSJan Kara * shared mapping due to PTE being read-only once the mapped page is prepared. 3480a862f68aSMike Rapoport * It handles locking of PTE and modifying it. 348166a6197cSJan Kara * 348266a6197cSJan Kara * The function expects the page to be locked or other protection against 348366a6197cSJan Kara * concurrent faults / writeback (such as DAX radix tree locks). 3484a862f68aSMike Rapoport * 34852797e79fSLiu Xiang * Return: %0 on success, %VM_FAULT_NOPAGE when PTE got changed before 3486a862f68aSMike Rapoport * we acquired PTE lock. 348766a6197cSJan Kara */ 3488a86bc96bSKefeng Wang static vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf, struct folio *folio) 348966a6197cSJan Kara { 349066a6197cSJan Kara WARN_ON_ONCE(!(vmf->vma->vm_flags & VM_SHARED)); 349166a6197cSJan Kara vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, vmf->address, 349266a6197cSJan Kara &vmf->ptl); 34933db82b93SHugh Dickins if (!vmf->pte) 34943db82b93SHugh Dickins return VM_FAULT_NOPAGE; 349566a6197cSJan Kara /* 349666a6197cSJan Kara * We might have raced with another page fault while we released the 349766a6197cSJan Kara * pte_offset_map_lock. 349866a6197cSJan Kara */ 3499c33c7948SRyan Roberts if (!pte_same(ptep_get(vmf->pte), vmf->orig_pte)) { 35007df67697SBibo Mao update_mmu_tlb(vmf->vma, vmf->address, vmf->pte); 350166a6197cSJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3502a19e2553SJan Kara return VM_FAULT_NOPAGE; 350366a6197cSJan Kara } 3504a86bc96bSKefeng Wang wp_page_reuse(vmf, folio); 3505a19e2553SJan Kara return 0; 350666a6197cSJan Kara } 350766a6197cSJan Kara 3508dd906184SBoaz Harrosh /* 3509dd906184SBoaz Harrosh * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED 3510dd906184SBoaz Harrosh * mapping 3511dd906184SBoaz Harrosh */ 35122b740303SSouptick Joarder static vm_fault_t wp_pfn_shared(struct vm_fault *vmf) 3513dd906184SBoaz Harrosh { 351482b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3515bae473a4SKirill A. Shutemov 3516dd906184SBoaz Harrosh if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) { 35172b740303SSouptick Joarder vm_fault_t ret; 3518dd906184SBoaz Harrosh 351982b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 35204a68fef1SMatthew Wilcox (Oracle) ret = vmf_can_call_fault(vmf); 35214a68fef1SMatthew Wilcox (Oracle) if (ret) 35224a68fef1SMatthew Wilcox (Oracle) return ret; 3523063e60d8SMatthew Wilcox (Oracle) 3524fe82221fSJan Kara vmf->flags |= FAULT_FLAG_MKWRITE; 352511bac800SDave Jiang ret = vma->vm_ops->pfn_mkwrite(vmf); 35262f89dc12SJan Kara if (ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)) 3527dd906184SBoaz Harrosh return ret; 3528a86bc96bSKefeng Wang return finish_mkwrite_fault(vmf, NULL); 3529dd906184SBoaz Harrosh } 3530a86bc96bSKefeng Wang wp_page_reuse(vmf, NULL); 3531cb8d8633SDavid Hildenbrand return 0; 3532dd906184SBoaz Harrosh } 3533dd906184SBoaz Harrosh 35345a97858bSSidhartha Kumar static vm_fault_t wp_page_shared(struct vm_fault *vmf, struct folio *folio) 353582b0f8c3SJan Kara __releases(vmf->ptl) 353693e478d4SShachar Raindel { 353782b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3538cb8d8633SDavid Hildenbrand vm_fault_t ret = 0; 353993e478d4SShachar Raindel 35405a97858bSSidhartha Kumar folio_get(folio); 354193e478d4SShachar Raindel 354293e478d4SShachar Raindel if (vma->vm_ops && vma->vm_ops->page_mkwrite) { 35432b740303SSouptick Joarder vm_fault_t tmp; 354493e478d4SShachar Raindel 354582b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 35464a68fef1SMatthew Wilcox (Oracle) tmp = vmf_can_call_fault(vmf); 35474a68fef1SMatthew Wilcox (Oracle) if (tmp) { 3548063e60d8SMatthew Wilcox (Oracle) folio_put(folio); 35494a68fef1SMatthew Wilcox (Oracle) return tmp; 3550063e60d8SMatthew Wilcox (Oracle) } 3551063e60d8SMatthew Wilcox (Oracle) 355286aa6998SSidhartha Kumar tmp = do_page_mkwrite(vmf, folio); 355393e478d4SShachar Raindel if (unlikely(!tmp || (tmp & 355493e478d4SShachar Raindel (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { 35555a97858bSSidhartha Kumar folio_put(folio); 355693e478d4SShachar Raindel return tmp; 355793e478d4SShachar Raindel } 3558a86bc96bSKefeng Wang tmp = finish_mkwrite_fault(vmf, folio); 3559a19e2553SJan Kara if (unlikely(tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) { 35605a97858bSSidhartha Kumar folio_unlock(folio); 35615a97858bSSidhartha Kumar folio_put(folio); 356266a6197cSJan Kara return tmp; 356393e478d4SShachar Raindel } 356466a6197cSJan Kara } else { 3565a86bc96bSKefeng Wang wp_page_reuse(vmf, folio); 35665a97858bSSidhartha Kumar folio_lock(folio); 356793e478d4SShachar Raindel } 356889b15332SJohannes Weiner ret |= fault_dirty_shared_page(vmf); 35695a97858bSSidhartha Kumar folio_put(folio); 357093e478d4SShachar Raindel 357189b15332SJohannes Weiner return ret; 357293e478d4SShachar Raindel } 357393e478d4SShachar Raindel 3574dec078ccSDavid Hildenbrand static bool wp_can_reuse_anon_folio(struct folio *folio, 3575dec078ccSDavid Hildenbrand struct vm_area_struct *vma) 3576dec078ccSDavid Hildenbrand { 3577dec078ccSDavid Hildenbrand /* 3578dec078ccSDavid Hildenbrand * We have to verify under folio lock: these early checks are 3579dec078ccSDavid Hildenbrand * just an optimization to avoid locking the folio and freeing 3580dec078ccSDavid Hildenbrand * the swapcache if there is little hope that we can reuse. 3581dec078ccSDavid Hildenbrand * 3582dec078ccSDavid Hildenbrand * KSM doesn't necessarily raise the folio refcount. 3583dec078ccSDavid Hildenbrand */ 3584dec078ccSDavid Hildenbrand if (folio_test_ksm(folio) || folio_ref_count(folio) > 3) 3585dec078ccSDavid Hildenbrand return false; 3586dec078ccSDavid Hildenbrand if (!folio_test_lru(folio)) 3587dec078ccSDavid Hildenbrand /* 3588dec078ccSDavid Hildenbrand * We cannot easily detect+handle references from 3589dec078ccSDavid Hildenbrand * remote LRU caches or references to LRU folios. 3590dec078ccSDavid Hildenbrand */ 3591dec078ccSDavid Hildenbrand lru_add_drain(); 3592dec078ccSDavid Hildenbrand if (folio_ref_count(folio) > 1 + folio_test_swapcache(folio)) 3593dec078ccSDavid Hildenbrand return false; 3594dec078ccSDavid Hildenbrand if (!folio_trylock(folio)) 3595dec078ccSDavid Hildenbrand return false; 3596dec078ccSDavid Hildenbrand if (folio_test_swapcache(folio)) 3597dec078ccSDavid Hildenbrand folio_free_swap(folio); 3598dec078ccSDavid Hildenbrand if (folio_test_ksm(folio) || folio_ref_count(folio) != 1) { 3599dec078ccSDavid Hildenbrand folio_unlock(folio); 3600dec078ccSDavid Hildenbrand return false; 3601dec078ccSDavid Hildenbrand } 3602dec078ccSDavid Hildenbrand /* 3603dec078ccSDavid Hildenbrand * Ok, we've got the only folio reference from our mapping 3604dec078ccSDavid Hildenbrand * and the folio is locked, it's dark out, and we're wearing 3605dec078ccSDavid Hildenbrand * sunglasses. Hit it. 3606dec078ccSDavid Hildenbrand */ 3607dec078ccSDavid Hildenbrand folio_move_anon_rmap(folio, vma); 3608dec078ccSDavid Hildenbrand folio_unlock(folio); 3609dec078ccSDavid Hildenbrand return true; 3610dec078ccSDavid Hildenbrand } 3611dec078ccSDavid Hildenbrand 36122f38ab2cSShachar Raindel /* 3613c89357e2SDavid Hildenbrand * This routine handles present pages, when 3614c89357e2SDavid Hildenbrand * * users try to write to a shared page (FAULT_FLAG_WRITE) 3615c89357e2SDavid Hildenbrand * * GUP wants to take a R/O pin on a possibly shared anonymous page 3616c89357e2SDavid Hildenbrand * (FAULT_FLAG_UNSHARE) 3617c89357e2SDavid Hildenbrand * 3618c89357e2SDavid Hildenbrand * It is done by copying the page to a new address and decrementing the 3619c89357e2SDavid Hildenbrand * shared-page counter for the old page. 36201da177e4SLinus Torvalds * 36211da177e4SLinus Torvalds * Note that this routine assumes that the protection checks have been 36221da177e4SLinus Torvalds * done by the caller (the low-level page fault routine in most cases). 3623c89357e2SDavid Hildenbrand * Thus, with FAULT_FLAG_WRITE, we can safely just mark it writable once we've 3624c89357e2SDavid Hildenbrand * done any necessary COW. 36251da177e4SLinus Torvalds * 3626c89357e2SDavid Hildenbrand * In case of FAULT_FLAG_WRITE, we also mark the page dirty at this point even 3627c89357e2SDavid Hildenbrand * though the page will change only once the write actually happens. This 3628c89357e2SDavid Hildenbrand * avoids a few races, and potentially makes it more efficient. 36291da177e4SLinus Torvalds * 3630c1e8d7c6SMichel Lespinasse * We enter with non-exclusive mmap_lock (to exclude vma changes, 36318f4e2101SHugh Dickins * but allow concurrent faults), with pte both mapped and locked. 3632c1e8d7c6SMichel Lespinasse * We return with mmap_lock still held, but pte unmapped and unlocked. 36331da177e4SLinus Torvalds */ 36342b740303SSouptick Joarder static vm_fault_t do_wp_page(struct vm_fault *vmf) 363582b0f8c3SJan Kara __releases(vmf->ptl) 36361da177e4SLinus Torvalds { 3637c89357e2SDavid Hildenbrand const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE; 363882b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3639b9086fdeSDavid Hildenbrand struct folio *folio = NULL; 3640d61ea1cbSPeter Xu pte_t pte; 36411da177e4SLinus Torvalds 3642c89357e2SDavid Hildenbrand if (likely(!unshare)) { 3643c33c7948SRyan Roberts if (userfaultfd_pte_wp(vma, ptep_get(vmf->pte))) { 3644d61ea1cbSPeter Xu if (!userfaultfd_wp_async(vma)) { 3645529b930bSAndrea Arcangeli pte_unmap_unlock(vmf->pte, vmf->ptl); 3646529b930bSAndrea Arcangeli return handle_userfault(vmf, VM_UFFD_WP); 3647529b930bSAndrea Arcangeli } 3648529b930bSAndrea Arcangeli 36496ce64428SNadav Amit /* 3650d61ea1cbSPeter Xu * Nothing needed (cache flush, TLB invalidations, 3651d61ea1cbSPeter Xu * etc.) because we're only removing the uffd-wp bit, 3652d61ea1cbSPeter Xu * which is completely invisible to the user. 3653d61ea1cbSPeter Xu */ 3654d61ea1cbSPeter Xu pte = pte_clear_uffd_wp(ptep_get(vmf->pte)); 3655d61ea1cbSPeter Xu 3656d61ea1cbSPeter Xu set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte); 3657d61ea1cbSPeter Xu /* 3658d61ea1cbSPeter Xu * Update this to be prepared for following up CoW 3659d61ea1cbSPeter Xu * handling 3660d61ea1cbSPeter Xu */ 3661d61ea1cbSPeter Xu vmf->orig_pte = pte; 3662d61ea1cbSPeter Xu } 3663d61ea1cbSPeter Xu 3664d61ea1cbSPeter Xu /* 36656ce64428SNadav Amit * Userfaultfd write-protect can defer flushes. Ensure the TLB 36666ce64428SNadav Amit * is flushed in this case before copying. 36676ce64428SNadav Amit */ 36686ce64428SNadav Amit if (unlikely(userfaultfd_wp(vmf->vma) && 36696ce64428SNadav Amit mm_tlb_flush_pending(vmf->vma->vm_mm))) 36706ce64428SNadav Amit flush_tlb_page(vmf->vma, vmf->address); 3671c89357e2SDavid Hildenbrand } 36726ce64428SNadav Amit 3673a41b70d6SJan Kara vmf->page = vm_normal_page(vma, vmf->address, vmf->orig_pte); 3674c89357e2SDavid Hildenbrand 36755a97858bSSidhartha Kumar if (vmf->page) 36765a97858bSSidhartha Kumar folio = page_folio(vmf->page); 36775a97858bSSidhartha Kumar 3678251b97f5SPeter Zijlstra /* 3679b9086fdeSDavid Hildenbrand * Shared mapping: we are guaranteed to have VM_WRITE and 3680b9086fdeSDavid Hildenbrand * FAULT_FLAG_WRITE set at this point. 3681b9086fdeSDavid Hildenbrand */ 3682b9086fdeSDavid Hildenbrand if (vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) { 3683b9086fdeSDavid Hildenbrand /* 368464e45507SPeter Feiner * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a 368564e45507SPeter Feiner * VM_PFNMAP VMA. 3686251b97f5SPeter Zijlstra * 3687251b97f5SPeter Zijlstra * We should not cow pages in a shared writeable mapping. 3688dd906184SBoaz Harrosh * Just mark the pages writable and/or call ops->pfn_mkwrite. 3689251b97f5SPeter Zijlstra */ 3690b9086fdeSDavid Hildenbrand if (!vmf->page) 36912994302bSJan Kara return wp_pfn_shared(vmf); 36925a97858bSSidhartha Kumar return wp_page_shared(vmf, folio); 3693251b97f5SPeter Zijlstra } 36941da177e4SLinus Torvalds 3695b9086fdeSDavid Hildenbrand /* 3696b9086fdeSDavid Hildenbrand * Private mapping: create an exclusive anonymous page copy if reuse 3697b9086fdeSDavid Hildenbrand * is impossible. We might miss VM_WRITE for FOLL_FORCE handling. 369853a05ad9SDavid Hildenbrand * 3699dec078ccSDavid Hildenbrand * If we encounter a page that is marked exclusive, we must reuse 3700dec078ccSDavid Hildenbrand * the page without further checks. 370153a05ad9SDavid Hildenbrand */ 3702dec078ccSDavid Hildenbrand if (folio && folio_test_anon(folio) && 3703dec078ccSDavid Hildenbrand (PageAnonExclusive(vmf->page) || wp_can_reuse_anon_folio(folio, vma))) { 3704dec078ccSDavid Hildenbrand if (!PageAnonExclusive(vmf->page)) 37055ca43289SDavid Hildenbrand SetPageAnonExclusive(vmf->page); 3706c89357e2SDavid Hildenbrand if (unlikely(unshare)) { 3707c89357e2SDavid Hildenbrand pte_unmap_unlock(vmf->pte, vmf->ptl); 3708c89357e2SDavid Hildenbrand return 0; 3709c89357e2SDavid Hildenbrand } 3710a86bc96bSKefeng Wang wp_page_reuse(vmf, folio); 3711cb8d8633SDavid Hildenbrand return 0; 37121da177e4SLinus Torvalds } 37131da177e4SLinus Torvalds /* 37141da177e4SLinus Torvalds * Ok, we need to copy. Oh, well.. 37151da177e4SLinus Torvalds */ 3716b9086fdeSDavid Hildenbrand if (folio) 3717b9086fdeSDavid Hildenbrand folio_get(folio); 371828766805SShachar Raindel 371982b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 372094bfe85bSYang Yang #ifdef CONFIG_KSM 3721b9086fdeSDavid Hildenbrand if (folio && folio_test_ksm(folio)) 372294bfe85bSYang Yang count_vm_event(COW_KSM); 372394bfe85bSYang Yang #endif 3724a41b70d6SJan Kara return wp_page_copy(vmf); 37251da177e4SLinus Torvalds } 37261da177e4SLinus Torvalds 372797a89413SPeter Zijlstra static void unmap_mapping_range_vma(struct vm_area_struct *vma, 37281da177e4SLinus Torvalds unsigned long start_addr, unsigned long end_addr, 37291da177e4SLinus Torvalds struct zap_details *details) 37301da177e4SLinus Torvalds { 3731f5cc4eefSAl Viro zap_page_range_single(vma, start_addr, end_addr - start_addr, details); 37321da177e4SLinus Torvalds } 37331da177e4SLinus Torvalds 3734f808c13fSDavidlohr Bueso static inline void unmap_mapping_range_tree(struct rb_root_cached *root, 3735232a6a1cSPeter Xu pgoff_t first_index, 3736232a6a1cSPeter Xu pgoff_t last_index, 37371da177e4SLinus Torvalds struct zap_details *details) 37381da177e4SLinus Torvalds { 37391da177e4SLinus Torvalds struct vm_area_struct *vma; 37401da177e4SLinus Torvalds pgoff_t vba, vea, zba, zea; 37411da177e4SLinus Torvalds 3742232a6a1cSPeter Xu vma_interval_tree_foreach(vma, root, first_index, last_index) { 37431da177e4SLinus Torvalds vba = vma->vm_pgoff; 3744d6e93217SLibin vea = vba + vma_pages(vma) - 1; 3745f9871da9SMiaohe Lin zba = max(first_index, vba); 3746f9871da9SMiaohe Lin zea = min(last_index, vea); 37471da177e4SLinus Torvalds 374897a89413SPeter Zijlstra unmap_mapping_range_vma(vma, 37491da177e4SLinus Torvalds ((zba - vba) << PAGE_SHIFT) + vma->vm_start, 37501da177e4SLinus Torvalds ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start, 375197a89413SPeter Zijlstra details); 37521da177e4SLinus Torvalds } 37531da177e4SLinus Torvalds } 37541da177e4SLinus Torvalds 37551da177e4SLinus Torvalds /** 37563506659eSMatthew Wilcox (Oracle) * unmap_mapping_folio() - Unmap single folio from processes. 37573506659eSMatthew Wilcox (Oracle) * @folio: The locked folio to be unmapped. 375822061a1fSHugh Dickins * 37593506659eSMatthew Wilcox (Oracle) * Unmap this folio from any userspace process which still has it mmaped. 376022061a1fSHugh Dickins * Typically, for efficiency, the range of nearby pages has already been 376122061a1fSHugh Dickins * unmapped by unmap_mapping_pages() or unmap_mapping_range(). But once 37623506659eSMatthew Wilcox (Oracle) * truncation or invalidation holds the lock on a folio, it may find that 37633506659eSMatthew Wilcox (Oracle) * the page has been remapped again: and then uses unmap_mapping_folio() 376422061a1fSHugh Dickins * to unmap it finally. 376522061a1fSHugh Dickins */ 37663506659eSMatthew Wilcox (Oracle) void unmap_mapping_folio(struct folio *folio) 376722061a1fSHugh Dickins { 37683506659eSMatthew Wilcox (Oracle) struct address_space *mapping = folio->mapping; 376922061a1fSHugh Dickins struct zap_details details = { }; 3770232a6a1cSPeter Xu pgoff_t first_index; 3771232a6a1cSPeter Xu pgoff_t last_index; 377222061a1fSHugh Dickins 37733506659eSMatthew Wilcox (Oracle) VM_BUG_ON(!folio_test_locked(folio)); 377422061a1fSHugh Dickins 37753506659eSMatthew Wilcox (Oracle) first_index = folio->index; 377687b11f86SSidhartha Kumar last_index = folio_next_index(folio) - 1; 3777232a6a1cSPeter Xu 37782e148f1eSPeter Xu details.even_cows = false; 37793506659eSMatthew Wilcox (Oracle) details.single_folio = folio; 3780999dad82SPeter Xu details.zap_flags = ZAP_FLAG_DROP_MARKER; 378122061a1fSHugh Dickins 37822c865995SHugh Dickins i_mmap_lock_read(mapping); 378322061a1fSHugh Dickins if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root))) 3784232a6a1cSPeter Xu unmap_mapping_range_tree(&mapping->i_mmap, first_index, 3785232a6a1cSPeter Xu last_index, &details); 37862c865995SHugh Dickins i_mmap_unlock_read(mapping); 378722061a1fSHugh Dickins } 378822061a1fSHugh Dickins 378922061a1fSHugh Dickins /** 3790977fbdcdSMatthew Wilcox * unmap_mapping_pages() - Unmap pages from processes. 3791977fbdcdSMatthew Wilcox * @mapping: The address space containing pages to be unmapped. 3792977fbdcdSMatthew Wilcox * @start: Index of first page to be unmapped. 3793977fbdcdSMatthew Wilcox * @nr: Number of pages to be unmapped. 0 to unmap to end of file. 3794977fbdcdSMatthew Wilcox * @even_cows: Whether to unmap even private COWed pages. 3795977fbdcdSMatthew Wilcox * 3796977fbdcdSMatthew Wilcox * Unmap the pages in this address space from any userspace process which 3797977fbdcdSMatthew Wilcox * has them mmaped. Generally, you want to remove COWed pages as well when 3798977fbdcdSMatthew Wilcox * a file is being truncated, but not when invalidating pages from the page 3799977fbdcdSMatthew Wilcox * cache. 3800977fbdcdSMatthew Wilcox */ 3801977fbdcdSMatthew Wilcox void unmap_mapping_pages(struct address_space *mapping, pgoff_t start, 3802977fbdcdSMatthew Wilcox pgoff_t nr, bool even_cows) 3803977fbdcdSMatthew Wilcox { 3804977fbdcdSMatthew Wilcox struct zap_details details = { }; 3805232a6a1cSPeter Xu pgoff_t first_index = start; 3806232a6a1cSPeter Xu pgoff_t last_index = start + nr - 1; 3807977fbdcdSMatthew Wilcox 38082e148f1eSPeter Xu details.even_cows = even_cows; 3809232a6a1cSPeter Xu if (last_index < first_index) 3810232a6a1cSPeter Xu last_index = ULONG_MAX; 3811977fbdcdSMatthew Wilcox 38122c865995SHugh Dickins i_mmap_lock_read(mapping); 3813977fbdcdSMatthew Wilcox if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root))) 3814232a6a1cSPeter Xu unmap_mapping_range_tree(&mapping->i_mmap, first_index, 3815232a6a1cSPeter Xu last_index, &details); 38162c865995SHugh Dickins i_mmap_unlock_read(mapping); 3817977fbdcdSMatthew Wilcox } 38186e0e99d5SDavid Howells EXPORT_SYMBOL_GPL(unmap_mapping_pages); 3819977fbdcdSMatthew Wilcox 3820977fbdcdSMatthew Wilcox /** 38218a5f14a2SKirill A. Shutemov * unmap_mapping_range - unmap the portion of all mmaps in the specified 3822977fbdcdSMatthew Wilcox * address_space corresponding to the specified byte range in the underlying 38238a5f14a2SKirill A. Shutemov * file. 38248a5f14a2SKirill A. Shutemov * 38253d41088fSMartin Waitz * @mapping: the address space containing mmaps to be unmapped. 38261da177e4SLinus Torvalds * @holebegin: byte in first page to unmap, relative to the start of 38271da177e4SLinus Torvalds * the underlying file. This will be rounded down to a PAGE_SIZE 382825d9e2d1Snpiggin@suse.de * boundary. Note that this is different from truncate_pagecache(), which 38291da177e4SLinus Torvalds * must keep the partial page. In contrast, we must get rid of 38301da177e4SLinus Torvalds * partial pages. 38311da177e4SLinus Torvalds * @holelen: size of prospective hole in bytes. This will be rounded 38321da177e4SLinus Torvalds * up to a PAGE_SIZE boundary. A holelen of zero truncates to the 38331da177e4SLinus Torvalds * end of the file. 38341da177e4SLinus Torvalds * @even_cows: 1 when truncating a file, unmap even private COWed pages; 38351da177e4SLinus Torvalds * but 0 when invalidating pagecache, don't throw away private data. 38361da177e4SLinus Torvalds */ 38371da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping, 38381da177e4SLinus Torvalds loff_t const holebegin, loff_t const holelen, int even_cows) 38391da177e4SLinus Torvalds { 38409eab0421SJiajun Xie pgoff_t hba = (pgoff_t)(holebegin) >> PAGE_SHIFT; 38419eab0421SJiajun Xie pgoff_t hlen = ((pgoff_t)(holelen) + PAGE_SIZE - 1) >> PAGE_SHIFT; 38421da177e4SLinus Torvalds 38431da177e4SLinus Torvalds /* Check for overflow. */ 38441da177e4SLinus Torvalds if (sizeof(holelen) > sizeof(hlen)) { 38451da177e4SLinus Torvalds long long holeend = 38461da177e4SLinus Torvalds (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 38471da177e4SLinus Torvalds if (holeend & ~(long long)ULONG_MAX) 38481da177e4SLinus Torvalds hlen = ULONG_MAX - hba + 1; 38491da177e4SLinus Torvalds } 38501da177e4SLinus Torvalds 3851977fbdcdSMatthew Wilcox unmap_mapping_pages(mapping, hba, hlen, even_cows); 38521da177e4SLinus Torvalds } 38531da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range); 38541da177e4SLinus Torvalds 38551da177e4SLinus Torvalds /* 3856b756a3b5SAlistair Popple * Restore a potential device exclusive pte to a working pte entry 3857b756a3b5SAlistair Popple */ 3858b756a3b5SAlistair Popple static vm_fault_t remove_device_exclusive_entry(struct vm_fault *vmf) 3859b756a3b5SAlistair Popple { 386019672a9eSMatthew Wilcox (Oracle) struct folio *folio = page_folio(vmf->page); 3861b756a3b5SAlistair Popple struct vm_area_struct *vma = vmf->vma; 3862b756a3b5SAlistair Popple struct mmu_notifier_range range; 3863fdc724d6SSuren Baghdasaryan vm_fault_t ret; 3864b756a3b5SAlistair Popple 38657c7b9629SAlistair Popple /* 38667c7b9629SAlistair Popple * We need a reference to lock the folio because we don't hold 38677c7b9629SAlistair Popple * the PTL so a racing thread can remove the device-exclusive 38687c7b9629SAlistair Popple * entry and unmap it. If the folio is free the entry must 38697c7b9629SAlistair Popple * have been removed already. If it happens to have already 38707c7b9629SAlistair Popple * been re-allocated after being freed all we do is lock and 38717c7b9629SAlistair Popple * unlock it. 38727c7b9629SAlistair Popple */ 38737c7b9629SAlistair Popple if (!folio_try_get(folio)) 38747c7b9629SAlistair Popple return 0; 38757c7b9629SAlistair Popple 3876fdc724d6SSuren Baghdasaryan ret = folio_lock_or_retry(folio, vmf); 3877fdc724d6SSuren Baghdasaryan if (ret) { 38787c7b9629SAlistair Popple folio_put(folio); 3879fdc724d6SSuren Baghdasaryan return ret; 38807c7b9629SAlistair Popple } 38817d4a8be0SAlistair Popple mmu_notifier_range_init_owner(&range, MMU_NOTIFY_EXCLUSIVE, 0, 3882b756a3b5SAlistair Popple vma->vm_mm, vmf->address & PAGE_MASK, 3883b756a3b5SAlistair Popple (vmf->address & PAGE_MASK) + PAGE_SIZE, NULL); 3884b756a3b5SAlistair Popple mmu_notifier_invalidate_range_start(&range); 3885b756a3b5SAlistair Popple 3886b756a3b5SAlistair Popple vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 3887b756a3b5SAlistair Popple &vmf->ptl); 3888c33c7948SRyan Roberts if (likely(vmf->pte && pte_same(ptep_get(vmf->pte), vmf->orig_pte))) 388919672a9eSMatthew Wilcox (Oracle) restore_exclusive_pte(vma, vmf->page, vmf->address, vmf->pte); 3890b756a3b5SAlistair Popple 38913db82b93SHugh Dickins if (vmf->pte) 3892b756a3b5SAlistair Popple pte_unmap_unlock(vmf->pte, vmf->ptl); 389319672a9eSMatthew Wilcox (Oracle) folio_unlock(folio); 38947c7b9629SAlistair Popple folio_put(folio); 3895b756a3b5SAlistair Popple 3896b756a3b5SAlistair Popple mmu_notifier_invalidate_range_end(&range); 3897b756a3b5SAlistair Popple return 0; 3898b756a3b5SAlistair Popple } 3899b756a3b5SAlistair Popple 3900a160e537SMatthew Wilcox (Oracle) static inline bool should_try_to_free_swap(struct folio *folio, 3901c145e0b4SDavid Hildenbrand struct vm_area_struct *vma, 3902c145e0b4SDavid Hildenbrand unsigned int fault_flags) 3903c145e0b4SDavid Hildenbrand { 3904a160e537SMatthew Wilcox (Oracle) if (!folio_test_swapcache(folio)) 3905c145e0b4SDavid Hildenbrand return false; 39069202d527SMatthew Wilcox (Oracle) if (mem_cgroup_swap_full(folio) || (vma->vm_flags & VM_LOCKED) || 3907a160e537SMatthew Wilcox (Oracle) folio_test_mlocked(folio)) 3908c145e0b4SDavid Hildenbrand return true; 3909c145e0b4SDavid Hildenbrand /* 3910c145e0b4SDavid Hildenbrand * If we want to map a page that's in the swapcache writable, we 3911c145e0b4SDavid Hildenbrand * have to detect via the refcount if we're really the exclusive 3912c145e0b4SDavid Hildenbrand * user. Try freeing the swapcache to get rid of the swapcache 3913c145e0b4SDavid Hildenbrand * reference only in case it's likely that we'll be the exlusive user. 3914c145e0b4SDavid Hildenbrand */ 3915a160e537SMatthew Wilcox (Oracle) return (fault_flags & FAULT_FLAG_WRITE) && !folio_test_ksm(folio) && 3916a160e537SMatthew Wilcox (Oracle) folio_ref_count(folio) == 2; 3917c145e0b4SDavid Hildenbrand } 3918c145e0b4SDavid Hildenbrand 39199c28a205SPeter Xu static vm_fault_t pte_marker_clear(struct vm_fault *vmf) 39209c28a205SPeter Xu { 39219c28a205SPeter Xu vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, 39229c28a205SPeter Xu vmf->address, &vmf->ptl); 39233db82b93SHugh Dickins if (!vmf->pte) 39243db82b93SHugh Dickins return 0; 39259c28a205SPeter Xu /* 39269c28a205SPeter Xu * Be careful so that we will only recover a special uffd-wp pte into a 39279c28a205SPeter Xu * none pte. Otherwise it means the pte could have changed, so retry. 39287e3ce3f8SPeter Xu * 39297e3ce3f8SPeter Xu * This should also cover the case where e.g. the pte changed 3930af19487fSAxel Rasmussen * quickly from a PTE_MARKER_UFFD_WP into PTE_MARKER_POISONED. 39317e3ce3f8SPeter Xu * So is_pte_marker() check is not enough to safely drop the pte. 39329c28a205SPeter Xu */ 3933c33c7948SRyan Roberts if (pte_same(vmf->orig_pte, ptep_get(vmf->pte))) 39349c28a205SPeter Xu pte_clear(vmf->vma->vm_mm, vmf->address, vmf->pte); 39359c28a205SPeter Xu pte_unmap_unlock(vmf->pte, vmf->ptl); 39369c28a205SPeter Xu return 0; 39379c28a205SPeter Xu } 39389c28a205SPeter Xu 39392bad466cSPeter Xu static vm_fault_t do_pte_missing(struct vm_fault *vmf) 39402bad466cSPeter Xu { 39412bad466cSPeter Xu if (vma_is_anonymous(vmf->vma)) 39422bad466cSPeter Xu return do_anonymous_page(vmf); 39432bad466cSPeter Xu else 39442bad466cSPeter Xu return do_fault(vmf); 39452bad466cSPeter Xu } 39462bad466cSPeter Xu 39479c28a205SPeter Xu /* 39489c28a205SPeter Xu * This is actually a page-missing access, but with uffd-wp special pte 39499c28a205SPeter Xu * installed. It means this pte was wr-protected before being unmapped. 39509c28a205SPeter Xu */ 39519c28a205SPeter Xu static vm_fault_t pte_marker_handle_uffd_wp(struct vm_fault *vmf) 39529c28a205SPeter Xu { 39539c28a205SPeter Xu /* 39549c28a205SPeter Xu * Just in case there're leftover special ptes even after the region 39557a079ba2SPeter Xu * got unregistered - we can simply clear them. 39569c28a205SPeter Xu */ 39572bad466cSPeter Xu if (unlikely(!userfaultfd_wp(vmf->vma))) 39589c28a205SPeter Xu return pte_marker_clear(vmf); 39599c28a205SPeter Xu 39602bad466cSPeter Xu return do_pte_missing(vmf); 39619c28a205SPeter Xu } 39629c28a205SPeter Xu 39635c041f5dSPeter Xu static vm_fault_t handle_pte_marker(struct vm_fault *vmf) 39645c041f5dSPeter Xu { 39655c041f5dSPeter Xu swp_entry_t entry = pte_to_swp_entry(vmf->orig_pte); 39665c041f5dSPeter Xu unsigned long marker = pte_marker_get(entry); 39675c041f5dSPeter Xu 39685c041f5dSPeter Xu /* 3969ca92ea3dSPeter Xu * PTE markers should never be empty. If anything weird happened, 3970ca92ea3dSPeter Xu * the best thing to do is to kill the process along with its mm. 39715c041f5dSPeter Xu */ 3972ca92ea3dSPeter Xu if (WARN_ON_ONCE(!marker)) 39735c041f5dSPeter Xu return VM_FAULT_SIGBUS; 39745c041f5dSPeter Xu 397515520a3fSPeter Xu /* Higher priority than uffd-wp when data corrupted */ 3976af19487fSAxel Rasmussen if (marker & PTE_MARKER_POISONED) 3977af19487fSAxel Rasmussen return VM_FAULT_HWPOISON; 397815520a3fSPeter Xu 39799c28a205SPeter Xu if (pte_marker_entry_uffd_wp(entry)) 39809c28a205SPeter Xu return pte_marker_handle_uffd_wp(vmf); 39819c28a205SPeter Xu 39829c28a205SPeter Xu /* This is an unknown pte marker */ 39839c28a205SPeter Xu return VM_FAULT_SIGBUS; 39845c041f5dSPeter Xu } 39855c041f5dSPeter Xu 3986b756a3b5SAlistair Popple /* 3987c1e8d7c6SMichel Lespinasse * We enter with non-exclusive mmap_lock (to exclude vma changes, 39888f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 39899a95f3cfSPaul Cassella * We return with pte unmapped and unlocked. 39909a95f3cfSPaul Cassella * 3991c1e8d7c6SMichel Lespinasse * We return with the mmap_lock locked or unlocked in the same cases 39929a95f3cfSPaul Cassella * as does filemap_fault(). 39931da177e4SLinus Torvalds */ 39942b740303SSouptick Joarder vm_fault_t do_swap_page(struct vm_fault *vmf) 39951da177e4SLinus Torvalds { 399682b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3997d4f9565aSMatthew Wilcox (Oracle) struct folio *swapcache, *folio = NULL; 3998d4f9565aSMatthew Wilcox (Oracle) struct page *page; 39992799e775SMiaohe Lin struct swap_info_struct *si = NULL; 400014f9135dSDavid Hildenbrand rmap_t rmap_flags = RMAP_NONE; 400113ddaf26SKairui Song bool need_clear_cache = false; 40021493a191SDavid Hildenbrand bool exclusive = false; 400365500d23SHugh Dickins swp_entry_t entry; 40041da177e4SLinus Torvalds pte_t pte; 40052b740303SSouptick Joarder vm_fault_t ret = 0; 4006aae466b0SJoonsoo Kim void *shadow = NULL; 40071da177e4SLinus Torvalds 40082ca99358SPeter Xu if (!pte_unmap_same(vmf)) 40098f4e2101SHugh Dickins goto out; 401065500d23SHugh Dickins 40112994302bSJan Kara entry = pte_to_swp_entry(vmf->orig_pte); 4012d1737fdbSAndi Kleen if (unlikely(non_swap_entry(entry))) { 40130697212aSChristoph Lameter if (is_migration_entry(entry)) { 401482b0f8c3SJan Kara migration_entry_wait(vma->vm_mm, vmf->pmd, 401582b0f8c3SJan Kara vmf->address); 4016b756a3b5SAlistair Popple } else if (is_device_exclusive_entry(entry)) { 4017b756a3b5SAlistair Popple vmf->page = pfn_swap_entry_to_page(entry); 4018b756a3b5SAlistair Popple ret = remove_device_exclusive_entry(vmf); 40195042db43SJérôme Glisse } else if (is_device_private_entry(entry)) { 40201235ccd0SSuren Baghdasaryan if (vmf->flags & FAULT_FLAG_VMA_LOCK) { 40211235ccd0SSuren Baghdasaryan /* 40221235ccd0SSuren Baghdasaryan * migrate_to_ram is not yet ready to operate 40231235ccd0SSuren Baghdasaryan * under VMA lock. 40241235ccd0SSuren Baghdasaryan */ 40251235ccd0SSuren Baghdasaryan vma_end_read(vma); 40261235ccd0SSuren Baghdasaryan ret = VM_FAULT_RETRY; 40271235ccd0SSuren Baghdasaryan goto out; 40281235ccd0SSuren Baghdasaryan } 40291235ccd0SSuren Baghdasaryan 4030af5cdaf8SAlistair Popple vmf->page = pfn_swap_entry_to_page(entry); 403116ce101dSAlistair Popple vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 403216ce101dSAlistair Popple vmf->address, &vmf->ptl); 40333db82b93SHugh Dickins if (unlikely(!vmf->pte || 4034c33c7948SRyan Roberts !pte_same(ptep_get(vmf->pte), 4035c33c7948SRyan Roberts vmf->orig_pte))) 40363b65f437SRyan Roberts goto unlock; 403716ce101dSAlistair Popple 403816ce101dSAlistair Popple /* 403916ce101dSAlistair Popple * Get a page reference while we know the page can't be 404016ce101dSAlistair Popple * freed. 404116ce101dSAlistair Popple */ 404216ce101dSAlistair Popple get_page(vmf->page); 404316ce101dSAlistair Popple pte_unmap_unlock(vmf->pte, vmf->ptl); 40444a955bedSAlistair Popple ret = vmf->page->pgmap->ops->migrate_to_ram(vmf); 404516ce101dSAlistair Popple put_page(vmf->page); 4046d1737fdbSAndi Kleen } else if (is_hwpoison_entry(entry)) { 4047d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 40485c041f5dSPeter Xu } else if (is_pte_marker_entry(entry)) { 40495c041f5dSPeter Xu ret = handle_pte_marker(vmf); 4050d1737fdbSAndi Kleen } else { 40512994302bSJan Kara print_bad_pte(vma, vmf->address, vmf->orig_pte, NULL); 4052d99be1a8SHugh Dickins ret = VM_FAULT_SIGBUS; 4053d1737fdbSAndi Kleen } 40540697212aSChristoph Lameter goto out; 40550697212aSChristoph Lameter } 40560bcac06fSMinchan Kim 40572799e775SMiaohe Lin /* Prevent swapoff from happening to us. */ 40582799e775SMiaohe Lin si = get_swap_device(entry); 40592799e775SMiaohe Lin if (unlikely(!si)) 40602799e775SMiaohe Lin goto out; 40610bcac06fSMinchan Kim 40625a423081SMatthew Wilcox (Oracle) folio = swap_cache_get_folio(entry, vma, vmf->address); 40635a423081SMatthew Wilcox (Oracle) if (folio) 40645a423081SMatthew Wilcox (Oracle) page = folio_file_page(folio, swp_offset(entry)); 4065d4f9565aSMatthew Wilcox (Oracle) swapcache = folio; 4066f8020772SMinchan Kim 4067d4f9565aSMatthew Wilcox (Oracle) if (!folio) { 4068a449bf58SQian Cai if (data_race(si->flags & SWP_SYNCHRONOUS_IO) && 4069eb085574SHuang Ying __swap_count(entry) == 1) { 407013ddaf26SKairui Song /* 407113ddaf26SKairui Song * Prevent parallel swapin from proceeding with 407213ddaf26SKairui Song * the cache flag. Otherwise, another thread may 407313ddaf26SKairui Song * finish swapin first, free the entry, and swapout 407413ddaf26SKairui Song * reusing the same entry. It's undetectable as 407513ddaf26SKairui Song * pte_same() returns true due to entry reuse. 407613ddaf26SKairui Song */ 407713ddaf26SKairui Song if (swapcache_prepare(entry)) { 407813ddaf26SKairui Song /* Relax a bit to prevent rapid repeated page faults */ 407913ddaf26SKairui Song schedule_timeout_uninterruptible(1); 408013ddaf26SKairui Song goto out; 408113ddaf26SKairui Song } 408213ddaf26SKairui Song need_clear_cache = true; 408313ddaf26SKairui Song 40840bcac06fSMinchan Kim /* skip swapcache */ 408563ad4addSMatthew Wilcox (Oracle) folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, 408663ad4addSMatthew Wilcox (Oracle) vma, vmf->address, false); 408763ad4addSMatthew Wilcox (Oracle) page = &folio->page; 408863ad4addSMatthew Wilcox (Oracle) if (folio) { 408963ad4addSMatthew Wilcox (Oracle) __folio_set_locked(folio); 409063ad4addSMatthew Wilcox (Oracle) __folio_set_swapbacked(folio); 40914c6355b2SJohannes Weiner 409265995918SMatthew Wilcox (Oracle) if (mem_cgroup_swapin_charge_folio(folio, 409363ad4addSMatthew Wilcox (Oracle) vma->vm_mm, GFP_KERNEL, 409463ad4addSMatthew Wilcox (Oracle) entry)) { 4095545b1b07SMichal Hocko ret = VM_FAULT_OOM; 40964c6355b2SJohannes Weiner goto out_page; 4097545b1b07SMichal Hocko } 40980add0c77SShakeel Butt mem_cgroup_swapin_uncharge_swap(entry); 40994c6355b2SJohannes Weiner 4100aae466b0SJoonsoo Kim shadow = get_shadow_from_swap_cache(entry); 4101aae466b0SJoonsoo Kim if (shadow) 410263ad4addSMatthew Wilcox (Oracle) workingset_refault(folio, shadow); 41030076f029SJoonsoo Kim 410463ad4addSMatthew Wilcox (Oracle) folio_add_lru(folio); 41050add0c77SShakeel Butt 4106c9bdf768SMatthew Wilcox (Oracle) /* To provide entry to swap_read_folio() */ 41073d2c9087SDavid Hildenbrand folio->swap = entry; 4108c9bdf768SMatthew Wilcox (Oracle) swap_read_folio(folio, true, NULL); 410963ad4addSMatthew Wilcox (Oracle) folio->private = NULL; 41100bcac06fSMinchan Kim } 4111aa8d22a1SMinchan Kim } else { 4112e9e9b7ecSMinchan Kim page = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, 4113e9e9b7ecSMinchan Kim vmf); 411463ad4addSMatthew Wilcox (Oracle) if (page) 411563ad4addSMatthew Wilcox (Oracle) folio = page_folio(page); 4116d4f9565aSMatthew Wilcox (Oracle) swapcache = folio; 41170bcac06fSMinchan Kim } 41180bcac06fSMinchan Kim 4119d4f9565aSMatthew Wilcox (Oracle) if (!folio) { 41201da177e4SLinus Torvalds /* 41218f4e2101SHugh Dickins * Back out if somebody else faulted in this pte 41228f4e2101SHugh Dickins * while we released the pte lock. 41231da177e4SLinus Torvalds */ 412482b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 412582b0f8c3SJan Kara vmf->address, &vmf->ptl); 4126c33c7948SRyan Roberts if (likely(vmf->pte && 4127c33c7948SRyan Roberts pte_same(ptep_get(vmf->pte), vmf->orig_pte))) 41281da177e4SLinus Torvalds ret = VM_FAULT_OOM; 412965500d23SHugh Dickins goto unlock; 41301da177e4SLinus Torvalds } 41311da177e4SLinus Torvalds 41321da177e4SLinus Torvalds /* Had to read the page from swap area: Major fault */ 41331da177e4SLinus Torvalds ret = VM_FAULT_MAJOR; 4134f8891e5eSChristoph Lameter count_vm_event(PGMAJFAULT); 41352262185cSRoman Gushchin count_memcg_event_mm(vma->vm_mm, PGMAJFAULT); 4136d1737fdbSAndi Kleen } else if (PageHWPoison(page)) { 413771f72525SWu Fengguang /* 413871f72525SWu Fengguang * hwpoisoned dirty swapcache pages are kept for killing 413971f72525SWu Fengguang * owner processes (which may be unknown at hwpoison time) 414071f72525SWu Fengguang */ 4141d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 41424779cb31SAndi Kleen goto out_release; 41431da177e4SLinus Torvalds } 41441da177e4SLinus Torvalds 4145fdc724d6SSuren Baghdasaryan ret |= folio_lock_or_retry(folio, vmf); 4146fdc724d6SSuren Baghdasaryan if (ret & VM_FAULT_RETRY) 4147d065bd81SMichel Lespinasse goto out_release; 41481da177e4SLinus Torvalds 414984d60fddSDavid Hildenbrand if (swapcache) { 41504969c119SAndrea Arcangeli /* 41513b344157SMatthew Wilcox (Oracle) * Make sure folio_free_swap() or swapoff did not release the 415284d60fddSDavid Hildenbrand * swapcache from under us. The page pin, and pte_same test 415384d60fddSDavid Hildenbrand * below, are not enough to exclude that. Even if it is still 415484d60fddSDavid Hildenbrand * swapcache, we need to check that the page's swap has not 415584d60fddSDavid Hildenbrand * changed. 41564969c119SAndrea Arcangeli */ 415763ad4addSMatthew Wilcox (Oracle) if (unlikely(!folio_test_swapcache(folio) || 4158cfeed8ffSDavid Hildenbrand page_swap_entry(page).val != entry.val)) 41594969c119SAndrea Arcangeli goto out_page; 41604969c119SAndrea Arcangeli 416184d60fddSDavid Hildenbrand /* 416284d60fddSDavid Hildenbrand * KSM sometimes has to copy on read faults, for example, if 416384d60fddSDavid Hildenbrand * page->index of !PageKSM() pages would be nonlinear inside the 416484d60fddSDavid Hildenbrand * anon VMA -- PageKSM() is lost on actual swapout. 416584d60fddSDavid Hildenbrand */ 416696db66d9SMatthew Wilcox (Oracle) folio = ksm_might_need_to_copy(folio, vma, vmf->address); 416796db66d9SMatthew Wilcox (Oracle) if (unlikely(!folio)) { 41685ad64688SHugh Dickins ret = VM_FAULT_OOM; 416996db66d9SMatthew Wilcox (Oracle) folio = swapcache; 41704969c119SAndrea Arcangeli goto out_page; 417196db66d9SMatthew Wilcox (Oracle) } else if (unlikely(folio == ERR_PTR(-EHWPOISON))) { 41726b970599SKefeng Wang ret = VM_FAULT_HWPOISON; 417396db66d9SMatthew Wilcox (Oracle) folio = swapcache; 41746b970599SKefeng Wang goto out_page; 41754969c119SAndrea Arcangeli } 417696db66d9SMatthew Wilcox (Oracle) if (folio != swapcache) 417796db66d9SMatthew Wilcox (Oracle) page = folio_page(folio, 0); 4178c145e0b4SDavid Hildenbrand 4179c145e0b4SDavid Hildenbrand /* 4180c145e0b4SDavid Hildenbrand * If we want to map a page that's in the swapcache writable, we 4181c145e0b4SDavid Hildenbrand * have to detect via the refcount if we're really the exclusive 4182c145e0b4SDavid Hildenbrand * owner. Try removing the extra reference from the local LRU 41831fec6890SMatthew Wilcox (Oracle) * caches if required. 4184c145e0b4SDavid Hildenbrand */ 4185d4f9565aSMatthew Wilcox (Oracle) if ((vmf->flags & FAULT_FLAG_WRITE) && folio == swapcache && 418663ad4addSMatthew Wilcox (Oracle) !folio_test_ksm(folio) && !folio_test_lru(folio)) 4187c145e0b4SDavid Hildenbrand lru_add_drain(); 418884d60fddSDavid Hildenbrand } 41895ad64688SHugh Dickins 41904231f842SKefeng Wang folio_throttle_swaprate(folio, GFP_KERNEL); 4191073e587eSKAMEZAWA Hiroyuki 41921da177e4SLinus Torvalds /* 41938f4e2101SHugh Dickins * Back out if somebody else already faulted in this pte. 41941da177e4SLinus Torvalds */ 419582b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 419682b0f8c3SJan Kara &vmf->ptl); 4197c33c7948SRyan Roberts if (unlikely(!vmf->pte || !pte_same(ptep_get(vmf->pte), vmf->orig_pte))) 4198b8107480SKirill Korotaev goto out_nomap; 4199b8107480SKirill Korotaev 420063ad4addSMatthew Wilcox (Oracle) if (unlikely(!folio_test_uptodate(folio))) { 4201b8107480SKirill Korotaev ret = VM_FAULT_SIGBUS; 4202b8107480SKirill Korotaev goto out_nomap; 42031da177e4SLinus Torvalds } 42041da177e4SLinus Torvalds 42058c7c6e34SKAMEZAWA Hiroyuki /* 420678fbe906SDavid Hildenbrand * PG_anon_exclusive reuses PG_mappedtodisk for anon pages. A swap pte 420778fbe906SDavid Hildenbrand * must never point at an anonymous page in the swapcache that is 420878fbe906SDavid Hildenbrand * PG_anon_exclusive. Sanity check that this holds and especially, that 420978fbe906SDavid Hildenbrand * no filesystem set PG_mappedtodisk on a page in the swapcache. Sanity 421078fbe906SDavid Hildenbrand * check after taking the PT lock and making sure that nobody 421178fbe906SDavid Hildenbrand * concurrently faulted in this page and set PG_anon_exclusive. 421278fbe906SDavid Hildenbrand */ 421363ad4addSMatthew Wilcox (Oracle) BUG_ON(!folio_test_anon(folio) && folio_test_mappedtodisk(folio)); 421463ad4addSMatthew Wilcox (Oracle) BUG_ON(folio_test_anon(folio) && PageAnonExclusive(page)); 421578fbe906SDavid Hildenbrand 421678fbe906SDavid Hildenbrand /* 42171493a191SDavid Hildenbrand * Check under PT lock (to protect against concurrent fork() sharing 42181493a191SDavid Hildenbrand * the swap entry concurrently) for certainly exclusive pages. 42191493a191SDavid Hildenbrand */ 422063ad4addSMatthew Wilcox (Oracle) if (!folio_test_ksm(folio)) { 42211493a191SDavid Hildenbrand exclusive = pte_swp_exclusive(vmf->orig_pte); 4222d4f9565aSMatthew Wilcox (Oracle) if (folio != swapcache) { 42231493a191SDavid Hildenbrand /* 42241493a191SDavid Hildenbrand * We have a fresh page that is not exposed to the 42251493a191SDavid Hildenbrand * swapcache -> certainly exclusive. 42261493a191SDavid Hildenbrand */ 42271493a191SDavid Hildenbrand exclusive = true; 422863ad4addSMatthew Wilcox (Oracle) } else if (exclusive && folio_test_writeback(folio) && 4229eacde327SMiaohe Lin data_race(si->flags & SWP_STABLE_WRITES)) { 42301493a191SDavid Hildenbrand /* 42311493a191SDavid Hildenbrand * This is tricky: not all swap backends support 42321493a191SDavid Hildenbrand * concurrent page modifications while under writeback. 42331493a191SDavid Hildenbrand * 42341493a191SDavid Hildenbrand * So if we stumble over such a page in the swapcache 42351493a191SDavid Hildenbrand * we must not set the page exclusive, otherwise we can 42361493a191SDavid Hildenbrand * map it writable without further checks and modify it 42371493a191SDavid Hildenbrand * while still under writeback. 42381493a191SDavid Hildenbrand * 42391493a191SDavid Hildenbrand * For these problematic swap backends, simply drop the 42401493a191SDavid Hildenbrand * exclusive marker: this is perfectly fine as we start 42411493a191SDavid Hildenbrand * writeback only if we fully unmapped the page and 42421493a191SDavid Hildenbrand * there are no unexpected references on the page after 42431493a191SDavid Hildenbrand * unmapping succeeded. After fully unmapped, no 42441493a191SDavid Hildenbrand * further GUP references (FOLL_GET and FOLL_PIN) can 42451493a191SDavid Hildenbrand * appear, so dropping the exclusive marker and mapping 42461493a191SDavid Hildenbrand * it only R/O is fine. 42471493a191SDavid Hildenbrand */ 42481493a191SDavid Hildenbrand exclusive = false; 42491493a191SDavid Hildenbrand } 42501493a191SDavid Hildenbrand } 42511493a191SDavid Hildenbrand 42521493a191SDavid Hildenbrand /* 42536dca4ac6SPeter Collingbourne * Some architectures may have to restore extra metadata to the page 42546dca4ac6SPeter Collingbourne * when reading from swap. This metadata may be indexed by swap entry 42556dca4ac6SPeter Collingbourne * so this must be called before swap_free(). 42566dca4ac6SPeter Collingbourne */ 42576dca4ac6SPeter Collingbourne arch_swap_restore(entry, folio); 42586dca4ac6SPeter Collingbourne 42596dca4ac6SPeter Collingbourne /* 4260c145e0b4SDavid Hildenbrand * Remove the swap entry and conditionally try to free up the swapcache. 4261c145e0b4SDavid Hildenbrand * We're already holding a reference on the page but haven't mapped it 4262c145e0b4SDavid Hildenbrand * yet. 42638c7c6e34SKAMEZAWA Hiroyuki */ 4264c145e0b4SDavid Hildenbrand swap_free(entry); 4265a160e537SMatthew Wilcox (Oracle) if (should_try_to_free_swap(folio, vma, vmf->flags)) 4266a160e537SMatthew Wilcox (Oracle) folio_free_swap(folio); 42671da177e4SLinus Torvalds 4268f1a79412SShakeel Butt inc_mm_counter(vma->vm_mm, MM_ANONPAGES); 4269f1a79412SShakeel Butt dec_mm_counter(vma->vm_mm, MM_SWAPENTS); 42701da177e4SLinus Torvalds pte = mk_pte(page, vma->vm_page_prot); 4271c145e0b4SDavid Hildenbrand 4272c145e0b4SDavid Hildenbrand /* 42731493a191SDavid Hildenbrand * Same logic as in do_wp_page(); however, optimize for pages that are 42741493a191SDavid Hildenbrand * certainly not shared either because we just allocated them without 42751493a191SDavid Hildenbrand * exposing them to the swapcache or because the swap entry indicates 42761493a191SDavid Hildenbrand * exclusivity. 4277c145e0b4SDavid Hildenbrand */ 427863ad4addSMatthew Wilcox (Oracle) if (!folio_test_ksm(folio) && 427963ad4addSMatthew Wilcox (Oracle) (exclusive || folio_ref_count(folio) == 1)) { 42806c287605SDavid Hildenbrand if (vmf->flags & FAULT_FLAG_WRITE) { 42811da177e4SLinus Torvalds pte = maybe_mkwrite(pte_mkdirty(pte), vma); 428282b0f8c3SJan Kara vmf->flags &= ~FAULT_FLAG_WRITE; 42836c287605SDavid Hildenbrand } 428414f9135dSDavid Hildenbrand rmap_flags |= RMAP_EXCLUSIVE; 42851da177e4SLinus Torvalds } 42861da177e4SLinus Torvalds flush_icache_page(vma, page); 42872994302bSJan Kara if (pte_swp_soft_dirty(vmf->orig_pte)) 4288179ef71cSCyrill Gorcunov pte = pte_mksoft_dirty(pte); 4289f1eb1bacSPeter Xu if (pte_swp_uffd_wp(vmf->orig_pte)) 4290f45ec5ffSPeter Xu pte = pte_mkuffd_wp(pte); 42912994302bSJan Kara vmf->orig_pte = pte; 42920bcac06fSMinchan Kim 42930bcac06fSMinchan Kim /* ksm created a completely new copy */ 4294d4f9565aSMatthew Wilcox (Oracle) if (unlikely(folio != swapcache && swapcache)) { 42952853b66bSMatthew Wilcox (Oracle) folio_add_new_anon_rmap(folio, vma, vmf->address); 429663ad4addSMatthew Wilcox (Oracle) folio_add_lru_vma(folio, vma); 42970bcac06fSMinchan Kim } else { 4298b832a354SDavid Hildenbrand folio_add_anon_rmap_pte(folio, page, vma, vmf->address, 4299b832a354SDavid Hildenbrand rmap_flags); 430000501b53SJohannes Weiner } 43011da177e4SLinus Torvalds 430263ad4addSMatthew Wilcox (Oracle) VM_BUG_ON(!folio_test_anon(folio) || 430363ad4addSMatthew Wilcox (Oracle) (pte_write(pte) && !PageAnonExclusive(page))); 43041eba86c0SPasha Tatashin set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte); 43051eba86c0SPasha Tatashin arch_do_swap_page(vma->vm_mm, vma, vmf->address, pte, vmf->orig_pte); 43061eba86c0SPasha Tatashin 430763ad4addSMatthew Wilcox (Oracle) folio_unlock(folio); 4308d4f9565aSMatthew Wilcox (Oracle) if (folio != swapcache && swapcache) { 43094969c119SAndrea Arcangeli /* 43104969c119SAndrea Arcangeli * Hold the lock to avoid the swap entry to be reused 43114969c119SAndrea Arcangeli * until we take the PT lock for the pte_same() check 43124969c119SAndrea Arcangeli * (to avoid false positives from pte_same). For 43134969c119SAndrea Arcangeli * further safety release the lock after the swap_free 43144969c119SAndrea Arcangeli * so that the swap count won't change under a 43154969c119SAndrea Arcangeli * parallel locked swapcache. 43164969c119SAndrea Arcangeli */ 4317d4f9565aSMatthew Wilcox (Oracle) folio_unlock(swapcache); 4318d4f9565aSMatthew Wilcox (Oracle) folio_put(swapcache); 43194969c119SAndrea Arcangeli } 4320c475a8abSHugh Dickins 432182b0f8c3SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) { 43222994302bSJan Kara ret |= do_wp_page(vmf); 432361469f1dSHugh Dickins if (ret & VM_FAULT_ERROR) 432461469f1dSHugh Dickins ret &= VM_FAULT_ERROR; 43251da177e4SLinus Torvalds goto out; 43261da177e4SLinus Torvalds } 43271da177e4SLinus Torvalds 43281da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 43295003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vma, vmf->address, vmf->pte, 1); 433065500d23SHugh Dickins unlock: 43313db82b93SHugh Dickins if (vmf->pte) 433282b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 43331da177e4SLinus Torvalds out: 433413ddaf26SKairui Song /* Clear the swap cache pin for direct swapin after PTL unlock */ 433513ddaf26SKairui Song if (need_clear_cache) 433613ddaf26SKairui Song swapcache_clear(si, entry); 43372799e775SMiaohe Lin if (si) 43382799e775SMiaohe Lin put_swap_device(si); 43391da177e4SLinus Torvalds return ret; 4340b8107480SKirill Korotaev out_nomap: 43413db82b93SHugh Dickins if (vmf->pte) 434282b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 4343bc43f75cSJohannes Weiner out_page: 434463ad4addSMatthew Wilcox (Oracle) folio_unlock(folio); 43454779cb31SAndi Kleen out_release: 434663ad4addSMatthew Wilcox (Oracle) folio_put(folio); 4347d4f9565aSMatthew Wilcox (Oracle) if (folio != swapcache && swapcache) { 4348d4f9565aSMatthew Wilcox (Oracle) folio_unlock(swapcache); 4349d4f9565aSMatthew Wilcox (Oracle) folio_put(swapcache); 43504969c119SAndrea Arcangeli } 435113ddaf26SKairui Song if (need_clear_cache) 435213ddaf26SKairui Song swapcache_clear(si, entry); 43532799e775SMiaohe Lin if (si) 43542799e775SMiaohe Lin put_swap_device(si); 435565500d23SHugh Dickins return ret; 43561da177e4SLinus Torvalds } 43571da177e4SLinus Torvalds 435819eaf449SRyan Roberts static bool pte_range_none(pte_t *pte, int nr_pages) 435919eaf449SRyan Roberts { 436019eaf449SRyan Roberts int i; 436119eaf449SRyan Roberts 436219eaf449SRyan Roberts for (i = 0; i < nr_pages; i++) { 436319eaf449SRyan Roberts if (!pte_none(ptep_get_lockless(pte + i))) 436419eaf449SRyan Roberts return false; 436519eaf449SRyan Roberts } 436619eaf449SRyan Roberts 436719eaf449SRyan Roberts return true; 436819eaf449SRyan Roberts } 436919eaf449SRyan Roberts 437019eaf449SRyan Roberts static struct folio *alloc_anon_folio(struct vm_fault *vmf) 437119eaf449SRyan Roberts { 437219eaf449SRyan Roberts struct vm_area_struct *vma = vmf->vma; 4373085ff35eSKefeng Wang #ifdef CONFIG_TRANSPARENT_HUGEPAGE 437419eaf449SRyan Roberts unsigned long orders; 437519eaf449SRyan Roberts struct folio *folio; 437619eaf449SRyan Roberts unsigned long addr; 437719eaf449SRyan Roberts pte_t *pte; 437819eaf449SRyan Roberts gfp_t gfp; 437919eaf449SRyan Roberts int order; 438019eaf449SRyan Roberts 438119eaf449SRyan Roberts /* 438219eaf449SRyan Roberts * If uffd is active for the vma we need per-page fault fidelity to 438319eaf449SRyan Roberts * maintain the uffd semantics. 438419eaf449SRyan Roberts */ 438519eaf449SRyan Roberts if (unlikely(userfaultfd_armed(vma))) 438619eaf449SRyan Roberts goto fallback; 438719eaf449SRyan Roberts 438819eaf449SRyan Roberts /* 438919eaf449SRyan Roberts * Get a list of all the (large) orders below PMD_ORDER that are enabled 439019eaf449SRyan Roberts * for this vma. Then filter out the orders that can't be allocated over 439119eaf449SRyan Roberts * the faulting address and still be fully contained in the vma. 439219eaf449SRyan Roberts */ 439319eaf449SRyan Roberts orders = thp_vma_allowable_orders(vma, vma->vm_flags, false, true, true, 439419eaf449SRyan Roberts BIT(PMD_ORDER) - 1); 439519eaf449SRyan Roberts orders = thp_vma_suitable_orders(vma, vmf->address, orders); 439619eaf449SRyan Roberts 439719eaf449SRyan Roberts if (!orders) 439819eaf449SRyan Roberts goto fallback; 439919eaf449SRyan Roberts 440019eaf449SRyan Roberts pte = pte_offset_map(vmf->pmd, vmf->address & PMD_MASK); 440119eaf449SRyan Roberts if (!pte) 440219eaf449SRyan Roberts return ERR_PTR(-EAGAIN); 440319eaf449SRyan Roberts 440419eaf449SRyan Roberts /* 440519eaf449SRyan Roberts * Find the highest order where the aligned range is completely 440619eaf449SRyan Roberts * pte_none(). Note that all remaining orders will be completely 440719eaf449SRyan Roberts * pte_none(). 440819eaf449SRyan Roberts */ 440919eaf449SRyan Roberts order = highest_order(orders); 441019eaf449SRyan Roberts while (orders) { 441119eaf449SRyan Roberts addr = ALIGN_DOWN(vmf->address, PAGE_SIZE << order); 441219eaf449SRyan Roberts if (pte_range_none(pte + pte_index(addr), 1 << order)) 441319eaf449SRyan Roberts break; 441419eaf449SRyan Roberts order = next_order(&orders, order); 441519eaf449SRyan Roberts } 441619eaf449SRyan Roberts 441719eaf449SRyan Roberts pte_unmap(pte); 441819eaf449SRyan Roberts 441919eaf449SRyan Roberts /* Try allocating the highest of the remaining orders. */ 442019eaf449SRyan Roberts gfp = vma_thp_gfp_mask(vma); 442119eaf449SRyan Roberts while (orders) { 442219eaf449SRyan Roberts addr = ALIGN_DOWN(vmf->address, PAGE_SIZE << order); 442319eaf449SRyan Roberts folio = vma_alloc_folio(gfp, order, vma, addr, true); 442419eaf449SRyan Roberts if (folio) { 4425085ff35eSKefeng Wang if (mem_cgroup_charge(folio, vma->vm_mm, gfp)) { 4426085ff35eSKefeng Wang folio_put(folio); 4427085ff35eSKefeng Wang goto next; 4428085ff35eSKefeng Wang } 4429085ff35eSKefeng Wang folio_throttle_swaprate(folio, gfp); 443019eaf449SRyan Roberts clear_huge_page(&folio->page, vmf->address, 1 << order); 443119eaf449SRyan Roberts return folio; 443219eaf449SRyan Roberts } 4433085ff35eSKefeng Wang next: 443419eaf449SRyan Roberts order = next_order(&orders, order); 443519eaf449SRyan Roberts } 443619eaf449SRyan Roberts 443719eaf449SRyan Roberts fallback: 443819eaf449SRyan Roberts #endif 4439085ff35eSKefeng Wang return folio_prealloc(vma->vm_mm, vma, vmf->address, true); 444019eaf449SRyan Roberts } 444119eaf449SRyan Roberts 44421da177e4SLinus Torvalds /* 4443c1e8d7c6SMichel Lespinasse * We enter with non-exclusive mmap_lock (to exclude vma changes, 44448f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 4445c1e8d7c6SMichel Lespinasse * We return with mmap_lock still held, but pte unmapped and unlocked. 44461da177e4SLinus Torvalds */ 44472b740303SSouptick Joarder static vm_fault_t do_anonymous_page(struct vm_fault *vmf) 44481da177e4SLinus Torvalds { 44492bad466cSPeter Xu bool uffd_wp = vmf_orig_pte_uffd_wp(vmf); 445082b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 445119eaf449SRyan Roberts unsigned long addr = vmf->address; 44526bc56a4dSMatthew Wilcox (Oracle) struct folio *folio; 44532b740303SSouptick Joarder vm_fault_t ret = 0; 445419eaf449SRyan Roberts int nr_pages = 1; 44551da177e4SLinus Torvalds pte_t entry; 445619eaf449SRyan Roberts int i; 44571da177e4SLinus Torvalds 44586b7339f4SKirill A. Shutemov /* File mapping without ->vm_ops ? */ 44596b7339f4SKirill A. Shutemov if (vma->vm_flags & VM_SHARED) 44606b7339f4SKirill A. Shutemov return VM_FAULT_SIGBUS; 44616b7339f4SKirill A. Shutemov 44627267ec00SKirill A. Shutemov /* 44633db82b93SHugh Dickins * Use pte_alloc() instead of pte_alloc_map(), so that OOM can 44643db82b93SHugh Dickins * be distinguished from a transient failure of pte_offset_map(). 44657267ec00SKirill A. Shutemov */ 44664cf58924SJoel Fernandes (Google) if (pte_alloc(vma->vm_mm, vmf->pmd)) 44677267ec00SKirill A. Shutemov return VM_FAULT_OOM; 44687267ec00SKirill A. Shutemov 446911ac5524SLinus Torvalds /* Use the zero-page for reads */ 447082b0f8c3SJan Kara if (!(vmf->flags & FAULT_FLAG_WRITE) && 4471bae473a4SKirill A. Shutemov !mm_forbids_zeropage(vma->vm_mm)) { 447282b0f8c3SJan Kara entry = pte_mkspecial(pfn_pte(my_zero_pfn(vmf->address), 447362eede62SHugh Dickins vma->vm_page_prot)); 447482b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 447582b0f8c3SJan Kara vmf->address, &vmf->ptl); 44763db82b93SHugh Dickins if (!vmf->pte) 44773db82b93SHugh Dickins goto unlock; 44782bad466cSPeter Xu if (vmf_pte_changed(vmf)) { 44797df67697SBibo Mao update_mmu_tlb(vma, vmf->address, vmf->pte); 4480a13ea5b7SHugh Dickins goto unlock; 44817df67697SBibo Mao } 44826b31d595SMichal Hocko ret = check_stable_address_space(vma->vm_mm); 44836b31d595SMichal Hocko if (ret) 44846b31d595SMichal Hocko goto unlock; 44856b251fc9SAndrea Arcangeli /* Deliver the page fault to userland, check inside PT lock */ 44866b251fc9SAndrea Arcangeli if (userfaultfd_missing(vma)) { 448782b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 448882b0f8c3SJan Kara return handle_userfault(vmf, VM_UFFD_MISSING); 44896b251fc9SAndrea Arcangeli } 4490a13ea5b7SHugh Dickins goto setpte; 4491a13ea5b7SHugh Dickins } 4492a13ea5b7SHugh Dickins 44931da177e4SLinus Torvalds /* Allocate our own private page. */ 44941da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 449565500d23SHugh Dickins goto oom; 449619eaf449SRyan Roberts /* Returns NULL on OOM or ERR_PTR(-EAGAIN) if we must retry the fault */ 449719eaf449SRyan Roberts folio = alloc_anon_folio(vmf); 449819eaf449SRyan Roberts if (IS_ERR(folio)) 449919eaf449SRyan Roberts return 0; 45006bc56a4dSMatthew Wilcox (Oracle) if (!folio) 450165500d23SHugh Dickins goto oom; 4502eb3c24f3SMel Gorman 450319eaf449SRyan Roberts nr_pages = folio_nr_pages(folio); 450419eaf449SRyan Roberts addr = ALIGN_DOWN(vmf->address, nr_pages * PAGE_SIZE); 450519eaf449SRyan Roberts 450652f37629SMinchan Kim /* 4507cb3184deSMatthew Wilcox (Oracle) * The memory barrier inside __folio_mark_uptodate makes sure that 4508f4f5329dSWei Yang * preceding stores to the page contents become visible before 450952f37629SMinchan Kim * the set_pte_at() write. 451052f37629SMinchan Kim */ 4511cb3184deSMatthew Wilcox (Oracle) __folio_mark_uptodate(folio); 45121da177e4SLinus Torvalds 4513cb3184deSMatthew Wilcox (Oracle) entry = mk_pte(&folio->page, vma->vm_page_prot); 451450c25ee9SThomas Bogendoerfer entry = pte_sw_mkyoung(entry); 45151ac0cb5dSHugh Dickins if (vma->vm_flags & VM_WRITE) 4516161e393cSRick Edgecombe entry = pte_mkwrite(pte_mkdirty(entry), vma); 45178f4e2101SHugh Dickins 451819eaf449SRyan Roberts vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, addr, &vmf->ptl); 45193db82b93SHugh Dickins if (!vmf->pte) 45203db82b93SHugh Dickins goto release; 452119eaf449SRyan Roberts if (nr_pages == 1 && vmf_pte_changed(vmf)) { 452219eaf449SRyan Roberts update_mmu_tlb(vma, addr, vmf->pte); 452319eaf449SRyan Roberts goto release; 452419eaf449SRyan Roberts } else if (nr_pages > 1 && !pte_range_none(vmf->pte, nr_pages)) { 452519eaf449SRyan Roberts for (i = 0; i < nr_pages; i++) 452619eaf449SRyan Roberts update_mmu_tlb(vma, addr + PAGE_SIZE * i, vmf->pte + i); 45278f4e2101SHugh Dickins goto release; 45287df67697SBibo Mao } 45299ba69294SHugh Dickins 45306b31d595SMichal Hocko ret = check_stable_address_space(vma->vm_mm); 45316b31d595SMichal Hocko if (ret) 45326b31d595SMichal Hocko goto release; 45336b31d595SMichal Hocko 45346b251fc9SAndrea Arcangeli /* Deliver the page fault to userland, check inside PT lock */ 45356b251fc9SAndrea Arcangeli if (userfaultfd_missing(vma)) { 453682b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 4537cb3184deSMatthew Wilcox (Oracle) folio_put(folio); 453882b0f8c3SJan Kara return handle_userfault(vmf, VM_UFFD_MISSING); 45396b251fc9SAndrea Arcangeli } 45406b251fc9SAndrea Arcangeli 454119eaf449SRyan Roberts folio_ref_add(folio, nr_pages - 1); 454219eaf449SRyan Roberts add_mm_counter(vma->vm_mm, MM_ANONPAGES, nr_pages); 454319eaf449SRyan Roberts folio_add_new_anon_rmap(folio, vma, addr); 4544cb3184deSMatthew Wilcox (Oracle) folio_add_lru_vma(folio, vma); 4545a13ea5b7SHugh Dickins setpte: 45462bad466cSPeter Xu if (uffd_wp) 45472bad466cSPeter Xu entry = pte_mkuffd_wp(entry); 454819eaf449SRyan Roberts set_ptes(vma->vm_mm, addr, vmf->pte, entry, nr_pages); 45491da177e4SLinus Torvalds 45501da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 455119eaf449SRyan Roberts update_mmu_cache_range(vmf, vma, addr, vmf->pte, nr_pages); 455265500d23SHugh Dickins unlock: 45533db82b93SHugh Dickins if (vmf->pte) 455482b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 45556b31d595SMichal Hocko return ret; 45568f4e2101SHugh Dickins release: 4557cb3184deSMatthew Wilcox (Oracle) folio_put(folio); 45588f4e2101SHugh Dickins goto unlock; 455965500d23SHugh Dickins oom: 45601da177e4SLinus Torvalds return VM_FAULT_OOM; 45611da177e4SLinus Torvalds } 45621da177e4SLinus Torvalds 45639a95f3cfSPaul Cassella /* 4564c1e8d7c6SMichel Lespinasse * The mmap_lock must have been held on entry, and may have been 45659a95f3cfSPaul Cassella * released depending on flags and vma->vm_ops->fault() return value. 45669a95f3cfSPaul Cassella * See filemap_fault() and __lock_page_retry(). 45679a95f3cfSPaul Cassella */ 45682b740303SSouptick Joarder static vm_fault_t __do_fault(struct vm_fault *vmf) 45697eae74afSKirill A. Shutemov { 457082b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 457101d1e0e6SMatthew Wilcox (Oracle) struct folio *folio; 45722b740303SSouptick Joarder vm_fault_t ret; 45737eae74afSKirill A. Shutemov 457463f3655fSMichal Hocko /* 457563f3655fSMichal Hocko * Preallocate pte before we take page_lock because this might lead to 457663f3655fSMichal Hocko * deadlocks for memcg reclaim which waits for pages under writeback: 457763f3655fSMichal Hocko * lock_page(A) 457863f3655fSMichal Hocko * SetPageWriteback(A) 457963f3655fSMichal Hocko * unlock_page(A) 458063f3655fSMichal Hocko * lock_page(B) 458163f3655fSMichal Hocko * lock_page(B) 4582d383807aSYanfei Xu * pte_alloc_one 458363f3655fSMichal Hocko * shrink_page_list 458463f3655fSMichal Hocko * wait_on_page_writeback(A) 458563f3655fSMichal Hocko * SetPageWriteback(B) 458663f3655fSMichal Hocko * unlock_page(B) 458763f3655fSMichal Hocko * # flush A, B to clear the writeback 458863f3655fSMichal Hocko */ 458963f3655fSMichal Hocko if (pmd_none(*vmf->pmd) && !vmf->prealloc_pte) { 4590a7069ee3SYanfei Xu vmf->prealloc_pte = pte_alloc_one(vma->vm_mm); 459163f3655fSMichal Hocko if (!vmf->prealloc_pte) 459263f3655fSMichal Hocko return VM_FAULT_OOM; 459363f3655fSMichal Hocko } 459463f3655fSMichal Hocko 459511bac800SDave Jiang ret = vma->vm_ops->fault(vmf); 45963917048dSJan Kara if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY | 4597b1aa812bSJan Kara VM_FAULT_DONE_COW))) 45987eae74afSKirill A. Shutemov return ret; 45997eae74afSKirill A. Shutemov 460001d1e0e6SMatthew Wilcox (Oracle) folio = page_folio(vmf->page); 4601667240e0SJan Kara if (unlikely(PageHWPoison(vmf->page))) { 4602e53ac737SRik van Riel vm_fault_t poisonret = VM_FAULT_HWPOISON; 4603e53ac737SRik van Riel if (ret & VM_FAULT_LOCKED) { 460401d1e0e6SMatthew Wilcox (Oracle) if (page_mapped(vmf->page)) 460501d1e0e6SMatthew Wilcox (Oracle) unmap_mapping_folio(folio); 460601d1e0e6SMatthew Wilcox (Oracle) /* Retry if a clean folio was removed from the cache. */ 460701d1e0e6SMatthew Wilcox (Oracle) if (mapping_evict_folio(folio->mapping, folio)) 46083149c79fSRik van Riel poisonret = VM_FAULT_NOPAGE; 460901d1e0e6SMatthew Wilcox (Oracle) folio_unlock(folio); 4610e53ac737SRik van Riel } 461101d1e0e6SMatthew Wilcox (Oracle) folio_put(folio); 4612936ca80dSJan Kara vmf->page = NULL; 4613e53ac737SRik van Riel return poisonret; 46147eae74afSKirill A. Shutemov } 46157eae74afSKirill A. Shutemov 46167eae74afSKirill A. Shutemov if (unlikely(!(ret & VM_FAULT_LOCKED))) 461701d1e0e6SMatthew Wilcox (Oracle) folio_lock(folio); 46187eae74afSKirill A. Shutemov else 461901d1e0e6SMatthew Wilcox (Oracle) VM_BUG_ON_PAGE(!folio_test_locked(folio), vmf->page); 46207eae74afSKirill A. Shutemov 46217eae74afSKirill A. Shutemov return ret; 46227eae74afSKirill A. Shutemov } 46237eae74afSKirill A. Shutemov 4624396bcc52SMatthew Wilcox (Oracle) #ifdef CONFIG_TRANSPARENT_HUGEPAGE 462582b0f8c3SJan Kara static void deposit_prealloc_pte(struct vm_fault *vmf) 4626953c66c2SAneesh Kumar K.V { 462782b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 4628953c66c2SAneesh Kumar K.V 462982b0f8c3SJan Kara pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, vmf->prealloc_pte); 4630953c66c2SAneesh Kumar K.V /* 4631953c66c2SAneesh Kumar K.V * We are going to consume the prealloc table, 4632953c66c2SAneesh Kumar K.V * count that as nr_ptes. 4633953c66c2SAneesh Kumar K.V */ 4634c4812909SKirill A. Shutemov mm_inc_nr_ptes(vma->vm_mm); 46357f2b6ce8STobin C Harding vmf->prealloc_pte = NULL; 4636953c66c2SAneesh Kumar K.V } 4637953c66c2SAneesh Kumar K.V 4638f9ce0be7SKirill A. Shutemov vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page) 463910102459SKirill A. Shutemov { 4640ef37b2eaSDavid Hildenbrand struct folio *folio = page_folio(page); 464182b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 464282b0f8c3SJan Kara bool write = vmf->flags & FAULT_FLAG_WRITE; 464382b0f8c3SJan Kara unsigned long haddr = vmf->address & HPAGE_PMD_MASK; 464410102459SKirill A. Shutemov pmd_t entry; 4645d01ac3c3SMatthew Wilcox (Oracle) vm_fault_t ret = VM_FAULT_FALLBACK; 464610102459SKirill A. Shutemov 46473485b883SRyan Roberts if (!thp_vma_suitable_order(vma, haddr, PMD_ORDER)) 4648d01ac3c3SMatthew Wilcox (Oracle) return ret; 464910102459SKirill A. Shutemov 4650ef37b2eaSDavid Hildenbrand if (page != &folio->page || folio_order(folio) != HPAGE_PMD_ORDER) 4651d01ac3c3SMatthew Wilcox (Oracle) return ret; 465210102459SKirill A. Shutemov 4653953c66c2SAneesh Kumar K.V /* 4654eac96c3eSYang Shi * Just backoff if any subpage of a THP is corrupted otherwise 4655eac96c3eSYang Shi * the corrupted page may mapped by PMD silently to escape the 4656eac96c3eSYang Shi * check. This kind of THP just can be PTE mapped. Access to 4657eac96c3eSYang Shi * the corrupted subpage should trigger SIGBUS as expected. 4658eac96c3eSYang Shi */ 4659ef37b2eaSDavid Hildenbrand if (unlikely(folio_test_has_hwpoisoned(folio))) 4660eac96c3eSYang Shi return ret; 4661eac96c3eSYang Shi 4662eac96c3eSYang Shi /* 4663f0953a1bSIngo Molnar * Archs like ppc64 need additional space to store information 4664953c66c2SAneesh Kumar K.V * related to pte entry. Use the preallocated table for that. 4665953c66c2SAneesh Kumar K.V */ 466682b0f8c3SJan Kara if (arch_needs_pgtable_deposit() && !vmf->prealloc_pte) { 46674cf58924SJoel Fernandes (Google) vmf->prealloc_pte = pte_alloc_one(vma->vm_mm); 466882b0f8c3SJan Kara if (!vmf->prealloc_pte) 4669953c66c2SAneesh Kumar K.V return VM_FAULT_OOM; 4670953c66c2SAneesh Kumar K.V } 4671953c66c2SAneesh Kumar K.V 467282b0f8c3SJan Kara vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); 467382b0f8c3SJan Kara if (unlikely(!pmd_none(*vmf->pmd))) 467410102459SKirill A. Shutemov goto out; 467510102459SKirill A. Shutemov 46769f1f5b60SMatthew Wilcox (Oracle) flush_icache_pages(vma, page, HPAGE_PMD_NR); 467710102459SKirill A. Shutemov 467810102459SKirill A. Shutemov entry = mk_huge_pmd(page, vma->vm_page_prot); 467910102459SKirill A. Shutemov if (write) 4680f55e1014SLinus Torvalds entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); 468110102459SKirill A. Shutemov 46826b27cc6cSKefeng Wang add_mm_counter(vma->vm_mm, mm_counter_file(folio), HPAGE_PMD_NR); 4683ef37b2eaSDavid Hildenbrand folio_add_file_rmap_pmd(folio, page, vma); 4684cea86fe2SHugh Dickins 4685953c66c2SAneesh Kumar K.V /* 4686953c66c2SAneesh Kumar K.V * deposit and withdraw with pmd lock held 4687953c66c2SAneesh Kumar K.V */ 4688953c66c2SAneesh Kumar K.V if (arch_needs_pgtable_deposit()) 468982b0f8c3SJan Kara deposit_prealloc_pte(vmf); 469010102459SKirill A. Shutemov 469182b0f8c3SJan Kara set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry); 469210102459SKirill A. Shutemov 469382b0f8c3SJan Kara update_mmu_cache_pmd(vma, haddr, vmf->pmd); 469410102459SKirill A. Shutemov 469510102459SKirill A. Shutemov /* fault is handled */ 469610102459SKirill A. Shutemov ret = 0; 469795ecedcdSKirill A. Shutemov count_vm_event(THP_FILE_MAPPED); 469810102459SKirill A. Shutemov out: 469982b0f8c3SJan Kara spin_unlock(vmf->ptl); 470010102459SKirill A. Shutemov return ret; 470110102459SKirill A. Shutemov } 470210102459SKirill A. Shutemov #else 4703f9ce0be7SKirill A. Shutemov vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page) 470410102459SKirill A. Shutemov { 4705f9ce0be7SKirill A. Shutemov return VM_FAULT_FALLBACK; 470610102459SKirill A. Shutemov } 470710102459SKirill A. Shutemov #endif 470810102459SKirill A. Shutemov 47093bd786f7SYin Fengwei /** 47103bd786f7SYin Fengwei * set_pte_range - Set a range of PTEs to point to pages in a folio. 47113bd786f7SYin Fengwei * @vmf: Fault decription. 47123bd786f7SYin Fengwei * @folio: The folio that contains @page. 47133bd786f7SYin Fengwei * @page: The first page to create a PTE for. 47143bd786f7SYin Fengwei * @nr: The number of PTEs to create. 47153bd786f7SYin Fengwei * @addr: The first address to create a PTE for. 47163bd786f7SYin Fengwei */ 47173bd786f7SYin Fengwei void set_pte_range(struct vm_fault *vmf, struct folio *folio, 47183bd786f7SYin Fengwei struct page *page, unsigned int nr, unsigned long addr) 47193bb97794SKirill A. Shutemov { 472082b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 47212bad466cSPeter Xu bool uffd_wp = vmf_orig_pte_uffd_wp(vmf); 472282b0f8c3SJan Kara bool write = vmf->flags & FAULT_FLAG_WRITE; 47233bd786f7SYin Fengwei bool prefault = in_range(vmf->address, addr, nr * PAGE_SIZE); 47243bb97794SKirill A. Shutemov pte_t entry; 47257267ec00SKirill A. Shutemov 47263bd786f7SYin Fengwei flush_icache_pages(vma, page, nr); 47273bb97794SKirill A. Shutemov entry = mk_pte(page, vma->vm_page_prot); 472846bdb427SWill Deacon 472946bdb427SWill Deacon if (prefault && arch_wants_old_prefaulted_pte()) 473046bdb427SWill Deacon entry = pte_mkold(entry); 473150c25ee9SThomas Bogendoerfer else 473250c25ee9SThomas Bogendoerfer entry = pte_sw_mkyoung(entry); 473346bdb427SWill Deacon 47343bb97794SKirill A. Shutemov if (write) 47353bb97794SKirill A. Shutemov entry = maybe_mkwrite(pte_mkdirty(entry), vma); 47369c28a205SPeter Xu if (unlikely(uffd_wp)) 4737f1eb1bacSPeter Xu entry = pte_mkuffd_wp(entry); 4738bae473a4SKirill A. Shutemov /* copy-on-write page */ 4739bae473a4SKirill A. Shutemov if (write && !(vma->vm_flags & VM_SHARED)) { 47403bd786f7SYin Fengwei add_mm_counter(vma->vm_mm, MM_ANONPAGES, nr); 47413bd786f7SYin Fengwei VM_BUG_ON_FOLIO(nr != 1, folio); 47423bd786f7SYin Fengwei folio_add_new_anon_rmap(folio, vma, addr); 47433bd786f7SYin Fengwei folio_add_lru_vma(folio, vma); 47443bb97794SKirill A. Shutemov } else { 47456b27cc6cSKefeng Wang add_mm_counter(vma->vm_mm, mm_counter_file(folio), nr); 474668f03208SDavid Hildenbrand folio_add_file_rmap_ptes(folio, page, nr, vma); 47473bb97794SKirill A. Shutemov } 47483bd786f7SYin Fengwei set_ptes(vma->vm_mm, addr, vmf->pte, entry, nr); 47493bd786f7SYin Fengwei 47503bd786f7SYin Fengwei /* no need to invalidate: a not-present page won't be cached */ 47513bd786f7SYin Fengwei update_mmu_cache_range(vmf, vma, addr, vmf->pte, nr); 47523bb97794SKirill A. Shutemov } 47533bb97794SKirill A. Shutemov 4754f46f2adeSPeter Xu static bool vmf_pte_changed(struct vm_fault *vmf) 4755f46f2adeSPeter Xu { 4756f46f2adeSPeter Xu if (vmf->flags & FAULT_FLAG_ORIG_PTE_VALID) 4757c33c7948SRyan Roberts return !pte_same(ptep_get(vmf->pte), vmf->orig_pte); 4758f46f2adeSPeter Xu 4759c33c7948SRyan Roberts return !pte_none(ptep_get(vmf->pte)); 4760f46f2adeSPeter Xu } 4761f46f2adeSPeter Xu 47629118c0cbSJan Kara /** 47639118c0cbSJan Kara * finish_fault - finish page fault once we have prepared the page to fault 47649118c0cbSJan Kara * 47659118c0cbSJan Kara * @vmf: structure describing the fault 47669118c0cbSJan Kara * 47679118c0cbSJan Kara * This function handles all that is needed to finish a page fault once the 47689118c0cbSJan Kara * page to fault in is prepared. It handles locking of PTEs, inserts PTE for 47699118c0cbSJan Kara * given page, adds reverse page mapping, handles memcg charges and LRU 4770a862f68aSMike Rapoport * addition. 47719118c0cbSJan Kara * 47729118c0cbSJan Kara * The function expects the page to be locked and on success it consumes a 47739118c0cbSJan Kara * reference of a page being mapped (for the PTE which maps it). 4774a862f68aSMike Rapoport * 4775a862f68aSMike Rapoport * Return: %0 on success, %VM_FAULT_ code in case of error. 47769118c0cbSJan Kara */ 47772b740303SSouptick Joarder vm_fault_t finish_fault(struct vm_fault *vmf) 47789118c0cbSJan Kara { 4779f9ce0be7SKirill A. Shutemov struct vm_area_struct *vma = vmf->vma; 47809118c0cbSJan Kara struct page *page; 4781f9ce0be7SKirill A. Shutemov vm_fault_t ret; 47829118c0cbSJan Kara 47839118c0cbSJan Kara /* Did we COW the page? */ 4784f9ce0be7SKirill A. Shutemov if ((vmf->flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) 47859118c0cbSJan Kara page = vmf->cow_page; 47869118c0cbSJan Kara else 47879118c0cbSJan Kara page = vmf->page; 47886b31d595SMichal Hocko 47896b31d595SMichal Hocko /* 47906b31d595SMichal Hocko * check even for read faults because we might have lost our CoWed 47916b31d595SMichal Hocko * page 47926b31d595SMichal Hocko */ 4793f9ce0be7SKirill A. Shutemov if (!(vma->vm_flags & VM_SHARED)) { 4794f9ce0be7SKirill A. Shutemov ret = check_stable_address_space(vma->vm_mm); 4795f9ce0be7SKirill A. Shutemov if (ret) 4796f9ce0be7SKirill A. Shutemov return ret; 4797f9ce0be7SKirill A. Shutemov } 4798f9ce0be7SKirill A. Shutemov 4799f9ce0be7SKirill A. Shutemov if (pmd_none(*vmf->pmd)) { 4800f9ce0be7SKirill A. Shutemov if (PageTransCompound(page)) { 4801f9ce0be7SKirill A. Shutemov ret = do_set_pmd(vmf, page); 4802f9ce0be7SKirill A. Shutemov if (ret != VM_FAULT_FALLBACK) 4803f9ce0be7SKirill A. Shutemov return ret; 4804f9ce0be7SKirill A. Shutemov } 4805f9ce0be7SKirill A. Shutemov 480603c4f204SQi Zheng if (vmf->prealloc_pte) 480703c4f204SQi Zheng pmd_install(vma->vm_mm, vmf->pmd, &vmf->prealloc_pte); 480803c4f204SQi Zheng else if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd))) 4809f9ce0be7SKirill A. Shutemov return VM_FAULT_OOM; 4810f9ce0be7SKirill A. Shutemov } 4811f9ce0be7SKirill A. Shutemov 4812f9ce0be7SKirill A. Shutemov vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 4813f9ce0be7SKirill A. Shutemov vmf->address, &vmf->ptl); 48143db82b93SHugh Dickins if (!vmf->pte) 48153db82b93SHugh Dickins return VM_FAULT_NOPAGE; 4816f9ce0be7SKirill A. Shutemov 481770427f6eSSergei Antonov /* Re-check under ptl */ 481870427f6eSSergei Antonov if (likely(!vmf_pte_changed(vmf))) { 48193bd786f7SYin Fengwei struct folio *folio = page_folio(page); 482070427f6eSSergei Antonov 48213bd786f7SYin Fengwei set_pte_range(vmf, folio, page, 1, vmf->address); 482270427f6eSSergei Antonov ret = 0; 482370427f6eSSergei Antonov } else { 4824f9ce0be7SKirill A. Shutemov update_mmu_tlb(vma, vmf->address, vmf->pte); 482570427f6eSSergei Antonov ret = VM_FAULT_NOPAGE; 482670427f6eSSergei Antonov } 482770427f6eSSergei Antonov 48289118c0cbSJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 48299118c0cbSJan Kara return ret; 48309118c0cbSJan Kara } 48319118c0cbSJan Kara 483253d36a56SLorenzo Stoakes static unsigned long fault_around_pages __read_mostly = 483353d36a56SLorenzo Stoakes 65536 >> PAGE_SHIFT; 4834a9b0f861SKirill A. Shutemov 48351592eef0SKirill A. Shutemov #ifdef CONFIG_DEBUG_FS 4836a9b0f861SKirill A. Shutemov static int fault_around_bytes_get(void *data, u64 *val) 48371592eef0SKirill A. Shutemov { 483853d36a56SLorenzo Stoakes *val = fault_around_pages << PAGE_SHIFT; 48391592eef0SKirill A. Shutemov return 0; 48401592eef0SKirill A. Shutemov } 48411592eef0SKirill A. Shutemov 4842b4903d6eSAndrey Ryabinin /* 4843da391d64SWilliam Kucharski * fault_around_bytes must be rounded down to the nearest page order as it's 4844da391d64SWilliam Kucharski * what do_fault_around() expects to see. 4845b4903d6eSAndrey Ryabinin */ 4846a9b0f861SKirill A. Shutemov static int fault_around_bytes_set(void *data, u64 val) 48471592eef0SKirill A. Shutemov { 4848a9b0f861SKirill A. Shutemov if (val / PAGE_SIZE > PTRS_PER_PTE) 48491592eef0SKirill A. Shutemov return -EINVAL; 485053d36a56SLorenzo Stoakes 485153d36a56SLorenzo Stoakes /* 485253d36a56SLorenzo Stoakes * The minimum value is 1 page, however this results in no fault-around 485353d36a56SLorenzo Stoakes * at all. See should_fault_around(). 485453d36a56SLorenzo Stoakes */ 485553d36a56SLorenzo Stoakes fault_around_pages = max(rounddown_pow_of_two(val) >> PAGE_SHIFT, 1UL); 485653d36a56SLorenzo Stoakes 48571592eef0SKirill A. Shutemov return 0; 48581592eef0SKirill A. Shutemov } 48590a1345f8SYevgen Pronenko DEFINE_DEBUGFS_ATTRIBUTE(fault_around_bytes_fops, 4860a9b0f861SKirill A. Shutemov fault_around_bytes_get, fault_around_bytes_set, "%llu\n"); 48611592eef0SKirill A. Shutemov 48621592eef0SKirill A. Shutemov static int __init fault_around_debugfs(void) 48631592eef0SKirill A. Shutemov { 4864d9f7979cSGreg Kroah-Hartman debugfs_create_file_unsafe("fault_around_bytes", 0644, NULL, NULL, 4865a9b0f861SKirill A. Shutemov &fault_around_bytes_fops); 48661592eef0SKirill A. Shutemov return 0; 48671592eef0SKirill A. Shutemov } 48681592eef0SKirill A. Shutemov late_initcall(fault_around_debugfs); 48691592eef0SKirill A. Shutemov #endif 48708c6e50b0SKirill A. Shutemov 48711fdb412bSKirill A. Shutemov /* 48721fdb412bSKirill A. Shutemov * do_fault_around() tries to map few pages around the fault address. The hope 48731fdb412bSKirill A. Shutemov * is that the pages will be needed soon and this will lower the number of 48741fdb412bSKirill A. Shutemov * faults to handle. 48751fdb412bSKirill A. Shutemov * 48761fdb412bSKirill A. Shutemov * It uses vm_ops->map_pages() to map the pages, which skips the page if it's 48771fdb412bSKirill A. Shutemov * not ready to be mapped: not up-to-date, locked, etc. 48781fdb412bSKirill A. Shutemov * 48799042599eSLorenzo Stoakes * This function doesn't cross VMA or page table boundaries, in order to call 48809042599eSLorenzo Stoakes * map_pages() and acquire a PTE lock only once. 48811fdb412bSKirill A. Shutemov * 488253d36a56SLorenzo Stoakes * fault_around_pages defines how many pages we'll try to map. 4883da391d64SWilliam Kucharski * do_fault_around() expects it to be set to a power of two less than or equal 4884da391d64SWilliam Kucharski * to PTRS_PER_PTE. 48851fdb412bSKirill A. Shutemov * 4886da391d64SWilliam Kucharski * The virtual address of the area that we map is naturally aligned to 488753d36a56SLorenzo Stoakes * fault_around_pages * PAGE_SIZE rounded down to the machine page size 4888da391d64SWilliam Kucharski * (and therefore to page order). This way it's easier to guarantee 4889da391d64SWilliam Kucharski * that we don't cross page table boundaries. 48901fdb412bSKirill A. Shutemov */ 48912b740303SSouptick Joarder static vm_fault_t do_fault_around(struct vm_fault *vmf) 48928c6e50b0SKirill A. Shutemov { 489353d36a56SLorenzo Stoakes pgoff_t nr_pages = READ_ONCE(fault_around_pages); 48949042599eSLorenzo Stoakes pgoff_t pte_off = pte_index(vmf->address); 48959042599eSLorenzo Stoakes /* The page offset of vmf->address within the VMA. */ 48969042599eSLorenzo Stoakes pgoff_t vma_off = vmf->pgoff - vmf->vma->vm_pgoff; 48979042599eSLorenzo Stoakes pgoff_t from_pte, to_pte; 489858ef47efSMatthew Wilcox (Oracle) vm_fault_t ret; 48998c6e50b0SKirill A. Shutemov 49009042599eSLorenzo Stoakes /* The PTE offset of the start address, clamped to the VMA. */ 49019042599eSLorenzo Stoakes from_pte = max(ALIGN_DOWN(pte_off, nr_pages), 49029042599eSLorenzo Stoakes pte_off - min(pte_off, vma_off)); 4903aecd6f44SKirill A. Shutemov 49049042599eSLorenzo Stoakes /* The PTE offset of the end address, clamped to the VMA and PTE. */ 49059042599eSLorenzo Stoakes to_pte = min3(from_pte + nr_pages, (pgoff_t)PTRS_PER_PTE, 49069042599eSLorenzo Stoakes pte_off + vma_pages(vmf->vma) - vma_off) - 1; 49078c6e50b0SKirill A. Shutemov 490882b0f8c3SJan Kara if (pmd_none(*vmf->pmd)) { 49094cf58924SJoel Fernandes (Google) vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm); 491082b0f8c3SJan Kara if (!vmf->prealloc_pte) 4911f9ce0be7SKirill A. Shutemov return VM_FAULT_OOM; 49128c6e50b0SKirill A. Shutemov } 49138c6e50b0SKirill A. Shutemov 491458ef47efSMatthew Wilcox (Oracle) rcu_read_lock(); 491558ef47efSMatthew Wilcox (Oracle) ret = vmf->vma->vm_ops->map_pages(vmf, 49169042599eSLorenzo Stoakes vmf->pgoff + from_pte - pte_off, 49179042599eSLorenzo Stoakes vmf->pgoff + to_pte - pte_off); 491858ef47efSMatthew Wilcox (Oracle) rcu_read_unlock(); 491958ef47efSMatthew Wilcox (Oracle) 492058ef47efSMatthew Wilcox (Oracle) return ret; 49217267ec00SKirill A. Shutemov } 49227267ec00SKirill A. Shutemov 49239c28a205SPeter Xu /* Return true if we should do read fault-around, false otherwise */ 49249c28a205SPeter Xu static inline bool should_fault_around(struct vm_fault *vmf) 49259c28a205SPeter Xu { 49269c28a205SPeter Xu /* No ->map_pages? No way to fault around... */ 49279c28a205SPeter Xu if (!vmf->vma->vm_ops->map_pages) 49289c28a205SPeter Xu return false; 49299c28a205SPeter Xu 49309c28a205SPeter Xu if (uffd_disable_fault_around(vmf->vma)) 49319c28a205SPeter Xu return false; 49329c28a205SPeter Xu 493353d36a56SLorenzo Stoakes /* A single page implies no faulting 'around' at all. */ 493453d36a56SLorenzo Stoakes return fault_around_pages > 1; 49359c28a205SPeter Xu } 49369c28a205SPeter Xu 49372b740303SSouptick Joarder static vm_fault_t do_read_fault(struct vm_fault *vmf) 4938e655fb29SKirill A. Shutemov { 49392b740303SSouptick Joarder vm_fault_t ret = 0; 494022d1e68fSSidhartha Kumar struct folio *folio; 49418c6e50b0SKirill A. Shutemov 49428c6e50b0SKirill A. Shutemov /* 49438c6e50b0SKirill A. Shutemov * Let's call ->map_pages() first and use ->fault() as fallback 49448c6e50b0SKirill A. Shutemov * if page by the offset is not ready to be mapped (cold cache or 49458c6e50b0SKirill A. Shutemov * something). 49468c6e50b0SKirill A. Shutemov */ 49479c28a205SPeter Xu if (should_fault_around(vmf)) { 49480721ec8bSJan Kara ret = do_fault_around(vmf); 49497267ec00SKirill A. Shutemov if (ret) 49507267ec00SKirill A. Shutemov return ret; 49518c6e50b0SKirill A. Shutemov } 4952e655fb29SKirill A. Shutemov 495312214ebaSMatthew Wilcox (Oracle) ret = vmf_can_call_fault(vmf); 495412214ebaSMatthew Wilcox (Oracle) if (ret) 495512214ebaSMatthew Wilcox (Oracle) return ret; 4956f5617ffeSMatthew Wilcox (Oracle) 4957936ca80dSJan Kara ret = __do_fault(vmf); 4958e655fb29SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 4959e655fb29SKirill A. Shutemov return ret; 4960e655fb29SKirill A. Shutemov 49619118c0cbSJan Kara ret |= finish_fault(vmf); 496222d1e68fSSidhartha Kumar folio = page_folio(vmf->page); 496322d1e68fSSidhartha Kumar folio_unlock(folio); 49647267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 496522d1e68fSSidhartha Kumar folio_put(folio); 4966e655fb29SKirill A. Shutemov return ret; 4967e655fb29SKirill A. Shutemov } 4968e655fb29SKirill A. Shutemov 49692b740303SSouptick Joarder static vm_fault_t do_cow_fault(struct vm_fault *vmf) 4970ec47c3b9SKirill A. Shutemov { 497182b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 4972e4621e70SKefeng Wang struct folio *folio; 49732b740303SSouptick Joarder vm_fault_t ret; 4974ec47c3b9SKirill A. Shutemov 49754de8c93aSMatthew Wilcox (Oracle) ret = vmf_can_call_fault(vmf); 49764de8c93aSMatthew Wilcox (Oracle) if (!ret) 49774de8c93aSMatthew Wilcox (Oracle) ret = vmf_anon_prepare(vmf); 49784de8c93aSMatthew Wilcox (Oracle) if (ret) 49794de8c93aSMatthew Wilcox (Oracle) return ret; 4980ec47c3b9SKirill A. Shutemov 4981e4621e70SKefeng Wang folio = folio_prealloc(vma->vm_mm, vma, vmf->address, false); 4982e4621e70SKefeng Wang if (!folio) 4983ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 4984ec47c3b9SKirill A. Shutemov 4985e4621e70SKefeng Wang vmf->cow_page = &folio->page; 4986ec47c3b9SKirill A. Shutemov 4987936ca80dSJan Kara ret = __do_fault(vmf); 4988ec47c3b9SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 4989ec47c3b9SKirill A. Shutemov goto uncharge_out; 49903917048dSJan Kara if (ret & VM_FAULT_DONE_COW) 49913917048dSJan Kara return ret; 4992ec47c3b9SKirill A. Shutemov 4993936ca80dSJan Kara copy_user_highpage(vmf->cow_page, vmf->page, vmf->address, vma); 4994e4621e70SKefeng Wang __folio_mark_uptodate(folio); 4995ec47c3b9SKirill A. Shutemov 49969118c0cbSJan Kara ret |= finish_fault(vmf); 4997936ca80dSJan Kara unlock_page(vmf->page); 4998936ca80dSJan Kara put_page(vmf->page); 49997267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 50007267ec00SKirill A. Shutemov goto uncharge_out; 5001ec47c3b9SKirill A. Shutemov return ret; 5002ec47c3b9SKirill A. Shutemov uncharge_out: 5003e4621e70SKefeng Wang folio_put(folio); 5004ec47c3b9SKirill A. Shutemov return ret; 5005ec47c3b9SKirill A. Shutemov } 5006ec47c3b9SKirill A. Shutemov 50072b740303SSouptick Joarder static vm_fault_t do_shared_fault(struct vm_fault *vmf) 50081da177e4SLinus Torvalds { 500982b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 50102b740303SSouptick Joarder vm_fault_t ret, tmp; 50116f609b7eSSidhartha Kumar struct folio *folio; 50121d65f86dSKAMEZAWA Hiroyuki 50134ed43798SMatthew Wilcox (Oracle) ret = vmf_can_call_fault(vmf); 50144ed43798SMatthew Wilcox (Oracle) if (ret) 50154ed43798SMatthew Wilcox (Oracle) return ret; 501654cb8821SNick Piggin 5017936ca80dSJan Kara ret = __do_fault(vmf); 50187eae74afSKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 5019f0c6d4d2SKirill A. Shutemov return ret; 50201da177e4SLinus Torvalds 50216f609b7eSSidhartha Kumar folio = page_folio(vmf->page); 50226f609b7eSSidhartha Kumar 50231da177e4SLinus Torvalds /* 5024f0c6d4d2SKirill A. Shutemov * Check if the backing address space wants to know that the page is 5025f0c6d4d2SKirill A. Shutemov * about to become writable 50261da177e4SLinus Torvalds */ 5027fb09a464SKirill A. Shutemov if (vma->vm_ops->page_mkwrite) { 50286f609b7eSSidhartha Kumar folio_unlock(folio); 502986aa6998SSidhartha Kumar tmp = do_page_mkwrite(vmf, folio); 5030fb09a464SKirill A. Shutemov if (unlikely(!tmp || 5031fb09a464SKirill A. Shutemov (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { 50326f609b7eSSidhartha Kumar folio_put(folio); 5033f0c6d4d2SKirill A. Shutemov return tmp; 503469676147SMark Fasheh } 5035d0217ac0SNick Piggin } 5036fb09a464SKirill A. Shutemov 50379118c0cbSJan Kara ret |= finish_fault(vmf); 50387267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | 50397267ec00SKirill A. Shutemov VM_FAULT_RETRY))) { 50406f609b7eSSidhartha Kumar folio_unlock(folio); 50416f609b7eSSidhartha Kumar folio_put(folio); 5042f0c6d4d2SKirill A. Shutemov return ret; 50439637a5efSDavid Howells } 5044d00806b1SNick Piggin 504589b15332SJohannes Weiner ret |= fault_dirty_shared_page(vmf); 5046b827e496SNick Piggin return ret; 504754cb8821SNick Piggin } 5048d00806b1SNick Piggin 50499a95f3cfSPaul Cassella /* 5050c1e8d7c6SMichel Lespinasse * We enter with non-exclusive mmap_lock (to exclude vma changes, 50519a95f3cfSPaul Cassella * but allow concurrent faults). 5052c1e8d7c6SMichel Lespinasse * The mmap_lock may have been released depending on flags and our 50539138e47eSMatthew Wilcox (Oracle) * return value. See filemap_fault() and __folio_lock_or_retry(). 5054c1e8d7c6SMichel Lespinasse * If mmap_lock is released, vma may become invalid (for example 5055fc8efd2dSJan Stancek * by other thread calling munmap()). 50569a95f3cfSPaul Cassella */ 50572b740303SSouptick Joarder static vm_fault_t do_fault(struct vm_fault *vmf) 505854cb8821SNick Piggin { 505982b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 5060fc8efd2dSJan Stancek struct mm_struct *vm_mm = vma->vm_mm; 50612b740303SSouptick Joarder vm_fault_t ret; 506254cb8821SNick Piggin 5063ff09d7ecSAneesh Kumar K.V /* 5064ff09d7ecSAneesh Kumar K.V * The VMA was not fully populated on mmap() or missing VM_DONTEXPAND 5065ff09d7ecSAneesh Kumar K.V */ 5066ff09d7ecSAneesh Kumar K.V if (!vma->vm_ops->fault) { 50673db82b93SHugh Dickins vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, 50683db82b93SHugh Dickins vmf->address, &vmf->ptl); 50693db82b93SHugh Dickins if (unlikely(!vmf->pte)) 5070b0b9b3dfSHugh Dickins ret = VM_FAULT_SIGBUS; 5071ff09d7ecSAneesh Kumar K.V else { 5072ff09d7ecSAneesh Kumar K.V /* 5073ff09d7ecSAneesh Kumar K.V * Make sure this is not a temporary clearing of pte 5074ff09d7ecSAneesh Kumar K.V * by holding ptl and checking again. A R/M/W update 5075ff09d7ecSAneesh Kumar K.V * of pte involves: take ptl, clearing the pte so that 5076ff09d7ecSAneesh Kumar K.V * we don't have concurrent modification by hardware 5077ff09d7ecSAneesh Kumar K.V * followed by an update. 5078ff09d7ecSAneesh Kumar K.V */ 5079c33c7948SRyan Roberts if (unlikely(pte_none(ptep_get(vmf->pte)))) 5080ff09d7ecSAneesh Kumar K.V ret = VM_FAULT_SIGBUS; 5081ff09d7ecSAneesh Kumar K.V else 5082ff09d7ecSAneesh Kumar K.V ret = VM_FAULT_NOPAGE; 5083ff09d7ecSAneesh Kumar K.V 5084ff09d7ecSAneesh Kumar K.V pte_unmap_unlock(vmf->pte, vmf->ptl); 5085ff09d7ecSAneesh Kumar K.V } 5086ff09d7ecSAneesh Kumar K.V } else if (!(vmf->flags & FAULT_FLAG_WRITE)) 5087b0b9b3dfSHugh Dickins ret = do_read_fault(vmf); 5088b0b9b3dfSHugh Dickins else if (!(vma->vm_flags & VM_SHARED)) 5089b0b9b3dfSHugh Dickins ret = do_cow_fault(vmf); 5090b0b9b3dfSHugh Dickins else 5091b0b9b3dfSHugh Dickins ret = do_shared_fault(vmf); 5092b0b9b3dfSHugh Dickins 5093b0b9b3dfSHugh Dickins /* preallocated pagetable is unused: free it */ 5094b0b9b3dfSHugh Dickins if (vmf->prealloc_pte) { 5095fc8efd2dSJan Stancek pte_free(vm_mm, vmf->prealloc_pte); 50967f2b6ce8STobin C Harding vmf->prealloc_pte = NULL; 5097b0b9b3dfSHugh Dickins } 5098b0b9b3dfSHugh Dickins return ret; 509954cb8821SNick Piggin } 510054cb8821SNick Piggin 5101cda6d936SKefeng Wang int numa_migrate_prep(struct folio *folio, struct vm_area_struct *vma, 5102f4c0d836SYang Shi unsigned long addr, int page_nid, int *flags) 51039532fec1SMel Gorman { 5104cda6d936SKefeng Wang folio_get(folio); 51059532fec1SMel Gorman 5106fc137c0dSRaghavendra K T /* Record the current PID acceesing VMA */ 5107fc137c0dSRaghavendra K T vma_set_access_pid_bit(vma); 5108fc137c0dSRaghavendra K T 51099532fec1SMel Gorman count_vm_numa_event(NUMA_HINT_FAULTS); 511004bb2f94SRik van Riel if (page_nid == numa_node_id()) { 51119532fec1SMel Gorman count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL); 511204bb2f94SRik van Riel *flags |= TNF_FAULT_LOCAL; 511304bb2f94SRik van Riel } 51149532fec1SMel Gorman 511575c70128SKefeng Wang return mpol_misplaced(folio, vma, addr); 51169532fec1SMel Gorman } 51179532fec1SMel Gorman 51182b740303SSouptick Joarder static vm_fault_t do_numa_page(struct vm_fault *vmf) 5119d10e63f2SMel Gorman { 512082b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 51216695cf68SKefeng Wang struct folio *folio = NULL; 51226695cf68SKefeng Wang int nid = NUMA_NO_NODE; 51236a56ccbcSDavid Hildenbrand bool writable = false; 512490572890SPeter Zijlstra int last_cpupid; 5125cbee9f88SPeter Zijlstra int target_nid; 512604a86453SAneesh Kumar K.V pte_t pte, old_pte; 51276688cc05SPeter Zijlstra int flags = 0; 5128d10e63f2SMel Gorman 5129d10e63f2SMel Gorman /* 5130d10e63f2SMel Gorman * The "pte" at this point cannot be used safely without 5131d10e63f2SMel Gorman * validation through pte_unmap_same(). It's of NUMA type but 5132d10e63f2SMel Gorman * the pfn may be screwed if the read is non atomic. 5133d10e63f2SMel Gorman */ 513482b0f8c3SJan Kara spin_lock(vmf->ptl); 5135c33c7948SRyan Roberts if (unlikely(!pte_same(ptep_get(vmf->pte), vmf->orig_pte))) { 513682b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 51374daae3b4SMel Gorman goto out; 51384daae3b4SMel Gorman } 51394daae3b4SMel Gorman 5140b99a342dSHuang Ying /* Get the normal PTE */ 5141b99a342dSHuang Ying old_pte = ptep_get(vmf->pte); 514204a86453SAneesh Kumar K.V pte = pte_modify(old_pte, vma->vm_page_prot); 5143d10e63f2SMel Gorman 51446a56ccbcSDavid Hildenbrand /* 51456a56ccbcSDavid Hildenbrand * Detect now whether the PTE could be writable; this information 51466a56ccbcSDavid Hildenbrand * is only valid while holding the PT lock. 51476a56ccbcSDavid Hildenbrand */ 51486a56ccbcSDavid Hildenbrand writable = pte_write(pte); 51496a56ccbcSDavid Hildenbrand if (!writable && vma_wants_manual_pte_write_upgrade(vma) && 51506a56ccbcSDavid Hildenbrand can_change_pte_writable(vma, vmf->address, pte)) 51516a56ccbcSDavid Hildenbrand writable = true; 51526a56ccbcSDavid Hildenbrand 51536695cf68SKefeng Wang folio = vm_normal_folio(vma, vmf->address, pte); 51546695cf68SKefeng Wang if (!folio || folio_is_zone_device(folio)) 5155b99a342dSHuang Ying goto out_map; 5156d10e63f2SMel Gorman 5157e81c4802SKirill A. Shutemov /* TODO: handle PTE-mapped THP */ 51586695cf68SKefeng Wang if (folio_test_large(folio)) 5159b99a342dSHuang Ying goto out_map; 5160e81c4802SKirill A. Shutemov 51616688cc05SPeter Zijlstra /* 5162bea66fbdSMel Gorman * Avoid grouping on RO pages in general. RO pages shouldn't hurt as 5163bea66fbdSMel Gorman * much anyway since they can be in shared cache state. This misses 5164bea66fbdSMel Gorman * the case where a mapping is writable but the process never writes 5165bea66fbdSMel Gorman * to it but pte_write gets cleared during protection updates and 5166bea66fbdSMel Gorman * pte_dirty has unpredictable behaviour between PTE scan updates, 5167bea66fbdSMel Gorman * background writeback, dirty balancing and application behaviour. 51686688cc05SPeter Zijlstra */ 51696a56ccbcSDavid Hildenbrand if (!writable) 51706688cc05SPeter Zijlstra flags |= TNF_NO_GROUP; 51716688cc05SPeter Zijlstra 5172dabe1d99SRik van Riel /* 51736695cf68SKefeng Wang * Flag if the folio is shared between multiple address spaces. This 5174dabe1d99SRik van Riel * is later used when determining whether to group tasks together 5175dabe1d99SRik van Riel */ 51766695cf68SKefeng Wang if (folio_estimated_sharers(folio) > 1 && (vma->vm_flags & VM_SHARED)) 5177dabe1d99SRik van Riel flags |= TNF_SHARED; 5178dabe1d99SRik van Riel 51796695cf68SKefeng Wang nid = folio_nid(folio); 518033024536SHuang Ying /* 518133024536SHuang Ying * For memory tiering mode, cpupid of slow memory page is used 518233024536SHuang Ying * to record page access time. So use default value. 518333024536SHuang Ying */ 518433024536SHuang Ying if ((sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING) && 51856695cf68SKefeng Wang !node_is_toptier(nid)) 518633024536SHuang Ying last_cpupid = (-1 & LAST_CPUPID_MASK); 518733024536SHuang Ying else 518867b33e3fSKefeng Wang last_cpupid = folio_last_cpupid(folio); 5189cda6d936SKefeng Wang target_nid = numa_migrate_prep(folio, vma, vmf->address, nid, &flags); 519098fa15f3SAnshuman Khandual if (target_nid == NUMA_NO_NODE) { 51916695cf68SKefeng Wang folio_put(folio); 5192b99a342dSHuang Ying goto out_map; 51934daae3b4SMel Gorman } 5194b99a342dSHuang Ying pte_unmap_unlock(vmf->pte, vmf->ptl); 51956a56ccbcSDavid Hildenbrand writable = false; 51964daae3b4SMel Gorman 51974daae3b4SMel Gorman /* Migrate to the requested node */ 51986695cf68SKefeng Wang if (migrate_misplaced_folio(folio, vma, target_nid)) { 51996695cf68SKefeng Wang nid = target_nid; 52006688cc05SPeter Zijlstra flags |= TNF_MIGRATED; 5201b99a342dSHuang Ying } else { 5202074c2381SMel Gorman flags |= TNF_MIGRATE_FAIL; 5203c7ad0880SHugh Dickins vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 5204c7ad0880SHugh Dickins vmf->address, &vmf->ptl); 5205c7ad0880SHugh Dickins if (unlikely(!vmf->pte)) 5206c7ad0880SHugh Dickins goto out; 5207c33c7948SRyan Roberts if (unlikely(!pte_same(ptep_get(vmf->pte), vmf->orig_pte))) { 5208b99a342dSHuang Ying pte_unmap_unlock(vmf->pte, vmf->ptl); 5209b99a342dSHuang Ying goto out; 5210b99a342dSHuang Ying } 5211b99a342dSHuang Ying goto out_map; 5212b99a342dSHuang Ying } 52134daae3b4SMel Gorman 52144daae3b4SMel Gorman out: 52156695cf68SKefeng Wang if (nid != NUMA_NO_NODE) 52166695cf68SKefeng Wang task_numa_fault(last_cpupid, nid, 1, flags); 5217d10e63f2SMel Gorman return 0; 5218b99a342dSHuang Ying out_map: 5219b99a342dSHuang Ying /* 5220b99a342dSHuang Ying * Make it present again, depending on how arch implements 5221b99a342dSHuang Ying * non-accessible ptes, some can allow access by kernel mode. 5222b99a342dSHuang Ying */ 5223b99a342dSHuang Ying old_pte = ptep_modify_prot_start(vma, vmf->address, vmf->pte); 5224b99a342dSHuang Ying pte = pte_modify(old_pte, vma->vm_page_prot); 5225b99a342dSHuang Ying pte = pte_mkyoung(pte); 52266a56ccbcSDavid Hildenbrand if (writable) 5227161e393cSRick Edgecombe pte = pte_mkwrite(pte, vma); 5228b99a342dSHuang Ying ptep_modify_prot_commit(vma, vmf->address, vmf->pte, old_pte, pte); 52295003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vma, vmf->address, vmf->pte, 1); 5230b99a342dSHuang Ying pte_unmap_unlock(vmf->pte, vmf->ptl); 5231b99a342dSHuang Ying goto out; 5232d10e63f2SMel Gorman } 5233d10e63f2SMel Gorman 52342b740303SSouptick Joarder static inline vm_fault_t create_huge_pmd(struct vm_fault *vmf) 5235b96375f7SMatthew Wilcox { 52368f5fd0e1SMatthew Wilcox (Oracle) struct vm_area_struct *vma = vmf->vma; 52378f5fd0e1SMatthew Wilcox (Oracle) if (vma_is_anonymous(vma)) 523882b0f8c3SJan Kara return do_huge_pmd_anonymous_page(vmf); 523940d49a3cSMatthew Wilcox (Oracle) if (vma->vm_ops->huge_fault) 52401d024e7aSMatthew Wilcox (Oracle) return vma->vm_ops->huge_fault(vmf, PMD_ORDER); 5241b96375f7SMatthew Wilcox return VM_FAULT_FALLBACK; 5242b96375f7SMatthew Wilcox } 5243b96375f7SMatthew Wilcox 5244183f24aaSGeert Uytterhoeven /* `inline' is required to avoid gcc 4.1.2 build error */ 52455db4f15cSYang Shi static inline vm_fault_t wp_huge_pmd(struct vm_fault *vmf) 5246b96375f7SMatthew Wilcox { 52478f5fd0e1SMatthew Wilcox (Oracle) struct vm_area_struct *vma = vmf->vma; 5248c89357e2SDavid Hildenbrand const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE; 5249aea06577SDavid Hildenbrand vm_fault_t ret; 5250c89357e2SDavid Hildenbrand 52518f5fd0e1SMatthew Wilcox (Oracle) if (vma_is_anonymous(vma)) { 5252c89357e2SDavid Hildenbrand if (likely(!unshare) && 5253d61ea1cbSPeter Xu userfaultfd_huge_pmd_wp(vma, vmf->orig_pmd)) { 5254d61ea1cbSPeter Xu if (userfaultfd_wp_async(vmf->vma)) 5255d61ea1cbSPeter Xu goto split; 5256529b930bSAndrea Arcangeli return handle_userfault(vmf, VM_UFFD_WP); 5257d61ea1cbSPeter Xu } 52585db4f15cSYang Shi return do_huge_pmd_wp_page(vmf); 5259529b930bSAndrea Arcangeli } 5260af9e4d5fSKirill A. Shutemov 52618f5fd0e1SMatthew Wilcox (Oracle) if (vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) { 52628f5fd0e1SMatthew Wilcox (Oracle) if (vma->vm_ops->huge_fault) { 52631d024e7aSMatthew Wilcox (Oracle) ret = vma->vm_ops->huge_fault(vmf, PMD_ORDER); 5264327e9fd4SThomas Hellstrom (VMware) if (!(ret & VM_FAULT_FALLBACK)) 5265327e9fd4SThomas Hellstrom (VMware) return ret; 5266327e9fd4SThomas Hellstrom (VMware) } 5267aea06577SDavid Hildenbrand } 5268327e9fd4SThomas Hellstrom (VMware) 5269d61ea1cbSPeter Xu split: 5270327e9fd4SThomas Hellstrom (VMware) /* COW or write-notify handled on pte level: split pmd. */ 52718f5fd0e1SMatthew Wilcox (Oracle) __split_huge_pmd(vma, vmf->pmd, vmf->address, false, NULL); 5272af9e4d5fSKirill A. Shutemov 5273b96375f7SMatthew Wilcox return VM_FAULT_FALLBACK; 5274b96375f7SMatthew Wilcox } 5275b96375f7SMatthew Wilcox 52762b740303SSouptick Joarder static vm_fault_t create_huge_pud(struct vm_fault *vmf) 5277a00cc7d9SMatthew Wilcox { 5278327e9fd4SThomas Hellstrom (VMware) #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && \ 5279327e9fd4SThomas Hellstrom (VMware) defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) 5280c4fd825eSMatthew Wilcox (Oracle) struct vm_area_struct *vma = vmf->vma; 5281a00cc7d9SMatthew Wilcox /* No support for anonymous transparent PUD pages yet */ 5282c4fd825eSMatthew Wilcox (Oracle) if (vma_is_anonymous(vma)) 528314c99d65SGowans, James return VM_FAULT_FALLBACK; 528440d49a3cSMatthew Wilcox (Oracle) if (vma->vm_ops->huge_fault) 52851d024e7aSMatthew Wilcox (Oracle) return vma->vm_ops->huge_fault(vmf, PUD_ORDER); 528614c99d65SGowans, James #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 528714c99d65SGowans, James return VM_FAULT_FALLBACK; 528814c99d65SGowans, James } 528914c99d65SGowans, James 529014c99d65SGowans, James static vm_fault_t wp_huge_pud(struct vm_fault *vmf, pud_t orig_pud) 529114c99d65SGowans, James { 529214c99d65SGowans, James #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && \ 529314c99d65SGowans, James defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) 5294c4fd825eSMatthew Wilcox (Oracle) struct vm_area_struct *vma = vmf->vma; 5295aea06577SDavid Hildenbrand vm_fault_t ret; 5296aea06577SDavid Hildenbrand 529714c99d65SGowans, James /* No support for anonymous transparent PUD pages yet */ 5298c4fd825eSMatthew Wilcox (Oracle) if (vma_is_anonymous(vma)) 5299327e9fd4SThomas Hellstrom (VMware) goto split; 5300c4fd825eSMatthew Wilcox (Oracle) if (vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) { 5301c4fd825eSMatthew Wilcox (Oracle) if (vma->vm_ops->huge_fault) { 53021d024e7aSMatthew Wilcox (Oracle) ret = vma->vm_ops->huge_fault(vmf, PUD_ORDER); 5303327e9fd4SThomas Hellstrom (VMware) if (!(ret & VM_FAULT_FALLBACK)) 5304327e9fd4SThomas Hellstrom (VMware) return ret; 5305327e9fd4SThomas Hellstrom (VMware) } 5306aea06577SDavid Hildenbrand } 5307327e9fd4SThomas Hellstrom (VMware) split: 5308327e9fd4SThomas Hellstrom (VMware) /* COW or write-notify not handled on PUD level: split pud.*/ 5309c4fd825eSMatthew Wilcox (Oracle) __split_huge_pud(vma, vmf->pud, vmf->address); 531014c99d65SGowans, James #endif /* CONFIG_TRANSPARENT_HUGEPAGE && CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 5311a00cc7d9SMatthew Wilcox return VM_FAULT_FALLBACK; 5312a00cc7d9SMatthew Wilcox } 5313a00cc7d9SMatthew Wilcox 53141da177e4SLinus Torvalds /* 53151da177e4SLinus Torvalds * These routines also need to handle stuff like marking pages dirty 53161da177e4SLinus Torvalds * and/or accessed for architectures that don't do it in hardware (most 53171da177e4SLinus Torvalds * RISC architectures). The early dirtying is also good on the i386. 53181da177e4SLinus Torvalds * 53191da177e4SLinus Torvalds * There is also a hook called "update_mmu_cache()" that architectures 53201da177e4SLinus Torvalds * with external mmu caches can use to update those (ie the Sparc or 53211da177e4SLinus Torvalds * PowerPC hashed page tables that act as extended TLBs). 53221da177e4SLinus Torvalds * 5323c1e8d7c6SMichel Lespinasse * We enter with non-exclusive mmap_lock (to exclude vma changes, but allow 53247267ec00SKirill A. Shutemov * concurrent faults). 53259a95f3cfSPaul Cassella * 5326c1e8d7c6SMichel Lespinasse * The mmap_lock may have been released depending on flags and our return value. 53279138e47eSMatthew Wilcox (Oracle) * See filemap_fault() and __folio_lock_or_retry(). 53281da177e4SLinus Torvalds */ 53292b740303SSouptick Joarder static vm_fault_t handle_pte_fault(struct vm_fault *vmf) 53301da177e4SLinus Torvalds { 53311da177e4SLinus Torvalds pte_t entry; 53321da177e4SLinus Torvalds 533382b0f8c3SJan Kara if (unlikely(pmd_none(*vmf->pmd))) { 53347267ec00SKirill A. Shutemov /* 53357267ec00SKirill A. Shutemov * Leave __pte_alloc() until later: because vm_ops->fault may 53367267ec00SKirill A. Shutemov * want to allocate huge page, and if we expose page table 53377267ec00SKirill A. Shutemov * for an instant, it will be difficult to retract from 53387267ec00SKirill A. Shutemov * concurrent faults and from rmap lookups. 53397267ec00SKirill A. Shutemov */ 534082b0f8c3SJan Kara vmf->pte = NULL; 5341f46f2adeSPeter Xu vmf->flags &= ~FAULT_FLAG_ORIG_PTE_VALID; 53427267ec00SKirill A. Shutemov } else { 5343f9ce0be7SKirill A. Shutemov /* 53447267ec00SKirill A. Shutemov * A regular pmd is established and it can't morph into a huge 5345c7ad0880SHugh Dickins * pmd by anon khugepaged, since that takes mmap_lock in write 5346c7ad0880SHugh Dickins * mode; but shmem or file collapse to THP could still morph 5347c7ad0880SHugh Dickins * it into a huge pmd: just retry later if so. 53487267ec00SKirill A. Shutemov */ 5349c7ad0880SHugh Dickins vmf->pte = pte_offset_map_nolock(vmf->vma->vm_mm, vmf->pmd, 5350c7ad0880SHugh Dickins vmf->address, &vmf->ptl); 5351c7ad0880SHugh Dickins if (unlikely(!vmf->pte)) 5352c7ad0880SHugh Dickins return 0; 535326e1a0c3SHugh Dickins vmf->orig_pte = ptep_get_lockless(vmf->pte); 5354f46f2adeSPeter Xu vmf->flags |= FAULT_FLAG_ORIG_PTE_VALID; 53557267ec00SKirill A. Shutemov 53562994302bSJan Kara if (pte_none(vmf->orig_pte)) { 535782b0f8c3SJan Kara pte_unmap(vmf->pte); 535882b0f8c3SJan Kara vmf->pte = NULL; 53597267ec00SKirill A. Shutemov } 53607267ec00SKirill A. Shutemov } 53617267ec00SKirill A. Shutemov 53622bad466cSPeter Xu if (!vmf->pte) 53632bad466cSPeter Xu return do_pte_missing(vmf); 53647267ec00SKirill A. Shutemov 53652994302bSJan Kara if (!pte_present(vmf->orig_pte)) 53662994302bSJan Kara return do_swap_page(vmf); 53671da177e4SLinus Torvalds 53682994302bSJan Kara if (pte_protnone(vmf->orig_pte) && vma_is_accessible(vmf->vma)) 53692994302bSJan Kara return do_numa_page(vmf); 5370d10e63f2SMel Gorman 537182b0f8c3SJan Kara spin_lock(vmf->ptl); 53722994302bSJan Kara entry = vmf->orig_pte; 5373c33c7948SRyan Roberts if (unlikely(!pte_same(ptep_get(vmf->pte), entry))) { 53747df67697SBibo Mao update_mmu_tlb(vmf->vma, vmf->address, vmf->pte); 53758f4e2101SHugh Dickins goto unlock; 53767df67697SBibo Mao } 5377c89357e2SDavid Hildenbrand if (vmf->flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) { 5378f6f37321SLinus Torvalds if (!pte_write(entry)) 53792994302bSJan Kara return do_wp_page(vmf); 5380c89357e2SDavid Hildenbrand else if (likely(vmf->flags & FAULT_FLAG_WRITE)) 53811da177e4SLinus Torvalds entry = pte_mkdirty(entry); 53821da177e4SLinus Torvalds } 53831da177e4SLinus Torvalds entry = pte_mkyoung(entry); 538482b0f8c3SJan Kara if (ptep_set_access_flags(vmf->vma, vmf->address, vmf->pte, entry, 538582b0f8c3SJan Kara vmf->flags & FAULT_FLAG_WRITE)) { 53865003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vmf->vma, vmf->address, 53875003a2bdSMatthew Wilcox (Oracle) vmf->pte, 1); 53881a44e149SAndrea Arcangeli } else { 5389b7333b58SYang Shi /* Skip spurious TLB flush for retried page fault */ 5390b7333b58SYang Shi if (vmf->flags & FAULT_FLAG_TRIED) 5391b7333b58SYang Shi goto unlock; 53921a44e149SAndrea Arcangeli /* 53931a44e149SAndrea Arcangeli * This is needed only for protection faults but the arch code 53941a44e149SAndrea Arcangeli * is not yet telling us if this is a protection fault or not. 53951a44e149SAndrea Arcangeli * This still avoids useless tlb flushes for .text page faults 53961a44e149SAndrea Arcangeli * with threads. 53971a44e149SAndrea Arcangeli */ 539882b0f8c3SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) 539999c29133SGerald Schaefer flush_tlb_fix_spurious_fault(vmf->vma, vmf->address, 540099c29133SGerald Schaefer vmf->pte); 54011a44e149SAndrea Arcangeli } 54028f4e2101SHugh Dickins unlock: 540382b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 540483c54070SNick Piggin return 0; 54051da177e4SLinus Torvalds } 54061da177e4SLinus Torvalds 54071da177e4SLinus Torvalds /* 54084ec31152SMatthew Wilcox (Oracle) * On entry, we hold either the VMA lock or the mmap_lock 54094ec31152SMatthew Wilcox (Oracle) * (FAULT_FLAG_VMA_LOCK tells you which). If VM_FAULT_RETRY is set in 54104ec31152SMatthew Wilcox (Oracle) * the result, the mmap_lock is not held on exit. See filemap_fault() 54114ec31152SMatthew Wilcox (Oracle) * and __folio_lock_or_retry(). 54121da177e4SLinus Torvalds */ 54132b740303SSouptick Joarder static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma, 54142b740303SSouptick Joarder unsigned long address, unsigned int flags) 54151da177e4SLinus Torvalds { 541682b0f8c3SJan Kara struct vm_fault vmf = { 5417bae473a4SKirill A. Shutemov .vma = vma, 54181a29d85eSJan Kara .address = address & PAGE_MASK, 5419824ddc60SNadav Amit .real_address = address, 5420bae473a4SKirill A. Shutemov .flags = flags, 54210721ec8bSJan Kara .pgoff = linear_page_index(vma, address), 5422667240e0SJan Kara .gfp_mask = __get_fault_gfp_mask(vma), 5423bae473a4SKirill A. Shutemov }; 5424dcddffd4SKirill A. Shutemov struct mm_struct *mm = vma->vm_mm; 54257da4e2cbSYang Shi unsigned long vm_flags = vma->vm_flags; 54261da177e4SLinus Torvalds pgd_t *pgd; 5427c2febafcSKirill A. Shutemov p4d_t *p4d; 54282b740303SSouptick Joarder vm_fault_t ret; 54291da177e4SLinus Torvalds 54301da177e4SLinus Torvalds pgd = pgd_offset(mm, address); 5431c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, address); 5432c2febafcSKirill A. Shutemov if (!p4d) 5433c2febafcSKirill A. Shutemov return VM_FAULT_OOM; 5434a00cc7d9SMatthew Wilcox 5435c2febafcSKirill A. Shutemov vmf.pud = pud_alloc(mm, p4d, address); 5436a00cc7d9SMatthew Wilcox if (!vmf.pud) 5437c74df32cSHugh Dickins return VM_FAULT_OOM; 5438625110b5SThomas Hellstrom retry_pud: 54397da4e2cbSYang Shi if (pud_none(*vmf.pud) && 54403485b883SRyan Roberts thp_vma_allowable_order(vma, vm_flags, false, true, true, PUD_ORDER)) { 5441a00cc7d9SMatthew Wilcox ret = create_huge_pud(&vmf); 5442a00cc7d9SMatthew Wilcox if (!(ret & VM_FAULT_FALLBACK)) 5443a00cc7d9SMatthew Wilcox return ret; 5444a00cc7d9SMatthew Wilcox } else { 5445a00cc7d9SMatthew Wilcox pud_t orig_pud = *vmf.pud; 5446a00cc7d9SMatthew Wilcox 5447a00cc7d9SMatthew Wilcox barrier(); 5448a00cc7d9SMatthew Wilcox if (pud_trans_huge(orig_pud) || pud_devmap(orig_pud)) { 5449a00cc7d9SMatthew Wilcox 5450c89357e2SDavid Hildenbrand /* 5451c89357e2SDavid Hildenbrand * TODO once we support anonymous PUDs: NUMA case and 5452c89357e2SDavid Hildenbrand * FAULT_FLAG_UNSHARE handling. 5453c89357e2SDavid Hildenbrand */ 5454c89357e2SDavid Hildenbrand if ((flags & FAULT_FLAG_WRITE) && !pud_write(orig_pud)) { 5455a00cc7d9SMatthew Wilcox ret = wp_huge_pud(&vmf, orig_pud); 5456a00cc7d9SMatthew Wilcox if (!(ret & VM_FAULT_FALLBACK)) 5457a00cc7d9SMatthew Wilcox return ret; 5458a00cc7d9SMatthew Wilcox } else { 5459a00cc7d9SMatthew Wilcox huge_pud_set_accessed(&vmf, orig_pud); 5460a00cc7d9SMatthew Wilcox return 0; 5461a00cc7d9SMatthew Wilcox } 5462a00cc7d9SMatthew Wilcox } 5463a00cc7d9SMatthew Wilcox } 5464a00cc7d9SMatthew Wilcox 5465a00cc7d9SMatthew Wilcox vmf.pmd = pmd_alloc(mm, vmf.pud, address); 546682b0f8c3SJan Kara if (!vmf.pmd) 5467c74df32cSHugh Dickins return VM_FAULT_OOM; 5468625110b5SThomas Hellstrom 5469625110b5SThomas Hellstrom /* Huge pud page fault raced with pmd_alloc? */ 5470625110b5SThomas Hellstrom if (pud_trans_unstable(vmf.pud)) 5471625110b5SThomas Hellstrom goto retry_pud; 5472625110b5SThomas Hellstrom 54737da4e2cbSYang Shi if (pmd_none(*vmf.pmd) && 54743485b883SRyan Roberts thp_vma_allowable_order(vma, vm_flags, false, true, true, PMD_ORDER)) { 5475a2d58167SDave Jiang ret = create_huge_pmd(&vmf); 5476c0292554SKirill A. Shutemov if (!(ret & VM_FAULT_FALLBACK)) 5477c0292554SKirill A. Shutemov return ret; 547871e3aac0SAndrea Arcangeli } else { 547926e1a0c3SHugh Dickins vmf.orig_pmd = pmdp_get_lockless(vmf.pmd); 54801f1d06c3SDavid Rientjes 54815db4f15cSYang Shi if (unlikely(is_swap_pmd(vmf.orig_pmd))) { 548284c3fc4eSZi Yan VM_BUG_ON(thp_migration_supported() && 54835db4f15cSYang Shi !is_pmd_migration_entry(vmf.orig_pmd)); 54845db4f15cSYang Shi if (is_pmd_migration_entry(vmf.orig_pmd)) 548584c3fc4eSZi Yan pmd_migration_entry_wait(mm, vmf.pmd); 548684c3fc4eSZi Yan return 0; 548784c3fc4eSZi Yan } 54885db4f15cSYang Shi if (pmd_trans_huge(vmf.orig_pmd) || pmd_devmap(vmf.orig_pmd)) { 54895db4f15cSYang Shi if (pmd_protnone(vmf.orig_pmd) && vma_is_accessible(vma)) 54905db4f15cSYang Shi return do_huge_pmd_numa_page(&vmf); 5491d10e63f2SMel Gorman 5492c89357e2SDavid Hildenbrand if ((flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) && 5493c89357e2SDavid Hildenbrand !pmd_write(vmf.orig_pmd)) { 54945db4f15cSYang Shi ret = wp_huge_pmd(&vmf); 54959845cbbdSKirill A. Shutemov if (!(ret & VM_FAULT_FALLBACK)) 54961f1d06c3SDavid Rientjes return ret; 5497a1dd450bSWill Deacon } else { 54985db4f15cSYang Shi huge_pmd_set_accessed(&vmf); 549971e3aac0SAndrea Arcangeli return 0; 550071e3aac0SAndrea Arcangeli } 550171e3aac0SAndrea Arcangeli } 55029845cbbdSKirill A. Shutemov } 550371e3aac0SAndrea Arcangeli 550482b0f8c3SJan Kara return handle_pte_fault(&vmf); 55051da177e4SLinus Torvalds } 55061da177e4SLinus Torvalds 5507bce617edSPeter Xu /** 5508f0953a1bSIngo Molnar * mm_account_fault - Do page fault accounting 5509809ef83cSYang Li * @mm: mm from which memcg should be extracted. It can be NULL. 5510bce617edSPeter Xu * @regs: the pt_regs struct pointer. When set to NULL, will skip accounting 5511bce617edSPeter Xu * of perf event counters, but we'll still do the per-task accounting to 5512bce617edSPeter Xu * the task who triggered this page fault. 5513bce617edSPeter Xu * @address: the faulted address. 5514bce617edSPeter Xu * @flags: the fault flags. 5515bce617edSPeter Xu * @ret: the fault retcode. 5516bce617edSPeter Xu * 5517f0953a1bSIngo Molnar * This will take care of most of the page fault accounting. Meanwhile, it 5518bce617edSPeter Xu * will also include the PERF_COUNT_SW_PAGE_FAULTS_[MAJ|MIN] perf counter 5519f0953a1bSIngo Molnar * updates. However, note that the handling of PERF_COUNT_SW_PAGE_FAULTS should 5520bce617edSPeter Xu * still be in per-arch page fault handlers at the entry of page fault. 5521bce617edSPeter Xu */ 552253156443SSuren Baghdasaryan static inline void mm_account_fault(struct mm_struct *mm, struct pt_regs *regs, 5523bce617edSPeter Xu unsigned long address, unsigned int flags, 5524bce617edSPeter Xu vm_fault_t ret) 5525bce617edSPeter Xu { 5526bce617edSPeter Xu bool major; 5527bce617edSPeter Xu 552853156443SSuren Baghdasaryan /* Incomplete faults will be accounted upon completion. */ 552953156443SSuren Baghdasaryan if (ret & VM_FAULT_RETRY) 553053156443SSuren Baghdasaryan return; 553153156443SSuren Baghdasaryan 5532bce617edSPeter Xu /* 553353156443SSuren Baghdasaryan * To preserve the behavior of older kernels, PGFAULT counters record 553453156443SSuren Baghdasaryan * both successful and failed faults, as opposed to perf counters, 553553156443SSuren Baghdasaryan * which ignore failed cases. 5536bce617edSPeter Xu */ 553753156443SSuren Baghdasaryan count_vm_event(PGFAULT); 553853156443SSuren Baghdasaryan count_memcg_event_mm(mm, PGFAULT); 553953156443SSuren Baghdasaryan 554053156443SSuren Baghdasaryan /* 554153156443SSuren Baghdasaryan * Do not account for unsuccessful faults (e.g. when the address wasn't 554253156443SSuren Baghdasaryan * valid). That includes arch_vma_access_permitted() failing before 554353156443SSuren Baghdasaryan * reaching here. So this is not a "this many hardware page faults" 554453156443SSuren Baghdasaryan * counter. We should use the hw profiling for that. 554553156443SSuren Baghdasaryan */ 554653156443SSuren Baghdasaryan if (ret & VM_FAULT_ERROR) 5547bce617edSPeter Xu return; 5548bce617edSPeter Xu 5549bce617edSPeter Xu /* 5550bce617edSPeter Xu * We define the fault as a major fault when the final successful fault 5551bce617edSPeter Xu * is VM_FAULT_MAJOR, or if it retried (which implies that we couldn't 5552bce617edSPeter Xu * handle it immediately previously). 5553bce617edSPeter Xu */ 5554bce617edSPeter Xu major = (ret & VM_FAULT_MAJOR) || (flags & FAULT_FLAG_TRIED); 5555bce617edSPeter Xu 5556a2beb5f1SPeter Xu if (major) 5557a2beb5f1SPeter Xu current->maj_flt++; 5558a2beb5f1SPeter Xu else 5559a2beb5f1SPeter Xu current->min_flt++; 5560a2beb5f1SPeter Xu 5561bce617edSPeter Xu /* 5562a2beb5f1SPeter Xu * If the fault is done for GUP, regs will be NULL. We only do the 5563a2beb5f1SPeter Xu * accounting for the per thread fault counters who triggered the 5564a2beb5f1SPeter Xu * fault, and we skip the perf event updates. 5565bce617edSPeter Xu */ 5566bce617edSPeter Xu if (!regs) 5567bce617edSPeter Xu return; 5568bce617edSPeter Xu 5569a2beb5f1SPeter Xu if (major) 5570bce617edSPeter Xu perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address); 5571a2beb5f1SPeter Xu else 5572bce617edSPeter Xu perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address); 5573bce617edSPeter Xu } 5574bce617edSPeter Xu 5575ec1c86b2SYu Zhao #ifdef CONFIG_LRU_GEN 5576ec1c86b2SYu Zhao static void lru_gen_enter_fault(struct vm_area_struct *vma) 5577ec1c86b2SYu Zhao { 55788788f678SYu Zhao /* the LRU algorithm only applies to accesses with recency */ 55798788f678SYu Zhao current->in_lru_fault = vma_has_recency(vma); 5580ec1c86b2SYu Zhao } 5581ec1c86b2SYu Zhao 5582ec1c86b2SYu Zhao static void lru_gen_exit_fault(void) 5583ec1c86b2SYu Zhao { 5584ec1c86b2SYu Zhao current->in_lru_fault = false; 5585ec1c86b2SYu Zhao } 5586ec1c86b2SYu Zhao #else 5587ec1c86b2SYu Zhao static void lru_gen_enter_fault(struct vm_area_struct *vma) 5588ec1c86b2SYu Zhao { 5589ec1c86b2SYu Zhao } 5590ec1c86b2SYu Zhao 5591ec1c86b2SYu Zhao static void lru_gen_exit_fault(void) 5592ec1c86b2SYu Zhao { 5593ec1c86b2SYu Zhao } 5594ec1c86b2SYu Zhao #endif /* CONFIG_LRU_GEN */ 5595ec1c86b2SYu Zhao 5596cdc5021cSDavid Hildenbrand static vm_fault_t sanitize_fault_flags(struct vm_area_struct *vma, 5597cdc5021cSDavid Hildenbrand unsigned int *flags) 5598cdc5021cSDavid Hildenbrand { 5599cdc5021cSDavid Hildenbrand if (unlikely(*flags & FAULT_FLAG_UNSHARE)) { 5600cdc5021cSDavid Hildenbrand if (WARN_ON_ONCE(*flags & FAULT_FLAG_WRITE)) 5601cdc5021cSDavid Hildenbrand return VM_FAULT_SIGSEGV; 5602cdc5021cSDavid Hildenbrand /* 5603cdc5021cSDavid Hildenbrand * FAULT_FLAG_UNSHARE only applies to COW mappings. Let's 5604cdc5021cSDavid Hildenbrand * just treat it like an ordinary read-fault otherwise. 5605cdc5021cSDavid Hildenbrand */ 5606cdc5021cSDavid Hildenbrand if (!is_cow_mapping(vma->vm_flags)) 5607cdc5021cSDavid Hildenbrand *flags &= ~FAULT_FLAG_UNSHARE; 560879881fedSDavid Hildenbrand } else if (*flags & FAULT_FLAG_WRITE) { 560979881fedSDavid Hildenbrand /* Write faults on read-only mappings are impossible ... */ 561079881fedSDavid Hildenbrand if (WARN_ON_ONCE(!(vma->vm_flags & VM_MAYWRITE))) 561179881fedSDavid Hildenbrand return VM_FAULT_SIGSEGV; 561279881fedSDavid Hildenbrand /* ... and FOLL_FORCE only applies to COW mappings. */ 561379881fedSDavid Hildenbrand if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE) && 561479881fedSDavid Hildenbrand !is_cow_mapping(vma->vm_flags))) 561579881fedSDavid Hildenbrand return VM_FAULT_SIGSEGV; 5616cdc5021cSDavid Hildenbrand } 56174089eef0SSuren Baghdasaryan #ifdef CONFIG_PER_VMA_LOCK 56184089eef0SSuren Baghdasaryan /* 56194089eef0SSuren Baghdasaryan * Per-VMA locks can't be used with FAULT_FLAG_RETRY_NOWAIT because of 56204089eef0SSuren Baghdasaryan * the assumption that lock is dropped on VM_FAULT_RETRY. 56214089eef0SSuren Baghdasaryan */ 56224089eef0SSuren Baghdasaryan if (WARN_ON_ONCE((*flags & 56234089eef0SSuren Baghdasaryan (FAULT_FLAG_VMA_LOCK | FAULT_FLAG_RETRY_NOWAIT)) == 56244089eef0SSuren Baghdasaryan (FAULT_FLAG_VMA_LOCK | FAULT_FLAG_RETRY_NOWAIT))) 56254089eef0SSuren Baghdasaryan return VM_FAULT_SIGSEGV; 56264089eef0SSuren Baghdasaryan #endif 56274089eef0SSuren Baghdasaryan 5628cdc5021cSDavid Hildenbrand return 0; 5629cdc5021cSDavid Hildenbrand } 5630cdc5021cSDavid Hildenbrand 56319a95f3cfSPaul Cassella /* 56329a95f3cfSPaul Cassella * By the time we get here, we already hold the mm semaphore 56339a95f3cfSPaul Cassella * 5634c1e8d7c6SMichel Lespinasse * The mmap_lock may have been released depending on flags and our 56359138e47eSMatthew Wilcox (Oracle) * return value. See filemap_fault() and __folio_lock_or_retry(). 56369a95f3cfSPaul Cassella */ 56372b740303SSouptick Joarder vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address, 5638bce617edSPeter Xu unsigned int flags, struct pt_regs *regs) 5639519e5247SJohannes Weiner { 564053156443SSuren Baghdasaryan /* If the fault handler drops the mmap_lock, vma may be freed */ 564153156443SSuren Baghdasaryan struct mm_struct *mm = vma->vm_mm; 56422b740303SSouptick Joarder vm_fault_t ret; 5643519e5247SJohannes Weiner 5644519e5247SJohannes Weiner __set_current_state(TASK_RUNNING); 5645519e5247SJohannes Weiner 5646cdc5021cSDavid Hildenbrand ret = sanitize_fault_flags(vma, &flags); 5647cdc5021cSDavid Hildenbrand if (ret) 564853156443SSuren Baghdasaryan goto out; 5649cdc5021cSDavid Hildenbrand 5650de0c799bSLaurent Dufour if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE, 5651de0c799bSLaurent Dufour flags & FAULT_FLAG_INSTRUCTION, 565253156443SSuren Baghdasaryan flags & FAULT_FLAG_REMOTE)) { 565353156443SSuren Baghdasaryan ret = VM_FAULT_SIGSEGV; 565453156443SSuren Baghdasaryan goto out; 565553156443SSuren Baghdasaryan } 5656de0c799bSLaurent Dufour 5657519e5247SJohannes Weiner /* 5658519e5247SJohannes Weiner * Enable the memcg OOM handling for faults triggered in user 5659519e5247SJohannes Weiner * space. Kernel faults are handled more gracefully. 5660519e5247SJohannes Weiner */ 5661519e5247SJohannes Weiner if (flags & FAULT_FLAG_USER) 566229ef680aSMichal Hocko mem_cgroup_enter_user_fault(); 5663519e5247SJohannes Weiner 5664ec1c86b2SYu Zhao lru_gen_enter_fault(vma); 5665ec1c86b2SYu Zhao 5666bae473a4SKirill A. Shutemov if (unlikely(is_vm_hugetlb_page(vma))) 5667bae473a4SKirill A. Shutemov ret = hugetlb_fault(vma->vm_mm, vma, address, flags); 5668bae473a4SKirill A. Shutemov else 5669dcddffd4SKirill A. Shutemov ret = __handle_mm_fault(vma, address, flags); 5670519e5247SJohannes Weiner 5671ec1c86b2SYu Zhao lru_gen_exit_fault(); 5672ec1c86b2SYu Zhao 567349426420SJohannes Weiner if (flags & FAULT_FLAG_USER) { 567429ef680aSMichal Hocko mem_cgroup_exit_user_fault(); 567549426420SJohannes Weiner /* 567649426420SJohannes Weiner * The task may have entered a memcg OOM situation but 567749426420SJohannes Weiner * if the allocation error was handled gracefully (no 567849426420SJohannes Weiner * VM_FAULT_OOM), there is no need to kill anything. 567949426420SJohannes Weiner * Just clean up the OOM state peacefully. 568049426420SJohannes Weiner */ 568149426420SJohannes Weiner if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM)) 568249426420SJohannes Weiner mem_cgroup_oom_synchronize(false); 568349426420SJohannes Weiner } 568453156443SSuren Baghdasaryan out: 568553156443SSuren Baghdasaryan mm_account_fault(mm, regs, address, flags, ret); 5686bce617edSPeter Xu 5687519e5247SJohannes Weiner return ret; 5688519e5247SJohannes Weiner } 5689e1d6d01aSJesse Barnes EXPORT_SYMBOL_GPL(handle_mm_fault); 5690519e5247SJohannes Weiner 5691c2508ec5SLinus Torvalds #ifdef CONFIG_LOCK_MM_AND_FIND_VMA 5692c2508ec5SLinus Torvalds #include <linux/extable.h> 5693c2508ec5SLinus Torvalds 5694c2508ec5SLinus Torvalds static inline bool get_mmap_lock_carefully(struct mm_struct *mm, struct pt_regs *regs) 5695c2508ec5SLinus Torvalds { 56964542057eSLinus Torvalds if (likely(mmap_read_trylock(mm))) 5697c2508ec5SLinus Torvalds return true; 5698c2508ec5SLinus Torvalds 5699c2508ec5SLinus Torvalds if (regs && !user_mode(regs)) { 57008fa50708SJiaxun Yang unsigned long ip = exception_ip(regs); 5701c2508ec5SLinus Torvalds if (!search_exception_tables(ip)) 5702c2508ec5SLinus Torvalds return false; 5703c2508ec5SLinus Torvalds } 5704c2508ec5SLinus Torvalds 5705eda00472SLinus Torvalds return !mmap_read_lock_killable(mm); 5706c2508ec5SLinus Torvalds } 5707c2508ec5SLinus Torvalds 5708c2508ec5SLinus Torvalds static inline bool mmap_upgrade_trylock(struct mm_struct *mm) 5709c2508ec5SLinus Torvalds { 5710c2508ec5SLinus Torvalds /* 5711c2508ec5SLinus Torvalds * We don't have this operation yet. 5712c2508ec5SLinus Torvalds * 5713c2508ec5SLinus Torvalds * It should be easy enough to do: it's basically a 5714c2508ec5SLinus Torvalds * atomic_long_try_cmpxchg_acquire() 5715c2508ec5SLinus Torvalds * from RWSEM_READER_BIAS -> RWSEM_WRITER_LOCKED, but 5716c2508ec5SLinus Torvalds * it also needs the proper lockdep magic etc. 5717c2508ec5SLinus Torvalds */ 5718c2508ec5SLinus Torvalds return false; 5719c2508ec5SLinus Torvalds } 5720c2508ec5SLinus Torvalds 5721c2508ec5SLinus Torvalds static inline bool upgrade_mmap_lock_carefully(struct mm_struct *mm, struct pt_regs *regs) 5722c2508ec5SLinus Torvalds { 5723c2508ec5SLinus Torvalds mmap_read_unlock(mm); 5724c2508ec5SLinus Torvalds if (regs && !user_mode(regs)) { 57258fa50708SJiaxun Yang unsigned long ip = exception_ip(regs); 5726c2508ec5SLinus Torvalds if (!search_exception_tables(ip)) 5727c2508ec5SLinus Torvalds return false; 5728c2508ec5SLinus Torvalds } 5729eda00472SLinus Torvalds return !mmap_write_lock_killable(mm); 5730c2508ec5SLinus Torvalds } 5731c2508ec5SLinus Torvalds 5732c2508ec5SLinus Torvalds /* 5733c2508ec5SLinus Torvalds * Helper for page fault handling. 5734c2508ec5SLinus Torvalds * 5735c2508ec5SLinus Torvalds * This is kind of equivalend to "mmap_read_lock()" followed 5736c2508ec5SLinus Torvalds * by "find_extend_vma()", except it's a lot more careful about 5737c2508ec5SLinus Torvalds * the locking (and will drop the lock on failure). 5738c2508ec5SLinus Torvalds * 5739c2508ec5SLinus Torvalds * For example, if we have a kernel bug that causes a page 5740c2508ec5SLinus Torvalds * fault, we don't want to just use mmap_read_lock() to get 5741c2508ec5SLinus Torvalds * the mm lock, because that would deadlock if the bug were 5742c2508ec5SLinus Torvalds * to happen while we're holding the mm lock for writing. 5743c2508ec5SLinus Torvalds * 5744c2508ec5SLinus Torvalds * So this checks the exception tables on kernel faults in 5745c2508ec5SLinus Torvalds * order to only do this all for instructions that are actually 5746c2508ec5SLinus Torvalds * expected to fault. 5747c2508ec5SLinus Torvalds * 5748c2508ec5SLinus Torvalds * We can also actually take the mm lock for writing if we 5749c2508ec5SLinus Torvalds * need to extend the vma, which helps the VM layer a lot. 5750c2508ec5SLinus Torvalds */ 5751c2508ec5SLinus Torvalds struct vm_area_struct *lock_mm_and_find_vma(struct mm_struct *mm, 5752c2508ec5SLinus Torvalds unsigned long addr, struct pt_regs *regs) 5753c2508ec5SLinus Torvalds { 5754c2508ec5SLinus Torvalds struct vm_area_struct *vma; 5755c2508ec5SLinus Torvalds 5756c2508ec5SLinus Torvalds if (!get_mmap_lock_carefully(mm, regs)) 5757c2508ec5SLinus Torvalds return NULL; 5758c2508ec5SLinus Torvalds 5759c2508ec5SLinus Torvalds vma = find_vma(mm, addr); 5760c2508ec5SLinus Torvalds if (likely(vma && (vma->vm_start <= addr))) 5761c2508ec5SLinus Torvalds return vma; 5762c2508ec5SLinus Torvalds 5763c2508ec5SLinus Torvalds /* 5764c2508ec5SLinus Torvalds * Well, dang. We might still be successful, but only 5765c2508ec5SLinus Torvalds * if we can extend a vma to do so. 5766c2508ec5SLinus Torvalds */ 5767c2508ec5SLinus Torvalds if (!vma || !(vma->vm_flags & VM_GROWSDOWN)) { 5768c2508ec5SLinus Torvalds mmap_read_unlock(mm); 5769c2508ec5SLinus Torvalds return NULL; 5770c2508ec5SLinus Torvalds } 5771c2508ec5SLinus Torvalds 5772c2508ec5SLinus Torvalds /* 5773c2508ec5SLinus Torvalds * We can try to upgrade the mmap lock atomically, 5774c2508ec5SLinus Torvalds * in which case we can continue to use the vma 5775c2508ec5SLinus Torvalds * we already looked up. 5776c2508ec5SLinus Torvalds * 5777c2508ec5SLinus Torvalds * Otherwise we'll have to drop the mmap lock and 5778c2508ec5SLinus Torvalds * re-take it, and also look up the vma again, 5779c2508ec5SLinus Torvalds * re-checking it. 5780c2508ec5SLinus Torvalds */ 5781c2508ec5SLinus Torvalds if (!mmap_upgrade_trylock(mm)) { 5782c2508ec5SLinus Torvalds if (!upgrade_mmap_lock_carefully(mm, regs)) 5783c2508ec5SLinus Torvalds return NULL; 5784c2508ec5SLinus Torvalds 5785c2508ec5SLinus Torvalds vma = find_vma(mm, addr); 5786c2508ec5SLinus Torvalds if (!vma) 5787c2508ec5SLinus Torvalds goto fail; 5788c2508ec5SLinus Torvalds if (vma->vm_start <= addr) 5789c2508ec5SLinus Torvalds goto success; 5790c2508ec5SLinus Torvalds if (!(vma->vm_flags & VM_GROWSDOWN)) 5791c2508ec5SLinus Torvalds goto fail; 5792c2508ec5SLinus Torvalds } 5793c2508ec5SLinus Torvalds 57948d7071afSLinus Torvalds if (expand_stack_locked(vma, addr)) 5795c2508ec5SLinus Torvalds goto fail; 5796c2508ec5SLinus Torvalds 5797c2508ec5SLinus Torvalds success: 5798c2508ec5SLinus Torvalds mmap_write_downgrade(mm); 5799c2508ec5SLinus Torvalds return vma; 5800c2508ec5SLinus Torvalds 5801c2508ec5SLinus Torvalds fail: 5802c2508ec5SLinus Torvalds mmap_write_unlock(mm); 5803c2508ec5SLinus Torvalds return NULL; 5804c2508ec5SLinus Torvalds } 5805c2508ec5SLinus Torvalds #endif 5806c2508ec5SLinus Torvalds 580750ee3253SSuren Baghdasaryan #ifdef CONFIG_PER_VMA_LOCK 580850ee3253SSuren Baghdasaryan /* 580950ee3253SSuren Baghdasaryan * Lookup and lock a VMA under RCU protection. Returned VMA is guaranteed to be 581050ee3253SSuren Baghdasaryan * stable and not isolated. If the VMA is not found or is being modified the 581150ee3253SSuren Baghdasaryan * function returns NULL. 581250ee3253SSuren Baghdasaryan */ 581350ee3253SSuren Baghdasaryan struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm, 581450ee3253SSuren Baghdasaryan unsigned long address) 581550ee3253SSuren Baghdasaryan { 581650ee3253SSuren Baghdasaryan MA_STATE(mas, &mm->mm_mt, address, address); 581750ee3253SSuren Baghdasaryan struct vm_area_struct *vma; 581850ee3253SSuren Baghdasaryan 581950ee3253SSuren Baghdasaryan rcu_read_lock(); 582050ee3253SSuren Baghdasaryan retry: 582150ee3253SSuren Baghdasaryan vma = mas_walk(&mas); 582250ee3253SSuren Baghdasaryan if (!vma) 582350ee3253SSuren Baghdasaryan goto inval; 582450ee3253SSuren Baghdasaryan 582550ee3253SSuren Baghdasaryan if (!vma_start_read(vma)) 582650ee3253SSuren Baghdasaryan goto inval; 582750ee3253SSuren Baghdasaryan 5828444eeb17SSuren Baghdasaryan /* 5829657b5146SJann Horn * find_mergeable_anon_vma uses adjacent vmas which are not locked. 5830657b5146SJann Horn * This check must happen after vma_start_read(); otherwise, a 5831657b5146SJann Horn * concurrent mremap() with MREMAP_DONTUNMAP could dissociate the VMA 5832657b5146SJann Horn * from its anon_vma. 5833657b5146SJann Horn */ 583429a22b9eSSuren Baghdasaryan if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma)) 5835657b5146SJann Horn goto inval_end_read; 5836444eeb17SSuren Baghdasaryan 583750ee3253SSuren Baghdasaryan /* Check since vm_start/vm_end might change before we lock the VMA */ 5838657b5146SJann Horn if (unlikely(address < vma->vm_start || address >= vma->vm_end)) 5839657b5146SJann Horn goto inval_end_read; 584050ee3253SSuren Baghdasaryan 584150ee3253SSuren Baghdasaryan /* Check if the VMA got isolated after we found it */ 584250ee3253SSuren Baghdasaryan if (vma->detached) { 584350ee3253SSuren Baghdasaryan vma_end_read(vma); 584452f23865SSuren Baghdasaryan count_vm_vma_lock_event(VMA_LOCK_MISS); 584550ee3253SSuren Baghdasaryan /* The area was replaced with another one */ 584650ee3253SSuren Baghdasaryan goto retry; 584750ee3253SSuren Baghdasaryan } 584850ee3253SSuren Baghdasaryan 584950ee3253SSuren Baghdasaryan rcu_read_unlock(); 585050ee3253SSuren Baghdasaryan return vma; 5851657b5146SJann Horn 5852657b5146SJann Horn inval_end_read: 5853657b5146SJann Horn vma_end_read(vma); 585450ee3253SSuren Baghdasaryan inval: 585550ee3253SSuren Baghdasaryan rcu_read_unlock(); 585652f23865SSuren Baghdasaryan count_vm_vma_lock_event(VMA_LOCK_ABORT); 585750ee3253SSuren Baghdasaryan return NULL; 585850ee3253SSuren Baghdasaryan } 585950ee3253SSuren Baghdasaryan #endif /* CONFIG_PER_VMA_LOCK */ 586050ee3253SSuren Baghdasaryan 586190eceff1SKirill A. Shutemov #ifndef __PAGETABLE_P4D_FOLDED 586290eceff1SKirill A. Shutemov /* 586390eceff1SKirill A. Shutemov * Allocate p4d page table. 586490eceff1SKirill A. Shutemov * We've already handled the fast-path in-line. 586590eceff1SKirill A. Shutemov */ 586690eceff1SKirill A. Shutemov int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address) 586790eceff1SKirill A. Shutemov { 586890eceff1SKirill A. Shutemov p4d_t *new = p4d_alloc_one(mm, address); 586990eceff1SKirill A. Shutemov if (!new) 587090eceff1SKirill A. Shutemov return -ENOMEM; 587190eceff1SKirill A. Shutemov 587290eceff1SKirill A. Shutemov spin_lock(&mm->page_table_lock); 5873ed33b5a6SQi Zheng if (pgd_present(*pgd)) { /* Another has populated it */ 587490eceff1SKirill A. Shutemov p4d_free(mm, new); 5875ed33b5a6SQi Zheng } else { 5876ed33b5a6SQi Zheng smp_wmb(); /* See comment in pmd_install() */ 587790eceff1SKirill A. Shutemov pgd_populate(mm, pgd, new); 5878ed33b5a6SQi Zheng } 587990eceff1SKirill A. Shutemov spin_unlock(&mm->page_table_lock); 588090eceff1SKirill A. Shutemov return 0; 588190eceff1SKirill A. Shutemov } 588290eceff1SKirill A. Shutemov #endif /* __PAGETABLE_P4D_FOLDED */ 588390eceff1SKirill A. Shutemov 58841da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED 58851da177e4SLinus Torvalds /* 58861da177e4SLinus Torvalds * Allocate page upper directory. 5887872fec16SHugh Dickins * We've already handled the fast-path in-line. 58881da177e4SLinus Torvalds */ 5889c2febafcSKirill A. Shutemov int __pud_alloc(struct mm_struct *mm, p4d_t *p4d, unsigned long address) 58901da177e4SLinus Torvalds { 5891c74df32cSHugh Dickins pud_t *new = pud_alloc_one(mm, address); 5892c74df32cSHugh Dickins if (!new) 58931bb3630eSHugh Dickins return -ENOMEM; 58941da177e4SLinus Torvalds 5895872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 5896b4e98d9aSKirill A. Shutemov if (!p4d_present(*p4d)) { 5897b4e98d9aSKirill A. Shutemov mm_inc_nr_puds(mm); 5898ed33b5a6SQi Zheng smp_wmb(); /* See comment in pmd_install() */ 5899c2febafcSKirill A. Shutemov p4d_populate(mm, p4d, new); 5900b4e98d9aSKirill A. Shutemov } else /* Another has populated it */ 5901c2febafcSKirill A. Shutemov pud_free(mm, new); 5902872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 59031bb3630eSHugh Dickins return 0; 59041da177e4SLinus Torvalds } 59051da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */ 59061da177e4SLinus Torvalds 59071da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED 59081da177e4SLinus Torvalds /* 59091da177e4SLinus Torvalds * Allocate page middle directory. 5910872fec16SHugh Dickins * We've already handled the fast-path in-line. 59111da177e4SLinus Torvalds */ 59121bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) 59131da177e4SLinus Torvalds { 5914a00cc7d9SMatthew Wilcox spinlock_t *ptl; 5915c74df32cSHugh Dickins pmd_t *new = pmd_alloc_one(mm, address); 5916c74df32cSHugh Dickins if (!new) 59171bb3630eSHugh Dickins return -ENOMEM; 59181da177e4SLinus Torvalds 5919a00cc7d9SMatthew Wilcox ptl = pud_lock(mm, pud); 5920dc6c9a35SKirill A. Shutemov if (!pud_present(*pud)) { 5921dc6c9a35SKirill A. Shutemov mm_inc_nr_pmds(mm); 5922ed33b5a6SQi Zheng smp_wmb(); /* See comment in pmd_install() */ 59231da177e4SLinus Torvalds pud_populate(mm, pud, new); 5924ed33b5a6SQi Zheng } else { /* Another has populated it */ 59255e541973SBenjamin Herrenschmidt pmd_free(mm, new); 5926ed33b5a6SQi Zheng } 5927a00cc7d9SMatthew Wilcox spin_unlock(ptl); 59281bb3630eSHugh Dickins return 0; 59291da177e4SLinus Torvalds } 59301da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */ 59311da177e4SLinus Torvalds 59323b6748e2SJohannes Weiner /** 59339fd6dad1SPaolo Bonzini * follow_pte - look up PTE at a user virtual address 59349fd6dad1SPaolo Bonzini * @mm: the mm_struct of the target address space 59359fd6dad1SPaolo Bonzini * @address: user virtual address 59369fd6dad1SPaolo Bonzini * @ptepp: location to store found PTE 59379fd6dad1SPaolo Bonzini * @ptlp: location to store the lock for the PTE 59389fd6dad1SPaolo Bonzini * 59399fd6dad1SPaolo Bonzini * On a successful return, the pointer to the PTE is stored in @ptepp; 59409fd6dad1SPaolo Bonzini * the corresponding lock is taken and its location is stored in @ptlp. 59419fd6dad1SPaolo Bonzini * The contents of the PTE are only stable until @ptlp is released; 59429fd6dad1SPaolo Bonzini * any further use, if any, must be protected against invalidation 59439fd6dad1SPaolo Bonzini * with MMU notifiers. 59449fd6dad1SPaolo Bonzini * 59459fd6dad1SPaolo Bonzini * Only IO mappings and raw PFN mappings are allowed. The mmap semaphore 59469fd6dad1SPaolo Bonzini * should be taken for read. 59479fd6dad1SPaolo Bonzini * 59489fd6dad1SPaolo Bonzini * KVM uses this function. While it is arguably less bad than ``follow_pfn``, 59499fd6dad1SPaolo Bonzini * it is not a good general-purpose API. 59509fd6dad1SPaolo Bonzini * 59519fd6dad1SPaolo Bonzini * Return: zero on success, -ve otherwise. 59529fd6dad1SPaolo Bonzini */ 59539fd6dad1SPaolo Bonzini int follow_pte(struct mm_struct *mm, unsigned long address, 59549fd6dad1SPaolo Bonzini pte_t **ptepp, spinlock_t **ptlp) 59559fd6dad1SPaolo Bonzini { 59560e5e64c0SMuchun Song pgd_t *pgd; 59570e5e64c0SMuchun Song p4d_t *p4d; 59580e5e64c0SMuchun Song pud_t *pud; 59590e5e64c0SMuchun Song pmd_t *pmd; 59600e5e64c0SMuchun Song pte_t *ptep; 59610e5e64c0SMuchun Song 59620e5e64c0SMuchun Song pgd = pgd_offset(mm, address); 59630e5e64c0SMuchun Song if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) 59640e5e64c0SMuchun Song goto out; 59650e5e64c0SMuchun Song 59660e5e64c0SMuchun Song p4d = p4d_offset(pgd, address); 59670e5e64c0SMuchun Song if (p4d_none(*p4d) || unlikely(p4d_bad(*p4d))) 59680e5e64c0SMuchun Song goto out; 59690e5e64c0SMuchun Song 59700e5e64c0SMuchun Song pud = pud_offset(p4d, address); 59710e5e64c0SMuchun Song if (pud_none(*pud) || unlikely(pud_bad(*pud))) 59720e5e64c0SMuchun Song goto out; 59730e5e64c0SMuchun Song 59740e5e64c0SMuchun Song pmd = pmd_offset(pud, address); 59750e5e64c0SMuchun Song VM_BUG_ON(pmd_trans_huge(*pmd)); 59760e5e64c0SMuchun Song 59770e5e64c0SMuchun Song ptep = pte_offset_map_lock(mm, pmd, address, ptlp); 59783db82b93SHugh Dickins if (!ptep) 59793db82b93SHugh Dickins goto out; 5980c33c7948SRyan Roberts if (!pte_present(ptep_get(ptep))) 59810e5e64c0SMuchun Song goto unlock; 59820e5e64c0SMuchun Song *ptepp = ptep; 59830e5e64c0SMuchun Song return 0; 59840e5e64c0SMuchun Song unlock: 59850e5e64c0SMuchun Song pte_unmap_unlock(ptep, *ptlp); 59860e5e64c0SMuchun Song out: 59870e5e64c0SMuchun Song return -EINVAL; 59889fd6dad1SPaolo Bonzini } 59899fd6dad1SPaolo Bonzini EXPORT_SYMBOL_GPL(follow_pte); 59909fd6dad1SPaolo Bonzini 59919fd6dad1SPaolo Bonzini /** 59923b6748e2SJohannes Weiner * follow_pfn - look up PFN at a user virtual address 59933b6748e2SJohannes Weiner * @vma: memory mapping 59943b6748e2SJohannes Weiner * @address: user virtual address 59953b6748e2SJohannes Weiner * @pfn: location to store found PFN 59963b6748e2SJohannes Weiner * 59973b6748e2SJohannes Weiner * Only IO mappings and raw PFN mappings are allowed. 59983b6748e2SJohannes Weiner * 59999fd6dad1SPaolo Bonzini * This function does not allow the caller to read the permissions 60009fd6dad1SPaolo Bonzini * of the PTE. Do not use it. 60019fd6dad1SPaolo Bonzini * 6002a862f68aSMike Rapoport * Return: zero and the pfn at @pfn on success, -ve otherwise. 60033b6748e2SJohannes Weiner */ 60043b6748e2SJohannes Weiner int follow_pfn(struct vm_area_struct *vma, unsigned long address, 60053b6748e2SJohannes Weiner unsigned long *pfn) 60063b6748e2SJohannes Weiner { 60073b6748e2SJohannes Weiner int ret = -EINVAL; 60083b6748e2SJohannes Weiner spinlock_t *ptl; 60093b6748e2SJohannes Weiner pte_t *ptep; 60103b6748e2SJohannes Weiner 60113b6748e2SJohannes Weiner if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 60123b6748e2SJohannes Weiner return ret; 60133b6748e2SJohannes Weiner 60149fd6dad1SPaolo Bonzini ret = follow_pte(vma->vm_mm, address, &ptep, &ptl); 60153b6748e2SJohannes Weiner if (ret) 60163b6748e2SJohannes Weiner return ret; 6017c33c7948SRyan Roberts *pfn = pte_pfn(ptep_get(ptep)); 60183b6748e2SJohannes Weiner pte_unmap_unlock(ptep, ptl); 60193b6748e2SJohannes Weiner return 0; 60203b6748e2SJohannes Weiner } 60213b6748e2SJohannes Weiner EXPORT_SYMBOL(follow_pfn); 60223b6748e2SJohannes Weiner 602328b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT 6024d87fe660Svenkatesh.pallipadi@intel.com int follow_phys(struct vm_area_struct *vma, 602528b2ee20SRik van Riel unsigned long address, unsigned int flags, 6026d87fe660Svenkatesh.pallipadi@intel.com unsigned long *prot, resource_size_t *phys) 602728b2ee20SRik van Riel { 602803668a4dSJohannes Weiner int ret = -EINVAL; 602928b2ee20SRik van Riel pte_t *ptep, pte; 603028b2ee20SRik van Riel spinlock_t *ptl; 603128b2ee20SRik van Riel 6032d87fe660Svenkatesh.pallipadi@intel.com if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 6033d87fe660Svenkatesh.pallipadi@intel.com goto out; 603428b2ee20SRik van Riel 60359fd6dad1SPaolo Bonzini if (follow_pte(vma->vm_mm, address, &ptep, &ptl)) 6036d87fe660Svenkatesh.pallipadi@intel.com goto out; 6037c33c7948SRyan Roberts pte = ptep_get(ptep); 603803668a4dSJohannes Weiner 6039f6f37321SLinus Torvalds if ((flags & FOLL_WRITE) && !pte_write(pte)) 604028b2ee20SRik van Riel goto unlock; 604128b2ee20SRik van Riel 604228b2ee20SRik van Riel *prot = pgprot_val(pte_pgprot(pte)); 604303668a4dSJohannes Weiner *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT; 604428b2ee20SRik van Riel 604503668a4dSJohannes Weiner ret = 0; 604628b2ee20SRik van Riel unlock: 604728b2ee20SRik van Riel pte_unmap_unlock(ptep, ptl); 604828b2ee20SRik van Riel out: 6049d87fe660Svenkatesh.pallipadi@intel.com return ret; 605028b2ee20SRik van Riel } 605128b2ee20SRik van Riel 605296667f8aSDaniel Vetter /** 605396667f8aSDaniel Vetter * generic_access_phys - generic implementation for iomem mmap access 605496667f8aSDaniel Vetter * @vma: the vma to access 6055f0953a1bSIngo Molnar * @addr: userspace address, not relative offset within @vma 605696667f8aSDaniel Vetter * @buf: buffer to read/write 605796667f8aSDaniel Vetter * @len: length of transfer 605896667f8aSDaniel Vetter * @write: set to FOLL_WRITE when writing, otherwise reading 605996667f8aSDaniel Vetter * 606096667f8aSDaniel Vetter * This is a generic implementation for &vm_operations_struct.access for an 606196667f8aSDaniel Vetter * iomem mapping. This callback is used by access_process_vm() when the @vma is 606296667f8aSDaniel Vetter * not page based. 606396667f8aSDaniel Vetter */ 606428b2ee20SRik van Riel int generic_access_phys(struct vm_area_struct *vma, unsigned long addr, 606528b2ee20SRik van Riel void *buf, int len, int write) 606628b2ee20SRik van Riel { 606728b2ee20SRik van Riel resource_size_t phys_addr; 606828b2ee20SRik van Riel unsigned long prot = 0; 60692bc7273bSKOSAKI Motohiro void __iomem *maddr; 607096667f8aSDaniel Vetter pte_t *ptep, pte; 607196667f8aSDaniel Vetter spinlock_t *ptl; 607296667f8aSDaniel Vetter int offset = offset_in_page(addr); 607396667f8aSDaniel Vetter int ret = -EINVAL; 607428b2ee20SRik van Riel 607596667f8aSDaniel Vetter if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 607696667f8aSDaniel Vetter return -EINVAL; 607796667f8aSDaniel Vetter 607896667f8aSDaniel Vetter retry: 6079e913a8cdSLinus Torvalds if (follow_pte(vma->vm_mm, addr, &ptep, &ptl)) 608096667f8aSDaniel Vetter return -EINVAL; 6081c33c7948SRyan Roberts pte = ptep_get(ptep); 608296667f8aSDaniel Vetter pte_unmap_unlock(ptep, ptl); 608396667f8aSDaniel Vetter 608496667f8aSDaniel Vetter prot = pgprot_val(pte_pgprot(pte)); 608596667f8aSDaniel Vetter phys_addr = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT; 608696667f8aSDaniel Vetter 608796667f8aSDaniel Vetter if ((write & FOLL_WRITE) && !pte_write(pte)) 608828b2ee20SRik van Riel return -EINVAL; 608928b2ee20SRik van Riel 60909cb12d7bSGrazvydas Ignotas maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot); 609124eee1e4Sjie@chenjie6@huwei.com if (!maddr) 609224eee1e4Sjie@chenjie6@huwei.com return -ENOMEM; 609324eee1e4Sjie@chenjie6@huwei.com 6094e913a8cdSLinus Torvalds if (follow_pte(vma->vm_mm, addr, &ptep, &ptl)) 609596667f8aSDaniel Vetter goto out_unmap; 609696667f8aSDaniel Vetter 6097c33c7948SRyan Roberts if (!pte_same(pte, ptep_get(ptep))) { 609896667f8aSDaniel Vetter pte_unmap_unlock(ptep, ptl); 609996667f8aSDaniel Vetter iounmap(maddr); 610096667f8aSDaniel Vetter 610196667f8aSDaniel Vetter goto retry; 610296667f8aSDaniel Vetter } 610396667f8aSDaniel Vetter 610428b2ee20SRik van Riel if (write) 610528b2ee20SRik van Riel memcpy_toio(maddr + offset, buf, len); 610628b2ee20SRik van Riel else 610728b2ee20SRik van Riel memcpy_fromio(buf, maddr + offset, len); 610896667f8aSDaniel Vetter ret = len; 610996667f8aSDaniel Vetter pte_unmap_unlock(ptep, ptl); 611096667f8aSDaniel Vetter out_unmap: 611128b2ee20SRik van Riel iounmap(maddr); 611228b2ee20SRik van Riel 611396667f8aSDaniel Vetter return ret; 611428b2ee20SRik van Riel } 61155a73633eSUwe Kleine-König EXPORT_SYMBOL_GPL(generic_access_phys); 611628b2ee20SRik van Riel #endif 611728b2ee20SRik van Riel 61180ec76a11SDavid Howells /* 6119d3f5ffcaSJohn Hubbard * Access another process' address space as given in mm. 61200ec76a11SDavid Howells */ 6121c43cfa42SLorenzo Stoakes static int __access_remote_vm(struct mm_struct *mm, unsigned long addr, 6122c43cfa42SLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 61230ec76a11SDavid Howells { 61240ec76a11SDavid Howells void *old_buf = buf; 6125442486ecSLorenzo Stoakes int write = gup_flags & FOLL_WRITE; 61260ec76a11SDavid Howells 6127d8ed45c5SMichel Lespinasse if (mmap_read_lock_killable(mm)) 61281e426fe2SKonstantin Khlebnikov return 0; 61291e426fe2SKonstantin Khlebnikov 613022883973SKirill A. Shutemov /* Untag the address before looking up the VMA */ 613122883973SKirill A. Shutemov addr = untagged_addr_remote(mm, addr); 613222883973SKirill A. Shutemov 6133eee9c708SLinus Torvalds /* Avoid triggering the temporary warning in __get_user_pages */ 6134eee9c708SLinus Torvalds if (!vma_lookup(mm, addr) && !expand_stack(mm, addr)) 6135eee9c708SLinus Torvalds return 0; 6136eee9c708SLinus Torvalds 6137183ff22bSSimon Arlott /* ignore errors, just check how much was successfully transferred */ 61380ec76a11SDavid Howells while (len) { 6139ca5e8632SLorenzo Stoakes int bytes, offset; 61400ec76a11SDavid Howells void *maddr; 6141ca5e8632SLorenzo Stoakes struct vm_area_struct *vma = NULL; 6142ca5e8632SLorenzo Stoakes struct page *page = get_user_page_vma_remote(mm, addr, 6143ca5e8632SLorenzo Stoakes gup_flags, &vma); 61440ec76a11SDavid Howells 61456a1960b8SLorenzo Stoakes if (IS_ERR(page)) { 61468d7071afSLinus Torvalds /* We might need to expand the stack to access it */ 61478d7071afSLinus Torvalds vma = vma_lookup(mm, addr); 61488d7071afSLinus Torvalds if (!vma) { 61498d7071afSLinus Torvalds vma = expand_stack(mm, addr); 61509471f1f2SLinus Torvalds 61519471f1f2SLinus Torvalds /* mmap_lock was dropped on failure */ 61528d7071afSLinus Torvalds if (!vma) 61539471f1f2SLinus Torvalds return buf - old_buf; 61549471f1f2SLinus Torvalds 61559471f1f2SLinus Torvalds /* Try again if stack expansion worked */ 61569471f1f2SLinus Torvalds continue; 61578d7071afSLinus Torvalds } 61588d7071afSLinus Torvalds 615928b2ee20SRik van Riel /* 616028b2ee20SRik van Riel * Check if this is a VM_IO | VM_PFNMAP VMA, which 616128b2ee20SRik van Riel * we can access using slightly different code. 616228b2ee20SRik van Riel */ 61639471f1f2SLinus Torvalds bytes = 0; 61649471f1f2SLinus Torvalds #ifdef CONFIG_HAVE_IOREMAP_PROT 616528b2ee20SRik van Riel if (vma->vm_ops && vma->vm_ops->access) 61669471f1f2SLinus Torvalds bytes = vma->vm_ops->access(vma, addr, buf, 616728b2ee20SRik van Riel len, write); 6168dbffcd03SRik van Riel #endif 61699471f1f2SLinus Torvalds if (bytes <= 0) 61709471f1f2SLinus Torvalds break; 617128b2ee20SRik van Riel } else { 61720ec76a11SDavid Howells bytes = len; 61730ec76a11SDavid Howells offset = addr & (PAGE_SIZE-1); 61740ec76a11SDavid Howells if (bytes > PAGE_SIZE-offset) 61750ec76a11SDavid Howells bytes = PAGE_SIZE-offset; 61760ec76a11SDavid Howells 6177f7ef5fe7SFabio M. De Francesco maddr = kmap_local_page(page); 61780ec76a11SDavid Howells if (write) { 61790ec76a11SDavid Howells copy_to_user_page(vma, page, addr, 61800ec76a11SDavid Howells maddr + offset, buf, bytes); 61810ec76a11SDavid Howells set_page_dirty_lock(page); 61820ec76a11SDavid Howells } else { 61830ec76a11SDavid Howells copy_from_user_page(vma, page, addr, 61840ec76a11SDavid Howells buf, maddr + offset, bytes); 61850ec76a11SDavid Howells } 6186f7ef5fe7SFabio M. De Francesco unmap_and_put_page(page, maddr); 618728b2ee20SRik van Riel } 61880ec76a11SDavid Howells len -= bytes; 61890ec76a11SDavid Howells buf += bytes; 61900ec76a11SDavid Howells addr += bytes; 61910ec76a11SDavid Howells } 6192d8ed45c5SMichel Lespinasse mmap_read_unlock(mm); 61930ec76a11SDavid Howells 61940ec76a11SDavid Howells return buf - old_buf; 61950ec76a11SDavid Howells } 619603252919SAndi Kleen 61975ddd36b9SStephen Wilson /** 6198ae91dbfcSRandy Dunlap * access_remote_vm - access another process' address space 61995ddd36b9SStephen Wilson * @mm: the mm_struct of the target address space 62005ddd36b9SStephen Wilson * @addr: start address to access 62015ddd36b9SStephen Wilson * @buf: source or destination buffer 62025ddd36b9SStephen Wilson * @len: number of bytes to transfer 62036347e8d5SLorenzo Stoakes * @gup_flags: flags modifying lookup behaviour 62045ddd36b9SStephen Wilson * 62055ddd36b9SStephen Wilson * The caller must hold a reference on @mm. 6206a862f68aSMike Rapoport * 6207a862f68aSMike Rapoport * Return: number of bytes copied from source to destination. 62085ddd36b9SStephen Wilson */ 62095ddd36b9SStephen Wilson int access_remote_vm(struct mm_struct *mm, unsigned long addr, 62106347e8d5SLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 62115ddd36b9SStephen Wilson { 6212d3f5ffcaSJohn Hubbard return __access_remote_vm(mm, addr, buf, len, gup_flags); 62135ddd36b9SStephen Wilson } 62145ddd36b9SStephen Wilson 621503252919SAndi Kleen /* 6216206cb636SStephen Wilson * Access another process' address space. 6217206cb636SStephen Wilson * Source/target buffer must be kernel space, 6218206cb636SStephen Wilson * Do not walk the page table directly, use get_user_pages 6219206cb636SStephen Wilson */ 6220206cb636SStephen Wilson int access_process_vm(struct task_struct *tsk, unsigned long addr, 6221f307ab6dSLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 6222206cb636SStephen Wilson { 6223206cb636SStephen Wilson struct mm_struct *mm; 6224206cb636SStephen Wilson int ret; 6225206cb636SStephen Wilson 6226206cb636SStephen Wilson mm = get_task_mm(tsk); 6227206cb636SStephen Wilson if (!mm) 6228206cb636SStephen Wilson return 0; 6229206cb636SStephen Wilson 6230d3f5ffcaSJohn Hubbard ret = __access_remote_vm(mm, addr, buf, len, gup_flags); 6231442486ecSLorenzo Stoakes 6232206cb636SStephen Wilson mmput(mm); 6233206cb636SStephen Wilson 6234206cb636SStephen Wilson return ret; 6235206cb636SStephen Wilson } 6236fcd35857SCatalin Marinas EXPORT_SYMBOL_GPL(access_process_vm); 6237206cb636SStephen Wilson 623803252919SAndi Kleen /* 623903252919SAndi Kleen * Print the name of a VMA. 624003252919SAndi Kleen */ 624103252919SAndi Kleen void print_vma_addr(char *prefix, unsigned long ip) 624203252919SAndi Kleen { 624303252919SAndi Kleen struct mm_struct *mm = current->mm; 624403252919SAndi Kleen struct vm_area_struct *vma; 624503252919SAndi Kleen 6246e8bff74aSIngo Molnar /* 62470a7f682dSMichal Hocko * we might be running from an atomic context so we cannot sleep 6248e8bff74aSIngo Molnar */ 6249d8ed45c5SMichel Lespinasse if (!mmap_read_trylock(mm)) 6250e8bff74aSIngo Molnar return; 6251e8bff74aSIngo Molnar 625203252919SAndi Kleen vma = find_vma(mm, ip); 625303252919SAndi Kleen if (vma && vma->vm_file) { 625403252919SAndi Kleen struct file *f = vma->vm_file; 62550a7f682dSMichal Hocko char *buf = (char *)__get_free_page(GFP_NOWAIT); 625603252919SAndi Kleen if (buf) { 62572fbc57c5SAndy Shevchenko char *p; 625803252919SAndi Kleen 62599bf39ab2SMiklos Szeredi p = file_path(f, buf, PAGE_SIZE); 626003252919SAndi Kleen if (IS_ERR(p)) 626103252919SAndi Kleen p = "?"; 62622fbc57c5SAndy Shevchenko printk("%s%s[%lx+%lx]", prefix, kbasename(p), 626303252919SAndi Kleen vma->vm_start, 626403252919SAndi Kleen vma->vm_end - vma->vm_start); 626503252919SAndi Kleen free_page((unsigned long)buf); 626603252919SAndi Kleen } 626703252919SAndi Kleen } 6268d8ed45c5SMichel Lespinasse mmap_read_unlock(mm); 626903252919SAndi Kleen } 62703ee1afa3SNick Piggin 6271662bbcb2SMichael S. Tsirkin #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP) 62729ec23531SDavid Hildenbrand void __might_fault(const char *file, int line) 62733ee1afa3SNick Piggin { 62749ec23531SDavid Hildenbrand if (pagefault_disabled()) 6275662bbcb2SMichael S. Tsirkin return; 627642a38756SThomas Gleixner __might_sleep(file, line); 62779ec23531SDavid Hildenbrand #if defined(CONFIG_DEBUG_ATOMIC_SLEEP) 6278662bbcb2SMichael S. Tsirkin if (current->mm) 6279da1c55f1SMichel Lespinasse might_lock_read(¤t->mm->mmap_lock); 62809ec23531SDavid Hildenbrand #endif 62813ee1afa3SNick Piggin } 62829ec23531SDavid Hildenbrand EXPORT_SYMBOL(__might_fault); 62833ee1afa3SNick Piggin #endif 628447ad8475SAndrea Arcangeli 628547ad8475SAndrea Arcangeli #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS) 6286c6ddfb6cSHuang Ying /* 6287c6ddfb6cSHuang Ying * Process all subpages of the specified huge page with the specified 6288c6ddfb6cSHuang Ying * operation. The target subpage will be processed last to keep its 6289c6ddfb6cSHuang Ying * cache lines hot. 6290c6ddfb6cSHuang Ying */ 62911cb9dc4bSLiu Shixin static inline int process_huge_page( 6292c6ddfb6cSHuang Ying unsigned long addr_hint, unsigned int pages_per_huge_page, 62931cb9dc4bSLiu Shixin int (*process_subpage)(unsigned long addr, int idx, void *arg), 6294c6ddfb6cSHuang Ying void *arg) 6295c6ddfb6cSHuang Ying { 62961cb9dc4bSLiu Shixin int i, n, base, l, ret; 6297c6ddfb6cSHuang Ying unsigned long addr = addr_hint & 6298c6ddfb6cSHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 6299c6ddfb6cSHuang Ying 6300c6ddfb6cSHuang Ying /* Process target subpage last to keep its cache lines hot */ 6301c6ddfb6cSHuang Ying might_sleep(); 6302c6ddfb6cSHuang Ying n = (addr_hint - addr) / PAGE_SIZE; 6303c6ddfb6cSHuang Ying if (2 * n <= pages_per_huge_page) { 6304c6ddfb6cSHuang Ying /* If target subpage in first half of huge page */ 6305c6ddfb6cSHuang Ying base = 0; 6306c6ddfb6cSHuang Ying l = n; 6307c6ddfb6cSHuang Ying /* Process subpages at the end of huge page */ 6308c6ddfb6cSHuang Ying for (i = pages_per_huge_page - 1; i >= 2 * n; i--) { 6309c6ddfb6cSHuang Ying cond_resched(); 63101cb9dc4bSLiu Shixin ret = process_subpage(addr + i * PAGE_SIZE, i, arg); 63111cb9dc4bSLiu Shixin if (ret) 63121cb9dc4bSLiu Shixin return ret; 6313c6ddfb6cSHuang Ying } 6314c6ddfb6cSHuang Ying } else { 6315c6ddfb6cSHuang Ying /* If target subpage in second half of huge page */ 6316c6ddfb6cSHuang Ying base = pages_per_huge_page - 2 * (pages_per_huge_page - n); 6317c6ddfb6cSHuang Ying l = pages_per_huge_page - n; 6318c6ddfb6cSHuang Ying /* Process subpages at the begin of huge page */ 6319c6ddfb6cSHuang Ying for (i = 0; i < base; i++) { 6320c6ddfb6cSHuang Ying cond_resched(); 63211cb9dc4bSLiu Shixin ret = process_subpage(addr + i * PAGE_SIZE, i, arg); 63221cb9dc4bSLiu Shixin if (ret) 63231cb9dc4bSLiu Shixin return ret; 6324c6ddfb6cSHuang Ying } 6325c6ddfb6cSHuang Ying } 6326c6ddfb6cSHuang Ying /* 6327c6ddfb6cSHuang Ying * Process remaining subpages in left-right-left-right pattern 6328c6ddfb6cSHuang Ying * towards the target subpage 6329c6ddfb6cSHuang Ying */ 6330c6ddfb6cSHuang Ying for (i = 0; i < l; i++) { 6331c6ddfb6cSHuang Ying int left_idx = base + i; 6332c6ddfb6cSHuang Ying int right_idx = base + 2 * l - 1 - i; 6333c6ddfb6cSHuang Ying 6334c6ddfb6cSHuang Ying cond_resched(); 63351cb9dc4bSLiu Shixin ret = process_subpage(addr + left_idx * PAGE_SIZE, left_idx, arg); 63361cb9dc4bSLiu Shixin if (ret) 63371cb9dc4bSLiu Shixin return ret; 6338c6ddfb6cSHuang Ying cond_resched(); 63391cb9dc4bSLiu Shixin ret = process_subpage(addr + right_idx * PAGE_SIZE, right_idx, arg); 63401cb9dc4bSLiu Shixin if (ret) 63411cb9dc4bSLiu Shixin return ret; 6342c6ddfb6cSHuang Ying } 63431cb9dc4bSLiu Shixin return 0; 6344c6ddfb6cSHuang Ying } 6345c6ddfb6cSHuang Ying 634647ad8475SAndrea Arcangeli static void clear_gigantic_page(struct page *page, 634747ad8475SAndrea Arcangeli unsigned long addr, 634847ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 634947ad8475SAndrea Arcangeli { 635047ad8475SAndrea Arcangeli int i; 635114455eabSCheng Li struct page *p; 635247ad8475SAndrea Arcangeli 635347ad8475SAndrea Arcangeli might_sleep(); 635414455eabSCheng Li for (i = 0; i < pages_per_huge_page; i++) { 635514455eabSCheng Li p = nth_page(page, i); 635647ad8475SAndrea Arcangeli cond_resched(); 635747ad8475SAndrea Arcangeli clear_user_highpage(p, addr + i * PAGE_SIZE); 635847ad8475SAndrea Arcangeli } 635947ad8475SAndrea Arcangeli } 6360c6ddfb6cSHuang Ying 63611cb9dc4bSLiu Shixin static int clear_subpage(unsigned long addr, int idx, void *arg) 6362c6ddfb6cSHuang Ying { 6363c6ddfb6cSHuang Ying struct page *page = arg; 6364c6ddfb6cSHuang Ying 636521fff064SKefeng Wang clear_user_highpage(nth_page(page, idx), addr); 63661cb9dc4bSLiu Shixin return 0; 6367c6ddfb6cSHuang Ying } 6368c6ddfb6cSHuang Ying 636947ad8475SAndrea Arcangeli void clear_huge_page(struct page *page, 6370c79b57e4SHuang Ying unsigned long addr_hint, unsigned int pages_per_huge_page) 637147ad8475SAndrea Arcangeli { 6372c79b57e4SHuang Ying unsigned long addr = addr_hint & 6373c79b57e4SHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 637447ad8475SAndrea Arcangeli 637547ad8475SAndrea Arcangeli if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { 637647ad8475SAndrea Arcangeli clear_gigantic_page(page, addr, pages_per_huge_page); 637747ad8475SAndrea Arcangeli return; 637847ad8475SAndrea Arcangeli } 637947ad8475SAndrea Arcangeli 6380c6ddfb6cSHuang Ying process_huge_page(addr_hint, pages_per_huge_page, clear_subpage, page); 638147ad8475SAndrea Arcangeli } 638247ad8475SAndrea Arcangeli 63831cb9dc4bSLiu Shixin static int copy_user_gigantic_page(struct folio *dst, struct folio *src, 638447ad8475SAndrea Arcangeli unsigned long addr, 638547ad8475SAndrea Arcangeli struct vm_area_struct *vma, 638647ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 638747ad8475SAndrea Arcangeli { 638847ad8475SAndrea Arcangeli int i; 6389c0e8150eSZhangPeng struct page *dst_page; 6390c0e8150eSZhangPeng struct page *src_page; 639147ad8475SAndrea Arcangeli 639214455eabSCheng Li for (i = 0; i < pages_per_huge_page; i++) { 6393c0e8150eSZhangPeng dst_page = folio_page(dst, i); 6394c0e8150eSZhangPeng src_page = folio_page(src, i); 639514455eabSCheng Li 639647ad8475SAndrea Arcangeli cond_resched(); 63971cb9dc4bSLiu Shixin if (copy_mc_user_highpage(dst_page, src_page, 63981cb9dc4bSLiu Shixin addr + i*PAGE_SIZE, vma)) { 63991cb9dc4bSLiu Shixin memory_failure_queue(page_to_pfn(src_page), 0); 64001cb9dc4bSLiu Shixin return -EHWPOISON; 640147ad8475SAndrea Arcangeli } 640247ad8475SAndrea Arcangeli } 64031cb9dc4bSLiu Shixin return 0; 64041cb9dc4bSLiu Shixin } 640547ad8475SAndrea Arcangeli 6406c9f4cd71SHuang Ying struct copy_subpage_arg { 6407c9f4cd71SHuang Ying struct page *dst; 6408c9f4cd71SHuang Ying struct page *src; 6409c9f4cd71SHuang Ying struct vm_area_struct *vma; 6410c9f4cd71SHuang Ying }; 6411c9f4cd71SHuang Ying 64121cb9dc4bSLiu Shixin static int copy_subpage(unsigned long addr, int idx, void *arg) 6413c9f4cd71SHuang Ying { 6414c9f4cd71SHuang Ying struct copy_subpage_arg *copy_arg = arg; 641521fff064SKefeng Wang struct page *dst = nth_page(copy_arg->dst, idx); 641621fff064SKefeng Wang struct page *src = nth_page(copy_arg->src, idx); 6417c9f4cd71SHuang Ying 641821fff064SKefeng Wang if (copy_mc_user_highpage(dst, src, addr, copy_arg->vma)) { 641921fff064SKefeng Wang memory_failure_queue(page_to_pfn(src), 0); 64201cb9dc4bSLiu Shixin return -EHWPOISON; 64211cb9dc4bSLiu Shixin } 64221cb9dc4bSLiu Shixin return 0; 6423c9f4cd71SHuang Ying } 6424c9f4cd71SHuang Ying 64251cb9dc4bSLiu Shixin int copy_user_large_folio(struct folio *dst, struct folio *src, 6426c0e8150eSZhangPeng unsigned long addr_hint, struct vm_area_struct *vma) 642747ad8475SAndrea Arcangeli { 6428c0e8150eSZhangPeng unsigned int pages_per_huge_page = folio_nr_pages(dst); 6429c9f4cd71SHuang Ying unsigned long addr = addr_hint & 6430c9f4cd71SHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 6431c9f4cd71SHuang Ying struct copy_subpage_arg arg = { 6432c0e8150eSZhangPeng .dst = &dst->page, 6433c0e8150eSZhangPeng .src = &src->page, 6434c9f4cd71SHuang Ying .vma = vma, 6435c9f4cd71SHuang Ying }; 643647ad8475SAndrea Arcangeli 64371cb9dc4bSLiu Shixin if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) 64381cb9dc4bSLiu Shixin return copy_user_gigantic_page(dst, src, addr, vma, 643947ad8475SAndrea Arcangeli pages_per_huge_page); 644047ad8475SAndrea Arcangeli 64411cb9dc4bSLiu Shixin return process_huge_page(addr_hint, pages_per_huge_page, copy_subpage, &arg); 644247ad8475SAndrea Arcangeli } 6443fa4d75c1SMike Kravetz 6444e87340caSZhangPeng long copy_folio_from_user(struct folio *dst_folio, 6445fa4d75c1SMike Kravetz const void __user *usr_src, 6446810a56b9SMike Kravetz bool allow_pagefault) 6447fa4d75c1SMike Kravetz { 6448e87340caSZhangPeng void *kaddr; 6449fa4d75c1SMike Kravetz unsigned long i, rc = 0; 6450e87340caSZhangPeng unsigned int nr_pages = folio_nr_pages(dst_folio); 6451e87340caSZhangPeng unsigned long ret_val = nr_pages * PAGE_SIZE; 645214455eabSCheng Li struct page *subpage; 6453fa4d75c1SMike Kravetz 6454e87340caSZhangPeng for (i = 0; i < nr_pages; i++) { 6455e87340caSZhangPeng subpage = folio_page(dst_folio, i); 6456e87340caSZhangPeng kaddr = kmap_local_page(subpage); 64570d508c1fSZhangPeng if (!allow_pagefault) 64580d508c1fSZhangPeng pagefault_disable(); 6459e87340caSZhangPeng rc = copy_from_user(kaddr, usr_src + i * PAGE_SIZE, PAGE_SIZE); 64600d508c1fSZhangPeng if (!allow_pagefault) 64610d508c1fSZhangPeng pagefault_enable(); 6462e87340caSZhangPeng kunmap_local(kaddr); 6463fa4d75c1SMike Kravetz 6464fa4d75c1SMike Kravetz ret_val -= (PAGE_SIZE - rc); 6465fa4d75c1SMike Kravetz if (rc) 6466fa4d75c1SMike Kravetz break; 6467fa4d75c1SMike Kravetz 6468e763243cSMuchun Song flush_dcache_page(subpage); 6469e763243cSMuchun Song 6470fa4d75c1SMike Kravetz cond_resched(); 6471fa4d75c1SMike Kravetz } 6472fa4d75c1SMike Kravetz return ret_val; 6473fa4d75c1SMike Kravetz } 647447ad8475SAndrea Arcangeli #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */ 647549076ec2SKirill A. Shutemov 647640b64acdSOlof Johansson #if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS 6477b35f1819SKirill A. Shutemov 6478b35f1819SKirill A. Shutemov static struct kmem_cache *page_ptl_cachep; 6479b35f1819SKirill A. Shutemov 6480b35f1819SKirill A. Shutemov void __init ptlock_cache_init(void) 6481b35f1819SKirill A. Shutemov { 6482b35f1819SKirill A. Shutemov page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0, 6483b35f1819SKirill A. Shutemov SLAB_PANIC, NULL); 6484b35f1819SKirill A. Shutemov } 6485b35f1819SKirill A. Shutemov 6486f5ecca06SVishal Moola (Oracle) bool ptlock_alloc(struct ptdesc *ptdesc) 648749076ec2SKirill A. Shutemov { 648849076ec2SKirill A. Shutemov spinlock_t *ptl; 648949076ec2SKirill A. Shutemov 6490b35f1819SKirill A. Shutemov ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL); 649149076ec2SKirill A. Shutemov if (!ptl) 649249076ec2SKirill A. Shutemov return false; 6493f5ecca06SVishal Moola (Oracle) ptdesc->ptl = ptl; 649449076ec2SKirill A. Shutemov return true; 649549076ec2SKirill A. Shutemov } 649649076ec2SKirill A. Shutemov 64976ed1b8a0SVishal Moola (Oracle) void ptlock_free(struct ptdesc *ptdesc) 649849076ec2SKirill A. Shutemov { 64996ed1b8a0SVishal Moola (Oracle) kmem_cache_free(page_ptl_cachep, ptdesc->ptl); 650049076ec2SKirill A. Shutemov } 650149076ec2SKirill A. Shutemov #endif 6502