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; 125cd8a6fe8SAlan Cox bool 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); 151cd8a6fe8SAlan Cox fs->lookup_still_valid = false; 1524866e085SJohn Dyson } 1534866e085SJohn Dyson } 1544866e085SJohn Dyson 1554866e085SJohn Dyson static void 156cfabea3dSKonstantin Belousov unlock_vp(struct faultstate *fs) 157cfabea3dSKonstantin Belousov { 158cfabea3dSKonstantin Belousov 159cfabea3dSKonstantin Belousov if (fs->vp != NULL) { 160cfabea3dSKonstantin Belousov vput(fs->vp); 161cfabea3dSKonstantin Belousov fs->vp = NULL; 162cfabea3dSKonstantin Belousov } 163cfabea3dSKonstantin Belousov } 164cfabea3dSKonstantin Belousov 165cfabea3dSKonstantin Belousov static void 166a51b0840SAlan Cox unlock_and_deallocate(struct faultstate *fs) 1674866e085SJohn Dyson { 168f29ba63eSAlan Cox 1694866e085SJohn Dyson vm_object_pip_wakeup(fs->object); 17089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(fs->object); 1714866e085SJohn Dyson if (fs->object != fs->first_object) { 17289f6b863SAttilio Rao VM_OBJECT_WLOCK(fs->first_object); 1732965a453SKip Macy vm_page_lock(fs->first_m); 1744866e085SJohn Dyson vm_page_free(fs->first_m); 1752965a453SKip Macy vm_page_unlock(fs->first_m); 1764866e085SJohn Dyson vm_object_pip_wakeup(fs->first_object); 17789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(fs->first_object); 1784866e085SJohn Dyson fs->first_m = NULL; 1794866e085SJohn Dyson } 1804866e085SJohn Dyson vm_object_deallocate(fs->first_object); 1814866e085SJohn Dyson unlock_map(fs); 182cfabea3dSKonstantin Belousov unlock_vp(fs); 1834866e085SJohn Dyson } 1844866e085SJohn Dyson 185a36f5532SKonstantin Belousov static void 186a36f5532SKonstantin Belousov vm_fault_dirty(vm_map_entry_t entry, vm_page_t m, vm_prot_t prot, 187a36f5532SKonstantin Belousov vm_prot_t fault_type, int fault_flags, boolean_t set_wd) 188a36f5532SKonstantin Belousov { 189a36f5532SKonstantin Belousov boolean_t need_dirty; 190a36f5532SKonstantin Belousov 191a36f5532SKonstantin Belousov if (((prot & VM_PROT_WRITE) == 0 && 192a36f5532SKonstantin Belousov (fault_flags & VM_FAULT_DIRTY) == 0) || 193a36f5532SKonstantin Belousov (m->oflags & VPO_UNMANAGED) != 0) 194a36f5532SKonstantin Belousov return; 195a36f5532SKonstantin Belousov 196a36f5532SKonstantin Belousov VM_OBJECT_ASSERT_LOCKED(m->object); 197a36f5532SKonstantin Belousov 198a36f5532SKonstantin Belousov need_dirty = ((fault_type & VM_PROT_WRITE) != 0 && 1996a875bf9SKonstantin Belousov (fault_flags & VM_FAULT_WIRE) == 0) || 200a36f5532SKonstantin Belousov (fault_flags & VM_FAULT_DIRTY) != 0; 201a36f5532SKonstantin Belousov 202a36f5532SKonstantin Belousov if (set_wd) 203a36f5532SKonstantin Belousov vm_object_set_writeable_dirty(m->object); 204a36f5532SKonstantin Belousov else 205a36f5532SKonstantin Belousov /* 206a36f5532SKonstantin Belousov * If two callers of vm_fault_dirty() with set_wd == 207a36f5532SKonstantin Belousov * FALSE, one for the map entry with MAP_ENTRY_NOSYNC 208a36f5532SKonstantin Belousov * flag set, other with flag clear, race, it is 209a36f5532SKonstantin Belousov * possible for the no-NOSYNC thread to see m->dirty 210a36f5532SKonstantin Belousov * != 0 and not clear VPO_NOSYNC. Take vm_page lock 211a36f5532SKonstantin Belousov * around manipulation of VPO_NOSYNC and 212a36f5532SKonstantin Belousov * vm_page_dirty() call, to avoid the race and keep 213a36f5532SKonstantin Belousov * m->oflags consistent. 214a36f5532SKonstantin Belousov */ 215a36f5532SKonstantin Belousov vm_page_lock(m); 216a36f5532SKonstantin Belousov 217a36f5532SKonstantin Belousov /* 218a36f5532SKonstantin Belousov * If this is a NOSYNC mmap we do not want to set VPO_NOSYNC 219a36f5532SKonstantin Belousov * if the page is already dirty to prevent data written with 220a36f5532SKonstantin Belousov * the expectation of being synced from not being synced. 221a36f5532SKonstantin Belousov * Likewise if this entry does not request NOSYNC then make 222a36f5532SKonstantin Belousov * sure the page isn't marked NOSYNC. Applications sharing 223a36f5532SKonstantin Belousov * data should use the same flags to avoid ping ponging. 224a36f5532SKonstantin Belousov */ 225a36f5532SKonstantin Belousov if ((entry->eflags & MAP_ENTRY_NOSYNC) != 0) { 226a36f5532SKonstantin Belousov if (m->dirty == 0) { 227a36f5532SKonstantin Belousov m->oflags |= VPO_NOSYNC; 228a36f5532SKonstantin Belousov } 229a36f5532SKonstantin Belousov } else { 230a36f5532SKonstantin Belousov m->oflags &= ~VPO_NOSYNC; 231a36f5532SKonstantin Belousov } 232a36f5532SKonstantin Belousov 233a36f5532SKonstantin Belousov /* 234a36f5532SKonstantin Belousov * If the fault is a write, we know that this page is being 235a36f5532SKonstantin Belousov * written NOW so dirty it explicitly to save on 236a36f5532SKonstantin Belousov * pmap_is_modified() calls later. 237a36f5532SKonstantin Belousov * 238a36f5532SKonstantin Belousov * Also tell the backing pager, if any, that it should remove 239a36f5532SKonstantin Belousov * any swap backing since the page is now dirty. 240a36f5532SKonstantin Belousov */ 241a36f5532SKonstantin Belousov if (need_dirty) 242a36f5532SKonstantin Belousov vm_page_dirty(m); 243a36f5532SKonstantin Belousov if (!set_wd) 244a36f5532SKonstantin Belousov vm_page_unlock(m); 245a36f5532SKonstantin Belousov if (need_dirty) 246a36f5532SKonstantin Belousov vm_pager_page_unswapped(m); 247a36f5532SKonstantin Belousov } 248a36f5532SKonstantin Belousov 249df8bae1dSRodney W. Grimes /* 250df8bae1dSRodney W. Grimes * vm_fault: 251df8bae1dSRodney W. Grimes * 252956f3135SPhilippe Charnier * Handle a page fault occurring at the given address, 253df8bae1dSRodney W. Grimes * requiring the given permissions, in the map specified. 254df8bae1dSRodney W. Grimes * If successful, the page is inserted into the 255df8bae1dSRodney W. Grimes * associated physical map. 256df8bae1dSRodney W. Grimes * 257df8bae1dSRodney W. Grimes * NOTE: the given address should be truncated to the 258df8bae1dSRodney W. Grimes * proper page address. 259df8bae1dSRodney W. Grimes * 260df8bae1dSRodney W. Grimes * KERN_SUCCESS is returned if the page fault is handled; otherwise, 261df8bae1dSRodney W. Grimes * a standard error specifying why the fault is fatal is returned. 262df8bae1dSRodney W. Grimes * 263df8bae1dSRodney W. Grimes * The map in question must be referenced, and remains so. 2640cddd8f0SMatthew Dillon * Caller may hold no locks. 265df8bae1dSRodney W. Grimes */ 266df8bae1dSRodney W. Grimes int 26723955314SAlfred Perlstein vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, 26823955314SAlfred Perlstein int fault_flags) 26923955314SAlfred Perlstein { 27035818d2eSJohn Baldwin struct thread *td; 27135818d2eSJohn Baldwin int result; 272acd11c74SAlan Cox 27335818d2eSJohn Baldwin td = curthread; 27435818d2eSJohn Baldwin if ((td->td_pflags & TDP_NOFAULTING) != 0) 2752801687dSKonstantin Belousov return (KERN_PROTECTION_FAILURE); 27635818d2eSJohn Baldwin #ifdef KTRACE 27735818d2eSJohn Baldwin if (map != kernel_map && KTRPOINT(td, KTR_FAULT)) 27835818d2eSJohn Baldwin ktrfault(vaddr, fault_type); 27935818d2eSJohn Baldwin #endif 280be996836SAttilio Rao result = vm_fault_hold(map, trunc_page(vaddr), fault_type, fault_flags, 281be996836SAttilio Rao NULL); 28235818d2eSJohn Baldwin #ifdef KTRACE 28335818d2eSJohn Baldwin if (map != kernel_map && KTRPOINT(td, KTR_FAULTEND)) 28435818d2eSJohn Baldwin ktrfaultend(result); 28535818d2eSJohn Baldwin #endif 28635818d2eSJohn Baldwin return (result); 287acd11c74SAlan Cox } 288acd11c74SAlan Cox 289acd11c74SAlan Cox int 290be996836SAttilio Rao vm_fault_hold(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, 291acd11c74SAlan Cox int fault_flags, vm_page_t *m_hold) 292acd11c74SAlan Cox { 293df8bae1dSRodney W. Grimes vm_prot_t prot; 294df8bae1dSRodney W. Grimes vm_object_t next_object; 2954866e085SJohn Dyson struct faultstate fs; 296d2bf64c3SKonstantin Belousov struct vnode *vp; 2970c3a4893SAlan Cox vm_offset_t e_end, e_start; 298afe55ca3SKonstantin Belousov vm_page_t m; 299*f994b207SAlan Cox int ahead, alloc_req, behind, cluster_offset, error, era, faultcount; 300*f994b207SAlan Cox int locked, map_generation, nera, result, rv; 3010c3a4893SAlan Cox u_char behavior; 302cd8a6fe8SAlan Cox boolean_t wired; /* Passed by reference. */ 303cd8a6fe8SAlan Cox bool dead, growstack, hardfault, is_first_object_locked; 304df8bae1dSRodney W. Grimes 30567596082SAttilio Rao PCPU_INC(cnt.v_vm_faults); 306d2bf64c3SKonstantin Belousov fs.vp = NULL; 307b0cd2017SGleb Smirnoff faultcount = 0; 3080c3a4893SAlan Cox nera = -1; 309cd8a6fe8SAlan Cox growstack = true; 310320023e2SAlan Cox hardfault = false; 311df8bae1dSRodney W. Grimes 312df8bae1dSRodney W. Grimes RetryFault:; 313df8bae1dSRodney W. Grimes 314df8bae1dSRodney W. Grimes /* 3150d94caffSDavid Greenman * Find the backing store object and offset into it to begin the 3160d94caffSDavid Greenman * search. 317df8bae1dSRodney W. Grimes */ 31840360b1bSMatthew Dillon fs.map = map; 31992de35b0SAlan Cox result = vm_map_lookup(&fs.map, vaddr, fault_type, &fs.entry, 32092de35b0SAlan Cox &fs.first_object, &fs.first_pindex, &prot, &wired); 32192de35b0SAlan Cox if (result != KERN_SUCCESS) { 3226139043bSAlan Cox if (growstack && result == KERN_INVALID_ADDRESS && 3232db65ab4SAlan Cox map != kernel_map) { 3246139043bSAlan Cox result = vm_map_growstack(curproc, vaddr); 325a976eb5eSAlan Cox if (result != KERN_SUCCESS) 3266139043bSAlan Cox return (KERN_FAILURE); 327cd8a6fe8SAlan Cox growstack = false; 3286139043bSAlan Cox goto RetryFault; 3296139043bSAlan Cox } 330a9ee028dSMark Johnston unlock_vp(&fs); 33192de35b0SAlan Cox return (result); 33209e0c6ccSJohn Dyson } 33309e0c6ccSJohn Dyson 3344866e085SJohn Dyson map_generation = fs.map->timestamp; 3352d8acc0fSJohn Dyson 3364866e085SJohn Dyson if (fs.entry->eflags & MAP_ENTRY_NOFAULT) { 33747221757SJohn Dyson panic("vm_fault: fault on nofault entry, addr: %lx", 33892c4c4ebSBruce Evans (u_long)vaddr); 3397aaaa4fdSJohn Dyson } 3407aaaa4fdSJohn Dyson 3414f9c9114SKonstantin Belousov if (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION && 3424f9c9114SKonstantin Belousov fs.entry->wiring_thread != curthread) { 3434f9c9114SKonstantin Belousov vm_map_unlock_read(fs.map); 3444f9c9114SKonstantin Belousov vm_map_lock(fs.map); 3454f9c9114SKonstantin Belousov if (vm_map_lookup_entry(fs.map, vaddr, &fs.entry) && 3464f9c9114SKonstantin Belousov (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION)) { 347cfabea3dSKonstantin Belousov unlock_vp(&fs); 3484f9c9114SKonstantin Belousov fs.entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 3494f9c9114SKonstantin Belousov vm_map_unlock_and_wait(fs.map, 0); 3504f9c9114SKonstantin Belousov } else 3514f9c9114SKonstantin Belousov vm_map_unlock(fs.map); 3524f9c9114SKonstantin Belousov goto RetryFault; 3534f9c9114SKonstantin Belousov } 3544f9c9114SKonstantin Belousov 355afe55ca3SKonstantin Belousov if (wired) 356afe55ca3SKonstantin Belousov fault_type = prot | (fault_type & VM_PROT_COPY); 3576a875bf9SKonstantin Belousov else 3586a875bf9SKonstantin Belousov KASSERT((fault_flags & VM_FAULT_WIRE) == 0, 3596a875bf9SKonstantin Belousov ("!wired && VM_FAULT_WIRE")); 360afe55ca3SKonstantin Belousov 3618d67b8c8SAlan Cox /* 3628d67b8c8SAlan Cox * Try to avoid lock contention on the top-level object through 3638d67b8c8SAlan Cox * special-case handling of some types of page faults, specifically, 3648d67b8c8SAlan Cox * those that are both (1) mapping an existing page from the top- 3658d67b8c8SAlan Cox * level object and (2) not having to mark that object as containing 3668d67b8c8SAlan Cox * dirty pages. Under these conditions, a read lock on the top-level 3678d67b8c8SAlan Cox * object suffices, allowing multiple page faults of a similar type to 3688d67b8c8SAlan Cox * run in parallel on the same top-level object. 3698d67b8c8SAlan Cox */ 370afe55ca3SKonstantin Belousov if (fs.vp == NULL /* avoid locked vnode leak */ && 3716a875bf9SKonstantin Belousov (fault_flags & (VM_FAULT_WIRE | VM_FAULT_DIRTY)) == 0 && 372afe55ca3SKonstantin Belousov /* avoid calling vm_object_set_writeable_dirty() */ 373afe55ca3SKonstantin Belousov ((prot & VM_PROT_WRITE) == 0 || 374f40cb1c6SKonstantin Belousov (fs.first_object->type != OBJT_VNODE && 375f40cb1c6SKonstantin Belousov (fs.first_object->flags & OBJ_TMPFS_NODE) == 0) || 376afe55ca3SKonstantin Belousov (fs.first_object->flags & OBJ_MIGHTBEDIRTY) != 0)) { 377afe55ca3SKonstantin Belousov VM_OBJECT_RLOCK(fs.first_object); 378afe55ca3SKonstantin Belousov if ((prot & VM_PROT_WRITE) != 0 && 379f40cb1c6SKonstantin Belousov (fs.first_object->type == OBJT_VNODE || 380f40cb1c6SKonstantin Belousov (fs.first_object->flags & OBJ_TMPFS_NODE) != 0) && 381afe55ca3SKonstantin Belousov (fs.first_object->flags & OBJ_MIGHTBEDIRTY) == 0) 382afe55ca3SKonstantin Belousov goto fast_failed; 383afe55ca3SKonstantin Belousov m = vm_page_lookup(fs.first_object, fs.first_pindex); 384b9ce8cc2SAlan Cox /* A busy page can be mapped for read|execute access. */ 385b9ce8cc2SAlan Cox if (m == NULL || ((prot & VM_PROT_WRITE) != 0 && 386b9ce8cc2SAlan Cox vm_page_busied(m)) || m->valid != VM_PAGE_BITS_ALL) 387afe55ca3SKonstantin Belousov goto fast_failed; 388afe55ca3SKonstantin Belousov result = pmap_enter(fs.map->pmap, vaddr, m, prot, 389afe55ca3SKonstantin Belousov fault_type | PMAP_ENTER_NOSLEEP | (wired ? PMAP_ENTER_WIRED : 390afe55ca3SKonstantin Belousov 0), 0); 391afe55ca3SKonstantin Belousov if (result != KERN_SUCCESS) 392afe55ca3SKonstantin Belousov goto fast_failed; 393afe55ca3SKonstantin Belousov if (m_hold != NULL) { 394afe55ca3SKonstantin Belousov *m_hold = m; 395afe55ca3SKonstantin Belousov vm_page_lock(m); 396afe55ca3SKonstantin Belousov vm_page_hold(m); 397afe55ca3SKonstantin Belousov vm_page_unlock(m); 398afe55ca3SKonstantin Belousov } 399a36f5532SKonstantin Belousov vm_fault_dirty(fs.entry, m, prot, fault_type, fault_flags, 400a36f5532SKonstantin Belousov FALSE); 401afe55ca3SKonstantin Belousov VM_OBJECT_RUNLOCK(fs.first_object); 402afe55ca3SKonstantin Belousov if (!wired) 403b0cd2017SGleb Smirnoff vm_fault_prefault(&fs, vaddr, PFBAK, PFFOR); 404afe55ca3SKonstantin Belousov vm_map_lookup_done(fs.map, fs.entry); 405afe55ca3SKonstantin Belousov curthread->td_ru.ru_minflt++; 406afe55ca3SKonstantin Belousov return (KERN_SUCCESS); 407afe55ca3SKonstantin Belousov fast_failed: 408afe55ca3SKonstantin Belousov if (!VM_OBJECT_TRYUPGRADE(fs.first_object)) { 409afe55ca3SKonstantin Belousov VM_OBJECT_RUNLOCK(fs.first_object); 410afe55ca3SKonstantin Belousov VM_OBJECT_WLOCK(fs.first_object); 411afe55ca3SKonstantin Belousov } 412afe55ca3SKonstantin Belousov } else { 413afe55ca3SKonstantin Belousov VM_OBJECT_WLOCK(fs.first_object); 414afe55ca3SKonstantin Belousov } 415afe55ca3SKonstantin Belousov 41695e5e988SJohn Dyson /* 41795e5e988SJohn Dyson * Make a reference to this object to prevent its disposal while we 41895e5e988SJohn Dyson * are messing with it. Once we have the reference, the map is free 41995e5e988SJohn Dyson * to be diddled. Since objects reference their shadows (and copies), 42095e5e988SJohn Dyson * they will stay around as well. 421fe8e0238SMatthew Dillon * 422fe8e0238SMatthew Dillon * Bump the paging-in-progress count to prevent size changes (e.g. 423fe8e0238SMatthew Dillon * truncation operations) during I/O. This must be done after 424fe8e0238SMatthew Dillon * obtaining the vnode lock in order to avoid possible deadlocks. 42595e5e988SJohn Dyson */ 426a976eb5eSAlan Cox vm_object_reference_locked(fs.first_object); 427d474eaaaSDoug Rabson vm_object_pip_add(fs.first_object, 1); 42895e5e988SJohn Dyson 429cd8a6fe8SAlan Cox fs.lookup_still_valid = true; 430df8bae1dSRodney W. Grimes 4314866e085SJohn Dyson fs.first_m = NULL; 432df8bae1dSRodney W. Grimes 433df8bae1dSRodney W. Grimes /* 434df8bae1dSRodney W. Grimes * Search for the page at object/offset. 435df8bae1dSRodney W. Grimes */ 4364866e085SJohn Dyson fs.object = fs.first_object; 4374866e085SJohn Dyson fs.pindex = fs.first_pindex; 438df8bae1dSRodney W. Grimes while (TRUE) { 4391c7c3c6aSMatthew Dillon /* 440725441f6SKonstantin Belousov * If the object is marked for imminent termination, 441725441f6SKonstantin Belousov * we retry here, since the collapse pass has raced 442725441f6SKonstantin Belousov * with us. Otherwise, if we see terminally dead 443725441f6SKonstantin Belousov * object, return fail. 4441c7c3c6aSMatthew Dillon */ 445725441f6SKonstantin Belousov if ((fs.object->flags & OBJ_DEAD) != 0) { 446725441f6SKonstantin Belousov dead = fs.object->type == OBJT_DEAD; 4474866e085SJohn Dyson unlock_and_deallocate(&fs); 448725441f6SKonstantin Belousov if (dead) 44947221757SJohn Dyson return (KERN_PROTECTION_FAILURE); 450725441f6SKonstantin Belousov pause("vmf_de", 1); 451725441f6SKonstantin Belousov goto RetryFault; 45247221757SJohn Dyson } 45347221757SJohn Dyson 4541c7c3c6aSMatthew Dillon /* 4551c7c3c6aSMatthew Dillon * See if page is resident 4561c7c3c6aSMatthew Dillon */ 4574866e085SJohn Dyson fs.m = vm_page_lookup(fs.object, fs.pindex); 4584866e085SJohn Dyson if (fs.m != NULL) { 45998cb733cSKenneth D. Merry /* 4601c7c3c6aSMatthew Dillon * Wait/Retry if the page is busy. We have to do this 461c7aebda8SAttilio Rao * if the page is either exclusive or shared busy 462c7aebda8SAttilio Rao * because the vm_pager may be using read busy for 463c7aebda8SAttilio Rao * pageouts (and even pageins if it is the vnode 464c7aebda8SAttilio Rao * pager), and we could end up trying to pagein and 465c7aebda8SAttilio Rao * pageout the same page simultaneously. 4661c7c3c6aSMatthew Dillon * 4671c7c3c6aSMatthew Dillon * We can theoretically allow the busy case on a read 4681c7c3c6aSMatthew Dillon * fault if the page is marked valid, but since such 4691c7c3c6aSMatthew Dillon * pages are typically already pmap'd, putting that 4701c7c3c6aSMatthew Dillon * special case in might be more effort then it is 4711c7c3c6aSMatthew Dillon * worth. We cannot under any circumstances mess 472c7aebda8SAttilio Rao * around with a shared busied page except, perhaps, 4731c7c3c6aSMatthew Dillon * to pmap it. 474df8bae1dSRodney W. Grimes */ 475c7aebda8SAttilio Rao if (vm_page_busied(fs.m)) { 476b88b6c9dSAlan Cox /* 477b88b6c9dSAlan Cox * Reference the page before unlocking and 478b88b6c9dSAlan Cox * sleeping so that the page daemon is less 479b88b6c9dSAlan Cox * likely to reclaim it. 480b88b6c9dSAlan Cox */ 4813407fefeSKonstantin Belousov vm_page_aflag_set(fs.m, PGA_REFERENCED); 482a51b0840SAlan Cox if (fs.object != fs.first_object) { 48389f6b863SAttilio Rao if (!VM_OBJECT_TRYWLOCK( 484a6e38685SKonstantin Belousov fs.first_object)) { 48589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(fs.object); 48689f6b863SAttilio Rao VM_OBJECT_WLOCK(fs.first_object); 48789f6b863SAttilio Rao VM_OBJECT_WLOCK(fs.object); 488a6e38685SKonstantin Belousov } 4892965a453SKip Macy vm_page_lock(fs.first_m); 490a51b0840SAlan Cox vm_page_free(fs.first_m); 4912965a453SKip Macy vm_page_unlock(fs.first_m); 492a51b0840SAlan Cox vm_object_pip_wakeup(fs.first_object); 49389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(fs.first_object); 494a51b0840SAlan Cox fs.first_m = NULL; 495a51b0840SAlan Cox } 496a51b0840SAlan Cox unlock_map(&fs); 497a51b0840SAlan Cox if (fs.m == vm_page_lookup(fs.object, 498a51b0840SAlan Cox fs.pindex)) { 499c7aebda8SAttilio Rao vm_page_sleep_if_busy(fs.m, "vmpfw"); 500a51b0840SAlan Cox } 501a51b0840SAlan Cox vm_object_pip_wakeup(fs.object); 50289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(fs.object); 50367596082SAttilio Rao PCPU_INC(cnt.v_intrans); 5044866e085SJohn Dyson vm_object_deallocate(fs.first_object); 505df8bae1dSRodney W. Grimes goto RetryFault; 506df8bae1dSRodney W. Grimes } 5073846a822SKonstantin Belousov vm_page_lock(fs.m); 5088d220203SAlan Cox vm_page_remque(fs.m); 5092965a453SKip Macy vm_page_unlock(fs.m); 5107615edaaSMatthew Dillon 5111c7c3c6aSMatthew Dillon /* 5121c7c3c6aSMatthew Dillon * Mark page busy for other processes, and the 5131c7c3c6aSMatthew Dillon * pagedaemon. If it still isn't completely valid 5141c7c3c6aSMatthew Dillon * (readable), jump to readrest, else break-out ( we 5151c7c3c6aSMatthew Dillon * found the page ). 5161c7c3c6aSMatthew Dillon */ 517c7aebda8SAttilio Rao vm_page_xbusy(fs.m); 518ff5958e7SAlan Cox if (fs.m->valid != VM_PAGE_BITS_ALL) 5190d94caffSDavid Greenman goto readrest; 520df8bae1dSRodney W. Grimes break; 521df8bae1dSRodney W. Grimes } 52210b4196bSAlan Cox KASSERT(fs.m == NULL, ("fs.m should be NULL, not %p", fs.m)); 5231c7c3c6aSMatthew Dillon 5241c7c3c6aSMatthew Dillon /* 52510b4196bSAlan Cox * Page is not resident. If the pager might contain the page 52610b4196bSAlan Cox * or this is the beginning of the search, allocate a new 52710b4196bSAlan Cox * page. (Default objects are zero-fill, so there is no real 52810b4196bSAlan Cox * pager for them.) 5291c7c3c6aSMatthew Dillon */ 5306a875bf9SKonstantin Belousov if (fs.object->type != OBJT_DEFAULT || 5316a875bf9SKonstantin Belousov fs.object == fs.first_object) { 5324866e085SJohn Dyson if (fs.pindex >= fs.object->size) { 5334866e085SJohn Dyson unlock_and_deallocate(&fs); 5345f55e841SDavid Greenman return (KERN_PROTECTION_FAILURE); 5355f55e841SDavid Greenman } 53622ba64e8SJohn Dyson 537df8bae1dSRodney W. Grimes /* 5380d94caffSDavid Greenman * Allocate a new page for this object/offset pair. 5393f1c4c4fSKonstantin Belousov * 5403f1c4c4fSKonstantin Belousov * Unlocked read of the p_flag is harmless. At 5413f1c4c4fSKonstantin Belousov * worst, the P_KILLED might be not observed 5423f1c4c4fSKonstantin Belousov * there, and allocation can fail, causing 5433f1c4c4fSKonstantin Belousov * restart and new reading of the p_flag. 544df8bae1dSRodney W. Grimes */ 5453f1c4c4fSKonstantin Belousov if (!vm_page_count_severe() || P_KILLED(curproc)) { 546f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 5473d653db0SAlan Cox vm_object_color(fs.object, atop(vaddr) - 5483d653db0SAlan Cox fs.pindex); 549f8a47341SAlan Cox #endif 5503f1c4c4fSKonstantin Belousov alloc_req = P_KILLED(curproc) ? 5513f1c4c4fSKonstantin Belousov VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL; 5523f1c4c4fSKonstantin Belousov if (fs.object->type != OBJT_VNODE && 5533f1c4c4fSKonstantin Belousov fs.object->backing_object == NULL) 5543f1c4c4fSKonstantin Belousov alloc_req |= VM_ALLOC_ZERO; 5554866e085SJohn Dyson fs.m = vm_page_alloc(fs.object, fs.pindex, 5563f1c4c4fSKonstantin Belousov alloc_req); 55740360b1bSMatthew Dillon } 5584866e085SJohn Dyson if (fs.m == NULL) { 5594866e085SJohn Dyson unlock_and_deallocate(&fs); 560ef6020d1SMike Silbersack VM_WAITPFAULT; 561df8bae1dSRodney W. Grimes goto RetryFault; 5620a2e596aSAlan Cox } else if (fs.m->valid == VM_PAGE_BITS_ALL) 5634ab8ab92SKonstantin Belousov break; 564df8bae1dSRodney W. Grimes } 56547221757SJohn Dyson 5660d94caffSDavid Greenman readrest: 5671c7c3c6aSMatthew Dillon /* 5680c3a4893SAlan Cox * If the pager for the current object might have the page, 5690c3a4893SAlan Cox * then determine the number of additional pages to read and 5700c3a4893SAlan Cox * potentially reprioritize previously read pages for earlier 5710c3a4893SAlan Cox * reclamation. These operations should only be performed 5720c3a4893SAlan Cox * once per page fault. Even if the current pager doesn't 5730c3a4893SAlan Cox * have the page, the number of additional pages to read will 5740c3a4893SAlan Cox * apply to subsequent objects in the shadow chain. 5751c7c3c6aSMatthew Dillon */ 5760c3a4893SAlan Cox if (fs.object->type != OBJT_DEFAULT && nera == -1 && 5770c3a4893SAlan Cox !P_KILLED(curproc)) { 5780c3a4893SAlan Cox KASSERT(fs.lookup_still_valid, ("map unlocked")); 5795268042bSAlan Cox era = fs.entry->read_ahead; 5800c3a4893SAlan Cox behavior = vm_map_entry_behavior(fs.entry); 5810c3a4893SAlan Cox if (behavior == MAP_ENTRY_BEHAV_RANDOM) { 5825268042bSAlan Cox nera = 0; 58313458803SAlan Cox } else if (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL) { 5845268042bSAlan Cox nera = VM_FAULT_READ_AHEAD_MAX; 585381b7242SAlan Cox if (vaddr == fs.entry->next_read) 5860c3a4893SAlan Cox vm_fault_dontneed(&fs, vaddr, nera); 587381b7242SAlan Cox } else if (vaddr == fs.entry->next_read) { 58813458803SAlan Cox /* 5895268042bSAlan Cox * This is a sequential fault. Arithmetically 5905268042bSAlan Cox * increase the requested number of pages in 5915268042bSAlan Cox * the read-ahead window. The requested 5925268042bSAlan Cox * number of pages is "# of sequential faults 5935268042bSAlan Cox * x (read ahead min + 1) + read ahead min" 59413458803SAlan Cox */ 5955268042bSAlan Cox nera = VM_FAULT_READ_AHEAD_MIN; 5965268042bSAlan Cox if (era > 0) { 5975268042bSAlan Cox nera += era + 1; 59813458803SAlan Cox if (nera > VM_FAULT_READ_AHEAD_MAX) 59913458803SAlan Cox nera = VM_FAULT_READ_AHEAD_MAX; 6005268042bSAlan Cox } 60113458803SAlan Cox if (era == VM_FAULT_READ_AHEAD_MAX) 6020c3a4893SAlan Cox vm_fault_dontneed(&fs, vaddr, nera); 6035268042bSAlan Cox } else { 6045268042bSAlan Cox /* 6050c3a4893SAlan Cox * This is a non-sequential fault. 6065268042bSAlan Cox */ 6075268042bSAlan Cox nera = 0; 608867a482dSJohn Dyson } 6090c3a4893SAlan Cox if (era != nera) { 6100c3a4893SAlan Cox /* 6110c3a4893SAlan Cox * A read lock on the map suffices to update 6120c3a4893SAlan Cox * the read ahead count safely. 6130c3a4893SAlan Cox */ 6145268042bSAlan Cox fs.entry->read_ahead = nera; 6150c3a4893SAlan Cox } 616d2bf64c3SKonstantin Belousov 617d2bf64c3SKonstantin Belousov /* 6180c3a4893SAlan Cox * Prepare for unlocking the map. Save the map 6190c3a4893SAlan Cox * entry's start and end addresses, which are used to 6200c3a4893SAlan Cox * optimize the size of the pager operation below. 6210c3a4893SAlan Cox * Even if the map entry's addresses change after 6220c3a4893SAlan Cox * unlocking the map, using the saved addresses is 6230c3a4893SAlan Cox * safe. 6240c3a4893SAlan Cox */ 6250c3a4893SAlan Cox e_start = fs.entry->start; 6260c3a4893SAlan Cox e_end = fs.entry->end; 6270c3a4893SAlan Cox } 6280c3a4893SAlan Cox 6290c3a4893SAlan Cox /* 6300c3a4893SAlan Cox * Call the pager to retrieve the page if there is a chance 6310c3a4893SAlan Cox * that the pager has it, and potentially retrieve additional 6320c3a4893SAlan Cox * pages at the same time. 6330c3a4893SAlan Cox */ 6340c3a4893SAlan Cox if (fs.object->type != OBJT_DEFAULT) { 6350c3a4893SAlan Cox /* 6360c3a4893SAlan Cox * We have either allocated a new page or found an 6370c3a4893SAlan Cox * existing page that is only partially valid. We 6380c3a4893SAlan Cox * hold a reference on fs.object and the page is 6390c3a4893SAlan Cox * exclusive busied. 640d2bf64c3SKonstantin Belousov */ 641d2bf64c3SKonstantin Belousov unlock_map(&fs); 642d2bf64c3SKonstantin Belousov 643022dfd69SKonstantin Belousov if (fs.object->type == OBJT_VNODE && 644022dfd69SKonstantin Belousov (vp = fs.object->handle) != fs.vp) { 645cfabea3dSKonstantin Belousov unlock_vp(&fs); 646d2bf64c3SKonstantin Belousov locked = VOP_ISLOCKED(vp); 647d2bf64c3SKonstantin Belousov 648d2bf64c3SKonstantin Belousov if (locked != LK_EXCLUSIVE) 649d2bf64c3SKonstantin Belousov locked = LK_SHARED; 650d2bf64c3SKonstantin Belousov /* Do not sleep for vnode lock while fs.m is busy */ 651d2bf64c3SKonstantin Belousov error = vget(vp, locked | LK_CANRECURSE | 652d2bf64c3SKonstantin Belousov LK_NOWAIT, curthread); 653d2bf64c3SKonstantin Belousov if (error != 0) { 654d2bf64c3SKonstantin Belousov vhold(vp); 655d2bf64c3SKonstantin Belousov release_page(&fs); 656d2bf64c3SKonstantin Belousov unlock_and_deallocate(&fs); 657d2bf64c3SKonstantin Belousov error = vget(vp, locked | LK_RETRY | 658d2bf64c3SKonstantin Belousov LK_CANRECURSE, curthread); 659d2bf64c3SKonstantin Belousov vdrop(vp); 660d2bf64c3SKonstantin Belousov fs.vp = vp; 661d2bf64c3SKonstantin Belousov KASSERT(error == 0, 662d2bf64c3SKonstantin Belousov ("vm_fault: vget failed")); 663d2bf64c3SKonstantin Belousov goto RetryFault; 664d2bf64c3SKonstantin Belousov } 665d2bf64c3SKonstantin Belousov fs.vp = vp; 666d2bf64c3SKonstantin Belousov } 667d2bf64c3SKonstantin Belousov KASSERT(fs.vp == NULL || !fs.map->system_map, 668d2bf64c3SKonstantin Belousov ("vm_fault: vnode-backed object mapped by system map")); 669d2bf64c3SKonstantin Belousov 670df8bae1dSRodney W. Grimes /* 671b0cd2017SGleb Smirnoff * Page in the requested page and hint the pager, 672b0cd2017SGleb Smirnoff * that it may bring up surrounding pages. 67326f9a767SRodney W. Grimes */ 6740c3a4893SAlan Cox if (nera == -1 || behavior == MAP_ENTRY_BEHAV_RANDOM || 6750c3a4893SAlan Cox P_KILLED(curproc)) { 6760c3a4893SAlan Cox behind = 0; 6770c3a4893SAlan Cox ahead = 0; 6780c3a4893SAlan Cox } else { 6790c3a4893SAlan Cox /* Is this a sequential fault? */ 6800c3a4893SAlan Cox if (nera > 0) { 6810c3a4893SAlan Cox behind = 0; 6820c3a4893SAlan Cox ahead = nera; 6830c3a4893SAlan Cox } else { 6840c3a4893SAlan Cox /* 6850c3a4893SAlan Cox * Request a cluster of pages that is 6860c3a4893SAlan Cox * aligned to a VM_FAULT_READ_DEFAULT 6870c3a4893SAlan Cox * page offset boundary within the 6880c3a4893SAlan Cox * object. Alignment to a page offset 6890c3a4893SAlan Cox * boundary is more likely to coincide 6900c3a4893SAlan Cox * with the underlying file system 6910c3a4893SAlan Cox * block than alignment to a virtual 6920c3a4893SAlan Cox * address boundary. 6930c3a4893SAlan Cox */ 6940c3a4893SAlan Cox cluster_offset = fs.pindex % 6950c3a4893SAlan Cox VM_FAULT_READ_DEFAULT; 6960c3a4893SAlan Cox behind = ulmin(cluster_offset, 6970c3a4893SAlan Cox atop(vaddr - e_start)); 6980c3a4893SAlan Cox ahead = VM_FAULT_READ_DEFAULT - 1 - 6990c3a4893SAlan Cox cluster_offset; 7000c3a4893SAlan Cox } 7010c3a4893SAlan Cox ahead = ulmin(ahead, atop(e_end - vaddr) - 1); 7020c3a4893SAlan Cox } 703b0cd2017SGleb Smirnoff rv = vm_pager_get_pages(fs.object, &fs.m, 1, 704b0cd2017SGleb Smirnoff &behind, &ahead); 70526f9a767SRodney W. Grimes if (rv == VM_PAGER_OK) { 706b0cd2017SGleb Smirnoff faultcount = behind + 1 + ahead; 707320023e2SAlan Cox hardfault = true; 7081c7c3c6aSMatthew Dillon break; /* break to PAGE HAS BEEN FOUND */ 709df8bae1dSRodney W. Grimes } 710a83c285cSDavid Greenman if (rv == VM_PAGER_ERROR) 711f3679e35SDavid Greenman printf("vm_fault: pager read error, pid %d (%s)\n", 712f3679e35SDavid Greenman curproc->p_pid, curproc->p_comm); 713521ddf39SAlan Cox 71426f9a767SRodney W. Grimes /* 715521ddf39SAlan Cox * If an I/O error occurred or the requested page was 716521ddf39SAlan Cox * outside the range of the pager, clean up and return 717521ddf39SAlan Cox * an error. 71826f9a767SRodney W. Grimes */ 719521ddf39SAlan Cox if (rv == VM_PAGER_ERROR || rv == VM_PAGER_BAD) { 7202965a453SKip Macy vm_page_lock(fs.m); 721230afe0bSKonstantin Belousov if (fs.m->wire_count == 0) 7224866e085SJohn Dyson vm_page_free(fs.m); 723230afe0bSKonstantin Belousov else 724230afe0bSKonstantin Belousov vm_page_xunbusy_maybelocked(fs.m); 7252965a453SKip Macy vm_page_unlock(fs.m); 7264866e085SJohn Dyson fs.m = NULL; 7274866e085SJohn Dyson unlock_and_deallocate(&fs); 728521ddf39SAlan Cox return (rv == VM_PAGER_ERROR ? KERN_FAILURE : 729521ddf39SAlan Cox KERN_PROTECTION_FAILURE); 73026f9a767SRodney W. Grimes } 731521ddf39SAlan Cox 732521ddf39SAlan Cox /* 733521ddf39SAlan Cox * The requested page does not exist at this object/ 734521ddf39SAlan Cox * offset. Remove the invalid page from the object, 735521ddf39SAlan Cox * waking up anyone waiting for it, and continue on to 736521ddf39SAlan Cox * the next object. However, if this is the top-level 737521ddf39SAlan Cox * object, we must leave the busy page in place to 738521ddf39SAlan Cox * prevent another process from rushing past us, and 739521ddf39SAlan Cox * inserting the page in that object at the same time 740521ddf39SAlan Cox * that we are. 741521ddf39SAlan Cox */ 7424866e085SJohn Dyson if (fs.object != fs.first_object) { 7432965a453SKip Macy vm_page_lock(fs.m); 744230afe0bSKonstantin Belousov if (fs.m->wire_count == 0) 7454866e085SJohn Dyson vm_page_free(fs.m); 746230afe0bSKonstantin Belousov else 747230afe0bSKonstantin Belousov vm_page_xunbusy_maybelocked(fs.m); 7482965a453SKip Macy vm_page_unlock(fs.m); 7494866e085SJohn Dyson fs.m = NULL; 750df8bae1dSRodney W. Grimes } 751df8bae1dSRodney W. Grimes } 75240360b1bSMatthew Dillon 753df8bae1dSRodney W. Grimes /* 7541c7c3c6aSMatthew Dillon * We get here if the object has default pager (or unwiring) 7551c7c3c6aSMatthew Dillon * or the pager doesn't have the page. 756df8bae1dSRodney W. Grimes */ 7574866e085SJohn Dyson if (fs.object == fs.first_object) 7584866e085SJohn Dyson fs.first_m = fs.m; 759df8bae1dSRodney W. Grimes 760df8bae1dSRodney W. Grimes /* 7610d94caffSDavid Greenman * Move on to the next object. Lock the next object before 7620d94caffSDavid Greenman * unlocking the current one. 763df8bae1dSRodney W. Grimes */ 7644866e085SJohn Dyson next_object = fs.object->backing_object; 765df8bae1dSRodney W. Grimes if (next_object == NULL) { 766df8bae1dSRodney W. Grimes /* 7670d94caffSDavid Greenman * If there's no object left, fill the page in the top 7680d94caffSDavid Greenman * object with zeros. 769df8bae1dSRodney W. Grimes */ 7704866e085SJohn Dyson if (fs.object != fs.first_object) { 7714866e085SJohn Dyson vm_object_pip_wakeup(fs.object); 77289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(fs.object); 773df8bae1dSRodney W. Grimes 7744866e085SJohn Dyson fs.object = fs.first_object; 7754866e085SJohn Dyson fs.pindex = fs.first_pindex; 7764866e085SJohn Dyson fs.m = fs.first_m; 77789f6b863SAttilio Rao VM_OBJECT_WLOCK(fs.object); 778df8bae1dSRodney W. Grimes } 7794866e085SJohn Dyson fs.first_m = NULL; 780df8bae1dSRodney W. Grimes 7814221e284SAlan Cox /* 7824221e284SAlan Cox * Zero the page if necessary and mark it valid. 7834221e284SAlan Cox */ 7844866e085SJohn Dyson if ((fs.m->flags & PG_ZERO) == 0) { 785fff6062aSAlan Cox pmap_zero_page(fs.m); 7864221e284SAlan Cox } else { 78767596082SAttilio Rao PCPU_INC(cnt.v_ozfod); 7884221e284SAlan Cox } 78967596082SAttilio Rao PCPU_INC(cnt.v_zfod); 7904221e284SAlan Cox fs.m->valid = VM_PAGE_BITS_ALL; 7917b9b301cSAlan Cox /* Don't try to prefault neighboring pages. */ 7927b9b301cSAlan Cox faultcount = 1; 7931c7c3c6aSMatthew Dillon break; /* break to PAGE HAS BEEN FOUND */ 7940d94caffSDavid Greenman } else { 795c8567c3aSAlan Cox KASSERT(fs.object != next_object, 796c8567c3aSAlan Cox ("object loop %p", next_object)); 79789f6b863SAttilio Rao VM_OBJECT_WLOCK(next_object); 798c8567c3aSAlan Cox vm_object_pip_add(next_object, 1); 799c8567c3aSAlan Cox if (fs.object != fs.first_object) 8004866e085SJohn Dyson vm_object_pip_wakeup(fs.object); 8016753423cSAlan Cox fs.pindex += 8026753423cSAlan Cox OFF_TO_IDX(fs.object->backing_object_offset); 80389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(fs.object); 8044866e085SJohn Dyson fs.object = next_object; 805df8bae1dSRodney W. Grimes } 806df8bae1dSRodney W. Grimes } 8071c7c3c6aSMatthew Dillon 808c7aebda8SAttilio Rao vm_page_assert_xbusied(fs.m); 809df8bae1dSRodney W. Grimes 810df8bae1dSRodney W. Grimes /* 8110d94caffSDavid Greenman * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock 812df8bae1dSRodney W. Grimes * is held.] 813df8bae1dSRodney W. Grimes */ 814df8bae1dSRodney W. Grimes 815df8bae1dSRodney W. Grimes /* 8160d94caffSDavid Greenman * If the page is being written, but isn't already owned by the 8170d94caffSDavid Greenman * top-level object, we have to copy it into a new page owned by the 8180d94caffSDavid Greenman * top-level object. 819df8bae1dSRodney W. Grimes */ 8204866e085SJohn Dyson if (fs.object != fs.first_object) { 821df8bae1dSRodney W. Grimes /* 8220d94caffSDavid Greenman * We only really need to copy if we want to write it. 823df8bae1dSRodney W. Grimes */ 824a6d42a0dSAlan Cox if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) { 825df8bae1dSRodney W. Grimes /* 8261c7c3c6aSMatthew Dillon * This allows pages to be virtually copied from a 8271c7c3c6aSMatthew Dillon * backing_object into the first_object, where the 8281c7c3c6aSMatthew Dillon * backing object has no other refs to it, and cannot 8291c7c3c6aSMatthew Dillon * gain any more refs. Instead of a bcopy, we just 8301c7c3c6aSMatthew Dillon * move the page from the backing object to the 8311c7c3c6aSMatthew Dillon * first object. Note that we must mark the page 8321c7c3c6aSMatthew Dillon * dirty in the first object so that it will go out 8331c7c3c6aSMatthew Dillon * to swap when needed. 834df8bae1dSRodney W. Grimes */ 835cd8a6fe8SAlan Cox is_first_object_locked = false; 836e50346b5SAlan Cox if ( 837de5f6a77SJohn Dyson /* 838de5f6a77SJohn Dyson * Only one shadow object 839de5f6a77SJohn Dyson */ 8404866e085SJohn Dyson (fs.object->shadow_count == 1) && 841de5f6a77SJohn Dyson /* 842de5f6a77SJohn Dyson * No COW refs, except us 843de5f6a77SJohn Dyson */ 8444866e085SJohn Dyson (fs.object->ref_count == 1) && 845de5f6a77SJohn Dyson /* 8465929bcfaSPhilippe Charnier * No one else can look this object up 847de5f6a77SJohn Dyson */ 8484866e085SJohn Dyson (fs.object->handle == NULL) && 849de5f6a77SJohn Dyson /* 850de5f6a77SJohn Dyson * No other ways to look the object up 851de5f6a77SJohn Dyson */ 8524866e085SJohn Dyson ((fs.object->type == OBJT_DEFAULT) || 8534866e085SJohn Dyson (fs.object->type == OBJT_SWAP)) && 85489f6b863SAttilio Rao (is_first_object_locked = VM_OBJECT_TRYWLOCK(fs.first_object)) && 855de5f6a77SJohn Dyson /* 856de5f6a77SJohn Dyson * We don't chase down the shadow chain 857de5f6a77SJohn Dyson */ 858e50346b5SAlan Cox fs.object == fs.first_object->backing_object) { 859bccdea45SAlan Cox vm_page_lock(fs.m); 860bccdea45SAlan Cox vm_page_remove(fs.m); 861bccdea45SAlan Cox vm_page_unlock(fs.m); 862eb00b276SAlan Cox vm_page_lock(fs.first_m); 863bccdea45SAlan Cox vm_page_replace_checked(fs.m, fs.first_object, 864bccdea45SAlan Cox fs.first_pindex, fs.first_m); 8656fee422eSConrad Meyer vm_page_free(fs.first_m); 8666fee422eSConrad Meyer vm_page_unlock(fs.first_m); 867bccdea45SAlan Cox vm_page_dirty(fs.m); 868dfdf9abdSAlan Cox #if VM_NRESERVLEVEL > 0 869dfdf9abdSAlan Cox /* 870dfdf9abdSAlan Cox * Rename the reservation. 871dfdf9abdSAlan Cox */ 872dfdf9abdSAlan Cox vm_reserv_rename(fs.m, fs.first_object, 873dfdf9abdSAlan Cox fs.object, OFF_TO_IDX( 874dfdf9abdSAlan Cox fs.first_object->backing_object_offset)); 875dfdf9abdSAlan Cox #endif 876bccdea45SAlan Cox /* 877bccdea45SAlan Cox * Removing the page from the backing object 878bccdea45SAlan Cox * unbusied it. 879bccdea45SAlan Cox */ 880c7aebda8SAttilio Rao vm_page_xbusy(fs.m); 881d98ddc46SAlan Cox fs.first_m = fs.m; 8824866e085SJohn Dyson fs.m = NULL; 88367596082SAttilio Rao PCPU_INC(cnt.v_cow_optim); 884de5f6a77SJohn Dyson } else { 885de5f6a77SJohn Dyson /* 886de5f6a77SJohn Dyson * Oh, well, lets copy it. 887de5f6a77SJohn Dyson */ 888669890eaSAlan Cox pmap_copy_page(fs.m, fs.first_m); 889669890eaSAlan Cox fs.first_m->valid = VM_PAGE_BITS_ALL; 890d8778512SAlan Cox if (wired && (fault_flags & 8916a875bf9SKonstantin Belousov VM_FAULT_WIRE) == 0) { 8922965a453SKip Macy vm_page_lock(fs.first_m); 893d8778512SAlan Cox vm_page_wire(fs.first_m); 8942965a453SKip Macy vm_page_unlock(fs.first_m); 8952965a453SKip Macy 8962965a453SKip Macy vm_page_lock(fs.m); 8973ae10f74SAttilio Rao vm_page_unwire(fs.m, PQ_INACTIVE); 8982965a453SKip Macy vm_page_unlock(fs.m); 899de5f6a77SJohn Dyson } 900df8bae1dSRodney W. Grimes /* 901df8bae1dSRodney W. Grimes * We no longer need the old page or object. 902df8bae1dSRodney W. Grimes */ 9034866e085SJohn Dyson release_page(&fs); 904de5f6a77SJohn Dyson } 9051c7c3c6aSMatthew Dillon /* 9061c7c3c6aSMatthew Dillon * fs.object != fs.first_object due to above 9071c7c3c6aSMatthew Dillon * conditional 9081c7c3c6aSMatthew Dillon */ 9094866e085SJohn Dyson vm_object_pip_wakeup(fs.object); 91089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(fs.object); 911df8bae1dSRodney W. Grimes /* 912df8bae1dSRodney W. Grimes * Only use the new page below... 913df8bae1dSRodney W. Grimes */ 9144866e085SJohn Dyson fs.object = fs.first_object; 9154866e085SJohn Dyson fs.pindex = fs.first_pindex; 916d98ddc46SAlan Cox fs.m = fs.first_m; 917f29ba63eSAlan Cox if (!is_first_object_locked) 91889f6b863SAttilio Rao VM_OBJECT_WLOCK(fs.object); 91967596082SAttilio Rao PCPU_INC(cnt.v_cow_faults); 9204d34e019SKonstantin Belousov curthread->td_cow++; 9210d94caffSDavid Greenman } else { 922df8bae1dSRodney W. Grimes prot &= ~VM_PROT_WRITE; 923df8bae1dSRodney W. Grimes } 924df8bae1dSRodney W. Grimes } 925df8bae1dSRodney W. Grimes 926df8bae1dSRodney W. Grimes /* 9270d94caffSDavid Greenman * We must verify that the maps have not changed since our last 9280d94caffSDavid Greenman * lookup. 929df8bae1dSRodney W. Grimes */ 93019dc5607STor Egge if (!fs.lookup_still_valid) { 931df8bae1dSRodney W. Grimes vm_object_t retry_object; 932a316d390SJohn Dyson vm_pindex_t retry_pindex; 933df8bae1dSRodney W. Grimes vm_prot_t retry_prot; 934df8bae1dSRodney W. Grimes 93519dc5607STor Egge if (!vm_map_trylock_read(fs.map)) { 936b823bbd6SMatthew Dillon release_page(&fs); 937b823bbd6SMatthew Dillon unlock_and_deallocate(&fs); 938b823bbd6SMatthew Dillon goto RetryFault; 939b823bbd6SMatthew Dillon } 940cd8a6fe8SAlan Cox fs.lookup_still_valid = true; 94119dc5607STor Egge if (fs.map->timestamp != map_generation) { 94219dc5607STor Egge result = vm_map_lookup_locked(&fs.map, vaddr, fault_type, 9434866e085SJohn Dyson &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired); 944df8bae1dSRodney W. Grimes 945df8bae1dSRodney W. Grimes /* 94644ed3417STor Egge * If we don't need the page any longer, put it on the inactive 9470d94caffSDavid Greenman * list (the easiest thing to do here). If no one needs it, 9480d94caffSDavid Greenman * pageout will grab it eventually. 949df8bae1dSRodney W. Grimes */ 950df8bae1dSRodney W. Grimes if (result != KERN_SUCCESS) { 9514866e085SJohn Dyson release_page(&fs); 9524866e085SJohn Dyson unlock_and_deallocate(&fs); 95319dc5607STor Egge 95419dc5607STor Egge /* 95519dc5607STor Egge * If retry of map lookup would have blocked then 95619dc5607STor Egge * retry fault from start. 95719dc5607STor Egge */ 95819dc5607STor Egge if (result == KERN_FAILURE) 95919dc5607STor Egge goto RetryFault; 960df8bae1dSRodney W. Grimes return (result); 961df8bae1dSRodney W. Grimes } 9624866e085SJohn Dyson if ((retry_object != fs.first_object) || 9634866e085SJohn Dyson (retry_pindex != fs.first_pindex)) { 9644866e085SJohn Dyson release_page(&fs); 9654866e085SJohn Dyson unlock_and_deallocate(&fs); 966df8bae1dSRodney W. Grimes goto RetryFault; 967df8bae1dSRodney W. Grimes } 96819dc5607STor Egge 969df8bae1dSRodney W. Grimes /* 9700d94caffSDavid Greenman * Check whether the protection has changed or the object has 9710d94caffSDavid Greenman * been copied while we left the map unlocked. Changing from 9720d94caffSDavid Greenman * read to write permission is OK - we leave the page 9730d94caffSDavid Greenman * write-protected, and catch the write fault. Changing from 9740d94caffSDavid Greenman * write to read permission means that we can't mark the page 9750d94caffSDavid Greenman * write-enabled after all. 976df8bae1dSRodney W. Grimes */ 977df8bae1dSRodney W. Grimes prot &= retry_prot; 978df8bae1dSRodney W. Grimes } 97919dc5607STor Egge } 980381b7242SAlan Cox 981d2bf64c3SKonstantin Belousov /* 982381b7242SAlan Cox * If the page was filled by a pager, save the virtual address that 983381b7242SAlan Cox * should be faulted on next under a sequential access pattern to the 984381b7242SAlan Cox * map entry. A read lock on the map suffices to update this address 985381b7242SAlan Cox * safely. 986d2bf64c3SKonstantin Belousov */ 9875758fe71SAlan Cox if (hardfault) 988381b7242SAlan Cox fs.entry->next_read = vaddr + ptoa(ahead) + PAGE_SIZE; 989d2bf64c3SKonstantin Belousov 990a36f5532SKonstantin Belousov vm_fault_dirty(fs.entry, fs.m, prot, fault_type, fault_flags, TRUE); 991c7aebda8SAttilio Rao vm_page_assert_xbusied(fs.m); 992c7aebda8SAttilio Rao 9934221e284SAlan Cox /* 99478cfe1f7SAlan Cox * Page must be completely valid or it is not fit to 9954221e284SAlan Cox * map into user space. vm_pager_get_pages() ensures this. 9964221e284SAlan Cox */ 99778cfe1f7SAlan Cox KASSERT(fs.m->valid == VM_PAGE_BITS_ALL, 99878cfe1f7SAlan Cox ("vm_fault: page %p partially invalid", fs.m)); 99989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(fs.object); 1000cbfbaad8SAlan Cox 100186735996SAlan Cox /* 100286735996SAlan Cox * Put this page into the physical map. We had to do the unlock above 100386735996SAlan Cox * because pmap_enter() may sleep. We don't put the page 100486735996SAlan Cox * back on the active queue until later so that the pageout daemon 100586735996SAlan Cox * won't find it (yet). 100686735996SAlan Cox */ 100739ffa8c1SKonstantin Belousov pmap_enter(fs.map->pmap, vaddr, fs.m, prot, 100839ffa8c1SKonstantin Belousov fault_type | (wired ? PMAP_ENTER_WIRED : 0), 0); 10096a875bf9SKonstantin Belousov if (faultcount != 1 && (fault_flags & VM_FAULT_WIRE) == 0 && 10107b9b301cSAlan Cox wired == 0) 1011b0cd2017SGleb Smirnoff vm_fault_prefault(&fs, vaddr, 1012b0cd2017SGleb Smirnoff faultcount > 0 ? behind : PFBAK, 1013b0cd2017SGleb Smirnoff faultcount > 0 ? ahead : PFFOR); 101489f6b863SAttilio Rao VM_OBJECT_WLOCK(fs.object); 10152965a453SKip Macy vm_page_lock(fs.m); 1016ff97964aSJohn Dyson 1017df8bae1dSRodney W. Grimes /* 10180d94caffSDavid Greenman * If the page is not wired down, then put it where the pageout daemon 10190d94caffSDavid Greenman * can find it. 1020df8bae1dSRodney W. Grimes */ 10216a875bf9SKonstantin Belousov if ((fault_flags & VM_FAULT_WIRE) != 0) { 10226a875bf9SKonstantin Belousov KASSERT(wired, ("VM_FAULT_WIRE && !wired")); 10234866e085SJohn Dyson vm_page_wire(fs.m); 102403679e23SAlan Cox } else 10254866e085SJohn Dyson vm_page_activate(fs.m); 1026acd11c74SAlan Cox if (m_hold != NULL) { 1027acd11c74SAlan Cox *m_hold = fs.m; 1028acd11c74SAlan Cox vm_page_hold(fs.m); 1029acd11c74SAlan Cox } 10302965a453SKip Macy vm_page_unlock(fs.m); 1031c7aebda8SAttilio Rao vm_page_xunbusy(fs.m); 1032eeec6babSJohn Baldwin 1033eebf3286SAlan Cox /* 1034eebf3286SAlan Cox * Unlock everything, and return 1035eebf3286SAlan Cox */ 1036eebf3286SAlan Cox unlock_and_deallocate(&fs); 1037b3a01bdfSAndrey Zonov if (hardfault) { 1038b3a01bdfSAndrey Zonov PCPU_INC(cnt.v_io_faults); 10391c4bcd05SJeff Roberson curthread->td_ru.ru_majflt++; 1040ae34b6ffSEdward Tomasz Napierala #ifdef RACCT 1041ae34b6ffSEdward Tomasz Napierala if (racct_enable && fs.object->type == OBJT_VNODE) { 1042ae34b6ffSEdward Tomasz Napierala PROC_LOCK(curproc); 1043ae34b6ffSEdward Tomasz Napierala if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) { 1044ae34b6ffSEdward Tomasz Napierala racct_add_force(curproc, RACCT_WRITEBPS, 1045ae34b6ffSEdward Tomasz Napierala PAGE_SIZE + behind * PAGE_SIZE); 1046ae34b6ffSEdward Tomasz Napierala racct_add_force(curproc, RACCT_WRITEIOPS, 1); 1047ae34b6ffSEdward Tomasz Napierala } else { 1048ae34b6ffSEdward Tomasz Napierala racct_add_force(curproc, RACCT_READBPS, 1049ae34b6ffSEdward Tomasz Napierala PAGE_SIZE + ahead * PAGE_SIZE); 1050ae34b6ffSEdward Tomasz Napierala racct_add_force(curproc, RACCT_READIOPS, 1); 1051ae34b6ffSEdward Tomasz Napierala } 1052ae34b6ffSEdward Tomasz Napierala PROC_UNLOCK(curproc); 1053ae34b6ffSEdward Tomasz Napierala } 1054ae34b6ffSEdward Tomasz Napierala #endif 1055b3a01bdfSAndrey Zonov } else 10561c4bcd05SJeff Roberson curthread->td_ru.ru_minflt++; 1057df8bae1dSRodney W. Grimes 1058df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 1059df8bae1dSRodney W. Grimes } 1060df8bae1dSRodney W. Grimes 1061df8bae1dSRodney W. Grimes /* 1062a8b0f100SAlan Cox * Speed up the reclamation of pages that precede the faulting pindex within 1063a8b0f100SAlan Cox * the first object of the shadow chain. Essentially, perform the equivalent 1064a8b0f100SAlan Cox * to madvise(..., MADV_DONTNEED) on a large cluster of pages that precedes 1065a8b0f100SAlan Cox * the faulting pindex by the cluster size when the pages read by vm_fault() 1066a8b0f100SAlan Cox * cross a cluster-size boundary. The cluster size is the greater of the 1067a8b0f100SAlan Cox * smallest superpage size and VM_FAULT_DONTNEED_MIN. 1068a8b0f100SAlan Cox * 1069a8b0f100SAlan Cox * When "fs->first_object" is a shadow object, the pages in the backing object 1070a8b0f100SAlan Cox * that precede the faulting pindex are deactivated by vm_fault(). So, this 1071a8b0f100SAlan Cox * function must only be concerned with pages in the first object. 107213458803SAlan Cox */ 107313458803SAlan Cox static void 1074a8b0f100SAlan Cox vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr, int ahead) 107513458803SAlan Cox { 1076a8b0f100SAlan Cox vm_map_entry_t entry; 107713458803SAlan Cox vm_object_t first_object, object; 1078a8b0f100SAlan Cox vm_offset_t end, start; 1079a8b0f100SAlan Cox vm_page_t m, m_next; 1080a8b0f100SAlan Cox vm_pindex_t pend, pstart; 1081a8b0f100SAlan Cox vm_size_t size; 108213458803SAlan Cox 108313458803SAlan Cox object = fs->object; 108489f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 108513458803SAlan Cox first_object = fs->first_object; 108613458803SAlan Cox if (first_object != object) { 1087b5ab20c0SAlan Cox if (!VM_OBJECT_TRYWLOCK(first_object)) { 108889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1089b5ab20c0SAlan Cox VM_OBJECT_WLOCK(first_object); 109089f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 109113458803SAlan Cox } 109213458803SAlan Cox } 1093a8b0f100SAlan Cox /* Neither fictitious nor unmanaged pages can be reclaimed. */ 109428634820SAlan Cox if ((first_object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0) { 1095a8b0f100SAlan Cox size = VM_FAULT_DONTNEED_MIN; 1096a8b0f100SAlan Cox if (MAXPAGESIZES > 1 && size < pagesizes[1]) 1097a8b0f100SAlan Cox size = pagesizes[1]; 1098a8b0f100SAlan Cox end = rounddown2(vaddr, size); 1099a8b0f100SAlan Cox if (vaddr - end >= size - PAGE_SIZE - ptoa(ahead) && 1100a8b0f100SAlan Cox (entry = fs->entry)->start < end) { 1101a8b0f100SAlan Cox if (end - entry->start < size) 1102a8b0f100SAlan Cox start = entry->start; 110313458803SAlan Cox else 1104a8b0f100SAlan Cox start = end - size; 1105a8b0f100SAlan Cox pmap_advise(fs->map->pmap, start, end, MADV_DONTNEED); 1106a8b0f100SAlan Cox pstart = OFF_TO_IDX(entry->offset) + atop(start - 1107a8b0f100SAlan Cox entry->start); 1108a8b0f100SAlan Cox m_next = vm_page_find_least(first_object, pstart); 1109a8b0f100SAlan Cox pend = OFF_TO_IDX(entry->offset) + atop(end - 1110a8b0f100SAlan Cox entry->start); 1111a8b0f100SAlan Cox while ((m = m_next) != NULL && m->pindex < pend) { 1112a8b0f100SAlan Cox m_next = TAILQ_NEXT(m, listq); 1113a8b0f100SAlan Cox if (m->valid != VM_PAGE_BITS_ALL || 1114a8b0f100SAlan Cox vm_page_busied(m)) 111513458803SAlan Cox continue; 1116d8015db3SAlan Cox 1117d8015db3SAlan Cox /* 1118d8015db3SAlan Cox * Don't clear PGA_REFERENCED, since it would 1119d8015db3SAlan Cox * likely represent a reference by a different 1120d8015db3SAlan Cox * process. 1121d8015db3SAlan Cox * 1122d8015db3SAlan Cox * Typically, at this point, prefetched pages 1123d8015db3SAlan Cox * are still in the inactive queue. Only 1124d8015db3SAlan Cox * pages that triggered page faults are in the 1125d8015db3SAlan Cox * active queue. 1126d8015db3SAlan Cox */ 112713458803SAlan Cox vm_page_lock(m); 1128d8015db3SAlan Cox vm_page_deactivate(m); 112913458803SAlan Cox vm_page_unlock(m); 113013458803SAlan Cox } 113113458803SAlan Cox } 1132a8b0f100SAlan Cox } 113313458803SAlan Cox if (first_object != object) 1134b5ab20c0SAlan Cox VM_OBJECT_WUNLOCK(first_object); 113513458803SAlan Cox } 113613458803SAlan Cox 113713458803SAlan Cox /* 1138566526a9SAlan Cox * vm_fault_prefault provides a quick way of clustering 1139566526a9SAlan Cox * pagefaults into a processes address space. It is a "cousin" 1140566526a9SAlan Cox * of vm_map_pmap_enter, except it runs at page fault time instead 1141566526a9SAlan Cox * of mmap time. 1142566526a9SAlan Cox */ 1143566526a9SAlan Cox static void 114463281952SAlan Cox vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra, 1145b0cd2017SGleb Smirnoff int backward, int forward) 1146566526a9SAlan Cox { 114763281952SAlan Cox pmap_t pmap; 114863281952SAlan Cox vm_map_entry_t entry; 114963281952SAlan Cox vm_object_t backing_object, lobject; 1150566526a9SAlan Cox vm_offset_t addr, starta; 1151566526a9SAlan Cox vm_pindex_t pindex; 11522053c127SStephan Uphoff vm_page_t m; 1153b0cd2017SGleb Smirnoff int i; 1154566526a9SAlan Cox 115563281952SAlan Cox pmap = fs->map->pmap; 1156950d5f7aSAlan Cox if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace)) 1157566526a9SAlan Cox return; 1158566526a9SAlan Cox 115963281952SAlan Cox entry = fs->entry; 1160566526a9SAlan Cox 116163281952SAlan Cox starta = addra - backward * PAGE_SIZE; 1162566526a9SAlan Cox if (starta < entry->start) { 1163566526a9SAlan Cox starta = entry->start; 1164566526a9SAlan Cox } else if (starta > addra) { 1165566526a9SAlan Cox starta = 0; 1166566526a9SAlan Cox } 1167566526a9SAlan Cox 116863281952SAlan Cox /* 116963281952SAlan Cox * Generate the sequence of virtual addresses that are candidates for 117063281952SAlan Cox * prefaulting in an outward spiral from the faulting virtual address, 117163281952SAlan Cox * "addra". Specifically, the sequence is "addra - PAGE_SIZE", "addra 117263281952SAlan Cox * + PAGE_SIZE", "addra - 2 * PAGE_SIZE", "addra + 2 * PAGE_SIZE", ... 117363281952SAlan Cox * If the candidate address doesn't have a backing physical page, then 117463281952SAlan Cox * the loop immediately terminates. 117563281952SAlan Cox */ 117663281952SAlan Cox for (i = 0; i < 2 * imax(backward, forward); i++) { 117763281952SAlan Cox addr = addra + ((i >> 1) + 1) * ((i & 1) == 0 ? -PAGE_SIZE : 117863281952SAlan Cox PAGE_SIZE); 117963281952SAlan Cox if (addr > addra + forward * PAGE_SIZE) 1180566526a9SAlan Cox addr = 0; 1181566526a9SAlan Cox 1182566526a9SAlan Cox if (addr < starta || addr >= entry->end) 1183566526a9SAlan Cox continue; 1184566526a9SAlan Cox 1185566526a9SAlan Cox if (!pmap_is_prefaultable(pmap, addr)) 1186566526a9SAlan Cox continue; 1187566526a9SAlan Cox 1188566526a9SAlan Cox pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT; 118963281952SAlan Cox lobject = entry->object.vm_object; 1190c141ae7fSAlan Cox VM_OBJECT_RLOCK(lobject); 1191566526a9SAlan Cox while ((m = vm_page_lookup(lobject, pindex)) == NULL && 1192566526a9SAlan Cox lobject->type == OBJT_DEFAULT && 1193566526a9SAlan Cox (backing_object = lobject->backing_object) != NULL) { 119436930fc9SAlan Cox KASSERT((lobject->backing_object_offset & PAGE_MASK) == 119536930fc9SAlan Cox 0, ("vm_fault_prefault: unaligned object offset")); 1196566526a9SAlan Cox pindex += lobject->backing_object_offset >> PAGE_SHIFT; 1197c141ae7fSAlan Cox VM_OBJECT_RLOCK(backing_object); 1198c141ae7fSAlan Cox VM_OBJECT_RUNLOCK(lobject); 1199566526a9SAlan Cox lobject = backing_object; 1200566526a9SAlan Cox } 1201cbfbaad8SAlan Cox if (m == NULL) { 1202c141ae7fSAlan Cox VM_OBJECT_RUNLOCK(lobject); 1203566526a9SAlan Cox break; 1204cbfbaad8SAlan Cox } 12050a2e596aSAlan Cox if (m->valid == VM_PAGE_BITS_ALL && 12063c4a2440SAlan Cox (m->flags & PG_FICTITIOUS) == 0) 12077bfda801SAlan Cox pmap_enter_quick(pmap, addr, m, entry->protection); 1208c141ae7fSAlan Cox VM_OBJECT_RUNLOCK(lobject); 1209566526a9SAlan Cox } 1210566526a9SAlan Cox } 1211566526a9SAlan Cox 1212566526a9SAlan Cox /* 121382de724fSAlan Cox * Hold each of the physical pages that are mapped by the specified range of 121482de724fSAlan Cox * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid 121582de724fSAlan Cox * and allow the specified types of access, "prot". If all of the implied 121682de724fSAlan Cox * pages are successfully held, then the number of held pages is returned 121782de724fSAlan Cox * together with pointers to those pages in the array "ma". However, if any 121882de724fSAlan Cox * of the pages cannot be held, -1 is returned. 121982de724fSAlan Cox */ 122082de724fSAlan Cox int 122182de724fSAlan Cox vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len, 122282de724fSAlan Cox vm_prot_t prot, vm_page_t *ma, int max_count) 122382de724fSAlan Cox { 122482de724fSAlan Cox vm_offset_t end, va; 122582de724fSAlan Cox vm_page_t *mp; 12267e14088dSKonstantin Belousov int count; 122782de724fSAlan Cox boolean_t pmap_failed; 122882de724fSAlan Cox 1229af32c419SKonstantin Belousov if (len == 0) 1230af32c419SKonstantin Belousov return (0); 123182de724fSAlan Cox end = round_page(addr + len); 123282de724fSAlan Cox addr = trunc_page(addr); 123382de724fSAlan Cox 123482de724fSAlan Cox /* 123582de724fSAlan Cox * Check for illegal addresses. 123682de724fSAlan Cox */ 123782de724fSAlan Cox if (addr < vm_map_min(map) || addr > end || end > vm_map_max(map)) 123882de724fSAlan Cox return (-1); 123982de724fSAlan Cox 12407e14088dSKonstantin Belousov if (atop(end - addr) > max_count) 124182de724fSAlan Cox panic("vm_fault_quick_hold_pages: count > max_count"); 12427e14088dSKonstantin Belousov count = atop(end - addr); 124382de724fSAlan Cox 124482de724fSAlan Cox /* 124582de724fSAlan Cox * Most likely, the physical pages are resident in the pmap, so it is 124682de724fSAlan Cox * faster to try pmap_extract_and_hold() first. 124782de724fSAlan Cox */ 124882de724fSAlan Cox pmap_failed = FALSE; 124982de724fSAlan Cox for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) { 125082de724fSAlan Cox *mp = pmap_extract_and_hold(map->pmap, va, prot); 125182de724fSAlan Cox if (*mp == NULL) 125282de724fSAlan Cox pmap_failed = TRUE; 125382de724fSAlan Cox else if ((prot & VM_PROT_WRITE) != 0 && 1254a5dbab54SAlan Cox (*mp)->dirty != VM_PAGE_BITS_ALL) { 125582de724fSAlan Cox /* 125682de724fSAlan Cox * Explicitly dirty the physical page. Otherwise, the 125782de724fSAlan Cox * caller's changes may go unnoticed because they are 125882de724fSAlan Cox * performed through an unmanaged mapping or by a DMA 125982de724fSAlan Cox * operation. 12603c76db4cSAlan Cox * 1261abb9b935SKonstantin Belousov * The object lock is not held here. 1262abb9b935SKonstantin Belousov * See vm_page_clear_dirty_mask(). 126382de724fSAlan Cox */ 12643c76db4cSAlan Cox vm_page_dirty(*mp); 126582de724fSAlan Cox } 126682de724fSAlan Cox } 126782de724fSAlan Cox if (pmap_failed) { 126882de724fSAlan Cox /* 126982de724fSAlan Cox * One or more pages could not be held by the pmap. Either no 127082de724fSAlan Cox * page was mapped at the specified virtual address or that 127182de724fSAlan Cox * mapping had insufficient permissions. Attempt to fault in 127282de724fSAlan Cox * and hold these pages. 127382de724fSAlan Cox */ 127482de724fSAlan Cox for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) 1275be996836SAttilio Rao if (*mp == NULL && vm_fault_hold(map, va, prot, 127682de724fSAlan Cox VM_FAULT_NORMAL, mp) != KERN_SUCCESS) 127782de724fSAlan Cox goto error; 127882de724fSAlan Cox } 127982de724fSAlan Cox return (count); 128082de724fSAlan Cox error: 128182de724fSAlan Cox for (mp = ma; mp < ma + count; mp++) 128282de724fSAlan Cox if (*mp != NULL) { 128382de724fSAlan Cox vm_page_lock(*mp); 128482de724fSAlan Cox vm_page_unhold(*mp); 128582de724fSAlan Cox vm_page_unlock(*mp); 128682de724fSAlan Cox } 128782de724fSAlan Cox return (-1); 128882de724fSAlan Cox } 128982de724fSAlan Cox 129082de724fSAlan Cox /* 1291df8bae1dSRodney W. Grimes * Routine: 1292df8bae1dSRodney W. Grimes * vm_fault_copy_entry 1293df8bae1dSRodney W. Grimes * Function: 1294210a6886SKonstantin Belousov * Create new shadow object backing dst_entry with private copy of 1295210a6886SKonstantin Belousov * all underlying pages. When src_entry is equal to dst_entry, 1296210a6886SKonstantin Belousov * function implements COW for wired-down map entry. Otherwise, 1297210a6886SKonstantin Belousov * it forks wired entry into dst_map. 1298df8bae1dSRodney W. Grimes * 1299df8bae1dSRodney W. Grimes * In/out conditions: 1300df8bae1dSRodney W. Grimes * The source and destination maps must be locked for write. 1301df8bae1dSRodney W. Grimes * The source map entry must be wired down (or be a sharing map 1302df8bae1dSRodney W. Grimes * entry corresponding to a main map entry that is wired down). 1303df8bae1dSRodney W. Grimes */ 130426f9a767SRodney W. Grimes void 1305121fd461SKonstantin Belousov vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map, 1306121fd461SKonstantin Belousov vm_map_entry_t dst_entry, vm_map_entry_t src_entry, 1307121fd461SKonstantin Belousov vm_ooffset_t *fork_charge) 1308df8bae1dSRodney W. Grimes { 1309210a6886SKonstantin Belousov vm_object_t backing_object, dst_object, object, src_object; 13107afab86cSAlan Cox vm_pindex_t dst_pindex, pindex, src_pindex; 1311210a6886SKonstantin Belousov vm_prot_t access, prot; 1312df8bae1dSRodney W. Grimes vm_offset_t vaddr; 1313df8bae1dSRodney W. Grimes vm_page_t dst_m; 1314df8bae1dSRodney W. Grimes vm_page_t src_m; 13154c74acf7SKonstantin Belousov boolean_t upgrade; 1316df8bae1dSRodney W. Grimes 1317df8bae1dSRodney W. Grimes #ifdef lint 1318df8bae1dSRodney W. Grimes src_map++; 13190d94caffSDavid Greenman #endif /* lint */ 1320df8bae1dSRodney W. Grimes 1321210a6886SKonstantin Belousov upgrade = src_entry == dst_entry; 13220973283dSKonstantin Belousov access = prot = dst_entry->protection; 1323210a6886SKonstantin Belousov 1324df8bae1dSRodney W. Grimes src_object = src_entry->object.vm_object; 13257afab86cSAlan Cox src_pindex = OFF_TO_IDX(src_entry->offset); 1326df8bae1dSRodney W. Grimes 13270973283dSKonstantin Belousov if (upgrade && (dst_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) { 13280973283dSKonstantin Belousov dst_object = src_object; 13290973283dSKonstantin Belousov vm_object_reference(dst_object); 13300973283dSKonstantin Belousov } else { 1331df8bae1dSRodney W. Grimes /* 13320d94caffSDavid Greenman * Create the top-level object for the destination entry. (Doesn't 13330d94caffSDavid Greenman * actually shadow anything - we copy the pages directly.) 1334df8bae1dSRodney W. Grimes */ 133524a1cce3SDavid Greenman dst_object = vm_object_allocate(OBJT_DEFAULT, 133657b5187bSAlan Cox OFF_TO_IDX(dst_entry->end - dst_entry->start)); 1337f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 1338f8a47341SAlan Cox dst_object->flags |= OBJ_COLORED; 1339f8a47341SAlan Cox dst_object->pg_color = atop(dst_entry->start); 1340f8a47341SAlan Cox #endif 13410973283dSKonstantin Belousov } 1342df8bae1dSRodney W. Grimes 134389f6b863SAttilio Rao VM_OBJECT_WLOCK(dst_object); 1344210a6886SKonstantin Belousov KASSERT(upgrade || dst_entry->object.vm_object == NULL, 1345121fd461SKonstantin Belousov ("vm_fault_copy_entry: vm_object not NULL")); 13460973283dSKonstantin Belousov if (src_object != dst_object) { 1347df8bae1dSRodney W. Grimes dst_entry->object.vm_object = dst_object; 1348df8bae1dSRodney W. Grimes dst_entry->offset = 0; 13493364c323SKonstantin Belousov dst_object->charge = dst_entry->end - dst_entry->start; 13500973283dSKonstantin Belousov } 1351210a6886SKonstantin Belousov if (fork_charge != NULL) { 1352ef694c1aSEdward Tomasz Napierala KASSERT(dst_entry->cred == NULL, 1353121fd461SKonstantin Belousov ("vm_fault_copy_entry: leaked swp charge")); 1354ef694c1aSEdward Tomasz Napierala dst_object->cred = curthread->td_ucred; 1355ef694c1aSEdward Tomasz Napierala crhold(dst_object->cred); 1356121fd461SKonstantin Belousov *fork_charge += dst_object->charge; 13570973283dSKonstantin Belousov } else if (dst_object->cred == NULL) { 13580973283dSKonstantin Belousov KASSERT(dst_entry->cred != NULL, ("no cred for entry %p", 13590973283dSKonstantin Belousov dst_entry)); 1360ef694c1aSEdward Tomasz Napierala dst_object->cred = dst_entry->cred; 1361ef694c1aSEdward Tomasz Napierala dst_entry->cred = NULL; 1362210a6886SKonstantin Belousov } 13630973283dSKonstantin Belousov 1364210a6886SKonstantin Belousov /* 1365210a6886SKonstantin Belousov * If not an upgrade, then enter the mappings in the pmap as 1366210a6886SKonstantin Belousov * read and/or execute accesses. Otherwise, enter them as 1367210a6886SKonstantin Belousov * write accesses. 1368210a6886SKonstantin Belousov * 1369210a6886SKonstantin Belousov * A writeable large page mapping is only created if all of 1370210a6886SKonstantin Belousov * the constituent small page mappings are modified. Marking 1371210a6886SKonstantin Belousov * PTEs as modified on inception allows promotion to happen 1372210a6886SKonstantin Belousov * without taking potentially large number of soft faults. 1373210a6886SKonstantin Belousov */ 1374210a6886SKonstantin Belousov if (!upgrade) 1375210a6886SKonstantin Belousov access &= ~VM_PROT_WRITE; 1376df8bae1dSRodney W. Grimes 1377df8bae1dSRodney W. Grimes /* 1378ef45823eSKonstantin Belousov * Loop through all of the virtual pages within the entry's 1379ef45823eSKonstantin Belousov * range, copying each page from the source object to the 1380ef45823eSKonstantin Belousov * destination object. Since the source is wired, those pages 1381ef45823eSKonstantin Belousov * must exist. In contrast, the destination is pageable. 1382ef45823eSKonstantin Belousov * Since the destination object does share any backing storage 1383ef45823eSKonstantin Belousov * with the source object, all of its pages must be dirtied, 1384ef45823eSKonstantin Belousov * regardless of whether they can be written. 1385df8bae1dSRodney W. Grimes */ 13867afab86cSAlan Cox for (vaddr = dst_entry->start, dst_pindex = 0; 1387df8bae1dSRodney W. Grimes vaddr < dst_entry->end; 13887afab86cSAlan Cox vaddr += PAGE_SIZE, dst_pindex++) { 13890973283dSKonstantin Belousov again: 1390df8bae1dSRodney W. Grimes /* 1391df8bae1dSRodney W. Grimes * Find the page in the source object, and copy it in. 13924c74acf7SKonstantin Belousov * Because the source is wired down, the page will be 13934c74acf7SKonstantin Belousov * in memory. 1394df8bae1dSRodney W. Grimes */ 13950973283dSKonstantin Belousov if (src_object != dst_object) 139683b375eaSAttilio Rao VM_OBJECT_RLOCK(src_object); 1397c5b65a67SAlan Cox object = src_object; 13987afab86cSAlan Cox pindex = src_pindex + dst_pindex; 13997afab86cSAlan Cox while ((src_m = vm_page_lookup(object, pindex)) == NULL && 1400c5b65a67SAlan Cox (backing_object = object->backing_object) != NULL) { 1401c5b65a67SAlan Cox /* 14024c74acf7SKonstantin Belousov * Unless the source mapping is read-only or 14034c74acf7SKonstantin Belousov * it is presently being upgraded from 14044c74acf7SKonstantin Belousov * read-only, the first object in the shadow 14054c74acf7SKonstantin Belousov * chain should provide all of the pages. In 14064c74acf7SKonstantin Belousov * other words, this loop body should never be 14074c74acf7SKonstantin Belousov * executed when the source mapping is already 14084c74acf7SKonstantin Belousov * read/write. 1409c5b65a67SAlan Cox */ 14104c74acf7SKonstantin Belousov KASSERT((src_entry->protection & VM_PROT_WRITE) == 0 || 14114c74acf7SKonstantin Belousov upgrade, 14124c74acf7SKonstantin Belousov ("vm_fault_copy_entry: main object missing page")); 14134c74acf7SKonstantin Belousov 141483b375eaSAttilio Rao VM_OBJECT_RLOCK(backing_object); 1415c5b65a67SAlan Cox pindex += OFF_TO_IDX(object->backing_object_offset); 14160973283dSKonstantin Belousov if (object != dst_object) 141783b375eaSAttilio Rao VM_OBJECT_RUNLOCK(object); 1418c5b65a67SAlan Cox object = backing_object; 1419c5b65a67SAlan Cox } 14204c74acf7SKonstantin Belousov KASSERT(src_m != NULL, ("vm_fault_copy_entry: page missing")); 14210973283dSKonstantin Belousov 14220973283dSKonstantin Belousov if (object != dst_object) { 14230973283dSKonstantin Belousov /* 14240973283dSKonstantin Belousov * Allocate a page in the destination object. 14250973283dSKonstantin Belousov */ 14262602a2eaSKonstantin Belousov dst_m = vm_page_alloc(dst_object, (src_object == 14272602a2eaSKonstantin Belousov dst_object ? src_pindex : 0) + dst_pindex, 14282602a2eaSKonstantin Belousov VM_ALLOC_NORMAL); 14290973283dSKonstantin Belousov if (dst_m == NULL) { 14300973283dSKonstantin Belousov VM_OBJECT_WUNLOCK(dst_object); 14310973283dSKonstantin Belousov VM_OBJECT_RUNLOCK(object); 14320973283dSKonstantin Belousov VM_WAIT; 1433c8f780e3SKonstantin Belousov VM_OBJECT_WLOCK(dst_object); 14340973283dSKonstantin Belousov goto again; 14350973283dSKonstantin Belousov } 1436669890eaSAlan Cox pmap_copy_page(src_m, dst_m); 143783b375eaSAttilio Rao VM_OBJECT_RUNLOCK(object); 1438669890eaSAlan Cox dst_m->valid = VM_PAGE_BITS_ALL; 1439bc79b37fSKonstantin Belousov dst_m->dirty = VM_PAGE_BITS_ALL; 14400973283dSKonstantin Belousov } else { 14410973283dSKonstantin Belousov dst_m = src_m; 14420973283dSKonstantin Belousov if (vm_page_sleep_if_busy(dst_m, "fltupg")) 14430973283dSKonstantin Belousov goto again; 14440973283dSKonstantin Belousov vm_page_xbusy(dst_m); 14450973283dSKonstantin Belousov KASSERT(dst_m->valid == VM_PAGE_BITS_ALL, 14460973283dSKonstantin Belousov ("invalid dst page %p", dst_m)); 14470973283dSKonstantin Belousov } 144889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(dst_object); 1449df8bae1dSRodney W. Grimes 1450df8bae1dSRodney W. Grimes /* 1451210a6886SKonstantin Belousov * Enter it in the pmap. If a wired, copy-on-write 1452210a6886SKonstantin Belousov * mapping is being replaced by a write-enabled 1453210a6886SKonstantin Belousov * mapping, then wire that new mapping. 1454df8bae1dSRodney W. Grimes */ 145539ffa8c1SKonstantin Belousov pmap_enter(dst_map->pmap, vaddr, dst_m, prot, 145639ffa8c1SKonstantin Belousov access | (upgrade ? PMAP_ENTER_WIRED : 0), 0); 1457df8bae1dSRodney W. Grimes 1458df8bae1dSRodney W. Grimes /* 1459df8bae1dSRodney W. Grimes * Mark it no longer busy, and put it on the active list. 1460df8bae1dSRodney W. Grimes */ 146189f6b863SAttilio Rao VM_OBJECT_WLOCK(dst_object); 14622965a453SKip Macy 1463210a6886SKonstantin Belousov if (upgrade) { 14640973283dSKonstantin Belousov if (src_m != dst_m) { 14652965a453SKip Macy vm_page_lock(src_m); 14663ae10f74SAttilio Rao vm_page_unwire(src_m, PQ_INACTIVE); 1467e20e8c15SKonstantin Belousov vm_page_unlock(src_m); 14682965a453SKip Macy vm_page_lock(dst_m); 1469210a6886SKonstantin Belousov vm_page_wire(dst_m); 1470e20e8c15SKonstantin Belousov vm_page_unlock(dst_m); 14712965a453SKip Macy } else { 14720973283dSKonstantin Belousov KASSERT(dst_m->wire_count > 0, 14730973283dSKonstantin Belousov ("dst_m %p is not wired", dst_m)); 14740973283dSKonstantin Belousov } 14750973283dSKonstantin Belousov } else { 14762965a453SKip Macy vm_page_lock(dst_m); 1477df8bae1dSRodney W. Grimes vm_page_activate(dst_m); 1478e20e8c15SKonstantin Belousov vm_page_unlock(dst_m); 14792965a453SKip Macy } 1480c7aebda8SAttilio Rao vm_page_xunbusy(dst_m); 1481df8bae1dSRodney W. Grimes } 148289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(dst_object); 1483210a6886SKonstantin Belousov if (upgrade) { 1484210a6886SKonstantin Belousov dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY); 1485210a6886SKonstantin Belousov vm_object_deallocate(src_object); 1486210a6886SKonstantin Belousov } 1487df8bae1dSRodney W. Grimes } 148826f9a767SRodney W. Grimes 14895730afc9SAlan Cox /* 14905730afc9SAlan Cox * Block entry into the machine-independent layer's page fault handler by 14915730afc9SAlan Cox * the calling thread. Subsequent calls to vm_fault() by that thread will 14925730afc9SAlan Cox * return KERN_PROTECTION_FAILURE. Enable machine-dependent handling of 14935730afc9SAlan Cox * spurious page faults. 14945730afc9SAlan Cox */ 14952801687dSKonstantin Belousov int 14962801687dSKonstantin Belousov vm_fault_disable_pagefaults(void) 14972801687dSKonstantin Belousov { 14982801687dSKonstantin Belousov 14995730afc9SAlan Cox return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR)); 15002801687dSKonstantin Belousov } 15012801687dSKonstantin Belousov 15022801687dSKonstantin Belousov void 15032801687dSKonstantin Belousov vm_fault_enable_pagefaults(int save) 15042801687dSKonstantin Belousov { 15052801687dSKonstantin Belousov 15062801687dSKonstantin Belousov curthread_pflags_restore(save); 15072801687dSKonstantin Belousov } 1508