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; 125dc5401d2SKonstantin Belousov int map_generation; 126cd8a6fe8SAlan Cox bool lookup_still_valid; 1274866e085SJohn Dyson struct vnode *vp; 1284866e085SJohn Dyson }; 1294866e085SJohn Dyson 130a8b0f100SAlan Cox static void vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr, 131a8b0f100SAlan Cox int ahead); 13263281952SAlan Cox static void vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra, 133b0cd2017SGleb Smirnoff int backward, int forward); 13413458803SAlan Cox 13562a59e8fSWarner Losh static inline void 1364866e085SJohn Dyson release_page(struct faultstate *fs) 1374866e085SJohn Dyson { 1380d0be82aSKonstantin Belousov 139c7aebda8SAttilio Rao vm_page_xunbusy(fs->m); 1402965a453SKip Macy vm_page_lock(fs->m); 1414866e085SJohn Dyson vm_page_deactivate(fs->m); 1422965a453SKip Macy vm_page_unlock(fs->m); 1434866e085SJohn Dyson fs->m = NULL; 1444866e085SJohn Dyson } 1454866e085SJohn Dyson 14662a59e8fSWarner Losh static inline void 1474866e085SJohn Dyson unlock_map(struct faultstate *fs) 1484866e085SJohn Dyson { 1490d0be82aSKonstantin Belousov 15025adb370SBrian Feldman if (fs->lookup_still_valid) { 1514866e085SJohn Dyson vm_map_lookup_done(fs->map, fs->entry); 152cd8a6fe8SAlan Cox fs->lookup_still_valid = false; 1534866e085SJohn Dyson } 1544866e085SJohn Dyson } 1554866e085SJohn Dyson 1564866e085SJohn Dyson static void 157cfabea3dSKonstantin Belousov unlock_vp(struct faultstate *fs) 158cfabea3dSKonstantin Belousov { 159cfabea3dSKonstantin Belousov 160cfabea3dSKonstantin Belousov if (fs->vp != NULL) { 161cfabea3dSKonstantin Belousov vput(fs->vp); 162cfabea3dSKonstantin Belousov fs->vp = NULL; 163cfabea3dSKonstantin Belousov } 164cfabea3dSKonstantin Belousov } 165cfabea3dSKonstantin Belousov 166cfabea3dSKonstantin Belousov static void 167a51b0840SAlan Cox unlock_and_deallocate(struct faultstate *fs) 1684866e085SJohn Dyson { 169f29ba63eSAlan Cox 1704866e085SJohn Dyson vm_object_pip_wakeup(fs->object); 17189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(fs->object); 1724866e085SJohn Dyson if (fs->object != fs->first_object) { 17389f6b863SAttilio Rao VM_OBJECT_WLOCK(fs->first_object); 1742965a453SKip Macy vm_page_lock(fs->first_m); 1754866e085SJohn Dyson vm_page_free(fs->first_m); 1762965a453SKip Macy vm_page_unlock(fs->first_m); 1774866e085SJohn Dyson vm_object_pip_wakeup(fs->first_object); 17889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(fs->first_object); 1794866e085SJohn Dyson fs->first_m = NULL; 1804866e085SJohn Dyson } 1814866e085SJohn Dyson vm_object_deallocate(fs->first_object); 1824866e085SJohn Dyson unlock_map(fs); 183cfabea3dSKonstantin Belousov unlock_vp(fs); 1844866e085SJohn Dyson } 1854866e085SJohn Dyson 186a36f5532SKonstantin Belousov static void 187a36f5532SKonstantin Belousov vm_fault_dirty(vm_map_entry_t entry, vm_page_t m, vm_prot_t prot, 188e26236e9SKonstantin Belousov vm_prot_t fault_type, int fault_flags, bool set_wd) 189a36f5532SKonstantin Belousov { 190e26236e9SKonstantin Belousov bool need_dirty; 191a36f5532SKonstantin Belousov 192a36f5532SKonstantin Belousov if (((prot & VM_PROT_WRITE) == 0 && 193a36f5532SKonstantin Belousov (fault_flags & VM_FAULT_DIRTY) == 0) || 194a36f5532SKonstantin Belousov (m->oflags & VPO_UNMANAGED) != 0) 195a36f5532SKonstantin Belousov return; 196a36f5532SKonstantin Belousov 197a36f5532SKonstantin Belousov VM_OBJECT_ASSERT_LOCKED(m->object); 198a36f5532SKonstantin Belousov 199a36f5532SKonstantin Belousov need_dirty = ((fault_type & VM_PROT_WRITE) != 0 && 2006a875bf9SKonstantin Belousov (fault_flags & VM_FAULT_WIRE) == 0) || 201a36f5532SKonstantin Belousov (fault_flags & VM_FAULT_DIRTY) != 0; 202a36f5532SKonstantin Belousov 203a36f5532SKonstantin Belousov if (set_wd) 204a36f5532SKonstantin Belousov vm_object_set_writeable_dirty(m->object); 205a36f5532SKonstantin Belousov else 206a36f5532SKonstantin Belousov /* 207a36f5532SKonstantin Belousov * If two callers of vm_fault_dirty() with set_wd == 208a36f5532SKonstantin Belousov * FALSE, one for the map entry with MAP_ENTRY_NOSYNC 209a36f5532SKonstantin Belousov * flag set, other with flag clear, race, it is 210a36f5532SKonstantin Belousov * possible for the no-NOSYNC thread to see m->dirty 211a36f5532SKonstantin Belousov * != 0 and not clear VPO_NOSYNC. Take vm_page lock 212a36f5532SKonstantin Belousov * around manipulation of VPO_NOSYNC and 213a36f5532SKonstantin Belousov * vm_page_dirty() call, to avoid the race and keep 214a36f5532SKonstantin Belousov * m->oflags consistent. 215a36f5532SKonstantin Belousov */ 216a36f5532SKonstantin Belousov vm_page_lock(m); 217a36f5532SKonstantin Belousov 218a36f5532SKonstantin Belousov /* 219a36f5532SKonstantin Belousov * If this is a NOSYNC mmap we do not want to set VPO_NOSYNC 220a36f5532SKonstantin Belousov * if the page is already dirty to prevent data written with 221a36f5532SKonstantin Belousov * the expectation of being synced from not being synced. 222a36f5532SKonstantin Belousov * Likewise if this entry does not request NOSYNC then make 223a36f5532SKonstantin Belousov * sure the page isn't marked NOSYNC. Applications sharing 224a36f5532SKonstantin Belousov * data should use the same flags to avoid ping ponging. 225a36f5532SKonstantin Belousov */ 226a36f5532SKonstantin Belousov if ((entry->eflags & MAP_ENTRY_NOSYNC) != 0) { 227a36f5532SKonstantin Belousov if (m->dirty == 0) { 228a36f5532SKonstantin Belousov m->oflags |= VPO_NOSYNC; 229a36f5532SKonstantin Belousov } 230a36f5532SKonstantin Belousov } else { 231a36f5532SKonstantin Belousov m->oflags &= ~VPO_NOSYNC; 232a36f5532SKonstantin Belousov } 233a36f5532SKonstantin Belousov 234a36f5532SKonstantin Belousov /* 235a36f5532SKonstantin Belousov * If the fault is a write, we know that this page is being 236a36f5532SKonstantin Belousov * written NOW so dirty it explicitly to save on 237a36f5532SKonstantin Belousov * pmap_is_modified() calls later. 238a36f5532SKonstantin Belousov * 239a36f5532SKonstantin Belousov * Also tell the backing pager, if any, that it should remove 240a36f5532SKonstantin Belousov * any swap backing since the page is now dirty. 241a36f5532SKonstantin Belousov */ 242a36f5532SKonstantin Belousov if (need_dirty) 243a36f5532SKonstantin Belousov vm_page_dirty(m); 244a36f5532SKonstantin Belousov if (!set_wd) 245a36f5532SKonstantin Belousov vm_page_unlock(m); 246a36f5532SKonstantin Belousov if (need_dirty) 247a36f5532SKonstantin Belousov vm_pager_page_unswapped(m); 248a36f5532SKonstantin Belousov } 249a36f5532SKonstantin Belousov 25041ddec83SKonstantin Belousov static void 25141ddec83SKonstantin Belousov vm_fault_fill_hold(vm_page_t *m_hold, vm_page_t m) 25241ddec83SKonstantin Belousov { 25341ddec83SKonstantin Belousov 25441ddec83SKonstantin Belousov if (m_hold != NULL) { 25541ddec83SKonstantin Belousov *m_hold = m; 25641ddec83SKonstantin Belousov vm_page_lock(m); 25741ddec83SKonstantin Belousov vm_page_hold(m); 25841ddec83SKonstantin Belousov vm_page_unlock(m); 25941ddec83SKonstantin Belousov } 26041ddec83SKonstantin Belousov } 26141ddec83SKonstantin Belousov 26241ddec83SKonstantin Belousov /* 26341ddec83SKonstantin Belousov * Unlocks fs.first_object and fs.map on success. 26441ddec83SKonstantin Belousov */ 26541ddec83SKonstantin Belousov static int 26641ddec83SKonstantin Belousov vm_fault_soft_fast(struct faultstate *fs, vm_offset_t vaddr, vm_prot_t prot, 26741ddec83SKonstantin Belousov int fault_type, int fault_flags, boolean_t wired, vm_page_t *m_hold) 26841ddec83SKonstantin Belousov { 26941ddec83SKonstantin Belousov vm_page_t m; 27041ddec83SKonstantin Belousov int rv; 27141ddec83SKonstantin Belousov 27241ddec83SKonstantin Belousov MPASS(fs->vp == NULL); 27341ddec83SKonstantin Belousov m = vm_page_lookup(fs->first_object, fs->first_pindex); 27441ddec83SKonstantin Belousov /* A busy page can be mapped for read|execute access. */ 27541ddec83SKonstantin Belousov if (m == NULL || ((prot & VM_PROT_WRITE) != 0 && 27641ddec83SKonstantin Belousov vm_page_busied(m)) || m->valid != VM_PAGE_BITS_ALL) 27741ddec83SKonstantin Belousov return (KERN_FAILURE); 27841ddec83SKonstantin Belousov rv = pmap_enter(fs->map->pmap, vaddr, m, prot, fault_type | 27941ddec83SKonstantin Belousov PMAP_ENTER_NOSLEEP | (wired ? PMAP_ENTER_WIRED : 0), 0); 28041ddec83SKonstantin Belousov if (rv != KERN_SUCCESS) 28141ddec83SKonstantin Belousov return (rv); 28241ddec83SKonstantin Belousov vm_fault_fill_hold(m_hold, m); 28341ddec83SKonstantin Belousov vm_fault_dirty(fs->entry, m, prot, fault_type, fault_flags, false); 28441ddec83SKonstantin Belousov VM_OBJECT_RUNLOCK(fs->first_object); 28541ddec83SKonstantin Belousov if (!wired) 28641ddec83SKonstantin Belousov vm_fault_prefault(fs, vaddr, PFBAK, PFFOR); 28741ddec83SKonstantin Belousov vm_map_lookup_done(fs->map, fs->entry); 28841ddec83SKonstantin Belousov curthread->td_ru.ru_minflt++; 28941ddec83SKonstantin Belousov return (KERN_SUCCESS); 29041ddec83SKonstantin Belousov } 29141ddec83SKonstantin Belousov 292c42b43a0SKonstantin Belousov static void 293c42b43a0SKonstantin Belousov vm_fault_restore_map_lock(struct faultstate *fs) 294c42b43a0SKonstantin Belousov { 295c42b43a0SKonstantin Belousov 296c42b43a0SKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED(fs->first_object); 297c42b43a0SKonstantin Belousov MPASS(fs->first_object->paging_in_progress > 0); 298c42b43a0SKonstantin Belousov 299c42b43a0SKonstantin Belousov if (!vm_map_trylock_read(fs->map)) { 300c42b43a0SKonstantin Belousov VM_OBJECT_WUNLOCK(fs->first_object); 301c42b43a0SKonstantin Belousov vm_map_lock_read(fs->map); 302c42b43a0SKonstantin Belousov VM_OBJECT_WLOCK(fs->first_object); 303c42b43a0SKonstantin Belousov } 304c42b43a0SKonstantin Belousov fs->lookup_still_valid = true; 305c42b43a0SKonstantin Belousov } 306c42b43a0SKonstantin Belousov 3077a432b84SKonstantin Belousov static void 3087a432b84SKonstantin Belousov vm_fault_populate_check_page(vm_page_t m) 3097a432b84SKonstantin Belousov { 3107a432b84SKonstantin Belousov 3117a432b84SKonstantin Belousov /* 3127a432b84SKonstantin Belousov * Check each page to ensure that the pager is obeying the 3137a432b84SKonstantin Belousov * interface: the page must be installed in the object, fully 3147a432b84SKonstantin Belousov * valid, and exclusively busied. 3157a432b84SKonstantin Belousov */ 3167a432b84SKonstantin Belousov MPASS(m != NULL); 3177a432b84SKonstantin Belousov MPASS(m->valid == VM_PAGE_BITS_ALL); 3187a432b84SKonstantin Belousov MPASS(vm_page_xbusied(m)); 3197a432b84SKonstantin Belousov } 3207a432b84SKonstantin Belousov 3217a432b84SKonstantin Belousov static void 3227a432b84SKonstantin Belousov vm_fault_populate_cleanup(vm_object_t object, vm_pindex_t first, 3237a432b84SKonstantin Belousov vm_pindex_t last) 3247a432b84SKonstantin Belousov { 3257a432b84SKonstantin Belousov vm_page_t m; 3267a432b84SKonstantin Belousov vm_pindex_t pidx; 3277a432b84SKonstantin Belousov 3287a432b84SKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED(object); 3297a432b84SKonstantin Belousov MPASS(first <= last); 3307a432b84SKonstantin Belousov for (pidx = first, m = vm_page_lookup(object, pidx); 3317a432b84SKonstantin Belousov pidx <= last; pidx++, m = vm_page_next(m)) { 3327a432b84SKonstantin Belousov vm_fault_populate_check_page(m); 3337a432b84SKonstantin Belousov vm_page_lock(m); 3347a432b84SKonstantin Belousov vm_page_deactivate(m); 3357a432b84SKonstantin Belousov vm_page_unlock(m); 3367a432b84SKonstantin Belousov vm_page_xunbusy(m); 3377a432b84SKonstantin Belousov } 3387a432b84SKonstantin Belousov } 339c42b43a0SKonstantin Belousov 340c42b43a0SKonstantin Belousov static int 341c42b43a0SKonstantin Belousov vm_fault_populate(struct faultstate *fs, vm_offset_t vaddr, vm_prot_t prot, 342c42b43a0SKonstantin Belousov int fault_type, int fault_flags, boolean_t wired, vm_page_t *m_hold) 343c42b43a0SKonstantin Belousov { 344c42b43a0SKonstantin Belousov vm_page_t m; 3457a432b84SKonstantin Belousov vm_pindex_t map_first, map_last, pager_first, pager_last, pidx; 346c42b43a0SKonstantin Belousov int rv; 347c42b43a0SKonstantin Belousov 348c42b43a0SKonstantin Belousov MPASS(fs->object == fs->first_object); 349c42b43a0SKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED(fs->first_object); 350c42b43a0SKonstantin Belousov MPASS(fs->first_object->paging_in_progress > 0); 351c42b43a0SKonstantin Belousov MPASS(fs->first_object->backing_object == NULL); 352c42b43a0SKonstantin Belousov MPASS(fs->lookup_still_valid); 353c42b43a0SKonstantin Belousov 3547a432b84SKonstantin Belousov pager_first = OFF_TO_IDX(fs->entry->offset); 3557a432b84SKonstantin Belousov pager_last = OFF_TO_IDX(fs->entry->offset + fs->entry->end - 356c42b43a0SKonstantin Belousov fs->entry->start) - 1; 357c42b43a0SKonstantin Belousov unlock_map(fs); 358c42b43a0SKonstantin Belousov unlock_vp(fs); 359c42b43a0SKonstantin Belousov 360c42b43a0SKonstantin Belousov /* 361c42b43a0SKonstantin Belousov * Call the pager (driver) populate() method. 362c42b43a0SKonstantin Belousov * 363c42b43a0SKonstantin Belousov * There is no guarantee that the method will be called again 364c42b43a0SKonstantin Belousov * if the current fault is for read, and a future fault is 365c42b43a0SKonstantin Belousov * for write. Report the entry's maximum allowed protection 366c42b43a0SKonstantin Belousov * to the driver. 367c42b43a0SKonstantin Belousov */ 368c42b43a0SKonstantin Belousov rv = vm_pager_populate(fs->first_object, fs->first_pindex, 3697a432b84SKonstantin Belousov fault_type, fs->entry->max_protection, &pager_first, &pager_last); 370c42b43a0SKonstantin Belousov 371c42b43a0SKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED(fs->first_object); 372c42b43a0SKonstantin Belousov if (rv == VM_PAGER_BAD) { 373c42b43a0SKonstantin Belousov /* 374c42b43a0SKonstantin Belousov * VM_PAGER_BAD is the backdoor for a pager to request 375c42b43a0SKonstantin Belousov * normal fault handling. 376c42b43a0SKonstantin Belousov */ 377c42b43a0SKonstantin Belousov vm_fault_restore_map_lock(fs); 378c42b43a0SKonstantin Belousov if (fs->map->timestamp != fs->map_generation) 379c42b43a0SKonstantin Belousov return (KERN_RESOURCE_SHORTAGE); /* RetryFault */ 380c42b43a0SKonstantin Belousov return (KERN_NOT_RECEIVER); 381c42b43a0SKonstantin Belousov } 382c42b43a0SKonstantin Belousov if (rv != VM_PAGER_OK) 383c42b43a0SKonstantin Belousov return (KERN_FAILURE); /* AKA SIGSEGV */ 384c42b43a0SKonstantin Belousov 385c42b43a0SKonstantin Belousov /* Ensure that the driver is obeying the interface. */ 3867a432b84SKonstantin Belousov MPASS(pager_first <= pager_last); 3877a432b84SKonstantin Belousov MPASS(fs->first_pindex <= pager_last); 3887a432b84SKonstantin Belousov MPASS(fs->first_pindex >= pager_first); 3897a432b84SKonstantin Belousov MPASS(pager_last < fs->first_object->size); 390c42b43a0SKonstantin Belousov 391c42b43a0SKonstantin Belousov vm_fault_restore_map_lock(fs); 3927a432b84SKonstantin Belousov if (fs->map->timestamp != fs->map_generation) { 3937a432b84SKonstantin Belousov vm_fault_populate_cleanup(fs->first_object, pager_first, 3947a432b84SKonstantin Belousov pager_last); 395c42b43a0SKonstantin Belousov return (KERN_RESOURCE_SHORTAGE); /* RetryFault */ 3967a432b84SKonstantin Belousov } 397c42b43a0SKonstantin Belousov 398c42b43a0SKonstantin Belousov /* 3997a432b84SKonstantin Belousov * The map is unchanged after our last unlock. Process the fault. 4007a432b84SKonstantin Belousov * 4017a432b84SKonstantin Belousov * The range [pager_first, pager_last] that is given to the 4027a432b84SKonstantin Belousov * pager is only a hint. The pager may populate any range 4037a432b84SKonstantin Belousov * within the object that includes the requested page index. 4047a432b84SKonstantin Belousov * In case the pager expanded the range, clip it to fit into 4057a432b84SKonstantin Belousov * the map entry. 406c42b43a0SKonstantin Belousov */ 4077a432b84SKonstantin Belousov map_first = MAX(OFF_TO_IDX(fs->entry->offset), pager_first); 4087a432b84SKonstantin Belousov if (map_first > pager_first) 4097a432b84SKonstantin Belousov vm_fault_populate_cleanup(fs->first_object, pager_first, 4107a432b84SKonstantin Belousov map_first - 1); 4117a432b84SKonstantin Belousov map_last = MIN(OFF_TO_IDX(fs->entry->end - fs->entry->start + 4127a432b84SKonstantin Belousov fs->entry->offset), pager_last); 4137a432b84SKonstantin Belousov if (map_last < pager_last) 4147a432b84SKonstantin Belousov vm_fault_populate_cleanup(fs->first_object, map_last + 1, 4157a432b84SKonstantin Belousov pager_last); 416c42b43a0SKonstantin Belousov 4177a432b84SKonstantin Belousov for (pidx = map_first, m = vm_page_lookup(fs->first_object, pidx); 4187a432b84SKonstantin Belousov pidx <= map_last; pidx++, m = vm_page_next(m)) { 4197a432b84SKonstantin Belousov vm_fault_populate_check_page(m); 420c42b43a0SKonstantin Belousov vm_fault_dirty(fs->entry, m, prot, fault_type, fault_flags, 421c42b43a0SKonstantin Belousov true); 422c42b43a0SKonstantin Belousov VM_OBJECT_WUNLOCK(fs->first_object); 423c42b43a0SKonstantin Belousov pmap_enter(fs->map->pmap, fs->entry->start + IDX_TO_OFF(pidx) - 424c42b43a0SKonstantin Belousov fs->entry->offset, m, prot, fault_type | (wired ? 425c42b43a0SKonstantin Belousov PMAP_ENTER_WIRED : 0), 0); 426c42b43a0SKonstantin Belousov VM_OBJECT_WLOCK(fs->first_object); 427c42b43a0SKonstantin Belousov if (pidx == fs->first_pindex) 428c42b43a0SKonstantin Belousov vm_fault_fill_hold(m_hold, m); 429c42b43a0SKonstantin Belousov vm_page_lock(m); 430c42b43a0SKonstantin Belousov if ((fault_flags & VM_FAULT_WIRE) != 0) { 431c42b43a0SKonstantin Belousov KASSERT(wired, ("VM_FAULT_WIRE && !wired")); 432c42b43a0SKonstantin Belousov vm_page_wire(m); 433c42b43a0SKonstantin Belousov } else { 434c42b43a0SKonstantin Belousov vm_page_activate(m); 435c42b43a0SKonstantin Belousov } 436c42b43a0SKonstantin Belousov vm_page_unlock(m); 437c42b43a0SKonstantin Belousov vm_page_xunbusy(m); 438c42b43a0SKonstantin Belousov } 439c42b43a0SKonstantin Belousov curthread->td_ru.ru_majflt++; 440c42b43a0SKonstantin Belousov return (KERN_SUCCESS); 441c42b43a0SKonstantin Belousov } 442c42b43a0SKonstantin Belousov 443df8bae1dSRodney W. Grimes /* 444df8bae1dSRodney W. Grimes * vm_fault: 445df8bae1dSRodney W. Grimes * 446956f3135SPhilippe Charnier * Handle a page fault occurring at the given address, 447df8bae1dSRodney W. Grimes * requiring the given permissions, in the map specified. 448df8bae1dSRodney W. Grimes * If successful, the page is inserted into the 449df8bae1dSRodney W. Grimes * associated physical map. 450df8bae1dSRodney W. Grimes * 451df8bae1dSRodney W. Grimes * NOTE: the given address should be truncated to the 452df8bae1dSRodney W. Grimes * proper page address. 453df8bae1dSRodney W. Grimes * 454df8bae1dSRodney W. Grimes * KERN_SUCCESS is returned if the page fault is handled; otherwise, 455df8bae1dSRodney W. Grimes * a standard error specifying why the fault is fatal is returned. 456df8bae1dSRodney W. Grimes * 457df8bae1dSRodney W. Grimes * The map in question must be referenced, and remains so. 4580cddd8f0SMatthew Dillon * Caller may hold no locks. 459df8bae1dSRodney W. Grimes */ 460df8bae1dSRodney W. Grimes int 46123955314SAlfred Perlstein vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, 46223955314SAlfred Perlstein int fault_flags) 46323955314SAlfred Perlstein { 46435818d2eSJohn Baldwin struct thread *td; 46535818d2eSJohn Baldwin int result; 466acd11c74SAlan Cox 46735818d2eSJohn Baldwin td = curthread; 46835818d2eSJohn Baldwin if ((td->td_pflags & TDP_NOFAULTING) != 0) 4692801687dSKonstantin Belousov return (KERN_PROTECTION_FAILURE); 47035818d2eSJohn Baldwin #ifdef KTRACE 47135818d2eSJohn Baldwin if (map != kernel_map && KTRPOINT(td, KTR_FAULT)) 47235818d2eSJohn Baldwin ktrfault(vaddr, fault_type); 47335818d2eSJohn Baldwin #endif 474be996836SAttilio Rao result = vm_fault_hold(map, trunc_page(vaddr), fault_type, fault_flags, 475be996836SAttilio Rao NULL); 47635818d2eSJohn Baldwin #ifdef KTRACE 47735818d2eSJohn Baldwin if (map != kernel_map && KTRPOINT(td, KTR_FAULTEND)) 47835818d2eSJohn Baldwin ktrfaultend(result); 47935818d2eSJohn Baldwin #endif 48035818d2eSJohn Baldwin return (result); 481acd11c74SAlan Cox } 482acd11c74SAlan Cox 483acd11c74SAlan Cox int 484be996836SAttilio Rao vm_fault_hold(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, 485acd11c74SAlan Cox int fault_flags, vm_page_t *m_hold) 486acd11c74SAlan Cox { 4874866e085SJohn Dyson struct faultstate fs; 488d2bf64c3SKonstantin Belousov struct vnode *vp; 489ebcddc72SAlan Cox vm_object_t next_object, retry_object; 4900c3a4893SAlan Cox vm_offset_t e_end, e_start; 491ebcddc72SAlan Cox vm_pindex_t retry_pindex; 492ebcddc72SAlan Cox vm_prot_t prot, retry_prot; 493f994b207SAlan Cox int ahead, alloc_req, behind, cluster_offset, error, era, faultcount; 494dc5401d2SKonstantin Belousov int locked, nera, result, rv; 4950c3a4893SAlan Cox u_char behavior; 496cd8a6fe8SAlan Cox boolean_t wired; /* Passed by reference. */ 497cd8a6fe8SAlan Cox bool dead, growstack, hardfault, is_first_object_locked; 498df8bae1dSRodney W. Grimes 49967596082SAttilio Rao PCPU_INC(cnt.v_vm_faults); 500d2bf64c3SKonstantin Belousov fs.vp = NULL; 501b0cd2017SGleb Smirnoff faultcount = 0; 5020c3a4893SAlan Cox nera = -1; 503cd8a6fe8SAlan Cox growstack = true; 504320023e2SAlan Cox hardfault = false; 505df8bae1dSRodney W. Grimes 506df8bae1dSRodney W. Grimes RetryFault:; 507df8bae1dSRodney W. Grimes 508df8bae1dSRodney W. Grimes /* 5090d94caffSDavid Greenman * Find the backing store object and offset into it to begin the 5100d94caffSDavid Greenman * search. 511df8bae1dSRodney W. Grimes */ 51240360b1bSMatthew Dillon fs.map = map; 51392de35b0SAlan Cox result = vm_map_lookup(&fs.map, vaddr, fault_type, &fs.entry, 51492de35b0SAlan Cox &fs.first_object, &fs.first_pindex, &prot, &wired); 51592de35b0SAlan Cox if (result != KERN_SUCCESS) { 5166139043bSAlan Cox if (growstack && result == KERN_INVALID_ADDRESS && 5172db65ab4SAlan Cox map != kernel_map) { 5186139043bSAlan Cox result = vm_map_growstack(curproc, vaddr); 519a976eb5eSAlan Cox if (result != KERN_SUCCESS) 5206139043bSAlan Cox return (KERN_FAILURE); 521cd8a6fe8SAlan Cox growstack = false; 5226139043bSAlan Cox goto RetryFault; 5236139043bSAlan Cox } 524a9ee028dSMark Johnston unlock_vp(&fs); 52592de35b0SAlan Cox return (result); 52609e0c6ccSJohn Dyson } 52709e0c6ccSJohn Dyson 528dc5401d2SKonstantin Belousov fs.map_generation = fs.map->timestamp; 5292d8acc0fSJohn Dyson 5304866e085SJohn Dyson if (fs.entry->eflags & MAP_ENTRY_NOFAULT) { 53105d58177SBjoern A. Zeeb panic("%s: fault on nofault entry, addr: %#lx", 53205d58177SBjoern A. Zeeb __func__, (u_long)vaddr); 5337aaaa4fdSJohn Dyson } 5347aaaa4fdSJohn Dyson 5354f9c9114SKonstantin Belousov if (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION && 5364f9c9114SKonstantin Belousov fs.entry->wiring_thread != curthread) { 5374f9c9114SKonstantin Belousov vm_map_unlock_read(fs.map); 5384f9c9114SKonstantin Belousov vm_map_lock(fs.map); 5394f9c9114SKonstantin Belousov if (vm_map_lookup_entry(fs.map, vaddr, &fs.entry) && 5404f9c9114SKonstantin Belousov (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION)) { 541cfabea3dSKonstantin Belousov unlock_vp(&fs); 5424f9c9114SKonstantin Belousov fs.entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 5434f9c9114SKonstantin Belousov vm_map_unlock_and_wait(fs.map, 0); 5444f9c9114SKonstantin Belousov } else 5454f9c9114SKonstantin Belousov vm_map_unlock(fs.map); 5464f9c9114SKonstantin Belousov goto RetryFault; 5474f9c9114SKonstantin Belousov } 5484f9c9114SKonstantin Belousov 549afe55ca3SKonstantin Belousov if (wired) 550afe55ca3SKonstantin Belousov fault_type = prot | (fault_type & VM_PROT_COPY); 5516a875bf9SKonstantin Belousov else 5526a875bf9SKonstantin Belousov KASSERT((fault_flags & VM_FAULT_WIRE) == 0, 5536a875bf9SKonstantin Belousov ("!wired && VM_FAULT_WIRE")); 554afe55ca3SKonstantin Belousov 5558d67b8c8SAlan Cox /* 5568d67b8c8SAlan Cox * Try to avoid lock contention on the top-level object through 5578d67b8c8SAlan Cox * special-case handling of some types of page faults, specifically, 5588d67b8c8SAlan Cox * those that are both (1) mapping an existing page from the top- 5598d67b8c8SAlan Cox * level object and (2) not having to mark that object as containing 5608d67b8c8SAlan Cox * dirty pages. Under these conditions, a read lock on the top-level 5618d67b8c8SAlan Cox * object suffices, allowing multiple page faults of a similar type to 5628d67b8c8SAlan Cox * run in parallel on the same top-level object. 5638d67b8c8SAlan Cox */ 564afe55ca3SKonstantin Belousov if (fs.vp == NULL /* avoid locked vnode leak */ && 5656a875bf9SKonstantin Belousov (fault_flags & (VM_FAULT_WIRE | VM_FAULT_DIRTY)) == 0 && 566afe55ca3SKonstantin Belousov /* avoid calling vm_object_set_writeable_dirty() */ 567afe55ca3SKonstantin Belousov ((prot & VM_PROT_WRITE) == 0 || 568f40cb1c6SKonstantin Belousov (fs.first_object->type != OBJT_VNODE && 569f40cb1c6SKonstantin Belousov (fs.first_object->flags & OBJ_TMPFS_NODE) == 0) || 570afe55ca3SKonstantin Belousov (fs.first_object->flags & OBJ_MIGHTBEDIRTY) != 0)) { 571afe55ca3SKonstantin Belousov VM_OBJECT_RLOCK(fs.first_object); 57241ddec83SKonstantin Belousov if ((prot & VM_PROT_WRITE) == 0 || 57341ddec83SKonstantin Belousov (fs.first_object->type != OBJT_VNODE && 57441ddec83SKonstantin Belousov (fs.first_object->flags & OBJ_TMPFS_NODE) == 0) || 57541ddec83SKonstantin Belousov (fs.first_object->flags & OBJ_MIGHTBEDIRTY) != 0) { 57641ddec83SKonstantin Belousov rv = vm_fault_soft_fast(&fs, vaddr, prot, fault_type, 57741ddec83SKonstantin Belousov fault_flags, wired, m_hold); 57841ddec83SKonstantin Belousov if (rv == KERN_SUCCESS) 57941ddec83SKonstantin Belousov return (rv); 580afe55ca3SKonstantin Belousov } 581afe55ca3SKonstantin Belousov if (!VM_OBJECT_TRYUPGRADE(fs.first_object)) { 582afe55ca3SKonstantin Belousov VM_OBJECT_RUNLOCK(fs.first_object); 583afe55ca3SKonstantin Belousov VM_OBJECT_WLOCK(fs.first_object); 584afe55ca3SKonstantin Belousov } 585afe55ca3SKonstantin Belousov } else { 586afe55ca3SKonstantin Belousov VM_OBJECT_WLOCK(fs.first_object); 587afe55ca3SKonstantin Belousov } 588afe55ca3SKonstantin Belousov 58995e5e988SJohn Dyson /* 59095e5e988SJohn Dyson * Make a reference to this object to prevent its disposal while we 59195e5e988SJohn Dyson * are messing with it. Once we have the reference, the map is free 59295e5e988SJohn Dyson * to be diddled. Since objects reference their shadows (and copies), 59395e5e988SJohn Dyson * they will stay around as well. 594fe8e0238SMatthew Dillon * 595fe8e0238SMatthew Dillon * Bump the paging-in-progress count to prevent size changes (e.g. 596dda4d369SAlan Cox * truncation operations) during I/O. 59795e5e988SJohn Dyson */ 598a976eb5eSAlan Cox vm_object_reference_locked(fs.first_object); 599d474eaaaSDoug Rabson vm_object_pip_add(fs.first_object, 1); 60095e5e988SJohn Dyson 601cd8a6fe8SAlan Cox fs.lookup_still_valid = true; 602df8bae1dSRodney W. Grimes 6034866e085SJohn Dyson fs.first_m = NULL; 604df8bae1dSRodney W. Grimes 605df8bae1dSRodney W. Grimes /* 606df8bae1dSRodney W. Grimes * Search for the page at object/offset. 607df8bae1dSRodney W. Grimes */ 6084866e085SJohn Dyson fs.object = fs.first_object; 6094866e085SJohn Dyson fs.pindex = fs.first_pindex; 610df8bae1dSRodney W. Grimes while (TRUE) { 6111c7c3c6aSMatthew Dillon /* 612725441f6SKonstantin Belousov * If the object is marked for imminent termination, 613725441f6SKonstantin Belousov * we retry here, since the collapse pass has raced 614725441f6SKonstantin Belousov * with us. Otherwise, if we see terminally dead 615725441f6SKonstantin Belousov * object, return fail. 6161c7c3c6aSMatthew Dillon */ 617725441f6SKonstantin Belousov if ((fs.object->flags & OBJ_DEAD) != 0) { 618725441f6SKonstantin Belousov dead = fs.object->type == OBJT_DEAD; 6194866e085SJohn Dyson unlock_and_deallocate(&fs); 620725441f6SKonstantin Belousov if (dead) 62147221757SJohn Dyson return (KERN_PROTECTION_FAILURE); 622725441f6SKonstantin Belousov pause("vmf_de", 1); 623725441f6SKonstantin Belousov goto RetryFault; 62447221757SJohn Dyson } 62547221757SJohn Dyson 6261c7c3c6aSMatthew Dillon /* 6271c7c3c6aSMatthew Dillon * See if page is resident 6281c7c3c6aSMatthew Dillon */ 6294866e085SJohn Dyson fs.m = vm_page_lookup(fs.object, fs.pindex); 6304866e085SJohn Dyson if (fs.m != NULL) { 63198cb733cSKenneth D. Merry /* 6321c7c3c6aSMatthew Dillon * Wait/Retry if the page is busy. We have to do this 633c7aebda8SAttilio Rao * if the page is either exclusive or shared busy 634c7aebda8SAttilio Rao * because the vm_pager may be using read busy for 635c7aebda8SAttilio Rao * pageouts (and even pageins if it is the vnode 636c7aebda8SAttilio Rao * pager), and we could end up trying to pagein and 637c7aebda8SAttilio Rao * pageout the same page simultaneously. 6381c7c3c6aSMatthew Dillon * 6391c7c3c6aSMatthew Dillon * We can theoretically allow the busy case on a read 6401c7c3c6aSMatthew Dillon * fault if the page is marked valid, but since such 6411c7c3c6aSMatthew Dillon * pages are typically already pmap'd, putting that 6421c7c3c6aSMatthew Dillon * special case in might be more effort then it is 6431c7c3c6aSMatthew Dillon * worth. We cannot under any circumstances mess 644c7aebda8SAttilio Rao * around with a shared busied page except, perhaps, 6451c7c3c6aSMatthew Dillon * to pmap it. 646df8bae1dSRodney W. Grimes */ 647c7aebda8SAttilio Rao if (vm_page_busied(fs.m)) { 648b88b6c9dSAlan Cox /* 649b88b6c9dSAlan Cox * Reference the page before unlocking and 650b88b6c9dSAlan Cox * sleeping so that the page daemon is less 651b88b6c9dSAlan Cox * likely to reclaim it. 652b88b6c9dSAlan Cox */ 6533407fefeSKonstantin Belousov vm_page_aflag_set(fs.m, PGA_REFERENCED); 654a51b0840SAlan Cox if (fs.object != fs.first_object) { 65589f6b863SAttilio Rao if (!VM_OBJECT_TRYWLOCK( 656a6e38685SKonstantin Belousov fs.first_object)) { 65789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(fs.object); 65889f6b863SAttilio Rao VM_OBJECT_WLOCK(fs.first_object); 65989f6b863SAttilio Rao VM_OBJECT_WLOCK(fs.object); 660a6e38685SKonstantin Belousov } 6612965a453SKip Macy vm_page_lock(fs.first_m); 662a51b0840SAlan Cox vm_page_free(fs.first_m); 6632965a453SKip Macy vm_page_unlock(fs.first_m); 664a51b0840SAlan Cox vm_object_pip_wakeup(fs.first_object); 66589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(fs.first_object); 666a51b0840SAlan Cox fs.first_m = NULL; 667a51b0840SAlan Cox } 668a51b0840SAlan Cox unlock_map(&fs); 669a51b0840SAlan Cox if (fs.m == vm_page_lookup(fs.object, 670a51b0840SAlan Cox fs.pindex)) { 671c7aebda8SAttilio Rao vm_page_sleep_if_busy(fs.m, "vmpfw"); 672a51b0840SAlan Cox } 673a51b0840SAlan Cox vm_object_pip_wakeup(fs.object); 67489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(fs.object); 67567596082SAttilio Rao PCPU_INC(cnt.v_intrans); 6764866e085SJohn Dyson vm_object_deallocate(fs.first_object); 677df8bae1dSRodney W. Grimes goto RetryFault; 678df8bae1dSRodney W. Grimes } 6793846a822SKonstantin Belousov vm_page_lock(fs.m); 6808d220203SAlan Cox vm_page_remque(fs.m); 6812965a453SKip Macy vm_page_unlock(fs.m); 6827615edaaSMatthew Dillon 6831c7c3c6aSMatthew Dillon /* 6841c7c3c6aSMatthew Dillon * Mark page busy for other processes, and the 6851c7c3c6aSMatthew Dillon * pagedaemon. If it still isn't completely valid 6861c7c3c6aSMatthew Dillon * (readable), jump to readrest, else break-out ( we 6871c7c3c6aSMatthew Dillon * found the page ). 6881c7c3c6aSMatthew Dillon */ 689c7aebda8SAttilio Rao vm_page_xbusy(fs.m); 690ff5958e7SAlan Cox if (fs.m->valid != VM_PAGE_BITS_ALL) 6910d94caffSDavid Greenman goto readrest; 692df8bae1dSRodney W. Grimes break; 693df8bae1dSRodney W. Grimes } 69410b4196bSAlan Cox KASSERT(fs.m == NULL, ("fs.m should be NULL, not %p", fs.m)); 6951c7c3c6aSMatthew Dillon 6961c7c3c6aSMatthew Dillon /* 69710b4196bSAlan Cox * Page is not resident. If the pager might contain the page 69810b4196bSAlan Cox * or this is the beginning of the search, allocate a new 69910b4196bSAlan Cox * page. (Default objects are zero-fill, so there is no real 70010b4196bSAlan Cox * pager for them.) 7011c7c3c6aSMatthew Dillon */ 7026a875bf9SKonstantin Belousov if (fs.object->type != OBJT_DEFAULT || 7036a875bf9SKonstantin Belousov fs.object == fs.first_object) { 7044866e085SJohn Dyson if (fs.pindex >= fs.object->size) { 7054866e085SJohn Dyson unlock_and_deallocate(&fs); 7065f55e841SDavid Greenman return (KERN_PROTECTION_FAILURE); 7075f55e841SDavid Greenman } 70822ba64e8SJohn Dyson 709c42b43a0SKonstantin Belousov if (fs.object == fs.first_object && 710c42b43a0SKonstantin Belousov (fs.first_object->flags & OBJ_POPULATE) != 0 && 711c42b43a0SKonstantin Belousov fs.first_object->shadow_count == 0) { 712c42b43a0SKonstantin Belousov rv = vm_fault_populate(&fs, vaddr, prot, 713c42b43a0SKonstantin Belousov fault_type, fault_flags, wired, m_hold); 714c42b43a0SKonstantin Belousov switch (rv) { 715c42b43a0SKonstantin Belousov case KERN_SUCCESS: 716c42b43a0SKonstantin Belousov case KERN_FAILURE: 717c42b43a0SKonstantin Belousov unlock_and_deallocate(&fs); 718c42b43a0SKonstantin Belousov return (rv); 719c42b43a0SKonstantin Belousov case KERN_RESOURCE_SHORTAGE: 720c42b43a0SKonstantin Belousov unlock_and_deallocate(&fs); 721c42b43a0SKonstantin Belousov goto RetryFault; 722c42b43a0SKonstantin Belousov case KERN_NOT_RECEIVER: 723c42b43a0SKonstantin Belousov /* 724c42b43a0SKonstantin Belousov * Pager's populate() method 725c42b43a0SKonstantin Belousov * returned VM_PAGER_BAD. 726c42b43a0SKonstantin Belousov */ 727c42b43a0SKonstantin Belousov break; 728c42b43a0SKonstantin Belousov default: 729c42b43a0SKonstantin Belousov panic("inconsistent return codes"); 730c42b43a0SKonstantin Belousov } 731c42b43a0SKonstantin Belousov } 732c42b43a0SKonstantin Belousov 733df8bae1dSRodney W. Grimes /* 7340d94caffSDavid Greenman * Allocate a new page for this object/offset pair. 7353f1c4c4fSKonstantin Belousov * 7363f1c4c4fSKonstantin Belousov * Unlocked read of the p_flag is harmless. At 7373f1c4c4fSKonstantin Belousov * worst, the P_KILLED might be not observed 7383f1c4c4fSKonstantin Belousov * there, and allocation can fail, causing 7393f1c4c4fSKonstantin Belousov * restart and new reading of the p_flag. 740df8bae1dSRodney W. Grimes */ 7413f1c4c4fSKonstantin Belousov if (!vm_page_count_severe() || P_KILLED(curproc)) { 742f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 7433d653db0SAlan Cox vm_object_color(fs.object, atop(vaddr) - 7443d653db0SAlan Cox fs.pindex); 745f8a47341SAlan Cox #endif 7463f1c4c4fSKonstantin Belousov alloc_req = P_KILLED(curproc) ? 7473f1c4c4fSKonstantin Belousov VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL; 7483f1c4c4fSKonstantin Belousov if (fs.object->type != OBJT_VNODE && 7493f1c4c4fSKonstantin Belousov fs.object->backing_object == NULL) 7503f1c4c4fSKonstantin Belousov alloc_req |= VM_ALLOC_ZERO; 7514866e085SJohn Dyson fs.m = vm_page_alloc(fs.object, fs.pindex, 7523f1c4c4fSKonstantin Belousov alloc_req); 75340360b1bSMatthew Dillon } 7544866e085SJohn Dyson if (fs.m == NULL) { 7554866e085SJohn Dyson unlock_and_deallocate(&fs); 756ef6020d1SMike Silbersack VM_WAITPFAULT; 757df8bae1dSRodney W. Grimes goto RetryFault; 7587667839aSAlan Cox } 759df8bae1dSRodney W. Grimes } 76047221757SJohn Dyson 7610d94caffSDavid Greenman readrest: 7621c7c3c6aSMatthew Dillon /* 76385702505SAlan Cox * At this point, we have either allocated a new page or found 76485702505SAlan Cox * an existing page that is only partially valid. 76585702505SAlan Cox * 76685702505SAlan Cox * We hold a reference on the current object and the page is 76785702505SAlan Cox * exclusive busied. 76885702505SAlan Cox */ 76985702505SAlan Cox 77085702505SAlan Cox /* 7710c3a4893SAlan Cox * If the pager for the current object might have the page, 7720c3a4893SAlan Cox * then determine the number of additional pages to read and 7730c3a4893SAlan Cox * potentially reprioritize previously read pages for earlier 7740c3a4893SAlan Cox * reclamation. These operations should only be performed 7750c3a4893SAlan Cox * once per page fault. Even if the current pager doesn't 7760c3a4893SAlan Cox * have the page, the number of additional pages to read will 7770c3a4893SAlan Cox * apply to subsequent objects in the shadow chain. 7781c7c3c6aSMatthew Dillon */ 7790c3a4893SAlan Cox if (fs.object->type != OBJT_DEFAULT && nera == -1 && 7800c3a4893SAlan Cox !P_KILLED(curproc)) { 7810c3a4893SAlan Cox KASSERT(fs.lookup_still_valid, ("map unlocked")); 7825268042bSAlan Cox era = fs.entry->read_ahead; 7830c3a4893SAlan Cox behavior = vm_map_entry_behavior(fs.entry); 7840c3a4893SAlan Cox if (behavior == MAP_ENTRY_BEHAV_RANDOM) { 7855268042bSAlan Cox nera = 0; 78613458803SAlan Cox } else if (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL) { 7875268042bSAlan Cox nera = VM_FAULT_READ_AHEAD_MAX; 788381b7242SAlan Cox if (vaddr == fs.entry->next_read) 7890c3a4893SAlan Cox vm_fault_dontneed(&fs, vaddr, nera); 790381b7242SAlan Cox } else if (vaddr == fs.entry->next_read) { 79113458803SAlan Cox /* 7925268042bSAlan Cox * This is a sequential fault. Arithmetically 7935268042bSAlan Cox * increase the requested number of pages in 7945268042bSAlan Cox * the read-ahead window. The requested 7955268042bSAlan Cox * number of pages is "# of sequential faults 7965268042bSAlan Cox * x (read ahead min + 1) + read ahead min" 79713458803SAlan Cox */ 7985268042bSAlan Cox nera = VM_FAULT_READ_AHEAD_MIN; 7995268042bSAlan Cox if (era > 0) { 8005268042bSAlan Cox nera += era + 1; 80113458803SAlan Cox if (nera > VM_FAULT_READ_AHEAD_MAX) 80213458803SAlan Cox nera = VM_FAULT_READ_AHEAD_MAX; 8035268042bSAlan Cox } 80413458803SAlan Cox if (era == VM_FAULT_READ_AHEAD_MAX) 8050c3a4893SAlan Cox vm_fault_dontneed(&fs, vaddr, nera); 8065268042bSAlan Cox } else { 8075268042bSAlan Cox /* 8080c3a4893SAlan Cox * This is a non-sequential fault. 8095268042bSAlan Cox */ 8105268042bSAlan Cox nera = 0; 811867a482dSJohn Dyson } 8120c3a4893SAlan Cox if (era != nera) { 8130c3a4893SAlan Cox /* 8140c3a4893SAlan Cox * A read lock on the map suffices to update 8150c3a4893SAlan Cox * the read ahead count safely. 8160c3a4893SAlan Cox */ 8175268042bSAlan Cox fs.entry->read_ahead = nera; 8180c3a4893SAlan Cox } 819d2bf64c3SKonstantin Belousov 820d2bf64c3SKonstantin Belousov /* 8210c3a4893SAlan Cox * Prepare for unlocking the map. Save the map 8220c3a4893SAlan Cox * entry's start and end addresses, which are used to 8230c3a4893SAlan Cox * optimize the size of the pager operation below. 8240c3a4893SAlan Cox * Even if the map entry's addresses change after 8250c3a4893SAlan Cox * unlocking the map, using the saved addresses is 8260c3a4893SAlan Cox * safe. 8270c3a4893SAlan Cox */ 8280c3a4893SAlan Cox e_start = fs.entry->start; 8290c3a4893SAlan Cox e_end = fs.entry->end; 8300c3a4893SAlan Cox } 8310c3a4893SAlan Cox 8320c3a4893SAlan Cox /* 8330c3a4893SAlan Cox * Call the pager to retrieve the page if there is a chance 8340c3a4893SAlan Cox * that the pager has it, and potentially retrieve additional 8350c3a4893SAlan Cox * pages at the same time. 8360c3a4893SAlan Cox */ 8370c3a4893SAlan Cox if (fs.object->type != OBJT_DEFAULT) { 8380c3a4893SAlan Cox /* 83985702505SAlan Cox * Release the map lock before locking the vnode or 84085702505SAlan Cox * sleeping in the pager. (If the current object has 84185702505SAlan Cox * a shadow, then an earlier iteration of this loop 84285702505SAlan Cox * may have already unlocked the map.) 843d2bf64c3SKonstantin Belousov */ 844d2bf64c3SKonstantin Belousov unlock_map(&fs); 845d2bf64c3SKonstantin Belousov 846022dfd69SKonstantin Belousov if (fs.object->type == OBJT_VNODE && 847022dfd69SKonstantin Belousov (vp = fs.object->handle) != fs.vp) { 84885702505SAlan Cox /* 84985702505SAlan Cox * Perform an unlock in case the desired vnode 85085702505SAlan Cox * changed while the map was unlocked during a 85185702505SAlan Cox * retry. 85285702505SAlan Cox */ 853cfabea3dSKonstantin Belousov unlock_vp(&fs); 854d2bf64c3SKonstantin Belousov 85585702505SAlan Cox locked = VOP_ISLOCKED(vp); 856d2bf64c3SKonstantin Belousov if (locked != LK_EXCLUSIVE) 857d2bf64c3SKonstantin Belousov locked = LK_SHARED; 858dda4d369SAlan Cox 859dda4d369SAlan Cox /* 860dda4d369SAlan Cox * We must not sleep acquiring the vnode lock 861dda4d369SAlan Cox * while we have the page exclusive busied or 862dda4d369SAlan Cox * the object's paging-in-progress count 863dda4d369SAlan Cox * incremented. Otherwise, we could deadlock. 864dda4d369SAlan Cox */ 865d2bf64c3SKonstantin Belousov error = vget(vp, locked | LK_CANRECURSE | 866d2bf64c3SKonstantin Belousov LK_NOWAIT, curthread); 867d2bf64c3SKonstantin Belousov if (error != 0) { 868d2bf64c3SKonstantin Belousov vhold(vp); 869d2bf64c3SKonstantin Belousov release_page(&fs); 870d2bf64c3SKonstantin Belousov unlock_and_deallocate(&fs); 871d2bf64c3SKonstantin Belousov error = vget(vp, locked | LK_RETRY | 872d2bf64c3SKonstantin Belousov LK_CANRECURSE, curthread); 873d2bf64c3SKonstantin Belousov vdrop(vp); 874d2bf64c3SKonstantin Belousov fs.vp = vp; 875d2bf64c3SKonstantin Belousov KASSERT(error == 0, 876d2bf64c3SKonstantin Belousov ("vm_fault: vget failed")); 877d2bf64c3SKonstantin Belousov goto RetryFault; 878d2bf64c3SKonstantin Belousov } 879d2bf64c3SKonstantin Belousov fs.vp = vp; 880d2bf64c3SKonstantin Belousov } 881d2bf64c3SKonstantin Belousov KASSERT(fs.vp == NULL || !fs.map->system_map, 882d2bf64c3SKonstantin Belousov ("vm_fault: vnode-backed object mapped by system map")); 883d2bf64c3SKonstantin Belousov 884df8bae1dSRodney W. Grimes /* 885b0cd2017SGleb Smirnoff * Page in the requested page and hint the pager, 886b0cd2017SGleb Smirnoff * that it may bring up surrounding pages. 88726f9a767SRodney W. Grimes */ 8880c3a4893SAlan Cox if (nera == -1 || behavior == MAP_ENTRY_BEHAV_RANDOM || 8890c3a4893SAlan Cox P_KILLED(curproc)) { 8900c3a4893SAlan Cox behind = 0; 8910c3a4893SAlan Cox ahead = 0; 8920c3a4893SAlan Cox } else { 8930c3a4893SAlan Cox /* Is this a sequential fault? */ 8940c3a4893SAlan Cox if (nera > 0) { 8950c3a4893SAlan Cox behind = 0; 8960c3a4893SAlan Cox ahead = nera; 8970c3a4893SAlan Cox } else { 8980c3a4893SAlan Cox /* 8990c3a4893SAlan Cox * Request a cluster of pages that is 9000c3a4893SAlan Cox * aligned to a VM_FAULT_READ_DEFAULT 9010c3a4893SAlan Cox * page offset boundary within the 9020c3a4893SAlan Cox * object. Alignment to a page offset 9030c3a4893SAlan Cox * boundary is more likely to coincide 9040c3a4893SAlan Cox * with the underlying file system 9050c3a4893SAlan Cox * block than alignment to a virtual 9060c3a4893SAlan Cox * address boundary. 9070c3a4893SAlan Cox */ 9080c3a4893SAlan Cox cluster_offset = fs.pindex % 9090c3a4893SAlan Cox VM_FAULT_READ_DEFAULT; 9100c3a4893SAlan Cox behind = ulmin(cluster_offset, 9110c3a4893SAlan Cox atop(vaddr - e_start)); 9120c3a4893SAlan Cox ahead = VM_FAULT_READ_DEFAULT - 1 - 9130c3a4893SAlan Cox cluster_offset; 9140c3a4893SAlan Cox } 9150c3a4893SAlan Cox ahead = ulmin(ahead, atop(e_end - vaddr) - 1); 9160c3a4893SAlan Cox } 917b0cd2017SGleb Smirnoff rv = vm_pager_get_pages(fs.object, &fs.m, 1, 918b0cd2017SGleb Smirnoff &behind, &ahead); 91926f9a767SRodney W. Grimes if (rv == VM_PAGER_OK) { 920b0cd2017SGleb Smirnoff faultcount = behind + 1 + ahead; 921320023e2SAlan Cox hardfault = true; 9221c7c3c6aSMatthew Dillon break; /* break to PAGE HAS BEEN FOUND */ 923df8bae1dSRodney W. Grimes } 924a83c285cSDavid Greenman if (rv == VM_PAGER_ERROR) 925f3679e35SDavid Greenman printf("vm_fault: pager read error, pid %d (%s)\n", 926f3679e35SDavid Greenman curproc->p_pid, curproc->p_comm); 927521ddf39SAlan Cox 92826f9a767SRodney W. Grimes /* 929521ddf39SAlan Cox * If an I/O error occurred or the requested page was 930521ddf39SAlan Cox * outside the range of the pager, clean up and return 931521ddf39SAlan Cox * an error. 93226f9a767SRodney W. Grimes */ 933521ddf39SAlan Cox if (rv == VM_PAGER_ERROR || rv == VM_PAGER_BAD) { 9342965a453SKip Macy vm_page_lock(fs.m); 935230afe0bSKonstantin Belousov if (fs.m->wire_count == 0) 9364866e085SJohn Dyson vm_page_free(fs.m); 937230afe0bSKonstantin Belousov else 938230afe0bSKonstantin Belousov vm_page_xunbusy_maybelocked(fs.m); 9392965a453SKip Macy vm_page_unlock(fs.m); 9404866e085SJohn Dyson fs.m = NULL; 9414866e085SJohn Dyson unlock_and_deallocate(&fs); 942521ddf39SAlan Cox return (rv == VM_PAGER_ERROR ? KERN_FAILURE : 943521ddf39SAlan Cox KERN_PROTECTION_FAILURE); 94426f9a767SRodney W. Grimes } 945521ddf39SAlan Cox 946521ddf39SAlan Cox /* 947521ddf39SAlan Cox * The requested page does not exist at this object/ 948521ddf39SAlan Cox * offset. Remove the invalid page from the object, 949521ddf39SAlan Cox * waking up anyone waiting for it, and continue on to 950521ddf39SAlan Cox * the next object. However, if this is the top-level 951521ddf39SAlan Cox * object, we must leave the busy page in place to 952521ddf39SAlan Cox * prevent another process from rushing past us, and 953521ddf39SAlan Cox * inserting the page in that object at the same time 954521ddf39SAlan Cox * that we are. 955521ddf39SAlan Cox */ 9564866e085SJohn Dyson if (fs.object != fs.first_object) { 9572965a453SKip Macy vm_page_lock(fs.m); 958230afe0bSKonstantin Belousov if (fs.m->wire_count == 0) 9594866e085SJohn Dyson vm_page_free(fs.m); 960230afe0bSKonstantin Belousov else 961230afe0bSKonstantin Belousov vm_page_xunbusy_maybelocked(fs.m); 9622965a453SKip Macy vm_page_unlock(fs.m); 9634866e085SJohn Dyson fs.m = NULL; 964df8bae1dSRodney W. Grimes } 965df8bae1dSRodney W. Grimes } 96640360b1bSMatthew Dillon 967df8bae1dSRodney W. Grimes /* 9681c7c3c6aSMatthew Dillon * We get here if the object has default pager (or unwiring) 9691c7c3c6aSMatthew Dillon * or the pager doesn't have the page. 970df8bae1dSRodney W. Grimes */ 9714866e085SJohn Dyson if (fs.object == fs.first_object) 9724866e085SJohn Dyson fs.first_m = fs.m; 973df8bae1dSRodney W. Grimes 974df8bae1dSRodney W. Grimes /* 9750d94caffSDavid Greenman * Move on to the next object. Lock the next object before 9760d94caffSDavid Greenman * unlocking the current one. 977df8bae1dSRodney W. Grimes */ 9784866e085SJohn Dyson next_object = fs.object->backing_object; 979df8bae1dSRodney W. Grimes if (next_object == NULL) { 980df8bae1dSRodney W. Grimes /* 9810d94caffSDavid Greenman * If there's no object left, fill the page in the top 9820d94caffSDavid Greenman * object with zeros. 983df8bae1dSRodney W. Grimes */ 9844866e085SJohn Dyson if (fs.object != fs.first_object) { 9854866e085SJohn Dyson vm_object_pip_wakeup(fs.object); 98689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(fs.object); 987df8bae1dSRodney W. Grimes 9884866e085SJohn Dyson fs.object = fs.first_object; 9894866e085SJohn Dyson fs.pindex = fs.first_pindex; 9904866e085SJohn Dyson fs.m = fs.first_m; 99189f6b863SAttilio Rao VM_OBJECT_WLOCK(fs.object); 992df8bae1dSRodney W. Grimes } 9934866e085SJohn Dyson fs.first_m = NULL; 994df8bae1dSRodney W. Grimes 9954221e284SAlan Cox /* 9964221e284SAlan Cox * Zero the page if necessary and mark it valid. 9974221e284SAlan Cox */ 9984866e085SJohn Dyson if ((fs.m->flags & PG_ZERO) == 0) { 999fff6062aSAlan Cox pmap_zero_page(fs.m); 10004221e284SAlan Cox } else { 100167596082SAttilio Rao PCPU_INC(cnt.v_ozfod); 10024221e284SAlan Cox } 100367596082SAttilio Rao PCPU_INC(cnt.v_zfod); 10044221e284SAlan Cox fs.m->valid = VM_PAGE_BITS_ALL; 10057b9b301cSAlan Cox /* Don't try to prefault neighboring pages. */ 10067b9b301cSAlan Cox faultcount = 1; 10071c7c3c6aSMatthew Dillon break; /* break to PAGE HAS BEEN FOUND */ 10080d94caffSDavid Greenman } else { 1009c8567c3aSAlan Cox KASSERT(fs.object != next_object, 1010c8567c3aSAlan Cox ("object loop %p", next_object)); 101189f6b863SAttilio Rao VM_OBJECT_WLOCK(next_object); 1012c8567c3aSAlan Cox vm_object_pip_add(next_object, 1); 1013c8567c3aSAlan Cox if (fs.object != fs.first_object) 10144866e085SJohn Dyson vm_object_pip_wakeup(fs.object); 10156753423cSAlan Cox fs.pindex += 10166753423cSAlan Cox OFF_TO_IDX(fs.object->backing_object_offset); 101789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(fs.object); 10184866e085SJohn Dyson fs.object = next_object; 1019df8bae1dSRodney W. Grimes } 1020df8bae1dSRodney W. Grimes } 10211c7c3c6aSMatthew Dillon 1022c7aebda8SAttilio Rao vm_page_assert_xbusied(fs.m); 1023df8bae1dSRodney W. Grimes 1024df8bae1dSRodney W. Grimes /* 10250d94caffSDavid Greenman * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock 1026df8bae1dSRodney W. Grimes * is held.] 1027df8bae1dSRodney W. Grimes */ 1028df8bae1dSRodney W. Grimes 1029df8bae1dSRodney W. Grimes /* 10300d94caffSDavid Greenman * If the page is being written, but isn't already owned by the 10310d94caffSDavid Greenman * top-level object, we have to copy it into a new page owned by the 10320d94caffSDavid Greenman * top-level object. 1033df8bae1dSRodney W. Grimes */ 10344866e085SJohn Dyson if (fs.object != fs.first_object) { 1035df8bae1dSRodney W. Grimes /* 10360d94caffSDavid Greenman * We only really need to copy if we want to write it. 1037df8bae1dSRodney W. Grimes */ 1038a6d42a0dSAlan Cox if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) { 1039df8bae1dSRodney W. Grimes /* 10401c7c3c6aSMatthew Dillon * This allows pages to be virtually copied from a 10411c7c3c6aSMatthew Dillon * backing_object into the first_object, where the 10421c7c3c6aSMatthew Dillon * backing object has no other refs to it, and cannot 10431c7c3c6aSMatthew Dillon * gain any more refs. Instead of a bcopy, we just 10441c7c3c6aSMatthew Dillon * move the page from the backing object to the 10451c7c3c6aSMatthew Dillon * first object. Note that we must mark the page 10461c7c3c6aSMatthew Dillon * dirty in the first object so that it will go out 10471c7c3c6aSMatthew Dillon * to swap when needed. 1048df8bae1dSRodney W. Grimes */ 1049cd8a6fe8SAlan Cox is_first_object_locked = false; 1050e50346b5SAlan Cox if ( 1051de5f6a77SJohn Dyson /* 1052de5f6a77SJohn Dyson * Only one shadow object 1053de5f6a77SJohn Dyson */ 10544866e085SJohn Dyson (fs.object->shadow_count == 1) && 1055de5f6a77SJohn Dyson /* 1056de5f6a77SJohn Dyson * No COW refs, except us 1057de5f6a77SJohn Dyson */ 10584866e085SJohn Dyson (fs.object->ref_count == 1) && 1059de5f6a77SJohn Dyson /* 10605929bcfaSPhilippe Charnier * No one else can look this object up 1061de5f6a77SJohn Dyson */ 10624866e085SJohn Dyson (fs.object->handle == NULL) && 1063de5f6a77SJohn Dyson /* 1064de5f6a77SJohn Dyson * No other ways to look the object up 1065de5f6a77SJohn Dyson */ 10664866e085SJohn Dyson ((fs.object->type == OBJT_DEFAULT) || 10674866e085SJohn Dyson (fs.object->type == OBJT_SWAP)) && 106889f6b863SAttilio Rao (is_first_object_locked = VM_OBJECT_TRYWLOCK(fs.first_object)) && 1069de5f6a77SJohn Dyson /* 1070de5f6a77SJohn Dyson * We don't chase down the shadow chain 1071de5f6a77SJohn Dyson */ 1072e50346b5SAlan Cox fs.object == fs.first_object->backing_object) { 1073bccdea45SAlan Cox vm_page_lock(fs.m); 1074bccdea45SAlan Cox vm_page_remove(fs.m); 1075bccdea45SAlan Cox vm_page_unlock(fs.m); 1076eb00b276SAlan Cox vm_page_lock(fs.first_m); 1077bccdea45SAlan Cox vm_page_replace_checked(fs.m, fs.first_object, 1078bccdea45SAlan Cox fs.first_pindex, fs.first_m); 10796fee422eSConrad Meyer vm_page_free(fs.first_m); 10806fee422eSConrad Meyer vm_page_unlock(fs.first_m); 1081bccdea45SAlan Cox vm_page_dirty(fs.m); 1082dfdf9abdSAlan Cox #if VM_NRESERVLEVEL > 0 1083dfdf9abdSAlan Cox /* 1084dfdf9abdSAlan Cox * Rename the reservation. 1085dfdf9abdSAlan Cox */ 1086dfdf9abdSAlan Cox vm_reserv_rename(fs.m, fs.first_object, 1087dfdf9abdSAlan Cox fs.object, OFF_TO_IDX( 1088dfdf9abdSAlan Cox fs.first_object->backing_object_offset)); 1089dfdf9abdSAlan Cox #endif 1090bccdea45SAlan Cox /* 1091bccdea45SAlan Cox * Removing the page from the backing object 1092bccdea45SAlan Cox * unbusied it. 1093bccdea45SAlan Cox */ 1094c7aebda8SAttilio Rao vm_page_xbusy(fs.m); 1095d98ddc46SAlan Cox fs.first_m = fs.m; 10964866e085SJohn Dyson fs.m = NULL; 109767596082SAttilio Rao PCPU_INC(cnt.v_cow_optim); 1098de5f6a77SJohn Dyson } else { 1099de5f6a77SJohn Dyson /* 1100de5f6a77SJohn Dyson * Oh, well, lets copy it. 1101de5f6a77SJohn Dyson */ 1102669890eaSAlan Cox pmap_copy_page(fs.m, fs.first_m); 1103669890eaSAlan Cox fs.first_m->valid = VM_PAGE_BITS_ALL; 1104d8778512SAlan Cox if (wired && (fault_flags & 11056a875bf9SKonstantin Belousov VM_FAULT_WIRE) == 0) { 11062965a453SKip Macy vm_page_lock(fs.first_m); 1107d8778512SAlan Cox vm_page_wire(fs.first_m); 11082965a453SKip Macy vm_page_unlock(fs.first_m); 11092965a453SKip Macy 11102965a453SKip Macy vm_page_lock(fs.m); 11113ae10f74SAttilio Rao vm_page_unwire(fs.m, PQ_INACTIVE); 11122965a453SKip Macy vm_page_unlock(fs.m); 1113de5f6a77SJohn Dyson } 1114df8bae1dSRodney W. Grimes /* 1115df8bae1dSRodney W. Grimes * We no longer need the old page or object. 1116df8bae1dSRodney W. Grimes */ 11174866e085SJohn Dyson release_page(&fs); 1118de5f6a77SJohn Dyson } 11191c7c3c6aSMatthew Dillon /* 11201c7c3c6aSMatthew Dillon * fs.object != fs.first_object due to above 11211c7c3c6aSMatthew Dillon * conditional 11221c7c3c6aSMatthew Dillon */ 11234866e085SJohn Dyson vm_object_pip_wakeup(fs.object); 112489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(fs.object); 1125df8bae1dSRodney W. Grimes /* 1126df8bae1dSRodney W. Grimes * Only use the new page below... 1127df8bae1dSRodney W. Grimes */ 11284866e085SJohn Dyson fs.object = fs.first_object; 11294866e085SJohn Dyson fs.pindex = fs.first_pindex; 1130d98ddc46SAlan Cox fs.m = fs.first_m; 1131f29ba63eSAlan Cox if (!is_first_object_locked) 113289f6b863SAttilio Rao VM_OBJECT_WLOCK(fs.object); 113367596082SAttilio Rao PCPU_INC(cnt.v_cow_faults); 11344d34e019SKonstantin Belousov curthread->td_cow++; 11350d94caffSDavid Greenman } else { 1136df8bae1dSRodney W. Grimes prot &= ~VM_PROT_WRITE; 1137df8bae1dSRodney W. Grimes } 1138df8bae1dSRodney W. Grimes } 1139df8bae1dSRodney W. Grimes 1140df8bae1dSRodney W. Grimes /* 11410d94caffSDavid Greenman * We must verify that the maps have not changed since our last 11420d94caffSDavid Greenman * lookup. 1143df8bae1dSRodney W. Grimes */ 114419dc5607STor Egge if (!fs.lookup_still_valid) { 114519dc5607STor Egge if (!vm_map_trylock_read(fs.map)) { 1146b823bbd6SMatthew Dillon release_page(&fs); 1147b823bbd6SMatthew Dillon unlock_and_deallocate(&fs); 1148b823bbd6SMatthew Dillon goto RetryFault; 1149b823bbd6SMatthew Dillon } 1150cd8a6fe8SAlan Cox fs.lookup_still_valid = true; 1151dc5401d2SKonstantin Belousov if (fs.map->timestamp != fs.map_generation) { 115219dc5607STor Egge result = vm_map_lookup_locked(&fs.map, vaddr, fault_type, 11534866e085SJohn Dyson &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired); 1154df8bae1dSRodney W. Grimes 1155df8bae1dSRodney W. Grimes /* 115644ed3417STor Egge * If we don't need the page any longer, put it on the inactive 11570d94caffSDavid Greenman * list (the easiest thing to do here). If no one needs it, 11580d94caffSDavid Greenman * pageout will grab it eventually. 1159df8bae1dSRodney W. Grimes */ 1160df8bae1dSRodney W. Grimes if (result != KERN_SUCCESS) { 11614866e085SJohn Dyson release_page(&fs); 11624866e085SJohn Dyson unlock_and_deallocate(&fs); 116319dc5607STor Egge 116419dc5607STor Egge /* 116519dc5607STor Egge * If retry of map lookup would have blocked then 116619dc5607STor Egge * retry fault from start. 116719dc5607STor Egge */ 116819dc5607STor Egge if (result == KERN_FAILURE) 116919dc5607STor Egge goto RetryFault; 1170df8bae1dSRodney W. Grimes return (result); 1171df8bae1dSRodney W. Grimes } 11724866e085SJohn Dyson if ((retry_object != fs.first_object) || 11734866e085SJohn Dyson (retry_pindex != fs.first_pindex)) { 11744866e085SJohn Dyson release_page(&fs); 11754866e085SJohn Dyson unlock_and_deallocate(&fs); 1176df8bae1dSRodney W. Grimes goto RetryFault; 1177df8bae1dSRodney W. Grimes } 117819dc5607STor Egge 1179df8bae1dSRodney W. Grimes /* 11800d94caffSDavid Greenman * Check whether the protection has changed or the object has 11810d94caffSDavid Greenman * been copied while we left the map unlocked. Changing from 11820d94caffSDavid Greenman * read to write permission is OK - we leave the page 11830d94caffSDavid Greenman * write-protected, and catch the write fault. Changing from 11840d94caffSDavid Greenman * write to read permission means that we can't mark the page 11850d94caffSDavid Greenman * write-enabled after all. 1186df8bae1dSRodney W. Grimes */ 1187df8bae1dSRodney W. Grimes prot &= retry_prot; 1188df8bae1dSRodney W. Grimes } 118919dc5607STor Egge } 1190381b7242SAlan Cox 1191d2bf64c3SKonstantin Belousov /* 1192381b7242SAlan Cox * If the page was filled by a pager, save the virtual address that 1193381b7242SAlan Cox * should be faulted on next under a sequential access pattern to the 1194381b7242SAlan Cox * map entry. A read lock on the map suffices to update this address 1195381b7242SAlan Cox * safely. 1196d2bf64c3SKonstantin Belousov */ 11975758fe71SAlan Cox if (hardfault) 1198381b7242SAlan Cox fs.entry->next_read = vaddr + ptoa(ahead) + PAGE_SIZE; 1199d2bf64c3SKonstantin Belousov 1200e26236e9SKonstantin Belousov vm_fault_dirty(fs.entry, fs.m, prot, fault_type, fault_flags, true); 1201c7aebda8SAttilio Rao vm_page_assert_xbusied(fs.m); 1202c7aebda8SAttilio Rao 12034221e284SAlan Cox /* 120478cfe1f7SAlan Cox * Page must be completely valid or it is not fit to 12054221e284SAlan Cox * map into user space. vm_pager_get_pages() ensures this. 12064221e284SAlan Cox */ 120778cfe1f7SAlan Cox KASSERT(fs.m->valid == VM_PAGE_BITS_ALL, 120878cfe1f7SAlan Cox ("vm_fault: page %p partially invalid", fs.m)); 120989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(fs.object); 1210cbfbaad8SAlan Cox 121186735996SAlan Cox /* 121286735996SAlan Cox * Put this page into the physical map. We had to do the unlock above 121386735996SAlan Cox * because pmap_enter() may sleep. We don't put the page 121486735996SAlan Cox * back on the active queue until later so that the pageout daemon 121586735996SAlan Cox * won't find it (yet). 121686735996SAlan Cox */ 121739ffa8c1SKonstantin Belousov pmap_enter(fs.map->pmap, vaddr, fs.m, prot, 121839ffa8c1SKonstantin Belousov fault_type | (wired ? PMAP_ENTER_WIRED : 0), 0); 12196a875bf9SKonstantin Belousov if (faultcount != 1 && (fault_flags & VM_FAULT_WIRE) == 0 && 12207b9b301cSAlan Cox wired == 0) 1221b0cd2017SGleb Smirnoff vm_fault_prefault(&fs, vaddr, 1222b0cd2017SGleb Smirnoff faultcount > 0 ? behind : PFBAK, 1223b0cd2017SGleb Smirnoff faultcount > 0 ? ahead : PFFOR); 122489f6b863SAttilio Rao VM_OBJECT_WLOCK(fs.object); 12252965a453SKip Macy vm_page_lock(fs.m); 1226ff97964aSJohn Dyson 1227df8bae1dSRodney W. Grimes /* 12280d94caffSDavid Greenman * If the page is not wired down, then put it where the pageout daemon 12290d94caffSDavid Greenman * can find it. 1230df8bae1dSRodney W. Grimes */ 12316a875bf9SKonstantin Belousov if ((fault_flags & VM_FAULT_WIRE) != 0) { 12326a875bf9SKonstantin Belousov KASSERT(wired, ("VM_FAULT_WIRE && !wired")); 12334866e085SJohn Dyson vm_page_wire(fs.m); 123403679e23SAlan Cox } else 12354866e085SJohn Dyson vm_page_activate(fs.m); 1236acd11c74SAlan Cox if (m_hold != NULL) { 1237acd11c74SAlan Cox *m_hold = fs.m; 1238acd11c74SAlan Cox vm_page_hold(fs.m); 1239acd11c74SAlan Cox } 12402965a453SKip Macy vm_page_unlock(fs.m); 1241c7aebda8SAttilio Rao vm_page_xunbusy(fs.m); 1242eeec6babSJohn Baldwin 1243eebf3286SAlan Cox /* 1244eebf3286SAlan Cox * Unlock everything, and return 1245eebf3286SAlan Cox */ 1246eebf3286SAlan Cox unlock_and_deallocate(&fs); 1247b3a01bdfSAndrey Zonov if (hardfault) { 1248b3a01bdfSAndrey Zonov PCPU_INC(cnt.v_io_faults); 12491c4bcd05SJeff Roberson curthread->td_ru.ru_majflt++; 1250ae34b6ffSEdward Tomasz Napierala #ifdef RACCT 1251ae34b6ffSEdward Tomasz Napierala if (racct_enable && fs.object->type == OBJT_VNODE) { 1252ae34b6ffSEdward Tomasz Napierala PROC_LOCK(curproc); 1253ae34b6ffSEdward Tomasz Napierala if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) { 1254ae34b6ffSEdward Tomasz Napierala racct_add_force(curproc, RACCT_WRITEBPS, 1255ae34b6ffSEdward Tomasz Napierala PAGE_SIZE + behind * PAGE_SIZE); 1256ae34b6ffSEdward Tomasz Napierala racct_add_force(curproc, RACCT_WRITEIOPS, 1); 1257ae34b6ffSEdward Tomasz Napierala } else { 1258ae34b6ffSEdward Tomasz Napierala racct_add_force(curproc, RACCT_READBPS, 1259ae34b6ffSEdward Tomasz Napierala PAGE_SIZE + ahead * PAGE_SIZE); 1260ae34b6ffSEdward Tomasz Napierala racct_add_force(curproc, RACCT_READIOPS, 1); 1261ae34b6ffSEdward Tomasz Napierala } 1262ae34b6ffSEdward Tomasz Napierala PROC_UNLOCK(curproc); 1263ae34b6ffSEdward Tomasz Napierala } 1264ae34b6ffSEdward Tomasz Napierala #endif 1265b3a01bdfSAndrey Zonov } else 12661c4bcd05SJeff Roberson curthread->td_ru.ru_minflt++; 1267df8bae1dSRodney W. Grimes 1268df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 1269df8bae1dSRodney W. Grimes } 1270df8bae1dSRodney W. Grimes 1271df8bae1dSRodney W. Grimes /* 1272a8b0f100SAlan Cox * Speed up the reclamation of pages that precede the faulting pindex within 1273a8b0f100SAlan Cox * the first object of the shadow chain. Essentially, perform the equivalent 1274a8b0f100SAlan Cox * to madvise(..., MADV_DONTNEED) on a large cluster of pages that precedes 1275a8b0f100SAlan Cox * the faulting pindex by the cluster size when the pages read by vm_fault() 1276a8b0f100SAlan Cox * cross a cluster-size boundary. The cluster size is the greater of the 1277a8b0f100SAlan Cox * smallest superpage size and VM_FAULT_DONTNEED_MIN. 1278a8b0f100SAlan Cox * 1279a8b0f100SAlan Cox * When "fs->first_object" is a shadow object, the pages in the backing object 1280a8b0f100SAlan Cox * that precede the faulting pindex are deactivated by vm_fault(). So, this 1281a8b0f100SAlan Cox * function must only be concerned with pages in the first object. 128213458803SAlan Cox */ 128313458803SAlan Cox static void 1284a8b0f100SAlan Cox vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr, int ahead) 128513458803SAlan Cox { 1286a8b0f100SAlan Cox vm_map_entry_t entry; 128713458803SAlan Cox vm_object_t first_object, object; 1288a8b0f100SAlan Cox vm_offset_t end, start; 1289a8b0f100SAlan Cox vm_page_t m, m_next; 1290a8b0f100SAlan Cox vm_pindex_t pend, pstart; 1291a8b0f100SAlan Cox vm_size_t size; 129213458803SAlan Cox 129313458803SAlan Cox object = fs->object; 129489f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 129513458803SAlan Cox first_object = fs->first_object; 129613458803SAlan Cox if (first_object != object) { 1297b5ab20c0SAlan Cox if (!VM_OBJECT_TRYWLOCK(first_object)) { 129889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1299b5ab20c0SAlan Cox VM_OBJECT_WLOCK(first_object); 130089f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 130113458803SAlan Cox } 130213458803SAlan Cox } 1303a8b0f100SAlan Cox /* Neither fictitious nor unmanaged pages can be reclaimed. */ 130428634820SAlan Cox if ((first_object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0) { 1305a8b0f100SAlan Cox size = VM_FAULT_DONTNEED_MIN; 1306a8b0f100SAlan Cox if (MAXPAGESIZES > 1 && size < pagesizes[1]) 1307a8b0f100SAlan Cox size = pagesizes[1]; 1308a8b0f100SAlan Cox end = rounddown2(vaddr, size); 1309a8b0f100SAlan Cox if (vaddr - end >= size - PAGE_SIZE - ptoa(ahead) && 1310a8b0f100SAlan Cox (entry = fs->entry)->start < end) { 1311a8b0f100SAlan Cox if (end - entry->start < size) 1312a8b0f100SAlan Cox start = entry->start; 131313458803SAlan Cox else 1314a8b0f100SAlan Cox start = end - size; 1315a8b0f100SAlan Cox pmap_advise(fs->map->pmap, start, end, MADV_DONTNEED); 1316a8b0f100SAlan Cox pstart = OFF_TO_IDX(entry->offset) + atop(start - 1317a8b0f100SAlan Cox entry->start); 1318a8b0f100SAlan Cox m_next = vm_page_find_least(first_object, pstart); 1319a8b0f100SAlan Cox pend = OFF_TO_IDX(entry->offset) + atop(end - 1320a8b0f100SAlan Cox entry->start); 1321a8b0f100SAlan Cox while ((m = m_next) != NULL && m->pindex < pend) { 1322a8b0f100SAlan Cox m_next = TAILQ_NEXT(m, listq); 1323a8b0f100SAlan Cox if (m->valid != VM_PAGE_BITS_ALL || 1324a8b0f100SAlan Cox vm_page_busied(m)) 132513458803SAlan Cox continue; 1326d8015db3SAlan Cox 1327d8015db3SAlan Cox /* 1328d8015db3SAlan Cox * Don't clear PGA_REFERENCED, since it would 1329d8015db3SAlan Cox * likely represent a reference by a different 1330d8015db3SAlan Cox * process. 1331d8015db3SAlan Cox * 1332d8015db3SAlan Cox * Typically, at this point, prefetched pages 1333d8015db3SAlan Cox * are still in the inactive queue. Only 1334d8015db3SAlan Cox * pages that triggered page faults are in the 1335d8015db3SAlan Cox * active queue. 1336d8015db3SAlan Cox */ 133713458803SAlan Cox vm_page_lock(m); 1338d8015db3SAlan Cox vm_page_deactivate(m); 133913458803SAlan Cox vm_page_unlock(m); 134013458803SAlan Cox } 134113458803SAlan Cox } 1342a8b0f100SAlan Cox } 134313458803SAlan Cox if (first_object != object) 1344b5ab20c0SAlan Cox VM_OBJECT_WUNLOCK(first_object); 134513458803SAlan Cox } 134613458803SAlan Cox 134713458803SAlan Cox /* 1348566526a9SAlan Cox * vm_fault_prefault provides a quick way of clustering 1349566526a9SAlan Cox * pagefaults into a processes address space. It is a "cousin" 1350566526a9SAlan Cox * of vm_map_pmap_enter, except it runs at page fault time instead 1351566526a9SAlan Cox * of mmap time. 1352566526a9SAlan Cox */ 1353566526a9SAlan Cox static void 135463281952SAlan Cox vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra, 1355b0cd2017SGleb Smirnoff int backward, int forward) 1356566526a9SAlan Cox { 135763281952SAlan Cox pmap_t pmap; 135863281952SAlan Cox vm_map_entry_t entry; 135963281952SAlan Cox vm_object_t backing_object, lobject; 1360566526a9SAlan Cox vm_offset_t addr, starta; 1361566526a9SAlan Cox vm_pindex_t pindex; 13622053c127SStephan Uphoff vm_page_t m; 1363b0cd2017SGleb Smirnoff int i; 1364566526a9SAlan Cox 136563281952SAlan Cox pmap = fs->map->pmap; 1366950d5f7aSAlan Cox if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace)) 1367566526a9SAlan Cox return; 1368566526a9SAlan Cox 136963281952SAlan Cox entry = fs->entry; 1370566526a9SAlan Cox 1371*63cdcaaeSKonstantin Belousov if (addra < backward * PAGE_SIZE) { 1372566526a9SAlan Cox starta = entry->start; 1373*63cdcaaeSKonstantin Belousov } else { 1374*63cdcaaeSKonstantin Belousov starta = addra - backward * PAGE_SIZE; 1375*63cdcaaeSKonstantin Belousov if (starta < entry->start) 1376*63cdcaaeSKonstantin Belousov starta = entry->start; 1377566526a9SAlan Cox } 1378566526a9SAlan Cox 137963281952SAlan Cox /* 138063281952SAlan Cox * Generate the sequence of virtual addresses that are candidates for 138163281952SAlan Cox * prefaulting in an outward spiral from the faulting virtual address, 138263281952SAlan Cox * "addra". Specifically, the sequence is "addra - PAGE_SIZE", "addra 138363281952SAlan Cox * + PAGE_SIZE", "addra - 2 * PAGE_SIZE", "addra + 2 * PAGE_SIZE", ... 138463281952SAlan Cox * If the candidate address doesn't have a backing physical page, then 138563281952SAlan Cox * the loop immediately terminates. 138663281952SAlan Cox */ 138763281952SAlan Cox for (i = 0; i < 2 * imax(backward, forward); i++) { 138863281952SAlan Cox addr = addra + ((i >> 1) + 1) * ((i & 1) == 0 ? -PAGE_SIZE : 138963281952SAlan Cox PAGE_SIZE); 139063281952SAlan Cox if (addr > addra + forward * PAGE_SIZE) 1391566526a9SAlan Cox addr = 0; 1392566526a9SAlan Cox 1393566526a9SAlan Cox if (addr < starta || addr >= entry->end) 1394566526a9SAlan Cox continue; 1395566526a9SAlan Cox 1396566526a9SAlan Cox if (!pmap_is_prefaultable(pmap, addr)) 1397566526a9SAlan Cox continue; 1398566526a9SAlan Cox 1399566526a9SAlan Cox pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT; 140063281952SAlan Cox lobject = entry->object.vm_object; 1401c141ae7fSAlan Cox VM_OBJECT_RLOCK(lobject); 1402566526a9SAlan Cox while ((m = vm_page_lookup(lobject, pindex)) == NULL && 1403566526a9SAlan Cox lobject->type == OBJT_DEFAULT && 1404566526a9SAlan Cox (backing_object = lobject->backing_object) != NULL) { 140536930fc9SAlan Cox KASSERT((lobject->backing_object_offset & PAGE_MASK) == 140636930fc9SAlan Cox 0, ("vm_fault_prefault: unaligned object offset")); 1407566526a9SAlan Cox pindex += lobject->backing_object_offset >> PAGE_SHIFT; 1408c141ae7fSAlan Cox VM_OBJECT_RLOCK(backing_object); 1409c141ae7fSAlan Cox VM_OBJECT_RUNLOCK(lobject); 1410566526a9SAlan Cox lobject = backing_object; 1411566526a9SAlan Cox } 1412cbfbaad8SAlan Cox if (m == NULL) { 1413c141ae7fSAlan Cox VM_OBJECT_RUNLOCK(lobject); 1414566526a9SAlan Cox break; 1415cbfbaad8SAlan Cox } 14160a2e596aSAlan Cox if (m->valid == VM_PAGE_BITS_ALL && 14173c4a2440SAlan Cox (m->flags & PG_FICTITIOUS) == 0) 14187bfda801SAlan Cox pmap_enter_quick(pmap, addr, m, entry->protection); 1419c141ae7fSAlan Cox VM_OBJECT_RUNLOCK(lobject); 1420566526a9SAlan Cox } 1421566526a9SAlan Cox } 1422566526a9SAlan Cox 1423566526a9SAlan Cox /* 142482de724fSAlan Cox * Hold each of the physical pages that are mapped by the specified range of 142582de724fSAlan Cox * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid 142682de724fSAlan Cox * and allow the specified types of access, "prot". If all of the implied 142782de724fSAlan Cox * pages are successfully held, then the number of held pages is returned 142882de724fSAlan Cox * together with pointers to those pages in the array "ma". However, if any 142982de724fSAlan Cox * of the pages cannot be held, -1 is returned. 143082de724fSAlan Cox */ 143182de724fSAlan Cox int 143282de724fSAlan Cox vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len, 143382de724fSAlan Cox vm_prot_t prot, vm_page_t *ma, int max_count) 143482de724fSAlan Cox { 143582de724fSAlan Cox vm_offset_t end, va; 143682de724fSAlan Cox vm_page_t *mp; 14377e14088dSKonstantin Belousov int count; 143882de724fSAlan Cox boolean_t pmap_failed; 143982de724fSAlan Cox 1440af32c419SKonstantin Belousov if (len == 0) 1441af32c419SKonstantin Belousov return (0); 144282de724fSAlan Cox end = round_page(addr + len); 144382de724fSAlan Cox addr = trunc_page(addr); 144482de724fSAlan Cox 144582de724fSAlan Cox /* 144682de724fSAlan Cox * Check for illegal addresses. 144782de724fSAlan Cox */ 144882de724fSAlan Cox if (addr < vm_map_min(map) || addr > end || end > vm_map_max(map)) 144982de724fSAlan Cox return (-1); 145082de724fSAlan Cox 14517e14088dSKonstantin Belousov if (atop(end - addr) > max_count) 145282de724fSAlan Cox panic("vm_fault_quick_hold_pages: count > max_count"); 14537e14088dSKonstantin Belousov count = atop(end - addr); 145482de724fSAlan Cox 145582de724fSAlan Cox /* 145682de724fSAlan Cox * Most likely, the physical pages are resident in the pmap, so it is 145782de724fSAlan Cox * faster to try pmap_extract_and_hold() first. 145882de724fSAlan Cox */ 145982de724fSAlan Cox pmap_failed = FALSE; 146082de724fSAlan Cox for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) { 146182de724fSAlan Cox *mp = pmap_extract_and_hold(map->pmap, va, prot); 146282de724fSAlan Cox if (*mp == NULL) 146382de724fSAlan Cox pmap_failed = TRUE; 146482de724fSAlan Cox else if ((prot & VM_PROT_WRITE) != 0 && 1465a5dbab54SAlan Cox (*mp)->dirty != VM_PAGE_BITS_ALL) { 146682de724fSAlan Cox /* 146782de724fSAlan Cox * Explicitly dirty the physical page. Otherwise, the 146882de724fSAlan Cox * caller's changes may go unnoticed because they are 146982de724fSAlan Cox * performed through an unmanaged mapping or by a DMA 147082de724fSAlan Cox * operation. 14713c76db4cSAlan Cox * 1472abb9b935SKonstantin Belousov * The object lock is not held here. 1473abb9b935SKonstantin Belousov * See vm_page_clear_dirty_mask(). 147482de724fSAlan Cox */ 14753c76db4cSAlan Cox vm_page_dirty(*mp); 147682de724fSAlan Cox } 147782de724fSAlan Cox } 147882de724fSAlan Cox if (pmap_failed) { 147982de724fSAlan Cox /* 148082de724fSAlan Cox * One or more pages could not be held by the pmap. Either no 148182de724fSAlan Cox * page was mapped at the specified virtual address or that 148282de724fSAlan Cox * mapping had insufficient permissions. Attempt to fault in 148382de724fSAlan Cox * and hold these pages. 148482de724fSAlan Cox */ 148582de724fSAlan Cox for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) 1486be996836SAttilio Rao if (*mp == NULL && vm_fault_hold(map, va, prot, 148782de724fSAlan Cox VM_FAULT_NORMAL, mp) != KERN_SUCCESS) 148882de724fSAlan Cox goto error; 148982de724fSAlan Cox } 149082de724fSAlan Cox return (count); 149182de724fSAlan Cox error: 149282de724fSAlan Cox for (mp = ma; mp < ma + count; mp++) 149382de724fSAlan Cox if (*mp != NULL) { 149482de724fSAlan Cox vm_page_lock(*mp); 149582de724fSAlan Cox vm_page_unhold(*mp); 149682de724fSAlan Cox vm_page_unlock(*mp); 149782de724fSAlan Cox } 149882de724fSAlan Cox return (-1); 149982de724fSAlan Cox } 150082de724fSAlan Cox 150182de724fSAlan Cox /* 1502df8bae1dSRodney W. Grimes * Routine: 1503df8bae1dSRodney W. Grimes * vm_fault_copy_entry 1504df8bae1dSRodney W. Grimes * Function: 1505210a6886SKonstantin Belousov * Create new shadow object backing dst_entry with private copy of 1506210a6886SKonstantin Belousov * all underlying pages. When src_entry is equal to dst_entry, 1507210a6886SKonstantin Belousov * function implements COW for wired-down map entry. Otherwise, 1508210a6886SKonstantin Belousov * it forks wired entry into dst_map. 1509df8bae1dSRodney W. Grimes * 1510df8bae1dSRodney W. Grimes * In/out conditions: 1511df8bae1dSRodney W. Grimes * The source and destination maps must be locked for write. 1512df8bae1dSRodney W. Grimes * The source map entry must be wired down (or be a sharing map 1513df8bae1dSRodney W. Grimes * entry corresponding to a main map entry that is wired down). 1514df8bae1dSRodney W. Grimes */ 151526f9a767SRodney W. Grimes void 1516121fd461SKonstantin Belousov vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map, 1517121fd461SKonstantin Belousov vm_map_entry_t dst_entry, vm_map_entry_t src_entry, 1518121fd461SKonstantin Belousov vm_ooffset_t *fork_charge) 1519df8bae1dSRodney W. Grimes { 1520210a6886SKonstantin Belousov vm_object_t backing_object, dst_object, object, src_object; 15217afab86cSAlan Cox vm_pindex_t dst_pindex, pindex, src_pindex; 1522210a6886SKonstantin Belousov vm_prot_t access, prot; 1523df8bae1dSRodney W. Grimes vm_offset_t vaddr; 1524df8bae1dSRodney W. Grimes vm_page_t dst_m; 1525df8bae1dSRodney W. Grimes vm_page_t src_m; 15264c74acf7SKonstantin Belousov boolean_t upgrade; 1527df8bae1dSRodney W. Grimes 1528df8bae1dSRodney W. Grimes #ifdef lint 1529df8bae1dSRodney W. Grimes src_map++; 15300d94caffSDavid Greenman #endif /* lint */ 1531df8bae1dSRodney W. Grimes 1532210a6886SKonstantin Belousov upgrade = src_entry == dst_entry; 15330973283dSKonstantin Belousov access = prot = dst_entry->protection; 1534210a6886SKonstantin Belousov 1535df8bae1dSRodney W. Grimes src_object = src_entry->object.vm_object; 15367afab86cSAlan Cox src_pindex = OFF_TO_IDX(src_entry->offset); 1537df8bae1dSRodney W. Grimes 15380973283dSKonstantin Belousov if (upgrade && (dst_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) { 15390973283dSKonstantin Belousov dst_object = src_object; 15400973283dSKonstantin Belousov vm_object_reference(dst_object); 15410973283dSKonstantin Belousov } else { 1542df8bae1dSRodney W. Grimes /* 15430d94caffSDavid Greenman * Create the top-level object for the destination entry. (Doesn't 15440d94caffSDavid Greenman * actually shadow anything - we copy the pages directly.) 1545df8bae1dSRodney W. Grimes */ 154624a1cce3SDavid Greenman dst_object = vm_object_allocate(OBJT_DEFAULT, 154757b5187bSAlan Cox OFF_TO_IDX(dst_entry->end - dst_entry->start)); 1548f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 1549f8a47341SAlan Cox dst_object->flags |= OBJ_COLORED; 1550f8a47341SAlan Cox dst_object->pg_color = atop(dst_entry->start); 1551f8a47341SAlan Cox #endif 15520973283dSKonstantin Belousov } 1553df8bae1dSRodney W. Grimes 155489f6b863SAttilio Rao VM_OBJECT_WLOCK(dst_object); 1555210a6886SKonstantin Belousov KASSERT(upgrade || dst_entry->object.vm_object == NULL, 1556121fd461SKonstantin Belousov ("vm_fault_copy_entry: vm_object not NULL")); 15570973283dSKonstantin Belousov if (src_object != dst_object) { 1558df8bae1dSRodney W. Grimes dst_entry->object.vm_object = dst_object; 1559df8bae1dSRodney W. Grimes dst_entry->offset = 0; 15603364c323SKonstantin Belousov dst_object->charge = dst_entry->end - dst_entry->start; 15610973283dSKonstantin Belousov } 1562210a6886SKonstantin Belousov if (fork_charge != NULL) { 1563ef694c1aSEdward Tomasz Napierala KASSERT(dst_entry->cred == NULL, 1564121fd461SKonstantin Belousov ("vm_fault_copy_entry: leaked swp charge")); 1565ef694c1aSEdward Tomasz Napierala dst_object->cred = curthread->td_ucred; 1566ef694c1aSEdward Tomasz Napierala crhold(dst_object->cred); 1567121fd461SKonstantin Belousov *fork_charge += dst_object->charge; 15680973283dSKonstantin Belousov } else if (dst_object->cred == NULL) { 15690973283dSKonstantin Belousov KASSERT(dst_entry->cred != NULL, ("no cred for entry %p", 15700973283dSKonstantin Belousov dst_entry)); 1571ef694c1aSEdward Tomasz Napierala dst_object->cred = dst_entry->cred; 1572ef694c1aSEdward Tomasz Napierala dst_entry->cred = NULL; 1573210a6886SKonstantin Belousov } 15740973283dSKonstantin Belousov 1575210a6886SKonstantin Belousov /* 1576210a6886SKonstantin Belousov * If not an upgrade, then enter the mappings in the pmap as 1577210a6886SKonstantin Belousov * read and/or execute accesses. Otherwise, enter them as 1578210a6886SKonstantin Belousov * write accesses. 1579210a6886SKonstantin Belousov * 1580210a6886SKonstantin Belousov * A writeable large page mapping is only created if all of 1581210a6886SKonstantin Belousov * the constituent small page mappings are modified. Marking 1582210a6886SKonstantin Belousov * PTEs as modified on inception allows promotion to happen 1583210a6886SKonstantin Belousov * without taking potentially large number of soft faults. 1584210a6886SKonstantin Belousov */ 1585210a6886SKonstantin Belousov if (!upgrade) 1586210a6886SKonstantin Belousov access &= ~VM_PROT_WRITE; 1587df8bae1dSRodney W. Grimes 1588df8bae1dSRodney W. Grimes /* 1589ef45823eSKonstantin Belousov * Loop through all of the virtual pages within the entry's 1590ef45823eSKonstantin Belousov * range, copying each page from the source object to the 1591ef45823eSKonstantin Belousov * destination object. Since the source is wired, those pages 1592ef45823eSKonstantin Belousov * must exist. In contrast, the destination is pageable. 1593ef45823eSKonstantin Belousov * Since the destination object does share any backing storage 1594ef45823eSKonstantin Belousov * with the source object, all of its pages must be dirtied, 1595ef45823eSKonstantin Belousov * regardless of whether they can be written. 1596df8bae1dSRodney W. Grimes */ 15977afab86cSAlan Cox for (vaddr = dst_entry->start, dst_pindex = 0; 1598df8bae1dSRodney W. Grimes vaddr < dst_entry->end; 15997afab86cSAlan Cox vaddr += PAGE_SIZE, dst_pindex++) { 16000973283dSKonstantin Belousov again: 1601df8bae1dSRodney W. Grimes /* 1602df8bae1dSRodney W. Grimes * Find the page in the source object, and copy it in. 16034c74acf7SKonstantin Belousov * Because the source is wired down, the page will be 16044c74acf7SKonstantin Belousov * in memory. 1605df8bae1dSRodney W. Grimes */ 16060973283dSKonstantin Belousov if (src_object != dst_object) 160783b375eaSAttilio Rao VM_OBJECT_RLOCK(src_object); 1608c5b65a67SAlan Cox object = src_object; 16097afab86cSAlan Cox pindex = src_pindex + dst_pindex; 16107afab86cSAlan Cox while ((src_m = vm_page_lookup(object, pindex)) == NULL && 1611c5b65a67SAlan Cox (backing_object = object->backing_object) != NULL) { 1612c5b65a67SAlan Cox /* 16134c74acf7SKonstantin Belousov * Unless the source mapping is read-only or 16144c74acf7SKonstantin Belousov * it is presently being upgraded from 16154c74acf7SKonstantin Belousov * read-only, the first object in the shadow 16164c74acf7SKonstantin Belousov * chain should provide all of the pages. In 16174c74acf7SKonstantin Belousov * other words, this loop body should never be 16184c74acf7SKonstantin Belousov * executed when the source mapping is already 16194c74acf7SKonstantin Belousov * read/write. 1620c5b65a67SAlan Cox */ 16214c74acf7SKonstantin Belousov KASSERT((src_entry->protection & VM_PROT_WRITE) == 0 || 16224c74acf7SKonstantin Belousov upgrade, 16234c74acf7SKonstantin Belousov ("vm_fault_copy_entry: main object missing page")); 16244c74acf7SKonstantin Belousov 162583b375eaSAttilio Rao VM_OBJECT_RLOCK(backing_object); 1626c5b65a67SAlan Cox pindex += OFF_TO_IDX(object->backing_object_offset); 16270973283dSKonstantin Belousov if (object != dst_object) 162883b375eaSAttilio Rao VM_OBJECT_RUNLOCK(object); 1629c5b65a67SAlan Cox object = backing_object; 1630c5b65a67SAlan Cox } 16314c74acf7SKonstantin Belousov KASSERT(src_m != NULL, ("vm_fault_copy_entry: page missing")); 16320973283dSKonstantin Belousov 16330973283dSKonstantin Belousov if (object != dst_object) { 16340973283dSKonstantin Belousov /* 16350973283dSKonstantin Belousov * Allocate a page in the destination object. 16360973283dSKonstantin Belousov */ 16372602a2eaSKonstantin Belousov dst_m = vm_page_alloc(dst_object, (src_object == 16382602a2eaSKonstantin Belousov dst_object ? src_pindex : 0) + dst_pindex, 16392602a2eaSKonstantin Belousov VM_ALLOC_NORMAL); 16400973283dSKonstantin Belousov if (dst_m == NULL) { 16410973283dSKonstantin Belousov VM_OBJECT_WUNLOCK(dst_object); 16420973283dSKonstantin Belousov VM_OBJECT_RUNLOCK(object); 16430973283dSKonstantin Belousov VM_WAIT; 1644c8f780e3SKonstantin Belousov VM_OBJECT_WLOCK(dst_object); 16450973283dSKonstantin Belousov goto again; 16460973283dSKonstantin Belousov } 1647669890eaSAlan Cox pmap_copy_page(src_m, dst_m); 164883b375eaSAttilio Rao VM_OBJECT_RUNLOCK(object); 1649669890eaSAlan Cox dst_m->valid = VM_PAGE_BITS_ALL; 1650bc79b37fSKonstantin Belousov dst_m->dirty = VM_PAGE_BITS_ALL; 16510973283dSKonstantin Belousov } else { 16520973283dSKonstantin Belousov dst_m = src_m; 16530973283dSKonstantin Belousov if (vm_page_sleep_if_busy(dst_m, "fltupg")) 16540973283dSKonstantin Belousov goto again; 16550973283dSKonstantin Belousov vm_page_xbusy(dst_m); 16560973283dSKonstantin Belousov KASSERT(dst_m->valid == VM_PAGE_BITS_ALL, 16570973283dSKonstantin Belousov ("invalid dst page %p", dst_m)); 16580973283dSKonstantin Belousov } 165989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(dst_object); 1660df8bae1dSRodney W. Grimes 1661df8bae1dSRodney W. Grimes /* 1662210a6886SKonstantin Belousov * Enter it in the pmap. If a wired, copy-on-write 1663210a6886SKonstantin Belousov * mapping is being replaced by a write-enabled 1664210a6886SKonstantin Belousov * mapping, then wire that new mapping. 1665df8bae1dSRodney W. Grimes */ 166639ffa8c1SKonstantin Belousov pmap_enter(dst_map->pmap, vaddr, dst_m, prot, 166739ffa8c1SKonstantin Belousov access | (upgrade ? PMAP_ENTER_WIRED : 0), 0); 1668df8bae1dSRodney W. Grimes 1669df8bae1dSRodney W. Grimes /* 1670df8bae1dSRodney W. Grimes * Mark it no longer busy, and put it on the active list. 1671df8bae1dSRodney W. Grimes */ 167289f6b863SAttilio Rao VM_OBJECT_WLOCK(dst_object); 16732965a453SKip Macy 1674210a6886SKonstantin Belousov if (upgrade) { 16750973283dSKonstantin Belousov if (src_m != dst_m) { 16762965a453SKip Macy vm_page_lock(src_m); 16773ae10f74SAttilio Rao vm_page_unwire(src_m, PQ_INACTIVE); 1678e20e8c15SKonstantin Belousov vm_page_unlock(src_m); 16792965a453SKip Macy vm_page_lock(dst_m); 1680210a6886SKonstantin Belousov vm_page_wire(dst_m); 1681e20e8c15SKonstantin Belousov vm_page_unlock(dst_m); 16822965a453SKip Macy } else { 16830973283dSKonstantin Belousov KASSERT(dst_m->wire_count > 0, 16840973283dSKonstantin Belousov ("dst_m %p is not wired", dst_m)); 16850973283dSKonstantin Belousov } 16860973283dSKonstantin Belousov } else { 16872965a453SKip Macy vm_page_lock(dst_m); 1688df8bae1dSRodney W. Grimes vm_page_activate(dst_m); 1689e20e8c15SKonstantin Belousov vm_page_unlock(dst_m); 16902965a453SKip Macy } 1691c7aebda8SAttilio Rao vm_page_xunbusy(dst_m); 1692df8bae1dSRodney W. Grimes } 169389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(dst_object); 1694210a6886SKonstantin Belousov if (upgrade) { 1695210a6886SKonstantin Belousov dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY); 1696210a6886SKonstantin Belousov vm_object_deallocate(src_object); 1697210a6886SKonstantin Belousov } 1698df8bae1dSRodney W. Grimes } 169926f9a767SRodney W. Grimes 17005730afc9SAlan Cox /* 17015730afc9SAlan Cox * Block entry into the machine-independent layer's page fault handler by 17025730afc9SAlan Cox * the calling thread. Subsequent calls to vm_fault() by that thread will 17035730afc9SAlan Cox * return KERN_PROTECTION_FAILURE. Enable machine-dependent handling of 17045730afc9SAlan Cox * spurious page faults. 17055730afc9SAlan Cox */ 17062801687dSKonstantin Belousov int 17072801687dSKonstantin Belousov vm_fault_disable_pagefaults(void) 17082801687dSKonstantin Belousov { 17092801687dSKonstantin Belousov 17105730afc9SAlan Cox return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR)); 17112801687dSKonstantin Belousov } 17122801687dSKonstantin Belousov 17132801687dSKonstantin Belousov void 17142801687dSKonstantin Belousov vm_fault_enable_pagefaults(int save) 17152801687dSKonstantin Belousov { 17162801687dSKonstantin Belousov 17172801687dSKonstantin Belousov curthread_pflags_restore(save); 17182801687dSKonstantin Belousov } 1719