1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/mm/memory.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 81da177e4SLinus Torvalds /* 91da177e4SLinus Torvalds * demand-loading started 01.12.91 - seems it is high on the list of 101da177e4SLinus Torvalds * things wanted, and it should be easy to implement. - Linus 111da177e4SLinus Torvalds */ 121da177e4SLinus Torvalds 131da177e4SLinus Torvalds /* 141da177e4SLinus Torvalds * Ok, demand-loading was easy, shared pages a little bit tricker. Shared 151da177e4SLinus Torvalds * pages started 02.12.91, seems to work. - Linus. 161da177e4SLinus Torvalds * 171da177e4SLinus Torvalds * Tested sharing by executing about 30 /bin/sh: under the old kernel it 181da177e4SLinus Torvalds * would have taken more than the 6M I have free, but it worked well as 191da177e4SLinus Torvalds * far as I could see. 201da177e4SLinus Torvalds * 211da177e4SLinus Torvalds * Also corrected some "invalidate()"s - I wasn't doing enough of them. 221da177e4SLinus Torvalds */ 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds /* 251da177e4SLinus Torvalds * Real VM (paging to/from disk) started 18.12.91. Much more work and 261da177e4SLinus Torvalds * thought has to go into this. Oh, well.. 271da177e4SLinus Torvalds * 19.12.91 - works, somewhat. Sometimes I get faults, don't know why. 281da177e4SLinus Torvalds * Found it. Everything seems to work now. 291da177e4SLinus Torvalds * 20.12.91 - Ok, making the swap-device changeable like the root. 301da177e4SLinus Torvalds */ 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds /* 331da177e4SLinus Torvalds * 05.04.94 - Multi-page memory management added for v1.1. 341da177e4SLinus Torvalds * Idea by Alex Bligh (alex@cconcepts.co.uk) 351da177e4SLinus Torvalds * 361da177e4SLinus Torvalds * 16.07.99 - Support of BIGMEM added by Gerhard Wichert, Siemens AG 371da177e4SLinus Torvalds * (Gerhard.Wichert@pdb.siemens.de) 381da177e4SLinus Torvalds * 391da177e4SLinus Torvalds * Aug/Sep 2004 Changed to four level page tables (Andi Kleen) 401da177e4SLinus Torvalds */ 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds #include <linux/kernel_stat.h> 431da177e4SLinus Torvalds #include <linux/mm.h> 4436090defSArnd Bergmann #include <linux/mm_inline.h> 456e84f315SIngo Molnar #include <linux/sched/mm.h> 46f7ccbae4SIngo Molnar #include <linux/sched/coredump.h> 476a3827d7SIngo Molnar #include <linux/sched/numa_balancing.h> 4829930025SIngo Molnar #include <linux/sched/task.h> 491da177e4SLinus Torvalds #include <linux/hugetlb.h> 501da177e4SLinus Torvalds #include <linux/mman.h> 511da177e4SLinus Torvalds #include <linux/swap.h> 521da177e4SLinus Torvalds #include <linux/highmem.h> 531da177e4SLinus Torvalds #include <linux/pagemap.h> 545042db43SJérôme Glisse #include <linux/memremap.h> 55b073d7f8SAlexander Potapenko #include <linux/kmsan.h> 569a840895SHugh Dickins #include <linux/ksm.h> 571da177e4SLinus Torvalds #include <linux/rmap.h> 58b95f1b31SPaul Gortmaker #include <linux/export.h> 590ff92245SShailabh Nagar #include <linux/delayacct.h> 601da177e4SLinus Torvalds #include <linux/init.h> 6101c8f1c4SDan Williams #include <linux/pfn_t.h> 62edc79b2aSPeter Zijlstra #include <linux/writeback.h> 638a9f3ccdSBalbir Singh #include <linux/memcontrol.h> 64cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h> 653dc14741SHugh Dickins #include <linux/swapops.h> 663dc14741SHugh Dickins #include <linux/elf.h> 675a0e3ad6STejun Heo #include <linux/gfp.h> 684daae3b4SMel Gorman #include <linux/migrate.h> 692fbc57c5SAndy Shevchenko #include <linux/string.h> 70467b171aSAneesh Kumar K.V #include <linux/memory-tiers.h> 711592eef0SKirill A. Shutemov #include <linux/debugfs.h> 726b251fc9SAndrea Arcangeli #include <linux/userfaultfd_k.h> 73bc2466e4SJan Kara #include <linux/dax.h> 746b31d595SMichal Hocko #include <linux/oom.h> 7598fa15f3SAnshuman Khandual #include <linux/numa.h> 76bce617edSPeter Xu #include <linux/perf_event.h> 77bce617edSPeter Xu #include <linux/ptrace.h> 78e80d3909SJoerg Roedel #include <linux/vmalloc.h> 7933024536SHuang Ying #include <linux/sched/sysctl.h> 801da177e4SLinus Torvalds 81b3d1411bSJoel Fernandes (Google) #include <trace/events/kmem.h> 82b3d1411bSJoel Fernandes (Google) 836952b61dSAlexey Dobriyan #include <asm/io.h> 8433a709b2SDave Hansen #include <asm/mmu_context.h> 851da177e4SLinus Torvalds #include <asm/pgalloc.h> 867c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 871da177e4SLinus Torvalds #include <asm/tlb.h> 881da177e4SLinus Torvalds #include <asm/tlbflush.h> 891da177e4SLinus Torvalds 90e80d3909SJoerg Roedel #include "pgalloc-track.h" 9142b77728SJan Beulich #include "internal.h" 92014bb1deSNeilBrown #include "swap.h" 9342b77728SJan Beulich 94af27d940SArnd Bergmann #if defined(LAST_CPUPID_NOT_IN_PAGE_FLAGS) && !defined(CONFIG_COMPILE_TEST) 9590572890SPeter Zijlstra #warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid. 9675980e97SPeter Zijlstra #endif 9775980e97SPeter Zijlstra 98a9ee6cf5SMike Rapoport #ifndef CONFIG_NUMA 991da177e4SLinus Torvalds unsigned long max_mapnr; 1001da177e4SLinus Torvalds EXPORT_SYMBOL(max_mapnr); 101166f61b9STobin C Harding 102166f61b9STobin C Harding struct page *mem_map; 1031da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map); 1041da177e4SLinus Torvalds #endif 1051da177e4SLinus Torvalds 1065c041f5dSPeter Xu static vm_fault_t do_fault(struct vm_fault *vmf); 1072bad466cSPeter Xu static vm_fault_t do_anonymous_page(struct vm_fault *vmf); 1082bad466cSPeter Xu static bool vmf_pte_changed(struct vm_fault *vmf); 1092bad466cSPeter Xu 1102bad466cSPeter Xu /* 1112bad466cSPeter Xu * Return true if the original pte was a uffd-wp pte marker (so the pte was 1122bad466cSPeter Xu * wr-protected). 1132bad466cSPeter Xu */ 1142bad466cSPeter Xu static bool vmf_orig_pte_uffd_wp(struct vm_fault *vmf) 1152bad466cSPeter Xu { 1162bad466cSPeter Xu if (!(vmf->flags & FAULT_FLAG_ORIG_PTE_VALID)) 1172bad466cSPeter Xu return false; 1182bad466cSPeter Xu 1192bad466cSPeter Xu return pte_marker_uffd_wp(vmf->orig_pte); 1202bad466cSPeter Xu } 1215c041f5dSPeter Xu 1221da177e4SLinus Torvalds /* 1231da177e4SLinus Torvalds * A number of key systems in x86 including ioremap() rely on the assumption 1241da177e4SLinus Torvalds * that high_memory defines the upper bound on direct map memory, then end 1251da177e4SLinus Torvalds * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and 1261da177e4SLinus Torvalds * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL 1271da177e4SLinus Torvalds * and ZONE_HIGHMEM. 1281da177e4SLinus Torvalds */ 1291da177e4SLinus Torvalds void *high_memory; 1301da177e4SLinus Torvalds EXPORT_SYMBOL(high_memory); 1311da177e4SLinus Torvalds 13232a93233SIngo Molnar /* 13332a93233SIngo Molnar * Randomize the address space (stacks, mmaps, brk, etc.). 13432a93233SIngo Molnar * 13532a93233SIngo Molnar * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization, 13632a93233SIngo Molnar * as ancient (libc5 based) binaries can segfault. ) 13732a93233SIngo Molnar */ 13832a93233SIngo Molnar int randomize_va_space __read_mostly = 13932a93233SIngo Molnar #ifdef CONFIG_COMPAT_BRK 14032a93233SIngo Molnar 1; 14132a93233SIngo Molnar #else 14232a93233SIngo Molnar 2; 14332a93233SIngo Molnar #endif 144a62eaf15SAndi Kleen 14546bdb427SWill Deacon #ifndef arch_wants_old_prefaulted_pte 14646bdb427SWill Deacon static inline bool arch_wants_old_prefaulted_pte(void) 14746bdb427SWill Deacon { 14846bdb427SWill Deacon /* 14946bdb427SWill Deacon * Transitioning a PTE from 'old' to 'young' can be expensive on 15046bdb427SWill Deacon * some architectures, even if it's performed in hardware. By 15146bdb427SWill Deacon * default, "false" means prefaulted entries will be 'young'. 15246bdb427SWill Deacon */ 15346bdb427SWill Deacon return false; 15446bdb427SWill Deacon } 15546bdb427SWill Deacon #endif 15646bdb427SWill Deacon 157a62eaf15SAndi Kleen static int __init disable_randmaps(char *s) 158a62eaf15SAndi Kleen { 159a62eaf15SAndi Kleen randomize_va_space = 0; 1609b41046cSOGAWA Hirofumi return 1; 161a62eaf15SAndi Kleen } 162a62eaf15SAndi Kleen __setup("norandmaps", disable_randmaps); 163a62eaf15SAndi Kleen 16462eede62SHugh Dickins unsigned long zero_pfn __read_mostly; 1650b70068eSArd Biesheuvel EXPORT_SYMBOL(zero_pfn); 1660b70068eSArd Biesheuvel 167166f61b9STobin C Harding unsigned long highest_memmap_pfn __read_mostly; 168166f61b9STobin C Harding 169a13ea5b7SHugh Dickins /* 170a13ea5b7SHugh Dickins * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init() 171a13ea5b7SHugh Dickins */ 172a13ea5b7SHugh Dickins static int __init init_zero_pfn(void) 173a13ea5b7SHugh Dickins { 174a13ea5b7SHugh Dickins zero_pfn = page_to_pfn(ZERO_PAGE(0)); 175a13ea5b7SHugh Dickins return 0; 176a13ea5b7SHugh Dickins } 177e720e7d0SIlya Lipnitskiy early_initcall(init_zero_pfn); 178a62eaf15SAndi Kleen 179f1a79412SShakeel Butt void mm_trace_rss_stat(struct mm_struct *mm, int member) 180b3d1411bSJoel Fernandes (Google) { 181f1a79412SShakeel Butt trace_rss_stat(mm, member); 182b3d1411bSJoel Fernandes (Google) } 183d559db08SKAMEZAWA Hiroyuki 1841da177e4SLinus Torvalds /* 1851da177e4SLinus Torvalds * Note: this doesn't free the actual pages themselves. That 1861da177e4SLinus Torvalds * has been handled earlier when unmapping all the memory regions. 1871da177e4SLinus Torvalds */ 1889e1b32caSBenjamin Herrenschmidt static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd, 1899e1b32caSBenjamin Herrenschmidt unsigned long addr) 1901da177e4SLinus Torvalds { 1912f569afdSMartin Schwidefsky pgtable_t token = pmd_pgtable(*pmd); 1921da177e4SLinus Torvalds pmd_clear(pmd); 1939e1b32caSBenjamin Herrenschmidt pte_free_tlb(tlb, token, addr); 194c4812909SKirill A. Shutemov mm_dec_nr_ptes(tlb->mm); 1951da177e4SLinus Torvalds } 1961da177e4SLinus Torvalds 197e0da382cSHugh Dickins static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud, 198e0da382cSHugh Dickins unsigned long addr, unsigned long end, 199e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 2001da177e4SLinus Torvalds { 2011da177e4SLinus Torvalds pmd_t *pmd; 2021da177e4SLinus Torvalds unsigned long next; 203e0da382cSHugh Dickins unsigned long start; 2041da177e4SLinus Torvalds 205e0da382cSHugh Dickins start = addr; 2061da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 2071da177e4SLinus Torvalds do { 2081da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 2091da177e4SLinus Torvalds if (pmd_none_or_clear_bad(pmd)) 2101da177e4SLinus Torvalds continue; 2119e1b32caSBenjamin Herrenschmidt free_pte_range(tlb, pmd, addr); 2121da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 2131da177e4SLinus Torvalds 214e0da382cSHugh Dickins start &= PUD_MASK; 215e0da382cSHugh Dickins if (start < floor) 216e0da382cSHugh Dickins return; 217e0da382cSHugh Dickins if (ceiling) { 218e0da382cSHugh Dickins ceiling &= PUD_MASK; 219e0da382cSHugh Dickins if (!ceiling) 220e0da382cSHugh Dickins return; 2211da177e4SLinus Torvalds } 222e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 223e0da382cSHugh Dickins return; 224e0da382cSHugh Dickins 225e0da382cSHugh Dickins pmd = pmd_offset(pud, start); 226e0da382cSHugh Dickins pud_clear(pud); 2279e1b32caSBenjamin Herrenschmidt pmd_free_tlb(tlb, pmd, start); 228dc6c9a35SKirill A. Shutemov mm_dec_nr_pmds(tlb->mm); 2291da177e4SLinus Torvalds } 2301da177e4SLinus Torvalds 231c2febafcSKirill A. Shutemov static inline void free_pud_range(struct mmu_gather *tlb, p4d_t *p4d, 232e0da382cSHugh Dickins unsigned long addr, unsigned long end, 233e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 2341da177e4SLinus Torvalds { 2351da177e4SLinus Torvalds pud_t *pud; 2361da177e4SLinus Torvalds unsigned long next; 237e0da382cSHugh Dickins unsigned long start; 2381da177e4SLinus Torvalds 239e0da382cSHugh Dickins start = addr; 240c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 2411da177e4SLinus Torvalds do { 2421da177e4SLinus Torvalds next = pud_addr_end(addr, end); 2431da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 2441da177e4SLinus Torvalds continue; 245e0da382cSHugh Dickins free_pmd_range(tlb, pud, addr, next, floor, ceiling); 2461da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 2471da177e4SLinus Torvalds 248c2febafcSKirill A. Shutemov start &= P4D_MASK; 249c2febafcSKirill A. Shutemov if (start < floor) 250c2febafcSKirill A. Shutemov return; 251c2febafcSKirill A. Shutemov if (ceiling) { 252c2febafcSKirill A. Shutemov ceiling &= P4D_MASK; 253c2febafcSKirill A. Shutemov if (!ceiling) 254c2febafcSKirill A. Shutemov return; 255c2febafcSKirill A. Shutemov } 256c2febafcSKirill A. Shutemov if (end - 1 > ceiling - 1) 257c2febafcSKirill A. Shutemov return; 258c2febafcSKirill A. Shutemov 259c2febafcSKirill A. Shutemov pud = pud_offset(p4d, start); 260c2febafcSKirill A. Shutemov p4d_clear(p4d); 261c2febafcSKirill A. Shutemov pud_free_tlb(tlb, pud, start); 262b4e98d9aSKirill A. Shutemov mm_dec_nr_puds(tlb->mm); 263c2febafcSKirill A. Shutemov } 264c2febafcSKirill A. Shutemov 265c2febafcSKirill A. Shutemov static inline void free_p4d_range(struct mmu_gather *tlb, pgd_t *pgd, 266c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 267c2febafcSKirill A. Shutemov unsigned long floor, unsigned long ceiling) 268c2febafcSKirill A. Shutemov { 269c2febafcSKirill A. Shutemov p4d_t *p4d; 270c2febafcSKirill A. Shutemov unsigned long next; 271c2febafcSKirill A. Shutemov unsigned long start; 272c2febafcSKirill A. Shutemov 273c2febafcSKirill A. Shutemov start = addr; 274c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 275c2febafcSKirill A. Shutemov do { 276c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 277c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(p4d)) 278c2febafcSKirill A. Shutemov continue; 279c2febafcSKirill A. Shutemov free_pud_range(tlb, p4d, addr, next, floor, ceiling); 280c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 281c2febafcSKirill A. Shutemov 282e0da382cSHugh Dickins start &= PGDIR_MASK; 283e0da382cSHugh Dickins if (start < floor) 284e0da382cSHugh Dickins return; 285e0da382cSHugh Dickins if (ceiling) { 286e0da382cSHugh Dickins ceiling &= PGDIR_MASK; 287e0da382cSHugh Dickins if (!ceiling) 288e0da382cSHugh Dickins return; 2891da177e4SLinus Torvalds } 290e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 291e0da382cSHugh Dickins return; 292e0da382cSHugh Dickins 293c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, start); 294e0da382cSHugh Dickins pgd_clear(pgd); 295c2febafcSKirill A. Shutemov p4d_free_tlb(tlb, p4d, start); 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds /* 299e0da382cSHugh Dickins * This function frees user-level page tables of a process. 3001da177e4SLinus Torvalds */ 30142b77728SJan Beulich void free_pgd_range(struct mmu_gather *tlb, 302e0da382cSHugh Dickins unsigned long addr, unsigned long end, 303e0da382cSHugh Dickins unsigned long floor, unsigned long ceiling) 3041da177e4SLinus Torvalds { 3051da177e4SLinus Torvalds pgd_t *pgd; 3061da177e4SLinus Torvalds unsigned long next; 3071da177e4SLinus Torvalds 308e0da382cSHugh Dickins /* 309e0da382cSHugh Dickins * The next few lines have given us lots of grief... 310e0da382cSHugh Dickins * 311e0da382cSHugh Dickins * Why are we testing PMD* at this top level? Because often 312e0da382cSHugh Dickins * there will be no work to do at all, and we'd prefer not to 313e0da382cSHugh Dickins * go all the way down to the bottom just to discover that. 314e0da382cSHugh Dickins * 315e0da382cSHugh Dickins * Why all these "- 1"s? Because 0 represents both the bottom 316e0da382cSHugh Dickins * of the address space and the top of it (using -1 for the 317e0da382cSHugh Dickins * top wouldn't help much: the masks would do the wrong thing). 318e0da382cSHugh Dickins * The rule is that addr 0 and floor 0 refer to the bottom of 319e0da382cSHugh Dickins * the address space, but end 0 and ceiling 0 refer to the top 320e0da382cSHugh Dickins * Comparisons need to use "end - 1" and "ceiling - 1" (though 321e0da382cSHugh Dickins * that end 0 case should be mythical). 322e0da382cSHugh Dickins * 323e0da382cSHugh Dickins * Wherever addr is brought up or ceiling brought down, we must 324e0da382cSHugh Dickins * be careful to reject "the opposite 0" before it confuses the 325e0da382cSHugh Dickins * subsequent tests. But what about where end is brought down 326e0da382cSHugh Dickins * by PMD_SIZE below? no, end can't go down to 0 there. 327e0da382cSHugh Dickins * 328e0da382cSHugh Dickins * Whereas we round start (addr) and ceiling down, by different 329e0da382cSHugh Dickins * masks at different levels, in order to test whether a table 330e0da382cSHugh Dickins * now has no other vmas using it, so can be freed, we don't 331e0da382cSHugh Dickins * bother to round floor or end up - the tests don't need that. 332e0da382cSHugh Dickins */ 333e0da382cSHugh Dickins 334e0da382cSHugh Dickins addr &= PMD_MASK; 335e0da382cSHugh Dickins if (addr < floor) { 336e0da382cSHugh Dickins addr += PMD_SIZE; 337e0da382cSHugh Dickins if (!addr) 338e0da382cSHugh Dickins return; 339e0da382cSHugh Dickins } 340e0da382cSHugh Dickins if (ceiling) { 341e0da382cSHugh Dickins ceiling &= PMD_MASK; 342e0da382cSHugh Dickins if (!ceiling) 343e0da382cSHugh Dickins return; 344e0da382cSHugh Dickins } 345e0da382cSHugh Dickins if (end - 1 > ceiling - 1) 346e0da382cSHugh Dickins end -= PMD_SIZE; 347e0da382cSHugh Dickins if (addr > end - 1) 348e0da382cSHugh Dickins return; 34907e32661SAneesh Kumar K.V /* 35007e32661SAneesh Kumar K.V * We add page table cache pages with PAGE_SIZE, 35107e32661SAneesh Kumar K.V * (see pte_free_tlb()), flush the tlb if we need 35207e32661SAneesh Kumar K.V */ 353ed6a7935SPeter Zijlstra tlb_change_page_size(tlb, PAGE_SIZE); 35442b77728SJan Beulich pgd = pgd_offset(tlb->mm, addr); 3551da177e4SLinus Torvalds do { 3561da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 3571da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 3581da177e4SLinus Torvalds continue; 359c2febafcSKirill A. Shutemov free_p4d_range(tlb, pgd, addr, next, floor, ceiling); 3601da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 361e0da382cSHugh Dickins } 362e0da382cSHugh Dickins 363fd892593SLiam R. Howlett void free_pgtables(struct mmu_gather *tlb, struct ma_state *mas, 364763ecb03SLiam R. Howlett struct vm_area_struct *vma, unsigned long floor, 36598e51a22SSuren Baghdasaryan unsigned long ceiling, bool mm_wr_locked) 366e0da382cSHugh Dickins { 367763ecb03SLiam R. Howlett do { 368e0da382cSHugh Dickins unsigned long addr = vma->vm_start; 369763ecb03SLiam R. Howlett struct vm_area_struct *next; 370763ecb03SLiam R. Howlett 371763ecb03SLiam R. Howlett /* 372763ecb03SLiam R. Howlett * Note: USER_PGTABLES_CEILING may be passed as ceiling and may 373763ecb03SLiam R. Howlett * be 0. This will underflow and is okay. 374763ecb03SLiam R. Howlett */ 375fd892593SLiam R. Howlett next = mas_find(mas, ceiling - 1); 376e0da382cSHugh Dickins 3778f4f8c16SHugh Dickins /* 37825d9e2d1Snpiggin@suse.de * Hide vma from rmap and truncate_pagecache before freeing 37925d9e2d1Snpiggin@suse.de * pgtables 3808f4f8c16SHugh Dickins */ 38198e51a22SSuren Baghdasaryan if (mm_wr_locked) 38298e51a22SSuren Baghdasaryan vma_start_write(vma); 3835beb4930SRik van Riel unlink_anon_vmas(vma); 3848f4f8c16SHugh Dickins unlink_file_vma(vma); 3858f4f8c16SHugh Dickins 3869da61aefSDavid Gibson if (is_vm_hugetlb_page(vma)) { 3873bf5ee95SHugh Dickins hugetlb_free_pgd_range(tlb, addr, vma->vm_end, 3883bf5ee95SHugh Dickins floor, next ? next->vm_start : ceiling); 3893bf5ee95SHugh Dickins } else { 3903bf5ee95SHugh Dickins /* 3913bf5ee95SHugh Dickins * Optimization: gather nearby vmas into one call down 3923bf5ee95SHugh Dickins */ 3933bf5ee95SHugh Dickins while (next && next->vm_start <= vma->vm_end + PMD_SIZE 3944866920bSDavid Gibson && !is_vm_hugetlb_page(next)) { 395e0da382cSHugh Dickins vma = next; 396fd892593SLiam R. Howlett next = mas_find(mas, ceiling - 1); 39798e51a22SSuren Baghdasaryan if (mm_wr_locked) 39898e51a22SSuren Baghdasaryan vma_start_write(vma); 3995beb4930SRik van Riel unlink_anon_vmas(vma); 4008f4f8c16SHugh Dickins unlink_file_vma(vma); 401e0da382cSHugh Dickins } 4023bf5ee95SHugh Dickins free_pgd_range(tlb, addr, vma->vm_end, 403e0da382cSHugh Dickins floor, next ? next->vm_start : ceiling); 4043bf5ee95SHugh Dickins } 405e0da382cSHugh Dickins vma = next; 406763ecb03SLiam R. Howlett } while (vma); 4071da177e4SLinus Torvalds } 4081da177e4SLinus Torvalds 40903c4f204SQi Zheng void pmd_install(struct mm_struct *mm, pmd_t *pmd, pgtable_t *pte) 4101da177e4SLinus Torvalds { 41103c4f204SQi Zheng spinlock_t *ptl = pmd_lock(mm, pmd); 4121bb3630eSHugh Dickins 41303c4f204SQi Zheng if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 41403c4f204SQi Zheng mm_inc_nr_ptes(mm); 415362a61adSNick Piggin /* 416362a61adSNick Piggin * Ensure all pte setup (eg. pte page lock and page clearing) are 417362a61adSNick Piggin * visible before the pte is made visible to other CPUs by being 418362a61adSNick Piggin * put into page tables. 419362a61adSNick Piggin * 420362a61adSNick Piggin * The other side of the story is the pointer chasing in the page 421362a61adSNick Piggin * table walking code (when walking the page table without locking; 422362a61adSNick Piggin * ie. most of the time). Fortunately, these data accesses consist 423362a61adSNick Piggin * of a chain of data-dependent loads, meaning most CPUs (alpha 424362a61adSNick Piggin * being the notable exception) will already guarantee loads are 425362a61adSNick Piggin * seen in-order. See the alpha page table accessors for the 426bb7cdd38SWill Deacon * smp_rmb() barriers in page table walking code. 427362a61adSNick Piggin */ 428362a61adSNick Piggin smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */ 429ed33b5a6SQi Zheng pmd_populate(mm, pmd, *pte); 430ed33b5a6SQi Zheng *pte = NULL; 4314b471e88SKirill A. Shutemov } 432c4088ebdSKirill A. Shutemov spin_unlock(ptl); 433ed33b5a6SQi Zheng } 434ed33b5a6SQi Zheng 435ed33b5a6SQi Zheng int __pte_alloc(struct mm_struct *mm, pmd_t *pmd) 436ed33b5a6SQi Zheng { 437ed33b5a6SQi Zheng pgtable_t new = pte_alloc_one(mm); 438ed33b5a6SQi Zheng if (!new) 439ed33b5a6SQi Zheng return -ENOMEM; 4408ac1f832SAndrea Arcangeli 44103c4f204SQi Zheng pmd_install(mm, pmd, &new); 4422f569afdSMartin Schwidefsky if (new) 4432f569afdSMartin Schwidefsky pte_free(mm, new); 4441bb3630eSHugh Dickins return 0; 4451da177e4SLinus Torvalds } 4461da177e4SLinus Torvalds 4474cf58924SJoel Fernandes (Google) int __pte_alloc_kernel(pmd_t *pmd) 4481da177e4SLinus Torvalds { 4494cf58924SJoel Fernandes (Google) pte_t *new = pte_alloc_one_kernel(&init_mm); 4501da177e4SLinus Torvalds if (!new) 4511bb3630eSHugh Dickins return -ENOMEM; 4521da177e4SLinus Torvalds 453872fec16SHugh Dickins spin_lock(&init_mm.page_table_lock); 4548ac1f832SAndrea Arcangeli if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ 455ed33b5a6SQi Zheng smp_wmb(); /* See comment in pmd_install() */ 456872fec16SHugh Dickins pmd_populate_kernel(&init_mm, pmd, new); 4572f569afdSMartin Schwidefsky new = NULL; 4584b471e88SKirill A. Shutemov } 459872fec16SHugh Dickins spin_unlock(&init_mm.page_table_lock); 4602f569afdSMartin Schwidefsky if (new) 4612f569afdSMartin Schwidefsky pte_free_kernel(&init_mm, new); 4621bb3630eSHugh Dickins return 0; 4631da177e4SLinus Torvalds } 4641da177e4SLinus Torvalds 465d559db08SKAMEZAWA Hiroyuki static inline void init_rss_vec(int *rss) 466ae859762SHugh Dickins { 467d559db08SKAMEZAWA Hiroyuki memset(rss, 0, sizeof(int) * NR_MM_COUNTERS); 468d559db08SKAMEZAWA Hiroyuki } 469d559db08SKAMEZAWA Hiroyuki 470d559db08SKAMEZAWA Hiroyuki static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss) 471d559db08SKAMEZAWA Hiroyuki { 472d559db08SKAMEZAWA Hiroyuki int i; 473d559db08SKAMEZAWA Hiroyuki 47434e55232SKAMEZAWA Hiroyuki if (current->mm == mm) 47505af2e10SDavid Rientjes sync_mm_rss(mm); 476d559db08SKAMEZAWA Hiroyuki for (i = 0; i < NR_MM_COUNTERS; i++) 477d559db08SKAMEZAWA Hiroyuki if (rss[i]) 478d559db08SKAMEZAWA Hiroyuki add_mm_counter(mm, i, rss[i]); 479ae859762SHugh Dickins } 480ae859762SHugh Dickins 4811da177e4SLinus Torvalds /* 4826aab341eSLinus Torvalds * This function is called to print an error when a bad pte 4836aab341eSLinus Torvalds * is found. For example, we might have a PFN-mapped pte in 4846aab341eSLinus Torvalds * a region that doesn't allow it. 485b5810039SNick Piggin * 486b5810039SNick Piggin * The calling function must still handle the error. 487b5810039SNick Piggin */ 4883dc14741SHugh Dickins static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr, 4893dc14741SHugh Dickins pte_t pte, struct page *page) 490b5810039SNick Piggin { 4913dc14741SHugh Dickins pgd_t *pgd = pgd_offset(vma->vm_mm, addr); 492c2febafcSKirill A. Shutemov p4d_t *p4d = p4d_offset(pgd, addr); 493c2febafcSKirill A. Shutemov pud_t *pud = pud_offset(p4d, addr); 4943dc14741SHugh Dickins pmd_t *pmd = pmd_offset(pud, addr); 4953dc14741SHugh Dickins struct address_space *mapping; 4963dc14741SHugh Dickins pgoff_t index; 497d936cf9bSHugh Dickins static unsigned long resume; 498d936cf9bSHugh Dickins static unsigned long nr_shown; 499d936cf9bSHugh Dickins static unsigned long nr_unshown; 500d936cf9bSHugh Dickins 501d936cf9bSHugh Dickins /* 502d936cf9bSHugh Dickins * Allow a burst of 60 reports, then keep quiet for that minute; 503d936cf9bSHugh Dickins * or allow a steady drip of one report per second. 504d936cf9bSHugh Dickins */ 505d936cf9bSHugh Dickins if (nr_shown == 60) { 506d936cf9bSHugh Dickins if (time_before(jiffies, resume)) { 507d936cf9bSHugh Dickins nr_unshown++; 508d936cf9bSHugh Dickins return; 509d936cf9bSHugh Dickins } 510d936cf9bSHugh Dickins if (nr_unshown) { 5111170532bSJoe Perches pr_alert("BUG: Bad page map: %lu messages suppressed\n", 512d936cf9bSHugh Dickins nr_unshown); 513d936cf9bSHugh Dickins nr_unshown = 0; 514d936cf9bSHugh Dickins } 515d936cf9bSHugh Dickins nr_shown = 0; 516d936cf9bSHugh Dickins } 517d936cf9bSHugh Dickins if (nr_shown++ == 0) 518d936cf9bSHugh Dickins resume = jiffies + 60 * HZ; 5193dc14741SHugh Dickins 5203dc14741SHugh Dickins mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL; 5213dc14741SHugh Dickins index = linear_page_index(vma, addr); 5223dc14741SHugh Dickins 5231170532bSJoe Perches pr_alert("BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n", 5243dc14741SHugh Dickins current->comm, 5253dc14741SHugh Dickins (long long)pte_val(pte), (long long)pmd_val(*pmd)); 526718a3821SWu Fengguang if (page) 527f0b791a3SDave Hansen dump_page(page, "bad pte"); 5286aa9b8b2SKefeng Wang pr_alert("addr:%px vm_flags:%08lx anon_vma:%px mapping:%px index:%lx\n", 5293dc14741SHugh Dickins (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index); 5307e0a1265SMatthew Wilcox (Oracle) pr_alert("file:%pD fault:%ps mmap:%ps read_folio:%ps\n", 5312682582aSKonstantin Khlebnikov vma->vm_file, 5322682582aSKonstantin Khlebnikov vma->vm_ops ? vma->vm_ops->fault : NULL, 5332682582aSKonstantin Khlebnikov vma->vm_file ? vma->vm_file->f_op->mmap : NULL, 5347e0a1265SMatthew Wilcox (Oracle) mapping ? mapping->a_ops->read_folio : NULL); 535b5810039SNick Piggin dump_stack(); 536373d4d09SRusty Russell add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); 537b5810039SNick Piggin } 538b5810039SNick Piggin 539b5810039SNick Piggin /* 5407e675137SNick Piggin * vm_normal_page -- This function gets the "struct page" associated with a pte. 5416aab341eSLinus Torvalds * 5427e675137SNick Piggin * "Special" mappings do not wish to be associated with a "struct page" (either 5437e675137SNick Piggin * it doesn't exist, or it exists but they don't want to touch it). In this 5447e675137SNick Piggin * case, NULL is returned here. "Normal" mappings do have a struct page. 545b379d790SJared Hulbert * 5467e675137SNick Piggin * There are 2 broad cases. Firstly, an architecture may define a pte_special() 5477e675137SNick Piggin * pte bit, in which case this function is trivial. Secondly, an architecture 5487e675137SNick Piggin * may not have a spare pte bit, which requires a more complicated scheme, 5497e675137SNick Piggin * described below. 5507e675137SNick Piggin * 5517e675137SNick Piggin * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a 5527e675137SNick Piggin * special mapping (even if there are underlying and valid "struct pages"). 5537e675137SNick Piggin * COWed pages of a VM_PFNMAP are always normal. 5546aab341eSLinus Torvalds * 555b379d790SJared Hulbert * The way we recognize COWed pages within VM_PFNMAP mappings is through the 556b379d790SJared Hulbert * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit 5577e675137SNick Piggin * set, and the vm_pgoff will point to the first PFN mapped: thus every special 5587e675137SNick Piggin * mapping will always honor the rule 5596aab341eSLinus Torvalds * 5606aab341eSLinus Torvalds * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT) 5616aab341eSLinus Torvalds * 5627e675137SNick Piggin * And for normal mappings this is false. 563b379d790SJared Hulbert * 5647e675137SNick Piggin * This restricts such mappings to be a linear translation from virtual address 5657e675137SNick Piggin * to pfn. To get around this restriction, we allow arbitrary mappings so long 5667e675137SNick Piggin * as the vma is not a COW mapping; in that case, we know that all ptes are 5677e675137SNick Piggin * special (because none can have been COWed). 568b379d790SJared Hulbert * 569b379d790SJared Hulbert * 5707e675137SNick Piggin * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP. 5717e675137SNick Piggin * 572b379d790SJared Hulbert * VM_MIXEDMAP mappings can likewise contain memory with or without "struct 573b379d790SJared Hulbert * page" backing, however the difference is that _all_ pages with a struct 574b379d790SJared Hulbert * page (that is, those where pfn_valid is true) are refcounted and considered 575b379d790SJared Hulbert * normal pages by the VM. The disadvantage is that pages are refcounted 576b379d790SJared Hulbert * (which can be slower and simply not an option for some PFNMAP users). The 577b379d790SJared Hulbert * advantage is that we don't have to follow the strict linearity rule of 578b379d790SJared Hulbert * PFNMAP mappings in order to support COWable mappings. 579b379d790SJared Hulbert * 580ee498ed7SHugh Dickins */ 58125b2995aSChristoph Hellwig struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, 58225b2995aSChristoph Hellwig pte_t pte) 583ee498ed7SHugh Dickins { 58422b31eecSHugh Dickins unsigned long pfn = pte_pfn(pte); 5857e675137SNick Piggin 58600b3a331SLaurent Dufour if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL)) { 587b38af472SHugh Dickins if (likely(!pte_special(pte))) 58822b31eecSHugh Dickins goto check_pfn; 589667a0a06SDavid Vrabel if (vma->vm_ops && vma->vm_ops->find_special_page) 590667a0a06SDavid Vrabel return vma->vm_ops->find_special_page(vma, addr); 591a13ea5b7SHugh Dickins if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)) 592a13ea5b7SHugh Dickins return NULL; 593df6ad698SJérôme Glisse if (is_zero_pfn(pfn)) 594df6ad698SJérôme Glisse return NULL; 595e1fb4a08SDave Jiang if (pte_devmap(pte)) 5963218f871SAlex Sierra /* 5973218f871SAlex Sierra * NOTE: New users of ZONE_DEVICE will not set pte_devmap() 5983218f871SAlex Sierra * and will have refcounts incremented on their struct pages 5993218f871SAlex Sierra * when they are inserted into PTEs, thus they are safe to 6003218f871SAlex Sierra * return here. Legacy ZONE_DEVICE pages that set pte_devmap() 6013218f871SAlex Sierra * do not have refcounts. Example of legacy ZONE_DEVICE is 6023218f871SAlex Sierra * MEMORY_DEVICE_FS_DAX type in pmem or virtio_fs drivers. 6033218f871SAlex Sierra */ 604e1fb4a08SDave Jiang return NULL; 605e1fb4a08SDave Jiang 60622b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 6077e675137SNick Piggin return NULL; 6087e675137SNick Piggin } 6097e675137SNick Piggin 61000b3a331SLaurent Dufour /* !CONFIG_ARCH_HAS_PTE_SPECIAL case follows: */ 6117e675137SNick Piggin 612b379d790SJared Hulbert if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { 613b379d790SJared Hulbert if (vma->vm_flags & VM_MIXEDMAP) { 614b379d790SJared Hulbert if (!pfn_valid(pfn)) 615b379d790SJared Hulbert return NULL; 616b379d790SJared Hulbert goto out; 617b379d790SJared Hulbert } else { 6187e675137SNick Piggin unsigned long off; 6197e675137SNick Piggin off = (addr - vma->vm_start) >> PAGE_SHIFT; 6206aab341eSLinus Torvalds if (pfn == vma->vm_pgoff + off) 6216aab341eSLinus Torvalds return NULL; 62267121172SLinus Torvalds if (!is_cow_mapping(vma->vm_flags)) 623fb155c16SLinus Torvalds return NULL; 6246aab341eSLinus Torvalds } 625b379d790SJared Hulbert } 6266aab341eSLinus Torvalds 627b38af472SHugh Dickins if (is_zero_pfn(pfn)) 628b38af472SHugh Dickins return NULL; 62900b3a331SLaurent Dufour 63022b31eecSHugh Dickins check_pfn: 63122b31eecSHugh Dickins if (unlikely(pfn > highest_memmap_pfn)) { 63222b31eecSHugh Dickins print_bad_pte(vma, addr, pte, NULL); 63322b31eecSHugh Dickins return NULL; 63422b31eecSHugh Dickins } 6356aab341eSLinus Torvalds 6366aab341eSLinus Torvalds /* 6377e675137SNick Piggin * NOTE! We still have PageReserved() pages in the page tables. 6387e675137SNick Piggin * eg. VDSO mappings can cause them to exist. 6396aab341eSLinus Torvalds */ 640b379d790SJared Hulbert out: 6416aab341eSLinus Torvalds return pfn_to_page(pfn); 642ee498ed7SHugh Dickins } 643ee498ed7SHugh Dickins 644318e9342SVishal Moola (Oracle) struct folio *vm_normal_folio(struct vm_area_struct *vma, unsigned long addr, 645318e9342SVishal Moola (Oracle) pte_t pte) 646318e9342SVishal Moola (Oracle) { 647318e9342SVishal Moola (Oracle) struct page *page = vm_normal_page(vma, addr, pte); 648318e9342SVishal Moola (Oracle) 649318e9342SVishal Moola (Oracle) if (page) 650318e9342SVishal Moola (Oracle) return page_folio(page); 651318e9342SVishal Moola (Oracle) return NULL; 652318e9342SVishal Moola (Oracle) } 653318e9342SVishal Moola (Oracle) 65428093f9fSGerald Schaefer #ifdef CONFIG_TRANSPARENT_HUGEPAGE 65528093f9fSGerald Schaefer struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr, 65628093f9fSGerald Schaefer pmd_t pmd) 65728093f9fSGerald Schaefer { 65828093f9fSGerald Schaefer unsigned long pfn = pmd_pfn(pmd); 65928093f9fSGerald Schaefer 66028093f9fSGerald Schaefer /* 66128093f9fSGerald Schaefer * There is no pmd_special() but there may be special pmds, e.g. 66228093f9fSGerald Schaefer * in a direct-access (dax) mapping, so let's just replicate the 66300b3a331SLaurent Dufour * !CONFIG_ARCH_HAS_PTE_SPECIAL case from vm_normal_page() here. 66428093f9fSGerald Schaefer */ 66528093f9fSGerald Schaefer if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { 66628093f9fSGerald Schaefer if (vma->vm_flags & VM_MIXEDMAP) { 66728093f9fSGerald Schaefer if (!pfn_valid(pfn)) 66828093f9fSGerald Schaefer return NULL; 66928093f9fSGerald Schaefer goto out; 67028093f9fSGerald Schaefer } else { 67128093f9fSGerald Schaefer unsigned long off; 67228093f9fSGerald Schaefer off = (addr - vma->vm_start) >> PAGE_SHIFT; 67328093f9fSGerald Schaefer if (pfn == vma->vm_pgoff + off) 67428093f9fSGerald Schaefer return NULL; 67528093f9fSGerald Schaefer if (!is_cow_mapping(vma->vm_flags)) 67628093f9fSGerald Schaefer return NULL; 67728093f9fSGerald Schaefer } 67828093f9fSGerald Schaefer } 67928093f9fSGerald Schaefer 680e1fb4a08SDave Jiang if (pmd_devmap(pmd)) 681e1fb4a08SDave Jiang return NULL; 6823cde287bSYu Zhao if (is_huge_zero_pmd(pmd)) 68328093f9fSGerald Schaefer return NULL; 68428093f9fSGerald Schaefer if (unlikely(pfn > highest_memmap_pfn)) 68528093f9fSGerald Schaefer return NULL; 68628093f9fSGerald Schaefer 68728093f9fSGerald Schaefer /* 68828093f9fSGerald Schaefer * NOTE! We still have PageReserved() pages in the page tables. 68928093f9fSGerald Schaefer * eg. VDSO mappings can cause them to exist. 69028093f9fSGerald Schaefer */ 69128093f9fSGerald Schaefer out: 69228093f9fSGerald Schaefer return pfn_to_page(pfn); 69328093f9fSGerald Schaefer } 69428093f9fSGerald Schaefer #endif 69528093f9fSGerald Schaefer 696b756a3b5SAlistair Popple static void restore_exclusive_pte(struct vm_area_struct *vma, 697b756a3b5SAlistair Popple struct page *page, unsigned long address, 698b756a3b5SAlistair Popple pte_t *ptep) 699b756a3b5SAlistair Popple { 700c33c7948SRyan Roberts pte_t orig_pte; 701b756a3b5SAlistair Popple pte_t pte; 702b756a3b5SAlistair Popple swp_entry_t entry; 703b756a3b5SAlistair Popple 704c33c7948SRyan Roberts orig_pte = ptep_get(ptep); 705b756a3b5SAlistair Popple pte = pte_mkold(mk_pte(page, READ_ONCE(vma->vm_page_prot))); 706c33c7948SRyan Roberts if (pte_swp_soft_dirty(orig_pte)) 707b756a3b5SAlistair Popple pte = pte_mksoft_dirty(pte); 708b756a3b5SAlistair Popple 709c33c7948SRyan Roberts entry = pte_to_swp_entry(orig_pte); 710c33c7948SRyan Roberts if (pte_swp_uffd_wp(orig_pte)) 711b756a3b5SAlistair Popple pte = pte_mkuffd_wp(pte); 712b756a3b5SAlistair Popple else if (is_writable_device_exclusive_entry(entry)) 713b756a3b5SAlistair Popple pte = maybe_mkwrite(pte_mkdirty(pte), vma); 714b756a3b5SAlistair Popple 7156c287605SDavid Hildenbrand VM_BUG_ON(pte_write(pte) && !(PageAnon(page) && PageAnonExclusive(page))); 7166c287605SDavid Hildenbrand 717b756a3b5SAlistair Popple /* 718b756a3b5SAlistair Popple * No need to take a page reference as one was already 719b756a3b5SAlistair Popple * created when the swap entry was made. 720b756a3b5SAlistair Popple */ 721b756a3b5SAlistair Popple if (PageAnon(page)) 722f1e2db12SDavid Hildenbrand page_add_anon_rmap(page, vma, address, RMAP_NONE); 723b756a3b5SAlistair Popple else 724b756a3b5SAlistair Popple /* 725b756a3b5SAlistair Popple * Currently device exclusive access only supports anonymous 726b756a3b5SAlistair Popple * memory so the entry shouldn't point to a filebacked page. 727b756a3b5SAlistair Popple */ 7284d8ff640SMiaohe Lin WARN_ON_ONCE(1); 729b756a3b5SAlistair Popple 7301eba86c0SPasha Tatashin set_pte_at(vma->vm_mm, address, ptep, pte); 7311eba86c0SPasha Tatashin 732b756a3b5SAlistair Popple /* 733b756a3b5SAlistair Popple * No need to invalidate - it was non-present before. However 734b756a3b5SAlistair Popple * secondary CPUs may have mappings that need invalidating. 735b756a3b5SAlistair Popple */ 736b756a3b5SAlistair Popple update_mmu_cache(vma, address, ptep); 737b756a3b5SAlistair Popple } 738b756a3b5SAlistair Popple 739b756a3b5SAlistair Popple /* 740b756a3b5SAlistair Popple * Tries to restore an exclusive pte if the page lock can be acquired without 741b756a3b5SAlistair Popple * sleeping. 742b756a3b5SAlistair Popple */ 743b756a3b5SAlistair Popple static int 744b756a3b5SAlistair Popple try_restore_exclusive_pte(pte_t *src_pte, struct vm_area_struct *vma, 745b756a3b5SAlistair Popple unsigned long addr) 746b756a3b5SAlistair Popple { 747c33c7948SRyan Roberts swp_entry_t entry = pte_to_swp_entry(ptep_get(src_pte)); 748b756a3b5SAlistair Popple struct page *page = pfn_swap_entry_to_page(entry); 749b756a3b5SAlistair Popple 750b756a3b5SAlistair Popple if (trylock_page(page)) { 751b756a3b5SAlistair Popple restore_exclusive_pte(vma, page, addr, src_pte); 752b756a3b5SAlistair Popple unlock_page(page); 753b756a3b5SAlistair Popple return 0; 754b756a3b5SAlistair Popple } 755b756a3b5SAlistair Popple 756b756a3b5SAlistair Popple return -EBUSY; 757b756a3b5SAlistair Popple } 758b756a3b5SAlistair Popple 759ee498ed7SHugh Dickins /* 7601da177e4SLinus Torvalds * copy one vm_area from one task to the other. Assumes the page tables 7611da177e4SLinus Torvalds * already present in the new task to be cleared in the whole range 7621da177e4SLinus Torvalds * covered by this vma. 7631da177e4SLinus Torvalds */ 7641da177e4SLinus Torvalds 765df3a57d1SLinus Torvalds static unsigned long 766df3a57d1SLinus Torvalds copy_nonpresent_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, 7678f34f1eaSPeter Xu pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *dst_vma, 7688f34f1eaSPeter Xu struct vm_area_struct *src_vma, unsigned long addr, int *rss) 7691da177e4SLinus Torvalds { 7708f34f1eaSPeter Xu unsigned long vm_flags = dst_vma->vm_flags; 771c33c7948SRyan Roberts pte_t orig_pte = ptep_get(src_pte); 772c33c7948SRyan Roberts pte_t pte = orig_pte; 7731da177e4SLinus Torvalds struct page *page; 774c33c7948SRyan Roberts swp_entry_t entry = pte_to_swp_entry(orig_pte); 7750697212aSChristoph Lameter 7762022b4d1SHugh Dickins if (likely(!non_swap_entry(entry))) { 777570a335bSHugh Dickins if (swap_duplicate(entry) < 0) 7789a5cc85cSAlistair Popple return -EIO; 779570a335bSHugh Dickins 7801da177e4SLinus Torvalds /* make sure dst_mm is on swapoff's mmlist. */ 7811da177e4SLinus Torvalds if (unlikely(list_empty(&dst_mm->mmlist))) { 7821da177e4SLinus Torvalds spin_lock(&mmlist_lock); 783f412ac08SHugh Dickins if (list_empty(&dst_mm->mmlist)) 784f412ac08SHugh Dickins list_add(&dst_mm->mmlist, 785f412ac08SHugh Dickins &src_mm->mmlist); 7861da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 7871da177e4SLinus Torvalds } 7881493a191SDavid Hildenbrand /* Mark the swap entry as shared. */ 789c33c7948SRyan Roberts if (pte_swp_exclusive(orig_pte)) { 790c33c7948SRyan Roberts pte = pte_swp_clear_exclusive(orig_pte); 7911493a191SDavid Hildenbrand set_pte_at(src_mm, addr, src_pte, pte); 7921493a191SDavid Hildenbrand } 793b084d435SKAMEZAWA Hiroyuki rss[MM_SWAPENTS]++; 7942022b4d1SHugh Dickins } else if (is_migration_entry(entry)) { 795af5cdaf8SAlistair Popple page = pfn_swap_entry_to_page(entry); 7969f9f1acdSKonstantin Khlebnikov 797eca56ff9SJerome Marchand rss[mm_counter(page)]++; 7989f9f1acdSKonstantin Khlebnikov 7996c287605SDavid Hildenbrand if (!is_readable_migration_entry(entry) && 8000697212aSChristoph Lameter is_cow_mapping(vm_flags)) { 8010697212aSChristoph Lameter /* 8026c287605SDavid Hildenbrand * COW mappings require pages in both parent and child 8036c287605SDavid Hildenbrand * to be set to read. A previously exclusive entry is 8046c287605SDavid Hildenbrand * now shared. 8050697212aSChristoph Lameter */ 8064dd845b5SAlistair Popple entry = make_readable_migration_entry( 8074dd845b5SAlistair Popple swp_offset(entry)); 8080697212aSChristoph Lameter pte = swp_entry_to_pte(entry); 809c33c7948SRyan Roberts if (pte_swp_soft_dirty(orig_pte)) 810c3d16e16SCyrill Gorcunov pte = pte_swp_mksoft_dirty(pte); 811c33c7948SRyan Roberts if (pte_swp_uffd_wp(orig_pte)) 812f45ec5ffSPeter Xu pte = pte_swp_mkuffd_wp(pte); 8130697212aSChristoph Lameter set_pte_at(src_mm, addr, src_pte, pte); 8140697212aSChristoph Lameter } 8155042db43SJérôme Glisse } else if (is_device_private_entry(entry)) { 816af5cdaf8SAlistair Popple page = pfn_swap_entry_to_page(entry); 8175042db43SJérôme Glisse 8185042db43SJérôme Glisse /* 8195042db43SJérôme Glisse * Update rss count even for unaddressable pages, as 8205042db43SJérôme Glisse * they should treated just like normal pages in this 8215042db43SJérôme Glisse * respect. 8225042db43SJérôme Glisse * 8235042db43SJérôme Glisse * We will likely want to have some new rss counters 8245042db43SJérôme Glisse * for unaddressable pages, at some point. But for now 8255042db43SJérôme Glisse * keep things as they are. 8265042db43SJérôme Glisse */ 8275042db43SJérôme Glisse get_page(page); 8285042db43SJérôme Glisse rss[mm_counter(page)]++; 829fb3d824dSDavid Hildenbrand /* Cannot fail as these pages cannot get pinned. */ 830fb3d824dSDavid Hildenbrand BUG_ON(page_try_dup_anon_rmap(page, false, src_vma)); 8315042db43SJérôme Glisse 8325042db43SJérôme Glisse /* 8335042db43SJérôme Glisse * We do not preserve soft-dirty information, because so 8345042db43SJérôme Glisse * far, checkpoint/restore is the only feature that 8355042db43SJérôme Glisse * requires that. And checkpoint/restore does not work 8365042db43SJérôme Glisse * when a device driver is involved (you cannot easily 8375042db43SJérôme Glisse * save and restore device driver state). 8385042db43SJérôme Glisse */ 8394dd845b5SAlistair Popple if (is_writable_device_private_entry(entry) && 8405042db43SJérôme Glisse is_cow_mapping(vm_flags)) { 8414dd845b5SAlistair Popple entry = make_readable_device_private_entry( 8424dd845b5SAlistair Popple swp_offset(entry)); 8435042db43SJérôme Glisse pte = swp_entry_to_pte(entry); 844c33c7948SRyan Roberts if (pte_swp_uffd_wp(orig_pte)) 845f45ec5ffSPeter Xu pte = pte_swp_mkuffd_wp(pte); 8465042db43SJérôme Glisse set_pte_at(src_mm, addr, src_pte, pte); 8475042db43SJérôme Glisse } 848b756a3b5SAlistair Popple } else if (is_device_exclusive_entry(entry)) { 849b756a3b5SAlistair Popple /* 850b756a3b5SAlistair Popple * Make device exclusive entries present by restoring the 851b756a3b5SAlistair Popple * original entry then copying as for a present pte. Device 852b756a3b5SAlistair Popple * exclusive entries currently only support private writable 853b756a3b5SAlistair Popple * (ie. COW) mappings. 854b756a3b5SAlistair Popple */ 855b756a3b5SAlistair Popple VM_BUG_ON(!is_cow_mapping(src_vma->vm_flags)); 856b756a3b5SAlistair Popple if (try_restore_exclusive_pte(src_pte, src_vma, addr)) 857b756a3b5SAlistair Popple return -EBUSY; 858b756a3b5SAlistair Popple return -ENOENT; 859c56d1b62SPeter Xu } else if (is_pte_marker_entry(entry)) { 860af19487fSAxel Rasmussen pte_marker marker = copy_pte_marker(entry, dst_vma); 861af19487fSAxel Rasmussen 862af19487fSAxel Rasmussen if (marker) 863af19487fSAxel Rasmussen set_pte_at(dst_mm, addr, dst_pte, 864af19487fSAxel Rasmussen make_pte_marker(marker)); 865c56d1b62SPeter Xu return 0; 8661da177e4SLinus Torvalds } 8678f34f1eaSPeter Xu if (!userfaultfd_wp(dst_vma)) 8688f34f1eaSPeter Xu pte = pte_swp_clear_uffd_wp(pte); 869df3a57d1SLinus Torvalds set_pte_at(dst_mm, addr, dst_pte, pte); 870df3a57d1SLinus Torvalds return 0; 8711da177e4SLinus Torvalds } 8721da177e4SLinus Torvalds 87370e806e4SPeter Xu /* 874b51ad4f8SDavid Hildenbrand * Copy a present and normal page. 87570e806e4SPeter Xu * 876b51ad4f8SDavid Hildenbrand * NOTE! The usual case is that this isn't required; 877b51ad4f8SDavid Hildenbrand * instead, the caller can just increase the page refcount 878b51ad4f8SDavid Hildenbrand * and re-use the pte the traditional way. 87970e806e4SPeter Xu * 88070e806e4SPeter Xu * And if we need a pre-allocated page but don't yet have 88170e806e4SPeter Xu * one, return a negative error to let the preallocation 88270e806e4SPeter Xu * code know so that it can do so outside the page table 88370e806e4SPeter Xu * lock. 88470e806e4SPeter Xu */ 88570e806e4SPeter Xu static inline int 886c78f4636SPeter Xu copy_present_page(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, 887c78f4636SPeter Xu pte_t *dst_pte, pte_t *src_pte, unsigned long addr, int *rss, 888edf50470SMatthew Wilcox (Oracle) struct folio **prealloc, struct page *page) 88970e806e4SPeter Xu { 890edf50470SMatthew Wilcox (Oracle) struct folio *new_folio; 891b51ad4f8SDavid Hildenbrand pte_t pte; 89270e806e4SPeter Xu 893edf50470SMatthew Wilcox (Oracle) new_folio = *prealloc; 894edf50470SMatthew Wilcox (Oracle) if (!new_folio) 89570e806e4SPeter Xu return -EAGAIN; 89670e806e4SPeter Xu 89770e806e4SPeter Xu /* 89870e806e4SPeter Xu * We have a prealloc page, all good! Take it 89970e806e4SPeter Xu * over and copy the page & arm it. 90070e806e4SPeter Xu */ 90170e806e4SPeter Xu *prealloc = NULL; 902edf50470SMatthew Wilcox (Oracle) copy_user_highpage(&new_folio->page, page, addr, src_vma); 903edf50470SMatthew Wilcox (Oracle) __folio_mark_uptodate(new_folio); 904edf50470SMatthew Wilcox (Oracle) folio_add_new_anon_rmap(new_folio, dst_vma, addr); 905edf50470SMatthew Wilcox (Oracle) folio_add_lru_vma(new_folio, dst_vma); 906edf50470SMatthew Wilcox (Oracle) rss[MM_ANONPAGES]++; 90770e806e4SPeter Xu 90870e806e4SPeter Xu /* All done, just insert the new page copy in the child */ 909edf50470SMatthew Wilcox (Oracle) pte = mk_pte(&new_folio->page, dst_vma->vm_page_prot); 910c78f4636SPeter Xu pte = maybe_mkwrite(pte_mkdirty(pte), dst_vma); 911c33c7948SRyan Roberts if (userfaultfd_pte_wp(dst_vma, ptep_get(src_pte))) 9128f34f1eaSPeter Xu /* Uffd-wp needs to be delivered to dest pte as well */ 913f1eb1bacSPeter Xu pte = pte_mkuffd_wp(pte); 914c78f4636SPeter Xu set_pte_at(dst_vma->vm_mm, addr, dst_pte, pte); 91570e806e4SPeter Xu return 0; 91670e806e4SPeter Xu } 91770e806e4SPeter Xu 91870e806e4SPeter Xu /* 91970e806e4SPeter Xu * Copy one pte. Returns 0 if succeeded, or -EAGAIN if one preallocated page 92070e806e4SPeter Xu * is required to copy this pte. 92170e806e4SPeter Xu */ 92270e806e4SPeter Xu static inline int 923c78f4636SPeter Xu copy_present_pte(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, 924c78f4636SPeter Xu pte_t *dst_pte, pte_t *src_pte, unsigned long addr, int *rss, 925edf50470SMatthew Wilcox (Oracle) struct folio **prealloc) 926df3a57d1SLinus Torvalds { 927c78f4636SPeter Xu struct mm_struct *src_mm = src_vma->vm_mm; 928c78f4636SPeter Xu unsigned long vm_flags = src_vma->vm_flags; 929c33c7948SRyan Roberts pte_t pte = ptep_get(src_pte); 930df3a57d1SLinus Torvalds struct page *page; 93114ddee41SMatthew Wilcox (Oracle) struct folio *folio; 932df3a57d1SLinus Torvalds 933c78f4636SPeter Xu page = vm_normal_page(src_vma, addr, pte); 93414ddee41SMatthew Wilcox (Oracle) if (page) 93514ddee41SMatthew Wilcox (Oracle) folio = page_folio(page); 93614ddee41SMatthew Wilcox (Oracle) if (page && folio_test_anon(folio)) { 937b51ad4f8SDavid Hildenbrand /* 938b51ad4f8SDavid Hildenbrand * If this page may have been pinned by the parent process, 939b51ad4f8SDavid Hildenbrand * copy the page immediately for the child so that we'll always 940b51ad4f8SDavid Hildenbrand * guarantee the pinned page won't be randomly replaced in the 941b51ad4f8SDavid Hildenbrand * future. 942b51ad4f8SDavid Hildenbrand */ 94314ddee41SMatthew Wilcox (Oracle) folio_get(folio); 944fb3d824dSDavid Hildenbrand if (unlikely(page_try_dup_anon_rmap(page, false, src_vma))) { 945fb3d824dSDavid Hildenbrand /* Page may be pinned, we have to copy. */ 94614ddee41SMatthew Wilcox (Oracle) folio_put(folio); 947b51ad4f8SDavid Hildenbrand return copy_present_page(dst_vma, src_vma, dst_pte, src_pte, 948b51ad4f8SDavid Hildenbrand addr, rss, prealloc, page); 949fb3d824dSDavid Hildenbrand } 950edf50470SMatthew Wilcox (Oracle) rss[MM_ANONPAGES]++; 951b51ad4f8SDavid Hildenbrand } else if (page) { 95214ddee41SMatthew Wilcox (Oracle) folio_get(folio); 953fb3d824dSDavid Hildenbrand page_dup_file_rmap(page, false); 954edf50470SMatthew Wilcox (Oracle) rss[mm_counter_file(page)]++; 95570e806e4SPeter Xu } 95670e806e4SPeter Xu 9571da177e4SLinus Torvalds /* 9581da177e4SLinus Torvalds * If it's a COW mapping, write protect it both 9591da177e4SLinus Torvalds * in the parent and the child 9601da177e4SLinus Torvalds */ 9611b2de5d0SLinus Torvalds if (is_cow_mapping(vm_flags) && pte_write(pte)) { 9621da177e4SLinus Torvalds ptep_set_wrprotect(src_mm, addr, src_pte); 9633dc90795SZachary Amsden pte = pte_wrprotect(pte); 9641da177e4SLinus Torvalds } 96514ddee41SMatthew Wilcox (Oracle) VM_BUG_ON(page && folio_test_anon(folio) && PageAnonExclusive(page)); 9661da177e4SLinus Torvalds 9671da177e4SLinus Torvalds /* 9681da177e4SLinus Torvalds * If it's a shared mapping, mark it clean in 9691da177e4SLinus Torvalds * the child 9701da177e4SLinus Torvalds */ 9711da177e4SLinus Torvalds if (vm_flags & VM_SHARED) 9721da177e4SLinus Torvalds pte = pte_mkclean(pte); 9731da177e4SLinus Torvalds pte = pte_mkold(pte); 9746aab341eSLinus Torvalds 9758f34f1eaSPeter Xu if (!userfaultfd_wp(dst_vma)) 976b569a176SPeter Xu pte = pte_clear_uffd_wp(pte); 977b569a176SPeter Xu 978c78f4636SPeter Xu set_pte_at(dst_vma->vm_mm, addr, dst_pte, pte); 97970e806e4SPeter Xu return 0; 9806aab341eSLinus Torvalds } 981ae859762SHugh Dickins 982edf50470SMatthew Wilcox (Oracle) static inline struct folio *page_copy_prealloc(struct mm_struct *src_mm, 983edf50470SMatthew Wilcox (Oracle) struct vm_area_struct *vma, unsigned long addr) 98470e806e4SPeter Xu { 985edf50470SMatthew Wilcox (Oracle) struct folio *new_folio; 98670e806e4SPeter Xu 987edf50470SMatthew Wilcox (Oracle) new_folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, vma, addr, false); 988edf50470SMatthew Wilcox (Oracle) if (!new_folio) 98970e806e4SPeter Xu return NULL; 99070e806e4SPeter Xu 991edf50470SMatthew Wilcox (Oracle) if (mem_cgroup_charge(new_folio, src_mm, GFP_KERNEL)) { 992edf50470SMatthew Wilcox (Oracle) folio_put(new_folio); 99370e806e4SPeter Xu return NULL; 99470e806e4SPeter Xu } 995e601ded4SKefeng Wang folio_throttle_swaprate(new_folio, GFP_KERNEL); 99670e806e4SPeter Xu 997edf50470SMatthew Wilcox (Oracle) return new_folio; 9981da177e4SLinus Torvalds } 9991da177e4SLinus Torvalds 1000c78f4636SPeter Xu static int 1001c78f4636SPeter Xu copy_pte_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, 1002c78f4636SPeter Xu pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr, 1003c78f4636SPeter Xu unsigned long end) 10041da177e4SLinus Torvalds { 1005c78f4636SPeter Xu struct mm_struct *dst_mm = dst_vma->vm_mm; 1006c78f4636SPeter Xu struct mm_struct *src_mm = src_vma->vm_mm; 1007c36987e2SDaisuke Nishimura pte_t *orig_src_pte, *orig_dst_pte; 10081da177e4SLinus Torvalds pte_t *src_pte, *dst_pte; 1009c33c7948SRyan Roberts pte_t ptent; 1010c74df32cSHugh Dickins spinlock_t *src_ptl, *dst_ptl; 101170e806e4SPeter Xu int progress, ret = 0; 1012d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 1013570a335bSHugh Dickins swp_entry_t entry = (swp_entry_t){0}; 1014edf50470SMatthew Wilcox (Oracle) struct folio *prealloc = NULL; 10151da177e4SLinus Torvalds 10161da177e4SLinus Torvalds again: 101770e806e4SPeter Xu progress = 0; 1018d559db08SKAMEZAWA Hiroyuki init_rss_vec(rss); 1019d559db08SKAMEZAWA Hiroyuki 10203db82b93SHugh Dickins /* 10213db82b93SHugh Dickins * copy_pmd_range()'s prior pmd_none_or_clear_bad(src_pmd), and the 10223db82b93SHugh Dickins * error handling here, assume that exclusive mmap_lock on dst and src 10233db82b93SHugh Dickins * protects anon from unexpected THP transitions; with shmem and file 10243db82b93SHugh Dickins * protected by mmap_lock-less collapse skipping areas with anon_vma 10253db82b93SHugh Dickins * (whereas vma_needs_copy() skips areas without anon_vma). A rework 10263db82b93SHugh Dickins * can remove such assumptions later, but this is good enough for now. 10273db82b93SHugh Dickins */ 1028c74df32cSHugh Dickins dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl); 102970e806e4SPeter Xu if (!dst_pte) { 103070e806e4SPeter Xu ret = -ENOMEM; 103170e806e4SPeter Xu goto out; 103270e806e4SPeter Xu } 10333db82b93SHugh Dickins src_pte = pte_offset_map_nolock(src_mm, src_pmd, addr, &src_ptl); 10343db82b93SHugh Dickins if (!src_pte) { 10353db82b93SHugh Dickins pte_unmap_unlock(dst_pte, dst_ptl); 10363db82b93SHugh Dickins /* ret == 0 */ 10373db82b93SHugh Dickins goto out; 10383db82b93SHugh Dickins } 1039f20dc5f7SIngo Molnar spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 1040c36987e2SDaisuke Nishimura orig_src_pte = src_pte; 1041c36987e2SDaisuke Nishimura orig_dst_pte = dst_pte; 10426606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 10431da177e4SLinus Torvalds 10441da177e4SLinus Torvalds do { 10451da177e4SLinus Torvalds /* 10461da177e4SLinus Torvalds * We are holding two locks at this point - either of them 10471da177e4SLinus Torvalds * could generate latencies in another task on another CPU. 10481da177e4SLinus Torvalds */ 1049e040f218SHugh Dickins if (progress >= 32) { 1050e040f218SHugh Dickins progress = 0; 1051e040f218SHugh Dickins if (need_resched() || 105295c354feSNick Piggin spin_needbreak(src_ptl) || spin_needbreak(dst_ptl)) 10531da177e4SLinus Torvalds break; 1054e040f218SHugh Dickins } 1055c33c7948SRyan Roberts ptent = ptep_get(src_pte); 1056c33c7948SRyan Roberts if (pte_none(ptent)) { 10571da177e4SLinus Torvalds progress++; 10581da177e4SLinus Torvalds continue; 10591da177e4SLinus Torvalds } 1060c33c7948SRyan Roberts if (unlikely(!pte_present(ptent))) { 10619a5cc85cSAlistair Popple ret = copy_nonpresent_pte(dst_mm, src_mm, 106279a1971cSLinus Torvalds dst_pte, src_pte, 10638f34f1eaSPeter Xu dst_vma, src_vma, 10648f34f1eaSPeter Xu addr, rss); 10659a5cc85cSAlistair Popple if (ret == -EIO) { 1066c33c7948SRyan Roberts entry = pte_to_swp_entry(ptep_get(src_pte)); 1067570a335bSHugh Dickins break; 1068b756a3b5SAlistair Popple } else if (ret == -EBUSY) { 1069b756a3b5SAlistair Popple break; 1070b756a3b5SAlistair Popple } else if (!ret) { 10711da177e4SLinus Torvalds progress += 8; 107279a1971cSLinus Torvalds continue; 107379a1971cSLinus Torvalds } 1074b756a3b5SAlistair Popple 1075b756a3b5SAlistair Popple /* 1076b756a3b5SAlistair Popple * Device exclusive entry restored, continue by copying 1077b756a3b5SAlistair Popple * the now present pte. 1078b756a3b5SAlistair Popple */ 1079b756a3b5SAlistair Popple WARN_ON_ONCE(ret != -ENOENT); 1080b756a3b5SAlistair Popple } 108170e806e4SPeter Xu /* copy_present_pte() will clear `*prealloc' if consumed */ 1082c78f4636SPeter Xu ret = copy_present_pte(dst_vma, src_vma, dst_pte, src_pte, 1083c78f4636SPeter Xu addr, rss, &prealloc); 108470e806e4SPeter Xu /* 108570e806e4SPeter Xu * If we need a pre-allocated page for this pte, drop the 108670e806e4SPeter Xu * locks, allocate, and try again. 108770e806e4SPeter Xu */ 108870e806e4SPeter Xu if (unlikely(ret == -EAGAIN)) 108970e806e4SPeter Xu break; 109070e806e4SPeter Xu if (unlikely(prealloc)) { 109170e806e4SPeter Xu /* 109270e806e4SPeter Xu * pre-alloc page cannot be reused by next time so as 109370e806e4SPeter Xu * to strictly follow mempolicy (e.g., alloc_page_vma() 109470e806e4SPeter Xu * will allocate page according to address). This 109570e806e4SPeter Xu * could only happen if one pinned pte changed. 109670e806e4SPeter Xu */ 1097edf50470SMatthew Wilcox (Oracle) folio_put(prealloc); 109870e806e4SPeter Xu prealloc = NULL; 109970e806e4SPeter Xu } 110079a1971cSLinus Torvalds progress += 8; 11011da177e4SLinus Torvalds } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end); 11021da177e4SLinus Torvalds 11036606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 11043db82b93SHugh Dickins pte_unmap_unlock(orig_src_pte, src_ptl); 1105d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(dst_mm, rss); 1106c36987e2SDaisuke Nishimura pte_unmap_unlock(orig_dst_pte, dst_ptl); 1107c74df32cSHugh Dickins cond_resched(); 1108570a335bSHugh Dickins 11099a5cc85cSAlistair Popple if (ret == -EIO) { 11109a5cc85cSAlistair Popple VM_WARN_ON_ONCE(!entry.val); 111170e806e4SPeter Xu if (add_swap_count_continuation(entry, GFP_KERNEL) < 0) { 111270e806e4SPeter Xu ret = -ENOMEM; 111370e806e4SPeter Xu goto out; 111470e806e4SPeter Xu } 111570e806e4SPeter Xu entry.val = 0; 1116b756a3b5SAlistair Popple } else if (ret == -EBUSY) { 1117b756a3b5SAlistair Popple goto out; 11189a5cc85cSAlistair Popple } else if (ret == -EAGAIN) { 1119c78f4636SPeter Xu prealloc = page_copy_prealloc(src_mm, src_vma, addr); 112070e806e4SPeter Xu if (!prealloc) 1121570a335bSHugh Dickins return -ENOMEM; 11229a5cc85cSAlistair Popple } else if (ret) { 11239a5cc85cSAlistair Popple VM_WARN_ON_ONCE(1); 11249a5cc85cSAlistair Popple } 11259a5cc85cSAlistair Popple 112670e806e4SPeter Xu /* We've captured and resolved the error. Reset, try again. */ 112770e806e4SPeter Xu ret = 0; 11289a5cc85cSAlistair Popple 11291da177e4SLinus Torvalds if (addr != end) 11301da177e4SLinus Torvalds goto again; 113170e806e4SPeter Xu out: 113270e806e4SPeter Xu if (unlikely(prealloc)) 1133edf50470SMatthew Wilcox (Oracle) folio_put(prealloc); 113470e806e4SPeter Xu return ret; 11351da177e4SLinus Torvalds } 11361da177e4SLinus Torvalds 1137c78f4636SPeter Xu static inline int 1138c78f4636SPeter Xu copy_pmd_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, 1139c78f4636SPeter Xu pud_t *dst_pud, pud_t *src_pud, unsigned long addr, 1140c78f4636SPeter Xu unsigned long end) 11411da177e4SLinus Torvalds { 1142c78f4636SPeter Xu struct mm_struct *dst_mm = dst_vma->vm_mm; 1143c78f4636SPeter Xu struct mm_struct *src_mm = src_vma->vm_mm; 11441da177e4SLinus Torvalds pmd_t *src_pmd, *dst_pmd; 11451da177e4SLinus Torvalds unsigned long next; 11461da177e4SLinus Torvalds 11471da177e4SLinus Torvalds dst_pmd = pmd_alloc(dst_mm, dst_pud, addr); 11481da177e4SLinus Torvalds if (!dst_pmd) 11491da177e4SLinus Torvalds return -ENOMEM; 11501da177e4SLinus Torvalds src_pmd = pmd_offset(src_pud, addr); 11511da177e4SLinus Torvalds do { 11521da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 115384c3fc4eSZi Yan if (is_swap_pmd(*src_pmd) || pmd_trans_huge(*src_pmd) 115484c3fc4eSZi Yan || pmd_devmap(*src_pmd)) { 115571e3aac0SAndrea Arcangeli int err; 1156c78f4636SPeter Xu VM_BUG_ON_VMA(next-addr != HPAGE_PMD_SIZE, src_vma); 11578f34f1eaSPeter Xu err = copy_huge_pmd(dst_mm, src_mm, dst_pmd, src_pmd, 11588f34f1eaSPeter Xu addr, dst_vma, src_vma); 115971e3aac0SAndrea Arcangeli if (err == -ENOMEM) 116071e3aac0SAndrea Arcangeli return -ENOMEM; 116171e3aac0SAndrea Arcangeli if (!err) 116271e3aac0SAndrea Arcangeli continue; 116371e3aac0SAndrea Arcangeli /* fall through */ 116471e3aac0SAndrea Arcangeli } 11651da177e4SLinus Torvalds if (pmd_none_or_clear_bad(src_pmd)) 11661da177e4SLinus Torvalds continue; 1167c78f4636SPeter Xu if (copy_pte_range(dst_vma, src_vma, dst_pmd, src_pmd, 1168c78f4636SPeter Xu addr, next)) 11691da177e4SLinus Torvalds return -ENOMEM; 11701da177e4SLinus Torvalds } while (dst_pmd++, src_pmd++, addr = next, addr != end); 11711da177e4SLinus Torvalds return 0; 11721da177e4SLinus Torvalds } 11731da177e4SLinus Torvalds 1174c78f4636SPeter Xu static inline int 1175c78f4636SPeter Xu copy_pud_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, 1176c78f4636SPeter Xu p4d_t *dst_p4d, p4d_t *src_p4d, unsigned long addr, 1177c78f4636SPeter Xu unsigned long end) 11781da177e4SLinus Torvalds { 1179c78f4636SPeter Xu struct mm_struct *dst_mm = dst_vma->vm_mm; 1180c78f4636SPeter Xu struct mm_struct *src_mm = src_vma->vm_mm; 11811da177e4SLinus Torvalds pud_t *src_pud, *dst_pud; 11821da177e4SLinus Torvalds unsigned long next; 11831da177e4SLinus Torvalds 1184c2febafcSKirill A. Shutemov dst_pud = pud_alloc(dst_mm, dst_p4d, addr); 11851da177e4SLinus Torvalds if (!dst_pud) 11861da177e4SLinus Torvalds return -ENOMEM; 1187c2febafcSKirill A. Shutemov src_pud = pud_offset(src_p4d, addr); 11881da177e4SLinus Torvalds do { 11891da177e4SLinus Torvalds next = pud_addr_end(addr, end); 1190a00cc7d9SMatthew Wilcox if (pud_trans_huge(*src_pud) || pud_devmap(*src_pud)) { 1191a00cc7d9SMatthew Wilcox int err; 1192a00cc7d9SMatthew Wilcox 1193c78f4636SPeter Xu VM_BUG_ON_VMA(next-addr != HPAGE_PUD_SIZE, src_vma); 1194a00cc7d9SMatthew Wilcox err = copy_huge_pud(dst_mm, src_mm, 1195c78f4636SPeter Xu dst_pud, src_pud, addr, src_vma); 1196a00cc7d9SMatthew Wilcox if (err == -ENOMEM) 1197a00cc7d9SMatthew Wilcox return -ENOMEM; 1198a00cc7d9SMatthew Wilcox if (!err) 1199a00cc7d9SMatthew Wilcox continue; 1200a00cc7d9SMatthew Wilcox /* fall through */ 1201a00cc7d9SMatthew Wilcox } 12021da177e4SLinus Torvalds if (pud_none_or_clear_bad(src_pud)) 12031da177e4SLinus Torvalds continue; 1204c78f4636SPeter Xu if (copy_pmd_range(dst_vma, src_vma, dst_pud, src_pud, 1205c78f4636SPeter Xu addr, next)) 12061da177e4SLinus Torvalds return -ENOMEM; 12071da177e4SLinus Torvalds } while (dst_pud++, src_pud++, addr = next, addr != end); 12081da177e4SLinus Torvalds return 0; 12091da177e4SLinus Torvalds } 12101da177e4SLinus Torvalds 1211c78f4636SPeter Xu static inline int 1212c78f4636SPeter Xu copy_p4d_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, 1213c78f4636SPeter Xu pgd_t *dst_pgd, pgd_t *src_pgd, unsigned long addr, 1214c78f4636SPeter Xu unsigned long end) 1215c2febafcSKirill A. Shutemov { 1216c78f4636SPeter Xu struct mm_struct *dst_mm = dst_vma->vm_mm; 1217c2febafcSKirill A. Shutemov p4d_t *src_p4d, *dst_p4d; 1218c2febafcSKirill A. Shutemov unsigned long next; 1219c2febafcSKirill A. Shutemov 1220c2febafcSKirill A. Shutemov dst_p4d = p4d_alloc(dst_mm, dst_pgd, addr); 1221c2febafcSKirill A. Shutemov if (!dst_p4d) 1222c2febafcSKirill A. Shutemov return -ENOMEM; 1223c2febafcSKirill A. Shutemov src_p4d = p4d_offset(src_pgd, addr); 1224c2febafcSKirill A. Shutemov do { 1225c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 1226c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(src_p4d)) 1227c2febafcSKirill A. Shutemov continue; 1228c78f4636SPeter Xu if (copy_pud_range(dst_vma, src_vma, dst_p4d, src_p4d, 1229c78f4636SPeter Xu addr, next)) 1230c2febafcSKirill A. Shutemov return -ENOMEM; 1231c2febafcSKirill A. Shutemov } while (dst_p4d++, src_p4d++, addr = next, addr != end); 1232c2febafcSKirill A. Shutemov return 0; 1233c2febafcSKirill A. Shutemov } 1234c2febafcSKirill A. Shutemov 1235c56d1b62SPeter Xu /* 1236c56d1b62SPeter Xu * Return true if the vma needs to copy the pgtable during this fork(). Return 1237c56d1b62SPeter Xu * false when we can speed up fork() by allowing lazy page faults later until 1238c56d1b62SPeter Xu * when the child accesses the memory range. 1239c56d1b62SPeter Xu */ 1240bc70fbf2SPeter Xu static bool 1241c56d1b62SPeter Xu vma_needs_copy(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) 1242c56d1b62SPeter Xu { 1243c56d1b62SPeter Xu /* 1244c56d1b62SPeter Xu * Always copy pgtables when dst_vma has uffd-wp enabled even if it's 1245c56d1b62SPeter Xu * file-backed (e.g. shmem). Because when uffd-wp is enabled, pgtable 1246c56d1b62SPeter Xu * contains uffd-wp protection information, that's something we can't 1247c56d1b62SPeter Xu * retrieve from page cache, and skip copying will lose those info. 1248c56d1b62SPeter Xu */ 1249c56d1b62SPeter Xu if (userfaultfd_wp(dst_vma)) 1250c56d1b62SPeter Xu return true; 1251c56d1b62SPeter Xu 1252bcd51a3cSMike Kravetz if (src_vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)) 1253c56d1b62SPeter Xu return true; 1254c56d1b62SPeter Xu 1255c56d1b62SPeter Xu if (src_vma->anon_vma) 1256c56d1b62SPeter Xu return true; 1257c56d1b62SPeter Xu 1258c56d1b62SPeter Xu /* 1259c56d1b62SPeter Xu * Don't copy ptes where a page fault will fill them correctly. Fork 1260c56d1b62SPeter Xu * becomes much lighter when there are big shared or private readonly 1261c56d1b62SPeter Xu * mappings. The tradeoff is that copy_page_range is more efficient 1262c56d1b62SPeter Xu * than faulting. 1263c56d1b62SPeter Xu */ 1264c56d1b62SPeter Xu return false; 1265c56d1b62SPeter Xu } 1266c56d1b62SPeter Xu 1267c78f4636SPeter Xu int 1268c78f4636SPeter Xu copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) 12691da177e4SLinus Torvalds { 12701da177e4SLinus Torvalds pgd_t *src_pgd, *dst_pgd; 12711da177e4SLinus Torvalds unsigned long next; 1272c78f4636SPeter Xu unsigned long addr = src_vma->vm_start; 1273c78f4636SPeter Xu unsigned long end = src_vma->vm_end; 1274c78f4636SPeter Xu struct mm_struct *dst_mm = dst_vma->vm_mm; 1275c78f4636SPeter Xu struct mm_struct *src_mm = src_vma->vm_mm; 1276ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 12772ec74c3eSSagi Grimberg bool is_cow; 1278cddb8a5cSAndrea Arcangeli int ret; 12791da177e4SLinus Torvalds 1280c56d1b62SPeter Xu if (!vma_needs_copy(dst_vma, src_vma)) 1281d992895bSNick Piggin return 0; 1282d992895bSNick Piggin 1283c78f4636SPeter Xu if (is_vm_hugetlb_page(src_vma)) 1284bc70fbf2SPeter Xu return copy_hugetlb_page_range(dst_mm, src_mm, dst_vma, src_vma); 12851da177e4SLinus Torvalds 1286c78f4636SPeter Xu if (unlikely(src_vma->vm_flags & VM_PFNMAP)) { 12872ab64037Svenkatesh.pallipadi@intel.com /* 12882ab64037Svenkatesh.pallipadi@intel.com * We do not free on error cases below as remove_vma 12892ab64037Svenkatesh.pallipadi@intel.com * gets called on error from higher level routine 12902ab64037Svenkatesh.pallipadi@intel.com */ 1291c78f4636SPeter Xu ret = track_pfn_copy(src_vma); 12922ab64037Svenkatesh.pallipadi@intel.com if (ret) 12932ab64037Svenkatesh.pallipadi@intel.com return ret; 12942ab64037Svenkatesh.pallipadi@intel.com } 12952ab64037Svenkatesh.pallipadi@intel.com 1296cddb8a5cSAndrea Arcangeli /* 1297cddb8a5cSAndrea Arcangeli * We need to invalidate the secondary MMU mappings only when 1298cddb8a5cSAndrea Arcangeli * there could be a permission downgrade on the ptes of the 1299cddb8a5cSAndrea Arcangeli * parent mm. And a permission downgrade will only happen if 1300cddb8a5cSAndrea Arcangeli * is_cow_mapping() returns true. 1301cddb8a5cSAndrea Arcangeli */ 1302c78f4636SPeter Xu is_cow = is_cow_mapping(src_vma->vm_flags); 1303ac46d4f3SJérôme Glisse 1304ac46d4f3SJérôme Glisse if (is_cow) { 13057269f999SJérôme Glisse mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_PAGE, 13067d4a8be0SAlistair Popple 0, src_mm, addr, end); 1307ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 130857efa1feSJason Gunthorpe /* 130957efa1feSJason Gunthorpe * Disabling preemption is not needed for the write side, as 131057efa1feSJason Gunthorpe * the read side doesn't spin, but goes to the mmap_lock. 131157efa1feSJason Gunthorpe * 131257efa1feSJason Gunthorpe * Use the raw variant of the seqcount_t write API to avoid 131357efa1feSJason Gunthorpe * lockdep complaining about preemptibility. 131457efa1feSJason Gunthorpe */ 1315e727bfd5SSuren Baghdasaryan vma_assert_write_locked(src_vma); 131657efa1feSJason Gunthorpe raw_write_seqcount_begin(&src_mm->write_protect_seq); 1317ac46d4f3SJérôme Glisse } 1318cddb8a5cSAndrea Arcangeli 1319cddb8a5cSAndrea Arcangeli ret = 0; 13201da177e4SLinus Torvalds dst_pgd = pgd_offset(dst_mm, addr); 13211da177e4SLinus Torvalds src_pgd = pgd_offset(src_mm, addr); 13221da177e4SLinus Torvalds do { 13231da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 13241da177e4SLinus Torvalds if (pgd_none_or_clear_bad(src_pgd)) 13251da177e4SLinus Torvalds continue; 1326c78f4636SPeter Xu if (unlikely(copy_p4d_range(dst_vma, src_vma, dst_pgd, src_pgd, 1327c78f4636SPeter Xu addr, next))) { 1328d155df53SMa Wupeng untrack_pfn_clear(dst_vma); 1329cddb8a5cSAndrea Arcangeli ret = -ENOMEM; 1330cddb8a5cSAndrea Arcangeli break; 1331cddb8a5cSAndrea Arcangeli } 13321da177e4SLinus Torvalds } while (dst_pgd++, src_pgd++, addr = next, addr != end); 1333cddb8a5cSAndrea Arcangeli 133457efa1feSJason Gunthorpe if (is_cow) { 133557efa1feSJason Gunthorpe raw_write_seqcount_end(&src_mm->write_protect_seq); 1336ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 133757efa1feSJason Gunthorpe } 1338cddb8a5cSAndrea Arcangeli return ret; 13391da177e4SLinus Torvalds } 13401da177e4SLinus Torvalds 13415abfd71dSPeter Xu /* Whether we should zap all COWed (private) pages too */ 13425abfd71dSPeter Xu static inline bool should_zap_cows(struct zap_details *details) 13435abfd71dSPeter Xu { 13445abfd71dSPeter Xu /* By default, zap all pages */ 13455abfd71dSPeter Xu if (!details) 13465abfd71dSPeter Xu return true; 13475abfd71dSPeter Xu 13485abfd71dSPeter Xu /* Or, we zap COWed pages only if the caller wants to */ 13492e148f1eSPeter Xu return details->even_cows; 13505abfd71dSPeter Xu } 13515abfd71dSPeter Xu 13522e148f1eSPeter Xu /* Decides whether we should zap this page with the page pointer specified */ 1353254ab940SPeter Xu static inline bool should_zap_page(struct zap_details *details, struct page *page) 13543506659eSMatthew Wilcox (Oracle) { 13555abfd71dSPeter Xu /* If we can make a decision without *page.. */ 13565abfd71dSPeter Xu if (should_zap_cows(details)) 1357254ab940SPeter Xu return true; 13583506659eSMatthew Wilcox (Oracle) 13595abfd71dSPeter Xu /* E.g. the caller passes NULL for the case of a zero page */ 13605abfd71dSPeter Xu if (!page) 1361254ab940SPeter Xu return true; 13625abfd71dSPeter Xu 13632e148f1eSPeter Xu /* Otherwise we should only zap non-anon pages */ 13642e148f1eSPeter Xu return !PageAnon(page); 13653506659eSMatthew Wilcox (Oracle) } 13663506659eSMatthew Wilcox (Oracle) 1367999dad82SPeter Xu static inline bool zap_drop_file_uffd_wp(struct zap_details *details) 1368999dad82SPeter Xu { 1369999dad82SPeter Xu if (!details) 1370999dad82SPeter Xu return false; 1371999dad82SPeter Xu 1372999dad82SPeter Xu return details->zap_flags & ZAP_FLAG_DROP_MARKER; 1373999dad82SPeter Xu } 1374999dad82SPeter Xu 1375999dad82SPeter Xu /* 1376999dad82SPeter Xu * This function makes sure that we'll replace the none pte with an uffd-wp 1377999dad82SPeter Xu * swap special pte marker when necessary. Must be with the pgtable lock held. 1378999dad82SPeter Xu */ 1379999dad82SPeter Xu static inline void 1380999dad82SPeter Xu zap_install_uffd_wp_if_needed(struct vm_area_struct *vma, 1381999dad82SPeter Xu unsigned long addr, pte_t *pte, 1382999dad82SPeter Xu struct zap_details *details, pte_t pteval) 1383999dad82SPeter Xu { 13842bad466cSPeter Xu /* Zap on anonymous always means dropping everything */ 13852bad466cSPeter Xu if (vma_is_anonymous(vma)) 13862bad466cSPeter Xu return; 13872bad466cSPeter Xu 1388999dad82SPeter Xu if (zap_drop_file_uffd_wp(details)) 1389999dad82SPeter Xu return; 1390999dad82SPeter Xu 1391999dad82SPeter Xu pte_install_uffd_wp_if_needed(vma, addr, pte, pteval); 1392999dad82SPeter Xu } 1393999dad82SPeter Xu 139451c6f666SRobin Holt static unsigned long zap_pte_range(struct mmu_gather *tlb, 1395b5810039SNick Piggin struct vm_area_struct *vma, pmd_t *pmd, 13961da177e4SLinus Torvalds unsigned long addr, unsigned long end, 139797a89413SPeter Zijlstra struct zap_details *details) 13981da177e4SLinus Torvalds { 1399b5810039SNick Piggin struct mm_struct *mm = tlb->mm; 1400d16dfc55SPeter Zijlstra int force_flush = 0; 1401d559db08SKAMEZAWA Hiroyuki int rss[NR_MM_COUNTERS]; 140297a89413SPeter Zijlstra spinlock_t *ptl; 14035f1a1907SSteven Rostedt pte_t *start_pte; 140497a89413SPeter Zijlstra pte_t *pte; 14058a5f14a2SKirill A. Shutemov swp_entry_t entry; 1406d559db08SKAMEZAWA Hiroyuki 1407ed6a7935SPeter Zijlstra tlb_change_page_size(tlb, PAGE_SIZE); 1408e303297eSPeter Zijlstra init_rss_vec(rss); 14093db82b93SHugh Dickins start_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl); 14103db82b93SHugh Dickins if (!pte) 14113db82b93SHugh Dickins return addr; 14123db82b93SHugh Dickins 14133ea27719SMel Gorman flush_tlb_batched_pending(mm); 14146606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 14151da177e4SLinus Torvalds do { 1416c33c7948SRyan Roberts pte_t ptent = ptep_get(pte); 14178018db85SPeter Xu struct page *page; 14188018db85SPeter Xu 1419166f61b9STobin C Harding if (pte_none(ptent)) 14201da177e4SLinus Torvalds continue; 142151c6f666SRobin Holt 14227b167b68SMinchan Kim if (need_resched()) 14237b167b68SMinchan Kim break; 14247b167b68SMinchan Kim 14256f5e6b9eSHugh Dickins if (pte_present(ptent)) { 14265df397deSLinus Torvalds unsigned int delay_rmap; 14275df397deSLinus Torvalds 142825b2995aSChristoph Hellwig page = vm_normal_page(vma, addr, ptent); 1429254ab940SPeter Xu if (unlikely(!should_zap_page(details, page))) 14301da177e4SLinus Torvalds continue; 1431b5810039SNick Piggin ptent = ptep_get_and_clear_full(mm, addr, pte, 1432a600388dSZachary Amsden tlb->fullmm); 14331da177e4SLinus Torvalds tlb_remove_tlb_entry(tlb, pte, addr); 1434999dad82SPeter Xu zap_install_uffd_wp_if_needed(vma, addr, pte, details, 1435999dad82SPeter Xu ptent); 1436e2942062Sxu xin if (unlikely(!page)) { 14376080d19fSxu xin ksm_might_unmap_zero_page(mm, ptent); 14381da177e4SLinus Torvalds continue; 1439e2942062Sxu xin } 1440eca56ff9SJerome Marchand 14415df397deSLinus Torvalds delay_rmap = 0; 1442eca56ff9SJerome Marchand if (!PageAnon(page)) { 14431cf35d47SLinus Torvalds if (pte_dirty(ptent)) { 14446237bcd9SHugh Dickins set_page_dirty(page); 14455df397deSLinus Torvalds if (tlb_delay_rmap(tlb)) { 14465df397deSLinus Torvalds delay_rmap = 1; 14475df397deSLinus Torvalds force_flush = 1; 14485df397deSLinus Torvalds } 14491cf35d47SLinus Torvalds } 14508788f678SYu Zhao if (pte_young(ptent) && likely(vma_has_recency(vma))) 1451bf3f3bc5SNick Piggin mark_page_accessed(page); 14526237bcd9SHugh Dickins } 1453eca56ff9SJerome Marchand rss[mm_counter(page)]--; 14545df397deSLinus Torvalds if (!delay_rmap) { 1455cea86fe2SHugh Dickins page_remove_rmap(page, vma, false); 14563dc14741SHugh Dickins if (unlikely(page_mapcount(page) < 0)) 14573dc14741SHugh Dickins print_bad_pte(vma, addr, ptent, page); 14585df397deSLinus Torvalds } 14595df397deSLinus Torvalds if (unlikely(__tlb_remove_page(tlb, page, delay_rmap))) { 14601cf35d47SLinus Torvalds force_flush = 1; 1461ce9ec37bSWill Deacon addr += PAGE_SIZE; 1462d16dfc55SPeter Zijlstra break; 14631cf35d47SLinus Torvalds } 14641da177e4SLinus Torvalds continue; 14651da177e4SLinus Torvalds } 14665042db43SJérôme Glisse 14675042db43SJérôme Glisse entry = pte_to_swp_entry(ptent); 1468b756a3b5SAlistair Popple if (is_device_private_entry(entry) || 1469b756a3b5SAlistair Popple is_device_exclusive_entry(entry)) { 14708018db85SPeter Xu page = pfn_swap_entry_to_page(entry); 1471254ab940SPeter Xu if (unlikely(!should_zap_page(details, page))) 14725042db43SJérôme Glisse continue; 1473999dad82SPeter Xu /* 1474999dad82SPeter Xu * Both device private/exclusive mappings should only 1475999dad82SPeter Xu * work with anonymous page so far, so we don't need to 1476999dad82SPeter Xu * consider uffd-wp bit when zap. For more information, 1477999dad82SPeter Xu * see zap_install_uffd_wp_if_needed(). 1478999dad82SPeter Xu */ 1479999dad82SPeter Xu WARN_ON_ONCE(!vma_is_anonymous(vma)); 14805042db43SJérôme Glisse rss[mm_counter(page)]--; 1481b756a3b5SAlistair Popple if (is_device_private_entry(entry)) 1482cea86fe2SHugh Dickins page_remove_rmap(page, vma, false); 14835042db43SJérôme Glisse put_page(page); 14848018db85SPeter Xu } else if (!non_swap_entry(entry)) { 14855abfd71dSPeter Xu /* Genuine swap entry, hence a private anon page */ 14865abfd71dSPeter Xu if (!should_zap_cows(details)) 14871da177e4SLinus Torvalds continue; 1488b084d435SKAMEZAWA Hiroyuki rss[MM_SWAPENTS]--; 14898018db85SPeter Xu if (unlikely(!free_swap_and_cache(entry))) 14908018db85SPeter Xu print_bad_pte(vma, addr, ptent, NULL); 14915abfd71dSPeter Xu } else if (is_migration_entry(entry)) { 1492af5cdaf8SAlistair Popple page = pfn_swap_entry_to_page(entry); 1493254ab940SPeter Xu if (!should_zap_page(details, page)) 14945abfd71dSPeter Xu continue; 1495eca56ff9SJerome Marchand rss[mm_counter(page)]--; 1496999dad82SPeter Xu } else if (pte_marker_entry_uffd_wp(entry)) { 14972bad466cSPeter Xu /* 14982bad466cSPeter Xu * For anon: always drop the marker; for file: only 14992bad466cSPeter Xu * drop the marker if explicitly requested. 15002bad466cSPeter Xu */ 15012bad466cSPeter Xu if (!vma_is_anonymous(vma) && 15022bad466cSPeter Xu !zap_drop_file_uffd_wp(details)) 1503999dad82SPeter Xu continue; 15049f186f9eSMiaohe Lin } else if (is_hwpoison_entry(entry) || 1505af19487fSAxel Rasmussen is_poisoned_swp_entry(entry)) { 15065abfd71dSPeter Xu if (!should_zap_cows(details)) 15075abfd71dSPeter Xu continue; 15085abfd71dSPeter Xu } else { 15095abfd71dSPeter Xu /* We should have covered all the swap entry types */ 15105abfd71dSPeter Xu WARN_ON_ONCE(1); 15119f9f1acdSKonstantin Khlebnikov } 15129888a1caSZachary Amsden pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); 1513999dad82SPeter Xu zap_install_uffd_wp_if_needed(vma, addr, pte, details, ptent); 151497a89413SPeter Zijlstra } while (pte++, addr += PAGE_SIZE, addr != end); 1515ae859762SHugh Dickins 1516d559db08SKAMEZAWA Hiroyuki add_mm_rss_vec(mm, rss); 15176606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 151851c6f666SRobin Holt 15191cf35d47SLinus Torvalds /* Do the actual TLB flush before dropping ptl */ 15205df397deSLinus Torvalds if (force_flush) { 15211cf35d47SLinus Torvalds tlb_flush_mmu_tlbonly(tlb); 15225df397deSLinus Torvalds tlb_flush_rmaps(tlb, vma); 15235df397deSLinus Torvalds } 15241cf35d47SLinus Torvalds pte_unmap_unlock(start_pte, ptl); 15251cf35d47SLinus Torvalds 15261cf35d47SLinus Torvalds /* 15271cf35d47SLinus Torvalds * If we forced a TLB flush (either due to running out of 15281cf35d47SLinus Torvalds * batch buffers or because we needed to flush dirty TLB 15291cf35d47SLinus Torvalds * entries before releasing the ptl), free the batched 15303db82b93SHugh Dickins * memory too. Come back again if we didn't do everything. 15311cf35d47SLinus Torvalds */ 15323db82b93SHugh Dickins if (force_flush) 1533fa0aafb8SPeter Zijlstra tlb_flush_mmu(tlb); 1534d16dfc55SPeter Zijlstra 153551c6f666SRobin Holt return addr; 15361da177e4SLinus Torvalds } 15371da177e4SLinus Torvalds 153851c6f666SRobin Holt static inline unsigned long zap_pmd_range(struct mmu_gather *tlb, 1539b5810039SNick Piggin struct vm_area_struct *vma, pud_t *pud, 15401da177e4SLinus Torvalds unsigned long addr, unsigned long end, 154197a89413SPeter Zijlstra struct zap_details *details) 15421da177e4SLinus Torvalds { 15431da177e4SLinus Torvalds pmd_t *pmd; 15441da177e4SLinus Torvalds unsigned long next; 15451da177e4SLinus Torvalds 15461da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 15471da177e4SLinus Torvalds do { 15481da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 154984c3fc4eSZi Yan if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) { 155053406ed1SHugh Dickins if (next - addr != HPAGE_PMD_SIZE) 1551fd60775aSDavid Rientjes __split_huge_pmd(vma, pmd, addr, false, NULL); 15523db82b93SHugh Dickins else if (zap_huge_pmd(tlb, vma, pmd, addr)) { 15533db82b93SHugh Dickins addr = next; 15543db82b93SHugh Dickins continue; 15553db82b93SHugh Dickins } 155671e3aac0SAndrea Arcangeli /* fall through */ 15573506659eSMatthew Wilcox (Oracle) } else if (details && details->single_folio && 15583506659eSMatthew Wilcox (Oracle) folio_test_pmd_mappable(details->single_folio) && 155922061a1fSHugh Dickins next - addr == HPAGE_PMD_SIZE && pmd_none(*pmd)) { 156022061a1fSHugh Dickins spinlock_t *ptl = pmd_lock(tlb->mm, pmd); 156122061a1fSHugh Dickins /* 156222061a1fSHugh Dickins * Take and drop THP pmd lock so that we cannot return 156322061a1fSHugh Dickins * prematurely, while zap_huge_pmd() has cleared *pmd, 156422061a1fSHugh Dickins * but not yet decremented compound_mapcount(). 156522061a1fSHugh Dickins */ 156622061a1fSHugh Dickins spin_unlock(ptl); 156771e3aac0SAndrea Arcangeli } 15683db82b93SHugh Dickins if (pmd_none(*pmd)) { 15693db82b93SHugh Dickins addr = next; 15703db82b93SHugh Dickins continue; 15713db82b93SHugh Dickins } 15723db82b93SHugh Dickins addr = zap_pte_range(tlb, vma, pmd, addr, next, details); 15733db82b93SHugh Dickins if (addr != next) 15743db82b93SHugh Dickins pmd--; 15753db82b93SHugh Dickins } while (pmd++, cond_resched(), addr != end); 157651c6f666SRobin Holt 157751c6f666SRobin Holt return addr; 15781da177e4SLinus Torvalds } 15791da177e4SLinus Torvalds 158051c6f666SRobin Holt static inline unsigned long zap_pud_range(struct mmu_gather *tlb, 1581c2febafcSKirill A. Shutemov struct vm_area_struct *vma, p4d_t *p4d, 15821da177e4SLinus Torvalds unsigned long addr, unsigned long end, 158397a89413SPeter Zijlstra struct zap_details *details) 15841da177e4SLinus Torvalds { 15851da177e4SLinus Torvalds pud_t *pud; 15861da177e4SLinus Torvalds unsigned long next; 15871da177e4SLinus Torvalds 1588c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 15891da177e4SLinus Torvalds do { 15901da177e4SLinus Torvalds next = pud_addr_end(addr, end); 1591a00cc7d9SMatthew Wilcox if (pud_trans_huge(*pud) || pud_devmap(*pud)) { 1592a00cc7d9SMatthew Wilcox if (next - addr != HPAGE_PUD_SIZE) { 159342fc5414SMichel Lespinasse mmap_assert_locked(tlb->mm); 1594a00cc7d9SMatthew Wilcox split_huge_pud(vma, pud, addr); 1595a00cc7d9SMatthew Wilcox } else if (zap_huge_pud(tlb, vma, pud, addr)) 1596a00cc7d9SMatthew Wilcox goto next; 1597a00cc7d9SMatthew Wilcox /* fall through */ 1598a00cc7d9SMatthew Wilcox } 159997a89413SPeter Zijlstra if (pud_none_or_clear_bad(pud)) 16001da177e4SLinus Torvalds continue; 160197a89413SPeter Zijlstra next = zap_pmd_range(tlb, vma, pud, addr, next, details); 1602a00cc7d9SMatthew Wilcox next: 1603a00cc7d9SMatthew Wilcox cond_resched(); 160497a89413SPeter Zijlstra } while (pud++, addr = next, addr != end); 160551c6f666SRobin Holt 160651c6f666SRobin Holt return addr; 16071da177e4SLinus Torvalds } 16081da177e4SLinus Torvalds 1609c2febafcSKirill A. Shutemov static inline unsigned long zap_p4d_range(struct mmu_gather *tlb, 1610c2febafcSKirill A. Shutemov struct vm_area_struct *vma, pgd_t *pgd, 1611c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 1612c2febafcSKirill A. Shutemov struct zap_details *details) 1613c2febafcSKirill A. Shutemov { 1614c2febafcSKirill A. Shutemov p4d_t *p4d; 1615c2febafcSKirill A. Shutemov unsigned long next; 1616c2febafcSKirill A. Shutemov 1617c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 1618c2febafcSKirill A. Shutemov do { 1619c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 1620c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(p4d)) 1621c2febafcSKirill A. Shutemov continue; 1622c2febafcSKirill A. Shutemov next = zap_pud_range(tlb, vma, p4d, addr, next, details); 1623c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 1624c2febafcSKirill A. Shutemov 1625c2febafcSKirill A. Shutemov return addr; 1626c2febafcSKirill A. Shutemov } 1627c2febafcSKirill A. Shutemov 1628aac45363SMichal Hocko void unmap_page_range(struct mmu_gather *tlb, 162951c6f666SRobin Holt struct vm_area_struct *vma, 16301da177e4SLinus Torvalds unsigned long addr, unsigned long end, 163197a89413SPeter Zijlstra struct zap_details *details) 16321da177e4SLinus Torvalds { 16331da177e4SLinus Torvalds pgd_t *pgd; 16341da177e4SLinus Torvalds unsigned long next; 16351da177e4SLinus Torvalds 16361da177e4SLinus Torvalds BUG_ON(addr >= end); 16371da177e4SLinus Torvalds tlb_start_vma(tlb, vma); 16381da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 16391da177e4SLinus Torvalds do { 16401da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 164197a89413SPeter Zijlstra if (pgd_none_or_clear_bad(pgd)) 16421da177e4SLinus Torvalds continue; 1643c2febafcSKirill A. Shutemov next = zap_p4d_range(tlb, vma, pgd, addr, next, details); 164497a89413SPeter Zijlstra } while (pgd++, addr = next, addr != end); 16451da177e4SLinus Torvalds tlb_end_vma(tlb, vma); 16461da177e4SLinus Torvalds } 16471da177e4SLinus Torvalds 1648f5cc4eefSAl Viro 1649f5cc4eefSAl Viro static void unmap_single_vma(struct mmu_gather *tlb, 16501da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long start_addr, 16514f74d2c8SLinus Torvalds unsigned long end_addr, 165268f48381SSuren Baghdasaryan struct zap_details *details, bool mm_wr_locked) 16531da177e4SLinus Torvalds { 1654f5cc4eefSAl Viro unsigned long start = max(vma->vm_start, start_addr); 16551da177e4SLinus Torvalds unsigned long end; 16561da177e4SLinus Torvalds 16571da177e4SLinus Torvalds if (start >= vma->vm_end) 1658f5cc4eefSAl Viro return; 16591da177e4SLinus Torvalds end = min(vma->vm_end, end_addr); 16601da177e4SLinus Torvalds if (end <= vma->vm_start) 1661f5cc4eefSAl Viro return; 16621da177e4SLinus Torvalds 1663cbc91f71SSrikar Dronamraju if (vma->vm_file) 1664cbc91f71SSrikar Dronamraju uprobe_munmap(vma, start, end); 1665cbc91f71SSrikar Dronamraju 1666b3b9c293SKonstantin Khlebnikov if (unlikely(vma->vm_flags & VM_PFNMAP)) 166768f48381SSuren Baghdasaryan untrack_pfn(vma, 0, 0, mm_wr_locked); 16682ab64037Svenkatesh.pallipadi@intel.com 16698b2a1238SAl Viro if (start != end) { 167051c6f666SRobin Holt if (unlikely(is_vm_hugetlb_page(vma))) { 1671a137e1ccSAndi Kleen /* 1672a137e1ccSAndi Kleen * It is undesirable to test vma->vm_file as it 1673a137e1ccSAndi Kleen * should be non-null for valid hugetlb area. 1674a137e1ccSAndi Kleen * However, vm_file will be NULL in the error 16757aa6b4adSDavidlohr Bueso * cleanup path of mmap_region. When 1676a137e1ccSAndi Kleen * hugetlbfs ->mmap method fails, 16777aa6b4adSDavidlohr Bueso * mmap_region() nullifies vma->vm_file 1678a137e1ccSAndi Kleen * before calling this function to clean up. 1679a137e1ccSAndi Kleen * Since no pte has actually been setup, it is 1680a137e1ccSAndi Kleen * safe to do nothing in this case. 1681a137e1ccSAndi Kleen */ 168224669e58SAneesh Kumar K.V if (vma->vm_file) { 168305e90bd0SPeter Xu zap_flags_t zap_flags = details ? 168405e90bd0SPeter Xu details->zap_flags : 0; 168505e90bd0SPeter Xu __unmap_hugepage_range_final(tlb, vma, start, end, 168605e90bd0SPeter Xu NULL, zap_flags); 168724669e58SAneesh Kumar K.V } 168851c6f666SRobin Holt } else 1689038c7aa1SAl Viro unmap_page_range(tlb, vma, start, end, details); 169097a89413SPeter Zijlstra } 169151c6f666SRobin Holt } 16921da177e4SLinus Torvalds 1693f5cc4eefSAl Viro /** 1694f5cc4eefSAl Viro * unmap_vmas - unmap a range of memory covered by a list of vma's 1695f5cc4eefSAl Viro * @tlb: address of the caller's struct mmu_gather 16966e412203SYang Li * @mas: the maple state 1697f5cc4eefSAl Viro * @vma: the starting vma 1698f5cc4eefSAl Viro * @start_addr: virtual address at which to start unmapping 1699f5cc4eefSAl Viro * @end_addr: virtual address at which to end unmapping 17006e412203SYang Li * @tree_end: The maximum index to check 1701809ef83cSYang Li * @mm_wr_locked: lock flag 1702f5cc4eefSAl Viro * 1703f5cc4eefSAl Viro * Unmap all pages in the vma list. 1704f5cc4eefSAl Viro * 1705f5cc4eefSAl Viro * Only addresses between `start' and `end' will be unmapped. 1706f5cc4eefSAl Viro * 1707f5cc4eefSAl Viro * The VMA list must be sorted in ascending virtual address order. 1708f5cc4eefSAl Viro * 1709f5cc4eefSAl Viro * unmap_vmas() assumes that the caller will flush the whole unmapped address 1710f5cc4eefSAl Viro * range after unmap_vmas() returns. So the only responsibility here is to 1711f5cc4eefSAl Viro * ensure that any thus-far unmapped pages are flushed before unmap_vmas() 1712f5cc4eefSAl Viro * drops the lock and schedules. 1713f5cc4eefSAl Viro */ 1714fd892593SLiam R. Howlett void unmap_vmas(struct mmu_gather *tlb, struct ma_state *mas, 1715f5cc4eefSAl Viro struct vm_area_struct *vma, unsigned long start_addr, 1716fd892593SLiam R. Howlett unsigned long end_addr, unsigned long tree_end, 1717fd892593SLiam R. Howlett bool mm_wr_locked) 1718f5cc4eefSAl Viro { 1719ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 1720999dad82SPeter Xu struct zap_details details = { 172104ada095SMike Kravetz .zap_flags = ZAP_FLAG_DROP_MARKER | ZAP_FLAG_UNMAP, 1722999dad82SPeter Xu /* Careful - we need to zap private pages too! */ 1723999dad82SPeter Xu .even_cows = true, 1724999dad82SPeter Xu }; 1725f5cc4eefSAl Viro 17267d4a8be0SAlistair Popple mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma->vm_mm, 17276f4f13e8SJérôme Glisse start_addr, end_addr); 1728ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 1729763ecb03SLiam R. Howlett do { 173068f48381SSuren Baghdasaryan unmap_single_vma(tlb, vma, start_addr, end_addr, &details, 173168f48381SSuren Baghdasaryan mm_wr_locked); 1732fd892593SLiam R. Howlett } while ((vma = mas_find(mas, tree_end - 1)) != NULL); 1733ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 17341da177e4SLinus Torvalds } 17351da177e4SLinus Torvalds 17361da177e4SLinus Torvalds /** 1737f5cc4eefSAl Viro * zap_page_range_single - remove user pages in a given range 1738f5cc4eefSAl Viro * @vma: vm_area_struct holding the applicable pages 1739f5cc4eefSAl Viro * @address: starting address of pages to zap 1740f5cc4eefSAl Viro * @size: number of bytes to zap 17418a5f14a2SKirill A. Shutemov * @details: details of shared cache invalidation 1742f5cc4eefSAl Viro * 1743f5cc4eefSAl Viro * The range must fit into one VMA. 1744f5cc4eefSAl Viro */ 174521b85b09SMike Kravetz void zap_page_range_single(struct vm_area_struct *vma, unsigned long address, 1746f5cc4eefSAl Viro unsigned long size, struct zap_details *details) 1747f5cc4eefSAl Viro { 174821b85b09SMike Kravetz const unsigned long end = address + size; 1749ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 1750f5cc4eefSAl Viro struct mmu_gather tlb; 1751f5cc4eefSAl Viro 1752f5cc4eefSAl Viro lru_add_drain(); 17537d4a8be0SAlistair Popple mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma->vm_mm, 175421b85b09SMike Kravetz address, end); 175521b85b09SMike Kravetz if (is_vm_hugetlb_page(vma)) 175621b85b09SMike Kravetz adjust_range_if_pmd_sharing_possible(vma, &range.start, 175721b85b09SMike Kravetz &range.end); 1758a72afd87SWill Deacon tlb_gather_mmu(&tlb, vma->vm_mm); 1759ac46d4f3SJérôme Glisse update_hiwater_rss(vma->vm_mm); 1760ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 176121b85b09SMike Kravetz /* 176221b85b09SMike Kravetz * unmap 'address-end' not 'range.start-range.end' as range 176321b85b09SMike Kravetz * could have been expanded for hugetlb pmd sharing. 176421b85b09SMike Kravetz */ 176568f48381SSuren Baghdasaryan unmap_single_vma(&tlb, vma, address, end, details, false); 1766ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 1767ae8eba8bSWill Deacon tlb_finish_mmu(&tlb); 17681da177e4SLinus Torvalds } 17691da177e4SLinus Torvalds 1770c627f9ccSJack Steiner /** 1771c627f9ccSJack Steiner * zap_vma_ptes - remove ptes mapping the vma 1772c627f9ccSJack Steiner * @vma: vm_area_struct holding ptes to be zapped 1773c627f9ccSJack Steiner * @address: starting address of pages to zap 1774c627f9ccSJack Steiner * @size: number of bytes to zap 1775c627f9ccSJack Steiner * 1776c627f9ccSJack Steiner * This function only unmaps ptes assigned to VM_PFNMAP vmas. 1777c627f9ccSJack Steiner * 1778c627f9ccSJack Steiner * The entire address range must be fully contained within the vma. 1779c627f9ccSJack Steiner * 1780c627f9ccSJack Steiner */ 178127d036e3SLeon Romanovsky void zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, 1782c627f9ccSJack Steiner unsigned long size) 1783c627f9ccSJack Steiner { 178488a35912SMiaohe Lin if (!range_in_vma(vma, address, address + size) || 1785c627f9ccSJack Steiner !(vma->vm_flags & VM_PFNMAP)) 178627d036e3SLeon Romanovsky return; 178727d036e3SLeon Romanovsky 1788f5cc4eefSAl Viro zap_page_range_single(vma, address, size, NULL); 1789c627f9ccSJack Steiner } 1790c627f9ccSJack Steiner EXPORT_SYMBOL_GPL(zap_vma_ptes); 1791c627f9ccSJack Steiner 17928cd3984dSArjun Roy static pmd_t *walk_to_pmd(struct mm_struct *mm, unsigned long addr) 1793c9cfcddfSLinus Torvalds { 1794c2febafcSKirill A. Shutemov pgd_t *pgd; 1795c2febafcSKirill A. Shutemov p4d_t *p4d; 1796c2febafcSKirill A. Shutemov pud_t *pud; 1797c2febafcSKirill A. Shutemov pmd_t *pmd; 1798c2febafcSKirill A. Shutemov 1799c2febafcSKirill A. Shutemov pgd = pgd_offset(mm, addr); 1800c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, addr); 1801c2febafcSKirill A. Shutemov if (!p4d) 1802c2febafcSKirill A. Shutemov return NULL; 1803c2febafcSKirill A. Shutemov pud = pud_alloc(mm, p4d, addr); 1804c2febafcSKirill A. Shutemov if (!pud) 1805c2febafcSKirill A. Shutemov return NULL; 1806c2febafcSKirill A. Shutemov pmd = pmd_alloc(mm, pud, addr); 1807c2febafcSKirill A. Shutemov if (!pmd) 1808c2febafcSKirill A. Shutemov return NULL; 1809c2febafcSKirill A. Shutemov 1810f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 18118cd3984dSArjun Roy return pmd; 18128cd3984dSArjun Roy } 18138cd3984dSArjun Roy 18148cd3984dSArjun Roy pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr, 18158cd3984dSArjun Roy spinlock_t **ptl) 18168cd3984dSArjun Roy { 18178cd3984dSArjun Roy pmd_t *pmd = walk_to_pmd(mm, addr); 18188cd3984dSArjun Roy 18198cd3984dSArjun Roy if (!pmd) 18208cd3984dSArjun Roy return NULL; 1821c9cfcddfSLinus Torvalds return pte_alloc_map_lock(mm, pmd, addr, ptl); 1822c9cfcddfSLinus Torvalds } 1823c9cfcddfSLinus Torvalds 18248efd6f5bSArjun Roy static int validate_page_before_insert(struct page *page) 18258efd6f5bSArjun Roy { 18268efd6f5bSArjun Roy if (PageAnon(page) || PageSlab(page) || page_has_type(page)) 18278efd6f5bSArjun Roy return -EINVAL; 18288efd6f5bSArjun Roy flush_dcache_page(page); 18298efd6f5bSArjun Roy return 0; 18308efd6f5bSArjun Roy } 18318efd6f5bSArjun Roy 1832cea86fe2SHugh Dickins static int insert_page_into_pte_locked(struct vm_area_struct *vma, pte_t *pte, 18338efd6f5bSArjun Roy unsigned long addr, struct page *page, pgprot_t prot) 18348efd6f5bSArjun Roy { 1835c33c7948SRyan Roberts if (!pte_none(ptep_get(pte))) 18368efd6f5bSArjun Roy return -EBUSY; 18378efd6f5bSArjun Roy /* Ok, finally just insert the thing.. */ 18388efd6f5bSArjun Roy get_page(page); 1839f1a79412SShakeel Butt inc_mm_counter(vma->vm_mm, mm_counter_file(page)); 1840cea86fe2SHugh Dickins page_add_file_rmap(page, vma, false); 1841cea86fe2SHugh Dickins set_pte_at(vma->vm_mm, addr, pte, mk_pte(page, prot)); 18428efd6f5bSArjun Roy return 0; 18438efd6f5bSArjun Roy } 18448efd6f5bSArjun Roy 18451da177e4SLinus Torvalds /* 1846238f58d8SLinus Torvalds * This is the old fallback for page remapping. 1847238f58d8SLinus Torvalds * 1848238f58d8SLinus Torvalds * For historical reasons, it only allows reserved pages. Only 1849238f58d8SLinus Torvalds * old drivers should use this, and they needed to mark their 1850238f58d8SLinus Torvalds * pages reserved for the old functions anyway. 1851238f58d8SLinus Torvalds */ 1852423bad60SNick Piggin static int insert_page(struct vm_area_struct *vma, unsigned long addr, 1853423bad60SNick Piggin struct page *page, pgprot_t prot) 1854238f58d8SLinus Torvalds { 1855238f58d8SLinus Torvalds int retval; 1856238f58d8SLinus Torvalds pte_t *pte; 1857238f58d8SLinus Torvalds spinlock_t *ptl; 1858238f58d8SLinus Torvalds 18598efd6f5bSArjun Roy retval = validate_page_before_insert(page); 18608efd6f5bSArjun Roy if (retval) 18615b4e655eSKAMEZAWA Hiroyuki goto out; 1862238f58d8SLinus Torvalds retval = -ENOMEM; 1863cea86fe2SHugh Dickins pte = get_locked_pte(vma->vm_mm, addr, &ptl); 1864238f58d8SLinus Torvalds if (!pte) 18655b4e655eSKAMEZAWA Hiroyuki goto out; 1866cea86fe2SHugh Dickins retval = insert_page_into_pte_locked(vma, pte, addr, page, prot); 1867238f58d8SLinus Torvalds pte_unmap_unlock(pte, ptl); 1868238f58d8SLinus Torvalds out: 1869238f58d8SLinus Torvalds return retval; 1870238f58d8SLinus Torvalds } 1871238f58d8SLinus Torvalds 1872cea86fe2SHugh Dickins static int insert_page_in_batch_locked(struct vm_area_struct *vma, pte_t *pte, 18738cd3984dSArjun Roy unsigned long addr, struct page *page, pgprot_t prot) 18748cd3984dSArjun Roy { 18758cd3984dSArjun Roy int err; 18768cd3984dSArjun Roy 18778cd3984dSArjun Roy if (!page_count(page)) 18788cd3984dSArjun Roy return -EINVAL; 18798cd3984dSArjun Roy err = validate_page_before_insert(page); 18807f70c2a6SArjun Roy if (err) 18817f70c2a6SArjun Roy return err; 1882cea86fe2SHugh Dickins return insert_page_into_pte_locked(vma, pte, addr, page, prot); 18838cd3984dSArjun Roy } 18848cd3984dSArjun Roy 18858cd3984dSArjun Roy /* insert_pages() amortizes the cost of spinlock operations 1886*bb7dbaafSMatthew Wilcox (Oracle) * when inserting pages in a loop. 18878cd3984dSArjun Roy */ 18888cd3984dSArjun Roy static int insert_pages(struct vm_area_struct *vma, unsigned long addr, 18898cd3984dSArjun Roy struct page **pages, unsigned long *num, pgprot_t prot) 18908cd3984dSArjun Roy { 18918cd3984dSArjun Roy pmd_t *pmd = NULL; 18927f70c2a6SArjun Roy pte_t *start_pte, *pte; 18937f70c2a6SArjun Roy spinlock_t *pte_lock; 18948cd3984dSArjun Roy struct mm_struct *const mm = vma->vm_mm; 18958cd3984dSArjun Roy unsigned long curr_page_idx = 0; 18968cd3984dSArjun Roy unsigned long remaining_pages_total = *num; 18978cd3984dSArjun Roy unsigned long pages_to_write_in_pmd; 18988cd3984dSArjun Roy int ret; 18998cd3984dSArjun Roy more: 19008cd3984dSArjun Roy ret = -EFAULT; 19018cd3984dSArjun Roy pmd = walk_to_pmd(mm, addr); 19028cd3984dSArjun Roy if (!pmd) 19038cd3984dSArjun Roy goto out; 19048cd3984dSArjun Roy 19058cd3984dSArjun Roy pages_to_write_in_pmd = min_t(unsigned long, 19068cd3984dSArjun Roy remaining_pages_total, PTRS_PER_PTE - pte_index(addr)); 19078cd3984dSArjun Roy 19088cd3984dSArjun Roy /* Allocate the PTE if necessary; takes PMD lock once only. */ 19098cd3984dSArjun Roy ret = -ENOMEM; 19108cd3984dSArjun Roy if (pte_alloc(mm, pmd)) 19118cd3984dSArjun Roy goto out; 19128cd3984dSArjun Roy 19138cd3984dSArjun Roy while (pages_to_write_in_pmd) { 19148cd3984dSArjun Roy int pte_idx = 0; 19158cd3984dSArjun Roy const int batch_size = min_t(int, pages_to_write_in_pmd, 8); 19168cd3984dSArjun Roy 19177f70c2a6SArjun Roy start_pte = pte_offset_map_lock(mm, pmd, addr, &pte_lock); 19183db82b93SHugh Dickins if (!start_pte) { 19193db82b93SHugh Dickins ret = -EFAULT; 19203db82b93SHugh Dickins goto out; 19213db82b93SHugh Dickins } 19227f70c2a6SArjun Roy for (pte = start_pte; pte_idx < batch_size; ++pte, ++pte_idx) { 1923cea86fe2SHugh Dickins int err = insert_page_in_batch_locked(vma, pte, 19248cd3984dSArjun Roy addr, pages[curr_page_idx], prot); 19258cd3984dSArjun Roy if (unlikely(err)) { 19267f70c2a6SArjun Roy pte_unmap_unlock(start_pte, pte_lock); 19278cd3984dSArjun Roy ret = err; 19288cd3984dSArjun Roy remaining_pages_total -= pte_idx; 19298cd3984dSArjun Roy goto out; 19308cd3984dSArjun Roy } 19318cd3984dSArjun Roy addr += PAGE_SIZE; 19328cd3984dSArjun Roy ++curr_page_idx; 19338cd3984dSArjun Roy } 19347f70c2a6SArjun Roy pte_unmap_unlock(start_pte, pte_lock); 19358cd3984dSArjun Roy pages_to_write_in_pmd -= batch_size; 19368cd3984dSArjun Roy remaining_pages_total -= batch_size; 19378cd3984dSArjun Roy } 19388cd3984dSArjun Roy if (remaining_pages_total) 19398cd3984dSArjun Roy goto more; 19408cd3984dSArjun Roy ret = 0; 19418cd3984dSArjun Roy out: 19428cd3984dSArjun Roy *num = remaining_pages_total; 19438cd3984dSArjun Roy return ret; 19448cd3984dSArjun Roy } 19458cd3984dSArjun Roy 19468cd3984dSArjun Roy /** 19478cd3984dSArjun Roy * vm_insert_pages - insert multiple pages into user vma, batching the pmd lock. 19488cd3984dSArjun Roy * @vma: user vma to map to 19498cd3984dSArjun Roy * @addr: target start user address of these pages 19508cd3984dSArjun Roy * @pages: source kernel pages 19518cd3984dSArjun Roy * @num: in: number of pages to map. out: number of pages that were *not* 19528cd3984dSArjun Roy * mapped. (0 means all pages were successfully mapped). 19538cd3984dSArjun Roy * 19548cd3984dSArjun Roy * Preferred over vm_insert_page() when inserting multiple pages. 19558cd3984dSArjun Roy * 19568cd3984dSArjun Roy * In case of error, we may have mapped a subset of the provided 19578cd3984dSArjun Roy * pages. It is the caller's responsibility to account for this case. 19588cd3984dSArjun Roy * 19598cd3984dSArjun Roy * The same restrictions apply as in vm_insert_page(). 19608cd3984dSArjun Roy */ 19618cd3984dSArjun Roy int vm_insert_pages(struct vm_area_struct *vma, unsigned long addr, 19628cd3984dSArjun Roy struct page **pages, unsigned long *num) 19638cd3984dSArjun Roy { 19648cd3984dSArjun Roy const unsigned long end_addr = addr + (*num * PAGE_SIZE) - 1; 19658cd3984dSArjun Roy 19668cd3984dSArjun Roy if (addr < vma->vm_start || end_addr >= vma->vm_end) 19678cd3984dSArjun Roy return -EFAULT; 19688cd3984dSArjun Roy if (!(vma->vm_flags & VM_MIXEDMAP)) { 1969d8ed45c5SMichel Lespinasse BUG_ON(mmap_read_trylock(vma->vm_mm)); 19708cd3984dSArjun Roy BUG_ON(vma->vm_flags & VM_PFNMAP); 19711c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_MIXEDMAP); 19728cd3984dSArjun Roy } 19738cd3984dSArjun Roy /* Defer page refcount checking till we're about to map that page. */ 19748cd3984dSArjun Roy return insert_pages(vma, addr, pages, num, vma->vm_page_prot); 19758cd3984dSArjun Roy } 19768cd3984dSArjun Roy EXPORT_SYMBOL(vm_insert_pages); 19778cd3984dSArjun Roy 1978bfa5bf6dSRolf Eike Beer /** 1979bfa5bf6dSRolf Eike Beer * vm_insert_page - insert single page into user vma 1980bfa5bf6dSRolf Eike Beer * @vma: user vma to map to 1981bfa5bf6dSRolf Eike Beer * @addr: target user address of this page 1982bfa5bf6dSRolf Eike Beer * @page: source kernel page 1983bfa5bf6dSRolf Eike Beer * 1984a145dd41SLinus Torvalds * This allows drivers to insert individual pages they've allocated 1985a145dd41SLinus Torvalds * into a user vma. 1986a145dd41SLinus Torvalds * 1987a145dd41SLinus Torvalds * The page has to be a nice clean _individual_ kernel allocation. 1988a145dd41SLinus Torvalds * If you allocate a compound page, you need to have marked it as 1989a145dd41SLinus Torvalds * such (__GFP_COMP), or manually just split the page up yourself 19908dfcc9baSNick Piggin * (see split_page()). 1991a145dd41SLinus Torvalds * 1992a145dd41SLinus Torvalds * NOTE! Traditionally this was done with "remap_pfn_range()" which 1993a145dd41SLinus Torvalds * took an arbitrary page protection parameter. This doesn't allow 1994a145dd41SLinus Torvalds * that. Your vma protection will have to be set up correctly, which 1995a145dd41SLinus Torvalds * means that if you want a shared writable mapping, you'd better 1996a145dd41SLinus Torvalds * ask for a shared writable mapping! 1997a145dd41SLinus Torvalds * 1998a145dd41SLinus Torvalds * The page does not need to be reserved. 19994b6e1e37SKonstantin Khlebnikov * 20004b6e1e37SKonstantin Khlebnikov * Usually this function is called from f_op->mmap() handler 2001c1e8d7c6SMichel Lespinasse * under mm->mmap_lock write-lock, so it can change vma->vm_flags. 20024b6e1e37SKonstantin Khlebnikov * Caller must set VM_MIXEDMAP on vma if it wants to call this 20034b6e1e37SKonstantin Khlebnikov * function from other places, for example from page-fault handler. 2004a862f68aSMike Rapoport * 2005a862f68aSMike Rapoport * Return: %0 on success, negative error code otherwise. 2006a145dd41SLinus Torvalds */ 2007423bad60SNick Piggin int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, 2008423bad60SNick Piggin struct page *page) 2009a145dd41SLinus Torvalds { 2010a145dd41SLinus Torvalds if (addr < vma->vm_start || addr >= vma->vm_end) 2011a145dd41SLinus Torvalds return -EFAULT; 2012a145dd41SLinus Torvalds if (!page_count(page)) 2013a145dd41SLinus Torvalds return -EINVAL; 20144b6e1e37SKonstantin Khlebnikov if (!(vma->vm_flags & VM_MIXEDMAP)) { 2015d8ed45c5SMichel Lespinasse BUG_ON(mmap_read_trylock(vma->vm_mm)); 20164b6e1e37SKonstantin Khlebnikov BUG_ON(vma->vm_flags & VM_PFNMAP); 20171c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_MIXEDMAP); 20184b6e1e37SKonstantin Khlebnikov } 2019423bad60SNick Piggin return insert_page(vma, addr, page, vma->vm_page_prot); 2020a145dd41SLinus Torvalds } 2021e3c3374fSLinus Torvalds EXPORT_SYMBOL(vm_insert_page); 2022a145dd41SLinus Torvalds 2023a667d745SSouptick Joarder /* 2024a667d745SSouptick Joarder * __vm_map_pages - maps range of kernel pages into user vma 2025a667d745SSouptick Joarder * @vma: user vma to map to 2026a667d745SSouptick Joarder * @pages: pointer to array of source kernel pages 2027a667d745SSouptick Joarder * @num: number of pages in page array 2028a667d745SSouptick Joarder * @offset: user's requested vm_pgoff 2029a667d745SSouptick Joarder * 2030a667d745SSouptick Joarder * This allows drivers to map range of kernel pages into a user vma. 2031a667d745SSouptick Joarder * 2032a667d745SSouptick Joarder * Return: 0 on success and error code otherwise. 2033a667d745SSouptick Joarder */ 2034a667d745SSouptick Joarder static int __vm_map_pages(struct vm_area_struct *vma, struct page **pages, 2035a667d745SSouptick Joarder unsigned long num, unsigned long offset) 2036a667d745SSouptick Joarder { 2037a667d745SSouptick Joarder unsigned long count = vma_pages(vma); 2038a667d745SSouptick Joarder unsigned long uaddr = vma->vm_start; 2039a667d745SSouptick Joarder int ret, i; 2040a667d745SSouptick Joarder 2041a667d745SSouptick Joarder /* Fail if the user requested offset is beyond the end of the object */ 204296756fcbSMiguel Ojeda if (offset >= num) 2043a667d745SSouptick Joarder return -ENXIO; 2044a667d745SSouptick Joarder 2045a667d745SSouptick Joarder /* Fail if the user requested size exceeds available object size */ 2046a667d745SSouptick Joarder if (count > num - offset) 2047a667d745SSouptick Joarder return -ENXIO; 2048a667d745SSouptick Joarder 2049a667d745SSouptick Joarder for (i = 0; i < count; i++) { 2050a667d745SSouptick Joarder ret = vm_insert_page(vma, uaddr, pages[offset + i]); 2051a667d745SSouptick Joarder if (ret < 0) 2052a667d745SSouptick Joarder return ret; 2053a667d745SSouptick Joarder uaddr += PAGE_SIZE; 2054a667d745SSouptick Joarder } 2055a667d745SSouptick Joarder 2056a667d745SSouptick Joarder return 0; 2057a667d745SSouptick Joarder } 2058a667d745SSouptick Joarder 2059a667d745SSouptick Joarder /** 2060a667d745SSouptick Joarder * vm_map_pages - maps range of kernel pages starts with non zero offset 2061a667d745SSouptick Joarder * @vma: user vma to map to 2062a667d745SSouptick Joarder * @pages: pointer to array of source kernel pages 2063a667d745SSouptick Joarder * @num: number of pages in page array 2064a667d745SSouptick Joarder * 2065a667d745SSouptick Joarder * Maps an object consisting of @num pages, catering for the user's 2066a667d745SSouptick Joarder * requested vm_pgoff 2067a667d745SSouptick Joarder * 2068a667d745SSouptick Joarder * If we fail to insert any page into the vma, the function will return 2069a667d745SSouptick Joarder * immediately leaving any previously inserted pages present. Callers 2070a667d745SSouptick Joarder * from the mmap handler may immediately return the error as their caller 2071a667d745SSouptick Joarder * will destroy the vma, removing any successfully inserted pages. Other 2072a667d745SSouptick Joarder * callers should make their own arrangements for calling unmap_region(). 2073a667d745SSouptick Joarder * 2074a667d745SSouptick Joarder * Context: Process context. Called by mmap handlers. 2075a667d745SSouptick Joarder * Return: 0 on success and error code otherwise. 2076a667d745SSouptick Joarder */ 2077a667d745SSouptick Joarder int vm_map_pages(struct vm_area_struct *vma, struct page **pages, 2078a667d745SSouptick Joarder unsigned long num) 2079a667d745SSouptick Joarder { 2080a667d745SSouptick Joarder return __vm_map_pages(vma, pages, num, vma->vm_pgoff); 2081a667d745SSouptick Joarder } 2082a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages); 2083a667d745SSouptick Joarder 2084a667d745SSouptick Joarder /** 2085a667d745SSouptick Joarder * vm_map_pages_zero - map range of kernel pages starts with zero offset 2086a667d745SSouptick Joarder * @vma: user vma to map to 2087a667d745SSouptick Joarder * @pages: pointer to array of source kernel pages 2088a667d745SSouptick Joarder * @num: number of pages in page array 2089a667d745SSouptick Joarder * 2090a667d745SSouptick Joarder * Similar to vm_map_pages(), except that it explicitly sets the offset 2091a667d745SSouptick Joarder * to 0. This function is intended for the drivers that did not consider 2092a667d745SSouptick Joarder * vm_pgoff. 2093a667d745SSouptick Joarder * 2094a667d745SSouptick Joarder * Context: Process context. Called by mmap handlers. 2095a667d745SSouptick Joarder * Return: 0 on success and error code otherwise. 2096a667d745SSouptick Joarder */ 2097a667d745SSouptick Joarder int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages, 2098a667d745SSouptick Joarder unsigned long num) 2099a667d745SSouptick Joarder { 2100a667d745SSouptick Joarder return __vm_map_pages(vma, pages, num, 0); 2101a667d745SSouptick Joarder } 2102a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages_zero); 2103a667d745SSouptick Joarder 21049b5a8e00SMatthew Wilcox static vm_fault_t insert_pfn(struct vm_area_struct *vma, unsigned long addr, 2105b2770da6SRoss Zwisler pfn_t pfn, pgprot_t prot, bool mkwrite) 2106423bad60SNick Piggin { 2107423bad60SNick Piggin struct mm_struct *mm = vma->vm_mm; 2108423bad60SNick Piggin pte_t *pte, entry; 2109423bad60SNick Piggin spinlock_t *ptl; 2110423bad60SNick Piggin 2111423bad60SNick Piggin pte = get_locked_pte(mm, addr, &ptl); 2112423bad60SNick Piggin if (!pte) 21139b5a8e00SMatthew Wilcox return VM_FAULT_OOM; 2114c33c7948SRyan Roberts entry = ptep_get(pte); 2115c33c7948SRyan Roberts if (!pte_none(entry)) { 2116b2770da6SRoss Zwisler if (mkwrite) { 2117b2770da6SRoss Zwisler /* 2118b2770da6SRoss Zwisler * For read faults on private mappings the PFN passed 2119b2770da6SRoss Zwisler * in may not match the PFN we have mapped if the 2120b2770da6SRoss Zwisler * mapped PFN is a writeable COW page. In the mkwrite 2121b2770da6SRoss Zwisler * case we are creating a writable PTE for a shared 2122f2c57d91SJan Kara * mapping and we expect the PFNs to match. If they 2123f2c57d91SJan Kara * don't match, we are likely racing with block 2124f2c57d91SJan Kara * allocation and mapping invalidation so just skip the 2125f2c57d91SJan Kara * update. 2126b2770da6SRoss Zwisler */ 2127c33c7948SRyan Roberts if (pte_pfn(entry) != pfn_t_to_pfn(pfn)) { 2128c33c7948SRyan Roberts WARN_ON_ONCE(!is_zero_pfn(pte_pfn(entry))); 2129423bad60SNick Piggin goto out_unlock; 2130f2c57d91SJan Kara } 2131c33c7948SRyan Roberts entry = pte_mkyoung(entry); 2132cae85cb8SJan Kara entry = maybe_mkwrite(pte_mkdirty(entry), vma); 2133cae85cb8SJan Kara if (ptep_set_access_flags(vma, addr, pte, entry, 1)) 2134cae85cb8SJan Kara update_mmu_cache(vma, addr, pte); 2135cae85cb8SJan Kara } 2136b2770da6SRoss Zwisler goto out_unlock; 2137b2770da6SRoss Zwisler } 2138423bad60SNick Piggin 2139423bad60SNick Piggin /* Ok, finally just insert the thing.. */ 214001c8f1c4SDan Williams if (pfn_t_devmap(pfn)) 214101c8f1c4SDan Williams entry = pte_mkdevmap(pfn_t_pte(pfn, prot)); 214201c8f1c4SDan Williams else 214301c8f1c4SDan Williams entry = pte_mkspecial(pfn_t_pte(pfn, prot)); 2144b2770da6SRoss Zwisler 2145b2770da6SRoss Zwisler if (mkwrite) { 2146b2770da6SRoss Zwisler entry = pte_mkyoung(entry); 2147b2770da6SRoss Zwisler entry = maybe_mkwrite(pte_mkdirty(entry), vma); 2148b2770da6SRoss Zwisler } 2149b2770da6SRoss Zwisler 2150423bad60SNick Piggin set_pte_at(mm, addr, pte, entry); 21514b3073e1SRussell King update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */ 2152423bad60SNick Piggin 2153423bad60SNick Piggin out_unlock: 2154423bad60SNick Piggin pte_unmap_unlock(pte, ptl); 21559b5a8e00SMatthew Wilcox return VM_FAULT_NOPAGE; 2156423bad60SNick Piggin } 2157423bad60SNick Piggin 2158f5e6d1d5SMatthew Wilcox /** 2159f5e6d1d5SMatthew Wilcox * vmf_insert_pfn_prot - insert single pfn into user vma with specified pgprot 2160f5e6d1d5SMatthew Wilcox * @vma: user vma to map to 2161f5e6d1d5SMatthew Wilcox * @addr: target user address of this page 2162f5e6d1d5SMatthew Wilcox * @pfn: source kernel pfn 2163f5e6d1d5SMatthew Wilcox * @pgprot: pgprot flags for the inserted page 2164f5e6d1d5SMatthew Wilcox * 2165a1a0aea5SRandy Dunlap * This is exactly like vmf_insert_pfn(), except that it allows drivers 2166f5e6d1d5SMatthew Wilcox * to override pgprot on a per-page basis. 2167f5e6d1d5SMatthew Wilcox * 2168f5e6d1d5SMatthew Wilcox * This only makes sense for IO mappings, and it makes no sense for 2169f5e6d1d5SMatthew Wilcox * COW mappings. In general, using multiple vmas is preferable; 2170ae2b01f3SMatthew Wilcox * vmf_insert_pfn_prot should only be used if using multiple VMAs is 2171f5e6d1d5SMatthew Wilcox * impractical. 2172f5e6d1d5SMatthew Wilcox * 217328d8b812SLorenzo Stoakes * pgprot typically only differs from @vma->vm_page_prot when drivers set 217428d8b812SLorenzo Stoakes * caching- and encryption bits different than those of @vma->vm_page_prot, 217528d8b812SLorenzo Stoakes * because the caching- or encryption mode may not be known at mmap() time. 217628d8b812SLorenzo Stoakes * 217728d8b812SLorenzo Stoakes * This is ok as long as @vma->vm_page_prot is not used by the core vm 217828d8b812SLorenzo Stoakes * to set caching and encryption bits for those vmas (except for COW pages). 217928d8b812SLorenzo Stoakes * This is ensured by core vm only modifying these page table entries using 218028d8b812SLorenzo Stoakes * functions that don't touch caching- or encryption bits, using pte_modify() 218128d8b812SLorenzo Stoakes * if needed. (See for example mprotect()). 218228d8b812SLorenzo Stoakes * 218328d8b812SLorenzo Stoakes * Also when new page-table entries are created, this is only done using the 218428d8b812SLorenzo Stoakes * fault() callback, and never using the value of vma->vm_page_prot, 218528d8b812SLorenzo Stoakes * except for page-table entries that point to anonymous pages as the result 218628d8b812SLorenzo Stoakes * of COW. 2187574c5b3dSThomas Hellstrom * 2188ae2b01f3SMatthew Wilcox * Context: Process context. May allocate using %GFP_KERNEL. 2189f5e6d1d5SMatthew Wilcox * Return: vm_fault_t value. 2190f5e6d1d5SMatthew Wilcox */ 2191f5e6d1d5SMatthew Wilcox vm_fault_t vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr, 2192f5e6d1d5SMatthew Wilcox unsigned long pfn, pgprot_t pgprot) 2193f5e6d1d5SMatthew Wilcox { 21946d958546SMatthew Wilcox /* 21956d958546SMatthew Wilcox * Technically, architectures with pte_special can avoid all these 21966d958546SMatthew Wilcox * restrictions (same for remap_pfn_range). However we would like 21976d958546SMatthew Wilcox * consistency in testing and feature parity among all, so we should 21986d958546SMatthew Wilcox * try to keep these invariants in place for everybody. 21996d958546SMatthew Wilcox */ 22006d958546SMatthew Wilcox BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))); 22016d958546SMatthew Wilcox BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) == 22026d958546SMatthew Wilcox (VM_PFNMAP|VM_MIXEDMAP)); 22036d958546SMatthew Wilcox BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); 22046d958546SMatthew Wilcox BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn)); 22056d958546SMatthew Wilcox 22066d958546SMatthew Wilcox if (addr < vma->vm_start || addr >= vma->vm_end) 22076d958546SMatthew Wilcox return VM_FAULT_SIGBUS; 22086d958546SMatthew Wilcox 22096d958546SMatthew Wilcox if (!pfn_modify_allowed(pfn, pgprot)) 22106d958546SMatthew Wilcox return VM_FAULT_SIGBUS; 22116d958546SMatthew Wilcox 22126d958546SMatthew Wilcox track_pfn_insert(vma, &pgprot, __pfn_to_pfn_t(pfn, PFN_DEV)); 22136d958546SMatthew Wilcox 22149b5a8e00SMatthew Wilcox return insert_pfn(vma, addr, __pfn_to_pfn_t(pfn, PFN_DEV), pgprot, 22156d958546SMatthew Wilcox false); 2216f5e6d1d5SMatthew Wilcox } 2217f5e6d1d5SMatthew Wilcox EXPORT_SYMBOL(vmf_insert_pfn_prot); 2218e0dc0d8fSNick Piggin 2219ae2b01f3SMatthew Wilcox /** 2220ae2b01f3SMatthew Wilcox * vmf_insert_pfn - insert single pfn into user vma 2221ae2b01f3SMatthew Wilcox * @vma: user vma to map to 2222ae2b01f3SMatthew Wilcox * @addr: target user address of this page 2223ae2b01f3SMatthew Wilcox * @pfn: source kernel pfn 2224ae2b01f3SMatthew Wilcox * 2225ae2b01f3SMatthew Wilcox * Similar to vm_insert_page, this allows drivers to insert individual pages 2226ae2b01f3SMatthew Wilcox * they've allocated into a user vma. Same comments apply. 2227ae2b01f3SMatthew Wilcox * 2228ae2b01f3SMatthew Wilcox * This function should only be called from a vm_ops->fault handler, and 2229ae2b01f3SMatthew Wilcox * in that case the handler should return the result of this function. 2230ae2b01f3SMatthew Wilcox * 2231ae2b01f3SMatthew Wilcox * vma cannot be a COW mapping. 2232ae2b01f3SMatthew Wilcox * 2233ae2b01f3SMatthew Wilcox * As this is called only for pages that do not currently exist, we 2234ae2b01f3SMatthew Wilcox * do not need to flush old virtual caches or the TLB. 2235ae2b01f3SMatthew Wilcox * 2236ae2b01f3SMatthew Wilcox * Context: Process context. May allocate using %GFP_KERNEL. 2237ae2b01f3SMatthew Wilcox * Return: vm_fault_t value. 2238ae2b01f3SMatthew Wilcox */ 2239ae2b01f3SMatthew Wilcox vm_fault_t vmf_insert_pfn(struct vm_area_struct *vma, unsigned long addr, 2240ae2b01f3SMatthew Wilcox unsigned long pfn) 2241ae2b01f3SMatthew Wilcox { 2242ae2b01f3SMatthew Wilcox return vmf_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot); 2243ae2b01f3SMatthew Wilcox } 2244ae2b01f3SMatthew Wilcox EXPORT_SYMBOL(vmf_insert_pfn); 2245ae2b01f3SMatthew Wilcox 2246785a3fabSDan Williams static bool vm_mixed_ok(struct vm_area_struct *vma, pfn_t pfn) 2247785a3fabSDan Williams { 2248785a3fabSDan Williams /* these checks mirror the abort conditions in vm_normal_page */ 2249785a3fabSDan Williams if (vma->vm_flags & VM_MIXEDMAP) 2250785a3fabSDan Williams return true; 2251785a3fabSDan Williams if (pfn_t_devmap(pfn)) 2252785a3fabSDan Williams return true; 2253785a3fabSDan Williams if (pfn_t_special(pfn)) 2254785a3fabSDan Williams return true; 2255785a3fabSDan Williams if (is_zero_pfn(pfn_t_to_pfn(pfn))) 2256785a3fabSDan Williams return true; 2257785a3fabSDan Williams return false; 2258785a3fabSDan Williams } 2259785a3fabSDan Williams 226079f3aa5bSMatthew Wilcox static vm_fault_t __vm_insert_mixed(struct vm_area_struct *vma, 226128d8b812SLorenzo Stoakes unsigned long addr, pfn_t pfn, bool mkwrite) 2262423bad60SNick Piggin { 226328d8b812SLorenzo Stoakes pgprot_t pgprot = vma->vm_page_prot; 226479f3aa5bSMatthew Wilcox int err; 226587744ab3SDan Williams 2266785a3fabSDan Williams BUG_ON(!vm_mixed_ok(vma, pfn)); 2267423bad60SNick Piggin 2268423bad60SNick Piggin if (addr < vma->vm_start || addr >= vma->vm_end) 226979f3aa5bSMatthew Wilcox return VM_FAULT_SIGBUS; 2270308a047cSBorislav Petkov 2271308a047cSBorislav Petkov track_pfn_insert(vma, &pgprot, pfn); 2272423bad60SNick Piggin 227342e4089cSAndi Kleen if (!pfn_modify_allowed(pfn_t_to_pfn(pfn), pgprot)) 227479f3aa5bSMatthew Wilcox return VM_FAULT_SIGBUS; 227542e4089cSAndi Kleen 2276423bad60SNick Piggin /* 2277423bad60SNick Piggin * If we don't have pte special, then we have to use the pfn_valid() 2278423bad60SNick Piggin * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must* 2279423bad60SNick Piggin * refcount the page if pfn_valid is true (hence insert_page rather 228062eede62SHugh Dickins * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP 228162eede62SHugh Dickins * without pte special, it would there be refcounted as a normal page. 2282423bad60SNick Piggin */ 228300b3a331SLaurent Dufour if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) && 228400b3a331SLaurent Dufour !pfn_t_devmap(pfn) && pfn_t_valid(pfn)) { 2285423bad60SNick Piggin struct page *page; 2286423bad60SNick Piggin 228703fc2da6SDan Williams /* 228803fc2da6SDan Williams * At this point we are committed to insert_page() 228903fc2da6SDan Williams * regardless of whether the caller specified flags that 229003fc2da6SDan Williams * result in pfn_t_has_page() == false. 229103fc2da6SDan Williams */ 229203fc2da6SDan Williams page = pfn_to_page(pfn_t_to_pfn(pfn)); 229379f3aa5bSMatthew Wilcox err = insert_page(vma, addr, page, pgprot); 229479f3aa5bSMatthew Wilcox } else { 22959b5a8e00SMatthew Wilcox return insert_pfn(vma, addr, pfn, pgprot, mkwrite); 2296423bad60SNick Piggin } 2297b2770da6SRoss Zwisler 22985d747637SMatthew Wilcox if (err == -ENOMEM) 22995d747637SMatthew Wilcox return VM_FAULT_OOM; 23005d747637SMatthew Wilcox if (err < 0 && err != -EBUSY) 23015d747637SMatthew Wilcox return VM_FAULT_SIGBUS; 23025d747637SMatthew Wilcox 23035d747637SMatthew Wilcox return VM_FAULT_NOPAGE; 2304423bad60SNick Piggin } 230579f3aa5bSMatthew Wilcox 230679f3aa5bSMatthew Wilcox vm_fault_t vmf_insert_mixed(struct vm_area_struct *vma, unsigned long addr, 230779f3aa5bSMatthew Wilcox pfn_t pfn) 230879f3aa5bSMatthew Wilcox { 230928d8b812SLorenzo Stoakes return __vm_insert_mixed(vma, addr, pfn, false); 231079f3aa5bSMatthew Wilcox } 23115d747637SMatthew Wilcox EXPORT_SYMBOL(vmf_insert_mixed); 2312423bad60SNick Piggin 2313ab77dab4SSouptick Joarder /* 2314ab77dab4SSouptick Joarder * If the insertion of PTE failed because someone else already added a 2315ab77dab4SSouptick Joarder * different entry in the mean time, we treat that as success as we assume 2316ab77dab4SSouptick Joarder * the same entry was actually inserted. 2317ab77dab4SSouptick Joarder */ 2318ab77dab4SSouptick Joarder vm_fault_t vmf_insert_mixed_mkwrite(struct vm_area_struct *vma, 2319ab77dab4SSouptick Joarder unsigned long addr, pfn_t pfn) 2320b2770da6SRoss Zwisler { 232128d8b812SLorenzo Stoakes return __vm_insert_mixed(vma, addr, pfn, true); 2322b2770da6SRoss Zwisler } 2323ab77dab4SSouptick Joarder EXPORT_SYMBOL(vmf_insert_mixed_mkwrite); 2324b2770da6SRoss Zwisler 2325a145dd41SLinus Torvalds /* 23261da177e4SLinus Torvalds * maps a range of physical memory into the requested pages. the old 23271da177e4SLinus Torvalds * mappings are removed. any references to nonexistent pages results 23281da177e4SLinus Torvalds * in null mappings (currently treated as "copy-on-access") 23291da177e4SLinus Torvalds */ 23301da177e4SLinus Torvalds static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd, 23311da177e4SLinus Torvalds unsigned long addr, unsigned long end, 23321da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 23331da177e4SLinus Torvalds { 233490a3e375SMiaohe Lin pte_t *pte, *mapped_pte; 2335c74df32cSHugh Dickins spinlock_t *ptl; 233642e4089cSAndi Kleen int err = 0; 23371da177e4SLinus Torvalds 233890a3e375SMiaohe Lin mapped_pte = pte = pte_alloc_map_lock(mm, pmd, addr, &ptl); 23391da177e4SLinus Torvalds if (!pte) 23401da177e4SLinus Torvalds return -ENOMEM; 23416606c3e0SZachary Amsden arch_enter_lazy_mmu_mode(); 23421da177e4SLinus Torvalds do { 2343c33c7948SRyan Roberts BUG_ON(!pte_none(ptep_get(pte))); 234442e4089cSAndi Kleen if (!pfn_modify_allowed(pfn, prot)) { 234542e4089cSAndi Kleen err = -EACCES; 234642e4089cSAndi Kleen break; 234742e4089cSAndi Kleen } 23487e675137SNick Piggin set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot))); 23491da177e4SLinus Torvalds pfn++; 23501da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 23516606c3e0SZachary Amsden arch_leave_lazy_mmu_mode(); 235290a3e375SMiaohe Lin pte_unmap_unlock(mapped_pte, ptl); 235342e4089cSAndi Kleen return err; 23541da177e4SLinus Torvalds } 23551da177e4SLinus Torvalds 23561da177e4SLinus Torvalds static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud, 23571da177e4SLinus Torvalds unsigned long addr, unsigned long end, 23581da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 23591da177e4SLinus Torvalds { 23601da177e4SLinus Torvalds pmd_t *pmd; 23611da177e4SLinus Torvalds unsigned long next; 236242e4089cSAndi Kleen int err; 23631da177e4SLinus Torvalds 23641da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 23651da177e4SLinus Torvalds pmd = pmd_alloc(mm, pud, addr); 23661da177e4SLinus Torvalds if (!pmd) 23671da177e4SLinus Torvalds return -ENOMEM; 2368f66055abSAndrea Arcangeli VM_BUG_ON(pmd_trans_huge(*pmd)); 23691da177e4SLinus Torvalds do { 23701da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 237142e4089cSAndi Kleen err = remap_pte_range(mm, pmd, addr, next, 237242e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 237342e4089cSAndi Kleen if (err) 237442e4089cSAndi Kleen return err; 23751da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 23761da177e4SLinus Torvalds return 0; 23771da177e4SLinus Torvalds } 23781da177e4SLinus Torvalds 2379c2febafcSKirill A. Shutemov static inline int remap_pud_range(struct mm_struct *mm, p4d_t *p4d, 23801da177e4SLinus Torvalds unsigned long addr, unsigned long end, 23811da177e4SLinus Torvalds unsigned long pfn, pgprot_t prot) 23821da177e4SLinus Torvalds { 23831da177e4SLinus Torvalds pud_t *pud; 23841da177e4SLinus Torvalds unsigned long next; 238542e4089cSAndi Kleen int err; 23861da177e4SLinus Torvalds 23871da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 2388c2febafcSKirill A. Shutemov pud = pud_alloc(mm, p4d, addr); 23891da177e4SLinus Torvalds if (!pud) 23901da177e4SLinus Torvalds return -ENOMEM; 23911da177e4SLinus Torvalds do { 23921da177e4SLinus Torvalds next = pud_addr_end(addr, end); 239342e4089cSAndi Kleen err = remap_pmd_range(mm, pud, addr, next, 239442e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 239542e4089cSAndi Kleen if (err) 239642e4089cSAndi Kleen return err; 23971da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 23981da177e4SLinus Torvalds return 0; 23991da177e4SLinus Torvalds } 24001da177e4SLinus Torvalds 2401c2febafcSKirill A. Shutemov static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd, 2402c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 2403c2febafcSKirill A. Shutemov unsigned long pfn, pgprot_t prot) 2404c2febafcSKirill A. Shutemov { 2405c2febafcSKirill A. Shutemov p4d_t *p4d; 2406c2febafcSKirill A. Shutemov unsigned long next; 240742e4089cSAndi Kleen int err; 2408c2febafcSKirill A. Shutemov 2409c2febafcSKirill A. Shutemov pfn -= addr >> PAGE_SHIFT; 2410c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, addr); 2411c2febafcSKirill A. Shutemov if (!p4d) 2412c2febafcSKirill A. Shutemov return -ENOMEM; 2413c2febafcSKirill A. Shutemov do { 2414c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 241542e4089cSAndi Kleen err = remap_pud_range(mm, p4d, addr, next, 241642e4089cSAndi Kleen pfn + (addr >> PAGE_SHIFT), prot); 241742e4089cSAndi Kleen if (err) 241842e4089cSAndi Kleen return err; 2419c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 2420c2febafcSKirill A. Shutemov return 0; 2421c2febafcSKirill A. Shutemov } 2422c2febafcSKirill A. Shutemov 242374ffa5a3SChristoph Hellwig /* 242474ffa5a3SChristoph Hellwig * Variant of remap_pfn_range that does not call track_pfn_remap. The caller 242574ffa5a3SChristoph Hellwig * must have pre-validated the caching bits of the pgprot_t. 2426bfa5bf6dSRolf Eike Beer */ 242774ffa5a3SChristoph Hellwig int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr, 24281da177e4SLinus Torvalds unsigned long pfn, unsigned long size, pgprot_t prot) 24291da177e4SLinus Torvalds { 24301da177e4SLinus Torvalds pgd_t *pgd; 24311da177e4SLinus Torvalds unsigned long next; 24322d15cab8SHugh Dickins unsigned long end = addr + PAGE_ALIGN(size); 24331da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 24341da177e4SLinus Torvalds int err; 24351da177e4SLinus Torvalds 24360c4123e3SAlex Zhang if (WARN_ON_ONCE(!PAGE_ALIGNED(addr))) 24370c4123e3SAlex Zhang return -EINVAL; 24380c4123e3SAlex Zhang 24391da177e4SLinus Torvalds /* 24401da177e4SLinus Torvalds * Physically remapped pages are special. Tell the 24411da177e4SLinus Torvalds * rest of the world about it: 24421da177e4SLinus Torvalds * VM_IO tells people not to look at these pages 24431da177e4SLinus Torvalds * (accesses can have side effects). 24446aab341eSLinus Torvalds * VM_PFNMAP tells the core MM that the base pages are just 24456aab341eSLinus Torvalds * raw PFN mappings, and do not have a "struct page" associated 24466aab341eSLinus Torvalds * with them. 2447314e51b9SKonstantin Khlebnikov * VM_DONTEXPAND 2448314e51b9SKonstantin Khlebnikov * Disable vma merging and expanding with mremap(). 2449314e51b9SKonstantin Khlebnikov * VM_DONTDUMP 2450314e51b9SKonstantin Khlebnikov * Omit vma from core dump, even when VM_IO turned off. 2451fb155c16SLinus Torvalds * 2452fb155c16SLinus Torvalds * There's a horrible special case to handle copy-on-write 2453fb155c16SLinus Torvalds * behaviour that some programs depend on. We mark the "original" 2454fb155c16SLinus Torvalds * un-COW'ed pages by matching them up with "vma->vm_pgoff". 2455b3b9c293SKonstantin Khlebnikov * See vm_normal_page() for details. 24561da177e4SLinus Torvalds */ 2457b3b9c293SKonstantin Khlebnikov if (is_cow_mapping(vma->vm_flags)) { 2458b3b9c293SKonstantin Khlebnikov if (addr != vma->vm_start || end != vma->vm_end) 2459b3b9c293SKonstantin Khlebnikov return -EINVAL; 24606aab341eSLinus Torvalds vma->vm_pgoff = pfn; 2461b3b9c293SKonstantin Khlebnikov } 2462b3b9c293SKonstantin Khlebnikov 24631c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP); 24641da177e4SLinus Torvalds 24651da177e4SLinus Torvalds BUG_ON(addr >= end); 24661da177e4SLinus Torvalds pfn -= addr >> PAGE_SHIFT; 24671da177e4SLinus Torvalds pgd = pgd_offset(mm, addr); 24681da177e4SLinus Torvalds flush_cache_range(vma, addr, end); 24691da177e4SLinus Torvalds do { 24701da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 2471c2febafcSKirill A. Shutemov err = remap_p4d_range(mm, pgd, addr, next, 24721da177e4SLinus Torvalds pfn + (addr >> PAGE_SHIFT), prot); 24731da177e4SLinus Torvalds if (err) 247474ffa5a3SChristoph Hellwig return err; 24751da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 24762ab64037Svenkatesh.pallipadi@intel.com 247774ffa5a3SChristoph Hellwig return 0; 247874ffa5a3SChristoph Hellwig } 24792ab64037Svenkatesh.pallipadi@intel.com 248074ffa5a3SChristoph Hellwig /** 248174ffa5a3SChristoph Hellwig * remap_pfn_range - remap kernel memory to userspace 248274ffa5a3SChristoph Hellwig * @vma: user vma to map to 248374ffa5a3SChristoph Hellwig * @addr: target page aligned user address to start at 248474ffa5a3SChristoph Hellwig * @pfn: page frame number of kernel physical memory address 248574ffa5a3SChristoph Hellwig * @size: size of mapping area 248674ffa5a3SChristoph Hellwig * @prot: page protection flags for this mapping 248774ffa5a3SChristoph Hellwig * 248874ffa5a3SChristoph Hellwig * Note: this is only safe if the mm semaphore is held when called. 248974ffa5a3SChristoph Hellwig * 249074ffa5a3SChristoph Hellwig * Return: %0 on success, negative error code otherwise. 249174ffa5a3SChristoph Hellwig */ 249274ffa5a3SChristoph Hellwig int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 249374ffa5a3SChristoph Hellwig unsigned long pfn, unsigned long size, pgprot_t prot) 249474ffa5a3SChristoph Hellwig { 249574ffa5a3SChristoph Hellwig int err; 249674ffa5a3SChristoph Hellwig 249774ffa5a3SChristoph Hellwig err = track_pfn_remap(vma, &prot, pfn, addr, PAGE_ALIGN(size)); 249874ffa5a3SChristoph Hellwig if (err) 249974ffa5a3SChristoph Hellwig return -EINVAL; 250074ffa5a3SChristoph Hellwig 250174ffa5a3SChristoph Hellwig err = remap_pfn_range_notrack(vma, addr, pfn, size, prot); 250274ffa5a3SChristoph Hellwig if (err) 250368f48381SSuren Baghdasaryan untrack_pfn(vma, pfn, PAGE_ALIGN(size), true); 25041da177e4SLinus Torvalds return err; 25051da177e4SLinus Torvalds } 25061da177e4SLinus Torvalds EXPORT_SYMBOL(remap_pfn_range); 25071da177e4SLinus Torvalds 2508b4cbb197SLinus Torvalds /** 2509b4cbb197SLinus Torvalds * vm_iomap_memory - remap memory to userspace 2510b4cbb197SLinus Torvalds * @vma: user vma to map to 2511abd69b9eSWang Wenhu * @start: start of the physical memory to be mapped 2512b4cbb197SLinus Torvalds * @len: size of area 2513b4cbb197SLinus Torvalds * 2514b4cbb197SLinus Torvalds * This is a simplified io_remap_pfn_range() for common driver use. The 2515b4cbb197SLinus Torvalds * driver just needs to give us the physical memory range to be mapped, 2516b4cbb197SLinus Torvalds * we'll figure out the rest from the vma information. 2517b4cbb197SLinus Torvalds * 2518b4cbb197SLinus Torvalds * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get 2519b4cbb197SLinus Torvalds * whatever write-combining details or similar. 2520a862f68aSMike Rapoport * 2521a862f68aSMike Rapoport * Return: %0 on success, negative error code otherwise. 2522b4cbb197SLinus Torvalds */ 2523b4cbb197SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len) 2524b4cbb197SLinus Torvalds { 2525b4cbb197SLinus Torvalds unsigned long vm_len, pfn, pages; 2526b4cbb197SLinus Torvalds 2527b4cbb197SLinus Torvalds /* Check that the physical memory area passed in looks valid */ 2528b4cbb197SLinus Torvalds if (start + len < start) 2529b4cbb197SLinus Torvalds return -EINVAL; 2530b4cbb197SLinus Torvalds /* 2531b4cbb197SLinus Torvalds * You *really* shouldn't map things that aren't page-aligned, 2532b4cbb197SLinus Torvalds * but we've historically allowed it because IO memory might 2533b4cbb197SLinus Torvalds * just have smaller alignment. 2534b4cbb197SLinus Torvalds */ 2535b4cbb197SLinus Torvalds len += start & ~PAGE_MASK; 2536b4cbb197SLinus Torvalds pfn = start >> PAGE_SHIFT; 2537b4cbb197SLinus Torvalds pages = (len + ~PAGE_MASK) >> PAGE_SHIFT; 2538b4cbb197SLinus Torvalds if (pfn + pages < pfn) 2539b4cbb197SLinus Torvalds return -EINVAL; 2540b4cbb197SLinus Torvalds 2541b4cbb197SLinus Torvalds /* We start the mapping 'vm_pgoff' pages into the area */ 2542b4cbb197SLinus Torvalds if (vma->vm_pgoff > pages) 2543b4cbb197SLinus Torvalds return -EINVAL; 2544b4cbb197SLinus Torvalds pfn += vma->vm_pgoff; 2545b4cbb197SLinus Torvalds pages -= vma->vm_pgoff; 2546b4cbb197SLinus Torvalds 2547b4cbb197SLinus Torvalds /* Can we fit all of the mapping? */ 2548b4cbb197SLinus Torvalds vm_len = vma->vm_end - vma->vm_start; 2549b4cbb197SLinus Torvalds if (vm_len >> PAGE_SHIFT > pages) 2550b4cbb197SLinus Torvalds return -EINVAL; 2551b4cbb197SLinus Torvalds 2552b4cbb197SLinus Torvalds /* Ok, let it rip */ 2553b4cbb197SLinus Torvalds return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot); 2554b4cbb197SLinus Torvalds } 2555b4cbb197SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory); 2556b4cbb197SLinus Torvalds 2557aee16b3cSJeremy Fitzhardinge static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd, 2558aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2559e80d3909SJoerg Roedel pte_fn_t fn, void *data, bool create, 2560e80d3909SJoerg Roedel pgtbl_mod_mask *mask) 2561aee16b3cSJeremy Fitzhardinge { 25628abb50c7SMiaohe Lin pte_t *pte, *mapped_pte; 2563be1db475SDaniel Axtens int err = 0; 25643f649ab7SKees Cook spinlock_t *ptl; 2565aee16b3cSJeremy Fitzhardinge 2566be1db475SDaniel Axtens if (create) { 25678abb50c7SMiaohe Lin mapped_pte = pte = (mm == &init_mm) ? 2568e80d3909SJoerg Roedel pte_alloc_kernel_track(pmd, addr, mask) : 2569aee16b3cSJeremy Fitzhardinge pte_alloc_map_lock(mm, pmd, addr, &ptl); 2570aee16b3cSJeremy Fitzhardinge if (!pte) 2571aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2572be1db475SDaniel Axtens } else { 25738abb50c7SMiaohe Lin mapped_pte = pte = (mm == &init_mm) ? 2574be1db475SDaniel Axtens pte_offset_kernel(pmd, addr) : 2575be1db475SDaniel Axtens pte_offset_map_lock(mm, pmd, addr, &ptl); 25763db82b93SHugh Dickins if (!pte) 25773db82b93SHugh Dickins return -EINVAL; 2578be1db475SDaniel Axtens } 2579aee16b3cSJeremy Fitzhardinge 258038e0edb1SJeremy Fitzhardinge arch_enter_lazy_mmu_mode(); 258138e0edb1SJeremy Fitzhardinge 2582eeb4a05fSChristoph Hellwig if (fn) { 2583aee16b3cSJeremy Fitzhardinge do { 2584c33c7948SRyan Roberts if (create || !pte_none(ptep_get(pte))) { 25858b1e0f81SAnshuman Khandual err = fn(pte++, addr, data); 2586aee16b3cSJeremy Fitzhardinge if (err) 2587aee16b3cSJeremy Fitzhardinge break; 2588be1db475SDaniel Axtens } 2589c36987e2SDaisuke Nishimura } while (addr += PAGE_SIZE, addr != end); 2590eeb4a05fSChristoph Hellwig } 2591e80d3909SJoerg Roedel *mask |= PGTBL_PTE_MODIFIED; 2592aee16b3cSJeremy Fitzhardinge 259338e0edb1SJeremy Fitzhardinge arch_leave_lazy_mmu_mode(); 259438e0edb1SJeremy Fitzhardinge 2595aee16b3cSJeremy Fitzhardinge if (mm != &init_mm) 25968abb50c7SMiaohe Lin pte_unmap_unlock(mapped_pte, ptl); 2597aee16b3cSJeremy Fitzhardinge return err; 2598aee16b3cSJeremy Fitzhardinge } 2599aee16b3cSJeremy Fitzhardinge 2600aee16b3cSJeremy Fitzhardinge static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud, 2601aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2602e80d3909SJoerg Roedel pte_fn_t fn, void *data, bool create, 2603e80d3909SJoerg Roedel pgtbl_mod_mask *mask) 2604aee16b3cSJeremy Fitzhardinge { 2605aee16b3cSJeremy Fitzhardinge pmd_t *pmd; 2606aee16b3cSJeremy Fitzhardinge unsigned long next; 2607be1db475SDaniel Axtens int err = 0; 2608aee16b3cSJeremy Fitzhardinge 2609ceb86879SAndi Kleen BUG_ON(pud_huge(*pud)); 2610ceb86879SAndi Kleen 2611be1db475SDaniel Axtens if (create) { 2612e80d3909SJoerg Roedel pmd = pmd_alloc_track(mm, pud, addr, mask); 2613aee16b3cSJeremy Fitzhardinge if (!pmd) 2614aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2615be1db475SDaniel Axtens } else { 2616be1db475SDaniel Axtens pmd = pmd_offset(pud, addr); 2617be1db475SDaniel Axtens } 2618aee16b3cSJeremy Fitzhardinge do { 2619aee16b3cSJeremy Fitzhardinge next = pmd_addr_end(addr, end); 26200c95cba4SNicholas Piggin if (pmd_none(*pmd) && !create) 26210c95cba4SNicholas Piggin continue; 26220c95cba4SNicholas Piggin if (WARN_ON_ONCE(pmd_leaf(*pmd))) 26230c95cba4SNicholas Piggin return -EINVAL; 26240c95cba4SNicholas Piggin if (!pmd_none(*pmd) && WARN_ON_ONCE(pmd_bad(*pmd))) { 26250c95cba4SNicholas Piggin if (!create) 26260c95cba4SNicholas Piggin continue; 26270c95cba4SNicholas Piggin pmd_clear_bad(pmd); 26280c95cba4SNicholas Piggin } 26290c95cba4SNicholas Piggin err = apply_to_pte_range(mm, pmd, addr, next, 26300c95cba4SNicholas Piggin fn, data, create, mask); 2631aee16b3cSJeremy Fitzhardinge if (err) 2632aee16b3cSJeremy Fitzhardinge break; 2633aee16b3cSJeremy Fitzhardinge } while (pmd++, addr = next, addr != end); 26340c95cba4SNicholas Piggin 2635aee16b3cSJeremy Fitzhardinge return err; 2636aee16b3cSJeremy Fitzhardinge } 2637aee16b3cSJeremy Fitzhardinge 2638c2febafcSKirill A. Shutemov static int apply_to_pud_range(struct mm_struct *mm, p4d_t *p4d, 2639aee16b3cSJeremy Fitzhardinge unsigned long addr, unsigned long end, 2640e80d3909SJoerg Roedel pte_fn_t fn, void *data, bool create, 2641e80d3909SJoerg Roedel pgtbl_mod_mask *mask) 2642aee16b3cSJeremy Fitzhardinge { 2643aee16b3cSJeremy Fitzhardinge pud_t *pud; 2644aee16b3cSJeremy Fitzhardinge unsigned long next; 2645be1db475SDaniel Axtens int err = 0; 2646aee16b3cSJeremy Fitzhardinge 2647be1db475SDaniel Axtens if (create) { 2648e80d3909SJoerg Roedel pud = pud_alloc_track(mm, p4d, addr, mask); 2649aee16b3cSJeremy Fitzhardinge if (!pud) 2650aee16b3cSJeremy Fitzhardinge return -ENOMEM; 2651be1db475SDaniel Axtens } else { 2652be1db475SDaniel Axtens pud = pud_offset(p4d, addr); 2653be1db475SDaniel Axtens } 2654aee16b3cSJeremy Fitzhardinge do { 2655aee16b3cSJeremy Fitzhardinge next = pud_addr_end(addr, end); 26560c95cba4SNicholas Piggin if (pud_none(*pud) && !create) 26570c95cba4SNicholas Piggin continue; 26580c95cba4SNicholas Piggin if (WARN_ON_ONCE(pud_leaf(*pud))) 26590c95cba4SNicholas Piggin return -EINVAL; 26600c95cba4SNicholas Piggin if (!pud_none(*pud) && WARN_ON_ONCE(pud_bad(*pud))) { 26610c95cba4SNicholas Piggin if (!create) 26620c95cba4SNicholas Piggin continue; 26630c95cba4SNicholas Piggin pud_clear_bad(pud); 26640c95cba4SNicholas Piggin } 26650c95cba4SNicholas Piggin err = apply_to_pmd_range(mm, pud, addr, next, 26660c95cba4SNicholas Piggin fn, data, create, mask); 2667aee16b3cSJeremy Fitzhardinge if (err) 2668aee16b3cSJeremy Fitzhardinge break; 2669aee16b3cSJeremy Fitzhardinge } while (pud++, addr = next, addr != end); 26700c95cba4SNicholas Piggin 2671aee16b3cSJeremy Fitzhardinge return err; 2672aee16b3cSJeremy Fitzhardinge } 2673aee16b3cSJeremy Fitzhardinge 2674c2febafcSKirill A. Shutemov static int apply_to_p4d_range(struct mm_struct *mm, pgd_t *pgd, 2675c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 2676e80d3909SJoerg Roedel pte_fn_t fn, void *data, bool create, 2677e80d3909SJoerg Roedel pgtbl_mod_mask *mask) 2678c2febafcSKirill A. Shutemov { 2679c2febafcSKirill A. Shutemov p4d_t *p4d; 2680c2febafcSKirill A. Shutemov unsigned long next; 2681be1db475SDaniel Axtens int err = 0; 2682c2febafcSKirill A. Shutemov 2683be1db475SDaniel Axtens if (create) { 2684e80d3909SJoerg Roedel p4d = p4d_alloc_track(mm, pgd, addr, mask); 2685c2febafcSKirill A. Shutemov if (!p4d) 2686c2febafcSKirill A. Shutemov return -ENOMEM; 2687be1db475SDaniel Axtens } else { 2688be1db475SDaniel Axtens p4d = p4d_offset(pgd, addr); 2689be1db475SDaniel Axtens } 2690c2febafcSKirill A. Shutemov do { 2691c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 26920c95cba4SNicholas Piggin if (p4d_none(*p4d) && !create) 26930c95cba4SNicholas Piggin continue; 26940c95cba4SNicholas Piggin if (WARN_ON_ONCE(p4d_leaf(*p4d))) 26950c95cba4SNicholas Piggin return -EINVAL; 26960c95cba4SNicholas Piggin if (!p4d_none(*p4d) && WARN_ON_ONCE(p4d_bad(*p4d))) { 26970c95cba4SNicholas Piggin if (!create) 26980c95cba4SNicholas Piggin continue; 26990c95cba4SNicholas Piggin p4d_clear_bad(p4d); 27000c95cba4SNicholas Piggin } 27010c95cba4SNicholas Piggin err = apply_to_pud_range(mm, p4d, addr, next, 27020c95cba4SNicholas Piggin fn, data, create, mask); 2703c2febafcSKirill A. Shutemov if (err) 2704c2febafcSKirill A. Shutemov break; 2705c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 27060c95cba4SNicholas Piggin 2707c2febafcSKirill A. Shutemov return err; 2708c2febafcSKirill A. Shutemov } 2709c2febafcSKirill A. Shutemov 2710be1db475SDaniel Axtens static int __apply_to_page_range(struct mm_struct *mm, unsigned long addr, 2711be1db475SDaniel Axtens unsigned long size, pte_fn_t fn, 2712be1db475SDaniel Axtens void *data, bool create) 2713be1db475SDaniel Axtens { 2714be1db475SDaniel Axtens pgd_t *pgd; 2715e80d3909SJoerg Roedel unsigned long start = addr, next; 2716be1db475SDaniel Axtens unsigned long end = addr + size; 2717e80d3909SJoerg Roedel pgtbl_mod_mask mask = 0; 2718be1db475SDaniel Axtens int err = 0; 2719be1db475SDaniel Axtens 2720be1db475SDaniel Axtens if (WARN_ON(addr >= end)) 2721be1db475SDaniel Axtens return -EINVAL; 2722be1db475SDaniel Axtens 2723be1db475SDaniel Axtens pgd = pgd_offset(mm, addr); 2724be1db475SDaniel Axtens do { 2725be1db475SDaniel Axtens next = pgd_addr_end(addr, end); 27260c95cba4SNicholas Piggin if (pgd_none(*pgd) && !create) 2727be1db475SDaniel Axtens continue; 27280c95cba4SNicholas Piggin if (WARN_ON_ONCE(pgd_leaf(*pgd))) 27290c95cba4SNicholas Piggin return -EINVAL; 27300c95cba4SNicholas Piggin if (!pgd_none(*pgd) && WARN_ON_ONCE(pgd_bad(*pgd))) { 27310c95cba4SNicholas Piggin if (!create) 27320c95cba4SNicholas Piggin continue; 27330c95cba4SNicholas Piggin pgd_clear_bad(pgd); 27340c95cba4SNicholas Piggin } 27350c95cba4SNicholas Piggin err = apply_to_p4d_range(mm, pgd, addr, next, 27360c95cba4SNicholas Piggin fn, data, create, &mask); 2737be1db475SDaniel Axtens if (err) 2738be1db475SDaniel Axtens break; 2739be1db475SDaniel Axtens } while (pgd++, addr = next, addr != end); 2740be1db475SDaniel Axtens 2741e80d3909SJoerg Roedel if (mask & ARCH_PAGE_TABLE_SYNC_MASK) 2742e80d3909SJoerg Roedel arch_sync_kernel_mappings(start, start + size); 2743e80d3909SJoerg Roedel 2744be1db475SDaniel Axtens return err; 2745be1db475SDaniel Axtens } 2746be1db475SDaniel Axtens 2747aee16b3cSJeremy Fitzhardinge /* 2748aee16b3cSJeremy Fitzhardinge * Scan a region of virtual memory, filling in page tables as necessary 2749aee16b3cSJeremy Fitzhardinge * and calling a provided function on each leaf page table. 2750aee16b3cSJeremy Fitzhardinge */ 2751aee16b3cSJeremy Fitzhardinge int apply_to_page_range(struct mm_struct *mm, unsigned long addr, 2752aee16b3cSJeremy Fitzhardinge unsigned long size, pte_fn_t fn, void *data) 2753aee16b3cSJeremy Fitzhardinge { 2754be1db475SDaniel Axtens return __apply_to_page_range(mm, addr, size, fn, data, true); 2755aee16b3cSJeremy Fitzhardinge } 2756aee16b3cSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(apply_to_page_range); 2757aee16b3cSJeremy Fitzhardinge 27581da177e4SLinus Torvalds /* 2759be1db475SDaniel Axtens * Scan a region of virtual memory, calling a provided function on 2760be1db475SDaniel Axtens * each leaf page table where it exists. 2761be1db475SDaniel Axtens * 2762be1db475SDaniel Axtens * Unlike apply_to_page_range, this does _not_ fill in page tables 2763be1db475SDaniel Axtens * where they are absent. 2764be1db475SDaniel Axtens */ 2765be1db475SDaniel Axtens int apply_to_existing_page_range(struct mm_struct *mm, unsigned long addr, 2766be1db475SDaniel Axtens unsigned long size, pte_fn_t fn, void *data) 2767be1db475SDaniel Axtens { 2768be1db475SDaniel Axtens return __apply_to_page_range(mm, addr, size, fn, data, false); 2769be1db475SDaniel Axtens } 2770be1db475SDaniel Axtens EXPORT_SYMBOL_GPL(apply_to_existing_page_range); 2771be1db475SDaniel Axtens 2772be1db475SDaniel Axtens /* 27739b4bdd2fSKirill A. Shutemov * handle_pte_fault chooses page fault handler according to an entry which was 27749b4bdd2fSKirill A. Shutemov * read non-atomically. Before making any commitment, on those architectures 27759b4bdd2fSKirill A. Shutemov * or configurations (e.g. i386 with PAE) which might give a mix of unmatched 27769b4bdd2fSKirill A. Shutemov * parts, do_swap_page must check under lock before unmapping the pte and 27779b4bdd2fSKirill A. Shutemov * proceeding (but do_wp_page is only called after already making such a check; 2778a335b2e1SRyota Ozaki * and do_anonymous_page can safely check later on). 27798f4e2101SHugh Dickins */ 27802ca99358SPeter Xu static inline int pte_unmap_same(struct vm_fault *vmf) 27818f4e2101SHugh Dickins { 27828f4e2101SHugh Dickins int same = 1; 2783923717cbSThomas Gleixner #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPTION) 27848f4e2101SHugh Dickins if (sizeof(pte_t) > sizeof(unsigned long)) { 2785c7ad0880SHugh Dickins spin_lock(vmf->ptl); 2786c33c7948SRyan Roberts same = pte_same(ptep_get(vmf->pte), vmf->orig_pte); 2787c7ad0880SHugh Dickins spin_unlock(vmf->ptl); 27888f4e2101SHugh Dickins } 27898f4e2101SHugh Dickins #endif 27902ca99358SPeter Xu pte_unmap(vmf->pte); 27912ca99358SPeter Xu vmf->pte = NULL; 27928f4e2101SHugh Dickins return same; 27938f4e2101SHugh Dickins } 27948f4e2101SHugh Dickins 2795a873dfe1STony Luck /* 2796a873dfe1STony Luck * Return: 2797a873dfe1STony Luck * 0: copied succeeded 2798a873dfe1STony Luck * -EHWPOISON: copy failed due to hwpoison in source page 2799a873dfe1STony Luck * -EAGAIN: copied failed (some other reason) 2800a873dfe1STony Luck */ 2801a873dfe1STony Luck static inline int __wp_page_copy_user(struct page *dst, struct page *src, 280283d116c5SJia He struct vm_fault *vmf) 28036aab341eSLinus Torvalds { 2804a873dfe1STony Luck int ret; 280583d116c5SJia He void *kaddr; 280683d116c5SJia He void __user *uaddr; 280783d116c5SJia He struct vm_area_struct *vma = vmf->vma; 280883d116c5SJia He struct mm_struct *mm = vma->vm_mm; 280983d116c5SJia He unsigned long addr = vmf->address; 281083d116c5SJia He 281183d116c5SJia He if (likely(src)) { 2812d302c239STony Luck if (copy_mc_user_highpage(dst, src, addr, vma)) { 2813d302c239STony Luck memory_failure_queue(page_to_pfn(src), 0); 2814a873dfe1STony Luck return -EHWPOISON; 2815d302c239STony Luck } 2816a873dfe1STony Luck return 0; 281783d116c5SJia He } 281883d116c5SJia He 28196aab341eSLinus Torvalds /* 28206aab341eSLinus Torvalds * If the source page was a PFN mapping, we don't have 28216aab341eSLinus Torvalds * a "struct page" for it. We do a best-effort copy by 28226aab341eSLinus Torvalds * just copying from the original user address. If that 28236aab341eSLinus Torvalds * fails, we just zero-fill it. Live with it. 28246aab341eSLinus Torvalds */ 282583d116c5SJia He kaddr = kmap_atomic(dst); 282683d116c5SJia He uaddr = (void __user *)(addr & PAGE_MASK); 282783d116c5SJia He 282883d116c5SJia He /* 282983d116c5SJia He * On architectures with software "accessed" bits, we would 283083d116c5SJia He * take a double page fault, so mark it accessed here. 283183d116c5SJia He */ 28323db82b93SHugh Dickins vmf->pte = NULL; 2833e1fd09e3SYu Zhao if (!arch_has_hw_pte_young() && !pte_young(vmf->orig_pte)) { 283483d116c5SJia He pte_t entry; 283583d116c5SJia He 283683d116c5SJia He vmf->pte = pte_offset_map_lock(mm, vmf->pmd, addr, &vmf->ptl); 2837c33c7948SRyan Roberts if (unlikely(!vmf->pte || !pte_same(ptep_get(vmf->pte), vmf->orig_pte))) { 283883d116c5SJia He /* 283983d116c5SJia He * Other thread has already handled the fault 28407df67697SBibo Mao * and update local tlb only 284183d116c5SJia He */ 2842a92cbb82SHugh Dickins if (vmf->pte) 28437df67697SBibo Mao update_mmu_tlb(vma, addr, vmf->pte); 2844a873dfe1STony Luck ret = -EAGAIN; 284583d116c5SJia He goto pte_unlock; 284683d116c5SJia He } 284783d116c5SJia He 284883d116c5SJia He entry = pte_mkyoung(vmf->orig_pte); 284983d116c5SJia He if (ptep_set_access_flags(vma, addr, vmf->pte, entry, 0)) 28505003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vma, addr, vmf->pte, 1); 285183d116c5SJia He } 28525d2a2dbbSLinus Torvalds 28535d2a2dbbSLinus Torvalds /* 28545d2a2dbbSLinus Torvalds * This really shouldn't fail, because the page is there 28555d2a2dbbSLinus Torvalds * in the page tables. But it might just be unreadable, 28565d2a2dbbSLinus Torvalds * in which case we just give up and fill the result with 28575d2a2dbbSLinus Torvalds * zeroes. 28585d2a2dbbSLinus Torvalds */ 285983d116c5SJia He if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) { 28603db82b93SHugh Dickins if (vmf->pte) 2861c3e5ea6eSKirill A. Shutemov goto warn; 2862c3e5ea6eSKirill A. Shutemov 2863c3e5ea6eSKirill A. Shutemov /* Re-validate under PTL if the page is still mapped */ 2864c3e5ea6eSKirill A. Shutemov vmf->pte = pte_offset_map_lock(mm, vmf->pmd, addr, &vmf->ptl); 2865c33c7948SRyan Roberts if (unlikely(!vmf->pte || !pte_same(ptep_get(vmf->pte), vmf->orig_pte))) { 28667df67697SBibo Mao /* The PTE changed under us, update local tlb */ 2867a92cbb82SHugh Dickins if (vmf->pte) 28687df67697SBibo Mao update_mmu_tlb(vma, addr, vmf->pte); 2869a873dfe1STony Luck ret = -EAGAIN; 2870c3e5ea6eSKirill A. Shutemov goto pte_unlock; 2871c3e5ea6eSKirill A. Shutemov } 2872c3e5ea6eSKirill A. Shutemov 2873c3e5ea6eSKirill A. Shutemov /* 2874985ba004SEthon Paul * The same page can be mapped back since last copy attempt. 2875c3e5ea6eSKirill A. Shutemov * Try to copy again under PTL. 2876c3e5ea6eSKirill A. Shutemov */ 2877c3e5ea6eSKirill A. Shutemov if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) { 287883d116c5SJia He /* 287983d116c5SJia He * Give a warn in case there can be some obscure 288083d116c5SJia He * use-case 288183d116c5SJia He */ 2882c3e5ea6eSKirill A. Shutemov warn: 288383d116c5SJia He WARN_ON_ONCE(1); 28843ecb01dfSJan Beulich clear_page(kaddr); 288583d116c5SJia He } 2886c3e5ea6eSKirill A. Shutemov } 288783d116c5SJia He 2888a873dfe1STony Luck ret = 0; 288983d116c5SJia He 289083d116c5SJia He pte_unlock: 28913db82b93SHugh Dickins if (vmf->pte) 289283d116c5SJia He pte_unmap_unlock(vmf->pte, vmf->ptl); 28939b04c5feSCong Wang kunmap_atomic(kaddr); 2894c4ec7b0dSDmitriy Monakhov flush_dcache_page(dst); 289583d116c5SJia He 289683d116c5SJia He return ret; 28976aab341eSLinus Torvalds } 28986aab341eSLinus Torvalds 2899c20cd45eSMichal Hocko static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma) 2900c20cd45eSMichal Hocko { 2901c20cd45eSMichal Hocko struct file *vm_file = vma->vm_file; 2902c20cd45eSMichal Hocko 2903c20cd45eSMichal Hocko if (vm_file) 2904c20cd45eSMichal Hocko return mapping_gfp_mask(vm_file->f_mapping) | __GFP_FS | __GFP_IO; 2905c20cd45eSMichal Hocko 2906c20cd45eSMichal Hocko /* 2907c20cd45eSMichal Hocko * Special mappings (e.g. VDSO) do not have any file so fake 2908c20cd45eSMichal Hocko * a default GFP_KERNEL for them. 2909c20cd45eSMichal Hocko */ 2910c20cd45eSMichal Hocko return GFP_KERNEL; 2911c20cd45eSMichal Hocko } 2912c20cd45eSMichal Hocko 29131da177e4SLinus Torvalds /* 2914fb09a464SKirill A. Shutemov * Notify the address space that the page is about to become writable so that 2915fb09a464SKirill A. Shutemov * it can prohibit this or wait for the page to get into an appropriate state. 2916fb09a464SKirill A. Shutemov * 2917fb09a464SKirill A. Shutemov * We do this without the lock held, so that it can sleep if it needs to. 2918fb09a464SKirill A. Shutemov */ 291986aa6998SSidhartha Kumar static vm_fault_t do_page_mkwrite(struct vm_fault *vmf, struct folio *folio) 2920fb09a464SKirill A. Shutemov { 29212b740303SSouptick Joarder vm_fault_t ret; 292238b8cb7fSJan Kara unsigned int old_flags = vmf->flags; 2923fb09a464SKirill A. Shutemov 292438b8cb7fSJan Kara vmf->flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE; 2925fb09a464SKirill A. Shutemov 2926dc617f29SDarrick J. Wong if (vmf->vma->vm_file && 2927dc617f29SDarrick J. Wong IS_SWAPFILE(vmf->vma->vm_file->f_mapping->host)) 2928dc617f29SDarrick J. Wong return VM_FAULT_SIGBUS; 2929dc617f29SDarrick J. Wong 293011bac800SDave Jiang ret = vmf->vma->vm_ops->page_mkwrite(vmf); 293138b8cb7fSJan Kara /* Restore original flags so that caller is not surprised */ 293238b8cb7fSJan Kara vmf->flags = old_flags; 2933fb09a464SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) 2934fb09a464SKirill A. Shutemov return ret; 2935fb09a464SKirill A. Shutemov if (unlikely(!(ret & VM_FAULT_LOCKED))) { 29363d243659SSidhartha Kumar folio_lock(folio); 29373d243659SSidhartha Kumar if (!folio->mapping) { 29383d243659SSidhartha Kumar folio_unlock(folio); 2939fb09a464SKirill A. Shutemov return 0; /* retry */ 2940fb09a464SKirill A. Shutemov } 2941fb09a464SKirill A. Shutemov ret |= VM_FAULT_LOCKED; 2942fb09a464SKirill A. Shutemov } else 29433d243659SSidhartha Kumar VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); 2944fb09a464SKirill A. Shutemov return ret; 2945fb09a464SKirill A. Shutemov } 2946fb09a464SKirill A. Shutemov 2947fb09a464SKirill A. Shutemov /* 294897ba0c2bSJan Kara * Handle dirtying of a page in shared file mapping on a write fault. 29494e047f89SShachar Raindel * 295097ba0c2bSJan Kara * The function expects the page to be locked and unlocks it. 29514e047f89SShachar Raindel */ 295289b15332SJohannes Weiner static vm_fault_t fault_dirty_shared_page(struct vm_fault *vmf) 29534e047f89SShachar Raindel { 295489b15332SJohannes Weiner struct vm_area_struct *vma = vmf->vma; 29554e047f89SShachar Raindel struct address_space *mapping; 295615b4919aSZhangPeng struct folio *folio = page_folio(vmf->page); 295797ba0c2bSJan Kara bool dirtied; 295897ba0c2bSJan Kara bool page_mkwrite = vma->vm_ops && vma->vm_ops->page_mkwrite; 29594e047f89SShachar Raindel 296015b4919aSZhangPeng dirtied = folio_mark_dirty(folio); 296115b4919aSZhangPeng VM_BUG_ON_FOLIO(folio_test_anon(folio), folio); 296297ba0c2bSJan Kara /* 296315b4919aSZhangPeng * Take a local copy of the address_space - folio.mapping may be zeroed 296415b4919aSZhangPeng * by truncate after folio_unlock(). The address_space itself remains 296515b4919aSZhangPeng * pinned by vma->vm_file's reference. We rely on folio_unlock()'s 296697ba0c2bSJan Kara * release semantics to prevent the compiler from undoing this copying. 296797ba0c2bSJan Kara */ 296815b4919aSZhangPeng mapping = folio_raw_mapping(folio); 296915b4919aSZhangPeng folio_unlock(folio); 29704e047f89SShachar Raindel 29714e047f89SShachar Raindel if (!page_mkwrite) 29724e047f89SShachar Raindel file_update_time(vma->vm_file); 297389b15332SJohannes Weiner 297489b15332SJohannes Weiner /* 297589b15332SJohannes Weiner * Throttle page dirtying rate down to writeback speed. 297689b15332SJohannes Weiner * 297789b15332SJohannes Weiner * mapping may be NULL here because some device drivers do not 297889b15332SJohannes Weiner * set page.mapping but still dirty their pages 297989b15332SJohannes Weiner * 2980c1e8d7c6SMichel Lespinasse * Drop the mmap_lock before waiting on IO, if we can. The file 298189b15332SJohannes Weiner * is pinning the mapping, as per above. 298289b15332SJohannes Weiner */ 298389b15332SJohannes Weiner if ((dirtied || page_mkwrite) && mapping) { 298489b15332SJohannes Weiner struct file *fpin; 298589b15332SJohannes Weiner 298689b15332SJohannes Weiner fpin = maybe_unlock_mmap_for_io(vmf, NULL); 298789b15332SJohannes Weiner balance_dirty_pages_ratelimited(mapping); 298889b15332SJohannes Weiner if (fpin) { 298989b15332SJohannes Weiner fput(fpin); 2990d9272525SPeter Xu return VM_FAULT_COMPLETED; 299189b15332SJohannes Weiner } 299289b15332SJohannes Weiner } 299389b15332SJohannes Weiner 299489b15332SJohannes Weiner return 0; 29954e047f89SShachar Raindel } 29964e047f89SShachar Raindel 299797ba0c2bSJan Kara /* 29984e047f89SShachar Raindel * Handle write page faults for pages that can be reused in the current vma 29994e047f89SShachar Raindel * 30004e047f89SShachar Raindel * This can happen either due to the mapping being with the VM_SHARED flag, 30014e047f89SShachar Raindel * or due to us being the last reference standing to the page. In either 30024e047f89SShachar Raindel * case, all we need to do here is to mark the page as writable and update 30034e047f89SShachar Raindel * any related book-keeping. 30044e047f89SShachar Raindel */ 3005997dd98dSJan Kara static inline void wp_page_reuse(struct vm_fault *vmf) 300682b0f8c3SJan Kara __releases(vmf->ptl) 30074e047f89SShachar Raindel { 300882b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3009a41b70d6SJan Kara struct page *page = vmf->page; 30104e047f89SShachar Raindel pte_t entry; 30116c287605SDavid Hildenbrand 3012c89357e2SDavid Hildenbrand VM_BUG_ON(!(vmf->flags & FAULT_FLAG_WRITE)); 3013cdb281e6SQi Zheng VM_BUG_ON(page && PageAnon(page) && !PageAnonExclusive(page)); 30146c287605SDavid Hildenbrand 30154e047f89SShachar Raindel /* 30164e047f89SShachar Raindel * Clear the pages cpupid information as the existing 30174e047f89SShachar Raindel * information potentially belongs to a now completely 30184e047f89SShachar Raindel * unrelated process. 30194e047f89SShachar Raindel */ 30204e047f89SShachar Raindel if (page) 30214e047f89SShachar Raindel page_cpupid_xchg_last(page, (1 << LAST_CPUPID_SHIFT) - 1); 30224e047f89SShachar Raindel 30232994302bSJan Kara flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); 30242994302bSJan Kara entry = pte_mkyoung(vmf->orig_pte); 30254e047f89SShachar Raindel entry = maybe_mkwrite(pte_mkdirty(entry), vma); 302682b0f8c3SJan Kara if (ptep_set_access_flags(vma, vmf->address, vmf->pte, entry, 1)) 30275003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vma, vmf->address, vmf->pte, 1); 302882b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3029798a6b87SPeter Xu count_vm_event(PGREUSE); 30304e047f89SShachar Raindel } 30314e047f89SShachar Raindel 30324e047f89SShachar Raindel /* 3033c89357e2SDavid Hildenbrand * Handle the case of a page which we actually need to copy to a new page, 3034c89357e2SDavid Hildenbrand * either due to COW or unsharing. 30352f38ab2cSShachar Raindel * 3036c1e8d7c6SMichel Lespinasse * Called with mmap_lock locked and the old page referenced, but 30372f38ab2cSShachar Raindel * without the ptl held. 30382f38ab2cSShachar Raindel * 30392f38ab2cSShachar Raindel * High level logic flow: 30402f38ab2cSShachar Raindel * 30412f38ab2cSShachar Raindel * - Allocate a page, copy the content of the old page to the new one. 30422f38ab2cSShachar Raindel * - Handle book keeping and accounting - cgroups, mmu-notifiers, etc. 30432f38ab2cSShachar Raindel * - Take the PTL. If the pte changed, bail out and release the allocated page 30442f38ab2cSShachar Raindel * - If the pte is still the way we remember it, update the page table and all 30452f38ab2cSShachar Raindel * relevant references. This includes dropping the reference the page-table 30462f38ab2cSShachar Raindel * held to the old page, as well as updating the rmap. 30472f38ab2cSShachar Raindel * - In any case, unlock the PTL and drop the reference we took to the old page. 30482f38ab2cSShachar Raindel */ 30492b740303SSouptick Joarder static vm_fault_t wp_page_copy(struct vm_fault *vmf) 30502f38ab2cSShachar Raindel { 3051c89357e2SDavid Hildenbrand const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE; 305282b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3053bae473a4SKirill A. Shutemov struct mm_struct *mm = vma->vm_mm; 305428d41a48SMatthew Wilcox (Oracle) struct folio *old_folio = NULL; 305528d41a48SMatthew Wilcox (Oracle) struct folio *new_folio = NULL; 30562f38ab2cSShachar Raindel pte_t entry; 30572f38ab2cSShachar Raindel int page_copied = 0; 3058ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 3059a873dfe1STony Luck int ret; 30602f38ab2cSShachar Raindel 3061662ce1dcSYang Yang delayacct_wpcopy_start(); 3062662ce1dcSYang Yang 306328d41a48SMatthew Wilcox (Oracle) if (vmf->page) 306428d41a48SMatthew Wilcox (Oracle) old_folio = page_folio(vmf->page); 30652f38ab2cSShachar Raindel if (unlikely(anon_vma_prepare(vma))) 30662f38ab2cSShachar Raindel goto oom; 30672f38ab2cSShachar Raindel 30682994302bSJan Kara if (is_zero_pfn(pte_pfn(vmf->orig_pte))) { 30696bc56a4dSMatthew Wilcox (Oracle) new_folio = vma_alloc_zeroed_movable_folio(vma, vmf->address); 30706bc56a4dSMatthew Wilcox (Oracle) if (!new_folio) 30712f38ab2cSShachar Raindel goto oom; 30722f38ab2cSShachar Raindel } else { 307328d41a48SMatthew Wilcox (Oracle) new_folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, vma, 307428d41a48SMatthew Wilcox (Oracle) vmf->address, false); 307528d41a48SMatthew Wilcox (Oracle) if (!new_folio) 30762f38ab2cSShachar Raindel goto oom; 307783d116c5SJia He 307828d41a48SMatthew Wilcox (Oracle) ret = __wp_page_copy_user(&new_folio->page, vmf->page, vmf); 3079a873dfe1STony Luck if (ret) { 308083d116c5SJia He /* 308183d116c5SJia He * COW failed, if the fault was solved by other, 308283d116c5SJia He * it's fine. If not, userspace would re-fault on 308383d116c5SJia He * the same address and we will handle the fault 308483d116c5SJia He * from the second attempt. 3085a873dfe1STony Luck * The -EHWPOISON case will not be retried. 308683d116c5SJia He */ 308728d41a48SMatthew Wilcox (Oracle) folio_put(new_folio); 308828d41a48SMatthew Wilcox (Oracle) if (old_folio) 308928d41a48SMatthew Wilcox (Oracle) folio_put(old_folio); 3090662ce1dcSYang Yang 3091662ce1dcSYang Yang delayacct_wpcopy_end(); 3092a873dfe1STony Luck return ret == -EHWPOISON ? VM_FAULT_HWPOISON : 0; 309383d116c5SJia He } 309428d41a48SMatthew Wilcox (Oracle) kmsan_copy_page_meta(&new_folio->page, vmf->page); 30952f38ab2cSShachar Raindel } 30962f38ab2cSShachar Raindel 309728d41a48SMatthew Wilcox (Oracle) if (mem_cgroup_charge(new_folio, mm, GFP_KERNEL)) 30982f38ab2cSShachar Raindel goto oom_free_new; 30994d4f75bfSKefeng Wang folio_throttle_swaprate(new_folio, GFP_KERNEL); 31002f38ab2cSShachar Raindel 310128d41a48SMatthew Wilcox (Oracle) __folio_mark_uptodate(new_folio); 3102eb3c24f3SMel Gorman 31037d4a8be0SAlistair Popple mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, 31046f4f13e8SJérôme Glisse vmf->address & PAGE_MASK, 3105ac46d4f3SJérôme Glisse (vmf->address & PAGE_MASK) + PAGE_SIZE); 3106ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 31072f38ab2cSShachar Raindel 31082f38ab2cSShachar Raindel /* 31092f38ab2cSShachar Raindel * Re-check the pte - we dropped the lock 31102f38ab2cSShachar Raindel */ 311182b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(mm, vmf->pmd, vmf->address, &vmf->ptl); 3112c33c7948SRyan Roberts if (likely(vmf->pte && pte_same(ptep_get(vmf->pte), vmf->orig_pte))) { 311328d41a48SMatthew Wilcox (Oracle) if (old_folio) { 311428d41a48SMatthew Wilcox (Oracle) if (!folio_test_anon(old_folio)) { 311528d41a48SMatthew Wilcox (Oracle) dec_mm_counter(mm, mm_counter_file(&old_folio->page)); 3116f1a79412SShakeel Butt inc_mm_counter(mm, MM_ANONPAGES); 31172f38ab2cSShachar Raindel } 31182f38ab2cSShachar Raindel } else { 31196080d19fSxu xin ksm_might_unmap_zero_page(mm, vmf->orig_pte); 3120f1a79412SShakeel Butt inc_mm_counter(mm, MM_ANONPAGES); 31212f38ab2cSShachar Raindel } 31222994302bSJan Kara flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); 312328d41a48SMatthew Wilcox (Oracle) entry = mk_pte(&new_folio->page, vma->vm_page_prot); 312450c25ee9SThomas Bogendoerfer entry = pte_sw_mkyoung(entry); 3125c89357e2SDavid Hildenbrand if (unlikely(unshare)) { 3126c89357e2SDavid Hildenbrand if (pte_soft_dirty(vmf->orig_pte)) 3127c89357e2SDavid Hildenbrand entry = pte_mksoft_dirty(entry); 3128c89357e2SDavid Hildenbrand if (pte_uffd_wp(vmf->orig_pte)) 3129c89357e2SDavid Hildenbrand entry = pte_mkuffd_wp(entry); 3130c89357e2SDavid Hildenbrand } else { 31312f38ab2cSShachar Raindel entry = maybe_mkwrite(pte_mkdirty(entry), vma); 3132c89357e2SDavid Hildenbrand } 3133111fe718SNicholas Piggin 31342f38ab2cSShachar Raindel /* 31352f38ab2cSShachar Raindel * Clear the pte entry and flush it first, before updating the 3136111fe718SNicholas Piggin * pte with the new entry, to keep TLBs on different CPUs in 3137111fe718SNicholas Piggin * sync. This code used to set the new PTE then flush TLBs, but 3138111fe718SNicholas Piggin * that left a window where the new PTE could be loaded into 3139111fe718SNicholas Piggin * some TLBs while the old PTE remains in others. 31402f38ab2cSShachar Raindel */ 3141ec8832d0SAlistair Popple ptep_clear_flush(vma, vmf->address, vmf->pte); 314228d41a48SMatthew Wilcox (Oracle) folio_add_new_anon_rmap(new_folio, vma, vmf->address); 314328d41a48SMatthew Wilcox (Oracle) folio_add_lru_vma(new_folio, vma); 31442f38ab2cSShachar Raindel /* 31452f38ab2cSShachar Raindel * We call the notify macro here because, when using secondary 31462f38ab2cSShachar Raindel * mmu page tables (such as kvm shadow page tables), we want the 31472f38ab2cSShachar Raindel * new page to be mapped directly into the secondary page table. 31482f38ab2cSShachar Raindel */ 3149c89357e2SDavid Hildenbrand BUG_ON(unshare && pte_write(entry)); 315082b0f8c3SJan Kara set_pte_at_notify(mm, vmf->address, vmf->pte, entry); 31515003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vma, vmf->address, vmf->pte, 1); 315228d41a48SMatthew Wilcox (Oracle) if (old_folio) { 31532f38ab2cSShachar Raindel /* 31542f38ab2cSShachar Raindel * Only after switching the pte to the new page may 31552f38ab2cSShachar Raindel * we remove the mapcount here. Otherwise another 31562f38ab2cSShachar Raindel * process may come and find the rmap count decremented 31572f38ab2cSShachar Raindel * before the pte is switched to the new page, and 31582f38ab2cSShachar Raindel * "reuse" the old page writing into it while our pte 31592f38ab2cSShachar Raindel * here still points into it and can be read by other 31602f38ab2cSShachar Raindel * threads. 31612f38ab2cSShachar Raindel * 31622f38ab2cSShachar Raindel * The critical issue is to order this 31632f38ab2cSShachar Raindel * page_remove_rmap with the ptp_clear_flush above. 31642f38ab2cSShachar Raindel * Those stores are ordered by (if nothing else,) 31652f38ab2cSShachar Raindel * the barrier present in the atomic_add_negative 31662f38ab2cSShachar Raindel * in page_remove_rmap. 31672f38ab2cSShachar Raindel * 31682f38ab2cSShachar Raindel * Then the TLB flush in ptep_clear_flush ensures that 31692f38ab2cSShachar Raindel * no process can access the old page before the 31702f38ab2cSShachar Raindel * decremented mapcount is visible. And the old page 31712f38ab2cSShachar Raindel * cannot be reused until after the decremented 31722f38ab2cSShachar Raindel * mapcount is visible. So transitively, TLBs to 31732f38ab2cSShachar Raindel * old page will be flushed before it can be reused. 31742f38ab2cSShachar Raindel */ 317528d41a48SMatthew Wilcox (Oracle) page_remove_rmap(vmf->page, vma, false); 31762f38ab2cSShachar Raindel } 31772f38ab2cSShachar Raindel 31782f38ab2cSShachar Raindel /* Free the old page.. */ 317928d41a48SMatthew Wilcox (Oracle) new_folio = old_folio; 31802f38ab2cSShachar Raindel page_copied = 1; 31813db82b93SHugh Dickins pte_unmap_unlock(vmf->pte, vmf->ptl); 31823db82b93SHugh Dickins } else if (vmf->pte) { 31837df67697SBibo Mao update_mmu_tlb(vma, vmf->address, vmf->pte); 31843db82b93SHugh Dickins pte_unmap_unlock(vmf->pte, vmf->ptl); 31852f38ab2cSShachar Raindel } 31862f38ab2cSShachar Raindel 3187ec8832d0SAlistair Popple mmu_notifier_invalidate_range_end(&range); 31883db82b93SHugh Dickins 31893db82b93SHugh Dickins if (new_folio) 31903db82b93SHugh Dickins folio_put(new_folio); 319128d41a48SMatthew Wilcox (Oracle) if (old_folio) { 3192f4c4a3f4SHuang Ying if (page_copied) 319328d41a48SMatthew Wilcox (Oracle) free_swap_cache(&old_folio->page); 319428d41a48SMatthew Wilcox (Oracle) folio_put(old_folio); 31952f38ab2cSShachar Raindel } 3196662ce1dcSYang Yang 3197662ce1dcSYang Yang delayacct_wpcopy_end(); 3198cb8d8633SDavid Hildenbrand return 0; 31992f38ab2cSShachar Raindel oom_free_new: 320028d41a48SMatthew Wilcox (Oracle) folio_put(new_folio); 32012f38ab2cSShachar Raindel oom: 320228d41a48SMatthew Wilcox (Oracle) if (old_folio) 320328d41a48SMatthew Wilcox (Oracle) folio_put(old_folio); 3204662ce1dcSYang Yang 3205662ce1dcSYang Yang delayacct_wpcopy_end(); 32062f38ab2cSShachar Raindel return VM_FAULT_OOM; 32072f38ab2cSShachar Raindel } 32082f38ab2cSShachar Raindel 320966a6197cSJan Kara /** 321066a6197cSJan Kara * finish_mkwrite_fault - finish page fault for a shared mapping, making PTE 321166a6197cSJan Kara * writeable once the page is prepared 321266a6197cSJan Kara * 321366a6197cSJan Kara * @vmf: structure describing the fault 321466a6197cSJan Kara * 321566a6197cSJan Kara * This function handles all that is needed to finish a write page fault in a 321666a6197cSJan Kara * shared mapping due to PTE being read-only once the mapped page is prepared. 3217a862f68aSMike Rapoport * It handles locking of PTE and modifying it. 321866a6197cSJan Kara * 321966a6197cSJan Kara * The function expects the page to be locked or other protection against 322066a6197cSJan Kara * concurrent faults / writeback (such as DAX radix tree locks). 3221a862f68aSMike Rapoport * 32222797e79fSLiu Xiang * Return: %0 on success, %VM_FAULT_NOPAGE when PTE got changed before 3223a862f68aSMike Rapoport * we acquired PTE lock. 322466a6197cSJan Kara */ 32252b740303SSouptick Joarder vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf) 322666a6197cSJan Kara { 322766a6197cSJan Kara WARN_ON_ONCE(!(vmf->vma->vm_flags & VM_SHARED)); 322866a6197cSJan Kara vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, vmf->address, 322966a6197cSJan Kara &vmf->ptl); 32303db82b93SHugh Dickins if (!vmf->pte) 32313db82b93SHugh Dickins return VM_FAULT_NOPAGE; 323266a6197cSJan Kara /* 323366a6197cSJan Kara * We might have raced with another page fault while we released the 323466a6197cSJan Kara * pte_offset_map_lock. 323566a6197cSJan Kara */ 3236c33c7948SRyan Roberts if (!pte_same(ptep_get(vmf->pte), vmf->orig_pte)) { 32377df67697SBibo Mao update_mmu_tlb(vmf->vma, vmf->address, vmf->pte); 323866a6197cSJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3239a19e2553SJan Kara return VM_FAULT_NOPAGE; 324066a6197cSJan Kara } 324166a6197cSJan Kara wp_page_reuse(vmf); 3242a19e2553SJan Kara return 0; 324366a6197cSJan Kara } 324466a6197cSJan Kara 3245dd906184SBoaz Harrosh /* 3246dd906184SBoaz Harrosh * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED 3247dd906184SBoaz Harrosh * mapping 3248dd906184SBoaz Harrosh */ 32492b740303SSouptick Joarder static vm_fault_t wp_pfn_shared(struct vm_fault *vmf) 3250dd906184SBoaz Harrosh { 325182b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3252bae473a4SKirill A. Shutemov 3253dd906184SBoaz Harrosh if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) { 32542b740303SSouptick Joarder vm_fault_t ret; 3255dd906184SBoaz Harrosh 325682b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3257063e60d8SMatthew Wilcox (Oracle) if (vmf->flags & FAULT_FLAG_VMA_LOCK) { 3258063e60d8SMatthew Wilcox (Oracle) vma_end_read(vmf->vma); 3259063e60d8SMatthew Wilcox (Oracle) return VM_FAULT_RETRY; 3260063e60d8SMatthew Wilcox (Oracle) } 3261063e60d8SMatthew Wilcox (Oracle) 3262fe82221fSJan Kara vmf->flags |= FAULT_FLAG_MKWRITE; 326311bac800SDave Jiang ret = vma->vm_ops->pfn_mkwrite(vmf); 32642f89dc12SJan Kara if (ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)) 3265dd906184SBoaz Harrosh return ret; 326666a6197cSJan Kara return finish_mkwrite_fault(vmf); 3267dd906184SBoaz Harrosh } 3268997dd98dSJan Kara wp_page_reuse(vmf); 3269cb8d8633SDavid Hildenbrand return 0; 3270dd906184SBoaz Harrosh } 3271dd906184SBoaz Harrosh 32725a97858bSSidhartha Kumar static vm_fault_t wp_page_shared(struct vm_fault *vmf, struct folio *folio) 327382b0f8c3SJan Kara __releases(vmf->ptl) 327493e478d4SShachar Raindel { 327582b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3276cb8d8633SDavid Hildenbrand vm_fault_t ret = 0; 327793e478d4SShachar Raindel 32785a97858bSSidhartha Kumar folio_get(folio); 327993e478d4SShachar Raindel 328093e478d4SShachar Raindel if (vma->vm_ops && vma->vm_ops->page_mkwrite) { 32812b740303SSouptick Joarder vm_fault_t tmp; 328293e478d4SShachar Raindel 328382b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 3284063e60d8SMatthew Wilcox (Oracle) if (vmf->flags & FAULT_FLAG_VMA_LOCK) { 3285063e60d8SMatthew Wilcox (Oracle) folio_put(folio); 3286063e60d8SMatthew Wilcox (Oracle) vma_end_read(vmf->vma); 3287063e60d8SMatthew Wilcox (Oracle) return VM_FAULT_RETRY; 3288063e60d8SMatthew Wilcox (Oracle) } 3289063e60d8SMatthew Wilcox (Oracle) 329086aa6998SSidhartha Kumar tmp = do_page_mkwrite(vmf, folio); 329193e478d4SShachar Raindel if (unlikely(!tmp || (tmp & 329293e478d4SShachar Raindel (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { 32935a97858bSSidhartha Kumar folio_put(folio); 329493e478d4SShachar Raindel return tmp; 329593e478d4SShachar Raindel } 329666a6197cSJan Kara tmp = finish_mkwrite_fault(vmf); 3297a19e2553SJan Kara if (unlikely(tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) { 32985a97858bSSidhartha Kumar folio_unlock(folio); 32995a97858bSSidhartha Kumar folio_put(folio); 330066a6197cSJan Kara return tmp; 330193e478d4SShachar Raindel } 330266a6197cSJan Kara } else { 3303997dd98dSJan Kara wp_page_reuse(vmf); 33045a97858bSSidhartha Kumar folio_lock(folio); 330593e478d4SShachar Raindel } 330689b15332SJohannes Weiner ret |= fault_dirty_shared_page(vmf); 33075a97858bSSidhartha Kumar folio_put(folio); 330893e478d4SShachar Raindel 330989b15332SJohannes Weiner return ret; 331093e478d4SShachar Raindel } 331193e478d4SShachar Raindel 33122f38ab2cSShachar Raindel /* 3313c89357e2SDavid Hildenbrand * This routine handles present pages, when 3314c89357e2SDavid Hildenbrand * * users try to write to a shared page (FAULT_FLAG_WRITE) 3315c89357e2SDavid Hildenbrand * * GUP wants to take a R/O pin on a possibly shared anonymous page 3316c89357e2SDavid Hildenbrand * (FAULT_FLAG_UNSHARE) 3317c89357e2SDavid Hildenbrand * 3318c89357e2SDavid Hildenbrand * It is done by copying the page to a new address and decrementing the 3319c89357e2SDavid Hildenbrand * shared-page counter for the old page. 33201da177e4SLinus Torvalds * 33211da177e4SLinus Torvalds * Note that this routine assumes that the protection checks have been 33221da177e4SLinus Torvalds * done by the caller (the low-level page fault routine in most cases). 3323c89357e2SDavid Hildenbrand * Thus, with FAULT_FLAG_WRITE, we can safely just mark it writable once we've 3324c89357e2SDavid Hildenbrand * done any necessary COW. 33251da177e4SLinus Torvalds * 3326c89357e2SDavid Hildenbrand * In case of FAULT_FLAG_WRITE, we also mark the page dirty at this point even 3327c89357e2SDavid Hildenbrand * though the page will change only once the write actually happens. This 3328c89357e2SDavid Hildenbrand * avoids a few races, and potentially makes it more efficient. 33291da177e4SLinus Torvalds * 3330c1e8d7c6SMichel Lespinasse * We enter with non-exclusive mmap_lock (to exclude vma changes, 33318f4e2101SHugh Dickins * but allow concurrent faults), with pte both mapped and locked. 3332c1e8d7c6SMichel Lespinasse * We return with mmap_lock still held, but pte unmapped and unlocked. 33331da177e4SLinus Torvalds */ 33342b740303SSouptick Joarder static vm_fault_t do_wp_page(struct vm_fault *vmf) 333582b0f8c3SJan Kara __releases(vmf->ptl) 33361da177e4SLinus Torvalds { 3337c89357e2SDavid Hildenbrand const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE; 333882b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3339b9086fdeSDavid Hildenbrand struct folio *folio = NULL; 33401da177e4SLinus Torvalds 3341c89357e2SDavid Hildenbrand if (likely(!unshare)) { 3342c33c7948SRyan Roberts if (userfaultfd_pte_wp(vma, ptep_get(vmf->pte))) { 3343529b930bSAndrea Arcangeli pte_unmap_unlock(vmf->pte, vmf->ptl); 3344529b930bSAndrea Arcangeli return handle_userfault(vmf, VM_UFFD_WP); 3345529b930bSAndrea Arcangeli } 3346529b930bSAndrea Arcangeli 33476ce64428SNadav Amit /* 33486ce64428SNadav Amit * Userfaultfd write-protect can defer flushes. Ensure the TLB 33496ce64428SNadav Amit * is flushed in this case before copying. 33506ce64428SNadav Amit */ 33516ce64428SNadav Amit if (unlikely(userfaultfd_wp(vmf->vma) && 33526ce64428SNadav Amit mm_tlb_flush_pending(vmf->vma->vm_mm))) 33536ce64428SNadav Amit flush_tlb_page(vmf->vma, vmf->address); 3354c89357e2SDavid Hildenbrand } 33556ce64428SNadav Amit 3356a41b70d6SJan Kara vmf->page = vm_normal_page(vma, vmf->address, vmf->orig_pte); 3357c89357e2SDavid Hildenbrand 33585a97858bSSidhartha Kumar if (vmf->page) 33595a97858bSSidhartha Kumar folio = page_folio(vmf->page); 33605a97858bSSidhartha Kumar 3361251b97f5SPeter Zijlstra /* 3362b9086fdeSDavid Hildenbrand * Shared mapping: we are guaranteed to have VM_WRITE and 3363b9086fdeSDavid Hildenbrand * FAULT_FLAG_WRITE set at this point. 3364b9086fdeSDavid Hildenbrand */ 3365b9086fdeSDavid Hildenbrand if (vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) { 3366b9086fdeSDavid Hildenbrand /* 336764e45507SPeter Feiner * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a 336864e45507SPeter Feiner * VM_PFNMAP VMA. 3369251b97f5SPeter Zijlstra * 3370251b97f5SPeter Zijlstra * We should not cow pages in a shared writeable mapping. 3371dd906184SBoaz Harrosh * Just mark the pages writable and/or call ops->pfn_mkwrite. 3372251b97f5SPeter Zijlstra */ 3373b9086fdeSDavid Hildenbrand if (!vmf->page) 33742994302bSJan Kara return wp_pfn_shared(vmf); 33755a97858bSSidhartha Kumar return wp_page_shared(vmf, folio); 3376251b97f5SPeter Zijlstra } 33771da177e4SLinus Torvalds 3378b9086fdeSDavid Hildenbrand /* 3379b9086fdeSDavid Hildenbrand * Private mapping: create an exclusive anonymous page copy if reuse 3380b9086fdeSDavid Hildenbrand * is impossible. We might miss VM_WRITE for FOLL_FORCE handling. 3381b9086fdeSDavid Hildenbrand */ 3382b9086fdeSDavid Hildenbrand if (folio && folio_test_anon(folio)) { 338353a05ad9SDavid Hildenbrand /* 33846c287605SDavid Hildenbrand * If the page is exclusive to this process we must reuse the 33856c287605SDavid Hildenbrand * page without further checks. 33866c287605SDavid Hildenbrand */ 3387e4a2ed94SMatthew Wilcox (Oracle) if (PageAnonExclusive(vmf->page)) 33886c287605SDavid Hildenbrand goto reuse; 33896c287605SDavid Hildenbrand 33906c287605SDavid Hildenbrand /* 3391e4a2ed94SMatthew Wilcox (Oracle) * We have to verify under folio lock: these early checks are 3392e4a2ed94SMatthew Wilcox (Oracle) * just an optimization to avoid locking the folio and freeing 339353a05ad9SDavid Hildenbrand * the swapcache if there is little hope that we can reuse. 339453a05ad9SDavid Hildenbrand * 3395e4a2ed94SMatthew Wilcox (Oracle) * KSM doesn't necessarily raise the folio refcount. 339653a05ad9SDavid Hildenbrand */ 3397e4a2ed94SMatthew Wilcox (Oracle) if (folio_test_ksm(folio) || folio_ref_count(folio) > 3) 3398d4c47097SDavid Hildenbrand goto copy; 3399e4a2ed94SMatthew Wilcox (Oracle) if (!folio_test_lru(folio)) 3400d4c47097SDavid Hildenbrand /* 34011fec6890SMatthew Wilcox (Oracle) * We cannot easily detect+handle references from 34021fec6890SMatthew Wilcox (Oracle) * remote LRU caches or references to LRU folios. 3403d4c47097SDavid Hildenbrand */ 3404d4c47097SDavid Hildenbrand lru_add_drain(); 3405e4a2ed94SMatthew Wilcox (Oracle) if (folio_ref_count(folio) > 1 + folio_test_swapcache(folio)) 340652d1e606SKirill Tkhai goto copy; 3407e4a2ed94SMatthew Wilcox (Oracle) if (!folio_trylock(folio)) 340852d1e606SKirill Tkhai goto copy; 3409e4a2ed94SMatthew Wilcox (Oracle) if (folio_test_swapcache(folio)) 3410e4a2ed94SMatthew Wilcox (Oracle) folio_free_swap(folio); 3411e4a2ed94SMatthew Wilcox (Oracle) if (folio_test_ksm(folio) || folio_ref_count(folio) != 1) { 3412e4a2ed94SMatthew Wilcox (Oracle) folio_unlock(folio); 341309854ba9SLinus Torvalds goto copy; 341452d1e606SKirill Tkhai } 3415c44b6743SRik van Riel /* 3416e4a2ed94SMatthew Wilcox (Oracle) * Ok, we've got the only folio reference from our mapping 3417e4a2ed94SMatthew Wilcox (Oracle) * and the folio is locked, it's dark out, and we're wearing 341853a05ad9SDavid Hildenbrand * sunglasses. Hit it. 3419c44b6743SRik van Riel */ 3420e4a2ed94SMatthew Wilcox (Oracle) page_move_anon_rmap(vmf->page, vma); 3421e4a2ed94SMatthew Wilcox (Oracle) folio_unlock(folio); 34226c287605SDavid Hildenbrand reuse: 3423c89357e2SDavid Hildenbrand if (unlikely(unshare)) { 3424c89357e2SDavid Hildenbrand pte_unmap_unlock(vmf->pte, vmf->ptl); 3425c89357e2SDavid Hildenbrand return 0; 3426c89357e2SDavid Hildenbrand } 3427be068f29SLinus Torvalds wp_page_reuse(vmf); 3428cb8d8633SDavid Hildenbrand return 0; 34291da177e4SLinus Torvalds } 343052d1e606SKirill Tkhai copy: 3431063e60d8SMatthew Wilcox (Oracle) if ((vmf->flags & FAULT_FLAG_VMA_LOCK) && !vma->anon_vma) { 3432063e60d8SMatthew Wilcox (Oracle) pte_unmap_unlock(vmf->pte, vmf->ptl); 3433063e60d8SMatthew Wilcox (Oracle) vma_end_read(vmf->vma); 3434063e60d8SMatthew Wilcox (Oracle) return VM_FAULT_RETRY; 3435063e60d8SMatthew Wilcox (Oracle) } 3436063e60d8SMatthew Wilcox (Oracle) 34371da177e4SLinus Torvalds /* 34381da177e4SLinus Torvalds * Ok, we need to copy. Oh, well.. 34391da177e4SLinus Torvalds */ 3440b9086fdeSDavid Hildenbrand if (folio) 3441b9086fdeSDavid Hildenbrand folio_get(folio); 344228766805SShachar Raindel 344382b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 344494bfe85bSYang Yang #ifdef CONFIG_KSM 3445b9086fdeSDavid Hildenbrand if (folio && folio_test_ksm(folio)) 344694bfe85bSYang Yang count_vm_event(COW_KSM); 344794bfe85bSYang Yang #endif 3448a41b70d6SJan Kara return wp_page_copy(vmf); 34491da177e4SLinus Torvalds } 34501da177e4SLinus Torvalds 345197a89413SPeter Zijlstra static void unmap_mapping_range_vma(struct vm_area_struct *vma, 34521da177e4SLinus Torvalds unsigned long start_addr, unsigned long end_addr, 34531da177e4SLinus Torvalds struct zap_details *details) 34541da177e4SLinus Torvalds { 3455f5cc4eefSAl Viro zap_page_range_single(vma, start_addr, end_addr - start_addr, details); 34561da177e4SLinus Torvalds } 34571da177e4SLinus Torvalds 3458f808c13fSDavidlohr Bueso static inline void unmap_mapping_range_tree(struct rb_root_cached *root, 3459232a6a1cSPeter Xu pgoff_t first_index, 3460232a6a1cSPeter Xu pgoff_t last_index, 34611da177e4SLinus Torvalds struct zap_details *details) 34621da177e4SLinus Torvalds { 34631da177e4SLinus Torvalds struct vm_area_struct *vma; 34641da177e4SLinus Torvalds pgoff_t vba, vea, zba, zea; 34651da177e4SLinus Torvalds 3466232a6a1cSPeter Xu vma_interval_tree_foreach(vma, root, first_index, last_index) { 34671da177e4SLinus Torvalds vba = vma->vm_pgoff; 3468d6e93217SLibin vea = vba + vma_pages(vma) - 1; 3469f9871da9SMiaohe Lin zba = max(first_index, vba); 3470f9871da9SMiaohe Lin zea = min(last_index, vea); 34711da177e4SLinus Torvalds 347297a89413SPeter Zijlstra unmap_mapping_range_vma(vma, 34731da177e4SLinus Torvalds ((zba - vba) << PAGE_SHIFT) + vma->vm_start, 34741da177e4SLinus Torvalds ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start, 347597a89413SPeter Zijlstra details); 34761da177e4SLinus Torvalds } 34771da177e4SLinus Torvalds } 34781da177e4SLinus Torvalds 34791da177e4SLinus Torvalds /** 34803506659eSMatthew Wilcox (Oracle) * unmap_mapping_folio() - Unmap single folio from processes. 34813506659eSMatthew Wilcox (Oracle) * @folio: The locked folio to be unmapped. 348222061a1fSHugh Dickins * 34833506659eSMatthew Wilcox (Oracle) * Unmap this folio from any userspace process which still has it mmaped. 348422061a1fSHugh Dickins * Typically, for efficiency, the range of nearby pages has already been 348522061a1fSHugh Dickins * unmapped by unmap_mapping_pages() or unmap_mapping_range(). But once 34863506659eSMatthew Wilcox (Oracle) * truncation or invalidation holds the lock on a folio, it may find that 34873506659eSMatthew Wilcox (Oracle) * the page has been remapped again: and then uses unmap_mapping_folio() 348822061a1fSHugh Dickins * to unmap it finally. 348922061a1fSHugh Dickins */ 34903506659eSMatthew Wilcox (Oracle) void unmap_mapping_folio(struct folio *folio) 349122061a1fSHugh Dickins { 34923506659eSMatthew Wilcox (Oracle) struct address_space *mapping = folio->mapping; 349322061a1fSHugh Dickins struct zap_details details = { }; 3494232a6a1cSPeter Xu pgoff_t first_index; 3495232a6a1cSPeter Xu pgoff_t last_index; 349622061a1fSHugh Dickins 34973506659eSMatthew Wilcox (Oracle) VM_BUG_ON(!folio_test_locked(folio)); 349822061a1fSHugh Dickins 34993506659eSMatthew Wilcox (Oracle) first_index = folio->index; 350087b11f86SSidhartha Kumar last_index = folio_next_index(folio) - 1; 3501232a6a1cSPeter Xu 35022e148f1eSPeter Xu details.even_cows = false; 35033506659eSMatthew Wilcox (Oracle) details.single_folio = folio; 3504999dad82SPeter Xu details.zap_flags = ZAP_FLAG_DROP_MARKER; 350522061a1fSHugh Dickins 35062c865995SHugh Dickins i_mmap_lock_read(mapping); 350722061a1fSHugh Dickins if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root))) 3508232a6a1cSPeter Xu unmap_mapping_range_tree(&mapping->i_mmap, first_index, 3509232a6a1cSPeter Xu last_index, &details); 35102c865995SHugh Dickins i_mmap_unlock_read(mapping); 351122061a1fSHugh Dickins } 351222061a1fSHugh Dickins 351322061a1fSHugh Dickins /** 3514977fbdcdSMatthew Wilcox * unmap_mapping_pages() - Unmap pages from processes. 3515977fbdcdSMatthew Wilcox * @mapping: The address space containing pages to be unmapped. 3516977fbdcdSMatthew Wilcox * @start: Index of first page to be unmapped. 3517977fbdcdSMatthew Wilcox * @nr: Number of pages to be unmapped. 0 to unmap to end of file. 3518977fbdcdSMatthew Wilcox * @even_cows: Whether to unmap even private COWed pages. 3519977fbdcdSMatthew Wilcox * 3520977fbdcdSMatthew Wilcox * Unmap the pages in this address space from any userspace process which 3521977fbdcdSMatthew Wilcox * has them mmaped. Generally, you want to remove COWed pages as well when 3522977fbdcdSMatthew Wilcox * a file is being truncated, but not when invalidating pages from the page 3523977fbdcdSMatthew Wilcox * cache. 3524977fbdcdSMatthew Wilcox */ 3525977fbdcdSMatthew Wilcox void unmap_mapping_pages(struct address_space *mapping, pgoff_t start, 3526977fbdcdSMatthew Wilcox pgoff_t nr, bool even_cows) 3527977fbdcdSMatthew Wilcox { 3528977fbdcdSMatthew Wilcox struct zap_details details = { }; 3529232a6a1cSPeter Xu pgoff_t first_index = start; 3530232a6a1cSPeter Xu pgoff_t last_index = start + nr - 1; 3531977fbdcdSMatthew Wilcox 35322e148f1eSPeter Xu details.even_cows = even_cows; 3533232a6a1cSPeter Xu if (last_index < first_index) 3534232a6a1cSPeter Xu last_index = ULONG_MAX; 3535977fbdcdSMatthew Wilcox 35362c865995SHugh Dickins i_mmap_lock_read(mapping); 3537977fbdcdSMatthew Wilcox if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root))) 3538232a6a1cSPeter Xu unmap_mapping_range_tree(&mapping->i_mmap, first_index, 3539232a6a1cSPeter Xu last_index, &details); 35402c865995SHugh Dickins i_mmap_unlock_read(mapping); 3541977fbdcdSMatthew Wilcox } 35426e0e99d5SDavid Howells EXPORT_SYMBOL_GPL(unmap_mapping_pages); 3543977fbdcdSMatthew Wilcox 3544977fbdcdSMatthew Wilcox /** 35458a5f14a2SKirill A. Shutemov * unmap_mapping_range - unmap the portion of all mmaps in the specified 3546977fbdcdSMatthew Wilcox * address_space corresponding to the specified byte range in the underlying 35478a5f14a2SKirill A. Shutemov * file. 35488a5f14a2SKirill A. Shutemov * 35493d41088fSMartin Waitz * @mapping: the address space containing mmaps to be unmapped. 35501da177e4SLinus Torvalds * @holebegin: byte in first page to unmap, relative to the start of 35511da177e4SLinus Torvalds * the underlying file. This will be rounded down to a PAGE_SIZE 355225d9e2d1Snpiggin@suse.de * boundary. Note that this is different from truncate_pagecache(), which 35531da177e4SLinus Torvalds * must keep the partial page. In contrast, we must get rid of 35541da177e4SLinus Torvalds * partial pages. 35551da177e4SLinus Torvalds * @holelen: size of prospective hole in bytes. This will be rounded 35561da177e4SLinus Torvalds * up to a PAGE_SIZE boundary. A holelen of zero truncates to the 35571da177e4SLinus Torvalds * end of the file. 35581da177e4SLinus Torvalds * @even_cows: 1 when truncating a file, unmap even private COWed pages; 35591da177e4SLinus Torvalds * but 0 when invalidating pagecache, don't throw away private data. 35601da177e4SLinus Torvalds */ 35611da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping, 35621da177e4SLinus Torvalds loff_t const holebegin, loff_t const holelen, int even_cows) 35631da177e4SLinus Torvalds { 35641da177e4SLinus Torvalds pgoff_t hba = holebegin >> PAGE_SHIFT; 35651da177e4SLinus Torvalds pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 35661da177e4SLinus Torvalds 35671da177e4SLinus Torvalds /* Check for overflow. */ 35681da177e4SLinus Torvalds if (sizeof(holelen) > sizeof(hlen)) { 35691da177e4SLinus Torvalds long long holeend = 35701da177e4SLinus Torvalds (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; 35711da177e4SLinus Torvalds if (holeend & ~(long long)ULONG_MAX) 35721da177e4SLinus Torvalds hlen = ULONG_MAX - hba + 1; 35731da177e4SLinus Torvalds } 35741da177e4SLinus Torvalds 3575977fbdcdSMatthew Wilcox unmap_mapping_pages(mapping, hba, hlen, even_cows); 35761da177e4SLinus Torvalds } 35771da177e4SLinus Torvalds EXPORT_SYMBOL(unmap_mapping_range); 35781da177e4SLinus Torvalds 35791da177e4SLinus Torvalds /* 3580b756a3b5SAlistair Popple * Restore a potential device exclusive pte to a working pte entry 3581b756a3b5SAlistair Popple */ 3582b756a3b5SAlistair Popple static vm_fault_t remove_device_exclusive_entry(struct vm_fault *vmf) 3583b756a3b5SAlistair Popple { 358419672a9eSMatthew Wilcox (Oracle) struct folio *folio = page_folio(vmf->page); 3585b756a3b5SAlistair Popple struct vm_area_struct *vma = vmf->vma; 3586b756a3b5SAlistair Popple struct mmu_notifier_range range; 3587fdc724d6SSuren Baghdasaryan vm_fault_t ret; 3588b756a3b5SAlistair Popple 35897c7b9629SAlistair Popple /* 35907c7b9629SAlistair Popple * We need a reference to lock the folio because we don't hold 35917c7b9629SAlistair Popple * the PTL so a racing thread can remove the device-exclusive 35927c7b9629SAlistair Popple * entry and unmap it. If the folio is free the entry must 35937c7b9629SAlistair Popple * have been removed already. If it happens to have already 35947c7b9629SAlistair Popple * been re-allocated after being freed all we do is lock and 35957c7b9629SAlistair Popple * unlock it. 35967c7b9629SAlistair Popple */ 35977c7b9629SAlistair Popple if (!folio_try_get(folio)) 35987c7b9629SAlistair Popple return 0; 35997c7b9629SAlistair Popple 3600fdc724d6SSuren Baghdasaryan ret = folio_lock_or_retry(folio, vmf); 3601fdc724d6SSuren Baghdasaryan if (ret) { 36027c7b9629SAlistair Popple folio_put(folio); 3603fdc724d6SSuren Baghdasaryan return ret; 36047c7b9629SAlistair Popple } 36057d4a8be0SAlistair Popple mmu_notifier_range_init_owner(&range, MMU_NOTIFY_EXCLUSIVE, 0, 3606b756a3b5SAlistair Popple vma->vm_mm, vmf->address & PAGE_MASK, 3607b756a3b5SAlistair Popple (vmf->address & PAGE_MASK) + PAGE_SIZE, NULL); 3608b756a3b5SAlistair Popple mmu_notifier_invalidate_range_start(&range); 3609b756a3b5SAlistair Popple 3610b756a3b5SAlistair Popple vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 3611b756a3b5SAlistair Popple &vmf->ptl); 3612c33c7948SRyan Roberts if (likely(vmf->pte && pte_same(ptep_get(vmf->pte), vmf->orig_pte))) 361319672a9eSMatthew Wilcox (Oracle) restore_exclusive_pte(vma, vmf->page, vmf->address, vmf->pte); 3614b756a3b5SAlistair Popple 36153db82b93SHugh Dickins if (vmf->pte) 3616b756a3b5SAlistair Popple pte_unmap_unlock(vmf->pte, vmf->ptl); 361719672a9eSMatthew Wilcox (Oracle) folio_unlock(folio); 36187c7b9629SAlistair Popple folio_put(folio); 3619b756a3b5SAlistair Popple 3620b756a3b5SAlistair Popple mmu_notifier_invalidate_range_end(&range); 3621b756a3b5SAlistair Popple return 0; 3622b756a3b5SAlistair Popple } 3623b756a3b5SAlistair Popple 3624a160e537SMatthew Wilcox (Oracle) static inline bool should_try_to_free_swap(struct folio *folio, 3625c145e0b4SDavid Hildenbrand struct vm_area_struct *vma, 3626c145e0b4SDavid Hildenbrand unsigned int fault_flags) 3627c145e0b4SDavid Hildenbrand { 3628a160e537SMatthew Wilcox (Oracle) if (!folio_test_swapcache(folio)) 3629c145e0b4SDavid Hildenbrand return false; 36309202d527SMatthew Wilcox (Oracle) if (mem_cgroup_swap_full(folio) || (vma->vm_flags & VM_LOCKED) || 3631a160e537SMatthew Wilcox (Oracle) folio_test_mlocked(folio)) 3632c145e0b4SDavid Hildenbrand return true; 3633c145e0b4SDavid Hildenbrand /* 3634c145e0b4SDavid Hildenbrand * If we want to map a page that's in the swapcache writable, we 3635c145e0b4SDavid Hildenbrand * have to detect via the refcount if we're really the exclusive 3636c145e0b4SDavid Hildenbrand * user. Try freeing the swapcache to get rid of the swapcache 3637c145e0b4SDavid Hildenbrand * reference only in case it's likely that we'll be the exlusive user. 3638c145e0b4SDavid Hildenbrand */ 3639a160e537SMatthew Wilcox (Oracle) return (fault_flags & FAULT_FLAG_WRITE) && !folio_test_ksm(folio) && 3640a160e537SMatthew Wilcox (Oracle) folio_ref_count(folio) == 2; 3641c145e0b4SDavid Hildenbrand } 3642c145e0b4SDavid Hildenbrand 36439c28a205SPeter Xu static vm_fault_t pte_marker_clear(struct vm_fault *vmf) 36449c28a205SPeter Xu { 36459c28a205SPeter Xu vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, 36469c28a205SPeter Xu vmf->address, &vmf->ptl); 36473db82b93SHugh Dickins if (!vmf->pte) 36483db82b93SHugh Dickins return 0; 36499c28a205SPeter Xu /* 36509c28a205SPeter Xu * Be careful so that we will only recover a special uffd-wp pte into a 36519c28a205SPeter Xu * none pte. Otherwise it means the pte could have changed, so retry. 36527e3ce3f8SPeter Xu * 36537e3ce3f8SPeter Xu * This should also cover the case where e.g. the pte changed 3654af19487fSAxel Rasmussen * quickly from a PTE_MARKER_UFFD_WP into PTE_MARKER_POISONED. 36557e3ce3f8SPeter Xu * So is_pte_marker() check is not enough to safely drop the pte. 36569c28a205SPeter Xu */ 3657c33c7948SRyan Roberts if (pte_same(vmf->orig_pte, ptep_get(vmf->pte))) 36589c28a205SPeter Xu pte_clear(vmf->vma->vm_mm, vmf->address, vmf->pte); 36599c28a205SPeter Xu pte_unmap_unlock(vmf->pte, vmf->ptl); 36609c28a205SPeter Xu return 0; 36619c28a205SPeter Xu } 36629c28a205SPeter Xu 36632bad466cSPeter Xu static vm_fault_t do_pte_missing(struct vm_fault *vmf) 36642bad466cSPeter Xu { 36652bad466cSPeter Xu if (vma_is_anonymous(vmf->vma)) 36662bad466cSPeter Xu return do_anonymous_page(vmf); 36672bad466cSPeter Xu else 36682bad466cSPeter Xu return do_fault(vmf); 36692bad466cSPeter Xu } 36702bad466cSPeter Xu 36719c28a205SPeter Xu /* 36729c28a205SPeter Xu * This is actually a page-missing access, but with uffd-wp special pte 36739c28a205SPeter Xu * installed. It means this pte was wr-protected before being unmapped. 36749c28a205SPeter Xu */ 36759c28a205SPeter Xu static vm_fault_t pte_marker_handle_uffd_wp(struct vm_fault *vmf) 36769c28a205SPeter Xu { 36779c28a205SPeter Xu /* 36789c28a205SPeter Xu * Just in case there're leftover special ptes even after the region 36797a079ba2SPeter Xu * got unregistered - we can simply clear them. 36809c28a205SPeter Xu */ 36812bad466cSPeter Xu if (unlikely(!userfaultfd_wp(vmf->vma))) 36829c28a205SPeter Xu return pte_marker_clear(vmf); 36839c28a205SPeter Xu 36842bad466cSPeter Xu return do_pte_missing(vmf); 36859c28a205SPeter Xu } 36869c28a205SPeter Xu 36875c041f5dSPeter Xu static vm_fault_t handle_pte_marker(struct vm_fault *vmf) 36885c041f5dSPeter Xu { 36895c041f5dSPeter Xu swp_entry_t entry = pte_to_swp_entry(vmf->orig_pte); 36905c041f5dSPeter Xu unsigned long marker = pte_marker_get(entry); 36915c041f5dSPeter Xu 36925c041f5dSPeter Xu /* 3693ca92ea3dSPeter Xu * PTE markers should never be empty. If anything weird happened, 3694ca92ea3dSPeter Xu * the best thing to do is to kill the process along with its mm. 36955c041f5dSPeter Xu */ 3696ca92ea3dSPeter Xu if (WARN_ON_ONCE(!marker)) 36975c041f5dSPeter Xu return VM_FAULT_SIGBUS; 36985c041f5dSPeter Xu 369915520a3fSPeter Xu /* Higher priority than uffd-wp when data corrupted */ 3700af19487fSAxel Rasmussen if (marker & PTE_MARKER_POISONED) 3701af19487fSAxel Rasmussen return VM_FAULT_HWPOISON; 370215520a3fSPeter Xu 37039c28a205SPeter Xu if (pte_marker_entry_uffd_wp(entry)) 37049c28a205SPeter Xu return pte_marker_handle_uffd_wp(vmf); 37059c28a205SPeter Xu 37069c28a205SPeter Xu /* This is an unknown pte marker */ 37079c28a205SPeter Xu return VM_FAULT_SIGBUS; 37085c041f5dSPeter Xu } 37095c041f5dSPeter Xu 3710b756a3b5SAlistair Popple /* 3711c1e8d7c6SMichel Lespinasse * We enter with non-exclusive mmap_lock (to exclude vma changes, 37128f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 37139a95f3cfSPaul Cassella * We return with pte unmapped and unlocked. 37149a95f3cfSPaul Cassella * 3715c1e8d7c6SMichel Lespinasse * We return with the mmap_lock locked or unlocked in the same cases 37169a95f3cfSPaul Cassella * as does filemap_fault(). 37171da177e4SLinus Torvalds */ 37182b740303SSouptick Joarder vm_fault_t do_swap_page(struct vm_fault *vmf) 37191da177e4SLinus Torvalds { 372082b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 3721d4f9565aSMatthew Wilcox (Oracle) struct folio *swapcache, *folio = NULL; 3722d4f9565aSMatthew Wilcox (Oracle) struct page *page; 37232799e775SMiaohe Lin struct swap_info_struct *si = NULL; 372414f9135dSDavid Hildenbrand rmap_t rmap_flags = RMAP_NONE; 37251493a191SDavid Hildenbrand bool exclusive = false; 372665500d23SHugh Dickins swp_entry_t entry; 37271da177e4SLinus Torvalds pte_t pte; 37282b740303SSouptick Joarder vm_fault_t ret = 0; 3729aae466b0SJoonsoo Kim void *shadow = NULL; 37301da177e4SLinus Torvalds 37312ca99358SPeter Xu if (!pte_unmap_same(vmf)) 37328f4e2101SHugh Dickins goto out; 373365500d23SHugh Dickins 37342994302bSJan Kara entry = pte_to_swp_entry(vmf->orig_pte); 3735d1737fdbSAndi Kleen if (unlikely(non_swap_entry(entry))) { 37360697212aSChristoph Lameter if (is_migration_entry(entry)) { 373782b0f8c3SJan Kara migration_entry_wait(vma->vm_mm, vmf->pmd, 373882b0f8c3SJan Kara vmf->address); 3739b756a3b5SAlistair Popple } else if (is_device_exclusive_entry(entry)) { 3740b756a3b5SAlistair Popple vmf->page = pfn_swap_entry_to_page(entry); 3741b756a3b5SAlistair Popple ret = remove_device_exclusive_entry(vmf); 37425042db43SJérôme Glisse } else if (is_device_private_entry(entry)) { 37431235ccd0SSuren Baghdasaryan if (vmf->flags & FAULT_FLAG_VMA_LOCK) { 37441235ccd0SSuren Baghdasaryan /* 37451235ccd0SSuren Baghdasaryan * migrate_to_ram is not yet ready to operate 37461235ccd0SSuren Baghdasaryan * under VMA lock. 37471235ccd0SSuren Baghdasaryan */ 37481235ccd0SSuren Baghdasaryan vma_end_read(vma); 37491235ccd0SSuren Baghdasaryan ret = VM_FAULT_RETRY; 37501235ccd0SSuren Baghdasaryan goto out; 37511235ccd0SSuren Baghdasaryan } 37521235ccd0SSuren Baghdasaryan 3753af5cdaf8SAlistair Popple vmf->page = pfn_swap_entry_to_page(entry); 375416ce101dSAlistair Popple vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 375516ce101dSAlistair Popple vmf->address, &vmf->ptl); 37563db82b93SHugh Dickins if (unlikely(!vmf->pte || 3757c33c7948SRyan Roberts !pte_same(ptep_get(vmf->pte), 3758c33c7948SRyan Roberts vmf->orig_pte))) 37593b65f437SRyan Roberts goto unlock; 376016ce101dSAlistair Popple 376116ce101dSAlistair Popple /* 376216ce101dSAlistair Popple * Get a page reference while we know the page can't be 376316ce101dSAlistair Popple * freed. 376416ce101dSAlistair Popple */ 376516ce101dSAlistair Popple get_page(vmf->page); 376616ce101dSAlistair Popple pte_unmap_unlock(vmf->pte, vmf->ptl); 37674a955bedSAlistair Popple ret = vmf->page->pgmap->ops->migrate_to_ram(vmf); 376816ce101dSAlistair Popple put_page(vmf->page); 3769d1737fdbSAndi Kleen } else if (is_hwpoison_entry(entry)) { 3770d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 37715c041f5dSPeter Xu } else if (is_pte_marker_entry(entry)) { 37725c041f5dSPeter Xu ret = handle_pte_marker(vmf); 3773d1737fdbSAndi Kleen } else { 37742994302bSJan Kara print_bad_pte(vma, vmf->address, vmf->orig_pte, NULL); 3775d99be1a8SHugh Dickins ret = VM_FAULT_SIGBUS; 3776d1737fdbSAndi Kleen } 37770697212aSChristoph Lameter goto out; 37780697212aSChristoph Lameter } 37790bcac06fSMinchan Kim 37802799e775SMiaohe Lin /* Prevent swapoff from happening to us. */ 37812799e775SMiaohe Lin si = get_swap_device(entry); 37822799e775SMiaohe Lin if (unlikely(!si)) 37832799e775SMiaohe Lin goto out; 37840bcac06fSMinchan Kim 37855a423081SMatthew Wilcox (Oracle) folio = swap_cache_get_folio(entry, vma, vmf->address); 37865a423081SMatthew Wilcox (Oracle) if (folio) 37875a423081SMatthew Wilcox (Oracle) page = folio_file_page(folio, swp_offset(entry)); 3788d4f9565aSMatthew Wilcox (Oracle) swapcache = folio; 3789f8020772SMinchan Kim 3790d4f9565aSMatthew Wilcox (Oracle) if (!folio) { 3791a449bf58SQian Cai if (data_race(si->flags & SWP_SYNCHRONOUS_IO) && 3792eb085574SHuang Ying __swap_count(entry) == 1) { 37930bcac06fSMinchan Kim /* skip swapcache */ 379463ad4addSMatthew Wilcox (Oracle) folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, 379563ad4addSMatthew Wilcox (Oracle) vma, vmf->address, false); 379663ad4addSMatthew Wilcox (Oracle) page = &folio->page; 379763ad4addSMatthew Wilcox (Oracle) if (folio) { 379863ad4addSMatthew Wilcox (Oracle) __folio_set_locked(folio); 379963ad4addSMatthew Wilcox (Oracle) __folio_set_swapbacked(folio); 38004c6355b2SJohannes Weiner 380165995918SMatthew Wilcox (Oracle) if (mem_cgroup_swapin_charge_folio(folio, 380263ad4addSMatthew Wilcox (Oracle) vma->vm_mm, GFP_KERNEL, 380363ad4addSMatthew Wilcox (Oracle) entry)) { 3804545b1b07SMichal Hocko ret = VM_FAULT_OOM; 38054c6355b2SJohannes Weiner goto out_page; 3806545b1b07SMichal Hocko } 38070add0c77SShakeel Butt mem_cgroup_swapin_uncharge_swap(entry); 38084c6355b2SJohannes Weiner 3809aae466b0SJoonsoo Kim shadow = get_shadow_from_swap_cache(entry); 3810aae466b0SJoonsoo Kim if (shadow) 381163ad4addSMatthew Wilcox (Oracle) workingset_refault(folio, shadow); 38120076f029SJoonsoo Kim 381363ad4addSMatthew Wilcox (Oracle) folio_add_lru(folio); 38140add0c77SShakeel Butt 38150add0c77SShakeel Butt /* To provide entry to swap_readpage() */ 38163d2c9087SDavid Hildenbrand folio->swap = entry; 38175169b844SNeilBrown swap_readpage(page, true, NULL); 381863ad4addSMatthew Wilcox (Oracle) folio->private = NULL; 38190bcac06fSMinchan Kim } 3820aa8d22a1SMinchan Kim } else { 3821e9e9b7ecSMinchan Kim page = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, 3822e9e9b7ecSMinchan Kim vmf); 382363ad4addSMatthew Wilcox (Oracle) if (page) 382463ad4addSMatthew Wilcox (Oracle) folio = page_folio(page); 3825d4f9565aSMatthew Wilcox (Oracle) swapcache = folio; 38260bcac06fSMinchan Kim } 38270bcac06fSMinchan Kim 3828d4f9565aSMatthew Wilcox (Oracle) if (!folio) { 38291da177e4SLinus Torvalds /* 38308f4e2101SHugh Dickins * Back out if somebody else faulted in this pte 38318f4e2101SHugh Dickins * while we released the pte lock. 38321da177e4SLinus Torvalds */ 383382b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 383482b0f8c3SJan Kara vmf->address, &vmf->ptl); 3835c33c7948SRyan Roberts if (likely(vmf->pte && 3836c33c7948SRyan Roberts pte_same(ptep_get(vmf->pte), vmf->orig_pte))) 38371da177e4SLinus Torvalds ret = VM_FAULT_OOM; 383865500d23SHugh Dickins goto unlock; 38391da177e4SLinus Torvalds } 38401da177e4SLinus Torvalds 38411da177e4SLinus Torvalds /* Had to read the page from swap area: Major fault */ 38421da177e4SLinus Torvalds ret = VM_FAULT_MAJOR; 3843f8891e5eSChristoph Lameter count_vm_event(PGMAJFAULT); 38442262185cSRoman Gushchin count_memcg_event_mm(vma->vm_mm, PGMAJFAULT); 3845d1737fdbSAndi Kleen } else if (PageHWPoison(page)) { 384671f72525SWu Fengguang /* 384771f72525SWu Fengguang * hwpoisoned dirty swapcache pages are kept for killing 384871f72525SWu Fengguang * owner processes (which may be unknown at hwpoison time) 384971f72525SWu Fengguang */ 3850d1737fdbSAndi Kleen ret = VM_FAULT_HWPOISON; 38514779cb31SAndi Kleen goto out_release; 38521da177e4SLinus Torvalds } 38531da177e4SLinus Torvalds 3854fdc724d6SSuren Baghdasaryan ret |= folio_lock_or_retry(folio, vmf); 3855fdc724d6SSuren Baghdasaryan if (ret & VM_FAULT_RETRY) 3856d065bd81SMichel Lespinasse goto out_release; 38571da177e4SLinus Torvalds 385884d60fddSDavid Hildenbrand if (swapcache) { 38594969c119SAndrea Arcangeli /* 38603b344157SMatthew Wilcox (Oracle) * Make sure folio_free_swap() or swapoff did not release the 386184d60fddSDavid Hildenbrand * swapcache from under us. The page pin, and pte_same test 386284d60fddSDavid Hildenbrand * below, are not enough to exclude that. Even if it is still 386384d60fddSDavid Hildenbrand * swapcache, we need to check that the page's swap has not 386484d60fddSDavid Hildenbrand * changed. 38654969c119SAndrea Arcangeli */ 386663ad4addSMatthew Wilcox (Oracle) if (unlikely(!folio_test_swapcache(folio) || 3867cfeed8ffSDavid Hildenbrand page_swap_entry(page).val != entry.val)) 38684969c119SAndrea Arcangeli goto out_page; 38694969c119SAndrea Arcangeli 387084d60fddSDavid Hildenbrand /* 387184d60fddSDavid Hildenbrand * KSM sometimes has to copy on read faults, for example, if 387284d60fddSDavid Hildenbrand * page->index of !PageKSM() pages would be nonlinear inside the 387384d60fddSDavid Hildenbrand * anon VMA -- PageKSM() is lost on actual swapout. 387484d60fddSDavid Hildenbrand */ 387582b0f8c3SJan Kara page = ksm_might_need_to_copy(page, vma, vmf->address); 38764969c119SAndrea Arcangeli if (unlikely(!page)) { 38775ad64688SHugh Dickins ret = VM_FAULT_OOM; 38784969c119SAndrea Arcangeli goto out_page; 38796b970599SKefeng Wang } else if (unlikely(PTR_ERR(page) == -EHWPOISON)) { 38806b970599SKefeng Wang ret = VM_FAULT_HWPOISON; 38816b970599SKefeng Wang goto out_page; 38824969c119SAndrea Arcangeli } 388363ad4addSMatthew Wilcox (Oracle) folio = page_folio(page); 3884c145e0b4SDavid Hildenbrand 3885c145e0b4SDavid Hildenbrand /* 3886c145e0b4SDavid Hildenbrand * If we want to map a page that's in the swapcache writable, we 3887c145e0b4SDavid Hildenbrand * have to detect via the refcount if we're really the exclusive 3888c145e0b4SDavid Hildenbrand * owner. Try removing the extra reference from the local LRU 38891fec6890SMatthew Wilcox (Oracle) * caches if required. 3890c145e0b4SDavid Hildenbrand */ 3891d4f9565aSMatthew Wilcox (Oracle) if ((vmf->flags & FAULT_FLAG_WRITE) && folio == swapcache && 389263ad4addSMatthew Wilcox (Oracle) !folio_test_ksm(folio) && !folio_test_lru(folio)) 3893c145e0b4SDavid Hildenbrand lru_add_drain(); 389484d60fddSDavid Hildenbrand } 38955ad64688SHugh Dickins 38964231f842SKefeng Wang folio_throttle_swaprate(folio, GFP_KERNEL); 3897073e587eSKAMEZAWA Hiroyuki 38981da177e4SLinus Torvalds /* 38998f4e2101SHugh Dickins * Back out if somebody else already faulted in this pte. 39001da177e4SLinus Torvalds */ 390182b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 390282b0f8c3SJan Kara &vmf->ptl); 3903c33c7948SRyan Roberts if (unlikely(!vmf->pte || !pte_same(ptep_get(vmf->pte), vmf->orig_pte))) 3904b8107480SKirill Korotaev goto out_nomap; 3905b8107480SKirill Korotaev 390663ad4addSMatthew Wilcox (Oracle) if (unlikely(!folio_test_uptodate(folio))) { 3907b8107480SKirill Korotaev ret = VM_FAULT_SIGBUS; 3908b8107480SKirill Korotaev goto out_nomap; 39091da177e4SLinus Torvalds } 39101da177e4SLinus Torvalds 39118c7c6e34SKAMEZAWA Hiroyuki /* 391278fbe906SDavid Hildenbrand * PG_anon_exclusive reuses PG_mappedtodisk for anon pages. A swap pte 391378fbe906SDavid Hildenbrand * must never point at an anonymous page in the swapcache that is 391478fbe906SDavid Hildenbrand * PG_anon_exclusive. Sanity check that this holds and especially, that 391578fbe906SDavid Hildenbrand * no filesystem set PG_mappedtodisk on a page in the swapcache. Sanity 391678fbe906SDavid Hildenbrand * check after taking the PT lock and making sure that nobody 391778fbe906SDavid Hildenbrand * concurrently faulted in this page and set PG_anon_exclusive. 391878fbe906SDavid Hildenbrand */ 391963ad4addSMatthew Wilcox (Oracle) BUG_ON(!folio_test_anon(folio) && folio_test_mappedtodisk(folio)); 392063ad4addSMatthew Wilcox (Oracle) BUG_ON(folio_test_anon(folio) && PageAnonExclusive(page)); 392178fbe906SDavid Hildenbrand 392278fbe906SDavid Hildenbrand /* 39231493a191SDavid Hildenbrand * Check under PT lock (to protect against concurrent fork() sharing 39241493a191SDavid Hildenbrand * the swap entry concurrently) for certainly exclusive pages. 39251493a191SDavid Hildenbrand */ 392663ad4addSMatthew Wilcox (Oracle) if (!folio_test_ksm(folio)) { 39271493a191SDavid Hildenbrand exclusive = pte_swp_exclusive(vmf->orig_pte); 3928d4f9565aSMatthew Wilcox (Oracle) if (folio != swapcache) { 39291493a191SDavid Hildenbrand /* 39301493a191SDavid Hildenbrand * We have a fresh page that is not exposed to the 39311493a191SDavid Hildenbrand * swapcache -> certainly exclusive. 39321493a191SDavid Hildenbrand */ 39331493a191SDavid Hildenbrand exclusive = true; 393463ad4addSMatthew Wilcox (Oracle) } else if (exclusive && folio_test_writeback(folio) && 3935eacde327SMiaohe Lin data_race(si->flags & SWP_STABLE_WRITES)) { 39361493a191SDavid Hildenbrand /* 39371493a191SDavid Hildenbrand * This is tricky: not all swap backends support 39381493a191SDavid Hildenbrand * concurrent page modifications while under writeback. 39391493a191SDavid Hildenbrand * 39401493a191SDavid Hildenbrand * So if we stumble over such a page in the swapcache 39411493a191SDavid Hildenbrand * we must not set the page exclusive, otherwise we can 39421493a191SDavid Hildenbrand * map it writable without further checks and modify it 39431493a191SDavid Hildenbrand * while still under writeback. 39441493a191SDavid Hildenbrand * 39451493a191SDavid Hildenbrand * For these problematic swap backends, simply drop the 39461493a191SDavid Hildenbrand * exclusive marker: this is perfectly fine as we start 39471493a191SDavid Hildenbrand * writeback only if we fully unmapped the page and 39481493a191SDavid Hildenbrand * there are no unexpected references on the page after 39491493a191SDavid Hildenbrand * unmapping succeeded. After fully unmapped, no 39501493a191SDavid Hildenbrand * further GUP references (FOLL_GET and FOLL_PIN) can 39511493a191SDavid Hildenbrand * appear, so dropping the exclusive marker and mapping 39521493a191SDavid Hildenbrand * it only R/O is fine. 39531493a191SDavid Hildenbrand */ 39541493a191SDavid Hildenbrand exclusive = false; 39551493a191SDavid Hildenbrand } 39561493a191SDavid Hildenbrand } 39571493a191SDavid Hildenbrand 39581493a191SDavid Hildenbrand /* 39596dca4ac6SPeter Collingbourne * Some architectures may have to restore extra metadata to the page 39606dca4ac6SPeter Collingbourne * when reading from swap. This metadata may be indexed by swap entry 39616dca4ac6SPeter Collingbourne * so this must be called before swap_free(). 39626dca4ac6SPeter Collingbourne */ 39636dca4ac6SPeter Collingbourne arch_swap_restore(entry, folio); 39646dca4ac6SPeter Collingbourne 39656dca4ac6SPeter Collingbourne /* 3966c145e0b4SDavid Hildenbrand * Remove the swap entry and conditionally try to free up the swapcache. 3967c145e0b4SDavid Hildenbrand * We're already holding a reference on the page but haven't mapped it 3968c145e0b4SDavid Hildenbrand * yet. 39698c7c6e34SKAMEZAWA Hiroyuki */ 3970c145e0b4SDavid Hildenbrand swap_free(entry); 3971a160e537SMatthew Wilcox (Oracle) if (should_try_to_free_swap(folio, vma, vmf->flags)) 3972a160e537SMatthew Wilcox (Oracle) folio_free_swap(folio); 39731da177e4SLinus Torvalds 3974f1a79412SShakeel Butt inc_mm_counter(vma->vm_mm, MM_ANONPAGES); 3975f1a79412SShakeel Butt dec_mm_counter(vma->vm_mm, MM_SWAPENTS); 39761da177e4SLinus Torvalds pte = mk_pte(page, vma->vm_page_prot); 3977c145e0b4SDavid Hildenbrand 3978c145e0b4SDavid Hildenbrand /* 39791493a191SDavid Hildenbrand * Same logic as in do_wp_page(); however, optimize for pages that are 39801493a191SDavid Hildenbrand * certainly not shared either because we just allocated them without 39811493a191SDavid Hildenbrand * exposing them to the swapcache or because the swap entry indicates 39821493a191SDavid Hildenbrand * exclusivity. 3983c145e0b4SDavid Hildenbrand */ 398463ad4addSMatthew Wilcox (Oracle) if (!folio_test_ksm(folio) && 398563ad4addSMatthew Wilcox (Oracle) (exclusive || folio_ref_count(folio) == 1)) { 39866c287605SDavid Hildenbrand if (vmf->flags & FAULT_FLAG_WRITE) { 39871da177e4SLinus Torvalds pte = maybe_mkwrite(pte_mkdirty(pte), vma); 398882b0f8c3SJan Kara vmf->flags &= ~FAULT_FLAG_WRITE; 39896c287605SDavid Hildenbrand } 399014f9135dSDavid Hildenbrand rmap_flags |= RMAP_EXCLUSIVE; 39911da177e4SLinus Torvalds } 39921da177e4SLinus Torvalds flush_icache_page(vma, page); 39932994302bSJan Kara if (pte_swp_soft_dirty(vmf->orig_pte)) 3994179ef71cSCyrill Gorcunov pte = pte_mksoft_dirty(pte); 3995f1eb1bacSPeter Xu if (pte_swp_uffd_wp(vmf->orig_pte)) 3996f45ec5ffSPeter Xu pte = pte_mkuffd_wp(pte); 39972994302bSJan Kara vmf->orig_pte = pte; 39980bcac06fSMinchan Kim 39990bcac06fSMinchan Kim /* ksm created a completely new copy */ 4000d4f9565aSMatthew Wilcox (Oracle) if (unlikely(folio != swapcache && swapcache)) { 400140f2bbf7SDavid Hildenbrand page_add_new_anon_rmap(page, vma, vmf->address); 400263ad4addSMatthew Wilcox (Oracle) folio_add_lru_vma(folio, vma); 40030bcac06fSMinchan Kim } else { 4004f1e2db12SDavid Hildenbrand page_add_anon_rmap(page, vma, vmf->address, rmap_flags); 400500501b53SJohannes Weiner } 40061da177e4SLinus Torvalds 400763ad4addSMatthew Wilcox (Oracle) VM_BUG_ON(!folio_test_anon(folio) || 400863ad4addSMatthew Wilcox (Oracle) (pte_write(pte) && !PageAnonExclusive(page))); 40091eba86c0SPasha Tatashin set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte); 40101eba86c0SPasha Tatashin arch_do_swap_page(vma->vm_mm, vma, vmf->address, pte, vmf->orig_pte); 40111eba86c0SPasha Tatashin 401263ad4addSMatthew Wilcox (Oracle) folio_unlock(folio); 4013d4f9565aSMatthew Wilcox (Oracle) if (folio != swapcache && swapcache) { 40144969c119SAndrea Arcangeli /* 40154969c119SAndrea Arcangeli * Hold the lock to avoid the swap entry to be reused 40164969c119SAndrea Arcangeli * until we take the PT lock for the pte_same() check 40174969c119SAndrea Arcangeli * (to avoid false positives from pte_same). For 40184969c119SAndrea Arcangeli * further safety release the lock after the swap_free 40194969c119SAndrea Arcangeli * so that the swap count won't change under a 40204969c119SAndrea Arcangeli * parallel locked swapcache. 40214969c119SAndrea Arcangeli */ 4022d4f9565aSMatthew Wilcox (Oracle) folio_unlock(swapcache); 4023d4f9565aSMatthew Wilcox (Oracle) folio_put(swapcache); 40244969c119SAndrea Arcangeli } 4025c475a8abSHugh Dickins 402682b0f8c3SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) { 40272994302bSJan Kara ret |= do_wp_page(vmf); 402861469f1dSHugh Dickins if (ret & VM_FAULT_ERROR) 402961469f1dSHugh Dickins ret &= VM_FAULT_ERROR; 40301da177e4SLinus Torvalds goto out; 40311da177e4SLinus Torvalds } 40321da177e4SLinus Torvalds 40331da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 40345003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vma, vmf->address, vmf->pte, 1); 403565500d23SHugh Dickins unlock: 40363db82b93SHugh Dickins if (vmf->pte) 403782b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 40381da177e4SLinus Torvalds out: 40392799e775SMiaohe Lin if (si) 40402799e775SMiaohe Lin put_swap_device(si); 40411da177e4SLinus Torvalds return ret; 4042b8107480SKirill Korotaev out_nomap: 40433db82b93SHugh Dickins if (vmf->pte) 404482b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 4045bc43f75cSJohannes Weiner out_page: 404663ad4addSMatthew Wilcox (Oracle) folio_unlock(folio); 40474779cb31SAndi Kleen out_release: 404863ad4addSMatthew Wilcox (Oracle) folio_put(folio); 4049d4f9565aSMatthew Wilcox (Oracle) if (folio != swapcache && swapcache) { 4050d4f9565aSMatthew Wilcox (Oracle) folio_unlock(swapcache); 4051d4f9565aSMatthew Wilcox (Oracle) folio_put(swapcache); 40524969c119SAndrea Arcangeli } 40532799e775SMiaohe Lin if (si) 40542799e775SMiaohe Lin put_swap_device(si); 405565500d23SHugh Dickins return ret; 40561da177e4SLinus Torvalds } 40571da177e4SLinus Torvalds 40581da177e4SLinus Torvalds /* 4059c1e8d7c6SMichel Lespinasse * We enter with non-exclusive mmap_lock (to exclude vma changes, 40608f4e2101SHugh Dickins * but allow concurrent faults), and pte mapped but not yet locked. 4061c1e8d7c6SMichel Lespinasse * We return with mmap_lock still held, but pte unmapped and unlocked. 40621da177e4SLinus Torvalds */ 40632b740303SSouptick Joarder static vm_fault_t do_anonymous_page(struct vm_fault *vmf) 40641da177e4SLinus Torvalds { 40652bad466cSPeter Xu bool uffd_wp = vmf_orig_pte_uffd_wp(vmf); 406682b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 40676bc56a4dSMatthew Wilcox (Oracle) struct folio *folio; 40682b740303SSouptick Joarder vm_fault_t ret = 0; 40691da177e4SLinus Torvalds pte_t entry; 40701da177e4SLinus Torvalds 40716b7339f4SKirill A. Shutemov /* File mapping without ->vm_ops ? */ 40726b7339f4SKirill A. Shutemov if (vma->vm_flags & VM_SHARED) 40736b7339f4SKirill A. Shutemov return VM_FAULT_SIGBUS; 40746b7339f4SKirill A. Shutemov 40757267ec00SKirill A. Shutemov /* 40763db82b93SHugh Dickins * Use pte_alloc() instead of pte_alloc_map(), so that OOM can 40773db82b93SHugh Dickins * be distinguished from a transient failure of pte_offset_map(). 40787267ec00SKirill A. Shutemov */ 40794cf58924SJoel Fernandes (Google) if (pte_alloc(vma->vm_mm, vmf->pmd)) 40807267ec00SKirill A. Shutemov return VM_FAULT_OOM; 40817267ec00SKirill A. Shutemov 408211ac5524SLinus Torvalds /* Use the zero-page for reads */ 408382b0f8c3SJan Kara if (!(vmf->flags & FAULT_FLAG_WRITE) && 4084bae473a4SKirill A. Shutemov !mm_forbids_zeropage(vma->vm_mm)) { 408582b0f8c3SJan Kara entry = pte_mkspecial(pfn_pte(my_zero_pfn(vmf->address), 408662eede62SHugh Dickins vma->vm_page_prot)); 408782b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 408882b0f8c3SJan Kara vmf->address, &vmf->ptl); 40893db82b93SHugh Dickins if (!vmf->pte) 40903db82b93SHugh Dickins goto unlock; 40912bad466cSPeter Xu if (vmf_pte_changed(vmf)) { 40927df67697SBibo Mao update_mmu_tlb(vma, vmf->address, vmf->pte); 4093a13ea5b7SHugh Dickins goto unlock; 40947df67697SBibo Mao } 40956b31d595SMichal Hocko ret = check_stable_address_space(vma->vm_mm); 40966b31d595SMichal Hocko if (ret) 40976b31d595SMichal Hocko goto unlock; 40986b251fc9SAndrea Arcangeli /* Deliver the page fault to userland, check inside PT lock */ 40996b251fc9SAndrea Arcangeli if (userfaultfd_missing(vma)) { 410082b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 410182b0f8c3SJan Kara return handle_userfault(vmf, VM_UFFD_MISSING); 41026b251fc9SAndrea Arcangeli } 4103a13ea5b7SHugh Dickins goto setpte; 4104a13ea5b7SHugh Dickins } 4105a13ea5b7SHugh Dickins 41061da177e4SLinus Torvalds /* Allocate our own private page. */ 41071da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 410865500d23SHugh Dickins goto oom; 41096bc56a4dSMatthew Wilcox (Oracle) folio = vma_alloc_zeroed_movable_folio(vma, vmf->address); 41106bc56a4dSMatthew Wilcox (Oracle) if (!folio) 411165500d23SHugh Dickins goto oom; 4112eb3c24f3SMel Gorman 41136bc56a4dSMatthew Wilcox (Oracle) if (mem_cgroup_charge(folio, vma->vm_mm, GFP_KERNEL)) 4114eb3c24f3SMel Gorman goto oom_free_page; 4115e2bf3e2cSKefeng Wang folio_throttle_swaprate(folio, GFP_KERNEL); 4116eb3c24f3SMel Gorman 411752f37629SMinchan Kim /* 4118cb3184deSMatthew Wilcox (Oracle) * The memory barrier inside __folio_mark_uptodate makes sure that 4119f4f5329dSWei Yang * preceding stores to the page contents become visible before 412052f37629SMinchan Kim * the set_pte_at() write. 412152f37629SMinchan Kim */ 4122cb3184deSMatthew Wilcox (Oracle) __folio_mark_uptodate(folio); 41231da177e4SLinus Torvalds 4124cb3184deSMatthew Wilcox (Oracle) entry = mk_pte(&folio->page, vma->vm_page_prot); 412550c25ee9SThomas Bogendoerfer entry = pte_sw_mkyoung(entry); 41261ac0cb5dSHugh Dickins if (vma->vm_flags & VM_WRITE) 41271ac0cb5dSHugh Dickins entry = pte_mkwrite(pte_mkdirty(entry)); 41288f4e2101SHugh Dickins 412982b0f8c3SJan Kara vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, 413082b0f8c3SJan Kara &vmf->ptl); 41313db82b93SHugh Dickins if (!vmf->pte) 41323db82b93SHugh Dickins goto release; 41332bad466cSPeter Xu if (vmf_pte_changed(vmf)) { 4134bce8cb3cSQi Zheng update_mmu_tlb(vma, vmf->address, vmf->pte); 41358f4e2101SHugh Dickins goto release; 41367df67697SBibo Mao } 41379ba69294SHugh Dickins 41386b31d595SMichal Hocko ret = check_stable_address_space(vma->vm_mm); 41396b31d595SMichal Hocko if (ret) 41406b31d595SMichal Hocko goto release; 41416b31d595SMichal Hocko 41426b251fc9SAndrea Arcangeli /* Deliver the page fault to userland, check inside PT lock */ 41436b251fc9SAndrea Arcangeli if (userfaultfd_missing(vma)) { 414482b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 4145cb3184deSMatthew Wilcox (Oracle) folio_put(folio); 414682b0f8c3SJan Kara return handle_userfault(vmf, VM_UFFD_MISSING); 41476b251fc9SAndrea Arcangeli } 41486b251fc9SAndrea Arcangeli 4149f1a79412SShakeel Butt inc_mm_counter(vma->vm_mm, MM_ANONPAGES); 4150cb3184deSMatthew Wilcox (Oracle) folio_add_new_anon_rmap(folio, vma, vmf->address); 4151cb3184deSMatthew Wilcox (Oracle) folio_add_lru_vma(folio, vma); 4152a13ea5b7SHugh Dickins setpte: 41532bad466cSPeter Xu if (uffd_wp) 41542bad466cSPeter Xu entry = pte_mkuffd_wp(entry); 415582b0f8c3SJan Kara set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry); 41561da177e4SLinus Torvalds 41571da177e4SLinus Torvalds /* No need to invalidate - it was non-present before */ 41585003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vma, vmf->address, vmf->pte, 1); 415965500d23SHugh Dickins unlock: 41603db82b93SHugh Dickins if (vmf->pte) 416182b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 41626b31d595SMichal Hocko return ret; 41638f4e2101SHugh Dickins release: 4164cb3184deSMatthew Wilcox (Oracle) folio_put(folio); 41658f4e2101SHugh Dickins goto unlock; 41668a9f3ccdSBalbir Singh oom_free_page: 4167cb3184deSMatthew Wilcox (Oracle) folio_put(folio); 416865500d23SHugh Dickins oom: 41691da177e4SLinus Torvalds return VM_FAULT_OOM; 41701da177e4SLinus Torvalds } 41711da177e4SLinus Torvalds 41729a95f3cfSPaul Cassella /* 4173c1e8d7c6SMichel Lespinasse * The mmap_lock must have been held on entry, and may have been 41749a95f3cfSPaul Cassella * released depending on flags and vma->vm_ops->fault() return value. 41759a95f3cfSPaul Cassella * See filemap_fault() and __lock_page_retry(). 41769a95f3cfSPaul Cassella */ 41772b740303SSouptick Joarder static vm_fault_t __do_fault(struct vm_fault *vmf) 41787eae74afSKirill A. Shutemov { 417982b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 41802b740303SSouptick Joarder vm_fault_t ret; 41817eae74afSKirill A. Shutemov 418263f3655fSMichal Hocko /* 418363f3655fSMichal Hocko * Preallocate pte before we take page_lock because this might lead to 418463f3655fSMichal Hocko * deadlocks for memcg reclaim which waits for pages under writeback: 418563f3655fSMichal Hocko * lock_page(A) 418663f3655fSMichal Hocko * SetPageWriteback(A) 418763f3655fSMichal Hocko * unlock_page(A) 418863f3655fSMichal Hocko * lock_page(B) 418963f3655fSMichal Hocko * lock_page(B) 4190d383807aSYanfei Xu * pte_alloc_one 419163f3655fSMichal Hocko * shrink_page_list 419263f3655fSMichal Hocko * wait_on_page_writeback(A) 419363f3655fSMichal Hocko * SetPageWriteback(B) 419463f3655fSMichal Hocko * unlock_page(B) 419563f3655fSMichal Hocko * # flush A, B to clear the writeback 419663f3655fSMichal Hocko */ 419763f3655fSMichal Hocko if (pmd_none(*vmf->pmd) && !vmf->prealloc_pte) { 4198a7069ee3SYanfei Xu vmf->prealloc_pte = pte_alloc_one(vma->vm_mm); 419963f3655fSMichal Hocko if (!vmf->prealloc_pte) 420063f3655fSMichal Hocko return VM_FAULT_OOM; 420163f3655fSMichal Hocko } 420263f3655fSMichal Hocko 420311bac800SDave Jiang ret = vma->vm_ops->fault(vmf); 42043917048dSJan Kara if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY | 4205b1aa812bSJan Kara VM_FAULT_DONE_COW))) 42067eae74afSKirill A. Shutemov return ret; 42077eae74afSKirill A. Shutemov 4208667240e0SJan Kara if (unlikely(PageHWPoison(vmf->page))) { 42093149c79fSRik van Riel struct page *page = vmf->page; 4210e53ac737SRik van Riel vm_fault_t poisonret = VM_FAULT_HWPOISON; 4211e53ac737SRik van Riel if (ret & VM_FAULT_LOCKED) { 42123149c79fSRik van Riel if (page_mapped(page)) 42133149c79fSRik van Riel unmap_mapping_pages(page_mapping(page), 42143149c79fSRik van Riel page->index, 1, false); 4215e53ac737SRik van Riel /* Retry if a clean page was removed from the cache. */ 42163149c79fSRik van Riel if (invalidate_inode_page(page)) 42173149c79fSRik van Riel poisonret = VM_FAULT_NOPAGE; 42183149c79fSRik van Riel unlock_page(page); 4219e53ac737SRik van Riel } 42203149c79fSRik van Riel put_page(page); 4221936ca80dSJan Kara vmf->page = NULL; 4222e53ac737SRik van Riel return poisonret; 42237eae74afSKirill A. Shutemov } 42247eae74afSKirill A. Shutemov 42257eae74afSKirill A. Shutemov if (unlikely(!(ret & VM_FAULT_LOCKED))) 4226667240e0SJan Kara lock_page(vmf->page); 42277eae74afSKirill A. Shutemov else 4228667240e0SJan Kara VM_BUG_ON_PAGE(!PageLocked(vmf->page), vmf->page); 42297eae74afSKirill A. Shutemov 42307eae74afSKirill A. Shutemov return ret; 42317eae74afSKirill A. Shutemov } 42327eae74afSKirill A. Shutemov 4233396bcc52SMatthew Wilcox (Oracle) #ifdef CONFIG_TRANSPARENT_HUGEPAGE 423482b0f8c3SJan Kara static void deposit_prealloc_pte(struct vm_fault *vmf) 4235953c66c2SAneesh Kumar K.V { 423682b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 4237953c66c2SAneesh Kumar K.V 423882b0f8c3SJan Kara pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, vmf->prealloc_pte); 4239953c66c2SAneesh Kumar K.V /* 4240953c66c2SAneesh Kumar K.V * We are going to consume the prealloc table, 4241953c66c2SAneesh Kumar K.V * count that as nr_ptes. 4242953c66c2SAneesh Kumar K.V */ 4243c4812909SKirill A. Shutemov mm_inc_nr_ptes(vma->vm_mm); 42447f2b6ce8STobin C Harding vmf->prealloc_pte = NULL; 4245953c66c2SAneesh Kumar K.V } 4246953c66c2SAneesh Kumar K.V 4247f9ce0be7SKirill A. Shutemov vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page) 424810102459SKirill A. Shutemov { 424982b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 425082b0f8c3SJan Kara bool write = vmf->flags & FAULT_FLAG_WRITE; 425182b0f8c3SJan Kara unsigned long haddr = vmf->address & HPAGE_PMD_MASK; 425210102459SKirill A. Shutemov pmd_t entry; 4253d01ac3c3SMatthew Wilcox (Oracle) vm_fault_t ret = VM_FAULT_FALLBACK; 425410102459SKirill A. Shutemov 425510102459SKirill A. Shutemov if (!transhuge_vma_suitable(vma, haddr)) 4256d01ac3c3SMatthew Wilcox (Oracle) return ret; 425710102459SKirill A. Shutemov 425810102459SKirill A. Shutemov page = compound_head(page); 4259d01ac3c3SMatthew Wilcox (Oracle) if (compound_order(page) != HPAGE_PMD_ORDER) 4260d01ac3c3SMatthew Wilcox (Oracle) return ret; 426110102459SKirill A. Shutemov 4262953c66c2SAneesh Kumar K.V /* 4263eac96c3eSYang Shi * Just backoff if any subpage of a THP is corrupted otherwise 4264eac96c3eSYang Shi * the corrupted page may mapped by PMD silently to escape the 4265eac96c3eSYang Shi * check. This kind of THP just can be PTE mapped. Access to 4266eac96c3eSYang Shi * the corrupted subpage should trigger SIGBUS as expected. 4267eac96c3eSYang Shi */ 4268eac96c3eSYang Shi if (unlikely(PageHasHWPoisoned(page))) 4269eac96c3eSYang Shi return ret; 4270eac96c3eSYang Shi 4271eac96c3eSYang Shi /* 4272f0953a1bSIngo Molnar * Archs like ppc64 need additional space to store information 4273953c66c2SAneesh Kumar K.V * related to pte entry. Use the preallocated table for that. 4274953c66c2SAneesh Kumar K.V */ 427582b0f8c3SJan Kara if (arch_needs_pgtable_deposit() && !vmf->prealloc_pte) { 42764cf58924SJoel Fernandes (Google) vmf->prealloc_pte = pte_alloc_one(vma->vm_mm); 427782b0f8c3SJan Kara if (!vmf->prealloc_pte) 4278953c66c2SAneesh Kumar K.V return VM_FAULT_OOM; 4279953c66c2SAneesh Kumar K.V } 4280953c66c2SAneesh Kumar K.V 428182b0f8c3SJan Kara vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); 428282b0f8c3SJan Kara if (unlikely(!pmd_none(*vmf->pmd))) 428310102459SKirill A. Shutemov goto out; 428410102459SKirill A. Shutemov 42859f1f5b60SMatthew Wilcox (Oracle) flush_icache_pages(vma, page, HPAGE_PMD_NR); 428610102459SKirill A. Shutemov 428710102459SKirill A. Shutemov entry = mk_huge_pmd(page, vma->vm_page_prot); 428810102459SKirill A. Shutemov if (write) 4289f55e1014SLinus Torvalds entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); 429010102459SKirill A. Shutemov 4291fadae295SYang Shi add_mm_counter(vma->vm_mm, mm_counter_file(page), HPAGE_PMD_NR); 4292cea86fe2SHugh Dickins page_add_file_rmap(page, vma, true); 4293cea86fe2SHugh Dickins 4294953c66c2SAneesh Kumar K.V /* 4295953c66c2SAneesh Kumar K.V * deposit and withdraw with pmd lock held 4296953c66c2SAneesh Kumar K.V */ 4297953c66c2SAneesh Kumar K.V if (arch_needs_pgtable_deposit()) 429882b0f8c3SJan Kara deposit_prealloc_pte(vmf); 429910102459SKirill A. Shutemov 430082b0f8c3SJan Kara set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry); 430110102459SKirill A. Shutemov 430282b0f8c3SJan Kara update_mmu_cache_pmd(vma, haddr, vmf->pmd); 430310102459SKirill A. Shutemov 430410102459SKirill A. Shutemov /* fault is handled */ 430510102459SKirill A. Shutemov ret = 0; 430695ecedcdSKirill A. Shutemov count_vm_event(THP_FILE_MAPPED); 430710102459SKirill A. Shutemov out: 430882b0f8c3SJan Kara spin_unlock(vmf->ptl); 430910102459SKirill A. Shutemov return ret; 431010102459SKirill A. Shutemov } 431110102459SKirill A. Shutemov #else 4312f9ce0be7SKirill A. Shutemov vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page) 431310102459SKirill A. Shutemov { 4314f9ce0be7SKirill A. Shutemov return VM_FAULT_FALLBACK; 431510102459SKirill A. Shutemov } 431610102459SKirill A. Shutemov #endif 431710102459SKirill A. Shutemov 43183bd786f7SYin Fengwei /** 43193bd786f7SYin Fengwei * set_pte_range - Set a range of PTEs to point to pages in a folio. 43203bd786f7SYin Fengwei * @vmf: Fault decription. 43213bd786f7SYin Fengwei * @folio: The folio that contains @page. 43223bd786f7SYin Fengwei * @page: The first page to create a PTE for. 43233bd786f7SYin Fengwei * @nr: The number of PTEs to create. 43243bd786f7SYin Fengwei * @addr: The first address to create a PTE for. 43253bd786f7SYin Fengwei */ 43263bd786f7SYin Fengwei void set_pte_range(struct vm_fault *vmf, struct folio *folio, 43273bd786f7SYin Fengwei struct page *page, unsigned int nr, unsigned long addr) 43283bb97794SKirill A. Shutemov { 432982b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 43302bad466cSPeter Xu bool uffd_wp = vmf_orig_pte_uffd_wp(vmf); 433182b0f8c3SJan Kara bool write = vmf->flags & FAULT_FLAG_WRITE; 43323bd786f7SYin Fengwei bool prefault = in_range(vmf->address, addr, nr * PAGE_SIZE); 43333bb97794SKirill A. Shutemov pte_t entry; 43347267ec00SKirill A. Shutemov 43353bd786f7SYin Fengwei flush_icache_pages(vma, page, nr); 43363bb97794SKirill A. Shutemov entry = mk_pte(page, vma->vm_page_prot); 433746bdb427SWill Deacon 433846bdb427SWill Deacon if (prefault && arch_wants_old_prefaulted_pte()) 433946bdb427SWill Deacon entry = pte_mkold(entry); 434050c25ee9SThomas Bogendoerfer else 434150c25ee9SThomas Bogendoerfer entry = pte_sw_mkyoung(entry); 434246bdb427SWill Deacon 43433bb97794SKirill A. Shutemov if (write) 43443bb97794SKirill A. Shutemov entry = maybe_mkwrite(pte_mkdirty(entry), vma); 43459c28a205SPeter Xu if (unlikely(uffd_wp)) 4346f1eb1bacSPeter Xu entry = pte_mkuffd_wp(entry); 4347bae473a4SKirill A. Shutemov /* copy-on-write page */ 4348bae473a4SKirill A. Shutemov if (write && !(vma->vm_flags & VM_SHARED)) { 43493bd786f7SYin Fengwei add_mm_counter(vma->vm_mm, MM_ANONPAGES, nr); 43503bd786f7SYin Fengwei VM_BUG_ON_FOLIO(nr != 1, folio); 43513bd786f7SYin Fengwei folio_add_new_anon_rmap(folio, vma, addr); 43523bd786f7SYin Fengwei folio_add_lru_vma(folio, vma); 43533bb97794SKirill A. Shutemov } else { 43543bd786f7SYin Fengwei add_mm_counter(vma->vm_mm, mm_counter_file(page), nr); 43553bd786f7SYin Fengwei folio_add_file_rmap_range(folio, page, nr, vma, false); 43563bb97794SKirill A. Shutemov } 43573bd786f7SYin Fengwei set_ptes(vma->vm_mm, addr, vmf->pte, entry, nr); 43583bd786f7SYin Fengwei 43593bd786f7SYin Fengwei /* no need to invalidate: a not-present page won't be cached */ 43603bd786f7SYin Fengwei update_mmu_cache_range(vmf, vma, addr, vmf->pte, nr); 43613bb97794SKirill A. Shutemov } 43623bb97794SKirill A. Shutemov 4363f46f2adeSPeter Xu static bool vmf_pte_changed(struct vm_fault *vmf) 4364f46f2adeSPeter Xu { 4365f46f2adeSPeter Xu if (vmf->flags & FAULT_FLAG_ORIG_PTE_VALID) 4366c33c7948SRyan Roberts return !pte_same(ptep_get(vmf->pte), vmf->orig_pte); 4367f46f2adeSPeter Xu 4368c33c7948SRyan Roberts return !pte_none(ptep_get(vmf->pte)); 4369f46f2adeSPeter Xu } 4370f46f2adeSPeter Xu 43719118c0cbSJan Kara /** 43729118c0cbSJan Kara * finish_fault - finish page fault once we have prepared the page to fault 43739118c0cbSJan Kara * 43749118c0cbSJan Kara * @vmf: structure describing the fault 43759118c0cbSJan Kara * 43769118c0cbSJan Kara * This function handles all that is needed to finish a page fault once the 43779118c0cbSJan Kara * page to fault in is prepared. It handles locking of PTEs, inserts PTE for 43789118c0cbSJan Kara * given page, adds reverse page mapping, handles memcg charges and LRU 4379a862f68aSMike Rapoport * addition. 43809118c0cbSJan Kara * 43819118c0cbSJan Kara * The function expects the page to be locked and on success it consumes a 43829118c0cbSJan Kara * reference of a page being mapped (for the PTE which maps it). 4383a862f68aSMike Rapoport * 4384a862f68aSMike Rapoport * Return: %0 on success, %VM_FAULT_ code in case of error. 43859118c0cbSJan Kara */ 43862b740303SSouptick Joarder vm_fault_t finish_fault(struct vm_fault *vmf) 43879118c0cbSJan Kara { 4388f9ce0be7SKirill A. Shutemov struct vm_area_struct *vma = vmf->vma; 43899118c0cbSJan Kara struct page *page; 4390f9ce0be7SKirill A. Shutemov vm_fault_t ret; 43919118c0cbSJan Kara 43929118c0cbSJan Kara /* Did we COW the page? */ 4393f9ce0be7SKirill A. Shutemov if ((vmf->flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) 43949118c0cbSJan Kara page = vmf->cow_page; 43959118c0cbSJan Kara else 43969118c0cbSJan Kara page = vmf->page; 43976b31d595SMichal Hocko 43986b31d595SMichal Hocko /* 43996b31d595SMichal Hocko * check even for read faults because we might have lost our CoWed 44006b31d595SMichal Hocko * page 44016b31d595SMichal Hocko */ 4402f9ce0be7SKirill A. Shutemov if (!(vma->vm_flags & VM_SHARED)) { 4403f9ce0be7SKirill A. Shutemov ret = check_stable_address_space(vma->vm_mm); 4404f9ce0be7SKirill A. Shutemov if (ret) 4405f9ce0be7SKirill A. Shutemov return ret; 4406f9ce0be7SKirill A. Shutemov } 4407f9ce0be7SKirill A. Shutemov 4408f9ce0be7SKirill A. Shutemov if (pmd_none(*vmf->pmd)) { 4409f9ce0be7SKirill A. Shutemov if (PageTransCompound(page)) { 4410f9ce0be7SKirill A. Shutemov ret = do_set_pmd(vmf, page); 4411f9ce0be7SKirill A. Shutemov if (ret != VM_FAULT_FALLBACK) 4412f9ce0be7SKirill A. Shutemov return ret; 4413f9ce0be7SKirill A. Shutemov } 4414f9ce0be7SKirill A. Shutemov 441503c4f204SQi Zheng if (vmf->prealloc_pte) 441603c4f204SQi Zheng pmd_install(vma->vm_mm, vmf->pmd, &vmf->prealloc_pte); 441703c4f204SQi Zheng else if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd))) 4418f9ce0be7SKirill A. Shutemov return VM_FAULT_OOM; 4419f9ce0be7SKirill A. Shutemov } 4420f9ce0be7SKirill A. Shutemov 4421f9ce0be7SKirill A. Shutemov vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 4422f9ce0be7SKirill A. Shutemov vmf->address, &vmf->ptl); 44233db82b93SHugh Dickins if (!vmf->pte) 44243db82b93SHugh Dickins return VM_FAULT_NOPAGE; 4425f9ce0be7SKirill A. Shutemov 442670427f6eSSergei Antonov /* Re-check under ptl */ 442770427f6eSSergei Antonov if (likely(!vmf_pte_changed(vmf))) { 44283bd786f7SYin Fengwei struct folio *folio = page_folio(page); 442970427f6eSSergei Antonov 44303bd786f7SYin Fengwei set_pte_range(vmf, folio, page, 1, vmf->address); 443170427f6eSSergei Antonov ret = 0; 443270427f6eSSergei Antonov } else { 4433f9ce0be7SKirill A. Shutemov update_mmu_tlb(vma, vmf->address, vmf->pte); 443470427f6eSSergei Antonov ret = VM_FAULT_NOPAGE; 443570427f6eSSergei Antonov } 443670427f6eSSergei Antonov 44379118c0cbSJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 44389118c0cbSJan Kara return ret; 44399118c0cbSJan Kara } 44409118c0cbSJan Kara 444153d36a56SLorenzo Stoakes static unsigned long fault_around_pages __read_mostly = 444253d36a56SLorenzo Stoakes 65536 >> PAGE_SHIFT; 4443a9b0f861SKirill A. Shutemov 44441592eef0SKirill A. Shutemov #ifdef CONFIG_DEBUG_FS 4445a9b0f861SKirill A. Shutemov static int fault_around_bytes_get(void *data, u64 *val) 44461592eef0SKirill A. Shutemov { 444753d36a56SLorenzo Stoakes *val = fault_around_pages << PAGE_SHIFT; 44481592eef0SKirill A. Shutemov return 0; 44491592eef0SKirill A. Shutemov } 44501592eef0SKirill A. Shutemov 4451b4903d6eSAndrey Ryabinin /* 4452da391d64SWilliam Kucharski * fault_around_bytes must be rounded down to the nearest page order as it's 4453da391d64SWilliam Kucharski * what do_fault_around() expects to see. 4454b4903d6eSAndrey Ryabinin */ 4455a9b0f861SKirill A. Shutemov static int fault_around_bytes_set(void *data, u64 val) 44561592eef0SKirill A. Shutemov { 4457a9b0f861SKirill A. Shutemov if (val / PAGE_SIZE > PTRS_PER_PTE) 44581592eef0SKirill A. Shutemov return -EINVAL; 445953d36a56SLorenzo Stoakes 446053d36a56SLorenzo Stoakes /* 446153d36a56SLorenzo Stoakes * The minimum value is 1 page, however this results in no fault-around 446253d36a56SLorenzo Stoakes * at all. See should_fault_around(). 446353d36a56SLorenzo Stoakes */ 446453d36a56SLorenzo Stoakes fault_around_pages = max(rounddown_pow_of_two(val) >> PAGE_SHIFT, 1UL); 446553d36a56SLorenzo Stoakes 44661592eef0SKirill A. Shutemov return 0; 44671592eef0SKirill A. Shutemov } 44680a1345f8SYevgen Pronenko DEFINE_DEBUGFS_ATTRIBUTE(fault_around_bytes_fops, 4469a9b0f861SKirill A. Shutemov fault_around_bytes_get, fault_around_bytes_set, "%llu\n"); 44701592eef0SKirill A. Shutemov 44711592eef0SKirill A. Shutemov static int __init fault_around_debugfs(void) 44721592eef0SKirill A. Shutemov { 4473d9f7979cSGreg Kroah-Hartman debugfs_create_file_unsafe("fault_around_bytes", 0644, NULL, NULL, 4474a9b0f861SKirill A. Shutemov &fault_around_bytes_fops); 44751592eef0SKirill A. Shutemov return 0; 44761592eef0SKirill A. Shutemov } 44771592eef0SKirill A. Shutemov late_initcall(fault_around_debugfs); 44781592eef0SKirill A. Shutemov #endif 44798c6e50b0SKirill A. Shutemov 44801fdb412bSKirill A. Shutemov /* 44811fdb412bSKirill A. Shutemov * do_fault_around() tries to map few pages around the fault address. The hope 44821fdb412bSKirill A. Shutemov * is that the pages will be needed soon and this will lower the number of 44831fdb412bSKirill A. Shutemov * faults to handle. 44841fdb412bSKirill A. Shutemov * 44851fdb412bSKirill A. Shutemov * It uses vm_ops->map_pages() to map the pages, which skips the page if it's 44861fdb412bSKirill A. Shutemov * not ready to be mapped: not up-to-date, locked, etc. 44871fdb412bSKirill A. Shutemov * 44889042599eSLorenzo Stoakes * This function doesn't cross VMA or page table boundaries, in order to call 44899042599eSLorenzo Stoakes * map_pages() and acquire a PTE lock only once. 44901fdb412bSKirill A. Shutemov * 449153d36a56SLorenzo Stoakes * fault_around_pages defines how many pages we'll try to map. 4492da391d64SWilliam Kucharski * do_fault_around() expects it to be set to a power of two less than or equal 4493da391d64SWilliam Kucharski * to PTRS_PER_PTE. 44941fdb412bSKirill A. Shutemov * 4495da391d64SWilliam Kucharski * The virtual address of the area that we map is naturally aligned to 449653d36a56SLorenzo Stoakes * fault_around_pages * PAGE_SIZE rounded down to the machine page size 4497da391d64SWilliam Kucharski * (and therefore to page order). This way it's easier to guarantee 4498da391d64SWilliam Kucharski * that we don't cross page table boundaries. 44991fdb412bSKirill A. Shutemov */ 45002b740303SSouptick Joarder static vm_fault_t do_fault_around(struct vm_fault *vmf) 45018c6e50b0SKirill A. Shutemov { 450253d36a56SLorenzo Stoakes pgoff_t nr_pages = READ_ONCE(fault_around_pages); 45039042599eSLorenzo Stoakes pgoff_t pte_off = pte_index(vmf->address); 45049042599eSLorenzo Stoakes /* The page offset of vmf->address within the VMA. */ 45059042599eSLorenzo Stoakes pgoff_t vma_off = vmf->pgoff - vmf->vma->vm_pgoff; 45069042599eSLorenzo Stoakes pgoff_t from_pte, to_pte; 450758ef47efSMatthew Wilcox (Oracle) vm_fault_t ret; 45088c6e50b0SKirill A. Shutemov 45099042599eSLorenzo Stoakes /* The PTE offset of the start address, clamped to the VMA. */ 45109042599eSLorenzo Stoakes from_pte = max(ALIGN_DOWN(pte_off, nr_pages), 45119042599eSLorenzo Stoakes pte_off - min(pte_off, vma_off)); 4512aecd6f44SKirill A. Shutemov 45139042599eSLorenzo Stoakes /* The PTE offset of the end address, clamped to the VMA and PTE. */ 45149042599eSLorenzo Stoakes to_pte = min3(from_pte + nr_pages, (pgoff_t)PTRS_PER_PTE, 45159042599eSLorenzo Stoakes pte_off + vma_pages(vmf->vma) - vma_off) - 1; 45168c6e50b0SKirill A. Shutemov 451782b0f8c3SJan Kara if (pmd_none(*vmf->pmd)) { 45184cf58924SJoel Fernandes (Google) vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm); 451982b0f8c3SJan Kara if (!vmf->prealloc_pte) 4520f9ce0be7SKirill A. Shutemov return VM_FAULT_OOM; 45218c6e50b0SKirill A. Shutemov } 45228c6e50b0SKirill A. Shutemov 452358ef47efSMatthew Wilcox (Oracle) rcu_read_lock(); 452458ef47efSMatthew Wilcox (Oracle) ret = vmf->vma->vm_ops->map_pages(vmf, 45259042599eSLorenzo Stoakes vmf->pgoff + from_pte - pte_off, 45269042599eSLorenzo Stoakes vmf->pgoff + to_pte - pte_off); 452758ef47efSMatthew Wilcox (Oracle) rcu_read_unlock(); 452858ef47efSMatthew Wilcox (Oracle) 452958ef47efSMatthew Wilcox (Oracle) return ret; 45307267ec00SKirill A. Shutemov } 45317267ec00SKirill A. Shutemov 45329c28a205SPeter Xu /* Return true if we should do read fault-around, false otherwise */ 45339c28a205SPeter Xu static inline bool should_fault_around(struct vm_fault *vmf) 45349c28a205SPeter Xu { 45359c28a205SPeter Xu /* No ->map_pages? No way to fault around... */ 45369c28a205SPeter Xu if (!vmf->vma->vm_ops->map_pages) 45379c28a205SPeter Xu return false; 45389c28a205SPeter Xu 45399c28a205SPeter Xu if (uffd_disable_fault_around(vmf->vma)) 45409c28a205SPeter Xu return false; 45419c28a205SPeter Xu 454253d36a56SLorenzo Stoakes /* A single page implies no faulting 'around' at all. */ 454353d36a56SLorenzo Stoakes return fault_around_pages > 1; 45449c28a205SPeter Xu } 45459c28a205SPeter Xu 45462b740303SSouptick Joarder static vm_fault_t do_read_fault(struct vm_fault *vmf) 4547e655fb29SKirill A. Shutemov { 45482b740303SSouptick Joarder vm_fault_t ret = 0; 454922d1e68fSSidhartha Kumar struct folio *folio; 45508c6e50b0SKirill A. Shutemov 45518c6e50b0SKirill A. Shutemov /* 45528c6e50b0SKirill A. Shutemov * Let's call ->map_pages() first and use ->fault() as fallback 45538c6e50b0SKirill A. Shutemov * if page by the offset is not ready to be mapped (cold cache or 45548c6e50b0SKirill A. Shutemov * something). 45558c6e50b0SKirill A. Shutemov */ 45569c28a205SPeter Xu if (should_fault_around(vmf)) { 45570721ec8bSJan Kara ret = do_fault_around(vmf); 45587267ec00SKirill A. Shutemov if (ret) 45597267ec00SKirill A. Shutemov return ret; 45608c6e50b0SKirill A. Shutemov } 4561e655fb29SKirill A. Shutemov 4562f5617ffeSMatthew Wilcox (Oracle) if (vmf->flags & FAULT_FLAG_VMA_LOCK) { 4563f5617ffeSMatthew Wilcox (Oracle) vma_end_read(vmf->vma); 4564f5617ffeSMatthew Wilcox (Oracle) return VM_FAULT_RETRY; 4565f5617ffeSMatthew Wilcox (Oracle) } 4566f5617ffeSMatthew Wilcox (Oracle) 4567936ca80dSJan Kara ret = __do_fault(vmf); 4568e655fb29SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 4569e655fb29SKirill A. Shutemov return ret; 4570e655fb29SKirill A. Shutemov 45719118c0cbSJan Kara ret |= finish_fault(vmf); 457222d1e68fSSidhartha Kumar folio = page_folio(vmf->page); 457322d1e68fSSidhartha Kumar folio_unlock(folio); 45747267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 457522d1e68fSSidhartha Kumar folio_put(folio); 4576e655fb29SKirill A. Shutemov return ret; 4577e655fb29SKirill A. Shutemov } 4578e655fb29SKirill A. Shutemov 45792b740303SSouptick Joarder static vm_fault_t do_cow_fault(struct vm_fault *vmf) 4580ec47c3b9SKirill A. Shutemov { 458182b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 45822b740303SSouptick Joarder vm_fault_t ret; 4583ec47c3b9SKirill A. Shutemov 458461a4b8d3SMatthew Wilcox (Oracle) if (vmf->flags & FAULT_FLAG_VMA_LOCK) { 458561a4b8d3SMatthew Wilcox (Oracle) vma_end_read(vma); 458661a4b8d3SMatthew Wilcox (Oracle) return VM_FAULT_RETRY; 458761a4b8d3SMatthew Wilcox (Oracle) } 458861a4b8d3SMatthew Wilcox (Oracle) 4589ec47c3b9SKirill A. Shutemov if (unlikely(anon_vma_prepare(vma))) 4590ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 4591ec47c3b9SKirill A. Shutemov 4592936ca80dSJan Kara vmf->cow_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vmf->address); 4593936ca80dSJan Kara if (!vmf->cow_page) 4594ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 4595ec47c3b9SKirill A. Shutemov 45968f425e4eSMatthew Wilcox (Oracle) if (mem_cgroup_charge(page_folio(vmf->cow_page), vma->vm_mm, 45978f425e4eSMatthew Wilcox (Oracle) GFP_KERNEL)) { 4598936ca80dSJan Kara put_page(vmf->cow_page); 4599ec47c3b9SKirill A. Shutemov return VM_FAULT_OOM; 4600ec47c3b9SKirill A. Shutemov } 460168fa572bSKefeng Wang folio_throttle_swaprate(page_folio(vmf->cow_page), GFP_KERNEL); 4602ec47c3b9SKirill A. Shutemov 4603936ca80dSJan Kara ret = __do_fault(vmf); 4604ec47c3b9SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 4605ec47c3b9SKirill A. Shutemov goto uncharge_out; 46063917048dSJan Kara if (ret & VM_FAULT_DONE_COW) 46073917048dSJan Kara return ret; 4608ec47c3b9SKirill A. Shutemov 4609936ca80dSJan Kara copy_user_highpage(vmf->cow_page, vmf->page, vmf->address, vma); 4610936ca80dSJan Kara __SetPageUptodate(vmf->cow_page); 4611ec47c3b9SKirill A. Shutemov 46129118c0cbSJan Kara ret |= finish_fault(vmf); 4613936ca80dSJan Kara unlock_page(vmf->page); 4614936ca80dSJan Kara put_page(vmf->page); 46157267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 46167267ec00SKirill A. Shutemov goto uncharge_out; 4617ec47c3b9SKirill A. Shutemov return ret; 4618ec47c3b9SKirill A. Shutemov uncharge_out: 4619936ca80dSJan Kara put_page(vmf->cow_page); 4620ec47c3b9SKirill A. Shutemov return ret; 4621ec47c3b9SKirill A. Shutemov } 4622ec47c3b9SKirill A. Shutemov 46232b740303SSouptick Joarder static vm_fault_t do_shared_fault(struct vm_fault *vmf) 46241da177e4SLinus Torvalds { 462582b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 46262b740303SSouptick Joarder vm_fault_t ret, tmp; 46276f609b7eSSidhartha Kumar struct folio *folio; 46281d65f86dSKAMEZAWA Hiroyuki 462961a4b8d3SMatthew Wilcox (Oracle) if (vmf->flags & FAULT_FLAG_VMA_LOCK) { 463061a4b8d3SMatthew Wilcox (Oracle) vma_end_read(vma); 463161a4b8d3SMatthew Wilcox (Oracle) return VM_FAULT_RETRY; 463261a4b8d3SMatthew Wilcox (Oracle) } 463361a4b8d3SMatthew Wilcox (Oracle) 4634936ca80dSJan Kara ret = __do_fault(vmf); 46357eae74afSKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) 4636f0c6d4d2SKirill A. Shutemov return ret; 46371da177e4SLinus Torvalds 46386f609b7eSSidhartha Kumar folio = page_folio(vmf->page); 46396f609b7eSSidhartha Kumar 46401da177e4SLinus Torvalds /* 4641f0c6d4d2SKirill A. Shutemov * Check if the backing address space wants to know that the page is 4642f0c6d4d2SKirill A. Shutemov * about to become writable 46431da177e4SLinus Torvalds */ 4644fb09a464SKirill A. Shutemov if (vma->vm_ops->page_mkwrite) { 46456f609b7eSSidhartha Kumar folio_unlock(folio); 464686aa6998SSidhartha Kumar tmp = do_page_mkwrite(vmf, folio); 4647fb09a464SKirill A. Shutemov if (unlikely(!tmp || 4648fb09a464SKirill A. Shutemov (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { 46496f609b7eSSidhartha Kumar folio_put(folio); 4650f0c6d4d2SKirill A. Shutemov return tmp; 465169676147SMark Fasheh } 4652d0217ac0SNick Piggin } 4653fb09a464SKirill A. Shutemov 46549118c0cbSJan Kara ret |= finish_fault(vmf); 46557267ec00SKirill A. Shutemov if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | 46567267ec00SKirill A. Shutemov VM_FAULT_RETRY))) { 46576f609b7eSSidhartha Kumar folio_unlock(folio); 46586f609b7eSSidhartha Kumar folio_put(folio); 4659f0c6d4d2SKirill A. Shutemov return ret; 46609637a5efSDavid Howells } 4661d00806b1SNick Piggin 466289b15332SJohannes Weiner ret |= fault_dirty_shared_page(vmf); 4663b827e496SNick Piggin return ret; 466454cb8821SNick Piggin } 4665d00806b1SNick Piggin 46669a95f3cfSPaul Cassella /* 4667c1e8d7c6SMichel Lespinasse * We enter with non-exclusive mmap_lock (to exclude vma changes, 46689a95f3cfSPaul Cassella * but allow concurrent faults). 4669c1e8d7c6SMichel Lespinasse * The mmap_lock may have been released depending on flags and our 46709138e47eSMatthew Wilcox (Oracle) * return value. See filemap_fault() and __folio_lock_or_retry(). 4671c1e8d7c6SMichel Lespinasse * If mmap_lock is released, vma may become invalid (for example 4672fc8efd2dSJan Stancek * by other thread calling munmap()). 46739a95f3cfSPaul Cassella */ 46742b740303SSouptick Joarder static vm_fault_t do_fault(struct vm_fault *vmf) 467554cb8821SNick Piggin { 467682b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 4677fc8efd2dSJan Stancek struct mm_struct *vm_mm = vma->vm_mm; 46782b740303SSouptick Joarder vm_fault_t ret; 467954cb8821SNick Piggin 4680ff09d7ecSAneesh Kumar K.V /* 4681ff09d7ecSAneesh Kumar K.V * The VMA was not fully populated on mmap() or missing VM_DONTEXPAND 4682ff09d7ecSAneesh Kumar K.V */ 4683ff09d7ecSAneesh Kumar K.V if (!vma->vm_ops->fault) { 46843db82b93SHugh Dickins vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, 46853db82b93SHugh Dickins vmf->address, &vmf->ptl); 46863db82b93SHugh Dickins if (unlikely(!vmf->pte)) 4687b0b9b3dfSHugh Dickins ret = VM_FAULT_SIGBUS; 4688ff09d7ecSAneesh Kumar K.V else { 4689ff09d7ecSAneesh Kumar K.V /* 4690ff09d7ecSAneesh Kumar K.V * Make sure this is not a temporary clearing of pte 4691ff09d7ecSAneesh Kumar K.V * by holding ptl and checking again. A R/M/W update 4692ff09d7ecSAneesh Kumar K.V * of pte involves: take ptl, clearing the pte so that 4693ff09d7ecSAneesh Kumar K.V * we don't have concurrent modification by hardware 4694ff09d7ecSAneesh Kumar K.V * followed by an update. 4695ff09d7ecSAneesh Kumar K.V */ 4696c33c7948SRyan Roberts if (unlikely(pte_none(ptep_get(vmf->pte)))) 4697ff09d7ecSAneesh Kumar K.V ret = VM_FAULT_SIGBUS; 4698ff09d7ecSAneesh Kumar K.V else 4699ff09d7ecSAneesh Kumar K.V ret = VM_FAULT_NOPAGE; 4700ff09d7ecSAneesh Kumar K.V 4701ff09d7ecSAneesh Kumar K.V pte_unmap_unlock(vmf->pte, vmf->ptl); 4702ff09d7ecSAneesh Kumar K.V } 4703ff09d7ecSAneesh Kumar K.V } else if (!(vmf->flags & FAULT_FLAG_WRITE)) 4704b0b9b3dfSHugh Dickins ret = do_read_fault(vmf); 4705b0b9b3dfSHugh Dickins else if (!(vma->vm_flags & VM_SHARED)) 4706b0b9b3dfSHugh Dickins ret = do_cow_fault(vmf); 4707b0b9b3dfSHugh Dickins else 4708b0b9b3dfSHugh Dickins ret = do_shared_fault(vmf); 4709b0b9b3dfSHugh Dickins 4710b0b9b3dfSHugh Dickins /* preallocated pagetable is unused: free it */ 4711b0b9b3dfSHugh Dickins if (vmf->prealloc_pte) { 4712fc8efd2dSJan Stancek pte_free(vm_mm, vmf->prealloc_pte); 47137f2b6ce8STobin C Harding vmf->prealloc_pte = NULL; 4714b0b9b3dfSHugh Dickins } 4715b0b9b3dfSHugh Dickins return ret; 471654cb8821SNick Piggin } 471754cb8821SNick Piggin 4718f4c0d836SYang Shi int numa_migrate_prep(struct page *page, struct vm_area_struct *vma, 4719f4c0d836SYang Shi unsigned long addr, int page_nid, int *flags) 47209532fec1SMel Gorman { 47219532fec1SMel Gorman get_page(page); 47229532fec1SMel Gorman 4723fc137c0dSRaghavendra K T /* Record the current PID acceesing VMA */ 4724fc137c0dSRaghavendra K T vma_set_access_pid_bit(vma); 4725fc137c0dSRaghavendra K T 47269532fec1SMel Gorman count_vm_numa_event(NUMA_HINT_FAULTS); 472704bb2f94SRik van Riel if (page_nid == numa_node_id()) { 47289532fec1SMel Gorman count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL); 472904bb2f94SRik van Riel *flags |= TNF_FAULT_LOCAL; 473004bb2f94SRik van Riel } 47319532fec1SMel Gorman 47329532fec1SMel Gorman return mpol_misplaced(page, vma, addr); 47339532fec1SMel Gorman } 47349532fec1SMel Gorman 47352b740303SSouptick Joarder static vm_fault_t do_numa_page(struct vm_fault *vmf) 4736d10e63f2SMel Gorman { 473782b0f8c3SJan Kara struct vm_area_struct *vma = vmf->vma; 47384daae3b4SMel Gorman struct page *page = NULL; 473998fa15f3SAnshuman Khandual int page_nid = NUMA_NO_NODE; 47406a56ccbcSDavid Hildenbrand bool writable = false; 474190572890SPeter Zijlstra int last_cpupid; 4742cbee9f88SPeter Zijlstra int target_nid; 474304a86453SAneesh Kumar K.V pte_t pte, old_pte; 47446688cc05SPeter Zijlstra int flags = 0; 4745d10e63f2SMel Gorman 4746d10e63f2SMel Gorman /* 4747d10e63f2SMel Gorman * The "pte" at this point cannot be used safely without 4748d10e63f2SMel Gorman * validation through pte_unmap_same(). It's of NUMA type but 4749d10e63f2SMel Gorman * the pfn may be screwed if the read is non atomic. 4750d10e63f2SMel Gorman */ 475182b0f8c3SJan Kara spin_lock(vmf->ptl); 4752c33c7948SRyan Roberts if (unlikely(!pte_same(ptep_get(vmf->pte), vmf->orig_pte))) { 475382b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 47544daae3b4SMel Gorman goto out; 47554daae3b4SMel Gorman } 47564daae3b4SMel Gorman 4757b99a342dSHuang Ying /* Get the normal PTE */ 4758b99a342dSHuang Ying old_pte = ptep_get(vmf->pte); 475904a86453SAneesh Kumar K.V pte = pte_modify(old_pte, vma->vm_page_prot); 4760d10e63f2SMel Gorman 47616a56ccbcSDavid Hildenbrand /* 47626a56ccbcSDavid Hildenbrand * Detect now whether the PTE could be writable; this information 47636a56ccbcSDavid Hildenbrand * is only valid while holding the PT lock. 47646a56ccbcSDavid Hildenbrand */ 47656a56ccbcSDavid Hildenbrand writable = pte_write(pte); 47666a56ccbcSDavid Hildenbrand if (!writable && vma_wants_manual_pte_write_upgrade(vma) && 47676a56ccbcSDavid Hildenbrand can_change_pte_writable(vma, vmf->address, pte)) 47686a56ccbcSDavid Hildenbrand writable = true; 47696a56ccbcSDavid Hildenbrand 477082b0f8c3SJan Kara page = vm_normal_page(vma, vmf->address, pte); 47713218f871SAlex Sierra if (!page || is_zone_device_page(page)) 4772b99a342dSHuang Ying goto out_map; 4773d10e63f2SMel Gorman 4774e81c4802SKirill A. Shutemov /* TODO: handle PTE-mapped THP */ 4775b99a342dSHuang Ying if (PageCompound(page)) 4776b99a342dSHuang Ying goto out_map; 4777e81c4802SKirill A. Shutemov 47786688cc05SPeter Zijlstra /* 4779bea66fbdSMel Gorman * Avoid grouping on RO pages in general. RO pages shouldn't hurt as 4780bea66fbdSMel Gorman * much anyway since they can be in shared cache state. This misses 4781bea66fbdSMel Gorman * the case where a mapping is writable but the process never writes 4782bea66fbdSMel Gorman * to it but pte_write gets cleared during protection updates and 4783bea66fbdSMel Gorman * pte_dirty has unpredictable behaviour between PTE scan updates, 4784bea66fbdSMel Gorman * background writeback, dirty balancing and application behaviour. 47856688cc05SPeter Zijlstra */ 47866a56ccbcSDavid Hildenbrand if (!writable) 47876688cc05SPeter Zijlstra flags |= TNF_NO_GROUP; 47886688cc05SPeter Zijlstra 4789dabe1d99SRik van Riel /* 4790dabe1d99SRik van Riel * Flag if the page is shared between multiple address spaces. This 4791dabe1d99SRik van Riel * is later used when determining whether to group tasks together 4792dabe1d99SRik van Riel */ 4793dabe1d99SRik van Riel if (page_mapcount(page) > 1 && (vma->vm_flags & VM_SHARED)) 4794dabe1d99SRik van Riel flags |= TNF_SHARED; 4795dabe1d99SRik van Riel 47968191acbdSMel Gorman page_nid = page_to_nid(page); 479733024536SHuang Ying /* 479833024536SHuang Ying * For memory tiering mode, cpupid of slow memory page is used 479933024536SHuang Ying * to record page access time. So use default value. 480033024536SHuang Ying */ 480133024536SHuang Ying if ((sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING) && 480233024536SHuang Ying !node_is_toptier(page_nid)) 480333024536SHuang Ying last_cpupid = (-1 & LAST_CPUPID_MASK); 480433024536SHuang Ying else 480533024536SHuang Ying last_cpupid = page_cpupid_last(page); 480682b0f8c3SJan Kara target_nid = numa_migrate_prep(page, vma, vmf->address, page_nid, 4807bae473a4SKirill A. Shutemov &flags); 480898fa15f3SAnshuman Khandual if (target_nid == NUMA_NO_NODE) { 48094daae3b4SMel Gorman put_page(page); 4810b99a342dSHuang Ying goto out_map; 48114daae3b4SMel Gorman } 4812b99a342dSHuang Ying pte_unmap_unlock(vmf->pte, vmf->ptl); 48136a56ccbcSDavid Hildenbrand writable = false; 48144daae3b4SMel Gorman 48154daae3b4SMel Gorman /* Migrate to the requested node */ 4816bf90ac19SWang Qing if (migrate_misplaced_page(page, vma, target_nid)) { 48178191acbdSMel Gorman page_nid = target_nid; 48186688cc05SPeter Zijlstra flags |= TNF_MIGRATED; 4819b99a342dSHuang Ying } else { 4820074c2381SMel Gorman flags |= TNF_MIGRATE_FAIL; 4821c7ad0880SHugh Dickins vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, 4822c7ad0880SHugh Dickins vmf->address, &vmf->ptl); 4823c7ad0880SHugh Dickins if (unlikely(!vmf->pte)) 4824c7ad0880SHugh Dickins goto out; 4825c33c7948SRyan Roberts if (unlikely(!pte_same(ptep_get(vmf->pte), vmf->orig_pte))) { 4826b99a342dSHuang Ying pte_unmap_unlock(vmf->pte, vmf->ptl); 4827b99a342dSHuang Ying goto out; 4828b99a342dSHuang Ying } 4829b99a342dSHuang Ying goto out_map; 4830b99a342dSHuang Ying } 48314daae3b4SMel Gorman 48324daae3b4SMel Gorman out: 483398fa15f3SAnshuman Khandual if (page_nid != NUMA_NO_NODE) 48346688cc05SPeter Zijlstra task_numa_fault(last_cpupid, page_nid, 1, flags); 4835d10e63f2SMel Gorman return 0; 4836b99a342dSHuang Ying out_map: 4837b99a342dSHuang Ying /* 4838b99a342dSHuang Ying * Make it present again, depending on how arch implements 4839b99a342dSHuang Ying * non-accessible ptes, some can allow access by kernel mode. 4840b99a342dSHuang Ying */ 4841b99a342dSHuang Ying old_pte = ptep_modify_prot_start(vma, vmf->address, vmf->pte); 4842b99a342dSHuang Ying pte = pte_modify(old_pte, vma->vm_page_prot); 4843b99a342dSHuang Ying pte = pte_mkyoung(pte); 48446a56ccbcSDavid Hildenbrand if (writable) 4845b99a342dSHuang Ying pte = pte_mkwrite(pte); 4846b99a342dSHuang Ying ptep_modify_prot_commit(vma, vmf->address, vmf->pte, old_pte, pte); 48475003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vma, vmf->address, vmf->pte, 1); 4848b99a342dSHuang Ying pte_unmap_unlock(vmf->pte, vmf->ptl); 4849b99a342dSHuang Ying goto out; 4850d10e63f2SMel Gorman } 4851d10e63f2SMel Gorman 48522b740303SSouptick Joarder static inline vm_fault_t create_huge_pmd(struct vm_fault *vmf) 4853b96375f7SMatthew Wilcox { 48548f5fd0e1SMatthew Wilcox (Oracle) struct vm_area_struct *vma = vmf->vma; 48558f5fd0e1SMatthew Wilcox (Oracle) if (vma_is_anonymous(vma)) 485682b0f8c3SJan Kara return do_huge_pmd_anonymous_page(vmf); 48578f5fd0e1SMatthew Wilcox (Oracle) if (vma->vm_ops->huge_fault) { 48588f5fd0e1SMatthew Wilcox (Oracle) if (vmf->flags & FAULT_FLAG_VMA_LOCK) { 48598f5fd0e1SMatthew Wilcox (Oracle) vma_end_read(vma); 48608f5fd0e1SMatthew Wilcox (Oracle) return VM_FAULT_RETRY; 48618f5fd0e1SMatthew Wilcox (Oracle) } 48628f5fd0e1SMatthew Wilcox (Oracle) return vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD); 48638f5fd0e1SMatthew Wilcox (Oracle) } 4864b96375f7SMatthew Wilcox return VM_FAULT_FALLBACK; 4865b96375f7SMatthew Wilcox } 4866b96375f7SMatthew Wilcox 4867183f24aaSGeert Uytterhoeven /* `inline' is required to avoid gcc 4.1.2 build error */ 48685db4f15cSYang Shi static inline vm_fault_t wp_huge_pmd(struct vm_fault *vmf) 4869b96375f7SMatthew Wilcox { 48708f5fd0e1SMatthew Wilcox (Oracle) struct vm_area_struct *vma = vmf->vma; 4871c89357e2SDavid Hildenbrand const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE; 4872aea06577SDavid Hildenbrand vm_fault_t ret; 4873c89357e2SDavid Hildenbrand 48748f5fd0e1SMatthew Wilcox (Oracle) if (vma_is_anonymous(vma)) { 4875c89357e2SDavid Hildenbrand if (likely(!unshare) && 48768f5fd0e1SMatthew Wilcox (Oracle) userfaultfd_huge_pmd_wp(vma, vmf->orig_pmd)) 4877529b930bSAndrea Arcangeli return handle_userfault(vmf, VM_UFFD_WP); 48785db4f15cSYang Shi return do_huge_pmd_wp_page(vmf); 4879529b930bSAndrea Arcangeli } 4880af9e4d5fSKirill A. Shutemov 48818f5fd0e1SMatthew Wilcox (Oracle) if (vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) { 48828f5fd0e1SMatthew Wilcox (Oracle) if (vma->vm_ops->huge_fault) { 48838f5fd0e1SMatthew Wilcox (Oracle) if (vmf->flags & FAULT_FLAG_VMA_LOCK) { 48848f5fd0e1SMatthew Wilcox (Oracle) vma_end_read(vma); 48858f5fd0e1SMatthew Wilcox (Oracle) return VM_FAULT_RETRY; 48868f5fd0e1SMatthew Wilcox (Oracle) } 48878f5fd0e1SMatthew Wilcox (Oracle) ret = vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD); 4888327e9fd4SThomas Hellstrom (VMware) if (!(ret & VM_FAULT_FALLBACK)) 4889327e9fd4SThomas Hellstrom (VMware) return ret; 4890327e9fd4SThomas Hellstrom (VMware) } 4891aea06577SDavid Hildenbrand } 4892327e9fd4SThomas Hellstrom (VMware) 4893327e9fd4SThomas Hellstrom (VMware) /* COW or write-notify handled on pte level: split pmd. */ 48948f5fd0e1SMatthew Wilcox (Oracle) __split_huge_pmd(vma, vmf->pmd, vmf->address, false, NULL); 4895af9e4d5fSKirill A. Shutemov 4896b96375f7SMatthew Wilcox return VM_FAULT_FALLBACK; 4897b96375f7SMatthew Wilcox } 4898b96375f7SMatthew Wilcox 48992b740303SSouptick Joarder static vm_fault_t create_huge_pud(struct vm_fault *vmf) 4900a00cc7d9SMatthew Wilcox { 4901327e9fd4SThomas Hellstrom (VMware) #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && \ 4902327e9fd4SThomas Hellstrom (VMware) defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) 4903c4fd825eSMatthew Wilcox (Oracle) struct vm_area_struct *vma = vmf->vma; 4904a00cc7d9SMatthew Wilcox /* No support for anonymous transparent PUD pages yet */ 4905c4fd825eSMatthew Wilcox (Oracle) if (vma_is_anonymous(vma)) 490614c99d65SGowans, James return VM_FAULT_FALLBACK; 4907c4fd825eSMatthew Wilcox (Oracle) if (vma->vm_ops->huge_fault) { 4908c4fd825eSMatthew Wilcox (Oracle) if (vmf->flags & FAULT_FLAG_VMA_LOCK) { 4909c4fd825eSMatthew Wilcox (Oracle) vma_end_read(vma); 4910c4fd825eSMatthew Wilcox (Oracle) return VM_FAULT_RETRY; 4911c4fd825eSMatthew Wilcox (Oracle) } 4912c4fd825eSMatthew Wilcox (Oracle) return vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD); 4913c4fd825eSMatthew Wilcox (Oracle) } 491414c99d65SGowans, James #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 491514c99d65SGowans, James return VM_FAULT_FALLBACK; 491614c99d65SGowans, James } 491714c99d65SGowans, James 491814c99d65SGowans, James static vm_fault_t wp_huge_pud(struct vm_fault *vmf, pud_t orig_pud) 491914c99d65SGowans, James { 492014c99d65SGowans, James #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && \ 492114c99d65SGowans, James defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) 4922c4fd825eSMatthew Wilcox (Oracle) struct vm_area_struct *vma = vmf->vma; 4923aea06577SDavid Hildenbrand vm_fault_t ret; 4924aea06577SDavid Hildenbrand 492514c99d65SGowans, James /* No support for anonymous transparent PUD pages yet */ 4926c4fd825eSMatthew Wilcox (Oracle) if (vma_is_anonymous(vma)) 4927327e9fd4SThomas Hellstrom (VMware) goto split; 4928c4fd825eSMatthew Wilcox (Oracle) if (vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) { 4929c4fd825eSMatthew Wilcox (Oracle) if (vma->vm_ops->huge_fault) { 4930c4fd825eSMatthew Wilcox (Oracle) if (vmf->flags & FAULT_FLAG_VMA_LOCK) { 4931c4fd825eSMatthew Wilcox (Oracle) vma_end_read(vma); 4932c4fd825eSMatthew Wilcox (Oracle) return VM_FAULT_RETRY; 4933c4fd825eSMatthew Wilcox (Oracle) } 4934c4fd825eSMatthew Wilcox (Oracle) ret = vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD); 4935327e9fd4SThomas Hellstrom (VMware) if (!(ret & VM_FAULT_FALLBACK)) 4936327e9fd4SThomas Hellstrom (VMware) return ret; 4937327e9fd4SThomas Hellstrom (VMware) } 4938aea06577SDavid Hildenbrand } 4939327e9fd4SThomas Hellstrom (VMware) split: 4940327e9fd4SThomas Hellstrom (VMware) /* COW or write-notify not handled on PUD level: split pud.*/ 4941c4fd825eSMatthew Wilcox (Oracle) __split_huge_pud(vma, vmf->pud, vmf->address); 494214c99d65SGowans, James #endif /* CONFIG_TRANSPARENT_HUGEPAGE && CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 4943a00cc7d9SMatthew Wilcox return VM_FAULT_FALLBACK; 4944a00cc7d9SMatthew Wilcox } 4945a00cc7d9SMatthew Wilcox 49461da177e4SLinus Torvalds /* 49471da177e4SLinus Torvalds * These routines also need to handle stuff like marking pages dirty 49481da177e4SLinus Torvalds * and/or accessed for architectures that don't do it in hardware (most 49491da177e4SLinus Torvalds * RISC architectures). The early dirtying is also good on the i386. 49501da177e4SLinus Torvalds * 49511da177e4SLinus Torvalds * There is also a hook called "update_mmu_cache()" that architectures 49521da177e4SLinus Torvalds * with external mmu caches can use to update those (ie the Sparc or 49531da177e4SLinus Torvalds * PowerPC hashed page tables that act as extended TLBs). 49541da177e4SLinus Torvalds * 4955c1e8d7c6SMichel Lespinasse * We enter with non-exclusive mmap_lock (to exclude vma changes, but allow 49567267ec00SKirill A. Shutemov * concurrent faults). 49579a95f3cfSPaul Cassella * 4958c1e8d7c6SMichel Lespinasse * The mmap_lock may have been released depending on flags and our return value. 49599138e47eSMatthew Wilcox (Oracle) * See filemap_fault() and __folio_lock_or_retry(). 49601da177e4SLinus Torvalds */ 49612b740303SSouptick Joarder static vm_fault_t handle_pte_fault(struct vm_fault *vmf) 49621da177e4SLinus Torvalds { 49631da177e4SLinus Torvalds pte_t entry; 49641da177e4SLinus Torvalds 496582b0f8c3SJan Kara if (unlikely(pmd_none(*vmf->pmd))) { 49667267ec00SKirill A. Shutemov /* 49677267ec00SKirill A. Shutemov * Leave __pte_alloc() until later: because vm_ops->fault may 49687267ec00SKirill A. Shutemov * want to allocate huge page, and if we expose page table 49697267ec00SKirill A. Shutemov * for an instant, it will be difficult to retract from 49707267ec00SKirill A. Shutemov * concurrent faults and from rmap lookups. 49717267ec00SKirill A. Shutemov */ 497282b0f8c3SJan Kara vmf->pte = NULL; 4973f46f2adeSPeter Xu vmf->flags &= ~FAULT_FLAG_ORIG_PTE_VALID; 49747267ec00SKirill A. Shutemov } else { 4975f9ce0be7SKirill A. Shutemov /* 49767267ec00SKirill A. Shutemov * A regular pmd is established and it can't morph into a huge 4977c7ad0880SHugh Dickins * pmd by anon khugepaged, since that takes mmap_lock in write 4978c7ad0880SHugh Dickins * mode; but shmem or file collapse to THP could still morph 4979c7ad0880SHugh Dickins * it into a huge pmd: just retry later if so. 49807267ec00SKirill A. Shutemov */ 4981c7ad0880SHugh Dickins vmf->pte = pte_offset_map_nolock(vmf->vma->vm_mm, vmf->pmd, 4982c7ad0880SHugh Dickins vmf->address, &vmf->ptl); 4983c7ad0880SHugh Dickins if (unlikely(!vmf->pte)) 4984c7ad0880SHugh Dickins return 0; 498526e1a0c3SHugh Dickins vmf->orig_pte = ptep_get_lockless(vmf->pte); 4986f46f2adeSPeter Xu vmf->flags |= FAULT_FLAG_ORIG_PTE_VALID; 49877267ec00SKirill A. Shutemov 49882994302bSJan Kara if (pte_none(vmf->orig_pte)) { 498982b0f8c3SJan Kara pte_unmap(vmf->pte); 499082b0f8c3SJan Kara vmf->pte = NULL; 49917267ec00SKirill A. Shutemov } 49927267ec00SKirill A. Shutemov } 49937267ec00SKirill A. Shutemov 49942bad466cSPeter Xu if (!vmf->pte) 49952bad466cSPeter Xu return do_pte_missing(vmf); 49967267ec00SKirill A. Shutemov 49972994302bSJan Kara if (!pte_present(vmf->orig_pte)) 49982994302bSJan Kara return do_swap_page(vmf); 49991da177e4SLinus Torvalds 50002994302bSJan Kara if (pte_protnone(vmf->orig_pte) && vma_is_accessible(vmf->vma)) 50012994302bSJan Kara return do_numa_page(vmf); 5002d10e63f2SMel Gorman 500382b0f8c3SJan Kara spin_lock(vmf->ptl); 50042994302bSJan Kara entry = vmf->orig_pte; 5005c33c7948SRyan Roberts if (unlikely(!pte_same(ptep_get(vmf->pte), entry))) { 50067df67697SBibo Mao update_mmu_tlb(vmf->vma, vmf->address, vmf->pte); 50078f4e2101SHugh Dickins goto unlock; 50087df67697SBibo Mao } 5009c89357e2SDavid Hildenbrand if (vmf->flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) { 5010f6f37321SLinus Torvalds if (!pte_write(entry)) 50112994302bSJan Kara return do_wp_page(vmf); 5012c89357e2SDavid Hildenbrand else if (likely(vmf->flags & FAULT_FLAG_WRITE)) 50131da177e4SLinus Torvalds entry = pte_mkdirty(entry); 50141da177e4SLinus Torvalds } 50151da177e4SLinus Torvalds entry = pte_mkyoung(entry); 501682b0f8c3SJan Kara if (ptep_set_access_flags(vmf->vma, vmf->address, vmf->pte, entry, 501782b0f8c3SJan Kara vmf->flags & FAULT_FLAG_WRITE)) { 50185003a2bdSMatthew Wilcox (Oracle) update_mmu_cache_range(vmf, vmf->vma, vmf->address, 50195003a2bdSMatthew Wilcox (Oracle) vmf->pte, 1); 50201a44e149SAndrea Arcangeli } else { 5021b7333b58SYang Shi /* Skip spurious TLB flush for retried page fault */ 5022b7333b58SYang Shi if (vmf->flags & FAULT_FLAG_TRIED) 5023b7333b58SYang Shi goto unlock; 50241a44e149SAndrea Arcangeli /* 50251a44e149SAndrea Arcangeli * This is needed only for protection faults but the arch code 50261a44e149SAndrea Arcangeli * is not yet telling us if this is a protection fault or not. 50271a44e149SAndrea Arcangeli * This still avoids useless tlb flushes for .text page faults 50281a44e149SAndrea Arcangeli * with threads. 50291a44e149SAndrea Arcangeli */ 503082b0f8c3SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) 503199c29133SGerald Schaefer flush_tlb_fix_spurious_fault(vmf->vma, vmf->address, 503299c29133SGerald Schaefer vmf->pte); 50331a44e149SAndrea Arcangeli } 50348f4e2101SHugh Dickins unlock: 503582b0f8c3SJan Kara pte_unmap_unlock(vmf->pte, vmf->ptl); 503683c54070SNick Piggin return 0; 50371da177e4SLinus Torvalds } 50381da177e4SLinus Torvalds 50391da177e4SLinus Torvalds /* 50404ec31152SMatthew Wilcox (Oracle) * On entry, we hold either the VMA lock or the mmap_lock 50414ec31152SMatthew Wilcox (Oracle) * (FAULT_FLAG_VMA_LOCK tells you which). If VM_FAULT_RETRY is set in 50424ec31152SMatthew Wilcox (Oracle) * the result, the mmap_lock is not held on exit. See filemap_fault() 50434ec31152SMatthew Wilcox (Oracle) * and __folio_lock_or_retry(). 50441da177e4SLinus Torvalds */ 50452b740303SSouptick Joarder static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma, 50462b740303SSouptick Joarder unsigned long address, unsigned int flags) 50471da177e4SLinus Torvalds { 504882b0f8c3SJan Kara struct vm_fault vmf = { 5049bae473a4SKirill A. Shutemov .vma = vma, 50501a29d85eSJan Kara .address = address & PAGE_MASK, 5051824ddc60SNadav Amit .real_address = address, 5052bae473a4SKirill A. Shutemov .flags = flags, 50530721ec8bSJan Kara .pgoff = linear_page_index(vma, address), 5054667240e0SJan Kara .gfp_mask = __get_fault_gfp_mask(vma), 5055bae473a4SKirill A. Shutemov }; 5056dcddffd4SKirill A. Shutemov struct mm_struct *mm = vma->vm_mm; 50577da4e2cbSYang Shi unsigned long vm_flags = vma->vm_flags; 50581da177e4SLinus Torvalds pgd_t *pgd; 5059c2febafcSKirill A. Shutemov p4d_t *p4d; 50602b740303SSouptick Joarder vm_fault_t ret; 50611da177e4SLinus Torvalds 50621da177e4SLinus Torvalds pgd = pgd_offset(mm, address); 5063c2febafcSKirill A. Shutemov p4d = p4d_alloc(mm, pgd, address); 5064c2febafcSKirill A. Shutemov if (!p4d) 5065c2febafcSKirill A. Shutemov return VM_FAULT_OOM; 5066a00cc7d9SMatthew Wilcox 5067c2febafcSKirill A. Shutemov vmf.pud = pud_alloc(mm, p4d, address); 5068a00cc7d9SMatthew Wilcox if (!vmf.pud) 5069c74df32cSHugh Dickins return VM_FAULT_OOM; 5070625110b5SThomas Hellstrom retry_pud: 50717da4e2cbSYang Shi if (pud_none(*vmf.pud) && 5072a7f4e6e4SZach O'Keefe hugepage_vma_check(vma, vm_flags, false, true, true)) { 5073a00cc7d9SMatthew Wilcox ret = create_huge_pud(&vmf); 5074a00cc7d9SMatthew Wilcox if (!(ret & VM_FAULT_FALLBACK)) 5075a00cc7d9SMatthew Wilcox return ret; 5076a00cc7d9SMatthew Wilcox } else { 5077a00cc7d9SMatthew Wilcox pud_t orig_pud = *vmf.pud; 5078a00cc7d9SMatthew Wilcox 5079a00cc7d9SMatthew Wilcox barrier(); 5080a00cc7d9SMatthew Wilcox if (pud_trans_huge(orig_pud) || pud_devmap(orig_pud)) { 5081a00cc7d9SMatthew Wilcox 5082c89357e2SDavid Hildenbrand /* 5083c89357e2SDavid Hildenbrand * TODO once we support anonymous PUDs: NUMA case and 5084c89357e2SDavid Hildenbrand * FAULT_FLAG_UNSHARE handling. 5085c89357e2SDavid Hildenbrand */ 5086c89357e2SDavid Hildenbrand if ((flags & FAULT_FLAG_WRITE) && !pud_write(orig_pud)) { 5087a00cc7d9SMatthew Wilcox ret = wp_huge_pud(&vmf, orig_pud); 5088a00cc7d9SMatthew Wilcox if (!(ret & VM_FAULT_FALLBACK)) 5089a00cc7d9SMatthew Wilcox return ret; 5090a00cc7d9SMatthew Wilcox } else { 5091a00cc7d9SMatthew Wilcox huge_pud_set_accessed(&vmf, orig_pud); 5092a00cc7d9SMatthew Wilcox return 0; 5093a00cc7d9SMatthew Wilcox } 5094a00cc7d9SMatthew Wilcox } 5095a00cc7d9SMatthew Wilcox } 5096a00cc7d9SMatthew Wilcox 5097a00cc7d9SMatthew Wilcox vmf.pmd = pmd_alloc(mm, vmf.pud, address); 509882b0f8c3SJan Kara if (!vmf.pmd) 5099c74df32cSHugh Dickins return VM_FAULT_OOM; 5100625110b5SThomas Hellstrom 5101625110b5SThomas Hellstrom /* Huge pud page fault raced with pmd_alloc? */ 5102625110b5SThomas Hellstrom if (pud_trans_unstable(vmf.pud)) 5103625110b5SThomas Hellstrom goto retry_pud; 5104625110b5SThomas Hellstrom 51057da4e2cbSYang Shi if (pmd_none(*vmf.pmd) && 5106a7f4e6e4SZach O'Keefe hugepage_vma_check(vma, vm_flags, false, true, true)) { 5107a2d58167SDave Jiang ret = create_huge_pmd(&vmf); 5108c0292554SKirill A. Shutemov if (!(ret & VM_FAULT_FALLBACK)) 5109c0292554SKirill A. Shutemov return ret; 511071e3aac0SAndrea Arcangeli } else { 511126e1a0c3SHugh Dickins vmf.orig_pmd = pmdp_get_lockless(vmf.pmd); 51121f1d06c3SDavid Rientjes 51135db4f15cSYang Shi if (unlikely(is_swap_pmd(vmf.orig_pmd))) { 511484c3fc4eSZi Yan VM_BUG_ON(thp_migration_supported() && 51155db4f15cSYang Shi !is_pmd_migration_entry(vmf.orig_pmd)); 51165db4f15cSYang Shi if (is_pmd_migration_entry(vmf.orig_pmd)) 511784c3fc4eSZi Yan pmd_migration_entry_wait(mm, vmf.pmd); 511884c3fc4eSZi Yan return 0; 511984c3fc4eSZi Yan } 51205db4f15cSYang Shi if (pmd_trans_huge(vmf.orig_pmd) || pmd_devmap(vmf.orig_pmd)) { 51215db4f15cSYang Shi if (pmd_protnone(vmf.orig_pmd) && vma_is_accessible(vma)) 51225db4f15cSYang Shi return do_huge_pmd_numa_page(&vmf); 5123d10e63f2SMel Gorman 5124c89357e2SDavid Hildenbrand if ((flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) && 5125c89357e2SDavid Hildenbrand !pmd_write(vmf.orig_pmd)) { 51265db4f15cSYang Shi ret = wp_huge_pmd(&vmf); 51279845cbbdSKirill A. Shutemov if (!(ret & VM_FAULT_FALLBACK)) 51281f1d06c3SDavid Rientjes return ret; 5129a1dd450bSWill Deacon } else { 51305db4f15cSYang Shi huge_pmd_set_accessed(&vmf); 513171e3aac0SAndrea Arcangeli return 0; 513271e3aac0SAndrea Arcangeli } 513371e3aac0SAndrea Arcangeli } 51349845cbbdSKirill A. Shutemov } 513571e3aac0SAndrea Arcangeli 513682b0f8c3SJan Kara return handle_pte_fault(&vmf); 51371da177e4SLinus Torvalds } 51381da177e4SLinus Torvalds 5139bce617edSPeter Xu /** 5140f0953a1bSIngo Molnar * mm_account_fault - Do page fault accounting 5141809ef83cSYang Li * @mm: mm from which memcg should be extracted. It can be NULL. 5142bce617edSPeter Xu * @regs: the pt_regs struct pointer. When set to NULL, will skip accounting 5143bce617edSPeter Xu * of perf event counters, but we'll still do the per-task accounting to 5144bce617edSPeter Xu * the task who triggered this page fault. 5145bce617edSPeter Xu * @address: the faulted address. 5146bce617edSPeter Xu * @flags: the fault flags. 5147bce617edSPeter Xu * @ret: the fault retcode. 5148bce617edSPeter Xu * 5149f0953a1bSIngo Molnar * This will take care of most of the page fault accounting. Meanwhile, it 5150bce617edSPeter Xu * will also include the PERF_COUNT_SW_PAGE_FAULTS_[MAJ|MIN] perf counter 5151f0953a1bSIngo Molnar * updates. However, note that the handling of PERF_COUNT_SW_PAGE_FAULTS should 5152bce617edSPeter Xu * still be in per-arch page fault handlers at the entry of page fault. 5153bce617edSPeter Xu */ 515453156443SSuren Baghdasaryan static inline void mm_account_fault(struct mm_struct *mm, struct pt_regs *regs, 5155bce617edSPeter Xu unsigned long address, unsigned int flags, 5156bce617edSPeter Xu vm_fault_t ret) 5157bce617edSPeter Xu { 5158bce617edSPeter Xu bool major; 5159bce617edSPeter Xu 516053156443SSuren Baghdasaryan /* Incomplete faults will be accounted upon completion. */ 516153156443SSuren Baghdasaryan if (ret & VM_FAULT_RETRY) 516253156443SSuren Baghdasaryan return; 516353156443SSuren Baghdasaryan 5164bce617edSPeter Xu /* 516553156443SSuren Baghdasaryan * To preserve the behavior of older kernels, PGFAULT counters record 516653156443SSuren Baghdasaryan * both successful and failed faults, as opposed to perf counters, 516753156443SSuren Baghdasaryan * which ignore failed cases. 5168bce617edSPeter Xu */ 516953156443SSuren Baghdasaryan count_vm_event(PGFAULT); 517053156443SSuren Baghdasaryan count_memcg_event_mm(mm, PGFAULT); 517153156443SSuren Baghdasaryan 517253156443SSuren Baghdasaryan /* 517353156443SSuren Baghdasaryan * Do not account for unsuccessful faults (e.g. when the address wasn't 517453156443SSuren Baghdasaryan * valid). That includes arch_vma_access_permitted() failing before 517553156443SSuren Baghdasaryan * reaching here. So this is not a "this many hardware page faults" 517653156443SSuren Baghdasaryan * counter. We should use the hw profiling for that. 517753156443SSuren Baghdasaryan */ 517853156443SSuren Baghdasaryan if (ret & VM_FAULT_ERROR) 5179bce617edSPeter Xu return; 5180bce617edSPeter Xu 5181bce617edSPeter Xu /* 5182bce617edSPeter Xu * We define the fault as a major fault when the final successful fault 5183bce617edSPeter Xu * is VM_FAULT_MAJOR, or if it retried (which implies that we couldn't 5184bce617edSPeter Xu * handle it immediately previously). 5185bce617edSPeter Xu */ 5186bce617edSPeter Xu major = (ret & VM_FAULT_MAJOR) || (flags & FAULT_FLAG_TRIED); 5187bce617edSPeter Xu 5188a2beb5f1SPeter Xu if (major) 5189a2beb5f1SPeter Xu current->maj_flt++; 5190a2beb5f1SPeter Xu else 5191a2beb5f1SPeter Xu current->min_flt++; 5192a2beb5f1SPeter Xu 5193bce617edSPeter Xu /* 5194a2beb5f1SPeter Xu * If the fault is done for GUP, regs will be NULL. We only do the 5195a2beb5f1SPeter Xu * accounting for the per thread fault counters who triggered the 5196a2beb5f1SPeter Xu * fault, and we skip the perf event updates. 5197bce617edSPeter Xu */ 5198bce617edSPeter Xu if (!regs) 5199bce617edSPeter Xu return; 5200bce617edSPeter Xu 5201a2beb5f1SPeter Xu if (major) 5202bce617edSPeter Xu perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address); 5203a2beb5f1SPeter Xu else 5204bce617edSPeter Xu perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address); 5205bce617edSPeter Xu } 5206bce617edSPeter Xu 5207ec1c86b2SYu Zhao #ifdef CONFIG_LRU_GEN 5208ec1c86b2SYu Zhao static void lru_gen_enter_fault(struct vm_area_struct *vma) 5209ec1c86b2SYu Zhao { 52108788f678SYu Zhao /* the LRU algorithm only applies to accesses with recency */ 52118788f678SYu Zhao current->in_lru_fault = vma_has_recency(vma); 5212ec1c86b2SYu Zhao } 5213ec1c86b2SYu Zhao 5214ec1c86b2SYu Zhao static void lru_gen_exit_fault(void) 5215ec1c86b2SYu Zhao { 5216ec1c86b2SYu Zhao current->in_lru_fault = false; 5217ec1c86b2SYu Zhao } 5218ec1c86b2SYu Zhao #else 5219ec1c86b2SYu Zhao static void lru_gen_enter_fault(struct vm_area_struct *vma) 5220ec1c86b2SYu Zhao { 5221ec1c86b2SYu Zhao } 5222ec1c86b2SYu Zhao 5223ec1c86b2SYu Zhao static void lru_gen_exit_fault(void) 5224ec1c86b2SYu Zhao { 5225ec1c86b2SYu Zhao } 5226ec1c86b2SYu Zhao #endif /* CONFIG_LRU_GEN */ 5227ec1c86b2SYu Zhao 5228cdc5021cSDavid Hildenbrand static vm_fault_t sanitize_fault_flags(struct vm_area_struct *vma, 5229cdc5021cSDavid Hildenbrand unsigned int *flags) 5230cdc5021cSDavid Hildenbrand { 5231cdc5021cSDavid Hildenbrand if (unlikely(*flags & FAULT_FLAG_UNSHARE)) { 5232cdc5021cSDavid Hildenbrand if (WARN_ON_ONCE(*flags & FAULT_FLAG_WRITE)) 5233cdc5021cSDavid Hildenbrand return VM_FAULT_SIGSEGV; 5234cdc5021cSDavid Hildenbrand /* 5235cdc5021cSDavid Hildenbrand * FAULT_FLAG_UNSHARE only applies to COW mappings. Let's 5236cdc5021cSDavid Hildenbrand * just treat it like an ordinary read-fault otherwise. 5237cdc5021cSDavid Hildenbrand */ 5238cdc5021cSDavid Hildenbrand if (!is_cow_mapping(vma->vm_flags)) 5239cdc5021cSDavid Hildenbrand *flags &= ~FAULT_FLAG_UNSHARE; 524079881fedSDavid Hildenbrand } else if (*flags & FAULT_FLAG_WRITE) { 524179881fedSDavid Hildenbrand /* Write faults on read-only mappings are impossible ... */ 524279881fedSDavid Hildenbrand if (WARN_ON_ONCE(!(vma->vm_flags & VM_MAYWRITE))) 524379881fedSDavid Hildenbrand return VM_FAULT_SIGSEGV; 524479881fedSDavid Hildenbrand /* ... and FOLL_FORCE only applies to COW mappings. */ 524579881fedSDavid Hildenbrand if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE) && 524679881fedSDavid Hildenbrand !is_cow_mapping(vma->vm_flags))) 524779881fedSDavid Hildenbrand return VM_FAULT_SIGSEGV; 5248cdc5021cSDavid Hildenbrand } 52494089eef0SSuren Baghdasaryan #ifdef CONFIG_PER_VMA_LOCK 52504089eef0SSuren Baghdasaryan /* 52514089eef0SSuren Baghdasaryan * Per-VMA locks can't be used with FAULT_FLAG_RETRY_NOWAIT because of 52524089eef0SSuren Baghdasaryan * the assumption that lock is dropped on VM_FAULT_RETRY. 52534089eef0SSuren Baghdasaryan */ 52544089eef0SSuren Baghdasaryan if (WARN_ON_ONCE((*flags & 52554089eef0SSuren Baghdasaryan (FAULT_FLAG_VMA_LOCK | FAULT_FLAG_RETRY_NOWAIT)) == 52564089eef0SSuren Baghdasaryan (FAULT_FLAG_VMA_LOCK | FAULT_FLAG_RETRY_NOWAIT))) 52574089eef0SSuren Baghdasaryan return VM_FAULT_SIGSEGV; 52584089eef0SSuren Baghdasaryan #endif 52594089eef0SSuren Baghdasaryan 5260cdc5021cSDavid Hildenbrand return 0; 5261cdc5021cSDavid Hildenbrand } 5262cdc5021cSDavid Hildenbrand 52639a95f3cfSPaul Cassella /* 52649a95f3cfSPaul Cassella * By the time we get here, we already hold the mm semaphore 52659a95f3cfSPaul Cassella * 5266c1e8d7c6SMichel Lespinasse * The mmap_lock may have been released depending on flags and our 52679138e47eSMatthew Wilcox (Oracle) * return value. See filemap_fault() and __folio_lock_or_retry(). 52689a95f3cfSPaul Cassella */ 52692b740303SSouptick Joarder vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address, 5270bce617edSPeter Xu unsigned int flags, struct pt_regs *regs) 5271519e5247SJohannes Weiner { 527253156443SSuren Baghdasaryan /* If the fault handler drops the mmap_lock, vma may be freed */ 527353156443SSuren Baghdasaryan struct mm_struct *mm = vma->vm_mm; 52742b740303SSouptick Joarder vm_fault_t ret; 5275519e5247SJohannes Weiner 5276519e5247SJohannes Weiner __set_current_state(TASK_RUNNING); 5277519e5247SJohannes Weiner 5278cdc5021cSDavid Hildenbrand ret = sanitize_fault_flags(vma, &flags); 5279cdc5021cSDavid Hildenbrand if (ret) 528053156443SSuren Baghdasaryan goto out; 5281cdc5021cSDavid Hildenbrand 5282de0c799bSLaurent Dufour if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE, 5283de0c799bSLaurent Dufour flags & FAULT_FLAG_INSTRUCTION, 528453156443SSuren Baghdasaryan flags & FAULT_FLAG_REMOTE)) { 528553156443SSuren Baghdasaryan ret = VM_FAULT_SIGSEGV; 528653156443SSuren Baghdasaryan goto out; 528753156443SSuren Baghdasaryan } 5288de0c799bSLaurent Dufour 5289519e5247SJohannes Weiner /* 5290519e5247SJohannes Weiner * Enable the memcg OOM handling for faults triggered in user 5291519e5247SJohannes Weiner * space. Kernel faults are handled more gracefully. 5292519e5247SJohannes Weiner */ 5293519e5247SJohannes Weiner if (flags & FAULT_FLAG_USER) 529429ef680aSMichal Hocko mem_cgroup_enter_user_fault(); 5295519e5247SJohannes Weiner 5296ec1c86b2SYu Zhao lru_gen_enter_fault(vma); 5297ec1c86b2SYu Zhao 5298bae473a4SKirill A. Shutemov if (unlikely(is_vm_hugetlb_page(vma))) 5299bae473a4SKirill A. Shutemov ret = hugetlb_fault(vma->vm_mm, vma, address, flags); 5300bae473a4SKirill A. Shutemov else 5301dcddffd4SKirill A. Shutemov ret = __handle_mm_fault(vma, address, flags); 5302519e5247SJohannes Weiner 5303ec1c86b2SYu Zhao lru_gen_exit_fault(); 5304ec1c86b2SYu Zhao 530549426420SJohannes Weiner if (flags & FAULT_FLAG_USER) { 530629ef680aSMichal Hocko mem_cgroup_exit_user_fault(); 530749426420SJohannes Weiner /* 530849426420SJohannes Weiner * The task may have entered a memcg OOM situation but 530949426420SJohannes Weiner * if the allocation error was handled gracefully (no 531049426420SJohannes Weiner * VM_FAULT_OOM), there is no need to kill anything. 531149426420SJohannes Weiner * Just clean up the OOM state peacefully. 531249426420SJohannes Weiner */ 531349426420SJohannes Weiner if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM)) 531449426420SJohannes Weiner mem_cgroup_oom_synchronize(false); 531549426420SJohannes Weiner } 531653156443SSuren Baghdasaryan out: 531753156443SSuren Baghdasaryan mm_account_fault(mm, regs, address, flags, ret); 5318bce617edSPeter Xu 5319519e5247SJohannes Weiner return ret; 5320519e5247SJohannes Weiner } 5321e1d6d01aSJesse Barnes EXPORT_SYMBOL_GPL(handle_mm_fault); 5322519e5247SJohannes Weiner 5323c2508ec5SLinus Torvalds #ifdef CONFIG_LOCK_MM_AND_FIND_VMA 5324c2508ec5SLinus Torvalds #include <linux/extable.h> 5325c2508ec5SLinus Torvalds 5326c2508ec5SLinus Torvalds static inline bool get_mmap_lock_carefully(struct mm_struct *mm, struct pt_regs *regs) 5327c2508ec5SLinus Torvalds { 5328c2508ec5SLinus Torvalds /* Even if this succeeds, make it clear we *might* have slept */ 5329c2508ec5SLinus Torvalds if (likely(mmap_read_trylock(mm))) { 5330c2508ec5SLinus Torvalds might_sleep(); 5331c2508ec5SLinus Torvalds return true; 5332c2508ec5SLinus Torvalds } 5333c2508ec5SLinus Torvalds 5334c2508ec5SLinus Torvalds if (regs && !user_mode(regs)) { 5335c2508ec5SLinus Torvalds unsigned long ip = instruction_pointer(regs); 5336c2508ec5SLinus Torvalds if (!search_exception_tables(ip)) 5337c2508ec5SLinus Torvalds return false; 5338c2508ec5SLinus Torvalds } 5339c2508ec5SLinus Torvalds 5340eda00472SLinus Torvalds return !mmap_read_lock_killable(mm); 5341c2508ec5SLinus Torvalds } 5342c2508ec5SLinus Torvalds 5343c2508ec5SLinus Torvalds static inline bool mmap_upgrade_trylock(struct mm_struct *mm) 5344c2508ec5SLinus Torvalds { 5345c2508ec5SLinus Torvalds /* 5346c2508ec5SLinus Torvalds * We don't have this operation yet. 5347c2508ec5SLinus Torvalds * 5348c2508ec5SLinus Torvalds * It should be easy enough to do: it's basically a 5349c2508ec5SLinus Torvalds * atomic_long_try_cmpxchg_acquire() 5350c2508ec5SLinus Torvalds * from RWSEM_READER_BIAS -> RWSEM_WRITER_LOCKED, but 5351c2508ec5SLinus Torvalds * it also needs the proper lockdep magic etc. 5352c2508ec5SLinus Torvalds */ 5353c2508ec5SLinus Torvalds return false; 5354c2508ec5SLinus Torvalds } 5355c2508ec5SLinus Torvalds 5356c2508ec5SLinus Torvalds static inline bool upgrade_mmap_lock_carefully(struct mm_struct *mm, struct pt_regs *regs) 5357c2508ec5SLinus Torvalds { 5358c2508ec5SLinus Torvalds mmap_read_unlock(mm); 5359c2508ec5SLinus Torvalds if (regs && !user_mode(regs)) { 5360c2508ec5SLinus Torvalds unsigned long ip = instruction_pointer(regs); 5361c2508ec5SLinus Torvalds if (!search_exception_tables(ip)) 5362c2508ec5SLinus Torvalds return false; 5363c2508ec5SLinus Torvalds } 5364eda00472SLinus Torvalds return !mmap_write_lock_killable(mm); 5365c2508ec5SLinus Torvalds } 5366c2508ec5SLinus Torvalds 5367c2508ec5SLinus Torvalds /* 5368c2508ec5SLinus Torvalds * Helper for page fault handling. 5369c2508ec5SLinus Torvalds * 5370c2508ec5SLinus Torvalds * This is kind of equivalend to "mmap_read_lock()" followed 5371c2508ec5SLinus Torvalds * by "find_extend_vma()", except it's a lot more careful about 5372c2508ec5SLinus Torvalds * the locking (and will drop the lock on failure). 5373c2508ec5SLinus Torvalds * 5374c2508ec5SLinus Torvalds * For example, if we have a kernel bug that causes a page 5375c2508ec5SLinus Torvalds * fault, we don't want to just use mmap_read_lock() to get 5376c2508ec5SLinus Torvalds * the mm lock, because that would deadlock if the bug were 5377c2508ec5SLinus Torvalds * to happen while we're holding the mm lock for writing. 5378c2508ec5SLinus Torvalds * 5379c2508ec5SLinus Torvalds * So this checks the exception tables on kernel faults in 5380c2508ec5SLinus Torvalds * order to only do this all for instructions that are actually 5381c2508ec5SLinus Torvalds * expected to fault. 5382c2508ec5SLinus Torvalds * 5383c2508ec5SLinus Torvalds * We can also actually take the mm lock for writing if we 5384c2508ec5SLinus Torvalds * need to extend the vma, which helps the VM layer a lot. 5385c2508ec5SLinus Torvalds */ 5386c2508ec5SLinus Torvalds struct vm_area_struct *lock_mm_and_find_vma(struct mm_struct *mm, 5387c2508ec5SLinus Torvalds unsigned long addr, struct pt_regs *regs) 5388c2508ec5SLinus Torvalds { 5389c2508ec5SLinus Torvalds struct vm_area_struct *vma; 5390c2508ec5SLinus Torvalds 5391c2508ec5SLinus Torvalds if (!get_mmap_lock_carefully(mm, regs)) 5392c2508ec5SLinus Torvalds return NULL; 5393c2508ec5SLinus Torvalds 5394c2508ec5SLinus Torvalds vma = find_vma(mm, addr); 5395c2508ec5SLinus Torvalds if (likely(vma && (vma->vm_start <= addr))) 5396c2508ec5SLinus Torvalds return vma; 5397c2508ec5SLinus Torvalds 5398c2508ec5SLinus Torvalds /* 5399c2508ec5SLinus Torvalds * Well, dang. We might still be successful, but only 5400c2508ec5SLinus Torvalds * if we can extend a vma to do so. 5401c2508ec5SLinus Torvalds */ 5402c2508ec5SLinus Torvalds if (!vma || !(vma->vm_flags & VM_GROWSDOWN)) { 5403c2508ec5SLinus Torvalds mmap_read_unlock(mm); 5404c2508ec5SLinus Torvalds return NULL; 5405c2508ec5SLinus Torvalds } 5406c2508ec5SLinus Torvalds 5407c2508ec5SLinus Torvalds /* 5408c2508ec5SLinus Torvalds * We can try to upgrade the mmap lock atomically, 5409c2508ec5SLinus Torvalds * in which case we can continue to use the vma 5410c2508ec5SLinus Torvalds * we already looked up. 5411c2508ec5SLinus Torvalds * 5412c2508ec5SLinus Torvalds * Otherwise we'll have to drop the mmap lock and 5413c2508ec5SLinus Torvalds * re-take it, and also look up the vma again, 5414c2508ec5SLinus Torvalds * re-checking it. 5415c2508ec5SLinus Torvalds */ 5416c2508ec5SLinus Torvalds if (!mmap_upgrade_trylock(mm)) { 5417c2508ec5SLinus Torvalds if (!upgrade_mmap_lock_carefully(mm, regs)) 5418c2508ec5SLinus Torvalds return NULL; 5419c2508ec5SLinus Torvalds 5420c2508ec5SLinus Torvalds vma = find_vma(mm, addr); 5421c2508ec5SLinus Torvalds if (!vma) 5422c2508ec5SLinus Torvalds goto fail; 5423c2508ec5SLinus Torvalds if (vma->vm_start <= addr) 5424c2508ec5SLinus Torvalds goto success; 5425c2508ec5SLinus Torvalds if (!(vma->vm_flags & VM_GROWSDOWN)) 5426c2508ec5SLinus Torvalds goto fail; 5427c2508ec5SLinus Torvalds } 5428c2508ec5SLinus Torvalds 54298d7071afSLinus Torvalds if (expand_stack_locked(vma, addr)) 5430c2508ec5SLinus Torvalds goto fail; 5431c2508ec5SLinus Torvalds 5432c2508ec5SLinus Torvalds success: 5433c2508ec5SLinus Torvalds mmap_write_downgrade(mm); 5434c2508ec5SLinus Torvalds return vma; 5435c2508ec5SLinus Torvalds 5436c2508ec5SLinus Torvalds fail: 5437c2508ec5SLinus Torvalds mmap_write_unlock(mm); 5438c2508ec5SLinus Torvalds return NULL; 5439c2508ec5SLinus Torvalds } 5440c2508ec5SLinus Torvalds #endif 5441c2508ec5SLinus Torvalds 544250ee3253SSuren Baghdasaryan #ifdef CONFIG_PER_VMA_LOCK 544350ee3253SSuren Baghdasaryan /* 544450ee3253SSuren Baghdasaryan * Lookup and lock a VMA under RCU protection. Returned VMA is guaranteed to be 544550ee3253SSuren Baghdasaryan * stable and not isolated. If the VMA is not found or is being modified the 544650ee3253SSuren Baghdasaryan * function returns NULL. 544750ee3253SSuren Baghdasaryan */ 544850ee3253SSuren Baghdasaryan struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm, 544950ee3253SSuren Baghdasaryan unsigned long address) 545050ee3253SSuren Baghdasaryan { 545150ee3253SSuren Baghdasaryan MA_STATE(mas, &mm->mm_mt, address, address); 545250ee3253SSuren Baghdasaryan struct vm_area_struct *vma; 545350ee3253SSuren Baghdasaryan 545450ee3253SSuren Baghdasaryan rcu_read_lock(); 545550ee3253SSuren Baghdasaryan retry: 545650ee3253SSuren Baghdasaryan vma = mas_walk(&mas); 545750ee3253SSuren Baghdasaryan if (!vma) 545850ee3253SSuren Baghdasaryan goto inval; 545950ee3253SSuren Baghdasaryan 546050ee3253SSuren Baghdasaryan if (!vma_start_read(vma)) 546150ee3253SSuren Baghdasaryan goto inval; 546250ee3253SSuren Baghdasaryan 5463444eeb17SSuren Baghdasaryan /* 5464657b5146SJann Horn * find_mergeable_anon_vma uses adjacent vmas which are not locked. 5465657b5146SJann Horn * This check must happen after vma_start_read(); otherwise, a 5466657b5146SJann Horn * concurrent mremap() with MREMAP_DONTUNMAP could dissociate the VMA 5467657b5146SJann Horn * from its anon_vma. 5468657b5146SJann Horn */ 546929a22b9eSSuren Baghdasaryan if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma)) 5470657b5146SJann Horn goto inval_end_read; 5471444eeb17SSuren Baghdasaryan 547250ee3253SSuren Baghdasaryan /* Check since vm_start/vm_end might change before we lock the VMA */ 5473657b5146SJann Horn if (unlikely(address < vma->vm_start || address >= vma->vm_end)) 5474657b5146SJann Horn goto inval_end_read; 547550ee3253SSuren Baghdasaryan 547650ee3253SSuren Baghdasaryan /* Check if the VMA got isolated after we found it */ 547750ee3253SSuren Baghdasaryan if (vma->detached) { 547850ee3253SSuren Baghdasaryan vma_end_read(vma); 547952f23865SSuren Baghdasaryan count_vm_vma_lock_event(VMA_LOCK_MISS); 548050ee3253SSuren Baghdasaryan /* The area was replaced with another one */ 548150ee3253SSuren Baghdasaryan goto retry; 548250ee3253SSuren Baghdasaryan } 548350ee3253SSuren Baghdasaryan 548450ee3253SSuren Baghdasaryan rcu_read_unlock(); 548550ee3253SSuren Baghdasaryan return vma; 5486657b5146SJann Horn 5487657b5146SJann Horn inval_end_read: 5488657b5146SJann Horn vma_end_read(vma); 548950ee3253SSuren Baghdasaryan inval: 549050ee3253SSuren Baghdasaryan rcu_read_unlock(); 549152f23865SSuren Baghdasaryan count_vm_vma_lock_event(VMA_LOCK_ABORT); 549250ee3253SSuren Baghdasaryan return NULL; 549350ee3253SSuren Baghdasaryan } 549450ee3253SSuren Baghdasaryan #endif /* CONFIG_PER_VMA_LOCK */ 549550ee3253SSuren Baghdasaryan 549690eceff1SKirill A. Shutemov #ifndef __PAGETABLE_P4D_FOLDED 549790eceff1SKirill A. Shutemov /* 549890eceff1SKirill A. Shutemov * Allocate p4d page table. 549990eceff1SKirill A. Shutemov * We've already handled the fast-path in-line. 550090eceff1SKirill A. Shutemov */ 550190eceff1SKirill A. Shutemov int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address) 550290eceff1SKirill A. Shutemov { 550390eceff1SKirill A. Shutemov p4d_t *new = p4d_alloc_one(mm, address); 550490eceff1SKirill A. Shutemov if (!new) 550590eceff1SKirill A. Shutemov return -ENOMEM; 550690eceff1SKirill A. Shutemov 550790eceff1SKirill A. Shutemov spin_lock(&mm->page_table_lock); 5508ed33b5a6SQi Zheng if (pgd_present(*pgd)) { /* Another has populated it */ 550990eceff1SKirill A. Shutemov p4d_free(mm, new); 5510ed33b5a6SQi Zheng } else { 5511ed33b5a6SQi Zheng smp_wmb(); /* See comment in pmd_install() */ 551290eceff1SKirill A. Shutemov pgd_populate(mm, pgd, new); 5513ed33b5a6SQi Zheng } 551490eceff1SKirill A. Shutemov spin_unlock(&mm->page_table_lock); 551590eceff1SKirill A. Shutemov return 0; 551690eceff1SKirill A. Shutemov } 551790eceff1SKirill A. Shutemov #endif /* __PAGETABLE_P4D_FOLDED */ 551890eceff1SKirill A. Shutemov 55191da177e4SLinus Torvalds #ifndef __PAGETABLE_PUD_FOLDED 55201da177e4SLinus Torvalds /* 55211da177e4SLinus Torvalds * Allocate page upper directory. 5522872fec16SHugh Dickins * We've already handled the fast-path in-line. 55231da177e4SLinus Torvalds */ 5524c2febafcSKirill A. Shutemov int __pud_alloc(struct mm_struct *mm, p4d_t *p4d, unsigned long address) 55251da177e4SLinus Torvalds { 5526c74df32cSHugh Dickins pud_t *new = pud_alloc_one(mm, address); 5527c74df32cSHugh Dickins if (!new) 55281bb3630eSHugh Dickins return -ENOMEM; 55291da177e4SLinus Torvalds 5530872fec16SHugh Dickins spin_lock(&mm->page_table_lock); 5531b4e98d9aSKirill A. Shutemov if (!p4d_present(*p4d)) { 5532b4e98d9aSKirill A. Shutemov mm_inc_nr_puds(mm); 5533ed33b5a6SQi Zheng smp_wmb(); /* See comment in pmd_install() */ 5534c2febafcSKirill A. Shutemov p4d_populate(mm, p4d, new); 5535b4e98d9aSKirill A. Shutemov } else /* Another has populated it */ 5536c2febafcSKirill A. Shutemov pud_free(mm, new); 5537872fec16SHugh Dickins spin_unlock(&mm->page_table_lock); 55381bb3630eSHugh Dickins return 0; 55391da177e4SLinus Torvalds } 55401da177e4SLinus Torvalds #endif /* __PAGETABLE_PUD_FOLDED */ 55411da177e4SLinus Torvalds 55421da177e4SLinus Torvalds #ifndef __PAGETABLE_PMD_FOLDED 55431da177e4SLinus Torvalds /* 55441da177e4SLinus Torvalds * Allocate page middle directory. 5545872fec16SHugh Dickins * We've already handled the fast-path in-line. 55461da177e4SLinus Torvalds */ 55471bb3630eSHugh Dickins int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) 55481da177e4SLinus Torvalds { 5549a00cc7d9SMatthew Wilcox spinlock_t *ptl; 5550c74df32cSHugh Dickins pmd_t *new = pmd_alloc_one(mm, address); 5551c74df32cSHugh Dickins if (!new) 55521bb3630eSHugh Dickins return -ENOMEM; 55531da177e4SLinus Torvalds 5554a00cc7d9SMatthew Wilcox ptl = pud_lock(mm, pud); 5555dc6c9a35SKirill A. Shutemov if (!pud_present(*pud)) { 5556dc6c9a35SKirill A. Shutemov mm_inc_nr_pmds(mm); 5557ed33b5a6SQi Zheng smp_wmb(); /* See comment in pmd_install() */ 55581da177e4SLinus Torvalds pud_populate(mm, pud, new); 5559ed33b5a6SQi Zheng } else { /* Another has populated it */ 55605e541973SBenjamin Herrenschmidt pmd_free(mm, new); 5561ed33b5a6SQi Zheng } 5562a00cc7d9SMatthew Wilcox spin_unlock(ptl); 55631bb3630eSHugh Dickins return 0; 55641da177e4SLinus Torvalds } 55651da177e4SLinus Torvalds #endif /* __PAGETABLE_PMD_FOLDED */ 55661da177e4SLinus Torvalds 55673b6748e2SJohannes Weiner /** 55689fd6dad1SPaolo Bonzini * follow_pte - look up PTE at a user virtual address 55699fd6dad1SPaolo Bonzini * @mm: the mm_struct of the target address space 55709fd6dad1SPaolo Bonzini * @address: user virtual address 55719fd6dad1SPaolo Bonzini * @ptepp: location to store found PTE 55729fd6dad1SPaolo Bonzini * @ptlp: location to store the lock for the PTE 55739fd6dad1SPaolo Bonzini * 55749fd6dad1SPaolo Bonzini * On a successful return, the pointer to the PTE is stored in @ptepp; 55759fd6dad1SPaolo Bonzini * the corresponding lock is taken and its location is stored in @ptlp. 55769fd6dad1SPaolo Bonzini * The contents of the PTE are only stable until @ptlp is released; 55779fd6dad1SPaolo Bonzini * any further use, if any, must be protected against invalidation 55789fd6dad1SPaolo Bonzini * with MMU notifiers. 55799fd6dad1SPaolo Bonzini * 55809fd6dad1SPaolo Bonzini * Only IO mappings and raw PFN mappings are allowed. The mmap semaphore 55819fd6dad1SPaolo Bonzini * should be taken for read. 55829fd6dad1SPaolo Bonzini * 55839fd6dad1SPaolo Bonzini * KVM uses this function. While it is arguably less bad than ``follow_pfn``, 55849fd6dad1SPaolo Bonzini * it is not a good general-purpose API. 55859fd6dad1SPaolo Bonzini * 55869fd6dad1SPaolo Bonzini * Return: zero on success, -ve otherwise. 55879fd6dad1SPaolo Bonzini */ 55889fd6dad1SPaolo Bonzini int follow_pte(struct mm_struct *mm, unsigned long address, 55899fd6dad1SPaolo Bonzini pte_t **ptepp, spinlock_t **ptlp) 55909fd6dad1SPaolo Bonzini { 55910e5e64c0SMuchun Song pgd_t *pgd; 55920e5e64c0SMuchun Song p4d_t *p4d; 55930e5e64c0SMuchun Song pud_t *pud; 55940e5e64c0SMuchun Song pmd_t *pmd; 55950e5e64c0SMuchun Song pte_t *ptep; 55960e5e64c0SMuchun Song 55970e5e64c0SMuchun Song pgd = pgd_offset(mm, address); 55980e5e64c0SMuchun Song if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) 55990e5e64c0SMuchun Song goto out; 56000e5e64c0SMuchun Song 56010e5e64c0SMuchun Song p4d = p4d_offset(pgd, address); 56020e5e64c0SMuchun Song if (p4d_none(*p4d) || unlikely(p4d_bad(*p4d))) 56030e5e64c0SMuchun Song goto out; 56040e5e64c0SMuchun Song 56050e5e64c0SMuchun Song pud = pud_offset(p4d, address); 56060e5e64c0SMuchun Song if (pud_none(*pud) || unlikely(pud_bad(*pud))) 56070e5e64c0SMuchun Song goto out; 56080e5e64c0SMuchun Song 56090e5e64c0SMuchun Song pmd = pmd_offset(pud, address); 56100e5e64c0SMuchun Song VM_BUG_ON(pmd_trans_huge(*pmd)); 56110e5e64c0SMuchun Song 56120e5e64c0SMuchun Song ptep = pte_offset_map_lock(mm, pmd, address, ptlp); 56133db82b93SHugh Dickins if (!ptep) 56143db82b93SHugh Dickins goto out; 5615c33c7948SRyan Roberts if (!pte_present(ptep_get(ptep))) 56160e5e64c0SMuchun Song goto unlock; 56170e5e64c0SMuchun Song *ptepp = ptep; 56180e5e64c0SMuchun Song return 0; 56190e5e64c0SMuchun Song unlock: 56200e5e64c0SMuchun Song pte_unmap_unlock(ptep, *ptlp); 56210e5e64c0SMuchun Song out: 56220e5e64c0SMuchun Song return -EINVAL; 56239fd6dad1SPaolo Bonzini } 56249fd6dad1SPaolo Bonzini EXPORT_SYMBOL_GPL(follow_pte); 56259fd6dad1SPaolo Bonzini 56269fd6dad1SPaolo Bonzini /** 56273b6748e2SJohannes Weiner * follow_pfn - look up PFN at a user virtual address 56283b6748e2SJohannes Weiner * @vma: memory mapping 56293b6748e2SJohannes Weiner * @address: user virtual address 56303b6748e2SJohannes Weiner * @pfn: location to store found PFN 56313b6748e2SJohannes Weiner * 56323b6748e2SJohannes Weiner * Only IO mappings and raw PFN mappings are allowed. 56333b6748e2SJohannes Weiner * 56349fd6dad1SPaolo Bonzini * This function does not allow the caller to read the permissions 56359fd6dad1SPaolo Bonzini * of the PTE. Do not use it. 56369fd6dad1SPaolo Bonzini * 5637a862f68aSMike Rapoport * Return: zero and the pfn at @pfn on success, -ve otherwise. 56383b6748e2SJohannes Weiner */ 56393b6748e2SJohannes Weiner int follow_pfn(struct vm_area_struct *vma, unsigned long address, 56403b6748e2SJohannes Weiner unsigned long *pfn) 56413b6748e2SJohannes Weiner { 56423b6748e2SJohannes Weiner int ret = -EINVAL; 56433b6748e2SJohannes Weiner spinlock_t *ptl; 56443b6748e2SJohannes Weiner pte_t *ptep; 56453b6748e2SJohannes Weiner 56463b6748e2SJohannes Weiner if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 56473b6748e2SJohannes Weiner return ret; 56483b6748e2SJohannes Weiner 56499fd6dad1SPaolo Bonzini ret = follow_pte(vma->vm_mm, address, &ptep, &ptl); 56503b6748e2SJohannes Weiner if (ret) 56513b6748e2SJohannes Weiner return ret; 5652c33c7948SRyan Roberts *pfn = pte_pfn(ptep_get(ptep)); 56533b6748e2SJohannes Weiner pte_unmap_unlock(ptep, ptl); 56543b6748e2SJohannes Weiner return 0; 56553b6748e2SJohannes Weiner } 56563b6748e2SJohannes Weiner EXPORT_SYMBOL(follow_pfn); 56573b6748e2SJohannes Weiner 565828b2ee20SRik van Riel #ifdef CONFIG_HAVE_IOREMAP_PROT 5659d87fe660Svenkatesh.pallipadi@intel.com int follow_phys(struct vm_area_struct *vma, 566028b2ee20SRik van Riel unsigned long address, unsigned int flags, 5661d87fe660Svenkatesh.pallipadi@intel.com unsigned long *prot, resource_size_t *phys) 566228b2ee20SRik van Riel { 566303668a4dSJohannes Weiner int ret = -EINVAL; 566428b2ee20SRik van Riel pte_t *ptep, pte; 566528b2ee20SRik van Riel spinlock_t *ptl; 566628b2ee20SRik van Riel 5667d87fe660Svenkatesh.pallipadi@intel.com if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 5668d87fe660Svenkatesh.pallipadi@intel.com goto out; 566928b2ee20SRik van Riel 56709fd6dad1SPaolo Bonzini if (follow_pte(vma->vm_mm, address, &ptep, &ptl)) 5671d87fe660Svenkatesh.pallipadi@intel.com goto out; 5672c33c7948SRyan Roberts pte = ptep_get(ptep); 567303668a4dSJohannes Weiner 5674f6f37321SLinus Torvalds if ((flags & FOLL_WRITE) && !pte_write(pte)) 567528b2ee20SRik van Riel goto unlock; 567628b2ee20SRik van Riel 567728b2ee20SRik van Riel *prot = pgprot_val(pte_pgprot(pte)); 567803668a4dSJohannes Weiner *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT; 567928b2ee20SRik van Riel 568003668a4dSJohannes Weiner ret = 0; 568128b2ee20SRik van Riel unlock: 568228b2ee20SRik van Riel pte_unmap_unlock(ptep, ptl); 568328b2ee20SRik van Riel out: 5684d87fe660Svenkatesh.pallipadi@intel.com return ret; 568528b2ee20SRik van Riel } 568628b2ee20SRik van Riel 568796667f8aSDaniel Vetter /** 568896667f8aSDaniel Vetter * generic_access_phys - generic implementation for iomem mmap access 568996667f8aSDaniel Vetter * @vma: the vma to access 5690f0953a1bSIngo Molnar * @addr: userspace address, not relative offset within @vma 569196667f8aSDaniel Vetter * @buf: buffer to read/write 569296667f8aSDaniel Vetter * @len: length of transfer 569396667f8aSDaniel Vetter * @write: set to FOLL_WRITE when writing, otherwise reading 569496667f8aSDaniel Vetter * 569596667f8aSDaniel Vetter * This is a generic implementation for &vm_operations_struct.access for an 569696667f8aSDaniel Vetter * iomem mapping. This callback is used by access_process_vm() when the @vma is 569796667f8aSDaniel Vetter * not page based. 569896667f8aSDaniel Vetter */ 569928b2ee20SRik van Riel int generic_access_phys(struct vm_area_struct *vma, unsigned long addr, 570028b2ee20SRik van Riel void *buf, int len, int write) 570128b2ee20SRik van Riel { 570228b2ee20SRik van Riel resource_size_t phys_addr; 570328b2ee20SRik van Riel unsigned long prot = 0; 57042bc7273bSKOSAKI Motohiro void __iomem *maddr; 570596667f8aSDaniel Vetter pte_t *ptep, pte; 570696667f8aSDaniel Vetter spinlock_t *ptl; 570796667f8aSDaniel Vetter int offset = offset_in_page(addr); 570896667f8aSDaniel Vetter int ret = -EINVAL; 570928b2ee20SRik van Riel 571096667f8aSDaniel Vetter if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 571196667f8aSDaniel Vetter return -EINVAL; 571296667f8aSDaniel Vetter 571396667f8aSDaniel Vetter retry: 5714e913a8cdSLinus Torvalds if (follow_pte(vma->vm_mm, addr, &ptep, &ptl)) 571596667f8aSDaniel Vetter return -EINVAL; 5716c33c7948SRyan Roberts pte = ptep_get(ptep); 571796667f8aSDaniel Vetter pte_unmap_unlock(ptep, ptl); 571896667f8aSDaniel Vetter 571996667f8aSDaniel Vetter prot = pgprot_val(pte_pgprot(pte)); 572096667f8aSDaniel Vetter phys_addr = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT; 572196667f8aSDaniel Vetter 572296667f8aSDaniel Vetter if ((write & FOLL_WRITE) && !pte_write(pte)) 572328b2ee20SRik van Riel return -EINVAL; 572428b2ee20SRik van Riel 57259cb12d7bSGrazvydas Ignotas maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot); 572624eee1e4Sjie@chenjie6@huwei.com if (!maddr) 572724eee1e4Sjie@chenjie6@huwei.com return -ENOMEM; 572824eee1e4Sjie@chenjie6@huwei.com 5729e913a8cdSLinus Torvalds if (follow_pte(vma->vm_mm, addr, &ptep, &ptl)) 573096667f8aSDaniel Vetter goto out_unmap; 573196667f8aSDaniel Vetter 5732c33c7948SRyan Roberts if (!pte_same(pte, ptep_get(ptep))) { 573396667f8aSDaniel Vetter pte_unmap_unlock(ptep, ptl); 573496667f8aSDaniel Vetter iounmap(maddr); 573596667f8aSDaniel Vetter 573696667f8aSDaniel Vetter goto retry; 573796667f8aSDaniel Vetter } 573896667f8aSDaniel Vetter 573928b2ee20SRik van Riel if (write) 574028b2ee20SRik van Riel memcpy_toio(maddr + offset, buf, len); 574128b2ee20SRik van Riel else 574228b2ee20SRik van Riel memcpy_fromio(buf, maddr + offset, len); 574396667f8aSDaniel Vetter ret = len; 574496667f8aSDaniel Vetter pte_unmap_unlock(ptep, ptl); 574596667f8aSDaniel Vetter out_unmap: 574628b2ee20SRik van Riel iounmap(maddr); 574728b2ee20SRik van Riel 574896667f8aSDaniel Vetter return ret; 574928b2ee20SRik van Riel } 57505a73633eSUwe Kleine-König EXPORT_SYMBOL_GPL(generic_access_phys); 575128b2ee20SRik van Riel #endif 575228b2ee20SRik van Riel 57530ec76a11SDavid Howells /* 5754d3f5ffcaSJohn Hubbard * Access another process' address space as given in mm. 57550ec76a11SDavid Howells */ 5756d3f5ffcaSJohn Hubbard int __access_remote_vm(struct mm_struct *mm, unsigned long addr, void *buf, 5757d3f5ffcaSJohn Hubbard int len, unsigned int gup_flags) 57580ec76a11SDavid Howells { 57590ec76a11SDavid Howells void *old_buf = buf; 5760442486ecSLorenzo Stoakes int write = gup_flags & FOLL_WRITE; 57610ec76a11SDavid Howells 5762d8ed45c5SMichel Lespinasse if (mmap_read_lock_killable(mm)) 57631e426fe2SKonstantin Khlebnikov return 0; 57641e426fe2SKonstantin Khlebnikov 5765eee9c708SLinus Torvalds /* Avoid triggering the temporary warning in __get_user_pages */ 5766eee9c708SLinus Torvalds if (!vma_lookup(mm, addr) && !expand_stack(mm, addr)) 5767eee9c708SLinus Torvalds return 0; 5768eee9c708SLinus Torvalds 5769183ff22bSSimon Arlott /* ignore errors, just check how much was successfully transferred */ 57700ec76a11SDavid Howells while (len) { 5771ca5e8632SLorenzo Stoakes int bytes, offset; 57720ec76a11SDavid Howells void *maddr; 5773ca5e8632SLorenzo Stoakes struct vm_area_struct *vma = NULL; 5774ca5e8632SLorenzo Stoakes struct page *page = get_user_page_vma_remote(mm, addr, 5775ca5e8632SLorenzo Stoakes gup_flags, &vma); 57760ec76a11SDavid Howells 5777ca5e8632SLorenzo Stoakes if (IS_ERR_OR_NULL(page)) { 57788d7071afSLinus Torvalds /* We might need to expand the stack to access it */ 57798d7071afSLinus Torvalds vma = vma_lookup(mm, addr); 57808d7071afSLinus Torvalds if (!vma) { 57818d7071afSLinus Torvalds vma = expand_stack(mm, addr); 57829471f1f2SLinus Torvalds 57839471f1f2SLinus Torvalds /* mmap_lock was dropped on failure */ 57848d7071afSLinus Torvalds if (!vma) 57859471f1f2SLinus Torvalds return buf - old_buf; 57869471f1f2SLinus Torvalds 57879471f1f2SLinus Torvalds /* Try again if stack expansion worked */ 57889471f1f2SLinus Torvalds continue; 57898d7071afSLinus Torvalds } 57908d7071afSLinus Torvalds 5791ca5e8632SLorenzo Stoakes 579228b2ee20SRik van Riel /* 579328b2ee20SRik van Riel * Check if this is a VM_IO | VM_PFNMAP VMA, which 579428b2ee20SRik van Riel * we can access using slightly different code. 579528b2ee20SRik van Riel */ 57969471f1f2SLinus Torvalds bytes = 0; 57979471f1f2SLinus Torvalds #ifdef CONFIG_HAVE_IOREMAP_PROT 579828b2ee20SRik van Riel if (vma->vm_ops && vma->vm_ops->access) 57999471f1f2SLinus Torvalds bytes = vma->vm_ops->access(vma, addr, buf, 580028b2ee20SRik van Riel len, write); 5801dbffcd03SRik van Riel #endif 58029471f1f2SLinus Torvalds if (bytes <= 0) 58039471f1f2SLinus Torvalds break; 580428b2ee20SRik van Riel } else { 58050ec76a11SDavid Howells bytes = len; 58060ec76a11SDavid Howells offset = addr & (PAGE_SIZE-1); 58070ec76a11SDavid Howells if (bytes > PAGE_SIZE-offset) 58080ec76a11SDavid Howells bytes = PAGE_SIZE-offset; 58090ec76a11SDavid Howells 58100ec76a11SDavid Howells maddr = kmap(page); 58110ec76a11SDavid Howells if (write) { 58120ec76a11SDavid Howells copy_to_user_page(vma, page, addr, 58130ec76a11SDavid Howells maddr + offset, buf, bytes); 58140ec76a11SDavid Howells set_page_dirty_lock(page); 58150ec76a11SDavid Howells } else { 58160ec76a11SDavid Howells copy_from_user_page(vma, page, addr, 58170ec76a11SDavid Howells buf, maddr + offset, bytes); 58180ec76a11SDavid Howells } 58190ec76a11SDavid Howells kunmap(page); 582009cbfeafSKirill A. Shutemov put_page(page); 582128b2ee20SRik van Riel } 58220ec76a11SDavid Howells len -= bytes; 58230ec76a11SDavid Howells buf += bytes; 58240ec76a11SDavid Howells addr += bytes; 58250ec76a11SDavid Howells } 5826d8ed45c5SMichel Lespinasse mmap_read_unlock(mm); 58270ec76a11SDavid Howells 58280ec76a11SDavid Howells return buf - old_buf; 58290ec76a11SDavid Howells } 583003252919SAndi Kleen 58315ddd36b9SStephen Wilson /** 5832ae91dbfcSRandy Dunlap * access_remote_vm - access another process' address space 58335ddd36b9SStephen Wilson * @mm: the mm_struct of the target address space 58345ddd36b9SStephen Wilson * @addr: start address to access 58355ddd36b9SStephen Wilson * @buf: source or destination buffer 58365ddd36b9SStephen Wilson * @len: number of bytes to transfer 58376347e8d5SLorenzo Stoakes * @gup_flags: flags modifying lookup behaviour 58385ddd36b9SStephen Wilson * 58395ddd36b9SStephen Wilson * The caller must hold a reference on @mm. 5840a862f68aSMike Rapoport * 5841a862f68aSMike Rapoport * Return: number of bytes copied from source to destination. 58425ddd36b9SStephen Wilson */ 58435ddd36b9SStephen Wilson int access_remote_vm(struct mm_struct *mm, unsigned long addr, 58446347e8d5SLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 58455ddd36b9SStephen Wilson { 5846d3f5ffcaSJohn Hubbard return __access_remote_vm(mm, addr, buf, len, gup_flags); 58475ddd36b9SStephen Wilson } 58485ddd36b9SStephen Wilson 584903252919SAndi Kleen /* 5850206cb636SStephen Wilson * Access another process' address space. 5851206cb636SStephen Wilson * Source/target buffer must be kernel space, 5852206cb636SStephen Wilson * Do not walk the page table directly, use get_user_pages 5853206cb636SStephen Wilson */ 5854206cb636SStephen Wilson int access_process_vm(struct task_struct *tsk, unsigned long addr, 5855f307ab6dSLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 5856206cb636SStephen Wilson { 5857206cb636SStephen Wilson struct mm_struct *mm; 5858206cb636SStephen Wilson int ret; 5859206cb636SStephen Wilson 5860206cb636SStephen Wilson mm = get_task_mm(tsk); 5861206cb636SStephen Wilson if (!mm) 5862206cb636SStephen Wilson return 0; 5863206cb636SStephen Wilson 5864d3f5ffcaSJohn Hubbard ret = __access_remote_vm(mm, addr, buf, len, gup_flags); 5865442486ecSLorenzo Stoakes 5866206cb636SStephen Wilson mmput(mm); 5867206cb636SStephen Wilson 5868206cb636SStephen Wilson return ret; 5869206cb636SStephen Wilson } 5870fcd35857SCatalin Marinas EXPORT_SYMBOL_GPL(access_process_vm); 5871206cb636SStephen Wilson 587203252919SAndi Kleen /* 587303252919SAndi Kleen * Print the name of a VMA. 587403252919SAndi Kleen */ 587503252919SAndi Kleen void print_vma_addr(char *prefix, unsigned long ip) 587603252919SAndi Kleen { 587703252919SAndi Kleen struct mm_struct *mm = current->mm; 587803252919SAndi Kleen struct vm_area_struct *vma; 587903252919SAndi Kleen 5880e8bff74aSIngo Molnar /* 58810a7f682dSMichal Hocko * we might be running from an atomic context so we cannot sleep 5882e8bff74aSIngo Molnar */ 5883d8ed45c5SMichel Lespinasse if (!mmap_read_trylock(mm)) 5884e8bff74aSIngo Molnar return; 5885e8bff74aSIngo Molnar 588603252919SAndi Kleen vma = find_vma(mm, ip); 588703252919SAndi Kleen if (vma && vma->vm_file) { 588803252919SAndi Kleen struct file *f = vma->vm_file; 58890a7f682dSMichal Hocko char *buf = (char *)__get_free_page(GFP_NOWAIT); 589003252919SAndi Kleen if (buf) { 58912fbc57c5SAndy Shevchenko char *p; 589203252919SAndi Kleen 58939bf39ab2SMiklos Szeredi p = file_path(f, buf, PAGE_SIZE); 589403252919SAndi Kleen if (IS_ERR(p)) 589503252919SAndi Kleen p = "?"; 58962fbc57c5SAndy Shevchenko printk("%s%s[%lx+%lx]", prefix, kbasename(p), 589703252919SAndi Kleen vma->vm_start, 589803252919SAndi Kleen vma->vm_end - vma->vm_start); 589903252919SAndi Kleen free_page((unsigned long)buf); 590003252919SAndi Kleen } 590103252919SAndi Kleen } 5902d8ed45c5SMichel Lespinasse mmap_read_unlock(mm); 590303252919SAndi Kleen } 59043ee1afa3SNick Piggin 5905662bbcb2SMichael S. Tsirkin #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP) 59069ec23531SDavid Hildenbrand void __might_fault(const char *file, int line) 59073ee1afa3SNick Piggin { 59089ec23531SDavid Hildenbrand if (pagefault_disabled()) 5909662bbcb2SMichael S. Tsirkin return; 591042a38756SThomas Gleixner __might_sleep(file, line); 59119ec23531SDavid Hildenbrand #if defined(CONFIG_DEBUG_ATOMIC_SLEEP) 5912662bbcb2SMichael S. Tsirkin if (current->mm) 5913da1c55f1SMichel Lespinasse might_lock_read(¤t->mm->mmap_lock); 59149ec23531SDavid Hildenbrand #endif 59153ee1afa3SNick Piggin } 59169ec23531SDavid Hildenbrand EXPORT_SYMBOL(__might_fault); 59173ee1afa3SNick Piggin #endif 591847ad8475SAndrea Arcangeli 591947ad8475SAndrea Arcangeli #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS) 5920c6ddfb6cSHuang Ying /* 5921c6ddfb6cSHuang Ying * Process all subpages of the specified huge page with the specified 5922c6ddfb6cSHuang Ying * operation. The target subpage will be processed last to keep its 5923c6ddfb6cSHuang Ying * cache lines hot. 5924c6ddfb6cSHuang Ying */ 59251cb9dc4bSLiu Shixin static inline int process_huge_page( 5926c6ddfb6cSHuang Ying unsigned long addr_hint, unsigned int pages_per_huge_page, 59271cb9dc4bSLiu Shixin int (*process_subpage)(unsigned long addr, int idx, void *arg), 5928c6ddfb6cSHuang Ying void *arg) 5929c6ddfb6cSHuang Ying { 59301cb9dc4bSLiu Shixin int i, n, base, l, ret; 5931c6ddfb6cSHuang Ying unsigned long addr = addr_hint & 5932c6ddfb6cSHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 5933c6ddfb6cSHuang Ying 5934c6ddfb6cSHuang Ying /* Process target subpage last to keep its cache lines hot */ 5935c6ddfb6cSHuang Ying might_sleep(); 5936c6ddfb6cSHuang Ying n = (addr_hint - addr) / PAGE_SIZE; 5937c6ddfb6cSHuang Ying if (2 * n <= pages_per_huge_page) { 5938c6ddfb6cSHuang Ying /* If target subpage in first half of huge page */ 5939c6ddfb6cSHuang Ying base = 0; 5940c6ddfb6cSHuang Ying l = n; 5941c6ddfb6cSHuang Ying /* Process subpages at the end of huge page */ 5942c6ddfb6cSHuang Ying for (i = pages_per_huge_page - 1; i >= 2 * n; i--) { 5943c6ddfb6cSHuang Ying cond_resched(); 59441cb9dc4bSLiu Shixin ret = process_subpage(addr + i * PAGE_SIZE, i, arg); 59451cb9dc4bSLiu Shixin if (ret) 59461cb9dc4bSLiu Shixin return ret; 5947c6ddfb6cSHuang Ying } 5948c6ddfb6cSHuang Ying } else { 5949c6ddfb6cSHuang Ying /* If target subpage in second half of huge page */ 5950c6ddfb6cSHuang Ying base = pages_per_huge_page - 2 * (pages_per_huge_page - n); 5951c6ddfb6cSHuang Ying l = pages_per_huge_page - n; 5952c6ddfb6cSHuang Ying /* Process subpages at the begin of huge page */ 5953c6ddfb6cSHuang Ying for (i = 0; i < base; i++) { 5954c6ddfb6cSHuang Ying cond_resched(); 59551cb9dc4bSLiu Shixin ret = process_subpage(addr + i * PAGE_SIZE, i, arg); 59561cb9dc4bSLiu Shixin if (ret) 59571cb9dc4bSLiu Shixin return ret; 5958c6ddfb6cSHuang Ying } 5959c6ddfb6cSHuang Ying } 5960c6ddfb6cSHuang Ying /* 5961c6ddfb6cSHuang Ying * Process remaining subpages in left-right-left-right pattern 5962c6ddfb6cSHuang Ying * towards the target subpage 5963c6ddfb6cSHuang Ying */ 5964c6ddfb6cSHuang Ying for (i = 0; i < l; i++) { 5965c6ddfb6cSHuang Ying int left_idx = base + i; 5966c6ddfb6cSHuang Ying int right_idx = base + 2 * l - 1 - i; 5967c6ddfb6cSHuang Ying 5968c6ddfb6cSHuang Ying cond_resched(); 59691cb9dc4bSLiu Shixin ret = process_subpage(addr + left_idx * PAGE_SIZE, left_idx, arg); 59701cb9dc4bSLiu Shixin if (ret) 59711cb9dc4bSLiu Shixin return ret; 5972c6ddfb6cSHuang Ying cond_resched(); 59731cb9dc4bSLiu Shixin ret = process_subpage(addr + right_idx * PAGE_SIZE, right_idx, arg); 59741cb9dc4bSLiu Shixin if (ret) 59751cb9dc4bSLiu Shixin return ret; 5976c6ddfb6cSHuang Ying } 59771cb9dc4bSLiu Shixin return 0; 5978c6ddfb6cSHuang Ying } 5979c6ddfb6cSHuang Ying 598047ad8475SAndrea Arcangeli static void clear_gigantic_page(struct page *page, 598147ad8475SAndrea Arcangeli unsigned long addr, 598247ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 598347ad8475SAndrea Arcangeli { 598447ad8475SAndrea Arcangeli int i; 598514455eabSCheng Li struct page *p; 598647ad8475SAndrea Arcangeli 598747ad8475SAndrea Arcangeli might_sleep(); 598814455eabSCheng Li for (i = 0; i < pages_per_huge_page; i++) { 598914455eabSCheng Li p = nth_page(page, i); 599047ad8475SAndrea Arcangeli cond_resched(); 599147ad8475SAndrea Arcangeli clear_user_highpage(p, addr + i * PAGE_SIZE); 599247ad8475SAndrea Arcangeli } 599347ad8475SAndrea Arcangeli } 5994c6ddfb6cSHuang Ying 59951cb9dc4bSLiu Shixin static int clear_subpage(unsigned long addr, int idx, void *arg) 5996c6ddfb6cSHuang Ying { 5997c6ddfb6cSHuang Ying struct page *page = arg; 5998c6ddfb6cSHuang Ying 5999c6ddfb6cSHuang Ying clear_user_highpage(page + idx, addr); 60001cb9dc4bSLiu Shixin return 0; 6001c6ddfb6cSHuang Ying } 6002c6ddfb6cSHuang Ying 600347ad8475SAndrea Arcangeli void clear_huge_page(struct page *page, 6004c79b57e4SHuang Ying unsigned long addr_hint, unsigned int pages_per_huge_page) 600547ad8475SAndrea Arcangeli { 6006c79b57e4SHuang Ying unsigned long addr = addr_hint & 6007c79b57e4SHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 600847ad8475SAndrea Arcangeli 600947ad8475SAndrea Arcangeli if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { 601047ad8475SAndrea Arcangeli clear_gigantic_page(page, addr, pages_per_huge_page); 601147ad8475SAndrea Arcangeli return; 601247ad8475SAndrea Arcangeli } 601347ad8475SAndrea Arcangeli 6014c6ddfb6cSHuang Ying process_huge_page(addr_hint, pages_per_huge_page, clear_subpage, page); 601547ad8475SAndrea Arcangeli } 601647ad8475SAndrea Arcangeli 60171cb9dc4bSLiu Shixin static int copy_user_gigantic_page(struct folio *dst, struct folio *src, 601847ad8475SAndrea Arcangeli unsigned long addr, 601947ad8475SAndrea Arcangeli struct vm_area_struct *vma, 602047ad8475SAndrea Arcangeli unsigned int pages_per_huge_page) 602147ad8475SAndrea Arcangeli { 602247ad8475SAndrea Arcangeli int i; 6023c0e8150eSZhangPeng struct page *dst_page; 6024c0e8150eSZhangPeng struct page *src_page; 602547ad8475SAndrea Arcangeli 602614455eabSCheng Li for (i = 0; i < pages_per_huge_page; i++) { 6027c0e8150eSZhangPeng dst_page = folio_page(dst, i); 6028c0e8150eSZhangPeng src_page = folio_page(src, i); 602914455eabSCheng Li 603047ad8475SAndrea Arcangeli cond_resched(); 60311cb9dc4bSLiu Shixin if (copy_mc_user_highpage(dst_page, src_page, 60321cb9dc4bSLiu Shixin addr + i*PAGE_SIZE, vma)) { 60331cb9dc4bSLiu Shixin memory_failure_queue(page_to_pfn(src_page), 0); 60341cb9dc4bSLiu Shixin return -EHWPOISON; 603547ad8475SAndrea Arcangeli } 603647ad8475SAndrea Arcangeli } 60371cb9dc4bSLiu Shixin return 0; 60381cb9dc4bSLiu Shixin } 603947ad8475SAndrea Arcangeli 6040c9f4cd71SHuang Ying struct copy_subpage_arg { 6041c9f4cd71SHuang Ying struct page *dst; 6042c9f4cd71SHuang Ying struct page *src; 6043c9f4cd71SHuang Ying struct vm_area_struct *vma; 6044c9f4cd71SHuang Ying }; 6045c9f4cd71SHuang Ying 60461cb9dc4bSLiu Shixin static int copy_subpage(unsigned long addr, int idx, void *arg) 6047c9f4cd71SHuang Ying { 6048c9f4cd71SHuang Ying struct copy_subpage_arg *copy_arg = arg; 6049c9f4cd71SHuang Ying 60501cb9dc4bSLiu Shixin if (copy_mc_user_highpage(copy_arg->dst + idx, copy_arg->src + idx, 60511cb9dc4bSLiu Shixin addr, copy_arg->vma)) { 60521cb9dc4bSLiu Shixin memory_failure_queue(page_to_pfn(copy_arg->src + idx), 0); 60531cb9dc4bSLiu Shixin return -EHWPOISON; 60541cb9dc4bSLiu Shixin } 60551cb9dc4bSLiu Shixin return 0; 6056c9f4cd71SHuang Ying } 6057c9f4cd71SHuang Ying 60581cb9dc4bSLiu Shixin int copy_user_large_folio(struct folio *dst, struct folio *src, 6059c0e8150eSZhangPeng unsigned long addr_hint, struct vm_area_struct *vma) 606047ad8475SAndrea Arcangeli { 6061c0e8150eSZhangPeng unsigned int pages_per_huge_page = folio_nr_pages(dst); 6062c9f4cd71SHuang Ying unsigned long addr = addr_hint & 6063c9f4cd71SHuang Ying ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); 6064c9f4cd71SHuang Ying struct copy_subpage_arg arg = { 6065c0e8150eSZhangPeng .dst = &dst->page, 6066c0e8150eSZhangPeng .src = &src->page, 6067c9f4cd71SHuang Ying .vma = vma, 6068c9f4cd71SHuang Ying }; 606947ad8475SAndrea Arcangeli 60701cb9dc4bSLiu Shixin if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) 60711cb9dc4bSLiu Shixin return copy_user_gigantic_page(dst, src, addr, vma, 607247ad8475SAndrea Arcangeli pages_per_huge_page); 607347ad8475SAndrea Arcangeli 60741cb9dc4bSLiu Shixin return process_huge_page(addr_hint, pages_per_huge_page, copy_subpage, &arg); 607547ad8475SAndrea Arcangeli } 6076fa4d75c1SMike Kravetz 6077e87340caSZhangPeng long copy_folio_from_user(struct folio *dst_folio, 6078fa4d75c1SMike Kravetz const void __user *usr_src, 6079810a56b9SMike Kravetz bool allow_pagefault) 6080fa4d75c1SMike Kravetz { 6081e87340caSZhangPeng void *kaddr; 6082fa4d75c1SMike Kravetz unsigned long i, rc = 0; 6083e87340caSZhangPeng unsigned int nr_pages = folio_nr_pages(dst_folio); 6084e87340caSZhangPeng unsigned long ret_val = nr_pages * PAGE_SIZE; 608514455eabSCheng Li struct page *subpage; 6086fa4d75c1SMike Kravetz 6087e87340caSZhangPeng for (i = 0; i < nr_pages; i++) { 6088e87340caSZhangPeng subpage = folio_page(dst_folio, i); 6089e87340caSZhangPeng kaddr = kmap_local_page(subpage); 60900d508c1fSZhangPeng if (!allow_pagefault) 60910d508c1fSZhangPeng pagefault_disable(); 6092e87340caSZhangPeng rc = copy_from_user(kaddr, usr_src + i * PAGE_SIZE, PAGE_SIZE); 60930d508c1fSZhangPeng if (!allow_pagefault) 60940d508c1fSZhangPeng pagefault_enable(); 6095e87340caSZhangPeng kunmap_local(kaddr); 6096fa4d75c1SMike Kravetz 6097fa4d75c1SMike Kravetz ret_val -= (PAGE_SIZE - rc); 6098fa4d75c1SMike Kravetz if (rc) 6099fa4d75c1SMike Kravetz break; 6100fa4d75c1SMike Kravetz 6101e763243cSMuchun Song flush_dcache_page(subpage); 6102e763243cSMuchun Song 6103fa4d75c1SMike Kravetz cond_resched(); 6104fa4d75c1SMike Kravetz } 6105fa4d75c1SMike Kravetz return ret_val; 6106fa4d75c1SMike Kravetz } 610747ad8475SAndrea Arcangeli #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */ 610849076ec2SKirill A. Shutemov 610940b64acdSOlof Johansson #if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS 6110b35f1819SKirill A. Shutemov 6111b35f1819SKirill A. Shutemov static struct kmem_cache *page_ptl_cachep; 6112b35f1819SKirill A. Shutemov 6113b35f1819SKirill A. Shutemov void __init ptlock_cache_init(void) 6114b35f1819SKirill A. Shutemov { 6115b35f1819SKirill A. Shutemov page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0, 6116b35f1819SKirill A. Shutemov SLAB_PANIC, NULL); 6117b35f1819SKirill A. Shutemov } 6118b35f1819SKirill A. Shutemov 6119f5ecca06SVishal Moola (Oracle) bool ptlock_alloc(struct ptdesc *ptdesc) 612049076ec2SKirill A. Shutemov { 612149076ec2SKirill A. Shutemov spinlock_t *ptl; 612249076ec2SKirill A. Shutemov 6123b35f1819SKirill A. Shutemov ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL); 612449076ec2SKirill A. Shutemov if (!ptl) 612549076ec2SKirill A. Shutemov return false; 6126f5ecca06SVishal Moola (Oracle) ptdesc->ptl = ptl; 612749076ec2SKirill A. Shutemov return true; 612849076ec2SKirill A. Shutemov } 612949076ec2SKirill A. Shutemov 61306ed1b8a0SVishal Moola (Oracle) void ptlock_free(struct ptdesc *ptdesc) 613149076ec2SKirill A. Shutemov { 61326ed1b8a0SVishal Moola (Oracle) kmem_cache_free(page_ptl_cachep, ptdesc->ptl); 613349076ec2SKirill A. Shutemov } 613449076ec2SKirill A. Shutemov #endif 6135