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 956*25365e10SDavid Hildenbrand /* Flags for folio_pte_batch(). */ 957*25365e10SDavid Hildenbrand typedef int __bitwise fpb_t; 958*25365e10SDavid Hildenbrand 959*25365e10SDavid Hildenbrand /* Compare PTEs after pte_mkclean(), ignoring the dirty bit. */ 960*25365e10SDavid Hildenbrand #define FPB_IGNORE_DIRTY ((__force fpb_t)BIT(0)) 961*25365e10SDavid Hildenbrand 962*25365e10SDavid Hildenbrand /* Compare PTEs after pte_clear_soft_dirty(), ignoring the soft-dirty bit. */ 963*25365e10SDavid Hildenbrand #define FPB_IGNORE_SOFT_DIRTY ((__force fpb_t)BIT(1)) 964*25365e10SDavid Hildenbrand 965*25365e10SDavid Hildenbrand static inline pte_t __pte_batch_clear_ignored(pte_t pte, fpb_t flags) 966*25365e10SDavid Hildenbrand { 967*25365e10SDavid Hildenbrand if (flags & FPB_IGNORE_DIRTY) 968*25365e10SDavid Hildenbrand pte = pte_mkclean(pte); 969*25365e10SDavid Hildenbrand if (likely(flags & FPB_IGNORE_SOFT_DIRTY)) 970*25365e10SDavid Hildenbrand pte = pte_clear_soft_dirty(pte); 971*25365e10SDavid Hildenbrand return pte_mkold(pte); 972*25365e10SDavid Hildenbrand } 973*25365e10SDavid Hildenbrand 97470e806e4SPeter Xu /* 975f8d93776SDavid Hildenbrand * Detect a PTE batch: consecutive (present) PTEs that map consecutive 976f8d93776SDavid Hildenbrand * pages of the same folio. 977f8d93776SDavid Hildenbrand * 978*25365e10SDavid Hildenbrand * All PTEs inside a PTE batch have the same PTE bits set, excluding the PFN, 979*25365e10SDavid Hildenbrand * the accessed bit, dirty bit (with FPB_IGNORE_DIRTY) and soft-dirty bit 980*25365e10SDavid Hildenbrand * (with FPB_IGNORE_SOFT_DIRTY). 981f8d93776SDavid Hildenbrand */ 982f8d93776SDavid Hildenbrand static inline int folio_pte_batch(struct folio *folio, unsigned long addr, 983*25365e10SDavid Hildenbrand pte_t *start_ptep, pte_t pte, int max_nr, fpb_t flags) 984f8d93776SDavid Hildenbrand { 985f8d93776SDavid Hildenbrand unsigned long folio_end_pfn = folio_pfn(folio) + folio_nr_pages(folio); 986f8d93776SDavid Hildenbrand const pte_t *end_ptep = start_ptep + max_nr; 987*25365e10SDavid Hildenbrand pte_t expected_pte = __pte_batch_clear_ignored(pte_next_pfn(pte), flags); 988f8d93776SDavid Hildenbrand pte_t *ptep = start_ptep + 1; 989f8d93776SDavid Hildenbrand 990f8d93776SDavid Hildenbrand VM_WARN_ON_FOLIO(!pte_present(pte), folio); 991f8d93776SDavid Hildenbrand 992f8d93776SDavid Hildenbrand while (ptep != end_ptep) { 993*25365e10SDavid Hildenbrand pte = __pte_batch_clear_ignored(ptep_get(ptep), flags); 994f8d93776SDavid Hildenbrand 995f8d93776SDavid Hildenbrand if (!pte_same(pte, expected_pte)) 996f8d93776SDavid Hildenbrand break; 997f8d93776SDavid Hildenbrand 998f8d93776SDavid Hildenbrand /* 999f8d93776SDavid Hildenbrand * Stop immediately once we reached the end of the folio. In 1000f8d93776SDavid Hildenbrand * corner cases the next PFN might fall into a different 1001f8d93776SDavid Hildenbrand * folio. 1002f8d93776SDavid Hildenbrand */ 1003f8d93776SDavid Hildenbrand if (pte_pfn(pte) == folio_end_pfn) 1004f8d93776SDavid Hildenbrand break; 1005f8d93776SDavid Hildenbrand 1006f8d93776SDavid Hildenbrand expected_pte = pte_next_pfn(expected_pte); 1007f8d93776SDavid Hildenbrand ptep++; 1008f8d93776SDavid Hildenbrand } 1009f8d93776SDavid Hildenbrand 1010f8d93776SDavid Hildenbrand return ptep - start_ptep; 1011f8d93776SDavid Hildenbrand } 1012f8d93776SDavid Hildenbrand 1013f8d93776SDavid Hildenbrand /* 1014f8d93776SDavid Hildenbrand * Copy one present PTE, trying to batch-process subsequent PTEs that map 1015f8d93776SDavid Hildenbrand * consecutive pages of the same folio by copying them as well. 1016f8d93776SDavid Hildenbrand * 1017f8d93776SDavid Hildenbrand * Returns -EAGAIN if one preallocated page is required to copy the next PTE. 1018f8d93776SDavid Hildenbrand * Otherwise, returns the number of copied PTEs (at least 1). 101970e806e4SPeter Xu */ 102070e806e4SPeter Xu static inline int 1021f8d93776SDavid Hildenbrand copy_present_ptes(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, 102253723298SDavid Hildenbrand pte_t *dst_pte, pte_t *src_pte, pte_t pte, unsigned long addr, 1023f8d93776SDavid Hildenbrand int max_nr, int *rss, struct folio **prealloc) 1024df3a57d1SLinus Torvalds { 1025df3a57d1SLinus Torvalds struct page *page; 102614ddee41SMatthew Wilcox (Oracle) struct folio *folio; 1027*25365e10SDavid Hildenbrand fpb_t flags = 0; 1028f8d93776SDavid Hildenbrand int err, nr; 1029df3a57d1SLinus Torvalds 1030c78f4636SPeter Xu page = vm_normal_page(src_vma, addr, pte); 103123ed1908SDavid Hildenbrand if (unlikely(!page)) 103223ed1908SDavid Hildenbrand goto copy_pte; 103323ed1908SDavid Hildenbrand 103414ddee41SMatthew Wilcox (Oracle) folio = page_folio(page); 1035f8d93776SDavid Hildenbrand 1036f8d93776SDavid Hildenbrand /* 1037f8d93776SDavid Hildenbrand * If we likely have to copy, just don't bother with batching. Make 1038f8d93776SDavid Hildenbrand * sure that the common "small folio" case is as fast as possible 1039f8d93776SDavid Hildenbrand * by keeping the batching logic separate. 1040f8d93776SDavid Hildenbrand */ 1041f8d93776SDavid Hildenbrand if (unlikely(!*prealloc && folio_test_large(folio) && max_nr != 1)) { 1042*25365e10SDavid Hildenbrand if (src_vma->vm_flags & VM_SHARED) 1043*25365e10SDavid Hildenbrand flags |= FPB_IGNORE_DIRTY; 1044*25365e10SDavid Hildenbrand if (!vma_soft_dirty_enabled(src_vma)) 1045*25365e10SDavid Hildenbrand flags |= FPB_IGNORE_SOFT_DIRTY; 1046*25365e10SDavid Hildenbrand 1047*25365e10SDavid Hildenbrand nr = folio_pte_batch(folio, addr, src_pte, pte, max_nr, flags); 1048f8d93776SDavid Hildenbrand folio_ref_add(folio, nr); 1049f8d93776SDavid Hildenbrand if (folio_test_anon(folio)) { 1050f8d93776SDavid Hildenbrand if (unlikely(folio_try_dup_anon_rmap_ptes(folio, page, 1051f8d93776SDavid Hildenbrand nr, src_vma))) { 1052f8d93776SDavid Hildenbrand folio_ref_sub(folio, nr); 1053f8d93776SDavid Hildenbrand return -EAGAIN; 1054f8d93776SDavid Hildenbrand } 1055f8d93776SDavid Hildenbrand rss[MM_ANONPAGES] += nr; 1056f8d93776SDavid Hildenbrand VM_WARN_ON_FOLIO(PageAnonExclusive(page), folio); 1057f8d93776SDavid Hildenbrand } else { 1058f8d93776SDavid Hildenbrand folio_dup_file_rmap_ptes(folio, page, nr); 1059f8d93776SDavid Hildenbrand rss[mm_counter_file(folio)] += nr; 1060f8d93776SDavid Hildenbrand } 1061f8d93776SDavid Hildenbrand __copy_present_ptes(dst_vma, src_vma, dst_pte, src_pte, pte, 1062f8d93776SDavid Hildenbrand addr, nr); 1063f8d93776SDavid Hildenbrand return nr; 1064f8d93776SDavid Hildenbrand } 1065f8d93776SDavid Hildenbrand 106623ed1908SDavid Hildenbrand folio_get(folio); 106723ed1908SDavid Hildenbrand if (folio_test_anon(folio)) { 1068b51ad4f8SDavid Hildenbrand /* 1069b51ad4f8SDavid Hildenbrand * If this page may have been pinned by the parent process, 1070b51ad4f8SDavid Hildenbrand * copy the page immediately for the child so that we'll always 1071b51ad4f8SDavid Hildenbrand * guarantee the pinned page won't be randomly replaced in the 1072b51ad4f8SDavid Hildenbrand * future. 1073b51ad4f8SDavid Hildenbrand */ 107408e7795eSDavid Hildenbrand if (unlikely(folio_try_dup_anon_rmap_pte(folio, page, src_vma))) { 1075fb3d824dSDavid Hildenbrand /* Page may be pinned, we have to copy. */ 107614ddee41SMatthew Wilcox (Oracle) folio_put(folio); 1077f8d93776SDavid Hildenbrand err = copy_present_page(dst_vma, src_vma, dst_pte, src_pte, 1078b51ad4f8SDavid Hildenbrand addr, rss, prealloc, page); 1079f8d93776SDavid Hildenbrand return err ? err : 1; 1080fb3d824dSDavid Hildenbrand } 1081edf50470SMatthew Wilcox (Oracle) rss[MM_ANONPAGES]++; 108223ed1908SDavid Hildenbrand VM_WARN_ON_FOLIO(PageAnonExclusive(page), folio); 108323ed1908SDavid Hildenbrand } else { 1084d8ef5e31SDavid Hildenbrand folio_dup_file_rmap_pte(folio, page); 10856b27cc6cSKefeng Wang rss[mm_counter_file(folio)]++; 108670e806e4SPeter Xu } 108770e806e4SPeter Xu 108823ed1908SDavid Hildenbrand copy_pte: 1089f8d93776SDavid Hildenbrand __copy_present_ptes(dst_vma, src_vma, dst_pte, src_pte, pte, addr, 1); 1090f8d93776SDavid Hildenbrand return 1; 10916aab341eSLinus Torvalds } 1092ae859762SHugh Dickins 1093294de6d8SKefeng Wang static inline struct folio *folio_prealloc(struct mm_struct *src_mm, 1094294de6d8SKefeng Wang struct vm_area_struct *vma, unsigned long addr, bool need_zero) 109570e806e4SPeter Xu { 1096edf50470SMatthew Wilcox (Oracle) struct folio *new_folio; 109770e806e4SPeter Xu 1098294de6d8SKefeng Wang if (need_zero) 1099294de6d8SKefeng Wang new_folio = vma_alloc_zeroed_movable_folio(vma, addr); 1100294de6d8SKefeng Wang else 1101294de6d8SKefeng Wang new_folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, vma, 1102294de6d8SKefeng Wang addr, false); 1103294de6d8SKefeng Wang 1104edf50470SMatthew Wilcox (Oracle) if (!new_folio) 110570e806e4SPeter Xu return NULL; 110670e806e4SPeter Xu 1107edf50470SMatthew Wilcox (Oracle) if (mem_cgroup_charge(new_folio, src_mm, GFP_KERNEL)) { 1108edf50470SMatthew Wilcox (Oracle) folio_put(new_folio); 110970e806e4SPeter Xu return NULL; 111070e806e4SPeter Xu } 1111e601ded4SKefeng Wang folio_throttle_swaprate(new_folio, GFP_KERNEL); 111270e806e4SPeter Xu 1113edf50470SMatthew Wilcox (Oracle) return new_folio; 11141da177e4SLinus Torvalds } 11151da177e4SLinus Torvalds 1116c78f4636SPeter Xu static int 1117c78f4636SPeter Xu copy_pte_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, 1118c78f4636SPeter Xu pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr, 1119c78f4636SPeter Xu unsigned long end) 11201da177e4SLinus Torvalds { 1121c78f4636SPeter Xu struct mm_struct *dst_mm = dst_vma->vm_mm; 1122c78f4636SPeter Xu struct mm_struct *src_mm = src_vma->vm_mm; 1123c36987e2SDaisuke Nishimura pte_t *orig_src_pte, *orig_dst_pte; 11241da177e4SLinus Torvalds pte_t *src_pte, *dst_pte; 1125c33c7948SRyan Roberts pte_t ptent; 1126c74df32cSHugh Dickins spinlock_t *src_ptl, *dst_ptl; 1127f8d93776SDavid Hildenbrand int progress, max_nr, ret = 0; 1128d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 1129570a335bSHugh Dickins swp_entry_t entry = (swp_entry_t){0}; 1130edf50470SMatthew Wilcox (Oracle) struct folio *prealloc = NULL; 1131f8d93776SDavid Hildenbrand int nr; 11321da177e4SLinus Torvalds 11331da177e4SLinus Torvalds again: 113470e806e4SPeter Xu progress = 0; 1135d559db08SKAMEZAWA Hiroyuki init_rss_vec(rss); 1136d559db08SKAMEZAWA Hiroyuki 11373db82b93SHugh Dickins /* 11383db82b93SHugh Dickins * copy_pmd_range()'s prior pmd_none_or_clear_bad(src_pmd), and the 11393db82b93SHugh Dickins * error handling here, assume that exclusive mmap_lock on dst and src 11403db82b93SHugh Dickins * protects anon from unexpected THP transitions; with shmem and file 11413db82b93SHugh Dickins * protected by mmap_lock-less collapse skipping areas with anon_vma 11423db82b93SHugh Dickins * (whereas vma_needs_copy() skips areas without anon_vma). A rework 11433db82b93SHugh Dickins * can remove such assumptions later, but this is good enough for now. 11443db82b93SHugh Dickins */ 1145c74df32cSHugh Dickins dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl); 114670e806e4SPeter Xu if (!dst_pte) { 114770e806e4SPeter Xu ret = -ENOMEM; 114870e806e4SPeter Xu goto out; 114970e806e4SPeter Xu } 11503db82b93SHugh Dickins src_pte = pte_offset_map_nolock(src_mm, src_pmd, addr, &src_ptl); 11513db82b93SHugh Dickins if (!src_pte) { 11523db82b93SHugh Dickins pte_unmap_unlock(dst_pte, dst_ptl); 11533db82b93SHugh Dickins /* ret == 0 */ 11543db82b93SHugh Dickins goto out; 11553db82b93SHugh Dickins } 1156f20dc5f7SIngo Molnar spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 1157c36987e2SDaisuke Nishimura orig_src_pte = src_pte; 1158c36987e2SDaisuke Nishimura orig_dst_pte = dst_pte; 11596606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 11601da177e4SLinus Torvalds 11611da177e4SLinus Torvalds do { 1162f8d93776SDavid Hildenbrand nr = 1; 1163f8d93776SDavid Hildenbrand 11641da177e4SLinus Torvalds /* 11651da177e4SLinus Torvalds * We are holding two locks at this point - either of them 11661da177e4SLinus Torvalds * could generate latencies in another task on another CPU. 11671da177e4SLinus Torvalds */ 1168e040f218SHugh Dickins if (progress >= 32) { 1169e040f218SHugh Dickins progress = 0; 1170e040f218SHugh Dickins if (need_resched() || 117195c354feSNick Piggin spin_needbreak(src_ptl) || spin_needbreak(dst_ptl)) 11721da177e4SLinus Torvalds break; 1173e040f218SHugh Dickins } 1174c33c7948SRyan Roberts ptent = ptep_get(src_pte); 1175c33c7948SRyan Roberts if (pte_none(ptent)) { 11761da177e4SLinus Torvalds progress++; 11771da177e4SLinus Torvalds continue; 11781da177e4SLinus Torvalds } 1179c33c7948SRyan Roberts if (unlikely(!pte_present(ptent))) { 11809a5cc85cSAlistair Popple ret = copy_nonpresent_pte(dst_mm, src_mm, 118179a1971cSLinus Torvalds dst_pte, src_pte, 11828f34f1eaSPeter Xu dst_vma, src_vma, 11838f34f1eaSPeter Xu addr, rss); 11849a5cc85cSAlistair Popple if (ret == -EIO) { 1185c33c7948SRyan Roberts entry = pte_to_swp_entry(ptep_get(src_pte)); 1186570a335bSHugh Dickins break; 1187b756a3b5SAlistair Popple } else if (ret == -EBUSY) { 1188b756a3b5SAlistair Popple break; 1189b756a3b5SAlistair Popple } else if (!ret) { 11901da177e4SLinus Torvalds progress += 8; 119179a1971cSLinus Torvalds continue; 119279a1971cSLinus Torvalds } 119353723298SDavid Hildenbrand ptent = ptep_get(src_pte); 119453723298SDavid Hildenbrand VM_WARN_ON_ONCE(!pte_present(ptent)); 1195b756a3b5SAlistair Popple 1196b756a3b5SAlistair Popple /* 1197b756a3b5SAlistair Popple * Device exclusive entry restored, continue by copying 1198b756a3b5SAlistair Popple * the now present pte. 1199b756a3b5SAlistair Popple */ 1200b756a3b5SAlistair Popple WARN_ON_ONCE(ret != -ENOENT); 1201b756a3b5SAlistair Popple } 1202f8d93776SDavid Hildenbrand /* copy_present_ptes() will clear `*prealloc' if consumed */ 1203f8d93776SDavid Hildenbrand max_nr = (end - addr) / PAGE_SIZE; 1204f8d93776SDavid Hildenbrand ret = copy_present_ptes(dst_vma, src_vma, dst_pte, src_pte, 1205f8d93776SDavid Hildenbrand ptent, addr, max_nr, rss, &prealloc); 120670e806e4SPeter Xu /* 120770e806e4SPeter Xu * If we need a pre-allocated page for this pte, drop the 120870e806e4SPeter Xu * locks, allocate, and try again. 120970e806e4SPeter Xu */ 121070e806e4SPeter Xu if (unlikely(ret == -EAGAIN)) 121170e806e4SPeter Xu break; 121270e806e4SPeter Xu if (unlikely(prealloc)) { 121370e806e4SPeter Xu /* 121470e806e4SPeter Xu * pre-alloc page cannot be reused by next time so as 121570e806e4SPeter Xu * to strictly follow mempolicy (e.g., alloc_page_vma() 121670e806e4SPeter Xu * will allocate page according to address). This 121770e806e4SPeter Xu * could only happen if one pinned pte changed. 121870e806e4SPeter Xu */ 1219edf50470SMatthew Wilcox (Oracle) folio_put(prealloc); 122070e806e4SPeter Xu prealloc = NULL; 122170e806e4SPeter Xu } 1222f8d93776SDavid Hildenbrand nr = ret; 1223f8d93776SDavid Hildenbrand progress += 8 * nr; 1224f8d93776SDavid Hildenbrand } while (dst_pte += nr, src_pte += nr, addr += PAGE_SIZE * nr, 1225f8d93776SDavid Hildenbrand addr != end); 12261da177e4SLinus Torvalds 12276606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 12283db82b93SHugh Dickins pte_unmap_unlock(orig_src_pte, src_ptl); 1229d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(dst_mm, rss); 1230c36987e2SDaisuke Nishimura pte_unmap_unlock(orig_dst_pte, dst_ptl); 1231c74df32cSHugh Dickins cond_resched(); 1232570a335bSHugh Dickins 12339a5cc85cSAlistair Popple if (ret == -EIO) { 12349a5cc85cSAlistair Popple VM_WARN_ON_ONCE(!entry.val); 123570e806e4SPeter Xu if (add_swap_count_continuation(entry, GFP_KERNEL) < 0) { 123670e806e4SPeter Xu ret = -ENOMEM; 123770e806e4SPeter Xu goto out; 123870e806e4SPeter Xu } 123970e806e4SPeter Xu entry.val = 0; 1240b756a3b5SAlistair Popple } else if (ret == -EBUSY) { 1241b756a3b5SAlistair Popple goto out; 12429a5cc85cSAlistair Popple } else if (ret == -EAGAIN) { 1243294de6d8SKefeng Wang prealloc = folio_prealloc(src_mm, src_vma, addr, false); 124470e806e4SPeter Xu if (!prealloc) 1245570a335bSHugh Dickins return -ENOMEM; 1246f8d93776SDavid Hildenbrand } else if (ret < 0) { 12479a5cc85cSAlistair Popple VM_WARN_ON_ONCE(1); 12489a5cc85cSAlistair Popple } 12499a5cc85cSAlistair Popple 125070e806e4SPeter Xu /* We've captured and resolved the error. Reset, try again. */ 125170e806e4SPeter Xu ret = 0; 12529a5cc85cSAlistair Popple 12531da177e4SLinus Torvalds if (addr != end) 12541da177e4SLinus Torvalds goto again; 125570e806e4SPeter Xu out: 125670e806e4SPeter Xu if (unlikely(prealloc)) 1257edf50470SMatthew Wilcox (Oracle) folio_put(prealloc); 125870e806e4SPeter Xu return ret; 12591da177e4SLinus Torvalds } 12601da177e4SLinus Torvalds 1261c78f4636SPeter Xu static inline int 1262c78f4636SPeter Xu copy_pmd_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, 1263c78f4636SPeter Xu pud_t *dst_pud, pud_t *src_pud, unsigned long addr, 1264c78f4636SPeter Xu unsigned long end) 12651da177e4SLinus Torvalds { 1266c78f4636SPeter Xu struct mm_struct *dst_mm = dst_vma->vm_mm; 1267c78f4636SPeter Xu struct mm_struct *src_mm = src_vma->vm_mm; 12681da177e4SLinus Torvalds pmd_t *src_pmd, *dst_pmd; 12691da177e4SLinus Torvalds unsigned long next; 12701da177e4SLinus Torvalds 12711da177e4SLinus Torvalds dst_pmd = pmd_alloc(dst_mm, dst_pud, addr); 12721da177e4SLinus Torvalds if (!dst_pmd) 12731da177e4SLinus Torvalds return -ENOMEM; 12741da177e4SLinus Torvalds src_pmd = pmd_offset(src_pud, addr); 12751da177e4SLinus Torvalds do { 12761da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 127784c3fc4eSZi Yan if (is_swap_pmd(*src_pmd) || pmd_trans_huge(*src_pmd) 127884c3fc4eSZi Yan || pmd_devmap(*src_pmd)) { 127971e3aac0SAndrea Arcangeli int err; 1280c78f4636SPeter Xu VM_BUG_ON_VMA(next-addr != HPAGE_PMD_SIZE, src_vma); 12818f34f1eaSPeter Xu err = copy_huge_pmd(dst_mm, src_mm, dst_pmd, src_pmd, 12828f34f1eaSPeter Xu addr, dst_vma, src_vma); 128371e3aac0SAndrea Arcangeli if (err == -ENOMEM) 128471e3aac0SAndrea Arcangeli return -ENOMEM; 128571e3aac0SAndrea Arcangeli if (!err) 128671e3aac0SAndrea Arcangeli continue; 128771e3aac0SAndrea Arcangeli /* fall through */ 128871e3aac0SAndrea Arcangeli } 12891da177e4SLinus Torvalds if (pmd_none_or_clear_bad(src_pmd)) 12901da177e4SLinus Torvalds continue; 1291c78f4636SPeter Xu if (copy_pte_range(dst_vma, src_vma, dst_pmd, src_pmd, 1292c78f4636SPeter Xu addr, next)) 12931da177e4SLinus Torvalds return -ENOMEM; 12941da177e4SLinus Torvalds } while (dst_pmd++, src_pmd++, addr = next, addr != end); 12951da177e4SLinus Torvalds return 0; 12961da177e4SLinus Torvalds } 12971da177e4SLinus Torvalds 1298c78f4636SPeter Xu static inline int 1299c78f4636SPeter Xu copy_pud_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, 1300c78f4636SPeter Xu p4d_t *dst_p4d, p4d_t *src_p4d, unsigned long addr, 1301c78f4636SPeter Xu unsigned long end) 13021da177e4SLinus Torvalds { 1303c78f4636SPeter Xu struct mm_struct *dst_mm = dst_vma->vm_mm; 1304c78f4636SPeter Xu struct mm_struct *src_mm = src_vma->vm_mm; 13051da177e4SLinus Torvalds pud_t *src_pud, *dst_pud; 13061da177e4SLinus Torvalds unsigned long next; 13071da177e4SLinus Torvalds 1308c2febafcSKirill A. Shutemov dst_pud = pud_alloc(dst_mm, dst_p4d, addr); 13091da177e4SLinus Torvalds if (!dst_pud) 13101da177e4SLinus Torvalds return -ENOMEM; 1311c2febafcSKirill A. Shutemov src_pud = pud_offset(src_p4d, addr); 13121da177e4SLinus Torvalds do { 13131da177e4SLinus Torvalds next = pud_addr_end(addr, end); 1314a00cc7d9SMatthew Wilcox if (pud_trans_huge(*src_pud) || pud_devmap(*src_pud)) { 1315a00cc7d9SMatthew Wilcox int err; 1316a00cc7d9SMatthew Wilcox 1317c78f4636SPeter Xu VM_BUG_ON_VMA(next-addr != HPAGE_PUD_SIZE, src_vma); 1318a00cc7d9SMatthew Wilcox err = copy_huge_pud(dst_mm, src_mm, 1319c78f4636SPeter Xu dst_pud, src_pud, addr, src_vma); 1320a00cc7d9SMatthew Wilcox if (err == -ENOMEM) 1321a00cc7d9SMatthew Wilcox return -ENOMEM; 1322a00cc7d9SMatthew Wilcox if (!err) 1323a00cc7d9SMatthew Wilcox continue; 1324a00cc7d9SMatthew Wilcox /* fall through */ 1325a00cc7d9SMatthew Wilcox } 13261da177e4SLinus Torvalds if (pud_none_or_clear_bad(src_pud)) 13271da177e4SLinus Torvalds continue; 1328c78f4636SPeter Xu if (copy_pmd_range(dst_vma, src_vma, dst_pud, src_pud, 1329c78f4636SPeter Xu addr, next)) 13301da177e4SLinus Torvalds return -ENOMEM; 13311da177e4SLinus Torvalds } while (dst_pud++, src_pud++, addr = next, addr != end); 13321da177e4SLinus Torvalds return 0; 13331da177e4SLinus Torvalds } 13341da177e4SLinus Torvalds 1335c78f4636SPeter Xu static inline int 1336c78f4636SPeter Xu copy_p4d_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, 1337c78f4636SPeter Xu pgd_t *dst_pgd, pgd_t *src_pgd, unsigned long addr, 1338c78f4636SPeter Xu unsigned long end) 1339c2febafcSKirill A. Shutemov { 1340c78f4636SPeter Xu struct mm_struct *dst_mm = dst_vma->vm_mm; 1341c2febafcSKirill A. Shutemov p4d_t *src_p4d, *dst_p4d; 1342c2febafcSKirill A. Shutemov unsigned long next; 1343c2febafcSKirill A. Shutemov 1344c2febafcSKirill A. Shutemov dst_p4d = p4d_alloc(dst_mm, dst_pgd, addr); 1345c2febafcSKirill A. Shutemov if (!dst_p4d) 1346c2febafcSKirill A. Shutemov return -ENOMEM; 1347c2febafcSKirill A. Shutemov src_p4d = p4d_offset(src_pgd, addr); 1348c2febafcSKirill A. Shutemov do { 1349c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 1350c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(src_p4d)) 1351c2febafcSKirill A. Shutemov continue; 1352c78f4636SPeter Xu if (copy_pud_range(dst_vma, src_vma, dst_p4d, src_p4d, 1353c78f4636SPeter Xu addr, next)) 1354c2febafcSKirill A. Shutemov return -ENOMEM; 1355c2febafcSKirill A. Shutemov } while (dst_p4d++, src_p4d++, addr = next, addr != end); 1356c2febafcSKirill A. Shutemov return 0; 1357c2febafcSKirill A. Shutemov } 1358c2febafcSKirill A. Shutemov 1359c56d1b62SPeter Xu /* 1360c56d1b62SPeter Xu * Return true if the vma needs to copy the pgtable during this fork(). Return 1361c56d1b62SPeter Xu * false when we can speed up fork() by allowing lazy page faults later until 1362c56d1b62SPeter Xu * when the child accesses the memory range. 1363c56d1b62SPeter Xu */ 1364bc70fbf2SPeter Xu static bool 1365c56d1b62SPeter Xu vma_needs_copy(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) 1366c56d1b62SPeter Xu { 1367c56d1b62SPeter Xu /* 1368c56d1b62SPeter Xu * Always copy pgtables when dst_vma has uffd-wp enabled even if it's 1369c56d1b62SPeter Xu * file-backed (e.g. shmem). Because when uffd-wp is enabled, pgtable 1370c56d1b62SPeter Xu * contains uffd-wp protection information, that's something we can't 1371c56d1b62SPeter Xu * retrieve from page cache, and skip copying will lose those info. 1372c56d1b62SPeter Xu */ 1373c56d1b62SPeter Xu if (userfaultfd_wp(dst_vma)) 1374c56d1b62SPeter Xu return true; 1375c56d1b62SPeter Xu 1376bcd51a3cSMike Kravetz if (src_vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)) 1377c56d1b62SPeter Xu return true; 1378c56d1b62SPeter Xu 1379c56d1b62SPeter Xu if (src_vma->anon_vma) 1380c56d1b62SPeter Xu return true; 1381c56d1b62SPeter Xu 1382c56d1b62SPeter Xu /* 1383c56d1b62SPeter Xu * Don't copy ptes where a page fault will fill them correctly. Fork 1384c56d1b62SPeter Xu * becomes much lighter when there are big shared or private readonly 1385c56d1b62SPeter Xu * mappings. The tradeoff is that copy_page_range is more efficient 1386c56d1b62SPeter Xu * than faulting. 1387c56d1b62SPeter Xu */ 1388c56d1b62SPeter Xu return false; 1389c56d1b62SPeter Xu } 1390c56d1b62SPeter Xu 1391c78f4636SPeter Xu int 1392c78f4636SPeter Xu copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) 13931da177e4SLinus Torvalds { 13941da177e4SLinus Torvalds pgd_t *src_pgd, *dst_pgd; 13951da177e4SLinus Torvalds unsigned long next; 1396c78f4636SPeter Xu unsigned long addr = src_vma->vm_start; 1397c78f4636SPeter Xu unsigned long end = src_vma->vm_end; 1398c78f4636SPeter Xu struct mm_struct *dst_mm = dst_vma->vm_mm; 1399c78f4636SPeter Xu struct mm_struct *src_mm = src_vma->vm_mm; 1400ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 14012ec74c3eSSagi Grimberg bool is_cow; 1402cddb8a5cSAndrea Arcangeli int ret; 14031da177e4SLinus Torvalds 1404c56d1b62SPeter Xu if (!vma_needs_copy(dst_vma, src_vma)) 1405d992895bSNick Piggin return 0; 1406d992895bSNick Piggin 1407c78f4636SPeter Xu if (is_vm_hugetlb_page(src_vma)) 1408bc70fbf2SPeter Xu return copy_hugetlb_page_range(dst_mm, src_mm, dst_vma, src_vma); 14091da177e4SLinus Torvalds 1410c78f4636SPeter Xu if (unlikely(src_vma->vm_flags & VM_PFNMAP)) { 14112ab64037Svenkatesh.pallipadi@intel.com /* 14122ab64037Svenkatesh.pallipadi@intel.com * We do not free on error cases below as remove_vma 14132ab64037Svenkatesh.pallipadi@intel.com * gets called on error from higher level routine 14142ab64037Svenkatesh.pallipadi@intel.com */ 1415c78f4636SPeter Xu ret = track_pfn_copy(src_vma); 14162ab64037Svenkatesh.pallipadi@intel.com if (ret) 14172ab64037Svenkatesh.pallipadi@intel.com return ret; 14182ab64037Svenkatesh.pallipadi@intel.com } 14192ab64037Svenkatesh.pallipadi@intel.com 1420cddb8a5cSAndrea Arcangeli /* 1421cddb8a5cSAndrea Arcangeli * We need to invalidate the secondary MMU mappings only when 1422cddb8a5cSAndrea Arcangeli * there could be a permission downgrade on the ptes of the 1423cddb8a5cSAndrea Arcangeli * parent mm. And a permission downgrade will only happen if 1424cddb8a5cSAndrea Arcangeli * is_cow_mapping() returns true. 1425cddb8a5cSAndrea Arcangeli */ 1426c78f4636SPeter Xu is_cow = is_cow_mapping(src_vma->vm_flags); 1427ac46d4f3SJérôme Glisse 1428ac46d4f3SJérôme Glisse if (is_cow) { 14297269f999SJérôme Glisse mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_PAGE, 14307d4a8be0SAlistair Popple 0, src_mm, addr, end); 1431ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 143257efa1feSJason Gunthorpe /* 143357efa1feSJason Gunthorpe * Disabling preemption is not needed for the write side, as 143457efa1feSJason Gunthorpe * the read side doesn't spin, but goes to the mmap_lock. 143557efa1feSJason Gunthorpe * 143657efa1feSJason Gunthorpe * Use the raw variant of the seqcount_t write API to avoid 143757efa1feSJason Gunthorpe * lockdep complaining about preemptibility. 143857efa1feSJason Gunthorpe */ 1439e727bfd5SSuren Baghdasaryan vma_assert_write_locked(src_vma); 144057efa1feSJason Gunthorpe raw_write_seqcount_begin(&src_mm->write_protect_seq); 1441ac46d4f3SJérôme Glisse } 1442cddb8a5cSAndrea Arcangeli 1443cddb8a5cSAndrea Arcangeli ret = 0; 14441da177e4SLinus Torvalds dst_pgd = pgd_offset(dst_mm, addr); 14451da177e4SLinus Torvalds src_pgd = pgd_offset(src_mm, addr); 14461da177e4SLinus Torvalds do { 14471da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 14481da177e4SLinus Torvalds if (pgd_none_or_clear_bad(src_pgd)) 14491da177e4SLinus Torvalds continue; 1450c78f4636SPeter Xu if (unlikely(copy_p4d_range(dst_vma, src_vma, dst_pgd, src_pgd, 1451c78f4636SPeter Xu addr, next))) { 1452d155df53SMa Wupeng untrack_pfn_clear(dst_vma); 1453cddb8a5cSAndrea Arcangeli ret = -ENOMEM; 1454cddb8a5cSAndrea Arcangeli break; 1455cddb8a5cSAndrea Arcangeli } 14561da177e4SLinus Torvalds } while (dst_pgd++, src_pgd++, addr = next, addr != end); 1457cddb8a5cSAndrea Arcangeli 145857efa1feSJason Gunthorpe if (is_cow) { 145957efa1feSJason Gunthorpe raw_write_seqcount_end(&src_mm->write_protect_seq); 1460ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 146157efa1feSJason Gunthorpe } 1462cddb8a5cSAndrea Arcangeli return ret; 14631da177e4SLinus Torvalds } 14641da177e4SLinus Torvalds 14655abfd71dSPeter Xu /* Whether we should zap all COWed (private) pages too */ 14665abfd71dSPeter Xu static inline bool should_zap_cows(struct zap_details *details) 14675abfd71dSPeter Xu { 14685abfd71dSPeter Xu /* By default, zap all pages */ 14695abfd71dSPeter Xu if (!details) 14705abfd71dSPeter Xu return true; 14715abfd71dSPeter Xu 14725abfd71dSPeter Xu /* Or, we zap COWed pages only if the caller wants to */ 14732e148f1eSPeter Xu return details->even_cows; 14745abfd71dSPeter Xu } 14755abfd71dSPeter Xu 1476eabafaaaSKefeng Wang /* Decides whether we should zap this folio with the folio pointer specified */ 1477eabafaaaSKefeng Wang static inline bool should_zap_folio(struct zap_details *details, 1478eabafaaaSKefeng Wang struct folio *folio) 14793506659eSMatthew Wilcox (Oracle) { 1480eabafaaaSKefeng Wang /* If we can make a decision without *folio.. */ 14815abfd71dSPeter Xu if (should_zap_cows(details)) 1482254ab940SPeter Xu return true; 14833506659eSMatthew Wilcox (Oracle) 1484eabafaaaSKefeng Wang /* E.g. the caller passes NULL for the case of a zero folio */ 1485eabafaaaSKefeng Wang if (!folio) 1486254ab940SPeter Xu return true; 14875abfd71dSPeter Xu 1488eabafaaaSKefeng Wang /* Otherwise we should only zap non-anon folios */ 1489eabafaaaSKefeng Wang return !folio_test_anon(folio); 14903506659eSMatthew Wilcox (Oracle) } 14913506659eSMatthew Wilcox (Oracle) 1492999dad82SPeter Xu static inline bool zap_drop_file_uffd_wp(struct zap_details *details) 1493999dad82SPeter Xu { 1494999dad82SPeter Xu if (!details) 1495999dad82SPeter Xu return false; 1496999dad82SPeter Xu 1497999dad82SPeter Xu return details->zap_flags & ZAP_FLAG_DROP_MARKER; 1498999dad82SPeter Xu } 1499999dad82SPeter Xu 1500999dad82SPeter Xu /* 1501999dad82SPeter Xu * This function makes sure that we'll replace the none pte with an uffd-wp 1502999dad82SPeter Xu * swap special pte marker when necessary. Must be with the pgtable lock held. 1503999dad82SPeter Xu */ 1504999dad82SPeter Xu static inline void 1505999dad82SPeter Xu zap_install_uffd_wp_if_needed(struct vm_area_struct *vma, 1506999dad82SPeter Xu unsigned long addr, pte_t *pte, 1507999dad82SPeter Xu struct zap_details *details, pte_t pteval) 1508999dad82SPeter Xu { 15092bad466cSPeter Xu /* Zap on anonymous always means dropping everything */ 15102bad466cSPeter Xu if (vma_is_anonymous(vma)) 15112bad466cSPeter Xu return; 15122bad466cSPeter Xu 1513999dad82SPeter Xu if (zap_drop_file_uffd_wp(details)) 1514999dad82SPeter Xu return; 1515999dad82SPeter Xu 1516999dad82SPeter Xu pte_install_uffd_wp_if_needed(vma, addr, pte, pteval); 1517999dad82SPeter Xu } 1518999dad82SPeter Xu 151951c6f666SRobin Holt static unsigned long zap_pte_range(struct mmu_gather *tlb, 1520b5810039SNick Piggin struct vm_area_struct *vma, pmd_t *pmd, 15211da177e4SLinus Torvalds unsigned long addr, unsigned long end, 152297a89413SPeter Zijlstra struct zap_details *details) 15231da177e4SLinus Torvalds { 1524b5810039SNick Piggin struct mm_struct *mm = tlb->mm; 1525d16dfc55SPeter Zijlstra int force_flush = 0; 1526d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 152797a89413SPeter Zijlstra spinlock_t *ptl; 15285f1a1907SSteven Rostedt pte_t *start_pte; 152997a89413SPeter Zijlstra pte_t *pte; 15308a5f14a2SKirill A. Shutemov swp_entry_t entry; 1531d559db08SKAMEZAWA Hiroyuki 1532ed6a7935SPeter Zijlstra tlb_change_page_size(tlb, PAGE_SIZE); 1533e303297eSPeter Zijlstra init_rss_vec(rss); 15343db82b93SHugh Dickins start_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl); 15353db82b93SHugh Dickins if (!pte) 15363db82b93SHugh Dickins return addr; 15373db82b93SHugh Dickins 15383ea27719SMel Gorman flush_tlb_batched_pending(mm); 15396606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 15401da177e4SLinus Torvalds do { 1541c33c7948SRyan Roberts pte_t ptent = ptep_get(pte); 1542eabafaaaSKefeng Wang struct folio *folio = NULL; 15438018db85SPeter Xu struct page *page; 15448018db85SPeter Xu 1545166f61b9STobin C Harding if (pte_none(ptent)) 15461da177e4SLinus Torvalds continue; 154751c6f666SRobin Holt 15487b167b68SMinchan Kim if (need_resched()) 15497b167b68SMinchan Kim break; 15507b167b68SMinchan Kim 15516f5e6b9eSHugh Dickins if (pte_present(ptent)) { 15525df397deSLinus Torvalds unsigned int delay_rmap; 15535df397deSLinus Torvalds 155425b2995aSChristoph Hellwig page = vm_normal_page(vma, addr, ptent); 1555eabafaaaSKefeng Wang if (page) 1556eabafaaaSKefeng Wang folio = page_folio(page); 1557eabafaaaSKefeng Wang 1558eabafaaaSKefeng Wang if (unlikely(!should_zap_folio(details, folio))) 15591da177e4SLinus Torvalds continue; 1560b5810039SNick Piggin ptent = ptep_get_and_clear_full(mm, addr, pte, 1561a600388dSZachary Amsden tlb->fullmm); 1562e5136e87SRick Edgecombe arch_check_zapped_pte(vma, ptent); 15631da177e4SLinus Torvalds tlb_remove_tlb_entry(tlb, pte, addr); 1564999dad82SPeter Xu zap_install_uffd_wp_if_needed(vma, addr, pte, details, 1565999dad82SPeter Xu ptent); 1566e2942062Sxu xin if (unlikely(!page)) { 15676080d19fSxu xin ksm_might_unmap_zero_page(mm, ptent); 15681da177e4SLinus Torvalds continue; 1569e2942062Sxu xin } 1570eca56ff9SJerome Marchand 15715df397deSLinus Torvalds delay_rmap = 0; 1572c4626503SDavid Hildenbrand if (!folio_test_anon(folio)) { 15731cf35d47SLinus Torvalds if (pte_dirty(ptent)) { 1574e4e3df29SDavid Hildenbrand folio_mark_dirty(folio); 15755df397deSLinus Torvalds if (tlb_delay_rmap(tlb)) { 15765df397deSLinus Torvalds delay_rmap = 1; 15775df397deSLinus Torvalds force_flush = 1; 15785df397deSLinus Torvalds } 15791cf35d47SLinus Torvalds } 15808788f678SYu Zhao if (pte_young(ptent) && likely(vma_has_recency(vma))) 1581c4626503SDavid Hildenbrand folio_mark_accessed(folio); 15826237bcd9SHugh Dickins } 1583a23f517bSKefeng Wang rss[mm_counter(folio)]--; 15845df397deSLinus Torvalds if (!delay_rmap) { 1585c4626503SDavid Hildenbrand folio_remove_rmap_pte(folio, page, vma); 15863dc14741SHugh Dickins if (unlikely(page_mapcount(page) < 0)) 15873dc14741SHugh Dickins print_bad_pte(vma, addr, ptent, page); 15885df397deSLinus Torvalds } 15895df397deSLinus Torvalds if (unlikely(__tlb_remove_page(tlb, page, delay_rmap))) { 15901cf35d47SLinus Torvalds force_flush = 1; 1591ce9ec37bSWill Deacon addr += PAGE_SIZE; 1592d16dfc55SPeter Zijlstra break; 15931cf35d47SLinus Torvalds } 15941da177e4SLinus Torvalds continue; 15951da177e4SLinus Torvalds } 15965042db43SJérôme Glisse 15975042db43SJérôme Glisse entry = pte_to_swp_entry(ptent); 1598b756a3b5SAlistair Popple if (is_device_private_entry(entry) || 1599b756a3b5SAlistair Popple is_device_exclusive_entry(entry)) { 16008018db85SPeter Xu page = pfn_swap_entry_to_page(entry); 1601c4626503SDavid Hildenbrand folio = page_folio(page); 1602eabafaaaSKefeng Wang if (unlikely(!should_zap_folio(details, folio))) 16035042db43SJérôme Glisse continue; 1604999dad82SPeter Xu /* 1605999dad82SPeter Xu * Both device private/exclusive mappings should only 1606999dad82SPeter Xu * work with anonymous page so far, so we don't need to 1607999dad82SPeter Xu * consider uffd-wp bit when zap. For more information, 1608999dad82SPeter Xu * see zap_install_uffd_wp_if_needed(). 1609999dad82SPeter Xu */ 1610999dad82SPeter Xu WARN_ON_ONCE(!vma_is_anonymous(vma)); 1611a23f517bSKefeng Wang rss[mm_counter(folio)]--; 1612b756a3b5SAlistair Popple if (is_device_private_entry(entry)) 1613c4626503SDavid Hildenbrand folio_remove_rmap_pte(folio, page, vma); 1614c4626503SDavid Hildenbrand folio_put(folio); 16158018db85SPeter Xu } else if (!non_swap_entry(entry)) { 16165abfd71dSPeter Xu /* Genuine swap entry, hence a private anon page */ 16175abfd71dSPeter Xu if (!should_zap_cows(details)) 16181da177e4SLinus Torvalds continue; 1619b084d435SKAMEZAWA Hiroyuki rss[MM_SWAPENTS]--; 16208018db85SPeter Xu if (unlikely(!free_swap_and_cache(entry))) 16218018db85SPeter Xu print_bad_pte(vma, addr, ptent, NULL); 16225abfd71dSPeter Xu } else if (is_migration_entry(entry)) { 1623eabafaaaSKefeng Wang folio = pfn_swap_entry_folio(entry); 1624eabafaaaSKefeng Wang if (!should_zap_folio(details, folio)) 16255abfd71dSPeter Xu continue; 1626a23f517bSKefeng Wang rss[mm_counter(folio)]--; 1627999dad82SPeter Xu } else if (pte_marker_entry_uffd_wp(entry)) { 16282bad466cSPeter Xu /* 16292bad466cSPeter Xu * For anon: always drop the marker; for file: only 16302bad466cSPeter Xu * drop the marker if explicitly requested. 16312bad466cSPeter Xu */ 16322bad466cSPeter Xu if (!vma_is_anonymous(vma) && 16332bad466cSPeter Xu !zap_drop_file_uffd_wp(details)) 1634999dad82SPeter Xu continue; 16359f186f9eSMiaohe Lin } else if (is_hwpoison_entry(entry) || 1636af19487fSAxel Rasmussen is_poisoned_swp_entry(entry)) { 16375abfd71dSPeter Xu if (!should_zap_cows(details)) 16385abfd71dSPeter Xu continue; 16395abfd71dSPeter Xu } else { 16405abfd71dSPeter Xu /* We should have covered all the swap entry types */ 1641727d16f1SAndrew Morton pr_alert("unrecognized swap entry 0x%lx\n", entry.val); 16425abfd71dSPeter Xu WARN_ON_ONCE(1); 16439f9f1acdSKonstantin Khlebnikov } 16449888a1caSZachary Amsden pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); 1645999dad82SPeter Xu zap_install_uffd_wp_if_needed(vma, addr, pte, details, ptent); 164697a89413SPeter Zijlstra } while (pte++, addr += PAGE_SIZE, addr != end); 1647ae859762SHugh Dickins 1648d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(mm, rss); 16496606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 165051c6f666SRobin Holt 16511cf35d47SLinus Torvalds /* Do the actual TLB flush before dropping ptl */ 16525df397deSLinus Torvalds if (force_flush) { 16531cf35d47SLinus Torvalds tlb_flush_mmu_tlbonly(tlb); 16545df397deSLinus Torvalds tlb_flush_rmaps(tlb, vma); 16555df397deSLinus Torvalds } 16561cf35d47SLinus Torvalds pte_unmap_unlock(start_pte, ptl); 16571cf35d47SLinus Torvalds 16581cf35d47SLinus Torvalds /* 16591cf35d47SLinus Torvalds * If we forced a TLB flush (either due to running out of 16601cf35d47SLinus Torvalds * batch buffers or because we needed to flush dirty TLB 16611cf35d47SLinus Torvalds * entries before releasing the ptl), free the batched 16623db82b93SHugh Dickins * memory too. Come back again if we didn't do everything. 16631cf35d47SLinus Torvalds */ 16643db82b93SHugh Dickins if (force_flush) 1665fa0aafb8SPeter Zijlstra tlb_flush_mmu(tlb); 1666d16dfc55SPeter Zijlstra 166751c6f666SRobin Holt return addr; 16681da177e4SLinus Torvalds } 16691da177e4SLinus Torvalds 167051c6f666SRobin Holt static inline unsigned long zap_pmd_range(struct mmu_gather *tlb, 1671b5810039SNick Piggin struct vm_area_struct *vma, pud_t *pud, 16721da177e4SLinus Torvalds unsigned long addr, unsigned long end, 167397a89413SPeter Zijlstra struct zap_details *details) 16741da177e4SLinus Torvalds { 16751da177e4SLinus Torvalds pmd_t *pmd; 16761da177e4SLinus Torvalds unsigned long next; 16771da177e4SLinus Torvalds 16781da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 16791da177e4SLinus Torvalds do { 16801da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 168184c3fc4eSZi Yan if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) { 168253406ed1SHugh Dickins if (next - addr != HPAGE_PMD_SIZE) 1683fd60775aSDavid Rientjes __split_huge_pmd(vma, pmd, addr, false, NULL); 16843db82b93SHugh Dickins else if (zap_huge_pmd(tlb, vma, pmd, addr)) { 16853db82b93SHugh Dickins addr = next; 16863db82b93SHugh Dickins continue; 16873db82b93SHugh Dickins } 168871e3aac0SAndrea Arcangeli /* fall through */ 16893506659eSMatthew Wilcox (Oracle) } else if (details && details->single_folio && 16903506659eSMatthew Wilcox (Oracle) folio_test_pmd_mappable(details->single_folio) && 169122061a1fSHugh Dickins next - addr == HPAGE_PMD_SIZE && pmd_none(*pmd)) { 169222061a1fSHugh Dickins spinlock_t *ptl = pmd_lock(tlb->mm, pmd); 169322061a1fSHugh Dickins /* 169422061a1fSHugh Dickins * Take and drop THP pmd lock so that we cannot return 169522061a1fSHugh Dickins * prematurely, while zap_huge_pmd() has cleared *pmd, 169622061a1fSHugh Dickins * but not yet decremented compound_mapcount(). 169722061a1fSHugh Dickins */ 169822061a1fSHugh Dickins spin_unlock(ptl); 169971e3aac0SAndrea Arcangeli } 17003db82b93SHugh Dickins if (pmd_none(*pmd)) { 17013db82b93SHugh Dickins addr = next; 17023db82b93SHugh Dickins continue; 17033db82b93SHugh Dickins } 17043db82b93SHugh Dickins addr = zap_pte_range(tlb, vma, pmd, addr, next, details); 17053db82b93SHugh Dickins if (addr != next) 17063db82b93SHugh Dickins pmd--; 17073db82b93SHugh Dickins } while (pmd++, cond_resched(), addr != end); 170851c6f666SRobin Holt 170951c6f666SRobin Holt return addr; 17101da177e4SLinus Torvalds } 17111da177e4SLinus Torvalds 171251c6f666SRobin Holt static inline unsigned long zap_pud_range(struct mmu_gather *tlb, 1713c2febafcSKirill A. Shutemov struct vm_area_struct *vma, p4d_t *p4d, 17141da177e4SLinus Torvalds unsigned long addr, unsigned long end, 171597a89413SPeter Zijlstra struct zap_details *details) 17161da177e4SLinus Torvalds { 17171da177e4SLinus Torvalds pud_t *pud; 17181da177e4SLinus Torvalds unsigned long next; 17191da177e4SLinus Torvalds 1720c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 17211da177e4SLinus Torvalds do { 17221da177e4SLinus Torvalds next = pud_addr_end(addr, end); 1723a00cc7d9SMatthew Wilcox if (pud_trans_huge(*pud) || pud_devmap(*pud)) { 1724a00cc7d9SMatthew Wilcox if (next - addr != HPAGE_PUD_SIZE) { 172542fc5414SMichel Lespinasse mmap_assert_locked(tlb->mm); 1726a00cc7d9SMatthew Wilcox split_huge_pud(vma, pud, addr); 1727a00cc7d9SMatthew Wilcox } else if (zap_huge_pud(tlb, vma, pud, addr)) 1728a00cc7d9SMatthew Wilcox goto next; 1729a00cc7d9SMatthew Wilcox /* fall through */ 1730a00cc7d9SMatthew Wilcox } 173197a89413SPeter Zijlstra if (pud_none_or_clear_bad(pud)) 17321da177e4SLinus Torvalds continue; 173397a89413SPeter Zijlstra next = zap_pmd_range(tlb, vma, pud, addr, next, details); 1734a00cc7d9SMatthew Wilcox next: 1735a00cc7d9SMatthew Wilcox cond_resched(); 173697a89413SPeter Zijlstra } while (pud++, addr = next, addr != end); 173751c6f666SRobin Holt 173851c6f666SRobin Holt return addr; 17391da177e4SLinus Torvalds } 17401da177e4SLinus Torvalds 1741c2febafcSKirill A. Shutemov static inline unsigned long zap_p4d_range(struct mmu_gather *tlb, 1742c2febafcSKirill A. Shutemov struct vm_area_struct *vma, pgd_t *pgd, 1743c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 1744c2febafcSKirill A. Shutemov struct zap_details *details) 1745c2febafcSKirill A. Shutemov { 1746c2febafcSKirill A. Shutemov p4d_t *p4d; 1747c2febafcSKirill A. Shutemov unsigned long next; 1748c2febafcSKirill A. Shutemov 1749c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 1750c2febafcSKirill A. Shutemov do { 1751c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 1752c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(p4d)) 1753c2febafcSKirill A. Shutemov continue; 1754c2febafcSKirill A. Shutemov next = zap_pud_range(tlb, vma, p4d, addr, next, details); 1755c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 1756c2febafcSKirill A. Shutemov 1757c2febafcSKirill A. Shutemov return addr; 1758c2febafcSKirill A. Shutemov } 1759c2febafcSKirill A. Shutemov 1760aac45363SMichal Hocko void unmap_page_range(struct mmu_gather *tlb, 176151c6f666SRobin Holt struct vm_area_struct *vma, 17621da177e4SLinus Torvalds unsigned long addr, unsigned long end, 176397a89413SPeter Zijlstra struct zap_details *details) 17641da177e4SLinus Torvalds { 17651da177e4SLinus Torvalds pgd_t *pgd; 17661da177e4SLinus Torvalds unsigned long next; 17671da177e4SLinus Torvalds 17681da177e4SLinus Torvalds BUG_ON(addr >= end); 17691da177e4SLinus Torvalds tlb_start_vma(tlb, vma); 17701da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 17711da177e4SLinus Torvalds do { 17721da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 177397a89413SPeter Zijlstra if (pgd_none_or_clear_bad(pgd)) 17741da177e4SLinus Torvalds continue; 1775c2febafcSKirill A. Shutemov next = zap_p4d_range(tlb, vma, pgd, addr, next, details); 177697a89413SPeter Zijlstra } while (pgd++, addr = next, addr != end); 17771da177e4SLinus Torvalds tlb_end_vma(tlb, vma); 17781da177e4SLinus Torvalds } 17791da177e4SLinus Torvalds 1780f5cc4eefSAl Viro 1781f5cc4eefSAl Viro static void unmap_single_vma(struct mmu_gather *tlb, 17821da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long start_addr, 17834f74d2c8SLinus Torvalds unsigned long end_addr, 178468f48381SSuren Baghdasaryan struct zap_details *details, bool mm_wr_locked) 17851da177e4SLinus Torvalds { 1786f5cc4eefSAl Viro unsigned long start = max(vma->vm_start, start_addr); 17871da177e4SLinus Torvalds unsigned long end; 17881da177e4SLinus Torvalds 17891da177e4SLinus Torvalds if (start >= vma->vm_end) 1790f5cc4eefSAl Viro return; 17911da177e4SLinus Torvalds end = min(vma->vm_end, end_addr); 17921da177e4SLinus Torvalds if (end <= vma->vm_start) 1793f5cc4eefSAl Viro return; 17941da177e4SLinus Torvalds 1795cbc91f71SSrikar Dronamraju if (vma->vm_file) 1796cbc91f71SSrikar Dronamraju uprobe_munmap(vma, start, end); 1797cbc91f71SSrikar Dronamraju 1798b3b9c293SKonstantin Khlebnikov if (unlikely(vma->vm_flags & VM_PFNMAP)) 179968f48381SSuren Baghdasaryan untrack_pfn(vma, 0, 0, mm_wr_locked); 18002ab64037Svenkatesh.pallipadi@intel.com 18018b2a1238SAl Viro if (start != end) { 180251c6f666SRobin Holt if (unlikely(is_vm_hugetlb_page(vma))) { 1803a137e1ccSAndi Kleen /* 1804a137e1ccSAndi Kleen * It is undesirable to test vma->vm_file as it 1805a137e1ccSAndi Kleen * should be non-null for valid hugetlb area. 1806a137e1ccSAndi Kleen * However, vm_file will be NULL in the error 18077aa6b4adSDavidlohr Bueso * cleanup path of mmap_region. When 1808a137e1ccSAndi Kleen * hugetlbfs ->mmap method fails, 18097aa6b4adSDavidlohr Bueso * mmap_region() nullifies vma->vm_file 1810a137e1ccSAndi Kleen * before calling this function to clean up. 1811a137e1ccSAndi Kleen * Since no pte has actually been setup, it is 1812a137e1ccSAndi Kleen * safe to do nothing in this case. 1813a137e1ccSAndi Kleen */ 181424669e58SAneesh Kumar K.V if (vma->vm_file) { 181505e90bd0SPeter Xu zap_flags_t zap_flags = details ? 181605e90bd0SPeter Xu details->zap_flags : 0; 18172820b0f0SRik van Riel __unmap_hugepage_range(tlb, vma, start, end, 181805e90bd0SPeter Xu NULL, zap_flags); 181924669e58SAneesh Kumar K.V } 182051c6f666SRobin Holt } else 1821038c7aa1SAl Viro unmap_page_range(tlb, vma, start, end, details); 182297a89413SPeter Zijlstra } 182351c6f666SRobin Holt } 18241da177e4SLinus Torvalds 1825f5cc4eefSAl Viro /** 1826f5cc4eefSAl Viro * unmap_vmas - unmap a range of memory covered by a list of vma's 1827f5cc4eefSAl Viro * @tlb: address of the caller's struct mmu_gather 18286e412203SYang Li * @mas: the maple state 1829f5cc4eefSAl Viro * @vma: the starting vma 1830f5cc4eefSAl Viro * @start_addr: virtual address at which to start unmapping 1831f5cc4eefSAl Viro * @end_addr: virtual address at which to end unmapping 18326e412203SYang Li * @tree_end: The maximum index to check 1833809ef83cSYang Li * @mm_wr_locked: lock flag 1834f5cc4eefSAl Viro * 1835f5cc4eefSAl Viro * Unmap all pages in the vma list. 1836f5cc4eefSAl Viro * 1837f5cc4eefSAl Viro * Only addresses between `start' and `end' will be unmapped. 1838f5cc4eefSAl Viro * 1839f5cc4eefSAl Viro * The VMA list must be sorted in ascending virtual address order. 1840f5cc4eefSAl Viro * 1841f5cc4eefSAl Viro * unmap_vmas() assumes that the caller will flush the whole unmapped address 1842f5cc4eefSAl Viro * range after unmap_vmas() returns. So the only responsibility here is to 1843f5cc4eefSAl Viro * ensure that any thus-far unmapped pages are flushed before unmap_vmas() 1844f5cc4eefSAl Viro * drops the lock and schedules. 1845f5cc4eefSAl Viro */ 1846fd892593SLiam R. Howlett void unmap_vmas(struct mmu_gather *tlb, struct ma_state *mas, 1847f5cc4eefSAl Viro struct vm_area_struct *vma, unsigned long start_addr, 1848fd892593SLiam R. Howlett unsigned long end_addr, unsigned long tree_end, 1849fd892593SLiam R. Howlett bool mm_wr_locked) 1850f5cc4eefSAl Viro { 1851ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 1852999dad82SPeter Xu struct zap_details details = { 185304ada095SMike Kravetz .zap_flags = ZAP_FLAG_DROP_MARKER | ZAP_FLAG_UNMAP, 1854999dad82SPeter Xu /* Careful - we need to zap private pages too! */ 1855999dad82SPeter Xu .even_cows = true, 1856999dad82SPeter Xu }; 1857f5cc4eefSAl Viro 18587d4a8be0SAlistair Popple mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma->vm_mm, 18596f4f13e8SJérôme Glisse start_addr, end_addr); 1860ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 1861763ecb03SLiam R. Howlett do { 18622820b0f0SRik van Riel unsigned long start = start_addr; 18632820b0f0SRik van Riel unsigned long end = end_addr; 18642820b0f0SRik van Riel hugetlb_zap_begin(vma, &start, &end); 18652820b0f0SRik van Riel unmap_single_vma(tlb, vma, start, end, &details, 186668f48381SSuren Baghdasaryan mm_wr_locked); 18672820b0f0SRik van Riel hugetlb_zap_end(vma, &details); 1868d2406291SPeng Zhang vma = mas_find(mas, tree_end - 1); 1869d2406291SPeng Zhang } while (vma && likely(!xa_is_zero(vma))); 1870ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 18711da177e4SLinus Torvalds } 18721da177e4SLinus Torvalds 18731da177e4SLinus Torvalds /** 1874f5cc4eefSAl Viro * zap_page_range_single - remove user pages in a given range 1875f5cc4eefSAl Viro * @vma: vm_area_struct holding the applicable pages 1876f5cc4eefSAl Viro * @address: starting address of pages to zap 1877f5cc4eefSAl Viro * @size: number of bytes to zap 18788a5f14a2SKirill A. Shutemov * @details: details of shared cache invalidation 1879f5cc4eefSAl Viro * 1880f5cc4eefSAl Viro * The range must fit into one VMA. 1881f5cc4eefSAl Viro */ 188221b85b09SMike Kravetz void zap_page_range_single(struct vm_area_struct *vma, unsigned long address, 1883f5cc4eefSAl Viro unsigned long size, struct zap_details *details) 1884f5cc4eefSAl Viro { 188521b85b09SMike Kravetz const unsigned long end = address + size; 1886ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 1887f5cc4eefSAl Viro struct mmu_gather tlb; 1888f5cc4eefSAl Viro 1889f5cc4eefSAl Viro lru_add_drain(); 18907d4a8be0SAlistair Popple mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma->vm_mm, 189121b85b09SMike Kravetz address, end); 18922820b0f0SRik van Riel hugetlb_zap_begin(vma, &range.start, &range.end); 1893a72afd87SWill Deacon tlb_gather_mmu(&tlb, vma->vm_mm); 1894ac46d4f3SJérôme Glisse update_hiwater_rss(vma->vm_mm); 1895ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 189621b85b09SMike Kravetz /* 189721b85b09SMike Kravetz * unmap 'address-end' not 'range.start-range.end' as range 189821b85b09SMike Kravetz * could have been expanded for hugetlb pmd sharing. 189921b85b09SMike Kravetz */ 190068f48381SSuren Baghdasaryan unmap_single_vma(&tlb, vma, address, end, details, false); 1901ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 1902ae8eba8bSWill Deacon tlb_finish_mmu(&tlb); 19032820b0f0SRik van Riel hugetlb_zap_end(vma, details); 19041da177e4SLinus Torvalds } 19051da177e4SLinus Torvalds 1906c627f9ccSJack Steiner /** 1907c627f9ccSJack Steiner * zap_vma_ptes - remove ptes mapping the vma 1908c627f9ccSJack Steiner * @vma: vm_area_struct holding ptes to be zapped 1909c627f9ccSJack Steiner * @address: starting address of pages to zap 1910c627f9ccSJack Steiner * @size: number of bytes to zap 1911c627f9ccSJack Steiner * 1912c627f9ccSJack Steiner * This function only unmaps ptes assigned to VM_PFNMAP vmas. 1913c627f9ccSJack Steiner * 1914c627f9ccSJack Steiner * The entire address range must be fully contained within the vma. 1915c627f9ccSJack Steiner * 1916c627f9ccSJack Steiner */ 191727d036e3SLeon Romanovsky void zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, 1918c627f9ccSJack Steiner unsigned long size) 1919c627f9ccSJack Steiner { 192088a35912SMiaohe Lin if (!range_in_vma(vma, address, address + size) || 1921c627f9ccSJack Steiner !(vma->vm_flags & VM_PFNMAP)) 192227d036e3SLeon Romanovsky return; 192327d036e3SLeon Romanovsky 1924f5cc4eefSAl Viro zap_page_range_single(vma, address, size, NULL); 1925c627f9ccSJack Steiner } 1926c627f9ccSJack Steiner EXPORT_SYMBOL_GPL(zap_vma_ptes); 1927c627f9ccSJack Steiner 19288cd3984dSArjun Roy static pmd_t *walk_to_pmd(struct mm_struct *mm, unsigned long addr) 1929c9cfcddfSLinus Torvalds { 1930c2febafcSKirill A. Shutemov pgd_t *pgd; 1931c2febafcSKirill A. Shutemov p4d_t *p4d; 1932c2febafcSKirill A. Shutemov pud_t *pud; 1933c2febafcSKirill A. Shutemov pmd_t *pmd; 1934c2febafcSKirill A. Shutemov 1935c2febafcSKirill A. Shutemov pgd = pgd_offset(mm, addr); 1936c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, addr); 1937c2febafcSKirill A. Shutemov if (!p4d) 1938c2febafcSKirill A. Shutemov return NULL; 1939c2febafcSKirill A. Shutemov pud = pud_alloc(mm, p4d, addr); 1940c2febafcSKirill A. Shutemov if (!pud) 1941c2febafcSKirill A. Shutemov return NULL; 1942c2febafcSKirill A. Shutemov pmd = pmd_alloc(mm, pud, addr); 1943c2febafcSKirill A. Shutemov if (!pmd) 1944c2febafcSKirill A. Shutemov return NULL; 1945c2febafcSKirill A. Shutemov 1946f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 19478cd3984dSArjun Roy return pmd; 19488cd3984dSArjun Roy } 19498cd3984dSArjun Roy 19508cd3984dSArjun Roy pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr, 19518cd3984dSArjun Roy spinlock_t **ptl) 19528cd3984dSArjun Roy { 19538cd3984dSArjun Roy pmd_t *pmd = walk_to_pmd(mm, addr); 19548cd3984dSArjun Roy 19558cd3984dSArjun Roy if (!pmd) 19568cd3984dSArjun Roy return NULL; 1957c9cfcddfSLinus Torvalds return pte_alloc_map_lock(mm, pmd, addr, ptl); 1958c9cfcddfSLinus Torvalds } 1959c9cfcddfSLinus Torvalds 19608efd6f5bSArjun Roy static int validate_page_before_insert(struct page *page) 19618efd6f5bSArjun Roy { 1962f8b6187dSKefeng Wang struct folio *folio = page_folio(page); 1963f8b6187dSKefeng Wang 1964f8b6187dSKefeng Wang if (folio_test_anon(folio) || folio_test_slab(folio) || 1965f8b6187dSKefeng Wang page_has_type(page)) 19668efd6f5bSArjun Roy return -EINVAL; 1967f8b6187dSKefeng Wang flush_dcache_folio(folio); 19688efd6f5bSArjun Roy return 0; 19698efd6f5bSArjun Roy } 19708efd6f5bSArjun Roy 1971cea86fe2SHugh Dickins static int insert_page_into_pte_locked(struct vm_area_struct *vma, pte_t *pte, 19728efd6f5bSArjun Roy unsigned long addr, struct page *page, pgprot_t prot) 19738efd6f5bSArjun Roy { 1974ef37b2eaSDavid Hildenbrand struct folio *folio = page_folio(page); 1975ef37b2eaSDavid Hildenbrand 1976c33c7948SRyan Roberts if (!pte_none(ptep_get(pte))) 19778efd6f5bSArjun Roy return -EBUSY; 19788efd6f5bSArjun Roy /* Ok, finally just insert the thing.. */ 1979ef37b2eaSDavid Hildenbrand folio_get(folio); 19806b27cc6cSKefeng Wang inc_mm_counter(vma->vm_mm, mm_counter_file(folio)); 1981ef37b2eaSDavid Hildenbrand folio_add_file_rmap_pte(folio, page, vma); 1982cea86fe2SHugh Dickins set_pte_at(vma->vm_mm, addr, pte, mk_pte(page, prot)); 19838efd6f5bSArjun Roy return 0; 19848efd6f5bSArjun Roy } 19858efd6f5bSArjun Roy 19861da177e4SLinus Torvalds /* 1987238f58d8SLinus Torvalds * This is the old fallback for page remapping. 1988238f58d8SLinus Torvalds * 1989238f58d8SLinus Torvalds * For historical reasons, it only allows reserved pages. Only 1990238f58d8SLinus Torvalds * old drivers should use this, and they needed to mark their 1991238f58d8SLinus Torvalds * pages reserved for the old functions anyway. 1992238f58d8SLinus Torvalds */ 1993423bad60SNick Piggin static int insert_page(struct vm_area_struct *vma, unsigned long addr, 1994423bad60SNick Piggin struct page *page, pgprot_t prot) 1995238f58d8SLinus Torvalds { 1996238f58d8SLinus Torvalds int retval; 1997238f58d8SLinus Torvalds pte_t *pte; 1998238f58d8SLinus Torvalds spinlock_t *ptl; 1999238f58d8SLinus Torvalds 20008efd6f5bSArjun Roy retval = validate_page_before_insert(page); 20018efd6f5bSArjun Roy if (retval) 20025b4e655eSKAMEZAWA Hiroyuki goto out; 2003238f58d8SLinus Torvalds retval = -ENOMEM; 2004cea86fe2SHugh Dickins pte = get_locked_pte(vma->vm_mm, addr, &ptl); 2005238f58d8SLinus Torvalds if (!pte) 20065b4e655eSKAMEZAWA Hiroyuki goto out; 2007cea86fe2SHugh Dickins retval = insert_page_into_pte_locked(vma, pte, addr, page, prot); 2008238f58d8SLinus Torvalds pte_unmap_unlock(pte, ptl); 2009238f58d8SLinus Torvalds out: 2010238f58d8SLinus Torvalds return retval; 2011238f58d8SLinus Torvalds } 2012238f58d8SLinus Torvalds 2013cea86fe2SHugh Dickins static int insert_page_in_batch_locked(struct vm_area_struct *vma, pte_t *pte, 20148cd3984dSArjun Roy unsigned long addr, struct page *page, pgprot_t prot) 20158cd3984dSArjun Roy { 20168cd3984dSArjun Roy int err; 20178cd3984dSArjun Roy 20188cd3984dSArjun Roy if (!page_count(page)) 20198cd3984dSArjun Roy return -EINVAL; 20208cd3984dSArjun Roy err = validate_page_before_insert(page); 20217f70c2a6SArjun Roy if (err) 20227f70c2a6SArjun Roy return err; 2023cea86fe2SHugh Dickins return insert_page_into_pte_locked(vma, pte, addr, page, prot); 20248cd3984dSArjun Roy } 20258cd3984dSArjun Roy 20268cd3984dSArjun Roy /* insert_pages() amortizes the cost of spinlock operations 2027bb7dbaafSMatthew Wilcox (Oracle) * when inserting pages in a loop. 20288cd3984dSArjun Roy */ 20298cd3984dSArjun Roy static int insert_pages(struct vm_area_struct *vma, unsigned long addr, 20308cd3984dSArjun Roy struct page **pages, unsigned long *num, pgprot_t prot) 20318cd3984dSArjun Roy { 20328cd3984dSArjun Roy pmd_t *pmd = NULL; 20337f70c2a6SArjun Roy pte_t *start_pte, *pte; 20347f70c2a6SArjun Roy spinlock_t *pte_lock; 20358cd3984dSArjun Roy struct mm_struct *const mm = vma->vm_mm; 20368cd3984dSArjun Roy unsigned long curr_page_idx = 0; 20378cd3984dSArjun Roy unsigned long remaining_pages_total = *num; 20388cd3984dSArjun Roy unsigned long pages_to_write_in_pmd; 20398cd3984dSArjun Roy int ret; 20408cd3984dSArjun Roy more: 20418cd3984dSArjun Roy ret = -EFAULT; 20428cd3984dSArjun Roy pmd = walk_to_pmd(mm, addr); 20438cd3984dSArjun Roy if (!pmd) 20448cd3984dSArjun Roy goto out; 20458cd3984dSArjun Roy 20468cd3984dSArjun Roy pages_to_write_in_pmd = min_t(unsigned long, 20478cd3984dSArjun Roy remaining_pages_total, PTRS_PER_PTE - pte_index(addr)); 20488cd3984dSArjun Roy 20498cd3984dSArjun Roy /* Allocate the PTE if necessary; takes PMD lock once only. */ 20508cd3984dSArjun Roy ret = -ENOMEM; 20518cd3984dSArjun Roy if (pte_alloc(mm, pmd)) 20528cd3984dSArjun Roy goto out; 20538cd3984dSArjun Roy 20548cd3984dSArjun Roy while (pages_to_write_in_pmd) { 20558cd3984dSArjun Roy int pte_idx = 0; 20568cd3984dSArjun Roy const int batch_size = min_t(int, pages_to_write_in_pmd, 8); 20578cd3984dSArjun Roy 20587f70c2a6SArjun Roy start_pte = pte_offset_map_lock(mm, pmd, addr, &pte_lock); 20593db82b93SHugh Dickins if (!start_pte) { 20603db82b93SHugh Dickins ret = -EFAULT; 20613db82b93SHugh Dickins goto out; 20623db82b93SHugh Dickins } 20637f70c2a6SArjun Roy for (pte = start_pte; pte_idx < batch_size; ++pte, ++pte_idx) { 2064cea86fe2SHugh Dickins int err = insert_page_in_batch_locked(vma, pte, 20658cd3984dSArjun Roy addr, pages[curr_page_idx], prot); 20668cd3984dSArjun Roy if (unlikely(err)) { 20677f70c2a6SArjun Roy pte_unmap_unlock(start_pte, pte_lock); 20688cd3984dSArjun Roy ret = err; 20698cd3984dSArjun Roy remaining_pages_total -= pte_idx; 20708cd3984dSArjun Roy goto out; 20718cd3984dSArjun Roy } 20728cd3984dSArjun Roy addr += PAGE_SIZE; 20738cd3984dSArjun Roy ++curr_page_idx; 20748cd3984dSArjun Roy } 20757f70c2a6SArjun Roy pte_unmap_unlock(start_pte, pte_lock); 20768cd3984dSArjun Roy pages_to_write_in_pmd -= batch_size; 20778cd3984dSArjun Roy remaining_pages_total -= batch_size; 20788cd3984dSArjun Roy } 20798cd3984dSArjun Roy if (remaining_pages_total) 20808cd3984dSArjun Roy goto more; 20818cd3984dSArjun Roy ret = 0; 20828cd3984dSArjun Roy out: 20838cd3984dSArjun Roy *num = remaining_pages_total; 20848cd3984dSArjun Roy return ret; 20858cd3984dSArjun Roy } 20868cd3984dSArjun Roy 20878cd3984dSArjun Roy /** 20888cd3984dSArjun Roy * vm_insert_pages - insert multiple pages into user vma, batching the pmd lock. 20898cd3984dSArjun Roy * @vma: user vma to map to 20908cd3984dSArjun Roy * @addr: target start user address of these pages 20918cd3984dSArjun Roy * @pages: source kernel pages 20928cd3984dSArjun Roy * @num: in: number of pages to map. out: number of pages that were *not* 20938cd3984dSArjun Roy * mapped. (0 means all pages were successfully mapped). 20948cd3984dSArjun Roy * 20958cd3984dSArjun Roy * Preferred over vm_insert_page() when inserting multiple pages. 20968cd3984dSArjun Roy * 20978cd3984dSArjun Roy * In case of error, we may have mapped a subset of the provided 20988cd3984dSArjun Roy * pages. It is the caller's responsibility to account for this case. 20998cd3984dSArjun Roy * 21008cd3984dSArjun Roy * The same restrictions apply as in vm_insert_page(). 21018cd3984dSArjun Roy */ 21028cd3984dSArjun Roy int vm_insert_pages(struct vm_area_struct *vma, unsigned long addr, 21038cd3984dSArjun Roy struct page **pages, unsigned long *num) 21048cd3984dSArjun Roy { 21058cd3984dSArjun Roy const unsigned long end_addr = addr + (*num * PAGE_SIZE) - 1; 21068cd3984dSArjun Roy 21078cd3984dSArjun Roy if (addr < vma->vm_start || end_addr >= vma->vm_end) 21088cd3984dSArjun Roy return -EFAULT; 21098cd3984dSArjun Roy if (!(vma->vm_flags & VM_MIXEDMAP)) { 2110d8ed45c5SMichel Lespinasse BUG_ON(mmap_read_trylock(vma->vm_mm)); 21118cd3984dSArjun Roy BUG_ON(vma->vm_flags & VM_PFNMAP); 21121c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_MIXEDMAP); 21138cd3984dSArjun Roy } 21148cd3984dSArjun Roy /* Defer page refcount checking till we're about to map that page. */ 21158cd3984dSArjun Roy return insert_pages(vma, addr, pages, num, vma->vm_page_prot); 21168cd3984dSArjun Roy } 21178cd3984dSArjun Roy EXPORT_SYMBOL(vm_insert_pages); 21188cd3984dSArjun Roy 2119bfa5bf6dSRolf Eike Beer /** 2120bfa5bf6dSRolf Eike Beer * vm_insert_page - insert single page into user vma 2121bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 2122bfa5bf6dSRolf Eike Beer * @addr: target user address of this page 2123bfa5bf6dSRolf Eike Beer * @page: source kernel page 2124bfa5bf6dSRolf Eike Beer * 2125a145dd41SLinus Torvalds * This allows drivers to insert individual pages they've allocated 2126a145dd41SLinus Torvalds * into a user vma. 2127a145dd41SLinus Torvalds * 2128a145dd41SLinus Torvalds * The page has to be a nice clean _individual_ kernel allocation. 2129a145dd41SLinus Torvalds * If you allocate a compound page, you need to have marked it as 2130a145dd41SLinus Torvalds * such (__GFP_COMP), or manually just split the page up yourself 21318dfcc9baSNick Piggin * (see split_page()). 2132a145dd41SLinus Torvalds * 2133a145dd41SLinus Torvalds * NOTE! Traditionally this was done with "remap_pfn_range()" which 2134a145dd41SLinus Torvalds * took an arbitrary page protection parameter. This doesn't allow 2135a145dd41SLinus Torvalds * that. Your vma protection will have to be set up correctly, which 2136a145dd41SLinus Torvalds * means that if you want a shared writable mapping, you'd better 2137a145dd41SLinus Torvalds * ask for a shared writable mapping! 2138a145dd41SLinus Torvalds * 2139a145dd41SLinus Torvalds * The page does not need to be reserved. 21404b6e1e37SKonstantin Khlebnikov * 21414b6e1e37SKonstantin Khlebnikov * Usually this function is called from f_op->mmap() handler 2142c1e8d7c6SMichel Lespinasse * under mm->mmap_lock write-lock, so it can change vma->vm_flags. 21434b6e1e37SKonstantin Khlebnikov * Caller must set VM_MIXEDMAP on vma if it wants to call this 21444b6e1e37SKonstantin Khlebnikov * function from other places, for example from page-fault handler. 2145a862f68aSMike Rapoport * 2146a862f68aSMike Rapoport * Return: %0 on success, negative error code otherwise. 2147a145dd41SLinus Torvalds */ 2148423bad60SNick Piggin int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, 2149423bad60SNick Piggin struct page *page) 2150a145dd41SLinus Torvalds { 2151a145dd41SLinus Torvalds if (addr < vma->vm_start || addr >= vma->vm_end) 2152a145dd41SLinus Torvalds return -EFAULT; 2153a145dd41SLinus Torvalds if (!page_count(page)) 2154a145dd41SLinus Torvalds return -EINVAL; 21554b6e1e37SKonstantin Khlebnikov if (!(vma->vm_flags & VM_MIXEDMAP)) { 2156d8ed45c5SMichel Lespinasse BUG_ON(mmap_read_trylock(vma->vm_mm)); 21574b6e1e37SKonstantin Khlebnikov BUG_ON(vma->vm_flags & VM_PFNMAP); 21581c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_MIXEDMAP); 21594b6e1e37SKonstantin Khlebnikov } 2160423bad60SNick Piggin return insert_page(vma, addr, page, vma->vm_page_prot); 2161a145dd41SLinus Torvalds } 2162e3c3374fSLinus Torvalds EXPORT_SYMBOL(vm_insert_page); 2163a145dd41SLinus Torvalds 2164a667d745SSouptick Joarder /* 2165a667d745SSouptick Joarder * __vm_map_pages - maps range of kernel pages into user vma 2166a667d745SSouptick Joarder * @vma: user vma to map to 2167a667d745SSouptick Joarder * @pages: pointer to array of source kernel pages 2168a667d745SSouptick Joarder * @num: number of pages in page array 2169a667d745SSouptick Joarder * @offset: user's requested vm_pgoff 2170a667d745SSouptick Joarder * 2171a667d745SSouptick Joarder * This allows drivers to map range of kernel pages into a user vma. 2172a667d745SSouptick Joarder * 2173a667d745SSouptick Joarder * Return: 0 on success and error code otherwise. 2174a667d745SSouptick Joarder */ 2175a667d745SSouptick Joarder static int __vm_map_pages(struct vm_area_struct *vma, struct page **pages, 2176a667d745SSouptick Joarder unsigned long num, unsigned long offset) 2177a667d745SSouptick Joarder { 2178a667d745SSouptick Joarder unsigned long count = vma_pages(vma); 2179a667d745SSouptick Joarder unsigned long uaddr = vma->vm_start; 2180a667d745SSouptick Joarder int ret, i; 2181a667d745SSouptick Joarder 2182a667d745SSouptick Joarder /* Fail if the user requested offset is beyond the end of the object */ 218396756fcbSMiguel Ojeda if (offset >= num) 2184a667d745SSouptick Joarder return -ENXIO; 2185a667d745SSouptick Joarder 2186a667d745SSouptick Joarder /* Fail if the user requested size exceeds available object size */ 2187a667d745SSouptick Joarder if (count > num - offset) 2188a667d745SSouptick Joarder return -ENXIO; 2189a667d745SSouptick Joarder 2190a667d745SSouptick Joarder for (i = 0; i < count; i++) { 2191a667d745SSouptick Joarder ret = vm_insert_page(vma, uaddr, pages[offset + i]); 2192a667d745SSouptick Joarder if (ret < 0) 2193a667d745SSouptick Joarder return ret; 2194a667d745SSouptick Joarder uaddr += PAGE_SIZE; 2195a667d745SSouptick Joarder } 2196a667d745SSouptick Joarder 2197a667d745SSouptick Joarder return 0; 2198a667d745SSouptick Joarder } 2199a667d745SSouptick Joarder 2200a667d745SSouptick Joarder /** 2201a667d745SSouptick Joarder * vm_map_pages - maps range of kernel pages starts with non zero offset 2202a667d745SSouptick Joarder * @vma: user vma to map to 2203a667d745SSouptick Joarder * @pages: pointer to array of source kernel pages 2204a667d745SSouptick Joarder * @num: number of pages in page array 2205a667d745SSouptick Joarder * 2206a667d745SSouptick Joarder * Maps an object consisting of @num pages, catering for the user's 2207a667d745SSouptick Joarder * requested vm_pgoff 2208a667d745SSouptick Joarder * 2209a667d745SSouptick Joarder * If we fail to insert any page into the vma, the function will return 2210a667d745SSouptick Joarder * immediately leaving any previously inserted pages present. Callers 2211a667d745SSouptick Joarder * from the mmap handler may immediately return the error as their caller 2212a667d745SSouptick Joarder * will destroy the vma, removing any successfully inserted pages. Other 2213a667d745SSouptick Joarder * callers should make their own arrangements for calling unmap_region(). 2214a667d745SSouptick Joarder * 2215a667d745SSouptick Joarder * Context: Process context. Called by mmap handlers. 2216a667d745SSouptick Joarder * Return: 0 on success and error code otherwise. 2217a667d745SSouptick Joarder */ 2218a667d745SSouptick Joarder int vm_map_pages(struct vm_area_struct *vma, struct page **pages, 2219a667d745SSouptick Joarder unsigned long num) 2220a667d745SSouptick Joarder { 2221a667d745SSouptick Joarder return __vm_map_pages(vma, pages, num, vma->vm_pgoff); 2222a667d745SSouptick Joarder } 2223a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages); 2224a667d745SSouptick Joarder 2225a667d745SSouptick Joarder /** 2226a667d745SSouptick Joarder * vm_map_pages_zero - map range of kernel pages starts with zero offset 2227a667d745SSouptick Joarder * @vma: user vma to map to 2228a667d745SSouptick Joarder * @pages: pointer to array of source kernel pages 2229a667d745SSouptick Joarder * @num: number of pages in page array 2230a667d745SSouptick Joarder * 2231a667d745SSouptick Joarder * Similar to vm_map_pages(), except that it explicitly sets the offset 2232a667d745SSouptick Joarder * to 0. This function is intended for the drivers that did not consider 2233a667d745SSouptick Joarder * vm_pgoff. 2234a667d745SSouptick Joarder * 2235a667d745SSouptick Joarder * Context: Process context. Called by mmap handlers. 2236a667d745SSouptick Joarder * Return: 0 on success and error code otherwise. 2237a667d745SSouptick Joarder */ 2238a667d745SSouptick Joarder int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages, 2239a667d745SSouptick Joarder unsigned long num) 2240a667d745SSouptick Joarder { 2241a667d745SSouptick Joarder return __vm_map_pages(vma, pages, num, 0); 2242a667d745SSouptick Joarder } 2243a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages_zero); 2244a667d745SSouptick Joarder 22459b5a8e00SMatthew Wilcox static vm_fault_t insert_pfn(struct vm_area_struct *vma, unsigned long addr, 2246b2770da6SRoss Zwisler pfn_t pfn, pgprot_t prot, bool mkwrite) 2247423bad60SNick Piggin { 2248423bad60SNick Piggin struct mm_struct *mm = vma->vm_mm; 2249423bad60SNick Piggin pte_t *pte, entry; 2250423bad60SNick Piggin spinlock_t *ptl; 2251423bad60SNick Piggin 2252423bad60SNick Piggin pte = get_locked_pte(mm, addr, &ptl); 2253423bad60SNick Piggin if (!pte) 22549b5a8e00SMatthew Wilcox return VM_FAULT_OOM; 2255c33c7948SRyan Roberts entry = ptep_get(pte); 2256c33c7948SRyan Roberts if (!pte_none(entry)) { 2257b2770da6SRoss Zwisler if (mkwrite) { 2258b2770da6SRoss Zwisler /* 2259b2770da6SRoss Zwisler * For read faults on private mappings the PFN passed 2260b2770da6SRoss Zwisler * in may not match the PFN we have mapped if the 2261b2770da6SRoss Zwisler * mapped PFN is a writeable COW page. In the mkwrite 2262b2770da6SRoss Zwisler * case we are creating a writable PTE for a shared 2263f2c57d91SJan Kara * mapping and we expect the PFNs to match. If they 2264f2c57d91SJan Kara * don't match, we are likely racing with block 2265f2c57d91SJan Kara * allocation and mapping invalidation so just skip the 2266f2c57d91SJan Kara * update. 2267b2770da6SRoss Zwisler */ 2268c33c7948SRyan Roberts if (pte_pfn(entry) != pfn_t_to_pfn(pfn)) { 2269c33c7948SRyan Roberts WARN_ON_ONCE(!is_zero_pfn(pte_pfn(entry))); 2270423bad60SNick Piggin goto out_unlock; 2271f2c57d91SJan Kara } 2272c33c7948SRyan Roberts entry = pte_mkyoung(entry); 2273cae85cb8SJan Kara entry = maybe_mkwrite(pte_mkdirty(entry), vma); 2274cae85cb8SJan Kara if (ptep_set_access_flags(vma, addr, pte, entry, 1)) 2275cae85cb8SJan Kara update_mmu_cache(vma, addr, pte); 2276cae85cb8SJan Kara } 2277b2770da6SRoss Zwisler goto out_unlock; 2278b2770da6SRoss Zwisler } 2279423bad60SNick Piggin 2280423bad60SNick Piggin /* Ok, finally just insert the thing.. */ 228101c8f1c4SDan Williams if (pfn_t_devmap(pfn)) 228201c8f1c4SDan Williams entry = pte_mkdevmap(pfn_t_pte(pfn, prot)); 228301c8f1c4SDan Williams else 228401c8f1c4SDan Williams entry = pte_mkspecial(pfn_t_pte(pfn, prot)); 2285b2770da6SRoss Zwisler 2286b2770da6SRoss Zwisler if (mkwrite) { 2287b2770da6SRoss Zwisler entry = pte_mkyoung(entry); 2288b2770da6SRoss Zwisler entry = maybe_mkwrite(pte_mkdirty(entry), vma); 2289b2770da6SRoss Zwisler } 2290b2770da6SRoss Zwisler 2291423bad60SNick Piggin set_pte_at(mm, addr, pte, entry); 22924b3073e1SRussell King update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */ 2293423bad60SNick Piggin 2294423bad60SNick Piggin out_unlock: 2295423bad60SNick Piggin pte_unmap_unlock(pte, ptl); 22969b5a8e00SMatthew Wilcox return VM_FAULT_NOPAGE; 2297423bad60SNick Piggin } 2298423bad60SNick Piggin 2299f5e6d1d5SMatthew Wilcox /** 2300f5e6d1d5SMatthew Wilcox * vmf_insert_pfn_prot - insert single pfn into user vma with specified pgprot 2301f5e6d1d5SMatthew Wilcox * @vma: user vma to map to 2302f5e6d1d5SMatthew Wilcox * @addr: target user address of this page 2303f5e6d1d5SMatthew Wilcox * @pfn: source kernel pfn 2304f5e6d1d5SMatthew Wilcox * @pgprot: pgprot flags for the inserted page 2305f5e6d1d5SMatthew Wilcox * 2306a1a0aea5SRandy Dunlap * This is exactly like vmf_insert_pfn(), except that it allows drivers 2307f5e6d1d5SMatthew Wilcox * to override pgprot on a per-page basis. 2308f5e6d1d5SMatthew Wilcox * 2309f5e6d1d5SMatthew Wilcox * This only makes sense for IO mappings, and it makes no sense for 2310f5e6d1d5SMatthew Wilcox * COW mappings. In general, using multiple vmas is preferable; 2311ae2b01f3SMatthew Wilcox * vmf_insert_pfn_prot should only be used if using multiple VMAs is 2312f5e6d1d5SMatthew Wilcox * impractical. 2313f5e6d1d5SMatthew Wilcox * 231428d8b812SLorenzo Stoakes * pgprot typically only differs from @vma->vm_page_prot when drivers set 231528d8b812SLorenzo Stoakes * caching- and encryption bits different than those of @vma->vm_page_prot, 231628d8b812SLorenzo Stoakes * because the caching- or encryption mode may not be known at mmap() time. 231728d8b812SLorenzo Stoakes * 231828d8b812SLorenzo Stoakes * This is ok as long as @vma->vm_page_prot is not used by the core vm 231928d8b812SLorenzo Stoakes * to set caching and encryption bits for those vmas (except for COW pages). 232028d8b812SLorenzo Stoakes * This is ensured by core vm only modifying these page table entries using 232128d8b812SLorenzo Stoakes * functions that don't touch caching- or encryption bits, using pte_modify() 232228d8b812SLorenzo Stoakes * if needed. (See for example mprotect()). 232328d8b812SLorenzo Stoakes * 232428d8b812SLorenzo Stoakes * Also when new page-table entries are created, this is only done using the 232528d8b812SLorenzo Stoakes * fault() callback, and never using the value of vma->vm_page_prot, 232628d8b812SLorenzo Stoakes * except for page-table entries that point to anonymous pages as the result 232728d8b812SLorenzo Stoakes * of COW. 2328574c5b3dSThomas Hellstrom * 2329ae2b01f3SMatthew Wilcox * Context: Process context. May allocate using %GFP_KERNEL. 2330f5e6d1d5SMatthew Wilcox * Return: vm_fault_t value. 2331f5e6d1d5SMatthew Wilcox */ 2332f5e6d1d5SMatthew Wilcox vm_fault_t vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr, 2333f5e6d1d5SMatthew Wilcox unsigned long pfn, pgprot_t pgprot) 2334f5e6d1d5SMatthew Wilcox { 23356d958546SMatthew Wilcox /* 23366d958546SMatthew Wilcox * Technically, architectures with pte_special can avoid all these 23376d958546SMatthew Wilcox * restrictions (same for remap_pfn_range). However we would like 23386d958546SMatthew Wilcox * consistency in testing and feature parity among all, so we should 23396d958546SMatthew Wilcox * try to keep these invariants in place for everybody. 23406d958546SMatthew Wilcox */ 23416d958546SMatthew Wilcox BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))); 23426d958546SMatthew Wilcox BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) == 23436d958546SMatthew Wilcox (VM_PFNMAP|VM_MIXEDMAP)); 23446d958546SMatthew Wilcox BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); 23456d958546SMatthew Wilcox BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn)); 23466d958546SMatthew Wilcox 23476d958546SMatthew Wilcox if (addr < vma->vm_start || addr >= vma->vm_end) 23486d958546SMatthew Wilcox return VM_FAULT_SIGBUS; 23496d958546SMatthew Wilcox 23506d958546SMatthew Wilcox if (!pfn_modify_allowed(pfn, pgprot)) 23516d958546SMatthew Wilcox return VM_FAULT_SIGBUS; 23526d958546SMatthew Wilcox 23536d958546SMatthew Wilcox track_pfn_insert(vma, &pgprot, __pfn_to_pfn_t(pfn, PFN_DEV)); 23546d958546SMatthew Wilcox 23559b5a8e00SMatthew Wilcox return insert_pfn(vma, addr, __pfn_to_pfn_t(pfn, PFN_DEV), pgprot, 23566d958546SMatthew Wilcox false); 2357f5e6d1d5SMatthew Wilcox } 2358f5e6d1d5SMatthew Wilcox EXPORT_SYMBOL(vmf_insert_pfn_prot); 2359e0dc0d8fSNick Piggin 2360ae2b01f3SMatthew Wilcox /** 2361ae2b01f3SMatthew Wilcox * vmf_insert_pfn - insert single pfn into user vma 2362ae2b01f3SMatthew Wilcox * @vma: user vma to map to 2363ae2b01f3SMatthew Wilcox * @addr: target user address of this page 2364ae2b01f3SMatthew Wilcox * @pfn: source kernel pfn 2365ae2b01f3SMatthew Wilcox * 2366ae2b01f3SMatthew Wilcox * Similar to vm_insert_page, this allows drivers to insert individual pages 2367ae2b01f3SMatthew Wilcox * they've allocated into a user vma. Same comments apply. 2368ae2b01f3SMatthew Wilcox * 2369ae2b01f3SMatthew Wilcox * This function should only be called from a vm_ops->fault handler, and 2370ae2b01f3SMatthew Wilcox * in that case the handler should return the result of this function. 2371ae2b01f3SMatthew Wilcox * 2372ae2b01f3SMatthew Wilcox * vma cannot be a COW mapping. 2373ae2b01f3SMatthew Wilcox * 2374ae2b01f3SMatthew Wilcox * As this is called only for pages that do not currently exist, we 2375ae2b01f3SMatthew Wilcox * do not need to flush old virtual caches or the TLB. 2376ae2b01f3SMatthew Wilcox * 2377ae2b01f3SMatthew Wilcox * Context: Process context. May allocate using %GFP_KERNEL. 2378ae2b01f3SMatthew Wilcox * Return: vm_fault_t value. 2379ae2b01f3SMatthew Wilcox */ 2380ae2b01f3SMatthew Wilcox vm_fault_t vmf_insert_pfn(struct vm_area_struct *vma, unsigned long addr, 2381ae2b01f3SMatthew Wilcox unsigned long pfn) 2382ae2b01f3SMatthew Wilcox { 2383ae2b01f3SMatthew Wilcox return vmf_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot); 2384ae2b01f3SMatthew Wilcox } 2385ae2b01f3SMatthew Wilcox EXPORT_SYMBOL(vmf_insert_pfn); 2386ae2b01f3SMatthew Wilcox 2387785a3fabSDan Williams static bool vm_mixed_ok(struct vm_area_struct *vma, pfn_t pfn) 2388785a3fabSDan Williams { 2389785a3fabSDan Williams /* these checks mirror the abort conditions in vm_normal_page */ 2390785a3fabSDan Williams if (vma->vm_flags & VM_MIXEDMAP) 2391785a3fabSDan Williams return true; 2392785a3fabSDan Williams if (pfn_t_devmap(pfn)) 2393785a3fabSDan Williams return true; 2394785a3fabSDan Williams if (pfn_t_special(pfn)) 2395785a3fabSDan Williams return true; 2396785a3fabSDan Williams if (is_zero_pfn(pfn_t_to_pfn(pfn))) 2397785a3fabSDan Williams return true; 2398785a3fabSDan Williams return false; 2399785a3fabSDan Williams } 2400785a3fabSDan Williams 240179f3aa5bSMatthew Wilcox static vm_fault_t __vm_insert_mixed(struct vm_area_struct *vma, 240228d8b812SLorenzo Stoakes unsigned long addr, pfn_t pfn, bool mkwrite) 2403423bad60SNick Piggin { 240428d8b812SLorenzo Stoakes pgprot_t pgprot = vma->vm_page_prot; 240579f3aa5bSMatthew Wilcox int err; 240687744ab3SDan Williams 2407785a3fabSDan Williams BUG_ON(!vm_mixed_ok(vma, pfn)); 2408423bad60SNick Piggin 2409423bad60SNick Piggin if (addr < vma->vm_start || addr >= vma->vm_end) 241079f3aa5bSMatthew Wilcox return VM_FAULT_SIGBUS; 2411308a047cSBorislav Petkov 2412308a047cSBorislav Petkov track_pfn_insert(vma, &pgprot, pfn); 2413423bad60SNick Piggin 241442e4089cSAndi Kleen if (!pfn_modify_allowed(pfn_t_to_pfn(pfn), pgprot)) 241579f3aa5bSMatthew Wilcox return VM_FAULT_SIGBUS; 241642e4089cSAndi Kleen 2417423bad60SNick Piggin /* 2418423bad60SNick Piggin * If we don't have pte special, then we have to use the pfn_valid() 2419423bad60SNick Piggin * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must* 2420423bad60SNick Piggin * refcount the page if pfn_valid is true (hence insert_page rather 242162eede62SHugh Dickins * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP 242262eede62SHugh Dickins * without pte special, it would there be refcounted as a normal page. 2423423bad60SNick Piggin */ 242400b3a331SLaurent Dufour if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) && 242500b3a331SLaurent Dufour !pfn_t_devmap(pfn) && pfn_t_valid(pfn)) { 2426423bad60SNick Piggin struct page *page; 2427423bad60SNick Piggin 242803fc2da6SDan Williams /* 242903fc2da6SDan Williams * At this point we are committed to insert_page() 243003fc2da6SDan Williams * regardless of whether the caller specified flags that 243103fc2da6SDan Williams * result in pfn_t_has_page() == false. 243203fc2da6SDan Williams */ 243303fc2da6SDan Williams page = pfn_to_page(pfn_t_to_pfn(pfn)); 243479f3aa5bSMatthew Wilcox err = insert_page(vma, addr, page, pgprot); 243579f3aa5bSMatthew Wilcox } else { 24369b5a8e00SMatthew Wilcox return insert_pfn(vma, addr, pfn, pgprot, mkwrite); 2437423bad60SNick Piggin } 2438b2770da6SRoss Zwisler 24395d747637SMatthew Wilcox if (err == -ENOMEM) 24405d747637SMatthew Wilcox return VM_FAULT_OOM; 24415d747637SMatthew Wilcox if (err < 0 && err != -EBUSY) 24425d747637SMatthew Wilcox return VM_FAULT_SIGBUS; 24435d747637SMatthew Wilcox 24445d747637SMatthew Wilcox return VM_FAULT_NOPAGE; 2445423bad60SNick Piggin } 244679f3aa5bSMatthew Wilcox 244779f3aa5bSMatthew Wilcox vm_fault_t vmf_insert_mixed(struct vm_area_struct *vma, unsigned long addr, 244879f3aa5bSMatthew Wilcox pfn_t pfn) 244979f3aa5bSMatthew Wilcox { 245028d8b812SLorenzo Stoakes return __vm_insert_mixed(vma, addr, pfn, false); 245179f3aa5bSMatthew Wilcox } 24525d747637SMatthew Wilcox EXPORT_SYMBOL(vmf_insert_mixed); 2453423bad60SNick Piggin 2454ab77dab4SSouptick Joarder /* 2455ab77dab4SSouptick Joarder * If the insertion of PTE failed because someone else already added a 2456ab77dab4SSouptick Joarder * different entry in the mean time, we treat that as success as we assume 2457ab77dab4SSouptick Joarder * the same entry was actually inserted. 2458ab77dab4SSouptick Joarder */ 2459ab77dab4SSouptick Joarder vm_fault_t vmf_insert_mixed_mkwrite(struct vm_area_struct *vma, 2460ab77dab4SSouptick Joarder unsigned long addr, pfn_t pfn) 2461b2770da6SRoss Zwisler { 246228d8b812SLorenzo Stoakes return __vm_insert_mixed(vma, addr, pfn, true); 2463b2770da6SRoss Zwisler } 2464ab77dab4SSouptick Joarder EXPORT_SYMBOL(vmf_insert_mixed_mkwrite); 2465b2770da6SRoss Zwisler 2466a145dd41SLinus Torvalds /* 24671da177e4SLinus Torvalds * maps a range of physical memory into the requested pages. the old 24681da177e4SLinus Torvalds * mappings are removed. any references to nonexistent pages results 24691da177e4SLinus Torvalds * in null mappings (currently treated as "copy-on-access") 24701da177e4SLinus Torvalds */ 24711da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd, 24721da177e4SLinus Torvalds unsigned long addr, unsigned long end, 24731da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 24741da177e4SLinus Torvalds { 247590a3e375SMiaohe Lin pte_t *pte, *mapped_pte; 2476c74df32cSHugh Dickins spinlock_t *ptl; 247742e4089cSAndi Kleen int err = 0; 24781da177e4SLinus Torvalds 247990a3e375SMiaohe Lin mapped_pte = pte = pte_alloc_map_lock(mm, pmd, addr, &ptl); 24801da177e4SLinus Torvalds if (!pte) 24811da177e4SLinus Torvalds return -ENOMEM; 24826606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 24831da177e4SLinus Torvalds do { 2484c33c7948SRyan Roberts BUG_ON(!pte_none(ptep_get(pte))); 248542e4089cSAndi Kleen if (!pfn_modify_allowed(pfn, prot)) { 248642e4089cSAndi Kleen err = -EACCES; 248742e4089cSAndi Kleen break; 248842e4089cSAndi Kleen } 24897e675137SNick Piggin set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot))); 24901da177e4SLinus Torvalds pfn++; 24911da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 24926606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 249390a3e375SMiaohe Lin pte_unmap_unlock(mapped_pte, ptl); 249442e4089cSAndi Kleen return err; 24951da177e4SLinus Torvalds } 24961da177e4SLinus Torvalds 24971da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud, 24981da177e4SLinus Torvalds unsigned long addr, unsigned long end, 24991da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 25001da177e4SLinus Torvalds { 25011da177e4SLinus Torvalds pmd_t *pmd; 25021da177e4SLinus Torvalds unsigned long next; 250342e4089cSAndi Kleen int err; 25041da177e4SLinus Torvalds 25051da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 25061da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, addr); 25071da177e4SLinus Torvalds if (!pmd) 25081da177e4SLinus Torvalds return -ENOMEM; 2509f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 25101da177e4SLinus Torvalds do { 25111da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 251242e4089cSAndi Kleen err = remap_pte_range(mm, pmd, addr, next, 251342e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 251442e4089cSAndi Kleen if (err) 251542e4089cSAndi Kleen return err; 25161da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 25171da177e4SLinus Torvalds return 0; 25181da177e4SLinus Torvalds } 25191da177e4SLinus Torvalds 2520c2febafcSKirill A. Shutemov static inline int remap_pud_range(struct mm_struct *mm, p4d_t *p4d, 25211da177e4SLinus Torvalds unsigned long addr, unsigned long end, 25221da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 25231da177e4SLinus Torvalds { 25241da177e4SLinus Torvalds pud_t *pud; 25251da177e4SLinus Torvalds unsigned long next; 252642e4089cSAndi Kleen int err; 25271da177e4SLinus Torvalds 25281da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 2529c2febafcSKirill A. Shutemov pud = pud_alloc(mm, p4d, addr); 25301da177e4SLinus Torvalds if (!pud) 25311da177e4SLinus Torvalds return -ENOMEM; 25321da177e4SLinus Torvalds do { 25331da177e4SLinus Torvalds next = pud_addr_end(addr, end); 253442e4089cSAndi Kleen err = remap_pmd_range(mm, pud, addr, next, 253542e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 253642e4089cSAndi Kleen if (err) 253742e4089cSAndi Kleen return err; 25381da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 25391da177e4SLinus Torvalds return 0; 25401da177e4SLinus Torvalds } 25411da177e4SLinus Torvalds 2542c2febafcSKirill A. Shutemov static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd, 2543c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 2544c2febafcSKirill A. Shutemov unsigned long pfn, pgprot_t prot) 2545c2febafcSKirill A. Shutemov { 2546c2febafcSKirill A. Shutemov p4d_t *p4d; 2547c2febafcSKirill A. Shutemov unsigned long next; 254842e4089cSAndi Kleen int err; 2549c2febafcSKirill A. Shutemov 2550c2febafcSKirill A. Shutemov pfn -= addr >> PAGE_SHIFT; 2551c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, addr); 2552c2febafcSKirill A. Shutemov if (!p4d) 2553c2febafcSKirill A. Shutemov return -ENOMEM; 2554c2febafcSKirill A. Shutemov do { 2555c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 255642e4089cSAndi Kleen err = remap_pud_range(mm, p4d, addr, next, 255742e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 255842e4089cSAndi Kleen if (err) 255942e4089cSAndi Kleen return err; 2560c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 2561c2febafcSKirill A. Shutemov return 0; 2562c2febafcSKirill A. Shutemov } 2563c2febafcSKirill A. Shutemov 256474ffa5a3SChristoph Hellwig /* 256574ffa5a3SChristoph Hellwig * Variant of remap_pfn_range that does not call track_pfn_remap. The caller 256674ffa5a3SChristoph Hellwig * must have pre-validated the caching bits of the pgprot_t. 2567bfa5bf6dSRolf Eike Beer */ 256874ffa5a3SChristoph Hellwig int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr, 25691da177e4SLinus Torvalds unsigned long pfn, unsigned long size, pgprot_t prot) 25701da177e4SLinus Torvalds { 25711da177e4SLinus Torvalds pgd_t *pgd; 25721da177e4SLinus Torvalds unsigned long next; 25732d15cab8SHugh Dickins unsigned long end = addr + PAGE_ALIGN(size); 25741da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 25751da177e4SLinus Torvalds int err; 25761da177e4SLinus Torvalds 25770c4123e3SAlex Zhang if (WARN_ON_ONCE(!PAGE_ALIGNED(addr))) 25780c4123e3SAlex Zhang return -EINVAL; 25790c4123e3SAlex Zhang 25801da177e4SLinus Torvalds /* 25811da177e4SLinus Torvalds * Physically remapped pages are special. Tell the 25821da177e4SLinus Torvalds * rest of the world about it: 25831da177e4SLinus Torvalds * VM_IO tells people not to look at these pages 25841da177e4SLinus Torvalds * (accesses can have side effects). 25856aab341eSLinus Torvalds * VM_PFNMAP tells the core MM that the base pages are just 25866aab341eSLinus Torvalds * raw PFN mappings, and do not have a "struct page" associated 25876aab341eSLinus Torvalds * with them. 2588314e51b9SKonstantin Khlebnikov * VM_DONTEXPAND 2589314e51b9SKonstantin Khlebnikov * Disable vma merging and expanding with mremap(). 2590314e51b9SKonstantin Khlebnikov * VM_DONTDUMP 2591314e51b9SKonstantin Khlebnikov * Omit vma from core dump, even when VM_IO turned off. 2592fb155c16SLinus Torvalds * 2593fb155c16SLinus Torvalds * There's a horrible special case to handle copy-on-write 2594fb155c16SLinus Torvalds * behaviour that some programs depend on. We mark the "original" 2595fb155c16SLinus Torvalds * un-COW'ed pages by matching them up with "vma->vm_pgoff". 2596b3b9c293SKonstantin Khlebnikov * See vm_normal_page() for details. 25971da177e4SLinus Torvalds */ 2598b3b9c293SKonstantin Khlebnikov if (is_cow_mapping(vma->vm_flags)) { 2599b3b9c293SKonstantin Khlebnikov if (addr != vma->vm_start || end != vma->vm_end) 2600b3b9c293SKonstantin Khlebnikov return -EINVAL; 26016aab341eSLinus Torvalds vma->vm_pgoff = pfn; 2602b3b9c293SKonstantin Khlebnikov } 2603b3b9c293SKonstantin Khlebnikov 26041c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP); 26051da177e4SLinus Torvalds 26061da177e4SLinus Torvalds BUG_ON(addr >= end); 26071da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 26081da177e4SLinus Torvalds pgd = pgd_offset(mm, addr); 26091da177e4SLinus Torvalds flush_cache_range(vma, addr, end); 26101da177e4SLinus Torvalds do { 26111da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 2612c2febafcSKirill A. Shutemov err = remap_p4d_range(mm, pgd, addr, next, 26131da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot); 26141da177e4SLinus Torvalds if (err) 261574ffa5a3SChristoph Hellwig return err; 26161da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 26172ab64037Svenkatesh.pallipadi@intel.com 261874ffa5a3SChristoph Hellwig return 0; 261974ffa5a3SChristoph Hellwig } 26202ab64037Svenkatesh.pallipadi@intel.com 262174ffa5a3SChristoph Hellwig /** 262274ffa5a3SChristoph Hellwig * remap_pfn_range - remap kernel memory to userspace 262374ffa5a3SChristoph Hellwig * @vma: user vma to map to 262474ffa5a3SChristoph Hellwig * @addr: target page aligned user address to start at 262574ffa5a3SChristoph Hellwig * @pfn: page frame number of kernel physical memory address 262674ffa5a3SChristoph Hellwig * @size: size of mapping area 262774ffa5a3SChristoph Hellwig * @prot: page protection flags for this mapping 262874ffa5a3SChristoph Hellwig * 262974ffa5a3SChristoph Hellwig * Note: this is only safe if the mm semaphore is held when called. 263074ffa5a3SChristoph Hellwig * 263174ffa5a3SChristoph Hellwig * Return: %0 on success, negative error code otherwise. 263274ffa5a3SChristoph Hellwig */ 263374ffa5a3SChristoph Hellwig int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 263474ffa5a3SChristoph Hellwig unsigned long pfn, unsigned long size, pgprot_t prot) 263574ffa5a3SChristoph Hellwig { 263674ffa5a3SChristoph Hellwig int err; 263774ffa5a3SChristoph Hellwig 263874ffa5a3SChristoph Hellwig err = track_pfn_remap(vma, &prot, pfn, addr, PAGE_ALIGN(size)); 263974ffa5a3SChristoph Hellwig if (err) 264074ffa5a3SChristoph Hellwig return -EINVAL; 264174ffa5a3SChristoph Hellwig 264274ffa5a3SChristoph Hellwig err = remap_pfn_range_notrack(vma, addr, pfn, size, prot); 264374ffa5a3SChristoph Hellwig if (err) 264468f48381SSuren Baghdasaryan untrack_pfn(vma, pfn, PAGE_ALIGN(size), true); 26451da177e4SLinus Torvalds return err; 26461da177e4SLinus Torvalds } 26471da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range); 26481da177e4SLinus Torvalds 2649b4cbb197SLinus Torvalds /** 2650b4cbb197SLinus Torvalds * vm_iomap_memory - remap memory to userspace 2651b4cbb197SLinus Torvalds * @vma: user vma to map to 2652abd69b9eSWang Wenhu * @start: start of the physical memory to be mapped 2653b4cbb197SLinus Torvalds * @len: size of area 2654b4cbb197SLinus Torvalds * 2655b4cbb197SLinus Torvalds * This is a simplified io_remap_pfn_range() for common driver use. The 2656b4cbb197SLinus Torvalds * driver just needs to give us the physical memory range to be mapped, 2657b4cbb197SLinus Torvalds * we'll figure out the rest from the vma information. 2658b4cbb197SLinus Torvalds * 2659b4cbb197SLinus Torvalds * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get 2660b4cbb197SLinus Torvalds * whatever write-combining details or similar. 2661a862f68aSMike Rapoport * 2662a862f68aSMike Rapoport * Return: %0 on success, negative error code otherwise. 2663b4cbb197SLinus Torvalds */ 2664b4cbb197SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len) 2665b4cbb197SLinus Torvalds { 2666b4cbb197SLinus Torvalds unsigned long vm_len, pfn, pages; 2667b4cbb197SLinus Torvalds 2668b4cbb197SLinus Torvalds /* Check that the physical memory area passed in looks valid */ 2669b4cbb197SLinus Torvalds if (start + len < start) 2670b4cbb197SLinus Torvalds return -EINVAL; 2671b4cbb197SLinus Torvalds /* 2672b4cbb197SLinus Torvalds * You *really* shouldn't map things that aren't page-aligned, 2673b4cbb197SLinus Torvalds * but we've historically allowed it because IO memory might 2674b4cbb197SLinus Torvalds * just have smaller alignment. 2675b4cbb197SLinus Torvalds */ 2676b4cbb197SLinus Torvalds len += start & ~PAGE_MASK; 2677b4cbb197SLinus Torvalds pfn = start >> PAGE_SHIFT; 2678b4cbb197SLinus Torvalds pages = (len + ~PAGE_MASK) >> PAGE_SHIFT; 2679b4cbb197SLinus Torvalds if (pfn + pages < pfn) 2680b4cbb197SLinus Torvalds return -EINVAL; 2681b4cbb197SLinus Torvalds 2682b4cbb197SLinus Torvalds /* We start the mapping 'vm_pgoff' pages into the area */ 2683b4cbb197SLinus Torvalds if (vma->vm_pgoff > pages) 2684b4cbb197SLinus Torvalds return -EINVAL; 2685b4cbb197SLinus Torvalds pfn += vma->vm_pgoff; 2686b4cbb197SLinus Torvalds pages -= vma->vm_pgoff; 2687b4cbb197SLinus Torvalds 2688b4cbb197SLinus Torvalds /* Can we fit all of the mapping? */ 2689b4cbb197SLinus Torvalds vm_len = vma->vm_end - vma->vm_start; 2690b4cbb197SLinus Torvalds if (vm_len >> PAGE_SHIFT > pages) 2691b4cbb197SLinus Torvalds return -EINVAL; 2692b4cbb197SLinus Torvalds 2693b4cbb197SLinus Torvalds /* Ok, let it rip */ 2694b4cbb197SLinus Torvalds return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot); 2695b4cbb197SLinus Torvalds } 2696b4cbb197SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory); 2697b4cbb197SLinus Torvalds 2698aee16b3cSJeremy Fitzhardinge static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd, 2699aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2700e80d3909SJoerg Roedel pte_fn_t fn, void *data, bool create, 2701e80d3909SJoerg Roedel pgtbl_mod_mask *mask) 2702aee16b3cSJeremy Fitzhardinge { 27038abb50c7SMiaohe Lin pte_t *pte, *mapped_pte; 2704be1db475SDaniel Axtens int err = 0; 27053f649ab7SKees Cook spinlock_t *ptl; 2706aee16b3cSJeremy Fitzhardinge 2707be1db475SDaniel Axtens if (create) { 27088abb50c7SMiaohe Lin mapped_pte = pte = (mm == &init_mm) ? 2709e80d3909SJoerg Roedel pte_alloc_kernel_track(pmd, addr, mask) : 2710aee16b3cSJeremy Fitzhardinge pte_alloc_map_lock(mm, pmd, addr, &ptl); 2711aee16b3cSJeremy Fitzhardinge if (!pte) 2712aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2713be1db475SDaniel Axtens } else { 27148abb50c7SMiaohe Lin mapped_pte = pte = (mm == &init_mm) ? 2715be1db475SDaniel Axtens pte_offset_kernel(pmd, addr) : 2716be1db475SDaniel Axtens pte_offset_map_lock(mm, pmd, addr, &ptl); 27173db82b93SHugh Dickins if (!pte) 27183db82b93SHugh Dickins return -EINVAL; 2719be1db475SDaniel Axtens } 2720aee16b3cSJeremy Fitzhardinge 272138e0edb1SJeremy Fitzhardinge arch_enter_lazy_mmu_mode(); 272238e0edb1SJeremy Fitzhardinge 2723eeb4a05fSChristoph Hellwig if (fn) { 2724aee16b3cSJeremy Fitzhardinge do { 2725c33c7948SRyan Roberts if (create || !pte_none(ptep_get(pte))) { 27268b1e0f81SAnshuman Khandual err = fn(pte++, addr, data); 2727aee16b3cSJeremy Fitzhardinge if (err) 2728aee16b3cSJeremy Fitzhardinge break; 2729be1db475SDaniel Axtens } 2730c36987e2SDaisuke Nishimura } while (addr += PAGE_SIZE, addr != end); 2731eeb4a05fSChristoph Hellwig } 2732e80d3909SJoerg Roedel *mask |= PGTBL_PTE_MODIFIED; 2733aee16b3cSJeremy Fitzhardinge 273438e0edb1SJeremy Fitzhardinge arch_leave_lazy_mmu_mode(); 273538e0edb1SJeremy Fitzhardinge 2736aee16b3cSJeremy Fitzhardinge if (mm != &init_mm) 27378abb50c7SMiaohe Lin pte_unmap_unlock(mapped_pte, ptl); 2738aee16b3cSJeremy Fitzhardinge return err; 2739aee16b3cSJeremy Fitzhardinge } 2740aee16b3cSJeremy Fitzhardinge 2741aee16b3cSJeremy Fitzhardinge static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud, 2742aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2743e80d3909SJoerg Roedel pte_fn_t fn, void *data, bool create, 2744e80d3909SJoerg Roedel pgtbl_mod_mask *mask) 2745aee16b3cSJeremy Fitzhardinge { 2746aee16b3cSJeremy Fitzhardinge pmd_t *pmd; 2747aee16b3cSJeremy Fitzhardinge unsigned long next; 2748be1db475SDaniel Axtens int err = 0; 2749aee16b3cSJeremy Fitzhardinge 2750ceb86879SAndi Kleen BUG_ON(pud_huge(*pud)); 2751ceb86879SAndi Kleen 2752be1db475SDaniel Axtens if (create) { 2753e80d3909SJoerg Roedel pmd = pmd_alloc_track(mm, pud, addr, mask); 2754aee16b3cSJeremy Fitzhardinge if (!pmd) 2755aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2756be1db475SDaniel Axtens } else { 2757be1db475SDaniel Axtens pmd = pmd_offset(pud, addr); 2758be1db475SDaniel Axtens } 2759aee16b3cSJeremy Fitzhardinge do { 2760aee16b3cSJeremy Fitzhardinge next = pmd_addr_end(addr, end); 27610c95cba4SNicholas Piggin if (pmd_none(*pmd) && !create) 27620c95cba4SNicholas Piggin continue; 27630c95cba4SNicholas Piggin if (WARN_ON_ONCE(pmd_leaf(*pmd))) 27640c95cba4SNicholas Piggin return -EINVAL; 27650c95cba4SNicholas Piggin if (!pmd_none(*pmd) && WARN_ON_ONCE(pmd_bad(*pmd))) { 27660c95cba4SNicholas Piggin if (!create) 27670c95cba4SNicholas Piggin continue; 27680c95cba4SNicholas Piggin pmd_clear_bad(pmd); 27690c95cba4SNicholas Piggin } 27700c95cba4SNicholas Piggin err = apply_to_pte_range(mm, pmd, addr, next, 27710c95cba4SNicholas Piggin fn, data, create, mask); 2772aee16b3cSJeremy Fitzhardinge if (err) 2773aee16b3cSJeremy Fitzhardinge break; 2774aee16b3cSJeremy Fitzhardinge } while (pmd++, addr = next, addr != end); 27750c95cba4SNicholas Piggin 2776aee16b3cSJeremy Fitzhardinge return err; 2777aee16b3cSJeremy Fitzhardinge } 2778aee16b3cSJeremy Fitzhardinge 2779c2febafcSKirill A. Shutemov static int apply_to_pud_range(struct mm_struct *mm, p4d_t *p4d, 2780aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2781e80d3909SJoerg Roedel pte_fn_t fn, void *data, bool create, 2782e80d3909SJoerg Roedel pgtbl_mod_mask *mask) 2783aee16b3cSJeremy Fitzhardinge { 2784aee16b3cSJeremy Fitzhardinge pud_t *pud; 2785aee16b3cSJeremy Fitzhardinge unsigned long next; 2786be1db475SDaniel Axtens int err = 0; 2787aee16b3cSJeremy Fitzhardinge 2788be1db475SDaniel Axtens if (create) { 2789e80d3909SJoerg Roedel pud = pud_alloc_track(mm, p4d, addr, mask); 2790aee16b3cSJeremy Fitzhardinge if (!pud) 2791aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2792be1db475SDaniel Axtens } else { 2793be1db475SDaniel Axtens pud = pud_offset(p4d, addr); 2794be1db475SDaniel Axtens } 2795aee16b3cSJeremy Fitzhardinge do { 2796aee16b3cSJeremy Fitzhardinge next = pud_addr_end(addr, end); 27970c95cba4SNicholas Piggin if (pud_none(*pud) && !create) 27980c95cba4SNicholas Piggin continue; 27990c95cba4SNicholas Piggin if (WARN_ON_ONCE(pud_leaf(*pud))) 28000c95cba4SNicholas Piggin return -EINVAL; 28010c95cba4SNicholas Piggin if (!pud_none(*pud) && WARN_ON_ONCE(pud_bad(*pud))) { 28020c95cba4SNicholas Piggin if (!create) 28030c95cba4SNicholas Piggin continue; 28040c95cba4SNicholas Piggin pud_clear_bad(pud); 28050c95cba4SNicholas Piggin } 28060c95cba4SNicholas Piggin err = apply_to_pmd_range(mm, pud, addr, next, 28070c95cba4SNicholas Piggin fn, data, create, mask); 2808aee16b3cSJeremy Fitzhardinge if (err) 2809aee16b3cSJeremy Fitzhardinge break; 2810aee16b3cSJeremy Fitzhardinge } while (pud++, addr = next, addr != end); 28110c95cba4SNicholas Piggin 2812aee16b3cSJeremy Fitzhardinge return err; 2813aee16b3cSJeremy Fitzhardinge } 2814aee16b3cSJeremy Fitzhardinge 2815c2febafcSKirill A. Shutemov static int apply_to_p4d_range(struct mm_struct *mm, pgd_t *pgd, 2816c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 2817e80d3909SJoerg Roedel pte_fn_t fn, void *data, bool create, 2818e80d3909SJoerg Roedel pgtbl_mod_mask *mask) 2819c2febafcSKirill A. Shutemov { 2820c2febafcSKirill A. Shutemov p4d_t *p4d; 2821c2febafcSKirill A. Shutemov unsigned long next; 2822be1db475SDaniel Axtens int err = 0; 2823c2febafcSKirill A. Shutemov 2824be1db475SDaniel Axtens if (create) { 2825e80d3909SJoerg Roedel p4d = p4d_alloc_track(mm, pgd, addr, mask); 2826c2febafcSKirill A. Shutemov if (!p4d) 2827c2febafcSKirill A. Shutemov return -ENOMEM; 2828be1db475SDaniel Axtens } else { 2829be1db475SDaniel Axtens p4d = p4d_offset(pgd, addr); 2830be1db475SDaniel Axtens } 2831c2febafcSKirill A. Shutemov do { 2832c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 28330c95cba4SNicholas Piggin if (p4d_none(*p4d) && !create) 28340c95cba4SNicholas Piggin continue; 28350c95cba4SNicholas Piggin if (WARN_ON_ONCE(p4d_leaf(*p4d))) 28360c95cba4SNicholas Piggin return -EINVAL; 28370c95cba4SNicholas Piggin if (!p4d_none(*p4d) && WARN_ON_ONCE(p4d_bad(*p4d))) { 28380c95cba4SNicholas Piggin if (!create) 28390c95cba4SNicholas Piggin continue; 28400c95cba4SNicholas Piggin p4d_clear_bad(p4d); 28410c95cba4SNicholas Piggin } 28420c95cba4SNicholas Piggin err = apply_to_pud_range(mm, p4d, addr, next, 28430c95cba4SNicholas Piggin fn, data, create, mask); 2844c2febafcSKirill A. Shutemov if (err) 2845c2febafcSKirill A. Shutemov break; 2846c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 28470c95cba4SNicholas Piggin 2848c2febafcSKirill A. Shutemov return err; 2849c2febafcSKirill A. Shutemov } 2850c2febafcSKirill A. Shutemov 2851be1db475SDaniel Axtens static int __apply_to_page_range(struct mm_struct *mm, unsigned long addr, 2852be1db475SDaniel Axtens unsigned long size, pte_fn_t fn, 2853be1db475SDaniel Axtens void *data, bool create) 2854be1db475SDaniel Axtens { 2855be1db475SDaniel Axtens pgd_t *pgd; 2856e80d3909SJoerg Roedel unsigned long start = addr, next; 2857be1db475SDaniel Axtens unsigned long end = addr + size; 2858e80d3909SJoerg Roedel pgtbl_mod_mask mask = 0; 2859be1db475SDaniel Axtens int err = 0; 2860be1db475SDaniel Axtens 2861be1db475SDaniel Axtens if (WARN_ON(addr >= end)) 2862be1db475SDaniel Axtens return -EINVAL; 2863be1db475SDaniel Axtens 2864be1db475SDaniel Axtens pgd = pgd_offset(mm, addr); 2865be1db475SDaniel Axtens do { 2866be1db475SDaniel Axtens next = pgd_addr_end(addr, end); 28670c95cba4SNicholas Piggin if (pgd_none(*pgd) && !create) 2868be1db475SDaniel Axtens continue; 28690c95cba4SNicholas Piggin if (WARN_ON_ONCE(pgd_leaf(*pgd))) 28700c95cba4SNicholas Piggin return -EINVAL; 28710c95cba4SNicholas Piggin if (!pgd_none(*pgd) && WARN_ON_ONCE(pgd_bad(*pgd))) { 28720c95cba4SNicholas Piggin if (!create) 28730c95cba4SNicholas Piggin continue; 28740c95cba4SNicholas Piggin pgd_clear_bad(pgd); 28750c95cba4SNicholas Piggin } 28760c95cba4SNicholas Piggin err = apply_to_p4d_range(mm, pgd, addr, next, 28770c95cba4SNicholas Piggin fn, data, create, &mask); 2878be1db475SDaniel Axtens if (err) 2879be1db475SDaniel Axtens break; 2880be1db475SDaniel Axtens } while (pgd++, addr = next, addr != end); 2881be1db475SDaniel Axtens 2882e80d3909SJoerg Roedel if (mask & ARCH_PAGE_TABLE_SYNC_MASK) 2883e80d3909SJoerg Roedel arch_sync_kernel_mappings(start, start + size); 2884e80d3909SJoerg Roedel 2885be1db475SDaniel Axtens return err; 2886be1db475SDaniel Axtens } 2887be1db475SDaniel Axtens 2888aee16b3cSJeremy Fitzhardinge /* 2889aee16b3cSJeremy Fitzhardinge * Scan a region of virtual memory, filling in page tables as necessary 2890aee16b3cSJeremy Fitzhardinge * and calling a provided function on each leaf page table. 2891aee16b3cSJeremy Fitzhardinge */ 2892aee16b3cSJeremy Fitzhardinge int apply_to_page_range(struct mm_struct *mm, unsigned long addr, 2893aee16b3cSJeremy Fitzhardinge unsigned long size, pte_fn_t fn, void *data) 2894aee16b3cSJeremy Fitzhardinge { 2895be1db475SDaniel Axtens return __apply_to_page_range(mm, addr, size, fn, data, true); 2896aee16b3cSJeremy Fitzhardinge } 2897aee16b3cSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(apply_to_page_range); 2898aee16b3cSJeremy Fitzhardinge 28991da177e4SLinus Torvalds /* 2900be1db475SDaniel Axtens * Scan a region of virtual memory, calling a provided function on 2901be1db475SDaniel Axtens * each leaf page table where it exists. 2902be1db475SDaniel Axtens * 2903be1db475SDaniel Axtens * Unlike apply_to_page_range, this does _not_ fill in page tables 2904be1db475SDaniel Axtens * where they are absent. 2905be1db475SDaniel Axtens */ 2906be1db475SDaniel Axtens int apply_to_existing_page_range(struct mm_struct *mm, unsigned long addr, 2907be1db475SDaniel Axtens unsigned long size, pte_fn_t fn, void *data) 2908be1db475SDaniel Axtens { 2909be1db475SDaniel Axtens return __apply_to_page_range(mm, addr, size, fn, data, false); 2910be1db475SDaniel Axtens } 2911be1db475SDaniel Axtens EXPORT_SYMBOL_GPL(apply_to_existing_page_range); 2912be1db475SDaniel Axtens 2913be1db475SDaniel Axtens /* 29149b4bdd2fSKirill A. Shutemov * handle_pte_fault chooses page fault handler according to an entry which was 29159b4bdd2fSKirill A. Shutemov * read non-atomically. Before making any commitment, on those architectures 29169b4bdd2fSKirill A. Shutemov * or configurations (e.g. i386 with PAE) which might give a mix of unmatched 29179b4bdd2fSKirill A. Shutemov * parts, do_swap_page must check under lock before unmapping the pte and 29189b4bdd2fSKirill A. Shutemov * proceeding (but do_wp_page is only called after already making such a check; 2919a335b2e1SRyota Ozaki * and do_anonymous_page can safely check later on). 29208f4e2101SHugh Dickins */ 29212ca99358SPeter Xu static inline int pte_unmap_same(struct vm_fault *vmf) 29228f4e2101SHugh Dickins { 29238f4e2101SHugh Dickins int same = 1; 2924923717cbSThomas Gleixner #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPTION) 29258f4e2101SHugh Dickins if (sizeof(pte_t) > sizeof(unsigned long)) { 2926c7ad0880SHugh Dickins spin_lock(vmf->ptl); 2927c33c7948SRyan Roberts same = pte_same(ptep_get(vmf->pte), vmf->orig_pte); 2928c7ad0880SHugh Dickins spin_unlock(vmf->ptl); 29298f4e2101SHugh Dickins } 29308f4e2101SHugh Dickins #endif 29312ca99358SPeter Xu pte_unmap(vmf->pte); 29322ca99358SPeter Xu vmf->pte = NULL; 29338f4e2101SHugh Dickins return same; 29348f4e2101SHugh Dickins } 29358f4e2101SHugh Dickins 2936a873dfe1STony Luck /* 2937a873dfe1STony Luck * Return: 2938a873dfe1STony Luck * 0: copied succeeded 2939a873dfe1STony Luck * -EHWPOISON: copy failed due to hwpoison in source page 2940a873dfe1STony Luck * -EAGAIN: copied failed (some other reason) 2941a873dfe1STony Luck */ 2942a873dfe1STony Luck static inline int __wp_page_copy_user(struct page *dst, struct page *src, 294383d116c5SJia He struct vm_fault *vmf) 29446aab341eSLinus Torvalds { 2945a873dfe1STony Luck int ret; 294683d116c5SJia He void *kaddr; 294783d116c5SJia He void __user *uaddr; 294883d116c5SJia He struct vm_area_struct *vma = vmf->vma; 294983d116c5SJia He struct mm_struct *mm = vma->vm_mm; 295083d116c5SJia He unsigned long addr = vmf->address; 295183d116c5SJia He 295283d116c5SJia He if (likely(src)) { 2953d302c239STony Luck if (copy_mc_user_highpage(dst, src, addr, vma)) { 2954d302c239STony Luck memory_failure_queue(page_to_pfn(src), 0); 2955a873dfe1STony Luck return -EHWPOISON; 2956d302c239STony Luck } 2957a873dfe1STony Luck return 0; 295883d116c5SJia He } 295983d116c5SJia He 29606aab341eSLinus Torvalds /* 29616aab341eSLinus Torvalds * If the source page was a PFN mapping, we don't have 29626aab341eSLinus Torvalds * a "struct page" for it. We do a best-effort copy by 29636aab341eSLinus Torvalds * just copying from the original user address. If that 29646aab341eSLinus Torvalds * fails, we just zero-fill it. Live with it. 29656aab341eSLinus Torvalds */ 296624d2613aSFabio M. De Francesco kaddr = kmap_local_page(dst); 296724d2613aSFabio M. De Francesco pagefault_disable(); 296883d116c5SJia He uaddr = (void __user *)(addr & PAGE_MASK); 296983d116c5SJia He 297083d116c5SJia He /* 297183d116c5SJia He * On architectures with software "accessed" bits, we would 297283d116c5SJia He * take a double page fault, so mark it accessed here. 297383d116c5SJia He */ 29743db82b93SHugh Dickins vmf->pte = NULL; 2975e1fd09e3SYu Zhao if (!arch_has_hw_pte_young() && !pte_young(vmf->orig_pte)) { 297683d116c5SJia He pte_t entry; 297783d116c5SJia He 297883d116c5SJia He vmf->pte = pte_offset_map_lock(mm, vmf->pmd, addr, &vmf->ptl); 2979c33c7948SRyan Roberts if (unlikely(!vmf->pte || !pte_same(ptep_get(vmf->pte), vmf->orig_pte))) { 298083d116c5SJia He /* 298183d116c5SJia He * Other thread has already handled the fault 29827df67697SBibo Mao * and update local tlb only 298383d116c5SJia He */ 2984a92cbb82SHugh Dickins if (vmf->pte) 29857df67697SBibo Mao update_mmu_tlb(vma, addr, vmf->pte); 2986a873dfe1STony Luck ret = -EAGAIN; 298783d116c5SJia He goto pte_unlock; 298883d116c5SJia He } 298983d116c5SJia He 299083d116c5SJia He entry = pte_mkyoung(vmf->orig_pte); 299183d116c5SJia He if (ptep_set_access_flags(vma, addr, vmf->pte, entry, 0)) 29925003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vma, addr, vmf->pte, 1); 299383d116c5SJia He } 29945d2a2dbbSLinus Torvalds 29955d2a2dbbSLinus Torvalds /* 29965d2a2dbbSLinus Torvalds * This really shouldn't fail, because the page is there 29975d2a2dbbSLinus Torvalds * in the page tables. But it might just be unreadable, 29985d2a2dbbSLinus Torvalds * in which case we just give up and fill the result with 29995d2a2dbbSLinus Torvalds * zeroes. 30005d2a2dbbSLinus Torvalds */ 300183d116c5SJia He if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) { 30023db82b93SHugh Dickins if (vmf->pte) 3003c3e5ea6eSKirill A. Shutemov goto warn; 3004c3e5ea6eSKirill A. Shutemov 3005c3e5ea6eSKirill A. Shutemov /* Re-validate under PTL if the page is still mapped */ 3006c3e5ea6eSKirill A. Shutemov vmf->pte = pte_offset_map_lock(mm, vmf->pmd, addr, &vmf->ptl); 3007c33c7948SRyan Roberts if (unlikely(!vmf->pte || !pte_same(ptep_get(vmf->pte), vmf->orig_pte))) { 30087df67697SBibo Mao /* The PTE changed under us, update local tlb */ 3009a92cbb82SHugh Dickins if (vmf->pte) 30107df67697SBibo Mao update_mmu_tlb(vma, addr, vmf->pte); 3011a873dfe1STony Luck ret = -EAGAIN; 3012c3e5ea6eSKirill A. Shutemov goto pte_unlock; 3013c3e5ea6eSKirill A. Shutemov } 3014c3e5ea6eSKirill A. Shutemov 3015c3e5ea6eSKirill A. Shutemov /* 3016985ba004SEthon Paul * The same page can be mapped back since last copy attempt. 3017c3e5ea6eSKirill A. Shutemov * Try to copy again under PTL. 3018c3e5ea6eSKirill A. Shutemov */ 3019c3e5ea6eSKirill A. Shutemov if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) { 302083d116c5SJia He /* 302183d116c5SJia He * Give a warn in case there can be some obscure 302283d116c5SJia He * use-case 302383d116c5SJia He */ 3024c3e5ea6eSKirill A. Shutemov warn: 302583d116c5SJia He WARN_ON_ONCE(1); 30263ecb01dfSJan Beulich clear_page(kaddr); 302783d116c5SJia He } 3028c3e5ea6eSKirill A. Shutemov } 302983d116c5SJia He 3030a873dfe1STony Luck ret = 0; 303183d116c5SJia He 303283d116c5SJia He pte_unlock: 30333db82b93SHugh Dickins if (vmf->pte) 303483d116c5SJia He pte_unmap_unlock(vmf->pte, vmf->ptl); 303524d2613aSFabio M. De Francesco pagefault_enable(); 303624d2613aSFabio M. De Francesco kunmap_local(kaddr); 3037c4ec7b0dSDmitriy Monakhov flush_dcache_page(dst); 303883d116c5SJia He 303983d116c5SJia He return ret; 30406aab341eSLinus Torvalds } 30416aab341eSLinus Torvalds 3042c20cd45eSMichal Hocko static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma) 3043c20cd45eSMichal Hocko { 3044c20cd45eSMichal Hocko struct file *vm_file = vma->vm_file; 3045c20cd45eSMichal Hocko 3046c20cd45eSMichal Hocko if (vm_file) 3047c20cd45eSMichal Hocko return mapping_gfp_mask(vm_file->f_mapping) | __GFP_FS | __GFP_IO; 3048c20cd45eSMichal Hocko 3049c20cd45eSMichal Hocko /* 3050c20cd45eSMichal Hocko * Special mappings (e.g. VDSO) do not have any file so fake 3051c20cd45eSMichal Hocko * a default GFP_KERNEL for them. 3052c20cd45eSMichal Hocko */ 3053c20cd45eSMichal Hocko return GFP_KERNEL; 3054c20cd45eSMichal Hocko } 3055c20cd45eSMichal Hocko 30561da177e4SLinus Torvalds /* 3057fb09a464SKirill A. Shutemov * Notify the address space that the page is about to become writable so that 3058fb09a464SKirill A. Shutemov * it can prohibit this or wait for the page to get into an appropriate state. 3059fb09a464SKirill A. Shutemov * 3060fb09a464SKirill A. Shutemov * We do this without the lock held, so that it can sleep if it needs to. 3061fb09a464SKirill A. Shutemov */ 306286aa6998SSidhartha Kumar static vm_fault_t do_page_mkwrite(struct vm_fault *vmf, struct folio *folio) 3063fb09a464SKirill A. Shutemov { 30642b740303SSouptick Joarder vm_fault_t ret; 306538b8cb7fSJan Kara unsigned int old_flags = vmf->flags; 3066fb09a464SKirill A. Shutemov 306738b8cb7fSJan Kara vmf->flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE; 3068fb09a464SKirill A. Shutemov 3069dc617f29SDarrick J. Wong if (vmf->vma->vm_file && 3070dc617f29SDarrick J. Wong IS_SWAPFILE(vmf->vma->vm_file->f_mapping->host)) 3071dc617f29SDarrick J. Wong return VM_FAULT_SIGBUS; 3072dc617f29SDarrick J. Wong 307311bac800SDave Jiang ret = vmf->vma->vm_ops->page_mkwrite(vmf); 307438b8cb7fSJan Kara /* Restore original flags so that caller is not surprised */ 307538b8cb7fSJan Kara vmf->flags = old_flags; 3076fb09a464SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) 3077fb09a464SKirill A. Shutemov return ret; 3078fb09a464SKirill A. Shutemov if (unlikely(!(ret & VM_FAULT_LOCKED))) { 30793d243659SSidhartha Kumar folio_lock(folio); 30803d243659SSidhartha Kumar if (!folio->mapping) { 30813d243659SSidhartha Kumar folio_unlock(folio); 3082fb09a464SKirill A. Shutemov return 0; /* retry */ 3083fb09a464SKirill A. Shutemov } 3084fb09a464SKirill A. Shutemov ret |= VM_FAULT_LOCKED; 3085fb09a464SKirill A. Shutemov } else 30863d243659SSidhartha Kumar VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); 3087fb09a464SKirill A. Shutemov return ret; 3088fb09a464SKirill A. Shutemov } 3089fb09a464SKirill A. Shutemov 3090fb09a464SKirill A. Shutemov /* 309197ba0c2bSJan Kara * Handle dirtying of a page in shared file mapping on a write fault. 30924e047f89SShachar Raindel * 309397ba0c2bSJan Kara * The function expects the page to be locked and unlocks it. 30944e047f89SShachar Raindel */ 309589b15332SJohannes Weiner static vm_fault_t fault_dirty_shared_page(struct vm_fault *vmf) 30964e047f89SShachar Raindel { 309789b15332SJohannes Weiner struct vm_area_struct *vma = vmf->vma; 30984e047f89SShachar Raindel struct address_space *mapping; 309915b4919aSZhangPeng struct folio *folio = page_folio(vmf->page); 310097ba0c2bSJan Kara bool dirtied; 310197ba0c2bSJan Kara bool page_mkwrite = vma->vm_ops && vma->vm_ops->page_mkwrite; 31024e047f89SShachar Raindel 310315b4919aSZhangPeng dirtied = folio_mark_dirty(folio); 310415b4919aSZhangPeng VM_BUG_ON_FOLIO(folio_test_anon(folio), folio); 310597ba0c2bSJan Kara /* 310615b4919aSZhangPeng * Take a local copy of the address_space - folio.mapping may be zeroed 310715b4919aSZhangPeng * by truncate after folio_unlock(). The address_space itself remains 310815b4919aSZhangPeng * pinned by vma->vm_file's reference. We rely on folio_unlock()'s 310997ba0c2bSJan Kara * release semantics to prevent the compiler from undoing this copying. 311097ba0c2bSJan Kara */ 311115b4919aSZhangPeng mapping = folio_raw_mapping(folio); 311215b4919aSZhangPeng folio_unlock(folio); 31134e047f89SShachar Raindel 31144e047f89SShachar Raindel if (!page_mkwrite) 31154e047f89SShachar Raindel file_update_time(vma->vm_file); 311689b15332SJohannes Weiner 311789b15332SJohannes Weiner /* 311889b15332SJohannes Weiner * Throttle page dirtying rate down to writeback speed. 311989b15332SJohannes Weiner * 312089b15332SJohannes Weiner * mapping may be NULL here because some device drivers do not 312189b15332SJohannes Weiner * set page.mapping but still dirty their pages 312289b15332SJohannes Weiner * 3123c1e8d7c6SMichel Lespinasse * Drop the mmap_lock before waiting on IO, if we can. The file 312489b15332SJohannes Weiner * is pinning the mapping, as per above. 312589b15332SJohannes Weiner */ 312689b15332SJohannes Weiner if ((dirtied || page_mkwrite) && mapping) { 312789b15332SJohannes Weiner struct file *fpin; 312889b15332SJohannes Weiner 312989b15332SJohannes Weiner fpin = maybe_unlock_mmap_for_io(vmf, NULL); 313089b15332SJohannes Weiner balance_dirty_pages_ratelimited(mapping); 313189b15332SJohannes Weiner if (fpin) { 313289b15332SJohannes Weiner fput(fpin); 3133d9272525SPeter Xu return VM_FAULT_COMPLETED; 313489b15332SJohannes Weiner } 313589b15332SJohannes Weiner } 313689b15332SJohannes Weiner 313789b15332SJohannes Weiner return 0; 31384e047f89SShachar Raindel } 31394e047f89SShachar Raindel 314097ba0c2bSJan Kara /* 31414e047f89SShachar Raindel * Handle write page faults for pages that can be reused in the current vma 31424e047f89SShachar Raindel * 31434e047f89SShachar Raindel * This can happen either due to the mapping being with the VM_SHARED flag, 31444e047f89SShachar Raindel * or due to us being the last reference standing to the page. In either 31454e047f89SShachar Raindel * case, all we need to do here is to mark the page as writable and update 31464e047f89SShachar Raindel * any related book-keeping. 31474e047f89SShachar Raindel */ 3148a86bc96bSKefeng Wang static inline void wp_page_reuse(struct vm_fault *vmf, struct folio *folio) 314982b0f8c3SJan Kara __releases(vmf->ptl) 31504e047f89SShachar Raindel { 315182b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 31524e047f89SShachar Raindel pte_t entry; 31536c287605SDavid Hildenbrand 3154c89357e2SDavid Hildenbrand VM_BUG_ON(!(vmf->flags & FAULT_FLAG_WRITE)); 31556c287605SDavid Hildenbrand 3156c2c3b514SKefeng Wang if (folio) { 3157c2c3b514SKefeng Wang VM_BUG_ON(folio_test_anon(folio) && 3158c2c3b514SKefeng Wang !PageAnonExclusive(vmf->page)); 31594e047f89SShachar Raindel /* 3160c2c3b514SKefeng Wang * Clear the folio's cpupid information as the existing 31614e047f89SShachar Raindel * information potentially belongs to a now completely 31624e047f89SShachar Raindel * unrelated process. 31634e047f89SShachar Raindel */ 3164c2c3b514SKefeng Wang folio_xchg_last_cpupid(folio, (1 << LAST_CPUPID_SHIFT) - 1); 3165c2c3b514SKefeng Wang } 31664e047f89SShachar Raindel 31672994302bSJan Kara flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); 31682994302bSJan Kara entry = pte_mkyoung(vmf->orig_pte); 31694e047f89SShachar Raindel entry = maybe_mkwrite(pte_mkdirty(entry), vma); 317082b0f8c3SJan Kara if (ptep_set_access_flags(vma, vmf->address, vmf->pte, entry, 1)) 31715003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vma, vmf->address, vmf->pte, 1); 317282b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3173798a6b87SPeter Xu count_vm_event(PGREUSE); 31744e047f89SShachar Raindel } 31754e047f89SShachar Raindel 31764ed43798SMatthew Wilcox (Oracle) /* 31774ed43798SMatthew Wilcox (Oracle) * We could add a bitflag somewhere, but for now, we know that all 31784ed43798SMatthew Wilcox (Oracle) * vm_ops that have a ->map_pages have been audited and don't need 31794ed43798SMatthew Wilcox (Oracle) * the mmap_lock to be held. 31804ed43798SMatthew Wilcox (Oracle) */ 31814ed43798SMatthew Wilcox (Oracle) static inline vm_fault_t vmf_can_call_fault(const struct vm_fault *vmf) 31824ed43798SMatthew Wilcox (Oracle) { 31834ed43798SMatthew Wilcox (Oracle) struct vm_area_struct *vma = vmf->vma; 31844ed43798SMatthew Wilcox (Oracle) 31854ed43798SMatthew Wilcox (Oracle) if (vma->vm_ops->map_pages || !(vmf->flags & FAULT_FLAG_VMA_LOCK)) 31864ed43798SMatthew Wilcox (Oracle) return 0; 31874ed43798SMatthew Wilcox (Oracle) vma_end_read(vma); 31884ed43798SMatthew Wilcox (Oracle) return VM_FAULT_RETRY; 31894ed43798SMatthew Wilcox (Oracle) } 31904ed43798SMatthew Wilcox (Oracle) 3191164b06f2SMatthew Wilcox (Oracle) static vm_fault_t vmf_anon_prepare(struct vm_fault *vmf) 3192164b06f2SMatthew Wilcox (Oracle) { 3193164b06f2SMatthew Wilcox (Oracle) struct vm_area_struct *vma = vmf->vma; 3194164b06f2SMatthew Wilcox (Oracle) 3195164b06f2SMatthew Wilcox (Oracle) if (likely(vma->anon_vma)) 3196164b06f2SMatthew Wilcox (Oracle) return 0; 3197164b06f2SMatthew Wilcox (Oracle) if (vmf->flags & FAULT_FLAG_VMA_LOCK) { 3198164b06f2SMatthew Wilcox (Oracle) vma_end_read(vma); 3199164b06f2SMatthew Wilcox (Oracle) return VM_FAULT_RETRY; 3200164b06f2SMatthew Wilcox (Oracle) } 3201164b06f2SMatthew Wilcox (Oracle) if (__anon_vma_prepare(vma)) 3202164b06f2SMatthew Wilcox (Oracle) return VM_FAULT_OOM; 3203164b06f2SMatthew Wilcox (Oracle) return 0; 3204164b06f2SMatthew Wilcox (Oracle) } 3205164b06f2SMatthew Wilcox (Oracle) 32064e047f89SShachar Raindel /* 3207c89357e2SDavid Hildenbrand * Handle the case of a page which we actually need to copy to a new page, 3208c89357e2SDavid Hildenbrand * either due to COW or unsharing. 32092f38ab2cSShachar Raindel * 3210c1e8d7c6SMichel Lespinasse * Called with mmap_lock locked and the old page referenced, but 32112f38ab2cSShachar Raindel * without the ptl held. 32122f38ab2cSShachar Raindel * 32132f38ab2cSShachar Raindel * High level logic flow: 32142f38ab2cSShachar Raindel * 32152f38ab2cSShachar Raindel * - Allocate a page, copy the content of the old page to the new one. 32162f38ab2cSShachar Raindel * - Handle book keeping and accounting - cgroups, mmu-notifiers, etc. 32172f38ab2cSShachar Raindel * - Take the PTL. If the pte changed, bail out and release the allocated page 32182f38ab2cSShachar Raindel * - If the pte is still the way we remember it, update the page table and all 32192f38ab2cSShachar Raindel * relevant references. This includes dropping the reference the page-table 32202f38ab2cSShachar Raindel * held to the old page, as well as updating the rmap. 32212f38ab2cSShachar Raindel * - In any case, unlock the PTL and drop the reference we took to the old page. 32222f38ab2cSShachar Raindel */ 32232b740303SSouptick Joarder static vm_fault_t wp_page_copy(struct vm_fault *vmf) 32242f38ab2cSShachar Raindel { 3225c89357e2SDavid Hildenbrand const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE; 322682b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3227bae473a4SKirill A. Shutemov struct mm_struct *mm = vma->vm_mm; 322828d41a48SMatthew Wilcox (Oracle) struct folio *old_folio = NULL; 322928d41a48SMatthew Wilcox (Oracle) struct folio *new_folio = NULL; 32302f38ab2cSShachar Raindel pte_t entry; 32312f38ab2cSShachar Raindel int page_copied = 0; 3232ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 3233164b06f2SMatthew Wilcox (Oracle) vm_fault_t ret; 3234cf503cc6SKefeng Wang bool pfn_is_zero; 32352f38ab2cSShachar Raindel 3236662ce1dcSYang Yang delayacct_wpcopy_start(); 3237662ce1dcSYang Yang 323828d41a48SMatthew Wilcox (Oracle) if (vmf->page) 323928d41a48SMatthew Wilcox (Oracle) old_folio = page_folio(vmf->page); 3240164b06f2SMatthew Wilcox (Oracle) ret = vmf_anon_prepare(vmf); 3241164b06f2SMatthew Wilcox (Oracle) if (unlikely(ret)) 3242164b06f2SMatthew Wilcox (Oracle) goto out; 32432f38ab2cSShachar Raindel 3244cf503cc6SKefeng Wang pfn_is_zero = is_zero_pfn(pte_pfn(vmf->orig_pte)); 3245cf503cc6SKefeng Wang new_folio = folio_prealloc(mm, vma, vmf->address, pfn_is_zero); 32466bc56a4dSMatthew Wilcox (Oracle) if (!new_folio) 32472f38ab2cSShachar Raindel goto oom; 3248cf503cc6SKefeng Wang 3249cf503cc6SKefeng Wang if (!pfn_is_zero) { 3250164b06f2SMatthew Wilcox (Oracle) int err; 325183d116c5SJia He 3252164b06f2SMatthew Wilcox (Oracle) err = __wp_page_copy_user(&new_folio->page, vmf->page, vmf); 3253164b06f2SMatthew Wilcox (Oracle) if (err) { 325483d116c5SJia He /* 325583d116c5SJia He * COW failed, if the fault was solved by other, 325683d116c5SJia He * it's fine. If not, userspace would re-fault on 325783d116c5SJia He * the same address and we will handle the fault 325883d116c5SJia He * from the second attempt. 3259a873dfe1STony Luck * The -EHWPOISON case will not be retried. 326083d116c5SJia He */ 326128d41a48SMatthew Wilcox (Oracle) folio_put(new_folio); 326228d41a48SMatthew Wilcox (Oracle) if (old_folio) 326328d41a48SMatthew Wilcox (Oracle) folio_put(old_folio); 3264662ce1dcSYang Yang 3265662ce1dcSYang Yang delayacct_wpcopy_end(); 3266164b06f2SMatthew Wilcox (Oracle) return err == -EHWPOISON ? VM_FAULT_HWPOISON : 0; 326783d116c5SJia He } 326828d41a48SMatthew Wilcox (Oracle) kmsan_copy_page_meta(&new_folio->page, vmf->page); 32692f38ab2cSShachar Raindel } 32702f38ab2cSShachar Raindel 327128d41a48SMatthew Wilcox (Oracle) __folio_mark_uptodate(new_folio); 3272eb3c24f3SMel Gorman 32737d4a8be0SAlistair Popple mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, 32746f4f13e8SJérôme Glisse vmf->address & PAGE_MASK, 3275ac46d4f3SJérôme Glisse (vmf->address & PAGE_MASK) + PAGE_SIZE); 3276ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 32772f38ab2cSShachar Raindel 32782f38ab2cSShachar Raindel /* 32792f38ab2cSShachar Raindel * Re-check the pte - we dropped the lock 32802f38ab2cSShachar Raindel */ 328182b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(mm, vmf->pmd, vmf->address, &vmf->ptl); 3282c33c7948SRyan Roberts if (likely(vmf->pte && pte_same(ptep_get(vmf->pte), vmf->orig_pte))) { 328328d41a48SMatthew Wilcox (Oracle) if (old_folio) { 328428d41a48SMatthew Wilcox (Oracle) if (!folio_test_anon(old_folio)) { 32856b27cc6cSKefeng Wang dec_mm_counter(mm, mm_counter_file(old_folio)); 3286f1a79412SShakeel Butt inc_mm_counter(mm, MM_ANONPAGES); 32872f38ab2cSShachar Raindel } 32882f38ab2cSShachar Raindel } else { 32896080d19fSxu xin ksm_might_unmap_zero_page(mm, vmf->orig_pte); 3290f1a79412SShakeel Butt inc_mm_counter(mm, MM_ANONPAGES); 32912f38ab2cSShachar Raindel } 32922994302bSJan Kara flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); 329328d41a48SMatthew Wilcox (Oracle) entry = mk_pte(&new_folio->page, vma->vm_page_prot); 329450c25ee9SThomas Bogendoerfer entry = pte_sw_mkyoung(entry); 3295c89357e2SDavid Hildenbrand if (unlikely(unshare)) { 3296c89357e2SDavid Hildenbrand if (pte_soft_dirty(vmf->orig_pte)) 3297c89357e2SDavid Hildenbrand entry = pte_mksoft_dirty(entry); 3298c89357e2SDavid Hildenbrand if (pte_uffd_wp(vmf->orig_pte)) 3299c89357e2SDavid Hildenbrand entry = pte_mkuffd_wp(entry); 3300c89357e2SDavid Hildenbrand } else { 33012f38ab2cSShachar Raindel entry = maybe_mkwrite(pte_mkdirty(entry), vma); 3302c89357e2SDavid Hildenbrand } 3303111fe718SNicholas Piggin 33042f38ab2cSShachar Raindel /* 33052f38ab2cSShachar Raindel * Clear the pte entry and flush it first, before updating the 3306111fe718SNicholas Piggin * pte with the new entry, to keep TLBs on different CPUs in 3307111fe718SNicholas Piggin * sync. This code used to set the new PTE then flush TLBs, but 3308111fe718SNicholas Piggin * that left a window where the new PTE could be loaded into 3309111fe718SNicholas Piggin * some TLBs while the old PTE remains in others. 33102f38ab2cSShachar Raindel */ 3311ec8832d0SAlistair Popple ptep_clear_flush(vma, vmf->address, vmf->pte); 331228d41a48SMatthew Wilcox (Oracle) folio_add_new_anon_rmap(new_folio, vma, vmf->address); 331328d41a48SMatthew Wilcox (Oracle) folio_add_lru_vma(new_folio, vma); 33142f38ab2cSShachar Raindel /* 33152f38ab2cSShachar Raindel * We call the notify macro here because, when using secondary 33162f38ab2cSShachar Raindel * mmu page tables (such as kvm shadow page tables), we want the 33172f38ab2cSShachar Raindel * new page to be mapped directly into the secondary page table. 33182f38ab2cSShachar Raindel */ 3319c89357e2SDavid Hildenbrand BUG_ON(unshare && pte_write(entry)); 332082b0f8c3SJan Kara set_pte_at_notify(mm, vmf->address, vmf->pte, entry); 33215003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vma, vmf->address, vmf->pte, 1); 332228d41a48SMatthew Wilcox (Oracle) if (old_folio) { 33232f38ab2cSShachar Raindel /* 33242f38ab2cSShachar Raindel * Only after switching the pte to the new page may 33252f38ab2cSShachar Raindel * we remove the mapcount here. Otherwise another 33262f38ab2cSShachar Raindel * process may come and find the rmap count decremented 33272f38ab2cSShachar Raindel * before the pte is switched to the new page, and 33282f38ab2cSShachar Raindel * "reuse" the old page writing into it while our pte 33292f38ab2cSShachar Raindel * here still points into it and can be read by other 33302f38ab2cSShachar Raindel * threads. 33312f38ab2cSShachar Raindel * 33322f38ab2cSShachar Raindel * The critical issue is to order this 3333c4626503SDavid Hildenbrand * folio_remove_rmap_pte() with the ptp_clear_flush 3334c4626503SDavid Hildenbrand * above. Those stores are ordered by (if nothing else,) 33352f38ab2cSShachar Raindel * the barrier present in the atomic_add_negative 3336c4626503SDavid Hildenbrand * in folio_remove_rmap_pte(); 33372f38ab2cSShachar Raindel * 33382f38ab2cSShachar Raindel * Then the TLB flush in ptep_clear_flush ensures that 33392f38ab2cSShachar Raindel * no process can access the old page before the 33402f38ab2cSShachar Raindel * decremented mapcount is visible. And the old page 33412f38ab2cSShachar Raindel * cannot be reused until after the decremented 33422f38ab2cSShachar Raindel * mapcount is visible. So transitively, TLBs to 33432f38ab2cSShachar Raindel * old page will be flushed before it can be reused. 33442f38ab2cSShachar Raindel */ 3345c4626503SDavid Hildenbrand folio_remove_rmap_pte(old_folio, vmf->page, vma); 33462f38ab2cSShachar Raindel } 33472f38ab2cSShachar Raindel 33482f38ab2cSShachar Raindel /* Free the old page.. */ 334928d41a48SMatthew Wilcox (Oracle) new_folio = old_folio; 33502f38ab2cSShachar Raindel page_copied = 1; 33513db82b93SHugh Dickins pte_unmap_unlock(vmf->pte, vmf->ptl); 33523db82b93SHugh Dickins } else if (vmf->pte) { 33537df67697SBibo Mao update_mmu_tlb(vma, vmf->address, vmf->pte); 33543db82b93SHugh Dickins pte_unmap_unlock(vmf->pte, vmf->ptl); 33552f38ab2cSShachar Raindel } 33562f38ab2cSShachar Raindel 3357ec8832d0SAlistair Popple mmu_notifier_invalidate_range_end(&range); 33583db82b93SHugh Dickins 33593db82b93SHugh Dickins if (new_folio) 33603db82b93SHugh Dickins folio_put(new_folio); 336128d41a48SMatthew Wilcox (Oracle) if (old_folio) { 3362f4c4a3f4SHuang Ying if (page_copied) 336328d41a48SMatthew Wilcox (Oracle) free_swap_cache(&old_folio->page); 336428d41a48SMatthew Wilcox (Oracle) folio_put(old_folio); 33652f38ab2cSShachar Raindel } 3366662ce1dcSYang Yang 3367662ce1dcSYang Yang delayacct_wpcopy_end(); 3368cb8d8633SDavid Hildenbrand return 0; 33692f38ab2cSShachar Raindel oom: 3370164b06f2SMatthew Wilcox (Oracle) ret = VM_FAULT_OOM; 3371164b06f2SMatthew Wilcox (Oracle) out: 337228d41a48SMatthew Wilcox (Oracle) if (old_folio) 337328d41a48SMatthew Wilcox (Oracle) folio_put(old_folio); 3374662ce1dcSYang Yang 3375662ce1dcSYang Yang delayacct_wpcopy_end(); 3376164b06f2SMatthew Wilcox (Oracle) return ret; 33772f38ab2cSShachar Raindel } 33782f38ab2cSShachar Raindel 337966a6197cSJan Kara /** 338066a6197cSJan Kara * finish_mkwrite_fault - finish page fault for a shared mapping, making PTE 338166a6197cSJan Kara * writeable once the page is prepared 338266a6197cSJan Kara * 338366a6197cSJan Kara * @vmf: structure describing the fault 3384a86bc96bSKefeng Wang * @folio: the folio of vmf->page 338566a6197cSJan Kara * 338666a6197cSJan Kara * This function handles all that is needed to finish a write page fault in a 338766a6197cSJan Kara * shared mapping due to PTE being read-only once the mapped page is prepared. 3388a862f68aSMike Rapoport * It handles locking of PTE and modifying it. 338966a6197cSJan Kara * 339066a6197cSJan Kara * The function expects the page to be locked or other protection against 339166a6197cSJan Kara * concurrent faults / writeback (such as DAX radix tree locks). 3392a862f68aSMike Rapoport * 33932797e79fSLiu Xiang * Return: %0 on success, %VM_FAULT_NOPAGE when PTE got changed before 3394a862f68aSMike Rapoport * we acquired PTE lock. 339566a6197cSJan Kara */ 3396a86bc96bSKefeng Wang static vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf, struct folio *folio) 339766a6197cSJan Kara { 339866a6197cSJan Kara WARN_ON_ONCE(!(vmf->vma->vm_flags & VM_SHARED)); 339966a6197cSJan Kara vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, vmf->address, 340066a6197cSJan Kara &vmf->ptl); 34013db82b93SHugh Dickins if (!vmf->pte) 34023db82b93SHugh Dickins return VM_FAULT_NOPAGE; 340366a6197cSJan Kara /* 340466a6197cSJan Kara * We might have raced with another page fault while we released the 340566a6197cSJan Kara * pte_offset_map_lock. 340666a6197cSJan Kara */ 3407c33c7948SRyan Roberts if (!pte_same(ptep_get(vmf->pte), vmf->orig_pte)) { 34087df67697SBibo Mao update_mmu_tlb(vmf->vma, vmf->address, vmf->pte); 340966a6197cSJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3410a19e2553SJan Kara return VM_FAULT_NOPAGE; 341166a6197cSJan Kara } 3412a86bc96bSKefeng Wang wp_page_reuse(vmf, folio); 3413a19e2553SJan Kara return 0; 341466a6197cSJan Kara } 341566a6197cSJan Kara 3416dd906184SBoaz Harrosh /* 3417dd906184SBoaz Harrosh * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED 3418dd906184SBoaz Harrosh * mapping 3419dd906184SBoaz Harrosh */ 34202b740303SSouptick Joarder static vm_fault_t wp_pfn_shared(struct vm_fault *vmf) 3421dd906184SBoaz Harrosh { 342282b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3423bae473a4SKirill A. Shutemov 3424dd906184SBoaz Harrosh if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) { 34252b740303SSouptick Joarder vm_fault_t ret; 3426dd906184SBoaz Harrosh 342782b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 34284a68fef1SMatthew Wilcox (Oracle) ret = vmf_can_call_fault(vmf); 34294a68fef1SMatthew Wilcox (Oracle) if (ret) 34304a68fef1SMatthew Wilcox (Oracle) return ret; 3431063e60d8SMatthew Wilcox (Oracle) 3432fe82221fSJan Kara vmf->flags |= FAULT_FLAG_MKWRITE; 343311bac800SDave Jiang ret = vma->vm_ops->pfn_mkwrite(vmf); 34342f89dc12SJan Kara if (ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)) 3435dd906184SBoaz Harrosh return ret; 3436a86bc96bSKefeng Wang return finish_mkwrite_fault(vmf, NULL); 3437dd906184SBoaz Harrosh } 3438a86bc96bSKefeng Wang wp_page_reuse(vmf, NULL); 3439cb8d8633SDavid Hildenbrand return 0; 3440dd906184SBoaz Harrosh } 3441dd906184SBoaz Harrosh 34425a97858bSSidhartha Kumar static vm_fault_t wp_page_shared(struct vm_fault *vmf, struct folio *folio) 344382b0f8c3SJan Kara __releases(vmf->ptl) 344493e478d4SShachar Raindel { 344582b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3446cb8d8633SDavid Hildenbrand vm_fault_t ret = 0; 344793e478d4SShachar Raindel 34485a97858bSSidhartha Kumar folio_get(folio); 344993e478d4SShachar Raindel 345093e478d4SShachar Raindel if (vma->vm_ops && vma->vm_ops->page_mkwrite) { 34512b740303SSouptick Joarder vm_fault_t tmp; 345293e478d4SShachar Raindel 345382b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 34544a68fef1SMatthew Wilcox (Oracle) tmp = vmf_can_call_fault(vmf); 34554a68fef1SMatthew Wilcox (Oracle) if (tmp) { 3456063e60d8SMatthew Wilcox (Oracle) folio_put(folio); 34574a68fef1SMatthew Wilcox (Oracle) return tmp; 3458063e60d8SMatthew Wilcox (Oracle) } 3459063e60d8SMatthew Wilcox (Oracle) 346086aa6998SSidhartha Kumar tmp = do_page_mkwrite(vmf, folio); 346193e478d4SShachar Raindel if (unlikely(!tmp || (tmp & 346293e478d4SShachar Raindel (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { 34635a97858bSSidhartha Kumar folio_put(folio); 346493e478d4SShachar Raindel return tmp; 346593e478d4SShachar Raindel } 3466a86bc96bSKefeng Wang tmp = finish_mkwrite_fault(vmf, folio); 3467a19e2553SJan Kara if (unlikely(tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) { 34685a97858bSSidhartha Kumar folio_unlock(folio); 34695a97858bSSidhartha Kumar folio_put(folio); 347066a6197cSJan Kara return tmp; 347193e478d4SShachar Raindel } 347266a6197cSJan Kara } else { 3473a86bc96bSKefeng Wang wp_page_reuse(vmf, folio); 34745a97858bSSidhartha Kumar folio_lock(folio); 347593e478d4SShachar Raindel } 347689b15332SJohannes Weiner ret |= fault_dirty_shared_page(vmf); 34775a97858bSSidhartha Kumar folio_put(folio); 347893e478d4SShachar Raindel 347989b15332SJohannes Weiner return ret; 348093e478d4SShachar Raindel } 348193e478d4SShachar Raindel 3482dec078ccSDavid Hildenbrand static bool wp_can_reuse_anon_folio(struct folio *folio, 3483dec078ccSDavid Hildenbrand struct vm_area_struct *vma) 3484dec078ccSDavid Hildenbrand { 3485dec078ccSDavid Hildenbrand /* 3486dec078ccSDavid Hildenbrand * We have to verify under folio lock: these early checks are 3487dec078ccSDavid Hildenbrand * just an optimization to avoid locking the folio and freeing 3488dec078ccSDavid Hildenbrand * the swapcache if there is little hope that we can reuse. 3489dec078ccSDavid Hildenbrand * 3490dec078ccSDavid Hildenbrand * KSM doesn't necessarily raise the folio refcount. 3491dec078ccSDavid Hildenbrand */ 3492dec078ccSDavid Hildenbrand if (folio_test_ksm(folio) || folio_ref_count(folio) > 3) 3493dec078ccSDavid Hildenbrand return false; 3494dec078ccSDavid Hildenbrand if (!folio_test_lru(folio)) 3495dec078ccSDavid Hildenbrand /* 3496dec078ccSDavid Hildenbrand * We cannot easily detect+handle references from 3497dec078ccSDavid Hildenbrand * remote LRU caches or references to LRU folios. 3498dec078ccSDavid Hildenbrand */ 3499dec078ccSDavid Hildenbrand lru_add_drain(); 3500dec078ccSDavid Hildenbrand if (folio_ref_count(folio) > 1 + folio_test_swapcache(folio)) 3501dec078ccSDavid Hildenbrand return false; 3502dec078ccSDavid Hildenbrand if (!folio_trylock(folio)) 3503dec078ccSDavid Hildenbrand return false; 3504dec078ccSDavid Hildenbrand if (folio_test_swapcache(folio)) 3505dec078ccSDavid Hildenbrand folio_free_swap(folio); 3506dec078ccSDavid Hildenbrand if (folio_test_ksm(folio) || folio_ref_count(folio) != 1) { 3507dec078ccSDavid Hildenbrand folio_unlock(folio); 3508dec078ccSDavid Hildenbrand return false; 3509dec078ccSDavid Hildenbrand } 3510dec078ccSDavid Hildenbrand /* 3511dec078ccSDavid Hildenbrand * Ok, we've got the only folio reference from our mapping 3512dec078ccSDavid Hildenbrand * and the folio is locked, it's dark out, and we're wearing 3513dec078ccSDavid Hildenbrand * sunglasses. Hit it. 3514dec078ccSDavid Hildenbrand */ 3515dec078ccSDavid Hildenbrand folio_move_anon_rmap(folio, vma); 3516dec078ccSDavid Hildenbrand folio_unlock(folio); 3517dec078ccSDavid Hildenbrand return true; 3518dec078ccSDavid Hildenbrand } 3519dec078ccSDavid Hildenbrand 35202f38ab2cSShachar Raindel /* 3521c89357e2SDavid Hildenbrand * This routine handles present pages, when 3522c89357e2SDavid Hildenbrand * * users try to write to a shared page (FAULT_FLAG_WRITE) 3523c89357e2SDavid Hildenbrand * * GUP wants to take a R/O pin on a possibly shared anonymous page 3524c89357e2SDavid Hildenbrand * (FAULT_FLAG_UNSHARE) 3525c89357e2SDavid Hildenbrand * 3526c89357e2SDavid Hildenbrand * It is done by copying the page to a new address and decrementing the 3527c89357e2SDavid Hildenbrand * shared-page counter for the old page. 35281da177e4SLinus Torvalds * 35291da177e4SLinus Torvalds * Note that this routine assumes that the protection checks have been 35301da177e4SLinus Torvalds * done by the caller (the low-level page fault routine in most cases). 3531c89357e2SDavid Hildenbrand * Thus, with FAULT_FLAG_WRITE, we can safely just mark it writable once we've 3532c89357e2SDavid Hildenbrand * done any necessary COW. 35331da177e4SLinus Torvalds * 3534c89357e2SDavid Hildenbrand * In case of FAULT_FLAG_WRITE, we also mark the page dirty at this point even 3535c89357e2SDavid Hildenbrand * though the page will change only once the write actually happens. This 3536c89357e2SDavid Hildenbrand * avoids a few races, and potentially makes it more efficient. 35371da177e4SLinus Torvalds * 3538c1e8d7c6SMichel Lespinasse * We enter with non-exclusive mmap_lock (to exclude vma changes, 35398f4e2101SHugh Dickins * but allow concurrent faults), with pte both mapped and locked. 3540c1e8d7c6SMichel Lespinasse * We return with mmap_lock still held, but pte unmapped and unlocked. 35411da177e4SLinus Torvalds */ 35422b740303SSouptick Joarder static vm_fault_t do_wp_page(struct vm_fault *vmf) 354382b0f8c3SJan Kara __releases(vmf->ptl) 35441da177e4SLinus Torvalds { 3545c89357e2SDavid Hildenbrand const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE; 354682b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3547b9086fdeSDavid Hildenbrand struct folio *folio = NULL; 3548d61ea1cbSPeter Xu pte_t pte; 35491da177e4SLinus Torvalds 3550c89357e2SDavid Hildenbrand if (likely(!unshare)) { 3551c33c7948SRyan Roberts if (userfaultfd_pte_wp(vma, ptep_get(vmf->pte))) { 3552d61ea1cbSPeter Xu if (!userfaultfd_wp_async(vma)) { 3553529b930bSAndrea Arcangeli pte_unmap_unlock(vmf->pte, vmf->ptl); 3554529b930bSAndrea Arcangeli return handle_userfault(vmf, VM_UFFD_WP); 3555529b930bSAndrea Arcangeli } 3556529b930bSAndrea Arcangeli 35576ce64428SNadav Amit /* 3558d61ea1cbSPeter Xu * Nothing needed (cache flush, TLB invalidations, 3559d61ea1cbSPeter Xu * etc.) because we're only removing the uffd-wp bit, 3560d61ea1cbSPeter Xu * which is completely invisible to the user. 3561d61ea1cbSPeter Xu */ 3562d61ea1cbSPeter Xu pte = pte_clear_uffd_wp(ptep_get(vmf->pte)); 3563d61ea1cbSPeter Xu 3564d61ea1cbSPeter Xu set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte); 3565d61ea1cbSPeter Xu /* 3566d61ea1cbSPeter Xu * Update this to be prepared for following up CoW 3567d61ea1cbSPeter Xu * handling 3568d61ea1cbSPeter Xu */ 3569d61ea1cbSPeter Xu vmf->orig_pte = pte; 3570d61ea1cbSPeter Xu } 3571d61ea1cbSPeter Xu 3572d61ea1cbSPeter Xu /* 35736ce64428SNadav Amit * Userfaultfd write-protect can defer flushes. Ensure the TLB 35746ce64428SNadav Amit * is flushed in this case before copying. 35756ce64428SNadav Amit */ 35766ce64428SNadav Amit if (unlikely(userfaultfd_wp(vmf->vma) && 35776ce64428SNadav Amit mm_tlb_flush_pending(vmf->vma->vm_mm))) 35786ce64428SNadav Amit flush_tlb_page(vmf->vma, vmf->address); 3579c89357e2SDavid Hildenbrand } 35806ce64428SNadav Amit 3581a41b70d6SJan Kara vmf->page = vm_normal_page(vma, vmf->address, vmf->orig_pte); 3582c89357e2SDavid Hildenbrand 35835a97858bSSidhartha Kumar if (vmf->page) 35845a97858bSSidhartha Kumar folio = page_folio(vmf->page); 35855a97858bSSidhartha Kumar 3586251b97f5SPeter Zijlstra /* 3587b9086fdeSDavid Hildenbrand * Shared mapping: we are guaranteed to have VM_WRITE and 3588b9086fdeSDavid Hildenbrand * FAULT_FLAG_WRITE set at this point. 3589b9086fdeSDavid Hildenbrand */ 3590b9086fdeSDavid Hildenbrand if (vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) { 3591b9086fdeSDavid Hildenbrand /* 359264e45507SPeter Feiner * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a 359364e45507SPeter Feiner * VM_PFNMAP VMA. 3594251b97f5SPeter Zijlstra * 3595251b97f5SPeter Zijlstra * We should not cow pages in a shared writeable mapping. 3596dd906184SBoaz Harrosh * Just mark the pages writable and/or call ops->pfn_mkwrite. 3597251b97f5SPeter Zijlstra */ 3598b9086fdeSDavid Hildenbrand if (!vmf->page) 35992994302bSJan Kara return wp_pfn_shared(vmf); 36005a97858bSSidhartha Kumar return wp_page_shared(vmf, folio); 3601251b97f5SPeter Zijlstra } 36021da177e4SLinus Torvalds 3603b9086fdeSDavid Hildenbrand /* 3604b9086fdeSDavid Hildenbrand * Private mapping: create an exclusive anonymous page copy if reuse 3605b9086fdeSDavid Hildenbrand * is impossible. We might miss VM_WRITE for FOLL_FORCE handling. 360653a05ad9SDavid Hildenbrand * 3607dec078ccSDavid Hildenbrand * If we encounter a page that is marked exclusive, we must reuse 3608dec078ccSDavid Hildenbrand * the page without further checks. 360953a05ad9SDavid Hildenbrand */ 3610dec078ccSDavid Hildenbrand if (folio && folio_test_anon(folio) && 3611dec078ccSDavid Hildenbrand (PageAnonExclusive(vmf->page) || wp_can_reuse_anon_folio(folio, vma))) { 3612dec078ccSDavid Hildenbrand if (!PageAnonExclusive(vmf->page)) 36135ca43289SDavid Hildenbrand SetPageAnonExclusive(vmf->page); 3614c89357e2SDavid Hildenbrand if (unlikely(unshare)) { 3615c89357e2SDavid Hildenbrand pte_unmap_unlock(vmf->pte, vmf->ptl); 3616c89357e2SDavid Hildenbrand return 0; 3617c89357e2SDavid Hildenbrand } 3618a86bc96bSKefeng Wang wp_page_reuse(vmf, folio); 3619cb8d8633SDavid Hildenbrand return 0; 36201da177e4SLinus Torvalds } 36211da177e4SLinus Torvalds /* 36221da177e4SLinus Torvalds * Ok, we need to copy. Oh, well.. 36231da177e4SLinus Torvalds */ 3624b9086fdeSDavid Hildenbrand if (folio) 3625b9086fdeSDavid Hildenbrand folio_get(folio); 362628766805SShachar Raindel 362782b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 362894bfe85bSYang Yang #ifdef CONFIG_KSM 3629b9086fdeSDavid Hildenbrand if (folio && folio_test_ksm(folio)) 363094bfe85bSYang Yang count_vm_event(COW_KSM); 363194bfe85bSYang Yang #endif 3632a41b70d6SJan Kara return wp_page_copy(vmf); 36331da177e4SLinus Torvalds } 36341da177e4SLinus Torvalds 363597a89413SPeter Zijlstra static void unmap_mapping_range_vma(struct vm_area_struct *vma, 36361da177e4SLinus Torvalds unsigned long start_addr, unsigned long end_addr, 36371da177e4SLinus Torvalds struct zap_details *details) 36381da177e4SLinus Torvalds { 3639f5cc4eefSAl Viro zap_page_range_single(vma, start_addr, end_addr - start_addr, details); 36401da177e4SLinus Torvalds } 36411da177e4SLinus Torvalds 3642f808c13fSDavidlohr Bueso static inline void unmap_mapping_range_tree(struct rb_root_cached *root, 3643232a6a1cSPeter Xu pgoff_t first_index, 3644232a6a1cSPeter Xu pgoff_t last_index, 36451da177e4SLinus Torvalds struct zap_details *details) 36461da177e4SLinus Torvalds { 36471da177e4SLinus Torvalds struct vm_area_struct *vma; 36481da177e4SLinus Torvalds pgoff_t vba, vea, zba, zea; 36491da177e4SLinus Torvalds 3650232a6a1cSPeter Xu vma_interval_tree_foreach(vma, root, first_index, last_index) { 36511da177e4SLinus Torvalds vba = vma->vm_pgoff; 3652d6e93217SLibin vea = vba + vma_pages(vma) - 1; 3653f9871da9SMiaohe Lin zba = max(first_index, vba); 3654f9871da9SMiaohe Lin zea = min(last_index, vea); 36551da177e4SLinus Torvalds 365697a89413SPeter Zijlstra unmap_mapping_range_vma(vma, 36571da177e4SLinus Torvalds ((zba - vba) << PAGE_SHIFT) + vma->vm_start, 36581da177e4SLinus Torvalds ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start, 365997a89413SPeter Zijlstra details); 36601da177e4SLinus Torvalds } 36611da177e4SLinus Torvalds } 36621da177e4SLinus Torvalds 36631da177e4SLinus Torvalds /** 36643506659eSMatthew Wilcox (Oracle) * unmap_mapping_folio() - Unmap single folio from processes. 36653506659eSMatthew Wilcox (Oracle) * @folio: The locked folio to be unmapped. 366622061a1fSHugh Dickins * 36673506659eSMatthew Wilcox (Oracle) * Unmap this folio from any userspace process which still has it mmaped. 366822061a1fSHugh Dickins * Typically, for efficiency, the range of nearby pages has already been 366922061a1fSHugh Dickins * unmapped by unmap_mapping_pages() or unmap_mapping_range(). But once 36703506659eSMatthew Wilcox (Oracle) * truncation or invalidation holds the lock on a folio, it may find that 36713506659eSMatthew Wilcox (Oracle) * the page has been remapped again: and then uses unmap_mapping_folio() 367222061a1fSHugh Dickins * to unmap it finally. 367322061a1fSHugh Dickins */ 36743506659eSMatthew Wilcox (Oracle) void unmap_mapping_folio(struct folio *folio) 367522061a1fSHugh Dickins { 36763506659eSMatthew Wilcox (Oracle) struct address_space *mapping = folio->mapping; 367722061a1fSHugh Dickins struct zap_details details = { }; 3678232a6a1cSPeter Xu pgoff_t first_index; 3679232a6a1cSPeter Xu pgoff_t last_index; 368022061a1fSHugh Dickins 36813506659eSMatthew Wilcox (Oracle) VM_BUG_ON(!folio_test_locked(folio)); 368222061a1fSHugh Dickins 36833506659eSMatthew Wilcox (Oracle) first_index = folio->index; 368487b11f86SSidhartha Kumar last_index = folio_next_index(folio) - 1; 3685232a6a1cSPeter Xu 36862e148f1eSPeter Xu details.even_cows = false; 36873506659eSMatthew Wilcox (Oracle) details.single_folio = folio; 3688999dad82SPeter Xu details.zap_flags = ZAP_FLAG_DROP_MARKER; 368922061a1fSHugh Dickins 36902c865995SHugh Dickins i_mmap_lock_read(mapping); 369122061a1fSHugh Dickins if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root))) 3692232a6a1cSPeter Xu unmap_mapping_range_tree(&mapping->i_mmap, first_index, 3693232a6a1cSPeter Xu last_index, &details); 36942c865995SHugh Dickins i_mmap_unlock_read(mapping); 369522061a1fSHugh Dickins } 369622061a1fSHugh Dickins 369722061a1fSHugh Dickins /** 3698977fbdcdSMatthew Wilcox * unmap_mapping_pages() - Unmap pages from processes. 3699977fbdcdSMatthew Wilcox * @mapping: The address space containing pages to be unmapped. 3700977fbdcdSMatthew Wilcox * @start: Index of first page to be unmapped. 3701977fbdcdSMatthew Wilcox * @nr: Number of pages to be unmapped. 0 to unmap to end of file. 3702977fbdcdSMatthew Wilcox * @even_cows: Whether to unmap even private COWed pages. 3703977fbdcdSMatthew Wilcox * 3704977fbdcdSMatthew Wilcox * Unmap the pages in this address space from any userspace process which 3705977fbdcdSMatthew Wilcox * has them mmaped. Generally, you want to remove COWed pages as well when 3706977fbdcdSMatthew Wilcox * a file is being truncated, but not when invalidating pages from the page 3707977fbdcdSMatthew Wilcox * cache. 3708977fbdcdSMatthew Wilcox */ 3709977fbdcdSMatthew Wilcox void unmap_mapping_pages(struct address_space *mapping, pgoff_t start, 3710977fbdcdSMatthew Wilcox pgoff_t nr, bool even_cows) 3711977fbdcdSMatthew Wilcox { 3712977fbdcdSMatthew Wilcox struct zap_details details = { }; 3713232a6a1cSPeter Xu pgoff_t first_index = start; 3714232a6a1cSPeter Xu pgoff_t last_index = start + nr - 1; 3715977fbdcdSMatthew Wilcox 37162e148f1eSPeter Xu details.even_cows = even_cows; 3717232a6a1cSPeter Xu if (last_index < first_index) 3718232a6a1cSPeter Xu last_index = ULONG_MAX; 3719977fbdcdSMatthew Wilcox 37202c865995SHugh Dickins i_mmap_lock_read(mapping); 3721977fbdcdSMatthew Wilcox if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root))) 3722232a6a1cSPeter Xu unmap_mapping_range_tree(&mapping->i_mmap, first_index, 3723232a6a1cSPeter Xu last_index, &details); 37242c865995SHugh Dickins i_mmap_unlock_read(mapping); 3725977fbdcdSMatthew Wilcox } 37266e0e99d5SDavid Howells EXPORT_SYMBOL_GPL(unmap_mapping_pages); 3727977fbdcdSMatthew Wilcox 3728977fbdcdSMatthew Wilcox /** 37298a5f14a2SKirill A. Shutemov * unmap_mapping_range - unmap the portion of all mmaps in the specified 3730977fbdcdSMatthew Wilcox * address_space corresponding to the specified byte range in the underlying 37318a5f14a2SKirill A. Shutemov * file. 37328a5f14a2SKirill A. Shutemov * 37333d41088fSMartin Waitz * @mapping: the address space containing mmaps to be unmapped. 37341da177e4SLinus Torvalds * @holebegin: byte in first page to unmap, relative to the start of 37351da177e4SLinus Torvalds * the underlying file. This will be rounded down to a PAGE_SIZE 373625d9e2d1Snpiggin@suse.de * boundary. Note that this is different from truncate_pagecache(), which 37371da177e4SLinus Torvalds * must keep the partial page. In contrast, we must get rid of 37381da177e4SLinus Torvalds * partial pages. 37391da177e4SLinus Torvalds * @holelen: size of prospective hole in bytes. This will be rounded 37401da177e4SLinus Torvalds * up to a PAGE_SIZE boundary. A holelen of zero truncates to the 37411da177e4SLinus Torvalds * end of the file. 37421da177e4SLinus Torvalds * @even_cows: 1 when truncating a file, unmap even private COWed pages; 37431da177e4SLinus Torvalds * but 0 when invalidating pagecache, don't throw away private data. 37441da177e4SLinus Torvalds */ 37451da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping, 37461da177e4SLinus Torvalds loff_t const holebegin, loff_t const holelen, int even_cows) 37471da177e4SLinus Torvalds { 37489eab0421SJiajun Xie pgoff_t hba = (pgoff_t)(holebegin) >> PAGE_SHIFT; 37499eab0421SJiajun Xie pgoff_t hlen = ((pgoff_t)(holelen) + PAGE_SIZE - 1) >> PAGE_SHIFT; 37501da177e4SLinus Torvalds 37511da177e4SLinus Torvalds /* Check for overflow. */ 37521da177e4SLinus Torvalds if (sizeof(holelen) > sizeof(hlen)) { 37531da177e4SLinus Torvalds long long holeend = 37541da177e4SLinus Torvalds (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 37551da177e4SLinus Torvalds if (holeend & ~(long long)ULONG_MAX) 37561da177e4SLinus Torvalds hlen = ULONG_MAX - hba + 1; 37571da177e4SLinus Torvalds } 37581da177e4SLinus Torvalds 3759977fbdcdSMatthew Wilcox unmap_mapping_pages(mapping, hba, hlen, even_cows); 37601da177e4SLinus Torvalds } 37611da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range); 37621da177e4SLinus Torvalds 37631da177e4SLinus Torvalds /* 3764b756a3b5SAlistair Popple * Restore a potential device exclusive pte to a working pte entry 3765b756a3b5SAlistair Popple */ 3766b756a3b5SAlistair Popple static vm_fault_t remove_device_exclusive_entry(struct vm_fault *vmf) 3767b756a3b5SAlistair Popple { 376819672a9eSMatthew Wilcox (Oracle) struct folio *folio = page_folio(vmf->page); 3769b756a3b5SAlistair Popple struct vm_area_struct *vma = vmf->vma; 3770b756a3b5SAlistair Popple struct mmu_notifier_range range; 3771fdc724d6SSuren Baghdasaryan vm_fault_t ret; 3772b756a3b5SAlistair Popple 37737c7b9629SAlistair Popple /* 37747c7b9629SAlistair Popple * We need a reference to lock the folio because we don't hold 37757c7b9629SAlistair Popple * the PTL so a racing thread can remove the device-exclusive 37767c7b9629SAlistair Popple * entry and unmap it. If the folio is free the entry must 37777c7b9629SAlistair Popple * have been removed already. If it happens to have already 37787c7b9629SAlistair Popple * been re-allocated after being freed all we do is lock and 37797c7b9629SAlistair Popple * unlock it. 37807c7b9629SAlistair Popple */ 37817c7b9629SAlistair Popple if (!folio_try_get(folio)) 37827c7b9629SAlistair Popple return 0; 37837c7b9629SAlistair Popple 3784fdc724d6SSuren Baghdasaryan ret = folio_lock_or_retry(folio, vmf); 3785fdc724d6SSuren Baghdasaryan if (ret) { 37867c7b9629SAlistair Popple folio_put(folio); 3787fdc724d6SSuren Baghdasaryan return ret; 37887c7b9629SAlistair Popple } 37897d4a8be0SAlistair Popple mmu_notifier_range_init_owner(&range, MMU_NOTIFY_EXCLUSIVE, 0, 3790b756a3b5SAlistair Popple vma->vm_mm, vmf->address & PAGE_MASK, 3791b756a3b5SAlistair Popple (vmf->address & PAGE_MASK) + PAGE_SIZE, NULL); 3792b756a3b5SAlistair Popple mmu_notifier_invalidate_range_start(&range); 3793b756a3b5SAlistair Popple 3794b756a3b5SAlistair Popple vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 3795b756a3b5SAlistair Popple &vmf->ptl); 3796c33c7948SRyan Roberts if (likely(vmf->pte && pte_same(ptep_get(vmf->pte), vmf->orig_pte))) 379719672a9eSMatthew Wilcox (Oracle) restore_exclusive_pte(vma, vmf->page, vmf->address, vmf->pte); 3798b756a3b5SAlistair Popple 37993db82b93SHugh Dickins if (vmf->pte) 3800b756a3b5SAlistair Popple pte_unmap_unlock(vmf->pte, vmf->ptl); 380119672a9eSMatthew Wilcox (Oracle) folio_unlock(folio); 38027c7b9629SAlistair Popple folio_put(folio); 3803b756a3b5SAlistair Popple 3804b756a3b5SAlistair Popple mmu_notifier_invalidate_range_end(&range); 3805b756a3b5SAlistair Popple return 0; 3806b756a3b5SAlistair Popple } 3807b756a3b5SAlistair Popple 3808a160e537SMatthew Wilcox (Oracle) static inline bool should_try_to_free_swap(struct folio *folio, 3809c145e0b4SDavid Hildenbrand struct vm_area_struct *vma, 3810c145e0b4SDavid Hildenbrand unsigned int fault_flags) 3811c145e0b4SDavid Hildenbrand { 3812a160e537SMatthew Wilcox (Oracle) if (!folio_test_swapcache(folio)) 3813c145e0b4SDavid Hildenbrand return false; 38149202d527SMatthew Wilcox (Oracle) if (mem_cgroup_swap_full(folio) || (vma->vm_flags & VM_LOCKED) || 3815a160e537SMatthew Wilcox (Oracle) folio_test_mlocked(folio)) 3816c145e0b4SDavid Hildenbrand return true; 3817c145e0b4SDavid Hildenbrand /* 3818c145e0b4SDavid Hildenbrand * If we want to map a page that's in the swapcache writable, we 3819c145e0b4SDavid Hildenbrand * have to detect via the refcount if we're really the exclusive 3820c145e0b4SDavid Hildenbrand * user. Try freeing the swapcache to get rid of the swapcache 3821c145e0b4SDavid Hildenbrand * reference only in case it's likely that we'll be the exlusive user. 3822c145e0b4SDavid Hildenbrand */ 3823a160e537SMatthew Wilcox (Oracle) return (fault_flags & FAULT_FLAG_WRITE) && !folio_test_ksm(folio) && 3824a160e537SMatthew Wilcox (Oracle) folio_ref_count(folio) == 2; 3825c145e0b4SDavid Hildenbrand } 3826c145e0b4SDavid Hildenbrand 38279c28a205SPeter Xu static vm_fault_t pte_marker_clear(struct vm_fault *vmf) 38289c28a205SPeter Xu { 38299c28a205SPeter Xu vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, 38309c28a205SPeter Xu vmf->address, &vmf->ptl); 38313db82b93SHugh Dickins if (!vmf->pte) 38323db82b93SHugh Dickins return 0; 38339c28a205SPeter Xu /* 38349c28a205SPeter Xu * Be careful so that we will only recover a special uffd-wp pte into a 38359c28a205SPeter Xu * none pte. Otherwise it means the pte could have changed, so retry. 38367e3ce3f8SPeter Xu * 38377e3ce3f8SPeter Xu * This should also cover the case where e.g. the pte changed 3838af19487fSAxel Rasmussen * quickly from a PTE_MARKER_UFFD_WP into PTE_MARKER_POISONED. 38397e3ce3f8SPeter Xu * So is_pte_marker() check is not enough to safely drop the pte. 38409c28a205SPeter Xu */ 3841c33c7948SRyan Roberts if (pte_same(vmf->orig_pte, ptep_get(vmf->pte))) 38429c28a205SPeter Xu pte_clear(vmf->vma->vm_mm, vmf->address, vmf->pte); 38439c28a205SPeter Xu pte_unmap_unlock(vmf->pte, vmf->ptl); 38449c28a205SPeter Xu return 0; 38459c28a205SPeter Xu } 38469c28a205SPeter Xu 38472bad466cSPeter Xu static vm_fault_t do_pte_missing(struct vm_fault *vmf) 38482bad466cSPeter Xu { 38492bad466cSPeter Xu if (vma_is_anonymous(vmf->vma)) 38502bad466cSPeter Xu return do_anonymous_page(vmf); 38512bad466cSPeter Xu else 38522bad466cSPeter Xu return do_fault(vmf); 38532bad466cSPeter Xu } 38542bad466cSPeter Xu 38559c28a205SPeter Xu /* 38569c28a205SPeter Xu * This is actually a page-missing access, but with uffd-wp special pte 38579c28a205SPeter Xu * installed. It means this pte was wr-protected before being unmapped. 38589c28a205SPeter Xu */ 38599c28a205SPeter Xu static vm_fault_t pte_marker_handle_uffd_wp(struct vm_fault *vmf) 38609c28a205SPeter Xu { 38619c28a205SPeter Xu /* 38629c28a205SPeter Xu * Just in case there're leftover special ptes even after the region 38637a079ba2SPeter Xu * got unregistered - we can simply clear them. 38649c28a205SPeter Xu */ 38652bad466cSPeter Xu if (unlikely(!userfaultfd_wp(vmf->vma))) 38669c28a205SPeter Xu return pte_marker_clear(vmf); 38679c28a205SPeter Xu 38682bad466cSPeter Xu return do_pte_missing(vmf); 38699c28a205SPeter Xu } 38709c28a205SPeter Xu 38715c041f5dSPeter Xu static vm_fault_t handle_pte_marker(struct vm_fault *vmf) 38725c041f5dSPeter Xu { 38735c041f5dSPeter Xu swp_entry_t entry = pte_to_swp_entry(vmf->orig_pte); 38745c041f5dSPeter Xu unsigned long marker = pte_marker_get(entry); 38755c041f5dSPeter Xu 38765c041f5dSPeter Xu /* 3877ca92ea3dSPeter Xu * PTE markers should never be empty. If anything weird happened, 3878ca92ea3dSPeter Xu * the best thing to do is to kill the process along with its mm. 38795c041f5dSPeter Xu */ 3880ca92ea3dSPeter Xu if (WARN_ON_ONCE(!marker)) 38815c041f5dSPeter Xu return VM_FAULT_SIGBUS; 38825c041f5dSPeter Xu 388315520a3fSPeter Xu /* Higher priority than uffd-wp when data corrupted */ 3884af19487fSAxel Rasmussen if (marker & PTE_MARKER_POISONED) 3885af19487fSAxel Rasmussen return VM_FAULT_HWPOISON; 388615520a3fSPeter Xu 38879c28a205SPeter Xu if (pte_marker_entry_uffd_wp(entry)) 38889c28a205SPeter Xu return pte_marker_handle_uffd_wp(vmf); 38899c28a205SPeter Xu 38909c28a205SPeter Xu /* This is an unknown pte marker */ 38919c28a205SPeter Xu return VM_FAULT_SIGBUS; 38925c041f5dSPeter Xu } 38935c041f5dSPeter Xu 3894b756a3b5SAlistair Popple /* 3895c1e8d7c6SMichel Lespinasse * We enter with non-exclusive mmap_lock (to exclude vma changes, 38968f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 38979a95f3cfSPaul Cassella * We return with pte unmapped and unlocked. 38989a95f3cfSPaul Cassella * 3899c1e8d7c6SMichel Lespinasse * We return with the mmap_lock locked or unlocked in the same cases 39009a95f3cfSPaul Cassella * as does filemap_fault(). 39011da177e4SLinus Torvalds */ 39022b740303SSouptick Joarder vm_fault_t do_swap_page(struct vm_fault *vmf) 39031da177e4SLinus Torvalds { 390482b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3905d4f9565aSMatthew Wilcox (Oracle) struct folio *swapcache, *folio = NULL; 3906d4f9565aSMatthew Wilcox (Oracle) struct page *page; 39072799e775SMiaohe Lin struct swap_info_struct *si = NULL; 390814f9135dSDavid Hildenbrand rmap_t rmap_flags = RMAP_NONE; 390913ddaf26SKairui Song bool need_clear_cache = false; 39101493a191SDavid Hildenbrand bool exclusive = false; 391165500d23SHugh Dickins swp_entry_t entry; 39121da177e4SLinus Torvalds pte_t pte; 39132b740303SSouptick Joarder vm_fault_t ret = 0; 3914aae466b0SJoonsoo Kim void *shadow = NULL; 39151da177e4SLinus Torvalds 39162ca99358SPeter Xu if (!pte_unmap_same(vmf)) 39178f4e2101SHugh Dickins goto out; 391865500d23SHugh Dickins 39192994302bSJan Kara entry = pte_to_swp_entry(vmf->orig_pte); 3920d1737fdbSAndi Kleen if (unlikely(non_swap_entry(entry))) { 39210697212aSChristoph Lameter if (is_migration_entry(entry)) { 392282b0f8c3SJan Kara migration_entry_wait(vma->vm_mm, vmf->pmd, 392382b0f8c3SJan Kara vmf->address); 3924b756a3b5SAlistair Popple } else if (is_device_exclusive_entry(entry)) { 3925b756a3b5SAlistair Popple vmf->page = pfn_swap_entry_to_page(entry); 3926b756a3b5SAlistair Popple ret = remove_device_exclusive_entry(vmf); 39275042db43SJérôme Glisse } else if (is_device_private_entry(entry)) { 39281235ccd0SSuren Baghdasaryan if (vmf->flags & FAULT_FLAG_VMA_LOCK) { 39291235ccd0SSuren Baghdasaryan /* 39301235ccd0SSuren Baghdasaryan * migrate_to_ram is not yet ready to operate 39311235ccd0SSuren Baghdasaryan * under VMA lock. 39321235ccd0SSuren Baghdasaryan */ 39331235ccd0SSuren Baghdasaryan vma_end_read(vma); 39341235ccd0SSuren Baghdasaryan ret = VM_FAULT_RETRY; 39351235ccd0SSuren Baghdasaryan goto out; 39361235ccd0SSuren Baghdasaryan } 39371235ccd0SSuren Baghdasaryan 3938af5cdaf8SAlistair Popple vmf->page = pfn_swap_entry_to_page(entry); 393916ce101dSAlistair Popple vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 394016ce101dSAlistair Popple vmf->address, &vmf->ptl); 39413db82b93SHugh Dickins if (unlikely(!vmf->pte || 3942c33c7948SRyan Roberts !pte_same(ptep_get(vmf->pte), 3943c33c7948SRyan Roberts vmf->orig_pte))) 39443b65f437SRyan Roberts goto unlock; 394516ce101dSAlistair Popple 394616ce101dSAlistair Popple /* 394716ce101dSAlistair Popple * Get a page reference while we know the page can't be 394816ce101dSAlistair Popple * freed. 394916ce101dSAlistair Popple */ 395016ce101dSAlistair Popple get_page(vmf->page); 395116ce101dSAlistair Popple pte_unmap_unlock(vmf->pte, vmf->ptl); 39524a955bedSAlistair Popple ret = vmf->page->pgmap->ops->migrate_to_ram(vmf); 395316ce101dSAlistair Popple put_page(vmf->page); 3954d1737fdbSAndi Kleen } else if (is_hwpoison_entry(entry)) { 3955d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 39565c041f5dSPeter Xu } else if (is_pte_marker_entry(entry)) { 39575c041f5dSPeter Xu ret = handle_pte_marker(vmf); 3958d1737fdbSAndi Kleen } else { 39592994302bSJan Kara print_bad_pte(vma, vmf->address, vmf->orig_pte, NULL); 3960d99be1a8SHugh Dickins ret = VM_FAULT_SIGBUS; 3961d1737fdbSAndi Kleen } 39620697212aSChristoph Lameter goto out; 39630697212aSChristoph Lameter } 39640bcac06fSMinchan Kim 39652799e775SMiaohe Lin /* Prevent swapoff from happening to us. */ 39662799e775SMiaohe Lin si = get_swap_device(entry); 39672799e775SMiaohe Lin if (unlikely(!si)) 39682799e775SMiaohe Lin goto out; 39690bcac06fSMinchan Kim 39705a423081SMatthew Wilcox (Oracle) folio = swap_cache_get_folio(entry, vma, vmf->address); 39715a423081SMatthew Wilcox (Oracle) if (folio) 39725a423081SMatthew Wilcox (Oracle) page = folio_file_page(folio, swp_offset(entry)); 3973d4f9565aSMatthew Wilcox (Oracle) swapcache = folio; 3974f8020772SMinchan Kim 3975d4f9565aSMatthew Wilcox (Oracle) if (!folio) { 3976a449bf58SQian Cai if (data_race(si->flags & SWP_SYNCHRONOUS_IO) && 3977eb085574SHuang Ying __swap_count(entry) == 1) { 397813ddaf26SKairui Song /* 397913ddaf26SKairui Song * Prevent parallel swapin from proceeding with 398013ddaf26SKairui Song * the cache flag. Otherwise, another thread may 398113ddaf26SKairui Song * finish swapin first, free the entry, and swapout 398213ddaf26SKairui Song * reusing the same entry. It's undetectable as 398313ddaf26SKairui Song * pte_same() returns true due to entry reuse. 398413ddaf26SKairui Song */ 398513ddaf26SKairui Song if (swapcache_prepare(entry)) { 398613ddaf26SKairui Song /* Relax a bit to prevent rapid repeated page faults */ 398713ddaf26SKairui Song schedule_timeout_uninterruptible(1); 398813ddaf26SKairui Song goto out; 398913ddaf26SKairui Song } 399013ddaf26SKairui Song need_clear_cache = true; 399113ddaf26SKairui Song 39920bcac06fSMinchan Kim /* skip swapcache */ 399363ad4addSMatthew Wilcox (Oracle) folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, 399463ad4addSMatthew Wilcox (Oracle) vma, vmf->address, false); 399563ad4addSMatthew Wilcox (Oracle) page = &folio->page; 399663ad4addSMatthew Wilcox (Oracle) if (folio) { 399763ad4addSMatthew Wilcox (Oracle) __folio_set_locked(folio); 399863ad4addSMatthew Wilcox (Oracle) __folio_set_swapbacked(folio); 39994c6355b2SJohannes Weiner 400065995918SMatthew Wilcox (Oracle) if (mem_cgroup_swapin_charge_folio(folio, 400163ad4addSMatthew Wilcox (Oracle) vma->vm_mm, GFP_KERNEL, 400263ad4addSMatthew Wilcox (Oracle) entry)) { 4003545b1b07SMichal Hocko ret = VM_FAULT_OOM; 40044c6355b2SJohannes Weiner goto out_page; 4005545b1b07SMichal Hocko } 40060add0c77SShakeel Butt mem_cgroup_swapin_uncharge_swap(entry); 40074c6355b2SJohannes Weiner 4008aae466b0SJoonsoo Kim shadow = get_shadow_from_swap_cache(entry); 4009aae466b0SJoonsoo Kim if (shadow) 401063ad4addSMatthew Wilcox (Oracle) workingset_refault(folio, shadow); 40110076f029SJoonsoo Kim 401263ad4addSMatthew Wilcox (Oracle) folio_add_lru(folio); 40130add0c77SShakeel Butt 4014c9bdf768SMatthew Wilcox (Oracle) /* To provide entry to swap_read_folio() */ 40153d2c9087SDavid Hildenbrand folio->swap = entry; 4016c9bdf768SMatthew Wilcox (Oracle) swap_read_folio(folio, true, NULL); 401763ad4addSMatthew Wilcox (Oracle) folio->private = NULL; 40180bcac06fSMinchan Kim } 4019aa8d22a1SMinchan Kim } else { 4020e9e9b7ecSMinchan Kim page = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, 4021e9e9b7ecSMinchan Kim vmf); 402263ad4addSMatthew Wilcox (Oracle) if (page) 402363ad4addSMatthew Wilcox (Oracle) folio = page_folio(page); 4024d4f9565aSMatthew Wilcox (Oracle) swapcache = folio; 40250bcac06fSMinchan Kim } 40260bcac06fSMinchan Kim 4027d4f9565aSMatthew Wilcox (Oracle) if (!folio) { 40281da177e4SLinus Torvalds /* 40298f4e2101SHugh Dickins * Back out if somebody else faulted in this pte 40308f4e2101SHugh Dickins * while we released the pte lock. 40311da177e4SLinus Torvalds */ 403282b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 403382b0f8c3SJan Kara vmf->address, &vmf->ptl); 4034c33c7948SRyan Roberts if (likely(vmf->pte && 4035c33c7948SRyan Roberts pte_same(ptep_get(vmf->pte), vmf->orig_pte))) 40361da177e4SLinus Torvalds ret = VM_FAULT_OOM; 403765500d23SHugh Dickins goto unlock; 40381da177e4SLinus Torvalds } 40391da177e4SLinus Torvalds 40401da177e4SLinus Torvalds /* Had to read the page from swap area: Major fault */ 40411da177e4SLinus Torvalds ret = VM_FAULT_MAJOR; 4042f8891e5eSChristoph Lameter count_vm_event(PGMAJFAULT); 40432262185cSRoman Gushchin count_memcg_event_mm(vma->vm_mm, PGMAJFAULT); 4044d1737fdbSAndi Kleen } else if (PageHWPoison(page)) { 404571f72525SWu Fengguang /* 404671f72525SWu Fengguang * hwpoisoned dirty swapcache pages are kept for killing 404771f72525SWu Fengguang * owner processes (which may be unknown at hwpoison time) 404871f72525SWu Fengguang */ 4049d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 40504779cb31SAndi Kleen goto out_release; 40511da177e4SLinus Torvalds } 40521da177e4SLinus Torvalds 4053fdc724d6SSuren Baghdasaryan ret |= folio_lock_or_retry(folio, vmf); 4054fdc724d6SSuren Baghdasaryan if (ret & VM_FAULT_RETRY) 4055d065bd81SMichel Lespinasse goto out_release; 40561da177e4SLinus Torvalds 405784d60fddSDavid Hildenbrand if (swapcache) { 40584969c119SAndrea Arcangeli /* 40593b344157SMatthew Wilcox (Oracle) * Make sure folio_free_swap() or swapoff did not release the 406084d60fddSDavid Hildenbrand * swapcache from under us. The page pin, and pte_same test 406184d60fddSDavid Hildenbrand * below, are not enough to exclude that. Even if it is still 406284d60fddSDavid Hildenbrand * swapcache, we need to check that the page's swap has not 406384d60fddSDavid Hildenbrand * changed. 40644969c119SAndrea Arcangeli */ 406563ad4addSMatthew Wilcox (Oracle) if (unlikely(!folio_test_swapcache(folio) || 4066cfeed8ffSDavid Hildenbrand page_swap_entry(page).val != entry.val)) 40674969c119SAndrea Arcangeli goto out_page; 40684969c119SAndrea Arcangeli 406984d60fddSDavid Hildenbrand /* 407084d60fddSDavid Hildenbrand * KSM sometimes has to copy on read faults, for example, if 407184d60fddSDavid Hildenbrand * page->index of !PageKSM() pages would be nonlinear inside the 407284d60fddSDavid Hildenbrand * anon VMA -- PageKSM() is lost on actual swapout. 407384d60fddSDavid Hildenbrand */ 407496db66d9SMatthew Wilcox (Oracle) folio = ksm_might_need_to_copy(folio, vma, vmf->address); 407596db66d9SMatthew Wilcox (Oracle) if (unlikely(!folio)) { 40765ad64688SHugh Dickins ret = VM_FAULT_OOM; 407796db66d9SMatthew Wilcox (Oracle) folio = swapcache; 40784969c119SAndrea Arcangeli goto out_page; 407996db66d9SMatthew Wilcox (Oracle) } else if (unlikely(folio == ERR_PTR(-EHWPOISON))) { 40806b970599SKefeng Wang ret = VM_FAULT_HWPOISON; 408196db66d9SMatthew Wilcox (Oracle) folio = swapcache; 40826b970599SKefeng Wang goto out_page; 40834969c119SAndrea Arcangeli } 408496db66d9SMatthew Wilcox (Oracle) if (folio != swapcache) 408596db66d9SMatthew Wilcox (Oracle) page = folio_page(folio, 0); 4086c145e0b4SDavid Hildenbrand 4087c145e0b4SDavid Hildenbrand /* 4088c145e0b4SDavid Hildenbrand * If we want to map a page that's in the swapcache writable, we 4089c145e0b4SDavid Hildenbrand * have to detect via the refcount if we're really the exclusive 4090c145e0b4SDavid Hildenbrand * owner. Try removing the extra reference from the local LRU 40911fec6890SMatthew Wilcox (Oracle) * caches if required. 4092c145e0b4SDavid Hildenbrand */ 4093d4f9565aSMatthew Wilcox (Oracle) if ((vmf->flags & FAULT_FLAG_WRITE) && folio == swapcache && 409463ad4addSMatthew Wilcox (Oracle) !folio_test_ksm(folio) && !folio_test_lru(folio)) 4095c145e0b4SDavid Hildenbrand lru_add_drain(); 409684d60fddSDavid Hildenbrand } 40975ad64688SHugh Dickins 40984231f842SKefeng Wang folio_throttle_swaprate(folio, GFP_KERNEL); 4099073e587eSKAMEZAWA Hiroyuki 41001da177e4SLinus Torvalds /* 41018f4e2101SHugh Dickins * Back out if somebody else already faulted in this pte. 41021da177e4SLinus Torvalds */ 410382b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 410482b0f8c3SJan Kara &vmf->ptl); 4105c33c7948SRyan Roberts if (unlikely(!vmf->pte || !pte_same(ptep_get(vmf->pte), vmf->orig_pte))) 4106b8107480SKirill Korotaev goto out_nomap; 4107b8107480SKirill Korotaev 410863ad4addSMatthew Wilcox (Oracle) if (unlikely(!folio_test_uptodate(folio))) { 4109b8107480SKirill Korotaev ret = VM_FAULT_SIGBUS; 4110b8107480SKirill Korotaev goto out_nomap; 41111da177e4SLinus Torvalds } 41121da177e4SLinus Torvalds 41138c7c6e34SKAMEZAWA Hiroyuki /* 411478fbe906SDavid Hildenbrand * PG_anon_exclusive reuses PG_mappedtodisk for anon pages. A swap pte 411578fbe906SDavid Hildenbrand * must never point at an anonymous page in the swapcache that is 411678fbe906SDavid Hildenbrand * PG_anon_exclusive. Sanity check that this holds and especially, that 411778fbe906SDavid Hildenbrand * no filesystem set PG_mappedtodisk on a page in the swapcache. Sanity 411878fbe906SDavid Hildenbrand * check after taking the PT lock and making sure that nobody 411978fbe906SDavid Hildenbrand * concurrently faulted in this page and set PG_anon_exclusive. 412078fbe906SDavid Hildenbrand */ 412163ad4addSMatthew Wilcox (Oracle) BUG_ON(!folio_test_anon(folio) && folio_test_mappedtodisk(folio)); 412263ad4addSMatthew Wilcox (Oracle) BUG_ON(folio_test_anon(folio) && PageAnonExclusive(page)); 412378fbe906SDavid Hildenbrand 412478fbe906SDavid Hildenbrand /* 41251493a191SDavid Hildenbrand * Check under PT lock (to protect against concurrent fork() sharing 41261493a191SDavid Hildenbrand * the swap entry concurrently) for certainly exclusive pages. 41271493a191SDavid Hildenbrand */ 412863ad4addSMatthew Wilcox (Oracle) if (!folio_test_ksm(folio)) { 41291493a191SDavid Hildenbrand exclusive = pte_swp_exclusive(vmf->orig_pte); 4130d4f9565aSMatthew Wilcox (Oracle) if (folio != swapcache) { 41311493a191SDavid Hildenbrand /* 41321493a191SDavid Hildenbrand * We have a fresh page that is not exposed to the 41331493a191SDavid Hildenbrand * swapcache -> certainly exclusive. 41341493a191SDavid Hildenbrand */ 41351493a191SDavid Hildenbrand exclusive = true; 413663ad4addSMatthew Wilcox (Oracle) } else if (exclusive && folio_test_writeback(folio) && 4137eacde327SMiaohe Lin data_race(si->flags & SWP_STABLE_WRITES)) { 41381493a191SDavid Hildenbrand /* 41391493a191SDavid Hildenbrand * This is tricky: not all swap backends support 41401493a191SDavid Hildenbrand * concurrent page modifications while under writeback. 41411493a191SDavid Hildenbrand * 41421493a191SDavid Hildenbrand * So if we stumble over such a page in the swapcache 41431493a191SDavid Hildenbrand * we must not set the page exclusive, otherwise we can 41441493a191SDavid Hildenbrand * map it writable without further checks and modify it 41451493a191SDavid Hildenbrand * while still under writeback. 41461493a191SDavid Hildenbrand * 41471493a191SDavid Hildenbrand * For these problematic swap backends, simply drop the 41481493a191SDavid Hildenbrand * exclusive marker: this is perfectly fine as we start 41491493a191SDavid Hildenbrand * writeback only if we fully unmapped the page and 41501493a191SDavid Hildenbrand * there are no unexpected references on the page after 41511493a191SDavid Hildenbrand * unmapping succeeded. After fully unmapped, no 41521493a191SDavid Hildenbrand * further GUP references (FOLL_GET and FOLL_PIN) can 41531493a191SDavid Hildenbrand * appear, so dropping the exclusive marker and mapping 41541493a191SDavid Hildenbrand * it only R/O is fine. 41551493a191SDavid Hildenbrand */ 41561493a191SDavid Hildenbrand exclusive = false; 41571493a191SDavid Hildenbrand } 41581493a191SDavid Hildenbrand } 41591493a191SDavid Hildenbrand 41601493a191SDavid Hildenbrand /* 41616dca4ac6SPeter Collingbourne * Some architectures may have to restore extra metadata to the page 41626dca4ac6SPeter Collingbourne * when reading from swap. This metadata may be indexed by swap entry 41636dca4ac6SPeter Collingbourne * so this must be called before swap_free(). 41646dca4ac6SPeter Collingbourne */ 41656dca4ac6SPeter Collingbourne arch_swap_restore(entry, folio); 41666dca4ac6SPeter Collingbourne 41676dca4ac6SPeter Collingbourne /* 4168c145e0b4SDavid Hildenbrand * Remove the swap entry and conditionally try to free up the swapcache. 4169c145e0b4SDavid Hildenbrand * We're already holding a reference on the page but haven't mapped it 4170c145e0b4SDavid Hildenbrand * yet. 41718c7c6e34SKAMEZAWA Hiroyuki */ 4172c145e0b4SDavid Hildenbrand swap_free(entry); 4173a160e537SMatthew Wilcox (Oracle) if (should_try_to_free_swap(folio, vma, vmf->flags)) 4174a160e537SMatthew Wilcox (Oracle) folio_free_swap(folio); 41751da177e4SLinus Torvalds 4176f1a79412SShakeel Butt inc_mm_counter(vma->vm_mm, MM_ANONPAGES); 4177f1a79412SShakeel Butt dec_mm_counter(vma->vm_mm, MM_SWAPENTS); 41781da177e4SLinus Torvalds pte = mk_pte(page, vma->vm_page_prot); 4179c145e0b4SDavid Hildenbrand 4180c145e0b4SDavid Hildenbrand /* 41811493a191SDavid Hildenbrand * Same logic as in do_wp_page(); however, optimize for pages that are 41821493a191SDavid Hildenbrand * certainly not shared either because we just allocated them without 41831493a191SDavid Hildenbrand * exposing them to the swapcache or because the swap entry indicates 41841493a191SDavid Hildenbrand * exclusivity. 4185c145e0b4SDavid Hildenbrand */ 418663ad4addSMatthew Wilcox (Oracle) if (!folio_test_ksm(folio) && 418763ad4addSMatthew Wilcox (Oracle) (exclusive || folio_ref_count(folio) == 1)) { 41886c287605SDavid Hildenbrand if (vmf->flags & FAULT_FLAG_WRITE) { 41891da177e4SLinus Torvalds pte = maybe_mkwrite(pte_mkdirty(pte), vma); 419082b0f8c3SJan Kara vmf->flags &= ~FAULT_FLAG_WRITE; 41916c287605SDavid Hildenbrand } 419214f9135dSDavid Hildenbrand rmap_flags |= RMAP_EXCLUSIVE; 41931da177e4SLinus Torvalds } 41941da177e4SLinus Torvalds flush_icache_page(vma, page); 41952994302bSJan Kara if (pte_swp_soft_dirty(vmf->orig_pte)) 4196179ef71cSCyrill Gorcunov pte = pte_mksoft_dirty(pte); 4197f1eb1bacSPeter Xu if (pte_swp_uffd_wp(vmf->orig_pte)) 4198f45ec5ffSPeter Xu pte = pte_mkuffd_wp(pte); 41992994302bSJan Kara vmf->orig_pte = pte; 42000bcac06fSMinchan Kim 42010bcac06fSMinchan Kim /* ksm created a completely new copy */ 4202d4f9565aSMatthew Wilcox (Oracle) if (unlikely(folio != swapcache && swapcache)) { 42032853b66bSMatthew Wilcox (Oracle) folio_add_new_anon_rmap(folio, vma, vmf->address); 420463ad4addSMatthew Wilcox (Oracle) folio_add_lru_vma(folio, vma); 42050bcac06fSMinchan Kim } else { 4206b832a354SDavid Hildenbrand folio_add_anon_rmap_pte(folio, page, vma, vmf->address, 4207b832a354SDavid Hildenbrand rmap_flags); 420800501b53SJohannes Weiner } 42091da177e4SLinus Torvalds 421063ad4addSMatthew Wilcox (Oracle) VM_BUG_ON(!folio_test_anon(folio) || 421163ad4addSMatthew Wilcox (Oracle) (pte_write(pte) && !PageAnonExclusive(page))); 42121eba86c0SPasha Tatashin set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte); 42131eba86c0SPasha Tatashin arch_do_swap_page(vma->vm_mm, vma, vmf->address, pte, vmf->orig_pte); 42141eba86c0SPasha Tatashin 421563ad4addSMatthew Wilcox (Oracle) folio_unlock(folio); 4216d4f9565aSMatthew Wilcox (Oracle) if (folio != swapcache && swapcache) { 42174969c119SAndrea Arcangeli /* 42184969c119SAndrea Arcangeli * Hold the lock to avoid the swap entry to be reused 42194969c119SAndrea Arcangeli * until we take the PT lock for the pte_same() check 42204969c119SAndrea Arcangeli * (to avoid false positives from pte_same). For 42214969c119SAndrea Arcangeli * further safety release the lock after the swap_free 42224969c119SAndrea Arcangeli * so that the swap count won't change under a 42234969c119SAndrea Arcangeli * parallel locked swapcache. 42244969c119SAndrea Arcangeli */ 4225d4f9565aSMatthew Wilcox (Oracle) folio_unlock(swapcache); 4226d4f9565aSMatthew Wilcox (Oracle) folio_put(swapcache); 42274969c119SAndrea Arcangeli } 4228c475a8abSHugh Dickins 422982b0f8c3SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) { 42302994302bSJan Kara ret |= do_wp_page(vmf); 423161469f1dSHugh Dickins if (ret & VM_FAULT_ERROR) 423261469f1dSHugh Dickins ret &= VM_FAULT_ERROR; 42331da177e4SLinus Torvalds goto out; 42341da177e4SLinus Torvalds } 42351da177e4SLinus Torvalds 42361da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 42375003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vma, vmf->address, vmf->pte, 1); 423865500d23SHugh Dickins unlock: 42393db82b93SHugh Dickins if (vmf->pte) 424082b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 42411da177e4SLinus Torvalds out: 424213ddaf26SKairui Song /* Clear the swap cache pin for direct swapin after PTL unlock */ 424313ddaf26SKairui Song if (need_clear_cache) 424413ddaf26SKairui Song swapcache_clear(si, entry); 42452799e775SMiaohe Lin if (si) 42462799e775SMiaohe Lin put_swap_device(si); 42471da177e4SLinus Torvalds return ret; 4248b8107480SKirill Korotaev out_nomap: 42493db82b93SHugh Dickins if (vmf->pte) 425082b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 4251bc43f75cSJohannes Weiner out_page: 425263ad4addSMatthew Wilcox (Oracle) folio_unlock(folio); 42534779cb31SAndi Kleen out_release: 425463ad4addSMatthew Wilcox (Oracle) folio_put(folio); 4255d4f9565aSMatthew Wilcox (Oracle) if (folio != swapcache && swapcache) { 4256d4f9565aSMatthew Wilcox (Oracle) folio_unlock(swapcache); 4257d4f9565aSMatthew Wilcox (Oracle) folio_put(swapcache); 42584969c119SAndrea Arcangeli } 425913ddaf26SKairui Song if (need_clear_cache) 426013ddaf26SKairui Song swapcache_clear(si, entry); 42612799e775SMiaohe Lin if (si) 42622799e775SMiaohe Lin put_swap_device(si); 426365500d23SHugh Dickins return ret; 42641da177e4SLinus Torvalds } 42651da177e4SLinus Torvalds 426619eaf449SRyan Roberts static bool pte_range_none(pte_t *pte, int nr_pages) 426719eaf449SRyan Roberts { 426819eaf449SRyan Roberts int i; 426919eaf449SRyan Roberts 427019eaf449SRyan Roberts for (i = 0; i < nr_pages; i++) { 427119eaf449SRyan Roberts if (!pte_none(ptep_get_lockless(pte + i))) 427219eaf449SRyan Roberts return false; 427319eaf449SRyan Roberts } 427419eaf449SRyan Roberts 427519eaf449SRyan Roberts return true; 427619eaf449SRyan Roberts } 427719eaf449SRyan Roberts 427819eaf449SRyan Roberts static struct folio *alloc_anon_folio(struct vm_fault *vmf) 427919eaf449SRyan Roberts { 428019eaf449SRyan Roberts struct vm_area_struct *vma = vmf->vma; 4281085ff35eSKefeng Wang #ifdef CONFIG_TRANSPARENT_HUGEPAGE 428219eaf449SRyan Roberts unsigned long orders; 428319eaf449SRyan Roberts struct folio *folio; 428419eaf449SRyan Roberts unsigned long addr; 428519eaf449SRyan Roberts pte_t *pte; 428619eaf449SRyan Roberts gfp_t gfp; 428719eaf449SRyan Roberts int order; 428819eaf449SRyan Roberts 428919eaf449SRyan Roberts /* 429019eaf449SRyan Roberts * If uffd is active for the vma we need per-page fault fidelity to 429119eaf449SRyan Roberts * maintain the uffd semantics. 429219eaf449SRyan Roberts */ 429319eaf449SRyan Roberts if (unlikely(userfaultfd_armed(vma))) 429419eaf449SRyan Roberts goto fallback; 429519eaf449SRyan Roberts 429619eaf449SRyan Roberts /* 429719eaf449SRyan Roberts * Get a list of all the (large) orders below PMD_ORDER that are enabled 429819eaf449SRyan Roberts * for this vma. Then filter out the orders that can't be allocated over 429919eaf449SRyan Roberts * the faulting address and still be fully contained in the vma. 430019eaf449SRyan Roberts */ 430119eaf449SRyan Roberts orders = thp_vma_allowable_orders(vma, vma->vm_flags, false, true, true, 430219eaf449SRyan Roberts BIT(PMD_ORDER) - 1); 430319eaf449SRyan Roberts orders = thp_vma_suitable_orders(vma, vmf->address, orders); 430419eaf449SRyan Roberts 430519eaf449SRyan Roberts if (!orders) 430619eaf449SRyan Roberts goto fallback; 430719eaf449SRyan Roberts 430819eaf449SRyan Roberts pte = pte_offset_map(vmf->pmd, vmf->address & PMD_MASK); 430919eaf449SRyan Roberts if (!pte) 431019eaf449SRyan Roberts return ERR_PTR(-EAGAIN); 431119eaf449SRyan Roberts 431219eaf449SRyan Roberts /* 431319eaf449SRyan Roberts * Find the highest order where the aligned range is completely 431419eaf449SRyan Roberts * pte_none(). Note that all remaining orders will be completely 431519eaf449SRyan Roberts * pte_none(). 431619eaf449SRyan Roberts */ 431719eaf449SRyan Roberts order = highest_order(orders); 431819eaf449SRyan Roberts while (orders) { 431919eaf449SRyan Roberts addr = ALIGN_DOWN(vmf->address, PAGE_SIZE << order); 432019eaf449SRyan Roberts if (pte_range_none(pte + pte_index(addr), 1 << order)) 432119eaf449SRyan Roberts break; 432219eaf449SRyan Roberts order = next_order(&orders, order); 432319eaf449SRyan Roberts } 432419eaf449SRyan Roberts 432519eaf449SRyan Roberts pte_unmap(pte); 432619eaf449SRyan Roberts 432719eaf449SRyan Roberts /* Try allocating the highest of the remaining orders. */ 432819eaf449SRyan Roberts gfp = vma_thp_gfp_mask(vma); 432919eaf449SRyan Roberts while (orders) { 433019eaf449SRyan Roberts addr = ALIGN_DOWN(vmf->address, PAGE_SIZE << order); 433119eaf449SRyan Roberts folio = vma_alloc_folio(gfp, order, vma, addr, true); 433219eaf449SRyan Roberts if (folio) { 4333085ff35eSKefeng Wang if (mem_cgroup_charge(folio, vma->vm_mm, gfp)) { 4334085ff35eSKefeng Wang folio_put(folio); 4335085ff35eSKefeng Wang goto next; 4336085ff35eSKefeng Wang } 4337085ff35eSKefeng Wang folio_throttle_swaprate(folio, gfp); 433819eaf449SRyan Roberts clear_huge_page(&folio->page, vmf->address, 1 << order); 433919eaf449SRyan Roberts return folio; 434019eaf449SRyan Roberts } 4341085ff35eSKefeng Wang next: 434219eaf449SRyan Roberts order = next_order(&orders, order); 434319eaf449SRyan Roberts } 434419eaf449SRyan Roberts 434519eaf449SRyan Roberts fallback: 434619eaf449SRyan Roberts #endif 4347085ff35eSKefeng Wang return folio_prealloc(vma->vm_mm, vma, vmf->address, true); 434819eaf449SRyan Roberts } 434919eaf449SRyan Roberts 43501da177e4SLinus Torvalds /* 4351c1e8d7c6SMichel Lespinasse * We enter with non-exclusive mmap_lock (to exclude vma changes, 43528f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 4353c1e8d7c6SMichel Lespinasse * We return with mmap_lock still held, but pte unmapped and unlocked. 43541da177e4SLinus Torvalds */ 43552b740303SSouptick Joarder static vm_fault_t do_anonymous_page(struct vm_fault *vmf) 43561da177e4SLinus Torvalds { 43572bad466cSPeter Xu bool uffd_wp = vmf_orig_pte_uffd_wp(vmf); 435882b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 435919eaf449SRyan Roberts unsigned long addr = vmf->address; 43606bc56a4dSMatthew Wilcox (Oracle) struct folio *folio; 43612b740303SSouptick Joarder vm_fault_t ret = 0; 436219eaf449SRyan Roberts int nr_pages = 1; 43631da177e4SLinus Torvalds pte_t entry; 436419eaf449SRyan Roberts int i; 43651da177e4SLinus Torvalds 43666b7339f4SKirill A. Shutemov /* File mapping without ->vm_ops ? */ 43676b7339f4SKirill A. Shutemov if (vma->vm_flags & VM_SHARED) 43686b7339f4SKirill A. Shutemov return VM_FAULT_SIGBUS; 43696b7339f4SKirill A. Shutemov 43707267ec00SKirill A. Shutemov /* 43713db82b93SHugh Dickins * Use pte_alloc() instead of pte_alloc_map(), so that OOM can 43723db82b93SHugh Dickins * be distinguished from a transient failure of pte_offset_map(). 43737267ec00SKirill A. Shutemov */ 43744cf58924SJoel Fernandes (Google) if (pte_alloc(vma->vm_mm, vmf->pmd)) 43757267ec00SKirill A. Shutemov return VM_FAULT_OOM; 43767267ec00SKirill A. Shutemov 437711ac5524SLinus Torvalds /* Use the zero-page for reads */ 437882b0f8c3SJan Kara if (!(vmf->flags & FAULT_FLAG_WRITE) && 4379bae473a4SKirill A. Shutemov !mm_forbids_zeropage(vma->vm_mm)) { 438082b0f8c3SJan Kara entry = pte_mkspecial(pfn_pte(my_zero_pfn(vmf->address), 438162eede62SHugh Dickins vma->vm_page_prot)); 438282b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 438382b0f8c3SJan Kara vmf->address, &vmf->ptl); 43843db82b93SHugh Dickins if (!vmf->pte) 43853db82b93SHugh Dickins goto unlock; 43862bad466cSPeter Xu if (vmf_pte_changed(vmf)) { 43877df67697SBibo Mao update_mmu_tlb(vma, vmf->address, vmf->pte); 4388a13ea5b7SHugh Dickins goto unlock; 43897df67697SBibo Mao } 43906b31d595SMichal Hocko ret = check_stable_address_space(vma->vm_mm); 43916b31d595SMichal Hocko if (ret) 43926b31d595SMichal Hocko goto unlock; 43936b251fc9SAndrea Arcangeli /* Deliver the page fault to userland, check inside PT lock */ 43946b251fc9SAndrea Arcangeli if (userfaultfd_missing(vma)) { 439582b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 439682b0f8c3SJan Kara return handle_userfault(vmf, VM_UFFD_MISSING); 43976b251fc9SAndrea Arcangeli } 4398a13ea5b7SHugh Dickins goto setpte; 4399a13ea5b7SHugh Dickins } 4400a13ea5b7SHugh Dickins 44011da177e4SLinus Torvalds /* Allocate our own private page. */ 44021da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 440365500d23SHugh Dickins goto oom; 440419eaf449SRyan Roberts /* Returns NULL on OOM or ERR_PTR(-EAGAIN) if we must retry the fault */ 440519eaf449SRyan Roberts folio = alloc_anon_folio(vmf); 440619eaf449SRyan Roberts if (IS_ERR(folio)) 440719eaf449SRyan Roberts return 0; 44086bc56a4dSMatthew Wilcox (Oracle) if (!folio) 440965500d23SHugh Dickins goto oom; 4410eb3c24f3SMel Gorman 441119eaf449SRyan Roberts nr_pages = folio_nr_pages(folio); 441219eaf449SRyan Roberts addr = ALIGN_DOWN(vmf->address, nr_pages * PAGE_SIZE); 441319eaf449SRyan Roberts 441452f37629SMinchan Kim /* 4415cb3184deSMatthew Wilcox (Oracle) * The memory barrier inside __folio_mark_uptodate makes sure that 4416f4f5329dSWei Yang * preceding stores to the page contents become visible before 441752f37629SMinchan Kim * the set_pte_at() write. 441852f37629SMinchan Kim */ 4419cb3184deSMatthew Wilcox (Oracle) __folio_mark_uptodate(folio); 44201da177e4SLinus Torvalds 4421cb3184deSMatthew Wilcox (Oracle) entry = mk_pte(&folio->page, vma->vm_page_prot); 442250c25ee9SThomas Bogendoerfer entry = pte_sw_mkyoung(entry); 44231ac0cb5dSHugh Dickins if (vma->vm_flags & VM_WRITE) 4424161e393cSRick Edgecombe entry = pte_mkwrite(pte_mkdirty(entry), vma); 44258f4e2101SHugh Dickins 442619eaf449SRyan Roberts vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, addr, &vmf->ptl); 44273db82b93SHugh Dickins if (!vmf->pte) 44283db82b93SHugh Dickins goto release; 442919eaf449SRyan Roberts if (nr_pages == 1 && vmf_pte_changed(vmf)) { 443019eaf449SRyan Roberts update_mmu_tlb(vma, addr, vmf->pte); 443119eaf449SRyan Roberts goto release; 443219eaf449SRyan Roberts } else if (nr_pages > 1 && !pte_range_none(vmf->pte, nr_pages)) { 443319eaf449SRyan Roberts for (i = 0; i < nr_pages; i++) 443419eaf449SRyan Roberts update_mmu_tlb(vma, addr + PAGE_SIZE * i, vmf->pte + i); 44358f4e2101SHugh Dickins goto release; 44367df67697SBibo Mao } 44379ba69294SHugh Dickins 44386b31d595SMichal Hocko ret = check_stable_address_space(vma->vm_mm); 44396b31d595SMichal Hocko if (ret) 44406b31d595SMichal Hocko goto release; 44416b31d595SMichal Hocko 44426b251fc9SAndrea Arcangeli /* Deliver the page fault to userland, check inside PT lock */ 44436b251fc9SAndrea Arcangeli if (userfaultfd_missing(vma)) { 444482b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 4445cb3184deSMatthew Wilcox (Oracle) folio_put(folio); 444682b0f8c3SJan Kara return handle_userfault(vmf, VM_UFFD_MISSING); 44476b251fc9SAndrea Arcangeli } 44486b251fc9SAndrea Arcangeli 444919eaf449SRyan Roberts folio_ref_add(folio, nr_pages - 1); 445019eaf449SRyan Roberts add_mm_counter(vma->vm_mm, MM_ANONPAGES, nr_pages); 445119eaf449SRyan Roberts folio_add_new_anon_rmap(folio, vma, addr); 4452cb3184deSMatthew Wilcox (Oracle) folio_add_lru_vma(folio, vma); 4453a13ea5b7SHugh Dickins setpte: 44542bad466cSPeter Xu if (uffd_wp) 44552bad466cSPeter Xu entry = pte_mkuffd_wp(entry); 445619eaf449SRyan Roberts set_ptes(vma->vm_mm, addr, vmf->pte, entry, nr_pages); 44571da177e4SLinus Torvalds 44581da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 445919eaf449SRyan Roberts update_mmu_cache_range(vmf, vma, addr, vmf->pte, nr_pages); 446065500d23SHugh Dickins unlock: 44613db82b93SHugh Dickins if (vmf->pte) 446282b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 44636b31d595SMichal Hocko return ret; 44648f4e2101SHugh Dickins release: 4465cb3184deSMatthew Wilcox (Oracle) folio_put(folio); 44668f4e2101SHugh Dickins goto unlock; 446765500d23SHugh Dickins oom: 44681da177e4SLinus Torvalds return VM_FAULT_OOM; 44691da177e4SLinus Torvalds } 44701da177e4SLinus Torvalds 44719a95f3cfSPaul Cassella /* 4472c1e8d7c6SMichel Lespinasse * The mmap_lock must have been held on entry, and may have been 44739a95f3cfSPaul Cassella * released depending on flags and vma->vm_ops->fault() return value. 44749a95f3cfSPaul Cassella * See filemap_fault() and __lock_page_retry(). 44759a95f3cfSPaul Cassella */ 44762b740303SSouptick Joarder static vm_fault_t __do_fault(struct vm_fault *vmf) 44777eae74afSKirill A. Shutemov { 447882b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 447901d1e0e6SMatthew Wilcox (Oracle) struct folio *folio; 44802b740303SSouptick Joarder vm_fault_t ret; 44817eae74afSKirill A. Shutemov 448263f3655fSMichal Hocko /* 448363f3655fSMichal Hocko * Preallocate pte before we take page_lock because this might lead to 448463f3655fSMichal Hocko * deadlocks for memcg reclaim which waits for pages under writeback: 448563f3655fSMichal Hocko * lock_page(A) 448663f3655fSMichal Hocko * SetPageWriteback(A) 448763f3655fSMichal Hocko * unlock_page(A) 448863f3655fSMichal Hocko * lock_page(B) 448963f3655fSMichal Hocko * lock_page(B) 4490d383807aSYanfei Xu * pte_alloc_one 449163f3655fSMichal Hocko * shrink_page_list 449263f3655fSMichal Hocko * wait_on_page_writeback(A) 449363f3655fSMichal Hocko * SetPageWriteback(B) 449463f3655fSMichal Hocko * unlock_page(B) 449563f3655fSMichal Hocko * # flush A, B to clear the writeback 449663f3655fSMichal Hocko */ 449763f3655fSMichal Hocko if (pmd_none(*vmf->pmd) && !vmf->prealloc_pte) { 4498a7069ee3SYanfei Xu vmf->prealloc_pte = pte_alloc_one(vma->vm_mm); 449963f3655fSMichal Hocko if (!vmf->prealloc_pte) 450063f3655fSMichal Hocko return VM_FAULT_OOM; 450163f3655fSMichal Hocko } 450263f3655fSMichal Hocko 450311bac800SDave Jiang ret = vma->vm_ops->fault(vmf); 45043917048dSJan Kara if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY | 4505b1aa812bSJan Kara VM_FAULT_DONE_COW))) 45067eae74afSKirill A. Shutemov return ret; 45077eae74afSKirill A. Shutemov 450801d1e0e6SMatthew Wilcox (Oracle) folio = page_folio(vmf->page); 4509667240e0SJan Kara if (unlikely(PageHWPoison(vmf->page))) { 4510e53ac737SRik van Riel vm_fault_t poisonret = VM_FAULT_HWPOISON; 4511e53ac737SRik van Riel if (ret & VM_FAULT_LOCKED) { 451201d1e0e6SMatthew Wilcox (Oracle) if (page_mapped(vmf->page)) 451301d1e0e6SMatthew Wilcox (Oracle) unmap_mapping_folio(folio); 451401d1e0e6SMatthew Wilcox (Oracle) /* Retry if a clean folio was removed from the cache. */ 451501d1e0e6SMatthew Wilcox (Oracle) if (mapping_evict_folio(folio->mapping, folio)) 45163149c79fSRik van Riel poisonret = VM_FAULT_NOPAGE; 451701d1e0e6SMatthew Wilcox (Oracle) folio_unlock(folio); 4518e53ac737SRik van Riel } 451901d1e0e6SMatthew Wilcox (Oracle) folio_put(folio); 4520936ca80dSJan Kara vmf->page = NULL; 4521e53ac737SRik van Riel return poisonret; 45227eae74afSKirill A. Shutemov } 45237eae74afSKirill A. Shutemov 45247eae74afSKirill A. Shutemov if (unlikely(!(ret & VM_FAULT_LOCKED))) 452501d1e0e6SMatthew Wilcox (Oracle) folio_lock(folio); 45267eae74afSKirill A. Shutemov else 452701d1e0e6SMatthew Wilcox (Oracle) VM_BUG_ON_PAGE(!folio_test_locked(folio), vmf->page); 45287eae74afSKirill A. Shutemov 45297eae74afSKirill A. Shutemov return ret; 45307eae74afSKirill A. Shutemov } 45317eae74afSKirill A. Shutemov 4532396bcc52SMatthew Wilcox (Oracle) #ifdef CONFIG_TRANSPARENT_HUGEPAGE 453382b0f8c3SJan Kara static void deposit_prealloc_pte(struct vm_fault *vmf) 4534953c66c2SAneesh Kumar K.V { 453582b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 4536953c66c2SAneesh Kumar K.V 453782b0f8c3SJan Kara pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, vmf->prealloc_pte); 4538953c66c2SAneesh Kumar K.V /* 4539953c66c2SAneesh Kumar K.V * We are going to consume the prealloc table, 4540953c66c2SAneesh Kumar K.V * count that as nr_ptes. 4541953c66c2SAneesh Kumar K.V */ 4542c4812909SKirill A. Shutemov mm_inc_nr_ptes(vma->vm_mm); 45437f2b6ce8STobin C Harding vmf->prealloc_pte = NULL; 4544953c66c2SAneesh Kumar K.V } 4545953c66c2SAneesh Kumar K.V 4546f9ce0be7SKirill A. Shutemov vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page) 454710102459SKirill A. Shutemov { 4548ef37b2eaSDavid Hildenbrand struct folio *folio = page_folio(page); 454982b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 455082b0f8c3SJan Kara bool write = vmf->flags & FAULT_FLAG_WRITE; 455182b0f8c3SJan Kara unsigned long haddr = vmf->address & HPAGE_PMD_MASK; 455210102459SKirill A. Shutemov pmd_t entry; 4553d01ac3c3SMatthew Wilcox (Oracle) vm_fault_t ret = VM_FAULT_FALLBACK; 455410102459SKirill A. Shutemov 45553485b883SRyan Roberts if (!thp_vma_suitable_order(vma, haddr, PMD_ORDER)) 4556d01ac3c3SMatthew Wilcox (Oracle) return ret; 455710102459SKirill A. Shutemov 4558ef37b2eaSDavid Hildenbrand if (page != &folio->page || folio_order(folio) != HPAGE_PMD_ORDER) 4559d01ac3c3SMatthew Wilcox (Oracle) return ret; 456010102459SKirill A. Shutemov 4561953c66c2SAneesh Kumar K.V /* 4562eac96c3eSYang Shi * Just backoff if any subpage of a THP is corrupted otherwise 4563eac96c3eSYang Shi * the corrupted page may mapped by PMD silently to escape the 4564eac96c3eSYang Shi * check. This kind of THP just can be PTE mapped. Access to 4565eac96c3eSYang Shi * the corrupted subpage should trigger SIGBUS as expected. 4566eac96c3eSYang Shi */ 4567ef37b2eaSDavid Hildenbrand if (unlikely(folio_test_has_hwpoisoned(folio))) 4568eac96c3eSYang Shi return ret; 4569eac96c3eSYang Shi 4570eac96c3eSYang Shi /* 4571f0953a1bSIngo Molnar * Archs like ppc64 need additional space to store information 4572953c66c2SAneesh Kumar K.V * related to pte entry. Use the preallocated table for that. 4573953c66c2SAneesh Kumar K.V */ 457482b0f8c3SJan Kara if (arch_needs_pgtable_deposit() && !vmf->prealloc_pte) { 45754cf58924SJoel Fernandes (Google) vmf->prealloc_pte = pte_alloc_one(vma->vm_mm); 457682b0f8c3SJan Kara if (!vmf->prealloc_pte) 4577953c66c2SAneesh Kumar K.V return VM_FAULT_OOM; 4578953c66c2SAneesh Kumar K.V } 4579953c66c2SAneesh Kumar K.V 458082b0f8c3SJan Kara vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); 458182b0f8c3SJan Kara if (unlikely(!pmd_none(*vmf->pmd))) 458210102459SKirill A. Shutemov goto out; 458310102459SKirill A. Shutemov 45849f1f5b60SMatthew Wilcox (Oracle) flush_icache_pages(vma, page, HPAGE_PMD_NR); 458510102459SKirill A. Shutemov 458610102459SKirill A. Shutemov entry = mk_huge_pmd(page, vma->vm_page_prot); 458710102459SKirill A. Shutemov if (write) 4588f55e1014SLinus Torvalds entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); 458910102459SKirill A. Shutemov 45906b27cc6cSKefeng Wang add_mm_counter(vma->vm_mm, mm_counter_file(folio), HPAGE_PMD_NR); 4591ef37b2eaSDavid Hildenbrand folio_add_file_rmap_pmd(folio, page, vma); 4592cea86fe2SHugh Dickins 4593953c66c2SAneesh Kumar K.V /* 4594953c66c2SAneesh Kumar K.V * deposit and withdraw with pmd lock held 4595953c66c2SAneesh Kumar K.V */ 4596953c66c2SAneesh Kumar K.V if (arch_needs_pgtable_deposit()) 459782b0f8c3SJan Kara deposit_prealloc_pte(vmf); 459810102459SKirill A. Shutemov 459982b0f8c3SJan Kara set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry); 460010102459SKirill A. Shutemov 460182b0f8c3SJan Kara update_mmu_cache_pmd(vma, haddr, vmf->pmd); 460210102459SKirill A. Shutemov 460310102459SKirill A. Shutemov /* fault is handled */ 460410102459SKirill A. Shutemov ret = 0; 460595ecedcdSKirill A. Shutemov count_vm_event(THP_FILE_MAPPED); 460610102459SKirill A. Shutemov out: 460782b0f8c3SJan Kara spin_unlock(vmf->ptl); 460810102459SKirill A. Shutemov return ret; 460910102459SKirill A. Shutemov } 461010102459SKirill A. Shutemov #else 4611f9ce0be7SKirill A. Shutemov vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page) 461210102459SKirill A. Shutemov { 4613f9ce0be7SKirill A. Shutemov return VM_FAULT_FALLBACK; 461410102459SKirill A. Shutemov } 461510102459SKirill A. Shutemov #endif 461610102459SKirill A. Shutemov 46173bd786f7SYin Fengwei /** 46183bd786f7SYin Fengwei * set_pte_range - Set a range of PTEs to point to pages in a folio. 46193bd786f7SYin Fengwei * @vmf: Fault decription. 46203bd786f7SYin Fengwei * @folio: The folio that contains @page. 46213bd786f7SYin Fengwei * @page: The first page to create a PTE for. 46223bd786f7SYin Fengwei * @nr: The number of PTEs to create. 46233bd786f7SYin Fengwei * @addr: The first address to create a PTE for. 46243bd786f7SYin Fengwei */ 46253bd786f7SYin Fengwei void set_pte_range(struct vm_fault *vmf, struct folio *folio, 46263bd786f7SYin Fengwei struct page *page, unsigned int nr, unsigned long addr) 46273bb97794SKirill A. Shutemov { 462882b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 46292bad466cSPeter Xu bool uffd_wp = vmf_orig_pte_uffd_wp(vmf); 463082b0f8c3SJan Kara bool write = vmf->flags & FAULT_FLAG_WRITE; 46313bd786f7SYin Fengwei bool prefault = in_range(vmf->address, addr, nr * PAGE_SIZE); 46323bb97794SKirill A. Shutemov pte_t entry; 46337267ec00SKirill A. Shutemov 46343bd786f7SYin Fengwei flush_icache_pages(vma, page, nr); 46353bb97794SKirill A. Shutemov entry = mk_pte(page, vma->vm_page_prot); 463646bdb427SWill Deacon 463746bdb427SWill Deacon if (prefault && arch_wants_old_prefaulted_pte()) 463846bdb427SWill Deacon entry = pte_mkold(entry); 463950c25ee9SThomas Bogendoerfer else 464050c25ee9SThomas Bogendoerfer entry = pte_sw_mkyoung(entry); 464146bdb427SWill Deacon 46423bb97794SKirill A. Shutemov if (write) 46433bb97794SKirill A. Shutemov entry = maybe_mkwrite(pte_mkdirty(entry), vma); 46449c28a205SPeter Xu if (unlikely(uffd_wp)) 4645f1eb1bacSPeter Xu entry = pte_mkuffd_wp(entry); 4646bae473a4SKirill A. Shutemov /* copy-on-write page */ 4647bae473a4SKirill A. Shutemov if (write && !(vma->vm_flags & VM_SHARED)) { 46483bd786f7SYin Fengwei add_mm_counter(vma->vm_mm, MM_ANONPAGES, nr); 46493bd786f7SYin Fengwei VM_BUG_ON_FOLIO(nr != 1, folio); 46503bd786f7SYin Fengwei folio_add_new_anon_rmap(folio, vma, addr); 46513bd786f7SYin Fengwei folio_add_lru_vma(folio, vma); 46523bb97794SKirill A. Shutemov } else { 46536b27cc6cSKefeng Wang add_mm_counter(vma->vm_mm, mm_counter_file(folio), nr); 465468f03208SDavid Hildenbrand folio_add_file_rmap_ptes(folio, page, nr, vma); 46553bb97794SKirill A. Shutemov } 46563bd786f7SYin Fengwei set_ptes(vma->vm_mm, addr, vmf->pte, entry, nr); 46573bd786f7SYin Fengwei 46583bd786f7SYin Fengwei /* no need to invalidate: a not-present page won't be cached */ 46593bd786f7SYin Fengwei update_mmu_cache_range(vmf, vma, addr, vmf->pte, nr); 46603bb97794SKirill A. Shutemov } 46613bb97794SKirill A. Shutemov 4662f46f2adeSPeter Xu static bool vmf_pte_changed(struct vm_fault *vmf) 4663f46f2adeSPeter Xu { 4664f46f2adeSPeter Xu if (vmf->flags & FAULT_FLAG_ORIG_PTE_VALID) 4665c33c7948SRyan Roberts return !pte_same(ptep_get(vmf->pte), vmf->orig_pte); 4666f46f2adeSPeter Xu 4667c33c7948SRyan Roberts return !pte_none(ptep_get(vmf->pte)); 4668f46f2adeSPeter Xu } 4669f46f2adeSPeter Xu 46709118c0cbSJan Kara /** 46719118c0cbSJan Kara * finish_fault - finish page fault once we have prepared the page to fault 46729118c0cbSJan Kara * 46739118c0cbSJan Kara * @vmf: structure describing the fault 46749118c0cbSJan Kara * 46759118c0cbSJan Kara * This function handles all that is needed to finish a page fault once the 46769118c0cbSJan Kara * page to fault in is prepared. It handles locking of PTEs, inserts PTE for 46779118c0cbSJan Kara * given page, adds reverse page mapping, handles memcg charges and LRU 4678a862f68aSMike Rapoport * addition. 46799118c0cbSJan Kara * 46809118c0cbSJan Kara * The function expects the page to be locked and on success it consumes a 46819118c0cbSJan Kara * reference of a page being mapped (for the PTE which maps it). 4682a862f68aSMike Rapoport * 4683a862f68aSMike Rapoport * Return: %0 on success, %VM_FAULT_ code in case of error. 46849118c0cbSJan Kara */ 46852b740303SSouptick Joarder vm_fault_t finish_fault(struct vm_fault *vmf) 46869118c0cbSJan Kara { 4687f9ce0be7SKirill A. Shutemov struct vm_area_struct *vma = vmf->vma; 46889118c0cbSJan Kara struct page *page; 4689f9ce0be7SKirill A. Shutemov vm_fault_t ret; 46909118c0cbSJan Kara 46919118c0cbSJan Kara /* Did we COW the page? */ 4692f9ce0be7SKirill A. Shutemov if ((vmf->flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) 46939118c0cbSJan Kara page = vmf->cow_page; 46949118c0cbSJan Kara else 46959118c0cbSJan Kara page = vmf->page; 46966b31d595SMichal Hocko 46976b31d595SMichal Hocko /* 46986b31d595SMichal Hocko * check even for read faults because we might have lost our CoWed 46996b31d595SMichal Hocko * page 47006b31d595SMichal Hocko */ 4701f9ce0be7SKirill A. Shutemov if (!(vma->vm_flags & VM_SHARED)) { 4702f9ce0be7SKirill A. Shutemov ret = check_stable_address_space(vma->vm_mm); 4703f9ce0be7SKirill A. Shutemov if (ret) 4704f9ce0be7SKirill A. Shutemov return ret; 4705f9ce0be7SKirill A. Shutemov } 4706f9ce0be7SKirill A. Shutemov 4707f9ce0be7SKirill A. Shutemov if (pmd_none(*vmf->pmd)) { 4708f9ce0be7SKirill A. Shutemov if (PageTransCompound(page)) { 4709f9ce0be7SKirill A. Shutemov ret = do_set_pmd(vmf, page); 4710f9ce0be7SKirill A. Shutemov if (ret != VM_FAULT_FALLBACK) 4711f9ce0be7SKirill A. Shutemov return ret; 4712f9ce0be7SKirill A. Shutemov } 4713f9ce0be7SKirill A. Shutemov 471403c4f204SQi Zheng if (vmf->prealloc_pte) 471503c4f204SQi Zheng pmd_install(vma->vm_mm, vmf->pmd, &vmf->prealloc_pte); 471603c4f204SQi Zheng else if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd))) 4717f9ce0be7SKirill A. Shutemov return VM_FAULT_OOM; 4718f9ce0be7SKirill A. Shutemov } 4719f9ce0be7SKirill A. Shutemov 4720f9ce0be7SKirill A. Shutemov vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 4721f9ce0be7SKirill A. Shutemov vmf->address, &vmf->ptl); 47223db82b93SHugh Dickins if (!vmf->pte) 47233db82b93SHugh Dickins return VM_FAULT_NOPAGE; 4724f9ce0be7SKirill A. Shutemov 472570427f6eSSergei Antonov /* Re-check under ptl */ 472670427f6eSSergei Antonov if (likely(!vmf_pte_changed(vmf))) { 47273bd786f7SYin Fengwei struct folio *folio = page_folio(page); 472870427f6eSSergei Antonov 47293bd786f7SYin Fengwei set_pte_range(vmf, folio, page, 1, vmf->address); 473070427f6eSSergei Antonov ret = 0; 473170427f6eSSergei Antonov } else { 4732f9ce0be7SKirill A. Shutemov update_mmu_tlb(vma, vmf->address, vmf->pte); 473370427f6eSSergei Antonov ret = VM_FAULT_NOPAGE; 473470427f6eSSergei Antonov } 473570427f6eSSergei Antonov 47369118c0cbSJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 47379118c0cbSJan Kara return ret; 47389118c0cbSJan Kara } 47399118c0cbSJan Kara 474053d36a56SLorenzo Stoakes static unsigned long fault_around_pages __read_mostly = 474153d36a56SLorenzo Stoakes 65536 >> PAGE_SHIFT; 4742a9b0f861SKirill A. Shutemov 47431592eef0SKirill A. Shutemov #ifdef CONFIG_DEBUG_FS 4744a9b0f861SKirill A. Shutemov static int fault_around_bytes_get(void *data, u64 *val) 47451592eef0SKirill A. Shutemov { 474653d36a56SLorenzo Stoakes *val = fault_around_pages << PAGE_SHIFT; 47471592eef0SKirill A. Shutemov return 0; 47481592eef0SKirill A. Shutemov } 47491592eef0SKirill A. Shutemov 4750b4903d6eSAndrey Ryabinin /* 4751da391d64SWilliam Kucharski * fault_around_bytes must be rounded down to the nearest page order as it's 4752da391d64SWilliam Kucharski * what do_fault_around() expects to see. 4753b4903d6eSAndrey Ryabinin */ 4754a9b0f861SKirill A. Shutemov static int fault_around_bytes_set(void *data, u64 val) 47551592eef0SKirill A. Shutemov { 4756a9b0f861SKirill A. Shutemov if (val / PAGE_SIZE > PTRS_PER_PTE) 47571592eef0SKirill A. Shutemov return -EINVAL; 475853d36a56SLorenzo Stoakes 475953d36a56SLorenzo Stoakes /* 476053d36a56SLorenzo Stoakes * The minimum value is 1 page, however this results in no fault-around 476153d36a56SLorenzo Stoakes * at all. See should_fault_around(). 476253d36a56SLorenzo Stoakes */ 476353d36a56SLorenzo Stoakes fault_around_pages = max(rounddown_pow_of_two(val) >> PAGE_SHIFT, 1UL); 476453d36a56SLorenzo Stoakes 47651592eef0SKirill A. Shutemov return 0; 47661592eef0SKirill A. Shutemov } 47670a1345f8SYevgen Pronenko DEFINE_DEBUGFS_ATTRIBUTE(fault_around_bytes_fops, 4768a9b0f861SKirill A. Shutemov fault_around_bytes_get, fault_around_bytes_set, "%llu\n"); 47691592eef0SKirill A. Shutemov 47701592eef0SKirill A. Shutemov static int __init fault_around_debugfs(void) 47711592eef0SKirill A. Shutemov { 4772d9f7979cSGreg Kroah-Hartman debugfs_create_file_unsafe("fault_around_bytes", 0644, NULL, NULL, 4773a9b0f861SKirill A. Shutemov &fault_around_bytes_fops); 47741592eef0SKirill A. Shutemov return 0; 47751592eef0SKirill A. Shutemov } 47761592eef0SKirill A. Shutemov late_initcall(fault_around_debugfs); 47771592eef0SKirill A. Shutemov #endif 47788c6e50b0SKirill A. Shutemov 47791fdb412bSKirill A. Shutemov /* 47801fdb412bSKirill A. Shutemov * do_fault_around() tries to map few pages around the fault address. The hope 47811fdb412bSKirill A. Shutemov * is that the pages will be needed soon and this will lower the number of 47821fdb412bSKirill A. Shutemov * faults to handle. 47831fdb412bSKirill A. Shutemov * 47841fdb412bSKirill A. Shutemov * It uses vm_ops->map_pages() to map the pages, which skips the page if it's 47851fdb412bSKirill A. Shutemov * not ready to be mapped: not up-to-date, locked, etc. 47861fdb412bSKirill A. Shutemov * 47879042599eSLorenzo Stoakes * This function doesn't cross VMA or page table boundaries, in order to call 47889042599eSLorenzo Stoakes * map_pages() and acquire a PTE lock only once. 47891fdb412bSKirill A. Shutemov * 479053d36a56SLorenzo Stoakes * fault_around_pages defines how many pages we'll try to map. 4791da391d64SWilliam Kucharski * do_fault_around() expects it to be set to a power of two less than or equal 4792da391d64SWilliam Kucharski * to PTRS_PER_PTE. 47931fdb412bSKirill A. Shutemov * 4794da391d64SWilliam Kucharski * The virtual address of the area that we map is naturally aligned to 479553d36a56SLorenzo Stoakes * fault_around_pages * PAGE_SIZE rounded down to the machine page size 4796da391d64SWilliam Kucharski * (and therefore to page order). This way it's easier to guarantee 4797da391d64SWilliam Kucharski * that we don't cross page table boundaries. 47981fdb412bSKirill A. Shutemov */ 47992b740303SSouptick Joarder static vm_fault_t do_fault_around(struct vm_fault *vmf) 48008c6e50b0SKirill A. Shutemov { 480153d36a56SLorenzo Stoakes pgoff_t nr_pages = READ_ONCE(fault_around_pages); 48029042599eSLorenzo Stoakes pgoff_t pte_off = pte_index(vmf->address); 48039042599eSLorenzo Stoakes /* The page offset of vmf->address within the VMA. */ 48049042599eSLorenzo Stoakes pgoff_t vma_off = vmf->pgoff - vmf->vma->vm_pgoff; 48059042599eSLorenzo Stoakes pgoff_t from_pte, to_pte; 480658ef47efSMatthew Wilcox (Oracle) vm_fault_t ret; 48078c6e50b0SKirill A. Shutemov 48089042599eSLorenzo Stoakes /* The PTE offset of the start address, clamped to the VMA. */ 48099042599eSLorenzo Stoakes from_pte = max(ALIGN_DOWN(pte_off, nr_pages), 48109042599eSLorenzo Stoakes pte_off - min(pte_off, vma_off)); 4811aecd6f44SKirill A. Shutemov 48129042599eSLorenzo Stoakes /* The PTE offset of the end address, clamped to the VMA and PTE. */ 48139042599eSLorenzo Stoakes to_pte = min3(from_pte + nr_pages, (pgoff_t)PTRS_PER_PTE, 48149042599eSLorenzo Stoakes pte_off + vma_pages(vmf->vma) - vma_off) - 1; 48158c6e50b0SKirill A. Shutemov 481682b0f8c3SJan Kara if (pmd_none(*vmf->pmd)) { 48174cf58924SJoel Fernandes (Google) vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm); 481882b0f8c3SJan Kara if (!vmf->prealloc_pte) 4819f9ce0be7SKirill A. Shutemov return VM_FAULT_OOM; 48208c6e50b0SKirill A. Shutemov } 48218c6e50b0SKirill A. Shutemov 482258ef47efSMatthew Wilcox (Oracle) rcu_read_lock(); 482358ef47efSMatthew Wilcox (Oracle) ret = vmf->vma->vm_ops->map_pages(vmf, 48249042599eSLorenzo Stoakes vmf->pgoff + from_pte - pte_off, 48259042599eSLorenzo Stoakes vmf->pgoff + to_pte - pte_off); 482658ef47efSMatthew Wilcox (Oracle) rcu_read_unlock(); 482758ef47efSMatthew Wilcox (Oracle) 482858ef47efSMatthew Wilcox (Oracle) return ret; 48297267ec00SKirill A. Shutemov } 48307267ec00SKirill A. Shutemov 48319c28a205SPeter Xu /* Return true if we should do read fault-around, false otherwise */ 48329c28a205SPeter Xu static inline bool should_fault_around(struct vm_fault *vmf) 48339c28a205SPeter Xu { 48349c28a205SPeter Xu /* No ->map_pages? No way to fault around... */ 48359c28a205SPeter Xu if (!vmf->vma->vm_ops->map_pages) 48369c28a205SPeter Xu return false; 48379c28a205SPeter Xu 48389c28a205SPeter Xu if (uffd_disable_fault_around(vmf->vma)) 48399c28a205SPeter Xu return false; 48409c28a205SPeter Xu 484153d36a56SLorenzo Stoakes /* A single page implies no faulting 'around' at all. */ 484253d36a56SLorenzo Stoakes return fault_around_pages > 1; 48439c28a205SPeter Xu } 48449c28a205SPeter Xu 48452b740303SSouptick Joarder static vm_fault_t do_read_fault(struct vm_fault *vmf) 4846e655fb29SKirill A. Shutemov { 48472b740303SSouptick Joarder vm_fault_t ret = 0; 484822d1e68fSSidhartha Kumar struct folio *folio; 48498c6e50b0SKirill A. Shutemov 48508c6e50b0SKirill A. Shutemov /* 48518c6e50b0SKirill A. Shutemov * Let's call ->map_pages() first and use ->fault() as fallback 48528c6e50b0SKirill A. Shutemov * if page by the offset is not ready to be mapped (cold cache or 48538c6e50b0SKirill A. Shutemov * something). 48548c6e50b0SKirill A. Shutemov */ 48559c28a205SPeter Xu if (should_fault_around(vmf)) { 48560721ec8bSJan Kara ret = do_fault_around(vmf); 48577267ec00SKirill A. Shutemov if (ret) 48587267ec00SKirill A. Shutemov return ret; 48598c6e50b0SKirill A. Shutemov } 4860e655fb29SKirill A. Shutemov 486112214ebaSMatthew Wilcox (Oracle) ret = vmf_can_call_fault(vmf); 486212214ebaSMatthew Wilcox (Oracle) if (ret) 486312214ebaSMatthew Wilcox (Oracle) return ret; 4864f5617ffeSMatthew Wilcox (Oracle) 4865936ca80dSJan Kara ret = __do_fault(vmf); 4866e655fb29SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 4867e655fb29SKirill A. Shutemov return ret; 4868e655fb29SKirill A. Shutemov 48699118c0cbSJan Kara ret |= finish_fault(vmf); 487022d1e68fSSidhartha Kumar folio = page_folio(vmf->page); 487122d1e68fSSidhartha Kumar folio_unlock(folio); 48727267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 487322d1e68fSSidhartha Kumar folio_put(folio); 4874e655fb29SKirill A. Shutemov return ret; 4875e655fb29SKirill A. Shutemov } 4876e655fb29SKirill A. Shutemov 48772b740303SSouptick Joarder static vm_fault_t do_cow_fault(struct vm_fault *vmf) 4878ec47c3b9SKirill A. Shutemov { 487982b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 4880e4621e70SKefeng Wang struct folio *folio; 48812b740303SSouptick Joarder vm_fault_t ret; 4882ec47c3b9SKirill A. Shutemov 48834de8c93aSMatthew Wilcox (Oracle) ret = vmf_can_call_fault(vmf); 48844de8c93aSMatthew Wilcox (Oracle) if (!ret) 48854de8c93aSMatthew Wilcox (Oracle) ret = vmf_anon_prepare(vmf); 48864de8c93aSMatthew Wilcox (Oracle) if (ret) 48874de8c93aSMatthew Wilcox (Oracle) return ret; 4888ec47c3b9SKirill A. Shutemov 4889e4621e70SKefeng Wang folio = folio_prealloc(vma->vm_mm, vma, vmf->address, false); 4890e4621e70SKefeng Wang if (!folio) 4891ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 4892ec47c3b9SKirill A. Shutemov 4893e4621e70SKefeng Wang vmf->cow_page = &folio->page; 4894ec47c3b9SKirill A. Shutemov 4895936ca80dSJan Kara ret = __do_fault(vmf); 4896ec47c3b9SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 4897ec47c3b9SKirill A. Shutemov goto uncharge_out; 48983917048dSJan Kara if (ret & VM_FAULT_DONE_COW) 48993917048dSJan Kara return ret; 4900ec47c3b9SKirill A. Shutemov 4901936ca80dSJan Kara copy_user_highpage(vmf->cow_page, vmf->page, vmf->address, vma); 4902e4621e70SKefeng Wang __folio_mark_uptodate(folio); 4903ec47c3b9SKirill A. Shutemov 49049118c0cbSJan Kara ret |= finish_fault(vmf); 4905936ca80dSJan Kara unlock_page(vmf->page); 4906936ca80dSJan Kara put_page(vmf->page); 49077267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 49087267ec00SKirill A. Shutemov goto uncharge_out; 4909ec47c3b9SKirill A. Shutemov return ret; 4910ec47c3b9SKirill A. Shutemov uncharge_out: 4911e4621e70SKefeng Wang folio_put(folio); 4912ec47c3b9SKirill A. Shutemov return ret; 4913ec47c3b9SKirill A. Shutemov } 4914ec47c3b9SKirill A. Shutemov 49152b740303SSouptick Joarder static vm_fault_t do_shared_fault(struct vm_fault *vmf) 49161da177e4SLinus Torvalds { 491782b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 49182b740303SSouptick Joarder vm_fault_t ret, tmp; 49196f609b7eSSidhartha Kumar struct folio *folio; 49201d65f86dSKAMEZAWA Hiroyuki 49214ed43798SMatthew Wilcox (Oracle) ret = vmf_can_call_fault(vmf); 49224ed43798SMatthew Wilcox (Oracle) if (ret) 49234ed43798SMatthew Wilcox (Oracle) return ret; 492454cb8821SNick Piggin 4925936ca80dSJan Kara ret = __do_fault(vmf); 49267eae74afSKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 4927f0c6d4d2SKirill A. Shutemov return ret; 49281da177e4SLinus Torvalds 49296f609b7eSSidhartha Kumar folio = page_folio(vmf->page); 49306f609b7eSSidhartha Kumar 49311da177e4SLinus Torvalds /* 4932f0c6d4d2SKirill A. Shutemov * Check if the backing address space wants to know that the page is 4933f0c6d4d2SKirill A. Shutemov * about to become writable 49341da177e4SLinus Torvalds */ 4935fb09a464SKirill A. Shutemov if (vma->vm_ops->page_mkwrite) { 49366f609b7eSSidhartha Kumar folio_unlock(folio); 493786aa6998SSidhartha Kumar tmp = do_page_mkwrite(vmf, folio); 4938fb09a464SKirill A. Shutemov if (unlikely(!tmp || 4939fb09a464SKirill A. Shutemov (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { 49406f609b7eSSidhartha Kumar folio_put(folio); 4941f0c6d4d2SKirill A. Shutemov return tmp; 494269676147SMark Fasheh } 4943d0217ac0SNick Piggin } 4944fb09a464SKirill A. Shutemov 49459118c0cbSJan Kara ret |= finish_fault(vmf); 49467267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | 49477267ec00SKirill A. Shutemov VM_FAULT_RETRY))) { 49486f609b7eSSidhartha Kumar folio_unlock(folio); 49496f609b7eSSidhartha Kumar folio_put(folio); 4950f0c6d4d2SKirill A. Shutemov return ret; 49519637a5efSDavid Howells } 4952d00806b1SNick Piggin 495389b15332SJohannes Weiner ret |= fault_dirty_shared_page(vmf); 4954b827e496SNick Piggin return ret; 495554cb8821SNick Piggin } 4956d00806b1SNick Piggin 49579a95f3cfSPaul Cassella /* 4958c1e8d7c6SMichel Lespinasse * We enter with non-exclusive mmap_lock (to exclude vma changes, 49599a95f3cfSPaul Cassella * but allow concurrent faults). 4960c1e8d7c6SMichel Lespinasse * The mmap_lock may have been released depending on flags and our 49619138e47eSMatthew Wilcox (Oracle) * return value. See filemap_fault() and __folio_lock_or_retry(). 4962c1e8d7c6SMichel Lespinasse * If mmap_lock is released, vma may become invalid (for example 4963fc8efd2dSJan Stancek * by other thread calling munmap()). 49649a95f3cfSPaul Cassella */ 49652b740303SSouptick Joarder static vm_fault_t do_fault(struct vm_fault *vmf) 496654cb8821SNick Piggin { 496782b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 4968fc8efd2dSJan Stancek struct mm_struct *vm_mm = vma->vm_mm; 49692b740303SSouptick Joarder vm_fault_t ret; 497054cb8821SNick Piggin 4971ff09d7ecSAneesh Kumar K.V /* 4972ff09d7ecSAneesh Kumar K.V * The VMA was not fully populated on mmap() or missing VM_DONTEXPAND 4973ff09d7ecSAneesh Kumar K.V */ 4974ff09d7ecSAneesh Kumar K.V if (!vma->vm_ops->fault) { 49753db82b93SHugh Dickins vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, 49763db82b93SHugh Dickins vmf->address, &vmf->ptl); 49773db82b93SHugh Dickins if (unlikely(!vmf->pte)) 4978b0b9b3dfSHugh Dickins ret = VM_FAULT_SIGBUS; 4979ff09d7ecSAneesh Kumar K.V else { 4980ff09d7ecSAneesh Kumar K.V /* 4981ff09d7ecSAneesh Kumar K.V * Make sure this is not a temporary clearing of pte 4982ff09d7ecSAneesh Kumar K.V * by holding ptl and checking again. A R/M/W update 4983ff09d7ecSAneesh Kumar K.V * of pte involves: take ptl, clearing the pte so that 4984ff09d7ecSAneesh Kumar K.V * we don't have concurrent modification by hardware 4985ff09d7ecSAneesh Kumar K.V * followed by an update. 4986ff09d7ecSAneesh Kumar K.V */ 4987c33c7948SRyan Roberts if (unlikely(pte_none(ptep_get(vmf->pte)))) 4988ff09d7ecSAneesh Kumar K.V ret = VM_FAULT_SIGBUS; 4989ff09d7ecSAneesh Kumar K.V else 4990ff09d7ecSAneesh Kumar K.V ret = VM_FAULT_NOPAGE; 4991ff09d7ecSAneesh Kumar K.V 4992ff09d7ecSAneesh Kumar K.V pte_unmap_unlock(vmf->pte, vmf->ptl); 4993ff09d7ecSAneesh Kumar K.V } 4994ff09d7ecSAneesh Kumar K.V } else if (!(vmf->flags & FAULT_FLAG_WRITE)) 4995b0b9b3dfSHugh Dickins ret = do_read_fault(vmf); 4996b0b9b3dfSHugh Dickins else if (!(vma->vm_flags & VM_SHARED)) 4997b0b9b3dfSHugh Dickins ret = do_cow_fault(vmf); 4998b0b9b3dfSHugh Dickins else 4999b0b9b3dfSHugh Dickins ret = do_shared_fault(vmf); 5000b0b9b3dfSHugh Dickins 5001b0b9b3dfSHugh Dickins /* preallocated pagetable is unused: free it */ 5002b0b9b3dfSHugh Dickins if (vmf->prealloc_pte) { 5003fc8efd2dSJan Stancek pte_free(vm_mm, vmf->prealloc_pte); 50047f2b6ce8STobin C Harding vmf->prealloc_pte = NULL; 5005b0b9b3dfSHugh Dickins } 5006b0b9b3dfSHugh Dickins return ret; 500754cb8821SNick Piggin } 500854cb8821SNick Piggin 5009cda6d936SKefeng Wang int numa_migrate_prep(struct folio *folio, struct vm_area_struct *vma, 5010f4c0d836SYang Shi unsigned long addr, int page_nid, int *flags) 50119532fec1SMel Gorman { 5012cda6d936SKefeng Wang folio_get(folio); 50139532fec1SMel Gorman 5014fc137c0dSRaghavendra K T /* Record the current PID acceesing VMA */ 5015fc137c0dSRaghavendra K T vma_set_access_pid_bit(vma); 5016fc137c0dSRaghavendra K T 50179532fec1SMel Gorman count_vm_numa_event(NUMA_HINT_FAULTS); 501804bb2f94SRik van Riel if (page_nid == numa_node_id()) { 50199532fec1SMel Gorman count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL); 502004bb2f94SRik van Riel *flags |= TNF_FAULT_LOCAL; 502104bb2f94SRik van Riel } 50229532fec1SMel Gorman 502375c70128SKefeng Wang return mpol_misplaced(folio, vma, addr); 50249532fec1SMel Gorman } 50259532fec1SMel Gorman 50262b740303SSouptick Joarder static vm_fault_t do_numa_page(struct vm_fault *vmf) 5027d10e63f2SMel Gorman { 502882b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 50296695cf68SKefeng Wang struct folio *folio = NULL; 50306695cf68SKefeng Wang int nid = NUMA_NO_NODE; 50316a56ccbcSDavid Hildenbrand bool writable = false; 503290572890SPeter Zijlstra int last_cpupid; 5033cbee9f88SPeter Zijlstra int target_nid; 503404a86453SAneesh Kumar K.V pte_t pte, old_pte; 50356688cc05SPeter Zijlstra int flags = 0; 5036d10e63f2SMel Gorman 5037d10e63f2SMel Gorman /* 5038d10e63f2SMel Gorman * The "pte" at this point cannot be used safely without 5039d10e63f2SMel Gorman * validation through pte_unmap_same(). It's of NUMA type but 5040d10e63f2SMel Gorman * the pfn may be screwed if the read is non atomic. 5041d10e63f2SMel Gorman */ 504282b0f8c3SJan Kara spin_lock(vmf->ptl); 5043c33c7948SRyan Roberts if (unlikely(!pte_same(ptep_get(vmf->pte), vmf->orig_pte))) { 504482b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 50454daae3b4SMel Gorman goto out; 50464daae3b4SMel Gorman } 50474daae3b4SMel Gorman 5048b99a342dSHuang Ying /* Get the normal PTE */ 5049b99a342dSHuang Ying old_pte = ptep_get(vmf->pte); 505004a86453SAneesh Kumar K.V pte = pte_modify(old_pte, vma->vm_page_prot); 5051d10e63f2SMel Gorman 50526a56ccbcSDavid Hildenbrand /* 50536a56ccbcSDavid Hildenbrand * Detect now whether the PTE could be writable; this information 50546a56ccbcSDavid Hildenbrand * is only valid while holding the PT lock. 50556a56ccbcSDavid Hildenbrand */ 50566a56ccbcSDavid Hildenbrand writable = pte_write(pte); 50576a56ccbcSDavid Hildenbrand if (!writable && vma_wants_manual_pte_write_upgrade(vma) && 50586a56ccbcSDavid Hildenbrand can_change_pte_writable(vma, vmf->address, pte)) 50596a56ccbcSDavid Hildenbrand writable = true; 50606a56ccbcSDavid Hildenbrand 50616695cf68SKefeng Wang folio = vm_normal_folio(vma, vmf->address, pte); 50626695cf68SKefeng Wang if (!folio || folio_is_zone_device(folio)) 5063b99a342dSHuang Ying goto out_map; 5064d10e63f2SMel Gorman 5065e81c4802SKirill A. Shutemov /* TODO: handle PTE-mapped THP */ 50666695cf68SKefeng Wang if (folio_test_large(folio)) 5067b99a342dSHuang Ying goto out_map; 5068e81c4802SKirill A. Shutemov 50696688cc05SPeter Zijlstra /* 5070bea66fbdSMel Gorman * Avoid grouping on RO pages in general. RO pages shouldn't hurt as 5071bea66fbdSMel Gorman * much anyway since they can be in shared cache state. This misses 5072bea66fbdSMel Gorman * the case where a mapping is writable but the process never writes 5073bea66fbdSMel Gorman * to it but pte_write gets cleared during protection updates and 5074bea66fbdSMel Gorman * pte_dirty has unpredictable behaviour between PTE scan updates, 5075bea66fbdSMel Gorman * background writeback, dirty balancing and application behaviour. 50766688cc05SPeter Zijlstra */ 50776a56ccbcSDavid Hildenbrand if (!writable) 50786688cc05SPeter Zijlstra flags |= TNF_NO_GROUP; 50796688cc05SPeter Zijlstra 5080dabe1d99SRik van Riel /* 50816695cf68SKefeng Wang * Flag if the folio is shared between multiple address spaces. This 5082dabe1d99SRik van Riel * is later used when determining whether to group tasks together 5083dabe1d99SRik van Riel */ 50846695cf68SKefeng Wang if (folio_estimated_sharers(folio) > 1 && (vma->vm_flags & VM_SHARED)) 5085dabe1d99SRik van Riel flags |= TNF_SHARED; 5086dabe1d99SRik van Riel 50876695cf68SKefeng Wang nid = folio_nid(folio); 508833024536SHuang Ying /* 508933024536SHuang Ying * For memory tiering mode, cpupid of slow memory page is used 509033024536SHuang Ying * to record page access time. So use default value. 509133024536SHuang Ying */ 509233024536SHuang Ying if ((sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING) && 50936695cf68SKefeng Wang !node_is_toptier(nid)) 509433024536SHuang Ying last_cpupid = (-1 & LAST_CPUPID_MASK); 509533024536SHuang Ying else 509667b33e3fSKefeng Wang last_cpupid = folio_last_cpupid(folio); 5097cda6d936SKefeng Wang target_nid = numa_migrate_prep(folio, vma, vmf->address, nid, &flags); 509898fa15f3SAnshuman Khandual if (target_nid == NUMA_NO_NODE) { 50996695cf68SKefeng Wang folio_put(folio); 5100b99a342dSHuang Ying goto out_map; 51014daae3b4SMel Gorman } 5102b99a342dSHuang Ying pte_unmap_unlock(vmf->pte, vmf->ptl); 51036a56ccbcSDavid Hildenbrand writable = false; 51044daae3b4SMel Gorman 51054daae3b4SMel Gorman /* Migrate to the requested node */ 51066695cf68SKefeng Wang if (migrate_misplaced_folio(folio, vma, target_nid)) { 51076695cf68SKefeng Wang nid = target_nid; 51086688cc05SPeter Zijlstra flags |= TNF_MIGRATED; 5109b99a342dSHuang Ying } else { 5110074c2381SMel Gorman flags |= TNF_MIGRATE_FAIL; 5111c7ad0880SHugh Dickins vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 5112c7ad0880SHugh Dickins vmf->address, &vmf->ptl); 5113c7ad0880SHugh Dickins if (unlikely(!vmf->pte)) 5114c7ad0880SHugh Dickins goto out; 5115c33c7948SRyan Roberts if (unlikely(!pte_same(ptep_get(vmf->pte), vmf->orig_pte))) { 5116b99a342dSHuang Ying pte_unmap_unlock(vmf->pte, vmf->ptl); 5117b99a342dSHuang Ying goto out; 5118b99a342dSHuang Ying } 5119b99a342dSHuang Ying goto out_map; 5120b99a342dSHuang Ying } 51214daae3b4SMel Gorman 51224daae3b4SMel Gorman out: 51236695cf68SKefeng Wang if (nid != NUMA_NO_NODE) 51246695cf68SKefeng Wang task_numa_fault(last_cpupid, nid, 1, flags); 5125d10e63f2SMel Gorman return 0; 5126b99a342dSHuang Ying out_map: 5127b99a342dSHuang Ying /* 5128b99a342dSHuang Ying * Make it present again, depending on how arch implements 5129b99a342dSHuang Ying * non-accessible ptes, some can allow access by kernel mode. 5130b99a342dSHuang Ying */ 5131b99a342dSHuang Ying old_pte = ptep_modify_prot_start(vma, vmf->address, vmf->pte); 5132b99a342dSHuang Ying pte = pte_modify(old_pte, vma->vm_page_prot); 5133b99a342dSHuang Ying pte = pte_mkyoung(pte); 51346a56ccbcSDavid Hildenbrand if (writable) 5135161e393cSRick Edgecombe pte = pte_mkwrite(pte, vma); 5136b99a342dSHuang Ying ptep_modify_prot_commit(vma, vmf->address, vmf->pte, old_pte, pte); 51375003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vma, vmf->address, vmf->pte, 1); 5138b99a342dSHuang Ying pte_unmap_unlock(vmf->pte, vmf->ptl); 5139b99a342dSHuang Ying goto out; 5140d10e63f2SMel Gorman } 5141d10e63f2SMel Gorman 51422b740303SSouptick Joarder static inline vm_fault_t create_huge_pmd(struct vm_fault *vmf) 5143b96375f7SMatthew Wilcox { 51448f5fd0e1SMatthew Wilcox (Oracle) struct vm_area_struct *vma = vmf->vma; 51458f5fd0e1SMatthew Wilcox (Oracle) if (vma_is_anonymous(vma)) 514682b0f8c3SJan Kara return do_huge_pmd_anonymous_page(vmf); 514740d49a3cSMatthew Wilcox (Oracle) if (vma->vm_ops->huge_fault) 51481d024e7aSMatthew Wilcox (Oracle) return vma->vm_ops->huge_fault(vmf, PMD_ORDER); 5149b96375f7SMatthew Wilcox return VM_FAULT_FALLBACK; 5150b96375f7SMatthew Wilcox } 5151b96375f7SMatthew Wilcox 5152183f24aaSGeert Uytterhoeven /* `inline' is required to avoid gcc 4.1.2 build error */ 51535db4f15cSYang Shi static inline vm_fault_t wp_huge_pmd(struct vm_fault *vmf) 5154b96375f7SMatthew Wilcox { 51558f5fd0e1SMatthew Wilcox (Oracle) struct vm_area_struct *vma = vmf->vma; 5156c89357e2SDavid Hildenbrand const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE; 5157aea06577SDavid Hildenbrand vm_fault_t ret; 5158c89357e2SDavid Hildenbrand 51598f5fd0e1SMatthew Wilcox (Oracle) if (vma_is_anonymous(vma)) { 5160c89357e2SDavid Hildenbrand if (likely(!unshare) && 5161d61ea1cbSPeter Xu userfaultfd_huge_pmd_wp(vma, vmf->orig_pmd)) { 5162d61ea1cbSPeter Xu if (userfaultfd_wp_async(vmf->vma)) 5163d61ea1cbSPeter Xu goto split; 5164529b930bSAndrea Arcangeli return handle_userfault(vmf, VM_UFFD_WP); 5165d61ea1cbSPeter Xu } 51665db4f15cSYang Shi return do_huge_pmd_wp_page(vmf); 5167529b930bSAndrea Arcangeli } 5168af9e4d5fSKirill A. Shutemov 51698f5fd0e1SMatthew Wilcox (Oracle) if (vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) { 51708f5fd0e1SMatthew Wilcox (Oracle) if (vma->vm_ops->huge_fault) { 51711d024e7aSMatthew Wilcox (Oracle) ret = vma->vm_ops->huge_fault(vmf, PMD_ORDER); 5172327e9fd4SThomas Hellstrom (VMware) if (!(ret & VM_FAULT_FALLBACK)) 5173327e9fd4SThomas Hellstrom (VMware) return ret; 5174327e9fd4SThomas Hellstrom (VMware) } 5175aea06577SDavid Hildenbrand } 5176327e9fd4SThomas Hellstrom (VMware) 5177d61ea1cbSPeter Xu split: 5178327e9fd4SThomas Hellstrom (VMware) /* COW or write-notify handled on pte level: split pmd. */ 51798f5fd0e1SMatthew Wilcox (Oracle) __split_huge_pmd(vma, vmf->pmd, vmf->address, false, NULL); 5180af9e4d5fSKirill A. Shutemov 5181b96375f7SMatthew Wilcox return VM_FAULT_FALLBACK; 5182b96375f7SMatthew Wilcox } 5183b96375f7SMatthew Wilcox 51842b740303SSouptick Joarder static vm_fault_t create_huge_pud(struct vm_fault *vmf) 5185a00cc7d9SMatthew Wilcox { 5186327e9fd4SThomas Hellstrom (VMware) #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && \ 5187327e9fd4SThomas Hellstrom (VMware) defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) 5188c4fd825eSMatthew Wilcox (Oracle) struct vm_area_struct *vma = vmf->vma; 5189a00cc7d9SMatthew Wilcox /* No support for anonymous transparent PUD pages yet */ 5190c4fd825eSMatthew Wilcox (Oracle) if (vma_is_anonymous(vma)) 519114c99d65SGowans, James return VM_FAULT_FALLBACK; 519240d49a3cSMatthew Wilcox (Oracle) if (vma->vm_ops->huge_fault) 51931d024e7aSMatthew Wilcox (Oracle) return vma->vm_ops->huge_fault(vmf, PUD_ORDER); 519414c99d65SGowans, James #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 519514c99d65SGowans, James return VM_FAULT_FALLBACK; 519614c99d65SGowans, James } 519714c99d65SGowans, James 519814c99d65SGowans, James static vm_fault_t wp_huge_pud(struct vm_fault *vmf, pud_t orig_pud) 519914c99d65SGowans, James { 520014c99d65SGowans, James #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && \ 520114c99d65SGowans, James defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) 5202c4fd825eSMatthew Wilcox (Oracle) struct vm_area_struct *vma = vmf->vma; 5203aea06577SDavid Hildenbrand vm_fault_t ret; 5204aea06577SDavid Hildenbrand 520514c99d65SGowans, James /* No support for anonymous transparent PUD pages yet */ 5206c4fd825eSMatthew Wilcox (Oracle) if (vma_is_anonymous(vma)) 5207327e9fd4SThomas Hellstrom (VMware) goto split; 5208c4fd825eSMatthew Wilcox (Oracle) if (vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) { 5209c4fd825eSMatthew Wilcox (Oracle) if (vma->vm_ops->huge_fault) { 52101d024e7aSMatthew Wilcox (Oracle) ret = vma->vm_ops->huge_fault(vmf, PUD_ORDER); 5211327e9fd4SThomas Hellstrom (VMware) if (!(ret & VM_FAULT_FALLBACK)) 5212327e9fd4SThomas Hellstrom (VMware) return ret; 5213327e9fd4SThomas Hellstrom (VMware) } 5214aea06577SDavid Hildenbrand } 5215327e9fd4SThomas Hellstrom (VMware) split: 5216327e9fd4SThomas Hellstrom (VMware) /* COW or write-notify not handled on PUD level: split pud.*/ 5217c4fd825eSMatthew Wilcox (Oracle) __split_huge_pud(vma, vmf->pud, vmf->address); 521814c99d65SGowans, James #endif /* CONFIG_TRANSPARENT_HUGEPAGE && CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 5219a00cc7d9SMatthew Wilcox return VM_FAULT_FALLBACK; 5220a00cc7d9SMatthew Wilcox } 5221a00cc7d9SMatthew Wilcox 52221da177e4SLinus Torvalds /* 52231da177e4SLinus Torvalds * These routines also need to handle stuff like marking pages dirty 52241da177e4SLinus Torvalds * and/or accessed for architectures that don't do it in hardware (most 52251da177e4SLinus Torvalds * RISC architectures). The early dirtying is also good on the i386. 52261da177e4SLinus Torvalds * 52271da177e4SLinus Torvalds * There is also a hook called "update_mmu_cache()" that architectures 52281da177e4SLinus Torvalds * with external mmu caches can use to update those (ie the Sparc or 52291da177e4SLinus Torvalds * PowerPC hashed page tables that act as extended TLBs). 52301da177e4SLinus Torvalds * 5231c1e8d7c6SMichel Lespinasse * We enter with non-exclusive mmap_lock (to exclude vma changes, but allow 52327267ec00SKirill A. Shutemov * concurrent faults). 52339a95f3cfSPaul Cassella * 5234c1e8d7c6SMichel Lespinasse * The mmap_lock may have been released depending on flags and our return value. 52359138e47eSMatthew Wilcox (Oracle) * See filemap_fault() and __folio_lock_or_retry(). 52361da177e4SLinus Torvalds */ 52372b740303SSouptick Joarder static vm_fault_t handle_pte_fault(struct vm_fault *vmf) 52381da177e4SLinus Torvalds { 52391da177e4SLinus Torvalds pte_t entry; 52401da177e4SLinus Torvalds 524182b0f8c3SJan Kara if (unlikely(pmd_none(*vmf->pmd))) { 52427267ec00SKirill A. Shutemov /* 52437267ec00SKirill A. Shutemov * Leave __pte_alloc() until later: because vm_ops->fault may 52447267ec00SKirill A. Shutemov * want to allocate huge page, and if we expose page table 52457267ec00SKirill A. Shutemov * for an instant, it will be difficult to retract from 52467267ec00SKirill A. Shutemov * concurrent faults and from rmap lookups. 52477267ec00SKirill A. Shutemov */ 524882b0f8c3SJan Kara vmf->pte = NULL; 5249f46f2adeSPeter Xu vmf->flags &= ~FAULT_FLAG_ORIG_PTE_VALID; 52507267ec00SKirill A. Shutemov } else { 5251f9ce0be7SKirill A. Shutemov /* 52527267ec00SKirill A. Shutemov * A regular pmd is established and it can't morph into a huge 5253c7ad0880SHugh Dickins * pmd by anon khugepaged, since that takes mmap_lock in write 5254c7ad0880SHugh Dickins * mode; but shmem or file collapse to THP could still morph 5255c7ad0880SHugh Dickins * it into a huge pmd: just retry later if so. 52567267ec00SKirill A. Shutemov */ 5257c7ad0880SHugh Dickins vmf->pte = pte_offset_map_nolock(vmf->vma->vm_mm, vmf->pmd, 5258c7ad0880SHugh Dickins vmf->address, &vmf->ptl); 5259c7ad0880SHugh Dickins if (unlikely(!vmf->pte)) 5260c7ad0880SHugh Dickins return 0; 526126e1a0c3SHugh Dickins vmf->orig_pte = ptep_get_lockless(vmf->pte); 5262f46f2adeSPeter Xu vmf->flags |= FAULT_FLAG_ORIG_PTE_VALID; 52637267ec00SKirill A. Shutemov 52642994302bSJan Kara if (pte_none(vmf->orig_pte)) { 526582b0f8c3SJan Kara pte_unmap(vmf->pte); 526682b0f8c3SJan Kara vmf->pte = NULL; 52677267ec00SKirill A. Shutemov } 52687267ec00SKirill A. Shutemov } 52697267ec00SKirill A. Shutemov 52702bad466cSPeter Xu if (!vmf->pte) 52712bad466cSPeter Xu return do_pte_missing(vmf); 52727267ec00SKirill A. Shutemov 52732994302bSJan Kara if (!pte_present(vmf->orig_pte)) 52742994302bSJan Kara return do_swap_page(vmf); 52751da177e4SLinus Torvalds 52762994302bSJan Kara if (pte_protnone(vmf->orig_pte) && vma_is_accessible(vmf->vma)) 52772994302bSJan Kara return do_numa_page(vmf); 5278d10e63f2SMel Gorman 527982b0f8c3SJan Kara spin_lock(vmf->ptl); 52802994302bSJan Kara entry = vmf->orig_pte; 5281c33c7948SRyan Roberts if (unlikely(!pte_same(ptep_get(vmf->pte), entry))) { 52827df67697SBibo Mao update_mmu_tlb(vmf->vma, vmf->address, vmf->pte); 52838f4e2101SHugh Dickins goto unlock; 52847df67697SBibo Mao } 5285c89357e2SDavid Hildenbrand if (vmf->flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) { 5286f6f37321SLinus Torvalds if (!pte_write(entry)) 52872994302bSJan Kara return do_wp_page(vmf); 5288c89357e2SDavid Hildenbrand else if (likely(vmf->flags & FAULT_FLAG_WRITE)) 52891da177e4SLinus Torvalds entry = pte_mkdirty(entry); 52901da177e4SLinus Torvalds } 52911da177e4SLinus Torvalds entry = pte_mkyoung(entry); 529282b0f8c3SJan Kara if (ptep_set_access_flags(vmf->vma, vmf->address, vmf->pte, entry, 529382b0f8c3SJan Kara vmf->flags & FAULT_FLAG_WRITE)) { 52945003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vmf->vma, vmf->address, 52955003a2bdSMatthew Wilcox (Oracle) vmf->pte, 1); 52961a44e149SAndrea Arcangeli } else { 5297b7333b58SYang Shi /* Skip spurious TLB flush for retried page fault */ 5298b7333b58SYang Shi if (vmf->flags & FAULT_FLAG_TRIED) 5299b7333b58SYang Shi goto unlock; 53001a44e149SAndrea Arcangeli /* 53011a44e149SAndrea Arcangeli * This is needed only for protection faults but the arch code 53021a44e149SAndrea Arcangeli * is not yet telling us if this is a protection fault or not. 53031a44e149SAndrea Arcangeli * This still avoids useless tlb flushes for .text page faults 53041a44e149SAndrea Arcangeli * with threads. 53051a44e149SAndrea Arcangeli */ 530682b0f8c3SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) 530799c29133SGerald Schaefer flush_tlb_fix_spurious_fault(vmf->vma, vmf->address, 530899c29133SGerald Schaefer vmf->pte); 53091a44e149SAndrea Arcangeli } 53108f4e2101SHugh Dickins unlock: 531182b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 531283c54070SNick Piggin return 0; 53131da177e4SLinus Torvalds } 53141da177e4SLinus Torvalds 53151da177e4SLinus Torvalds /* 53164ec31152SMatthew Wilcox (Oracle) * On entry, we hold either the VMA lock or the mmap_lock 53174ec31152SMatthew Wilcox (Oracle) * (FAULT_FLAG_VMA_LOCK tells you which). If VM_FAULT_RETRY is set in 53184ec31152SMatthew Wilcox (Oracle) * the result, the mmap_lock is not held on exit. See filemap_fault() 53194ec31152SMatthew Wilcox (Oracle) * and __folio_lock_or_retry(). 53201da177e4SLinus Torvalds */ 53212b740303SSouptick Joarder static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma, 53222b740303SSouptick Joarder unsigned long address, unsigned int flags) 53231da177e4SLinus Torvalds { 532482b0f8c3SJan Kara struct vm_fault vmf = { 5325bae473a4SKirill A. Shutemov .vma = vma, 53261a29d85eSJan Kara .address = address & PAGE_MASK, 5327824ddc60SNadav Amit .real_address = address, 5328bae473a4SKirill A. Shutemov .flags = flags, 53290721ec8bSJan Kara .pgoff = linear_page_index(vma, address), 5330667240e0SJan Kara .gfp_mask = __get_fault_gfp_mask(vma), 5331bae473a4SKirill A. Shutemov }; 5332dcddffd4SKirill A. Shutemov struct mm_struct *mm = vma->vm_mm; 53337da4e2cbSYang Shi unsigned long vm_flags = vma->vm_flags; 53341da177e4SLinus Torvalds pgd_t *pgd; 5335c2febafcSKirill A. Shutemov p4d_t *p4d; 53362b740303SSouptick Joarder vm_fault_t ret; 53371da177e4SLinus Torvalds 53381da177e4SLinus Torvalds pgd = pgd_offset(mm, address); 5339c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, address); 5340c2febafcSKirill A. Shutemov if (!p4d) 5341c2febafcSKirill A. Shutemov return VM_FAULT_OOM; 5342a00cc7d9SMatthew Wilcox 5343c2febafcSKirill A. Shutemov vmf.pud = pud_alloc(mm, p4d, address); 5344a00cc7d9SMatthew Wilcox if (!vmf.pud) 5345c74df32cSHugh Dickins return VM_FAULT_OOM; 5346625110b5SThomas Hellstrom retry_pud: 53477da4e2cbSYang Shi if (pud_none(*vmf.pud) && 53483485b883SRyan Roberts thp_vma_allowable_order(vma, vm_flags, false, true, true, PUD_ORDER)) { 5349a00cc7d9SMatthew Wilcox ret = create_huge_pud(&vmf); 5350a00cc7d9SMatthew Wilcox if (!(ret & VM_FAULT_FALLBACK)) 5351a00cc7d9SMatthew Wilcox return ret; 5352a00cc7d9SMatthew Wilcox } else { 5353a00cc7d9SMatthew Wilcox pud_t orig_pud = *vmf.pud; 5354a00cc7d9SMatthew Wilcox 5355a00cc7d9SMatthew Wilcox barrier(); 5356a00cc7d9SMatthew Wilcox if (pud_trans_huge(orig_pud) || pud_devmap(orig_pud)) { 5357a00cc7d9SMatthew Wilcox 5358c89357e2SDavid Hildenbrand /* 5359c89357e2SDavid Hildenbrand * TODO once we support anonymous PUDs: NUMA case and 5360c89357e2SDavid Hildenbrand * FAULT_FLAG_UNSHARE handling. 5361c89357e2SDavid Hildenbrand */ 5362c89357e2SDavid Hildenbrand if ((flags & FAULT_FLAG_WRITE) && !pud_write(orig_pud)) { 5363a00cc7d9SMatthew Wilcox ret = wp_huge_pud(&vmf, orig_pud); 5364a00cc7d9SMatthew Wilcox if (!(ret & VM_FAULT_FALLBACK)) 5365a00cc7d9SMatthew Wilcox return ret; 5366a00cc7d9SMatthew Wilcox } else { 5367a00cc7d9SMatthew Wilcox huge_pud_set_accessed(&vmf, orig_pud); 5368a00cc7d9SMatthew Wilcox return 0; 5369a00cc7d9SMatthew Wilcox } 5370a00cc7d9SMatthew Wilcox } 5371a00cc7d9SMatthew Wilcox } 5372a00cc7d9SMatthew Wilcox 5373a00cc7d9SMatthew Wilcox vmf.pmd = pmd_alloc(mm, vmf.pud, address); 537482b0f8c3SJan Kara if (!vmf.pmd) 5375c74df32cSHugh Dickins return VM_FAULT_OOM; 5376625110b5SThomas Hellstrom 5377625110b5SThomas Hellstrom /* Huge pud page fault raced with pmd_alloc? */ 5378625110b5SThomas Hellstrom if (pud_trans_unstable(vmf.pud)) 5379625110b5SThomas Hellstrom goto retry_pud; 5380625110b5SThomas Hellstrom 53817da4e2cbSYang Shi if (pmd_none(*vmf.pmd) && 53823485b883SRyan Roberts thp_vma_allowable_order(vma, vm_flags, false, true, true, PMD_ORDER)) { 5383a2d58167SDave Jiang ret = create_huge_pmd(&vmf); 5384c0292554SKirill A. Shutemov if (!(ret & VM_FAULT_FALLBACK)) 5385c0292554SKirill A. Shutemov return ret; 538671e3aac0SAndrea Arcangeli } else { 538726e1a0c3SHugh Dickins vmf.orig_pmd = pmdp_get_lockless(vmf.pmd); 53881f1d06c3SDavid Rientjes 53895db4f15cSYang Shi if (unlikely(is_swap_pmd(vmf.orig_pmd))) { 539084c3fc4eSZi Yan VM_BUG_ON(thp_migration_supported() && 53915db4f15cSYang Shi !is_pmd_migration_entry(vmf.orig_pmd)); 53925db4f15cSYang Shi if (is_pmd_migration_entry(vmf.orig_pmd)) 539384c3fc4eSZi Yan pmd_migration_entry_wait(mm, vmf.pmd); 539484c3fc4eSZi Yan return 0; 539584c3fc4eSZi Yan } 53965db4f15cSYang Shi if (pmd_trans_huge(vmf.orig_pmd) || pmd_devmap(vmf.orig_pmd)) { 53975db4f15cSYang Shi if (pmd_protnone(vmf.orig_pmd) && vma_is_accessible(vma)) 53985db4f15cSYang Shi return do_huge_pmd_numa_page(&vmf); 5399d10e63f2SMel Gorman 5400c89357e2SDavid Hildenbrand if ((flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) && 5401c89357e2SDavid Hildenbrand !pmd_write(vmf.orig_pmd)) { 54025db4f15cSYang Shi ret = wp_huge_pmd(&vmf); 54039845cbbdSKirill A. Shutemov if (!(ret & VM_FAULT_FALLBACK)) 54041f1d06c3SDavid Rientjes return ret; 5405a1dd450bSWill Deacon } else { 54065db4f15cSYang Shi huge_pmd_set_accessed(&vmf); 540771e3aac0SAndrea Arcangeli return 0; 540871e3aac0SAndrea Arcangeli } 540971e3aac0SAndrea Arcangeli } 54109845cbbdSKirill A. Shutemov } 541171e3aac0SAndrea Arcangeli 541282b0f8c3SJan Kara return handle_pte_fault(&vmf); 54131da177e4SLinus Torvalds } 54141da177e4SLinus Torvalds 5415bce617edSPeter Xu /** 5416f0953a1bSIngo Molnar * mm_account_fault - Do page fault accounting 5417809ef83cSYang Li * @mm: mm from which memcg should be extracted. It can be NULL. 5418bce617edSPeter Xu * @regs: the pt_regs struct pointer. When set to NULL, will skip accounting 5419bce617edSPeter Xu * of perf event counters, but we'll still do the per-task accounting to 5420bce617edSPeter Xu * the task who triggered this page fault. 5421bce617edSPeter Xu * @address: the faulted address. 5422bce617edSPeter Xu * @flags: the fault flags. 5423bce617edSPeter Xu * @ret: the fault retcode. 5424bce617edSPeter Xu * 5425f0953a1bSIngo Molnar * This will take care of most of the page fault accounting. Meanwhile, it 5426bce617edSPeter Xu * will also include the PERF_COUNT_SW_PAGE_FAULTS_[MAJ|MIN] perf counter 5427f0953a1bSIngo Molnar * updates. However, note that the handling of PERF_COUNT_SW_PAGE_FAULTS should 5428bce617edSPeter Xu * still be in per-arch page fault handlers at the entry of page fault. 5429bce617edSPeter Xu */ 543053156443SSuren Baghdasaryan static inline void mm_account_fault(struct mm_struct *mm, struct pt_regs *regs, 5431bce617edSPeter Xu unsigned long address, unsigned int flags, 5432bce617edSPeter Xu vm_fault_t ret) 5433bce617edSPeter Xu { 5434bce617edSPeter Xu bool major; 5435bce617edSPeter Xu 543653156443SSuren Baghdasaryan /* Incomplete faults will be accounted upon completion. */ 543753156443SSuren Baghdasaryan if (ret & VM_FAULT_RETRY) 543853156443SSuren Baghdasaryan return; 543953156443SSuren Baghdasaryan 5440bce617edSPeter Xu /* 544153156443SSuren Baghdasaryan * To preserve the behavior of older kernels, PGFAULT counters record 544253156443SSuren Baghdasaryan * both successful and failed faults, as opposed to perf counters, 544353156443SSuren Baghdasaryan * which ignore failed cases. 5444bce617edSPeter Xu */ 544553156443SSuren Baghdasaryan count_vm_event(PGFAULT); 544653156443SSuren Baghdasaryan count_memcg_event_mm(mm, PGFAULT); 544753156443SSuren Baghdasaryan 544853156443SSuren Baghdasaryan /* 544953156443SSuren Baghdasaryan * Do not account for unsuccessful faults (e.g. when the address wasn't 545053156443SSuren Baghdasaryan * valid). That includes arch_vma_access_permitted() failing before 545153156443SSuren Baghdasaryan * reaching here. So this is not a "this many hardware page faults" 545253156443SSuren Baghdasaryan * counter. We should use the hw profiling for that. 545353156443SSuren Baghdasaryan */ 545453156443SSuren Baghdasaryan if (ret & VM_FAULT_ERROR) 5455bce617edSPeter Xu return; 5456bce617edSPeter Xu 5457bce617edSPeter Xu /* 5458bce617edSPeter Xu * We define the fault as a major fault when the final successful fault 5459bce617edSPeter Xu * is VM_FAULT_MAJOR, or if it retried (which implies that we couldn't 5460bce617edSPeter Xu * handle it immediately previously). 5461bce617edSPeter Xu */ 5462bce617edSPeter Xu major = (ret & VM_FAULT_MAJOR) || (flags & FAULT_FLAG_TRIED); 5463bce617edSPeter Xu 5464a2beb5f1SPeter Xu if (major) 5465a2beb5f1SPeter Xu current->maj_flt++; 5466a2beb5f1SPeter Xu else 5467a2beb5f1SPeter Xu current->min_flt++; 5468a2beb5f1SPeter Xu 5469bce617edSPeter Xu /* 5470a2beb5f1SPeter Xu * If the fault is done for GUP, regs will be NULL. We only do the 5471a2beb5f1SPeter Xu * accounting for the per thread fault counters who triggered the 5472a2beb5f1SPeter Xu * fault, and we skip the perf event updates. 5473bce617edSPeter Xu */ 5474bce617edSPeter Xu if (!regs) 5475bce617edSPeter Xu return; 5476bce617edSPeter Xu 5477a2beb5f1SPeter Xu if (major) 5478bce617edSPeter Xu perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address); 5479a2beb5f1SPeter Xu else 5480bce617edSPeter Xu perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address); 5481bce617edSPeter Xu } 5482bce617edSPeter Xu 5483ec1c86b2SYu Zhao #ifdef CONFIG_LRU_GEN 5484ec1c86b2SYu Zhao static void lru_gen_enter_fault(struct vm_area_struct *vma) 5485ec1c86b2SYu Zhao { 54868788f678SYu Zhao /* the LRU algorithm only applies to accesses with recency */ 54878788f678SYu Zhao current->in_lru_fault = vma_has_recency(vma); 5488ec1c86b2SYu Zhao } 5489ec1c86b2SYu Zhao 5490ec1c86b2SYu Zhao static void lru_gen_exit_fault(void) 5491ec1c86b2SYu Zhao { 5492ec1c86b2SYu Zhao current->in_lru_fault = false; 5493ec1c86b2SYu Zhao } 5494ec1c86b2SYu Zhao #else 5495ec1c86b2SYu Zhao static void lru_gen_enter_fault(struct vm_area_struct *vma) 5496ec1c86b2SYu Zhao { 5497ec1c86b2SYu Zhao } 5498ec1c86b2SYu Zhao 5499ec1c86b2SYu Zhao static void lru_gen_exit_fault(void) 5500ec1c86b2SYu Zhao { 5501ec1c86b2SYu Zhao } 5502ec1c86b2SYu Zhao #endif /* CONFIG_LRU_GEN */ 5503ec1c86b2SYu Zhao 5504cdc5021cSDavid Hildenbrand static vm_fault_t sanitize_fault_flags(struct vm_area_struct *vma, 5505cdc5021cSDavid Hildenbrand unsigned int *flags) 5506cdc5021cSDavid Hildenbrand { 5507cdc5021cSDavid Hildenbrand if (unlikely(*flags & FAULT_FLAG_UNSHARE)) { 5508cdc5021cSDavid Hildenbrand if (WARN_ON_ONCE(*flags & FAULT_FLAG_WRITE)) 5509cdc5021cSDavid Hildenbrand return VM_FAULT_SIGSEGV; 5510cdc5021cSDavid Hildenbrand /* 5511cdc5021cSDavid Hildenbrand * FAULT_FLAG_UNSHARE only applies to COW mappings. Let's 5512cdc5021cSDavid Hildenbrand * just treat it like an ordinary read-fault otherwise. 5513cdc5021cSDavid Hildenbrand */ 5514cdc5021cSDavid Hildenbrand if (!is_cow_mapping(vma->vm_flags)) 5515cdc5021cSDavid Hildenbrand *flags &= ~FAULT_FLAG_UNSHARE; 551679881fedSDavid Hildenbrand } else if (*flags & FAULT_FLAG_WRITE) { 551779881fedSDavid Hildenbrand /* Write faults on read-only mappings are impossible ... */ 551879881fedSDavid Hildenbrand if (WARN_ON_ONCE(!(vma->vm_flags & VM_MAYWRITE))) 551979881fedSDavid Hildenbrand return VM_FAULT_SIGSEGV; 552079881fedSDavid Hildenbrand /* ... and FOLL_FORCE only applies to COW mappings. */ 552179881fedSDavid Hildenbrand if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE) && 552279881fedSDavid Hildenbrand !is_cow_mapping(vma->vm_flags))) 552379881fedSDavid Hildenbrand return VM_FAULT_SIGSEGV; 5524cdc5021cSDavid Hildenbrand } 55254089eef0SSuren Baghdasaryan #ifdef CONFIG_PER_VMA_LOCK 55264089eef0SSuren Baghdasaryan /* 55274089eef0SSuren Baghdasaryan * Per-VMA locks can't be used with FAULT_FLAG_RETRY_NOWAIT because of 55284089eef0SSuren Baghdasaryan * the assumption that lock is dropped on VM_FAULT_RETRY. 55294089eef0SSuren Baghdasaryan */ 55304089eef0SSuren Baghdasaryan if (WARN_ON_ONCE((*flags & 55314089eef0SSuren Baghdasaryan (FAULT_FLAG_VMA_LOCK | FAULT_FLAG_RETRY_NOWAIT)) == 55324089eef0SSuren Baghdasaryan (FAULT_FLAG_VMA_LOCK | FAULT_FLAG_RETRY_NOWAIT))) 55334089eef0SSuren Baghdasaryan return VM_FAULT_SIGSEGV; 55344089eef0SSuren Baghdasaryan #endif 55354089eef0SSuren Baghdasaryan 5536cdc5021cSDavid Hildenbrand return 0; 5537cdc5021cSDavid Hildenbrand } 5538cdc5021cSDavid Hildenbrand 55399a95f3cfSPaul Cassella /* 55409a95f3cfSPaul Cassella * By the time we get here, we already hold the mm semaphore 55419a95f3cfSPaul Cassella * 5542c1e8d7c6SMichel Lespinasse * The mmap_lock may have been released depending on flags and our 55439138e47eSMatthew Wilcox (Oracle) * return value. See filemap_fault() and __folio_lock_or_retry(). 55449a95f3cfSPaul Cassella */ 55452b740303SSouptick Joarder vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address, 5546bce617edSPeter Xu unsigned int flags, struct pt_regs *regs) 5547519e5247SJohannes Weiner { 554853156443SSuren Baghdasaryan /* If the fault handler drops the mmap_lock, vma may be freed */ 554953156443SSuren Baghdasaryan struct mm_struct *mm = vma->vm_mm; 55502b740303SSouptick Joarder vm_fault_t ret; 5551519e5247SJohannes Weiner 5552519e5247SJohannes Weiner __set_current_state(TASK_RUNNING); 5553519e5247SJohannes Weiner 5554cdc5021cSDavid Hildenbrand ret = sanitize_fault_flags(vma, &flags); 5555cdc5021cSDavid Hildenbrand if (ret) 555653156443SSuren Baghdasaryan goto out; 5557cdc5021cSDavid Hildenbrand 5558de0c799bSLaurent Dufour if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE, 5559de0c799bSLaurent Dufour flags & FAULT_FLAG_INSTRUCTION, 556053156443SSuren Baghdasaryan flags & FAULT_FLAG_REMOTE)) { 556153156443SSuren Baghdasaryan ret = VM_FAULT_SIGSEGV; 556253156443SSuren Baghdasaryan goto out; 556353156443SSuren Baghdasaryan } 5564de0c799bSLaurent Dufour 5565519e5247SJohannes Weiner /* 5566519e5247SJohannes Weiner * Enable the memcg OOM handling for faults triggered in user 5567519e5247SJohannes Weiner * space. Kernel faults are handled more gracefully. 5568519e5247SJohannes Weiner */ 5569519e5247SJohannes Weiner if (flags & FAULT_FLAG_USER) 557029ef680aSMichal Hocko mem_cgroup_enter_user_fault(); 5571519e5247SJohannes Weiner 5572ec1c86b2SYu Zhao lru_gen_enter_fault(vma); 5573ec1c86b2SYu Zhao 5574bae473a4SKirill A. Shutemov if (unlikely(is_vm_hugetlb_page(vma))) 5575bae473a4SKirill A. Shutemov ret = hugetlb_fault(vma->vm_mm, vma, address, flags); 5576bae473a4SKirill A. Shutemov else 5577dcddffd4SKirill A. Shutemov ret = __handle_mm_fault(vma, address, flags); 5578519e5247SJohannes Weiner 5579ec1c86b2SYu Zhao lru_gen_exit_fault(); 5580ec1c86b2SYu Zhao 558149426420SJohannes Weiner if (flags & FAULT_FLAG_USER) { 558229ef680aSMichal Hocko mem_cgroup_exit_user_fault(); 558349426420SJohannes Weiner /* 558449426420SJohannes Weiner * The task may have entered a memcg OOM situation but 558549426420SJohannes Weiner * if the allocation error was handled gracefully (no 558649426420SJohannes Weiner * VM_FAULT_OOM), there is no need to kill anything. 558749426420SJohannes Weiner * Just clean up the OOM state peacefully. 558849426420SJohannes Weiner */ 558949426420SJohannes Weiner if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM)) 559049426420SJohannes Weiner mem_cgroup_oom_synchronize(false); 559149426420SJohannes Weiner } 559253156443SSuren Baghdasaryan out: 559353156443SSuren Baghdasaryan mm_account_fault(mm, regs, address, flags, ret); 5594bce617edSPeter Xu 5595519e5247SJohannes Weiner return ret; 5596519e5247SJohannes Weiner } 5597e1d6d01aSJesse Barnes EXPORT_SYMBOL_GPL(handle_mm_fault); 5598519e5247SJohannes Weiner 5599c2508ec5SLinus Torvalds #ifdef CONFIG_LOCK_MM_AND_FIND_VMA 5600c2508ec5SLinus Torvalds #include <linux/extable.h> 5601c2508ec5SLinus Torvalds 5602c2508ec5SLinus Torvalds static inline bool get_mmap_lock_carefully(struct mm_struct *mm, struct pt_regs *regs) 5603c2508ec5SLinus Torvalds { 56044542057eSLinus Torvalds if (likely(mmap_read_trylock(mm))) 5605c2508ec5SLinus Torvalds return true; 5606c2508ec5SLinus Torvalds 5607c2508ec5SLinus Torvalds if (regs && !user_mode(regs)) { 56088fa50708SJiaxun Yang unsigned long ip = exception_ip(regs); 5609c2508ec5SLinus Torvalds if (!search_exception_tables(ip)) 5610c2508ec5SLinus Torvalds return false; 5611c2508ec5SLinus Torvalds } 5612c2508ec5SLinus Torvalds 5613eda00472SLinus Torvalds return !mmap_read_lock_killable(mm); 5614c2508ec5SLinus Torvalds } 5615c2508ec5SLinus Torvalds 5616c2508ec5SLinus Torvalds static inline bool mmap_upgrade_trylock(struct mm_struct *mm) 5617c2508ec5SLinus Torvalds { 5618c2508ec5SLinus Torvalds /* 5619c2508ec5SLinus Torvalds * We don't have this operation yet. 5620c2508ec5SLinus Torvalds * 5621c2508ec5SLinus Torvalds * It should be easy enough to do: it's basically a 5622c2508ec5SLinus Torvalds * atomic_long_try_cmpxchg_acquire() 5623c2508ec5SLinus Torvalds * from RWSEM_READER_BIAS -> RWSEM_WRITER_LOCKED, but 5624c2508ec5SLinus Torvalds * it also needs the proper lockdep magic etc. 5625c2508ec5SLinus Torvalds */ 5626c2508ec5SLinus Torvalds return false; 5627c2508ec5SLinus Torvalds } 5628c2508ec5SLinus Torvalds 5629c2508ec5SLinus Torvalds static inline bool upgrade_mmap_lock_carefully(struct mm_struct *mm, struct pt_regs *regs) 5630c2508ec5SLinus Torvalds { 5631c2508ec5SLinus Torvalds mmap_read_unlock(mm); 5632c2508ec5SLinus Torvalds if (regs && !user_mode(regs)) { 56338fa50708SJiaxun Yang unsigned long ip = exception_ip(regs); 5634c2508ec5SLinus Torvalds if (!search_exception_tables(ip)) 5635c2508ec5SLinus Torvalds return false; 5636c2508ec5SLinus Torvalds } 5637eda00472SLinus Torvalds return !mmap_write_lock_killable(mm); 5638c2508ec5SLinus Torvalds } 5639c2508ec5SLinus Torvalds 5640c2508ec5SLinus Torvalds /* 5641c2508ec5SLinus Torvalds * Helper for page fault handling. 5642c2508ec5SLinus Torvalds * 5643c2508ec5SLinus Torvalds * This is kind of equivalend to "mmap_read_lock()" followed 5644c2508ec5SLinus Torvalds * by "find_extend_vma()", except it's a lot more careful about 5645c2508ec5SLinus Torvalds * the locking (and will drop the lock on failure). 5646c2508ec5SLinus Torvalds * 5647c2508ec5SLinus Torvalds * For example, if we have a kernel bug that causes a page 5648c2508ec5SLinus Torvalds * fault, we don't want to just use mmap_read_lock() to get 5649c2508ec5SLinus Torvalds * the mm lock, because that would deadlock if the bug were 5650c2508ec5SLinus Torvalds * to happen while we're holding the mm lock for writing. 5651c2508ec5SLinus Torvalds * 5652c2508ec5SLinus Torvalds * So this checks the exception tables on kernel faults in 5653c2508ec5SLinus Torvalds * order to only do this all for instructions that are actually 5654c2508ec5SLinus Torvalds * expected to fault. 5655c2508ec5SLinus Torvalds * 5656c2508ec5SLinus Torvalds * We can also actually take the mm lock for writing if we 5657c2508ec5SLinus Torvalds * need to extend the vma, which helps the VM layer a lot. 5658c2508ec5SLinus Torvalds */ 5659c2508ec5SLinus Torvalds struct vm_area_struct *lock_mm_and_find_vma(struct mm_struct *mm, 5660c2508ec5SLinus Torvalds unsigned long addr, struct pt_regs *regs) 5661c2508ec5SLinus Torvalds { 5662c2508ec5SLinus Torvalds struct vm_area_struct *vma; 5663c2508ec5SLinus Torvalds 5664c2508ec5SLinus Torvalds if (!get_mmap_lock_carefully(mm, regs)) 5665c2508ec5SLinus Torvalds return NULL; 5666c2508ec5SLinus Torvalds 5667c2508ec5SLinus Torvalds vma = find_vma(mm, addr); 5668c2508ec5SLinus Torvalds if (likely(vma && (vma->vm_start <= addr))) 5669c2508ec5SLinus Torvalds return vma; 5670c2508ec5SLinus Torvalds 5671c2508ec5SLinus Torvalds /* 5672c2508ec5SLinus Torvalds * Well, dang. We might still be successful, but only 5673c2508ec5SLinus Torvalds * if we can extend a vma to do so. 5674c2508ec5SLinus Torvalds */ 5675c2508ec5SLinus Torvalds if (!vma || !(vma->vm_flags & VM_GROWSDOWN)) { 5676c2508ec5SLinus Torvalds mmap_read_unlock(mm); 5677c2508ec5SLinus Torvalds return NULL; 5678c2508ec5SLinus Torvalds } 5679c2508ec5SLinus Torvalds 5680c2508ec5SLinus Torvalds /* 5681c2508ec5SLinus Torvalds * We can try to upgrade the mmap lock atomically, 5682c2508ec5SLinus Torvalds * in which case we can continue to use the vma 5683c2508ec5SLinus Torvalds * we already looked up. 5684c2508ec5SLinus Torvalds * 5685c2508ec5SLinus Torvalds * Otherwise we'll have to drop the mmap lock and 5686c2508ec5SLinus Torvalds * re-take it, and also look up the vma again, 5687c2508ec5SLinus Torvalds * re-checking it. 5688c2508ec5SLinus Torvalds */ 5689c2508ec5SLinus Torvalds if (!mmap_upgrade_trylock(mm)) { 5690c2508ec5SLinus Torvalds if (!upgrade_mmap_lock_carefully(mm, regs)) 5691c2508ec5SLinus Torvalds return NULL; 5692c2508ec5SLinus Torvalds 5693c2508ec5SLinus Torvalds vma = find_vma(mm, addr); 5694c2508ec5SLinus Torvalds if (!vma) 5695c2508ec5SLinus Torvalds goto fail; 5696c2508ec5SLinus Torvalds if (vma->vm_start <= addr) 5697c2508ec5SLinus Torvalds goto success; 5698c2508ec5SLinus Torvalds if (!(vma->vm_flags & VM_GROWSDOWN)) 5699c2508ec5SLinus Torvalds goto fail; 5700c2508ec5SLinus Torvalds } 5701c2508ec5SLinus Torvalds 57028d7071afSLinus Torvalds if (expand_stack_locked(vma, addr)) 5703c2508ec5SLinus Torvalds goto fail; 5704c2508ec5SLinus Torvalds 5705c2508ec5SLinus Torvalds success: 5706c2508ec5SLinus Torvalds mmap_write_downgrade(mm); 5707c2508ec5SLinus Torvalds return vma; 5708c2508ec5SLinus Torvalds 5709c2508ec5SLinus Torvalds fail: 5710c2508ec5SLinus Torvalds mmap_write_unlock(mm); 5711c2508ec5SLinus Torvalds return NULL; 5712c2508ec5SLinus Torvalds } 5713c2508ec5SLinus Torvalds #endif 5714c2508ec5SLinus Torvalds 571550ee3253SSuren Baghdasaryan #ifdef CONFIG_PER_VMA_LOCK 571650ee3253SSuren Baghdasaryan /* 571750ee3253SSuren Baghdasaryan * Lookup and lock a VMA under RCU protection. Returned VMA is guaranteed to be 571850ee3253SSuren Baghdasaryan * stable and not isolated. If the VMA is not found or is being modified the 571950ee3253SSuren Baghdasaryan * function returns NULL. 572050ee3253SSuren Baghdasaryan */ 572150ee3253SSuren Baghdasaryan struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm, 572250ee3253SSuren Baghdasaryan unsigned long address) 572350ee3253SSuren Baghdasaryan { 572450ee3253SSuren Baghdasaryan MA_STATE(mas, &mm->mm_mt, address, address); 572550ee3253SSuren Baghdasaryan struct vm_area_struct *vma; 572650ee3253SSuren Baghdasaryan 572750ee3253SSuren Baghdasaryan rcu_read_lock(); 572850ee3253SSuren Baghdasaryan retry: 572950ee3253SSuren Baghdasaryan vma = mas_walk(&mas); 573050ee3253SSuren Baghdasaryan if (!vma) 573150ee3253SSuren Baghdasaryan goto inval; 573250ee3253SSuren Baghdasaryan 573350ee3253SSuren Baghdasaryan if (!vma_start_read(vma)) 573450ee3253SSuren Baghdasaryan goto inval; 573550ee3253SSuren Baghdasaryan 5736444eeb17SSuren Baghdasaryan /* 5737657b5146SJann Horn * find_mergeable_anon_vma uses adjacent vmas which are not locked. 5738657b5146SJann Horn * This check must happen after vma_start_read(); otherwise, a 5739657b5146SJann Horn * concurrent mremap() with MREMAP_DONTUNMAP could dissociate the VMA 5740657b5146SJann Horn * from its anon_vma. 5741657b5146SJann Horn */ 574229a22b9eSSuren Baghdasaryan if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma)) 5743657b5146SJann Horn goto inval_end_read; 5744444eeb17SSuren Baghdasaryan 574550ee3253SSuren Baghdasaryan /* Check since vm_start/vm_end might change before we lock the VMA */ 5746657b5146SJann Horn if (unlikely(address < vma->vm_start || address >= vma->vm_end)) 5747657b5146SJann Horn goto inval_end_read; 574850ee3253SSuren Baghdasaryan 574950ee3253SSuren Baghdasaryan /* Check if the VMA got isolated after we found it */ 575050ee3253SSuren Baghdasaryan if (vma->detached) { 575150ee3253SSuren Baghdasaryan vma_end_read(vma); 575252f23865SSuren Baghdasaryan count_vm_vma_lock_event(VMA_LOCK_MISS); 575350ee3253SSuren Baghdasaryan /* The area was replaced with another one */ 575450ee3253SSuren Baghdasaryan goto retry; 575550ee3253SSuren Baghdasaryan } 575650ee3253SSuren Baghdasaryan 575750ee3253SSuren Baghdasaryan rcu_read_unlock(); 575850ee3253SSuren Baghdasaryan return vma; 5759657b5146SJann Horn 5760657b5146SJann Horn inval_end_read: 5761657b5146SJann Horn vma_end_read(vma); 576250ee3253SSuren Baghdasaryan inval: 576350ee3253SSuren Baghdasaryan rcu_read_unlock(); 576452f23865SSuren Baghdasaryan count_vm_vma_lock_event(VMA_LOCK_ABORT); 576550ee3253SSuren Baghdasaryan return NULL; 576650ee3253SSuren Baghdasaryan } 576750ee3253SSuren Baghdasaryan #endif /* CONFIG_PER_VMA_LOCK */ 576850ee3253SSuren Baghdasaryan 576990eceff1SKirill A. Shutemov #ifndef __PAGETABLE_P4D_FOLDED 577090eceff1SKirill A. Shutemov /* 577190eceff1SKirill A. Shutemov * Allocate p4d page table. 577290eceff1SKirill A. Shutemov * We've already handled the fast-path in-line. 577390eceff1SKirill A. Shutemov */ 577490eceff1SKirill A. Shutemov int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address) 577590eceff1SKirill A. Shutemov { 577690eceff1SKirill A. Shutemov p4d_t *new = p4d_alloc_one(mm, address); 577790eceff1SKirill A. Shutemov if (!new) 577890eceff1SKirill A. Shutemov return -ENOMEM; 577990eceff1SKirill A. Shutemov 578090eceff1SKirill A. Shutemov spin_lock(&mm->page_table_lock); 5781ed33b5a6SQi Zheng if (pgd_present(*pgd)) { /* Another has populated it */ 578290eceff1SKirill A. Shutemov p4d_free(mm, new); 5783ed33b5a6SQi Zheng } else { 5784ed33b5a6SQi Zheng smp_wmb(); /* See comment in pmd_install() */ 578590eceff1SKirill A. Shutemov pgd_populate(mm, pgd, new); 5786ed33b5a6SQi Zheng } 578790eceff1SKirill A. Shutemov spin_unlock(&mm->page_table_lock); 578890eceff1SKirill A. Shutemov return 0; 578990eceff1SKirill A. Shutemov } 579090eceff1SKirill A. Shutemov #endif /* __PAGETABLE_P4D_FOLDED */ 579190eceff1SKirill A. Shutemov 57921da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED 57931da177e4SLinus Torvalds /* 57941da177e4SLinus Torvalds * Allocate page upper directory. 5795872fec16SHugh Dickins * We've already handled the fast-path in-line. 57961da177e4SLinus Torvalds */ 5797c2febafcSKirill A. Shutemov int __pud_alloc(struct mm_struct *mm, p4d_t *p4d, unsigned long address) 57981da177e4SLinus Torvalds { 5799c74df32cSHugh Dickins pud_t *new = pud_alloc_one(mm, address); 5800c74df32cSHugh Dickins if (!new) 58011bb3630eSHugh Dickins return -ENOMEM; 58021da177e4SLinus Torvalds 5803872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 5804b4e98d9aSKirill A. Shutemov if (!p4d_present(*p4d)) { 5805b4e98d9aSKirill A. Shutemov mm_inc_nr_puds(mm); 5806ed33b5a6SQi Zheng smp_wmb(); /* See comment in pmd_install() */ 5807c2febafcSKirill A. Shutemov p4d_populate(mm, p4d, new); 5808b4e98d9aSKirill A. Shutemov } else /* Another has populated it */ 5809c2febafcSKirill A. Shutemov pud_free(mm, new); 5810872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 58111bb3630eSHugh Dickins return 0; 58121da177e4SLinus Torvalds } 58131da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */ 58141da177e4SLinus Torvalds 58151da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED 58161da177e4SLinus Torvalds /* 58171da177e4SLinus Torvalds * Allocate page middle directory. 5818872fec16SHugh Dickins * We've already handled the fast-path in-line. 58191da177e4SLinus Torvalds */ 58201bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) 58211da177e4SLinus Torvalds { 5822a00cc7d9SMatthew Wilcox spinlock_t *ptl; 5823c74df32cSHugh Dickins pmd_t *new = pmd_alloc_one(mm, address); 5824c74df32cSHugh Dickins if (!new) 58251bb3630eSHugh Dickins return -ENOMEM; 58261da177e4SLinus Torvalds 5827a00cc7d9SMatthew Wilcox ptl = pud_lock(mm, pud); 5828dc6c9a35SKirill A. Shutemov if (!pud_present(*pud)) { 5829dc6c9a35SKirill A. Shutemov mm_inc_nr_pmds(mm); 5830ed33b5a6SQi Zheng smp_wmb(); /* See comment in pmd_install() */ 58311da177e4SLinus Torvalds pud_populate(mm, pud, new); 5832ed33b5a6SQi Zheng } else { /* Another has populated it */ 58335e541973SBenjamin Herrenschmidt pmd_free(mm, new); 5834ed33b5a6SQi Zheng } 5835a00cc7d9SMatthew Wilcox spin_unlock(ptl); 58361bb3630eSHugh Dickins return 0; 58371da177e4SLinus Torvalds } 58381da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */ 58391da177e4SLinus Torvalds 58403b6748e2SJohannes Weiner /** 58419fd6dad1SPaolo Bonzini * follow_pte - look up PTE at a user virtual address 58429fd6dad1SPaolo Bonzini * @mm: the mm_struct of the target address space 58439fd6dad1SPaolo Bonzini * @address: user virtual address 58449fd6dad1SPaolo Bonzini * @ptepp: location to store found PTE 58459fd6dad1SPaolo Bonzini * @ptlp: location to store the lock for the PTE 58469fd6dad1SPaolo Bonzini * 58479fd6dad1SPaolo Bonzini * On a successful return, the pointer to the PTE is stored in @ptepp; 58489fd6dad1SPaolo Bonzini * the corresponding lock is taken and its location is stored in @ptlp. 58499fd6dad1SPaolo Bonzini * The contents of the PTE are only stable until @ptlp is released; 58509fd6dad1SPaolo Bonzini * any further use, if any, must be protected against invalidation 58519fd6dad1SPaolo Bonzini * with MMU notifiers. 58529fd6dad1SPaolo Bonzini * 58539fd6dad1SPaolo Bonzini * Only IO mappings and raw PFN mappings are allowed. The mmap semaphore 58549fd6dad1SPaolo Bonzini * should be taken for read. 58559fd6dad1SPaolo Bonzini * 58569fd6dad1SPaolo Bonzini * KVM uses this function. While it is arguably less bad than ``follow_pfn``, 58579fd6dad1SPaolo Bonzini * it is not a good general-purpose API. 58589fd6dad1SPaolo Bonzini * 58599fd6dad1SPaolo Bonzini * Return: zero on success, -ve otherwise. 58609fd6dad1SPaolo Bonzini */ 58619fd6dad1SPaolo Bonzini int follow_pte(struct mm_struct *mm, unsigned long address, 58629fd6dad1SPaolo Bonzini pte_t **ptepp, spinlock_t **ptlp) 58639fd6dad1SPaolo Bonzini { 58640e5e64c0SMuchun Song pgd_t *pgd; 58650e5e64c0SMuchun Song p4d_t *p4d; 58660e5e64c0SMuchun Song pud_t *pud; 58670e5e64c0SMuchun Song pmd_t *pmd; 58680e5e64c0SMuchun Song pte_t *ptep; 58690e5e64c0SMuchun Song 58700e5e64c0SMuchun Song pgd = pgd_offset(mm, address); 58710e5e64c0SMuchun Song if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) 58720e5e64c0SMuchun Song goto out; 58730e5e64c0SMuchun Song 58740e5e64c0SMuchun Song p4d = p4d_offset(pgd, address); 58750e5e64c0SMuchun Song if (p4d_none(*p4d) || unlikely(p4d_bad(*p4d))) 58760e5e64c0SMuchun Song goto out; 58770e5e64c0SMuchun Song 58780e5e64c0SMuchun Song pud = pud_offset(p4d, address); 58790e5e64c0SMuchun Song if (pud_none(*pud) || unlikely(pud_bad(*pud))) 58800e5e64c0SMuchun Song goto out; 58810e5e64c0SMuchun Song 58820e5e64c0SMuchun Song pmd = pmd_offset(pud, address); 58830e5e64c0SMuchun Song VM_BUG_ON(pmd_trans_huge(*pmd)); 58840e5e64c0SMuchun Song 58850e5e64c0SMuchun Song ptep = pte_offset_map_lock(mm, pmd, address, ptlp); 58863db82b93SHugh Dickins if (!ptep) 58873db82b93SHugh Dickins goto out; 5888c33c7948SRyan Roberts if (!pte_present(ptep_get(ptep))) 58890e5e64c0SMuchun Song goto unlock; 58900e5e64c0SMuchun Song *ptepp = ptep; 58910e5e64c0SMuchun Song return 0; 58920e5e64c0SMuchun Song unlock: 58930e5e64c0SMuchun Song pte_unmap_unlock(ptep, *ptlp); 58940e5e64c0SMuchun Song out: 58950e5e64c0SMuchun Song return -EINVAL; 58969fd6dad1SPaolo Bonzini } 58979fd6dad1SPaolo Bonzini EXPORT_SYMBOL_GPL(follow_pte); 58989fd6dad1SPaolo Bonzini 58999fd6dad1SPaolo Bonzini /** 59003b6748e2SJohannes Weiner * follow_pfn - look up PFN at a user virtual address 59013b6748e2SJohannes Weiner * @vma: memory mapping 59023b6748e2SJohannes Weiner * @address: user virtual address 59033b6748e2SJohannes Weiner * @pfn: location to store found PFN 59043b6748e2SJohannes Weiner * 59053b6748e2SJohannes Weiner * Only IO mappings and raw PFN mappings are allowed. 59063b6748e2SJohannes Weiner * 59079fd6dad1SPaolo Bonzini * This function does not allow the caller to read the permissions 59089fd6dad1SPaolo Bonzini * of the PTE. Do not use it. 59099fd6dad1SPaolo Bonzini * 5910a862f68aSMike Rapoport * Return: zero and the pfn at @pfn on success, -ve otherwise. 59113b6748e2SJohannes Weiner */ 59123b6748e2SJohannes Weiner int follow_pfn(struct vm_area_struct *vma, unsigned long address, 59133b6748e2SJohannes Weiner unsigned long *pfn) 59143b6748e2SJohannes Weiner { 59153b6748e2SJohannes Weiner int ret = -EINVAL; 59163b6748e2SJohannes Weiner spinlock_t *ptl; 59173b6748e2SJohannes Weiner pte_t *ptep; 59183b6748e2SJohannes Weiner 59193b6748e2SJohannes Weiner if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 59203b6748e2SJohannes Weiner return ret; 59213b6748e2SJohannes Weiner 59229fd6dad1SPaolo Bonzini ret = follow_pte(vma->vm_mm, address, &ptep, &ptl); 59233b6748e2SJohannes Weiner if (ret) 59243b6748e2SJohannes Weiner return ret; 5925c33c7948SRyan Roberts *pfn = pte_pfn(ptep_get(ptep)); 59263b6748e2SJohannes Weiner pte_unmap_unlock(ptep, ptl); 59273b6748e2SJohannes Weiner return 0; 59283b6748e2SJohannes Weiner } 59293b6748e2SJohannes Weiner EXPORT_SYMBOL(follow_pfn); 59303b6748e2SJohannes Weiner 593128b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT 5932d87fe660Svenkatesh.pallipadi@intel.com int follow_phys(struct vm_area_struct *vma, 593328b2ee20SRik van Riel unsigned long address, unsigned int flags, 5934d87fe660Svenkatesh.pallipadi@intel.com unsigned long *prot, resource_size_t *phys) 593528b2ee20SRik van Riel { 593603668a4dSJohannes Weiner int ret = -EINVAL; 593728b2ee20SRik van Riel pte_t *ptep, pte; 593828b2ee20SRik van Riel spinlock_t *ptl; 593928b2ee20SRik van Riel 5940d87fe660Svenkatesh.pallipadi@intel.com if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 5941d87fe660Svenkatesh.pallipadi@intel.com goto out; 594228b2ee20SRik van Riel 59439fd6dad1SPaolo Bonzini if (follow_pte(vma->vm_mm, address, &ptep, &ptl)) 5944d87fe660Svenkatesh.pallipadi@intel.com goto out; 5945c33c7948SRyan Roberts pte = ptep_get(ptep); 594603668a4dSJohannes Weiner 5947f6f37321SLinus Torvalds if ((flags & FOLL_WRITE) && !pte_write(pte)) 594828b2ee20SRik van Riel goto unlock; 594928b2ee20SRik van Riel 595028b2ee20SRik van Riel *prot = pgprot_val(pte_pgprot(pte)); 595103668a4dSJohannes Weiner *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT; 595228b2ee20SRik van Riel 595303668a4dSJohannes Weiner ret = 0; 595428b2ee20SRik van Riel unlock: 595528b2ee20SRik van Riel pte_unmap_unlock(ptep, ptl); 595628b2ee20SRik van Riel out: 5957d87fe660Svenkatesh.pallipadi@intel.com return ret; 595828b2ee20SRik van Riel } 595928b2ee20SRik van Riel 596096667f8aSDaniel Vetter /** 596196667f8aSDaniel Vetter * generic_access_phys - generic implementation for iomem mmap access 596296667f8aSDaniel Vetter * @vma: the vma to access 5963f0953a1bSIngo Molnar * @addr: userspace address, not relative offset within @vma 596496667f8aSDaniel Vetter * @buf: buffer to read/write 596596667f8aSDaniel Vetter * @len: length of transfer 596696667f8aSDaniel Vetter * @write: set to FOLL_WRITE when writing, otherwise reading 596796667f8aSDaniel Vetter * 596896667f8aSDaniel Vetter * This is a generic implementation for &vm_operations_struct.access for an 596996667f8aSDaniel Vetter * iomem mapping. This callback is used by access_process_vm() when the @vma is 597096667f8aSDaniel Vetter * not page based. 597196667f8aSDaniel Vetter */ 597228b2ee20SRik van Riel int generic_access_phys(struct vm_area_struct *vma, unsigned long addr, 597328b2ee20SRik van Riel void *buf, int len, int write) 597428b2ee20SRik van Riel { 597528b2ee20SRik van Riel resource_size_t phys_addr; 597628b2ee20SRik van Riel unsigned long prot = 0; 59772bc7273bSKOSAKI Motohiro void __iomem *maddr; 597896667f8aSDaniel Vetter pte_t *ptep, pte; 597996667f8aSDaniel Vetter spinlock_t *ptl; 598096667f8aSDaniel Vetter int offset = offset_in_page(addr); 598196667f8aSDaniel Vetter int ret = -EINVAL; 598228b2ee20SRik van Riel 598396667f8aSDaniel Vetter if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 598496667f8aSDaniel Vetter return -EINVAL; 598596667f8aSDaniel Vetter 598696667f8aSDaniel Vetter retry: 5987e913a8cdSLinus Torvalds if (follow_pte(vma->vm_mm, addr, &ptep, &ptl)) 598896667f8aSDaniel Vetter return -EINVAL; 5989c33c7948SRyan Roberts pte = ptep_get(ptep); 599096667f8aSDaniel Vetter pte_unmap_unlock(ptep, ptl); 599196667f8aSDaniel Vetter 599296667f8aSDaniel Vetter prot = pgprot_val(pte_pgprot(pte)); 599396667f8aSDaniel Vetter phys_addr = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT; 599496667f8aSDaniel Vetter 599596667f8aSDaniel Vetter if ((write & FOLL_WRITE) && !pte_write(pte)) 599628b2ee20SRik van Riel return -EINVAL; 599728b2ee20SRik van Riel 59989cb12d7bSGrazvydas Ignotas maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot); 599924eee1e4Sjie@chenjie6@huwei.com if (!maddr) 600024eee1e4Sjie@chenjie6@huwei.com return -ENOMEM; 600124eee1e4Sjie@chenjie6@huwei.com 6002e913a8cdSLinus Torvalds if (follow_pte(vma->vm_mm, addr, &ptep, &ptl)) 600396667f8aSDaniel Vetter goto out_unmap; 600496667f8aSDaniel Vetter 6005c33c7948SRyan Roberts if (!pte_same(pte, ptep_get(ptep))) { 600696667f8aSDaniel Vetter pte_unmap_unlock(ptep, ptl); 600796667f8aSDaniel Vetter iounmap(maddr); 600896667f8aSDaniel Vetter 600996667f8aSDaniel Vetter goto retry; 601096667f8aSDaniel Vetter } 601196667f8aSDaniel Vetter 601228b2ee20SRik van Riel if (write) 601328b2ee20SRik van Riel memcpy_toio(maddr + offset, buf, len); 601428b2ee20SRik van Riel else 601528b2ee20SRik van Riel memcpy_fromio(buf, maddr + offset, len); 601696667f8aSDaniel Vetter ret = len; 601796667f8aSDaniel Vetter pte_unmap_unlock(ptep, ptl); 601896667f8aSDaniel Vetter out_unmap: 601928b2ee20SRik van Riel iounmap(maddr); 602028b2ee20SRik van Riel 602196667f8aSDaniel Vetter return ret; 602228b2ee20SRik van Riel } 60235a73633eSUwe Kleine-König EXPORT_SYMBOL_GPL(generic_access_phys); 602428b2ee20SRik van Riel #endif 602528b2ee20SRik van Riel 60260ec76a11SDavid Howells /* 6027d3f5ffcaSJohn Hubbard * Access another process' address space as given in mm. 60280ec76a11SDavid Howells */ 6029c43cfa42SLorenzo Stoakes static int __access_remote_vm(struct mm_struct *mm, unsigned long addr, 6030c43cfa42SLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 60310ec76a11SDavid Howells { 60320ec76a11SDavid Howells void *old_buf = buf; 6033442486ecSLorenzo Stoakes int write = gup_flags & FOLL_WRITE; 60340ec76a11SDavid Howells 6035d8ed45c5SMichel Lespinasse if (mmap_read_lock_killable(mm)) 60361e426fe2SKonstantin Khlebnikov return 0; 60371e426fe2SKonstantin Khlebnikov 603822883973SKirill A. Shutemov /* Untag the address before looking up the VMA */ 603922883973SKirill A. Shutemov addr = untagged_addr_remote(mm, addr); 604022883973SKirill A. Shutemov 6041eee9c708SLinus Torvalds /* Avoid triggering the temporary warning in __get_user_pages */ 6042eee9c708SLinus Torvalds if (!vma_lookup(mm, addr) && !expand_stack(mm, addr)) 6043eee9c708SLinus Torvalds return 0; 6044eee9c708SLinus Torvalds 6045183ff22bSSimon Arlott /* ignore errors, just check how much was successfully transferred */ 60460ec76a11SDavid Howells while (len) { 6047ca5e8632SLorenzo Stoakes int bytes, offset; 60480ec76a11SDavid Howells void *maddr; 6049ca5e8632SLorenzo Stoakes struct vm_area_struct *vma = NULL; 6050ca5e8632SLorenzo Stoakes struct page *page = get_user_page_vma_remote(mm, addr, 6051ca5e8632SLorenzo Stoakes gup_flags, &vma); 60520ec76a11SDavid Howells 60536a1960b8SLorenzo Stoakes if (IS_ERR(page)) { 60548d7071afSLinus Torvalds /* We might need to expand the stack to access it */ 60558d7071afSLinus Torvalds vma = vma_lookup(mm, addr); 60568d7071afSLinus Torvalds if (!vma) { 60578d7071afSLinus Torvalds vma = expand_stack(mm, addr); 60589471f1f2SLinus Torvalds 60599471f1f2SLinus Torvalds /* mmap_lock was dropped on failure */ 60608d7071afSLinus Torvalds if (!vma) 60619471f1f2SLinus Torvalds return buf - old_buf; 60629471f1f2SLinus Torvalds 60639471f1f2SLinus Torvalds /* Try again if stack expansion worked */ 60649471f1f2SLinus Torvalds continue; 60658d7071afSLinus Torvalds } 60668d7071afSLinus Torvalds 606728b2ee20SRik van Riel /* 606828b2ee20SRik van Riel * Check if this is a VM_IO | VM_PFNMAP VMA, which 606928b2ee20SRik van Riel * we can access using slightly different code. 607028b2ee20SRik van Riel */ 60719471f1f2SLinus Torvalds bytes = 0; 60729471f1f2SLinus Torvalds #ifdef CONFIG_HAVE_IOREMAP_PROT 607328b2ee20SRik van Riel if (vma->vm_ops && vma->vm_ops->access) 60749471f1f2SLinus Torvalds bytes = vma->vm_ops->access(vma, addr, buf, 607528b2ee20SRik van Riel len, write); 6076dbffcd03SRik van Riel #endif 60779471f1f2SLinus Torvalds if (bytes <= 0) 60789471f1f2SLinus Torvalds break; 607928b2ee20SRik van Riel } else { 60800ec76a11SDavid Howells bytes = len; 60810ec76a11SDavid Howells offset = addr & (PAGE_SIZE-1); 60820ec76a11SDavid Howells if (bytes > PAGE_SIZE-offset) 60830ec76a11SDavid Howells bytes = PAGE_SIZE-offset; 60840ec76a11SDavid Howells 6085f7ef5fe7SFabio M. De Francesco maddr = kmap_local_page(page); 60860ec76a11SDavid Howells if (write) { 60870ec76a11SDavid Howells copy_to_user_page(vma, page, addr, 60880ec76a11SDavid Howells maddr + offset, buf, bytes); 60890ec76a11SDavid Howells set_page_dirty_lock(page); 60900ec76a11SDavid Howells } else { 60910ec76a11SDavid Howells copy_from_user_page(vma, page, addr, 60920ec76a11SDavid Howells buf, maddr + offset, bytes); 60930ec76a11SDavid Howells } 6094f7ef5fe7SFabio M. De Francesco unmap_and_put_page(page, maddr); 609528b2ee20SRik van Riel } 60960ec76a11SDavid Howells len -= bytes; 60970ec76a11SDavid Howells buf += bytes; 60980ec76a11SDavid Howells addr += bytes; 60990ec76a11SDavid Howells } 6100d8ed45c5SMichel Lespinasse mmap_read_unlock(mm); 61010ec76a11SDavid Howells 61020ec76a11SDavid Howells return buf - old_buf; 61030ec76a11SDavid Howells } 610403252919SAndi Kleen 61055ddd36b9SStephen Wilson /** 6106ae91dbfcSRandy Dunlap * access_remote_vm - access another process' address space 61075ddd36b9SStephen Wilson * @mm: the mm_struct of the target address space 61085ddd36b9SStephen Wilson * @addr: start address to access 61095ddd36b9SStephen Wilson * @buf: source or destination buffer 61105ddd36b9SStephen Wilson * @len: number of bytes to transfer 61116347e8d5SLorenzo Stoakes * @gup_flags: flags modifying lookup behaviour 61125ddd36b9SStephen Wilson * 61135ddd36b9SStephen Wilson * The caller must hold a reference on @mm. 6114a862f68aSMike Rapoport * 6115a862f68aSMike Rapoport * Return: number of bytes copied from source to destination. 61165ddd36b9SStephen Wilson */ 61175ddd36b9SStephen Wilson int access_remote_vm(struct mm_struct *mm, unsigned long addr, 61186347e8d5SLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 61195ddd36b9SStephen Wilson { 6120d3f5ffcaSJohn Hubbard return __access_remote_vm(mm, addr, buf, len, gup_flags); 61215ddd36b9SStephen Wilson } 61225ddd36b9SStephen Wilson 612303252919SAndi Kleen /* 6124206cb636SStephen Wilson * Access another process' address space. 6125206cb636SStephen Wilson * Source/target buffer must be kernel space, 6126206cb636SStephen Wilson * Do not walk the page table directly, use get_user_pages 6127206cb636SStephen Wilson */ 6128206cb636SStephen Wilson int access_process_vm(struct task_struct *tsk, unsigned long addr, 6129f307ab6dSLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 6130206cb636SStephen Wilson { 6131206cb636SStephen Wilson struct mm_struct *mm; 6132206cb636SStephen Wilson int ret; 6133206cb636SStephen Wilson 6134206cb636SStephen Wilson mm = get_task_mm(tsk); 6135206cb636SStephen Wilson if (!mm) 6136206cb636SStephen Wilson return 0; 6137206cb636SStephen Wilson 6138d3f5ffcaSJohn Hubbard ret = __access_remote_vm(mm, addr, buf, len, gup_flags); 6139442486ecSLorenzo Stoakes 6140206cb636SStephen Wilson mmput(mm); 6141206cb636SStephen Wilson 6142206cb636SStephen Wilson return ret; 6143206cb636SStephen Wilson } 6144fcd35857SCatalin Marinas EXPORT_SYMBOL_GPL(access_process_vm); 6145206cb636SStephen Wilson 614603252919SAndi Kleen /* 614703252919SAndi Kleen * Print the name of a VMA. 614803252919SAndi Kleen */ 614903252919SAndi Kleen void print_vma_addr(char *prefix, unsigned long ip) 615003252919SAndi Kleen { 615103252919SAndi Kleen struct mm_struct *mm = current->mm; 615203252919SAndi Kleen struct vm_area_struct *vma; 615303252919SAndi Kleen 6154e8bff74aSIngo Molnar /* 61550a7f682dSMichal Hocko * we might be running from an atomic context so we cannot sleep 6156e8bff74aSIngo Molnar */ 6157d8ed45c5SMichel Lespinasse if (!mmap_read_trylock(mm)) 6158e8bff74aSIngo Molnar return; 6159e8bff74aSIngo Molnar 616003252919SAndi Kleen vma = find_vma(mm, ip); 616103252919SAndi Kleen if (vma && vma->vm_file) { 616203252919SAndi Kleen struct file *f = vma->vm_file; 61630a7f682dSMichal Hocko char *buf = (char *)__get_free_page(GFP_NOWAIT); 616403252919SAndi Kleen if (buf) { 61652fbc57c5SAndy Shevchenko char *p; 616603252919SAndi Kleen 61679bf39ab2SMiklos Szeredi p = file_path(f, buf, PAGE_SIZE); 616803252919SAndi Kleen if (IS_ERR(p)) 616903252919SAndi Kleen p = "?"; 61702fbc57c5SAndy Shevchenko printk("%s%s[%lx+%lx]", prefix, kbasename(p), 617103252919SAndi Kleen vma->vm_start, 617203252919SAndi Kleen vma->vm_end - vma->vm_start); 617303252919SAndi Kleen free_page((unsigned long)buf); 617403252919SAndi Kleen } 617503252919SAndi Kleen } 6176d8ed45c5SMichel Lespinasse mmap_read_unlock(mm); 617703252919SAndi Kleen } 61783ee1afa3SNick Piggin 6179662bbcb2SMichael S. Tsirkin #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP) 61809ec23531SDavid Hildenbrand void __might_fault(const char *file, int line) 61813ee1afa3SNick Piggin { 61829ec23531SDavid Hildenbrand if (pagefault_disabled()) 6183662bbcb2SMichael S. Tsirkin return; 618442a38756SThomas Gleixner __might_sleep(file, line); 61859ec23531SDavid Hildenbrand #if defined(CONFIG_DEBUG_ATOMIC_SLEEP) 6186662bbcb2SMichael S. Tsirkin if (current->mm) 6187da1c55f1SMichel Lespinasse might_lock_read(¤t->mm->mmap_lock); 61889ec23531SDavid Hildenbrand #endif 61893ee1afa3SNick Piggin } 61909ec23531SDavid Hildenbrand EXPORT_SYMBOL(__might_fault); 61913ee1afa3SNick Piggin #endif 619247ad8475SAndrea Arcangeli 619347ad8475SAndrea Arcangeli #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS) 6194c6ddfb6cSHuang Ying /* 6195c6ddfb6cSHuang Ying * Process all subpages of the specified huge page with the specified 6196c6ddfb6cSHuang Ying * operation. The target subpage will be processed last to keep its 6197c6ddfb6cSHuang Ying * cache lines hot. 6198c6ddfb6cSHuang Ying */ 61991cb9dc4bSLiu Shixin static inline int process_huge_page( 6200c6ddfb6cSHuang Ying unsigned long addr_hint, unsigned int pages_per_huge_page, 62011cb9dc4bSLiu Shixin int (*process_subpage)(unsigned long addr, int idx, void *arg), 6202c6ddfb6cSHuang Ying void *arg) 6203c6ddfb6cSHuang Ying { 62041cb9dc4bSLiu Shixin int i, n, base, l, ret; 6205c6ddfb6cSHuang Ying unsigned long addr = addr_hint & 6206c6ddfb6cSHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 6207c6ddfb6cSHuang Ying 6208c6ddfb6cSHuang Ying /* Process target subpage last to keep its cache lines hot */ 6209c6ddfb6cSHuang Ying might_sleep(); 6210c6ddfb6cSHuang Ying n = (addr_hint - addr) / PAGE_SIZE; 6211c6ddfb6cSHuang Ying if (2 * n <= pages_per_huge_page) { 6212c6ddfb6cSHuang Ying /* If target subpage in first half of huge page */ 6213c6ddfb6cSHuang Ying base = 0; 6214c6ddfb6cSHuang Ying l = n; 6215c6ddfb6cSHuang Ying /* Process subpages at the end of huge page */ 6216c6ddfb6cSHuang Ying for (i = pages_per_huge_page - 1; i >= 2 * n; i--) { 6217c6ddfb6cSHuang Ying cond_resched(); 62181cb9dc4bSLiu Shixin ret = process_subpage(addr + i * PAGE_SIZE, i, arg); 62191cb9dc4bSLiu Shixin if (ret) 62201cb9dc4bSLiu Shixin return ret; 6221c6ddfb6cSHuang Ying } 6222c6ddfb6cSHuang Ying } else { 6223c6ddfb6cSHuang Ying /* If target subpage in second half of huge page */ 6224c6ddfb6cSHuang Ying base = pages_per_huge_page - 2 * (pages_per_huge_page - n); 6225c6ddfb6cSHuang Ying l = pages_per_huge_page - n; 6226c6ddfb6cSHuang Ying /* Process subpages at the begin of huge page */ 6227c6ddfb6cSHuang Ying for (i = 0; i < base; i++) { 6228c6ddfb6cSHuang Ying cond_resched(); 62291cb9dc4bSLiu Shixin ret = process_subpage(addr + i * PAGE_SIZE, i, arg); 62301cb9dc4bSLiu Shixin if (ret) 62311cb9dc4bSLiu Shixin return ret; 6232c6ddfb6cSHuang Ying } 6233c6ddfb6cSHuang Ying } 6234c6ddfb6cSHuang Ying /* 6235c6ddfb6cSHuang Ying * Process remaining subpages in left-right-left-right pattern 6236c6ddfb6cSHuang Ying * towards the target subpage 6237c6ddfb6cSHuang Ying */ 6238c6ddfb6cSHuang Ying for (i = 0; i < l; i++) { 6239c6ddfb6cSHuang Ying int left_idx = base + i; 6240c6ddfb6cSHuang Ying int right_idx = base + 2 * l - 1 - i; 6241c6ddfb6cSHuang Ying 6242c6ddfb6cSHuang Ying cond_resched(); 62431cb9dc4bSLiu Shixin ret = process_subpage(addr + left_idx * PAGE_SIZE, left_idx, arg); 62441cb9dc4bSLiu Shixin if (ret) 62451cb9dc4bSLiu Shixin return ret; 6246c6ddfb6cSHuang Ying cond_resched(); 62471cb9dc4bSLiu Shixin ret = process_subpage(addr + right_idx * PAGE_SIZE, right_idx, arg); 62481cb9dc4bSLiu Shixin if (ret) 62491cb9dc4bSLiu Shixin return ret; 6250c6ddfb6cSHuang Ying } 62511cb9dc4bSLiu Shixin return 0; 6252c6ddfb6cSHuang Ying } 6253c6ddfb6cSHuang Ying 625447ad8475SAndrea Arcangeli static void clear_gigantic_page(struct page *page, 625547ad8475SAndrea Arcangeli unsigned long addr, 625647ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 625747ad8475SAndrea Arcangeli { 625847ad8475SAndrea Arcangeli int i; 625914455eabSCheng Li struct page *p; 626047ad8475SAndrea Arcangeli 626147ad8475SAndrea Arcangeli might_sleep(); 626214455eabSCheng Li for (i = 0; i < pages_per_huge_page; i++) { 626314455eabSCheng Li p = nth_page(page, i); 626447ad8475SAndrea Arcangeli cond_resched(); 626547ad8475SAndrea Arcangeli clear_user_highpage(p, addr + i * PAGE_SIZE); 626647ad8475SAndrea Arcangeli } 626747ad8475SAndrea Arcangeli } 6268c6ddfb6cSHuang Ying 62691cb9dc4bSLiu Shixin static int clear_subpage(unsigned long addr, int idx, void *arg) 6270c6ddfb6cSHuang Ying { 6271c6ddfb6cSHuang Ying struct page *page = arg; 6272c6ddfb6cSHuang Ying 627321fff064SKefeng Wang clear_user_highpage(nth_page(page, idx), addr); 62741cb9dc4bSLiu Shixin return 0; 6275c6ddfb6cSHuang Ying } 6276c6ddfb6cSHuang Ying 627747ad8475SAndrea Arcangeli void clear_huge_page(struct page *page, 6278c79b57e4SHuang Ying unsigned long addr_hint, unsigned int pages_per_huge_page) 627947ad8475SAndrea Arcangeli { 6280c79b57e4SHuang Ying unsigned long addr = addr_hint & 6281c79b57e4SHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 628247ad8475SAndrea Arcangeli 628347ad8475SAndrea Arcangeli if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { 628447ad8475SAndrea Arcangeli clear_gigantic_page(page, addr, pages_per_huge_page); 628547ad8475SAndrea Arcangeli return; 628647ad8475SAndrea Arcangeli } 628747ad8475SAndrea Arcangeli 6288c6ddfb6cSHuang Ying process_huge_page(addr_hint, pages_per_huge_page, clear_subpage, page); 628947ad8475SAndrea Arcangeli } 629047ad8475SAndrea Arcangeli 62911cb9dc4bSLiu Shixin static int copy_user_gigantic_page(struct folio *dst, struct folio *src, 629247ad8475SAndrea Arcangeli unsigned long addr, 629347ad8475SAndrea Arcangeli struct vm_area_struct *vma, 629447ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 629547ad8475SAndrea Arcangeli { 629647ad8475SAndrea Arcangeli int i; 6297c0e8150eSZhangPeng struct page *dst_page; 6298c0e8150eSZhangPeng struct page *src_page; 629947ad8475SAndrea Arcangeli 630014455eabSCheng Li for (i = 0; i < pages_per_huge_page; i++) { 6301c0e8150eSZhangPeng dst_page = folio_page(dst, i); 6302c0e8150eSZhangPeng src_page = folio_page(src, i); 630314455eabSCheng Li 630447ad8475SAndrea Arcangeli cond_resched(); 63051cb9dc4bSLiu Shixin if (copy_mc_user_highpage(dst_page, src_page, 63061cb9dc4bSLiu Shixin addr + i*PAGE_SIZE, vma)) { 63071cb9dc4bSLiu Shixin memory_failure_queue(page_to_pfn(src_page), 0); 63081cb9dc4bSLiu Shixin return -EHWPOISON; 630947ad8475SAndrea Arcangeli } 631047ad8475SAndrea Arcangeli } 63111cb9dc4bSLiu Shixin return 0; 63121cb9dc4bSLiu Shixin } 631347ad8475SAndrea Arcangeli 6314c9f4cd71SHuang Ying struct copy_subpage_arg { 6315c9f4cd71SHuang Ying struct page *dst; 6316c9f4cd71SHuang Ying struct page *src; 6317c9f4cd71SHuang Ying struct vm_area_struct *vma; 6318c9f4cd71SHuang Ying }; 6319c9f4cd71SHuang Ying 63201cb9dc4bSLiu Shixin static int copy_subpage(unsigned long addr, int idx, void *arg) 6321c9f4cd71SHuang Ying { 6322c9f4cd71SHuang Ying struct copy_subpage_arg *copy_arg = arg; 632321fff064SKefeng Wang struct page *dst = nth_page(copy_arg->dst, idx); 632421fff064SKefeng Wang struct page *src = nth_page(copy_arg->src, idx); 6325c9f4cd71SHuang Ying 632621fff064SKefeng Wang if (copy_mc_user_highpage(dst, src, addr, copy_arg->vma)) { 632721fff064SKefeng Wang memory_failure_queue(page_to_pfn(src), 0); 63281cb9dc4bSLiu Shixin return -EHWPOISON; 63291cb9dc4bSLiu Shixin } 63301cb9dc4bSLiu Shixin return 0; 6331c9f4cd71SHuang Ying } 6332c9f4cd71SHuang Ying 63331cb9dc4bSLiu Shixin int copy_user_large_folio(struct folio *dst, struct folio *src, 6334c0e8150eSZhangPeng unsigned long addr_hint, struct vm_area_struct *vma) 633547ad8475SAndrea Arcangeli { 6336c0e8150eSZhangPeng unsigned int pages_per_huge_page = folio_nr_pages(dst); 6337c9f4cd71SHuang Ying unsigned long addr = addr_hint & 6338c9f4cd71SHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 6339c9f4cd71SHuang Ying struct copy_subpage_arg arg = { 6340c0e8150eSZhangPeng .dst = &dst->page, 6341c0e8150eSZhangPeng .src = &src->page, 6342c9f4cd71SHuang Ying .vma = vma, 6343c9f4cd71SHuang Ying }; 634447ad8475SAndrea Arcangeli 63451cb9dc4bSLiu Shixin if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) 63461cb9dc4bSLiu Shixin return copy_user_gigantic_page(dst, src, addr, vma, 634747ad8475SAndrea Arcangeli pages_per_huge_page); 634847ad8475SAndrea Arcangeli 63491cb9dc4bSLiu Shixin return process_huge_page(addr_hint, pages_per_huge_page, copy_subpage, &arg); 635047ad8475SAndrea Arcangeli } 6351fa4d75c1SMike Kravetz 6352e87340caSZhangPeng long copy_folio_from_user(struct folio *dst_folio, 6353fa4d75c1SMike Kravetz const void __user *usr_src, 6354810a56b9SMike Kravetz bool allow_pagefault) 6355fa4d75c1SMike Kravetz { 6356e87340caSZhangPeng void *kaddr; 6357fa4d75c1SMike Kravetz unsigned long i, rc = 0; 6358e87340caSZhangPeng unsigned int nr_pages = folio_nr_pages(dst_folio); 6359e87340caSZhangPeng unsigned long ret_val = nr_pages * PAGE_SIZE; 636014455eabSCheng Li struct page *subpage; 6361fa4d75c1SMike Kravetz 6362e87340caSZhangPeng for (i = 0; i < nr_pages; i++) { 6363e87340caSZhangPeng subpage = folio_page(dst_folio, i); 6364e87340caSZhangPeng kaddr = kmap_local_page(subpage); 63650d508c1fSZhangPeng if (!allow_pagefault) 63660d508c1fSZhangPeng pagefault_disable(); 6367e87340caSZhangPeng rc = copy_from_user(kaddr, usr_src + i * PAGE_SIZE, PAGE_SIZE); 63680d508c1fSZhangPeng if (!allow_pagefault) 63690d508c1fSZhangPeng pagefault_enable(); 6370e87340caSZhangPeng kunmap_local(kaddr); 6371fa4d75c1SMike Kravetz 6372fa4d75c1SMike Kravetz ret_val -= (PAGE_SIZE - rc); 6373fa4d75c1SMike Kravetz if (rc) 6374fa4d75c1SMike Kravetz break; 6375fa4d75c1SMike Kravetz 6376e763243cSMuchun Song flush_dcache_page(subpage); 6377e763243cSMuchun Song 6378fa4d75c1SMike Kravetz cond_resched(); 6379fa4d75c1SMike Kravetz } 6380fa4d75c1SMike Kravetz return ret_val; 6381fa4d75c1SMike Kravetz } 638247ad8475SAndrea Arcangeli #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */ 638349076ec2SKirill A. Shutemov 638440b64acdSOlof Johansson #if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS 6385b35f1819SKirill A. Shutemov 6386b35f1819SKirill A. Shutemov static struct kmem_cache *page_ptl_cachep; 6387b35f1819SKirill A. Shutemov 6388b35f1819SKirill A. Shutemov void __init ptlock_cache_init(void) 6389b35f1819SKirill A. Shutemov { 6390b35f1819SKirill A. Shutemov page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0, 6391b35f1819SKirill A. Shutemov SLAB_PANIC, NULL); 6392b35f1819SKirill A. Shutemov } 6393b35f1819SKirill A. Shutemov 6394f5ecca06SVishal Moola (Oracle) bool ptlock_alloc(struct ptdesc *ptdesc) 639549076ec2SKirill A. Shutemov { 639649076ec2SKirill A. Shutemov spinlock_t *ptl; 639749076ec2SKirill A. Shutemov 6398b35f1819SKirill A. Shutemov ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL); 639949076ec2SKirill A. Shutemov if (!ptl) 640049076ec2SKirill A. Shutemov return false; 6401f5ecca06SVishal Moola (Oracle) ptdesc->ptl = ptl; 640249076ec2SKirill A. Shutemov return true; 640349076ec2SKirill A. Shutemov } 640449076ec2SKirill A. Shutemov 64056ed1b8a0SVishal Moola (Oracle) void ptlock_free(struct ptdesc *ptdesc) 640649076ec2SKirill A. Shutemov { 64076ed1b8a0SVishal Moola (Oracle) kmem_cache_free(page_ptl_cachep, ptdesc->ptl); 640849076ec2SKirill A. Shutemov } 640949076ec2SKirill A. Shutemov #endif 6410