160727d8bSWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 426f9a767SRodney W. Grimes * Copyright (c) 1994 John S. Dyson 526f9a767SRodney W. Grimes * All rights reserved. 626f9a767SRodney W. Grimes * Copyright (c) 1994 David Greenman 726f9a767SRodney W. Grimes * All rights reserved. 826f9a767SRodney W. Grimes * 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 11df8bae1dSRodney W. Grimes * The Mach Operating System project at Carnegie-Mellon University. 12df8bae1dSRodney W. Grimes * 13df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 14df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 15df8bae1dSRodney W. Grimes * are met: 16df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 17df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 18df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 19df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 20df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 21df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 225929bcfaSPhilippe Charnier * must display the following acknowledgement: 23df8bae1dSRodney W. Grimes * This product includes software developed by the University of 24df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 25df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 26df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 27df8bae1dSRodney W. Grimes * without specific prior written permission. 28df8bae1dSRodney W. Grimes * 29df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39df8bae1dSRodney W. Grimes * SUCH DAMAGE. 40df8bae1dSRodney W. Grimes * 413c4dd356SDavid Greenman * from: @(#)vm_fault.c 8.4 (Berkeley) 1/12/94 42df8bae1dSRodney W. Grimes * 43df8bae1dSRodney W. Grimes * 44df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 45df8bae1dSRodney W. Grimes * All rights reserved. 46df8bae1dSRodney W. Grimes * 47df8bae1dSRodney W. Grimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 48df8bae1dSRodney W. Grimes * 49df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and 50df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright 51df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the 52df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions 53df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 54df8bae1dSRodney W. Grimes * 55df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 56df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 57df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 58df8bae1dSRodney W. Grimes * 59df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to 60df8bae1dSRodney W. Grimes * 61df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 62df8bae1dSRodney W. Grimes * School of Computer Science 63df8bae1dSRodney W. Grimes * Carnegie Mellon University 64df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890 65df8bae1dSRodney W. Grimes * 66df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 67df8bae1dSRodney W. Grimes * rights to redistribute these changes. 68df8bae1dSRodney W. Grimes */ 69df8bae1dSRodney W. Grimes 70df8bae1dSRodney W. Grimes /* 71df8bae1dSRodney W. Grimes * Page fault handling module. 72df8bae1dSRodney W. Grimes */ 73874651b1SDavid E. O'Brien 74874651b1SDavid E. O'Brien #include <sys/cdefs.h> 75874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 76874651b1SDavid E. O'Brien 7735818d2eSJohn Baldwin #include "opt_ktrace.h" 78f8a47341SAlan Cox #include "opt_vm.h" 79f8a47341SAlan Cox 80df8bae1dSRodney W. Grimes #include <sys/param.h> 81df8bae1dSRodney W. Grimes #include <sys/systm.h> 824edf4a58SJohn Baldwin #include <sys/kernel.h> 83fb919e4dSMark Murray #include <sys/lock.h> 84a8b0f100SAlan Cox #include <sys/mman.h> 8526f9a767SRodney W. Grimes #include <sys/proc.h> 86ae34b6ffSEdward Tomasz Napierala #include <sys/racct.h> 8726f9a767SRodney W. Grimes #include <sys/resourcevar.h> 8889f6b863SAttilio Rao #include <sys/rwlock.h> 8923955314SAlfred Perlstein #include <sys/sysctl.h> 904edf4a58SJohn Baldwin #include <sys/vmmeter.h> 914edf4a58SJohn Baldwin #include <sys/vnode.h> 9235818d2eSJohn Baldwin #ifdef KTRACE 9335818d2eSJohn Baldwin #include <sys/ktrace.h> 9435818d2eSJohn Baldwin #endif 95df8bae1dSRodney W. Grimes 96df8bae1dSRodney W. Grimes #include <vm/vm.h> 97efeaf95aSDavid Greenman #include <vm/vm_param.h> 98efeaf95aSDavid Greenman #include <vm/pmap.h> 99efeaf95aSDavid Greenman #include <vm/vm_map.h> 100efeaf95aSDavid Greenman #include <vm/vm_object.h> 101df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 102df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h> 103a83c285cSDavid Greenman #include <vm/vm_kern.h> 10424a1cce3SDavid Greenman #include <vm/vm_pager.h> 105efeaf95aSDavid Greenman #include <vm/vm_extern.h> 106dfdf9abdSAlan Cox #include <vm/vm_reserv.h> 107df8bae1dSRodney W. Grimes 108566526a9SAlan Cox #define PFBAK 4 109566526a9SAlan Cox #define PFFOR 4 110566526a9SAlan Cox 1115268042bSAlan Cox #define VM_FAULT_READ_DEFAULT (1 + VM_FAULT_READ_AHEAD_INIT) 11213458803SAlan Cox #define VM_FAULT_READ_MAX (1 + VM_FAULT_READ_AHEAD_MAX) 113a8b0f100SAlan Cox 114a8b0f100SAlan Cox #define VM_FAULT_DONTNEED_MIN 1048576 11526f9a767SRodney W. Grimes 1164866e085SJohn Dyson struct faultstate { 1174866e085SJohn Dyson vm_page_t m; 1184866e085SJohn Dyson vm_object_t object; 1194866e085SJohn Dyson vm_pindex_t pindex; 1204866e085SJohn Dyson vm_page_t first_m; 1214866e085SJohn Dyson vm_object_t first_object; 1224866e085SJohn Dyson vm_pindex_t first_pindex; 1234866e085SJohn Dyson vm_map_t map; 1244866e085SJohn Dyson vm_map_entry_t entry; 12525adb370SBrian Feldman int lookup_still_valid; 1264866e085SJohn Dyson struct vnode *vp; 1274866e085SJohn Dyson }; 1284866e085SJohn Dyson 129a8b0f100SAlan Cox static void vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr, 130a8b0f100SAlan Cox int ahead); 13163281952SAlan Cox static void vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra, 132b0cd2017SGleb Smirnoff int backward, int forward); 13313458803SAlan Cox 13462a59e8fSWarner Losh static inline void 1354866e085SJohn Dyson release_page(struct faultstate *fs) 1364866e085SJohn Dyson { 1370d0be82aSKonstantin Belousov 138c7aebda8SAttilio Rao vm_page_xunbusy(fs->m); 1392965a453SKip Macy vm_page_lock(fs->m); 1404866e085SJohn Dyson vm_page_deactivate(fs->m); 1412965a453SKip Macy vm_page_unlock(fs->m); 1424866e085SJohn Dyson fs->m = NULL; 1434866e085SJohn Dyson } 1444866e085SJohn Dyson 14562a59e8fSWarner Losh static inline void 1464866e085SJohn Dyson unlock_map(struct faultstate *fs) 1474866e085SJohn Dyson { 1480d0be82aSKonstantin Belousov 14925adb370SBrian Feldman if (fs->lookup_still_valid) { 1504866e085SJohn Dyson vm_map_lookup_done(fs->map, fs->entry); 15125adb370SBrian Feldman fs->lookup_still_valid = FALSE; 1524866e085SJohn Dyson } 1534866e085SJohn Dyson } 1544866e085SJohn Dyson 1554866e085SJohn Dyson static void 156a51b0840SAlan Cox unlock_and_deallocate(struct faultstate *fs) 1574866e085SJohn Dyson { 158f29ba63eSAlan Cox 1594866e085SJohn Dyson vm_object_pip_wakeup(fs->object); 16089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(fs->object); 1614866e085SJohn Dyson if (fs->object != fs->first_object) { 16289f6b863SAttilio Rao VM_OBJECT_WLOCK(fs->first_object); 1632965a453SKip Macy vm_page_lock(fs->first_m); 1644866e085SJohn Dyson vm_page_free(fs->first_m); 1652965a453SKip Macy vm_page_unlock(fs->first_m); 1664866e085SJohn Dyson vm_object_pip_wakeup(fs->first_object); 16789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(fs->first_object); 1684866e085SJohn Dyson fs->first_m = NULL; 1694866e085SJohn Dyson } 1704866e085SJohn Dyson vm_object_deallocate(fs->first_object); 1714866e085SJohn Dyson unlock_map(fs); 1724866e085SJohn Dyson if (fs->vp != NULL) { 1730cddd8f0SMatthew Dillon vput(fs->vp); 1744866e085SJohn Dyson fs->vp = NULL; 1754866e085SJohn Dyson } 1764866e085SJohn Dyson } 1774866e085SJohn Dyson 178a36f5532SKonstantin Belousov static void 179a36f5532SKonstantin Belousov vm_fault_dirty(vm_map_entry_t entry, vm_page_t m, vm_prot_t prot, 180a36f5532SKonstantin Belousov vm_prot_t fault_type, int fault_flags, boolean_t set_wd) 181a36f5532SKonstantin Belousov { 182a36f5532SKonstantin Belousov boolean_t need_dirty; 183a36f5532SKonstantin Belousov 184a36f5532SKonstantin Belousov if (((prot & VM_PROT_WRITE) == 0 && 185a36f5532SKonstantin Belousov (fault_flags & VM_FAULT_DIRTY) == 0) || 186a36f5532SKonstantin Belousov (m->oflags & VPO_UNMANAGED) != 0) 187a36f5532SKonstantin Belousov return; 188a36f5532SKonstantin Belousov 189a36f5532SKonstantin Belousov VM_OBJECT_ASSERT_LOCKED(m->object); 190a36f5532SKonstantin Belousov 191a36f5532SKonstantin Belousov need_dirty = ((fault_type & VM_PROT_WRITE) != 0 && 1926a875bf9SKonstantin Belousov (fault_flags & VM_FAULT_WIRE) == 0) || 193a36f5532SKonstantin Belousov (fault_flags & VM_FAULT_DIRTY) != 0; 194a36f5532SKonstantin Belousov 195a36f5532SKonstantin Belousov if (set_wd) 196a36f5532SKonstantin Belousov vm_object_set_writeable_dirty(m->object); 197a36f5532SKonstantin Belousov else 198a36f5532SKonstantin Belousov /* 199a36f5532SKonstantin Belousov * If two callers of vm_fault_dirty() with set_wd == 200a36f5532SKonstantin Belousov * FALSE, one for the map entry with MAP_ENTRY_NOSYNC 201a36f5532SKonstantin Belousov * flag set, other with flag clear, race, it is 202a36f5532SKonstantin Belousov * possible for the no-NOSYNC thread to see m->dirty 203a36f5532SKonstantin Belousov * != 0 and not clear VPO_NOSYNC. Take vm_page lock 204a36f5532SKonstantin Belousov * around manipulation of VPO_NOSYNC and 205a36f5532SKonstantin Belousov * vm_page_dirty() call, to avoid the race and keep 206a36f5532SKonstantin Belousov * m->oflags consistent. 207a36f5532SKonstantin Belousov */ 208a36f5532SKonstantin Belousov vm_page_lock(m); 209a36f5532SKonstantin Belousov 210a36f5532SKonstantin Belousov /* 211a36f5532SKonstantin Belousov * If this is a NOSYNC mmap we do not want to set VPO_NOSYNC 212a36f5532SKonstantin Belousov * if the page is already dirty to prevent data written with 213a36f5532SKonstantin Belousov * the expectation of being synced from not being synced. 214a36f5532SKonstantin Belousov * Likewise if this entry does not request NOSYNC then make 215a36f5532SKonstantin Belousov * sure the page isn't marked NOSYNC. Applications sharing 216a36f5532SKonstantin Belousov * data should use the same flags to avoid ping ponging. 217a36f5532SKonstantin Belousov */ 218a36f5532SKonstantin Belousov if ((entry->eflags & MAP_ENTRY_NOSYNC) != 0) { 219a36f5532SKonstantin Belousov if (m->dirty == 0) { 220a36f5532SKonstantin Belousov m->oflags |= VPO_NOSYNC; 221a36f5532SKonstantin Belousov } 222a36f5532SKonstantin Belousov } else { 223a36f5532SKonstantin Belousov m->oflags &= ~VPO_NOSYNC; 224a36f5532SKonstantin Belousov } 225a36f5532SKonstantin Belousov 226a36f5532SKonstantin Belousov /* 227a36f5532SKonstantin Belousov * If the fault is a write, we know that this page is being 228a36f5532SKonstantin Belousov * written NOW so dirty it explicitly to save on 229a36f5532SKonstantin Belousov * pmap_is_modified() calls later. 230a36f5532SKonstantin Belousov * 231a36f5532SKonstantin Belousov * Also tell the backing pager, if any, that it should remove 232a36f5532SKonstantin Belousov * any swap backing since the page is now dirty. 233a36f5532SKonstantin Belousov */ 234a36f5532SKonstantin Belousov if (need_dirty) 235a36f5532SKonstantin Belousov vm_page_dirty(m); 236a36f5532SKonstantin Belousov if (!set_wd) 237a36f5532SKonstantin Belousov vm_page_unlock(m); 238a36f5532SKonstantin Belousov if (need_dirty) 239a36f5532SKonstantin Belousov vm_pager_page_unswapped(m); 240a36f5532SKonstantin Belousov } 241a36f5532SKonstantin Belousov 242df8bae1dSRodney W. Grimes /* 243df8bae1dSRodney W. Grimes * vm_fault: 244df8bae1dSRodney W. Grimes * 245956f3135SPhilippe Charnier * Handle a page fault occurring at the given address, 246df8bae1dSRodney W. Grimes * requiring the given permissions, in the map specified. 247df8bae1dSRodney W. Grimes * If successful, the page is inserted into the 248df8bae1dSRodney W. Grimes * associated physical map. 249df8bae1dSRodney W. Grimes * 250df8bae1dSRodney W. Grimes * NOTE: the given address should be truncated to the 251df8bae1dSRodney W. Grimes * proper page address. 252df8bae1dSRodney W. Grimes * 253df8bae1dSRodney W. Grimes * KERN_SUCCESS is returned if the page fault is handled; otherwise, 254df8bae1dSRodney W. Grimes * a standard error specifying why the fault is fatal is returned. 255df8bae1dSRodney W. Grimes * 256df8bae1dSRodney W. Grimes * The map in question must be referenced, and remains so. 2570cddd8f0SMatthew Dillon * Caller may hold no locks. 258df8bae1dSRodney W. Grimes */ 259df8bae1dSRodney W. Grimes int 26023955314SAlfred Perlstein vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, 26123955314SAlfred Perlstein int fault_flags) 26223955314SAlfred Perlstein { 26335818d2eSJohn Baldwin struct thread *td; 26435818d2eSJohn Baldwin int result; 265acd11c74SAlan Cox 26635818d2eSJohn Baldwin td = curthread; 26735818d2eSJohn Baldwin if ((td->td_pflags & TDP_NOFAULTING) != 0) 2682801687dSKonstantin Belousov return (KERN_PROTECTION_FAILURE); 26935818d2eSJohn Baldwin #ifdef KTRACE 27035818d2eSJohn Baldwin if (map != kernel_map && KTRPOINT(td, KTR_FAULT)) 27135818d2eSJohn Baldwin ktrfault(vaddr, fault_type); 27235818d2eSJohn Baldwin #endif 273be996836SAttilio Rao result = vm_fault_hold(map, trunc_page(vaddr), fault_type, fault_flags, 274be996836SAttilio Rao NULL); 27535818d2eSJohn Baldwin #ifdef KTRACE 27635818d2eSJohn Baldwin if (map != kernel_map && KTRPOINT(td, KTR_FAULTEND)) 27735818d2eSJohn Baldwin ktrfaultend(result); 27835818d2eSJohn Baldwin #endif 27935818d2eSJohn Baldwin return (result); 280acd11c74SAlan Cox } 281acd11c74SAlan Cox 282acd11c74SAlan Cox int 283be996836SAttilio Rao vm_fault_hold(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, 284acd11c74SAlan Cox int fault_flags, vm_page_t *m_hold) 285acd11c74SAlan Cox { 286df8bae1dSRodney W. Grimes vm_prot_t prot; 287b0cd2017SGleb Smirnoff int alloc_req, era, faultcount, nera, result; 2883f1c66b8SKonstantin Belousov boolean_t dead, growstack, is_first_object_locked, wired; 2892d8acc0fSJohn Dyson int map_generation; 290df8bae1dSRodney W. Grimes vm_object_t next_object; 2914866e085SJohn Dyson int hardfault; 2924866e085SJohn Dyson struct faultstate fs; 293d2bf64c3SKonstantin Belousov struct vnode *vp; 2940c3a4893SAlan Cox vm_offset_t e_end, e_start; 295afe55ca3SKonstantin Belousov vm_page_t m; 2960c3a4893SAlan Cox int ahead, behind, cluster_offset, error, locked, rv; 2970c3a4893SAlan Cox u_char behavior; 298df8bae1dSRodney W. Grimes 2994866e085SJohn Dyson hardfault = 0; 3006139043bSAlan Cox growstack = TRUE; 30167596082SAttilio Rao PCPU_INC(cnt.v_vm_faults); 302d2bf64c3SKonstantin Belousov fs.vp = NULL; 303b0cd2017SGleb Smirnoff faultcount = 0; 3040c3a4893SAlan Cox nera = -1; 305df8bae1dSRodney W. Grimes 306df8bae1dSRodney W. Grimes RetryFault:; 307df8bae1dSRodney W. Grimes 308df8bae1dSRodney W. Grimes /* 3090d94caffSDavid Greenman * Find the backing store object and offset into it to begin the 3100d94caffSDavid Greenman * search. 311df8bae1dSRodney W. Grimes */ 31240360b1bSMatthew Dillon fs.map = map; 31392de35b0SAlan Cox result = vm_map_lookup(&fs.map, vaddr, fault_type, &fs.entry, 31492de35b0SAlan Cox &fs.first_object, &fs.first_pindex, &prot, &wired); 31592de35b0SAlan Cox if (result != KERN_SUCCESS) { 3166139043bSAlan Cox if (growstack && result == KERN_INVALID_ADDRESS && 3172db65ab4SAlan Cox map != kernel_map) { 3186139043bSAlan Cox result = vm_map_growstack(curproc, vaddr); 319a976eb5eSAlan Cox if (result != KERN_SUCCESS) 3206139043bSAlan Cox return (KERN_FAILURE); 3216139043bSAlan Cox growstack = FALSE; 3226139043bSAlan Cox goto RetryFault; 3236139043bSAlan Cox } 324*eb17fb15SMark Johnston if (fs.vp != NULL) 325*eb17fb15SMark Johnston vput(fs.vp); 32692de35b0SAlan Cox return (result); 32709e0c6ccSJohn Dyson } 32809e0c6ccSJohn Dyson 3294866e085SJohn Dyson map_generation = fs.map->timestamp; 3302d8acc0fSJohn Dyson 3314866e085SJohn Dyson if (fs.entry->eflags & MAP_ENTRY_NOFAULT) { 33247221757SJohn Dyson panic("vm_fault: fault on nofault entry, addr: %lx", 33392c4c4ebSBruce Evans (u_long)vaddr); 3347aaaa4fdSJohn Dyson } 3357aaaa4fdSJohn Dyson 3364f9c9114SKonstantin Belousov if (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION && 3374f9c9114SKonstantin Belousov fs.entry->wiring_thread != curthread) { 3384f9c9114SKonstantin Belousov vm_map_unlock_read(fs.map); 3394f9c9114SKonstantin Belousov vm_map_lock(fs.map); 3404f9c9114SKonstantin Belousov if (vm_map_lookup_entry(fs.map, vaddr, &fs.entry) && 3414f9c9114SKonstantin Belousov (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION)) { 34285af31a4SKonstantin Belousov if (fs.vp != NULL) { 34385af31a4SKonstantin Belousov vput(fs.vp); 34485af31a4SKonstantin Belousov fs.vp = NULL; 34585af31a4SKonstantin Belousov } 3464f9c9114SKonstantin Belousov fs.entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 3474f9c9114SKonstantin Belousov vm_map_unlock_and_wait(fs.map, 0); 3484f9c9114SKonstantin Belousov } else 3494f9c9114SKonstantin Belousov vm_map_unlock(fs.map); 3504f9c9114SKonstantin Belousov goto RetryFault; 3514f9c9114SKonstantin Belousov } 3524f9c9114SKonstantin Belousov 353afe55ca3SKonstantin Belousov if (wired) 354afe55ca3SKonstantin Belousov fault_type = prot | (fault_type & VM_PROT_COPY); 3556a875bf9SKonstantin Belousov else 3566a875bf9SKonstantin Belousov KASSERT((fault_flags & VM_FAULT_WIRE) == 0, 3576a875bf9SKonstantin Belousov ("!wired && VM_FAULT_WIRE")); 358afe55ca3SKonstantin Belousov 3598d67b8c8SAlan Cox /* 3608d67b8c8SAlan Cox * Try to avoid lock contention on the top-level object through 3618d67b8c8SAlan Cox * special-case handling of some types of page faults, specifically, 3628d67b8c8SAlan Cox * those that are both (1) mapping an existing page from the top- 3638d67b8c8SAlan Cox * level object and (2) not having to mark that object as containing 3648d67b8c8SAlan Cox * dirty pages. Under these conditions, a read lock on the top-level 3658d67b8c8SAlan Cox * object suffices, allowing multiple page faults of a similar type to 3668d67b8c8SAlan Cox * run in parallel on the same top-level object. 3678d67b8c8SAlan Cox */ 368afe55ca3SKonstantin Belousov if (fs.vp == NULL /* avoid locked vnode leak */ && 3696a875bf9SKonstantin Belousov (fault_flags & (VM_FAULT_WIRE | VM_FAULT_DIRTY)) == 0 && 370afe55ca3SKonstantin Belousov /* avoid calling vm_object_set_writeable_dirty() */ 371afe55ca3SKonstantin Belousov ((prot & VM_PROT_WRITE) == 0 || 372f40cb1c6SKonstantin Belousov (fs.first_object->type != OBJT_VNODE && 373f40cb1c6SKonstantin Belousov (fs.first_object->flags & OBJ_TMPFS_NODE) == 0) || 374afe55ca3SKonstantin Belousov (fs.first_object->flags & OBJ_MIGHTBEDIRTY) != 0)) { 375afe55ca3SKonstantin Belousov VM_OBJECT_RLOCK(fs.first_object); 376afe55ca3SKonstantin Belousov if ((prot & VM_PROT_WRITE) != 0 && 377f40cb1c6SKonstantin Belousov (fs.first_object->type == OBJT_VNODE || 378f40cb1c6SKonstantin Belousov (fs.first_object->flags & OBJ_TMPFS_NODE) != 0) && 379afe55ca3SKonstantin Belousov (fs.first_object->flags & OBJ_MIGHTBEDIRTY) == 0) 380afe55ca3SKonstantin Belousov goto fast_failed; 381afe55ca3SKonstantin Belousov m = vm_page_lookup(fs.first_object, fs.first_pindex); 382b9ce8cc2SAlan Cox /* A busy page can be mapped for read|execute access. */ 383b9ce8cc2SAlan Cox if (m == NULL || ((prot & VM_PROT_WRITE) != 0 && 384b9ce8cc2SAlan Cox vm_page_busied(m)) || m->valid != VM_PAGE_BITS_ALL) 385afe55ca3SKonstantin Belousov goto fast_failed; 386afe55ca3SKonstantin Belousov result = pmap_enter(fs.map->pmap, vaddr, m, prot, 387afe55ca3SKonstantin Belousov fault_type | PMAP_ENTER_NOSLEEP | (wired ? PMAP_ENTER_WIRED : 388afe55ca3SKonstantin Belousov 0), 0); 389afe55ca3SKonstantin Belousov if (result != KERN_SUCCESS) 390afe55ca3SKonstantin Belousov goto fast_failed; 391afe55ca3SKonstantin Belousov if (m_hold != NULL) { 392afe55ca3SKonstantin Belousov *m_hold = m; 393afe55ca3SKonstantin Belousov vm_page_lock(m); 394afe55ca3SKonstantin Belousov vm_page_hold(m); 395afe55ca3SKonstantin Belousov vm_page_unlock(m); 396afe55ca3SKonstantin Belousov } 397a36f5532SKonstantin Belousov vm_fault_dirty(fs.entry, m, prot, fault_type, fault_flags, 398a36f5532SKonstantin Belousov FALSE); 399afe55ca3SKonstantin Belousov VM_OBJECT_RUNLOCK(fs.first_object); 400afe55ca3SKonstantin Belousov if (!wired) 401b0cd2017SGleb Smirnoff vm_fault_prefault(&fs, vaddr, PFBAK, PFFOR); 402afe55ca3SKonstantin Belousov vm_map_lookup_done(fs.map, fs.entry); 403afe55ca3SKonstantin Belousov curthread->td_ru.ru_minflt++; 404afe55ca3SKonstantin Belousov return (KERN_SUCCESS); 405afe55ca3SKonstantin Belousov fast_failed: 406afe55ca3SKonstantin Belousov if (!VM_OBJECT_TRYUPGRADE(fs.first_object)) { 407afe55ca3SKonstantin Belousov VM_OBJECT_RUNLOCK(fs.first_object); 408afe55ca3SKonstantin Belousov VM_OBJECT_WLOCK(fs.first_object); 409afe55ca3SKonstantin Belousov } 410afe55ca3SKonstantin Belousov } else { 411afe55ca3SKonstantin Belousov VM_OBJECT_WLOCK(fs.first_object); 412afe55ca3SKonstantin Belousov } 413afe55ca3SKonstantin Belousov 41495e5e988SJohn Dyson /* 41595e5e988SJohn Dyson * Make a reference to this object to prevent its disposal while we 41695e5e988SJohn Dyson * are messing with it. Once we have the reference, the map is free 41795e5e988SJohn Dyson * to be diddled. Since objects reference their shadows (and copies), 41895e5e988SJohn Dyson * they will stay around as well. 419fe8e0238SMatthew Dillon * 420fe8e0238SMatthew Dillon * Bump the paging-in-progress count to prevent size changes (e.g. 421fe8e0238SMatthew Dillon * truncation operations) during I/O. This must be done after 422fe8e0238SMatthew Dillon * obtaining the vnode lock in order to avoid possible deadlocks. 42395e5e988SJohn Dyson */ 424a976eb5eSAlan Cox vm_object_reference_locked(fs.first_object); 425d474eaaaSDoug Rabson vm_object_pip_add(fs.first_object, 1); 42695e5e988SJohn Dyson 42725adb370SBrian Feldman fs.lookup_still_valid = TRUE; 428df8bae1dSRodney W. Grimes 4294866e085SJohn Dyson fs.first_m = NULL; 430df8bae1dSRodney W. Grimes 431df8bae1dSRodney W. Grimes /* 432df8bae1dSRodney W. Grimes * Search for the page at object/offset. 433df8bae1dSRodney W. Grimes */ 4344866e085SJohn Dyson fs.object = fs.first_object; 4354866e085SJohn Dyson fs.pindex = fs.first_pindex; 436df8bae1dSRodney W. Grimes while (TRUE) { 4371c7c3c6aSMatthew Dillon /* 438725441f6SKonstantin Belousov * If the object is marked for imminent termination, 439725441f6SKonstantin Belousov * we retry here, since the collapse pass has raced 440725441f6SKonstantin Belousov * with us. Otherwise, if we see terminally dead 441725441f6SKonstantin Belousov * object, return fail. 4421c7c3c6aSMatthew Dillon */ 443725441f6SKonstantin Belousov if ((fs.object->flags & OBJ_DEAD) != 0) { 444725441f6SKonstantin Belousov dead = fs.object->type == OBJT_DEAD; 4454866e085SJohn Dyson unlock_and_deallocate(&fs); 446725441f6SKonstantin Belousov if (dead) 44747221757SJohn Dyson return (KERN_PROTECTION_FAILURE); 448725441f6SKonstantin Belousov pause("vmf_de", 1); 449725441f6SKonstantin Belousov goto RetryFault; 45047221757SJohn Dyson } 45147221757SJohn Dyson 4521c7c3c6aSMatthew Dillon /* 4531c7c3c6aSMatthew Dillon * See if page is resident 4541c7c3c6aSMatthew Dillon */ 4554866e085SJohn Dyson fs.m = vm_page_lookup(fs.object, fs.pindex); 4564866e085SJohn Dyson if (fs.m != NULL) { 45798cb733cSKenneth D. Merry /* 4581c7c3c6aSMatthew Dillon * Wait/Retry if the page is busy. We have to do this 459c7aebda8SAttilio Rao * if the page is either exclusive or shared busy 460c7aebda8SAttilio Rao * because the vm_pager may be using read busy for 461c7aebda8SAttilio Rao * pageouts (and even pageins if it is the vnode 462c7aebda8SAttilio Rao * pager), and we could end up trying to pagein and 463c7aebda8SAttilio Rao * pageout the same page simultaneously. 4641c7c3c6aSMatthew Dillon * 4651c7c3c6aSMatthew Dillon * We can theoretically allow the busy case on a read 4661c7c3c6aSMatthew Dillon * fault if the page is marked valid, but since such 4671c7c3c6aSMatthew Dillon * pages are typically already pmap'd, putting that 4681c7c3c6aSMatthew Dillon * special case in might be more effort then it is 4691c7c3c6aSMatthew Dillon * worth. We cannot under any circumstances mess 470c7aebda8SAttilio Rao * around with a shared busied page except, perhaps, 4711c7c3c6aSMatthew Dillon * to pmap it. 472df8bae1dSRodney W. Grimes */ 473c7aebda8SAttilio Rao if (vm_page_busied(fs.m)) { 474b88b6c9dSAlan Cox /* 475b88b6c9dSAlan Cox * Reference the page before unlocking and 476b88b6c9dSAlan Cox * sleeping so that the page daemon is less 477b88b6c9dSAlan Cox * likely to reclaim it. 478b88b6c9dSAlan Cox */ 4793407fefeSKonstantin Belousov vm_page_aflag_set(fs.m, PGA_REFERENCED); 480a51b0840SAlan Cox if (fs.object != fs.first_object) { 48189f6b863SAttilio Rao if (!VM_OBJECT_TRYWLOCK( 482a6e38685SKonstantin Belousov fs.first_object)) { 48389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(fs.object); 48489f6b863SAttilio Rao VM_OBJECT_WLOCK(fs.first_object); 48589f6b863SAttilio Rao VM_OBJECT_WLOCK(fs.object); 486a6e38685SKonstantin Belousov } 4872965a453SKip Macy vm_page_lock(fs.first_m); 488a51b0840SAlan Cox vm_page_free(fs.first_m); 4892965a453SKip Macy vm_page_unlock(fs.first_m); 490a51b0840SAlan Cox vm_object_pip_wakeup(fs.first_object); 49189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(fs.first_object); 492a51b0840SAlan Cox fs.first_m = NULL; 493a51b0840SAlan Cox } 494a51b0840SAlan Cox unlock_map(&fs); 495a51b0840SAlan Cox if (fs.m == vm_page_lookup(fs.object, 496a51b0840SAlan Cox fs.pindex)) { 497c7aebda8SAttilio Rao vm_page_sleep_if_busy(fs.m, "vmpfw"); 498a51b0840SAlan Cox } 499a51b0840SAlan Cox vm_object_pip_wakeup(fs.object); 50089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(fs.object); 50167596082SAttilio Rao PCPU_INC(cnt.v_intrans); 5024866e085SJohn Dyson vm_object_deallocate(fs.first_object); 503df8bae1dSRodney W. Grimes goto RetryFault; 504df8bae1dSRodney W. Grimes } 5053846a822SKonstantin Belousov vm_page_lock(fs.m); 5068d220203SAlan Cox vm_page_remque(fs.m); 5072965a453SKip Macy vm_page_unlock(fs.m); 5087615edaaSMatthew Dillon 5091c7c3c6aSMatthew Dillon /* 5101c7c3c6aSMatthew Dillon * Mark page busy for other processes, and the 5111c7c3c6aSMatthew Dillon * pagedaemon. If it still isn't completely valid 5121c7c3c6aSMatthew Dillon * (readable), jump to readrest, else break-out ( we 5131c7c3c6aSMatthew Dillon * found the page ). 5141c7c3c6aSMatthew Dillon */ 515c7aebda8SAttilio Rao vm_page_xbusy(fs.m); 516ff5958e7SAlan Cox if (fs.m->valid != VM_PAGE_BITS_ALL) 5170d94caffSDavid Greenman goto readrest; 518df8bae1dSRodney W. Grimes break; 519df8bae1dSRodney W. Grimes } 52010b4196bSAlan Cox KASSERT(fs.m == NULL, ("fs.m should be NULL, not %p", fs.m)); 5211c7c3c6aSMatthew Dillon 5221c7c3c6aSMatthew Dillon /* 52310b4196bSAlan Cox * Page is not resident. If the pager might contain the page 52410b4196bSAlan Cox * or this is the beginning of the search, allocate a new 52510b4196bSAlan Cox * page. (Default objects are zero-fill, so there is no real 52610b4196bSAlan Cox * pager for them.) 5271c7c3c6aSMatthew Dillon */ 5286a875bf9SKonstantin Belousov if (fs.object->type != OBJT_DEFAULT || 5296a875bf9SKonstantin Belousov fs.object == fs.first_object) { 5304866e085SJohn Dyson if (fs.pindex >= fs.object->size) { 5314866e085SJohn Dyson unlock_and_deallocate(&fs); 5325f55e841SDavid Greenman return (KERN_PROTECTION_FAILURE); 5335f55e841SDavid Greenman } 53422ba64e8SJohn Dyson 535df8bae1dSRodney W. Grimes /* 5360d94caffSDavid Greenman * Allocate a new page for this object/offset pair. 5373f1c4c4fSKonstantin Belousov * 5383f1c4c4fSKonstantin Belousov * Unlocked read of the p_flag is harmless. At 5393f1c4c4fSKonstantin Belousov * worst, the P_KILLED might be not observed 5403f1c4c4fSKonstantin Belousov * there, and allocation can fail, causing 5413f1c4c4fSKonstantin Belousov * restart and new reading of the p_flag. 542df8bae1dSRodney W. Grimes */ 5433f1c4c4fSKonstantin Belousov if (!vm_page_count_severe() || P_KILLED(curproc)) { 544f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 5453d653db0SAlan Cox vm_object_color(fs.object, atop(vaddr) - 5463d653db0SAlan Cox fs.pindex); 547f8a47341SAlan Cox #endif 5483f1c4c4fSKonstantin Belousov alloc_req = P_KILLED(curproc) ? 5493f1c4c4fSKonstantin Belousov VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL; 5503f1c4c4fSKonstantin Belousov if (fs.object->type != OBJT_VNODE && 5513f1c4c4fSKonstantin Belousov fs.object->backing_object == NULL) 5523f1c4c4fSKonstantin Belousov alloc_req |= VM_ALLOC_ZERO; 5534866e085SJohn Dyson fs.m = vm_page_alloc(fs.object, fs.pindex, 5543f1c4c4fSKonstantin Belousov alloc_req); 55540360b1bSMatthew Dillon } 5564866e085SJohn Dyson if (fs.m == NULL) { 5574866e085SJohn Dyson unlock_and_deallocate(&fs); 558ef6020d1SMike Silbersack VM_WAITPFAULT; 559df8bae1dSRodney W. Grimes goto RetryFault; 5600a2e596aSAlan Cox } else if (fs.m->valid == VM_PAGE_BITS_ALL) 5614ab8ab92SKonstantin Belousov break; 562df8bae1dSRodney W. Grimes } 56347221757SJohn Dyson 5640d94caffSDavid Greenman readrest: 5651c7c3c6aSMatthew Dillon /* 5660c3a4893SAlan Cox * If the pager for the current object might have the page, 5670c3a4893SAlan Cox * then determine the number of additional pages to read and 5680c3a4893SAlan Cox * potentially reprioritize previously read pages for earlier 5690c3a4893SAlan Cox * reclamation. These operations should only be performed 5700c3a4893SAlan Cox * once per page fault. Even if the current pager doesn't 5710c3a4893SAlan Cox * have the page, the number of additional pages to read will 5720c3a4893SAlan Cox * apply to subsequent objects in the shadow chain. 5731c7c3c6aSMatthew Dillon */ 5740c3a4893SAlan Cox if (fs.object->type != OBJT_DEFAULT && nera == -1 && 5750c3a4893SAlan Cox !P_KILLED(curproc)) { 5760c3a4893SAlan Cox KASSERT(fs.lookup_still_valid, ("map unlocked")); 5775268042bSAlan Cox era = fs.entry->read_ahead; 5780c3a4893SAlan Cox behavior = vm_map_entry_behavior(fs.entry); 5790c3a4893SAlan Cox if (behavior == MAP_ENTRY_BEHAV_RANDOM) { 5805268042bSAlan Cox nera = 0; 58113458803SAlan Cox } else if (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL) { 5825268042bSAlan Cox nera = VM_FAULT_READ_AHEAD_MAX; 583381b7242SAlan Cox if (vaddr == fs.entry->next_read) 5840c3a4893SAlan Cox vm_fault_dontneed(&fs, vaddr, nera); 585381b7242SAlan Cox } else if (vaddr == fs.entry->next_read) { 58613458803SAlan Cox /* 5875268042bSAlan Cox * This is a sequential fault. Arithmetically 5885268042bSAlan Cox * increase the requested number of pages in 5895268042bSAlan Cox * the read-ahead window. The requested 5905268042bSAlan Cox * number of pages is "# of sequential faults 5915268042bSAlan Cox * x (read ahead min + 1) + read ahead min" 59213458803SAlan Cox */ 5935268042bSAlan Cox nera = VM_FAULT_READ_AHEAD_MIN; 5945268042bSAlan Cox if (era > 0) { 5955268042bSAlan Cox nera += era + 1; 59613458803SAlan Cox if (nera > VM_FAULT_READ_AHEAD_MAX) 59713458803SAlan Cox nera = VM_FAULT_READ_AHEAD_MAX; 5985268042bSAlan Cox } 59913458803SAlan Cox if (era == VM_FAULT_READ_AHEAD_MAX) 6000c3a4893SAlan Cox vm_fault_dontneed(&fs, vaddr, nera); 6015268042bSAlan Cox } else { 6025268042bSAlan Cox /* 6030c3a4893SAlan Cox * This is a non-sequential fault. 6045268042bSAlan Cox */ 6055268042bSAlan Cox nera = 0; 606867a482dSJohn Dyson } 6070c3a4893SAlan Cox if (era != nera) { 6080c3a4893SAlan Cox /* 6090c3a4893SAlan Cox * A read lock on the map suffices to update 6100c3a4893SAlan Cox * the read ahead count safely. 6110c3a4893SAlan Cox */ 6125268042bSAlan Cox fs.entry->read_ahead = nera; 6130c3a4893SAlan Cox } 614d2bf64c3SKonstantin Belousov 615d2bf64c3SKonstantin Belousov /* 6160c3a4893SAlan Cox * Prepare for unlocking the map. Save the map 6170c3a4893SAlan Cox * entry's start and end addresses, which are used to 6180c3a4893SAlan Cox * optimize the size of the pager operation below. 6190c3a4893SAlan Cox * Even if the map entry's addresses change after 6200c3a4893SAlan Cox * unlocking the map, using the saved addresses is 6210c3a4893SAlan Cox * safe. 6220c3a4893SAlan Cox */ 6230c3a4893SAlan Cox e_start = fs.entry->start; 6240c3a4893SAlan Cox e_end = fs.entry->end; 6250c3a4893SAlan Cox } 6260c3a4893SAlan Cox 6270c3a4893SAlan Cox /* 6280c3a4893SAlan Cox * Call the pager to retrieve the page if there is a chance 6290c3a4893SAlan Cox * that the pager has it, and potentially retrieve additional 6300c3a4893SAlan Cox * pages at the same time. 6310c3a4893SAlan Cox */ 6320c3a4893SAlan Cox if (fs.object->type != OBJT_DEFAULT) { 6330c3a4893SAlan Cox /* 6340c3a4893SAlan Cox * We have either allocated a new page or found an 6350c3a4893SAlan Cox * existing page that is only partially valid. We 6360c3a4893SAlan Cox * hold a reference on fs.object and the page is 6370c3a4893SAlan Cox * exclusive busied. 638d2bf64c3SKonstantin Belousov */ 639d2bf64c3SKonstantin Belousov unlock_map(&fs); 640d2bf64c3SKonstantin Belousov 641d2bf64c3SKonstantin Belousov if (fs.object->type == OBJT_VNODE) { 642d2bf64c3SKonstantin Belousov vp = fs.object->handle; 643d2bf64c3SKonstantin Belousov if (vp == fs.vp) 644d2bf64c3SKonstantin Belousov goto vnode_locked; 645d2bf64c3SKonstantin Belousov else if (fs.vp != NULL) { 646d2bf64c3SKonstantin Belousov vput(fs.vp); 647d2bf64c3SKonstantin Belousov fs.vp = NULL; 648d2bf64c3SKonstantin Belousov } 649d2bf64c3SKonstantin Belousov locked = VOP_ISLOCKED(vp); 650d2bf64c3SKonstantin Belousov 651d2bf64c3SKonstantin Belousov if (locked != LK_EXCLUSIVE) 652d2bf64c3SKonstantin Belousov locked = LK_SHARED; 653d2bf64c3SKonstantin Belousov /* Do not sleep for vnode lock while fs.m is busy */ 654d2bf64c3SKonstantin Belousov error = vget(vp, locked | LK_CANRECURSE | 655d2bf64c3SKonstantin Belousov LK_NOWAIT, curthread); 656d2bf64c3SKonstantin Belousov if (error != 0) { 657d2bf64c3SKonstantin Belousov vhold(vp); 658d2bf64c3SKonstantin Belousov release_page(&fs); 659d2bf64c3SKonstantin Belousov unlock_and_deallocate(&fs); 660d2bf64c3SKonstantin Belousov error = vget(vp, locked | LK_RETRY | 661d2bf64c3SKonstantin Belousov LK_CANRECURSE, curthread); 662d2bf64c3SKonstantin Belousov vdrop(vp); 663d2bf64c3SKonstantin Belousov fs.vp = vp; 664d2bf64c3SKonstantin Belousov KASSERT(error == 0, 665d2bf64c3SKonstantin Belousov ("vm_fault: vget failed")); 666d2bf64c3SKonstantin Belousov goto RetryFault; 667d2bf64c3SKonstantin Belousov } 668d2bf64c3SKonstantin Belousov fs.vp = vp; 669d2bf64c3SKonstantin Belousov } 670d2bf64c3SKonstantin Belousov vnode_locked: 671d2bf64c3SKonstantin Belousov KASSERT(fs.vp == NULL || !fs.map->system_map, 672d2bf64c3SKonstantin Belousov ("vm_fault: vnode-backed object mapped by system map")); 673d2bf64c3SKonstantin Belousov 674df8bae1dSRodney W. Grimes /* 675b0cd2017SGleb Smirnoff * Page in the requested page and hint the pager, 676b0cd2017SGleb Smirnoff * that it may bring up surrounding pages. 67726f9a767SRodney W. Grimes */ 6780c3a4893SAlan Cox if (nera == -1 || behavior == MAP_ENTRY_BEHAV_RANDOM || 6790c3a4893SAlan Cox P_KILLED(curproc)) { 6800c3a4893SAlan Cox behind = 0; 6810c3a4893SAlan Cox ahead = 0; 6820c3a4893SAlan Cox } else { 6830c3a4893SAlan Cox /* Is this a sequential fault? */ 6840c3a4893SAlan Cox if (nera > 0) { 6850c3a4893SAlan Cox behind = 0; 6860c3a4893SAlan Cox ahead = nera; 6870c3a4893SAlan Cox } else { 6880c3a4893SAlan Cox /* 6890c3a4893SAlan Cox * Request a cluster of pages that is 6900c3a4893SAlan Cox * aligned to a VM_FAULT_READ_DEFAULT 6910c3a4893SAlan Cox * page offset boundary within the 6920c3a4893SAlan Cox * object. Alignment to a page offset 6930c3a4893SAlan Cox * boundary is more likely to coincide 6940c3a4893SAlan Cox * with the underlying file system 6950c3a4893SAlan Cox * block than alignment to a virtual 6960c3a4893SAlan Cox * address boundary. 6970c3a4893SAlan Cox */ 6980c3a4893SAlan Cox cluster_offset = fs.pindex % 6990c3a4893SAlan Cox VM_FAULT_READ_DEFAULT; 7000c3a4893SAlan Cox behind = ulmin(cluster_offset, 7010c3a4893SAlan Cox atop(vaddr - e_start)); 7020c3a4893SAlan Cox ahead = VM_FAULT_READ_DEFAULT - 1 - 7030c3a4893SAlan Cox cluster_offset; 7040c3a4893SAlan Cox } 7050c3a4893SAlan Cox ahead = ulmin(ahead, atop(e_end - vaddr) - 1); 7060c3a4893SAlan Cox } 707b0cd2017SGleb Smirnoff rv = vm_pager_get_pages(fs.object, &fs.m, 1, 708b0cd2017SGleb Smirnoff &behind, &ahead); 70926f9a767SRodney W. Grimes if (rv == VM_PAGER_OK) { 710b0cd2017SGleb Smirnoff faultcount = behind + 1 + ahead; 71126f9a767SRodney W. Grimes hardfault++; 7121c7c3c6aSMatthew Dillon break; /* break to PAGE HAS BEEN FOUND */ 713df8bae1dSRodney W. Grimes } 714a83c285cSDavid Greenman if (rv == VM_PAGER_ERROR) 715f3679e35SDavid Greenman printf("vm_fault: pager read error, pid %d (%s)\n", 716f3679e35SDavid Greenman curproc->p_pid, curproc->p_comm); 717521ddf39SAlan Cox 71826f9a767SRodney W. Grimes /* 719521ddf39SAlan Cox * If an I/O error occurred or the requested page was 720521ddf39SAlan Cox * outside the range of the pager, clean up and return 721521ddf39SAlan Cox * an error. 72226f9a767SRodney W. Grimes */ 723521ddf39SAlan Cox if (rv == VM_PAGER_ERROR || rv == VM_PAGER_BAD) { 7242965a453SKip Macy vm_page_lock(fs.m); 7254866e085SJohn Dyson vm_page_free(fs.m); 7262965a453SKip Macy vm_page_unlock(fs.m); 7274866e085SJohn Dyson fs.m = NULL; 7284866e085SJohn Dyson unlock_and_deallocate(&fs); 729521ddf39SAlan Cox return (rv == VM_PAGER_ERROR ? KERN_FAILURE : 730521ddf39SAlan Cox KERN_PROTECTION_FAILURE); 73126f9a767SRodney W. Grimes } 732521ddf39SAlan Cox 733521ddf39SAlan Cox /* 734521ddf39SAlan Cox * The requested page does not exist at this object/ 735521ddf39SAlan Cox * offset. Remove the invalid page from the object, 736521ddf39SAlan Cox * waking up anyone waiting for it, and continue on to 737521ddf39SAlan Cox * the next object. However, if this is the top-level 738521ddf39SAlan Cox * object, we must leave the busy page in place to 739521ddf39SAlan Cox * prevent another process from rushing past us, and 740521ddf39SAlan Cox * inserting the page in that object at the same time 741521ddf39SAlan Cox * that we are. 742521ddf39SAlan Cox */ 7434866e085SJohn Dyson if (fs.object != fs.first_object) { 7442965a453SKip Macy vm_page_lock(fs.m); 7454866e085SJohn Dyson vm_page_free(fs.m); 7462965a453SKip Macy vm_page_unlock(fs.m); 7474866e085SJohn Dyson fs.m = NULL; 748df8bae1dSRodney W. Grimes } 749df8bae1dSRodney W. Grimes } 75040360b1bSMatthew Dillon 751df8bae1dSRodney W. Grimes /* 7521c7c3c6aSMatthew Dillon * We get here if the object has default pager (or unwiring) 7531c7c3c6aSMatthew Dillon * or the pager doesn't have the page. 754df8bae1dSRodney W. Grimes */ 7554866e085SJohn Dyson if (fs.object == fs.first_object) 7564866e085SJohn Dyson fs.first_m = fs.m; 757df8bae1dSRodney W. Grimes 758df8bae1dSRodney W. Grimes /* 7590d94caffSDavid Greenman * Move on to the next object. Lock the next object before 7600d94caffSDavid Greenman * unlocking the current one. 761df8bae1dSRodney W. Grimes */ 7624866e085SJohn Dyson next_object = fs.object->backing_object; 763df8bae1dSRodney W. Grimes if (next_object == NULL) { 764df8bae1dSRodney W. Grimes /* 7650d94caffSDavid Greenman * If there's no object left, fill the page in the top 7660d94caffSDavid Greenman * object with zeros. 767df8bae1dSRodney W. Grimes */ 7684866e085SJohn Dyson if (fs.object != fs.first_object) { 7694866e085SJohn Dyson vm_object_pip_wakeup(fs.object); 77089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(fs.object); 771df8bae1dSRodney W. Grimes 7724866e085SJohn Dyson fs.object = fs.first_object; 7734866e085SJohn Dyson fs.pindex = fs.first_pindex; 7744866e085SJohn Dyson fs.m = fs.first_m; 77589f6b863SAttilio Rao VM_OBJECT_WLOCK(fs.object); 776df8bae1dSRodney W. Grimes } 7774866e085SJohn Dyson fs.first_m = NULL; 778df8bae1dSRodney W. Grimes 7794221e284SAlan Cox /* 7804221e284SAlan Cox * Zero the page if necessary and mark it valid. 7814221e284SAlan Cox */ 7824866e085SJohn Dyson if ((fs.m->flags & PG_ZERO) == 0) { 783fff6062aSAlan Cox pmap_zero_page(fs.m); 7844221e284SAlan Cox } else { 78567596082SAttilio Rao PCPU_INC(cnt.v_ozfod); 7864221e284SAlan Cox } 78767596082SAttilio Rao PCPU_INC(cnt.v_zfod); 7884221e284SAlan Cox fs.m->valid = VM_PAGE_BITS_ALL; 7897b9b301cSAlan Cox /* Don't try to prefault neighboring pages. */ 7907b9b301cSAlan Cox faultcount = 1; 7911c7c3c6aSMatthew Dillon break; /* break to PAGE HAS BEEN FOUND */ 7920d94caffSDavid Greenman } else { 793c8567c3aSAlan Cox KASSERT(fs.object != next_object, 794c8567c3aSAlan Cox ("object loop %p", next_object)); 79589f6b863SAttilio Rao VM_OBJECT_WLOCK(next_object); 796c8567c3aSAlan Cox vm_object_pip_add(next_object, 1); 797c8567c3aSAlan Cox if (fs.object != fs.first_object) 7984866e085SJohn Dyson vm_object_pip_wakeup(fs.object); 7996753423cSAlan Cox fs.pindex += 8006753423cSAlan Cox OFF_TO_IDX(fs.object->backing_object_offset); 80189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(fs.object); 8024866e085SJohn Dyson fs.object = next_object; 803df8bae1dSRodney W. Grimes } 804df8bae1dSRodney W. Grimes } 8051c7c3c6aSMatthew Dillon 806c7aebda8SAttilio Rao vm_page_assert_xbusied(fs.m); 807df8bae1dSRodney W. Grimes 808df8bae1dSRodney W. Grimes /* 8090d94caffSDavid Greenman * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock 810df8bae1dSRodney W. Grimes * is held.] 811df8bae1dSRodney W. Grimes */ 812df8bae1dSRodney W. Grimes 813df8bae1dSRodney W. Grimes /* 8140d94caffSDavid Greenman * If the page is being written, but isn't already owned by the 8150d94caffSDavid Greenman * top-level object, we have to copy it into a new page owned by the 8160d94caffSDavid Greenman * top-level object. 817df8bae1dSRodney W. Grimes */ 8184866e085SJohn Dyson if (fs.object != fs.first_object) { 819df8bae1dSRodney W. Grimes /* 8200d94caffSDavid Greenman * We only really need to copy if we want to write it. 821df8bae1dSRodney W. Grimes */ 822a6d42a0dSAlan Cox if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) { 823df8bae1dSRodney W. Grimes /* 8241c7c3c6aSMatthew Dillon * This allows pages to be virtually copied from a 8251c7c3c6aSMatthew Dillon * backing_object into the first_object, where the 8261c7c3c6aSMatthew Dillon * backing object has no other refs to it, and cannot 8271c7c3c6aSMatthew Dillon * gain any more refs. Instead of a bcopy, we just 8281c7c3c6aSMatthew Dillon * move the page from the backing object to the 8291c7c3c6aSMatthew Dillon * first object. Note that we must mark the page 8301c7c3c6aSMatthew Dillon * dirty in the first object so that it will go out 8311c7c3c6aSMatthew Dillon * to swap when needed. 832df8bae1dSRodney W. Grimes */ 833ebf75125SAlan Cox is_first_object_locked = FALSE; 834e50346b5SAlan Cox if ( 835de5f6a77SJohn Dyson /* 836de5f6a77SJohn Dyson * Only one shadow object 837de5f6a77SJohn Dyson */ 8384866e085SJohn Dyson (fs.object->shadow_count == 1) && 839de5f6a77SJohn Dyson /* 840de5f6a77SJohn Dyson * No COW refs, except us 841de5f6a77SJohn Dyson */ 8424866e085SJohn Dyson (fs.object->ref_count == 1) && 843de5f6a77SJohn Dyson /* 8445929bcfaSPhilippe Charnier * No one else can look this object up 845de5f6a77SJohn Dyson */ 8464866e085SJohn Dyson (fs.object->handle == NULL) && 847de5f6a77SJohn Dyson /* 848de5f6a77SJohn Dyson * No other ways to look the object up 849de5f6a77SJohn Dyson */ 8504866e085SJohn Dyson ((fs.object->type == OBJT_DEFAULT) || 8514866e085SJohn Dyson (fs.object->type == OBJT_SWAP)) && 85289f6b863SAttilio Rao (is_first_object_locked = VM_OBJECT_TRYWLOCK(fs.first_object)) && 853de5f6a77SJohn Dyson /* 854de5f6a77SJohn Dyson * We don't chase down the shadow chain 855de5f6a77SJohn Dyson */ 856e50346b5SAlan Cox fs.object == fs.first_object->backing_object) { 857bccdea45SAlan Cox vm_page_lock(fs.m); 858bccdea45SAlan Cox vm_page_remove(fs.m); 859bccdea45SAlan Cox vm_page_unlock(fs.m); 860eb00b276SAlan Cox vm_page_lock(fs.first_m); 861bccdea45SAlan Cox vm_page_replace_checked(fs.m, fs.first_object, 862bccdea45SAlan Cox fs.first_pindex, fs.first_m); 8636fee422eSConrad Meyer vm_page_free(fs.first_m); 8646fee422eSConrad Meyer vm_page_unlock(fs.first_m); 865bccdea45SAlan Cox vm_page_dirty(fs.m); 866dfdf9abdSAlan Cox #if VM_NRESERVLEVEL > 0 867dfdf9abdSAlan Cox /* 868dfdf9abdSAlan Cox * Rename the reservation. 869dfdf9abdSAlan Cox */ 870dfdf9abdSAlan Cox vm_reserv_rename(fs.m, fs.first_object, 871dfdf9abdSAlan Cox fs.object, OFF_TO_IDX( 872dfdf9abdSAlan Cox fs.first_object->backing_object_offset)); 873dfdf9abdSAlan Cox #endif 874bccdea45SAlan Cox /* 875bccdea45SAlan Cox * Removing the page from the backing object 876bccdea45SAlan Cox * unbusied it. 877bccdea45SAlan Cox */ 878c7aebda8SAttilio Rao vm_page_xbusy(fs.m); 879d98ddc46SAlan Cox fs.first_m = fs.m; 8804866e085SJohn Dyson fs.m = NULL; 88167596082SAttilio Rao PCPU_INC(cnt.v_cow_optim); 882de5f6a77SJohn Dyson } else { 883de5f6a77SJohn Dyson /* 884de5f6a77SJohn Dyson * Oh, well, lets copy it. 885de5f6a77SJohn Dyson */ 886669890eaSAlan Cox pmap_copy_page(fs.m, fs.first_m); 887669890eaSAlan Cox fs.first_m->valid = VM_PAGE_BITS_ALL; 888d8778512SAlan Cox if (wired && (fault_flags & 8896a875bf9SKonstantin Belousov VM_FAULT_WIRE) == 0) { 8902965a453SKip Macy vm_page_lock(fs.first_m); 891d8778512SAlan Cox vm_page_wire(fs.first_m); 8922965a453SKip Macy vm_page_unlock(fs.first_m); 8932965a453SKip Macy 8942965a453SKip Macy vm_page_lock(fs.m); 8953ae10f74SAttilio Rao vm_page_unwire(fs.m, PQ_INACTIVE); 8962965a453SKip Macy vm_page_unlock(fs.m); 897de5f6a77SJohn Dyson } 898df8bae1dSRodney W. Grimes /* 899df8bae1dSRodney W. Grimes * We no longer need the old page or object. 900df8bae1dSRodney W. Grimes */ 9014866e085SJohn Dyson release_page(&fs); 902de5f6a77SJohn Dyson } 9031c7c3c6aSMatthew Dillon /* 9041c7c3c6aSMatthew Dillon * fs.object != fs.first_object due to above 9051c7c3c6aSMatthew Dillon * conditional 9061c7c3c6aSMatthew Dillon */ 9074866e085SJohn Dyson vm_object_pip_wakeup(fs.object); 90889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(fs.object); 909df8bae1dSRodney W. Grimes /* 910df8bae1dSRodney W. Grimes * Only use the new page below... 911df8bae1dSRodney W. Grimes */ 9124866e085SJohn Dyson fs.object = fs.first_object; 9134866e085SJohn Dyson fs.pindex = fs.first_pindex; 914d98ddc46SAlan Cox fs.m = fs.first_m; 915f29ba63eSAlan Cox if (!is_first_object_locked) 91689f6b863SAttilio Rao VM_OBJECT_WLOCK(fs.object); 91767596082SAttilio Rao PCPU_INC(cnt.v_cow_faults); 9184d34e019SKonstantin Belousov curthread->td_cow++; 9190d94caffSDavid Greenman } else { 920df8bae1dSRodney W. Grimes prot &= ~VM_PROT_WRITE; 921df8bae1dSRodney W. Grimes } 922df8bae1dSRodney W. Grimes } 923df8bae1dSRodney W. Grimes 924df8bae1dSRodney W. Grimes /* 9250d94caffSDavid Greenman * We must verify that the maps have not changed since our last 9260d94caffSDavid Greenman * lookup. 927df8bae1dSRodney W. Grimes */ 92819dc5607STor Egge if (!fs.lookup_still_valid) { 929df8bae1dSRodney W. Grimes vm_object_t retry_object; 930a316d390SJohn Dyson vm_pindex_t retry_pindex; 931df8bae1dSRodney W. Grimes vm_prot_t retry_prot; 932df8bae1dSRodney W. Grimes 93319dc5607STor Egge if (!vm_map_trylock_read(fs.map)) { 934b823bbd6SMatthew Dillon release_page(&fs); 935b823bbd6SMatthew Dillon unlock_and_deallocate(&fs); 936b823bbd6SMatthew Dillon goto RetryFault; 937b823bbd6SMatthew Dillon } 93819dc5607STor Egge fs.lookup_still_valid = TRUE; 93919dc5607STor Egge if (fs.map->timestamp != map_generation) { 94019dc5607STor Egge result = vm_map_lookup_locked(&fs.map, vaddr, fault_type, 9414866e085SJohn Dyson &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired); 942df8bae1dSRodney W. Grimes 943df8bae1dSRodney W. Grimes /* 94444ed3417STor Egge * If we don't need the page any longer, put it on the inactive 9450d94caffSDavid Greenman * list (the easiest thing to do here). If no one needs it, 9460d94caffSDavid Greenman * pageout will grab it eventually. 947df8bae1dSRodney W. Grimes */ 948df8bae1dSRodney W. Grimes if (result != KERN_SUCCESS) { 9494866e085SJohn Dyson release_page(&fs); 9504866e085SJohn Dyson unlock_and_deallocate(&fs); 95119dc5607STor Egge 95219dc5607STor Egge /* 95319dc5607STor Egge * If retry of map lookup would have blocked then 95419dc5607STor Egge * retry fault from start. 95519dc5607STor Egge */ 95619dc5607STor Egge if (result == KERN_FAILURE) 95719dc5607STor Egge goto RetryFault; 958df8bae1dSRodney W. Grimes return (result); 959df8bae1dSRodney W. Grimes } 9604866e085SJohn Dyson if ((retry_object != fs.first_object) || 9614866e085SJohn Dyson (retry_pindex != fs.first_pindex)) { 9624866e085SJohn Dyson release_page(&fs); 9634866e085SJohn Dyson unlock_and_deallocate(&fs); 964df8bae1dSRodney W. Grimes goto RetryFault; 965df8bae1dSRodney W. Grimes } 96619dc5607STor Egge 967df8bae1dSRodney W. Grimes /* 9680d94caffSDavid Greenman * Check whether the protection has changed or the object has 9690d94caffSDavid Greenman * been copied while we left the map unlocked. Changing from 9700d94caffSDavid Greenman * read to write permission is OK - we leave the page 9710d94caffSDavid Greenman * write-protected, and catch the write fault. Changing from 9720d94caffSDavid Greenman * write to read permission means that we can't mark the page 9730d94caffSDavid Greenman * write-enabled after all. 974df8bae1dSRodney W. Grimes */ 975df8bae1dSRodney W. Grimes prot &= retry_prot; 976df8bae1dSRodney W. Grimes } 97719dc5607STor Egge } 978381b7242SAlan Cox 979d2bf64c3SKonstantin Belousov /* 980381b7242SAlan Cox * If the page was filled by a pager, save the virtual address that 981381b7242SAlan Cox * should be faulted on next under a sequential access pattern to the 982381b7242SAlan Cox * map entry. A read lock on the map suffices to update this address 983381b7242SAlan Cox * safely. 984d2bf64c3SKonstantin Belousov */ 9855758fe71SAlan Cox if (hardfault) 986381b7242SAlan Cox fs.entry->next_read = vaddr + ptoa(ahead) + PAGE_SIZE; 987d2bf64c3SKonstantin Belousov 988a36f5532SKonstantin Belousov vm_fault_dirty(fs.entry, fs.m, prot, fault_type, fault_flags, TRUE); 989c7aebda8SAttilio Rao vm_page_assert_xbusied(fs.m); 990c7aebda8SAttilio Rao 9914221e284SAlan Cox /* 99278cfe1f7SAlan Cox * Page must be completely valid or it is not fit to 9934221e284SAlan Cox * map into user space. vm_pager_get_pages() ensures this. 9944221e284SAlan Cox */ 99578cfe1f7SAlan Cox KASSERT(fs.m->valid == VM_PAGE_BITS_ALL, 99678cfe1f7SAlan Cox ("vm_fault: page %p partially invalid", fs.m)); 99789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(fs.object); 998cbfbaad8SAlan Cox 99986735996SAlan Cox /* 100086735996SAlan Cox * Put this page into the physical map. We had to do the unlock above 100186735996SAlan Cox * because pmap_enter() may sleep. We don't put the page 100286735996SAlan Cox * back on the active queue until later so that the pageout daemon 100386735996SAlan Cox * won't find it (yet). 100486735996SAlan Cox */ 100539ffa8c1SKonstantin Belousov pmap_enter(fs.map->pmap, vaddr, fs.m, prot, 100639ffa8c1SKonstantin Belousov fault_type | (wired ? PMAP_ENTER_WIRED : 0), 0); 10076a875bf9SKonstantin Belousov if (faultcount != 1 && (fault_flags & VM_FAULT_WIRE) == 0 && 10087b9b301cSAlan Cox wired == 0) 1009b0cd2017SGleb Smirnoff vm_fault_prefault(&fs, vaddr, 1010b0cd2017SGleb Smirnoff faultcount > 0 ? behind : PFBAK, 1011b0cd2017SGleb Smirnoff faultcount > 0 ? ahead : PFFOR); 101289f6b863SAttilio Rao VM_OBJECT_WLOCK(fs.object); 10132965a453SKip Macy vm_page_lock(fs.m); 1014ff97964aSJohn Dyson 1015df8bae1dSRodney W. Grimes /* 10160d94caffSDavid Greenman * If the page is not wired down, then put it where the pageout daemon 10170d94caffSDavid Greenman * can find it. 1018df8bae1dSRodney W. Grimes */ 10196a875bf9SKonstantin Belousov if ((fault_flags & VM_FAULT_WIRE) != 0) { 10206a875bf9SKonstantin Belousov KASSERT(wired, ("VM_FAULT_WIRE && !wired")); 10214866e085SJohn Dyson vm_page_wire(fs.m); 102203679e23SAlan Cox } else 10234866e085SJohn Dyson vm_page_activate(fs.m); 1024acd11c74SAlan Cox if (m_hold != NULL) { 1025acd11c74SAlan Cox *m_hold = fs.m; 1026acd11c74SAlan Cox vm_page_hold(fs.m); 1027acd11c74SAlan Cox } 10282965a453SKip Macy vm_page_unlock(fs.m); 1029c7aebda8SAttilio Rao vm_page_xunbusy(fs.m); 1030eeec6babSJohn Baldwin 1031eebf3286SAlan Cox /* 1032eebf3286SAlan Cox * Unlock everything, and return 1033eebf3286SAlan Cox */ 1034eebf3286SAlan Cox unlock_and_deallocate(&fs); 1035b3a01bdfSAndrey Zonov if (hardfault) { 1036b3a01bdfSAndrey Zonov PCPU_INC(cnt.v_io_faults); 10371c4bcd05SJeff Roberson curthread->td_ru.ru_majflt++; 1038ae34b6ffSEdward Tomasz Napierala #ifdef RACCT 1039ae34b6ffSEdward Tomasz Napierala if (racct_enable && fs.object->type == OBJT_VNODE) { 1040ae34b6ffSEdward Tomasz Napierala PROC_LOCK(curproc); 1041ae34b6ffSEdward Tomasz Napierala if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) { 1042ae34b6ffSEdward Tomasz Napierala racct_add_force(curproc, RACCT_WRITEBPS, 1043ae34b6ffSEdward Tomasz Napierala PAGE_SIZE + behind * PAGE_SIZE); 1044ae34b6ffSEdward Tomasz Napierala racct_add_force(curproc, RACCT_WRITEIOPS, 1); 1045ae34b6ffSEdward Tomasz Napierala } else { 1046ae34b6ffSEdward Tomasz Napierala racct_add_force(curproc, RACCT_READBPS, 1047ae34b6ffSEdward Tomasz Napierala PAGE_SIZE + ahead * PAGE_SIZE); 1048ae34b6ffSEdward Tomasz Napierala racct_add_force(curproc, RACCT_READIOPS, 1); 1049ae34b6ffSEdward Tomasz Napierala } 1050ae34b6ffSEdward Tomasz Napierala PROC_UNLOCK(curproc); 1051ae34b6ffSEdward Tomasz Napierala } 1052ae34b6ffSEdward Tomasz Napierala #endif 1053b3a01bdfSAndrey Zonov } else 10541c4bcd05SJeff Roberson curthread->td_ru.ru_minflt++; 1055df8bae1dSRodney W. Grimes 1056df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 1057df8bae1dSRodney W. Grimes } 1058df8bae1dSRodney W. Grimes 1059df8bae1dSRodney W. Grimes /* 1060a8b0f100SAlan Cox * Speed up the reclamation of pages that precede the faulting pindex within 1061a8b0f100SAlan Cox * the first object of the shadow chain. Essentially, perform the equivalent 1062a8b0f100SAlan Cox * to madvise(..., MADV_DONTNEED) on a large cluster of pages that precedes 1063a8b0f100SAlan Cox * the faulting pindex by the cluster size when the pages read by vm_fault() 1064a8b0f100SAlan Cox * cross a cluster-size boundary. The cluster size is the greater of the 1065a8b0f100SAlan Cox * smallest superpage size and VM_FAULT_DONTNEED_MIN. 1066a8b0f100SAlan Cox * 1067a8b0f100SAlan Cox * When "fs->first_object" is a shadow object, the pages in the backing object 1068a8b0f100SAlan Cox * that precede the faulting pindex are deactivated by vm_fault(). So, this 1069a8b0f100SAlan Cox * function must only be concerned with pages in the first object. 107013458803SAlan Cox */ 107113458803SAlan Cox static void 1072a8b0f100SAlan Cox vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr, int ahead) 107313458803SAlan Cox { 1074a8b0f100SAlan Cox vm_map_entry_t entry; 107513458803SAlan Cox vm_object_t first_object, object; 1076a8b0f100SAlan Cox vm_offset_t end, start; 1077a8b0f100SAlan Cox vm_page_t m, m_next; 1078a8b0f100SAlan Cox vm_pindex_t pend, pstart; 1079a8b0f100SAlan Cox vm_size_t size; 108013458803SAlan Cox 108113458803SAlan Cox object = fs->object; 108289f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 108313458803SAlan Cox first_object = fs->first_object; 108413458803SAlan Cox if (first_object != object) { 1085b5ab20c0SAlan Cox if (!VM_OBJECT_TRYWLOCK(first_object)) { 108689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1087b5ab20c0SAlan Cox VM_OBJECT_WLOCK(first_object); 108889f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 108913458803SAlan Cox } 109013458803SAlan Cox } 1091a8b0f100SAlan Cox /* Neither fictitious nor unmanaged pages can be reclaimed. */ 109228634820SAlan Cox if ((first_object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0) { 1093a8b0f100SAlan Cox size = VM_FAULT_DONTNEED_MIN; 1094a8b0f100SAlan Cox if (MAXPAGESIZES > 1 && size < pagesizes[1]) 1095a8b0f100SAlan Cox size = pagesizes[1]; 1096a8b0f100SAlan Cox end = rounddown2(vaddr, size); 1097a8b0f100SAlan Cox if (vaddr - end >= size - PAGE_SIZE - ptoa(ahead) && 1098a8b0f100SAlan Cox (entry = fs->entry)->start < end) { 1099a8b0f100SAlan Cox if (end - entry->start < size) 1100a8b0f100SAlan Cox start = entry->start; 110113458803SAlan Cox else 1102a8b0f100SAlan Cox start = end - size; 1103a8b0f100SAlan Cox pmap_advise(fs->map->pmap, start, end, MADV_DONTNEED); 1104a8b0f100SAlan Cox pstart = OFF_TO_IDX(entry->offset) + atop(start - 1105a8b0f100SAlan Cox entry->start); 1106a8b0f100SAlan Cox m_next = vm_page_find_least(first_object, pstart); 1107a8b0f100SAlan Cox pend = OFF_TO_IDX(entry->offset) + atop(end - 1108a8b0f100SAlan Cox entry->start); 1109a8b0f100SAlan Cox while ((m = m_next) != NULL && m->pindex < pend) { 1110a8b0f100SAlan Cox m_next = TAILQ_NEXT(m, listq); 1111a8b0f100SAlan Cox if (m->valid != VM_PAGE_BITS_ALL || 1112a8b0f100SAlan Cox vm_page_busied(m)) 111313458803SAlan Cox continue; 1114d8015db3SAlan Cox 1115d8015db3SAlan Cox /* 1116d8015db3SAlan Cox * Don't clear PGA_REFERENCED, since it would 1117d8015db3SAlan Cox * likely represent a reference by a different 1118d8015db3SAlan Cox * process. 1119d8015db3SAlan Cox * 1120d8015db3SAlan Cox * Typically, at this point, prefetched pages 1121d8015db3SAlan Cox * are still in the inactive queue. Only 1122d8015db3SAlan Cox * pages that triggered page faults are in the 1123d8015db3SAlan Cox * active queue. 1124d8015db3SAlan Cox */ 112513458803SAlan Cox vm_page_lock(m); 1126d8015db3SAlan Cox vm_page_deactivate(m); 112713458803SAlan Cox vm_page_unlock(m); 112813458803SAlan Cox } 112913458803SAlan Cox } 1130a8b0f100SAlan Cox } 113113458803SAlan Cox if (first_object != object) 1132b5ab20c0SAlan Cox VM_OBJECT_WUNLOCK(first_object); 113313458803SAlan Cox } 113413458803SAlan Cox 113513458803SAlan Cox /* 1136566526a9SAlan Cox * vm_fault_prefault provides a quick way of clustering 1137566526a9SAlan Cox * pagefaults into a processes address space. It is a "cousin" 1138566526a9SAlan Cox * of vm_map_pmap_enter, except it runs at page fault time instead 1139566526a9SAlan Cox * of mmap time. 1140566526a9SAlan Cox */ 1141566526a9SAlan Cox static void 114263281952SAlan Cox vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra, 1143b0cd2017SGleb Smirnoff int backward, int forward) 1144566526a9SAlan Cox { 114563281952SAlan Cox pmap_t pmap; 114663281952SAlan Cox vm_map_entry_t entry; 114763281952SAlan Cox vm_object_t backing_object, lobject; 1148566526a9SAlan Cox vm_offset_t addr, starta; 1149566526a9SAlan Cox vm_pindex_t pindex; 11502053c127SStephan Uphoff vm_page_t m; 1151b0cd2017SGleb Smirnoff int i; 1152566526a9SAlan Cox 115363281952SAlan Cox pmap = fs->map->pmap; 1154950d5f7aSAlan Cox if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace)) 1155566526a9SAlan Cox return; 1156566526a9SAlan Cox 115763281952SAlan Cox entry = fs->entry; 1158566526a9SAlan Cox 115963281952SAlan Cox starta = addra - backward * PAGE_SIZE; 1160566526a9SAlan Cox if (starta < entry->start) { 1161566526a9SAlan Cox starta = entry->start; 1162566526a9SAlan Cox } else if (starta > addra) { 1163566526a9SAlan Cox starta = 0; 1164566526a9SAlan Cox } 1165566526a9SAlan Cox 116663281952SAlan Cox /* 116763281952SAlan Cox * Generate the sequence of virtual addresses that are candidates for 116863281952SAlan Cox * prefaulting in an outward spiral from the faulting virtual address, 116963281952SAlan Cox * "addra". Specifically, the sequence is "addra - PAGE_SIZE", "addra 117063281952SAlan Cox * + PAGE_SIZE", "addra - 2 * PAGE_SIZE", "addra + 2 * PAGE_SIZE", ... 117163281952SAlan Cox * If the candidate address doesn't have a backing physical page, then 117263281952SAlan Cox * the loop immediately terminates. 117363281952SAlan Cox */ 117463281952SAlan Cox for (i = 0; i < 2 * imax(backward, forward); i++) { 117563281952SAlan Cox addr = addra + ((i >> 1) + 1) * ((i & 1) == 0 ? -PAGE_SIZE : 117663281952SAlan Cox PAGE_SIZE); 117763281952SAlan Cox if (addr > addra + forward * PAGE_SIZE) 1178566526a9SAlan Cox addr = 0; 1179566526a9SAlan Cox 1180566526a9SAlan Cox if (addr < starta || addr >= entry->end) 1181566526a9SAlan Cox continue; 1182566526a9SAlan Cox 1183566526a9SAlan Cox if (!pmap_is_prefaultable(pmap, addr)) 1184566526a9SAlan Cox continue; 1185566526a9SAlan Cox 1186566526a9SAlan Cox pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT; 118763281952SAlan Cox lobject = entry->object.vm_object; 1188c141ae7fSAlan Cox VM_OBJECT_RLOCK(lobject); 1189566526a9SAlan Cox while ((m = vm_page_lookup(lobject, pindex)) == NULL && 1190566526a9SAlan Cox lobject->type == OBJT_DEFAULT && 1191566526a9SAlan Cox (backing_object = lobject->backing_object) != NULL) { 119236930fc9SAlan Cox KASSERT((lobject->backing_object_offset & PAGE_MASK) == 119336930fc9SAlan Cox 0, ("vm_fault_prefault: unaligned object offset")); 1194566526a9SAlan Cox pindex += lobject->backing_object_offset >> PAGE_SHIFT; 1195c141ae7fSAlan Cox VM_OBJECT_RLOCK(backing_object); 1196c141ae7fSAlan Cox VM_OBJECT_RUNLOCK(lobject); 1197566526a9SAlan Cox lobject = backing_object; 1198566526a9SAlan Cox } 1199cbfbaad8SAlan Cox if (m == NULL) { 1200c141ae7fSAlan Cox VM_OBJECT_RUNLOCK(lobject); 1201566526a9SAlan Cox break; 1202cbfbaad8SAlan Cox } 12030a2e596aSAlan Cox if (m->valid == VM_PAGE_BITS_ALL && 12043c4a2440SAlan Cox (m->flags & PG_FICTITIOUS) == 0) 12057bfda801SAlan Cox pmap_enter_quick(pmap, addr, m, entry->protection); 1206c141ae7fSAlan Cox VM_OBJECT_RUNLOCK(lobject); 1207566526a9SAlan Cox } 1208566526a9SAlan Cox } 1209566526a9SAlan Cox 1210566526a9SAlan Cox /* 121182de724fSAlan Cox * Hold each of the physical pages that are mapped by the specified range of 121282de724fSAlan Cox * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid 121382de724fSAlan Cox * and allow the specified types of access, "prot". If all of the implied 121482de724fSAlan Cox * pages are successfully held, then the number of held pages is returned 121582de724fSAlan Cox * together with pointers to those pages in the array "ma". However, if any 121682de724fSAlan Cox * of the pages cannot be held, -1 is returned. 121782de724fSAlan Cox */ 121882de724fSAlan Cox int 121982de724fSAlan Cox vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len, 122082de724fSAlan Cox vm_prot_t prot, vm_page_t *ma, int max_count) 122182de724fSAlan Cox { 122282de724fSAlan Cox vm_offset_t end, va; 122382de724fSAlan Cox vm_page_t *mp; 12247e14088dSKonstantin Belousov int count; 122582de724fSAlan Cox boolean_t pmap_failed; 122682de724fSAlan Cox 1227af32c419SKonstantin Belousov if (len == 0) 1228af32c419SKonstantin Belousov return (0); 122982de724fSAlan Cox end = round_page(addr + len); 123082de724fSAlan Cox addr = trunc_page(addr); 123182de724fSAlan Cox 123282de724fSAlan Cox /* 123382de724fSAlan Cox * Check for illegal addresses. 123482de724fSAlan Cox */ 123582de724fSAlan Cox if (addr < vm_map_min(map) || addr > end || end > vm_map_max(map)) 123682de724fSAlan Cox return (-1); 123782de724fSAlan Cox 12387e14088dSKonstantin Belousov if (atop(end - addr) > max_count) 123982de724fSAlan Cox panic("vm_fault_quick_hold_pages: count > max_count"); 12407e14088dSKonstantin Belousov count = atop(end - addr); 124182de724fSAlan Cox 124282de724fSAlan Cox /* 124382de724fSAlan Cox * Most likely, the physical pages are resident in the pmap, so it is 124482de724fSAlan Cox * faster to try pmap_extract_and_hold() first. 124582de724fSAlan Cox */ 124682de724fSAlan Cox pmap_failed = FALSE; 124782de724fSAlan Cox for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) { 124882de724fSAlan Cox *mp = pmap_extract_and_hold(map->pmap, va, prot); 124982de724fSAlan Cox if (*mp == NULL) 125082de724fSAlan Cox pmap_failed = TRUE; 125182de724fSAlan Cox else if ((prot & VM_PROT_WRITE) != 0 && 1252a5dbab54SAlan Cox (*mp)->dirty != VM_PAGE_BITS_ALL) { 125382de724fSAlan Cox /* 125482de724fSAlan Cox * Explicitly dirty the physical page. Otherwise, the 125582de724fSAlan Cox * caller's changes may go unnoticed because they are 125682de724fSAlan Cox * performed through an unmanaged mapping or by a DMA 125782de724fSAlan Cox * operation. 12583c76db4cSAlan Cox * 1259abb9b935SKonstantin Belousov * The object lock is not held here. 1260abb9b935SKonstantin Belousov * See vm_page_clear_dirty_mask(). 126182de724fSAlan Cox */ 12623c76db4cSAlan Cox vm_page_dirty(*mp); 126382de724fSAlan Cox } 126482de724fSAlan Cox } 126582de724fSAlan Cox if (pmap_failed) { 126682de724fSAlan Cox /* 126782de724fSAlan Cox * One or more pages could not be held by the pmap. Either no 126882de724fSAlan Cox * page was mapped at the specified virtual address or that 126982de724fSAlan Cox * mapping had insufficient permissions. Attempt to fault in 127082de724fSAlan Cox * and hold these pages. 127182de724fSAlan Cox */ 127282de724fSAlan Cox for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) 1273be996836SAttilio Rao if (*mp == NULL && vm_fault_hold(map, va, prot, 127482de724fSAlan Cox VM_FAULT_NORMAL, mp) != KERN_SUCCESS) 127582de724fSAlan Cox goto error; 127682de724fSAlan Cox } 127782de724fSAlan Cox return (count); 127882de724fSAlan Cox error: 127982de724fSAlan Cox for (mp = ma; mp < ma + count; mp++) 128082de724fSAlan Cox if (*mp != NULL) { 128182de724fSAlan Cox vm_page_lock(*mp); 128282de724fSAlan Cox vm_page_unhold(*mp); 128382de724fSAlan Cox vm_page_unlock(*mp); 128482de724fSAlan Cox } 128582de724fSAlan Cox return (-1); 128682de724fSAlan Cox } 128782de724fSAlan Cox 128882de724fSAlan Cox /* 1289df8bae1dSRodney W. Grimes * Routine: 1290df8bae1dSRodney W. Grimes * vm_fault_copy_entry 1291df8bae1dSRodney W. Grimes * Function: 1292210a6886SKonstantin Belousov * Create new shadow object backing dst_entry with private copy of 1293210a6886SKonstantin Belousov * all underlying pages. When src_entry is equal to dst_entry, 1294210a6886SKonstantin Belousov * function implements COW for wired-down map entry. Otherwise, 1295210a6886SKonstantin Belousov * it forks wired entry into dst_map. 1296df8bae1dSRodney W. Grimes * 1297df8bae1dSRodney W. Grimes * In/out conditions: 1298df8bae1dSRodney W. Grimes * The source and destination maps must be locked for write. 1299df8bae1dSRodney W. Grimes * The source map entry must be wired down (or be a sharing map 1300df8bae1dSRodney W. Grimes * entry corresponding to a main map entry that is wired down). 1301df8bae1dSRodney W. Grimes */ 130226f9a767SRodney W. Grimes void 1303121fd461SKonstantin Belousov vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map, 1304121fd461SKonstantin Belousov vm_map_entry_t dst_entry, vm_map_entry_t src_entry, 1305121fd461SKonstantin Belousov vm_ooffset_t *fork_charge) 1306df8bae1dSRodney W. Grimes { 1307210a6886SKonstantin Belousov vm_object_t backing_object, dst_object, object, src_object; 13087afab86cSAlan Cox vm_pindex_t dst_pindex, pindex, src_pindex; 1309210a6886SKonstantin Belousov vm_prot_t access, prot; 1310df8bae1dSRodney W. Grimes vm_offset_t vaddr; 1311df8bae1dSRodney W. Grimes vm_page_t dst_m; 1312df8bae1dSRodney W. Grimes vm_page_t src_m; 13134c74acf7SKonstantin Belousov boolean_t upgrade; 1314df8bae1dSRodney W. Grimes 1315df8bae1dSRodney W. Grimes #ifdef lint 1316df8bae1dSRodney W. Grimes src_map++; 13170d94caffSDavid Greenman #endif /* lint */ 1318df8bae1dSRodney W. Grimes 1319210a6886SKonstantin Belousov upgrade = src_entry == dst_entry; 13200973283dSKonstantin Belousov access = prot = dst_entry->protection; 1321210a6886SKonstantin Belousov 1322df8bae1dSRodney W. Grimes src_object = src_entry->object.vm_object; 13237afab86cSAlan Cox src_pindex = OFF_TO_IDX(src_entry->offset); 1324df8bae1dSRodney W. Grimes 13250973283dSKonstantin Belousov if (upgrade && (dst_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) { 13260973283dSKonstantin Belousov dst_object = src_object; 13270973283dSKonstantin Belousov vm_object_reference(dst_object); 13280973283dSKonstantin Belousov } else { 1329df8bae1dSRodney W. Grimes /* 13300d94caffSDavid Greenman * Create the top-level object for the destination entry. (Doesn't 13310d94caffSDavid Greenman * actually shadow anything - we copy the pages directly.) 1332df8bae1dSRodney W. Grimes */ 133324a1cce3SDavid Greenman dst_object = vm_object_allocate(OBJT_DEFAULT, 133457b5187bSAlan Cox OFF_TO_IDX(dst_entry->end - dst_entry->start)); 1335f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 1336f8a47341SAlan Cox dst_object->flags |= OBJ_COLORED; 1337f8a47341SAlan Cox dst_object->pg_color = atop(dst_entry->start); 1338f8a47341SAlan Cox #endif 13390973283dSKonstantin Belousov } 1340df8bae1dSRodney W. Grimes 134189f6b863SAttilio Rao VM_OBJECT_WLOCK(dst_object); 1342210a6886SKonstantin Belousov KASSERT(upgrade || dst_entry->object.vm_object == NULL, 1343121fd461SKonstantin Belousov ("vm_fault_copy_entry: vm_object not NULL")); 13440973283dSKonstantin Belousov if (src_object != dst_object) { 1345df8bae1dSRodney W. Grimes dst_entry->object.vm_object = dst_object; 1346df8bae1dSRodney W. Grimes dst_entry->offset = 0; 13473364c323SKonstantin Belousov dst_object->charge = dst_entry->end - dst_entry->start; 13480973283dSKonstantin Belousov } 1349210a6886SKonstantin Belousov if (fork_charge != NULL) { 1350ef694c1aSEdward Tomasz Napierala KASSERT(dst_entry->cred == NULL, 1351121fd461SKonstantin Belousov ("vm_fault_copy_entry: leaked swp charge")); 1352ef694c1aSEdward Tomasz Napierala dst_object->cred = curthread->td_ucred; 1353ef694c1aSEdward Tomasz Napierala crhold(dst_object->cred); 1354121fd461SKonstantin Belousov *fork_charge += dst_object->charge; 13550973283dSKonstantin Belousov } else if (dst_object->cred == NULL) { 13560973283dSKonstantin Belousov KASSERT(dst_entry->cred != NULL, ("no cred for entry %p", 13570973283dSKonstantin Belousov dst_entry)); 1358ef694c1aSEdward Tomasz Napierala dst_object->cred = dst_entry->cred; 1359ef694c1aSEdward Tomasz Napierala dst_entry->cred = NULL; 1360210a6886SKonstantin Belousov } 13610973283dSKonstantin Belousov 1362210a6886SKonstantin Belousov /* 1363210a6886SKonstantin Belousov * If not an upgrade, then enter the mappings in the pmap as 1364210a6886SKonstantin Belousov * read and/or execute accesses. Otherwise, enter them as 1365210a6886SKonstantin Belousov * write accesses. 1366210a6886SKonstantin Belousov * 1367210a6886SKonstantin Belousov * A writeable large page mapping is only created if all of 1368210a6886SKonstantin Belousov * the constituent small page mappings are modified. Marking 1369210a6886SKonstantin Belousov * PTEs as modified on inception allows promotion to happen 1370210a6886SKonstantin Belousov * without taking potentially large number of soft faults. 1371210a6886SKonstantin Belousov */ 1372210a6886SKonstantin Belousov if (!upgrade) 1373210a6886SKonstantin Belousov access &= ~VM_PROT_WRITE; 1374df8bae1dSRodney W. Grimes 1375df8bae1dSRodney W. Grimes /* 1376ef45823eSKonstantin Belousov * Loop through all of the virtual pages within the entry's 1377ef45823eSKonstantin Belousov * range, copying each page from the source object to the 1378ef45823eSKonstantin Belousov * destination object. Since the source is wired, those pages 1379ef45823eSKonstantin Belousov * must exist. In contrast, the destination is pageable. 1380ef45823eSKonstantin Belousov * Since the destination object does share any backing storage 1381ef45823eSKonstantin Belousov * with the source object, all of its pages must be dirtied, 1382ef45823eSKonstantin Belousov * regardless of whether they can be written. 1383df8bae1dSRodney W. Grimes */ 13847afab86cSAlan Cox for (vaddr = dst_entry->start, dst_pindex = 0; 1385df8bae1dSRodney W. Grimes vaddr < dst_entry->end; 13867afab86cSAlan Cox vaddr += PAGE_SIZE, dst_pindex++) { 13870973283dSKonstantin Belousov again: 1388df8bae1dSRodney W. Grimes /* 1389df8bae1dSRodney W. Grimes * Find the page in the source object, and copy it in. 13904c74acf7SKonstantin Belousov * Because the source is wired down, the page will be 13914c74acf7SKonstantin Belousov * in memory. 1392df8bae1dSRodney W. Grimes */ 13930973283dSKonstantin Belousov if (src_object != dst_object) 139483b375eaSAttilio Rao VM_OBJECT_RLOCK(src_object); 1395c5b65a67SAlan Cox object = src_object; 13967afab86cSAlan Cox pindex = src_pindex + dst_pindex; 13977afab86cSAlan Cox while ((src_m = vm_page_lookup(object, pindex)) == NULL && 1398c5b65a67SAlan Cox (backing_object = object->backing_object) != NULL) { 1399c5b65a67SAlan Cox /* 14004c74acf7SKonstantin Belousov * Unless the source mapping is read-only or 14014c74acf7SKonstantin Belousov * it is presently being upgraded from 14024c74acf7SKonstantin Belousov * read-only, the first object in the shadow 14034c74acf7SKonstantin Belousov * chain should provide all of the pages. In 14044c74acf7SKonstantin Belousov * other words, this loop body should never be 14054c74acf7SKonstantin Belousov * executed when the source mapping is already 14064c74acf7SKonstantin Belousov * read/write. 1407c5b65a67SAlan Cox */ 14084c74acf7SKonstantin Belousov KASSERT((src_entry->protection & VM_PROT_WRITE) == 0 || 14094c74acf7SKonstantin Belousov upgrade, 14104c74acf7SKonstantin Belousov ("vm_fault_copy_entry: main object missing page")); 14114c74acf7SKonstantin Belousov 141283b375eaSAttilio Rao VM_OBJECT_RLOCK(backing_object); 1413c5b65a67SAlan Cox pindex += OFF_TO_IDX(object->backing_object_offset); 14140973283dSKonstantin Belousov if (object != dst_object) 141583b375eaSAttilio Rao VM_OBJECT_RUNLOCK(object); 1416c5b65a67SAlan Cox object = backing_object; 1417c5b65a67SAlan Cox } 14184c74acf7SKonstantin Belousov KASSERT(src_m != NULL, ("vm_fault_copy_entry: page missing")); 14190973283dSKonstantin Belousov 14200973283dSKonstantin Belousov if (object != dst_object) { 14210973283dSKonstantin Belousov /* 14220973283dSKonstantin Belousov * Allocate a page in the destination object. 14230973283dSKonstantin Belousov */ 14242602a2eaSKonstantin Belousov dst_m = vm_page_alloc(dst_object, (src_object == 14252602a2eaSKonstantin Belousov dst_object ? src_pindex : 0) + dst_pindex, 14262602a2eaSKonstantin Belousov VM_ALLOC_NORMAL); 14270973283dSKonstantin Belousov if (dst_m == NULL) { 14280973283dSKonstantin Belousov VM_OBJECT_WUNLOCK(dst_object); 14290973283dSKonstantin Belousov VM_OBJECT_RUNLOCK(object); 14300973283dSKonstantin Belousov VM_WAIT; 1431c8f780e3SKonstantin Belousov VM_OBJECT_WLOCK(dst_object); 14320973283dSKonstantin Belousov goto again; 14330973283dSKonstantin Belousov } 1434669890eaSAlan Cox pmap_copy_page(src_m, dst_m); 143583b375eaSAttilio Rao VM_OBJECT_RUNLOCK(object); 1436669890eaSAlan Cox dst_m->valid = VM_PAGE_BITS_ALL; 1437bc79b37fSKonstantin Belousov dst_m->dirty = VM_PAGE_BITS_ALL; 14380973283dSKonstantin Belousov } else { 14390973283dSKonstantin Belousov dst_m = src_m; 14400973283dSKonstantin Belousov if (vm_page_sleep_if_busy(dst_m, "fltupg")) 14410973283dSKonstantin Belousov goto again; 14420973283dSKonstantin Belousov vm_page_xbusy(dst_m); 14430973283dSKonstantin Belousov KASSERT(dst_m->valid == VM_PAGE_BITS_ALL, 14440973283dSKonstantin Belousov ("invalid dst page %p", dst_m)); 14450973283dSKonstantin Belousov } 144689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(dst_object); 1447df8bae1dSRodney W. Grimes 1448df8bae1dSRodney W. Grimes /* 1449210a6886SKonstantin Belousov * Enter it in the pmap. If a wired, copy-on-write 1450210a6886SKonstantin Belousov * mapping is being replaced by a write-enabled 1451210a6886SKonstantin Belousov * mapping, then wire that new mapping. 1452df8bae1dSRodney W. Grimes */ 145339ffa8c1SKonstantin Belousov pmap_enter(dst_map->pmap, vaddr, dst_m, prot, 145439ffa8c1SKonstantin Belousov access | (upgrade ? PMAP_ENTER_WIRED : 0), 0); 1455df8bae1dSRodney W. Grimes 1456df8bae1dSRodney W. Grimes /* 1457df8bae1dSRodney W. Grimes * Mark it no longer busy, and put it on the active list. 1458df8bae1dSRodney W. Grimes */ 145989f6b863SAttilio Rao VM_OBJECT_WLOCK(dst_object); 14602965a453SKip Macy 1461210a6886SKonstantin Belousov if (upgrade) { 14620973283dSKonstantin Belousov if (src_m != dst_m) { 14632965a453SKip Macy vm_page_lock(src_m); 14643ae10f74SAttilio Rao vm_page_unwire(src_m, PQ_INACTIVE); 1465e20e8c15SKonstantin Belousov vm_page_unlock(src_m); 14662965a453SKip Macy vm_page_lock(dst_m); 1467210a6886SKonstantin Belousov vm_page_wire(dst_m); 1468e20e8c15SKonstantin Belousov vm_page_unlock(dst_m); 14692965a453SKip Macy } else { 14700973283dSKonstantin Belousov KASSERT(dst_m->wire_count > 0, 14710973283dSKonstantin Belousov ("dst_m %p is not wired", dst_m)); 14720973283dSKonstantin Belousov } 14730973283dSKonstantin Belousov } else { 14742965a453SKip Macy vm_page_lock(dst_m); 1475df8bae1dSRodney W. Grimes vm_page_activate(dst_m); 1476e20e8c15SKonstantin Belousov vm_page_unlock(dst_m); 14772965a453SKip Macy } 1478c7aebda8SAttilio Rao vm_page_xunbusy(dst_m); 1479df8bae1dSRodney W. Grimes } 148089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(dst_object); 1481210a6886SKonstantin Belousov if (upgrade) { 1482210a6886SKonstantin Belousov dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY); 1483210a6886SKonstantin Belousov vm_object_deallocate(src_object); 1484210a6886SKonstantin Belousov } 1485df8bae1dSRodney W. Grimes } 148626f9a767SRodney W. Grimes 14875730afc9SAlan Cox /* 14885730afc9SAlan Cox * Block entry into the machine-independent layer's page fault handler by 14895730afc9SAlan Cox * the calling thread. Subsequent calls to vm_fault() by that thread will 14905730afc9SAlan Cox * return KERN_PROTECTION_FAILURE. Enable machine-dependent handling of 14915730afc9SAlan Cox * spurious page faults. 14925730afc9SAlan Cox */ 14932801687dSKonstantin Belousov int 14942801687dSKonstantin Belousov vm_fault_disable_pagefaults(void) 14952801687dSKonstantin Belousov { 14962801687dSKonstantin Belousov 14975730afc9SAlan Cox return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR)); 14982801687dSKonstantin Belousov } 14992801687dSKonstantin Belousov 15002801687dSKonstantin Belousov void 15012801687dSKonstantin Belousov vm_fault_enable_pagefaults(int save) 15022801687dSKonstantin Belousov { 15032801687dSKonstantin Belousov 15042801687dSKonstantin Belousov curthread_pflags_restore(save); 15052801687dSKonstantin Belousov } 1506