160727d8bSWarner Losh /*- 2796df753SPedro F. Giffuni * SPDX-License-Identifier: (BSD-4-Clause AND MIT-CMU) 3df57947fSPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1991, 1993 5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 626f9a767SRodney W. Grimes * Copyright (c) 1994 John S. Dyson 726f9a767SRodney W. Grimes * All rights reserved. 826f9a767SRodney W. Grimes * Copyright (c) 1994 David Greenman 926f9a767SRodney W. Grimes * All rights reserved. 1026f9a767SRodney W. Grimes * 11df8bae1dSRodney W. Grimes * 12df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 13df8bae1dSRodney W. Grimes * The Mach Operating System project at Carnegie-Mellon University. 14df8bae1dSRodney W. Grimes * 15df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 16df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 17df8bae1dSRodney W. Grimes * are met: 18df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 19df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 20df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 21df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 22df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 23df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 245929bcfaSPhilippe Charnier * must display the following acknowledgement: 25df8bae1dSRodney W. Grimes * This product includes software developed by the University of 26df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 27df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 28df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 29df8bae1dSRodney W. Grimes * without specific prior written permission. 30df8bae1dSRodney W. Grimes * 31df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 32df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 33df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 34df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 35df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 36df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 37df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 38df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 39df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 40df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 41df8bae1dSRodney W. Grimes * SUCH DAMAGE. 42df8bae1dSRodney W. Grimes * 433c4dd356SDavid Greenman * from: @(#)vm_fault.c 8.4 (Berkeley) 1/12/94 44df8bae1dSRodney W. Grimes * 45df8bae1dSRodney W. Grimes * 46df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 47df8bae1dSRodney W. Grimes * All rights reserved. 48df8bae1dSRodney W. Grimes * 49df8bae1dSRodney W. Grimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 50df8bae1dSRodney W. Grimes * 51df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and 52df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright 53df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the 54df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions 55df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 56df8bae1dSRodney W. Grimes * 57df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 58df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 59df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 60df8bae1dSRodney W. Grimes * 61df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to 62df8bae1dSRodney W. Grimes * 63df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 64df8bae1dSRodney W. Grimes * School of Computer Science 65df8bae1dSRodney W. Grimes * Carnegie Mellon University 66df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890 67df8bae1dSRodney W. Grimes * 68df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 69df8bae1dSRodney W. Grimes * rights to redistribute these changes. 70df8bae1dSRodney W. Grimes */ 71df8bae1dSRodney W. Grimes 72df8bae1dSRodney W. Grimes /* 73df8bae1dSRodney W. Grimes * Page fault handling module. 74df8bae1dSRodney W. Grimes */ 75874651b1SDavid E. O'Brien 76874651b1SDavid E. O'Brien #include <sys/cdefs.h> 77874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 78874651b1SDavid E. O'Brien 7935818d2eSJohn Baldwin #include "opt_ktrace.h" 80f8a47341SAlan Cox #include "opt_vm.h" 81f8a47341SAlan Cox 82df8bae1dSRodney W. Grimes #include <sys/param.h> 83df8bae1dSRodney W. Grimes #include <sys/systm.h> 844edf4a58SJohn Baldwin #include <sys/kernel.h> 85fb919e4dSMark Murray #include <sys/lock.h> 86a8b0f100SAlan Cox #include <sys/mman.h> 87eeacb3b0SMark Johnston #include <sys/mutex.h> 8826f9a767SRodney W. Grimes #include <sys/proc.h> 89ae34b6ffSEdward Tomasz Napierala #include <sys/racct.h> 9011b57401SHans Petter Selasky #include <sys/refcount.h> 9126f9a767SRodney W. Grimes #include <sys/resourcevar.h> 9289f6b863SAttilio Rao #include <sys/rwlock.h> 93df08823dSKonstantin Belousov #include <sys/signalvar.h> 9423955314SAlfred Perlstein #include <sys/sysctl.h> 95df08823dSKonstantin Belousov #include <sys/sysent.h> 964edf4a58SJohn Baldwin #include <sys/vmmeter.h> 974edf4a58SJohn Baldwin #include <sys/vnode.h> 9835818d2eSJohn Baldwin #ifdef KTRACE 9935818d2eSJohn Baldwin #include <sys/ktrace.h> 10035818d2eSJohn Baldwin #endif 101df8bae1dSRodney W. Grimes 102df8bae1dSRodney W. Grimes #include <vm/vm.h> 103efeaf95aSDavid Greenman #include <vm/vm_param.h> 104efeaf95aSDavid Greenman #include <vm/pmap.h> 105efeaf95aSDavid Greenman #include <vm/vm_map.h> 106efeaf95aSDavid Greenman #include <vm/vm_object.h> 107df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 108df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h> 109a83c285cSDavid Greenman #include <vm/vm_kern.h> 11024a1cce3SDavid Greenman #include <vm/vm_pager.h> 111efeaf95aSDavid Greenman #include <vm/vm_extern.h> 112dfdf9abdSAlan Cox #include <vm/vm_reserv.h> 113df8bae1dSRodney W. Grimes 114566526a9SAlan Cox #define PFBAK 4 115566526a9SAlan Cox #define PFFOR 4 116566526a9SAlan Cox 1175268042bSAlan Cox #define VM_FAULT_READ_DEFAULT (1 + VM_FAULT_READ_AHEAD_INIT) 11813458803SAlan Cox #define VM_FAULT_READ_MAX (1 + VM_FAULT_READ_AHEAD_MAX) 119a8b0f100SAlan Cox 120a8b0f100SAlan Cox #define VM_FAULT_DONTNEED_MIN 1048576 12126f9a767SRodney W. Grimes 1224866e085SJohn Dyson struct faultstate { 1232c2f4413SJeff Roberson /* Fault parameters. */ 1245949b1caSJeff Roberson vm_offset_t vaddr; 1252c2f4413SJeff Roberson vm_page_t *m_hold; 1262c2f4413SJeff Roberson vm_prot_t fault_type; 1272c2f4413SJeff Roberson vm_prot_t prot; 1282c2f4413SJeff Roberson int fault_flags; 1292c2f4413SJeff Roberson boolean_t wired; 1302c2f4413SJeff Roberson 1312c2f4413SJeff Roberson /* Page reference for cow. */ 13258447749SJeff Roberson vm_page_t m_cow; 1332c2f4413SJeff Roberson 1342c2f4413SJeff Roberson /* Current object. */ 1354866e085SJohn Dyson vm_object_t object; 1364866e085SJohn Dyson vm_pindex_t pindex; 1372c2f4413SJeff Roberson vm_page_t m; 1382c2f4413SJeff Roberson 1392c2f4413SJeff Roberson /* Top-level map object. */ 1404866e085SJohn Dyson vm_object_t first_object; 1414866e085SJohn Dyson vm_pindex_t first_pindex; 1422c2f4413SJeff Roberson vm_page_t first_m; 1432c2f4413SJeff Roberson 1442c2f4413SJeff Roberson /* Map state. */ 1454866e085SJohn Dyson vm_map_t map; 1464866e085SJohn Dyson vm_map_entry_t entry; 147dc5401d2SKonstantin Belousov int map_generation; 148cd8a6fe8SAlan Cox bool lookup_still_valid; 1492c2f4413SJeff Roberson 1502c2f4413SJeff Roberson /* Vnode if locked. */ 1514866e085SJohn Dyson struct vnode *vp; 1524866e085SJohn Dyson }; 1534866e085SJohn Dyson 154a8b0f100SAlan Cox static void vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr, 155a8b0f100SAlan Cox int ahead); 15663281952SAlan Cox static void vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra, 157a7163bb9SKonstantin Belousov int backward, int forward, bool obj_locked); 15813458803SAlan Cox 159245139c6SKonstantin Belousov static int vm_pfault_oom_attempts = 3; 160245139c6SKonstantin Belousov SYSCTL_INT(_vm, OID_AUTO, pfault_oom_attempts, CTLFLAG_RWTUN, 161245139c6SKonstantin Belousov &vm_pfault_oom_attempts, 0, 162245139c6SKonstantin Belousov "Number of page allocation attempts in page fault handler before it " 163245139c6SKonstantin Belousov "triggers OOM handling"); 164245139c6SKonstantin Belousov 165245139c6SKonstantin Belousov static int vm_pfault_oom_wait = 10; 166245139c6SKonstantin Belousov SYSCTL_INT(_vm, OID_AUTO, pfault_oom_wait, CTLFLAG_RWTUN, 167245139c6SKonstantin Belousov &vm_pfault_oom_wait, 0, 168245139c6SKonstantin Belousov "Number of seconds to wait for free pages before retrying " 169245139c6SKonstantin Belousov "the page fault handler"); 170245139c6SKonstantin Belousov 17162a59e8fSWarner Losh static inline void 1724bf95d00SJeff Roberson fault_page_release(vm_page_t *mp) 1734866e085SJohn Dyson { 1744bf95d00SJeff Roberson vm_page_t m; 1750d0be82aSKonstantin Belousov 1764bf95d00SJeff Roberson m = *mp; 1774bf95d00SJeff Roberson if (m != NULL) { 178be801aaaSMark Johnston /* 1794bf95d00SJeff Roberson * We are likely to loop around again and attempt to busy 1804bf95d00SJeff Roberson * this page. Deactivating it leaves it available for 1814bf95d00SJeff Roberson * pageout while optimizing fault restarts. 182be801aaaSMark Johnston */ 1834bf95d00SJeff Roberson vm_page_deactivate(m); 1844bf95d00SJeff Roberson vm_page_xunbusy(m); 1854bf95d00SJeff Roberson *mp = NULL; 1864bf95d00SJeff Roberson } 1874bf95d00SJeff Roberson } 1884bf95d00SJeff Roberson 1894bf95d00SJeff Roberson static inline void 1904bf95d00SJeff Roberson fault_page_free(vm_page_t *mp) 1914bf95d00SJeff Roberson { 1924bf95d00SJeff Roberson vm_page_t m; 1934bf95d00SJeff Roberson 1944bf95d00SJeff Roberson m = *mp; 1954bf95d00SJeff Roberson if (m != NULL) { 1964bf95d00SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(m->object); 1974bf95d00SJeff Roberson if (!vm_page_wired(m)) 1984bf95d00SJeff Roberson vm_page_free(m); 199419f0b1fSJeff Roberson else 200419f0b1fSJeff Roberson vm_page_xunbusy(m); 2014bf95d00SJeff Roberson *mp = NULL; 2024866e085SJohn Dyson } 203be2c5610SMark Johnston } 2044866e085SJohn Dyson 20562a59e8fSWarner Losh static inline void 2064866e085SJohn Dyson unlock_map(struct faultstate *fs) 2074866e085SJohn Dyson { 2080d0be82aSKonstantin Belousov 20925adb370SBrian Feldman if (fs->lookup_still_valid) { 2104866e085SJohn Dyson vm_map_lookup_done(fs->map, fs->entry); 211cd8a6fe8SAlan Cox fs->lookup_still_valid = false; 2124866e085SJohn Dyson } 2134866e085SJohn Dyson } 2144866e085SJohn Dyson 2154866e085SJohn Dyson static void 216cfabea3dSKonstantin Belousov unlock_vp(struct faultstate *fs) 217cfabea3dSKonstantin Belousov { 218cfabea3dSKonstantin Belousov 219cfabea3dSKonstantin Belousov if (fs->vp != NULL) { 220cfabea3dSKonstantin Belousov vput(fs->vp); 221cfabea3dSKonstantin Belousov fs->vp = NULL; 222cfabea3dSKonstantin Belousov } 223cfabea3dSKonstantin Belousov } 224cfabea3dSKonstantin Belousov 225cfabea3dSKonstantin Belousov static void 2264b3e0665SJeff Roberson fault_deallocate(struct faultstate *fs) 2274866e085SJohn Dyson { 228f29ba63eSAlan Cox 22958447749SJeff Roberson fault_page_release(&fs->m_cow); 2304bf95d00SJeff Roberson fault_page_release(&fs->m); 2314866e085SJohn Dyson vm_object_pip_wakeup(fs->object); 2324866e085SJohn Dyson if (fs->object != fs->first_object) { 23389f6b863SAttilio Rao VM_OBJECT_WLOCK(fs->first_object); 2344bf95d00SJeff Roberson fault_page_free(&fs->first_m); 23589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(fs->first_object); 2364bf95d00SJeff Roberson vm_object_pip_wakeup(fs->first_object); 2374866e085SJohn Dyson } 2384866e085SJohn Dyson vm_object_deallocate(fs->first_object); 2394866e085SJohn Dyson unlock_map(fs); 240cfabea3dSKonstantin Belousov unlock_vp(fs); 2414866e085SJohn Dyson } 2424866e085SJohn Dyson 243a36f5532SKonstantin Belousov static void 2444b3e0665SJeff Roberson unlock_and_deallocate(struct faultstate *fs) 2454b3e0665SJeff Roberson { 2464b3e0665SJeff Roberson 2474b3e0665SJeff Roberson VM_OBJECT_WUNLOCK(fs->object); 2484b3e0665SJeff Roberson fault_deallocate(fs); 2494b3e0665SJeff Roberson } 2504b3e0665SJeff Roberson 2514b3e0665SJeff Roberson static void 2522c2f4413SJeff Roberson vm_fault_dirty(struct faultstate *fs, vm_page_t m) 253a36f5532SKonstantin Belousov { 254e26236e9SKonstantin Belousov bool need_dirty; 255a36f5532SKonstantin Belousov 2562c2f4413SJeff Roberson if (((fs->prot & VM_PROT_WRITE) == 0 && 2572c2f4413SJeff Roberson (fs->fault_flags & VM_FAULT_DIRTY) == 0) || 258a36f5532SKonstantin Belousov (m->oflags & VPO_UNMANAGED) != 0) 259a36f5532SKonstantin Belousov return; 260a36f5532SKonstantin Belousov 2610012f373SJeff Roberson VM_PAGE_OBJECT_BUSY_ASSERT(m); 262a36f5532SKonstantin Belousov 2632c2f4413SJeff Roberson need_dirty = ((fs->fault_type & VM_PROT_WRITE) != 0 && 2642c2f4413SJeff Roberson (fs->fault_flags & VM_FAULT_WIRE) == 0) || 2652c2f4413SJeff Roberson (fs->fault_flags & VM_FAULT_DIRTY) != 0; 266a36f5532SKonstantin Belousov 267a36f5532SKonstantin Belousov vm_object_set_writeable_dirty(m->object); 26867d0e293SJeff Roberson 269a36f5532SKonstantin Belousov /* 270a8081778SJeff Roberson * If the fault is a write, we know that this page is being 271a8081778SJeff Roberson * written NOW so dirty it explicitly to save on 272a8081778SJeff Roberson * pmap_is_modified() calls later. 273a8081778SJeff Roberson * 274a8081778SJeff Roberson * Also, since the page is now dirty, we can possibly tell 275a8081778SJeff Roberson * the pager to release any swap backing the page. 276a36f5532SKonstantin Belousov */ 277a8081778SJeff Roberson if (need_dirty && vm_page_set_dirty(m) == 0) { 278a36f5532SKonstantin Belousov /* 279fff5403fSJeff Roberson * If this is a NOSYNC mmap we do not want to set PGA_NOSYNC 280a36f5532SKonstantin Belousov * if the page is already dirty to prevent data written with 281a36f5532SKonstantin Belousov * the expectation of being synced from not being synced. 282a36f5532SKonstantin Belousov * Likewise if this entry does not request NOSYNC then make 283a36f5532SKonstantin Belousov * sure the page isn't marked NOSYNC. Applications sharing 284a36f5532SKonstantin Belousov * data should use the same flags to avoid ping ponging. 285a36f5532SKonstantin Belousov */ 2862c2f4413SJeff Roberson if ((fs->entry->eflags & MAP_ENTRY_NOSYNC) != 0) 287fff5403fSJeff Roberson vm_page_aflag_set(m, PGA_NOSYNC); 288a8081778SJeff Roberson else 289fff5403fSJeff Roberson vm_page_aflag_clear(m, PGA_NOSYNC); 290a36f5532SKonstantin Belousov } 291a36f5532SKonstantin Belousov 292a36f5532SKonstantin Belousov } 293a36f5532SKonstantin Belousov 29441ddec83SKonstantin Belousov /* 29541ddec83SKonstantin Belousov * Unlocks fs.first_object and fs.map on success. 29641ddec83SKonstantin Belousov */ 29741ddec83SKonstantin Belousov static int 2982c2f4413SJeff Roberson vm_fault_soft_fast(struct faultstate *fs) 29941ddec83SKonstantin Belousov { 3008b5e1472SAlan Cox vm_page_t m, m_map; 3012bf8cb38SAlan Cox #if (defined(__aarch64__) || defined(__amd64__) || (defined(__arm__) && \ 302f6893f09SMark Johnston __ARM_ARCH >= 6) || defined(__i386__) || defined(__riscv)) && \ 303f6893f09SMark Johnston VM_NRESERVLEVEL > 0 3048b5e1472SAlan Cox vm_page_t m_super; 30590ea34bfSAlan Cox int flags; 3068b5e1472SAlan Cox #endif 30790ea34bfSAlan Cox int psind, rv; 3082c2f4413SJeff Roberson vm_offset_t vaddr; 30941ddec83SKonstantin Belousov 31041ddec83SKonstantin Belousov MPASS(fs->vp == NULL); 3112c2f4413SJeff Roberson vaddr = fs->vaddr; 312205be21dSJeff Roberson vm_object_busy(fs->first_object); 31341ddec83SKonstantin Belousov m = vm_page_lookup(fs->first_object, fs->first_pindex); 31441ddec83SKonstantin Belousov /* A busy page can be mapped for read|execute access. */ 3152c2f4413SJeff Roberson if (m == NULL || ((fs->prot & VM_PROT_WRITE) != 0 && 3160012f373SJeff Roberson vm_page_busied(m)) || !vm_page_all_valid(m)) { 317205be21dSJeff Roberson rv = KERN_FAILURE; 318205be21dSJeff Roberson goto out; 319205be21dSJeff Roberson } 3208b5e1472SAlan Cox m_map = m; 3218b5e1472SAlan Cox psind = 0; 3222bf8cb38SAlan Cox #if (defined(__aarch64__) || defined(__amd64__) || (defined(__arm__) && \ 323f6893f09SMark Johnston __ARM_ARCH >= 6) || defined(__i386__) || defined(__riscv)) && \ 324f6893f09SMark Johnston VM_NRESERVLEVEL > 0 3258b5e1472SAlan Cox if ((m->flags & PG_FICTITIOUS) == 0 && 3268b5e1472SAlan Cox (m_super = vm_reserv_to_superpage(m)) != NULL && 3278b5e1472SAlan Cox rounddown2(vaddr, pagesizes[m_super->psind]) >= fs->entry->start && 3288b5e1472SAlan Cox roundup2(vaddr + 1, pagesizes[m_super->psind]) <= fs->entry->end && 3298b5e1472SAlan Cox (vaddr & (pagesizes[m_super->psind] - 1)) == (VM_PAGE_TO_PHYS(m) & 3302c2f4413SJeff Roberson (pagesizes[m_super->psind] - 1)) && !fs->wired && 3318b5e1472SAlan Cox pmap_ps_enabled(fs->map->pmap)) { 3328b5e1472SAlan Cox flags = PS_ALL_VALID; 3332c2f4413SJeff Roberson if ((fs->prot & VM_PROT_WRITE) != 0) { 3348b5e1472SAlan Cox /* 3358b5e1472SAlan Cox * Create a superpage mapping allowing write access 3368b5e1472SAlan Cox * only if none of the constituent pages are busy and 3378b5e1472SAlan Cox * all of them are already dirty (except possibly for 3388b5e1472SAlan Cox * the page that was faulted on). 3398b5e1472SAlan Cox */ 3408b5e1472SAlan Cox flags |= PS_NONE_BUSY; 3418b5e1472SAlan Cox if ((fs->first_object->flags & OBJ_UNMANAGED) == 0) 3428b5e1472SAlan Cox flags |= PS_ALL_DIRTY; 3438b5e1472SAlan Cox } 3448b5e1472SAlan Cox if (vm_page_ps_test(m_super, flags, m)) { 3458b5e1472SAlan Cox m_map = m_super; 3468b5e1472SAlan Cox psind = m_super->psind; 3478b5e1472SAlan Cox vaddr = rounddown2(vaddr, pagesizes[psind]); 3488b5e1472SAlan Cox /* Preset the modified bit for dirty superpages. */ 3498b5e1472SAlan Cox if ((flags & PS_ALL_DIRTY) != 0) 3502c2f4413SJeff Roberson fs->fault_type |= VM_PROT_WRITE; 3518b5e1472SAlan Cox } 3528b5e1472SAlan Cox } 3538b5e1472SAlan Cox #endif 3542c2f4413SJeff Roberson rv = pmap_enter(fs->map->pmap, vaddr, m_map, fs->prot, fs->fault_type | 3552c2f4413SJeff Roberson PMAP_ENTER_NOSLEEP | (fs->wired ? PMAP_ENTER_WIRED : 0), psind); 35641ddec83SKonstantin Belousov if (rv != KERN_SUCCESS) 357205be21dSJeff Roberson goto out; 3582c2f4413SJeff Roberson if (fs->m_hold != NULL) { 3592c2f4413SJeff Roberson (*fs->m_hold) = m; 360fee2a2faSMark Johnston vm_page_wire(m); 361fee2a2faSMark Johnston } 3622c2f4413SJeff Roberson if (psind == 0 && !fs->wired) 363a7163bb9SKonstantin Belousov vm_fault_prefault(fs, vaddr, PFBAK, PFFOR, true); 364a7163bb9SKonstantin Belousov VM_OBJECT_RUNLOCK(fs->first_object); 3652c2f4413SJeff Roberson vm_fault_dirty(fs, m); 36641ddec83SKonstantin Belousov vm_map_lookup_done(fs->map, fs->entry); 36741ddec83SKonstantin Belousov curthread->td_ru.ru_minflt++; 368205be21dSJeff Roberson 369205be21dSJeff Roberson out: 370205be21dSJeff Roberson vm_object_unbusy(fs->first_object); 371205be21dSJeff Roberson return (rv); 37241ddec83SKonstantin Belousov } 37341ddec83SKonstantin Belousov 374c42b43a0SKonstantin Belousov static void 375c42b43a0SKonstantin Belousov vm_fault_restore_map_lock(struct faultstate *fs) 376c42b43a0SKonstantin Belousov { 377c42b43a0SKonstantin Belousov 378c42b43a0SKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED(fs->first_object); 37911b57401SHans Petter Selasky MPASS(REFCOUNT_COUNT(fs->first_object->paging_in_progress) > 0); 380c42b43a0SKonstantin Belousov 381c42b43a0SKonstantin Belousov if (!vm_map_trylock_read(fs->map)) { 382c42b43a0SKonstantin Belousov VM_OBJECT_WUNLOCK(fs->first_object); 383c42b43a0SKonstantin Belousov vm_map_lock_read(fs->map); 384c42b43a0SKonstantin Belousov VM_OBJECT_WLOCK(fs->first_object); 385c42b43a0SKonstantin Belousov } 386c42b43a0SKonstantin Belousov fs->lookup_still_valid = true; 387c42b43a0SKonstantin Belousov } 388c42b43a0SKonstantin Belousov 3897a432b84SKonstantin Belousov static void 3907a432b84SKonstantin Belousov vm_fault_populate_check_page(vm_page_t m) 3917a432b84SKonstantin Belousov { 3927a432b84SKonstantin Belousov 3937a432b84SKonstantin Belousov /* 3947a432b84SKonstantin Belousov * Check each page to ensure that the pager is obeying the 3957a432b84SKonstantin Belousov * interface: the page must be installed in the object, fully 3967a432b84SKonstantin Belousov * valid, and exclusively busied. 3977a432b84SKonstantin Belousov */ 3987a432b84SKonstantin Belousov MPASS(m != NULL); 3990012f373SJeff Roberson MPASS(vm_page_all_valid(m)); 4007a432b84SKonstantin Belousov MPASS(vm_page_xbusied(m)); 4017a432b84SKonstantin Belousov } 4027a432b84SKonstantin Belousov 4037a432b84SKonstantin Belousov static void 4047a432b84SKonstantin Belousov vm_fault_populate_cleanup(vm_object_t object, vm_pindex_t first, 4057a432b84SKonstantin Belousov vm_pindex_t last) 4067a432b84SKonstantin Belousov { 4077a432b84SKonstantin Belousov vm_page_t m; 4087a432b84SKonstantin Belousov vm_pindex_t pidx; 4097a432b84SKonstantin Belousov 4107a432b84SKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED(object); 4117a432b84SKonstantin Belousov MPASS(first <= last); 4127a432b84SKonstantin Belousov for (pidx = first, m = vm_page_lookup(object, pidx); 4137a432b84SKonstantin Belousov pidx <= last; pidx++, m = vm_page_next(m)) { 4147a432b84SKonstantin Belousov vm_fault_populate_check_page(m); 4157a432b84SKonstantin Belousov vm_page_deactivate(m); 4167a432b84SKonstantin Belousov vm_page_xunbusy(m); 4177a432b84SKonstantin Belousov } 4187a432b84SKonstantin Belousov } 419c42b43a0SKonstantin Belousov 420c42b43a0SKonstantin Belousov static int 4212c2f4413SJeff Roberson vm_fault_populate(struct faultstate *fs) 422c42b43a0SKonstantin Belousov { 42370183daaSAlan Cox vm_offset_t vaddr; 424c42b43a0SKonstantin Belousov vm_page_t m; 4257a432b84SKonstantin Belousov vm_pindex_t map_first, map_last, pager_first, pager_last, pidx; 42670183daaSAlan Cox int i, npages, psind, rv; 427c42b43a0SKonstantin Belousov 428c42b43a0SKonstantin Belousov MPASS(fs->object == fs->first_object); 429c42b43a0SKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED(fs->first_object); 43011b57401SHans Petter Selasky MPASS(REFCOUNT_COUNT(fs->first_object->paging_in_progress) > 0); 431c42b43a0SKonstantin Belousov MPASS(fs->first_object->backing_object == NULL); 432c42b43a0SKonstantin Belousov MPASS(fs->lookup_still_valid); 433c42b43a0SKonstantin Belousov 4347a432b84SKonstantin Belousov pager_first = OFF_TO_IDX(fs->entry->offset); 43589564188SAlan Cox pager_last = pager_first + atop(fs->entry->end - fs->entry->start) - 1; 436c42b43a0SKonstantin Belousov unlock_map(fs); 437c42b43a0SKonstantin Belousov unlock_vp(fs); 438c42b43a0SKonstantin Belousov 439c42b43a0SKonstantin Belousov /* 440c42b43a0SKonstantin Belousov * Call the pager (driver) populate() method. 441c42b43a0SKonstantin Belousov * 442c42b43a0SKonstantin Belousov * There is no guarantee that the method will be called again 443c42b43a0SKonstantin Belousov * if the current fault is for read, and a future fault is 444c42b43a0SKonstantin Belousov * for write. Report the entry's maximum allowed protection 445c42b43a0SKonstantin Belousov * to the driver. 446c42b43a0SKonstantin Belousov */ 447c42b43a0SKonstantin Belousov rv = vm_pager_populate(fs->first_object, fs->first_pindex, 4482c2f4413SJeff Roberson fs->fault_type, fs->entry->max_protection, &pager_first, &pager_last); 449c42b43a0SKonstantin Belousov 450c42b43a0SKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED(fs->first_object); 451c42b43a0SKonstantin Belousov if (rv == VM_PAGER_BAD) { 452c42b43a0SKonstantin Belousov /* 453c42b43a0SKonstantin Belousov * VM_PAGER_BAD is the backdoor for a pager to request 454c42b43a0SKonstantin Belousov * normal fault handling. 455c42b43a0SKonstantin Belousov */ 456c42b43a0SKonstantin Belousov vm_fault_restore_map_lock(fs); 457c42b43a0SKonstantin Belousov if (fs->map->timestamp != fs->map_generation) 458c42b43a0SKonstantin Belousov return (KERN_RESOURCE_SHORTAGE); /* RetryFault */ 459c42b43a0SKonstantin Belousov return (KERN_NOT_RECEIVER); 460c42b43a0SKonstantin Belousov } 461c42b43a0SKonstantin Belousov if (rv != VM_PAGER_OK) 462c42b43a0SKonstantin Belousov return (KERN_FAILURE); /* AKA SIGSEGV */ 463c42b43a0SKonstantin Belousov 464c42b43a0SKonstantin Belousov /* Ensure that the driver is obeying the interface. */ 4657a432b84SKonstantin Belousov MPASS(pager_first <= pager_last); 4667a432b84SKonstantin Belousov MPASS(fs->first_pindex <= pager_last); 4677a432b84SKonstantin Belousov MPASS(fs->first_pindex >= pager_first); 4687a432b84SKonstantin Belousov MPASS(pager_last < fs->first_object->size); 469c42b43a0SKonstantin Belousov 470c42b43a0SKonstantin Belousov vm_fault_restore_map_lock(fs); 4717a432b84SKonstantin Belousov if (fs->map->timestamp != fs->map_generation) { 4727a432b84SKonstantin Belousov vm_fault_populate_cleanup(fs->first_object, pager_first, 4737a432b84SKonstantin Belousov pager_last); 474c42b43a0SKonstantin Belousov return (KERN_RESOURCE_SHORTAGE); /* RetryFault */ 4757a432b84SKonstantin Belousov } 476c42b43a0SKonstantin Belousov 477c42b43a0SKonstantin Belousov /* 4787a432b84SKonstantin Belousov * The map is unchanged after our last unlock. Process the fault. 4797a432b84SKonstantin Belousov * 4807a432b84SKonstantin Belousov * The range [pager_first, pager_last] that is given to the 4817a432b84SKonstantin Belousov * pager is only a hint. The pager may populate any range 4827a432b84SKonstantin Belousov * within the object that includes the requested page index. 4837a432b84SKonstantin Belousov * In case the pager expanded the range, clip it to fit into 4847a432b84SKonstantin Belousov * the map entry. 485c42b43a0SKonstantin Belousov */ 48689564188SAlan Cox map_first = OFF_TO_IDX(fs->entry->offset); 48789564188SAlan Cox if (map_first > pager_first) { 4887a432b84SKonstantin Belousov vm_fault_populate_cleanup(fs->first_object, pager_first, 4897a432b84SKonstantin Belousov map_first - 1); 49089564188SAlan Cox pager_first = map_first; 49189564188SAlan Cox } 49289564188SAlan Cox map_last = map_first + atop(fs->entry->end - fs->entry->start) - 1; 49389564188SAlan Cox if (map_last < pager_last) { 4947a432b84SKonstantin Belousov vm_fault_populate_cleanup(fs->first_object, map_last + 1, 4957a432b84SKonstantin Belousov pager_last); 49689564188SAlan Cox pager_last = map_last; 49789564188SAlan Cox } 49889564188SAlan Cox for (pidx = pager_first, m = vm_page_lookup(fs->first_object, pidx); 49970183daaSAlan Cox pidx <= pager_last; 50070183daaSAlan Cox pidx += npages, m = vm_page_next(&m[npages - 1])) { 50170183daaSAlan Cox vaddr = fs->entry->start + IDX_TO_OFF(pidx) - fs->entry->offset; 5022bf8cb38SAlan Cox #if defined(__aarch64__) || defined(__amd64__) || (defined(__arm__) && \ 503f6893f09SMark Johnston __ARM_ARCH >= 6) || defined(__i386__) || defined(__riscv) 50470183daaSAlan Cox psind = m->psind; 50570183daaSAlan Cox if (psind > 0 && ((vaddr & (pagesizes[psind] - 1)) != 0 || 50670183daaSAlan Cox pidx + OFF_TO_IDX(pagesizes[psind]) - 1 > pager_last || 5072c2f4413SJeff Roberson !pmap_ps_enabled(fs->map->pmap) || fs->wired)) 50870183daaSAlan Cox psind = 0; 50970183daaSAlan Cox #else 51070183daaSAlan Cox psind = 0; 51170183daaSAlan Cox #endif 51270183daaSAlan Cox npages = atop(pagesizes[psind]); 51370183daaSAlan Cox for (i = 0; i < npages; i++) { 51470183daaSAlan Cox vm_fault_populate_check_page(&m[i]); 5152c2f4413SJeff Roberson vm_fault_dirty(fs, &m[i]); 51670183daaSAlan Cox } 517c42b43a0SKonstantin Belousov VM_OBJECT_WUNLOCK(fs->first_object); 5182c2f4413SJeff Roberson rv = pmap_enter(fs->map->pmap, vaddr, m, fs->prot, fs->fault_type | 5192c2f4413SJeff Roberson (fs->wired ? PMAP_ENTER_WIRED : 0), psind); 520e7a9df16SKonstantin Belousov #if defined(__amd64__) 521e7a9df16SKonstantin Belousov if (psind > 0 && rv == KERN_FAILURE) { 522e7a9df16SKonstantin Belousov for (i = 0; i < npages; i++) { 523e7a9df16SKonstantin Belousov rv = pmap_enter(fs->map->pmap, vaddr + ptoa(i), 5242c2f4413SJeff Roberson &m[i], fs->prot, fs->fault_type | 5252c2f4413SJeff Roberson (fs->wired ? PMAP_ENTER_WIRED : 0), 0); 526e7a9df16SKonstantin Belousov MPASS(rv == KERN_SUCCESS); 527e7a9df16SKonstantin Belousov } 528e7a9df16SKonstantin Belousov } 529e7a9df16SKonstantin Belousov #else 530e7a9df16SKonstantin Belousov MPASS(rv == KERN_SUCCESS); 531e7a9df16SKonstantin Belousov #endif 532c42b43a0SKonstantin Belousov VM_OBJECT_WLOCK(fs->first_object); 53370183daaSAlan Cox for (i = 0; i < npages; i++) { 5342c2f4413SJeff Roberson if ((fs->fault_flags & VM_FAULT_WIRE) != 0) 53570183daaSAlan Cox vm_page_wire(&m[i]); 5369f5632e6SMark Johnston else 53770183daaSAlan Cox vm_page_activate(&m[i]); 5382c2f4413SJeff Roberson if (fs->m_hold != NULL && m[i].pindex == fs->first_pindex) { 5392c2f4413SJeff Roberson (*fs->m_hold) = &m[i]; 540eeacb3b0SMark Johnston vm_page_wire(&m[i]); 541c42b43a0SKonstantin Belousov } 5424cdea4a8SJeff Roberson vm_page_xunbusy(&m[i]); 54370183daaSAlan Cox } 544c42b43a0SKonstantin Belousov } 545c42b43a0SKonstantin Belousov curthread->td_ru.ru_majflt++; 546c42b43a0SKonstantin Belousov return (KERN_SUCCESS); 547c42b43a0SKonstantin Belousov } 548c42b43a0SKonstantin Belousov 549df08823dSKonstantin Belousov static int prot_fault_translation; 550df08823dSKonstantin Belousov SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RWTUN, 551df08823dSKonstantin Belousov &prot_fault_translation, 0, 552df08823dSKonstantin Belousov "Control signal to deliver on protection fault"); 553df08823dSKonstantin Belousov 554df08823dSKonstantin Belousov /* compat definition to keep common code for signal translation */ 555df08823dSKonstantin Belousov #define UCODE_PAGEFLT 12 556df08823dSKonstantin Belousov #ifdef T_PAGEFLT 557df08823dSKonstantin Belousov _Static_assert(UCODE_PAGEFLT == T_PAGEFLT, "T_PAGEFLT"); 558df08823dSKonstantin Belousov #endif 559df08823dSKonstantin Belousov 560df8bae1dSRodney W. Grimes /* 561df08823dSKonstantin Belousov * vm_fault_trap: 562df8bae1dSRodney W. Grimes * 563956f3135SPhilippe Charnier * Handle a page fault occurring at the given address, 564df8bae1dSRodney W. Grimes * requiring the given permissions, in the map specified. 565df8bae1dSRodney W. Grimes * If successful, the page is inserted into the 566df8bae1dSRodney W. Grimes * associated physical map. 567df8bae1dSRodney W. Grimes * 568df8bae1dSRodney W. Grimes * NOTE: the given address should be truncated to the 569df8bae1dSRodney W. Grimes * proper page address. 570df8bae1dSRodney W. Grimes * 571df8bae1dSRodney W. Grimes * KERN_SUCCESS is returned if the page fault is handled; otherwise, 572df8bae1dSRodney W. Grimes * a standard error specifying why the fault is fatal is returned. 573df8bae1dSRodney W. Grimes * 574df8bae1dSRodney W. Grimes * The map in question must be referenced, and remains so. 5750cddd8f0SMatthew Dillon * Caller may hold no locks. 576df8bae1dSRodney W. Grimes */ 577df8bae1dSRodney W. Grimes int 578df08823dSKonstantin Belousov vm_fault_trap(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, 579df08823dSKonstantin Belousov int fault_flags, int *signo, int *ucode) 58023955314SAlfred Perlstein { 58135818d2eSJohn Baldwin int result; 582acd11c74SAlan Cox 583df08823dSKonstantin Belousov MPASS(signo == NULL || ucode != NULL); 58435818d2eSJohn Baldwin #ifdef KTRACE 585c31cec45SKonstantin Belousov if (map != kernel_map && KTRPOINT(curthread, KTR_FAULT)) 58635818d2eSJohn Baldwin ktrfault(vaddr, fault_type); 58735818d2eSJohn Baldwin #endif 588df08823dSKonstantin Belousov result = vm_fault(map, trunc_page(vaddr), fault_type, fault_flags, 589be996836SAttilio Rao NULL); 590df08823dSKonstantin Belousov KASSERT(result == KERN_SUCCESS || result == KERN_FAILURE || 591df08823dSKonstantin Belousov result == KERN_INVALID_ADDRESS || 592df08823dSKonstantin Belousov result == KERN_RESOURCE_SHORTAGE || 593df08823dSKonstantin Belousov result == KERN_PROTECTION_FAILURE || 594df08823dSKonstantin Belousov result == KERN_OUT_OF_BOUNDS, 595df08823dSKonstantin Belousov ("Unexpected Mach error %d from vm_fault()", result)); 59635818d2eSJohn Baldwin #ifdef KTRACE 597c31cec45SKonstantin Belousov if (map != kernel_map && KTRPOINT(curthread, KTR_FAULTEND)) 59835818d2eSJohn Baldwin ktrfaultend(result); 59935818d2eSJohn Baldwin #endif 600df08823dSKonstantin Belousov if (result != KERN_SUCCESS && signo != NULL) { 601df08823dSKonstantin Belousov switch (result) { 602df08823dSKonstantin Belousov case KERN_FAILURE: 603df08823dSKonstantin Belousov case KERN_INVALID_ADDRESS: 604df08823dSKonstantin Belousov *signo = SIGSEGV; 605df08823dSKonstantin Belousov *ucode = SEGV_MAPERR; 606df08823dSKonstantin Belousov break; 607df08823dSKonstantin Belousov case KERN_RESOURCE_SHORTAGE: 608df08823dSKonstantin Belousov *signo = SIGBUS; 609df08823dSKonstantin Belousov *ucode = BUS_OOMERR; 610df08823dSKonstantin Belousov break; 611df08823dSKonstantin Belousov case KERN_OUT_OF_BOUNDS: 612df08823dSKonstantin Belousov *signo = SIGBUS; 613df08823dSKonstantin Belousov *ucode = BUS_OBJERR; 614df08823dSKonstantin Belousov break; 615df08823dSKonstantin Belousov case KERN_PROTECTION_FAILURE: 616df08823dSKonstantin Belousov if (prot_fault_translation == 0) { 617df08823dSKonstantin Belousov /* 618df08823dSKonstantin Belousov * Autodetect. This check also covers 619df08823dSKonstantin Belousov * the images without the ABI-tag ELF 620df08823dSKonstantin Belousov * note. 621df08823dSKonstantin Belousov */ 622df08823dSKonstantin Belousov if (SV_CURPROC_ABI() == SV_ABI_FREEBSD && 623df08823dSKonstantin Belousov curproc->p_osrel >= P_OSREL_SIGSEGV) { 624df08823dSKonstantin Belousov *signo = SIGSEGV; 625df08823dSKonstantin Belousov *ucode = SEGV_ACCERR; 626df08823dSKonstantin Belousov } else { 627df08823dSKonstantin Belousov *signo = SIGBUS; 628df08823dSKonstantin Belousov *ucode = UCODE_PAGEFLT; 629df08823dSKonstantin Belousov } 630df08823dSKonstantin Belousov } else if (prot_fault_translation == 1) { 631df08823dSKonstantin Belousov /* Always compat mode. */ 632df08823dSKonstantin Belousov *signo = SIGBUS; 633df08823dSKonstantin Belousov *ucode = UCODE_PAGEFLT; 634df08823dSKonstantin Belousov } else { 635df08823dSKonstantin Belousov /* Always SIGSEGV mode. */ 636df08823dSKonstantin Belousov *signo = SIGSEGV; 637df08823dSKonstantin Belousov *ucode = SEGV_ACCERR; 638df08823dSKonstantin Belousov } 639df08823dSKonstantin Belousov break; 640df08823dSKonstantin Belousov default: 641df08823dSKonstantin Belousov KASSERT(0, ("Unexpected Mach error %d from vm_fault()", 642df08823dSKonstantin Belousov result)); 643df08823dSKonstantin Belousov break; 644df08823dSKonstantin Belousov } 645df08823dSKonstantin Belousov } 64635818d2eSJohn Baldwin return (result); 647acd11c74SAlan Cox } 648acd11c74SAlan Cox 6490ddd3082SKonstantin Belousov static int 6501e40fe41SJeff Roberson vm_fault_lock_vnode(struct faultstate *fs, bool objlocked) 6510ddd3082SKonstantin Belousov { 6520ddd3082SKonstantin Belousov struct vnode *vp; 6530ddd3082SKonstantin Belousov int error, locked; 6540ddd3082SKonstantin Belousov 6550ddd3082SKonstantin Belousov if (fs->object->type != OBJT_VNODE) 6560ddd3082SKonstantin Belousov return (KERN_SUCCESS); 6570ddd3082SKonstantin Belousov vp = fs->object->handle; 65816b0c092SKonstantin Belousov if (vp == fs->vp) { 65916b0c092SKonstantin Belousov ASSERT_VOP_LOCKED(vp, "saved vnode is not locked"); 6600ddd3082SKonstantin Belousov return (KERN_SUCCESS); 66116b0c092SKonstantin Belousov } 6620ddd3082SKonstantin Belousov 6630ddd3082SKonstantin Belousov /* 6640ddd3082SKonstantin Belousov * Perform an unlock in case the desired vnode changed while 6650ddd3082SKonstantin Belousov * the map was unlocked during a retry. 6660ddd3082SKonstantin Belousov */ 6670ddd3082SKonstantin Belousov unlock_vp(fs); 6680ddd3082SKonstantin Belousov 6690ddd3082SKonstantin Belousov locked = VOP_ISLOCKED(vp); 6700ddd3082SKonstantin Belousov if (locked != LK_EXCLUSIVE) 6710ddd3082SKonstantin Belousov locked = LK_SHARED; 6720ddd3082SKonstantin Belousov 6730ddd3082SKonstantin Belousov /* 6740ddd3082SKonstantin Belousov * We must not sleep acquiring the vnode lock while we have 6750ddd3082SKonstantin Belousov * the page exclusive busied or the object's 6760ddd3082SKonstantin Belousov * paging-in-progress count incremented. Otherwise, we could 6770ddd3082SKonstantin Belousov * deadlock. 6780ddd3082SKonstantin Belousov */ 6790ddd3082SKonstantin Belousov error = vget(vp, locked | LK_CANRECURSE | LK_NOWAIT, curthread); 6800ddd3082SKonstantin Belousov if (error == 0) { 6810ddd3082SKonstantin Belousov fs->vp = vp; 6820ddd3082SKonstantin Belousov return (KERN_SUCCESS); 6830ddd3082SKonstantin Belousov } 6840ddd3082SKonstantin Belousov 6850ddd3082SKonstantin Belousov vhold(vp); 6861e40fe41SJeff Roberson if (objlocked) 6870ddd3082SKonstantin Belousov unlock_and_deallocate(fs); 6881e40fe41SJeff Roberson else 6891e40fe41SJeff Roberson fault_deallocate(fs); 6900ddd3082SKonstantin Belousov error = vget(vp, locked | LK_RETRY | LK_CANRECURSE, curthread); 6910ddd3082SKonstantin Belousov vdrop(vp); 6920ddd3082SKonstantin Belousov fs->vp = vp; 6930ddd3082SKonstantin Belousov KASSERT(error == 0, ("vm_fault: vget failed %d", error)); 6940ddd3082SKonstantin Belousov return (KERN_RESOURCE_SHORTAGE); 6950ddd3082SKonstantin Belousov } 6960ddd3082SKonstantin Belousov 697bef91632SJeff Roberson /* 6985949b1caSJeff Roberson * Calculate the desired readahead. Handle drop-behind. 6995949b1caSJeff Roberson * 7005949b1caSJeff Roberson * Returns the number of readahead blocks to pass to the pager. 7015949b1caSJeff Roberson */ 7025949b1caSJeff Roberson static int 7035949b1caSJeff Roberson vm_fault_readahead(struct faultstate *fs) 7045949b1caSJeff Roberson { 7055949b1caSJeff Roberson int era, nera; 7065949b1caSJeff Roberson u_char behavior; 7075949b1caSJeff Roberson 7085949b1caSJeff Roberson KASSERT(fs->lookup_still_valid, ("map unlocked")); 7095949b1caSJeff Roberson era = fs->entry->read_ahead; 7105949b1caSJeff Roberson behavior = vm_map_entry_behavior(fs->entry); 7115949b1caSJeff Roberson if (behavior == MAP_ENTRY_BEHAV_RANDOM) { 7125949b1caSJeff Roberson nera = 0; 7135949b1caSJeff Roberson } else if (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL) { 7145949b1caSJeff Roberson nera = VM_FAULT_READ_AHEAD_MAX; 7155949b1caSJeff Roberson if (fs->vaddr == fs->entry->next_read) 7165949b1caSJeff Roberson vm_fault_dontneed(fs, fs->vaddr, nera); 7175949b1caSJeff Roberson } else if (fs->vaddr == fs->entry->next_read) { 7185949b1caSJeff Roberson /* 7195949b1caSJeff Roberson * This is a sequential fault. Arithmetically 7205949b1caSJeff Roberson * increase the requested number of pages in 7215949b1caSJeff Roberson * the read-ahead window. The requested 7225949b1caSJeff Roberson * number of pages is "# of sequential faults 7235949b1caSJeff Roberson * x (read ahead min + 1) + read ahead min" 7245949b1caSJeff Roberson */ 7255949b1caSJeff Roberson nera = VM_FAULT_READ_AHEAD_MIN; 7265949b1caSJeff Roberson if (era > 0) { 7275949b1caSJeff Roberson nera += era + 1; 7285949b1caSJeff Roberson if (nera > VM_FAULT_READ_AHEAD_MAX) 7295949b1caSJeff Roberson nera = VM_FAULT_READ_AHEAD_MAX; 7305949b1caSJeff Roberson } 7315949b1caSJeff Roberson if (era == VM_FAULT_READ_AHEAD_MAX) 7325949b1caSJeff Roberson vm_fault_dontneed(fs, fs->vaddr, nera); 7335949b1caSJeff Roberson } else { 7345949b1caSJeff Roberson /* 7355949b1caSJeff Roberson * This is a non-sequential fault. 7365949b1caSJeff Roberson */ 7375949b1caSJeff Roberson nera = 0; 7385949b1caSJeff Roberson } 7395949b1caSJeff Roberson if (era != nera) { 7405949b1caSJeff Roberson /* 7415949b1caSJeff Roberson * A read lock on the map suffices to update 7425949b1caSJeff Roberson * the read ahead count safely. 7435949b1caSJeff Roberson */ 7445949b1caSJeff Roberson fs->entry->read_ahead = nera; 7455949b1caSJeff Roberson } 7465949b1caSJeff Roberson 7475949b1caSJeff Roberson return (nera); 7485949b1caSJeff Roberson } 7495949b1caSJeff Roberson 750c308a3a6SJeff Roberson static int 751c308a3a6SJeff Roberson vm_fault_lookup(struct faultstate *fs) 752c308a3a6SJeff Roberson { 753c308a3a6SJeff Roberson int result; 754c308a3a6SJeff Roberson 755c308a3a6SJeff Roberson KASSERT(!fs->lookup_still_valid, 756c308a3a6SJeff Roberson ("vm_fault_lookup: Map already locked.")); 757c308a3a6SJeff Roberson result = vm_map_lookup(&fs->map, fs->vaddr, fs->fault_type | 758c308a3a6SJeff Roberson VM_PROT_FAULT_LOOKUP, &fs->entry, &fs->first_object, 759c308a3a6SJeff Roberson &fs->first_pindex, &fs->prot, &fs->wired); 760c308a3a6SJeff Roberson if (result != KERN_SUCCESS) { 761c308a3a6SJeff Roberson unlock_vp(fs); 762c308a3a6SJeff Roberson return (result); 763c308a3a6SJeff Roberson } 764c308a3a6SJeff Roberson 765c308a3a6SJeff Roberson fs->map_generation = fs->map->timestamp; 766c308a3a6SJeff Roberson 767c308a3a6SJeff Roberson if (fs->entry->eflags & MAP_ENTRY_NOFAULT) { 768c308a3a6SJeff Roberson panic("%s: fault on nofault entry, addr: %#lx", 769c308a3a6SJeff Roberson __func__, (u_long)fs->vaddr); 770c308a3a6SJeff Roberson } 771c308a3a6SJeff Roberson 772c308a3a6SJeff Roberson if (fs->entry->eflags & MAP_ENTRY_IN_TRANSITION && 773c308a3a6SJeff Roberson fs->entry->wiring_thread != curthread) { 774c308a3a6SJeff Roberson vm_map_unlock_read(fs->map); 775c308a3a6SJeff Roberson vm_map_lock(fs->map); 776c308a3a6SJeff Roberson if (vm_map_lookup_entry(fs->map, fs->vaddr, &fs->entry) && 777c308a3a6SJeff Roberson (fs->entry->eflags & MAP_ENTRY_IN_TRANSITION)) { 778c308a3a6SJeff Roberson unlock_vp(fs); 779c308a3a6SJeff Roberson fs->entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 780c308a3a6SJeff Roberson vm_map_unlock_and_wait(fs->map, 0); 781c308a3a6SJeff Roberson } else 782c308a3a6SJeff Roberson vm_map_unlock(fs->map); 783c308a3a6SJeff Roberson return (KERN_RESOURCE_SHORTAGE); 784c308a3a6SJeff Roberson } 785c308a3a6SJeff Roberson 786c308a3a6SJeff Roberson MPASS((fs->entry->eflags & MAP_ENTRY_GUARD) == 0); 787c308a3a6SJeff Roberson 788c308a3a6SJeff Roberson if (fs->wired) 789c308a3a6SJeff Roberson fs->fault_type = fs->prot | (fs->fault_type & VM_PROT_COPY); 790c308a3a6SJeff Roberson else 791c308a3a6SJeff Roberson KASSERT((fs->fault_flags & VM_FAULT_WIRE) == 0, 792c308a3a6SJeff Roberson ("!fs->wired && VM_FAULT_WIRE")); 793c308a3a6SJeff Roberson fs->lookup_still_valid = true; 794c308a3a6SJeff Roberson 795c308a3a6SJeff Roberson return (KERN_SUCCESS); 796c308a3a6SJeff Roberson } 797c308a3a6SJeff Roberson 798*fcb04758SJeff Roberson static int 799*fcb04758SJeff Roberson vm_fault_relookup(struct faultstate *fs) 800*fcb04758SJeff Roberson { 801*fcb04758SJeff Roberson vm_object_t retry_object; 802*fcb04758SJeff Roberson vm_pindex_t retry_pindex; 803*fcb04758SJeff Roberson vm_prot_t retry_prot; 804*fcb04758SJeff Roberson int result; 805*fcb04758SJeff Roberson 806*fcb04758SJeff Roberson if (!vm_map_trylock_read(fs->map)) 807*fcb04758SJeff Roberson return (KERN_RESTART); 808*fcb04758SJeff Roberson 809*fcb04758SJeff Roberson fs->lookup_still_valid = true; 810*fcb04758SJeff Roberson if (fs->map->timestamp == fs->map_generation) 811*fcb04758SJeff Roberson return (KERN_SUCCESS); 812*fcb04758SJeff Roberson 813*fcb04758SJeff Roberson result = vm_map_lookup_locked(&fs->map, fs->vaddr, fs->fault_type, 814*fcb04758SJeff Roberson &fs->entry, &retry_object, &retry_pindex, &retry_prot, 815*fcb04758SJeff Roberson &fs->wired); 816*fcb04758SJeff Roberson if (result != KERN_SUCCESS) { 817*fcb04758SJeff Roberson /* 818*fcb04758SJeff Roberson * If retry of map lookup would have blocked then 819*fcb04758SJeff Roberson * retry fault from start. 820*fcb04758SJeff Roberson */ 821*fcb04758SJeff Roberson if (result == KERN_FAILURE) 822*fcb04758SJeff Roberson return (KERN_RESTART); 823*fcb04758SJeff Roberson return (result); 824*fcb04758SJeff Roberson } 825*fcb04758SJeff Roberson if (retry_object != fs->first_object || 826*fcb04758SJeff Roberson retry_pindex != fs->first_pindex) 827*fcb04758SJeff Roberson return (KERN_RESTART); 828*fcb04758SJeff Roberson 829*fcb04758SJeff Roberson /* 830*fcb04758SJeff Roberson * Check whether the protection has changed or the object has 831*fcb04758SJeff Roberson * been copied while we left the map unlocked. Changing from 832*fcb04758SJeff Roberson * read to write permission is OK - we leave the page 833*fcb04758SJeff Roberson * write-protected, and catch the write fault. Changing from 834*fcb04758SJeff Roberson * write to read permission means that we can't mark the page 835*fcb04758SJeff Roberson * write-enabled after all. 836*fcb04758SJeff Roberson */ 837*fcb04758SJeff Roberson fs->prot &= retry_prot; 838*fcb04758SJeff Roberson fs->fault_type &= retry_prot; 839*fcb04758SJeff Roberson if (fs->prot == 0) 840*fcb04758SJeff Roberson return (KERN_RESTART); 841*fcb04758SJeff Roberson 842*fcb04758SJeff Roberson /* Reassert because wired may have changed. */ 843*fcb04758SJeff Roberson KASSERT(fs->wired || (fs->fault_flags & VM_FAULT_WIRE) == 0, 844*fcb04758SJeff Roberson ("!wired && VM_FAULT_WIRE")); 845*fcb04758SJeff Roberson 846*fcb04758SJeff Roberson return (KERN_SUCCESS); 847*fcb04758SJeff Roberson } 848*fcb04758SJeff Roberson 8495949b1caSJeff Roberson /* 850bef91632SJeff Roberson * Wait/Retry if the page is busy. We have to do this if the page is 851bef91632SJeff Roberson * either exclusive or shared busy because the vm_pager may be using 852bef91632SJeff Roberson * read busy for pageouts (and even pageins if it is the vnode pager), 853bef91632SJeff Roberson * and we could end up trying to pagein and pageout the same page 854bef91632SJeff Roberson * simultaneously. 855bef91632SJeff Roberson * 856bef91632SJeff Roberson * We can theoretically allow the busy case on a read fault if the page 857bef91632SJeff Roberson * is marked valid, but since such pages are typically already pmap'd, 858bef91632SJeff Roberson * putting that special case in might be more effort then it is worth. 859bef91632SJeff Roberson * We cannot under any circumstances mess around with a shared busied 860bef91632SJeff Roberson * page except, perhaps, to pmap it. 861bef91632SJeff Roberson */ 862bef91632SJeff Roberson static void 863bef91632SJeff Roberson vm_fault_busy_sleep(struct faultstate *fs) 864bef91632SJeff Roberson { 865bef91632SJeff Roberson /* 866bef91632SJeff Roberson * Reference the page before unlocking and 867bef91632SJeff Roberson * sleeping so that the page daemon is less 868bef91632SJeff Roberson * likely to reclaim it. 869bef91632SJeff Roberson */ 870bef91632SJeff Roberson vm_page_aflag_set(fs->m, PGA_REFERENCED); 871bef91632SJeff Roberson if (fs->object != fs->first_object) { 872bef91632SJeff Roberson fault_page_release(&fs->first_m); 873bef91632SJeff Roberson vm_object_pip_wakeup(fs->first_object); 874bef91632SJeff Roberson } 875bef91632SJeff Roberson vm_object_pip_wakeup(fs->object); 876bef91632SJeff Roberson unlock_map(fs); 877bef91632SJeff Roberson if (fs->m == vm_page_lookup(fs->object, fs->pindex)) 8787e1b379eSJeff Roberson vm_page_busy_sleep(fs->m, "vmpfw", false); 8797e1b379eSJeff Roberson else 880bef91632SJeff Roberson VM_OBJECT_WUNLOCK(fs->object); 881bef91632SJeff Roberson VM_CNT_INC(v_intrans); 882bef91632SJeff Roberson vm_object_deallocate(fs->first_object); 883bef91632SJeff Roberson } 884bef91632SJeff Roberson 885acd11c74SAlan Cox int 886df08823dSKonstantin Belousov vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, 887acd11c74SAlan Cox int fault_flags, vm_page_t *m_hold) 888acd11c74SAlan Cox { 8894866e085SJohn Dyson struct faultstate fs; 89023984ce5SMark Johnston struct domainset *dset; 891*fcb04758SJeff Roberson vm_object_t next_object; 8920c3a4893SAlan Cox vm_offset_t e_end, e_start; 8935949b1caSJeff Roberson int ahead, alloc_req, behind, cluster_offset, faultcount; 8940ddd3082SKonstantin Belousov int nera, oom, result, rv; 8950c3a4893SAlan Cox u_char behavior; 89619bd0d9cSKonstantin Belousov bool dead, hardfault, is_first_object_locked; 897df8bae1dSRodney W. Grimes 89883c9dea1SGleb Smirnoff VM_CNT_INC(v_vm_faults); 899c31cec45SKonstantin Belousov 900c31cec45SKonstantin Belousov if ((curthread->td_pflags & TDP_NOFAULTING) != 0) 901c31cec45SKonstantin Belousov return (KERN_PROTECTION_FAILURE); 902c31cec45SKonstantin Belousov 903d2bf64c3SKonstantin Belousov fs.vp = NULL; 9045949b1caSJeff Roberson fs.vaddr = vaddr; 9052c2f4413SJeff Roberson fs.m_hold = m_hold; 9062c2f4413SJeff Roberson fs.fault_flags = fault_flags; 907c308a3a6SJeff Roberson fs.map = map; 908c308a3a6SJeff Roberson fs.lookup_still_valid = false; 909b0cd2017SGleb Smirnoff faultcount = 0; 9100c3a4893SAlan Cox nera = -1; 911320023e2SAlan Cox hardfault = false; 912df8bae1dSRodney W. Grimes 913245139c6SKonstantin Belousov RetryFault: 914245139c6SKonstantin Belousov oom = 0; 915245139c6SKonstantin Belousov RetryFault_oom: 9162c2f4413SJeff Roberson fs.fault_type = fault_type; 917df8bae1dSRodney W. Grimes 918df8bae1dSRodney W. Grimes /* 9190d94caffSDavid Greenman * Find the backing store object and offset into it to begin the 9200d94caffSDavid Greenman * search. 921df8bae1dSRodney W. Grimes */ 922c308a3a6SJeff Roberson result = vm_fault_lookup(&fs); 92392de35b0SAlan Cox if (result != KERN_SUCCESS) { 924c308a3a6SJeff Roberson if (result == KERN_RESOURCE_SHORTAGE) 925c308a3a6SJeff Roberson goto RetryFault; 92692de35b0SAlan Cox return (result); 92709e0c6ccSJohn Dyson } 92809e0c6ccSJohn Dyson 9298d67b8c8SAlan Cox /* 9308d67b8c8SAlan Cox * Try to avoid lock contention on the top-level object through 9318d67b8c8SAlan Cox * special-case handling of some types of page faults, specifically, 93267d0e293SJeff Roberson * those that are mapping an existing page from the top-level object. 93367d0e293SJeff Roberson * Under this condition, a read lock on the object suffices, allowing 93467d0e293SJeff Roberson * multiple page faults of a similar type to run in parallel. 9358d67b8c8SAlan Cox */ 936afe55ca3SKonstantin Belousov if (fs.vp == NULL /* avoid locked vnode leak */ && 9372c2f4413SJeff Roberson (fs.fault_flags & (VM_FAULT_WIRE | VM_FAULT_DIRTY)) == 0) { 938afe55ca3SKonstantin Belousov VM_OBJECT_RLOCK(fs.first_object); 9392c2f4413SJeff Roberson rv = vm_fault_soft_fast(&fs); 94041ddec83SKonstantin Belousov if (rv == KERN_SUCCESS) 94141ddec83SKonstantin Belousov return (rv); 942afe55ca3SKonstantin Belousov if (!VM_OBJECT_TRYUPGRADE(fs.first_object)) { 943afe55ca3SKonstantin Belousov VM_OBJECT_RUNLOCK(fs.first_object); 944afe55ca3SKonstantin Belousov VM_OBJECT_WLOCK(fs.first_object); 945afe55ca3SKonstantin Belousov } 946afe55ca3SKonstantin Belousov } else { 947afe55ca3SKonstantin Belousov VM_OBJECT_WLOCK(fs.first_object); 948afe55ca3SKonstantin Belousov } 949afe55ca3SKonstantin Belousov 95095e5e988SJohn Dyson /* 95195e5e988SJohn Dyson * Make a reference to this object to prevent its disposal while we 95295e5e988SJohn Dyson * are messing with it. Once we have the reference, the map is free 95395e5e988SJohn Dyson * to be diddled. Since objects reference their shadows (and copies), 95495e5e988SJohn Dyson * they will stay around as well. 955fe8e0238SMatthew Dillon * 956fe8e0238SMatthew Dillon * Bump the paging-in-progress count to prevent size changes (e.g. 957dda4d369SAlan Cox * truncation operations) during I/O. 95895e5e988SJohn Dyson */ 959a976eb5eSAlan Cox vm_object_reference_locked(fs.first_object); 960d474eaaaSDoug Rabson vm_object_pip_add(fs.first_object, 1); 96195e5e988SJohn Dyson 96258447749SJeff Roberson fs.m_cow = fs.m = fs.first_m = NULL; 963df8bae1dSRodney W. Grimes 964df8bae1dSRodney W. Grimes /* 965df8bae1dSRodney W. Grimes * Search for the page at object/offset. 966df8bae1dSRodney W. Grimes */ 9674866e085SJohn Dyson fs.object = fs.first_object; 9684866e085SJohn Dyson fs.pindex = fs.first_pindex; 969df8bae1dSRodney W. Grimes while (TRUE) { 9704bf95d00SJeff Roberson KASSERT(fs.m == NULL, 9714bf95d00SJeff Roberson ("page still set %p at loop start", fs.m)); 9721c7c3c6aSMatthew Dillon /* 973725441f6SKonstantin Belousov * If the object is marked for imminent termination, 974725441f6SKonstantin Belousov * we retry here, since the collapse pass has raced 975725441f6SKonstantin Belousov * with us. Otherwise, if we see terminally dead 976725441f6SKonstantin Belousov * object, return fail. 9771c7c3c6aSMatthew Dillon */ 978725441f6SKonstantin Belousov if ((fs.object->flags & OBJ_DEAD) != 0) { 979725441f6SKonstantin Belousov dead = fs.object->type == OBJT_DEAD; 9804866e085SJohn Dyson unlock_and_deallocate(&fs); 981725441f6SKonstantin Belousov if (dead) 98247221757SJohn Dyson return (KERN_PROTECTION_FAILURE); 983725441f6SKonstantin Belousov pause("vmf_de", 1); 984725441f6SKonstantin Belousov goto RetryFault; 98547221757SJohn Dyson } 98647221757SJohn Dyson 9871c7c3c6aSMatthew Dillon /* 9881c7c3c6aSMatthew Dillon * See if page is resident 9891c7c3c6aSMatthew Dillon */ 9904866e085SJohn Dyson fs.m = vm_page_lookup(fs.object, fs.pindex); 9914866e085SJohn Dyson if (fs.m != NULL) { 99263e97555SJeff Roberson if (vm_page_tryxbusy(fs.m) == 0) { 993bef91632SJeff Roberson vm_fault_busy_sleep(&fs); 994df8bae1dSRodney W. Grimes goto RetryFault; 995df8bae1dSRodney W. Grimes } 9967615edaaSMatthew Dillon 9971c7c3c6aSMatthew Dillon /* 99863e97555SJeff Roberson * The page is marked busy for other processes and the 9991c7c3c6aSMatthew Dillon * pagedaemon. If it still isn't completely valid 10001c7c3c6aSMatthew Dillon * (readable), jump to readrest, else break-out ( we 10011c7c3c6aSMatthew Dillon * found the page ). 10021c7c3c6aSMatthew Dillon */ 10030012f373SJeff Roberson if (!vm_page_all_valid(fs.m)) 10040d94caffSDavid Greenman goto readrest; 10051e40fe41SJeff Roberson VM_OBJECT_WUNLOCK(fs.object); 10061e40fe41SJeff Roberson break; /* break to PAGE HAS BEEN FOUND. */ 1007df8bae1dSRodney W. Grimes } 100810b4196bSAlan Cox KASSERT(fs.m == NULL, ("fs.m should be NULL, not %p", fs.m)); 10091e40fe41SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(fs.object); 10101c7c3c6aSMatthew Dillon 10111c7c3c6aSMatthew Dillon /* 101210b4196bSAlan Cox * Page is not resident. If the pager might contain the page 101310b4196bSAlan Cox * or this is the beginning of the search, allocate a new 101410b4196bSAlan Cox * page. (Default objects are zero-fill, so there is no real 101510b4196bSAlan Cox * pager for them.) 10161c7c3c6aSMatthew Dillon */ 10176a875bf9SKonstantin Belousov if (fs.object->type != OBJT_DEFAULT || 10186a875bf9SKonstantin Belousov fs.object == fs.first_object) { 1019208b81bbSKonstantin Belousov if ((fs.object->flags & OBJ_SIZEVNLOCK) != 0) { 10201e40fe41SJeff Roberson rv = vm_fault_lock_vnode(&fs, true); 1021208b81bbSKonstantin Belousov MPASS(rv == KERN_SUCCESS || 1022208b81bbSKonstantin Belousov rv == KERN_RESOURCE_SHORTAGE); 1023208b81bbSKonstantin Belousov if (rv == KERN_RESOURCE_SHORTAGE) 1024208b81bbSKonstantin Belousov goto RetryFault; 1025208b81bbSKonstantin Belousov } 10264866e085SJohn Dyson if (fs.pindex >= fs.object->size) { 10274866e085SJohn Dyson unlock_and_deallocate(&fs); 1028df08823dSKonstantin Belousov return (KERN_OUT_OF_BOUNDS); 10295f55e841SDavid Greenman } 103022ba64e8SJohn Dyson 1031c42b43a0SKonstantin Belousov if (fs.object == fs.first_object && 1032c42b43a0SKonstantin Belousov (fs.first_object->flags & OBJ_POPULATE) != 0 && 1033c42b43a0SKonstantin Belousov fs.first_object->shadow_count == 0) { 10342c2f4413SJeff Roberson rv = vm_fault_populate(&fs); 1035c42b43a0SKonstantin Belousov switch (rv) { 1036c42b43a0SKonstantin Belousov case KERN_SUCCESS: 1037c42b43a0SKonstantin Belousov case KERN_FAILURE: 1038c42b43a0SKonstantin Belousov unlock_and_deallocate(&fs); 1039c42b43a0SKonstantin Belousov return (rv); 1040c42b43a0SKonstantin Belousov case KERN_RESOURCE_SHORTAGE: 1041c42b43a0SKonstantin Belousov unlock_and_deallocate(&fs); 1042c42b43a0SKonstantin Belousov goto RetryFault; 1043c42b43a0SKonstantin Belousov case KERN_NOT_RECEIVER: 1044c42b43a0SKonstantin Belousov /* 1045c42b43a0SKonstantin Belousov * Pager's populate() method 1046c42b43a0SKonstantin Belousov * returned VM_PAGER_BAD. 1047c42b43a0SKonstantin Belousov */ 1048c42b43a0SKonstantin Belousov break; 1049c42b43a0SKonstantin Belousov default: 1050c42b43a0SKonstantin Belousov panic("inconsistent return codes"); 1051c42b43a0SKonstantin Belousov } 1052c42b43a0SKonstantin Belousov } 1053c42b43a0SKonstantin Belousov 1054df8bae1dSRodney W. Grimes /* 10550d94caffSDavid Greenman * Allocate a new page for this object/offset pair. 10563f1c4c4fSKonstantin Belousov * 10573f1c4c4fSKonstantin Belousov * Unlocked read of the p_flag is harmless. At 10583f1c4c4fSKonstantin Belousov * worst, the P_KILLED might be not observed 10593f1c4c4fSKonstantin Belousov * there, and allocation can fail, causing 10603f1c4c4fSKonstantin Belousov * restart and new reading of the p_flag. 1061df8bae1dSRodney W. Grimes */ 106223984ce5SMark Johnston dset = fs.object->domain.dr_policy; 106323984ce5SMark Johnston if (dset == NULL) 106423984ce5SMark Johnston dset = curthread->td_domain.dr_policy; 106523984ce5SMark Johnston if (!vm_page_count_severe_set(&dset->ds_mask) || 106623984ce5SMark Johnston P_KILLED(curproc)) { 1067f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 10683d653db0SAlan Cox vm_object_color(fs.object, atop(vaddr) - 10693d653db0SAlan Cox fs.pindex); 1070f8a47341SAlan Cox #endif 10713f1c4c4fSKonstantin Belousov alloc_req = P_KILLED(curproc) ? 10723f1c4c4fSKonstantin Belousov VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL; 10733f1c4c4fSKonstantin Belousov if (fs.object->type != OBJT_VNODE && 10743f1c4c4fSKonstantin Belousov fs.object->backing_object == NULL) 10753f1c4c4fSKonstantin Belousov alloc_req |= VM_ALLOC_ZERO; 10764866e085SJohn Dyson fs.m = vm_page_alloc(fs.object, fs.pindex, 10773f1c4c4fSKonstantin Belousov alloc_req); 107840360b1bSMatthew Dillon } 10794866e085SJohn Dyson if (fs.m == NULL) { 10804866e085SJohn Dyson unlock_and_deallocate(&fs); 1081245139c6SKonstantin Belousov if (vm_pfault_oom_attempts < 0 || 1082245139c6SKonstantin Belousov oom < vm_pfault_oom_attempts) { 1083245139c6SKonstantin Belousov oom++; 1084245139c6SKonstantin Belousov vm_waitpfault(dset, 1085245139c6SKonstantin Belousov vm_pfault_oom_wait * hz); 1086245139c6SKonstantin Belousov goto RetryFault_oom; 1087245139c6SKonstantin Belousov } 1088245139c6SKonstantin Belousov if (bootverbose) 1089245139c6SKonstantin Belousov printf( 1090245139c6SKonstantin Belousov "proc %d (%s) failed to alloc page on fault, starting OOM\n", 1091245139c6SKonstantin Belousov curproc->p_pid, curproc->p_comm); 1092245139c6SKonstantin Belousov vm_pageout_oom(VM_OOM_MEM_PF); 1093df8bae1dSRodney W. Grimes goto RetryFault; 10947667839aSAlan Cox } 1095df8bae1dSRodney W. Grimes } 109647221757SJohn Dyson 10970d94caffSDavid Greenman readrest: 10981c7c3c6aSMatthew Dillon /* 10991e40fe41SJeff Roberson * Default objects have no pager so no exclusive busy exists 11001e40fe41SJeff Roberson * to protect this page in the chain. Skip to the next 11011e40fe41SJeff Roberson * object without dropping the lock to preserve atomicity of 11021e40fe41SJeff Roberson * shadow faults. 11031e40fe41SJeff Roberson */ 11041e40fe41SJeff Roberson if (fs.object->type == OBJT_DEFAULT) 11051e40fe41SJeff Roberson goto next; 11061e40fe41SJeff Roberson 11071e40fe41SJeff Roberson /* 110885702505SAlan Cox * At this point, we have either allocated a new page or found 110985702505SAlan Cox * an existing page that is only partially valid. 111085702505SAlan Cox * 111185702505SAlan Cox * We hold a reference on the current object and the page is 11121e40fe41SJeff Roberson * exclusive busied. The exclusive busy prevents simultaneous 11131e40fe41SJeff Roberson * faults and collapses while the object lock is dropped. 111485702505SAlan Cox */ 11151e40fe41SJeff Roberson VM_OBJECT_WUNLOCK(fs.object); 111685702505SAlan Cox 111785702505SAlan Cox /* 11180c3a4893SAlan Cox * If the pager for the current object might have the page, 11190c3a4893SAlan Cox * then determine the number of additional pages to read and 11200c3a4893SAlan Cox * potentially reprioritize previously read pages for earlier 11210c3a4893SAlan Cox * reclamation. These operations should only be performed 11220c3a4893SAlan Cox * once per page fault. Even if the current pager doesn't 11230c3a4893SAlan Cox * have the page, the number of additional pages to read will 11240c3a4893SAlan Cox * apply to subsequent objects in the shadow chain. 11251c7c3c6aSMatthew Dillon */ 11261e40fe41SJeff Roberson if (nera == -1 && !P_KILLED(curproc)) { 11275949b1caSJeff Roberson nera = vm_fault_readahead(&fs); 1128d2bf64c3SKonstantin Belousov /* 11290c3a4893SAlan Cox * Prepare for unlocking the map. Save the map 11300c3a4893SAlan Cox * entry's start and end addresses, which are used to 11310c3a4893SAlan Cox * optimize the size of the pager operation below. 11320c3a4893SAlan Cox * Even if the map entry's addresses change after 11330c3a4893SAlan Cox * unlocking the map, using the saved addresses is 11340c3a4893SAlan Cox * safe. 11350c3a4893SAlan Cox */ 11360c3a4893SAlan Cox e_start = fs.entry->start; 11370c3a4893SAlan Cox e_end = fs.entry->end; 11385949b1caSJeff Roberson behavior = vm_map_entry_behavior(fs.entry); 11390c3a4893SAlan Cox } 11400c3a4893SAlan Cox 11410c3a4893SAlan Cox /* 11420c3a4893SAlan Cox * Call the pager to retrieve the page if there is a chance 11430c3a4893SAlan Cox * that the pager has it, and potentially retrieve additional 11440c3a4893SAlan Cox * pages at the same time. 11450c3a4893SAlan Cox */ 11460c3a4893SAlan Cox if (fs.object->type != OBJT_DEFAULT) { 11470c3a4893SAlan Cox /* 114885702505SAlan Cox * Release the map lock before locking the vnode or 114985702505SAlan Cox * sleeping in the pager. (If the current object has 115085702505SAlan Cox * a shadow, then an earlier iteration of this loop 115185702505SAlan Cox * may have already unlocked the map.) 1152d2bf64c3SKonstantin Belousov */ 1153d2bf64c3SKonstantin Belousov unlock_map(&fs); 1154d2bf64c3SKonstantin Belousov 11551e40fe41SJeff Roberson rv = vm_fault_lock_vnode(&fs, false); 11560ddd3082SKonstantin Belousov MPASS(rv == KERN_SUCCESS || 11570ddd3082SKonstantin Belousov rv == KERN_RESOURCE_SHORTAGE); 11580ddd3082SKonstantin Belousov if (rv == KERN_RESOURCE_SHORTAGE) 1159d2bf64c3SKonstantin Belousov goto RetryFault; 1160d2bf64c3SKonstantin Belousov KASSERT(fs.vp == NULL || !fs.map->system_map, 1161d2bf64c3SKonstantin Belousov ("vm_fault: vnode-backed object mapped by system map")); 1162d2bf64c3SKonstantin Belousov 1163df8bae1dSRodney W. Grimes /* 1164b0cd2017SGleb Smirnoff * Page in the requested page and hint the pager, 1165b0cd2017SGleb Smirnoff * that it may bring up surrounding pages. 116626f9a767SRodney W. Grimes */ 11670c3a4893SAlan Cox if (nera == -1 || behavior == MAP_ENTRY_BEHAV_RANDOM || 11680c3a4893SAlan Cox P_KILLED(curproc)) { 11690c3a4893SAlan Cox behind = 0; 11700c3a4893SAlan Cox ahead = 0; 11710c3a4893SAlan Cox } else { 11720c3a4893SAlan Cox /* Is this a sequential fault? */ 11730c3a4893SAlan Cox if (nera > 0) { 11740c3a4893SAlan Cox behind = 0; 11750c3a4893SAlan Cox ahead = nera; 11760c3a4893SAlan Cox } else { 11770c3a4893SAlan Cox /* 11780c3a4893SAlan Cox * Request a cluster of pages that is 11790c3a4893SAlan Cox * aligned to a VM_FAULT_READ_DEFAULT 11800c3a4893SAlan Cox * page offset boundary within the 11810c3a4893SAlan Cox * object. Alignment to a page offset 11820c3a4893SAlan Cox * boundary is more likely to coincide 11830c3a4893SAlan Cox * with the underlying file system 11840c3a4893SAlan Cox * block than alignment to a virtual 11850c3a4893SAlan Cox * address boundary. 11860c3a4893SAlan Cox */ 11870c3a4893SAlan Cox cluster_offset = fs.pindex % 11880c3a4893SAlan Cox VM_FAULT_READ_DEFAULT; 11890c3a4893SAlan Cox behind = ulmin(cluster_offset, 11900c3a4893SAlan Cox atop(vaddr - e_start)); 11910c3a4893SAlan Cox ahead = VM_FAULT_READ_DEFAULT - 1 - 11920c3a4893SAlan Cox cluster_offset; 11930c3a4893SAlan Cox } 11940c3a4893SAlan Cox ahead = ulmin(ahead, atop(e_end - vaddr) - 1); 11950c3a4893SAlan Cox } 1196b0cd2017SGleb Smirnoff rv = vm_pager_get_pages(fs.object, &fs.m, 1, 1197b0cd2017SGleb Smirnoff &behind, &ahead); 119826f9a767SRodney W. Grimes if (rv == VM_PAGER_OK) { 1199b0cd2017SGleb Smirnoff faultcount = behind + 1 + ahead; 1200320023e2SAlan Cox hardfault = true; 12011e40fe41SJeff Roberson break; /* break to PAGE HAS BEEN FOUND. */ 1202df8bae1dSRodney W. Grimes } 12031e40fe41SJeff Roberson VM_OBJECT_WLOCK(fs.object); 1204a83c285cSDavid Greenman if (rv == VM_PAGER_ERROR) 1205f3679e35SDavid Greenman printf("vm_fault: pager read error, pid %d (%s)\n", 1206f3679e35SDavid Greenman curproc->p_pid, curproc->p_comm); 1207521ddf39SAlan Cox 120826f9a767SRodney W. Grimes /* 1209521ddf39SAlan Cox * If an I/O error occurred or the requested page was 1210521ddf39SAlan Cox * outside the range of the pager, clean up and return 1211521ddf39SAlan Cox * an error. 121226f9a767SRodney W. Grimes */ 1213521ddf39SAlan Cox if (rv == VM_PAGER_ERROR || rv == VM_PAGER_BAD) { 12144bf95d00SJeff Roberson fault_page_free(&fs.m); 12154866e085SJohn Dyson unlock_and_deallocate(&fs); 1216df08823dSKonstantin Belousov return (KERN_OUT_OF_BOUNDS); 121726f9a767SRodney W. Grimes } 1218521ddf39SAlan Cox 12194bf95d00SJeff Roberson } 12204bf95d00SJeff Roberson 12211e40fe41SJeff Roberson next: 1222521ddf39SAlan Cox /* 1223521ddf39SAlan Cox * The requested page does not exist at this object/ 1224521ddf39SAlan Cox * offset. Remove the invalid page from the object, 1225521ddf39SAlan Cox * waking up anyone waiting for it, and continue on to 1226521ddf39SAlan Cox * the next object. However, if this is the top-level 1227521ddf39SAlan Cox * object, we must leave the busy page in place to 1228521ddf39SAlan Cox * prevent another process from rushing past us, and 1229521ddf39SAlan Cox * inserting the page in that object at the same time 1230521ddf39SAlan Cox * that we are. 1231521ddf39SAlan Cox */ 12324bf95d00SJeff Roberson if (fs.object == fs.first_object) { 12334866e085SJohn Dyson fs.first_m = fs.m; 12344bf95d00SJeff Roberson fs.m = NULL; 12354bf95d00SJeff Roberson } else 12364bf95d00SJeff Roberson fault_page_free(&fs.m); 1237df8bae1dSRodney W. Grimes 1238df8bae1dSRodney W. Grimes /* 12390d94caffSDavid Greenman * Move on to the next object. Lock the next object before 12400d94caffSDavid Greenman * unlocking the current one. 1241df8bae1dSRodney W. Grimes */ 12421e40fe41SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(fs.object); 12434866e085SJohn Dyson next_object = fs.object->backing_object; 1244df8bae1dSRodney W. Grimes if (next_object == NULL) { 1245df8bae1dSRodney W. Grimes /* 12460d94caffSDavid Greenman * If there's no object left, fill the page in the top 12470d94caffSDavid Greenman * object with zeros. 1248df8bae1dSRodney W. Grimes */ 12491e40fe41SJeff Roberson VM_OBJECT_WUNLOCK(fs.object); 12504866e085SJohn Dyson if (fs.object != fs.first_object) { 12514866e085SJohn Dyson vm_object_pip_wakeup(fs.object); 12524866e085SJohn Dyson fs.object = fs.first_object; 12534866e085SJohn Dyson fs.pindex = fs.first_pindex; 1254df8bae1dSRodney W. Grimes } 12554bf95d00SJeff Roberson MPASS(fs.first_m != NULL); 12564bf95d00SJeff Roberson MPASS(fs.m == NULL); 12574bf95d00SJeff Roberson fs.m = fs.first_m; 12584866e085SJohn Dyson fs.first_m = NULL; 1259df8bae1dSRodney W. Grimes 12604221e284SAlan Cox /* 12614221e284SAlan Cox * Zero the page if necessary and mark it valid. 12624221e284SAlan Cox */ 12634866e085SJohn Dyson if ((fs.m->flags & PG_ZERO) == 0) { 1264fff6062aSAlan Cox pmap_zero_page(fs.m); 12654221e284SAlan Cox } else { 126683c9dea1SGleb Smirnoff VM_CNT_INC(v_ozfod); 12674221e284SAlan Cox } 126883c9dea1SGleb Smirnoff VM_CNT_INC(v_zfod); 12690012f373SJeff Roberson vm_page_valid(fs.m); 12707b9b301cSAlan Cox /* Don't try to prefault neighboring pages. */ 12717b9b301cSAlan Cox faultcount = 1; 12721e40fe41SJeff Roberson break; /* break to PAGE HAS BEEN FOUND. */ 12730d94caffSDavid Greenman } else { 12744bf95d00SJeff Roberson MPASS(fs.first_m != NULL); 1275c8567c3aSAlan Cox KASSERT(fs.object != next_object, 1276c8567c3aSAlan Cox ("object loop %p", next_object)); 127789f6b863SAttilio Rao VM_OBJECT_WLOCK(next_object); 1278c8567c3aSAlan Cox vm_object_pip_add(next_object, 1); 1279c8567c3aSAlan Cox if (fs.object != fs.first_object) 12804866e085SJohn Dyson vm_object_pip_wakeup(fs.object); 12816753423cSAlan Cox fs.pindex += 12826753423cSAlan Cox OFF_TO_IDX(fs.object->backing_object_offset); 128389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(fs.object); 12844866e085SJohn Dyson fs.object = next_object; 1285df8bae1dSRodney W. Grimes } 1286df8bae1dSRodney W. Grimes } 12871c7c3c6aSMatthew Dillon 1288df8bae1dSRodney W. Grimes /* 12891e40fe41SJeff Roberson * PAGE HAS BEEN FOUND. A valid page has been found and exclusively 12901e40fe41SJeff Roberson * busied. The object lock must no longer be held. 1291df8bae1dSRodney W. Grimes */ 12921e40fe41SJeff Roberson vm_page_assert_xbusied(fs.m); 12931e40fe41SJeff Roberson VM_OBJECT_ASSERT_UNLOCKED(fs.object); 1294df8bae1dSRodney W. Grimes 1295df8bae1dSRodney W. Grimes /* 12960d94caffSDavid Greenman * If the page is being written, but isn't already owned by the 12970d94caffSDavid Greenman * top-level object, we have to copy it into a new page owned by the 12980d94caffSDavid Greenman * top-level object. 1299df8bae1dSRodney W. Grimes */ 13004866e085SJohn Dyson if (fs.object != fs.first_object) { 1301df8bae1dSRodney W. Grimes /* 13020d94caffSDavid Greenman * We only really need to copy if we want to write it. 1303df8bae1dSRodney W. Grimes */ 13042c2f4413SJeff Roberson if ((fs.fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) { 1305df8bae1dSRodney W. Grimes /* 13061c7c3c6aSMatthew Dillon * This allows pages to be virtually copied from a 13071c7c3c6aSMatthew Dillon * backing_object into the first_object, where the 13081c7c3c6aSMatthew Dillon * backing object has no other refs to it, and cannot 13091c7c3c6aSMatthew Dillon * gain any more refs. Instead of a bcopy, we just 13101c7c3c6aSMatthew Dillon * move the page from the backing object to the 13111c7c3c6aSMatthew Dillon * first object. Note that we must mark the page 13121c7c3c6aSMatthew Dillon * dirty in the first object so that it will go out 13131c7c3c6aSMatthew Dillon * to swap when needed. 1314df8bae1dSRodney W. Grimes */ 1315cd8a6fe8SAlan Cox is_first_object_locked = false; 1316e50346b5SAlan Cox if ( 1317de5f6a77SJohn Dyson /* 1318de5f6a77SJohn Dyson * Only one shadow object 1319de5f6a77SJohn Dyson */ 13201e40fe41SJeff Roberson fs.object->shadow_count == 1 && 1321de5f6a77SJohn Dyson /* 1322de5f6a77SJohn Dyson * No COW refs, except us 1323de5f6a77SJohn Dyson */ 13241e40fe41SJeff Roberson fs.object->ref_count == 1 && 1325de5f6a77SJohn Dyson /* 13265929bcfaSPhilippe Charnier * No one else can look this object up 1327de5f6a77SJohn Dyson */ 13281e40fe41SJeff Roberson fs.object->handle == NULL && 1329de5f6a77SJohn Dyson /* 1330de5f6a77SJohn Dyson * No other ways to look the object up 1331de5f6a77SJohn Dyson */ 13321e40fe41SJeff Roberson (fs.object->flags & OBJ_ANON) != 0 && 133389f6b863SAttilio Rao (is_first_object_locked = VM_OBJECT_TRYWLOCK(fs.first_object)) && 1334de5f6a77SJohn Dyson /* 1335de5f6a77SJohn Dyson * We don't chase down the shadow chain 1336de5f6a77SJohn Dyson */ 13371e40fe41SJeff Roberson fs.object == fs.first_object->backing_object && 13381e40fe41SJeff Roberson VM_OBJECT_TRYWLOCK(fs.object)) { 1339fee2a2faSMark Johnston 13403cf3b4e6SJeff Roberson /* 13413cf3b4e6SJeff Roberson * Remove but keep xbusy for replace. fs.m is 13423cf3b4e6SJeff Roberson * moved into fs.first_object and left busy 13433cf3b4e6SJeff Roberson * while fs.first_m is conditionally freed. 13443cf3b4e6SJeff Roberson */ 13453cf3b4e6SJeff Roberson vm_page_remove_xbusy(fs.m); 13463cf3b4e6SJeff Roberson vm_page_replace(fs.m, fs.first_object, 1347bccdea45SAlan Cox fs.first_pindex, fs.first_m); 1348bccdea45SAlan Cox vm_page_dirty(fs.m); 1349dfdf9abdSAlan Cox #if VM_NRESERVLEVEL > 0 1350dfdf9abdSAlan Cox /* 1351dfdf9abdSAlan Cox * Rename the reservation. 1352dfdf9abdSAlan Cox */ 1353dfdf9abdSAlan Cox vm_reserv_rename(fs.m, fs.first_object, 1354dfdf9abdSAlan Cox fs.object, OFF_TO_IDX( 1355dfdf9abdSAlan Cox fs.first_object->backing_object_offset)); 1356dfdf9abdSAlan Cox #endif 13574b3e0665SJeff Roberson VM_OBJECT_WUNLOCK(fs.object); 13581e40fe41SJeff Roberson VM_OBJECT_WUNLOCK(fs.first_object); 1359d98ddc46SAlan Cox fs.first_m = fs.m; 13604866e085SJohn Dyson fs.m = NULL; 136183c9dea1SGleb Smirnoff VM_CNT_INC(v_cow_optim); 1362de5f6a77SJohn Dyson } else { 13631e40fe41SJeff Roberson if (is_first_object_locked) 13641e40fe41SJeff Roberson VM_OBJECT_WUNLOCK(fs.first_object); 1365de5f6a77SJohn Dyson /* 1366de5f6a77SJohn Dyson * Oh, well, lets copy it. 1367de5f6a77SJohn Dyson */ 1368669890eaSAlan Cox pmap_copy_page(fs.m, fs.first_m); 13690012f373SJeff Roberson vm_page_valid(fs.first_m); 13702c2f4413SJeff Roberson if (fs.wired && (fs.fault_flags & 13716a875bf9SKonstantin Belousov VM_FAULT_WIRE) == 0) { 1372d8778512SAlan Cox vm_page_wire(fs.first_m); 13733ae10f74SAttilio Rao vm_page_unwire(fs.m, PQ_INACTIVE); 1374de5f6a77SJohn Dyson } 1375df8bae1dSRodney W. Grimes /* 137658447749SJeff Roberson * Save the cow page to be released after 137758447749SJeff Roberson * pmap_enter is complete. 1378df8bae1dSRodney W. Grimes */ 137958447749SJeff Roberson fs.m_cow = fs.m; 138058447749SJeff Roberson fs.m = NULL; 1381de5f6a77SJohn Dyson } 13821c7c3c6aSMatthew Dillon /* 13831c7c3c6aSMatthew Dillon * fs.object != fs.first_object due to above 13841c7c3c6aSMatthew Dillon * conditional 13851c7c3c6aSMatthew Dillon */ 13864866e085SJohn Dyson vm_object_pip_wakeup(fs.object); 13879f1abe3dSAlan Cox 13889f1abe3dSAlan Cox /* 13899f1abe3dSAlan Cox * We only try to prefault read-only mappings to the 13909f1abe3dSAlan Cox * neighboring pages when this copy-on-write fault is 13919f1abe3dSAlan Cox * a hard fault. In other cases, trying to prefault 13929f1abe3dSAlan Cox * is typically wasted effort. 13939f1abe3dSAlan Cox */ 13949f1abe3dSAlan Cox if (faultcount == 0) 13959f1abe3dSAlan Cox faultcount = 1; 13969f1abe3dSAlan Cox 1397df8bae1dSRodney W. Grimes /* 1398df8bae1dSRodney W. Grimes * Only use the new page below... 1399df8bae1dSRodney W. Grimes */ 14004866e085SJohn Dyson fs.object = fs.first_object; 14014866e085SJohn Dyson fs.pindex = fs.first_pindex; 1402d98ddc46SAlan Cox fs.m = fs.first_m; 140383c9dea1SGleb Smirnoff VM_CNT_INC(v_cow_faults); 14044d34e019SKonstantin Belousov curthread->td_cow++; 14050d94caffSDavid Greenman } else { 14062c2f4413SJeff Roberson fs.prot &= ~VM_PROT_WRITE; 1407df8bae1dSRodney W. Grimes } 1408df8bae1dSRodney W. Grimes } 1409df8bae1dSRodney W. Grimes 1410df8bae1dSRodney W. Grimes /* 14110d94caffSDavid Greenman * We must verify that the maps have not changed since our last 14120d94caffSDavid Greenman * lookup. 1413df8bae1dSRodney W. Grimes */ 141419dc5607STor Egge if (!fs.lookup_still_valid) { 1415*fcb04758SJeff Roberson result = vm_fault_relookup(&fs); 1416df8bae1dSRodney W. Grimes if (result != KERN_SUCCESS) { 14171e40fe41SJeff Roberson fault_deallocate(&fs); 1418*fcb04758SJeff Roberson if (result == KERN_RESTART) 141919dc5607STor Egge goto RetryFault; 1420df8bae1dSRodney W. Grimes return (result); 1421df8bae1dSRodney W. Grimes } 142219dc5607STor Egge } 14231e40fe41SJeff Roberson VM_OBJECT_ASSERT_UNLOCKED(fs.object); 1424381b7242SAlan Cox 1425d2bf64c3SKonstantin Belousov /* 1426381b7242SAlan Cox * If the page was filled by a pager, save the virtual address that 1427381b7242SAlan Cox * should be faulted on next under a sequential access pattern to the 1428381b7242SAlan Cox * map entry. A read lock on the map suffices to update this address 1429381b7242SAlan Cox * safely. 1430d2bf64c3SKonstantin Belousov */ 14315758fe71SAlan Cox if (hardfault) 1432381b7242SAlan Cox fs.entry->next_read = vaddr + ptoa(ahead) + PAGE_SIZE; 1433d2bf64c3SKonstantin Belousov 14344221e284SAlan Cox /* 143578cfe1f7SAlan Cox * Page must be completely valid or it is not fit to 14364221e284SAlan Cox * map into user space. vm_pager_get_pages() ensures this. 14374221e284SAlan Cox */ 14381e40fe41SJeff Roberson vm_page_assert_xbusied(fs.m); 14390012f373SJeff Roberson KASSERT(vm_page_all_valid(fs.m), 144078cfe1f7SAlan Cox ("vm_fault: page %p partially invalid", fs.m)); 14411e40fe41SJeff Roberson 14422c2f4413SJeff Roberson vm_fault_dirty(&fs, fs.m); 1443cbfbaad8SAlan Cox 144486735996SAlan Cox /* 144586735996SAlan Cox * Put this page into the physical map. We had to do the unlock above 144686735996SAlan Cox * because pmap_enter() may sleep. We don't put the page 144786735996SAlan Cox * back on the active queue until later so that the pageout daemon 144886735996SAlan Cox * won't find it (yet). 144986735996SAlan Cox */ 14502c2f4413SJeff Roberson pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, 14512c2f4413SJeff Roberson fs.fault_type | (fs.wired ? PMAP_ENTER_WIRED : 0), 0); 14522c2f4413SJeff Roberson if (faultcount != 1 && (fs.fault_flags & VM_FAULT_WIRE) == 0 && 14532c2f4413SJeff Roberson fs.wired == 0) 1454b0cd2017SGleb Smirnoff vm_fault_prefault(&fs, vaddr, 1455b0cd2017SGleb Smirnoff faultcount > 0 ? behind : PFBAK, 1456a7163bb9SKonstantin Belousov faultcount > 0 ? ahead : PFFOR, false); 1457ff97964aSJohn Dyson 1458df8bae1dSRodney W. Grimes /* 14590d94caffSDavid Greenman * If the page is not wired down, then put it where the pageout daemon 14600d94caffSDavid Greenman * can find it. 1461df8bae1dSRodney W. Grimes */ 14622c2f4413SJeff Roberson if ((fs.fault_flags & VM_FAULT_WIRE) != 0) 14634866e085SJohn Dyson vm_page_wire(fs.m); 14649f5632e6SMark Johnston else 14654866e085SJohn Dyson vm_page_activate(fs.m); 14662c2f4413SJeff Roberson if (fs.m_hold != NULL) { 14672c2f4413SJeff Roberson (*fs.m_hold) = fs.m; 1468eeacb3b0SMark Johnston vm_page_wire(fs.m); 1469acd11c74SAlan Cox } 1470c7aebda8SAttilio Rao vm_page_xunbusy(fs.m); 14714bf95d00SJeff Roberson fs.m = NULL; 1472eeec6babSJohn Baldwin 1473eebf3286SAlan Cox /* 1474eebf3286SAlan Cox * Unlock everything, and return 1475eebf3286SAlan Cox */ 14764b3e0665SJeff Roberson fault_deallocate(&fs); 1477b3a01bdfSAndrey Zonov if (hardfault) { 147883c9dea1SGleb Smirnoff VM_CNT_INC(v_io_faults); 14791c4bcd05SJeff Roberson curthread->td_ru.ru_majflt++; 1480ae34b6ffSEdward Tomasz Napierala #ifdef RACCT 1481ae34b6ffSEdward Tomasz Napierala if (racct_enable && fs.object->type == OBJT_VNODE) { 1482ae34b6ffSEdward Tomasz Napierala PROC_LOCK(curproc); 14832c2f4413SJeff Roberson if ((fs.fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) { 1484ae34b6ffSEdward Tomasz Napierala racct_add_force(curproc, RACCT_WRITEBPS, 1485ae34b6ffSEdward Tomasz Napierala PAGE_SIZE + behind * PAGE_SIZE); 1486ae34b6ffSEdward Tomasz Napierala racct_add_force(curproc, RACCT_WRITEIOPS, 1); 1487ae34b6ffSEdward Tomasz Napierala } else { 1488ae34b6ffSEdward Tomasz Napierala racct_add_force(curproc, RACCT_READBPS, 1489ae34b6ffSEdward Tomasz Napierala PAGE_SIZE + ahead * PAGE_SIZE); 1490ae34b6ffSEdward Tomasz Napierala racct_add_force(curproc, RACCT_READIOPS, 1); 1491ae34b6ffSEdward Tomasz Napierala } 1492ae34b6ffSEdward Tomasz Napierala PROC_UNLOCK(curproc); 1493ae34b6ffSEdward Tomasz Napierala } 1494ae34b6ffSEdward Tomasz Napierala #endif 1495b3a01bdfSAndrey Zonov } else 14961c4bcd05SJeff Roberson curthread->td_ru.ru_minflt++; 1497df8bae1dSRodney W. Grimes 1498df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 1499df8bae1dSRodney W. Grimes } 1500df8bae1dSRodney W. Grimes 1501df8bae1dSRodney W. Grimes /* 1502a8b0f100SAlan Cox * Speed up the reclamation of pages that precede the faulting pindex within 1503a8b0f100SAlan Cox * the first object of the shadow chain. Essentially, perform the equivalent 1504a8b0f100SAlan Cox * to madvise(..., MADV_DONTNEED) on a large cluster of pages that precedes 1505a8b0f100SAlan Cox * the faulting pindex by the cluster size when the pages read by vm_fault() 1506a8b0f100SAlan Cox * cross a cluster-size boundary. The cluster size is the greater of the 1507a8b0f100SAlan Cox * smallest superpage size and VM_FAULT_DONTNEED_MIN. 1508a8b0f100SAlan Cox * 1509a8b0f100SAlan Cox * When "fs->first_object" is a shadow object, the pages in the backing object 1510a8b0f100SAlan Cox * that precede the faulting pindex are deactivated by vm_fault(). So, this 1511a8b0f100SAlan Cox * function must only be concerned with pages in the first object. 151213458803SAlan Cox */ 151313458803SAlan Cox static void 1514a8b0f100SAlan Cox vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr, int ahead) 151513458803SAlan Cox { 1516a8b0f100SAlan Cox vm_map_entry_t entry; 151713458803SAlan Cox vm_object_t first_object, object; 1518a8b0f100SAlan Cox vm_offset_t end, start; 1519a8b0f100SAlan Cox vm_page_t m, m_next; 1520a8b0f100SAlan Cox vm_pindex_t pend, pstart; 1521a8b0f100SAlan Cox vm_size_t size; 152213458803SAlan Cox 152313458803SAlan Cox object = fs->object; 15241e40fe41SJeff Roberson VM_OBJECT_ASSERT_UNLOCKED(object); 152513458803SAlan Cox first_object = fs->first_object; 1526a8b0f100SAlan Cox /* Neither fictitious nor unmanaged pages can be reclaimed. */ 152728634820SAlan Cox if ((first_object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0) { 15281e40fe41SJeff Roberson VM_OBJECT_RLOCK(first_object); 1529a8b0f100SAlan Cox size = VM_FAULT_DONTNEED_MIN; 1530a8b0f100SAlan Cox if (MAXPAGESIZES > 1 && size < pagesizes[1]) 1531a8b0f100SAlan Cox size = pagesizes[1]; 1532a8b0f100SAlan Cox end = rounddown2(vaddr, size); 1533a8b0f100SAlan Cox if (vaddr - end >= size - PAGE_SIZE - ptoa(ahead) && 1534a8b0f100SAlan Cox (entry = fs->entry)->start < end) { 1535a8b0f100SAlan Cox if (end - entry->start < size) 1536a8b0f100SAlan Cox start = entry->start; 153713458803SAlan Cox else 1538a8b0f100SAlan Cox start = end - size; 1539a8b0f100SAlan Cox pmap_advise(fs->map->pmap, start, end, MADV_DONTNEED); 1540a8b0f100SAlan Cox pstart = OFF_TO_IDX(entry->offset) + atop(start - 1541a8b0f100SAlan Cox entry->start); 1542a8b0f100SAlan Cox m_next = vm_page_find_least(first_object, pstart); 1543a8b0f100SAlan Cox pend = OFF_TO_IDX(entry->offset) + atop(end - 1544a8b0f100SAlan Cox entry->start); 1545a8b0f100SAlan Cox while ((m = m_next) != NULL && m->pindex < pend) { 1546a8b0f100SAlan Cox m_next = TAILQ_NEXT(m, listq); 15470012f373SJeff Roberson if (!vm_page_all_valid(m) || 1548a8b0f100SAlan Cox vm_page_busied(m)) 154913458803SAlan Cox continue; 1550d8015db3SAlan Cox 1551d8015db3SAlan Cox /* 1552d8015db3SAlan Cox * Don't clear PGA_REFERENCED, since it would 1553d8015db3SAlan Cox * likely represent a reference by a different 1554d8015db3SAlan Cox * process. 1555d8015db3SAlan Cox * 1556d8015db3SAlan Cox * Typically, at this point, prefetched pages 1557d8015db3SAlan Cox * are still in the inactive queue. Only 1558d8015db3SAlan Cox * pages that triggered page faults are in the 15599f5632e6SMark Johnston * active queue. The test for whether the page 15609f5632e6SMark Johnston * is in the inactive queue is racy; in the 15619f5632e6SMark Johnston * worst case we will requeue the page 15629f5632e6SMark Johnston * unnecessarily. 1563d8015db3SAlan Cox */ 15640eb50f9cSMark Johnston if (!vm_page_inactive(m)) 1565d8015db3SAlan Cox vm_page_deactivate(m); 156613458803SAlan Cox } 156713458803SAlan Cox } 15681e40fe41SJeff Roberson VM_OBJECT_RUNLOCK(first_object); 1569a8b0f100SAlan Cox } 157013458803SAlan Cox } 157113458803SAlan Cox 157213458803SAlan Cox /* 1573566526a9SAlan Cox * vm_fault_prefault provides a quick way of clustering 1574566526a9SAlan Cox * pagefaults into a processes address space. It is a "cousin" 1575566526a9SAlan Cox * of vm_map_pmap_enter, except it runs at page fault time instead 1576566526a9SAlan Cox * of mmap time. 1577566526a9SAlan Cox */ 1578566526a9SAlan Cox static void 157963281952SAlan Cox vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra, 1580a7163bb9SKonstantin Belousov int backward, int forward, bool obj_locked) 1581566526a9SAlan Cox { 158263281952SAlan Cox pmap_t pmap; 158363281952SAlan Cox vm_map_entry_t entry; 158463281952SAlan Cox vm_object_t backing_object, lobject; 1585566526a9SAlan Cox vm_offset_t addr, starta; 1586566526a9SAlan Cox vm_pindex_t pindex; 15872053c127SStephan Uphoff vm_page_t m; 1588b0cd2017SGleb Smirnoff int i; 1589566526a9SAlan Cox 159063281952SAlan Cox pmap = fs->map->pmap; 1591950d5f7aSAlan Cox if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace)) 1592566526a9SAlan Cox return; 1593566526a9SAlan Cox 159463281952SAlan Cox entry = fs->entry; 1595566526a9SAlan Cox 159663cdcaaeSKonstantin Belousov if (addra < backward * PAGE_SIZE) { 1597566526a9SAlan Cox starta = entry->start; 159863cdcaaeSKonstantin Belousov } else { 159963cdcaaeSKonstantin Belousov starta = addra - backward * PAGE_SIZE; 160063cdcaaeSKonstantin Belousov if (starta < entry->start) 160163cdcaaeSKonstantin Belousov starta = entry->start; 1602566526a9SAlan Cox } 1603566526a9SAlan Cox 160463281952SAlan Cox /* 160563281952SAlan Cox * Generate the sequence of virtual addresses that are candidates for 160663281952SAlan Cox * prefaulting in an outward spiral from the faulting virtual address, 160763281952SAlan Cox * "addra". Specifically, the sequence is "addra - PAGE_SIZE", "addra 160863281952SAlan Cox * + PAGE_SIZE", "addra - 2 * PAGE_SIZE", "addra + 2 * PAGE_SIZE", ... 160963281952SAlan Cox * If the candidate address doesn't have a backing physical page, then 161063281952SAlan Cox * the loop immediately terminates. 161163281952SAlan Cox */ 161263281952SAlan Cox for (i = 0; i < 2 * imax(backward, forward); i++) { 161363281952SAlan Cox addr = addra + ((i >> 1) + 1) * ((i & 1) == 0 ? -PAGE_SIZE : 161463281952SAlan Cox PAGE_SIZE); 161563281952SAlan Cox if (addr > addra + forward * PAGE_SIZE) 1616566526a9SAlan Cox addr = 0; 1617566526a9SAlan Cox 1618566526a9SAlan Cox if (addr < starta || addr >= entry->end) 1619566526a9SAlan Cox continue; 1620566526a9SAlan Cox 1621566526a9SAlan Cox if (!pmap_is_prefaultable(pmap, addr)) 1622566526a9SAlan Cox continue; 1623566526a9SAlan Cox 1624566526a9SAlan Cox pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT; 162563281952SAlan Cox lobject = entry->object.vm_object; 1626a7163bb9SKonstantin Belousov if (!obj_locked) 1627c141ae7fSAlan Cox VM_OBJECT_RLOCK(lobject); 1628566526a9SAlan Cox while ((m = vm_page_lookup(lobject, pindex)) == NULL && 1629566526a9SAlan Cox lobject->type == OBJT_DEFAULT && 1630566526a9SAlan Cox (backing_object = lobject->backing_object) != NULL) { 163136930fc9SAlan Cox KASSERT((lobject->backing_object_offset & PAGE_MASK) == 163236930fc9SAlan Cox 0, ("vm_fault_prefault: unaligned object offset")); 1633566526a9SAlan Cox pindex += lobject->backing_object_offset >> PAGE_SHIFT; 1634c141ae7fSAlan Cox VM_OBJECT_RLOCK(backing_object); 1635a7163bb9SKonstantin Belousov if (!obj_locked || lobject != entry->object.vm_object) 1636c141ae7fSAlan Cox VM_OBJECT_RUNLOCK(lobject); 1637566526a9SAlan Cox lobject = backing_object; 1638566526a9SAlan Cox } 1639cbfbaad8SAlan Cox if (m == NULL) { 1640a7163bb9SKonstantin Belousov if (!obj_locked || lobject != entry->object.vm_object) 1641c141ae7fSAlan Cox VM_OBJECT_RUNLOCK(lobject); 1642566526a9SAlan Cox break; 1643cbfbaad8SAlan Cox } 16440012f373SJeff Roberson if (vm_page_all_valid(m) && 16453c4a2440SAlan Cox (m->flags & PG_FICTITIOUS) == 0) 16467bfda801SAlan Cox pmap_enter_quick(pmap, addr, m, entry->protection); 1647a7163bb9SKonstantin Belousov if (!obj_locked || lobject != entry->object.vm_object) 1648c141ae7fSAlan Cox VM_OBJECT_RUNLOCK(lobject); 1649566526a9SAlan Cox } 1650566526a9SAlan Cox } 1651566526a9SAlan Cox 1652566526a9SAlan Cox /* 165382de724fSAlan Cox * Hold each of the physical pages that are mapped by the specified range of 165482de724fSAlan Cox * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid 165582de724fSAlan Cox * and allow the specified types of access, "prot". If all of the implied 165682de724fSAlan Cox * pages are successfully held, then the number of held pages is returned 165782de724fSAlan Cox * together with pointers to those pages in the array "ma". However, if any 165882de724fSAlan Cox * of the pages cannot be held, -1 is returned. 165982de724fSAlan Cox */ 166082de724fSAlan Cox int 166182de724fSAlan Cox vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len, 166282de724fSAlan Cox vm_prot_t prot, vm_page_t *ma, int max_count) 166382de724fSAlan Cox { 166482de724fSAlan Cox vm_offset_t end, va; 166582de724fSAlan Cox vm_page_t *mp; 16667e14088dSKonstantin Belousov int count; 166782de724fSAlan Cox boolean_t pmap_failed; 166882de724fSAlan Cox 1669af32c419SKonstantin Belousov if (len == 0) 1670af32c419SKonstantin Belousov return (0); 167182de724fSAlan Cox end = round_page(addr + len); 167282de724fSAlan Cox addr = trunc_page(addr); 167382de724fSAlan Cox 167482de724fSAlan Cox /* 167582de724fSAlan Cox * Check for illegal addresses. 167682de724fSAlan Cox */ 167782de724fSAlan Cox if (addr < vm_map_min(map) || addr > end || end > vm_map_max(map)) 167882de724fSAlan Cox return (-1); 167982de724fSAlan Cox 16807e14088dSKonstantin Belousov if (atop(end - addr) > max_count) 168182de724fSAlan Cox panic("vm_fault_quick_hold_pages: count > max_count"); 16827e14088dSKonstantin Belousov count = atop(end - addr); 168382de724fSAlan Cox 168482de724fSAlan Cox /* 168582de724fSAlan Cox * Most likely, the physical pages are resident in the pmap, so it is 168682de724fSAlan Cox * faster to try pmap_extract_and_hold() first. 168782de724fSAlan Cox */ 168882de724fSAlan Cox pmap_failed = FALSE; 168982de724fSAlan Cox for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) { 169082de724fSAlan Cox *mp = pmap_extract_and_hold(map->pmap, va, prot); 169182de724fSAlan Cox if (*mp == NULL) 169282de724fSAlan Cox pmap_failed = TRUE; 169382de724fSAlan Cox else if ((prot & VM_PROT_WRITE) != 0 && 1694a5dbab54SAlan Cox (*mp)->dirty != VM_PAGE_BITS_ALL) { 169582de724fSAlan Cox /* 169682de724fSAlan Cox * Explicitly dirty the physical page. Otherwise, the 169782de724fSAlan Cox * caller's changes may go unnoticed because they are 169882de724fSAlan Cox * performed through an unmanaged mapping or by a DMA 169982de724fSAlan Cox * operation. 17003c76db4cSAlan Cox * 1701abb9b935SKonstantin Belousov * The object lock is not held here. 1702abb9b935SKonstantin Belousov * See vm_page_clear_dirty_mask(). 170382de724fSAlan Cox */ 17043c76db4cSAlan Cox vm_page_dirty(*mp); 170582de724fSAlan Cox } 170682de724fSAlan Cox } 170782de724fSAlan Cox if (pmap_failed) { 170882de724fSAlan Cox /* 170982de724fSAlan Cox * One or more pages could not be held by the pmap. Either no 171082de724fSAlan Cox * page was mapped at the specified virtual address or that 171182de724fSAlan Cox * mapping had insufficient permissions. Attempt to fault in 171282de724fSAlan Cox * and hold these pages. 17138ec533d3SKonstantin Belousov * 17148ec533d3SKonstantin Belousov * If vm_fault_disable_pagefaults() was called, 17158ec533d3SKonstantin Belousov * i.e., TDP_NOFAULTING is set, we must not sleep nor 17168ec533d3SKonstantin Belousov * acquire MD VM locks, which means we must not call 1717df08823dSKonstantin Belousov * vm_fault(). Some (out of tree) callers mark 17188ec533d3SKonstantin Belousov * too wide a code area with vm_fault_disable_pagefaults() 17198ec533d3SKonstantin Belousov * already, use the VM_PROT_QUICK_NOFAULT flag to request 17208ec533d3SKonstantin Belousov * the proper behaviour explicitly. 172182de724fSAlan Cox */ 17228ec533d3SKonstantin Belousov if ((prot & VM_PROT_QUICK_NOFAULT) != 0 && 17238ec533d3SKonstantin Belousov (curthread->td_pflags & TDP_NOFAULTING) != 0) 17248ec533d3SKonstantin Belousov goto error; 172582de724fSAlan Cox for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) 1726df08823dSKonstantin Belousov if (*mp == NULL && vm_fault(map, va, prot, 172782de724fSAlan Cox VM_FAULT_NORMAL, mp) != KERN_SUCCESS) 172882de724fSAlan Cox goto error; 172982de724fSAlan Cox } 173082de724fSAlan Cox return (count); 173182de724fSAlan Cox error: 173282de724fSAlan Cox for (mp = ma; mp < ma + count; mp++) 1733fee2a2faSMark Johnston if (*mp != NULL) 1734fee2a2faSMark Johnston vm_page_unwire(*mp, PQ_INACTIVE); 173582de724fSAlan Cox return (-1); 173682de724fSAlan Cox } 173782de724fSAlan Cox 173882de724fSAlan Cox /* 1739df8bae1dSRodney W. Grimes * Routine: 1740df8bae1dSRodney W. Grimes * vm_fault_copy_entry 1741df8bae1dSRodney W. Grimes * Function: 1742210a6886SKonstantin Belousov * Create new shadow object backing dst_entry with private copy of 1743210a6886SKonstantin Belousov * all underlying pages. When src_entry is equal to dst_entry, 1744210a6886SKonstantin Belousov * function implements COW for wired-down map entry. Otherwise, 1745210a6886SKonstantin Belousov * it forks wired entry into dst_map. 1746df8bae1dSRodney W. Grimes * 1747df8bae1dSRodney W. Grimes * In/out conditions: 1748df8bae1dSRodney W. Grimes * The source and destination maps must be locked for write. 1749df8bae1dSRodney W. Grimes * The source map entry must be wired down (or be a sharing map 1750df8bae1dSRodney W. Grimes * entry corresponding to a main map entry that is wired down). 1751df8bae1dSRodney W. Grimes */ 175226f9a767SRodney W. Grimes void 1753121fd461SKonstantin Belousov vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map, 1754121fd461SKonstantin Belousov vm_map_entry_t dst_entry, vm_map_entry_t src_entry, 1755121fd461SKonstantin Belousov vm_ooffset_t *fork_charge) 1756df8bae1dSRodney W. Grimes { 1757210a6886SKonstantin Belousov vm_object_t backing_object, dst_object, object, src_object; 17587afab86cSAlan Cox vm_pindex_t dst_pindex, pindex, src_pindex; 1759210a6886SKonstantin Belousov vm_prot_t access, prot; 1760df8bae1dSRodney W. Grimes vm_offset_t vaddr; 1761df8bae1dSRodney W. Grimes vm_page_t dst_m; 1762df8bae1dSRodney W. Grimes vm_page_t src_m; 17634c74acf7SKonstantin Belousov boolean_t upgrade; 1764df8bae1dSRodney W. Grimes 1765df8bae1dSRodney W. Grimes #ifdef lint 1766df8bae1dSRodney W. Grimes src_map++; 17670d94caffSDavid Greenman #endif /* lint */ 1768df8bae1dSRodney W. Grimes 1769210a6886SKonstantin Belousov upgrade = src_entry == dst_entry; 17700973283dSKonstantin Belousov access = prot = dst_entry->protection; 1771210a6886SKonstantin Belousov 1772df8bae1dSRodney W. Grimes src_object = src_entry->object.vm_object; 17737afab86cSAlan Cox src_pindex = OFF_TO_IDX(src_entry->offset); 1774df8bae1dSRodney W. Grimes 17750973283dSKonstantin Belousov if (upgrade && (dst_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) { 17760973283dSKonstantin Belousov dst_object = src_object; 17770973283dSKonstantin Belousov vm_object_reference(dst_object); 17780973283dSKonstantin Belousov } else { 1779df8bae1dSRodney W. Grimes /* 178067388836SKonstantin Belousov * Create the top-level object for the destination entry. 178167388836SKonstantin Belousov * Doesn't actually shadow anything - we copy the pages 178267388836SKonstantin Belousov * directly. 1783df8bae1dSRodney W. Grimes */ 178467388836SKonstantin Belousov dst_object = vm_object_allocate_anon(atop(dst_entry->end - 178567388836SKonstantin Belousov dst_entry->start), NULL, NULL, 0); 1786f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 1787f8a47341SAlan Cox dst_object->flags |= OBJ_COLORED; 1788f8a47341SAlan Cox dst_object->pg_color = atop(dst_entry->start); 1789f8a47341SAlan Cox #endif 1790a60d3db1SKonstantin Belousov dst_object->domain = src_object->domain; 1791a60d3db1SKonstantin Belousov dst_object->charge = dst_entry->end - dst_entry->start; 17920973283dSKonstantin Belousov } 1793df8bae1dSRodney W. Grimes 179489f6b863SAttilio Rao VM_OBJECT_WLOCK(dst_object); 1795210a6886SKonstantin Belousov KASSERT(upgrade || dst_entry->object.vm_object == NULL, 1796121fd461SKonstantin Belousov ("vm_fault_copy_entry: vm_object not NULL")); 17970973283dSKonstantin Belousov if (src_object != dst_object) { 1798df8bae1dSRodney W. Grimes dst_entry->object.vm_object = dst_object; 1799df8bae1dSRodney W. Grimes dst_entry->offset = 0; 180078022527SKonstantin Belousov dst_entry->eflags &= ~MAP_ENTRY_VN_EXEC; 18010973283dSKonstantin Belousov } 1802210a6886SKonstantin Belousov if (fork_charge != NULL) { 1803ef694c1aSEdward Tomasz Napierala KASSERT(dst_entry->cred == NULL, 1804121fd461SKonstantin Belousov ("vm_fault_copy_entry: leaked swp charge")); 1805ef694c1aSEdward Tomasz Napierala dst_object->cred = curthread->td_ucred; 1806ef694c1aSEdward Tomasz Napierala crhold(dst_object->cred); 1807121fd461SKonstantin Belousov *fork_charge += dst_object->charge; 18089f25ab83SKonstantin Belousov } else if ((dst_object->type == OBJT_DEFAULT || 18099f25ab83SKonstantin Belousov dst_object->type == OBJT_SWAP) && 18109f25ab83SKonstantin Belousov dst_object->cred == NULL) { 18110973283dSKonstantin Belousov KASSERT(dst_entry->cred != NULL, ("no cred for entry %p", 18120973283dSKonstantin Belousov dst_entry)); 1813ef694c1aSEdward Tomasz Napierala dst_object->cred = dst_entry->cred; 1814ef694c1aSEdward Tomasz Napierala dst_entry->cred = NULL; 1815210a6886SKonstantin Belousov } 18160973283dSKonstantin Belousov 1817210a6886SKonstantin Belousov /* 1818210a6886SKonstantin Belousov * If not an upgrade, then enter the mappings in the pmap as 1819210a6886SKonstantin Belousov * read and/or execute accesses. Otherwise, enter them as 1820210a6886SKonstantin Belousov * write accesses. 1821210a6886SKonstantin Belousov * 1822210a6886SKonstantin Belousov * A writeable large page mapping is only created if all of 1823210a6886SKonstantin Belousov * the constituent small page mappings are modified. Marking 1824210a6886SKonstantin Belousov * PTEs as modified on inception allows promotion to happen 1825210a6886SKonstantin Belousov * without taking potentially large number of soft faults. 1826210a6886SKonstantin Belousov */ 1827210a6886SKonstantin Belousov if (!upgrade) 1828210a6886SKonstantin Belousov access &= ~VM_PROT_WRITE; 1829df8bae1dSRodney W. Grimes 1830df8bae1dSRodney W. Grimes /* 1831ef45823eSKonstantin Belousov * Loop through all of the virtual pages within the entry's 1832ef45823eSKonstantin Belousov * range, copying each page from the source object to the 1833ef45823eSKonstantin Belousov * destination object. Since the source is wired, those pages 1834ef45823eSKonstantin Belousov * must exist. In contrast, the destination is pageable. 18356939b4d3SMark Johnston * Since the destination object doesn't share any backing storage 1836ef45823eSKonstantin Belousov * with the source object, all of its pages must be dirtied, 1837ef45823eSKonstantin Belousov * regardless of whether they can be written. 1838df8bae1dSRodney W. Grimes */ 18397afab86cSAlan Cox for (vaddr = dst_entry->start, dst_pindex = 0; 1840df8bae1dSRodney W. Grimes vaddr < dst_entry->end; 18417afab86cSAlan Cox vaddr += PAGE_SIZE, dst_pindex++) { 18420973283dSKonstantin Belousov again: 1843df8bae1dSRodney W. Grimes /* 1844df8bae1dSRodney W. Grimes * Find the page in the source object, and copy it in. 18454c74acf7SKonstantin Belousov * Because the source is wired down, the page will be 18464c74acf7SKonstantin Belousov * in memory. 1847df8bae1dSRodney W. Grimes */ 18480973283dSKonstantin Belousov if (src_object != dst_object) 184983b375eaSAttilio Rao VM_OBJECT_RLOCK(src_object); 1850c5b65a67SAlan Cox object = src_object; 18517afab86cSAlan Cox pindex = src_pindex + dst_pindex; 18527afab86cSAlan Cox while ((src_m = vm_page_lookup(object, pindex)) == NULL && 1853c5b65a67SAlan Cox (backing_object = object->backing_object) != NULL) { 1854c5b65a67SAlan Cox /* 18554c74acf7SKonstantin Belousov * Unless the source mapping is read-only or 18564c74acf7SKonstantin Belousov * it is presently being upgraded from 18574c74acf7SKonstantin Belousov * read-only, the first object in the shadow 18584c74acf7SKonstantin Belousov * chain should provide all of the pages. In 18594c74acf7SKonstantin Belousov * other words, this loop body should never be 18604c74acf7SKonstantin Belousov * executed when the source mapping is already 18614c74acf7SKonstantin Belousov * read/write. 1862c5b65a67SAlan Cox */ 18634c74acf7SKonstantin Belousov KASSERT((src_entry->protection & VM_PROT_WRITE) == 0 || 18644c74acf7SKonstantin Belousov upgrade, 18654c74acf7SKonstantin Belousov ("vm_fault_copy_entry: main object missing page")); 18664c74acf7SKonstantin Belousov 186783b375eaSAttilio Rao VM_OBJECT_RLOCK(backing_object); 1868c5b65a67SAlan Cox pindex += OFF_TO_IDX(object->backing_object_offset); 18690973283dSKonstantin Belousov if (object != dst_object) 187083b375eaSAttilio Rao VM_OBJECT_RUNLOCK(object); 1871c5b65a67SAlan Cox object = backing_object; 1872c5b65a67SAlan Cox } 18734c74acf7SKonstantin Belousov KASSERT(src_m != NULL, ("vm_fault_copy_entry: page missing")); 18740973283dSKonstantin Belousov 18750973283dSKonstantin Belousov if (object != dst_object) { 18760973283dSKonstantin Belousov /* 18770973283dSKonstantin Belousov * Allocate a page in the destination object. 18780973283dSKonstantin Belousov */ 18792602a2eaSKonstantin Belousov dst_m = vm_page_alloc(dst_object, (src_object == 18802602a2eaSKonstantin Belousov dst_object ? src_pindex : 0) + dst_pindex, 18812602a2eaSKonstantin Belousov VM_ALLOC_NORMAL); 18820973283dSKonstantin Belousov if (dst_m == NULL) { 18830973283dSKonstantin Belousov VM_OBJECT_WUNLOCK(dst_object); 18840973283dSKonstantin Belousov VM_OBJECT_RUNLOCK(object); 18852c0f13aaSKonstantin Belousov vm_wait(dst_object); 1886c8f780e3SKonstantin Belousov VM_OBJECT_WLOCK(dst_object); 18870973283dSKonstantin Belousov goto again; 18880973283dSKonstantin Belousov } 1889669890eaSAlan Cox pmap_copy_page(src_m, dst_m); 189083b375eaSAttilio Rao VM_OBJECT_RUNLOCK(object); 189145d72c7dSKonstantin Belousov dst_m->dirty = dst_m->valid = src_m->valid; 18920973283dSKonstantin Belousov } else { 18930973283dSKonstantin Belousov dst_m = src_m; 189463e97555SJeff Roberson if (vm_page_busy_acquire(dst_m, VM_ALLOC_WAITFAIL) == 0) 18950973283dSKonstantin Belousov goto again; 189663e97555SJeff Roberson if (dst_m->pindex >= dst_object->size) { 1897c62637d6SKonstantin Belousov /* 1898c62637d6SKonstantin Belousov * We are upgrading. Index can occur 1899c62637d6SKonstantin Belousov * out of bounds if the object type is 1900c62637d6SKonstantin Belousov * vnode and the file was truncated. 1901c62637d6SKonstantin Belousov */ 190263e97555SJeff Roberson vm_page_xunbusy(dst_m); 1903c62637d6SKonstantin Belousov break; 190463e97555SJeff Roberson } 19050973283dSKonstantin Belousov } 190689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(dst_object); 1907df8bae1dSRodney W. Grimes 1908df8bae1dSRodney W. Grimes /* 1909210a6886SKonstantin Belousov * Enter it in the pmap. If a wired, copy-on-write 1910210a6886SKonstantin Belousov * mapping is being replaced by a write-enabled 1911210a6886SKonstantin Belousov * mapping, then wire that new mapping. 191245d72c7dSKonstantin Belousov * 191345d72c7dSKonstantin Belousov * The page can be invalid if the user called 191445d72c7dSKonstantin Belousov * msync(MS_INVALIDATE) or truncated the backing vnode 191545d72c7dSKonstantin Belousov * or shared memory object. In this case, do not 191645d72c7dSKonstantin Belousov * insert it into pmap, but still do the copy so that 191745d72c7dSKonstantin Belousov * all copies of the wired map entry have similar 191845d72c7dSKonstantin Belousov * backing pages. 1919df8bae1dSRodney W. Grimes */ 19200012f373SJeff Roberson if (vm_page_all_valid(dst_m)) { 192139ffa8c1SKonstantin Belousov pmap_enter(dst_map->pmap, vaddr, dst_m, prot, 192239ffa8c1SKonstantin Belousov access | (upgrade ? PMAP_ENTER_WIRED : 0), 0); 192345d72c7dSKonstantin Belousov } 1924df8bae1dSRodney W. Grimes 1925df8bae1dSRodney W. Grimes /* 1926df8bae1dSRodney W. Grimes * Mark it no longer busy, and put it on the active list. 1927df8bae1dSRodney W. Grimes */ 192889f6b863SAttilio Rao VM_OBJECT_WLOCK(dst_object); 19292965a453SKip Macy 1930210a6886SKonstantin Belousov if (upgrade) { 19310973283dSKonstantin Belousov if (src_m != dst_m) { 19323ae10f74SAttilio Rao vm_page_unwire(src_m, PQ_INACTIVE); 1933210a6886SKonstantin Belousov vm_page_wire(dst_m); 19342965a453SKip Macy } else { 1935d842aa51SMark Johnston KASSERT(vm_page_wired(dst_m), 19360973283dSKonstantin Belousov ("dst_m %p is not wired", dst_m)); 19370973283dSKonstantin Belousov } 19380973283dSKonstantin Belousov } else { 1939df8bae1dSRodney W. Grimes vm_page_activate(dst_m); 19402965a453SKip Macy } 1941c7aebda8SAttilio Rao vm_page_xunbusy(dst_m); 1942df8bae1dSRodney W. Grimes } 194389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(dst_object); 1944210a6886SKonstantin Belousov if (upgrade) { 1945210a6886SKonstantin Belousov dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY); 1946210a6886SKonstantin Belousov vm_object_deallocate(src_object); 1947210a6886SKonstantin Belousov } 1948df8bae1dSRodney W. Grimes } 194926f9a767SRodney W. Grimes 19505730afc9SAlan Cox /* 19515730afc9SAlan Cox * Block entry into the machine-independent layer's page fault handler by 19525730afc9SAlan Cox * the calling thread. Subsequent calls to vm_fault() by that thread will 19535730afc9SAlan Cox * return KERN_PROTECTION_FAILURE. Enable machine-dependent handling of 19545730afc9SAlan Cox * spurious page faults. 19555730afc9SAlan Cox */ 19562801687dSKonstantin Belousov int 19572801687dSKonstantin Belousov vm_fault_disable_pagefaults(void) 19582801687dSKonstantin Belousov { 19592801687dSKonstantin Belousov 19605730afc9SAlan Cox return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR)); 19612801687dSKonstantin Belousov } 19622801687dSKonstantin Belousov 19632801687dSKonstantin Belousov void 19642801687dSKonstantin Belousov vm_fault_enable_pagefaults(int save) 19652801687dSKonstantin Belousov { 19662801687dSKonstantin Belousov 19672801687dSKonstantin Belousov curthread_pflags_restore(save); 19682801687dSKonstantin Belousov } 1969